tracing: Use guard() rather than scoped_guard()
[drm/drm-misc.git] / fs / hfsplus / wrapper.c
blob74801911bc1cc45bc14dd1035f606186882f266c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/hfsplus/wrapper.c
5 * Copyright (C) 2001
6 * Brad Boyer (flar@allandria.com)
7 * (C) 2003 Ardis Technologies <roman@ardistech.com>
9 * Handling of HFS wrappers around HFS+ volumes
12 #include <linux/fs.h>
13 #include <linux/blkdev.h>
14 #include <linux/cdrom.h>
15 #include <linux/unaligned.h>
17 #include "hfsplus_fs.h"
18 #include "hfsplus_raw.h"
20 struct hfsplus_wd {
21 u32 ablk_size;
22 u16 ablk_start;
23 u16 embed_start;
24 u16 embed_count;
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
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.
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.
46 * Returns: %0 on success else -errno code
48 int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
49 void *buf, void **data, blk_opf_t opf)
51 const enum req_op op = opf & REQ_OP_MASK;
52 struct bio *bio;
53 int ret = 0;
54 u64 io_size;
55 loff_t start;
56 int offset;
59 * Align sector to hardware sector size and find offset. We
60 * assume that io_size is a power of two, which _should_
61 * be true.
63 io_size = hfsplus_min_io_size(sb);
64 start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT;
65 offset = start & (io_size - 1);
66 sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1);
68 bio = bio_alloc(sb->s_bdev, 1, opf, GFP_NOIO);
69 bio->bi_iter.bi_sector = sector;
71 if (op != REQ_OP_WRITE && data)
72 *data = (u8 *)buf + offset;
74 while (io_size > 0) {
75 unsigned int page_offset = offset_in_page(buf);
76 unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset,
77 io_size);
79 ret = bio_add_page(bio, virt_to_page(buf), len, page_offset);
80 if (ret != len) {
81 ret = -EIO;
82 goto out;
84 io_size -= len;
85 buf = (u8 *)buf + len;
88 ret = submit_bio_wait(bio);
89 out:
90 bio_put(bio);
91 return ret < 0 ? ret : 0;
94 static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
96 u32 extent;
97 u16 attrib;
98 __be16 sig;
100 sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
101 if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
102 sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
103 return 0;
105 attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
106 if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
107 !(attrib & HFSP_WRAP_ATTRIB_SPARED))
108 return 0;
110 wd->ablk_size =
111 be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
112 if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
113 return 0;
114 if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
115 return 0;
116 wd->ablk_start =
117 be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
119 extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
120 wd->embed_start = (extent >> 16) & 0xFFFF;
121 wd->embed_count = extent & 0xFFFF;
123 return 1;
126 static int hfsplus_get_last_session(struct super_block *sb,
127 sector_t *start, sector_t *size)
129 struct cdrom_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk);
131 /* default values */
132 *start = 0;
133 *size = bdev_nr_sectors(sb->s_bdev);
135 if (HFSPLUS_SB(sb)->session >= 0) {
136 struct cdrom_tocentry te;
138 if (!cdi)
139 return -EINVAL;
141 te.cdte_track = HFSPLUS_SB(sb)->session;
142 te.cdte_format = CDROM_LBA;
143 if (cdrom_read_tocentry(cdi, &te) ||
144 (te.cdte_ctrl & CDROM_DATA_TRACK) != 4) {
145 pr_err("invalid session number or type of track\n");
146 return -EINVAL;
148 *start = (sector_t)te.cdte_addr.lba << 2;
149 } else if (cdi) {
150 struct cdrom_multisession ms_info;
152 ms_info.addr_format = CDROM_LBA;
153 if (cdrom_multisession(cdi, &ms_info) == 0 && ms_info.xa_flag)
154 *start = (sector_t)ms_info.addr.lba << 2;
157 return 0;
160 /* Find the volume header and fill in some minimum bits in superblock */
161 /* Takes in super block, returns true if good data read */
162 int hfsplus_read_wrapper(struct super_block *sb)
164 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
165 struct hfsplus_wd wd;
166 sector_t part_start, part_size;
167 u32 blocksize;
168 int error = 0;
170 error = -EINVAL;
171 blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
172 if (!blocksize)
173 goto out;
175 sbi->min_io_size = blocksize;
177 if (hfsplus_get_last_session(sb, &part_start, &part_size))
178 goto out;
180 error = -ENOMEM;
181 sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
182 if (!sbi->s_vhdr_buf)
183 goto out;
184 sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
185 if (!sbi->s_backup_vhdr_buf)
186 goto out_free_vhdr;
188 reread:
189 error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
190 sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
191 REQ_OP_READ);
192 if (error)
193 goto out_free_backup_vhdr;
195 error = -EINVAL;
196 switch (sbi->s_vhdr->signature) {
197 case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
198 set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
199 fallthrough;
200 case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
201 break;
202 case cpu_to_be16(HFSP_WRAP_MAGIC):
203 if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
204 goto out_free_backup_vhdr;
205 wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
206 part_start += (sector_t)wd.ablk_start +
207 (sector_t)wd.embed_start * wd.ablk_size;
208 part_size = (sector_t)wd.embed_count * wd.ablk_size;
209 goto reread;
210 default:
212 * Check for a partition block.
214 * (should do this only for cdrom/loop though)
216 if (hfs_part_find(sb, &part_start, &part_size))
217 goto out_free_backup_vhdr;
218 goto reread;
221 error = hfsplus_submit_bio(sb, part_start + part_size - 2,
222 sbi->s_backup_vhdr_buf,
223 (void **)&sbi->s_backup_vhdr, REQ_OP_READ);
224 if (error)
225 goto out_free_backup_vhdr;
227 error = -EINVAL;
228 if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
229 pr_warn("invalid secondary volume header\n");
230 goto out_free_backup_vhdr;
233 blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
236 * Block size must be at least as large as a sector and a multiple of 2.
238 if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
239 goto out_free_backup_vhdr;
240 sbi->alloc_blksz = blocksize;
241 sbi->alloc_blksz_shift = ilog2(blocksize);
242 blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE);
245 * Align block size to block offset.
247 while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
248 blocksize >>= 1;
250 if (sb_set_blocksize(sb, blocksize) != blocksize) {
251 pr_err("unable to set blocksize to %u!\n", blocksize);
252 goto out_free_backup_vhdr;
255 sbi->blockoffset =
256 part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
257 sbi->part_start = part_start;
258 sbi->sect_count = part_size;
259 sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
260 return 0;
262 out_free_backup_vhdr:
263 kfree(sbi->s_backup_vhdr_buf);
264 out_free_vhdr:
265 kfree(sbi->s_vhdr_buf);
266 out:
267 return error;