kobject.c 26 KB

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