item.c 5.4 KB

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