Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * dir.c - Operations for configfs directories.
0004  *
0005  * Based on sysfs:
0006  *  sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
0007  *
0008  * configfs Copyright (C) 2005 Oracle.  All rights reserved.
0009  */
0010 
0011 #undef DEBUG
0012 
0013 #include <linux/fs.h>
0014 #include <linux/fsnotify.h>
0015 #include <linux/mount.h>
0016 #include <linux/module.h>
0017 #include <linux/slab.h>
0018 #include <linux/err.h>
0019 
0020 #include <linux/configfs.h>
0021 #include "configfs_internal.h"
0022 
0023 /*
0024  * Protects mutations of configfs_dirent linkage together with proper i_mutex
0025  * Also protects mutations of symlinks linkage to target configfs_dirent
0026  * Mutators of configfs_dirent linkage must *both* have the proper inode locked
0027  * and configfs_dirent_lock locked, in that order.
0028  * This allows one to safely traverse configfs_dirent trees and symlinks without
0029  * having to lock inodes.
0030  *
0031  * Protects setting of CONFIGFS_USET_DROPPING: checking the flag
0032  * unlocked is not reliable unless in detach_groups() called from
0033  * rmdir()/unregister() and from configfs_attach_group()
0034  */
0035 DEFINE_SPINLOCK(configfs_dirent_lock);
0036 
0037 /*
0038  * All of link_obj/unlink_obj/link_group/unlink_group require that
0039  * subsys->su_mutex is held.
0040  * But parent configfs_subsystem is NULL when config_item is root.
0041  * Use this mutex when config_item is root.
0042  */
0043 static DEFINE_MUTEX(configfs_subsystem_mutex);
0044 
0045 static void configfs_d_iput(struct dentry * dentry,
0046                 struct inode * inode)
0047 {
0048     struct configfs_dirent *sd = dentry->d_fsdata;
0049 
0050     if (sd) {
0051         /* Coordinate with configfs_readdir */
0052         spin_lock(&configfs_dirent_lock);
0053         /*
0054          * Set sd->s_dentry to null only when this dentry is the one
0055          * that is going to be killed.  Otherwise configfs_d_iput may
0056          * run just after configfs_lookup and set sd->s_dentry to
0057          * NULL even it's still in use.
0058          */
0059         if (sd->s_dentry == dentry)
0060             sd->s_dentry = NULL;
0061 
0062         spin_unlock(&configfs_dirent_lock);
0063         configfs_put(sd);
0064     }
0065     iput(inode);
0066 }
0067 
0068 const struct dentry_operations configfs_dentry_ops = {
0069     .d_iput     = configfs_d_iput,
0070     .d_delete   = always_delete_dentry,
0071 };
0072 
0073 #ifdef CONFIG_LOCKDEP
0074 
0075 /*
0076  * Helpers to make lockdep happy with our recursive locking of default groups'
0077  * inodes (see configfs_attach_group() and configfs_detach_group()).
0078  * We put default groups i_mutexes in separate classes according to their depth
0079  * from the youngest non-default group ancestor.
0080  *
0081  * For a non-default group A having default groups A/B, A/C, and A/C/D, default
0082  * groups A/B and A/C will have their inode's mutex in class
0083  * default_group_class[0], and default group A/C/D will be in
0084  * default_group_class[1].
0085  *
0086  * The lock classes are declared and assigned in inode.c, according to the
0087  * s_depth value.
0088  * The s_depth value is initialized to -1, adjusted to >= 0 when attaching
0089  * default groups, and reset to -1 when all default groups are attached. During
0090  * attachment, if configfs_create() sees s_depth > 0, the lock class of the new
0091  * inode's mutex is set to default_group_class[s_depth - 1].
0092  */
0093 
0094 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
0095 {
0096     sd->s_depth = -1;
0097 }
0098 
0099 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
0100                       struct configfs_dirent *sd)
0101 {
0102     int parent_depth = parent_sd->s_depth;
0103 
0104     if (parent_depth >= 0)
0105         sd->s_depth = parent_depth + 1;
0106 }
0107 
0108 static void
0109 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
0110 {
0111     /*
0112      * item's i_mutex class is already setup, so s_depth is now only
0113      * used to set new sub-directories s_depth, which is always done
0114      * with item's i_mutex locked.
0115      */
0116     /*
0117      *  sd->s_depth == -1 iff we are a non default group.
0118      *  else (we are a default group) sd->s_depth > 0 (see
0119      *  create_dir()).
0120      */
0121     if (sd->s_depth == -1)
0122         /*
0123          * We are a non default group and we are going to create
0124          * default groups.
0125          */
0126         sd->s_depth = 0;
0127 }
0128 
0129 static void
0130 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
0131 {
0132     /* We will not create default groups anymore. */
0133     sd->s_depth = -1;
0134 }
0135 
0136 #else /* CONFIG_LOCKDEP */
0137 
0138 static void configfs_init_dirent_depth(struct configfs_dirent *sd)
0139 {
0140 }
0141 
0142 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
0143                       struct configfs_dirent *sd)
0144 {
0145 }
0146 
0147 static void
0148 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
0149 {
0150 }
0151 
0152 static void
0153 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
0154 {
0155 }
0156 
0157 #endif /* CONFIG_LOCKDEP */
0158 
0159 static struct configfs_fragment *new_fragment(void)
0160 {
0161     struct configfs_fragment *p;
0162 
0163     p = kmalloc(sizeof(struct configfs_fragment), GFP_KERNEL);
0164     if (p) {
0165         atomic_set(&p->frag_count, 1);
0166         init_rwsem(&p->frag_sem);
0167         p->frag_dead = false;
0168     }
0169     return p;
0170 }
0171 
0172 void put_fragment(struct configfs_fragment *frag)
0173 {
0174     if (frag && atomic_dec_and_test(&frag->frag_count))
0175         kfree(frag);
0176 }
0177 
0178 struct configfs_fragment *get_fragment(struct configfs_fragment *frag)
0179 {
0180     if (likely(frag))
0181         atomic_inc(&frag->frag_count);
0182     return frag;
0183 }
0184 
0185 /*
0186  * Allocates a new configfs_dirent and links it to the parent configfs_dirent
0187  */
0188 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
0189                            void *element, int type,
0190                            struct configfs_fragment *frag)
0191 {
0192     struct configfs_dirent * sd;
0193 
0194     sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
0195     if (!sd)
0196         return ERR_PTR(-ENOMEM);
0197 
0198     atomic_set(&sd->s_count, 1);
0199     INIT_LIST_HEAD(&sd->s_children);
0200     sd->s_element = element;
0201     sd->s_type = type;
0202     configfs_init_dirent_depth(sd);
0203     spin_lock(&configfs_dirent_lock);
0204     if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
0205         spin_unlock(&configfs_dirent_lock);
0206         kmem_cache_free(configfs_dir_cachep, sd);
0207         return ERR_PTR(-ENOENT);
0208     }
0209     sd->s_frag = get_fragment(frag);
0210     list_add(&sd->s_sibling, &parent_sd->s_children);
0211     spin_unlock(&configfs_dirent_lock);
0212 
0213     return sd;
0214 }
0215 
0216 /*
0217  *
0218  * Return -EEXIST if there is already a configfs element with the same
0219  * name for the same parent.
0220  *
0221  * called with parent inode's i_mutex held
0222  */
0223 static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
0224                   const unsigned char *new)
0225 {
0226     struct configfs_dirent * sd;
0227 
0228     list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
0229         if (sd->s_element) {
0230             const unsigned char *existing = configfs_get_name(sd);
0231             if (strcmp(existing, new))
0232                 continue;
0233             else
0234                 return -EEXIST;
0235         }
0236     }
0237 
0238     return 0;
0239 }
0240 
0241 
0242 int configfs_make_dirent(struct configfs_dirent * parent_sd,
0243              struct dentry * dentry, void * element,
0244              umode_t mode, int type, struct configfs_fragment *frag)
0245 {
0246     struct configfs_dirent * sd;
0247 
0248     sd = configfs_new_dirent(parent_sd, element, type, frag);
0249     if (IS_ERR(sd))
0250         return PTR_ERR(sd);
0251 
0252     sd->s_mode = mode;
0253     sd->s_dentry = dentry;
0254     if (dentry)
0255         dentry->d_fsdata = configfs_get(sd);
0256 
0257     return 0;
0258 }
0259 
0260 static void configfs_remove_dirent(struct dentry *dentry)
0261 {
0262     struct configfs_dirent *sd = dentry->d_fsdata;
0263 
0264     if (!sd)
0265         return;
0266     spin_lock(&configfs_dirent_lock);
0267     list_del_init(&sd->s_sibling);
0268     spin_unlock(&configfs_dirent_lock);
0269     configfs_put(sd);
0270 }
0271 
0272 /**
0273  *  configfs_create_dir - create a directory for an config_item.
0274  *  @item:      config_itemwe're creating directory for.
0275  *  @dentry:    config_item's dentry.
0276  *  @frag:      config_item's fragment.
0277  *
0278  *  Note: user-created entries won't be allowed under this new directory
0279  *  until it is validated by configfs_dir_set_ready()
0280  */
0281 
0282 static int configfs_create_dir(struct config_item *item, struct dentry *dentry,
0283                 struct configfs_fragment *frag)
0284 {
0285     int error;
0286     umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
0287     struct dentry *p = dentry->d_parent;
0288     struct inode *inode;
0289 
0290     BUG_ON(!item);
0291 
0292     error = configfs_dirent_exists(p->d_fsdata, dentry->d_name.name);
0293     if (unlikely(error))
0294         return error;
0295 
0296     error = configfs_make_dirent(p->d_fsdata, dentry, item, mode,
0297                      CONFIGFS_DIR | CONFIGFS_USET_CREATING,
0298                      frag);
0299     if (unlikely(error))
0300         return error;
0301 
0302     configfs_set_dir_dirent_depth(p->d_fsdata, dentry->d_fsdata);
0303     inode = configfs_create(dentry, mode);
0304     if (IS_ERR(inode))
0305         goto out_remove;
0306 
0307     inode->i_op = &configfs_dir_inode_operations;
0308     inode->i_fop = &configfs_dir_operations;
0309     /* directory inodes start off with i_nlink == 2 (for "." entry) */
0310     inc_nlink(inode);
0311     d_instantiate(dentry, inode);
0312     /* already hashed */
0313     dget(dentry);  /* pin directory dentries in core */
0314     inc_nlink(d_inode(p));
0315     item->ci_dentry = dentry;
0316     return 0;
0317 
0318 out_remove:
0319     configfs_remove_dirent(dentry);
0320     return PTR_ERR(inode);
0321 }
0322 
0323 /*
0324  * Allow userspace to create new entries under a new directory created with
0325  * configfs_create_dir(), and under all of its chidlren directories recursively.
0326  * @sd      configfs_dirent of the new directory to validate
0327  *
0328  * Caller must hold configfs_dirent_lock.
0329  */
0330 static void configfs_dir_set_ready(struct configfs_dirent *sd)
0331 {
0332     struct configfs_dirent *child_sd;
0333 
0334     sd->s_type &= ~CONFIGFS_USET_CREATING;
0335     list_for_each_entry(child_sd, &sd->s_children, s_sibling)
0336         if (child_sd->s_type & CONFIGFS_USET_CREATING)
0337             configfs_dir_set_ready(child_sd);
0338 }
0339 
0340 /*
0341  * Check that a directory does not belong to a directory hierarchy being
0342  * attached and not validated yet.
0343  * @sd      configfs_dirent of the directory to check
0344  *
0345  * @return  non-zero iff the directory was validated
0346  *
0347  * Note: takes configfs_dirent_lock, so the result may change from false to true
0348  * in two consecutive calls, but never from true to false.
0349  */
0350 int configfs_dirent_is_ready(struct configfs_dirent *sd)
0351 {
0352     int ret;
0353 
0354     spin_lock(&configfs_dirent_lock);
0355     ret = !(sd->s_type & CONFIGFS_USET_CREATING);
0356     spin_unlock(&configfs_dirent_lock);
0357 
0358     return ret;
0359 }
0360 
0361 int configfs_create_link(struct configfs_dirent *target, struct dentry *parent,
0362         struct dentry *dentry, char *body)
0363 {
0364     int err = 0;
0365     umode_t mode = S_IFLNK | S_IRWXUGO;
0366     struct configfs_dirent *p = parent->d_fsdata;
0367     struct inode *inode;
0368 
0369     err = configfs_make_dirent(p, dentry, target, mode, CONFIGFS_ITEM_LINK,
0370             p->s_frag);
0371     if (err)
0372         return err;
0373 
0374     inode = configfs_create(dentry, mode);
0375     if (IS_ERR(inode))
0376         goto out_remove;
0377 
0378     inode->i_link = body;
0379     inode->i_op = &configfs_symlink_inode_operations;
0380     d_instantiate(dentry, inode);
0381     dget(dentry);  /* pin link dentries in core */
0382     return 0;
0383 
0384 out_remove:
0385     configfs_remove_dirent(dentry);
0386     return PTR_ERR(inode);
0387 }
0388 
0389 static void remove_dir(struct dentry * d)
0390 {
0391     struct dentry * parent = dget(d->d_parent);
0392 
0393     configfs_remove_dirent(d);
0394 
0395     if (d_really_is_positive(d))
0396         simple_rmdir(d_inode(parent),d);
0397 
0398     pr_debug(" o %pd removing done (%d)\n", d, d_count(d));
0399 
0400     dput(parent);
0401 }
0402 
0403 /**
0404  * configfs_remove_dir - remove an config_item's directory.
0405  * @item:   config_item we're removing.
0406  *
0407  * The only thing special about this is that we remove any files in
0408  * the directory before we remove the directory, and we've inlined
0409  * what used to be configfs_rmdir() below, instead of calling separately.
0410  *
0411  * Caller holds the mutex of the item's inode
0412  */
0413 
0414 static void configfs_remove_dir(struct config_item * item)
0415 {
0416     struct dentry * dentry = dget(item->ci_dentry);
0417 
0418     if (!dentry)
0419         return;
0420 
0421     remove_dir(dentry);
0422     /**
0423      * Drop reference from dget() on entrance.
0424      */
0425     dput(dentry);
0426 }
0427 
0428 static struct dentry * configfs_lookup(struct inode *dir,
0429                        struct dentry *dentry,
0430                        unsigned int flags)
0431 {
0432     struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
0433     struct configfs_dirent * sd;
0434     struct inode *inode = NULL;
0435 
0436     if (dentry->d_name.len > NAME_MAX)
0437         return ERR_PTR(-ENAMETOOLONG);
0438 
0439     /*
0440      * Fake invisibility if dir belongs to a group/default groups hierarchy
0441      * being attached
0442      *
0443      * This forbids userspace to read/write attributes of items which may
0444      * not complete their initialization, since the dentries of the
0445      * attributes won't be instantiated.
0446      */
0447     if (!configfs_dirent_is_ready(parent_sd))
0448         return ERR_PTR(-ENOENT);
0449 
0450     spin_lock(&configfs_dirent_lock);
0451     list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
0452         if ((sd->s_type & CONFIGFS_NOT_PINNED) &&
0453             !strcmp(configfs_get_name(sd), dentry->d_name.name)) {
0454             struct configfs_attribute *attr = sd->s_element;
0455             umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
0456 
0457             dentry->d_fsdata = configfs_get(sd);
0458             sd->s_dentry = dentry;
0459             spin_unlock(&configfs_dirent_lock);
0460 
0461             inode = configfs_create(dentry, mode);
0462             if (IS_ERR(inode)) {
0463                 configfs_put(sd);
0464                 return ERR_CAST(inode);
0465             }
0466             if (sd->s_type & CONFIGFS_ITEM_BIN_ATTR) {
0467                 inode->i_size = 0;
0468                 inode->i_fop = &configfs_bin_file_operations;
0469             } else {
0470                 inode->i_size = PAGE_SIZE;
0471                 inode->i_fop = &configfs_file_operations;
0472             }
0473             goto done;
0474         }
0475     }
0476     spin_unlock(&configfs_dirent_lock);
0477 done:
0478     d_add(dentry, inode);
0479     return NULL;
0480 }
0481 
0482 /*
0483  * Only subdirectories count here.  Files (CONFIGFS_NOT_PINNED) are
0484  * attributes and are removed by rmdir().  We recurse, setting
0485  * CONFIGFS_USET_DROPPING on all children that are candidates for
0486  * default detach.
0487  * If there is an error, the caller will reset the flags via
0488  * configfs_detach_rollback().
0489  */
0490 static int configfs_detach_prep(struct dentry *dentry, struct dentry **wait)
0491 {
0492     struct configfs_dirent *parent_sd = dentry->d_fsdata;
0493     struct configfs_dirent *sd;
0494     int ret;
0495 
0496     /* Mark that we're trying to drop the group */
0497     parent_sd->s_type |= CONFIGFS_USET_DROPPING;
0498 
0499     ret = -EBUSY;
0500     if (parent_sd->s_links)
0501         goto out;
0502 
0503     ret = 0;
0504     list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
0505         if (!sd->s_element ||
0506             (sd->s_type & CONFIGFS_NOT_PINNED))
0507             continue;
0508         if (sd->s_type & CONFIGFS_USET_DEFAULT) {
0509             /* Abort if racing with mkdir() */
0510             if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
0511                 if (wait)
0512                     *wait= dget(sd->s_dentry);
0513                 return -EAGAIN;
0514             }
0515 
0516             /*
0517              * Yup, recursive.  If there's a problem, blame
0518              * deep nesting of default_groups
0519              */
0520             ret = configfs_detach_prep(sd->s_dentry, wait);
0521             if (!ret)
0522                 continue;
0523         } else
0524             ret = -ENOTEMPTY;
0525 
0526         break;
0527     }
0528 
0529 out:
0530     return ret;
0531 }
0532 
0533 /*
0534  * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was
0535  * set.
0536  */
0537 static void configfs_detach_rollback(struct dentry *dentry)
0538 {
0539     struct configfs_dirent *parent_sd = dentry->d_fsdata;
0540     struct configfs_dirent *sd;
0541 
0542     parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
0543 
0544     list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
0545         if (sd->s_type & CONFIGFS_USET_DEFAULT)
0546             configfs_detach_rollback(sd->s_dentry);
0547 }
0548 
0549 static void detach_attrs(struct config_item * item)
0550 {
0551     struct dentry * dentry = dget(item->ci_dentry);
0552     struct configfs_dirent * parent_sd;
0553     struct configfs_dirent * sd, * tmp;
0554 
0555     if (!dentry)
0556         return;
0557 
0558     pr_debug("configfs %s: dropping attrs for  dir\n",
0559          dentry->d_name.name);
0560 
0561     parent_sd = dentry->d_fsdata;
0562     list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
0563         if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
0564             continue;
0565         spin_lock(&configfs_dirent_lock);
0566         list_del_init(&sd->s_sibling);
0567         spin_unlock(&configfs_dirent_lock);
0568         configfs_drop_dentry(sd, dentry);
0569         configfs_put(sd);
0570     }
0571 
0572     /**
0573      * Drop reference from dget() on entrance.
0574      */
0575     dput(dentry);
0576 }
0577 
0578 static int populate_attrs(struct config_item *item)
0579 {
0580     const struct config_item_type *t = item->ci_type;
0581     struct configfs_attribute *attr;
0582     struct configfs_bin_attribute *bin_attr;
0583     int error = 0;
0584     int i;
0585 
0586     if (!t)
0587         return -EINVAL;
0588     if (t->ct_attrs) {
0589         for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
0590             if ((error = configfs_create_file(item, attr)))
0591                 break;
0592         }
0593     }
0594     if (t->ct_bin_attrs) {
0595         for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) {
0596             error = configfs_create_bin_file(item, bin_attr);
0597             if (error)
0598                 break;
0599         }
0600     }
0601 
0602     if (error)
0603         detach_attrs(item);
0604 
0605     return error;
0606 }
0607 
0608 static int configfs_attach_group(struct config_item *parent_item,
0609                  struct config_item *item,
0610                  struct dentry *dentry,
0611                  struct configfs_fragment *frag);
0612 static void configfs_detach_group(struct config_item *item);
0613 
0614 static void detach_groups(struct config_group *group)
0615 {
0616     struct dentry * dentry = dget(group->cg_item.ci_dentry);
0617     struct dentry *child;
0618     struct configfs_dirent *parent_sd;
0619     struct configfs_dirent *sd, *tmp;
0620 
0621     if (!dentry)
0622         return;
0623 
0624     parent_sd = dentry->d_fsdata;
0625     list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
0626         if (!sd->s_element ||
0627             !(sd->s_type & CONFIGFS_USET_DEFAULT))
0628             continue;
0629 
0630         child = sd->s_dentry;
0631 
0632         inode_lock(d_inode(child));
0633 
0634         configfs_detach_group(sd->s_element);
0635         d_inode(child)->i_flags |= S_DEAD;
0636         dont_mount(child);
0637 
0638         inode_unlock(d_inode(child));
0639 
0640         d_delete(child);
0641         dput(child);
0642     }
0643 
0644     /**
0645      * Drop reference from dget() on entrance.
0646      */
0647     dput(dentry);
0648 }
0649 
0650 /*
0651  * This fakes mkdir(2) on a default_groups[] entry.  It
0652  * creates a dentry, attachs it, and then does fixup
0653  * on the sd->s_type.
0654  *
0655  * We could, perhaps, tweak our parent's ->mkdir for a minute and
0656  * try using vfs_mkdir.  Just a thought.
0657  */
0658 static int create_default_group(struct config_group *parent_group,
0659                 struct config_group *group,
0660                 struct configfs_fragment *frag)
0661 {
0662     int ret;
0663     struct configfs_dirent *sd;
0664     /* We trust the caller holds a reference to parent */
0665     struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
0666 
0667     if (!group->cg_item.ci_name)
0668         group->cg_item.ci_name = group->cg_item.ci_namebuf;
0669 
0670     ret = -ENOMEM;
0671     child = d_alloc_name(parent, group->cg_item.ci_name);
0672     if (child) {
0673         d_add(child, NULL);
0674 
0675         ret = configfs_attach_group(&parent_group->cg_item,
0676                         &group->cg_item, child, frag);
0677         if (!ret) {
0678             sd = child->d_fsdata;
0679             sd->s_type |= CONFIGFS_USET_DEFAULT;
0680         } else {
0681             BUG_ON(d_inode(child));
0682             d_drop(child);
0683             dput(child);
0684         }
0685     }
0686 
0687     return ret;
0688 }
0689 
0690 static int populate_groups(struct config_group *group,
0691                struct configfs_fragment *frag)
0692 {
0693     struct config_group *new_group;
0694     int ret = 0;
0695 
0696     list_for_each_entry(new_group, &group->default_groups, group_entry) {
0697         ret = create_default_group(group, new_group, frag);
0698         if (ret) {
0699             detach_groups(group);
0700             break;
0701         }
0702     }
0703 
0704     return ret;
0705 }
0706 
0707 void configfs_remove_default_groups(struct config_group *group)
0708 {
0709     struct config_group *g, *n;
0710 
0711     list_for_each_entry_safe(g, n, &group->default_groups, group_entry) {
0712         list_del(&g->group_entry);
0713         config_item_put(&g->cg_item);
0714     }
0715 }
0716 EXPORT_SYMBOL(configfs_remove_default_groups);
0717 
0718 /*
0719  * All of link_obj/unlink_obj/link_group/unlink_group require that
0720  * subsys->su_mutex is held.
0721  */
0722 
0723 static void unlink_obj(struct config_item *item)
0724 {
0725     struct config_group *group;
0726 
0727     group = item->ci_group;
0728     if (group) {
0729         list_del_init(&item->ci_entry);
0730 
0731         item->ci_group = NULL;
0732         item->ci_parent = NULL;
0733 
0734         /* Drop the reference for ci_entry */
0735         config_item_put(item);
0736 
0737         /* Drop the reference for ci_parent */
0738         config_group_put(group);
0739     }
0740 }
0741 
0742 static void link_obj(struct config_item *parent_item, struct config_item *item)
0743 {
0744     /*
0745      * Parent seems redundant with group, but it makes certain
0746      * traversals much nicer.
0747      */
0748     item->ci_parent = parent_item;
0749 
0750     /*
0751      * We hold a reference on the parent for the child's ci_parent
0752      * link.
0753      */
0754     item->ci_group = config_group_get(to_config_group(parent_item));
0755     list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
0756 
0757     /*
0758      * We hold a reference on the child for ci_entry on the parent's
0759      * cg_children
0760      */
0761     config_item_get(item);
0762 }
0763 
0764 static void unlink_group(struct config_group *group)
0765 {
0766     struct config_group *new_group;
0767 
0768     list_for_each_entry(new_group, &group->default_groups, group_entry)
0769         unlink_group(new_group);
0770 
0771     group->cg_subsys = NULL;
0772     unlink_obj(&group->cg_item);
0773 }
0774 
0775 static void link_group(struct config_group *parent_group, struct config_group *group)
0776 {
0777     struct config_group *new_group;
0778     struct configfs_subsystem *subsys = NULL; /* gcc is a turd */
0779 
0780     link_obj(&parent_group->cg_item, &group->cg_item);
0781 
0782     if (parent_group->cg_subsys)
0783         subsys = parent_group->cg_subsys;
0784     else if (configfs_is_root(&parent_group->cg_item))
0785         subsys = to_configfs_subsystem(group);
0786     else
0787         BUG();
0788     group->cg_subsys = subsys;
0789 
0790     list_for_each_entry(new_group, &group->default_groups, group_entry)
0791         link_group(group, new_group);
0792 }
0793 
0794 /*
0795  * The goal is that configfs_attach_item() (and
0796  * configfs_attach_group()) can be called from either the VFS or this
0797  * module.  That is, they assume that the items have been created,
0798  * the dentry allocated, and the dcache is all ready to go.
0799  *
0800  * If they fail, they must clean up after themselves as if they
0801  * had never been called.  The caller (VFS or local function) will
0802  * handle cleaning up the dcache bits.
0803  *
0804  * configfs_detach_group() and configfs_detach_item() behave similarly on
0805  * the way out.  They assume that the proper semaphores are held, they
0806  * clean up the configfs items, and they expect their callers will
0807  * handle the dcache bits.
0808  */
0809 static int configfs_attach_item(struct config_item *parent_item,
0810                 struct config_item *item,
0811                 struct dentry *dentry,
0812                 struct configfs_fragment *frag)
0813 {
0814     int ret;
0815 
0816     ret = configfs_create_dir(item, dentry, frag);
0817     if (!ret) {
0818         ret = populate_attrs(item);
0819         if (ret) {
0820             /*
0821              * We are going to remove an inode and its dentry but
0822              * the VFS may already have hit and used them. Thus,
0823              * we must lock them as rmdir() would.
0824              */
0825             inode_lock(d_inode(dentry));
0826             configfs_remove_dir(item);
0827             d_inode(dentry)->i_flags |= S_DEAD;
0828             dont_mount(dentry);
0829             inode_unlock(d_inode(dentry));
0830             d_delete(dentry);
0831         }
0832     }
0833 
0834     return ret;
0835 }
0836 
0837 /* Caller holds the mutex of the item's inode */
0838 static void configfs_detach_item(struct config_item *item)
0839 {
0840     detach_attrs(item);
0841     configfs_remove_dir(item);
0842 }
0843 
0844 static int configfs_attach_group(struct config_item *parent_item,
0845                  struct config_item *item,
0846                  struct dentry *dentry,
0847                  struct configfs_fragment *frag)
0848 {
0849     int ret;
0850     struct configfs_dirent *sd;
0851 
0852     ret = configfs_attach_item(parent_item, item, dentry, frag);
0853     if (!ret) {
0854         sd = dentry->d_fsdata;
0855         sd->s_type |= CONFIGFS_USET_DIR;
0856 
0857         /*
0858          * FYI, we're faking mkdir in populate_groups()
0859          * We must lock the group's inode to avoid races with the VFS
0860          * which can already hit the inode and try to add/remove entries
0861          * under it.
0862          *
0863          * We must also lock the inode to remove it safely in case of
0864          * error, as rmdir() would.
0865          */
0866         inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
0867         configfs_adjust_dir_dirent_depth_before_populate(sd);
0868         ret = populate_groups(to_config_group(item), frag);
0869         if (ret) {
0870             configfs_detach_item(item);
0871             d_inode(dentry)->i_flags |= S_DEAD;
0872             dont_mount(dentry);
0873         }
0874         configfs_adjust_dir_dirent_depth_after_populate(sd);
0875         inode_unlock(d_inode(dentry));
0876         if (ret)
0877             d_delete(dentry);
0878     }
0879 
0880     return ret;
0881 }
0882 
0883 /* Caller holds the mutex of the group's inode */
0884 static void configfs_detach_group(struct config_item *item)
0885 {
0886     detach_groups(to_config_group(item));
0887     configfs_detach_item(item);
0888 }
0889 
0890 /*
0891  * After the item has been detached from the filesystem view, we are
0892  * ready to tear it out of the hierarchy.  Notify the client before
0893  * we do that so they can perform any cleanup that requires
0894  * navigating the hierarchy.  A client does not need to provide this
0895  * callback.  The subsystem semaphore MUST be held by the caller, and
0896  * references must be valid for both items.  It also assumes the
0897  * caller has validated ci_type.
0898  */
0899 static void client_disconnect_notify(struct config_item *parent_item,
0900                      struct config_item *item)
0901 {
0902     const struct config_item_type *type;
0903 
0904     type = parent_item->ci_type;
0905     BUG_ON(!type);
0906 
0907     if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
0908         type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
0909                               item);
0910 }
0911 
0912 /*
0913  * Drop the initial reference from make_item()/make_group()
0914  * This function assumes that reference is held on item
0915  * and that item holds a valid reference to the parent.  Also, it
0916  * assumes the caller has validated ci_type.
0917  */
0918 static void client_drop_item(struct config_item *parent_item,
0919                  struct config_item *item)
0920 {
0921     const struct config_item_type *type;
0922 
0923     type = parent_item->ci_type;
0924     BUG_ON(!type);
0925 
0926     /*
0927      * If ->drop_item() exists, it is responsible for the
0928      * config_item_put().
0929      */
0930     if (type->ct_group_ops && type->ct_group_ops->drop_item)
0931         type->ct_group_ops->drop_item(to_config_group(parent_item),
0932                           item);
0933     else
0934         config_item_put(item);
0935 }
0936 
0937 #ifdef DEBUG
0938 static void configfs_dump_one(struct configfs_dirent *sd, int level)
0939 {
0940     pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd));
0941 
0942 #define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type);
0943     type_print(CONFIGFS_ROOT);
0944     type_print(CONFIGFS_DIR);
0945     type_print(CONFIGFS_ITEM_ATTR);
0946     type_print(CONFIGFS_ITEM_LINK);
0947     type_print(CONFIGFS_USET_DIR);
0948     type_print(CONFIGFS_USET_DEFAULT);
0949     type_print(CONFIGFS_USET_DROPPING);
0950 #undef type_print
0951 }
0952 
0953 static int configfs_dump(struct configfs_dirent *sd, int level)
0954 {
0955     struct configfs_dirent *child_sd;
0956     int ret = 0;
0957 
0958     configfs_dump_one(sd, level);
0959 
0960     if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
0961         return 0;
0962 
0963     list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
0964         ret = configfs_dump(child_sd, level + 2);
0965         if (ret)
0966             break;
0967     }
0968 
0969     return ret;
0970 }
0971 #endif
0972 
0973 
0974 /*
0975  * configfs_depend_item() and configfs_undepend_item()
0976  *
0977  * WARNING: Do not call these from a configfs callback!
0978  *
0979  * This describes these functions and their helpers.
0980  *
0981  * Allow another kernel system to depend on a config_item.  If this
0982  * happens, the item cannot go away until the dependent can live without
0983  * it.  The idea is to give client modules as simple an interface as
0984  * possible.  When a system asks them to depend on an item, they just
0985  * call configfs_depend_item().  If the item is live and the client
0986  * driver is in good shape, we'll happily do the work for them.
0987  *
0988  * Why is the locking complex?  Because configfs uses the VFS to handle
0989  * all locking, but this function is called outside the normal
0990  * VFS->configfs path.  So it must take VFS locks to prevent the
0991  * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc).  This is
0992  * why you can't call these functions underneath configfs callbacks.
0993  *
0994  * Note, btw, that this can be called at *any* time, even when a configfs
0995  * subsystem isn't registered, or when configfs is loading or unloading.
0996  * Just like configfs_register_subsystem().  So we take the same
0997  * precautions.  We pin the filesystem.  We lock configfs_dirent_lock.
0998  * If we can find the target item in the
0999  * configfs tree, it must be part of the subsystem tree as well, so we
1000  * do not need the subsystem semaphore.  Holding configfs_dirent_lock helps
1001  * locking out mkdir() and rmdir(), who might be racing us.
1002  */
1003 
1004 /*
1005  * configfs_depend_prep()
1006  *
1007  * Only subdirectories count here.  Files (CONFIGFS_NOT_PINNED) are
1008  * attributes.  This is similar but not the same to configfs_detach_prep().
1009  * Note that configfs_detach_prep() expects the parent to be locked when it
1010  * is called, but we lock the parent *inside* configfs_depend_prep().  We
1011  * do that so we can unlock it if we find nothing.
1012  *
1013  * Here we do a depth-first search of the dentry hierarchy looking for
1014  * our object.
1015  * We deliberately ignore items tagged as dropping since they are virtually
1016  * dead, as well as items in the middle of attachment since they virtually
1017  * do not exist yet. This completes the locking out of racing mkdir() and
1018  * rmdir().
1019  * Note: subdirectories in the middle of attachment start with s_type =
1020  * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir().  When
1021  * CONFIGFS_USET_CREATING is set, we ignore the item.  The actual set of
1022  * s_type is in configfs_new_dirent(), which has configfs_dirent_lock.
1023  *
1024  * If the target is not found, -ENOENT is bubbled up.
1025  *
1026  * This adds a requirement that all config_items be unique!
1027  *
1028  * This is recursive.  There isn't
1029  * much on the stack, though, so folks that need this function - be careful
1030  * about your stack!  Patches will be accepted to make it iterative.
1031  */
1032 static int configfs_depend_prep(struct dentry *origin,
1033                 struct config_item *target)
1034 {
1035     struct configfs_dirent *child_sd, *sd;
1036     int ret = 0;
1037 
1038     BUG_ON(!origin || !origin->d_fsdata);
1039     sd = origin->d_fsdata;
1040 
1041     if (sd->s_element == target)  /* Boo-yah */
1042         goto out;
1043 
1044     list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
1045         if ((child_sd->s_type & CONFIGFS_DIR) &&
1046             !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1047             !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
1048             ret = configfs_depend_prep(child_sd->s_dentry,
1049                            target);
1050             if (!ret)
1051                 goto out;  /* Child path boo-yah */
1052         }
1053     }
1054 
1055     /* We looped all our children and didn't find target */
1056     ret = -ENOENT;
1057 
1058 out:
1059     return ret;
1060 }
1061 
1062 static int configfs_do_depend_item(struct dentry *subsys_dentry,
1063                    struct config_item *target)
1064 {
1065     struct configfs_dirent *p;
1066     int ret;
1067 
1068     spin_lock(&configfs_dirent_lock);
1069     /* Scan the tree, return 0 if found */
1070     ret = configfs_depend_prep(subsys_dentry, target);
1071     if (ret)
1072         goto out_unlock_dirent_lock;
1073 
1074     /*
1075      * We are sure that the item is not about to be removed by rmdir(), and
1076      * not in the middle of attachment by mkdir().
1077      */
1078     p = target->ci_dentry->d_fsdata;
1079     p->s_dependent_count += 1;
1080 
1081 out_unlock_dirent_lock:
1082     spin_unlock(&configfs_dirent_lock);
1083 
1084     return ret;
1085 }
1086 
1087 static inline struct configfs_dirent *
1088 configfs_find_subsys_dentry(struct configfs_dirent *root_sd,
1089                 struct config_item *subsys_item)
1090 {
1091     struct configfs_dirent *p;
1092     struct configfs_dirent *ret = NULL;
1093 
1094     list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1095         if (p->s_type & CONFIGFS_DIR &&
1096             p->s_element == subsys_item) {
1097             ret = p;
1098             break;
1099         }
1100     }
1101 
1102     return ret;
1103 }
1104 
1105 
1106 int configfs_depend_item(struct configfs_subsystem *subsys,
1107              struct config_item *target)
1108 {
1109     int ret;
1110     struct configfs_dirent *subsys_sd;
1111     struct config_item *s_item = &subsys->su_group.cg_item;
1112     struct dentry *root;
1113 
1114     /*
1115      * Pin the configfs filesystem.  This means we can safely access
1116      * the root of the configfs filesystem.
1117      */
1118     root = configfs_pin_fs();
1119     if (IS_ERR(root))
1120         return PTR_ERR(root);
1121 
1122     /*
1123      * Next, lock the root directory.  We're going to check that the
1124      * subsystem is really registered, and so we need to lock out
1125      * configfs_[un]register_subsystem().
1126      */
1127     inode_lock(d_inode(root));
1128 
1129     subsys_sd = configfs_find_subsys_dentry(root->d_fsdata, s_item);
1130     if (!subsys_sd) {
1131         ret = -ENOENT;
1132         goto out_unlock_fs;
1133     }
1134 
1135     /* Ok, now we can trust subsys/s_item */
1136     ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
1137 
1138 out_unlock_fs:
1139     inode_unlock(d_inode(root));
1140 
1141     /*
1142      * If we succeeded, the fs is pinned via other methods.  If not,
1143      * we're done with it anyway.  So release_fs() is always right.
1144      */
1145     configfs_release_fs();
1146 
1147     return ret;
1148 }
1149 EXPORT_SYMBOL(configfs_depend_item);
1150 
1151 /*
1152  * Release the dependent linkage.  This is much simpler than
1153  * configfs_depend_item() because we know that the client driver is
1154  * pinned, thus the subsystem is pinned, and therefore configfs is pinned.
1155  */
1156 void configfs_undepend_item(struct config_item *target)
1157 {
1158     struct configfs_dirent *sd;
1159 
1160     /*
1161      * Since we can trust everything is pinned, we just need
1162      * configfs_dirent_lock.
1163      */
1164     spin_lock(&configfs_dirent_lock);
1165 
1166     sd = target->ci_dentry->d_fsdata;
1167     BUG_ON(sd->s_dependent_count < 1);
1168 
1169     sd->s_dependent_count -= 1;
1170 
1171     /*
1172      * After this unlock, we cannot trust the item to stay alive!
1173      * DO NOT REFERENCE item after this unlock.
1174      */
1175     spin_unlock(&configfs_dirent_lock);
1176 }
1177 EXPORT_SYMBOL(configfs_undepend_item);
1178 
1179 /*
1180  * caller_subsys is a caller's subsystem not target's. This is used to
1181  * determine if we should lock root and check subsys or not. When we are
1182  * in the same subsystem as our target there is no need to do locking as
1183  * we know that subsys is valid and is not unregistered during this function
1184  * as we are called from callback of one of his children and VFS holds a lock
1185  * on some inode. Otherwise we have to lock our root to  ensure that target's
1186  * subsystem it is not unregistered during this function.
1187  */
1188 int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys,
1189                   struct config_item *target)
1190 {
1191     struct configfs_subsystem *target_subsys;
1192     struct config_group *root, *parent;
1193     struct configfs_dirent *subsys_sd;
1194     int ret = -ENOENT;
1195 
1196     /* Disallow this function for configfs root */
1197     if (configfs_is_root(target))
1198         return -EINVAL;
1199 
1200     parent = target->ci_group;
1201     /*
1202      * This may happen when someone is trying to depend root
1203      * directory of some subsystem
1204      */
1205     if (configfs_is_root(&parent->cg_item)) {
1206         target_subsys = to_configfs_subsystem(to_config_group(target));
1207         root = parent;
1208     } else {
1209         target_subsys = parent->cg_subsys;
1210         /* Find a cofnigfs root as we may need it for locking */
1211         for (root = parent; !configfs_is_root(&root->cg_item);
1212              root = root->cg_item.ci_group)
1213             ;
1214     }
1215 
1216     if (target_subsys != caller_subsys) {
1217         /*
1218          * We are in other configfs subsystem, so we have to do
1219          * additional locking to prevent other subsystem from being
1220          * unregistered
1221          */
1222         inode_lock(d_inode(root->cg_item.ci_dentry));
1223 
1224         /*
1225          * As we are trying to depend item from other subsystem
1226          * we have to check if this subsystem is still registered
1227          */
1228         subsys_sd = configfs_find_subsys_dentry(
1229                 root->cg_item.ci_dentry->d_fsdata,
1230                 &target_subsys->su_group.cg_item);
1231         if (!subsys_sd)
1232             goto out_root_unlock;
1233     } else {
1234         subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata;
1235     }
1236 
1237     /* Now we can execute core of depend item */
1238     ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
1239 
1240     if (target_subsys != caller_subsys)
1241 out_root_unlock:
1242         /*
1243          * We were called from subsystem other than our target so we
1244          * took some locks so now it's time to release them
1245          */
1246         inode_unlock(d_inode(root->cg_item.ci_dentry));
1247 
1248     return ret;
1249 }
1250 EXPORT_SYMBOL(configfs_depend_item_unlocked);
1251 
1252 static int configfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
1253               struct dentry *dentry, umode_t mode)
1254 {
1255     int ret = 0;
1256     int module_got = 0;
1257     struct config_group *group = NULL;
1258     struct config_item *item = NULL;
1259     struct config_item *parent_item;
1260     struct configfs_subsystem *subsys;
1261     struct configfs_dirent *sd;
1262     const struct config_item_type *type;
1263     struct module *subsys_owner = NULL, *new_item_owner = NULL;
1264     struct configfs_fragment *frag;
1265     char *name;
1266 
1267     sd = dentry->d_parent->d_fsdata;
1268 
1269     /*
1270      * Fake invisibility if dir belongs to a group/default groups hierarchy
1271      * being attached
1272      */
1273     if (!configfs_dirent_is_ready(sd)) {
1274         ret = -ENOENT;
1275         goto out;
1276     }
1277 
1278     if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1279         ret = -EPERM;
1280         goto out;
1281     }
1282 
1283     frag = new_fragment();
1284     if (!frag) {
1285         ret = -ENOMEM;
1286         goto out;
1287     }
1288 
1289     /* Get a working ref for the duration of this function */
1290     parent_item = configfs_get_config_item(dentry->d_parent);
1291     type = parent_item->ci_type;
1292     subsys = to_config_group(parent_item)->cg_subsys;
1293     BUG_ON(!subsys);
1294 
1295     if (!type || !type->ct_group_ops ||
1296         (!type->ct_group_ops->make_group &&
1297          !type->ct_group_ops->make_item)) {
1298         ret = -EPERM;  /* Lack-of-mkdir returns -EPERM */
1299         goto out_put;
1300     }
1301 
1302     /*
1303      * The subsystem may belong to a different module than the item
1304      * being created.  We don't want to safely pin the new item but
1305      * fail to pin the subsystem it sits under.
1306      */
1307     if (!subsys->su_group.cg_item.ci_type) {
1308         ret = -EINVAL;
1309         goto out_put;
1310     }
1311     subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1312     if (!try_module_get(subsys_owner)) {
1313         ret = -EINVAL;
1314         goto out_put;
1315     }
1316 
1317     name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1318     if (!name) {
1319         ret = -ENOMEM;
1320         goto out_subsys_put;
1321     }
1322 
1323     snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1324 
1325     mutex_lock(&subsys->su_mutex);
1326     if (type->ct_group_ops->make_group) {
1327         group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1328         if (!group)
1329             group = ERR_PTR(-ENOMEM);
1330         if (!IS_ERR(group)) {
1331             link_group(to_config_group(parent_item), group);
1332             item = &group->cg_item;
1333         } else
1334             ret = PTR_ERR(group);
1335     } else {
1336         item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1337         if (!item)
1338             item = ERR_PTR(-ENOMEM);
1339         if (!IS_ERR(item))
1340             link_obj(parent_item, item);
1341         else
1342             ret = PTR_ERR(item);
1343     }
1344     mutex_unlock(&subsys->su_mutex);
1345 
1346     kfree(name);
1347     if (ret) {
1348         /*
1349          * If ret != 0, then link_obj() was never called.
1350          * There are no extra references to clean up.
1351          */
1352         goto out_subsys_put;
1353     }
1354 
1355     /*
1356      * link_obj() has been called (via link_group() for groups).
1357      * From here on out, errors must clean that up.
1358      */
1359 
1360     type = item->ci_type;
1361     if (!type) {
1362         ret = -EINVAL;
1363         goto out_unlink;
1364     }
1365 
1366     new_item_owner = type->ct_owner;
1367     if (!try_module_get(new_item_owner)) {
1368         ret = -EINVAL;
1369         goto out_unlink;
1370     }
1371 
1372     /*
1373      * I hate doing it this way, but if there is
1374      * an error,  module_put() probably should
1375      * happen after any cleanup.
1376      */
1377     module_got = 1;
1378 
1379     /*
1380      * Make racing rmdir() fail if it did not tag parent with
1381      * CONFIGFS_USET_DROPPING
1382      * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will
1383      * fail and let rmdir() terminate correctly
1384      */
1385     spin_lock(&configfs_dirent_lock);
1386     /* This will make configfs_detach_prep() fail */
1387     sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1388     spin_unlock(&configfs_dirent_lock);
1389 
1390     if (group)
1391         ret = configfs_attach_group(parent_item, item, dentry, frag);
1392     else
1393         ret = configfs_attach_item(parent_item, item, dentry, frag);
1394 
1395     spin_lock(&configfs_dirent_lock);
1396     sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
1397     if (!ret)
1398         configfs_dir_set_ready(dentry->d_fsdata);
1399     spin_unlock(&configfs_dirent_lock);
1400 
1401 out_unlink:
1402     if (ret) {
1403         /* Tear down everything we built up */
1404         mutex_lock(&subsys->su_mutex);
1405 
1406         client_disconnect_notify(parent_item, item);
1407         if (group)
1408             unlink_group(group);
1409         else
1410             unlink_obj(item);
1411         client_drop_item(parent_item, item);
1412 
1413         mutex_unlock(&subsys->su_mutex);
1414 
1415         if (module_got)
1416             module_put(new_item_owner);
1417     }
1418 
1419 out_subsys_put:
1420     if (ret)
1421         module_put(subsys_owner);
1422 
1423 out_put:
1424     /*
1425      * link_obj()/link_group() took a reference from child->parent,
1426      * so the parent is safely pinned.  We can drop our working
1427      * reference.
1428      */
1429     config_item_put(parent_item);
1430     put_fragment(frag);
1431 
1432 out:
1433     return ret;
1434 }
1435 
1436 static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1437 {
1438     struct config_item *parent_item;
1439     struct config_item *item;
1440     struct configfs_subsystem *subsys;
1441     struct configfs_dirent *sd;
1442     struct configfs_fragment *frag;
1443     struct module *subsys_owner = NULL, *dead_item_owner = NULL;
1444     int ret;
1445 
1446     sd = dentry->d_fsdata;
1447     if (sd->s_type & CONFIGFS_USET_DEFAULT)
1448         return -EPERM;
1449 
1450     /* Get a working ref until we have the child */
1451     parent_item = configfs_get_config_item(dentry->d_parent);
1452     subsys = to_config_group(parent_item)->cg_subsys;
1453     BUG_ON(!subsys);
1454 
1455     if (!parent_item->ci_type) {
1456         config_item_put(parent_item);
1457         return -EINVAL;
1458     }
1459 
1460     /* configfs_mkdir() shouldn't have allowed this */
1461     BUG_ON(!subsys->su_group.cg_item.ci_type);
1462     subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1463 
1464     /*
1465      * Ensure that no racing symlink() will make detach_prep() fail while
1466      * the new link is temporarily attached
1467      */
1468     do {
1469         struct dentry *wait;
1470 
1471         mutex_lock(&configfs_symlink_mutex);
1472         spin_lock(&configfs_dirent_lock);
1473         /*
1474          * Here's where we check for dependents.  We're protected by
1475          * configfs_dirent_lock.
1476          * If no dependent, atomically tag the item as dropping.
1477          */
1478         ret = sd->s_dependent_count ? -EBUSY : 0;
1479         if (!ret) {
1480             ret = configfs_detach_prep(dentry, &wait);
1481             if (ret)
1482                 configfs_detach_rollback(dentry);
1483         }
1484         spin_unlock(&configfs_dirent_lock);
1485         mutex_unlock(&configfs_symlink_mutex);
1486 
1487         if (ret) {
1488             if (ret != -EAGAIN) {
1489                 config_item_put(parent_item);
1490                 return ret;
1491             }
1492 
1493             /* Wait until the racing operation terminates */
1494             inode_lock(d_inode(wait));
1495             inode_unlock(d_inode(wait));
1496             dput(wait);
1497         }
1498     } while (ret == -EAGAIN);
1499 
1500     frag = sd->s_frag;
1501     if (down_write_killable(&frag->frag_sem)) {
1502         spin_lock(&configfs_dirent_lock);
1503         configfs_detach_rollback(dentry);
1504         spin_unlock(&configfs_dirent_lock);
1505         config_item_put(parent_item);
1506         return -EINTR;
1507     }
1508     frag->frag_dead = true;
1509     up_write(&frag->frag_sem);
1510 
1511     /* Get a working ref for the duration of this function */
1512     item = configfs_get_config_item(dentry);
1513 
1514     /* Drop reference from above, item already holds one. */
1515     config_item_put(parent_item);
1516 
1517     if (item->ci_type)
1518         dead_item_owner = item->ci_type->ct_owner;
1519 
1520     if (sd->s_type & CONFIGFS_USET_DIR) {
1521         configfs_detach_group(item);
1522 
1523         mutex_lock(&subsys->su_mutex);
1524         client_disconnect_notify(parent_item, item);
1525         unlink_group(to_config_group(item));
1526     } else {
1527         configfs_detach_item(item);
1528 
1529         mutex_lock(&subsys->su_mutex);
1530         client_disconnect_notify(parent_item, item);
1531         unlink_obj(item);
1532     }
1533 
1534     client_drop_item(parent_item, item);
1535     mutex_unlock(&subsys->su_mutex);
1536 
1537     /* Drop our reference from above */
1538     config_item_put(item);
1539 
1540     module_put(dead_item_owner);
1541     module_put(subsys_owner);
1542 
1543     return 0;
1544 }
1545 
1546 const struct inode_operations configfs_dir_inode_operations = {
1547     .mkdir      = configfs_mkdir,
1548     .rmdir      = configfs_rmdir,
1549     .symlink    = configfs_symlink,
1550     .unlink     = configfs_unlink,
1551     .lookup     = configfs_lookup,
1552     .setattr    = configfs_setattr,
1553 };
1554 
1555 const struct inode_operations configfs_root_inode_operations = {
1556     .lookup     = configfs_lookup,
1557     .setattr    = configfs_setattr,
1558 };
1559 
1560 static int configfs_dir_open(struct inode *inode, struct file *file)
1561 {
1562     struct dentry * dentry = file->f_path.dentry;
1563     struct configfs_dirent * parent_sd = dentry->d_fsdata;
1564     int err;
1565 
1566     inode_lock(d_inode(dentry));
1567     /*
1568      * Fake invisibility if dir belongs to a group/default groups hierarchy
1569      * being attached
1570      */
1571     err = -ENOENT;
1572     if (configfs_dirent_is_ready(parent_sd)) {
1573         file->private_data = configfs_new_dirent(parent_sd, NULL, 0, NULL);
1574         if (IS_ERR(file->private_data))
1575             err = PTR_ERR(file->private_data);
1576         else
1577             err = 0;
1578     }
1579     inode_unlock(d_inode(dentry));
1580 
1581     return err;
1582 }
1583 
1584 static int configfs_dir_close(struct inode *inode, struct file *file)
1585 {
1586     struct dentry * dentry = file->f_path.dentry;
1587     struct configfs_dirent * cursor = file->private_data;
1588 
1589     inode_lock(d_inode(dentry));
1590     spin_lock(&configfs_dirent_lock);
1591     list_del_init(&cursor->s_sibling);
1592     spin_unlock(&configfs_dirent_lock);
1593     inode_unlock(d_inode(dentry));
1594 
1595     release_configfs_dirent(cursor);
1596 
1597     return 0;
1598 }
1599 
1600 /* Relationship between s_mode and the DT_xxx types */
1601 static inline unsigned char dt_type(struct configfs_dirent *sd)
1602 {
1603     return (sd->s_mode >> 12) & 15;
1604 }
1605 
1606 static int configfs_readdir(struct file *file, struct dir_context *ctx)
1607 {
1608     struct dentry *dentry = file->f_path.dentry;
1609     struct super_block *sb = dentry->d_sb;
1610     struct configfs_dirent * parent_sd = dentry->d_fsdata;
1611     struct configfs_dirent *cursor = file->private_data;
1612     struct list_head *p, *q = &cursor->s_sibling;
1613     ino_t ino = 0;
1614 
1615     if (!dir_emit_dots(file, ctx))
1616         return 0;
1617     spin_lock(&configfs_dirent_lock);
1618     if (ctx->pos == 2)
1619         list_move(q, &parent_sd->s_children);
1620     for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1621         struct configfs_dirent *next;
1622         const char *name;
1623         int len;
1624         struct inode *inode = NULL;
1625 
1626         next = list_entry(p, struct configfs_dirent, s_sibling);
1627         if (!next->s_element)
1628             continue;
1629 
1630         /*
1631          * We'll have a dentry and an inode for
1632          * PINNED items and for open attribute
1633          * files.  We lock here to prevent a race
1634          * with configfs_d_iput() clearing
1635          * s_dentry before calling iput().
1636          *
1637          * Why do we go to the trouble?  If
1638          * someone has an attribute file open,
1639          * the inode number should match until
1640          * they close it.  Beyond that, we don't
1641          * care.
1642          */
1643         dentry = next->s_dentry;
1644         if (dentry)
1645             inode = d_inode(dentry);
1646         if (inode)
1647             ino = inode->i_ino;
1648         spin_unlock(&configfs_dirent_lock);
1649         if (!inode)
1650             ino = iunique(sb, 2);
1651 
1652         name = configfs_get_name(next);
1653         len = strlen(name);
1654 
1655         if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1656             return 0;
1657 
1658         spin_lock(&configfs_dirent_lock);
1659         list_move(q, p);
1660         p = q;
1661         ctx->pos++;
1662     }
1663     spin_unlock(&configfs_dirent_lock);
1664     return 0;
1665 }
1666 
1667 static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
1668 {
1669     struct dentry * dentry = file->f_path.dentry;
1670 
1671     switch (whence) {
1672         case 1:
1673             offset += file->f_pos;
1674             fallthrough;
1675         case 0:
1676             if (offset >= 0)
1677                 break;
1678             fallthrough;
1679         default:
1680             return -EINVAL;
1681     }
1682     if (offset != file->f_pos) {
1683         file->f_pos = offset;
1684         if (file->f_pos >= 2) {
1685             struct configfs_dirent *sd = dentry->d_fsdata;
1686             struct configfs_dirent *cursor = file->private_data;
1687             struct list_head *p;
1688             loff_t n = file->f_pos - 2;
1689 
1690             spin_lock(&configfs_dirent_lock);
1691             list_del(&cursor->s_sibling);
1692             p = sd->s_children.next;
1693             while (n && p != &sd->s_children) {
1694                 struct configfs_dirent *next;
1695                 next = list_entry(p, struct configfs_dirent,
1696                            s_sibling);
1697                 if (next->s_element)
1698                     n--;
1699                 p = p->next;
1700             }
1701             list_add_tail(&cursor->s_sibling, p);
1702             spin_unlock(&configfs_dirent_lock);
1703         }
1704     }
1705     return offset;
1706 }
1707 
1708 const struct file_operations configfs_dir_operations = {
1709     .open       = configfs_dir_open,
1710     .release    = configfs_dir_close,
1711     .llseek     = configfs_dir_lseek,
1712     .read       = generic_read_dir,
1713     .iterate_shared = configfs_readdir,
1714 };
1715 
1716 /**
1717  * configfs_register_group - creates a parent-child relation between two groups
1718  * @parent_group:   parent group
1719  * @group:      child group
1720  *
1721  * link groups, creates dentry for the child and attaches it to the
1722  * parent dentry.
1723  *
1724  * Return: 0 on success, negative errno code on error
1725  */
1726 int configfs_register_group(struct config_group *parent_group,
1727                 struct config_group *group)
1728 {
1729     struct configfs_subsystem *subsys = parent_group->cg_subsys;
1730     struct dentry *parent;
1731     struct configfs_fragment *frag;
1732     int ret;
1733 
1734     frag = new_fragment();
1735     if (!frag)
1736         return -ENOMEM;
1737 
1738     mutex_lock(&subsys->su_mutex);
1739     link_group(parent_group, group);
1740     mutex_unlock(&subsys->su_mutex);
1741 
1742     parent = parent_group->cg_item.ci_dentry;
1743 
1744     inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
1745     ret = create_default_group(parent_group, group, frag);
1746     if (ret)
1747         goto err_out;
1748 
1749     spin_lock(&configfs_dirent_lock);
1750     configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata);
1751     spin_unlock(&configfs_dirent_lock);
1752     inode_unlock(d_inode(parent));
1753     put_fragment(frag);
1754     return 0;
1755 err_out:
1756     inode_unlock(d_inode(parent));
1757     mutex_lock(&subsys->su_mutex);
1758     unlink_group(group);
1759     mutex_unlock(&subsys->su_mutex);
1760     put_fragment(frag);
1761     return ret;
1762 }
1763 EXPORT_SYMBOL(configfs_register_group);
1764 
1765 /**
1766  * configfs_unregister_group() - unregisters a child group from its parent
1767  * @group: parent group to be unregistered
1768  *
1769  * Undoes configfs_register_group()
1770  */
1771 void configfs_unregister_group(struct config_group *group)
1772 {
1773     struct configfs_subsystem *subsys = group->cg_subsys;
1774     struct dentry *dentry = group->cg_item.ci_dentry;
1775     struct dentry *parent = group->cg_item.ci_parent->ci_dentry;
1776     struct configfs_dirent *sd = dentry->d_fsdata;
1777     struct configfs_fragment *frag = sd->s_frag;
1778 
1779     down_write(&frag->frag_sem);
1780     frag->frag_dead = true;
1781     up_write(&frag->frag_sem);
1782 
1783     inode_lock_nested(d_inode(parent), I_MUTEX_PARENT);
1784     spin_lock(&configfs_dirent_lock);
1785     configfs_detach_prep(dentry, NULL);
1786     spin_unlock(&configfs_dirent_lock);
1787 
1788     configfs_detach_group(&group->cg_item);
1789     d_inode(dentry)->i_flags |= S_DEAD;
1790     dont_mount(dentry);
1791     d_drop(dentry);
1792     fsnotify_rmdir(d_inode(parent), dentry);
1793     inode_unlock(d_inode(parent));
1794 
1795     dput(dentry);
1796 
1797     mutex_lock(&subsys->su_mutex);
1798     unlink_group(group);
1799     mutex_unlock(&subsys->su_mutex);
1800 }
1801 EXPORT_SYMBOL(configfs_unregister_group);
1802 
1803 /**
1804  * configfs_register_default_group() - allocates and registers a child group
1805  * @parent_group:   parent group
1806  * @name:       child group name
1807  * @item_type:      child item type description
1808  *
1809  * boilerplate to allocate and register a child group with its parent. We need
1810  * kzalloc'ed memory because child's default_group is initially empty.
1811  *
1812  * Return: allocated config group or ERR_PTR() on error
1813  */
1814 struct config_group *
1815 configfs_register_default_group(struct config_group *parent_group,
1816                 const char *name,
1817                 const struct config_item_type *item_type)
1818 {
1819     int ret;
1820     struct config_group *group;
1821 
1822     group = kzalloc(sizeof(*group), GFP_KERNEL);
1823     if (!group)
1824         return ERR_PTR(-ENOMEM);
1825     config_group_init_type_name(group, name, item_type);
1826 
1827     ret = configfs_register_group(parent_group, group);
1828     if (ret) {
1829         kfree(group);
1830         return ERR_PTR(ret);
1831     }
1832     return group;
1833 }
1834 EXPORT_SYMBOL(configfs_register_default_group);
1835 
1836 /**
1837  * configfs_unregister_default_group() - unregisters and frees a child group
1838  * @group:  the group to act on
1839  */
1840 void configfs_unregister_default_group(struct config_group *group)
1841 {
1842     configfs_unregister_group(group);
1843     kfree(group);
1844 }
1845 EXPORT_SYMBOL(configfs_unregister_default_group);
1846 
1847 int configfs_register_subsystem(struct configfs_subsystem *subsys)
1848 {
1849     int err;
1850     struct config_group *group = &subsys->su_group;
1851     struct dentry *dentry;
1852     struct dentry *root;
1853     struct configfs_dirent *sd;
1854     struct configfs_fragment *frag;
1855 
1856     frag = new_fragment();
1857     if (!frag)
1858         return -ENOMEM;
1859 
1860     root = configfs_pin_fs();
1861     if (IS_ERR(root)) {
1862         put_fragment(frag);
1863         return PTR_ERR(root);
1864     }
1865 
1866     if (!group->cg_item.ci_name)
1867         group->cg_item.ci_name = group->cg_item.ci_namebuf;
1868 
1869     sd = root->d_fsdata;
1870     mutex_lock(&configfs_subsystem_mutex);
1871     link_group(to_config_group(sd->s_element), group);
1872     mutex_unlock(&configfs_subsystem_mutex);
1873 
1874     inode_lock_nested(d_inode(root), I_MUTEX_PARENT);
1875 
1876     err = -ENOMEM;
1877     dentry = d_alloc_name(root, group->cg_item.ci_name);
1878     if (dentry) {
1879         d_add(dentry, NULL);
1880 
1881         err = configfs_attach_group(sd->s_element, &group->cg_item,
1882                         dentry, frag);
1883         if (err) {
1884             BUG_ON(d_inode(dentry));
1885             d_drop(dentry);
1886             dput(dentry);
1887         } else {
1888             spin_lock(&configfs_dirent_lock);
1889             configfs_dir_set_ready(dentry->d_fsdata);
1890             spin_unlock(&configfs_dirent_lock);
1891         }
1892     }
1893 
1894     inode_unlock(d_inode(root));
1895 
1896     if (err) {
1897         mutex_lock(&configfs_subsystem_mutex);
1898         unlink_group(group);
1899         mutex_unlock(&configfs_subsystem_mutex);
1900         configfs_release_fs();
1901     }
1902     put_fragment(frag);
1903 
1904     return err;
1905 }
1906 
1907 void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1908 {
1909     struct config_group *group = &subsys->su_group;
1910     struct dentry *dentry = group->cg_item.ci_dentry;
1911     struct dentry *root = dentry->d_sb->s_root;
1912     struct configfs_dirent *sd = dentry->d_fsdata;
1913     struct configfs_fragment *frag = sd->s_frag;
1914 
1915     if (dentry->d_parent != root) {
1916         pr_err("Tried to unregister non-subsystem!\n");
1917         return;
1918     }
1919 
1920     down_write(&frag->frag_sem);
1921     frag->frag_dead = true;
1922     up_write(&frag->frag_sem);
1923 
1924     inode_lock_nested(d_inode(root),
1925               I_MUTEX_PARENT);
1926     inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD);
1927     mutex_lock(&configfs_symlink_mutex);
1928     spin_lock(&configfs_dirent_lock);
1929     if (configfs_detach_prep(dentry, NULL)) {
1930         pr_err("Tried to unregister non-empty subsystem!\n");
1931     }
1932     spin_unlock(&configfs_dirent_lock);
1933     mutex_unlock(&configfs_symlink_mutex);
1934     configfs_detach_group(&group->cg_item);
1935     d_inode(dentry)->i_flags |= S_DEAD;
1936     dont_mount(dentry);
1937     inode_unlock(d_inode(dentry));
1938 
1939     d_drop(dentry);
1940     fsnotify_rmdir(d_inode(root), dentry);
1941 
1942     inode_unlock(d_inode(root));
1943 
1944     dput(dentry);
1945 
1946     mutex_lock(&configfs_subsystem_mutex);
1947     unlink_group(group);
1948     mutex_unlock(&configfs_subsystem_mutex);
1949     configfs_release_fs();
1950 }
1951 
1952 EXPORT_SYMBOL(configfs_register_subsystem);
1953 EXPORT_SYMBOL(configfs_unregister_subsystem);