symlink.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. spin_lock(&configfs_dirent_lock);
  72. if (target_sd->s_type & CONFIGFS_USET_DROPPING) {
  73. spin_unlock(&configfs_dirent_lock);
  74. config_item_put(item);
  75. kfree(sl);
  76. return -ENOENT;
  77. }
  78. list_add(&sl->sl_list, &target_sd->s_links);
  79. spin_unlock(&configfs_dirent_lock);
  80. ret = configfs_create_link(sl, parent_item->ci_dentry,
  81. dentry);
  82. if (ret) {
  83. spin_lock(&configfs_dirent_lock);
  84. list_del_init(&sl->sl_list);
  85. spin_unlock(&configfs_dirent_lock);
  86. config_item_put(item);
  87. kfree(sl);
  88. }
  89. }
  90. return ret;
  91. }
  92. static int get_target(const char *symname, struct nameidata *nd,
  93. struct config_item **target)
  94. {
  95. int ret;
  96. ret = path_lookup(symname, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, nd);
  97. if (!ret) {
  98. if (nd->path.dentry->d_sb == configfs_sb) {
  99. *target = configfs_get_config_item(nd->path.dentry);
  100. if (!*target) {
  101. ret = -ENOENT;
  102. path_put(&nd->path);
  103. }
  104. } else
  105. ret = -EPERM;
  106. }
  107. return ret;
  108. }
  109. int configfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  110. {
  111. int ret;
  112. struct nameidata nd;
  113. struct config_item *parent_item;
  114. struct config_item *target_item;
  115. struct config_item_type *type;
  116. ret = -EPERM; /* What lack-of-symlink returns */
  117. if (dentry->d_parent == configfs_sb->s_root)
  118. goto out;
  119. parent_item = configfs_get_config_item(dentry->d_parent);
  120. type = parent_item->ci_type;
  121. if (!type || !type->ct_item_ops ||
  122. !type->ct_item_ops->allow_link)
  123. goto out_put;
  124. ret = get_target(symname, &nd, &target_item);
  125. if (ret)
  126. goto out_put;
  127. ret = type->ct_item_ops->allow_link(parent_item, target_item);
  128. if (!ret) {
  129. ret = create_link(parent_item, target_item, dentry);
  130. if (ret && type->ct_item_ops->drop_link)
  131. type->ct_item_ops->drop_link(parent_item,
  132. target_item);
  133. }
  134. config_item_put(target_item);
  135. path_put(&nd.path);
  136. out_put:
  137. config_item_put(parent_item);
  138. out:
  139. return ret;
  140. }
  141. int configfs_unlink(struct inode *dir, struct dentry *dentry)
  142. {
  143. struct configfs_dirent *sd = dentry->d_fsdata;
  144. struct configfs_symlink *sl;
  145. struct config_item *parent_item;
  146. struct config_item_type *type;
  147. int ret;
  148. ret = -EPERM; /* What lack-of-symlink returns */
  149. if (!(sd->s_type & CONFIGFS_ITEM_LINK))
  150. goto out;
  151. BUG_ON(dentry->d_parent == configfs_sb->s_root);
  152. sl = sd->s_element;
  153. parent_item = configfs_get_config_item(dentry->d_parent);
  154. type = parent_item->ci_type;
  155. spin_lock(&configfs_dirent_lock);
  156. list_del_init(&sd->s_sibling);
  157. spin_unlock(&configfs_dirent_lock);
  158. configfs_drop_dentry(sd, dentry->d_parent);
  159. dput(dentry);
  160. configfs_put(sd);
  161. /*
  162. * drop_link() must be called before
  163. * list_del_init(&sl->sl_list), so that the order of
  164. * drop_link(this, target) and drop_item(target) is preserved.
  165. */
  166. if (type && type->ct_item_ops &&
  167. type->ct_item_ops->drop_link)
  168. type->ct_item_ops->drop_link(parent_item,
  169. sl->sl_target);
  170. spin_lock(&configfs_dirent_lock);
  171. list_del_init(&sl->sl_list);
  172. spin_unlock(&configfs_dirent_lock);
  173. /* Put reference from create_link() */
  174. config_item_put(sl->sl_target);
  175. kfree(sl);
  176. config_item_put(parent_item);
  177. ret = 0;
  178. out:
  179. return ret;
  180. }
  181. static int configfs_get_target_path(struct config_item * item, struct config_item * target,
  182. char *path)
  183. {
  184. char * s;
  185. int depth, size;
  186. depth = item_depth(item);
  187. size = item_path_length(target) + depth * 3 - 1;
  188. if (size > PATH_MAX)
  189. return -ENAMETOOLONG;
  190. pr_debug("%s: depth = %d, size = %d\n", __func__, depth, size);
  191. for (s = path; depth--; s += 3)
  192. strcpy(s,"../");
  193. fill_item_path(target, path, size);
  194. pr_debug("%s: path = '%s'\n", __func__, path);
  195. return 0;
  196. }
  197. static int configfs_getlink(struct dentry *dentry, char * path)
  198. {
  199. struct config_item *item, *target_item;
  200. int error = 0;
  201. item = configfs_get_config_item(dentry->d_parent);
  202. if (!item)
  203. return -EINVAL;
  204. target_item = configfs_get_config_item(dentry);
  205. if (!target_item) {
  206. config_item_put(item);
  207. return -EINVAL;
  208. }
  209. down_read(&configfs_rename_sem);
  210. error = configfs_get_target_path(item, target_item, path);
  211. up_read(&configfs_rename_sem);
  212. config_item_put(item);
  213. config_item_put(target_item);
  214. return error;
  215. }
  216. static void *configfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  217. {
  218. int error = -ENOMEM;
  219. unsigned long page = get_zeroed_page(GFP_KERNEL);
  220. if (page) {
  221. error = configfs_getlink(dentry, (char *)page);
  222. if (!error) {
  223. nd_set_link(nd, (char *)page);
  224. return (void *)page;
  225. }
  226. }
  227. nd_set_link(nd, ERR_PTR(error));
  228. return NULL;
  229. }
  230. static void configfs_put_link(struct dentry *dentry, struct nameidata *nd,
  231. void *cookie)
  232. {
  233. if (cookie) {
  234. unsigned long page = (unsigned long)cookie;
  235. free_page(page);
  236. }
  237. }
  238. const struct inode_operations configfs_symlink_inode_operations = {
  239. .follow_link = configfs_follow_link,
  240. .readlink = generic_readlink,
  241. .put_link = configfs_put_link,
  242. .setattr = configfs_setattr,
  243. };