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