xref: /linux/fs/hfsplus/wrapper.c (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
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 <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 
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  * @op: direction of I/O
34  * @op_flags: request op flags
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,
48 		       void *buf, void **data, int op, int op_flags)
49 {
50 	struct bio *bio;
51 	int ret = 0;
52 	u64 io_size;
53 	loff_t start;
54 	int offset;
55 
56 	/*
57 	 * Align sector to hardware sector size and find offset. We
58 	 * assume that io_size is a power of two, which _should_
59 	 * be true.
60 	 */
61 	io_size = hfsplus_min_io_size(sb);
62 	start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT;
63 	offset = start & (io_size - 1);
64 	sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1);
65 
66 	bio = bio_alloc(sb->s_bdev, 1, op | op_flags, GFP_NOIO);
67 	bio->bi_iter.bi_sector = sector;
68 
69 	if (op != WRITE && data)
70 		*data = (u8 *)buf + offset;
71 
72 	while (io_size > 0) {
73 		unsigned int page_offset = offset_in_page(buf);
74 		unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset,
75 					 io_size);
76 
77 		ret = bio_add_page(bio, virt_to_page(buf), len, page_offset);
78 		if (ret != len) {
79 			ret = -EIO;
80 			goto out;
81 		}
82 		io_size -= len;
83 		buf = (u8 *)buf + len;
84 	}
85 
86 	ret = submit_bio_wait(bio);
87 out:
88 	bio_put(bio);
89 	return ret < 0 ? ret : 0;
90 }
91 
92 static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
93 {
94 	u32 extent;
95 	u16 attrib;
96 	__be16 sig;
97 
98 	sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
99 	if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
100 	    sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
101 		return 0;
102 
103 	attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
104 	if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
105 	   !(attrib & HFSP_WRAP_ATTRIB_SPARED))
106 		return 0;
107 
108 	wd->ablk_size =
109 		be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
110 	if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
111 		return 0;
112 	if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
113 		return 0;
114 	wd->ablk_start =
115 		be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
116 
117 	extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
118 	wd->embed_start = (extent >> 16) & 0xFFFF;
119 	wd->embed_count = extent & 0xFFFF;
120 
121 	return 1;
122 }
123 
124 static int hfsplus_get_last_session(struct super_block *sb,
125 				    sector_t *start, sector_t *size)
126 {
127 	struct cdrom_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk);
128 
129 	/* default values */
130 	*start = 0;
131 	*size = bdev_nr_sectors(sb->s_bdev);
132 
133 	if (HFSPLUS_SB(sb)->session >= 0) {
134 		struct cdrom_tocentry te;
135 
136 		if (!cdi)
137 			return -EINVAL;
138 
139 		te.cdte_track = HFSPLUS_SB(sb)->session;
140 		te.cdte_format = CDROM_LBA;
141 		if (cdrom_read_tocentry(cdi, &te) ||
142 		    (te.cdte_ctrl & CDROM_DATA_TRACK) != 4) {
143 			pr_err("invalid session number or type of track\n");
144 			return -EINVAL;
145 		}
146 		*start = (sector_t)te.cdte_addr.lba << 2;
147 	} else if (cdi) {
148 		struct cdrom_multisession ms_info;
149 
150 		ms_info.addr_format = CDROM_LBA;
151 		if (cdrom_multisession(cdi, &ms_info) == 0 && ms_info.xa_flag)
152 			*start = (sector_t)ms_info.addr.lba << 2;
153 	}
154 
155 	return 0;
156 }
157 
158 /* Find the volume header and fill in some minimum bits in superblock */
159 /* Takes in super block, returns true if good data read */
160 int hfsplus_read_wrapper(struct super_block *sb)
161 {
162 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
163 	struct hfsplus_wd wd;
164 	sector_t part_start, part_size;
165 	u32 blocksize;
166 	int error = 0;
167 
168 	error = -EINVAL;
169 	blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
170 	if (!blocksize)
171 		goto out;
172 
173 	if (hfsplus_get_last_session(sb, &part_start, &part_size))
174 		goto out;
175 
176 	error = -ENOMEM;
177 	sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
178 	if (!sbi->s_vhdr_buf)
179 		goto out;
180 	sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
181 	if (!sbi->s_backup_vhdr_buf)
182 		goto out_free_vhdr;
183 
184 reread:
185 	error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
186 				   sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
187 				   REQ_OP_READ, 0);
188 	if (error)
189 		goto out_free_backup_vhdr;
190 
191 	error = -EINVAL;
192 	switch (sbi->s_vhdr->signature) {
193 	case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
194 		set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
195 		fallthrough;
196 	case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
197 		break;
198 	case cpu_to_be16(HFSP_WRAP_MAGIC):
199 		if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
200 			goto out_free_backup_vhdr;
201 		wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
202 		part_start += (sector_t)wd.ablk_start +
203 			       (sector_t)wd.embed_start * wd.ablk_size;
204 		part_size = (sector_t)wd.embed_count * wd.ablk_size;
205 		goto reread;
206 	default:
207 		/*
208 		 * Check for a partition block.
209 		 *
210 		 * (should do this only for cdrom/loop though)
211 		 */
212 		if (hfs_part_find(sb, &part_start, &part_size))
213 			goto out_free_backup_vhdr;
214 		goto reread;
215 	}
216 
217 	error = hfsplus_submit_bio(sb, part_start + part_size - 2,
218 				   sbi->s_backup_vhdr_buf,
219 				   (void **)&sbi->s_backup_vhdr, REQ_OP_READ,
220 				   0);
221 	if (error)
222 		goto out_free_backup_vhdr;
223 
224 	error = -EINVAL;
225 	if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
226 		pr_warn("invalid secondary volume header\n");
227 		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.
234 	 */
235 	if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
236 		goto out_free_backup_vhdr;
237 	sbi->alloc_blksz = blocksize;
238 	sbi->alloc_blksz_shift = ilog2(blocksize);
239 	blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE);
240 
241 	/*
242 	 * Align block size to block offset.
243 	 */
244 	while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
245 		blocksize >>= 1;
246 
247 	if (sb_set_blocksize(sb, blocksize) != blocksize) {
248 		pr_err("unable to set blocksize to %u!\n", blocksize);
249 		goto out_free_backup_vhdr;
250 	}
251 
252 	sbi->blockoffset =
253 		part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
254 	sbi->part_start = part_start;
255 	sbi->sect_count = part_size;
256 	sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
257 	return 0;
258 
259 out_free_backup_vhdr:
260 	kfree(sbi->s_backup_vhdr_buf);
261 out_free_vhdr:
262 	kfree(sbi->s_vhdr_buf);
263 out:
264 	return error;
265 }
266