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