1
2
3
4
5
6
7
8#ifndef __LINUX_FSNOTIFY_BACKEND_H
9#define __LINUX_FSNOTIFY_BACKEND_H
10
11#ifdef __KERNEL__
12
13#include <linux/idr.h>
14#include <linux/fs.h>
15#include <linux/list.h>
16#include <linux/path.h>
17#include <linux/spinlock.h>
18#include <linux/types.h>
19#include <linux/atomic.h>
20#include <linux/user_namespace.h>
21#include <linux/refcount.h>
22
23
24
25
26
27
28
29#define FS_ACCESS 0x00000001
30#define FS_MODIFY 0x00000002
31#define FS_ATTRIB 0x00000004
32#define FS_CLOSE_WRITE 0x00000008
33#define FS_CLOSE_NOWRITE 0x00000010
34#define FS_OPEN 0x00000020
35#define FS_MOVED_FROM 0x00000040
36#define FS_MOVED_TO 0x00000080
37#define FS_CREATE 0x00000100
38#define FS_DELETE 0x00000200
39#define FS_DELETE_SELF 0x00000400
40#define FS_MOVE_SELF 0x00000800
41#define FS_OPEN_EXEC 0x00001000
42
43#define FS_UNMOUNT 0x00002000
44#define FS_Q_OVERFLOW 0x00004000
45#define FS_IN_IGNORED 0x00008000
46
47#define FS_OPEN_PERM 0x00010000
48#define FS_ACCESS_PERM 0x00020000
49#define FS_OPEN_EXEC_PERM 0x00040000
50#define FS_DIR_MODIFY 0x00080000
51
52#define FS_EXCL_UNLINK 0x04000000
53
54
55#define FS_EVENT_ON_CHILD 0x08000000
56
57#define FS_DN_RENAME 0x10000000
58#define FS_DN_MULTISHOT 0x20000000
59#define FS_ISDIR 0x40000000
60#define FS_IN_ONESHOT 0x80000000
61
62#define FS_MOVE (FS_MOVED_FROM | FS_MOVED_TO)
63
64
65
66
67
68
69
70#define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | \
71 FS_DIR_MODIFY)
72
73#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \
74 FS_OPEN_EXEC_PERM)
75
76
77
78
79
80#define FS_EVENTS_POSS_ON_CHILD (ALL_FSNOTIFY_PERM_EVENTS | \
81 FS_ACCESS | FS_MODIFY | FS_ATTRIB | \
82 FS_CLOSE_WRITE | FS_CLOSE_NOWRITE | \
83 FS_OPEN | FS_OPEN_EXEC)
84
85
86#define ALL_FSNOTIFY_EVENTS (ALL_FSNOTIFY_DIRENT_EVENTS | \
87 FS_EVENTS_POSS_ON_CHILD | \
88 FS_DELETE_SELF | FS_MOVE_SELF | FS_DN_RENAME | \
89 FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED)
90
91
92#define ALL_FSNOTIFY_FLAGS (FS_EXCL_UNLINK | FS_ISDIR | FS_IN_ONESHOT | \
93 FS_DN_MULTISHOT | FS_EVENT_ON_CHILD)
94
95#define ALL_FSNOTIFY_BITS (ALL_FSNOTIFY_EVENTS | ALL_FSNOTIFY_FLAGS)
96
97struct fsnotify_group;
98struct fsnotify_event;
99struct fsnotify_mark;
100struct fsnotify_event_private_data;
101struct fsnotify_fname;
102struct fsnotify_iter_info;
103
104struct mem_cgroup;
105
106
107
108
109
110
111
112
113
114
115
116
117struct fsnotify_ops {
118 int (*handle_event)(struct fsnotify_group *group,
119 struct inode *inode,
120 u32 mask, const void *data, int data_type,
121 const struct qstr *file_name, u32 cookie,
122 struct fsnotify_iter_info *iter_info);
123 void (*free_group_priv)(struct fsnotify_group *group);
124 void (*freeing_mark)(struct fsnotify_mark *mark, struct fsnotify_group *group);
125 void (*free_event)(struct fsnotify_event *event);
126
127 void (*free_mark)(struct fsnotify_mark *mark);
128};
129
130
131
132
133
134
135struct fsnotify_event {
136 struct list_head list;
137 unsigned long objectid;
138};
139
140
141
142
143
144
145
146struct fsnotify_group {
147 const struct fsnotify_ops *ops;
148
149
150
151
152
153
154
155
156
157 refcount_t refcnt;
158
159
160 spinlock_t notification_lock;
161 struct list_head notification_list;
162 wait_queue_head_t notification_waitq;
163 unsigned int q_len;
164 unsigned int max_events;
165
166
167
168
169 #define FS_PRIO_0 0
170 #define FS_PRIO_1 1
171 #define FS_PRIO_2 2
172 unsigned int priority;
173 bool shutdown;
174
175
176 struct mutex mark_mutex;
177 atomic_t num_marks;
178
179
180 atomic_t user_waits;
181
182 struct list_head marks_list;
183
184 struct fasync_struct *fsn_fa;
185
186 struct fsnotify_event *overflow_event;
187
188
189
190 struct mem_cgroup *memcg;
191
192
193 union {
194 void *private;
195#ifdef CONFIG_INOTIFY_USER
196 struct inotify_group_private_data {
197 spinlock_t idr_lock;
198 struct idr idr;
199 struct ucounts *ucounts;
200 } inotify_data;
201#endif
202#ifdef CONFIG_FANOTIFY
203 struct fanotify_group_private_data {
204
205 struct list_head access_list;
206 wait_queue_head_t access_waitq;
207 int flags;
208 int f_flags;
209 unsigned int max_marks;
210 struct user_struct *user;
211 } fanotify_data;
212#endif
213 };
214};
215
216
217enum fsnotify_data_type {
218 FSNOTIFY_EVENT_NONE,
219 FSNOTIFY_EVENT_PATH,
220 FSNOTIFY_EVENT_INODE,
221};
222
223static inline const struct inode *fsnotify_data_inode(const void *data,
224 int data_type)
225{
226 switch (data_type) {
227 case FSNOTIFY_EVENT_INODE:
228 return data;
229 case FSNOTIFY_EVENT_PATH:
230 return d_inode(((const struct path *)data)->dentry);
231 default:
232 return NULL;
233 }
234}
235
236static inline const struct path *fsnotify_data_path(const void *data,
237 int data_type)
238{
239 switch (data_type) {
240 case FSNOTIFY_EVENT_PATH:
241 return data;
242 default:
243 return NULL;
244 }
245}
246
247enum fsnotify_obj_type {
248 FSNOTIFY_OBJ_TYPE_INODE,
249 FSNOTIFY_OBJ_TYPE_VFSMOUNT,
250 FSNOTIFY_OBJ_TYPE_SB,
251 FSNOTIFY_OBJ_TYPE_COUNT,
252 FSNOTIFY_OBJ_TYPE_DETACHED = FSNOTIFY_OBJ_TYPE_COUNT
253};
254
255#define FSNOTIFY_OBJ_TYPE_INODE_FL (1U << FSNOTIFY_OBJ_TYPE_INODE)
256#define FSNOTIFY_OBJ_TYPE_VFSMOUNT_FL (1U << FSNOTIFY_OBJ_TYPE_VFSMOUNT)
257#define FSNOTIFY_OBJ_TYPE_SB_FL (1U << FSNOTIFY_OBJ_TYPE_SB)
258#define FSNOTIFY_OBJ_ALL_TYPES_MASK ((1U << FSNOTIFY_OBJ_TYPE_COUNT) - 1)
259
260static inline bool fsnotify_valid_obj_type(unsigned int type)
261{
262 return (type < FSNOTIFY_OBJ_TYPE_COUNT);
263}
264
265struct fsnotify_iter_info {
266 struct fsnotify_mark *marks[FSNOTIFY_OBJ_TYPE_COUNT];
267 unsigned int report_mask;
268 int srcu_idx;
269};
270
271static inline bool fsnotify_iter_should_report_type(
272 struct fsnotify_iter_info *iter_info, int type)
273{
274 return (iter_info->report_mask & (1U << type));
275}
276
277static inline void fsnotify_iter_set_report_type(
278 struct fsnotify_iter_info *iter_info, int type)
279{
280 iter_info->report_mask |= (1U << type);
281}
282
283static inline void fsnotify_iter_set_report_type_mark(
284 struct fsnotify_iter_info *iter_info, int type,
285 struct fsnotify_mark *mark)
286{
287 iter_info->marks[type] = mark;
288 iter_info->report_mask |= (1U << type);
289}
290
291#define FSNOTIFY_ITER_FUNCS(name, NAME) \
292static inline struct fsnotify_mark *fsnotify_iter_##name##_mark( \
293 struct fsnotify_iter_info *iter_info) \
294{ \
295 return (iter_info->report_mask & FSNOTIFY_OBJ_TYPE_##NAME##_FL) ? \
296 iter_info->marks[FSNOTIFY_OBJ_TYPE_##NAME] : NULL; \
297}
298
299FSNOTIFY_ITER_FUNCS(inode, INODE)
300FSNOTIFY_ITER_FUNCS(vfsmount, VFSMOUNT)
301FSNOTIFY_ITER_FUNCS(sb, SB)
302
303#define fsnotify_foreach_obj_type(type) \
304 for (type = 0; type < FSNOTIFY_OBJ_TYPE_COUNT; type++)
305
306
307
308
309
310struct fsnotify_mark_connector;
311typedef struct fsnotify_mark_connector __rcu *fsnotify_connp_t;
312
313
314
315
316
317
318
319struct fsnotify_mark_connector {
320 spinlock_t lock;
321 unsigned short type;
322#define FSNOTIFY_CONN_FLAG_HAS_FSID 0x01
323 unsigned short flags;
324 __kernel_fsid_t fsid;
325 union {
326
327 fsnotify_connp_t *obj;
328
329 struct fsnotify_mark_connector *destroy_next;
330 };
331 struct hlist_head list;
332};
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348struct fsnotify_mark {
349
350 __u32 mask;
351
352
353 refcount_t refcnt;
354
355
356 struct fsnotify_group *group;
357
358
359
360 struct list_head g_list;
361
362 spinlock_t lock;
363
364 struct hlist_node obj_list;
365
366 struct fsnotify_mark_connector *connector;
367
368 __u32 ignored_mask;
369#define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY 0x01
370#define FSNOTIFY_MARK_FLAG_ALIVE 0x02
371#define FSNOTIFY_MARK_FLAG_ATTACHED 0x04
372 unsigned int flags;
373};
374
375#ifdef CONFIG_FSNOTIFY
376
377
378
379
380extern int fsnotify(struct inode *to_tell, __u32 mask, const void *data,
381 int data_type, const struct qstr *name, u32 cookie);
382extern int fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
383 int data_type);
384extern void __fsnotify_inode_delete(struct inode *inode);
385extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt);
386extern void fsnotify_sb_delete(struct super_block *sb);
387extern u32 fsnotify_get_cookie(void);
388
389static inline int fsnotify_inode_watches_children(struct inode *inode)
390{
391
392 if (!(inode->i_fsnotify_mask & FS_EVENT_ON_CHILD))
393 return 0;
394
395
396 return inode->i_fsnotify_mask & FS_EVENTS_POSS_ON_CHILD;
397}
398
399
400
401
402
403static inline void fsnotify_update_flags(struct dentry *dentry)
404{
405 assert_spin_locked(&dentry->d_lock);
406
407
408
409
410
411
412
413
414 if (fsnotify_inode_watches_children(dentry->d_parent->d_inode))
415 dentry->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
416 else
417 dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
418}
419
420
421
422
423extern struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops);
424
425extern void fsnotify_get_group(struct fsnotify_group *group);
426
427extern void fsnotify_put_group(struct fsnotify_group *group);
428
429extern void fsnotify_group_stop_queueing(struct fsnotify_group *group);
430
431extern void fsnotify_destroy_group(struct fsnotify_group *group);
432
433extern int fsnotify_fasync(int fd, struct file *file, int on);
434
435extern void fsnotify_destroy_event(struct fsnotify_group *group,
436 struct fsnotify_event *event);
437
438extern int fsnotify_add_event(struct fsnotify_group *group,
439 struct fsnotify_event *event,
440 int (*merge)(struct list_head *,
441 struct fsnotify_event *));
442
443static inline void fsnotify_queue_overflow(struct fsnotify_group *group)
444{
445 fsnotify_add_event(group, group->overflow_event, NULL);
446}
447
448
449extern bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group);
450
451extern struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group);
452
453extern struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group);
454
455extern void fsnotify_remove_queued_event(struct fsnotify_group *group,
456 struct fsnotify_event *event);
457
458
459
460
461extern __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn);
462
463extern void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn);
464extern void fsnotify_init_mark(struct fsnotify_mark *mark,
465 struct fsnotify_group *group);
466
467extern struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
468 struct fsnotify_group *group);
469
470extern int fsnotify_get_conn_fsid(const struct fsnotify_mark_connector *conn,
471 __kernel_fsid_t *fsid);
472
473extern int fsnotify_add_mark(struct fsnotify_mark *mark,
474 fsnotify_connp_t *connp, unsigned int type,
475 int allow_dups, __kernel_fsid_t *fsid);
476extern int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
477 fsnotify_connp_t *connp,
478 unsigned int type, int allow_dups,
479 __kernel_fsid_t *fsid);
480
481
482static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
483 struct inode *inode,
484 int allow_dups)
485{
486 return fsnotify_add_mark(mark, &inode->i_fsnotify_marks,
487 FSNOTIFY_OBJ_TYPE_INODE, allow_dups, NULL);
488}
489static inline int fsnotify_add_inode_mark_locked(struct fsnotify_mark *mark,
490 struct inode *inode,
491 int allow_dups)
492{
493 return fsnotify_add_mark_locked(mark, &inode->i_fsnotify_marks,
494 FSNOTIFY_OBJ_TYPE_INODE, allow_dups,
495 NULL);
496}
497
498
499extern void fsnotify_destroy_mark(struct fsnotify_mark *mark,
500 struct fsnotify_group *group);
501
502extern void fsnotify_detach_mark(struct fsnotify_mark *mark);
503
504extern void fsnotify_free_mark(struct fsnotify_mark *mark);
505
506extern void fsnotify_wait_marks_destroyed(void);
507
508extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group, unsigned int type);
509
510static inline void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group)
511{
512 fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_VFSMOUNT_FL);
513}
514
515static inline void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
516{
517 fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_INODE_FL);
518}
519
520static inline void fsnotify_clear_sb_marks_by_group(struct fsnotify_group *group)
521{
522 fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_SB_FL);
523}
524extern void fsnotify_get_mark(struct fsnotify_mark *mark);
525extern void fsnotify_put_mark(struct fsnotify_mark *mark);
526extern void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info);
527extern bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info);
528
529static inline void fsnotify_init_event(struct fsnotify_event *event,
530 unsigned long objectid)
531{
532 INIT_LIST_HEAD(&event->list);
533 event->objectid = objectid;
534}
535
536#else
537
538static inline int fsnotify(struct inode *to_tell, __u32 mask, const void *data,
539 int data_type, const struct qstr *name, u32 cookie)
540{
541 return 0;
542}
543
544static inline int fsnotify_parent(struct dentry *dentry, __u32 mask,
545 const void *data, int data_type)
546{
547 return 0;
548}
549
550static inline void __fsnotify_inode_delete(struct inode *inode)
551{}
552
553static inline void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
554{}
555
556static inline void fsnotify_sb_delete(struct super_block *sb)
557{}
558
559static inline void fsnotify_update_flags(struct dentry *dentry)
560{}
561
562static inline u32 fsnotify_get_cookie(void)
563{
564 return 0;
565}
566
567static inline void fsnotify_unmount_inodes(struct super_block *sb)
568{}
569
570#endif
571
572#endif
573
574#endif
575