1 /* 2 * 3 * based on code of fs/reiserfs/dev.c by 4 * 5 * (C) Copyright 2003 - 2004 6 * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com> 7 * 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. 12 * 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. 17 * 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. 21 */ 22 23 24 #include <common.h> 25 #include <config.h> 26 #include <zfs_common.h> 27 28 static block_dev_desc_t *zfs_block_dev_desc; 29 static disk_partition_t part_info; 30 31 int zfs_set_blk_dev(block_dev_desc_t *rbdd, int part) 32 { 33 zfs_block_dev_desc = rbdd; 34 35 if (part == 0) { 36 /* disk doesn't use partition table */ 37 part_info.start = 0; 38 part_info.size = rbdd->lba; 39 part_info.blksz = rbdd->blksz; 40 } else { 41 if (get_partition_info(zfs_block_dev_desc, part, &part_info)) 42 return 0; 43 } 44 45 return part_info.size; 46 } 47 48 /* err */ 49 int zfs_devread(int sector, int byte_offset, int byte_len, char *buf) 50 { 51 short sec_buffer[SECTOR_SIZE/sizeof(short)]; 52 char *sec_buf = (char *)sec_buffer; 53 unsigned block_len; 54 55 /* 56 * Check partition boundaries 57 */ 58 if ((sector < 0) || 59 ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >= 60 part_info.size)) { 61 /* errnum = ERR_OUTSIDE_PART; */ 62 printf(" ** zfs_devread() read outside partition sector %d\n", sector); 63 return 1; 64 } 65 66 /* 67 * Get the read to the beginning of a partition. 68 */ 69 sector += byte_offset >> SECTOR_BITS; 70 byte_offset &= SECTOR_SIZE - 1; 71 72 debug(" <%d, %d, %d>\n", sector, byte_offset, byte_len); 73 74 if (zfs_block_dev_desc == NULL) { 75 printf("** Invalid Block Device Descriptor (NULL)\n"); 76 return 1; 77 } 78 79 if (byte_offset != 0) { 80 /* read first part which isn't aligned with start of sector */ 81 if (zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev, 82 part_info.start + sector, 1, 83 (unsigned long *) sec_buf) != 1) { 84 printf(" ** zfs_devread() read error **\n"); 85 return 1; 86 } 87 memcpy(buf, sec_buf + byte_offset, 88 min(SECTOR_SIZE - byte_offset, byte_len)); 89 buf += min(SECTOR_SIZE - byte_offset, byte_len); 90 byte_len -= min(SECTOR_SIZE - byte_offset, byte_len); 91 sector++; 92 } 93 94 if (byte_len == 0) 95 return 0; 96 97 /* read sector aligned part */ 98 block_len = byte_len & ~(SECTOR_SIZE - 1); 99 100 if (block_len == 0) { 101 u8 p[SECTOR_SIZE]; 102 103 block_len = SECTOR_SIZE; 104 zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev, 105 part_info.start + sector, 106 1, (unsigned long *)p); 107 memcpy(buf, p, byte_len); 108 return 0; 109 } 110 111 if (zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev, 112 part_info.start + sector, 113 block_len / SECTOR_SIZE, 114 (unsigned long *) buf) != 115 block_len / SECTOR_SIZE) { 116 printf(" ** zfs_devread() read error - block\n"); 117 return 1; 118 } 119 120 block_len = byte_len & ~(SECTOR_SIZE - 1); 121 buf += block_len; 122 byte_len -= block_len; 123 sector += block_len / SECTOR_SIZE; 124 125 if (byte_len != 0) { 126 /* read rest of data which are not in whole sector */ 127 if (zfs_block_dev_desc-> 128 block_read(zfs_block_dev_desc->dev, 129 part_info.start + sector, 1, 130 (unsigned long *) sec_buf) != 1) { 131 printf(" ** zfs_devread() read error - last part\n"); 132 return 1; 133 } 134 memcpy(buf, sec_buf, byte_len); 135 } 136 return 0; 137 } 138