getroot.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. //int referral_count = 0;
  119. dprintk("--> nfs4_path_walk(,,%s)\n", path);
  120. fsinfo.fattr = &fattr;
  121. nfs_fattr_init(&fattr);
  122. if (*path++ != '/') {
  123. dprintk("nfs4_get_root: Path does not begin with a slash\n");
  124. return -EINVAL;
  125. }
  126. /* Start by getting the root filehandle from the server */
  127. ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
  128. if (ret < 0) {
  129. dprintk("nfs4_get_root: getroot error = %d\n", -ret);
  130. return ret;
  131. }
  132. if (fattr.type != NFDIR) {
  133. printk(KERN_ERR "nfs4_get_root:"
  134. " getroot encountered non-directory\n");
  135. return -ENOTDIR;
  136. }
  137. if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  138. printk(KERN_ERR "nfs4_get_root:"
  139. " getroot obtained referral\n");
  140. return -EREMOTE;
  141. }
  142. next_component:
  143. dprintk("Next: %s\n", path);
  144. /* extract the next bit of the path */
  145. if (!*path)
  146. goto path_walk_complete;
  147. name.name = path;
  148. while (*path && *path != '/')
  149. path++;
  150. name.len = path - (const char *) name.name;
  151. eat_dot_dir:
  152. while (*path == '/')
  153. path++;
  154. if (path[0] == '.' && (path[1] == '/' || !path[1])) {
  155. path += 2;
  156. goto eat_dot_dir;
  157. }
  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. if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  179. printk(KERN_ERR "nfs4_get_root:"
  180. " lookupfh obtained referral\n");
  181. return -EREMOTE;
  182. }
  183. goto next_component;
  184. path_walk_complete:
  185. memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
  186. dprintk("<-- nfs4_path_walk() = 0\n");
  187. return 0;
  188. }
  189. /*
  190. * get an NFS4 root dentry from the root filehandle
  191. */
  192. struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
  193. {
  194. struct nfs_server *server = NFS_SB(sb);
  195. struct nfs_fattr fattr;
  196. struct dentry *mntroot;
  197. struct inode *inode;
  198. int error;
  199. dprintk("--> nfs4_get_root()\n");
  200. /* create a dummy root dentry with dummy inode for this superblock */
  201. if (!sb->s_root) {
  202. struct nfs_fh dummyfh;
  203. struct dentry *root;
  204. struct inode *iroot;
  205. memset(&dummyfh, 0, sizeof(dummyfh));
  206. memset(&fattr, 0, sizeof(fattr));
  207. nfs_fattr_init(&fattr);
  208. fattr.valid = NFS_ATTR_FATTR;
  209. fattr.type = NFDIR;
  210. fattr.mode = S_IFDIR | S_IRUSR | S_IWUSR;
  211. fattr.nlink = 2;
  212. iroot = nfs_fhget(sb, &dummyfh, &fattr);
  213. if (IS_ERR(iroot))
  214. return ERR_PTR(PTR_ERR(iroot));
  215. root = d_alloc_root(iroot);
  216. if (!root) {
  217. iput(iroot);
  218. return ERR_PTR(-ENOMEM);
  219. }
  220. sb->s_root = root;
  221. }
  222. /* get the info about the server and filesystem */
  223. error = nfs4_server_capabilities(server, mntfh);
  224. if (error < 0) {
  225. dprintk("nfs_get_root: getcaps error = %d\n",
  226. -error);
  227. return ERR_PTR(error);
  228. }
  229. /* get the actual root for this mount */
  230. error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
  231. if (error < 0) {
  232. dprintk("nfs_get_root: getattr error = %d\n", -error);
  233. return ERR_PTR(error);
  234. }
  235. inode = nfs_fhget(sb, mntfh, &fattr);
  236. if (IS_ERR(inode)) {
  237. dprintk("nfs_get_root: get root inode failed\n");
  238. return ERR_PTR(PTR_ERR(inode));
  239. }
  240. /* root dentries normally start off anonymous and get spliced in later
  241. * if the dentry tree reaches them; however if the dentry already
  242. * exists, we'll pick it up at this point and use it as the root
  243. */
  244. mntroot = d_alloc_anon(inode);
  245. if (!mntroot) {
  246. iput(inode);
  247. dprintk("nfs_get_root: get root dentry failed\n");
  248. return ERR_PTR(-ENOMEM);
  249. }
  250. security_d_instantiate(mntroot, inode);
  251. if (!mntroot->d_op)
  252. mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
  253. dprintk("<-- nfs4_get_root()\n");
  254. return mntroot;
  255. }
  256. #endif /* CONFIG_NFS_V4 */