kobject.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. #include <linux/kobject.h>
  13. #include <linux/string.h>
  14. #include <linux/module.h>
  15. #include <linux/stat.h>
  16. #include <linux/slab.h>
  17. /**
  18. * populate_dir - populate directory with attributes.
  19. * @kobj: object we're working on.
  20. *
  21. * Most subsystems have a set of default attributes that
  22. * are associated with an object that registers with them.
  23. * This is a helper called during object registration that
  24. * loops through the default attributes of the subsystem
  25. * and creates attributes files for them in sysfs.
  26. *
  27. */
  28. static int populate_dir(struct kobject * kobj)
  29. {
  30. struct kobj_type * t = get_ktype(kobj);
  31. struct attribute * attr;
  32. int error = 0;
  33. int i;
  34. if (t && t->default_attrs) {
  35. for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
  36. if ((error = sysfs_create_file(kobj,attr)))
  37. break;
  38. }
  39. }
  40. return error;
  41. }
  42. static int create_dir(struct kobject * kobj, struct dentry *shadow_parent)
  43. {
  44. int error = 0;
  45. if (kobject_name(kobj)) {
  46. error = sysfs_create_dir(kobj, shadow_parent);
  47. if (!error) {
  48. if ((error = populate_dir(kobj)))
  49. sysfs_remove_dir(kobj);
  50. }
  51. }
  52. return error;
  53. }
  54. static inline struct kobject * to_kobj(struct list_head * entry)
  55. {
  56. return container_of(entry,struct kobject,entry);
  57. }
  58. static int get_kobj_path_length(struct kobject *kobj)
  59. {
  60. int length = 1;
  61. struct kobject * parent = kobj;
  62. /* walk up the ancestors until we hit the one pointing to the
  63. * root.
  64. * Add 1 to strlen for leading '/' of each level.
  65. */
  66. do {
  67. if (kobject_name(parent) == NULL)
  68. return 0;
  69. length += strlen(kobject_name(parent)) + 1;
  70. parent = parent->parent;
  71. } while (parent);
  72. return length;
  73. }
  74. static void fill_kobj_path(struct kobject *kobj, char *path, int length)
  75. {
  76. struct kobject * parent;
  77. --length;
  78. for (parent = kobj; parent; parent = parent->parent) {
  79. int cur = strlen(kobject_name(parent));
  80. /* back up enough to print this name with '/' */
  81. length -= cur;
  82. strncpy (path + length, kobject_name(parent), cur);
  83. *(path + --length) = '/';
  84. }
  85. pr_debug("%s: path = '%s'\n",__FUNCTION__,path);
  86. }
  87. /**
  88. * kobject_get_path - generate and return the path associated with a given kobj
  89. * and kset pair. The result must be freed by the caller with kfree().
  90. *
  91. * @kobj: kobject in question, with which to build the path
  92. * @gfp_mask: the allocation type used to allocate the path
  93. */
  94. char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
  95. {
  96. char *path;
  97. int len;
  98. len = get_kobj_path_length(kobj);
  99. if (len == 0)
  100. return NULL;
  101. path = kzalloc(len, gfp_mask);
  102. if (!path)
  103. return NULL;
  104. fill_kobj_path(kobj, path, len);
  105. return path;
  106. }
  107. EXPORT_SYMBOL_GPL(kobject_get_path);
  108. /**
  109. * kobject_init - initialize object.
  110. * @kobj: object in question.
  111. */
  112. void kobject_init(struct kobject * kobj)
  113. {
  114. if (!kobj)
  115. return;
  116. kref_init(&kobj->kref);
  117. INIT_LIST_HEAD(&kobj->entry);
  118. init_waitqueue_head(&kobj->poll);
  119. kobj->kset = kset_get(kobj->kset);
  120. }
  121. /**
  122. * unlink - remove kobject from kset list.
  123. * @kobj: kobject.
  124. *
  125. * Remove the kobject from the kset list and decrement
  126. * its parent's refcount.
  127. * This is separated out, so we can use it in both
  128. * kobject_del() and kobject_add() on error.
  129. */
  130. static void unlink(struct kobject * kobj)
  131. {
  132. if (kobj->kset) {
  133. spin_lock(&kobj->kset->list_lock);
  134. list_del_init(&kobj->entry);
  135. spin_unlock(&kobj->kset->list_lock);
  136. }
  137. kobject_put(kobj);
  138. }
  139. /**
  140. * kobject_add - add an object to the hierarchy.
  141. * @kobj: object.
  142. * @shadow_parent: sysfs directory to add to.
  143. */
  144. int kobject_shadow_add(struct kobject * kobj, struct dentry *shadow_parent)
  145. {
  146. int error = 0;
  147. struct kobject * parent;
  148. if (!(kobj = kobject_get(kobj)))
  149. return -ENOENT;
  150. if (!kobj->k_name)
  151. kobj->k_name = kobj->name;
  152. if (!kobj->k_name) {
  153. pr_debug("kobject attempted to be registered with no name!\n");
  154. WARN_ON(1);
  155. return -EINVAL;
  156. }
  157. parent = kobject_get(kobj->parent);
  158. pr_debug("kobject %s: registering. parent: %s, set: %s\n",
  159. kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>",
  160. kobj->kset ? kobj->kset->kobj.name : "<NULL>" );
  161. if (kobj->kset) {
  162. spin_lock(&kobj->kset->list_lock);
  163. if (!parent)
  164. parent = kobject_get(&kobj->kset->kobj);
  165. list_add_tail(&kobj->entry,&kobj->kset->list);
  166. spin_unlock(&kobj->kset->list_lock);
  167. }
  168. kobj->parent = parent;
  169. error = create_dir(kobj, shadow_parent);
  170. if (error) {
  171. /* unlink does the kobject_put() for us */
  172. unlink(kobj);
  173. kobject_put(parent);
  174. /* be noisy on error issues */
  175. if (error == -EEXIST)
  176. printk("kobject_add failed for %s with -EEXIST, "
  177. "don't try to register things with the "
  178. "same name in the same directory.\n",
  179. kobject_name(kobj));
  180. else
  181. printk("kobject_add failed for %s (%d)\n",
  182. kobject_name(kobj), error);
  183. dump_stack();
  184. }
  185. return error;
  186. }
  187. /**
  188. * kobject_add - add an object to the hierarchy.
  189. * @kobj: object.
  190. */
  191. int kobject_add(struct kobject * kobj)
  192. {
  193. return kobject_shadow_add(kobj, NULL);
  194. }
  195. /**
  196. * kobject_register - initialize and add an object.
  197. * @kobj: object in question.
  198. */
  199. int kobject_register(struct kobject * kobj)
  200. {
  201. int error = -EINVAL;
  202. if (kobj) {
  203. kobject_init(kobj);
  204. error = kobject_add(kobj);
  205. if (!error)
  206. kobject_uevent(kobj, KOBJ_ADD);
  207. }
  208. return error;
  209. }
  210. /**
  211. * kobject_set_name - Set the name of an object
  212. * @kobj: object.
  213. * @fmt: format string used to build the name
  214. *
  215. * If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated
  216. * string that @kobj->k_name points to. Otherwise, use the static
  217. * @kobj->name array.
  218. */
  219. int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
  220. {
  221. int error = 0;
  222. int limit = KOBJ_NAME_LEN;
  223. int need;
  224. va_list args;
  225. char * name;
  226. /*
  227. * First, try the static array
  228. */
  229. va_start(args,fmt);
  230. need = vsnprintf(kobj->name,limit,fmt,args);
  231. va_end(args);
  232. if (need < limit)
  233. name = kobj->name;
  234. else {
  235. /*
  236. * Need more space? Allocate it and try again
  237. */
  238. limit = need + 1;
  239. name = kmalloc(limit,GFP_KERNEL);
  240. if (!name) {
  241. error = -ENOMEM;
  242. goto Done;
  243. }
  244. va_start(args,fmt);
  245. need = vsnprintf(name,limit,fmt,args);
  246. va_end(args);
  247. /* Still? Give up. */
  248. if (need >= limit) {
  249. kfree(name);
  250. error = -EFAULT;
  251. goto Done;
  252. }
  253. }
  254. /* Free the old name, if necessary. */
  255. if (kobj->k_name && kobj->k_name != kobj->name)
  256. kfree(kobj->k_name);
  257. /* Now, set the new name */
  258. kobj->k_name = name;
  259. Done:
  260. return error;
  261. }
  262. EXPORT_SYMBOL(kobject_set_name);
  263. /**
  264. * kobject_rename - change the name of an object
  265. * @kobj: object in question.
  266. * @new_name: object's new name
  267. */
  268. int kobject_rename(struct kobject * kobj, const char *new_name)
  269. {
  270. int error = 0;
  271. kobj = kobject_get(kobj);
  272. if (!kobj)
  273. return -EINVAL;
  274. if (!kobj->parent)
  275. return -EINVAL;
  276. error = sysfs_rename_dir(kobj, kobj->parent->dentry, new_name);
  277. kobject_put(kobj);
  278. return error;
  279. }
  280. /**
  281. * kobject_rename - change the name of an object
  282. * @kobj: object in question.
  283. * @new_name: object's new name
  284. */
  285. int kobject_shadow_rename(struct kobject * kobj, struct dentry *new_parent,
  286. const char *new_name)
  287. {
  288. int error = 0;
  289. kobj = kobject_get(kobj);
  290. if (!kobj)
  291. return -EINVAL;
  292. error = sysfs_rename_dir(kobj, new_parent, new_name);
  293. kobject_put(kobj);
  294. return error;
  295. }
  296. /**
  297. * kobject_move - move object to another parent
  298. * @kobj: object in question.
  299. * @new_parent: object's new parent (can be NULL)
  300. */
  301. int kobject_move(struct kobject *kobj, struct kobject *new_parent)
  302. {
  303. int error;
  304. struct kobject *old_parent;
  305. const char *devpath = NULL;
  306. char *devpath_string = NULL;
  307. char *envp[2];
  308. kobj = kobject_get(kobj);
  309. if (!kobj)
  310. return -EINVAL;
  311. new_parent = kobject_get(new_parent);
  312. if (!new_parent) {
  313. if (kobj->kset)
  314. new_parent = kobject_get(&kobj->kset->kobj);
  315. }
  316. /* old object path */
  317. devpath = kobject_get_path(kobj, GFP_KERNEL);
  318. if (!devpath) {
  319. error = -ENOMEM;
  320. goto out;
  321. }
  322. devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
  323. if (!devpath_string) {
  324. error = -ENOMEM;
  325. goto out;
  326. }
  327. sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
  328. envp[0] = devpath_string;
  329. envp[1] = NULL;
  330. error = sysfs_move_dir(kobj, new_parent);
  331. if (error)
  332. goto out;
  333. old_parent = kobj->parent;
  334. kobj->parent = new_parent;
  335. kobject_put(old_parent);
  336. kobject_uevent_env(kobj, KOBJ_MOVE, envp);
  337. out:
  338. kobject_put(kobj);
  339. kfree(devpath_string);
  340. kfree(devpath);
  341. return error;
  342. }
  343. /**
  344. * kobject_del - unlink kobject from hierarchy.
  345. * @kobj: object.
  346. */
  347. void kobject_del(struct kobject * kobj)
  348. {
  349. if (!kobj)
  350. return;
  351. sysfs_remove_dir(kobj);
  352. unlink(kobj);
  353. }
  354. /**
  355. * kobject_unregister - remove object from hierarchy and decrement refcount.
  356. * @kobj: object going away.
  357. */
  358. void kobject_unregister(struct kobject * kobj)
  359. {
  360. if (!kobj)
  361. return;
  362. pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
  363. kobject_uevent(kobj, KOBJ_REMOVE);
  364. kobject_del(kobj);
  365. kobject_put(kobj);
  366. }
  367. /**
  368. * kobject_get - increment refcount for object.
  369. * @kobj: object.
  370. */
  371. struct kobject * kobject_get(struct kobject * kobj)
  372. {
  373. if (kobj)
  374. kref_get(&kobj->kref);
  375. return kobj;
  376. }
  377. /**
  378. * kobject_cleanup - free kobject resources.
  379. * @kobj: object.
  380. */
  381. void kobject_cleanup(struct kobject * kobj)
  382. {
  383. struct kobj_type * t = get_ktype(kobj);
  384. struct kset * s = kobj->kset;
  385. struct kobject * parent = kobj->parent;
  386. pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
  387. if (kobj->k_name != kobj->name)
  388. kfree(kobj->k_name);
  389. kobj->k_name = NULL;
  390. if (t && t->release)
  391. t->release(kobj);
  392. if (s)
  393. kset_put(s);
  394. kobject_put(parent);
  395. }
  396. static void kobject_release(struct kref *kref)
  397. {
  398. kobject_cleanup(container_of(kref, struct kobject, kref));
  399. }
  400. /**
  401. * kobject_put - decrement refcount for object.
  402. * @kobj: object.
  403. *
  404. * Decrement the refcount, and if 0, call kobject_cleanup().
  405. */
  406. void kobject_put(struct kobject * kobj)
  407. {
  408. if (kobj)
  409. kref_put(&kobj->kref, kobject_release);
  410. }
  411. static void dir_release(struct kobject *kobj)
  412. {
  413. kfree(kobj);
  414. }
  415. static struct kobj_type dir_ktype = {
  416. .release = dir_release,
  417. .sysfs_ops = NULL,
  418. .default_attrs = NULL,
  419. };
  420. /**
  421. * kobject_add_dir - add sub directory of object.
  422. * @parent: object in which a directory is created.
  423. * @name: directory name.
  424. *
  425. * Add a plain directory object as child of given object.
  426. */
  427. struct kobject *kobject_add_dir(struct kobject *parent, const char *name)
  428. {
  429. struct kobject *k;
  430. int ret;
  431. if (!parent)
  432. return NULL;
  433. k = kzalloc(sizeof(*k), GFP_KERNEL);
  434. if (!k)
  435. return NULL;
  436. k->parent = parent;
  437. k->ktype = &dir_ktype;
  438. kobject_set_name(k, name);
  439. ret = kobject_register(k);
  440. if (ret < 0) {
  441. printk(KERN_WARNING "kobject_add_dir: "
  442. "kobject_register error: %d\n", ret);
  443. kobject_del(k);
  444. return NULL;
  445. }
  446. return k;
  447. }
  448. /**
  449. * kset_init - initialize a kset for use
  450. * @k: kset
  451. */
  452. void kset_init(struct kset * k)
  453. {
  454. kobject_init(&k->kobj);
  455. INIT_LIST_HEAD(&k->list);
  456. spin_lock_init(&k->list_lock);
  457. }
  458. /**
  459. * kset_add - add a kset object to the hierarchy.
  460. * @k: kset.
  461. *
  462. * Simply, this adds the kset's embedded kobject to the
  463. * hierarchy.
  464. * We also try to make sure that the kset's embedded kobject
  465. * has a parent before it is added. We only care if the embedded
  466. * kobject is not part of a kset itself, since kobject_add()
  467. * assigns a parent in that case.
  468. * If that is the case, and the kset has a controlling subsystem,
  469. * then we set the kset's parent to be said subsystem.
  470. */
  471. int kset_add(struct kset * k)
  472. {
  473. if (!k->kobj.parent && !k->kobj.kset && k->subsys)
  474. k->kobj.parent = &k->subsys->kset.kobj;
  475. return kobject_add(&k->kobj);
  476. }
  477. /**
  478. * kset_register - initialize and add a kset.
  479. * @k: kset.
  480. */
  481. int kset_register(struct kset * k)
  482. {
  483. if (!k)
  484. return -EINVAL;
  485. kset_init(k);
  486. return kset_add(k);
  487. }
  488. /**
  489. * kset_unregister - remove a kset.
  490. * @k: kset.
  491. */
  492. void kset_unregister(struct kset * k)
  493. {
  494. if (!k)
  495. return;
  496. kobject_unregister(&k->kobj);
  497. }
  498. /**
  499. * kset_find_obj - search for object in kset.
  500. * @kset: kset we're looking in.
  501. * @name: object's name.
  502. *
  503. * Lock kset via @kset->subsys, and iterate over @kset->list,
  504. * looking for a matching kobject. If matching object is found
  505. * take a reference and return the object.
  506. */
  507. struct kobject * kset_find_obj(struct kset * kset, const char * name)
  508. {
  509. struct list_head * entry;
  510. struct kobject * ret = NULL;
  511. spin_lock(&kset->list_lock);
  512. list_for_each(entry,&kset->list) {
  513. struct kobject * k = to_kobj(entry);
  514. if (kobject_name(k) && !strcmp(kobject_name(k),name)) {
  515. ret = kobject_get(k);
  516. break;
  517. }
  518. }
  519. spin_unlock(&kset->list_lock);
  520. return ret;
  521. }
  522. void subsystem_init(struct subsystem * s)
  523. {
  524. init_rwsem(&s->rwsem);
  525. kset_init(&s->kset);
  526. }
  527. /**
  528. * subsystem_register - register a subsystem.
  529. * @s: the subsystem we're registering.
  530. *
  531. * Once we register the subsystem, we want to make sure that
  532. * the kset points back to this subsystem for correct usage of
  533. * the rwsem.
  534. */
  535. int subsystem_register(struct subsystem * s)
  536. {
  537. int error;
  538. if (!s)
  539. return -EINVAL;
  540. subsystem_init(s);
  541. pr_debug("subsystem %s: registering\n",s->kset.kobj.name);
  542. if (!(error = kset_add(&s->kset))) {
  543. if (!s->kset.subsys)
  544. s->kset.subsys = s;
  545. }
  546. return error;
  547. }
  548. void subsystem_unregister(struct subsystem * s)
  549. {
  550. if (!s)
  551. return;
  552. pr_debug("subsystem %s: unregistering\n",s->kset.kobj.name);
  553. kset_unregister(&s->kset);
  554. }
  555. /**
  556. * subsystem_create_file - export sysfs attribute file.
  557. * @s: subsystem.
  558. * @a: subsystem attribute descriptor.
  559. */
  560. int subsys_create_file(struct subsystem * s, struct subsys_attribute * a)
  561. {
  562. int error = 0;
  563. if (!s || !a)
  564. return -EINVAL;
  565. if (subsys_get(s)) {
  566. error = sysfs_create_file(&s->kset.kobj,&a->attr);
  567. subsys_put(s);
  568. }
  569. return error;
  570. }
  571. /**
  572. * subsystem_remove_file - remove sysfs attribute file.
  573. * @s: subsystem.
  574. * @a: attribute desciptor.
  575. */
  576. #if 0
  577. void subsys_remove_file(struct subsystem * s, struct subsys_attribute * a)
  578. {
  579. if (subsys_get(s)) {
  580. sysfs_remove_file(&s->kset.kobj,&a->attr);
  581. subsys_put(s);
  582. }
  583. }
  584. #endif /* 0 */
  585. EXPORT_SYMBOL(kobject_init);
  586. EXPORT_SYMBOL(kobject_register);
  587. EXPORT_SYMBOL(kobject_unregister);
  588. EXPORT_SYMBOL(kobject_get);
  589. EXPORT_SYMBOL(kobject_put);
  590. EXPORT_SYMBOL(kobject_add);
  591. EXPORT_SYMBOL(kobject_del);
  592. EXPORT_SYMBOL(kset_register);
  593. EXPORT_SYMBOL(kset_unregister);
  594. EXPORT_SYMBOL(subsystem_register);
  595. EXPORT_SYMBOL(subsystem_unregister);
  596. EXPORT_SYMBOL(subsys_create_file);