]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/hfsplus/wrapper.c
block/fs/drivers: remove rw argument from submit_bio
[mirror_ubuntu-bionic-kernel.git] / fs / hfsplus / wrapper.c
CommitLineData
1da177e4
LT
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>
1da177e4
LT
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
915ab236
FF
27/**
28 * hfsplus_submit_bio - Perform block I/O
6596528e
SF
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)
52399b17 48{
52399b17 49 struct bio *bio;
50176dde 50 int ret = 0;
a6dc8c04 51 u64 io_size;
6596528e
SF
52 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);
52399b17
CH
64
65 bio = bio_alloc(GFP_NOIO, 1);
4f024f37 66 bio->bi_iter.bi_sector = sector;
6596528e 67 bio->bi_bdev = sb->s_bdev;
4e49ea4a 68 bio->bi_rw = rw;
52399b17 69
6596528e
SF
70 if (!(rw & WRITE) && data)
71 *data = (u8 *)buf + offset;
72
73 while (io_size > 0) {
74 unsigned int page_offset = offset_in_page(buf);
75 unsigned int len = min_t(unsigned int, PAGE_SIZE - page_offset,
76 io_size);
77
78 ret = bio_add_page(bio, virt_to_page(buf), len, page_offset);
79 if (ret != len) {
80 ret = -EIO;
81 goto out;
82 }
83 io_size -= len;
84 buf = (u8 *)buf + len;
85 }
52399b17 86
4e49ea4a 87 ret = submit_bio_wait(bio);
6596528e 88out:
50176dde 89 bio_put(bio);
6596528e 90 return ret < 0 ? ret : 0;
52399b17
CH
91}
92
1da177e4
LT
93static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
94{
95 u32 extent;
96 u16 attrib;
2179d372 97 __be16 sig;
1da177e4 98
2179d372
DE
99 sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
100 if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
101 sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
1da177e4
LT
102 return 0;
103
104 attrib = be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ATTRIB));
105 if (!(attrib & HFSP_WRAP_ATTRIB_SLOCK) ||
106 !(attrib & HFSP_WRAP_ATTRIB_SPARED))
107 return 0;
108
2753cc28
AS
109 wd->ablk_size =
110 be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
1da177e4
LT
111 if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
112 return 0;
113 if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
114 return 0;
2753cc28
AS
115 wd->ablk_start =
116 be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
1da177e4 117
8b3789e5 118 extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
1da177e4
LT
119 wd->embed_start = (extent >> 16) & 0xFFFF;
120 wd->embed_count = extent & 0xFFFF;
121
122 return 1;
123}
124
125static int hfsplus_get_last_session(struct super_block *sb,
126 sector_t *start, sector_t *size)
127{
128 struct cdrom_multisession ms_info;
129 struct cdrom_tocentry te;
130 int res;
131
132 /* default values */
133 *start = 0;
134 *size = sb->s_bdev->bd_inode->i_size >> 9;
135
dd73a01a
CH
136 if (HFSPLUS_SB(sb)->session >= 0) {
137 te.cdte_track = HFSPLUS_SB(sb)->session;
1da177e4 138 te.cdte_format = CDROM_LBA;
2753cc28
AS
139 res = ioctl_by_bdev(sb->s_bdev,
140 CDROMREADTOCENTRY, (unsigned long)&te);
1da177e4
LT
141 if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
142 *start = (sector_t)te.cdte_addr.lba << 2;
143 return 0;
144 }
d6142673 145 pr_err("invalid session number or type of track\n");
1da177e4
LT
146 return -EINVAL;
147 }
148 ms_info.addr_format = CDROM_LBA;
2753cc28
AS
149 res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION,
150 (unsigned long)&ms_info);
1da177e4
LT
151 if (!res && ms_info.xa_flag)
152 *start = (sector_t)ms_info.addr.lba << 2;
153 return 0;
154}
155
156/* Find the volume header and fill in some minimum bits in superblock */
157/* Takes in super block, returns true if good data read */
158int hfsplus_read_wrapper(struct super_block *sb)
159{
dd73a01a 160 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
1da177e4
LT
161 struct hfsplus_wd wd;
162 sector_t part_start, part_size;
163 u32 blocksize;
52399b17 164 int error = 0;
1da177e4 165
52399b17 166 error = -EINVAL;
1da177e4
LT
167 blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
168 if (!blocksize)
52399b17 169 goto out;
1da177e4
LT
170
171 if (hfsplus_get_last_session(sb, &part_start, &part_size))
52399b17 172 goto out;
1da177e4 173
52399b17 174 error = -ENOMEM;
6596528e
SF
175 sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
176 if (!sbi->s_vhdr_buf)
52399b17 177 goto out;
6596528e
SF
178 sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
179 if (!sbi->s_backup_vhdr_buf)
52399b17
CH
180 goto out_free_vhdr;
181
182reread:
6596528e
SF
183 error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
184 sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
185 READ);
52399b17
CH
186 if (error)
187 goto out_free_backup_vhdr;
188
189 error = -EINVAL;
190 switch (sbi->s_vhdr->signature) {
191 case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
192 set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
193 /*FALLTHRU*/
194 case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
195 break;
196 case cpu_to_be16(HFSP_WRAP_MAGIC):
197 if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
a1dbcef0 198 goto out_free_backup_vhdr;
52399b17 199 wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
4ba2d5fd
CH
200 part_start += (sector_t)wd.ablk_start +
201 (sector_t)wd.embed_start * wd.ablk_size;
202 part_size = (sector_t)wd.embed_count * wd.ablk_size;
52399b17
CH
203 goto reread;
204 default:
205 /*
206 * Check for a partition block.
207 *
1da177e4
LT
208 * (should do this only for cdrom/loop though)
209 */
210 if (hfs_part_find(sb, &part_start, &part_size))
a1dbcef0 211 goto out_free_backup_vhdr;
52399b17
CH
212 goto reread;
213 }
214
6596528e
SF
215 error = hfsplus_submit_bio(sb, part_start + part_size - 2,
216 sbi->s_backup_vhdr_buf,
217 (void **)&sbi->s_backup_vhdr, READ);
52399b17
CH
218 if (error)
219 goto out_free_backup_vhdr;
220
221 error = -EINVAL;
222 if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
d6142673 223 pr_warn("invalid secondary volume header\n");
52399b17 224 goto out_free_backup_vhdr;
1da177e4
LT
225 }
226
52399b17 227 blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
1da177e4 228
52399b17
CH
229 /*
230 * Block size must be at least as large as a sector and a multiple of 2.
1da177e4 231 */
52399b17
CH
232 if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
233 goto out_free_backup_vhdr;
dd73a01a 234 sbi->alloc_blksz = blocksize;
297cc272 235 sbi->alloc_blksz_shift = ilog2(blocksize);
915ab236 236 blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE);
1da177e4 237
52399b17
CH
238 /*
239 * Align block size to block offset.
240 */
1da177e4
LT
241 while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
242 blocksize >>= 1;
243
244 if (sb_set_blocksize(sb, blocksize) != blocksize) {
d6142673 245 pr_err("unable to set blocksize to %u!\n", blocksize);
52399b17 246 goto out_free_backup_vhdr;
1da177e4
LT
247 }
248
dd73a01a
CH
249 sbi->blockoffset =
250 part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
52399b17 251 sbi->part_start = part_start;
dd73a01a
CH
252 sbi->sect_count = part_size;
253 sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
1da177e4 254 return 0;
52399b17
CH
255
256out_free_backup_vhdr:
f588c960 257 kfree(sbi->s_backup_vhdr_buf);
52399b17 258out_free_vhdr:
f588c960 259 kfree(sbi->s_vhdr_buf);
52399b17
CH
260out:
261 return error;
1da177e4 262}