16 #include <linux/string.h>
17 #include <linux/export.h>
18 #include <linux/stat.h>
19 #include <linux/slab.h>
30 static int populate_dir(
struct kobject *kobj)
47 static int create_dir(
struct kobject *kobj)
52 error = populate_dir(kobj);
59 static int get_kobj_path_length(
struct kobject *kobj)
69 if (kobject_name(parent) ==
NULL)
71 length +=
strlen(kobject_name(parent)) + 1;
77 static void fill_kobj_path(
struct kobject *kobj,
char *
path,
int length)
86 strncpy(path + length, kobject_name(parent), cur);
90 pr_debug(
"kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
91 kobj, __func__, path);
107 len = get_kobj_path_length(kobj);
110 path = kzalloc(len, gfp_mask);
113 fill_kobj_path(kobj, path, len);
120 static void kobj_kset_join(
struct kobject *kobj)
125 kset_get(kobj->
kset);
126 spin_lock(&kobj->
kset->list_lock);
128 spin_unlock(&kobj->
kset->list_lock);
132 static void kobj_kset_leave(
struct kobject *kobj)
137 spin_lock(&kobj->
kset->list_lock);
138 list_del_init(&kobj->
entry);
139 spin_unlock(&kobj->
kset->list_lock);
140 kset_put(kobj->
kset);
143 static void kobject_init_internal(
struct kobject *kobj)
147 kref_init(&kobj->
kref);
148 INIT_LIST_HEAD(&kobj->
entry);
156 static int kobject_add_internal(
struct kobject *kobj)
164 if (!kobj->
name || !kobj->
name[0]) {
165 WARN(1,
"kobject: (%p): attempted to be registered with empty "
176 kobj_kset_join(kobj);
180 pr_debug(
"kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
181 kobject_name(kobj), kobj, __func__,
182 parent ? kobject_name(parent) :
"<NULL>",
183 kobj->
kset ? kobject_name(&kobj->
kset->kobj) :
"<NULL>");
185 error = create_dir(kobj);
187 kobj_kset_leave(kobj);
193 WARN(1,
"%s failed for %s with "
194 "-EEXIST, don't try to register things with "
195 "the same name in the same directory.\n",
196 __func__, kobject_name(kobj));
198 WARN(1,
"%s failed for %s (error: %d parent: %s)\n",
199 __func__, kobject_name(kobj), error,
200 parent ? kobject_name(parent) :
"'none'");
216 const char *old_name = kobj->
name;
219 if (kobj->
name && !fmt)
273 err_str =
"invalid kobject pointer!";
277 err_str =
"must have a ktype to be initialized properly!\n";
283 "object, something is seriously wrong.\n", kobj);
287 kobject_init_internal(kobj);
297 static int kobject_add_varg(
struct kobject *kobj,
struct kobject *parent,
308 return kobject_add_internal(kobj);
337 const char *fmt, ...)
347 "uninitialized object, something is seriously wrong.\n",
348 kobject_name(kobj), kobj);
353 retval = kobject_add_varg(kobj, parent, fmt, args);
372 struct kobject *parent,
const char *fmt, ...)
380 retval = kobject_add_varg(kobj, parent, fmt, args);
400 const char *devpath =
NULL;
402 char *devpath_string =
NULL;
417 if (!devpath_string) {
421 sprintf(devpath_string,
"DEVPATH_OLD=%s", devpath);
422 envp[0] = devpath_string;
436 dup_name = kobj->
name;
446 kfree(devpath_string);
463 const char *devpath =
NULL;
464 char *devpath_string =
NULL;
482 if (!devpath_string) {
486 sprintf(devpath_string,
"DEVPATH_OLD=%s", devpath);
487 envp[0] = devpath_string;
492 old_parent = kobj->
parent;
493 kobj->
parent = new_parent;
500 kfree(devpath_string);
516 kobj_kset_leave(kobj);
528 kref_get(&kobj->
kref);
536 static void kobject_cleanup(
struct kobject *kobj)
541 pr_debug(
"kobject: '%s' (%p): %s\n",
542 kobject_name(kobj), kobj, __func__);
545 pr_debug(
"kobject: '%s' (%p): does not have a release() "
546 "function, it is broken and must be fixed.\n",
547 kobject_name(kobj), kobj);
551 pr_debug(
"kobject: '%s' (%p): auto cleanup 'remove' event\n",
552 kobject_name(kobj), kobj);
558 pr_debug(
"kobject: '%s' (%p): auto cleanup kobject_del\n",
559 kobject_name(kobj), kobj);
564 pr_debug(
"kobject: '%s' (%p): calling ktype release\n",
565 kobject_name(kobj), kobj);
571 pr_debug(
"kobject: '%s': free name\n", name);
576 static void kobject_release(
struct kref *
kref)
592 "initialized, yet kobject_put() is being "
593 "called.\n", kobject_name(kobj), kobj);
594 kref_put(&kobj->
kref, kobject_release);
598 static void dynamic_kobj_release(
struct kobject *kobj)
600 pr_debug(
"kobject: (%p): %s\n", kobj, __func__);
604 static struct kobj_type dynamic_kobj_ktype = {
605 .release = dynamic_kobj_release,
671 kobject_init_internal(&k->
kobj);
672 INIT_LIST_HEAD(&k->
list);
685 ret = kattr->
show(kobj, kattr, buf);
690 const char *buf,
size_t count)
697 ret = kattr->
store(kobj, kattr, buf, count);
702 .show = kobj_attr_show,
703 .store = kobj_attr_store,
718 err = kobject_add_internal(&k->
kobj);
753 if (kobject_name(k) && !
strcmp(kobject_name(k), name)) {
763 static void kset_release(
struct kobject *kobj)
766 pr_debug(
"kobject: '%s' (%p): %s\n",
767 kobject_name(kobj), kobj, __func__);
773 .release = kset_release,
791 static struct kset *kset_create(
const char *name,
807 kset->
kobj.parent = parent_kobj;
814 kset->
kobj.ktype = &kset_ktype;
841 kset = kset_create(name, uevent_ops, parent_kobj);
862 spin_lock(&kobj_ns_type_lock);
873 if (kobj_ns_ops_tbl[type])
877 kobj_ns_ops_tbl[
type] = ops;
880 spin_unlock(&kobj_ns_type_lock);
888 spin_lock(&kobj_ns_type_lock);
890 registered = kobj_ns_ops_tbl[
type] !=
NULL;
891 spin_unlock(&kobj_ns_type_lock);
900 if (parent && parent->
ktype->child_ns_type)
901 ops = parent->
ktype->child_ns_type(parent);
916 spin_lock(&kobj_ns_type_lock);
918 kobj_ns_ops_tbl[type])
920 spin_unlock(&kobj_ns_type_lock);
929 spin_lock(&kobj_ns_type_lock);
931 kobj_ns_ops_tbl[type])
933 spin_unlock(&kobj_ns_type_lock);
942 spin_lock(&kobj_ns_type_lock);
944 kobj_ns_ops_tbl[type])
946 spin_unlock(&kobj_ns_type_lock);
953 spin_lock(&kobj_ns_type_lock);
955 kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->
drop_ns)
957 spin_unlock(&kobj_ns_type_lock);