kobject.c 23 KB

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