ericvh / vdiskfs
fork of plan9port
Prototype live venti-backed disk for virtual environments and clouds
| commit 3122: | f49186c08544 |
| parent 3120: | 15ff98eb5446 |
| branch: | default |
| tags: | tip |
grande: initial prototype
Grade is a simple venti back-end that stores blocks in a hierarchy of
files named by the score. At the moment, there is no caching, nor do
we check for collisions or data integrity.
Signed-off-by: Eric Van Hensbergen <ericvh@gmail.com>
4 months ago
| r3122:f49186c08544 | 207 loc | 3.5 KB | embed / history / annotate / raw / |
|---|
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 | /*
* Grande - Venti File System Backend
* Copyright (c) 2009
*/
/* Based on ro.c Copyright (c) 2004 Russ Cox */
#include <u.h>
#include <libc.h>
#include <venti.h>
#include <thread.h>
#include <libsec.h>
#ifndef _UNISTD_H_
#pragma varargck type "F" VtFcall*
#pragma varargck type "T" void
#endif
VtConn *z;
int verbose;
char *stowage;
enum
{
STACK = 8192,
MAXNAMELEN = 256,
STOWDEPTH = 2,
};
void
usage(void)
{
fprint(2, "usage: venti/grande [-v] [-a address] [-s stowage]\n");
threadexitsall("usage");
}
int
makedir(char *s, int mode)
{
int f;
if(access(s, AEXIST) == 0){
fprint(2, "mkdir: %s already exists\n", s);
return -1;
}
f = create(s, OREAD, DMDIR | mode);
if(f < 0){
fprint(2, "mkdir: can't create %s: %r\n", s);
return -1;
}
close(f);
return 0;
}
void
mkdirp(char *s, int mode)
{
char *p;
for(p=strchr(s+1, '/'); p; p=strchr(p+1, '/')){
*p = 0;
if(access(s, AEXIST) != 0 && makedir(s, mode) < 0)
return;
*p = '/';
}
if(access(s, AEXIST) != 0)
makedir(s, mode);
}
void
readthread(void *v)
{
VtReq *r;
int pathlen = strlen(stowage) + (STOWDEPTH * 3) + VtScoreSize;
char *filepath = malloc(pathlen);
uchar *buf;
int fd, n, sz;
char err[ERRMAX];
r = v;
snprint(filepath, pathlen, "%s/%2.2x/%2.2x/%V", stowage, r->tx.score[0], r->tx.score[1], r->tx.score);
fd = open(filepath, OREAD);
if(fd < 0) {
r->rx.msgtype = VtRerror;
rerrstr(err, sizeof err);
r->rx.error = vtstrdup(err);
goto out;
}
sz = r->tx.count;
buf = malloc(sz);
n = read(fd, buf, sz);
if(n < 0) {
r->rx.msgtype = VtRerror;
rerrstr(err, sizeof err);
r->rx.error = vtstrdup(err);
free(buf);
goto out;
}
r->rx.data = packetforeign(buf, n, free, buf);
close(fd);
if(verbose)
fprint(2, "-> %F\n", &r->rx);
out:
vtrespond(r);
}
void
writethread(void *v)
{
VtReq *r;
int pathlen = strlen(stowage) + (STOWDEPTH * 3) + VtScoreSize;
char *filepath = malloc(pathlen);
uchar *buf;
int fd, sz;
r = v;
packetsha1(r->tx.data, r->rx.score);
snprint(filepath, pathlen, "%s/%2.2x/%2.2x", stowage, r->rx.score[0], r->rx.score[1], r->rx.score);
mkdirp(filepath, 0777);
snprint(filepath, pathlen, "%s/%2.2x/%2.2x/%V", stowage, r->rx.score[0], r->rx.score[1], r->rx.score);
fd = create(filepath, OWRITE, 0444|OEXCL);
if(fd < 0) {
/* File already exists - TODO: Check it */
goto end;
}
sz = packetsize(r->tx.data);
buf = malloc(sz);
packetcopy(r->tx.data, buf, 0, sz);
write(fd, buf, sz);
close(fd);
free(buf);
end:
if(verbose)
fprint(2, "-> %F\n", &r->rx);
free(filepath);
vtrespond(r);
}
void
threadmain(int argc, char **argv)
{
VtReq *r;
VtSrv *srv;
char *address;
fmtinstall('F', vtfcallfmt);
fmtinstall('V', vtscorefmt);
address = "tcp!*!venti";
stowage = "/tmp/stow";
ARGBEGIN{
case 'v':
verbose++;
break;
case 'a':
address = EARGF(usage());
break;
case 's':
stowage = EARGF(usage());
break;
default:
usage();
}ARGEND
/*
* check if stowage exists, else create it
*/
mkdirp(stowage, 0777);
srv = vtlisten(address);
if(srv == nil)
sysfatal("vtlisten %s: %r", address);
while((r = vtgetreq(srv)) != nil){
r->rx.msgtype = r->tx.msgtype+1;
if(verbose)
fprint(2, "<- %F\n", &r->tx);
switch(r->tx.msgtype){
case VtTping:
break;
case VtTgoodbye:
break;
case VtTread:
threadcreate(readthread, r, 16384);
continue;
case VtTwrite:
threadcreate(writethread, r, 16384);
continue;
case VtTsync:
break;
}
if(verbose)
fprint(2, "-> %F\n", &r->rx);
vtrespond(r);
}
threadexitsall(nil);
}
|
