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