getroot.c 5.9 KB

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