Skip to content

fs: Keep the current directory in the task group. - #21907

Open
casaroli wants to merge 7 commits into
apache:masterfrom
casaroli:fb-cwd-kernel-up
Open

casaroli wants to merge 7 commits into
apache:masterfrom
casaroli:fb-cwd-kernel-up

Conversation

@casaroli

@casaroli casaroli commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

NuttX keeps the current directory in the environment variable PWD. chdir() is libc code that calls setenv("PWD"), getcwd() returns getenv("PWD"), and the kernel joins getenv("PWD") to each relative path. So a program that sets or unsets PWD moves its current directory (env PWD=/etc ls lists /etc), a child started with an empty environment starts in /, chdir() changes nothing with CONFIG_DISABLE_ENVIRON, and in protected and kernel builds the kernel locks the environment, which is in the memory of the process, for each relative path. POSIX makes the current directory an attribute of the process; PWD is only information for the shell.

This PR makes the current directory kernel state of the task group (tg_cwd):

  • chdir() and fchdir() move to fs/vfs/fs_cwd.c and are system calls. The kernel keeps the canonical absolute path as a string in the kernel heap, one per task group. A new task group gets a copy of the path of its creator. nx_getcwd() is a new system call; getcwd() stays in libc, so getcwd(NULL, size) allocates in the heap of the caller. Relative lookups (inode_search_setup()) and the *at() functions with AT_FDCWD use tg_cwd, not PWD. chdir() does not write PWD and OLDPWD any more: the shell sets them.
  • The next commit follows changes of the directory: after rename() of the current directory or of a parent, getcwd() gives the new name; after rmdir() of it, getcwd() and relative names give ENOENT; umount() of a file system that holds a current directory gives EBUSY. There is no list of current directories: rename(), rmdir() and umount() walk the threads of all task groups with nxsched_foreach() under one kernel mutex. The walk does not allocate or block; rename() gives the new path to one task group after the other.
  • The next commit adds Documentation/os/filesystem/current_directory.rst and updates chroot.rst.
  • The next commit adds CONFIG_FS_CWD_TRACKING for these updates: on by default, off with CONFIG_DEFAULT_SMALL. Without it the current directory keeps its path, as before. It costs 1720 B on mps2-an500:nsh and 1176 B on imx93-qsb:rpmsg (Arm GNU 13.2).
  • The last three commits disable CONFIG_FS_CWD_TRACKING in the configurations that do not fit with it: b-g431b-esc1:cansock and nucleo-g431rb:cansock (in CI 876 and 660 B were free before this series; 2796 and 2804 B free here with the option off), imx93-qsb:rpmsg (1232 B of ITCM free; 40 B with the option on) and sam3u-ek:knsh, which also needs the System V message queues off (1884 B of kflash free; with the option off only, it overflows by 20 B). No NSH command is removed.

NSH sets PWD and OLDPWD after cd since apache/nuttx-apps#4061 (merged). The ostest test is apache/nuttx-apps#4062.

Impact

What a program sees, in every build mode (flat too):

  • Setting, unsetting or clearing PWD does not move the current directory. A child started with an empty environment (execve(), posix_spawn()) starts in the directory of its parent, not in /.
  • chdir() does not change PWD or OLDPWD. A program that reads PWD after its own chdir() sees the old value; getcwd() gives the directory. The value that getcwd() gives is the same as before (chdir() stored realpath() of the path).
  • After rmdir() of the current directory, getcwd() and relative names fail with ENOENT (before: the old name, and a new directory of that name became the current directory). After rename(), getcwd() gives the new name. umount() of a file system that holds a current directory fails with EBUSY.
  • chdir(), fchdir(), getcwd(), get_current_dir_name(), nftw() with FTW_CHDIR (now one chdir() per object and one return at the end; before, a chdir() and a setenv() per object) and the chdir("/") of daemon() work with CONFIG_DISABLE_ENVIRON. fchdir() of a descriptor that is not a directory gives ENOTDIR.
  • Not changed: the threads of a task group share one current directory, as before (the environment was per task group too): a chdir() in one thread changes it for all, as POSIX says. A task from task_create(), posix_spawn(), vfork() or fork() starts in the directory of its creator and then has its own, as before. The search permission check of chdir() is the same code, moved from libc. The kernel threads share one current directory of their own.
  • The relative lookup no longer locks the environment of the process from the kernel; it takes a kernel mutex instead.
  • Protected and kernel builds: three new system calls (chdir, fchdir, nx_getcwd), next to the other file system calls; the numbers after them move by three.
  • Size (text, Arm GNU 13.2 / xPack RISC-V 14.3, on master b27c838): mps2-an500:nsh (Cortex-M4, flat) +2480 B; rv-virt:knsh64 (kernel) +2874 B; rv-virt:pnsh64 (protected) +2848 B; qemu-armv8a:citest_smp +2464 B. The updates of the current directory (CONFIG_FS_CWD_TRACKING) are 1720 B of this on mps2-an500:nsh (measured on master c68c0bf).

