kobject.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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 sysfs_dirent *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 sysfs_dirent *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. kobject_set_name(kobj, "NO_NAME");
  153. if (!*kobj->k_name) {
  154. pr_debug("kobject attempted to be registered with no name!\n");
  155. WARN_ON(1);
  156. kobject_put(kobj);
  157. return -EINVAL;
  158. }
  159. parent = kobject_get(kobj->parent);
  160. pr_debug("kobject %s: registering. parent: %s, set: %s\n",
  161. kobject_name(kobj), parent ? kobject_name(parent) : "<NULL>",
  162. kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>" );
  163. if (kobj->kset) {
  164. spin_lock(&kobj->kset->list_lock);
  165. if (!parent)
  166. parent = kobject_get(&kobj->kset->kobj);
  167. list_add_tail(&kobj->entry,&kobj->kset->list);
  168. spin_unlock(&kobj->kset->list_lock);
  169. kobj->parent = parent;
  170. }
  171. error = create_dir(kobj, shadow_parent);
  172. if (error) {
  173. /* unlink does the kobject_put() for us */
  174. unlink(kobj);
  175. kobject_put(parent);
  176. /* be noisy on error issues */
  177. if (error == -EEXIST)
  178. printk(KERN_ERR "kobject_add failed for %s with "
  179. "-EEXIST, don't try to register things with "
  180. "the same name in the same directory.\n",
  181. kobject_name(kobj));
  182. else
  183. printk(KERN_ERR "kobject_add failed for %s (%d)\n",
  184. kobject_name(kobj), error);
  185. dump_stack();
  186. }
  187. return error;
  188. }
  189. /**
  190. * kobject_add - add an object to the hierarchy.
  191. * @kobj: object.
  192. */
  193. int kobject_add(struct kobject * kobj)
  194. {
  195. return kobject_shadow_add(kobj, NULL);
  196. }
  197. /**
  198. * kobject_register - initialize and add an object.
  199. * @kobj: object in question.
  200. */
  201. int kobject_register(struct kobject * kobj)
  202. {
  203. int error = -EINVAL;
  204. if (kobj) {
  205. kobject_init(kobj);
  206. error = kobject_add(kobj);
  207. if (!error)
  208. kobject_uevent(kobj, KOBJ_ADD);
  209. }
  210. return error;
  211. }
  212. /**
  213. * kobject_set_name - Set the name of an object
  214. * @kobj: object.
  215. * @fmt: format string used to build the name
  216. *
  217. * If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated
  218. * string that @kobj->k_name points to. Otherwise, use the static
  219. * @kobj->name array.
  220. */
  221. int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
  222. {
  223. int error = 0;
  224. int limit;
  225. int need;
  226. va_list args;
  227. char *name;
  228. /* find out how big a buffer we need */
  229. name = kmalloc(1024, GFP_KERNEL);
  230. if (!name) {
  231. error = -ENOMEM;
  232. goto done;
  233. }
  234. va_start(args, fmt);
  235. need = vsnprintf(name, 1024, fmt, args);
  236. va_end(args);
  237. kfree(name);
  238. /* Allocate the new space and copy the string in */
  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. /* something wrong with the string we copied? */
  249. if (need >= limit) {
  250. kfree(name);
  251. error = -EFAULT;
  252. goto done;
  253. }
  254. /* Free the old name, if necessary. */
  255. kfree(kobj->k_name);
  256. /* Now, set the new name */
  257. kobj->k_name = name;
  258. done:
  259. return error;
  260. }
  261. EXPORT_SYMBOL(kobject_set_name);
  262. /**
  263. * kobject_rename - change the name of an object
  264. * @kobj: object in question.
  265. * @new_name: object's new name
  266. */
  267. int kobject_rename(struct kobject * kobj, const char *new_name)
  268. {
  269. int error = 0;
  270. const char *devpath = NULL;
  271. char *devpath_string = NULL;
  272. char *envp[2];
  273. kobj = kobject_get(kobj);
  274. if (!kobj)
  275. return -EINVAL;
  276. if (!kobj->parent)
  277. return -EINVAL;
  278. devpath = kobject_get_path(kobj, GFP_KERNEL);
  279. if (!devpath) {
  280. error = -ENOMEM;
  281. goto out;
  282. }
  283. devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
  284. if (!devpath_string) {
  285. error = -ENOMEM;
  286. goto out;
  287. }
  288. sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
  289. envp[0] = devpath_string;
  290. envp[1] = NULL;
  291. /* Note : if we want to send the new name alone, not the full path,
  292. * we could probably use kobject_name(kobj); */
  293. error = sysfs_rename_dir(kobj, kobj->parent->sd, new_name);
  294. /* This function is mostly/only used for network interface.
  295. * Some hotplug package track interfaces by their name and
  296. * therefore want to know when the name is changed by the user. */
  297. if (!error)
  298. kobject_uevent_env(kobj, KOBJ_MOVE, envp);
  299. out:
  300. kfree(devpath_string);
  301. kfree(devpath);
  302. kobject_put(kobj);
  303. return error;
  304. }
  305. /**
  306. * kobject_rename - change the name of an object
  307. * @kobj: object in question.
  308. * @new_parent: object's new parent
  309. * @new_name: object's new name
  310. */
  311. int kobject_shadow_rename(struct kobject *kobj,
  312. struct sysfs_dirent *new_parent, const char *new_name)
  313. {
  314. int error = 0;
  315. kobj = kobject_get(kobj);
  316. if (!kobj)
  317. return -EINVAL;
  318. error = sysfs_rename_dir(kobj, new_parent, new_name);
  319. kobject_put(kobj);
  320. return error;
  321. }
  322. /**
  323. * kobject_move - move object to another parent
  324. * @kobj: object in question.
  325. * @new_parent: object's new parent (can be NULL)
  326. */
  327. int kobject_move(struct kobject *kobj, struct kobject *new_parent)
  328. {
  329. int error;
  330. struct kobject *old_parent;
  331. const char *devpath = NULL;
  332. char *devpath_string = NULL;
  333. char *envp[2];
  334. kobj = kobject_get(kobj);
  335. if (!kobj)
  336. return -EINVAL;
  337. new_parent = kobject_get(new_parent);
  338. if (!new_parent) {
  339. if (kobj->kset)
  340. new_parent = kobject_get(&kobj->kset->kobj);
  341. }
  342. /* old object path */
  343. devpath = kobject_get_path(kobj, GFP_KERNEL);
  344. if (!devpath) {
  345. error = -ENOMEM;
  346. goto out;
  347. }
  348. devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
  349. if (!devpath_string) {
  350. error = -ENOMEM;
  351. goto out;
  352. }
  353. sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
  354. envp[0] = devpath_string;
  355. envp[1] = NULL;
  356. error = sysfs_move_dir(kobj, new_parent);
  357. if (error)
  358. goto out;
  359. old_parent = kobj->parent;
  360. kobj->parent = new_parent;
  361. new_parent = NULL;
  362. kobject_put(old_parent);
  363. kobject_uevent_env(kobj, KOBJ_MOVE, envp);
  364. out:
  365. kobject_put(new_parent);
  366. kobject_put(kobj);
  367. kfree(devpath_string);
  368. kfree(devpath);
  369. return error;
  370. }
  371. /**
  372. * kobject_del - unlink kobject from hierarchy.
  373. * @kobj: object.
  374. */
  375. void kobject_del(struct kobject * kobj)
  376. {
  377. if (!kobj)
  378. return;
  379. sysfs_remove_dir(kobj);
  380. unlink(kobj);
  381. }
  382. /**
  383. * kobject_unregister - remove object from hierarchy and decrement refcount.
  384. * @kobj: object going away.
  385. */
  386. void kobject_unregister(struct kobject * kobj)
  387. {
  388. if (!kobj)
  389. return;
  390. pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
  391. kobject_uevent(kobj, KOBJ_REMOVE);
  392. kobject_del(kobj);
  393. kobject_put(kobj);
  394. }
  395. /**
  396. * kobject_get - increment refcount for object.
  397. * @kobj: object.
  398. */
  399. struct kobject * kobject_get(struct kobject * kobj)
  400. {
  401. if (kobj)
  402. kref_get(&kobj->kref);
  403. return kobj;
  404. }
  405. /**
  406. * kobject_cleanup - free kobject resources.
  407. * @kobj: object.
  408. */
  409. void kobject_cleanup(struct kobject * kobj)
  410. {
  411. struct kobj_type * t = get_ktype(kobj);
  412. struct kset * s = kobj->kset;
  413. struct kobject * parent = kobj->parent;
  414. const char *name = kobj->k_name;
  415. pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
  416. if (t && t->release) {
  417. t->release(kobj);
  418. /* If we have a release function, we can guess that this was
  419. * not a statically allocated kobject, so we should be safe to
  420. * free the name */
  421. kfree(name);
  422. }
  423. if (s)
  424. kset_put(s);
  425. kobject_put(parent);
  426. }
  427. static void kobject_release(struct kref *kref)
  428. {
  429. kobject_cleanup(container_of(kref, struct kobject, kref));
  430. }
  431. /**
  432. * kobject_put - decrement refcount for object.
  433. * @kobj: object.
  434. *
  435. * Decrement the refcount, and if 0, call kobject_cleanup().
  436. */
  437. void kobject_put(struct kobject * kobj)
  438. {
  439. if (kobj)
  440. kref_put(&kobj->kref, kobject_release);
  441. }
  442. static void dir_release(struct kobject *kobj)
  443. {
  444. kfree(kobj);
  445. }
  446. static struct kobj_type dir_ktype = {
  447. .release = dir_release,
  448. .sysfs_ops = NULL,
  449. .default_attrs = NULL,
  450. };
  451. /**
  452. * kobject_kset_add_dir - add sub directory of object.
  453. * @kset: kset the directory is belongs to.
  454. * @parent: object in which a directory is created.
  455. * @name: directory name.
  456. *
  457. * Add a plain directory object as child of given object.
  458. */
  459. struct kobject *kobject_kset_add_dir(struct kset *kset,
  460. struct kobject *parent, const char *name)
  461. {
  462. struct kobject *k;
  463. int ret;
  464. if (!parent)
  465. return NULL;
  466. k = kzalloc(sizeof(*k), GFP_KERNEL);
  467. if (!k)
  468. return NULL;
  469. k->kset = kset;
  470. k->parent = parent;
  471. k->ktype = &dir_ktype;
  472. kobject_set_name(k, name);
  473. ret = kobject_register(k);
  474. if (ret < 0) {
  475. printk(KERN_WARNING "%s: kobject_register error: %d\n",
  476. __func__, ret);
  477. kobject_del(k);
  478. return NULL;
  479. }
  480. return k;
  481. }
  482. /**
  483. * kobject_add_dir - add sub directory of object.
  484. * @parent: object in which a directory is created.
  485. * @name: directory name.
  486. *
  487. * Add a plain directory object as child of given object.
  488. */
  489. struct kobject *kobject_add_dir(struct kobject *parent, const char *name)
  490. {
  491. return kobject_kset_add_dir(NULL, parent, name);
  492. }
  493. /**
  494. * kset_init - initialize a kset for use
  495. * @k: kset
  496. */
  497. void kset_init(struct kset * k)
  498. {
  499. kobject_init(&k->kobj);
  500. INIT_LIST_HEAD(&k->list);
  501. spin_lock_init(&k->list_lock);
  502. }
  503. /**
  504. * kset_add - add a kset object to the hierarchy.
  505. * @k: kset.
  506. */
  507. int kset_add(struct kset * k)
  508. {
  509. return kobject_add(&k->kobj);
  510. }
  511. /**
  512. * kset_register - initialize and add a kset.
  513. * @k: kset.
  514. */
  515. int kset_register(struct kset * k)
  516. {
  517. int err;
  518. if (!k)
  519. return -EINVAL;
  520. kset_init(k);
  521. err = kset_add(k);
  522. if (err)
  523. return err;
  524. kobject_uevent(&k->kobj, KOBJ_ADD);
  525. return 0;
  526. }
  527. /**
  528. * kset_unregister - remove a kset.
  529. * @k: kset.
  530. */
  531. void kset_unregister(struct kset * k)
  532. {
  533. if (!k)
  534. return;
  535. kobject_unregister(&k->kobj);
  536. }
  537. /**
  538. * kset_find_obj - search for object in kset.
  539. * @kset: kset we're looking in.
  540. * @name: object's name.
  541. *
  542. * Lock kset via @kset->subsys, and iterate over @kset->list,
  543. * looking for a matching kobject. If matching object is found
  544. * take a reference and return the object.
  545. */
  546. struct kobject * kset_find_obj(struct kset * kset, const char * name)
  547. {
  548. struct list_head * entry;
  549. struct kobject * ret = NULL;
  550. spin_lock(&kset->list_lock);
  551. list_for_each(entry,&kset->list) {
  552. struct kobject * k = to_kobj(entry);
  553. if (kobject_name(k) && !strcmp(kobject_name(k),name)) {
  554. ret = kobject_get(k);
  555. break;
  556. }
  557. }
  558. spin_unlock(&kset->list_lock);
  559. return ret;
  560. }
  561. void subsystem_init(struct kset *s)
  562. {
  563. kset_init(s);
  564. }
  565. int subsystem_register(struct kset *s)
  566. {
  567. return kset_register(s);
  568. }
  569. void subsystem_unregister(struct kset *s)
  570. {
  571. kset_unregister(s);
  572. }
  573. /**
  574. * subsystem_create_file - export sysfs attribute file.
  575. * @s: subsystem.
  576. * @a: subsystem attribute descriptor.
  577. */
  578. int subsys_create_file(struct kset *s, struct subsys_attribute *a)
  579. {
  580. int error = 0;
  581. if (!s || !a)
  582. return -EINVAL;
  583. if (kset_get(s)) {
  584. error = sysfs_create_file(&s->kobj, &a->attr);
  585. kset_put(s);
  586. }
  587. return error;
  588. }
  589. EXPORT_SYMBOL(kobject_init);
  590. EXPORT_SYMBOL(kobject_register);
  591. EXPORT_SYMBOL(kobject_unregister);
  592. EXPORT_SYMBOL(kobject_get);
  593. EXPORT_SYMBOL(kobject_put);
  594. EXPORT_SYMBOL(kobject_add);
  595. EXPORT_SYMBOL(kobject_del);
  596. EXPORT_SYMBOL(kset_register);
  597. EXPORT_SYMBOL(kset_unregister);
  598. EXPORT_SYMBOL(subsystem_register);
  599. EXPORT_SYMBOL(subsystem_unregister);
  600. EXPORT_SYMBOL(subsys_create_file);