vxfs_bmap.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) 2000-2001 Christoph Hellwig.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions, and the following disclaimer,
  10. * without modification.
  11. * 2. The name of the author may not be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. * Alternatively, this software may be distributed under the terms of the
  15. * GNU General Public License ("GPL").
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  21. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Veritas filesystem driver - filesystem to disk block mapping.
  31. */
  32. #include <linux/fs.h>
  33. #include <linux/buffer_head.h>
  34. #include <linux/kernel.h>
  35. #include "vxfs.h"
  36. #include "vxfs_inode.h"
  37. #include "vxfs_extern.h"
  38. #ifdef DIAGNOSTIC
  39. static void
  40. vxfs_typdump(struct vxfs_typed *typ)
  41. {
  42. printk(KERN_DEBUG "type=%Lu ", typ->vt_hdr >> VXFS_TYPED_TYPESHIFT);
  43. printk("offset=%Lx ", typ->vt_hdr & VXFS_TYPED_OFFSETMASK);
  44. printk("block=%x ", typ->vt_block);
  45. printk("size=%x\n", typ->vt_size);
  46. }
  47. #endif
  48. /**
  49. * vxfs_bmap_ext4 - do bmap for ext4 extents
  50. * @ip: pointer to the inode we do bmap for
  51. * @iblock: logical block.
  52. *
  53. * Description:
  54. * vxfs_bmap_ext4 performs the bmap operation for inodes with
  55. * ext4-style extents (which are much like the traditional UNIX
  56. * inode organisation).
  57. *
  58. * Returns:
  59. * The physical block number on success, else Zero.
  60. */
  61. static daddr_t
  62. vxfs_bmap_ext4(struct inode *ip, long bn)
  63. {
  64. struct super_block *sb = ip->i_sb;
  65. struct vxfs_inode_info *vip = VXFS_INO(ip);
  66. struct vxfs_sb_info *sbi = VXFS_SBI(sb);
  67. unsigned long bsize = sb->s_blocksize;
  68. u32 indsize = fs32_to_cpu(sbi, vip->vii_ext4.ve4_indsize);
  69. int i;
  70. if (indsize > sb->s_blocksize)
  71. goto fail_size;
  72. for (i = 0; i < VXFS_NDADDR; i++) {
  73. struct direct *d = vip->vii_ext4.ve4_direct + i;
  74. if (bn >= 0 && bn < fs32_to_cpu(sbi, d->size))
  75. return (bn + fs32_to_cpu(sbi, d->extent));
  76. bn -= fs32_to_cpu(sbi, d->size);
  77. }
  78. if ((bn / (indsize * indsize * bsize / 4)) == 0) {
  79. struct buffer_head *buf;
  80. daddr_t bno;
  81. __fs32 *indir;
  82. buf = sb_bread(sb,
  83. fs32_to_cpu(sbi, vip->vii_ext4.ve4_indir[0]));
  84. if (!buf || !buffer_mapped(buf))
  85. goto fail_buf;
  86. indir = (__fs32 *)buf->b_data;
  87. bno = fs32_to_cpu(sbi, indir[(bn / indsize) % (indsize * bn)]) +
  88. (bn % indsize);
  89. brelse(buf);
  90. return bno;
  91. } else
  92. printk(KERN_WARNING "no matching indir?");
  93. return 0;
  94. fail_size:
  95. printk("vxfs: indirect extent too big!\n");
  96. fail_buf:
  97. return 0;
  98. }
  99. /**
  100. * vxfs_bmap_indir - recursion for vxfs_bmap_typed
  101. * @ip: pointer to the inode we do bmap for
  102. * @indir: indirect block we start reading at
  103. * @size: size of the typed area to search
  104. * @block: partially result from further searches
  105. *
  106. * Description:
  107. * vxfs_bmap_indir reads a &struct vxfs_typed at @indir
  108. * and performs the type-defined action.
  109. *
  110. * Return Value:
  111. * The physical block number on success, else Zero.
  112. *
  113. * Note:
  114. * Kernelstack is rare. Unrecurse?
  115. */
  116. static daddr_t
  117. vxfs_bmap_indir(struct inode *ip, long indir, int size, long block)
  118. {
  119. struct vxfs_sb_info *sbi = VXFS_SBI(ip->i_sb);
  120. struct buffer_head *bp = NULL;
  121. daddr_t pblock = 0;
  122. int i;
  123. for (i = 0; i < size * VXFS_TYPED_PER_BLOCK(ip->i_sb); i++) {
  124. struct vxfs_typed *typ;
  125. int64_t off;
  126. bp = sb_bread(ip->i_sb,
  127. indir + (i / VXFS_TYPED_PER_BLOCK(ip->i_sb)));
  128. if (!bp || !buffer_mapped(bp))
  129. return 0;
  130. typ = ((struct vxfs_typed *)bp->b_data) +
  131. (i % VXFS_TYPED_PER_BLOCK(ip->i_sb));
  132. off = fs64_to_cpu(sbi, typ->vt_hdr) & VXFS_TYPED_OFFSETMASK;
  133. if (block < off) {
  134. brelse(bp);
  135. continue;
  136. }
  137. switch ((u_int32_t)(fs64_to_cpu(sbi, typ->vt_hdr) >>
  138. VXFS_TYPED_TYPESHIFT)) {
  139. case VXFS_TYPED_INDIRECT:
  140. pblock = vxfs_bmap_indir(ip,
  141. fs32_to_cpu(sbi, typ->vt_block),
  142. fs32_to_cpu(sbi, typ->vt_size),
  143. block - off);
  144. if (pblock == -2)
  145. break;
  146. goto out;
  147. case VXFS_TYPED_DATA:
  148. if ((block - off) >= fs32_to_cpu(sbi, typ->vt_size))
  149. break;
  150. pblock = fs32_to_cpu(sbi, typ->vt_block) + block - off;
  151. goto out;
  152. case VXFS_TYPED_INDIRECT_DEV4:
  153. case VXFS_TYPED_DATA_DEV4: {
  154. struct vxfs_typed_dev4 *typ4 =
  155. (struct vxfs_typed_dev4 *)typ;
  156. printk(KERN_INFO "\n\nTYPED_DEV4 detected!\n");
  157. printk(KERN_INFO "block: %llu\tsize: %lld\tdev: %d\n",
  158. fs64_to_cpu(sbi, typ4->vd4_block),
  159. fs64_to_cpu(sbi, typ4->vd4_size),
  160. fs32_to_cpu(sbi, typ4->vd4_dev));
  161. goto fail;
  162. }
  163. default:
  164. printk(KERN_ERR "%s:%d vt_hdr %llu\n", __func__,
  165. __LINE__, fs64_to_cpu(sbi, typ->vt_hdr));
  166. BUG();
  167. }
  168. brelse(bp);
  169. }
  170. fail:
  171. pblock = 0;
  172. out:
  173. brelse(bp);
  174. return (pblock);
  175. }
  176. /**
  177. * vxfs_bmap_typed - bmap for typed extents
  178. * @ip: pointer to the inode we do bmap for
  179. * @iblock: logical block
  180. *
  181. * Description:
  182. * Performs the bmap operation for typed extents.
  183. *
  184. * Return Value:
  185. * The physical block number on success, else Zero.
  186. */
  187. static daddr_t
  188. vxfs_bmap_typed(struct inode *ip, long iblock)
  189. {
  190. struct vxfs_inode_info *vip = VXFS_INO(ip);
  191. struct vxfs_sb_info *sbi = VXFS_SBI(ip->i_sb);
  192. daddr_t pblock = 0;
  193. int i;
  194. for (i = 0; i < VXFS_NTYPED; i++) {
  195. struct vxfs_typed *typ = vip->vii_org.typed + i;
  196. u64 hdr = fs64_to_cpu(sbi, typ->vt_hdr);
  197. int64_t off = (hdr & VXFS_TYPED_OFFSETMASK);
  198. #ifdef DIAGNOSTIC
  199. vxfs_typdump(typ);
  200. #endif
  201. if (iblock < off)
  202. continue;
  203. switch ((u32)(hdr >> VXFS_TYPED_TYPESHIFT)) {
  204. case VXFS_TYPED_INDIRECT:
  205. pblock = vxfs_bmap_indir(ip,
  206. fs32_to_cpu(sbi, typ->vt_block),
  207. fs32_to_cpu(sbi, typ->vt_size),
  208. iblock - off);
  209. if (pblock == -2)
  210. break;
  211. return (pblock);
  212. case VXFS_TYPED_DATA:
  213. if ((iblock - off) < fs32_to_cpu(sbi, typ->vt_size))
  214. return (fs32_to_cpu(sbi, typ->vt_block) +
  215. iblock - off);
  216. break;
  217. case VXFS_TYPED_INDIRECT_DEV4:
  218. case VXFS_TYPED_DATA_DEV4: {
  219. struct vxfs_typed_dev4 *typ4 =
  220. (struct vxfs_typed_dev4 *)typ;
  221. printk(KERN_INFO "\n\nTYPED_DEV4 detected!\n");
  222. printk(KERN_INFO "block: %llu\tsize: %lld\tdev: %d\n",
  223. fs64_to_cpu(sbi, typ4->vd4_block),
  224. fs64_to_cpu(sbi, typ4->vd4_size),
  225. fs32_to_cpu(sbi, typ4->vd4_dev));
  226. return 0;
  227. }
  228. default:
  229. BUG();
  230. }
  231. }
  232. return 0;
  233. }
  234. /**
  235. * vxfs_bmap1 - vxfs-internal bmap operation
  236. * @ip: pointer to the inode we do bmap for
  237. * @iblock: logical block
  238. *
  239. * Description:
  240. * vxfs_bmap1 perfoms a logical to physical block mapping
  241. * for vxfs-internal purposes.
  242. *
  243. * Return Value:
  244. * The physical block number on success, else Zero.
  245. */
  246. daddr_t
  247. vxfs_bmap1(struct inode *ip, long iblock)
  248. {
  249. struct vxfs_inode_info *vip = VXFS_INO(ip);
  250. if (VXFS_ISEXT4(vip))
  251. return vxfs_bmap_ext4(ip, iblock);
  252. if (VXFS_ISTYPED(vip))
  253. return vxfs_bmap_typed(ip, iblock);
  254. if (VXFS_ISNONE(vip))
  255. goto unsupp;
  256. if (VXFS_ISIMMED(vip))
  257. goto unsupp;
  258. printk(KERN_WARNING "vxfs: inode %ld has no valid orgtype (%x)\n",
  259. ip->i_ino, vip->vii_orgtype);
  260. BUG();
  261. unsupp:
  262. printk(KERN_WARNING "vxfs: inode %ld has an unsupported orgtype (%x)\n",
  263. ip->i_ino, vip->vii_orgtype);
  264. return 0;
  265. }