item.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /**
  126. * config_item_cleanup - free config_item resources.
  127. * @item: item.
  128. */
  129. void config_item_cleanup(struct config_item * item)
  130. {
  131. struct config_item_type * t = item->ci_type;
  132. struct config_group * s = item->ci_group;
  133. struct config_item * parent = item->ci_parent;
  134. pr_debug("config_item %s: cleaning up\n",config_item_name(item));
  135. if (item->ci_name != item->ci_namebuf)
  136. kfree(item->ci_name);
  137. item->ci_name = NULL;
  138. if (t && t->ct_item_ops && t->ct_item_ops->release)
  139. t->ct_item_ops->release(item);
  140. if (s)
  141. config_group_put(s);
  142. if (parent)
  143. config_item_put(parent);
  144. }
  145. static void config_item_release(struct kref *kref)
  146. {
  147. config_item_cleanup(container_of(kref, struct config_item, ci_kref));
  148. }
  149. /**
  150. * config_item_put - decrement refcount for item.
  151. * @item: item.
  152. *
  153. * Decrement the refcount, and if 0, call config_item_cleanup().
  154. */
  155. void config_item_put(struct config_item * item)
  156. {
  157. if (item)
  158. kref_put(&item->ci_kref, config_item_release);
  159. }
  160. /**
  161. * config_group_init - initialize a group for use
  162. * @k: group
  163. */
  164. void config_group_init(struct config_group *group)
  165. {
  166. config_item_init(&group->cg_item);
  167. INIT_LIST_HEAD(&group->cg_children);
  168. }
  169. /**
  170. * config_group_find_obj - search for item in group.
  171. * @group: group we're looking in.
  172. * @name: item's name.
  173. *
  174. * Lock group via @group->cg_subsys, and iterate over @group->cg_list,
  175. * looking for a matching config_item. If matching item is found
  176. * take a reference and return the item.
  177. */
  178. struct config_item * config_group_find_obj(struct config_group * group, const char * name)
  179. {
  180. struct list_head * entry;
  181. struct config_item * ret = NULL;
  182. /* XXX LOCKING! */
  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_item_init);
  194. EXPORT_SYMBOL(config_group_init);
  195. EXPORT_SYMBOL(config_item_get);
  196. EXPORT_SYMBOL(config_item_put);
  197. EXPORT_SYMBOL(config_group_find_obj);