Skip to content

fs/vfs: Call the unlink operation of a node when its last name goes. - #22585

Merged
xiaoxiang781216 merged 2 commits into
apache:masterfrom
casaroli:fb-shmfs-link-free-up
Dec 5, 2026
Merged

xiaoxiang781216 merged 2 commits into
apache:masterfrom
casaroli:fb-shmfs-link-free-up

Conversation

@casaroli

@casaroli casaroli commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

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 adds INODE_NLINK_INC to 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 at unlink() 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()). When nx_unlink() removes the last hard link of a node that has no other name, it calls the unlink operation of that node after inode_unlock(): the unlink operation of a driver can wait for a thread that takes the inode lock (for example rptun_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: with CONFIG_FS_LINKS, the VFS calls the unlink() method of a driver only when the last name of its node goes.

Changes to link() and rename() with hard links (refuse a message queue or a named semaphore, keep the hard links at rename()) 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 that stat() now gives st_nlink 1 also without CONFIG_FS_LINKS (before: 0). shm_unlink() now calls the unlink operation after the removal of the name (before: before the removal); shmfs_unlink() always returns OK. Without CONFIG_FS_LINKS, INODE_GET_NLINK() gives 1 and INODE_HAS_LINKS() gives false, so the callers need no #ifdef; the hard-link code of nx_unlink() is built but finds no hard link. Size without CONFIG_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_shmlink01 and test_nuttx_fs_fifolink01 in cmocka_fs_test. The FIFO case makes a FIFO, links a second name to it, removes the first name, checks with mallinfo() that the buffer of the FIFO is still allocated, removes the second name, and checks that the buffer is free again.

Configuration (CI image, -Werror, QEMU TCG) master 0c10aa1 this PR
rv-virt:citest (flat, riscv32; a CI configuration) shmlink01 FAIL, fifolink01 FAIL; whole cmocka_fs_test: 49 pass, 2 fail whole cmocka_fs_test: 51 of 51 pass
qemu-armv8a:nsh (flat, arm64) + CM_FS_TEST, FS_SHMFS, PIPES, PSEUDOFS_FILE, FS_LINKS shmlink01 FAIL, fifolink01 FAIL; 49 pass, 2 fail 51 of 51 pass
rv-virt:knsh64 (kernel, riscv64) + FS_SHMFS, PIPES, FS_LINKS ostest: Exiting with status 0

A scratch program on qemu-armv8a:nsh (flat) measures mallinfo().uordblks above 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: after unlink() or shm_unlink() of both names, read() on the open file gives the data, and the memory goes at close(). After all names are gone: 0 in every case.

Builds only (CI image, -Werror): arduino-mega2560:nsh and teensy-2.0:hello (AVR, 16-bit int); sam3u-ek:knsh (protected); qemu-armv8a:nsh + FS_SHMFS, PIPES, PSEUDOFS_FILE with FS_LINKS off; qemu-armv8a:nsh + DISABLE_PSEUDOFS_OPERATIONS: all OK, no warnings.

After the review (no #ifdef CONFIG_FS_LINKS in nx_unlink() and inode_stat()), CI image, -Werror: rv-virt:citest, whole cmocka_fs_test: 51 of 51 pass. Builds with no new warnings: rv-virt:citest with FS_LINKS off; arduino-mega2560:nsh; rv-virt:nsh64 as is, with FS_LINKS, FS_SHMFS, PIPES, PSEUDOFS_FILE, and with DISABLE_PSEUDOFS_OPERATIONS; sam3u-ek:knsh.

tools/checkpatch.sh -c -u -m -g: pass.

GUIDINGLI
GUIDINGLI previously approved these changes Nov 30, 2026
@github-actions github-actions Bot added Area: File System File System issues Size: S The size of the change in this PR is small labels Nov 30, 2026
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +8 B (+0.0%, 74,269 B / 262,144 B, total: 28% used)

esp32-devkitc

  • ROM: .flash.text +144 B (+0.1%, 137,992 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +144 B (+0.1%, 104,112 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +4 B (+0.0%, 88,400 B / 4,194,304 B, total: 2% used)

mps2-an500-citest

  • flash: .text +136 B (+0.1%, 260,836 B / 4,194,304 B, total: 6% used)

mps2-an500-citest-protected

  • flash: .text +136 B (+0.1%, 125,956 B / 4,194,304 B, total: 3% used)

qemu-armv8a

  • Code: .text.inode_stat +8 B, .text.nx_unlink +108 B (+0.1%, 396,508 B)

qemu-intel64

  • Code: .text +223 B (+0.0%, 8,787,759 B)

rx65n-rsk2mb

  • ROM: .text +160 B (+0.2%, 99,196 B / 2,097,152 B, total: 5% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Comment thread fs/inode/fs_inodefree.c Resolved
@casaroli
casaroli force-pushed the fb-shmfs-link-free-up branch from 96a8867 to 5eb4a0e Compare November 30, 2026 16:26
@casaroli casaroli changed the title fs/shm: Free the object of a shm inode when the inode is freed. fs/vfs: Call the unlink operation of a node when its last name goes. Nov 30, 2026
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium and removed Area: File System File System issues Size: S The size of the change in this PR is small labels Nov 30, 2026
@casaroli
casaroli force-pushed the fb-shmfs-link-free-up branch from 5eb4a0e to af16256 Compare December 1, 2026 15:39
Comment thread fs/inode/fs_inoderemove.c Resolved
Comment thread fs/inode/fs_inoderemove.c Resolved
@casaroli
casaroli force-pushed the fb-shmfs-link-free-up branch from af16256 to 824f691 Compare December 2, 2026 00:19
Comment thread fs/inode/inode.h Resolved
@casaroli
casaroli force-pushed the fb-shmfs-link-free-up branch 2 times, most recently from 2abf43c to 7f440a4 Compare December 3, 2026 14:13
@casaroli
casaroli marked this pull request as draft December 4, 2026 10:04
@casaroli
casaroli marked this pull request as ready for review December 4, 2026 10:04
@casaroli
casaroli marked this pull request as draft December 4, 2026 21:16
@casaroli
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
casaroli force-pushed the fb-shmfs-link-free-up branch from 7f440a4 to e5c23eb Compare December 5, 2026 03:24
@xiaoxiang781216
xiaoxiang781216 merged commit a374d06 into apache:master Dec 5, 2026
54 of 59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants