kobject.c 14 KB

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