ops_export.c 6.3 KB

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