dnotify.c 11 KB

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