wrapper.c 6.8 KB

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