Loading...
1/*
2 * linux/fs/hfsplus/wrapper.c
3 *
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
7 *
8 * Handling of HFS wrappers around HFS+ volumes
9 */
10
11#include <linux/fs.h>
12#include <linux/blkdev.h>
13#include <linux/cdrom.h>
14#include <linux/genhd.h>
15#include <asm/unaligned.h>
16
17#include "hfsplus_fs.h"
18#include "hfsplus_raw.h"
19
20struct hfsplus_wd {
21 u32 ablk_size;
22 u16 ablk_start;
23 u16 embed_start;
24 u16 embed_count;
25};
26
27static void hfsplus_end_io_sync(struct bio *bio, int err)
28{
29 if (err)
30 clear_bit(BIO_UPTODATE, &bio->bi_flags);
31 complete(bio->bi_private);
32}
33
34/*
35 * hfsplus_submit_bio - Perfrom block I/O
36 * @sb: super block of volume for I/O
37 * @sector: block to read or write, for blocks of HFSPLUS_SECTOR_SIZE bytes
38 * @buf: buffer for I/O
39 * @data: output pointer for location of requested data
40 * @rw: direction of I/O
41 *
42 * The unit of I/O is hfsplus_min_io_size(sb), which may be bigger than
43 * HFSPLUS_SECTOR_SIZE, and @buf must be sized accordingly. On reads
44 * @data will return a pointer to the start of the requested sector,
45 * which may not be the same location as @buf.
46 *
47 * If @sector is not aligned to the bdev logical block size it will
48 * be rounded down. For writes this means that @buf should contain data
49 * that starts at the rounded-down address. As long as the data was
50 * read using hfsplus_submit_bio() and the same buffer is used things
51 * will work correctly.
52 */
53int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
54 void *buf, void **data, int rw)
55{
56 DECLARE_COMPLETION_ONSTACK(wait);
57 struct bio *bio;
58 int ret = 0;
59 unsigned int io_size;
60 loff_t start;
61 int offset;
62
63 /*
64 * Align sector to hardware sector size and find offset. We
65 * assume that io_size is a power of two, which _should_
66 * be true.
67 */
68 io_size = hfsplus_min_io_size(sb);
69 start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT;
70 offset = start & (io_size - 1);
71 sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1);
72
73 bio = bio_alloc(GFP_NOIO, 1);
74 bio->bi_sector = sector;
75 bio->bi_bdev = sb->s_bdev;
76 bio->bi_end_io = hfsplus_end_io_sync;
77 bio->bi_private = &wait;
78
79 if (!(rw & WRITE) && data)
80 *data = (u8 *)buf + offset;
81
82 while (io_size > 0) {
83 unsigned int page_offset = offset_in_page(buf);
84 unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset,
85 io_size);
86
87 ret = bio_add_page(bio, virt_to_page(buf), len, page_offset);
88 if (ret != len) {
89 ret = -EIO;
90 goto out;
91 }
92 io_size -= len;
93 buf = (u8 *)buf + len;
94 }
95
96 submit_bio(rw, bio);
97 wait_for_completion(&wait);
98
99 if (!bio_flagged(bio, BIO_UPTODATE))
100 ret = -EIO;
101
102out:
103 bio_put(bio);
104 return ret < 0 ? ret : 0;
105}
106
107static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
108{
109 u32 extent;
110 u16 attrib;
111 __be16 sig;
112
113 sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
114 if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
115 sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
116 return 0;
117
118 attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
119 if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
120 !(attrib & HFSP_WRAP_ATTRIB_SPARED))
121 return 0;
122
123 wd->ablk_size =
124 be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
125 if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
126 return 0;
127 if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
128 return 0;
129 wd->ablk_start =
130 be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
131
132 extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
133 wd->embed_start = (extent >> 16) & 0xFFFF;
134 wd->embed_count = extent & 0xFFFF;
135
136 return 1;
137}
138
139static int hfsplus_get_last_session(struct super_block *sb,
140 sector_t *start, sector_t *size)
141{
142 struct cdrom_multisession ms_info;
143 struct cdrom_tocentry te;
144 int res;
145
146 /* default values */
147 *start = 0;
148 *size = sb->s_bdev->bd_inode->i_size >> 9;
149
150 if (HFSPLUS_SB(sb)->session >= 0) {
151 te.cdte_track = HFSPLUS_SB(sb)->session;
152 te.cdte_format = CDROM_LBA;
153 res = ioctl_by_bdev(sb->s_bdev,
154 CDROMREADTOCENTRY, (unsigned long)&te);
155 if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
156 *start = (sector_t)te.cdte_addr.lba << 2;
157 return 0;
158 }
159 printk(KERN_ERR "hfs: invalid session number or type of track\n");
160 return -EINVAL;
161 }
162 ms_info.addr_format = CDROM_LBA;
163 res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION,
164 (unsigned long)&ms_info);
165 if (!res && ms_info.xa_flag)
166 *start = (sector_t)ms_info.addr.lba << 2;
167 return 0;
168}
169
170/* Find the volume header and fill in some minimum bits in superblock */
171/* Takes in super block, returns true if good data read */
172int hfsplus_read_wrapper(struct super_block *sb)
173{
174 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
175 struct hfsplus_wd wd;
176 sector_t part_start, part_size;
177 u32 blocksize;
178 int error = 0;
179
180 error = -EINVAL;
181 blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
182 if (!blocksize)
183 goto out;
184
185 if (hfsplus_get_last_session(sb, &part_start, &part_size))
186 goto out;
187
188 error = -ENOMEM;
189 sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
190 if (!sbi->s_vhdr_buf)
191 goto out;
192 sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
193 if (!sbi->s_backup_vhdr_buf)
194 goto out_free_vhdr;
195
196reread:
197 error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
198 sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
199 READ);
200 if (error)
201 goto out_free_backup_vhdr;
202
203 error = -EINVAL;
204 switch (sbi->s_vhdr->signature) {
205 case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
206 set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
207 /*FALLTHRU*/
208 case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
209 break;
210 case cpu_to_be16(HFSP_WRAP_MAGIC):
211 if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
212 goto out_free_backup_vhdr;
213 wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
214 part_start += (sector_t)wd.ablk_start +
215 (sector_t)wd.embed_start * wd.ablk_size;
216 part_size = (sector_t)wd.embed_count * wd.ablk_size;
217 goto reread;
218 default:
219 /*
220 * Check for a partition block.
221 *
222 * (should do this only for cdrom/loop though)
223 */
224 if (hfs_part_find(sb, &part_start, &part_size))
225 goto out_free_backup_vhdr;
226 goto reread;
227 }
228
229 error = hfsplus_submit_bio(sb, part_start + part_size - 2,
230 sbi->s_backup_vhdr_buf,
231 (void **)&sbi->s_backup_vhdr, READ);
232 if (error)
233 goto out_free_backup_vhdr;
234
235 error = -EINVAL;
236 if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
237 printk(KERN_WARNING
238 "hfs: invalid secondary volume header\n");
239 goto out_free_backup_vhdr;
240 }
241
242 blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
243
244 /*
245 * Block size must be at least as large as a sector and a multiple of 2.
246 */
247 if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
248 goto out_free_backup_vhdr;
249 sbi->alloc_blksz = blocksize;
250 sbi->alloc_blksz_shift = 0;
251 while ((blocksize >>= 1) != 0)
252 sbi->alloc_blksz_shift++;
253 blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE);
254
255 /*
256 * Align block size to block offset.
257 */
258 while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
259 blocksize >>= 1;
260
261 if (sb_set_blocksize(sb, blocksize) != blocksize) {
262 printk(KERN_ERR "hfs: unable to set blocksize to %u!\n",
263 blocksize);
264 goto out_free_backup_vhdr;
265 }
266
267 sbi->blockoffset =
268 part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
269 sbi->part_start = part_start;
270 sbi->sect_count = part_size;
271 sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
272 return 0;
273
274out_free_backup_vhdr:
275 kfree(sbi->s_backup_vhdr_buf);
276out_free_vhdr:
277 kfree(sbi->s_vhdr_buf);
278out:
279 return error;
280}
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/genhd.h>
16#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
28/**
29 * hfsplus_submit_bio - Perform block I/O
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
34 * @op: direction of I/O
35 * @op_flags: request op flags
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 */
48int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
49 void *buf, void **data, int op, int op_flags)
50{
51 struct bio *bio;
52 int ret = 0;
53 u64 io_size;
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);
66
67 bio = bio_alloc(GFP_NOIO, 1);
68 bio->bi_iter.bi_sector = sector;
69 bio_set_dev(bio, sb->s_bdev);
70 bio_set_op_attrs(bio, op, op_flags);
71
72 if (op != WRITE && data)
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 }
88
89 ret = submit_bio_wait(bio);
90out:
91 bio_put(bio);
92 return ret < 0 ? ret : 0;
93}
94
95static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
96{
97 u32 extent;
98 u16 attrib;
99 __be16 sig;
100
101 sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
102 if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
103 sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
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
111 wd->ablk_size =
112 be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
113 if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
114 return 0;
115 if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
116 return 0;
117 wd->ablk_start =
118 be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
119
120 extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
121 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{
130 struct cdrom_multisession ms_info;
131 struct cdrom_tocentry te;
132 int res;
133
134 /* default values */
135 *start = 0;
136 *size = i_size_read(sb->s_bdev->bd_inode) >> 9;
137
138 if (HFSPLUS_SB(sb)->session >= 0) {
139 te.cdte_track = HFSPLUS_SB(sb)->session;
140 te.cdte_format = CDROM_LBA;
141 res = ioctl_by_bdev(sb->s_bdev,
142 CDROMREADTOCENTRY, (unsigned long)&te);
143 if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
144 *start = (sector_t)te.cdte_addr.lba << 2;
145 return 0;
146 }
147 pr_err("invalid session number or type of track\n");
148 return -EINVAL;
149 }
150 ms_info.addr_format = CDROM_LBA;
151 res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION,
152 (unsigned long)&ms_info);
153 if (!res && ms_info.xa_flag)
154 *start = (sector_t)ms_info.addr.lba << 2;
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 */
160int 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
184reread:
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 /*FALLTHRU*/
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
259out_free_backup_vhdr:
260 kfree(sbi->s_backup_vhdr_buf);
261out_free_vhdr:
262 kfree(sbi->s_vhdr_buf);
263out:
264 return error;
265}