getroot.c 7.4 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/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 <linux/security.h>
  35. #include <asm/system.h>
  36. #include <asm/uaccess.h>
  37. #include "nfs4_fs.h"
  38. #include "delegation.h"
  39. #include "internal.h"
  40. #define NFSDBG_FACILITY NFSDBG_CLIENT
  41. #define NFS_PARANOIA 1
  42. /*
  43. * get an NFS2/NFS3 root dentry from the root filehandle
  44. */
  45. struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh)
  46. {
  47. struct nfs_server *server = NFS_SB(sb);
  48. struct nfs_fsinfo fsinfo;
  49. struct nfs_fattr fattr;
  50. struct dentry *mntroot;
  51. struct inode *inode;
  52. int error;
  53. /* create a dummy root dentry with dummy inode for this superblock */
  54. if (!sb->s_root) {
  55. struct nfs_fh dummyfh;
  56. struct dentry *root;
  57. struct inode *iroot;
  58. memset(&dummyfh, 0, sizeof(dummyfh));
  59. memset(&fattr, 0, sizeof(fattr));
  60. nfs_fattr_init(&fattr);
  61. fattr.valid = NFS_ATTR_FATTR;
  62. fattr.type = NFDIR;
  63. fattr.mode = S_IFDIR | S_IRUSR | S_IWUSR;
  64. fattr.nlink = 2;
  65. iroot = nfs_fhget(sb, &dummyfh, &fattr);
  66. if (IS_ERR(iroot))
  67. return ERR_PTR(PTR_ERR(iroot));
  68. root = d_alloc_root(iroot);
  69. if (!root) {
  70. iput(iroot);
  71. return ERR_PTR(-ENOMEM);
  72. }
  73. sb->s_root = root;
  74. }
  75. /* get the actual root for this mount */
  76. fsinfo.fattr = &fattr;
  77. error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
  78. if (error < 0) {
  79. dprintk("nfs_get_root: getattr error = %d\n", -error);
  80. return ERR_PTR(error);
  81. }
  82. inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
  83. if (IS_ERR(inode)) {
  84. dprintk("nfs_get_root: get root inode failed\n");
  85. return ERR_PTR(PTR_ERR(inode));
  86. }
  87. /* root dentries normally start off anonymous and get spliced in later
  88. * if the dentry tree reaches them; however if the dentry already
  89. * exists, we'll pick it up at this point and use it as the root
  90. */
  91. mntroot = d_alloc_anon(inode);
  92. if (!mntroot) {
  93. iput(inode);
  94. dprintk("nfs_get_root: get root dentry failed\n");
  95. return ERR_PTR(-ENOMEM);
  96. }
  97. security_d_instantiate(mntroot, inode);
  98. if (!mntroot->d_op)
  99. mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
  100. return mntroot;
  101. }
  102. #ifdef CONFIG_NFS_V4
  103. /*
  104. * Do a simple pathwalk from the root FH of the server to the nominated target
  105. * of the mountpoint
  106. * - give error on symlinks
  107. * - give error on ".." occurring in the path
  108. * - follow traversals
  109. */
  110. int nfs4_path_walk(struct nfs_server *server,
  111. struct nfs_fh *mntfh,
  112. const char *path)
  113. {
  114. struct nfs_fsinfo fsinfo;
  115. struct nfs_fattr fattr;
  116. struct nfs_fh lastfh;
  117. struct qstr name;
  118. int ret;
  119. //int referral_count = 0;
  120. dprintk("--> nfs4_path_walk(,,%s)\n", path);
  121. fsinfo.fattr = &fattr;
  122. nfs_fattr_init(&fattr);
  123. if (*path++ != '/') {
  124. dprintk("nfs4_get_root: Path does not begin with a slash\n");
  125. return -EINVAL;
  126. }
  127. /* Start by getting the root filehandle from the server */
  128. ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
  129. if (ret < 0) {
  130. dprintk("nfs4_get_root: getroot error = %d\n", -ret);
  131. return ret;
  132. }
  133. if (fattr.type != NFDIR) {
  134. printk(KERN_ERR "nfs4_get_root:"
  135. " getroot encountered non-directory\n");
  136. return -ENOTDIR;
  137. }
  138. if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  139. printk(KERN_ERR "nfs4_get_root:"
  140. " getroot obtained referral\n");
  141. return -EREMOTE;
  142. }
  143. next_component:
  144. dprintk("Next: %s\n", path);
  145. /* extract the next bit of the path */
  146. if (!*path)
  147. goto path_walk_complete;
  148. name.name = path;
  149. while (*path && *path != '/')
  150. path++;
  151. name.len = path - (const char *) name.name;
  152. eat_dot_dir:
  153. while (*path == '/')
  154. path++;
  155. if (path[0] == '.' && (path[1] == '/' || !path[1])) {
  156. path += 2;
  157. goto eat_dot_dir;
  158. }
  159. if (path[0] == '.' && path[1] == '.' && (path[2] == '/' || !path[2])
  160. ) {
  161. printk(KERN_ERR "nfs4_get_root:"
  162. " Mount path contains reference to \"..\"\n");
  163. return -EINVAL;
  164. }
  165. /* lookup the next FH in the sequence */
  166. memcpy(&lastfh, mntfh, sizeof(lastfh));
  167. dprintk("LookupFH: %*.*s [%s]\n", name.len, name.len, name.name, path);
  168. ret = server->nfs_client->rpc_ops->lookupfh(server, &lastfh, &name,
  169. mntfh, &fattr);
  170. if (ret < 0) {
  171. dprintk("nfs4_get_root: getroot error = %d\n", -ret);
  172. return ret;
  173. }
  174. if (fattr.type != NFDIR) {
  175. printk(KERN_ERR "nfs4_get_root:"
  176. " lookupfh encountered non-directory\n");
  177. return -ENOTDIR;
  178. }
  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 */