Skip to content

Commit a376007

Browse files
akaherrostedt
authored andcommitted
eventfs: Implement functions to create files and dirs when accessed
Add create_file() and create_dir() functions to create the files and directories respectively when they are accessed. The functions will be called from the lookup operation of the inode_operations or from the open function of file_operations. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ajay Kaher <[email protected]> Co-developed-by: Steven Rostedt (VMware) <[email protected]> Signed-off-by: Steven Rostedt (VMware) <[email protected]> Tested-by: Ching-lin Yu <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 6394044 commit a376007

File tree

3 files changed

+136
-2
lines changed

3 files changed

+136
-2
lines changed

fs/tracefs/event_inode.c

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,34 @@ static struct dentry *create_file(const char *name, umode_t mode,
101101
struct dentry *parent, void *data,
102102
const struct file_operations *fop)
103103
{
104-
return NULL;
104+
struct tracefs_inode *ti;
105+
struct dentry *dentry;
106+
struct inode *inode;
107+
108+
if (!(mode & S_IFMT))
109+
mode |= S_IFREG;
110+
111+
if (WARN_ON_ONCE(!S_ISREG(mode)))
112+
return NULL;
113+
114+
dentry = eventfs_start_creating(name, parent);
115+
116+
if (IS_ERR(dentry))
117+
return dentry;
118+
119+
inode = tracefs_get_inode(dentry->d_sb);
120+
if (unlikely(!inode))
121+
return eventfs_failed_creating(dentry);
122+
123+
inode->i_mode = mode;
124+
inode->i_fop = fop;
125+
inode->i_private = data;
126+
127+
ti = get_tracefs(inode);
128+
ti->flags |= TRACEFS_EVENT_INODE;
129+
d_instantiate(dentry, inode);
130+
fsnotify_create(dentry->d_parent->d_inode, dentry);
131+
return eventfs_end_creating(dentry);
105132
};
106133

107134
/**
@@ -123,7 +150,31 @@ static struct dentry *create_file(const char *name, umode_t mode,
123150
*/
124151
static struct dentry *create_dir(const char *name, struct dentry *parent, void *data)
125152
{
126-
return NULL;
153+
struct tracefs_inode *ti;
154+
struct dentry *dentry;
155+
struct inode *inode;
156+
157+
dentry = eventfs_start_creating(name, parent);
158+
if (IS_ERR(dentry))
159+
return dentry;
160+
161+
inode = tracefs_get_inode(dentry->d_sb);
162+
if (unlikely(!inode))
163+
return eventfs_failed_creating(dentry);
164+
165+
inode->i_mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
166+
inode->i_op = &eventfs_root_dir_inode_operations;
167+
inode->i_fop = &eventfs_file_operations;
168+
inode->i_private = data;
169+
170+
ti = get_tracefs(inode);
171+
ti->flags |= TRACEFS_EVENT_INODE;
172+
173+
inc_nlink(inode);
174+
d_instantiate(dentry, inode);
175+
inc_nlink(dentry->d_parent->d_inode);
176+
fsnotify_mkdir(dentry->d_parent->d_inode, dentry);
177+
return eventfs_end_creating(dentry);
127178
}
128179

129180
/**
@@ -234,6 +285,12 @@ create_dentry(struct eventfs_file *ef, struct dentry *parent, bool lookup)
234285
} else {
235286
/* A race here, should try again (unless freed) */
236287
invalidate = true;
288+
289+
/*
290+
* Should never happen unless we get here due to being freed.
291+
* Otherwise it means two dentries exist with the same name.
292+
*/
293+
WARN_ON_ONCE(!ef->is_freed);
237294
}
238295
mutex_unlock(&eventfs_mutex);
239296
if (invalidate)

fs/tracefs/inode.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,80 @@ struct dentry *tracefs_end_creating(struct dentry *dentry)
474474
return dentry;
475475
}
476476

477+
/**
478+
* eventfs_start_creating - start the process of creating a dentry
479+
* @name: Name of the file created for the dentry
480+
* @parent: The parent dentry where this dentry will be created
481+
*
482+
* This is a simple helper function for the dynamically created eventfs
483+
* files. When the directory of the eventfs files are accessed, their
484+
* dentries are created on the fly. This function is used to start that
485+
* process.
486+
*/
487+
struct dentry *eventfs_start_creating(const char *name, struct dentry *parent)
488+
{
489+
struct dentry *dentry;
490+
int error;
491+
492+
error = simple_pin_fs(&trace_fs_type, &tracefs_mount,
493+
&tracefs_mount_count);
494+
if (error)
495+
return ERR_PTR(error);
496+
497+
/*
498+
* If the parent is not specified, we create it in the root.
499+
* We need the root dentry to do this, which is in the super
500+
* block. A pointer to that is in the struct vfsmount that we
501+
* have around.
502+
*/
503+
if (!parent)
504+
parent = tracefs_mount->mnt_root;
505+
506+
if (unlikely(IS_DEADDIR(parent->d_inode)))
507+
dentry = ERR_PTR(-ENOENT);
508+
else
509+
dentry = lookup_one_len(name, parent, strlen(name));
510+
511+
if (!IS_ERR(dentry) && dentry->d_inode) {
512+
dput(dentry);
513+
dentry = ERR_PTR(-EEXIST);
514+
}
515+
516+
if (IS_ERR(dentry))
517+
simple_release_fs(&tracefs_mount, &tracefs_mount_count);
518+
519+
return dentry;
520+
}
521+
522+
/**
523+
* eventfs_failed_creating - clean up a failed eventfs dentry creation
524+
* @dentry: The dentry to clean up
525+
*
526+
* If after calling eventfs_start_creating(), a failure is detected, the
527+
* resources created by eventfs_start_creating() needs to be cleaned up. In
528+
* that case, this function should be called to perform that clean up.
529+
*/
530+
struct dentry *eventfs_failed_creating(struct dentry *dentry)
531+
{
532+
dput(dentry);
533+
simple_release_fs(&tracefs_mount, &tracefs_mount_count);
534+
return NULL;
535+
}
536+
537+
/**
538+
* eventfs_end_creating - Finish the process of creating a eventfs dentry
539+
* @dentry: The dentry that has successfully been created.
540+
*
541+
* This function is currently just a place holder to match
542+
* eventfs_start_creating(). In case any synchronization needs to be added,
543+
* this function will be used to implement that without having to modify
544+
* the callers of eventfs_start_creating().
545+
*/
546+
struct dentry *eventfs_end_creating(struct dentry *dentry)
547+
{
548+
return dentry;
549+
}
550+
477551
/**
478552
* tracefs_create_file - create a file in the tracefs filesystem
479553
* @name: a pointer to a string containing the name of the file to create.

fs/tracefs/internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ struct dentry *tracefs_start_creating(const char *name, struct dentry *parent);
2121
struct dentry *tracefs_end_creating(struct dentry *dentry);
2222
struct dentry *tracefs_failed_creating(struct dentry *dentry);
2323
struct inode *tracefs_get_inode(struct super_block *sb);
24+
struct dentry *eventfs_start_creating(const char *name, struct dentry *parent);
25+
struct dentry *eventfs_failed_creating(struct dentry *dentry);
26+
struct dentry *eventfs_end_creating(struct dentry *dentry);
2427
void eventfs_set_ef_status_free(struct dentry *dentry);
2528

2629
#endif /* _TRACEFS_INTERNAL_H */

0 commit comments

Comments
 (0)