hfsplus: use raw bio access for the volume headers
[pandora-kernel.git] / 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 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
61 static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
62 {
63         u32 extent;
64         u16 attrib;
65         __be16 sig;
66
67         sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
68         if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
69             sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
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
84         extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
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
102         if (HFSPLUS_SB(sb)->session >= 0) {
103                 te.cdte_track = HFSPLUS_SB(sb)->session;
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                 }
110                 printk(KERN_ERR "hfs: invalid session number or type of track\n");
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 {
124         struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
125         struct hfsplus_wd wd;
126         sector_t part_start, part_size;
127         u32 blocksize;
128         int error = 0;
129
130         error = -EINVAL;
131         blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
132         if (!blocksize)
133                 goto out;
134
135         if (hfsplus_get_last_session(sb, &part_start, &part_size))
136                 goto out;
137         if ((u64)part_start + part_size > 0x100000000ULL) {
138                 pr_err("hfs: volumes larger than 2TB are not supported yet\n");
139                 goto out;
140         }
141
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;
149
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                  *
175                  * (should do this only for cdrom/loop though)
176                  */
177                 if (hfs_part_find(sb, &part_start, &part_size))
178                         goto out;
179                 goto reread;
180         }
181
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;
187
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.
199          */
200         if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
201                 goto out_free_backup_vhdr;
202         sbi->alloc_blksz = blocksize;
203         sbi->alloc_blksz_shift = 0;
204         while ((blocksize >>= 1) != 0)
205                 sbi->alloc_blksz_shift++;
206         blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE);
207
208         /*
209          * Align block size to block offset.
210          */
211         while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
212                 blocksize >>= 1;
213
214         if (sb_set_blocksize(sb, blocksize) != blocksize) {
215                 printk(KERN_ERR "hfs: unable to set blocksize to %u!\n", blocksize);
216                 goto out_free_backup_vhdr;
217         }
218
219         sbi->blockoffset =
220                 part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
221         sbi->part_start = part_start;
222         sbi->sect_count = part_size;
223         sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
224         return 0;
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;
232 }