kobject.c 26 KB

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