aboutsummaryrefslogtreecommitdiffstats
path: root/lib/kobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/kobject.c')
-rw-r--r--lib/kobject.c93
1 files changed, 61 insertions, 32 deletions
diff --git a/lib/kobject.c b/lib/kobject.c
index aa89edcd2b63..f2ccdbac8ed9 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -18,7 +18,7 @@
18#include <linux/random.h> 18#include <linux/random.h>
19 19
20/** 20/**
21 * kobject_namespace - return @kobj's namespace tag 21 * kobject_namespace() - Return @kobj's namespace tag.
22 * @kobj: kobject in question 22 * @kobj: kobject in question
23 * 23 *
24 * Returns namespace tag of @kobj if its parent has namespace ops enabled 24 * Returns namespace tag of @kobj if its parent has namespace ops enabled
@@ -36,7 +36,7 @@ const void *kobject_namespace(struct kobject *kobj)
36} 36}
37 37
38/** 38/**
39 * kobject_get_ownership - get sysfs ownership data for @kobj 39 * kobject_get_ownership() - Get sysfs ownership data for @kobj.
40 * @kobj: kobject in question 40 * @kobj: kobject in question
41 * @uid: kernel user ID for sysfs objects 41 * @uid: kernel user ID for sysfs objects
42 * @gid: kernel group ID for sysfs objects 42 * @gid: kernel group ID for sysfs objects
@@ -82,6 +82,7 @@ static int populate_dir(struct kobject *kobj)
82 82
83static int create_dir(struct kobject *kobj) 83static int create_dir(struct kobject *kobj)
84{ 84{
85 const struct kobj_type *ktype = get_ktype(kobj);
85 const struct kobj_ns_type_operations *ops; 86 const struct kobj_ns_type_operations *ops;
86 int error; 87 int error;
87 88
@@ -95,6 +96,14 @@ static int create_dir(struct kobject *kobj)
95 return error; 96 return error;
96 } 97 }
97 98
99 if (ktype) {
100 error = sysfs_create_groups(kobj, ktype->default_groups);
101 if (error) {
102 sysfs_remove_dir(kobj);
103 return error;
104 }
105 }
106
98 /* 107 /*
99 * @kobj->sd may be deleted by an ancestor going away. Hold an 108 * @kobj->sd may be deleted by an ancestor going away. Hold an
100 * extra reference so that it stays until @kobj is gone. 109 * extra reference so that it stays until @kobj is gone.
@@ -153,12 +162,11 @@ static void fill_kobj_path(struct kobject *kobj, char *path, int length)
153} 162}
154 163
155/** 164/**
156 * kobject_get_path - generate and return the path associated with a given kobj and kset pair. 165 * kobject_get_path() - Allocate memory and fill in the path for @kobj.
157 *
158 * @kobj: kobject in question, with which to build the path 166 * @kobj: kobject in question, with which to build the path
159 * @gfp_mask: the allocation type used to allocate the path 167 * @gfp_mask: the allocation type used to allocate the path
160 * 168 *
161 * The result must be freed by the caller with kfree(). 169 * Return: The newly allocated memory, caller must free with kfree().
162 */ 170 */
163char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask) 171char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
164{ 172{
@@ -265,7 +273,7 @@ static int kobject_add_internal(struct kobject *kobj)
265} 273}
266 274
267/** 275/**
268 * kobject_set_name_vargs - Set the name of an kobject 276 * kobject_set_name_vargs() - Set the name of a kobject.
269 * @kobj: struct kobject to set the name of 277 * @kobj: struct kobject to set the name of
270 * @fmt: format string used to build the name 278 * @fmt: format string used to build the name
271 * @vargs: vargs to format the string. 279 * @vargs: vargs to format the string.
@@ -305,7 +313,7 @@ int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
305} 313}
306 314
307/** 315/**
308 * kobject_set_name - Set the name of a kobject 316 * kobject_set_name() - Set the name of a kobject.
309 * @kobj: struct kobject to set the name of 317 * @kobj: struct kobject to set the name of
310 * @fmt: format string used to build the name 318 * @fmt: format string used to build the name
311 * 319 *
@@ -327,7 +335,7 @@ int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
327EXPORT_SYMBOL(kobject_set_name); 335EXPORT_SYMBOL(kobject_set_name);
328 336
329/** 337/**
330 * kobject_init - initialize a kobject structure 338 * kobject_init() - Initialize a kobject structure.
331 * @kobj: pointer to the kobject to initialize 339 * @kobj: pointer to the kobject to initialize
332 * @ktype: pointer to the ktype for this kobject. 340 * @ktype: pointer to the ktype for this kobject.
333 * 341 *
@@ -383,7 +391,7 @@ static __printf(3, 0) int kobject_add_varg(struct kobject *kobj,
383} 391}
384 392
385/** 393/**
386 * kobject_add - the main kobject add function 394 * kobject_add() - The main kobject add function.
387 * @kobj: the kobject to add 395 * @kobj: the kobject to add
388 * @parent: pointer to the parent of the kobject. 396 * @parent: pointer to the parent of the kobject.
389 * @fmt: format to name the kobject with. 397 * @fmt: format to name the kobject with.
@@ -397,15 +405,23 @@ static __printf(3, 0) int kobject_add_varg(struct kobject *kobj,
397 * is assigned to the kobject, then the kobject will be located in the 405 * is assigned to the kobject, then the kobject will be located in the
398 * root of the sysfs tree. 406 * root of the sysfs tree.
399 * 407 *
400 * If this function returns an error, kobject_put() must be called to
401 * properly clean up the memory associated with the object.
402 * Under no instance should the kobject that is passed to this function
403 * be directly freed with a call to kfree(), that can leak memory.
404 *
405 * Note, no "add" uevent will be created with this call, the caller should set 408 * Note, no "add" uevent will be created with this call, the caller should set
406 * up all of the necessary sysfs files for the object and then call 409 * up all of the necessary sysfs files for the object and then call
407 * kobject_uevent() with the UEVENT_ADD parameter to ensure that 410 * kobject_uevent() with the UEVENT_ADD parameter to ensure that
408 * userspace is properly notified of this kobject's creation. 411 * userspace is properly notified of this kobject's creation.
412 *
413 * Return: If this function returns an error, kobject_put() must be
414 * called to properly clean up the memory associated with the
415 * object. Under no instance should the kobject that is passed
416 * to this function be directly freed with a call to kfree(),
417 * that can leak memory.
418 *
419 * If this function returns success, kobject_put() must also be called
420 * in order to properly clean up the memory associated with the object.
421 *
422 * In short, once this function is called, kobject_put() MUST be called
423 * when the use of the object is finished in order to properly free
424 * everything.
409 */ 425 */
410int kobject_add(struct kobject *kobj, struct kobject *parent, 426int kobject_add(struct kobject *kobj, struct kobject *parent,
411 const char *fmt, ...) 427 const char *fmt, ...)
@@ -431,15 +447,19 @@ int kobject_add(struct kobject *kobj, struct kobject *parent,
431EXPORT_SYMBOL(kobject_add); 447EXPORT_SYMBOL(kobject_add);
432 448
433/** 449/**
434 * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy 450 * kobject_init_and_add() - Initialize a kobject structure and add it to
451 * the kobject hierarchy.
435 * @kobj: pointer to the kobject to initialize 452 * @kobj: pointer to the kobject to initialize
436 * @ktype: pointer to the ktype for this kobject. 453 * @ktype: pointer to the ktype for this kobject.
437 * @parent: pointer to the parent of this kobject. 454 * @parent: pointer to the parent of this kobject.
438 * @fmt: the name of the kobject. 455 * @fmt: the name of the kobject.
439 * 456 *
440 * This function combines the call to kobject_init() and 457 * This function combines the call to kobject_init() and kobject_add().
441 * kobject_add(). The same type of error handling after a call to 458 *
442 * kobject_add() and kobject lifetime rules are the same here. 459 * If this function returns an error, kobject_put() must be called to
460 * properly clean up the memory associated with the object. This is the
461 * same type of error handling after a call to kobject_add() and kobject
462 * lifetime rules are the same here.
443 */ 463 */
444int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, 464int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
445 struct kobject *parent, const char *fmt, ...) 465 struct kobject *parent, const char *fmt, ...)
@@ -458,7 +478,7 @@ int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
458EXPORT_SYMBOL_GPL(kobject_init_and_add); 478EXPORT_SYMBOL_GPL(kobject_init_and_add);
459 479
460/** 480/**
461 * kobject_rename - change the name of an object 481 * kobject_rename() - Change the name of an object.
462 * @kobj: object in question. 482 * @kobj: object in question.
463 * @new_name: object's new name 483 * @new_name: object's new name
464 * 484 *
@@ -525,7 +545,7 @@ out:
525EXPORT_SYMBOL_GPL(kobject_rename); 545EXPORT_SYMBOL_GPL(kobject_rename);
526 546
527/** 547/**
528 * kobject_move - move object to another parent 548 * kobject_move() - Move object to another parent.
529 * @kobj: object in question. 549 * @kobj: object in question.
530 * @new_parent: object's new parent (can be NULL) 550 * @new_parent: object's new parent (can be NULL)
531 */ 551 */
@@ -578,17 +598,26 @@ out:
578EXPORT_SYMBOL_GPL(kobject_move); 598EXPORT_SYMBOL_GPL(kobject_move);
579 599
580/** 600/**
581 * kobject_del - unlink kobject from hierarchy. 601 * kobject_del() - Unlink kobject from hierarchy.
582 * @kobj: object. 602 * @kobj: object.
603 *
604 * This is the function that should be called to delete an object
605 * successfully added via kobject_add().
583 */ 606 */
584void kobject_del(struct kobject *kobj) 607void kobject_del(struct kobject *kobj)
585{ 608{
586 struct kernfs_node *sd; 609 struct kernfs_node *sd;
610 const struct kobj_type *ktype;
587 611
588 if (!kobj) 612 if (!kobj)
589 return; 613 return;
590 614
591 sd = kobj->sd; 615 sd = kobj->sd;
616 ktype = get_ktype(kobj);
617
618 if (ktype)
619 sysfs_remove_groups(kobj, ktype->default_groups);
620
592 sysfs_remove_dir(kobj); 621 sysfs_remove_dir(kobj);
593 sysfs_put(sd); 622 sysfs_put(sd);
594 623
@@ -600,7 +629,7 @@ void kobject_del(struct kobject *kobj)
600EXPORT_SYMBOL(kobject_del); 629EXPORT_SYMBOL(kobject_del);
601 630
602/** 631/**
603 * kobject_get - increment refcount for object. 632 * kobject_get() - Increment refcount for object.
604 * @kobj: object. 633 * @kobj: object.
605 */ 634 */
606struct kobject *kobject_get(struct kobject *kobj) 635struct kobject *kobject_get(struct kobject *kobj)
@@ -693,7 +722,7 @@ static void kobject_release(struct kref *kref)
693} 722}
694 723
695/** 724/**
696 * kobject_put - decrement refcount for object. 725 * kobject_put() - Decrement refcount for object.
697 * @kobj: object. 726 * @kobj: object.
698 * 727 *
699 * Decrement the refcount, and if 0, call kobject_cleanup(). 728 * Decrement the refcount, and if 0, call kobject_cleanup().
@@ -722,7 +751,7 @@ static struct kobj_type dynamic_kobj_ktype = {
722}; 751};
723 752
724/** 753/**
725 * kobject_create - create a struct kobject dynamically 754 * kobject_create() - Create a struct kobject dynamically.
726 * 755 *
727 * This function creates a kobject structure dynamically and sets it up 756 * This function creates a kobject structure dynamically and sets it up
728 * to be a "dynamic" kobject with a default release function set up. 757 * to be a "dynamic" kobject with a default release function set up.
@@ -745,8 +774,8 @@ struct kobject *kobject_create(void)
745} 774}
746 775
747/** 776/**
748 * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs 777 * kobject_create_and_add() - Create a struct kobject dynamically and
749 * 778 * register it with sysfs.
750 * @name: the name for the kobject 779 * @name: the name for the kobject
751 * @parent: the parent kobject of this kobject, if any. 780 * @parent: the parent kobject of this kobject, if any.
752 * 781 *
@@ -777,7 +806,7 @@ struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
777EXPORT_SYMBOL_GPL(kobject_create_and_add); 806EXPORT_SYMBOL_GPL(kobject_create_and_add);
778 807
779/** 808/**
780 * kset_init - initialize a kset for use 809 * kset_init() - Initialize a kset for use.
781 * @k: kset 810 * @k: kset
782 */ 811 */
783void kset_init(struct kset *k) 812void kset_init(struct kset *k)
@@ -819,7 +848,7 @@ const struct sysfs_ops kobj_sysfs_ops = {
819EXPORT_SYMBOL_GPL(kobj_sysfs_ops); 848EXPORT_SYMBOL_GPL(kobj_sysfs_ops);
820 849
821/** 850/**
822 * kset_register - initialize and add a kset. 851 * kset_register() - Initialize and add a kset.
823 * @k: kset. 852 * @k: kset.
824 */ 853 */
825int kset_register(struct kset *k) 854int kset_register(struct kset *k)
@@ -839,7 +868,7 @@ int kset_register(struct kset *k)
839EXPORT_SYMBOL(kset_register); 868EXPORT_SYMBOL(kset_register);
840 869
841/** 870/**
842 * kset_unregister - remove a kset. 871 * kset_unregister() - Remove a kset.
843 * @k: kset. 872 * @k: kset.
844 */ 873 */
845void kset_unregister(struct kset *k) 874void kset_unregister(struct kset *k)
@@ -852,7 +881,7 @@ void kset_unregister(struct kset *k)
852EXPORT_SYMBOL(kset_unregister); 881EXPORT_SYMBOL(kset_unregister);
853 882
854/** 883/**
855 * kset_find_obj - search for object in kset. 884 * kset_find_obj() - Search for object in kset.
856 * @kset: kset we're looking in. 885 * @kset: kset we're looking in.
857 * @name: object's name. 886 * @name: object's name.
858 * 887 *
@@ -900,7 +929,7 @@ static struct kobj_type kset_ktype = {
900}; 929};
901 930
902/** 931/**
903 * kset_create - create a struct kset dynamically 932 * kset_create() - Create a struct kset dynamically.
904 * 933 *
905 * @name: the name for the kset 934 * @name: the name for the kset
906 * @uevent_ops: a struct kset_uevent_ops for the kset 935 * @uevent_ops: a struct kset_uevent_ops for the kset
@@ -944,7 +973,7 @@ static struct kset *kset_create(const char *name,
944} 973}
945 974
946/** 975/**
947 * kset_create_and_add - create a struct kset dynamically and add it to sysfs 976 * kset_create_and_add() - Create a struct kset dynamically and add it to sysfs.
948 * 977 *
949 * @name: the name for the kset 978 * @name: the name for the kset
950 * @uevent_ops: a struct kset_uevent_ops for the kset 979 * @uevent_ops: a struct kset_uevent_ops for the kset