dnotify.c 11 KB

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