sysfs: add sysfs_dirent->s_name
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / sysfs / symlink.c
index d2eac3ceed5f31cf9735a68ecc69dc4a6c6771ed..c72820450e7ca8b3a0a9ce7b8686a3a0b62fa1a1 100644 (file)
@@ -3,9 +3,11 @@
  */
 
 #include <linux/fs.h>
+#include <linux/mount.h>
 #include <linux/module.h>
 #include <linux/kobject.h>
 #include <linux/namei.h>
+#include <asm/semaphore.h>
 
 #include "sysfs.h"
 
@@ -47,30 +49,28 @@ static int sysfs_add_link(struct dentry * parent, const char * name, struct kobj
 {
        struct sysfs_dirent * parent_sd = parent->d_fsdata;
        struct sysfs_symlink * sl;
-       int error = 0;
+       struct sysfs_dirent * sd;
+       int error;
 
        error = -ENOMEM;
-       sl = kmalloc(sizeof(*sl), GFP_KERNEL);
+       sl = kzalloc(sizeof(*sl), GFP_KERNEL);
        if (!sl)
-               goto exit1;
-
-       sl->link_name = kmalloc(strlen(name) + 1, GFP_KERNEL);
-       if (!sl->link_name)
-               goto exit2;
+               goto err_out;
 
-       strcpy(sl->link_name, name);
        sl->target_kobj = kobject_get(target);
 
-       error = sysfs_make_dirent(parent_sd, NULL, sl, S_IFLNK|S_IRWXUGO,
-                               SYSFS_KOBJ_LINK);
-       if (!error)
-               return 0;
+       sd = sysfs_new_dirent(name, sl, S_IFLNK|S_IRWXUGO, SYSFS_KOBJ_LINK);
+       if (!sd)
+               goto err_out;
+       sysfs_attach_dirent(sd, parent_sd, NULL);
+
+       return 0;
 
-       kobject_put(target);
-       kfree(sl->link_name);
-exit2:
-       kfree(sl);
-exit1:
+ err_out:
+       if (sl) {
+               kobject_put(sl->target_kobj);
+               kfree(sl);
+       }
        return error;
 }
 
@@ -82,10 +82,19 @@ exit1:
  */
 int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
 {
-       struct dentry * dentry = kobj->dentry;
+       struct dentry *dentry = NULL;
        int error = -EEXIST;
 
-       BUG_ON(!kobj || !kobj->dentry || !name);
+       BUG_ON(!name);
+
+       if (!kobj) {
+               if (sysfs_mount && sysfs_mount->mnt_sb)
+                       dentry = sysfs_mount->mnt_sb->s_root;
+       } else
+               dentry = kobj->dentry;
+
+       if (!dentry)
+               return -EFAULT;
 
        mutex_lock(&dentry->d_inode->i_mutex);
        if (!sysfs_dirent_exist(dentry->d_fsdata, name))
@@ -170,7 +179,7 @@ static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *co
                free_page((unsigned long)page);
 }
 
-struct inode_operations sysfs_symlink_inode_operations = {
+const struct inode_operations sysfs_symlink_inode_operations = {
        .readlink = generic_readlink,
        .follow_link = sysfs_follow_link,
        .put_link = sysfs_put_link,