diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2008-01-25 11:34:42 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2008-01-25 11:35:13 -0500 |
commit | df8dc74e8a383eaf2d9b44b80a71ec6f0e52b42e (patch) | |
tree | bc3799a43e8b94fa84b32e37b1c124d5e4868f50 /lib/kobject.c | |
parent | 556a169dab38b5100df6f4a45b655dddd3db94c1 (diff) | |
parent | 4a3ad20ccd8f4d2a0535cf98fa83f7b561ba59a9 (diff) |
Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-2.6
This can be broken down into these major areas:
- Documentation updates (language translations and fixes, as
well as kobject and kset documenatation updates.)
- major kset/kobject/ktype rework and fixes. This cleans up the
kset and kobject and ktype relationship and architecture,
making sense of things now, and good documenation and samples
are provided for others to use. Also the attributes for
kobjects are much easier to handle now. This cleaned up a LOT
of code all through the kernel, making kobjects easier to use
if you want to.
- struct bus_type has been reworked to now handle the lifetime
rules properly, as the kobject is properly dynamic.
- struct driver has also been reworked, and now the lifetime
issues are resolved.
- the block subsystem has been converted to use struct device
now, and not "raw" kobjects. This patch has been in the -mm
tree for over a year now, and finally all the issues are
worked out with it. Older distros now properly work with new
kernels, and no userspace updates are needed at all.
- nozomi driver is added. This has also been in -mm for a long
time, and many people have asked for it to go in. It is now
in good enough shape to do so.
- lots of class_device conversions to use struct device instead.
The tree is almost all cleaned up now, only SCSI and IB is the
remaining code to fix up...
* git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-2.6: (196 commits)
Driver core: coding style fixes
Kobject: fix coding style issues in kobject c files
Kobject: fix coding style issues in kobject.h
Driver core: fix coding style issues in device.h
spi: use class iteration api
scsi: use class iteration api
rtc: use class iteration api
power supply : use class iteration api
ieee1394: use class iteration api
Driver Core: add class iteration api
Driver core: Cleanup get_device_parent() in device_add() and device_move()
UIO: constify function pointer tables
Driver Core: constify the name passed to platform_device_register_simple
driver core: fix build with SYSFS=n
sysfs: make SYSFS_DEPRECATED depend on SYSFS
Driver core: use LIST_HEAD instead of call to INIT_LIST_HEAD in __init
kobject: add sample code for how to use ksets/ktypes/kobjects
kobject: add sample code for how to use kobjects in a simple manner.
kobject: update the kobject/kset documentation
kobject: remove old, outdated documentation.
...
Diffstat (limited to 'lib/kobject.c')
-rw-r--r-- | lib/kobject.c | 734 |
1 files changed, 455 insertions, 279 deletions
diff --git a/lib/kobject.c b/lib/kobject.c index 3590f022a609..1d63ead1815e 100644 --- a/lib/kobject.c +++ b/lib/kobject.c | |||
@@ -18,58 +18,57 @@ | |||
18 | #include <linux/stat.h> | 18 | #include <linux/stat.h> |
19 | #include <linux/slab.h> | 19 | #include <linux/slab.h> |
20 | 20 | ||
21 | /** | 21 | /* |
22 | * populate_dir - populate directory with attributes. | 22 | * populate_dir - populate directory with attributes. |
23 | * @kobj: object we're working on. | 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 | * | 24 | * |
25 | * Most subsystems have a set of default attributes that are associated | ||
26 | * with an object that registers with them. This is a helper called during | ||
27 | * object registration that loops through the default attributes of the | ||
28 | * subsystem and creates attributes files for them in sysfs. | ||
31 | */ | 29 | */ |
32 | 30 | static int populate_dir(struct kobject *kobj) | |
33 | static int populate_dir(struct kobject * kobj) | ||
34 | { | 31 | { |
35 | struct kobj_type * t = get_ktype(kobj); | 32 | struct kobj_type *t = get_ktype(kobj); |
36 | struct attribute * attr; | 33 | struct attribute *attr; |
37 | int error = 0; | 34 | int error = 0; |
38 | int i; | 35 | int i; |
39 | 36 | ||
40 | if (t && t->default_attrs) { | 37 | if (t && t->default_attrs) { |
41 | for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) { | 38 | for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) { |
42 | if ((error = sysfs_create_file(kobj,attr))) | 39 | error = sysfs_create_file(kobj, attr); |
40 | if (error) | ||
43 | break; | 41 | break; |
44 | } | 42 | } |
45 | } | 43 | } |
46 | return error; | 44 | return error; |
47 | } | 45 | } |
48 | 46 | ||
49 | static int create_dir(struct kobject * kobj) | 47 | static int create_dir(struct kobject *kobj) |
50 | { | 48 | { |
51 | int error = 0; | 49 | int error = 0; |
52 | if (kobject_name(kobj)) { | 50 | if (kobject_name(kobj)) { |
53 | error = sysfs_create_dir(kobj); | 51 | error = sysfs_create_dir(kobj); |
54 | if (!error) { | 52 | if (!error) { |
55 | if ((error = populate_dir(kobj))) | 53 | error = populate_dir(kobj); |
54 | if (error) | ||
56 | sysfs_remove_dir(kobj); | 55 | sysfs_remove_dir(kobj); |
57 | } | 56 | } |
58 | } | 57 | } |
59 | return error; | 58 | return error; |
60 | } | 59 | } |
61 | 60 | ||
62 | static inline struct kobject * to_kobj(struct list_head * entry) | 61 | static inline struct kobject *to_kobj(struct list_head *entry) |
63 | { | 62 | { |
64 | return container_of(entry,struct kobject,entry); | 63 | return container_of(entry, struct kobject, entry); |
65 | } | 64 | } |
66 | 65 | ||
67 | static int get_kobj_path_length(struct kobject *kobj) | 66 | static int get_kobj_path_length(struct kobject *kobj) |
68 | { | 67 | { |
69 | int length = 1; | 68 | int length = 1; |
70 | struct kobject * parent = kobj; | 69 | struct kobject *parent = kobj; |
71 | 70 | ||
72 | /* walk up the ancestors until we hit the one pointing to the | 71 | /* walk up the ancestors until we hit the one pointing to the |
73 | * root. | 72 | * root. |
74 | * Add 1 to strlen for leading '/' of each level. | 73 | * Add 1 to strlen for leading '/' of each level. |
75 | */ | 74 | */ |
@@ -84,18 +83,19 @@ static int get_kobj_path_length(struct kobject *kobj) | |||
84 | 83 | ||
85 | static void fill_kobj_path(struct kobject *kobj, char *path, int length) | 84 | static void fill_kobj_path(struct kobject *kobj, char *path, int length) |
86 | { | 85 | { |
87 | struct kobject * parent; | 86 | struct kobject *parent; |
88 | 87 | ||
89 | --length; | 88 | --length; |
90 | for (parent = kobj; parent; parent = parent->parent) { | 89 | for (parent = kobj; parent; parent = parent->parent) { |
91 | int cur = strlen(kobject_name(parent)); | 90 | int cur = strlen(kobject_name(parent)); |
92 | /* back up enough to print this name with '/' */ | 91 | /* back up enough to print this name with '/' */ |
93 | length -= cur; | 92 | length -= cur; |
94 | strncpy (path + length, kobject_name(parent), cur); | 93 | strncpy(path + length, kobject_name(parent), cur); |
95 | *(path + --length) = '/'; | 94 | *(path + --length) = '/'; |
96 | } | 95 | } |
97 | 96 | ||
98 | pr_debug("%s: path = '%s'\n",__FUNCTION__,path); | 97 | pr_debug("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj), |
98 | kobj, __FUNCTION__, path); | ||
99 | } | 99 | } |
100 | 100 | ||
101 | /** | 101 | /** |
@@ -123,179 +123,286 @@ char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask) | |||
123 | } | 123 | } |
124 | EXPORT_SYMBOL_GPL(kobject_get_path); | 124 | EXPORT_SYMBOL_GPL(kobject_get_path); |
125 | 125 | ||
126 | /** | 126 | /* add the kobject to its kset's list */ |
127 | * kobject_init - initialize object. | 127 | static void kobj_kset_join(struct kobject *kobj) |
128 | * @kobj: object in question. | ||
129 | */ | ||
130 | void kobject_init(struct kobject * kobj) | ||
131 | { | 128 | { |
132 | if (!kobj) | 129 | if (!kobj->kset) |
133 | return; | 130 | return; |
134 | kref_init(&kobj->kref); | 131 | |
135 | INIT_LIST_HEAD(&kobj->entry); | 132 | kset_get(kobj->kset); |
136 | kobj->kset = kset_get(kobj->kset); | 133 | spin_lock(&kobj->kset->list_lock); |
134 | list_add_tail(&kobj->entry, &kobj->kset->list); | ||
135 | spin_unlock(&kobj->kset->list_lock); | ||
137 | } | 136 | } |
138 | 137 | ||
138 | /* remove the kobject from its kset's list */ | ||
139 | static void kobj_kset_leave(struct kobject *kobj) | ||
140 | { | ||
141 | if (!kobj->kset) | ||
142 | return; | ||
139 | 143 | ||
140 | /** | 144 | spin_lock(&kobj->kset->list_lock); |
141 | * unlink - remove kobject from kset list. | 145 | list_del_init(&kobj->entry); |
142 | * @kobj: kobject. | 146 | spin_unlock(&kobj->kset->list_lock); |
143 | * | 147 | kset_put(kobj->kset); |
144 | * Remove the kobject from the kset list and decrement | 148 | } |
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 | 149 | ||
150 | static void unlink(struct kobject * kobj) | 150 | static void kobject_init_internal(struct kobject *kobj) |
151 | { | 151 | { |
152 | if (kobj->kset) { | 152 | if (!kobj) |
153 | spin_lock(&kobj->kset->list_lock); | 153 | return; |
154 | list_del_init(&kobj->entry); | 154 | kref_init(&kobj->kref); |
155 | spin_unlock(&kobj->kset->list_lock); | 155 | INIT_LIST_HEAD(&kobj->entry); |
156 | } | ||
157 | kobject_put(kobj); | ||
158 | } | 156 | } |
159 | 157 | ||
160 | /** | ||
161 | * kobject_add - add an object to the hierarchy. | ||
162 | * @kobj: object. | ||
163 | */ | ||
164 | 158 | ||
165 | int kobject_add(struct kobject * kobj) | 159 | static int kobject_add_internal(struct kobject *kobj) |
166 | { | 160 | { |
167 | int error = 0; | 161 | int error = 0; |
168 | struct kobject * parent; | 162 | struct kobject *parent; |
169 | 163 | ||
170 | if (!(kobj = kobject_get(kobj))) | 164 | if (!kobj) |
171 | return -ENOENT; | 165 | return -ENOENT; |
172 | if (!kobj->k_name) | 166 | |
173 | kobject_set_name(kobj, "NO_NAME"); | 167 | if (!kobj->name || !kobj->name[0]) { |
174 | if (!*kobj->k_name) { | 168 | pr_debug("kobject: (%p): attempted to be registered with empty " |
175 | pr_debug("kobject attempted to be registered with no name!\n"); | 169 | "name!\n", kobj); |
176 | WARN_ON(1); | 170 | WARN_ON(1); |
177 | kobject_put(kobj); | ||
178 | return -EINVAL; | 171 | return -EINVAL; |
179 | } | 172 | } |
180 | parent = kobject_get(kobj->parent); | ||
181 | 173 | ||
182 | pr_debug("kobject %s: registering. parent: %s, set: %s\n", | 174 | parent = kobject_get(kobj->parent); |
183 | kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>", | ||
184 | kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>" ); | ||
185 | 175 | ||
176 | /* join kset if set, use it as parent if we do not already have one */ | ||
186 | if (kobj->kset) { | 177 | if (kobj->kset) { |
187 | spin_lock(&kobj->kset->list_lock); | ||
188 | |||
189 | if (!parent) | 178 | if (!parent) |
190 | parent = kobject_get(&kobj->kset->kobj); | 179 | parent = kobject_get(&kobj->kset->kobj); |
191 | 180 | kobj_kset_join(kobj); | |
192 | list_add_tail(&kobj->entry,&kobj->kset->list); | ||
193 | spin_unlock(&kobj->kset->list_lock); | ||
194 | kobj->parent = parent; | 181 | kobj->parent = parent; |
195 | } | 182 | } |
196 | 183 | ||
184 | pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n", | ||
185 | kobject_name(kobj), kobj, __FUNCTION__, | ||
186 | parent ? kobject_name(parent) : "<NULL>", | ||
187 | kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>"); | ||
188 | |||
197 | error = create_dir(kobj); | 189 | error = create_dir(kobj); |
198 | if (error) { | 190 | if (error) { |
199 | /* unlink does the kobject_put() for us */ | 191 | kobj_kset_leave(kobj); |
200 | unlink(kobj); | ||
201 | kobject_put(parent); | 192 | kobject_put(parent); |
193 | kobj->parent = NULL; | ||
202 | 194 | ||
203 | /* be noisy on error issues */ | 195 | /* be noisy on error issues */ |
204 | if (error == -EEXIST) | 196 | if (error == -EEXIST) |
205 | printk(KERN_ERR "kobject_add failed for %s with " | 197 | printk(KERN_ERR "%s failed for %s with " |
206 | "-EEXIST, don't try to register things with " | 198 | "-EEXIST, don't try to register things with " |
207 | "the same name in the same directory.\n", | 199 | "the same name in the same directory.\n", |
208 | kobject_name(kobj)); | 200 | __FUNCTION__, kobject_name(kobj)); |
209 | else | 201 | else |
210 | printk(KERN_ERR "kobject_add failed for %s (%d)\n", | 202 | printk(KERN_ERR "%s failed for %s (%d)\n", |
211 | kobject_name(kobj), error); | 203 | __FUNCTION__, kobject_name(kobj), error); |
212 | dump_stack(); | 204 | dump_stack(); |
213 | } | 205 | } else |
206 | kobj->state_in_sysfs = 1; | ||
214 | 207 | ||
215 | return error; | 208 | return error; |
216 | } | 209 | } |
217 | 210 | ||
218 | /** | 211 | /** |
219 | * kobject_register - initialize and add an object. | 212 | * kobject_set_name_vargs - Set the name of an kobject |
220 | * @kobj: object in question. | 213 | * @kobj: struct kobject to set the name of |
214 | * @fmt: format string used to build the name | ||
215 | * @vargs: vargs to format the string. | ||
221 | */ | 216 | */ |
222 | 217 | static int kobject_set_name_vargs(struct kobject *kobj, const char *fmt, | |
223 | int kobject_register(struct kobject * kobj) | 218 | va_list vargs) |
224 | { | 219 | { |
225 | int error = -EINVAL; | 220 | va_list aq; |
226 | if (kobj) { | 221 | char *name; |
227 | kobject_init(kobj); | 222 | |
228 | error = kobject_add(kobj); | 223 | va_copy(aq, vargs); |
229 | if (!error) | 224 | name = kvasprintf(GFP_KERNEL, fmt, vargs); |
230 | kobject_uevent(kobj, KOBJ_ADD); | 225 | va_end(aq); |
231 | } | ||
232 | return error; | ||
233 | } | ||
234 | 226 | ||
227 | if (!name) | ||
228 | return -ENOMEM; | ||
229 | |||
230 | /* Free the old name, if necessary. */ | ||
231 | kfree(kobj->name); | ||
232 | |||
233 | /* Now, set the new name */ | ||
234 | kobj->name = name; | ||
235 | |||
236 | return 0; | ||
237 | } | ||
235 | 238 | ||
236 | /** | 239 | /** |
237 | * kobject_set_name - Set the name of a kobject | 240 | * kobject_set_name - Set the name of a kobject |
238 | * @kobj: kobject to name | 241 | * @kobj: struct kobject to set the name of |
239 | * @fmt: format string used to build the name | 242 | * @fmt: format string used to build the name |
240 | * | 243 | * |
241 | * This sets the name of the kobject. If you have already added the | 244 | * This sets the name of the kobject. If you have already added the |
242 | * kobject to the system, you must call kobject_rename() in order to | 245 | * kobject to the system, you must call kobject_rename() in order to |
243 | * change the name of the kobject. | 246 | * change the name of the kobject. |
244 | */ | 247 | */ |
245 | int kobject_set_name(struct kobject * kobj, const char * fmt, ...) | 248 | int kobject_set_name(struct kobject *kobj, const char *fmt, ...) |
246 | { | 249 | { |
247 | int error = 0; | ||
248 | int limit; | ||
249 | int need; | ||
250 | va_list args; | 250 | va_list args; |
251 | char *name; | 251 | int retval; |
252 | 252 | ||
253 | /* find out how big a buffer we need */ | ||
254 | name = kmalloc(1024, GFP_KERNEL); | ||
255 | if (!name) { | ||
256 | error = -ENOMEM; | ||
257 | goto done; | ||
258 | } | ||
259 | va_start(args, fmt); | 253 | va_start(args, fmt); |
260 | need = vsnprintf(name, 1024, fmt, args); | 254 | retval = kobject_set_name_vargs(kobj, fmt, args); |
261 | va_end(args); | 255 | va_end(args); |
262 | kfree(name); | ||
263 | 256 | ||
264 | /* Allocate the new space and copy the string in */ | 257 | return retval; |
265 | limit = need + 1; | 258 | } |
266 | name = kmalloc(limit, GFP_KERNEL); | 259 | EXPORT_SYMBOL(kobject_set_name); |
267 | if (!name) { | 260 | |
268 | error = -ENOMEM; | 261 | /** |
269 | goto done; | 262 | * kobject_init - initialize a kobject structure |
263 | * @kobj: pointer to the kobject to initialize | ||
264 | * @ktype: pointer to the ktype for this kobject. | ||
265 | * | ||
266 | * This function will properly initialize a kobject such that it can then | ||
267 | * be passed to the kobject_add() call. | ||
268 | * | ||
269 | * After this function is called, the kobject MUST be cleaned up by a call | ||
270 | * to kobject_put(), not by a call to kfree directly to ensure that all of | ||
271 | * the memory is cleaned up properly. | ||
272 | */ | ||
273 | void kobject_init(struct kobject *kobj, struct kobj_type *ktype) | ||
274 | { | ||
275 | char *err_str; | ||
276 | |||
277 | if (!kobj) { | ||
278 | err_str = "invalid kobject pointer!"; | ||
279 | goto error; | ||
280 | } | ||
281 | if (!ktype) { | ||
282 | err_str = "must have a ktype to be initialized properly!\n"; | ||
283 | goto error; | ||
284 | } | ||
285 | if (kobj->state_initialized) { | ||
286 | /* do not error out as sometimes we can recover */ | ||
287 | printk(KERN_ERR "kobject (%p): tried to init an initialized " | ||
288 | "object, something is seriously wrong.\n", kobj); | ||
289 | dump_stack(); | ||
270 | } | 290 | } |
271 | va_start(args, fmt); | ||
272 | need = vsnprintf(name, limit, fmt, args); | ||
273 | va_end(args); | ||
274 | 291 | ||
275 | /* something wrong with the string we copied? */ | 292 | kref_init(&kobj->kref); |
276 | if (need >= limit) { | 293 | INIT_LIST_HEAD(&kobj->entry); |
277 | kfree(name); | 294 | kobj->ktype = ktype; |
278 | error = -EFAULT; | 295 | kobj->state_in_sysfs = 0; |
279 | goto done; | 296 | kobj->state_add_uevent_sent = 0; |
297 | kobj->state_remove_uevent_sent = 0; | ||
298 | kobj->state_initialized = 1; | ||
299 | return; | ||
300 | |||
301 | error: | ||
302 | printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str); | ||
303 | dump_stack(); | ||
304 | } | ||
305 | EXPORT_SYMBOL(kobject_init); | ||
306 | |||
307 | static int kobject_add_varg(struct kobject *kobj, struct kobject *parent, | ||
308 | const char *fmt, va_list vargs) | ||
309 | { | ||
310 | va_list aq; | ||
311 | int retval; | ||
312 | |||
313 | va_copy(aq, vargs); | ||
314 | retval = kobject_set_name_vargs(kobj, fmt, aq); | ||
315 | va_end(aq); | ||
316 | if (retval) { | ||
317 | printk(KERN_ERR "kobject: can not set name properly!\n"); | ||
318 | return retval; | ||
280 | } | 319 | } |
320 | kobj->parent = parent; | ||
321 | return kobject_add_internal(kobj); | ||
322 | } | ||
281 | 323 | ||
282 | /* Free the old name, if necessary. */ | 324 | /** |
283 | kfree(kobj->k_name); | 325 | * kobject_add - the main kobject add function |
326 | * @kobj: the kobject to add | ||
327 | * @parent: pointer to the parent of the kobject. | ||
328 | * @fmt: format to name the kobject with. | ||
329 | * | ||
330 | * The kobject name is set and added to the kobject hierarchy in this | ||
331 | * function. | ||
332 | * | ||
333 | * If @parent is set, then the parent of the @kobj will be set to it. | ||
334 | * If @parent is NULL, then the parent of the @kobj will be set to the | ||
335 | * kobject associted with the kset assigned to this kobject. If no kset | ||
336 | * is assigned to the kobject, then the kobject will be located in the | ||
337 | * root of the sysfs tree. | ||
338 | * | ||
339 | * If this function returns an error, kobject_put() must be called to | ||
340 | * properly clean up the memory associated with the object. | ||
341 | * Under no instance should the kobject that is passed to this function | ||
342 | * be directly freed with a call to kfree(), that can leak memory. | ||
343 | * | ||
344 | * Note, no "add" uevent will be created with this call, the caller should set | ||
345 | * up all of the necessary sysfs files for the object and then call | ||
346 | * kobject_uevent() with the UEVENT_ADD parameter to ensure that | ||
347 | * userspace is properly notified of this kobject's creation. | ||
348 | */ | ||
349 | int kobject_add(struct kobject *kobj, struct kobject *parent, | ||
350 | const char *fmt, ...) | ||
351 | { | ||
352 | va_list args; | ||
353 | int retval; | ||
284 | 354 | ||
285 | /* Now, set the new name */ | 355 | if (!kobj) |
286 | kobj->k_name = name; | 356 | return -EINVAL; |
287 | done: | 357 | |
288 | return error; | 358 | if (!kobj->state_initialized) { |
359 | printk(KERN_ERR "kobject '%s' (%p): tried to add an " | ||
360 | "uninitialized object, something is seriously wrong.\n", | ||
361 | kobject_name(kobj), kobj); | ||
362 | dump_stack(); | ||
363 | return -EINVAL; | ||
364 | } | ||
365 | va_start(args, fmt); | ||
366 | retval = kobject_add_varg(kobj, parent, fmt, args); | ||
367 | va_end(args); | ||
368 | |||
369 | return retval; | ||
289 | } | 370 | } |
290 | EXPORT_SYMBOL(kobject_set_name); | 371 | EXPORT_SYMBOL(kobject_add); |
291 | 372 | ||
292 | /** | 373 | /** |
293 | * kobject_rename - change the name of an object | 374 | * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy |
294 | * @kobj: object in question. | 375 | * @kobj: pointer to the kobject to initialize |
295 | * @new_name: object's new name | 376 | * @ktype: pointer to the ktype for this kobject. |
377 | * @parent: pointer to the parent of this kobject. | ||
378 | * @fmt: the name of the kobject. | ||
379 | * | ||
380 | * This function combines the call to kobject_init() and | ||
381 | * kobject_add(). The same type of error handling after a call to | ||
382 | * kobject_add() and kobject lifetime rules are the same here. | ||
296 | */ | 383 | */ |
384 | int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, | ||
385 | struct kobject *parent, const char *fmt, ...) | ||
386 | { | ||
387 | va_list args; | ||
388 | int retval; | ||
389 | |||
390 | kobject_init(kobj, ktype); | ||
391 | |||
392 | va_start(args, fmt); | ||
393 | retval = kobject_add_varg(kobj, parent, fmt, args); | ||
394 | va_end(args); | ||
395 | |||
396 | return retval; | ||
397 | } | ||
398 | EXPORT_SYMBOL_GPL(kobject_init_and_add); | ||
297 | 399 | ||
298 | int kobject_rename(struct kobject * kobj, const char *new_name) | 400 | /** |
401 | * kobject_rename - change the name of an object | ||
402 | * @kobj: object in question. | ||
403 | * @new_name: object's new name | ||
404 | */ | ||
405 | int kobject_rename(struct kobject *kobj, const char *new_name) | ||
299 | { | 406 | { |
300 | int error = 0; | 407 | int error = 0; |
301 | const char *devpath = NULL; | 408 | const char *devpath = NULL; |
@@ -334,8 +441,6 @@ int kobject_rename(struct kobject * kobj, const char *new_name) | |||
334 | sprintf(devpath_string, "DEVPATH_OLD=%s", devpath); | 441 | sprintf(devpath_string, "DEVPATH_OLD=%s", devpath); |
335 | envp[0] = devpath_string; | 442 | envp[0] = devpath_string; |
336 | envp[1] = NULL; | 443 | envp[1] = NULL; |
337 | /* Note : if we want to send the new name alone, not the full path, | ||
338 | * we could probably use kobject_name(kobj); */ | ||
339 | 444 | ||
340 | error = sysfs_rename_dir(kobj, new_name); | 445 | error = sysfs_rename_dir(kobj, new_name); |
341 | 446 | ||
@@ -354,11 +459,10 @@ out: | |||
354 | } | 459 | } |
355 | 460 | ||
356 | /** | 461 | /** |
357 | * kobject_move - move object to another parent | 462 | * kobject_move - move object to another parent |
358 | * @kobj: object in question. | 463 | * @kobj: object in question. |
359 | * @new_parent: object's new parent (can be NULL) | 464 | * @new_parent: object's new parent (can be NULL) |
360 | */ | 465 | */ |
361 | |||
362 | int kobject_move(struct kobject *kobj, struct kobject *new_parent) | 466 | int kobject_move(struct kobject *kobj, struct kobject *new_parent) |
363 | { | 467 | { |
364 | int error; | 468 | int error; |
@@ -406,68 +510,74 @@ out: | |||
406 | } | 510 | } |
407 | 511 | ||
408 | /** | 512 | /** |
409 | * kobject_del - unlink kobject from hierarchy. | 513 | * kobject_del - unlink kobject from hierarchy. |
410 | * @kobj: object. | 514 | * @kobj: object. |
411 | */ | 515 | */ |
412 | 516 | void kobject_del(struct kobject *kobj) | |
413 | void kobject_del(struct kobject * kobj) | ||
414 | { | 517 | { |
415 | if (!kobj) | 518 | if (!kobj) |
416 | return; | 519 | return; |
417 | sysfs_remove_dir(kobj); | ||
418 | unlink(kobj); | ||
419 | } | ||
420 | 520 | ||
421 | /** | 521 | sysfs_remove_dir(kobj); |
422 | * kobject_unregister - remove object from hierarchy and decrement refcount. | 522 | kobj->state_in_sysfs = 0; |
423 | * @kobj: object going away. | 523 | kobj_kset_leave(kobj); |
424 | */ | 524 | kobject_put(kobj->parent); |
425 | 525 | kobj->parent = NULL; | |
426 | void kobject_unregister(struct kobject * kobj) | ||
427 | { | ||
428 | if (!kobj) | ||
429 | return; | ||
430 | pr_debug("kobject %s: unregistering\n",kobject_name(kobj)); | ||
431 | kobject_uevent(kobj, KOBJ_REMOVE); | ||
432 | kobject_del(kobj); | ||
433 | kobject_put(kobj); | ||
434 | } | 526 | } |
435 | 527 | ||
436 | /** | 528 | /** |
437 | * kobject_get - increment refcount for object. | 529 | * kobject_get - increment refcount for object. |
438 | * @kobj: object. | 530 | * @kobj: object. |
439 | */ | 531 | */ |
440 | 532 | struct kobject *kobject_get(struct kobject *kobj) | |
441 | struct kobject * kobject_get(struct kobject * kobj) | ||
442 | { | 533 | { |
443 | if (kobj) | 534 | if (kobj) |
444 | kref_get(&kobj->kref); | 535 | kref_get(&kobj->kref); |
445 | return kobj; | 536 | return kobj; |
446 | } | 537 | } |
447 | 538 | ||
448 | /** | 539 | /* |
449 | * kobject_cleanup - free kobject resources. | 540 | * kobject_cleanup - free kobject resources. |
450 | * @kobj: object. | 541 | * @kobj: object to cleanup |
451 | */ | 542 | */ |
452 | 543 | static void kobject_cleanup(struct kobject *kobj) | |
453 | void kobject_cleanup(struct kobject * kobj) | ||
454 | { | 544 | { |
455 | struct kobj_type * t = get_ktype(kobj); | 545 | struct kobj_type *t = get_ktype(kobj); |
456 | struct kset * s = kobj->kset; | 546 | const char *name = kobj->name; |
457 | struct kobject * parent = kobj->parent; | 547 | |
458 | const char *name = kobj->k_name; | 548 | pr_debug("kobject: '%s' (%p): %s\n", |
549 | kobject_name(kobj), kobj, __FUNCTION__); | ||
550 | |||
551 | if (t && !t->release) | ||
552 | pr_debug("kobject: '%s' (%p): does not have a release() " | ||
553 | "function, it is broken and must be fixed.\n", | ||
554 | kobject_name(kobj), kobj); | ||
555 | |||
556 | /* send "remove" if the caller did not do it but sent "add" */ | ||
557 | if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) { | ||
558 | pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n", | ||
559 | kobject_name(kobj), kobj); | ||
560 | kobject_uevent(kobj, KOBJ_REMOVE); | ||
561 | } | ||
562 | |||
563 | /* remove from sysfs if the caller did not do it */ | ||
564 | if (kobj->state_in_sysfs) { | ||
565 | pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n", | ||
566 | kobject_name(kobj), kobj); | ||
567 | kobject_del(kobj); | ||
568 | } | ||
459 | 569 | ||
460 | pr_debug("kobject %s: cleaning up\n",kobject_name(kobj)); | ||
461 | if (t && t->release) { | 570 | if (t && t->release) { |
571 | pr_debug("kobject: '%s' (%p): calling ktype release\n", | ||
572 | kobject_name(kobj), kobj); | ||
462 | t->release(kobj); | 573 | t->release(kobj); |
463 | /* If we have a release function, we can guess that this was | 574 | } |
464 | * not a statically allocated kobject, so we should be safe to | 575 | |
465 | * free the name */ | 576 | /* free name if we allocated it */ |
577 | if (name) { | ||
578 | pr_debug("kobject: '%s': free name\n", name); | ||
466 | kfree(name); | 579 | kfree(name); |
467 | } | 580 | } |
468 | if (s) | ||
469 | kset_put(s); | ||
470 | kobject_put(parent); | ||
471 | } | 581 | } |
472 | 582 | ||
473 | static void kobject_release(struct kref *kref) | 583 | static void kobject_release(struct kref *kref) |
@@ -476,107 +586,130 @@ static void kobject_release(struct kref *kref) | |||
476 | } | 586 | } |
477 | 587 | ||
478 | /** | 588 | /** |
479 | * kobject_put - decrement refcount for object. | 589 | * kobject_put - decrement refcount for object. |
480 | * @kobj: object. | 590 | * @kobj: object. |
481 | * | 591 | * |
482 | * Decrement the refcount, and if 0, call kobject_cleanup(). | 592 | * Decrement the refcount, and if 0, call kobject_cleanup(). |
483 | */ | 593 | */ |
484 | void kobject_put(struct kobject * kobj) | 594 | void kobject_put(struct kobject *kobj) |
485 | { | 595 | { |
486 | if (kobj) | 596 | if (kobj) |
487 | kref_put(&kobj->kref, kobject_release); | 597 | kref_put(&kobj->kref, kobject_release); |
488 | } | 598 | } |
489 | 599 | ||
490 | 600 | static void dynamic_kobj_release(struct kobject *kobj) | |
491 | static void dir_release(struct kobject *kobj) | ||
492 | { | 601 | { |
602 | pr_debug("kobject: (%p): %s\n", kobj, __FUNCTION__); | ||
493 | kfree(kobj); | 603 | kfree(kobj); |
494 | } | 604 | } |
495 | 605 | ||
496 | static struct kobj_type dir_ktype = { | 606 | static struct kobj_type dynamic_kobj_ktype = { |
497 | .release = dir_release, | 607 | .release = dynamic_kobj_release, |
498 | .sysfs_ops = NULL, | 608 | .sysfs_ops = &kobj_sysfs_ops, |
499 | .default_attrs = NULL, | ||
500 | }; | 609 | }; |
501 | 610 | ||
502 | /** | 611 | /** |
503 | * kobject_kset_add_dir - add sub directory of object. | 612 | * kobject_create - create a struct kobject dynamically |
504 | * @kset: kset the directory is belongs to. | ||
505 | * @parent: object in which a directory is created. | ||
506 | * @name: directory name. | ||
507 | * | 613 | * |
508 | * Add a plain directory object as child of given object. | 614 | * This function creates a kobject structure dynamically and sets it up |
615 | * to be a "dynamic" kobject with a default release function set up. | ||
616 | * | ||
617 | * If the kobject was not able to be created, NULL will be returned. | ||
618 | * The kobject structure returned from here must be cleaned up with a | ||
619 | * call to kobject_put() and not kfree(), as kobject_init() has | ||
620 | * already been called on this structure. | ||
509 | */ | 621 | */ |
510 | struct kobject *kobject_kset_add_dir(struct kset *kset, | 622 | struct kobject *kobject_create(void) |
511 | struct kobject *parent, const char *name) | ||
512 | { | 623 | { |
513 | struct kobject *k; | 624 | struct kobject *kobj; |
514 | int ret; | ||
515 | |||
516 | if (!parent) | ||
517 | return NULL; | ||
518 | |||
519 | k = kzalloc(sizeof(*k), GFP_KERNEL); | ||
520 | if (!k) | ||
521 | return NULL; | ||
522 | 625 | ||
523 | k->kset = kset; | 626 | kobj = kzalloc(sizeof(*kobj), GFP_KERNEL); |
524 | k->parent = parent; | 627 | if (!kobj) |
525 | k->ktype = &dir_ktype; | ||
526 | kobject_set_name(k, name); | ||
527 | ret = kobject_register(k); | ||
528 | if (ret < 0) { | ||
529 | printk(KERN_WARNING "%s: kobject_register error: %d\n", | ||
530 | __func__, ret); | ||
531 | kobject_del(k); | ||
532 | return NULL; | 628 | return NULL; |
533 | } | ||
534 | 629 | ||
535 | return k; | 630 | kobject_init(kobj, &dynamic_kobj_ktype); |
631 | return kobj; | ||
536 | } | 632 | } |
537 | 633 | ||
538 | /** | 634 | /** |
539 | * kobject_add_dir - add sub directory of object. | 635 | * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs |
540 | * @parent: object in which a directory is created. | 636 | * |
541 | * @name: directory name. | 637 | * @name: the name for the kset |
638 | * @parent: the parent kobject of this kobject, if any. | ||
639 | * | ||
640 | * This function creates a kset structure dynamically and registers it | ||
641 | * with sysfs. When you are finished with this structure, call | ||
642 | * kobject_put() and the structure will be dynamically freed when | ||
643 | * it is no longer being used. | ||
542 | * | 644 | * |
543 | * Add a plain directory object as child of given object. | 645 | * If the kobject was not able to be created, NULL will be returned. |
544 | */ | 646 | */ |
545 | struct kobject *kobject_add_dir(struct kobject *parent, const char *name) | 647 | struct kobject *kobject_create_and_add(const char *name, struct kobject *parent) |
546 | { | 648 | { |
547 | return kobject_kset_add_dir(NULL, parent, name); | 649 | struct kobject *kobj; |
650 | int retval; | ||
651 | |||
652 | kobj = kobject_create(); | ||
653 | if (!kobj) | ||
654 | return NULL; | ||
655 | |||
656 | retval = kobject_add(kobj, parent, "%s", name); | ||
657 | if (retval) { | ||
658 | printk(KERN_WARNING "%s: kobject_add error: %d\n", | ||
659 | __FUNCTION__, retval); | ||
660 | kobject_put(kobj); | ||
661 | kobj = NULL; | ||
662 | } | ||
663 | return kobj; | ||
548 | } | 664 | } |
665 | EXPORT_SYMBOL_GPL(kobject_create_and_add); | ||
549 | 666 | ||
550 | /** | 667 | /** |
551 | * kset_init - initialize a kset for use | 668 | * kset_init - initialize a kset for use |
552 | * @k: kset | 669 | * @k: kset |
553 | */ | 670 | */ |
554 | 671 | void kset_init(struct kset *k) | |
555 | void kset_init(struct kset * k) | ||
556 | { | 672 | { |
557 | kobject_init(&k->kobj); | 673 | kobject_init_internal(&k->kobj); |
558 | INIT_LIST_HEAD(&k->list); | 674 | INIT_LIST_HEAD(&k->list); |
559 | spin_lock_init(&k->list_lock); | 675 | spin_lock_init(&k->list_lock); |
560 | } | 676 | } |
561 | 677 | ||
678 | /* default kobject attribute operations */ | ||
679 | static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr, | ||
680 | char *buf) | ||
681 | { | ||
682 | struct kobj_attribute *kattr; | ||
683 | ssize_t ret = -EIO; | ||
562 | 684 | ||
563 | /** | 685 | kattr = container_of(attr, struct kobj_attribute, attr); |
564 | * kset_add - add a kset object to the hierarchy. | 686 | if (kattr->show) |
565 | * @k: kset. | 687 | ret = kattr->show(kobj, kattr, buf); |
566 | */ | 688 | return ret; |
689 | } | ||
567 | 690 | ||
568 | int kset_add(struct kset * k) | 691 | static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr, |
692 | const char *buf, size_t count) | ||
569 | { | 693 | { |
570 | return kobject_add(&k->kobj); | 694 | struct kobj_attribute *kattr; |
695 | ssize_t ret = -EIO; | ||
696 | |||
697 | kattr = container_of(attr, struct kobj_attribute, attr); | ||
698 | if (kattr->store) | ||
699 | ret = kattr->store(kobj, kattr, buf, count); | ||
700 | return ret; | ||
571 | } | 701 | } |
572 | 702 | ||
703 | struct sysfs_ops kobj_sysfs_ops = { | ||
704 | .show = kobj_attr_show, | ||
705 | .store = kobj_attr_store, | ||
706 | }; | ||
573 | 707 | ||
574 | /** | 708 | /** |
575 | * kset_register - initialize and add a kset. | 709 | * kset_register - initialize and add a kset. |
576 | * @k: kset. | 710 | * @k: kset. |
577 | */ | 711 | */ |
578 | 712 | int kset_register(struct kset *k) | |
579 | int kset_register(struct kset * k) | ||
580 | { | 713 | { |
581 | int err; | 714 | int err; |
582 | 715 | ||
@@ -584,46 +717,42 @@ int kset_register(struct kset * k) | |||
584 | return -EINVAL; | 717 | return -EINVAL; |
585 | 718 | ||
586 | kset_init(k); | 719 | kset_init(k); |
587 | err = kset_add(k); | 720 | err = kobject_add_internal(&k->kobj); |
588 | if (err) | 721 | if (err) |
589 | return err; | 722 | return err; |
590 | kobject_uevent(&k->kobj, KOBJ_ADD); | 723 | kobject_uevent(&k->kobj, KOBJ_ADD); |
591 | return 0; | 724 | return 0; |
592 | } | 725 | } |
593 | 726 | ||
594 | |||
595 | /** | 727 | /** |
596 | * kset_unregister - remove a kset. | 728 | * kset_unregister - remove a kset. |
597 | * @k: kset. | 729 | * @k: kset. |
598 | */ | 730 | */ |
599 | 731 | void kset_unregister(struct kset *k) | |
600 | void kset_unregister(struct kset * k) | ||
601 | { | 732 | { |
602 | if (!k) | 733 | if (!k) |
603 | return; | 734 | return; |
604 | kobject_unregister(&k->kobj); | 735 | kobject_put(&k->kobj); |
605 | } | 736 | } |
606 | 737 | ||
607 | |||
608 | /** | 738 | /** |
609 | * kset_find_obj - search for object in kset. | 739 | * kset_find_obj - search for object in kset. |
610 | * @kset: kset we're looking in. | 740 | * @kset: kset we're looking in. |
611 | * @name: object's name. | 741 | * @name: object's name. |
612 | * | 742 | * |
613 | * Lock kset via @kset->subsys, and iterate over @kset->list, | 743 | * Lock kset via @kset->subsys, and iterate over @kset->list, |
614 | * looking for a matching kobject. If matching object is found | 744 | * looking for a matching kobject. If matching object is found |
615 | * take a reference and return the object. | 745 | * take a reference and return the object. |
616 | */ | 746 | */ |
617 | 747 | struct kobject *kset_find_obj(struct kset *kset, const char *name) | |
618 | struct kobject * kset_find_obj(struct kset * kset, const char * name) | ||
619 | { | 748 | { |
620 | struct list_head * entry; | 749 | struct list_head *entry; |
621 | struct kobject * ret = NULL; | 750 | struct kobject *ret = NULL; |
622 | 751 | ||
623 | spin_lock(&kset->list_lock); | 752 | spin_lock(&kset->list_lock); |
624 | list_for_each(entry,&kset->list) { | 753 | list_for_each(entry, &kset->list) { |
625 | struct kobject * k = to_kobj(entry); | 754 | struct kobject *k = to_kobj(entry); |
626 | if (kobject_name(k) && !strcmp(kobject_name(k),name)) { | 755 | if (kobject_name(k) && !strcmp(kobject_name(k), name)) { |
627 | ret = kobject_get(k); | 756 | ret = kobject_get(k); |
628 | break; | 757 | break; |
629 | } | 758 | } |
@@ -632,47 +761,94 @@ struct kobject * kset_find_obj(struct kset * kset, const char * name) | |||
632 | return ret; | 761 | return ret; |
633 | } | 762 | } |
634 | 763 | ||
635 | int subsystem_register(struct kset *s) | 764 | static void kset_release(struct kobject *kobj) |
636 | { | 765 | { |
637 | return kset_register(s); | 766 | struct kset *kset = container_of(kobj, struct kset, kobj); |
767 | pr_debug("kobject: '%s' (%p): %s\n", | ||
768 | kobject_name(kobj), kobj, __FUNCTION__); | ||
769 | kfree(kset); | ||
638 | } | 770 | } |
639 | 771 | ||
640 | void subsystem_unregister(struct kset *s) | 772 | static struct kobj_type kset_ktype = { |
773 | .sysfs_ops = &kobj_sysfs_ops, | ||
774 | .release = kset_release, | ||
775 | }; | ||
776 | |||
777 | /** | ||
778 | * kset_create - create a struct kset dynamically | ||
779 | * | ||
780 | * @name: the name for the kset | ||
781 | * @uevent_ops: a struct kset_uevent_ops for the kset | ||
782 | * @parent_kobj: the parent kobject of this kset, if any. | ||
783 | * | ||
784 | * This function creates a kset structure dynamically. This structure can | ||
785 | * then be registered with the system and show up in sysfs with a call to | ||
786 | * kset_register(). When you are finished with this structure, if | ||
787 | * kset_register() has been called, call kset_unregister() and the | ||
788 | * structure will be dynamically freed when it is no longer being used. | ||
789 | * | ||
790 | * If the kset was not able to be created, NULL will be returned. | ||
791 | */ | ||
792 | static struct kset *kset_create(const char *name, | ||
793 | struct kset_uevent_ops *uevent_ops, | ||
794 | struct kobject *parent_kobj) | ||
641 | { | 795 | { |
642 | kset_unregister(s); | 796 | struct kset *kset; |
797 | |||
798 | kset = kzalloc(sizeof(*kset), GFP_KERNEL); | ||
799 | if (!kset) | ||
800 | return NULL; | ||
801 | kobject_set_name(&kset->kobj, name); | ||
802 | kset->uevent_ops = uevent_ops; | ||
803 | kset->kobj.parent = parent_kobj; | ||
804 | |||
805 | /* | ||
806 | * The kobject of this kset will have a type of kset_ktype and belong to | ||
807 | * no kset itself. That way we can properly free it when it is | ||
808 | * finished being used. | ||
809 | */ | ||
810 | kset->kobj.ktype = &kset_ktype; | ||
811 | kset->kobj.kset = NULL; | ||
812 | |||
813 | return kset; | ||
643 | } | 814 | } |
644 | 815 | ||
645 | /** | 816 | /** |
646 | * subsystem_create_file - export sysfs attribute file. | 817 | * kset_create_and_add - create a struct kset dynamically and add it to sysfs |
647 | * @s: subsystem. | 818 | * |
648 | * @a: subsystem attribute descriptor. | 819 | * @name: the name for the kset |
820 | * @uevent_ops: a struct kset_uevent_ops for the kset | ||
821 | * @parent_kobj: the parent kobject of this kset, if any. | ||
822 | * | ||
823 | * This function creates a kset structure dynamically and registers it | ||
824 | * with sysfs. When you are finished with this structure, call | ||
825 | * kset_unregister() and the structure will be dynamically freed when it | ||
826 | * is no longer being used. | ||
827 | * | ||
828 | * If the kset was not able to be created, NULL will be returned. | ||
649 | */ | 829 | */ |
650 | 830 | struct kset *kset_create_and_add(const char *name, | |
651 | int subsys_create_file(struct kset *s, struct subsys_attribute *a) | 831 | struct kset_uevent_ops *uevent_ops, |
832 | struct kobject *parent_kobj) | ||
652 | { | 833 | { |
653 | int error = 0; | 834 | struct kset *kset; |
654 | 835 | int error; | |
655 | if (!s || !a) | ||
656 | return -EINVAL; | ||
657 | 836 | ||
658 | if (kset_get(s)) { | 837 | kset = kset_create(name, uevent_ops, parent_kobj); |
659 | error = sysfs_create_file(&s->kobj, &a->attr); | 838 | if (!kset) |
660 | kset_put(s); | 839 | return NULL; |
840 | error = kset_register(kset); | ||
841 | if (error) { | ||
842 | kfree(kset); | ||
843 | return NULL; | ||
661 | } | 844 | } |
662 | return error; | 845 | return kset; |
663 | } | 846 | } |
847 | EXPORT_SYMBOL_GPL(kset_create_and_add); | ||
664 | 848 | ||
665 | EXPORT_SYMBOL(kobject_init); | ||
666 | EXPORT_SYMBOL(kobject_register); | ||
667 | EXPORT_SYMBOL(kobject_unregister); | ||
668 | EXPORT_SYMBOL(kobject_get); | 849 | EXPORT_SYMBOL(kobject_get); |
669 | EXPORT_SYMBOL(kobject_put); | 850 | EXPORT_SYMBOL(kobject_put); |
670 | EXPORT_SYMBOL(kobject_add); | ||
671 | EXPORT_SYMBOL(kobject_del); | 851 | EXPORT_SYMBOL(kobject_del); |
672 | 852 | ||
673 | EXPORT_SYMBOL(kset_register); | 853 | EXPORT_SYMBOL(kset_register); |
674 | EXPORT_SYMBOL(kset_unregister); | 854 | EXPORT_SYMBOL(kset_unregister); |
675 | |||
676 | EXPORT_SYMBOL(subsystem_register); | ||
677 | EXPORT_SYMBOL(subsystem_unregister); | ||
678 | EXPORT_SYMBOL(subsys_create_file); | ||