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 p = lookpart(lun, name);
80 /* adding identical partition is no-op */
81 if(p->offset == start && p->length == end - start && p->mode == mode)
83 werrstr("partition name already in use");
88 werrstr("no free partition slots");
93 p->id = p - lun->part;
94 p->name = estrdup(name);
96 p->length = end - start;
102 delpart(Umsc *lun, char *s)
106 p = lookpart(lun, s);
107 if(p == nil || p->id <= Qdata){
108 werrstr("partition not found");
119 fixlength(Umsc *lun, vlong blocks)
124 part[Qdata].length = blocks;
125 for(p=&part[Qdata+1]; p < &part[Qmax]; p++){
126 if(p->inuse && p->offset + p->length > blocks){
127 if(p->offset > blocks){
131 p->length = blocks - p->offset;
139 addpart(lun, "/", 0, 0, DMDIR | 0555);
140 addpart(lun, "ctl", 0, 0, 0664);
141 addpart(lun, "raw", 0, 0, 0640);
142 addpart(lun, "data", 0, lun->blocks, 0640);
146 * ctl parsing & formatting (adapted from partfs)
155 part = &lun->part[0];
158 fmtprint(&fmt, "dev %s\n", dev->dir);
159 fmtprint(&fmt, "lun %zd\n", lun - &ums->lun[0]);
160 if(lun->flags & Finqok)
161 fmtprint(&fmt, "inquiry %s\n", lun->inq);
163 fmtprint(&fmt, "geometry %llud %ld\n", lun->blocks, lun->lbsize);
164 for (p = &part[Qdata+1]; p < &part[Qmax]; p++)
166 fmtprint(&fmt, "part %s %lld %lld\n",
167 p->name, p->offset, p->offset + p->length);
168 return fmtstrflush(&fmt);
172 ctlparse(Umsc *lun, char *msg)
178 argc = tokenize(msg, argv, nelem(argv));
181 werrstr("empty control message");
185 if(strcmp(argv[0], "part") == 0){
187 werrstr("part takes 3 args");
190 start = strtoll(argv[2], 0, 0);
191 end = strtoll(argv[3], 0, 0);
192 return addpart(lun, argv[1], start, end, 0640);
193 }else if(strcmp(argv[0], "delpart") == 0){
195 werrstr("delpart takes 1 arg");
198 return delpart(lun, argv[1]);
200 werrstr("unknown ctl");
205 * These are used by scuzz scsireq
207 int exabyte, force6bytecmds;
218 r = Rd2h|Rclass|Riface;
219 if(usbcmd(dev, r, Getmaxlun, 0, 0, &max, 1) < 0){
220 dprint(2, "disk: %s: getmaxlun failed: %r\n", dev->dir);
222 max &= 017; /* 15 is the max. allowed */
223 dprint(2, "disk: %s: maxlun %d\n", dev->dir, max);
233 r = Rh2d|Rclass|Riface;
234 if(usbcmd(dev, r, Umsreset, 0, 0, nil, 0) < 0){
235 fprint(2, "disk: reset: %r\n");
247 if(unstall(dev, ums->epin, Ein) < 0)
248 dprint(2, "disk: unstall epin: %r\n");
249 /* do we need this when epin == epout? */
250 if(unstall(dev, ums->epout, Eout) < 0)
251 dprint(2, "disk: unstall epout: %r\n");
258 return (ul & (ul - 1)) == 0;
262 * return smallest power of 2 >= n
269 for(i = 0; (1 << i) < n; i++)
275 umscapacity(Umsc *lun)
282 memset(data, 0, sizeof data);
283 if(SRrcapacity(lun, data) < 0 && SRrcapacity(lun, data) < 0)
285 lun->blocks = GETBELONG(data);
286 lun->lbsize = GETBELONG(data+4);
287 if(lun->blocks == 0xFFFFFFFF){
288 if(SRrcapacity16(lun, data) < 0){
293 lun->lbsize = GETBELONG(data + 8);
294 lun->blocks = (uvlong)GETBELONG(data)<<32 |
298 lun->blocks++; /* SRcapacity returns LBA of last block */
299 lun->capacity = (vlong)lun->blocks * lun->lbsize;
300 fixlength(lun, lun->blocks);
302 fprint(2, "disk: logical block size %lud, # blocks %llud\n",
303 lun->lbsize, lun->blocks);
315 ums->maxlun = getmaxlun();
316 ums->lun = mallocz((ums->maxlun+1) * sizeof(*ums->lun), 1);
318 for(i = 0; i <= ums->maxlun; i++){
323 lun->flags = Fopen | Fusb | Frw10;
324 if(SRinquiry(lun) < 0 && SRinquiry(lun) < 0){
325 dprint(2, "disk: lun %d inquiry failed\n", i);
328 switch(lun->inquiry[0]){
330 case Devworm: /* a little different than the others */
335 fprint(2, "disk: lun %d is not a disk (type %#02x)\n",
340 if(SRready(lun) < 0 && SRready(lun) < 0 && SRready(lun) < 0)
341 dprint(2, "disk: lun %d not ready\n", i);
343 if((lun->inquiry[0] & 0x1F) == 0){
349 * we ignore the device type reported by inquiry.
350 * Some devices return a wrong value but would still work.
353 lun->inq = smprint("%.48s", (char *)lun->inquiry+8);
357 dprint(2, "disk: all luns failed\n");
358 devctl(dev, "detach");
366 * called by SR*() commands provided by scuzz's scsireq
369 umsrequest(Umsc *umsc, ScsiPtr *cmd, ScsiPtr *data, int *status)
378 memcpy(cbw.signature, "USBC", 4);
379 cbw.tag = ++ums->seq;
380 cbw.datalen = data->count;
381 cbw.flags = data->write? CbwDataOut: CbwDataIn;
383 if(cmd->count < 1 || cmd->count > 16)
384 print("disk: umsrequest: bad cmd count: %ld\n", cmd->count);
386 cbw.len = cmd->count;
387 assert(cmd->count <= sizeof(cbw.command));
388 memcpy(cbw.command, cmd->p, cmd->count);
389 memset(cbw.command + cmd->count, 0, sizeof(cbw.command) - cmd->count);
391 werrstr(""); /* we use %r later even for n == 0 */
393 fprint(2, "disk: cmd: tag %#lx: ", cbw.tag);
394 for(n = 0; n < cbw.len; n++)
395 fprint(2, " %2.2x", cbw.command[n]&0xFF);
396 fprint(2, " datalen: %ld\n", cbw.datalen);
399 /* issue tunnelled scsi command */
400 if(write(ums->epout->dfd, &cbw, CbwLen) != CbwLen){
401 fprint(2, "disk: cmd: %r\n");
405 /* transfer the data */
409 n = write(ums->epout->dfd, data->p, nio);
411 n = read(ums->epin->dfd, data->p, nio);
412 if (n >= 0 && n < nio) /* didn't fill data->p? */
413 memset(data->p + n, 0, nio - n);
418 fprint(2, "disk: data: %r\n");
420 fprint(2, "disk: data: %d bytes\n", n);
423 unstall(dev, ums->epin, Ein);
426 /* read the transfer's status */
427 n = read(ums->epin->dfd, &csw, CswLen);
429 /* n == 0 means "stalled" */
430 unstall(dev, ums->epin, Ein);
431 n = read(ums->epin->dfd, &csw, CswLen);
434 if(n != CswLen || strncmp(csw.signature, "USBS", 4) != 0){
435 dprint(2, "disk: read n=%d: status: %r\n", n);
438 if(csw.tag != cbw.tag){
439 dprint(2, "disk: status tag mismatch\n");
442 if(csw.status >= CswPhaseErr){
443 dprint(2, "disk: phase error\n");
446 if(csw.dataresidue == 0 || ums->wrongresidues)
447 csw.dataresidue = data->count - nio;
449 fprint(2, "disk: status: %2.2ux residue: %ld\n",
450 csw.status, csw.dataresidue);
451 if(cbw.command[0] == ScmdRsense){
452 fprint(2, "sense data:");
453 for(n = 0; n < data->count - csw.dataresidue; n++)
454 fprint(2, " %2.2x", data->p[n]);
466 dprint(2, "disk: phase error\n");
470 return data->count - csw.dataresidue;
474 if(ums->nerrs++ > 15)
475 sysfatal("%s: too many errors", dev->dir);
483 req->fid->qid = (Qid) {0, 0, QTDIR};
484 req->ofcall.qid = req->fid->qid;
489 dwalk(Fid *fid, char *name, Qid *qid)
494 if((fid->qid.type & QTDIR) == 0)
495 return "walk in non-directory";
496 if(strcmp(name, "..") == 0){
497 fid->qid = (Qid){0, 0, QTDIR};
501 if(fid->qid.path == 0){
502 for(lun = ums->lun; lun <= ums->lun + ums->maxlun; lun++)
503 if(strcmp(lun->name, name) == 0){
504 fid->qid.path = (lun - ums->lun + 1) << 16;
506 fid->qid.type = QTDIR;
510 return "does not exist";
512 lun = ums->lun + (fid->qid.path >> 16) - 1;
513 p = lookpart(lun, name);
515 return "does not exist";
516 fid->qid.path |= p->id;
517 fid->qid.vers = p->vers;
518 fid->qid.type = p->mode >> 24;
524 dostat(Umsc *lun, int path, Dir *d)
528 p = &lun->part[path];
530 d->qid.vers = p->vers;
531 d->qid.type =p->mode >> 24;
533 d->length = (vlong) p->length * lun->lbsize;
534 d->name = strdup(p->name);
535 d->uid = strdup(getuser());
536 d->gid = strdup(d->uid);
537 d->muid = strdup(d->uid);
541 dirgen(int n, Dir* d, void *aux)
547 for(i = Qctl; i < Qmax; i++){
548 if(lun->part[i].inuse == 0)
556 d->qid.path |= (lun - ums->lun) << 16;
561 rootgen(int n, Dir *d, void *)
568 d->qid.path = (n + 1) << 16;
570 d->mode = 0555 | DMDIR;
571 d->name = strdup(lun->name);
572 d->uid = strdup(getuser());
573 d->gid = strdup(d->uid);
574 d->muid = strdup(d->uid);
587 d->qid = req->fid->qid;
588 if(req->fid->qid.path == 0){
589 d->name = strdup("");
590 d->mode = 0555 | DMDIR;
591 d->uid = strdup(getuser());
592 d->gid = strdup(d->uid);
593 d->muid = strdup(d->uid);
595 path = req->fid->qid.path & 0xFFFF;
596 lun = ums->lun + (req->fid->qid.path >> 16) - 1;
597 dostat(lun, path, d);
608 if(req->ofcall.qid.path == 0){
612 path = req->ofcall.qid.path & 0xFFFF;
613 lun = ums->lun + (req->ofcall.qid.path >> 16) - 1;
616 srvrelease(req->srv);
620 srvacquire(req->srv);
627 * check i/o parameters and compute values needed later.
628 * we shift & mask manually to avoid run-time calls to _divv and _modv,
629 * since we don't need general division nor its cost.
632 setup(Umsc *lun, Part *p, char *data, int count, vlong offset)
634 long nb, lbsize, lbshift, lbmask;
637 if(count < 0 || lun->lbsize <= 0 && umscapacity(lun) < 0 ||
640 lbsize = lun->lbsize;
641 assert(ispow2(lbsize));
642 lbshift = log2(lbsize);
645 bno = offset >> lbshift; /* offset / lbsize */
646 nb = ((offset + count + lbsize - 1) >> lbshift) - bno;
648 if(bno + nb > p->length) /* past end of partition? */
649 nb = p->length - bno;
650 if(nb * lbsize > Maxiosize)
651 nb = Maxiosize / lbsize;
653 if(bno >= p->length || nb == 0)
656 bno += p->offset; /* start of partition */
658 lun->off = offset & lbmask; /* offset % lbsize */
659 if(lun->off == 0 && (count & lbmask) == 0)
662 /* not transferring full, aligned blocks; need intermediary */
663 lun->bufp = lun->buf;
668 * Upon SRread/SRwrite errors we assume the medium may have changed,
669 * and ask again for the capacity of the media.
670 * BUG: How to proceed to avoid confussing dossrv??
689 dirread9p(req, rootgen, nil);
693 path = q.path & 0xFFFF;
694 lun = ums->lun + (q.path >> 16) - 1;
695 count = req->ifcall.count;
696 data = req->ofcall.data;
697 offset = req->ifcall.offset;
701 dirread9p(req, dirgen, lun);
717 if(lun->lbsize <= 0 && umscapacity(lun) < 0){
718 respond(req, "phase error");
723 respond(req, "phase error");
727 lun->data.count = count;
729 count = umsrequest(lun,&lun->cmd,&lun->data,&lun->status);
730 lun->phase = Pstatus;
732 lun->lbsize = 0; /* medium may have changed */
735 req->ofcall.count = count;
740 n = snprint(buf, sizeof buf, "%11.0ud ", lun->status);
741 readbuf(req, buf, n);
749 p = &lun->part[path];
751 respond(req, "permission denied");
754 count = setup(lun, p, data, count, offset);
759 n = SRread(lun, lun->bufp, lun->nb * lun->lbsize);
761 lun->lbsize = 0; /* medium may have changed */
764 } else if (lun->bufp == data)
768 * if n == lun->nb*lun->lbsize (as expected),
769 * just copy count bytes.
771 if(lun->off + count > n)
772 count = n - lun->off; /* short read */
774 memmove(data, lun->bufp + lun->off, count);
776 req->ofcall.count = count;
799 lun = ums->lun + (req->fid->qid.path >> 16) - 1;
800 path = req->fid->qid.path & 0xFFFF;
801 count = req->ifcall.count;
802 data = req->ifcall.data;
803 offset = req->ifcall.offset;
811 s = emallocz(count+1, 1);
812 memmove(s, data, count);
813 if(s[count-1] == '\n')
815 if(ctlparse(lun, s) == -1)
818 req->ofcall.count = count;
824 if(lun->lbsize <= 0 && umscapacity(lun) < 0){
825 respond(req, "phase error");
830 if(count != 6 && count != 10 && count != 12 && count != 16){
831 respond(req, "bad command length");
834 memmove(lun->rawcmd, data, count);
835 lun->cmd.p = lun->rawcmd;
836 lun->cmd.count = count;
839 req->ofcall.count = count;
844 lun->data.count = count;
846 count = umsrequest(lun,&lun->cmd,&lun->data,&lun->status);
847 lun->phase = Pstatus;
849 lun->lbsize = 0; /* medium may have changed */
852 req->ofcall.count = count;
858 respond(req, "phase error");
864 p = &lun->part[path];
866 respond(req, "permission denied");
869 len = ocount = count;
870 count = setup(lun, p, data, count, offset);
876 if (lun->bufp == lun->buf) {
877 count = SRread(lun, lun->bufp, lun->nb * lun->lbsize);
879 lun->lbsize = 0; /* medium may have changed */
884 * if count == lun->nb*lun->lbsize, as expected, just
885 * copy len (the original count) bytes of user data.
887 if(lun->off + len > count)
888 len = count - lun->off; /* short read */
890 memmove(lun->bufp + lun->off, data, len);
894 count = SRwrite(lun, lun->bufp, lun->nb * lun->lbsize);
896 lun->lbsize = 0; /* medium may have changed */
900 if(lun->off + len > count)
901 count -= lun->off; /* short write */
902 /* never report more bytes written than requested */
905 else if(count > ocount)
908 req->ofcall.count = count;
918 findendpoints(Ums *ums)
927 for(i = 0; i < nelem(ud->ep); i++){
928 if((ep = ud->ep[i]) == nil)
930 csp = ep->iface->csp;
932 if(!(Class(csp) == Clstorage && (Proto(csp) == Protobulk)))
934 if(sc != Subatapi && sc != Sub8070 && sc != Subscsi)
935 fprint(2, "disk: subclass %#ulx not supported. trying anyway\n", sc);
936 if(ep->type == Ebulk){
937 if(ep->dir == Eboth || ep->dir == Ein)
940 if(ep->dir == Eboth || ep->dir == Eout)
945 dprint(2, "disk: ep ids: in %d out %d\n", epin, epout);
946 if(epin == -1 || epout == -1)
948 ums->epin = openep(dev, epin);
949 if(ums->epin == nil){
950 fprint(2, "disk: openep %d: %r\n", epin);
955 ums->epout = ums->epin;
957 ums->epout = openep(dev, epout);
958 if(ums->epout == nil){
959 fprint(2, "disk: openep %d: %r\n", epout);
963 if(ums->epin == ums->epout)
964 opendevdata(ums->epin, ORDWR);
966 opendevdata(ums->epin, OREAD);
967 opendevdata(ums->epout, OWRITE);
969 if(ums->epin->dfd < 0 || ums->epout->dfd < 0){
970 fprint(2, "disk: open i/o ep data: %r\n");
972 closedev(ums->epout);
975 dprint(2, "disk: ep in %s out %s\n", ums->epin->dir, ums->epout->dir);
977 devctl(ums->epin, "timeout 2000");
978 devctl(ums->epout, "timeout 2000");
980 if(usbdebug > 1 || diskdebug > 2){
981 devctl(ums->epin, "debug 1");
982 devctl(ums->epout, "debug 1");
983 devctl(dev, "debug 1");
991 fprint(2, "usage: %s [-d] devid\n", argv0);
996 * some devices like usb modems appear as mass storage
997 * for windows driver installation. switch mode here
998 * and exit if we detect one so the real driver can
1002 notreallyums(Dev *dev)
1005 if(dev->usb->vid == 0x12d1 && dev->usb->did == 0x1003){
1006 usbcmd(dev, Rh2d|Rstd|Rdev, Rsetfeature, Fdevremotewakeup, 0x02, nil, 0);
1007 exits("mode switch");
1011 static Srv diskfs = {
1021 main(int argc, char **argv)
1032 scsidebug(diskdebug);
1040 dev = getdev(*argv);
1042 sysfatal("getdev: %r");
1044 ums = dev->aux = emallocz(sizeof(Ums), 1);
1046 if(findendpoints(ums) < 0)
1047 sysfatal("endpoints not found");
1050 * SanDISK 512M gets residues wrong.
1052 if(dev->usb->vid == 0x0781 && dev->usb->did == 0x5150)
1053 ums->wrongresidues = 1;
1056 sysfatal("umsinit: %r\n");
1058 for(i = 0; i <= ums->maxlun; i++){
1061 snprint(lun->name, sizeof(lun->name), "sdU%s.%d", dev->hname, i);
1063 snprint(lun->name, sizeof(lun->name), "sdU%s", dev->hname);
1066 snprint(buf, sizeof buf, "%d.disk", dev->id);
1067 postsharesrv(&diskfs, nil, "usb", buf);