chan.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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. QLock chanprint; /* probably asking for trouble (deadlocks) -rsc */
  9. int domount(Chan**, Mhead**);
  10. void
  11. dumpmount(void) /* DEBUGGING */
  12. {
  13. Pgrp *pg;
  14. Mount *t;
  15. Mhead **h, **he, *f;
  16. if(up == nil){
  17. print("no process for dumpmount\n");
  18. return;
  19. }
  20. pg = up->pgrp;
  21. if(pg == nil){
  22. print("no pgrp for dumpmount\n");
  23. return;
  24. }
  25. rlock(&pg->ns);
  26. if(waserror()) {
  27. runlock(&pg->ns);
  28. nexterror();
  29. }
  30. he = &pg->mnthash[MNTHASH];
  31. for(h = pg->mnthash; h < he; h++) {
  32. for(f = *h; f; f = f->hash) {
  33. print("head: %p: %s 0x%llux.%lud %C %lud -> \n", f,
  34. f->from->name->s, f->from->qid.path,
  35. f->from->qid.vers, devtab[f->from->type]->dc,
  36. f->from->dev);
  37. for(t = f->mount; t; t = t->next)
  38. print("\t%p: %s (umh %p) (path %.8llux dev %C %lud)\n", t, t->to->name->s, t->to->umh, t->to->qid.path, devtab[t->to->type]->dc, t->to->dev);
  39. }
  40. }
  41. poperror();
  42. runlock(&pg->ns);
  43. }
  44. char*
  45. channame(Chan *c) /* DEBUGGING */
  46. {
  47. if(c == nil)
  48. return "<nil chan>";
  49. if(c->name == nil)
  50. return "<nil name>";
  51. if(c->name->s == nil)
  52. return "<nil name.s>";
  53. return c->name->s;
  54. }
  55. enum
  56. {
  57. CNAMESLOP = 20
  58. };
  59. struct
  60. {
  61. Lock;
  62. int fid;
  63. Chan *free;
  64. Chan *list;
  65. }chanalloc;
  66. typedef struct Elemlist Elemlist;
  67. struct Elemlist
  68. {
  69. char *aname; /* original name */
  70. char *name; /* copy of name, so '/' can be overwritten */
  71. int nelems;
  72. char **elems;
  73. int *off;
  74. int mustbedir;
  75. int nerror;
  76. };
  77. #define SEP(c) ((c) == 0 || (c) == '/')
  78. void cleancname(Cname*);
  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->name = 0;
  226. c->ismtpt = 0;
  227. return c;
  228. }
  229. static Ref ncname;
  230. Cname*
  231. newcname(char *s)
  232. {
  233. Cname *n;
  234. int i;
  235. n = smalloc(sizeof(Cname));
  236. i = strlen(s);
  237. n->len = i;
  238. n->alen = i+CNAMESLOP;
  239. n->s = smalloc(n->alen);
  240. memmove(n->s, s, i+1);
  241. n->ref = 1;
  242. incref(&ncname);
  243. return n;
  244. }
  245. void
  246. cnameclose(Cname *n)
  247. {
  248. if(n == nil)
  249. return;
  250. if(decref(n))
  251. return;
  252. decref(&ncname);
  253. free(n->s);
  254. free(n);
  255. }
  256. Cname*
  257. addelem(Cname *n, char *s)
  258. {
  259. int i, a;
  260. char *t;
  261. Cname *new;
  262. if(s[0]=='.' && s[1]=='\0')
  263. return n;
  264. if(n->ref > 1){
  265. /* copy on write */
  266. new = newcname(n->s);
  267. cnameclose(n);
  268. n = new;
  269. }
  270. i = strlen(s);
  271. if(n->len+1+i+1 > n->alen){
  272. a = n->len+1+i+1 + CNAMESLOP;
  273. t = smalloc(a);
  274. memmove(t, n->s, n->len+1);
  275. free(n->s);
  276. n->s = t;
  277. n->alen = a;
  278. }
  279. if(n->len>0 && n->s[n->len-1]!='/' && s[0]!='/') /* don't insert extra slash if one is present */
  280. n->s[n->len++] = '/';
  281. memmove(n->s+n->len, s, i+1);
  282. n->len += i;
  283. if(isdotdot(s))
  284. cleancname(n);
  285. return n;
  286. }
  287. void
  288. chanfree(Chan *c)
  289. {
  290. c->flag = CFREE;
  291. if(c->dirrock != nil){
  292. free(c->dirrock);
  293. c->dirrock = 0;
  294. c->nrock = 0;
  295. c->mrock = 0;
  296. }
  297. if(c->umh != nil){
  298. putmhead(c->umh);
  299. c->umh = nil;
  300. }
  301. if(c->umc != nil){
  302. cclose(c->umc);
  303. c->umc = nil;
  304. }
  305. if(c->mux != nil){
  306. muxclose(c->mux);
  307. c->mux = nil;
  308. }
  309. if(c->mchan != nil){
  310. cclose(c->mchan);
  311. c->mchan = nil;
  312. }
  313. cnameclose(c->name);
  314. lock(&chanalloc);
  315. c->next = chanalloc.free;
  316. chanalloc.free = c;
  317. unlock(&chanalloc);
  318. }
  319. void
  320. cclose(Chan *c)
  321. {
  322. if(c->flag&CFREE)
  323. panic("cclose %lux", getcallerpc(&c));
  324. if(decref(c))
  325. return;
  326. if(!waserror()){
  327. devtab[c->type]->close(c);
  328. poperror();
  329. }
  330. chanfree(c);
  331. }
  332. /*
  333. * Make sure we have the only copy of c. (Copy on write.)
  334. */
  335. Chan*
  336. cunique(Chan *c)
  337. {
  338. Chan *nc;
  339. if(c->ref != 1) {
  340. nc = cclone(c);
  341. cclose(c);
  342. c = nc;
  343. }
  344. return c;
  345. }
  346. int
  347. eqqid(Qid a, Qid b)
  348. {
  349. return a.path==b.path && a.vers==b.vers;
  350. }
  351. int
  352. eqchan(Chan *a, Chan *b, int pathonly)
  353. {
  354. if(a->qid.path != b->qid.path)
  355. return 0;
  356. if(!pathonly && a->qid.vers!=b->qid.vers)
  357. return 0;
  358. if(a->type != b->type)
  359. return 0;
  360. if(a->dev != b->dev)
  361. return 0;
  362. return 1;
  363. }
  364. int
  365. eqchantdqid(Chan *a, int type, int dev, Qid qid, int pathonly)
  366. {
  367. if(a->qid.path != qid.path)
  368. return 0;
  369. if(!pathonly && a->qid.vers!=qid.vers)
  370. return 0;
  371. if(a->type != type)
  372. return 0;
  373. if(a->dev != dev)
  374. return 0;
  375. return 1;
  376. }
  377. Mhead*
  378. newmhead(Chan *from)
  379. {
  380. Mhead *mh;
  381. mh = smalloc(sizeof(Mhead));
  382. mh->ref = 1;
  383. mh->from = from;
  384. incref(from);
  385. /*
  386. n = from->name->len;
  387. if(n >= sizeof(mh->fromname))
  388. n = sizeof(mh->fromname)-1;
  389. memmove(mh->fromname, from->name->s, n);
  390. mh->fromname[n] = 0;
  391. */
  392. return mh;
  393. }
  394. int
  395. cmount(Chan **newp, Chan *old, int flag, char *spec)
  396. {
  397. Pgrp *pg;
  398. int order, flg;
  399. Mhead *m, **l, *mh;
  400. Mount *nm, *f, *um, **h;
  401. Chan *new;
  402. if(QTDIR & (old->qid.type^(*newp)->qid.type))
  403. error(Emount);
  404. if(old->umh)print("cmount old extra umh\n");
  405. order = flag&MORDER;
  406. if((old->qid.type&QTDIR)==0 && order != MREPL)
  407. error(Emount);
  408. new = *newp;
  409. mh = new->umh;
  410. /*
  411. * Not allowed to bind when the old directory
  412. * is itself a union. (Maybe it should be allowed, but I don't see
  413. * what the semantics would be.)
  414. *
  415. * We need to check mh->mount->next to tell unions apart from
  416. * simple mount points, so that things like
  417. * mount -c fd /root
  418. * bind -c /root /
  419. * work. The check of mount->mflag catches things like
  420. * mount fd /root
  421. * bind -c /root /
  422. *
  423. * This is far more complicated than it should be, but I don't
  424. * see an easier way at the moment. -rsc
  425. */
  426. if((flag&MCREATE) && mh && mh->mount
  427. && (mh->mount->next || !(mh->mount->mflag&MCREATE)))
  428. error(Emount);
  429. pg = up->pgrp;
  430. wlock(&pg->ns);
  431. l = &MOUNTH(pg, old->qid);
  432. for(m = *l; m; m = m->hash) {
  433. if(eqchan(m->from, old, 1))
  434. break;
  435. l = &m->hash;
  436. }
  437. if(m == nil) {
  438. /*
  439. * nothing mounted here yet. create a mount
  440. * head and add to the hash table.
  441. */
  442. m = newmhead(old);
  443. *l = m;
  444. /*
  445. * if this is a union mount, add the old
  446. * node to the mount chain.
  447. */
  448. if(order != MREPL)
  449. m->mount = newmount(m, old, 0, 0);
  450. }
  451. wlock(&m->lock);
  452. if(waserror()){
  453. wunlock(&m->lock);
  454. nexterror();
  455. }
  456. wunlock(&pg->ns);
  457. nm = newmount(m, new, flag, spec);
  458. if(mh != nil && mh->mount != nil) {
  459. /*
  460. * copy a union when binding it onto a directory
  461. */
  462. flg = order;
  463. if(order == MREPL)
  464. flg = MAFTER;
  465. h = &nm->next;
  466. um = mh->mount;
  467. for(um = um->next; um; um = um->next) {
  468. f = newmount(m, um->to, flg, um->spec);
  469. *h = f;
  470. h = &f->next;
  471. }
  472. }
  473. if(m->mount && order == MREPL) {
  474. mountfree(m->mount);
  475. m->mount = 0;
  476. }
  477. if(flag & MCREATE)
  478. nm->mflag |= MCREATE;
  479. if(m->mount && order == MAFTER) {
  480. for(f = m->mount; f->next; f = f->next)
  481. ;
  482. f->next = nm;
  483. }
  484. else {
  485. for(f = nm; f->next; f = f->next)
  486. ;
  487. f->next = m->mount;
  488. m->mount = nm;
  489. }
  490. wunlock(&m->lock);
  491. poperror();
  492. return nm->mountid;
  493. }
  494. void
  495. cunmount(Chan *mnt, Chan *mounted)
  496. {
  497. Pgrp *pg;
  498. Mhead *m, **l;
  499. Mount *f, **p;
  500. if(mnt->umh) /* should not happen */
  501. print("cunmount newp extra umh %p has %p\n", mnt, mnt->umh);
  502. /*
  503. * It _can_ happen that mounted->umh is non-nil,
  504. * because mounted is the result of namec(Aopen)
  505. * (see sysfile.c:/^sysunmount).
  506. * If we open a union directory, it will have a umh.
  507. * Although surprising, this is okay, since the
  508. * cclose will take care of freeing the umh.
  509. */
  510. pg = up->pgrp;
  511. wlock(&pg->ns);
  512. l = &MOUNTH(pg, mnt->qid);
  513. for(m = *l; m; m = m->hash) {
  514. if(eqchan(m->from, mnt, 1))
  515. break;
  516. l = &m->hash;
  517. }
  518. if(m == 0) {
  519. wunlock(&pg->ns);
  520. error(Eunmount);
  521. }
  522. wlock(&m->lock);
  523. if(mounted == 0) {
  524. *l = m->hash;
  525. wunlock(&pg->ns);
  526. mountfree(m->mount);
  527. m->mount = nil;
  528. cclose(m->from);
  529. wunlock(&m->lock);
  530. putmhead(m);
  531. return;
  532. }
  533. p = &m->mount;
  534. for(f = *p; f; f = f->next) {
  535. /* BUG: Needs to be 2 pass */
  536. if(eqchan(f->to, mounted, 1) ||
  537. (f->to->mchan && eqchan(f->to->mchan, mounted, 1))) {
  538. *p = f->next;
  539. f->next = 0;
  540. mountfree(f);
  541. if(m->mount == nil) {
  542. *l = m->hash;
  543. cclose(m->from);
  544. wunlock(&m->lock);
  545. wunlock(&pg->ns);
  546. putmhead(m);
  547. return;
  548. }
  549. wunlock(&m->lock);
  550. wunlock(&pg->ns);
  551. return;
  552. }
  553. p = &f->next;
  554. }
  555. wunlock(&m->lock);
  556. wunlock(&pg->ns);
  557. error(Eunion);
  558. }
  559. Chan*
  560. cclone(Chan *c)
  561. {
  562. Chan *nc;
  563. Walkqid *wq;
  564. wq = devtab[c->type]->walk(c, nil, nil, 0);
  565. if(wq == nil)
  566. error("clone failed");
  567. nc = wq->clone;
  568. free(wq);
  569. nc->name = c->name;
  570. if(c->name)
  571. incref(c->name);
  572. return nc;
  573. }
  574. int
  575. findmount(Chan **cp, Mhead **mp, int type, int dev, Qid qid)
  576. {
  577. Pgrp *pg;
  578. Mhead *m;
  579. pg = up->pgrp;
  580. rlock(&pg->ns);
  581. for(m = MOUNTH(pg, qid); m; m = m->hash){
  582. rlock(&m->lock);
  583. if(m->from == nil){
  584. print("m %p m->from 0\n", m);
  585. runlock(&m->lock);
  586. continue;
  587. }
  588. if(eqchantdqid(m->from, type, dev, qid, 1)){
  589. runlock(&pg->ns);
  590. if(mp != nil){
  591. incref(m);
  592. if(*mp != nil)
  593. putmhead(*mp);
  594. *mp = m;
  595. }
  596. if(*cp != nil)
  597. cclose(*cp);
  598. incref(m->mount->to);
  599. *cp = m->mount->to;
  600. runlock(&m->lock);
  601. return 1;
  602. }
  603. runlock(&m->lock);
  604. }
  605. runlock(&pg->ns);
  606. return 0;
  607. }
  608. int
  609. domount(Chan **cp, Mhead **mp)
  610. {
  611. return findmount(cp, mp, (*cp)->type, (*cp)->dev, (*cp)->qid);
  612. }
  613. Chan*
  614. undomount(Chan *c, Cname *name)
  615. {
  616. Chan *nc;
  617. Pgrp *pg;
  618. Mount *t;
  619. Mhead **h, **he, *f;
  620. pg = up->pgrp;
  621. rlock(&pg->ns);
  622. if(waserror()) {
  623. runlock(&pg->ns);
  624. nexterror();
  625. }
  626. he = &pg->mnthash[MNTHASH];
  627. for(h = pg->mnthash; h < he; h++) {
  628. for(f = *h; f; f = f->hash) {
  629. if(strcmp(f->from->name->s, name->s) != 0)
  630. continue;
  631. for(t = f->mount; t; t = t->next) {
  632. if(eqchan(c, t->to, 1)) {
  633. /*
  634. * We want to come out on the left hand side of the mount
  635. * point using the element of the union that we entered on.
  636. * To do this, find the element that has a from name of
  637. * c->name->s.
  638. */
  639. if(strcmp(t->head->from->name->s, name->s) != 0)
  640. continue;
  641. nc = t->head->from;
  642. incref(nc);
  643. cclose(c);
  644. c = nc;
  645. break;
  646. }
  647. }
  648. }
  649. }
  650. poperror();
  651. runlock(&pg->ns);
  652. return c;
  653. }
  654. /*
  655. * Either walks all the way or not at all. No partial results in *cp.
  656. * *nerror is the number of names to display in an error message.
  657. */
  658. static char Edoesnotexist[] = "does not exist";
  659. int
  660. walk(Chan **cp, char **names, int nnames, int nomount, int *nerror)
  661. {
  662. int dev, dotdot, i, n, nhave, ntry, type;
  663. Chan *c, *nc;
  664. Cname *cname;
  665. Mount *f;
  666. Mhead *mh, *nmh;
  667. Walkqid *wq;
  668. c = *cp;
  669. incref(c);
  670. cname = c->name;
  671. incref(cname);
  672. mh = nil;
  673. /*
  674. * While we haven't gotten all the way down the path:
  675. * 1. step through a mount point, if any
  676. * 2. send a walk request for initial dotdot or initial prefix without dotdot
  677. * 3. move to the first mountpoint along the way.
  678. * 4. repeat.
  679. *
  680. * An invariant is that each time through the loop, c is on the undomount
  681. * side of the mount point, and c's name is cname.
  682. */
  683. for(nhave=0; nhave<nnames; nhave+=n){
  684. if((c->qid.type&QTDIR)==0){
  685. if(nerror)
  686. *nerror = nhave;
  687. cnameclose(cname);
  688. cclose(c);
  689. strcpy(up->errstr, Enotdir);
  690. if(mh != nil)
  691. putmhead(mh);
  692. return -1;
  693. }
  694. ntry = nnames - nhave;
  695. if(ntry > MAXWELEM)
  696. ntry = MAXWELEM;
  697. dotdot = 0;
  698. for(i=0; i<ntry; i++){
  699. if(isdotdot(names[nhave+i])){
  700. if(i==0) {
  701. dotdot = 1;
  702. ntry = 1;
  703. } else
  704. ntry = i;
  705. break;
  706. }
  707. }
  708. if(!dotdot && !nomount)
  709. domount(&c, &mh);
  710. type = c->type;
  711. dev = c->dev;
  712. if((wq = devtab[type]->walk(c, nil, names+nhave, ntry)) == nil){
  713. /* try a union mount, if any */
  714. if(mh && !nomount){
  715. /*
  716. * mh->mount->to == c, so start at mh->mount->next
  717. */
  718. rlock(&mh->lock);
  719. for(f = mh->mount->next; f; f = f->next)
  720. if((wq = devtab[f->to->type]->walk(f->to, nil, names+nhave, ntry)) != nil)
  721. break;
  722. runlock(&mh->lock);
  723. if(f != nil){
  724. type = f->to->type;
  725. dev = f->to->dev;
  726. }
  727. }
  728. if(wq == nil){
  729. cclose(c);
  730. cnameclose(cname);
  731. if(nerror)
  732. *nerror = nhave+1;
  733. if(mh != nil)
  734. putmhead(mh);
  735. return -1;
  736. }
  737. }
  738. nmh = nil;
  739. if(dotdot){
  740. assert(wq->nqid == 1);
  741. assert(wq->clone != nil);
  742. cname = addelem(cname, "..");
  743. nc = undomount(wq->clone, cname);
  744. n = 1;
  745. }else{
  746. nc = nil;
  747. if(!nomount)
  748. for(i=0; i<wq->nqid && i<ntry-1; i++)
  749. if(findmount(&nc, &nmh, type, dev, wq->qid[i]))
  750. break;
  751. if(nc == nil){ /* no mount points along path */
  752. if(wq->clone == nil){
  753. cclose(c);
  754. cnameclose(cname);
  755. if(wq->nqid==0 || (wq->qid[wq->nqid-1].type&QTDIR)){
  756. if(nerror)
  757. *nerror = nhave+wq->nqid+1;
  758. strcpy(up->errstr, Edoesnotexist);
  759. }else{
  760. if(nerror)
  761. *nerror = nhave+wq->nqid;
  762. strcpy(up->errstr, Enotdir);
  763. }
  764. free(wq);
  765. if(mh != nil)
  766. putmhead(mh);
  767. return -1;
  768. }
  769. n = wq->nqid;
  770. nc = wq->clone;
  771. }else{ /* stopped early, at a mount point */
  772. if(wq->clone != nil){
  773. cclose(wq->clone);
  774. wq->clone = nil;
  775. }
  776. n = i+1;
  777. }
  778. for(i=0; i<n; i++)
  779. cname = addelem(cname, names[nhave+i]);
  780. }
  781. cclose(c);
  782. c = nc;
  783. putmhead(mh);
  784. mh = nmh;
  785. free(wq);
  786. }
  787. putmhead(mh);
  788. c = cunique(c);
  789. if(c->umh != nil){ //BUG
  790. print("walk umh\n");
  791. putmhead(c->umh);
  792. c->umh = nil;
  793. }
  794. cnameclose(c->name);
  795. c->name = cname;
  796. cclose(*cp);
  797. *cp = c;
  798. if(nerror)
  799. *nerror = nhave;
  800. return 0;
  801. }
  802. /*
  803. * c is a mounted non-creatable directory. find a creatable one.
  804. */
  805. Chan*
  806. createdir(Chan *c, Mhead *m)
  807. {
  808. Chan *nc;
  809. Mount *f;
  810. rlock(&m->lock);
  811. if(waserror()) {
  812. runlock(&m->lock);
  813. nexterror();
  814. }
  815. for(f = m->mount; f; f = f->next) {
  816. if(f->mflag&MCREATE) {
  817. nc = cclone(f->to);
  818. runlock(&m->lock);
  819. poperror();
  820. cclose(c);
  821. return nc;
  822. }
  823. }
  824. error(Enocreate);
  825. return 0;
  826. }
  827. void
  828. saveregisters(void)
  829. {
  830. }
  831. /*
  832. * In place, rewrite name to compress multiple /, eliminate ., and process ..
  833. */
  834. void
  835. cleancname(Cname *n)
  836. {
  837. char *p;
  838. if(n->s[0] == '#'){
  839. p = strchr(n->s, '/');
  840. if(p == nil)
  841. return;
  842. cleanname(p);
  843. /*
  844. * The correct name is #i rather than #i/,
  845. * but the correct name of #/ is #/.
  846. */
  847. if(strcmp(p, "/")==0 && n->s[1] != '/')
  848. *p = '\0';
  849. }else
  850. cleanname(n->s);
  851. n->len = strlen(n->s);
  852. }
  853. static void
  854. growparse(Elemlist *e)
  855. {
  856. char **new;
  857. int *inew;
  858. enum { Delta = 8 };
  859. if(e->nelems % Delta == 0){
  860. new = smalloc((e->nelems+Delta) * sizeof(char*));
  861. memmove(new, e->elems, e->nelems*sizeof(char*));
  862. free(e->elems);
  863. e->elems = new;
  864. inew = smalloc((e->nelems+Delta+1) * sizeof(int));
  865. memmove(inew, e->off, e->nelems*sizeof(int));
  866. free(e->off);
  867. e->off = inew;
  868. }
  869. }
  870. /*
  871. * The name is known to be valid.
  872. * Copy the name so slashes can be overwritten.
  873. * An empty string will set nelem=0.
  874. * A path ending in / or /. or /.//./ etc. will have
  875. * e.mustbedir = 1, so that we correctly
  876. * reject, e.g., "/adm/users/." when /adm/users is a file
  877. * rather than a directory.
  878. */
  879. static void
  880. parsename(char *name, Elemlist *e)
  881. {
  882. char *slash;
  883. kstrdup(&e->name, name);
  884. name = e->name;
  885. e->nelems = 0;
  886. e->elems = nil;
  887. e->off = smalloc(sizeof(int));
  888. e->off[0] = skipslash(name) - name;
  889. for(;;){
  890. name = skipslash(name);
  891. if(*name=='\0'){
  892. e->mustbedir = 1;
  893. break;
  894. }
  895. growparse(e);
  896. e->elems[e->nelems++] = name;
  897. slash = utfrune(name, '/');
  898. if(slash == nil){
  899. e->off[e->nelems] = name+strlen(name) - e->name;
  900. e->mustbedir = 0;
  901. break;
  902. }
  903. e->off[e->nelems] = slash - e->name;
  904. *slash++ = '\0';
  905. name = slash;
  906. }
  907. e->off[e->nelems] = name - e->name;
  908. }
  909. void*
  910. memrchr(void *va, int c, long n)
  911. {
  912. uchar *a, *e;
  913. a = va;
  914. for(e=a+n-1; e>a; e--)
  915. if(*e == c)
  916. return e;
  917. return nil;
  918. }
  919. void
  920. namelenerror(char *aname, int len, char *err)
  921. {
  922. char *name;
  923. if(len < ERRMAX/3 || (name=memrchr(aname, '/', len))==nil || name==aname)
  924. snprint(up->genbuf, sizeof up->genbuf, "%.*s", len, aname);
  925. else
  926. snprint(up->genbuf, sizeof up->genbuf, "...%.*s", (int)(len-(name-aname)), name);
  927. snprint(up->errstr, ERRMAX, "%#q %s", up->genbuf, err);
  928. nexterror();
  929. }
  930. void
  931. nameerror(char *name, char *err)
  932. {
  933. namelenerror(name, strlen(name), err);
  934. }
  935. /*
  936. * Turn a name into a channel.
  937. * &name[0] is known to be a valid address. It may be a kernel address.
  938. *
  939. * Opening with amode Aopen, Acreate, Aremove, or Aaccess guarantees
  940. * that the result will be the only reference to that particular fid.
  941. * This is necessary since we might pass the result to
  942. * devtab[]->remove().
  943. *
  944. * Opening Atodir or Amount does not guarantee this.
  945. *
  946. * Under certain circumstances, opening Aaccess will cause
  947. * an unnecessary clone in order to get a cunique Chan so it
  948. * can attach the correct name. Sysstat and sys_stat need the
  949. * correct name so they can rewrite the stat info.
  950. */
  951. Chan*
  952. namec(char *aname, int amode, int omode, ulong perm)
  953. {
  954. int n, t, nomount;
  955. Chan *c, *cnew;
  956. Cname *cname;
  957. Elemlist e;
  958. Rune r;
  959. Mhead *m;
  960. char *createerr, tmperrbuf[ERRMAX];
  961. char *name;
  962. if(aname[0] == '\0')
  963. error("empty file name");
  964. aname = validnamedup(aname, 1);
  965. if(waserror()){
  966. free(aname);
  967. nexterror();
  968. }
  969. name = aname;
  970. /*
  971. * Find the starting off point (the current slash, the root of
  972. * a device tree, or the current dot) as well as the name to
  973. * evaluate starting there.
  974. */
  975. nomount = 0;
  976. switch(name[0]){
  977. case '/':
  978. c = up->slash;
  979. incref(c);
  980. break;
  981. case '#':
  982. nomount = 1;
  983. up->genbuf[0] = '\0';
  984. n = 0;
  985. while(*name!='\0' && (*name != '/' || n < 2)){
  986. if(n >= sizeof(up->genbuf)-1)
  987. error(Efilename);
  988. up->genbuf[n++] = *name++;
  989. }
  990. up->genbuf[n] = '\0';
  991. /*
  992. * noattach is sandboxing.
  993. *
  994. * the OK exceptions are:
  995. * | it only gives access to pipes you create
  996. * d this process's file descriptors
  997. * e this process's environment
  998. * the iffy exceptions are:
  999. * c time and pid, but also cons and consctl
  1000. * p control of your own processes (and unfortunately
  1001. * any others left unprotected)
  1002. */
  1003. n = chartorune(&r, up->genbuf+1)+1;
  1004. /* actually / is caught by parsing earlier */
  1005. if(utfrune("M", r))
  1006. error(Enoattach);
  1007. if(up->pgrp->noattach && utfrune("|decp", r)==nil)
  1008. error(Enoattach);
  1009. t = devno(r, 1);
  1010. if(t == -1)
  1011. error(Ebadsharp);
  1012. c = devtab[t]->attach(up->genbuf+n);
  1013. break;
  1014. default:
  1015. c = up->dot;
  1016. incref(c);
  1017. break;
  1018. }
  1019. e.aname = aname;
  1020. e.name = nil;
  1021. e.elems = nil;
  1022. e.off = nil;
  1023. e.nelems = 0;
  1024. e.nerror = 0;
  1025. if(waserror()){
  1026. cclose(c);
  1027. free(e.name);
  1028. free(e.elems);
  1029. free(e.off);
  1030. /*
  1031. * Prepare nice error, showing first e.nerror elements of name.
  1032. */
  1033. if(e.nerror == 0)
  1034. nexterror();
  1035. strcpy(tmperrbuf, up->errstr);
  1036. namelenerror(aname, (name-aname)+e.off[e.nerror], tmperrbuf);
  1037. }
  1038. /*
  1039. * Build a list of elements in the path.
  1040. */
  1041. parsename(name, &e);
  1042. /*
  1043. * On create, ....
  1044. */
  1045. if(amode == Acreate){
  1046. /* perm must have DMDIR if last element is / or /. */
  1047. if(e.mustbedir && !(perm&DMDIR)){
  1048. e.nerror = e.nelems;
  1049. error("create without DMDIR");
  1050. }
  1051. /* don't try to walk the last path element just yet. */
  1052. if(e.nelems == 0)
  1053. error(Eexist);
  1054. e.nelems--;
  1055. }
  1056. if(walk(&c, e.elems, e.nelems, nomount, &e.nerror) < 0){
  1057. if(e.nerror < 0 || e.nerror > e.nelems){
  1058. print("namec %s walk error nerror=%d\n", aname, e.nerror);
  1059. e.nerror = 0;
  1060. }
  1061. nexterror();
  1062. }
  1063. if(e.mustbedir && !(c->qid.type&QTDIR))
  1064. error("not a directory");
  1065. if(amode == Aopen && (omode&3) == OEXEC && (c->qid.type&QTDIR))
  1066. error("cannot exec directory");
  1067. switch(amode){
  1068. case Abind:
  1069. m = nil;
  1070. if(!nomount)
  1071. domount(&c, &m);
  1072. if(c->umh != nil)
  1073. putmhead(c->umh);
  1074. c->umh = m;
  1075. break;
  1076. case Aaccess:
  1077. case Aremove:
  1078. case Aopen:
  1079. Open:
  1080. /* save the name; domount might change c */
  1081. cname = c->name;
  1082. incref(cname);
  1083. m = nil;
  1084. if(!nomount)
  1085. domount(&c, &m);
  1086. /* our own copy to open or remove */
  1087. c = cunique(c);
  1088. /* now it's our copy anyway, we can put the name back */
  1089. cnameclose(c->name);
  1090. c->name = cname;
  1091. /* record whether c is on a mount point */
  1092. c->ismtpt = m!=nil;
  1093. switch(amode){
  1094. case Aaccess:
  1095. case Aremove:
  1096. putmhead(m);
  1097. break;
  1098. case Aopen:
  1099. case Acreate:
  1100. if(c->umh != nil){
  1101. print("cunique umh Open\n");
  1102. putmhead(c->umh);
  1103. c->umh = nil;
  1104. }
  1105. /* only save the mount head if it's a multiple element union */
  1106. if(m && m->mount && m->mount->next)
  1107. c->umh = m;
  1108. else
  1109. putmhead(m);
  1110. /* save registers else error() in open has wrong value of c saved */
  1111. saveregisters();
  1112. if(omode == OEXEC)
  1113. c->flag &= ~CCACHE;
  1114. c = devtab[c->type]->open(c, omode&~OCEXEC);
  1115. if(omode & OCEXEC)
  1116. c->flag |= CCEXEC;
  1117. if(omode & ORCLOSE)
  1118. c->flag |= CRCLOSE;
  1119. break;
  1120. }
  1121. break;
  1122. case Atodir:
  1123. /*
  1124. * Directories (e.g. for cd) are left before the mount point,
  1125. * so one may mount on / or . and see the effect.
  1126. */
  1127. if(!(c->qid.type & QTDIR))
  1128. error(Enotdir);
  1129. break;
  1130. case Amount:
  1131. /*
  1132. * When mounting on an already mounted upon directory,
  1133. * one wants subsequent mounts to be attached to the
  1134. * original directory, not the replacement. Don't domount.
  1135. */
  1136. break;
  1137. case Acreate:
  1138. /*
  1139. * We've already walked all but the last element.
  1140. * If the last exists, try to open it OTRUNC.
  1141. * If omode&OEXCL is set, just give up.
  1142. */
  1143. e.nelems++;
  1144. e.nerror++;
  1145. if(walk(&c, e.elems+e.nelems-1, 1, nomount, nil) == 0){
  1146. if(omode&OEXCL)
  1147. error(Eexist);
  1148. omode |= OTRUNC;
  1149. goto Open;
  1150. }
  1151. /*
  1152. * The semantics of the create(2) system call are that if the
  1153. * file exists and can be written, it is to be opened with truncation.
  1154. * On the other hand, the create(5) message fails if the file exists.
  1155. * If we get two create(2) calls happening simultaneously,
  1156. * they might both get here and send create(5) messages, but only
  1157. * one of the messages will succeed. To provide the expected create(2)
  1158. * semantics, the call with the failed message needs to try the above
  1159. * walk again, opening for truncation. This correctly solves the
  1160. * create/create race, in the sense that any observable outcome can
  1161. * be explained as one happening before the other.
  1162. * The create/create race is quite common. For example, it happens
  1163. * when two rc subshells simultaneously update the same
  1164. * environment variable.
  1165. *
  1166. * The implementation still admits a create/create/remove race:
  1167. * (A) walk to file, fails
  1168. * (B) walk to file, fails
  1169. * (A) create file, succeeds, returns
  1170. * (B) create file, fails
  1171. * (A) remove file, succeeds, returns
  1172. * (B) walk to file, return failure.
  1173. *
  1174. * This is hardly as common as the create/create race, and is really
  1175. * not too much worse than what might happen if (B) got a hold of a
  1176. * file descriptor and then the file was removed -- either way (B) can't do
  1177. * anything with the result of the create call. So we don't care about this race.
  1178. *
  1179. * Applications that care about more fine-grained decision of the races
  1180. * can use the OEXCL flag to get at the underlying create(5) semantics;
  1181. * by default we provide the common case.
  1182. *
  1183. * We need to stay behind the mount point in case we
  1184. * need to do the first walk again (should the create fail).
  1185. *
  1186. * We also need to cross the mount point and find the directory
  1187. * in the union in which we should be creating.
  1188. *
  1189. * The channel staying behind is c, the one moving forward is cnew.
  1190. */
  1191. m = nil;
  1192. cnew = nil; /* is this assignment necessary? */
  1193. if(!waserror()){ /* try create */
  1194. if(!nomount && findmount(&cnew, &m, c->type, c->dev, c->qid))
  1195. cnew = createdir(cnew, m);
  1196. else{
  1197. cnew = c;
  1198. incref(cnew);
  1199. }
  1200. /*
  1201. * We need our own copy of the Chan because we're
  1202. * about to send a create, which will move it. Once we have
  1203. * our own copy, we can fix the name, which might be wrong
  1204. * if findmount gave us a new Chan.
  1205. */
  1206. cnew = cunique(cnew);
  1207. cnameclose(cnew->name);
  1208. cnew->name = c->name;
  1209. incref(cnew->name);
  1210. devtab[cnew->type]->create(cnew, e.elems[e.nelems-1], omode&~(OEXCL|OCEXEC), perm);
  1211. poperror();
  1212. if(omode & OCEXEC)
  1213. cnew->flag |= CCEXEC;
  1214. if(omode & ORCLOSE)
  1215. cnew->flag |= CRCLOSE;
  1216. if(m)
  1217. putmhead(m);
  1218. cclose(c);
  1219. c = cnew;
  1220. c->name = addelem(c->name, e.elems[e.nelems-1]);
  1221. break;
  1222. }else{ /* create failed */
  1223. cclose(cnew);
  1224. if(m)
  1225. putmhead(m);
  1226. if(omode & OEXCL)
  1227. nexterror();
  1228. /* save error */
  1229. createerr = up->errstr;
  1230. up->errstr = tmperrbuf;
  1231. /* note: we depend that walk does not error */
  1232. if(walk(&c, e.elems+e.nelems-1, 1, nomount, nil) < 0){
  1233. up->errstr = createerr;
  1234. error(createerr); /* report true error */
  1235. }
  1236. up->errstr = createerr;
  1237. omode |= OTRUNC;
  1238. goto Open;
  1239. }
  1240. panic("namec: not reached");
  1241. default:
  1242. panic("unknown namec access %d\n", amode);
  1243. }
  1244. /* place final element in genbuf for e.g. exec */
  1245. if(e.nelems > 0)
  1246. kstrcpy(up->genbuf, e.elems[e.nelems-1], sizeof up->genbuf);
  1247. else
  1248. kstrcpy(up->genbuf, ".", sizeof up->genbuf);
  1249. free(e.name);
  1250. free(e.elems);
  1251. free(e.off);
  1252. poperror(); /* e c */
  1253. free(name);
  1254. poperror(); /* name */
  1255. return c;
  1256. }
  1257. /*
  1258. * name is valid. skip leading / and ./ as much as possible
  1259. */
  1260. char*
  1261. skipslash(char *name)
  1262. {
  1263. while(name[0]=='/' || (name[0]=='.' && (name[1]==0 || name[1]=='/')))
  1264. name++;
  1265. return name;
  1266. }
  1267. char isfrog[256]={
  1268. /*NUL*/ 1, 1, 1, 1, 1, 1, 1, 1,
  1269. /*BKS*/ 1, 1, 1, 1, 1, 1, 1, 1,
  1270. /*DLE*/ 1, 1, 1, 1, 1, 1, 1, 1,
  1271. /*CAN*/ 1, 1, 1, 1, 1, 1, 1, 1,
  1272. ['/'] 1,
  1273. [0x7f] 1,
  1274. };
  1275. /*
  1276. * Check that the name
  1277. * a) is in valid memory.
  1278. * b) is shorter than 2^16 bytes, so it can fit in a 9P string field.
  1279. * c) contains no frogs.
  1280. * The first byte is known to be addressible by the requester, so the
  1281. * routine works for kernel and user memory both.
  1282. * The parameter slashok flags whether a slash character is an error
  1283. * or a valid character.
  1284. *
  1285. * The parameter dup flags whether the string should be copied
  1286. * out of user space before being scanned the second time.
  1287. * (Otherwise a malicious thread could remove the NUL, causing us
  1288. * to access unchecked addresses.)
  1289. */
  1290. static char*
  1291. validname0(char *aname, int slashok, int dup, ulong pc)
  1292. {
  1293. char *p, *ename, *name, *s;
  1294. uint t;
  1295. int c, n;
  1296. Rune r;
  1297. name = aname;
  1298. if(((ulong)name & KZERO) != KZERO) {
  1299. if(!dup)
  1300. print("warning: validname called from %lux with user pointer", pc);
  1301. p = name;
  1302. t = BY2PG-((ulong)p&(BY2PG-1));
  1303. while((ename=vmemchr(p, 0, t)) == nil) {
  1304. p += t;
  1305. t = BY2PG;
  1306. }
  1307. }else
  1308. ename = memchr(name, 0, (1<<16));
  1309. if(ename==nil || ename-name>=(1<<16))
  1310. error("name too long");
  1311. s = nil;
  1312. if(dup){
  1313. n = ename-name;
  1314. s = smalloc(n+1);
  1315. memmove(s, name, n);
  1316. s[n] = 0;
  1317. aname = s;
  1318. name = s;
  1319. }
  1320. while(*name){
  1321. /* all characters above '~' are ok */
  1322. c = *(uchar*)name;
  1323. if(c >= Runeself)
  1324. name += chartorune(&r, name);
  1325. else{
  1326. if(isfrog[c])
  1327. if(!slashok || c!='/'){
  1328. snprint(up->genbuf, sizeof(up->genbuf), "%s: %q", Ebadchar, aname);
  1329. free(s);
  1330. error(up->genbuf);
  1331. }
  1332. name++;
  1333. }
  1334. }
  1335. return s;
  1336. }
  1337. void
  1338. validname(char *aname, int slashok)
  1339. {
  1340. validname0(aname, slashok, 0, getcallerpc(&aname));
  1341. }
  1342. char*
  1343. validnamedup(char *aname, int slashok)
  1344. {
  1345. return validname0(aname, slashok, 1, 0);
  1346. }
  1347. void
  1348. isdir(Chan *c)
  1349. {
  1350. if(c->qid.type & QTDIR)
  1351. return;
  1352. error(Enotdir);
  1353. }
  1354. /*
  1355. * This is necessary because there are many
  1356. * pointers to the top of a given mount list:
  1357. *
  1358. * - the mhead in the namespace hash table
  1359. * - the mhead in chans returned from findmount:
  1360. * used in namec and then by unionread.
  1361. * - the mhead in chans returned from createdir:
  1362. * used in the open/create race protect, which is gone.
  1363. *
  1364. * The RWlock in the Mhead protects the mount list it contains.
  1365. * The mount list is deleted when we cunmount.
  1366. * The RWlock ensures that nothing is using the mount list at that time.
  1367. *
  1368. * It is okay to replace c->mh with whatever you want as
  1369. * long as you are sure you have a unique reference to it.
  1370. *
  1371. * This comment might belong somewhere else.
  1372. */
  1373. void
  1374. putmhead(Mhead *m)
  1375. {
  1376. if(m && decref(m) == 0){
  1377. m->mount = (Mount*)0xCafeBeef;
  1378. free(m);
  1379. }
  1380. }