blob: 51ae6f1eb4a55bff5430944870d90eca7aaca70a [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>
15#include <linux/genhd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/unaligned.h>
17
18#include "hfsplus_fs.h"
19#include "hfsplus_raw.h"
20
21struct hfsplus_wd {
22 u32 ablk_size;
23 u16 ablk_start;
24 u16 embed_start;
25 u16 embed_count;
26};
27
Fabian Frederick915ab232014-06-06 14:36:27 -070028/**
29 * hfsplus_submit_bio - Perform block I/O
Seth Forshee65965282011-07-18 08:06:23 -070030 * @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 Christie67ed2592016-06-05 14:31:58 -050034 * @op: direction of I/O
35 * @op_flags: request op flags
Seth Forshee65965282011-07-18 08:06:23 -070036 *
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 */
48int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
Mike Christie67ed2592016-06-05 14:31:58 -050049 void *buf, void **data, int op, int op_flags)
Christoph Hellwig52399b12010-11-23 14:37:47 +010050{
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
67 bio = bio_alloc(GFP_NOIO, 1);
Kent Overstreet4f024f32013-10-11 15:44:27 -070068 bio->bi_iter.bi_sector = sector;
Christoph Hellwig74d46992017-08-23 19:10:32 +020069 bio_set_dev(bio, sb->s_bdev);
Mike Christie67ed2592016-06-05 14:31:58 -050070 bio_set_op_attrs(bio, op, op_flags);
Christoph Hellwig52399b12010-11-23 14:37:47 +010071
Mike Christie67ed2592016-06-05 14:31:58 -050072 if (op != WRITE && data)
Seth Forshee65965282011-07-18 08:06:23 -070073 *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 Hellwig52399b12010-11-23 14:37:47 +010088
Mike Christie4e49ea42016-06-05 14:31:41 -050089 ret = submit_bio_wait(bio);
Seth Forshee65965282011-07-18 08:06:23 -070090out:
Seth Forshee50176dd2011-05-31 16:35:50 -050091 bio_put(bio);
Seth Forshee65965282011-07-18 08:06:23 -070092 return ret < 0 ? ret : 0;
Christoph Hellwig52399b12010-11-23 14:37:47 +010093}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
96{
97 u32 extent;
98 u16 attrib;
David Elliott2179d372006-01-18 17:43:08 -080099 __be16 sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
David Elliott2179d372006-01-18 17:43:08 -0800101 sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
102 if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
103 sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 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 Salikhmetov2753cc22010-12-16 18:08:38 +0200111 wd->ablk_size =
112 be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
114 return 0;
115 if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
116 return 0;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200117 wd->ablk_start =
118 be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Harvey Harrison8b3789e2008-04-29 01:03:44 -0700120 extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 wd->embed_start = (extent >> 16) & 0xFFFF;
122 wd->embed_count = extent & 0xFFFF;
123
124 return 1;
125}
126
127static int hfsplus_get_last_session(struct super_block *sb,
128 sector_t *start, sector_t *size)
129{
Christoph Hellwigf252fa32020-04-25 09:57:04 +0200130 struct cdrom_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 /* default values */
133 *start = 0;
Christoph Hellwig78ed9612021-10-18 12:11:16 +0200134 *size = bdev_nr_sectors(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200136 if (HFSPLUS_SB(sb)->session >= 0) {
Christoph Hellwigf252fa32020-04-25 09:57:04 +0200137 struct cdrom_tocentry te;
138
139 if (!cdi)
140 return -EINVAL;
141
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200142 te.cdte_track = HFSPLUS_SB(sb)->session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 te.cdte_format = CDROM_LBA;
Christoph Hellwigf252fa32020-04-25 09:57:04 +0200144 if (cdrom_read_tocentry(cdi, &te) ||
145 (te.cdte_ctrl & CDROM_DATA_TRACK) != 4) {
146 pr_err("invalid session number or type of track\n");
147 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
Christoph Hellwigf252fa32020-04-25 09:57:04 +0200149 *start = (sector_t)te.cdte_addr.lba << 2;
150 } else if (cdi) {
151 struct cdrom_multisession ms_info;
152
153 ms_info.addr_format = CDROM_LBA;
154 if (cdrom_multisession(cdi, &ms_info) == 0 && ms_info.xa_flag)
155 *start = (sector_t)ms_info.addr.lba << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
Christoph Hellwigf252fa32020-04-25 09:57:04 +0200157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return 0;
159}
160
161/* Find the volume header and fill in some minimum bits in superblock */
162/* Takes in super block, returns true if good data read */
163int hfsplus_read_wrapper(struct super_block *sb)
164{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200165 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct hfsplus_wd wd;
167 sector_t part_start, part_size;
168 u32 blocksize;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100169 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Christoph Hellwig52399b12010-11-23 14:37:47 +0100171 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
173 if (!blocksize)
Christoph Hellwig52399b12010-11-23 14:37:47 +0100174 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (hfsplus_get_last_session(sb, &part_start, &part_size))
Christoph Hellwig52399b12010-11-23 14:37:47 +0100177 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Christoph Hellwig52399b12010-11-23 14:37:47 +0100179 error = -ENOMEM;
Seth Forshee65965282011-07-18 08:06:23 -0700180 sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
181 if (!sbi->s_vhdr_buf)
Christoph Hellwig52399b12010-11-23 14:37:47 +0100182 goto out;
Seth Forshee65965282011-07-18 08:06:23 -0700183 sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
184 if (!sbi->s_backup_vhdr_buf)
Christoph Hellwig52399b12010-11-23 14:37:47 +0100185 goto out_free_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Christoph Hellwig52399b12010-11-23 14:37:47 +0100187reread:
Seth Forshee65965282011-07-18 08:06:23 -0700188 error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
189 sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
Mike Christie67ed2592016-06-05 14:31:58 -0500190 REQ_OP_READ, 0);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100191 if (error)
192 goto out_free_backup_vhdr;
193
194 error = -EINVAL;
195 switch (sbi->s_vhdr->signature) {
196 case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
197 set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500198 fallthrough;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100199 case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
200 break;
201 case cpu_to_be16(HFSP_WRAP_MAGIC):
202 if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
Chuck Ebberta1dbcef2011-02-02 10:55:06 -0500203 goto out_free_backup_vhdr;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100204 wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
Christoph Hellwig4ba2d5f2011-02-16 09:34:22 +0100205 part_start += (sector_t)wd.ablk_start +
206 (sector_t)wd.embed_start * wd.ablk_size;
207 part_size = (sector_t)wd.embed_count * wd.ablk_size;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100208 goto reread;
209 default:
210 /*
211 * Check for a partition block.
212 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 * (should do this only for cdrom/loop though)
214 */
215 if (hfs_part_find(sb, &part_start, &part_size))
Chuck Ebberta1dbcef2011-02-02 10:55:06 -0500216 goto out_free_backup_vhdr;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100217 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219
Seth Forshee65965282011-07-18 08:06:23 -0700220 error = hfsplus_submit_bio(sb, part_start + part_size - 2,
221 sbi->s_backup_vhdr_buf,
Mike Christie67ed2592016-06-05 14:31:58 -0500222 (void **)&sbi->s_backup_vhdr, REQ_OP_READ,
223 0);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100224 if (error)
225 goto out_free_backup_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Christoph Hellwig52399b12010-11-23 14:37:47 +0100227 error = -EINVAL;
228 if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
Joe Perchesd6142672013-04-30 15:27:55 -0700229 pr_warn("invalid secondary volume header\n");
Christoph Hellwig52399b12010-11-23 14:37:47 +0100230 goto out_free_backup_vhdr;
231 }
232
233 blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
234
235 /*
236 * Block size must be at least as large as a sector and a multiple of 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
Christoph Hellwig52399b12010-11-23 14:37:47 +0100238 if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
239 goto out_free_backup_vhdr;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200240 sbi->alloc_blksz = blocksize;
Fabian Frederick297cc272014-06-06 14:36:29 -0700241 sbi->alloc_blksz_shift = ilog2(blocksize);
Fabian Frederick915ab232014-06-06 14:36:27 -0700242 blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Christoph Hellwig52399b12010-11-23 14:37:47 +0100244 /*
245 * Align block size to block offset.
246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
248 blocksize >>= 1;
249
250 if (sb_set_blocksize(sb, blocksize) != blocksize) {
Joe Perchesd6142672013-04-30 15:27:55 -0700251 pr_err("unable to set blocksize to %u!\n", blocksize);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100252 goto out_free_backup_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200255 sbi->blockoffset =
256 part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100257 sbi->part_start = part_start;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200258 sbi->sect_count = part_size;
259 sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 0;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100261
262out_free_backup_vhdr:
Seth Forsheef588c962011-09-15 10:48:27 -0400263 kfree(sbi->s_backup_vhdr_buf);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100264out_free_vhdr:
Seth Forsheef588c962011-09-15 10:48:27 -0400265 kfree(sbi->s_vhdr_buf);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100266out:
267 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}