symlink.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. const 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. /*
  140. * This is really sick. What they wanted was a hybrid of
  141. * link(2) and symlink(2) - they wanted the target resolved
  142. * at syscall time (as link(2) would've done), be a directory
  143. * (which link(2) would've refused to do) *AND* be a deep
  144. * fucking magic, making the target busy from rmdir POV.
  145. * symlink(2) is nothing of that sort, and the locking it
  146. * gets matches the normal symlink(2) semantics. Without
  147. * attempts to resolve the target (which might very well
  148. * not even exist yet) done prior to locking the parent
  149. * directory. This perversion, OTOH, needs to resolve
  150. * the target, which would lead to obvious deadlocks if
  151. * attempted with any directories locked.
  152. *
  153. * Unfortunately, that garbage is userland ABI and we should've
  154. * said "no" back in 2005. Too late now, so we get to
  155. * play very ugly games with locking.
  156. *
  157. * Try *ANYTHING* of that sort in new code, and you will
  158. * really regret it. Just ask yourself - what could a BOFH
  159. * do to me and do I want to find it out first-hand?
  160. *
  161. * AV, a thoroughly annoyed bastard.
  162. */
  163. inode_unlock(dir);
  164. ret = get_target(symname, &path, &target_item, dentry->d_sb);
  165. inode_lock(dir);
  166. if (ret)
  167. goto out_put;
  168. if (dentry->d_inode || d_unhashed(dentry))
  169. ret = -EEXIST;
  170. else
  171. ret = inode_permission(dir, MAY_WRITE | MAY_EXEC);
  172. if (!ret)
  173. ret = type->ct_item_ops->allow_link(parent_item, target_item);
  174. if (!ret) {
  175. mutex_lock(&configfs_symlink_mutex);
  176. ret = create_link(parent_item, target_item, dentry);
  177. mutex_unlock(&configfs_symlink_mutex);
  178. if (ret && type->ct_item_ops->drop_link)
  179. type->ct_item_ops->drop_link(parent_item,
  180. target_item);
  181. }
  182. config_item_put(target_item);
  183. path_put(&path);
  184. out_put:
  185. config_item_put(parent_item);
  186. out:
  187. return ret;
  188. }
  189. int configfs_unlink(struct inode *dir, struct dentry *dentry)
  190. {
  191. struct configfs_dirent *sd = dentry->d_fsdata;
  192. struct configfs_symlink *sl;
  193. struct config_item *parent_item;
  194. const struct config_item_type *type;
  195. int ret;
  196. ret = -EPERM; /* What lack-of-symlink returns */
  197. if (!(sd->s_type & CONFIGFS_ITEM_LINK))
  198. goto out;
  199. sl = sd->s_element;
  200. parent_item = configfs_get_config_item(dentry->d_parent);
  201. type = parent_item->ci_type;
  202. spin_lock(&configfs_dirent_lock);
  203. list_del_init(&sd->s_sibling);
  204. spin_unlock(&configfs_dirent_lock);
  205. configfs_drop_dentry(sd, dentry->d_parent);
  206. dput(dentry);
  207. configfs_put(sd);
  208. /*
  209. * drop_link() must be called before
  210. * list_del_init(&sl->sl_list), so that the order of
  211. * drop_link(this, target) and drop_item(target) is preserved.
  212. */
  213. if (type && type->ct_item_ops &&
  214. type->ct_item_ops->drop_link)
  215. type->ct_item_ops->drop_link(parent_item,
  216. sl->sl_target);
  217. spin_lock(&configfs_dirent_lock);
  218. list_del_init(&sl->sl_list);
  219. spin_unlock(&configfs_dirent_lock);
  220. /* Put reference from create_link() */
  221. config_item_put(sl->sl_target);
  222. kfree(sl);
  223. config_item_put(parent_item);
  224. ret = 0;
  225. out:
  226. return ret;
  227. }
  228. static int configfs_get_target_path(struct config_item * item, struct config_item * target,
  229. char *path)
  230. {
  231. char * s;
  232. int depth, size;
  233. depth = item_depth(item);
  234. size = item_path_length(target) + depth * 3 - 1;
  235. if (size > PATH_MAX)
  236. return -ENAMETOOLONG;
  237. pr_debug("%s: depth = %d, size = %d\n", __func__, depth, size);
  238. for (s = path; depth--; s += 3)
  239. strcpy(s,"../");
  240. fill_item_path(target, path, size);
  241. pr_debug("%s: path = '%s'\n", __func__, path);
  242. return 0;
  243. }
  244. static int configfs_getlink(struct dentry *dentry, char * path)
  245. {
  246. struct config_item *item, *target_item;
  247. int error = 0;
  248. item = configfs_get_config_item(dentry->d_parent);
  249. if (!item)
  250. return -EINVAL;
  251. target_item = configfs_get_config_item(dentry);
  252. if (!target_item) {
  253. config_item_put(item);
  254. return -EINVAL;
  255. }
  256. down_read(&configfs_rename_sem);
  257. error = configfs_get_target_path(item, target_item, path);
  258. up_read(&configfs_rename_sem);
  259. config_item_put(item);
  260. config_item_put(target_item);
  261. return error;
  262. }
  263. static const char *configfs_get_link(struct dentry *dentry,
  264. struct inode *inode,
  265. struct delayed_call *done)
  266. {
  267. char *body;
  268. int error;
  269. if (!dentry)
  270. return ERR_PTR(-ECHILD);
  271. body = kzalloc(PAGE_SIZE, GFP_KERNEL);
  272. if (!body)
  273. return ERR_PTR(-ENOMEM);
  274. error = configfs_getlink(dentry, body);
  275. if (!error) {
  276. set_delayed_call(done, kfree_link, body);
  277. return body;
  278. }
  279. kfree(body);
  280. return ERR_PTR(error);
  281. }
  282. const struct inode_operations configfs_symlink_inode_operations = {
  283. .get_link = configfs_get_link,
  284. .setattr = configfs_setattr,
  285. };