2 * linux/fs/hfsplus/wrapper.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handling of HFS wrappers around HFS+ volumes
12 #include <linux/blkdev.h>
13 #include <linux/cdrom.h>
14 #include <linux/genhd.h>
15 #include <asm/unaligned.h>
17 #include "hfsplus_fs.h"
18 #include "hfsplus_raw.h"
27 static void hfsplus_end_io_sync(struct bio
*bio
, int err
)
30 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
31 complete(bio
->bi_private
);
35 * hfsplus_submit_bio - Perfrom block I/O
36 * @sb: super block of volume for I/O
37 * @sector: block to read or write, for blocks of HFSPLUS_SECTOR_SIZE bytes
38 * @buf: buffer for I/O
39 * @data: output pointer for location of requested data
40 * @rw: direction of I/O
42 * The unit of I/O is hfsplus_min_io_size(sb), which may be bigger than
43 * HFSPLUS_SECTOR_SIZE, and @buf must be sized accordingly. On reads
44 * @data will return a pointer to the start of the requested sector,
45 * which may not be the same location as @buf.
47 * If @sector is not aligned to the bdev logical block size it will
48 * be rounded down. For writes this means that @buf should contain data
49 * that starts at the rounded-down address. As long as the data was
50 * read using hfsplus_submit_bio() and the same buffer is used things
51 * will work correctly.
53 int hfsplus_submit_bio(struct super_block
*sb
, sector_t sector
,
54 void *buf
, void **data
, int rw
)
56 DECLARE_COMPLETION_ONSTACK(wait
);
64 * Align sector to hardware sector size and find offset. We
65 * assume that io_size is a power of two, which _should_
68 io_size
= hfsplus_min_io_size(sb
);
69 start
= (loff_t
)sector
<< HFSPLUS_SECTOR_SHIFT
;
70 offset
= start
& (io_size
- 1);
71 sector
&= ~((io_size
>> HFSPLUS_SECTOR_SHIFT
) - 1);
73 bio
= bio_alloc(GFP_NOIO
, 1);
74 bio
->bi_sector
= sector
;
75 bio
->bi_bdev
= sb
->s_bdev
;
76 bio
->bi_end_io
= hfsplus_end_io_sync
;
77 bio
->bi_private
= &wait
;
79 if (!(rw
& WRITE
) && data
)
80 *data
= (u8
*)buf
+ offset
;
83 unsigned int page_offset
= offset_in_page(buf
);
84 unsigned int len
= min_t(unsigned int, PAGE_SIZE
- page_offset
,
87 ret
= bio_add_page(bio
, virt_to_page(buf
), len
, page_offset
);
93 buf
= (u8
*)buf
+ len
;
97 wait_for_completion(&wait
);
99 if (!bio_flagged(bio
, BIO_UPTODATE
))
104 return ret
< 0 ? ret
: 0;
107 static int hfsplus_read_mdb(void *bufptr
, struct hfsplus_wd
*wd
)
113 sig
= *(__be16
*)(bufptr
+ HFSP_WRAPOFF_EMBEDSIG
);
114 if (sig
!= cpu_to_be16(HFSPLUS_VOLHEAD_SIG
) &&
115 sig
!= cpu_to_be16(HFSPLUS_VOLHEAD_SIGX
))
118 attrib
= be16_to_cpu(*(__be16
*)(bufptr
+ HFSP_WRAPOFF_ATTRIB
));
119 if (!(attrib
& HFSP_WRAP_ATTRIB_SLOCK
) ||
120 !(attrib
& HFSP_WRAP_ATTRIB_SPARED
))
124 be32_to_cpu(*(__be32
*)(bufptr
+ HFSP_WRAPOFF_ABLKSIZE
));
125 if (wd
->ablk_size
< HFSPLUS_SECTOR_SIZE
)
127 if (wd
->ablk_size
% HFSPLUS_SECTOR_SIZE
)
130 be16_to_cpu(*(__be16
*)(bufptr
+ HFSP_WRAPOFF_ABLKSTART
));
132 extent
= get_unaligned_be32(bufptr
+ HFSP_WRAPOFF_EMBEDEXT
);
133 wd
->embed_start
= (extent
>> 16) & 0xFFFF;
134 wd
->embed_count
= extent
& 0xFFFF;
139 static int hfsplus_get_last_session(struct super_block
*sb
,
140 sector_t
*start
, sector_t
*size
)
142 struct cdrom_multisession ms_info
;
143 struct cdrom_tocentry te
;
148 *size
= sb
->s_bdev
->bd_inode
->i_size
>> 9;
150 if (HFSPLUS_SB(sb
)->session
>= 0) {
151 te
.cdte_track
= HFSPLUS_SB(sb
)->session
;
152 te
.cdte_format
= CDROM_LBA
;
153 res
= ioctl_by_bdev(sb
->s_bdev
,
154 CDROMREADTOCENTRY
, (unsigned long)&te
);
155 if (!res
&& (te
.cdte_ctrl
& CDROM_DATA_TRACK
) == 4) {
156 *start
= (sector_t
)te
.cdte_addr
.lba
<< 2;
159 printk(KERN_ERR
"hfs: invalid session number or type of track\n");
162 ms_info
.addr_format
= CDROM_LBA
;
163 res
= ioctl_by_bdev(sb
->s_bdev
, CDROMMULTISESSION
,
164 (unsigned long)&ms_info
);
165 if (!res
&& ms_info
.xa_flag
)
166 *start
= (sector_t
)ms_info
.addr
.lba
<< 2;
170 /* Find the volume header and fill in some minimum bits in superblock */
171 /* Takes in super block, returns true if good data read */
172 int hfsplus_read_wrapper(struct super_block
*sb
)
174 struct hfsplus_sb_info
*sbi
= HFSPLUS_SB(sb
);
175 struct hfsplus_wd wd
;
176 sector_t part_start
, part_size
;
181 blocksize
= sb_min_blocksize(sb
, HFSPLUS_SECTOR_SIZE
);
185 if (hfsplus_get_last_session(sb
, &part_start
, &part_size
))
187 if ((u64
)part_start
+ part_size
> 0x100000000ULL
) {
188 pr_err("hfs: volumes larger than 2TB are not supported yet\n");
193 sbi
->s_vhdr_buf
= kmalloc(hfsplus_min_io_size(sb
), GFP_KERNEL
);
194 if (!sbi
->s_vhdr_buf
)
196 sbi
->s_backup_vhdr_buf
= kmalloc(hfsplus_min_io_size(sb
), GFP_KERNEL
);
197 if (!sbi
->s_backup_vhdr_buf
)
201 error
= hfsplus_submit_bio(sb
, part_start
+ HFSPLUS_VOLHEAD_SECTOR
,
202 sbi
->s_vhdr_buf
, (void **)&sbi
->s_vhdr
,
205 goto out_free_backup_vhdr
;
208 switch (sbi
->s_vhdr
->signature
) {
209 case cpu_to_be16(HFSPLUS_VOLHEAD_SIGX
):
210 set_bit(HFSPLUS_SB_HFSX
, &sbi
->flags
);
212 case cpu_to_be16(HFSPLUS_VOLHEAD_SIG
):
214 case cpu_to_be16(HFSP_WRAP_MAGIC
):
215 if (!hfsplus_read_mdb(sbi
->s_vhdr
, &wd
))
216 goto out_free_backup_vhdr
;
217 wd
.ablk_size
>>= HFSPLUS_SECTOR_SHIFT
;
218 part_start
+= wd
.ablk_start
+ wd
.embed_start
* wd
.ablk_size
;
219 part_size
= wd
.embed_count
* wd
.ablk_size
;
223 * Check for a partition block.
225 * (should do this only for cdrom/loop though)
227 if (hfs_part_find(sb
, &part_start
, &part_size
))
228 goto out_free_backup_vhdr
;
232 error
= hfsplus_submit_bio(sb
, part_start
+ part_size
- 2,
233 sbi
->s_backup_vhdr_buf
,
234 (void **)&sbi
->s_backup_vhdr
, READ
);
236 goto out_free_backup_vhdr
;
239 if (sbi
->s_backup_vhdr
->signature
!= sbi
->s_vhdr
->signature
) {
241 "hfs: invalid secondary volume header\n");
242 goto out_free_backup_vhdr
;
245 blocksize
= be32_to_cpu(sbi
->s_vhdr
->blocksize
);
248 * Block size must be at least as large as a sector and a multiple of 2.
250 if (blocksize
< HFSPLUS_SECTOR_SIZE
|| ((blocksize
- 1) & blocksize
))
251 goto out_free_backup_vhdr
;
252 sbi
->alloc_blksz
= blocksize
;
253 sbi
->alloc_blksz_shift
= 0;
254 while ((blocksize
>>= 1) != 0)
255 sbi
->alloc_blksz_shift
++;
256 blocksize
= min(sbi
->alloc_blksz
, (u32
)PAGE_SIZE
);
259 * Align block size to block offset.
261 while (part_start
& ((blocksize
>> HFSPLUS_SECTOR_SHIFT
) - 1))
264 if (sb_set_blocksize(sb
, blocksize
) != blocksize
) {
265 printk(KERN_ERR
"hfs: unable to set blocksize to %u!\n",
267 goto out_free_backup_vhdr
;
271 part_start
>> (sb
->s_blocksize_bits
- HFSPLUS_SECTOR_SHIFT
);
272 sbi
->part_start
= part_start
;
273 sbi
->sect_count
= part_size
;
274 sbi
->fs_shift
= sbi
->alloc_blksz_shift
- sb
->s_blocksize_bits
;
277 out_free_backup_vhdr
:
278 kfree(sbi
->s_backup_vhdr_buf
);
280 kfree(sbi
->s_vhdr_buf
);