getroot.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. return 0;
  56. }
  57. /*
  58. * get an NFS2/NFS3 root dentry from the root filehandle
  59. */
  60. struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh)
  61. {
  62. struct nfs_server *server = NFS_SB(sb);
  63. struct nfs_fsinfo fsinfo;
  64. struct nfs_fattr fattr;
  65. struct dentry *mntroot;
  66. struct inode *inode;
  67. int error;
  68. /* get the actual root for this mount */
  69. fsinfo.fattr = &fattr;
  70. error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
  71. if (error < 0) {
  72. dprintk("nfs_get_root: getattr error = %d\n", -error);
  73. return ERR_PTR(error);
  74. }
  75. inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
  76. if (IS_ERR(inode)) {
  77. dprintk("nfs_get_root: get root inode failed\n");
  78. return ERR_PTR(PTR_ERR(inode));
  79. }
  80. error = nfs_superblock_set_dummy_root(sb, inode);
  81. if (error != 0)
  82. return ERR_PTR(error);
  83. /* root dentries normally start off anonymous and get spliced in later
  84. * if the dentry tree reaches them; however if the dentry already
  85. * exists, we'll pick it up at this point and use it as the root
  86. */
  87. mntroot = d_alloc_anon(inode);
  88. if (!mntroot) {
  89. iput(inode);
  90. dprintk("nfs_get_root: get root dentry failed\n");
  91. return ERR_PTR(-ENOMEM);
  92. }
  93. security_d_instantiate(mntroot, inode);
  94. if (!mntroot->d_op)
  95. mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
  96. return mntroot;
  97. }
  98. #ifdef CONFIG_NFS_V4
  99. /*
  100. * Do a simple pathwalk from the root FH of the server to the nominated target
  101. * of the mountpoint
  102. * - give error on symlinks
  103. * - give error on ".." occurring in the path
  104. * - follow traversals
  105. */
  106. int nfs4_path_walk(struct nfs_server *server,
  107. struct nfs_fh *mntfh,
  108. const char *path)
  109. {
  110. struct nfs_fsinfo fsinfo;
  111. struct nfs_fattr fattr;
  112. struct nfs_fh lastfh;
  113. struct qstr name;
  114. int ret;
  115. dprintk("--> nfs4_path_walk(,,%s)\n", path);
  116. fsinfo.fattr = &fattr;
  117. nfs_fattr_init(&fattr);
  118. /* Eat leading slashes */
  119. while (*path == '/')
  120. path++;
  121. /* Start by getting the root filehandle from the server */
  122. ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
  123. if (ret < 0) {
  124. dprintk("nfs4_get_root: getroot error = %d\n", -ret);
  125. return ret;
  126. }
  127. if (fattr.type != NFDIR) {
  128. printk(KERN_ERR "nfs4_get_root:"
  129. " getroot encountered non-directory\n");
  130. return -ENOTDIR;
  131. }
  132. /* FIXME: It is quite valid for the server to return a referral here */
  133. if (fattr.valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  134. printk(KERN_ERR "nfs4_get_root:"
  135. " getroot obtained referral\n");
  136. return -EREMOTE;
  137. }
  138. next_component:
  139. dprintk("Next: %s\n", path);
  140. /* extract the next bit of the path */
  141. if (!*path)
  142. goto path_walk_complete;
  143. name.name = path;
  144. while (*path && *path != '/')
  145. path++;
  146. name.len = path - (const char *) name.name;
  147. if (name.len > NFS4_MAXNAMLEN)
  148. return -ENAMETOOLONG;
  149. eat_dot_dir:
  150. while (*path == '/')
  151. path++;
  152. if (path[0] == '.' && (path[1] == '/' || !path[1])) {
  153. path += 2;
  154. goto eat_dot_dir;
  155. }
  156. /* FIXME: Why shouldn't the user be able to use ".." in the path? */
  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. /* FIXME: Referrals are quite valid here too */
  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. /* get the info about the server and filesystem */
  201. error = nfs4_server_capabilities(server, mntfh);
  202. if (error < 0) {
  203. dprintk("nfs_get_root: getcaps error = %d\n",
  204. -error);
  205. return ERR_PTR(error);
  206. }
  207. /* get the actual root for this mount */
  208. error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
  209. if (error < 0) {
  210. dprintk("nfs_get_root: getattr error = %d\n", -error);
  211. return ERR_PTR(error);
  212. }
  213. inode = nfs_fhget(sb, mntfh, &fattr);
  214. if (IS_ERR(inode)) {
  215. dprintk("nfs_get_root: get root inode failed\n");
  216. return ERR_PTR(PTR_ERR(inode));
  217. }
  218. error = nfs_superblock_set_dummy_root(sb, inode);
  219. if (error != 0)
  220. return ERR_PTR(error);
  221. /* root dentries normally start off anonymous and get spliced in later
  222. * if the dentry tree reaches them; however if the dentry already
  223. * exists, we'll pick it up at this point and use it as the root
  224. */
  225. mntroot = d_alloc_anon(inode);
  226. if (!mntroot) {
  227. iput(inode);
  228. dprintk("nfs_get_root: get root dentry failed\n");
  229. return ERR_PTR(-ENOMEM);
  230. }
  231. security_d_instantiate(mntroot, inode);
  232. if (!mntroot->d_op)
  233. mntroot->d_op = server->nfs_client->rpc_ops->dentry_ops;
  234. dprintk("<-- nfs4_get_root()\n");
  235. return mntroot;
  236. }
  237. #endif /* CONFIG_NFS_V4 */