kobject.c 21 KB

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