kobject: add kobject_init_and_add function
[safe/jmp/linux-2.6] / lib / kobject.c
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/module.h>
18 #include <linux/stat.h>
19 #include <linux/slab.h>
20
21 /**
22  *      populate_dir - populate directory with attributes.
23  *      @kobj:  object we're working on.
24  *
25  *      Most subsystems have a set of default attributes that 
26  *      are associated with an object that registers with them.
27  *      This is a helper called during object registration that 
28  *      loops through the default attributes of the subsystem 
29  *      and creates attributes files for them in sysfs.
30  *
31  */
32
33 static int populate_dir(struct kobject * kobj)
34 {
35         struct kobj_type * t = get_ktype(kobj);
36         struct attribute * attr;
37         int error = 0;
38         int i;
39         
40         if (t && t->default_attrs) {
41                 for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
42                         if ((error = sysfs_create_file(kobj,attr)))
43                                 break;
44                 }
45         }
46         return error;
47 }
48
49 static int create_dir(struct kobject * kobj)
50 {
51         int error = 0;
52         if (kobject_name(kobj)) {
53                 error = sysfs_create_dir(kobj);
54                 if (!error) {
55                         if ((error = populate_dir(kobj)))
56                                 sysfs_remove_dir(kobj);
57                 }
58         }
59         return error;
60 }
61
62 static inline struct kobject * to_kobj(struct list_head * entry)
63 {
64         return container_of(entry,struct kobject,entry);
65 }
66
67 static int get_kobj_path_length(struct kobject *kobj)
68 {
69         int length = 1;
70         struct kobject * parent = kobj;
71
72         /* walk up the ancestors until we hit the one pointing to the 
73          * root.
74          * Add 1 to strlen for leading '/' of each level.
75          */
76         do {
77                 if (kobject_name(parent) == NULL)
78                         return 0;
79                 length += strlen(kobject_name(parent)) + 1;
80                 parent = parent->parent;
81         } while (parent);
82         return length;
83 }
84
85 static void fill_kobj_path(struct kobject *kobj, char *path, int length)
86 {
87         struct kobject * parent;
88
89         --length;
90         for (parent = kobj; parent; parent = parent->parent) {
91                 int cur = strlen(kobject_name(parent));
92                 /* back up enough to print this name with '/' */
93                 length -= cur;
94                 strncpy (path + length, kobject_name(parent), cur);
95                 *(path + --length) = '/';
96         }
97
98         pr_debug("%s: path = '%s'\n",__FUNCTION__,path);
99 }
100
101 /**
102  * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
103  *
104  * @kobj:       kobject in question, with which to build the path
105  * @gfp_mask:   the allocation type used to allocate the path
106  *
107  * The result must be freed by the caller with kfree().
108  */
109 char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
110 {
111         char *path;
112         int len;
113
114         len = get_kobj_path_length(kobj);
115         if (len == 0)
116                 return NULL;
117         path = kzalloc(len, gfp_mask);
118         if (!path)
119                 return NULL;
120         fill_kobj_path(kobj, path, len);
121
122         return path;
123 }
124 EXPORT_SYMBOL_GPL(kobject_get_path);
125
126 /**
127  *      kobject_init - initialize object.
128  *      @kobj:  object in question.
129  */
130 void kobject_init(struct kobject * kobj)
131 {
132         if (!kobj)
133                 return;
134         kref_init(&kobj->kref);
135         INIT_LIST_HEAD(&kobj->entry);
136         kobj->kset = kset_get(kobj->kset);
137 }
138
139
140 /**
141  *      unlink - remove kobject from kset list.
142  *      @kobj:  kobject.
143  *
144  *      Remove the kobject from the kset list and decrement
145  *      its parent's refcount.
146  *      This is separated out, so we can use it in both 
147  *      kobject_del() and kobject_add() on error.
148  */
149
150 static void unlink(struct kobject * kobj)
151 {
152         if (kobj->kset) {
153                 spin_lock(&kobj->kset->list_lock);
154                 list_del_init(&kobj->entry);
155                 spin_unlock(&kobj->kset->list_lock);
156         }
157         kobject_put(kobj);
158 }
159
160 /**
161  *      kobject_add - add an object to the hierarchy.
162  *      @kobj:  object.
163  */
164
165 int kobject_add(struct kobject * kobj)
166 {
167         int error = 0;
168         struct kobject * parent;
169
170         if (!(kobj = kobject_get(kobj)))
171                 return -ENOENT;
172         if (!kobj->k_name)
173                 kobject_set_name(kobj, "NO_NAME");
174         if (!*kobj->k_name) {
175                 pr_debug("kobject attempted to be registered with no name!\n");
176                 WARN_ON(1);
177                 kobject_put(kobj);
178                 return -EINVAL;
179         }
180         parent = kobject_get(kobj->parent);
181
182         pr_debug("kobject %s: registering. parent: %s, set: %s\n",
183                  kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>", 
184                  kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>" );
185
186         if (kobj->kset) {
187                 spin_lock(&kobj->kset->list_lock);
188
189                 if (!parent)
190                         parent = kobject_get(&kobj->kset->kobj);
191
192                 list_add_tail(&kobj->entry,&kobj->kset->list);
193                 spin_unlock(&kobj->kset->list_lock);
194                 kobj->parent = parent;
195         }
196
197         error = create_dir(kobj);
198         if (error) {
199                 /* unlink does the kobject_put() for us */
200                 unlink(kobj);
201                 kobject_put(parent);
202
203                 /* be noisy on error issues */
204                 if (error == -EEXIST)
205                         printk(KERN_ERR "kobject_add failed for %s with "
206                                "-EEXIST, don't try to register things with "
207                                "the same name in the same directory.\n",
208                                kobject_name(kobj));
209                 else
210                         printk(KERN_ERR "kobject_add failed for %s (%d)\n",
211                                kobject_name(kobj), error);
212                 dump_stack();
213         }
214
215         return error;
216 }
217
218 /**
219  *      kobject_register - initialize and add an object.
220  *      @kobj:  object in question.
221  */
222
223 int kobject_register(struct kobject * kobj)
224 {
225         int error = -EINVAL;
226         if (kobj) {
227                 kobject_init(kobj);
228                 error = kobject_add(kobj);
229                 if (!error)
230                         kobject_uevent(kobj, KOBJ_ADD);
231         }
232         return error;
233 }
234
235 /**
236  * kobject_set_name_vargs - Set the name of an kobject
237  * @kobj: struct kobject to set the name of
238  * @fmt: format string used to build the name
239  * @vargs: vargs to format the string.
240  */
241 static int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
242                                   va_list vargs)
243 {
244         va_list aq;
245         char *name;
246
247         va_copy(aq, vargs);
248         name = kvasprintf(GFP_KERNEL, fmt, vargs);
249         va_end(aq);
250
251         if (!name)
252                 return -ENOMEM;
253
254         /* Free the old name, if necessary. */
255         kfree(kobj->k_name);
256
257         /* Now, set the new name */
258         kobj->k_name = name;
259
260         return 0;
261 }
262
263 /**
264  * kobject_set_name - Set the name of a kobject
265  * @kobj: struct kobject to set the name of
266  * @fmt: format string used to build the name
267  *
268  * This sets the name of the kobject.  If you have already added the
269  * kobject to the system, you must call kobject_rename() in order to
270  * change the name of the kobject.
271  */
272 int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
273 {
274         va_list args;
275         int retval;
276
277         va_start(args, fmt);
278         retval = kobject_set_name_vargs(kobj, fmt, args);
279         va_end(args);
280
281         return retval;
282 }
283 EXPORT_SYMBOL(kobject_set_name);
284
285 /**
286  * kobject_init_ng - initialize a kobject structure
287  * @kobj: pointer to the kobject to initialize
288  * @ktype: pointer to the ktype for this kobject.
289  *
290  * This function will properly initialize a kobject such that it can then
291  * be passed to the kobject_add() call.
292  *
293  * After this function is called, the kobject MUST be cleaned up by a call
294  * to kobject_put(), not by a call to kfree directly to ensure that all of
295  * the memory is cleaned up properly.
296  */
297 void kobject_init_ng(struct kobject *kobj, struct kobj_type *ktype)
298 {
299         char *err_str;
300
301         if (!kobj) {
302                 err_str = "invalid kobject pointer!";
303                 goto error;
304         }
305         if (!ktype) {
306                 err_str = "must have a ktype to be initialized properly!\n";
307                 goto error;
308         }
309         if (atomic_read(&kobj->kref.refcount)) {
310                 /* do not error out as sometimes we can recover */
311                 printk(KERN_ERR "kobject: reference count is already set, "
312                        "something is seriously wrong.\n");
313                 dump_stack();
314         }
315
316         kref_init(&kobj->kref);
317         INIT_LIST_HEAD(&kobj->entry);
318         kobj->ktype = ktype;
319         return;
320
321 error:
322         printk(KERN_ERR "kobject: %s\n", err_str);
323         dump_stack();
324 }
325 EXPORT_SYMBOL(kobject_init_ng);
326
327 static int kobject_add_varg(struct kobject *kobj, struct kobject *parent,
328                             const char *fmt, va_list vargs)
329 {
330         va_list aq;
331         int retval;
332
333         va_copy(aq, vargs);
334         retval = kobject_set_name_vargs(kobj, fmt, aq);
335         va_end(aq);
336         if (retval) {
337                 printk(KERN_ERR "kobject: can not set name properly!\n");
338                 return retval;
339         }
340         kobj->parent = parent;
341         return kobject_add(kobj);
342 }
343
344 /**
345  * kobject_add_ng - the main kobject add function
346  * @kobj: the kobject to add
347  * @parent: pointer to the parent of the kobject.
348  * @fmt: format to name the kobject with.
349  *
350  * The kobject name is set and added to the kobject hierarchy in this
351  * function.
352  *
353  * If @parent is set, then the parent of the @kobj will be set to it.
354  * If @parent is NULL, then the parent of the @kobj will be set to the
355  * kobject associted with the kset assigned to this kobject.  If no kset
356  * is assigned to the kobject, then the kobject will be located in the
357  * root of the sysfs tree.
358  *
359  * If this function returns an error, kobject_put() must be called to
360  * properly clean up the memory associated with the object.
361  *
362  * If the function is successful, the only way to properly clean up the
363  * memory is with a call to kobject_del(), in which case, a call to
364  * kobject_put() is not necessary (kobject_del() does the final
365  * kobject_put() to call the release function in the ktype's release
366  * pointer.)
367  *
368  * Under no instance should the kobject that is passed to this function
369  * be directly freed with a call to kfree(), that can leak memory.
370  *
371  * Note, no uevent will be created with this call, the caller should set
372  * up all of the necessary sysfs files for the object and then call
373  * kobject_uevent() with the UEVENT_ADD parameter to ensure that
374  * userspace is properly notified of this kobject's creation.
375  */
376 int kobject_add_ng(struct kobject *kobj, struct kobject *parent,
377                    const char *fmt, ...)
378 {
379         va_list args;
380         int retval;
381
382         if (!kobj)
383                 return -EINVAL;
384
385         va_start(args, fmt);
386         retval = kobject_add_varg(kobj, parent, fmt, args);
387         va_end(args);
388
389         return retval;
390 }
391 EXPORT_SYMBOL(kobject_add_ng);
392
393 /**
394  * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
395  * @kobj: pointer to the kobject to initialize
396  * @ktype: pointer to the ktype for this kobject.
397  * @parent: pointer to the parent of this kobject.
398  * @fmt: the name of the kobject.
399  *
400  * This function combines the call to kobject_init_ng() and
401  * kobject_add_ng().  The same type of error handling after a call to
402  * kobject_add_ng() and kobject lifetime rules are the same here.
403  */
404 int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
405                          struct kobject *parent, const char *fmt, ...)
406 {
407         va_list args;
408         int retval;
409
410         kobject_init_ng(kobj, ktype);
411
412         va_start(args, fmt);
413         retval = kobject_add_varg(kobj, parent, fmt, args);
414         va_end(args);
415
416         return retval;
417 }
418 EXPORT_SYMBOL_GPL(kobject_init_and_add);
419
420 /**
421  *      kobject_rename - change the name of an object
422  *      @kobj:  object in question.
423  *      @new_name: object's new name
424  */
425
426 int kobject_rename(struct kobject * kobj, const char *new_name)
427 {
428         int error = 0;
429         const char *devpath = NULL;
430         char *devpath_string = NULL;
431         char *envp[2];
432
433         kobj = kobject_get(kobj);
434         if (!kobj)
435                 return -EINVAL;
436         if (!kobj->parent)
437                 return -EINVAL;
438
439         /* see if this name is already in use */
440         if (kobj->kset) {
441                 struct kobject *temp_kobj;
442                 temp_kobj = kset_find_obj(kobj->kset, new_name);
443                 if (temp_kobj) {
444                         printk(KERN_WARNING "kobject '%s' cannot be renamed "
445                                "to '%s' as '%s' is already in existence.\n",
446                                kobject_name(kobj), new_name, new_name);
447                         kobject_put(temp_kobj);
448                         return -EINVAL;
449                 }
450         }
451
452         devpath = kobject_get_path(kobj, GFP_KERNEL);
453         if (!devpath) {
454                 error = -ENOMEM;
455                 goto out;
456         }
457         devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
458         if (!devpath_string) {
459                 error = -ENOMEM;
460                 goto out;
461         }
462         sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
463         envp[0] = devpath_string;
464         envp[1] = NULL;
465
466         error = sysfs_rename_dir(kobj, new_name);
467
468         /* This function is mostly/only used for network interface.
469          * Some hotplug package track interfaces by their name and
470          * therefore want to know when the name is changed by the user. */
471         if (!error)
472                 kobject_uevent_env(kobj, KOBJ_MOVE, envp);
473
474 out:
475         kfree(devpath_string);
476         kfree(devpath);
477         kobject_put(kobj);
478
479         return error;
480 }
481
482 /**
483  *      kobject_move - move object to another parent
484  *      @kobj:  object in question.
485  *      @new_parent: object's new parent (can be NULL)
486  */
487
488 int kobject_move(struct kobject *kobj, struct kobject *new_parent)
489 {
490         int error;
491         struct kobject *old_parent;
492         const char *devpath = NULL;
493         char *devpath_string = NULL;
494         char *envp[2];
495
496         kobj = kobject_get(kobj);
497         if (!kobj)
498                 return -EINVAL;
499         new_parent = kobject_get(new_parent);
500         if (!new_parent) {
501                 if (kobj->kset)
502                         new_parent = kobject_get(&kobj->kset->kobj);
503         }
504         /* old object path */
505         devpath = kobject_get_path(kobj, GFP_KERNEL);
506         if (!devpath) {
507                 error = -ENOMEM;
508                 goto out;
509         }
510         devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
511         if (!devpath_string) {
512                 error = -ENOMEM;
513                 goto out;
514         }
515         sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
516         envp[0] = devpath_string;
517         envp[1] = NULL;
518         error = sysfs_move_dir(kobj, new_parent);
519         if (error)
520                 goto out;
521         old_parent = kobj->parent;
522         kobj->parent = new_parent;
523         new_parent = NULL;
524         kobject_put(old_parent);
525         kobject_uevent_env(kobj, KOBJ_MOVE, envp);
526 out:
527         kobject_put(new_parent);
528         kobject_put(kobj);
529         kfree(devpath_string);
530         kfree(devpath);
531         return error;
532 }
533
534 /**
535  *      kobject_del - unlink kobject from hierarchy.
536  *      @kobj:  object.
537  */
538
539 void kobject_del(struct kobject * kobj)
540 {
541         if (!kobj)
542                 return;
543         sysfs_remove_dir(kobj);
544         unlink(kobj);
545 }
546
547 /**
548  *      kobject_unregister - remove object from hierarchy and decrement refcount.
549  *      @kobj:  object going away.
550  */
551
552 void kobject_unregister(struct kobject * kobj)
553 {
554         if (!kobj)
555                 return;
556         pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
557         kobject_uevent(kobj, KOBJ_REMOVE);
558         kobject_del(kobj);
559         kobject_put(kobj);
560 }
561
562 /**
563  *      kobject_get - increment refcount for object.
564  *      @kobj:  object.
565  */
566
567 struct kobject * kobject_get(struct kobject * kobj)
568 {
569         if (kobj)
570                 kref_get(&kobj->kref);
571         return kobj;
572 }
573
574 /*
575  * kobject_cleanup - free kobject resources.
576  * @kobj: object to cleanup
577  */
578 static void kobject_cleanup(struct kobject *kobj)
579 {
580         struct kobj_type * t = get_ktype(kobj);
581         struct kset * s = kobj->kset;
582         struct kobject * parent = kobj->parent;
583         const char *name = kobj->k_name;
584
585         pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
586         if (t && t->release) {
587                 t->release(kobj);
588                 /* If we have a release function, we can guess that this was
589                  * not a statically allocated kobject, so we should be safe to
590                  * free the name */
591                 kfree(name);
592         }
593         if (s)
594                 kset_put(s);
595         kobject_put(parent);
596 }
597
598 static void kobject_release(struct kref *kref)
599 {
600         kobject_cleanup(container_of(kref, struct kobject, kref));
601 }
602
603 /**
604  *      kobject_put - decrement refcount for object.
605  *      @kobj:  object.
606  *
607  *      Decrement the refcount, and if 0, call kobject_cleanup().
608  */
609 void kobject_put(struct kobject * kobj)
610 {
611         if (kobj)
612                 kref_put(&kobj->kref, kobject_release);
613 }
614
615
616 static void dir_release(struct kobject *kobj)
617 {
618         kfree(kobj);
619 }
620
621 static struct kobj_type dir_ktype = {
622         .release        = dir_release,
623         .sysfs_ops      = NULL,
624         .default_attrs  = NULL,
625 };
626
627 /**
628  *      kobject_kset_add_dir - add sub directory of object.
629  *      @kset:          kset the directory is belongs to.
630  *      @parent:        object in which a directory is created.
631  *      @name:  directory name.
632  *
633  *      Add a plain directory object as child of given object.
634  */
635 struct kobject *kobject_kset_add_dir(struct kset *kset,
636                                      struct kobject *parent, const char *name)
637 {
638         struct kobject *k;
639         int ret;
640
641         if (!parent)
642                 return NULL;
643
644         k = kzalloc(sizeof(*k), GFP_KERNEL);
645         if (!k)
646                 return NULL;
647
648         k->kset = kset;
649         k->parent = parent;
650         k->ktype = &dir_ktype;
651         kobject_set_name(k, name);
652         ret = kobject_register(k);
653         if (ret < 0) {
654                 printk(KERN_WARNING "%s: kobject_register error: %d\n",
655                         __func__, ret);
656                 kobject_del(k);
657                 return NULL;
658         }
659
660         return k;
661 }
662
663 /**
664  *      kobject_add_dir - add sub directory of object.
665  *      @parent:        object in which a directory is created.
666  *      @name:  directory name.
667  *
668  *      Add a plain directory object as child of given object.
669  */
670 struct kobject *kobject_add_dir(struct kobject *parent, const char *name)
671 {
672         return kobject_kset_add_dir(NULL, parent, name);
673 }
674
675 /**
676  *      kset_init - initialize a kset for use
677  *      @k:     kset 
678  */
679
680 void kset_init(struct kset * k)
681 {
682         kobject_init(&k->kobj);
683         INIT_LIST_HEAD(&k->list);
684         spin_lock_init(&k->list_lock);
685 }
686
687
688 /**
689  *      kset_add - add a kset object to the hierarchy.
690  *      @k:     kset.
691  */
692
693 int kset_add(struct kset * k)
694 {
695         return kobject_add(&k->kobj);
696 }
697
698
699 /**
700  *      kset_register - initialize and add a kset.
701  *      @k:     kset.
702  */
703
704 int kset_register(struct kset * k)
705 {
706         int err;
707
708         if (!k)
709                 return -EINVAL;
710
711         kset_init(k);
712         err = kset_add(k);
713         if (err)
714                 return err;
715         kobject_uevent(&k->kobj, KOBJ_ADD);
716         return 0;
717 }
718
719
720 /**
721  *      kset_unregister - remove a kset.
722  *      @k:     kset.
723  */
724
725 void kset_unregister(struct kset * k)
726 {
727         if (!k)
728                 return;
729         kobject_unregister(&k->kobj);
730 }
731
732
733 /**
734  *      kset_find_obj - search for object in kset.
735  *      @kset:  kset we're looking in.
736  *      @name:  object's name.
737  *
738  *      Lock kset via @kset->subsys, and iterate over @kset->list,
739  *      looking for a matching kobject. If matching object is found
740  *      take a reference and return the object.
741  */
742
743 struct kobject * kset_find_obj(struct kset * kset, const char * name)
744 {
745         struct list_head * entry;
746         struct kobject * ret = NULL;
747
748         spin_lock(&kset->list_lock);
749         list_for_each(entry,&kset->list) {
750                 struct kobject * k = to_kobj(entry);
751                 if (kobject_name(k) && !strcmp(kobject_name(k),name)) {
752                         ret = kobject_get(k);
753                         break;
754                 }
755         }
756         spin_unlock(&kset->list_lock);
757         return ret;
758 }
759
760 int subsystem_register(struct kset *s)
761 {
762         return kset_register(s);
763 }
764
765 void subsystem_unregister(struct kset *s)
766 {
767         kset_unregister(s);
768 }
769
770 /**
771  *      subsystem_create_file - export sysfs attribute file.
772  *      @s:     subsystem.
773  *      @a:     subsystem attribute descriptor.
774  */
775
776 int subsys_create_file(struct kset *s, struct subsys_attribute *a)
777 {
778         int error = 0;
779
780         if (!s || !a)
781                 return -EINVAL;
782
783         if (kset_get(s)) {
784                 error = sysfs_create_file(&s->kobj, &a->attr);
785                 kset_put(s);
786         }
787         return error;
788 }
789
790 EXPORT_SYMBOL(kobject_init);
791 EXPORT_SYMBOL(kobject_register);
792 EXPORT_SYMBOL(kobject_unregister);
793 EXPORT_SYMBOL(kobject_get);
794 EXPORT_SYMBOL(kobject_put);
795 EXPORT_SYMBOL(kobject_add);
796 EXPORT_SYMBOL(kobject_del);
797
798 EXPORT_SYMBOL(kset_register);
799 EXPORT_SYMBOL(kset_unregister);
800
801 EXPORT_SYMBOL(subsystem_register);
802 EXPORT_SYMBOL(subsystem_unregister);
803 EXPORT_SYMBOL(subsys_create_file);