Repository navigation
fs/vfs: Call the unlink operation of a node when its last name goes. - #22585
Merged
Merged
Conversation
GUIDINGLI
previously approved these changes
Nov 30, 2026
|
casaroli
force-pushed
the
fb-shmfs-link-free-up
branch
from
November 30, 2026 16:26
96a8867 to
5eb4a0e
Compare
casaroli
requested review from
acassis,
btashton,
pkarashchenko and
pussuw
as code owners
November 30, 2026 16:26
casaroli
force-pushed
the
fb-shmfs-link-free-up
branch
from
December 1, 2026 15:39
5eb4a0e to
af16256
Compare
casaroli
force-pushed
the
fb-shmfs-link-free-up
branch
from
December 2, 2026 00:19
af16256 to
824f691
Compare
casaroli
force-pushed
the
fb-shmfs-link-free-up
branch
2 times, most recently
from
December 3, 2026 14:13
2abf43c to
7f440a4
Compare
xiaoxiang781216
approved these changes
Dec 3, 2026
casaroli
marked this pull request as draft
December 4, 2026 10:04
casaroli
marked this pull request as ready for review
December 4, 2026 10:04
acassis
approved these changes
Dec 4, 2026
casaroli
marked this pull request as draft
December 4, 2026 21:16
casaroli
marked this pull request as ready for review
December 4, 2026 21:16
A hard link (link() in the pseudo file system) is a second name of a driver, FIFO or shm node. It adds INODE_NLINK_INC to the reference count of the node. Before, the VFS called the unlink operation of the node at the removal of its original name only, and never at the removal of a hard link: - A FIFO, a pseudo file or a driver freed its object at unlink() of the original name when no file was open, also while a hard link still named the node. The hard link then named an inode whose private data was freed. - shmfs_unlink() did not free a shm object while a hard link existed, and nothing freed it when the hard link went: the object and its memory were lost. Now: - nx_unlink() does not call the unlink operation while a hard link still names the node (INODE_HAS_LINKS()). - When nx_unlink() removes the last hard link of a node that has no other name, it calls the unlink operation of the node. It holds a reference to the node while the link goes, and calls the operation after inode_unlock(): the unlink operation of a driver can wait for a thread that takes the inode lock. A static helper, unlink_notify(), calls the unlink operation in both places. - shm_unlink() calls the unlink operation after the removal of the name, when the object has no name left. - shmfs_unlink() counts the reference of the name only while the name is in the tree. Without CONFIG_FS_LINKS, INODE_GET_NLINK() gives 1 and INODE_HAS_LINKS() gives false, so the callers need no #ifdef. stat() of a node in the pseudo file system then gives st_nlink 1 (before: 0). Apart from st_nlink, a node without hard links behaves as before. The change is the same in flat, protected and kernel builds. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
With CONFIG_FS_LINKS, a hard link is a second name of a device node. The VFS calls the unlink() method of the driver only when the last name of the node goes. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
casaroli
force-pushed
the
fb-shmfs-link-free-up
branch
from
December 5, 2026 03:24
7f440a4 to
e5c23eb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A hard link (
link()in the pseudo file system,CONFIG_FS_LINKS) is a second name of a driver, FIFO or shm node. It addsINODE_NLINK_INCto the reference count of the node.nx_unlink()called the unlink operation of the node at the removal of its original name only, and never at the removal of a hard link. So a FIFO, a pseudo file or a driver freed its object atunlink()of the original name when no file was open, also while a hard link still named the node. And a shm object with a hard link was never freed:shmfs_unlink()did not free it while the link existed, and nothing freed it when the link went.Now the unlink operation runs when the last name of the node goes.
nx_unlink()does not call it while a hard link still names the node (INODE_HAS_LINKS()). Whennx_unlink()removes the last hard link of a node that has no other name, it calls the unlink operation of that node afterinode_unlock(): the unlink operation of a driver can wait for a thread that takes the inode lock (for examplerptun_dev_unlink()). A static helper,unlink_notify(), calls the unlink operation in both places.shm_unlink()calls the unlink operation after the removal of the name, when the object has no name left.shmfs_unlink()counts the reference of the name only while the name is in the tree.The second commit updates
Documentation/guides/nsh/remove_device_drivers_nsh.rst: withCONFIG_FS_LINKS, the VFS calls theunlink()method of a driver only when the last name of its node goes.Changes to
link()andrename()with hard links (refuse a message queue or a named semaphore, keep the hard links atrename()) come in a follow-up PR.Impact
Only nodes in the pseudo file system with a hard link (
CONFIG_FS_LINKS). A node without hard links behaves as before, except thatstat()now givesst_nlink1 also withoutCONFIG_FS_LINKS(before: 0).shm_unlink()now calls the unlink operation after the removal of the name (before: before the removal);shmfs_unlink()always returnsOK. WithoutCONFIG_FS_LINKS,INODE_GET_NLINK()gives 1 andINODE_HAS_LINKS()gives false, so the callers need no#ifdef; the hard-link code ofnx_unlink()is built but finds no hard link. Size withoutCONFIG_FS_LINKS: sam3u-ek:knsh kflash 66880 B, master 66780 B; rv-virt:nsh64 text 250122 B, master 250002 B. The same in flat, protected and kernel builds. No API change.Testing
The apps PR apache/nuttx-apps#4211 has
test_nuttx_fs_shmlink01andtest_nuttx_fs_fifolink01incmocka_fs_test. The FIFO case makes a FIFO, links a second name to it, removes the first name, checks withmallinfo()that the buffer of the FIFO is still allocated, removes the second name, and checks that the buffer is free again.-Werror, QEMU TCG)cmocka_fs_test: 49 pass, 2 failcmocka_fs_test: 51 of 51 passCM_FS_TEST,FS_SHMFS,PIPES,PSEUDOFS_FILE,FS_LINKSFS_SHMFS,PIPES,FS_LINKSExiting with status 0A scratch program on qemu-armv8a:nsh (flat) measures
mallinfo().uordblksabove the start, 8 rounds per case, after unlink of the first name while the hard link exists: FIFO with a link: master 192 (the 1184-byte FIFO object is freed), this PR 1376 (kept); pseudo file with a link: master 192, this PR 2304. A shm object with a hard link and an open file: afterunlink()orshm_unlink()of both names,read()on the open file gives the data, and the memory goes atclose(). After all names are gone: 0 in every case.Builds only (CI image,
-Werror): arduino-mega2560:nsh and teensy-2.0:hello (AVR, 16-bitint); sam3u-ek:knsh (protected); qemu-armv8a:nsh +FS_SHMFS,PIPES,PSEUDOFS_FILEwithFS_LINKSoff; qemu-armv8a:nsh +DISABLE_PSEUDOFS_OPERATIONS: all OK, no warnings.After the review (no
#ifdef CONFIG_FS_LINKSinnx_unlink()andinode_stat()), CI image,-Werror: rv-virt:citest, wholecmocka_fs_test: 51 of 51 pass. Builds with no new warnings: rv-virt:citest withFS_LINKSoff; arduino-mega2560:nsh; rv-virt:nsh64 as is, withFS_LINKS,FS_SHMFS,PIPES,PSEUDOFS_FILE, and withDISABLE_PSEUDOFS_OPERATIONS; sam3u-ek:knsh.tools/checkpatch.sh -c -u -m -g: pass.