1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * linux/fs/hfsplus/wrapper.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 2001
61da177e4SLinus Torvalds * Brad Boyer (flar@allandria.com)
71da177e4SLinus Torvalds * (C) 2003 Ardis Technologies <roman@ardistech.com>
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * Handling of HFS wrappers around HFS+ volumes
101da177e4SLinus Torvalds */
111da177e4SLinus Torvalds
121da177e4SLinus Torvalds #include <linux/fs.h>
131da177e4SLinus Torvalds #include <linux/blkdev.h>
141da177e4SLinus Torvalds #include <linux/cdrom.h>
151da177e4SLinus Torvalds #include <asm/unaligned.h>
161da177e4SLinus Torvalds
171da177e4SLinus Torvalds #include "hfsplus_fs.h"
181da177e4SLinus Torvalds #include "hfsplus_raw.h"
191da177e4SLinus Torvalds
201da177e4SLinus Torvalds struct hfsplus_wd {
211da177e4SLinus Torvalds u32 ablk_size;
221da177e4SLinus Torvalds u16 ablk_start;
231da177e4SLinus Torvalds u16 embed_start;
241da177e4SLinus Torvalds u16 embed_count;
251da177e4SLinus Torvalds };
261da177e4SLinus Torvalds
27915ab236SFabian Frederick /**
28915ab236SFabian Frederick * hfsplus_submit_bio - Perform block I/O
296596528eSSeth Forshee * @sb: super block of volume for I/O
306596528eSSeth Forshee * @sector: block to read or write, for blocks of HFSPLUS_SECTOR_SIZE bytes
316596528eSSeth Forshee * @buf: buffer for I/O
326596528eSSeth Forshee * @data: output pointer for location of requested data
3367ed2596SMike Christie * @op: direction of I/O
3467ed2596SMike Christie * @op_flags: request op flags
356596528eSSeth Forshee *
366596528eSSeth Forshee * The unit of I/O is hfsplus_min_io_size(sb), which may be bigger than
376596528eSSeth Forshee * HFSPLUS_SECTOR_SIZE, and @buf must be sized accordingly. On reads
386596528eSSeth Forshee * @data will return a pointer to the start of the requested sector,
396596528eSSeth Forshee * which may not be the same location as @buf.
406596528eSSeth Forshee *
416596528eSSeth Forshee * If @sector is not aligned to the bdev logical block size it will
426596528eSSeth Forshee * be rounded down. For writes this means that @buf should contain data
436596528eSSeth Forshee * that starts at the rounded-down address. As long as the data was
446596528eSSeth Forshee * read using hfsplus_submit_bio() and the same buffer is used things
456596528eSSeth Forshee * will work correctly.
466596528eSSeth Forshee */
hfsplus_submit_bio(struct super_block * sb,sector_t sector,void * buf,void ** data,blk_opf_t opf)476596528eSSeth Forshee int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
48c85f9992SBart Van Assche void *buf, void **data, blk_opf_t opf)
4952399b17SChristoph Hellwig {
50c85f9992SBart Van Assche const enum req_op op = opf & REQ_OP_MASK;
5152399b17SChristoph Hellwig struct bio *bio;
5250176ddeSSeth Forshee int ret = 0;
53a6dc8c04SJanne Kalliomäki u64 io_size;
546596528eSSeth Forshee loff_t start;
556596528eSSeth Forshee int offset;
566596528eSSeth Forshee
576596528eSSeth Forshee /*
586596528eSSeth Forshee * Align sector to hardware sector size and find offset. We
596596528eSSeth Forshee * assume that io_size is a power of two, which _should_
606596528eSSeth Forshee * be true.
616596528eSSeth Forshee */
626596528eSSeth Forshee io_size = hfsplus_min_io_size(sb);
636596528eSSeth Forshee start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT;
646596528eSSeth Forshee offset = start & (io_size - 1);
656596528eSSeth Forshee sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1);
6652399b17SChristoph Hellwig
67c85f9992SBart Van Assche bio = bio_alloc(sb->s_bdev, 1, opf, GFP_NOIO);
684f024f37SKent Overstreet bio->bi_iter.bi_sector = sector;
6952399b17SChristoph Hellwig
70c85f9992SBart Van Assche if (op != REQ_OP_WRITE && data)
716596528eSSeth Forshee *data = (u8 *)buf + offset;
726596528eSSeth Forshee
736596528eSSeth Forshee while (io_size > 0) {
746596528eSSeth Forshee unsigned int page_offset = offset_in_page(buf);
756596528eSSeth Forshee unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset,
766596528eSSeth Forshee io_size);
776596528eSSeth Forshee
786596528eSSeth Forshee ret = bio_add_page(bio, virt_to_page(buf), len, page_offset);
796596528eSSeth Forshee if (ret != len) {
806596528eSSeth Forshee ret = -EIO;
816596528eSSeth Forshee goto out;
826596528eSSeth Forshee }
836596528eSSeth Forshee io_size -= len;
846596528eSSeth Forshee buf = (u8 *)buf + len;
856596528eSSeth Forshee }
8652399b17SChristoph Hellwig
874e49ea4aSMike Christie ret = submit_bio_wait(bio);
886596528eSSeth Forshee out:
8950176ddeSSeth Forshee bio_put(bio);
906596528eSSeth Forshee return ret < 0 ? ret : 0;
9152399b17SChristoph Hellwig }
9252399b17SChristoph Hellwig
hfsplus_read_mdb(void * bufptr,struct hfsplus_wd * wd)931da177e4SLinus Torvalds static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
941da177e4SLinus Torvalds {
951da177e4SLinus Torvalds u32 extent;
961da177e4SLinus Torvalds u16 attrib;
972179d372SDavid Elliott __be16 sig;
981da177e4SLinus Torvalds
992179d372SDavid Elliott sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
1002179d372SDavid Elliott if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
1012179d372SDavid Elliott sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
1021da177e4SLinus Torvalds return 0;
1031da177e4SLinus Torvalds
1041da177e4SLinus Torvalds attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
1051da177e4SLinus Torvalds if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
1061da177e4SLinus Torvalds !(attrib & HFSP_WRAP_ATTRIB_SPARED))
1071da177e4SLinus Torvalds return 0;
1081da177e4SLinus Torvalds
1092753cc28SAnton Salikhmetov wd->ablk_size =
1102753cc28SAnton Salikhmetov be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
1111da177e4SLinus Torvalds if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
1121da177e4SLinus Torvalds return 0;
1131da177e4SLinus Torvalds if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
1141da177e4SLinus Torvalds return 0;
1152753cc28SAnton Salikhmetov wd->ablk_start =
1162753cc28SAnton Salikhmetov be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
1171da177e4SLinus Torvalds
1188b3789e5SHarvey Harrison extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
1191da177e4SLinus Torvalds wd->embed_start = (extent >> 16) & 0xFFFF;
1201da177e4SLinus Torvalds wd->embed_count = extent & 0xFFFF;
1211da177e4SLinus Torvalds
1221da177e4SLinus Torvalds return 1;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds
hfsplus_get_last_session(struct super_block * sb,sector_t * start,sector_t * size)1251da177e4SLinus Torvalds static int hfsplus_get_last_session(struct super_block *sb,
1261da177e4SLinus Torvalds sector_t *start, sector_t *size)
1271da177e4SLinus Torvalds {
128f252fa33SChristoph Hellwig struct cdrom_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk);
1291da177e4SLinus Torvalds
1301da177e4SLinus Torvalds /* default values */
1311da177e4SLinus Torvalds *start = 0;
13278ed961bSChristoph Hellwig *size = bdev_nr_sectors(sb->s_bdev);
1331da177e4SLinus Torvalds
134dd73a01aSChristoph Hellwig if (HFSPLUS_SB(sb)->session >= 0) {
135f252fa33SChristoph Hellwig struct cdrom_tocentry te;
136f252fa33SChristoph Hellwig
137f252fa33SChristoph Hellwig if (!cdi)
138f252fa33SChristoph Hellwig return -EINVAL;
139f252fa33SChristoph Hellwig
140dd73a01aSChristoph Hellwig te.cdte_track = HFSPLUS_SB(sb)->session;
1411da177e4SLinus Torvalds te.cdte_format = CDROM_LBA;
142f252fa33SChristoph Hellwig if (cdrom_read_tocentry(cdi, &te) ||
143f252fa33SChristoph Hellwig (te.cdte_ctrl & CDROM_DATA_TRACK) != 4) {
144d6142673SJoe Perches pr_err("invalid session number or type of track\n");
1451da177e4SLinus Torvalds return -EINVAL;
1461da177e4SLinus Torvalds }
147f252fa33SChristoph Hellwig *start = (sector_t)te.cdte_addr.lba << 2;
148f252fa33SChristoph Hellwig } else if (cdi) {
149f252fa33SChristoph Hellwig struct cdrom_multisession ms_info;
150f252fa33SChristoph Hellwig
1511da177e4SLinus Torvalds ms_info.addr_format = CDROM_LBA;
152f252fa33SChristoph Hellwig if (cdrom_multisession(cdi, &ms_info) == 0 && ms_info.xa_flag)
1531da177e4SLinus Torvalds *start = (sector_t)ms_info.addr.lba << 2;
154f252fa33SChristoph Hellwig }
155f252fa33SChristoph Hellwig
1561da177e4SLinus Torvalds return 0;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds
1591da177e4SLinus Torvalds /* Find the volume header and fill in some minimum bits in superblock */
1601da177e4SLinus Torvalds /* Takes in super block, returns true if good data read */
hfsplus_read_wrapper(struct super_block * sb)1611da177e4SLinus Torvalds int hfsplus_read_wrapper(struct super_block *sb)
1621da177e4SLinus Torvalds {
163dd73a01aSChristoph Hellwig struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
1641da177e4SLinus Torvalds struct hfsplus_wd wd;
1651da177e4SLinus Torvalds sector_t part_start, part_size;
1661da177e4SLinus Torvalds u32 blocksize;
16752399b17SChristoph Hellwig int error = 0;
1681da177e4SLinus Torvalds
16952399b17SChristoph Hellwig error = -EINVAL;
1701da177e4SLinus Torvalds blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
1711da177e4SLinus Torvalds if (!blocksize)
17252399b17SChristoph Hellwig goto out;
1731da177e4SLinus Torvalds
174*21900e84SThadeu Lima de Souza Cascardo sbi->min_io_size = blocksize;
175*21900e84SThadeu Lima de Souza Cascardo
1761da177e4SLinus Torvalds if (hfsplus_get_last_session(sb, &part_start, &part_size))
17752399b17SChristoph Hellwig goto out;
1781da177e4SLinus Torvalds
17952399b17SChristoph Hellwig error = -ENOMEM;
1806596528eSSeth Forshee sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
1816596528eSSeth Forshee if (!sbi->s_vhdr_buf)
18252399b17SChristoph Hellwig goto out;
1836596528eSSeth Forshee sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
1846596528eSSeth Forshee if (!sbi->s_backup_vhdr_buf)
18552399b17SChristoph Hellwig goto out_free_vhdr;
18652399b17SChristoph Hellwig
18752399b17SChristoph Hellwig reread:
1886596528eSSeth Forshee error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
1896596528eSSeth Forshee sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
190c85f9992SBart Van Assche REQ_OP_READ);
19152399b17SChristoph Hellwig if (error)
19252399b17SChristoph Hellwig goto out_free_backup_vhdr;
19352399b17SChristoph Hellwig
19452399b17SChristoph Hellwig error = -EINVAL;
19552399b17SChristoph Hellwig switch (sbi->s_vhdr->signature) {
19652399b17SChristoph Hellwig case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
19752399b17SChristoph Hellwig set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
198df561f66SGustavo A. R. Silva fallthrough;
19952399b17SChristoph Hellwig case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
20052399b17SChristoph Hellwig break;
20152399b17SChristoph Hellwig case cpu_to_be16(HFSP_WRAP_MAGIC):
20252399b17SChristoph Hellwig if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
203a1dbcef0SChuck Ebbert goto out_free_backup_vhdr;
2041da177e4SLinus Torvalds wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
2054ba2d5fdSChristoph Hellwig part_start += (sector_t)wd.ablk_start +
2064ba2d5fdSChristoph Hellwig (sector_t)wd.embed_start * wd.ablk_size;
2074ba2d5fdSChristoph Hellwig part_size = (sector_t)wd.embed_count * wd.ablk_size;
20852399b17SChristoph Hellwig goto reread;
20952399b17SChristoph Hellwig default:
21052399b17SChristoph Hellwig /*
21152399b17SChristoph Hellwig * Check for a partition block.
21252399b17SChristoph Hellwig *
2131da177e4SLinus Torvalds * (should do this only for cdrom/loop though)
2141da177e4SLinus Torvalds */
2151da177e4SLinus Torvalds if (hfs_part_find(sb, &part_start, &part_size))
216a1dbcef0SChuck Ebbert goto out_free_backup_vhdr;
21752399b17SChristoph Hellwig goto reread;
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds
2206596528eSSeth Forshee error = hfsplus_submit_bio(sb, part_start + part_size - 2,
2216596528eSSeth Forshee sbi->s_backup_vhdr_buf,
222c85f9992SBart Van Assche (void **)&sbi->s_backup_vhdr, REQ_OP_READ);
22352399b17SChristoph Hellwig if (error)
22452399b17SChristoph Hellwig goto out_free_backup_vhdr;
2251da177e4SLinus Torvalds
22652399b17SChristoph Hellwig error = -EINVAL;
22752399b17SChristoph Hellwig if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
228d6142673SJoe Perches pr_warn("invalid secondary volume header\n");
22952399b17SChristoph Hellwig goto out_free_backup_vhdr;
23052399b17SChristoph Hellwig }
23152399b17SChristoph Hellwig
23252399b17SChristoph Hellwig blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
23352399b17SChristoph Hellwig
23452399b17SChristoph Hellwig /*
23552399b17SChristoph Hellwig * Block size must be at least as large as a sector and a multiple of 2.
2361da177e4SLinus Torvalds */
23752399b17SChristoph Hellwig if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
23852399b17SChristoph Hellwig goto out_free_backup_vhdr;
239dd73a01aSChristoph Hellwig sbi->alloc_blksz = blocksize;
240297cc272SFabian Frederick sbi->alloc_blksz_shift = ilog2(blocksize);
241915ab236SFabian Frederick blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE);
2421da177e4SLinus Torvalds
24352399b17SChristoph Hellwig /*
24452399b17SChristoph Hellwig * Align block size to block offset.
24552399b17SChristoph Hellwig */
2461da177e4SLinus Torvalds while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
2471da177e4SLinus Torvalds blocksize >>= 1;
2481da177e4SLinus Torvalds
2491da177e4SLinus Torvalds if (sb_set_blocksize(sb, blocksize) != blocksize) {
250d6142673SJoe Perches pr_err("unable to set blocksize to %u!\n", blocksize);
25152399b17SChristoph Hellwig goto out_free_backup_vhdr;
2521da177e4SLinus Torvalds }
2531da177e4SLinus Torvalds
254dd73a01aSChristoph Hellwig sbi->blockoffset =
255dd73a01aSChristoph Hellwig part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
25652399b17SChristoph Hellwig sbi->part_start = part_start;
257dd73a01aSChristoph Hellwig sbi->sect_count = part_size;
258dd73a01aSChristoph Hellwig sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
2591da177e4SLinus Torvalds return 0;
26052399b17SChristoph Hellwig
26152399b17SChristoph Hellwig out_free_backup_vhdr:
262f588c960SSeth Forshee kfree(sbi->s_backup_vhdr_buf);
26352399b17SChristoph Hellwig out_free_vhdr:
264f588c960SSeth Forshee kfree(sbi->s_vhdr_buf);
26552399b17SChristoph Hellwig out:
26652399b17SChristoph Hellwig return error;
2671da177e4SLinus Torvalds }
268