Skip to content

Commit 9c61f3b

Browse files
amir73iljankara
authored andcommitted
fanotify: break up fanotify_alloc_event()
Break up fanotify_alloc_event() into helpers by event struct type. Suggested-by: Jan Kara <[email protected]> Signed-off-by: Amir Goldstein <[email protected]> Signed-off-by: Jan Kara <[email protected]>
1 parent b8a6c3a commit 9c61f3b

File tree

1 file changed

+89
-65
lines changed

1 file changed

+89
-65
lines changed

fs/notify/fanotify/fanotify.c

Lines changed: 89 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,84 @@ static struct inode *fanotify_fid_inode(struct inode *to_tell, u32 event_mask,
344344
return fsnotify_data_inode(data, data_type);
345345
}
346346

347+
static struct fanotify_event *fanotify_alloc_path_event(const struct path *path,
348+
gfp_t gfp)
349+
{
350+
struct fanotify_path_event *pevent;
351+
352+
pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
353+
if (!pevent)
354+
return NULL;
355+
356+
pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH;
357+
pevent->path = *path;
358+
path_get(path);
359+
360+
return &pevent->fae;
361+
}
362+
363+
static struct fanotify_event *fanotify_alloc_perm_event(const struct path *path,
364+
gfp_t gfp)
365+
{
366+
struct fanotify_perm_event *pevent;
367+
368+
pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
369+
if (!pevent)
370+
return NULL;
371+
372+
pevent->fae.type = FANOTIFY_EVENT_TYPE_PATH_PERM;
373+
pevent->response = 0;
374+
pevent->state = FAN_EVENT_INIT;
375+
pevent->path = *path;
376+
path_get(path);
377+
378+
return &pevent->fae;
379+
}
380+
381+
static struct fanotify_event *fanotify_alloc_fid_event(struct inode *id,
382+
__kernel_fsid_t *fsid,
383+
gfp_t gfp)
384+
{
385+
struct fanotify_fid_event *ffe;
386+
387+
ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
388+
if (!ffe)
389+
return NULL;
390+
391+
ffe->fae.type = FANOTIFY_EVENT_TYPE_FID;
392+
ffe->fsid = *fsid;
393+
fanotify_encode_fh(&ffe->object_fh, id, gfp);
394+
395+
return &ffe->fae;
396+
}
397+
398+
static struct fanotify_event *fanotify_alloc_name_event(struct inode *id,
399+
__kernel_fsid_t *fsid,
400+
const struct qstr *file_name,
401+
gfp_t gfp)
402+
{
403+
struct fanotify_name_event *fne;
404+
405+
fne = kmalloc(sizeof(*fne) + file_name->len + 1, gfp);
406+
if (!fne)
407+
return NULL;
408+
409+
fne->fae.type = FANOTIFY_EVENT_TYPE_FID_NAME;
410+
fne->fsid = *fsid;
411+
fanotify_encode_fh(&fne->dir_fh, id, gfp);
412+
fne->name_len = file_name->len;
413+
strcpy(fne->name, file_name->name);
414+
415+
return &fne->fae;
416+
}
417+
347418
static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
348-
struct inode *inode, u32 mask,
349-
const void *data, int data_type,
350-
const struct qstr *file_name,
351-
__kernel_fsid_t *fsid)
419+
struct inode *inode, u32 mask,
420+
const void *data, int data_type,
421+
const struct qstr *file_name,
422+
__kernel_fsid_t *fsid)
352423
{
353424
struct fanotify_event *event = NULL;
354-
struct fanotify_fid_event *ffe = NULL;
355-
struct fanotify_name_event *fne = NULL;
356425
gfp_t gfp = GFP_KERNEL_ACCOUNT;
357426
struct inode *id = fanotify_fid_inode(inode, mask, data, data_type);
358427
const struct path *path = fsnotify_data_path(data, data_type);
@@ -372,55 +441,23 @@ static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
372441
memalloc_use_memcg(group->memcg);
373442

374443
if (fanotify_is_perm_event(mask)) {
375-
struct fanotify_perm_event *pevent;
376-
377-
pevent = kmem_cache_alloc(fanotify_perm_event_cachep, gfp);
378-
if (!pevent)
379-
goto out;
380-
381-
event = &pevent->fae;
382-
event->type = FANOTIFY_EVENT_TYPE_PATH_PERM;
383-
pevent->response = 0;
384-
pevent->state = FAN_EVENT_INIT;
385-
goto init;
386-
}
387-
388-
/*
389-
* For FAN_DIR_MODIFY event, we report the fid of the directory and
390-
* the name of the modified entry.
391-
* Allocate an fanotify_name_event struct and copy the name.
392-
*/
393-
if (mask & FAN_DIR_MODIFY && !(WARN_ON_ONCE(!file_name))) {
394-
fne = kmalloc(sizeof(*fne) + file_name->len + 1, gfp);
395-
if (!fne)
396-
goto out;
397-
398-
event = &fne->fae;
399-
event->type = FANOTIFY_EVENT_TYPE_FID_NAME;
400-
fne->name_len = file_name->len;
401-
strcpy(fne->name, file_name->name);
402-
goto init;
403-
}
404-
405-
if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
406-
ffe = kmem_cache_alloc(fanotify_fid_event_cachep, gfp);
407-
if (!ffe)
408-
goto out;
409-
410-
event = &ffe->fae;
411-
event->type = FANOTIFY_EVENT_TYPE_FID;
444+
event = fanotify_alloc_perm_event(path, gfp);
445+
} else if (mask & FAN_DIR_MODIFY && !(WARN_ON_ONCE(!file_name))) {
446+
/*
447+
* For FAN_DIR_MODIFY event, we report the fid of the directory
448+
* and the name of the modified entry.
449+
* Allocate an fanotify_name_event struct and copy the name.
450+
*/
451+
event = fanotify_alloc_name_event(id, fsid, file_name, gfp);
452+
} else if (FAN_GROUP_FLAG(group, FAN_REPORT_FID)) {
453+
event = fanotify_alloc_fid_event(id, fsid, gfp);
412454
} else {
413-
struct fanotify_path_event *pevent;
414-
415-
pevent = kmem_cache_alloc(fanotify_path_event_cachep, gfp);
416-
if (!pevent)
417-
goto out;
418-
419-
event = &pevent->fae;
420-
event->type = FANOTIFY_EVENT_TYPE_PATH;
455+
event = fanotify_alloc_path_event(path, gfp);
421456
}
422457

423-
init:
458+
if (!event)
459+
goto out;
460+
424461
/*
425462
* Use the victim inode instead of the watching inode as the id for
426463
* event queue, so event reported on parent is merged with event
@@ -432,19 +469,6 @@ static struct fanotify_event *fanotify_alloc_event(struct fsnotify_group *group,
432469
else
433470
event->pid = get_pid(task_tgid(current));
434471

435-
if (fsid && fanotify_event_fsid(event))
436-
*fanotify_event_fsid(event) = *fsid;
437-
438-
if (fanotify_event_object_fh(event))
439-
fanotify_encode_fh(fanotify_event_object_fh(event), id, gfp);
440-
441-
if (fanotify_event_dir_fh(event))
442-
fanotify_encode_fh(fanotify_event_dir_fh(event), id, gfp);
443-
444-
if (fanotify_event_has_path(event)) {
445-
*fanotify_event_path(event) = *path;
446-
path_get(path);
447-
}
448472
out:
449473
memalloc_unuse_memcg();
450474
return event;

0 commit comments

Comments
 (0)