2 #include "../port/lib.h"
6 #include "../port/error.h"
9 * The sys*() routines needn't poperror() as they return directly to syscall().
21 pprint("warning: process exceeds %d file descriptors\n", ex);
25 growfd(Fgrp *f, int fd) /* fd is always >= 0 */
27 Chan **newfd, **oldfd;
31 if(fd >= f->nfd+DELTAFD)
32 return -1; /* out of range */
34 * Unbounded allocation is unwise; besides, there are only 16 bits
39 print("no free file descriptors\n");
42 newfd = malloc((f->nfd+DELTAFD)*sizeof(Chan*));
46 memmove(newfd, oldfd, f->nfd*sizeof(Chan*));
51 if(fd/100 > f->maxfd/100)
52 f->exceed = (fd/100)*100;
59 * this assumes that the fgrp is locked
62 findfreefd(Fgrp *f, int start)
66 for(fd=start; fd<f->nfd; fd++)
69 if(fd >= f->nfd && growfd(f, fd) < 0)
82 fd = findfreefd(f, 0);
95 newfd2(int fd[2], Chan *c[2])
101 fd[0] = findfreefd(f, 0);
106 fd[1] = findfreefd(f, fd[0]+1);
120 fdtochan(int fd, int mode, int chkmnt, int iref)
129 if(fd<0 || f->nfd<=fd || (c = f->fd[fd])==0) {
137 if(chkmnt && (c->flag&CMSG)) {
143 if(mode<0 || c->mode==ORDWR)
146 if((mode&OTRUNC) && c->mode==OREAD) {
152 if((mode&~OTRUNC) != c->mode) {
163 o &= ~(OTRUNC|OCEXEC|ORCLOSE);
172 sysfd2path(va_list list)
179 fd = va_arg(list, int);
180 buf = va_arg(list, char*);
181 len = va_arg(list, uint);
182 validaddr((uintptr)buf, len, 1);
183 c = fdtochan(fd, -1, 0, 1);
184 snprint(buf, len, "%s", chanpath(c));
190 syspipe(va_list list)
195 static char *datastr[] = {"data", "data1"};
197 ufd = va_arg(list, int*);
198 validaddr((uintptr)ufd, sizeof(fd), 1);
199 evenaddr((uintptr)ufd);
201 ufd[0] = ufd[1] = fd[0] = fd[1] = -1;
202 d = devtab[devno('|', 0)];
203 c[0] = namec("#|", Atodir, 0, 0);
212 if(walk(&c[0], datastr+0, 1, 1, nil) < 0)
214 if(walk(&c[1], datastr+1, 1, 1, nil) < 0)
216 c[0] = d->open(c[0], ORDWR);
217 c[1] = d->open(c[1], ORDWR);
218 if(newfd2(fd, c) < 0)
233 fd = va_arg(list, int);
236 * Close after dup'ing, so date > #d/1 works
238 c = fdtochan(fd, -1, 0, 1);
239 fd = va_arg(list, int);
242 if(fd<0 || growfd(f, fd)<0) {
269 sysopen(va_list list)
276 name = va_arg(list, char*);
277 mode = va_arg(list, ulong);
278 openmode(mode); /* error check only */
279 validaddr((uintptr)name, 1, 0);
280 c = namec(name, Aopen, mode, 0);
293 fdclose(int fd, int flag)
300 if(c == nil || (flag != 0 && (c->flag&flag) == 0)){
306 while(fd > 0 && f->fd[fd] == nil)
314 sysclose(va_list list)
318 fd = va_arg(list, int);
319 fdtochan(fd, -1, 0, 0);
325 unionread(Chan *c, void *va, long n)
336 /* bring mount in sync with c->uri and c->umc */
337 for(i = 0; mount != nil && i < c->uri; i++)
342 /* Error causes component of union to be skipped */
343 if(mount->to && !waserror()){
345 c->umc = cclone(mount->to);
346 c->umc = devtab[c->umc->type]->open(c->umc, OREAD);
349 nr = devtab[c->umc->type]->read(c->umc, va, n, c->umc->offset);
350 c->umc->offset += nr;
356 /* Advance to next element */
365 qunlock(&c->umqlock);
378 qunlock(&c->umqlock);
382 dirfixed(uchar *p, uchar *e, Dir *d)
386 len = GBIT16(p)+BIT16SZ;
390 p += BIT16SZ; /* ignore size */
391 d->type = devno(GBIT16(p), 1);
395 d->qid.type = GBIT8(p);
397 d->qid.vers = GBIT32(p);
399 d->qid.path = GBIT64(p);
403 d->atime = GBIT32(p);
405 d->mtime = GBIT32(p);
407 d->length = GBIT64(p);
413 dirname(uchar *p, int *n)
415 p += BIT16SZ+BIT16SZ+BIT32SZ+BIT8SZ+BIT32SZ+BIT64SZ
416 + BIT32SZ+BIT32SZ+BIT32SZ+BIT64SZ;
418 return (char*)p+BIT16SZ;
422 dirsetname(char *name, int len, uchar *p, long n, long maxn)
431 oname = dirname(p, &olen);
434 PBIT16(p, nn-BIT16SZ);
439 memmove(oname+len, oname+olen, p+n-(uchar*)(oname+olen));
440 PBIT16((uchar*)(oname-2), len);
441 memmove(oname, name, len);
446 * Mountfix might have caused the fixed results of the directory read
447 * to overflow the buffer. Catch the overflow in c->dirrock.
450 mountrock(Chan *c, uchar *p, uchar **pe)
457 /* find last directory entry */
459 len = BIT16SZ+GBIT16(p);
466 qlock(&c->rockqlock);
467 if(c->nrock+len > c->mrock){
468 n = ROUND(c->nrock+len, 1024);
470 memmove(r, c->dirrock, c->nrock);
475 memmove(c->dirrock+c->nrock, p, len);
477 qunlock(&c->rockqlock);
484 * Satisfy a directory read with the results saved in c->dirrock.
487 mountrockread(Chan *c, uchar *op, long n, long *nn)
490 uchar *rp, *erp, *ep, *p;
496 /* copy out what we can */
497 qlock(&c->rockqlock);
502 while(rp+BIT16SZ <= erp){
503 dirlen = BIT16SZ+GBIT16(rp);
506 memmove(p, rp, dirlen);
512 qunlock(&c->rockqlock);
518 memmove(c->dirrock, rp, erp-rp);
522 qunlock(&c->rockqlock);
533 * Rewrite the results of a directory read to reflect current
534 * name space bindings and mounts. Specifically, replace
535 * directory entries for bind and mount points with the results
536 * of statting what is mounted there. Except leave the old names.
539 mountfix(Chan *c, uchar *op, long n, long maxn)
555 for(e=&p[n]; p+BIT16SZ<e; p+=dirlen){
556 dirlen = dirfixed(p, e, &d);
561 if(findmount(&nc, &mh, d.type, d.dev, d.qid)){
563 * If it's a union directory and the original is
564 * in the union, don't rewrite anything.
566 for(m=mh->mount; m; m=m->next)
567 if(eqchantdqid(m->to, d.type, d.dev, d.qid, 1))
570 name = dirname(p, &nname);
572 * Do the stat but fix the name. If it fails, leave old entry.
573 * BUG: If it fails because there isn't room for the entry,
574 * what can we do? Nothing, really. Might as well skip it.
582 l = devtab[nc->type]->stat(nc, buf, nbuf);
583 l = dirsetname(name, nname, buf, l, nbuf);
589 * Shift data in buffer to accomodate new entry,
590 * possibly overflowing into rock.
592 rest = e - (p+dirlen);
594 while(p+l+rest > op+maxn){
600 rest = e - (p+dirlen);
604 memmove(p+l, p+dirlen, rest);
610 * Rewrite directory entry.
623 error("oops in rockfix");
629 read(int fd, uchar *p, long n, vlong *offp)
635 validaddr((uintptr)p, n, 1);
636 c = fdtochan(fd, OREAD, 1, 1);
644 * The offset is passed through on directories, normally.
645 * Sysseek complains, but pread is used by servers like exportfs,
646 * that shouldn't need to worry about this issue.
648 * Notice that c->devoffset is the offset that c's dev is seeing.
649 * The number of bytes read on this fd (c->offset) may be different
650 * due to rewritings in rockfix.
652 if(offp == nil) /* use and maintain channel's offset */
659 if(off == 0){ /* rewind to the beginning of the directory */
668 if(c->qid.type & QTDIR){
669 if(mountrockread(c, p, n, &nn)){
670 /* do nothing: mountrockread filled buffer */
672 nn = unionread(c, p, n);
676 nn = devtab[c->type]->read(c, p, n, c->devoffset);
678 nnn = mountfix(c, p, nn, n);
680 nnn = nn = devtab[c->type]->read(c, p, n, off);
693 sys_read(va_list list)
699 fd = va_arg(list, int);
700 buf = va_arg(list, void*);
701 len = va_arg(list, long);
702 return (uintptr)read(fd, buf, len, nil);
706 syspread(va_list list)
713 fd = va_arg(list, int);
714 buf = va_arg(list, void*);
715 len = va_arg(list, long);
716 off = va_arg(list, vlong);
721 return (uintptr)read(fd, buf, len, offp);
725 write(int fd, void *buf, long len, vlong *offp)
731 validaddr((uintptr)buf, len, 0);
733 c = fdtochan(fd, OWRITE, 1, 1);
744 if(c->qid.type & QTDIR)
749 if(offp == nil){ /* use and maintain channel's offset */
760 m = devtab[c->type]->write(c, buf, n, off);
761 if(offp == nil && m < n){
773 sys_write(va_list list)
779 fd = va_arg(list, int);
780 buf = va_arg(list, void*);
781 len = va_arg(list, long);
782 return (uintptr)write(fd, buf, len, nil);
786 syspwrite(va_list list)
793 fd = va_arg(list, int);
794 buf = va_arg(list, void*);
795 len = va_arg(list, long);
796 off = va_arg(list, vlong);
801 return (uintptr)write(fd, buf, len, offp);
805 sseek(int fd, vlong o, int type)
808 uchar buf[sizeof(Dir)+100];
813 c = fdtochan(fd, -1, 1, 1);
818 if(devtab[c->type]->dc == L'|')
825 if((c->qid.type & QTDIR) && off != 0)
833 if(c->qid.type & QTDIR)
835 lock(c); /* lock for read/write update */
846 if(c->qid.type & QTDIR)
848 n = devtab[c->type]->stat(c, buf, sizeof buf);
849 if(convM2D(buf, n, &dir, nil) == 0)
850 error("internal error: stat error in seek");
851 off = dir.length + o;
868 sysseek(va_list list)
873 v = va_arg(list, vlong*);
874 evenaddr((uintptr)v);
875 validaddr((uintptr)v, sizeof(vlong), 1);
877 fd = va_arg(list, int);
878 n = va_arg(list, vlong);
879 t = va_arg(list, int);
881 *v = sseek(fd, n, t);
887 sysoseek(va_list list)
892 fd = va_arg(list, int);
893 n = va_arg(list, long);
894 t = va_arg(list, int);
895 return (uintptr)sseek(fd, n, t);
899 validstat(uchar *s, int n)
904 if(statcheck(s, n) < 0)
906 /* verify that name entry is acceptable */
907 s += STATFIXLEN - 4*BIT16SZ; /* location of first string */
909 * s now points at count for first string.
910 * if it's too long, let the server decide; this is
911 * only for his protection anyway. otherwise
912 * we'd have to allocate and waserror.
920 /* name could be '/' */
921 if(strcmp(buf, "/") != 0)
934 s = strrchr(p->s, '/');
941 sysfstat(va_list list)
948 fd = va_arg(list, int);
949 s = va_arg(list, uchar*);
950 l = va_arg(list, uint);
951 validaddr((uintptr)s, l, 1);
953 c = fdtochan(fd, -1, 0, 1);
958 l = devtab[c->type]->stat(c, s, l);
965 sysstat(va_list list)
972 name = va_arg(list, char*);
973 s = va_arg(list, uchar*);
974 l = va_arg(list, uint);
975 validaddr((uintptr)s, l, 1);
976 validaddr((uintptr)name, 1, 0);
977 c = namec(name, Aaccess, 0, 0);
982 r = devtab[c->type]->stat(c, s, l);
983 name = pathlast(c->path);
985 r = dirsetname(name, strlen(name), s, r, l);
993 syschdir(va_list list)
998 name = va_arg(list, char*);
999 validaddr((uintptr)name, 1, 0);
1000 c = namec(name, Atodir, 0, 0);
1007 bindmount(int ismount, int fd, int afd, char* arg0, char* arg1, ulong flag, char* spec)
1010 Chan *c0, *c1, *ac, *bc;
1018 if((flag&~MMASK) || (flag&MORDER)==(MBEFORE|MAFTER))
1022 validaddr((uintptr)spec, 1, 0);
1023 spec = validnamedup(spec, 1);
1029 if(up->pgrp->noattach)
1033 bc = fdtochan(fd, ORDWR, 0, 1);
1042 ac = fdtochan(afd, ORDWR, 0, 1);
1044 bogus.flags = flag & MCACHE;
1046 bogus.authchan = ac;
1048 ret = devno('M', 0);
1049 c0 = devtab[ret]->attach((char*)&bogus);
1050 poperror(); /* ac bc */
1056 validaddr((uintptr)arg0, 1, 0);
1057 c0 = namec(arg0, Abind, 0, 0);
1065 validaddr((uintptr)arg1, 1, 0);
1066 c1 = namec(arg1, Amount, 0, 0);
1072 ret = cmount(&c0, c1, flag, spec);
1087 sysbind(va_list list)
1092 arg0 = va_arg(list, char*);
1093 arg1 = va_arg(list, char*);
1094 flag = va_arg(list, ulong);
1095 return (uintptr)bindmount(0, -1, -1, arg0, arg1, flag, nil);
1099 sysmount(va_list list)
1105 fd = va_arg(list, int);
1106 afd = va_arg(list, int);
1107 arg1 = va_arg(list, char*);
1108 flag = va_arg(list, ulong);
1109 spec = va_arg(list, char*);
1110 return (uintptr)bindmount(1, fd, afd, nil, arg1, flag, spec);
1114 sys_mount(va_list list)
1120 fd = va_arg(list, int);
1121 arg1 = va_arg(list, char*);
1122 flag = va_arg(list, ulong);
1123 spec = va_arg(list, char*);
1124 return (uintptr)bindmount(1, fd, -1, nil, arg1, flag, spec);
1128 sysunmount(va_list list)
1130 Chan *cmount, *cmounted;
1133 name = va_arg(list, char*);
1134 old = va_arg(list, char*);
1137 validaddr((uintptr)old, 1, 0);
1138 cmount = namec(old, Amount, 0, 0);
1148 * This has to be namec(..., Aopen, ...) because
1149 * if arg[0] is something like /srv/cs or /fd/0,
1150 * opening it is the only way to get at the real
1153 validaddr((uintptr)name, 1, 0);
1154 cmounted = namec(name, Aopen, OREAD, 0);
1156 cunmount(cmount, cmounted);
1165 syscreate(va_list list)
1171 name = va_arg(list, char*);
1172 mode = va_arg(list, int);
1173 perm = va_arg(list, int);
1174 openmode(mode&~OEXCL); /* error check only; OEXCL okay here */
1175 validaddr((uintptr)name, 1, 0);
1176 c = namec(name, Acreate, mode, perm);
1189 sysremove(va_list list)
1194 name = va_arg(list, char*);
1195 validaddr((uintptr)name, 1, 0);
1196 c = namec(name, Aremove, 0, 0);
1198 * Removing mount points is disallowed to avoid surprises
1199 * (which should be removed: the mount point or the mounted Chan?).
1206 c->type = 0; /* see below */
1210 devtab[c->type]->remove(c);
1212 * Remove clunks the fid, but we need to recover the Chan
1213 * so fake it up. rootclose() is known to be a nop.
1222 wstat(Chan *c, uchar *d, int nd)
1233 * Renaming mount points is disallowed to avoid surprises
1234 * (which should be renamed? the mount point or the mounted Chan?).
1236 dirname(d, &namelen);
1238 nameerror(chanpath(c), Eismtpt);
1240 l = devtab[c->type]->wstat(c, d, nd);
1247 syswstat(va_list list)
1254 name = va_arg(list, char*);
1255 s = va_arg(list, uchar*);
1256 l = va_arg(list, uint);
1257 validaddr((uintptr)s, l, 0);
1259 validaddr((uintptr)name, 1, 0);
1260 c = namec(name, Aaccess, 0, 0);
1261 return (uintptr)wstat(c, s, l);
1265 sysfwstat(va_list list)
1272 fd = va_arg(list, int);
1273 s = va_arg(list, uchar*);
1274 l = va_arg(list, uint);
1275 validaddr((uintptr)s, l, 0);
1277 c = fdtochan(fd, -1, 1, 1);
1278 return (uintptr)wstat(c, s, l);
1282 packoldstat(uchar *buf, Dir *d)
1287 /* lay down old stat buffer - grotty code but it's temporary */
1289 strncpy((char*)p, d->name, 28);
1291 strncpy((char*)p, d->uid, 28);
1293 strncpy((char*)p, d->gid, 28);
1295 q = d->qid.path & ~DMDIR; /* make sure doesn't accidentally look like directory */
1296 if(d->qid.type & QTDIR) /* this is the real test of a new directory */
1300 PBIT32(p, d->qid.vers);
1304 PBIT32(p, d->atime);
1306 PBIT32(p, d->mtime);
1308 PBIT64(p, d->length);
1316 sys_stat(va_list list)
1320 uchar *s, buf[128]; /* old DIRLEN plus a little should be plenty */
1321 char strs[128], *name;
1323 char old[] = "old stat system call - recompile";
1325 name = va_arg(list, char*);
1326 s = va_arg(list, uchar*);
1327 validaddr((uintptr)s, 116, 1);
1328 validaddr((uintptr)name, 1, 0);
1329 c = namec(name, Aaccess, 0, 0);
1334 l = devtab[c->type]->stat(c, buf, sizeof buf);
1335 /* buf contains a new stat buf; convert to old. yuck. */
1336 if(l <= BIT16SZ) /* buffer too small; time to face reality */
1338 name = pathlast(c->path);
1340 l = dirsetname(name, strlen(name), buf, l, sizeof buf);
1341 l = convM2D(buf, l, &d, strs);
1352 sys_fstat(va_list list)
1357 uchar *s, buf[128]; /* old DIRLEN plus a little should be plenty */
1360 char old[] = "old fstat system call - recompile";
1363 fd = va_arg(list, int);
1364 s = va_arg(list, uchar*);
1365 validaddr((uintptr)s, 116, 1);
1366 c = fdtochan(fd, -1, 0, 1);
1371 l = devtab[c->type]->stat(c, buf, sizeof buf);
1372 /* buf contains a new stat buf; convert to old. yuck. */
1373 if(l <= BIT16SZ) /* buffer too small; time to face reality */
1375 name = pathlast(c->path);
1377 l = dirsetname(name, strlen(name), buf, l, sizeof buf);
1378 l = convM2D(buf, l, &d, strs);
1391 error("old wstat system call - recompile");
1398 error("old fwstat system call - recompile");