vserver 1.9.3
[linux-2.6.git] / include / linux / kobject.h
1 /*
2  * kobject.h - generic kernel object infrastructure.
3  *
4  * Copyright (c) 2002-2003      Patrick Mochel
5  * Copyright (c) 2002-2003      Open Source Development Labs
6  *
7  * This file is released under the GPLv2.
8  *
9  * 
10  * Please read Documentation/kobject.txt before using the kobject
11  * interface, ESPECIALLY the parts about reference counts and object
12  * destructors. 
13  */
14
15 #ifndef _KOBJECT_H_
16 #define _KOBJECT_H_
17
18 #ifdef __KERNEL__
19
20 #include <linux/types.h>
21 #include <linux/list.h>
22 #include <linux/sysfs.h>
23 #include <linux/rwsem.h>
24 #include <linux/kref.h>
25 #include <asm/atomic.h>
26
27 #define KOBJ_NAME_LEN   20
28
29 struct kobject {
30         char                    * k_name;
31         char                    name[KOBJ_NAME_LEN];
32         struct kref             kref;
33         struct list_head        entry;
34         struct kobject          * parent;
35         struct kset             * kset;
36         struct kobj_type        * ktype;
37         struct dentry           * dentry;
38 };
39
40 extern int kobject_set_name(struct kobject *, const char *, ...)
41         __attribute__((format(printf,2,3)));
42
43 static inline char * kobject_name(struct kobject * kobj)
44 {
45         return kobj->k_name;
46 }
47
48 extern void kobject_init(struct kobject *);
49 extern void kobject_cleanup(struct kobject *);
50
51 extern int kobject_add(struct kobject *);
52 extern void kobject_del(struct kobject *);
53
54 extern int kobject_rename(struct kobject *, char *new_name);
55
56 extern int kobject_register(struct kobject *);
57 extern void kobject_unregister(struct kobject *);
58
59 extern struct kobject * kobject_get(struct kobject *);
60 extern void kobject_put(struct kobject *);
61
62 extern void kobject_hotplug(const char *action, struct kobject *);
63
64 extern char * kobject_get_path(struct kset *, struct kobject *, int);
65
66 struct kobj_type {
67         void (*release)(struct kobject *);
68         struct sysfs_ops        * sysfs_ops;
69         struct attribute        ** default_attrs;
70 };
71
72
73 /**
74  *      kset - a set of kobjects of a specific type, belonging
75  *      to a specific subsystem.
76  *
77  *      All kobjects of a kset should be embedded in an identical 
78  *      type. This type may have a descriptor, which the kset points
79  *      to. This allows there to exist sets of objects of the same
80  *      type in different subsystems.
81  *
82  *      A subsystem does not have to be a list of only one type 
83  *      of object; multiple ksets can belong to one subsystem. All 
84  *      ksets of a subsystem share the subsystem's lock.
85  *
86  *      Each kset can support hotplugging; if it does, it will be given
87  *      the opportunity to filter out specific kobjects from being
88  *      reported, as well as to add its own "data" elements to the
89  *      environment being passed to the hotplug helper.
90  */
91 struct kset_hotplug_ops {
92         int (*filter)(struct kset *kset, struct kobject *kobj);
93         char *(*name)(struct kset *kset, struct kobject *kobj);
94         int (*hotplug)(struct kset *kset, struct kobject *kobj, char **envp,
95                         int num_envp, char *buffer, int buffer_size);
96 };
97
98 struct kset {
99         struct subsystem        * subsys;
100         struct kobj_type        * ktype;
101         struct list_head        list;
102         struct kobject          kobj;
103         struct kset_hotplug_ops * hotplug_ops;
104 };
105
106
107 extern void kset_init(struct kset * k);
108 extern int kset_add(struct kset * k);
109 extern int kset_register(struct kset * k);
110 extern void kset_unregister(struct kset * k);
111
112 static inline struct kset * to_kset(struct kobject * kobj)
113 {
114         return kobj ? container_of(kobj,struct kset,kobj) : NULL;
115 }
116
117 static inline struct kset * kset_get(struct kset * k)
118 {
119         return k ? to_kset(kobject_get(&k->kobj)) : NULL;
120 }
121
122 static inline void kset_put(struct kset * k)
123 {
124         kobject_put(&k->kobj);
125 }
126
127 static inline struct kobj_type * get_ktype(struct kobject * k)
128 {
129         if (k->kset && k->kset->ktype)
130                 return k->kset->ktype;
131         else 
132                 return k->ktype;
133 }
134
135 extern struct kobject * kset_find_obj(struct kset *, const char *);
136
137
138 /**
139  * Use this when initializing an embedded kset with no other 
140  * fields to initialize.
141  */
142 #define set_kset_name(str)      .kset = { .kobj = { .name = str } }
143
144
145
146 struct subsystem {
147         struct kset             kset;
148         struct rw_semaphore     rwsem;
149 };
150
151 #define decl_subsys(_name,_type,_hotplug_ops) \
152 struct subsystem _name##_subsys = { \
153         .kset = { \
154                 .kobj = { .name = __stringify(_name) }, \
155                 .ktype = _type, \
156                 .hotplug_ops =_hotplug_ops, \
157         } \
158 }
159 #define decl_subsys_name(_varname,_name,_type,_hotplug_ops) \
160 struct subsystem _varname##_subsys = { \
161         .kset = { \
162                 .kobj = { .name = __stringify(_name) }, \
163                 .ktype = _type, \
164                 .hotplug_ops =_hotplug_ops, \
165         } \
166 }
167
168
169 /**
170  * Helpers for setting the kset of registered objects.
171  * Often, a registered object belongs to a kset embedded in a 
172  * subsystem. These do no magic, just make the resulting code
173  * easier to follow. 
174  */
175
176 /**
177  *      kobj_set_kset_s(obj,subsys) - set kset for embedded kobject.
178  *      @obj:           ptr to some object type.
179  *      @subsys:        a subsystem object (not a ptr).
180  *
181  *      Can be used for any object type with an embedded ->kobj.
182  */
183
184 #define kobj_set_kset_s(obj,subsys) \
185         (obj)->kobj.kset = &(subsys).kset
186
187 /**
188  *      kset_set_kset_s(obj,subsys) - set kset for embedded kset.
189  *      @obj:           ptr to some object type.
190  *      @subsys:        a subsystem object (not a ptr).
191  *
192  *      Can be used for any object type with an embedded ->kset.
193  *      Sets the kset of @obj's  embedded kobject (via its embedded
194  *      kset) to @subsys.kset. This makes @obj a member of that 
195  *      kset.
196  */
197
198 #define kset_set_kset_s(obj,subsys) \
199         (obj)->kset.kobj.kset = &(subsys).kset
200
201 /**
202  *      subsys_set_kset(obj,subsys) - set kset for subsystem
203  *      @obj:           ptr to some object type.
204  *      @subsys:        a subsystem object (not a ptr).
205  *
206  *      Can be used for any object type with an embedded ->subsys.
207  *      Sets the kset of @obj's kobject to @subsys.kset. This makes
208  *      the object a member of that kset.
209  */
210
211 #define subsys_set_kset(obj,_subsys) \
212         (obj)->subsys.kset.kobj.kset = &(_subsys).kset
213
214 extern void subsystem_init(struct subsystem *);
215 extern int subsystem_register(struct subsystem *);
216 extern void subsystem_unregister(struct subsystem *);
217
218 static inline struct subsystem * subsys_get(struct subsystem * s)
219 {
220         return s ? container_of(kset_get(&s->kset),struct subsystem,kset) : NULL;
221 }
222
223 static inline void subsys_put(struct subsystem * s)
224 {
225         kset_put(&s->kset);
226 }
227
228 struct subsys_attribute {
229         struct attribute attr;
230         ssize_t (*show)(struct subsystem *, char *);
231         ssize_t (*store)(struct subsystem *, const char *, size_t); 
232 };
233
234 extern int subsys_create_file(struct subsystem * , struct subsys_attribute *);
235 extern void subsys_remove_file(struct subsystem * , struct subsys_attribute *);
236
237 #endif /* __KERNEL__ */
238 #endif /* _KOBJECT_H_ */