symlink.c 7.2 KB

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