wrapper.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. /*
  30. * hfsplus_submit_bio - Perfrom block I/O
  31. * @sb: super block of volume for I/O
  32. * @sector: block to read or write, for blocks of HFSPLUS_SECTOR_SIZE bytes
  33. * @buf: buffer for I/O
  34. * @data: output pointer for location of requested data
  35. * @rw: direction of I/O
  36. *
  37. * The unit of I/O is hfsplus_min_io_size(sb), which may be bigger than
  38. * HFSPLUS_SECTOR_SIZE, and @buf must be sized accordingly. On reads
  39. * @data will return a pointer to the start of the requested sector,
  40. * which may not be the same location as @buf.
  41. *
  42. * If @sector is not aligned to the bdev logical block size it will
  43. * be rounded down. For writes this means that @buf should contain data
  44. * that starts at the rounded-down address. As long as the data was
  45. * read using hfsplus_submit_bio() and the same buffer is used things
  46. * will work correctly.
  47. */
  48. int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
  49. void *buf, void **data, int rw)
  50. {
  51. DECLARE_COMPLETION_ONSTACK(wait);
  52. struct bio *bio;
  53. int ret = 0;
  54. unsigned int io_size;
  55. loff_t start;
  56. int offset;
  57. /*
  58. * Align sector to hardware sector size and find offset. We
  59. * assume that io_size is a power of two, which _should_
  60. * be true.
  61. */
  62. io_size = hfsplus_min_io_size(sb);
  63. start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT;
  64. offset = start & (io_size - 1);
  65. sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1);
  66. bio = bio_alloc(GFP_NOIO, 1);
  67. bio->bi_sector = sector;
  68. bio->bi_bdev = sb->s_bdev;
  69. bio->bi_end_io = hfsplus_end_io_sync;
  70. bio->bi_private = &wait;
  71. if (!(rw & WRITE) && data)
  72. *data = (u8 *)buf + offset;
  73. while (io_size > 0) {
  74. unsigned int page_offset = offset_in_page(buf);
  75. unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset,
  76. io_size);
  77. ret = bio_add_page(bio, virt_to_page(buf), len, page_offset);
  78. if (ret != len) {
  79. ret = -EIO;
  80. goto out;
  81. }
  82. io_size -= len;
  83. buf = (u8 *)buf + len;
  84. }
  85. submit_bio(rw, bio);
  86. wait_for_completion(&wait);
  87. if (!bio_flagged(bio, BIO_UPTODATE))
  88. ret = -EIO;
  89. out:
  90. bio_put(bio);
  91. return ret < 0 ? ret : 0;
  92. }
  93. static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
  94. {
  95. u32 extent;
  96. u16 attrib;
  97. __be16 sig;
  98. sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
  99. if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
  100. sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
  101. return 0;
  102. attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
  103. if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
  104. !(attrib & HFSP_WRAP_ATTRIB_SPARED))
  105. return 0;
  106. wd->ablk_size =
  107. be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
  108. if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
  109. return 0;
  110. if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
  111. return 0;
  112. wd->ablk_start =
  113. be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
  114. extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
  115. wd->embed_start = (extent >> 16) & 0xFFFF;
  116. wd->embed_count = extent & 0xFFFF;
  117. return 1;
  118. }
  119. static int hfsplus_get_last_session(struct super_block *sb,
  120. sector_t *start, sector_t *size)
  121. {
  122. struct cdrom_multisession ms_info;
  123. struct cdrom_tocentry te;
  124. int res;
  125. /* default values */
  126. *start = 0;
  127. *size = sb->s_bdev->bd_inode->i_size >> 9;
  128. if (HFSPLUS_SB(sb)->session >= 0) {
  129. te.cdte_track = HFSPLUS_SB(sb)->session;
  130. te.cdte_format = CDROM_LBA;
  131. res = ioctl_by_bdev(sb->s_bdev,
  132. CDROMREADTOCENTRY, (unsigned long)&te);
  133. if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
  134. *start = (sector_t)te.cdte_addr.lba << 2;
  135. return 0;
  136. }
  137. printk(KERN_ERR "hfs: invalid session number or type of track\n");
  138. return -EINVAL;
  139. }
  140. ms_info.addr_format = CDROM_LBA;
  141. res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION,
  142. (unsigned long)&ms_info);
  143. if (!res && ms_info.xa_flag)
  144. *start = (sector_t)ms_info.addr.lba << 2;
  145. return 0;
  146. }
  147. /* Find the volume header and fill in some minimum bits in superblock */
  148. /* Takes in super block, returns true if good data read */
  149. int hfsplus_read_wrapper(struct super_block *sb)
  150. {
  151. struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
  152. struct hfsplus_wd wd;
  153. sector_t part_start, part_size;
  154. u32 blocksize;
  155. int error = 0;
  156. error = -EINVAL;
  157. blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
  158. if (!blocksize)
  159. goto out;
  160. if (hfsplus_get_last_session(sb, &part_start, &part_size))
  161. goto out;
  162. if ((u64)part_start + part_size > 0x100000000ULL) {
  163. pr_err("hfs: volumes larger than 2TB are not supported yet\n");
  164. goto out;
  165. }
  166. error = -ENOMEM;
  167. sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
  168. if (!sbi->s_vhdr_buf)
  169. goto out;
  170. sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
  171. if (!sbi->s_backup_vhdr_buf)
  172. goto out_free_vhdr;
  173. reread:
  174. error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
  175. sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
  176. READ);
  177. if (error)
  178. goto out_free_backup_vhdr;
  179. error = -EINVAL;
  180. switch (sbi->s_vhdr->signature) {
  181. case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
  182. set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
  183. /*FALLTHRU*/
  184. case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
  185. break;
  186. case cpu_to_be16(HFSP_WRAP_MAGIC):
  187. if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
  188. goto out_free_backup_vhdr;
  189. wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
  190. part_start += wd.ablk_start + wd.embed_start * wd.ablk_size;
  191. part_size = wd.embed_count * wd.ablk_size;
  192. goto reread;
  193. default:
  194. /*
  195. * Check for a partition block.
  196. *
  197. * (should do this only for cdrom/loop though)
  198. */
  199. if (hfs_part_find(sb, &part_start, &part_size))
  200. goto out_free_backup_vhdr;
  201. goto reread;
  202. }
  203. error = hfsplus_submit_bio(sb, part_start + part_size - 2,
  204. sbi->s_backup_vhdr_buf,
  205. (void **)&sbi->s_backup_vhdr, READ);
  206. if (error)
  207. goto out_free_backup_vhdr;
  208. error = -EINVAL;
  209. if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
  210. printk(KERN_WARNING
  211. "hfs: invalid secondary volume header\n");
  212. goto out_free_backup_vhdr;
  213. }
  214. blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
  215. /*
  216. * Block size must be at least as large as a sector and a multiple of 2.
  217. */
  218. if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
  219. goto out_free_backup_vhdr;
  220. sbi->alloc_blksz = blocksize;
  221. sbi->alloc_blksz_shift = 0;
  222. while ((blocksize >>= 1) != 0)
  223. sbi->alloc_blksz_shift++;
  224. blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE);
  225. /*
  226. * Align block size to block offset.
  227. */
  228. while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
  229. blocksize >>= 1;
  230. if (sb_set_blocksize(sb, blocksize) != blocksize) {
  231. printk(KERN_ERR "hfs: unable to set blocksize to %u!\n",
  232. blocksize);
  233. goto out_free_backup_vhdr;
  234. }
  235. sbi->blockoffset =
  236. part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
  237. sbi->part_start = part_start;
  238. sbi->sect_count = part_size;
  239. sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
  240. return 0;
  241. out_free_backup_vhdr:
  242. kfree(sbi->s_backup_vhdr_buf);
  243. out_free_vhdr:
  244. kfree(sbi->s_vhdr_buf);
  245. out:
  246. return error;
  247. }