kobject.c 25 KB

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