chan.c 34 KB

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