dnotify.c 11 KB

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