inotify.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. inotify
  2. a powerful yet simple file change notification system
  3. Document started 15 Mar 2005 by Robert Love <rml@novell.com>
  4. (i) User Interface
  5. Inotify is controlled by a set of three system calls and normal file I/O on a
  6. returned file descriptor.
  7. First step in using inotify is to initialise an inotify instance:
  8. int fd = inotify_init ();
  9. Each instance is associated with a unique, ordered queue.
  10. Change events are managed by "watches". A watch is an (object,mask) pair where
  11. the object is a file or directory and the mask is a bit mask of one or more
  12. inotify events that the application wishes to receive. See <linux/inotify.h>
  13. for valid events. A watch is referenced by a watch descriptor, or wd.
  14. Watches are added via a path to the file.
  15. Watches on a directory will return events on any files inside of the directory.
  16. Adding a watch is simple:
  17. int wd = inotify_add_watch (fd, path, mask);
  18. Where "fd" is the return value from inotify_init(), path is the path to the
  19. object to watch, and mask is the watch mask (see <linux/inotify.h>).
  20. You can update an existing watch in the same manner, by passing in a new mask.
  21. An existing watch is removed via
  22. int ret = inotify_rm_watch (fd, wd);
  23. Events are provided in the form of an inotify_event structure that is read(2)
  24. from a given inotify instance. The filename is of dynamic length and follows
  25. the struct. It is of size len. The filename is padded with null bytes to
  26. ensure proper alignment. This padding is reflected in len.
  27. You can slurp multiple events by passing a large buffer, for example
  28. size_t len = read (fd, buf, BUF_LEN);
  29. Where "buf" is a pointer to an array of "inotify_event" structures at least
  30. BUF_LEN bytes in size. The above example will return as many events as are
  31. available and fit in BUF_LEN.
  32. Each inotify instance fd is also select()- and poll()-able.
  33. You can find the size of the current event queue via the standard FIONREAD
  34. ioctl on the fd returned by inotify_init().
  35. All watches are destroyed and cleaned up on close.
  36. (ii)
  37. Prototypes:
  38. int inotify_init (void);
  39. int inotify_add_watch (int fd, const char *path, __u32 mask);
  40. int inotify_rm_watch (int fd, __u32 mask);
  41. (iii) Internal Kernel Implementation
  42. Each inotify instance is associated with an inotify_device structure.
  43. Each watch is associated with an inotify_watch structure. Watches are chained
  44. off of each associated device and each associated inode.
  45. See fs/inotify.c for the locking and lifetime rules.
  46. (iv) Rationale
  47. Q: What is the design decision behind not tying the watch to the open fd of
  48. the watched object?
  49. A: Watches are associated with an open inotify device, not an open file.
  50. This solves the primary problem with dnotify: keeping the file open pins
  51. the file and thus, worse, pins the mount. Dnotify is therefore infeasible
  52. for use on a desktop system with removable media as the media cannot be
  53. unmounted. Watching a file should not require that it be open.
  54. Q: What is the design decision behind using an-fd-per-instance as opposed to
  55. an fd-per-watch?
  56. A: An fd-per-watch quickly consumes more file descriptors than are allowed,
  57. more fd's than are feasible to manage, and more fd's than are optimally
  58. select()-able. Yes, root can bump the per-process fd limit and yes, users
  59. can use epoll, but requiring both is a silly and extraneous requirement.
  60. A watch consumes less memory than an open file, separating the number
  61. spaces is thus sensible. The current design is what user-space developers
  62. want: Users initialize inotify, once, and add n watches, requiring but one
  63. fd and no twiddling with fd limits. Initializing an inotify instance two
  64. thousand times is silly. If we can implement user-space's preferences
  65. cleanly--and we can, the idr layer makes stuff like this trivial--then we
  66. should.
  67. There are other good arguments. With a single fd, there is a single
  68. item to block on, which is mapped to a single queue of events. The single
  69. fd returns all watch events and also any potential out-of-band data. If
  70. every fd was a separate watch,
  71. - There would be no way to get event ordering. Events on file foo and
  72. file bar would pop poll() on both fd's, but there would be no way to tell
  73. which happened first. A single queue trivially gives you ordering. Such
  74. ordering is crucial to existing applications such as Beagle. Imagine
  75. "mv a b ; mv b a" events without ordering.
  76. - We'd have to maintain n fd's and n internal queues with state,
  77. versus just one. It is a lot messier in the kernel. A single, linear
  78. queue is the data structure that makes sense.
  79. - User-space developers prefer the current API. The Beagle guys, for
  80. example, love it. Trust me, I asked. It is not a surprise: Who'd want
  81. to manage and block on 1000 fd's via select?
  82. - No way to get out of band data.
  83. - 1024 is still too low. ;-)
  84. When you talk about designing a file change notification system that
  85. scales to 1000s of directories, juggling 1000s of fd's just does not seem
  86. the right interface. It is too heavy.
  87. Additionally, it _is_ possible to more than one instance and
  88. juggle more than one queue and thus more than one associated fd. There
  89. need not be a one-fd-per-process mapping; it is one-fd-per-queue and a
  90. process can easily want more than one queue.
  91. Q: Why the system call approach?
  92. A: The poor user-space interface is the second biggest problem with dnotify.
  93. Signals are a terrible, terrible interface for file notification. Or for
  94. anything, for that matter. The ideal solution, from all perspectives, is a
  95. file descriptor-based one that allows basic file I/O and poll/select.
  96. Obtaining the fd and managing the watches could have been done either via a
  97. device file or a family of new system calls. We decided to implement a
  98. family of system calls because that is the preffered approach for new kernel
  99. interfaces. The only real difference was whether we wanted to use open(2)
  100. and ioctl(2) or a couple of new system calls. System calls beat ioctls.