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/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_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk);
  114. /* default values */
  115. *start = 0;
  116. *size = i_size_read(sb->s_bdev->bd_inode) >> 9;
  117. if (HFSPLUS_SB(sb)->session >= 0) {
  118. struct cdrom_tocentry te;
  119. if (!cdi)
  120. return -EINVAL;
  121. te.cdte_track = HFSPLUS_SB(sb)->session;
  122. te.cdte_format = CDROM_LBA;
  123. if (cdrom_read_tocentry(cdi, &te) ||
  124. (te.cdte_ctrl & CDROM_DATA_TRACK) != 4) {
  125. pr_err("invalid session number or type of track\n");
  126. return -EINVAL;
  127. }
  128. *start = (sector_t)te.cdte_addr.lba << 2;
  129. } else if (cdi) {
  130. struct cdrom_multisession ms_info;
  131. ms_info.addr_format = CDROM_LBA;
  132. if (cdrom_multisession(cdi, &ms_info) == 0 && ms_info.xa_flag)
  133. *start = (sector_t)ms_info.addr.lba << 2;
  134. }
  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. 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. 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. }