kobject.c 11 KB

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