item.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. void config_item_init(struct config_item *item)
  45. {
  46. kref_init(&item->ci_kref);
  47. INIT_LIST_HEAD(&item->ci_entry);
  48. }
  49. EXPORT_SYMBOL(config_item_init);
  50. /**
  51. * config_item_set_name - Set the name of an item
  52. * @item: item.
  53. * @fmt: The vsnprintf()'s format string.
  54. *
  55. * If strlen(name) >= CONFIGFS_ITEM_NAME_LEN, then use a
  56. * dynamically allocated string that @item->ci_name points to.
  57. * Otherwise, use the static @item->ci_namebuf array.
  58. */
  59. int config_item_set_name(struct config_item *item, const char *fmt, ...)
  60. {
  61. int error = 0;
  62. int limit = CONFIGFS_ITEM_NAME_LEN;
  63. int need;
  64. va_list args;
  65. char *name;
  66. /*
  67. * First, try the static array
  68. */
  69. va_start(args, fmt);
  70. need = vsnprintf(item->ci_namebuf, limit, fmt, args);
  71. va_end(args);
  72. if (need < limit)
  73. name = item->ci_namebuf;
  74. else {
  75. /*
  76. * Need more space? Allocate it and try again
  77. */
  78. limit = need + 1;
  79. name = kmalloc(limit, GFP_KERNEL);
  80. if (!name) {
  81. error = -ENOMEM;
  82. goto Done;
  83. }
  84. va_start(args, fmt);
  85. need = vsnprintf(name, limit, fmt, args);
  86. va_end(args);
  87. /* Still? Give up. */
  88. if (need >= limit) {
  89. kfree(name);
  90. error = -EFAULT;
  91. goto Done;
  92. }
  93. }
  94. /* Free the old name, if necessary. */
  95. if (item->ci_name && item->ci_name != item->ci_namebuf)
  96. kfree(item->ci_name);
  97. /* Now, set the new name */
  98. item->ci_name = name;
  99. Done:
  100. return error;
  101. }
  102. EXPORT_SYMBOL(config_item_set_name);
  103. void config_item_init_type_name(struct config_item *item,
  104. const char *name,
  105. struct config_item_type *type)
  106. {
  107. config_item_set_name(item, name);
  108. item->ci_type = type;
  109. config_item_init(item);
  110. }
  111. EXPORT_SYMBOL(config_item_init_type_name);
  112. void config_group_init_type_name(struct config_group *group, const char *name,
  113. struct config_item_type *type)
  114. {
  115. config_item_set_name(&group->cg_item, name);
  116. group->cg_item.ci_type = type;
  117. config_group_init(group);
  118. }
  119. EXPORT_SYMBOL(config_group_init_type_name);
  120. struct config_item *config_item_get(struct config_item *item)
  121. {
  122. if (item)
  123. kref_get(&item->ci_kref);
  124. return item;
  125. }
  126. EXPORT_SYMBOL(config_item_get);
  127. static void config_item_cleanup(struct config_item *item)
  128. {
  129. struct config_item_type *t = item->ci_type;
  130. struct config_group *s = item->ci_group;
  131. struct config_item *parent = item->ci_parent;
  132. pr_debug("config_item %s: cleaning up\n", config_item_name(item));
  133. if (item->ci_name != item->ci_namebuf)
  134. kfree(item->ci_name);
  135. item->ci_name = NULL;
  136. if (t && t->ct_item_ops && t->ct_item_ops->release)
  137. t->ct_item_ops->release(item);
  138. if (s)
  139. config_group_put(s);
  140. if (parent)
  141. config_item_put(parent);
  142. }
  143. static void config_item_release(struct kref *kref)
  144. {
  145. config_item_cleanup(container_of(kref, struct config_item, ci_kref));
  146. }
  147. /**
  148. * config_item_put - decrement refcount for item.
  149. * @item: item.
  150. *
  151. * Decrement the refcount, and if 0, call config_item_cleanup().
  152. */
  153. void config_item_put(struct config_item *item)
  154. {
  155. if (item)
  156. kref_put(&item->ci_kref, config_item_release);
  157. }
  158. EXPORT_SYMBOL(config_item_put);
  159. /**
  160. * config_group_init - initialize a group for use
  161. * @group: config_group
  162. */
  163. void config_group_init(struct config_group *group)
  164. {
  165. config_item_init(&group->cg_item);
  166. INIT_LIST_HEAD(&group->cg_children);
  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);