chan.c 34 KB

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