From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
To: linux-fsdevel@vger.kernel.org
Cc: Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
Maxime Ripard <mripard@kernel.org>,
Thomas Zimmermann <tzimmermann@suse.de>,
David Airlie <airlied@linux.ie>, Daniel Vetter <daniel@ffwll.ch>,
Frederic Barrat <fbarrat@linux.ibm.com>,
Andrew Donnellan <ajd@linux.ibm.com>,
Arnd Bergmann <arnd@arndb.de>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Manoj N. Kumar" <manoj@linux.ibm.com>,
"Matthew R. Ochs" <mrochs@linux.ibm.com>,
Uma Krishnan <ukrishn@linux.ibm.com>,
"James E.J. Bottomley" <jejb@linux.ibm.com>,
"Martin K. Petersen" <martin.petersen@oracle.com>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Joel Becker <jlbec@evilplan.org>, Christoph Hellwig <hch@lst.de>,
"Rafael J. Wysocki" <rafael@kernel.org>,
John Johansen <john.johansen@canonical.com>,
James Morris <jmorris@namei.org>,
"Serge E. Hallyn" <serge@hallyn.com>,
dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
linuxppc-dev@lists.ozlabs.org, linux-scsi@vger.kernel.org,
linux-security-module@vger.kernel.org,
Paolo Bonzini <pbonzini@redhat.com>,
Emanuele Giuseppe Esposito <eesposit@redhat.com>
Subject: [PATCH v2 5/7] libfs: add file creation functions
Date: Tue, 21 Apr 2020 15:57:39 +0200 [thread overview]
Message-ID: <20200421135741.30657-3-eesposit@redhat.com> (raw)
In-Reply-To: <20200421135119.30007-1-eesposit@redhat.com>
A bunch of code is duplicated between debugfs and tracefs, unify it to the
libfs library.
The code is very similar, except that dentry and inode creation are unified
into a single function (unlike start_creating in debugfs and tracefs, which
only takes care of dentries). This adds an output parameter to the
creation functions, but pushes all error recovery into fs/libfs.c.
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
---
fs/libfs.c | 226 +++++++++++++++++++++++++++++++++++++++++++++
include/linux/fs.h | 18 ++++
2 files changed, 244 insertions(+)
diff --git a/fs/libfs.c b/fs/libfs.c
index 5c76e4c648dc..90b0c221d9a2 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -751,6 +751,232 @@ struct inode *simple_alloc_anon_inode(struct simple_fs *fs)
}
EXPORT_SYMBOL(simple_alloc_anon_inode);
+static struct dentry *failed_creating(struct simple_fs *fs, struct dentry *dentry)
+{
+ inode_unlock(d_inode(dentry->d_parent));
+ dput(dentry);
+ simple_release_fs(fs);
+ return ERR_PTR(-ENOMEM);
+}
+
+/**
+ * simplefs_create_dentry - creates a new dentry and inode
+ * @fs: a pointer to a struct simple_fs containing the reference counter
+ * and vfs_mount pointer
+ * @type: the fs type
+ * @name: dentry name
+ * @parent: parent dentry. If this parameter is NULL,
+ * then the dentry will be created in the root of the
+ * filesystem.
+ * @inode: pointer that will contain a newly created inode
+ *
+ * This function returns a new dentry, or NULL on error. On success, a
+ * new inode is created and stored into @inode. Also note that the inode
+ * for the parent directory is locked by simplefs_create_dentry(),
+ * and will be unlocked by simple_finish_dentry().
+ **/
+struct dentry *simplefs_create_dentry(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, struct dentry *parent,
+ struct inode **inode)
+{
+ struct dentry *dentry;
+ int error;
+
+ pr_debug("creating file '%s'\n", name);
+
+ if (IS_ERR(parent))
+ return parent;
+
+ error = simple_pin_fs(fs, type);
+ if (error) {
+ pr_err("Unable to pin filesystem for file '%s'\n", name);
+ return ERR_PTR(error);
+ }
+
+ /* If the parent is not specified, we create it in the root.
+ * We need the root dentry to do this, which is in the super
+ * block. A pointer to that is in the struct vfsmount that we
+ * have around.
+ */
+ if (!parent)
+ parent = fs->mount->mnt_root;
+
+ inode_lock(d_inode(parent));
+ dentry = lookup_one_len(name, parent, strlen(name));
+ if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
+ if (d_is_dir(dentry))
+ pr_err("Directory '%s' with parent '%s' already present!\n",
+ name, parent->d_name.name);
+ else
+ pr_err("File '%s' in directory '%s' already present!\n",
+ name, parent->d_name.name);
+ dput(dentry);
+ dentry = ERR_PTR(-EEXIST);
+ }
+
+ if (IS_ERR(dentry)) {
+ inode_unlock(d_inode(parent));
+ simple_release_fs(fs);
+ }
+
+
+ if (IS_ERR(dentry))
+ return dentry;
+
+ *inode = new_inode_current_time(fs->mount->mnt_sb);
+ if (unlikely(!(*inode))) {
+ pr_err("out of free inodes, can not create file '%s'\n",
+ name);
+ return failed_creating(fs, dentry);
+ }
+
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_create_dentry);
+
+/**
+ * simplefs_create_file - creates a new file dentry and inode
+ * @fs: a pointer to a struct simple_fs containing the reference counter
+ * and vfs_mount pointer
+ * @type: the fs type
+ * @name: file name
+ * @mode: file mode
+ * @parent: parent dentry. If this parameter is NULL,
+ * then the file will be created in the root of the
+ * filesystem.
+ * @data: what will the file contain
+ * @inode: pointer that will contain a newly created inode
+ *
+ * This function returns a new dentry, or NULL on error. On success, a
+ * new inode is created and stored into @inode. Also note that the inode
+ * for the parent directory is locked by simplefs_create_dentry(),
+ * and will be unlocked by simple_finish_dentry().
+ **/
+struct dentry *simplefs_create_file(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, umode_t mode,
+ struct dentry *parent, void *data,
+ struct inode **inode)
+{
+ struct dentry *dentry;
+
+ WARN_ON((mode & S_IFMT) && !S_ISREG(mode));
+ mode |= S_IFREG;
+
+ dentry = simplefs_create_dentry(fs, type, name, parent, inode);
+
+ if (IS_ERR(dentry))
+ return dentry;
+
+ (*inode)->i_mode = mode;
+ (*inode)->i_private = data;
+
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_create_file);
+
+
+/**
+ * simplefs_finish_dentry- complete creation of a new dentry
+ * @dentry: the dentry being created
+ * @inode: the inode associated to the dentry
+ *
+ * This function completes the creation of a dentry.
+ * This includes associating @inode with the dentry, ensuring the link
+ * counts are consistent and informing fsnotify.
+ **/
+struct dentry *simplefs_finish_dentry(struct dentry *dentry, struct inode *inode)
+{
+ d_instantiate(dentry, inode);
+ if (S_ISDIR(inode->i_mode)) {
+ inc_nlink(d_inode(dentry->d_parent));
+ fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
+ } else {
+ fsnotify_create(d_inode(dentry->d_parent), dentry);
+ }
+ inode_unlock(d_inode(dentry->d_parent));
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_finish_dentry);
+
+/**
+ * simplefs_create_dir - creates a new directory dentry and inode
+ * @fs: a pointer to a struct simple_fs containing the reference counter
+ * and vfs_mount pointer
+ * @type: the fs type
+ * @name: dir name
+ * @mode: dir mode
+ * @parent: parent dentry. If this parameter is NULL,
+ * then the directory will be created in the root of the
+ * filesystem.
+ * @inode: pointer that will contain a newly created inode
+ *
+ * This function returns a new dentry, or NULL on error. On success, a
+ * new inode is created and stored into @inode. Also note that the inode
+ * for the parent directory is locked by simplefs_create_dentry(),
+ * and will be unlocked by simple_finish_dentry().
+ **/
+struct dentry *simplefs_create_dir(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, umode_t mode, struct dentry *parent,
+ struct inode **inode)
+{
+ struct dentry *dentry;
+
+ WARN_ON((mode & S_IFMT) && !S_ISDIR(mode));
+ mode |= S_IFDIR;
+
+ dentry = simplefs_create_dentry(fs, type, name, parent, inode);
+ if (IS_ERR(dentry))
+ return dentry;
+
+ (*inode)->i_mode = mode;
+ (*inode)->i_op = &simple_dir_inode_operations;
+ (*inode)->i_fop = &simple_dir_operations;
+
+ /* directory inodes start off with i_nlink == 2 (for "." entry) */
+ inc_nlink(*inode);
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_create_dir);
+
+/**
+ * simplefs_create_symlink - creates a new symlink dentry and inode
+ * @fs: a pointer to a struct simple_fs containing the reference counter
+ * and vfs_mount pointer
+ * @type: the fs type
+ * @name: symlink name
+ * @parent: parent dentry. If this parameter is NULL,
+ * then the symbolic link will be created in the root of the
+ * filesystem.
+ * @inode: pointer that will contain a newly created inode
+ *
+ * This function returns a new dentry, or NULL on error. On success, a
+ * new inode is created and stored into @inode. Also note that the inode
+ * for the parent directory is locked by simplefs_create_dentry(),
+ * and will be unlocked by simple_finish_dentry().
+ **/
+struct dentry *simplefs_create_symlink(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, struct dentry *parent,
+ const char *target, struct inode **inode)
+{
+ struct dentry *dentry;
+ char *link = kstrdup(target, GFP_KERNEL);
+
+ if (!link)
+ return ERR_PTR(-ENOMEM);
+
+ dentry = simplefs_create_dentry(fs, type, name, parent, inode);
+ if (IS_ERR(dentry)) {
+ kfree_link(link);
+ return dentry;
+ }
+
+ (*inode)->i_mode = S_IFLNK | S_IRWXUGO;
+ (*inode)->i_link = link;
+ (*inode)->i_op = &simple_symlink_inode_operations;
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_create_symlink);
+
/**
* simple_read_from_buffer - copy data from the buffer to user space
* @to: the user space buffer to read to
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 5e93de72118b..0569540fbe61 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3375,6 +3375,24 @@ extern void simple_release_fs(struct simple_fs *);
extern struct inode *simple_alloc_anon_inode(struct simple_fs *fs);
+extern struct dentry *simplefs_create_dentry(struct simple_fs *fs,
+ struct file_system_type *type,
+ const char *name, struct dentry *parent,
+ struct inode **inode);
+struct dentry *simplefs_finish_dentry(struct dentry *dentry, struct inode *inode);
+
+extern struct dentry *simplefs_create_file(struct simple_fs *fs,
+ struct file_system_type *type,
+ const char *name, umode_t mode,
+ struct dentry *parent, void *data,
+ struct inode **inode);
+extern struct dentry *simplefs_create_dir(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, umode_t mode, struct dentry *parent,
+ struct inode **inode);
+extern struct dentry *simplefs_create_symlink(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, struct dentry *parent,
+ const char *target, struct inode **inode);
+
extern ssize_t simple_read_from_buffer(void __user *to, size_t count,
loff_t *ppos, const void *from, size_t available);
extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
--
2.25.2
WARNING: multiple messages have this Message-ID (diff)
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
To: linux-fsdevel@vger.kernel.org
Cc: "Rafael J. Wysocki" <rafael@kernel.org>,
David Airlie <airlied@linux.ie>,
dri-devel@lists.freedesktop.org, Christoph Hellwig <hch@lst.de>,
Andrew Donnellan <ajd@linux.ibm.com>,
Emanuele Giuseppe Esposito <eesposit@redhat.com>,
linux-scsi@vger.kernel.org, James Morris <jmorris@namei.org>,
"Serge E. Hallyn" <serge@hallyn.com>,
Daniel Vetter <daniel@ffwll.ch>, Arnd Bergmann <arnd@arndb.de>,
"James E.J. Bottomley" <jejb@linux.ibm.com>,
Maarten Lankhorst <maarten.lankhorst@linux.intel.com>,
Maxime Ripard <mripard@kernel.org>,
"Manoj N. Kumar" <manoj@linux.ibm.com>,
Alexander Viro <viro@zeniv.linux.org.uk>,
"Matthew R. Ochs" <mrochs@linux.ibm.com>,
Uma Krishnan <ukrishn@linux.ibm.com>,
John Johansen <john.johansen@canonical.com>,
"Martin K. Petersen" <martin.petersen@oracle.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-kernel@vger.kernel.org,
linux-security-module@vger.kernel.org,
Thomas Zimmermann <tzimmermann@suse.de>,
Frederic Barrat <fbarrat@linux.ibm.com>,
Paolo Bonzini <pbonzini@redhat.com>,
linuxppc-dev@lists.ozlabs.org, Joel Becker <jlbec@evilplan.org>
Subject: [PATCH v2 5/7] libfs: add file creation functions
Date: Tue, 21 Apr 2020 15:57:39 +0200 [thread overview]
Message-ID: <20200421135741.30657-3-eesposit@redhat.com> (raw)
In-Reply-To: <20200421135119.30007-1-eesposit@redhat.com>
A bunch of code is duplicated between debugfs and tracefs, unify it to the
libfs library.
The code is very similar, except that dentry and inode creation are unified
into a single function (unlike start_creating in debugfs and tracefs, which
only takes care of dentries). This adds an output parameter to the
creation functions, but pushes all error recovery into fs/libfs.c.
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
---
fs/libfs.c | 226 +++++++++++++++++++++++++++++++++++++++++++++
include/linux/fs.h | 18 ++++
2 files changed, 244 insertions(+)
diff --git a/fs/libfs.c b/fs/libfs.c
index 5c76e4c648dc..90b0c221d9a2 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -751,6 +751,232 @@ struct inode *simple_alloc_anon_inode(struct simple_fs *fs)
}
EXPORT_SYMBOL(simple_alloc_anon_inode);
+static struct dentry *failed_creating(struct simple_fs *fs, struct dentry *dentry)
+{
+ inode_unlock(d_inode(dentry->d_parent));
+ dput(dentry);
+ simple_release_fs(fs);
+ return ERR_PTR(-ENOMEM);
+}
+
+/**
+ * simplefs_create_dentry - creates a new dentry and inode
+ * @fs: a pointer to a struct simple_fs containing the reference counter
+ * and vfs_mount pointer
+ * @type: the fs type
+ * @name: dentry name
+ * @parent: parent dentry. If this parameter is NULL,
+ * then the dentry will be created in the root of the
+ * filesystem.
+ * @inode: pointer that will contain a newly created inode
+ *
+ * This function returns a new dentry, or NULL on error. On success, a
+ * new inode is created and stored into @inode. Also note that the inode
+ * for the parent directory is locked by simplefs_create_dentry(),
+ * and will be unlocked by simple_finish_dentry().
+ **/
+struct dentry *simplefs_create_dentry(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, struct dentry *parent,
+ struct inode **inode)
+{
+ struct dentry *dentry;
+ int error;
+
+ pr_debug("creating file '%s'\n", name);
+
+ if (IS_ERR(parent))
+ return parent;
+
+ error = simple_pin_fs(fs, type);
+ if (error) {
+ pr_err("Unable to pin filesystem for file '%s'\n", name);
+ return ERR_PTR(error);
+ }
+
+ /* If the parent is not specified, we create it in the root.
+ * We need the root dentry to do this, which is in the super
+ * block. A pointer to that is in the struct vfsmount that we
+ * have around.
+ */
+ if (!parent)
+ parent = fs->mount->mnt_root;
+
+ inode_lock(d_inode(parent));
+ dentry = lookup_one_len(name, parent, strlen(name));
+ if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
+ if (d_is_dir(dentry))
+ pr_err("Directory '%s' with parent '%s' already present!\n",
+ name, parent->d_name.name);
+ else
+ pr_err("File '%s' in directory '%s' already present!\n",
+ name, parent->d_name.name);
+ dput(dentry);
+ dentry = ERR_PTR(-EEXIST);
+ }
+
+ if (IS_ERR(dentry)) {
+ inode_unlock(d_inode(parent));
+ simple_release_fs(fs);
+ }
+
+
+ if (IS_ERR(dentry))
+ return dentry;
+
+ *inode = new_inode_current_time(fs->mount->mnt_sb);
+ if (unlikely(!(*inode))) {
+ pr_err("out of free inodes, can not create file '%s'\n",
+ name);
+ return failed_creating(fs, dentry);
+ }
+
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_create_dentry);
+
+/**
+ * simplefs_create_file - creates a new file dentry and inode
+ * @fs: a pointer to a struct simple_fs containing the reference counter
+ * and vfs_mount pointer
+ * @type: the fs type
+ * @name: file name
+ * @mode: file mode
+ * @parent: parent dentry. If this parameter is NULL,
+ * then the file will be created in the root of the
+ * filesystem.
+ * @data: what will the file contain
+ * @inode: pointer that will contain a newly created inode
+ *
+ * This function returns a new dentry, or NULL on error. On success, a
+ * new inode is created and stored into @inode. Also note that the inode
+ * for the parent directory is locked by simplefs_create_dentry(),
+ * and will be unlocked by simple_finish_dentry().
+ **/
+struct dentry *simplefs_create_file(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, umode_t mode,
+ struct dentry *parent, void *data,
+ struct inode **inode)
+{
+ struct dentry *dentry;
+
+ WARN_ON((mode & S_IFMT) && !S_ISREG(mode));
+ mode |= S_IFREG;
+
+ dentry = simplefs_create_dentry(fs, type, name, parent, inode);
+
+ if (IS_ERR(dentry))
+ return dentry;
+
+ (*inode)->i_mode = mode;
+ (*inode)->i_private = data;
+
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_create_file);
+
+
+/**
+ * simplefs_finish_dentry- complete creation of a new dentry
+ * @dentry: the dentry being created
+ * @inode: the inode associated to the dentry
+ *
+ * This function completes the creation of a dentry.
+ * This includes associating @inode with the dentry, ensuring the link
+ * counts are consistent and informing fsnotify.
+ **/
+struct dentry *simplefs_finish_dentry(struct dentry *dentry, struct inode *inode)
+{
+ d_instantiate(dentry, inode);
+ if (S_ISDIR(inode->i_mode)) {
+ inc_nlink(d_inode(dentry->d_parent));
+ fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
+ } else {
+ fsnotify_create(d_inode(dentry->d_parent), dentry);
+ }
+ inode_unlock(d_inode(dentry->d_parent));
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_finish_dentry);
+
+/**
+ * simplefs_create_dir - creates a new directory dentry and inode
+ * @fs: a pointer to a struct simple_fs containing the reference counter
+ * and vfs_mount pointer
+ * @type: the fs type
+ * @name: dir name
+ * @mode: dir mode
+ * @parent: parent dentry. If this parameter is NULL,
+ * then the directory will be created in the root of the
+ * filesystem.
+ * @inode: pointer that will contain a newly created inode
+ *
+ * This function returns a new dentry, or NULL on error. On success, a
+ * new inode is created and stored into @inode. Also note that the inode
+ * for the parent directory is locked by simplefs_create_dentry(),
+ * and will be unlocked by simple_finish_dentry().
+ **/
+struct dentry *simplefs_create_dir(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, umode_t mode, struct dentry *parent,
+ struct inode **inode)
+{
+ struct dentry *dentry;
+
+ WARN_ON((mode & S_IFMT) && !S_ISDIR(mode));
+ mode |= S_IFDIR;
+
+ dentry = simplefs_create_dentry(fs, type, name, parent, inode);
+ if (IS_ERR(dentry))
+ return dentry;
+
+ (*inode)->i_mode = mode;
+ (*inode)->i_op = &simple_dir_inode_operations;
+ (*inode)->i_fop = &simple_dir_operations;
+
+ /* directory inodes start off with i_nlink == 2 (for "." entry) */
+ inc_nlink(*inode);
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_create_dir);
+
+/**
+ * simplefs_create_symlink - creates a new symlink dentry and inode
+ * @fs: a pointer to a struct simple_fs containing the reference counter
+ * and vfs_mount pointer
+ * @type: the fs type
+ * @name: symlink name
+ * @parent: parent dentry. If this parameter is NULL,
+ * then the symbolic link will be created in the root of the
+ * filesystem.
+ * @inode: pointer that will contain a newly created inode
+ *
+ * This function returns a new dentry, or NULL on error. On success, a
+ * new inode is created and stored into @inode. Also note that the inode
+ * for the parent directory is locked by simplefs_create_dentry(),
+ * and will be unlocked by simple_finish_dentry().
+ **/
+struct dentry *simplefs_create_symlink(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, struct dentry *parent,
+ const char *target, struct inode **inode)
+{
+ struct dentry *dentry;
+ char *link = kstrdup(target, GFP_KERNEL);
+
+ if (!link)
+ return ERR_PTR(-ENOMEM);
+
+ dentry = simplefs_create_dentry(fs, type, name, parent, inode);
+ if (IS_ERR(dentry)) {
+ kfree_link(link);
+ return dentry;
+ }
+
+ (*inode)->i_mode = S_IFLNK | S_IRWXUGO;
+ (*inode)->i_link = link;
+ (*inode)->i_op = &simple_symlink_inode_operations;
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_create_symlink);
+
/**
* simple_read_from_buffer - copy data from the buffer to user space
* @to: the user space buffer to read to
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 5e93de72118b..0569540fbe61 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3375,6 +3375,24 @@ extern void simple_release_fs(struct simple_fs *);
extern struct inode *simple_alloc_anon_inode(struct simple_fs *fs);
+extern struct dentry *simplefs_create_dentry(struct simple_fs *fs,
+ struct file_system_type *type,
+ const char *name, struct dentry *parent,
+ struct inode **inode);
+struct dentry *simplefs_finish_dentry(struct dentry *dentry, struct inode *inode);
+
+extern struct dentry *simplefs_create_file(struct simple_fs *fs,
+ struct file_system_type *type,
+ const char *name, umode_t mode,
+ struct dentry *parent, void *data,
+ struct inode **inode);
+extern struct dentry *simplefs_create_dir(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, umode_t mode, struct dentry *parent,
+ struct inode **inode);
+extern struct dentry *simplefs_create_symlink(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, struct dentry *parent,
+ const char *target, struct inode **inode);
+
extern ssize_t simple_read_from_buffer(void __user *to, size_t count,
loff_t *ppos, const void *from, size_t available);
extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
--
2.25.2
WARNING: multiple messages have this Message-ID (diff)
From: Emanuele Giuseppe Esposito <eesposit@redhat.com>
To: linux-fsdevel@vger.kernel.org
Cc: "Rafael J. Wysocki" <rafael@kernel.org>,
David Airlie <airlied@linux.ie>,
dri-devel@lists.freedesktop.org, Christoph Hellwig <hch@lst.de>,
Andrew Donnellan <ajd@linux.ibm.com>,
Emanuele Giuseppe Esposito <eesposit@redhat.com>,
linux-scsi@vger.kernel.org, James Morris <jmorris@namei.org>,
"Serge E. Hallyn" <serge@hallyn.com>,
Arnd Bergmann <arnd@arndb.de>,
"James E.J. Bottomley" <jejb@linux.ibm.com>,
"Manoj N. Kumar" <manoj@linux.ibm.com>,
Alexander Viro <viro@zeniv.linux.org.uk>,
"Matthew R. Ochs" <mrochs@linux.ibm.com>,
Uma Krishnan <ukrishn@linux.ibm.com>,
John Johansen <john.johansen@canonical.com>,
"Martin K. Petersen" <martin.petersen@oracle.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
linux-kernel@vger.kernel.org,
linux-security-module@vger.kernel.org,
Thomas Zimmermann <tzimmermann@suse.de>,
Frederic Barrat <fbarrat@linux.ibm.com>,
Paolo Bonzini <pbonzini@redhat.com>,
linuxppc-dev@lists.ozlabs.org, Joel Becker <jlbec@evilplan.org>
Subject: [PATCH v2 5/7] libfs: add file creation functions
Date: Tue, 21 Apr 2020 15:57:39 +0200 [thread overview]
Message-ID: <20200421135741.30657-3-eesposit@redhat.com> (raw)
In-Reply-To: <20200421135119.30007-1-eesposit@redhat.com>
A bunch of code is duplicated between debugfs and tracefs, unify it to the
libfs library.
The code is very similar, except that dentry and inode creation are unified
into a single function (unlike start_creating in debugfs and tracefs, which
only takes care of dentries). This adds an output parameter to the
creation functions, but pushes all error recovery into fs/libfs.c.
Signed-off-by: Emanuele Giuseppe Esposito <eesposit@redhat.com>
---
fs/libfs.c | 226 +++++++++++++++++++++++++++++++++++++++++++++
include/linux/fs.h | 18 ++++
2 files changed, 244 insertions(+)
diff --git a/fs/libfs.c b/fs/libfs.c
index 5c76e4c648dc..90b0c221d9a2 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -751,6 +751,232 @@ struct inode *simple_alloc_anon_inode(struct simple_fs *fs)
}
EXPORT_SYMBOL(simple_alloc_anon_inode);
+static struct dentry *failed_creating(struct simple_fs *fs, struct dentry *dentry)
+{
+ inode_unlock(d_inode(dentry->d_parent));
+ dput(dentry);
+ simple_release_fs(fs);
+ return ERR_PTR(-ENOMEM);
+}
+
+/**
+ * simplefs_create_dentry - creates a new dentry and inode
+ * @fs: a pointer to a struct simple_fs containing the reference counter
+ * and vfs_mount pointer
+ * @type: the fs type
+ * @name: dentry name
+ * @parent: parent dentry. If this parameter is NULL,
+ * then the dentry will be created in the root of the
+ * filesystem.
+ * @inode: pointer that will contain a newly created inode
+ *
+ * This function returns a new dentry, or NULL on error. On success, a
+ * new inode is created and stored into @inode. Also note that the inode
+ * for the parent directory is locked by simplefs_create_dentry(),
+ * and will be unlocked by simple_finish_dentry().
+ **/
+struct dentry *simplefs_create_dentry(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, struct dentry *parent,
+ struct inode **inode)
+{
+ struct dentry *dentry;
+ int error;
+
+ pr_debug("creating file '%s'\n", name);
+
+ if (IS_ERR(parent))
+ return parent;
+
+ error = simple_pin_fs(fs, type);
+ if (error) {
+ pr_err("Unable to pin filesystem for file '%s'\n", name);
+ return ERR_PTR(error);
+ }
+
+ /* If the parent is not specified, we create it in the root.
+ * We need the root dentry to do this, which is in the super
+ * block. A pointer to that is in the struct vfsmount that we
+ * have around.
+ */
+ if (!parent)
+ parent = fs->mount->mnt_root;
+
+ inode_lock(d_inode(parent));
+ dentry = lookup_one_len(name, parent, strlen(name));
+ if (!IS_ERR(dentry) && d_really_is_positive(dentry)) {
+ if (d_is_dir(dentry))
+ pr_err("Directory '%s' with parent '%s' already present!\n",
+ name, parent->d_name.name);
+ else
+ pr_err("File '%s' in directory '%s' already present!\n",
+ name, parent->d_name.name);
+ dput(dentry);
+ dentry = ERR_PTR(-EEXIST);
+ }
+
+ if (IS_ERR(dentry)) {
+ inode_unlock(d_inode(parent));
+ simple_release_fs(fs);
+ }
+
+
+ if (IS_ERR(dentry))
+ return dentry;
+
+ *inode = new_inode_current_time(fs->mount->mnt_sb);
+ if (unlikely(!(*inode))) {
+ pr_err("out of free inodes, can not create file '%s'\n",
+ name);
+ return failed_creating(fs, dentry);
+ }
+
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_create_dentry);
+
+/**
+ * simplefs_create_file - creates a new file dentry and inode
+ * @fs: a pointer to a struct simple_fs containing the reference counter
+ * and vfs_mount pointer
+ * @type: the fs type
+ * @name: file name
+ * @mode: file mode
+ * @parent: parent dentry. If this parameter is NULL,
+ * then the file will be created in the root of the
+ * filesystem.
+ * @data: what will the file contain
+ * @inode: pointer that will contain a newly created inode
+ *
+ * This function returns a new dentry, or NULL on error. On success, a
+ * new inode is created and stored into @inode. Also note that the inode
+ * for the parent directory is locked by simplefs_create_dentry(),
+ * and will be unlocked by simple_finish_dentry().
+ **/
+struct dentry *simplefs_create_file(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, umode_t mode,
+ struct dentry *parent, void *data,
+ struct inode **inode)
+{
+ struct dentry *dentry;
+
+ WARN_ON((mode & S_IFMT) && !S_ISREG(mode));
+ mode |= S_IFREG;
+
+ dentry = simplefs_create_dentry(fs, type, name, parent, inode);
+
+ if (IS_ERR(dentry))
+ return dentry;
+
+ (*inode)->i_mode = mode;
+ (*inode)->i_private = data;
+
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_create_file);
+
+
+/**
+ * simplefs_finish_dentry- complete creation of a new dentry
+ * @dentry: the dentry being created
+ * @inode: the inode associated to the dentry
+ *
+ * This function completes the creation of a dentry.
+ * This includes associating @inode with the dentry, ensuring the link
+ * counts are consistent and informing fsnotify.
+ **/
+struct dentry *simplefs_finish_dentry(struct dentry *dentry, struct inode *inode)
+{
+ d_instantiate(dentry, inode);
+ if (S_ISDIR(inode->i_mode)) {
+ inc_nlink(d_inode(dentry->d_parent));
+ fsnotify_mkdir(d_inode(dentry->d_parent), dentry);
+ } else {
+ fsnotify_create(d_inode(dentry->d_parent), dentry);
+ }
+ inode_unlock(d_inode(dentry->d_parent));
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_finish_dentry);
+
+/**
+ * simplefs_create_dir - creates a new directory dentry and inode
+ * @fs: a pointer to a struct simple_fs containing the reference counter
+ * and vfs_mount pointer
+ * @type: the fs type
+ * @name: dir name
+ * @mode: dir mode
+ * @parent: parent dentry. If this parameter is NULL,
+ * then the directory will be created in the root of the
+ * filesystem.
+ * @inode: pointer that will contain a newly created inode
+ *
+ * This function returns a new dentry, or NULL on error. On success, a
+ * new inode is created and stored into @inode. Also note that the inode
+ * for the parent directory is locked by simplefs_create_dentry(),
+ * and will be unlocked by simple_finish_dentry().
+ **/
+struct dentry *simplefs_create_dir(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, umode_t mode, struct dentry *parent,
+ struct inode **inode)
+{
+ struct dentry *dentry;
+
+ WARN_ON((mode & S_IFMT) && !S_ISDIR(mode));
+ mode |= S_IFDIR;
+
+ dentry = simplefs_create_dentry(fs, type, name, parent, inode);
+ if (IS_ERR(dentry))
+ return dentry;
+
+ (*inode)->i_mode = mode;
+ (*inode)->i_op = &simple_dir_inode_operations;
+ (*inode)->i_fop = &simple_dir_operations;
+
+ /* directory inodes start off with i_nlink == 2 (for "." entry) */
+ inc_nlink(*inode);
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_create_dir);
+
+/**
+ * simplefs_create_symlink - creates a new symlink dentry and inode
+ * @fs: a pointer to a struct simple_fs containing the reference counter
+ * and vfs_mount pointer
+ * @type: the fs type
+ * @name: symlink name
+ * @parent: parent dentry. If this parameter is NULL,
+ * then the symbolic link will be created in the root of the
+ * filesystem.
+ * @inode: pointer that will contain a newly created inode
+ *
+ * This function returns a new dentry, or NULL on error. On success, a
+ * new inode is created and stored into @inode. Also note that the inode
+ * for the parent directory is locked by simplefs_create_dentry(),
+ * and will be unlocked by simple_finish_dentry().
+ **/
+struct dentry *simplefs_create_symlink(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, struct dentry *parent,
+ const char *target, struct inode **inode)
+{
+ struct dentry *dentry;
+ char *link = kstrdup(target, GFP_KERNEL);
+
+ if (!link)
+ return ERR_PTR(-ENOMEM);
+
+ dentry = simplefs_create_dentry(fs, type, name, parent, inode);
+ if (IS_ERR(dentry)) {
+ kfree_link(link);
+ return dentry;
+ }
+
+ (*inode)->i_mode = S_IFLNK | S_IRWXUGO;
+ (*inode)->i_link = link;
+ (*inode)->i_op = &simple_symlink_inode_operations;
+ return dentry;
+}
+EXPORT_SYMBOL(simplefs_create_symlink);
+
/**
* simple_read_from_buffer - copy data from the buffer to user space
* @to: the user space buffer to read to
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 5e93de72118b..0569540fbe61 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -3375,6 +3375,24 @@ extern void simple_release_fs(struct simple_fs *);
extern struct inode *simple_alloc_anon_inode(struct simple_fs *fs);
+extern struct dentry *simplefs_create_dentry(struct simple_fs *fs,
+ struct file_system_type *type,
+ const char *name, struct dentry *parent,
+ struct inode **inode);
+struct dentry *simplefs_finish_dentry(struct dentry *dentry, struct inode *inode);
+
+extern struct dentry *simplefs_create_file(struct simple_fs *fs,
+ struct file_system_type *type,
+ const char *name, umode_t mode,
+ struct dentry *parent, void *data,
+ struct inode **inode);
+extern struct dentry *simplefs_create_dir(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, umode_t mode, struct dentry *parent,
+ struct inode **inode);
+extern struct dentry *simplefs_create_symlink(struct simple_fs *fs, struct file_system_type *type,
+ const char *name, struct dentry *parent,
+ const char *target, struct inode **inode);
+
extern ssize_t simple_read_from_buffer(void __user *to, size_t count,
loff_t *ppos, const void *from, size_t available);
extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
--
2.25.2
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel
next prev parent reply other threads:[~2020-04-21 13:58 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-21 13:51 [PATCH v2 0/7] libfs: group and simplify linux fs code Emanuele Giuseppe Esposito
2020-04-21 13:51 ` Emanuele Giuseppe Esposito
2020-04-21 13:51 ` Emanuele Giuseppe Esposito
2020-04-21 13:51 ` [PATCH v2 1/7] apparmor: just use vfs_kern_mount to make .null Emanuele Giuseppe Esposito
2020-04-21 13:51 ` Emanuele Giuseppe Esposito
2020-04-21 13:51 ` Emanuele Giuseppe Esposito
2020-04-21 13:51 ` [PATCH v2 2/7] libfs: wrap simple_pin_fs/simple_release_fs arguments in a struct Emanuele Giuseppe Esposito
2020-04-21 13:51 ` Emanuele Giuseppe Esposito
2020-04-21 13:51 ` Emanuele Giuseppe Esposito
2020-04-22 16:50 ` James Morris
2020-04-22 16:50 ` James Morris
2020-04-22 16:50 ` James Morris
2020-04-21 13:57 ` [PATCH v2 3/7] libfs: introduce new_inode_current_time Emanuele Giuseppe Esposito
2020-04-21 13:57 ` Emanuele Giuseppe Esposito
2020-04-21 13:57 ` Emanuele Giuseppe Esposito
2020-04-21 13:57 ` [PATCH v2 4/7] libfs: add alloc_anon_inode wrapper Emanuele Giuseppe Esposito
2020-04-21 13:57 ` Emanuele Giuseppe Esposito
2020-04-21 13:57 ` Emanuele Giuseppe Esposito
2020-04-21 13:57 ` Emanuele Giuseppe Esposito [this message]
2020-04-21 13:57 ` [PATCH v2 5/7] libfs: add file creation functions Emanuele Giuseppe Esposito
2020-04-21 13:57 ` Emanuele Giuseppe Esposito
2020-04-21 13:57 ` [PATCH v2 6/7] debugfs: switch to simplefs inode creation API Emanuele Giuseppe Esposito
2020-04-21 13:57 ` Emanuele Giuseppe Esposito
2020-04-21 13:57 ` Emanuele Giuseppe Esposito
2020-04-27 15:15 ` Paolo Bonzini
2020-04-27 15:15 ` Paolo Bonzini
2020-04-27 15:15 ` Paolo Bonzini
2020-04-21 13:57 ` [PATCH v2 7/7] tracefs: " Emanuele Giuseppe Esposito
2020-04-21 13:57 ` Emanuele Giuseppe Esposito
2020-04-21 13:57 ` Emanuele Giuseppe Esposito
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200421135741.30657-3-eesposit@redhat.com \
--to=eesposit@redhat.com \
--cc=airlied@linux.ie \
--cc=ajd@linux.ibm.com \
--cc=arnd@arndb.de \
--cc=daniel@ffwll.ch \
--cc=dri-devel@lists.freedesktop.org \
--cc=fbarrat@linux.ibm.com \
--cc=gregkh@linuxfoundation.org \
--cc=hch@lst.de \
--cc=jejb@linux.ibm.com \
--cc=jlbec@evilplan.org \
--cc=jmorris@namei.org \
--cc=john.johansen@canonical.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=maarten.lankhorst@linux.intel.com \
--cc=manoj@linux.ibm.com \
--cc=martin.petersen@oracle.com \
--cc=mripard@kernel.org \
--cc=mrochs@linux.ibm.com \
--cc=pbonzini@redhat.com \
--cc=rafael@kernel.org \
--cc=serge@hallyn.com \
--cc=tzimmermann@suse.de \
--cc=ukrishn@linux.ibm.com \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.