export.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/exportfs.h>
  14. #include <linux/gfs2_ondisk.h>
  15. #include <linux/crc32.h>
  16. #include "gfs2.h"
  17. #include "incore.h"
  18. #include "dir.h"
  19. #include "glock.h"
  20. #include "glops.h"
  21. #include "inode.h"
  22. #include "super.h"
  23. #include "rgrp.h"
  24. #include "util.h"
  25. #define GFS2_SMALL_FH_SIZE 4
  26. #define GFS2_LARGE_FH_SIZE 8
  27. #define GFS2_OLD_FH_SIZE 10
  28. static int gfs2_encode_fh(struct dentry *dentry, __u32 *p, int *len,
  29. int connectable)
  30. {
  31. __be32 *fh = (__force __be32 *)p;
  32. struct inode *inode = dentry->d_inode;
  33. struct super_block *sb = inode->i_sb;
  34. struct gfs2_inode *ip = GFS2_I(inode);
  35. if (*len < GFS2_SMALL_FH_SIZE ||
  36. (connectable && *len < GFS2_LARGE_FH_SIZE))
  37. return 255;
  38. fh[0] = cpu_to_be32(ip->i_no_formal_ino >> 32);
  39. fh[1] = cpu_to_be32(ip->i_no_formal_ino & 0xFFFFFFFF);
  40. fh[2] = cpu_to_be32(ip->i_no_addr >> 32);
  41. fh[3] = cpu_to_be32(ip->i_no_addr & 0xFFFFFFFF);
  42. *len = GFS2_SMALL_FH_SIZE;
  43. if (!connectable || inode == sb->s_root->d_inode)
  44. return *len;
  45. spin_lock(&dentry->d_lock);
  46. inode = dentry->d_parent->d_inode;
  47. ip = GFS2_I(inode);
  48. igrab(inode);
  49. spin_unlock(&dentry->d_lock);
  50. fh[4] = cpu_to_be32(ip->i_no_formal_ino >> 32);
  51. fh[5] = cpu_to_be32(ip->i_no_formal_ino & 0xFFFFFFFF);
  52. fh[6] = cpu_to_be32(ip->i_no_addr >> 32);
  53. fh[7] = cpu_to_be32(ip->i_no_addr & 0xFFFFFFFF);
  54. *len = GFS2_LARGE_FH_SIZE;
  55. iput(inode);
  56. return *len;
  57. }
  58. struct get_name_filldir {
  59. struct gfs2_inum_host inum;
  60. char *name;
  61. };
  62. static int get_name_filldir(void *opaque, const char *name, int length,
  63. loff_t offset, u64 inum, unsigned int type)
  64. {
  65. struct get_name_filldir *gnfd = opaque;
  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 = parent->d_inode;
  76. struct inode *inode = child->d_inode;
  77. struct gfs2_inode *dip, *ip;
  78. struct get_name_filldir gnfd;
  79. struct gfs2_holder gh;
  80. u64 offset = 0;
  81. int error;
  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. gnfd.name = name;
  92. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &gh);
  93. if (error)
  94. return error;
  95. error = gfs2_dir_read(dir, &offset, &gnfd, get_name_filldir);
  96. gfs2_glock_dq_uninit(&gh);
  97. if (!error && !*name)
  98. error = -ENOENT;
  99. return error;
  100. }
  101. static struct dentry *gfs2_get_parent(struct dentry *child)
  102. {
  103. struct qstr dotdot;
  104. struct dentry *dentry;
  105. /*
  106. * XXX(hch): it would be a good idea to keep this around as a
  107. * static variable.
  108. */
  109. gfs2_str2qstr(&dotdot, "..");
  110. dentry = d_obtain_alias(gfs2_lookupi(child->d_inode, &dotdot, 1));
  111. if (!IS_ERR(dentry))
  112. dentry->d_op = &gfs2_dops;
  113. return dentry;
  114. }
  115. static struct dentry *gfs2_get_dentry(struct super_block *sb,
  116. struct gfs2_inum_host *inum)
  117. {
  118. struct gfs2_sbd *sdp = sb->s_fs_info;
  119. struct gfs2_holder i_gh;
  120. struct inode *inode;
  121. struct dentry *dentry;
  122. int error;
  123. inode = gfs2_ilookup(sb, inum->no_addr);
  124. if (inode) {
  125. if (GFS2_I(inode)->i_no_formal_ino != inum->no_formal_ino) {
  126. iput(inode);
  127. return ERR_PTR(-ESTALE);
  128. }
  129. goto out_inode;
  130. }
  131. error = gfs2_glock_nq_num(sdp, inum->no_addr, &gfs2_inode_glops,
  132. LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
  133. if (error)
  134. return ERR_PTR(error);
  135. error = gfs2_check_blk_type(sdp, inum->no_addr, GFS2_BLKST_DINODE);
  136. if (error)
  137. goto fail;
  138. inode = gfs2_inode_lookup(sb, DT_UNKNOWN, inum->no_addr, 0, 0);
  139. if (IS_ERR(inode)) {
  140. error = PTR_ERR(inode);
  141. goto fail;
  142. }
  143. error = gfs2_inode_refresh(GFS2_I(inode));
  144. if (error) {
  145. iput(inode);
  146. goto fail;
  147. }
  148. /* Pick up the works we bypass in gfs2_inode_lookup */
  149. if (inode->i_state & I_NEW)
  150. gfs2_set_iop(inode);
  151. if (GFS2_I(inode)->i_no_formal_ino != inum->no_formal_ino) {
  152. iput(inode);
  153. goto fail;
  154. }
  155. error = -EIO;
  156. if (GFS2_I(inode)->i_diskflags & GFS2_DIF_SYSTEM) {
  157. iput(inode);
  158. goto fail;
  159. }
  160. gfs2_glock_dq_uninit(&i_gh);
  161. out_inode:
  162. dentry = d_obtain_alias(inode);
  163. if (!IS_ERR(dentry))
  164. dentry->d_op = &gfs2_dops;
  165. return dentry;
  166. fail:
  167. gfs2_glock_dq_uninit(&i_gh);
  168. return ERR_PTR(error);
  169. }
  170. static struct dentry *gfs2_fh_to_dentry(struct super_block *sb, struct fid *fid,
  171. int fh_len, int fh_type)
  172. {
  173. struct gfs2_inum_host this;
  174. __be32 *fh = (__force __be32 *)fid->raw;
  175. switch (fh_type) {
  176. case GFS2_SMALL_FH_SIZE:
  177. case GFS2_LARGE_FH_SIZE:
  178. case GFS2_OLD_FH_SIZE:
  179. this.no_formal_ino = ((u64)be32_to_cpu(fh[0])) << 32;
  180. this.no_formal_ino |= be32_to_cpu(fh[1]);
  181. this.no_addr = ((u64)be32_to_cpu(fh[2])) << 32;
  182. this.no_addr |= be32_to_cpu(fh[3]);
  183. return gfs2_get_dentry(sb, &this);
  184. default:
  185. return NULL;
  186. }
  187. }
  188. static struct dentry *gfs2_fh_to_parent(struct super_block *sb, struct fid *fid,
  189. int fh_len, int fh_type)
  190. {
  191. struct gfs2_inum_host parent;
  192. __be32 *fh = (__force __be32 *)fid->raw;
  193. switch (fh_type) {
  194. case GFS2_LARGE_FH_SIZE:
  195. case GFS2_OLD_FH_SIZE:
  196. parent.no_formal_ino = ((u64)be32_to_cpu(fh[4])) << 32;
  197. parent.no_formal_ino |= be32_to_cpu(fh[5]);
  198. parent.no_addr = ((u64)be32_to_cpu(fh[6])) << 32;
  199. parent.no_addr |= be32_to_cpu(fh[7]);
  200. return gfs2_get_dentry(sb, &parent);
  201. default:
  202. return NULL;
  203. }
  204. }
  205. const struct export_operations gfs2_export_ops = {
  206. .encode_fh = gfs2_encode_fh,
  207. .fh_to_dentry = gfs2_fh_to_dentry,
  208. .fh_to_parent = gfs2_fh_to_parent,
  209. .get_name = gfs2_get_name,
  210. .get_parent = gfs2_get_parent,
  211. };