blob: 0b791adf02e53d6498e5c36fc94b2402b77ad2a0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/unaligned.h>
16
17#include "hfsplus_fs.h"
18#include "hfsplus_raw.h"
19
20struct hfsplus_wd {
21 u32 ablk_size;
22 u16 ablk_start;
23 u16 embed_start;
24 u16 embed_count;
25};
26
Fabian Frederick915ab232014-06-06 14:36:27 -070027/**
28 * hfsplus_submit_bio - Perform block I/O
Seth Forshee65965282011-07-18 08:06:23 -070029 * @sb: super block of volume for I/O
30 * @sector: block to read or write, for blocks of HFSPLUS_SECTOR_SIZE bytes
31 * @buf: buffer for I/O
32 * @data: output pointer for location of requested data
Mike Christie67ed2592016-06-05 14:31:58 -050033 * @op: direction of I/O
34 * @op_flags: request op flags
Seth Forshee65965282011-07-18 08:06:23 -070035 *
36 * The unit of I/O is hfsplus_min_io_size(sb), which may be bigger than
37 * HFSPLUS_SECTOR_SIZE, and @buf must be sized accordingly. On reads
38 * @data will return a pointer to the start of the requested sector,
39 * which may not be the same location as @buf.
40 *
41 * If @sector is not aligned to the bdev logical block size it will
42 * be rounded down. For writes this means that @buf should contain data
43 * that starts at the rounded-down address. As long as the data was
44 * read using hfsplus_submit_bio() and the same buffer is used things
45 * will work correctly.
46 */
47int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
Bart Van Asschec85f9992022-07-14 11:07:20 -070048 void *buf, void **data, blk_opf_t opf)
Christoph Hellwig52399b12010-11-23 14:37:47 +010049{
Bart Van Asschec85f9992022-07-14 11:07:20 -070050 const enum req_op op = opf & REQ_OP_MASK;
Christoph Hellwig52399b12010-11-23 14:37:47 +010051 struct bio *bio;
Seth Forshee50176dd2011-05-31 16:35:50 -050052 int ret = 0;
Janne Kalliomäkia6dc8c02012-06-17 17:05:24 -040053 u64 io_size;
Seth Forshee65965282011-07-18 08:06:23 -070054 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 Hellwig52399b12010-11-23 14:37:47 +010066
Bart Van Asschec85f9992022-07-14 11:07:20 -070067 bio = bio_alloc(sb->s_bdev, 1, opf, GFP_NOIO);
Kent Overstreet4f024f32013-10-11 15:44:27 -070068 bio->bi_iter.bi_sector = sector;
Christoph Hellwig52399b12010-11-23 14:37:47 +010069
Bart Van Asschec85f9992022-07-14 11:07:20 -070070 if (op != REQ_OP_WRITE && data)
Seth Forshee65965282011-07-18 08:06:23 -070071 *data = (u8 *)buf + offset;
72
73 while (io_size > 0) {
74 unsigned int page_offset = offset_in_page(buf);
75 unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset,
76 io_size);
77
78 ret = bio_add_page(bio, virt_to_page(buf), len, page_offset);
79 if (ret != len) {
80 ret = -EIO;
81 goto out;
82 }
83 io_size -= len;
84 buf = (u8 *)buf + len;
85 }
Christoph Hellwig52399b12010-11-23 14:37:47 +010086
Mike Christie4e49ea42016-06-05 14:31:41 -050087 ret = submit_bio_wait(bio);
Seth Forshee65965282011-07-18 08:06:23 -070088out:
Seth Forshee50176dd2011-05-31 16:35:50 -050089 bio_put(bio);
Seth Forshee65965282011-07-18 08:06:23 -070090 return ret < 0 ? ret : 0;
Christoph Hellwig52399b12010-11-23 14:37:47 +010091}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
94{
95 u32 extent;
96 u16 attrib;
David Elliott2179d372006-01-18 17:43:08 -080097 __be16 sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
David Elliott2179d372006-01-18 17:43:08 -080099 sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
100 if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
101 sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 0;
103
104 attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
105 if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
106 !(attrib & HFSP_WRAP_ATTRIB_SPARED))
107 return 0;
108
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200109 wd->ablk_size =
110 be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
112 return 0;
113 if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
114 return 0;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200115 wd->ablk_start =
116 be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Harvey Harrison8b3789e2008-04-29 01:03:44 -0700118 extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 wd->embed_start = (extent >> 16) & 0xFFFF;
120 wd->embed_count = extent & 0xFFFF;
121
122 return 1;
123}
124
125static int hfsplus_get_last_session(struct super_block *sb,
126 sector_t *start, sector_t *size)
127{
Christoph Hellwigf252fa32020-04-25 09:57:04 +0200128 struct cdrom_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 /* default values */
131 *start = 0;
Christoph Hellwig78ed9612021-10-18 12:11:16 +0200132 *size = bdev_nr_sectors(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200134 if (HFSPLUS_SB(sb)->session >= 0) {
Christoph Hellwigf252fa32020-04-25 09:57:04 +0200135 struct cdrom_tocentry te;
136
137 if (!cdi)
138 return -EINVAL;
139
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200140 te.cdte_track = HFSPLUS_SB(sb)->session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 te.cdte_format = CDROM_LBA;
Christoph Hellwigf252fa32020-04-25 09:57:04 +0200142 if (cdrom_read_tocentry(cdi, &te) ||
143 (te.cdte_ctrl & CDROM_DATA_TRACK) != 4) {
144 pr_err("invalid session number or type of track\n");
145 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
Christoph Hellwigf252fa32020-04-25 09:57:04 +0200147 *start = (sector_t)te.cdte_addr.lba << 2;
148 } else if (cdi) {
149 struct cdrom_multisession ms_info;
150
151 ms_info.addr_format = CDROM_LBA;
152 if (cdrom_multisession(cdi, &ms_info) == 0 && ms_info.xa_flag)
153 *start = (sector_t)ms_info.addr.lba << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
Christoph Hellwigf252fa32020-04-25 09:57:04 +0200155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return 0;
157}
158
159/* Find the volume header and fill in some minimum bits in superblock */
160/* Takes in super block, returns true if good data read */
161int hfsplus_read_wrapper(struct super_block *sb)
162{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200163 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct hfsplus_wd wd;
165 sector_t part_start, part_size;
166 u32 blocksize;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100167 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Christoph Hellwig52399b12010-11-23 14:37:47 +0100169 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
171 if (!blocksize)
Christoph Hellwig52399b12010-11-23 14:37:47 +0100172 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if (hfsplus_get_last_session(sb, &part_start, &part_size))
Christoph Hellwig52399b12010-11-23 14:37:47 +0100175 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Christoph Hellwig52399b12010-11-23 14:37:47 +0100177 error = -ENOMEM;
Seth Forshee65965282011-07-18 08:06:23 -0700178 sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
179 if (!sbi->s_vhdr_buf)
Christoph Hellwig52399b12010-11-23 14:37:47 +0100180 goto out;
Seth Forshee65965282011-07-18 08:06:23 -0700181 sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
182 if (!sbi->s_backup_vhdr_buf)
Christoph Hellwig52399b12010-11-23 14:37:47 +0100183 goto out_free_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Christoph Hellwig52399b12010-11-23 14:37:47 +0100185reread:
Seth Forshee65965282011-07-18 08:06:23 -0700186 error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
187 sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
Bart Van Asschec85f9992022-07-14 11:07:20 -0700188 REQ_OP_READ);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100189 if (error)
190 goto out_free_backup_vhdr;
191
192 error = -EINVAL;
193 switch (sbi->s_vhdr->signature) {
194 case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
195 set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500196 fallthrough;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100197 case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
198 break;
199 case cpu_to_be16(HFSP_WRAP_MAGIC):
200 if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
Chuck Ebberta1dbcef2011-02-02 10:55:06 -0500201 goto out_free_backup_vhdr;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100202 wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
Christoph Hellwig4ba2d5f2011-02-16 09:34:22 +0100203 part_start += (sector_t)wd.ablk_start +
204 (sector_t)wd.embed_start * wd.ablk_size;
205 part_size = (sector_t)wd.embed_count * wd.ablk_size;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100206 goto reread;
207 default:
208 /*
209 * Check for a partition block.
210 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * (should do this only for cdrom/loop though)
212 */
213 if (hfs_part_find(sb, &part_start, &part_size))
Chuck Ebberta1dbcef2011-02-02 10:55:06 -0500214 goto out_free_backup_vhdr;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100215 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
Seth Forshee65965282011-07-18 08:06:23 -0700218 error = hfsplus_submit_bio(sb, part_start + part_size - 2,
219 sbi->s_backup_vhdr_buf,
Bart Van Asschec85f9992022-07-14 11:07:20 -0700220 (void **)&sbi->s_backup_vhdr, REQ_OP_READ);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100221 if (error)
222 goto out_free_backup_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Christoph Hellwig52399b12010-11-23 14:37:47 +0100224 error = -EINVAL;
225 if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
Joe Perchesd61426732013-04-30 15:27:55 -0700226 pr_warn("invalid secondary volume header\n");
Christoph Hellwig52399b12010-11-23 14:37:47 +0100227 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 Torvalds1da177e2005-04-16 15:20:36 -0700234 */
Christoph Hellwig52399b12010-11-23 14:37:47 +0100235 if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
236 goto out_free_backup_vhdr;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200237 sbi->alloc_blksz = blocksize;
Fabian Frederick297cc272014-06-06 14:36:29 -0700238 sbi->alloc_blksz_shift = ilog2(blocksize);
Fabian Frederick915ab232014-06-06 14:36:27 -0700239 blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Christoph Hellwig52399b12010-11-23 14:37:47 +0100241 /*
242 * Align block size to block offset.
243 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
245 blocksize >>= 1;
246
247 if (sb_set_blocksize(sb, blocksize) != blocksize) {
Joe Perchesd61426732013-04-30 15:27:55 -0700248 pr_err("unable to set blocksize to %u!\n", blocksize);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100249 goto out_free_backup_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200252 sbi->blockoffset =
253 part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100254 sbi->part_start = part_start;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200255 sbi->sect_count = part_size;
256 sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return 0;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100258
259out_free_backup_vhdr:
Seth Forsheef588c962011-09-15 10:48:27 -0400260 kfree(sbi->s_backup_vhdr_buf);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100261out_free_vhdr:
Seth Forsheef588c962011-09-15 10:48:27 -0400262 kfree(sbi->s_vhdr_buf);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100263out:
264 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}