kobject.c 26 KB

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