Commit ea015218 authored by Eric W. Biederman's avatar Eric W. Biederman

kernfs: Add support for always empty directories.

Add a new function kernfs_create_empty_dir that can be used to create
directory that can not be modified.

Update the code to use make_empty_dir_inode when reporting a
permanently empty directory to the vfs.

Update the code to not allow adding to permanently empty directories.

Cc: stable@vger.kernel.org
Signed-off-by: default avatar"Eric W. Biederman" <ebiederm@xmission.com>
parent eb6d38d5
...@@ -585,6 +585,9 @@ int kernfs_add_one(struct kernfs_node *kn) ...@@ -585,6 +585,9 @@ int kernfs_add_one(struct kernfs_node *kn)
goto out_unlock; goto out_unlock;
ret = -ENOENT; ret = -ENOENT;
if (parent->flags & KERNFS_EMPTY_DIR)
goto out_unlock;
if ((parent->flags & KERNFS_ACTIVATED) && !kernfs_active(parent)) if ((parent->flags & KERNFS_ACTIVATED) && !kernfs_active(parent))
goto out_unlock; goto out_unlock;
...@@ -776,6 +779,38 @@ struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent, ...@@ -776,6 +779,38 @@ struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
return ERR_PTR(rc); return ERR_PTR(rc);
} }
/**
* kernfs_create_empty_dir - create an always empty directory
* @parent: parent in which to create a new directory
* @name: name of the new directory
*
* Returns the created node on success, ERR_PTR() value on failure.
*/
struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
const char *name)
{
struct kernfs_node *kn;
int rc;
/* allocate */
kn = kernfs_new_node(parent, name, S_IRUGO|S_IXUGO|S_IFDIR, KERNFS_DIR);
if (!kn)
return ERR_PTR(-ENOMEM);
kn->flags |= KERNFS_EMPTY_DIR;
kn->dir.root = parent->dir.root;
kn->ns = NULL;
kn->priv = NULL;
/* link in */
rc = kernfs_add_one(kn);
if (!rc)
return kn;
kernfs_put(kn);
return ERR_PTR(rc);
}
static struct dentry *kernfs_iop_lookup(struct inode *dir, static struct dentry *kernfs_iop_lookup(struct inode *dir,
struct dentry *dentry, struct dentry *dentry,
unsigned int flags) unsigned int flags)
...@@ -1247,7 +1282,8 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, ...@@ -1247,7 +1282,8 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,
mutex_lock(&kernfs_mutex); mutex_lock(&kernfs_mutex);
error = -ENOENT; error = -ENOENT;
if (!kernfs_active(kn) || !kernfs_active(new_parent)) if (!kernfs_active(kn) || !kernfs_active(new_parent) ||
(new_parent->flags & KERNFS_EMPTY_DIR))
goto out; goto out;
error = 0; error = 0;
......
...@@ -296,6 +296,8 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode) ...@@ -296,6 +296,8 @@ static void kernfs_init_inode(struct kernfs_node *kn, struct inode *inode)
case KERNFS_DIR: case KERNFS_DIR:
inode->i_op = &kernfs_dir_iops; inode->i_op = &kernfs_dir_iops;
inode->i_fop = &kernfs_dir_fops; inode->i_fop = &kernfs_dir_fops;
if (kn->flags & KERNFS_EMPTY_DIR)
make_empty_dir_inode(inode);
break; break;
case KERNFS_FILE: case KERNFS_FILE:
inode->i_size = kn->attr.size; inode->i_size = kn->attr.size;
......
...@@ -45,6 +45,7 @@ enum kernfs_node_flag { ...@@ -45,6 +45,7 @@ enum kernfs_node_flag {
KERNFS_LOCKDEP = 0x0100, KERNFS_LOCKDEP = 0x0100,
KERNFS_SUICIDAL = 0x0400, KERNFS_SUICIDAL = 0x0400,
KERNFS_SUICIDED = 0x0800, KERNFS_SUICIDED = 0x0800,
KERNFS_EMPTY_DIR = 0x1000,
}; };
/* @flags for kernfs_create_root() */ /* @flags for kernfs_create_root() */
...@@ -285,6 +286,8 @@ void kernfs_destroy_root(struct kernfs_root *root); ...@@ -285,6 +286,8 @@ void kernfs_destroy_root(struct kernfs_root *root);
struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent, struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
const char *name, umode_t mode, const char *name, umode_t mode,
void *priv, const void *ns); void *priv, const void *ns);
struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent,
const char *name);
struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent, struct kernfs_node *__kernfs_create_file(struct kernfs_node *parent,
const char *name, const char *name,
umode_t mode, loff_t size, umode_t mode, loff_t size,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment