kobject.c 21 KB

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