kobject.c 21 KB

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