11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/hfsplus/wrapper.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2001 51da177e4SLinus Torvalds * Brad Boyer (flar@allandria.com) 61da177e4SLinus Torvalds * (C) 2003 Ardis Technologies <roman@ardistech.com> 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Handling of HFS wrappers around HFS+ volumes 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/fs.h> 121da177e4SLinus Torvalds #include <linux/blkdev.h> 131da177e4SLinus Torvalds #include <linux/cdrom.h> 141da177e4SLinus Torvalds #include <linux/genhd.h> 151da177e4SLinus Torvalds #include <asm/unaligned.h> 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include "hfsplus_fs.h" 181da177e4SLinus Torvalds #include "hfsplus_raw.h" 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds struct hfsplus_wd { 211da177e4SLinus Torvalds u32 ablk_size; 221da177e4SLinus Torvalds u16 ablk_start; 231da177e4SLinus Torvalds u16 embed_start; 241da177e4SLinus Torvalds u16 embed_count; 251da177e4SLinus Torvalds }; 261da177e4SLinus Torvalds 27915ab236SFabian Frederick /** 28915ab236SFabian Frederick * hfsplus_submit_bio - Perform block I/O 296596528eSSeth Forshee * @sb: super block of volume for I/O 306596528eSSeth Forshee * @sector: block to read or write, for blocks of HFSPLUS_SECTOR_SIZE bytes 316596528eSSeth Forshee * @buf: buffer for I/O 326596528eSSeth Forshee * @data: output pointer for location of requested data 3367ed2596SMike Christie * @op: direction of I/O 3467ed2596SMike Christie * @op_flags: request op flags 356596528eSSeth Forshee * 366596528eSSeth Forshee * The unit of I/O is hfsplus_min_io_size(sb), which may be bigger than 376596528eSSeth Forshee * HFSPLUS_SECTOR_SIZE, and @buf must be sized accordingly. On reads 386596528eSSeth Forshee * @data will return a pointer to the start of the requested sector, 396596528eSSeth Forshee * which may not be the same location as @buf. 406596528eSSeth Forshee * 416596528eSSeth Forshee * If @sector is not aligned to the bdev logical block size it will 426596528eSSeth Forshee * be rounded down. For writes this means that @buf should contain data 436596528eSSeth Forshee * that starts at the rounded-down address. As long as the data was 446596528eSSeth Forshee * read using hfsplus_submit_bio() and the same buffer is used things 456596528eSSeth Forshee * will work correctly. 466596528eSSeth Forshee */ 476596528eSSeth Forshee int hfsplus_submit_bio(struct super_block *sb, sector_t sector, 4867ed2596SMike Christie void *buf, void **data, int op, int op_flags) 4952399b17SChristoph Hellwig { 5052399b17SChristoph Hellwig struct bio *bio; 5150176ddeSSeth Forshee int ret = 0; 52a6dc8c04SJanne Kalliomäki u64 io_size; 536596528eSSeth Forshee loff_t start; 546596528eSSeth Forshee int offset; 556596528eSSeth Forshee 566596528eSSeth Forshee /* 576596528eSSeth Forshee * Align sector to hardware sector size and find offset. We 586596528eSSeth Forshee * assume that io_size is a power of two, which _should_ 596596528eSSeth Forshee * be true. 606596528eSSeth Forshee */ 616596528eSSeth Forshee io_size = hfsplus_min_io_size(sb); 626596528eSSeth Forshee start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT; 636596528eSSeth Forshee offset = start & (io_size - 1); 646596528eSSeth Forshee sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1); 6552399b17SChristoph Hellwig 6652399b17SChristoph Hellwig bio = bio_alloc(GFP_NOIO, 1); 674f024f37SKent Overstreet bio->bi_iter.bi_sector = sector; 6874d46992SChristoph Hellwig bio_set_dev(bio, sb->s_bdev); 6967ed2596SMike Christie bio_set_op_attrs(bio, op, op_flags); 7052399b17SChristoph Hellwig 7167ed2596SMike Christie if (op != WRITE && data) 726596528eSSeth Forshee *data = (u8 *)buf + offset; 736596528eSSeth Forshee 746596528eSSeth Forshee while (io_size > 0) { 756596528eSSeth Forshee unsigned int page_offset = offset_in_page(buf); 766596528eSSeth Forshee unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset, 776596528eSSeth Forshee io_size); 786596528eSSeth Forshee 796596528eSSeth Forshee ret = bio_add_page(bio, virt_to_page(buf), len, page_offset); 806596528eSSeth Forshee if (ret != len) { 816596528eSSeth Forshee ret = -EIO; 826596528eSSeth Forshee goto out; 836596528eSSeth Forshee } 846596528eSSeth Forshee io_size -= len; 856596528eSSeth Forshee buf = (u8 *)buf + len; 866596528eSSeth Forshee } 8752399b17SChristoph Hellwig 884e49ea4aSMike Christie ret = submit_bio_wait(bio); 896596528eSSeth Forshee out: 9050176ddeSSeth Forshee bio_put(bio); 916596528eSSeth Forshee return ret < 0 ? ret : 0; 9252399b17SChristoph Hellwig } 9352399b17SChristoph Hellwig 941da177e4SLinus Torvalds static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd) 951da177e4SLinus Torvalds { 961da177e4SLinus Torvalds u32 extent; 971da177e4SLinus Torvalds u16 attrib; 982179d372SDavid Elliott __be16 sig; 991da177e4SLinus Torvalds 1002179d372SDavid Elliott sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG); 1012179d372SDavid Elliott if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) && 1022179d372SDavid Elliott sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX)) 1031da177e4SLinus Torvalds return 0; 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB)); 1061da177e4SLinus Torvalds if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) || 1071da177e4SLinus Torvalds !(attrib & HFSP_WRAP_ATTRIB_SPARED)) 1081da177e4SLinus Torvalds return 0; 1091da177e4SLinus Torvalds 1102753cc28SAnton Salikhmetov wd->ablk_size = 1112753cc28SAnton Salikhmetov be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE)); 1121da177e4SLinus Torvalds if (wd->ablk_size < HFSPLUS_SECTOR_SIZE) 1131da177e4SLinus Torvalds return 0; 1141da177e4SLinus Torvalds if (wd->ablk_size % HFSPLUS_SECTOR_SIZE) 1151da177e4SLinus Torvalds return 0; 1162753cc28SAnton Salikhmetov wd->ablk_start = 1172753cc28SAnton Salikhmetov be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART)); 1181da177e4SLinus Torvalds 1198b3789e5SHarvey Harrison extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT); 1201da177e4SLinus Torvalds wd->embed_start = (extent >> 16) & 0xFFFF; 1211da177e4SLinus Torvalds wd->embed_count = extent & 0xFFFF; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds return 1; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds static int hfsplus_get_last_session(struct super_block *sb, 1271da177e4SLinus Torvalds sector_t *start, sector_t *size) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds struct cdrom_multisession ms_info; 1301da177e4SLinus Torvalds struct cdrom_tocentry te; 1311da177e4SLinus Torvalds int res; 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds /* default values */ 1341da177e4SLinus Torvalds *start = 0; 135a3f22350SFabian Frederick *size = i_size_read(sb->s_bdev->bd_inode) >> 9; 1361da177e4SLinus Torvalds 137dd73a01aSChristoph Hellwig if (HFSPLUS_SB(sb)->session >= 0) { 138dd73a01aSChristoph Hellwig te.cdte_track = HFSPLUS_SB(sb)->session; 1391da177e4SLinus Torvalds te.cdte_format = CDROM_LBA; 1402753cc28SAnton Salikhmetov res = ioctl_by_bdev(sb->s_bdev, 1412753cc28SAnton Salikhmetov CDROMREADTOCENTRY, (unsigned long)&te); 1421da177e4SLinus Torvalds if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) { 1431da177e4SLinus Torvalds *start = (sector_t)te.cdte_addr.lba << 2; 1441da177e4SLinus Torvalds return 0; 1451da177e4SLinus Torvalds } 146d6142673SJoe Perches pr_err("invalid session number or type of track\n"); 1471da177e4SLinus Torvalds return -EINVAL; 1481da177e4SLinus Torvalds } 1491da177e4SLinus Torvalds ms_info.addr_format = CDROM_LBA; 1502753cc28SAnton Salikhmetov res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION, 1512753cc28SAnton Salikhmetov (unsigned long)&ms_info); 1521da177e4SLinus Torvalds if (!res && ms_info.xa_flag) 1531da177e4SLinus Torvalds *start = (sector_t)ms_info.addr.lba << 2; 1541da177e4SLinus Torvalds return 0; 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds /* Find the volume header and fill in some minimum bits in superblock */ 1581da177e4SLinus Torvalds /* Takes in super block, returns true if good data read */ 1591da177e4SLinus Torvalds int hfsplus_read_wrapper(struct super_block *sb) 1601da177e4SLinus Torvalds { 161dd73a01aSChristoph Hellwig struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb); 1621da177e4SLinus Torvalds struct hfsplus_wd wd; 1631da177e4SLinus Torvalds sector_t part_start, part_size; 1641da177e4SLinus Torvalds u32 blocksize; 16552399b17SChristoph Hellwig int error = 0; 1661da177e4SLinus Torvalds 16752399b17SChristoph Hellwig error = -EINVAL; 1681da177e4SLinus Torvalds blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE); 1691da177e4SLinus Torvalds if (!blocksize) 17052399b17SChristoph Hellwig goto out; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds if (hfsplus_get_last_session(sb, &part_start, &part_size)) 17352399b17SChristoph Hellwig goto out; 1741da177e4SLinus Torvalds 17552399b17SChristoph Hellwig error = -ENOMEM; 1766596528eSSeth Forshee sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL); 1776596528eSSeth Forshee if (!sbi->s_vhdr_buf) 17852399b17SChristoph Hellwig goto out; 1796596528eSSeth Forshee sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL); 1806596528eSSeth Forshee if (!sbi->s_backup_vhdr_buf) 18152399b17SChristoph Hellwig goto out_free_vhdr; 18252399b17SChristoph Hellwig 18352399b17SChristoph Hellwig reread: 1846596528eSSeth Forshee error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, 1856596528eSSeth Forshee sbi->s_vhdr_buf, (void **)&sbi->s_vhdr, 18667ed2596SMike Christie REQ_OP_READ, 0); 18752399b17SChristoph Hellwig if (error) 18852399b17SChristoph Hellwig goto out_free_backup_vhdr; 18952399b17SChristoph Hellwig 19052399b17SChristoph Hellwig error = -EINVAL; 19152399b17SChristoph Hellwig switch (sbi->s_vhdr->signature) { 19252399b17SChristoph Hellwig case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX): 19352399b17SChristoph Hellwig set_bit(HFSPLUS_SB_HFSX, &sbi->flags); 19452399b17SChristoph Hellwig /*FALLTHRU*/ 19552399b17SChristoph Hellwig case cpu_to_be16(HFSPLUS_VOLHEAD_SIG): 19652399b17SChristoph Hellwig break; 19752399b17SChristoph Hellwig case cpu_to_be16(HFSP_WRAP_MAGIC): 19852399b17SChristoph Hellwig if (!hfsplus_read_mdb(sbi->s_vhdr, &wd)) 199a1dbcef0SChuck Ebbert goto out_free_backup_vhdr; 2001da177e4SLinus Torvalds wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT; 2014ba2d5fdSChristoph Hellwig part_start += (sector_t)wd.ablk_start + 2024ba2d5fdSChristoph Hellwig (sector_t)wd.embed_start * wd.ablk_size; 2034ba2d5fdSChristoph Hellwig part_size = (sector_t)wd.embed_count * wd.ablk_size; 20452399b17SChristoph Hellwig goto reread; 20552399b17SChristoph Hellwig default: 20652399b17SChristoph Hellwig /* 20752399b17SChristoph Hellwig * Check for a partition block. 20852399b17SChristoph Hellwig * 2091da177e4SLinus Torvalds * (should do this only for cdrom/loop though) 2101da177e4SLinus Torvalds */ 2111da177e4SLinus Torvalds if (hfs_part_find(sb, &part_start, &part_size)) 212a1dbcef0SChuck Ebbert goto out_free_backup_vhdr; 21352399b17SChristoph Hellwig goto reread; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2166596528eSSeth Forshee error = hfsplus_submit_bio(sb, part_start + part_size - 2, 2176596528eSSeth Forshee sbi->s_backup_vhdr_buf, 21867ed2596SMike Christie (void **)&sbi->s_backup_vhdr, REQ_OP_READ, 21967ed2596SMike Christie 0); 22052399b17SChristoph Hellwig if (error) 22152399b17SChristoph Hellwig goto out_free_backup_vhdr; 2221da177e4SLinus Torvalds 22352399b17SChristoph Hellwig error = -EINVAL; 22452399b17SChristoph Hellwig if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) { 225d6142673SJoe Perches pr_warn("invalid secondary volume header\n"); 22652399b17SChristoph Hellwig goto out_free_backup_vhdr; 22752399b17SChristoph Hellwig } 22852399b17SChristoph Hellwig 22952399b17SChristoph Hellwig blocksize = be32_to_cpu(sbi->s_vhdr->blocksize); 23052399b17SChristoph Hellwig 23152399b17SChristoph Hellwig /* 23252399b17SChristoph Hellwig * Block size must be at least as large as a sector and a multiple of 2. 2331da177e4SLinus Torvalds */ 23452399b17SChristoph Hellwig if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize)) 23552399b17SChristoph Hellwig goto out_free_backup_vhdr; 236dd73a01aSChristoph Hellwig sbi->alloc_blksz = blocksize; 237297cc272SFabian Frederick sbi->alloc_blksz_shift = ilog2(blocksize); 238915ab236SFabian Frederick blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE); 2391da177e4SLinus Torvalds 24052399b17SChristoph Hellwig /* 24152399b17SChristoph Hellwig * Align block size to block offset. 24252399b17SChristoph Hellwig */ 2431da177e4SLinus Torvalds while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1)) 2441da177e4SLinus Torvalds blocksize >>= 1; 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds if (sb_set_blocksize(sb, blocksize) != blocksize) { 247d6142673SJoe Perches pr_err("unable to set blocksize to %u!\n", blocksize); 24852399b17SChristoph Hellwig goto out_free_backup_vhdr; 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds 251dd73a01aSChristoph Hellwig sbi->blockoffset = 252dd73a01aSChristoph Hellwig part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT); 25352399b17SChristoph Hellwig sbi->part_start = part_start; 254dd73a01aSChristoph Hellwig sbi->sect_count = part_size; 255dd73a01aSChristoph Hellwig sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits; 2561da177e4SLinus Torvalds return 0; 25752399b17SChristoph Hellwig 25852399b17SChristoph Hellwig out_free_backup_vhdr: 259f588c960SSeth Forshee kfree(sbi->s_backup_vhdr_buf); 26052399b17SChristoph Hellwig out_free_vhdr: 261f588c960SSeth Forshee kfree(sbi->s_vhdr_buf); 26252399b17SChristoph Hellwig out: 26352399b17SChristoph Hellwig return error; 2641da177e4SLinus Torvalds } 265