getroot.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #define NFSDBG_FACILITY NFSDBG_CLIENT
  32. /*
  33. * Set the superblock root dentry.
  34. * Note that this function frees the inode in case of error.
  35. */
  36. static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode)
  37. {
  38. /* The mntroot acts as the dummy root dentry for this superblock */
  39. if (sb->s_root == NULL) {
  40. sb->s_root = d_make_root(inode);
  41. if (sb->s_root == NULL)
  42. return -ENOMEM;
  43. ihold(inode);
  44. /*
  45. * Ensure that this dentry is invisible to d_find_alias().
  46. * Otherwise, it may be spliced into the tree by
  47. * d_materialise_unique if a parent directory from the same
  48. * filesystem gets mounted at a later time.
  49. * This again causes shrink_dcache_for_umount_subtree() to
  50. * Oops, since the test for IS_ROOT() will fail.
  51. */
  52. spin_lock(&sb->s_root->d_inode->i_lock);
  53. spin_lock(&sb->s_root->d_lock);
  54. hlist_del_init(&sb->s_root->d_alias);
  55. spin_unlock(&sb->s_root->d_lock);
  56. spin_unlock(&sb->s_root->d_inode->i_lock);
  57. }
  58. return 0;
  59. }
  60. /*
  61. * get an NFS2/NFS3 root dentry from the root filehandle
  62. */
  63. struct dentry *nfs_get_root(struct super_block *sb, struct nfs_fh *mntfh,
  64. const char *devname)
  65. {
  66. struct nfs_server *server = NFS_SB(sb);
  67. struct nfs_fsinfo fsinfo;
  68. struct dentry *ret;
  69. struct inode *inode;
  70. void *name = kstrdup(devname, GFP_KERNEL);
  71. int error;
  72. if (!name)
  73. return ERR_PTR(-ENOMEM);
  74. /* get the actual root for this mount */
  75. fsinfo.fattr = nfs_alloc_fattr();
  76. if (fsinfo.fattr == NULL) {
  77. kfree(name);
  78. return ERR_PTR(-ENOMEM);
  79. }
  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. spin_lock(&ret->d_lock);
  108. if (IS_ROOT(ret) && !(ret->d_flags & DCACHE_NFSFS_RENAMED)) {
  109. ret->d_fsdata = name;
  110. name = NULL;
  111. }
  112. spin_unlock(&ret->d_lock);
  113. out:
  114. if (name)
  115. kfree(name);
  116. nfs_free_fattr(fsinfo.fattr);
  117. return ret;
  118. }