getroot.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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/security.h>
  32. #include <asm/uaccess.h>
  33. #include "nfs4_fs.h"
  34. #include "delegation.h"
  35. #include "internal.h"
  36. #define NFSDBG_FACILITY NFSDBG_CLIENT
  37. /*
  38. * Set the superblock root dentry.
  39. * Note that this function frees the inode in case of error.
  40. */
  41. static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode)
  42. {
  43. /* The mntroot acts as the dummy root dentry for this superblock */
  44. if (sb->s_root == NULL) {
  45. sb->s_root = d_make_root(inode);
  46. if (sb->s_root == NULL)
  47. return -ENOMEM;
  48. ihold(inode);
  49. /*
  50. * Ensure that this dentry is invisible to d_find_alias().
  51. * Otherwise, it may be spliced into the tree by
  52. * d_materialise_unique if a parent directory from the same
  53. * filesystem gets mounted at a later time.
  54. * This again causes shrink_dcache_for_umount_subtree() to
  55. * Oops, since the test for IS_ROOT() will fail.
  56. */
  57. spin_lock(&sb->s_root->d_inode->i_lock);
  58. spin_lock(&sb->s_root->d_lock);
  59. list_del_init(&sb->s_root->d_u.d_alias);
  60. spin_unlock(&sb->s_root->d_lock);
  61. spin_unlock(&sb->s_root->d_inode->i_lock);
  62. }
  63. return 0;
  64. }
  65. /*
  66. * get an NFS2/NFS3 root dentry from the root filehandle
  67. */
  68. struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh,
  69. const char *devname)
  70. {
  71. struct nfs_server *server = NFS_SB(sb);
  72. struct nfs_fsinfo fsinfo;
  73. struct dentry *ret;
  74. struct inode *inode;
  75. void *name = kstrdup(devname, GFP_KERNEL);
  76. int error;
  77. if (!name)
  78. return ERR_PTR(-ENOMEM);
  79. /* get the actual root for this mount */
  80. fsinfo.fattr = nfs_alloc_fattr();
  81. if (fsinfo.fattr == NULL) {
  82. kfree(name);
  83. return ERR_PTR(-ENOMEM);
  84. }
  85. error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
  86. if (error < 0) {
  87. dprintk("nfs_get_root: getattr error = %d\n", -error);
  88. ret = ERR_PTR(error);
  89. goto out;
  90. }
  91. inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
  92. if (IS_ERR(inode)) {
  93. dprintk("nfs_get_root: get root inode failed\n");
  94. ret = ERR_CAST(inode);
  95. goto out;
  96. }
  97. error = nfs_superblock_set_dummy_root(sb, inode);
  98. if (error != 0) {
  99. ret = ERR_PTR(error);
  100. goto out;
  101. }
  102. /* root dentries normally start off anonymous and get spliced in later
  103. * if the dentry tree reaches them; however if the dentry already
  104. * exists, we'll pick it up at this point and use it as the root
  105. */
  106. ret = d_obtain_alias(inode);
  107. if (IS_ERR(ret)) {
  108. dprintk("nfs_get_root: get root dentry failed\n");
  109. goto out;
  110. }
  111. security_d_instantiate(ret, inode);
  112. spin_lock(&ret->d_lock);
  113. if (IS_ROOT(ret) && !(ret->d_flags & DCACHE_NFSFS_RENAMED)) {
  114. ret->d_fsdata = name;
  115. name = NULL;
  116. }
  117. spin_unlock(&ret->d_lock);
  118. out:
  119. if (name)
  120. kfree(name);
  121. nfs_free_fattr(fsinfo.fattr);
  122. return ret;
  123. }
  124. #ifdef CONFIG_NFS_V4
  125. int nfs4_get_rootfh(struct nfs_server *server, struct nfs_fh *mntfh)
  126. {
  127. struct nfs_fsinfo fsinfo;
  128. int ret = -ENOMEM;
  129. dprintk("--> nfs4_get_rootfh()\n");
  130. fsinfo.fattr = nfs_alloc_fattr();
  131. if (fsinfo.fattr == NULL)
  132. goto out;
  133. /* Start by getting the root filehandle from the server */
  134. ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
  135. if (ret < 0) {
  136. dprintk("nfs4_get_rootfh: getroot error = %d\n", -ret);
  137. goto out;
  138. }
  139. if (!(fsinfo.fattr->valid & NFS_ATTR_FATTR_TYPE)
  140. || !S_ISDIR(fsinfo.fattr->mode)) {
  141. printk(KERN_ERR "nfs4_get_rootfh:"
  142. " getroot encountered non-directory\n");
  143. ret = -ENOTDIR;
  144. goto out;
  145. }
  146. if (fsinfo.fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  147. printk(KERN_ERR "nfs4_get_rootfh:"
  148. " getroot obtained referral\n");
  149. ret = -EREMOTE;
  150. goto out;
  151. }
  152. memcpy(&server->fsid, &fsinfo.fattr->fsid, sizeof(server->fsid));
  153. out:
  154. nfs_free_fattr(fsinfo.fattr);
  155. dprintk("<-- nfs4_get_rootfh() = %d\n", ret);
  156. return ret;
  157. }
  158. /*
  159. * get an NFS4 root dentry from the root filehandle
  160. */
  161. struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh,
  162. const char *devname)
  163. {
  164. struct nfs_server *server = NFS_SB(sb);
  165. struct nfs_fattr *fattr = NULL;
  166. struct dentry *ret;
  167. struct inode *inode;
  168. void *name = kstrdup(devname, GFP_KERNEL);
  169. int error;
  170. dprintk("--> nfs4_get_root()\n");
  171. if (!name)
  172. return ERR_PTR(-ENOMEM);
  173. /* get the info about the server and filesystem */
  174. error = nfs4_server_capabilities(server, mntfh);
  175. if (error < 0) {
  176. dprintk("nfs_get_root: getcaps error = %d\n",
  177. -error);
  178. kfree(name);
  179. return ERR_PTR(error);
  180. }
  181. fattr = nfs_alloc_fattr();
  182. if (fattr == NULL) {
  183. kfree(name);
  184. return ERR_PTR(-ENOMEM);
  185. }
  186. /* get the actual root for this mount */
  187. error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
  188. if (error < 0) {
  189. dprintk("nfs_get_root: getattr error = %d\n", -error);
  190. ret = ERR_PTR(error);
  191. goto out;
  192. }
  193. if (fattr->valid & NFS_ATTR_FATTR_FSID &&
  194. !nfs_fsid_equal(&server->fsid, &fattr->fsid))
  195. memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
  196. inode = nfs_fhget(sb, mntfh, fattr);
  197. if (IS_ERR(inode)) {
  198. dprintk("nfs_get_root: get root inode failed\n");
  199. ret = ERR_CAST(inode);
  200. goto out;
  201. }
  202. error = nfs_superblock_set_dummy_root(sb, inode);
  203. if (error != 0) {
  204. ret = ERR_PTR(error);
  205. goto out;
  206. }
  207. /* root dentries normally start off anonymous and get spliced in later
  208. * if the dentry tree reaches them; however if the dentry already
  209. * exists, we'll pick it up at this point and use it as the root
  210. */
  211. ret = d_obtain_alias(inode);
  212. if (IS_ERR(ret)) {
  213. dprintk("nfs_get_root: get root dentry failed\n");
  214. goto out;
  215. }
  216. security_d_instantiate(ret, inode);
  217. spin_lock(&ret->d_lock);
  218. if (IS_ROOT(ret) && !(ret->d_flags & DCACHE_NFSFS_RENAMED)) {
  219. ret->d_fsdata = name;
  220. name = NULL;
  221. }
  222. spin_unlock(&ret->d_lock);
  223. out:
  224. if (name)
  225. kfree(name);
  226. nfs_free_fattr(fattr);
  227. dprintk("<-- nfs4_get_root()\n");
  228. return ret;
  229. }
  230. #endif /* CONFIG_NFS_V4 */