chan.c 35 KB

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