kobject.c 25 KB

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