Commit d8e2a8a
committed
fs: implement inotify(2)
inotify was stubbed (inotify_init returned EMFILE, add_watch/rm_watch returned
EINVAL), so programs that watch the filesystem for changes could not run.
Implement it against OSv's VFS. All path mutations route through a handful of
central functions in fs/vfs/vfs_syscalls.cc; those now call osv_inotify_notify()
with the absolute path affected and the event mask. libc/inotify.cc keeps a
registry of inotify instances (each a pollable special_file, modeled on
eventfd), matches every event against their watches, and queues a
struct inotify_event for the fd, waking any poller.
- inotify_init/inotify_init1 create the fd (IN_CLOEXEC/IN_NONBLOCK honored).
- inotify_add_watch resolves the path to absolute, adds (or merges, with
IN_MASK_ADD) a watch, and returns a watch descriptor.
- inotify_rm_watch removes a watch and queues IN_IGNORED.
- read() returns as many packed inotify_event structures as fit in the buffer
(blocking, or EAGAIN when non-blocking), rejecting a too-small buffer with
EINVAL; poll() reports POLLIN when events are queued.
A directory watch fires for changes to entries within it (reporting the entry
name); a watch on the object itself fires with no name. Events wired:
IN_CREATE (mkdir, open O_CREAT), IN_DELETE (rmdir, unlink), IN_MOVED_FROM /
IN_MOVED_TO (rename), each with IN_ISDIR when the object is a directory.
The rename hook rebuilds the full destination path because sys_rename truncates
`dest` to its parent in place before the VOP.
Not yet covered (follow-ups): IN_MODIFY/IN_CLOSE_WRITE on writes (needs an
fd-to-path lookup), IN_ACCESS/IN_OPEN, and recursive watches.
Add tests/tst-inotify.cc covering create/delete/move on files and a
subdirectory (names and IN_ISDIR), IN_IGNORED on watch removal, and the
EAGAIN/EINVAL paths. Passes on OSv under KVM; tst-remove (unlink/rename/rmdir)
continues to pass, so the VFS mutation paths are unaffected.1 parent 3aba46c commit d8e2a8a
5 files changed
Lines changed: 430 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
116 | 119 | | |
117 | 120 | | |
118 | 121 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| 44 | + | |
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
| |||
141 | 142 | | |
142 | 143 | | |
143 | 144 | | |
| 145 | + | |
144 | 146 | | |
145 | 147 | | |
146 | 148 | | |
| |||
568 | 570 | | |
569 | 571 | | |
570 | 572 | | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
571 | 576 | | |
572 | 577 | | |
573 | 578 | | |
| |||
609 | 614 | | |
610 | 615 | | |
611 | 616 | | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
612 | 620 | | |
613 | 621 | | |
614 | 622 | | |
| |||
824 | 832 | | |
825 | 833 | | |
826 | 834 | | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
827 | 848 | | |
828 | 849 | | |
829 | 850 | | |
| |||
1016 | 1037 | | |
1017 | 1038 | | |
1018 | 1039 | | |
| 1040 | + | |
| 1041 | + | |
| 1042 | + | |
1019 | 1043 | | |
1020 | 1044 | | |
1021 | 1045 | | |
| |||
0 commit comments