kobject.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * kobject.c - library routines for handling generic kernel objects
  3. *
  4. * Copyright (c) 2002-2003 Patrick Mochel <mochel@osdl.org>
  5. * Copyright (c) 2006-2007 Greg Kroah-Hartman <greg@kroah.com>
  6. * Copyright (c) 2006-2007 Novell Inc.
  7. *
  8. * This file is released under the GPLv2.
  9. *
  10. *
  11. * Please see the file Documentation/kobject.txt for critical information
  12. * about using the kobject interface.
  13. */
  14. #include <linux/kobject.h>
  15. #include <linux/string.h>
  16. #include <linux/module.h>
  17. #include <linux/stat.h>
  18. #include <linux/slab.h>
  19. /**
  20. * populate_dir - populate directory with attributes.
  21. * @kobj: object we're working on.
  22. *
  23. * Most subsystems have a set of default attributes that
  24. * are associated with an object that registers with them.
  25. * This is a helper called during object registration that
  26. * loops through the default attributes of the subsystem
  27. * and creates attributes files for them in sysfs.
  28. *
  29. */
  30. static int populate_dir(struct kobject * kobj)
  31. {
  32. struct kobj_type * t = get_ktype(kobj);
  33. struct attribute * attr;
  34. int error = 0;
  35. int i;
  36. if (t && t->default_attrs) {
  37. for (i = 0; (attr = t->default_attrs[i]) != NULL; i++) {
  38. if ((error = sysfs_create_file(kobj,attr)))
  39. break;
  40. }
  41. }
  42. return error;
  43. }
  44. static int create_dir(struct kobject * kobj)
  45. {
  46. int error = 0;
  47. if (kobject_name(kobj)) {
  48. error = sysfs_create_dir(kobj);
  49. if (!error) {
  50. if ((error = populate_dir(kobj)))
  51. sysfs_remove_dir(kobj);
  52. }
  53. }
  54. return error;
  55. }
  56. static inline struct kobject * to_kobj(struct list_head * entry)
  57. {
  58. return container_of(entry,struct kobject,entry);
  59. }
  60. static int get_kobj_path_length(struct kobject *kobj)
  61. {
  62. int length = 1;
  63. struct kobject * parent = kobj;
  64. /* walk up the ancestors until we hit the one pointing to the
  65. * root.
  66. * Add 1 to strlen for leading '/' of each level.
  67. */
  68. do {
  69. if (kobject_name(parent) == NULL)
  70. return 0;
  71. length += strlen(kobject_name(parent)) + 1;
  72. parent = parent->parent;
  73. } while (parent);
  74. return length;
  75. }
  76. static void fill_kobj_path(struct kobject *kobj, char *path, int length)
  77. {
  78. struct kobject * parent;
  79. --length;
  80. for (parent = kobj; parent; parent = parent->parent) {
  81. int cur = strlen(kobject_name(parent));
  82. /* back up enough to print this name with '/' */
  83. length -= cur;
  84. strncpy (path + length, kobject_name(parent), cur);
  85. *(path + --length) = '/';
  86. }
  87. pr_debug("%s: path = '%s'\n",__FUNCTION__,path);
  88. }
  89. /**
  90. * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
  91. *
  92. * @kobj: kobject in question, with which to build the path
  93. * @gfp_mask: the allocation type used to allocate the path
  94. *
  95. * The result must be freed by the caller with kfree().
  96. */
  97. char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
  98. {
  99. char *path;
  100. int len;
  101. len = get_kobj_path_length(kobj);
  102. if (len == 0)
  103. return NULL;
  104. path = kzalloc(len, gfp_mask);
  105. if (!path)
  106. return NULL;
  107. fill_kobj_path(kobj, path, len);
  108. return path;
  109. }
  110. EXPORT_SYMBOL_GPL(kobject_get_path);
  111. /**
  112. * kobject_init - initialize object.
  113. * @kobj: object in question.
  114. */
  115. void kobject_init(struct kobject * kobj)
  116. {
  117. if (!kobj)
  118. return;
  119. kref_init(&kobj->kref);
  120. INIT_LIST_HEAD(&kobj->entry);
  121. kobj->kset = kset_get(kobj->kset);
  122. }
  123. /**
  124. * unlink - remove kobject from kset list.
  125. * @kobj: kobject.
  126. *
  127. * Remove the kobject from the kset list and decrement
  128. * its parent's refcount.
  129. * This is separated out, so we can use it in both
  130. * kobject_del() and kobject_add() on error.
  131. */
  132. static void unlink(struct kobject * kobj)
  133. {
  134. if (kobj->kset) {
  135. spin_lock(&kobj->kset->list_lock);
  136. list_del_init(&kobj->entry);
  137. spin_unlock(&kobj->kset->list_lock);
  138. }
  139. kobject_put(kobj);
  140. }
  141. /**
  142. * kobject_add - add an object to the hierarchy.
  143. * @kobj: object.
  144. */
  145. int kobject_add(struct kobject * kobj)
  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);
  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_register - initialize and add an object.
  191. * @kobj: object in question.
  192. */
  193. int kobject_register(struct kobject * kobj)
  194. {
  195. int error = -EINVAL;
  196. if (kobj) {
  197. kobject_init(kobj);
  198. error = kobject_add(kobj);
  199. if (!error)
  200. kobject_uevent(kobj, KOBJ_ADD);
  201. }
  202. return error;
  203. }
  204. /**
  205. * kobject_set_name - Set the name of an object
  206. * @kobj: object.
  207. * @fmt: format string used to build the name
  208. *
  209. * If strlen(name) >= KOBJ_NAME_LEN, then use a dynamically allocated
  210. * string that @kobj->k_name points to. Otherwise, use the static
  211. * @kobj->name array.
  212. */
  213. int kobject_set_name(struct kobject * kobj, const char * fmt, ...)
  214. {
  215. int error = 0;
  216. int limit;
  217. int need;
  218. va_list args;
  219. char *name;
  220. /* find out how big a buffer we need */
  221. name = kmalloc(1024, GFP_KERNEL);
  222. if (!name) {
  223. error = -ENOMEM;
  224. goto done;
  225. }
  226. va_start(args, fmt);
  227. need = vsnprintf(name, 1024, fmt, args);
  228. va_end(args);
  229. kfree(name);
  230. /* Allocate the new space and copy the string in */
  231. limit = need + 1;
  232. name = kmalloc(limit, GFP_KERNEL);
  233. if (!name) {
  234. error = -ENOMEM;
  235. goto done;
  236. }
  237. va_start(args, fmt);
  238. need = vsnprintf(name, limit, fmt, args);
  239. va_end(args);
  240. /* something wrong with the string we copied? */
  241. if (need >= limit) {
  242. kfree(name);
  243. error = -EFAULT;
  244. goto done;
  245. }
  246. /* Free the old name, if necessary. */
  247. kfree(kobj->k_name);
  248. /* Now, set the new name */
  249. kobj->k_name = name;
  250. done:
  251. return error;
  252. }
  253. EXPORT_SYMBOL(kobject_set_name);
  254. /**
  255. * kobject_rename - change the name of an object
  256. * @kobj: object in question.
  257. * @new_name: object's new name
  258. */
  259. int kobject_rename(struct kobject * kobj, const char *new_name)
  260. {
  261. int error = 0;
  262. const char *devpath = NULL;
  263. char *devpath_string = NULL;
  264. char *envp[2];
  265. kobj = kobject_get(kobj);
  266. if (!kobj)
  267. return -EINVAL;
  268. if (!kobj->parent)
  269. return -EINVAL;
  270. devpath = kobject_get_path(kobj, GFP_KERNEL);
  271. if (!devpath) {
  272. error = -ENOMEM;
  273. goto out;
  274. }
  275. devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
  276. if (!devpath_string) {
  277. error = -ENOMEM;
  278. goto out;
  279. }
  280. sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
  281. envp[0] = devpath_string;
  282. envp[1] = NULL;
  283. /* Note : if we want to send the new name alone, not the full path,
  284. * we could probably use kobject_name(kobj); */
  285. error = sysfs_rename_dir(kobj, new_name);
  286. /* This function is mostly/only used for network interface.
  287. * Some hotplug package track interfaces by their name and
  288. * therefore want to know when the name is changed by the user. */
  289. if (!error)
  290. kobject_uevent_env(kobj, KOBJ_MOVE, envp);
  291. out:
  292. kfree(devpath_string);
  293. kfree(devpath);
  294. kobject_put(kobj);
  295. return error;
  296. }
  297. /**
  298. * kobject_move - move object to another parent
  299. * @kobj: object in question.
  300. * @new_parent: object's new parent (can be NULL)
  301. */
  302. int kobject_move(struct kobject *kobj, struct kobject *new_parent)
  303. {
  304. int error;
  305. struct kobject *old_parent;
  306. const char *devpath = NULL;
  307. char *devpath_string = NULL;
  308. char *envp[2];
  309. kobj = kobject_get(kobj);
  310. if (!kobj)
  311. return -EINVAL;
  312. new_parent = kobject_get(new_parent);
  313. if (!new_parent) {
  314. if (kobj->kset)
  315. new_parent = kobject_get(&kobj->kset->kobj);
  316. }
  317. /* old object path */
  318. devpath = kobject_get_path(kobj, GFP_KERNEL);
  319. if (!devpath) {
  320. error = -ENOMEM;
  321. goto out;
  322. }
  323. devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
  324. if (!devpath_string) {
  325. error = -ENOMEM;
  326. goto out;
  327. }
  328. sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
  329. envp[0] = devpath_string;
  330. envp[1] = NULL;
  331. error = sysfs_move_dir(kobj, new_parent);
  332. if (error)
  333. goto out;
  334. old_parent = kobj->parent;
  335. kobj->parent = new_parent;
  336. new_parent = NULL;
  337. kobject_put(old_parent);
  338. kobject_uevent_env(kobj, KOBJ_MOVE, envp);
  339. out:
  340. kobject_put(new_parent);
  341. kobject_put(kobj);
  342. kfree(devpath_string);
  343. kfree(devpath);
  344. return error;
  345. }
  346. /**
  347. * kobject_del - unlink kobject from hierarchy.
  348. * @kobj: object.
  349. */
  350. void kobject_del(struct kobject * kobj)
  351. {
  352. if (!kobj)
  353. return;
  354. sysfs_remove_dir(kobj);
  355. unlink(kobj);
  356. }
  357. /**
  358. * kobject_unregister - remove object from hierarchy and decrement refcount.
  359. * @kobj: object going away.
  360. */
  361. void kobject_unregister(struct kobject * kobj)
  362. {
  363. if (!kobj)
  364. return;
  365. pr_debug("kobject %s: unregistering\n",kobject_name(kobj));
  366. kobject_uevent(kobj, KOBJ_REMOVE);
  367. kobject_del(kobj);
  368. kobject_put(kobj);
  369. }
  370. /**
  371. * kobject_get - increment refcount for object.
  372. * @kobj: object.
  373. */
  374. struct kobject * kobject_get(struct kobject * kobj)
  375. {
  376. if (kobj)
  377. kref_get(&kobj->kref);
  378. return kobj;
  379. }
  380. /**
  381. * kobject_cleanup - free kobject resources.
  382. * @kobj: object.
  383. */
  384. void kobject_cleanup(struct kobject * kobj)
  385. {
  386. struct kobj_type * t = get_ktype(kobj);
  387. struct kset * s = kobj->kset;
  388. struct kobject * parent = kobj->parent;
  389. const char *name = kobj->k_name;
  390. pr_debug("kobject %s: cleaning up\n",kobject_name(kobj));
  391. if (t && t->release) {
  392. t->release(kobj);
  393. /* If we have a release function, we can guess that this was
  394. * not a statically allocated kobject, so we should be safe to
  395. * free the name */
  396. kfree(name);
  397. }
  398. if (s)
  399. kset_put(s);
  400. kobject_put(parent);
  401. }
  402. static void kobject_release(struct kref *kref)
  403. {
  404. kobject_cleanup(container_of(kref, struct kobject, kref));
  405. }
  406. /**
  407. * kobject_put - decrement refcount for object.
  408. * @kobj: object.
  409. *
  410. * Decrement the refcount, and if 0, call kobject_cleanup().
  411. */
  412. void kobject_put(struct kobject * kobj)
  413. {
  414. if (kobj)
  415. kref_put(&kobj->kref, kobject_release);
  416. }
  417. static void dir_release(struct kobject *kobj)
  418. {
  419. kfree(kobj);
  420. }
  421. static struct kobj_type dir_ktype = {
  422. .release = dir_release,
  423. .sysfs_ops = NULL,
  424. .default_attrs = NULL,
  425. };
  426. /**
  427. * kobject_kset_add_dir - add sub directory of object.
  428. * @kset: kset the directory is belongs to.
  429. * @parent: object in which a directory is created.
  430. * @name: directory name.
  431. *
  432. * Add a plain directory object as child of given object.
  433. */
  434. struct kobject *kobject_kset_add_dir(struct kset *kset,
  435. struct kobject *parent, const char *name)
  436. {
  437. struct kobject *k;
  438. int ret;
  439. if (!parent)
  440. return NULL;
  441. k = kzalloc(sizeof(*k), GFP_KERNEL);
  442. if (!k)
  443. return NULL;
  444. k->kset = kset;
  445. k->parent = parent;
  446. k->ktype = &dir_ktype;
  447. kobject_set_name(k, name);
  448. ret = kobject_register(k);
  449. if (ret < 0) {
  450. printk(KERN_WARNING "%s: kobject_register error: %d\n",
  451. __func__, ret);
  452. kobject_del(k);
  453. return NULL;
  454. }
  455. return k;
  456. }
  457. /**
  458. * kobject_add_dir - add sub directory of object.
  459. * @parent: object in which a directory is created.
  460. * @name: directory name.
  461. *
  462. * Add a plain directory object as child of given object.
  463. */
  464. struct kobject *kobject_add_dir(struct kobject *parent, const char *name)
  465. {
  466. return kobject_kset_add_dir(NULL, parent, name);
  467. }
  468. /**
  469. * kset_init - initialize a kset for use
  470. * @k: kset
  471. */
  472. void kset_init(struct kset * k)
  473. {
  474. kobject_init(&k->kobj);
  475. INIT_LIST_HEAD(&k->list);
  476. spin_lock_init(&k->list_lock);
  477. }
  478. /**
  479. * kset_add - add a kset object to the hierarchy.
  480. * @k: kset.
  481. */
  482. int kset_add(struct kset * k)
  483. {
  484. return kobject_add(&k->kobj);
  485. }
  486. /**
  487. * kset_register - initialize and add a kset.
  488. * @k: kset.
  489. */
  490. int kset_register(struct kset * k)
  491. {
  492. int err;
  493. if (!k)
  494. return -EINVAL;
  495. kset_init(k);
  496. err = kset_add(k);
  497. if (err)
  498. return err;
  499. kobject_uevent(&k->kobj, KOBJ_ADD);
  500. return 0;
  501. }
  502. /**
  503. * kset_unregister - remove a kset.
  504. * @k: kset.
  505. */
  506. void kset_unregister(struct kset * k)
  507. {
  508. if (!k)
  509. return;
  510. kobject_unregister(&k->kobj);
  511. }
  512. /**
  513. * kset_find_obj - search for object in kset.
  514. * @kset: kset we're looking in.
  515. * @name: object's name.
  516. *
  517. * Lock kset via @kset->subsys, and iterate over @kset->list,
  518. * looking for a matching kobject. If matching object is found
  519. * take a reference and return the object.
  520. */
  521. struct kobject * kset_find_obj(struct kset * kset, const char * name)
  522. {
  523. struct list_head * entry;
  524. struct kobject * ret = NULL;
  525. spin_lock(&kset->list_lock);
  526. list_for_each(entry,&kset->list) {
  527. struct kobject * k = to_kobj(entry);
  528. if (kobject_name(k) && !strcmp(kobject_name(k),name)) {
  529. ret = kobject_get(k);
  530. break;
  531. }
  532. }
  533. spin_unlock(&kset->list_lock);
  534. return ret;
  535. }
  536. int subsystem_register(struct kset *s)
  537. {
  538. return kset_register(s);
  539. }
  540. void subsystem_unregister(struct kset *s)
  541. {
  542. kset_unregister(s);
  543. }
  544. /**
  545. * subsystem_create_file - export sysfs attribute file.
  546. * @s: subsystem.
  547. * @a: subsystem attribute descriptor.
  548. */
  549. int subsys_create_file(struct kset *s, struct subsys_attribute *a)
  550. {
  551. int error = 0;
  552. if (!s || !a)
  553. return -EINVAL;
  554. if (kset_get(s)) {
  555. error = sysfs_create_file(&s->kobj, &a->attr);
  556. kset_put(s);
  557. }
  558. return error;
  559. }
  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);