9871f76cd9c2cd78a6ba6ad49b829913d3c1e372
[linux-2.6-microblaze.git] / fs / notify / fanotify / fanotify.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <linux/fsnotify_backend.h>
3 #include <linux/path.h>
4 #include <linux/slab.h>
5 #include <linux/exportfs.h>
6
7 extern struct kmem_cache *fanotify_mark_cache;
8 extern struct kmem_cache *fanotify_fid_event_cachep;
9 extern struct kmem_cache *fanotify_path_event_cachep;
10 extern struct kmem_cache *fanotify_perm_event_cachep;
11
12 /* Possible states of the permission event */
13 enum {
14         FAN_EVENT_INIT,
15         FAN_EVENT_REPORTED,
16         FAN_EVENT_ANSWERED,
17         FAN_EVENT_CANCELED,
18 };
19
20 /*
21  * 3 dwords are sufficient for most local fs (64bit ino, 32bit generation).
22  * fh buf should be dword aligned. On 64bit arch, the ext_buf pointer is
23  * stored in either the first or last 2 dwords.
24  */
25 #define FANOTIFY_INLINE_FH_LEN  (3 << 2)
26 #define FANOTIFY_FH_HDR_LEN     offsetof(struct fanotify_fh, buf)
27
28 /* Fixed size struct for file handle */
29 struct fanotify_fh {
30         u8 type;
31         u8 len;
32 #define FANOTIFY_FH_FLAG_EXT_BUF 1
33         u8 flags;
34         u8 pad;
35         unsigned char buf[];
36 } __aligned(4);
37
38 /* Variable size struct for dir file handle + child file handle + name */
39 struct fanotify_info {
40         /* size of dir_fh/file_fh including fanotify_fh hdr size */
41         u8 dir_fh_totlen;
42         u8 file_fh_totlen;
43         u8 name_len;
44         u8 pad;
45         unsigned char buf[];
46         /*
47          * (struct fanotify_fh) dir_fh starts at buf[0]
48          * (optional) file_fh starts at buf[dir_fh_totlen]
49          * name starts at buf[dir_fh_totlen + file_fh_totlen]
50          */
51 } __aligned(4);
52
53 static inline bool fanotify_fh_has_ext_buf(struct fanotify_fh *fh)
54 {
55         return (fh->flags & FANOTIFY_FH_FLAG_EXT_BUF);
56 }
57
58 static inline char **fanotify_fh_ext_buf_ptr(struct fanotify_fh *fh)
59 {
60         BUILD_BUG_ON(FANOTIFY_FH_HDR_LEN % 4);
61         BUILD_BUG_ON(__alignof__(char *) - 4 + sizeof(char *) >
62                      FANOTIFY_INLINE_FH_LEN);
63         return (char **)ALIGN((unsigned long)(fh->buf), __alignof__(char *));
64 }
65
66 static inline void *fanotify_fh_ext_buf(struct fanotify_fh *fh)
67 {
68         return *fanotify_fh_ext_buf_ptr(fh);
69 }
70
71 static inline void *fanotify_fh_buf(struct fanotify_fh *fh)
72 {
73         return fanotify_fh_has_ext_buf(fh) ? fanotify_fh_ext_buf(fh) : fh->buf;
74 }
75
76 static inline int fanotify_info_dir_fh_len(struct fanotify_info *info)
77 {
78         if (!info->dir_fh_totlen ||
79             WARN_ON_ONCE(info->dir_fh_totlen < FANOTIFY_FH_HDR_LEN))
80                 return 0;
81
82         return info->dir_fh_totlen - FANOTIFY_FH_HDR_LEN;
83 }
84
85 static inline struct fanotify_fh *fanotify_info_dir_fh(struct fanotify_info *info)
86 {
87         BUILD_BUG_ON(offsetof(struct fanotify_info, buf) % 4);
88
89         return (struct fanotify_fh *)info->buf;
90 }
91
92 static inline int fanotify_info_file_fh_len(struct fanotify_info *info)
93 {
94         if (!info->file_fh_totlen ||
95             WARN_ON_ONCE(info->file_fh_totlen < FANOTIFY_FH_HDR_LEN))
96                 return 0;
97
98         return info->file_fh_totlen - FANOTIFY_FH_HDR_LEN;
99 }
100
101 static inline struct fanotify_fh *fanotify_info_file_fh(struct fanotify_info *info)
102 {
103         return (struct fanotify_fh *)(info->buf + info->dir_fh_totlen);
104 }
105
106 static inline const char *fanotify_info_name(struct fanotify_info *info)
107 {
108         return info->buf + info->dir_fh_totlen + info->file_fh_totlen;
109 }
110
111 static inline void fanotify_info_init(struct fanotify_info *info)
112 {
113         info->dir_fh_totlen = 0;
114         info->file_fh_totlen = 0;
115         info->name_len = 0;
116 }
117
118 static inline unsigned int fanotify_info_len(struct fanotify_info *info)
119 {
120         return info->dir_fh_totlen + info->file_fh_totlen + info->name_len;
121 }
122
123 static inline void fanotify_info_copy_name(struct fanotify_info *info,
124                                            const struct qstr *name)
125 {
126         info->name_len = name->len;
127         strcpy(info->buf + info->dir_fh_totlen + info->file_fh_totlen,
128                name->name);
129 }
130
131 /*
132  * Common structure for fanotify events. Concrete structs are allocated in
133  * fanotify_handle_event() and freed when the information is retrieved by
134  * userspace. The type of event determines how it was allocated, how it will
135  * be freed and which concrete struct it may be cast to.
136  */
137 enum fanotify_event_type {
138         FANOTIFY_EVENT_TYPE_FID, /* fixed length */
139         FANOTIFY_EVENT_TYPE_FID_NAME, /* variable length */
140         FANOTIFY_EVENT_TYPE_PATH,
141         FANOTIFY_EVENT_TYPE_PATH_PERM,
142         FANOTIFY_EVENT_TYPE_OVERFLOW, /* struct fanotify_event */
143         __FANOTIFY_EVENT_TYPE_NUM
144 };
145
146 #define FANOTIFY_EVENT_TYPE_BITS \
147         (ilog2(__FANOTIFY_EVENT_TYPE_NUM - 1) + 1)
148 #define FANOTIFY_EVENT_HASH_BITS \
149         (32 - FANOTIFY_EVENT_TYPE_BITS)
150
151 struct fanotify_event {
152         struct fsnotify_event fse;
153         u32 mask;
154         struct {
155                 unsigned int type : FANOTIFY_EVENT_TYPE_BITS;
156                 unsigned int hash : FANOTIFY_EVENT_HASH_BITS;
157         };
158         struct pid *pid;
159 };
160
161 static inline void fanotify_init_event(struct fanotify_event *event,
162                                        unsigned int hash, u32 mask)
163 {
164         fsnotify_init_event(&event->fse);
165         event->hash = hash;
166         event->mask = mask;
167         event->pid = NULL;
168 }
169
170 struct fanotify_fid_event {
171         struct fanotify_event fae;
172         __kernel_fsid_t fsid;
173         struct fanotify_fh object_fh;
174         /* Reserve space in object_fh.buf[] - access with fanotify_fh_buf() */
175         unsigned char _inline_fh_buf[FANOTIFY_INLINE_FH_LEN];
176 };
177
178 static inline struct fanotify_fid_event *
179 FANOTIFY_FE(struct fanotify_event *event)
180 {
181         return container_of(event, struct fanotify_fid_event, fae);
182 }
183
184 struct fanotify_name_event {
185         struct fanotify_event fae;
186         __kernel_fsid_t fsid;
187         struct fanotify_info info;
188 };
189
190 static inline struct fanotify_name_event *
191 FANOTIFY_NE(struct fanotify_event *event)
192 {
193         return container_of(event, struct fanotify_name_event, fae);
194 }
195
196 static inline __kernel_fsid_t *fanotify_event_fsid(struct fanotify_event *event)
197 {
198         if (event->type == FANOTIFY_EVENT_TYPE_FID)
199                 return &FANOTIFY_FE(event)->fsid;
200         else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
201                 return &FANOTIFY_NE(event)->fsid;
202         else
203                 return NULL;
204 }
205
206 static inline struct fanotify_fh *fanotify_event_object_fh(
207                                                 struct fanotify_event *event)
208 {
209         if (event->type == FANOTIFY_EVENT_TYPE_FID)
210                 return &FANOTIFY_FE(event)->object_fh;
211         else if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
212                 return fanotify_info_file_fh(&FANOTIFY_NE(event)->info);
213         else
214                 return NULL;
215 }
216
217 static inline struct fanotify_info *fanotify_event_info(
218                                                 struct fanotify_event *event)
219 {
220         if (event->type == FANOTIFY_EVENT_TYPE_FID_NAME)
221                 return &FANOTIFY_NE(event)->info;
222         else
223                 return NULL;
224 }
225
226 static inline int fanotify_event_object_fh_len(struct fanotify_event *event)
227 {
228         struct fanotify_info *info = fanotify_event_info(event);
229         struct fanotify_fh *fh = fanotify_event_object_fh(event);
230
231         if (info)
232                 return info->file_fh_totlen ? fh->len : 0;
233         else
234                 return fh ? fh->len : 0;
235 }
236
237 static inline int fanotify_event_dir_fh_len(struct fanotify_event *event)
238 {
239         struct fanotify_info *info = fanotify_event_info(event);
240
241         return info ? fanotify_info_dir_fh_len(info) : 0;
242 }
243
244 struct fanotify_path_event {
245         struct fanotify_event fae;
246         struct path path;
247 };
248
249 static inline struct fanotify_path_event *
250 FANOTIFY_PE(struct fanotify_event *event)
251 {
252         return container_of(event, struct fanotify_path_event, fae);
253 }
254
255 /*
256  * Structure for permission fanotify events. It gets allocated and freed in
257  * fanotify_handle_event() since we wait there for user response. When the
258  * information is retrieved by userspace the structure is moved from
259  * group->notification_list to group->fanotify_data.access_list to wait for
260  * user response.
261  */
262 struct fanotify_perm_event {
263         struct fanotify_event fae;
264         struct path path;
265         unsigned short response;        /* userspace answer to the event */
266         unsigned short state;           /* state of the event */
267         int fd;         /* fd we passed to userspace for this event */
268 };
269
270 static inline struct fanotify_perm_event *
271 FANOTIFY_PERM(struct fanotify_event *event)
272 {
273         return container_of(event, struct fanotify_perm_event, fae);
274 }
275
276 static inline bool fanotify_is_perm_event(u32 mask)
277 {
278         return IS_ENABLED(CONFIG_FANOTIFY_ACCESS_PERMISSIONS) &&
279                 mask & FANOTIFY_PERM_EVENTS;
280 }
281
282 static inline struct fanotify_event *FANOTIFY_E(struct fsnotify_event *fse)
283 {
284         return container_of(fse, struct fanotify_event, fse);
285 }
286
287 static inline bool fanotify_event_has_path(struct fanotify_event *event)
288 {
289         return event->type == FANOTIFY_EVENT_TYPE_PATH ||
290                 event->type == FANOTIFY_EVENT_TYPE_PATH_PERM;
291 }
292
293 static inline struct path *fanotify_event_path(struct fanotify_event *event)
294 {
295         if (event->type == FANOTIFY_EVENT_TYPE_PATH)
296                 return &FANOTIFY_PE(event)->path;
297         else if (event->type == FANOTIFY_EVENT_TYPE_PATH_PERM)
298                 return &FANOTIFY_PERM(event)->path;
299         else
300                 return NULL;
301 }