Skip to content

Commit 3bcf386

Browse files
committed
fsnotify: store struct file not struct path
Al explains that calling dentry_open() with a mnt/dentry pair is only garunteed to be safe if they are already used in an open struct file. To make sure this is the case don't store and use a struct path in fsnotify, always use a struct file. Signed-off-by: Eric Paris <eparis@redhat.com>
1 parent f70ab54 commit 3bcf386

8 files changed

Lines changed: 56 additions & 63 deletions

File tree

‎fs/notify/fanotify/fanotify.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ static bool should_merge(struct fsnotify_event *old, struct fsnotify_event *new)
1717
old->data_type == new->data_type &&
1818
old->tgid == new->tgid) {
1919
switch (old->data_type) {
20-
case (FSNOTIFY_EVENT_PATH):
21-
if ((old->path.mnt == new->path.mnt) &&
22-
(old->path.dentry == new->path.dentry))
20+
case (FSNOTIFY_EVENT_FILE):
21+
if ((old->file->f_path.mnt == new->file->f_path.mnt) &&
22+
(old->file->f_path.dentry == new->file->f_path.dentry))
2323
return true;
2424
case (FSNOTIFY_EVENT_NONE):
2525
return true;
@@ -226,7 +226,7 @@ static bool fanotify_should_send_event(struct fsnotify_group *group, struct inod
226226
return false;
227227

228228
/* if we don't have enough info to send an event to userspace say no */
229-
if (data_type != FSNOTIFY_EVENT_PATH)
229+
if (data_type != FSNOTIFY_EVENT_FILE)
230230
return false;
231231

232232
if (mnt)

‎fs/notify/fanotify/fanotify_user.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
6565
if (client_fd < 0)
6666
return client_fd;
6767

68-
if (event->data_type != FSNOTIFY_EVENT_PATH) {
68+
if (event->data_type != FSNOTIFY_EVENT_FILE) {
6969
WARN_ON(1);
7070
put_unused_fd(client_fd);
7171
return -EINVAL;
@@ -75,8 +75,8 @@ static int create_fd(struct fsnotify_group *group, struct fsnotify_event *event)
7575
* we need a new file handle for the userspace program so it can read even if it was
7676
* originally opened O_WRONLY.
7777
*/
78-
dentry = dget(event->path.dentry);
79-
mnt = mntget(event->path.mnt);
78+
dentry = dget(event->file->f_path.dentry);
79+
mnt = mntget(event->file->f_path.mnt);
8080
/* it's possible this event was an overflow event. in that case dentry and mnt
8181
* are NULL; That's fine, just don't call dentry open */
8282
if (dentry && mnt)

‎fs/notify/fsnotify.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ void __fsnotify_update_child_dentry_flags(struct inode *inode)
8484
}
8585

8686
/* Notify this dentry's parent about a child's events. */
87-
void __fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
87+
void __fsnotify_parent(struct file *file, struct dentry *dentry, __u32 mask)
8888
{
8989
struct dentry *parent;
9090
struct inode *p_inode;
9191
bool send = false;
9292
bool should_update_children = false;
9393

9494
if (!dentry)
95-
dentry = path->dentry;
95+
dentry = file->f_path.dentry;
9696

9797
if (!(dentry->d_flags & DCACHE_FSNOTIFY_PARENT_WATCHED))
9898
return;
@@ -124,8 +124,8 @@ void __fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
124124
* specifies these are events which came from a child. */
125125
mask |= FS_EVENT_ON_CHILD;
126126

127-
if (path)
128-
fsnotify(p_inode, mask, path, FSNOTIFY_EVENT_PATH,
127+
if (file)
128+
fsnotify(p_inode, mask, file, FSNOTIFY_EVENT_FILE,
129129
dentry->d_name.name, 0);
130130
else
131131
fsnotify(p_inode, mask, dentry->d_inode, FSNOTIFY_EVENT_INODE,
@@ -154,10 +154,10 @@ void __fsnotify_flush_ignored_mask(struct inode *inode, void *data, int data_is)
154154
spin_unlock(&inode->i_lock);
155155
}
156156

157-
if (data_is == FSNOTIFY_EVENT_PATH) {
157+
if (data_is == FSNOTIFY_EVENT_FILE) {
158158
struct vfsmount *mnt;
159159

160-
mnt = ((struct path *)data)->mnt;
160+
mnt = ((struct file *)data)->f_path.mnt;
161161
if (mnt && !hlist_empty(&mnt->mnt_fsnotify_marks)) {
162162
spin_lock(&mnt->mnt_root->d_lock);
163163
hlist_for_each_entry(mark, node, &mnt->mnt_fsnotify_marks, m.m_list) {
@@ -228,8 +228,8 @@ int fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is,
228228
!(test_mask & fsnotify_vfsmount_mask))
229229
return 0;
230230

231-
if (data_is == FSNOTIFY_EVENT_PATH)
232-
mnt = ((struct path *)data)->mnt;
231+
if (data_is == FSNOTIFY_EVENT_FILE)
232+
mnt = ((struct file *)data)->f_path.mnt;
233233

234234
/* if this inode's directed listeners don't care and nothing on the vfsmount
235235
* listeners list cares, nothing to do */

‎fs/notify/inotify/inotify_fsnotify.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ static bool event_compare(struct fsnotify_event *old, struct fsnotify_event *new
5252
!strcmp(old->file_name, new->file_name))
5353
return true;
5454
break;
55-
case (FSNOTIFY_EVENT_PATH):
56-
if ((old->path.mnt == new->path.mnt) &&
57-
(old->path.dentry == new->path.dentry))
55+
case (FSNOTIFY_EVENT_FILE):
56+
if ((old->file->f_path.mnt == new->file->f_path.mnt) &&
57+
(old->file->f_path.dentry == new->file->f_path.dentry))
5858
return true;
5959
break;
6060
case (FSNOTIFY_EVENT_NONE):
@@ -165,10 +165,10 @@ static bool inotify_should_send_event(struct fsnotify_group *group, struct inode
165165
send = (fsn_mark->mask & mask);
166166

167167
if (send && (fsn_mark->mask & FS_EXCL_UNLINK) &&
168-
(data_type == FSNOTIFY_EVENT_PATH)) {
169-
struct path *path = data;
168+
(data_type == FSNOTIFY_EVENT_FILE)) {
169+
struct file *file = data;
170170

171-
if (d_unlinked(path->dentry))
171+
if (d_unlinked(file->f_path.dentry))
172172
send = false;
173173
}
174174

‎fs/notify/notification.c‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* allocated and used.
3232
*/
3333

34+
#include <linux/file.h>
3435
#include <linux/fs.h>
3536
#include <linux/init.h>
3637
#include <linux/kernel.h>
@@ -89,8 +90,8 @@ void fsnotify_put_event(struct fsnotify_event *event)
8990
if (atomic_dec_and_test(&event->refcnt)) {
9091
pr_debug("%s: event=%p\n", __func__, event);
9192

92-
if (event->data_type == FSNOTIFY_EVENT_PATH)
93-
path_put(&event->path);
93+
if (event->data_type == FSNOTIFY_EVENT_FILE)
94+
fput(event->file);
9495

9596
BUG_ON(!list_empty(&event->private_data_list));
9697

@@ -375,8 +376,8 @@ struct fsnotify_event *fsnotify_clone_event(struct fsnotify_event *old_event)
375376
}
376377
}
377378
event->tgid = get_pid(old_event->tgid);
378-
if (event->data_type == FSNOTIFY_EVENT_PATH)
379-
path_get(&event->path);
379+
if (event->data_type == FSNOTIFY_EVENT_FILE)
380+
get_file(event->file);
380381

381382
return event;
382383
}
@@ -423,20 +424,17 @@ struct fsnotify_event *fsnotify_create_event(struct inode *to_tell, __u32 mask,
423424
event->data_type = data_type;
424425

425426
switch (data_type) {
426-
case FSNOTIFY_EVENT_PATH: {
427-
struct path *path = data;
428-
event->path.dentry = path->dentry;
429-
event->path.mnt = path->mnt;
430-
path_get(&event->path);
427+
case FSNOTIFY_EVENT_FILE: {
428+
event->file = data;
429+
get_file(event->file);
431430
break;
432431
}
433432
case FSNOTIFY_EVENT_INODE:
434433
event->inode = data;
435434
break;
436435
case FSNOTIFY_EVENT_NONE:
437436
event->inode = NULL;
438-
event->path.dentry = NULL;
439-
event->path.mnt = NULL;
437+
event->file = NULL;
440438
break;
441439
default:
442440
BUG();

‎include/linux/fsnotify.h‎

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,18 @@ static inline void fsnotify_d_instantiate(struct dentry *dentry,
2626
}
2727

2828
/* Notify this dentry's parent about a child's events. */
29-
static inline void fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
29+
static inline void fsnotify_parent(struct file *file, struct dentry *dentry, __u32 mask)
3030
{
3131
if (!dentry)
32-
dentry = path->dentry;
32+
dentry = file->f_path.dentry;
3333

34-
__fsnotify_parent(path, dentry, mask);
34+
__fsnotify_parent(file, dentry, mask);
3535
}
3636

3737
/* simple call site for access decisions */
3838
static inline int fsnotify_perm(struct file *file, int mask)
3939
{
40-
struct path *path = &file->f_path;
41-
struct inode *inode = path->dentry->d_inode;
40+
struct inode *inode = file->f_path.dentry->d_inode;
4241
__u32 fsnotify_mask = 0;
4342

4443
if (file->f_mode & FMODE_NONOTIFY)
@@ -52,7 +51,7 @@ static inline int fsnotify_perm(struct file *file, int mask)
5251
else
5352
BUG();
5453

55-
return fsnotify(inode, fsnotify_mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
54+
return fsnotify(inode, fsnotify_mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
5655
}
5756

5857
/*
@@ -187,16 +186,15 @@ static inline void fsnotify_mkdir(struct inode *inode, struct dentry *dentry)
187186
*/
188187
static inline void fsnotify_access(struct file *file)
189188
{
190-
struct path *path = &file->f_path;
191-
struct inode *inode = path->dentry->d_inode;
189+
struct inode *inode = file->f_path.dentry->d_inode;
192190
__u32 mask = FS_ACCESS;
193191

194192
if (S_ISDIR(inode->i_mode))
195193
mask |= FS_IN_ISDIR;
196194

197195
if (!(file->f_mode & FMODE_NONOTIFY)) {
198-
fsnotify_parent(path, NULL, mask);
199-
fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
196+
fsnotify_parent(file, NULL, mask);
197+
fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
200198
}
201199
}
202200

@@ -205,16 +203,15 @@ static inline void fsnotify_access(struct file *file)
205203
*/
206204
static inline void fsnotify_modify(struct file *file)
207205
{
208-
struct path *path = &file->f_path;
209-
struct inode *inode = path->dentry->d_inode;
206+
struct inode *inode = file->f_path.dentry->d_inode;
210207
__u32 mask = FS_MODIFY;
211208

212209
if (S_ISDIR(inode->i_mode))
213210
mask |= FS_IN_ISDIR;
214211

215212
if (!(file->f_mode & FMODE_NONOTIFY)) {
216-
fsnotify_parent(path, NULL, mask);
217-
fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
213+
fsnotify_parent(file, NULL, mask);
214+
fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
218215
}
219216
}
220217

@@ -223,16 +220,15 @@ static inline void fsnotify_modify(struct file *file)
223220
*/
224221
static inline void fsnotify_open(struct file *file)
225222
{
226-
struct path *path = &file->f_path;
227-
struct inode *inode = path->dentry->d_inode;
223+
struct inode *inode = file->f_path.dentry->d_inode;
228224
__u32 mask = FS_OPEN;
229225

230226
if (S_ISDIR(inode->i_mode))
231227
mask |= FS_IN_ISDIR;
232228

233229
if (!(file->f_mode & FMODE_NONOTIFY)) {
234-
fsnotify_parent(path, NULL, mask);
235-
fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
230+
fsnotify_parent(file, NULL, mask);
231+
fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
236232
}
237233
}
238234

@@ -241,7 +237,6 @@ static inline void fsnotify_open(struct file *file)
241237
*/
242238
static inline void fsnotify_close(struct file *file)
243239
{
244-
struct path *path = &file->f_path;
245240
struct inode *inode = file->f_path.dentry->d_inode;
246241
fmode_t mode = file->f_mode;
247242
__u32 mask = (mode & FMODE_WRITE) ? FS_CLOSE_WRITE : FS_CLOSE_NOWRITE;
@@ -250,8 +245,8 @@ static inline void fsnotify_close(struct file *file)
250245
mask |= FS_IN_ISDIR;
251246

252247
if (!(file->f_mode & FMODE_NONOTIFY)) {
253-
fsnotify_parent(path, NULL, mask);
254-
fsnotify(inode, mask, path, FSNOTIFY_EVENT_PATH, NULL, 0);
248+
fsnotify_parent(file, NULL, mask);
249+
fsnotify(inode, mask, file, FSNOTIFY_EVENT_FILE, NULL, 0);
255250
}
256251
}
257252

‎include/linux/fsnotify_backend.h‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,20 +223,20 @@ struct fsnotify_event {
223223
/* to_tell may ONLY be dereferenced during handle_event(). */
224224
struct inode *to_tell; /* either the inode the event happened to or its parent */
225225
/*
226-
* depending on the event type we should have either a path or inode
227-
* We hold a reference on path, but NOT on inode. Since we have the ref on
228-
* the path, it may be dereferenced at any point during this object's
226+
* depending on the event type we should have either a file or inode
227+
* We hold a reference on file, but NOT on inode. Since we have the ref on
228+
* the file, it may be dereferenced at any point during this object's
229229
* lifetime. That reference is dropped when this object's refcnt hits
230-
* 0. If this event contains an inode instead of a path, the inode may
230+
* 0. If this event contains an inode instead of a file, the inode may
231231
* ONLY be used during handle_event().
232232
*/
233233
union {
234-
struct path path;
234+
struct file *file;
235235
struct inode *inode;
236236
};
237237
/* when calling fsnotify tell it if the data is a path or inode */
238238
#define FSNOTIFY_EVENT_NONE 0
239-
#define FSNOTIFY_EVENT_PATH 1
239+
#define FSNOTIFY_EVENT_FILE 1
240240
#define FSNOTIFY_EVENT_INODE 2
241241
int data_type; /* which of the above union we have */
242242
atomic_t refcnt; /* how many groups still are using/need to send this event */
@@ -311,7 +311,7 @@ struct fsnotify_mark {
311311
/* main fsnotify call to send events */
312312
extern int fsnotify(struct inode *to_tell, __u32 mask, void *data, int data_is,
313313
const unsigned char *name, u32 cookie);
314-
extern void __fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask);
314+
extern void __fsnotify_parent(struct file *file, struct dentry *dentry, __u32 mask);
315315
extern void __fsnotify_inode_delete(struct inode *inode);
316316
extern void __fsnotify_vfsmount_delete(struct vfsmount *mnt);
317317
extern u32 fsnotify_get_cookie(void);
@@ -444,7 +444,7 @@ static inline int fsnotify(struct inode *to_tell, __u32 mask, void *data, int da
444444
return 0;
445445
}
446446

447-
static inline void __fsnotify_parent(struct path *path, struct dentry *dentry, __u32 mask)
447+
static inline void __fsnotify_parent(struct file *file, struct dentry *dentry, __u32 mask)
448448
{}
449449

450450
static inline void __fsnotify_inode_delete(struct inode *inode)

‎kernel/audit_watch.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,8 @@ static int audit_watch_handle_event(struct fsnotify_group *group, struct fsnotif
545545
return 0;
546546

547547
switch (event->data_type) {
548-
case (FSNOTIFY_EVENT_PATH):
549-
inode = event->path.dentry->d_inode;
548+
case (FSNOTIFY_EVENT_FILE):
549+
inode = event->file->f_path.dentry->d_inode;
550550
break;
551551
case (FSNOTIFY_EVENT_INODE):
552552
inode = event->inode;

0 commit comments

Comments
 (0)