xref: /plan9-contrib/sys/src/cmd/usb/lib/fs.c (revision 6891d8578618fb7ccda4a131c122d4d0e6580c4b)
1 /*
2  * Framework for USB devices that provide a file tree.
3  * The main process (usbd or the driver's main proc)
4  * calls fsinit() to start FS operation.
5  *
6  * One or more drivers call fsstart/fsend to register
7  * or unregister their operations for their subtrees.
8  *
9  * root dir has qids with 0 in high 32 bits.
10  * for other files we keep the device id in there.
11  * The low 32 bits for directories at / must be 0.
12  */
13 #include <u.h>
14 #include <libc.h>
15 #include <thread.h>
16 #include <fcall.h>
17 #include "usb.h"
18 #include "usbfs.h"
19 
20 #undef dprint
21 #define dprint if(usbfsdebug)fprint
22 
23 typedef struct Rpc Rpc;
24 
25 enum
26 {
27 	Nproc = 3,		/* max nb. of cached FS procs */
28 
29 	Nofid = ~0,		/* null value for fid number */
30 	Notag = ~0,		/* null value for tags */
31 	Dietag = 0xdead,		/* fake tag to ask outproc to die */
32 
33 	Stack = 16 * 1024,
34 
35 	/* Fsproc requests */
36 	Run = 0,		/* call f(r) */
37 	Exit,			/* terminate */
38 
39 };
40 
41 struct Rpc
42 {
43 	Fcall	t;
44 	Fcall	r;
45 	Fid*	fid;
46 	int	flushed;
47 	Rpc*	next;
48 	char	data[Bufsize];
49 };
50 
51 int usbfsdebug;
52 
53 char Enotfound[] = "file not found";
54 char Etoosmall[] = "parameter too small";
55 char Eio[] = "i/o error";
56 char Eperm[] = "permission denied";
57 char Ebadcall[] = "unknown fs call";
58 char Ebadfid[] = "fid not found";
59 char Einuse[] = "fid already in use";
60 char Eisopen[] = "it is already open";
61 char Ebadctl[] = "unknown control request";
62 
63 static char *user;
64 static ulong epoch;
65 static ulong msgsize = Msgsize;
66 static int fsfd = -1;
67 static Channel *outc;	/* of Rpc* */
68 
69 static QLock rpclck;	/* protect vars in this block */
70 static Fid *freefids;
71 static Fid *fids;
72 static Rpc *freerpcs;
73 static Rpc *rpcs;
74 
75 static Channel*procc;
76 static Channel*endc;
77 
78 static Usbfs* fsops;
79 
80 static void fsioproc(void*);
81 
82 static void
83 schedproc(void*)
84 {
85 	Channel *proc[Nproc];
86 	int nproc;
87 	Channel *p;
88 
89 	Alt a[] =
90 	{
91 		{procc, &proc[0], CHANSND},
92 		{endc, &p, CHANRCV},
93 		{nil, nil, CHANEND}
94 	};
95 	memset(proc, 0, sizeof(proc));
96 	nproc = 0;
97 	for(;;){
98 		if(nproc == 0){
99 			proc[0] = chancreate(sizeof(Rpc*), 0);
100 			proccreate(fsioproc, proc[0], Stack);
101 			nproc++;
102 		}
103 		switch(alt(a)){
104 		case 0:
105 			proc[0] = nil;
106 			if(nproc > 1){
107 				proc[0] = proc[nproc-1];
108 				proc[nproc-1] = nil;
109 			}
110 			nproc--;
111 			break;
112 		case 1:
113 			if(nproc < nelem(proc))
114 				proc[nproc++] = p;
115 			else
116 				sendp(p, nil);
117 			break;
118 		default:
119 			sysfatal("alt");
120 		}
121 	}
122 }
123 
124 static void
125 dump(void)
126 {
127 	Rpc *rpc;
128 	Fid *fid;
129 
130 	qlock(&rpclck);
131 	fprint(2, "dump:\n");
132 	for(rpc = rpcs; rpc != nil; rpc = rpc->next)
133 		fprint(2, "rpc %#p %F next %#p\n", rpc, &rpc->t, rpc->next);
134 	for(fid = fids; fid != nil; fid = fid->next)
135 		fprint(2, "fid %d qid %#llux omode %d aux %#p\n",
136 			fid->fid, fid->qid.path, fid->omode, fid->aux);
137 	fprint(2, "\n");
138 	qunlock(&rpclck);
139 }
140 
141 static Rpc*
142 newrpc(void)
143 {
144 	Rpc *r;
145 
146 	qlock(&rpclck);
147 	r = freerpcs;
148 	if(r != nil)
149 		freerpcs = r->next;
150 	else
151 		r = emallocz(sizeof(Rpc), 0);
152 	r->next = rpcs;
153 	rpcs = r;
154 	r->t.tag = r->r.tag = Notag;
155 	r->t.fid = r->r.fid = Nofid;
156 	r->t.type = r->r.type = 0;
157 	r->flushed = 0;
158 	r->fid = nil;
159 	r->r.data = (char*)r->data;
160 	qunlock(&rpclck);
161 	return r;
162 }
163 
164 static void
165 freerpc(Rpc *r)
166 {
167 	Rpc **l;
168 	if(r == nil)
169 		return;
170 	qlock(&rpclck);
171 	for(l = &rpcs; *l != nil && *l != r; l = &(*l)->next)
172 		;
173 	assert(*l == r);
174 	*l = r->next;
175 	r->next = freerpcs;
176 	freerpcs = r;
177 	r->t.type = 0;
178 	r->t.tag = 0x77777777;
179 	qunlock(&rpclck);
180 }
181 
182 static void
183 flushrpc(int tag)
184 {
185 	Rpc *r;
186 
187 	qlock(&rpclck);
188 	for(r = rpcs; r != nil; r = r->next)
189 		if(r->t.tag == tag){
190 			r->flushed = 1;
191 			break;
192 		}
193 	qunlock(&rpclck);
194 }
195 
196 static Fid*
197 getfid(int fid, int alloc)
198 {
199 	Fid *f;
200 
201 	qlock(&rpclck);
202 	for(f = fids; f != nil && f->fid != fid; f = f->next)
203 		;
204 	if(f != nil && alloc != 0){	/* fid in use */
205 		qunlock(&rpclck);
206 		return nil;
207 	}
208 	if(f == nil && alloc != 0){
209 		if(freefids != nil){
210 			f = freefids;
211 			freefids = freefids->next;
212 		}else
213 			f = emallocz(sizeof(Fid), 1);
214 		f->fid = fid;
215 		f->aux = nil;
216 		f->omode = ONONE;
217 		f->next = fids;
218 		fids = f;
219 	}
220 	qunlock(&rpclck);
221 	return f;
222 }
223 
224 static void
225 freefid(Fid *f)
226 {
227 	Fid **l;
228 
229 	if(f == nil)
230 		return;
231 	if(fsops->clunk != nil)
232 		fsops->clunk(fsops, f);
233 	qlock(&rpclck);
234 	for(l = &fids; *l != nil && *l != f; l = &(*l)->next)
235 		;
236 	assert(*l == f);
237 	*l = f->next;
238 	f->next = freefids;
239 	freefids = f;
240 	qunlock(&rpclck);
241 }
242 
243 static Rpc*
244 fserror(Rpc *rpc, char* fmt, ...)
245 {
246 	va_list arg;
247 	char *c;
248 
249 	va_start(arg, fmt);
250 	c = (char*)rpc->data;
251 	vseprint(c, c+sizeof(rpc->data), fmt, arg);
252 	va_end(arg);
253 	rpc->r.type = Rerror;
254 	rpc->r.ename = (char*)rpc->data;
255 	return rpc;
256 }
257 
258 static Rpc*
259 fsversion(Rpc *r)
260 {
261 	if(r->t.msize < 256)
262 		return fserror(r, Etoosmall);
263 	if(strncmp(r->t.version, "9P2000", 6) != 0)
264 		return fserror(r, "wrong version");
265 	if(r->t.msize < msgsize)
266 		msgsize = r->t.msize;
267 	r->r.msize = msgsize;
268 	r->r.version = "9P2000";
269 	return r;
270 }
271 
272 static Rpc*
273 fsattach(Rpc *r)
274 {
275 	static int already;
276 
277 	/* Reload user because at boot it could be still none */
278 	user=getuser();
279 	if(already++ > 0 && strcmp(r->t.uname, user) != 0)
280 		return fserror(r, Eperm);
281 	if(r->fid == nil)
282 		return fserror(r, Einuse);
283 
284 	r->r.qid.type = QTDIR;
285 	r->r.qid.path = fsops->qid;
286 	r->r.qid.vers = 0;
287 	r->fid->qid = r->r.qid;
288 	return r;
289 }
290 
291 static Rpc*
292 fswalk(Rpc *r)
293 {
294 	int i;
295 	Fid *nfid;
296 	Fid *ofid;
297 
298 	if(r->fid->omode != ONONE)
299 		return fserror(r, Eisopen);
300 
301 	nfid = nil;
302 	ofid = r->fid;
303 	if(r->t.newfid != r->t.fid){
304 		nfid = getfid(r->t.newfid, 1);
305 		if(nfid == nil)
306 			return fserror(r, Einuse);
307 		nfid->qid = r->fid->qid;
308 		if(fsops->clone != nil)
309 			fsops->clone(fsops, ofid, nfid);
310 		else
311 			nfid->aux = r->fid->aux;
312 		r->fid = nfid;
313 	}
314 	r->r.nwqid = 0;
315 	for(i = 0; i < r->t.nwname; i++)
316 		if(fsops->walk(fsops, r->fid, r->t.wname[i]) < 0)
317 			break;
318 		else
319 			r->r.wqid[i] = r->fid->qid;
320 	r->r.nwqid = i;
321 	if(i != r->t.nwname && r->t.nwname > 0){
322 		if(nfid != nil)
323 			freefid(nfid);
324 		r->fid = ofid;
325 	}
326 	if(i == 0 && r->t.nwname > 0)
327 		return fserror(r, "%r");
328 	return r;
329 }
330 
331 static void
332 fsioproc(void* a)
333 {
334 	Channel *p = a;
335 	Rpc *rpc;
336 	long rc;
337 	Fcall *t;
338 	Fcall *r;
339 	Fid *fid;
340 
341 	dprint(2, "%s: fsioproc pid %d\n", argv0, getpid());
342 	while((rpc = recvp(p)) != nil){
343 		t = &rpc->t;
344 		r = &rpc->r;
345 		fid = rpc->fid;
346 		rc = -1;
347 		dprint(2, "%s: fsioproc pid %d: req %d\n", argv0, getpid(), t->type);
348 		switch(t->type){
349 		case Topen:
350 			rc = fsops->open(fsops, fid, t->mode);
351 			if(rc >= 0){
352 				r->iounit = 0;
353 				r->qid = fid->qid;
354 				fid->omode = t->mode & 3;
355 			}
356 			break;
357 		case Tread:
358 			rc = fsops->read(fsops,fid,r->data,t->count,t->offset);
359 			if(rc >= 0){
360 				if(rc > t->count)
361 					print("%s: bug: read %ld bytes > %ud wanted\n",
362 						argv0, rc, t->count);
363 				r->count = rc;
364 			}
365 			break;
366 		case Twrite:
367 			rc = fsops->write(fsops,fid,t->data,t->count,t->offset);
368 			r->count = rc;
369 			break;
370 		default:
371 			sysfatal("fsioproc: bad type");
372 		}
373 		if(rc < 0)
374 			sendp(outc, fserror(rpc, "%r"));
375 		else
376 			sendp(outc, rpc);
377 		sendp(endc, p);
378 	}
379 	chanfree(p);
380 	dprint(2, "%s: fsioproc %d exiting\n", argv0, getpid());
381 	threadexits(nil);
382 }
383 
384 static Rpc*
385 fsopen(Rpc *r)
386 {
387 	Channel *p;
388 
389 	if(r->fid->omode != ONONE)
390 		return fserror(r, Eisopen);
391 	if((r->t.mode & 3) != OREAD && (r->fid->qid.type & QTDIR) != 0)
392 		return fserror(r, Eperm);
393 	p = recvp(procc);
394 	sendp(p, r);
395 	return nil;
396 }
397 
398 int
399 usbdirread(Usbfs*f, Qid q, char *data, long cnt, vlong off, Dirgen gen, void *arg)
400 {
401 	Dir d;
402 	char name[Namesz];
403 	int i;
404 	int n;
405 	int nd;
406 
407 	memset(&d, 0, sizeof(d));
408 	d.name = name;
409 	d.uid = d.gid = d.muid = user;
410 	d.atime = time(nil);
411 	d.mtime = epoch;
412 	d.length = 0;
413 	for(i = n = 0; gen(f, q, i, &d, arg) >= 0; i++){
414 		if(usbfsdebug > 1)
415 			fprint(2, "%s: dir %d q %#llux: %D\n", argv0, i, q.path, &d);
416 		nd = convD2M(&d, (uchar*)data+n, cnt-n);
417 		if(nd <= BIT16SZ)
418 			break;
419 		if(off > 0)
420 			off -= nd;
421 		else
422 			n += nd;
423 		d.name = name;
424 		d.uid = d.gid = d.muid = user;
425 		d.atime = time(nil);
426 		d.mtime = epoch;
427 		d.length = 0;
428 	}
429 	return n;
430 }
431 
432 long
433 usbreadbuf(void *data, long count, vlong offset, void *buf, long n)
434 {
435 	if(offset >= n)
436 		return 0;
437 	if(offset + count > n)
438 		count = n - offset;
439 	memmove(data, (char*)buf + offset, count);
440 	return count;
441 }
442 
443 static Rpc*
444 fsread(Rpc *r)
445 {
446 	Channel *p;
447 
448 	if(r->fid->omode != OREAD && r->fid->omode != ORDWR)
449 		return fserror(r, Eperm);
450 	p = recvp(procc);
451 	sendp(p, r);
452 	return nil;
453 }
454 
455 static Rpc*
456 fswrite(Rpc *r)
457 {
458 	Channel *p;
459 
460 	if(r->fid->omode != OWRITE && r->fid->omode != ORDWR)
461 		return fserror(r, Eperm);
462 	p = recvp(procc);
463 	sendp(p, r);
464 	return nil;
465 }
466 
467 static Rpc*
468 fsclunk(Rpc *r)
469 {
470 	freefid(r->fid);
471 	return r;
472 }
473 
474 static Rpc*
475 fsno(Rpc *r)
476 {
477 	return fserror(r, Eperm);
478 }
479 
480 static Rpc*
481 fsstat(Rpc *r)
482 {
483 	Dir d;
484 	char name[Namesz];
485 
486 	memset(&d, 0, sizeof(d));
487 	d.name = name;
488 	d.uid = d.gid = d.muid = user;
489 	d.atime = time(nil);
490 	d.mtime = epoch;
491 	d.length = 0;
492 	if(fsops->stat(fsops, r->fid->qid, &d) < 0)
493 		return fserror(r, "%r");
494 	r->r.stat = (uchar*)r->data;
495 	r->r.nstat = convD2M(&d, (uchar*)r->data, msgsize);
496 	return r;
497 }
498 
499 static Rpc*
500 fsflush(Rpc *r)
501 {
502 	/*
503 	 * Flag it as flushed and respond.
504 	 * Either outproc will reply to the flushed request
505 	 * before responding to flush, or it will never reply to it.
506 	 * Note that we do NOT abort the ongoing I/O.
507 	 * That might leave the affected endpoints in a failed
508 	 * state. Instead, we pretend the request is aborted.
509 	 *
510 	 * Only open, read, and write are processed
511 	 * by auxiliary processes and other requests wil never be
512 	 * flushed in practice.
513 	 */
514 	flushrpc(r->t.oldtag);
515 	return r;
516 }
517 
518 Rpc* (*fscalls[])(Rpc*) = {
519 	[Tversion]	fsversion,
520 	[Tauth]		fsno,
521 	[Tattach]	fsattach,
522 	[Twalk]		fswalk,
523 	[Topen]		fsopen,
524 	[Tcreate]	fsno,
525 	[Tread]		fsread,
526 	[Twrite]	fswrite,
527 	[Tclunk]	fsclunk,
528 	[Tremove]	fsno,
529 	[Tstat]		fsstat,
530 	[Twstat]	fsno,
531 	[Tflush]	fsflush,
532 };
533 
534 static void
535 outproc(void*)
536 {
537 	static uchar buf[Bufsize];
538 	Rpc *rpc;
539 	int nw;
540 	static int once = 0;
541 
542 	if(once++ != 0)
543 		sysfatal("more than one outproc");
544 	for(;;){
545 		do
546 			rpc = recvp(outc);
547 		while(rpc == nil);		/* a delayed reply */
548 		if(rpc->t.tag == Dietag)
549 			break;
550 		if(rpc->flushed){
551 			dprint(2, "outproc: tag %d flushed\n", rpc->t.tag);
552 			freerpc(rpc);
553 			continue;
554 		}
555 		dprint(2, "-> %F\n", &rpc->r);
556 		nw = convS2M(&rpc->r, buf, sizeof(buf));
557 		if(nw == sizeof(buf))
558 			fprint(2, "%s: outproc: buffer is too small\n", argv0);
559 		if(nw <= BIT16SZ)
560 			fprint(2, "%s: conS2M failed\n", argv0);
561 		else if(write(fsfd, buf, nw) != nw){
562 			fprint(2, "%s: outproc: write: %r", argv0);
563 			/* continue and let the reader abort us */
564 		}
565 		if(usbfsdebug > 1)
566 			dump();
567 		freerpc(rpc);
568 	}
569 	dprint(2, "%s: outproc: exiting\n", argv0);
570 }
571 
572 static void
573 usbfs(void*)
574 {
575 	Rpc *rpc;
576 	int nr;
577 	static int once = 0;
578 
579 	if(once++ != 0)
580 		sysfatal("more than one usbfs proc");
581 
582 	outc = chancreate(sizeof(Rpc*), 1);
583 	procc = chancreate(sizeof(Channel*), 0);
584 	endc = chancreate(sizeof(Channel*), 0);
585 	if(outc == nil || procc == nil || endc == nil)
586 		sysfatal("chancreate: %r");
587 	threadcreate(schedproc, nil, Stack);
588 	proccreate(outproc, nil, Stack);
589 	for(;;){
590 		rpc = newrpc();
591 		do{
592 			nr = read9pmsg(fsfd, rpc->data, sizeof(rpc->data));
593 		}while(nr == 0);
594 		if(nr < 0){
595 			dprint(2, "%s: usbfs: read: '%r'", argv0);
596 			if(fsops->end != nil)
597 				fsops->end(fsops);
598 			else
599 				closedev(fsops->dev);
600 			rpc->t.tag = Dietag;
601 			sendp(outc, rpc);
602 			break;
603 		}
604 		if(convM2S((uchar*)rpc->data, nr, &rpc->t) <=0){
605 			dprint(2, "%s: convM2S failed\n", argv0);
606 			freerpc(rpc);
607 			continue;
608 		}
609 		dprint(2, "<- %F\n", &rpc->t);
610 		rpc->r.tag = rpc->t.tag;
611 		rpc->r.type = rpc->t.type + 1;
612 		rpc->r.fid = rpc->t.fid;
613 		if(fscalls[rpc->t.type] == nil){
614 			sendp(outc, fserror(rpc, Ebadcall));
615 			continue;
616 		}
617 		if(rpc->t.fid != Nofid){
618 			if(rpc->t.type == Tattach)
619 				rpc->fid = getfid(rpc->t.fid, 1);
620 			else
621 				rpc->fid = getfid(rpc->t.fid, 0);
622 			if(rpc->fid == nil){
623 				sendp(outc, fserror(rpc, Ebadfid));
624 				continue;
625 			}
626 		}
627 		sendp(outc, fscalls[rpc->t.type](rpc));
628 	}
629 	dprint(2, "%s: ubfs: eof: exiting\n", argv0);
630 }
631 
632 void
633 usbfsinit(char* srv, char *mnt, Usbfs *f, int flag)
634 {
635 	int fd[2];
636 	int sfd;
637 	int afd;
638 	char sfile[40];
639 
640 	fsops = f;
641 	if(pipe(fd) < 0)
642 		sysfatal("pipe: %r");
643 	user = getuser();
644 	epoch = time(nil);
645 
646 	fmtinstall('D', dirfmt);
647 	fmtinstall('M', dirmodefmt);
648 	fmtinstall('F', fcallfmt);
649 	fsfd = fd[1];
650 	procrfork(usbfs, nil, Stack, RFNAMEG);	/* no RFFDG */
651 	if(srv != nil){
652 		snprint(sfile, sizeof(sfile), "#s/%s", srv);
653 		remove(sfile);
654 		sfd = create(sfile, OWRITE, 0660);
655 		if(sfd < 0)
656 			sysfatal("post: %r");
657 		snprint(sfile, sizeof(sfile), "%d", fd[0]);
658 		if(write(sfd, sfile, strlen(sfile)) != strlen(sfile))
659 			sysfatal("post: %r");
660 		close(sfd);
661 	}
662 	if(mnt != nil){
663 		sfd = dup(fd[0], -1);	/* debug */
664 		afd = fauth(sfd, "");
665 		if(afd >= 0)
666 			sysfatal("authentication required??");
667 		if(mount(sfd, -1, mnt, flag, "") < 0)
668 			sysfatal("mount: %r");
669 	}
670 	close(fd[0]);
671 }
672 
673