dnotify.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Directory notifications for Linux.
  3. *
  4. * Copyright (C) 2000,2001,2002 Stephen Rothwell
  5. *
  6. * Copyright (C) 2009 Eric Paris <Red Hat Inc>
  7. * dnotify was largly rewritten to use the new fsnotify infrastructure
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2, or (at your option) any
  12. * later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. */
  19. #include <linux/fs.h>
  20. #include <linux/module.h>
  21. #include <linux/sched.h>
  22. #include <linux/dnotify.h>
  23. #include <linux/init.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/slab.h>
  26. #include <linux/fdtable.h>
  27. #include <linux/fsnotify_backend.h>
  28. int dir_notify_enable __read_mostly = 1;
  29. static struct kmem_cache *dnotify_struct_cache __read_mostly;
  30. static struct kmem_cache *dnotify_mark_cache __read_mostly;
  31. static struct fsnotify_group *dnotify_group __read_mostly;
  32. /*
  33. * dnotify will attach one of these to each inode (i_fsnotify_marks) which
  34. * is being watched by dnotify. If multiple userspace applications are watching
  35. * the same directory with dnotify their information is chained in dn
  36. */
  37. struct dnotify_mark {
  38. struct fsnotify_mark fsn_mark;
  39. struct dnotify_struct *dn;
  40. };
  41. /*
  42. * When a process starts or stops watching an inode the set of events which
  43. * dnotify cares about for that inode may change. This function runs the
  44. * list of everything receiving dnotify events about this directory and calculates
  45. * the set of all those events. After it updates what dnotify is interested in
  46. * it calls the fsnotify function so it can update the set of all events relevant
  47. * to this inode.
  48. */
  49. static void dnotify_recalc_inode_mask(struct fsnotify_mark *fsn_mark)
  50. {
  51. __u32 new_mask, old_mask;
  52. struct dnotify_struct *dn;
  53. struct dnotify_mark *dn_mark = container_of(fsn_mark,
  54. struct dnotify_mark,
  55. fsn_mark);
  56. assert_spin_locked(&fsn_mark->lock);
  57. old_mask = fsn_mark->mask;
  58. new_mask = 0;
  59. for (dn = dn_mark->dn; dn != NULL; dn = dn->dn_next)
  60. new_mask |= (dn->dn_mask & ~FS_DN_MULTISHOT);
  61. fsnotify_set_mark_mask_locked(fsn_mark, new_mask);
  62. if (old_mask == new_mask)
  63. return;
  64. if (fsn_mark->inode)
  65. fsnotify_recalc_inode_mask(fsn_mark->inode);
  66. }
  67. /*
  68. * Mains fsnotify call where events are delivered to dnotify.
  69. * Find the dnotify mark on the relevant inode, run the list of dnotify structs
  70. * on that mark and determine which of them has expressed interest in receiving
  71. * events of this type. When found send the correct process and signal and
  72. * destroy the dnotify struct if it was not registered to receive multiple
  73. * events.
  74. */
  75. static int dnotify_handle_event(struct fsnotify_group *group,
  76. struct inode *inode,
  77. struct fsnotify_mark *inode_mark,
  78. struct fsnotify_mark *vfsmount_mark,
  79. u32 mask, void *data, int data_type,
  80. const unsigned char *file_name, u32 cookie)
  81. {
  82. struct dnotify_mark *dn_mark;
  83. struct dnotify_struct *dn;
  84. struct dnotify_struct **prev;
  85. struct fown_struct *fown;
  86. __u32 test_mask = mask & ~FS_EVENT_ON_CHILD;
  87. /* not a dir, dnotify doesn't care */
  88. if (!S_ISDIR(inode->i_mode))
  89. return 0;
  90. BUG_ON(vfsmount_mark);
  91. dn_mark = container_of(inode_mark, struct dnotify_mark, fsn_mark);
  92. spin_lock(&inode_mark->lock);
  93. prev = &dn_mark->dn;
  94. while ((dn = *prev) != NULL) {
  95. if ((dn->dn_mask & test_mask) == 0) {
  96. prev = &dn->dn_next;
  97. continue;
  98. }
  99. fown = &dn->dn_filp->f_owner;
  100. send_sigio(fown, dn->dn_fd, POLL_MSG);
  101. if (dn->dn_mask & FS_DN_MULTISHOT)
  102. prev = &dn->dn_next;
  103. else {
  104. *prev = dn->dn_next;
  105. kmem_cache_free(dnotify_struct_cache, dn);
  106. dnotify_recalc_inode_mask(inode_mark);
  107. }
  108. }
  109. spin_unlock(&inode_mark->lock);
  110. return 0;
  111. }
  112. static void dnotify_free_mark(struct fsnotify_mark *fsn_mark)
  113. {
  114. struct dnotify_mark *dn_mark = container_of(fsn_mark,
  115. struct dnotify_mark,
  116. fsn_mark);
  117. BUG_ON(dn_mark->dn);
  118. kmem_cache_free(dnotify_mark_cache, dn_mark);
  119. }
  120. static struct fsnotify_ops dnotify_fsnotify_ops = {
  121. .handle_event = dnotify_handle_event,
  122. };
  123. /*
  124. * Called every time a file is closed. Looks first for a dnotify mark on the
  125. * inode. If one is found run all of the ->dn structures attached to that
  126. * mark for one relevant to this process closing the file and remove that
  127. * dnotify_struct. If that was the last dnotify_struct also remove the
  128. * fsnotify_mark.
  129. */
  130. void dnotify_flush(struct file *filp, fl_owner_t id)
  131. {
  132. struct fsnotify_mark *fsn_mark;
  133. struct dnotify_mark *dn_mark;
  134. struct dnotify_struct *dn;
  135. struct dnotify_struct **prev;
  136. struct inode *inode;
  137. bool free = false;
  138. inode = file_inode(filp);
  139. if (!S_ISDIR(inode->i_mode))
  140. return;
  141. fsn_mark = fsnotify_find_inode_mark(dnotify_group, inode);
  142. if (!fsn_mark)
  143. return;
  144. dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
  145. mutex_lock(&dnotify_group->mark_mutex);
  146. spin_lock(&fsn_mark->lock);
  147. prev = &dn_mark->dn;
  148. while ((dn = *prev) != NULL) {
  149. if ((dn->dn_owner == id) && (dn->dn_filp == filp)) {
  150. *prev = dn->dn_next;
  151. kmem_cache_free(dnotify_struct_cache, dn);
  152. dnotify_recalc_inode_mask(fsn_mark);
  153. break;
  154. }
  155. prev = &dn->dn_next;
  156. }
  157. spin_unlock(&fsn_mark->lock);
  158. /* nothing else could have found us thanks to the dnotify_groups
  159. mark_mutex */
  160. if (dn_mark->dn == NULL) {
  161. fsnotify_detach_mark(fsn_mark);
  162. free = true;
  163. }
  164. mutex_unlock(&dnotify_group->mark_mutex);
  165. if (free)
  166. fsnotify_free_mark(fsn_mark);
  167. fsnotify_put_mark(fsn_mark);
  168. }
  169. /* this conversion is done only at watch creation */
  170. static __u32 convert_arg(unsigned long arg)
  171. {
  172. __u32 new_mask = FS_EVENT_ON_CHILD;
  173. if (arg & DN_MULTISHOT)
  174. new_mask |= FS_DN_MULTISHOT;
  175. if (arg & DN_DELETE)
  176. new_mask |= (FS_DELETE | FS_MOVED_FROM);
  177. if (arg & DN_MODIFY)
  178. new_mask |= FS_MODIFY;
  179. if (arg & DN_ACCESS)
  180. new_mask |= FS_ACCESS;
  181. if (arg & DN_ATTRIB)
  182. new_mask |= FS_ATTRIB;
  183. if (arg & DN_RENAME)
  184. new_mask |= FS_DN_RENAME;
  185. if (arg & DN_CREATE)
  186. new_mask |= (FS_CREATE | FS_MOVED_TO);
  187. return new_mask;
  188. }
  189. /*
  190. * If multiple processes watch the same inode with dnotify there is only one
  191. * dnotify mark in inode->i_fsnotify_marks but we chain a dnotify_struct
  192. * onto that mark. This function either attaches the new dnotify_struct onto
  193. * that list, or it |= the mask onto an existing dnofiy_struct.
  194. */
  195. static int attach_dn(struct dnotify_struct *dn, struct dnotify_mark *dn_mark,
  196. fl_owner_t id, int fd, struct file *filp, __u32 mask)
  197. {
  198. struct dnotify_struct *odn;
  199. odn = dn_mark->dn;
  200. while (odn != NULL) {
  201. /* adding more events to existing dnofiy_struct? */
  202. if ((odn->dn_owner == id) && (odn->dn_filp == filp)) {
  203. odn->dn_fd = fd;
  204. odn->dn_mask |= mask;
  205. return -EEXIST;
  206. }
  207. odn = odn->dn_next;
  208. }
  209. dn->dn_mask = mask;
  210. dn->dn_fd = fd;
  211. dn->dn_filp = filp;
  212. dn->dn_owner = id;
  213. dn->dn_next = dn_mark->dn;
  214. dn_mark->dn = dn;
  215. return 0;
  216. }
  217. /*
  218. * When a process calls fcntl to attach a dnotify watch to a directory it ends
  219. * up here. Allocate both a mark for fsnotify to add and a dnotify_struct to be
  220. * attached to the fsnotify_mark.
  221. */
  222. int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
  223. {
  224. struct dnotify_mark *new_dn_mark, *dn_mark;
  225. struct fsnotify_mark *new_fsn_mark, *fsn_mark;
  226. struct dnotify_struct *dn;
  227. struct inode *inode;
  228. fl_owner_t id = current->files;
  229. struct file *f;
  230. int destroy = 0, error = 0;
  231. __u32 mask;
  232. /* we use these to tell if we need to kfree */
  233. new_fsn_mark = NULL;
  234. dn = NULL;
  235. if (!dir_notify_enable) {
  236. error = -EINVAL;
  237. goto out_err;
  238. }
  239. /* a 0 mask means we are explicitly removing the watch */
  240. if ((arg & ~DN_MULTISHOT) == 0) {
  241. dnotify_flush(filp, id);
  242. error = 0;
  243. goto out_err;
  244. }
  245. /* dnotify only works on directories */
  246. inode = file_inode(filp);
  247. if (!S_ISDIR(inode->i_mode)) {
  248. error = -ENOTDIR;
  249. goto out_err;
  250. }
  251. /* expect most fcntl to add new rather than augment old */
  252. dn = kmem_cache_alloc(dnotify_struct_cache, GFP_KERNEL);
  253. if (!dn) {
  254. error = -ENOMEM;
  255. goto out_err;
  256. }
  257. /* new fsnotify mark, we expect most fcntl calls to add a new mark */
  258. new_dn_mark = kmem_cache_alloc(dnotify_mark_cache, GFP_KERNEL);
  259. if (!new_dn_mark) {
  260. error = -ENOMEM;
  261. goto out_err;
  262. }
  263. /* convert the userspace DN_* "arg" to the internal FS_* defines in fsnotify */
  264. mask = convert_arg(arg);
  265. /* set up the new_fsn_mark and new_dn_mark */
  266. new_fsn_mark = &new_dn_mark->fsn_mark;
  267. fsnotify_init_mark(new_fsn_mark, dnotify_free_mark);
  268. new_fsn_mark->mask = mask;
  269. new_dn_mark->dn = NULL;
  270. /* this is needed to prevent the fcntl/close race described below */
  271. mutex_lock(&dnotify_group->mark_mutex);
  272. /* add the new_fsn_mark or find an old one. */
  273. fsn_mark = fsnotify_find_inode_mark(dnotify_group, inode);
  274. if (fsn_mark) {
  275. dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
  276. spin_lock(&fsn_mark->lock);
  277. } else {
  278. fsnotify_add_mark_locked(new_fsn_mark, dnotify_group, inode,
  279. NULL, 0);
  280. spin_lock(&new_fsn_mark->lock);
  281. fsn_mark = new_fsn_mark;
  282. dn_mark = new_dn_mark;
  283. /* we used new_fsn_mark, so don't free it */
  284. new_fsn_mark = NULL;
  285. }
  286. rcu_read_lock();
  287. f = fcheck(fd);
  288. rcu_read_unlock();
  289. /* if (f != filp) means that we lost a race and another task/thread
  290. * actually closed the fd we are still playing with before we grabbed
  291. * the dnotify_groups mark_mutex and fsn_mark->lock. Since closing the
  292. * fd is the only time we clean up the marks we need to get our mark
  293. * off the list. */
  294. if (f != filp) {
  295. /* if we added ourselves, shoot ourselves, it's possible that
  296. * the flush actually did shoot this fsn_mark. That's fine too
  297. * since multiple calls to destroy_mark is perfectly safe, if
  298. * we found a dn_mark already attached to the inode, just sod
  299. * off silently as the flush at close time dealt with it.
  300. */
  301. if (dn_mark == new_dn_mark)
  302. destroy = 1;
  303. goto out;
  304. }
  305. __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
  306. error = attach_dn(dn, dn_mark, id, fd, filp, mask);
  307. /* !error means that we attached the dn to the dn_mark, so don't free it */
  308. if (!error)
  309. dn = NULL;
  310. /* -EEXIST means that we didn't add this new dn and used an old one.
  311. * that isn't an error (and the unused dn should be freed) */
  312. else if (error == -EEXIST)
  313. error = 0;
  314. dnotify_recalc_inode_mask(fsn_mark);
  315. out:
  316. spin_unlock(&fsn_mark->lock);
  317. if (destroy)
  318. fsnotify_detach_mark(fsn_mark);
  319. mutex_unlock(&dnotify_group->mark_mutex);
  320. if (destroy)
  321. fsnotify_free_mark(fsn_mark);
  322. fsnotify_put_mark(fsn_mark);
  323. out_err:
  324. if (new_fsn_mark)
  325. fsnotify_put_mark(new_fsn_mark);
  326. if (dn)
  327. kmem_cache_free(dnotify_struct_cache, dn);
  328. return error;
  329. }
  330. static int __init dnotify_init(void)
  331. {
  332. dnotify_struct_cache = KMEM_CACHE(dnotify_struct, SLAB_PANIC);
  333. dnotify_mark_cache = KMEM_CACHE(dnotify_mark, SLAB_PANIC);
  334. dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops);
  335. if (IS_ERR(dnotify_group))
  336. panic("unable to allocate fsnotify group for dnotify\n");
  337. return 0;
  338. }
  339. module_init(dnotify_init)