wrapper.c 7.0 KB

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