kobject.c 27 KB

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