ops_export.c 6.2 KB

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