Repository navigation
Conversation
|
7453cd9 to
be1787f
Compare
be1787f to
aa3bb20
Compare
🔗 Cross-repo PR dependenciesThe 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 |
aa3bb20 to
469b143
Compare
469b143 to
0100b12
Compare
|
@casaroli please fix the conflict |
|
Thanks @casaroli, this is a good change. With the current directory in Blocking1.
Should fix2. The A few lines above it, the existing 3. With 4. Cost on small targets / make the tracking optional. Four commits remove user-visible features (NSH Minor / nits
Looks good
Requesting changes for (1). (2) and (3) are small fixes, and (4) is up for discussion. |
0100b12 to
08cc9ee
Compare
|
1, 2, 3: Done. |
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>
08cc9ee to
a6942fa
Compare
|
4 and the nftw() cost: Done. |
Summary
NuttX keeps the current directory in the environment variable
PWD.chdir()is libc code that callssetenv("PWD"),getcwd()returnsgetenv("PWD"), and the kernel joinsgetenv("PWD")to each relative path. So a program that sets or unsetsPWDmoves its current directory (env PWD=/etc lslists/etc), a child started with an empty environment starts in/,chdir()changes nothing withCONFIG_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;PWDis only information for the shell.This PR makes the current directory kernel state of the task group (
tg_cwd):chdir()andfchdir()move tofs/vfs/fs_cwd.cand 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, sogetcwd(NULL, size)allocates in the heap of the caller. Relative lookups (inode_search_setup()) and the*at()functions withAT_FDCWDusetg_cwd, notPWD.chdir()does not writePWDandOLDPWDany more: the shell sets them.rename()of the current directory or of a parent,getcwd()gives the new name; afterrmdir()of it,getcwd()and relative names giveENOENT;umount()of a file system that holds a current directory givesEBUSY. There is no list of current directories:rename(),rmdir()andumount()walk the threads of all task groups withnxsched_foreach()under one kernel mutex. The walk does not allocate or block;rename()gives the new path to one task group after the other.Documentation/os/filesystem/current_directory.rstand updateschroot.rst.CONFIG_FS_CWD_TRACKINGfor these updates: on by default, off withCONFIG_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).CONFIG_FS_CWD_TRACKINGin 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
PWDandOLDPWDaftercdsince apache/nuttx-apps#4061 (merged). The ostest test is apache/nuttx-apps#4062.Impact
What a program sees, in every build mode (flat too):
PWDdoes 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 changePWDorOLDPWD. A program that readsPWDafter its ownchdir()sees the old value;getcwd()gives the directory. The value thatgetcwd()gives is the same as before (chdir()storedrealpath()of the path).rmdir()of the current directory,getcwd()and relative names fail withENOENT(before: the old name, and a new directory of that name became the current directory). Afterrename(),getcwd()gives the new name.umount()of a file system that holds a current directory fails withEBUSY.chdir(),fchdir(),getcwd(),get_current_dir_name(),nftw()withFTW_CHDIR(now onechdir()per object and one return at the end; before, achdir()and asetenv()per object) and thechdir("/")ofdaemon()work withCONFIG_DISABLE_ENVIRON.fchdir()of a descriptor that is not a directory givesENOTDIR.chdir()in one thread changes it for all, as POSIX says. A task fromtask_create(),posix_spawn(),vfork()orfork()starts in the directory of its creator and then has its own, as before. The search permission check ofchdir()is the same code, moved from libc. The kernel threads share one current directory of their own.chdir,fchdir,nx_getcwd), next to the other file system calls; the numbers after them move by three.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 bylib_realpath()andnftw(),chdir()bynftw()anddaemon(), andPWDis read only by the code this PR changes (setenv("PWD")at boot innx_bringup()stays). In nuttx-apps, NSH readsPWDforpwd,cd -and relative arguments (apache/nuttx-apps#4061 sets it aftercdandchroot);ftpc_lpwd()readsPWDonce at connect, which NSH sets; thttpd, bas, readline, vi and the cmocka fs and syscall suites callchdir()andgetcwd()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_CHROOTandCONFIG_FS_TMPFSenabled:cwd_test(apache/nuttx-apps#4062), qemu-armv8a:knsh (kernel)cwd_test, qemu-armv8a:nsh (flat)cwdkernel.c(29 cases), qemu-armv8a:knshcwdchroot.c(5 cases), qemu-armv8a:knshThe cases that still fail:
at-dirfdandat-dirfd-moved(the*at()functions resolve a directory descriptor by its name, a separate change),chdir-eacces(the test needssetuid(), which this configuration does not have), andfstatdir(fstat()of a directory descriptor inside achroot()jail, a separate bug). They fail on master too.cwdkernelalso shows that 1000chdir()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 withCONFIG_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 aftergetprimefrom a race on master in the exit of the last two threads of a group (a lostSIGCHLD), 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
cwdkernelgives 26 PASS, 3 FAIL andcwdchroot4 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 checksrename(),rename()onto an empty directory,rmdir()andumount()of their current directories, and 100 short-lived task groups thatchdir()and exit while the directory is renamed 50 times: all PASS.cwdnftw(nftw()withFTW_CHDIR, absolute and relative paths,FTW_DEPTH) and theumount2()/fchdir()test pass too, andcwdnftwand ostest pass on qemu-armv8a:nsh withCONFIG_FS_CWD_TRACKING=n(the cwd test of apache/nuttx-apps#4062 skips itsrename()andrmdir()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 withCONFIG_DISABLE_MOUNTPOINTand withCONFIG_DEFAULT_SMALL, mps2-an500:nsh withoutCONFIG_FS_IOCTL_FILEPATH.tools/refresh.sh --silentleaves the changed defconfigs as they are.tools/checkpatch.sh -c -u -m -gpasses.