kobject.c 21 KB

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