kobject.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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("kobject: '%s' (%p): %s: path = '%s'\n", kobject_name(kobj),
  88. kobj, __FUNCTION__,path);
  89. }
  90. /**
  91. * kobject_get_path - generate and return the path associated with a given kobj and kset pair.
  92. *
  93. * @kobj: kobject in question, with which to build the path
  94. * @gfp_mask: the allocation type used to allocate the path
  95. *
  96. * The result must be freed by the caller with kfree().
  97. */
  98. char *kobject_get_path(struct kobject *kobj, gfp_t gfp_mask)
  99. {
  100. char *path;
  101. int len;
  102. len = get_kobj_path_length(kobj);
  103. if (len == 0)
  104. return NULL;
  105. path = kzalloc(len, gfp_mask);
  106. if (!path)
  107. return NULL;
  108. fill_kobj_path(kobj, path, len);
  109. return path;
  110. }
  111. EXPORT_SYMBOL_GPL(kobject_get_path);
  112. /**
  113. * kobject_init - initialize object.
  114. * @kobj: object in question.
  115. */
  116. void kobject_init(struct kobject * kobj)
  117. {
  118. if (!kobj)
  119. return;
  120. kref_init(&kobj->kref);
  121. INIT_LIST_HEAD(&kobj->entry);
  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. struct kobject *parent = kobj->parent;
  135. if (kobj->kset) {
  136. spin_lock(&kobj->kset->list_lock);
  137. list_del_init(&kobj->entry);
  138. spin_unlock(&kobj->kset->list_lock);
  139. }
  140. kobj->parent = NULL;
  141. kobject_put(kobj);
  142. kobject_put(parent);
  143. }
  144. /**
  145. * kobject_add - add an object to the hierarchy.
  146. * @kobj: object.
  147. */
  148. int kobject_add(struct kobject * kobj)
  149. {
  150. int error = 0;
  151. struct kobject * parent;
  152. if (!(kobj = kobject_get(kobj)))
  153. return -ENOENT;
  154. if (!kobj->k_name)
  155. kobject_set_name(kobj, "NO_NAME");
  156. if (!*kobj->k_name) {
  157. pr_debug("kobject (%p) attempted to be registered with no "
  158. "name!\n", kobj);
  159. WARN_ON(1);
  160. kobject_put(kobj);
  161. return -EINVAL;
  162. }
  163. parent = kobject_get(kobj->parent);
  164. pr_debug("kobject: '%s' (%p): %s: parent: '%s', set: '%s'\n",
  165. kobject_name(kobj), kobj, __FUNCTION__,
  166. parent ? kobject_name(parent) : "<NULL>",
  167. kobj->kset ? kobject_name(&kobj->kset->kobj) : "<NULL>" );
  168. if (kobj->kset) {
  169. kobj->kset = kset_get(kobj->kset);
  170. if (!parent) {
  171. parent = kobject_get(&kobj->kset->kobj);
  172. /*
  173. * If the kset is our parent, get a second
  174. * reference, we drop both the kset and the
  175. * parent ref on cleanup
  176. */
  177. kobject_get(parent);
  178. }
  179. spin_lock(&kobj->kset->list_lock);
  180. list_add_tail(&kobj->entry, &kobj->kset->list);
  181. spin_unlock(&kobj->kset->list_lock);
  182. kobj->parent = parent;
  183. }
  184. error = create_dir(kobj);
  185. if (error) {
  186. /* unlink does the kobject_put() for us */
  187. unlink(kobj);
  188. /* be noisy on error issues */
  189. if (error == -EEXIST)
  190. printk(KERN_ERR "kobject_add failed for %s with "
  191. "-EEXIST, don't try to register things with "
  192. "the same name in the same directory.\n",
  193. kobject_name(kobj));
  194. else
  195. printk(KERN_ERR "kobject_add failed for %s (%d)\n",
  196. kobject_name(kobj), error);
  197. dump_stack();
  198. }
  199. return error;
  200. }
  201. /**
  202. * kobject_register - initialize and add an object.
  203. * @kobj: object in question.
  204. */
  205. int kobject_register(struct kobject * kobj)
  206. {
  207. int error = -EINVAL;
  208. if (kobj) {
  209. kobject_init(kobj);
  210. error = kobject_add(kobj);
  211. if (!error)
  212. kobject_uevent(kobj, KOBJ_ADD);
  213. }
  214. return error;
  215. }
  216. /**
  217. * kobject_set_name_vargs - Set the name of an kobject
  218. * @kobj: struct kobject to set the name of
  219. * @fmt: format string used to build the name
  220. * @vargs: vargs to format the string.
  221. */
  222. static int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
  223. va_list vargs)
  224. {
  225. va_list aq;
  226. char *name;
  227. va_copy(aq, vargs);
  228. name = kvasprintf(GFP_KERNEL, fmt, vargs);
  229. va_end(aq);
  230. if (!name)
  231. return -ENOMEM;
  232. /* Free the old name, if necessary. */
  233. kfree(kobj->k_name);
  234. /* Now, set the new name */
  235. kobj->k_name = name;
  236. return 0;
  237. }
  238. /**
  239. * kobject_set_name - Set the name of a kobject
  240. * @kobj: struct kobject to set the name of
  241. * @fmt: format string used to build the name
  242. *
  243. * This sets the name of the kobject. If you have already added the
  244. * kobject to the system, you must call kobject_rename() in order to
  245. * change the name of the kobject.
  246. */
  247. int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
  248. {
  249. va_list args;
  250. int retval;
  251. va_start(args, fmt);
  252. retval = kobject_set_name_vargs(kobj, fmt, args);
  253. va_end(args);
  254. return retval;
  255. }
  256. EXPORT_SYMBOL(kobject_set_name);
  257. /**
  258. * kobject_init_ng - initialize a kobject structure
  259. * @kobj: pointer to the kobject to initialize
  260. * @ktype: pointer to the ktype for this kobject.
  261. *
  262. * This function will properly initialize a kobject such that it can then
  263. * be passed to the kobject_add() call.
  264. *
  265. * After this function is called, the kobject MUST be cleaned up by a call
  266. * to kobject_put(), not by a call to kfree directly to ensure that all of
  267. * the memory is cleaned up properly.
  268. */
  269. void kobject_init_ng(struct kobject *kobj, struct kobj_type *ktype)
  270. {
  271. char *err_str;
  272. if (!kobj) {
  273. err_str = "invalid kobject pointer!";
  274. goto error;
  275. }
  276. if (!ktype) {
  277. err_str = "must have a ktype to be initialized properly!\n";
  278. goto error;
  279. }
  280. if (atomic_read(&kobj->kref.refcount)) {
  281. /* do not error out as sometimes we can recover */
  282. printk(KERN_ERR "kobject: reference count is already set, "
  283. "something is seriously wrong.\n");
  284. dump_stack();
  285. }
  286. kref_init(&kobj->kref);
  287. INIT_LIST_HEAD(&kobj->entry);
  288. kobj->ktype = ktype;
  289. return;
  290. error:
  291. printk(KERN_ERR "kobject: %s\n", err_str);
  292. dump_stack();
  293. }
  294. EXPORT_SYMBOL(kobject_init_ng);
  295. static int kobject_add_varg(struct kobject *kobj, struct kobject *parent,
  296. const char *fmt, va_list vargs)
  297. {
  298. va_list aq;
  299. int retval;
  300. va_copy(aq, vargs);
  301. retval = kobject_set_name_vargs(kobj, fmt, aq);
  302. va_end(aq);
  303. if (retval) {
  304. printk(KERN_ERR "kobject: can not set name properly!\n");
  305. return retval;
  306. }
  307. kobj->parent = parent;
  308. return kobject_add(kobj);
  309. }
  310. /**
  311. * kobject_add_ng - the main kobject add function
  312. * @kobj: the kobject to add
  313. * @parent: pointer to the parent of the kobject.
  314. * @fmt: format to name the kobject with.
  315. *
  316. * The kobject name is set and added to the kobject hierarchy in this
  317. * function.
  318. *
  319. * If @parent is set, then the parent of the @kobj will be set to it.
  320. * If @parent is NULL, then the parent of the @kobj will be set to the
  321. * kobject associted with the kset assigned to this kobject. If no kset
  322. * is assigned to the kobject, then the kobject will be located in the
  323. * root of the sysfs tree.
  324. *
  325. * If this function returns an error, kobject_put() must be called to
  326. * properly clean up the memory associated with the object.
  327. *
  328. * If the function is successful, the only way to properly clean up the
  329. * memory is with a call to kobject_del(), in which case, a call to
  330. * kobject_put() is not necessary (kobject_del() does the final
  331. * kobject_put() to call the release function in the ktype's release
  332. * pointer.)
  333. *
  334. * Under no instance should the kobject that is passed to this function
  335. * be directly freed with a call to kfree(), that can leak memory.
  336. *
  337. * Note, no uevent will be created with this call, the caller should set
  338. * up all of the necessary sysfs files for the object and then call
  339. * kobject_uevent() with the UEVENT_ADD parameter to ensure that
  340. * userspace is properly notified of this kobject's creation.
  341. */
  342. int kobject_add_ng(struct kobject *kobj, struct kobject *parent,
  343. const char *fmt, ...)
  344. {
  345. va_list args;
  346. int retval;
  347. if (!kobj)
  348. return -EINVAL;
  349. va_start(args, fmt);
  350. retval = kobject_add_varg(kobj, parent, fmt, args);
  351. va_end(args);
  352. return retval;
  353. }
  354. EXPORT_SYMBOL(kobject_add_ng);
  355. /**
  356. * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
  357. * @kobj: pointer to the kobject to initialize
  358. * @ktype: pointer to the ktype for this kobject.
  359. * @parent: pointer to the parent of this kobject.
  360. * @fmt: the name of the kobject.
  361. *
  362. * This function combines the call to kobject_init_ng() and
  363. * kobject_add_ng(). The same type of error handling after a call to
  364. * kobject_add_ng() and kobject lifetime rules are the same here.
  365. */
  366. int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
  367. struct kobject *parent, const char *fmt, ...)
  368. {
  369. va_list args;
  370. int retval;
  371. kobject_init_ng(kobj, ktype);
  372. va_start(args, fmt);
  373. retval = kobject_add_varg(kobj, parent, fmt, args);
  374. va_end(args);
  375. return retval;
  376. }
  377. EXPORT_SYMBOL_GPL(kobject_init_and_add);
  378. /**
  379. * kobject_rename - change the name of an object
  380. * @kobj: object in question.
  381. * @new_name: object's new name
  382. */
  383. int kobject_rename(struct kobject * kobj, const char *new_name)
  384. {
  385. int error = 0;
  386. const char *devpath = NULL;
  387. char *devpath_string = NULL;
  388. char *envp[2];
  389. kobj = kobject_get(kobj);
  390. if (!kobj)
  391. return -EINVAL;
  392. if (!kobj->parent)
  393. return -EINVAL;
  394. /* see if this name is already in use */
  395. if (kobj->kset) {
  396. struct kobject *temp_kobj;
  397. temp_kobj = kset_find_obj(kobj->kset, new_name);
  398. if (temp_kobj) {
  399. printk(KERN_WARNING "kobject '%s' cannot be renamed "
  400. "to '%s' as '%s' is already in existence.\n",
  401. kobject_name(kobj), new_name, new_name);
  402. kobject_put(temp_kobj);
  403. return -EINVAL;
  404. }
  405. }
  406. devpath = kobject_get_path(kobj, GFP_KERNEL);
  407. if (!devpath) {
  408. error = -ENOMEM;
  409. goto out;
  410. }
  411. devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
  412. if (!devpath_string) {
  413. error = -ENOMEM;
  414. goto out;
  415. }
  416. sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
  417. envp[0] = devpath_string;
  418. envp[1] = NULL;
  419. error = sysfs_rename_dir(kobj, new_name);
  420. /* This function is mostly/only used for network interface.
  421. * Some hotplug package track interfaces by their name and
  422. * therefore want to know when the name is changed by the user. */
  423. if (!error)
  424. kobject_uevent_env(kobj, KOBJ_MOVE, envp);
  425. out:
  426. kfree(devpath_string);
  427. kfree(devpath);
  428. kobject_put(kobj);
  429. return error;
  430. }
  431. /**
  432. * kobject_move - move object to another parent
  433. * @kobj: object in question.
  434. * @new_parent: object's new parent (can be NULL)
  435. */
  436. int kobject_move(struct kobject *kobj, struct kobject *new_parent)
  437. {
  438. int error;
  439. struct kobject *old_parent;
  440. const char *devpath = NULL;
  441. char *devpath_string = NULL;
  442. char *envp[2];
  443. kobj = kobject_get(kobj);
  444. if (!kobj)
  445. return -EINVAL;
  446. new_parent = kobject_get(new_parent);
  447. if (!new_parent) {
  448. if (kobj->kset)
  449. new_parent = kobject_get(&kobj->kset->kobj);
  450. }
  451. /* old object path */
  452. devpath = kobject_get_path(kobj, GFP_KERNEL);
  453. if (!devpath) {
  454. error = -ENOMEM;
  455. goto out;
  456. }
  457. devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
  458. if (!devpath_string) {
  459. error = -ENOMEM;
  460. goto out;
  461. }
  462. sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
  463. envp[0] = devpath_string;
  464. envp[1] = NULL;
  465. error = sysfs_move_dir(kobj, new_parent);
  466. if (error)
  467. goto out;
  468. old_parent = kobj->parent;
  469. kobj->parent = new_parent;
  470. new_parent = NULL;
  471. kobject_put(old_parent);
  472. kobject_uevent_env(kobj, KOBJ_MOVE, envp);
  473. out:
  474. kobject_put(new_parent);
  475. kobject_put(kobj);
  476. kfree(devpath_string);
  477. kfree(devpath);
  478. return error;
  479. }
  480. /**
  481. * kobject_del - unlink kobject from hierarchy.
  482. * @kobj: object.
  483. */
  484. void kobject_del(struct kobject * kobj)
  485. {
  486. if (!kobj)
  487. return;
  488. sysfs_remove_dir(kobj);
  489. unlink(kobj);
  490. }
  491. /**
  492. * kobject_unregister - remove object from hierarchy and decrement refcount.
  493. * @kobj: object going away.
  494. */
  495. void kobject_unregister(struct kobject * kobj)
  496. {
  497. if (!kobj)
  498. return;
  499. pr_debug("kobject: '%s' (%p): %s\n",
  500. kobject_name(kobj), kobj, __FUNCTION__);
  501. kobject_uevent(kobj, KOBJ_REMOVE);
  502. kobject_del(kobj);
  503. kobject_put(kobj);
  504. }
  505. /**
  506. * kobject_get - increment refcount for object.
  507. * @kobj: object.
  508. */
  509. struct kobject * kobject_get(struct kobject * kobj)
  510. {
  511. if (kobj)
  512. kref_get(&kobj->kref);
  513. return kobj;
  514. }
  515. /*
  516. * kobject_cleanup - free kobject resources.
  517. * @kobj: object to cleanup
  518. */
  519. static void kobject_cleanup(struct kobject *kobj)
  520. {
  521. struct kobj_type * t = get_ktype(kobj);
  522. struct kset * s = kobj->kset;
  523. const char *name = kobj->k_name;
  524. pr_debug("kobject: '%s' (%p): %s\n",
  525. kobject_name(kobj), kobj, __FUNCTION__);
  526. if (t && t->release) {
  527. t->release(kobj);
  528. /* If we have a release function, we can guess that this was
  529. * not a statically allocated kobject, so we should be safe to
  530. * free the name */
  531. kfree(name);
  532. }
  533. if (s)
  534. kset_put(s);
  535. }
  536. static void kobject_release(struct kref *kref)
  537. {
  538. kobject_cleanup(container_of(kref, struct kobject, kref));
  539. }
  540. /**
  541. * kobject_put - decrement refcount for object.
  542. * @kobj: object.
  543. *
  544. * Decrement the refcount, and if 0, call kobject_cleanup().
  545. */
  546. void kobject_put(struct kobject * kobj)
  547. {
  548. if (kobj)
  549. kref_put(&kobj->kref, kobject_release);
  550. }
  551. static void dynamic_kobj_release(struct kobject *kobj)
  552. {
  553. pr_debug("kobject: '%s' (%p): %s\n",
  554. kobject_name(kobj), kobj, __FUNCTION__);
  555. kfree(kobj);
  556. }
  557. static struct kobj_type dynamic_kobj_ktype = {
  558. .release = dynamic_kobj_release,
  559. .sysfs_ops = &kobj_sysfs_ops,
  560. };
  561. /**
  562. * kobject_create - create a struct kobject dynamically
  563. *
  564. * This function creates a kobject structure dynamically and sets it up
  565. * to be a "dynamic" kobject with a default release function set up.
  566. *
  567. * If the kobject was not able to be created, NULL will be returned.
  568. * The kobject structure returned from here must be cleaned up with a
  569. * call to kobject_put() and not kfree(), as kobject_init_ng() has
  570. * already been called on this structure.
  571. */
  572. struct kobject *kobject_create(void)
  573. {
  574. struct kobject *kobj;
  575. kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
  576. if (!kobj)
  577. return NULL;
  578. kobject_init_ng(kobj, &dynamic_kobj_ktype);
  579. return kobj;
  580. }
  581. /**
  582. * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
  583. *
  584. * @name: the name for the kset
  585. * @parent: the parent kobject of this kobject, if any.
  586. *
  587. * This function creates a kset structure dynamically and registers it
  588. * with sysfs. When you are finished with this structure, call
  589. * kobject_unregister() and the structure will be dynamically freed when
  590. * it is no longer being used.
  591. *
  592. * If the kobject was not able to be created, NULL will be returned.
  593. */
  594. struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
  595. {
  596. struct kobject *kobj;
  597. int retval;
  598. kobj = kobject_create();
  599. if (!kobj)
  600. return NULL;
  601. retval = kobject_add_ng(kobj, parent, "%s", name);
  602. if (retval) {
  603. printk(KERN_WARNING "%s: kobject_add error: %d\n",
  604. __FUNCTION__, retval);
  605. kobject_put(kobj);
  606. kobj = NULL;
  607. }
  608. return kobj;
  609. }
  610. EXPORT_SYMBOL_GPL(kobject_create_and_add);
  611. /**
  612. * kset_init - initialize a kset for use
  613. * @k: kset
  614. */
  615. void kset_init(struct kset * k)
  616. {
  617. kobject_init(&k->kobj);
  618. INIT_LIST_HEAD(&k->list);
  619. spin_lock_init(&k->list_lock);
  620. }
  621. /* default kobject attribute operations */
  622. static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
  623. char *buf)
  624. {
  625. struct kobj_attribute *kattr;
  626. ssize_t ret = -EIO;
  627. kattr = container_of(attr, struct kobj_attribute, attr);
  628. if (kattr->show)
  629. ret = kattr->show(kobj, kattr, buf);
  630. return ret;
  631. }
  632. static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
  633. const char *buf, size_t count)
  634. {
  635. struct kobj_attribute *kattr;
  636. ssize_t ret = -EIO;
  637. kattr = container_of(attr, struct kobj_attribute, attr);
  638. if (kattr->store)
  639. ret = kattr->store(kobj, kattr, buf, count);
  640. return ret;
  641. }
  642. struct sysfs_ops kobj_sysfs_ops = {
  643. .show = kobj_attr_show,
  644. .store = kobj_attr_store,
  645. };
  646. /**
  647. * kset_add - add a kset object to the hierarchy.
  648. * @k: kset.
  649. */
  650. int kset_add(struct kset * k)
  651. {
  652. return kobject_add(&k->kobj);
  653. }
  654. /**
  655. * kset_register - initialize and add a kset.
  656. * @k: kset.
  657. */
  658. int kset_register(struct kset * k)
  659. {
  660. int err;
  661. if (!k)
  662. return -EINVAL;
  663. kset_init(k);
  664. err = kset_add(k);
  665. if (err)
  666. return err;
  667. kobject_uevent(&k->kobj, KOBJ_ADD);
  668. return 0;
  669. }
  670. /**
  671. * kset_unregister - remove a kset.
  672. * @k: kset.
  673. */
  674. void kset_unregister(struct kset * k)
  675. {
  676. if (!k)
  677. return;
  678. kobject_unregister(&k->kobj);
  679. }
  680. /**
  681. * kset_find_obj - search for object in kset.
  682. * @kset: kset we're looking in.
  683. * @name: object's name.
  684. *
  685. * Lock kset via @kset->subsys, and iterate over @kset->list,
  686. * looking for a matching kobject. If matching object is found
  687. * take a reference and return the object.
  688. */
  689. struct kobject * kset_find_obj(struct kset * kset, const char * name)
  690. {
  691. struct list_head * entry;
  692. struct kobject * ret = NULL;
  693. spin_lock(&kset->list_lock);
  694. list_for_each(entry,&kset->list) {
  695. struct kobject * k = to_kobj(entry);
  696. if (kobject_name(k) && !strcmp(kobject_name(k),name)) {
  697. ret = kobject_get(k);
  698. break;
  699. }
  700. }
  701. spin_unlock(&kset->list_lock);
  702. return ret;
  703. }
  704. static void kset_release(struct kobject *kobj)
  705. {
  706. struct kset *kset = container_of(kobj, struct kset, kobj);
  707. pr_debug("kobject: '%s' (%p): %s\n",
  708. kobject_name(kobj), kobj, __FUNCTION__);
  709. kfree(kset);
  710. }
  711. static struct kobj_type kset_ktype = {
  712. .sysfs_ops = &kobj_sysfs_ops,
  713. .release = kset_release,
  714. };
  715. /**
  716. * kset_create - create a struct kset dynamically
  717. *
  718. * @name: the name for the kset
  719. * @uevent_ops: a struct kset_uevent_ops for the kset
  720. * @parent_kobj: the parent kobject of this kset, if any.
  721. *
  722. * This function creates a kset structure dynamically. This structure can
  723. * then be registered with the system and show up in sysfs with a call to
  724. * kset_register(). When you are finished with this structure, if
  725. * kset_register() has been called, call kset_unregister() and the
  726. * structure will be dynamically freed when it is no longer being used.
  727. *
  728. * If the kset was not able to be created, NULL will be returned.
  729. */
  730. static struct kset *kset_create(const char *name,
  731. struct kset_uevent_ops *uevent_ops,
  732. struct kobject *parent_kobj)
  733. {
  734. struct kset *kset;
  735. kset = kzalloc(sizeof(*kset), GFP_KERNEL);
  736. if (!kset)
  737. return NULL;
  738. kobject_set_name(&kset->kobj, name);
  739. kset->uevent_ops = uevent_ops;
  740. kset->kobj.parent = parent_kobj;
  741. /*
  742. * The kobject of this kset will have a type of kset_ktype and belong to
  743. * no kset itself. That way we can properly free it when it is
  744. * finished being used.
  745. */
  746. kset->kobj.ktype = &kset_ktype;
  747. kset->kobj.kset = NULL;
  748. return kset;
  749. }
  750. /**
  751. * kset_create_and_add - create a struct kset dynamically and add it to sysfs
  752. *
  753. * @name: the name for the kset
  754. * @uevent_ops: a struct kset_uevent_ops for the kset
  755. * @parent_kobj: the parent kobject of this kset, if any.
  756. *
  757. * This function creates a kset structure dynamically and registers it
  758. * with sysfs. When you are finished with this structure, call
  759. * kset_unregister() and the structure will be dynamically freed when it
  760. * is no longer being used.
  761. *
  762. * If the kset was not able to be created, NULL will be returned.
  763. */
  764. struct kset *kset_create_and_add(const char *name,
  765. struct kset_uevent_ops *uevent_ops,
  766. struct kobject *parent_kobj)
  767. {
  768. struct kset *kset;
  769. int error;
  770. kset = kset_create(name, uevent_ops, parent_kobj);
  771. if (!kset)
  772. return NULL;
  773. error = kset_register(kset);
  774. if (error) {
  775. kfree(kset);
  776. return NULL;
  777. }
  778. return kset;
  779. }
  780. EXPORT_SYMBOL_GPL(kset_create_and_add);
  781. EXPORT_SYMBOL(kobject_init);
  782. EXPORT_SYMBOL(kobject_register);
  783. EXPORT_SYMBOL(kobject_unregister);
  784. EXPORT_SYMBOL(kobject_get);
  785. EXPORT_SYMBOL(kobject_put);
  786. EXPORT_SYMBOL(kobject_add);
  787. EXPORT_SYMBOL(kobject_del);
  788. EXPORT_SYMBOL(kset_register);
  789. EXPORT_SYMBOL(kset_unregister);