]>
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 ([email protected]) | |
7 | * (C) 2003 Ardis Technologies <[email protected]> | |
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> | |
1da177e4 LT |
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 | ||
915ab236 FF |
28 | /** |
29 | * hfsplus_submit_bio - Perform block I/O | |
6596528e SF |
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 | |
67ed2596 MC |
34 | * @op: direction of I/O |
35 | * @op_flags: request op flags | |
6596528e SF |
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, | |
67ed2596 | 49 | void *buf, void **data, int op, int op_flags) |
52399b17 | 50 | { |
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 CH |
66 | |
67 | bio = bio_alloc(GFP_NOIO, 1); | |
4f024f37 | 68 | bio->bi_iter.bi_sector = sector; |
74d46992 | 69 | bio_set_dev(bio, sb->s_bdev); |
67ed2596 | 70 | bio_set_op_attrs(bio, op, op_flags); |
52399b17 | 71 | |
67ed2596 | 72 | if (op != WRITE && data) |
6596528e SF |
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 | } | |
52399b17 | 88 | |
4e49ea4a | 89 | ret = submit_bio_wait(bio); |
6596528e | 90 | out: |
50176dde | 91 | bio_put(bio); |
6596528e | 92 | return ret < 0 ? ret : 0; |
52399b17 CH |
93 | } |
94 | ||
1da177e4 LT |
95 | static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd) |
96 | { | |
97 | u32 extent; | |
98 | u16 attrib; | |
2179d372 | 99 | __be16 sig; |
1da177e4 | 100 | |
2179d372 DE |
101 | sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG); |
102 | if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) && | |
103 | sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX)) | |
1da177e4 LT |
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 | ||
2753cc28 AS |
111 | wd->ablk_size = |
112 | be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE)); | |
1da177e4 LT |
113 | if (wd->ablk_size < HFSPLUS_SECTOR_SIZE) |
114 | return 0; | |
115 | if (wd->ablk_size % HFSPLUS_SECTOR_SIZE) | |
116 | return 0; | |
2753cc28 AS |
117 | wd->ablk_start = |
118 | be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART)); | |
1da177e4 | 119 | |
8b3789e5 | 120 | extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT); |
1da177e4 LT |
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 | { | |
f252fa33 | 130 | struct cdrom_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk); |
1da177e4 LT |
131 | |
132 | /* default values */ | |
133 | *start = 0; | |
a3f22350 | 134 | *size = i_size_read(sb->s_bdev->bd_inode) >> 9; |
1da177e4 | 135 | |
dd73a01a | 136 | if (HFSPLUS_SB(sb)->session >= 0) { |
f252fa33 CH |
137 | struct cdrom_tocentry te; |
138 | ||
139 | if (!cdi) | |
140 | return -EINVAL; | |
141 | ||
dd73a01a | 142 | te.cdte_track = HFSPLUS_SB(sb)->session; |
1da177e4 | 143 | te.cdte_format = CDROM_LBA; |
f252fa33 CH |
144 | 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; | |
1da177e4 | 148 | } |
f252fa33 CH |
149 | *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; | |
1da177e4 | 156 | } |
f252fa33 | 157 | |
1da177e4 LT |
158 | 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 */ | |
163 | int hfsplus_read_wrapper(struct super_block *sb) | |
164 | { | |
dd73a01a | 165 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb); |
1da177e4 LT |
166 | struct hfsplus_wd wd; |
167 | sector_t part_start, part_size; | |
168 | u32 blocksize; | |
52399b17 | 169 | int error = 0; |
1da177e4 | 170 | |
52399b17 | 171 | error = -EINVAL; |
1da177e4 LT |
172 | blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE); |
173 | if (!blocksize) | |
52399b17 | 174 | goto out; |
1da177e4 LT |
175 | |
176 | if (hfsplus_get_last_session(sb, &part_start, &part_size)) | |
52399b17 | 177 | goto out; |
1da177e4 | 178 | |
52399b17 | 179 | error = -ENOMEM; |
6596528e SF |
180 | sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL); |
181 | if (!sbi->s_vhdr_buf) | |
52399b17 | 182 | goto out; |
6596528e SF |
183 | sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL); |
184 | if (!sbi->s_backup_vhdr_buf) | |
52399b17 CH |
185 | goto out_free_vhdr; |
186 | ||
187 | reread: | |
6596528e SF |
188 | error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, |
189 | sbi->s_vhdr_buf, (void **)&sbi->s_vhdr, | |
67ed2596 | 190 | REQ_OP_READ, 0); |
52399b17 CH |
191 | 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); | |
df561f66 | 198 | fallthrough; |
52399b17 CH |
199 | 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)) | |
a1dbcef0 | 203 | goto out_free_backup_vhdr; |
52399b17 | 204 | wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT; |
4ba2d5fd CH |
205 | 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; | |
52399b17 CH |
208 | goto reread; |
209 | default: | |
210 | /* | |
211 | * Check for a partition block. | |
212 | * | |
1da177e4 LT |
213 | * (should do this only for cdrom/loop though) |
214 | */ | |
215 | if (hfs_part_find(sb, &part_start, &part_size)) | |
a1dbcef0 | 216 | goto out_free_backup_vhdr; |
52399b17 CH |
217 | goto reread; |
218 | } | |
219 | ||
6596528e SF |
220 | error = hfsplus_submit_bio(sb, part_start + part_size - 2, |
221 | sbi->s_backup_vhdr_buf, | |
67ed2596 MC |
222 | (void **)&sbi->s_backup_vhdr, REQ_OP_READ, |
223 | 0); | |
52399b17 CH |
224 | if (error) |
225 | goto out_free_backup_vhdr; | |
226 | ||
227 | error = -EINVAL; | |
228 | if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) { | |
d6142673 | 229 | pr_warn("invalid secondary volume header\n"); |
52399b17 | 230 | goto out_free_backup_vhdr; |
1da177e4 LT |
231 | } |
232 | ||
52399b17 | 233 | blocksize = be32_to_cpu(sbi->s_vhdr->blocksize); |
1da177e4 | 234 | |
52399b17 CH |
235 | /* |
236 | * Block size must be at least as large as a sector and a multiple of 2. | |
1da177e4 | 237 | */ |
52399b17 CH |
238 | if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize)) |
239 | goto out_free_backup_vhdr; | |
dd73a01a | 240 | sbi->alloc_blksz = blocksize; |
297cc272 | 241 | sbi->alloc_blksz_shift = ilog2(blocksize); |
915ab236 | 242 | blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE); |
1da177e4 | 243 | |
52399b17 CH |
244 | /* |
245 | * Align block size to block offset. | |
246 | */ | |
1da177e4 LT |
247 | while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1)) |
248 | blocksize >>= 1; | |
249 | ||
250 | if (sb_set_blocksize(sb, blocksize) != blocksize) { | |
d6142673 | 251 | pr_err("unable to set blocksize to %u!\n", blocksize); |
52399b17 | 252 | goto out_free_backup_vhdr; |
1da177e4 LT |
253 | } |
254 | ||
dd73a01a CH |
255 | sbi->blockoffset = |
256 | part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT); | |
52399b17 | 257 | sbi->part_start = part_start; |
dd73a01a CH |
258 | sbi->sect_count = part_size; |
259 | sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits; | |
1da177e4 | 260 | return 0; |
52399b17 CH |
261 | |
262 | out_free_backup_vhdr: | |
f588c960 | 263 | kfree(sbi->s_backup_vhdr_buf); |
52399b17 | 264 | out_free_vhdr: |
f588c960 | 265 | kfree(sbi->s_vhdr_buf); |
52399b17 CH |
266 | out: |
267 | return error; | |
1da177e4 | 268 | } |