chan.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. #include "dat.h"
  2. #include "fns.h"
  3. #include "error.h"
  4. char*
  5. c2name(Chan *c) /* DEBUGGING */
  6. {
  7. if(c == nil)
  8. return "<nil chan>";
  9. if(c->name == nil)
  10. return "<nil name>";
  11. if(c->name->s == nil)
  12. return "<nil name.s>";
  13. return c->name->s;
  14. }
  15. enum
  16. {
  17. CNAMESLOP = 20
  18. };
  19. struct
  20. {
  21. Lock l;
  22. int fid;
  23. Chan *free;
  24. Chan *list;
  25. }chanalloc;
  26. typedef struct Elemlist Elemlist;
  27. struct Elemlist
  28. {
  29. char *name; /* copy of name, so '/' can be overwritten */
  30. int nelems;
  31. char **elems;
  32. int *off;
  33. int mustbedir;
  34. };
  35. #define SEP(c) ((c) == 0 || (c) == '/')
  36. void cleancname(Cname*);
  37. int
  38. isdotdot(char *p)
  39. {
  40. return p[0]=='.' && p[1]=='.' && p[2]=='\0';
  41. }
  42. int
  43. incref(Ref *r)
  44. {
  45. int x;
  46. lock(&r->lk);
  47. x = ++r->ref;
  48. unlock(&r->lk);
  49. return x;
  50. }
  51. int
  52. decref(Ref *r)
  53. {
  54. int x;
  55. lock(&r->lk);
  56. x = --r->ref;
  57. unlock(&r->lk);
  58. if(x < 0)
  59. panic("decref, pc=0x%lux", getcallerpc(&r));
  60. return x;
  61. }
  62. /*
  63. * Rather than strncpy, which zeros the rest of the buffer, kstrcpy
  64. * truncates if necessary, always zero terminates, does not zero fill,
  65. * and puts ... at the end of the string if it's too long. Usually used to
  66. * save a string in up->genbuf;
  67. */
  68. void
  69. kstrcpy(char *s, char *t, int ns)
  70. {
  71. int nt;
  72. nt = strlen(t);
  73. if(nt+1 <= ns){
  74. memmove(s, t, nt+1);
  75. return;
  76. }
  77. /* too long */
  78. if(ns < 4){
  79. /* but very short! */
  80. strncpy(s, t, ns);
  81. return;
  82. }
  83. /* truncate with ... at character boundary (very rare case) */
  84. memmove(s, t, ns-4);
  85. ns -= 4;
  86. s[ns] = '\0';
  87. /* look for first byte of UTF-8 sequence by skipping continuation bytes */
  88. while(ns>0 && (s[--ns]&0xC0)==0x80)
  89. ;
  90. strcpy(s+ns, "...");
  91. }
  92. int
  93. emptystr(char *s)
  94. {
  95. return s == nil || s[0] == '\0';
  96. }
  97. /*
  98. * Atomically replace *p with copy of s
  99. */
  100. void
  101. kstrdup(char **p, char *s)
  102. {
  103. int n;
  104. char *t, *prev;
  105. n = strlen(s)+1;
  106. t = kmalloc(n);
  107. if(t == nil)
  108. panic("kstrdup: no memory");
  109. memmove(t, s, n);
  110. prev = *p;
  111. *p = t;
  112. free(prev);
  113. }
  114. static char isfrog[256]=
  115. {
  116. /*NUL*/ 1, 1, 1, 1, 1, 1, 1, 1,
  117. /*BKS*/ 1, 1, 1, 1, 1, 1, 1, 1,
  118. /*DLE*/ 1, 1, 1, 1, 1, 1, 1, 1,
  119. /*CAN*/ 1, 1, 1, 1, 1, 1, 1, 1
  120. };
  121. void
  122. chandevinit(void)
  123. {
  124. int i;
  125. /* isfrog[' '] = 1; */ /* let's see what happens */
  126. isfrog['/'] = 1;
  127. isfrog[0x7f] = 1;
  128. for(i=0; devtab[i] != nil; i++)
  129. devtab[i]->init();
  130. }
  131. Chan*
  132. newchan(void)
  133. {
  134. Chan *c;
  135. lock(&chanalloc.l);
  136. c = chanalloc.free;
  137. if(c != 0)
  138. chanalloc.free = c->next;
  139. unlock(&chanalloc.l);
  140. if(c == nil) {
  141. c = malloc(sizeof(Chan));
  142. if(c == nil)
  143. error(Enomem);
  144. lock(&chanalloc.l);
  145. c->fid = ++chanalloc.fid;
  146. c->link = chanalloc.list;
  147. chanalloc.list = c;
  148. unlock(&chanalloc.l);
  149. }
  150. /* if you get an error before associating with a dev,
  151. close calls rootclose, a nop */
  152. c->type = 0;
  153. c->flag = 0;
  154. c->r.ref = 1;
  155. c->dev = 0;
  156. c->offset = 0;
  157. c->iounit = 0;
  158. c->umh = 0;
  159. c->uri = 0;
  160. c->dri = 0;
  161. c->aux = 0;
  162. c->mchan = 0;
  163. c->mcp = 0;
  164. c->mux = 0;
  165. c->mqid.path = 0;
  166. c->mqid.vers = 0;
  167. c->mqid.type = 0;
  168. c->name = 0;
  169. return c;
  170. }
  171. static Ref ncname;
  172. Cname*
  173. newcname(char *s)
  174. {
  175. Cname *n;
  176. int i;
  177. n = smalloc(sizeof(Cname));
  178. i = strlen(s);
  179. n->len = i;
  180. n->alen = i+CNAMESLOP;
  181. n->s = smalloc(n->alen);
  182. memmove(n->s, s, i+1);
  183. n->r.ref = 1;
  184. incref(&ncname);
  185. return n;
  186. }
  187. void
  188. cnameclose(Cname *n)
  189. {
  190. if(n == nil)
  191. return;
  192. if(decref(&n->r))
  193. return;
  194. decref(&ncname);
  195. free(n->s);
  196. free(n);
  197. }
  198. Cname*
  199. addelem(Cname *n, char *s)
  200. {
  201. int i, a;
  202. char *t;
  203. Cname *new;
  204. if(s[0]=='.' && s[1]=='\0')
  205. return n;
  206. if(n->r.ref > 1){
  207. /* copy on write */
  208. new = newcname(n->s);
  209. cnameclose(n);
  210. n = new;
  211. }
  212. i = strlen(s);
  213. if(n->len+1+i+1 > n->alen){
  214. a = n->len+1+i+1 + CNAMESLOP;
  215. t = smalloc(a);
  216. memmove(t, n->s, n->len+1);
  217. free(n->s);
  218. n->s = t;
  219. n->alen = a;
  220. }
  221. if(n->len>0 && n->s[n->len-1]!='/' && s[0]!='/') /* don't insert extra slash if one is present */
  222. n->s[n->len++] = '/';
  223. memmove(n->s+n->len, s, i+1);
  224. n->len += i;
  225. if(isdotdot(s))
  226. cleancname(n);
  227. return n;
  228. }
  229. void
  230. chanfree(Chan *c)
  231. {
  232. c->flag = CFREE;
  233. if(c->umh != nil){
  234. putmhead(c->umh);
  235. c->umh = nil;
  236. }
  237. if(c->umc != nil){
  238. cclose(c->umc);
  239. c->umc = nil;
  240. }
  241. if(c->mux != nil){
  242. muxclose(c->mux);
  243. c->mux = nil;
  244. }
  245. if(c->mchan != nil){
  246. cclose(c->mchan);
  247. c->mchan = nil;
  248. }
  249. cnameclose(c->name);
  250. lock(&chanalloc.l);
  251. c->next = chanalloc.free;
  252. chanalloc.free = c;
  253. unlock(&chanalloc.l);
  254. }
  255. void
  256. cclose(Chan *c)
  257. {
  258. if(c == 0)
  259. return;
  260. if(c->flag&CFREE)
  261. panic("cclose %lux", getcallerpc(&c));
  262. if(decref(&c->r))
  263. return;
  264. if(!waserror()){
  265. devtab[c->type]->close(c);
  266. poperror();
  267. }
  268. chanfree(c);
  269. }
  270. /*
  271. * Make sure we have the only copy of c. (Copy on write.)
  272. */
  273. Chan*
  274. cunique(Chan *c)
  275. {
  276. Chan *nc;
  277. if(c->r.ref != 1) {
  278. nc = cclone(c);
  279. cclose(c);
  280. c = nc;
  281. }
  282. return c;
  283. }
  284. int
  285. eqqid(Qid a, Qid b)
  286. {
  287. return a.path==b.path && a.vers==b.vers;
  288. }
  289. int
  290. eqchan(Chan *a, Chan *b, int pathonly)
  291. {
  292. if(a->qid.path != b->qid.path)
  293. return 0;
  294. if(!pathonly && a->qid.vers!=b->qid.vers)
  295. return 0;
  296. if(a->type != b->type)
  297. return 0;
  298. if(a->dev != b->dev)
  299. return 0;
  300. return 1;
  301. }
  302. int
  303. eqchantdqid(Chan *a, int type, int dev, Qid qid, int pathonly)
  304. {
  305. if(a->qid.path != qid.path)
  306. return 0;
  307. if(!pathonly && a->qid.vers!=qid.vers)
  308. return 0;
  309. if(a->type != type)
  310. return 0;
  311. if(a->dev != dev)
  312. return 0;
  313. return 1;
  314. }
  315. Mhead*
  316. newmhead(Chan *from)
  317. {
  318. Mhead *mh;
  319. mh = smalloc(sizeof(Mhead));
  320. mh->r.ref = 1;
  321. mh->from = from;
  322. incref(&from->r);
  323. /*
  324. n = from->name->len;
  325. if(n >= sizeof(mh->fromname))
  326. n = sizeof(mh->fromname)-1;
  327. memmove(mh->fromname, from->name->s, n);
  328. mh->fromname[n] = 0;
  329. */
  330. return mh;
  331. }
  332. int
  333. cmount(Chan *new, Chan *old, int flag, char *spec)
  334. {
  335. Pgrp *pg;
  336. int order, flg;
  337. Mhead *m, **l, *mh;
  338. Mount *nm, *f, *um, **h;
  339. if(QTDIR & (old->qid.type^new->qid.type))
  340. error(Emount);
  341. if(old->umh)
  342. print("cmount old extra umh\n");
  343. order = flag&MORDER;
  344. if((old->qid.type&QTDIR)==0 && order != MREPL)
  345. error(Emount);
  346. mh = new->umh;
  347. /*
  348. * Not allowed to bind when the old directory
  349. * is itself a union. (Maybe it should be allowed, but I don't see
  350. * what the semantics would be.)
  351. *
  352. * We need to check mh->mount->next to tell unions apart from
  353. * simple mount points, so that things like
  354. * mount -c fd /root
  355. * bind -c /root /
  356. * work. The check of mount->mflag catches things like
  357. * mount fd /root
  358. * bind -c /root /
  359. *
  360. * This is far more complicated than it should be, but I don't
  361. * see an easier way at the moment. -rsc
  362. */
  363. if((flag&MCREATE) && mh && mh->mount
  364. && (mh->mount->next || !(mh->mount->mflag&MCREATE)))
  365. error(Emount);
  366. pg = up->env->pgrp;
  367. wlock(&pg->ns);
  368. l = &MOUNTH(pg, old->qid);
  369. for(m = *l; m; m = m->hash) {
  370. if(eqchan(m->from, old, 1))
  371. break;
  372. l = &m->hash;
  373. }
  374. if(m == nil) {
  375. /*
  376. * nothing mounted here yet. create a mount
  377. * head and add to the hash table.
  378. */
  379. m = newmhead(old);
  380. *l = m;
  381. /*
  382. * if this is a union mount, add the old
  383. * node to the mount chain.
  384. */
  385. if(order != MREPL)
  386. m->mount = newmount(m, old, 0, 0);
  387. }
  388. wlock(&m->lock);
  389. if(waserror()){
  390. wunlock(&m->lock);
  391. nexterror();
  392. }
  393. wunlock(&pg->ns);
  394. nm = newmount(m, new, flag, spec);
  395. if(mh != nil && mh->mount != nil) {
  396. /*
  397. * copy a union when binding it onto a directory
  398. */
  399. flg = order;
  400. if(order == MREPL)
  401. flg = MAFTER;
  402. h = &nm->next;
  403. um = mh->mount;
  404. for(um = um->next; um; um = um->next) {
  405. f = newmount(m, um->to, flg, um->spec);
  406. *h = f;
  407. h = &f->next;
  408. }
  409. }
  410. if(m->mount && order == MREPL) {
  411. mountfree(m->mount);
  412. m->mount = 0;
  413. }
  414. if(flag & MCREATE)
  415. nm->mflag |= MCREATE;
  416. if(m->mount && order == MAFTER) {
  417. for(f = m->mount; f->next; f = f->next)
  418. ;
  419. f->next = nm;
  420. }
  421. else {
  422. for(f = nm; f->next; f = f->next)
  423. ;
  424. f->next = m->mount;
  425. m->mount = nm;
  426. }
  427. wunlock(&m->lock);
  428. poperror();
  429. return nm->mountid;
  430. }
  431. void
  432. cunmount(Chan *mnt, Chan *mounted)
  433. {
  434. Pgrp *pg;
  435. Mhead *m, **l;
  436. Mount *f, **p;
  437. if(mnt->umh) /* should not happen */
  438. print("cunmount newp extra umh %p has %p\n", mnt, mnt->umh);
  439. /*
  440. * It _can_ happen that mounted->umh is non-nil,
  441. * because mounted is the result of namec(Aopen)
  442. * (see sysfile.c:/^sysunmount).
  443. * If we open a union directory, it will have a umh.
  444. * Although surprising, this is okay, since the
  445. * cclose will take care of freeing the umh.
  446. */
  447. pg = up->env->pgrp;
  448. wlock(&pg->ns);
  449. l = &MOUNTH(pg, mnt->qid);
  450. for(m = *l; m; m = m->hash) {
  451. if(eqchan(m->from, mnt, 1))
  452. break;
  453. l = &m->hash;
  454. }
  455. if(m == 0) {
  456. wunlock(&pg->ns);
  457. error(Eunmount);
  458. }
  459. wlock(&m->lock);
  460. if(mounted == 0) {
  461. *l = m->hash;
  462. wunlock(&pg->ns);
  463. mountfree(m->mount);
  464. m->mount = nil;
  465. cclose(m->from);
  466. wunlock(&m->lock);
  467. putmhead(m);
  468. return;
  469. }
  470. p = &m->mount;
  471. for(f = *p; f; f = f->next) {
  472. /* BUG: Needs to be 2 pass */
  473. if(eqchan(f->to, mounted, 1) ||
  474. (f->to->mchan && eqchan(f->to->mchan, mounted, 1))) {
  475. *p = f->next;
  476. f->next = 0;
  477. mountfree(f);
  478. if(m->mount == nil) {
  479. *l = m->hash;
  480. cclose(m->from);
  481. wunlock(&m->lock);
  482. wunlock(&pg->ns);
  483. putmhead(m);
  484. return;
  485. }
  486. wunlock(&m->lock);
  487. wunlock(&pg->ns);
  488. return;
  489. }
  490. p = &f->next;
  491. }
  492. wunlock(&m->lock);
  493. wunlock(&pg->ns);
  494. error(Eunion);
  495. }
  496. Chan*
  497. cclone(Chan *c)
  498. {
  499. Chan *nc;
  500. Walkqid *wq;
  501. wq = devtab[c->type]->walk(c, nil, nil, 0);
  502. if(wq == nil)
  503. error("clone failed");
  504. nc = wq->clone;
  505. free(wq);
  506. nc->name = c->name;
  507. if(c->name)
  508. incref(&c->name->r);
  509. return nc;
  510. }
  511. int
  512. findmount(Chan **cp, Mhead **mp, int type, int dev, Qid qid)
  513. {
  514. Pgrp *pg;
  515. Mhead *m;
  516. pg = up->env->pgrp;
  517. rlock(&pg->ns);
  518. for(m = MOUNTH(pg, qid); m; m = m->hash){
  519. rlock(&m->lock);
  520. if(m->from == nil){
  521. print("m %p m->from 0\n", m);
  522. runlock(&m->lock);
  523. continue;
  524. }
  525. if(eqchantdqid(m->from, type, dev, qid, 1)) {
  526. runlock(&pg->ns);
  527. if(mp != nil){
  528. incref(&m->r);
  529. if(*mp != nil)
  530. putmhead(*mp);
  531. *mp = m;
  532. }
  533. if(*cp != nil)
  534. cclose(*cp);
  535. incref(&m->mount->to->r);
  536. *cp = m->mount->to;
  537. runlock(&m->lock);
  538. return 1;
  539. }
  540. runlock(&m->lock);
  541. }
  542. runlock(&pg->ns);
  543. return 0;
  544. }
  545. int
  546. domount(Chan **cp, Mhead **mp)
  547. {
  548. return findmount(cp, mp, (*cp)->type, (*cp)->dev, (*cp)->qid);
  549. }
  550. Chan*
  551. undomount(Chan *c, Cname *name)
  552. {
  553. Chan *nc;
  554. Pgrp *pg;
  555. Mount *t;
  556. Mhead **h, **he, *f;
  557. pg = up->env->pgrp;
  558. rlock(&pg->ns);
  559. if(waserror()) {
  560. runlock(&pg->ns);
  561. nexterror();
  562. }
  563. he = &pg->mnthash[MNTHASH];
  564. for(h = pg->mnthash; h < he; h++) {
  565. for(f = *h; f; f = f->hash) {
  566. if(strcmp(f->from->name->s, name->s) != 0)
  567. continue;
  568. for(t = f->mount; t; t = t->next) {
  569. if(eqchan(c, t->to, 1)) {
  570. /*
  571. * We want to come out on the left hand side of the mount
  572. * point using the element of the union that we entered on.
  573. * To do this, find the element that has a from name of
  574. * c->name->s.
  575. */
  576. if(strcmp(t->head->from->name->s, name->s) != 0)
  577. continue;
  578. nc = t->head->from;
  579. incref(&nc->r);
  580. cclose(c);
  581. c = nc;
  582. break;
  583. }
  584. }
  585. }
  586. }
  587. poperror();
  588. runlock(&pg->ns);
  589. return c;
  590. }
  591. /*
  592. * Either walks all the way or not at all. No partial results in *cp.
  593. * *nerror is the number of names to display in an error message.
  594. */
  595. static char Edoesnotexist[] = "does not exist";
  596. int
  597. walk(Chan **cp, char **names, int nnames, int nomount, int *nerror)
  598. {
  599. int dev, dotdot, i, n, nhave, ntry, type;
  600. Chan *c, *nc;
  601. Cname *cname;
  602. Mount *f;
  603. Mhead *mh, *nmh;
  604. Walkqid *wq;
  605. c = *cp;
  606. incref(&c->r);
  607. cname = c->name;
  608. incref(&cname->r);
  609. mh = nil;
  610. /*
  611. * While we haven't gotten all the way down the path:
  612. * 1. step through a mount point, if any
  613. * 2. send a walk request for initial dotdot or initial prefix without dotdot
  614. * 3. move to the first mountpoint along the way.
  615. * 4. repeat.
  616. *
  617. * An invariant is that each time through the loop, c is on the undomount
  618. * side of the mount point, and c's name is cname.
  619. */
  620. for(nhave=0; nhave<nnames; nhave+=n){
  621. if((c->qid.type&QTDIR)==0){
  622. if(nerror)
  623. *nerror = nhave;
  624. cnameclose(cname);
  625. cclose(c);
  626. strcpy(up->env->errstr, Enotdir);
  627. if(mh != nil)
  628. putmhead(mh);
  629. return -1;
  630. }
  631. ntry = nnames - nhave;
  632. if(ntry > MAXWELEM)
  633. ntry = MAXWELEM;
  634. dotdot = 0;
  635. for(i=0; i<ntry; i++){
  636. if(isdotdot(names[nhave+i])){
  637. if(i==0) {
  638. dotdot = 1;
  639. ntry = 1;
  640. } else
  641. ntry = i;
  642. break;
  643. }
  644. }
  645. if(!dotdot && !nomount)
  646. domount(&c, &mh);
  647. type = c->type;
  648. dev = c->dev;
  649. if((wq = devtab[type]->walk(c, nil, names+nhave, ntry)) == nil){
  650. /* try a union mount, if any */
  651. if(mh && !nomount){
  652. /*
  653. * mh->mount == c, so start at mh->mount->next
  654. */
  655. rlock(&mh->lock);
  656. for(f = mh->mount->next; f; f = f->next)
  657. if((wq = devtab[f->to->type]->walk(f->to, nil, names+nhave, ntry)) != nil)
  658. break;
  659. runlock(&mh->lock);
  660. if(f != nil){
  661. type = f->to->type;
  662. dev = f->to->dev;
  663. }
  664. }
  665. if(wq == nil){
  666. cclose(c);
  667. cnameclose(cname);
  668. if(nerror)
  669. *nerror = nhave+1;
  670. if(mh != nil)
  671. putmhead(mh);
  672. return -1;
  673. }
  674. }
  675. nmh = nil;
  676. if(dotdot) {
  677. assert(wq->nqid == 1);
  678. assert(wq->clone != nil);
  679. cname = addelem(cname, "..");
  680. nc = undomount(wq->clone, cname);
  681. n = 1;
  682. } else {
  683. nc = nil;
  684. if(!nomount)
  685. for(i=0; i<wq->nqid && i<ntry-1; i++)
  686. if(findmount(&nc, &nmh, type, dev, wq->qid[i]))
  687. break;
  688. if(nc == nil){ /* no mount points along path */
  689. if(wq->clone == nil){
  690. cclose(c);
  691. cnameclose(cname);
  692. if(wq->nqid==0 || (wq->qid[wq->nqid-1].type&QTDIR)){
  693. if(nerror)
  694. *nerror = nhave+wq->nqid+1;
  695. strcpy(up->env->errstr, Edoesnotexist);
  696. }else{
  697. if(nerror)
  698. *nerror = nhave+wq->nqid;
  699. strcpy(up->env->errstr, Enotdir);
  700. }
  701. free(wq);
  702. if(mh != nil)
  703. putmhead(mh);
  704. return -1;
  705. }
  706. n = wq->nqid;
  707. nc = wq->clone;
  708. }else{ /* stopped early, at a mount point */
  709. if(wq->clone != nil){
  710. cclose(wq->clone);
  711. wq->clone = nil;
  712. }
  713. n = i+1;
  714. }
  715. for(i=0; i<n; i++)
  716. cname = addelem(cname, names[nhave+i]);
  717. }
  718. cclose(c);
  719. c = nc;
  720. putmhead(mh);
  721. mh = nmh;
  722. free(wq);
  723. }
  724. putmhead(mh);
  725. c = cunique(c);
  726. if(c->umh != nil){ /* BUG */
  727. print("walk umh\n");
  728. putmhead(c->umh);
  729. c->umh = nil;
  730. }
  731. cnameclose(c->name);
  732. c->name = cname;
  733. cclose(*cp);
  734. *cp = c;
  735. if(nerror)
  736. *nerror = 0;
  737. return 0;
  738. }
  739. /*
  740. * c is a mounted non-creatable directory. find a creatable one.
  741. */
  742. Chan*
  743. createdir(Chan *c, Mhead *m)
  744. {
  745. Chan *nc;
  746. Mount *f;
  747. rlock(&m->lock);
  748. if(waserror()) {
  749. runlock(&m->lock);
  750. nexterror();
  751. }
  752. for(f = m->mount; f; f = f->next) {
  753. if(f->mflag&MCREATE) {
  754. nc = cclone(f->to);
  755. runlock(&m->lock);
  756. poperror();
  757. cclose(c);
  758. return nc;
  759. }
  760. }
  761. error(Enocreate);
  762. return 0;
  763. }
  764. /*
  765. * In place, rewrite name to compress multiple /, eliminate ., and process ..
  766. */
  767. void
  768. cleancname(Cname *n)
  769. {
  770. char *p;
  771. if(n->s[0] == '#'){
  772. p = strchr(n->s, '/');
  773. if(p == nil)
  774. return;
  775. cleanname(p);
  776. /*
  777. * The correct name is #i rather than #i/,
  778. * but the correct name of #/ is #/.
  779. */
  780. if(strcmp(p, "/")==0 && n->s[1] != '/')
  781. *p = '\0';
  782. }else
  783. cleanname(n->s);
  784. n->len = strlen(n->s);
  785. }
  786. static void
  787. growparse(Elemlist *e)
  788. {
  789. char **new;
  790. int *inew;
  791. enum { Delta = 8 };
  792. if(e->nelems % Delta == 0){
  793. new = smalloc((e->nelems+Delta) * sizeof(char*));
  794. memmove(new, e->elems, e->nelems*sizeof(char*));
  795. free(e->elems);
  796. e->elems = new;
  797. inew = smalloc((e->nelems+Delta+1) * sizeof(int));
  798. memmove(inew, e->off, e->nelems*sizeof(int));
  799. free(e->off);
  800. e->off = inew;
  801. }
  802. }
  803. /*
  804. * The name is known to be valid.
  805. * Copy the name so slashes can be overwritten.
  806. * An empty string will set nelem=0.
  807. * A path ending in / or /. or /.//./ etc. will have
  808. * e.mustbedir = 1, so that we correctly
  809. * reject, e.g., "/adm/users/." when /adm/users is a file
  810. * rather than a directory.
  811. */
  812. static void
  813. parsename(char *name, Elemlist *e)
  814. {
  815. char *slash;
  816. kstrdup(&e->name, name);
  817. name = e->name;
  818. e->nelems = 0;
  819. e->elems = nil;
  820. e->off = smalloc(sizeof(int));
  821. e->off[0] = skipslash(name) - name;
  822. for(;;){
  823. name = skipslash(name);
  824. if(*name=='\0'){
  825. e->mustbedir = 1;
  826. break;
  827. }
  828. growparse(e);
  829. e->elems[e->nelems++] = name;
  830. slash = utfrune(name, '/');
  831. if(slash == nil){
  832. e->off[e->nelems] = name+strlen(name) - e->name;
  833. e->mustbedir = 0;
  834. break;
  835. }
  836. e->off[e->nelems] = slash - e->name;
  837. *slash++ = '\0';
  838. name = slash;
  839. }
  840. }
  841. static void*
  842. kmemrchr(void *va, int c, long n)
  843. {
  844. uchar *a, *e;
  845. a = va;
  846. for(e=a+n-1; e>a; e--)
  847. if(*e == c)
  848. return e;
  849. return nil;
  850. }
  851. static void
  852. saveregisters(void)
  853. {
  854. }
  855. /*
  856. * Turn a name into a channel.
  857. * &name[0] is known to be a valid address. It may be a kernel address.
  858. *
  859. * Opening with amode Aopen, Acreate, or Aremove guarantees
  860. * that the result will be the only reference to that particular fid.
  861. * This is necessary since we might pass the result to
  862. * devtab[]->remove().
  863. *
  864. * Opening Atodir, Amount, or Aaccess does not guarantee this.
  865. *
  866. * Opening Aaccess can, under certain conditions, return a
  867. * correct Chan* but with an incorrect Cname attached.
  868. * Since the functions that open Aaccess (sysstat, syswstat, sys_stat)
  869. * do not use the Cname*, this avoids an unnecessary clone.
  870. */
  871. Chan*
  872. namec(char *aname, int amode, int omode, ulong perm)
  873. {
  874. int n, prefix, len, t, nomount, npath;
  875. Chan *c, *cnew;
  876. Cname *cname;
  877. Elemlist e;
  878. Rune r;
  879. Mhead *m;
  880. char *createerr, tmperrbuf[ERRMAX];
  881. char *name;
  882. name = aname;
  883. if(name[0] == '\0')
  884. error("empty file name");
  885. validname(name, 1);
  886. /*
  887. * Find the starting off point (the current slash, the root of
  888. * a device tree, or the current dot) as well as the name to
  889. * evaluate starting there.
  890. */
  891. nomount = 0;
  892. switch(name[0]){
  893. case '/':
  894. c = up->env->pgrp->slash;
  895. incref(&c->r);
  896. break;
  897. case '#':
  898. nomount = 1;
  899. up->genbuf[0] = '\0';
  900. n = 0;
  901. while(*name!='\0' && (*name != '/' || n < 2)){
  902. if(n >= sizeof(up->genbuf)-1)
  903. error(Efilename);
  904. up->genbuf[n++] = *name++;
  905. }
  906. up->genbuf[n] = '\0';
  907. n = chartorune(&r, up->genbuf+1)+1;
  908. if(r == 'M')
  909. error(Enoattach);
  910. /*
  911. * the nodevs exceptions are
  912. * | it only gives access to pipes you create
  913. * e this process's environment
  914. * s private file2chan creation space
  915. * D private secure sockets name space
  916. * a private TLS name space
  917. */
  918. if(up->env->pgrp->nodevs &&
  919. (utfrune("|esDa", r) == nil || r == 's' && up->genbuf[n]!='\0'))
  920. error(Enoattach);
  921. t = devno(r, 1);
  922. if(t == -1)
  923. error(Ebadsharp);
  924. c = devtab[t]->attach(up->genbuf+n);
  925. break;
  926. default:
  927. c = up->env->pgrp->dot;
  928. incref(&c->r);
  929. break;
  930. }
  931. prefix = name - aname;
  932. e.name = nil;
  933. e.elems = nil;
  934. e.off = nil;
  935. e.nelems = 0;
  936. if(waserror()){
  937. cclose(c);
  938. free(e.name);
  939. free(e.elems);
  940. free(e.off);
  941. //dumpmount();
  942. nexterror();
  943. }
  944. /*
  945. * Build a list of elements in the path.
  946. */
  947. parsename(name, &e);
  948. /*
  949. * On create, ....
  950. */
  951. if(amode == Acreate){
  952. /* perm must have DMDIR if last element is / or /. */
  953. if(e.mustbedir && !(perm&DMDIR)){
  954. npath = e.nelems;
  955. strcpy(tmperrbuf, "create without DMDIR");
  956. goto NameError;
  957. }
  958. /* don't try to walk the last path element just yet. */
  959. if(e.nelems == 0)
  960. error(Eexist);
  961. e.nelems--;
  962. }
  963. if(walk(&c, e.elems, e.nelems, nomount, &npath) < 0){
  964. if(npath < 0 || npath > e.nelems){
  965. print("namec %s walk error npath=%d\n", aname, npath);
  966. nexterror();
  967. }
  968. strcpy(tmperrbuf, up->env->errstr);
  969. NameError:
  970. len = prefix+e.off[npath];
  971. if(len < ERRMAX/3 || (name=kmemrchr(aname, '/', len))==nil || name==aname)
  972. snprint(up->genbuf, sizeof up->genbuf, "%.*s", len, aname);
  973. else
  974. snprint(up->genbuf, sizeof up->genbuf, "...%.*s", (int)(len-(name-aname)), name);
  975. snprint(up->env->errstr, ERRMAX, "%#q %s", up->genbuf, tmperrbuf);
  976. nexterror();
  977. }
  978. if(e.mustbedir && !(c->qid.type&QTDIR)){
  979. npath = e.nelems;
  980. strcpy(tmperrbuf, "not a directory");
  981. goto NameError;
  982. }
  983. if(amode == Aopen && (omode&3) == OEXEC && (c->qid.type&QTDIR)){
  984. npath = e.nelems;
  985. error("cannot exec directory");
  986. }
  987. switch(amode){
  988. case Aaccess:
  989. if(!nomount)
  990. domount(&c, nil);
  991. break;
  992. case Abind:
  993. m = nil;
  994. if(!nomount)
  995. domount(&c, &m);
  996. if(c->umh != nil)
  997. putmhead(c->umh);
  998. c->umh = m;
  999. break;
  1000. case Aremove:
  1001. case Aopen:
  1002. Open:
  1003. /* save the name; domount might change c */
  1004. cname = c->name;
  1005. incref(&cname->r);
  1006. m = nil;
  1007. if(!nomount)
  1008. domount(&c, &m);
  1009. /* our own copy to open or remove */
  1010. c = cunique(c);
  1011. /* now it's our copy anyway, we can put the name back */
  1012. cnameclose(c->name);
  1013. c->name = cname;
  1014. switch(amode){
  1015. case Aremove:
  1016. putmhead(m);
  1017. break;
  1018. case Aopen:
  1019. case Acreate:
  1020. if(c->umh != nil){
  1021. print("cunique umh\n");
  1022. putmhead(c->umh);
  1023. c->umh = nil;
  1024. }
  1025. /* only save the mount head if it's a multiple element union */
  1026. if(m && m->mount && m->mount->next)
  1027. c->umh = m;
  1028. else
  1029. putmhead(m);
  1030. /* save registers else error() in open has wrong value of c saved */
  1031. saveregisters();
  1032. if(omode == OEXEC)
  1033. c->flag &= ~CCACHE;
  1034. c = devtab[c->type]->open(c, omode&~OCEXEC);
  1035. if(omode & OCEXEC)
  1036. c->flag |= CCEXEC;
  1037. if(omode & ORCLOSE)
  1038. c->flag |= CRCLOSE;
  1039. break;
  1040. }
  1041. break;
  1042. case Atodir:
  1043. /*
  1044. * Directories (e.g. for cd) are left before the mount point,
  1045. * so one may mount on / or . and see the effect.
  1046. */
  1047. if(!(c->qid.type & QTDIR))
  1048. error(Enotdir);
  1049. break;
  1050. case Amount:
  1051. /*
  1052. * When mounting on an already mounted upon directory,
  1053. * one wants subsequent mounts to be attached to the
  1054. * original directory, not the replacement. Don't domount.
  1055. */
  1056. break;
  1057. case Acreate:
  1058. /*
  1059. * We've already walked all but the last element.
  1060. * If the last exists, try to open it OTRUNC.
  1061. * If omode&OEXCL is set, just give up.
  1062. */
  1063. e.nelems++;
  1064. if(walk(&c, e.elems+e.nelems-1, 1, nomount, nil) == 0){
  1065. if(omode&OEXCL)
  1066. error(Eexist);
  1067. omode |= OTRUNC;
  1068. goto Open;
  1069. }
  1070. /*
  1071. * The semantics of the create(2) system call are that if the
  1072. * file exists and can be written, it is to be opened with truncation.
  1073. * On the other hand, the create(5) message fails if the file exists.
  1074. * If we get two create(2) calls happening simultaneously,
  1075. * they might both get here and send create(5) messages, but only
  1076. * one of the messages will succeed. To provide the expected create(2)
  1077. * semantics, the call with the failed message needs to try the above
  1078. * walk again, opening for truncation. This correctly solves the
  1079. * create/create race, in the sense that any observable outcome can
  1080. * be explained as one happening before the other.
  1081. * The create/create race is quite common. For example, it happens
  1082. * when two rc subshells simultaneously update the same
  1083. * environment variable.
  1084. *
  1085. * The implementation still admits a create/create/remove race:
  1086. * (A) walk to file, fails
  1087. * (B) walk to file, fails
  1088. * (A) create file, succeeds, returns
  1089. * (B) create file, fails
  1090. * (A) remove file, succeeds, returns
  1091. * (B) walk to file, return failure.
  1092. *
  1093. * This is hardly as common as the create/create race, and is really
  1094. * not too much worse than what might happen if (B) got a hold of a
  1095. * file descriptor and then the file was removed -- either way (B) can't do
  1096. * anything with the result of the create call. So we don't care about this race.
  1097. *
  1098. * Applications that care about more fine-grained decision of the races
  1099. * can use the OEXCL flag to get at the underlying create(5) semantics;
  1100. * by default we provide the common case.
  1101. *
  1102. * We need to stay behind the mount point in case we
  1103. * need to do the first walk again (should the create fail).
  1104. *
  1105. * We also need to cross the mount point and find the directory
  1106. * in the union in which we should be creating.
  1107. *
  1108. * The channel staying behind is c, the one moving forward is cnew.
  1109. */
  1110. m = nil;
  1111. cnew = nil; /* is this assignment necessary? */
  1112. if(!waserror()){ /* try create */
  1113. if(!nomount && findmount(&cnew, &m, c->type, c->dev, c->qid))
  1114. cnew = createdir(cnew, m);
  1115. else{
  1116. cnew = c;
  1117. incref(&cnew->r);
  1118. }
  1119. /*
  1120. * We need our own copy of the Chan because we're
  1121. * about to send a create, which will move it. Once we have
  1122. * our own copy, we can fix the name, which might be wrong
  1123. * if findmount gave us a new Chan.
  1124. */
  1125. cnew = cunique(cnew);
  1126. cnameclose(cnew->name);
  1127. cnew->name = c->name;
  1128. incref(&cnew->name->r);
  1129. devtab[cnew->type]->create(cnew, e.elems[e.nelems-1], omode&~(OEXCL|OCEXEC), perm);
  1130. poperror();
  1131. if(omode & OCEXEC)
  1132. cnew->flag |= CCEXEC;
  1133. if(omode & ORCLOSE)
  1134. cnew->flag |= CRCLOSE;
  1135. if(m)
  1136. putmhead(m);
  1137. cclose(c);
  1138. c = cnew;
  1139. c->name = addelem(c->name, e.elems[e.nelems-1]);
  1140. break;
  1141. }else{ /* create failed */
  1142. cclose(cnew);
  1143. if(m)
  1144. putmhead(m);
  1145. if(omode & OEXCL)
  1146. nexterror();
  1147. /* save error */
  1148. createerr = up->env->errstr;
  1149. up->env->errstr = tmperrbuf;
  1150. /* note: we depend that walk does not error */
  1151. if(walk(&c, e.elems+e.nelems-1, 1, nomount, nil) < 0){
  1152. up->env->errstr = createerr;
  1153. error(createerr); /* report true error */
  1154. }
  1155. up->env->errstr = createerr;
  1156. omode |= OTRUNC;
  1157. goto Open;
  1158. }
  1159. default:
  1160. panic("unknown namec access %d\n", amode);
  1161. }
  1162. poperror();
  1163. /* place final element in genbuf for e.g. exec */
  1164. if(e.nelems > 0)
  1165. kstrcpy(up->genbuf, e.elems[e.nelems-1], sizeof up->genbuf);
  1166. else
  1167. kstrcpy(up->genbuf, ".", sizeof up->genbuf);
  1168. free(e.name);
  1169. free(e.elems);
  1170. free(e.off);
  1171. return c;
  1172. }
  1173. /*
  1174. * name is valid. skip leading / and ./ as much as possible
  1175. */
  1176. char*
  1177. skipslash(char *name)
  1178. {
  1179. while(name[0]=='/' || (name[0]=='.' && (name[1]==0 || name[1]=='/')))
  1180. name++;
  1181. return name;
  1182. }
  1183. /*
  1184. * Check that the name
  1185. * a) is in valid memory.
  1186. * b) is shorter than 2^16 bytes, so it can fit in a 9P string field.
  1187. * c) contains no frogs.
  1188. * The first byte is known to be addressible by the requester, so the
  1189. * routine works for kernel and user memory both.
  1190. * The parameter slashok flags whether a slash character is an error
  1191. * or a valid character.
  1192. */
  1193. void
  1194. validname(char *aname, int slashok)
  1195. {
  1196. char *ename, *name;
  1197. int c;
  1198. Rune r;
  1199. name = aname;
  1200. ename = memchr(name, 0, (1<<16));
  1201. if(ename==nil || ename-name>=(1<<16))
  1202. error("name too long");
  1203. while(*name){
  1204. /* all characters above '~' are ok */
  1205. c = *(uchar*)name;
  1206. if(c >= Runeself)
  1207. name += chartorune(&r, name);
  1208. else{
  1209. if(isfrog[c])
  1210. if(!slashok || c!='/'){
  1211. snprint(up->genbuf, sizeof(up->genbuf), "%s: %q", Ebadchar, aname);
  1212. error(up->genbuf);
  1213. }
  1214. name++;
  1215. }
  1216. }
  1217. }
  1218. void
  1219. isdir(Chan *c)
  1220. {
  1221. if(c->qid.type & QTDIR)
  1222. return;
  1223. error(Enotdir);
  1224. }
  1225. /*
  1226. * This is necessary because there are many
  1227. * pointers to the top of a given mount list:
  1228. *
  1229. * - the mhead in the namespace hash table
  1230. * - the mhead in chans returned from findmount:
  1231. * used in namec and then by unionread.
  1232. * - the mhead in chans returned from createdir:
  1233. * used in the open/create race protect, which is gone.
  1234. *
  1235. * The RWlock in the Mhead protects the mount list it contains.
  1236. * The mount list is deleted when we cunmount.
  1237. * The RWlock ensures that nothing is using the mount list at that time.
  1238. *
  1239. * It is okay to replace c->mh with whatever you want as
  1240. * long as you are sure you have a unique reference to it.
  1241. *
  1242. * This comment might belong somewhere else.
  1243. */
  1244. void
  1245. putmhead(Mhead *m)
  1246. {
  1247. if(m && decref(&m->r) == 0){
  1248. m->mount = (Mount*)0xCafeBeef;
  1249. free(m);
  1250. }
  1251. }