Add linux-next specific files for 20110716
[linux-2.6/next.git] / fs / hfsplus / wrapper.c
blob2f933e83f5c5f6212842f0a0146a802b7b2a605b
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 <asm/unaligned.h>
17 #include "hfsplus_fs.h"
18 #include "hfsplus_raw.h"
20 struct hfsplus_wd {
21 u32 ablk_size;
22 u16 ablk_start;
23 u16 embed_start;
24 u16 embed_count;
27 static void hfsplus_end_io_sync(struct bio *bio, int err)
29 if (err)
30 clear_bit(BIO_UPTODATE, &bio->bi_flags);
31 complete(bio->bi_private);
34 int hfsplus_submit_bio(struct block_device *bdev, sector_t sector,
35 void *data, int rw)
37 DECLARE_COMPLETION_ONSTACK(wait);
38 struct bio *bio;
39 int ret = 0;
41 bio = bio_alloc(GFP_NOIO, 1);
42 bio->bi_sector = sector;
43 bio->bi_bdev = bdev;
44 bio->bi_end_io = hfsplus_end_io_sync;
45 bio->bi_private = &wait;
48 * We always submit one sector at a time, so bio_add_page must not fail.
50 if (bio_add_page(bio, virt_to_page(data), HFSPLUS_SECTOR_SIZE,
51 offset_in_page(data)) != HFSPLUS_SECTOR_SIZE)
52 BUG();
54 submit_bio(rw, bio);
55 wait_for_completion(&wait);
57 if (!bio_flagged(bio, BIO_UPTODATE))
58 ret = -EIO;
60 bio_put(bio);
61 return ret;
64 static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
66 u32 extent;
67 u16 attrib;
68 __be16 sig;
70 sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
71 if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
72 sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
73 return 0;
75 attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
76 if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
77 !(attrib & HFSP_WRAP_ATTRIB_SPARED))
78 return 0;
80 wd->ablk_size =
81 be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
82 if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
83 return 0;
84 if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
85 return 0;
86 wd->ablk_start =
87 be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
89 extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
90 wd->embed_start = (extent >> 16) & 0xFFFF;
91 wd->embed_count = extent & 0xFFFF;
93 return 1;
96 static int hfsplus_get_last_session(struct super_block *sb,
97 sector_t *start, sector_t *size)
99 struct cdrom_multisession ms_info;
100 struct cdrom_tocentry te;
101 int res;
103 /* default values */
104 *start = 0;
105 *size = sb->s_bdev->bd_inode->i_size >> 9;
107 if (HFSPLUS_SB(sb)->session >= 0) {
108 te.cdte_track = HFSPLUS_SB(sb)->session;
109 te.cdte_format = CDROM_LBA;
110 res = ioctl_by_bdev(sb->s_bdev,
111 CDROMREADTOCENTRY, (unsigned long)&te);
112 if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
113 *start = (sector_t)te.cdte_addr.lba << 2;
114 return 0;
116 printk(KERN_ERR "hfs: invalid session number or type of track\n");
117 return -EINVAL;
119 ms_info.addr_format = CDROM_LBA;
120 res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION,
121 (unsigned long)&ms_info);
122 if (!res && ms_info.xa_flag)
123 *start = (sector_t)ms_info.addr.lba << 2;
124 return 0;
127 /* Find the volume header and fill in some minimum bits in superblock */
128 /* Takes in super block, returns true if good data read */
129 int hfsplus_read_wrapper(struct super_block *sb)
131 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
132 struct hfsplus_wd wd;
133 sector_t part_start, part_size;
134 u32 blocksize;
135 int error = 0;
137 error = -EINVAL;
138 blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
139 if (!blocksize)
140 goto out;
142 if (hfsplus_get_last_session(sb, &part_start, &part_size))
143 goto out;
145 error = -ENOMEM;
146 sbi->s_vhdr = kmalloc(HFSPLUS_SECTOR_SIZE, GFP_KERNEL);
147 if (!sbi->s_vhdr)
148 goto out;
149 sbi->s_backup_vhdr = kmalloc(HFSPLUS_SECTOR_SIZE, GFP_KERNEL);
150 if (!sbi->s_backup_vhdr)
151 goto out_free_vhdr;
153 reread:
154 error = hfsplus_submit_bio(sb->s_bdev,
155 part_start + HFSPLUS_VOLHEAD_SECTOR,
156 sbi->s_vhdr, READ);
157 if (error)
158 goto out_free_backup_vhdr;
160 error = -EINVAL;
161 switch (sbi->s_vhdr->signature) {
162 case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
163 set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
164 /*FALLTHRU*/
165 case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
166 break;
167 case cpu_to_be16(HFSP_WRAP_MAGIC):
168 if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
169 goto out_free_backup_vhdr;
170 wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
171 part_start += (sector_t)wd.ablk_start +
172 (sector_t)wd.embed_start * wd.ablk_size;
173 part_size = (sector_t)wd.embed_count * wd.ablk_size;
174 goto reread;
175 default:
177 * Check for a partition block.
179 * (should do this only for cdrom/loop though)
181 if (hfs_part_find(sb, &part_start, &part_size))
182 goto out_free_backup_vhdr;
183 goto reread;
186 error = hfsplus_submit_bio(sb->s_bdev,
187 part_start + part_size - 2,
188 sbi->s_backup_vhdr, READ);
189 if (error)
190 goto out_free_backup_vhdr;
192 error = -EINVAL;
193 if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
194 printk(KERN_WARNING
195 "hfs: invalid secondary volume header\n");
196 goto out_free_backup_vhdr;
199 blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
202 * Block size must be at least as large as a sector and a multiple of 2.
204 if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
205 goto out_free_backup_vhdr;
206 sbi->alloc_blksz = blocksize;
207 sbi->alloc_blksz_shift = 0;
208 while ((blocksize >>= 1) != 0)
209 sbi->alloc_blksz_shift++;
210 blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE);
213 * Align block size to block offset.
215 while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
216 blocksize >>= 1;
218 if (sb_set_blocksize(sb, blocksize) != blocksize) {
219 printk(KERN_ERR "hfs: unable to set blocksize to %u!\n",
220 blocksize);
221 goto out_free_backup_vhdr;
224 sbi->blockoffset =
225 part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
226 sbi->part_start = part_start;
227 sbi->sect_count = part_size;
228 sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
229 return 0;
231 out_free_backup_vhdr:
232 kfree(sbi->s_backup_vhdr);
233 out_free_vhdr:
234 kfree(sbi->s_vhdr);
235 out:
236 return error;