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