dnotify.c 12 KB

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