Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/blkdev.h> |
| 14 | #include <linux/cdrom.h> |
| 15 | #include <linux/genhd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/unaligned.h> |
| 17 | |
| 18 | #include "hfsplus_fs.h" |
| 19 | #include "hfsplus_raw.h" |
| 20 | |
| 21 | struct hfsplus_wd { |
| 22 | u32 ablk_size; |
| 23 | u16 ablk_start; |
| 24 | u16 embed_start; |
| 25 | u16 embed_count; |
| 26 | }; |
| 27 | |
Fabian Frederick | 915ab23 | 2014-06-06 14:36:27 -0700 | [diff] [blame] | 28 | /** |
| 29 | * hfsplus_submit_bio - Perform block I/O |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 30 | * @sb: super block of volume for I/O |
| 31 | * @sector: block to read or write, for blocks of HFSPLUS_SECTOR_SIZE bytes |
| 32 | * @buf: buffer for I/O |
| 33 | * @data: output pointer for location of requested data |
Mike Christie | 67ed259 | 2016-06-05 14:31:58 -0500 | [diff] [blame] | 34 | * @op: direction of I/O |
| 35 | * @op_flags: request op flags |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 36 | * |
| 37 | * The unit of I/O is hfsplus_min_io_size(sb), which may be bigger than |
| 38 | * HFSPLUS_SECTOR_SIZE, and @buf must be sized accordingly. On reads |
| 39 | * @data will return a pointer to the start of the requested sector, |
| 40 | * which may not be the same location as @buf. |
| 41 | * |
| 42 | * If @sector is not aligned to the bdev logical block size it will |
| 43 | * be rounded down. For writes this means that @buf should contain data |
| 44 | * that starts at the rounded-down address. As long as the data was |
| 45 | * read using hfsplus_submit_bio() and the same buffer is used things |
| 46 | * will work correctly. |
| 47 | */ |
| 48 | int hfsplus_submit_bio(struct super_block *sb, sector_t sector, |
Mike Christie | 67ed259 | 2016-06-05 14:31:58 -0500 | [diff] [blame] | 49 | void *buf, void **data, int op, int op_flags) |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 50 | { |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 51 | struct bio *bio; |
Seth Forshee | 50176dd | 2011-05-31 16:35:50 -0500 | [diff] [blame] | 52 | int ret = 0; |
Janne Kalliomäki | a6dc8c0 | 2012-06-17 17:05:24 -0400 | [diff] [blame] | 53 | u64 io_size; |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 54 | loff_t start; |
| 55 | int offset; |
| 56 | |
| 57 | /* |
| 58 | * Align sector to hardware sector size and find offset. We |
| 59 | * assume that io_size is a power of two, which _should_ |
| 60 | * be true. |
| 61 | */ |
| 62 | io_size = hfsplus_min_io_size(sb); |
| 63 | start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT; |
| 64 | offset = start & (io_size - 1); |
| 65 | sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 66 | |
| 67 | bio = bio_alloc(GFP_NOIO, 1); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 68 | bio->bi_iter.bi_sector = sector; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 69 | bio_set_dev(bio, sb->s_bdev); |
Mike Christie | 67ed259 | 2016-06-05 14:31:58 -0500 | [diff] [blame] | 70 | bio_set_op_attrs(bio, op, op_flags); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 71 | |
Mike Christie | 67ed259 | 2016-06-05 14:31:58 -0500 | [diff] [blame] | 72 | if (op != WRITE && data) |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 73 | *data = (u8 *)buf + offset; |
| 74 | |
| 75 | while (io_size > 0) { |
| 76 | unsigned int page_offset = offset_in_page(buf); |
| 77 | unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset, |
| 78 | io_size); |
| 79 | |
| 80 | ret = bio_add_page(bio, virt_to_page(buf), len, page_offset); |
| 81 | if (ret != len) { |
| 82 | ret = -EIO; |
| 83 | goto out; |
| 84 | } |
| 85 | io_size -= len; |
| 86 | buf = (u8 *)buf + len; |
| 87 | } |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 88 | |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 89 | ret = submit_bio_wait(bio); |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 90 | out: |
Seth Forshee | 50176dd | 2011-05-31 16:35:50 -0500 | [diff] [blame] | 91 | bio_put(bio); |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 92 | return ret < 0 ? ret : 0; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd) |
| 96 | { |
| 97 | u32 extent; |
| 98 | u16 attrib; |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 99 | __be16 sig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 101 | sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG); |
| 102 | if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) && |
| 103 | sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | return 0; |
| 105 | |
| 106 | attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB)); |
| 107 | if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) || |
| 108 | !(attrib & HFSP_WRAP_ATTRIB_SPARED)) |
| 109 | return 0; |
| 110 | |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 111 | wd->ablk_size = |
| 112 | be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | if (wd->ablk_size < HFSPLUS_SECTOR_SIZE) |
| 114 | return 0; |
| 115 | if (wd->ablk_size % HFSPLUS_SECTOR_SIZE) |
| 116 | return 0; |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 117 | wd->ablk_start = |
| 118 | be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Harvey Harrison | 8b3789e | 2008-04-29 01:03:44 -0700 | [diff] [blame] | 120 | extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | wd->embed_start = (extent >> 16) & 0xFFFF; |
| 122 | wd->embed_count = extent & 0xFFFF; |
| 123 | |
| 124 | return 1; |
| 125 | } |
| 126 | |
| 127 | static int hfsplus_get_last_session(struct super_block *sb, |
| 128 | sector_t *start, sector_t *size) |
| 129 | { |
| 130 | struct cdrom_multisession ms_info; |
| 131 | struct cdrom_tocentry te; |
| 132 | int res; |
| 133 | |
| 134 | /* default values */ |
| 135 | *start = 0; |
Fabian Frederick | a3f2235 | 2017-02-27 14:27:19 -0800 | [diff] [blame] | 136 | *size = i_size_read(sb->s_bdev->bd_inode) >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 138 | if (HFSPLUS_SB(sb)->session >= 0) { |
| 139 | te.cdte_track = HFSPLUS_SB(sb)->session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | te.cdte_format = CDROM_LBA; |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 141 | res = ioctl_by_bdev(sb->s_bdev, |
| 142 | CDROMREADTOCENTRY, (unsigned long)&te); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) { |
| 144 | *start = (sector_t)te.cdte_addr.lba << 2; |
| 145 | return 0; |
| 146 | } |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 147 | pr_err("invalid session number or type of track\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return -EINVAL; |
| 149 | } |
| 150 | ms_info.addr_format = CDROM_LBA; |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 151 | res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION, |
| 152 | (unsigned long)&ms_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | if (!res && ms_info.xa_flag) |
| 154 | *start = (sector_t)ms_info.addr.lba << 2; |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /* Find the volume header and fill in some minimum bits in superblock */ |
| 159 | /* Takes in super block, returns true if good data read */ |
| 160 | int hfsplus_read_wrapper(struct super_block *sb) |
| 161 | { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 162 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | struct hfsplus_wd wd; |
| 164 | sector_t part_start, part_size; |
| 165 | u32 blocksize; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 166 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 168 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE); |
| 170 | if (!blocksize) |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 171 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | if (hfsplus_get_last_session(sb, &part_start, &part_size)) |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 174 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 176 | error = -ENOMEM; |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 177 | sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL); |
| 178 | if (!sbi->s_vhdr_buf) |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 179 | goto out; |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 180 | sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL); |
| 181 | if (!sbi->s_backup_vhdr_buf) |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 182 | goto out_free_vhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 184 | reread: |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 185 | error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, |
| 186 | sbi->s_vhdr_buf, (void **)&sbi->s_vhdr, |
Mike Christie | 67ed259 | 2016-06-05 14:31:58 -0500 | [diff] [blame] | 187 | REQ_OP_READ, 0); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 188 | if (error) |
| 189 | goto out_free_backup_vhdr; |
| 190 | |
| 191 | error = -EINVAL; |
| 192 | switch (sbi->s_vhdr->signature) { |
| 193 | case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX): |
| 194 | set_bit(HFSPLUS_SB_HFSX, &sbi->flags); |
| 195 | /*FALLTHRU*/ |
| 196 | case cpu_to_be16(HFSPLUS_VOLHEAD_SIG): |
| 197 | break; |
| 198 | case cpu_to_be16(HFSP_WRAP_MAGIC): |
| 199 | if (!hfsplus_read_mdb(sbi->s_vhdr, &wd)) |
Chuck Ebbert | a1dbcef | 2011-02-02 10:55:06 -0500 | [diff] [blame] | 200 | goto out_free_backup_vhdr; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 201 | wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT; |
Christoph Hellwig | 4ba2d5f | 2011-02-16 09:34:22 +0100 | [diff] [blame] | 202 | part_start += (sector_t)wd.ablk_start + |
| 203 | (sector_t)wd.embed_start * wd.ablk_size; |
| 204 | part_size = (sector_t)wd.embed_count * wd.ablk_size; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 205 | goto reread; |
| 206 | default: |
| 207 | /* |
| 208 | * Check for a partition block. |
| 209 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | * (should do this only for cdrom/loop though) |
| 211 | */ |
| 212 | if (hfs_part_find(sb, &part_start, &part_size)) |
Chuck Ebbert | a1dbcef | 2011-02-02 10:55:06 -0500 | [diff] [blame] | 213 | goto out_free_backup_vhdr; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 214 | goto reread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 217 | error = hfsplus_submit_bio(sb, part_start + part_size - 2, |
| 218 | sbi->s_backup_vhdr_buf, |
Mike Christie | 67ed259 | 2016-06-05 14:31:58 -0500 | [diff] [blame] | 219 | (void **)&sbi->s_backup_vhdr, REQ_OP_READ, |
| 220 | 0); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 221 | if (error) |
| 222 | goto out_free_backup_vhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 224 | error = -EINVAL; |
| 225 | if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 226 | pr_warn("invalid secondary volume header\n"); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 227 | goto out_free_backup_vhdr; |
| 228 | } |
| 229 | |
| 230 | blocksize = be32_to_cpu(sbi->s_vhdr->blocksize); |
| 231 | |
| 232 | /* |
| 233 | * Block size must be at least as large as a sector and a multiple of 2. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | */ |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 235 | if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize)) |
| 236 | goto out_free_backup_vhdr; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 237 | sbi->alloc_blksz = blocksize; |
Fabian Frederick | 297cc27 | 2014-06-06 14:36:29 -0700 | [diff] [blame] | 238 | sbi->alloc_blksz_shift = ilog2(blocksize); |
Fabian Frederick | 915ab23 | 2014-06-06 14:36:27 -0700 | [diff] [blame] | 239 | blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 241 | /* |
| 242 | * Align block size to block offset. |
| 243 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1)) |
| 245 | blocksize >>= 1; |
| 246 | |
| 247 | if (sb_set_blocksize(sb, blocksize) != blocksize) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 248 | pr_err("unable to set blocksize to %u!\n", blocksize); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 249 | goto out_free_backup_vhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 252 | sbi->blockoffset = |
| 253 | part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 254 | sbi->part_start = part_start; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 255 | sbi->sect_count = part_size; |
| 256 | sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | return 0; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 258 | |
| 259 | out_free_backup_vhdr: |
Seth Forshee | f588c96 | 2011-09-15 10:48:27 -0400 | [diff] [blame] | 260 | kfree(sbi->s_backup_vhdr_buf); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 261 | out_free_vhdr: |
Seth Forshee | f588c96 | 2011-09-15 10:48:27 -0400 | [diff] [blame] | 262 | kfree(sbi->s_vhdr_buf); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 263 | out: |
| 264 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |