xref: /linux-6.15/include/linux/fsnotify.h (revision 20dd026d)
1 #ifndef _LINUX_FS_NOTIFY_H
2 #define _LINUX_FS_NOTIFY_H
3 
4 /*
5  * include/linux/fsnotify.h - generic hooks for filesystem notification, to
6  * reduce in-source duplication from both dnotify and inotify.
7  *
8  * We don't compile any of this away in some complicated menagerie of ifdefs.
9  * Instead, we rely on the code inside to optimize away as needed.
10  *
11  * (C) Copyright 2005 Robert Love
12  */
13 
14 #ifdef __KERNEL__
15 
16 #include <linux/dnotify.h>
17 #include <linux/inotify.h>
18 
19 /*
20  * fsnotify_move - file old_name at old_dir was moved to new_name at new_dir
21  */
22 static inline void fsnotify_move(struct inode *old_dir, struct inode *new_dir,
23 				 const char *old_name, const char *new_name,
24 				 int isdir, struct inode *target, struct inode *source)
25 {
26 	u32 cookie = inotify_get_cookie();
27 
28 	if (old_dir == new_dir)
29 		inode_dir_notify(old_dir, DN_RENAME);
30 	else {
31 		inode_dir_notify(old_dir, DN_DELETE);
32 		inode_dir_notify(new_dir, DN_CREATE);
33 	}
34 
35 	if (isdir)
36 		isdir = IN_ISDIR;
37 	inotify_inode_queue_event(old_dir, IN_MOVED_FROM|isdir,cookie,old_name);
38 	inotify_inode_queue_event(new_dir, IN_MOVED_TO|isdir, cookie, new_name);
39 
40 	if (target) {
41 		inotify_inode_queue_event(target, IN_DELETE_SELF, 0, NULL);
42 		inotify_inode_is_dead(target);
43 	}
44 
45 	if (source) {
46 		inotify_inode_queue_event(source, IN_MOVE_SELF, 0, NULL);
47 	}
48 }
49 
50 /*
51  * fsnotify_nameremove - a filename was removed from a directory
52  */
53 static inline void fsnotify_nameremove(struct dentry *dentry, int isdir)
54 {
55 	if (isdir)
56 		isdir = IN_ISDIR;
57 	dnotify_parent(dentry, DN_DELETE);
58 	inotify_dentry_parent_queue_event(dentry, IN_DELETE|isdir, 0, dentry->d_name.name);
59 }
60 
61 /*
62  * fsnotify_inoderemove - an inode is going away
63  */
64 static inline void fsnotify_inoderemove(struct inode *inode)
65 {
66 	inotify_inode_queue_event(inode, IN_DELETE_SELF, 0, NULL);
67 	inotify_inode_is_dead(inode);
68 }
69 
70 /*
71  * fsnotify_create - 'name' was linked in
72  */
73 static inline void fsnotify_create(struct inode *inode, const char *name)
74 {
75 	inode_dir_notify(inode, DN_CREATE);
76 	inotify_inode_queue_event(inode, IN_CREATE, 0, name);
77 }
78 
79 /*
80  * fsnotify_mkdir - directory 'name' was created
81  */
82 static inline void fsnotify_mkdir(struct inode *inode, const char *name)
83 {
84 	inode_dir_notify(inode, DN_CREATE);
85 	inotify_inode_queue_event(inode, IN_CREATE | IN_ISDIR, 0, name);
86 }
87 
88 /*
89  * fsnotify_access - file was read
90  */
91 static inline void fsnotify_access(struct dentry *dentry)
92 {
93 	struct inode *inode = dentry->d_inode;
94 	u32 mask = IN_ACCESS;
95 
96 	if (S_ISDIR(inode->i_mode))
97 		mask |= IN_ISDIR;
98 
99 	dnotify_parent(dentry, DN_ACCESS);
100 	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
101 	inotify_inode_queue_event(inode, mask, 0, NULL);
102 }
103 
104 /*
105  * fsnotify_modify - file was modified
106  */
107 static inline void fsnotify_modify(struct dentry *dentry)
108 {
109 	struct inode *inode = dentry->d_inode;
110 	u32 mask = IN_MODIFY;
111 
112 	if (S_ISDIR(inode->i_mode))
113 		mask |= IN_ISDIR;
114 
115 	dnotify_parent(dentry, DN_MODIFY);
116 	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
117 	inotify_inode_queue_event(inode, mask, 0, NULL);
118 }
119 
120 /*
121  * fsnotify_open - file was opened
122  */
123 static inline void fsnotify_open(struct dentry *dentry)
124 {
125 	struct inode *inode = dentry->d_inode;
126 	u32 mask = IN_OPEN;
127 
128 	if (S_ISDIR(inode->i_mode))
129 		mask |= IN_ISDIR;
130 
131 	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
132 	inotify_inode_queue_event(inode, mask, 0, NULL);
133 }
134 
135 /*
136  * fsnotify_close - file was closed
137  */
138 static inline void fsnotify_close(struct file *file)
139 {
140 	struct dentry *dentry = file->f_dentry;
141 	struct inode *inode = dentry->d_inode;
142 	const char *name = dentry->d_name.name;
143 	mode_t mode = file->f_mode;
144 	u32 mask = (mode & FMODE_WRITE) ? IN_CLOSE_WRITE : IN_CLOSE_NOWRITE;
145 
146 	if (S_ISDIR(inode->i_mode))
147 		mask |= IN_ISDIR;
148 
149 	inotify_dentry_parent_queue_event(dentry, mask, 0, name);
150 	inotify_inode_queue_event(inode, mask, 0, NULL);
151 }
152 
153 /*
154  * fsnotify_xattr - extended attributes were changed
155  */
156 static inline void fsnotify_xattr(struct dentry *dentry)
157 {
158 	struct inode *inode = dentry->d_inode;
159 	u32 mask = IN_ATTRIB;
160 
161 	if (S_ISDIR(inode->i_mode))
162 		mask |= IN_ISDIR;
163 
164 	inotify_dentry_parent_queue_event(dentry, mask, 0, dentry->d_name.name);
165 	inotify_inode_queue_event(inode, mask, 0, NULL);
166 }
167 
168 /*
169  * fsnotify_change - notify_change event.  file was modified and/or metadata
170  * was changed.
171  */
172 static inline void fsnotify_change(struct dentry *dentry, unsigned int ia_valid)
173 {
174 	struct inode *inode = dentry->d_inode;
175 	int dn_mask = 0;
176 	u32 in_mask = 0;
177 
178 	if (ia_valid & ATTR_UID) {
179 		in_mask |= IN_ATTRIB;
180 		dn_mask |= DN_ATTRIB;
181 	}
182 	if (ia_valid & ATTR_GID) {
183 		in_mask |= IN_ATTRIB;
184 		dn_mask |= DN_ATTRIB;
185 	}
186 	if (ia_valid & ATTR_SIZE) {
187 		in_mask |= IN_MODIFY;
188 		dn_mask |= DN_MODIFY;
189 	}
190 	/* both times implies a utime(s) call */
191 	if ((ia_valid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME))
192 	{
193 		in_mask |= IN_ATTRIB;
194 		dn_mask |= DN_ATTRIB;
195 	} else if (ia_valid & ATTR_ATIME) {
196 		in_mask |= IN_ACCESS;
197 		dn_mask |= DN_ACCESS;
198 	} else if (ia_valid & ATTR_MTIME) {
199 		in_mask |= IN_MODIFY;
200 		dn_mask |= DN_MODIFY;
201 	}
202 	if (ia_valid & ATTR_MODE) {
203 		in_mask |= IN_ATTRIB;
204 		dn_mask |= DN_ATTRIB;
205 	}
206 
207 	if (dn_mask)
208 		dnotify_parent(dentry, dn_mask);
209 	if (in_mask) {
210 		if (S_ISDIR(inode->i_mode))
211 			in_mask |= IN_ISDIR;
212 		inotify_inode_queue_event(inode, in_mask, 0, NULL);
213 		inotify_dentry_parent_queue_event(dentry, in_mask, 0,
214 						  dentry->d_name.name);
215 	}
216 }
217 
218 #ifdef CONFIG_INOTIFY	/* inotify helpers */
219 
220 /*
221  * fsnotify_oldname_init - save off the old filename before we change it
222  */
223 static inline const char *fsnotify_oldname_init(const char *name)
224 {
225 	return kstrdup(name, GFP_KERNEL);
226 }
227 
228 /*
229  * fsnotify_oldname_free - free the name we got from fsnotify_oldname_init
230  */
231 static inline void fsnotify_oldname_free(const char *old_name)
232 {
233 	kfree(old_name);
234 }
235 
236 #else	/* CONFIG_INOTIFY */
237 
238 static inline const char *fsnotify_oldname_init(const char *name)
239 {
240 	return NULL;
241 }
242 
243 static inline void fsnotify_oldname_free(const char *old_name)
244 {
245 }
246 
247 #endif	/* ! CONFIG_INOTIFY */
248 
249 #endif	/* __KERNEL__ */
250 
251 #endif	/* _LINUX_FS_NOTIFY_H */
252