kobject.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 = 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. */
  143. int kobject_add(struct kobject * kobj)
  144. {
  145. int error = 0;
  146. struct kobject * parent;
  147. if (!(kobj = kobject_get(kobj)))
  148. return -ENOENT;
  149. if (!kobj->k_name)
  150. kobj->k_name = kobj->name;
  151. if (!kobj->k_name) {
  152. pr_debug("kobject attempted to be registered with no name!\n");
  153. WARN_ON(1);
  154. return -EINVAL;
  155. }
  156. parent = kobject_get(kobj->parent);
  157. pr_debug("kobject %s: registering. parent: %s, set: %s\n",
  158. kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>",
  159. kobj->kset ? kobj->kset->kobj.name : "<NULL>" );
  160. if (kobj->kset) {
  161. spin_lock(&kobj->kset->list_lock);
  162. if (!parent)
  163. parent = kobject_get(&kobj->kset->kobj);
  164. list_add_tail(&kobj->entry,&kobj->kset->list);
  165. spin_unlock(&kobj->kset->list_lock);
  166. }
  167. kobj->parent = parent;
  168. error = create_dir(kobj);
  169. if (error) {
  170. /* unlink does the kobject_put() for us */
  171. unlink(kobj);
  172. if (parent)
  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_register - initialize and add an object.
  189. * @kobj: object in question.
  190. */
  191. int kobject_register(struct kobject * kobj)
  192. {
  193. int error = -EINVAL;
  194. if (kobj) {
  195. kobject_init(kobj);
  196. error = kobject_add(kobj);
  197. if (!error)
  198. kobject_uevent(kobj, KOBJ_ADD);
  199. }
  200. return error;
  201. }
  202. /**
  203. * kobject_set_name - Set the name of an object
  204. * @kobj: object.
  205. * @fmt: format string used to build the name
  206. *
  207. * If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated
  208. * string that @kobj->k_name points to. Otherwise, use the static
  209. * @kobj->name array.
  210. */
  211. int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
  212. {
  213. int error = 0;
  214. int limit = KOBJ_NAME_LEN;
  215. int need;
  216. va_list args;
  217. char * name;
  218. /*
  219. * First, try the static array
  220. */
  221. va_start(args,fmt);
  222. need = vsnprintf(kobj->name,limit,fmt,args);
  223. va_end(args);
  224. if (need < limit)
  225. name = kobj->name;
  226. else {
  227. /*
  228. * Need more space? Allocate it and try again
  229. */
  230. limit = need + 1;
  231. name = kmalloc(limit,GFP_KERNEL);
  232. if (!name) {
  233. error = -ENOMEM;
  234. goto Done;
  235. }
  236. va_start(args,fmt);
  237. need = vsnprintf(name,limit,fmt,args);
  238. va_end(args);
  239. /* Still? Give up. */
  240. if (need >= limit) {
  241. kfree(name);
  242. error = -EFAULT;
  243. goto Done;
  244. }
  245. }
  246. /* Free the old name, if necessary. */
  247. if (kobj->k_name && kobj->k_name != kobj->name)
  248. kfree(kobj->k_name);
  249. /* Now, set the new name */
  250. kobj->k_name = name;
  251. Done:
  252. return error;
  253. }
  254. EXPORT_SYMBOL(kobject_set_name);
  255. /**
  256. * kobject_rename - change the name of an object
  257. * @kobj: object in question.
  258. * @new_name: object's new name
  259. */
  260. int kobject_rename(struct kobject * kobj, const char *new_name)
  261. {
  262. int error = 0;
  263. kobj = kobject_get(kobj);
  264. if (!kobj)
  265. return -EINVAL;
  266. error = sysfs_rename_dir(kobj, new_name);
  267. kobject_put(kobj);
  268. return error;
  269. }
  270. /**
  271. * kobject_move - move object to another parent
  272. * @kobj: object in question.
  273. * @new_parent: object's new parent (can be NULL)
  274. */
  275. int kobject_move(struct kobject *kobj, struct kobject *new_parent)
  276. {
  277. int error;
  278. struct kobject *old_parent;
  279. const char *devpath = NULL;
  280. char *devpath_string = NULL;
  281. char *envp[2];
  282. kobj = kobject_get(kobj);
  283. if (!kobj)
  284. return -EINVAL;
  285. new_parent = kobject_get(new_parent);
  286. if (!new_parent) {
  287. if (kobj->kset)
  288. new_parent = kobject_get(&kobj->kset->kobj);
  289. }
  290. /* old object path */
  291. devpath = kobject_get_path(kobj, GFP_KERNEL);
  292. if (!devpath) {
  293. error = -ENOMEM;
  294. goto out;
  295. }
  296. devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
  297. if (!devpath_string) {
  298. error = -ENOMEM;
  299. goto out;
  300. }
  301. sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
  302. envp[0] = devpath_string;
  303. envp[1] = NULL;
  304. error = sysfs_move_dir(kobj, new_parent);
  305. if (error)
  306. goto out;
  307. old_parent = kobj->parent;
  308. kobj->parent = new_parent;
  309. kobject_put(old_parent);
  310. kobject_uevent_env(kobj, KOBJ_MOVE, envp);
  311. out:
  312. kobject_put(kobj);
  313. kfree(devpath_string);
  314. kfree(devpath);
  315. return error;
  316. }
  317. /**
  318. * kobject_del - unlink kobject from hierarchy.
  319. * @kobj: object.
  320. */
  321. void kobject_del(struct kobject * kobj)
  322. {
  323. if (!kobj)
  324. return;
  325. sysfs_remove_dir(kobj);
  326. unlink(kobj);
  327. }
  328. /**
  329. * kobject_unregister - remove object from hierarchy and decrement refcount.
  330. * @kobj: object going away.
  331. */
  332. void kobject_unregister(struct kobject * kobj)
  333. {
  334. if (!kobj)
  335. return;
  336. pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
  337. kobject_uevent(kobj, KOBJ_REMOVE);
  338. kobject_del(kobj);
  339. kobject_put(kobj);
  340. }
  341. /**
  342. * kobject_get - increment refcount for object.
  343. * @kobj: object.
  344. */
  345. struct kobject * kobject_get(struct kobject * kobj)
  346. {
  347. if (kobj)
  348. kref_get(&kobj->kref);
  349. return kobj;
  350. }
  351. /**
  352. * kobject_cleanup - free kobject resources.
  353. * @kobj: object.
  354. */
  355. void kobject_cleanup(struct kobject * kobj)
  356. {
  357. struct kobj_type * t = get_ktype(kobj);
  358. struct kset * s = kobj->kset;
  359. struct kobject * parent = kobj->parent;
  360. pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
  361. if (kobj->k_name != kobj->name)
  362. kfree(kobj->k_name);
  363. kobj->k_name = NULL;
  364. if (t && t->release)
  365. t->release(kobj);
  366. if (s)
  367. kset_put(s);
  368. if (parent)
  369. kobject_put(parent);
  370. }
  371. static void kobject_release(struct kref *kref)
  372. {
  373. kobject_cleanup(container_of(kref, struct kobject, kref));
  374. }
  375. /**
  376. * kobject_put - decrement refcount for object.
  377. * @kobj: object.
  378. *
  379. * Decrement the refcount, and if 0, call kobject_cleanup().
  380. */
  381. void kobject_put(struct kobject * kobj)
  382. {
  383. if (kobj)
  384. kref_put(&kobj->kref, kobject_release);
  385. }
  386. static void dir_release(struct kobject *kobj)
  387. {
  388. kfree(kobj);
  389. }
  390. static struct kobj_type dir_ktype = {
  391. .release = dir_release,
  392. .sysfs_ops = NULL,
  393. .default_attrs = NULL,
  394. };
  395. /**
  396. * kobject_add_dir - add sub directory of object.
  397. * @parent: object in which a directory is created.
  398. * @name: directory name.
  399. *
  400. * Add a plain directory object as child of given object.
  401. */
  402. struct kobject *kobject_add_dir(struct kobject *parent, const char *name)
  403. {
  404. struct kobject *k;
  405. int ret;
  406. if (!parent)
  407. return NULL;
  408. k = kzalloc(sizeof(*k), GFP_KERNEL);
  409. if (!k)
  410. return NULL;
  411. k->parent = parent;
  412. k->ktype = &dir_ktype;
  413. kobject_set_name(k, name);
  414. ret = kobject_register(k);
  415. if (ret < 0) {
  416. printk(KERN_WARNING "kobject_add_dir: "
  417. "kobject_register error: %d\n", ret);
  418. kobject_del(k);
  419. return NULL;
  420. }
  421. return k;
  422. }
  423. /**
  424. * kset_init - initialize a kset for use
  425. * @k: kset
  426. */
  427. void kset_init(struct kset * k)
  428. {
  429. kobject_init(&k->kobj);
  430. INIT_LIST_HEAD(&k->list);
  431. spin_lock_init(&k->list_lock);
  432. }
  433. /**
  434. * kset_add - add a kset object to the hierarchy.
  435. * @k: kset.
  436. *
  437. * Simply, this adds the kset's embedded kobject to the
  438. * hierarchy.
  439. * We also try to make sure that the kset's embedded kobject
  440. * has a parent before it is added. We only care if the embedded
  441. * kobject is not part of a kset itself, since kobject_add()
  442. * assigns a parent in that case.
  443. * If that is the case, and the kset has a controlling subsystem,
  444. * then we set the kset's parent to be said subsystem.
  445. */
  446. int kset_add(struct kset * k)
  447. {
  448. if (!k->kobj.parent && !k->kobj.kset && k->subsys)
  449. k->kobj.parent = &k->subsys->kset.kobj;
  450. return kobject_add(&k->kobj);
  451. }
  452. /**
  453. * kset_register - initialize and add a kset.
  454. * @k: kset.
  455. */
  456. int kset_register(struct kset * k)
  457. {
  458. if (!k)
  459. return -EINVAL;
  460. kset_init(k);
  461. return kset_add(k);
  462. }
  463. /**
  464. * kset_unregister - remove a kset.
  465. * @k: kset.
  466. */
  467. void kset_unregister(struct kset * k)
  468. {
  469. if (!k)
  470. return;
  471. kobject_unregister(&k->kobj);
  472. }
  473. /**
  474. * kset_find_obj - search for object in kset.
  475. * @kset: kset we're looking in.
  476. * @name: object's name.
  477. *
  478. * Lock kset via @kset->subsys, and iterate over @kset->list,
  479. * looking for a matching kobject. If matching object is found
  480. * take a reference and return the object.
  481. */
  482. struct kobject * kset_find_obj(struct kset * kset, const char * name)
  483. {
  484. struct list_head * entry;
  485. struct kobject * ret = NULL;
  486. spin_lock(&kset->list_lock);
  487. list_for_each(entry,&kset->list) {
  488. struct kobject * k = to_kobj(entry);
  489. if (kobject_name(k) && !strcmp(kobject_name(k),name)) {
  490. ret = kobject_get(k);
  491. break;
  492. }
  493. }
  494. spin_unlock(&kset->list_lock);
  495. return ret;
  496. }
  497. void subsystem_init(struct subsystem * s)
  498. {
  499. init_rwsem(&s->rwsem);
  500. kset_init(&s->kset);
  501. }
  502. /**
  503. * subsystem_register - register a subsystem.
  504. * @s: the subsystem we're registering.
  505. *
  506. * Once we register the subsystem, we want to make sure that
  507. * the kset points back to this subsystem for correct usage of
  508. * the rwsem.
  509. */
  510. int subsystem_register(struct subsystem * s)
  511. {
  512. int error;
  513. if (!s)
  514. return -EINVAL;
  515. subsystem_init(s);
  516. pr_debug("subsystem %s: registering\n",s->kset.kobj.name);
  517. if (!(error = kset_add(&s->kset))) {
  518. if (!s->kset.subsys)
  519. s->kset.subsys = s;
  520. }
  521. return error;
  522. }
  523. void subsystem_unregister(struct subsystem * s)
  524. {
  525. if (!s)
  526. return;
  527. pr_debug("subsystem %s: unregistering\n",s->kset.kobj.name);
  528. kset_unregister(&s->kset);
  529. }
  530. /**
  531. * subsystem_create_file - export sysfs attribute file.
  532. * @s: subsystem.
  533. * @a: subsystem attribute descriptor.
  534. */
  535. int subsys_create_file(struct subsystem * s, struct subsys_attribute * a)
  536. {
  537. int error = 0;
  538. if (!s || !a)
  539. return -EINVAL;
  540. if (subsys_get(s)) {
  541. error = sysfs_create_file(&s->kset.kobj,&a->attr);
  542. subsys_put(s);
  543. }
  544. return error;
  545. }
  546. /**
  547. * subsystem_remove_file - remove sysfs attribute file.
  548. * @s: subsystem.
  549. * @a: attribute desciptor.
  550. */
  551. #if 0
  552. void subsys_remove_file(struct subsystem * s, struct subsys_attribute * a)
  553. {
  554. if (subsys_get(s)) {
  555. sysfs_remove_file(&s->kset.kobj,&a->attr);
  556. subsys_put(s);
  557. }
  558. }
  559. #endif /* 0 */
  560. EXPORT_SYMBOL(kobject_init);
  561. EXPORT_SYMBOL(kobject_register);
  562. EXPORT_SYMBOL(kobject_unregister);
  563. EXPORT_SYMBOL(kobject_get);
  564. EXPORT_SYMBOL(kobject_put);
  565. EXPORT_SYMBOL(kobject_add);
  566. EXPORT_SYMBOL(kobject_del);
  567. EXPORT_SYMBOL(kset_register);
  568. EXPORT_SYMBOL(kset_unregister);
  569. EXPORT_SYMBOL(subsystem_register);
  570. EXPORT_SYMBOL(subsystem_unregister);
  571. EXPORT_SYMBOL(subsys_create_file);