ops_export.c 6.6 KB

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