Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
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> | |
1da177e4 LT |
15 | #include <asm/unaligned.h> |
16 | ||
17 | #include "hfsplus_fs.h" | |
18 | #include "hfsplus_raw.h" | |
19 | ||
20 | struct hfsplus_wd { | |
21 | u32 ablk_size; | |
22 | u16 ablk_start; | |
23 | u16 embed_start; | |
24 | u16 embed_count; | |
25 | }; | |
26 | ||
915ab236 FF |
27 | /** |
28 | * hfsplus_submit_bio - Perform block I/O | |
6596528e SF |
29 | * @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 | |
67ed2596 MC |
33 | * @op: direction of I/O |
34 | * @op_flags: request op flags | |
6596528e SF |
35 | * |
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 | */ | |
47 | int hfsplus_submit_bio(struct super_block *sb, sector_t sector, | |
c85f9992 | 48 | void *buf, void **data, blk_opf_t opf) |
52399b17 | 49 | { |
c85f9992 | 50 | const enum req_op op = opf & REQ_OP_MASK; |
52399b17 | 51 | struct bio *bio; |
50176dde | 52 | int ret = 0; |
a6dc8c04 | 53 | u64 io_size; |
6596528e SF |
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); | |
52399b17 | 66 | |
c85f9992 | 67 | bio = bio_alloc(sb->s_bdev, 1, opf, GFP_NOIO); |
4f024f37 | 68 | bio->bi_iter.bi_sector = sector; |
52399b17 | 69 | |
c85f9992 | 70 | if (op != REQ_OP_WRITE && data) |
6596528e SF |
71 | *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 | } | |
52399b17 | 86 | |
4e49ea4a | 87 | ret = submit_bio_wait(bio); |
6596528e | 88 | out: |
50176dde | 89 | bio_put(bio); |
6596528e | 90 | return ret < 0 ? ret : 0; |
52399b17 CH |
91 | } |
92 | ||
1da177e4 LT |
93 | static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd) |
94 | { | |
95 | u32 extent; | |
96 | u16 attrib; | |
2179d372 | 97 | __be16 sig; |
1da177e4 | 98 | |
2179d372 DE |
99 | sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG); |
100 | if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) && | |
101 | sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX)) | |
1da177e4 LT |
102 | 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 | ||
2753cc28 AS |
109 | wd->ablk_size = |
110 | be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE)); | |
1da177e4 LT |
111 | if (wd->ablk_size < HFSPLUS_SECTOR_SIZE) |
112 | return 0; | |
113 | if (wd->ablk_size % HFSPLUS_SECTOR_SIZE) | |
114 | return 0; | |
2753cc28 AS |
115 | wd->ablk_start = |
116 | be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART)); | |
1da177e4 | 117 | |
8b3789e5 | 118 | extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT); |
1da177e4 LT |
119 | wd->embed_start = (extent >> 16) & 0xFFFF; |
120 | wd->embed_count = extent & 0xFFFF; | |
121 | ||
122 | return 1; | |
123 | } | |
124 | ||
125 | static int hfsplus_get_last_session(struct super_block *sb, | |
126 | sector_t *start, sector_t *size) | |
127 | { | |
f252fa33 | 128 | struct cdrom_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk); |
1da177e4 LT |
129 | |
130 | /* default values */ | |
131 | *start = 0; | |
78ed961b | 132 | *size = bdev_nr_sectors(sb->s_bdev); |
1da177e4 | 133 | |
dd73a01a | 134 | if (HFSPLUS_SB(sb)->session >= 0) { |
f252fa33 CH |
135 | struct cdrom_tocentry te; |
136 | ||
137 | if (!cdi) | |
138 | return -EINVAL; | |
139 | ||
dd73a01a | 140 | te.cdte_track = HFSPLUS_SB(sb)->session; |
1da177e4 | 141 | te.cdte_format = CDROM_LBA; |
f252fa33 CH |
142 | 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; | |
1da177e4 | 146 | } |
f252fa33 CH |
147 | *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; | |
1da177e4 | 154 | } |
f252fa33 | 155 | |
1da177e4 LT |
156 | 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 */ | |
161 | int hfsplus_read_wrapper(struct super_block *sb) | |
162 | { | |
dd73a01a | 163 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb); |
1da177e4 LT |
164 | struct hfsplus_wd wd; |
165 | sector_t part_start, part_size; | |
166 | u32 blocksize; | |
52399b17 | 167 | int error = 0; |
1da177e4 | 168 | |
52399b17 | 169 | error = -EINVAL; |
1da177e4 LT |
170 | blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE); |
171 | if (!blocksize) | |
52399b17 | 172 | goto out; |
1da177e4 LT |
173 | |
174 | if (hfsplus_get_last_session(sb, &part_start, &part_size)) | |
52399b17 | 175 | goto out; |
1da177e4 | 176 | |
52399b17 | 177 | error = -ENOMEM; |
6596528e SF |
178 | sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL); |
179 | if (!sbi->s_vhdr_buf) | |
52399b17 | 180 | goto out; |
6596528e SF |
181 | sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL); |
182 | if (!sbi->s_backup_vhdr_buf) | |
52399b17 CH |
183 | goto out_free_vhdr; |
184 | ||
185 | reread: | |
6596528e SF |
186 | error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, |
187 | sbi->s_vhdr_buf, (void **)&sbi->s_vhdr, | |
c85f9992 | 188 | REQ_OP_READ); |
52399b17 CH |
189 | 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); | |
df561f66 | 196 | fallthrough; |
52399b17 CH |
197 | 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)) | |
a1dbcef0 | 201 | goto out_free_backup_vhdr; |
52399b17 | 202 | wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT; |
4ba2d5fd CH |
203 | 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; | |
52399b17 CH |
206 | goto reread; |
207 | default: | |
208 | /* | |
209 | * Check for a partition block. | |
210 | * | |
1da177e4 LT |
211 | * (should do this only for cdrom/loop though) |
212 | */ | |
213 | if (hfs_part_find(sb, &part_start, &part_size)) | |
a1dbcef0 | 214 | goto out_free_backup_vhdr; |
52399b17 CH |
215 | goto reread; |
216 | } | |
217 | ||
6596528e SF |
218 | error = hfsplus_submit_bio(sb, part_start + part_size - 2, |
219 | sbi->s_backup_vhdr_buf, | |
c85f9992 | 220 | (void **)&sbi->s_backup_vhdr, REQ_OP_READ); |
52399b17 CH |
221 | if (error) |
222 | goto out_free_backup_vhdr; | |
223 | ||
224 | error = -EINVAL; | |
225 | if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) { | |
d6142673 | 226 | pr_warn("invalid secondary volume header\n"); |
52399b17 | 227 | goto out_free_backup_vhdr; |
1da177e4 LT |
228 | } |
229 | ||
52399b17 | 230 | blocksize = be32_to_cpu(sbi->s_vhdr->blocksize); |
1da177e4 | 231 | |
52399b17 CH |
232 | /* |
233 | * Block size must be at least as large as a sector and a multiple of 2. | |
1da177e4 | 234 | */ |
52399b17 CH |
235 | if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize)) |
236 | goto out_free_backup_vhdr; | |
dd73a01a | 237 | sbi->alloc_blksz = blocksize; |
297cc272 | 238 | sbi->alloc_blksz_shift = ilog2(blocksize); |
915ab236 | 239 | blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE); |
1da177e4 | 240 | |
52399b17 CH |
241 | /* |
242 | * Align block size to block offset. | |
243 | */ | |
1da177e4 LT |
244 | while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1)) |
245 | blocksize >>= 1; | |
246 | ||
247 | if (sb_set_blocksize(sb, blocksize) != blocksize) { | |
d6142673 | 248 | pr_err("unable to set blocksize to %u!\n", blocksize); |
52399b17 | 249 | goto out_free_backup_vhdr; |
1da177e4 LT |
250 | } |
251 | ||
dd73a01a CH |
252 | sbi->blockoffset = |
253 | part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT); | |
52399b17 | 254 | sbi->part_start = part_start; |
dd73a01a CH |
255 | sbi->sect_count = part_size; |
256 | sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits; | |
1da177e4 | 257 | return 0; |
52399b17 CH |
258 | |
259 | out_free_backup_vhdr: | |
f588c960 | 260 | kfree(sbi->s_backup_vhdr_buf); |
52399b17 | 261 | out_free_vhdr: |
f588c960 | 262 | kfree(sbi->s_vhdr_buf); |
52399b17 CH |
263 | out: |
264 | return error; | |
1da177e4 | 265 | } |