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