kobject.c 11 KB

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