xref: /openbmc/linux/fs/hfsplus/wrapper.c (revision 6596528e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/fs/hfsplus/wrapper.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2001
51da177e4SLinus Torvalds  * Brad Boyer (flar@allandria.com)
61da177e4SLinus Torvalds  * (C) 2003 Ardis Technologies <roman@ardistech.com>
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Handling of HFS wrappers around HFS+ volumes
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/fs.h>
121da177e4SLinus Torvalds #include <linux/blkdev.h>
131da177e4SLinus Torvalds #include <linux/cdrom.h>
141da177e4SLinus Torvalds #include <linux/genhd.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 
2752399b17SChristoph Hellwig static void hfsplus_end_io_sync(struct bio *bio, int err)
2852399b17SChristoph Hellwig {
2952399b17SChristoph Hellwig 	if (err)
3052399b17SChristoph Hellwig 		clear_bit(BIO_UPTODATE, &bio->bi_flags);
3152399b17SChristoph Hellwig 	complete(bio->bi_private);
3252399b17SChristoph Hellwig }
3352399b17SChristoph Hellwig 
346596528eSSeth Forshee /*
356596528eSSeth Forshee  * hfsplus_submit_bio - Perfrom block I/O
366596528eSSeth Forshee  * @sb: super block of volume for I/O
376596528eSSeth Forshee  * @sector: block to read or write, for blocks of HFSPLUS_SECTOR_SIZE bytes
386596528eSSeth Forshee  * @buf: buffer for I/O
396596528eSSeth Forshee  * @data: output pointer for location of requested data
406596528eSSeth Forshee  * @rw: direction of I/O
416596528eSSeth Forshee  *
426596528eSSeth Forshee  * The unit of I/O is hfsplus_min_io_size(sb), which may be bigger than
436596528eSSeth Forshee  * HFSPLUS_SECTOR_SIZE, and @buf must be sized accordingly. On reads
446596528eSSeth Forshee  * @data will return a pointer to the start of the requested sector,
456596528eSSeth Forshee  * which may not be the same location as @buf.
466596528eSSeth Forshee  *
476596528eSSeth Forshee  * If @sector is not aligned to the bdev logical block size it will
486596528eSSeth Forshee  * be rounded down. For writes this means that @buf should contain data
496596528eSSeth Forshee  * that starts at the rounded-down address. As long as the data was
506596528eSSeth Forshee  * read using hfsplus_submit_bio() and the same buffer is used things
516596528eSSeth Forshee  * will work correctly.
526596528eSSeth Forshee  */
536596528eSSeth Forshee int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
546596528eSSeth Forshee 		void *buf, void **data, int rw)
5552399b17SChristoph Hellwig {
5652399b17SChristoph Hellwig 	DECLARE_COMPLETION_ONSTACK(wait);
5752399b17SChristoph Hellwig 	struct bio *bio;
5850176ddeSSeth Forshee 	int ret = 0;
596596528eSSeth Forshee 	unsigned int io_size;
606596528eSSeth Forshee 	loff_t start;
616596528eSSeth Forshee 	int offset;
626596528eSSeth Forshee 
636596528eSSeth Forshee 	/*
646596528eSSeth Forshee 	 * Align sector to hardware sector size and find offset. We
656596528eSSeth Forshee 	 * assume that io_size is a power of two, which _should_
666596528eSSeth Forshee 	 * be true.
676596528eSSeth Forshee 	 */
686596528eSSeth Forshee 	io_size = hfsplus_min_io_size(sb);
696596528eSSeth Forshee 	start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT;
706596528eSSeth Forshee 	offset = start & (io_size - 1);
716596528eSSeth Forshee 	sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1);
7252399b17SChristoph Hellwig 
7352399b17SChristoph Hellwig 	bio = bio_alloc(GFP_NOIO, 1);
7452399b17SChristoph Hellwig 	bio->bi_sector = sector;
756596528eSSeth Forshee 	bio->bi_bdev = sb->s_bdev;
7652399b17SChristoph Hellwig 	bio->bi_end_io = hfsplus_end_io_sync;
7752399b17SChristoph Hellwig 	bio->bi_private = &wait;
7852399b17SChristoph Hellwig 
796596528eSSeth Forshee 	if (!(rw & WRITE) && data)
806596528eSSeth Forshee 		*data = (u8 *)buf + offset;
816596528eSSeth Forshee 
826596528eSSeth Forshee 	while (io_size > 0) {
836596528eSSeth Forshee 		unsigned int page_offset = offset_in_page(buf);
846596528eSSeth Forshee 		unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset,
856596528eSSeth Forshee 					 io_size);
866596528eSSeth Forshee 
876596528eSSeth Forshee 		ret = bio_add_page(bio, virt_to_page(buf), len, page_offset);
886596528eSSeth Forshee 		if (ret != len) {
896596528eSSeth Forshee 			ret = -EIO;
906596528eSSeth Forshee 			goto out;
916596528eSSeth Forshee 		}
926596528eSSeth Forshee 		io_size -= len;
936596528eSSeth Forshee 		buf = (u8 *)buf + len;
946596528eSSeth Forshee 	}
9552399b17SChristoph Hellwig 
9652399b17SChristoph Hellwig 	submit_bio(rw, bio);
9752399b17SChristoph Hellwig 	wait_for_completion(&wait);
9852399b17SChristoph Hellwig 
9952399b17SChristoph Hellwig 	if (!bio_flagged(bio, BIO_UPTODATE))
10050176ddeSSeth Forshee 		ret = -EIO;
10150176ddeSSeth Forshee 
1026596528eSSeth Forshee out:
10350176ddeSSeth Forshee 	bio_put(bio);
1046596528eSSeth Forshee 	return ret < 0 ? ret : 0;
10552399b17SChristoph Hellwig }
10652399b17SChristoph Hellwig 
1071da177e4SLinus Torvalds static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds 	u32 extent;
1101da177e4SLinus Torvalds 	u16 attrib;
1112179d372SDavid Elliott 	__be16 sig;
1121da177e4SLinus Torvalds 
1132179d372SDavid Elliott 	sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
1142179d372SDavid Elliott 	if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
1152179d372SDavid Elliott 	    sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
1161da177e4SLinus Torvalds 		return 0;
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
1191da177e4SLinus Torvalds 	if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
1201da177e4SLinus Torvalds 	   !(attrib & HFSP_WRAP_ATTRIB_SPARED))
1211da177e4SLinus Torvalds 		return 0;
1221da177e4SLinus Torvalds 
1232753cc28SAnton Salikhmetov 	wd->ablk_size =
1242753cc28SAnton Salikhmetov 		be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
1251da177e4SLinus Torvalds 	if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
1261da177e4SLinus Torvalds 		return 0;
1271da177e4SLinus Torvalds 	if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
1281da177e4SLinus Torvalds 		return 0;
1292753cc28SAnton Salikhmetov 	wd->ablk_start =
1302753cc28SAnton Salikhmetov 		be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
1311da177e4SLinus Torvalds 
1328b3789e5SHarvey Harrison 	extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
1331da177e4SLinus Torvalds 	wd->embed_start = (extent >> 16) & 0xFFFF;
1341da177e4SLinus Torvalds 	wd->embed_count = extent & 0xFFFF;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	return 1;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds static int hfsplus_get_last_session(struct super_block *sb,
1401da177e4SLinus Torvalds 				    sector_t *start, sector_t *size)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	struct cdrom_multisession ms_info;
1431da177e4SLinus Torvalds 	struct cdrom_tocentry te;
1441da177e4SLinus Torvalds 	int res;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	/* default values */
1471da177e4SLinus Torvalds 	*start = 0;
1481da177e4SLinus Torvalds 	*size = sb->s_bdev->bd_inode->i_size >> 9;
1491da177e4SLinus Torvalds 
150dd73a01aSChristoph Hellwig 	if (HFSPLUS_SB(sb)->session >= 0) {
151dd73a01aSChristoph Hellwig 		te.cdte_track = HFSPLUS_SB(sb)->session;
1521da177e4SLinus Torvalds 		te.cdte_format = CDROM_LBA;
1532753cc28SAnton Salikhmetov 		res = ioctl_by_bdev(sb->s_bdev,
1542753cc28SAnton Salikhmetov 			CDROMREADTOCENTRY, (unsigned long)&te);
1551da177e4SLinus Torvalds 		if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
1561da177e4SLinus Torvalds 			*start = (sector_t)te.cdte_addr.lba << 2;
1571da177e4SLinus Torvalds 			return 0;
1581da177e4SLinus Torvalds 		}
159634725a9SRoman Zippel 		printk(KERN_ERR "hfs: invalid session number or type of track\n");
1601da177e4SLinus Torvalds 		return -EINVAL;
1611da177e4SLinus Torvalds 	}
1621da177e4SLinus Torvalds 	ms_info.addr_format = CDROM_LBA;
1632753cc28SAnton Salikhmetov 	res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION,
1642753cc28SAnton Salikhmetov 		(unsigned long)&ms_info);
1651da177e4SLinus Torvalds 	if (!res && ms_info.xa_flag)
1661da177e4SLinus Torvalds 		*start = (sector_t)ms_info.addr.lba << 2;
1671da177e4SLinus Torvalds 	return 0;
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds /* Find the volume header and fill in some minimum bits in superblock */
1711da177e4SLinus Torvalds /* Takes in super block, returns true if good data read */
1721da177e4SLinus Torvalds int hfsplus_read_wrapper(struct super_block *sb)
1731da177e4SLinus Torvalds {
174dd73a01aSChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
1751da177e4SLinus Torvalds 	struct hfsplus_wd wd;
1761da177e4SLinus Torvalds 	sector_t part_start, part_size;
1771da177e4SLinus Torvalds 	u32 blocksize;
17852399b17SChristoph Hellwig 	int error = 0;
1791da177e4SLinus Torvalds 
18052399b17SChristoph Hellwig 	error = -EINVAL;
1811da177e4SLinus Torvalds 	blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
1821da177e4SLinus Torvalds 	if (!blocksize)
18352399b17SChristoph Hellwig 		goto out;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	if (hfsplus_get_last_session(sb, &part_start, &part_size))
18652399b17SChristoph Hellwig 		goto out;
1871da177e4SLinus Torvalds 
18852399b17SChristoph Hellwig 	error = -ENOMEM;
1896596528eSSeth Forshee 	sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
1906596528eSSeth Forshee 	if (!sbi->s_vhdr_buf)
19152399b17SChristoph Hellwig 		goto out;
1926596528eSSeth Forshee 	sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
1936596528eSSeth Forshee 	if (!sbi->s_backup_vhdr_buf)
19452399b17SChristoph Hellwig 		goto out_free_vhdr;
19552399b17SChristoph Hellwig 
19652399b17SChristoph Hellwig reread:
1976596528eSSeth Forshee 	error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
1986596528eSSeth Forshee 				   sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
1996596528eSSeth Forshee 				   READ);
20052399b17SChristoph Hellwig 	if (error)
20152399b17SChristoph Hellwig 		goto out_free_backup_vhdr;
20252399b17SChristoph Hellwig 
20352399b17SChristoph Hellwig 	error = -EINVAL;
20452399b17SChristoph Hellwig 	switch (sbi->s_vhdr->signature) {
20552399b17SChristoph Hellwig 	case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
20652399b17SChristoph Hellwig 		set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
20752399b17SChristoph Hellwig 		/*FALLTHRU*/
20852399b17SChristoph Hellwig 	case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
20952399b17SChristoph Hellwig 		break;
21052399b17SChristoph Hellwig 	case cpu_to_be16(HFSP_WRAP_MAGIC):
21152399b17SChristoph Hellwig 		if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
212a1dbcef0SChuck Ebbert 			goto out_free_backup_vhdr;
2131da177e4SLinus Torvalds 		wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
2144ba2d5fdSChristoph Hellwig 		part_start += (sector_t)wd.ablk_start +
2154ba2d5fdSChristoph Hellwig 			       (sector_t)wd.embed_start * wd.ablk_size;
2164ba2d5fdSChristoph Hellwig 		part_size = (sector_t)wd.embed_count * wd.ablk_size;
21752399b17SChristoph Hellwig 		goto reread;
21852399b17SChristoph Hellwig 	default:
21952399b17SChristoph Hellwig 		/*
22052399b17SChristoph Hellwig 		 * Check for a partition block.
22152399b17SChristoph Hellwig 		 *
2221da177e4SLinus Torvalds 		 * (should do this only for cdrom/loop though)
2231da177e4SLinus Torvalds 		 */
2241da177e4SLinus Torvalds 		if (hfs_part_find(sb, &part_start, &part_size))
225a1dbcef0SChuck Ebbert 			goto out_free_backup_vhdr;
22652399b17SChristoph Hellwig 		goto reread;
2271da177e4SLinus Torvalds 	}
2281da177e4SLinus Torvalds 
2296596528eSSeth Forshee 	error = hfsplus_submit_bio(sb, part_start + part_size - 2,
2306596528eSSeth Forshee 				   sbi->s_backup_vhdr_buf,
2316596528eSSeth Forshee 				   (void **)&sbi->s_backup_vhdr, READ);
23252399b17SChristoph Hellwig 	if (error)
23352399b17SChristoph Hellwig 		goto out_free_backup_vhdr;
2341da177e4SLinus Torvalds 
23552399b17SChristoph Hellwig 	error = -EINVAL;
23652399b17SChristoph Hellwig 	if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
23752399b17SChristoph Hellwig 		printk(KERN_WARNING
23852399b17SChristoph Hellwig 			"hfs: invalid secondary volume header\n");
23952399b17SChristoph Hellwig 		goto out_free_backup_vhdr;
24052399b17SChristoph Hellwig 	}
24152399b17SChristoph Hellwig 
24252399b17SChristoph Hellwig 	blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
24352399b17SChristoph Hellwig 
24452399b17SChristoph Hellwig 	/*
24552399b17SChristoph Hellwig 	 * Block size must be at least as large as a sector and a multiple of 2.
2461da177e4SLinus Torvalds 	 */
24752399b17SChristoph Hellwig 	if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
24852399b17SChristoph Hellwig 		goto out_free_backup_vhdr;
249dd73a01aSChristoph Hellwig 	sbi->alloc_blksz = blocksize;
250dd73a01aSChristoph Hellwig 	sbi->alloc_blksz_shift = 0;
2511da177e4SLinus Torvalds 	while ((blocksize >>= 1) != 0)
252dd73a01aSChristoph Hellwig 		sbi->alloc_blksz_shift++;
253dd73a01aSChristoph Hellwig 	blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE);
2541da177e4SLinus Torvalds 
25552399b17SChristoph Hellwig 	/*
25652399b17SChristoph Hellwig 	 * Align block size to block offset.
25752399b17SChristoph Hellwig 	 */
2581da177e4SLinus Torvalds 	while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
2591da177e4SLinus Torvalds 		blocksize >>= 1;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	if (sb_set_blocksize(sb, blocksize) != blocksize) {
2622753cc28SAnton Salikhmetov 		printk(KERN_ERR "hfs: unable to set blocksize to %u!\n",
2632753cc28SAnton Salikhmetov 			blocksize);
26452399b17SChristoph Hellwig 		goto out_free_backup_vhdr;
2651da177e4SLinus Torvalds 	}
2661da177e4SLinus Torvalds 
267dd73a01aSChristoph Hellwig 	sbi->blockoffset =
268dd73a01aSChristoph Hellwig 		part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
26952399b17SChristoph Hellwig 	sbi->part_start = part_start;
270dd73a01aSChristoph Hellwig 	sbi->sect_count = part_size;
271dd73a01aSChristoph Hellwig 	sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
2721da177e4SLinus Torvalds 	return 0;
27352399b17SChristoph Hellwig 
27452399b17SChristoph Hellwig out_free_backup_vhdr:
27552399b17SChristoph Hellwig 	kfree(sbi->s_backup_vhdr);
27652399b17SChristoph Hellwig out_free_vhdr:
27752399b17SChristoph Hellwig 	kfree(sbi->s_vhdr);
27852399b17SChristoph Hellwig out:
27952399b17SChristoph Hellwig 	return error;
2801da177e4SLinus Torvalds }
281