symlink.c 6.8 KB

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