getroot.c 7.5 KB

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