wrapper.c 4.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 *  linux/fs/hfsplus/wrapper.c
 *
 * Copyright (C) 2001
 * Brad Boyer (flar@allandria.com)
 * (C) 2003 Ardis Technologies <roman@ardistech.com>
 *
 * Handling of HFS wrappers around HFS+ volumes
 */

#include <linux/fs.h>
#include <linux/blkdev.h>
#include <linux/cdrom.h>
#include <linux/genhd.h>
#include <asm/unaligned.h>

#include "hfsplus_fs.h"
#include "hfsplus_raw.h"

struct hfsplus_wd {
	u32 ablk_size;
	u16 ablk_start;
	u16 embed_start;
	u16 embed_count;
};

static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
{
	u32 extent;
	u16 attrib;
D
David Elliott 已提交
31
	__be16 sig;
L
Linus Torvalds 已提交
32

D
David Elliott 已提交
33 34 35
	sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
	if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
	    sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
L
Linus Torvalds 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49
		return 0;

	attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
	if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
	   !(attrib & HFSP_WRAP_ATTRIB_SPARED))
		return 0;

	wd->ablk_size = be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
	if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
		return 0;
	if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
		return 0;
	wd->ablk_start = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));

50
	extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	wd->embed_start = (extent >> 16) & 0xFFFF;
	wd->embed_count = extent & 0xFFFF;

	return 1;
}

static int hfsplus_get_last_session(struct super_block *sb,
				    sector_t *start, sector_t *size)
{
	struct cdrom_multisession ms_info;
	struct cdrom_tocentry te;
	int res;

	/* default values */
	*start = 0;
	*size = sb->s_bdev->bd_inode->i_size >> 9;

68 69
	if (HFSPLUS_SB(sb)->session >= 0) {
		te.cdte_track = HFSPLUS_SB(sb)->session;
L
Linus Torvalds 已提交
70 71 72 73 74 75
		te.cdte_format = CDROM_LBA;
		res = ioctl_by_bdev(sb->s_bdev, CDROMREADTOCENTRY, (unsigned long)&te);
		if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
			*start = (sector_t)te.cdte_addr.lba << 2;
			return 0;
		}
R
Roman Zippel 已提交
76
		printk(KERN_ERR "hfs: invalid session number or type of track\n");
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89
		return -EINVAL;
	}
	ms_info.addr_format = CDROM_LBA;
	res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION, (unsigned long)&ms_info);
	if (!res && ms_info.xa_flag)
		*start = (sector_t)ms_info.addr.lba << 2;
	return 0;
}

/* Find the volume header and fill in some minimum bits in superblock */
/* Takes in super block, returns true if good data read */
int hfsplus_read_wrapper(struct super_block *sb)
{
90
	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
L
Linus Torvalds 已提交
91 92 93 94 95 96 97 98 99 100 101 102
	struct buffer_head *bh;
	struct hfsplus_vh *vhdr;
	struct hfsplus_wd wd;
	sector_t part_start, part_size;
	u32 blocksize;

	blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
	if (!blocksize)
		return -EINVAL;

	if (hfsplus_get_last_session(sb, &part_start, &part_size))
		return -EINVAL;
103 104 105 106
	if ((u64)part_start + part_size > 0x100000000ULL) {
		pr_err("hfs: volumes larger than 2TB are not supported yet\n");
		return -EINVAL;
	}
L
Linus Torvalds 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	while (1) {
		bh = sb_bread512(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, vhdr);
		if (!bh)
			return -EIO;

		if (vhdr->signature == cpu_to_be16(HFSP_WRAP_MAGIC)) {
			if (!hfsplus_read_mdb(vhdr, &wd))
				goto error;
			wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
			part_start += wd.ablk_start + wd.embed_start * wd.ablk_size;
			part_size = wd.embed_count * wd.ablk_size;
			brelse(bh);
			bh = sb_bread512(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, vhdr);
			if (!bh)
				return -EIO;
		}
		if (vhdr->signature == cpu_to_be16(HFSPLUS_VOLHEAD_SIG))
			break;
D
David Elliott 已提交
125
		if (vhdr->signature == cpu_to_be16(HFSPLUS_VOLHEAD_SIGX)) {
126
			sbi->flags |= HFSPLUS_SB_HFSX;
D
David Elliott 已提交
127 128
			break;
		}
L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
		brelse(bh);

		/* check for a partition block
		 * (should do this only for cdrom/loop though)
		 */
		if (hfs_part_find(sb, &part_start, &part_size))
			return -EINVAL;
	}

	blocksize = be32_to_cpu(vhdr->blocksize);
	brelse(bh);

	/* block size must be at least as large as a sector
	 * and a multiple of 2
	 */
	if (blocksize < HFSPLUS_SECTOR_SIZE ||
	    ((blocksize - 1) & blocksize))
		return -EINVAL;
147 148
	sbi->alloc_blksz = blocksize;
	sbi->alloc_blksz_shift = 0;
L
Linus Torvalds 已提交
149
	while ((blocksize >>= 1) != 0)
150 151
		sbi->alloc_blksz_shift++;
	blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE);
L
Linus Torvalds 已提交
152 153 154 155 156 157

	/* align block size to block offset */
	while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
		blocksize >>= 1;

	if (sb_set_blocksize(sb, blocksize) != blocksize) {
R
Roman Zippel 已提交
158
		printk(KERN_ERR "hfs: unable to set blocksize to %u!\n", blocksize);
L
Linus Torvalds 已提交
159 160 161
		return -EINVAL;
	}

162 163 164 165
	sbi->blockoffset =
		part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
	sbi->sect_count = part_size;
	sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
L
Linus Torvalds 已提交
166 167 168 169 170 171

	bh = sb_bread512(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, vhdr);
	if (!bh)
		return -EIO;

	/* should still be the same... */
172
	if (vhdr->signature != (sbi->flags & HFSPLUS_SB_HFSX ?
D
David Elliott 已提交
173 174
				cpu_to_be16(HFSPLUS_VOLHEAD_SIGX) :
				cpu_to_be16(HFSPLUS_VOLHEAD_SIG)))
L
Linus Torvalds 已提交
175
		goto error;
176 177
	sbi->s_vhbh = bh;
	sbi->s_vhdr = vhdr;
L
Linus Torvalds 已提交
178 179 180 181 182 183

	return 0;
 error:
	brelse(bh);
	return -EINVAL;
}