chan.c 34 KB

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