kobject.c 23 KB

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