xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/utils/inotify.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Inode based directory notification for Linux
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2005 John McCutchan
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker #ifndef _LINUX_INOTIFY_H
8*49cdfc7eSAndroid Build Coastguard Worker #define _LINUX_INOTIFY_H
9*49cdfc7eSAndroid Build Coastguard Worker 
10*49cdfc7eSAndroid Build Coastguard Worker #include <linux/types.h>
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker /*
13*49cdfc7eSAndroid Build Coastguard Worker  * struct inotify_event - structure read from the inotify device for each event
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * When you are watching a directory, you will receive the filename for events
16*49cdfc7eSAndroid Build Coastguard Worker  * such as IN_CREATE, IN_DELETE, IN_OPEN, IN_CLOSE, ..., relative to the wd.
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker struct inotify_event {
19*49cdfc7eSAndroid Build Coastguard Worker 	__s32		wd;		/* watch descriptor */
20*49cdfc7eSAndroid Build Coastguard Worker 	__u32		mask;		/* watch mask */
21*49cdfc7eSAndroid Build Coastguard Worker 	__u32		cookie;		/* cookie to synchronize two events */
22*49cdfc7eSAndroid Build Coastguard Worker 	__u32		len;		/* length (including nulls) of name */
23*49cdfc7eSAndroid Build Coastguard Worker 	char		name[0];	/* stub for possible name */
24*49cdfc7eSAndroid Build Coastguard Worker };
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker /* the following are legal, implemented events that user-space can watch for */
27*49cdfc7eSAndroid Build Coastguard Worker #define IN_ACCESS		0x00000001	/* File was accessed */
28*49cdfc7eSAndroid Build Coastguard Worker #define IN_MODIFY		0x00000002	/* File was modified */
29*49cdfc7eSAndroid Build Coastguard Worker #define IN_ATTRIB		0x00000004	/* Metadata changed */
30*49cdfc7eSAndroid Build Coastguard Worker #define IN_CLOSE_WRITE		0x00000008	/* Writtable file was closed */
31*49cdfc7eSAndroid Build Coastguard Worker #define IN_CLOSE_NOWRITE	0x00000010	/* Unwrittable file closed */
32*49cdfc7eSAndroid Build Coastguard Worker #define IN_OPEN			0x00000020	/* File was opened */
33*49cdfc7eSAndroid Build Coastguard Worker #define IN_MOVED_FROM		0x00000040	/* File was moved from X */
34*49cdfc7eSAndroid Build Coastguard Worker #define IN_MOVED_TO		0x00000080	/* File was moved to Y */
35*49cdfc7eSAndroid Build Coastguard Worker #define IN_CREATE		0x00000100	/* Subfile was created */
36*49cdfc7eSAndroid Build Coastguard Worker #define IN_DELETE		0x00000200	/* Subfile was deleted */
37*49cdfc7eSAndroid Build Coastguard Worker #define IN_DELETE_SELF		0x00000400	/* Self was deleted */
38*49cdfc7eSAndroid Build Coastguard Worker #define IN_MOVE_SELF		0x00000800	/* Self was moved */
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker /* the following are legal events.  they are sent as needed to any watch */
41*49cdfc7eSAndroid Build Coastguard Worker #define IN_UNMOUNT		0x00002000	/* Backing fs was unmounted */
42*49cdfc7eSAndroid Build Coastguard Worker #define IN_Q_OVERFLOW		0x00004000	/* Event queued overflowed */
43*49cdfc7eSAndroid Build Coastguard Worker #define IN_IGNORED		0x00008000	/* File was ignored */
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker /* helper events */
46*49cdfc7eSAndroid Build Coastguard Worker #define IN_CLOSE		(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) /* close */
47*49cdfc7eSAndroid Build Coastguard Worker #define IN_MOVE			(IN_MOVED_FROM | IN_MOVED_TO) /* moves */
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker /* special flags */
50*49cdfc7eSAndroid Build Coastguard Worker #define IN_ONLYDIR		0x01000000	/* only watch the path if it is a directory */
51*49cdfc7eSAndroid Build Coastguard Worker #define IN_DONT_FOLLOW		0x02000000	/* don't follow a sym link */
52*49cdfc7eSAndroid Build Coastguard Worker #define IN_MASK_ADD		0x20000000	/* add to the mask of an already existing watch */
53*49cdfc7eSAndroid Build Coastguard Worker #define IN_ISDIR		0x40000000	/* event occurred against dir */
54*49cdfc7eSAndroid Build Coastguard Worker #define IN_ONESHOT		0x80000000	/* only send event once */
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker /*
57*49cdfc7eSAndroid Build Coastguard Worker  * All of the events - we build the list by hand so that we can add flags in
58*49cdfc7eSAndroid Build Coastguard Worker  * the future and not break backward compatibility.  Apps will get only the
59*49cdfc7eSAndroid Build Coastguard Worker  * events that they originally wanted.  Be sure to add new events here!
60*49cdfc7eSAndroid Build Coastguard Worker  */
61*49cdfc7eSAndroid Build Coastguard Worker #define IN_ALL_EVENTS	(IN_ACCESS | IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE | \
62*49cdfc7eSAndroid Build Coastguard Worker 			 IN_CLOSE_NOWRITE | IN_OPEN | IN_MOVED_FROM | \
63*49cdfc7eSAndroid Build Coastguard Worker 			 IN_MOVED_TO | IN_DELETE | IN_CREATE | IN_DELETE_SELF | \
64*49cdfc7eSAndroid Build Coastguard Worker 			 IN_MOVE_SELF)
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker #ifdef __KERNEL__
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker #include <linux/dcache.h>
69*49cdfc7eSAndroid Build Coastguard Worker #include <linux/fs.h>
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker /*
72*49cdfc7eSAndroid Build Coastguard Worker  * struct inotify_watch - represents a watch request on a specific inode
73*49cdfc7eSAndroid Build Coastguard Worker  *
74*49cdfc7eSAndroid Build Coastguard Worker  * h_list is protected by ih->mutex of the associated inotify_handle.
75*49cdfc7eSAndroid Build Coastguard Worker  * i_list, mask are protected by inode->inotify_mutex of the associated inode.
76*49cdfc7eSAndroid Build Coastguard Worker  * ih, inode, and wd are never written to once the watch is created.
77*49cdfc7eSAndroid Build Coastguard Worker  *
78*49cdfc7eSAndroid Build Coastguard Worker  * Callers must use the established inotify interfaces to access inotify_watch
79*49cdfc7eSAndroid Build Coastguard Worker  * contents.  The content of this structure is private to the inotify
80*49cdfc7eSAndroid Build Coastguard Worker  * implementation.
81*49cdfc7eSAndroid Build Coastguard Worker  */
82*49cdfc7eSAndroid Build Coastguard Worker struct inotify_watch {
83*49cdfc7eSAndroid Build Coastguard Worker 	struct list_head	h_list;	/* entry in inotify_handle's list */
84*49cdfc7eSAndroid Build Coastguard Worker 	struct list_head	i_list;	/* entry in inode's list */
85*49cdfc7eSAndroid Build Coastguard Worker 	atomic_t		count;	/* reference count */
86*49cdfc7eSAndroid Build Coastguard Worker 	struct inotify_handle	*ih;	/* associated inotify handle */
87*49cdfc7eSAndroid Build Coastguard Worker 	struct inode		*inode;	/* associated inode */
88*49cdfc7eSAndroid Build Coastguard Worker 	__s32			wd;	/* watch descriptor */
89*49cdfc7eSAndroid Build Coastguard Worker 	__u32			mask;	/* event mask for this watch */
90*49cdfc7eSAndroid Build Coastguard Worker };
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker struct inotify_operations {
93*49cdfc7eSAndroid Build Coastguard Worker 	void (*handle_event)(struct inotify_watch *, u32, u32, u32,
94*49cdfc7eSAndroid Build Coastguard Worker 			     const char *, struct inode *);
95*49cdfc7eSAndroid Build Coastguard Worker 	void (*destroy_watch)(struct inotify_watch *);
96*49cdfc7eSAndroid Build Coastguard Worker };
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker #ifdef CONFIG_INOTIFY
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker /* Kernel API for producing events */
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker extern void inotify_d_instantiate(struct dentry *, struct inode *);
103*49cdfc7eSAndroid Build Coastguard Worker extern void inotify_d_move(struct dentry *);
104*49cdfc7eSAndroid Build Coastguard Worker extern void inotify_inode_queue_event(struct inode *, __u32, __u32,
105*49cdfc7eSAndroid Build Coastguard Worker 				      const char *, struct inode *);
106*49cdfc7eSAndroid Build Coastguard Worker extern void inotify_dentry_parent_queue_event(struct dentry *, __u32, __u32,
107*49cdfc7eSAndroid Build Coastguard Worker 					      const char *);
108*49cdfc7eSAndroid Build Coastguard Worker extern void inotify_unmount_inodes(struct list_head *);
109*49cdfc7eSAndroid Build Coastguard Worker extern void inotify_inode_is_dead(struct inode *);
110*49cdfc7eSAndroid Build Coastguard Worker extern u32 inotify_get_cookie(void);
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker /* Kernel Consumer API */
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker extern struct inotify_handle *inotify_init(const struct inotify_operations *);
115*49cdfc7eSAndroid Build Coastguard Worker extern void inotify_init_watch(struct inotify_watch *);
116*49cdfc7eSAndroid Build Coastguard Worker extern void inotify_destroy(struct inotify_handle *);
117*49cdfc7eSAndroid Build Coastguard Worker extern __s32 inotify_find_watch(struct inotify_handle *, struct inode *,
118*49cdfc7eSAndroid Build Coastguard Worker 				struct inotify_watch **);
119*49cdfc7eSAndroid Build Coastguard Worker extern __s32 inotify_find_update_watch(struct inotify_handle *, struct inode *,
120*49cdfc7eSAndroid Build Coastguard Worker 				       u32);
121*49cdfc7eSAndroid Build Coastguard Worker extern __s32 inotify_add_watch(struct inotify_handle *, struct inotify_watch *,
122*49cdfc7eSAndroid Build Coastguard Worker 			       struct inode *, __u32);
123*49cdfc7eSAndroid Build Coastguard Worker extern int inotify_rm_watch(struct inotify_handle *, struct inotify_watch *);
124*49cdfc7eSAndroid Build Coastguard Worker extern int inotify_rm_wd(struct inotify_handle *, __u32);
125*49cdfc7eSAndroid Build Coastguard Worker extern void inotify_remove_watch_locked(struct inotify_handle *,
126*49cdfc7eSAndroid Build Coastguard Worker 					struct inotify_watch *);
127*49cdfc7eSAndroid Build Coastguard Worker extern void get_inotify_watch(struct inotify_watch *);
128*49cdfc7eSAndroid Build Coastguard Worker extern void put_inotify_watch(struct inotify_watch *);
129*49cdfc7eSAndroid Build Coastguard Worker 
130*49cdfc7eSAndroid Build Coastguard Worker #else
131*49cdfc7eSAndroid Build Coastguard Worker 
inotify_d_instantiate(struct dentry * dentry,struct inode * inode)132*49cdfc7eSAndroid Build Coastguard Worker static inline void inotify_d_instantiate(struct dentry *dentry,
133*49cdfc7eSAndroid Build Coastguard Worker 					struct inode *inode)
134*49cdfc7eSAndroid Build Coastguard Worker {
135*49cdfc7eSAndroid Build Coastguard Worker }
136*49cdfc7eSAndroid Build Coastguard Worker 
inotify_d_move(struct dentry * dentry)137*49cdfc7eSAndroid Build Coastguard Worker static inline void inotify_d_move(struct dentry *dentry)
138*49cdfc7eSAndroid Build Coastguard Worker {
139*49cdfc7eSAndroid Build Coastguard Worker }
140*49cdfc7eSAndroid Build Coastguard Worker 
inotify_inode_queue_event(struct inode * inode,__u32 mask,__u32 cookie,const char * filename,struct inode * n_inode)141*49cdfc7eSAndroid Build Coastguard Worker static inline void inotify_inode_queue_event(struct inode *inode,
142*49cdfc7eSAndroid Build Coastguard Worker 					     __u32 mask, __u32 cookie,
143*49cdfc7eSAndroid Build Coastguard Worker 					     const char *filename,
144*49cdfc7eSAndroid Build Coastguard Worker 					     struct inode *n_inode)
145*49cdfc7eSAndroid Build Coastguard Worker {
146*49cdfc7eSAndroid Build Coastguard Worker }
147*49cdfc7eSAndroid Build Coastguard Worker 
inotify_dentry_parent_queue_event(struct dentry * dentry,__u32 mask,__u32 cookie,const char * filename)148*49cdfc7eSAndroid Build Coastguard Worker static inline void inotify_dentry_parent_queue_event(struct dentry *dentry,
149*49cdfc7eSAndroid Build Coastguard Worker 						     __u32 mask, __u32 cookie,
150*49cdfc7eSAndroid Build Coastguard Worker 						     const char *filename)
151*49cdfc7eSAndroid Build Coastguard Worker {
152*49cdfc7eSAndroid Build Coastguard Worker }
153*49cdfc7eSAndroid Build Coastguard Worker 
inotify_unmount_inodes(struct list_head * list)154*49cdfc7eSAndroid Build Coastguard Worker static inline void inotify_unmount_inodes(struct list_head *list)
155*49cdfc7eSAndroid Build Coastguard Worker {
156*49cdfc7eSAndroid Build Coastguard Worker }
157*49cdfc7eSAndroid Build Coastguard Worker 
inotify_inode_is_dead(struct inode * inode)158*49cdfc7eSAndroid Build Coastguard Worker static inline void inotify_inode_is_dead(struct inode *inode)
159*49cdfc7eSAndroid Build Coastguard Worker {
160*49cdfc7eSAndroid Build Coastguard Worker }
161*49cdfc7eSAndroid Build Coastguard Worker 
inotify_get_cookie(void)162*49cdfc7eSAndroid Build Coastguard Worker static inline u32 inotify_get_cookie(void)
163*49cdfc7eSAndroid Build Coastguard Worker {
164*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
165*49cdfc7eSAndroid Build Coastguard Worker }
166*49cdfc7eSAndroid Build Coastguard Worker 
inotify_init(const struct inotify_operations * ops)167*49cdfc7eSAndroid Build Coastguard Worker static inline struct inotify_handle *inotify_init(const struct inotify_operations *ops)
168*49cdfc7eSAndroid Build Coastguard Worker {
169*49cdfc7eSAndroid Build Coastguard Worker 	return ERR_PTR(-EOPNOTSUPP);
170*49cdfc7eSAndroid Build Coastguard Worker }
171*49cdfc7eSAndroid Build Coastguard Worker 
inotify_init_watch(struct inotify_watch * watch)172*49cdfc7eSAndroid Build Coastguard Worker static inline void inotify_init_watch(struct inotify_watch *watch)
173*49cdfc7eSAndroid Build Coastguard Worker {
174*49cdfc7eSAndroid Build Coastguard Worker }
175*49cdfc7eSAndroid Build Coastguard Worker 
inotify_destroy(struct inotify_handle * ih)176*49cdfc7eSAndroid Build Coastguard Worker static inline void inotify_destroy(struct inotify_handle *ih)
177*49cdfc7eSAndroid Build Coastguard Worker {
178*49cdfc7eSAndroid Build Coastguard Worker }
179*49cdfc7eSAndroid Build Coastguard Worker 
inotify_find_watch(struct inotify_handle * ih,struct inode * inode,struct inotify_watch ** watchp)180*49cdfc7eSAndroid Build Coastguard Worker static inline __s32 inotify_find_watch(struct inotify_handle *ih, struct inode *inode,
181*49cdfc7eSAndroid Build Coastguard Worker 				       struct inotify_watch **watchp)
182*49cdfc7eSAndroid Build Coastguard Worker {
183*49cdfc7eSAndroid Build Coastguard Worker 	return -EOPNOTSUPP;
184*49cdfc7eSAndroid Build Coastguard Worker }
185*49cdfc7eSAndroid Build Coastguard Worker 
inotify_find_update_watch(struct inotify_handle * ih,struct inode * inode,u32 mask)186*49cdfc7eSAndroid Build Coastguard Worker static inline __s32 inotify_find_update_watch(struct inotify_handle *ih,
187*49cdfc7eSAndroid Build Coastguard Worker 					      struct inode *inode, u32 mask)
188*49cdfc7eSAndroid Build Coastguard Worker {
189*49cdfc7eSAndroid Build Coastguard Worker 	return -EOPNOTSUPP;
190*49cdfc7eSAndroid Build Coastguard Worker }
191*49cdfc7eSAndroid Build Coastguard Worker 
inotify_add_watch(struct inotify_handle * ih,struct inotify_watch * watch,struct inode * inode,__u32 mask)192*49cdfc7eSAndroid Build Coastguard Worker static inline __s32 inotify_add_watch(struct inotify_handle *ih,
193*49cdfc7eSAndroid Build Coastguard Worker 				      struct inotify_watch *watch,
194*49cdfc7eSAndroid Build Coastguard Worker 				      struct inode *inode, __u32 mask)
195*49cdfc7eSAndroid Build Coastguard Worker {
196*49cdfc7eSAndroid Build Coastguard Worker 	return -EOPNOTSUPP;
197*49cdfc7eSAndroid Build Coastguard Worker }
198*49cdfc7eSAndroid Build Coastguard Worker 
inotify_rm_watch(struct inotify_handle * ih,struct inotify_watch * watch)199*49cdfc7eSAndroid Build Coastguard Worker static inline int inotify_rm_watch(struct inotify_handle *ih,
200*49cdfc7eSAndroid Build Coastguard Worker 				   struct inotify_watch *watch)
201*49cdfc7eSAndroid Build Coastguard Worker {
202*49cdfc7eSAndroid Build Coastguard Worker 	return -EOPNOTSUPP;
203*49cdfc7eSAndroid Build Coastguard Worker }
204*49cdfc7eSAndroid Build Coastguard Worker 
inotify_rm_wd(struct inotify_handle * ih,__u32 wd)205*49cdfc7eSAndroid Build Coastguard Worker static inline int inotify_rm_wd(struct inotify_handle *ih, __u32 wd)
206*49cdfc7eSAndroid Build Coastguard Worker {
207*49cdfc7eSAndroid Build Coastguard Worker 	return -EOPNOTSUPP;
208*49cdfc7eSAndroid Build Coastguard Worker }
209*49cdfc7eSAndroid Build Coastguard Worker 
inotify_remove_watch_locked(struct inotify_handle * ih,struct inotify_watch * watch)210*49cdfc7eSAndroid Build Coastguard Worker static inline void inotify_remove_watch_locked(struct inotify_handle *ih,
211*49cdfc7eSAndroid Build Coastguard Worker 					       struct inotify_watch *watch)
212*49cdfc7eSAndroid Build Coastguard Worker {
213*49cdfc7eSAndroid Build Coastguard Worker }
214*49cdfc7eSAndroid Build Coastguard Worker 
get_inotify_watch(struct inotify_watch * watch)215*49cdfc7eSAndroid Build Coastguard Worker static inline void get_inotify_watch(struct inotify_watch *watch)
216*49cdfc7eSAndroid Build Coastguard Worker {
217*49cdfc7eSAndroid Build Coastguard Worker }
218*49cdfc7eSAndroid Build Coastguard Worker 
put_inotify_watch(struct inotify_watch * watch)219*49cdfc7eSAndroid Build Coastguard Worker static inline void put_inotify_watch(struct inotify_watch *watch)
220*49cdfc7eSAndroid Build Coastguard Worker {
221*49cdfc7eSAndroid Build Coastguard Worker }
222*49cdfc7eSAndroid Build Coastguard Worker 
223*49cdfc7eSAndroid Build Coastguard Worker #endif	/* CONFIG_INOTIFY */
224*49cdfc7eSAndroid Build Coastguard Worker 
225*49cdfc7eSAndroid Build Coastguard Worker #endif	/* __KERNEL __ */
226*49cdfc7eSAndroid Build Coastguard Worker 
227*49cdfc7eSAndroid Build Coastguard Worker #endif	/* _LINUX_INOTIFY_H */
228