fatfs: ratelimit corruption report
[safe/jmp/linux-2.6] / fs / sysfs / symlink.c
index 817f596..f71246b 100644 (file)
  */
 
 #include <linux/fs.h>
+#include <linux/gfp.h>
 #include <linux/mount.h>
 #include <linux/module.h>
 #include <linux/kobject.h>
 #include <linux/namei.h>
 #include <linux/mutex.h>
+#include <linux/security.h>
 
 #include "sysfs.h"
 
-/**
- *     sysfs_create_link - create symlink between two objects.
- *     @kobj:  object whose directory we're creating the link in.
- *     @target:        object we're pointing to.
- *     @name:          name of the symlink.
- */
-int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char * name)
+static int sysfs_do_create_link(struct kobject *kobj, struct kobject *target,
+                               const char *name, int warn)
 {
        struct sysfs_dirent *parent_sd = NULL;
        struct sysfs_dirent *target_sd = NULL;
@@ -61,11 +58,16 @@ int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char
        if (!sd)
                goto out_put;
 
+       if (sysfs_ns_type(parent_sd))
+               sd->s_ns = target->ktype->namespace(target);
        sd->s_symlink.target_sd = target_sd;
        target_sd = NULL;       /* reference is now owned by the symlink */
 
        sysfs_addrm_start(&acxt, parent_sd);
-       error = sysfs_add_one(&acxt, sd);
+       if (warn)
+               error = sysfs_add_one(&acxt, sd);
+       else
+               error = __sysfs_add_one(&acxt, sd);
        sysfs_addrm_finish(&acxt);
 
        if (error)
@@ -80,6 +82,53 @@ int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char
 }
 
 /**
+ *     sysfs_create_link - create symlink between two objects.
+ *     @kobj:  object whose directory we're creating the link in.
+ *     @target:        object we're pointing to.
+ *     @name:          name of the symlink.
+ */
+int sysfs_create_link(struct kobject *kobj, struct kobject *target,
+                     const char *name)
+{
+       return sysfs_do_create_link(kobj, target, name, 1);
+}
+
+/**
+ *     sysfs_create_link_nowarn - create symlink between two objects.
+ *     @kobj:  object whose directory we're creating the link in.
+ *     @target:        object we're pointing to.
+ *     @name:          name of the symlink.
+ *
+ *     This function does the same as sysf_create_link(), but it
+ *     doesn't warn if the link already exists.
+ */
+int sysfs_create_link_nowarn(struct kobject *kobj, struct kobject *target,
+                            const char *name)
+{
+       return sysfs_do_create_link(kobj, target, name, 0);
+}
+
+/**
+ *     sysfs_delete_link - remove symlink in object's directory.
+ *     @kobj:  object we're acting for.
+ *     @targ:  object we're pointing to.
+ *     @name:  name of the symlink to remove.
+ *
+ *     Unlike sysfs_remove_link sysfs_delete_link has enough information
+ *     to successfully delete symlinks in tagged directories.
+ */
+void sysfs_delete_link(struct kobject *kobj, struct kobject *targ,
+                       const char *name)
+{
+       const void *ns = NULL;
+       spin_lock(&sysfs_assoc_lock);
+       if (targ->sd)
+               ns = targ->sd->s_ns;
+       spin_unlock(&sysfs_assoc_lock);
+       sysfs_hash_and_remove(kobj->sd, ns, name);
+}
+
+/**
  *     sysfs_remove_link - remove symlink in object's directory.
  *     @kobj:  object we're acting for.
  *     @name:  name of the symlink to remove.
@@ -94,7 +143,52 @@ void sysfs_remove_link(struct kobject * kobj, const char * name)
        else
                parent_sd = kobj->sd;
 
-       sysfs_hash_and_remove(parent_sd, name);
+       sysfs_hash_and_remove(parent_sd, NULL, name);
+}
+
+/**
+ *     sysfs_rename_link - rename symlink in object's directory.
+ *     @kobj:  object we're acting for.
+ *     @targ:  object we're pointing to.
+ *     @old:   previous name of the symlink.
+ *     @new:   new name of the symlink.
+ *
+ *     A helper function for the common rename symlink idiom.
+ */
+int sysfs_rename_link(struct kobject *kobj, struct kobject *targ,
+                       const char *old, const char *new)
+{
+       struct sysfs_dirent *parent_sd, *sd = NULL;
+       const void *old_ns = NULL, *new_ns = NULL;
+       int result;
+
+       if (!kobj)
+               parent_sd = &sysfs_root;
+       else
+               parent_sd = kobj->sd;
+
+       if (targ->sd)
+               old_ns = targ->sd->s_ns;
+
+       result = -ENOENT;
+       sd = sysfs_get_dirent(parent_sd, old_ns, old);
+       if (!sd)
+               goto out;
+
+       result = -EINVAL;
+       if (sysfs_type(sd) != SYSFS_KOBJ_LINK)
+               goto out;
+       if (sd->s_symlink.target_sd->s_dir.kobj != targ)
+               goto out;
+
+       if (sysfs_ns_type(parent_sd))
+               new_ns = targ->ktype->namespace(targ);
+
+       result = sysfs_rename(sd, parent_sd, new_ns, new);
+
+out:
+       sysfs_put(sd);
+       return result;
 }
 
 static int sysfs_get_target_path(struct sysfs_dirent *parent_sd,
@@ -167,8 +261,11 @@ static void *sysfs_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
        int error = -ENOMEM;
        unsigned long page = get_zeroed_page(GFP_KERNEL);
-       if (page)
+       if (page) {
                error = sysfs_getlink(dentry, (char *) page); 
+               if (error < 0)
+                       free_page((unsigned long)page);
+       }
        nd_set_link(nd, error ? ERR_PTR(error) : (char *)page);
        return NULL;
 }
@@ -181,11 +278,16 @@ static void sysfs_put_link(struct dentry *dentry, struct nameidata *nd, void *co
 }
 
 const struct inode_operations sysfs_symlink_inode_operations = {
-       .readlink = generic_readlink,
-       .follow_link = sysfs_follow_link,
-       .put_link = sysfs_put_link,
+       .setxattr       = sysfs_setxattr,
+       .readlink       = generic_readlink,
+       .follow_link    = sysfs_follow_link,
+       .put_link       = sysfs_put_link,
+       .setattr        = sysfs_setattr,
+       .getattr        = sysfs_getattr,
+       .permission     = sysfs_permission,
 };
 
 
 EXPORT_SYMBOL_GPL(sysfs_create_link);
 EXPORT_SYMBOL_GPL(sysfs_remove_link);
+EXPORT_SYMBOL_GPL(sysfs_rename_link);