kobject.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. return -ENOMEM;
  231. /* ewww... some of these buggers have '/' in the name ... */
  232. while ((s = strchr(kobj->name, '/')))
  233. s[0] = '!';
  234. kfree(old_name);
  235. return 0;
  236. }
  237. /**
  238. * kobject_set_name - Set the name of a kobject
  239. * @kobj: struct kobject to set the name of
  240. * @fmt: format string used to build the name
  241. *
  242. * This sets the name of the kobject. If you have already added the
  243. * kobject to the system, you must call kobject_rename() in order to
  244. * change the name of the kobject.
  245. */
  246. int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
  247. {
  248. va_list vargs;
  249. int retval;
  250. va_start(vargs, fmt);
  251. retval = kobject_set_name_vargs(kobj, fmt, vargs);
  252. va_end(vargs);
  253. return retval;
  254. }
  255. EXPORT_SYMBOL(kobject_set_name);
  256. /**
  257. * kobject_init - initialize a kobject structure
  258. * @kobj: pointer to the kobject to initialize
  259. * @ktype: pointer to the ktype for this kobject.
  260. *
  261. * This function will properly initialize a kobject such that it can then
  262. * be passed to the kobject_add() call.
  263. *
  264. * After this function is called, the kobject MUST be cleaned up by a call
  265. * to kobject_put(), not by a call to kfree directly to ensure that all of
  266. * the memory is cleaned up properly.
  267. */
  268. void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
  269. {
  270. char *err_str;
  271. if (!kobj) {
  272. err_str = "invalid kobject pointer!";
  273. goto error;
  274. }
  275. if (!ktype) {
  276. err_str = "must have a ktype to be initialized properly!\n";
  277. goto error;
  278. }
  279. if (kobj->state_initialized) {
  280. /* do not error out as sometimes we can recover */
  281. printk(KERN_ERR "kobject (%p): tried to init an initialized "
  282. "object, something is seriously wrong.\n", kobj);
  283. dump_stack();
  284. }
  285. kobject_init_internal(kobj);
  286. kobj->ktype = ktype;
  287. return;
  288. error:
  289. printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);
  290. dump_stack();
  291. }
  292. EXPORT_SYMBOL(kobject_init);
  293. static int kobject_add_varg(struct kobject *kobj, struct kobject *parent,
  294. const char *fmt, va_list vargs)
  295. {
  296. int retval;
  297. retval = kobject_set_name_vargs(kobj, fmt, vargs);
  298. if (retval) {
  299. printk(KERN_ERR "kobject: can not set name properly!\n");
  300. return retval;
  301. }
  302. kobj->parent = parent;
  303. return kobject_add_internal(kobj);
  304. }
  305. /**
  306. * kobject_add - the main kobject add function
  307. * @kobj: the kobject to add
  308. * @parent: pointer to the parent of the kobject.
  309. * @fmt: format to name the kobject with.
  310. *
  311. * The kobject name is set and added to the kobject hierarchy in this
  312. * function.
  313. *
  314. * If @parent is set, then the parent of the @kobj will be set to it.
  315. * If @parent is NULL, then the parent of the @kobj will be set to the
  316. * kobject associted with the kset assigned to this kobject. If no kset
  317. * is assigned to the kobject, then the kobject will be located in the
  318. * root of the sysfs tree.
  319. *
  320. * If this function returns an error, kobject_put() must be called to
  321. * properly clean up the memory associated with the object.
  322. * Under no instance should the kobject that is passed to this function
  323. * be directly freed with a call to kfree(), that can leak memory.
  324. *
  325. * Note, no "add" uevent will be created with this call, the caller should set
  326. * up all of the necessary sysfs files for the object and then call
  327. * kobject_uevent() with the UEVENT_ADD parameter to ensure that
  328. * userspace is properly notified of this kobject's creation.
  329. */
  330. int kobject_add(struct kobject *kobj, struct kobject *parent,
  331. const char *fmt, ...)
  332. {
  333. va_list args;
  334. int retval;
  335. if (!kobj)
  336. return -EINVAL;
  337. if (!kobj->state_initialized) {
  338. printk(KERN_ERR "kobject '%s' (%p): tried to add an "
  339. "uninitialized object, something is seriously wrong.\n",
  340. kobject_name(kobj), kobj);
  341. dump_stack();
  342. return -EINVAL;
  343. }
  344. va_start(args, fmt);
  345. retval = kobject_add_varg(kobj, parent, fmt, args);
  346. va_end(args);
  347. return retval;
  348. }
  349. EXPORT_SYMBOL(kobject_add);
  350. /**
  351. * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
  352. * @kobj: pointer to the kobject to initialize
  353. * @ktype: pointer to the ktype for this kobject.
  354. * @parent: pointer to the parent of this kobject.
  355. * @fmt: the name of the kobject.
  356. *
  357. * This function combines the call to kobject_init() and
  358. * kobject_add(). The same type of error handling after a call to
  359. * kobject_add() and kobject lifetime rules are the same here.
  360. */
  361. int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
  362. struct kobject *parent, const char *fmt, ...)
  363. {
  364. va_list args;
  365. int retval;
  366. kobject_init(kobj, ktype);
  367. va_start(args, fmt);
  368. retval = kobject_add_varg(kobj, parent, fmt, args);
  369. va_end(args);
  370. return retval;
  371. }
  372. EXPORT_SYMBOL_GPL(kobject_init_and_add);
  373. /**
  374. * kobject_rename - change the name of an object
  375. * @kobj: object in question.
  376. * @new_name: object's new name
  377. *
  378. * It is the responsibility of the caller to provide mutual
  379. * exclusion between two different calls of kobject_rename
  380. * on the same kobject and to ensure that new_name is valid and
  381. * won't conflict with other kobjects.
  382. */
  383. int kobject_rename(struct kobject *kobj, const char *new_name)
  384. {
  385. int error = 0;
  386. const char *devpath = NULL;
  387. const char *dup_name = NULL, *name;
  388. char *devpath_string = NULL;
  389. char *envp[2];
  390. kobj = kobject_get(kobj);
  391. if (!kobj)
  392. return -EINVAL;
  393. if (!kobj->parent)
  394. return -EINVAL;
  395. devpath = kobject_get_path(kobj, GFP_KERNEL);
  396. if (!devpath) {
  397. error = -ENOMEM;
  398. goto out;
  399. }
  400. devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
  401. if (!devpath_string) {
  402. error = -ENOMEM;
  403. goto out;
  404. }
  405. sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
  406. envp[0] = devpath_string;
  407. envp[1] = NULL;
  408. name = dup_name = kstrdup(new_name, GFP_KERNEL);
  409. if (!name) {
  410. error = -ENOMEM;
  411. goto out;
  412. }
  413. error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
  414. if (error)
  415. goto out;
  416. /* Install the new kobject name */
  417. dup_name = kobj->name;
  418. kobj->name = name;
  419. /* This function is mostly/only used for network interface.
  420. * Some hotplug package track interfaces by their name and
  421. * therefore want to know when the name is changed by the user. */
  422. kobject_uevent_env(kobj, KOBJ_MOVE, envp);
  423. out:
  424. kfree(dup_name);
  425. kfree(devpath_string);
  426. kfree(devpath);
  427. kobject_put(kobj);
  428. return error;
  429. }
  430. EXPORT_SYMBOL_GPL(kobject_rename);
  431. /**
  432. * kobject_move - move object to another parent
  433. * @kobj: object in question.
  434. * @new_parent: object's new parent (can be NULL)
  435. */
  436. int kobject_move(struct kobject *kobj, struct kobject *new_parent)
  437. {
  438. int error;
  439. struct kobject *old_parent;
  440. const char *devpath = NULL;
  441. char *devpath_string = NULL;
  442. char *envp[2];
  443. kobj = kobject_get(kobj);
  444. if (!kobj)
  445. return -EINVAL;
  446. new_parent = kobject_get(new_parent);
  447. if (!new_parent) {
  448. if (kobj->kset)
  449. new_parent = kobject_get(&kobj->kset->kobj);
  450. }
  451. /* old object path */
  452. devpath = kobject_get_path(kobj, GFP_KERNEL);
  453. if (!devpath) {
  454. error = -ENOMEM;
  455. goto out;
  456. }
  457. devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
  458. if (!devpath_string) {
  459. error = -ENOMEM;
  460. goto out;
  461. }
  462. sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
  463. envp[0] = devpath_string;
  464. envp[1] = NULL;
  465. error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
  466. if (error)
  467. goto out;
  468. old_parent = kobj->parent;
  469. kobj->parent = new_parent;
  470. new_parent = NULL;
  471. kobject_put(old_parent);
  472. kobject_uevent_env(kobj, KOBJ_MOVE, envp);
  473. out:
  474. kobject_put(new_parent);
  475. kobject_put(kobj);
  476. kfree(devpath_string);
  477. kfree(devpath);
  478. return error;
  479. }
  480. /**
  481. * kobject_del - unlink kobject from hierarchy.
  482. * @kobj: object.
  483. */
  484. void kobject_del(struct kobject *kobj)
  485. {
  486. struct sysfs_dirent *sd;
  487. if (!kobj)
  488. return;
  489. sd = kobj->sd;
  490. sysfs_remove_dir(kobj);
  491. sysfs_put(sd);
  492. kobj->state_in_sysfs = 0;
  493. kobj_kset_leave(kobj);
  494. kobject_put(kobj->parent);
  495. kobj->parent = NULL;
  496. }
  497. /**
  498. * kobject_get - increment refcount for object.
  499. * @kobj: object.
  500. */
  501. struct kobject *kobject_get(struct kobject *kobj)
  502. {
  503. if (kobj)
  504. kref_get(&kobj->kref);
  505. return kobj;
  506. }
  507. static struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
  508. {
  509. if (!kref_get_unless_zero(&kobj->kref))
  510. kobj = NULL;
  511. return kobj;
  512. }
  513. /*
  514. * kobject_cleanup - free kobject resources.
  515. * @kobj: object to cleanup
  516. */
  517. static void kobject_cleanup(struct kobject *kobj)
  518. {
  519. struct kobj_type *t = get_ktype(kobj);
  520. const char *name = kobj->name;
  521. pr_debug("kobject: '%s' (%p): %s, parent %p\n",
  522. kobject_name(kobj), kobj, __func__, kobj->parent);
  523. if (t && !t->release)
  524. pr_debug("kobject: '%s' (%p): does not have a release() "
  525. "function, it is broken and must be fixed.\n",
  526. kobject_name(kobj), kobj);
  527. /* send "remove" if the caller did not do it but sent "add" */
  528. if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
  529. pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
  530. kobject_name(kobj), kobj);
  531. kobject_uevent(kobj, KOBJ_REMOVE);
  532. }
  533. /* remove from sysfs if the caller did not do it */
  534. if (kobj->state_in_sysfs) {
  535. pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n",
  536. kobject_name(kobj), kobj);
  537. kobject_del(kobj);
  538. }
  539. if (t && t->release) {
  540. pr_debug("kobject: '%s' (%p): calling ktype release\n",
  541. kobject_name(kobj), kobj);
  542. t->release(kobj);
  543. }
  544. /* free name if we allocated it */
  545. if (name) {
  546. pr_debug("kobject: '%s': free name\n", name);
  547. kfree(name);
  548. }
  549. }
  550. #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
  551. static void kobject_delayed_cleanup(struct work_struct *work)
  552. {
  553. kobject_cleanup(container_of(to_delayed_work(work),
  554. struct kobject, release));
  555. }
  556. #endif
  557. static void kobject_release(struct kref *kref)
  558. {
  559. struct kobject *kobj = container_of(kref, struct kobject, kref);
  560. #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
  561. unsigned long delay = HZ + HZ * (get_random_int() & 0x3);
  562. pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n",
  563. kobject_name(kobj), kobj, __func__, kobj->parent, delay);
  564. INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
  565. schedule_delayed_work(&kobj->release, delay);
  566. #else
  567. kobject_cleanup(kobj);
  568. #endif
  569. }
  570. /**
  571. * kobject_put - decrement refcount for object.
  572. * @kobj: object.
  573. *
  574. * Decrement the refcount, and if 0, call kobject_cleanup().
  575. */
  576. void kobject_put(struct kobject *kobj)
  577. {
  578. if (kobj) {
  579. if (!kobj->state_initialized)
  580. WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
  581. "initialized, yet kobject_put() is being "
  582. "called.\n", kobject_name(kobj), kobj);
  583. kref_put(&kobj->kref, kobject_release);
  584. }
  585. }
  586. static void dynamic_kobj_release(struct kobject *kobj)
  587. {
  588. pr_debug("kobject: (%p): %s\n", kobj, __func__);
  589. kfree(kobj);
  590. }
  591. static struct kobj_type dynamic_kobj_ktype = {
  592. .release = dynamic_kobj_release,
  593. .sysfs_ops = &kobj_sysfs_ops,
  594. };
  595. /**
  596. * kobject_create - create a struct kobject dynamically
  597. *
  598. * This function creates a kobject structure dynamically and sets it up
  599. * to be a "dynamic" kobject with a default release function set up.
  600. *
  601. * If the kobject was not able to be created, NULL will be returned.
  602. * The kobject structure returned from here must be cleaned up with a
  603. * call to kobject_put() and not kfree(), as kobject_init() has
  604. * already been called on this structure.
  605. */
  606. struct kobject *kobject_create(void)
  607. {
  608. struct kobject *kobj;
  609. kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
  610. if (!kobj)
  611. return NULL;
  612. kobject_init(kobj, &dynamic_kobj_ktype);
  613. return kobj;
  614. }
  615. /**
  616. * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
  617. *
  618. * @name: the name for the kobject
  619. * @parent: the parent kobject of this kobject, if any.
  620. *
  621. * This function creates a kobject structure dynamically and registers it
  622. * with sysfs. When you are finished with this structure, call
  623. * kobject_put() and the structure will be dynamically freed when
  624. * it is no longer being used.
  625. *
  626. * If the kobject was not able to be created, NULL will be returned.
  627. */
  628. struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
  629. {
  630. struct kobject *kobj;
  631. int retval;
  632. kobj = kobject_create();
  633. if (!kobj)
  634. return NULL;
  635. retval = kobject_add(kobj, parent, "%s", name);
  636. if (retval) {
  637. printk(KERN_WARNING "%s: kobject_add error: %d\n",
  638. __func__, retval);
  639. kobject_put(kobj);
  640. kobj = NULL;
  641. }
  642. return kobj;
  643. }
  644. EXPORT_SYMBOL_GPL(kobject_create_and_add);
  645. /**
  646. * kset_init - initialize a kset for use
  647. * @k: kset
  648. */
  649. void kset_init(struct kset *k)
  650. {
  651. kobject_init_internal(&k->kobj);
  652. INIT_LIST_HEAD(&k->list);
  653. spin_lock_init(&k->list_lock);
  654. }
  655. /* default kobject attribute operations */
  656. static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
  657. char *buf)
  658. {
  659. struct kobj_attribute *kattr;
  660. ssize_t ret = -EIO;
  661. kattr = container_of(attr, struct kobj_attribute, attr);
  662. if (kattr->show)
  663. ret = kattr->show(kobj, kattr, buf);
  664. return ret;
  665. }
  666. static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
  667. const char *buf, size_t count)
  668. {
  669. struct kobj_attribute *kattr;
  670. ssize_t ret = -EIO;
  671. kattr = container_of(attr, struct kobj_attribute, attr);
  672. if (kattr->store)
  673. ret = kattr->store(kobj, kattr, buf, count);
  674. return ret;
  675. }
  676. const struct sysfs_ops kobj_sysfs_ops = {
  677. .show = kobj_attr_show,
  678. .store = kobj_attr_store,
  679. };
  680. /**
  681. * kobj_completion_init - initialize a kobj_completion object.
  682. * @kc: kobj_completion
  683. * @ktype: type of kobject to initialize
  684. *
  685. * kobj_completion structures can be embedded within structures with different
  686. * lifetime rules. During the release of the enclosing object, we can
  687. * wait on the release of the kobject so that we don't free it while it's
  688. * still busy.
  689. */
  690. void kobj_completion_init(struct kobj_completion *kc, struct kobj_type *ktype)
  691. {
  692. init_completion(&kc->kc_unregister);
  693. kobject_init(&kc->kc_kobj, ktype);
  694. }
  695. EXPORT_SYMBOL_GPL(kobj_completion_init);
  696. /**
  697. * kobj_completion_release - release a kobj_completion object
  698. * @kobj: kobject embedded in kobj_completion
  699. *
  700. * Used with kobject_release to notify waiters that the kobject has been
  701. * released.
  702. */
  703. void kobj_completion_release(struct kobject *kobj)
  704. {
  705. struct kobj_completion *kc = kobj_to_kobj_completion(kobj);
  706. complete(&kc->kc_unregister);
  707. }
  708. EXPORT_SYMBOL_GPL(kobj_completion_release);
  709. /**
  710. * kobj_completion_del_and_wait - release the kobject and wait for it
  711. * @kc: kobj_completion object to release
  712. *
  713. * Delete the kobject from sysfs and drop the reference count. Then wait
  714. * until any other outstanding references are also dropped. This routine
  715. * is only necessary once other references may have been taken on the
  716. * kobject. Typically this happens when the kobject has been published
  717. * to sysfs via kobject_add.
  718. */
  719. void kobj_completion_del_and_wait(struct kobj_completion *kc)
  720. {
  721. kobject_del(&kc->kc_kobj);
  722. kobject_put(&kc->kc_kobj);
  723. wait_for_completion(&kc->kc_unregister);
  724. }
  725. EXPORT_SYMBOL_GPL(kobj_completion_del_and_wait);
  726. /**
  727. * kset_register - initialize and add a kset.
  728. * @k: kset.
  729. */
  730. int kset_register(struct kset *k)
  731. {
  732. int err;
  733. if (!k)
  734. return -EINVAL;
  735. kset_init(k);
  736. err = kobject_add_internal(&k->kobj);
  737. if (err)
  738. return err;
  739. kobject_uevent(&k->kobj, KOBJ_ADD);
  740. return 0;
  741. }
  742. /**
  743. * kset_unregister - remove a kset.
  744. * @k: kset.
  745. */
  746. void kset_unregister(struct kset *k)
  747. {
  748. if (!k)
  749. return;
  750. kobject_put(&k->kobj);
  751. }
  752. /**
  753. * kset_find_obj - search for object in kset.
  754. * @kset: kset we're looking in.
  755. * @name: object's name.
  756. *
  757. * Lock kset via @kset->subsys, and iterate over @kset->list,
  758. * looking for a matching kobject. If matching object is found
  759. * take a reference and return the object.
  760. */
  761. struct kobject *kset_find_obj(struct kset *kset, const char *name)
  762. {
  763. struct kobject *k;
  764. struct kobject *ret = NULL;
  765. spin_lock(&kset->list_lock);
  766. list_for_each_entry(k, &kset->list, entry) {
  767. if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
  768. ret = kobject_get_unless_zero(k);
  769. break;
  770. }
  771. }
  772. spin_unlock(&kset->list_lock);
  773. return ret;
  774. }
  775. static void kset_release(struct kobject *kobj)
  776. {
  777. struct kset *kset = container_of(kobj, struct kset, kobj);
  778. pr_debug("kobject: '%s' (%p): %s\n",
  779. kobject_name(kobj), kobj, __func__);
  780. kfree(kset);
  781. }
  782. static struct kobj_type kset_ktype = {
  783. .sysfs_ops = &kobj_sysfs_ops,
  784. .release = kset_release,
  785. };
  786. /**
  787. * kset_create - create a struct kset dynamically
  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. This structure can
  794. * then be registered with the system and show up in sysfs with a call to
  795. * kset_register(). When you are finished with this structure, if
  796. * kset_register() has been called, call kset_unregister() and the
  797. * structure will be dynamically freed when it is no longer being used.
  798. *
  799. * If the kset was not able to be created, NULL will be returned.
  800. */
  801. static struct kset *kset_create(const char *name,
  802. const struct kset_uevent_ops *uevent_ops,
  803. struct kobject *parent_kobj)
  804. {
  805. struct kset *kset;
  806. int retval;
  807. kset = kzalloc(sizeof(*kset), GFP_KERNEL);
  808. if (!kset)
  809. return NULL;
  810. retval = kobject_set_name(&kset->kobj, "%s", name);
  811. if (retval) {
  812. kfree(kset);
  813. return NULL;
  814. }
  815. kset->uevent_ops = uevent_ops;
  816. kset->kobj.parent = parent_kobj;
  817. /*
  818. * The kobject of this kset will have a type of kset_ktype and belong to
  819. * no kset itself. That way we can properly free it when it is
  820. * finished being used.
  821. */
  822. kset->kobj.ktype = &kset_ktype;
  823. kset->kobj.kset = NULL;
  824. return kset;
  825. }
  826. /**
  827. * kset_create_and_add - create a struct kset dynamically and add it to sysfs
  828. *
  829. * @name: the name for the kset
  830. * @uevent_ops: a struct kset_uevent_ops for the kset
  831. * @parent_kobj: the parent kobject of this kset, if any.
  832. *
  833. * This function creates a kset structure dynamically and registers it
  834. * with sysfs. When you are finished with this structure, call
  835. * kset_unregister() and the structure will be dynamically freed when it
  836. * is no longer being used.
  837. *
  838. * If the kset was not able to be created, NULL will be returned.
  839. */
  840. struct kset *kset_create_and_add(const char *name,
  841. const struct kset_uevent_ops *uevent_ops,
  842. struct kobject *parent_kobj)
  843. {
  844. struct kset *kset;
  845. int error;
  846. kset = kset_create(name, uevent_ops, parent_kobj);
  847. if (!kset)
  848. return NULL;
  849. error = kset_register(kset);
  850. if (error) {
  851. kfree(kset);
  852. return NULL;
  853. }
  854. return kset;
  855. }
  856. EXPORT_SYMBOL_GPL(kset_create_and_add);
  857. static DEFINE_SPINLOCK(kobj_ns_type_lock);
  858. static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
  859. int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
  860. {
  861. enum kobj_ns_type type = ops->type;
  862. int error;
  863. spin_lock(&kobj_ns_type_lock);
  864. error = -EINVAL;
  865. if (type >= KOBJ_NS_TYPES)
  866. goto out;
  867. error = -EINVAL;
  868. if (type <= KOBJ_NS_TYPE_NONE)
  869. goto out;
  870. error = -EBUSY;
  871. if (kobj_ns_ops_tbl[type])
  872. goto out;
  873. error = 0;
  874. kobj_ns_ops_tbl[type] = ops;
  875. out:
  876. spin_unlock(&kobj_ns_type_lock);
  877. return error;
  878. }
  879. int kobj_ns_type_registered(enum kobj_ns_type type)
  880. {
  881. int registered = 0;
  882. spin_lock(&kobj_ns_type_lock);
  883. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES))
  884. registered = kobj_ns_ops_tbl[type] != NULL;
  885. spin_unlock(&kobj_ns_type_lock);
  886. return registered;
  887. }
  888. const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
  889. {
  890. const struct kobj_ns_type_operations *ops = NULL;
  891. if (parent && parent->ktype->child_ns_type)
  892. ops = parent->ktype->child_ns_type(parent);
  893. return ops;
  894. }
  895. const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
  896. {
  897. return kobj_child_ns_ops(kobj->parent);
  898. }
  899. bool kobj_ns_current_may_mount(enum kobj_ns_type type)
  900. {
  901. bool may_mount = true;
  902. spin_lock(&kobj_ns_type_lock);
  903. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  904. kobj_ns_ops_tbl[type])
  905. may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
  906. spin_unlock(&kobj_ns_type_lock);
  907. return may_mount;
  908. }
  909. void *kobj_ns_grab_current(enum kobj_ns_type type)
  910. {
  911. void *ns = NULL;
  912. spin_lock(&kobj_ns_type_lock);
  913. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  914. kobj_ns_ops_tbl[type])
  915. ns = kobj_ns_ops_tbl[type]->grab_current_ns();
  916. spin_unlock(&kobj_ns_type_lock);
  917. return ns;
  918. }
  919. const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
  920. {
  921. const void *ns = NULL;
  922. spin_lock(&kobj_ns_type_lock);
  923. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  924. kobj_ns_ops_tbl[type])
  925. ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
  926. spin_unlock(&kobj_ns_type_lock);
  927. return ns;
  928. }
  929. const void *kobj_ns_initial(enum kobj_ns_type type)
  930. {
  931. const void *ns = NULL;
  932. spin_lock(&kobj_ns_type_lock);
  933. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  934. kobj_ns_ops_tbl[type])
  935. ns = kobj_ns_ops_tbl[type]->initial_ns();
  936. spin_unlock(&kobj_ns_type_lock);
  937. return ns;
  938. }
  939. void kobj_ns_drop(enum kobj_ns_type type, void *ns)
  940. {
  941. spin_lock(&kobj_ns_type_lock);
  942. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  943. kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
  944. kobj_ns_ops_tbl[type]->drop_ns(ns);
  945. spin_unlock(&kobj_ns_type_lock);
  946. }
  947. EXPORT_SYMBOL(kobject_get);
  948. EXPORT_SYMBOL(kobject_put);
  949. EXPORT_SYMBOL(kobject_del);
  950. EXPORT_SYMBOL(kset_register);
  951. EXPORT_SYMBOL(kset_unregister);