ops_export.c 6.2 KB

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