wrapper.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * linux/fs/hfsplus/wrapper.c
  3. *
  4. * Copyright (C) 2001
  5. * Brad Boyer (flar@allandria.com)
  6. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  7. *
  8. * Handling of HFS wrappers around HFS+ volumes
  9. */
  10. #include <linux/fs.h>
  11. #include <linux/blkdev.h>
  12. #include <linux/cdrom.h>
  13. #include <linux/genhd.h>
  14. #include <asm/unaligned.h>
  15. #include "hfsplus_fs.h"
  16. #include "hfsplus_raw.h"
  17. struct hfsplus_wd {
  18. u32 ablk_size;
  19. u16 ablk_start;
  20. u16 embed_start;
  21. u16 embed_count;
  22. };
  23. static void hfsplus_end_io_sync(struct bio *bio, int err)
  24. {
  25. if (err)
  26. clear_bit(BIO_UPTODATE, &bio->bi_flags);
  27. complete(bio->bi_private);
  28. }
  29. int hfsplus_submit_bio(struct block_device *bdev, sector_t sector,
  30. void *data, int rw)
  31. {
  32. DECLARE_COMPLETION_ONSTACK(wait);
  33. struct bio *bio;
  34. bio = bio_alloc(GFP_NOIO, 1);
  35. bio->bi_sector = sector;
  36. bio->bi_bdev = bdev;
  37. bio->bi_end_io = hfsplus_end_io_sync;
  38. bio->bi_private = &wait;
  39. /*
  40. * We always submit one sector at a time, so bio_add_page must not fail.
  41. */
  42. if (bio_add_page(bio, virt_to_page(data), HFSPLUS_SECTOR_SIZE,
  43. offset_in_page(data)) != HFSPLUS_SECTOR_SIZE)
  44. BUG();
  45. submit_bio(rw, bio);
  46. wait_for_completion(&wait);
  47. if (!bio_flagged(bio, BIO_UPTODATE))
  48. return -EIO;
  49. return 0;
  50. }
  51. static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
  52. {
  53. u32 extent;
  54. u16 attrib;
  55. __be16 sig;
  56. sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
  57. if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
  58. sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
  59. return 0;
  60. attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
  61. if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
  62. !(attrib & HFSP_WRAP_ATTRIB_SPARED))
  63. return 0;
  64. wd->ablk_size = be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
  65. if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
  66. return 0;
  67. if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
  68. return 0;
  69. wd->ablk_start = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
  70. extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
  71. wd->embed_start = (extent >> 16) & 0xFFFF;
  72. wd->embed_count = extent & 0xFFFF;
  73. return 1;
  74. }
  75. static int hfsplus_get_last_session(struct super_block *sb,
  76. sector_t *start, sector_t *size)
  77. {
  78. struct cdrom_multisession ms_info;
  79. struct cdrom_tocentry te;
  80. int res;
  81. /* default values */
  82. *start = 0;
  83. *size = sb->s_bdev->bd_inode->i_size >> 9;
  84. if (HFSPLUS_SB(sb)->session >= 0) {
  85. te.cdte_track = HFSPLUS_SB(sb)->session;
  86. te.cdte_format = CDROM_LBA;
  87. res = ioctl_by_bdev(sb->s_bdev, CDROMREADTOCENTRY, (unsigned long)&te);
  88. if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
  89. *start = (sector_t)te.cdte_addr.lba << 2;
  90. return 0;
  91. }
  92. printk(KERN_ERR "hfs: invalid session number or type of track\n");
  93. return -EINVAL;
  94. }
  95. ms_info.addr_format = CDROM_LBA;
  96. res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION, (unsigned long)&ms_info);
  97. if (!res && ms_info.xa_flag)
  98. *start = (sector_t)ms_info.addr.lba << 2;
  99. return 0;
  100. }
  101. /* Find the volume header and fill in some minimum bits in superblock */
  102. /* Takes in super block, returns true if good data read */
  103. int hfsplus_read_wrapper(struct super_block *sb)
  104. {
  105. struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
  106. struct hfsplus_wd wd;
  107. sector_t part_start, part_size;
  108. u32 blocksize;
  109. int error = 0;
  110. error = -EINVAL;
  111. blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
  112. if (!blocksize)
  113. goto out;
  114. if (hfsplus_get_last_session(sb, &part_start, &part_size))
  115. goto out;
  116. if ((u64)part_start + part_size > 0x100000000ULL) {
  117. pr_err("hfs: volumes larger than 2TB are not supported yet\n");
  118. goto out;
  119. }
  120. error = -ENOMEM;
  121. sbi->s_vhdr = kmalloc(HFSPLUS_SECTOR_SIZE, GFP_KERNEL);
  122. if (!sbi->s_vhdr)
  123. goto out;
  124. sbi->s_backup_vhdr = kmalloc(HFSPLUS_SECTOR_SIZE, GFP_KERNEL);
  125. if (!sbi->s_backup_vhdr)
  126. goto out_free_vhdr;
  127. reread:
  128. error = hfsplus_submit_bio(sb->s_bdev,
  129. part_start + HFSPLUS_VOLHEAD_SECTOR,
  130. sbi->s_vhdr, READ);
  131. if (error)
  132. goto out_free_backup_vhdr;
  133. error = -EINVAL;
  134. switch (sbi->s_vhdr->signature) {
  135. case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
  136. set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
  137. /*FALLTHRU*/
  138. case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
  139. break;
  140. case cpu_to_be16(HFSP_WRAP_MAGIC):
  141. if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
  142. goto out;
  143. wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
  144. part_start += wd.ablk_start + wd.embed_start * wd.ablk_size;
  145. part_size = wd.embed_count * wd.ablk_size;
  146. goto reread;
  147. default:
  148. /*
  149. * Check for a partition block.
  150. *
  151. * (should do this only for cdrom/loop though)
  152. */
  153. if (hfs_part_find(sb, &part_start, &part_size))
  154. goto out;
  155. goto reread;
  156. }
  157. error = hfsplus_submit_bio(sb->s_bdev,
  158. part_start + part_size - 2,
  159. sbi->s_backup_vhdr, READ);
  160. if (error)
  161. goto out_free_backup_vhdr;
  162. error = -EINVAL;
  163. if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
  164. printk(KERN_WARNING
  165. "hfs: invalid secondary volume header\n");
  166. goto out_free_backup_vhdr;
  167. }
  168. blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
  169. /*
  170. * Block size must be at least as large as a sector and a multiple of 2.
  171. */
  172. if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
  173. goto out_free_backup_vhdr;
  174. sbi->alloc_blksz = blocksize;
  175. sbi->alloc_blksz_shift = 0;
  176. while ((blocksize >>= 1) != 0)
  177. sbi->alloc_blksz_shift++;
  178. blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE);
  179. /*
  180. * Align block size to block offset.
  181. */
  182. while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
  183. blocksize >>= 1;
  184. if (sb_set_blocksize(sb, blocksize) != blocksize) {
  185. printk(KERN_ERR "hfs: unable to set blocksize to %u!\n", blocksize);
  186. goto out_free_backup_vhdr;
  187. }
  188. sbi->blockoffset =
  189. part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
  190. sbi->part_start = part_start;
  191. sbi->sect_count = part_size;
  192. sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
  193. return 0;
  194. out_free_backup_vhdr:
  195. kfree(sbi->s_backup_vhdr);
  196. out_free_vhdr:
  197. kfree(sbi->s_vhdr);
  198. out:
  199. return error;
  200. }