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