1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * fs/kernfs/dir.c - kernfs directory implementation 4 * 5 * Copyright (c) 2001-3 Patrick Mochel 6 * Copyright (c) 2007 SUSE Linux Products GmbH 7 * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org> 8 */ 9 10 #include <linux/sched.h> 11 #include <linux/fs.h> 12 #include <linux/namei.h> 13 #include <linux/idr.h> 14 #include <linux/slab.h> 15 #include <linux/security.h> 16 #include <linux/hash.h> 17 #include <linux/ns_common.h> 18 19 #include "kernfs-internal.h" 20 21 /* 22 * Don't use rename_lock to piggy back on pr_cont_buf. We don't want to 23 * call pr_cont() while holding rename_lock. Because sometimes pr_cont() 24 * will perform wakeups when releasing console_sem. Holding rename_lock 25 * will introduce deadlock if the scheduler reads the kernfs_name in the 26 * wakeup path. 27 */ 28 static DEFINE_SPINLOCK(kernfs_pr_cont_lock); 29 static char kernfs_pr_cont_buf[PATH_MAX]; /* protected by pr_cont_lock */ 30 31 #define rb_to_kn(X) rb_entry((X), struct kernfs_node, rb) 32 33 static bool __kernfs_active(struct kernfs_node *kn) 34 { 35 return atomic_read(&kn->active) >= 0; 36 } 37 38 static bool kernfs_active(struct kernfs_node *kn) 39 { 40 lockdep_assert_held(&kernfs_root(kn)->kernfs_rwsem); 41 return __kernfs_active(kn); 42 } 43 44 static bool kernfs_lockdep(struct kernfs_node *kn) 45 { 46 #ifdef CONFIG_DEBUG_LOCK_ALLOC 47 return kn->flags & KERNFS_LOCKDEP; 48 #else 49 return false; 50 #endif 51 } 52 53 /* kernfs_node_depth - compute depth from @from to @to */ 54 static size_t kernfs_depth(struct kernfs_node *from, struct kernfs_node *to) 55 { 56 size_t depth = 0; 57 58 while (rcu_dereference(to->__parent) && to != from) { 59 depth++; 60 to = rcu_dereference(to->__parent); 61 } 62 return depth; 63 } 64 65 static struct kernfs_node *kernfs_common_ancestor(struct kernfs_node *a, 66 struct kernfs_node *b) 67 { 68 size_t da, db; 69 struct kernfs_root *ra = kernfs_root(a), *rb = kernfs_root(b); 70 71 if (ra != rb) 72 return NULL; 73 74 da = kernfs_depth(ra->kn, a); 75 db = kernfs_depth(rb->kn, b); 76 77 while (da > db) { 78 a = rcu_dereference(a->__parent); 79 da--; 80 } 81 while (db > da) { 82 b = rcu_dereference(b->__parent); 83 db--; 84 } 85 86 /* worst case b and a will be the same at root */ 87 while (b != a) { 88 b = rcu_dereference(b->__parent); 89 a = rcu_dereference(a->__parent); 90 } 91 92 return a; 93 } 94 95 /** 96 * kernfs_path_from_node_locked - find a pseudo-absolute path to @kn_to, 97 * where kn_from is treated as root of the path. 98 * @kn_from: kernfs node which should be treated as root for the path 99 * @kn_to: kernfs node to which path is needed 100 * @buf: buffer to copy the path into 101 * @buflen: size of @buf 102 * 103 * We need to handle couple of scenarios here: 104 * [1] when @kn_from is an ancestor of @kn_to at some level 105 * kn_from: /n1/n2/n3 106 * kn_to: /n1/n2/n3/n4/n5 107 * result: /n4/n5 108 * 109 * [2] when @kn_from is on a different hierarchy and we need to find common 110 * ancestor between @kn_from and @kn_to. 111 * kn_from: /n1/n2/n3/n4 112 * kn_to: /n1/n2/n5 113 * result: /../../n5 114 * OR 115 * kn_from: /n1/n2/n3/n4/n5 [depth=5] 116 * kn_to: /n1/n2/n3 [depth=3] 117 * result: /../.. 118 * 119 * [3] when @kn_to is %NULL result will be "(null)" 120 * 121 * Return: the length of the constructed path. If the path would have been 122 * greater than @buflen, @buf contains the truncated path with the trailing 123 * '\0'. On error, -errno is returned. 124 */ 125 static int kernfs_path_from_node_locked(struct kernfs_node *kn_to, 126 struct kernfs_node *kn_from, 127 char *buf, size_t buflen) 128 { 129 struct kernfs_node *kn, *common; 130 const char parent_str[] = "/.."; 131 size_t depth_from, depth_to, len = 0; 132 ssize_t copied; 133 int i, j; 134 135 if (!kn_to) 136 return strscpy(buf, "(null)", buflen); 137 138 if (!kn_from) 139 kn_from = kernfs_root(kn_to)->kn; 140 141 if (kn_from == kn_to) 142 return strscpy(buf, "/", buflen); 143 144 common = kernfs_common_ancestor(kn_from, kn_to); 145 if (WARN_ON(!common)) 146 return -EINVAL; 147 148 depth_to = kernfs_depth(common, kn_to); 149 depth_from = kernfs_depth(common, kn_from); 150 151 buf[0] = '\0'; 152 153 for (i = 0; i < depth_from; i++) { 154 copied = strscpy(buf + len, parent_str, buflen - len); 155 if (copied < 0) 156 return copied; 157 len += copied; 158 } 159 160 /* Calculate how many bytes we need for the rest */ 161 for (i = depth_to - 1; i >= 0; i--) { 162 const char *name; 163 164 for (kn = kn_to, j = 0; j < i; j++) 165 kn = rcu_dereference(kn->__parent); 166 167 name = rcu_dereference(kn->name); 168 len += scnprintf(buf + len, buflen - len, "/%s", name); 169 } 170 171 return len; 172 } 173 174 /** 175 * kernfs_name - obtain the name of a given node 176 * @kn: kernfs_node of interest 177 * @buf: buffer to copy @kn's name into 178 * @buflen: size of @buf 179 * 180 * Copies the name of @kn into @buf of @buflen bytes. The behavior is 181 * similar to strscpy(). 182 * 183 * Fills buffer with "(null)" if @kn is %NULL. 184 * 185 * Return: the resulting length of @buf. If @buf isn't long enough, 186 * it's filled up to @buflen-1 and nul terminated, and returns -E2BIG. 187 * 188 * This function can be called from any context. 189 */ 190 int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen) 191 { 192 struct kernfs_node *kn_parent; 193 194 if (!kn) 195 return strscpy(buf, "(null)", buflen); 196 197 guard(rcu)(); 198 /* 199 * KERNFS_ROOT_INVARIANT_PARENT is ignored here. The name is RCU freed and 200 * the parent is either existing or not. 201 */ 202 kn_parent = rcu_dereference(kn->__parent); 203 return strscpy(buf, kn_parent ? rcu_dereference(kn->name) : "/", buflen); 204 } 205 206 /** 207 * kernfs_path_from_node - build path of node @to relative to @from. 208 * @from: parent kernfs_node relative to which we need to build the path 209 * @to: kernfs_node of interest 210 * @buf: buffer to copy @to's path into 211 * @buflen: size of @buf 212 * 213 * Builds @to's path relative to @from in @buf. @from and @to must 214 * be on the same kernfs-root. If @from is not parent of @to, then a relative 215 * path (which includes '..'s) as needed to reach from @from to @to is 216 * returned. 217 * 218 * Return: the length of the constructed path. If the path would have been 219 * greater than @buflen, @buf contains the truncated path with the trailing 220 * '\0'. On error, -errno is returned. 221 */ 222 int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from, 223 char *buf, size_t buflen) 224 { 225 struct kernfs_root *root; 226 227 guard(rcu)(); 228 if (to) { 229 root = kernfs_root(to); 230 if (!(root->flags & KERNFS_ROOT_INVARIANT_PARENT)) { 231 guard(read_lock_irqsave)(&root->kernfs_rename_lock); 232 return kernfs_path_from_node_locked(to, from, buf, buflen); 233 } 234 } 235 return kernfs_path_from_node_locked(to, from, buf, buflen); 236 } 237 EXPORT_SYMBOL_GPL(kernfs_path_from_node); 238 239 /** 240 * pr_cont_kernfs_name - pr_cont name of a kernfs_node 241 * @kn: kernfs_node of interest 242 * 243 * This function can be called from any context. 244 */ 245 void pr_cont_kernfs_name(struct kernfs_node *kn) 246 { 247 unsigned long flags; 248 249 spin_lock_irqsave(&kernfs_pr_cont_lock, flags); 250 251 kernfs_name(kn, kernfs_pr_cont_buf, sizeof(kernfs_pr_cont_buf)); 252 pr_cont("%s", kernfs_pr_cont_buf); 253 254 spin_unlock_irqrestore(&kernfs_pr_cont_lock, flags); 255 } 256 257 /** 258 * pr_cont_kernfs_path - pr_cont path of a kernfs_node 259 * @kn: kernfs_node of interest 260 * 261 * This function can be called from any context. 262 */ 263 void pr_cont_kernfs_path(struct kernfs_node *kn) 264 { 265 unsigned long flags; 266 int sz; 267 268 spin_lock_irqsave(&kernfs_pr_cont_lock, flags); 269 270 sz = kernfs_path_from_node(kn, NULL, kernfs_pr_cont_buf, 271 sizeof(kernfs_pr_cont_buf)); 272 if (sz < 0) { 273 if (sz == -E2BIG) 274 pr_cont("(name too long)"); 275 else 276 pr_cont("(error)"); 277 goto out; 278 } 279 280 pr_cont("%s", kernfs_pr_cont_buf); 281 282 out: 283 spin_unlock_irqrestore(&kernfs_pr_cont_lock, flags); 284 } 285 286 /** 287 * kernfs_get_parent - determine the parent node and pin it 288 * @kn: kernfs_node of interest 289 * 290 * Determines @kn's parent, pins and returns it. This function can be 291 * called from any context. 292 * 293 * Return: parent node of @kn 294 */ 295 struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn) 296 { 297 struct kernfs_node *parent; 298 struct kernfs_root *root; 299 unsigned long flags; 300 301 root = kernfs_root(kn); 302 read_lock_irqsave(&root->kernfs_rename_lock, flags); 303 parent = kernfs_parent(kn); 304 kernfs_get(parent); 305 read_unlock_irqrestore(&root->kernfs_rename_lock, flags); 306 307 return parent; 308 } 309 310 /* 311 * kernfs_ns_id - return the namespace id for a given namespace 312 * @ns: namespace tag (may be NULL) 313 * 314 * Use the 64-bit namespace id instead of raw pointers for hashing 315 * and comparison to avoid leaking kernel addresses to userspace. 316 */ 317 static u64 kernfs_ns_id(const struct ns_common *ns) 318 { 319 return ns ? ns->ns_id : 0; 320 } 321 322 /** 323 * kernfs_name_hash - calculate hash of @ns + @name 324 * @name: Null terminated string to hash 325 * @ns: Namespace tag to hash 326 * 327 * Return: 31-bit hash of ns + name (so it fits in an off_t) 328 */ 329 static unsigned int kernfs_name_hash(const char *name, 330 const struct ns_common *ns) 331 { 332 unsigned long hash = init_name_hash(kernfs_ns_id(ns)); 333 unsigned int len = strlen(name); 334 while (len--) 335 hash = partial_name_hash(*name++, hash); 336 hash = end_name_hash(hash); 337 hash &= 0x7fffffffU; 338 /* Reserve hash numbers 0, 1 and INT_MAX for magic directory entries */ 339 if (hash < 2) 340 hash += 2; 341 if (hash >= INT_MAX) 342 hash = INT_MAX - 1; 343 return hash; 344 } 345 346 static int kernfs_name_compare(unsigned int hash, const char *name, 347 const struct ns_common *ns, const struct kernfs_node *kn) 348 { 349 u64 ns_id = kernfs_ns_id(ns); 350 u64 kn_ns_id = kernfs_ns_id(kn->ns); 351 352 if (hash < kn->hash) 353 return -1; 354 if (hash > kn->hash) 355 return 1; 356 if (ns_id < kn_ns_id) 357 return -1; 358 if (ns_id > kn_ns_id) 359 return 1; 360 return strcmp(name, kernfs_rcu_name(kn)); 361 } 362 363 static int kernfs_sd_compare(const struct kernfs_node *left, 364 const struct kernfs_node *right) 365 { 366 return kernfs_name_compare(left->hash, kernfs_rcu_name(left), left->ns, right); 367 } 368 369 /** 370 * kernfs_link_sibling - link kernfs_node into sibling rbtree 371 * @kn: kernfs_node of interest 372 * 373 * Link @kn into its sibling rbtree which starts from 374 * @kn->parent->dir.children. 375 * 376 * Locking: 377 * kernfs_rwsem held exclusive 378 * 379 * Return: 380 * %0 on success, -EEXIST on failure. 381 */ 382 static int kernfs_link_sibling(struct kernfs_node *kn) 383 { 384 struct rb_node *parent = NULL; 385 struct kernfs_node *kn_parent; 386 struct rb_node **node; 387 388 kn_parent = kernfs_parent(kn); 389 node = &kn_parent->dir.children.rb_node; 390 391 while (*node) { 392 struct kernfs_node *pos; 393 int result; 394 395 pos = rb_to_kn(*node); 396 parent = *node; 397 result = kernfs_sd_compare(kn, pos); 398 if (result < 0) 399 node = &pos->rb.rb_left; 400 else if (result > 0) 401 node = &pos->rb.rb_right; 402 else 403 return -EEXIST; 404 } 405 406 /* add new node and rebalance the tree */ 407 rb_link_node(&kn->rb, parent, node); 408 rb_insert_color(&kn->rb, &kn_parent->dir.children); 409 410 /* successfully added, account subdir number */ 411 down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); 412 if (kernfs_type(kn) == KERNFS_DIR) 413 kn_parent->dir.subdirs++; 414 kernfs_inc_rev(kn_parent); 415 up_write(&kernfs_root(kn)->kernfs_iattr_rwsem); 416 417 return 0; 418 } 419 420 /** 421 * kernfs_unlink_sibling - unlink kernfs_node from sibling rbtree 422 * @kn: kernfs_node of interest 423 * 424 * Try to unlink @kn from its sibling rbtree which starts from 425 * kn->parent->dir.children. 426 * 427 * Return: %true if @kn was actually removed, 428 * %false if @kn wasn't on the rbtree. 429 * 430 * Locking: 431 * kernfs_rwsem held exclusive 432 */ 433 static bool kernfs_unlink_sibling(struct kernfs_node *kn) 434 { 435 struct kernfs_node *kn_parent; 436 437 if (RB_EMPTY_NODE(&kn->rb)) 438 return false; 439 440 kn_parent = kernfs_parent(kn); 441 down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); 442 if (kernfs_type(kn) == KERNFS_DIR) 443 kn_parent->dir.subdirs--; 444 kernfs_inc_rev(kn_parent); 445 up_write(&kernfs_root(kn)->kernfs_iattr_rwsem); 446 447 rb_erase(&kn->rb, &kn_parent->dir.children); 448 RB_CLEAR_NODE(&kn->rb); 449 return true; 450 } 451 452 /** 453 * kernfs_get_active - get an active reference to kernfs_node 454 * @kn: kernfs_node to get an active reference to 455 * 456 * Get an active reference of @kn. This function is noop if @kn 457 * is %NULL. 458 * 459 * Return: 460 * Pointer to @kn on success, %NULL on failure. 461 */ 462 struct kernfs_node *kernfs_get_active(struct kernfs_node *kn) 463 { 464 if (unlikely(!kn)) 465 return NULL; 466 467 if (!atomic_inc_unless_negative(&kn->active)) 468 return NULL; 469 470 if (kernfs_lockdep(kn)) 471 rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_); 472 return kn; 473 } 474 475 /** 476 * kernfs_put_active - put an active reference to kernfs_node 477 * @kn: kernfs_node to put an active reference to 478 * 479 * Put an active reference to @kn. This function is noop if @kn 480 * is %NULL. 481 */ 482 void kernfs_put_active(struct kernfs_node *kn) 483 { 484 int v; 485 486 if (unlikely(!kn)) 487 return; 488 489 if (kernfs_lockdep(kn)) 490 rwsem_release(&kn->dep_map, _RET_IP_); 491 v = atomic_dec_return(&kn->active); 492 if (likely(v != KN_DEACTIVATED_BIAS)) 493 return; 494 495 wake_up_all(&kernfs_root(kn)->deactivate_waitq); 496 } 497 498 /** 499 * kernfs_drain - drain kernfs_node 500 * @kn: kernfs_node to drain 501 * 502 * Drain existing usages and nuke all existing mmaps of @kn. Multiple 503 * removers may invoke this function concurrently on @kn and all will 504 * return after draining is complete. 505 */ 506 static void kernfs_drain(struct kernfs_node *kn) 507 __releases(&kernfs_root(kn)->kernfs_rwsem) 508 __acquires(&kernfs_root(kn)->kernfs_rwsem) 509 { 510 struct kernfs_root *root = kernfs_root(kn); 511 512 lockdep_assert_held_write(&root->kernfs_rwsem); 513 WARN_ON_ONCE(kernfs_active(kn)); 514 515 /* 516 * Skip draining if already fully drained. This avoids draining and its 517 * lockdep annotations for nodes which have never been activated 518 * allowing embedding kernfs_remove() in create error paths without 519 * worrying about draining. 520 */ 521 if (atomic_read(&kn->active) == KN_DEACTIVATED_BIAS && 522 !kernfs_should_drain_open_files(kn)) 523 return; 524 525 up_write(&root->kernfs_rwsem); 526 527 if (kernfs_lockdep(kn)) { 528 rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_); 529 if (atomic_read(&kn->active) != KN_DEACTIVATED_BIAS) 530 lock_contended(&kn->dep_map, _RET_IP_); 531 } 532 533 wait_event(root->deactivate_waitq, 534 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS); 535 536 if (kernfs_lockdep(kn)) { 537 lock_acquired(&kn->dep_map, _RET_IP_); 538 rwsem_release(&kn->dep_map, _RET_IP_); 539 } 540 541 if (kernfs_should_drain_open_files(kn)) 542 kernfs_drain_open_files(kn); 543 544 down_write(&root->kernfs_rwsem); 545 } 546 547 /** 548 * kernfs_get - get a reference count on a kernfs_node 549 * @kn: the target kernfs_node 550 */ 551 void kernfs_get(struct kernfs_node *kn) 552 { 553 if (kn) { 554 WARN_ON(!atomic_read(&kn->count)); 555 atomic_inc(&kn->count); 556 } 557 } 558 EXPORT_SYMBOL_GPL(kernfs_get); 559 560 static void kernfs_free_rcu(struct rcu_head *rcu) 561 { 562 struct kernfs_node *kn = container_of(rcu, struct kernfs_node, rcu); 563 564 /* If the whole node goes away, then name can't be used outside */ 565 kfree_const(rcu_access_pointer(kn->name)); 566 567 if (kn->iattr) { 568 simple_xattrs_free(&kn->iattr->xattrs, NULL); 569 kmem_cache_free(kernfs_iattrs_cache, kn->iattr); 570 } 571 572 kmem_cache_free(kernfs_node_cache, kn); 573 } 574 575 /** 576 * kernfs_put - put a reference count on a kernfs_node 577 * @kn: the target kernfs_node 578 * 579 * Put a reference count of @kn and destroy it if it reached zero. 580 */ 581 void kernfs_put(struct kernfs_node *kn) 582 { 583 struct kernfs_node *parent; 584 struct kernfs_root *root; 585 586 if (!kn || !atomic_dec_and_test(&kn->count)) 587 return; 588 root = kernfs_root(kn); 589 repeat: 590 /* 591 * Moving/renaming is always done while holding reference. 592 * kn->parent won't change beneath us. 593 */ 594 parent = kernfs_parent(kn); 595 596 WARN_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS, 597 "kernfs_put: %s/%s: released with incorrect active_ref %d\n", 598 parent ? rcu_dereference(parent->name) : "", 599 rcu_dereference(kn->name), atomic_read(&kn->active)); 600 601 if (kernfs_type(kn) == KERNFS_LINK) 602 kernfs_put(kn->symlink.target_kn); 603 604 spin_lock(&root->kernfs_idr_lock); 605 idr_remove(&root->ino_idr, (u32)kernfs_ino(kn)); 606 spin_unlock(&root->kernfs_idr_lock); 607 608 call_rcu(&kn->rcu, kernfs_free_rcu); 609 610 kn = parent; 611 if (kn) { 612 if (atomic_dec_and_test(&kn->count)) 613 goto repeat; 614 } else { 615 /* just released the root kn, free @root too */ 616 idr_destroy(&root->ino_idr); 617 kfree_rcu(root, rcu); 618 } 619 } 620 EXPORT_SYMBOL_GPL(kernfs_put); 621 622 /** 623 * kernfs_node_from_dentry - determine kernfs_node associated with a dentry 624 * @dentry: the dentry in question 625 * 626 * Return: the kernfs_node associated with @dentry. If @dentry is not a 627 * kernfs one, %NULL is returned. 628 * 629 * While the returned kernfs_node will stay accessible as long as @dentry 630 * is accessible, the returned node can be in any state and the caller is 631 * fully responsible for determining what's accessible. 632 */ 633 struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry) 634 { 635 if (dentry->d_sb->s_op == &kernfs_sops) 636 return kernfs_dentry_node(dentry); 637 return NULL; 638 } 639 640 static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, 641 struct kernfs_node *parent, 642 const char *name, umode_t mode, 643 kuid_t uid, kgid_t gid, 644 unsigned flags) 645 { 646 struct kernfs_node *kn; 647 u32 id_highbits; 648 int ret; 649 650 name = kstrdup_const(name, GFP_KERNEL); 651 if (!name) 652 return NULL; 653 654 kn = kmem_cache_zalloc(kernfs_node_cache, GFP_KERNEL); 655 if (!kn) 656 goto err_out1; 657 658 idr_preload(GFP_KERNEL); 659 spin_lock(&root->kernfs_idr_lock); 660 ret = idr_alloc_cyclic(&root->ino_idr, kn, 1, 0, GFP_ATOMIC); 661 if (ret >= 0 && ret < root->last_id_lowbits) 662 root->id_highbits++; 663 id_highbits = root->id_highbits; 664 root->last_id_lowbits = ret; 665 spin_unlock(&root->kernfs_idr_lock); 666 idr_preload_end(); 667 if (ret < 0) 668 goto err_out2; 669 670 kn->id = (u64)id_highbits << 32 | ret; 671 672 atomic_set(&kn->count, 1); 673 atomic_set(&kn->active, KN_DEACTIVATED_BIAS); 674 RB_CLEAR_NODE(&kn->rb); 675 676 rcu_assign_pointer(kn->name, name); 677 kn->mode = mode; 678 kn->flags = flags; 679 680 if (!uid_eq(uid, GLOBAL_ROOT_UID) || !gid_eq(gid, GLOBAL_ROOT_GID)) { 681 struct iattr iattr = { 682 .ia_valid = ATTR_UID | ATTR_GID, 683 .ia_uid = uid, 684 .ia_gid = gid, 685 }; 686 687 ret = __kernfs_setattr(kn, &iattr); 688 if (ret < 0) 689 goto err_out3; 690 } 691 692 if (parent) { 693 ret = security_kernfs_init_security(parent, kn); 694 if (ret) 695 goto err_out4; 696 } 697 698 return kn; 699 700 err_out4: 701 if (kn->iattr) { 702 simple_xattrs_free(&kn->iattr->xattrs, NULL); 703 kmem_cache_free(kernfs_iattrs_cache, kn->iattr); 704 } 705 err_out3: 706 spin_lock(&root->kernfs_idr_lock); 707 idr_remove(&root->ino_idr, (u32)kernfs_ino(kn)); 708 spin_unlock(&root->kernfs_idr_lock); 709 err_out2: 710 kmem_cache_free(kernfs_node_cache, kn); 711 err_out1: 712 kfree_const(name); 713 return NULL; 714 } 715 716 struct kernfs_node *kernfs_new_node(struct kernfs_node *parent, 717 const char *name, umode_t mode, 718 kuid_t uid, kgid_t gid, 719 unsigned flags) 720 { 721 struct kernfs_node *kn; 722 723 if (parent->mode & S_ISGID) { 724 /* this code block imitates inode_init_owner() for 725 * kernfs 726 */ 727 728 if (parent->iattr) 729 gid = parent->iattr->ia_gid; 730 731 if (flags & KERNFS_DIR) 732 mode |= S_ISGID; 733 } 734 735 kn = __kernfs_new_node(kernfs_root(parent), parent, 736 name, mode, uid, gid, flags); 737 if (kn) { 738 kernfs_get(parent); 739 rcu_assign_pointer(kn->__parent, parent); 740 } 741 return kn; 742 } 743 744 /* 745 * kernfs_find_and_get_node_by_id - get kernfs_node from node id 746 * @root: the kernfs root 747 * @id: the target node id 748 * 749 * @id's lower 32bits encode ino and upper gen. If the gen portion is 750 * zero, all generations are matched. 751 * 752 * Return: %NULL on failure, 753 * otherwise a kernfs node with reference counter incremented. 754 */ 755 struct kernfs_node *kernfs_find_and_get_node_by_id(struct kernfs_root *root, 756 u64 id) 757 { 758 struct kernfs_node *kn; 759 ino_t ino = kernfs_id_ino(id); 760 u32 gen = kernfs_id_gen(id); 761 762 rcu_read_lock(); 763 764 kn = idr_find(&root->ino_idr, (u32)ino); 765 if (!kn) 766 goto err_unlock; 767 768 if (sizeof(ino_t) >= sizeof(u64)) { 769 /* we looked up with the low 32bits, compare the whole */ 770 if (kernfs_ino(kn) != ino) 771 goto err_unlock; 772 } else { 773 /* 0 matches all generations */ 774 if (unlikely(gen && kernfs_gen(kn) != gen)) 775 goto err_unlock; 776 } 777 778 /* 779 * We should fail if @kn has never been activated and guarantee success 780 * if the caller knows that @kn is active. Both can be achieved by 781 * __kernfs_active() which tests @kn->active without kernfs_rwsem. 782 */ 783 if (unlikely(!__kernfs_active(kn) || !atomic_inc_not_zero(&kn->count))) 784 goto err_unlock; 785 786 rcu_read_unlock(); 787 return kn; 788 err_unlock: 789 rcu_read_unlock(); 790 return NULL; 791 } 792 793 /** 794 * kernfs_add_one - add kernfs_node to parent without warning 795 * @kn: kernfs_node to be added 796 * 797 * The caller must already have initialized @kn->parent. This 798 * function increments nlink of the parent's inode if @kn is a 799 * directory and link into the children list of the parent. 800 * 801 * Return: 802 * %0 on success, -EEXIST if entry with the given name already 803 * exists. 804 */ 805 int kernfs_add_one(struct kernfs_node *kn) 806 { 807 struct kernfs_root *root = kernfs_root(kn); 808 struct kernfs_iattrs *ps_iattr; 809 struct kernfs_node *parent; 810 bool has_ns; 811 int ret; 812 813 down_write(&root->kernfs_rwsem); 814 parent = kernfs_parent(kn); 815 816 ret = -EINVAL; 817 has_ns = kernfs_ns_enabled(parent); 818 if (WARN(has_ns != (bool)kn->ns, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", 819 has_ns ? "required" : "invalid", 820 kernfs_rcu_name(parent), kernfs_rcu_name(kn))) 821 goto out_unlock; 822 823 if (kernfs_type(parent) != KERNFS_DIR) 824 goto out_unlock; 825 826 ret = -ENOENT; 827 if (parent->flags & (KERNFS_REMOVING | KERNFS_EMPTY_DIR)) 828 goto out_unlock; 829 830 kn->hash = kernfs_name_hash(kernfs_rcu_name(kn), kn->ns); 831 832 ret = kernfs_link_sibling(kn); 833 if (ret) 834 goto out_unlock; 835 836 /* Update timestamps on the parent */ 837 down_write(&root->kernfs_iattr_rwsem); 838 839 ps_iattr = parent->iattr; 840 if (ps_iattr) { 841 ktime_get_real_ts64(&ps_iattr->ia_ctime); 842 ps_iattr->ia_mtime = ps_iattr->ia_ctime; 843 } 844 845 up_write(&root->kernfs_iattr_rwsem); 846 up_write(&root->kernfs_rwsem); 847 848 /* 849 * Activate the new node unless CREATE_DEACTIVATED is requested. 850 * If not activated here, the kernfs user is responsible for 851 * activating the node with kernfs_activate(). A node which hasn't 852 * been activated is not visible to userland and its removal won't 853 * trigger deactivation. 854 */ 855 if (!(kernfs_root(kn)->flags & KERNFS_ROOT_CREATE_DEACTIVATED)) 856 kernfs_activate(kn); 857 return 0; 858 859 out_unlock: 860 up_write(&root->kernfs_rwsem); 861 return ret; 862 } 863 864 /** 865 * kernfs_find_ns - find kernfs_node with the given name 866 * @parent: kernfs_node to search under 867 * @name: name to look for 868 * @ns: the namespace tag to use 869 * 870 * Look for kernfs_node with name @name under @parent. 871 * 872 * Return: pointer to the found kernfs_node on success, %NULL on failure. 873 */ 874 static struct kernfs_node *kernfs_find_ns(struct kernfs_node *parent, 875 const unsigned char *name, 876 const struct ns_common *ns) 877 { 878 struct rb_node *node = parent->dir.children.rb_node; 879 bool has_ns = kernfs_ns_enabled(parent); 880 unsigned int hash; 881 882 lockdep_assert_held(&kernfs_root(parent)->kernfs_rwsem); 883 884 if (has_ns != (bool)ns) { 885 WARN(1, KERN_WARNING "kernfs: ns %s in '%s' for '%s'\n", 886 has_ns ? "required" : "invalid", kernfs_rcu_name(parent), name); 887 return NULL; 888 } 889 890 hash = kernfs_name_hash(name, ns); 891 while (node) { 892 struct kernfs_node *kn; 893 int result; 894 895 kn = rb_to_kn(node); 896 result = kernfs_name_compare(hash, name, ns, kn); 897 if (result < 0) 898 node = node->rb_left; 899 else if (result > 0) 900 node = node->rb_right; 901 else 902 return kn; 903 } 904 return NULL; 905 } 906 907 static struct kernfs_node *kernfs_walk_ns(struct kernfs_node *parent, 908 const unsigned char *path, 909 const struct ns_common *ns) 910 { 911 ssize_t len; 912 char *p, *name; 913 914 lockdep_assert_held_read(&kernfs_root(parent)->kernfs_rwsem); 915 916 spin_lock_irq(&kernfs_pr_cont_lock); 917 918 len = strscpy(kernfs_pr_cont_buf, path, sizeof(kernfs_pr_cont_buf)); 919 920 if (len < 0) { 921 spin_unlock_irq(&kernfs_pr_cont_lock); 922 return NULL; 923 } 924 925 p = kernfs_pr_cont_buf; 926 927 while ((name = strsep(&p, "/")) && parent) { 928 if (*name == '\0') 929 continue; 930 parent = kernfs_find_ns(parent, name, ns); 931 } 932 933 spin_unlock_irq(&kernfs_pr_cont_lock); 934 935 return parent; 936 } 937 938 /** 939 * kernfs_find_and_get_ns - find and get kernfs_node with the given name 940 * @parent: kernfs_node to search under 941 * @name: name to look for 942 * @ns: the namespace tag to use 943 * 944 * Look for kernfs_node with name @name under @parent and get a reference 945 * if found. This function may sleep. 946 * 947 * Return: pointer to the found kernfs_node on success, %NULL on failure. 948 */ 949 struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent, 950 const char *name, 951 const struct ns_common *ns) 952 { 953 struct kernfs_node *kn; 954 struct kernfs_root *root = kernfs_root(parent); 955 956 down_read(&root->kernfs_rwsem); 957 kn = kernfs_find_ns(parent, name, ns); 958 kernfs_get(kn); 959 up_read(&root->kernfs_rwsem); 960 961 return kn; 962 } 963 EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns); 964 965 /** 966 * kernfs_walk_and_get_ns - find and get kernfs_node with the given path 967 * @parent: kernfs_node to search under 968 * @path: path to look for 969 * @ns: the namespace tag to use 970 * 971 * Look for kernfs_node with path @path under @parent and get a reference 972 * if found. This function may sleep. 973 * 974 * Return: pointer to the found kernfs_node on success, %NULL on failure. 975 */ 976 struct kernfs_node *kernfs_walk_and_get_ns(struct kernfs_node *parent, 977 const char *path, 978 const struct ns_common *ns) 979 { 980 struct kernfs_node *kn; 981 struct kernfs_root *root = kernfs_root(parent); 982 983 down_read(&root->kernfs_rwsem); 984 kn = kernfs_walk_ns(parent, path, ns); 985 kernfs_get(kn); 986 up_read(&root->kernfs_rwsem); 987 988 return kn; 989 } 990 991 unsigned int kernfs_root_flags(struct kernfs_node *kn) 992 { 993 return kernfs_root(kn)->flags; 994 } 995 996 /** 997 * kernfs_create_root - create a new kernfs hierarchy 998 * @scops: optional syscall operations for the hierarchy 999 * @flags: KERNFS_ROOT_* flags 1000 * @priv: opaque data associated with the new directory 1001 * 1002 * Return: the root of the new hierarchy on success, ERR_PTR() value on 1003 * failure. 1004 */ 1005 struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops, 1006 unsigned int flags, void *priv) 1007 { 1008 struct kernfs_root *root; 1009 struct kernfs_node *kn; 1010 1011 root = kzalloc_obj(*root); 1012 if (!root) 1013 return ERR_PTR(-ENOMEM); 1014 1015 idr_init(&root->ino_idr); 1016 spin_lock_init(&root->kernfs_idr_lock); 1017 init_rwsem(&root->kernfs_rwsem); 1018 init_rwsem(&root->kernfs_iattr_rwsem); 1019 init_rwsem(&root->kernfs_supers_rwsem); 1020 INIT_LIST_HEAD(&root->supers); 1021 rwlock_init(&root->kernfs_rename_lock); 1022 1023 /* 1024 * On 64bit ino setups, id is ino. On 32bit, low 32bits are ino. 1025 * High bits generation. The starting value for both ino and 1026 * genenration is 1. Initialize upper 32bit allocation 1027 * accordingly. 1028 */ 1029 if (sizeof(ino_t) >= sizeof(u64)) 1030 root->id_highbits = 0; 1031 else 1032 root->id_highbits = 1; 1033 1034 kn = __kernfs_new_node(root, NULL, "", S_IFDIR | S_IRUGO | S_IXUGO, 1035 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, 1036 KERNFS_DIR); 1037 if (!kn) { 1038 idr_destroy(&root->ino_idr); 1039 kfree(root); 1040 return ERR_PTR(-ENOMEM); 1041 } 1042 1043 kn->priv = priv; 1044 kn->dir.root = root; 1045 1046 root->syscall_ops = scops; 1047 root->flags = flags; 1048 root->kn = kn; 1049 init_waitqueue_head(&root->deactivate_waitq); 1050 1051 if (!(root->flags & KERNFS_ROOT_CREATE_DEACTIVATED)) 1052 kernfs_activate(kn); 1053 1054 return root; 1055 } 1056 1057 /** 1058 * kernfs_destroy_root - destroy a kernfs hierarchy 1059 * @root: root of the hierarchy to destroy 1060 * 1061 * Destroy the hierarchy anchored at @root by removing all existing 1062 * directories and destroying @root. 1063 */ 1064 void kernfs_destroy_root(struct kernfs_root *root) 1065 { 1066 /* 1067 * kernfs_remove holds kernfs_rwsem from the root so the root 1068 * shouldn't be freed during the operation. 1069 */ 1070 kernfs_get(root->kn); 1071 kernfs_remove(root->kn); 1072 kernfs_put(root->kn); /* will also free @root */ 1073 } 1074 1075 /** 1076 * kernfs_root_to_node - return the kernfs_node associated with a kernfs_root 1077 * @root: root to use to lookup 1078 * 1079 * Return: @root's kernfs_node 1080 */ 1081 struct kernfs_node *kernfs_root_to_node(struct kernfs_root *root) 1082 { 1083 return root->kn; 1084 } 1085 1086 /** 1087 * kernfs_create_dir_ns - create a directory 1088 * @parent: parent in which to create a new directory 1089 * @name: name of the new directory 1090 * @mode: mode of the new directory 1091 * @uid: uid of the new directory 1092 * @gid: gid of the new directory 1093 * @priv: opaque data associated with the new directory 1094 * @ns: optional namespace tag of the directory 1095 * 1096 * Return: the created node on success, ERR_PTR() value on failure. 1097 */ 1098 struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent, 1099 const char *name, umode_t mode, 1100 kuid_t uid, kgid_t gid, 1101 void *priv, 1102 const struct ns_common *ns) 1103 { 1104 struct kernfs_node *kn; 1105 int rc; 1106 1107 /* allocate */ 1108 kn = kernfs_new_node(parent, name, mode | S_IFDIR, 1109 uid, gid, KERNFS_DIR); 1110 if (!kn) 1111 return ERR_PTR(-ENOMEM); 1112 1113 kn->dir.root = parent->dir.root; 1114 kn->ns = ns; 1115 kn->priv = priv; 1116 1117 /* link in */ 1118 rc = kernfs_add_one(kn); 1119 if (!rc) 1120 return kn; 1121 1122 kernfs_put(kn); 1123 return ERR_PTR(rc); 1124 } 1125 1126 /** 1127 * kernfs_create_empty_dir - create an always empty directory 1128 * @parent: parent in which to create a new directory 1129 * @name: name of the new directory 1130 * 1131 * Return: the created node on success, ERR_PTR() value on failure. 1132 */ 1133 struct kernfs_node *kernfs_create_empty_dir(struct kernfs_node *parent, 1134 const char *name) 1135 { 1136 struct kernfs_node *kn; 1137 int rc; 1138 1139 /* allocate */ 1140 kn = kernfs_new_node(parent, name, S_IRUGO|S_IXUGO|S_IFDIR, 1141 GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, KERNFS_DIR); 1142 if (!kn) 1143 return ERR_PTR(-ENOMEM); 1144 1145 kn->flags |= KERNFS_EMPTY_DIR; 1146 kn->dir.root = parent->dir.root; 1147 kn->ns = NULL; 1148 kn->priv = NULL; 1149 1150 /* link in */ 1151 rc = kernfs_add_one(kn); 1152 if (!rc) 1153 return kn; 1154 1155 kernfs_put(kn); 1156 return ERR_PTR(rc); 1157 } 1158 1159 static int kernfs_dop_revalidate(struct inode *dir, const struct qstr *name, 1160 struct dentry *dentry, unsigned int flags) 1161 { 1162 struct kernfs_node *kn, *parent; 1163 struct kernfs_root *root; 1164 1165 if (flags & LOOKUP_RCU) 1166 return -ECHILD; 1167 1168 /* Negative hashed dentry? */ 1169 if (d_really_is_negative(dentry)) { 1170 /* If the kernfs parent node has changed discard and 1171 * proceed to ->lookup. 1172 * 1173 * There's nothing special needed here when getting the 1174 * dentry parent, even if a concurrent rename is in 1175 * progress. That's because the dentry is negative so 1176 * it can only be the target of the rename and it will 1177 * be doing a d_move() not a replace. Consequently the 1178 * dentry d_parent won't change over the d_move(). 1179 * 1180 * Also kernfs negative dentries transitioning from 1181 * negative to positive during revalidate won't happen 1182 * because they are invalidated on containing directory 1183 * changes and the lookup re-done so that a new positive 1184 * dentry can be properly created. 1185 */ 1186 root = kernfs_root_from_sb(dentry->d_sb); 1187 down_read(&root->kernfs_rwsem); 1188 parent = kernfs_dentry_node(dentry->d_parent); 1189 if (parent) { 1190 if (kernfs_dir_changed(parent, dentry)) { 1191 up_read(&root->kernfs_rwsem); 1192 return 0; 1193 } 1194 } 1195 up_read(&root->kernfs_rwsem); 1196 1197 /* The kernfs parent node hasn't changed, leave the 1198 * dentry negative and return success. 1199 */ 1200 return 1; 1201 } 1202 1203 kn = kernfs_dentry_node(dentry); 1204 root = kernfs_root(kn); 1205 down_read(&root->kernfs_rwsem); 1206 1207 /* The kernfs node has been deactivated */ 1208 if (!kernfs_active(kn)) 1209 goto out_bad; 1210 1211 parent = kernfs_parent(kn); 1212 /* The kernfs node has been moved? */ 1213 if (kernfs_dentry_node(dentry->d_parent) != parent) 1214 goto out_bad; 1215 1216 /* The kernfs node has been renamed */ 1217 if (strcmp(dentry->d_name.name, kernfs_rcu_name(kn)) != 0) 1218 goto out_bad; 1219 1220 /* The kernfs node has been moved to a different namespace */ 1221 if (parent && kernfs_ns_enabled(parent) && 1222 kernfs_ns_id(kernfs_info(dentry->d_sb)->ns) != kernfs_ns_id(kn->ns)) 1223 goto out_bad; 1224 1225 up_read(&root->kernfs_rwsem); 1226 return 1; 1227 out_bad: 1228 up_read(&root->kernfs_rwsem); 1229 return 0; 1230 } 1231 1232 const struct dentry_operations kernfs_dops = { 1233 .d_revalidate = kernfs_dop_revalidate, 1234 }; 1235 1236 static struct dentry *kernfs_iop_lookup(struct inode *dir, 1237 struct dentry *dentry, 1238 unsigned int flags) 1239 { 1240 struct kernfs_node *parent = dir->i_private; 1241 struct kernfs_node *kn; 1242 struct kernfs_root *root; 1243 struct inode *inode = NULL; 1244 const struct ns_common *ns = NULL; 1245 1246 root = kernfs_root(parent); 1247 down_read(&root->kernfs_rwsem); 1248 if (kernfs_ns_enabled(parent)) 1249 ns = kernfs_info(dir->i_sb)->ns; 1250 1251 kn = kernfs_find_ns(parent, dentry->d_name.name, ns); 1252 /* attach dentry and inode */ 1253 if (kn) { 1254 /* Inactive nodes are invisible to the VFS so don't 1255 * create a negative. 1256 */ 1257 if (!kernfs_active(kn)) { 1258 up_read(&root->kernfs_rwsem); 1259 return NULL; 1260 } 1261 inode = kernfs_get_inode(dir->i_sb, kn); 1262 if (!inode) 1263 inode = ERR_PTR(-ENOMEM); 1264 } 1265 /* 1266 * Needed for negative dentry validation. 1267 * The negative dentry can be created in kernfs_iop_lookup() 1268 * or transforms from positive dentry in dentry_unlink_inode() 1269 * called from vfs_rmdir(). 1270 */ 1271 if (!IS_ERR(inode)) 1272 kernfs_set_rev(parent, dentry); 1273 up_read(&root->kernfs_rwsem); 1274 1275 /* instantiate and hash (possibly negative) dentry */ 1276 return d_splice_alias(inode, dentry); 1277 } 1278 1279 static struct dentry *kernfs_iop_mkdir(struct mnt_idmap *idmap, 1280 struct inode *dir, struct dentry *dentry, 1281 umode_t mode) 1282 { 1283 struct kernfs_node *parent = dir->i_private; 1284 struct kernfs_syscall_ops *scops = kernfs_root(parent)->syscall_ops; 1285 int ret; 1286 1287 if (!scops || !scops->mkdir) 1288 return ERR_PTR(-EPERM); 1289 1290 if (!kernfs_get_active(parent)) 1291 return ERR_PTR(-ENODEV); 1292 1293 ret = scops->mkdir(parent, dentry->d_name.name, mode); 1294 1295 kernfs_put_active(parent); 1296 return ERR_PTR(ret); 1297 } 1298 1299 static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry) 1300 { 1301 struct kernfs_node *kn = kernfs_dentry_node(dentry); 1302 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops; 1303 int ret; 1304 1305 if (!scops || !scops->rmdir) 1306 return -EPERM; 1307 1308 if (!kernfs_get_active(kn)) 1309 return -ENODEV; 1310 1311 ret = scops->rmdir(kn); 1312 1313 kernfs_put_active(kn); 1314 return ret; 1315 } 1316 1317 static int kernfs_iop_rename(struct mnt_idmap *idmap, 1318 struct inode *old_dir, struct dentry *old_dentry, 1319 struct inode *new_dir, struct dentry *new_dentry, 1320 unsigned int flags) 1321 { 1322 struct kernfs_node *kn = kernfs_dentry_node(old_dentry); 1323 struct kernfs_node *new_parent = new_dir->i_private; 1324 struct kernfs_syscall_ops *scops = kernfs_root(kn)->syscall_ops; 1325 int ret; 1326 1327 if (flags) 1328 return -EINVAL; 1329 1330 if (!scops || !scops->rename) 1331 return -EPERM; 1332 1333 if (!kernfs_get_active(kn)) 1334 return -ENODEV; 1335 1336 if (!kernfs_get_active(new_parent)) { 1337 kernfs_put_active(kn); 1338 return -ENODEV; 1339 } 1340 1341 ret = scops->rename(kn, new_parent, new_dentry->d_name.name); 1342 1343 kernfs_put_active(new_parent); 1344 kernfs_put_active(kn); 1345 return ret; 1346 } 1347 1348 const struct inode_operations kernfs_dir_iops = { 1349 .lookup = kernfs_iop_lookup, 1350 .permission = kernfs_iop_permission, 1351 .setattr = kernfs_iop_setattr, 1352 .getattr = kernfs_iop_getattr, 1353 .listxattr = kernfs_iop_listxattr, 1354 1355 .mkdir = kernfs_iop_mkdir, 1356 .rmdir = kernfs_iop_rmdir, 1357 .rename = kernfs_iop_rename, 1358 }; 1359 1360 static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos) 1361 { 1362 struct kernfs_node *last; 1363 1364 while (true) { 1365 struct rb_node *rbn; 1366 1367 last = pos; 1368 1369 if (kernfs_type(pos) != KERNFS_DIR) 1370 break; 1371 1372 rbn = rb_first(&pos->dir.children); 1373 if (!rbn) 1374 break; 1375 1376 pos = rb_to_kn(rbn); 1377 } 1378 1379 return last; 1380 } 1381 1382 /** 1383 * kernfs_next_descendant_post - find the next descendant for post-order walk 1384 * @pos: the current position (%NULL to initiate traversal) 1385 * @root: kernfs_node whose descendants to walk 1386 * 1387 * Find the next descendant to visit for post-order traversal of @root's 1388 * descendants. @root is included in the iteration and the last node to be 1389 * visited. 1390 * 1391 * Return: the next descendant to visit or %NULL when done. 1392 */ 1393 static struct kernfs_node *kernfs_next_descendant_post(struct kernfs_node *pos, 1394 struct kernfs_node *root) 1395 { 1396 struct rb_node *rbn; 1397 1398 lockdep_assert_held_write(&kernfs_root(root)->kernfs_rwsem); 1399 1400 /* if first iteration, visit leftmost descendant which may be root */ 1401 if (!pos) 1402 return kernfs_leftmost_descendant(root); 1403 1404 /* if we visited @root, we're done */ 1405 if (pos == root) 1406 return NULL; 1407 1408 /* if there's an unvisited sibling, visit its leftmost descendant */ 1409 rbn = rb_next(&pos->rb); 1410 if (rbn) 1411 return kernfs_leftmost_descendant(rb_to_kn(rbn)); 1412 1413 /* no sibling left, visit parent */ 1414 return kernfs_parent(pos); 1415 } 1416 1417 static void kernfs_activate_one(struct kernfs_node *kn) 1418 { 1419 lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem); 1420 1421 kn->flags |= KERNFS_ACTIVATED; 1422 1423 if (kernfs_active(kn) || (kn->flags & (KERNFS_HIDDEN | KERNFS_REMOVING))) 1424 return; 1425 1426 WARN_ON_ONCE(rcu_access_pointer(kn->__parent) && RB_EMPTY_NODE(&kn->rb)); 1427 WARN_ON_ONCE(atomic_read(&kn->active) != KN_DEACTIVATED_BIAS); 1428 1429 atomic_sub(KN_DEACTIVATED_BIAS, &kn->active); 1430 } 1431 1432 /** 1433 * kernfs_activate - activate a node which started deactivated 1434 * @kn: kernfs_node whose subtree is to be activated 1435 * 1436 * If the root has KERNFS_ROOT_CREATE_DEACTIVATED set, a newly created node 1437 * needs to be explicitly activated. A node which hasn't been activated 1438 * isn't visible to userland and deactivation is skipped during its 1439 * removal. This is useful to construct atomic init sequences where 1440 * creation of multiple nodes should either succeed or fail atomically. 1441 * 1442 * The caller is responsible for ensuring that this function is not called 1443 * after kernfs_remove*() is invoked on @kn. 1444 */ 1445 void kernfs_activate(struct kernfs_node *kn) 1446 { 1447 struct kernfs_node *pos; 1448 struct kernfs_root *root = kernfs_root(kn); 1449 1450 down_write(&root->kernfs_rwsem); 1451 1452 pos = NULL; 1453 while ((pos = kernfs_next_descendant_post(pos, kn))) 1454 kernfs_activate_one(pos); 1455 1456 up_write(&root->kernfs_rwsem); 1457 } 1458 1459 /** 1460 * kernfs_show - show or hide a node 1461 * @kn: kernfs_node to show or hide 1462 * @show: whether to show or hide 1463 * 1464 * If @show is %false, @kn is marked hidden and deactivated. A hidden node is 1465 * ignored in future activaitons. If %true, the mark is removed and activation 1466 * state is restored. This function won't implicitly activate a new node in a 1467 * %KERNFS_ROOT_CREATE_DEACTIVATED root which hasn't been activated yet. 1468 * 1469 * To avoid recursion complexities, directories aren't supported for now. 1470 */ 1471 void kernfs_show(struct kernfs_node *kn, bool show) 1472 { 1473 struct kernfs_root *root = kernfs_root(kn); 1474 1475 if (WARN_ON_ONCE(kernfs_type(kn) == KERNFS_DIR)) 1476 return; 1477 1478 down_write(&root->kernfs_rwsem); 1479 1480 if (show) { 1481 kn->flags &= ~KERNFS_HIDDEN; 1482 if (kn->flags & KERNFS_ACTIVATED) 1483 kernfs_activate_one(kn); 1484 } else { 1485 kn->flags |= KERNFS_HIDDEN; 1486 if (kernfs_active(kn)) 1487 atomic_add(KN_DEACTIVATED_BIAS, &kn->active); 1488 kernfs_drain(kn); 1489 } 1490 1491 up_write(&root->kernfs_rwsem); 1492 } 1493 1494 static void __kernfs_remove(struct kernfs_node *kn) 1495 { 1496 struct kernfs_node *pos, *parent; 1497 1498 /* Short-circuit if non-root @kn has already finished removal. */ 1499 if (!kn) 1500 return; 1501 1502 lockdep_assert_held_write(&kernfs_root(kn)->kernfs_rwsem); 1503 1504 /* 1505 * This is for kernfs_remove_self() which plays with active ref 1506 * after removal. 1507 */ 1508 if (kernfs_parent(kn) && RB_EMPTY_NODE(&kn->rb)) 1509 return; 1510 1511 pr_debug("kernfs %s: removing\n", kernfs_rcu_name(kn)); 1512 1513 /* prevent new usage by marking all nodes removing and deactivating */ 1514 pos = NULL; 1515 while ((pos = kernfs_next_descendant_post(pos, kn))) { 1516 pos->flags |= KERNFS_REMOVING; 1517 if (kernfs_active(pos)) 1518 atomic_add(KN_DEACTIVATED_BIAS, &pos->active); 1519 } 1520 1521 /* deactivate and unlink the subtree node-by-node */ 1522 do { 1523 pos = kernfs_leftmost_descendant(kn); 1524 1525 /* 1526 * kernfs_drain() may drop kernfs_rwsem temporarily and @pos's 1527 * base ref could have been put by someone else by the time 1528 * the function returns. Make sure it doesn't go away 1529 * underneath us. 1530 */ 1531 kernfs_get(pos); 1532 1533 kernfs_drain(pos); 1534 parent = kernfs_parent(pos); 1535 /* 1536 * kernfs_unlink_sibling() succeeds once per node. Use it 1537 * to decide who's responsible for cleanups. 1538 */ 1539 if (!parent || kernfs_unlink_sibling(pos)) { 1540 struct kernfs_iattrs *ps_iattr = 1541 parent ? parent->iattr : NULL; 1542 1543 /* update timestamps on the parent */ 1544 down_write(&kernfs_root(kn)->kernfs_iattr_rwsem); 1545 1546 if (ps_iattr) { 1547 ktime_get_real_ts64(&ps_iattr->ia_ctime); 1548 ps_iattr->ia_mtime = ps_iattr->ia_ctime; 1549 } 1550 1551 up_write(&kernfs_root(kn)->kernfs_iattr_rwsem); 1552 kernfs_put(pos); 1553 } 1554 1555 kernfs_put(pos); 1556 } while (pos != kn); 1557 } 1558 1559 /** 1560 * kernfs_remove - remove a kernfs_node recursively 1561 * @kn: the kernfs_node to remove 1562 * 1563 * Remove @kn along with all its subdirectories and files. 1564 */ 1565 void kernfs_remove(struct kernfs_node *kn) 1566 { 1567 struct kernfs_root *root; 1568 1569 if (!kn) 1570 return; 1571 1572 root = kernfs_root(kn); 1573 1574 down_write(&root->kernfs_rwsem); 1575 __kernfs_remove(kn); 1576 up_write(&root->kernfs_rwsem); 1577 } 1578 1579 /** 1580 * kernfs_break_active_protection - break out of active protection 1581 * @kn: the self kernfs_node 1582 * 1583 * The caller must be running off of a kernfs operation which is invoked 1584 * with an active reference - e.g. one of kernfs_ops. Each invocation of 1585 * this function must also be matched with an invocation of 1586 * kernfs_unbreak_active_protection(). 1587 * 1588 * This function releases the active reference of @kn the caller is 1589 * holding. Once this function is called, @kn may be removed at any point 1590 * and the caller is solely responsible for ensuring that the objects it 1591 * dereferences are accessible. 1592 */ 1593 void kernfs_break_active_protection(struct kernfs_node *kn) 1594 { 1595 /* 1596 * Take out ourself out of the active ref dependency chain. If 1597 * we're called without an active ref, lockdep will complain. 1598 */ 1599 kernfs_put_active(kn); 1600 } 1601 1602 /** 1603 * kernfs_unbreak_active_protection - undo kernfs_break_active_protection() 1604 * @kn: the self kernfs_node 1605 * 1606 * If kernfs_break_active_protection() was called, this function must be 1607 * invoked before finishing the kernfs operation. Note that while this 1608 * function restores the active reference, it doesn't and can't actually 1609 * restore the active protection - @kn may already or be in the process of 1610 * being drained and removed. Once kernfs_break_active_protection() is 1611 * invoked, that protection is irreversibly gone for the kernfs operation 1612 * instance. 1613 * 1614 * While this function may be called at any point after 1615 * kernfs_break_active_protection() is invoked, its most useful location 1616 * would be right before the enclosing kernfs operation returns. 1617 */ 1618 void kernfs_unbreak_active_protection(struct kernfs_node *kn) 1619 { 1620 /* 1621 * @kn->active could be in any state; however, the increment we do 1622 * here will be undone as soon as the enclosing kernfs operation 1623 * finishes and this temporary bump can't break anything. If @kn 1624 * is alive, nothing changes. If @kn is being deactivated, the 1625 * soon-to-follow put will either finish deactivation or restore 1626 * deactivated state. If @kn is already removed, the temporary 1627 * bump is guaranteed to be gone before @kn is released. 1628 */ 1629 atomic_inc(&kn->active); 1630 if (kernfs_lockdep(kn)) 1631 rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_); 1632 } 1633 1634 /** 1635 * kernfs_remove_self - remove a kernfs_node from its own method 1636 * @kn: the self kernfs_node to remove 1637 * 1638 * The caller must be running off of a kernfs operation which is invoked 1639 * with an active reference - e.g. one of kernfs_ops. This can be used to 1640 * implement a file operation which deletes itself. 1641 * 1642 * For example, the "delete" file for a sysfs device directory can be 1643 * implemented by invoking kernfs_remove_self() on the "delete" file 1644 * itself. This function breaks the circular dependency of trying to 1645 * deactivate self while holding an active ref itself. It isn't necessary 1646 * to modify the usual removal path to use kernfs_remove_self(). The 1647 * "delete" implementation can simply invoke kernfs_remove_self() on self 1648 * before proceeding with the usual removal path. kernfs will ignore later 1649 * kernfs_remove() on self. 1650 * 1651 * kernfs_remove_self() can be called multiple times concurrently on the 1652 * same kernfs_node. Only the first one actually performs removal and 1653 * returns %true. All others will wait until the kernfs operation which 1654 * won self-removal finishes and return %false. Note that the losers wait 1655 * for the completion of not only the winning kernfs_remove_self() but also 1656 * the whole kernfs_ops which won the arbitration. This can be used to 1657 * guarantee, for example, all concurrent writes to a "delete" file to 1658 * finish only after the whole operation is complete. 1659 * 1660 * Return: %true if @kn is removed by this call, otherwise %false. 1661 */ 1662 bool kernfs_remove_self(struct kernfs_node *kn) 1663 { 1664 bool ret; 1665 struct kernfs_root *root = kernfs_root(kn); 1666 1667 down_write(&root->kernfs_rwsem); 1668 kernfs_break_active_protection(kn); 1669 1670 /* 1671 * SUICIDAL is used to arbitrate among competing invocations. Only 1672 * the first one will actually perform removal. When the removal 1673 * is complete, SUICIDED is set and the active ref is restored 1674 * while kernfs_rwsem for held exclusive. The ones which lost 1675 * arbitration waits for SUICIDED && drained which can happen only 1676 * after the enclosing kernfs operation which executed the winning 1677 * instance of kernfs_remove_self() finished. 1678 */ 1679 if (!(kn->flags & KERNFS_SUICIDAL)) { 1680 kn->flags |= KERNFS_SUICIDAL; 1681 __kernfs_remove(kn); 1682 kn->flags |= KERNFS_SUICIDED; 1683 ret = true; 1684 } else { 1685 wait_queue_head_t *waitq = &kernfs_root(kn)->deactivate_waitq; 1686 DEFINE_WAIT(wait); 1687 1688 while (true) { 1689 prepare_to_wait(waitq, &wait, TASK_UNINTERRUPTIBLE); 1690 1691 if ((kn->flags & KERNFS_SUICIDED) && 1692 atomic_read(&kn->active) == KN_DEACTIVATED_BIAS) 1693 break; 1694 1695 up_write(&root->kernfs_rwsem); 1696 schedule(); 1697 down_write(&root->kernfs_rwsem); 1698 } 1699 finish_wait(waitq, &wait); 1700 WARN_ON_ONCE(!RB_EMPTY_NODE(&kn->rb)); 1701 ret = false; 1702 } 1703 1704 /* 1705 * This must be done while kernfs_rwsem held exclusive; otherwise, 1706 * waiting for SUICIDED && deactivated could finish prematurely. 1707 */ 1708 kernfs_unbreak_active_protection(kn); 1709 1710 up_write(&root->kernfs_rwsem); 1711 return ret; 1712 } 1713 1714 /** 1715 * kernfs_remove_by_name_ns - find a kernfs_node by name and remove it 1716 * @parent: parent of the target 1717 * @name: name of the kernfs_node to remove 1718 * @ns: namespace tag of the kernfs_node to remove 1719 * 1720 * Look for the kernfs_node with @name and @ns under @parent and remove it. 1721 * 1722 * Return: %0 on success, -ENOENT if such entry doesn't exist. 1723 */ 1724 int kernfs_remove_by_name_ns(struct kernfs_node *parent, const char *name, 1725 const struct ns_common *ns) 1726 { 1727 struct kernfs_node *kn; 1728 struct kernfs_root *root; 1729 1730 if (!parent) { 1731 WARN(1, KERN_WARNING "kernfs: can not remove '%s', no directory\n", 1732 name); 1733 return -ENOENT; 1734 } 1735 1736 root = kernfs_root(parent); 1737 down_write(&root->kernfs_rwsem); 1738 1739 kn = kernfs_find_ns(parent, name, ns); 1740 if (kn) { 1741 kernfs_get(kn); 1742 __kernfs_remove(kn); 1743 kernfs_put(kn); 1744 } 1745 1746 up_write(&root->kernfs_rwsem); 1747 1748 if (kn) 1749 return 0; 1750 else 1751 return -ENOENT; 1752 } 1753 1754 /** 1755 * kernfs_rename_ns - move and rename a kernfs_node 1756 * @kn: target node 1757 * @new_parent: new parent to put @sd under 1758 * @new_name: new name 1759 * @new_ns: new namespace tag 1760 * 1761 * Return: %0 on success, -errno on failure. 1762 */ 1763 int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, 1764 const char *new_name, const struct ns_common *new_ns) 1765 { 1766 struct kernfs_node *old_parent; 1767 struct kernfs_root *root; 1768 const char *old_name; 1769 int error; 1770 1771 /* can't move or rename root */ 1772 if (!rcu_access_pointer(kn->__parent)) 1773 return -EINVAL; 1774 1775 root = kernfs_root(kn); 1776 down_write(&root->kernfs_rwsem); 1777 1778 error = -ENOENT; 1779 if (!kernfs_active(kn) || !kernfs_active(new_parent) || 1780 (new_parent->flags & KERNFS_EMPTY_DIR)) 1781 goto out; 1782 1783 old_parent = kernfs_parent(kn); 1784 if (root->flags & KERNFS_ROOT_INVARIANT_PARENT) { 1785 error = -EINVAL; 1786 if (WARN_ON_ONCE(old_parent != new_parent)) 1787 goto out; 1788 } 1789 1790 error = 0; 1791 old_name = kernfs_rcu_name(kn); 1792 if (!new_name) 1793 new_name = old_name; 1794 if ((old_parent == new_parent) && 1795 (kernfs_ns_id(kn->ns) == kernfs_ns_id(new_ns)) && 1796 (strcmp(old_name, new_name) == 0)) 1797 goto out; /* nothing to rename */ 1798 1799 error = -EEXIST; 1800 if (kernfs_find_ns(new_parent, new_name, new_ns)) 1801 goto out; 1802 1803 /* rename kernfs_node */ 1804 if (strcmp(old_name, new_name) != 0) { 1805 error = -ENOMEM; 1806 new_name = kstrdup_const(new_name, GFP_KERNEL); 1807 if (!new_name) 1808 goto out; 1809 } else { 1810 new_name = NULL; 1811 } 1812 1813 /* 1814 * Move to the appropriate place in the appropriate directories rbtree. 1815 */ 1816 kernfs_unlink_sibling(kn); 1817 1818 /* rename_lock protects ->parent accessors */ 1819 if (old_parent != new_parent) { 1820 kernfs_get(new_parent); 1821 write_lock_irq(&root->kernfs_rename_lock); 1822 1823 rcu_assign_pointer(kn->__parent, new_parent); 1824 1825 kn->ns = new_ns; 1826 if (new_name) 1827 rcu_assign_pointer(kn->name, new_name); 1828 1829 write_unlock_irq(&root->kernfs_rename_lock); 1830 kernfs_put(old_parent); 1831 } else { 1832 /* name assignment is RCU protected, parent is the same */ 1833 kn->ns = new_ns; 1834 if (new_name) 1835 rcu_assign_pointer(kn->name, new_name); 1836 } 1837 1838 kn->hash = kernfs_name_hash(new_name ?: old_name, kn->ns); 1839 kernfs_link_sibling(kn); 1840 1841 if (new_name && !is_kernel_rodata((unsigned long)old_name)) 1842 kfree_rcu_mightsleep(old_name); 1843 1844 error = 0; 1845 out: 1846 up_write(&root->kernfs_rwsem); 1847 return error; 1848 } 1849 1850 static int kernfs_dir_fop_release(struct inode *inode, struct file *filp) 1851 { 1852 kernfs_put(filp->private_data); 1853 return 0; 1854 } 1855 1856 static struct kernfs_node *kernfs_dir_pos(const struct ns_common *ns, 1857 struct kernfs_node *parent, loff_t hash, struct kernfs_node *pos) 1858 { 1859 if (pos) { 1860 int valid = kernfs_active(pos) && 1861 rcu_access_pointer(pos->__parent) == parent && 1862 hash == pos->hash; 1863 kernfs_put(pos); 1864 if (!valid) 1865 pos = NULL; 1866 } 1867 if (!pos && (hash > 1) && (hash < INT_MAX)) { 1868 struct rb_node *node = parent->dir.children.rb_node; 1869 u64 ns_id = kernfs_ns_id(ns); 1870 while (node) { 1871 pos = rb_to_kn(node); 1872 1873 if (hash < pos->hash) 1874 node = node->rb_left; 1875 else if (hash > pos->hash) 1876 node = node->rb_right; 1877 else if (ns_id < kernfs_ns_id(pos->ns)) 1878 node = node->rb_left; 1879 else if (ns_id > kernfs_ns_id(pos->ns)) 1880 node = node->rb_right; 1881 else 1882 break; 1883 } 1884 } 1885 /* Skip over entries which are dying/dead or in the wrong namespace */ 1886 while (pos && (!kernfs_active(pos) || 1887 kernfs_ns_id(pos->ns) != kernfs_ns_id(ns))) { 1888 struct rb_node *node = rb_next(&pos->rb); 1889 if (!node) 1890 pos = NULL; 1891 else 1892 pos = rb_to_kn(node); 1893 } 1894 return pos; 1895 } 1896 1897 static struct kernfs_node *kernfs_dir_next_pos(const struct ns_common *ns, 1898 struct kernfs_node *parent, ino_t ino, struct kernfs_node *pos) 1899 { 1900 pos = kernfs_dir_pos(ns, parent, ino, pos); 1901 if (pos) { 1902 do { 1903 struct rb_node *node = rb_next(&pos->rb); 1904 if (!node) 1905 pos = NULL; 1906 else 1907 pos = rb_to_kn(node); 1908 } while (pos && (!kernfs_active(pos) || 1909 kernfs_ns_id(pos->ns) != kernfs_ns_id(ns))); 1910 } 1911 return pos; 1912 } 1913 1914 static int kernfs_fop_readdir(struct file *file, struct dir_context *ctx) 1915 { 1916 struct dentry *dentry = file->f_path.dentry; 1917 struct kernfs_node *parent = kernfs_dentry_node(dentry); 1918 struct kernfs_node *pos = file->private_data; 1919 struct kernfs_root *root; 1920 const struct ns_common *ns = NULL; 1921 1922 if (!dir_emit_dots(file, ctx)) 1923 return 0; 1924 1925 root = kernfs_root(parent); 1926 down_read(&root->kernfs_rwsem); 1927 1928 if (kernfs_ns_enabled(parent)) 1929 ns = kernfs_info(dentry->d_sb)->ns; 1930 1931 for (pos = kernfs_dir_pos(ns, parent, ctx->pos, pos); 1932 pos; 1933 pos = kernfs_dir_next_pos(ns, parent, ctx->pos, pos)) { 1934 const char *name = kernfs_rcu_name(pos); 1935 unsigned int type = fs_umode_to_dtype(pos->mode); 1936 int len = strlen(name); 1937 ino_t ino = kernfs_ino(pos); 1938 1939 ctx->pos = pos->hash; 1940 file->private_data = pos; 1941 kernfs_get(pos); 1942 1943 if (!dir_emit(ctx, name, len, ino, type)) { 1944 up_read(&root->kernfs_rwsem); 1945 return 0; 1946 } 1947 } 1948 up_read(&root->kernfs_rwsem); 1949 file->private_data = NULL; 1950 ctx->pos = INT_MAX; 1951 return 0; 1952 } 1953 1954 const struct file_operations kernfs_dir_fops = { 1955 .read = generic_read_dir, 1956 .iterate_shared = kernfs_fop_readdir, 1957 .release = kernfs_dir_fop_release, 1958 .llseek = generic_file_llseek, 1959 }; 1960