1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* -*- mode: c; c-basic-offset: 8; -*- 3 * vim: noexpandtab sw=8 ts=8 sts=0: 4 * 5 * dir.c - Operations for configfs directories. 6 * 7 * Based on sysfs: 8 * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel 9 * 10 * configfs Copyright (C) 2005 Oracle. All rights reserved. 11 */ 12 13 #undef DEBUG 14 15 #include <linux/fs.h> 16 #include <linux/mount.h> 17 #include <linux/module.h> 18 #include <linux/slab.h> 19 #include <linux/err.h> 20 21 #include <linux/configfs.h> 22 #include "configfs_internal.h" 23 24 DECLARE_RWSEM(configfs_rename_sem); 25 /* 26 * Protects mutations of configfs_dirent linkage together with proper i_mutex 27 * Also protects mutations of symlinks linkage to target configfs_dirent 28 * Mutators of configfs_dirent linkage must *both* have the proper inode locked 29 * and configfs_dirent_lock locked, in that order. 30 * This allows one to safely traverse configfs_dirent trees and symlinks without 31 * having to lock inodes. 32 * 33 * Protects setting of CONFIGFS_USET_DROPPING: checking the flag 34 * unlocked is not reliable unless in detach_groups() called from 35 * rmdir()/unregister() and from configfs_attach_group() 36 */ 37 DEFINE_SPINLOCK(configfs_dirent_lock); 38 39 static void configfs_d_iput(struct dentry * dentry, 40 struct inode * inode) 41 { 42 struct configfs_dirent *sd = dentry->d_fsdata; 43 44 if (sd) { 45 /* Coordinate with configfs_readdir */ 46 spin_lock(&configfs_dirent_lock); 47 /* 48 * Set sd->s_dentry to null only when this dentry is the one 49 * that is going to be killed. Otherwise configfs_d_iput may 50 * run just after configfs_attach_attr and set sd->s_dentry to 51 * NULL even it's still in use. 52 */ 53 if (sd->s_dentry == dentry) 54 sd->s_dentry = NULL; 55 56 spin_unlock(&configfs_dirent_lock); 57 configfs_put(sd); 58 } 59 iput(inode); 60 } 61 62 const struct dentry_operations configfs_dentry_ops = { 63 .d_iput = configfs_d_iput, 64 .d_delete = always_delete_dentry, 65 }; 66 67 #ifdef CONFIG_LOCKDEP 68 69 /* 70 * Helpers to make lockdep happy with our recursive locking of default groups' 71 * inodes (see configfs_attach_group() and configfs_detach_group()). 72 * We put default groups i_mutexes in separate classes according to their depth 73 * from the youngest non-default group ancestor. 74 * 75 * For a non-default group A having default groups A/B, A/C, and A/C/D, default 76 * groups A/B and A/C will have their inode's mutex in class 77 * default_group_class[0], and default group A/C/D will be in 78 * default_group_class[1]. 79 * 80 * The lock classes are declared and assigned in inode.c, according to the 81 * s_depth value. 82 * The s_depth value is initialized to -1, adjusted to >= 0 when attaching 83 * default groups, and reset to -1 when all default groups are attached. During 84 * attachment, if configfs_create() sees s_depth > 0, the lock class of the new 85 * inode's mutex is set to default_group_class[s_depth - 1]. 86 */ 87 88 static void configfs_init_dirent_depth(struct configfs_dirent *sd) 89 { 90 sd->s_depth = -1; 91 } 92 93 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd, 94 struct configfs_dirent *sd) 95 { 96 int parent_depth = parent_sd->s_depth; 97 98 if (parent_depth >= 0) 99 sd->s_depth = parent_depth + 1; 100 } 101 102 static void 103 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd) 104 { 105 /* 106 * item's i_mutex class is already setup, so s_depth is now only 107 * used to set new sub-directories s_depth, which is always done 108 * with item's i_mutex locked. 109 */ 110 /* 111 * sd->s_depth == -1 iff we are a non default group. 112 * else (we are a default group) sd->s_depth > 0 (see 113 * create_dir()). 114 */ 115 if (sd->s_depth == -1) 116 /* 117 * We are a non default group and we are going to create 118 * default groups. 119 */ 120 sd->s_depth = 0; 121 } 122 123 static void 124 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd) 125 { 126 /* We will not create default groups anymore. */ 127 sd->s_depth = -1; 128 } 129 130 #else /* CONFIG_LOCKDEP */ 131 132 static void configfs_init_dirent_depth(struct configfs_dirent *sd) 133 { 134 } 135 136 static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd, 137 struct configfs_dirent *sd) 138 { 139 } 140 141 static void 142 configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd) 143 { 144 } 145 146 static void 147 configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd) 148 { 149 } 150 151 #endif /* CONFIG_LOCKDEP */ 152 153 /* 154 * Allocates a new configfs_dirent and links it to the parent configfs_dirent 155 */ 156 static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd, 157 void *element, int type) 158 { 159 struct configfs_dirent * sd; 160 161 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL); 162 if (!sd) 163 return ERR_PTR(-ENOMEM); 164 165 atomic_set(&sd->s_count, 1); 166 INIT_LIST_HEAD(&sd->s_links); 167 INIT_LIST_HEAD(&sd->s_children); 168 sd->s_element = element; 169 sd->s_type = type; 170 configfs_init_dirent_depth(sd); 171 spin_lock(&configfs_dirent_lock); 172 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) { 173 spin_unlock(&configfs_dirent_lock); 174 kmem_cache_free(configfs_dir_cachep, sd); 175 return ERR_PTR(-ENOENT); 176 } 177 list_add(&sd->s_sibling, &parent_sd->s_children); 178 spin_unlock(&configfs_dirent_lock); 179 180 return sd; 181 } 182 183 /* 184 * 185 * Return -EEXIST if there is already a configfs element with the same 186 * name for the same parent. 187 * 188 * called with parent inode's i_mutex held 189 */ 190 static int configfs_dirent_exists(struct configfs_dirent *parent_sd, 191 const unsigned char *new) 192 { 193 struct configfs_dirent * sd; 194 195 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { 196 if (sd->s_element) { 197 const unsigned char *existing = configfs_get_name(sd); 198 if (strcmp(existing, new)) 199 continue; 200 else 201 return -EEXIST; 202 } 203 } 204 205 return 0; 206 } 207 208 209 int configfs_make_dirent(struct configfs_dirent * parent_sd, 210 struct dentry * dentry, void * element, 211 umode_t mode, int type) 212 { 213 struct configfs_dirent * sd; 214 215 sd = configfs_new_dirent(parent_sd, element, type); 216 if (IS_ERR(sd)) 217 return PTR_ERR(sd); 218 219 sd->s_mode = mode; 220 sd->s_dentry = dentry; 221 if (dentry) 222 dentry->d_fsdata = configfs_get(sd); 223 224 return 0; 225 } 226 227 static void init_dir(struct inode * inode) 228 { 229 inode->i_op = &configfs_dir_inode_operations; 230 inode->i_fop = &configfs_dir_operations; 231 232 /* directory inodes start off with i_nlink == 2 (for "." entry) */ 233 inc_nlink(inode); 234 } 235 236 static void configfs_init_file(struct inode * inode) 237 { 238 inode->i_size = PAGE_SIZE; 239 inode->i_fop = &configfs_file_operations; 240 } 241 242 static void configfs_init_bin_file(struct inode *inode) 243 { 244 inode->i_size = 0; 245 inode->i_fop = &configfs_bin_file_operations; 246 } 247 248 static void init_symlink(struct inode * inode) 249 { 250 inode->i_op = &configfs_symlink_inode_operations; 251 } 252 253 /** 254 * configfs_create_dir - create a directory for an config_item. 255 * @item: config_itemwe're creating directory for. 256 * @dentry: config_item's dentry. 257 * 258 * Note: user-created entries won't be allowed under this new directory 259 * until it is validated by configfs_dir_set_ready() 260 */ 261 262 static int configfs_create_dir(struct config_item *item, struct dentry *dentry) 263 { 264 int error; 265 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; 266 struct dentry *p = dentry->d_parent; 267 268 BUG_ON(!item); 269 270 error = configfs_dirent_exists(p->d_fsdata, dentry->d_name.name); 271 if (unlikely(error)) 272 return error; 273 274 error = configfs_make_dirent(p->d_fsdata, dentry, item, mode, 275 CONFIGFS_DIR | CONFIGFS_USET_CREATING); 276 if (unlikely(error)) 277 return error; 278 279 configfs_set_dir_dirent_depth(p->d_fsdata, dentry->d_fsdata); 280 error = configfs_create(dentry, mode, init_dir); 281 if (!error) { 282 inc_nlink(d_inode(p)); 283 item->ci_dentry = dentry; 284 } else { 285 struct configfs_dirent *sd = dentry->d_fsdata; 286 if (sd) { 287 spin_lock(&configfs_dirent_lock); 288 list_del_init(&sd->s_sibling); 289 spin_unlock(&configfs_dirent_lock); 290 configfs_put(sd); 291 } 292 } 293 return error; 294 } 295 296 /* 297 * Allow userspace to create new entries under a new directory created with 298 * configfs_create_dir(), and under all of its chidlren directories recursively. 299 * @sd configfs_dirent of the new directory to validate 300 * 301 * Caller must hold configfs_dirent_lock. 302 */ 303 static void configfs_dir_set_ready(struct configfs_dirent *sd) 304 { 305 struct configfs_dirent *child_sd; 306 307 sd->s_type &= ~CONFIGFS_USET_CREATING; 308 list_for_each_entry(child_sd, &sd->s_children, s_sibling) 309 if (child_sd->s_type & CONFIGFS_USET_CREATING) 310 configfs_dir_set_ready(child_sd); 311 } 312 313 /* 314 * Check that a directory does not belong to a directory hierarchy being 315 * attached and not validated yet. 316 * @sd configfs_dirent of the directory to check 317 * 318 * @return non-zero iff the directory was validated 319 * 320 * Note: takes configfs_dirent_lock, so the result may change from false to true 321 * in two consecutive calls, but never from true to false. 322 */ 323 int configfs_dirent_is_ready(struct configfs_dirent *sd) 324 { 325 int ret; 326 327 spin_lock(&configfs_dirent_lock); 328 ret = !(sd->s_type & CONFIGFS_USET_CREATING); 329 spin_unlock(&configfs_dirent_lock); 330 331 return ret; 332 } 333 334 int configfs_create_link(struct configfs_symlink *sl, 335 struct dentry *parent, 336 struct dentry *dentry) 337 { 338 int err = 0; 339 umode_t mode = S_IFLNK | S_IRWXUGO; 340 341 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode, 342 CONFIGFS_ITEM_LINK); 343 if (!err) { 344 err = configfs_create(dentry, mode, init_symlink); 345 if (err) { 346 struct configfs_dirent *sd = dentry->d_fsdata; 347 if (sd) { 348 spin_lock(&configfs_dirent_lock); 349 list_del_init(&sd->s_sibling); 350 spin_unlock(&configfs_dirent_lock); 351 configfs_put(sd); 352 } 353 } 354 } 355 return err; 356 } 357 358 static void remove_dir(struct dentry * d) 359 { 360 struct dentry * parent = dget(d->d_parent); 361 struct configfs_dirent * sd; 362 363 sd = d->d_fsdata; 364 spin_lock(&configfs_dirent_lock); 365 list_del_init(&sd->s_sibling); 366 spin_unlock(&configfs_dirent_lock); 367 configfs_put(sd); 368 if (d_really_is_positive(d)) 369 simple_rmdir(d_inode(parent),d); 370 371 pr_debug(" o %pd removing done (%d)\n", d, d_count(d)); 372 373 dput(parent); 374 } 375 376 /** 377 * configfs_remove_dir - remove an config_item's directory. 378 * @item: config_item we're removing. 379 * 380 * The only thing special about this is that we remove any files in 381 * the directory before we remove the directory, and we've inlined 382 * what used to be configfs_rmdir() below, instead of calling separately. 383 * 384 * Caller holds the mutex of the item's inode 385 */ 386 387 static void configfs_remove_dir(struct config_item * item) 388 { 389 struct dentry * dentry = dget(item->ci_dentry); 390 391 if (!dentry) 392 return; 393 394 remove_dir(dentry); 395 /** 396 * Drop reference from dget() on entrance. 397 */ 398 dput(dentry); 399 } 400 401 402 /* attaches attribute's configfs_dirent to the dentry corresponding to the 403 * attribute file 404 */ 405 static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry) 406 { 407 struct configfs_attribute * attr = sd->s_element; 408 int error; 409 410 spin_lock(&configfs_dirent_lock); 411 dentry->d_fsdata = configfs_get(sd); 412 sd->s_dentry = dentry; 413 spin_unlock(&configfs_dirent_lock); 414 415 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG, 416 (sd->s_type & CONFIGFS_ITEM_BIN_ATTR) ? 417 configfs_init_bin_file : 418 configfs_init_file); 419 if (error) 420 configfs_put(sd); 421 return error; 422 } 423 424 static struct dentry * configfs_lookup(struct inode *dir, 425 struct dentry *dentry, 426 unsigned int flags) 427 { 428 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata; 429 struct configfs_dirent * sd; 430 int found = 0; 431 int err; 432 433 /* 434 * Fake invisibility if dir belongs to a group/default groups hierarchy 435 * being attached 436 * 437 * This forbids userspace to read/write attributes of items which may 438 * not complete their initialization, since the dentries of the 439 * attributes won't be instantiated. 440 */ 441 err = -ENOENT; 442 if (!configfs_dirent_is_ready(parent_sd)) 443 goto out; 444 445 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { 446 if (sd->s_type & CONFIGFS_NOT_PINNED) { 447 const unsigned char * name = configfs_get_name(sd); 448 449 if (strcmp(name, dentry->d_name.name)) 450 continue; 451 452 found = 1; 453 err = configfs_attach_attr(sd, dentry); 454 break; 455 } 456 } 457 458 if (!found) { 459 /* 460 * If it doesn't exist and it isn't a NOT_PINNED item, 461 * it must be negative. 462 */ 463 if (dentry->d_name.len > NAME_MAX) 464 return ERR_PTR(-ENAMETOOLONG); 465 d_add(dentry, NULL); 466 return NULL; 467 } 468 469 out: 470 return ERR_PTR(err); 471 } 472 473 /* 474 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are 475 * attributes and are removed by rmdir(). We recurse, setting 476 * CONFIGFS_USET_DROPPING on all children that are candidates for 477 * default detach. 478 * If there is an error, the caller will reset the flags via 479 * configfs_detach_rollback(). 480 */ 481 static int configfs_detach_prep(struct dentry *dentry, struct dentry **wait) 482 { 483 struct configfs_dirent *parent_sd = dentry->d_fsdata; 484 struct configfs_dirent *sd; 485 int ret; 486 487 /* Mark that we're trying to drop the group */ 488 parent_sd->s_type |= CONFIGFS_USET_DROPPING; 489 490 ret = -EBUSY; 491 if (!list_empty(&parent_sd->s_links)) 492 goto out; 493 494 ret = 0; 495 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { 496 if (!sd->s_element || 497 (sd->s_type & CONFIGFS_NOT_PINNED)) 498 continue; 499 if (sd->s_type & CONFIGFS_USET_DEFAULT) { 500 /* Abort if racing with mkdir() */ 501 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) { 502 if (wait) 503 *wait= dget(sd->s_dentry); 504 return -EAGAIN; 505 } 506 507 /* 508 * Yup, recursive. If there's a problem, blame 509 * deep nesting of default_groups 510 */ 511 ret = configfs_detach_prep(sd->s_dentry, wait); 512 if (!ret) 513 continue; 514 } else 515 ret = -ENOTEMPTY; 516 517 break; 518 } 519 520 out: 521 return ret; 522 } 523 524 /* 525 * Walk the tree, resetting CONFIGFS_USET_DROPPING wherever it was 526 * set. 527 */ 528 static void configfs_detach_rollback(struct dentry *dentry) 529 { 530 struct configfs_dirent *parent_sd = dentry->d_fsdata; 531 struct configfs_dirent *sd; 532 533 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING; 534 535 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) 536 if (sd->s_type & CONFIGFS_USET_DEFAULT) 537 configfs_detach_rollback(sd->s_dentry); 538 } 539 540 static void detach_attrs(struct config_item * item) 541 { 542 struct dentry * dentry = dget(item->ci_dentry); 543 struct configfs_dirent * parent_sd; 544 struct configfs_dirent * sd, * tmp; 545 546 if (!dentry) 547 return; 548 549 pr_debug("configfs %s: dropping attrs for dir\n", 550 dentry->d_name.name); 551 552 parent_sd = dentry->d_fsdata; 553 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) { 554 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED)) 555 continue; 556 spin_lock(&configfs_dirent_lock); 557 list_del_init(&sd->s_sibling); 558 spin_unlock(&configfs_dirent_lock); 559 configfs_drop_dentry(sd, dentry); 560 configfs_put(sd); 561 } 562 563 /** 564 * Drop reference from dget() on entrance. 565 */ 566 dput(dentry); 567 } 568 569 static int populate_attrs(struct config_item *item) 570 { 571 const struct config_item_type *t = item->ci_type; 572 struct configfs_attribute *attr; 573 struct configfs_bin_attribute *bin_attr; 574 int error = 0; 575 int i; 576 577 if (!t) 578 return -EINVAL; 579 if (t->ct_attrs) { 580 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) { 581 if ((error = configfs_create_file(item, attr))) 582 break; 583 } 584 } 585 if (t->ct_bin_attrs) { 586 for (i = 0; (bin_attr = t->ct_bin_attrs[i]) != NULL; i++) { 587 error = configfs_create_bin_file(item, bin_attr); 588 if (error) 589 break; 590 } 591 } 592 593 if (error) 594 detach_attrs(item); 595 596 return error; 597 } 598 599 static int configfs_attach_group(struct config_item *parent_item, 600 struct config_item *item, 601 struct dentry *dentry); 602 static void configfs_detach_group(struct config_item *item); 603 604 static void detach_groups(struct config_group *group) 605 { 606 struct dentry * dentry = dget(group->cg_item.ci_dentry); 607 struct dentry *child; 608 struct configfs_dirent *parent_sd; 609 struct configfs_dirent *sd, *tmp; 610 611 if (!dentry) 612 return; 613 614 parent_sd = dentry->d_fsdata; 615 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) { 616 if (!sd->s_element || 617 !(sd->s_type & CONFIGFS_USET_DEFAULT)) 618 continue; 619 620 child = sd->s_dentry; 621 622 inode_lock(d_inode(child)); 623 624 configfs_detach_group(sd->s_element); 625 d_inode(child)->i_flags |= S_DEAD; 626 dont_mount(child); 627 628 inode_unlock(d_inode(child)); 629 630 d_delete(child); 631 dput(child); 632 } 633 634 /** 635 * Drop reference from dget() on entrance. 636 */ 637 dput(dentry); 638 } 639 640 /* 641 * This fakes mkdir(2) on a default_groups[] entry. It 642 * creates a dentry, attachs it, and then does fixup 643 * on the sd->s_type. 644 * 645 * We could, perhaps, tweak our parent's ->mkdir for a minute and 646 * try using vfs_mkdir. Just a thought. 647 */ 648 static int create_default_group(struct config_group *parent_group, 649 struct config_group *group) 650 { 651 int ret; 652 struct configfs_dirent *sd; 653 /* We trust the caller holds a reference to parent */ 654 struct dentry *child, *parent = parent_group->cg_item.ci_dentry; 655 656 if (!group->cg_item.ci_name) 657 group->cg_item.ci_name = group->cg_item.ci_namebuf; 658 659 ret = -ENOMEM; 660 child = d_alloc_name(parent, group->cg_item.ci_name); 661 if (child) { 662 d_add(child, NULL); 663 664 ret = configfs_attach_group(&parent_group->cg_item, 665 &group->cg_item, child); 666 if (!ret) { 667 sd = child->d_fsdata; 668 sd->s_type |= CONFIGFS_USET_DEFAULT; 669 } else { 670 BUG_ON(d_inode(child)); 671 d_drop(child); 672 dput(child); 673 } 674 } 675 676 return ret; 677 } 678 679 static int populate_groups(struct config_group *group) 680 { 681 struct config_group *new_group; 682 int ret = 0; 683 684 list_for_each_entry(new_group, &group->default_groups, group_entry) { 685 ret = create_default_group(group, new_group); 686 if (ret) { 687 detach_groups(group); 688 break; 689 } 690 } 691 692 return ret; 693 } 694 695 void configfs_remove_default_groups(struct config_group *group) 696 { 697 struct config_group *g, *n; 698 699 list_for_each_entry_safe(g, n, &group->default_groups, group_entry) { 700 list_del(&g->group_entry); 701 config_item_put(&g->cg_item); 702 } 703 } 704 EXPORT_SYMBOL(configfs_remove_default_groups); 705 706 /* 707 * All of link_obj/unlink_obj/link_group/unlink_group require that 708 * subsys->su_mutex is held. 709 */ 710 711 static void unlink_obj(struct config_item *item) 712 { 713 struct config_group *group; 714 715 group = item->ci_group; 716 if (group) { 717 list_del_init(&item->ci_entry); 718 719 item->ci_group = NULL; 720 item->ci_parent = NULL; 721 722 /* Drop the reference for ci_entry */ 723 config_item_put(item); 724 725 /* Drop the reference for ci_parent */ 726 config_group_put(group); 727 } 728 } 729 730 static void link_obj(struct config_item *parent_item, struct config_item *item) 731 { 732 /* 733 * Parent seems redundant with group, but it makes certain 734 * traversals much nicer. 735 */ 736 item->ci_parent = parent_item; 737 738 /* 739 * We hold a reference on the parent for the child's ci_parent 740 * link. 741 */ 742 item->ci_group = config_group_get(to_config_group(parent_item)); 743 list_add_tail(&item->ci_entry, &item->ci_group->cg_children); 744 745 /* 746 * We hold a reference on the child for ci_entry on the parent's 747 * cg_children 748 */ 749 config_item_get(item); 750 } 751 752 static void unlink_group(struct config_group *group) 753 { 754 struct config_group *new_group; 755 756 list_for_each_entry(new_group, &group->default_groups, group_entry) 757 unlink_group(new_group); 758 759 group->cg_subsys = NULL; 760 unlink_obj(&group->cg_item); 761 } 762 763 static void link_group(struct config_group *parent_group, struct config_group *group) 764 { 765 struct config_group *new_group; 766 struct configfs_subsystem *subsys = NULL; /* gcc is a turd */ 767 768 link_obj(&parent_group->cg_item, &group->cg_item); 769 770 if (parent_group->cg_subsys) 771 subsys = parent_group->cg_subsys; 772 else if (configfs_is_root(&parent_group->cg_item)) 773 subsys = to_configfs_subsystem(group); 774 else 775 BUG(); 776 group->cg_subsys = subsys; 777 778 list_for_each_entry(new_group, &group->default_groups, group_entry) 779 link_group(group, new_group); 780 } 781 782 /* 783 * The goal is that configfs_attach_item() (and 784 * configfs_attach_group()) can be called from either the VFS or this 785 * module. That is, they assume that the items have been created, 786 * the dentry allocated, and the dcache is all ready to go. 787 * 788 * If they fail, they must clean up after themselves as if they 789 * had never been called. The caller (VFS or local function) will 790 * handle cleaning up the dcache bits. 791 * 792 * configfs_detach_group() and configfs_detach_item() behave similarly on 793 * the way out. They assume that the proper semaphores are held, they 794 * clean up the configfs items, and they expect their callers will 795 * handle the dcache bits. 796 */ 797 static int configfs_attach_item(struct config_item *parent_item, 798 struct config_item *item, 799 struct dentry *dentry) 800 { 801 int ret; 802 803 ret = configfs_create_dir(item, dentry); 804 if (!ret) { 805 ret = populate_attrs(item); 806 if (ret) { 807 /* 808 * We are going to remove an inode and its dentry but 809 * the VFS may already have hit and used them. Thus, 810 * we must lock them as rmdir() would. 811 */ 812 inode_lock(d_inode(dentry)); 813 configfs_remove_dir(item); 814 d_inode(dentry)->i_flags |= S_DEAD; 815 dont_mount(dentry); 816 inode_unlock(d_inode(dentry)); 817 d_delete(dentry); 818 } 819 } 820 821 return ret; 822 } 823 824 /* Caller holds the mutex of the item's inode */ 825 static void configfs_detach_item(struct config_item *item) 826 { 827 detach_attrs(item); 828 configfs_remove_dir(item); 829 } 830 831 static int configfs_attach_group(struct config_item *parent_item, 832 struct config_item *item, 833 struct dentry *dentry) 834 { 835 int ret; 836 struct configfs_dirent *sd; 837 838 ret = configfs_attach_item(parent_item, item, dentry); 839 if (!ret) { 840 sd = dentry->d_fsdata; 841 sd->s_type |= CONFIGFS_USET_DIR; 842 843 /* 844 * FYI, we're faking mkdir in populate_groups() 845 * We must lock the group's inode to avoid races with the VFS 846 * which can already hit the inode and try to add/remove entries 847 * under it. 848 * 849 * We must also lock the inode to remove it safely in case of 850 * error, as rmdir() would. 851 */ 852 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD); 853 configfs_adjust_dir_dirent_depth_before_populate(sd); 854 ret = populate_groups(to_config_group(item)); 855 if (ret) { 856 configfs_detach_item(item); 857 d_inode(dentry)->i_flags |= S_DEAD; 858 dont_mount(dentry); 859 } 860 configfs_adjust_dir_dirent_depth_after_populate(sd); 861 inode_unlock(d_inode(dentry)); 862 if (ret) 863 d_delete(dentry); 864 } 865 866 return ret; 867 } 868 869 /* Caller holds the mutex of the group's inode */ 870 static void configfs_detach_group(struct config_item *item) 871 { 872 detach_groups(to_config_group(item)); 873 configfs_detach_item(item); 874 } 875 876 /* 877 * After the item has been detached from the filesystem view, we are 878 * ready to tear it out of the hierarchy. Notify the client before 879 * we do that so they can perform any cleanup that requires 880 * navigating the hierarchy. A client does not need to provide this 881 * callback. The subsystem semaphore MUST be held by the caller, and 882 * references must be valid for both items. It also assumes the 883 * caller has validated ci_type. 884 */ 885 static void client_disconnect_notify(struct config_item *parent_item, 886 struct config_item *item) 887 { 888 const struct config_item_type *type; 889 890 type = parent_item->ci_type; 891 BUG_ON(!type); 892 893 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify) 894 type->ct_group_ops->disconnect_notify(to_config_group(parent_item), 895 item); 896 } 897 898 /* 899 * Drop the initial reference from make_item()/make_group() 900 * This function assumes that reference is held on item 901 * and that item holds a valid reference to the parent. Also, it 902 * assumes the caller has validated ci_type. 903 */ 904 static void client_drop_item(struct config_item *parent_item, 905 struct config_item *item) 906 { 907 const struct config_item_type *type; 908 909 type = parent_item->ci_type; 910 BUG_ON(!type); 911 912 /* 913 * If ->drop_item() exists, it is responsible for the 914 * config_item_put(). 915 */ 916 if (type->ct_group_ops && type->ct_group_ops->drop_item) 917 type->ct_group_ops->drop_item(to_config_group(parent_item), 918 item); 919 else 920 config_item_put(item); 921 } 922 923 #ifdef DEBUG 924 static void configfs_dump_one(struct configfs_dirent *sd, int level) 925 { 926 pr_info("%*s\"%s\":\n", level, " ", configfs_get_name(sd)); 927 928 #define type_print(_type) if (sd->s_type & _type) pr_info("%*s %s\n", level, " ", #_type); 929 type_print(CONFIGFS_ROOT); 930 type_print(CONFIGFS_DIR); 931 type_print(CONFIGFS_ITEM_ATTR); 932 type_print(CONFIGFS_ITEM_LINK); 933 type_print(CONFIGFS_USET_DIR); 934 type_print(CONFIGFS_USET_DEFAULT); 935 type_print(CONFIGFS_USET_DROPPING); 936 #undef type_print 937 } 938 939 static int configfs_dump(struct configfs_dirent *sd, int level) 940 { 941 struct configfs_dirent *child_sd; 942 int ret = 0; 943 944 configfs_dump_one(sd, level); 945 946 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT))) 947 return 0; 948 949 list_for_each_entry(child_sd, &sd->s_children, s_sibling) { 950 ret = configfs_dump(child_sd, level + 2); 951 if (ret) 952 break; 953 } 954 955 return ret; 956 } 957 #endif 958 959 960 /* 961 * configfs_depend_item() and configfs_undepend_item() 962 * 963 * WARNING: Do not call these from a configfs callback! 964 * 965 * This describes these functions and their helpers. 966 * 967 * Allow another kernel system to depend on a config_item. If this 968 * happens, the item cannot go away until the dependent can live without 969 * it. The idea is to give client modules as simple an interface as 970 * possible. When a system asks them to depend on an item, they just 971 * call configfs_depend_item(). If the item is live and the client 972 * driver is in good shape, we'll happily do the work for them. 973 * 974 * Why is the locking complex? Because configfs uses the VFS to handle 975 * all locking, but this function is called outside the normal 976 * VFS->configfs path. So it must take VFS locks to prevent the 977 * VFS->configfs stuff (configfs_mkdir(), configfs_rmdir(), etc). This is 978 * why you can't call these functions underneath configfs callbacks. 979 * 980 * Note, btw, that this can be called at *any* time, even when a configfs 981 * subsystem isn't registered, or when configfs is loading or unloading. 982 * Just like configfs_register_subsystem(). So we take the same 983 * precautions. We pin the filesystem. We lock configfs_dirent_lock. 984 * If we can find the target item in the 985 * configfs tree, it must be part of the subsystem tree as well, so we 986 * do not need the subsystem semaphore. Holding configfs_dirent_lock helps 987 * locking out mkdir() and rmdir(), who might be racing us. 988 */ 989 990 /* 991 * configfs_depend_prep() 992 * 993 * Only subdirectories count here. Files (CONFIGFS_NOT_PINNED) are 994 * attributes. This is similar but not the same to configfs_detach_prep(). 995 * Note that configfs_detach_prep() expects the parent to be locked when it 996 * is called, but we lock the parent *inside* configfs_depend_prep(). We 997 * do that so we can unlock it if we find nothing. 998 * 999 * Here we do a depth-first search of the dentry hierarchy looking for 1000 * our object. 1001 * We deliberately ignore items tagged as dropping since they are virtually 1002 * dead, as well as items in the middle of attachment since they virtually 1003 * do not exist yet. This completes the locking out of racing mkdir() and 1004 * rmdir(). 1005 * Note: subdirectories in the middle of attachment start with s_type = 1006 * CONFIGFS_DIR|CONFIGFS_USET_CREATING set by create_dir(). When 1007 * CONFIGFS_USET_CREATING is set, we ignore the item. The actual set of 1008 * s_type is in configfs_new_dirent(), which has configfs_dirent_lock. 1009 * 1010 * If the target is not found, -ENOENT is bubbled up. 1011 * 1012 * This adds a requirement that all config_items be unique! 1013 * 1014 * This is recursive. There isn't 1015 * much on the stack, though, so folks that need this function - be careful 1016 * about your stack! Patches will be accepted to make it iterative. 1017 */ 1018 static int configfs_depend_prep(struct dentry *origin, 1019 struct config_item *target) 1020 { 1021 struct configfs_dirent *child_sd, *sd; 1022 int ret = 0; 1023 1024 BUG_ON(!origin || !origin->d_fsdata); 1025 sd = origin->d_fsdata; 1026 1027 if (sd->s_element == target) /* Boo-yah */ 1028 goto out; 1029 1030 list_for_each_entry(child_sd, &sd->s_children, s_sibling) { 1031 if ((child_sd->s_type & CONFIGFS_DIR) && 1032 !(child_sd->s_type & CONFIGFS_USET_DROPPING) && 1033 !(child_sd->s_type & CONFIGFS_USET_CREATING)) { 1034 ret = configfs_depend_prep(child_sd->s_dentry, 1035 target); 1036 if (!ret) 1037 goto out; /* Child path boo-yah */ 1038 } 1039 } 1040 1041 /* We looped all our children and didn't find target */ 1042 ret = -ENOENT; 1043 1044 out: 1045 return ret; 1046 } 1047 1048 static int configfs_do_depend_item(struct dentry *subsys_dentry, 1049 struct config_item *target) 1050 { 1051 struct configfs_dirent *p; 1052 int ret; 1053 1054 spin_lock(&configfs_dirent_lock); 1055 /* Scan the tree, return 0 if found */ 1056 ret = configfs_depend_prep(subsys_dentry, target); 1057 if (ret) 1058 goto out_unlock_dirent_lock; 1059 1060 /* 1061 * We are sure that the item is not about to be removed by rmdir(), and 1062 * not in the middle of attachment by mkdir(). 1063 */ 1064 p = target->ci_dentry->d_fsdata; 1065 p->s_dependent_count += 1; 1066 1067 out_unlock_dirent_lock: 1068 spin_unlock(&configfs_dirent_lock); 1069 1070 return ret; 1071 } 1072 1073 static inline struct configfs_dirent * 1074 configfs_find_subsys_dentry(struct configfs_dirent *root_sd, 1075 struct config_item *subsys_item) 1076 { 1077 struct configfs_dirent *p; 1078 struct configfs_dirent *ret = NULL; 1079 1080 list_for_each_entry(p, &root_sd->s_children, s_sibling) { 1081 if (p->s_type & CONFIGFS_DIR && 1082 p->s_element == subsys_item) { 1083 ret = p; 1084 break; 1085 } 1086 } 1087 1088 return ret; 1089 } 1090 1091 1092 int configfs_depend_item(struct configfs_subsystem *subsys, 1093 struct config_item *target) 1094 { 1095 int ret; 1096 struct configfs_dirent *subsys_sd; 1097 struct config_item *s_item = &subsys->su_group.cg_item; 1098 struct dentry *root; 1099 1100 /* 1101 * Pin the configfs filesystem. This means we can safely access 1102 * the root of the configfs filesystem. 1103 */ 1104 root = configfs_pin_fs(); 1105 if (IS_ERR(root)) 1106 return PTR_ERR(root); 1107 1108 /* 1109 * Next, lock the root directory. We're going to check that the 1110 * subsystem is really registered, and so we need to lock out 1111 * configfs_[un]register_subsystem(). 1112 */ 1113 inode_lock(d_inode(root)); 1114 1115 subsys_sd = configfs_find_subsys_dentry(root->d_fsdata, s_item); 1116 if (!subsys_sd) { 1117 ret = -ENOENT; 1118 goto out_unlock_fs; 1119 } 1120 1121 /* Ok, now we can trust subsys/s_item */ 1122 ret = configfs_do_depend_item(subsys_sd->s_dentry, target); 1123 1124 out_unlock_fs: 1125 inode_unlock(d_inode(root)); 1126 1127 /* 1128 * If we succeeded, the fs is pinned via other methods. If not, 1129 * we're done with it anyway. So release_fs() is always right. 1130 */ 1131 configfs_release_fs(); 1132 1133 return ret; 1134 } 1135 EXPORT_SYMBOL(configfs_depend_item); 1136 1137 /* 1138 * Release the dependent linkage. This is much simpler than 1139 * configfs_depend_item() because we know that that the client driver is 1140 * pinned, thus the subsystem is pinned, and therefore configfs is pinned. 1141 */ 1142 void configfs_undepend_item(struct config_item *target) 1143 { 1144 struct configfs_dirent *sd; 1145 1146 /* 1147 * Since we can trust everything is pinned, we just need 1148 * configfs_dirent_lock. 1149 */ 1150 spin_lock(&configfs_dirent_lock); 1151 1152 sd = target->ci_dentry->d_fsdata; 1153 BUG_ON(sd->s_dependent_count < 1); 1154 1155 sd->s_dependent_count -= 1; 1156 1157 /* 1158 * After this unlock, we cannot trust the item to stay alive! 1159 * DO NOT REFERENCE item after this unlock. 1160 */ 1161 spin_unlock(&configfs_dirent_lock); 1162 } 1163 EXPORT_SYMBOL(configfs_undepend_item); 1164 1165 /* 1166 * caller_subsys is a caller's subsystem not target's. This is used to 1167 * determine if we should lock root and check subsys or not. When we are 1168 * in the same subsystem as our target there is no need to do locking as 1169 * we know that subsys is valid and is not unregistered during this function 1170 * as we are called from callback of one of his children and VFS holds a lock 1171 * on some inode. Otherwise we have to lock our root to ensure that target's 1172 * subsystem it is not unregistered during this function. 1173 */ 1174 int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys, 1175 struct config_item *target) 1176 { 1177 struct configfs_subsystem *target_subsys; 1178 struct config_group *root, *parent; 1179 struct configfs_dirent *subsys_sd; 1180 int ret = -ENOENT; 1181 1182 /* Disallow this function for configfs root */ 1183 if (configfs_is_root(target)) 1184 return -EINVAL; 1185 1186 parent = target->ci_group; 1187 /* 1188 * This may happen when someone is trying to depend root 1189 * directory of some subsystem 1190 */ 1191 if (configfs_is_root(&parent->cg_item)) { 1192 target_subsys = to_configfs_subsystem(to_config_group(target)); 1193 root = parent; 1194 } else { 1195 target_subsys = parent->cg_subsys; 1196 /* Find a cofnigfs root as we may need it for locking */ 1197 for (root = parent; !configfs_is_root(&root->cg_item); 1198 root = root->cg_item.ci_group) 1199 ; 1200 } 1201 1202 if (target_subsys != caller_subsys) { 1203 /* 1204 * We are in other configfs subsystem, so we have to do 1205 * additional locking to prevent other subsystem from being 1206 * unregistered 1207 */ 1208 inode_lock(d_inode(root->cg_item.ci_dentry)); 1209 1210 /* 1211 * As we are trying to depend item from other subsystem 1212 * we have to check if this subsystem is still registered 1213 */ 1214 subsys_sd = configfs_find_subsys_dentry( 1215 root->cg_item.ci_dentry->d_fsdata, 1216 &target_subsys->su_group.cg_item); 1217 if (!subsys_sd) 1218 goto out_root_unlock; 1219 } else { 1220 subsys_sd = target_subsys->su_group.cg_item.ci_dentry->d_fsdata; 1221 } 1222 1223 /* Now we can execute core of depend item */ 1224 ret = configfs_do_depend_item(subsys_sd->s_dentry, target); 1225 1226 if (target_subsys != caller_subsys) 1227 out_root_unlock: 1228 /* 1229 * We were called from subsystem other than our target so we 1230 * took some locks so now it's time to release them 1231 */ 1232 inode_unlock(d_inode(root->cg_item.ci_dentry)); 1233 1234 return ret; 1235 } 1236 EXPORT_SYMBOL(configfs_depend_item_unlocked); 1237 1238 static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 1239 { 1240 int ret = 0; 1241 int module_got = 0; 1242 struct config_group *group = NULL; 1243 struct config_item *item = NULL; 1244 struct config_item *parent_item; 1245 struct configfs_subsystem *subsys; 1246 struct configfs_dirent *sd; 1247 const struct config_item_type *type; 1248 struct module *subsys_owner = NULL, *new_item_owner = NULL; 1249 char *name; 1250 1251 sd = dentry->d_parent->d_fsdata; 1252 1253 /* 1254 * Fake invisibility if dir belongs to a group/default groups hierarchy 1255 * being attached 1256 */ 1257 if (!configfs_dirent_is_ready(sd)) { 1258 ret = -ENOENT; 1259 goto out; 1260 } 1261 1262 if (!(sd->s_type & CONFIGFS_USET_DIR)) { 1263 ret = -EPERM; 1264 goto out; 1265 } 1266 1267 /* Get a working ref for the duration of this function */ 1268 parent_item = configfs_get_config_item(dentry->d_parent); 1269 type = parent_item->ci_type; 1270 subsys = to_config_group(parent_item)->cg_subsys; 1271 BUG_ON(!subsys); 1272 1273 if (!type || !type->ct_group_ops || 1274 (!type->ct_group_ops->make_group && 1275 !type->ct_group_ops->make_item)) { 1276 ret = -EPERM; /* Lack-of-mkdir returns -EPERM */ 1277 goto out_put; 1278 } 1279 1280 /* 1281 * The subsystem may belong to a different module than the item 1282 * being created. We don't want to safely pin the new item but 1283 * fail to pin the subsystem it sits under. 1284 */ 1285 if (!subsys->su_group.cg_item.ci_type) { 1286 ret = -EINVAL; 1287 goto out_put; 1288 } 1289 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner; 1290 if (!try_module_get(subsys_owner)) { 1291 ret = -EINVAL; 1292 goto out_put; 1293 } 1294 1295 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL); 1296 if (!name) { 1297 ret = -ENOMEM; 1298 goto out_subsys_put; 1299 } 1300 1301 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name); 1302 1303 mutex_lock(&subsys->su_mutex); 1304 if (type->ct_group_ops->make_group) { 1305 group = type->ct_group_ops->make_group(to_config_group(parent_item), name); 1306 if (!group) 1307 group = ERR_PTR(-ENOMEM); 1308 if (!IS_ERR(group)) { 1309 link_group(to_config_group(parent_item), group); 1310 item = &group->cg_item; 1311 } else 1312 ret = PTR_ERR(group); 1313 } else { 1314 item = type->ct_group_ops->make_item(to_config_group(parent_item), name); 1315 if (!item) 1316 item = ERR_PTR(-ENOMEM); 1317 if (!IS_ERR(item)) 1318 link_obj(parent_item, item); 1319 else 1320 ret = PTR_ERR(item); 1321 } 1322 mutex_unlock(&subsys->su_mutex); 1323 1324 kfree(name); 1325 if (ret) { 1326 /* 1327 * If ret != 0, then link_obj() was never called. 1328 * There are no extra references to clean up. 1329 */ 1330 goto out_subsys_put; 1331 } 1332 1333 /* 1334 * link_obj() has been called (via link_group() for groups). 1335 * From here on out, errors must clean that up. 1336 */ 1337 1338 type = item->ci_type; 1339 if (!type) { 1340 ret = -EINVAL; 1341 goto out_unlink; 1342 } 1343 1344 new_item_owner = type->ct_owner; 1345 if (!try_module_get(new_item_owner)) { 1346 ret = -EINVAL; 1347 goto out_unlink; 1348 } 1349 1350 /* 1351 * I hate doing it this way, but if there is 1352 * an error, module_put() probably should 1353 * happen after any cleanup. 1354 */ 1355 module_got = 1; 1356 1357 /* 1358 * Make racing rmdir() fail if it did not tag parent with 1359 * CONFIGFS_USET_DROPPING 1360 * Note: if CONFIGFS_USET_DROPPING is already set, attach_group() will 1361 * fail and let rmdir() terminate correctly 1362 */ 1363 spin_lock(&configfs_dirent_lock); 1364 /* This will make configfs_detach_prep() fail */ 1365 sd->s_type |= CONFIGFS_USET_IN_MKDIR; 1366 spin_unlock(&configfs_dirent_lock); 1367 1368 if (group) 1369 ret = configfs_attach_group(parent_item, item, dentry); 1370 else 1371 ret = configfs_attach_item(parent_item, item, dentry); 1372 1373 spin_lock(&configfs_dirent_lock); 1374 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR; 1375 if (!ret) 1376 configfs_dir_set_ready(dentry->d_fsdata); 1377 spin_unlock(&configfs_dirent_lock); 1378 1379 out_unlink: 1380 if (ret) { 1381 /* Tear down everything we built up */ 1382 mutex_lock(&subsys->su_mutex); 1383 1384 client_disconnect_notify(parent_item, item); 1385 if (group) 1386 unlink_group(group); 1387 else 1388 unlink_obj(item); 1389 client_drop_item(parent_item, item); 1390 1391 mutex_unlock(&subsys->su_mutex); 1392 1393 if (module_got) 1394 module_put(new_item_owner); 1395 } 1396 1397 out_subsys_put: 1398 if (ret) 1399 module_put(subsys_owner); 1400 1401 out_put: 1402 /* 1403 * link_obj()/link_group() took a reference from child->parent, 1404 * so the parent is safely pinned. We can drop our working 1405 * reference. 1406 */ 1407 config_item_put(parent_item); 1408 1409 out: 1410 return ret; 1411 } 1412 1413 static int configfs_rmdir(struct inode *dir, struct dentry *dentry) 1414 { 1415 struct config_item *parent_item; 1416 struct config_item *item; 1417 struct configfs_subsystem *subsys; 1418 struct configfs_dirent *sd; 1419 struct module *subsys_owner = NULL, *dead_item_owner = NULL; 1420 int ret; 1421 1422 sd = dentry->d_fsdata; 1423 if (sd->s_type & CONFIGFS_USET_DEFAULT) 1424 return -EPERM; 1425 1426 /* Get a working ref until we have the child */ 1427 parent_item = configfs_get_config_item(dentry->d_parent); 1428 subsys = to_config_group(parent_item)->cg_subsys; 1429 BUG_ON(!subsys); 1430 1431 if (!parent_item->ci_type) { 1432 config_item_put(parent_item); 1433 return -EINVAL; 1434 } 1435 1436 /* configfs_mkdir() shouldn't have allowed this */ 1437 BUG_ON(!subsys->su_group.cg_item.ci_type); 1438 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner; 1439 1440 /* 1441 * Ensure that no racing symlink() will make detach_prep() fail while 1442 * the new link is temporarily attached 1443 */ 1444 do { 1445 struct dentry *wait; 1446 1447 mutex_lock(&configfs_symlink_mutex); 1448 spin_lock(&configfs_dirent_lock); 1449 /* 1450 * Here's where we check for dependents. We're protected by 1451 * configfs_dirent_lock. 1452 * If no dependent, atomically tag the item as dropping. 1453 */ 1454 ret = sd->s_dependent_count ? -EBUSY : 0; 1455 if (!ret) { 1456 ret = configfs_detach_prep(dentry, &wait); 1457 if (ret) 1458 configfs_detach_rollback(dentry); 1459 } 1460 spin_unlock(&configfs_dirent_lock); 1461 mutex_unlock(&configfs_symlink_mutex); 1462 1463 if (ret) { 1464 if (ret != -EAGAIN) { 1465 config_item_put(parent_item); 1466 return ret; 1467 } 1468 1469 /* Wait until the racing operation terminates */ 1470 inode_lock(d_inode(wait)); 1471 inode_unlock(d_inode(wait)); 1472 dput(wait); 1473 } 1474 } while (ret == -EAGAIN); 1475 1476 /* Get a working ref for the duration of this function */ 1477 item = configfs_get_config_item(dentry); 1478 1479 /* Drop reference from above, item already holds one. */ 1480 config_item_put(parent_item); 1481 1482 if (item->ci_type) 1483 dead_item_owner = item->ci_type->ct_owner; 1484 1485 if (sd->s_type & CONFIGFS_USET_DIR) { 1486 configfs_detach_group(item); 1487 1488 mutex_lock(&subsys->su_mutex); 1489 client_disconnect_notify(parent_item, item); 1490 unlink_group(to_config_group(item)); 1491 } else { 1492 configfs_detach_item(item); 1493 1494 mutex_lock(&subsys->su_mutex); 1495 client_disconnect_notify(parent_item, item); 1496 unlink_obj(item); 1497 } 1498 1499 client_drop_item(parent_item, item); 1500 mutex_unlock(&subsys->su_mutex); 1501 1502 /* Drop our reference from above */ 1503 config_item_put(item); 1504 1505 module_put(dead_item_owner); 1506 module_put(subsys_owner); 1507 1508 return 0; 1509 } 1510 1511 const struct inode_operations configfs_dir_inode_operations = { 1512 .mkdir = configfs_mkdir, 1513 .rmdir = configfs_rmdir, 1514 .symlink = configfs_symlink, 1515 .unlink = configfs_unlink, 1516 .lookup = configfs_lookup, 1517 .setattr = configfs_setattr, 1518 }; 1519 1520 const struct inode_operations configfs_root_inode_operations = { 1521 .lookup = configfs_lookup, 1522 .setattr = configfs_setattr, 1523 }; 1524 1525 #if 0 1526 int configfs_rename_dir(struct config_item * item, const char *new_name) 1527 { 1528 int error = 0; 1529 struct dentry * new_dentry, * parent; 1530 1531 if (!strcmp(config_item_name(item), new_name)) 1532 return -EINVAL; 1533 1534 if (!item->parent) 1535 return -EINVAL; 1536 1537 down_write(&configfs_rename_sem); 1538 parent = item->parent->dentry; 1539 1540 inode_lock(d_inode(parent)); 1541 1542 new_dentry = lookup_one_len(new_name, parent, strlen(new_name)); 1543 if (!IS_ERR(new_dentry)) { 1544 if (d_really_is_negative(new_dentry)) { 1545 error = config_item_set_name(item, "%s", new_name); 1546 if (!error) { 1547 d_add(new_dentry, NULL); 1548 d_move(item->dentry, new_dentry); 1549 } 1550 else 1551 d_delete(new_dentry); 1552 } else 1553 error = -EEXIST; 1554 dput(new_dentry); 1555 } 1556 inode_unlock(d_inode(parent)); 1557 up_write(&configfs_rename_sem); 1558 1559 return error; 1560 } 1561 #endif 1562 1563 static int configfs_dir_open(struct inode *inode, struct file *file) 1564 { 1565 struct dentry * dentry = file->f_path.dentry; 1566 struct configfs_dirent * parent_sd = dentry->d_fsdata; 1567 int err; 1568 1569 inode_lock(d_inode(dentry)); 1570 /* 1571 * Fake invisibility if dir belongs to a group/default groups hierarchy 1572 * being attached 1573 */ 1574 err = -ENOENT; 1575 if (configfs_dirent_is_ready(parent_sd)) { 1576 file->private_data = configfs_new_dirent(parent_sd, NULL, 0); 1577 if (IS_ERR(file->private_data)) 1578 err = PTR_ERR(file->private_data); 1579 else 1580 err = 0; 1581 } 1582 inode_unlock(d_inode(dentry)); 1583 1584 return err; 1585 } 1586 1587 static int configfs_dir_close(struct inode *inode, struct file *file) 1588 { 1589 struct dentry * dentry = file->f_path.dentry; 1590 struct configfs_dirent * cursor = file->private_data; 1591 1592 inode_lock(d_inode(dentry)); 1593 spin_lock(&configfs_dirent_lock); 1594 list_del_init(&cursor->s_sibling); 1595 spin_unlock(&configfs_dirent_lock); 1596 inode_unlock(d_inode(dentry)); 1597 1598 release_configfs_dirent(cursor); 1599 1600 return 0; 1601 } 1602 1603 /* Relationship between s_mode and the DT_xxx types */ 1604 static inline unsigned char dt_type(struct configfs_dirent *sd) 1605 { 1606 return (sd->s_mode >> 12) & 15; 1607 } 1608 1609 static int configfs_readdir(struct file *file, struct dir_context *ctx) 1610 { 1611 struct dentry *dentry = file->f_path.dentry; 1612 struct super_block *sb = dentry->d_sb; 1613 struct configfs_dirent * parent_sd = dentry->d_fsdata; 1614 struct configfs_dirent *cursor = file->private_data; 1615 struct list_head *p, *q = &cursor->s_sibling; 1616 ino_t ino = 0; 1617 1618 if (!dir_emit_dots(file, ctx)) 1619 return 0; 1620 spin_lock(&configfs_dirent_lock); 1621 if (ctx->pos == 2) 1622 list_move(q, &parent_sd->s_children); 1623 for (p = q->next; p != &parent_sd->s_children; p = p->next) { 1624 struct configfs_dirent *next; 1625 const char *name; 1626 int len; 1627 struct inode *inode = NULL; 1628 1629 next = list_entry(p, struct configfs_dirent, s_sibling); 1630 if (!next->s_element) 1631 continue; 1632 1633 /* 1634 * We'll have a dentry and an inode for 1635 * PINNED items and for open attribute 1636 * files. We lock here to prevent a race 1637 * with configfs_d_iput() clearing 1638 * s_dentry before calling iput(). 1639 * 1640 * Why do we go to the trouble? If 1641 * someone has an attribute file open, 1642 * the inode number should match until 1643 * they close it. Beyond that, we don't 1644 * care. 1645 */ 1646 dentry = next->s_dentry; 1647 if (dentry) 1648 inode = d_inode(dentry); 1649 if (inode) 1650 ino = inode->i_ino; 1651 spin_unlock(&configfs_dirent_lock); 1652 if (!inode) 1653 ino = iunique(sb, 2); 1654 1655 name = configfs_get_name(next); 1656 len = strlen(name); 1657 1658 if (!dir_emit(ctx, name, len, ino, dt_type(next))) 1659 return 0; 1660 1661 spin_lock(&configfs_dirent_lock); 1662 list_move(q, p); 1663 p = q; 1664 ctx->pos++; 1665 } 1666 spin_unlock(&configfs_dirent_lock); 1667 return 0; 1668 } 1669 1670 static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence) 1671 { 1672 struct dentry * dentry = file->f_path.dentry; 1673 1674 switch (whence) { 1675 case 1: 1676 offset += file->f_pos; 1677 /* fall through */ 1678 case 0: 1679 if (offset >= 0) 1680 break; 1681 /* fall through */ 1682 default: 1683 return -EINVAL; 1684 } 1685 if (offset != file->f_pos) { 1686 file->f_pos = offset; 1687 if (file->f_pos >= 2) { 1688 struct configfs_dirent *sd = dentry->d_fsdata; 1689 struct configfs_dirent *cursor = file->private_data; 1690 struct list_head *p; 1691 loff_t n = file->f_pos - 2; 1692 1693 spin_lock(&configfs_dirent_lock); 1694 list_del(&cursor->s_sibling); 1695 p = sd->s_children.next; 1696 while (n && p != &sd->s_children) { 1697 struct configfs_dirent *next; 1698 next = list_entry(p, struct configfs_dirent, 1699 s_sibling); 1700 if (next->s_element) 1701 n--; 1702 p = p->next; 1703 } 1704 list_add_tail(&cursor->s_sibling, p); 1705 spin_unlock(&configfs_dirent_lock); 1706 } 1707 } 1708 return offset; 1709 } 1710 1711 const struct file_operations configfs_dir_operations = { 1712 .open = configfs_dir_open, 1713 .release = configfs_dir_close, 1714 .llseek = configfs_dir_lseek, 1715 .read = generic_read_dir, 1716 .iterate_shared = configfs_readdir, 1717 }; 1718 1719 /** 1720 * configfs_register_group - creates a parent-child relation between two groups 1721 * @parent_group: parent group 1722 * @group: child group 1723 * 1724 * link groups, creates dentry for the child and attaches it to the 1725 * parent dentry. 1726 * 1727 * Return: 0 on success, negative errno code on error 1728 */ 1729 int configfs_register_group(struct config_group *parent_group, 1730 struct config_group *group) 1731 { 1732 struct configfs_subsystem *subsys = parent_group->cg_subsys; 1733 struct dentry *parent; 1734 int ret; 1735 1736 mutex_lock(&subsys->su_mutex); 1737 link_group(parent_group, group); 1738 mutex_unlock(&subsys->su_mutex); 1739 1740 parent = parent_group->cg_item.ci_dentry; 1741 1742 inode_lock_nested(d_inode(parent), I_MUTEX_PARENT); 1743 ret = create_default_group(parent_group, group); 1744 if (ret) 1745 goto err_out; 1746 1747 spin_lock(&configfs_dirent_lock); 1748 configfs_dir_set_ready(group->cg_item.ci_dentry->d_fsdata); 1749 spin_unlock(&configfs_dirent_lock); 1750 inode_unlock(d_inode(parent)); 1751 return 0; 1752 err_out: 1753 inode_unlock(d_inode(parent)); 1754 mutex_lock(&subsys->su_mutex); 1755 unlink_group(group); 1756 mutex_unlock(&subsys->su_mutex); 1757 return ret; 1758 } 1759 EXPORT_SYMBOL(configfs_register_group); 1760 1761 /** 1762 * configfs_unregister_group() - unregisters a child group from its parent 1763 * @group: parent group to be unregistered 1764 * 1765 * Undoes configfs_register_group() 1766 */ 1767 void configfs_unregister_group(struct config_group *group) 1768 { 1769 struct configfs_subsystem *subsys = group->cg_subsys; 1770 struct dentry *dentry = group->cg_item.ci_dentry; 1771 struct dentry *parent = group->cg_item.ci_parent->ci_dentry; 1772 1773 mutex_lock(&subsys->su_mutex); 1774 if (!group->cg_item.ci_parent->ci_group) { 1775 /* 1776 * The parent has already been unlinked and detached 1777 * due to a rmdir. 1778 */ 1779 goto unlink_group; 1780 } 1781 mutex_unlock(&subsys->su_mutex); 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_delete(dentry); 1792 inode_unlock(d_inode(parent)); 1793 1794 dput(dentry); 1795 1796 mutex_lock(&subsys->su_mutex); 1797 unlink_group: 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 1855 root = configfs_pin_fs(); 1856 if (IS_ERR(root)) 1857 return PTR_ERR(root); 1858 1859 if (!group->cg_item.ci_name) 1860 group->cg_item.ci_name = group->cg_item.ci_namebuf; 1861 1862 sd = root->d_fsdata; 1863 link_group(to_config_group(sd->s_element), group); 1864 1865 inode_lock_nested(d_inode(root), I_MUTEX_PARENT); 1866 1867 err = -ENOMEM; 1868 dentry = d_alloc_name(root, group->cg_item.ci_name); 1869 if (dentry) { 1870 d_add(dentry, NULL); 1871 1872 err = configfs_attach_group(sd->s_element, &group->cg_item, 1873 dentry); 1874 if (err) { 1875 BUG_ON(d_inode(dentry)); 1876 d_drop(dentry); 1877 dput(dentry); 1878 } else { 1879 spin_lock(&configfs_dirent_lock); 1880 configfs_dir_set_ready(dentry->d_fsdata); 1881 spin_unlock(&configfs_dirent_lock); 1882 } 1883 } 1884 1885 inode_unlock(d_inode(root)); 1886 1887 if (err) { 1888 unlink_group(group); 1889 configfs_release_fs(); 1890 } 1891 1892 return err; 1893 } 1894 1895 void configfs_unregister_subsystem(struct configfs_subsystem *subsys) 1896 { 1897 struct config_group *group = &subsys->su_group; 1898 struct dentry *dentry = group->cg_item.ci_dentry; 1899 struct dentry *root = dentry->d_sb->s_root; 1900 1901 if (dentry->d_parent != root) { 1902 pr_err("Tried to unregister non-subsystem!\n"); 1903 return; 1904 } 1905 1906 inode_lock_nested(d_inode(root), 1907 I_MUTEX_PARENT); 1908 inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD); 1909 mutex_lock(&configfs_symlink_mutex); 1910 spin_lock(&configfs_dirent_lock); 1911 if (configfs_detach_prep(dentry, NULL)) { 1912 pr_err("Tried to unregister non-empty subsystem!\n"); 1913 } 1914 spin_unlock(&configfs_dirent_lock); 1915 mutex_unlock(&configfs_symlink_mutex); 1916 configfs_detach_group(&group->cg_item); 1917 d_inode(dentry)->i_flags |= S_DEAD; 1918 dont_mount(dentry); 1919 inode_unlock(d_inode(dentry)); 1920 1921 d_delete(dentry); 1922 1923 inode_unlock(d_inode(root)); 1924 1925 dput(dentry); 1926 1927 unlink_group(group); 1928 configfs_release_fs(); 1929 } 1930 1931 EXPORT_SYMBOL(configfs_register_subsystem); 1932 EXPORT_SYMBOL(configfs_unregister_subsystem); 1933