source: GPL/branches/uniaud32-next/lib32/kobject.c@ 660

Last change on this file since 660 was 615, checked in by Paul Smedley, 5 years ago

Add source for uniaud32 based on code from linux kernel 5.4.86

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