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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
#include "logfsos.h"
#include "logfs.h"
#include "local.h"
enum {
ThrowAway,
Keep,
Repack,
Error,
};
#define setaction(a) if(*actionp < (a)) *actionp = a
#define REPACK setaction(Repack)
#define KEEP setaction(Keep)
#define OPTCOPYEX(name, etag, stag) \
if(e->etag != s->stag) { \
s->stag = e->etag; \
REPACK; \
}
#define OPTSTRCOPYEX(name, etag, stag) \
if(strcmp(e->etag, s->stag) != 0) { \
s->stag = e->etag; \
REPACK; \
}
#define OPTCOPY(name, tag, sunion) OPTCOPYEX(name, tag, u.sunion.tag)
#define OPTSTRCOPY(name, tag, sunion) OPTSTRCOPYEX(name, tag, u.sunion.tag)
static char *
sweepcreate(LogfsServer *server, LogMessage *s, int *actionp)
{
Entry *pe, *e;
e = logfspathmapfinde(server->pathmap, s->u.create.newpath);
if(e == nil)
/* file no longer exists */
return nil;
pe = logfspathmapfinde(server->pathmap, s->path);
if(pe == nil)
/* file exists but parent doesn't - not good, but can continue */
return "parent missing";
if((pe->perm & DMDIR) == 0 || (e->perm & DMDIR) != (s->u.create.perm & DMDIR))
/* parent must be a directory, and
* the directory mode cannot change
*/
return logfseinternal;
if((e->perm & DMDIR) == 0) {
OPTCOPYEX("cvers", u.file.cvers, u.create.cvers);
}
OPTSTRCOPY("name", name, create);
OPTCOPY("mtime", mtime, create);
OPTCOPY("perm", perm, create);
OPTSTRCOPY("uid", uid, create);
OPTSTRCOPY("gid", gid, create);
KEEP;
return nil;
}
static char *
sweepwrite(LogfsServer *server, LogMessage *s, int readoffset, Entry **ep, int *trimp, int *actionp)
{
Entry *e;
Extent extent;
Extent *ext;
*ep = nil;
e = logfspathmapfinde(server->pathmap, s->path);
if(e == nil)
/* gone, gone */
return nil;
if(e->perm & DMDIR)
return logfseinternal;
if(e->u.file.cvers != s->u.write.cvers)
/* trunced more recently */
return nil;
extent.min = s->u.write.offset;
extent.max = extent.min + s->u.write.count;
extent.flashaddr = s->u.write.flashaddr;
ext = logfsextentlistmatch(e->u.file.extent, &extent);
if(ext == nil)
return nil;
if(s->u.write.data) {
/*
* trim the front of the data so that when fixing up extents,
* flashaddr refers to the first byte
*/
int offset;
logfsflashaddr2o(server, ext->flashaddr, &offset);
*trimp = offset - readoffset;
*ep = e;
}
KEEP;
return nil;
}
typedef struct FixupState {
LogfsServer *server;
int oldoffset;
u32int newflashaddr;
} FixupState;
static int
fixup(void *magic, Extent *e)
{
FixupState *state = magic;
int offset;
logfsflashaddr2o(state->server, e->flashaddr, &offset);
e->flashaddr = state->newflashaddr + (offset - state->oldoffset);
return 1;
}
static char *
sweepblock(LogfsServer *server, uchar *buf)
{
char *errmsg;
LogSegment *active = server->activelog;
LogSegment *swept = server->sweptlog;
int pagesize, ppb, page;
LogfsLowLevel *ll = server->ll;
LogfsLowLevelReadResult llrr;
int markedbad;
long oblock;
if(active == nil)
return nil;
if(swept == nil) {
errmsg = logfslogsegmentnew(server, loggensucc(active->gen), &server->sweptlog);
if(errmsg)
return errmsg;
swept = server->sweptlog;
}
/*
* if this is last block in the active log, flush it, so that the read of the last page works
*/
if(active->unsweptblockindex == active->curblockindex)
logfslogsegmentflush(server, 1);
ppb = (1 << ll->l2pagesperblock);
pagesize = (1 << ll->l2pagesize);
for(page = 0; page < ppb; page++) {
uchar *p, *bufend;
errmsg = (*ll->readpagerange)(ll, buf, active->blockmap[active->unsweptblockindex], page, 0, pagesize, &llrr);
if(errmsg)
goto fail;
if(llrr != LogfsLowLevelReadResultOk)
logfsserverreplacelogblock(server, active, active->unsweptblockindex);
p = buf;
if(*p == 0xff)
break;
bufend = p + pagesize;
while(p < bufend) {
int action;
uint size;
LogMessage s;
Entry *e;
int trim;
size = logfsconvM2S(p, bufend - p, &s);
if(size == 0)
return "parse failure";
if(server->trace > 1) {
print("A>> ");
logfsdumpS(&s);
print("\n");
}
if(s.type == LogfsLogTend)
break;
action = ThrowAway;
switch(s.type) {
case LogfsLogTstart:
break;
case LogfsLogTcreate:
errmsg = sweepcreate(server, &s, &action);
break;
case LogfsLogTremove:
/* always obsolete; might check that path really doesn't exist */
break;
case LogfsLogTtrunc:
/* always obsolete, unless collecting out of order */
break;
case LogfsLogTwrite:
errmsg = sweepwrite(server, &s, s.u.write.data ? s.u.write.data - buf : 0, &e, &trim, &action);
break;
case LogfsLogTwstat:
/* always obsolete, unless collecting out of order */
break;
default:
return "bad tag in log page";
}
if(action == Error)
return errmsg;
if(errmsg)
print("bad sweep: %s\n", errmsg);
if(action == Keep)
action = Repack; /* input buffer has been wrecked, so can't just copy it */
if(action == Keep) {
/* write 'size' bytes to log */
errmsg = logfslogbytes(server, 0, p, size);
if(errmsg)
goto fail;
}
else if(action == Repack) {
/* TODO - handle writes */
if(s.type == LogfsLogTwrite && s.u.write.data) {
FixupState state;
errmsg = logfslogwrite(server, 0, s.path, s.u.write.offset + trim, s.u.write.count - trim,
s.u.write.mtime, s.u.write.cvers,
s.u.write.muid, s.u.write.data + trim, &state.newflashaddr);
if(errmsg == nil && s.u.write.data != nil) {
Extent extent;
/* TODO - deal with a failure to write the changes */
state.oldoffset = s.u.write.data - buf + trim;
state.server = server;
extent.min = s.u.write.offset;
extent.max = extent.min + s.u.write.count;
extent.flashaddr = s.u.write.flashaddr;
logfsextentlistmatchall(e->u.file.extent, fixup, &state, &extent);
}
}
else
errmsg = logfslog(server, 0, &s);
if(errmsg)
goto fail;
}
p += size;
}
}
/*
* this log block is no longer needed
*/
oblock = active->blockmap[active->unsweptblockindex++];
errmsg = logfsbootfettleblock(server->lb, oblock, LogfsTnone, ~0, &markedbad);
if(errmsg)
goto fail;
if(active->unsweptblockindex > active->curblockindex) {
/*
* the activelog is now empty, so make the sweptlog the active one
*/
logfslogsegmentfree(&active);
server->activelog = swept;
server->sweptlog = nil;
swept->dirty = 0;
}
return nil;
fail:
return errmsg;
}
char *
logfsserverlogsweep(LogfsServer *server, int justone, int *didsomething)
{
uchar *buf;
char *errmsg;
/*
* TODO - is it even worth doing?
*/
*didsomething = 0;
if(!server->activelog->dirty)
return nil;
buf = logfsrealloc(nil, (1 << server->ll->l2pagesize));
if(buf == nil)
return Enomem;
errmsg = nil;
while(server->activelog->unsweptblockindex <= server->activelog->curblockindex) {
errmsg = sweepblock(server, buf);
if(errmsg)
break;
if(server->sweptlog == nil || justone)
break;
}
logfsfreemem(buf);
*didsomething = 1;
return errmsg;
}
|