dnotify.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. static DEFINE_MUTEX(dnotify_mark_mutex);
  33. /*
  34. * dnotify will attach one of these to each inode (i_fsnotify_marks) which
  35. * is being watched by dnotify. If multiple userspace applications are watching
  36. * the same directory with dnotify their information is chained in dn
  37. */
  38. struct dnotify_mark {
  39. struct fsnotify_mark fsn_mark;
  40. struct dnotify_struct *dn;
  41. };
  42. /*
  43. * When a process starts or stops watching an inode the set of events which
  44. * dnotify cares about for that inode may change. This function runs the
  45. * list of everything receiving dnotify events about this directory and calculates
  46. * the set of all those events. After it updates what dnotify is interested in
  47. * it calls the fsnotify function so it can update the set of all events relevant
  48. * to this inode.
  49. */
  50. static void dnotify_recalc_inode_mask(struct fsnotify_mark *fsn_mark)
  51. {
  52. __u32 new_mask, old_mask;
  53. struct dnotify_struct *dn;
  54. struct dnotify_mark *dn_mark = container_of(fsn_mark,
  55. struct dnotify_mark,
  56. fsn_mark);
  57. assert_spin_locked(&fsn_mark->lock);
  58. old_mask = fsn_mark->mask;
  59. new_mask = 0;
  60. for (dn = dn_mark->dn; dn != NULL; dn = dn->dn_next)
  61. new_mask |= (dn->dn_mask & ~FS_DN_MULTISHOT);
  62. fsnotify_set_mark_mask_locked(fsn_mark, new_mask);
  63. if (old_mask == new_mask)
  64. return;
  65. if (fsn_mark->i.inode)
  66. fsnotify_recalc_inode_mask(fsn_mark->i.inode);
  67. }
  68. /*
  69. * Mains fsnotify call where events are delivered to dnotify.
  70. * Find the dnotify mark on the relevant inode, run the list of dnotify structs
  71. * on that mark and determine which of them has expressed interest in receiving
  72. * events of this type. When found send the correct process and signal and
  73. * destroy the dnotify struct if it was not registered to receive multiple
  74. * events.
  75. */
  76. static int dnotify_handle_event(struct fsnotify_group *group,
  77. struct fsnotify_event *event)
  78. {
  79. struct fsnotify_mark *fsn_mark = NULL;
  80. struct dnotify_mark *dn_mark;
  81. struct inode *to_tell;
  82. struct dnotify_struct *dn;
  83. struct dnotify_struct **prev;
  84. struct fown_struct *fown;
  85. __u32 test_mask = event->mask & ~FS_EVENT_ON_CHILD;
  86. to_tell = event->to_tell;
  87. fsn_mark = fsnotify_find_inode_mark(group, to_tell);
  88. if (unlikely(!fsn_mark))
  89. return 0;
  90. dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
  91. spin_lock(&fsn_mark->lock);
  92. prev = &dn_mark->dn;
  93. while ((dn = *prev) != NULL) {
  94. if ((dn->dn_mask & test_mask) == 0) {
  95. prev = &dn->dn_next;
  96. continue;
  97. }
  98. fown = &dn->dn_filp->f_owner;
  99. send_sigio(fown, dn->dn_fd, POLL_MSG);
  100. if (dn->dn_mask & FS_DN_MULTISHOT)
  101. prev = &dn->dn_next;
  102. else {
  103. *prev = dn->dn_next;
  104. kmem_cache_free(dnotify_struct_cache, dn);
  105. dnotify_recalc_inode_mask(fsn_mark);
  106. }
  107. }
  108. spin_unlock(&fsn_mark->lock);
  109. fsnotify_put_mark(fsn_mark);
  110. return 0;
  111. }
  112. /*
  113. * Given an inode and mask determine if dnotify would be interested in sending
  114. * userspace notification for that pair.
  115. */
  116. static bool dnotify_should_send_event(struct fsnotify_group *group,
  117. struct inode *inode, struct vfsmount *mnt,
  118. __u32 mask, void *data, int data_type)
  119. {
  120. struct fsnotify_mark *fsn_mark;
  121. bool send;
  122. /* !dir_notify_enable should never get here, don't waste time checking
  123. if (!dir_notify_enable)
  124. return 0; */
  125. /* not a dir, dnotify doesn't care */
  126. if (!S_ISDIR(inode->i_mode))
  127. return false;
  128. fsn_mark = fsnotify_find_inode_mark(group, inode);
  129. if (!fsn_mark)
  130. return false;
  131. mask = (mask & ~FS_EVENT_ON_CHILD);
  132. send = (mask & fsn_mark->mask);
  133. fsnotify_put_mark(fsn_mark); /* matches fsnotify_find_inode_mark */
  134. return send;
  135. }
  136. static void dnotify_free_mark(struct fsnotify_mark *fsn_mark)
  137. {
  138. struct dnotify_mark *dn_mark = container_of(fsn_mark,
  139. struct dnotify_mark,
  140. fsn_mark);
  141. BUG_ON(dn_mark->dn);
  142. kmem_cache_free(dnotify_mark_cache, dn_mark);
  143. }
  144. static struct fsnotify_ops dnotify_fsnotify_ops = {
  145. .handle_event = dnotify_handle_event,
  146. .should_send_event = dnotify_should_send_event,
  147. .free_group_priv = NULL,
  148. .freeing_mark = NULL,
  149. .free_event_priv = NULL,
  150. };
  151. /*
  152. * Called every time a file is closed. Looks first for a dnotify mark on the
  153. * inode. If one is found run all of the ->dn structures attached to that
  154. * mark for one relevant to this process closing the file and remove that
  155. * dnotify_struct. If that was the last dnotify_struct also remove the
  156. * fsnotify_mark.
  157. */
  158. void dnotify_flush(struct file *filp, fl_owner_t id)
  159. {
  160. struct fsnotify_mark *fsn_mark;
  161. struct dnotify_mark *dn_mark;
  162. struct dnotify_struct *dn;
  163. struct dnotify_struct **prev;
  164. struct inode *inode;
  165. inode = filp->f_path.dentry->d_inode;
  166. if (!S_ISDIR(inode->i_mode))
  167. return;
  168. fsn_mark = fsnotify_find_inode_mark(dnotify_group, inode);
  169. if (!fsn_mark)
  170. return;
  171. dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
  172. mutex_lock(&dnotify_mark_mutex);
  173. spin_lock(&fsn_mark->lock);
  174. prev = &dn_mark->dn;
  175. while ((dn = *prev) != NULL) {
  176. if ((dn->dn_owner == id) && (dn->dn_filp == filp)) {
  177. *prev = dn->dn_next;
  178. kmem_cache_free(dnotify_struct_cache, dn);
  179. dnotify_recalc_inode_mask(fsn_mark);
  180. break;
  181. }
  182. prev = &dn->dn_next;
  183. }
  184. spin_unlock(&fsn_mark->lock);
  185. /* nothing else could have found us thanks to the dnotify_mark_mutex */
  186. if (dn_mark->dn == NULL)
  187. fsnotify_destroy_mark(fsn_mark);
  188. fsnotify_recalc_group_mask(dnotify_group);
  189. mutex_unlock(&dnotify_mark_mutex);
  190. fsnotify_put_mark(fsn_mark);
  191. }
  192. /* this conversion is done only at watch creation */
  193. static __u32 convert_arg(unsigned long arg)
  194. {
  195. __u32 new_mask = FS_EVENT_ON_CHILD;
  196. if (arg & DN_MULTISHOT)
  197. new_mask |= FS_DN_MULTISHOT;
  198. if (arg & DN_DELETE)
  199. new_mask |= (FS_DELETE | FS_MOVED_FROM);
  200. if (arg & DN_MODIFY)
  201. new_mask |= FS_MODIFY;
  202. if (arg & DN_ACCESS)
  203. new_mask |= FS_ACCESS;
  204. if (arg & DN_ATTRIB)
  205. new_mask |= FS_ATTRIB;
  206. if (arg & DN_RENAME)
  207. new_mask |= FS_DN_RENAME;
  208. if (arg & DN_CREATE)
  209. new_mask |= (FS_CREATE | FS_MOVED_TO);
  210. return new_mask;
  211. }
  212. /*
  213. * If multiple processes watch the same inode with dnotify there is only one
  214. * dnotify mark in inode->i_fsnotify_marks but we chain a dnotify_struct
  215. * onto that mark. This function either attaches the new dnotify_struct onto
  216. * that list, or it |= the mask onto an existing dnofiy_struct.
  217. */
  218. static int attach_dn(struct dnotify_struct *dn, struct dnotify_mark *dn_mark,
  219. fl_owner_t id, int fd, struct file *filp, __u32 mask)
  220. {
  221. struct dnotify_struct *odn;
  222. odn = dn_mark->dn;
  223. while (odn != NULL) {
  224. /* adding more events to existing dnofiy_struct? */
  225. if ((odn->dn_owner == id) && (odn->dn_filp == filp)) {
  226. odn->dn_fd = fd;
  227. odn->dn_mask |= mask;
  228. return -EEXIST;
  229. }
  230. odn = odn->dn_next;
  231. }
  232. dn->dn_mask = mask;
  233. dn->dn_fd = fd;
  234. dn->dn_filp = filp;
  235. dn->dn_owner = id;
  236. dn->dn_next = dn_mark->dn;
  237. dn_mark->dn = dn;
  238. return 0;
  239. }
  240. /*
  241. * When a process calls fcntl to attach a dnotify watch to a directory it ends
  242. * up here. Allocate both a mark for fsnotify to add and a dnotify_struct to be
  243. * attached to the fsnotify_mark.
  244. */
  245. int fcntl_dirnotify(int fd, struct file *filp, unsigned long arg)
  246. {
  247. struct dnotify_mark *new_dn_mark, *dn_mark;
  248. struct fsnotify_mark *new_fsn_mark, *fsn_mark;
  249. struct dnotify_struct *dn;
  250. struct inode *inode;
  251. fl_owner_t id = current->files;
  252. struct file *f;
  253. int destroy = 0, error = 0;
  254. __u32 mask;
  255. /* we use these to tell if we need to kfree */
  256. new_fsn_mark = NULL;
  257. dn = NULL;
  258. if (!dir_notify_enable) {
  259. error = -EINVAL;
  260. goto out_err;
  261. }
  262. /* a 0 mask means we are explicitly removing the watch */
  263. if ((arg & ~DN_MULTISHOT) == 0) {
  264. dnotify_flush(filp, id);
  265. error = 0;
  266. goto out_err;
  267. }
  268. /* dnotify only works on directories */
  269. inode = filp->f_path.dentry->d_inode;
  270. if (!S_ISDIR(inode->i_mode)) {
  271. error = -ENOTDIR;
  272. goto out_err;
  273. }
  274. /* expect most fcntl to add new rather than augment old */
  275. dn = kmem_cache_alloc(dnotify_struct_cache, GFP_KERNEL);
  276. if (!dn) {
  277. error = -ENOMEM;
  278. goto out_err;
  279. }
  280. /* new fsnotify mark, we expect most fcntl calls to add a new mark */
  281. new_dn_mark = kmem_cache_alloc(dnotify_mark_cache, GFP_KERNEL);
  282. if (!new_dn_mark) {
  283. error = -ENOMEM;
  284. goto out_err;
  285. }
  286. /* convert the userspace DN_* "arg" to the internal FS_* defines in fsnotify */
  287. mask = convert_arg(arg);
  288. /* set up the new_fsn_mark and new_dn_mark */
  289. new_fsn_mark = &new_dn_mark->fsn_mark;
  290. fsnotify_init_mark(new_fsn_mark, dnotify_free_mark);
  291. new_fsn_mark->mask = mask;
  292. new_dn_mark->dn = NULL;
  293. /* this is needed to prevent the fcntl/close race described below */
  294. mutex_lock(&dnotify_mark_mutex);
  295. /* add the new_fsn_mark or find an old one. */
  296. fsn_mark = fsnotify_find_inode_mark(dnotify_group, inode);
  297. if (fsn_mark) {
  298. dn_mark = container_of(fsn_mark, struct dnotify_mark, fsn_mark);
  299. spin_lock(&fsn_mark->lock);
  300. } else {
  301. fsnotify_add_mark(new_fsn_mark, dnotify_group, inode, NULL, 0);
  302. spin_lock(&new_fsn_mark->lock);
  303. fsn_mark = new_fsn_mark;
  304. dn_mark = new_dn_mark;
  305. /* we used new_fsn_mark, so don't free it */
  306. new_fsn_mark = NULL;
  307. }
  308. rcu_read_lock();
  309. f = fcheck(fd);
  310. rcu_read_unlock();
  311. /* if (f != filp) means that we lost a race and another task/thread
  312. * actually closed the fd we are still playing with before we grabbed
  313. * the dnotify_mark_mutex and fsn_mark->lock. Since closing the fd is the
  314. * only time we clean up the marks we need to get our mark off
  315. * the list. */
  316. if (f != filp) {
  317. /* if we added ourselves, shoot ourselves, it's possible that
  318. * the flush actually did shoot this fsn_mark. That's fine too
  319. * since multiple calls to destroy_mark is perfectly safe, if
  320. * we found a dn_mark already attached to the inode, just sod
  321. * off silently as the flush at close time dealt with it.
  322. */
  323. if (dn_mark == new_dn_mark)
  324. destroy = 1;
  325. goto out;
  326. }
  327. error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
  328. if (error) {
  329. /* if we added, we must shoot */
  330. if (dn_mark == new_dn_mark)
  331. destroy = 1;
  332. goto out;
  333. }
  334. error = attach_dn(dn, dn_mark, id, fd, filp, mask);
  335. /* !error means that we attached the dn to the dn_mark, so don't free it */
  336. if (!error)
  337. dn = NULL;
  338. /* -EEXIST means that we didn't add this new dn and used an old one.
  339. * that isn't an error (and the unused dn should be freed) */
  340. else if (error == -EEXIST)
  341. error = 0;
  342. dnotify_recalc_inode_mask(fsn_mark);
  343. out:
  344. spin_unlock(&fsn_mark->lock);
  345. if (destroy)
  346. fsnotify_destroy_mark(fsn_mark);
  347. fsnotify_recalc_group_mask(dnotify_group);
  348. mutex_unlock(&dnotify_mark_mutex);
  349. fsnotify_put_mark(fsn_mark);
  350. out_err:
  351. if (new_fsn_mark)
  352. fsnotify_put_mark(new_fsn_mark);
  353. if (dn)
  354. kmem_cache_free(dnotify_struct_cache, dn);
  355. return error;
  356. }
  357. static int __init dnotify_init(void)
  358. {
  359. dnotify_struct_cache = KMEM_CACHE(dnotify_struct, SLAB_PANIC);
  360. dnotify_mark_cache = KMEM_CACHE(dnotify_mark, SLAB_PANIC);
  361. dnotify_group = fsnotify_alloc_group(&dnotify_fsnotify_ops);
  362. if (IS_ERR(dnotify_group))
  363. panic("unable to allocate fsnotify group for dnotify\n");
  364. return 0;
  365. }
  366. module_init(dnotify_init)