lkml.org 
[lkml]   [2009]   [May]   [20]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    Patch in this message
    /
    From
    Subject[PATCH 17/20] sysfs: Merge sysfs_rename_dir and sysfs_move_dir
    Date
    From: Eric W. Biederman <ebiederm@xmission.com>

    These two functions do 90% of the same work and it doesn't significantly
    obfuscate the function to allow both the parent dir and the name to change
    at the same time. So merge them together to simplify maintenance, and
    increase testing.

    Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
    ---
    fs/sysfs/dir.c | 66 +++++++++++++++++++++++--------------------------------
    1 files changed, 28 insertions(+), 38 deletions(-)

    diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c
    index 3289fd8..3ed4489 100644
    --- a/fs/sysfs/dir.c
    +++ b/fs/sysfs/dir.c
    @@ -660,30 +660,42 @@ void sysfs_remove_dir(struct kobject * kobj)
    remove_dir(sd);
    }

    -int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
    +static int sysfs_mv_dir(struct sysfs_dirent *sd,
    + struct sysfs_dirent *new_parent_sd, const char *new_name)
    {
    - struct sysfs_dirent *sd = kobj->sd;
    const char *dup_name = NULL;
    int error;

    mutex_lock(&sysfs_mutex);

    error = 0;
    - if (strcmp(sd->s_name, new_name) == 0)
    + if ((sd->s_parent == new_parent_sd) &&
    + (strcmp(sd->s_name, new_name) == 0))
    goto out; /* nothing to rename */

    error = -EEXIST;
    - if (sysfs_find_dirent(sd->s_parent, new_name))
    + if (sysfs_find_dirent(new_parent_sd, new_name))
    goto out;

    /* rename sysfs_dirent */
    - error = -ENOMEM;
    - new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
    - if (!new_name)
    - goto out;
    + if (strcmp(sd->s_name, new_name) != 0) {
    + error = -ENOMEM;
    + new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
    + if (!new_name)
    + goto out;
    +
    + dup_name = sd->s_name;
    + sd->s_name = new_name;
    + }

    - dup_name = sd->s_name;
    - sd->s_name = new_name;
    + /* Remove from old parent's list and insert into new parent's list. */
    + if (sd->s_parent != new_parent_sd) {
    + sysfs_unlink_sibling(sd);
    + sysfs_get(new_parent_sd);
    + sysfs_put(sd->s_parent);
    + sd->s_parent = new_parent_sd;
    + sysfs_link_sibling(sd);
    + }

    error = 0;
    out:
    @@ -692,36 +704,14 @@ int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
    return error;
    }

    -int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
    +int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
    {
    - struct sysfs_dirent *sd = kobj->sd;
    - struct sysfs_dirent *new_parent_sd;
    - int error;
    -
    - BUG_ON(!sd->s_parent);
    -
    - mutex_lock(&sysfs_mutex);
    - new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root;
    -
    - error = 0;
    - if (sd->s_parent == new_parent_sd)
    - goto out; /* nothing to move */
    -
    - error = -EEXIST;
    - if (sysfs_find_dirent(new_parent_sd, sd->s_name))
    - goto out;
    -
    - /* Remove from old parent's list and insert into new parent's list. */
    - sysfs_unlink_sibling(sd);
    - sysfs_get(new_parent_sd);
    - sysfs_put(sd->s_parent);
    - sd->s_parent = new_parent_sd;
    - sysfs_link_sibling(sd);
    + return sysfs_mv_dir(kobj->sd, kobj->sd->s_parent, new_name);
    +}

    - error = 0;
    -out:
    - mutex_unlock(&sysfs_mutex);
    - return error;
    +int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
    +{
    + return sysfs_mv_dir(kobj->sd, new_parent_kobj->sd, kobj->sd->s_name);
    }

    /* Relationship between s_mode and the DT_xxx types */
    --
    1.6.1.2.350.g88cc


    \
     
     \ /
      Last update: 2009-05-21 02:37    [W:3.676 / U:0.040 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site