2 * (C) Copyright 2003 - 2004
3 * Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "reiserfs_private.h"
27 static block_dev_desc_t
*reiserfs_block_dev_desc
;
28 static disk_partition_t part_info
;
31 int reiserfs_set_blk_dev(block_dev_desc_t
*rbdd
, int part
)
33 reiserfs_block_dev_desc
= rbdd
;
36 /* disk doesn't use partition table */
38 part_info
.size
= rbdd
->lba
;
39 part_info
.blksz
= rbdd
->blksz
;
41 if (get_partition_info (reiserfs_block_dev_desc
, part
, &part_info
)) {
45 return (part_info
.size
);
49 int reiserfs_devread (int sector
, int byte_offset
, int byte_len
, char *buf
)
51 char sec_buf
[SECTOR_SIZE
];
54 unsigned len = byte_len;
58 * Check partition boundaries
61 || ((sector
+ ((byte_offset
+ byte_len
- 1) >> SECTOR_BITS
))
63 /* errnum = ERR_OUTSIDE_PART; */
64 printf (" ** reiserfs_devread() read outside partition\n");
69 * Get the read to the beginning of a partition.
71 sector
+= byte_offset
>> SECTOR_BITS
;
72 byte_offset
&= SECTOR_SIZE
- 1;
75 printf (" <%d, %d, %d> ", sector
, byte_offset
, byte_len
);
79 if (reiserfs_block_dev_desc
== NULL
)
83 if (byte_offset
!= 0) {
84 /* read first part which isn't aligned with start of sector */
85 if (reiserfs_block_dev_desc
->block_read(reiserfs_block_dev_desc
->dev
,
86 part_info
.start
+sector
, 1, (unsigned long *)sec_buf
) != 1) {
87 printf (" ** reiserfs_devread() read error\n");
90 memcpy(buf
, sec_buf
+byte_offset
, min(SECTOR_SIZE
-byte_offset
, byte_len
));
91 buf
+=min(SECTOR_SIZE
-byte_offset
, byte_len
);
92 byte_len
-=min(SECTOR_SIZE
-byte_offset
, byte_len
);
96 /* read sector aligned part */
97 block_len
= byte_len
& ~(SECTOR_SIZE
-1);
98 if (reiserfs_block_dev_desc
->block_read(reiserfs_block_dev_desc
->dev
,
99 part_info
.start
+sector
, block_len
/SECTOR_SIZE
, (unsigned long *)buf
) !=
100 block_len
/SECTOR_SIZE
) {
101 printf (" ** reiserfs_devread() read error - block\n");
106 sector
+= block_len
/SECTOR_SIZE
;
108 if ( byte_len
!= 0 ) {
109 /* read rest of data which are not in whole sector */
110 if (reiserfs_block_dev_desc
->block_read(reiserfs_block_dev_desc
->dev
,
111 part_info
.start
+sector
, 1, (unsigned long *)sec_buf
) != 1) {
112 printf (" ** reiserfs_devread() read error - last part\n");
115 memcpy(buf
, sec_buf
, byte_len
);