ops_export.c 6.6 KB

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