symlink.c 7.5 KB

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