blob: ebb85e5f65499f3098fdecdc76333b40d7ad3d6b [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
Fabian Frederick915ab232014-06-06 14:36:27 -070027/**
28 * hfsplus_submit_bio - Perform block I/O
Seth Forshee65965282011-07-18 08:06:23 -070029 * @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
Mike Christie67ed2592016-06-05 14:31:58 -050033 * @op: direction of I/O
34 * @op_flags: request op flags
Seth Forshee65965282011-07-18 08:06:23 -070035 *
36 * The unit of I/O is hfsplus_min_io_size(sb), which may be bigger than
37 * HFSPLUS_SECTOR_SIZE, and @buf must be sized accordingly. On reads
38 * @data will return a pointer to the start of the requested sector,
39 * which may not be the same location as @buf.
40 *
41 * If @sector is not aligned to the bdev logical block size it will
42 * be rounded down. For writes this means that @buf should contain data
43 * that starts at the rounded-down address. As long as the data was
44 * read using hfsplus_submit_bio() and the same buffer is used things
45 * will work correctly.
46 */
47int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
Mike Christie67ed2592016-06-05 14:31:58 -050048 void *buf, void **data, int op, int op_flags)
Christoph Hellwig52399b12010-11-23 14:37:47 +010049{
Christoph Hellwig52399b12010-11-23 14:37:47 +010050 struct bio *bio;
Seth Forshee50176dd2011-05-31 16:35:50 -050051 int ret = 0;
Janne Kalliomäkia6dc8c02012-06-17 17:05:24 -040052 u64 io_size;
Seth Forshee65965282011-07-18 08:06:23 -070053 loff_t start;
54 int offset;
55
56 /*
57 * Align sector to hardware sector size and find offset. We
58 * assume that io_size is a power of two, which _should_
59 * be true.
60 */
61 io_size = hfsplus_min_io_size(sb);
62 start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT;
63 offset = start & (io_size - 1);
64 sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1);
Christoph Hellwig52399b12010-11-23 14:37:47 +010065
66 bio = bio_alloc(GFP_NOIO, 1);
Kent Overstreet4f024f32013-10-11 15:44:27 -070067 bio->bi_iter.bi_sector = sector;
Seth Forshee65965282011-07-18 08:06:23 -070068 bio->bi_bdev = sb->s_bdev;
Mike Christie67ed2592016-06-05 14:31:58 -050069 bio_set_op_attrs(bio, op, op_flags);
Christoph Hellwig52399b12010-11-23 14:37:47 +010070
Mike Christie67ed2592016-06-05 14:31:58 -050071 if (op != WRITE && data)
Seth Forshee65965282011-07-18 08:06:23 -070072 *data = (u8 *)buf + offset;
73
74 while (io_size > 0) {
75 unsigned int page_offset = offset_in_page(buf);
76 unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset,
77 io_size);
78
79 ret = bio_add_page(bio, virt_to_page(buf), len, page_offset);
80 if (ret != len) {
81 ret = -EIO;
82 goto out;
83 }
84 io_size -= len;
85 buf = (u8 *)buf + len;
86 }
Christoph Hellwig52399b12010-11-23 14:37:47 +010087
Mike Christie4e49ea42016-06-05 14:31:41 -050088 ret = submit_bio_wait(bio);
Seth Forshee65965282011-07-18 08:06:23 -070089out:
Seth Forshee50176dd2011-05-31 16:35:50 -050090 bio_put(bio);
Seth Forshee65965282011-07-18 08:06:23 -070091 return ret < 0 ? ret : 0;
Christoph Hellwig52399b12010-11-23 14:37:47 +010092}
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
95{
96 u32 extent;
97 u16 attrib;
David Elliott2179d372006-01-18 17:43:08 -080098 __be16 sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
David Elliott2179d372006-01-18 17:43:08 -0800100 sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
101 if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
102 sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return 0;
104
105 attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
106 if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
107 !(attrib & HFSP_WRAP_ATTRIB_SPARED))
108 return 0;
109
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200110 wd->ablk_size =
111 be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
113 return 0;
114 if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
115 return 0;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200116 wd->ablk_start =
117 be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Harvey Harrison8b3789e2008-04-29 01:03:44 -0700119 extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 wd->embed_start = (extent >> 16) & 0xFFFF;
121 wd->embed_count = extent & 0xFFFF;
122
123 return 1;
124}
125
126static int hfsplus_get_last_session(struct super_block *sb,
127 sector_t *start, sector_t *size)
128{
129 struct cdrom_multisession ms_info;
130 struct cdrom_tocentry te;
131 int res;
132
133 /* default values */
134 *start = 0;
135 *size = sb->s_bdev->bd_inode->i_size >> 9;
136
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200137 if (HFSPLUS_SB(sb)->session >= 0) {
138 te.cdte_track = HFSPLUS_SB(sb)->session;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 te.cdte_format = CDROM_LBA;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200140 res = ioctl_by_bdev(sb->s_bdev,
141 CDROMREADTOCENTRY, (unsigned long)&te);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
143 *start = (sector_t)te.cdte_addr.lba << 2;
144 return 0;
145 }
Joe Perchesd6142672013-04-30 15:27:55 -0700146 pr_err("invalid session number or type of track\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return -EINVAL;
148 }
149 ms_info.addr_format = CDROM_LBA;
Anton Salikhmetov2753cc22010-12-16 18:08:38 +0200150 res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION,
151 (unsigned long)&ms_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 if (!res && ms_info.xa_flag)
153 *start = (sector_t)ms_info.addr.lba << 2;
154 return 0;
155}
156
157/* Find the volume header and fill in some minimum bits in superblock */
158/* Takes in super block, returns true if good data read */
159int hfsplus_read_wrapper(struct super_block *sb)
160{
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200161 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct hfsplus_wd wd;
163 sector_t part_start, part_size;
164 u32 blocksize;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100165 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Christoph Hellwig52399b12010-11-23 14:37:47 +0100167 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
169 if (!blocksize)
Christoph Hellwig52399b12010-11-23 14:37:47 +0100170 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 if (hfsplus_get_last_session(sb, &part_start, &part_size))
Christoph Hellwig52399b12010-11-23 14:37:47 +0100173 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Christoph Hellwig52399b12010-11-23 14:37:47 +0100175 error = -ENOMEM;
Seth Forshee65965282011-07-18 08:06:23 -0700176 sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
177 if (!sbi->s_vhdr_buf)
Christoph Hellwig52399b12010-11-23 14:37:47 +0100178 goto out;
Seth Forshee65965282011-07-18 08:06:23 -0700179 sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
180 if (!sbi->s_backup_vhdr_buf)
Christoph Hellwig52399b12010-11-23 14:37:47 +0100181 goto out_free_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Christoph Hellwig52399b12010-11-23 14:37:47 +0100183reread:
Seth Forshee65965282011-07-18 08:06:23 -0700184 error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
185 sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
Mike Christie67ed2592016-06-05 14:31:58 -0500186 REQ_OP_READ, 0);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100187 if (error)
188 goto out_free_backup_vhdr;
189
190 error = -EINVAL;
191 switch (sbi->s_vhdr->signature) {
192 case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
193 set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
194 /*FALLTHRU*/
195 case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
196 break;
197 case cpu_to_be16(HFSP_WRAP_MAGIC):
198 if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
Chuck Ebberta1dbcef2011-02-02 10:55:06 -0500199 goto out_free_backup_vhdr;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100200 wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
Christoph Hellwig4ba2d5f2011-02-16 09:34:22 +0100201 part_start += (sector_t)wd.ablk_start +
202 (sector_t)wd.embed_start * wd.ablk_size;
203 part_size = (sector_t)wd.embed_count * wd.ablk_size;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100204 goto reread;
205 default:
206 /*
207 * Check for a partition block.
208 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * (should do this only for cdrom/loop though)
210 */
211 if (hfs_part_find(sb, &part_start, &part_size))
Chuck Ebberta1dbcef2011-02-02 10:55:06 -0500212 goto out_free_backup_vhdr;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100213 goto reread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
Seth Forshee65965282011-07-18 08:06:23 -0700216 error = hfsplus_submit_bio(sb, part_start + part_size - 2,
217 sbi->s_backup_vhdr_buf,
Mike Christie67ed2592016-06-05 14:31:58 -0500218 (void **)&sbi->s_backup_vhdr, REQ_OP_READ,
219 0);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100220 if (error)
221 goto out_free_backup_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Christoph Hellwig52399b12010-11-23 14:37:47 +0100223 error = -EINVAL;
224 if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
Joe Perchesd6142672013-04-30 15:27:55 -0700225 pr_warn("invalid secondary volume header\n");
Christoph Hellwig52399b12010-11-23 14:37:47 +0100226 goto out_free_backup_vhdr;
227 }
228
229 blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
230
231 /*
232 * Block size must be at least as large as a sector and a multiple of 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
Christoph Hellwig52399b12010-11-23 14:37:47 +0100234 if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
235 goto out_free_backup_vhdr;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200236 sbi->alloc_blksz = blocksize;
Fabian Frederick297cc272014-06-06 14:36:29 -0700237 sbi->alloc_blksz_shift = ilog2(blocksize);
Fabian Frederick915ab232014-06-06 14:36:27 -0700238 blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Christoph Hellwig52399b12010-11-23 14:37:47 +0100240 /*
241 * Align block size to block offset.
242 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
244 blocksize >>= 1;
245
246 if (sb_set_blocksize(sb, blocksize) != blocksize) {
Joe Perchesd6142672013-04-30 15:27:55 -0700247 pr_err("unable to set blocksize to %u!\n", blocksize);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100248 goto out_free_backup_vhdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200251 sbi->blockoffset =
252 part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100253 sbi->part_start = part_start;
Christoph Hellwigdd73a012010-10-01 05:42:59 +0200254 sbi->sect_count = part_size;
255 sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
Christoph Hellwig52399b12010-11-23 14:37:47 +0100257
258out_free_backup_vhdr:
Seth Forsheef588c962011-09-15 10:48:27 -0400259 kfree(sbi->s_backup_vhdr_buf);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100260out_free_vhdr:
Seth Forsheef588c962011-09-15 10:48:27 -0400261 kfree(sbi->s_vhdr_buf);
Christoph Hellwig52399b12010-11-23 14:37:47 +0100262out:
263 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}