kobject.c 27 KB

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