Blob


1 /*
2 * TO DO:
3 * - gc of file systems (not going to do just yet?)
4 * - statistics file
5 * - configure on amsterdam
6 */
8 #include <u.h>
9 #include <libc.h>
10 #include <bio.h>
11 #include <ip.h>
12 #include <thread.h>
13 #include <libsec.h>
14 #include <sunrpc.h>
15 #include <nfs3.h>
16 #include <diskfs.h>
17 #include <venti.h>
18 #include "nfs3srv.h"
20 #define trace if(!tracecalls){}else print
22 typedef struct Ipokay Ipokay;
23 typedef struct Config Config;
24 typedef struct Ctree Ctree;
25 typedef struct Cnode Cnode;
27 struct Ipokay
28 {
29 int okay;
30 uchar ip[IPaddrlen];
31 uchar mask[IPaddrlen];
32 };
34 struct Config
35 {
36 Ipokay *ok;
37 uint nok;
38 ulong mtime;
39 Ctree *ctree;
40 };
42 char *addr;
43 int blocksize;
44 int cachesize;
45 Config config;
46 char *configfile;
47 int encryptedhandles = 1;
48 Channel *nfschan;
49 Channel *mountchan;
50 Channel *timerchan;
51 Nfs3Handle root;
52 SunSrv *srv;
53 int tracecalls;
54 VtCache *vcache;
55 VtConn *z;
57 void cryptinit(void);
58 void timerthread(void*);
59 void timerproc(void*);
61 extern void handleunparse(Fsys*, Nfs3Handle*, Nfs3Handle*, int);
62 extern Nfs3Status handleparse(Nfs3Handle*, Fsys**, Nfs3Handle*, int);
64 Nfs3Status logread(Cnode*, u32int, u64int, uchar**, u32int*, u1int*);
65 Nfs3Status refreshdiskread(Cnode*, u32int, u64int, uchar**, u32int*, u1int*);
66 Nfs3Status refreshconfigread(Cnode*, u32int, u64int, uchar**, u32int*, u1int*);
68 int readconfigfile(Config *cp);
69 void setrootfid(void);
70 int ipokay(uchar *ip, ushort port);
72 u64int unittoull(char*);
74 void
75 usage(void)
76 {
77 fprint(2, "usage: vnfs [-LLRVir] [-a addr] [-b blocksize] [-c cachesize] configfile\n");
78 threadexitsall("usage");
79 }
81 void
82 threadmain(int argc, char **argv)
83 {
84 fmtinstall('B', sunrpcfmt);
85 fmtinstall('C', suncallfmt);
86 fmtinstall('F', vtfcallfmt);
87 fmtinstall('H', encodefmt);
88 fmtinstall('I', eipfmt);
89 fmtinstall('V', vtscorefmt);
90 sunfmtinstall(&nfs3prog);
91 sunfmtinstall(&nfsmount3prog);
93 addr = "udp!*!2049";
94 blocksize = 8192;
95 cachesize = 400;
96 srv = sunsrv();
97 srv->ipokay = ipokay;
98 cryptinit();
100 ARGBEGIN{
101 default:
102 usage();
103 case 'E':
104 encryptedhandles = 0;
105 break;
106 case 'L':
107 if(srv->localonly == 0)
108 srv->localonly = 1;
109 else
110 srv->localparanoia = 1;
111 break;
112 case 'R':
113 srv->chatty++;
114 break;
115 case 'T':
116 tracecalls = 1;
117 break;
118 case 'V':
119 if(chattyventi++)
120 vttracelevel++;
121 break;
122 case 'a':
123 addr = EARGF(usage());
124 break;
125 case 'b':
126 blocksize = unittoull(EARGF(usage()));
127 break;
128 case 'c':
129 cachesize = unittoull(EARGF(usage()));
130 break;
131 case 'i':
132 insecure = 1;
133 break;
134 case 'r':
135 srv->alwaysreject++;
136 break;
137 }ARGEND
139 if(argc != 1)
140 usage();
142 if((z = vtdial(nil)) == nil)
143 sysfatal("vtdial: %r");
144 if(vtconnect(z) < 0)
145 sysfatal("vtconnect: %r");
146 if((vcache = vtcachealloc(z, blocksize*cachesize)) == nil)
147 sysfatal("vtcache: %r");
149 configfile = argv[0];
150 if(readconfigfile(&config) < 0)
151 sysfatal("readConfig: %r");
152 setrootfid();
154 nfschan = chancreate(sizeof(SunMsg*), 0);
155 mountchan = chancreate(sizeof(SunMsg*), 0);
156 timerchan = chancreate(sizeof(void*), 0);
158 if(sunsrvudp(srv, addr) < 0)
159 sysfatal("starting server: %r");
161 sunsrvthreadcreate(srv, nfs3proc, nfschan);
162 sunsrvthreadcreate(srv, mount3proc, mountchan);
163 sunsrvthreadcreate(srv, timerthread, nil);
164 proccreate(timerproc, nil, 32768);
166 sunsrvprog(srv, &nfs3prog, nfschan);
167 sunsrvprog(srv, &nfsmount3prog, mountchan);
169 threadexits(nil);
172 #define TWID64 ((u64int)~(u64int)0)
174 u64int
175 unittoull(char *s)
177 char *es;
178 u64int n;
180 if(s == nil)
181 return TWID64;
182 n = strtoul(s, &es, 0);
183 if(*es == 'k' || *es == 'K'){
184 n *= 1024;
185 es++;
186 }else if(*es == 'm' || *es == 'M'){
187 n *= 1024*1024;
188 es++;
189 }else if(*es == 'g' || *es == 'G'){
190 n *= 1024*1024*1024;
191 es++;
192 }else if(*es == 't' || *es == 'T'){
193 n *= 1024*1024;
194 n *= 1024*1024;
196 if(*es != '\0')
197 return TWID64;
198 return n;
201 /*
202 * Handles.
204 * We store all the state about which file a client is accessing in
205 * the handle, so that we don't have to maintain any per-client state
206 * ourselves. In order to avoid leaking handles or letting clients
207 * create arbitrary handles, we sign and encrypt each handle with
208 * AES using a key selected randomly when the server starts.
209 * Thus, handles cannot be used across sessions.
211 * The decrypted handles begin with the following header:
213 * sessid[8] random session id chosen at start time
214 * len[4] length of handle that follows
216 * If we're pressed for space in the rest of the handle, we could
217 * probably reduce the amount of sessid bytes. Note that the sessid
218 * bytes must be consistent during a run of vnfs, or else some
219 * clients (e.g., Linux 2.4) eventually notice that successive TLookups
220 * return different handles, and they return "Stale NFS file handle"
221 * errors to system calls in response (even though we never sent
222 * that error!).
224 * Security woes aside, the fact that we have to shove everything
225 * into the handles is quite annoying. We have to encode, in 40 bytes:
227 * - position in the synthesized config tree
228 * - enough of the path to do glob matching
229 * - position in an archived file system image
231 * and the handles need to be stable across changes in the config file
232 * (though not across server restarts since encryption screws
233 * that up nicely).
235 * We encode each of the first two as a 10-byte hash that is
236 * the first half of a SHA1 hash.
237 */
239 enum
241 SessidSize = 8,
242 HeaderSize = SessidSize+4,
243 MaxHandleSize = Nfs3MaxHandleSize - HeaderSize
244 };
246 AESstate aesstate;
247 uchar sessid[SessidSize];
249 static void
250 hencrypt(Nfs3Handle *h)
252 uchar *p;
253 AESstate aes;
255 /*
256 * root handle has special encryption - a single 0 byte - so that it
257 * never goes stale.
258 */
259 if(h->len == root.len && memcmp(h->h, root.h, root.len) == 0){
260 h->h[0] = 0;
261 h->len = 1;
262 return;
265 if(!encryptedhandles)
266 return;
268 if(h->len > MaxHandleSize){
269 /* oops */
270 fprint(2, "handle too long: %.*lH\n", h->len, h->h);
271 memset(h->h, 'X', Nfs3MaxHandleSize);
272 h->len = Nfs3MaxHandleSize;
273 return;
276 p = h->h;
277 memmove(p+HeaderSize, p, h->len);
278 memmove(p, sessid, SessidSize);
279 *(u32int*)(p+SessidSize) = h->len;
280 h->len += HeaderSize;
282 if(encryptedhandles){
283 while(h->len < MaxHandleSize)
284 h->h[h->len++] = 0;
285 aes = aesstate;
286 aesCBCencrypt(h->h, MaxHandleSize, &aes);
290 static Nfs3Status
291 hdecrypt(Nfs3Handle *h)
293 AESstate aes;
295 if(h->len == 1 && h->h[0] == 0){ /* single 0 byte is root */
296 *h = root;
297 return Nfs3Ok;
300 if(!encryptedhandles)
301 return Nfs3Ok;
303 if(h->len <= HeaderSize)
304 return Nfs3ErrBadHandle;
305 if(encryptedhandles){
306 if(h->len != MaxHandleSize)
307 return Nfs3ErrBadHandle;
308 aes = aesstate;
309 aesCBCdecrypt(h->h, h->len, &aes);
311 if(memcmp(h->h, sessid, SessidSize) != 0)
312 return Nfs3ErrStale; /* give benefit of doubt */
313 h->len = *(u32int*)(h->h+SessidSize);
314 if(h->len >= MaxHandleSize-HeaderSize)
315 return Nfs3ErrBadHandle;
316 memmove(h->h, h->h+HeaderSize, h->len);
317 return Nfs3Ok;
320 void
321 cryptinit(void)
323 uchar key[32], ivec[AESbsize];
324 int i;
326 *(u32int*)sessid = truerand();
327 for(i=0; i<nelem(key); i+=4)
328 *(u32int*)&key[i] = truerand();
329 for(i=0; i<nelem(ivec); i++)
330 ivec[i] = fastrand();
331 setupAESstate(&aesstate, key, sizeof key, ivec);
334 /*
335 * Config file.
337 * The main purpose of the configuration file is to define a tree
338 * in which the archived file system images are mounted.
339 * The tree is stored as Entry structures, defined below.
341 * The configuration file also allows one to define shell-like
342 * glob expressions matching paths that are not to be displayed.
343 * The matched files or directories are shown in directory listings
344 * (could suppress these if we cared) but they cannot be opened,
345 * read, or written, and getattr returns zeroed data.
346 */
347 enum
349 /* sizes used in handles; see nfs server below */
350 CnodeHandleSize = 8,
351 FsysHandleOffset = CnodeHandleSize
352 };
354 /*
355 * Config file tree.
356 */
357 struct Ctree
359 Cnode *root;
360 Cnode *hash[1024];
361 };
363 struct Cnode
365 char *name; /* path element */
366 Cnode *parent; /* in tree */
367 Cnode *nextsib; /* in tree */
368 Cnode *kidlist; /* in tree */
369 Cnode *nexthash; /* in hash list */
371 Nfs3Status (*read)(Cnode*, u32int, u64int, uchar**, u32int*, u1int*); /* synthesized read fn */
373 uchar handle[VtScoreSize]; /* sha1(path to here) */
374 ulong mtime; /* mtime for this directory entry */
376 /* fsys overlay on this node */
377 Fsys *fsys; /* cache of memory structure */
378 Nfs3Handle fsyshandle;
379 int isblackhole; /* walking down keeps you here */
381 /*
382 * mount point info.
383 * if a mount point is inside another file system,
384 * the fsys and fsyshandle above have the old fs info,
385 * the mfsys and mfsyshandle below have the new one.
386 * getattrs must use the old info for consistency.
387 */
388 int ismtpt; /* whether there is an fsys mounted here */
389 uchar fsysscore[VtScoreSize]; /* score of fsys image on venti */
390 char *fsysimage; /* raw disk image */
391 Fsys *mfsys; /* mounted file system (nil until walked) */
392 Nfs3Handle mfsyshandle; /* handle to root of mounted fsys */
394 int mark; /* gc */
395 };
397 static uint
398 dumbhash(uchar *s)
400 return (s[0]<<2)|(s[1]>>6); /* first 10 bits */
403 static Cnode*
404 mkcnode(Ctree *t, Cnode *parent, char *elem, uint elen, char *path, uint plen)
406 uint h;
407 Cnode *n;
409 n = emalloc(sizeof *n + elen+1);
410 n->name = (char*)(n+1);
411 memmove(n->name, elem, elen);
412 n->name[elen] = 0;
413 n->parent = parent;
414 if(parent){
415 n->nextsib = parent->kidlist;
416 parent->kidlist = n;
418 n->kidlist = nil;
419 sha1((uchar*)path, plen, n->handle, nil);
420 h = dumbhash(n->handle);
421 n->nexthash = t->hash[h];
422 t->hash[h] = n;
424 return n;
427 void
428 markctree(Ctree *t)
430 int i;
431 Cnode *n;
433 for(i=0; i<nelem(t->hash); i++)
434 for(n=t->hash[i]; n; n=n->nexthash)
435 if(n->name[0] != '+')
436 n->mark = 1;
439 int
440 refreshdisk(void)
442 int i;
443 Cnode *n;
444 Ctree *t;
446 t = config.ctree;
447 for(i=0; i<nelem(t->hash); i++)
448 for(n=t->hash[i]; n; n=n->nexthash){
449 if(n->mfsys)
450 disksync(n->mfsys->disk);
451 if(n->fsys)
452 disksync(n->fsys->disk);
454 return 0;
457 void
458 sweepctree(Ctree *t)
460 int i;
461 Cnode *n;
463 /* just zero all the garbage and leave it linked into the tree */
464 for(i=0; i<nelem(t->hash); i++){
465 for(n=t->hash[i]; n; n=n->nexthash){
466 if(!n->mark)
467 continue;
468 n->fsys = nil;
469 free(n->fsysimage);
470 n->fsysimage = nil;
471 memset(n->fsysscore, 0, sizeof n->fsysscore);
472 n->mfsys = nil;
473 n->ismtpt = 0;
474 memset(&n->fsyshandle, 0, sizeof n->fsyshandle);
475 memset(&n->mfsyshandle, 0, sizeof n->mfsyshandle);
480 static Cnode*
481 cnodewalk(Cnode *n, char *name, uint len, int markokay)
483 Cnode *nn;
485 for(nn=n->kidlist; nn; nn=nn->nextsib)
486 if(strncmp(nn->name, name, len) == 0 && nn->name[len] == 0)
487 if(!nn->mark || markokay)
488 return nn;
489 return nil;
492 Cnode*
493 ctreewalkpath(Ctree *t, char *name, ulong createmtime)
495 Cnode *n, *nn;
496 char *p, *nextp;
498 n = t->root;
499 p = name;
500 for(; *p; p=nextp){
501 n->mark = 0;
502 assert(*p == '/');
503 p++;
504 nextp = strchr(p, '/');
505 if(nextp == nil)
506 nextp = p+strlen(p);
507 if((nn = cnodewalk(n, p, nextp-p, 1)) == nil){
508 if(createmtime == 0)
509 return nil;
510 nn = mkcnode(t, n, p, nextp-p, name, nextp-name);
511 nn->mtime = createmtime;
513 if(nn->mark)
514 nn->mark = 0;
515 n = nn;
517 n->mark = 0;
518 return n;
521 Ctree*
522 mkctree(void)
524 Ctree *t;
526 t = emalloc(sizeof *t);
527 t->root = mkcnode(t, nil, "", 0, "", 0);
529 ctreewalkpath(t, "/+log", time(0))->read = logread;
530 ctreewalkpath(t, "/+refreshdisk", time(0))->read = refreshdiskread;
531 ctreewalkpath(t, "/+refreshconfig", time(0))->read = refreshconfigread;
533 return t;
536 Cnode*
537 ctreemountfsys(Ctree *t, char *path, ulong time, uchar *score, char *file)
539 Cnode *n;
541 if(time == 0)
542 time = 1;
543 n = ctreewalkpath(t, path, time);
544 if(score){
545 if(n->ismtpt && (n->fsysimage || memcmp(n->fsysscore, score, VtScoreSize) != 0)){
546 free(n->fsysimage);
547 n->fsysimage = nil;
548 n->fsys = nil; /* leak (might be other refs) */
550 memmove(n->fsysscore, score, VtScoreSize);
551 }else{
552 if(n->ismtpt && (n->fsysimage==nil || strcmp(n->fsysimage, file) != 0)){
553 free(n->fsysimage);
554 n->fsysimage = nil;
555 n->fsys = nil; /* leak (might be other refs) */
557 n->fsysimage = emalloc(strlen(file)+1);
558 strcpy(n->fsysimage, file);
560 n->ismtpt = 1;
561 return n;
564 Cnode*
565 cnodebyhandle(Ctree *t, uchar *p)
567 int h;
568 Cnode *n;
570 h = dumbhash(p);
571 for(n=t->hash[h]; n; n=n->nexthash)
572 if(memcmp(n->handle, p, CnodeHandleSize) == 0)
573 return n;
574 return nil;
577 static int
578 parseipandmask(char *s, uchar *ip, uchar *mask)
580 char *p, *q;
582 p = strchr(s, '/');
583 if(p)
584 *p++ = 0;
585 if(parseip(ip, s) == ~0UL)
586 return -1;
587 if(p == nil)
588 memset(mask, 0xFF, IPaddrlen);
589 else{
590 if(isdigit((uchar)*p) && strtol(p, &q, 10)>=0 && *q==0)
591 *--p = '/';
592 if(parseipmask(mask, p) == ~0UL)
593 return -1;
594 if(*p != '/')
595 *--p = '/';
597 /*fprint(2, "parseipandmask %s => %I %I\n", s, ip, mask); */
598 return 0;
601 static int
602 parsetime(char *s, ulong *time)
604 ulong x;
605 char *p;
606 int i;
607 Tm tm;
609 /* decimal integer is seconds since 1970 */
610 x = strtoul(s, &p, 10);
611 if(x > 0 && *p == 0){
612 *time = x;
613 return 0;
616 /* otherwise expect yyyy/mmdd/hhmm */
617 if(strlen(s) != 14 || s[4] != '/' || s[9] != '/')
618 return -1;
619 for(i=0; i<4; i++)
620 if(!isdigit((uchar)s[i]) || !isdigit((uchar)s[i+5]) || !isdigit((uchar)s[i+10]))
621 return -1;
622 memset(&tm, 0, sizeof tm);
623 tm.year = atoi(s)-1900;
624 if(tm.year < 0 || tm.year > 200)
625 return -1;
626 tm.mon = (s[5]-'0')*10+s[6]-'0' - 1;
627 if(tm.mon < 0 || tm.mon > 11)
628 return -1;
629 tm.mday = (s[7]-'0')*10+s[8]-'0';
630 if(tm.mday < 0 || tm.mday > 31)
631 return -1;
632 tm.hour = (s[10]-'0')*10+s[11]-'0';
633 if(tm.hour < 0 || tm.hour > 23)
634 return -1;
635 tm.min = (s[12]-'0')*10+s[13]-'0';
636 if(tm.min < 0 || tm.min > 59)
637 return -1;
638 strcpy(tm.zone, "XXX"); /* anything but GMT */
639 if(0){
640 print("tm2sec %d/%d/%d/%d/%d\n",
641 tm.year, tm.mon, tm.mday, tm.hour, tm.min);
643 *time = tm2sec(&tm);
644 if(0) print("time %lud\n", *time);
645 return 0;
649 int
650 readconfigfile(Config *cp)
652 char *f[10], *image, *p, *pref, *q, *name;
653 int nf, line;
654 uchar scorebuf[VtScoreSize], *score;
655 ulong time;
656 Biobuf *b;
657 Config c;
658 Dir *dir;
660 name = configfile;
661 c = *cp;
662 if((dir = dirstat(name)) == nil)
663 return -1;
664 if(c.mtime == dir->mtime){
665 free(dir);
666 return 0;
668 c.mtime = dir->mtime;
669 free(dir);
670 if((b = Bopen(name, OREAD)) == nil)
671 return -1;
673 /*
674 * Reuse old tree, garbage collecting entries that
675 * are not mentioned in the new config file.
676 */
677 if(c.ctree == nil)
678 c.ctree = mkctree();
680 markctree(c.ctree);
681 c.ok = nil;
682 c.nok = 0;
684 line = 0;
685 for(; (p=Brdstr(b, '\n', 1)) != nil; free(p)){
686 line++;
687 if((q = strchr(p, '#')) != nil)
688 *q = 0;
689 nf = tokenize(p, f, nelem(f));
690 if(nf == 0)
691 continue;
692 if(strcmp(f[0], "mount") == 0){
693 if(nf != 4){
694 werrstr("syntax error: mount /path /dev|score mtime");
695 goto badline;
697 if(f[1][0] != '/'){
698 werrstr("unrooted path %s", f[1]);
699 goto badline;
701 score = nil;
702 image = nil;
703 if(f[2][0] == '/'){
704 if(access(f[2], AEXIST) < 0){
705 werrstr("image %s does not exist", f[2]);
706 goto badline;
708 image = f[2];
709 }else{
710 if(vtparsescore(f[2], &pref, scorebuf) < 0){
711 werrstr("bad score %s", f[2]);
712 goto badline;
714 score = scorebuf;
716 if(parsetime(f[3], &time) < 0){
717 fprint(2, "%s:%d: bad time %s\n", name, line, f[3]);
718 time = 1;
720 ctreemountfsys(c.ctree, f[1], time, score, image);
721 continue;
723 if(strcmp(f[0], "allow") == 0 || strcmp(f[0], "deny") == 0){
724 if(nf != 2){
725 werrstr("syntax error: allow|deny ip[/mask]");
726 goto badline;
728 c.ok = erealloc(c.ok, (c.nok+1)*sizeof(c.ok[0]));
729 if(parseipandmask(f[1], c.ok[c.nok].ip, c.ok[c.nok].mask) < 0){
730 werrstr("bad ip[/mask]: %s", f[1]);
731 goto badline;
733 c.ok[c.nok].okay = (strcmp(f[0], "allow") == 0);
734 c.nok++;
735 continue;
737 werrstr("unknown verb '%s'", f[0]);
738 badline:
739 fprint(2, "%s:%d: %r\n", name, line);
741 Bterm(b);
743 sweepctree(c.ctree);
744 free(cp->ok);
745 *cp = c;
746 return 0;
749 int
750 ipokay(uchar *ip, ushort port)
752 int i;
753 uchar ipx[IPaddrlen];
754 Ipokay *ok;
756 for(i=0; i<config.nok; i++){
757 ok = &config.ok[i];
758 maskip(ip, ok->mask, ipx);
759 if(0) fprint(2, "%I & %I = %I (== %I?)\n",
760 ip, ok->mask, ipx, ok->ip);
761 if(memcmp(ipx, ok->ip, IPaddrlen) == 0)
762 return ok->okay;
764 if(config.nok == 0) /* all is permitted */
765 return 1;
766 /* otherwise default is none allowed */
767 return 0;
770 Nfs3Status
771 cnodelookup(Ctree *t, Cnode **np, char *name)
773 Cnode *n, *nn;
775 n = *np;
776 if(n->isblackhole)
777 return Nfs3Ok;
778 if((nn = cnodewalk(n, name, strlen(name), 0)) == nil){
779 if(n->ismtpt || n->fsys){
780 if((nn = cnodewalk(n, "", 0, 1)) == nil){
781 nn = mkcnode(t, n, "", 0, (char*)n->handle, SHA1dlen);
782 nn->isblackhole = 1;
784 nn->mark = 0;
787 if(nn == nil)
788 return Nfs3ErrNoEnt;
789 *np = nn;
790 return Nfs3Ok;
793 Nfs3Status
794 cnodegetattr(Cnode *n, Nfs3Attr *attr)
796 memset(attr, 0, sizeof *attr);
797 if(n->read){
798 attr->type = Nfs3FileReg;
799 attr->mode = 0444;
800 attr->size = 512;
801 attr->nlink = 1;
802 }else{
803 attr->type = Nfs3FileDir;
804 attr->mode = 0555;
805 attr->size = 1024;
806 attr->nlink = 10;
808 attr->fileid = *(u64int*)n->handle;
809 attr->atime.sec = n->mtime;
810 attr->mtime.sec = n->mtime;
811 attr->ctime.sec = n->mtime;
812 return Nfs3Ok;
815 Nfs3Status
816 cnodereaddir(Cnode *n, u32int count, u64int cookie, uchar **pdata, u32int *pcount, u1int *peof)
818 uchar *data, *p, *ep, *np;
819 u64int c;
820 Nfs3Entry ne;
822 n = n->kidlist;
823 c = cookie;
824 for(; c && n; c--)
825 n = n->nextsib;
826 if(n == nil){
827 *pdata = 0;
828 *pcount = 0;
829 *peof = 1;
830 return Nfs3Ok;
833 data = emalloc(count);
834 p = data;
835 ep = data+count;
836 while(n && p < ep){
837 if(n->mark || n->name[0] == '+'){
838 n = n->nextsib;
839 ++cookie;
840 continue;
842 ne.name = n->name;
843 ne.namelen = strlen(n->name);
844 ne.cookie = ++cookie;
845 ne.fileid = *(u64int*)n->handle;
846 if(nfs3entrypack(p, ep, &np, &ne) < 0)
847 break;
848 p = np;
849 n = n->nextsib;
851 *pdata = data;
852 *pcount = p - data;
853 *peof = n==nil;
854 return Nfs3Ok;
857 void
858 timerproc(void *v)
860 for(;;){
861 sleep(60*1000);
862 sendp(timerchan, 0);
866 void
867 timerthread(void *v)
869 for(;;){
870 recvp(timerchan);
871 /* refreshconfig(); */
875 /*
876 * Actually serve the NFS requests. Called from nfs3srv.c.
877 * Each request runs in its own thread (coroutine).
879 * Decrypted handles have the form:
881 * config[20] - SHA1 hash identifying a config tree node
882 * glob[10] - SHA1 hash prefix identifying a glob state
883 * fsyshandle[<=10] - disk file system handle (usually 4 bytes)
884 */
886 /*
887 * A fid represents a point in the file tree.
888 * There are three components, all derived from the handle:
890 * - config tree position (also used to find fsys)
891 * - glob state for exclusions
892 * - file system position
893 */
894 enum
896 HAccess,
897 HAttr,
898 HWalk,
899 HDotdot,
900 HRead
901 };
902 typedef struct Fid Fid;
903 struct Fid
905 Cnode *cnode;
906 Fsys *fsys;
907 Nfs3Handle fsyshandle;
908 };
910 int
911 handlecmp(Nfs3Handle *h, Nfs3Handle *h1)
913 if(h->len != h1->len)
914 return h->len - h1->len;
915 return memcmp(h->h, h1->h, h->len);
918 Nfs3Status
919 handletofid(Nfs3Handle *eh, Fid *fid, int mode)
921 int domount;
922 Cnode *n;
923 Disk *disk, *cdisk;
924 Fsys *fsys;
925 Nfs3Status ok;
926 Nfs3Handle h2, *h, *fh;
928 memset(fid, 0, sizeof *fid);
930 domount = 1;
931 if(mode == HDotdot)
932 domount = 0;
933 /*
934 * Not necessary, but speeds up ls -l /dump/2005
935 * HAttr and HAccess must be handled the same way
936 * because both can be used to fetch attributes.
937 * Acting differently yields inconsistencies at mount points,
938 * and causes FreeBSD ls -l to fail.
939 */
940 if(mode == HAttr || mode == HAccess)
941 domount = 0;
943 /*
944 * Decrypt handle.
945 */
946 h2 = *eh;
947 h = &h2;
948 if((ok = hdecrypt(h)) != Nfs3Ok)
949 return ok;
950 trace("handletofid: decrypted %.*lH\n", h->len, h->h);
951 if(h->len < FsysHandleOffset)
952 return Nfs3ErrBadHandle;
954 /*
955 * Find place in config tree.
956 */
957 if((n = cnodebyhandle(config.ctree, h->h)) == nil)
958 return Nfs3ErrStale;
959 fid->cnode = n;
961 if(n->ismtpt && domount){
962 /*
963 * Open fsys for mount point if needed.
964 */
965 if(n->mfsys == nil){
966 trace("handletofid: mounting %V/%s\n", n->fsysscore, n->fsysimage);
967 if(n->fsysimage){
968 if(strcmp(n->fsysimage, "/dev/null") == 0)
969 return Nfs3ErrAcces;
970 if((disk = diskopenfile(n->fsysimage)) == nil){
971 fprint(2, "cannot open disk %s: %r\n", n->fsysimage);
972 return Nfs3ErrIo;
974 if((cdisk = diskcache(disk, blocksize, 64)) == nil){
975 fprint(2, "cannot cache disk %s: %r\n", n->fsysimage);
976 diskclose(disk);
978 disk = cdisk;
979 }else{
980 if((disk = diskopenventi(vcache, n->fsysscore)) == nil){
981 fprint(2, "cannot open venti disk %V: %r\n", n->fsysscore);
982 return Nfs3ErrIo;
985 if((fsys = fsysopen(disk)) == nil){
986 fprint(2, "cannot open fsys on %V: %r\n", n->fsysscore);
987 diskclose(disk);
988 return Nfs3ErrIo;
990 n->mfsys = fsys;
991 fsysroot(fsys, &n->mfsyshandle);
994 /*
995 * Use inner handle.
996 */
997 fid->fsys = n->mfsys;
998 fid->fsyshandle = n->mfsyshandle;
999 }else{
1001 * Use fsys handle from tree or from handle.
1002 * This assumes that fsyshandle was set by fidtohandle
1003 * earlier, so it's not okay to reuse handles (except the root)
1004 * across sessions. The encryption above makes and
1005 * enforces the same restriction, so this is okay.
1007 fid->fsys = n->fsys;
1008 fh = &fid->fsyshandle;
1009 if(n->isblackhole){
1010 fh->len = h->len-FsysHandleOffset;
1011 memmove(fh->h, h->h+FsysHandleOffset, fh->len);
1012 }else
1013 *fh = n->fsyshandle;
1014 trace("handletofid: fsyshandle %.*lH\n", fh->len, fh->h);
1018 * TO DO (maybe): some sort of path restriction here.
1020 trace("handletofid: cnode %s fsys %p fsyshandle %.*lH\n",
1021 n->name, fid->fsys, fid->fsyshandle.len, fid->fsyshandle.h);
1022 return Nfs3Ok;
1025 void
1026 _fidtohandle(Fid *fid, Nfs3Handle *h)
1028 Cnode *n;
1030 n = fid->cnode;
1032 * Record fsys handle in n, don't bother sending it to client
1033 * for black holes.
1035 n->fsys = fid->fsys;
1036 if(!n->isblackhole){
1037 n->fsyshandle = fid->fsyshandle;
1038 fid->fsyshandle.len = 0;
1040 memmove(h->h, n->handle, CnodeHandleSize);
1041 memmove(h->h+FsysHandleOffset, fid->fsyshandle.h, fid->fsyshandle.len);
1042 h->len = FsysHandleOffset+fid->fsyshandle.len;
1045 void
1046 fidtohandle(Fid *fid, Nfs3Handle *h)
1048 _fidtohandle(fid, h);
1049 hencrypt(h);
1052 void
1053 setrootfid(void)
1055 Fid fid;
1057 memset(&fid, 0, sizeof fid);
1058 fid.cnode = config.ctree->root;
1059 _fidtohandle(&fid, &root);
1062 void
1063 fsgetroot(Nfs3Handle *h)
1065 *h = root;
1066 hencrypt(h);
1069 Nfs3Status
1070 fsgetattr(SunAuthUnix *au, Nfs3Handle *h, Nfs3Attr *attr)
1072 Fid fid;
1073 Nfs3Status ok;
1075 trace("getattr %.*lH\n", h->len, h->h);
1076 if((ok = handletofid(h, &fid, HAttr)) != Nfs3Ok)
1077 return ok;
1078 if(fid.fsys)
1079 return fsysgetattr(fid.fsys, au, &fid.fsyshandle, attr);
1080 else
1081 return cnodegetattr(fid.cnode, attr);
1085 * Lookup is always the hard part.
1087 Nfs3Status
1088 fslookup(SunAuthUnix *au, Nfs3Handle *h, char *name, Nfs3Handle *nh)
1090 Fid fid;
1091 Cnode *n;
1092 Nfs3Status ok;
1093 Nfs3Handle xh;
1094 int mode;
1096 trace("lookup %.*lH %s\n", h->len, h->h, name);
1098 mode = HWalk;
1099 if(strcmp(name, "..") == 0 || strcmp(name, ".") == 0)
1100 mode = HDotdot;
1101 if((ok = handletofid(h, &fid, mode)) != Nfs3Ok){
1102 nfs3errstr(ok);
1103 trace("lookup: handletofid %r\n");
1104 return ok;
1107 if(strcmp(name, ".") == 0){
1108 fidtohandle(&fid, nh);
1109 return Nfs3Ok;
1113 * Walk down file system and cnode simultaneously.
1114 * If dotdot and file system doesn't move, need to walk
1115 * up cnode. Save the corresponding fsys handles in
1116 * the cnode as we walk down so that we'll have them
1117 * for dotdotting back up.
1119 n = fid.cnode;
1120 if(mode == HWalk){
1122 * Walk down config tree and file system simultaneously.
1124 if((ok = cnodelookup(config.ctree, &n, name)) != Nfs3Ok){
1125 nfs3errstr(ok);
1126 trace("lookup: cnodelookup: %r\n");
1127 return ok;
1129 fid.cnode = n;
1130 if(fid.fsys){
1131 if((ok = fsyslookup(fid.fsys, au, &fid.fsyshandle, name, &xh)) != Nfs3Ok){
1132 nfs3errstr(ok);
1133 trace("lookup: fsyslookup: %r\n");
1134 return ok;
1136 fid.fsyshandle = xh;
1138 }else{
1140 * Walking dotdot. Ick.
1142 trace("lookup dotdot fsys=%p\n", fid.fsys);
1143 if(fid.fsys){
1145 * Walk up file system, then try up config tree.
1147 if((ok = fsyslookup(fid.fsys, au, &fid.fsyshandle, "..", &xh)) != Nfs3Ok){
1148 nfs3errstr(ok);
1149 trace("lookup fsyslookup: %r\n");
1150 return ok;
1152 fid.fsyshandle = xh;
1155 * Usually just go to n->parent.
1157 * If we're in a subtree of the mounted file system that
1158 * isn't represented explicitly by the config tree (instead
1159 * the black hole node represents the entire file tree),
1160 * then we only go to n->parent when we've dotdotted back
1161 * to the right handle.
1163 if(n->parent == nil)
1164 trace("lookup dotdot: no parent\n");
1165 else{
1166 trace("lookup dotdot: parent %.*lH, have %.*lH\n",
1167 n->parent->fsyshandle.len, n->parent->fsyshandle.h,
1168 xh.len, xh.h);
1171 if(n->isblackhole){
1172 if(handlecmp(&n->parent->mfsyshandle, &xh) == 0)
1173 n = n->parent;
1174 }else{
1175 if(n->parent)
1176 n = n->parent;
1178 }else{
1180 * No file system, just walk up.
1182 if(n->parent)
1183 n = n->parent;
1185 fid.fsys = n->fsys;
1186 if(!n->isblackhole)
1187 fid.fsyshandle = n->fsyshandle;
1188 fid.cnode = n;
1190 fidtohandle(&fid, nh);
1191 return Nfs3Ok;
1194 Nfs3Status
1195 fsaccess(SunAuthUnix *au, Nfs3Handle *h, u32int want, u32int *got, Nfs3Attr *attr)
1197 Fid fid;
1198 Nfs3Status ok;
1200 trace("access %.*lH 0x%ux\n", h->len, h->h, want);
1201 if((ok = handletofid(h, &fid, HAccess)) != Nfs3Ok)
1202 return ok;
1203 if(fid.fsys)
1204 return fsysaccess(fid.fsys, au, &fid.fsyshandle, want, got, attr);
1205 *got = want & (Nfs3AccessRead|Nfs3AccessLookup|Nfs3AccessExecute);
1206 return cnodegetattr(fid.cnode, attr);
1209 Nfs3Status
1210 fsreadlink(SunAuthUnix *au, Nfs3Handle *h, char **link)
1212 Fid fid;
1213 Nfs3Status ok;
1215 trace("readlink %.*lH\n", h->len, h->h);
1216 if((ok = handletofid(h, &fid, HRead)) != Nfs3Ok)
1217 return ok;
1218 if(fid.fsys)
1219 return fsysreadlink(fid.fsys, au, &fid.fsyshandle, link);
1220 *link = 0;
1221 return Nfs3ErrNotSupp;
1224 Nfs3Status
1225 fsreadfile(SunAuthUnix *au, Nfs3Handle *h, u32int count, u64int offset, uchar **data, u32int *pcount, u1int *peof)
1227 Fid fid;
1228 Nfs3Status ok;
1230 trace("readfile %.*lH\n", h->len, h->h);
1231 if((ok = handletofid(h, &fid, HRead)) != Nfs3Ok)
1232 return ok;
1233 if(fid.cnode->read)
1234 return fid.cnode->read(fid.cnode, count, offset, data, pcount, peof);
1235 if(fid.fsys)
1236 return fsysreadfile(fid.fsys, au, &fid.fsyshandle, count, offset, data, pcount, peof);
1237 return Nfs3ErrNotSupp;
1240 Nfs3Status
1241 fsreaddir(SunAuthUnix *au, Nfs3Handle *h, u32int len, u64int cookie, uchar **pdata, u32int *pcount, u1int *peof)
1243 Fid fid;
1244 Nfs3Status ok;
1246 trace("readdir %.*lH\n", h->len, h->h);
1247 if((ok = handletofid(h, &fid, HRead)) != Nfs3Ok)
1248 return ok;
1249 if(fid.fsys)
1250 return fsysreaddir(fid.fsys, au, &fid.fsyshandle, len, cookie, pdata, pcount, peof);
1251 return cnodereaddir(fid.cnode, len, cookie, pdata, pcount, peof);
1254 Nfs3Status
1255 logread(Cnode *n, u32int count, u64int offset, uchar **data, u32int *pcount, u1int *peof)
1257 *pcount = 0;
1258 *peof = 1;
1259 return Nfs3Ok;
1262 Nfs3Status
1263 refreshdiskread(Cnode *n, u32int count, u64int offset, uchar **data, u32int *pcount, u1int *peof)
1265 char buf[128];
1267 if(offset != 0){
1268 *pcount = 0;
1269 *peof = 1;
1270 return Nfs3Ok;
1272 if(refreshdisk() < 0)
1273 snprint(buf, sizeof buf, "refreshdisk: %r\n");
1274 else
1275 strcpy(buf, "ok\n");
1276 *data = emalloc(strlen(buf));
1277 strcpy((char*)*data, buf);
1278 *pcount = strlen(buf);
1279 *peof = 1;
1280 return Nfs3Ok;
1283 Nfs3Status
1284 refreshconfigread(Cnode *n, u32int count, u64int offset, uchar **data, u32int *pcount, u1int *peof)
1286 char buf[128];
1288 if(offset != 0){
1289 *pcount = 0;
1290 *peof = 1;
1291 return Nfs3Ok;
1293 if(readconfigfile(&config) < 0)
1294 snprint(buf, sizeof buf, "readconfig: %r\n");
1295 else
1296 strcpy(buf, "ok\n");
1297 *data = emalloc(strlen(buf));
1298 strcpy((char*)*data, buf);
1299 *pcount = strlen(buf);
1300 *peof = 1;
1301 return Nfs3Ok;