export.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  5. */
  6. #include <linux/spinlock.h>
  7. #include <linux/completion.h>
  8. #include <linux/buffer_head.h>
  9. #include <linux/exportfs.h>
  10. #include <linux/gfs2_ondisk.h>
  11. #include <linux/crc32.h>
  12. #include "gfs2.h"
  13. #include "incore.h"
  14. #include "dir.h"
  15. #include "glock.h"
  16. #include "glops.h"
  17. #include "inode.h"
  18. #include "super.h"
  19. #include "rgrp.h"
  20. #include "util.h"
  21. #define GFS2_SMALL_FH_SIZE 4
  22. #define GFS2_LARGE_FH_SIZE 8
  23. #define GFS2_OLD_FH_SIZE 10
  24. static int gfs2_encode_fh(struct inode *inode, __u32 *p, int *len,
  25. struct inode *parent)
  26. {
  27. __be32 *fh = (__force __be32 *)p;
  28. struct super_block *sb = inode->i_sb;
  29. struct gfs2_inode *ip = GFS2_I(inode);
  30. if (parent && (*len < GFS2_LARGE_FH_SIZE)) {
  31. *len = GFS2_LARGE_FH_SIZE;
  32. return FILEID_INVALID;
  33. } else if (*len < GFS2_SMALL_FH_SIZE) {
  34. *len = GFS2_SMALL_FH_SIZE;
  35. return FILEID_INVALID;
  36. }
  37. fh[0] = cpu_to_be32(ip->i_no_formal_ino >> 32);
  38. fh[1] = cpu_to_be32(ip->i_no_formal_ino & 0xFFFFFFFF);
  39. fh[2] = cpu_to_be32(ip->i_no_addr >> 32);
  40. fh[3] = cpu_to_be32(ip->i_no_addr & 0xFFFFFFFF);
  41. *len = GFS2_SMALL_FH_SIZE;
  42. if (!parent || inode == d_inode(sb->s_root))
  43. return *len;
  44. ip = GFS2_I(parent);
  45. fh[4] = cpu_to_be32(ip->i_no_formal_ino >> 32);
  46. fh[5] = cpu_to_be32(ip->i_no_formal_ino & 0xFFFFFFFF);
  47. fh[6] = cpu_to_be32(ip->i_no_addr >> 32);
  48. fh[7] = cpu_to_be32(ip->i_no_addr & 0xFFFFFFFF);
  49. *len = GFS2_LARGE_FH_SIZE;
  50. return *len;
  51. }
  52. struct get_name_filldir {
  53. struct dir_context ctx;
  54. struct gfs2_inum_host inum;
  55. char *name;
  56. };
  57. static bool get_name_filldir(struct dir_context *ctx, const char *name,
  58. int length, loff_t offset, u64 inum,
  59. unsigned int type)
  60. {
  61. struct get_name_filldir *gnfd =
  62. container_of(ctx, struct get_name_filldir, ctx);
  63. if (inum != gnfd->inum.no_addr)
  64. return true;
  65. memcpy(gnfd->name, name, length);
  66. gnfd->name[length] = 0;
  67. return false;
  68. }
  69. static int gfs2_get_name(struct dentry *parent, char *name,
  70. struct dentry *child)
  71. {
  72. struct inode *dir = d_inode(parent);
  73. struct inode *inode = d_inode(child);
  74. struct gfs2_inode *dip, *ip;
  75. struct get_name_filldir gnfd = {
  76. .ctx.actor = get_name_filldir,
  77. .name = name
  78. };
  79. struct gfs2_holder gh;
  80. int error;
  81. struct file_ra_state f_ra = { .start = 0 };
  82. if (!dir)
  83. return -EINVAL;
  84. if (!S_ISDIR(dir->i_mode) || !inode)
  85. return -EINVAL;
  86. dip = GFS2_I(dir);
  87. ip = GFS2_I(inode);
  88. *name = 0;
  89. gnfd.inum.no_addr = ip->i_no_addr;
  90. gnfd.inum.no_formal_ino = ip->i_no_formal_ino;
  91. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &gh);
  92. if (error)
  93. return error;
  94. error = gfs2_dir_read(dir, &gnfd.ctx, &f_ra);
  95. gfs2_glock_dq_uninit(&gh);
  96. if (!error && !*name)
  97. error = -ENOENT;
  98. return error;
  99. }
  100. static struct dentry *gfs2_get_parent(struct dentry *child)
  101. {
  102. return d_obtain_alias(gfs2_lookupi(d_inode(child), &gfs2_qdotdot, 1));
  103. }
  104. static struct dentry *gfs2_get_dentry(struct super_block *sb,
  105. struct gfs2_inum_host *inum)
  106. {
  107. struct gfs2_sbd *sdp = sb->s_fs_info;
  108. struct inode *inode;
  109. if (!inum->no_formal_ino)
  110. return ERR_PTR(-ESTALE);
  111. inode = gfs2_lookup_by_inum(sdp, inum->no_addr, inum->no_formal_ino,
  112. GFS2_BLKST_DINODE);
  113. if (IS_ERR(inode))
  114. return ERR_CAST(inode);
  115. return d_obtain_alias(inode);
  116. }
  117. static struct dentry *gfs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
  118. int fh_len, int fh_type)
  119. {
  120. struct gfs2_inum_host this;
  121. __be32 *fh = (__force __be32 *)fid->raw;
  122. switch (fh_type) {
  123. case GFS2_SMALL_FH_SIZE:
  124. case GFS2_LARGE_FH_SIZE:
  125. case GFS2_OLD_FH_SIZE:
  126. if (fh_len < GFS2_SMALL_FH_SIZE)
  127. return NULL;
  128. this.no_formal_ino = ((u64)be32_to_cpu(fh[0])) << 32;
  129. this.no_formal_ino |= be32_to_cpu(fh[1]);
  130. this.no_addr = ((u64)be32_to_cpu(fh[2])) << 32;
  131. this.no_addr |= be32_to_cpu(fh[3]);
  132. return gfs2_get_dentry(sb, &this);
  133. default:
  134. return NULL;
  135. }
  136. }
  137. static struct dentry *gfs2_fh_to_parent(struct super_block *sb, struct fid *fid,
  138. int fh_len, int fh_type)
  139. {
  140. struct gfs2_inum_host parent;
  141. __be32 *fh = (__force __be32 *)fid->raw;
  142. switch (fh_type) {
  143. case GFS2_LARGE_FH_SIZE:
  144. case GFS2_OLD_FH_SIZE:
  145. if (fh_len < GFS2_LARGE_FH_SIZE)
  146. return NULL;
  147. parent.no_formal_ino = ((u64)be32_to_cpu(fh[4])) << 32;
  148. parent.no_formal_ino |= be32_to_cpu(fh[5]);
  149. parent.no_addr = ((u64)be32_to_cpu(fh[6])) << 32;
  150. parent.no_addr |= be32_to_cpu(fh[7]);
  151. return gfs2_get_dentry(sb, &parent);
  152. default:
  153. return NULL;
  154. }
  155. }
  156. const struct export_operations gfs2_export_ops = {
  157. .encode_fh = gfs2_encode_fh,
  158. .fh_to_dentry = gfs2_fh_to_dentry,
  159. .fh_to_parent = gfs2_fh_to_parent,
  160. .get_name = gfs2_get_name,
  161. .get_parent = gfs2_get_parent,
  162. };