chan.c 31 KB

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