ops_export.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 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 v.2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <asm/semaphore.h>
  15. #include "gfs2.h"
  16. #include "dir.h"
  17. #include "glock.h"
  18. #include "glops.h"
  19. #include "inode.h"
  20. #include "ops_export.h"
  21. #include "rgrp.h"
  22. static struct dentry *gfs2_decode_fh(struct super_block *sb,
  23. __u32 *fh,
  24. int fh_len,
  25. int fh_type,
  26. int (*acceptable)(void *context,
  27. struct dentry *dentry),
  28. void *context)
  29. {
  30. struct gfs2_inum this, parent;
  31. atomic_inc(&get_v2sdp(sb)->sd_ops_export);
  32. if (fh_type != fh_len)
  33. return NULL;
  34. memset(&parent, 0, sizeof(struct gfs2_inum));
  35. switch (fh_type) {
  36. case 8:
  37. parent.no_formal_ino = ((uint64_t)be32_to_cpu(fh[4])) << 32;
  38. parent.no_formal_ino |= be32_to_cpu(fh[5]);
  39. parent.no_addr = ((uint64_t)be32_to_cpu(fh[6])) << 32;
  40. parent.no_addr |= be32_to_cpu(fh[7]);
  41. case 4:
  42. this.no_formal_ino = ((uint64_t)be32_to_cpu(fh[0])) << 32;
  43. this.no_formal_ino |= be32_to_cpu(fh[1]);
  44. this.no_addr = ((uint64_t)be32_to_cpu(fh[2])) << 32;
  45. this.no_addr |= be32_to_cpu(fh[3]);
  46. break;
  47. default:
  48. return NULL;
  49. }
  50. return gfs2_export_ops.find_exported_dentry(sb, &this, &parent,
  51. acceptable, context);
  52. }
  53. static int gfs2_encode_fh(struct dentry *dentry, __u32 *fh, int *len,
  54. int connectable)
  55. {
  56. struct inode *inode = dentry->d_inode;
  57. struct gfs2_inode *ip = get_v2ip(inode);
  58. struct gfs2_sbd *sdp = ip->i_sbd;
  59. atomic_inc(&sdp->sd_ops_export);
  60. if (*len < 4 || (connectable && *len < 8))
  61. return 255;
  62. fh[0] = ip->i_num.no_formal_ino >> 32;
  63. fh[0] = cpu_to_be32(fh[0]);
  64. fh[1] = ip->i_num.no_formal_ino & 0xFFFFFFFF;
  65. fh[1] = cpu_to_be32(fh[1]);
  66. fh[2] = ip->i_num.no_addr >> 32;
  67. fh[2] = cpu_to_be32(fh[2]);
  68. fh[3] = ip->i_num.no_addr & 0xFFFFFFFF;
  69. fh[3] = cpu_to_be32(fh[3]);
  70. *len = 4;
  71. if (!connectable || ip == get_v2ip(sdp->sd_root_dir))
  72. return *len;
  73. spin_lock(&dentry->d_lock);
  74. inode = dentry->d_parent->d_inode;
  75. ip = get_v2ip(inode);
  76. gfs2_inode_hold(ip);
  77. spin_unlock(&dentry->d_lock);
  78. fh[4] = ip->i_num.no_formal_ino >> 32;
  79. fh[4] = cpu_to_be32(fh[4]);
  80. fh[5] = ip->i_num.no_formal_ino & 0xFFFFFFFF;
  81. fh[5] = cpu_to_be32(fh[5]);
  82. fh[6] = ip->i_num.no_addr >> 32;
  83. fh[6] = cpu_to_be32(fh[6]);
  84. fh[7] = ip->i_num.no_addr & 0xFFFFFFFF;
  85. fh[7] = cpu_to_be32(fh[7]);
  86. *len = 8;
  87. gfs2_inode_put(ip);
  88. return *len;
  89. }
  90. struct get_name_filldir {
  91. struct gfs2_inum inum;
  92. char *name;
  93. };
  94. static int get_name_filldir(void *opaque, const char *name, unsigned int length,
  95. uint64_t offset, struct gfs2_inum *inum,
  96. unsigned int type)
  97. {
  98. struct get_name_filldir *gnfd = (struct get_name_filldir *)opaque;
  99. if (!gfs2_inum_equal(inum, &gnfd->inum))
  100. return 0;
  101. memcpy(gnfd->name, name, length);
  102. gnfd->name[length] = 0;
  103. return 1;
  104. }
  105. static int gfs2_get_name(struct dentry *parent, char *name,
  106. struct dentry *child)
  107. {
  108. struct inode *dir = parent->d_inode;
  109. struct inode *inode = child->d_inode;
  110. struct gfs2_inode *dip, *ip;
  111. struct get_name_filldir gnfd;
  112. struct gfs2_holder gh;
  113. uint64_t offset = 0;
  114. int error;
  115. if (!dir)
  116. return -EINVAL;
  117. atomic_inc(&get_v2sdp(dir->i_sb)->sd_ops_export);
  118. if (!S_ISDIR(dir->i_mode) || !inode)
  119. return -EINVAL;
  120. dip = get_v2ip(dir);
  121. ip = get_v2ip(inode);
  122. *name = 0;
  123. gnfd.inum = ip->i_num;
  124. gnfd.name = name;
  125. error = gfs2_glock_nq_init(dip->i_gl, LM_ST_SHARED, 0, &gh);
  126. if (error)
  127. return error;
  128. error = gfs2_dir_read(dip, &offset, &gnfd, get_name_filldir);
  129. gfs2_glock_dq_uninit(&gh);
  130. if (!error && !*name)
  131. error = -ENOENT;
  132. return error;
  133. }
  134. static struct dentry *gfs2_get_parent(struct dentry *child)
  135. {
  136. struct gfs2_inode *dip = get_v2ip(child->d_inode);
  137. struct qstr dotdot = { .name = "..", .len = 2 };
  138. struct gfs2_inode *ip;
  139. struct inode *inode;
  140. struct dentry *dentry;
  141. int error;
  142. atomic_inc(&dip->i_sbd->sd_ops_export);
  143. error = gfs2_lookupi(dip, &dotdot, 1, &ip);
  144. if (error)
  145. return ERR_PTR(error);
  146. inode = gfs2_ip2v(ip);
  147. gfs2_inode_put(ip);
  148. if (!inode)
  149. return ERR_PTR(-ENOMEM);
  150. dentry = d_alloc_anon(inode);
  151. if (!dentry) {
  152. iput(inode);
  153. return ERR_PTR(-ENOMEM);
  154. }
  155. return dentry;
  156. }
  157. static struct dentry *gfs2_get_dentry(struct super_block *sb, void *inum_p)
  158. {
  159. struct gfs2_sbd *sdp = get_v2sdp(sb);
  160. struct gfs2_inum *inum = (struct gfs2_inum *)inum_p;
  161. struct gfs2_holder i_gh, ri_gh, rgd_gh;
  162. struct gfs2_rgrpd *rgd;
  163. struct gfs2_inode *ip;
  164. struct inode *inode;
  165. struct dentry *dentry;
  166. int error;
  167. atomic_inc(&sdp->sd_ops_export);
  168. /* System files? */
  169. inode = gfs2_iget(sb, inum);
  170. if (inode) {
  171. ip = get_v2ip(inode);
  172. if (ip->i_num.no_formal_ino != inum->no_formal_ino) {
  173. iput(inode);
  174. return ERR_PTR(-ESTALE);
  175. }
  176. goto out_inode;
  177. }
  178. error = gfs2_glock_nq_num(sdp,
  179. inum->no_addr, &gfs2_inode_glops,
  180. LM_ST_SHARED, LM_FLAG_ANY | GL_LOCAL_EXCL,
  181. &i_gh);
  182. if (error)
  183. return ERR_PTR(error);
  184. error = gfs2_inode_get(i_gh.gh_gl, inum, NO_CREATE, &ip);
  185. if (error)
  186. goto fail;
  187. if (ip)
  188. goto out_ip;
  189. error = gfs2_rindex_hold(sdp, &ri_gh);
  190. if (error)
  191. goto fail;
  192. error = -EINVAL;
  193. rgd = gfs2_blk2rgrpd(sdp, inum->no_addr);
  194. if (!rgd)
  195. goto fail_rindex;
  196. error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_SHARED, 0, &rgd_gh);
  197. if (error)
  198. goto fail_rindex;
  199. error = -ESTALE;
  200. if (gfs2_get_block_type(rgd, inum->no_addr) != GFS2_BLKST_DINODE)
  201. goto fail_rgd;
  202. gfs2_glock_dq_uninit(&rgd_gh);
  203. gfs2_glock_dq_uninit(&ri_gh);
  204. error = gfs2_inode_get(i_gh.gh_gl, inum, CREATE, &ip);
  205. if (error)
  206. goto fail;
  207. error = gfs2_inode_refresh(ip);
  208. if (error) {
  209. gfs2_inode_put(ip);
  210. goto fail;
  211. }
  212. atomic_inc(&sdp->sd_fh2dentry_misses);
  213. out_ip:
  214. error = -EIO;
  215. if (ip->i_di.di_flags & GFS2_DIF_SYSTEM) {
  216. gfs2_inode_put(ip);
  217. goto fail;
  218. }
  219. gfs2_glock_dq_uninit(&i_gh);
  220. inode = gfs2_ip2v(ip);
  221. gfs2_inode_put(ip);
  222. if (!inode)
  223. return ERR_PTR(-ENOMEM);
  224. out_inode:
  225. dentry = d_alloc_anon(inode);
  226. if (!dentry) {
  227. iput(inode);
  228. return ERR_PTR(-ENOMEM);
  229. }
  230. return dentry;
  231. fail_rgd:
  232. gfs2_glock_dq_uninit(&rgd_gh);
  233. fail_rindex:
  234. gfs2_glock_dq_uninit(&ri_gh);
  235. fail:
  236. gfs2_glock_dq_uninit(&i_gh);
  237. return ERR_PTR(error);
  238. }
  239. struct export_operations gfs2_export_ops = {
  240. .decode_fh = gfs2_decode_fh,
  241. .encode_fh = gfs2_encode_fh,
  242. .get_name = gfs2_get_name,
  243. .get_parent = gfs2_get_parent,
  244. .get_dentry = gfs2_get_dentry,
  245. };