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 | |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 34 | /* |
| 35 | * hfsplus_submit_bio - Perfrom block I/O |
| 36 | * @sb: super block of volume for I/O |
| 37 | * @sector: block to read or write, for blocks of HFSPLUS_SECTOR_SIZE bytes |
| 38 | * @buf: buffer for I/O |
| 39 | * @data: output pointer for location of requested data |
| 40 | * @rw: direction of I/O |
| 41 | * |
| 42 | * The unit of I/O is hfsplus_min_io_size(sb), which may be bigger than |
| 43 | * HFSPLUS_SECTOR_SIZE, and @buf must be sized accordingly. On reads |
| 44 | * @data will return a pointer to the start of the requested sector, |
| 45 | * which may not be the same location as @buf. |
| 46 | * |
| 47 | * If @sector is not aligned to the bdev logical block size it will |
| 48 | * be rounded down. For writes this means that @buf should contain data |
| 49 | * that starts at the rounded-down address. As long as the data was |
| 50 | * read using hfsplus_submit_bio() and the same buffer is used things |
| 51 | * will work correctly. |
| 52 | */ |
| 53 | int hfsplus_submit_bio(struct super_block *sb, sector_t sector, |
| 54 | void *buf, void **data, int rw) |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 55 | { |
| 56 | DECLARE_COMPLETION_ONSTACK(wait); |
| 57 | struct bio *bio; |
Seth Forshee | 50176dd | 2011-05-31 16:35:50 -0500 | [diff] [blame] | 58 | int ret = 0; |
Janne Kalliomäki | a6dc8c0 | 2012-06-17 17:05:24 -0400 | [diff] [blame] | 59 | u64 io_size; |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 60 | loff_t start; |
| 61 | int offset; |
| 62 | |
| 63 | /* |
| 64 | * Align sector to hardware sector size and find offset. We |
| 65 | * assume that io_size is a power of two, which _should_ |
| 66 | * be true. |
| 67 | */ |
| 68 | io_size = hfsplus_min_io_size(sb); |
| 69 | start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT; |
| 70 | offset = start & (io_size - 1); |
| 71 | sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 72 | |
| 73 | bio = bio_alloc(GFP_NOIO, 1); |
| 74 | bio->bi_sector = sector; |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 75 | bio->bi_bdev = sb->s_bdev; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 76 | bio->bi_end_io = hfsplus_end_io_sync; |
| 77 | bio->bi_private = &wait; |
| 78 | |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 79 | if (!(rw & WRITE) && data) |
| 80 | *data = (u8 *)buf + offset; |
| 81 | |
| 82 | while (io_size > 0) { |
| 83 | unsigned int page_offset = offset_in_page(buf); |
| 84 | unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset, |
| 85 | io_size); |
| 86 | |
| 87 | ret = bio_add_page(bio, virt_to_page(buf), len, page_offset); |
| 88 | if (ret != len) { |
| 89 | ret = -EIO; |
| 90 | goto out; |
| 91 | } |
| 92 | io_size -= len; |
| 93 | buf = (u8 *)buf + len; |
| 94 | } |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 95 | |
| 96 | submit_bio(rw, bio); |
| 97 | wait_for_completion(&wait); |
| 98 | |
| 99 | if (!bio_flagged(bio, BIO_UPTODATE)) |
Seth Forshee | 50176dd | 2011-05-31 16:35:50 -0500 | [diff] [blame] | 100 | ret = -EIO; |
| 101 | |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 102 | out: |
Seth Forshee | 50176dd | 2011-05-31 16:35:50 -0500 | [diff] [blame] | 103 | bio_put(bio); |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 104 | return ret < 0 ? ret : 0; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd) |
| 108 | { |
| 109 | u32 extent; |
| 110 | u16 attrib; |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 111 | __be16 sig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
David Elliott | 2179d37 | 2006-01-18 17:43:08 -0800 | [diff] [blame] | 113 | sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG); |
| 114 | if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) && |
| 115 | sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | return 0; |
| 117 | |
| 118 | attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB)); |
| 119 | if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) || |
| 120 | !(attrib & HFSP_WRAP_ATTRIB_SPARED)) |
| 121 | return 0; |
| 122 | |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 123 | wd->ablk_size = |
| 124 | be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | if (wd->ablk_size < HFSPLUS_SECTOR_SIZE) |
| 126 | return 0; |
| 127 | if (wd->ablk_size % HFSPLUS_SECTOR_SIZE) |
| 128 | return 0; |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 129 | wd->ablk_start = |
| 130 | be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Harvey Harrison | 8b3789e | 2008-04-29 01:03:44 -0700 | [diff] [blame] | 132 | extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | wd->embed_start = (extent >> 16) & 0xFFFF; |
| 134 | wd->embed_count = extent & 0xFFFF; |
| 135 | |
| 136 | return 1; |
| 137 | } |
| 138 | |
| 139 | static int hfsplus_get_last_session(struct super_block *sb, |
| 140 | sector_t *start, sector_t *size) |
| 141 | { |
| 142 | struct cdrom_multisession ms_info; |
| 143 | struct cdrom_tocentry te; |
| 144 | int res; |
| 145 | |
| 146 | /* default values */ |
| 147 | *start = 0; |
| 148 | *size = sb->s_bdev->bd_inode->i_size >> 9; |
| 149 | |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 150 | if (HFSPLUS_SB(sb)->session >= 0) { |
| 151 | te.cdte_track = HFSPLUS_SB(sb)->session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | te.cdte_format = CDROM_LBA; |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 153 | res = ioctl_by_bdev(sb->s_bdev, |
| 154 | CDROMREADTOCENTRY, (unsigned long)&te); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) { |
| 156 | *start = (sector_t)te.cdte_addr.lba << 2; |
| 157 | return 0; |
| 158 | } |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 159 | pr_err("invalid session number or type of track\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | return -EINVAL; |
| 161 | } |
| 162 | ms_info.addr_format = CDROM_LBA; |
Anton Salikhmetov | 2753cc2 | 2010-12-16 18:08:38 +0200 | [diff] [blame] | 163 | res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION, |
| 164 | (unsigned long)&ms_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | if (!res && ms_info.xa_flag) |
| 166 | *start = (sector_t)ms_info.addr.lba << 2; |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* Find the volume header and fill in some minimum bits in superblock */ |
| 171 | /* Takes in super block, returns true if good data read */ |
| 172 | int hfsplus_read_wrapper(struct super_block *sb) |
| 173 | { |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 174 | struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | struct hfsplus_wd wd; |
| 176 | sector_t part_start, part_size; |
| 177 | u32 blocksize; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 178 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 180 | error = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE); |
| 182 | if (!blocksize) |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 183 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
| 185 | if (hfsplus_get_last_session(sb, &part_start, &part_size)) |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 186 | goto out; |
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 = -ENOMEM; |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 189 | sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL); |
| 190 | if (!sbi->s_vhdr_buf) |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 191 | goto out; |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 192 | sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL); |
| 193 | if (!sbi->s_backup_vhdr_buf) |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 194 | goto out_free_vhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 196 | reread: |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 197 | error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, |
| 198 | sbi->s_vhdr_buf, (void **)&sbi->s_vhdr, |
| 199 | READ); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 200 | if (error) |
| 201 | goto out_free_backup_vhdr; |
| 202 | |
| 203 | error = -EINVAL; |
| 204 | switch (sbi->s_vhdr->signature) { |
| 205 | case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX): |
| 206 | set_bit(HFSPLUS_SB_HFSX, &sbi->flags); |
| 207 | /*FALLTHRU*/ |
| 208 | case cpu_to_be16(HFSPLUS_VOLHEAD_SIG): |
| 209 | break; |
| 210 | case cpu_to_be16(HFSP_WRAP_MAGIC): |
| 211 | if (!hfsplus_read_mdb(sbi->s_vhdr, &wd)) |
Chuck Ebbert | a1dbcef | 2011-02-02 10:55:06 -0500 | [diff] [blame] | 212 | goto out_free_backup_vhdr; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 213 | wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT; |
Christoph Hellwig | 4ba2d5f | 2011-02-16 09:34:22 +0100 | [diff] [blame] | 214 | part_start += (sector_t)wd.ablk_start + |
| 215 | (sector_t)wd.embed_start * wd.ablk_size; |
| 216 | part_size = (sector_t)wd.embed_count * wd.ablk_size; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 217 | goto reread; |
| 218 | default: |
| 219 | /* |
| 220 | * Check for a partition block. |
| 221 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | * (should do this only for cdrom/loop though) |
| 223 | */ |
| 224 | if (hfs_part_find(sb, &part_start, &part_size)) |
Chuck Ebbert | a1dbcef | 2011-02-02 10:55:06 -0500 | [diff] [blame] | 225 | goto out_free_backup_vhdr; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 226 | goto reread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Seth Forshee | 6596528 | 2011-07-18 08:06:23 -0700 | [diff] [blame] | 229 | error = hfsplus_submit_bio(sb, part_start + part_size - 2, |
| 230 | sbi->s_backup_vhdr_buf, |
| 231 | (void **)&sbi->s_backup_vhdr, READ); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 232 | if (error) |
| 233 | goto out_free_backup_vhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 235 | error = -EINVAL; |
| 236 | if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 237 | pr_warn("invalid secondary volume header\n"); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 238 | goto out_free_backup_vhdr; |
| 239 | } |
| 240 | |
| 241 | blocksize = be32_to_cpu(sbi->s_vhdr->blocksize); |
| 242 | |
| 243 | /* |
| 244 | * 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] | 245 | */ |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 246 | if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize)) |
| 247 | goto out_free_backup_vhdr; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 248 | sbi->alloc_blksz = blocksize; |
| 249 | sbi->alloc_blksz_shift = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | while ((blocksize >>= 1) != 0) |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 251 | sbi->alloc_blksz_shift++; |
| 252 | blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 254 | /* |
| 255 | * Align block size to block offset. |
| 256 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1)) |
| 258 | blocksize >>= 1; |
| 259 | |
| 260 | if (sb_set_blocksize(sb, blocksize) != blocksize) { |
Joe Perches | d614267 | 2013-04-30 15:27:55 -0700 | [diff] [blame] | 261 | pr_err("unable to set blocksize to %u!\n", blocksize); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 262 | goto out_free_backup_vhdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 265 | sbi->blockoffset = |
| 266 | part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 267 | sbi->part_start = part_start; |
Christoph Hellwig | dd73a01 | 2010-10-01 05:42:59 +0200 | [diff] [blame] | 268 | sbi->sect_count = part_size; |
| 269 | sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | return 0; |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 271 | |
| 272 | out_free_backup_vhdr: |
Seth Forshee | f588c96 | 2011-09-15 10:48:27 -0400 | [diff] [blame] | 273 | kfree(sbi->s_backup_vhdr_buf); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 274 | out_free_vhdr: |
Seth Forshee | f588c96 | 2011-09-15 10:48:27 -0400 | [diff] [blame] | 275 | kfree(sbi->s_vhdr_buf); |
Christoph Hellwig | 52399b1 | 2010-11-23 14:37:47 +0100 | [diff] [blame] | 276 | out: |
| 277 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } |