export.c 4.9 KB

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