wrapper.c 7.31 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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
/*
 *  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;
};

27 28 29 30 31 32 33
static void hfsplus_end_io_sync(struct bio *bio, int err)
{
	if (err)
		clear_bit(BIO_UPTODATE, &bio->bi_flags);
	complete(bio->bi_private);
}

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/*
 * hfsplus_submit_bio - Perfrom block I/O
 * @sb: super block of volume for I/O
 * @sector: block to read or write, for blocks of HFSPLUS_SECTOR_SIZE bytes
 * @buf: buffer for I/O
 * @data: output pointer for location of requested data
 * @rw: direction of I/O
 *
 * The unit of I/O is hfsplus_min_io_size(sb), which may be bigger than
 * HFSPLUS_SECTOR_SIZE, and @buf must be sized accordingly. On reads
 * @data will return a pointer to the start of the requested sector,
 * which may not be the same location as @buf.
 *
 * If @sector is not aligned to the bdev logical block size it will
 * be rounded down. For writes this means that @buf should contain data
 * that starts at the rounded-down address. As long as the data was
 * read using hfsplus_submit_bio() and the same buffer is used things
 * will work correctly.
 */
int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
		void *buf, void **data, int rw)
55 56 57
{
	DECLARE_COMPLETION_ONSTACK(wait);
	struct bio *bio;
58
	int ret = 0;
59
	u64 io_size;
60 61 62 63 64 65 66 67 68 69 70 71
	loff_t start;
	int offset;

	/*
	 * Align sector to hardware sector size and find offset. We
	 * assume that io_size is a power of two, which _should_
	 * be true.
	 */
	io_size = hfsplus_min_io_size(sb);
	start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT;
	offset = start & (io_size - 1);
	sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1);
72 73 74

	bio = bio_alloc(GFP_NOIO, 1);
	bio->bi_sector = sector;
75
	bio->bi_bdev = sb->s_bdev;
76 77 78
	bio->bi_end_io = hfsplus_end_io_sync;
	bio->bi_private = &wait;

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	if (!(rw & WRITE) && data)
		*data = (u8 *)buf + offset;

	while (io_size > 0) {
		unsigned int page_offset = offset_in_page(buf);
		unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset,
					 io_size);

		ret = bio_add_page(bio, virt_to_page(buf), len, page_offset);
		if (ret != len) {
			ret = -EIO;
			goto out;
		}
		io_size -= len;
		buf = (u8 *)buf + len;
	}
95 96 97 98 99

	submit_bio(rw, bio);
	wait_for_completion(&wait);

	if (!bio_flagged(bio, BIO_UPTODATE))
100 101
		ret = -EIO;

102
out:
103
	bio_put(bio);
104
	return ret < 0 ? ret : 0;
105 106
}

Linus Torvalds's avatar
Linus Torvalds committed
107 108 109 110
static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
{
	u32 extent;
	u16 attrib;
111
	__be16 sig;
Linus Torvalds's avatar
Linus Torvalds committed
112

113 114 115
	sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
	if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
	    sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
Linus Torvalds's avatar
Linus Torvalds committed
116 117 118 119 120 121 122
		return 0;

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

123 124
	wd->ablk_size =
		be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
Linus Torvalds's avatar
Linus Torvalds committed
125 126 127 128
	if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
		return 0;
	if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
		return 0;
129 130
	wd->ablk_start =
		be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
Linus Torvalds's avatar
Linus Torvalds committed
131

132
	extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
Linus Torvalds's avatar
Linus Torvalds committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	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;

150 151
	if (HFSPLUS_SB(sb)->session >= 0) {
		te.cdte_track = HFSPLUS_SB(sb)->session;
Linus Torvalds's avatar
Linus Torvalds committed
152
		te.cdte_format = CDROM_LBA;
153 154
		res = ioctl_by_bdev(sb->s_bdev,
			CDROMREADTOCENTRY, (unsigned long)&te);
Linus Torvalds's avatar
Linus Torvalds committed
155 156 157 158
		if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
			*start = (sector_t)te.cdte_addr.lba << 2;
			return 0;
		}
159
		printk(KERN_ERR "hfs: invalid session number or type of track\n");
Linus Torvalds's avatar
Linus Torvalds committed
160 161 162
		return -EINVAL;
	}
	ms_info.addr_format = CDROM_LBA;
