dnotify.c 12 KB

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