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