163 164
	res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION,
		(unsigned long)&ms_info);
Linus Torvalds's avatar
Linus Torvalds committed
165 166 167 168 169 170 171 172 173
	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)
{
174
	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
Linus Torvalds's avatar
Linus Torvalds committed
175 176 177
	struct hfsplus_wd wd;
	sector_t part_start, part_size;
	u32 blocksize;
178
	int error = 0;
Linus Torvalds's avatar
Linus Torvalds committed
179

180
	error = -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
181 182
	blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
	if (!blocksize)
183
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
184 185

	if (hfsplus_get_last_session(sb, &part_start, &part_size))
186
		goto out;
187 188
	if ((u64)part_start + part_size > 0x100000000ULL) {
		pr_err("hfs: volumes larger than 2TB are not supported yet\n");
189
		goto out;
190
	}
Linus Torvalds's avatar
Linus Torvalds committed
191

192
	error = -ENOMEM;
193 194
	sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
	if (!sbi->s_vhdr_buf)
195
		goto out;
196 197
	sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
	if (!sbi->s_backup_vhdr_buf)
198 199 200
		goto out_free_vhdr;

reread:
201 202 203
	error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
				   sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
				   READ);
204 205 206 207 208 209 210 211 212 213 214 215
	if (error)
		goto out_free_backup_vhdr;

	error = -EINVAL;
	switch (sbi->s_vhdr->signature) {
	case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
		set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
		/*FALLTHRU*/
	case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
		break;
	case cpu_to_be16(HFSP_WRAP_MAGIC):
		if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
216
			goto out_free_backup_vhdr;
217 218 219 220 221 222 223 224
		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;
		goto reread;
	default:
		/*
		 * Check for a partition block.
		 *
Linus Torvalds's avatar
Linus Torvalds committed
225 226 227
		 * (should do this only for cdrom/loop though)
		 */
		if (hfs_part_find(sb, &part_start, &part_size))
228
			goto out_free_backup_vhdr;
229 230 231
		goto reread;
	}

232 233 234
	error = hfsplus_submit_bio(sb, part_start + part_size - 2,
				   sbi->s_backup_vhdr_buf,
				   (void **)&sbi->s_backup_vhdr, READ);
235 236 237 238 239 240 241 242
	if (error)
		goto out_free_backup_vhdr;

	error = -EINVAL;
	if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
		printk(KERN_WARNING
			"hfs: invalid secondary volume header\n");
		goto out_free_backup_vhdr;
Linus Torvalds's avatar
Linus Torvalds committed
243 244
	}

245
	blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
Linus Torvalds's avatar
Linus Torvalds committed
246

247 248
	/*
	 * Block size must be at least as large as a sector and a multiple of 2.
Linus Torvalds's avatar
Linus Torvalds committed
249
	 */
250 251
	if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
		goto out_free_backup_vhdr;
252 253
	sbi->alloc_blksz = blocksize;
	sbi->alloc_blksz_shift = 0;
Linus Torvalds's avatar
Linus Torvalds committed
254
	while ((blocksize >>= 1) != 0)
255 256
		sbi->alloc_blksz_shift++;
	blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE);
Linus Torvalds's avatar
Linus Torvalds committed
257

258 259 260
	/*
	 * Align block size to block offset.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
261 262 263 264
	while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
		blocksize >>= 1;

	if (sb_set_blocksize(sb, blocksize) != blocksize) {
265 266
		printk(KERN_ERR "hfs: unable to set blocksize to %u!\n",
			blocksize);
267
		goto out_free_backup_vhdr;
Linus Torvalds's avatar
Linus Torvalds committed
268 269
	}

270 271
	sbi->blockoffset =
		part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
272
	sbi->part_start = part_start;
273 274
	sbi->sect_count = part_size;
	sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
Linus Torvalds's avatar
Linus Torvalds committed
275
	return 0;
276 277

out_free_backup_vhdr:
278
	kfree(sbi->s_backup_vhdr_buf);
279
out_free_vhdr:
280
	kfree(sbi->s_vhdr_buf);
281 282
out:
	return error;
Linus Torvalds's avatar
Linus Torvalds committed
283
}