File: wrapper.c

package info (click to toggle)
kernel-source-2.4.27 2.4.27-10sarge5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 191,224 kB
  • ctags: 610,077
  • sloc: ansic: 3,299,602; asm: 164,708; makefile: 10,962; sh: 3,725; perl: 2,273; yacc: 1,651; cpp: 820; lex: 752; tcl: 577; awk: 251; lisp: 218; sed: 79
file content (149 lines) | stat: -rw-r--r-- 4,110 bytes parent folder | download
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 *  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/version.h>
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
#include <linux/buffer_head.h>
#endif
#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(unsigned char *bufptr, struct hfsplus_wd *wd)
{
	u32 extent;
	u16 attrib;

	if (be16_to_cpu(*(u16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG)) != HFSPLUS_VOLHEAD_SIG)
		return 0;

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

	wd->ablk_size = be32_to_cpu(*(u32 *)(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(*(u16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));

	extent = be32_to_cpu(get_unaligned((u32 *)(bufptr + HFSP_WRAPOFF_EMBEDEXT)));
	wd->embed_start = (extent >> 16) & 0xFFFF;
	wd->embed_count = extent & 0xFFFF;

	return 1;
}

/* 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)
{
	struct buffer_head *bh;
	struct hfsplus_vh *vhdr;
	char *bufptr;
	unsigned long block, offset, vhsect;
	struct hfsplus_wd wd;
	u32 blocksize, blockoffset;
	u16 sig;

	blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
	if (!blocksize) {
		printk("HFS+-fs: unable to configure block size\n");
		return -EINVAL;
	}

	block = (HFSPLUS_VOLHEAD_SECTOR * HFSPLUS_SECTOR_SIZE) / blocksize;
	offset = (HFSPLUS_VOLHEAD_SECTOR * HFSPLUS_SECTOR_SIZE) % blocksize;

	bh = sb_bread(sb, block);
	if (!bh) {
		printk("HFS+-fs: unable to read VHDR or MDB\n");
		return -EIO;
	}

	bufptr = bh->b_data + offset;
	sig = be16_to_cpu(*(u16 *)(bufptr + HFSP_WRAPOFF_SIG));
	if (sig == HFSP_WRAP_MAGIC) {
		if (!hfsplus_read_mdb(bufptr, &wd))
			goto error;
		vhsect = (wd.ablk_start + wd.embed_start * (wd.ablk_size >> 9))
			+ HFSPLUS_VOLHEAD_SECTOR;
		block = (vhsect * HFSPLUS_SECTOR_SIZE) / blocksize;
		offset = (vhsect * HFSPLUS_SECTOR_SIZE) % blocksize;
		brelse(bh);
		bh = sb_bread(sb, block);
		if (!bh) {
			printk("HFS+-fs: unable to read VHDR\n");
			return -EIO;
		}
		HFSPLUS_SB(sb).sect_count = wd.embed_count * (wd.ablk_size >> 9);
	} else {
		wd.ablk_start = 0;
		wd.ablk_size = blocksize;
		wd.embed_start = 0;
		HFSPLUS_SB(sb).sect_count = sb->s_bdev->bd_inode->i_size >> 9;
	}
	vhdr = (struct hfsplus_vh *)(bh->b_data + offset);
	if (be16_to_cpu(vhdr->signature) != HFSPLUS_VOLHEAD_SIG)
		goto error;
	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;

	/* block offset must be a multiple of the block size */
	blockoffset = wd.ablk_start + wd.embed_start * (wd.ablk_size >> 9);
	if (blockoffset % (blocksize / HFSPLUS_SECTOR_SIZE)) {
		printk("HFS+-fs: embedded blocks not aligned with wrapper\n");
		return -EINVAL;
	}
	blockoffset /= blocksize / HFSPLUS_SECTOR_SIZE;
	HFSPLUS_SB(sb).blockoffset = blockoffset;

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

	block = blockoffset + HFSPLUS_VOLHEAD_SECTOR /
		(blocksize / HFSPLUS_SECTOR_SIZE);
	offset = (HFSPLUS_VOLHEAD_SECTOR * HFSPLUS_SECTOR_SIZE) % blocksize;
	bh = sb_bread(sb, block);
	if (!bh) {
		printk("HFS+-fs: unable to read VHDR or MDB\n");
		return -EIO;
	}
	vhdr = (struct hfsplus_vh *)(bh->b_data + offset);
	/* should still be the same... */
	if (be16_to_cpu(vhdr->signature) != HFSPLUS_VOLHEAD_SIG)
		goto error;
	HFSPLUS_SB(sb).s_vhbh = bh;
	HFSPLUS_SB(sb).s_vhdr = vhdr;

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