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#include <linux/mempool.h>
23
24
25
26
27
28
29
30#define FS_ACCESS 0x00000001
31#define FS_MODIFY 0x00000002
32#define FS_ATTRIB 0x00000004
33#define FS_CLOSE_WRITE 0x00000008
34#define FS_CLOSE_NOWRITE 0x00000010
35#define FS_OPEN 0x00000020
36#define FS_MOVED_FROM 0x00000040
37#define FS_MOVED_TO 0x00000080
38#define FS_CREATE 0x00000100
39#define FS_DELETE 0x00000200
40#define FS_DELETE_SELF 0x00000400
41#define FS_MOVE_SELF 0x00000800
42#define FS_OPEN_EXEC 0x00001000
43
44#define FS_UNMOUNT 0x00002000
45#define FS_Q_OVERFLOW 0x00004000
46#define FS_ERROR 0x00008000
47
48
49
50
51
52#define FS_IN_IGNORED 0x00008000
53
54#define FS_OPEN_PERM 0x00010000
55#define FS_ACCESS_PERM 0x00020000
56#define FS_OPEN_EXEC_PERM 0x00040000
57
58#define FS_EXCL_UNLINK 0x04000000
59
60
61
62
63
64#define FS_EVENT_ON_CHILD 0x08000000
65
66#define FS_RENAME 0x10000000
67#define FS_DN_MULTISHOT 0x20000000
68#define FS_ISDIR 0x40000000
69#define FS_IN_ONESHOT 0x80000000
70
71#define FS_MOVE (FS_MOVED_FROM | FS_MOVED_TO)
72
73
74
75
76
77
78
79#define ALL_FSNOTIFY_DIRENT_EVENTS (FS_CREATE | FS_DELETE | FS_MOVE | FS_RENAME)
80
81#define ALL_FSNOTIFY_PERM_EVENTS (FS_OPEN_PERM | FS_ACCESS_PERM | \
82 FS_OPEN_EXEC_PERM)
83
84
85
86
87
88#define FS_EVENTS_POSS_ON_CHILD (ALL_FSNOTIFY_PERM_EVENTS | \
89 FS_ACCESS | FS_MODIFY | FS_ATTRIB | \
90 FS_CLOSE_WRITE | FS_CLOSE_NOWRITE | \
91 FS_OPEN | FS_OPEN_EXEC)
92
93
94
95
96
97
98
99#define FS_EVENTS_POSS_TO_PARENT (FS_EVENTS_POSS_ON_CHILD)
100
101
102#define ALL_FSNOTIFY_EVENTS (ALL_FSNOTIFY_DIRENT_EVENTS | \
103 FS_EVENTS_POSS_ON_CHILD | \
104 FS_DELETE_SELF | FS_MOVE_SELF | \
105 FS_UNMOUNT | FS_Q_OVERFLOW | FS_IN_IGNORED | \
106 FS_ERROR)
107
108
109#define ALL_FSNOTIFY_FLAGS (FS_EXCL_UNLINK | FS_ISDIR | FS_IN_ONESHOT | \
110 FS_DN_MULTISHOT | FS_EVENT_ON_CHILD)
111
112#define ALL_FSNOTIFY_BITS (ALL_FSNOTIFY_EVENTS | ALL_FSNOTIFY_FLAGS)
113
114struct fsnotify_group;
115struct fsnotify_event;
116struct fsnotify_mark;
117struct fsnotify_event_private_data;
118struct fsnotify_fname;
119struct fsnotify_iter_info;
120
121struct mem_cgroup;
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
147
148
149
150
151
152
153
154
155
156
157struct fsnotify_ops {
158 int (*handle_event)(struct fsnotify_group *group, u32 mask,
159 const void *data, int data_type, struct inode *dir,
160 const struct qstr *file_name, u32 cookie,
161 struct fsnotify_iter_info *iter_info);
162 int (*handle_inode_event)(struct fsnotify_mark *mark, u32 mask,
163 struct inode *inode, struct inode *dir,
164 const struct qstr *file_name, u32 cookie);
165 void (*free_group_priv)(struct fsnotify_group *group);
166 void (*freeing_mark)(struct fsnotify_mark *mark, struct fsnotify_group *group);
167 void (*free_event)(struct fsnotify_group *group, struct fsnotify_event *event);
168
169 void (*free_mark)(struct fsnotify_mark *mark);
170};
171
172
173
174
175
176
177struct fsnotify_event {
178 struct list_head list;
179};
180
181
182
183
184
185
186
187struct fsnotify_group {
188 const struct fsnotify_ops *ops;
189
190
191
192
193
194
195
196
197
198 refcount_t refcnt;
199
200
201 spinlock_t notification_lock;
202 struct list_head notification_list;
203 wait_queue_head_t notification_waitq;
204 unsigned int q_len;
205 unsigned int max_events;
206
207
208
209
210 #define FS_PRIO_0 0
211 #define FS_PRIO_1 1
212 #define FS_PRIO_2 2
213 unsigned int priority;
214 bool shutdown;
215
216
217 struct mutex mark_mutex;
218 atomic_t user_waits;
219
220 struct list_head marks_list;
221
222 struct fasync_struct *fsn_fa;
223
224 struct fsnotify_event *overflow_event;
225
226
227
228 struct mem_cgroup *memcg;
229
230
231 union {
232 void *private;
233#ifdef CONFIG_INOTIFY_USER
234 struct inotify_group_private_data {
235 spinlock_t idr_lock;
236 struct idr idr;
237 struct ucounts *ucounts;
238 } inotify_data;
239#endif
240#ifdef CONFIG_FANOTIFY
241 struct fanotify_group_private_data {
242
243 struct hlist_head *merge_hash;
244
245 struct list_head access_list;
246 wait_queue_head_t access_waitq;
247 int flags;
248 int f_flags;
249 struct ucounts *ucounts;
250 mempool_t error_events_pool;
251 } fanotify_data;
252#endif
253 };
254};
255
256
257enum fsnotify_data_type {
258 FSNOTIFY_EVENT_NONE,
259 FSNOTIFY_EVENT_PATH,
260 FSNOTIFY_EVENT_INODE,
261 FSNOTIFY_EVENT_DENTRY,
262 FSNOTIFY_EVENT_ERROR,
263};
264
265struct fs_error_report {
266 int error;
267 struct inode *inode;
268 struct super_block *sb;
269};
270
271static inline struct inode *fsnotify_data_inode(const void *data, int data_type)
272{
273 switch (data_type) {
274 case FSNOTIFY_EVENT_INODE:
275 return (struct inode *)data;
276 case FSNOTIFY_EVENT_DENTRY:
277 return d_inode(data);
278 case FSNOTIFY_EVENT_PATH:
279 return d_inode(((const struct path *)data)->dentry);
280 case FSNOTIFY_EVENT_ERROR:
281 return ((struct fs_error_report *)data)->inode;
282 default:
283 return NULL;
284 }
285}
286
287static inline struct dentry *fsnotify_data_dentry(const void *data, int data_type)
288{
289 switch (data_type) {
290 case FSNOTIFY_EVENT_DENTRY:
291
292 return (struct dentry *)data;
293 case FSNOTIFY_EVENT_PATH:
294 return ((const struct path *)data)->dentry;
295 default:
296 return NULL;
297 }
298}
299
300static inline const struct path *fsnotify_data_path(const void *data,
301 int data_type)
302{
303 switch (data_type) {
304 case FSNOTIFY_EVENT_PATH:
305 return data;
306 default:
307 return NULL;
308 }
309}
310
311static inline struct super_block *fsnotify_data_sb(const void *data,
312 int data_type)
313{
314 switch (data_type) {
315 case FSNOTIFY_EVENT_INODE:
316 return ((struct inode *)data)->i_sb;
317 case FSNOTIFY_EVENT_DENTRY:
318 return ((struct dentry *)data)->d_sb;
319 case FSNOTIFY_EVENT_PATH:
320 return ((const struct path *)data)->dentry->d_sb;
321 case FSNOTIFY_EVENT_ERROR:
322 return ((struct fs_error_report *) data)->sb;
323 default:
324 return NULL;
325 }
326}
327
328static inline struct fs_error_report *fsnotify_data_error_report(
329 const void *data,
330 int data_type)
331{
332 switch (data_type) {
333 case FSNOTIFY_EVENT_ERROR:
334 return (struct fs_error_report *) data;
335 default:
336 return NULL;
337 }
338}
339
340
341
342
343
344
345
346
347enum fsnotify_iter_type {
348 FSNOTIFY_ITER_TYPE_INODE,
349 FSNOTIFY_ITER_TYPE_VFSMOUNT,
350 FSNOTIFY_ITER_TYPE_SB,
351 FSNOTIFY_ITER_TYPE_PARENT,
352 FSNOTIFY_ITER_TYPE_INODE2,
353 FSNOTIFY_ITER_TYPE_COUNT
354};
355
356
357enum fsnotify_obj_type {
358 FSNOTIFY_OBJ_TYPE_ANY = -1,
359 FSNOTIFY_OBJ_TYPE_INODE,
360 FSNOTIFY_OBJ_TYPE_VFSMOUNT,
361 FSNOTIFY_OBJ_TYPE_SB,
362 FSNOTIFY_OBJ_TYPE_COUNT,
363 FSNOTIFY_OBJ_TYPE_DETACHED = FSNOTIFY_OBJ_TYPE_COUNT
364};
365
366static inline bool fsnotify_valid_obj_type(unsigned int obj_type)
367{
368 return (obj_type < FSNOTIFY_OBJ_TYPE_COUNT);
369}
370
371struct fsnotify_iter_info {
372 struct fsnotify_mark *marks[FSNOTIFY_ITER_TYPE_COUNT];
373 unsigned int report_mask;
374 int srcu_idx;
375};
376
377static inline bool fsnotify_iter_should_report_type(
378 struct fsnotify_iter_info *iter_info, int iter_type)
379{
380 return (iter_info->report_mask & (1U << iter_type));
381}
382
383static inline void fsnotify_iter_set_report_type(
384 struct fsnotify_iter_info *iter_info, int iter_type)
385{
386 iter_info->report_mask |= (1U << iter_type);
387}
388
389static inline void fsnotify_iter_set_report_type_mark(
390 struct fsnotify_iter_info *iter_info, int iter_type,
391 struct fsnotify_mark *mark)
392{
393 iter_info->marks[iter_type] = mark;
394 iter_info->report_mask |= (1U << iter_type);
395}
396
397#define FSNOTIFY_ITER_FUNCS(name, NAME) \
398static inline struct fsnotify_mark *fsnotify_iter_##name##_mark( \
399 struct fsnotify_iter_info *iter_info) \
400{ \
401 return (iter_info->report_mask & (1U << FSNOTIFY_ITER_TYPE_##NAME)) ? \
402 iter_info->marks[FSNOTIFY_ITER_TYPE_##NAME] : NULL; \
403}
404
405FSNOTIFY_ITER_FUNCS(inode, INODE)
406FSNOTIFY_ITER_FUNCS(parent, PARENT)
407FSNOTIFY_ITER_FUNCS(vfsmount, VFSMOUNT)
408FSNOTIFY_ITER_FUNCS(sb, SB)
409
410#define fsnotify_foreach_iter_type(type) \
411 for (type = 0; type < FSNOTIFY_ITER_TYPE_COUNT; type++)
412
413
414
415
416
417struct fsnotify_mark_connector;
418typedef struct fsnotify_mark_connector __rcu *fsnotify_connp_t;
419
420
421
422
423
424
425
426struct fsnotify_mark_connector {
427 spinlock_t lock;
428 unsigned short type;
429#define FSNOTIFY_CONN_FLAG_HAS_FSID 0x01
430 unsigned short flags;
431 __kernel_fsid_t fsid;
432 union {
433
434 fsnotify_connp_t *obj;
435
436 struct fsnotify_mark_connector *destroy_next;
437 };
438 struct hlist_head list;
439};
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455struct fsnotify_mark {
456
457 __u32 mask;
458
459
460 refcount_t refcnt;
461
462
463 struct fsnotify_group *group;
464
465
466
467 struct list_head g_list;
468
469 spinlock_t lock;
470
471 struct hlist_node obj_list;
472
473 struct fsnotify_mark_connector *connector;
474
475 __u32 ignored_mask;
476#define FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY 0x01
477#define FSNOTIFY_MARK_FLAG_ALIVE 0x02
478#define FSNOTIFY_MARK_FLAG_ATTACHED 0x04
479 unsigned int flags;
480};
481
482#ifdef CONFIG_FSNOTIFY
483
484
485
486
487extern int fsnotify(__u32 mask, const void *data, int data_type,
488 struct inode *dir, const struct qstr *name,
489 struct inode *inode, u32 cookie);
490extern int __fsnotify_parent(struct dentry *dentry, __u32 mask, const void *data,
491 int data_type);
492extern void __fsnotify_inode_delete(struct inode *inode);
493extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt);
494extern void fsnotify_sb_delete(struct super_block *sb);
495extern u32 fsnotify_get_cookie(void);
496
497static inline __u32 fsnotify_parent_needed_mask(__u32 mask)
498{
499
500 if (!(mask & FS_EVENT_ON_CHILD))
501 return 0;
502
503
504
505
506
507 return mask & FS_EVENTS_POSS_TO_PARENT;
508}
509
510static inline int fsnotify_inode_watches_children(struct inode *inode)
511{
512
513 if (!(inode->i_fsnotify_mask & FS_EVENT_ON_CHILD))
514 return 0;
515
516
517 return inode->i_fsnotify_mask & FS_EVENTS_POSS_ON_CHILD;
518}
519
520
521
522
523
524static inline void fsnotify_update_flags(struct dentry *dentry)
525{
526 assert_spin_locked(&dentry->d_lock);
527
528
529
530
531
532
533
534
535 if (fsnotify_inode_watches_children(dentry->d_parent->d_inode))
536 dentry->d_flags |= DCACHE_FSNOTIFY_PARENT_WATCHED;
537 else
538 dentry->d_flags &= ~DCACHE_FSNOTIFY_PARENT_WATCHED;
539}
540
541
542
543
544extern struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops);
545extern struct fsnotify_group *fsnotify_alloc_user_group(const struct fsnotify_ops *ops);
546
547extern void fsnotify_get_group(struct fsnotify_group *group);
548
549extern void fsnotify_put_group(struct fsnotify_group *group);
550
551extern void fsnotify_group_stop_queueing(struct fsnotify_group *group);
552
553extern void fsnotify_destroy_group(struct fsnotify_group *group);
554
555extern int fsnotify_fasync(int fd, struct file *file, int on);
556
557extern void fsnotify_destroy_event(struct fsnotify_group *group,
558 struct fsnotify_event *event);
559
560extern int fsnotify_insert_event(struct fsnotify_group *group,
561 struct fsnotify_event *event,
562 int (*merge)(struct fsnotify_group *,
563 struct fsnotify_event *),
564 void (*insert)(struct fsnotify_group *,
565 struct fsnotify_event *));
566
567static inline int fsnotify_add_event(struct fsnotify_group *group,
568 struct fsnotify_event *event,
569 int (*merge)(struct fsnotify_group *,
570 struct fsnotify_event *))
571{
572 return fsnotify_insert_event(group, event, merge, NULL);
573}
574
575
576static inline void fsnotify_queue_overflow(struct fsnotify_group *group)
577{
578 fsnotify_add_event(group, group->overflow_event, NULL);
579}
580
581static inline bool fsnotify_is_overflow_event(u32 mask)
582{
583 return mask & FS_Q_OVERFLOW;
584}
585
586static inline bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group)
587{
588 assert_spin_locked(&group->notification_lock);
589
590 return list_empty(&group->notification_list);
591}
592
593extern bool fsnotify_notify_queue_is_empty(struct fsnotify_group *group);
594
595extern struct fsnotify_event *fsnotify_peek_first_event(struct fsnotify_group *group);
596
597extern struct fsnotify_event *fsnotify_remove_first_event(struct fsnotify_group *group);
598
599extern void fsnotify_remove_queued_event(struct fsnotify_group *group,
600 struct fsnotify_event *event);
601
602
603
604
605static inline __u32 fsnotify_calc_mask(struct fsnotify_mark *mark)
606{
607 __u32 mask = mark->mask;
608
609 if (!mark->ignored_mask)
610 return mask;
611
612
613 if (!(mark->flags & FSNOTIFY_MARK_FLAG_IGNORED_SURV_MODIFY))
614 mask |= FS_MODIFY;
615
616
617
618
619
620 return mask | (mark->ignored_mask & ALL_FSNOTIFY_EVENTS);
621}
622
623
624extern __u32 fsnotify_conn_mask(struct fsnotify_mark_connector *conn);
625
626extern void fsnotify_recalc_mask(struct fsnotify_mark_connector *conn);
627extern void fsnotify_init_mark(struct fsnotify_mark *mark,
628 struct fsnotify_group *group);
629
630extern struct fsnotify_mark *fsnotify_find_mark(fsnotify_connp_t *connp,
631 struct fsnotify_group *group);
632
633extern int fsnotify_get_conn_fsid(const struct fsnotify_mark_connector *conn,
634 __kernel_fsid_t *fsid);
635
636extern int fsnotify_add_mark(struct fsnotify_mark *mark,
637 fsnotify_connp_t *connp, unsigned int obj_type,
638 int allow_dups, __kernel_fsid_t *fsid);
639extern int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
640 fsnotify_connp_t *connp,
641 unsigned int obj_type, int allow_dups,
642 __kernel_fsid_t *fsid);
643
644
645static inline int fsnotify_add_inode_mark(struct fsnotify_mark *mark,
646 struct inode *inode,
647 int allow_dups)
648{
649 return fsnotify_add_mark(mark, &inode->i_fsnotify_marks,
650 FSNOTIFY_OBJ_TYPE_INODE, allow_dups, NULL);
651}
652static inline int fsnotify_add_inode_mark_locked(struct fsnotify_mark *mark,
653 struct inode *inode,
654 int allow_dups)
655{
656 return fsnotify_add_mark_locked(mark, &inode->i_fsnotify_marks,
657 FSNOTIFY_OBJ_TYPE_INODE, allow_dups,
658 NULL);
659}
660
661
662extern void fsnotify_destroy_mark(struct fsnotify_mark *mark,
663 struct fsnotify_group *group);
664
665extern void fsnotify_detach_mark(struct fsnotify_mark *mark);
666
667extern void fsnotify_free_mark(struct fsnotify_mark *mark);
668
669extern void fsnotify_wait_marks_destroyed(void);
670
671extern void fsnotify_clear_marks_by_group(struct fsnotify_group *group,
672 unsigned int obj_type);
673
674static inline void fsnotify_clear_vfsmount_marks_by_group(struct fsnotify_group *group)
675{
676 fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_VFSMOUNT);
677}
678
679static inline void fsnotify_clear_inode_marks_by_group(struct fsnotify_group *group)
680{
681 fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_INODE);
682}
683
684static inline void fsnotify_clear_sb_marks_by_group(struct fsnotify_group *group)
685{
686 fsnotify_clear_marks_by_group(group, FSNOTIFY_OBJ_TYPE_SB);
687}
688extern void fsnotify_get_mark(struct fsnotify_mark *mark);
689extern void fsnotify_put_mark(struct fsnotify_mark *mark);
690extern void fsnotify_finish_user_wait(struct fsnotify_iter_info *iter_info);
691extern bool fsnotify_prepare_user_wait(struct fsnotify_iter_info *iter_info);
692
693static inline void fsnotify_init_event(struct fsnotify_event *event)
694{
695 INIT_LIST_HEAD(&event->list);
696}
697
698#else
699
700static inline int fsnotify(__u32 mask, const void *data, int data_type,
701 struct inode *dir, const struct qstr *name,
702 struct inode *inode, u32 cookie)
703{
704 return 0;
705}
706
707static inline int __fsnotify_parent(struct dentry *dentry, __u32 mask,
708 const void *data, int data_type)
709{
710 return 0;
711}
712
713static inline void __fsnotify_inode_delete(struct inode *inode)
714{}
715
716static inline void __fsnotify_vfsmount_delete(struct vfsmount *mnt)
717{}
718
719static inline void fsnotify_sb_delete(struct super_block *sb)
720{}
721
722static inline void fsnotify_update_flags(struct dentry *dentry)
723{}
724
725static inline u32 fsnotify_get_cookie(void)
726{
727 return 0;
728}
729
730static inline void fsnotify_unmount_inodes(struct super_block *sb)
731{}
732
733#endif
734
735#endif
736
737#endif
738