PageRenderTime 63ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/sys/src/9k/port/chan.c

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