ops_export.c 6.5 KB

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