item.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 error = 0;
  61. int limit = CONFIGFS_ITEM_NAME_LEN;
  62. int need;
  63. va_list args;
  64. char *name;
  65. /*
  66. * First, try the static array
  67. */
  68. va_start(args, fmt);
  69. need = vsnprintf(item->ci_namebuf, limit, fmt, args);
  70. va_end(args);
  71. if (need < limit)
  72. name = item->ci_namebuf;
  73. else {
  74. /*
  75. * Need more space? Allocate it and try again
  76. */
  77. limit = need + 1;
  78. name = kmalloc(limit, GFP_KERNEL);
  79. if (!name) {
  80. error = -ENOMEM;
  81. goto Done;
  82. }
  83. va_start(args, fmt);
  84. need = vsnprintf(name, limit, fmt, args);
  85. va_end(args);
  86. /* Still? Give up. */
  87. if (need >= limit) {
  88. kfree(name);
  89. error = -EFAULT;
  90. goto Done;
  91. }
  92. }
  93. /* Free the old name, if necessary. */
  94. if (item->ci_name && item->ci_name != item->ci_namebuf)
  95. kfree(item->ci_name);
  96. /* Now, set the new name */
  97. item->ci_name = name;
  98. Done:
  99. return error;
  100. }
  101. EXPORT_SYMBOL(config_item_set_name);
  102. void config_item_init_type_name(struct config_item *item,
  103. const char *name,
  104. struct config_item_type *type)
  105. {
  106. config_item_set_name(item, "%s", name);
  107. item->ci_type = type;
  108. config_item_init(item);
  109. }
  110. EXPORT_SYMBOL(config_item_init_type_name);
  111. void config_group_init_type_name(struct config_group *group, const char *name,
  112. struct config_item_type *type)
  113. {
  114. config_item_set_name(&group->cg_item, "%s", name);
  115. group->cg_item.ci_type = type;
  116. config_group_init(group);
  117. }
  118. EXPORT_SYMBOL(config_group_init_type_name);
  119. struct config_item *config_item_get(struct config_item *item)
  120. {
  121. if (item)
  122. kref_get(&item->ci_kref);
  123. return item;
  124. }
  125. EXPORT_SYMBOL(config_item_get);
  126. static void config_item_cleanup(struct config_item *item)
  127. {
  128. struct config_item_type *t = item->ci_type;
  129. struct config_group *s = item->ci_group;
  130. struct config_item *parent = item->ci_parent;
  131. pr_debug("config_item %s: cleaning up\n", config_item_name(item));
  132. if (item->ci_name != item->ci_namebuf)
  133. kfree(item->ci_name);
  134. item->ci_name = NULL;
  135. if (t && t->ct_item_ops && t->ct_item_ops->release)
  136. t->ct_item_ops->release(item);
  137. if (s)
  138. config_group_put(s);
  139. if (parent)
  140. config_item_put(parent);
  141. }
  142. static void config_item_release(struct kref *kref)
  143. {
  144. config_item_cleanup(container_of(kref, struct config_item, ci_kref));
  145. }
  146. /**
  147. * config_item_put - decrement refcount for item.
  148. * @item: item.
  149. *
  150. * Decrement the refcount, and if 0, call config_item_cleanup().
  151. */
  152. void config_item_put(struct config_item *item)
  153. {
  154. if (item)
  155. kref_put(&item->ci_kref, config_item_release);
  156. }
  157. EXPORT_SYMBOL(config_item_put);
  158. /**
  159. * config_group_init - initialize a group for use
  160. * @group: config_group
  161. */
  162. void config_group_init(struct config_group *group)
  163. {
  164. config_item_init(&group->cg_item);
  165. INIT_LIST_HEAD(&group->cg_children);
  166. INIT_LIST_HEAD(&group->default_groups);
  167. }
  168. EXPORT_SYMBOL(config_group_init);
  169. /**
  170. * config_group_find_item - search for item in group.
  171. * @group: group we're looking in.
  172. * @name: item's name.
  173. *
  174. * Iterate over @group->cg_list, looking for a matching config_item.
  175. * If matching item is found take a reference and return the item.
  176. * Caller must have locked group via @group->cg_subsys->su_mtx.
  177. */
  178. struct config_item *config_group_find_item(struct config_group *group,
  179. const char *name)
  180. {
  181. struct list_head *entry;
  182. struct config_item *ret = NULL;
  183. list_for_each(entry, &group->cg_children) {
  184. struct config_item *item = to_item(entry);
  185. if (config_item_name(item) &&
  186. !strcmp(config_item_name(item), name)) {
  187. ret = config_item_get(item);
  188. break;
  189. }
  190. }
  191. return ret;
  192. }
  193. EXPORT_SYMBOL(config_group_find_item);