Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <asm/unaligned.h> |
| 16 | |
| 17 | #include "hfsplus_fs.h" |
| 18 | #include "hfsplus_raw.h" |
| 19 | |
| 20 | struct hfsplus_wd { |
| 21 | u32 ablk_size; |
| 22 | u16 ablk_start; |
| 23 | u16 embed_start; |
| 24 | u16 embed_count; |
| 25 | }; |
| 26 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 27 | static 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 | |
| 34 | int 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd) |
| 62 | { |
| 63 | u32 extent; |
| 64 | u16 attrib; |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 65 | __be16 sig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 67 | sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG); |
| 68 | if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) && |
| 69 | sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | 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 Harrison | 8b3789e | 2008-04-29 01:03:44 -0700 | [diff] [blame] | 84 | extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | wd->embed_start = (extent >> 16) & 0xFFFF; |
| 86 | wd->embed_count = extent & 0xFFFF; |
| 87 | |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | static 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 Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 102 | if (HFSPLUS_SB(sb)->session >= 0) { |
| 103 | te.cdte_track = HFSPLUS_SB(sb)->session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
| 108 | return 0; |
| 109 | } |
Roman Zippel | 634725a | 2006-01-18 17:43:05 -0800 | [diff] [blame] | 110 | printk(KERN_ERR "hfs: invalid session number or type of track\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | 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 */ |
| 122 | int hfsplus_read_wrapper(struct super_block *sb) |
| 123 | { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 124 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | struct hfsplus_wd wd; |
| 126 | sector_t part_start, part_size; |
| 127 | u32 blocksize; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 128 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 130 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE); |
| 132 | if (!blocksize) |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 133 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | if (hfsplus_get_last_session(sb, &part_start, &part_size)) |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 136 | goto out; |
Ben Hutchings | 5c36fe3 | 2009-10-26 16:49:51 -0700 | [diff] [blame] | 137 | if ((u64)part_start + part_size > 0x100000000ULL) { |
| 138 | pr_err("hfs: volumes larger than 2TB are not supported yet\n"); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 139 | goto out; |
Ben Hutchings | 5c36fe3 | 2009-10-26 16:49:51 -0700 | [diff] [blame] | 140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 142 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 150 | reread: |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | * (should do this only for cdrom/loop though) |
| 176 | */ |
| 177 | if (hfs_part_find(sb, &part_start, &part_size)) |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 178 | goto out; |
| 179 | goto reread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 182 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 188 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | */ |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 200 | if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize)) |
| 201 | goto out_free_backup_vhdr; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 202 | sbi->alloc_blksz = blocksize; |
| 203 | sbi->alloc_blksz_shift = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | while ((blocksize >>= 1) != 0) |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 205 | sbi->alloc_blksz_shift++; |
| 206 | blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 208 | /* |
| 209 | * Align block size to block offset. |
| 210 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1)) |
| 212 | blocksize >>= 1; |
| 213 | |
| 214 | if (sb_set_blocksize(sb, blocksize) != blocksize) { |
Roman Zippel | 634725a | 2006-01-18 17:43:05 -0800 | [diff] [blame] | 215 | printk(KERN_ERR "hfs: unable to set blocksize to %u!\n", blocksize); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 216 | goto out_free_backup_vhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 219 | sbi->blockoffset = |
| 220 | part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 221 | sbi->part_start = part_start; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 222 | sbi->sect_count = part_size; |
| 223 | sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | return 0; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame^] | 225 | |
| 226 | out_free_backup_vhdr: |
| 227 | kfree(sbi->s_backup_vhdr); |
| 228 | out_free_vhdr: |
| 229 | kfree(sbi->s_vhdr); |
| 230 | out: |
| 231 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |