kobject.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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 *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. if (!kobj->state_initialized)
  506. WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
  507. "initialized, yet kobject_get() is being "
  508. "called.\n", kobject_name(kobj), kobj);
  509. kref_get(&kobj->kref);
  510. }
  511. return kobj;
  512. }
  513. static struct kobject * __must_check kobject_get_unless_zero(struct kobject *kobj)
  514. {
  515. if (!kref_get_unless_zero(&kobj->kref))
  516. kobj = NULL;
  517. return kobj;
  518. }
  519. /*
  520. * kobject_cleanup - free kobject resources.
  521. * @kobj: object to cleanup
  522. */
  523. static void kobject_cleanup(struct kobject *kobj)
  524. {
  525. struct kobj_type *t = get_ktype(kobj);
  526. const char *name = kobj->name;
  527. pr_debug("kobject: '%s' (%p): %s, parent %p\n",
  528. kobject_name(kobj), kobj, __func__, kobj->parent);
  529. if (t && !t->release)
  530. pr_debug("kobject: '%s' (%p): does not have a release() "
  531. "function, it is broken and must be fixed.\n",
  532. kobject_name(kobj), kobj);
  533. /* send "remove" if the caller did not do it but sent "add" */
  534. if (kobj->state_add_uevent_sent && !kobj->state_remove_uevent_sent) {
  535. pr_debug("kobject: '%s' (%p): auto cleanup 'remove' event\n",
  536. kobject_name(kobj), kobj);
  537. kobject_uevent(kobj, KOBJ_REMOVE);
  538. }
  539. /* remove from sysfs if the caller did not do it */
  540. if (kobj->state_in_sysfs) {
  541. pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n",
  542. kobject_name(kobj), kobj);
  543. kobject_del(kobj);
  544. }
  545. if (t && t->release) {
  546. pr_debug("kobject: '%s' (%p): calling ktype release\n",
  547. kobject_name(kobj), kobj);
  548. t->release(kobj);
  549. }
  550. /* free name if we allocated it */
  551. if (name) {
  552. pr_debug("kobject: '%s': free name\n", name);
  553. kfree(name);
  554. }
  555. }
  556. #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
  557. static void kobject_delayed_cleanup(struct work_struct *work)
  558. {
  559. kobject_cleanup(container_of(to_delayed_work(work),
  560. struct kobject, release));
  561. }
  562. #endif
  563. static void kobject_release(struct kref *kref)
  564. {
  565. struct kobject *kobj = container_of(kref, struct kobject, kref);
  566. #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
  567. unsigned long delay = HZ + HZ * (get_random_int() & 0x3);
  568. pr_info("kobject: '%s' (%p): %s, parent %p (delayed %ld)\n",
  569. kobject_name(kobj), kobj, __func__, kobj->parent, delay);
  570. INIT_DELAYED_WORK(&kobj->release, kobject_delayed_cleanup);
  571. schedule_delayed_work(&kobj->release, delay);
  572. #else
  573. kobject_cleanup(kobj);
  574. #endif
  575. }
  576. /**
  577. * kobject_put - decrement refcount for object.
  578. * @kobj: object.
  579. *
  580. * Decrement the refcount, and if 0, call kobject_cleanup().
  581. */
  582. void kobject_put(struct kobject *kobj)
  583. {
  584. if (kobj) {
  585. if (!kobj->state_initialized)
  586. WARN(1, KERN_WARNING "kobject: '%s' (%p): is not "
  587. "initialized, yet kobject_put() is being "
  588. "called.\n", kobject_name(kobj), kobj);
  589. kref_put(&kobj->kref, kobject_release);
  590. }
  591. }
  592. static void dynamic_kobj_release(struct kobject *kobj)
  593. {
  594. pr_debug("kobject: (%p): %s\n", kobj, __func__);
  595. kfree(kobj);
  596. }
  597. static struct kobj_type dynamic_kobj_ktype = {
  598. .release = dynamic_kobj_release,
  599. .sysfs_ops = &kobj_sysfs_ops,
  600. };
  601. /**
  602. * kobject_create - create a struct kobject dynamically
  603. *
  604. * This function creates a kobject structure dynamically and sets it up
  605. * to be a "dynamic" kobject with a default release function set up.
  606. *
  607. * If the kobject was not able to be created, NULL will be returned.
  608. * The kobject structure returned from here must be cleaned up with a
  609. * call to kobject_put() and not kfree(), as kobject_init() has
  610. * already been called on this structure.
  611. */
  612. struct kobject *kobject_create(void)
  613. {
  614. struct kobject *kobj;
  615. kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
  616. if (!kobj)
  617. return NULL;
  618. kobject_init(kobj, &dynamic_kobj_ktype);
  619. return kobj;
  620. }
  621. /**
  622. * kobject_create_and_add - create a struct kobject dynamically and register it with sysfs
  623. *
  624. * @name: the name for the kobject
  625. * @parent: the parent kobject of this kobject, if any.
  626. *
  627. * This function creates a kobject structure dynamically and registers it
  628. * with sysfs. When you are finished with this structure, call
  629. * kobject_put() and the structure will be dynamically freed when
  630. * it is no longer being used.
  631. *
  632. * If the kobject was not able to be created, NULL will be returned.
  633. */
  634. struct kobject *kobject_create_and_add(const char *name, struct kobject *parent)
  635. {
  636. struct kobject *kobj;
  637. int retval;
  638. kobj = kobject_create();
  639. if (!kobj)
  640. return NULL;
  641. retval = kobject_add(kobj, parent, "%s", name);
  642. if (retval) {
  643. printk(KERN_WARNING "%s: kobject_add error: %d\n",
  644. __func__, retval);
  645. kobject_put(kobj);
  646. kobj = NULL;
  647. }
  648. return kobj;
  649. }
  650. EXPORT_SYMBOL_GPL(kobject_create_and_add);
  651. /**
  652. * kset_init - initialize a kset for use
  653. * @k: kset
  654. */
  655. void kset_init(struct kset *k)
  656. {
  657. kobject_init_internal(&k->kobj);
  658. INIT_LIST_HEAD(&k->list);
  659. spin_lock_init(&k->list_lock);
  660. }
  661. /* default kobject attribute operations */
  662. static ssize_t kobj_attr_show(struct kobject *kobj, struct attribute *attr,
  663. char *buf)
  664. {
  665. struct kobj_attribute *kattr;
  666. ssize_t ret = -EIO;
  667. kattr = container_of(attr, struct kobj_attribute, attr);
  668. if (kattr->show)
  669. ret = kattr->show(kobj, kattr, buf);
  670. return ret;
  671. }
  672. static ssize_t kobj_attr_store(struct kobject *kobj, struct attribute *attr,
  673. const char *buf, size_t count)
  674. {
  675. struct kobj_attribute *kattr;
  676. ssize_t ret = -EIO;
  677. kattr = container_of(attr, struct kobj_attribute, attr);
  678. if (kattr->store)
  679. ret = kattr->store(kobj, kattr, buf, count);
  680. return ret;
  681. }
  682. const struct sysfs_ops kobj_sysfs_ops = {
  683. .show = kobj_attr_show,
  684. .store = kobj_attr_store,
  685. };
  686. EXPORT_SYMBOL_GPL(kobj_sysfs_ops);
  687. /**
  688. * kset_register - initialize and add a kset.
  689. * @k: kset.
  690. */
  691. int kset_register(struct kset *k)
  692. {
  693. int err;
  694. if (!k)
  695. return -EINVAL;
  696. kset_init(k);
  697. err = kobject_add_internal(&k->kobj);
  698. if (err)
  699. return err;
  700. kobject_uevent(&k->kobj, KOBJ_ADD);
  701. return 0;
  702. }
  703. /**
  704. * kset_unregister - remove a kset.
  705. * @k: kset.
  706. */
  707. void kset_unregister(struct kset *k)
  708. {
  709. if (!k)
  710. return;
  711. kobject_del(&k->kobj);
  712. kobject_put(&k->kobj);
  713. }
  714. /**
  715. * kset_find_obj - search for object in kset.
  716. * @kset: kset we're looking in.
  717. * @name: object's name.
  718. *
  719. * Lock kset via @kset->subsys, and iterate over @kset->list,
  720. * looking for a matching kobject. If matching object is found
  721. * take a reference and return the object.
  722. */
  723. struct kobject *kset_find_obj(struct kset *kset, const char *name)
  724. {
  725. struct kobject *k;
  726. struct kobject *ret = NULL;
  727. spin_lock(&kset->list_lock);
  728. list_for_each_entry(k, &kset->list, entry) {
  729. if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
  730. ret = kobject_get_unless_zero(k);
  731. break;
  732. }
  733. }
  734. spin_unlock(&kset->list_lock);
  735. return ret;
  736. }
  737. static void kset_release(struct kobject *kobj)
  738. {
  739. struct kset *kset = container_of(kobj, struct kset, kobj);
  740. pr_debug("kobject: '%s' (%p): %s\n",
  741. kobject_name(kobj), kobj, __func__);
  742. kfree(kset);
  743. }
  744. static struct kobj_type kset_ktype = {
  745. .sysfs_ops = &kobj_sysfs_ops,
  746. .release = kset_release,
  747. };
  748. /**
  749. * kset_create - create a struct kset dynamically
  750. *
  751. * @name: the name for the kset
  752. * @uevent_ops: a struct kset_uevent_ops for the kset
  753. * @parent_kobj: the parent kobject of this kset, if any.
  754. *
  755. * This function creates a kset structure dynamically. This structure can
  756. * then be registered with the system and show up in sysfs with a call to
  757. * kset_register(). When you are finished with this structure, if
  758. * kset_register() has been called, call kset_unregister() and the
  759. * structure will be dynamically freed when it is no longer being used.
  760. *
  761. * If the kset was not able to be created, NULL will be returned.
  762. */
  763. static struct kset *kset_create(const char *name,
  764. const struct kset_uevent_ops *uevent_ops,
  765. struct kobject *parent_kobj)
  766. {
  767. struct kset *kset;
  768. int retval;
  769. kset = kzalloc(sizeof(*kset), GFP_KERNEL);
  770. if (!kset)
  771. return NULL;
  772. retval = kobject_set_name(&kset->kobj, "%s", name);
  773. if (retval) {
  774. kfree(kset);
  775. return NULL;
  776. }
  777. kset->uevent_ops = uevent_ops;
  778. kset->kobj.parent = parent_kobj;
  779. /*
  780. * The kobject of this kset will have a type of kset_ktype and belong to
  781. * no kset itself. That way we can properly free it when it is
  782. * finished being used.
  783. */
  784. kset->kobj.ktype = &kset_ktype;
  785. kset->kobj.kset = NULL;
  786. return kset;
  787. }
  788. /**
  789. * kset_create_and_add - create a struct kset dynamically and add it to sysfs
  790. *
  791. * @name: the name for the kset
  792. * @uevent_ops: a struct kset_uevent_ops for the kset
  793. * @parent_kobj: the parent kobject of this kset, if any.
  794. *
  795. * This function creates a kset structure dynamically and registers it
  796. * with sysfs. When you are finished with this structure, call
  797. * kset_unregister() and the structure will be dynamically freed when it
  798. * is no longer being used.
  799. *
  800. * If the kset was not able to be created, NULL will be returned.
  801. */
  802. struct kset *kset_create_and_add(const char *name,
  803. const struct kset_uevent_ops *uevent_ops,
  804. struct kobject *parent_kobj)
  805. {
  806. struct kset *kset;
  807. int error;
  808. kset = kset_create(name, uevent_ops, parent_kobj);
  809. if (!kset)
  810. return NULL;
  811. error = kset_register(kset);
  812. if (error) {
  813. kfree(kset);
  814. return NULL;
  815. }
  816. return kset;
  817. }
  818. EXPORT_SYMBOL_GPL(kset_create_and_add);
  819. static DEFINE_SPINLOCK(kobj_ns_type_lock);
  820. static const struct kobj_ns_type_operations *kobj_ns_ops_tbl[KOBJ_NS_TYPES];
  821. int kobj_ns_type_register(const struct kobj_ns_type_operations *ops)
  822. {
  823. enum kobj_ns_type type = ops->type;
  824. int error;
  825. spin_lock(&kobj_ns_type_lock);
  826. error = -EINVAL;
  827. if (type >= KOBJ_NS_TYPES)
  828. goto out;
  829. error = -EINVAL;
  830. if (type <= KOBJ_NS_TYPE_NONE)
  831. goto out;
  832. error = -EBUSY;
  833. if (kobj_ns_ops_tbl[type])
  834. goto out;
  835. error = 0;
  836. kobj_ns_ops_tbl[type] = ops;
  837. out:
  838. spin_unlock(&kobj_ns_type_lock);
  839. return error;
  840. }
  841. int kobj_ns_type_registered(enum kobj_ns_type type)
  842. {
  843. int registered = 0;
  844. spin_lock(&kobj_ns_type_lock);
  845. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES))
  846. registered = kobj_ns_ops_tbl[type] != NULL;
  847. spin_unlock(&kobj_ns_type_lock);
  848. return registered;
  849. }
  850. const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent)
  851. {
  852. const struct kobj_ns_type_operations *ops = NULL;
  853. if (parent && parent->ktype && parent->ktype->child_ns_type)
  854. ops = parent->ktype->child_ns_type(parent);
  855. return ops;
  856. }
  857. const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj)
  858. {
  859. return kobj_child_ns_ops(kobj->parent);
  860. }
  861. bool kobj_ns_current_may_mount(enum kobj_ns_type type)
  862. {
  863. bool may_mount = true;
  864. spin_lock(&kobj_ns_type_lock);
  865. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  866. kobj_ns_ops_tbl[type])
  867. may_mount = kobj_ns_ops_tbl[type]->current_may_mount();
  868. spin_unlock(&kobj_ns_type_lock);
  869. return may_mount;
  870. }
  871. void *kobj_ns_grab_current(enum kobj_ns_type type)
  872. {
  873. void *ns = NULL;
  874. spin_lock(&kobj_ns_type_lock);
  875. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  876. kobj_ns_ops_tbl[type])
  877. ns = kobj_ns_ops_tbl[type]->grab_current_ns();
  878. spin_unlock(&kobj_ns_type_lock);
  879. return ns;
  880. }
  881. const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk)
  882. {
  883. const void *ns = NULL;
  884. spin_lock(&kobj_ns_type_lock);
  885. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  886. kobj_ns_ops_tbl[type])
  887. ns = kobj_ns_ops_tbl[type]->netlink_ns(sk);
  888. spin_unlock(&kobj_ns_type_lock);
  889. return ns;
  890. }
  891. const void *kobj_ns_initial(enum kobj_ns_type type)
  892. {
  893. const void *ns = NULL;
  894. spin_lock(&kobj_ns_type_lock);
  895. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  896. kobj_ns_ops_tbl[type])
  897. ns = kobj_ns_ops_tbl[type]->initial_ns();
  898. spin_unlock(&kobj_ns_type_lock);
  899. return ns;
  900. }
  901. void kobj_ns_drop(enum kobj_ns_type type, void *ns)
  902. {
  903. spin_lock(&kobj_ns_type_lock);
  904. if ((type > KOBJ_NS_TYPE_NONE) && (type < KOBJ_NS_TYPES) &&
  905. kobj_ns_ops_tbl[type] && kobj_ns_ops_tbl[type]->drop_ns)
  906. kobj_ns_ops_tbl[type]->drop_ns(ns);
  907. spin_unlock(&kobj_ns_type_lock);
  908. }
  909. EXPORT_SYMBOL(kobject_get);
  910. EXPORT_SYMBOL(kobject_put);
  911. EXPORT_SYMBOL(kobject_del);
  912. EXPORT_SYMBOL(kset_register);
  913. EXPORT_SYMBOL(kset_unregister);