getroot.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 ([email protected])
  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), *clone_server;
  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_with_label(server);
  75. if (fsinfo.fattr == NULL)
  76. goto out_name;
  77. error = server->nfs_client->rpc_ops->getroot(server, ctx->mntfh, &fsinfo);
  78. if (error < 0) {
  79. dprintk("nfs_get_root: getattr error = %d\n", -error);
  80. nfs_errorf(fc, "NFS: Couldn't getattr on root");
  81. goto out_fattr;
  82. }
  83. inode = nfs_fhget(s, ctx->mntfh, fsinfo.fattr);
  84. if (IS_ERR(inode)) {
  85. dprintk("nfs_get_root: get root inode failed\n");
  86. error = PTR_ERR(inode);
  87. nfs_errorf(fc, "NFS: Couldn't get root inode");
  88. goto out_fattr;
  89. }
  90. error = nfs_superblock_set_dummy_root(s, inode);
  91. if (error != 0)
  92. goto out_fattr;
  93. /* root dentries normally start off anonymous and get spliced in later
  94. * if the dentry tree reaches them; however if the dentry already
  95. * exists, we'll pick it up at this point and use it as the root
  96. */
  97. root = d_obtain_root(inode);
  98. if (IS_ERR(root)) {
  99. dprintk("nfs_get_root: get root dentry failed\n");
  100. error = PTR_ERR(root);
  101. nfs_errorf(fc, "NFS: Couldn't get root dentry");
  102. goto out_fattr;
  103. }
  104. security_d_instantiate(root, inode);
  105. spin_lock(&root->d_lock);
  106. if (IS_ROOT(root) && !root->d_fsdata &&
  107. !(root->d_flags & DCACHE_NFSFS_RENAMED)) {
  108. root->d_fsdata = name;
  109. name = NULL;
  110. }
  111. spin_unlock(&root->d_lock);
  112. fc->root = root;
  113. if (server->caps & NFS_CAP_SECURITY_LABEL)
  114. kflags |= SECURITY_LSM_NATIVE_LABELS;
  115. if (ctx->clone_data.sb) {
  116. if (d_inode(fc->root)->i_fop != &nfs_dir_operations) {
  117. error = -ESTALE;
  118. goto error_splat_root;
  119. }
  120. /* clone lsm security options from the parent to the new sb */
  121. error = security_sb_clone_mnt_opts(ctx->clone_data.sb,
  122. s, kflags, &kflags_out);
  123. if (error)
  124. goto error_splat_root;
  125. clone_server = NFS_SB(ctx->clone_data.sb);
  126. server->has_sec_mnt_opts = clone_server->has_sec_mnt_opts;
  127. } else {
  128. error = security_sb_set_mnt_opts(s, fc->security,
  129. kflags, &kflags_out);
  130. }
  131. if (error)
  132. goto error_splat_root;
  133. if (server->caps & NFS_CAP_SECURITY_LABEL &&
  134. !(kflags_out & SECURITY_LSM_NATIVE_LABELS))
  135. server->caps &= ~NFS_CAP_SECURITY_LABEL;
  136. nfs_setsecurity(inode, fsinfo.fattr);
  137. error = 0;
  138. out_fattr:
  139. nfs_free_fattr(fsinfo.fattr);
  140. out_name:
  141. kfree(name);
  142. out:
  143. return error;
  144. error_splat_root:
  145. dput(fc->root);
  146. fc->root = NULL;
  147. goto out_fattr;
  148. }