2 #include "../port/lib.h"
6 #include "../port/error.h"
8 int chandebug=0; /* toggled by sysr1 */
9 #define DBG if(chandebug)iprint
17 static struct Chanalloc
25 typedef struct Elemlist Elemlist;
29 char *aname; /* original name */
30 char *name; /* copy of name, so '/' can be overwritten */
39 #define SEP(c) ((c) == 0 || (c) == '/')
42 dumpmount(void) /* DEBUGGING */
49 print("no process for dumpmount\n");
54 print("no pgrp for dumpmount\n");
63 he = &pg->mnthash[MNTHASH];
64 for(h = pg->mnthash; h < he; h++){
65 for(f = *h; f; f = f->hash){
66 print("head: %#p: %s %#llux.%lud %C %lud -> \n", f,
67 f->from->path->s, f->from->qid.path,
68 f->from->qid.vers, devtab[f->from->type]->dc,
70 for(t = f->mount; t; t = t->next)
71 print("\t%#p: %s (umh %#p) (path %#.8llux dev %C %lud)\n", t, t->to->path->s, t->to->umh, t->to->qid.path, devtab[t->to->type]->dc, t->to->dev);
86 return "<nil path.s>";
93 return p[0]=='.' && p[1]=='.' && p[2]=='\0';
116 panic("decref pc=%#p", getcallerpc(&r));
121 * Rather than strncpy, which zeros the rest of the buffer, kstrcpy
122 * truncates if necessary, always zero terminates, does not zero fill,
123 * and puts ... at the end of the string if it's too long. Usually used to
124 * save a string in up->genbuf;
127 kstrcpy(char *s, char *t, int ns)
137 /* too long, truncate */
141 /* append ... if there is space */
145 /* look for first byte of UTF-8 sequence by skipping continuation bytes */
146 while(ns>0 && (s[--ns]&0xC0)==0x80)
162 * Atomically replace *p with copy of s
165 kstrdup(char **p, char *s)
171 /* if it's a user, we can wait for memory; if not, something's very wrong */
177 panic("kstrdup: no memory");
179 setmalloctag(t, getcallerpc(&p));
192 todinit(); /* avoid later reentry causing infinite recursion */
193 for(i=0; devtab[i] != nil; i++)
202 for(i=0; devtab[i] != nil; i++)
207 chandevshutdown(void)
211 /* shutdown in reverse order */
212 for(i=0; devtab[i] != nil; i++)
214 for(i--; i >= 0; i--)
215 devtab[i]->shutdown();
226 chanalloc.free = c->next;
232 c = smalloc(sizeof(Chan));
234 c->fid = ++chanalloc.fid;
235 c->link = chanalloc.list;
240 /* if you get an error before associating with a dev,
241 close calls rootclose, a nop */
256 memset(&c->mqid, 0, sizeof(c->mqid));
271 p = smalloc(sizeof(Path));
274 p->alen = i+PATHSLOP;
275 p->s = smalloc(p->alen);
276 memmove(p->s, s, i+1);
281 * Cannot use newpath for arbitrary names because the mtpt
282 * array will not be populated correctly. The names #/ and / are
283 * allowed, but other names with / in them draw warnings.
285 if(strchr(s, '/') && strcmp(s, "#/") != 0 && strcmp(s, "/") != 0)
286 print("newpath: %s from %#p\n", s, getcallerpc(&s));
289 p->malen = PATHMSLOP;
290 p->mtpt = smalloc(p->malen*sizeof p->mtpt[0]);
300 pp = smalloc(sizeof(Path));
303 DBG("copypath %s %p => %p\n", p->s, p, pp);
307 pp->s = smalloc(p->alen);
308 memmove(pp->s, p->s, p->len+1);
311 pp->malen = p->malen;
312 pp->mtpt = smalloc(p->malen*sizeof pp->mtpt[0]);
313 for(i=0; i<pp->mlen; i++){
314 pp->mtpt[i] = p->mtpt[i];
330 DBG("pathclose %p %s ref=%ld =>", p, p->s, p->ref);
331 for(i=0; i<p->mlen; i++)
332 DBG(" %p", p->mtpt[i]);
339 for(i=0; i<p->mlen; i++)
347 * In place, rewrite name to compress multiple /, eliminate ., and process ..
348 * (Really only called to remove a trailing .. that has been added.
349 * Otherwise would need to update n->mtpt as well.)
352 fixdotdotname(Path *p)
357 r = strchr(p->s, '/');
363 * The correct name is #i rather than #i/,
364 * but the correct name of #/ is #/.
366 if(strcmp(r, "/")==0 && p->s[1] != '/')
370 p->len = strlen(p->s);
388 addelem(Path *p, char *s, Chan *from)
394 if(s[0]=='.' && s[1]=='\0')
400 if(p->len+1+i+1 > p->alen){
401 a = p->len+1+i+1 + PATHSLOP;
403 memmove(t, p->s, p->len+1);
408 /* don't insert extra slash if one is present */
409 if(p->len>0 && p->s[p->len-1]!='/' && s[0]!='/')
410 p->s[p->len++] = '/';
411 memmove(p->s+p->len, s, i+1);
415 DBG("addelem %s .. => rm %p\n", p->s, p->mtpt[p->mlen-1]);
416 if(p->mlen>1 && (c = p->mtpt[--p->mlen])){
417 p->mtpt[p->mlen] = nil;
421 if(p->mlen >= p->malen){
422 p->malen = p->mlen+1+PATHMSLOP;
423 tt = smalloc(p->malen*sizeof tt[0]);
424 memmove(tt, p->mtpt, p->mlen*sizeof tt[0]);
428 DBG("addelem %s %s => add %p\n", p->s, s, from);
429 p->mtpt[p->mlen++] = from;
441 if(c->dirrock != nil){
468 c->next = chanalloc.free;
474 * Queue a chan to be closed by one of the clunk procs.
489 return clunkq.head != nil;
498 if(clunkq.head == nil){
500 tsleep(&clunkq.r, clunkwork, nil, 5000);
511 clunkq.head = c->next;
515 devtab[c->type]->close(c);
530 clunkq.tail->next = c;
536 if(up != 0 && palloc.Lock.p != up && canqlock(&clunkq.q)){
539 kproc("closeproc", closeproc, nil);
548 if(c == nil || c->ref < 1 || c->flag&CFREE)
549 panic("cclose %#p", getcallerpc(&c));
551 DBG("cclose %p name=%s ref=%ld\n", c, chanpath(c), c->ref);
556 if(devtab[c->type]->dc == L'M')
557 if((c->flag&(CRCLOSE|CCACHE)) == CCACHE)
558 if((c->qid.type&(QTEXCL|QTMOUNT|QTAUTH)) == 0)
559 if((clunkq.nqueued - clunkq.nclosed) < 64){
565 devtab[c->type]->close(c);
574 if(c == nil || c->ref < 1 || c->flag&CFREE)
575 panic("ccloseq %#p", getcallerpc(&c));
577 DBG("ccloseq %p name=%s ref=%ld\n", c, chanpath(c), c->ref);
586 * Make sure we have the only copy of c. (Copy on write.)
605 return a.path==b.path && a.vers==b.vers;
609 eqchan(Chan *a, Chan *b, int skipvers)
611 if(a->qid.path != b->qid.path)
613 if(!skipvers && a->qid.vers!=b->qid.vers)
615 if(a->type != b->type)
623 eqchantdqid(Chan *a, int type, int dev, Qid qid, int skipvers)
625 if(a->qid.path != qid.path)
627 if(!skipvers && a->qid.vers!=qid.vers)
641 mh = smalloc(sizeof(Mhead));
649 cmount(Chan **newp, Chan *old, int flag, char *spec)
654 Mount *nm, *f, *um, **h;
657 if(QTDIR & (old->qid.type^(*newp)->qid.type))
661 print("cmount: unexpected umh, caller %#p\n", getcallerpc(&newp));
665 if((old->qid.type&QTDIR)==0 && order != MREPL)
672 * Not allowed to bind when the old directory is itself a union.
673 * (Maybe it should be allowed, but I don't see what the semantics
676 * We need to check mh->mount->next to tell unions apart from
677 * simple mount points, so that things like
682 * The check of mount->mflag allows things like
686 * This is far more complicated than it should be, but I don't
687 * see an easier way at the moment.
689 if((flag&MCREATE) && mh && mh->mount
690 && (mh->mount->next || !(mh->mount->mflag&MCREATE)))
696 l = &MOUNTH(pg, old->qid);
697 for(m = *l; m; m = m->hash){
698 if(eqchan(m->from, old, 1))
705 * nothing mounted here yet. create a mount
706 * head and add to the hash table.
712 * if this is a union mount, add the old
713 * node to the mount chain.
716 m->mount = newmount(m, old, 0, 0);
725 nm = newmount(m, new, flag, spec);
726 if(mh != nil && mh->mount != nil){
728 * copy a union when binding it onto a directory
735 for(um = um->next; um; um = um->next){
736 f = newmount(m, um->to, flg, um->spec);
742 if(m->mount && order == MREPL){
748 nm->mflag |= MCREATE;
750 if(m->mount && order == MAFTER){
751 for(f = m->mount; f->next; f = f->next)
755 for(f = nm; f->next; f = f->next)
767 cunmount(Chan *mnt, Chan *mounted)
773 if(mnt->umh) /* should not happen */
774 print("cunmount newp extra umh %p has %p\n", mnt, mnt->umh);
777 * It _can_ happen that mounted->umh is non-nil,
778 * because mounted is the result of namec(Aopen)
779 * (see sysfile.c:/^sysunmount).
780 * If we open a union directory, it will have a umh.
781 * Although surprising, this is okay, since the
782 * cclose will take care of freeing the umh.
788 l = &MOUNTH(pg, mnt->qid);
789 for(m = *l; m; m = m->hash){
790 if(eqchan(m->from, mnt, 1))
813 for(f = *p; f; f = f->next){
814 /* BUG: Needs to be 2 pass */
815 if(eqchan(f->to, mounted, 1) ||
816 (f->to->mchan && eqchan(f->to->mchan, mounted, 1))){
845 if(c == nil || c->ref < 1 || c->flag&CFREE)
846 panic("cclone: %#p", getcallerpc(&c));
847 wq = devtab[c->type]->walk(c, nil, nil, 0);
849 error("clone failed");
858 /* also used by sysfile.c:/^mountfix */
860 findmount(Chan **cp, Mhead **mp, int type, int dev, Qid qid)
867 for(m = MOUNTH(pg, qid); m; m = m->hash){
870 print("m %p m->from 0\n", m);
874 if(eqchantdqid(m->from, type, dev, qid, 1)){
884 incref(m->mount->to);
897 * Calls findmount but also updates path.
900 domount(Chan **cp, Mhead **mp, Path **path)
905 if(findmount(cp, mp, (*cp)->type, (*cp)->dev, (*cp)->qid) == 0)
912 print("domount: path %s has mlen==%d\n", p->s, p->mlen);
914 lc = &p->mtpt[p->mlen-1];
915 DBG("domount %p %s => add %p (was %p)\n", p, p->s, (*mp)->from, p->mtpt[p->mlen-1]);
927 * If c is the right-hand-side of a mount point, returns the left hand side.
928 * Changes name to reflect the fact that we've uncrossed the mountpoint,
929 * so name had better be ours to change!
932 undomount(Chan *c, Path *path)
936 if(path->ref != 1 || path->mlen == 0)
937 print("undomount: path %s ref %ld mlen %d caller %#p\n",
938 path->s, path->ref, path->mlen, getcallerpc(&c));
940 if(path->mlen>0 && (nc=path->mtpt[path->mlen-1]) != nil){
941 DBG("undomount %p %s => remove %p\n", path, path->s, nc);
943 path->mtpt[path->mlen-1] = nil;
950 * Call dev walk but catch errors.
953 ewalk(Chan *c, Chan *nc, char **name, int nname)
959 wq = devtab[c->type]->walk(c, nc, name, nname);
965 * Either walks all the way or not at all. No partial results in *cp.
966 * *nerror is the number of names to display in an error message.
968 static char Edoesnotexist[] = "does not exist";
970 walk(Chan **cp, char **names, int nnames, int nomount, int *nerror)
972 int dev, didmount, dotdot, i, n, nhave, ntry, type;
986 * While we haven't gotten all the way down the path:
987 * 1. step through a mount point, if any
988 * 2. send a walk request for initial dotdot or initial prefix without dotdot
989 * 3. move to the first mountpoint along the way.
992 * Each time through the loop:
994 * If didmount==0, c is on the undomount side of the mount point.
995 * If didmount==1, c is on the domount side of the mount point.
996 * Either way, c's full path is path.
999 for(nhave=0; nhave<nnames; nhave+=n){
1000 if((c->qid.type&QTDIR)==0){
1005 kstrcpy(up->errstr, Enotdir, ERRMAX);
1010 ntry = nnames - nhave;
1014 for(i=0; i<ntry; i++){
1015 if(isdotdot(names[nhave+i])){
1025 if(!dotdot && !nomount && !didmount)
1026 domount(&c, &mh, &path);
1031 if((wq = ewalk(c, nil, names+nhave, ntry)) == nil){
1032 /* try a union mount, if any */
1035 * mh->mount->to == c, so start at mh->mount->next
1038 if((f = mh->mount) != nil)
1040 for(; f != nil; f = f->next)
1041 if((wq = ewalk(f->to, nil, names+nhave, ntry)) != nil){
1061 assert(wq->nqid == 1);
1062 assert(wq->clone != nil);
1064 path = addelem(path, "..", nil);
1065 nc = undomount(wq->clone, path);
1072 for(i=0; i<wq->nqid && i<ntry-1; i++){
1073 if(findmount(&nc, &nmh, type, dev, wq->qid[i])){
1079 if(nc == nil){ /* no mount points along path */
1080 if(wq->clone == nil){
1083 if(wq->nqid==0 || (wq->qid[wq->nqid-1].type&QTDIR)){
1085 *nerror = nhave+wq->nqid+1;
1086 kstrcpy(up->errstr, Edoesnotexist, ERRMAX);
1089 *nerror = nhave+wq->nqid;
1090 kstrcpy(up->errstr, Enotdir, ERRMAX);
1099 }else{ /* stopped early, at a mount point */
1101 if(wq->clone != nil){
1111 path = addelem(path, names[nhave+i], mtpt);
1125 if(c->umh != nil){ //BUG
1126 print("walk umh\n");
1142 * c is a mounted non-creatable directory. find a creatable one.
1145 createdir(Chan *c, Mhead *m)
1155 for(f = m->mount; f; f = f->next){
1156 if(f->mflag&MCREATE){
1174 growparse(Elemlist *e)
1180 if(e->nelems % Delta == 0){
1181 new = smalloc((e->nelems+Delta) * sizeof(char*));
1182 memmove(new, e->elems, e->nelems*sizeof(char*));
1185 inew = smalloc((e->nelems+Delta+1) * sizeof(int));
1186 memmove(inew, e->off, (e->nelems+1)*sizeof(int));
1193 * The name is known to be valid.
1194 * Copy the name so slashes can be overwritten.
1195 * An empty string will set nelem=0.
1196 * A path ending in / or /. or /.//./ etc. will have
1197 * e.mustbedir = 1, so that we correctly
1198 * reject, e.g., "/adm/users/." when /adm/users is a file
1199 * rather than a directory.
1202 parsename(char *aname, Elemlist *e)
1206 kstrdup(&e->name, aname);
1210 e->off = smalloc(sizeof(int));
1211 e->off[0] = skipslash(name) - name;
1213 name = skipslash(name);
1215 e->off[e->nelems] = name+strlen(name) - e->name;
1220 e->elems[e->nelems++] = name;
1221 slash = utfrune(name, '/');
1223 e->off[e->nelems] = name+strlen(name) - e->name;
1227 e->off[e->nelems] = slash - e->name;
1235 print("parsename %s:", e->name);
1236 for(i=0; i<=e->nelems; i++)
1237 print(" %d", e->off[i]);
1243 memrchr(void *va, int c, long n)
1248 for(e=a+n-1; e>a; e--)
1255 namelenerror(char *aname, int len, char *err)
1257 char *ename, *name, *next;
1261 * If the name is short enough, just use the whole thing.
1263 errlen = strlen(err);
1264 if(len < ERRMAX/3 || len+errlen < 2*ERRMAX/3)
1265 snprint(up->genbuf, sizeof up->genbuf, "%.*s",
1266 utfnlen(aname, len), aname);
1269 * Print a suffix of the name, but try to get a little info.
1275 next = memrchr(aname, '/', name-aname);
1279 }while(len < ERRMAX/3 || len + errlen < 2*ERRMAX/3);
1282 * If the name is ridiculously long, chop it.
1285 name = ename-ERRMAX/4;
1287 panic("bad math in namelenerror");
1288 /* walk out of current UTF sequence */
1289 for(i=0; (*name&0xC0)==0x80 && i<UTFmax; i++)
1292 snprint(up->genbuf, sizeof up->genbuf, "...%.*s",
1293 utfnlen(name, ename-name), name);
1295 snprint(up->errstr, ERRMAX, "%#q %s", up->genbuf, err);
1300 nameerror(char *name, char *err)
1302 namelenerror(name, strlen(name), err);
1306 * Turn a name into a channel.
1307 * &name[0] is known to be a valid address. It may be a kernel address.
1309 * Opening with amode Aopen, Acreate, Aremove, or Aaccess guarantees
1310 * that the result will be the only reference to that particular fid.
1311 * This is necessary since we might pass the result to
1312 * devtab[]->remove().
1314 * Opening Atodir or Amount does not guarantee this.
1316 * Under certain circumstances, opening Aaccess will cause
1317 * an unnecessary clone in order to get a cunique Chan so it
1318 * can attach the correct name. Sysstat and sys_stat need the
1319 * correct name so they can rewrite the stat info.
1322 namec(char *aname, int amode, int omode, ulong perm)
1324 int len, n, t, nomount;
1330 char *createerr, tmperrbuf[ERRMAX];
1333 if(aname[0] == '\0')
1334 error("empty file name");
1335 aname = validnamedup(aname, 1);
1340 DBG("namec %s %d %d\n", aname, amode, omode);
1344 * Find the starting off point (the current slash, the root of
1345 * a device tree, or the current dot) as well as the name to
1346 * evaluate starting there.
1357 up->genbuf[0] = '\0';
1359 while(*name != '\0' && (*name != '/' || n < 2)){
1360 if(n >= sizeof(up->genbuf)-1)
1362 up->genbuf[n++] = *name++;
1364 up->genbuf[n] = '\0';
1366 * noattach is sandboxing.
1368 * the OK exceptions are:
1369 * | it only gives access to pipes you create
1370 * d this process's file descriptors
1371 * e this process's environment
1372 * the iffy exceptions are:
1373 * c time and pid, but also cons and consctl
1374 * p control of your own processes (and unfortunately
1375 * any others left unprotected)
1377 n = chartorune(&r, up->genbuf+1)+1;
1378 /* actually / is caught by parsing earlier */
1381 if(up->pgrp->noattach && utfrune("|decp", r)==nil)
1386 c = devtab[t]->attach(up->genbuf+n);
1396 e.prefix = name - aname;
1407 * Prepare nice error, showing first e.nerror elements of name.
1411 strcpy(tmperrbuf, up->errstr);
1412 if(e.off[e.nerror]==0)
1413 print("nerror=%d but off=%d\n",
1414 e.nerror, e.off[e.nerror]);
1416 print("showing %d+%d/%d (of %d) of %s (%d %d)\n", e.prefix, e.off[e.nerror], e.nerror, e.nelems, aname, e.off[0], e.off[1]);
1417 len = e.prefix+e.off[e.nerror];
1419 namelenerror(aname, len, tmperrbuf);
1423 * Build a list of elements in the name.
1425 parsename(name, &e);
1430 if(amode == Acreate){
1431 /* perm must have DMDIR if last element is / or /. */
1432 if(e.mustbedir && !(perm&DMDIR)){
1433 e.nerror = e.nelems;
1434 error("create without DMDIR");
1437 /* don't try to walk the last path element just yet. */
1443 if(walk(&c, e.elems, e.nelems, nomount, &e.nerror) < 0){
1444 if(e.nerror < 0 || e.nerror > e.nelems){
1445 print("namec %s walk error nerror=%d\n", aname, e.nerror);
1451 if(e.mustbedir && !(c->qid.type&QTDIR))
1452 error("not a directory");
1454 if(amode == Aopen && (omode&3) == OEXEC && (c->qid.type&QTDIR))
1455 error("cannot exec directory");
1459 /* no need to maintain path - cannot dotdot an Abind */
1462 domount(&c, &m, nil);
1472 /* save&update the name; domount might change c */
1477 domount(&c, &m, &path);
1479 /* our own copy to open or remove */
1482 /* now it's our copy anyway, we can put the name back */
1486 /* record whether c is on a mount point */
1498 print("cunique umh Open\n");
1502 /* only save the mount head if it's a multiple element union */
1503 if(m && m->mount && m->mount->next)
1508 /* save registers else error() in open has wrong value of c saved */
1511 c = devtab[c->type]->open(c, omode&~OCEXEC);
1523 * Directories (e.g. for cd) are left before the mount point,
1524 * so one may mount on / or . and see the effect.
1526 if(!(c->qid.type & QTDIR))
1532 * When mounting on an already mounted upon directory,
1533 * one wants subsequent mounts to be attached to the
1534 * original directory, not the replacement. Don't domount.
1540 * We've already walked all but the last element.
1541 * If the last exists, try to open it OTRUNC.
1542 * If omode&OEXCL is set, just give up.
1546 if(walk(&c, e.elems+e.nelems-1, 1, nomount, nil) == 0){
1554 * The semantics of the create(2) system call are that if the
1555 * file exists and can be written, it is to be opened with truncation.
1556 * On the other hand, the create(5) message fails if the file exists.
1557 * If we get two create(2) calls happening simultaneously,
1558 * they might both get here and send create(5) messages, but only
1559 * one of the messages will succeed. To provide the expected create(2)
1560 * semantics, the call with the failed message needs to try the above
1561 * walk again, opening for truncation. This correctly solves the
1562 * create/create race, in the sense that any observable outcome can
1563 * be explained as one happening before the other.
1564 * The create/create race is quite common. For example, it happens
1565 * when two rc subshells simultaneously update the same
1566 * environment variable.
1568 * The implementation still admits a create/create/remove race:
1569 * (A) walk to file, fails
1570 * (B) walk to file, fails
1571 * (A) create file, succeeds, returns
1572 * (B) create file, fails
1573 * (A) remove file, succeeds, returns
1574 * (B) walk to file, return failure.
1576 * This is hardly as common as the create/create race, and is really
1577 * not too much worse than what might happen if (B) got a hold of a
1578 * file descriptor and then the file was removed -- either way (B) can't do
1579 * anything with the result of the create call. So we don't care about this race.
1581 * Applications that care about more fine-grained decision of the races
1582 * can use the OEXCL flag to get at the underlying create(5) semantics;
1583 * by default we provide the common case.
1585 * We need to stay behind the mount point in case we
1586 * need to do the first walk again (should the create fail).
1588 * We also need to cross the mount point and find the directory
1589 * in the union in which we should be creating.
1591 * The channel staying behind is c, the one moving forward is cnew.
1594 cnew = nil; /* is this assignment necessary? */
1595 if(!waserror()){ /* try create */
1596 if(!nomount && findmount(&cnew, &m, c->type, c->dev, c->qid))
1597 cnew = createdir(cnew, m);
1604 * We need our own copy of the Chan because we're
1605 * about to send a create, which will move it. Once we have
1606 * our own copy, we can fix the name, which might be wrong
1607 * if findmount gave us a new Chan.
1609 cnew = cunique(cnew);
1610 pathclose(cnew->path);
1611 cnew->path = c->path;
1614 cnew = devtab[cnew->type]->create(cnew, e.elems[e.nelems-1], omode&~(OEXCL|OCEXEC), perm);
1617 cnew->flag |= CCEXEC;
1619 cnew->flag |= CRCLOSE;
1624 c->path = addelem(c->path, e.elems[e.nelems-1], nil);
1635 createerr = up->errstr;
1636 up->errstr = tmperrbuf;
1637 /* note: we depend that walk does not error */
1638 if(walk(&c, e.elems+e.nelems-1, 1, nomount, nil) < 0){
1639 up->errstr = createerr;
1640 error(createerr); /* report true error */
1642 up->errstr = createerr;
1647 panic("unknown namec access %d", amode);
1650 /* place final element in genbuf for e.g. exec */
1652 kstrcpy(up->genbuf, e.elems[e.nelems-1], sizeof up->genbuf);
1654 kstrcpy(up->genbuf, ".", sizeof up->genbuf);
1658 poperror(); /* e c */
1660 poperror(); /* aname */
1666 * name is valid. skip leading / and ./ as much as possible
1669 skipslash(char *name)
1671 while(name[0]=='/' || (name[0]=='.' && (name[1]==0 || name[1]=='/')))
1677 /*NUL*/ 1, 1, 1, 1, 1, 1, 1, 1,
1678 /*BKS*/ 1, 1, 1, 1, 1, 1, 1, 1,
1679 /*DLE*/ 1, 1, 1, 1, 1, 1, 1, 1,
1680 /*CAN*/ 1, 1, 1, 1, 1, 1, 1, 1,
1686 * Check that the name
1687 * a) is in valid memory.
1688 * b) is shorter than 2^16 bytes, so it can fit in a 9P string field.
1689 * c) contains no frogs.
1690 * The first byte is known to be addressible by the requester, so the
1691 * routine works for kernel and user memory both.
1692 * The parameter slashok flags whether a slash character is an error
1693 * or a valid character.
1695 * The parameter dup flags whether the string should be copied
1696 * out of user space before being scanned the second time.
1697 * (Otherwise a malicious thread could remove the NUL, causing us
1698 * to access unchecked addresses.)
1701 validname0(char *aname, int slashok, int dup, ulong pc)
1703 char *ename, *name, *s;
1708 if((ulong)name < KZERO){
1710 print("warning: validname called from %#p with user pointer", pc);
1711 ename = vmemchr(name, 0, (1<<16));
1713 ename = memchr(name, 0, (1<<16));
1715 if(ename==nil || ename-name>=(1<<16))
1716 error("name too long");
1722 memmove(s, name, n);
1726 setmalloctag(s, pc);
1730 /* all characters above '~' are ok */
1733 name += chartorune(&r, name);
1736 if(!slashok || c!='/'){
1737 snprint(up->genbuf, sizeof(up->genbuf), "%s: %q", Ebadchar, aname);
1748 validname(char *aname, int slashok)
1750 validname0(aname, slashok, 0, getcallerpc(&aname));
1754 validnamedup(char *aname, int slashok)
1756 return validname0(aname, slashok, 1, getcallerpc(&aname));
1762 if(c->qid.type & QTDIR)
1768 * This is necessary because there are many
1769 * pointers to the top of a given mount list:
1771 * - the mhead in the namespace hash table
1772 * - the mhead in chans returned from findmount:
1773 * used in namec and then by unionread.
1774 * - the mhead in chans returned from createdir:
1775 * used in the open/create race protect, which is gone.
1777 * The RWlock in the Mhead protects the mount list it contains.
1778 * The mount list is deleted when we cunmount.
1779 * The RWlock ensures that nothing is using the mount list at that time.
1781 * It is okay to replace c->mh with whatever you want as
1782 * long as you are sure you have a unique reference to it.
1784 * This comment might belong somewhere else.
1789 if(m && decref(m) == 0){
1790 m->mount = (Mount*)0xCafeBeef;