getroot.c 7.4 KB

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