aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/venti/part.c
blob: dbf7b8608d9ab713df2e536f4fe0dc9fbef89bd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "stdinc.h"
#include "dat.h"
#include "fns.h"

u32int	maxblocksize;
int	readonly;

Part*
initpart(char *name, int writable)
{
	Part *part;
	Dir *dir;
	int how;

	part = MK(Part);
	part->name = estrdup(name);
	if(!writable && readonly)
		how = OREAD;
	else
		how = ORDWR;
	part->fd = open(name, how);
	if(part->fd < 0){
		if(how == ORDWR)
			part->fd = open(name, OREAD);
		if(part->fd < 0){
			freepart(part);
			seterr(EOk, "can't open partition='%s': %r", name);
			return nil;
		}
		fprint(2, "warning: %s opened for reading only\n", name);
	}
	dir = dirfstat(part->fd);
	if(dir == nil){
		freepart(part);
		seterr(EOk, "can't stat partition='%s': %r", name);
		return nil;
	}
	part->size = dir->length;
	part->blocksize = 0;
	free(dir);
	return part;
}

void
freepart(Part *part)
{
	if(part == nil)
		return;
	close(part->fd);
	free(part->name);
	free(part);
}

void
partblocksize(Part *part, u32int blocksize)
{
	if(part->blocksize)
		sysfatal("resetting partition=%s's block size", part->name);
	part->blocksize = blocksize;
	if(blocksize > maxblocksize)
		maxblocksize = blocksize;
}

int
writepart(Part *part, u64int addr, u8int *buf, u32int n)
{
	long m, mm, nn;

	qlock(&stats.lock);
	stats.diskwrites++;
	stats.diskbwrites += n;
	qunlock(&stats.lock);

	if(addr > part->size || addr + n > part->size){
		seterr(ECorrupt, "out of bounds write to partition='%s'", part->name);
		return -1;
	}
	print("write %s %lud at %llud\n", part->name, n, addr);
	for(nn = 0; nn < n; nn += m){
		mm = n - nn;
		if(mm > MaxIo)
			mm = MaxIo;
		m = pwrite(part->fd, &buf[nn], mm, addr + nn);
		if(m != mm){
			if(m < 0){
				seterr(EOk, "can't write partition='%s': %r", part->name);
				return -1;
			}
			logerr(EOk, "truncated write to partition='%s' n=%ld wrote=%ld", part->name, mm, m);
		}
	}
	return 0;
}

int
readpart(Part *part, u64int addr, u8int *buf, u32int n)
{
	long m, mm, nn;
	int i;

	qlock(&stats.lock);
	stats.diskreads++;
	stats.diskbreads += n;
	qunlock(&stats.lock);

	if(addr > part->size || addr + n > part->size){
		seterr(ECorrupt, "out of bounds read from partition='%s': addr=%lld n=%d size=%lld", part->name, addr, n, part->size);
		return -1;
	}
	print("read %s %lud at %llud\n", part->name, n, addr);
	for(nn = 0; nn < n; nn += m){
		mm = n - nn;
		if(mm > MaxIo)
			mm = MaxIo;
		m = -1;
		for(i=0; i<4; i++) {
			m = pread(part->fd, &buf[nn], mm, addr + nn);
			if(m == mm)
				break;
		}
		if(m != mm){
			if(m < 0){
				seterr(EOk, "can't read partition='%s': %r", part->name);
				return -1;
			}
			logerr(EOk, "warning: truncated read from partition='%s' n=%ld read=%ld", part->name, mm, m);
		}
	}
	return 0;
}