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