wrapper.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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/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. * @opf: I/O operation type and flags
  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. * Returns: %0 on success else -errno code
  43. */
  44. int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
  45. void *buf, void **data, blk_opf_t opf)
  46. {
  47. const enum req_op op = opf & REQ_OP_MASK;
  48. struct bio *bio;
  49. int ret = 0;
  50. u64 io_size;
  51. loff_t start;
  52. int offset;
  53. /*
  54. * Align sector to hardware sector size and find offset. We
  55. * assume that io_size is a power of two, which _should_
  56. * be true.
  57. */
  58. io_size = hfsplus_min_io_size(sb);
  59. start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT;
  60. offset = start & (io_size - 1);
  61. sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1);
  62. bio = bio_alloc(sb->s_bdev, 1, opf, GFP_NOIO);
  63. bio->bi_iter.bi_sector = sector;
  64. if (op != REQ_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_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk);
  113. /* default values */
  114. *start = 0;
  115. *size = bdev_nr_sectors(sb->s_bdev);
  116. if (HFSPLUS_SB(sb)->session >= 0) {
  117. struct cdrom_tocentry te;
  118. if (!cdi)
  119. return -EINVAL;
  120. te.cdte_track = HFSPLUS_SB(sb)->session;
  121. te.cdte_format = CDROM_LBA;
  122. if (cdrom_read_tocentry(cdi, &te) ||
  123. (te.cdte_ctrl & CDROM_DATA_TRACK) != 4) {
  124. pr_err("invalid session number or type of track\n");
  125. return -EINVAL;
  126. }
  127. *start = (sector_t)te.cdte_addr.lba << 2;
  128. } else if (cdi) {
  129. struct cdrom_multisession ms_info;
  130. ms_info.addr_format = CDROM_LBA;
  131. if (cdrom_multisession(cdi, &ms_info) == 0 && ms_info.xa_flag)
  132. *start = (sector_t)ms_info.addr.lba << 2;
  133. }
  134. return 0;
  135. }
  136. /* Find the volume header and fill in some minimum bits in superblock */
  137. /* Takes in super block, returns true if good data read */
  138. int hfsplus_read_wrapper(struct super_block *sb)
  139. {
  140. struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
  141. struct hfsplus_wd wd;
  142. sector_t part_start, part_size;
  143. u32 blocksize;
  144. int error = 0;
  145. error = -EINVAL;
  146. blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
  147. if (!blocksize)
  148. goto out;
  149. sbi->min_io_size = blocksize;
  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);
  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. fallthrough;
  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. if (error)
  194. goto out_free_backup_vhdr;
  195. error = -EINVAL;
  196. if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
  197. pr_warn("invalid secondary volume header\n");
  198. goto out_free_backup_vhdr;
  199. }
  200. blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
  201. /*
  202. * Block size must be at least as large as a sector and a multiple of 2.
  203. */
  204. if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
  205. goto out_free_backup_vhdr;
  206. sbi->alloc_blksz = blocksize;
  207. sbi->alloc_blksz_shift = ilog2(blocksize);
  208. blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE);
  209. /*
  210. * Align block size to block offset.
  211. */
  212. while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
  213. blocksize >>= 1;
  214. if (sb_set_blocksize(sb, blocksize) != blocksize) {
  215. pr_err("unable to set blocksize to %u!\n", blocksize);
  216. goto out_free_backup_vhdr;
  217. }
  218. sbi->blockoffset =
  219. part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
  220. sbi->part_start = part_start;
  221. sbi->sect_count = part_size;
  222. sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
  223. return 0;
  224. out_free_backup_vhdr:
  225. kfree(sbi->s_backup_vhdr_buf);
  226. out_free_vhdr:
  227. kfree(sbi->s_vhdr_buf);
  228. out:
  229. return error;
  230. }