wrapper.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. u64 io_size = hfsplus_min_io_size(sb);
  48. loff_t start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT;
  49. int offset = start & (io_size - 1);
  50. if ((opf & REQ_OP_MASK) != REQ_OP_WRITE && data)
  51. *data = (u8 *)buf + offset;
  52. /*
  53. * Align sector to hardware sector size and find offset. We assume that
  54. * io_size is a power of two, which _should_ be true.
  55. */
  56. sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1);
  57. return bdev_rw_virt(sb->s_bdev, sector, buf, io_size, opf);
  58. }
  59. static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
  60. {
  61. u32 extent;
  62. u16 attrib;
  63. __be16 sig;
  64. sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
  65. if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
  66. sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
  67. return 0;
  68. attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
  69. if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
  70. !(attrib & HFSP_WRAP_ATTRIB_SPARED))
  71. return 0;
  72. wd->ablk_size =
  73. be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
  74. if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
  75. return 0;
  76. if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
  77. return 0;
  78. wd->ablk_start =
  79. be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
  80. extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
  81. wd->embed_start = (extent >> 16) & 0xFFFF;
  82. wd->embed_count = extent & 0xFFFF;
  83. return 1;
  84. }
  85. static int hfsplus_get_last_session(struct super_block *sb,
  86. sector_t *start, sector_t *size)
  87. {
  88. struct cdrom_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk);
  89. /* default values */
  90. *start = 0;
  91. *size = bdev_nr_sectors(sb->s_bdev);
  92. if (HFSPLUS_SB(sb)->session >= 0) {
  93. struct cdrom_tocentry te;
  94. if (!cdi)
  95. return -EINVAL;
  96. te.cdte_track = HFSPLUS_SB(sb)->session;
  97. te.cdte_format = CDROM_LBA;
  98. if (cdrom_read_tocentry(cdi, &te) ||
  99. (te.cdte_ctrl & CDROM_DATA_TRACK) != 4) {
  100. pr_err("invalid session number or type of track\n");
  101. return -EINVAL;
  102. }
  103. *start = (sector_t)te.cdte_addr.lba << 2;
  104. } else if (cdi) {
  105. struct cdrom_multisession ms_info;
  106. ms_info.addr_format = CDROM_LBA;
  107. if (cdrom_multisession(cdi, &ms_info) == 0 && ms_info.xa_flag)
  108. *start = (sector_t)ms_info.addr.lba << 2;
  109. }
  110. return 0;
  111. }
  112. /* Find the volume header and fill in some minimum bits in superblock */
  113. /* Takes in super block, returns true if good data read */
  114. int hfsplus_read_wrapper(struct super_block *sb)
  115. {
  116. struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
  117. struct hfsplus_wd wd;
  118. sector_t part_start, part_size;
  119. u32 blocksize;
  120. int error = 0;
  121. error = -EINVAL;
  122. blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
  123. if (!blocksize)
  124. goto out;
  125. sbi->min_io_size = blocksize;
  126. if (hfsplus_get_last_session(sb, &part_start, &part_size))
  127. goto out;
  128. error = -ENOMEM;
  129. sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
  130. if (!sbi->s_vhdr_buf)
  131. goto out;
  132. sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
  133. if (!sbi->s_backup_vhdr_buf)
  134. goto out_free_vhdr;
  135. reread:
  136. error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
  137. sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
  138. REQ_OP_READ);
  139. if (error)
  140. goto out_free_backup_vhdr;
  141. error = -EINVAL;
  142. switch (sbi->s_vhdr->signature) {
  143. case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
  144. set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
  145. fallthrough;
  146. case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
  147. break;
  148. case cpu_to_be16(HFSP_WRAP_MAGIC):
  149. if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
  150. goto out_free_backup_vhdr;
  151. wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
  152. part_start += (sector_t)wd.ablk_start +
  153. (sector_t)wd.embed_start * wd.ablk_size;
  154. part_size = (sector_t)wd.embed_count * wd.ablk_size;
  155. goto reread;
  156. default:
  157. /*
  158. * Check for a partition block.
  159. *
  160. * (should do this only for cdrom/loop though)
  161. */
  162. if (hfs_part_find(sb, &part_start, &part_size))
  163. goto out_free_backup_vhdr;
  164. goto reread;
  165. }
  166. error = hfsplus_submit_bio(sb, part_start + part_size - 2,
  167. sbi->s_backup_vhdr_buf,
  168. (void **)&sbi->s_backup_vhdr, REQ_OP_READ);
  169. if (error)
  170. goto out_free_backup_vhdr;
  171. error = -EINVAL;
  172. if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
  173. pr_warn("invalid secondary volume header\n");
  174. goto out_free_backup_vhdr;
  175. }
  176. blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
  177. /*
  178. * Block size must be at least as large as a sector and a multiple of 2.
  179. */
  180. if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
  181. goto out_free_backup_vhdr;
  182. sbi->alloc_blksz = blocksize;
  183. sbi->alloc_blksz_shift = ilog2(blocksize);
  184. blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE);
  185. /*
  186. * Align block size to block offset.
  187. */
  188. while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
  189. blocksize >>= 1;
  190. if (sb_set_blocksize(sb, blocksize) != blocksize) {
  191. pr_err("unable to set blocksize to %u!\n", blocksize);
  192. goto out_free_backup_vhdr;
  193. }
  194. sbi->blockoffset =
  195. part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
  196. sbi->part_start = part_start;
  197. sbi->sect_count = part_size;
  198. sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
  199. return 0;
  200. out_free_backup_vhdr:
  201. kfree(sbi->s_backup_vhdr_buf);
  202. out_free_vhdr:
  203. kfree(sbi->s_vhdr_buf);
  204. out:
  205. return error;
  206. }