]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - drivers/base/devtmpfs.c
devtmpfs: support !CONFIG_TMPFS
[linux-2.6.git] / drivers / base / devtmpfs.c
index 880a203b6688c752fffa5571bb584522f779226d..af0600143d1c506efb32d3d7f5038cf1af02aa7e 100644 (file)
 #include <linux/namei.h>
 #include <linux/fs.h>
 #include <linux/shmem_fs.h>
+#include <linux/ramfs.h>
 #include <linux/cred.h>
 #include <linux/sched.h>
 #include <linux/init_task.h>
+#include <linux/slab.h>
 
 static struct vfsmount *dev_mnt;
 
@@ -32,7 +34,7 @@ static int dev_mount = 1;
 static int dev_mount;
 #endif
 
-static rwlock_t dirlock;
+static DEFINE_MUTEX(dirlock);
 
 static int __init mount_param(char *str)
 {
@@ -44,7 +46,11 @@ __setup("devtmpfs.mount=", mount_param);
 static int dev_get_sb(struct file_system_type *fs_type, int flags,
                      const char *dev_name, void *data, struct vfsmount *mnt)
 {
+#ifdef CONFIG_TMPFS
        return get_sb_single(fs_type, flags, data, shmem_fill_super, mnt);
+#else
+       return get_sb_single(fs_type, flags, data, ramfs_fill_super, mnt);
+#endif
 }
 
 static struct file_system_type dev_fs_type = {
@@ -76,44 +82,35 @@ static int dev_mkdir(const char *name, mode_t mode)
        dentry = lookup_create(&nd, 1);
        if (!IS_ERR(dentry)) {
                err = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
+               if (!err)
+                       /* mark as kernel-created inode */
+                       dentry->d_inode->i_private = &dev_mnt;
                dput(dentry);
        } else {
                err = PTR_ERR(dentry);
        }
-       mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
 
+       mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
        path_put(&nd.path);
        return err;
 }
 
 static int create_path(const char *nodepath)
 {
-       struct nameidata nd;
-       int err = 0;
+       int err;
 
-       read_lock(&dirlock);
-       err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
-                             nodepath, LOOKUP_PARENT, &nd);
-       if (err == 0) {
-               struct dentry *dentry;
-
-               /* create directory right away */
-               dentry = lookup_create(&nd, 1);
-               if (!IS_ERR(dentry)) {
-                       err = vfs_mkdir(nd.path.dentry->d_inode,
-                                       dentry, 0755);
-                       dput(dentry);
-               }
-               mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
-               path_put(&nd.path);
-       } else if (err == -ENOENT) {
+       mutex_lock(&dirlock);
+       err = dev_mkdir(nodepath, 0755);
+       if (err == -ENOENT) {
                char *path;
                char *s;
 
                /* parent directories do not exist, create them */
                path = kstrdup(nodepath, GFP_KERNEL);
-               if (!path)
-                       return -ENOMEM;
+               if (!path) {
+                       err = -ENOMEM;
+                       goto out;
+               }
                s = path;
                for (;;) {
                        s = strchr(s, '/');
@@ -128,8 +125,8 @@ static int create_path(const char *nodepath)
                }
                kfree(path);
        }
-       read_unlock(&dirlock);
-
+out:
+       mutex_unlock(&dirlock);
        return err;
 }
 
@@ -213,16 +210,21 @@ static int dev_rmdir(const char *name)
        mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
        dentry = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len);
        if (!IS_ERR(dentry)) {
-               if (dentry->d_inode)
-                       err = vfs_rmdir(nd.path.dentry->d_inode, dentry);
-               else
+               if (dentry->d_inode) {
+                       if (dentry->d_inode->i_private == &dev_mnt)
+                               err = vfs_rmdir(nd.path.dentry->d_inode,
+                                               dentry);
+                       else
+                               err = -EPERM;
+               } else {
                        err = -ENOENT;
+               }
                dput(dentry);
        } else {
                err = PTR_ERR(dentry);
        }
-       mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
 
+       mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
        path_put(&nd.path);
        return err;
 }
@@ -236,7 +238,7 @@ static int delete_path(const char *nodepath)
        if (!path)
                return -ENOMEM;
 
-       write_lock(&dirlock);
+       mutex_lock(&dirlock);
        for (;;) {
                char *base;
 
@@ -248,7 +250,7 @@ static int delete_path(const char *nodepath)
                if (err)
                        break;
        }
-       write_unlock(&dirlock);
+       mutex_unlock(&dirlock);
 
        kfree(path);
        return err;
@@ -305,6 +307,19 @@ int devtmpfs_delete_node(struct device *dev)
                if (dentry->d_inode) {
                        err = vfs_getattr(nd.path.mnt, dentry, &stat);
                        if (!err && dev_mynode(dev, dentry->d_inode, &stat)) {
+                               struct iattr newattrs;
+                               /*
+                                * before unlinking this node, reset permissions
+                                * of possible references like hardlinks
+                                */
+                               newattrs.ia_uid = 0;
+                               newattrs.ia_gid = 0;
+                               newattrs.ia_mode = stat.mode & ~0777;
+                               newattrs.ia_valid =
+                                       ATTR_UID|ATTR_GID|ATTR_MODE;
+                               mutex_lock(&dentry->d_inode->i_mutex);
+                               notify_change(dentry, &newattrs);
+                               mutex_unlock(&dentry->d_inode->i_mutex);
                                err = vfs_unlink(nd.path.dentry->d_inode,
                                                 dentry);
                                if (!err || err == -ENOENT)
@@ -358,8 +373,7 @@ int __init devtmpfs_init(void)
 {
        int err;
        struct vfsmount *mnt;
-
-       rwlock_init(&dirlock);
+       char options[] = "mode=0755";
 
        err = register_filesystem(&dev_fs_type);
        if (err) {
@@ -368,7 +382,7 @@ int __init devtmpfs_init(void)
                return err;
        }
 
-       mnt = kern_mount(&dev_fs_type);
+       mnt = kern_mount_data(&dev_fs_type, options);
        if (IS_ERR(mnt)) {
                err = PTR_ERR(mnt);
                printk(KERN_ERR "devtmpfs: unable to create devtmpfs %i\n", err);