]>
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> | |
5 | * | |
6 | * This file is released under the GPLv2. | |
7 | * | |
8 | * | |
9 | * Please see the file Documentation/kobject.txt for critical information | |
10 | * about using the kobject interface. | |
11 | */ | |
12 | ||
13 | #include <linux/kobject.h> | |
14 | #include <linux/string.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/stat.h> | |
4e57b681 | 17 | #include <linux/slab.h> |
1da177e4 LT |
18 | |
19 | /** | |
20 | * populate_dir - populate directory with attributes. | |
21 | * @kobj: object we're working on. | |
22 | * | |
23 | * Most subsystems have a set of default attributes that | |
24 | * are associated with an object that registers with them. | |
25 | * This is a helper called during object registration that | |
26 | * loops through the default attributes of the subsystem | |
27 | * and creates attributes files for them in sysfs. | |
28 | * | |
29 | */ | |
30 | ||
31 | static int populate_dir(struct kobject * kobj) | |
32 | { | |
33 | struct kobj_type * t = get_ktype(kobj); | |
34 | struct attribute * attr; | |
35 | int error = 0; | |
36 | int i; | |
37 | ||
38 | if (t && t->default_attrs) { | |
39 | for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) { | |
40 | if ((error = sysfs_create_file(kobj,attr))) | |
41 | break; | |
42 | } | |
43 | } | |
44 | return error; | |
45 | } | |
46 | ||
47 | static 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 | if ((error = populate_dir(kobj))) | |
54 | sysfs_remove_dir(kobj); | |
55 | } | |
56 | } | |
57 | return error; | |
58 | } | |
59 | ||
60 | static inline struct kobject * to_kobj(struct list_head * entry) | |
61 | { | |
62 | return container_of(entry,struct kobject,entry); | |
63 | } | |
64 | ||
65 | static int get_kobj_path_length(struct kobject *kobj) | |
66 | { | |
67 | int length = 1; | |
68 | struct kobject * parent = kobj; | |
69 | ||
70 | /* walk up the ancestors until we hit the one pointing to the | |
71 | * root. | |
72 | * Add 1 to strlen for leading '/' of each level. | |
73 | */ | |
74 | do { | |
75 | length += strlen(kobject_name(parent)) + 1; | |
76 | parent = parent->parent; | |
77 | } while (parent); | |
78 | return length; | |
79 | } | |
80 | ||
81 | static void fill_kobj_path(struct kobject *kobj, char *path, int length) | |
82 | { | |
83 | struct kobject * parent; | |
84 | ||
85 | --length; | |
86 | for (parent = kobj; parent; parent = parent->parent) { | |
87 | int cur = strlen(kobject_name(parent)); | |
88 | /* back up enough to print this name with '/' */ | |
89 | length -= cur; | |
90 | strncpy (path + length, kobject_name(parent), cur); | |
91 | *(path + --length) = '/'; | |
92 | } | |
93 | ||
94 | pr_debug("%s: path = '%s'\n",__FUNCTION__,path); | |
95 | } | |
96 | ||
97 | /** | |
98 | * kobject_get_path - generate and return the path associated with a given kobj | |
99 | * and kset pair. The result must be freed by the caller with kfree(). | |
100 | * | |
101 | * @kobj: kobject in question, with which to build the path | |
102 | * @gfp_mask: the allocation type used to allocate the path | |
103 | */ | |
fd4f2df2 | 104 | char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask) |
1da177e4 LT |
105 | { |
106 | char *path; | |
107 | int len; | |
108 | ||
109 | len = get_kobj_path_length(kobj); | |
110 | path = kmalloc(len, gfp_mask); | |
111 | if (!path) | |
112 | return NULL; | |
113 | memset(path, 0x00, len); | |
114 | fill_kobj_path(kobj, path, len); | |
115 | ||
116 | return path; | |
117 | } | |
118 | ||
119 | /** | |
120 | * kobject_init - initialize object. | |
121 | * @kobj: object in question. | |
122 | */ | |
123 | void kobject_init(struct kobject * kobj) | |
124 | { | |
125 | kref_init(&kobj->kref); | |
126 | INIT_LIST_HEAD(&kobj->entry); | |
127 | kobj->kset = kset_get(kobj->kset); | |
128 | } | |
129 | ||
130 | ||
131 | /** | |
132 | * unlink - remove kobject from kset list. | |
133 | * @kobj: kobject. | |
134 | * | |
135 | * Remove the kobject from the kset list and decrement | |
136 | * its parent's refcount. | |
137 | * This is separated out, so we can use it in both | |
138 | * kobject_del() and kobject_add() on error. | |
139 | */ | |
140 | ||
141 | static void unlink(struct kobject * kobj) | |
142 | { | |
143 | if (kobj->kset) { | |
144 | spin_lock(&kobj->kset->list_lock); | |
145 | list_del_init(&kobj->entry); | |
146 | spin_unlock(&kobj->kset->list_lock); | |
147 | } | |
148 | kobject_put(kobj); | |
149 | } | |
150 | ||
151 | /** | |
152 | * kobject_add - add an object to the hierarchy. | |
153 | * @kobj: object. | |
154 | */ | |
155 | ||
156 | int kobject_add(struct kobject * kobj) | |
157 | { | |
158 | int error = 0; | |
159 | struct kobject * parent; | |
160 | ||
161 | if (!(kobj = kobject_get(kobj))) | |
162 | return -ENOENT; | |
163 | if (!kobj->k_name) | |
164 | kobj->k_name = kobj->name; | |
c171fef5 GKH |
165 | if (!kobj->k_name) { |
166 | pr_debug("kobject attempted to be registered with no name!\n"); | |
167 | WARN_ON(1); | |
168 | return -EINVAL; | |
169 | } | |
1da177e4 LT |
170 | parent = kobject_get(kobj->parent); |
171 | ||
172 | pr_debug("kobject %s: registering. parent: %s, set: %s\n", | |
173 | kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>", | |
174 | kobj->kset ? kobj->kset->kobj.name : "<NULL>" ); | |
175 | ||
176 | if (kobj->kset) { | |
177 | spin_lock(&kobj->kset->list_lock); | |
178 | ||
179 | if (!parent) | |
180 | parent = kobject_get(&kobj->kset->kobj); | |
181 | ||
182 | list_add_tail(&kobj->entry,&kobj->kset->list); | |
183 | spin_unlock(&kobj->kset->list_lock); | |
184 | } | |
185 | kobj->parent = parent; | |
186 | ||
187 | error = create_dir(kobj); | |
188 | if (error) { | |
189 | /* unlink does the kobject_put() for us */ | |
190 | unlink(kobj); | |
191 | if (parent) | |
192 | kobject_put(parent); | |
1da177e4 LT |
193 | } |
194 | ||
195 | return error; | |
196 | } | |
197 | ||
198 | ||
199 | /** | |
200 | * kobject_register - initialize and add an object. | |
201 | * @kobj: object in question. | |
202 | */ | |
203 | ||
204 | int kobject_register(struct kobject * kobj) | |
205 | { | |
206 | int error = 0; | |
207 | if (kobj) { | |
208 | kobject_init(kobj); | |
209 | error = kobject_add(kobj); | |
210 | if (error) { | |
211 | printk("kobject_register failed for %s (%d)\n", | |
212 | kobject_name(kobj),error); | |
213 | dump_stack(); | |
18c3d527 | 214 | } else |
312c004d | 215 | kobject_uevent(kobj, KOBJ_ADD); |
1da177e4 LT |
216 | } else |
217 | error = -EINVAL; | |
218 | return error; | |
219 | } | |
220 | ||
221 | ||
222 | /** | |
223 | * kobject_set_name - Set the name of an object | |
224 | * @kobj: object. | |
67be2dd1 | 225 | * @fmt: format string used to build the name |
1da177e4 LT |
226 | * |
227 | * If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated | |
228 | * string that @kobj->k_name points to. Otherwise, use the static | |
229 | * @kobj->name array. | |
230 | */ | |
1da177e4 LT |
231 | int kobject_set_name(struct kobject * kobj, const char * fmt, ...) |
232 | { | |
233 | int error = 0; | |
234 | int limit = KOBJ_NAME_LEN; | |
235 | int need; | |
236 | va_list args; | |
237 | char * name; | |
238 | ||
239 | /* | |
240 | * First, try the static array | |
241 | */ | |
242 | va_start(args,fmt); | |
243 | need = vsnprintf(kobj->name,limit,fmt,args); | |
244 | va_end(args); | |
245 | if (need < limit) | |
246 | name = kobj->name; | |
247 | else { | |
248 | /* | |
249 | * Need more space? Allocate it and try again | |
250 | */ | |
251 | limit = need + 1; | |
252 | name = kmalloc(limit,GFP_KERNEL); | |
253 | if (!name) { | |
254 | error = -ENOMEM; | |
255 | goto Done; | |
256 | } | |
257 | va_start(args,fmt); | |
258 | need = vsnprintf(name,limit,fmt,args); | |
259 | va_end(args); | |
260 | ||
261 | /* Still? Give up. */ | |
262 | if (need >= limit) { | |
263 | kfree(name); | |
264 | error = -EFAULT; | |
265 | goto Done; | |
266 | } | |
267 | } | |
268 | ||
269 | /* Free the old name, if necessary. */ | |
270 | if (kobj->k_name && kobj->k_name != kobj->name) | |
271 | kfree(kobj->k_name); | |
272 | ||
273 | /* Now, set the new name */ | |
274 | kobj->k_name = name; | |
275 | Done: | |
276 | return error; | |
277 | } | |
278 | ||
279 | EXPORT_SYMBOL(kobject_set_name); | |
280 | ||
281 | ||
282 | /** | |
283 | * kobject_rename - change the name of an object | |
284 | * @kobj: object in question. | |
285 | * @new_name: object's new name | |
286 | */ | |
287 | ||
f3b4f3c6 | 288 | int kobject_rename(struct kobject * kobj, const char *new_name) |
1da177e4 LT |
289 | { |
290 | int error = 0; | |
291 | ||
292 | kobj = kobject_get(kobj); | |
293 | if (!kobj) | |
294 | return -EINVAL; | |
295 | error = sysfs_rename_dir(kobj, new_name); | |
296 | kobject_put(kobj); | |
297 | ||
298 | return error; | |
299 | } | |
300 | ||
301 | /** | |
302 | * kobject_del - unlink kobject from hierarchy. | |
303 | * @kobj: object. | |
304 | */ | |
305 | ||
306 | void kobject_del(struct kobject * kobj) | |
307 | { | |
1da177e4 LT |
308 | sysfs_remove_dir(kobj); |
309 | unlink(kobj); | |
310 | } | |
311 | ||
312 | /** | |
313 | * kobject_unregister - remove object from hierarchy and decrement refcount. | |
314 | * @kobj: object going away. | |
315 | */ | |
316 | ||
317 | void kobject_unregister(struct kobject * kobj) | |
318 | { | |
319 | pr_debug("kobject %s: unregistering\n",kobject_name(kobj)); | |
312c004d | 320 | kobject_uevent(kobj, KOBJ_REMOVE); |
1da177e4 LT |
321 | kobject_del(kobj); |
322 | kobject_put(kobj); | |
323 | } | |
324 | ||
325 | /** | |
326 | * kobject_get - increment refcount for object. | |
327 | * @kobj: object. | |
328 | */ | |
329 | ||
330 | struct kobject * kobject_get(struct kobject * kobj) | |
331 | { | |
332 | if (kobj) | |
333 | kref_get(&kobj->kref); | |
334 | return kobj; | |
335 | } | |
336 | ||
337 | /** | |
338 | * kobject_cleanup - free kobject resources. | |
339 | * @kobj: object. | |
340 | */ | |
341 | ||
342 | void kobject_cleanup(struct kobject * kobj) | |
343 | { | |
344 | struct kobj_type * t = get_ktype(kobj); | |
345 | struct kset * s = kobj->kset; | |
346 | struct kobject * parent = kobj->parent; | |
347 | ||
348 | pr_debug("kobject %s: cleaning up\n",kobject_name(kobj)); | |
349 | if (kobj->k_name != kobj->name) | |
350 | kfree(kobj->k_name); | |
351 | kobj->k_name = NULL; | |
352 | if (t && t->release) | |
353 | t->release(kobj); | |
354 | if (s) | |
355 | kset_put(s); | |
356 | if (parent) | |
357 | kobject_put(parent); | |
358 | } | |
359 | ||
360 | static void kobject_release(struct kref *kref) | |
361 | { | |
362 | kobject_cleanup(container_of(kref, struct kobject, kref)); | |
363 | } | |
364 | ||
365 | /** | |
366 | * kobject_put - decrement refcount for object. | |
367 | * @kobj: object. | |
368 | * | |
369 | * Decrement the refcount, and if 0, call kobject_cleanup(). | |
370 | */ | |
371 | void kobject_put(struct kobject * kobj) | |
372 | { | |
373 | if (kobj) | |
374 | kref_put(&kobj->kref, kobject_release); | |
375 | } | |
376 | ||
377 | ||
378 | /** | |
379 | * kset_init - initialize a kset for use | |
380 | * @k: kset | |
381 | */ | |
382 | ||
383 | void kset_init(struct kset * k) | |
384 | { | |
385 | kobject_init(&k->kobj); | |
386 | INIT_LIST_HEAD(&k->list); | |
387 | spin_lock_init(&k->list_lock); | |
388 | } | |
389 | ||
390 | ||
391 | /** | |
392 | * kset_add - add a kset object to the hierarchy. | |
393 | * @k: kset. | |
394 | * | |
395 | * Simply, this adds the kset's embedded kobject to the | |
396 | * hierarchy. | |
397 | * We also try to make sure that the kset's embedded kobject | |
398 | * has a parent before it is added. We only care if the embedded | |
399 | * kobject is not part of a kset itself, since kobject_add() | |
400 | * assigns a parent in that case. | |
401 | * If that is the case, and the kset has a controlling subsystem, | |
402 | * then we set the kset's parent to be said subsystem. | |
403 | */ | |
404 | ||
405 | int kset_add(struct kset * k) | |
406 | { | |
407 | if (!k->kobj.parent && !k->kobj.kset && k->subsys) | |
408 | k->kobj.parent = &k->subsys->kset.kobj; | |
409 | ||
410 | return kobject_add(&k->kobj); | |
411 | } | |
412 | ||
413 | ||
414 | /** | |
415 | * kset_register - initialize and add a kset. | |
416 | * @k: kset. | |
417 | */ | |
418 | ||
419 | int kset_register(struct kset * k) | |
420 | { | |
421 | kset_init(k); | |
422 | return kset_add(k); | |
423 | } | |
424 | ||
425 | ||
426 | /** | |
427 | * kset_unregister - remove a kset. | |
428 | * @k: kset. | |
429 | */ | |
430 | ||
431 | void kset_unregister(struct kset * k) | |
432 | { | |
433 | kobject_unregister(&k->kobj); | |
434 | } | |
435 | ||
436 | ||
437 | /** | |
438 | * kset_find_obj - search for object in kset. | |
439 | * @kset: kset we're looking in. | |
440 | * @name: object's name. | |
441 | * | |
442 | * Lock kset via @kset->subsys, and iterate over @kset->list, | |
443 | * looking for a matching kobject. If matching object is found | |
444 | * take a reference and return the object. | |
445 | */ | |
446 | ||
447 | struct kobject * kset_find_obj(struct kset * kset, const char * name) | |
448 | { | |
449 | struct list_head * entry; | |
450 | struct kobject * ret = NULL; | |
451 | ||
452 | spin_lock(&kset->list_lock); | |
453 | list_for_each(entry,&kset->list) { | |
454 | struct kobject * k = to_kobj(entry); | |
455 | if (kobject_name(k) && !strcmp(kobject_name(k),name)) { | |
456 | ret = kobject_get(k); | |
457 | break; | |
458 | } | |
459 | } | |
460 | spin_unlock(&kset->list_lock); | |
461 | return ret; | |
462 | } | |
463 | ||
464 | ||
465 | void subsystem_init(struct subsystem * s) | |
466 | { | |
467 | init_rwsem(&s->rwsem); | |
468 | kset_init(&s->kset); | |
469 | } | |
470 | ||
471 | /** | |
472 | * subsystem_register - register a subsystem. | |
473 | * @s: the subsystem we're registering. | |
474 | * | |
475 | * Once we register the subsystem, we want to make sure that | |
476 | * the kset points back to this subsystem for correct usage of | |
477 | * the rwsem. | |
478 | */ | |
479 | ||
480 | int subsystem_register(struct subsystem * s) | |
481 | { | |
482 | int error; | |
483 | ||
484 | subsystem_init(s); | |
485 | pr_debug("subsystem %s: registering\n",s->kset.kobj.name); | |
486 | ||
487 | if (!(error = kset_add(&s->kset))) { | |
488 | if (!s->kset.subsys) | |
489 | s->kset.subsys = s; | |
490 | } | |
491 | return error; | |
492 | } | |
493 | ||
494 | void subsystem_unregister(struct subsystem * s) | |
495 | { | |
496 | pr_debug("subsystem %s: unregistering\n",s->kset.kobj.name); | |
497 | kset_unregister(&s->kset); | |
498 | } | |
499 | ||
500 | ||
501 | /** | |
502 | * subsystem_create_file - export sysfs attribute file. | |
503 | * @s: subsystem. | |
504 | * @a: subsystem attribute descriptor. | |
505 | */ | |
506 | ||
507 | int subsys_create_file(struct subsystem * s, struct subsys_attribute * a) | |
508 | { | |
509 | int error = 0; | |
510 | if (subsys_get(s)) { | |
511 | error = sysfs_create_file(&s->kset.kobj,&a->attr); | |
512 | subsys_put(s); | |
513 | } | |
514 | return error; | |
515 | } | |
516 | ||
517 | ||
518 | /** | |
519 | * subsystem_remove_file - remove sysfs attribute file. | |
520 | * @s: subsystem. | |
521 | * @a: attribute desciptor. | |
522 | */ | |
523 | ||
524 | void subsys_remove_file(struct subsystem * s, struct subsys_attribute * a) | |
525 | { | |
526 | if (subsys_get(s)) { | |
527 | sysfs_remove_file(&s->kset.kobj,&a->attr); | |
528 | subsys_put(s); | |
529 | } | |
530 | } | |
531 | ||
532 | EXPORT_SYMBOL(kobject_init); | |
533 | EXPORT_SYMBOL(kobject_register); | |
534 | EXPORT_SYMBOL(kobject_unregister); | |
535 | EXPORT_SYMBOL(kobject_get); | |
536 | EXPORT_SYMBOL(kobject_put); | |
537 | EXPORT_SYMBOL(kobject_add); | |
538 | EXPORT_SYMBOL(kobject_del); | |
539 | ||
540 | EXPORT_SYMBOL(kset_register); | |
541 | EXPORT_SYMBOL(kset_unregister); | |
542 | EXPORT_SYMBOL(kset_find_obj); | |
543 | ||
544 | EXPORT_SYMBOL(subsystem_init); | |
545 | EXPORT_SYMBOL(subsystem_register); | |
546 | EXPORT_SYMBOL(subsystem_unregister); | |
547 | EXPORT_SYMBOL(subsys_create_file); | |
548 | EXPORT_SYMBOL(subsys_remove_file); |