pagecache-zeroing-zero_user_segment-zero_user_segments-and-zero_user
[linux-2.6/linux-trees-mm.git] / lib / kobject.c
blob8f26b06222252e8d127431f90dcc7ede29f47dff
1 /*
2 * kobject.c - library routines for handling generic kernel objects
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.
8 * This file is released under the GPLv2.
11 * Please see the file Documentation/kobject.txt for critical information
12 * about using the kobject interface.
15 #include <linux/kobject.h>
16 #include <linux/string.h>
17 #include <linux/module.h>
18 #include <linux/stat.h>
19 #include <linux/slab.h>
20 #include <linux/kallsyms.h>
21 #include <asm-generic/sections.h>
23 /**
24 * populate_dir - populate directory with attributes.
25 * @kobj: object we're working on.
27 * Most subsystems have a set of default attributes that
28 * are associated with an object that registers with them.
29 * This is a helper called during object registration that
30 * loops through the default attributes of the subsystem
31 * and creates attributes files for them in sysfs.
35 static int populate_dir(struct kobject * kobj)
37 struct kobj_type * t = get_ktype(kobj);
38 struct attribute * attr;
39 int error = 0;
40 int i;
42 if (t && t->default_attrs) {
43 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
44 if ((error = sysfs_create_file(kobj,attr)))
45 break;
48 return error;
51 static int create_dir(struct kobject * kobj)
53 int error = 0;
54 if (kobject_name(kobj)) {
55 error = sysfs_create_dir(kobj);
56 if (!error) {
57 if ((error = populate_dir(kobj)))
58 sysfs_remove_dir(kobj);
61 return error;
64 static inline struct kobject * to_kobj(struct list_head * entry)
66 return container_of(entry,struct kobject,entry);
69 static int get_kobj_path_length(struct kobject *kobj)
71 int length = 1;
72 struct kobject * parent = kobj;
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.
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;
87 static void fill_kobj_path(struct kobject *kobj, char *path, int length)
89 struct kobject * parent;
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) = '/';
100 pr_debug("%s: path = '%s'\n",__FUNCTION__,path);
104 * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
106 * @kobj: kobject in question, with which to build the path
107 * @gfp_mask: the allocation type used to allocate the path
109 * The result must be freed by the caller with kfree().
111 char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
113 char *path;
114 int len;
116 len = get_kobj_path_length(kobj);
117 if (len == 0)
118 return NULL;
119 path = kzalloc(len, gfp_mask);
120 if (!path)
121 return NULL;
122 fill_kobj_path(kobj, path, len);
124 return path;
126 EXPORT_SYMBOL_GPL(kobject_get_path);
128 #ifdef CONFIG_X86_32
129 static int ptr_in_range(void *ptr, void *start, void *end)
132 * This should hopefully get rid of causing warnings
133 * if the architecture did not set one of the section
134 * variables up.
136 if (start >= end)
137 return 0;
139 if ((ptr >= start) && (ptr < end))
140 return 1;
141 return 0;
144 void verify_dynamic_kobject_allocation(struct kobject *kobj)
146 char *namebuf;
147 const char *ret;
149 namebuf = kzalloc(KSYM_NAME_LEN, GFP_KERNEL);
150 ret = kallsyms_lookup((unsigned long)kobj, NULL, NULL, NULL,
151 namebuf);
153 * This is the X86_32-only part of this function.
154 * This is here because it is valid to have a kobject
155 * in an __init section, but only after those
156 * sections have been freed back to the dynamic pool.
158 if (!initmem_now_dynamic &&
159 ptr_in_range(kobj, __init_begin, __init_end))
160 goto out;
161 if (!ret || !strlen(ret))
162 goto out;
163 pr_debug("---- begin silly warning ----\n");
164 pr_debug("This is a janitorial warning, not a kernel bug.\n");
165 #ifdef CONFIG_DEBUG_KOBJECT
166 pr_debug("The kobject at, or inside '%s'@(0x%p) is not dynamically allocated.\n",
167 namebuf, kobj);
168 #endif
169 pr_debug("kobjects must be dynamically allocated, not static\n");
170 /* dump_stack(); */
171 pr_debug("---- end silly warning ----\n");
172 out:
173 kfree(namebuf);
175 #else
176 static void verify_dynamic_kobject_allocation(struct kobject *kobj)
179 #endif
182 * kobject_init - initialize object.
183 * @kobj: object in question.
185 void kobject_init(struct kobject * kobj)
187 if (!kobj)
188 return;
189 WARN_ON(atomic_read(&kobj->kref.refcount));
190 verify_dynamic_kobject_allocation(kobj);
191 kref_init(&kobj->kref);
192 INIT_LIST_HEAD(&kobj->entry);
193 kobj->kset = kset_get(kobj->kset);
198 * unlink - remove kobject from kset list.
199 * @kobj: kobject.
201 * Remove the kobject from the kset list and decrement
202 * its parent's refcount.
203 * This is separated out, so we can use it in both
204 * kobject_del() and kobject_add() on error.
207 static void unlink(struct kobject * kobj)
209 if (kobj->kset) {
210 spin_lock(&kobj->kset->list_lock);
211 list_del_init(&kobj->entry);
212 spin_unlock(&kobj->kset->list_lock);
214 kobject_put(kobj);
218 * kobject_add - add an object to the hierarchy.
219 * @kobj: object.
222 int kobject_add(struct kobject * kobj)
224 int error = 0;
225 struct kobject * parent;
227 if (!(kobj = kobject_get(kobj)))
228 return -ENOENT;
229 if (!kobj->k_name)
230 kobject_set_name(kobj, "NO_NAME");
231 if (!*kobj->k_name) {
232 pr_debug("kobject attempted to be registered with no name!\n");
233 WARN_ON(1);
234 kobject_put(kobj);
235 return -EINVAL;
237 parent = kobject_get(kobj->parent);
239 pr_debug("kobject %s: registering. parent: %s, set: %s\n",
240 kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>",
241 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>" );
243 if (kobj->kset) {
244 spin_lock(&kobj->kset->list_lock);
246 if (!parent) {
247 parent = kobject_get(&kobj->kset->kobj);
249 * If the kset is our parent, get a second
250 * reference, we drop both the kset and the
251 * parent ref on cleanup
253 kobject_get(parent);
256 list_add_tail(&kobj->entry,&kobj->kset->list);
257 spin_unlock(&kobj->kset->list_lock);
258 kobj->parent = parent;
261 error = create_dir(kobj);
262 if (error) {
263 /* unlink does the kobject_put() for us */
264 unlink(kobj);
265 kobject_put(parent);
267 /* be noisy on error issues */
268 if (error == -EEXIST)
269 printk(KERN_ERR "kobject_add failed for %s with "
270 "-EEXIST, don't try to register things with "
271 "the same name in the same directory.\n",
272 kobject_name(kobj));
273 else
274 printk(KERN_ERR "kobject_add failed for %s (%d)\n",
275 kobject_name(kobj), error);
276 dump_stack();
279 return error;
283 * kobject_register - initialize and add an object.
284 * @kobj: object in question.
287 int kobject_register(struct kobject * kobj)
289 int error = -EINVAL;
290 if (kobj) {
291 kobject_init(kobj);
292 error = kobject_add(kobj);
293 if (!error)
294 kobject_uevent(kobj, KOBJ_ADD);
296 return error;
301 * kobject_set_name - Set the name of an object
302 * @kobj: object.
303 * @fmt: format string used to build the name
305 * If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated
306 * string that @kobj->k_name points to. Otherwise, use the static
307 * @kobj->name array.
309 int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
311 int error = 0;
312 int limit;
313 int need;
314 va_list args;
315 char *name;
317 /* find out how big a buffer we need */
318 name = kmalloc(1024, GFP_KERNEL);
319 if (!name) {
320 error = -ENOMEM;
321 goto done;
323 va_start(args, fmt);
324 need = vsnprintf(name, 1024, fmt, args);
325 va_end(args);
326 kfree(name);
328 /* Allocate the new space and copy the string in */
329 limit = need + 1;
330 name = kmalloc(limit, GFP_KERNEL);
331 if (!name) {
332 error = -ENOMEM;
333 goto done;
335 va_start(args, fmt);
336 need = vsnprintf(name, limit, fmt, args);
337 va_end(args);
339 /* something wrong with the string we copied? */
340 if (need >= limit) {
341 kfree(name);
342 error = -EFAULT;
343 goto done;
346 /* Free the old name, if necessary. */
347 kfree(kobj->k_name);
349 /* Now, set the new name */
350 kobj->k_name = name;
351 done:
352 return error;
354 EXPORT_SYMBOL(kobject_set_name);
357 * kobject_rename - change the name of an object
358 * @kobj: object in question.
359 * @new_name: object's new name
362 int kobject_rename(struct kobject * kobj, const char *new_name)
364 int error = 0;
365 const char *devpath = NULL;
366 char *devpath_string = NULL;
367 char *envp[2];
369 kobj = kobject_get(kobj);
370 if (!kobj)
371 return -EINVAL;
372 if (!kobj->parent)
373 return -EINVAL;
375 /* see if this name is already in use */
376 if (kobj->kset) {
377 struct kobject *temp_kobj;
378 temp_kobj = kset_find_obj(kobj->kset, new_name);
379 if (temp_kobj) {
380 printk(KERN_WARNING "kobject '%s' can not be renamed "
381 "to '%s' as '%s' is already in existance.\n",
382 kobject_name(kobj), new_name, new_name);
383 kobject_put(temp_kobj);
384 return -EINVAL;
388 devpath = kobject_get_path(kobj, GFP_KERNEL);
389 if (!devpath) {
390 error = -ENOMEM;
391 goto out;
393 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
394 if (!devpath_string) {
395 error = -ENOMEM;
396 goto out;
398 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
399 envp[0] = devpath_string;
400 envp[1] = NULL;
402 error = sysfs_rename_dir(kobj, new_name);
404 /* This function is mostly/only used for network interface.
405 * Some hotplug package track interfaces by their name and
406 * therefore want to know when the name is changed by the user. */
407 if (!error)
408 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
410 out:
411 kfree(devpath_string);
412 kfree(devpath);
413 kobject_put(kobj);
415 return error;
419 * kobject_move - move object to another parent
420 * @kobj: object in question.
421 * @new_parent: object's new parent (can be NULL)
424 int kobject_move(struct kobject *kobj, struct kobject *new_parent)
426 int error;
427 struct kobject *old_parent;
428 const char *devpath = NULL;
429 char *devpath_string = NULL;
430 char *envp[2];
432 kobj = kobject_get(kobj);
433 if (!kobj)
434 return -EINVAL;
435 new_parent = kobject_get(new_parent);
436 if (!new_parent) {
437 if (kobj->kset)
438 new_parent = kobject_get(&kobj->kset->kobj);
440 /* old object path */
441 devpath = kobject_get_path(kobj, GFP_KERNEL);
442 if (!devpath) {
443 error = -ENOMEM;
444 goto out;
446 devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
447 if (!devpath_string) {
448 error = -ENOMEM;
449 goto out;
451 sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
452 envp[0] = devpath_string;
453 envp[1] = NULL;
454 error = sysfs_move_dir(kobj, new_parent);
455 if (error)
456 goto out;
457 old_parent = kobj->parent;
458 kobj->parent = new_parent;
459 new_parent = NULL;
460 kobject_put(old_parent);
461 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
462 out:
463 kobject_put(new_parent);
464 kobject_put(kobj);
465 kfree(devpath_string);
466 kfree(devpath);
467 return error;
471 * kobject_del - unlink kobject from hierarchy.
472 * @kobj: object.
475 void kobject_del(struct kobject * kobj)
477 if (!kobj)
478 return;
479 sysfs_remove_dir(kobj);
480 unlink(kobj);
484 * kobject_unregister - remove object from hierarchy and decrement refcount.
485 * @kobj: object going away.
488 void kobject_unregister(struct kobject * kobj)
490 if (!kobj)
491 return;
492 pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
493 kobject_uevent(kobj, KOBJ_REMOVE);
494 kobject_del(kobj);
495 kobject_put(kobj);
499 * kobject_get - increment refcount for object.
500 * @kobj: object.
503 struct kobject * kobject_get(struct kobject * kobj)
505 if (kobj)
506 kref_get(&kobj->kref);
507 return kobj;
511 * kobject_cleanup - free kobject resources.
512 * @kobj: object.
515 void kobject_cleanup(struct kobject * kobj)
517 struct kobj_type * t = get_ktype(kobj);
518 struct kset * s = kobj->kset;
519 struct kobject * parent = kobj->parent;
520 const char *name = kobj->k_name;
522 pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
523 if (t && t->release) {
524 t->release(kobj);
525 /* If we have a release function, we can guess that this was
526 * not a statically allocated kobject, so we should be safe to
527 * free the name */
528 kfree(name);
529 } else {
530 pr_debug("kobject '%s' does not have a release() function, "
531 "if this is not a directory kobject, it is broken "
532 "and must be fixed.\n", name);
534 if (s)
535 kset_put(s);
536 kobject_put(parent);
539 static void kobject_release(struct kref *kref)
541 kobject_cleanup(container_of(kref, struct kobject, kref));
545 * kobject_put - decrement refcount for object.
546 * @kobj: object.
548 * Decrement the refcount, and if 0, call kobject_cleanup().
550 void kobject_put(struct kobject * kobj)
552 if (kobj)
553 kref_put(&kobj->kref, kobject_release);
556 static void dynamic_kobj_release(struct kobject *kobj)
558 pr_debug("%s: freeing %s\n", __FUNCTION__, kobject_name(kobj));
559 kfree(kobj);
562 static struct kobj_type dynamic_kobj_ktype = {
563 .release = dynamic_kobj_release,
564 .sysfs_ops = &kobj_sysfs_ops,
568 * kobject_create - create a struct kobject dynamically
570 * @name: the name for the kset
571 * @parent: the parent kobject of this kobject, if any.
573 * This function creates a kobject structure dynamically and sets it up
574 * to be a "dynamic" kobject with a default release function set up.
576 * If the kobject was not able to be created, NULL will be returned.
578 struct kobject *kobject_create(const char *name, struct kobject *parent)
580 struct kobject *kobj;
582 kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
583 if (!kobj)
584 return NULL;
586 kobject_set_name(kobj, name);
587 kobj->parent = parent;
588 kobj->ktype = &dynamic_kobj_ktype;
589 return kobj;
593 * kobject_create_and_register - create a struct kobject dynamically and register it with sysfs
595 * @name: the name for the kset
596 * @parent: the parent kobject of this kobject, if any.
598 * This function creates a kset structure dynamically and registers it
599 * with sysfs. When you are finished with this structure, call
600 * kobject_unregister() and the structure will be dynamically freed when
601 * it is no longer being used.
603 * If the kobject was not able to be created, NULL will be returned.
605 struct kobject *kobject_create_and_register(const char *name,
606 struct kobject *parent)
608 struct kobject *kobj;
609 int retval;
611 kobj = kobject_create(name, parent);
612 if (!kobj)
613 return NULL;
615 retval = kobject_register(kobj);
616 if (retval) {
617 printk(KERN_WARNING "%s: kobject_register error: %d\n",
618 __FUNCTION__, retval);
619 kfree(kobj);
620 kobj = NULL;
622 return kobj;
624 EXPORT_SYMBOL_GPL(kobject_create_and_register);
627 * kset_init - initialize a kset for use
628 * @k: kset
631 void kset_init(struct kset * k)
633 kobject_init(&k->kobj);
634 INIT_LIST_HEAD(&k->list);
635 spin_lock_init(&k->list_lock);
638 /* default kobject attribute operations */
639 static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
640 char *buf)
642 struct kobj_attribute *kattr;
643 ssize_t ret = -EIO;
645 kattr = container_of(attr, struct kobj_attribute, attr);
646 if (kattr->show)
647 ret = kattr->show(kobj, kattr, buf);
648 return ret;
651 static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
652 const char *buf, size_t count)
654 struct kobj_attribute *kattr;
655 ssize_t ret = -EIO;
657 kattr = container_of(attr, struct kobj_attribute, attr);
658 if (kattr->store)
659 ret = kattr->store(kobj, kattr, buf, count);
660 return ret;
663 struct sysfs_ops kobj_sysfs_ops = {
664 .show = kobj_attr_show,
665 .store = kobj_attr_store,
669 * kset_add - add a kset object to the hierarchy.
670 * @k: kset.
673 int kset_add(struct kset * k)
675 return kobject_add(&k->kobj);
680 * kset_register - initialize and add a kset.
681 * @k: kset.
684 int kset_register(struct kset * k)
686 int err;
688 if (!k)
689 return -EINVAL;
691 kset_init(k);
692 err = kset_add(k);
693 if (err)
694 return err;
695 kobject_uevent(&k->kobj, KOBJ_ADD);
696 return 0;
701 * kset_unregister - remove a kset.
702 * @k: kset.
705 void kset_unregister(struct kset * k)
707 if (!k)
708 return;
709 kobject_unregister(&k->kobj);
714 * kset_find_obj - search for object in kset.
715 * @kset: kset we're looking in.
716 * @name: object's name.
718 * Lock kset via @kset->subsys, and iterate over @kset->list,
719 * looking for a matching kobject. If matching object is found
720 * take a reference and return the object.
723 struct kobject * kset_find_obj(struct kset * kset, const char * name)
725 struct list_head * entry;
726 struct kobject * ret = NULL;
728 spin_lock(&kset->list_lock);
729 list_for_each(entry,&kset->list) {
730 struct kobject * k = to_kobj(entry);
731 if (kobject_name(k) && !strcmp(kobject_name(k),name)) {
732 ret = kobject_get(k);
733 break;
736 spin_unlock(&kset->list_lock);
737 return ret;
740 static void kset_release(struct kobject *kobj)
742 struct kset *kset = container_of(kobj, struct kset, kobj);
743 pr_debug("kset %s: now freed\n", kobject_name(kobj));
744 kfree(kset);
747 static struct kobj_type kset_ktype = {
748 .sysfs_ops = &kobj_sysfs_ops,
749 .release = kset_release,
753 * kset_create - create a struct kset dynamically
755 * @name: the name for the kset
756 * @uevent_ops: a struct kset_uevent_ops for the kset
757 * @parent_kobj: the parent kobject of this kset, if any.
758 * @parent_kset: the parent kset of this kset, if any.
760 * This function creates a kset structure dynamically. This structure can
761 * then be registered with the system and show up in sysfs with a call to
762 * kset_register(). When you are finished with this structure, if
763 * kset_register() has been called, call kset_unregister() and the
764 * structure will be dynamically freed when it is no longer being used.
766 * If the kset was not able to be created, NULL will be returned.
768 * NOTE, you can not have both a @parent_kobj and a @parent_kset, pick one
769 * or the other.
771 static struct kset *kset_create(const char *name,
772 struct kset_uevent_ops *uevent_ops,
773 struct kobject *parent_kobj,
774 struct kset *parent_kset)
776 struct kset *kset;
778 if ((parent_kobj) && (parent_kset)) {
779 printk(KERN_WARNING "Can not specify both a parent kset and a "
780 "parent kobject for %s\n", __FUNCTION__);
781 WARN_ON(1);
782 return NULL;
785 kset = kzalloc(sizeof(*kset), GFP_KERNEL);
786 if (!kset)
787 return NULL;
788 kobject_set_name(&kset->kobj, name);
789 kset->uevent_ops = uevent_ops;
790 kset->kobj.parent = parent_kobj;
791 kset->kobj.kset = parent_kset;
792 kset->kobj.ktype = &kset_ktype;
794 return kset;
798 * kset_create_and_register - create a struct kset dynamically and register it with sysfs
800 * @name: the name for the kset
801 * @uevent_ops: a struct kset_uevent_ops for the kset
802 * @parent_kobj: the parent kobject of this kset, if any.
803 * @parent_kset: the parent kset of this kset, if any.
805 * This function creates a kset structure dynamically and registers it
806 * with sysfs. When you are finished with this structure, call
807 * kset_unregister() and the structure will be dynamically freed when it
808 * is no longer being used.
810 * If the kset was not able to be created, NULL will be returned.
812 * NOTE, you can not have both a @parent_kobj and a @parent_kset, pick one
813 * or the other.
815 struct kset *kset_create_and_register(const char *name,
816 struct kset_uevent_ops *uevent_ops,
817 struct kobject *parent_kobj,
818 struct kset *parent_kset)
820 struct kset *kset;
821 int error;
823 kset = kset_create(name, uevent_ops, parent_kobj, parent_kset);
824 if (!kset)
825 return NULL;
826 error = kset_register(kset);
827 if (error) {
828 kfree(kset);
829 return NULL;
831 return kset;
833 EXPORT_SYMBOL_GPL(kset_create_and_register);
835 EXPORT_SYMBOL(kobject_init);
836 EXPORT_SYMBOL(kobject_register);
837 EXPORT_SYMBOL(kobject_unregister);
838 EXPORT_SYMBOL(kobject_get);
839 EXPORT_SYMBOL(kobject_put);
840 EXPORT_SYMBOL(kobject_add);
841 EXPORT_SYMBOL(kobject_del);
843 EXPORT_SYMBOL(kset_register);
844 EXPORT_SYMBOL(kset_unregister);