-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.c
163 lines (142 loc) · 3.03 KB
/
log.c
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "log.h"
#include "fs.h"
#include "file.h"
#include "monitor.h"
#include "mem.h"
#include "buf.h"
struct log log;
static void recover_from_log(void);
static void commit();
void initlog(void)
{
if (sizeof(struct logheader) >= BSIZE)
PANIC("initlog: too big header");
struct superblock sb;
readsb(ROOTDEV, &sb);
log.start = sb.size - sb.nlog;
log.size = sb.nlog;
log.dev = ROOTDEV;
recover_from_log();
}
static void install_trans(void)
{
int tail;
for (tail = 0; tail < log.lh.n; tail++)
{
struct buf *lbuf = bread(log.dev, log.start+tail+1);
struct buf *dbuf = bread(log.dev, log.lh.sector[tail]);
memcpy(dbuf->data, lbuf->data, BSIZE);
bwrite(dbuf);
brelse(lbuf);
brelse(dbuf);
}
}
static void read_head(void)
{
struct buf *buf = bread(log.dev, log.start);
struct logheader *lh = (struct logheader*)(buf->data);
int i;
log.lh.n = lh->n;
for (i = 0; i < log.lh.n; i++)
{
log.lh.sector[i] = lh->sector[i];
}
brelse(buf);
}
static void write_head(void)
{
struct buf *buf = bread(log.dev, log.start);
struct logheader *hb = (struct logheader *)(buf->data);
int i;
hb->n = log.lh.n;
for (i = 0; i < log.lh.n; i++)
{
hb->sector[i] = log.lh.sector[i];
}
bwrite(buf);
brelse(buf);
}
static void recover_from_log(void)
{
read_head();
install_trans();
log.lh.n = 0;
write_head();
}
void begin_op(void)
{
while (1)
{
if (log.committing)
{
continue;
}
else if (log.lh.n + (log.outstanding+1)*MAXOPBLOCKS > LOGSIZE)
{
continue;
}
else
{
log.outstanding += 1;
break;
}
}
}
void end_op(void)
{
int do_commint = 0;
log.outstanding -= 1;
if (log.committing)
PANIC("log.committing");
if (log.outstanding == 0)
{
do_commint = 1;
log.committing = 1;
}
if (do_commint)
{
commit();
log.committing = 0;
}
}
static void write_log(void)
{
int tail;
for (tail = 0; tail < log.lh.n; tail++)
{
struct buf *to = bread(log.dev, log.start+tail+1);
struct buf *from = bread(log.dev, log.lh.sector[tail]);
memcpy(to->data, from->data, BSIZE);
bwrite(to);
brelse(from);
brelse(to);
}
}
static void commit()
{
if (log.lh.n > 0)
{
write_log();
write_head();
install_trans();
log.lh.n = 0;
write_head();
}
}
void
log_write(struct buf *b)
{
int i;
if (log.lh.n >= LOGSIZE || log.lh.n >= log.size - 1)
PANIC("too big a transaction");
if (log.outstanding < 1)
PANIC("log_write outside of trans");
for (i = 0; i < log.lh.n; i++) {
if (log.lh.sector[i] == b->sector) // log absorbtion
break;
}
log.lh.sector[i] = b->sector;
if (i == log.lh.n)
log.lh.n++;
b->flags |= BUF_DIRTY; // prevent eviction
}