chan.c 33 KB

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