getroot.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* getroot.c: get the root dentry for an NFS mount
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/time.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mm.h>
  12. #include <linux/string.h>
  13. #include <linux/stat.h>
  14. #include <linux/errno.h>
  15. #include <linux/unistd.h>
  16. #include <linux/sunrpc/clnt.h>
  17. #include <linux/sunrpc/stats.h>
  18. #include <linux/nfs_fs.h>
  19. #include <linux/nfs_mount.h>
  20. #include <linux/lockd/bind.h>
  21. #include <linux/seq_file.h>
  22. #include <linux/mount.h>
  23. #include <linux/vfs.h>
  24. #include <linux/namei.h>
  25. #include <linux/security.h>
  26. #include <linux/uaccess.h>
  27. #include "internal.h"
  28. #define NFSDBG_FACILITY NFSDBG_CLIENT
  29. /*
  30. * Set the superblock root dentry.
  31. * Note that this function frees the inode in case of error.
  32. */
  33. static int nfs_superblock_set_dummy_root(struct super_block *sb, struct inode *inode)
  34. {
  35. /* The mntroot acts as the dummy root dentry for this superblock */
  36. if (sb->s_root == NULL) {
  37. sb->s_root = d_make_root(inode);
  38. if (sb->s_root == NULL)
  39. return -ENOMEM;
  40. ihold(inode);
  41. /*
  42. * Ensure that this dentry is invisible to d_find_alias().
  43. * Otherwise, it may be spliced into the tree by
  44. * d_splice_alias if a parent directory from the same
  45. * filesystem gets mounted at a later time.
  46. * This again causes shrink_dcache_for_umount_subtree() to
  47. * Oops, since the test for IS_ROOT() will fail.
  48. */
  49. spin_lock(&d_inode(sb->s_root)->i_lock);
  50. spin_lock(&sb->s_root->d_lock);
  51. hlist_del_init(&sb->s_root->d_u.d_alias);
  52. spin_unlock(&sb->s_root->d_lock);
  53. spin_unlock(&d_inode(sb->s_root)->i_lock);
  54. }
  55. return 0;
  56. }
  57. /*
  58. * get an NFS2/NFS3 root dentry from the root filehandle
  59. */
  60. int nfs_get_root(struct super_block *s, struct fs_context *fc)
  61. {
  62. struct nfs_fs_context *ctx = nfs_fc2context(fc);
  63. struct nfs_server *server = NFS_SB(s);
  64. struct nfs_fsinfo fsinfo;
  65. struct dentry *root;
  66. struct inode *inode;
  67. char *name;
  68. int error = -ENOMEM;
  69. unsigned long kflags = 0, kflags_out = 0;
  70. name = kstrdup(fc->source, GFP_KERNEL);
  71. if (!name)
  72. goto out;
  73. /* get the actual root for this mount */
  74. fsinfo.fattr = nfs_alloc_fattr();
  75. if (fsinfo.fattr == NULL)
  76. goto out_name;
  77. fsinfo.fattr->label = nfs4_label_alloc(server, GFP_KERNEL);
  78. if (IS_ERR(fsinfo.fattr->label))
  79. goto out_fattr;
  80. error = server->nfs_client->rpc_ops->getroot(server, ctx->mntfh, &fsinfo);
  81. if (error < 0) {
  82. dprintk("nfs_get_root: getattr error = %d\n", -error);
  83. nfs_errorf(fc, "NFS: Couldn't getattr on root");
  84. goto out_label;
  85. }
  86. inode = nfs_fhget(s, ctx->mntfh, fsinfo.fattr, NULL);
  87. if (IS_ERR(inode)) {
  88. dprintk("nfs_get_root: get root inode failed\n");
  89. error = PTR_ERR(inode);
  90. nfs_errorf(fc, "NFS: Couldn't get root inode");
  91. goto out_label;
  92. }
  93. error = nfs_superblock_set_dummy_root(s, inode);
  94. if (error != 0)
  95. goto out_label;
  96. /* root dentries normally start off anonymous and get spliced in later
  97. * if the dentry tree reaches them; however if the dentry already
  98. * exists, we'll pick it up at this point and use it as the root
  99. */
  100. root = d_obtain_root(inode);
  101. if (IS_ERR(root)) {
  102. dprintk("nfs_get_root: get root dentry failed\n");
  103. error = PTR_ERR(root);
  104. nfs_errorf(fc, "NFS: Couldn't get root dentry");
  105. goto out_label;
  106. }
  107. security_d_instantiate(root, inode);
  108. spin_lock(&root->d_lock);
  109. if (IS_ROOT(root) && !root->d_fsdata &&
  110. !(root->d_flags & DCACHE_NFSFS_RENAMED)) {
  111. root->d_fsdata = name;
  112. name = NULL;
  113. }
  114. spin_unlock(&root->d_lock);
  115. fc->root = root;
  116. if (NFS_SB(s)->caps & NFS_CAP_SECURITY_LABEL)
  117. kflags |= SECURITY_LSM_NATIVE_LABELS;
  118. if (ctx->clone_data.sb) {
  119. if (d_inode(fc->root)->i_fop != &nfs_dir_operations) {
  120. error = -ESTALE;
  121. goto error_splat_root;
  122. }
  123. /* clone lsm security options from the parent to the new sb */
  124. error = security_sb_clone_mnt_opts(ctx->clone_data.sb,
  125. s, kflags, &kflags_out);
  126. } else {
  127. error = security_sb_set_mnt_opts(s, fc->security,
  128. kflags, &kflags_out);
  129. }
  130. if (error)
  131. goto error_splat_root;
  132. if (NFS_SB(s)->caps & NFS_CAP_SECURITY_LABEL &&
  133. !(kflags_out & SECURITY_LSM_NATIVE_LABELS))
  134. NFS_SB(s)->caps &= ~NFS_CAP_SECURITY_LABEL;
  135. nfs_setsecurity(inode, fsinfo.fattr, fsinfo.fattr->label);
  136. error = 0;
  137. out_label:
  138. nfs4_label_free(fsinfo.fattr->label);
  139. out_fattr:
  140. nfs_free_fattr(fsinfo.fattr);
  141. out_name:
  142. kfree(name);
  143. out:
  144. return error;
  145. error_splat_root:
  146. dput(fc->root);
  147. fc->root = NULL;
  148. goto out_label;
  149. }