1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <common.h>
22#include <config.h>
23#include <reiserfs.h>
24
25#include "reiserfs_private.h"
26
27static block_dev_desc_t *reiserfs_block_dev_desc;
28static disk_partition_t part_info;
29
30
31int reiserfs_set_blk_dev(block_dev_desc_t *rbdd, int part)
32{
33 reiserfs_block_dev_desc = rbdd;
34
35 if (part == 0) {
36
37 part_info.start = 0;
38 part_info.size = rbdd->lba;
39 part_info.blksz = rbdd->blksz;
40 } else {
41 if (get_partition_info (reiserfs_block_dev_desc, part, &part_info)) {
42 return 0;
43 }
44 }
45 return (part_info.size);
46}
47
48
49int reiserfs_devread (int sector, int byte_offset, int byte_len, char *buf)
50{
51 char sec_buf[SECTOR_SIZE];
52 unsigned block_len;
53
54
55
56
57
58
59
60 if (sector < 0
61 || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS))
62 >= part_info.size)) {
63
64 printf (" ** reiserfs_devread() read outside partition\n");
65 return 0;
66 }
67
68
69
70
71 sector += byte_offset >> SECTOR_BITS;
72 byte_offset &= SECTOR_SIZE - 1;
73
74#if defined(DEBUG)
75 printf (" <%d, %d, %d> ", sector, byte_offset, byte_len);
76#endif
77
78
79 if (reiserfs_block_dev_desc == NULL)
80 return 0;
81
82
83 if (byte_offset != 0) {
84
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");
88 return 0;
89 }
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);
93 sector++;
94 }
95
96
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");
102 return 0;
103 }
104 buf+=block_len;
105 byte_len-=block_len;
106 sector+= block_len/SECTOR_SIZE;
107
108 if ( byte_len != 0 ) {
109
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");
113 return 0;
114 }
115 memcpy(buf, sec_buf, byte_len);
116 }
117
118 return 1;
119}
120