In-tree users: in nuttx, getcwd() is used by lib_realpath() and nftw(), chdir() by nftw() and daemon(), and PWD is read only by the code this PR changes (setenv("PWD") at boot in nx_bringup() stays). In nuttx-apps, NSH reads PWD for pwd, cd - and relative arguments (apache/nuttx-apps#4061 sets it after cd and chroot); ftpc_lpwd() reads PWD once at connect, which NSH sets; thttpd, bas, readline, vi and the cmocka fs and syscall suites call chdir() and getcwd() and need no change.

Testing

QEMU with HVF, nuttx master b27c838 against this PR (aa3bb20 on b27c838), CONFIG_PIPES, CONFIG_FS_LINKS, CONFIG_PSEUDOFS_SOFTLINKS, CONFIG_FS_CHROOT and CONFIG_FS_TMPFS enabled:

Test master this PR
ostest cwd_test (apache/nuttx-apps#4062), qemu-armv8a:knsh (kernel) 4 errors (PWD set, PWD unset, rename, rmdir), ostest asserts PASS, ostest status 0
ostest cwd_test, qemu-armv8a:nsh (flat) the same 4 errors, ostest asserts PASS, ostest status 0
test program cwdkernel.c (29 cases), qemu-armv8a:knsh 11 PASS, 18 FAIL 26 PASS, 3 FAIL
test program cwdchroot.c (5 cases), qemu-armv8a:knsh 1 PASS, 4 FAIL 4 PASS, 1 FAIL

The cases that still fail: at-dirfd and at-dirfd-moved (the *at() functions resolve a directory descriptor by its name, a separate change), chdir-eacces (the test needs setuid(), which this configuration does not have), and fstatdir (fstat() of a directory descriptor inside a chroot() jail, a separate bug). They fail on master too. cwdkernel also shows that 1000 chdir() calls and 200 children leave the kernel heap as it was.

NSH on qemu-armv8a:nsh (flat) with this PR and apps master (with apache/nuttx-apps#4061): cd /proc, pwd (/proc), echo $PWD $OLDPWD (/proc /), cat version (relative name), cd -, cd /dev, ls null, cd ..: all right.

Builds with -Werror, 0 warnings, master and this PR: rv-virt:pnsh64 (protected, rv64), rv-virt:knsh64 (kernel, rv64), mps2-an500:nsh, qemu-armv8a:nsh with CONFIG_DISABLE_ENVIRON, qemu-armv8a:citest_smp, sam3u-ek:knsh, imx93-qsb:rpmsg, verdin-mx8mp:rpmsg. qemu-armv8a:citest_smp with NTFC in the CI docker image: a run can hang after getprime from a race on master in the exit of the last two threads of a group (a lost SIGCHLD), fixed in #22005; with that fix, 8 of 8 runs pass. In the CI docker image (GNU 13.2, clang for the stm32l4 and stm32h7 boards, as the CI lists), this PR on 5cc3bde links: nucleo-l432kc:wgen (62.40% of flash), nucleo-l432kc:nsh (62.43%), b-g431b-esc1:nsh (53.02%), nucleo-g431kb:nsh (52.99%), nucleo-g431rb:nsh (24.95%), weact-stm32h750:sdcard (97.59%), ea3131:pgnsh. mps2-an500:citest and citest_protected build too.

After the review (0100b12 on master 6a6b5fb: each task group has its own copy of the directory, in one allocation): on qemu-armv8a:knsh and qemu-armv8a:nsh, ostest with the cwd test of apache/nuttx-apps#4062 exits with status 0; on mps2-an500:citest_protected (protected, QEMU TCG) two ostest runs with the cwd test exit with status 0; on knsh cwdkernel gives 26 PASS, 3 FAIL and cwdchroot 4 PASS, 1 FAIL, the same cases as before (rename, rmdir and umount by another process pass). Builds with -Werror, 0 warnings: the configurations above, and sim:nsh with Make and CMake in the CI docker image. Size against the previous push, CI docker image: sam3u-ek:knsh +36 B (1540 B of kflash free), b-g431b-esc1:cansock +104 B (1732 B free), nucleo-g431rb:cansock +104 B (1516 B free), weact-stm32h750:sdcard +64 B (2744 B free), verdin-mx8mp:rpmsg -48 B; imx93-qsb:rpmsg +48 B (1388 B of ITCM free, local Arm GNU 13.2).

After the second review (on master c9bc7af: the walk of the task groups instead of a list, CONFIG_FS_CWD_TRACKING, nftw(), the board commits): ostest with the cwd test exits with status 0 on qemu-armv8a:nsh, qemu-armv8a:nsh_smp (2 CPUs), rv-virt:knsh64 and rv-virt:ksmp64 (4 CPUs). A new test program, cwdgroups, starts other task groups (task_create() in flat builds, fork() in kernel builds) and checks rename(), rename() onto an empty directory, rmdir() and umount() of their current directories, and 100 short-lived task groups that chdir() and exit while the directory is renamed 50 times: all PASS. cwdnftw (nftw() with FTW_CHDIR, absolute and relative paths, FTW_DEPTH) and the umount2()/fchdir() test pass too, and cwdnftw and ostest pass on qemu-armv8a:nsh with CONFIG_FS_CWD_TRACKING=n (the cwd test of apache/nuttx-apps#4062 skips its rename() and rmdir() checks then). Builds with -Werror, 0 new warnings: the board configurations above, mps2-an500:nsh, rv-virt:pnsh64, rv-virt:knsh64, mps2-an500:citest_protected, qemu-armv8a:citest_smp, qemu-armv8a:knsh, qemu-armv8a:nsh with CONFIG_DISABLE_MOUNTPOINT and with CONFIG_DEFAULT_SMALL, mps2-an500:nsh without CONFIG_FS_IOCTL_FILEPATH. tools/refresh.sh --silent leaves the changed defconfigs as they are.

tools/checkpatch.sh -c -u -m -g passes.

cederom
cederom previously approved these changes Nov 16, 2026
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. Board: arm labels Nov 16, 2026
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .data +11 B, .text +3,894 B, .trampolines +36 B (+6.1%, 68,808 B / 262,144 B, total: 26% used)
  • sram: .data +11 B (+0.4%, 2,779 B / 8,192 B, total: 34% used)

esp32-devkitc

  • ROM: .dram0.data +16 B, .flash.rodata +8 B, .flash.text +1,784 B (+1.4%, 135,572 B / 4,194,272 B, total: 3% used)
  • dram0_0_seg: .dram0.data +16 B (+0.2%, 10,292 B / 180,736 B, total: 6% used)
  • drom0_0_seg: .flash.rodata +8 B (+0.1%, 14,092 B / 4,194,272 B, total: 0% used)
  • irom0_0_seg: .flash.text +1,784 B (+1.8%, 101,200 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .data +16 B, .text +484 B (+0.6%, 87,404 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .data +16 B, .text +660 B (+1.0%, 67,032 B / 131,072 B, total: 51% used)
  • kseg1_datamem: .data +16 B (+0.6%, 2,652 B / 32,256 B, total: 8% used)

mps2-an500-citest

  • flash: .data +16 B, .text +1,704 B (+0.7%, 249,696 B / 4,194,304 B, total: 6% used)
  • sram1: .bss +8 B, .data +16 B (+0.1%, 16,332 B / 16,777,216 B, total: 0% used)

mps2-an500-citest-protected

  • flash: .data +16 B, .text +2,304 B (+2.0%, 119,192 B / 4,194,304 B, total: 3% used)
  • sram1: .bss +8 B, .data +16 B (+0.3%, 8,400 B / 16,777,216 B, total: 0% used)

qemu-armv7a-citest

  • ROM: .data +112 B, .init_section +1,600 B, .text +2,496 B (+0.8%, 558,296 B / 1,048,576 B, total: 53% used)

qemu-armv7a-citest-knsh

  • ROM: .data +16 B, .init_section -2,488 B, .text +2,488 B (+0.0%, 161,184 B / 1,048,576 B, total: 15% used)

qemu-armv8a

  • Code: .rodata +16 B, .text.chdir -100 B, .text.getcwd -28 B, .text.group_allocate +32 B, .text.group_leave +8 B, .text.inode_search_setup +40 B, .text.nx_umount2 +168 B, .text.rename +84 B, .text.rmdir +8 B (+0.7%, 381,016 B)
  • Data: .bss.g_kthread_group +8 B (+0.0%, 78,289 B)

qemu-intel64

  • Code: .text +3,315 B (+0.0%, 8,710,651 B)

rx65n-rsk2mb

  • RAM: .data +16 B (+0.2%, 6,944 B / 262,144 B, total: 3% used)
  • ROM: .rodata -4 B, .text +2,288 B (+2.4%, 95,540 B / 2,097,152 B, total: 5% used)

s698pm-dkit

  • Code: .text +7,120 B (+1.7%, 418,016 B)
  • Data: .data +16 B (+0.1%, 12,176 B)

stm32-nucleo-f103rb

  • flash: .data +16 B, .text +328 B (+1.0%, 35,516 B / 131,072 B, total: 27% used)
  • sram: .data +16 B (+0.6%, 2,524 B / 20,480 B, total: 12% used)

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

@casaroli
casaroli force-pushed the fb-cwd-kernel-up branch 2 times, most recently from 7453cd9 to be1787f Compare November 18, 2026 07:38
@casaroli
casaroli marked this pull request as ready for review November 18, 2026 07:39
@casaroli
casaroli marked this pull request as draft November 18, 2026 08:05
@casaroli
casaroli marked this pull request as ready for review November 18, 2026 08:05
@casaroli
casaroli requested a review from raiden00pl as a code owner November 18, 2026 13:07
@casaroli
casaroli marked this pull request as draft November 18, 2026 14:13
@casaroli
casaroli marked this pull request as ready for review November 18, 2026 14:13
@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx/actions/runs/44321927865

Comment thread libs/libc/unistd/lib_daemon.c Resolved
Comment thread libs/libc/dirent/lib_nftw.c Resolved
Comment thread fs/vfs/Make.defs Resolved
Comment thread fs/vfs/fs_cwd.c Resolved
Comment thread fs/vfs/fs_cwd.c Resolved
Comment thread fs/vfs/fs_cwd.c Resolved
Comment thread fs/mount/fs_umount2.c Resolved
Comment thread fs/vfs/fs_cwd.c Resolved
Comment thread fs/vfs/fs_cwd.c Resolved
Comment thread fs/vfs/fs_cwd.c Resolved
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@casaroli please fix the conflict

@cederom

cederom commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Thanks @casaroli, this is a good change. With the current directory in tg_cwd, env PWD=... ls and env -i pwd can no longer move it, chdir() works with CONFIG_DISABLE_ENVIRON, and the kernel no longer takes the user-space environment mutex for every relative lookup. The board commits come before the fs commits, so every step of the series still builds. I read the whole series at 0100b12. Comments below, most important first.

Blocking

1. nx_umount2() returns EBUSY even with MNT_FORCE. This breaks the unmount-all at reboot. (fs/mount/fs_umount2.c:210)

umount_all_handler() calls nx_umount2(mountpoint, MNT_FORCE) to sync and unmount every volume before a reboot. The new fs_cwd_busy() check comes before syncfs, and it ignores flags. So after cd /mnt/sd in NSH, reboot neither unmounts nor syncs /mnt/sd. That is the most common case, because the task doing the reboot is usually the shell that sits in that directory. Please skip the check when flags & (MNT_FORCE | MNT_DETACH), or at least do it after syncfs. With a forced unmount, the affected current directories could also be marked removed, so they return ENOENT instead of pointing into an empty pseudo directory.

Should fix

2. The fs_cwd_removed() call added to nx_unlink() can never run. (fs/vfs/fs_unlink.c:196)

A few lines above it, the existing else if (INODE_IS_PSEUDODIR(inode)) { ret = -EISDIR; goto errout_with_inode; } already rejects pseudo directories. Either drop the hunk and the "unlink() of an empty directory of the pseudo file system" line in the commit message, or, if unlink() of an empty pseudo directory is meant to work, change that branch. The first seems right: rmdir() already covers it.

3. fchdir() maps every F_GETPATH failure to ENOTDIR. (fs/vfs/fs_cwd.c:921)

With CONFIG_FS_IOCTL_FILEPATH=n (the default under DEFAULT_SMALL), F_GETPATH returns -ENOTTY for every descriptor. fchdir() on a real directory then reports ENOTDIR, which is misleading. The old libc fchdir() passed the error through. Please map only the "no path" case to ENOTDIR and return something like ENOSYS/ENOTSUP when the feature is compiled out. The new doc page should say that fchdir() needs CONFIG_FS_IOCTL_FILEPATH.

4. Cost on small targets / make the tracking optional.

Four commits remove user-visible features (NSH cp/mv/rm/mkdir/... on verdin-mx8mp, SysV message queues on sam3u-ek:knsh, NET_STATISTICS/du/uptime on the G4 cansock configs) to make room for about 1.4 to 2 KiB that every config now pays, including configs that never call chdir(). Most of that is the rename/rmdir/umount tracking in the second fs commit (fs_cwd_hostpath(), the list walks, the inode_getpath() call in umount). I'd suggest putting that commit behind a Kconfig option, e.g. FS_CWD_TRACKING with default !DEFAULT_SMALL, keeping the simple "kernel keeps the path" behaviour when it is off. That would also answer @xiaoxiang781216's question about the simpler version, and the board defconfig changes might not be needed at all. If the board commits stay, the board maintainers should ack them.

Minor / nits

  • cwd_rename() silently keeps the old name when fs_heap_malloc() fails. That is reasonable, but please add it to the "Limits" list in current_directory.rst together with the chdir()/rename() race.
  • fs_cwd_renamed() and fs_cwd_removed() hold two to three temp buffers while rename() still holds its own search buffers, and lib_realpath() takes more. With the default LIBC_MAX_TEMPBUFFER=2 this falls through to kmm_malloc(). That's fine with LIBC_TEMPBUFFER_MALLOC=y, but with it off the update silently does nothing. Worth a comment.
  • nftw(FTW_CHDIR) now costs a getcwd() plus two chdir() system calls per entry, each with realpath() and a kernel allocation. The chdir(cwd) return value is ignored on restore, as before. Is that acceptable for large trees?
  • A chdir() in a kernel thread changes the directory of all kernel threads (shared g_kthread_group). It's documented, but a DEBUGASSERT or a note in chdir() might save someone a surprise.
  • inode_search_setup(): buflen still adds rootlen for relative paths, which fs_cwd_join() no longer prepends when tg_cwd != NULL. Harmless, but the comment should say so.
  • sched/init/nx_bringup.c still does setenv("PWD", CONFIG_LIBC_HOMEDIR). That is fine now that PWD is informational, but a one-line comment would help.
  • .github/CODEOWNERS still lists libs/libc/unistd/lib_restoredir.c, lib_chdir.c and lib_fchdir.c.
  • The verdin-mx8mp commit message says the board "runs from the 128 KiB ITCM" but quotes "region `flash' overflowed". Please make them match.
  • The branch conflicts with master (as already noted).

Looks good

  • The lock order is consistent: g_cwd_lock is only ever taken inside the inode lock (umount) or with no other lock held, and it is never held across realpath()/stat().
  • cwd_rename() uses list_for_every_entry_safe and inserts the replacement after the current node, so the moved entry is never reprocessed (it would otherwise match newpath and be marked removed). That's subtle but correct, and worth a one-line comment.
  • The chroot handling is sound: the host path is stored, fs_cwd_join() refuses a current directory outside tg_root under the lock, and dst_min still stops .. at the root.
  • The CONFIG_FS_PERMISSION search check from the old libc chdir() is kept.
  • The documentation is clear and matches the code.

Requesting changes for (1). (2) and (3) are small fixes, and (4) is up for discussion.

@casaroli

Copy link
Copy Markdown
Contributor Author

1, 2, 3: Done.

@github-actions github-actions Bot added Area: CI and removed Area: Documentation Improvements or additions to documentation labels Nov 24, 2026
The current directory was the environment variable PWD.  chdir() was
libc code that called setenv("PWD"), getcwd() returned getenv("PWD"),
and the kernel joined getenv("PWD") to each relative path.  So:

- A program that set or unset PWD moved its current directory:
  "env PWD=/etc ls" listed /etc, and "env -i pwd" printed "/".  POSIX
  makes the current directory an attribute of the process.  PWD is only
  information for the shell.
- With CONFIG_DISABLE_ENVIRON, chdir() changed nothing.
- In protected and kernel builds, each relative path locked the
  environment mutex, which is in the memory of the process, from the
  kernel.

Now the current directory is kernel state of the task group, tg_cwd.
NULL is the default, CONFIG_LIBC_HOMEDIR inside the root of the group,
as before.  chdir() allocates the canonical absolute path (no ".", ".."
or symbolic links) in the kernel heap, as a string.  A new task group
gets a copy of the directory of its creator (fork(), vfork(),
posix_spawn(), task_create() and exec).  The threads of a group use the
same directory.  Kernel threads keep their own.

- chdir() and fchdir() move to fs/vfs/fs_cwd.c and are system calls.
  fchdir() checks that the descriptor is a directory.
- nx_getcwd() is a new system call.  getcwd() stays in libc, because
  getcwd(NULL, size) allocates in the heap of the caller.  getcwd()
  fails with ENOENT if the directory is outside the chroot() root.
- The lookup of a relative path (inode_search_setup()) joins the
  kernel state, not $PWD.  A directory outside the root gives ENOENT,
  so relative names stay inside a chroot() jail, as before.
- lib_getfullpath() gives a relative path with AT_FDCWD to the kernel
  unchanged.
- nftw() with FTW_CHDIR calls chdir() once for each object and goes
  back to the first directory once at the end (getcwd() and chdir()).
  Before, it called chdir() and lib_restoredir() for each object.  A
  relative path gets the current directory in front of it for the walk.
  lib_restoredir() is removed.
- chdir(), fchdir(), getcwd(), get_current_dir_name(), FTW_CHDIR and
  the chdir("/") of daemon() do not depend on CONFIG_DISABLE_ENVIRON
  any more.

chdir() does not write PWD or OLDPWD any more.  A shell sets them, as
on other systems.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
A current directory is a path.  Before, a rename() of the directory or
of a parent left the old name: getcwd() gave it and relative paths used
it.  A removed directory was still the current directory by its name,
and a new directory of that name became the current directory.
umount() of the file system that held it succeeded.  On other systems
the current directory is the directory itself.

Now the VFS updates the current directories:

- rename(): each current directory at or below the old path gets the
  new path.  A current directory that the rename replaced (an empty
  directory) is removed.
- rmdir(): a current directory at that path is removed.  getcwd()
  fails with ENOENT and a relative path gives ENOENT, also after a new
  directory of that name appears.
- umount2() fails with EBUSY if a current directory is at or below the
  mount point, except with MNT_FORCE or MNT_DETACH: the unmount of all
  file systems before a reboot uses MNT_FORCE and must sync and unmount
  each one.  After such an unmount, a current directory in the file
  system is removed, as after rmdir().  A current directory in another
  file system mounted below the mount point (mounted before it, and so
  still mounted after the unmount) does not change.

There is no list of current directories.  The VFS walks the threads of
all task groups with nxsched_foreach() under one kernel mutex, which
also guards tg_cwd of each group.  The walk does not allocate or block:
it marks a directory removed (an empty string), or it returns one task
group whose directory moves, and rename() gives the new path to one
group after the other.  The walk skips the threads that exit.  The
group of any other thread stays: its release takes the same mutex.

The paths are compared in their canonical form.  After a successful
rename() of a directory or rmdir(), and only when a task group has set
a current directory, the VFS resolves the directory of each name with
realpath() and adds the last name.  A rename() of a file costs nothing
more.  A change that does not go through this VFS (another host changes
a shared file system) is not seen.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
The new page os/filesystem/current_directory.rst says what chdir(),
fchdir() and getcwd() do, how a task group inherits the directory, that
the shell sets PWD, what happens after rename(), rmdir() and umount(),
and the limits of a directory that the kernel keeps as a path.

chroot.rst said that the current directory is the PWD path, read inside
the root.  Now it is kernel state: a directory outside the root gives
ENOENT for relative paths and getcwd().

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
The updates of the current directories after rename(), rmdir() and
umount() cost code in each configuration, also in one that never calls
chdir(): the path resolution of the changed directory, the walk of the
task groups, and the path of the mount point in umount().

The new option CONFIG_FS_CWD_TRACKING enables them.  It is on by
default, and off with CONFIG_DEFAULT_SMALL.  Without it, the current
directory keeps its path, as before the updates: after rename() a
relative path uses the old name, after rmdir() and mkdir() of the same
name it uses the new directory, and umount() does not look at the
current directories.

With Arm GNU 13.2 the option costs 1720 bytes of flash on
mps2-an500:nsh and 1176 bytes of ITCM on imx93-qsb:rpmsg.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
b-g431b-esc1:cansock and nucleo-g431rb:cansock have 128 KiB of flash
and little of it free: in CI, 876 and 660 bytes were free before this
series.  The kernel state of the current directory (fs/vfs/fs_cwd.c)
and its updates after rename(), rmdir() and umount() add about 1.2 KiB,
so the images do not fit in CI.

Disable CONFIG_FS_CWD_TRACKING.  The current directory then keeps its
path after rename() and rmdir(), as before.  This saves 1192 bytes:
2796 and 2804 bytes of flash are free with Arm GNU 13.2 here.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
The kernel flash (kflash, 80 KiB) of sam3u-ek:knsh is almost full.  With
the kernel state of the current directory (fs/vfs/fs_cwd.c) and its
updates after rename(), rmdir() and umount(), the link fails: "region
`kflash' overflowed by 1152 bytes" (Arm GNU 13.2).

Disable CONFIG_FS_CWD_TRACKING.  That alone is not enough ("overflowed
by 20 bytes"), so disable the System V message queues too: nothing in
this configuration uses msgget(), msgsnd(), msgrcv() or msgctl().  Then
1884 bytes of kflash are free.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
imx93-qsb:rpmsg runs from the 126 KiB ITCM.  With the kernel state of
the current directory (fs/vfs/fs_cwd.c) and its updates after rename(),
rmdir() and umount(), 40 bytes of the ITCM are free (Arm GNU 13.2):
one more feature and the link fails.

Disable CONFIG_FS_CWD_TRACKING.  The current directory then keeps its
path after rename() and rmdir(), as before.  Then 1232 bytes of the
ITCM are free.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@casaroli

Copy link
Copy Markdown
Contributor Author

4 and the nftw() cost: Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: CI Board: arm Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants