dnotify.c 11 KB

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