linux/fs/hfsplus/wrapper.c
<<
>>
Prefs
   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>
  15#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
  27static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
  28{
  29        u32 extent;
  30        u16 attrib;
  31        __be16 sig;
  32
  33        sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
  34        if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
  35            sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
  36                return 0;
  37
  38        attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
  39        if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
  40           !(attrib & HFSP_WRAP_ATTRIB_SPARED))
  41                return 0;
  42
  43        wd->ablk_size = be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
  44        if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
  45                return 0;
  46        if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
  47                return 0;
  48        wd->ablk_start = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
  49
  50        extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
  51        wd->embed_start = (extent >> 16) & 0xFFFF;
  52        wd->embed_count = extent & 0xFFFF;
  53
  54        return 1;
  55}
  56
  57static int hfsplus_get_last_session(struct super_block *sb,
  58                                    sector_t *start, sector_t *size)
  59{
  60        struct cdrom_multisession ms_info;
  61        struct cdrom_tocentry te;
  62        int res;
  63
  64        /* default values */
  65        *start = 0;
  66        *size = sb->s_bdev->bd_inode->i_size >> 9;
  67
  68        if (HFSPLUS_SB(sb).session >= 0) {
  69                te.cdte_track = HFSPLUS_SB(sb).session;
  70                te.cdte_format = CDROM_LBA;
  71                res = ioctl_by_bdev(sb->s_bdev, CDROMREADTOCENTRY, (unsigned long)&te);
  72                if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
  73                        *start = (sector_t)te.cdte_addr.lba << 2;
  74                        return 0;
  75                }
  76                printk(KERN_ERR "hfs: invalid session number or type of track\n");
  77                return -EINVAL;
  78        }
  79        ms_info.addr_format = CDROM_LBA;
  80        res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION, (unsigned long)&ms_info);
  81        if (!res && ms_info.xa_flag)
  82                *start = (sector_t)ms_info.addr.lba << 2;
  83        return 0;
  84}
  85
  86/* Find the volume header and fill in some minimum bits in superblock */
  87/* Takes in super block, returns true if good data read */
  88int hfsplus_read_wrapper(struct super_block *sb)
  89{
  90        struct buffer_head *bh;
  91        struct hfsplus_vh *vhdr;
  92        struct hfsplus_wd wd;
  93        sector_t part_start, part_size;
  94        u32 blocksize;
  95
  96        blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
  97        if (!blocksize)
  98                return -EINVAL;
  99
 100        if (hfsplus_get_last_session(sb, &part_start, &part_size))
 101                return -EINVAL;
 102        if ((u64)part_start + part_size > 0x100000000ULL) {
 103                pr_err("hfs: volumes larger than 2TB are not supported yet\n");
 104                return -EINVAL;
 105        }
 106        while (1) {
 107                bh = sb_bread512(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, vhdr);
 108                if (!bh)
 109                        return -EIO;
 110
 111                if (vhdr->signature == cpu_to_be16(HFSP_WRAP_MAGIC)) {
 112                        if (!hfsplus_read_mdb(vhdr, &wd))
 113                                goto error;
 114                        wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
 115                        part_start += wd.ablk_start + wd.embed_start * wd.ablk_size;
 116                        part_size = wd.embed_count * wd.ablk_size;
 117                        brelse(bh);
 118                        bh = sb_bread512(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, vhdr);
 119                        if (!bh)
 120                                return -EIO;
 121                }
 122                if (vhdr->signature == cpu_to_be16(HFSPLUS_VOLHEAD_SIG))
 123                        break;
 124                if (vhdr->signature == cpu_to_be16(HFSPLUS_VOLHEAD_SIGX)) {
 125                        HFSPLUS_SB(sb).flags |= HFSPLUS_SB_HFSX;
 126                        break;
 127                }
 128                brelse(bh);
 129
 130                /* check for a partition block
 131                 * (should do this only for cdrom/loop though)
 132                 */
 133                if (hfs_part_find(sb, &part_start, &part_size))
 134                        return -EINVAL;
 135        }
 136
 137        blocksize = be32_to_cpu(vhdr->blocksize);
 138        brelse(bh);
 139
 140        /* block size must be at least as large as a sector
 141         * and a multiple of 2
 142         */
 143        if (blocksize < HFSPLUS_SECTOR_SIZE ||
 144            ((blocksize - 1) & blocksize))
 145                return -EINVAL;
 146        HFSPLUS_SB(sb).alloc_blksz = blocksize;
 147        HFSPLUS_SB(sb).alloc_blksz_shift = 0;
 148        while ((blocksize >>= 1) != 0)
 149                HFSPLUS_SB(sb).alloc_blksz_shift++;
 150        blocksize = min(HFSPLUS_SB(sb).alloc_blksz, (u32)PAGE_SIZE);
 151
 152        /* align block size to block offset */
 153        while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
 154                blocksize >>= 1;
 155
 156        if (sb_set_blocksize(sb, blocksize) != blocksize) {
 157                printk(KERN_ERR "hfs: unable to set blocksize to %u!\n", blocksize);
 158                return -EINVAL;
 159        }
 160
 161        HFSPLUS_SB(sb).blockoffset = part_start >>
 162                        (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
 163        HFSPLUS_SB(sb).sect_count = part_size;
 164        HFSPLUS_SB(sb).fs_shift = HFSPLUS_SB(sb).alloc_blksz_shift -
 165                        sb->s_blocksize_bits;
 166
 167        bh = sb_bread512(sb, part_start + HFSPLUS_VOLHEAD_SECTOR, vhdr);
 168        if (!bh)
 169                return -EIO;
 170
 171        /* should still be the same... */
 172        if (vhdr->signature != (HFSPLUS_SB(sb).flags & HFSPLUS_SB_HFSX ?
 173                                cpu_to_be16(HFSPLUS_VOLHEAD_SIGX) :
 174                                cpu_to_be16(HFSPLUS_VOLHEAD_SIG)))
 175                goto error;
 176        HFSPLUS_SB(sb).s_vhbh = bh;
 177        HFSPLUS_SB(sb).s_vhdr = vhdr;
 178
 179        return 0;
 180 error:
 181        brelse(bh);
 182        return -EINVAL;
 183}
 184