kobject.c 21 KB

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