kobject.c 18 KB

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