PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/os/port/chan.c

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