1 /* 2 * fs/sysfs/dir.c - sysfs core and dir operation implementation 3 * 4 * Copyright (c) 2001-3 Patrick Mochel 5 * Copyright (c) 2007 SUSE Linux Products GmbH 6 * Copyright (c) 2007 Tejun Heo <teheo@suse.de> 7 * 8 * This file is released under the GPLv2. 9 * 10 * Please see Documentation/filesystems/sysfs.txt for more information. 11 */ 12 13 #undef DEBUG 14 15 #include <linux/fs.h> 16 #include <linux/mount.h> 17 #include <linux/module.h> 18 #include <linux/kobject.h> 19 #include <linux/namei.h> 20 #include <linux/idr.h> 21 #include <linux/completion.h> 22 #include <linux/mutex.h> 23 #include <linux/slab.h> 24 #include <linux/security.h> 25 #include <linux/hash.h> 26 #include "sysfs.h" 27 28 DEFINE_MUTEX(sysfs_mutex); 29 DEFINE_SPINLOCK(sysfs_assoc_lock); 30 31 #define to_sysfs_dirent(X) rb_entry((X), struct sysfs_dirent, s_rb); 32 33 static DEFINE_SPINLOCK(sysfs_ino_lock); 34 static DEFINE_IDA(sysfs_ino_ida); 35 36 /** 37 * sysfs_name_hash 38 * @ns: Namespace tag to hash 39 * @name: Null terminated string to hash 40 * 41 * Returns 31 bit hash of ns + name (so it fits in an off_t ) 42 */ 43 static unsigned int sysfs_name_hash(const void *ns, const char *name) 44 { 45 unsigned long hash = init_name_hash(); 46 unsigned int len = strlen(name); 47 while (len--) 48 hash = partial_name_hash(*name++, hash); 49 hash = ( end_name_hash(hash) ^ hash_ptr( (void *)ns, 31 ) ); 50 hash &= 0x7fffffffU; 51 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */ 52 if (hash < 1) 53 hash += 2; 54 if (hash >= INT_MAX) 55 hash = INT_MAX - 1; 56 return hash; 57 } 58 59 static int sysfs_name_compare(unsigned int hash, const void *ns, 60 const char *name, const struct sysfs_dirent *sd) 61 { 62 if (hash != sd->s_hash) 63 return hash - sd->s_hash; 64 if (ns != sd->s_ns) 65 return ns - sd->s_ns; 66 return strcmp(name, sd->s_name); 67 } 68 69 static int sysfs_sd_compare(const struct sysfs_dirent *left, 70 const struct sysfs_dirent *right) 71 { 72 return sysfs_name_compare(left->s_hash, left->s_ns, left->s_name, 73 right); 74 } 75 76 /** 77 * sysfs_link_subling - link sysfs_dirent into sibling rbtree 78 * @sd: sysfs_dirent of interest 79 * 80 * Link @sd into its sibling rbtree which starts from 81 * sd->s_parent->s_dir.children. 82 * 83 * Locking: 84 * mutex_lock(sysfs_mutex) 85 * 86 * RETURNS: 87 * 0 on susccess -EEXIST on failure. 88 */ 89 static int sysfs_link_sibling(struct sysfs_dirent *sd) 90 { 91 struct rb_node **node = &sd->s_parent->s_dir.children.rb_node; 92 struct rb_node *parent = NULL; 93 94 if (sysfs_type(sd) == SYSFS_DIR) 95 sd->s_parent->s_dir.subdirs++; 96 97 while (*node) { 98 struct sysfs_dirent *pos; 99 int result; 100 101 pos = to_sysfs_dirent(*node); 102 parent = *node; 103 result = sysfs_sd_compare(sd, pos); 104 if (result < 0) 105 node = &pos->s_rb.rb_left; 106 else if (result > 0) 107 node = &pos->s_rb.rb_right; 108 else 109 return -EEXIST; 110 } 111 /* add new node and rebalance the tree */ 112 rb_link_node(&sd->s_rb, parent, node); 113 rb_insert_color(&sd->s_rb, &sd->s_parent->s_dir.children); 114 return 0; 115 } 116 117 /** 118 * sysfs_unlink_sibling - unlink sysfs_dirent from sibling rbtree 119 * @sd: sysfs_dirent of interest 120 * 121 * Unlink @sd from its sibling rbtree which starts from 122 * sd->s_parent->s_dir.children. 123 * 124 * Locking: 125 * mutex_lock(sysfs_mutex) 126 */ 127 static void sysfs_unlink_sibling(struct sysfs_dirent *sd) 128 { 129 if (sysfs_type(sd) == SYSFS_DIR) 130 sd->s_parent->s_dir.subdirs--; 131 132 rb_erase(&sd->s_rb, &sd->s_parent->s_dir.children); 133 } 134 135 #ifdef CONFIG_DEBUG_LOCK_ALLOC 136 137 /* Test for attributes that want to ignore lockdep for read-locking */ 138 static bool ignore_lockdep(struct sysfs_dirent *sd) 139 { 140 return sysfs_type(sd) == SYSFS_KOBJ_ATTR && 141 sd->s_attr.attr->ignore_lockdep; 142 } 143 144 #else 145 146 static inline bool ignore_lockdep(struct sysfs_dirent *sd) 147 { 148 return true; 149 } 150 151 #endif 152 153 /** 154 * sysfs_get_active - get an active reference to sysfs_dirent 155 * @sd: sysfs_dirent to get an active reference to 156 * 157 * Get an active reference of @sd. This function is noop if @sd 158 * is NULL. 159 * 160 * RETURNS: 161 * Pointer to @sd on success, NULL on failure. 162 */ 163 struct sysfs_dirent *sysfs_get_active(struct sysfs_dirent *sd) 164 { 165 if (unlikely(!sd)) 166 return NULL; 167 168 while (1) { 169 int v, t; 170 171 v = atomic_read(&sd->s_active); 172 if (unlikely(v < 0)) 173 return NULL; 174 175 t = atomic_cmpxchg(&sd->s_active, v, v + 1); 176 if (likely(t == v)) 177 break; 178 if (t < 0) 179 return NULL; 180 181 cpu_relax(); 182 } 183 184 if (likely(!ignore_lockdep(sd))) 185 rwsem_acquire_read(&sd->dep_map, 0, 1, _RET_IP_); 186 return sd; 187 } 188 189 /** 190 * sysfs_put_active - put an active reference to sysfs_dirent 191 * @sd: sysfs_dirent to put an active reference to 192 * 193 * Put an active reference to @sd. This function is noop if @sd 194 * is NULL. 195 */ 196 void sysfs_put_active(struct sysfs_dirent *sd) 197 { 198 int v; 199 200 if (unlikely(!sd)) 201 return; 202 203 if (likely(!ignore_lockdep(sd))) 204 rwsem_release(&sd->dep_map, 1, _RET_IP_); 205 v = atomic_dec_return(&sd->s_active); 206 if (likely(v != SD_DEACTIVATED_BIAS)) 207 return; 208 209 /* atomic_dec_return() is a mb(), we'll always see the updated 210 * sd->u.completion. 211 */ 212 complete(sd->u.completion); 213 } 214 215 /** 216 * sysfs_deactivate - deactivate sysfs_dirent 217 * @sd: sysfs_dirent to deactivate 218 * 219 * Deny new active references and drain existing ones. 220 */ 221 static void sysfs_deactivate(struct sysfs_dirent *sd) 222 { 223 DECLARE_COMPLETION_ONSTACK(wait); 224 int v; 225 226 BUG_ON(!(sd->s_flags & SYSFS_FLAG_REMOVED)); 227 228 if (!(sysfs_type(sd) & SYSFS_ACTIVE_REF)) 229 return; 230 231 sd->u.completion = (void *)&wait; 232 233 rwsem_acquire(&sd->dep_map, 0, 0, _RET_IP_); 234 /* atomic_add_return() is a mb(), put_active() will always see 235 * the updated sd->u.completion. 236 */ 237 v = atomic_add_return(SD_DEACTIVATED_BIAS, &sd->s_active); 238 239 if (v != SD_DEACTIVATED_BIAS) { 240 lock_contended(&sd->dep_map, _RET_IP_); 241 wait_for_completion(&wait); 242 } 243 244 lock_acquired(&sd->dep_map, _RET_IP_); 245 rwsem_release(&sd->dep_map, 1, _RET_IP_); 246 } 247 248 static int sysfs_alloc_ino(unsigned int *pino) 249 { 250 int ino, rc; 251 252 retry: 253 spin_lock(&sysfs_ino_lock); 254 rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino); 255 spin_unlock(&sysfs_ino_lock); 256 257 if (rc == -EAGAIN) { 258 if (ida_pre_get(&sysfs_ino_ida, GFP_KERNEL)) 259 goto retry; 260 rc = -ENOMEM; 261 } 262 263 *pino = ino; 264 return rc; 265 } 266 267 static void sysfs_free_ino(unsigned int ino) 268 { 269 spin_lock(&sysfs_ino_lock); 270 ida_remove(&sysfs_ino_ida, ino); 271 spin_unlock(&sysfs_ino_lock); 272 } 273 274 void release_sysfs_dirent(struct sysfs_dirent * sd) 275 { 276 struct sysfs_dirent *parent_sd; 277 278 repeat: 279 /* Moving/renaming is always done while holding reference. 280 * sd->s_parent won't change beneath us. 281 */ 282 parent_sd = sd->s_parent; 283 284 if (sysfs_type(sd) == SYSFS_KOBJ_LINK) 285 sysfs_put(sd->s_symlink.target_sd); 286 if (sysfs_type(sd) & SYSFS_COPY_NAME) 287 kfree(sd->s_name); 288 if (sd->s_iattr && sd->s_iattr->ia_secdata) 289 security_release_secctx(sd->s_iattr->ia_secdata, 290 sd->s_iattr->ia_secdata_len); 291 kfree(sd->s_iattr); 292 sysfs_free_ino(sd->s_ino); 293 kmem_cache_free(sysfs_dir_cachep, sd); 294 295 sd = parent_sd; 296 if (sd && atomic_dec_and_test(&sd->s_count)) 297 goto repeat; 298 } 299 300 static int sysfs_dentry_delete(const struct dentry *dentry) 301 { 302 struct sysfs_dirent *sd = dentry->d_fsdata; 303 return !!(sd->s_flags & SYSFS_FLAG_REMOVED); 304 } 305 306 static int sysfs_dentry_revalidate(struct dentry *dentry, struct nameidata *nd) 307 { 308 struct sysfs_dirent *sd; 309 int is_dir; 310 int type; 311 312 if (nd->flags & LOOKUP_RCU) 313 return -ECHILD; 314 315 sd = dentry->d_fsdata; 316 mutex_lock(&sysfs_mutex); 317 318 /* The sysfs dirent has been deleted */ 319 if (sd->s_flags & SYSFS_FLAG_REMOVED) 320 goto out_bad; 321 322 /* The sysfs dirent has been moved? */ 323 if (dentry->d_parent->d_fsdata != sd->s_parent) 324 goto out_bad; 325 326 /* The sysfs dirent has been renamed */ 327 if (strcmp(dentry->d_name.name, sd->s_name) != 0) 328 goto out_bad; 329 330 /* The sysfs dirent has been moved to a different namespace */ 331 type = KOBJ_NS_TYPE_NONE; 332 if (sd->s_parent) { 333 type = sysfs_ns_type(sd->s_parent); 334 if (type != KOBJ_NS_TYPE_NONE && 335 sysfs_info(dentry->d_sb)->ns[type] != sd->s_ns) 336 goto out_bad; 337 } 338 339 mutex_unlock(&sysfs_mutex); 340 out_valid: 341 return 1; 342 out_bad: 343 /* Remove the dentry from the dcache hashes. 344 * If this is a deleted dentry we use d_drop instead of d_delete 345 * so sysfs doesn't need to cope with negative dentries. 346 * 347 * If this is a dentry that has simply been renamed we 348 * use d_drop to remove it from the dcache lookup on its 349 * old parent. If this dentry persists later when a lookup 350 * is performed at its new name the dentry will be readded 351 * to the dcache hashes. 352 */ 353 is_dir = (sysfs_type(sd) == SYSFS_DIR); 354 mutex_unlock(&sysfs_mutex); 355 if (is_dir) { 356 /* If we have submounts we must allow the vfs caches 357 * to lie about the state of the filesystem to prevent 358 * leaks and other nasty things. 359 */ 360 if (have_submounts(dentry)) 361 goto out_valid; 362 shrink_dcache_parent(dentry); 363 } 364 d_drop(dentry); 365 return 0; 366 } 367 368 static void sysfs_dentry_iput(struct dentry *dentry, struct inode *inode) 369 { 370 struct sysfs_dirent * sd = dentry->d_fsdata; 371 372 sysfs_put(sd); 373 iput(inode); 374 } 375 376 static const struct dentry_operations sysfs_dentry_ops = { 377 .d_revalidate = sysfs_dentry_revalidate, 378 .d_delete = sysfs_dentry_delete, 379 .d_iput = sysfs_dentry_iput, 380 }; 381 382 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type) 383 { 384 char *dup_name = NULL; 385 struct sysfs_dirent *sd; 386 387 if (type & SYSFS_COPY_NAME) { 388 name = dup_name = kstrdup(name, GFP_KERNEL); 389 if (!name) 390 return NULL; 391 } 392 393 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); 394 if (!sd) 395 goto err_out1; 396 397 if (sysfs_alloc_ino(&sd->s_ino)) 398 goto err_out2; 399 400 atomic_set(&sd->s_count, 1); 401 atomic_set(&sd->s_active, 0); 402 403 sd->s_name = name; 404 sd->s_mode = mode; 405 sd->s_flags = type; 406 407 return sd; 408 409 err_out2: 410 kmem_cache_free(sysfs_dir_cachep, sd); 411 err_out1: 412 kfree(dup_name); 413 return NULL; 414 } 415 416 /** 417 * sysfs_addrm_start - prepare for sysfs_dirent add/remove 418 * @acxt: pointer to sysfs_addrm_cxt to be used 419 * @parent_sd: parent sysfs_dirent 420 * 421 * This function is called when the caller is about to add or 422 * remove sysfs_dirent under @parent_sd. This function acquires 423 * sysfs_mutex. @acxt is used to keep and pass context to 424 * other addrm functions. 425 * 426 * LOCKING: 427 * Kernel thread context (may sleep). sysfs_mutex is locked on 428 * return. 429 */ 430 void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt, 431 struct sysfs_dirent *parent_sd) 432 { 433 memset(acxt, 0, sizeof(*acxt)); 434 acxt->parent_sd = parent_sd; 435 436 mutex_lock(&sysfs_mutex); 437 } 438 439 /** 440 * __sysfs_add_one - add sysfs_dirent to parent without warning 441 * @acxt: addrm context to use 442 * @sd: sysfs_dirent to be added 443 * 444 * Get @acxt->parent_sd and set sd->s_parent to it and increment 445 * nlink of parent inode if @sd is a directory and link into the 446 * children list of the parent. 447 * 448 * This function should be called between calls to 449 * sysfs_addrm_start() and sysfs_addrm_finish() and should be 450 * passed the same @acxt as passed to sysfs_addrm_start(). 451 * 452 * LOCKING: 453 * Determined by sysfs_addrm_start(). 454 * 455 * RETURNS: 456 * 0 on success, -EEXIST if entry with the given name already 457 * exists. 458 */ 459 int __sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd) 460 { 461 struct sysfs_inode_attrs *ps_iattr; 462 int ret; 463 464 if (!!sysfs_ns_type(acxt->parent_sd) != !!sd->s_ns) { 465 WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n", 466 sysfs_ns_type(acxt->parent_sd)? "required": "invalid", 467 acxt->parent_sd->s_name, sd->s_name); 468 return -EINVAL; 469 } 470 471 sd->s_hash = sysfs_name_hash(sd->s_ns, sd->s_name); 472 sd->s_parent = sysfs_get(acxt->parent_sd); 473 474 ret = sysfs_link_sibling(sd); 475 if (ret) 476 return ret; 477 478 /* Update timestamps on the parent */ 479 ps_iattr = acxt->parent_sd->s_iattr; 480 if (ps_iattr) { 481 struct iattr *ps_iattrs = &ps_iattr->ia_iattr; 482 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME; 483 } 484 485 return 0; 486 } 487 488 /** 489 * sysfs_pathname - return full path to sysfs dirent 490 * @sd: sysfs_dirent whose path we want 491 * @path: caller allocated buffer 492 * 493 * Gives the name "/" to the sysfs_root entry; any path returned 494 * is relative to wherever sysfs is mounted. 495 * 496 * XXX: does no error checking on @path size 497 */ 498 static char *sysfs_pathname(struct sysfs_dirent *sd, char *path) 499 { 500 if (sd->s_parent) { 501 sysfs_pathname(sd->s_parent, path); 502 strcat(path, "/"); 503 } 504 strcat(path, sd->s_name); 505 return path; 506 } 507 508 /** 509 * sysfs_add_one - add sysfs_dirent to parent 510 * @acxt: addrm context to use 511 * @sd: sysfs_dirent to be added 512 * 513 * Get @acxt->parent_sd and set sd->s_parent to it and increment 514 * nlink of parent inode if @sd is a directory and link into the 515 * children list of the parent. 516 * 517 * This function should be called between calls to 518 * sysfs_addrm_start() and sysfs_addrm_finish() and should be 519 * passed the same @acxt as passed to sysfs_addrm_start(). 520 * 521 * LOCKING: 522 * Determined by sysfs_addrm_start(). 523 * 524 * RETURNS: 525 * 0 on success, -EEXIST if entry with the given name already 526 * exists. 527 */ 528 int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd) 529 { 530 int ret; 531 532 ret = __sysfs_add_one(acxt, sd); 533 if (ret == -EEXIST) { 534 char *path = kzalloc(PATH_MAX, GFP_KERNEL); 535 WARN(1, KERN_WARNING 536 "sysfs: cannot create duplicate filename '%s'\n", 537 (path == NULL) ? sd->s_name : 538 strcat(strcat(sysfs_pathname(acxt->parent_sd, path), "/"), 539 sd->s_name)); 540 kfree(path); 541 } 542 543 return ret; 544 } 545 546 /** 547 * sysfs_remove_one - remove sysfs_dirent from parent 548 * @acxt: addrm context to use 549 * @sd: sysfs_dirent to be removed 550 * 551 * Mark @sd removed and drop nlink of parent inode if @sd is a 552 * directory. @sd is unlinked from the children list. 553 * 554 * This function should be called between calls to 555 * sysfs_addrm_start() and sysfs_addrm_finish() and should be 556 * passed the same @acxt as passed to sysfs_addrm_start(). 557 * 558 * LOCKING: 559 * Determined by sysfs_addrm_start(). 560 */ 561 void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd) 562 { 563 struct sysfs_inode_attrs *ps_iattr; 564 565 BUG_ON(sd->s_flags & SYSFS_FLAG_REMOVED); 566 567 sysfs_unlink_sibling(sd); 568 569 /* Update timestamps on the parent */ 570 ps_iattr = acxt->parent_sd->s_iattr; 571 if (ps_iattr) { 572 struct iattr *ps_iattrs = &ps_iattr->ia_iattr; 573 ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME; 574 } 575 576 sd->s_flags |= SYSFS_FLAG_REMOVED; 577 sd->u.removed_list = acxt->removed; 578 acxt->removed = sd; 579 } 580 581 /** 582 * sysfs_addrm_finish - finish up sysfs_dirent add/remove 583 * @acxt: addrm context to finish up 584 * 585 * Finish up sysfs_dirent add/remove. Resources acquired by 586 * sysfs_addrm_start() are released and removed sysfs_dirents are 587 * cleaned up. 588 * 589 * LOCKING: 590 * sysfs_mutex is released. 591 */ 592 void sysfs_addrm_finish(struct sysfs_addrm_cxt *acxt) 593 { 594 /* release resources acquired by sysfs_addrm_start() */ 595 mutex_unlock(&sysfs_mutex); 596 597 /* kill removed sysfs_dirents */ 598 while (acxt->removed) { 599 struct sysfs_dirent *sd = acxt->removed; 600 601 acxt->removed = sd->u.removed_list; 602 603 sysfs_deactivate(sd); 604 unmap_bin_file(sd); 605 sysfs_put(sd); 606 } 607 } 608 609 /** 610 * sysfs_find_dirent - find sysfs_dirent with the given name 611 * @parent_sd: sysfs_dirent to search under 612 * @name: name to look for 613 * 614 * Look for sysfs_dirent with name @name under @parent_sd. 615 * 616 * LOCKING: 617 * mutex_lock(sysfs_mutex) 618 * 619 * RETURNS: 620 * Pointer to sysfs_dirent if found, NULL if not. 621 */ 622 struct sysfs_dirent *sysfs_find_dirent(struct sysfs_dirent *parent_sd, 623 const void *ns, 624 const unsigned char *name) 625 { 626 struct rb_node *node = parent_sd->s_dir.children.rb_node; 627 unsigned int hash; 628 629 if (!!sysfs_ns_type(parent_sd) != !!ns) { 630 WARN(1, KERN_WARNING "sysfs: ns %s in '%s' for '%s'\n", 631 sysfs_ns_type(parent_sd)? "required": "invalid", 632 parent_sd->s_name, name); 633 return NULL; 634 } 635 636 hash = sysfs_name_hash(ns, name); 637 while (node) { 638 struct sysfs_dirent *sd; 639 int result; 640 641 sd = to_sysfs_dirent(node); 642 result = sysfs_name_compare(hash, ns, name, sd); 643 if (result < 0) 644 node = node->rb_left; 645 else if (result > 0) 646 node = node->rb_right; 647 else 648 return sd; 649 } 650 return NULL; 651 } 652 653 /** 654 * sysfs_get_dirent - find and get sysfs_dirent with the given name 655 * @parent_sd: sysfs_dirent to search under 656 * @name: name to look for 657 * 658 * Look for sysfs_dirent with name @name under @parent_sd and get 659 * it if found. 660 * 661 * LOCKING: 662 * Kernel thread context (may sleep). Grabs sysfs_mutex. 663 * 664 * RETURNS: 665 * Pointer to sysfs_dirent if found, NULL if not. 666 */ 667 struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd, 668 const void *ns, 669 const unsigned char *name) 670 { 671 struct sysfs_dirent *sd; 672 673 mutex_lock(&sysfs_mutex); 674 sd = sysfs_find_dirent(parent_sd, ns, name); 675 sysfs_get(sd); 676 mutex_unlock(&sysfs_mutex); 677 678 return sd; 679 } 680 EXPORT_SYMBOL_GPL(sysfs_get_dirent); 681 682 static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, 683 enum kobj_ns_type type, const void *ns, const char *name, 684 struct sysfs_dirent **p_sd) 685 { 686 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; 687 struct sysfs_addrm_cxt acxt; 688 struct sysfs_dirent *sd; 689 int rc; 690 691 /* allocate */ 692 sd = sysfs_new_dirent(name, mode, SYSFS_DIR); 693 if (!sd) 694 return -ENOMEM; 695 696 sd->s_flags |= (type << SYSFS_NS_TYPE_SHIFT); 697 sd->s_ns = ns; 698 sd->s_dir.kobj = kobj; 699 700 /* link in */ 701 sysfs_addrm_start(&acxt, parent_sd); 702 rc = sysfs_add_one(&acxt, sd); 703 sysfs_addrm_finish(&acxt); 704 705 if (rc == 0) 706 *p_sd = sd; 707 else 708 sysfs_put(sd); 709 710 return rc; 711 } 712 713 int sysfs_create_subdir(struct kobject *kobj, const char *name, 714 struct sysfs_dirent **p_sd) 715 { 716 return create_dir(kobj, kobj->sd, 717 KOBJ_NS_TYPE_NONE, NULL, name, p_sd); 718 } 719 720 /** 721 * sysfs_read_ns_type: return associated ns_type 722 * @kobj: the kobject being queried 723 * 724 * Each kobject can be tagged with exactly one namespace type 725 * (i.e. network or user). Return the ns_type associated with 726 * this object if any 727 */ 728 static enum kobj_ns_type sysfs_read_ns_type(struct kobject *kobj) 729 { 730 const struct kobj_ns_type_operations *ops; 731 enum kobj_ns_type type; 732 733 ops = kobj_child_ns_ops(kobj); 734 if (!ops) 735 return KOBJ_NS_TYPE_NONE; 736 737 type = ops->type; 738 BUG_ON(type <= KOBJ_NS_TYPE_NONE); 739 BUG_ON(type >= KOBJ_NS_TYPES); 740 BUG_ON(!kobj_ns_type_registered(type)); 741 742 return type; 743 } 744 745 /** 746 * sysfs_create_dir - create a directory for an object. 747 * @kobj: object we're creating directory for. 748 */ 749 int sysfs_create_dir(struct kobject * kobj) 750 { 751 enum kobj_ns_type type; 752 struct sysfs_dirent *parent_sd, *sd; 753 const void *ns = NULL; 754 int error = 0; 755 756 BUG_ON(!kobj); 757 758 if (kobj->parent) 759 parent_sd = kobj->parent->sd; 760 else 761 parent_sd = &sysfs_root; 762 763 if (!parent_sd) 764 return -ENOENT; 765 766 if (sysfs_ns_type(parent_sd)) 767 ns = kobj->ktype->namespace(kobj); 768 type = sysfs_read_ns_type(kobj); 769 770 error = create_dir(kobj, parent_sd, type, ns, kobject_name(kobj), &sd); 771 if (!error) 772 kobj->sd = sd; 773 return error; 774 } 775 776 static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, 777 struct nameidata *nd) 778 { 779 struct dentry *ret = NULL; 780 struct dentry *parent = dentry->d_parent; 781 struct sysfs_dirent *parent_sd = parent->d_fsdata; 782 struct sysfs_dirent *sd; 783 struct inode *inode; 784 enum kobj_ns_type type; 785 const void *ns; 786 787 mutex_lock(&sysfs_mutex); 788 789 type = sysfs_ns_type(parent_sd); 790 ns = sysfs_info(dir->i_sb)->ns[type]; 791 792 sd = sysfs_find_dirent(parent_sd, ns, dentry->d_name.name); 793 794 /* no such entry */ 795 if (!sd) { 796 ret = ERR_PTR(-ENOENT); 797 goto out_unlock; 798 } 799 800 /* attach dentry and inode */ 801 inode = sysfs_get_inode(dir->i_sb, sd); 802 if (!inode) { 803 ret = ERR_PTR(-ENOMEM); 804 goto out_unlock; 805 } 806 807 /* instantiate and hash dentry */ 808 ret = d_find_alias(inode); 809 if (!ret) { 810 d_set_d_op(dentry, &sysfs_dentry_ops); 811 dentry->d_fsdata = sysfs_get(sd); 812 d_add(dentry, inode); 813 } else { 814 d_move(ret, dentry); 815 iput(inode); 816 } 817 818 out_unlock: 819 mutex_unlock(&sysfs_mutex); 820 return ret; 821 } 822 823 const struct inode_operations sysfs_dir_inode_operations = { 824 .lookup = sysfs_lookup, 825 .permission = sysfs_permission, 826 .setattr = sysfs_setattr, 827 .getattr = sysfs_getattr, 828 .setxattr = sysfs_setxattr, 829 }; 830 831 static void remove_dir(struct sysfs_dirent *sd) 832 { 833 struct sysfs_addrm_cxt acxt; 834 835 sysfs_addrm_start(&acxt, sd->s_parent); 836 sysfs_remove_one(&acxt, sd); 837 sysfs_addrm_finish(&acxt); 838 } 839 840 void sysfs_remove_subdir(struct sysfs_dirent *sd) 841 { 842 remove_dir(sd); 843 } 844 845 846 static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd) 847 { 848 struct sysfs_addrm_cxt acxt; 849 struct rb_node *pos; 850 851 if (!dir_sd) 852 return; 853 854 pr_debug("sysfs %s: removing dir\n", dir_sd->s_name); 855 sysfs_addrm_start(&acxt, dir_sd); 856 pos = rb_first(&dir_sd->s_dir.children); 857 while (pos) { 858 struct sysfs_dirent *sd = to_sysfs_dirent(pos); 859 pos = rb_next(pos); 860 if (sysfs_type(sd) != SYSFS_DIR) 861 sysfs_remove_one(&acxt, sd); 862 } 863 sysfs_addrm_finish(&acxt); 864 865 remove_dir(dir_sd); 866 } 867 868 /** 869 * sysfs_remove_dir - remove an object's directory. 870 * @kobj: object. 871 * 872 * The only thing special about this is that we remove any files in 873 * the directory before we remove the directory, and we've inlined 874 * what used to be sysfs_rmdir() below, instead of calling separately. 875 */ 876 877 void sysfs_remove_dir(struct kobject * kobj) 878 { 879 struct sysfs_dirent *sd = kobj->sd; 880 881 spin_lock(&sysfs_assoc_lock); 882 kobj->sd = NULL; 883 spin_unlock(&sysfs_assoc_lock); 884 885 __sysfs_remove_dir(sd); 886 } 887 888 int sysfs_rename(struct sysfs_dirent *sd, 889 struct sysfs_dirent *new_parent_sd, const void *new_ns, 890 const char *new_name) 891 { 892 int error; 893 894 mutex_lock(&sysfs_mutex); 895 896 error = 0; 897 if ((sd->s_parent == new_parent_sd) && (sd->s_ns == new_ns) && 898 (strcmp(sd->s_name, new_name) == 0)) 899 goto out; /* nothing to rename */ 900 901 error = -EEXIST; 902 if (sysfs_find_dirent(new_parent_sd, new_ns, new_name)) 903 goto out; 904 905 /* rename sysfs_dirent */ 906 if (strcmp(sd->s_name, new_name) != 0) { 907 error = -ENOMEM; 908 new_name = kstrdup(new_name, GFP_KERNEL); 909 if (!new_name) 910 goto out; 911 912 kfree(sd->s_name); 913 sd->s_name = new_name; 914 } 915 916 /* Move to the appropriate place in the appropriate directories rbtree. */ 917 sysfs_unlink_sibling(sd); 918 sysfs_get(new_parent_sd); 919 sysfs_put(sd->s_parent); 920 sd->s_ns = new_ns; 921 sd->s_hash = sysfs_name_hash(sd->s_ns, sd->s_name); 922 sd->s_parent = new_parent_sd; 923 sysfs_link_sibling(sd); 924 925 error = 0; 926 out: 927 mutex_unlock(&sysfs_mutex); 928 return error; 929 } 930 931 int sysfs_rename_dir(struct kobject *kobj, const char *new_name) 932 { 933 struct sysfs_dirent *parent_sd = kobj->sd->s_parent; 934 const void *new_ns = NULL; 935 936 if (sysfs_ns_type(parent_sd)) 937 new_ns = kobj->ktype->namespace(kobj); 938 939 return sysfs_rename(kobj->sd, parent_sd, new_ns, new_name); 940 } 941 942 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj) 943 { 944 struct sysfs_dirent *sd = kobj->sd; 945 struct sysfs_dirent *new_parent_sd; 946 const void *new_ns = NULL; 947 948 BUG_ON(!sd->s_parent); 949 if (sysfs_ns_type(sd->s_parent)) 950 new_ns = kobj->ktype->namespace(kobj); 951 new_parent_sd = new_parent_kobj && new_parent_kobj->sd ? 952 new_parent_kobj->sd : &sysfs_root; 953 954 return sysfs_rename(sd, new_parent_sd, new_ns, sd->s_name); 955 } 956 957 /* Relationship between s_mode and the DT_xxx types */ 958 static inline unsigned char dt_type(struct sysfs_dirent *sd) 959 { 960 return (sd->s_mode >> 12) & 15; 961 } 962 963 static int sysfs_dir_release(struct inode *inode, struct file *filp) 964 { 965 sysfs_put(filp->private_data); 966 return 0; 967 } 968 969 static struct sysfs_dirent *sysfs_dir_pos(const void *ns, 970 struct sysfs_dirent *parent_sd, loff_t hash, struct sysfs_dirent *pos) 971 { 972 if (pos) { 973 int valid = !(pos->s_flags & SYSFS_FLAG_REMOVED) && 974 pos->s_parent == parent_sd && 975 hash == pos->s_hash; 976 sysfs_put(pos); 977 if (!valid) 978 pos = NULL; 979 } 980 if (!pos && (hash > 1) && (hash < INT_MAX)) { 981 struct rb_node *node = parent_sd->s_dir.children.rb_node; 982 while (node) { 983 pos = to_sysfs_dirent(node); 984 985 if (hash < pos->s_hash) 986 node = node->rb_left; 987 else if (hash > pos->s_hash) 988 node = node->rb_right; 989 else 990 break; 991 } 992 } 993 /* Skip over entries in the wrong namespace */ 994 while (pos && pos->s_ns != ns) { 995 struct rb_node *node = rb_next(&pos->s_rb); 996 if (!node) 997 pos = NULL; 998 else 999 pos = to_sysfs_dirent(node); 1000 } 1001 return pos; 1002 } 1003 1004 static struct sysfs_dirent *sysfs_dir_next_pos(const void *ns, 1005 struct sysfs_dirent *parent_sd, ino_t ino, struct sysfs_dirent *pos) 1006 { 1007 pos = sysfs_dir_pos(ns, parent_sd, ino, pos); 1008 if (pos) do { 1009 struct rb_node *node = rb_next(&pos->s_rb); 1010 if (!node) 1011 pos = NULL; 1012 else 1013 pos = to_sysfs_dirent(node); 1014 } while (pos && pos->s_ns != ns); 1015 return pos; 1016 } 1017 1018 static int sysfs_readdir(struct file * filp, void * dirent, filldir_t filldir) 1019 { 1020 struct dentry *dentry = filp->f_path.dentry; 1021 struct sysfs_dirent * parent_sd = dentry->d_fsdata; 1022 struct sysfs_dirent *pos = filp->private_data; 1023 enum kobj_ns_type type; 1024 const void *ns; 1025 ino_t ino; 1026 1027 type = sysfs_ns_type(parent_sd); 1028 ns = sysfs_info(dentry->d_sb)->ns[type]; 1029 1030 if (filp->f_pos == 0) { 1031 ino = parent_sd->s_ino; 1032 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) == 0) 1033 filp->f_pos++; 1034 } 1035 if (filp->f_pos == 1) { 1036 if (parent_sd->s_parent) 1037 ino = parent_sd->s_parent->s_ino; 1038 else 1039 ino = parent_sd->s_ino; 1040 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) == 0) 1041 filp->f_pos++; 1042 } 1043 mutex_lock(&sysfs_mutex); 1044 for (pos = sysfs_dir_pos(ns, parent_sd, filp->f_pos, pos); 1045 pos; 1046 pos = sysfs_dir_next_pos(ns, parent_sd, filp->f_pos, pos)) { 1047 const char * name; 1048 unsigned int type; 1049 int len, ret; 1050 1051 name = pos->s_name; 1052 len = strlen(name); 1053 ino = pos->s_ino; 1054 type = dt_type(pos); 1055 filp->f_pos = pos->s_hash; 1056 filp->private_data = sysfs_get(pos); 1057 1058 mutex_unlock(&sysfs_mutex); 1059 ret = filldir(dirent, name, len, filp->f_pos, ino, type); 1060 mutex_lock(&sysfs_mutex); 1061 if (ret < 0) 1062 break; 1063 } 1064 mutex_unlock(&sysfs_mutex); 1065 if ((filp->f_pos > 1) && !pos) { /* EOF */ 1066 filp->f_pos = INT_MAX; 1067 filp->private_data = NULL; 1068 } 1069 return 0; 1070 } 1071 1072 1073 const struct file_operations sysfs_dir_operations = { 1074 .read = generic_read_dir, 1075 .readdir = sysfs_readdir, 1076 .release = sysfs_dir_release, 1077 .llseek = generic_file_llseek, 1078 }; 1079