export.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 dentry *dentry, __u32 *p, int *len,
  28. int connectable)
  29. {
  30. __be32 *fh = (__force __be32 *)p;
  31. struct inode *inode = dentry->d_inode;
  32. struct super_block *sb = inode->i_sb;
  33. struct gfs2_inode *ip = GFS2_I(inode);
  34. if (*len < GFS2_SMALL_FH_SIZE ||
  35. (connectable && *len < GFS2_LARGE_FH_SIZE))
  36. return 255;
  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 (!connectable || inode == sb->s_root->d_inode)
  43. return *len;
  44. spin_lock(&dentry->d_lock);
  45. inode = dentry->d_parent->d_inode;
  46. ip = GFS2_I(inode);
  47. igrab(inode);
  48. spin_unlock(&dentry->d_lock);
  49. fh[4] = cpu_to_be32(ip->i_no_formal_ino >> 32);
  50. fh[5] = cpu_to_be32(ip->i_no_formal_ino & 0xFFFFFFFF);
  51. fh[6] = cpu_to_be32(ip->i_no_addr >> 32);
  52. fh[7] = cpu_to_be32(ip->i_no_addr & 0xFFFFFFFF);
  53. *len = GFS2_LARGE_FH_SIZE;
  54. iput(inode);
  55. return *len;
  56. }
  57. struct get_name_filldir {
  58. struct gfs2_inum_host inum;
  59. char *name;
  60. };
  61. static int get_name_filldir(void *opaque, const char *name, int length,
  62. loff_t offset, u64 inum, unsigned int type)
  63. {
  64. struct get_name_filldir *gnfd = opaque;
  65. if (inum != gnfd->inum.no_addr)
  66. return 0;
  67. memcpy(gnfd->name, name, length);
  68. gnfd->name[length] = 0;
  69. return 1;
  70. }
  71. static int gfs2_get_name(struct dentry *parent, char *name,
  72. struct dentry *child)
  73. {
  74. struct inode *dir = parent->d_inode;
  75. struct inode *inode = child->d_inode;
  76. struct gfs2_inode *dip, *ip;
  77. struct get_name_filldir gnfd;
  78. struct gfs2_holder gh;
  79. u64 offset = 0;
  80. int error;
  81. if (!dir)
  82. return -EINVAL;
  83. if (!S_ISDIR(dir->i_mode) || !inode)
  84. return -EINVAL;
  85. dip = GFS2_I(dir);
  86. ip = GFS2_I(inode);
  87. *name = 0;
  88. gnfd.inum.no_addr = ip->i_no_addr;
  89. gnfd.inum.no_formal_ino = ip->i_no_formal_ino;
  90. gnfd.name = name;
  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, &offset, &gnfd, get_name_filldir);
  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. struct dentry *dentry;
  103. dentry = d_obtain_alias(gfs2_lookupi(child->d_inode, &gfs2_qdotdot, 1));
  104. if (!IS_ERR(dentry))
  105. d_set_d_op(dentry, &gfs2_dops);
  106. return dentry;
  107. }
  108. static struct dentry *gfs2_get_dentry(struct super_block *sb,
  109. struct gfs2_inum_host *inum)
  110. {
  111. struct gfs2_sbd *sdp = sb->s_fs_info;
  112. struct inode *inode;
  113. struct dentry *dentry;
  114. inode = gfs2_ilookup(sb, inum->no_addr);
  115. if (inode) {
  116. if (GFS2_I(inode)->i_no_formal_ino != inum->no_formal_ino) {
  117. iput(inode);
  118. return ERR_PTR(-ESTALE);
  119. }
  120. goto out_inode;
  121. }
  122. inode = gfs2_lookup_by_inum(sdp, inum->no_addr, &inum->no_formal_ino,
  123. GFS2_BLKST_DINODE);
  124. if (IS_ERR(inode))
  125. return ERR_CAST(inode);
  126. out_inode:
  127. dentry = d_obtain_alias(inode);
  128. if (!IS_ERR(dentry))
  129. d_set_d_op(dentry, &gfs2_dops);
  130. return dentry;
  131. }
  132. static struct dentry *gfs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
  133. int fh_len, int fh_type)
  134. {
  135. struct gfs2_inum_host this;
  136. __be32 *fh = (__force __be32 *)fid->raw;
  137. switch (fh_type) {
  138. case GFS2_SMALL_FH_SIZE:
  139. case GFS2_LARGE_FH_SIZE:
  140. case GFS2_OLD_FH_SIZE:
  141. this.no_formal_ino = ((u64)be32_to_cpu(fh[0])) << 32;
  142. this.no_formal_ino |= be32_to_cpu(fh[1]);
  143. this.no_addr = ((u64)be32_to_cpu(fh[2])) << 32;
  144. this.no_addr |= be32_to_cpu(fh[3]);
  145. return gfs2_get_dentry(sb, &this);
  146. default:
  147. return NULL;
  148. }
  149. }
  150. static struct dentry *gfs2_fh_to_parent(struct super_block *sb, struct fid *fid,
  151. int fh_len, int fh_type)
  152. {
  153. struct gfs2_inum_host parent;
  154. __be32 *fh = (__force __be32 *)fid->raw;
  155. switch (fh_type) {
  156. case GFS2_LARGE_FH_SIZE:
  157. case GFS2_OLD_FH_SIZE:
  158. parent.no_formal_ino = ((u64)be32_to_cpu(fh[4])) << 32;
  159. parent.no_formal_ino |= be32_to_cpu(fh[5]);
  160. parent.no_addr = ((u64)be32_to_cpu(fh[6])) << 32;
  161. parent.no_addr |= be32_to_cpu(fh[7]);
  162. return gfs2_get_dentry(sb, &parent);
  163. default:
  164. return NULL;
  165. }
  166. }
  167. const struct export_operations gfs2_export_ops = {
  168. .encode_fh = gfs2_encode_fh,
  169. .fh_to_dentry = gfs2_fh_to_dentry,
  170. .fh_to_parent = gfs2_fh_to_parent,
  171. .get_name = gfs2_get_name,
  172. .get_parent = gfs2_get_parent,
  173. };