Skip to content

Commit 9740d17

Browse files
amir73iljankara
authored andcommitted
fsnotify: pass optional file access range in pre-content event
We would like to add file range information to pre-content events. Pass a struct file_range with offset and length to event handler along with pre-content permission event. The offset and length are aligned to page size, but we may need to align them to minimum folio size for filesystems with large block size. Signed-off-by: Amir Goldstein <[email protected]> Signed-off-by: Jan Kara <[email protected]> Link: https://patch.msgid.link/88eddee301231d814aede27fb4d5b41ae37c9702.1731684329.git.josef@toxicpanda.com
1 parent f156524 commit 9740d17

File tree

5 files changed

+71
-4
lines changed

5 files changed

+71
-4
lines changed

fs/notify/fanotify/fanotify.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -548,9 +548,13 @@ static struct fanotify_event *fanotify_alloc_path_event(const struct path *path,
548548
return &pevent->fae;
549549
}
550550

551-
static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
551+
static struct fanotify_event *fanotify_alloc_perm_event(const void *data,
552+
int data_type,
552553
gfp_t gfp)
553554
{
555+
const struct path *path = fsnotify_data_path(data, data_type);
556+
const struct file_range *range =
557+
fsnotify_data_file_range(data, data_type);
554558
struct fanotify_perm_event *pevent;
555559

556560
pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
@@ -564,6 +568,9 @@ static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
564568
pevent->hdr.len = 0;
565569
pevent->state = FAN_EVENT_INIT;
566570
pevent->path = *path;
571+
/* NULL ppos means no range info */
572+
pevent->ppos = range ? &range->pos : NULL;
573+
pevent->count = range ? range->count : 0;
567574
path_get(path);
568575

569576
return &pevent->fae;
@@ -801,7 +808,7 @@ static struct fanotify_event *fanotify_alloc_event(
801808
old_memcg = set_active_memcg(group->memcg);
802809

803810
if (fanotify_is_perm_event(mask)) {
804-
event = fanotify_alloc_perm_event(path, gfp);
811+
event = fanotify_alloc_perm_event(data, data_type, gfp);
805812
} else if (fanotify_is_error_event(mask)) {
806813
event = fanotify_alloc_error_event(group, fsid, data,
807814
data_type, &hash);

fs/notify/fanotify/fanotify.h

+2
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ FANOTIFY_PE(struct fanotify_event *event)
425425
struct fanotify_perm_event {
426426
struct fanotify_event fae;
427427
struct path path;
428+
const loff_t *ppos; /* optional file range info */
429+
size_t count;
428430
u32 response; /* userspace answer to the event */
429431
unsigned short state; /* state of the event */
430432
int fd; /* fd we passed to userspace for this event */

fs/notify/fsnotify.c

+18
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,24 @@ static inline bool fsnotify_object_watched(struct inode *inode, __u32 mnt_mask,
203203
return mask & marks_mask & ALL_FSNOTIFY_EVENTS;
204204
}
205205

206+
/* Report pre-content event with optional range info */
207+
int fsnotify_pre_content(const struct path *path, const loff_t *ppos,
208+
size_t count)
209+
{
210+
struct file_range range;
211+
212+
/* Report page aligned range only when pos is known */
213+
if (!ppos)
214+
return fsnotify_path(path, FS_PRE_ACCESS);
215+
216+
range.path = path;
217+
range.pos = PAGE_ALIGN_DOWN(*ppos);
218+
range.count = PAGE_ALIGN(*ppos + count) - range.pos;
219+
220+
return fsnotify_parent(path->dentry, FS_PRE_ACCESS, &range,
221+
FSNOTIFY_EVENT_FILE_RANGE);
222+
}
223+
206224
/*
207225
* Notify this dentry's parent about a child's events with child name info
208226
* if parent is watching or if inode/sb/mount are interested in events with

include/linux/fsnotify.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
154154
* read()/write() and other types of access generate pre-content events.
155155
*/
156156
if (unlikely(FMODE_FSNOTIFY_HSM(file->f_mode))) {
157-
int ret = fsnotify_path(&file->f_path, FS_PRE_ACCESS);
157+
int ret = fsnotify_pre_content(&file->f_path, ppos, count);
158158

159159
if (ret)
160160
return ret;
@@ -171,7 +171,7 @@ static inline int fsnotify_file_area_perm(struct file *file, int perm_mask,
171171
}
172172

173173
/*
174-
* fsnotify_file_perm - permission hook before file access
174+
* fsnotify_file_perm - permission hook before file access (unknown range)
175175
*/
176176
static inline int fsnotify_file_perm(struct file *file, int perm_mask)
177177
{

include/linux/fsnotify_backend.h

+40
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ static inline void fsnotify_group_assert_locked(struct fsnotify_group *group)
294294
/* When calling fsnotify tell it if the data is a path or inode */
295295
enum fsnotify_data_type {
296296
FSNOTIFY_EVENT_NONE,
297+
FSNOTIFY_EVENT_FILE_RANGE,
297298
FSNOTIFY_EVENT_PATH,
298299
FSNOTIFY_EVENT_INODE,
299300
FSNOTIFY_EVENT_DENTRY,
@@ -306,6 +307,17 @@ struct fs_error_report {
306307
struct super_block *sb;
307308
};
308309

310+
struct file_range {
311+
const struct path *path;
312+
loff_t pos;
313+
size_t count;
314+
};
315+
316+
static inline const struct path *file_range_path(const struct file_range *range)
317+
{
318+
return range->path;
319+
}
320+
309321
static inline struct inode *fsnotify_data_inode(const void *data, int data_type)
310322
{
311323
switch (data_type) {
@@ -315,6 +327,8 @@ static inline struct inode *fsnotify_data_inode(const void *data, int data_type)
315327
return d_inode(data);
316328
case FSNOTIFY_EVENT_PATH:
317329
return d_inode(((const struct path *)data)->dentry);
330+
case FSNOTIFY_EVENT_FILE_RANGE:
331+
return d_inode(file_range_path(data)->dentry);
318332
case FSNOTIFY_EVENT_ERROR:
319333
return ((struct fs_error_report *)data)->inode;
320334
default:
@@ -330,6 +344,8 @@ static inline struct dentry *fsnotify_data_dentry(const void *data, int data_typ
330344
return (struct dentry *)data;
331345
case FSNOTIFY_EVENT_PATH:
332346
return ((const struct path *)data)->dentry;
347+
case FSNOTIFY_EVENT_FILE_RANGE:
348+
return file_range_path(data)->dentry;
333349
default:
334350
return NULL;
335351
}
@@ -341,6 +357,8 @@ static inline const struct path *fsnotify_data_path(const void *data,
341357
switch (data_type) {
342358
case FSNOTIFY_EVENT_PATH:
343359
return data;
360+
case FSNOTIFY_EVENT_FILE_RANGE:
361+
return file_range_path(data);
344362
default:
345363
return NULL;
346364
}
@@ -356,6 +374,8 @@ static inline struct super_block *fsnotify_data_sb(const void *data,
356374
return ((struct dentry *)data)->d_sb;
357375
case FSNOTIFY_EVENT_PATH:
358376
return ((const struct path *)data)->dentry->d_sb;
377+
case FSNOTIFY_EVENT_FILE_RANGE:
378+
return file_range_path(data)->dentry->d_sb;
359379
case FSNOTIFY_EVENT_ERROR:
360380
return ((struct fs_error_report *) data)->sb;
361381
default:
@@ -375,6 +395,18 @@ static inline struct fs_error_report *fsnotify_data_error_report(
375395
}
376396
}
377397

398+
static inline const struct file_range *fsnotify_data_file_range(
399+
const void *data,
400+
int data_type)
401+
{
402+
switch (data_type) {
403+
case FSNOTIFY_EVENT_FILE_RANGE:
404+
return (struct file_range *)data;
405+
default:
406+
return NULL;
407+
}
408+
}
409+
378410
/*
379411
* Index to merged marks iterator array that correlates to a type of watch.
380412
* The type of watched object can be deduced from the iterator type, but not
@@ -863,9 +895,17 @@ static inline void fsnotify_init_event(struct fsnotify_event *event)
863895
{
864896
INIT_LIST_HEAD(&event->list);
865897
}
898+
int fsnotify_pre_content(const struct path *path, const loff_t *ppos,
899+
size_t count);
866900

867901
#else
868902

903+
static inline int fsnotify_pre_content(const struct path *path,
904+
const loff_t *ppos, size_t count)
905+
{
906+
return 0;
907+
}
908+
869909
static inline int fsnotify(__u32 mask, const void *data, int data_type,
870910
struct inode *dir, const struct qstr *name,
871911
struct inode *inode, u32 cookie)

0 commit comments

Comments
 (0)