symlink.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. /* Protects attachments of new symlinks */
  32. DEFINE_MUTEX(configfs_symlink_mutex);
  33. static int item_depth(struct config_item * item)
  34. {
  35. struct config_item * p = item;
  36. int depth = 0;
  37. do { depth++; } while ((p = p->ci_parent) && !configfs_is_root(p));
  38. return depth;
  39. }
  40. static int item_path_length(struct config_item * item)
  41. {
  42. struct config_item * p = item;
  43. int length = 1;
  44. do {
  45. length += strlen(config_item_name(p)) + 1;
  46. p = p->ci_parent;
  47. } while (p && !configfs_is_root(p));
  48. return length;
  49. }
  50. static void fill_item_path(struct config_item * item, char * buffer, int length)
  51. {
  52. struct config_item * p;
  53. --length;
  54. for (p = item; p && !configfs_is_root(p); p = p->ci_parent) {
  55. int cur = strlen(config_item_name(p));
  56. /* back up enough to print this bus id with '/' */
  57. length -= cur;
  58. strncpy(buffer + length,config_item_name(p),cur);
  59. *(buffer + --length) = '/';
  60. }
  61. }
  62. static int create_link(struct config_item *parent_item,
  63. struct config_item *item,
  64. struct dentry *dentry)
  65. {
  66. struct configfs_dirent *target_sd = item->ci_dentry->d_fsdata;
  67. struct configfs_symlink *sl;
  68. int ret;
  69. ret = -ENOENT;
  70. if (!configfs_dirent_is_ready(target_sd))
  71. goto out;
  72. ret = -ENOMEM;
  73. sl = kmalloc(sizeof(struct configfs_symlink), GFP_KERNEL);
  74. if (sl) {
  75. sl->sl_target = config_item_get(item);
  76. spin_lock(&configfs_dirent_lock);
  77. if (target_sd->s_type & CONFIGFS_USET_DROPPING) {
  78. spin_unlock(&configfs_dirent_lock);
  79. config_item_put(item);
  80. kfree(sl);
  81. return -ENOENT;
  82. }
  83. list_add(&sl->sl_list, &target_sd->s_links);
  84. spin_unlock(&configfs_dirent_lock);
  85. ret = configfs_create_link(sl, parent_item->ci_dentry,
  86. dentry);
  87. if (ret) {
  88. spin_lock(&configfs_dirent_lock);
  89. list_del_init(&sl->sl_list);
  90. spin_unlock(&configfs_dirent_lock);
  91. config_item_put(item);
  92. kfree(sl);
  93. }
  94. }
  95. out:
  96. return ret;
  97. }
  98. static int get_target(const char *symname, struct path *path,
  99. struct config_item **target)
  100. {
  101. int ret;
  102. ret = kern_path(symname, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, path);
  103. if (!ret) {
  104. if (path->dentry->d_sb == configfs_sb) {
  105. *target = configfs_get_config_item(path->dentry);
  106. if (!*target) {
  107. ret = -ENOENT;
  108. path_put(path);
  109. }
  110. } else
  111. ret = -EPERM;
  112. }
  113. return ret;
  114. }
  115. int configfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  116. {
  117. int ret;
  118. struct path path;
  119. struct configfs_dirent *sd;
  120. struct config_item *parent_item;
  121. struct config_item *target_item = NULL;
  122. struct config_item_type *type;
  123. ret = -EPERM; /* What lack-of-symlink returns */
  124. if (dentry->d_parent == configfs_sb->s_root)
  125. goto out;
  126. sd = dentry->d_parent->d_fsdata;
  127. /*
  128. * Fake invisibility if dir belongs to a group/default groups hierarchy
  129. * being attached
  130. */
  131. ret = -ENOENT;
  132. if (!configfs_dirent_is_ready(sd))
  133. goto out;
  134. parent_item = configfs_get_config_item(dentry->d_parent);
  135. type = parent_item->ci_type;
  136. ret = -EPERM;
  137. if (!type || !type->ct_item_ops ||
  138. !type->ct_item_ops->allow_link)
  139. goto out_put;
  140. ret = get_target(symname, &path, &target_item);
  141. if (ret)
  142. goto out_put;
  143. ret = type->ct_item_ops->allow_link(parent_item, target_item);
  144. if (!ret) {
  145. mutex_lock(&configfs_symlink_mutex);
  146. ret = create_link(parent_item, target_item, dentry);
  147. mutex_unlock(&configfs_symlink_mutex);
  148. if (ret && type->ct_item_ops->drop_link)
  149. type->ct_item_ops->drop_link(parent_item,
  150. target_item);
  151. }
  152. config_item_put(target_item);
  153. path_put(&path);
  154. out_put:
  155. config_item_put(parent_item);
  156. out:
  157. return ret;
  158. }
  159. int configfs_unlink(struct inode *dir, struct dentry *dentry)
  160. {
  161. struct configfs_dirent *sd = dentry->d_fsdata;
  162. struct configfs_symlink *sl;
  163. struct config_item *parent_item;
  164. struct config_item_type *type;
  165. int ret;
  166. ret = -EPERM; /* What lack-of-symlink returns */
  167. if (!(sd->s_type & CONFIGFS_ITEM_LINK))
  168. goto out;
  169. BUG_ON(dentry->d_parent == configfs_sb->s_root);
  170. sl = sd->s_element;
  171. parent_item = configfs_get_config_item(dentry->d_parent);
  172. type = parent_item->ci_type;
  173. spin_lock(&configfs_dirent_lock);
  174. list_del_init(&sd->s_sibling);
  175. spin_unlock(&configfs_dirent_lock);
  176. configfs_drop_dentry(sd, dentry->d_parent);
  177. dput(dentry);
  178. configfs_put(sd);
  179. /*
  180. * drop_link() must be called before
  181. * list_del_init(&sl->sl_list), so that the order of
  182. * drop_link(this, target) and drop_item(target) is preserved.
  183. */
  184. if (type && type->ct_item_ops &&
  185. type->ct_item_ops->drop_link)
  186. type->ct_item_ops->drop_link(parent_item,
  187. sl->sl_target);
  188. spin_lock(&configfs_dirent_lock);
  189. list_del_init(&sl->sl_list);
  190. spin_unlock(&configfs_dirent_lock);
  191. /* Put reference from create_link() */
  192. config_item_put(sl->sl_target);
  193. kfree(sl);
  194. config_item_put(parent_item);
  195. ret = 0;
  196. out:
  197. return ret;
  198. }
  199. static int configfs_get_target_path(struct config_item * item, struct config_item * target,
  200. char *path)
  201. {
  202. char * s;
  203. int depth, size;
  204. depth = item_depth(item);
  205. size = item_path_length(target) + depth * 3 - 1;
  206. if (size > PATH_MAX)
  207. return -ENAMETOOLONG;
  208. pr_debug("%s: depth = %d, size = %d\n", __func__, depth, size);
  209. for (s = path; depth--; s += 3)
  210. strcpy(s,"../");
  211. fill_item_path(target, path, size);
  212. pr_debug("%s: path = '%s'\n", __func__, path);
  213. return 0;
  214. }
  215. static int configfs_getlink(struct dentry *dentry, char * path)
  216. {
  217. struct config_item *item, *target_item;
  218. int error = 0;
  219. item = configfs_get_config_item(dentry->d_parent);
  220. if (!item)
  221. return -EINVAL;
  222. target_item = configfs_get_config_item(dentry);
  223. if (!target_item) {
  224. config_item_put(item);
  225. return -EINVAL;
  226. }
  227. down_read(&configfs_rename_sem);
  228. error = configfs_get_target_path(item, target_item, path);
  229. up_read(&configfs_rename_sem);
  230. config_item_put(item);
  231. config_item_put(target_item);
  232. return error;
  233. }
  234. static void *configfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  235. {
  236. int error = -ENOMEM;
  237. unsigned long page = get_zeroed_page(GFP_KERNEL);
  238. if (page) {
  239. error = configfs_getlink(dentry, (char *)page);
  240. if (!error) {
  241. nd_set_link(nd, (char *)page);
  242. return (void *)page;
  243. }
  244. }
  245. nd_set_link(nd, ERR_PTR(error));
  246. return NULL;
  247. }
  248. static void configfs_put_link(struct dentry *dentry, struct nameidata *nd,
  249. void *cookie)
  250. {
  251. if (cookie) {
  252. unsigned long page = (unsigned long)cookie;
  253. free_page(page);
  254. }
  255. }
  256. const struct inode_operations configfs_symlink_inode_operations = {
  257. .follow_link = configfs_follow_link,
  258. .readlink = generic_readlink,
  259. .put_link = configfs_put_link,
  260. .setattr = configfs_setattr,
  261. };