symlink.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * symlink.c - operations for configfs symlinks.
  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 sysfs:
  22. * sysfs is Copyright (C) 2001, 2002, 2003 Patrick Mochel
  23. *
  24. * configfs Copyright (C) 2005 Oracle. All rights reserved.
  25. */
  26. #include <linux/fs.h>
  27. #include <linux/module.h>
  28. #include <linux/namei.h>
  29. #include <linux/configfs.h>
  30. #include "configfs_internal.h"
  31. static int item_depth(struct config_item * item)
  32. {
  33. struct config_item * p = item;
  34. int depth = 0;
  35. do { depth++; } while ((p = p->ci_parent) && !configfs_is_root(p));
  36. return depth;
  37. }
  38. static int item_path_length(struct config_item * item)
  39. {
  40. struct config_item * p = item;
  41. int length = 1;
  42. do {
  43. length += strlen(config_item_name(p)) + 1;
  44. p = p->ci_parent;
  45. } while (p && !configfs_is_root(p));
  46. return length;
  47. }
  48. static void fill_item_path(struct config_item * item, char * buffer, int length)
  49. {
  50. struct config_item * p;
  51. --length;
  52. for (p = item; p && !configfs_is_root(p); p = p->ci_parent) {
  53. int cur = strlen(config_item_name(p));
  54. /* back up enough to print this bus id with '/' */
  55. length -= cur;
  56. strncpy(buffer + length,config_item_name(p),cur);
  57. *(buffer + --length) = '/';
  58. }
  59. }
  60. static int create_link(struct config_item *parent_item,
  61. struct config_item *item,
  62. struct dentry *dentry)
  63. {
  64. struct configfs_dirent *target_sd = item->ci_dentry->d_fsdata;
  65. struct configfs_symlink *sl;
  66. int ret;
  67. ret = -ENOMEM;
  68. sl = kmalloc(sizeof(struct configfs_symlink), GFP_KERNEL);
  69. if (sl) {
  70. sl->sl_target = config_item_get(item);
  71. /* FIXME: needs a lock, I'd bet */
  72. list_add(&sl->sl_list, &target_sd->s_links);
  73. ret = configfs_create_link(sl, parent_item->ci_dentry,
  74. dentry);
  75. if (ret) {
  76. list_del_init(&sl->sl_list);
  77. config_item_put(item);
  78. kfree(sl);
  79. }
  80. }
  81. return ret;
  82. }
  83. static int get_target(const char *symname, struct nameidata *nd,
  84. struct config_item **target)
  85. {
  86. int ret;
  87. ret = path_lookup(symname, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, nd);
  88. if (!ret) {
  89. if (nd->dentry->d_sb == configfs_sb) {
  90. *target = configfs_get_config_item(nd->dentry);
  91. if (!*target) {
  92. ret = -ENOENT;
  93. path_release(nd);
  94. }
  95. } else
  96. ret = -EPERM;
  97. }
  98. return ret;
  99. }
  100. int configfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  101. {
  102. int ret;
  103. struct nameidata nd;
  104. struct config_item *parent_item;
  105. struct config_item *target_item;
  106. struct config_item_type *type;
  107. ret = -EPERM; /* What lack-of-symlink returns */
  108. if (dentry->d_parent == configfs_sb->s_root)
  109. goto out;
  110. parent_item = configfs_get_config_item(dentry->d_parent);
  111. type = parent_item->ci_type;
  112. if (!type || !type->ct_item_ops ||
  113. !type->ct_item_ops->allow_link)
  114. goto out_put;
  115. ret = get_target(symname, &nd, &target_item);
  116. if (ret)
  117. goto out_put;
  118. ret = type->ct_item_ops->allow_link(parent_item, target_item);
  119. if (!ret)
  120. ret = create_link(parent_item, target_item, dentry);
  121. config_item_put(target_item);
  122. path_release(&nd);
  123. out_put:
  124. config_item_put(parent_item);
  125. out:
  126. return ret;
  127. }
  128. int configfs_unlink(struct inode *dir, struct dentry *dentry)
  129. {
  130. struct configfs_dirent *sd = dentry->d_fsdata;
  131. struct configfs_symlink *sl;
  132. struct config_item *parent_item;
  133. struct config_item_type *type;
  134. int ret;
  135. ret = -EPERM; /* What lack-of-symlink returns */
  136. if (!(sd->s_type & CONFIGFS_ITEM_LINK))
  137. goto out;
  138. BUG_ON(dentry->d_parent == configfs_sb->s_root);
  139. sl = sd->s_element;
  140. parent_item = configfs_get_config_item(dentry->d_parent);
  141. type = parent_item->ci_type;
  142. list_del_init(&sd->s_sibling);
  143. configfs_drop_dentry(sd, dentry->d_parent);
  144. dput(dentry);
  145. configfs_put(sd);
  146. /*
  147. * drop_link() must be called before
  148. * list_del_init(&sl->sl_list), so that the order of
  149. * drop_link(this, target) and drop_item(target) is preserved.
  150. */
  151. if (type && type->ct_item_ops &&
  152. type->ct_item_ops->drop_link)
  153. type->ct_item_ops->drop_link(parent_item,
  154. sl->sl_target);
  155. /* FIXME: Needs lock */
  156. list_del_init(&sl->sl_list);
  157. /* Put reference from create_link() */
  158. config_item_put(sl->sl_target);
  159. kfree(sl);
  160. config_item_put(parent_item);
  161. ret = 0;
  162. out:
  163. return ret;
  164. }
  165. static int configfs_get_target_path(struct config_item * item, struct config_item * target,
  166. char *path)
  167. {
  168. char * s;
  169. int depth, size;
  170. depth = item_depth(item);
  171. size = item_path_length(target) + depth * 3 - 1;
  172. if (size > PATH_MAX)
  173. return -ENAMETOOLONG;
  174. pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
  175. for (s = path; depth--; s += 3)
  176. strcpy(s,"../");
  177. fill_item_path(target, path, size);
  178. pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
  179. return 0;
  180. }
  181. static int configfs_getlink(struct dentry *dentry, char * path)
  182. {
  183. struct config_item *item, *target_item;
  184. int error = 0;
  185. item = configfs_get_config_item(dentry->d_parent);
  186. if (!item)
  187. return -EINVAL;
  188. target_item = configfs_get_config_item(dentry);
  189. if (!target_item) {
  190. config_item_put(item);
  191. return -EINVAL;
  192. }
  193. down_read(&configfs_rename_sem);
  194. error = configfs_get_target_path(item, target_item, path);
  195. up_read(&configfs_rename_sem);
  196. config_item_put(item);
  197. config_item_put(target_item);
  198. return error;
  199. }
  200. static void *configfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  201. {
  202. int error = -ENOMEM;
  203. unsigned long page = get_zeroed_page(GFP_KERNEL);
  204. if (page) {
  205. error = configfs_getlink(dentry, (char *)page);
  206. if (!error) {
  207. nd_set_link(nd, (char *)page);
  208. return (void *)page;
  209. }
  210. }
  211. nd_set_link(nd, ERR_PTR(error));
  212. return NULL;
  213. }
  214. static void configfs_put_link(struct dentry *dentry, struct nameidata *nd,
  215. void *cookie)
  216. {
  217. if (cookie) {
  218. unsigned long page = (unsigned long)cookie;
  219. free_page(page);
  220. }
  221. }
  222. const struct inode_operations configfs_symlink_inode_operations = {
  223. .follow_link = configfs_follow_link,
  224. .readlink = generic_readlink,
  225. .put_link = configfs_put_link,
  226. .setattr = configfs_setattr,
  227. };