kobject.c 27 KB

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