getroot.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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/config.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/time.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/string.h>
  18. #include <linux/stat.h>
  19. #include <linux/errno.h>
  20. #include <linux/unistd.h>
  21. #include <linux/sunrpc/clnt.h>
  22. #include <linux/sunrpc/stats.h>
  23. #include <linux/nfs_fs.h>
  24. #include <linux/nfs_mount.h>
  25. #include <linux/nfs4_mount.h>
  26. #include <linux/lockd/bind.h>
  27. #include <linux/smp_lock.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/mount.h>
  30. #include <linux/nfs_idmap.h>
  31. #include <linux/vfs.h>
  32. #include <linux/namei.h>
  33. #include <linux/namespace.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. if (!mntroot->d_op)
  97. mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
  98. return mntroot;
  99. }
  100. #ifdef CONFIG_NFS_V4
  101. /*
  102. * Do a simple pathwalk from the root FH of the server to the nominated target
  103. * of the mountpoint
  104. * - give error on symlinks
  105. * - give error on ".." occurring in the path
  106. * - follow traversals
  107. */
  108. int nfs4_path_walk(struct nfs_server *server,
  109. struct nfs_fh *mntfh,
  110. const char *path)
  111. {
  112. struct nfs_fsinfo fsinfo;
  113. struct nfs_fattr fattr;
  114. struct nfs_fh lastfh;
  115. struct qstr name;
  116. int ret;
  117. //int referral_count = 0;
  118. dprintk("--> nfs4_path_walk(,,%s)\n", path);
  119. fsinfo.fattr = &fattr;
  120. nfs_fattr_init(&fattr);
  121. if (*path++ != '/') {
  122. dprintk("nfs4_get_root: Path does not begin with a slash\n");
  123. return -EINVAL;
  124. }
  125. /* Start by getting the root filehandle from the server */
  126. ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
  127. if (ret < 0) {
  128. dprintk("nfs4_get_root: getroot error = %d\n", -ret);
  129. return ret;
  130. }
  131. if (fattr.type != NFDIR) {
  132. printk(KERN_ERR "nfs4_get_root:"
  133. " getroot encountered non-directory\n");
  134. return -ENOTDIR;
  135. }
  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. if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || !path[2])
  158. ) {
  159. printk(KERN_ERR "nfs4_get_root:"
  160. " Mount path contains reference to \"..\"\n");
  161. return -EINVAL;
  162. }
  163. /* lookup the next FH in the sequence */
  164. memcpy(&lastfh, mntfh, sizeof(lastfh));
  165. dprintk("LookupFH: %*.*s [%s]\n", name.len, name.len, name.name, path);
  166. ret = server->nfs_client->rpc_ops->lookupfh(server, &lastfh, &name,
  167. mntfh, &fattr);
  168. if (ret < 0) {
  169. dprintk("nfs4_get_root: getroot error = %d\n", -ret);
  170. return ret;
  171. }
  172. if (fattr.type != NFDIR) {
  173. printk(KERN_ERR "nfs4_get_root:"
  174. " lookupfh encountered non-directory\n");
  175. return -ENOTDIR;
  176. }
  177. if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  178. printk(KERN_ERR "nfs4_get_root:"
  179. " lookupfh obtained referral\n");
  180. return -EREMOTE;
  181. }
  182. goto next_component;
  183. path_walk_complete:
  184. memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
  185. dprintk("<-- nfs4_path_walk() = 0\n");
  186. return 0;
  187. }
  188. /*
  189. * get an NFS4 root dentry from the root filehandle
  190. */
  191. struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh)
  192. {
  193. struct nfs_server *server = NFS_SB(sb);
  194. struct nfs_fattr fattr;
  195. struct dentry *mntroot;
  196. struct inode *inode;
  197. int error;
  198. dprintk("--> nfs4_get_root()\n");
  199. /* create a dummy root dentry with dummy inode for this superblock */
  200. if (!sb->s_root) {
  201. struct nfs_fh dummyfh;
  202. struct dentry *root;
  203. struct inode *iroot;
  204. memset(&dummyfh, 0, sizeof(dummyfh));
  205. memset(&fattr, 0, sizeof(fattr));
  206. nfs_fattr_init(&fattr);
  207. fattr.valid = NFS_ATTR_FATTR;
  208. fattr.type = NFDIR;
  209. fattr.mode = S_IFDIR | S_IRUSR | S_IWUSR;
  210. fattr.nlink = 2;
  211. iroot = nfs_fhget(sb, &dummyfh, &fattr);
  212. if (IS_ERR(iroot))
  213. return ERR_PTR(PTR_ERR(iroot));
  214. root = d_alloc_root(iroot);
  215. if (!root) {
  216. iput(iroot);
  217. return ERR_PTR(-ENOMEM);
  218. }
  219. sb->s_root = root;
  220. }
  221. /* get the info about the server and filesystem */
  222. error = nfs4_server_capabilities(server, mntfh);
  223. if (error < 0) {
  224. dprintk("nfs_get_root: getcaps error = %d\n",
  225. -error);
  226. return ERR_PTR(error);
  227. }
  228. /* get the actual root for this mount */
  229. error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
  230. if (error < 0) {
  231. dprintk("nfs_get_root: getattr error = %d\n", -error);
  232. return ERR_PTR(error);
  233. }
  234. inode = nfs_fhget(sb, mntfh, &fattr);
  235. if (IS_ERR(inode)) {
  236. dprintk("nfs_get_root: get root inode failed\n");
  237. return ERR_PTR(PTR_ERR(inode));
  238. }
  239. /* root dentries normally start off anonymous and get spliced in later
  240. * if the dentry tree reaches them; however if the dentry already
  241. * exists, we'll pick it up at this point and use it as the root
  242. */
  243. mntroot = d_alloc_anon(inode);
  244. if (!mntroot) {
  245. iput(inode);
  246. dprintk("nfs_get_root: get root dentry failed\n");
  247. return ERR_PTR(-ENOMEM);
  248. }
  249. if (!mntroot->d_op)
  250. mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
  251. dprintk("<-- nfs4_get_root()\n");
  252. return mntroot;
  253. }
  254. #endif /* CONFIG_NFS_V4 */