kobject.c 21 KB

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