2 * linux/fs/hfsplus/wrapper.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handling of HFS wrappers around HFS+ volumes
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"
27 static void hfsplus_end_io_sync(struct bio *bio, int 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,
37 DECLARE_COMPLETION_ONSTACK(wait);
40 bio = bio_alloc(GFP_NOIO, 1);
41 bio->bi_sector = sector;
43 bio->bi_end_io = hfsplus_end_io_sync;
44 bio->bi_private = &wait;
47 * We always submit one sector at a time, so bio_add_page must not fail.
49 if (bio_add_page(bio, virt_to_page(data), HFSPLUS_SECTOR_SIZE,
50 offset_in_page(data)) != HFSPLUS_SECTOR_SIZE)
54 wait_for_completion(&wait);
56 if (!bio_flagged(bio, BIO_UPTODATE))
61 static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
67 sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
68 if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
69 sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
72 attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
73 if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
74 !(attrib & HFSP_WRAP_ATTRIB_SPARED))
77 wd->ablk_size = be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
78 if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
80 if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
82 wd->ablk_start = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
84 extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
85 wd->embed_start = (extent >> 16) & 0xFFFF;
86 wd->embed_count = extent & 0xFFFF;
91 static int hfsplus_get_last_session(struct super_block *sb,
92 sector_t *start, sector_t *size)
94 struct cdrom_multisession ms_info;
95 struct cdrom_tocentry te;
100 *size = sb->s_bdev->bd_inode->i_size >> 9;
102 if (HFSPLUS_SB(sb)->session >= 0) {
103 te.cdte_track = HFSPLUS_SB(sb)->session;
104 te.cdte_format = CDROM_LBA;
105 res = ioctl_by_bdev(sb->s_bdev, CDROMREADTOCENTRY, (unsigned long)&te);
106 if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
107 *start = (sector_t)te.cdte_addr.lba << 2;
110 printk(KERN_ERR "hfs: invalid session number or type of track\n");
113 ms_info.addr_format = CDROM_LBA;
114 res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION, (unsigned long)&ms_info);
115 if (!res && ms_info.xa_flag)
116 *start = (sector_t)ms_info.addr.lba << 2;
120 /* Find the volume header and fill in some minimum bits in superblock */
121 /* Takes in super block, returns true if good data read */
122 int hfsplus_read_wrapper(struct super_block *sb)
124 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
125 struct hfsplus_wd wd;
126 sector_t part_start, part_size;
131 blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
135 if (hfsplus_get_last_session(sb, &part_start, &part_size))
137 if ((u64)part_start + part_size > 0x100000000ULL) {
138 pr_err("hfs: volumes larger than 2TB are not supported yet\n");
143 sbi->s_vhdr = kmalloc(HFSPLUS_SECTOR_SIZE, GFP_KERNEL);
146 sbi->s_backup_vhdr = kmalloc(HFSPLUS_SECTOR_SIZE, GFP_KERNEL);
147 if (!sbi->s_backup_vhdr)
151 error = hfsplus_submit_bio(sb->s_bdev,
152 part_start + HFSPLUS_VOLHEAD_SECTOR,
155 goto out_free_backup_vhdr;
158 switch (sbi->s_vhdr->signature) {
159 case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
160 set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
162 case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
164 case cpu_to_be16(HFSP_WRAP_MAGIC):
165 if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
167 wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
168 part_start += wd.ablk_start + wd.embed_start * wd.ablk_size;
169 part_size = wd.embed_count * wd.ablk_size;
173 * Check for a partition block.
175 * (should do this only for cdrom/loop though)
177 if (hfs_part_find(sb, &part_start, &part_size))
182 error = hfsplus_submit_bio(sb->s_bdev,
183 part_start + part_size - 2,
184 sbi->s_backup_vhdr, READ);
186 goto out_free_backup_vhdr;
189 if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
191 "hfs: invalid secondary volume header\n");
192 goto out_free_backup_vhdr;
195 blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
198 * Block size must be at least as large as a sector and a multiple of 2.
200 if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
201 goto out_free_backup_vhdr;
202 sbi->alloc_blksz = blocksize;
203 sbi->alloc_blksz_shift = 0;
204 while ((blocksize >>= 1) != 0)
205 sbi->alloc_blksz_shift++;
206 blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE);
209 * Align block size to block offset.
211 while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
214 if (sb_set_blocksize(sb, blocksize) != blocksize) {
215 printk(KERN_ERR "hfs: unable to set blocksize to %u!\n", blocksize);
216 goto out_free_backup_vhdr;
220 part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
221 sbi->part_start = part_start;
222 sbi->sect_count = part_size;
223 sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
226 out_free_backup_vhdr:
227 kfree(sbi->s_backup_vhdr);