kobject.c 26 KB

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