kobject.c 27 KB

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