getroot.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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(&sb->s_root->d_inode->i_lock);
  61. spin_lock(&sb->s_root->d_lock);
  62. list_del_init(&sb->s_root->d_alias);
  63. spin_unlock(&sb->s_root->d_lock);
  64. spin_unlock(&sb->s_root->d_inode->i_lock);
  65. }
  66. return 0;
  67. }
  68. /*
  69. * get an NFS2/NFS3 root dentry from the root filehandle
  70. */
  71. struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh,
  72. const char *devname)
  73. {
  74. struct nfs_server *server = NFS_SB(sb);
  75. struct nfs_fsinfo fsinfo;
  76. struct dentry *ret;
  77. struct inode *inode;
  78. void *name = kstrdup(devname, GFP_KERNEL);
  79. int error;
  80. if (!name)
  81. return ERR_PTR(-ENOMEM);
  82. /* get the actual root for this mount */
  83. fsinfo.fattr = nfs_alloc_fattr();
  84. if (fsinfo.fattr == NULL) {
  85. kfree(name);
  86. return ERR_PTR(-ENOMEM);
  87. }
  88. error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
  89. if (error < 0) {
  90. dprintk("nfs_get_root: getattr error = %d\n", -error);
  91. ret = ERR_PTR(error);
  92. goto out;
  93. }
  94. inode = nfs_fhget(sb, mntfh, fsinfo.fattr);
  95. if (IS_ERR(inode)) {
  96. dprintk("nfs_get_root: get root inode failed\n");
  97. ret = ERR_CAST(inode);
  98. goto out;
  99. }
  100. error = nfs_superblock_set_dummy_root(sb, inode);
  101. if (error != 0) {
  102. ret = ERR_PTR(error);
  103. goto out;
  104. }
  105. /* root dentries normally start off anonymous and get spliced in later
  106. * if the dentry tree reaches them; however if the dentry already
  107. * exists, we'll pick it up at this point and use it as the root
  108. */
  109. ret = d_obtain_alias(inode);
  110. if (IS_ERR(ret)) {
  111. dprintk("nfs_get_root: get root dentry failed\n");
  112. goto out;
  113. }
  114. security_d_instantiate(ret, inode);
  115. spin_lock(&ret->d_lock);
  116. if (IS_ROOT(ret) && !(ret->d_flags & DCACHE_NFSFS_RENAMED)) {
  117. ret->d_fsdata = name;
  118. name = NULL;
  119. }
  120. spin_unlock(&ret->d_lock);
  121. out:
  122. if (name)
  123. kfree(name);
  124. nfs_free_fattr(fsinfo.fattr);
  125. return ret;
  126. }
  127. #ifdef CONFIG_NFS_V4
  128. int nfs4_get_rootfh(struct nfs_server *server, struct nfs_fh *mntfh)
  129. {
  130. struct nfs_fsinfo fsinfo;
  131. int ret = -ENOMEM;
  132. dprintk("--> nfs4_get_rootfh()\n");
  133. fsinfo.fattr = nfs_alloc_fattr();
  134. if (fsinfo.fattr == NULL)
  135. goto out;
  136. /* Start by getting the root filehandle from the server */
  137. ret = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
  138. if (ret < 0) {
  139. dprintk("nfs4_get_rootfh: getroot error = %d\n", -ret);
  140. goto out;
  141. }
  142. if (!(fsinfo.fattr->valid & NFS_ATTR_FATTR_TYPE)
  143. || !S_ISDIR(fsinfo.fattr->mode)) {
  144. printk(KERN_ERR "nfs4_get_rootfh:"
  145. " getroot encountered non-directory\n");
  146. ret = -ENOTDIR;
  147. goto out;
  148. }
  149. if (fsinfo.fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  150. printk(KERN_ERR "nfs4_get_rootfh:"
  151. " getroot obtained referral\n");
  152. ret = -EREMOTE;
  153. goto out;
  154. }
  155. memcpy(&server->fsid, &fsinfo.fattr->fsid, sizeof(server->fsid));
  156. out:
  157. nfs_free_fattr(fsinfo.fattr);
  158. dprintk("<-- nfs4_get_rootfh() = %d\n", ret);
  159. return ret;
  160. }
  161. /*
  162. * get an NFS4 root dentry from the root filehandle
  163. */
  164. struct dentry *nfs4_get_root(struct super_block *sb, struct nfs_fh *mntfh,
  165. const char *devname)
  166. {
  167. struct nfs_server *server = NFS_SB(sb);
  168. struct nfs_fattr *fattr = NULL;
  169. struct dentry *ret;
  170. struct inode *inode;
  171. void *name = kstrdup(devname, GFP_KERNEL);
  172. int error;
  173. dprintk("--> nfs4_get_root()\n");
  174. if (!name)
  175. return ERR_PTR(-ENOMEM);
  176. /* get the info about the server and filesystem */
  177. error = nfs4_server_capabilities(server, mntfh);
  178. if (error < 0) {
  179. dprintk("nfs_get_root: getcaps error = %d\n",
  180. -error);
  181. kfree(name);
  182. return ERR_PTR(error);
  183. }
  184. fattr = nfs_alloc_fattr();
  185. if (fattr == NULL) {
  186. kfree(name);
  187. return ERR_PTR(-ENOMEM);
  188. }
  189. /* get the actual root for this mount */
  190. error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
  191. if (error < 0) {
  192. dprintk("nfs_get_root: getattr error = %d\n", -error);
  193. ret = ERR_PTR(error);
  194. goto out;
  195. }
  196. if (fattr->valid & NFS_ATTR_FATTR_FSID &&
  197. !nfs_fsid_equal(&server->fsid, &fattr->fsid))
  198. memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
  199. inode = nfs_fhget(sb, mntfh, fattr);
  200. if (IS_ERR(inode)) {
  201. dprintk("nfs_get_root: get root inode failed\n");
  202. ret = ERR_CAST(inode);
  203. goto out;
  204. }
  205. error = nfs_superblock_set_dummy_root(sb, inode);
  206. if (error != 0) {
  207. ret = ERR_PTR(error);
  208. goto out;
  209. }
  210. /* root dentries normally start off anonymous and get spliced in later
  211. * if the dentry tree reaches them; however if the dentry already
  212. * exists, we'll pick it up at this point and use it as the root
  213. */
  214. ret = d_obtain_alias(inode);
  215. if (IS_ERR(ret)) {
  216. dprintk("nfs_get_root: get root dentry failed\n");
  217. goto out;
  218. }
  219. security_d_instantiate(ret, inode);
  220. spin_lock(&ret->d_lock);
  221. if (IS_ROOT(ret) && !(ret->d_flags & DCACHE_NFSFS_RENAMED)) {
  222. ret->d_fsdata = name;
  223. name = NULL;
  224. }
  225. spin_unlock(&ret->d_lock);
  226. out:
  227. if (name)
  228. kfree(name);
  229. nfs_free_fattr(fattr);
  230. dprintk("<-- nfs4_get_root()\n");
  231. return ret;
  232. }
  233. #endif /* CONFIG_NFS_V4 */