fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / sysfs / dir.c
index cfd290d..511edef 100644 (file)
@@ -43,6 +43,7 @@ static struct sysfs_dirent * sysfs_new_dirent(struct sysfs_dirent * parent_sd,
 
        memset(sd, 0, sizeof(*sd));
        atomic_set(&sd->s_count, 1);
+       atomic_set(&sd->s_event, 1);
        INIT_LIST_HEAD(&sd->s_children);
        list_add(&sd->s_sibling, &parent_sd->s_children);
        sd->s_element = element;
@@ -50,6 +51,32 @@ static struct sysfs_dirent * sysfs_new_dirent(struct sysfs_dirent * parent_sd,
        return sd;
 }
 
+/*
+ *
+ * Return -EEXIST if there is already a sysfs element with the same name for
+ * the same parent.
+ *
+ * called with parent inode's i_mutex held
+ */
+int sysfs_dirent_exist(struct sysfs_dirent *parent_sd,
+                         const unsigned char *new)
+{
+       struct sysfs_dirent * sd;
+
+       list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
+               if (sd->s_element) {
+                       const unsigned char *existing = sysfs_get_name(sd);
+                       if (strcmp(existing, new))
+                               continue;
+                       else
+                               return -EEXIST;
+               }
+       }
+
+       return 0;
+}
+
+
 int sysfs_make_dirent(struct sysfs_dirent * parent_sd, struct dentry * dentry,
                        void * element, umode_t mode, int type)
 {
@@ -76,7 +103,7 @@ static int init_dir(struct inode * inode)
        inode->i_fop = &sysfs_dir_operations;
 
        /* directory inodes start off with i_nlink == 2 (for "." entry) */
-       inode->i_nlink++;
+       inc_nlink(inode);
        return 0;
 }
 
@@ -102,11 +129,15 @@ static int create_dir(struct kobject * k, struct dentry * p,
        mutex_lock(&p->d_inode->i_mutex);
        *d = lookup_one_len(n, p, strlen(n));
        if (!IS_ERR(*d)) {
-               error = sysfs_make_dirent(p->d_fsdata, *d, k, mode, SYSFS_DIR);
+               if (sysfs_dirent_exist(p->d_fsdata, n))
+                       error = -EEXIST;
+               else
+                       error = sysfs_make_dirent(p->d_fsdata, *d, k, mode,
+                                                               SYSFS_DIR);
                if (!error) {
                        error = sysfs_create(*d, mode, init_dir);
                        if (!error) {
-                               p->d_inode->i_nlink++;
+                               inc_nlink(p->d_inode);
                                (*d)->d_op = &sysfs_dentry_ops;
                                d_rehash(*d);
                        }
@@ -341,9 +372,54 @@ int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
        return error;
 }
 
+int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent)
+{
+       struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry;
+       struct sysfs_dirent *new_parent_sd, *sd;
+       int error;
+
+       if (!new_parent)
+               return -EINVAL;
+
+       old_parent_dentry = kobj->parent ?
+               kobj->parent->dentry : sysfs_mount->mnt_sb->s_root;
+       new_parent_dentry = new_parent->dentry;
+
+again:
+       mutex_lock(&old_parent_dentry->d_inode->i_mutex);
+       if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) {
+               mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
+               goto again;
+       }
+
+       new_parent_sd = new_parent_dentry->d_fsdata;
+       sd = kobj->dentry->d_fsdata;
+
+       new_dentry = lookup_one_len(kobj->name, new_parent_dentry,
+                                   strlen(kobj->name));
+       if (IS_ERR(new_dentry)) {
+               error = PTR_ERR(new_dentry);
+               goto out;
+       } else
+               error = 0;
+       d_add(new_dentry, NULL);
+       d_move(kobj->dentry, new_dentry);
+       dput(new_dentry);
+
+       /* Remove from old parent's list and insert into new parent's list. */
+       list_del_init(&sd->s_sibling);
+       list_add(&sd->s_sibling, &new_parent_sd->s_children);
+
+out:
+       mutex_unlock(&new_parent_dentry->d_inode->i_mutex);
+       mutex_unlock(&old_parent_dentry->d_inode->i_mutex);
+
+       return error;
+}
+
 static int sysfs_dir_open(struct inode *inode, struct file *file)
 {
-       struct dentry * dentry = file->f_dentry;
+       struct dentry * dentry = file->f_path.dentry;
        struct sysfs_dirent * parent_sd = dentry->d_fsdata;
 
        mutex_lock(&dentry->d_inode->i_mutex);
@@ -356,7 +432,7 @@ static int sysfs_dir_open(struct inode *inode, struct file *file)
 
 static int sysfs_dir_close(struct inode *inode, struct file *file)
 {
-       struct dentry * dentry = file->f_dentry;
+       struct dentry * dentry = file->f_path.dentry;
        struct sysfs_dirent * cursor = file->private_data;
 
        mutex_lock(&dentry->d_inode->i_mutex);
@@ -376,7 +452,7 @@ static inline unsigned char dt_type(struct sysfs_dirent *sd)
 
 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
 {
-       struct dentry *dentry = filp->f_dentry;
+       struct dentry *dentry = filp->f_path.dentry;
        struct sysfs_dirent * parent_sd = dentry->d_fsdata;
        struct sysfs_dirent *cursor = filp->private_data;
        struct list_head *p, *q = &cursor->s_sibling;
@@ -399,10 +475,9 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
                        i++;
                        /* fallthrough */
                default:
-                       if (filp->f_pos == 2) {
-                               list_del(q);
-                               list_add(q, &parent_sd->s_children);
-                       }
+                       if (filp->f_pos == 2)
+                               list_move(q, &parent_sd->s_children);
+
                        for (p=q->next; p!= &parent_sd->s_children; p=p->next) {
                                struct sysfs_dirent *next;
                                const char * name;
@@ -424,8 +499,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
                                                 dt_type(next)) < 0)
                                        return 0;
 
-                               list_del(q);
-                               list_add(q, p);
+                               list_move(q, p);
                                p = q;
                                filp->f_pos++;
                        }
@@ -435,7 +509,7 @@ static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
 
 static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
 {
-       struct dentry * dentry = file->f_dentry;
+       struct dentry * dentry = file->f_path.dentry;
 
        mutex_lock(&dentry->d_inode->i_mutex);
        switch (origin) {
@@ -445,7 +519,7 @@ static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
                        if (offset >= 0)
                                break;
                default:
-                       mutex_unlock(&file->f_dentry->d_inode->i_mutex);
+                       mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
                        return -EINVAL;
        }
        if (offset != file->f_pos) {
@@ -473,14 +547,10 @@ static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin)
        return offset;
 }
 
-struct file_operations sysfs_dir_operations = {
+const struct file_operations sysfs_dir_operations = {
        .open           = sysfs_dir_open,
        .release        = sysfs_dir_close,
        .llseek         = sysfs_dir_lseek,
        .read           = generic_read_dir,
        .readdir        = sysfs_readdir,
 };
-
-EXPORT_SYMBOL_GPL(sysfs_create_dir);
-EXPORT_SYMBOL_GPL(sysfs_remove_dir);
-EXPORT_SYMBOL_GPL(sysfs_rename_dir);