chan.c 29 KB

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