getroot.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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_CAST(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_obtain_alias(inode);
  99. if (IS_ERR(mntroot)) {
  100. dprintk("nfs_get_root: get root dentry failed\n");
  101. return mntroot;
  102. }
  103. security_d_instantiate(mntroot, inode);
  104. if (!mntroot->d_op)
  105. mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
  106. return mntroot;
  107. }
  108. #ifdef CONFIG_NFS_V4
  109. /*
  110. * Do a simple pathwalk from the root FH of the server to the nominated target
  111. * of the mountpoint
  112. * - give error on symlinks
  113. * - give error on ".." occurring in the path
  114. * - follow traversals
  115. */
  116. int nfs4_path_walk(struct nfs_server *server,
  117. struct nfs_fh *mntfh,
  118. const char *path)
  119. {
  120. struct nfs_fsinfo fsinfo;
  121. struct nfs_fattr fattr;
  122. struct nfs_fh lastfh;
  123. struct qstr name;
  124. int ret;
  125. dprintk("--> nfs4_path_walk(,,%s)\n", path);
  126. fsinfo.fattr = &fattr;
  127. nfs_fattr_init(&fattr);
  128. /* Eat leading slashes */
  129. while (*path == '/')
  130. path++;
  131. /* Start by getting the root filehandle from the server */
  132. ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
  133. if (ret < 0) {
  134. dprintk("nfs4_get_root: getroot error = %d\n", -ret);
  135. return ret;
  136. }
  137. if (!S_ISDIR(fattr.mode)) {
  138. printk(KERN_ERR "nfs4_get_root:"
  139. " getroot encountered non-directory\n");
  140. return -ENOTDIR;
  141. }
  142. /* FIXME: It is quite valid for the server to return a referral here */
  143. if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  144. printk(KERN_ERR "nfs4_get_root:"
  145. " getroot obtained referral\n");
  146. return -EREMOTE;
  147. }
  148. next_component:
  149. dprintk("Next: %s\n", path);
  150. /* extract the next bit of the path */
  151. if (!*path)
  152. goto path_walk_complete;
  153. name.name = path;
  154. while (*path && *path != '/')
  155. path++;
  156. name.len = path - (const char *) name.name;
  157. if (name.len > NFS4_MAXNAMLEN)
  158. return -ENAMETOOLONG;
  159. eat_dot_dir:
  160. while (*path == '/')
  161. path++;
  162. if (path[0] == '.' && (path[1] == '/' || !path[1])) {
  163. path += 2;
  164. goto eat_dot_dir;
  165. }
  166. /* FIXME: Why shouldn't the user be able to use ".." in the path? */
  167. if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || !path[2])
  168. ) {
  169. printk(KERN_ERR "nfs4_get_root:"
  170. " Mount path contains reference to \"..\"\n");
  171. return -EINVAL;
  172. }
  173. /* lookup the next FH in the sequence */
  174. memcpy(&lastfh, mntfh, sizeof(lastfh));
  175. dprintk("LookupFH: %*.*s [%s]\n", name.len, name.len, name.name, path);
  176. ret = server->nfs_client->rpc_ops->lookupfh(server, &lastfh, &name,
  177. mntfh, &fattr);
  178. if (ret < 0) {
  179. dprintk("nfs4_get_root: getroot error = %d\n", -ret);
  180. return ret;
  181. }
  182. if (!S_ISDIR(fattr.mode)) {
  183. printk(KERN_ERR "nfs4_get_root:"
  184. " lookupfh encountered non-directory\n");
  185. return -ENOTDIR;
  186. }
  187. /* FIXME: Referrals are quite valid here too */
  188. if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  189. printk(KERN_ERR "nfs4_get_root:"
  190. " lookupfh obtained referral\n");
  191. return -EREMOTE;
  192. }
  193. goto next_component;
  194. path_walk_complete:
  195. memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
  196. dprintk("<-- nfs4_path_walk() = 0\n");
  197. return 0;
  198. }
  199. /*
  200. * get an NFS4 root dentry from the root filehandle
  201. */
  202. struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
  203. {
  204. struct nfs_server *server = NFS_SB(sb);
  205. struct nfs_fattr fattr;
  206. struct dentry *mntroot;
  207. struct inode *inode;
  208. int error;
  209. dprintk("--> nfs4_get_root()\n");
  210. /* get the info about the server and filesystem */
  211. error = nfs4_server_capabilities(server, mntfh);
  212. if (error < 0) {
  213. dprintk("nfs_get_root: getcaps error = %d\n",
  214. -error);
  215. return ERR_PTR(error);
  216. }
  217. /* get the actual root for this mount */
  218. error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
  219. if (error < 0) {
  220. dprintk("nfs_get_root: getattr error = %d\n", -error);
  221. return ERR_PTR(error);
  222. }
  223. inode = nfs_fhget(sb, mntfh, &fattr);
  224. if (IS_ERR(inode)) {
  225. dprintk("nfs_get_root: get root inode failed\n");
  226. return ERR_CAST(inode);
  227. }
  228. error = nfs_superblock_set_dummy_root(sb, inode);
  229. if (error != 0)
  230. return ERR_PTR(error);
  231. /* root dentries normally start off anonymous and get spliced in later
  232. * if the dentry tree reaches them; however if the dentry already
  233. * exists, we'll pick it up at this point and use it as the root
  234. */
  235. mntroot = d_obtain_alias(inode);
  236. if (IS_ERR(mntroot)) {
  237. dprintk("nfs_get_root: get root dentry failed\n");
  238. return mntroot;
  239. }
  240. security_d_instantiate(mntroot, inode);
  241. if (!mntroot->d_op)
  242. mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
  243. dprintk("<-- nfs4_get_root()\n");
  244. return mntroot;
  245. }
  246. #endif /* CONFIG_NFS_V4 */