kobject.c 26 KB

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