xref: /openbmc/linux/fs/hfsplus/wrapper.c (revision f252fa33)
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 <linux/genhd.h>
161da177e4SLinus Torvalds #include <asm/unaligned.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include "hfsplus_fs.h"
191da177e4SLinus Torvalds #include "hfsplus_raw.h"
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds struct hfsplus_wd {
221da177e4SLinus Torvalds 	u32 ablk_size;
231da177e4SLinus Torvalds 	u16 ablk_start;
241da177e4SLinus Torvalds 	u16 embed_start;
251da177e4SLinus Torvalds 	u16 embed_count;
261da177e4SLinus Torvalds };
271da177e4SLinus Torvalds 
28915ab236SFabian Frederick /**
29915ab236SFabian Frederick  * hfsplus_submit_bio - Perform block I/O
306596528eSSeth Forshee  * @sb: super block of volume for I/O
316596528eSSeth Forshee  * @sector: block to read or write, for blocks of HFSPLUS_SECTOR_SIZE bytes
326596528eSSeth Forshee  * @buf: buffer for I/O
336596528eSSeth Forshee  * @data: output pointer for location of requested data
3467ed2596SMike Christie  * @op: direction of I/O
3567ed2596SMike Christie  * @op_flags: request op flags
366596528eSSeth Forshee  *
376596528eSSeth Forshee  * The unit of I/O is hfsplus_min_io_size(sb), which may be bigger than
386596528eSSeth Forshee  * HFSPLUS_SECTOR_SIZE, and @buf must be sized accordingly. On reads
396596528eSSeth Forshee  * @data will return a pointer to the start of the requested sector,
406596528eSSeth Forshee  * which may not be the same location as @buf.
416596528eSSeth Forshee  *
426596528eSSeth Forshee  * If @sector is not aligned to the bdev logical block size it will
436596528eSSeth Forshee  * be rounded down. For writes this means that @buf should contain data
446596528eSSeth Forshee  * that starts at the rounded-down address. As long as the data was
456596528eSSeth Forshee  * read using hfsplus_submit_bio() and the same buffer is used things
466596528eSSeth Forshee  * will work correctly.
476596528eSSeth Forshee  */
486596528eSSeth Forshee int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
4967ed2596SMike Christie 		       void *buf, void **data, int op, int op_flags)
5052399b17SChristoph Hellwig {
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 
6752399b17SChristoph Hellwig 	bio = bio_alloc(GFP_NOIO, 1);
684f024f37SKent Overstreet 	bio->bi_iter.bi_sector = sector;
6974d46992SChristoph Hellwig 	bio_set_dev(bio, sb->s_bdev);
7067ed2596SMike Christie 	bio_set_op_attrs(bio, op, op_flags);
7152399b17SChristoph Hellwig 
7267ed2596SMike Christie 	if (op != WRITE && data)
736596528eSSeth Forshee 		*data = (u8 *)buf + offset;
746596528eSSeth Forshee 
756596528eSSeth Forshee 	while (io_size > 0) {
766596528eSSeth Forshee 		unsigned int page_offset = offset_in_page(buf);
776596528eSSeth Forshee 		unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset,
786596528eSSeth Forshee 					 io_size);
796596528eSSeth Forshee 
806596528eSSeth Forshee 		ret = bio_add_page(bio, virt_to_page(buf), len, page_offset);
816596528eSSeth Forshee 		if (ret != len) {
826596528eSSeth Forshee 			ret = -EIO;
836596528eSSeth Forshee 			goto out;
846596528eSSeth Forshee 		}
856596528eSSeth Forshee 		io_size -= len;
866596528eSSeth Forshee 		buf = (u8 *)buf + len;
876596528eSSeth Forshee 	}
8852399b17SChristoph Hellwig 
894e49ea4aSMike Christie 	ret = submit_bio_wait(bio);
906596528eSSeth Forshee out:
9150176ddeSSeth Forshee 	bio_put(bio);
926596528eSSeth Forshee 	return ret < 0 ? ret : 0;
9352399b17SChristoph Hellwig }
9452399b17SChristoph Hellwig 
951da177e4SLinus Torvalds static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
961da177e4SLinus Torvalds {
971da177e4SLinus Torvalds 	u32 extent;
981da177e4SLinus Torvalds 	u16 attrib;
992179d372SDavid Elliott 	__be16 sig;
1001da177e4SLinus Torvalds 
1012179d372SDavid Elliott 	sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
1022179d372SDavid Elliott 	if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
1032179d372SDavid Elliott 	    sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
1041da177e4SLinus Torvalds 		return 0;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
1071da177e4SLinus Torvalds 	if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
1081da177e4SLinus Torvalds 	   !(attrib & HFSP_WRAP_ATTRIB_SPARED))
1091da177e4SLinus Torvalds 		return 0;
1101da177e4SLinus Torvalds 
1112753cc28SAnton Salikhmetov 	wd->ablk_size =
1122753cc28SAnton Salikhmetov 		be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
1131da177e4SLinus Torvalds 	if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
1141da177e4SLinus Torvalds 		return 0;
1151da177e4SLinus Torvalds 	if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
1161da177e4SLinus Torvalds 		return 0;
1172753cc28SAnton Salikhmetov 	wd->ablk_start =
1182753cc28SAnton Salikhmetov 		be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
1191da177e4SLinus Torvalds 
1208b3789e5SHarvey Harrison 	extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
1211da177e4SLinus Torvalds 	wd->embed_start = (extent >> 16) & 0xFFFF;
1221da177e4SLinus Torvalds 	wd->embed_count = extent & 0xFFFF;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	return 1;
1251da177e4SLinus Torvalds }
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds static int hfsplus_get_last_session(struct super_block *sb,
1281da177e4SLinus Torvalds 				    sector_t *start, sector_t *size)
1291da177e4SLinus Torvalds {
130f252fa33SChristoph Hellwig 	struct cdrom_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk);
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	/* default values */
1331da177e4SLinus Torvalds 	*start = 0;
134a3f22350SFabian Frederick 	*size = i_size_read(sb->s_bdev->bd_inode) >> 9;
1351da177e4SLinus Torvalds 
136dd73a01aSChristoph Hellwig 	if (HFSPLUS_SB(sb)->session >= 0) {
137f252fa33SChristoph Hellwig 		struct cdrom_tocentry te;
138f252fa33SChristoph Hellwig 
139f252fa33SChristoph Hellwig 		if (!cdi)
140f252fa33SChristoph Hellwig 			return -EINVAL;
141f252fa33SChristoph Hellwig 
142dd73a01aSChristoph Hellwig 		te.cdte_track = HFSPLUS_SB(sb)->session;
1431da177e4SLinus Torvalds 		te.cdte_format = CDROM_LBA;
144f252fa33SChristoph Hellwig 		if (cdrom_read_tocentry(cdi, &te) ||
145f252fa33SChristoph Hellwig 		    (te.cdte_ctrl & CDROM_DATA_TRACK) != 4) {
146d6142673SJoe Perches 			pr_err("invalid session number or type of track\n");
1471da177e4SLinus Torvalds 			return -EINVAL;
1481da177e4SLinus Torvalds 		}
149f252fa33SChristoph Hellwig 		*start = (sector_t)te.cdte_addr.lba << 2;
150f252fa33SChristoph Hellwig 	} else if (cdi) {
151f252fa33SChristoph Hellwig 		struct cdrom_multisession ms_info;
152f252fa33SChristoph Hellwig 
1531da177e4SLinus Torvalds 		ms_info.addr_format = CDROM_LBA;
154f252fa33SChristoph Hellwig 		if (cdrom_multisession(cdi, &ms_info) == 0 && ms_info.xa_flag)
1551da177e4SLinus Torvalds 			*start = (sector_t)ms_info.addr.lba << 2;
156f252fa33SChristoph Hellwig 	}
157f252fa33SChristoph Hellwig 
1581da177e4SLinus Torvalds 	return 0;
1591da177e4SLinus Torvalds }
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds /* Find the volume header and fill in some minimum bits in superblock */
1621da177e4SLinus Torvalds /* Takes in super block, returns true if good data read */
1631da177e4SLinus Torvalds int hfsplus_read_wrapper(struct super_block *sb)
1641da177e4SLinus Torvalds {
165dd73a01aSChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
1661da177e4SLinus Torvalds 	struct hfsplus_wd wd;
1671da177e4SLinus Torvalds 	sector_t part_start, part_size;
1681da177e4SLinus Torvalds 	u32 blocksize;
16952399b17SChristoph Hellwig 	int error = 0;
1701da177e4SLinus Torvalds 
17152399b17SChristoph Hellwig 	error = -EINVAL;
1721da177e4SLinus Torvalds 	blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
1731da177e4SLinus Torvalds 	if (!blocksize)
17452399b17SChristoph Hellwig 		goto out;
1751da177e4SLinus Torvalds 
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,
19067ed2596SMike Christie 				   REQ_OP_READ, 0);
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);
19852399b17SChristoph Hellwig 		/*FALLTHRU*/
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,
22267ed2596SMike Christie 				   (void **)&sbi->s_backup_vhdr, REQ_OP_READ,
22367ed2596SMike Christie 				   0);
22452399b17SChristoph Hellwig 	if (error)
22552399b17SChristoph Hellwig 		goto out_free_backup_vhdr;
2261da177e4SLinus Torvalds 
22752399b17SChristoph Hellwig 	error = -EINVAL;
22852399b17SChristoph Hellwig 	if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
229d6142673SJoe Perches 		pr_warn("invalid secondary volume header\n");
23052399b17SChristoph Hellwig 		goto out_free_backup_vhdr;
23152399b17SChristoph Hellwig 	}
23252399b17SChristoph Hellwig 
23352399b17SChristoph Hellwig 	blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
23452399b17SChristoph Hellwig 
23552399b17SChristoph Hellwig 	/*
23652399b17SChristoph Hellwig 	 * Block size must be at least as large as a sector and a multiple of 2.
2371da177e4SLinus Torvalds 	 */
23852399b17SChristoph Hellwig 	if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
23952399b17SChristoph Hellwig 		goto out_free_backup_vhdr;
240dd73a01aSChristoph Hellwig 	sbi->alloc_blksz = blocksize;
241297cc272SFabian Frederick 	sbi->alloc_blksz_shift = ilog2(blocksize);
242915ab236SFabian Frederick 	blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE);
2431da177e4SLinus Torvalds 
24452399b17SChristoph Hellwig 	/*
24552399b17SChristoph Hellwig 	 * Align block size to block offset.
24652399b17SChristoph Hellwig 	 */
2471da177e4SLinus Torvalds 	while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
2481da177e4SLinus Torvalds 		blocksize >>= 1;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	if (sb_set_blocksize(sb, blocksize) != blocksize) {
251d6142673SJoe Perches 		pr_err("unable to set blocksize to %u!\n", blocksize);
25252399b17SChristoph Hellwig 		goto out_free_backup_vhdr;
2531da177e4SLinus Torvalds 	}
2541da177e4SLinus Torvalds 
255dd73a01aSChristoph Hellwig 	sbi->blockoffset =
256dd73a01aSChristoph Hellwig 		part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
25752399b17SChristoph Hellwig 	sbi->part_start = part_start;
258dd73a01aSChristoph Hellwig 	sbi->sect_count = part_size;
259dd73a01aSChristoph Hellwig 	sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
2601da177e4SLinus Torvalds 	return 0;
26152399b17SChristoph Hellwig 
26252399b17SChristoph Hellwig out_free_backup_vhdr:
263f588c960SSeth Forshee 	kfree(sbi->s_backup_vhdr_buf);
26452399b17SChristoph Hellwig out_free_vhdr:
265f588c960SSeth Forshee 	kfree(sbi->s_vhdr_buf);
26652399b17SChristoph Hellwig out:
26752399b17SChristoph Hellwig 	return error;
2681da177e4SLinus Torvalds }
269