item.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* -*- mode: c; c-basic-offset: 8; -*-
  3. * vim: noexpandtab sw=8 ts=8 sts=0:
  4. *
  5. * item.c - library routines for handling generic config items
  6. *
  7. * Based on kobject:
  8. * kobject is Copyright (c) 2002-2003 Patrick Mochel
  9. *
  10. * configfs Copyright (C) 2005 Oracle. All rights reserved.
  11. *
  12. * Please see the file Documentation/filesystems/configfs.rst for
  13. * critical information about using the config_item interface.
  14. */
  15. #include <linux/string.h>
  16. #include <linux/module.h>
  17. #include <linux/stat.h>
  18. #include <linux/slab.h>
  19. #include <linux/configfs.h>
  20. static inline struct config_item *to_item(struct list_head *entry)
  21. {
  22. return container_of(entry, struct config_item, ci_entry);
  23. }
  24. /* Evil kernel */
  25. static void config_item_release(struct kref *kref);
  26. /**
  27. * config_item_init - initialize item.
  28. * @item: item in question.
  29. */
  30. static void config_item_init(struct config_item *item)
  31. {
  32. kref_init(&item->ci_kref);
  33. INIT_LIST_HEAD(&item->ci_entry);
  34. }
  35. /**
  36. * config_item_set_name - Set the name of an item
  37. * @item: item.
  38. * @fmt: The vsnprintf()'s format string.
  39. *
  40. * If strlen(name) >= CONFIGFS_ITEM_NAME_LEN, then use a
  41. * dynamically allocated string that @item->ci_name points to.
  42. * Otherwise, use the static @item->ci_namebuf array.
  43. */
  44. int config_item_set_name(struct config_item *item, const char *fmt, ...)
  45. {
  46. int limit = CONFIGFS_ITEM_NAME_LEN;
  47. int need;
  48. va_list args;
  49. char *name;
  50. /*
  51. * First, try the static array
  52. */
  53. va_start(args, fmt);
  54. need = vsnprintf(item->ci_namebuf, limit, fmt, args);
  55. va_end(args);
  56. if (need < limit)
  57. name = item->ci_namebuf;
  58. else {
  59. va_start(args, fmt);
  60. name = kvasprintf(GFP_KERNEL, fmt, args);
  61. va_end(args);
  62. if (!name)
  63. return -EFAULT;
  64. }
  65. /* Free the old name, if necessary. */
  66. if (item->ci_name && item->ci_name != item->ci_namebuf)
  67. kfree(item->ci_name);
  68. /* Now, set the new name */
  69. item->ci_name = name;
  70. return 0;
  71. }
  72. EXPORT_SYMBOL(config_item_set_name);
  73. void config_item_init_type_name(struct config_item *item,
  74. const char *name,
  75. const struct config_item_type *type)
  76. {
  77. config_item_set_name(item, "%s", name);
  78. item->ci_type = type;
  79. config_item_init(item);
  80. }
  81. EXPORT_SYMBOL(config_item_init_type_name);
  82. void config_group_init_type_name(struct config_group *group, const char *name,
  83. const struct config_item_type *type)
  84. {
  85. config_item_set_name(&group->cg_item, "%s", name);
  86. group->cg_item.ci_type = type;
  87. config_group_init(group);
  88. }
  89. EXPORT_SYMBOL(config_group_init_type_name);
  90. struct config_item *config_item_get(struct config_item *item)
  91. {
  92. if (item)
  93. kref_get(&item->ci_kref);
  94. return item;
  95. }
  96. EXPORT_SYMBOL(config_item_get);
  97. struct config_item *config_item_get_unless_zero(struct config_item *item)
  98. {
  99. if (item && kref_get_unless_zero(&item->ci_kref))
  100. return item;
  101. return NULL;
  102. }
  103. EXPORT_SYMBOL(config_item_get_unless_zero);
  104. static void config_item_cleanup(struct config_item *item)
  105. {
  106. const struct config_item_type *t = item->ci_type;
  107. struct config_group *s = item->ci_group;
  108. struct config_item *parent = item->ci_parent;
  109. pr_debug("config_item %s: cleaning up\n", config_item_name(item));
  110. if (item->ci_name != item->ci_namebuf)
  111. kfree(item->ci_name);
  112. item->ci_name = NULL;
  113. if (t && t->ct_item_ops && t->ct_item_ops->release)
  114. t->ct_item_ops->release(item);
  115. if (s)
  116. config_group_put(s);
  117. if (parent)
  118. config_item_put(parent);
  119. }
  120. static void config_item_release(struct kref *kref)
  121. {
  122. config_item_cleanup(container_of(kref, struct config_item, ci_kref));
  123. }
  124. /**
  125. * config_item_put - decrement refcount for item.
  126. * @item: item.
  127. *
  128. * Decrement the refcount, and if 0, call config_item_cleanup().
  129. */
  130. void config_item_put(struct config_item *item)
  131. {
  132. if (item)
  133. kref_put(&item->ci_kref, config_item_release);
  134. }
  135. EXPORT_SYMBOL(config_item_put);
  136. /**
  137. * config_group_init - initialize a group for use
  138. * @group: config_group
  139. */
  140. void config_group_init(struct config_group *group)
  141. {
  142. config_item_init(&group->cg_item);
  143. INIT_LIST_HEAD(&group->cg_children);
  144. INIT_LIST_HEAD(&group->default_groups);
  145. }
  146. EXPORT_SYMBOL(config_group_init);
  147. /**
  148. * config_group_find_item - search for item in group.
  149. * @group: group we're looking in.
  150. * @name: item's name.
  151. *
  152. * Iterate over @group->cg_list, looking for a matching config_item.
  153. * If matching item is found take a reference and return the item.
  154. * Caller must have locked group via @group->cg_subsys->su_mtx.
  155. */
  156. struct config_item *config_group_find_item(struct config_group *group,
  157. const char *name)
  158. {
  159. struct list_head *entry;
  160. struct config_item *ret = NULL;
  161. list_for_each(entry, &group->cg_children) {
  162. struct config_item *item = to_item(entry);
  163. if (config_item_name(item) &&
  164. !strcmp(config_item_name(item), name)) {
  165. ret = config_item_get(item);
  166. break;
  167. }
  168. }
  169. return ret;
  170. }
  171. EXPORT_SYMBOL(config_group_find_item);