kobject.c 26 KB

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