kobject.c 27 KB

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