include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / drivers / base / devtmpfs.c
index 48526b9..057cf11 100644 (file)
@@ -23,6 +23,7 @@
 #include <linux/cred.h>
 #include <linux/sched.h>
 #include <linux/init_task.h>
+#include <linux/slab.h>
 
 static struct vfsmount *dev_mnt;
 
@@ -32,6 +33,8 @@ static int dev_mount = 1;
 static int dev_mount;
 #endif
 
+static DEFINE_MUTEX(dirlock);
+
 static int __init mount_param(char *str)
 {
        dev_mount = simple_strtoul(str, NULL, 0);
@@ -74,47 +77,37 @@ 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)
 {
-       char *path;
-       struct nameidata nd;
-       int err = 0;
-
-       path = kstrdup(nodepath, GFP_KERNEL);
-       if (!path)
-               return -ENOMEM;
-
-       err = vfs_path_lookup(dev_mnt->mnt_root, dev_mnt,
-                             path, 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);
+       int err;
 
-               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) {
+                       err = -ENOMEM;
+                       goto out;
+               }
                s = path;
-               while (1) {
+               for (;;) {
                        s = strchr(s, '/');
                        if (!s)
                                break;
@@ -125,9 +118,10 @@ static int create_path(const char *nodepath)
                        s[0] = '/';
                        s++;
                }
+               kfree(path);
        }
-
-       kfree(path);
+out:
+       mutex_unlock(&dirlock);
        return err;
 }
 
@@ -211,16 +205,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;
 }
@@ -234,7 +233,8 @@ static int delete_path(const char *nodepath)
        if (!path)
                return -ENOMEM;
 
-       while (1) {
+       mutex_lock(&dirlock);
+       for (;;) {
                char *base;
 
                base = strrchr(path, '/');
@@ -245,6 +245,7 @@ static int delete_path(const char *nodepath)
                if (err)
                        break;
        }
+       mutex_unlock(&dirlock);
 
        kfree(path);
        return err;
@@ -301,6 +302,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)
@@ -328,9 +342,8 @@ out:
  * If configured, or requested by the commandline, devtmpfs will be
  * auto-mounted after the kernel mounted the root filesystem.
  */
-int devtmpfs_mount(const char *mountpoint)
+int devtmpfs_mount(const char *mntdir)
 {
-       struct path path;
        int err;
 
        if (!dev_mount)
@@ -339,15 +352,11 @@ int devtmpfs_mount(const char *mountpoint)
        if (!dev_mnt)
                return 0;
 
-       err = kern_path(mountpoint, LOOKUP_FOLLOW, &path);
-       if (err)
-               return err;
-       err = do_add_mount(dev_mnt, &path, 0, NULL);
+       err = sys_mount("devtmpfs", (char *)mntdir, "devtmpfs", MS_SILENT, NULL);
        if (err)
                printk(KERN_INFO "devtmpfs: error mounting %i\n", err);
        else
                printk(KERN_INFO "devtmpfs: mounted\n");
-       path_put(&path);
        return err;
 }
 
@@ -359,6 +368,7 @@ int __init devtmpfs_init(void)
 {
        int err;
        struct vfsmount *mnt;
+       char options[] = "mode=0755";
 
        err = register_filesystem(&dev_fs_type);
        if (err) {
@@ -367,7 +377,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);