Linux Audio

Check our new training course

Loading...
v4.6
 
  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
 20struct hfsplus_wd {
 21	u32 ablk_size;
 22	u16 ablk_start;
 23	u16 embed_start;
 24	u16 embed_count;
 25};
 26
 27/**
 28 * hfsplus_submit_bio - Perform 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)
 48{
 
 49	struct bio *bio;
 50	int ret = 0;
 51	u64 io_size;
 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);
 64
 65	bio = bio_alloc(GFP_NOIO, 1);
 66	bio->bi_iter.bi_sector = sector;
 67	bio->bi_bdev = sb->s_bdev;
 68
 69	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	}
 85
 86	ret = submit_bio_wait(rw, bio);
 87out:
 88	bio_put(bio);
 89	return ret < 0 ? ret : 0;
 90}
 91
 92static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
 93{
 94	u32 extent;
 95	u16 attrib;
 96	__be16 sig;
 97
 98	sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
 99	if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
100	    sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
101		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
108	wd->ablk_size =
109		be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
110	if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
111		return 0;
112	if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
113		return 0;
114	wd->ablk_start =
115		be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
116
117	extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
118	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
135	if (HFSPLUS_SB(sb)->session >= 0) {
 
 
 
 
 
136		te.cdte_track = HFSPLUS_SB(sb)->session;
137		te.cdte_format = CDROM_LBA;
138		res = ioctl_by_bdev(sb->s_bdev,
139			CDROMREADTOCENTRY, (unsigned long)&te);
140		if (!res && (te.cdte_ctrl & CDROM_DATA_TRACK) == 4) {
141			*start = (sector_t)te.cdte_addr.lba << 2;
142			return 0;
143		}
144		pr_err("invalid session number or type of track\n");
145		return -EINVAL;
 
 
 
 
 
146	}
147	ms_info.addr_format = CDROM_LBA;
148	res = ioctl_by_bdev(sb->s_bdev, CDROMMULTISESSION,
149		(unsigned long)&ms_info);
150	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{
159	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
160	struct hfsplus_wd wd;
161	sector_t part_start, part_size;
162	u32 blocksize;
163	int error = 0;
164
165	error = -EINVAL;
166	blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
167	if (!blocksize)
168		goto out;
169
170	if (hfsplus_get_last_session(sb, &part_start, &part_size))
171		goto out;
172
173	error = -ENOMEM;
174	sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
175	if (!sbi->s_vhdr_buf)
176		goto out;
177	sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
178	if (!sbi->s_backup_vhdr_buf)
179		goto out_free_vhdr;
180
181reread:
182	error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
183				   sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
184				   READ);
185	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))
197			goto out_free_backup_vhdr;
198		wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
199		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;
202		goto reread;
203	default:
204		/*
205		 * Check for a partition block.
206		 *
207		 * (should do this only for cdrom/loop though)
208		 */
209		if (hfs_part_find(sb, &part_start, &part_size))
210			goto out_free_backup_vhdr;
211		goto reread;
212	}
213
214	error = hfsplus_submit_bio(sb, part_start + part_size - 2,
215				   sbi->s_backup_vhdr_buf,
216				   (void **)&sbi->s_backup_vhdr, READ);
217	if (error)
218		goto out_free_backup_vhdr;
219
220	error = -EINVAL;
221	if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
222		pr_warn("invalid secondary volume header\n");
223		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.
230	 */
231	if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
232		goto out_free_backup_vhdr;
233	sbi->alloc_blksz = blocksize;
234	sbi->alloc_blksz_shift = ilog2(blocksize);
235	blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE);
236
237	/*
238	 * Align block size to block offset.
239	 */
240	while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
241		blocksize >>= 1;
242
243	if (sb_set_blocksize(sb, blocksize) != blocksize) {
244		pr_err("unable to set blocksize to %u!\n", blocksize);
245		goto out_free_backup_vhdr;
246	}
247
248	sbi->blockoffset =
249		part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
250	sbi->part_start = part_start;
251	sbi->sect_count = part_size;
252	sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
253	return 0;
254
255out_free_backup_vhdr:
256	kfree(sbi->s_backup_vhdr_buf);
257out_free_vhdr:
258	kfree(sbi->s_vhdr_buf);
259out:
260	return error;
261}
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 *  linux/fs/hfsplus/wrapper.c
  4 *
  5 * Copyright (C) 2001
  6 * Brad Boyer (flar@allandria.com)
  7 * (C) 2003 Ardis Technologies <roman@ardistech.com>
  8 *
  9 * Handling of HFS wrappers around HFS+ volumes
 10 */
 11
 12#include <linux/fs.h>
 13#include <linux/blkdev.h>
 14#include <linux/cdrom.h>
 
 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
 27/**
 28 * hfsplus_submit_bio - Perform 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 * @opf: request op flags
 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 *
 46 * Returns: %0 on success else -errno code
 47 */
 48int hfsplus_submit_bio(struct super_block *sb, sector_t sector,
 49		       void *buf, void **data, blk_opf_t opf)
 50{
 51	const enum req_op op = opf & REQ_OP_MASK;
 52	struct bio *bio;
 53	int ret = 0;
 54	u64 io_size;
 55	loff_t start;
 56	int offset;
 57
 58	/*
 59	 * Align sector to hardware sector size and find offset. We
 60	 * assume that io_size is a power of two, which _should_
 61	 * be true.
 62	 */
 63	io_size = hfsplus_min_io_size(sb);
 64	start = (loff_t)sector << HFSPLUS_SECTOR_SHIFT;
 65	offset = start & (io_size - 1);
 66	sector &= ~((io_size >> HFSPLUS_SECTOR_SHIFT) - 1);
 67
 68	bio = bio_alloc(sb->s_bdev, 1, opf, GFP_NOIO);
 69	bio->bi_iter.bi_sector = sector;
 
 70
 71	if (op != REQ_OP_WRITE && data)
 72		*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	}
 87
 88	ret = submit_bio_wait(bio);
 89out:
 90	bio_put(bio);
 91	return ret < 0 ? ret : 0;
 92}
 93
 94static int hfsplus_read_mdb(void *bufptr, struct hfsplus_wd *wd)
 95{
 96	u32 extent;
 97	u16 attrib;
 98	__be16 sig;
 99
100	sig = *(__be16 *)(bufptr + HFSP_WRAPOFF_EMBEDSIG);
101	if (sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIG) &&
102	    sig != cpu_to_be16(HFSPLUS_VOLHEAD_SIGX))
103		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
110	wd->ablk_size =
111		be32_to_cpu(*(__be32 *)(bufptr + HFSP_WRAPOFF_ABLKSIZE));
112	if (wd->ablk_size < HFSPLUS_SECTOR_SIZE)
113		return 0;
114	if (wd->ablk_size % HFSPLUS_SECTOR_SIZE)
115		return 0;
116	wd->ablk_start =
117		be16_to_cpu(*(__be16 *)(bufptr + HFSP_WRAPOFF_ABLKSTART));
118
119	extent = get_unaligned_be32(bufptr + HFSP_WRAPOFF_EMBEDEXT);
120	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_device_info *cdi = disk_to_cdi(sb->s_bdev->bd_disk);
 
 
130
131	/* default values */
132	*start = 0;
133	*size = bdev_nr_sectors(sb->s_bdev);
134
135	if (HFSPLUS_SB(sb)->session >= 0) {
136		struct cdrom_tocentry te;
137
138		if (!cdi)
139			return -EINVAL;
140
141		te.cdte_track = HFSPLUS_SB(sb)->session;
142		te.cdte_format = CDROM_LBA;
143		if (cdrom_read_tocentry(cdi, &te) ||
144		    (te.cdte_ctrl & CDROM_DATA_TRACK) != 4) {
145			pr_err("invalid session number or type of track\n");
146			return -EINVAL;
 
147		}
148		*start = (sector_t)te.cdte_addr.lba << 2;
149	} else if (cdi) {
150		struct cdrom_multisession ms_info;
151
152		ms_info.addr_format = CDROM_LBA;
153		if (cdrom_multisession(cdi, &ms_info) == 0 && ms_info.xa_flag)
154			*start = (sector_t)ms_info.addr.lba << 2;
155	}
156
 
 
 
 
157	return 0;
158}
159
160/* Find the volume header and fill in some minimum bits in superblock */
161/* Takes in super block, returns true if good data read */
162int hfsplus_read_wrapper(struct super_block *sb)
163{
164	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
165	struct hfsplus_wd wd;
166	sector_t part_start, part_size;
167	u32 blocksize;
168	int error = 0;
169
170	error = -EINVAL;
171	blocksize = sb_min_blocksize(sb, HFSPLUS_SECTOR_SIZE);
172	if (!blocksize)
173		goto out;
174
175	if (hfsplus_get_last_session(sb, &part_start, &part_size))
176		goto out;
177
178	error = -ENOMEM;
179	sbi->s_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
180	if (!sbi->s_vhdr_buf)
181		goto out;
182	sbi->s_backup_vhdr_buf = kmalloc(hfsplus_min_io_size(sb), GFP_KERNEL);
183	if (!sbi->s_backup_vhdr_buf)
184		goto out_free_vhdr;
185
186reread:
187	error = hfsplus_submit_bio(sb, part_start + HFSPLUS_VOLHEAD_SECTOR,
188				   sbi->s_vhdr_buf, (void **)&sbi->s_vhdr,
189				   REQ_OP_READ);
190	if (error)
191		goto out_free_backup_vhdr;
192
193	error = -EINVAL;
194	switch (sbi->s_vhdr->signature) {
195	case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX):
196		set_bit(HFSPLUS_SB_HFSX, &sbi->flags);
197		fallthrough;
198	case cpu_to_be16(HFSPLUS_VOLHEAD_SIG):
199		break;
200	case cpu_to_be16(HFSP_WRAP_MAGIC):
201		if (!hfsplus_read_mdb(sbi->s_vhdr, &wd))
202			goto out_free_backup_vhdr;
203		wd.ablk_size >>= HFSPLUS_SECTOR_SHIFT;
204		part_start += (sector_t)wd.ablk_start +
205			       (sector_t)wd.embed_start * wd.ablk_size;
206		part_size = (sector_t)wd.embed_count * wd.ablk_size;
207		goto reread;
208	default:
209		/*
210		 * Check for a partition block.
211		 *
212		 * (should do this only for cdrom/loop though)
213		 */
214		if (hfs_part_find(sb, &part_start, &part_size))
215			goto out_free_backup_vhdr;
216		goto reread;
217	}
218
219	error = hfsplus_submit_bio(sb, part_start + part_size - 2,
220				   sbi->s_backup_vhdr_buf,
221				   (void **)&sbi->s_backup_vhdr, REQ_OP_READ);
222	if (error)
223		goto out_free_backup_vhdr;
224
225	error = -EINVAL;
226	if (sbi->s_backup_vhdr->signature != sbi->s_vhdr->signature) {
227		pr_warn("invalid secondary volume header\n");
228		goto out_free_backup_vhdr;
229	}
230
231	blocksize = be32_to_cpu(sbi->s_vhdr->blocksize);
232
233	/*
234	 * Block size must be at least as large as a sector and a multiple of 2.
235	 */
236	if (blocksize < HFSPLUS_SECTOR_SIZE || ((blocksize - 1) & blocksize))
237		goto out_free_backup_vhdr;
238	sbi->alloc_blksz = blocksize;
239	sbi->alloc_blksz_shift = ilog2(blocksize);
240	blocksize = min_t(u32, sbi->alloc_blksz, PAGE_SIZE);
241
242	/*
243	 * Align block size to block offset.
244	 */
245	while (part_start & ((blocksize >> HFSPLUS_SECTOR_SHIFT) - 1))
246		blocksize >>= 1;
247
248	if (sb_set_blocksize(sb, blocksize) != blocksize) {
249		pr_err("unable to set blocksize to %u!\n", blocksize);
250		goto out_free_backup_vhdr;
251	}
252
253	sbi->blockoffset =
254		part_start >> (sb->s_blocksize_bits - HFSPLUS_SECTOR_SHIFT);
255	sbi->part_start = part_start;
256	sbi->sect_count = part_size;
257	sbi->fs_shift = sbi->alloc_blksz_shift - sb->s_blocksize_bits;
258	return 0;
259
260out_free_backup_vhdr:
261	kfree(sbi->s_backup_vhdr_buf);
262out_free_vhdr:
263	kfree(sbi->s_vhdr_buf);
264out:
265	return error;
266}