wrapper.c 6.9 KB

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