blob: 4b86468125c155f4af29ed230bc84f60da10b3dc [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;
Seth Forshee50176dd2011-05-31 16:35:50 -050039 int ret = 0;
Christoph Hellwig52399b12010-11-23 14:37:47 +010040
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;
46
47 /*
48 * We always submit one sector at a time, so bio_add_page must not fail.
49 */
50 if (bio_add_page(bio, virt_to_page(data), HFSPLUS_SECTOR_SIZE,
51 offset_in_page(data)) != HFSPLUS_SECTOR_SIZE)
52 BUG();
53
54 submit_bio(rw, bio);
55 wait_for_completion(&wait);
56
57 if (!bio_flagged(bio, BIO_UPTODATE))
Seth Forshee50176dd2011-05-31 16:35:50 -050058 ret = -EIO;
59
60 bio_put(bio);
61 return ret;
Christoph Hellwig52399b12010-11-23 14:37:47 +010062}
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
65{
66 u32 extent;
67 u16 attrib;
David Elliott2179d372006-01-18 17:43:08 -080068 __be16 sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
David Elliott2179d372006-01-18 17:43:08 -080070 sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
71 if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
72 sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return 0;
74
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;
79
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020080 wd->ablk_size =
81 be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
83 return 0;
84 if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
85 return 0;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +020086 wd->ablk_start =
87 be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Harvey Harrison8b3789e2008-04-29 01:03:44 -070089 extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 wd->embed_start = (extent >> 16) & 0xFFFF;
91 wd->embed_count = extent & 0xFFFF;
92
93 return 1;
94}
95
96static int hfsplus_get_last_session(struct super_block *sb,
97 sector_t *start, sector_t *size)
98{
99 struct cdrom_multisession ms_info;
100 struct cdrom_tocentry te;
101 int res;
102
103 /* default values */
104 *start = 0;
105 *size = sb->s_bdev->bd_inode->i_size >> 9;
106
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200107 if (HFSPLUS_SB(sb)->session >= 0) {
108 te.cdte_track = HFSPLUS_SB(sb)->session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 te.cdte_format = CDROM_LBA;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200110 res = ioctl_by_bdev(sb->s_bdev,
111 CDROMREADTOCENTRY, (unsigned long)&te);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
113 *start = (sector_t)te.cdte_addr.lba << 2;
114 return 0;
115 }
Roman Zippel634725a2006-01-18 17:43:05 -0800116 printk(KERN_ERR "hfs: invalid session number or type of track\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return -EINVAL;
118 }
119 ms_info.addr_format = CDROM_LBA;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200120 res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION,
121 (unsigned long)&ms_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (!res && ms_info.xa_flag)
123 *start = (sector_t)ms_info.addr.lba << 2;
124 return 0;
125}
126
127/* Find the volume header and fill in some minimum bits in superblock */
128/* Takes in super block, returns true if good data read */
129int hfsplus_read_wrapper(struct super_block *sb)
130{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200131 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 struct hfsplus_wd wd;
133 sector_t part_start, part_size;
134 u32 blocksize;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100135 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Christoph Hellwig52399b12010-11-23 14:37:47 +0100137 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
139 if (!blocksize)
Christoph Hellwig52399b12010-11-23 14:37:47 +0100140 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if (hfsplus_get_last_session(sb, &part_start, &part_size))
Christoph Hellwig52399b12010-11-23 14:37:47 +0100143 goto out;
Ben Hutchings5c36fe32009-10-26 16:49:51 -0700144 if ((u64)part_start + part_size > 0x100000000ULL) {
145 pr_err("hfs: volumes larger than 2TB are not supported yet\n");
Christoph Hellwig52399b12010-11-23 14:37:47 +0100146 goto out;
Ben Hutchings5c36fe32009-10-26 16:49:51 -0700147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Christoph Hellwig52399b12010-11-23 14:37:47 +0100149 error = -ENOMEM;
150 sbi->s_vhdr = kmalloc(HFSPLUS_SECTOR_SIZE, GFP_KERNEL);
151 if (!sbi->s_vhdr)
152 goto out;
153 sbi->s_backup_vhdr = kmalloc(HFSPLUS_SECTOR_SIZE, GFP_KERNEL);
154 if (!sbi->s_backup_vhdr)
155 goto out_free_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Christoph Hellwig52399b12010-11-23 14:37:47 +0100157reread:
158 error = hfsplus_submit_bio(sb->s_bdev,
159 part_start + HFSPLUS_VOLHEAD_SECTOR,
160 sbi->s_vhdr, READ);
161 if (error)
162 goto out_free_backup_vhdr;
163
164 error = -EINVAL;
165 switch (sbi->s_vhdr->signature) {
166 case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
167 set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
168 /*FALLTHRU*/
169 case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
170 break;
171 case cpu_to_be16(HFSP_WRAP_MAGIC):
172 if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
Chuck Ebberta1dbcef2011-02-02 10:55:06 -0500173 goto out_free_backup_vhdr;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100174 wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
Christoph Hellwig4ba2d5f2011-02-16 09:34:22 +0100175 part_start += (sector_t)wd.ablk_start +
176 (sector_t)wd.embed_start * wd.ablk_size;
177 part_size = (sector_t)wd.embed_count * wd.ablk_size;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100178 goto reread;
179 default:
180 /*
181 * Check for a partition block.
182 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * (should do this only for cdrom/loop though)
184 */
185 if (hfs_part_find(sb, &part_start, &part_size))
Chuck Ebberta1dbcef2011-02-02 10:55:06 -0500186 goto out_free_backup_vhdr;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100187 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
Christoph Hellwig52399b12010-11-23 14:37:47 +0100190 error = hfsplus_submit_bio(sb->s_bdev,
191 part_start + part_size - 2,
192 sbi->s_backup_vhdr, READ);
193 if (error)
194 goto out_free_backup_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Christoph Hellwig52399b12010-11-23 14:37:47 +0100196 error = -EINVAL;
197 if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
198 printk(KERN_WARNING
199 "hfs: invalid secondary volume header\n");
200 goto out_free_backup_vhdr;
201 }
202
203 blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
204
205 /*
206 * Block size must be at least as large as a sector and a multiple of 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
Christoph Hellwig52399b12010-11-23 14:37:47 +0100208 if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
209 goto out_free_backup_vhdr;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200210 sbi->alloc_blksz = blocksize;
211 sbi->alloc_blksz_shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 while ((blocksize >>= 1) != 0)
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200213 sbi->alloc_blksz_shift++;
214 blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Christoph Hellwig52399b12010-11-23 14:37:47 +0100216 /*
217 * Align block size to block offset.
218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
220 blocksize >>= 1;
221
222 if (sb_set_blocksize(sb, blocksize) != blocksize) {
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200223 printk(KERN_ERR "hfs: unable to set blocksize to %u!\n",
224 blocksize);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100225 goto out_free_backup_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200228 sbi->blockoffset =
229 part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100230 sbi->part_start = part_start;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200231 sbi->sect_count = part_size;
232 sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return 0;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100234
235out_free_backup_vhdr:
236 kfree(sbi->s_backup_vhdr);
237out_free_vhdr:
238 kfree(sbi->s_vhdr);
239out:
240 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}