2 * usb/disk - usb mass storage file server
4 * supports only the scsi command interface, not ata.
30 typedef struct Dirtab Dirtab;
40 * Partition management (adapted from disk/partfs)
44 lookpart(Umsc *lun, char *name)
49 for(p=part; p < &part[Qmax]; p++){
50 if(p->inuse && strcmp(p->name, name) == 0)
62 for(p=part; p < &part[Qmax]; p++){
70 addpart(Umsc *lun, char *name, vlong start, vlong end, ulong mode)
74 if(start < 0 || start > end || end > lun->blocks){
75 werrstr("bad partition boundaries");
78 if(lookpart(lun, name) != nil) {
79 werrstr("partition name already in use");
84 werrstr("no free partition slots");
89 p->id = p - lun->part;
90 p->name = estrdup(name);
92 p->length = end - start;
98 delpart(Umsc *lun, char *s)
102 p = lookpart(lun, s);
103 if(p == nil || p->id <= Qdata){
104 werrstr("partition not found");
115 fixlength(Umsc *lun, vlong blocks)
120 part[Qdata].length = blocks;
121 for(p=&part[Qdata+1]; p < &part[Qmax]; p++){
122 if(p->inuse && p->offset + p->length > blocks){
123 if(p->offset > blocks){
127 p->length = blocks - p->offset;
135 addpart(lun, "/", 0, 0, DMDIR | 0555);
136 addpart(lun, "ctl", 0, 0, 0664);
137 addpart(lun, "raw", 0, 0, 0640);
138 addpart(lun, "data", 0, lun->blocks, 0640);
142 * ctl parsing & formatting (adapted from partfs)
151 part = &lun->part[0];
154 fmtprint(&fmt, "dev %s\n", dev->dir);
155 fmtprint(&fmt, "lun %ld\n", lun - &ums->lun[0]);
156 if(lun->flags & Finqok)
157 fmtprint(&fmt, "inquiry %s\n", lun->inq);
159 fmtprint(&fmt, "geometry %llud %ld\n", lun->blocks, lun->lbsize);
160 for (p = &part[Qdata+1]; p < &part[Qmax]; p++)
162 fmtprint(&fmt, "part %s %lld %lld\n",
163 p->name, p->offset, p->offset + p->length);
164 return fmtstrflush(&fmt);
168 ctlparse(Umsc *lun, char *msg)
174 argc = tokenize(msg, argv, nelem(argv));
177 werrstr("empty control message");
181 if(strcmp(argv[0], "part") == 0){
183 werrstr("part takes 3 args");
186 start = strtoll(argv[2], 0, 0);
187 end = strtoll(argv[3], 0, 0);
188 return addpart(lun, argv[1], start, end, 0640);
189 }else if(strcmp(argv[0], "delpart") == 0){
191 werrstr("delpart takes 1 arg");
194 return delpart(lun, argv[1]);
196 werrstr("unknown ctl");
201 * These are used by scuzz scsireq
203 int exabyte, force6bytecmds;
208 ding(void *, char *msg)
210 if(strstr(msg, "alarm") != nil)
222 r = Rd2h|Rclass|Riface;
223 if(usbcmd(dev, r, Getmaxlun, 0, 0, &max, 1) < 0){
224 dprint(2, "disk: %s: getmaxlun failed: %r\n", dev->dir);
226 max &= 017; /* 15 is the max. allowed */
227 dprint(2, "disk: %s: maxlun %d\n", dev->dir, max);
237 r = Rh2d|Rclass|Riface;
238 if(usbcmd(dev, r, Umsreset, 0, 0, nil, 0) < 0){
239 fprint(2, "disk: reset: %r\n");
250 if(unstall(dev, ums->epin, Ein) < 0)
251 dprint(2, "disk: unstall epin: %r\n");
253 /* do we need this when epin == epout? */
254 if(unstall(dev, ums->epout, Eout) < 0)
255 dprint(2, "disk: unstall epout: %r\n");
263 return (ul & (ul - 1)) == 0;
267 * return smallest power of 2 >= n
274 for(i = 0; (1 << i) < n; i++)
280 umscapacity(Umsc *lun)
287 memset(data, 0, sizeof data);
288 if(SRrcapacity(lun, data) < 0 && SRrcapacity(lun, data) < 0)
290 lun->blocks = GETBELONG(data);
291 lun->lbsize = GETBELONG(data+4);
292 if(lun->blocks == 0xFFFFFFFF){
293 if(SRrcapacity16(lun, data) < 0){
298 lun->lbsize = GETBELONG(data + 8);
299 lun->blocks = (uvlong)GETBELONG(data)<<32 |
303 lun->blocks++; /* SRcapacity returns LBA of last block */
304 lun->capacity = (vlong)lun->blocks * lun->lbsize;
305 fixlength(lun, lun->blocks);
307 fprint(2, "disk: logical block size %lud, # blocks %llud\n",
308 lun->lbsize, lun->blocks);
320 ums->maxlun = getmaxlun();
321 ums->lun = mallocz((ums->maxlun+1) * sizeof(*ums->lun), 1);
323 for(i = 0; i <= ums->maxlun; i++){
328 lun->flags = Fopen | Fusb | Frw10;
329 if(SRinquiry(lun) < 0 && SRinquiry(lun) < 0){
330 dprint(2, "disk: lun %d inquiry failed\n", i);
333 switch(lun->inquiry[0]){
335 case Devworm: /* a little different than the others */
340 fprint(2, "disk: lun %d is not a disk (type %#02x)\n",
345 if(SRready(lun) < 0 && SRready(lun) < 0 && SRready(lun) < 0)
346 dprint(2, "disk: lun %d not ready\n", i);
348 if((lun->inquiry[0] & 0x1F) == 0){
354 * we ignore the device type reported by inquiry.
355 * Some devices return a wrong value but would still work.
358 lun->inq = smprint("%.48s", (char *)lun->inquiry+8);
362 dprint(2, "disk: all luns failed\n");
363 devctl(dev, "detach");
371 * called by SR*() commands provided by scuzz's scsireq
374 umsrequest(Umsc *umsc, ScsiPtr *cmd, ScsiPtr *data, int *status)
383 memcpy(cbw.signature, "USBC", 4);
384 cbw.tag = ++ums->seq;
385 cbw.datalen = data->count;
386 cbw.flags = data->write? CbwDataOut: CbwDataIn;
388 if(cmd->count < 1 || cmd->count > 16)
389 print("disk: umsrequest: bad cmd count: %ld\n", cmd->count);
391 cbw.len = cmd->count;
392 assert(cmd->count <= sizeof(cbw.command));
393 memcpy(cbw.command, cmd->p, cmd->count);
394 memset(cbw.command + cmd->count, 0, sizeof(cbw.command) - cmd->count);
396 werrstr(""); /* we use %r later even for n == 0 */
398 fprint(2, "disk: cmd: tag %#lx: ", cbw.tag);
399 for(n = 0; n < cbw.len; n++)
400 fprint(2, " %2.2x", cbw.command[n]&0xFF);
401 fprint(2, " datalen: %ld\n", cbw.datalen);
404 /* issue tunnelled scsi command */
405 if(write(ums->epout->dfd, &cbw, CbwLen) != CbwLen){
406 fprint(2, "disk: cmd: %r\n");
410 /* transfer the data */
414 n = write(ums->epout->dfd, data->p, nio);
416 n = read(ums->epin->dfd, data->p, nio);
418 if (n >= 0 && left > 0) /* didn't fill data->p? */
419 memset(data->p + n, 0, left);
424 fprint(2, "disk: data: %r\n");
426 fprint(2, "disk: data: %d bytes\n", n);
429 unstall(dev, ums->epin, Ein);
432 /* read the transfer's status */
433 n = read(ums->epin->dfd, &csw, CswLen);
435 /* n == 0 means "stalled" */
436 unstall(dev, ums->epin, Ein);
437 n = read(ums->epin->dfd, &csw, CswLen);
440 if(n != CswLen || strncmp(csw.signature, "USBS", 4) != 0){
441 dprint(2, "disk: read n=%d: status: %r\n", n);
444 if(csw.tag != cbw.tag){
445 dprint(2, "disk: status tag mismatch\n");
448 if(csw.status >= CswPhaseErr){
449 dprint(2, "disk: phase error\n");
452 if(csw.dataresidue == 0 || ums->wrongresidues)
453 csw.dataresidue = data->count - nio;
455 fprint(2, "disk: status: %2.2ux residue: %ld\n",
456 csw.status, csw.dataresidue);
457 if(cbw.command[0] == ScmdRsense){
458 fprint(2, "sense data:");
459 for(n = 0; n < data->count - csw.dataresidue; n++)
460 fprint(2, " %2.2x", data->p[n]);
472 dprint(2, "disk: phase error\n");
476 return data->count - csw.dataresidue;
480 if(ums->nerrs++ > 15)
481 sysfatal("%s: too many errors", dev->dir);
489 req->fid->qid = (Qid) {0, 0, QTDIR};
490 req->ofcall.qid = req->fid->qid;
495 dwalk(Fid *fid, char *name, Qid *qid)
500 if((fid->qid.type & QTDIR) == 0)
501 return "walk in non-directory";
502 if(strcmp(name, "..") == 0){
503 fid->qid = (Qid){0, 0, QTDIR};
507 if(fid->qid.path == 0){
508 for(lun = ums->lun; lun <= ums->lun + ums->maxlun; lun++)
509 if(strcmp(lun->name, name) == 0){
510 fid->qid.path = (lun - ums->lun + 1) << 16;
512 fid->qid.type = QTDIR;
516 return "does not exist";
518 lun = ums->lun + (fid->qid.path >> 16) - 1;
519 p = lookpart(lun, name);
521 return "does not exist";
522 fid->qid.path |= p->id;
523 fid->qid.vers = p->vers;
524 fid->qid.type = p->mode >> 24;
530 dostat(Umsc *lun, int path, Dir *d)
534 p = &lun->part[path];
536 d->qid.vers = p->vers;
537 d->qid.type =p->mode >> 24;
539 d->length = (vlong) p->length * lun->lbsize;
540 d->name = strdup(p->name);
541 d->uid = strdup(getuser());
542 d->gid = strdup(d->uid);
543 d->muid = strdup(d->uid);
547 dirgen(int n, Dir* d, void *aux)
553 for(i = Qctl; i < Qmax; i++){
554 if(lun->part[i].inuse == 0)
562 d->qid.path |= (lun - ums->lun) << 16;
567 rootgen(int n, Dir *d, void *)
574 d->qid.path = (n + 1) << 16;
576 d->mode = 0555 | DMDIR;
577 d->name = strdup(lun->name);
578 d->uid = strdup(getuser());
579 d->gid = strdup(d->uid);
580 d->muid = strdup(d->uid);
593 d->qid = req->fid->qid;
594 if(req->fid->qid.path == 0){
595 d->name = strdup("");
596 d->mode = 0555 | DMDIR;
597 d->uid = strdup(getuser());
598 d->gid = strdup(d->uid);
599 d->muid = strdup(d->uid);
601 path = req->fid->qid.path & 0xFFFF;
602 lun = ums->lun + (req->fid->qid.path >> 16) - 1;
603 dostat(lun, path, d);
614 if(req->ofcall.qid.path == 0){
618 path = req->ofcall.qid.path & 0xFFFF;
619 lun = ums->lun + (req->ofcall.qid.path >> 16) - 1;
622 srvrelease(req->srv);
626 srvacquire(req->srv);
633 * check i/o parameters and compute values needed later.
634 * we shift & mask manually to avoid run-time calls to _divv and _modv,
635 * since we don't need general division nor its cost.
638 setup(Umsc *lun, Part *p, char *data, int count, vlong offset)
640 long nb, lbsize, lbshift, lbmask;
643 if(count < 0 || lun->lbsize <= 0 && umscapacity(lun) < 0 ||
646 lbsize = lun->lbsize;
647 assert(ispow2(lbsize));
648 lbshift = log2(lbsize);
651 bno = offset >> lbshift; /* offset / lbsize */
652 nb = ((offset + count + lbsize - 1) >> lbshift) - bno;
654 if(bno + nb > p->length) /* past end of partition? */
655 nb = p->length - bno;
656 if(nb * lbsize > Maxiosize)
657 nb = Maxiosize / lbsize;
659 if(bno >= p->length || nb == 0)
662 bno += p->offset; /* start of partition */
664 lun->off = offset & lbmask; /* offset % lbsize */
665 if(lun->off == 0 && (count & lbmask) == 0)
668 /* not transferring full, aligned blocks; need intermediary */
669 lun->bufp = lun->buf;
674 * Upon SRread/SRwrite errors we assume the medium may have changed,
675 * and ask again for the capacity of the media.
676 * BUG: How to proceed to avoid confussing dossrv??
695 dirread9p(req, rootgen, nil);
699 path = q.path & 0xFFFF;
700 lun = ums->lun + (q.path >> 16) - 1;
701 count = req->ifcall.count;
702 data = req->ofcall.data;
703 offset = req->ifcall.offset;
707 dirread9p(req, dirgen, lun);
723 if(lun->lbsize <= 0 && umscapacity(lun) < 0){
724 respond(req, "phase error");
729 respond(req, "phase error");
733 lun->data.count = count;
735 count = umsrequest(lun,&lun->cmd,&lun->data,&lun->status);
736 lun->phase = Pstatus;
738 lun->lbsize = 0; /* medium may have changed */
741 req->ofcall.count = count;
746 n = snprint(buf, sizeof buf, "%11.0ud ", lun->status);
747 readbuf(req, buf, n);
755 p = &lun->part[path];
757 respond(req, "permission denied");
760 count = setup(lun, p, data, count, offset);
765 n = SRread(lun, lun->bufp, lun->nb * lun->lbsize);
767 lun->lbsize = 0; /* medium may have changed */
770 } else if (lun->bufp == data)
774 * if n == lun->nb*lun->lbsize (as expected),
775 * just copy count bytes.
777 if(lun->off + count > n)
778 count = n - lun->off; /* short read */
780 memmove(data, lun->bufp + lun->off, count);
782 req->ofcall.count = count;
805 lun = ums->lun + (req->fid->qid.path >> 16) - 1;
806 path = req->fid->qid.path & 0xFFFF;
807 count = req->ifcall.count;
808 data = req->ifcall.data;
809 offset = req->ifcall.offset;
817 s = emallocz(count+1, 1);
818 memmove(s, data, count);
819 if(s[count-1] == '\n')
821 if(ctlparse(lun, s) == -1)
824 req->ofcall.count = count;
830 if(lun->lbsize <= 0 && umscapacity(lun) < 0){
831 respond(req, "phase error");
836 if(count != 6 && count != 10 && count != 12 && count != 16){
837 respond(req, "bad command length");
840 memmove(lun->rawcmd, data, count);
841 lun->cmd.p = lun->rawcmd;
842 lun->cmd.count = count;
845 req->ofcall.count = count;
850 lun->data.count = count;
852 count = umsrequest(lun,&lun->cmd,&lun->data,&lun->status);
853 lun->phase = Pstatus;
855 lun->lbsize = 0; /* medium may have changed */
858 req->ofcall.count = count;
864 respond(req, "phase error");
870 p = &lun->part[path];
872 respond(req, "permission denied");
875 len = ocount = count;
876 count = setup(lun, p, data, count, offset);
882 if (lun->bufp == lun->buf) {
883 count = SRread(lun, lun->bufp, lun->nb * lun->lbsize);
885 lun->lbsize = 0; /* medium may have changed */
890 * if count == lun->nb*lun->lbsize, as expected, just
891 * copy len (the original count) bytes of user data.
893 if(lun->off + len > count)
894 len = count - lun->off; /* short read */
896 memmove(lun->bufp + lun->off, data, len);
900 count = SRwrite(lun, lun->bufp, lun->nb * lun->lbsize);
902 lun->lbsize = 0; /* medium may have changed */
906 if(lun->off + len > count)
907 count -= lun->off; /* short write */
908 /* never report more bytes written than requested */
911 else if(count > ocount)
914 req->ofcall.count = count;
924 findendpoints(Ums *ums)
933 for(i = 0; i < nelem(ud->ep); i++){
934 if((ep = ud->ep[i]) == nil)
936 csp = ep->iface->csp;
938 if(!(Class(csp) == Clstorage && (Proto(csp) == Protobulk)))
940 if(sc != Subatapi && sc != Sub8070 && sc != Subscsi)
941 fprint(2, "disk: subclass %#ulx not supported. trying anyway\n", sc);
942 if(ep->type == Ebulk){
943 if(ep->dir == Eboth || ep->dir == Ein)
946 if(ep->dir == Eboth || ep->dir == Eout)
951 dprint(2, "disk: ep ids: in %d out %d\n", epin, epout);
952 if(epin == -1 || epout == -1)
954 ums->epin = openep(dev, epin);
955 if(ums->epin == nil){
956 fprint(2, "disk: openep %d: %r\n", epin);
961 ums->epout = ums->epin;
963 ums->epout = openep(dev, epout);
964 if(ums->epout == nil){
965 fprint(2, "disk: openep %d: %r\n", epout);
969 if(ums->epin == ums->epout)
970 opendevdata(ums->epin, ORDWR);
972 opendevdata(ums->epin, OREAD);
973 opendevdata(ums->epout, OWRITE);
975 if(ums->epin->dfd < 0 || ums->epout->dfd < 0){
976 fprint(2, "disk: open i/o ep data: %r\n");
978 closedev(ums->epout);
981 dprint(2, "disk: ep in %s out %s\n", ums->epin->dir, ums->epout->dir);
983 devctl(ums->epin, "timeout 2000");
984 devctl(ums->epout, "timeout 2000");
986 if(usbdebug > 1 || diskdebug > 2){
987 devctl(ums->epin, "debug 1");
988 devctl(ums->epout, "debug 1");
989 devctl(dev, "debug 1");
997 fprint(2, "usage: %s [-d] devid\n", argv0);
1008 closedev(ums->epin);
1009 closedev(ums->epout);
1010 ums->epin = ums->epout = nil;
1015 static Srv diskfs = {
1025 main(int argc, char **argv)
1036 scsidebug(diskdebug);
1045 dev = getdev(atoi(*argv));
1047 sysfatal("getdev: %r");
1048 ums = dev->aux = emallocz(sizeof(Ums), 1);
1050 dev->free = umsdevfree;
1051 if(findendpoints(ums) < 0)
1052 sysfatal("endpoints not found");
1055 * SanDISK 512M gets residues wrong.
1057 if(dev->usb->vid == 0x0781 && dev->usb->did == 0x5150)
1058 ums->wrongresidues = 1;
1061 sysfatal("umsinit: %r\n");
1063 for(i = 0; i <= ums->maxlun; i++){
1065 snprint(lun->name, sizeof(lun->name), "sdU%d.%d", dev->id, i);
1068 snprint(buf, sizeof buf, "%d.disk", dev->id);
1069 postsharesrv(&diskfs, nil, "usb", buf);