History

chan: minimize differences with 9front...

cinap_lenrek committed 5 years ago

Commit 823eea41 parent c6f547e4 files · +133 −133Browse files

Unified · Split

4 files
  1. kern/chan.c +104 −97
  2. kern/devmnt.c +3 −0
  3. kern/fns.h +2 −1
  4. kern/sysfile.c +24 −35
kern/chan.c+104−97
@@ -441,6 +441,12 @@ cunique(Chan *c) · 440 unmodified lines
441441 c = nc;
442442 }
443443
444+ if(c->umh != nil){ //BUG
445+ print("cunique umh != nil from %#p\n", getcallerpc(&c));
446+ putmhead(c->umh);
447+ c->umh = nil;
448+ }
449+
444450 return c;
445451 }
446452
@@ -512,128 +518,124 @@ newmhead(Chan *from) · 65 unmodified lines
512518 void
513519 putmhead(Mhead *m)
514520 {
515− if(m != nil && decref(&m->ref) == 0){
516− assert(m->mount == nil);
517− cclose(m->from);
518− free(m);
519− }
521+ if(m == nil)
522+ return;
523+ if(decref(&m->ref))
524+ return;
525+ assert(m->mount == nil);
526+ cclose(m->from);
527+ free(m);
520528 }
521529
522530 int
523−cmount(Chan **newp, Chan *old, int flag, char *spec)
531+cmount(Chan *new, Chan *old, int flag, char *spec)
524532 {
525− int order, flg;
526− Chan *new;
533+ int order;
527534 Mhead *m, **l, *mh;
528− Mount *nm, *f, *um, **h;
535+ Mount *nm, *f, *um;
529536 Pgrp *pg;
530537
531− if(QTDIR & (old->qid.type^(*newp)->qid.type))
532− error(Emount);
533−
534538 if(old->umh != nil)
535− print("cmount: unexpected umh, caller %#p\n", getcallerpc(&newp));
539+ print("cmount: unexpected umh, caller %#p\n", getcallerpc(&new));
540+
541+ if(QTDIR & (old->qid.type^new->qid.type))
542+ error(Emount);
536543
537544 order = flag&MORDER;
538545
539546 if((old->qid.type&QTDIR) == 0 && order != MREPL)
540547 error(Emount);
541548
542− new = *newp;
549+ nm = newmount(new, flag, spec);
543550 mh = new->umh;
551+ if(mh != nil) {
552+ rlock(&mh->lock);
553+ if(waserror()) {
554+ runlock(&mh->lock);
555+ mountfree(nm);
556+ nexterror();
557+ }
558+ um = mh->mount;
559+ if(um != nil){
560+ /*
561+ * Not allowed to bind when the old directory is itself a union.
562+ * (Maybe it should be allowed, but I don't see what the semantics
563+ * would be.)
564+ *
565+ * We need to check mh->mount->next to tell unions apart from
566+ * simple mount points, so that things like
567+ * mount -c fd /root
568+ * bind -c /root /
569+ * work.
570+ *
571+ * The check of mount->mflag allows things like
572+ * mount fd /root
573+ * bind -c /root /
574+ *
575+ * This is far more complicated than it should be, but I don't
576+ * see an easier way at the moment.
577+ */
578+ if((flag&MCREATE) != 0 && (um->next != nil || (um->mflag&MCREATE) == 0))
579+ error(Emount);
544580
545− /*
546− * Not allowed to bind when the old directory is itself a union.
547− * (Maybe it should be allowed, but I don't see what the semantics
548− * would be.)
549− *
550− * We need to check mh->mount->next to tell unions apart from
551− * simple mount points, so that things like
552− * mount -c fd /root
553− * bind -c /root /
554− * work.
555− *
556− * The check of mount->mflag allows things like
557− * mount fd /root
558− * bind -c /root /
559− *
560− * This is far more complicated than it should be, but I don't
561− * see an easier way at the moment.
562− */
563− if((flag&MCREATE) != 0 && mh != nil && mh->mount != nil
564− && (mh->mount->next != nil || (mh->mount->mflag&MCREATE) == 0))
565− error(Emount);
581+ /*
582+ * copy a union when binding it onto a directory
583+ */
584+ f = nm;
585+ for(um = um->next; um != nil; um = um->next){
586+ f->next = newmount(um->to, order==MREPL? MAFTER: order, um->spec);
587+ f = f->next;
588+ }
589+ }
590+ runlock(&mh->lock);
591+ poperror();
592+ }
566593
567594 pg = up->pgrp;
568595 wlock(&pg->ns);
569−
570596 l = &MOUNTH(pg, old->qid);
571597 for(m = *l; m != nil; m = m->hash){
572598 if(eqchan(m->from, old, 1))
573599 break;
574600 l = &m->hash;
575601 }
576−
577602 if(m == nil){
578603 /*
579604 * nothing mounted here yet. create a mount
580605 * head and add to the hash table.
581606 */
582607 m = newmhead(old);
583− *l = m;
584−
585608 /*
586609 * if this is a union mount, add the old
587610 * node to the mount chain.
588611 */
589612 if(order != MREPL)
590613 m->mount = newmount(old, 0, nil);
614+ *l = m;
591615 }
592616 wlock(&m->lock);
593− if(waserror()){
594− wunlock(&m->lock);
595− nexterror();
596− }
597− wunlock(&pg->ns);
598−
599− nm = newmount(new, flag, spec);
600− if(mh != nil && mh->mount != nil){
601− /*
602− * copy a union when binding it onto a directory
603− */
604− flg = order;
605− if(order == MREPL)
606− flg = MAFTER;
607− h = &nm->next;
608− um = mh->mount;
609− for(um = um->next; um != nil; um = um->next){
610− f = newmount(um->to, flg, um->spec);
611− *h = f;
612− h = &f->next;
613− }
614− }
615−
616− if(m->mount != nil && order == MREPL){
617− mountfree(m->mount);
618− m->mount = nil;
619− }
620−
621− if(flag & MCREATE)
622− nm->mflag |= MCREATE;
623−
624− if(m->mount != nil && order == MAFTER){
625− for(f = m->mount; f->next != nil; f = f->next)
617+ um = m->mount;
618+ if(um != nil && order == MAFTER){
619+ for(f = um; f->next != nil; f = f->next)
626620 ;
627621 f->next = nm;
628− }else{
629− for(f = nm; f->next != nil; f = f->next)
630− ;
631− f->next = m->mount;
622+ um = nil;
623+ } else {
624+ if(order != MREPL){
625+ for(f = nm; f->next != nil; f = f->next)
626+ ;
627+ f->next = um;
628+ um = nil;
629+ }
632630 m->mount = nm;
633631 }
632+ order = nm->mountid;
634633 wunlock(&m->lock);
635− poperror();
636− return nm->mountid;
634+ wunlock(&pg->ns);
635+
636+ mountfree(um);
637+
638+ return order;
637639 }
638640
639641 void
@@ -736,13 +738,13 @@ findmount(Chan **cp, Mhead **mp, int type, int dev, Qid qid) · 96 unmodified lines
736738 rlock(&pg->ns);
737739 for(m = MOUNTH(pg, qid); m != nil; m = m->hash){
738740 if(eqchantdqid(m->from, type, dev, qid, 1)){
739− rlock(&m->lock);
740− runlock(&pg->ns);
741741 if(mp != nil)
742742 incref(&m->ref);
743+ rlock(&m->lock);
743744 to = m->mount->to;
744745 incref(&to->ref);
745746 runlock(&m->lock);
747+ runlock(&pg->ns);
746748 if(mp != nil){
747749 putmhead(*mp);
748750 *mp = m;
@@ -957,7 +959,7 @@ walk(Chan **cp, char **names, int nnames, int nomount, int *nerror) · 208 unmodified lines
957959 n = wq->nqid;
958960 nc = wq->clone;
959961 }else{ /* stopped early, at a mount point */
960− didmount = 1;
962+ assert(didmount);
961963 if(wq->clone != nil){
962964 cclose(wq->clone);
963965 wq->clone = nil;
@@ -979,11 +981,6 @@ walk(Chan **cp, char **names, int nnames, int nomount, int *nerror) · 15 unmodified lines
979981 }
980982 putmhead(mh);
981983 c = cunique(c);
982− if(c->umh != nil){ //BUG
983− print("walk umh\n");
984− putmhead(c->umh);
985− c->umh = nil;
986− }
987984
988985 pathclose(c->path);
989986 c->path = path;
@@ -1293,8 +1290,13 @@ namec(char *aname, int amode, int omode, ulong perm) · 303 unmodified lines
12931290 m = nil;
12941291 if(!nomount)
12951292 domount(&c, &m, nil);
1296− putmhead(c->umh);
1293+ if(waserror()){
1294+ putmhead(m);
1295+ nexterror();
1296+ }
1297+ c = cunique(c);
12971298 c->umh = m;
1299+ poperror();
12981300 break;
12991301
13001302 case Aaccess:
@@ -1311,9 +1313,13 @@ namec(char *aname, int amode, int omode, ulong perm) · 10 unmodified lines
13111313 m = nil;
13121314 if(!nomount)
13131315 domount(&c, &m, &path);
1314−
1316+ if(waserror()){
1317+ putmhead(m);
1318+ nexterror();
1319+ }
13151320 /* our own copy to open or remove */
13161321 c = cunique(c);
1322+ poperror();
13171323
13181324 /* now it's our copy anyway, we can put the name back */
13191325 pathclose(c->path);
@@ -1331,16 +1337,17 @@ namec(char *aname, int amode, int omode, ulong perm) · 11 unmodified lines
13311337
13321338 case Aopen:
13331339 case Acreate:
1334− if(c->umh != nil){
1335− print("cunique umh Open\n");
1336− putmhead(c->umh);
1337− c->umh = nil;
1338− }
13391340 /* only save the mount head if it's a multiple element union */
1340− if(m != nil && m->mount != nil && m->mount->next != nil)
1341− c->umh = m;
1342− else
1343− putmhead(m);
1341+ if(m != nil) {
1342+ rlock(&m->lock);
1343+ if(m->mount != nil && m->mount->next != nil) {
1344+ c->umh = m;
1345+ runlock(&m->lock);
1346+ } else {
1347+ runlock(&m->lock);
1348+ putmhead(m);
1349+ }
1350+ }
13441351
13451352 /* save registers else error() in open has wrong value of c saved */
13461353 saveregisters();
kern/devmnt.c+3−0
@@ -301,6 +301,9 @@ mntattach(Chan *c, Chan *ac, char *spec, int flags) · 300 unmodified lines
301301 Mnt *m;
302302 Mntrpc *r;
303303
304+ if(ac != nil && ac->mchan != c)
305+ error(Ebadusefd);
306+
304307 m = c->mux;
305308 if(m == nil){
306309 mntversion(c, nil, 0, 0);
kern/fns.h+2−1
@@ -25,7 +25,7 @@ void closemount(Mount*); · 24 unmodified lines
2525 void closepgrp(Pgrp*);
2626 void closergrp(Rgrp*);
2727 void cmderror(Cmdbuf*, char*);
28−int cmount(Chan**, Chan*, int, char*);
28+int cmount(Chan*, Chan*, int, char*);
2929 Block* concatblock(Block*);
3030 Block* copyblock(Block*, int);
3131 void cunmount(Chan*, Chan*);
@@ -101,6 +101,7 @@ void mkqid(Qid*, vlong, ulong, int); · 69 unmodified lines
101101 Chan* mntauth(Chan*, char*);
102102 void mntdump(void);
103103 long mntversion(Chan*, char*, int, int);
104+Chan* mntattach(Chan*, Chan*, char*, int);
104105 void mountfree(Mount*);
105106 void muxclose(Mnt*);
106107 Chan* namec(char*, int, int, ulong);
kern/sysfile.c+24−35
@@ -293,7 +293,7 @@ _sysopen(char *name, int mode) · 292 unmodified lines
293293 }
294294
295295 static void
296−closefd(int fd, int flag)
296+fdclose(int fd, int flag)
297297 {
298298 int i;
299299 Chan *c;
@@ -325,7 +325,7 @@ long · 25 unmodified lines
325325 _sysclose(int fd)
326326 {
327327 fdtochan(fd, -1, 0, 0);
328− closefd(fd, 0);
328+ fdclose(fd, 0);
329329
330330 return 0;
331331 }
@@ -636,31 +636,30 @@ _syschdir(char *name) · 304 unmodified lines
636636 return 0;
637637 }
638638
639−long
640−bindmount(int ismount, int fd, int afd, char* arg0, char* arg1, ulong flag, char* spec)
639+static int
640+bindmount(int ismount, int fd, int afd, char* arg0, char* arg1, int flag, char* spec)
641641 {
642642 int ret;
643643 Chan *c0, *c1, *ac, *bc;
644− struct{
645− Chan *chan;
646− Chan *authchan;
647− char *spec;
648− int flags;
649− }bogus;
650644
651645 if((flag&~MMASK) || (flag&MORDER)==(MBEFORE|MAFTER))
652646 error(Ebadarg);
653647
654− bogus.flags = flag & MCACHE;
655−
656648 if(ismount){
649+ validaddr((uintptr)spec, 1, 0);
650+ spec = validnamedup(spec, 1);
651+ if(waserror()){
652+ free(spec);
653+ nexterror();
654+ }
655+
657656 if(up->pgrp->noattach)
658657 error(Enoattach);
659658
660659 ac = nil;
661660 bc = fdtochan(fd, ORDWR, 0, 1);
662661 if(waserror()) {
663− if(ac)
662+ if(ac != nil)
664663 cclose(ac);
665664 cclose(bc);
666665 nexterror();
@@ -669,26 +668,14 @@ bindmount(int ismount, int fd, int afd, char* arg0, char* arg1, ulong flag, char · 2 unmodified lines
669668 if(afd >= 0)
670669 ac = fdtochan(afd, ORDWR, 0, 1);
671670
672− bogus.chan = bc;
673− bogus.authchan = ac;
674−
675− validaddr((ulong)spec, 1, 0);
676− bogus.spec = spec;
677− if(waserror())
678− error(Ebadspec);
679− validname(spec, 1);
680− poperror();
681−
682− ret = devno('M', 0);
683− c0 = devtab[ret]->attach((char*)&bogus);
684−
685− poperror();
686− if(ac)
671+ c0 = mntattach(bc, ac, spec, flag&MCACHE);
672+ poperror(); /* ac bc */
673+ if(ac != nil)
687674 cclose(ac);
688675 cclose(bc);
689676 }else{
690− bogus.spec = 0;
691− validaddr((ulong)arg0, 1, 0);
677+ spec = nil;
678+ validaddr((uintptr)arg0, 1, 0);
692679 c0 = namec(arg0, Abind, 0, 0);
693680 }
694681
@@ -697,22 +684,24 @@ bindmount(int ismount, int fd, int afd, char* arg0, char* arg1, ulong flag, char · 2 unmodified lines
697684 nexterror();
698685 }
699686
700− validaddr((ulong)arg1, 1, 0);
687+ validaddr((uintptr)arg1, 1, 0);
701688 c1 = namec(arg1, Amount, 0, 0);
702689 if(waserror()){
703690 cclose(c1);
704691 nexterror();
705692 }
706693
707− ret = cmount(&c0, c1, flag, bogus.spec);
694+ ret = cmount(c0, c1, flag, spec);
708695
709696 poperror();
710697 cclose(c1);
711698 poperror();
712699 cclose(c0);
713− if(ismount)
714− closefd(fd, 0);
715−
700+ if(ismount){
701+ fdclose(fd, 0);
702+ poperror();
703+ free(spec);
704+ }
716705 return ret;
717706 }
718707

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t