wrapper.c 6.9 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
/*
 *  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
/**
 * hfsplus_submit_bio - Perform block I/O
29 30 31 32
 * @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
M
Mike Christie 已提交
33 34
 * @op: direction of I/O
 * @op_flags: request op flags
35 36 37 38 39 40 41 42 43 44 45 46 47
 *
 * 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,
M
Mike Christie 已提交
48
		       void *buf, void **data, int op, int op_flags)
49 50
{
	struct bio *bio;
51
	int ret = 0;
52
	u64 io_size;
53 54 55 56 57 58 59 60 61 62 63 64
	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);
65 66

	bio = bio_alloc(GFP_NOIO, 1);
67
	bio->bi_iter.bi_sector = sector;
68
	bio->bi_bdev = sb->s_bdev;
M
Mike Christie 已提交
69
	bio_set_op_attrs(bio, op, op_flags);
70

M
Mike Christie 已提交
71
	if (op != WRITE && data)
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
		*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;
	}
87

88
	ret = submit_bio_wait(bio);
89
out:
90
	bio_put(bio);
91
	return ret < 0 ? ret : 0;
92 93
}

L
Linus Torvalds 已提交
94 95 96 97
static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
{
	u32 extent;
	u16 attrib;
D
David Elliott 已提交
98
	__be16 sig;
L
Linus Torvalds 已提交
99

D
David Elliott 已提交
100 101 102
	sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
	if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
	    sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
L
Linus Torvalds 已提交
103 104 105 106 107 108 109
		return 0;

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

110 111
	wd->ablk_size =
		be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
L
Linus Torvalds 已提交
112 113 114 115
	if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
		return 0;
	if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
		return 0;
116 117
	wd->ablk_start =
		be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
L
Linus Torvalds 已提交
118

119
	extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
L
Linus Torvalds 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	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;
135
	*size = i_size_read(sb->s_bdev->bd_inode) >> 9;
L
Linus Torvalds 已提交
136

137 138
	if (HFSPLUS_SB(sb)->session >= 0) {
		te.cdte_track = HFSPLUS_SB(sb)->session;
L
Linus Torvalds 已提交
139
		te.cdte_format = CDROM_LBA;
140 141
		res = ioctl_by_bdev(sb->s_bdev,
			CDROMREADTOCENTRY, (unsigned long)&te);
L
Linus Torvalds 已提交
142 143 144 145
		if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
			*start = (sector_t)te.cdte_addr.lba << 2;
			return 0;
		}
146
		pr_err("invalid session number or type of track\n");
L
Linus Torvalds 已提交
147 148 149
		return -EINVAL;
	}
	ms_info.addr_format = CDROM_LBA;
150 151
	res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION,
		(unsigned long)&ms_info);
L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159 160
	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)
{
161
	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
L
Linus Torvalds 已提交
162 163 164
	struct hfsplus_wd wd;
	sector_t part_start, part_size;
	u32 blocksize;
165
	int error = 0;
L
Linus Torvalds 已提交
166

167
	error = -EINVAL;
L
Linus Torvalds 已提交
168 169
	blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
	if (!blocksize)
170
		goto out;
L
Linus Torvalds 已提交
171 172

	if (hfsplus_get_last_session(sb, &part_start, &part_size))
173
		goto out;
L
Linus Torvalds 已提交
174

175
	error = -ENOMEM;
176 177
	sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
	if (!sbi->s_vhdr_buf)
178
		goto out;
179 180
	sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
	if (!sbi->s_backup_vhdr_buf)
181 182 183
		goto out_free_vhdr;

reread:
184 185
	error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
				   sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
M
Mike Christie 已提交
186
				   REQ_OP_READ, 0);
187 188 189 190 191 192 193 194 195 196 197 198
	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))
199
			goto out_free_backup_vhdr;
200
		wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
201 202 203
		part_start += (sector_t)wd.ablk_start +
			       (sector_t)wd.embed_start * wd.ablk_size;
		part_size = (sector_t)wd.embed_count * wd.ablk_size;
204 205 206 207 208
		goto reread;
	default:
		/*
		 * Check for a partition block.
		 *
L
Linus Torvalds 已提交
209 210 211
		 * (should do this only for cdrom/loop though)
		 */
		if (hfs_part_find(sb, &part_start, &part_size))
212
			goto out_free_backup_vhdr;
213 214 215
		goto reread;
	}

216 217
	error = hfsplus_submit_bio(sb, part_start + part_size - 2,
				   sbi->s_backup_vhdr_buf,
M
Mike Christie 已提交
218 219
				   (void **)&sbi->s_backup_vhdr, REQ_OP_READ,
				   0);
220 221 222 223 224
	if (error)
		goto out_free_backup_vhdr;

	error = -EINVAL;
	if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
225
		pr_warn("invalid secondary volume header\n");
226
		goto out_free_backup_vhdr;
L
Linus Torvalds 已提交
227 228
	}

229
	blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
L
Linus Torvalds 已提交
230

231 232
	/*
	 * Block size must be at least as large as a sector and a multiple of 2.
L
Linus Torvalds 已提交
233
	 */
234 235
	if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
		goto out_free_backup_vhdr;
236
	sbi->alloc_blksz = blocksize;
237
	sbi->alloc_blksz_shift = ilog2(blocksize);
238
	blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE);
L
Linus Torvalds 已提交
239

240 241 242
	/*
	 * Align block size to block offset.
	 */
L
Linus Torvalds 已提交
243 244 245 246
	while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
		blocksize >>= 1;

	if (sb_set_blocksize(sb, blocksize) != blocksize) {
247
		pr_err("unable to set blocksize to %u!\n", blocksize);
248
		goto out_free_backup_vhdr;
L
Linus Torvalds 已提交
249 250
	}

251 252
	sbi->blockoffset =
		part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
253
	sbi->part_start = part_start;
254 255
	sbi->sect_count = part_size;
	sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
L
Linus Torvalds 已提交
256
	return 0;
257 258

out_free_backup_vhdr:
259
	kfree(sbi->s_backup_vhdr_buf);
260
out_free_vhdr:
261
	kfree(sbi->s_vhdr_buf);
262 263
out:
	return error;
L
Linus Torvalds 已提交
264
}