dnotify.c 12 KB

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