blob: 15e0eabb489efb494543f9b1b7d143681899c47b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/hfsplus/wrapper.c
3 *
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
7 *
8 * Handling of HFS wrappers around HFS+ volumes
9 */
10
11#include <linux/fs.h>
12#include <linux/blkdev.h>
13#include <linux/cdrom.h>
14#include <linux/genhd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/unaligned.h>
16
17#include "hfsplus_fs.h"
18#include "hfsplus_raw.h"
19
20struct hfsplus_wd {
21 u32 ablk_size;
22 u16 ablk_start;
23 u16 embed_start;
24 u16 embed_count;
25};
26
Christoph Hellwig52399b12010-11-23 14:37:47 +010027static void hfsplus_end_io_sync(struct bio *bio, int err)
28{
29 if (err)
30 clear_bit(BIO_UPTODATE, &bio->bi_flags);
31 complete(bio->bi_private);
32}
33
34int hfsplus_submit_bio(struct block_device *bdev, sector_t sector,
35 void *data, int rw)
36{
37 DECLARE_COMPLETION_ONSTACK(wait);
38 struct bio *bio;
39
40 bio = bio_alloc(GFP_NOIO, 1);
41 bio->bi_sector = sector;
42 bio->bi_bdev = bdev;
43 bio->bi_end_io = hfsplus_end_io_sync;
44 bio->bi_private = &wait;
45
46 /*
47 * We always submit one sector at a time, so bio_add_page must not fail.
48 */
49 if (bio_add_page(bio, virt_to_page(data), HFSPLUS_SECTOR_SIZE,
50 offset_in_page(data)) != HFSPLUS_SECTOR_SIZE)
51 BUG();
52
53 submit_bio(rw, bio);
54 wait_for_completion(&wait);
55
56 if (!bio_flagged(bio, BIO_UPTODATE))
57 return -EIO;
58 return 0;
59}
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
62{
63 u32 extent;
64 u16 attrib;
David Elliott2179d372006-01-18 17:43:08 -080065 __be16 sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
David Elliott2179d372006-01-18 17:43:08 -080067 sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
68 if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
69 sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return 0;
71
72 attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
73 if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
74 !(attrib & HFSP_WRAP_ATTRIB_SPARED))
75 return 0;
76
77 wd->ablk_size = be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
78 if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
79 return 0;
80 if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
81 return 0;
82 wd->ablk_start = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
83
Harvey Harrison8b3789e2008-04-29 01:03:44 -070084 extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 wd->embed_start = (extent >> 16) & 0xFFFF;
86 wd->embed_count = extent & 0xFFFF;
87
88 return 1;
89}
90
91static int hfsplus_get_last_session(struct super_block *sb,
92 sector_t *start, sector_t *size)
93{
94 struct cdrom_multisession ms_info;
95 struct cdrom_tocentry te;
96 int res;
97
98 /* default values */
99 *start = 0;
100 *size = sb->s_bdev->bd_inode->i_size >> 9;
101
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200102 if (HFSPLUS_SB(sb)->session >= 0) {
103 te.cdte_track = HFSPLUS_SB(sb)->session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 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;
108 return 0;
109 }
Roman Zippel634725a2006-01-18 17:43:05 -0800110 printk(KERN_ERR "hfs: invalid session number or type of track\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return -EINVAL;
112 }
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;
117 return 0;
118}
119
120/* Find the volume header and fill in some minimum bits in superblock */
121/* Takes in super block, returns true if good data read */
122int hfsplus_read_wrapper(struct super_block *sb)
123{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200124 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 struct hfsplus_wd wd;
126 sector_t part_start, part_size;
127 u32 blocksize;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100128 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Christoph Hellwig52399b12010-11-23 14:37:47 +0100130 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
132 if (!blocksize)
Christoph Hellwig52399b12010-11-23 14:37:47 +0100133 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 if (hfsplus_get_last_session(sb, &part_start, &part_size))
Christoph Hellwig52399b12010-11-23 14:37:47 +0100136 goto out;
Ben Hutchings5c36fe32009-10-26 16:49:51 -0700137 if ((u64)part_start + part_size > 0x100000000ULL) {
138 pr_err("hfs: volumes larger than 2TB are not supported yet\n");
Christoph Hellwig52399b12010-11-23 14:37:47 +0100139 goto out;
Ben Hutchings5c36fe32009-10-26 16:49:51 -0700140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Christoph Hellwig52399b12010-11-23 14:37:47 +0100142 error = -ENOMEM;
143 sbi->s_vhdr = kmalloc(HFSPLUS_SECTOR_SIZE, GFP_KERNEL);
144 if (!sbi->s_vhdr)
145 goto out;
146 sbi->s_backup_vhdr = kmalloc(HFSPLUS_SECTOR_SIZE, GFP_KERNEL);
147 if (!sbi->s_backup_vhdr)
148 goto out_free_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Christoph Hellwig52399b12010-11-23 14:37:47 +0100150reread:
151 error = hfsplus_submit_bio(sb->s_bdev,
152 part_start + HFSPLUS_VOLHEAD_SECTOR,
153 sbi->s_vhdr, READ);
154 if (error)
155 goto out_free_backup_vhdr;
156
157 error = -EINVAL;
158 switch (sbi->s_vhdr->signature) {
159 case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
160 set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
161 /*FALLTHRU*/
162 case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
163 break;
164 case cpu_to_be16(HFSP_WRAP_MAGIC):
165 if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
166 goto out;
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;
170 goto reread;
171 default:
172 /*
173 * Check for a partition block.
174 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * (should do this only for cdrom/loop though)
176 */
177 if (hfs_part_find(sb, &part_start, &part_size))
Christoph Hellwig52399b12010-11-23 14:37:47 +0100178 goto out;
179 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181
Christoph Hellwig52399b12010-11-23 14:37:47 +0100182 error = hfsplus_submit_bio(sb->s_bdev,
183 part_start + part_size - 2,
184 sbi->s_backup_vhdr, READ);
185 if (error)
186 goto out_free_backup_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Christoph Hellwig52399b12010-11-23 14:37:47 +0100188 error = -EINVAL;
189 if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
190 printk(KERN_WARNING
191 "hfs: invalid secondary volume header\n");
192 goto out_free_backup_vhdr;
193 }
194
195 blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
196
197 /*
198 * Block size must be at least as large as a sector and a multiple of 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
Christoph Hellwig52399b12010-11-23 14:37:47 +0100200 if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
201 goto out_free_backup_vhdr;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200202 sbi->alloc_blksz = blocksize;
203 sbi->alloc_blksz_shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 while ((blocksize >>= 1) != 0)
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200205 sbi->alloc_blksz_shift++;
206 blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Christoph Hellwig52399b12010-11-23 14:37:47 +0100208 /*
209 * Align block size to block offset.
210 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
212 blocksize >>= 1;
213
214 if (sb_set_blocksize(sb, blocksize) != blocksize) {
Roman Zippel634725a2006-01-18 17:43:05 -0800215 printk(KERN_ERR "hfs: unable to set blocksize to %u!\n", blocksize);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100216 goto out_free_backup_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200219 sbi->blockoffset =
220 part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100221 sbi->part_start = part_start;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200222 sbi->sect_count = part_size;
223 sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return 0;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100225
226out_free_backup_vhdr:
227 kfree(sbi->s_backup_vhdr);
228out_free_vhdr:
229 kfree(sbi->s_vhdr);
230out:
231 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}