cederom commented on PR #21907:
URL: https://github.com/apache/nuttx/pull/21907#issuecomment-7136488507
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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]