1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * kobject.c - library routines for handling generic kernel objects 4 * 5 * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org> 6 * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com> 7 * Copyright (c) 2006-2007 Novell Inc. 8 * 9 * This file is released under the GPLv2. 10 * 11 * 12 * Please see the file Documentation/kobject.txt for critical information 13 * about using the kobject interface. 14 */ 15 16 #include <linux/kobject.h> 17 #include <linux/string.h> 18 #include <linux/export.h> 19 #include <linux/stat.h> 20 #include <linux/slab.h> 21 #include <linux/random.h> 22 23 /** 24 * kobject_namespace - return @kobj's namespace tag 25 * @kobj: kobject in question 26 * 27 * Returns namespace tag of @kobj if its parent has namespace ops enabled 28 * and thus @kobj should have a namespace tag associated with it. Returns 29 * %NULL otherwise. 30 */ 31 const void *kobject_namespace(struct kobject *kobj) 32 { 33 const struct kobj_ns_type_operations *ns_ops = kobj_ns_ops(kobj); 34 35 if (!ns_ops || ns_ops->type == KOBJ_NS_TYPE_NONE) 36 return NULL; 37 38 return kobj->ktype->namespace(kobj); 39 } 40 41 /* 42 * populate_dir - populate directory with attributes. 43 * @kobj: object we're working on. 44 * 45 * Most subsystems have a set of default attributes that are associated 46 * with an object that registers with them. This is a helper called during 47 * object registration that loops through the default attributes of the 48 * subsystem and creates attributes files for them in sysfs. 49 */ 50 static int populate_dir(struct kobject *kobj) 51 { 52 struct kobj_type *t = get_ktype(kobj); 53 struct attribute *attr; 54 int error = 0; 55 int i; 56 57 if (t && t->default_attrs) { 58 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) { 59 error = sysfs_create_file(kobj, attr); 60 if (error) 61 break; 62 } 63 } 64 return error; 65 } 66 67 static int create_dir(struct kobject *kobj) 68 { 69 const struct kobj_ns_type_operations *ops; 70 int error; 71 72 error = sysfs_create_dir_ns(kobj, kobject_namespace(kobj)); 73 if (error) 74 return error; 75 76 error = populate_dir(kobj); 77 if (error) { 78 sysfs_remove_dir(kobj); 79 return error; 80 } 81 82 /* 83 * @kobj->sd may be deleted by an ancestor going away. Hold an 84 * extra reference so that it stays until @kobj is gone. 85 */ 86 sysfs_get(kobj->sd); 87 88 /* 89 * If @kobj has ns_ops, its children need to be filtered based on 90 * their namespace tags. Enable namespace support on @kobj->sd. 91 */ 92 ops = kobj_child_ns_ops(kobj); 93 if (ops) { 94 BUG_ON(ops->type <= KOBJ_NS_TYPE_NONE); 95 BUG_ON(ops->type >= KOBJ_NS_TYPES); 96 BUG_ON(!kobj_ns_type_registered(ops->type)); 97 98 sysfs_enable_ns(kobj->sd); 99 } 100 101 return 0; 102 } 103 104 static int get_kobj_path_length(struct kobject *kobj) 105 { 106 int length = 1; 107 struct kobject *parent = kobj; 108 109 /* walk up the ancestors until we hit the one pointing to the 110 * root. 111 * Add 1 to strlen for leading '/' of each level. 112 */ 113 do { 114 if (kobject_name(parent) == NULL) 115 return 0; 116 length += strlen(kobject_name(parent)) + 1; 117 parent = parent->parent; 118 } while (parent); 119 return length; 120 } 121 122 static void fill_kobj_path(struct kobject *kobj, char *path, int length) 123 { 124 struct kobject *parent; 125 126 --length; 127 for (parent = kobj; parent; parent = parent->parent) { 128 int cur = strlen(kobject_name(parent)); 129 /* back up enough to print this name with '/' */ 130 length -= cur; 131 strncpy(path + length, kobject_name(parent), cur); 132 *(path + --length) = '/'; 133 } 134 135 pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj), 136 kobj, __func__, path); 137 } 138 139 /** 140 * kobject_get_path - generate and return the path associated with a given kobj and kset pair. 141 * 142 * @kobj: kobject in question, with which to build the path 143 * @gfp_mask: the allocation type used to allocate the path 144 * 145 * The result must be freed by the caller with kfree(). 146 */ 147 char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask) 148 { 149 char *path; 150 int len; 151 152 len = get_kobj_path_length(kobj); 153 if (len == 0) 154 return NULL; 155 path = kzalloc(len, gfp_mask); 156 if (!path) 157 return NULL; 158 fill_kobj_path(kobj, path, len); 159 160 return path; 161 } 162 EXPORT_SYMBOL_GPL(kobject_get_path); 163 164 /* add the kobject to its kset's list */ 165 static void kobj_kset_join(struct kobject *kobj) 166 { 167 if (!kobj->kset) 168 return; 169 170 kset_get(kobj->kset); 171 spin_lock(&kobj->kset->list_lock); 172 list_add_tail(&kobj->entry, &kobj->kset->list); 173 spin_unlock(&kobj->kset->list_lock); 174 } 175 176 /* remove the kobject from its kset's list */ 177 static void kobj_kset_leave(struct kobject *kobj) 178 { 179 if (!kobj->kset) 180 return; 181 182 spin_lock(&kobj->kset->list_lock); 183 list_del_init(&kobj->entry); 184 spin_unlock(&kobj->kset->list_lock); 185 kset_put(kobj->kset); 186 } 187 188 static void kobject_init_internal(struct kobject *kobj) 189 { 190 if (!kobj) 191 return; 192 kref_init(&kobj->kref); 193 INIT_LIST_HEAD(&kobj->entry); 194 kobj->state_in_sysfs = 0; 195 kobj->state_add_uevent_sent = 0; 196 kobj->state_remove_uevent_sent = 0; 197 kobj->state_initialized = 1; 198 } 199 200 201 static int kobject_add_internal(struct kobject *kobj) 202 { 203 int error = 0; 204 struct kobject *parent; 205 206 if (!kobj) 207 return -ENOENT; 208 209 if (!kobj->name || !kobj->name[0]) { 210 WARN(1, "kobject: (%p): attempted to be registered with empty " 211 "name!\n", kobj); 212 return -EINVAL; 213 } 214 215 parent = kobject_get(kobj->parent); 216 217 /* join kset if set, use it as parent if we do not already have one */ 218 if (kobj->kset) { 219 if (!parent) 220 parent = kobject_get(&kobj->kset->kobj); 221 kobj_kset_join(kobj); 222 kobj->parent = parent; 223 } 224 225 pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n", 226 kobject_name(kobj), kobj, __func__, 227 parent ? kobject_name(parent) : "<NULL>", 228 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>"); 229 230 error = create_dir(kobj); 231 if (error) { 232 kobj_kset_leave(kobj); 233 kobject_put(parent); 234 kobj->parent = NULL; 235 236 /* be noisy on error issues */ 237 if (error == -EEXIST) 238 WARN(1, "%s failed for %s with " 239 "-EEXIST, don't try to register things with " 240 "the same name in the same directory.\n", 241 __func__, kobject_name(kobj)); 242 else 243 WARN(1, "%s failed for %s (error: %d parent: %s)\n", 244 __func__, kobject_name(kobj), error, 245 parent ? kobject_name(parent) : "'none'"); 246 } else 247 kobj->state_in_sysfs = 1; 248 249 return error; 250 } 251 252 /** 253 * kobject_set_name_vargs - Set the name of an kobject 254 * @kobj: struct kobject to set the name of 255 * @fmt: format string used to build the name 256 * @vargs: vargs to format the string. 257 */ 258 int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, 259 va_list vargs) 260 { 261 const char *s; 262 263 if (kobj->name && !fmt) 264 return 0; 265 266 s = kvasprintf_const(GFP_KERNEL, fmt, vargs); 267 if (!s) 268 return -ENOMEM; 269 270 /* 271 * ewww... some of these buggers have '/' in the name ... If 272 * that's the case, we need to make sure we have an actual 273 * allocated copy to modify, since kvasprintf_const may have 274 * returned something from .rodata. 275 */ 276 if (strchr(s, '/')) { 277 char *t; 278 279 t = kstrdup(s, GFP_KERNEL); 280 kfree_const(s); 281 if (!t) 282 return -ENOMEM; 283 strreplace(t, '/', '!'); 284 s = t; 285 } 286 kfree_const(kobj->name); 287 kobj->name = s; 288 289 return 0; 290 } 291 292 /** 293 * kobject_set_name - Set the name of a kobject 294 * @kobj: struct kobject to set the name of 295 * @fmt: format string used to build the name 296 * 297 * This sets the name of the kobject. If you have already added the 298 * kobject to the system, you must call kobject_rename() in order to 299 * change the name of the kobject. 300 */ 301 int kobject_set_name(struct kobject *kobj, const char *fmt, ...) 302 { 303 va_list vargs; 304 int retval; 305 306 va_start(vargs, fmt); 307 retval = kobject_set_name_vargs(kobj, fmt, vargs); 308 va_end(vargs); 309 310 return retval; 311 } 312 EXPORT_SYMBOL(kobject_set_name); 313 314 /** 315 * kobject_init - initialize a kobject structure 316 * @kobj: pointer to the kobject to initialize 317 * @ktype: pointer to the ktype for this kobject. 318 * 319 * This function will properly initialize a kobject such that it can then 320 * be passed to the kobject_add() call. 321 * 322 * After this function is called, the kobject MUST be cleaned up by a call 323 * to kobject_put(), not by a call to kfree directly to ensure that all of 324 * the memory is cleaned up properly. 325 */ 326 void kobject_init(struct kobject *kobj, struct kobj_type *ktype) 327 { 328 char *err_str; 329 330 if (!kobj) { 331 err_str = "invalid kobject pointer!"; 332 goto error; 333 } 334 if (!ktype) { 335 err_str = "must have a ktype to be initialized properly!\n"; 336 goto error; 337 } 338 if (kobj->state_initialized) { 339 /* do not error out as sometimes we can recover */ 340 printk(KERN_ERR "kobject (%p): tried to init an initialized " 341 "object, something is seriously wrong.\n", kobj); 342 dump_stack(); 343 } 344 345 kobject_init_internal(kobj); 346 kobj->ktype = ktype; 347 return; 348 349 error: 350 printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str); 351 dump_stack(); 352 } 353 EXPORT_SYMBOL(kobject_init); 354 355 static __printf(3, 0) int kobject_add_varg(struct kobject *kobj, 356 struct kobject *parent, 357 const char *fmt, va_list vargs) 358 { 359 int retval; 360 361 retval = kobject_set_name_vargs(kobj, fmt, vargs); 362 if (retval) { 363 printk(KERN_ERR "kobject: can not set name properly!\n"); 364 return retval; 365 } 366 kobj->parent = parent; 367 return kobject_add_internal(kobj); 368 } 369 370 /** 371 * kobject_add - the main kobject add function 372 * @kobj: the kobject to add 373 * @parent: pointer to the parent of the kobject. 374 * @fmt: format to name the kobject with. 375 * 376 * The kobject name is set and added to the kobject hierarchy in this 377 * function. 378 * 379 * If @parent is set, then the parent of the @kobj will be set to it. 380 * If @parent is NULL, then the parent of the @kobj will be set to the 381 * kobject associated with the kset assigned to this kobject. If no kset 382 * is assigned to the kobject, then the kobject will be located in the 383 * root of the sysfs tree. 384 * 385 * If this function returns an error, kobject_put() must be called to 386 * properly clean up the memory associated with the object. 387 * Under no instance should the kobject that is passed to this function 388 * be directly freed with a call to kfree(), that can leak memory. 389 * 390 * Note, no "add" uevent will be created with this call, the caller should set 391 * up all of the necessary sysfs files for the object and then call 392 * kobject_uevent() with the UEVENT_ADD parameter to ensure that 393 * userspace is properly notified of this kobject's creation. 394 */ 395 int kobject_add(struct kobject *kobj, struct kobject *parent, 396 const char *fmt, ...) 397 { 398 va_list args; 399 int retval; 400 401 if (!kobj) 402 return -EINVAL; 403 404 if (!kobj->state_initialized) { 405 printk(KERN_ERR "kobject '%s' (%p): tried to add an " 406 "uninitialized object, something is seriously wrong.\n", 407 kobject_name(kobj), kobj); 408 dump_stack(); 409 return -EINVAL; 410 } 411 va_start(args, fmt); 412 retval = kobject_add_varg(kobj, parent, fmt, args); 413 va_end(args); 414 415 return retval; 416 } 417 EXPORT_SYMBOL(kobject_add); 418 419 /** 420 * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy 421 * @kobj: pointer to the kobject to initialize 422 * @ktype: pointer to the ktype for this kobject. 423 * @parent: pointer to the parent of this kobject. 424 * @fmt: the name of the kobject. 425 * 426 * This function combines the call to kobject_init() and 427 * kobject_add(). The same type of error handling after a call to 428 * kobject_add() and kobject lifetime rules are the same here. 429 */ 430 int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, 431 struct kobject *parent, const char *fmt, ...) 432 { 433 va_list args; 434 int retval; 435 436 kobject_init(kobj, ktype); 437 438 va_start(args, fmt); 439 retval = kobject_add_varg(kobj, parent, fmt, args); 440 va_end(args); 441 442 return retval; 443 } 444 EXPORT_SYMBOL_GPL(kobject_init_and_add); 445 446 /** 447 * kobject_rename - change the name of an object 448 * @kobj: object in question. 449 * @new_name: object's new name 450 * 451 * It is the responsibility of the caller to provide mutual 452 * exclusion between two different calls of kobject_rename 453 * on the same kobject and to ensure that new_name is valid and 454 * won't conflict with other kobjects. 455 */ 456 int kobject_rename(struct kobject *kobj, const char *new_name) 457 { 458 int error = 0; 459 const char *devpath = NULL; 460 const char *dup_name = NULL, *name; 461 char *devpath_string = NULL; 462 char *envp[2]; 463 464 kobj = kobject_get(kobj); 465 if (!kobj) 466 return -EINVAL; 467 if (!kobj->parent) 468 return -EINVAL; 469 470 devpath = kobject_get_path(kobj, GFP_KERNEL); 471 if (!devpath) { 472 error = -ENOMEM; 473 goto out; 474 } 475 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL); 476 if (!devpath_string) { 477 error = -ENOMEM; 478 goto out; 479 } 480 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath); 481 envp[0] = devpath_string; 482 envp[1] = NULL; 483 484 name = dup_name = kstrdup_const(new_name, GFP_KERNEL); 485 if (!name) { 486 error = -ENOMEM; 487 goto out; 488 } 489 490 error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj)); 491 if (error) 492 goto out; 493 494 /* Install the new kobject name */ 495 dup_name = kobj->name; 496 kobj->name = name; 497 498 /* This function is mostly/only used for network interface. 499 * Some hotplug package track interfaces by their name and 500 * therefore want to know when the name is changed by the user. */ 501 kobject_uevent_env(kobj, KOBJ_MOVE, envp); 502 503 out: 504 kfree_const(dup_name); 505 kfree(devpath_string); 506 kfree(devpath); 507 kobject_put(kobj); 508 509 return error; 510 } 511 EXPORT_SYMBOL_GPL(kobject_rename); 512 513 /** 514 * kobject_move - move object to another parent 515 * @kobj: object in question. 516 * @new_parent: object's new parent (can be NULL) 517 */ 518 int kobject_move(struct kobject *kobj, struct kobject *new_parent) 519 { 520 int error; 521 struct kobject *old_parent; 522 const char *devpath = NULL; 523 char *devpath_string = NULL; 524 char *envp[2]; 525 526 kobj = kobject_get(kobj); 527 if (!kobj) 528 return -EINVAL; 529 new_parent = kobject_get(new_parent); 530 if (!new_parent) { 531 if (kobj->kset) 532 new_parent = kobject_get(&kobj->kset->kobj); 533 } 534 535 /* old object path */ 536 devpath = kobject_get_path(kobj, GFP_KERNEL); 537 if (!devpath) { 538 error = -ENOMEM; 539 goto out; 540 } 541 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL); 542 if (!devpath_string) { 543 error = -ENOMEM; 544 goto out; 545 } 546 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath); 547 envp[0] = devpath_string; 548 envp[1] = NULL; 549 error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj)); 550 if (error) 551 goto out; 552 old_parent = kobj->parent; 553 kobj->parent = new_parent; 554 new_parent = NULL; 555 kobject_put(old_parent); 556 kobject_uevent_env(kobj, KOBJ_MOVE, envp); 557 out: 558 kobject_put(new_parent); 559 kobject_put(kobj); 560 kfree(devpath_string); 561 kfree(devpath); 562 return error; 563 } 564 EXPORT_SYMBOL_GPL(kobject_move); 565 566 /** 567 * kobject_del - unlink kobject from hierarchy. 568 * @kobj: object. 569 */ 570 void kobject_del(struct kobject *kobj) 571 { 572 struct kernfs_node *sd; 573 574 if (!kobj) 575 return; 576 577 sd = kobj->sd; 578 sysfs_remove_dir(kobj); 579 sysfs_put(sd); 580 581 kobj->state_in_sysfs = 0; 582 kobj_kset_leave(kobj); 583 kobject_put(kobj->parent); 584 kobj->parent = NULL; 585 } 586 EXPORT_SYMBOL(kobject_del); 587 588 /** 589 * kobject_get - increment refcount for object. 590 * @kobj: object. 591 */ 592 struct kobject *kobject_get(struct kobject *kobj) 593 { 594 if (kobj) { 595 if (!kobj->state_initialized) 596 WARN(1, KERN_WARNING "kobject: '%s' (%p): is not " 597 "initialized, yet kobject_get() is being " 598 "called.\n", kobject_name(kobj), kobj); 599 kref_get(&kobj->kref); 600 } 601 return kobj; 602 } 603 EXPORT_SYMBOL(kobject_get); 604 605 struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj) 606 { 607 if (!kobj) 608 return NULL; 609 if (!kref_get_unless_zero(&kobj->kref)) 610 kobj = NULL; 611 return kobj; 612 } 613 EXPORT_SYMBOL(kobject_get_unless_zero); 614 615 /* 616 * kobject_cleanup - free kobject resources. 617 * @kobj: object to cleanup 618 */ 619 static void kobject_cleanup(struct kobject *kobj) 620 { 621 struct kobj_type *t = get_ktype(kobj); 622 const char *name = kobj->name; 623 624 pr_debug("kobject: '%s' (%p): %s, parent %p\n", 625 kobject_name(kobj), kobj, __func__, kobj->parent); 626 627 if (t && !t->release) 628 pr_debug("kobject: '%s' (%p): does not have a release() " 629 "function, it is broken and must be fixed.\n", 630 kobject_name(kobj), kobj); 631 632 /* send "remove" if the caller did not do it but sent "add" */ 633 if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) { 634 pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n", 635 kobject_name(kobj), kobj); 636 kobject_uevent(kobj, KOBJ_REMOVE); 637 } 638 639 /* remove from sysfs if the caller did not do it */ 640 if (kobj->state_in_sysfs) { 641 pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n", 642 kobject_name(kobj), kobj); 643 kobject_del(kobj); 644 } 645 646 if (t && t->release) { 647 pr_debug("kobject: '%s' (%p): calling ktype release\n", 648 kobject_name(kobj), kobj); 649 t->release(kobj); 650 } 651 652 /* free name if we allocated it */ 653 if (name) { 654 pr_debug("kobject: '%s': free name\n", name); 655 kfree_const(name); 656 } 657 } 658 659 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE 660 static void kobject_delayed_cleanup(struct work_struct *work) 661 { 662 kobject_cleanup(container_of(to_delayed_work(work), 663 struct kobject, release)); 664 } 665 #endif 666 667 static void kobject_release(struct kref *kref) 668 { 669 struct kobject *kobj = container_of(kref, struct kobject, kref); 670 #ifdef CONFIG_DEBUG_KOBJECT_RELEASE 671 unsigned long delay = HZ + HZ * (get_random_int() & 0x3); 672 pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n", 673 kobject_name(kobj), kobj, __func__, kobj->parent, delay); 674 INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup); 675 676 schedule_delayed_work(&kobj->release, delay); 677 #else 678 kobject_cleanup(kobj); 679 #endif 680 } 681 682 /** 683 * kobject_put - decrement refcount for object. 684 * @kobj: object. 685 * 686 * Decrement the refcount, and if 0, call kobject_cleanup(). 687 */ 688 void kobject_put(struct kobject *kobj) 689 { 690 if (kobj) { 691 if (!kobj->state_initialized) 692 WARN(1, KERN_WARNING "kobject: '%s' (%p): is not " 693 "initialized, yet kobject_put() is being " 694 "called.\n", kobject_name(kobj), kobj); 695 kref_put(&kobj->kref, kobject_release); 696 } 697 } 698 EXPORT_SYMBOL(kobject_put); 699 700 static void dynamic_kobj_release(struct kobject *kobj) 701 { 702 pr_debug("kobject: (%p): %s\n", kobj, __func__); 703 kfree(kobj); 704 } 705 706 static struct kobj_type dynamic_kobj_ktype = { 707 .release = dynamic_kobj_release, 708 .sysfs_ops = &kobj_sysfs_ops, 709 }; 710 711 /** 712 * kobject_create - create a struct kobject dynamically 713 * 714 * This function creates a kobject structure dynamically and sets it up 715 * to be a "dynamic" kobject with a default release function set up. 716 * 717 * If the kobject was not able to be created, NULL will be returned. 718 * The kobject structure returned from here must be cleaned up with a 719 * call to kobject_put() and not kfree(), as kobject_init() has 720 * already been called on this structure. 721 */ 722 struct kobject *kobject_create(void) 723 { 724 struct kobject *kobj; 725 726 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL); 727 if (!kobj) 728 return NULL; 729 730 kobject_init(kobj, &dynamic_kobj_ktype); 731 return kobj; 732 } 733 734 /** 735 * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs 736 * 737 * @name: the name for the kobject 738 * @parent: the parent kobject of this kobject, if any. 739 * 740 * This function creates a kobject structure dynamically and registers it 741 * with sysfs. When you are finished with this structure, call 742 * kobject_put() and the structure will be dynamically freed when 743 * it is no longer being used. 744 * 745 * If the kobject was not able to be created, NULL will be returned. 746 */ 747 struct kobject *kobject_create_and_add(const char *name, struct kobject *parent) 748 { 749 struct kobject *kobj; 750 int retval; 751 752 kobj = kobject_create(); 753 if (!kobj) 754 return NULL; 755 756 retval = kobject_add(kobj, parent, "%s", name); 757 if (retval) { 758 printk(KERN_WARNING "%s: kobject_add error: %d\n", 759 __func__, retval); 760 kobject_put(kobj); 761 kobj = NULL; 762 } 763 return kobj; 764 } 765 EXPORT_SYMBOL_GPL(kobject_create_and_add); 766 767 /** 768 * kset_init - initialize a kset for use 769 * @k: kset 770 */ 771 void kset_init(struct kset *k) 772 { 773 kobject_init_internal(&k->kobj); 774 INIT_LIST_HEAD(&k->list); 775 spin_lock_init(&k->list_lock); 776 } 777 778 /* default kobject attribute operations */ 779 static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr, 780 char *buf) 781 { 782 struct kobj_attribute *kattr; 783 ssize_t ret = -EIO; 784 785 kattr = container_of(attr, struct kobj_attribute, attr); 786 if (kattr->show) 787 ret = kattr->show(kobj, kattr, buf); 788 return ret; 789 } 790 791 static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr, 792 const char *buf, size_t count) 793 { 794 struct kobj_attribute *kattr; 795 ssize_t ret = -EIO; 796 797 kattr = container_of(attr, struct kobj_attribute, attr); 798 if (kattr->store) 799 ret = kattr->store(kobj, kattr, buf, count); 800 return ret; 801 } 802 803 const struct sysfs_ops kobj_sysfs_ops = { 804 .show = kobj_attr_show, 805 .store = kobj_attr_store, 806 }; 807 EXPORT_SYMBOL_GPL(kobj_sysfs_ops); 808 809 /** 810 * kset_register - initialize and add a kset. 811 * @k: kset. 812 */ 813 int kset_register(struct kset *k) 814 { 815 int err; 816 817 if (!k) 818 return -EINVAL; 819 820 kset_init(k); 821 err = kobject_add_internal(&k->kobj); 822 if (err) 823 return err; 824 kobject_uevent(&k->kobj, KOBJ_ADD); 825 return 0; 826 } 827 EXPORT_SYMBOL(kset_register); 828 829 /** 830 * kset_unregister - remove a kset. 831 * @k: kset. 832 */ 833 void kset_unregister(struct kset *k) 834 { 835 if (!k) 836 return; 837 kobject_del(&k->kobj); 838 kobject_put(&k->kobj); 839 } 840 EXPORT_SYMBOL(kset_unregister); 841 842 /** 843 * kset_find_obj - search for object in kset. 844 * @kset: kset we're looking in. 845 * @name: object's name. 846 * 847 * Lock kset via @kset->subsys, and iterate over @kset->list, 848 * looking for a matching kobject. If matching object is found 849 * take a reference and return the object. 850 */ 851 struct kobject *kset_find_obj(struct kset *kset, const char *name) 852 { 853 struct kobject *k; 854 struct kobject *ret = NULL; 855 856 spin_lock(&kset->list_lock); 857 858 list_for_each_entry(k, &kset->list, entry) { 859 if (kobject_name(k) && !strcmp(kobject_name(k), name)) { 860 ret = kobject_get_unless_zero(k); 861 break; 862 } 863 } 864 865 spin_unlock(&kset->list_lock); 866 return ret; 867 } 868 EXPORT_SYMBOL_GPL(kset_find_obj); 869 870 static void kset_release(struct kobject *kobj) 871 { 872 struct kset *kset = container_of(kobj, struct kset, kobj); 873 pr_debug("kobject: '%s' (%p): %s\n", 874 kobject_name(kobj), kobj, __func__); 875 kfree(kset); 876 } 877 878 static struct kobj_type kset_ktype = { 879 .sysfs_ops = &kobj_sysfs_ops, 880 .release = kset_release, 881 }; 882 883 /** 884 * kset_create - create a struct kset dynamically 885 * 886 * @name: the name for the kset 887 * @uevent_ops: a struct kset_uevent_ops for the kset 888 * @parent_kobj: the parent kobject of this kset, if any. 889 * 890 * This function creates a kset structure dynamically. This structure can 891 * then be registered with the system and show up in sysfs with a call to 892 * kset_register(). When you are finished with this structure, if 893 * kset_register() has been called, call kset_unregister() and the 894 * structure will be dynamically freed when it is no longer being used. 895 * 896 * If the kset was not able to be created, NULL will be returned. 897 */ 898 static struct kset *kset_create(const char *name, 899 const struct kset_uevent_ops *uevent_ops, 900 struct kobject *parent_kobj) 901 { 902 struct kset *kset; 903 int retval; 904 905 kset = kzalloc(sizeof(*kset), GFP_KERNEL); 906 if (!kset) 907 return NULL; 908 retval = kobject_set_name(&kset->kobj, "%s", name); 909 if (retval) { 910 kfree(kset); 911 return NULL; 912 } 913 kset->uevent_ops = uevent_ops; 914 kset->kobj.parent = parent_kobj; 915 916 /* 917 * The kobject of this kset will have a type of kset_ktype and belong to 918 * no kset itself. That way we can properly free it when it is 919 * finished being used. 920 */ 921 kset->kobj.ktype = &kset_ktype; 922 kset->kobj.kset = NULL; 923 924 return kset; 925 } 926 927 /** 928 * kset_create_and_add - create a struct kset dynamically and add it to sysfs 929 * 930 * @name: the name for the kset 931 * @uevent_ops: a struct kset_uevent_ops for the kset 932 * @parent_kobj: the parent kobject of this kset, if any. 933 * 934 * This function creates a kset structure dynamically and registers it 935 * with sysfs. When you are finished with this structure, call 936 * kset_unregister() and the structure will be dynamically freed when it 937 * is no longer being used. 938 * 939 * If the kset was not able to be created, NULL will be returned. 940 */ 941 struct kset *kset_create_and_add(const char *name, 942 const struct kset_uevent_ops *uevent_ops, 943 struct kobject *parent_kobj) 944 { 945 struct kset *kset; 946 int error; 947 948 kset = kset_create(name, uevent_ops, parent_kobj); 949 if (!kset) 950 return NULL; 951 error = kset_register(kset); 952 if (error) { 953 kfree(kset); 954 return NULL; 955 } 956 return kset; 957 } 958 EXPORT_SYMBOL_GPL(kset_create_and_add); 959 960 961 static DEFINE_SPINLOCK(kobj_ns_type_lock); 962 static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES]; 963 964 int kobj_ns_type_register(const struct kobj_ns_type_operations *ops) 965 { 966 enum kobj_ns_type type = ops->type; 967 int error; 968 969 spin_lock(&kobj_ns_type_lock); 970 971 error = -EINVAL; 972 if (type >= KOBJ_NS_TYPES) 973 goto out; 974 975 error = -EINVAL; 976 if (type <= KOBJ_NS_TYPE_NONE) 977 goto out; 978 979 error = -EBUSY; 980 if (kobj_ns_ops_tbl[type]) 981 goto out; 982 983 error = 0; 984 kobj_ns_ops_tbl[type] = ops; 985 986 out: 987 spin_unlock(&kobj_ns_type_lock); 988 return error; 989 } 990 991 int kobj_ns_type_registered(enum kobj_ns_type type) 992 { 993 int registered = 0; 994 995 spin_lock(&kobj_ns_type_lock); 996 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES)) 997 registered = kobj_ns_ops_tbl[type] != NULL; 998 spin_unlock(&kobj_ns_type_lock); 999 1000 return registered; 1001 } 1002 1003 const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent) 1004 { 1005 const struct kobj_ns_type_operations *ops = NULL; 1006 1007 if (parent && parent->ktype && parent->ktype->child_ns_type) 1008 ops = parent->ktype->child_ns_type(parent); 1009 1010 return ops; 1011 } 1012 1013 const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj) 1014 { 1015 return kobj_child_ns_ops(kobj->parent); 1016 } 1017 1018 bool kobj_ns_current_may_mount(enum kobj_ns_type type) 1019 { 1020 bool may_mount = true; 1021 1022 spin_lock(&kobj_ns_type_lock); 1023 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && 1024 kobj_ns_ops_tbl[type]) 1025 may_mount = kobj_ns_ops_tbl[type]->current_may_mount(); 1026 spin_unlock(&kobj_ns_type_lock); 1027 1028 return may_mount; 1029 } 1030 1031 void *kobj_ns_grab_current(enum kobj_ns_type type) 1032 { 1033 void *ns = NULL; 1034 1035 spin_lock(&kobj_ns_type_lock); 1036 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && 1037 kobj_ns_ops_tbl[type]) 1038 ns = kobj_ns_ops_tbl[type]->grab_current_ns(); 1039 spin_unlock(&kobj_ns_type_lock); 1040 1041 return ns; 1042 } 1043 1044 const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk) 1045 { 1046 const void *ns = NULL; 1047 1048 spin_lock(&kobj_ns_type_lock); 1049 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && 1050 kobj_ns_ops_tbl[type]) 1051 ns = kobj_ns_ops_tbl[type]->netlink_ns(sk); 1052 spin_unlock(&kobj_ns_type_lock); 1053 1054 return ns; 1055 } 1056 1057 const void *kobj_ns_initial(enum kobj_ns_type type) 1058 { 1059 const void *ns = NULL; 1060 1061 spin_lock(&kobj_ns_type_lock); 1062 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && 1063 kobj_ns_ops_tbl[type]) 1064 ns = kobj_ns_ops_tbl[type]->initial_ns(); 1065 spin_unlock(&kobj_ns_type_lock); 1066 1067 return ns; 1068 } 1069 1070 void kobj_ns_drop(enum kobj_ns_type type, void *ns) 1071 { 1072 spin_lock(&kobj_ns_type_lock); 1073 if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) && 1074 kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns) 1075 kobj_ns_ops_tbl[type]->drop_ns(ns); 1076 spin_unlock(&kobj_ns_type_lock); 1077 } 1078