PageRenderTime 64ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/kern/chan.c

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