chan.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  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 %#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("decref pc=%#p", 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 %#p\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 %#p", 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 %#p", 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 %#p\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 %#p\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, didmount, 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. * Each time through the loop:
  853. *
  854. * If didmount==0, c is on the undomount side of the mount point.
  855. * If didmount==1, c is on the domount side of the mount point.
  856. * Either way, c's full path is path.
  857. */
  858. didmount = 0;
  859. for(nhave=0; nhave<nnames; nhave+=n){
  860. if((c->qid.type&QTDIR)==0){
  861. if(nerror)
  862. *nerror = nhave;
  863. pathclose(path);
  864. cclose(c);
  865. strcpy(up->errstr, Enotdir);
  866. if(mh != nil)
  867. putmhead(mh);
  868. return -1;
  869. }
  870. ntry = nnames - nhave;
  871. if(ntry > MAXWELEM)
  872. ntry = MAXWELEM;
  873. dotdot = 0;
  874. for(i=0; i<ntry; i++){
  875. if(isdotdot(names[nhave+i])){
  876. if(i==0){
  877. dotdot = 1;
  878. ntry = 1;
  879. }else
  880. ntry = i;
  881. break;
  882. }
  883. }
  884. if(!dotdot && !nomount && !didmount)
  885. domount(&c, &mh, &path);
  886. type = c->type;
  887. dev = c->dev;
  888. if((wq = ewalk(c, nil, names+nhave, ntry)) == nil){
  889. /* try a union mount, if any */
  890. if(mh && !nomount){
  891. /*
  892. * mh->mount->to == c, so start at mh->mount->next
  893. */
  894. rlock(&mh->lock);
  895. for(f = mh->mount->next; f; f = f->next)
  896. if((wq = ewalk(f->to, nil, names+nhave, ntry)) != nil)
  897. break;
  898. runlock(&mh->lock);
  899. if(f != nil){
  900. type = f->to->type;
  901. dev = f->to->dev;
  902. }
  903. }
  904. if(wq == nil){
  905. cclose(c);
  906. pathclose(path);
  907. if(nerror)
  908. *nerror = nhave+1;
  909. if(mh != nil)
  910. putmhead(mh);
  911. return -1;
  912. }
  913. }
  914. didmount = 0;
  915. if(dotdot){
  916. assert(wq->nqid == 1);
  917. assert(wq->clone != nil);
  918. path = addelem(path, "..", nil);
  919. nc = undomount(wq->clone, path);
  920. nmh = nil;
  921. n = 1;
  922. }else{
  923. nc = nil;
  924. nmh = nil;
  925. if(!nomount){
  926. for(i=0; i<wq->nqid && i<ntry-1; i++){
  927. if(findmount(&nc, &nmh, type, dev, wq->qid[i])){
  928. didmount = 1;
  929. break;
  930. }
  931. }
  932. }
  933. if(nc == nil){ /* no mount points along path */
  934. if(wq->clone == nil){
  935. cclose(c);
  936. pathclose(path);
  937. if(wq->nqid==0 || (wq->qid[wq->nqid-1].type&QTDIR)){
  938. if(nerror)
  939. *nerror = nhave+wq->nqid+1;
  940. strcpy(up->errstr, Edoesnotexist);
  941. }else{
  942. if(nerror)
  943. *nerror = nhave+wq->nqid;
  944. strcpy(up->errstr, Enotdir);
  945. }
  946. free(wq);
  947. if(mh != nil)
  948. putmhead(mh);
  949. return -1;
  950. }
  951. n = wq->nqid;
  952. nc = wq->clone;
  953. }else{ /* stopped early, at a mount point */
  954. didmount = 1;
  955. if(wq->clone != nil){
  956. cclose(wq->clone);
  957. wq->clone = nil;
  958. }
  959. n = i+1;
  960. }
  961. for(i=0; i<n; i++){
  962. mtpt = nil;
  963. if(i==n-1 && nmh)
  964. mtpt = nmh->from;
  965. path = addelem(path, names[nhave+i], mtpt);
  966. }
  967. }
  968. cclose(c);
  969. c = nc;
  970. putmhead(mh);
  971. mh = nmh;
  972. free(wq);
  973. }
  974. putmhead(mh);
  975. c = cunique(c);
  976. if(c->umh != nil){ //BUG
  977. print("walk umh\n");
  978. putmhead(c->umh);
  979. c->umh = nil;
  980. }
  981. pathclose(c->path);
  982. c->path = path;
  983. cclose(*cp);
  984. *cp = c;
  985. if(nerror)
  986. *nerror = nhave;
  987. return 0;
  988. }
  989. /*
  990. * c is a mounted non-creatable directory. find a creatable one.
  991. */
  992. Chan*
  993. createdir(Chan *c, Mhead *m)
  994. {
  995. Chan *nc;
  996. Mount *f;
  997. rlock(&m->lock);
  998. if(waserror()){
  999. runlock(&m->lock);
  1000. nexterror();
  1001. }
  1002. for(f = m->mount; f; f = f->next){
  1003. if(f->mflag&MCREATE){
  1004. nc = cclone(f->to);
  1005. runlock(&m->lock);
  1006. poperror();
  1007. cclose(c);
  1008. return nc;
  1009. }
  1010. }
  1011. error(Enocreate);
  1012. return 0;
  1013. }
  1014. void
  1015. saveregisters(void)
  1016. {
  1017. }
  1018. static void
  1019. growparse(Elemlist *e)
  1020. {
  1021. char **new;
  1022. int *inew;
  1023. enum { Delta = 8 };
  1024. if(e->nelems % Delta == 0){
  1025. new = smalloc((e->nelems+Delta) * sizeof(char*));
  1026. memmove(new, e->elems, e->nelems*sizeof(char*));
  1027. free(e->elems);
  1028. e->elems = new;
  1029. inew = smalloc((e->nelems+Delta+1) * sizeof(int));
  1030. memmove(inew, e->off, (e->nelems+1)*sizeof(int));
  1031. free(e->off);
  1032. e->off = inew;
  1033. }
  1034. }
  1035. /*
  1036. * The name is known to be valid.
  1037. * Copy the name so slashes can be overwritten.
  1038. * An empty string will set nelem=0.
  1039. * A path ending in / or /. or /.//./ etc. will have
  1040. * e.mustbedir = 1, so that we correctly
  1041. * reject, e.g., "/adm/users/." when /adm/users is a file
  1042. * rather than a directory.
  1043. */
  1044. static void
  1045. parsename(char *aname, Elemlist *e)
  1046. {
  1047. char *name, *slash;
  1048. kstrdup(&e->name, aname);
  1049. name = e->name;
  1050. e->nelems = 0;
  1051. e->elems = nil;
  1052. e->off = smalloc(sizeof(int));
  1053. e->off[0] = skipslash(name) - name;
  1054. for(;;){
  1055. name = skipslash(name);
  1056. if(*name == '\0'){
  1057. e->off[e->nelems] = name+strlen(name) - e->name;
  1058. e->mustbedir = 1;
  1059. break;
  1060. }
  1061. growparse(e);
  1062. e->elems[e->nelems++] = name;
  1063. slash = utfrune(name, '/');
  1064. if(slash == nil){
  1065. e->off[e->nelems] = name+strlen(name) - e->name;
  1066. e->mustbedir = 0;
  1067. break;
  1068. }
  1069. e->off[e->nelems] = slash - e->name;
  1070. *slash++ = '\0';
  1071. name = slash;
  1072. }
  1073. if(0 && chandebug){
  1074. int i;
  1075. print("parsename %s:", e->name);
  1076. for(i=0; i<=e->nelems; i++)
  1077. print(" %d", e->off[i]);
  1078. print("\n");
  1079. }
  1080. }
  1081. void*
  1082. memrchr(void *va, int c, long n)
  1083. {
  1084. uchar *a, *e;
  1085. a = va;
  1086. for(e=a+n-1; e>a; e--)
  1087. if(*e == c)
  1088. return e;
  1089. return nil;
  1090. }
  1091. void
  1092. namelenerror(char *aname, int len, char *err)
  1093. {
  1094. char *ename, *name, *next;
  1095. int i, errlen;
  1096. /*
  1097. * If the name is short enough, just use the whole thing.
  1098. */
  1099. errlen = strlen(err);
  1100. if(len < ERRMAX/3 || len+errlen < 2*ERRMAX/3)
  1101. snprint(up->genbuf, sizeof up->genbuf, "%.*s",
  1102. utfnlen(aname, len), aname);
  1103. else{
  1104. /*
  1105. * Print a suffix of the name, but try to get a little info.
  1106. */
  1107. ename = aname+len;
  1108. next = ename;
  1109. do{
  1110. name = next;
  1111. next = memrchr(aname, '/', name-aname);
  1112. if(next == nil)
  1113. next = aname;
  1114. len = ename-next;
  1115. }while(len < ERRMAX/3 || len + errlen < 2*ERRMAX/3);
  1116. /*
  1117. * If the name is ridiculously long, chop it.
  1118. */
  1119. if(name == ename){
  1120. name = ename-ERRMAX/4;
  1121. if(name <= aname)
  1122. panic("bad math in namelenerror");
  1123. /* walk out of current UTF sequence */
  1124. for(i=0; (*name&0xC0)==0x80 && i<3; i++)
  1125. name++;
  1126. }
  1127. snprint(up->genbuf, sizeof up->genbuf, "...%.*s",
  1128. utfnlen(name, ename-name), name);
  1129. }
  1130. snprint(up->errstr, ERRMAX, "%#q %s", up->genbuf, err);
  1131. nexterror();
  1132. }
  1133. void
  1134. nameerror(char *name, char *err)
  1135. {
  1136. namelenerror(name, strlen(name), err);
  1137. }
  1138. /*
  1139. * Turn a name into a channel.
  1140. * &name[0] is known to be a valid address. It may be a kernel address.
  1141. *
  1142. * Opening with amode Aopen, Acreate, Aremove, or Aaccess guarantees
  1143. * that the result will be the only reference to that particular fid.
  1144. * This is necessary since we might pass the result to
  1145. * devtab[]->remove().
  1146. *
  1147. * Opening Atodir or Amount does not guarantee this.
  1148. *
  1149. * Under certain circumstances, opening Aaccess will cause
  1150. * an unnecessary clone in order to get a cunique Chan so it
  1151. * can attach the correct name. Sysstat and sys_stat need the
  1152. * correct name so they can rewrite the stat info.
  1153. */
  1154. Chan*
  1155. namec(char *aname, int amode, int omode, ulong perm)
  1156. {
  1157. int len, n, t, nomount;
  1158. Chan *c, *cnew;
  1159. Path *path;
  1160. Elemlist e;
  1161. Rune r;
  1162. Mhead *m;
  1163. char *createerr, tmperrbuf[ERRMAX];
  1164. char *name;
  1165. if(aname[0] == '\0')
  1166. error("empty file name");
  1167. aname = validnamedup(aname, 1);
  1168. if(waserror()){
  1169. free(aname);
  1170. nexterror();
  1171. }
  1172. DBG("namec %s %d %d\n", aname, amode, omode);
  1173. name = aname;
  1174. /*
  1175. * Find the starting off point (the current slash, the root of
  1176. * a device tree, or the current dot) as well as the name to
  1177. * evaluate starting there.
  1178. */
  1179. nomount = 0;
  1180. switch(name[0]){
  1181. case '/':
  1182. c = up->slash;
  1183. incref(c);
  1184. break;
  1185. case '#':
  1186. nomount = 1;
  1187. up->genbuf[0] = '\0';
  1188. n = 0;
  1189. while(*name != '\0' && (*name != '/' || n < 2)){
  1190. if(n >= sizeof(up->genbuf)-1)
  1191. error(Efilename);
  1192. up->genbuf[n++] = *name++;
  1193. }
  1194. up->genbuf[n] = '\0';
  1195. /*
  1196. * noattach is sandboxing.
  1197. *
  1198. * the OK exceptions are:
  1199. * | it only gives access to pipes you create
  1200. * d this process's file descriptors
  1201. * e this process's environment
  1202. * the iffy exceptions are:
  1203. * c time and pid, but also cons and consctl
  1204. * p control of your own processes (and unfortunately
  1205. * any others left unprotected)
  1206. */
  1207. n = chartorune(&r, up->genbuf+1)+1;
  1208. /* actually / is caught by parsing earlier */
  1209. if(utfrune("M", r))
  1210. error(Enoattach);
  1211. if(up->pgrp->noattach && utfrune("|decp", r)==nil)
  1212. error(Enoattach);
  1213. t = devno(r, 1);
  1214. if(t == -1)
  1215. error(Ebadsharp);
  1216. c = devtab[t]->attach(up->genbuf+n);
  1217. break;
  1218. default:
  1219. c = up->dot;
  1220. incref(c);
  1221. break;
  1222. }
  1223. e.aname = aname;
  1224. e.prefix = name - aname;
  1225. e.name = nil;
  1226. e.elems = nil;
  1227. e.off = nil;
  1228. e.nelems = 0;
  1229. e.nerror = 0;
  1230. if(waserror()){
  1231. cclose(c);
  1232. free(e.name);
  1233. free(e.elems);
  1234. /*
  1235. * Prepare nice error, showing first e.nerror elements of name.
  1236. */
  1237. if(e.nerror == 0)
  1238. nexterror();
  1239. strcpy(tmperrbuf, up->errstr);
  1240. if(e.off[e.nerror]==0)
  1241. print("nerror=%d but off=%d\n",
  1242. e.nerror, e.off[e.nerror]);
  1243. if(0 && chandebug)
  1244. 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]);
  1245. len = e.prefix+e.off[e.nerror];
  1246. free(e.off);
  1247. namelenerror(aname, len, tmperrbuf);
  1248. }
  1249. /*
  1250. * Build a list of elements in the name.
  1251. */
  1252. parsename(name, &e);
  1253. /*
  1254. * On create, ....
  1255. */
  1256. if(amode == Acreate){
  1257. /* perm must have DMDIR if last element is / or /. */
  1258. if(e.mustbedir && !(perm&DMDIR)){
  1259. e.nerror = e.nelems;
  1260. error("create without DMDIR");
  1261. }
  1262. /* don't try to walk the last path element just yet. */
  1263. if(e.nelems == 0)
  1264. error(Eexist);
  1265. e.nelems--;
  1266. }
  1267. if(walk(&c, e.elems, e.nelems, nomount, &e.nerror) < 0){
  1268. if(e.nerror < 0 || e.nerror > e.nelems){
  1269. print("namec %s walk error nerror=%d\n", aname, e.nerror);
  1270. e.nerror = 0;
  1271. }
  1272. nexterror();
  1273. }
  1274. if(e.mustbedir && !(c->qid.type&QTDIR))
  1275. error("not a directory");
  1276. if(amode == Aopen && (omode&3) == OEXEC && (c->qid.type&QTDIR))
  1277. error("cannot exec directory");
  1278. switch(amode){
  1279. case Abind:
  1280. /* no need to maintain path - cannot dotdot an Abind */
  1281. m = nil;
  1282. if(!nomount)
  1283. domount(&c, &m, nil);
  1284. if(c->umh != nil)
  1285. putmhead(c->umh);
  1286. c->umh = m;
  1287. break;
  1288. case Aaccess:
  1289. case Aremove:
  1290. case Aopen:
  1291. Open:
  1292. /* save&update the name; domount might change c */
  1293. path = c->path;
  1294. incref(path);
  1295. m = nil;
  1296. if(!nomount)
  1297. domount(&c, &m, &path);
  1298. /* our own copy to open or remove */
  1299. c = cunique(c);
  1300. /* now it's our copy anyway, we can put the name back */
  1301. pathclose(c->path);
  1302. c->path = path;
  1303. /* record whether c is on a mount point */
  1304. c->ismtpt = m!=nil;
  1305. switch(amode){
  1306. case Aaccess:
  1307. case Aremove:
  1308. putmhead(m);
  1309. break;
  1310. case Aopen:
  1311. case Acreate:
  1312. if(c->umh != nil){
  1313. print("cunique umh Open\n");
  1314. putmhead(c->umh);
  1315. c->umh = nil;
  1316. }
  1317. /* only save the mount head if it's a multiple element union */
  1318. if(m && m->mount && m->mount->next)
  1319. c->umh = m;
  1320. else
  1321. putmhead(m);
  1322. /* save registers else error() in open has wrong value of c saved */
  1323. saveregisters();
  1324. if(omode == OEXEC)
  1325. c->flag &= ~CCACHE;
  1326. c = devtab[c->type]->open(c, omode&~OCEXEC);
  1327. if(omode & OCEXEC)
  1328. c->flag |= CCEXEC;
  1329. if(omode & ORCLOSE)
  1330. c->flag |= CRCLOSE;
  1331. break;
  1332. }
  1333. break;
  1334. case Atodir:
  1335. /*
  1336. * Directories (e.g. for cd) are left before the mount point,
  1337. * so one may mount on / or . and see the effect.
  1338. */
  1339. if(!(c->qid.type & QTDIR))
  1340. error(Enotdir);
  1341. break;
  1342. case Amount:
  1343. /*
  1344. * When mounting on an already mounted upon directory,
  1345. * one wants subsequent mounts to be attached to the
  1346. * original directory, not the replacement. Don't domount.
  1347. */
  1348. break;
  1349. case Acreate:
  1350. /*
  1351. * We've already walked all but the last element.
  1352. * If the last exists, try to open it OTRUNC.
  1353. * If omode&OEXCL is set, just give up.
  1354. */
  1355. e.nelems++;
  1356. e.nerror++;
  1357. if(walk(&c, e.elems+e.nelems-1, 1, nomount, nil) == 0){
  1358. if(omode&OEXCL)
  1359. error(Eexist);
  1360. omode |= OTRUNC;
  1361. goto Open;
  1362. }
  1363. /*
  1364. * The semantics of the create(2) system call are that if the
  1365. * file exists and can be written, it is to be opened with truncation.
  1366. * On the other hand, the create(5) message fails if the file exists.
  1367. * If we get two create(2) calls happening simultaneously,
  1368. * they might both get here and send create(5) messages, but only
  1369. * one of the messages will succeed. To provide the expected create(2)
  1370. * semantics, the call with the failed message needs to try the above
  1371. * walk again, opening for truncation. This correctly solves the
  1372. * create/create race, in the sense that any observable outcome can
  1373. * be explained as one happening before the other.
  1374. * The create/create race is quite common. For example, it happens
  1375. * when two rc subshells simultaneously update the same
  1376. * environment variable.
  1377. *
  1378. * The implementation still admits a create/create/remove race:
  1379. * (A) walk to file, fails
  1380. * (B) walk to file, fails
  1381. * (A) create file, succeeds, returns
  1382. * (B) create file, fails
  1383. * (A) remove file, succeeds, returns
  1384. * (B) walk to file, return failure.
  1385. *
  1386. * This is hardly as common as the create/create race, and is really
  1387. * not too much worse than what might happen if (B) got a hold of a
  1388. * file descriptor and then the file was removed -- either way (B) can't do
  1389. * anything with the result of the create call. So we don't care about this race.
  1390. *
  1391. * Applications that care about more fine-grained decision of the races
  1392. * can use the OEXCL flag to get at the underlying create(5) semantics;
  1393. * by default we provide the common case.
  1394. *
  1395. * We need to stay behind the mount point in case we
  1396. * need to do the first walk again (should the create fail).
  1397. *
  1398. * We also need to cross the mount point and find the directory
  1399. * in the union in which we should be creating.
  1400. *
  1401. * The channel staying behind is c, the one moving forward is cnew.
  1402. */
  1403. m = nil;
  1404. cnew = nil; /* is this assignment necessary? */
  1405. if(!waserror()){ /* try create */
  1406. if(!nomount && findmount(&cnew, &m, c->type, c->dev, c->qid))
  1407. cnew = createdir(cnew, m);
  1408. else{
  1409. cnew = c;
  1410. incref(cnew);
  1411. }
  1412. /*
  1413. * We need our own copy of the Chan because we're
  1414. * about to send a create, which will move it. Once we have
  1415. * our own copy, we can fix the name, which might be wrong
  1416. * if findmount gave us a new Chan.
  1417. */
  1418. cnew = cunique(cnew);
  1419. pathclose(cnew->path);
  1420. cnew->path = c->path;
  1421. incref(cnew->path);
  1422. devtab[cnew->type]->create(cnew, e.elems[e.nelems-1], omode&~(OEXCL|OCEXEC), perm);
  1423. poperror();
  1424. if(omode & OCEXEC)
  1425. cnew->flag |= CCEXEC;
  1426. if(omode & ORCLOSE)
  1427. cnew->flag |= CRCLOSE;
  1428. if(m)
  1429. putmhead(m);
  1430. cclose(c);
  1431. c = cnew;
  1432. c->path = addelem(c->path, e.elems[e.nelems-1], nil);
  1433. break;
  1434. }
  1435. /* create failed */
  1436. cclose(cnew);
  1437. if(m)
  1438. putmhead(m);
  1439. if(omode & OEXCL)
  1440. nexterror();
  1441. /* save error */
  1442. createerr = up->errstr;
  1443. up->errstr = tmperrbuf;
  1444. /* note: we depend that walk does not error */
  1445. if(walk(&c, e.elems+e.nelems-1, 1, nomount, nil) < 0){
  1446. up->errstr = createerr;
  1447. error(createerr); /* report true error */
  1448. }
  1449. up->errstr = createerr;
  1450. omode |= OTRUNC;
  1451. goto Open;
  1452. default:
  1453. panic("unknown namec access %d\n", amode);
  1454. }
  1455. /* place final element in genbuf for e.g. exec */
  1456. if(e.nelems > 0)
  1457. kstrcpy(up->genbuf, e.elems[e.nelems-1], sizeof up->genbuf);
  1458. else
  1459. kstrcpy(up->genbuf, ".", sizeof up->genbuf);
  1460. free(e.name);
  1461. free(e.elems);
  1462. free(e.off);
  1463. poperror(); /* e c */
  1464. free(aname);
  1465. poperror(); /* aname */
  1466. return c;
  1467. }
  1468. /*
  1469. * name is valid. skip leading / and ./ as much as possible
  1470. */
  1471. char*
  1472. skipslash(char *name)
  1473. {
  1474. while(name[0]=='/' || (name[0]=='.' && (name[1]==0 || name[1]=='/')))
  1475. name++;
  1476. return name;
  1477. }
  1478. char isfrog[256]={
  1479. /*NUL*/ 1, 1, 1, 1, 1, 1, 1, 1,
  1480. /*BKS*/ 1, 1, 1, 1, 1, 1, 1, 1,
  1481. /*DLE*/ 1, 1, 1, 1, 1, 1, 1, 1,
  1482. /*CAN*/ 1, 1, 1, 1, 1, 1, 1, 1,
  1483. ['/'] 1,
  1484. [0x7f] 1,
  1485. };
  1486. /*
  1487. * Check that the name
  1488. * a) is in valid memory.
  1489. * b) is shorter than 2^16 bytes, so it can fit in a 9P string field.
  1490. * c) contains no frogs.
  1491. * The first byte is known to be addressible by the requester, so the
  1492. * routine works for kernel and user memory both.
  1493. * The parameter slashok flags whether a slash character is an error
  1494. * or a valid character.
  1495. *
  1496. * The parameter dup flags whether the string should be copied
  1497. * out of user space before being scanned the second time.
  1498. * (Otherwise a malicious thread could remove the NUL, causing us
  1499. * to access unchecked addresses.)
  1500. */
  1501. static char*
  1502. validname0(char *aname, int slashok, int dup, ulong pc)
  1503. {
  1504. char *p, *ename, *name, *s;
  1505. uint t;
  1506. int c, n;
  1507. Rune r;
  1508. name = aname;
  1509. if((ulong)name < KZERO){
  1510. validaddr((ulong)name, 1, 0);
  1511. if(!dup)
  1512. print("warning: validname called from %lux with user pointer", pc);
  1513. p = name;
  1514. t = BY2PG-((ulong)p&(BY2PG-1));
  1515. while((ename=vmemchr(p, 0, t)) == nil){
  1516. p += t;
  1517. t = BY2PG;
  1518. }
  1519. }else
  1520. ename = memchr(name, 0, (1<<16));
  1521. if(ename==nil || ename-name>=(1<<16))
  1522. error("name too long");
  1523. s = nil;
  1524. if(dup){
  1525. n = ename-name;
  1526. s = smalloc(n+1);
  1527. memmove(s, name, n);
  1528. s[n] = 0;
  1529. aname = s;
  1530. name = s;
  1531. setmalloctag(s, pc);
  1532. }
  1533. while(*name){
  1534. /* all characters above '~' are ok */
  1535. c = *(uchar*)name;
  1536. if(c >= Runeself)
  1537. name += chartorune(&r, name);
  1538. else{
  1539. if(isfrog[c])
  1540. if(!slashok || c!='/'){
  1541. snprint(up->genbuf, sizeof(up->genbuf), "%s: %q", Ebadchar, aname);
  1542. free(s);
  1543. error(up->genbuf);
  1544. }
  1545. name++;
  1546. }
  1547. }
  1548. return s;
  1549. }
  1550. void
  1551. validname(char *aname, int slashok)
  1552. {
  1553. validname0(aname, slashok, 0, getcallerpc(&aname));
  1554. }
  1555. char*
  1556. validnamedup(char *aname, int slashok)
  1557. {
  1558. return validname0(aname, slashok, 1, getcallerpc(&aname));
  1559. }
  1560. void
  1561. isdir(Chan *c)
  1562. {
  1563. if(c->qid.type & QTDIR)
  1564. return;
  1565. error(Enotdir);
  1566. }
  1567. /*
  1568. * This is necessary because there are many
  1569. * pointers to the top of a given mount list:
  1570. *
  1571. * - the mhead in the namespace hash table
  1572. * - the mhead in chans returned from findmount:
  1573. * used in namec and then by unionread.
  1574. * - the mhead in chans returned from createdir:
  1575. * used in the open/create race protect, which is gone.
  1576. *
  1577. * The RWlock in the Mhead protects the mount list it contains.
  1578. * The mount list is deleted when we cunmount.
  1579. * The RWlock ensures that nothing is using the mount list at that time.
  1580. *
  1581. * It is okay to replace c->mh with whatever you want as
  1582. * long as you are sure you have a unique reference to it.
  1583. *
  1584. * This comment might belong somewhere else.
  1585. */
  1586. void
  1587. putmhead(Mhead *m)
  1588. {
  1589. if(m && decref(m) == 0){
  1590. m->mount = (Mount*)0xCafeBeef;
  1591. free(m);
  1592. }
  1593. }