Linux-2.6.12-rc2
[linux-2.6/kvm.git] / fs / hfsplus / wrapper.c
blob0c51d6338b0b78bd6ea8b611ffa26c20d2096a8a
1 /*
2 * linux/fs/hfsplus/wrapper.c
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handling of HFS wrappers around HFS+ volumes
9 */
11 #include <linux/fs.h>
12 #include <linux/blkdev.h>
13 #include <linux/cdrom.h>
14 #include <linux/genhd.h>
15 #include <linux/version.h>
16 #include <asm/unaligned.h>
18 #include "hfsplus_fs.h"
19 #include "hfsplus_raw.h"
21 struct hfsplus_wd {
22 u32 ablk_size;
23 u16 ablk_start;
24 u16 embed_start;
25 u16 embed_count;
28 static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
30 u32 extent;
31 u16 attrib;
33 if (be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG)) != HFSPLUS_VOLHEAD_SIG)
34 return 0;
36 attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
37 if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
38 !(attrib & HFSP_WRAP_ATTRIB_SPARED))
39 return 0;
41 wd->ablk_size = be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
42 if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
43 return 0;
44 if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
45 return 0;
46 wd->ablk_start = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
48 extent = be32_to_cpu(get_unaligned((__be32 *)(bufptr + HFSP_WRAPOFF_EMBEDEXT)));
49 wd->embed_start = (extent >> 16) & 0xFFFF;
50 wd->embed_count = extent & 0xFFFF;
52 return 1;
55 static int hfsplus_get_last_session(struct super_block *sb,
56 sector_t *start, sector_t *size)
58 struct cdrom_multisession ms_info;
59 struct cdrom_tocentry te;
60 int res;
62 /* default values */
63 *start = 0;
64 *size = sb->s_bdev->bd_inode->i_size >> 9;
66 if (HFSPLUS_SB(sb).session >= 0) {
67 te.cdte_track = HFSPLUS_SB(sb).session;
68 te.cdte_format = CDROM_LBA;
69 res = ioctl_by_bdev(sb->s_bdev, CDROMREADTOCENTRY, (unsigned long)&te);
70 if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
71 *start = (sector_t)te.cdte_addr.lba << 2;
72 return 0;
74 printk(KERN_ERR "HFS: Invalid session number or type of track\n");
75 return -EINVAL;
77 ms_info.addr_format = CDROM_LBA;
78 res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION, (unsigned long)&ms_info);
79 if (!res && ms_info.xa_flag)
80 *start = (sector_t)ms_info.addr.lba << 2;
81 return 0;
84 /* Find the volume header and fill in some minimum bits in superblock */
85 /* Takes in super block, returns true if good data read */
86 int hfsplus_read_wrapper(struct super_block *sb)
88 struct buffer_head *bh;
89 struct hfsplus_vh *vhdr;
90 struct hfsplus_wd wd;
91 sector_t part_start, part_size;
92 u32 blocksize;
94 blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
95 if (!blocksize)
96 return -EINVAL;
98 if (hfsplus_get_last_session(sb, &part_start, &part_size))
99 return -EINVAL;
100 while (1) {
101 bh = sb_bread512(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, vhdr);
102 if (!bh)
103 return -EIO;
105 if (vhdr->signature == cpu_to_be16(HFSP_WRAP_MAGIC)) {
106 if (!hfsplus_read_mdb(vhdr, &wd))
107 goto error;
108 wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
109 part_start += wd.ablk_start + wd.embed_start * wd.ablk_size;
110 part_size = wd.embed_count * wd.ablk_size;
111 brelse(bh);
112 bh = sb_bread512(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, vhdr);
113 if (!bh)
114 return -EIO;
116 if (vhdr->signature == cpu_to_be16(HFSPLUS_VOLHEAD_SIG))
117 break;
118 brelse(bh);
120 /* check for a partition block
121 * (should do this only for cdrom/loop though)
123 if (hfs_part_find(sb, &part_start, &part_size))
124 return -EINVAL;
127 blocksize = be32_to_cpu(vhdr->blocksize);
128 brelse(bh);
130 /* block size must be at least as large as a sector
131 * and a multiple of 2
133 if (blocksize < HFSPLUS_SECTOR_SIZE ||
134 ((blocksize - 1) & blocksize))
135 return -EINVAL;
136 HFSPLUS_SB(sb).alloc_blksz = blocksize;
137 HFSPLUS_SB(sb).alloc_blksz_shift = 0;
138 while ((blocksize >>= 1) != 0)
139 HFSPLUS_SB(sb).alloc_blksz_shift++;
140 blocksize = min(HFSPLUS_SB(sb).alloc_blksz, (u32)PAGE_SIZE);
142 /* align block size to block offset */
143 while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
144 blocksize >>= 1;
146 if (sb_set_blocksize(sb, blocksize) != blocksize) {
147 printk("HFS+: unable to blocksize to %u!\n", blocksize);
148 return -EINVAL;
151 HFSPLUS_SB(sb).blockoffset = part_start >>
152 (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
153 HFSPLUS_SB(sb).sect_count = part_size;
154 HFSPLUS_SB(sb).fs_shift = HFSPLUS_SB(sb).alloc_blksz_shift -
155 sb->s_blocksize_bits;
157 bh = sb_bread512(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, vhdr);
158 if (!bh)
159 return -EIO;
161 /* should still be the same... */
162 if (be16_to_cpu(vhdr->signature) != HFSPLUS_VOLHEAD_SIG)
163 goto error;
164 HFSPLUS_SB(sb).s_vhbh = bh;
165 HFSPLUS_SB(sb).s_vhdr = vhdr;
167 return 0;
168 error:
169 brelse(bh);
170 return -EINVAL;