item.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.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. /**
  50. * config_item_set_name - Set the name of an item
  51. * @item: item.
  52. * @name: name.
  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, 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, 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. static void config_item_cleanup(struct config_item * item)
  126. {
  127. struct config_item_type * t = item->ci_type;
  128. struct config_group * s = item->ci_group;
  129. struct config_item * parent = item->ci_parent;
  130. pr_debug("config_item %s: cleaning up\n",config_item_name(item));
  131. if (item->ci_name != item->ci_namebuf)
  132. kfree(item->ci_name);
  133. item->ci_name = NULL;
  134. if (t && t->ct_item_ops && t->ct_item_ops->release)
  135. t->ct_item_ops->release(item);
  136. if (s)
  137. config_group_put(s);
  138. if (parent)
  139. config_item_put(parent);
  140. }
  141. static void config_item_release(struct kref *kref)
  142. {
  143. config_item_cleanup(container_of(kref, struct config_item, ci_kref));
  144. }
  145. /**
  146. * config_item_put - decrement refcount for item.
  147. * @item: item.
  148. *
  149. * Decrement the refcount, and if 0, call config_item_cleanup().
  150. */
  151. void config_item_put(struct config_item * item)
  152. {
  153. if (item)
  154. kref_put(&item->ci_kref, config_item_release);
  155. }
  156. /**
  157. * config_group_init - initialize a group for use
  158. * @k: group
  159. */
  160. void config_group_init(struct config_group *group)
  161. {
  162. config_item_init(&group->cg_item);
  163. INIT_LIST_HEAD(&group->cg_children);
  164. }
  165. /**
  166. * config_group_find_item - search for item in group.
  167. * @group: group we're looking in.
  168. * @name: item's name.
  169. *
  170. * Iterate over @group->cg_list, looking for a matching config_item.
  171. * If matching item is found take a reference and return the item.
  172. * Caller must have locked group via @group->cg_subsys->su_mtx.
  173. */
  174. struct config_item *config_group_find_item(struct config_group *group,
  175. const char *name)
  176. {
  177. struct list_head * entry;
  178. struct config_item * ret = NULL;
  179. list_for_each(entry,&group->cg_children) {
  180. struct config_item * item = to_item(entry);
  181. if (config_item_name(item) &&
  182. !strcmp(config_item_name(item), name)) {
  183. ret = config_item_get(item);
  184. break;
  185. }
  186. }
  187. return ret;
  188. }
  189. EXPORT_SYMBOL(config_item_init);
  190. EXPORT_SYMBOL(config_group_init);
  191. EXPORT_SYMBOL(config_item_get);
  192. EXPORT_SYMBOL(config_item_put);
  193. EXPORT_SYMBOL(config_group_find_item);