getroot.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* getroot.c: get the root dentry for an NFS mount
  2. *
  3. * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/time.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/string.h>
  17. #include <linux/stat.h>
  18. #include <linux/errno.h>
  19. #include <linux/unistd.h>
  20. #include <linux/sunrpc/clnt.h>
  21. #include <linux/sunrpc/stats.h>
  22. #include <linux/nfs_fs.h>
  23. #include <linux/nfs_mount.h>
  24. #include <linux/nfs4_mount.h>
  25. #include <linux/lockd/bind.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/mount.h>
  28. #include <linux/nfs_idmap.h>
  29. #include <linux/vfs.h>
  30. #include <linux/namei.h>
  31. #include <linux/mnt_namespace.h>
  32. #include <linux/security.h>
  33. #include <asm/system.h>
  34. #include <asm/uaccess.h>
  35. #include "nfs4_fs.h"
  36. #include "delegation.h"
  37. #include "internal.h"
  38. #define NFSDBG_FACILITY NFSDBG_CLIENT
  39. /*
  40. * Set the superblock root dentry.
  41. * Note that this function frees the inode in case of error.
  42. */
  43. static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode)
  44. {
  45. /* The mntroot acts as the dummy root dentry for this superblock */
  46. if (sb->s_root == NULL) {
  47. sb->s_root = d_alloc_root(inode);
  48. if (sb->s_root == NULL) {
  49. iput(inode);
  50. return -ENOMEM;
  51. }
  52. /* Circumvent igrab(): we know the inode is not being freed */
  53. atomic_inc(&inode->i_count);
  54. /*
  55. * Ensure that this dentry is invisible to d_find_alias().
  56. * Otherwise, it may be spliced into the tree by
  57. * d_materialise_unique if a parent directory from the same
  58. * filesystem gets mounted at a later time.
  59. * This again causes shrink_dcache_for_umount_subtree() to
  60. * Oops, since the test for IS_ROOT() will fail.
  61. */
  62. spin_lock(&dcache_lock);
  63. list_del_init(&sb->s_root->d_alias);
  64. spin_unlock(&dcache_lock);
  65. }
  66. return 0;
  67. }
  68. /*
  69. * get an NFS2/NFS3 root dentry from the root filehandle
  70. */
  71. struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh)
  72. {
  73. struct nfs_server *server = NFS_SB(sb);
  74. struct nfs_fsinfo fsinfo;
  75. struct nfs_fattr fattr;
  76. struct dentry *mntroot;
  77. struct inode *inode;
  78. int error;
  79. /* get the actual root for this mount */
  80. fsinfo.fattr = &fattr;
  81. error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
  82. if (error < 0) {
  83. dprintk("nfs_get_root: getattr error = %d\n", -error);
  84. return ERR_PTR(error);
  85. }
  86. inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
  87. if (IS_ERR(inode)) {
  88. dprintk("nfs_get_root: get root inode failed\n");
  89. return ERR_PTR(PTR_ERR(inode));
  90. }
  91. error = nfs_superblock_set_dummy_root(sb, inode);
  92. if (error != 0)
  93. return ERR_PTR(error);
  94. /* root dentries normally start off anonymous and get spliced in later
  95. * if the dentry tree reaches them; however if the dentry already
  96. * exists, we'll pick it up at this point and use it as the root
  97. */
  98. mntroot = d_alloc_anon(inode);
  99. if (!mntroot) {
  100. iput(inode);
  101. dprintk("nfs_get_root: get root dentry failed\n");
  102. return ERR_PTR(-ENOMEM);
  103. }
  104. security_d_instantiate(mntroot, inode);
  105. if (!mntroot->d_op)
  106. mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
  107. return mntroot;
  108. }
  109. #ifdef CONFIG_NFS_V4
  110. /*
  111. * Do a simple pathwalk from the root FH of the server to the nominated target
  112. * of the mountpoint
  113. * - give error on symlinks
  114. * - give error on ".." occurring in the path
  115. * - follow traversals
  116. */
  117. int nfs4_path_walk(struct nfs_server *server,
  118. struct nfs_fh *mntfh,
  119. const char *path)
  120. {
  121. struct nfs_fsinfo fsinfo;
  122. struct nfs_fattr fattr;
  123. struct nfs_fh lastfh;
  124. struct qstr name;
  125. int ret;
  126. dprintk("--> nfs4_path_walk(,,%s)\n", path);
  127. fsinfo.fattr = &fattr;
  128. nfs_fattr_init(&fattr);
  129. /* Eat leading slashes */
  130. while (*path == '/')
  131. path++;
  132. /* Start by getting the root filehandle from the server */
  133. ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
  134. if (ret < 0) {
  135. dprintk("nfs4_get_root: getroot error = %d\n", -ret);
  136. return ret;
  137. }
  138. if (fattr.type != NFDIR) {
  139. printk(KERN_ERR "nfs4_get_root:"
  140. " getroot encountered non-directory\n");
  141. return -ENOTDIR;
  142. }
  143. /* FIXME: It is quite valid for the server to return a referral here */
  144. if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  145. printk(KERN_ERR "nfs4_get_root:"
  146. " getroot obtained referral\n");
  147. return -EREMOTE;
  148. }
  149. next_component:
  150. dprintk("Next: %s\n", path);
  151. /* extract the next bit of the path */
  152. if (!*path)
  153. goto path_walk_complete;
  154. name.name = path;
  155. while (*path && *path != '/')
  156. path++;
  157. name.len = path - (const char *) name.name;
  158. if (name.len > NFS4_MAXNAMLEN)
  159. return -ENAMETOOLONG;
  160. eat_dot_dir:
  161. while (*path == '/')
  162. path++;
  163. if (path[0] == '.' && (path[1] == '/' || !path[1])) {
  164. path += 2;
  165. goto eat_dot_dir;
  166. }
  167. /* FIXME: Why shouldn't the user be able to use ".." in the path? */
  168. if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || !path[2])
  169. ) {
  170. printk(KERN_ERR "nfs4_get_root:"
  171. " Mount path contains reference to \"..\"\n");
  172. return -EINVAL;
  173. }
  174. /* lookup the next FH in the sequence */
  175. memcpy(&lastfh, mntfh, sizeof(lastfh));
  176. dprintk("LookupFH: %*.*s [%s]\n", name.len, name.len, name.name, path);
  177. ret = server->nfs_client->rpc_ops->lookupfh(server, &lastfh, &name,
  178. mntfh, &fattr);
  179. if (ret < 0) {
  180. dprintk("nfs4_get_root: getroot error = %d\n", -ret);
  181. return ret;
  182. }
  183. if (fattr.type != NFDIR) {
  184. printk(KERN_ERR "nfs4_get_root:"
  185. " lookupfh encountered non-directory\n");
  186. return -ENOTDIR;
  187. }
  188. /* FIXME: Referrals are quite valid here too */
  189. if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  190. printk(KERN_ERR "nfs4_get_root:"
  191. " lookupfh obtained referral\n");
  192. return -EREMOTE;
  193. }
  194. goto next_component;
  195. path_walk_complete:
  196. memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
  197. dprintk("<-- nfs4_path_walk() = 0\n");
  198. return 0;
  199. }
  200. /*
  201. * get an NFS4 root dentry from the root filehandle
  202. */
  203. struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
  204. {
  205. struct nfs_server *server = NFS_SB(sb);
  206. struct nfs_fattr fattr;
  207. struct dentry *mntroot;
  208. struct inode *inode;
  209. int error;
  210. dprintk("--> nfs4_get_root()\n");
  211. /* get the info about the server and filesystem */
  212. error = nfs4_server_capabilities(server, mntfh);
  213. if (error < 0) {
  214. dprintk("nfs_get_root: getcaps error = %d\n",
  215. -error);
  216. return ERR_PTR(error);
  217. }
  218. /* get the actual root for this mount */
  219. error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
  220. if (error < 0) {
  221. dprintk("nfs_get_root: getattr error = %d\n", -error);
  222. return ERR_PTR(error);
  223. }
  224. inode = nfs_fhget(sb, mntfh, &fattr);
  225. if (IS_ERR(inode)) {
  226. dprintk("nfs_get_root: get root inode failed\n");
  227. return ERR_PTR(PTR_ERR(inode));
  228. }
  229. error = nfs_superblock_set_dummy_root(sb, inode);
  230. if (error != 0)
  231. return ERR_PTR(error);
  232. /* root dentries normally start off anonymous and get spliced in later
  233. * if the dentry tree reaches them; however if the dentry already
  234. * exists, we'll pick it up at this point and use it as the root
  235. */
  236. mntroot = d_alloc_anon(inode);
  237. if (!mntroot) {
  238. iput(inode);
  239. dprintk("nfs_get_root: get root dentry failed\n");
  240. return ERR_PTR(-ENOMEM);
  241. }
  242. security_d_instantiate(mntroot, inode);
  243. if (!mntroot->d_op)
  244. mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
  245. dprintk("<-- nfs4_get_root()\n");
  246. return mntroot;
  247. }
  248. #endif /* CONFIG_NFS_V4 */