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