0001
0002
0003
0004
0005
0006
0007
0008
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
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 DEFINE_SPINLOCK(configfs_dirent_lock);
0036
0037
0038
0039
0040
0041
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
0052 spin_lock(&configfs_dirent_lock);
0053
0054
0055
0056
0057
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
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
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
0113
0114
0115
0116
0117
0118
0119
0120
0121 if (sd->s_depth == -1)
0122
0123
0124
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
0133 sd->s_depth = -1;
0134 }
0135
0136 #else
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
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
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
0219
0220
0221
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
0274
0275
0276
0277
0278
0279
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
0310 inc_nlink(inode);
0311 d_instantiate(dentry, inode);
0312
0313 dget(dentry);
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
0325
0326
0327
0328
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
0342
0343
0344
0345
0346
0347
0348
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);
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
0405
0406
0407
0408
0409
0410
0411
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
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
0441
0442
0443
0444
0445
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
0484
0485
0486
0487
0488
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
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
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
0518
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
0535
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
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
0646
0647 dput(dentry);
0648 }
0649
0650
0651
0652
0653
0654
0655
0656
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
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
0720
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
0735 config_item_put(item);
0736
0737
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
0746
0747
0748 item->ci_parent = parent_item;
0749
0750
0751
0752
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
0759
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;
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
0796
0797
0798
0799
0800
0801
0802
0803
0804
0805
0806
0807
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
0822
0823
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
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
0859
0860
0861
0862
0863
0864
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
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
0892
0893
0894
0895
0896
0897
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
0914
0915
0916
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
0928
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
0976
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987
0988
0989
0990
0991
0992
0993
0994
0995
0996
0997
0998
0999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
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)
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;
1052 }
1053 }
1054
1055
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
1070 ret = configfs_depend_prep(subsys_dentry, target);
1071 if (ret)
1072 goto out_unlock_dirent_lock;
1073
1074
1075
1076
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
1116
1117
1118 root = configfs_pin_fs();
1119 if (IS_ERR(root))
1120 return PTR_ERR(root);
1121
1122
1123
1124
1125
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
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
1143
1144
1145 configfs_release_fs();
1146
1147 return ret;
1148 }
1149 EXPORT_SYMBOL(configfs_depend_item);
1150
1151
1152
1153
1154
1155
1156 void configfs_undepend_item(struct config_item *target)
1157 {
1158 struct configfs_dirent *sd;
1159
1160
1161
1162
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
1173
1174
1175 spin_unlock(&configfs_dirent_lock);
1176 }
1177 EXPORT_SYMBOL(configfs_undepend_item);
1178
1179
1180
1181
1182
1183
1184
1185
1186
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
1197 if (configfs_is_root(target))
1198 return -EINVAL;
1199
1200 parent = target->ci_group;
1201
1202
1203
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
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
1219
1220
1221
1222 inode_lock(d_inode(root->cg_item.ci_dentry));
1223
1224
1225
1226
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
1238 ret = configfs_do_depend_item(subsys_sd->s_dentry, target);
1239
1240 if (target_subsys != caller_subsys)
1241 out_root_unlock:
1242
1243
1244
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
1271
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
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;
1299 goto out_put;
1300 }
1301
1302
1303
1304
1305
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
1350
1351
1352 goto out_subsys_put;
1353 }
1354
1355
1356
1357
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
1374
1375
1376
1377 module_got = 1;
1378
1379
1380
1381
1382
1383
1384
1385 spin_lock(&configfs_dirent_lock);
1386
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
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
1426
1427
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
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
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
1466
1467
1468 do {
1469 struct dentry *wait;
1470
1471 mutex_lock(&configfs_symlink_mutex);
1472 spin_lock(&configfs_dirent_lock);
1473
1474
1475
1476
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
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
1512 item = configfs_get_config_item(dentry);
1513
1514
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
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
1569
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
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
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
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
1718
1719
1720
1721
1722
1723
1724
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
1767
1768
1769
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
1805
1806
1807
1808
1809
1810
1811
1812
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
1838
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);