kobject.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. /**
  17. * populate_dir - populate directory with attributes.
  18. * @kobj: object we're working on.
  19. *
  20. * Most subsystems have a set of default attributes that
  21. * are associated with an object that registers with them.
  22. * This is a helper called during object registration that
  23. * loops through the default attributes of the subsystem
  24. * and creates attributes files for them in sysfs.
  25. *
  26. */
  27. static int populate_dir(struct kobject * kobj)
  28. {
  29. struct kobj_type * t = get_ktype(kobj);
  30. struct attribute * attr;
  31. int error = 0;
  32. int i;
  33. if (t && t->default_attrs) {
  34. for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
  35. if ((error = sysfs_create_file(kobj,attr)))
  36. break;
  37. }
  38. }
  39. return error;
  40. }
  41. static int create_dir(struct kobject * kobj)
  42. {
  43. int error = 0;
  44. if (kobject_name(kobj)) {
  45. error = sysfs_create_dir(kobj);
  46. if (!error) {
  47. if ((error = populate_dir(kobj)))
  48. sysfs_remove_dir(kobj);
  49. }
  50. }
  51. return error;
  52. }
  53. static inline struct kobject * to_kobj(struct list_head * entry)
  54. {
  55. return container_of(entry,struct kobject,entry);
  56. }
  57. static int get_kobj_path_length(struct kobject *kobj)
  58. {
  59. int length = 1;
  60. struct kobject * parent = kobj;
  61. /* walk up the ancestors until we hit the one pointing to the
  62. * root.
  63. * Add 1 to strlen for leading '/' of each level.
  64. */
  65. do {
  66. length += strlen(kobject_name(parent)) + 1;
  67. parent = parent->parent;
  68. } while (parent);
  69. return length;
  70. }
  71. static void fill_kobj_path(struct kobject *kobj, char *path, int length)
  72. {
  73. struct kobject * parent;
  74. --length;
  75. for (parent = kobj; parent; parent = parent->parent) {
  76. int cur = strlen(kobject_name(parent));
  77. /* back up enough to print this name with '/' */
  78. length -= cur;
  79. strncpy (path + length, kobject_name(parent), cur);
  80. *(path + --length) = '/';
  81. }
  82. pr_debug("%s: path = '%s'\n",__FUNCTION__,path);
  83. }
  84. /**
  85. * kobject_get_path - generate and return the path associated with a given kobj
  86. * and kset pair. The result must be freed by the caller with kfree().
  87. *
  88. * @kobj: kobject in question, with which to build the path
  89. * @gfp_mask: the allocation type used to allocate the path
  90. */
  91. char *kobject_get_path(struct kobject *kobj, int gfp_mask)
  92. {
  93. char *path;
  94. int len;
  95. len = get_kobj_path_length(kobj);
  96. path = kmalloc(len, gfp_mask);
  97. if (!path)
  98. return NULL;
  99. memset(path, 0x00, len);
  100. fill_kobj_path(kobj, path, len);
  101. return path;
  102. }
  103. /**
  104. * kobject_init - initialize object.
  105. * @kobj: object in question.
  106. */
  107. void kobject_init(struct kobject * kobj)
  108. {
  109. kref_init(&kobj->kref);
  110. INIT_LIST_HEAD(&kobj->entry);
  111. kobj->kset = kset_get(kobj->kset);
  112. }
  113. /**
  114. * unlink - remove kobject from kset list.
  115. * @kobj: kobject.
  116. *
  117. * Remove the kobject from the kset list and decrement
  118. * its parent's refcount.
  119. * This is separated out, so we can use it in both
  120. * kobject_del() and kobject_add() on error.
  121. */
  122. static void unlink(struct kobject * kobj)
  123. {
  124. if (kobj->kset) {
  125. spin_lock(&kobj->kset->list_lock);
  126. list_del_init(&kobj->entry);
  127. spin_unlock(&kobj->kset->list_lock);
  128. }
  129. kobject_put(kobj);
  130. }
  131. /**
  132. * kobject_add - add an object to the hierarchy.
  133. * @kobj: object.
  134. */
  135. int kobject_add(struct kobject * kobj)
  136. {
  137. int error = 0;
  138. struct kobject * parent;
  139. if (!(kobj = kobject_get(kobj)))
  140. return -ENOENT;
  141. if (!kobj->k_name)
  142. kobj->k_name = kobj->name;
  143. parent = kobject_get(kobj->parent);
  144. pr_debug("kobject %s: registering. parent: %s, set: %s\n",
  145. kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>",
  146. kobj->kset ? kobj->kset->kobj.name : "<NULL>" );
  147. if (kobj->kset) {
  148. spin_lock(&kobj->kset->list_lock);
  149. if (!parent)
  150. parent = kobject_get(&kobj->kset->kobj);
  151. list_add_tail(&kobj->entry,&kobj->kset->list);
  152. spin_unlock(&kobj->kset->list_lock);
  153. }
  154. kobj->parent = parent;
  155. error = create_dir(kobj);
  156. if (error) {
  157. /* unlink does the kobject_put() for us */
  158. unlink(kobj);
  159. if (parent)
  160. kobject_put(parent);
  161. }
  162. return error;
  163. }
  164. /**
  165. * kobject_register - initialize and add an object.
  166. * @kobj: object in question.
  167. */
  168. int kobject_register(struct kobject * kobj)
  169. {
  170. int error = 0;
  171. if (kobj) {
  172. kobject_init(kobj);
  173. error = kobject_add(kobj);
  174. if (error) {
  175. printk("kobject_register failed for %s (%d)\n",
  176. kobject_name(kobj),error);
  177. dump_stack();
  178. } else
  179. kobject_hotplug(kobj, KOBJ_ADD);
  180. } else
  181. error = -EINVAL;
  182. return error;
  183. }
  184. /**
  185. * kobject_set_name - Set the name of an object
  186. * @kobj: object.
  187. * @fmt: format string used to build the name
  188. *
  189. * If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated
  190. * string that @kobj->k_name points to. Otherwise, use the static
  191. * @kobj->name array.
  192. */
  193. int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
  194. {
  195. int error = 0;
  196. int limit = KOBJ_NAME_LEN;
  197. int need;
  198. va_list args;
  199. char * name;
  200. /*
  201. * First, try the static array
  202. */
  203. va_start(args,fmt);
  204. need = vsnprintf(kobj->name,limit,fmt,args);
  205. va_end(args);
  206. if (need < limit)
  207. name = kobj->name;
  208. else {
  209. /*
  210. * Need more space? Allocate it and try again
  211. */
  212. limit = need + 1;
  213. name = kmalloc(limit,GFP_KERNEL);
  214. if (!name) {
  215. error = -ENOMEM;
  216. goto Done;
  217. }
  218. va_start(args,fmt);
  219. need = vsnprintf(name,limit,fmt,args);
  220. va_end(args);
  221. /* Still? Give up. */
  222. if (need >= limit) {
  223. kfree(name);
  224. error = -EFAULT;
  225. goto Done;
  226. }
  227. }
  228. /* Free the old name, if necessary. */
  229. if (kobj->k_name && kobj->k_name != kobj->name)
  230. kfree(kobj->k_name);
  231. /* Now, set the new name */
  232. kobj->k_name = name;
  233. Done:
  234. return error;
  235. }
  236. EXPORT_SYMBOL(kobject_set_name);
  237. /**
  238. * kobject_rename - change the name of an object
  239. * @kobj: object in question.
  240. * @new_name: object's new name
  241. */
  242. int kobject_rename(struct kobject * kobj, const char *new_name)
  243. {
  244. int error = 0;
  245. kobj = kobject_get(kobj);
  246. if (!kobj)
  247. return -EINVAL;
  248. error = sysfs_rename_dir(kobj, new_name);
  249. kobject_put(kobj);
  250. return error;
  251. }
  252. /**
  253. * kobject_del - unlink kobject from hierarchy.
  254. * @kobj: object.
  255. */
  256. void kobject_del(struct kobject * kobj)
  257. {
  258. sysfs_remove_dir(kobj);
  259. unlink(kobj);
  260. }
  261. /**
  262. * kobject_unregister - remove object from hierarchy and decrement refcount.
  263. * @kobj: object going away.
  264. */
  265. void kobject_unregister(struct kobject * kobj)
  266. {
  267. pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
  268. kobject_hotplug(kobj, KOBJ_REMOVE);
  269. kobject_del(kobj);
  270. kobject_put(kobj);
  271. }
  272. /**
  273. * kobject_get - increment refcount for object.
  274. * @kobj: object.
  275. */
  276. struct kobject * kobject_get(struct kobject * kobj)
  277. {
  278. if (kobj)
  279. kref_get(&kobj->kref);
  280. return kobj;
  281. }
  282. /**
  283. * kobject_cleanup - free kobject resources.
  284. * @kobj: object.
  285. */
  286. void kobject_cleanup(struct kobject * kobj)
  287. {
  288. struct kobj_type * t = get_ktype(kobj);
  289. struct kset * s = kobj->kset;
  290. struct kobject * parent = kobj->parent;
  291. pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
  292. if (kobj->k_name != kobj->name)
  293. kfree(kobj->k_name);
  294. kobj->k_name = NULL;
  295. if (t && t->release)
  296. t->release(kobj);
  297. if (s)
  298. kset_put(s);
  299. if (parent)
  300. kobject_put(parent);
  301. }
  302. static void kobject_release(struct kref *kref)
  303. {
  304. kobject_cleanup(container_of(kref, struct kobject, kref));
  305. }
  306. /**
  307. * kobject_put - decrement refcount for object.
  308. * @kobj: object.
  309. *
  310. * Decrement the refcount, and if 0, call kobject_cleanup().
  311. */
  312. void kobject_put(struct kobject * kobj)
  313. {
  314. if (kobj)
  315. kref_put(&kobj->kref, kobject_release);
  316. }
  317. /**
  318. * kset_init - initialize a kset for use
  319. * @k: kset
  320. */
  321. void kset_init(struct kset * k)
  322. {
  323. kobject_init(&k->kobj);
  324. INIT_LIST_HEAD(&k->list);
  325. spin_lock_init(&k->list_lock);
  326. }
  327. /**
  328. * kset_add - add a kset object to the hierarchy.
  329. * @k: kset.
  330. *
  331. * Simply, this adds the kset's embedded kobject to the
  332. * hierarchy.
  333. * We also try to make sure that the kset's embedded kobject
  334. * has a parent before it is added. We only care if the embedded
  335. * kobject is not part of a kset itself, since kobject_add()
  336. * assigns a parent in that case.
  337. * If that is the case, and the kset has a controlling subsystem,
  338. * then we set the kset's parent to be said subsystem.
  339. */
  340. int kset_add(struct kset * k)
  341. {
  342. if (!k->kobj.parent && !k->kobj.kset && k->subsys)
  343. k->kobj.parent = &k->subsys->kset.kobj;
  344. return kobject_add(&k->kobj);
  345. }
  346. /**
  347. * kset_register - initialize and add a kset.
  348. * @k: kset.
  349. */
  350. int kset_register(struct kset * k)
  351. {
  352. kset_init(k);
  353. return kset_add(k);
  354. }
  355. /**
  356. * kset_unregister - remove a kset.
  357. * @k: kset.
  358. */
  359. void kset_unregister(struct kset * k)
  360. {
  361. kobject_unregister(&k->kobj);
  362. }
  363. /**
  364. * kset_find_obj - search for object in kset.
  365. * @kset: kset we're looking in.
  366. * @name: object's name.
  367. *
  368. * Lock kset via @kset->subsys, and iterate over @kset->list,
  369. * looking for a matching kobject. If matching object is found
  370. * take a reference and return the object.
  371. */
  372. struct kobject * kset_find_obj(struct kset * kset, const char * name)
  373. {
  374. struct list_head * entry;
  375. struct kobject * ret = NULL;
  376. spin_lock(&kset->list_lock);
  377. list_for_each(entry,&kset->list) {
  378. struct kobject * k = to_kobj(entry);
  379. if (kobject_name(k) && !strcmp(kobject_name(k),name)) {
  380. ret = kobject_get(k);
  381. break;
  382. }
  383. }
  384. spin_unlock(&kset->list_lock);
  385. return ret;
  386. }
  387. void subsystem_init(struct subsystem * s)
  388. {
  389. init_rwsem(&s->rwsem);
  390. kset_init(&s->kset);
  391. }
  392. /**
  393. * subsystem_register - register a subsystem.
  394. * @s: the subsystem we're registering.
  395. *
  396. * Once we register the subsystem, we want to make sure that
  397. * the kset points back to this subsystem for correct usage of
  398. * the rwsem.
  399. */
  400. int subsystem_register(struct subsystem * s)
  401. {
  402. int error;
  403. subsystem_init(s);
  404. pr_debug("subsystem %s: registering\n",s->kset.kobj.name);
  405. if (!(error = kset_add(&s->kset))) {
  406. if (!s->kset.subsys)
  407. s->kset.subsys = s;
  408. }
  409. return error;
  410. }
  411. void subsystem_unregister(struct subsystem * s)
  412. {
  413. pr_debug("subsystem %s: unregistering\n",s->kset.kobj.name);
  414. kset_unregister(&s->kset);
  415. }
  416. /**
  417. * subsystem_create_file - export sysfs attribute file.
  418. * @s: subsystem.
  419. * @a: subsystem attribute descriptor.
  420. */
  421. int subsys_create_file(struct subsystem * s, struct subsys_attribute * a)
  422. {
  423. int error = 0;
  424. if (subsys_get(s)) {
  425. error = sysfs_create_file(&s->kset.kobj,&a->attr);
  426. subsys_put(s);
  427. }
  428. return error;
  429. }
  430. /**
  431. * subsystem_remove_file - remove sysfs attribute file.
  432. * @s: subsystem.
  433. * @a: attribute desciptor.
  434. */
  435. void subsys_remove_file(struct subsystem * s, struct subsys_attribute * a)
  436. {
  437. if (subsys_get(s)) {
  438. sysfs_remove_file(&s->kset.kobj,&a->attr);
  439. subsys_put(s);
  440. }
  441. }
  442. EXPORT_SYMBOL(kobject_init);
  443. EXPORT_SYMBOL(kobject_register);
  444. EXPORT_SYMBOL(kobject_unregister);
  445. EXPORT_SYMBOL(kobject_get);
  446. EXPORT_SYMBOL(kobject_put);
  447. EXPORT_SYMBOL(kobject_add);
  448. EXPORT_SYMBOL(kobject_del);
  449. EXPORT_SYMBOL(kset_register);
  450. EXPORT_SYMBOL(kset_unregister);
  451. EXPORT_SYMBOL(kset_find_obj);
  452. EXPORT_SYMBOL(subsystem_init);
  453. EXPORT_SYMBOL(subsystem_register);
  454. EXPORT_SYMBOL(subsystem_unregister);
  455. EXPORT_SYMBOL(subsys_create_file);
  456. EXPORT_SYMBOL(subsys_remove_file);