Merge git://oss.sgi.com:8090/xfs/xfs-2.6
[linux-drm-fsl-dcu.git] / 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  *
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>
17 #include <linux/slab.h>
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, struct dentry *shadow_parent)
48 {
49         int error = 0;
50         if (kobject_name(kobj)) {
51                 error = sysfs_create_dir(kobj, shadow_parent);
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                 if (kobject_name(parent) == NULL)
76                         return 0;
77                 length += strlen(kobject_name(parent)) + 1;
78                 parent = parent->parent;
79         } while (parent);
80         return length;
81 }
82
83 static void fill_kobj_path(struct kobject *kobj, char *path, int length)
84 {
85         struct kobject * parent;
86
87         --length;
88         for (parent = kobj; parent; parent = parent->parent) {
89                 int cur = strlen(kobject_name(parent));
90                 /* back up enough to print this name with '/' */
91                 length -= cur;
92                 strncpy (path + length, kobject_name(parent), cur);
93                 *(path + --length) = '/';
94         }
95
96         pr_debug("%s: path = '%s'\n",__FUNCTION__,path);
97 }
98
99 /**
100  * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
101  *
102  * @kobj:       kobject in question, with which to build the path
103  * @gfp_mask:   the allocation type used to allocate the path
104  *
105  * The result must be freed by the caller with kfree().
106  */
107 char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
108 {
109         char *path;
110         int len;
111
112         len = get_kobj_path_length(kobj);
113         if (len == 0)
114                 return NULL;
115         path = kzalloc(len, gfp_mask);
116         if (!path)
117                 return NULL;
118         fill_kobj_path(kobj, path, len);
119
120         return path;
121 }
122 EXPORT_SYMBOL_GPL(kobject_get_path);
123
124 /**
125  *      kobject_init - initialize object.
126  *      @kobj:  object in question.
127  */
128 void kobject_init(struct kobject * kobj)
129 {
130         if (!kobj)
131                 return;
132         kref_init(&kobj->kref);
133         INIT_LIST_HEAD(&kobj->entry);
134         init_waitqueue_head(&kobj->poll);
135         kobj->kset = kset_get(kobj->kset);
136 }
137
138
139 /**
140  *      unlink - remove kobject from kset list.
141  *      @kobj:  kobject.
142  *
143  *      Remove the kobject from the kset list and decrement
144  *      its parent's refcount.
145  *      This is separated out, so we can use it in both 
146  *      kobject_del() and kobject_add() on error.
147  */
148
149 static void unlink(struct kobject * kobj)
150 {
151         if (kobj->kset) {
152                 spin_lock(&kobj->kset->list_lock);
153                 list_del_init(&kobj->entry);
154                 spin_unlock(&kobj->kset->list_lock);
155         }
156         kobject_put(kobj);
157 }
158
159 /**
160  *      kobject_add - add an object to the hierarchy.
161  *      @kobj:  object.
162  *      @shadow_parent: sysfs directory to add to.
163  */
164
165 int kobject_shadow_add(struct kobject * kobj, struct dentry *shadow_parent)
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                 kobj->k_name = kobj->name;
174         if (!kobj->k_name) {
175                 pr_debug("kobject attempted to be registered with no name!\n");
176                 WARN_ON(1);
177                 return -EINVAL;
178         }
179         parent = kobject_get(kobj->parent);
180
181         pr_debug("kobject %s: registering. parent: %s, set: %s\n",
182                  kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>", 
183                  kobj->kset ? kobj->kset->kobj.name : "<NULL>" );
184
185         if (kobj->kset) {
186                 spin_lock(&kobj->kset->list_lock);
187
188                 if (!parent)
189                         parent = kobject_get(&kobj->kset->kobj);
190
191                 list_add_tail(&kobj->entry,&kobj->kset->list);
192                 spin_unlock(&kobj->kset->list_lock);
193         }
194         kobj->parent = parent;
195
196         error = create_dir(kobj, shadow_parent);
197         if (error) {
198                 /* unlink does the kobject_put() for us */
199                 unlink(kobj);
200                 kobject_put(parent);
201
202                 /* be noisy on error issues */
203                 if (error == -EEXIST)
204                         printk("kobject_add failed for %s with -EEXIST, "
205                                "don't try to register things with the "
206                                "same name in the same directory.\n",
207                                kobject_name(kobj));
208                 else
209                         printk("kobject_add failed for %s (%d)\n",
210                                kobject_name(kobj), error);
211                  dump_stack();
212         }
213
214         return error;
215 }
216
217 /**
218  *      kobject_add - add an object to the hierarchy.
219  *      @kobj:  object.
220  */
221 int kobject_add(struct kobject * kobj)
222 {
223         return kobject_shadow_add(kobj, NULL);
224 }
225
226
227 /**
228  *      kobject_register - initialize and add an object.
229  *      @kobj:  object in question.
230  */
231
232 int kobject_register(struct kobject * kobj)
233 {
234         int error = -EINVAL;
235         if (kobj) {
236                 kobject_init(kobj);
237                 error = kobject_add(kobj);
238                 if (!error)
239                         kobject_uevent(kobj, KOBJ_ADD);
240         }
241         return error;
242 }
243
244
245 /**
246  *      kobject_set_name - Set the name of an object
247  *      @kobj:  object.
248  *      @fmt:   format string used to build the name
249  *
250  *      If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated
251  *      string that @kobj->k_name points to. Otherwise, use the static 
252  *      @kobj->name array.
253  */
254 int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
255 {
256         int error = 0;
257         int limit = KOBJ_NAME_LEN;
258         int need;
259         va_list args;
260         char * name;
261
262         /* 
263          * First, try the static array 
264          */
265         va_start(args,fmt);
266         need = vsnprintf(kobj->name,limit,fmt,args);
267         va_end(args);
268         if (need < limit) 
269                 name = kobj->name;
270         else {
271                 /* 
272                  * Need more space? Allocate it and try again 
273                  */
274                 limit = need + 1;
275                 name = kmalloc(limit,GFP_KERNEL);
276                 if (!name) {
277                         error = -ENOMEM;
278                         goto Done;
279                 }
280                 va_start(args,fmt);
281                 need = vsnprintf(name,limit,fmt,args);
282                 va_end(args);
283
284                 /* Still? Give up. */
285                 if (need >= limit) {
286                         kfree(name);
287                         error = -EFAULT;
288                         goto Done;
289                 }
290         }
291
292         /* Free the old name, if necessary. */
293         if (kobj->k_name && kobj->k_name != kobj->name)
294                 kfree(kobj->k_name);
295
296         /* Now, set the new name */
297         kobj->k_name = name;
298  Done:
299         return error;
300 }
301
302 EXPORT_SYMBOL(kobject_set_name);
303
304
305 /**
306  *      kobject_rename - change the name of an object
307  *      @kobj:  object in question.
308  *      @new_name: object's new name
309  */
310
311 int kobject_rename(struct kobject * kobj, const char *new_name)
312 {
313         int error = 0;
314
315         kobj = kobject_get(kobj);
316         if (!kobj)
317                 return -EINVAL;
318         if (!kobj->parent)
319                 return -EINVAL;
320         error = sysfs_rename_dir(kobj, kobj->parent->dentry, new_name);
321         kobject_put(kobj);
322
323         return error;
324 }
325
326 /**
327  *      kobject_rename - change the name of an object
328  *      @kobj:  object in question.
329  *      @new_name: object's new name
330  */
331
332 int kobject_shadow_rename(struct kobject * kobj, struct dentry *new_parent,
333                           const char *new_name)
334 {
335         int error = 0;
336
337         kobj = kobject_get(kobj);
338         if (!kobj)
339                 return -EINVAL;
340         error = sysfs_rename_dir(kobj, new_parent, new_name);
341         kobject_put(kobj);
342
343         return error;
344 }
345
346 /**
347  *      kobject_move - move object to another parent
348  *      @kobj:  object in question.
349  *      @new_parent: object's new parent (can be NULL)
350  */
351
352 int kobject_move(struct kobject *kobj, struct kobject *new_parent)
353 {
354         int error;
355         struct kobject *old_parent;
356         const char *devpath = NULL;
357         char *devpath_string = NULL;
358         char *envp[2];
359
360         kobj = kobject_get(kobj);
361         if (!kobj)
362                 return -EINVAL;
363         new_parent = kobject_get(new_parent);
364         if (!new_parent) {
365                 if (kobj->kset)
366                         new_parent = kobject_get(&kobj->kset->kobj);
367         }
368         /* old object path */
369         devpath = kobject_get_path(kobj, GFP_KERNEL);
370         if (!devpath) {
371                 error = -ENOMEM;
372                 goto out;
373         }
374         devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
375         if (!devpath_string) {
376                 error = -ENOMEM;
377                 goto out;
378         }
379         sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
380         envp[0] = devpath_string;
381         envp[1] = NULL;
382         error = sysfs_move_dir(kobj, new_parent);
383         if (error)
384                 goto out;
385         old_parent = kobj->parent;
386         kobj->parent = new_parent;
387         kobject_put(old_parent);
388         kobject_uevent_env(kobj, KOBJ_MOVE, envp);
389 out:
390         kobject_put(kobj);
391         kfree(devpath_string);
392         kfree(devpath);
393         return error;
394 }
395
396 /**
397  *      kobject_del - unlink kobject from hierarchy.
398  *      @kobj:  object.
399  */
400
401 void kobject_del(struct kobject * kobj)
402 {
403         if (!kobj)
404                 return;
405         sysfs_remove_dir(kobj);
406         unlink(kobj);
407 }
408
409 /**
410  *      kobject_unregister - remove object from hierarchy and decrement refcount.
411  *      @kobj:  object going away.
412  */
413
414 void kobject_unregister(struct kobject * kobj)
415 {
416         if (!kobj)
417                 return;
418         pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
419         kobject_uevent(kobj, KOBJ_REMOVE);
420         kobject_del(kobj);
421         kobject_put(kobj);
422 }
423
424 /**
425  *      kobject_get - increment refcount for object.
426  *      @kobj:  object.
427  */
428
429 struct kobject * kobject_get(struct kobject * kobj)
430 {
431         if (kobj)
432                 kref_get(&kobj->kref);
433         return kobj;
434 }
435
436 /**
437  *      kobject_cleanup - free kobject resources. 
438  *      @kobj:  object.
439  */
440
441 void kobject_cleanup(struct kobject * kobj)
442 {
443         struct kobj_type * t = get_ktype(kobj);
444         struct kset * s = kobj->kset;
445         struct kobject * parent = kobj->parent;
446
447         pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
448         if (kobj->k_name != kobj->name)
449                 kfree(kobj->k_name);
450         kobj->k_name = NULL;
451         if (t && t->release)
452                 t->release(kobj);
453         if (s)
454                 kset_put(s);
455         kobject_put(parent);
456 }
457
458 static void kobject_release(struct kref *kref)
459 {
460         kobject_cleanup(container_of(kref, struct kobject, kref));
461 }
462
463 /**
464  *      kobject_put - decrement refcount for object.
465  *      @kobj:  object.
466  *
467  *      Decrement the refcount, and if 0, call kobject_cleanup().
468  */
469 void kobject_put(struct kobject * kobj)
470 {
471         if (kobj)
472                 kref_put(&kobj->kref, kobject_release);
473 }
474
475
476 static void dir_release(struct kobject *kobj)
477 {
478         kfree(kobj);
479 }
480
481 static struct kobj_type dir_ktype = {
482         .release        = dir_release,
483         .sysfs_ops      = NULL,
484         .default_attrs  = NULL,
485 };
486
487 /**
488  *      kobject_add_dir - add sub directory of object.
489  *      @parent:        object in which a directory is created.
490  *      @name:  directory name.
491  *
492  *      Add a plain directory object as child of given object.
493  */
494 struct kobject *kobject_add_dir(struct kobject *parent, const char *name)
495 {
496         struct kobject *k;
497         int ret;
498
499         if (!parent)
500                 return NULL;
501
502         k = kzalloc(sizeof(*k), GFP_KERNEL);
503         if (!k)
504                 return NULL;
505
506         k->parent = parent;
507         k->ktype = &dir_ktype;
508         kobject_set_name(k, name);
509         ret = kobject_register(k);
510         if (ret < 0) {
511                 printk(KERN_WARNING "kobject_add_dir: "
512                         "kobject_register error: %d\n", ret);
513                 kobject_del(k);
514                 return NULL;
515         }
516
517         return k;
518 }
519
520 /**
521  *      kset_init - initialize a kset for use
522  *      @k:     kset 
523  */
524
525 void kset_init(struct kset * k)
526 {
527         kobject_init(&k->kobj);
528         INIT_LIST_HEAD(&k->list);
529         spin_lock_init(&k->list_lock);
530 }
531
532
533 /**
534  *      kset_add - add a kset object to the hierarchy.
535  *      @k:     kset.
536  *
537  *      Simply, this adds the kset's embedded kobject to the 
538  *      hierarchy. 
539  *      We also try to make sure that the kset's embedded kobject
540  *      has a parent before it is added. We only care if the embedded
541  *      kobject is not part of a kset itself, since kobject_add()
542  *      assigns a parent in that case. 
543  *      If that is the case, and the kset has a controlling subsystem,
544  *      then we set the kset's parent to be said subsystem. 
545  */
546
547 int kset_add(struct kset * k)
548 {
549         if (!k->kobj.parent && !k->kobj.kset && k->subsys)
550                 k->kobj.parent = &k->subsys->kset.kobj;
551
552         return kobject_add(&k->kobj);
553 }
554
555
556 /**
557  *      kset_register - initialize and add a kset.
558  *      @k:     kset.
559  */
560
561 int kset_register(struct kset * k)
562 {
563         if (!k)
564                 return -EINVAL;
565         kset_init(k);
566         return kset_add(k);
567 }
568
569
570 /**
571  *      kset_unregister - remove a kset.
572  *      @k:     kset.
573  */
574
575 void kset_unregister(struct kset * k)
576 {
577         if (!k)
578                 return;
579         kobject_unregister(&k->kobj);
580 }
581
582
583 /**
584  *      kset_find_obj - search for object in kset.
585  *      @kset:  kset we're looking in.
586  *      @name:  object's name.
587  *
588  *      Lock kset via @kset->subsys, and iterate over @kset->list,
589  *      looking for a matching kobject. If matching object is found
590  *      take a reference and return the object.
591  */
592
593 struct kobject * kset_find_obj(struct kset * kset, const char * name)
594 {
595         struct list_head * entry;
596         struct kobject * ret = NULL;
597
598         spin_lock(&kset->list_lock);
599         list_for_each(entry,&kset->list) {
600                 struct kobject * k = to_kobj(entry);
601                 if (kobject_name(k) && !strcmp(kobject_name(k),name)) {
602                         ret = kobject_get(k);
603                         break;
604                 }
605         }
606         spin_unlock(&kset->list_lock);
607         return ret;
608 }
609
610
611 void subsystem_init(struct subsystem * s)
612 {
613         init_rwsem(&s->rwsem);
614         kset_init(&s->kset);
615 }
616
617 /**
618  *      subsystem_register - register a subsystem.
619  *      @s:     the subsystem we're registering.
620  *
621  *      Once we register the subsystem, we want to make sure that 
622  *      the kset points back to this subsystem for correct usage of 
623  *      the rwsem. 
624  */
625
626 int subsystem_register(struct subsystem * s)
627 {
628         int error;
629
630         if (!s)
631                 return -EINVAL;
632
633         subsystem_init(s);
634         pr_debug("subsystem %s: registering\n",s->kset.kobj.name);
635
636         if (!(error = kset_add(&s->kset))) {
637                 if (!s->kset.subsys)
638                         s->kset.subsys = s;
639         }
640         return error;
641 }
642
643 void subsystem_unregister(struct subsystem * s)
644 {
645         if (!s)
646                 return;
647         pr_debug("subsystem %s: unregistering\n",s->kset.kobj.name);
648         kset_unregister(&s->kset);
649 }
650
651
652 /**
653  *      subsystem_create_file - export sysfs attribute file.
654  *      @s:     subsystem.
655  *      @a:     subsystem attribute descriptor.
656  */
657
658 int subsys_create_file(struct subsystem * s, struct subsys_attribute * a)
659 {
660         int error = 0;
661
662         if (!s || !a)
663                 return -EINVAL;
664
665         if (subsys_get(s)) {
666                 error = sysfs_create_file(&s->kset.kobj,&a->attr);
667                 subsys_put(s);
668         }
669         return error;
670 }
671
672
673 /**
674  *      subsystem_remove_file - remove sysfs attribute file.
675  *      @s:     subsystem.
676  *      @a:     attribute desciptor.
677  */
678 #if 0
679 void subsys_remove_file(struct subsystem * s, struct subsys_attribute * a)
680 {
681         if (subsys_get(s)) {
682                 sysfs_remove_file(&s->kset.kobj,&a->attr);
683                 subsys_put(s);
684         }
685 }
686 #endif  /*  0  */
687
688 EXPORT_SYMBOL(kobject_init);
689 EXPORT_SYMBOL(kobject_register);
690 EXPORT_SYMBOL(kobject_unregister);
691 EXPORT_SYMBOL(kobject_get);
692 EXPORT_SYMBOL(kobject_put);
693 EXPORT_SYMBOL(kobject_add);
694 EXPORT_SYMBOL(kobject_del);
695
696 EXPORT_SYMBOL(kset_register);
697 EXPORT_SYMBOL(kset_unregister);
698
699 EXPORT_SYMBOL(subsystem_register);
700 EXPORT_SYMBOL(subsystem_unregister);
701 EXPORT_SYMBOL(subsys_create_file);