getroot.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/lockd/bind.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/mount.h>
  27. #include <linux/vfs.h>
  28. #include <linux/namei.h>
  29. #include <linux/security.h>
  30. #include <asm/uaccess.h>
  31. #include "internal.h"
  32. #define NFSDBG_FACILITY NFSDBG_CLIENT
  33. /*
  34. * Set the superblock root dentry.
  35. * Note that this function frees the inode in case of error.
  36. */
  37. static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode)
  38. {
  39. /* The mntroot acts as the dummy root dentry for this superblock */
  40. if (sb->s_root == NULL) {
  41. sb->s_root = d_make_root(inode);
  42. if (sb->s_root == NULL)
  43. return -ENOMEM;
  44. ihold(inode);
  45. /*
  46. * Ensure that this dentry is invisible to d_find_alias().
  47. * Otherwise, it may be spliced into the tree by
  48. * d_splice_alias if a parent directory from the same
  49. * filesystem gets mounted at a later time.
  50. * This again causes shrink_dcache_for_umount_subtree() to
  51. * Oops, since the test for IS_ROOT() will fail.
  52. */
  53. spin_lock(&d_inode(sb->s_root)->i_lock);
  54. spin_lock(&sb->s_root->d_lock);
  55. hlist_del_init(&sb->s_root->d_u.d_alias);
  56. spin_unlock(&sb->s_root->d_lock);
  57. spin_unlock(&d_inode(sb->s_root)->i_lock);
  58. }
  59. return 0;
  60. }
  61. /*
  62. * get an NFS2/NFS3 root dentry from the root filehandle
  63. */
  64. struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh,
  65. const char *devname)
  66. {
  67. struct nfs_server *server = NFS_SB(sb);
  68. struct nfs_fsinfo fsinfo;
  69. struct dentry *ret;
  70. struct inode *inode;
  71. void *name = kstrdup(devname, GFP_KERNEL);
  72. int error;
  73. if (!name)
  74. return ERR_PTR(-ENOMEM);
  75. /* get the actual root for this mount */
  76. fsinfo.fattr = nfs_alloc_fattr();
  77. if (fsinfo.fattr == NULL) {
  78. kfree(name);
  79. return ERR_PTR(-ENOMEM);
  80. }
  81. error = server->nfs_client->rpc_ops->getroot(server, mntfh, &fsinfo);
  82. if (error < 0) {
  83. dprintk("nfs_get_root: getattr error = %d\n", -error);
  84. ret = ERR_PTR(error);
  85. goto out;
  86. }
  87. inode = nfs_fhget(sb, mntfh, fsinfo.fattr, NULL);
  88. if (IS_ERR(inode)) {
  89. dprintk("nfs_get_root: get root inode failed\n");
  90. ret = ERR_CAST(inode);
  91. goto out;
  92. }
  93. error = nfs_superblock_set_dummy_root(sb, inode);
  94. if (error != 0) {
  95. ret = ERR_PTR(error);
  96. goto out;
  97. }
  98. /* root dentries normally start off anonymous and get spliced in later
  99. * if the dentry tree reaches them; however if the dentry already
  100. * exists, we'll pick it up at this point and use it as the root
  101. */
  102. ret = d_obtain_root(inode);
  103. if (IS_ERR(ret)) {
  104. dprintk("nfs_get_root: get root dentry failed\n");
  105. goto out;
  106. }
  107. security_d_instantiate(ret, inode);
  108. spin_lock(&ret->d_lock);
  109. if (IS_ROOT(ret) && !ret->d_fsdata &&
  110. !(ret->d_flags & DCACHE_NFSFS_RENAMED)) {
  111. ret->d_fsdata = name;
  112. name = NULL;
  113. }
  114. spin_unlock(&ret->d_lock);
  115. out:
  116. kfree(name);
  117. nfs_free_fattr(fsinfo.fattr);
  118. return ret;
  119. }