wrapper.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. u64 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. error = -ENOMEM;
  163. sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
  164. if (!sbi->s_vhdr_buf)
  165. goto out;
  166. sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
  167. if (!sbi->s_backup_vhdr_buf)
  168. goto out_free_vhdr;
  169. reread:
  170. error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
  171. sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
  172. READ);
  173. if (error)
  174. goto out_free_backup_vhdr;
  175. error = -EINVAL;
  176. switch (sbi->s_vhdr->signature) {
  177. case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
  178. set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
  179. /*FALLTHRU*/
  180. case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
  181. break;
  182. case cpu_to_be16(HFSP_WRAP_MAGIC):
  183. if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
  184. goto out_free_backup_vhdr;
  185. wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
  186. part_start += (sector_t)wd.ablk_start +
  187. (sector_t)wd.embed_start * wd.ablk_size;
  188. part_size = (sector_t)wd.embed_count * wd.ablk_size;
  189. goto reread;
  190. default:
  191. /*
  192. * Check for a partition block.
  193. *
  194. * (should do this only for cdrom/loop though)
  195. */
  196. if (hfs_part_find(sb, &part_start, &part_size))
  197. goto out_free_backup_vhdr;
  198. goto reread;
  199. }
  200. error = hfsplus_submit_bio(sb, part_start + part_size - 2,
  201. sbi->s_backup_vhdr_buf,
  202. (void **)&sbi->s_backup_vhdr, READ);
  203. if (error)
  204. goto out_free_backup_vhdr;
  205. error = -EINVAL;
  206. if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
  207. printk(KERN_WARNING
  208. "hfs: invalid secondary volume header\n");
  209. goto out_free_backup_vhdr;
  210. }
  211. blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
  212. /*
  213. * Block size must be at least as large as a sector and a multiple of 2.
  214. */
  215. if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
  216. goto out_free_backup_vhdr;
  217. sbi->alloc_blksz = blocksize;
  218. sbi->alloc_blksz_shift = 0;
  219. while ((blocksize >>= 1) != 0)
  220. sbi->alloc_blksz_shift++;
  221. blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE);
  222. /*
  223. * Align block size to block offset.
  224. */
  225. while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
  226. blocksize >>= 1;
  227. if (sb_set_blocksize(sb, blocksize) != blocksize) {
  228. printk(KERN_ERR "hfs: unable to set blocksize to %u!\n",
  229. blocksize);
  230. goto out_free_backup_vhdr;
  231. }
  232. sbi->blockoffset =
  233. part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
  234. sbi->part_start = part_start;
  235. sbi->sect_count = part_size;
  236. sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
  237. return 0;
  238. out_free_backup_vhdr:
  239. kfree(sbi->s_backup_vhdr_buf);
  240. out_free_vhdr:
  241. kfree(sbi->s_vhdr_buf);
  242. out:
  243. return error;
  244. }