blob: 3f999649587ff8185ebd326c3672acee83542de4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070015#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
Seth Forshee65965282011-07-18 08:06:23 -070027/*
28 * hfsplus_submit_bio - Perfrom block I/O
29 * @sb: super block of volume for I/O
30 * @sector: block to read or write, for blocks of HFSPLUS_SECTOR_SIZE bytes
31 * @buf: buffer for I/O
32 * @data: output pointer for location of requested data
33 * @rw: direction of I/O
34 *
35 * The unit of I/O is hfsplus_min_io_size(sb), which may be bigger than
36 * HFSPLUS_SECTOR_SIZE, and @buf must be sized accordingly. On reads
37 * @data will return a pointer to the start of the requested sector,
38 * which may not be the same location as @buf.
39 *
40 * If @sector is not aligned to the bdev logical block size it will
41 * be rounded down. For writes this means that @buf should contain data
42 * that starts at the rounded-down address. As long as the data was
43 * read using hfsplus_submit_bio() and the same buffer is used things
44 * will work correctly.
45 */
46int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
47 void *buf, void **data, int rw)
Christoph Hellwig52399b12010-11-23 14:37:47 +010048{
Christoph Hellwig52399b12010-11-23 14:37:47 +010049 struct bio *bio;
Seth Forshee50176dd2011-05-31 16:35:50 -050050 int ret = 0;
Janne Kalliomäkia6dc8c02012-06-17 17:05:24 -040051 u64 io_size;
Seth Forshee65965282011-07-18 08:06:23 -070052 loff_t start;
53 int offset;
54
55 /*
56 * Align sector to hardware sector size and find offset. We
57 * assume that io_size is a power of two, which _should_
58 * be true.
59 */
60 io_size = hfsplus_min_io_size(sb);
61 start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT;
62 offset = start & (io_size - 1);
63 sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1);
Christoph Hellwig52399b12010-11-23 14:37:47 +010064
65 bio = bio_alloc(GFP_NOIO, 1);
Kent Overstreet4f024f32013-10-11 15:44:27 -070066 bio->bi_iter.bi_sector = sector;
Seth Forshee65965282011-07-18 08:06:23 -070067 bio->bi_bdev = sb->s_bdev;
Christoph Hellwig52399b12010-11-23 14:37:47 +010068
Seth Forshee65965282011-07-18 08:06:23 -070069 if (!(rw & WRITE) && data)
70 *data = (u8 *)buf + offset;
71
72 while (io_size > 0) {
73 unsigned int page_offset = offset_in_page(buf);
74 unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset,
75 io_size);
76
77 ret = bio_add_page(bio, virt_to_page(buf), len, page_offset);
78 if (ret != len) {
79 ret = -EIO;
80 goto out;
81 }
82 io_size -= len;
83 buf = (u8 *)buf + len;
84 }
Christoph Hellwig52399b12010-11-23 14:37:47 +010085
Kent Overstreet33879d42013-11-23 22:33:32 -080086 ret = submit_bio_wait(rw, bio);
Seth Forshee65965282011-07-18 08:06:23 -070087out:
Seth Forshee50176dd2011-05-31 16:35:50 -050088 bio_put(bio);
Seth Forshee65965282011-07-18 08:06:23 -070089 return ret < 0 ? ret : 0;
Christoph Hellwig52399b12010-11-23 14:37:47 +010090}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
93{
94 u32 extent;
95 u16 attrib;
David Elliott2179d372006-01-18 17:43:08 -080096 __be16 sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
David Elliott2179d372006-01-18 17:43:08 -080098 sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
99 if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
100 sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return 0;
102
103 attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
104 if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
105 !(attrib & HFSP_WRAP_ATTRIB_SPARED))
106 return 0;
107
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200108 wd->ablk_size =
109 be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
111 return 0;
112 if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
113 return 0;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200114 wd->ablk_start =
115 be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Harvey Harrison8b3789e2008-04-29 01:03:44 -0700117 extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 wd->embed_start = (extent >> 16) & 0xFFFF;
119 wd->embed_count = extent & 0xFFFF;
120
121 return 1;
122}
123
124static int hfsplus_get_last_session(struct super_block *sb,
125 sector_t *start, sector_t *size)
126{
127 struct cdrom_multisession ms_info;
128 struct cdrom_tocentry te;
129 int res;
130
131 /* default values */
132 *start = 0;
133 *size = sb->s_bdev->bd_inode->i_size >> 9;
134
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200135 if (HFSPLUS_SB(sb)->session >= 0) {
136 te.cdte_track = HFSPLUS_SB(sb)->session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 te.cdte_format = CDROM_LBA;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200138 res = ioctl_by_bdev(sb->s_bdev,
139 CDROMREADTOCENTRY, (unsigned long)&te);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
141 *start = (sector_t)te.cdte_addr.lba << 2;
142 return 0;
143 }
Joe Perchesd6142672013-04-30 15:27:55 -0700144 pr_err("invalid session number or type of track\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return -EINVAL;
146 }
147 ms_info.addr_format = CDROM_LBA;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200148 res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION,
149 (unsigned long)&ms_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (!res && ms_info.xa_flag)
151 *start = (sector_t)ms_info.addr.lba << 2;
152 return 0;
153}
154
155/* Find the volume header and fill in some minimum bits in superblock */
156/* Takes in super block, returns true if good data read */
157int hfsplus_read_wrapper(struct super_block *sb)
158{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200159 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct hfsplus_wd wd;
161 sector_t part_start, part_size;
162 u32 blocksize;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100163 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Christoph Hellwig52399b12010-11-23 14:37:47 +0100165 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
167 if (!blocksize)
Christoph Hellwig52399b12010-11-23 14:37:47 +0100168 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 if (hfsplus_get_last_session(sb, &part_start, &part_size))
Christoph Hellwig52399b12010-11-23 14:37:47 +0100171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Christoph Hellwig52399b12010-11-23 14:37:47 +0100173 error = -ENOMEM;
Seth Forshee65965282011-07-18 08:06:23 -0700174 sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
175 if (!sbi->s_vhdr_buf)
Christoph Hellwig52399b12010-11-23 14:37:47 +0100176 goto out;
Seth Forshee65965282011-07-18 08:06:23 -0700177 sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
178 if (!sbi->s_backup_vhdr_buf)
Christoph Hellwig52399b12010-11-23 14:37:47 +0100179 goto out_free_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Christoph Hellwig52399b12010-11-23 14:37:47 +0100181reread:
Seth Forshee65965282011-07-18 08:06:23 -0700182 error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
183 sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
184 READ);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100185 if (error)
186 goto out_free_backup_vhdr;
187
188 error = -EINVAL;
189 switch (sbi->s_vhdr->signature) {
190 case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
191 set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
192 /*FALLTHRU*/
193 case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
194 break;
195 case cpu_to_be16(HFSP_WRAP_MAGIC):
196 if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
Chuck Ebberta1dbcef2011-02-02 10:55:06 -0500197 goto out_free_backup_vhdr;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100198 wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
Christoph Hellwig4ba2d5f2011-02-16 09:34:22 +0100199 part_start += (sector_t)wd.ablk_start +
200 (sector_t)wd.embed_start * wd.ablk_size;
201 part_size = (sector_t)wd.embed_count * wd.ablk_size;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100202 goto reread;
203 default:
204 /*
205 * Check for a partition block.
206 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * (should do this only for cdrom/loop though)
208 */
209 if (hfs_part_find(sb, &part_start, &part_size))
Chuck Ebberta1dbcef2011-02-02 10:55:06 -0500210 goto out_free_backup_vhdr;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100211 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
Seth Forshee65965282011-07-18 08:06:23 -0700214 error = hfsplus_submit_bio(sb, part_start + part_size - 2,
215 sbi->s_backup_vhdr_buf,
216 (void **)&sbi->s_backup_vhdr, READ);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100217 if (error)
218 goto out_free_backup_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Christoph Hellwig52399b12010-11-23 14:37:47 +0100220 error = -EINVAL;
221 if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
Joe Perchesd6142672013-04-30 15:27:55 -0700222 pr_warn("invalid secondary volume header\n");
Christoph Hellwig52399b12010-11-23 14:37:47 +0100223 goto out_free_backup_vhdr;
224 }
225
226 blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
227
228 /*
229 * Block size must be at least as large as a sector and a multiple of 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
Christoph Hellwig52399b12010-11-23 14:37:47 +0100231 if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
232 goto out_free_backup_vhdr;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200233 sbi->alloc_blksz = blocksize;
234 sbi->alloc_blksz_shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 while ((blocksize >>= 1) != 0)
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200236 sbi->alloc_blksz_shift++;
237 blocksize = min(sbi->alloc_blksz, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Christoph Hellwig52399b12010-11-23 14:37:47 +0100239 /*
240 * Align block size to block offset.
241 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
243 blocksize >>= 1;
244
245 if (sb_set_blocksize(sb, blocksize) != blocksize) {
Joe Perchesd6142672013-04-30 15:27:55 -0700246 pr_err("unable to set blocksize to %u!\n", blocksize);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100247 goto out_free_backup_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200250 sbi->blockoffset =
251 part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100252 sbi->part_start = part_start;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200253 sbi->sect_count = part_size;
254 sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return 0;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100256
257out_free_backup_vhdr:
Seth Forsheef588c962011-09-15 10:48:27 -0400258 kfree(sbi->s_backup_vhdr_buf);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100259out_free_vhdr:
Seth Forsheef588c962011-09-15 10:48:27 -0400260 kfree(sbi->s_vhdr_buf);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100261out:
262 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}