1 // SPDX-License-Identifier: GPL-2.0
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>
15 #include <linux/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
27 /**
28 * hfsplus_submit_bio - Perform block I/O
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
33 * @opf: I/O operation type and flags
34 *
35 * The unit of I/O is hfsplus_min_io_size(sb), which may be bigger than
36 * HFSPLUS_SECTOR_SIZE, and @buf must be sized accordingly. On reads
37 * @data will return a pointer to the start of the requested sector,
38 * which may not be the same location as @buf.
39 *
40 * If @sector is not aligned to the bdev logical block size it will
41 * be rounded down. For writes this means that @buf should contain data
42 * that starts at the rounded-down address. As long as the data was
43 * read using hfsplus_submit_bio() and the same buffer is used things
44 * will work correctly.
45 *
46 * Returns: %0 on success else -errno code
47 */
hfsplus_submit_bio(struct super_block * sb,sector_t sector,void * buf,void ** data,blk_opf_t opf)48 int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
49 void *buf, void **data, blk_opf_t opf)
50 {
51 u64 io_size = hfsplus_min_io_size(sb);
52 loff_t start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT;
53 int offset = start & (io_size - 1);
54
55 if ((opf & REQ_OP_MASK) != REQ_OP_WRITE && data)
56 *data = (u8 *)buf + offset;
57
58 /*
59 * Align sector to hardware sector size and find offset. We assume that
60 * io_size is a power of two, which _should_ be true.
61 */
62 sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1);
63 return bdev_rw_virt(sb->s_bdev, sector, buf, io_size, opf);
64 }
65
hfsplus_read_mdb(void * bufptr,struct hfsplus_wd * wd)66 static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
67 {
68 u32 extent;
69 u16 attrib;
70 __be16 sig;
71
72 sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
73 if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
74 sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
75 return 0;
76
77 attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
78 if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
79 !(attrib & HFSP_WRAP_ATTRIB_SPARED))
80 return 0;
81
82 wd->ablk_size =
83 be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
84 if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
85 return 0;
86 if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
87 return 0;
88 wd->ablk_start =
89 be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
90
91 extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
92 wd->embed_start = (extent >> 16) & 0xFFFF;
93 wd->embed_count = extent & 0xFFFF;
94
95 return 1;
96 }
97
hfsplus_get_last_session(struct super_block * sb,sector_t * start,sector_t * size)98 static int hfsplus_get_last_session(struct super_block *sb,
99 sector_t *start, sector_t *size)
100 {
101 struct cdrom_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk);
102
103 /* default values */
104 *start = 0;
105 *size = bdev_nr_sectors(sb->s_bdev);
106
107 if (HFSPLUS_SB(sb)->session >= 0) {
108 struct cdrom_tocentry te;
109
110 if (!cdi)
111 return -EINVAL;
112
113 te.cdte_track = HFSPLUS_SB(sb)->session;
114 te.cdte_format = CDROM_LBA;
115 if (cdrom_read_tocentry(cdi, &te) ||
116 (te.cdte_ctrl & CDROM_DATA_TRACK) != 4) {
117 pr_err("invalid session number or type of track\n");
118 return -EINVAL;
119 }
120 *start = (sector_t)te.cdte_addr.lba << 2;
121 } else if (cdi) {
122 struct cdrom_multisession ms_info;
123
124 ms_info.addr_format = CDROM_LBA;
125 if (cdrom_multisession(cdi, &ms_info) == 0 && ms_info.xa_flag)
126 *start = (sector_t)ms_info.addr.lba << 2;
127 }
128
129 return 0;
130 }
131
132 /* Find the volume header and fill in some minimum bits in superblock */
133 /* Takes in super block, returns true if good data read */
hfsplus_read_wrapper(struct super_block * sb)134 int hfsplus_read_wrapper(struct super_block *sb)
135 {
136 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
137 struct hfsplus_wd wd;
138 sector_t part_start, part_size;
139 u32 blocksize;
140 int error = 0;
141
142 error = -EINVAL;
143 blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
144 if (!blocksize)
145 goto out;
146
147 sbi->min_io_size = blocksize;
148
149 if (hfsplus_get_last_session(sb, &part_start, &part_size))
150 goto out;
151
152 error = -ENOMEM;
153 sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
154 if (!sbi->s_vhdr_buf)
155 goto out;
156 sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
157 if (!sbi->s_backup_vhdr_buf)
158 goto out_free_vhdr;
159
160 reread:
161 error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
162 sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
163 REQ_OP_READ);
164 if (error)
165 goto out_free_backup_vhdr;
166
167 error = -EINVAL;
168 switch (sbi->s_vhdr->signature) {
169 case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
170 set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
171 fallthrough;
172 case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
173 break;
174 case cpu_to_be16(HFSP_WRAP_MAGIC):
175 if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
176 goto out_free_backup_vhdr;
177 wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
178 part_start += (sector_t)wd.ablk_start +
179 (sector_t)wd.embed_start * wd.ablk_size;
180 part_size = (sector_t)wd.embed_count * wd.ablk_size;
181 goto reread;
182 default:
183 /*
184 * Check for a partition block.
185 *
186 * (should do this only for cdrom/loop though)
187 */
188 if (hfs_part_find(sb, &part_start, &part_size))
189 goto out_free_backup_vhdr;
190 goto reread;
191 }
192
193 error = hfsplus_submit_bio(sb, part_start + part_size - 2,
194 sbi->s_backup_vhdr_buf,
195 (void **)&sbi->s_backup_vhdr, REQ_OP_READ);
196 if (error)
197 goto out_free_backup_vhdr;
198
199 error = -EINVAL;
200 if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
201 pr_warn("invalid secondary volume header\n");
202 goto out_free_backup_vhdr;
203 }
204
205 blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
206
207 /*
208 * Block size must be at least as large as a sector and a multiple of 2.
209 */
210 if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
211 goto out_free_backup_vhdr;
212 sbi->alloc_blksz = blocksize;
213 sbi->alloc_blksz_shift = ilog2(blocksize);
214 blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE);
215
216 /*
217 * Align block size to block offset.
218 */
219 while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
220 blocksize >>= 1;
221
222 if (sb_set_blocksize(sb, blocksize) != blocksize) {
223 pr_err("unable to set blocksize to %u!\n", blocksize);
224 goto out_free_backup_vhdr;
225 }
226
227 sbi->blockoffset =
228 part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
229 sbi->part_start = part_start;
230 sbi->sect_count = part_size;
231 sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
232 return 0;
233
234 out_free_backup_vhdr:
235 kfree(sbi->s_backup_vhdr_buf);
236 out_free_vhdr:
237 kfree(sbi->s_vhdr_buf);
238 out:
239 return error;
240 }
241