chan.c 34 KB

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