aboutsummaryrefslogtreecommitdiffstats
path: root/lib/kobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/kobject.c')
-rw-r--r--lib/kobject.c135
1 files changed, 49 insertions, 86 deletions
diff --git a/lib/kobject.c b/lib/kobject.c
index 4b08e0ff95c8..03d40360ff1b 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -2,6 +2,8 @@
2 * kobject.c - library routines for handling generic kernel objects 2 * kobject.c - library routines for handling generic kernel objects
3 * 3 *
4 * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org> 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.
5 * 7 *
6 * This file is released under the GPLv2. 8 * This file is released under the GPLv2.
7 * 9 *
@@ -44,11 +46,11 @@ static int populate_dir(struct kobject * kobj)
44 return error; 46 return error;
45} 47}
46 48
47static int create_dir(struct kobject *kobj, struct sysfs_dirent *shadow_parent) 49static int create_dir(struct kobject * kobj)
48{ 50{
49 int error = 0; 51 int error = 0;
50 if (kobject_name(kobj)) { 52 if (kobject_name(kobj)) {
51 error = sysfs_create_dir(kobj, shadow_parent); 53 error = sysfs_create_dir(kobj);
52 if (!error) { 54 if (!error) {
53 if ((error = populate_dir(kobj))) 55 if ((error = populate_dir(kobj)))
54 sysfs_remove_dir(kobj); 56 sysfs_remove_dir(kobj);
@@ -131,7 +133,6 @@ void kobject_init(struct kobject * kobj)
131 return; 133 return;
132 kref_init(&kobj->kref); 134 kref_init(&kobj->kref);
133 INIT_LIST_HEAD(&kobj->entry); 135 INIT_LIST_HEAD(&kobj->entry);
134 init_waitqueue_head(&kobj->poll);
135 kobj->kset = kset_get(kobj->kset); 136 kobj->kset = kset_get(kobj->kset);
136} 137}
137 138
@@ -157,12 +158,11 @@ static void unlink(struct kobject * kobj)
157} 158}
158 159
159/** 160/**
160 * kobject_shadow_add - add an object to the hierarchy. 161 * kobject_add - add an object to the hierarchy.
161 * @kobj: object. 162 * @kobj: object.
162 * @shadow_parent: sysfs directory to add to.
163 */ 163 */
164 164
165int kobject_shadow_add(struct kobject *kobj, struct sysfs_dirent *shadow_parent) 165int kobject_add(struct kobject * kobj)
166{ 166{
167 int error = 0; 167 int error = 0;
168 struct kobject * parent; 168 struct kobject * parent;
@@ -170,7 +170,7 @@ int kobject_shadow_add(struct kobject *kobj, struct sysfs_dirent *shadow_parent)
170 if (!(kobj = kobject_get(kobj))) 170 if (!(kobj = kobject_get(kobj)))
171 return -ENOENT; 171 return -ENOENT;
172 if (!kobj->k_name) 172 if (!kobj->k_name)
173 kobj->k_name = kobj->name; 173 kobject_set_name(kobj, "NO_NAME");
174 if (!*kobj->k_name) { 174 if (!*kobj->k_name) {
175 pr_debug("kobject attempted to be registered with no name!\n"); 175 pr_debug("kobject attempted to be registered with no name!\n");
176 WARN_ON(1); 176 WARN_ON(1);
@@ -181,7 +181,7 @@ int kobject_shadow_add(struct kobject *kobj, struct sysfs_dirent *shadow_parent)
181 181
182 pr_debug("kobject %s: registering. parent: %s, set: %s\n", 182 pr_debug("kobject %s: registering. parent: %s, set: %s\n",
183 kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>", 183 kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>",
184 kobj->kset ? kobj->kset->kobj.name : "<NULL>" ); 184 kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>" );
185 185
186 if (kobj->kset) { 186 if (kobj->kset) {
187 spin_lock(&kobj->kset->list_lock); 187 spin_lock(&kobj->kset->list_lock);
@@ -194,7 +194,7 @@ int kobject_shadow_add(struct kobject *kobj, struct sysfs_dirent *shadow_parent)
194 kobj->parent = parent; 194 kobj->parent = parent;
195 } 195 }
196 196
197 error = create_dir(kobj, shadow_parent); 197 error = create_dir(kobj);
198 if (error) { 198 if (error) {
199 /* unlink does the kobject_put() for us */ 199 /* unlink does the kobject_put() for us */
200 unlink(kobj); 200 unlink(kobj);
@@ -216,16 +216,6 @@ int kobject_shadow_add(struct kobject *kobj, struct sysfs_dirent *shadow_parent)
216} 216}
217 217
218/** 218/**
219 * kobject_add - add an object to the hierarchy.
220 * @kobj: object.
221 */
222int kobject_add(struct kobject * kobj)
223{
224 return kobject_shadow_add(kobj, NULL);
225}
226
227
228/**
229 * kobject_register - initialize and add an object. 219 * kobject_register - initialize and add an object.
230 * @kobj: object in question. 220 * @kobj: object in question.
231 */ 221 */
@@ -255,54 +245,50 @@ int kobject_register(struct kobject * kobj)
255int kobject_set_name(struct kobject * kobj, const char * fmt, ...) 245int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
256{ 246{
257 int error = 0; 247 int error = 0;
258 int limit = KOBJ_NAME_LEN; 248 int limit;
259 int need; 249 int need;
260 va_list args; 250 va_list args;
261 char * name; 251 char *name;
262 252
263 /* 253 /* find out how big a buffer we need */
264 * First, try the static array 254 name = kmalloc(1024, GFP_KERNEL);
265 */ 255 if (!name) {
266 va_start(args,fmt); 256 error = -ENOMEM;
267 need = vsnprintf(kobj->name,limit,fmt,args); 257 goto done;
258 }
259 va_start(args, fmt);
260 need = vsnprintf(name, 1024, fmt, args);
268 va_end(args); 261 va_end(args);
269 if (need < limit) 262 kfree(name);
270 name = kobj->name; 263
271 else { 264 /* Allocate the new space and copy the string in */
272 /* 265 limit = need + 1;
273 * Need more space? Allocate it and try again 266 name = kmalloc(limit, GFP_KERNEL);
274 */ 267 if (!name) {
275 limit = need + 1; 268 error = -ENOMEM;
276 name = kmalloc(limit,GFP_KERNEL); 269 goto done;
277 if (!name) { 270 }
278 error = -ENOMEM; 271 va_start(args, fmt);
279 goto Done; 272 need = vsnprintf(name, limit, fmt, args);
280 } 273 va_end(args);
281 va_start(args,fmt); 274
282 need = vsnprintf(name,limit,fmt,args); 275 /* something wrong with the string we copied? */
283 va_end(args); 276 if (need >= limit) {
284 277 kfree(name);
285 /* Still? Give up. */ 278 error = -EFAULT;
286 if (need >= limit) { 279 goto done;
287 kfree(name);
288 error = -EFAULT;
289 goto Done;
290 }
291 } 280 }
292 281
293 /* Free the old name, if necessary. */ 282 /* Free the old name, if necessary. */
294 if (kobj->k_name && kobj->k_name != kobj->name) 283 kfree(kobj->k_name);
295 kfree(kobj->k_name);
296 284
297 /* Now, set the new name */ 285 /* Now, set the new name */
298 kobj->k_name = name; 286 kobj->k_name = name;
299 Done: 287done:
300 return error; 288 return error;
301} 289}
302
303EXPORT_SYMBOL(kobject_set_name); 290EXPORT_SYMBOL(kobject_set_name);
304 291
305
306/** 292/**
307 * kobject_rename - change the name of an object 293 * kobject_rename - change the name of an object
308 * @kobj: object in question. 294 * @kobj: object in question.
@@ -338,7 +324,7 @@ int kobject_rename(struct kobject * kobj, const char *new_name)
338 /* Note : if we want to send the new name alone, not the full path, 324 /* Note : if we want to send the new name alone, not the full path,
339 * we could probably use kobject_name(kobj); */ 325 * we could probably use kobject_name(kobj); */
340 326
341 error = sysfs_rename_dir(kobj, kobj->parent->sd, new_name); 327 error = sysfs_rename_dir(kobj, new_name);
342 328
343 /* This function is mostly/only used for network interface. 329 /* This function is mostly/only used for network interface.
344 * Some hotplug package track interfaces by their name and 330 * Some hotplug package track interfaces by their name and
@@ -355,27 +341,6 @@ out:
355} 341}
356 342
357/** 343/**
358 * kobject_rename - change the name of an object
359 * @kobj: object in question.
360 * @new_parent: object's new parent
361 * @new_name: object's new name
362 */
363
364int kobject_shadow_rename(struct kobject *kobj,
365 struct sysfs_dirent *new_parent, const char *new_name)
366{
367 int error = 0;
368
369 kobj = kobject_get(kobj);
370 if (!kobj)
371 return -EINVAL;
372 error = sysfs_rename_dir(kobj, new_parent, new_name);
373 kobject_put(kobj);
374
375 return error;
376}
377
378/**
379 * kobject_move - move object to another parent 344 * kobject_move - move object to another parent
380 * @kobj: object in question. 345 * @kobj: object in question.
381 * @new_parent: object's new parent (can be NULL) 346 * @new_parent: object's new parent (can be NULL)
@@ -477,13 +442,16 @@ void kobject_cleanup(struct kobject * kobj)
477 struct kobj_type * t = get_ktype(kobj); 442 struct kobj_type * t = get_ktype(kobj);
478 struct kset * s = kobj->kset; 443 struct kset * s = kobj->kset;
479 struct kobject * parent = kobj->parent; 444 struct kobject * parent = kobj->parent;
445 const char *name = kobj->k_name;
480 446
481 pr_debug("kobject %s: cleaning up\n",kobject_name(kobj)); 447 pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
482 if (kobj->k_name != kobj->name) 448 if (t && t->release) {
483 kfree(kobj->k_name);
484 kobj->k_name = NULL;
485 if (t && t->release)
486 t->release(kobj); 449 t->release(kobj);
450 /* If we have a release function, we can guess that this was
451 * not a statically allocated kobject, so we should be safe to
452 * free the name */
453 kfree(name);
454 }
487 if (s) 455 if (s)
488 kset_put(s); 456 kset_put(s);
489 kobject_put(parent); 457 kobject_put(parent);
@@ -651,11 +619,6 @@ struct kobject * kset_find_obj(struct kset * kset, const char * name)
651 return ret; 619 return ret;
652} 620}
653 621
654void subsystem_init(struct kset *s)
655{
656 kset_init(s);
657}
658
659int subsystem_register(struct kset *s) 622int subsystem_register(struct kset *s)
660{ 623{
661 return kset_register(s); 624 return kset_register(s);
@@ -679,9 +642,9 @@ int subsys_create_file(struct kset *s, struct subsys_attribute *a)
679 if (!s || !a) 642 if (!s || !a)
680 return -EINVAL; 643 return -EINVAL;
681 644
682 if (subsys_get(s)) { 645 if (kset_get(s)) {
683 error = sysfs_create_file(&s->kobj, &a->attr); 646 error = sysfs_create_file(&s->kobj, &a->attr);
684 subsys_put(s); 647 kset_put(s);
685 } 648 }
686 return error; 649 return error;
687} 650}