ops_export.c 6.3 KB

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