kobject.c 15 KB

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