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