kobject.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. kernfs_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 *old_name = kobj->name;
  224. char *s;
  225. if (kobj->name && !fmt)
  226. return 0;
  227. kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
  228. if (!kobj->name) {
  229. kobj->name = old_name;
  230. return -ENOMEM;
  231. }
  232. /* ewww... some of these buggers have '/' in the name ... */
  233. while ((s = strchr(kobj->name, '/')))
  234. s[0] = '!';
  235. kfree(old_name);
  236. return 0;
  237. }
  238. /**
  239. * kobject_set_name - Set the name of a kobject
  240. * @kobj: struct kobject to set the name of
  241. * @fmt: format string used to build the name
  242. *
  243. * This sets the name of the kobject. If you have already added the
  244. * kobject to the system, you must call kobject_rename() in order to
  245. * change the name of the kobject.
  246. */
  247. int kobject_set_name(struct kobject *kobj, const char *fmt, ...)
  248. {
  249. va_list vargs;
  250. int retval;
  251. va_start(vargs, fmt);
  252. retval = kobject_set_name_vargs(kobj, fmt, vargs);
  253. va_end(vargs);
  254. return retval;
  255. }
  256. EXPORT_SYMBOL(kobject_set_name);
  257. /**
  258. * kobject_init - initialize a kobject structure
  259. * @kobj: pointer to the kobject to initialize
  260. * @ktype: pointer to the ktype for this kobject.
  261. *
  262. * This function will properly initialize a kobject such that it can then
  263. * be passed to the kobject_add() call.
  264. *
  265. * After this function is called, the kobject MUST be cleaned up by a call
  266. * to kobject_put(), not by a call to kfree directly to ensure that all of
  267. * the memory is cleaned up properly.
  268. */
  269. void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
  270. {
  271. char *err_str;
  272. if (!kobj) {
  273. err_str = "invalid kobject pointer!";
  274. goto error;
  275. }
  276. if (!ktype) {
  277. err_str = "must have a ktype to be initialized properly!\n";
  278. goto error;
  279. }
  280. if (kobj->state_initialized) {
  281. /* do not error out as sometimes we can recover */
  282. printk(KERN_ERR "kobject (%p): tried to init an initialized "
  283. "object, something is seriously wrong.\n", kobj);
  284. dump_stack();
  285. }
  286. kobject_init_internal(kobj);
  287. kobj->ktype = ktype;
  288. return;
  289. error:
  290. printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);
  291. dump_stack();
  292. }
  293. EXPORT_SYMBOL(kobject_init);
  294. static int kobject_add_varg(struct kobject *kobj, struct kobject *parent,
  295. const char *fmt, va_list vargs)
  296. {
  297. int retval;
  298. retval = kobject_set_name_vargs(kobj, fmt, vargs);
  299. if (retval) {
  300. printk(KERN_ERR "kobject: can not set name properly!\n");
  301. return retval;
  302. }
  303. kobj->parent = parent;
  304. return kobject_add_internal(kobj);
  305. }
  306. /**
  307. * kobject_add - the main kobject add function
  308. * @kobj: the kobject to add
  309. * @parent: pointer to the parent of the kobject.
  310. * @fmt: format to name the kobject with.
  311. *
  312. * The kobject name is set and added to the kobject hierarchy in this
  313. * function.
  314. *
  315. * If @parent is set, then the parent of the @kobj will be set to it.
  316. * If @parent is NULL, then the parent of the @kobj will be set to the
  317. * kobject associated with the kset assigned to this kobject. If no kset
  318. * is assigned to the kobject, then the kobject will be located in the
  319. * root of the sysfs tree.
  320. *
  321. * If this function returns an error, kobject_put() must be called to
  322. * properly clean up the memory associated with the object.
  323. * Under no instance should the kobject that is passed to this function
  324. * be directly freed with a call to kfree(), that can leak memory.
  325. *
  326. * Note, no "add" uevent will be created with this call, the caller should set
  327. * up all of the necessary sysfs files for the object and then call
  328. * kobject_uevent() with the UEVENT_ADD parameter to ensure that
  329. * userspace is properly notified of this kobject's creation.
  330. */
  331. int kobject_add(struct kobject *kobj, struct kobject *parent,
  332. const char *fmt, ...)
  333. {
  334. va_list args;
  335. int retval;
  336. if (!kobj)
  337. return -EINVAL;
  338. if (!kobj->state_initialized) {
  339. printk(KERN_ERR "kobject '%s' (%p): tried to add an "
  340. "uninitialized object, something is seriously wrong.\n",
  341. kobject_name(kobj), kobj);
  342. dump_stack();
  343. return -EINVAL;
  344. }
  345. va_start(args, fmt);
  346. retval = kobject_add_varg(kobj, parent, fmt, args);
  347. va_end(args);
  348. return retval;
  349. }
  350. EXPORT_SYMBOL(kobject_add);
  351. /**
  352. * kobject_init_and_add - initialize a kobject structure and add it to the kobject hierarchy
  353. * @kobj: pointer to the kobject to initialize
  354. * @ktype: pointer to the ktype for this kobject.
  355. * @parent: pointer to the parent of this kobject.
  356. * @fmt: the name of the kobject.
  357. *
  358. * This function combines the call to kobject_init() and
  359. * kobject_add(). The same type of error handling after a call to
  360. * kobject_add() and kobject lifetime rules are the same here.
  361. */
  362. int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
  363. struct kobject *parent, const char *fmt, ...)
  364. {
  365. va_list args;
  366. int retval;
  367. kobject_init(kobj, ktype);
  368. va_start(args, fmt);
  369. retval = kobject_add_varg(kobj, parent, fmt, args);
  370. va_end(args);
  371. return retval;
  372. }
  373. EXPORT_SYMBOL_GPL(kobject_init_and_add);
  374. /**
  375. * kobject_rename - change the name of an object
  376. * @kobj: object in question.
  377. * @new_name: object's new name
  378. *
  379. * It is the responsibility of the caller to provide mutual
  380. * exclusion between two different calls of kobject_rename
  381. * on the same kobject and to ensure that new_name is valid and
  382. * won't conflict with other kobjects.
  383. */
  384. int kobject_rename(struct kobject *kobj, const char *new_name)
  385. {
  386. int error = 0;
  387. const char *devpath = NULL;
  388. const char *dup_name = NULL, *name;
  389. char *devpath_string = NULL;
  390. char *envp[2];
  391. kobj = kobject_get(kobj);
  392. if (!kobj)
  393. return -EINVAL;
  394. if (!kobj->parent)
  395. return -EINVAL;
  396. devpath = kobject_get_path(kobj, GFP_KERNEL);
  397. if (!devpath) {
  398. error = -ENOMEM;
  399. goto out;
  400. }
  401. devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
  402. if (!devpath_string) {
  403. error = -ENOMEM;
  404. goto out;
  405. }
  406. sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
  407. envp[0] = devpath_string;
  408. envp[1] = NULL;
  409. name = dup_name = kstrdup(new_name, GFP_KERNEL);
  410. if (!name) {
  411. error = -ENOMEM;
  412. goto out;
  413. }
  414. error = sysfs_rename_dir_ns(kobj, new_name, kobject_namespace(kobj));
  415. if (error)
  416. goto out;
  417. /* Install the new kobject name */
  418. dup_name = kobj->name;
  419. kobj->name = name;
  420. /* This function is mostly/only used for network interface.
  421. * Some hotplug package track interfaces by their name and
  422. * therefore want to know when the name is changed by the user. */
  423. kobject_uevent_env(kobj, KOBJ_MOVE, envp);
  424. out:
  425. kfree(dup_name);
  426. kfree(devpath_string);
  427. kfree(devpath);
  428. kobject_put(kobj);
  429. return error;
  430. }
  431. EXPORT_SYMBOL_GPL(kobject_rename);
  432. /**
  433. * kobject_move - move object to another parent
  434. * @kobj: object in question.
  435. * @new_parent: object's new parent (can be NULL)
  436. */
  437. int kobject_move(struct kobject *kobj, struct kobject *new_parent)
  438. {
  439. int error;
  440. struct kobject *old_parent;
  441. const char *devpath = NULL;
  442. char *devpath_string = NULL;
  443. char *envp[2];
  444. kobj = kobject_get(kobj);
  445. if (!kobj)
  446. return -EINVAL;
  447. new_parent = kobject_get(new_parent);
  448. if (!new_parent) {
  449. if (kobj->kset)
  450. new_parent = kobject_get(&kobj->kset->kobj);
  451. }
  452. /* old object path */
  453. devpath = kobject_get_path(kobj, GFP_KERNEL);
  454. if (!devpath) {
  455. error = -ENOMEM;
  456. goto out;
  457. }
  458. devpath_string = kmalloc(strlen(devpath) + 15, GFP_KERNEL);
  459. if (!devpath_string) {
  460. error = -ENOMEM;
  461. goto out;
  462. }
  463. sprintf(devpath_string, "DEVPATH_OLD=%s", devpath);
  464. envp[0] = devpath_string;
  465. envp[1] = NULL;
  466. error = sysfs_move_dir_ns(kobj, new_parent, kobject_namespace(kobj));
  467. if (error)
  468. goto out;
  469. old_parent = kobj->parent;
  470. kobj->parent = new_parent;
  471. new_parent = NULL;
  472. kobject_put(old_parent);
  473. kobject_uevent_env(kobj, KOBJ_MOVE, envp);
  474. out:
  475. kobject_put(new_parent);
  476. kobject_put(kobj);
  477. kfree(devpath_string);
  478. kfree(devpath);
  479. return error;
  480. }
  481. /**
  482. * kobject_del - unlink kobject from hierarchy.
  483. * @kobj: object.
  484. */
  485. void kobject_del(struct kobject *kobj)
  486. {
  487. struct kernfs_node *sd;
  488. if (!kobj)
  489. return;
  490. sd = kobj->sd;
  491. sysfs_remove_dir(kobj);
  492. sysfs_put(sd);
  493. kobj->state_in_sysfs = 0;
  494. kobj_kset_leave(kobj);
  495. kobject_put(kobj->parent);
  496. kobj->parent = NULL;
  497. }
  498. /**
  499. * kobject_get - increment refcount for object.
  500. * @kobj: object.
  501. */
  502. struct kobject *kobject_get(struct kobject *kobj)
  503. {
  504. if (kobj)
  505. kref_get(&kobj->kref);
  506. return kobj;
  507. }
  508. static struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
  509. {
  510. if (!kref_get_unless_zero(&kobj->kref))
  511. kobj = NULL;
  512. return kobj;
  513. }
  514. /*
  515. * kobject_cleanup - free kobject resources.
  516. * @kobj: object to cleanup
  517. */
  518. static void kobject_cleanup(struct kobject *kobj)
  519. {
  520. struct kobj_type *t = get_ktype(kobj);
  521. const char *name = kobj->name;
  522. pr_debug("kobject: '%s' (%p): %s, parent %p\n",
  523. kobject_name(kobj), kobj, __func__, kobj->parent);
  524. if (t && !t->release)
  525. pr_debug("kobject: '%s' (%p): does not have a release() "
  526. "function, it is broken and must be fixed.\n",
  527. kobject_name(kobj), kobj);
  528. /* send "remove" if the caller did not do it but sent "add" */
  529. if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
  530. pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
  531. kobject_name(kobj), kobj);
  532. kobject_uevent(kobj, KOBJ_REMOVE);
  533. }
  534. /* remove from sysfs if the caller did not do it */
  535. if (kobj->state_in_sysfs) {
  536. pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n",
  537. kobject_name(kobj), kobj);
  538. kobject_del(kobj);
  539. }
  540. if (t && t->release) {
  541. pr_debug("kobject: '%s' (%p): calling ktype release\n",
  542. kobject_name(kobj), kobj);
  543. t->release(kobj);
  544. }
  545. /* free name if we allocated it */
  546. if (name) {
  547. pr_debug("kobject: '%s': free name\n", name);
  548. kfree(name);
  549. }
  550. }
  551. #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
  552. static void kobject_delayed_cleanup(struct work_struct *work)
  553. {
  554. kobject_cleanup(container_of(to_delayed_work(work),
  555. struct kobject, release));
  556. }
  557. #endif
  558. static void kobject_release(struct kref *kref)
  559. {
  560. struct kobject *kobj = container_of(kref, struct kobject, kref);
  561. #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
  562. unsigned long delay = HZ + HZ * (get_random_int() & 0x3);
  563. pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n",
  564. kobject_name(kobj), kobj, __func__, kobj->parent, delay);
  565. INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
  566. schedule_delayed_work(&kobj->release, delay);
  567. #else
  568. kobject_cleanup(kobj);
  569. #endif
  570. }
  571. /**
  572. * kobject_put - decrement refcount for object.
  573. * @kobj: object.
  574. *
  575. * Decrement the refcount, and if 0, call kobject_cleanup().
  576. */
  577. void kobject_put(struct kobject *kobj)
  578. {
  579. if (kobj) {
  580. if (!kobj->state_initialized)
  581. WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
  582. "initialized, yet kobject_put() is being "
  583. "called.\n", kobject_name(kobj), kobj);
  584. kref_put(&kobj->kref, kobject_release);
  585. }
  586. }
  587. static void dynamic_kobj_release(struct kobject *kobj)
  588. {
  589. pr_debug("kobject: (%p): %s\n", kobj, __func__);
  590. kfree(kobj);
  591. }
  592. static struct kobj_type dynamic_kobj_ktype = {
  593. .release = dynamic_kobj_release,
  594. .sysfs_ops = &kobj_sysfs_ops,
  595. };
  596. /**
  597. * kobject_create - create a struct kobject dynamically
  598. *
  599. * This function creates a kobject structure dynamically and sets it up
  600. * to be a "dynamic" kobject with a default release function set up.
  601. *
  602. * If the kobject was not able to be created, NULL will be returned.
  603. * The kobject structure returned from here must be cleaned up with a
  604. * call to kobject_put() and not kfree(), as kobject_init() has
  605. * already been called on this structure.
  606. */
  607. struct kobject *kobject_create(void)
  608. {
  609. struct kobject *kobj;
  610. kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
  611. if (!kobj)
  612. return NULL;
  613. kobject_init(kobj, &dynamic_kobj_ktype);
  614. return kobj;
  615. }
  616. /**
  617. * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
  618. *
  619. * @name: the name for the kobject
  620. * @parent: the parent kobject of this kobject, if any.
  621. *
  622. * This function creates a kobject structure dynamically and registers it
  623. * with sysfs. When you are finished with this structure, call
  624. * kobject_put() and the structure will be dynamically freed when
  625. * it is no longer being used.
  626. *
  627. * If the kobject was not able to be created, NULL will be returned.
  628. */
  629. struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
  630. {
  631. struct kobject *kobj;
  632. int retval;
  633. kobj = kobject_create();
  634. if (!kobj)
  635. return NULL;
  636. retval = kobject_add(kobj, parent, "%s", name);
  637. if (retval) {
  638. printk(KERN_WARNING "%s: kobject_add error: %d\n",
  639. __func__, retval);
  640. kobject_put(kobj);
  641. kobj = NULL;
  642. }
  643. return kobj;
  644. }
  645. EXPORT_SYMBOL_GPL(kobject_create_and_add);
  646. /**
  647. * kset_init - initialize a kset for use
  648. * @k: kset
  649. */
  650. void kset_init(struct kset *k)
  651. {
  652. kobject_init_internal(&k->kobj);
  653. INIT_LIST_HEAD(&k->list);
  654. spin_lock_init(&k->list_lock);
  655. }
  656. /* default kobject attribute operations */
  657. static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
  658. char *buf)
  659. {
  660. struct kobj_attribute *kattr;
  661. ssize_t ret = -EIO;
  662. kattr = container_of(attr, struct kobj_attribute, attr);
  663. if (kattr->show)
  664. ret = kattr->show(kobj, kattr, buf);
  665. return ret;
  666. }
  667. static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
  668. const char *buf, size_t count)
  669. {
  670. struct kobj_attribute *kattr;
  671. ssize_t ret = -EIO;
  672. kattr = container_of(attr, struct kobj_attribute, attr);
  673. if (kattr->store)
  674. ret = kattr->store(kobj, kattr, buf, count);
  675. return ret;
  676. }
  677. const struct sysfs_ops kobj_sysfs_ops = {
  678. .show = kobj_attr_show,
  679. .store = kobj_attr_store,
  680. };
  681. /**
  682. * kset_register - initialize and add a kset.
  683. * @k: kset.
  684. */
  685. int kset_register(struct kset *k)
  686. {
  687. int err;
  688. if (!k)
  689. return -EINVAL;
  690. kset_init(k);
  691. err = kobject_add_internal(&k->kobj);
  692. if (err)
  693. return err;
  694. kobject_uevent(&k->kobj, KOBJ_ADD);
  695. return 0;
  696. }
  697. /**
  698. * kset_unregister - remove a kset.
  699. * @k: kset.
  700. */
  701. void kset_unregister(struct kset *k)
  702. {
  703. if (!k)
  704. return;
  705. kobject_del(&k->kobj);
  706. kobject_put(&k->kobj);
  707. }
  708. /**
  709. * kset_find_obj - search for object in kset.
  710. * @kset: kset we're looking in.
  711. * @name: object's name.
  712. *
  713. * Lock kset via @kset->subsys, and iterate over @kset->list,
  714. * looking for a matching kobject. If matching object is found
  715. * take a reference and return the object.
  716. */
  717. struct kobject *kset_find_obj(struct kset *kset, const char *name)
  718. {
  719. struct kobject *k;
  720. struct kobject *ret = NULL;
  721. spin_lock(&kset->list_lock);
  722. list_for_each_entry(k, &kset->list, entry) {
  723. if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
  724. ret = kobject_get_unless_zero(k);
  725. break;
  726. }
  727. }
  728. spin_unlock(&kset->list_lock);
  729. return ret;
  730. }
  731. static void kset_release(struct kobject *kobj)
  732. {
  733. struct kset *kset = container_of(kobj, struct kset, kobj);
  734. pr_debug("kobject: '%s' (%p): %s\n",
  735. kobject_name(kobj), kobj, __func__);
  736. kfree(kset);
  737. }
  738. static struct kobj_type kset_ktype = {
  739. .sysfs_ops = &kobj_sysfs_ops,
  740. .release = kset_release,
  741. };
  742. /**
  743. * kset_create - create a struct kset dynamically
  744. *
  745. * @name: the name for the kset
  746. * @uevent_ops: a struct kset_uevent_ops for the kset
  747. * @parent_kobj: the parent kobject of this kset, if any.
  748. *
  749. * This function creates a kset structure dynamically. This structure can
  750. * then be registered with the system and show up in sysfs with a call to
  751. * kset_register(). When you are finished with this structure, if
  752. * kset_register() has been called, call kset_unregister() and the
  753. * structure will be dynamically freed when it is no longer being used.
  754. *
  755. * If the kset was not able to be created, NULL will be returned.
  756. */
  757. static struct kset *kset_create(const char *name,
  758. const struct kset_uevent_ops *uevent_ops,
  759. struct kobject *parent_kobj)
  760. {
  761. struct kset *kset;
  762. int retval;
  763. kset = kzalloc(sizeof(*kset), GFP_KERNEL);
  764. if (!kset)
  765. return NULL;
  766. retval = kobject_set_name(&kset->kobj, "%s", name);
  767. if (retval) {
  768. kfree(kset);
  769. return NULL;
  770. }
  771. kset->uevent_ops = uevent_ops;
  772. kset->kobj.parent = parent_kobj;
  773. /*
  774. * The kobject of this kset will have a type of kset_ktype and belong to
  775. * no kset itself. That way we can properly free it when it is
  776. * finished being used.
  777. */
  778. kset->kobj.ktype = &kset_ktype;
  779. kset->kobj.kset = NULL;
  780. return kset;
  781. }
  782. /**
  783. * kset_create_and_add - create a struct kset dynamically and add it to sysfs
  784. *
  785. * @name: the name for the kset
  786. * @uevent_ops: a struct kset_uevent_ops for the kset
  787. * @parent_kobj: the parent kobject of this kset, if any.
  788. *
  789. * This function creates a kset structure dynamically and registers it
  790. * with sysfs. When you are finished with this structure, call
  791. * kset_unregister() and the structure will be dynamically freed when it
  792. * is no longer being used.
  793. *
  794. * If the kset was not able to be created, NULL will be returned.
  795. */
  796. struct kset *kset_create_and_add(const char *name,
  797. const struct kset_uevent_ops *uevent_ops,
  798. struct kobject *parent_kobj)
  799. {
  800. struct kset *kset;
  801. int error;
  802. kset = kset_create(name, uevent_ops, parent_kobj);
  803. if (!kset)
  804. return NULL;
  805. error = kset_register(kset);
  806. if (error) {
  807. kfree(kset);
  808. return NULL;
  809. }
  810. return kset;
  811. }
  812. EXPORT_SYMBOL_GPL(kset_create_and_add);
  813. static DEFINE_SPINLOCK(kobj_ns_type_lock);
  814. static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
  815. int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
  816. {
  817. enum kobj_ns_type type = ops->type;
  818. int error;
  819. spin_lock(&kobj_ns_type_lock);
  820. error = -EINVAL;
  821. if (type >= KOBJ_NS_TYPES)
  822. goto out;
  823. error = -EINVAL;
  824. if (type <= KOBJ_NS_TYPE_NONE)
  825. goto out;
  826. error = -EBUSY;
  827. if (kobj_ns_ops_tbl[type])
  828. goto out;
  829. error = 0;
  830. kobj_ns_ops_tbl[type] = ops;
  831. out:
  832. spin_unlock(&kobj_ns_type_lock);
  833. return error;
  834. }
  835. int kobj_ns_type_registered(enum kobj_ns_type type)
  836. {
  837. int registered = 0;
  838. spin_lock(&kobj_ns_type_lock);
  839. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES))
  840. registered = kobj_ns_ops_tbl[type] != NULL;
  841. spin_unlock(&kobj_ns_type_lock);
  842. return registered;
  843. }
  844. const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
  845. {
  846. const struct kobj_ns_type_operations *ops = NULL;
  847. if (parent && parent->ktype->child_ns_type)
  848. ops = parent->ktype->child_ns_type(parent);
  849. return ops;
  850. }
  851. const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
  852. {
  853. return kobj_child_ns_ops(kobj->parent);
  854. }
  855. bool kobj_ns_current_may_mount(enum kobj_ns_type type)
  856. {
  857. bool may_mount = true;
  858. spin_lock(&kobj_ns_type_lock);
  859. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  860. kobj_ns_ops_tbl[type])
  861. may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
  862. spin_unlock(&kobj_ns_type_lock);
  863. return may_mount;
  864. }
  865. void *kobj_ns_grab_current(enum kobj_ns_type type)
  866. {
  867. void *ns = NULL;
  868. spin_lock(&kobj_ns_type_lock);
  869. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  870. kobj_ns_ops_tbl[type])
  871. ns = kobj_ns_ops_tbl[type]->grab_current_ns();
  872. spin_unlock(&kobj_ns_type_lock);
  873. return ns;
  874. }
  875. const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
  876. {
  877. const void *ns = NULL;
  878. spin_lock(&kobj_ns_type_lock);
  879. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  880. kobj_ns_ops_tbl[type])
  881. ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
  882. spin_unlock(&kobj_ns_type_lock);
  883. return ns;
  884. }
  885. const void *kobj_ns_initial(enum kobj_ns_type type)
  886. {
  887. const void *ns = NULL;
  888. spin_lock(&kobj_ns_type_lock);
  889. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  890. kobj_ns_ops_tbl[type])
  891. ns = kobj_ns_ops_tbl[type]->initial_ns();
  892. spin_unlock(&kobj_ns_type_lock);
  893. return ns;
  894. }
  895. void kobj_ns_drop(enum kobj_ns_type type, void *ns)
  896. {
  897. spin_lock(&kobj_ns_type_lock);
  898. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  899. kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
  900. kobj_ns_ops_tbl[type]->drop_ns(ns);
  901. spin_unlock(&kobj_ns_type_lock);
  902. }
  903. EXPORT_SYMBOL(kobject_get);
  904. EXPORT_SYMBOL(kobject_put);
  905. EXPORT_SYMBOL(kobject_del);
  906. EXPORT_SYMBOL(kset_register);
  907. EXPORT_SYMBOL(kset_unregister);