fsnotify_backend.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Filesystem access notification for Linux
  4. *
  5. * Copyright (C) 2008 Red Hat, Inc., Eric Paris <[email protected]>
  6. */
  7. #ifndef __LINUX_FSNOTIFY_BACKEND_H
  8. #define __LINUX_FSNOTIFY_BACKEND_H
  9. #ifdef __KERNEL__
  10. #include <linux/idr.h> /* inotify uses this */
  11. #include <linux/fs.h> /* struct inode */
  12. #include <linux/list.h>
  13. #include <linux/path.h> /* struct path */
  14. #include <linux/spinlock.h>
  15. #include <linux/types.h>
  16. #include <linux/atomic.h>
  17. #include <linux/user_namespace.h>
  18. #include <linux/refcount.h>
  19. #include <linux/mempool.h>
  20. #include <linux/sched/mm.h>
  21. /*
  22. * IN_* from inotfy.h lines up EXACTLY with FS_*, this is so we can easily
  23. * convert between them. dnotify only needs conversion at watch creation
  24. * so no perf loss there. fanotify isn't defined yet, so it can use the
  25. * wholes if it needs more events.
  26. */
  27. #define FS_ACCESS 0x00000001 /* File was accessed */
  28. #define FS_MODIFY 0x00000002 /* File was modified */
  29. #define FS_ATTRIB 0x00000004 /* Metadata changed */
  30. #define FS_CLOSE_WRITE 0x00000008 /* Writtable file was closed */
  31. #define FS_CLOSE_NOWRITE 0x00000010 /* Unwrittable file closed */
  32. #define FS_OPEN 0x00000020 /* File was opened */
  33. #define FS_MOVED_FROM 0x00000040 /* File was moved from X */
  34. #define FS_MOVED_TO 0x00000080 /* File was moved to Y */
  35. #define FS_CREATE 0x00000100 /* Subfile was created */
  36. #define FS_DELETE 0x00000200 /* Subfile was deleted */
  37. #define FS_DELETE_SELF 0x00000400 /* Self was deleted */
  38. #define FS_MOVE_SELF 0x00000800 /* Self was moved */
  39. #define FS_OPEN_EXEC 0x00001000 /* File was opened for exec */
  40. #define FS_UNMOUNT 0x00002000 /* inode on umount fs */
  41. #define FS_Q_OVERFLOW 0x00004000 /* Event queued overflowed */
  42. #define FS_ERROR 0x00008000 /* Filesystem Error (fanotify) */
  43. /*
  44. * FS_IN_IGNORED overloads FS_ERROR. It is only used internally by inotify
  45. * which does not support FS_ERROR.
  46. */
  47. #define FS_IN_IGNORED 0x00008000 /* last inotify event here */
  48. #define FS_OPEN_PERM 0x00010000 /* open event in an permission hook */
  49. #define FS_ACCESS_PERM 0x00020000 /* access event in a permissions hook */
  50. #define FS_OPEN_EXEC_PERM 0x00040000 /* open/exec event in a permission hook */
  51. /*
  52. * Set on inode mark that cares about things that happen to its children.
  53. * Always set for dnotify and inotify.
  54. * Set on inode/sb/mount marks that care about parent/name info.
  55. */
  56. #define FS_EVENT_ON_CHILD 0x08000000
  57. #define FS_RENAME 0x10000000 /* File was renamed */
  58. #define FS_DN_MULTISHOT 0x20000000 /* dnotify multishot */
  59. #define FS_ISDIR 0x40000000 /* event occurred against dir */
  60. #define FS_MOVE (FS_MOVED_FROM | FS_MOVED_TO)
  61. /*
  62. * Directory entry modification events - reported only to directory
  63. * where entry is modified and not to a watching parent.
  64. * The watching parent may get an FS_ATTRIB|FS_EVENT_ON_CHILD event
  65. * when a directory entry inside a child subdir changes.
  66. */
  67. #define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME)
  68. #define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \
  69. FS_OPEN_EXEC_PERM)
  70. /*
  71. * This is a list of all events that may get sent to a parent that is watching
  72. * with flag FS_EVENT_ON_CHILD based on fs event on a child of that directory.
  73. */
  74. #define FS_EVENTS_POSS_ON_CHILD (ALL_FSNOTIFY_PERM_EVENTS | \
  75. FS_ACCESS | FS_MODIFY | FS_ATTRIB | \
  76. FS_CLOSE_WRITE | FS_CLOSE_NOWRITE | \
  77. FS_OPEN | FS_OPEN_EXEC)
  78. /*
  79. * This is a list of all events that may get sent with the parent inode as the
  80. * @to_tell argument of fsnotify().
  81. * It may include events that can be sent to an inode/sb/mount mark, but cannot
  82. * be sent to a parent watching children.
  83. */
  84. #define FS_EVENTS_POSS_TO_PARENT (FS_EVENTS_POSS_ON_CHILD)
  85. /* Events that can be reported to backends */
  86. #define ALL_FSNOTIFY_EVENTS (ALL_FSNOTIFY_DIRENT_EVENTS | \
  87. FS_EVENTS_POSS_ON_CHILD | \
  88. FS_DELETE_SELF | FS_MOVE_SELF | \
  89. FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED | \
  90. FS_ERROR)
  91. /* Extra flags that may be reported with event or control handling of events */
  92. #define ALL_FSNOTIFY_FLAGS (FS_ISDIR | FS_EVENT_ON_CHILD | FS_DN_MULTISHOT)
  93. #define ALL_FSNOTIFY_BITS (ALL_FSNOTIFY_EVENTS | ALL_FSNOTIFY_FLAGS)
  94. struct fsnotify_group;
  95. struct fsnotify_event;
  96. struct fsnotify_mark;
  97. struct fsnotify_event_private_data;
  98. struct fsnotify_fname;
  99. struct fsnotify_iter_info;
  100. struct mem_cgroup;
  101. /*
  102. * Each group much define these ops. The fsnotify infrastructure will call
  103. * these operations for each relevant group.
  104. *
  105. * handle_event - main call for a group to handle an fs event
  106. * @group: group to notify
  107. * @mask: event type and flags
  108. * @data: object that event happened on
  109. * @data_type: type of object for fanotify_data_XXX() accessors
  110. * @dir: optional directory associated with event -
  111. * if @file_name is not NULL, this is the directory that
  112. * @file_name is relative to
  113. * @file_name: optional file name associated with event
  114. * @cookie: inotify rename cookie
  115. * @iter_info: array of marks from this group that are interested in the event
  116. *
  117. * handle_inode_event - simple variant of handle_event() for groups that only
  118. * have inode marks and don't have ignore mask
  119. * @mark: mark to notify
  120. * @mask: event type and flags
  121. * @inode: inode that event happened on
  122. * @dir: optional directory associated with event -
  123. * if @file_name is not NULL, this is the directory that
  124. * @file_name is relative to.
  125. * Either @inode or @dir must be non-NULL.
  126. * @file_name: optional file name associated with event
  127. * @cookie: inotify rename cookie
  128. *
  129. * free_group_priv - called when a group refcnt hits 0 to clean up the private union
  130. * freeing_mark - called when a mark is being destroyed for some reason. The group
  131. * MUST be holding a reference on each mark and that reference must be
  132. * dropped in this function. inotify uses this function to send
  133. * userspace messages that marks have been removed.
  134. */
  135. struct fsnotify_ops {
  136. int (*handle_event)(struct fsnotify_group *group, u32 mask,
  137. const void *data, int data_type, struct inode *dir,
  138. const struct qstr *file_name, u32 cookie,
  139. struct fsnotify_iter_info *iter_info);
  140. int (*handle_inode_event)(struct fsnotify_mark *mark, u32 mask,
  141. struct inode *inode, struct inode *dir,
  142. const struct qstr *file_name, u32 cookie);
  143. void (*free_group_priv)(struct fsnotify_group *group);
  144. void (*freeing_mark)(struct fsnotify_mark *mark, struct fsnotify_group *group);
  145. void (*free_event)(struct fsnotify_group *group, struct fsnotify_event *event);
  146. /* called on final put+free to free memory */
  147. void (*free_mark)(struct fsnotify_mark *mark);
  148. };
  149. /*
  150. * all of the information about the original object we want to now send to
  151. * a group. If you want to carry more info from the accessing task to the
  152. * listener this structure is where you need to be adding fields.
  153. */
  154. struct fsnotify_event {
  155. struct list_head list;
  156. };
  157. /*
  158. * A group is a "thing" that wants to receive notification about filesystem
  159. * events. The mask holds the subset of event types this group cares about.
  160. * refcnt on a group is up to the implementor and at any moment if it goes 0
  161. * everything will be cleaned up.
  162. */
  163. struct fsnotify_group {
  164. const struct fsnotify_ops *ops; /* how this group handles things */
  165. /*
  166. * How the refcnt is used is up to each group. When the refcnt hits 0
  167. * fsnotify will clean up all of the resources associated with this group.
  168. * As an example, the dnotify group will always have a refcnt=1 and that
  169. * will never change. Inotify, on the other hand, has a group per
  170. * inotify_init() and the refcnt will hit 0 only when that fd has been
  171. * closed.
  172. */
  173. refcount_t refcnt; /* things with interest in this group */
  174. /* needed to send notification to userspace */
  175. spinlock_t notification_lock; /* protect the notification_list */
  176. struct list_head notification_list; /* list of event_holder this group needs to send to userspace */
  177. wait_queue_head_t notification_waitq; /* read() on the notification file blocks on this waitq */
  178. unsigned int q_len; /* events on the queue */
  179. unsigned int max_events; /* maximum events allowed on the list */
  180. /*
  181. * Valid fsnotify group priorities. Events are send in order from highest
  182. * priority to lowest priority. We default to the lowest priority.
  183. */
  184. #define FS_PRIO_0 0 /* normal notifiers, no permissions */
  185. #define FS_PRIO_1 1 /* fanotify content based access control */
  186. #define FS_PRIO_2 2 /* fanotify pre-content access */
  187. unsigned int priority;
  188. bool shutdown; /* group is being shut down, don't queue more events */
  189. #define FSNOTIFY_GROUP_USER 0x01 /* user allocated group */
  190. #define FSNOTIFY_GROUP_DUPS 0x02 /* allow multiple marks per object */
  191. #define FSNOTIFY_GROUP_NOFS 0x04 /* group lock is not direct reclaim safe */
  192. int flags;
  193. unsigned int owner_flags; /* stored flags of mark_mutex owner */
  194. /* stores all fastpath marks assoc with this group so they can be cleaned on unregister */
  195. struct mutex mark_mutex; /* protect marks_list */
  196. atomic_t user_waits; /* Number of tasks waiting for user
  197. * response */
  198. struct list_head marks_list; /* all inode marks for this group */
  199. struct fasync_struct *fsn_fa; /* async notification */
  200. struct fsnotify_event *overflow_event; /* Event we queue when the
  201. * notification list is too
  202. * full */
  203. struct mem_cgroup *memcg; /* memcg to charge allocations */
  204. /* groups can define private fields here or use the void *private */
  205. union {
  206. void *private;
  207. #ifdef CONFIG_INOTIFY_USER
  208. struct inotify_group_private_data {
  209. spinlock_t idr_lock;
  210. struct idr idr;
  211. struct ucounts *ucounts;
  212. } inotify_data;
  213. #endif
  214. #ifdef CONFIG_FANOTIFY
  215. struct fanotify_group_private_data {
  216. /* Hash table of events for merge */
  217. struct hlist_head *merge_hash;
  218. /* allows a group to block waiting for a userspace response */
  219. struct list_head access_list;
  220. wait_queue_head_t access_waitq;
  221. int flags; /* flags from fanotify_init() */
  222. int f_flags; /* event_f_flags from fanotify_init() */
  223. struct ucounts *ucounts;
  224. mempool_t error_events_pool;
  225. } fanotify_data;
  226. #endif /* CONFIG_FANOTIFY */
  227. };
  228. };
  229. /*
  230. * These helpers are used to prevent deadlock when reclaiming inodes with
  231. * evictable marks of the same group that is allocating a new mark.
  232. */
  233. static inline void fsnotify_group_lock(struct fsnotify_group *group)
  234. {
  235. mutex_lock(&group->mark_mutex);
  236. if (group->flags & FSNOTIFY_GROUP_NOFS)
  237. group->owner_flags = memalloc_nofs_save();
  238. }
  239. static inline void fsnotify_group_unlock(struct fsnotify_group *group)
  240. {
  241. if (group->flags & FSNOTIFY_GROUP_NOFS)
  242. memalloc_nofs_restore(group->owner_flags);
  243. mutex_unlock(&group->mark_mutex);
  244. }
  245. static inline void fsnotify_group_assert_locked(struct fsnotify_group *group)
  246. {
  247. WARN_ON_ONCE(!mutex_is_locked(&group->mark_mutex));
  248. if (group->flags & FSNOTIFY_GROUP_NOFS)
  249. WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
  250. }
  251. /* When calling fsnotify tell it if the data is a path or inode */
  252. enum fsnotify_data_type {
  253. FSNOTIFY_EVENT_NONE,
  254. FSNOTIFY_EVENT_PATH,
  255. FSNOTIFY_EVENT_INODE,
  256. FSNOTIFY_EVENT_DENTRY,
  257. FSNOTIFY_EVENT_ERROR,
  258. };
  259. struct fs_error_report {
  260. int error;
  261. struct inode *inode;
  262. struct super_block *sb;
  263. };
  264. static inline struct inode *fsnotify_data_inode(const void *data, int data_type)
  265. {
  266. switch (data_type) {
  267. case FSNOTIFY_EVENT_INODE:
  268. return (struct inode *)data;
  269. case FSNOTIFY_EVENT_DENTRY:
  270. return d_inode(data);
  271. case FSNOTIFY_EVENT_PATH:
  272. return d_inode(((const struct path *)data)->dentry);
  273. case FSNOTIFY_EVENT_ERROR:
  274. return ((struct fs_error_report *)data)->inode;
  275. default:
  276. return NULL;
  277. }
  278. }
  279. static inline struct dentry *fsnotify_data_dentry(const void *data, int data_type)
  280. {
  281. switch (data_type) {
  282. case FSNOTIFY_EVENT_DENTRY:
  283. /* Non const is needed for dget() */
  284. return (struct dentry *)data;
  285. case FSNOTIFY_EVENT_PATH:
  286. return ((const struct path *)data)->dentry;
  287. default:
  288. return NULL;
  289. }
  290. }
  291. static inline const struct path *fsnotify_data_path(const void *data,
  292. int data_type)
  293. {
  294. switch (data_type) {
  295. case FSNOTIFY_EVENT_PATH:
  296. return data;
  297. default:
  298. return NULL;
  299. }
  300. }
  301. static inline struct super_block *fsnotify_data_sb(const void *data,
  302. int data_type)
  303. {
  304. switch (data_type) {
  305. case FSNOTIFY_EVENT_INODE:
  306. return ((struct inode *)data)->i_sb;
  307. case FSNOTIFY_EVENT_DENTRY:
  308. return ((struct dentry *)data)->d_sb;
  309. case FSNOTIFY_EVENT_PATH:
  310. return ((const struct path *)data)->dentry->d_sb;
  311. case FSNOTIFY_EVENT_ERROR:
  312. return ((struct fs_error_report *) data)->sb;
  313. default:
  314. return NULL;
  315. }
  316. }
  317. static inline struct fs_error_report *fsnotify_data_error_report(
  318. const void *data,
  319. int data_type)
  320. {
  321. switch (data_type) {
  322. case FSNOTIFY_EVENT_ERROR:
  323. return (struct fs_error_report *) data;
  324. default:
  325. return NULL;
  326. }
  327. }
  328. /*
  329. * Index to merged marks iterator array that correlates to a type of watch.
  330. * The type of watched object can be deduced from the iterator type, but not
  331. * the other way around, because an event can match different watched objects
  332. * of the same object type.
  333. * For example, both parent and child are watching an object of type inode.
  334. */
  335. enum fsnotify_iter_type {
  336. FSNOTIFY_ITER_TYPE_INODE,
  337. FSNOTIFY_ITER_TYPE_VFSMOUNT,
  338. FSNOTIFY_ITER_TYPE_SB,
  339. FSNOTIFY_ITER_TYPE_PARENT,
  340. FSNOTIFY_ITER_TYPE_INODE2,
  341. FSNOTIFY_ITER_TYPE_COUNT
  342. };
  343. /* The type of object that a mark is attached to */
  344. enum fsnotify_obj_type {
  345. FSNOTIFY_OBJ_TYPE_ANY = -1,
  346. FSNOTIFY_OBJ_TYPE_INODE,
  347. FSNOTIFY_OBJ_TYPE_VFSMOUNT,
  348. FSNOTIFY_OBJ_TYPE_SB,
  349. FSNOTIFY_OBJ_TYPE_COUNT,
  350. FSNOTIFY_OBJ_TYPE_DETACHED = FSNOTIFY_OBJ_TYPE_COUNT
  351. };
  352. static inline bool fsnotify_valid_obj_type(unsigned int obj_type)
  353. {
  354. return (obj_type < FSNOTIFY_OBJ_TYPE_COUNT);
  355. }
  356. struct fsnotify_iter_info {
  357. struct fsnotify_mark *marks[FSNOTIFY_ITER_TYPE_COUNT];
  358. struct fsnotify_group *current_group;
  359. unsigned int report_mask;
  360. int srcu_idx;
  361. };
  362. static inline bool fsnotify_iter_should_report_type(
  363. struct fsnotify_iter_info *iter_info, int iter_type)
  364. {
  365. return (iter_info->report_mask & (1U << iter_type));
  366. }
  367. static inline void fsnotify_iter_set_report_type(
  368. struct fsnotify_iter_info *iter_info, int iter_type)
  369. {
  370. iter_info->report_mask |= (1U << iter_type);
  371. }
  372. static inline struct fsnotify_mark *fsnotify_iter_mark(
  373. struct fsnotify_iter_info *iter_info, int iter_type)
  374. {
  375. if (fsnotify_iter_should_report_type(iter_info, iter_type))
  376. return iter_info->marks[iter_type];
  377. return NULL;
  378. }
  379. static inline int fsnotify_iter_step(struct fsnotify_iter_info *iter, int type,
  380. struct fsnotify_mark **markp)
  381. {
  382. while (type < FSNOTIFY_ITER_TYPE_COUNT) {
  383. *markp = fsnotify_iter_mark(iter, type);
  384. if (*markp)
  385. break;
  386. type++;
  387. }
  388. return type;
  389. }
  390. #define FSNOTIFY_ITER_FUNCS(name, NAME) \
  391. static inline struct fsnotify_mark *fsnotify_iter_##name##_mark( \
  392. struct fsnotify_iter_info *iter_info) \
  393. { \
  394. return fsnotify_iter_mark(iter_info, FSNOTIFY_ITER_TYPE_##NAME); \
  395. }
  396. FSNOTIFY_ITER_FUNCS(inode, INODE)
  397. FSNOTIFY_ITER_FUNCS(parent, PARENT)
  398. FSNOTIFY_ITER_FUNCS(vfsmount, VFSMOUNT)
  399. FSNOTIFY_ITER_FUNCS(sb, SB)
  400. #define fsnotify_foreach_iter_type(type) \
  401. for (type = 0; type < FSNOTIFY_ITER_TYPE_COUNT; type++)
  402. #define fsnotify_foreach_iter_mark_type(iter, mark, type) \
  403. for (type = 0; \
  404. type = fsnotify_iter_step(iter, type, &mark), \
  405. type < FSNOTIFY_ITER_TYPE_COUNT; \
  406. type++)
  407. /*
  408. * fsnotify_connp_t is what we embed in objects which connector can be attached
  409. * to. fsnotify_connp_t * is how we refer from connector back to object.
  410. */
  411. struct fsnotify_mark_connector;
  412. typedef struct fsnotify_mark_connector __rcu *fsnotify_connp_t;
  413. /*
  414. * Inode/vfsmount/sb point to this structure which tracks all marks attached to
  415. * the inode/vfsmount/sb. The reference to inode/vfsmount/sb is held by this
  416. * structure. We destroy this structure when there are no more marks attached
  417. * to it. The structure is protected by fsnotify_mark_srcu.
  418. */
  419. struct fsnotify_mark_connector {
  420. spinlock_t lock;
  421. unsigned short type; /* Type of object [lock] */
  422. #define FSNOTIFY_CONN_FLAG_HAS_FSID 0x01
  423. #define FSNOTIFY_CONN_FLAG_HAS_IREF 0x02
  424. unsigned short flags; /* flags [lock] */
  425. __kernel_fsid_t fsid; /* fsid of filesystem containing object */
  426. union {
  427. /* Object pointer [lock] */
  428. fsnotify_connp_t *obj;
  429. /* Used listing heads to free after srcu period expires */
  430. struct fsnotify_mark_connector *destroy_next;
  431. };
  432. struct hlist_head list;
  433. };
  434. /*
  435. * A mark is simply an object attached to an in core inode which allows an
  436. * fsnotify listener to indicate they are either no longer interested in events
  437. * of a type matching mask or only interested in those events.
  438. *
  439. * These are flushed when an inode is evicted from core and may be flushed
  440. * when the inode is modified (as seen by fsnotify_access). Some fsnotify
  441. * users (such as dnotify) will flush these when the open fd is closed and not
  442. * at inode eviction or modification.
  443. *
  444. * Text in brackets is showing the lock(s) protecting modifications of a
  445. * particular entry. obj_lock means either inode->i_lock or
  446. * mnt->mnt_root->d_lock depending on the mark type.
  447. */
  448. struct fsnotify_mark {
  449. /* Mask this mark is for [mark->lock, group->mark_mutex] */
  450. __u32 mask;
  451. /* We hold one for presence in g_list. Also one ref for each 'thing'
  452. * in kernel that found and may be using this mark. */
  453. refcount_t refcnt;
  454. /* Group this mark is for. Set on mark creation, stable until last ref
  455. * is dropped */
  456. struct fsnotify_group *group;
  457. /* List of marks by group->marks_list. Also reused for queueing
  458. * mark into destroy_list when it's waiting for the end of SRCU period
  459. * before it can be freed. [group->mark_mutex] */
  460. struct list_head g_list;
  461. /* Protects inode / mnt pointers, flags, masks */
  462. spinlock_t lock;
  463. /* List of marks for inode / vfsmount [connector->lock, mark ref] */
  464. struct hlist_node obj_list;
  465. /* Head of list of marks for an object [mark ref] */
  466. struct fsnotify_mark_connector *connector;
  467. /* Events types and flags to ignore [mark->lock, group->mark_mutex] */
  468. __u32 ignore_mask;
  469. /* General fsnotify mark flags */
  470. #define FSNOTIFY_MARK_FLAG_ALIVE 0x0001
  471. #define FSNOTIFY_MARK_FLAG_ATTACHED 0x0002
  472. /* inotify mark flags */
  473. #define FSNOTIFY_MARK_FLAG_EXCL_UNLINK 0x0010
  474. #define FSNOTIFY_MARK_FLAG_IN_ONESHOT 0x0020
  475. /* fanotify mark flags */
  476. #define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY 0x0100
  477. #define FSNOTIFY_MARK_FLAG_NO_IREF 0x0200
  478. #define FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS 0x0400
  479. unsigned int flags; /* flags [mark->lock] */
  480. };
  481. #ifdef CONFIG_FSNOTIFY
  482. /* called from the vfs helpers */
  483. /* main fsnotify call to send events */
  484. extern int fsnotify(__u32 mask, const void *data, int data_type,
  485. struct inode *dir, const struct qstr *name,
  486. struct inode *inode, u32 cookie);
  487. extern int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
  488. int data_type);
  489. extern void __fsnotify_inode_delete(struct inode *inode);
  490. extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt);
  491. extern void fsnotify_sb_delete(struct super_block *sb);
  492. extern u32 fsnotify_get_cookie(void);
  493. static inline __u32 fsnotify_parent_needed_mask(__u32 mask)
  494. {
  495. /* FS_EVENT_ON_CHILD is set on marks that want parent/name info */
  496. if (!(mask & FS_EVENT_ON_CHILD))
  497. return 0;
  498. /*
  499. * This object might be watched by a mark that cares about parent/name
  500. * info, does it care about the specific set of events that can be
  501. * reported with parent/name info?
  502. */
  503. return mask & FS_EVENTS_POSS_TO_PARENT;
  504. }
  505. static inline int fsnotify_inode_watches_children(struct inode *inode)
  506. {
  507. /* FS_EVENT_ON_CHILD is set if the inode may care */
  508. if (!(inode->i_fsnotify_mask & FS_EVENT_ON_CHILD))
  509. return 0;
  510. /* this inode might care about child events, does it care about the
  511. * specific set of events that can happen on a child? */
  512. return inode->i_fsnotify_mask & FS_EVENTS_POSS_ON_CHILD;
  513. }
  514. /*
  515. * Update the dentry with a flag indicating the interest of its parent to receive
  516. * filesystem events when those events happens to this dentry->d_inode.
  517. */
  518. static inline void fsnotify_update_flags(struct dentry *dentry)
  519. {
  520. assert_spin_locked(&dentry->d_lock);
  521. /*
  522. * Serialisation of setting PARENT_WATCHED on the dentries is provided
  523. * by d_lock. If inotify_inode_watched changes after we have taken
  524. * d_lock, the following __fsnotify_update_child_dentry_flags call will
  525. * find our entry, so it will spin until we complete here, and update
  526. * us with the new state.
  527. */
  528. if (fsnotify_inode_watches_children(dentry->d_parent->d_inode))
  529. dentry->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
  530. else
  531. dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
  532. }
  533. /* called from fsnotify listeners, such as fanotify or dnotify */
  534. /* create a new group */
  535. extern struct fsnotify_group *fsnotify_alloc_group(
  536. const struct fsnotify_ops *ops,
  537. int flags);
  538. /* get reference to a group */
  539. extern void fsnotify_get_group(struct fsnotify_group *group);
  540. /* drop reference on a group from fsnotify_alloc_group */
  541. extern void fsnotify_put_group(struct fsnotify_group *group);
  542. /* group destruction begins, stop queuing new events */
  543. extern void fsnotify_group_stop_queueing(struct fsnotify_group *group);
  544. /* destroy group */
  545. extern void fsnotify_destroy_group(struct fsnotify_group *group);
  546. /* fasync handler function */
  547. extern int fsnotify_fasync(int fd, struct file *file, int on);
  548. /* Free event from memory */
  549. extern void fsnotify_destroy_event(struct fsnotify_group *group,
  550. struct fsnotify_event *event);
  551. /* attach the event to the group notification queue */
  552. extern int fsnotify_insert_event(struct fsnotify_group *group,
  553. struct fsnotify_event *event,
  554. int (*merge)(struct fsnotify_group *,
  555. struct fsnotify_event *),
  556. void (*insert)(struct fsnotify_group *,
  557. struct fsnotify_event *));
  558. static inline int fsnotify_add_event(struct fsnotify_group *group,
  559. struct fsnotify_event *event,
  560. int (*merge)(struct fsnotify_group *,
  561. struct fsnotify_event *))
  562. {
  563. return fsnotify_insert_event(group, event, merge, NULL);
  564. }
  565. /* Queue overflow event to a notification group */
  566. static inline void fsnotify_queue_overflow(struct fsnotify_group *group)
  567. {
  568. fsnotify_add_event(group, group->overflow_event, NULL);
  569. }
  570. static inline bool fsnotify_is_overflow_event(u32 mask)
  571. {
  572. return mask & FS_Q_OVERFLOW;
  573. }
  574. static inline bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
  575. {
  576. assert_spin_locked(&group->notification_lock);
  577. return list_empty(&group->notification_list);
  578. }
  579. extern bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group);
  580. /* return, but do not dequeue the first event on the notification queue */
  581. extern struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group);
  582. /* return AND dequeue the first event on the notification queue */
  583. extern struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group);
  584. /* Remove event queued in the notification list */
  585. extern void fsnotify_remove_queued_event(struct fsnotify_group *group,
  586. struct fsnotify_event *event);
  587. /* functions used to manipulate the marks attached to inodes */
  588. /*
  589. * Canonical "ignore mask" including event flags.
  590. *
  591. * Note the subtle semantic difference from the legacy ->ignored_mask.
  592. * ->ignored_mask traditionally only meant which events should be ignored,
  593. * while ->ignore_mask also includes flags regarding the type of objects on
  594. * which events should be ignored.
  595. */
  596. static inline __u32 fsnotify_ignore_mask(struct fsnotify_mark *mark)
  597. {
  598. __u32 ignore_mask = mark->ignore_mask;
  599. /* The event flags in ignore mask take effect */
  600. if (mark->flags & FSNOTIFY_MARK_FLAG_HAS_IGNORE_FLAGS)
  601. return ignore_mask;
  602. /*
  603. * Legacy behavior:
  604. * - Always ignore events on dir
  605. * - Ignore events on child if parent is watching children
  606. */
  607. ignore_mask |= FS_ISDIR;
  608. ignore_mask &= ~FS_EVENT_ON_CHILD;
  609. ignore_mask |= mark->mask & FS_EVENT_ON_CHILD;
  610. return ignore_mask;
  611. }
  612. /* Legacy ignored_mask - only event types to ignore */
  613. static inline __u32 fsnotify_ignored_events(struct fsnotify_mark *mark)
  614. {
  615. return mark->ignore_mask & ALL_FSNOTIFY_EVENTS;
  616. }
  617. /*
  618. * Check if mask (or ignore mask) should be applied depending if victim is a
  619. * directory and whether it is reported to a watching parent.
  620. */
  621. static inline bool fsnotify_mask_applicable(__u32 mask, bool is_dir,
  622. int iter_type)
  623. {
  624. /* Should mask be applied to a directory? */
  625. if (is_dir && !(mask & FS_ISDIR))
  626. return false;
  627. /* Should mask be applied to a child? */
  628. if (iter_type == FSNOTIFY_ITER_TYPE_PARENT &&
  629. !(mask & FS_EVENT_ON_CHILD))
  630. return false;
  631. return true;
  632. }
  633. /*
  634. * Effective ignore mask taking into account if event victim is a
  635. * directory and whether it is reported to a watching parent.
  636. */
  637. static inline __u32 fsnotify_effective_ignore_mask(struct fsnotify_mark *mark,
  638. bool is_dir, int iter_type)
  639. {
  640. __u32 ignore_mask = fsnotify_ignored_events(mark);
  641. if (!ignore_mask)
  642. return 0;
  643. /* For non-dir and non-child, no need to consult the event flags */
  644. if (!is_dir && iter_type != FSNOTIFY_ITER_TYPE_PARENT)
  645. return ignore_mask;
  646. ignore_mask = fsnotify_ignore_mask(mark);
  647. if (!fsnotify_mask_applicable(ignore_mask, is_dir, iter_type))
  648. return 0;
  649. return ignore_mask & ALL_FSNOTIFY_EVENTS;
  650. }
  651. /* Get mask for calculating object interest taking ignore mask into account */
  652. static inline __u32 fsnotify_calc_mask(struct fsnotify_mark *mark)
  653. {
  654. __u32 mask = mark->mask;
  655. if (!fsnotify_ignored_events(mark))
  656. return mask;
  657. /* Interest in FS_MODIFY may be needed for clearing ignore mask */
  658. if (!(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
  659. mask |= FS_MODIFY;
  660. /*
  661. * If mark is interested in ignoring events on children, the object must
  662. * show interest in those events for fsnotify_parent() to notice it.
  663. */
  664. return mask | mark->ignore_mask;
  665. }
  666. /* Get mask of events for a list of marks */
  667. extern __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn);
  668. /* Calculate mask of events for a list of marks */
  669. extern void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn);
  670. extern void fsnotify_init_mark(struct fsnotify_mark *mark,
  671. struct fsnotify_group *group);
  672. /* Find mark belonging to given group in the list of marks */
  673. extern struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
  674. struct fsnotify_group *group);
  675. /* Get cached fsid of filesystem containing object */
  676. extern int fsnotify_get_conn_fsid(const struct fsnotify_mark_connector *conn,
  677. __kernel_fsid_t *fsid);
  678. /* attach the mark to the object */
  679. extern int fsnotify_add_mark(struct fsnotify_mark *mark,
  680. fsnotify_connp_t *connp, unsigned int obj_type,
  681. int add_flags, __kernel_fsid_t *fsid);
  682. extern int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
  683. fsnotify_connp_t *connp,
  684. unsigned int obj_type, int add_flags,
  685. __kernel_fsid_t *fsid);
  686. /* attach the mark to the inode */
  687. static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
  688. struct inode *inode,
  689. int add_flags)
  690. {
  691. return fsnotify_add_mark(mark, &inode->i_fsnotify_marks,
  692. FSNOTIFY_OBJ_TYPE_INODE, add_flags, NULL);
  693. }
  694. static inline int fsnotify_add_inode_mark_locked(struct fsnotify_mark *mark,
  695. struct inode *inode,
  696. int add_flags)
  697. {
  698. return fsnotify_add_mark_locked(mark, &inode->i_fsnotify_marks,
  699. FSNOTIFY_OBJ_TYPE_INODE, add_flags,
  700. NULL);
  701. }
  702. /* given a group and a mark, flag mark to be freed when all references are dropped */
  703. extern void fsnotify_destroy_mark(struct fsnotify_mark *mark,
  704. struct fsnotify_group *group);
  705. /* detach mark from inode / mount list, group list, drop inode reference */
  706. extern void fsnotify_detach_mark(struct fsnotify_mark *mark);
  707. /* free mark */
  708. extern void fsnotify_free_mark(struct fsnotify_mark *mark);
  709. /* Wait until all marks queued for destruction are destroyed */
  710. extern void fsnotify_wait_marks_destroyed(void);
  711. /* Clear all of the marks of a group attached to a given object type */
  712. extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
  713. unsigned int obj_type);
  714. /* run all the marks in a group, and clear all of the vfsmount marks */
  715. static inline void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group)
  716. {
  717. fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_VFSMOUNT);
  718. }
  719. /* run all the marks in a group, and clear all of the inode marks */
  720. static inline void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
  721. {
  722. fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_INODE);
  723. }
  724. /* run all the marks in a group, and clear all of the sn marks */
  725. static inline void fsnotify_clear_sb_marks_by_group(struct fsnotify_group *group)
  726. {
  727. fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_SB);
  728. }
  729. extern void fsnotify_get_mark(struct fsnotify_mark *mark);
  730. extern void fsnotify_put_mark(struct fsnotify_mark *mark);
  731. extern void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info);
  732. extern bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info);
  733. static inline void fsnotify_init_event(struct fsnotify_event *event)
  734. {
  735. INIT_LIST_HEAD(&event->list);
  736. }
  737. #else
  738. static inline int fsnotify(__u32 mask, const void *data, int data_type,
  739. struct inode *dir, const struct qstr *name,
  740. struct inode *inode, u32 cookie)
  741. {
  742. return 0;
  743. }
  744. static inline int __fsnotify_parent(struct dentry *dentry, __u32 mask,
  745. const void *data, int data_type)
  746. {
  747. return 0;
  748. }
  749. static inline void __fsnotify_inode_delete(struct inode *inode)
  750. {}
  751. static inline void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
  752. {}
  753. static inline void fsnotify_sb_delete(struct super_block *sb)
  754. {}
  755. static inline void fsnotify_update_flags(struct dentry *dentry)
  756. {}
  757. static inline u32 fsnotify_get_cookie(void)
  758. {
  759. return 0;
  760. }
  761. static inline void fsnotify_unmount_inodes(struct super_block *sb)
  762. {}
  763. #endif /* CONFIG_FSNOTIFY */
  764. #endif /* __KERNEL __ */
  765. #endif /* __LINUX_FSNOTIFY_BACKEND_H */