3 * based on code of fs/reiserfs/dev.c by
5 * (C) Copyright 2003 - 2004
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <zfs_common.h>
28 static block_dev_desc_t *zfs_block_dev_desc;
29 static disk_partition_t *part_info;
31 void zfs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
33 zfs_block_dev_desc = rbdd;
38 int zfs_devread(int sector, int byte_offset, int byte_len, char *buf)
40 short sec_buffer[SECTOR_SIZE/sizeof(short)];
41 char *sec_buf = (char *)sec_buffer;
45 * Check partition boundaries
48 ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >=
50 /* errnum = ERR_OUTSIDE_PART; */
51 printf(" ** zfs_devread() read outside partition sector %d\n", sector);
56 * Get the read to the beginning of a partition.
58 sector += byte_offset >> SECTOR_BITS;
59 byte_offset &= SECTOR_SIZE - 1;
61 debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len);
63 if (zfs_block_dev_desc == NULL) {
64 printf("** Invalid Block Device Descriptor (NULL)\n");
68 if (byte_offset != 0) {
69 /* read first part which isn't aligned with start of sector */
70 if (zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev,
71 part_info->start + sector, 1,
72 (unsigned long *)sec_buf) != 1) {
73 printf(" ** zfs_devread() read error **\n");
76 memcpy(buf, sec_buf + byte_offset,
77 min(SECTOR_SIZE - byte_offset, byte_len));
78 buf += min(SECTOR_SIZE - byte_offset, byte_len);
79 byte_len -= min(SECTOR_SIZE - byte_offset, byte_len);
86 /* read sector aligned part */
87 block_len = byte_len & ~(SECTOR_SIZE - 1);
92 block_len = SECTOR_SIZE;
93 zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev,
94 part_info->start + sector,
95 1, (unsigned long *)p);
96 memcpy(buf, p, byte_len);
100 if (zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev,
101 part_info->start + sector, block_len / SECTOR_SIZE,
102 (unsigned long *) buf) != block_len / SECTOR_SIZE) {
103 printf(" ** zfs_devread() read error - block\n");
107 block_len = byte_len & ~(SECTOR_SIZE - 1);
109 byte_len -= block_len;
110 sector += block_len / SECTOR_SIZE;
113 /* read rest of data which are not in whole sector */
114 if (zfs_block_dev_desc->
115 block_read(zfs_block_dev_desc->dev,
116 part_info->start + sector, 1,
117 (unsigned long *) sec_buf) != 1) {
118 printf(" ** zfs_devread() read error - last part\n");
121 memcpy(buf, sec_buf, byte_len);