kobject.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /*
  2. * kobject.c - library routines for handling generic kernel objects
  3. *
  4. * Copyright (c) 2002-2003 Patrick Mochel <[email protected]>
  5. * Copyright (c) 2006-2007 Greg Kroah-Hartman <[email protected]>
  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. memcpy(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. struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
  526. {
  527. if (!kobj)
  528. return NULL;
  529. if (!kref_get_unless_zero(&kobj->kref))
  530. kobj = NULL;
  531. return kobj;
  532. }
  533. EXPORT_SYMBOL(kobject_get_unless_zero);
  534. /*
  535. * kobject_cleanup - free kobject resources.
  536. * @kobj: object to cleanup
  537. */
  538. static void kobject_cleanup(struct kobject *kobj)
  539. {
  540. struct kobj_type *t = get_ktype(kobj);
  541. const char *name = kobj->name;
  542. pr_debug("kobject: '%s' (%p): %s, parent %p\n",
  543. kobject_name(kobj), kobj, __func__, kobj->parent);
  544. if (t && !t->release)
  545. pr_debug("kobject: '%s' (%p): does not have a release() "
  546. "function, it is broken and must be fixed.\n",
  547. kobject_name(kobj), kobj);
  548. /* send "remove" if the caller did not do it but sent "add" */
  549. if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
  550. pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
  551. kobject_name(kobj), kobj);
  552. kobject_uevent(kobj, KOBJ_REMOVE);
  553. }
  554. /* remove from sysfs if the caller did not do it */
  555. if (kobj->state_in_sysfs) {
  556. pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n",
  557. kobject_name(kobj), kobj);
  558. kobject_del(kobj);
  559. }
  560. if (t && t->release) {
  561. pr_debug("kobject: '%s' (%p): calling ktype release\n",
  562. kobject_name(kobj), kobj);
  563. t->release(kobj);
  564. }
  565. /* free name if we allocated it */
  566. if (name) {
  567. pr_debug("kobject: '%s': free name\n", name);
  568. kfree_const(name);
  569. }
  570. }
  571. #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
  572. static void kobject_delayed_cleanup(struct work_struct *work)
  573. {
  574. kobject_cleanup(container_of(to_delayed_work(work),
  575. struct kobject, release));
  576. }
  577. #endif
  578. static void kobject_release(struct kref *kref)
  579. {
  580. struct kobject *kobj = container_of(kref, struct kobject, kref);
  581. #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
  582. unsigned long delay = HZ + HZ * (get_random_int() & 0x3);
  583. pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n",
  584. kobject_name(kobj), kobj, __func__, kobj->parent, delay);
  585. INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
  586. schedule_delayed_work(&kobj->release, delay);
  587. #else
  588. kobject_cleanup(kobj);
  589. #endif
  590. }
  591. /**
  592. * kobject_put - decrement refcount for object.
  593. * @kobj: object.
  594. *
  595. * Decrement the refcount, and if 0, call kobject_cleanup().
  596. */
  597. void kobject_put(struct kobject *kobj)
  598. {
  599. if (kobj) {
  600. if (!kobj->state_initialized)
  601. WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
  602. "initialized, yet kobject_put() is being "
  603. "called.\n", kobject_name(kobj), kobj);
  604. kref_put(&kobj->kref, kobject_release);
  605. }
  606. }
  607. EXPORT_SYMBOL(kobject_put);
  608. static void dynamic_kobj_release(struct kobject *kobj)
  609. {
  610. pr_debug("kobject: (%p): %s\n", kobj, __func__);
  611. kfree(kobj);
  612. }
  613. static struct kobj_type dynamic_kobj_ktype = {
  614. .release = dynamic_kobj_release,
  615. .sysfs_ops = &kobj_sysfs_ops,
  616. };
  617. /**
  618. * kobject_create - create a struct kobject dynamically
  619. *
  620. * This function creates a kobject structure dynamically and sets it up
  621. * to be a "dynamic" kobject with a default release function set up.
  622. *
  623. * If the kobject was not able to be created, NULL will be returned.
  624. * The kobject structure returned from here must be cleaned up with a
  625. * call to kobject_put() and not kfree(), as kobject_init() has
  626. * already been called on this structure.
  627. */
  628. struct kobject *kobject_create(void)
  629. {
  630. struct kobject *kobj;
  631. kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
  632. if (!kobj)
  633. return NULL;
  634. kobject_init(kobj, &dynamic_kobj_ktype);
  635. return kobj;
  636. }
  637. /**
  638. * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
  639. *
  640. * @name: the name for the kobject
  641. * @parent: the parent kobject of this kobject, if any.
  642. *
  643. * This function creates a kobject structure dynamically and registers it
  644. * with sysfs. When you are finished with this structure, call
  645. * kobject_put() and the structure will be dynamically freed when
  646. * it is no longer being used.
  647. *
  648. * If the kobject was not able to be created, NULL will be returned.
  649. */
  650. struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
  651. {
  652. struct kobject *kobj;
  653. int retval;
  654. kobj = kobject_create();
  655. if (!kobj)
  656. return NULL;
  657. retval = kobject_add(kobj, parent, "%s", name);
  658. if (retval) {
  659. printk(KERN_WARNING "%s: kobject_add error: %d\n",
  660. __func__, retval);
  661. kobject_put(kobj);
  662. kobj = NULL;
  663. }
  664. return kobj;
  665. }
  666. EXPORT_SYMBOL_GPL(kobject_create_and_add);
  667. /**
  668. * kset_init - initialize a kset for use
  669. * @k: kset
  670. */
  671. void kset_init(struct kset *k)
  672. {
  673. kobject_init_internal(&k->kobj);
  674. INIT_LIST_HEAD(&k->list);
  675. spin_lock_init(&k->list_lock);
  676. }
  677. /* default kobject attribute operations */
  678. static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
  679. char *buf)
  680. {
  681. struct kobj_attribute *kattr;
  682. ssize_t ret = -EIO;
  683. kattr = container_of(attr, struct kobj_attribute, attr);
  684. if (kattr->show)
  685. ret = kattr->show(kobj, kattr, buf);
  686. return ret;
  687. }
  688. static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
  689. const char *buf, size_t count)
  690. {
  691. struct kobj_attribute *kattr;
  692. ssize_t ret = -EIO;
  693. kattr = container_of(attr, struct kobj_attribute, attr);
  694. if (kattr->store)
  695. ret = kattr->store(kobj, kattr, buf, count);
  696. return ret;
  697. }
  698. const struct sysfs_ops kobj_sysfs_ops = {
  699. .show = kobj_attr_show,
  700. .store = kobj_attr_store,
  701. };
  702. EXPORT_SYMBOL_GPL(kobj_sysfs_ops);
  703. /**
  704. * kset_register - initialize and add a kset.
  705. * @k: kset.
  706. */
  707. int kset_register(struct kset *k)
  708. {
  709. int err;
  710. if (!k)
  711. return -EINVAL;
  712. kset_init(k);
  713. err = kobject_add_internal(&k->kobj);
  714. if (err)
  715. return err;
  716. kobject_uevent(&k->kobj, KOBJ_ADD);
  717. return 0;
  718. }
  719. EXPORT_SYMBOL(kset_register);
  720. /**
  721. * kset_unregister - remove a kset.
  722. * @k: kset.
  723. */
  724. void kset_unregister(struct kset *k)
  725. {
  726. if (!k)
  727. return;
  728. kobject_del(&k->kobj);
  729. kobject_put(&k->kobj);
  730. }
  731. EXPORT_SYMBOL(kset_unregister);
  732. /**
  733. * kset_find_obj - search for object in kset.
  734. * @kset: kset we're looking in.
  735. * @name: object's name.
  736. *
  737. * Lock kset via @kset->subsys, and iterate over @kset->list,
  738. * looking for a matching kobject. If matching object is found
  739. * take a reference and return the object.
  740. */
  741. struct kobject *kset_find_obj(struct kset *kset, const char *name)
  742. {
  743. struct kobject *k;
  744. struct kobject *ret = NULL;
  745. spin_lock(&kset->list_lock);
  746. list_for_each_entry(k, &kset->list, entry) {
  747. if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
  748. ret = kobject_get_unless_zero(k);
  749. break;
  750. }
  751. }
  752. spin_unlock(&kset->list_lock);
  753. return ret;
  754. }
  755. EXPORT_SYMBOL_GPL(kset_find_obj);
  756. static void kset_release(struct kobject *kobj)
  757. {
  758. struct kset *kset = container_of(kobj, struct kset, kobj);
  759. pr_debug("kobject: '%s' (%p): %s\n",
  760. kobject_name(kobj), kobj, __func__);
  761. kfree(kset);
  762. }
  763. static struct kobj_type kset_ktype = {
  764. .sysfs_ops = &kobj_sysfs_ops,
  765. .release = kset_release,
  766. };
  767. /**
  768. * kset_create - create a struct kset dynamically
  769. *
  770. * @name: the name for the kset
  771. * @uevent_ops: a struct kset_uevent_ops for the kset
  772. * @parent_kobj: the parent kobject of this kset, if any.
  773. *
  774. * This function creates a kset structure dynamically. This structure can
  775. * then be registered with the system and show up in sysfs with a call to
  776. * kset_register(). When you are finished with this structure, if
  777. * kset_register() has been called, call kset_unregister() and the
  778. * structure will be dynamically freed when it is no longer being used.
  779. *
  780. * If the kset was not able to be created, NULL will be returned.
  781. */
  782. static struct kset *kset_create(const char *name,
  783. const struct kset_uevent_ops *uevent_ops,
  784. struct kobject *parent_kobj)
  785. {
  786. struct kset *kset;
  787. int retval;
  788. kset = kzalloc(sizeof(*kset), GFP_KERNEL);
  789. if (!kset)
  790. return NULL;
  791. retval = kobject_set_name(&kset->kobj, "%s", name);
  792. if (retval) {
  793. kfree(kset);
  794. return NULL;
  795. }
  796. kset->uevent_ops = uevent_ops;
  797. kset->kobj.parent = parent_kobj;
  798. /*
  799. * The kobject of this kset will have a type of kset_ktype and belong to
  800. * no kset itself. That way we can properly free it when it is
  801. * finished being used.
  802. */
  803. kset->kobj.ktype = &kset_ktype;
  804. kset->kobj.kset = NULL;
  805. return kset;
  806. }
  807. /**
  808. * kset_create_and_add - create a struct kset dynamically and add it to sysfs
  809. *
  810. * @name: the name for the kset
  811. * @uevent_ops: a struct kset_uevent_ops for the kset
  812. * @parent_kobj: the parent kobject of this kset, if any.
  813. *
  814. * This function creates a kset structure dynamically and registers it
  815. * with sysfs. When you are finished with this structure, call
  816. * kset_unregister() and the structure will be dynamically freed when it
  817. * is no longer being used.
  818. *
  819. * If the kset was not able to be created, NULL will be returned.
  820. */
  821. struct kset *kset_create_and_add(const char *name,
  822. const struct kset_uevent_ops *uevent_ops,
  823. struct kobject *parent_kobj)
  824. {
  825. struct kset *kset;
  826. int error;
  827. kset = kset_create(name, uevent_ops, parent_kobj);
  828. if (!kset)
  829. return NULL;
  830. error = kset_register(kset);
  831. if (error) {
  832. kfree(kset);
  833. return NULL;
  834. }
  835. return kset;
  836. }
  837. EXPORT_SYMBOL_GPL(kset_create_and_add);
  838. static DEFINE_SPINLOCK(kobj_ns_type_lock);
  839. static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
  840. int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
  841. {
  842. enum kobj_ns_type type = ops->type;
  843. int error;
  844. spin_lock(&kobj_ns_type_lock);
  845. error = -EINVAL;
  846. if (type >= KOBJ_NS_TYPES)
  847. goto out;
  848. error = -EINVAL;
  849. if (type <= KOBJ_NS_TYPE_NONE)
  850. goto out;
  851. error = -EBUSY;
  852. if (kobj_ns_ops_tbl[type])
  853. goto out;
  854. error = 0;
  855. kobj_ns_ops_tbl[type] = ops;
  856. out:
  857. spin_unlock(&kobj_ns_type_lock);
  858. return error;
  859. }
  860. int kobj_ns_type_registered(enum kobj_ns_type type)
  861. {
  862. int registered = 0;
  863. spin_lock(&kobj_ns_type_lock);
  864. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES))
  865. registered = kobj_ns_ops_tbl[type] != NULL;
  866. spin_unlock(&kobj_ns_type_lock);
  867. return registered;
  868. }
  869. const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
  870. {
  871. const struct kobj_ns_type_operations *ops = NULL;
  872. if (parent && parent->ktype && parent->ktype->child_ns_type)
  873. ops = parent->ktype->child_ns_type(parent);
  874. return ops;
  875. }
  876. const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
  877. {
  878. return kobj_child_ns_ops(kobj->parent);
  879. }
  880. bool kobj_ns_current_may_mount(enum kobj_ns_type type)
  881. {
  882. bool may_mount = true;
  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. may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
  887. spin_unlock(&kobj_ns_type_lock);
  888. return may_mount;
  889. }
  890. void *kobj_ns_grab_current(enum kobj_ns_type type)
  891. {
  892. 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]->grab_current_ns();
  897. spin_unlock(&kobj_ns_type_lock);
  898. return ns;
  899. }
  900. const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
  901. {
  902. const void *ns = NULL;
  903. spin_lock(&kobj_ns_type_lock);
  904. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  905. kobj_ns_ops_tbl[type])
  906. ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
  907. spin_unlock(&kobj_ns_type_lock);
  908. return ns;
  909. }
  910. const void *kobj_ns_initial(enum kobj_ns_type type)
  911. {
  912. const void *ns = NULL;
  913. spin_lock(&kobj_ns_type_lock);
  914. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  915. kobj_ns_ops_tbl[type])
  916. ns = kobj_ns_ops_tbl[type]->initial_ns();
  917. spin_unlock(&kobj_ns_type_lock);
  918. return ns;
  919. }
  920. void kobj_ns_drop(enum kobj_ns_type type, void *ns)
  921. {
  922. spin_lock(&kobj_ns_type_lock);
  923. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  924. kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
  925. kobj_ns_ops_tbl[type]->drop_ns(ns);
  926. spin_unlock(&kobj_ns_type_lock);
  927. }