jingfei195887 opened a new pull request, #22116:
URL: https://github.com/apache/nuttx/pull/22116
## Summary
Let a file system take part in inotify, with userfs as the first user.
- **fs/vfs: Add notify callback and notify_event injection API.**
A new optional `notify` method in `struct mountpt_operations` is called
once per watched path: `NOTIFY_WATCH_ADD` returns a file system watch
handle and `NOTIFY_WATCH_RM` passes it back, so the backing store can
start and stop reporting changes. `notify_event()` injects the reported
events into the local queue. While a path has a file system watch,
locally generated events on it are suppressed, so each operation is
reported once.
- **fs/userfs: Forward record locks and watch life cycle to the daemon.**
`fcntl()` locks reach userfs as `FIOC_GETLK/SETLK/SETLKW`, which userfs
forwarded verbatim; the daemon received a `struct flock` pointer it
cannot dereference. The lock ioctls are now marshaled over new
`USERFS_REQ_GETLK/SETLK` requests to optional `getlk`/`setlk` daemon
methods (`FIOC_SETLKW` retries on `-EAGAIN`); a daemon without them
answers `-ENOSYS` and the local lock table is used. The optional
`notify` daemon method (`USERFS_REQ_NOTIFY`) reports the watch life
cycle. `USERFS_REQ_MAXSIZE` grows to 64 for the flock request.
- **fs/vfs: Support event injection through the inotify descriptor.**
With `CONFIG_FS_NOTIFY_INJECT`, one `write()` of a `struct
notify_inject_s` (mask + NUL terminated absolute path) to an inotify
descriptor injects one event.
- **fs/userfs: Add userfs_notify_event injection helper.**
Lets a daemon report spontaneous changes to watched files.
Applied to current master: inotify uses `lib_get_pathbuffer()` and the
`inode_search_setup()` / `inode_find(&desc, &inode)` API; documentation
goes to `Documentation/os/filesystem/`.
## Impact
- New feature / API change: YES. New optional `notify` member at the end
of `struct mountpt_operations` (under `CONFIG_FS_NOTIFY`); new optional
`getlk`, `setlk`, `notify` members at the end of `struct
userfs_operations_s`; new `notify_event()`, `userfs_notify_event()` and
`struct notify_inject_s`. Existing positional initializers still build.
- Behavior: file systems without a `notify` method are unchanged. A userfs
daemon no longer receives raw lock ioctls.
- Security: `CONFIG_FS_NOTIFY_INJECT` (default n) lets any process that
opens an inotify descriptor forge events for any path, and the inotify
descriptor is then opened `O_RDWR`. `FS_USERFS` selects it when
`FS_NOTIFY` is enabled, so that a daemon can report changes. Open to
restricting this if reviewers prefer.
- Build, hardware, documentation (updated), compatibility: see above.
## Testing
Host: Ubuntu 22.04 x86_64, gcc 13.1.0. Base: master 20a363b9404.
`sim:nsh` with `CONFIG_FS_USERFS`, `CONFIG_FS_NOTIFY`, the UDP loopback
network and `CONFIG_FS_LOCK_BUCKET_SIZE=16`. A test app runs a userfs
daemon (`userfs_run()`) that implements `getlk`, `setlk` and `notify`
when they exist, and counts the requests it receives. It opens a file on
the mount, takes `F_SETLK` and `F_GETLK`, adds and removes an inotify
watch on the file, and injects `IN_MODIFY` with `userfs_notify_event()`.
Testing logs before change:
```
userfsfwd: F_SETLK ret=-1 errno=25
userfsfwd: F_GETLK ret=-1 errno=25
userfsfwd: daemon saw lock ioctl=2 setlk=0 getlk=0
userfsfwd: add_watch wd=1 errno=0; daemon ADD=0 path=""
userfsfwd: inject not supported
userfsfwd: daemon RM=0
```
Testing logs after change:
```
userfsfwd: F_SETLK ret=0 errno=0
userfsfwd: F_GETLK ret=0 errno=0
userfsfwd: daemon saw lock ioctl=0 setlk=1 getlk=1
userfsfwd: add_watch wd=1 errno=0; daemon ADD=1 path="f"
userfsfwd: inject ret=0 events=1
userfsfwd: daemon RM=1
```
After the change, with `CONFIG_FS_NOTIFY` off and a daemon without
`getlk`/`setlk`, the locks fall back to the local table as before (this
daemon has no `F_GETPATH`, hence `ENOTTY` as on master), and the daemon
receives no raw lock ioctl:
```
userfsfwd: F_SETLK ret=-1 errno=25
userfsfwd: daemon saw lock ioctl=0 setlk=0 getlk=0
```
- `./tools/checkpatch.sh -g master..HEAD`: all checks pass.
- Make and CMake builds of the configuration above pass with no warnings
in the changed files; `ostest` exits with status 0.
- Every commit builds on its own with `CONFIG_FS_NOTIFY` on and off.
- Verified on sim only.
## PR verification Self-Check
* [x] This PR introduces only one functional change.
* [x] I have updated all required description fields above.
* [x] My PR adheres to Contributing Guidelines and Documentation (git commit
title and message, coding standard, etc).
* [ ] My PR is still work in progress (not ready for review).
* [x] My PR is ready for review and can be safely merged into a codebase.
--
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]