sysfile.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  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. /*
  8. * The sys*() routines needn't poperror() as they return directly to syscall().
  9. */
  10. static void
  11. unlockfgrp(Fgrp *f)
  12. {
  13. int ex;
  14. ex = f->exceed;
  15. f->exceed = 0;
  16. unlock(f);
  17. if(ex)
  18. pprint("warning: process exceeds %d file descriptors\n", ex);
  19. }
  20. int
  21. growfd(Fgrp *f, int fd) /* fd is always >= 0 */
  22. {
  23. Chan **newfd, **oldfd;
  24. if(fd < f->nfd)
  25. return 0;
  26. if(fd >= f->nfd+DELTAFD)
  27. return -1; /* out of range */
  28. /*
  29. * Unbounded allocation is unwise; besides, there are only 16 bits
  30. * of fid in 9P
  31. */
  32. if(f->nfd >= 5000){
  33. Exhausted:
  34. print("no free file descriptors\n");
  35. return -1;
  36. }
  37. newfd = malloc((f->nfd+DELTAFD)*sizeof(Chan*));
  38. if(newfd == 0)
  39. goto Exhausted;
  40. oldfd = f->fd;
  41. memmove(newfd, oldfd, f->nfd*sizeof(Chan*));
  42. f->fd = newfd;
  43. free(oldfd);
  44. f->nfd += DELTAFD;
  45. if(fd > f->maxfd){
  46. if(fd/100 > f->maxfd/100)
  47. f->exceed = (fd/100)*100;
  48. f->maxfd = fd;
  49. }
  50. return 1;
  51. }
  52. /*
  53. * this assumes that the fgrp is locked
  54. */
  55. int
  56. findfreefd(Fgrp *f, int start)
  57. {
  58. int fd;
  59. for(fd=start; fd<f->nfd; fd++)
  60. if(f->fd[fd] == 0)
  61. break;
  62. if(fd >= f->nfd && growfd(f, fd) < 0)
  63. return -1;
  64. return fd;
  65. }
  66. int
  67. newfd(Chan *c)
  68. {
  69. int fd;
  70. Fgrp *f;
  71. f = up->fgrp;
  72. lock(f);
  73. fd = findfreefd(f, 0);
  74. if(fd < 0){
  75. unlockfgrp(f);
  76. return -1;
  77. }
  78. if(fd > f->maxfd)
  79. f->maxfd = fd;
  80. f->fd[fd] = c;
  81. unlockfgrp(f);
  82. return fd;
  83. }
  84. int
  85. newfd2(int fd[2], Chan *c[2])
  86. {
  87. Fgrp *f;
  88. f = up->fgrp;
  89. lock(f);
  90. fd[0] = findfreefd(f, 0);
  91. if(fd[0] < 0){
  92. unlockfgrp(f);
  93. return -1;
  94. }
  95. fd[1] = findfreefd(f, fd[0]+1);
  96. if(fd[1] < 0){
  97. unlockfgrp(f);
  98. return -1;
  99. }
  100. if(fd[1] > f->maxfd)
  101. f->maxfd = fd[1];
  102. f->fd[fd[0]] = c[0];
  103. f->fd[fd[1]] = c[1];
  104. unlockfgrp(f);
  105. return 0;
  106. }
  107. Chan*
  108. fdtochan(int fd, int mode, int chkmnt, int iref)
  109. {
  110. Chan *c;
  111. Fgrp *f;
  112. c = 0;
  113. f = up->fgrp;
  114. lock(f);
  115. if(fd<0 || f->nfd<=fd || (c = f->fd[fd])==0) {
  116. unlock(f);
  117. error(Ebadfd);
  118. }
  119. if(iref)
  120. incref(c);
  121. unlock(f);
  122. if(chkmnt && (c->flag&CMSG)) {
  123. if(iref)
  124. cclose(c);
  125. error(Ebadusefd);
  126. }
  127. if(mode<0 || c->mode==ORDWR)
  128. return c;
  129. if((mode&OTRUNC) && c->mode==OREAD) {
  130. if(iref)
  131. cclose(c);
  132. error(Ebadusefd);
  133. }
  134. if((mode&~OTRUNC) != c->mode) {
  135. if(iref)
  136. cclose(c);
  137. error(Ebadusefd);
  138. }
  139. return c;
  140. }
  141. int
  142. openmode(ulong o)
  143. {
  144. o &= ~(OTRUNC|OCEXEC|ORCLOSE);
  145. if(o > OEXEC)
  146. error(Ebadarg);
  147. if(o == OEXEC)
  148. return OREAD;
  149. return o;
  150. }
  151. long
  152. sysfd2path(ulong *arg)
  153. {
  154. Chan *c;
  155. validaddr(arg[1], arg[2], 1);
  156. c = fdtochan(arg[0], -1, 0, 1);
  157. snprint((char*)arg[1], arg[2], "%s", chanpath(c));
  158. cclose(c);
  159. return 0;
  160. }
  161. long
  162. syspipe(ulong *arg)
  163. {
  164. int fd[2];
  165. Chan *c[2];
  166. Dev *d;
  167. static char *datastr[] = {"data", "data1"};
  168. validaddr(arg[0], 2*BY2WD, 1);
  169. evenaddr(arg[0]);
  170. d = devtab[devno('|', 0)];
  171. c[0] = namec("#|", Atodir, 0, 0);
  172. c[1] = 0;
  173. fd[0] = -1;
  174. fd[1] = -1;
  175. if(waserror()){
  176. cclose(c[0]);
  177. if(c[1])
  178. cclose(c[1]);
  179. nexterror();
  180. }
  181. c[1] = cclone(c[0]);
  182. if(walk(&c[0], datastr+0, 1, 1, nil) < 0)
  183. error(Egreg);
  184. if(walk(&c[1], datastr+1, 1, 1, nil) < 0)
  185. error(Egreg);
  186. c[0] = d->open(c[0], ORDWR);
  187. c[1] = d->open(c[1], ORDWR);
  188. if(newfd2(fd, c) < 0)
  189. error(Enofd);
  190. poperror();
  191. ((long*)arg[0])[0] = fd[0];
  192. ((long*)arg[0])[1] = fd[1];
  193. return 0;
  194. }
  195. long
  196. sysdup(ulong *arg)
  197. {
  198. int fd;
  199. Chan *c, *oc;
  200. Fgrp *f = up->fgrp;
  201. /*
  202. * Close after dup'ing, so date > #d/1 works
  203. */
  204. c = fdtochan(arg[0], -1, 0, 1);
  205. fd = arg[1];
  206. if(fd != -1){
  207. lock(f);
  208. if(fd<0 || growfd(f, fd)<0) {
  209. unlockfgrp(f);
  210. cclose(c);
  211. error(Ebadfd);
  212. }
  213. if(fd > f->maxfd)
  214. f->maxfd = fd;
  215. oc = f->fd[fd];
  216. f->fd[fd] = c;
  217. unlockfgrp(f);
  218. if(oc)
  219. cclose(oc);
  220. }else{
  221. if(waserror()) {
  222. cclose(c);
  223. nexterror();
  224. }
  225. fd = newfd(c);
  226. if(fd < 0)
  227. error(Enofd);
  228. poperror();
  229. }
  230. return fd;
  231. }
  232. long
  233. sysopen(ulong *arg)
  234. {
  235. int fd;
  236. Chan *c;
  237. openmode(arg[1]); /* error check only */
  238. validaddr(arg[0], 1, 0);
  239. c = namec((char*)arg[0], Aopen, arg[1], 0);
  240. if(waserror()){
  241. cclose(c);
  242. nexterror();
  243. }
  244. fd = newfd(c);
  245. if(fd < 0)
  246. error(Enofd);
  247. poperror();
  248. return fd;
  249. }
  250. void
  251. fdclose(int fd, int flag)
  252. {
  253. int i;
  254. Chan *c;
  255. Fgrp *f = up->fgrp;
  256. lock(f);
  257. c = f->fd[fd];
  258. if(c == 0){
  259. /* can happen for users with shared fd tables */
  260. unlock(f);
  261. return;
  262. }
  263. if(flag){
  264. if(c==0 || !(c->flag&flag)){
  265. unlock(f);
  266. return;
  267. }
  268. }
  269. f->fd[fd] = 0;
  270. if(fd == f->maxfd)
  271. for(i=fd; --i>=0 && f->fd[i]==0; )
  272. f->maxfd = i;
  273. unlock(f);
  274. cclose(c);
  275. }
  276. long
  277. sysclose(ulong *arg)
  278. {
  279. fdtochan(arg[0], -1, 0, 0);
  280. fdclose(arg[0], 0);
  281. return 0;
  282. }
  283. long
  284. unionread(Chan *c, void *va, long n)
  285. {
  286. int i;
  287. long nr;
  288. Mhead *m;
  289. Mount *mount;
  290. qlock(&c->umqlock);
  291. m = c->umh;
  292. rlock(&m->lock);
  293. mount = m->mount;
  294. /* bring mount in sync with c->uri and c->umc */
  295. for(i = 0; mount != nil && i < c->uri; i++)
  296. mount = mount->next;
  297. nr = 0;
  298. while(mount != nil){
  299. /* Error causes component of union to be skipped */
  300. if(mount->to && !waserror()){
  301. if(c->umc == nil){
  302. c->umc = cclone(mount->to);
  303. c->umc = devtab[c->umc->type]->open(c->umc, OREAD);
  304. }
  305. nr = devtab[c->umc->type]->read(c->umc, va, n, c->umc->offset);
  306. c->umc->offset += nr;
  307. poperror();
  308. }
  309. if(nr > 0)
  310. break;
  311. /* Advance to next element */
  312. c->uri++;
  313. if(c->umc){
  314. cclose(c->umc);
  315. c->umc = nil;
  316. }
  317. mount = mount->next;
  318. }
  319. runlock(&m->lock);
  320. qunlock(&c->umqlock);
  321. return nr;
  322. }
  323. static void
  324. unionrewind(Chan *c)
  325. {
  326. qlock(&c->umqlock);
  327. c->uri = 0;
  328. if(c->umc){
  329. cclose(c->umc);
  330. c->umc = nil;
  331. }
  332. qunlock(&c->umqlock);
  333. }
  334. static int
  335. dirfixed(uchar *p, uchar *e, Dir *d)
  336. {
  337. int len;
  338. len = GBIT16(p)+BIT16SZ;
  339. if(p + len > e)
  340. return -1;
  341. p += BIT16SZ; /* ignore size */
  342. d->type = devno(GBIT16(p), 1);
  343. p += BIT16SZ;
  344. d->dev = GBIT32(p);
  345. p += BIT32SZ;
  346. d->qid.type = GBIT8(p);
  347. p += BIT8SZ;
  348. d->qid.vers = GBIT32(p);
  349. p += BIT32SZ;
  350. d->qid.path = GBIT64(p);
  351. p += BIT64SZ;
  352. d->mode = GBIT32(p);
  353. p += BIT32SZ;
  354. d->atime = GBIT32(p);
  355. p += BIT32SZ;
  356. d->mtime = GBIT32(p);
  357. p += BIT32SZ;
  358. d->length = GBIT64(p);
  359. return len;
  360. }
  361. static char*
  362. dirname(uchar *p, int *n)
  363. {
  364. p += BIT16SZ+BIT16SZ+BIT32SZ+BIT8SZ+BIT32SZ+BIT64SZ
  365. + BIT32SZ+BIT32SZ+BIT32SZ+BIT64SZ;
  366. *n = GBIT16(p);
  367. return (char*)p+BIT16SZ;
  368. }
  369. static long
  370. dirsetname(char *name, int len, uchar *p, long n, long maxn)
  371. {
  372. char *oname;
  373. int olen;
  374. long nn;
  375. if(n == BIT16SZ)
  376. return BIT16SZ;
  377. oname = dirname(p, &olen);
  378. nn = n+len-olen;
  379. PBIT16(p, nn-BIT16SZ);
  380. if(nn > maxn)
  381. return BIT16SZ;
  382. if(len != olen)
  383. memmove(oname+len, oname+olen, p+n-(uchar*)(oname+olen));
  384. PBIT16((uchar*)(oname-2), len);
  385. memmove(oname, name, len);
  386. return nn;
  387. }
  388. /*
  389. * Mountfix might have caused the fixed results of the directory read
  390. * to overflow the buffer. Catch the overflow in c->dirrock.
  391. */
  392. static void
  393. mountrock(Chan *c, uchar *p, uchar **pe)
  394. {
  395. uchar *e, *r;
  396. int len, n;
  397. e = *pe;
  398. /* find last directory entry */
  399. for(;;){
  400. len = BIT16SZ+GBIT16(p);
  401. if(p+len >= e)
  402. break;
  403. p += len;
  404. }
  405. /* save it away */
  406. qlock(&c->rockqlock);
  407. if(c->nrock+len > c->mrock){
  408. n = ROUND(c->nrock+len, 1024);
  409. r = smalloc(n);
  410. memmove(r, c->dirrock, c->nrock);
  411. free(c->dirrock);
  412. c->dirrock = r;
  413. c->mrock = n;
  414. }
  415. memmove(c->dirrock+c->nrock, p, len);
  416. c->nrock += len;
  417. qunlock(&c->rockqlock);
  418. /* drop it */
  419. *pe = p;
  420. }
  421. /*
  422. * Satisfy a directory read with the results saved in c->dirrock.
  423. */
  424. static int
  425. mountrockread(Chan *c, uchar *op, long n, long *nn)
  426. {
  427. long dirlen;
  428. uchar *rp, *erp, *ep, *p;
  429. /* common case */
  430. if(c->nrock == 0)
  431. return 0;
  432. /* copy out what we can */
  433. qlock(&c->rockqlock);
  434. rp = c->dirrock;
  435. erp = rp+c->nrock;
  436. p = op;
  437. ep = p+n;
  438. while(rp+BIT16SZ <= erp){
  439. dirlen = BIT16SZ+GBIT16(rp);
  440. if(p+dirlen > ep)
  441. break;
  442. memmove(p, rp, dirlen);
  443. p += dirlen;
  444. rp += dirlen;
  445. }
  446. if(p == op){
  447. qunlock(&c->rockqlock);
  448. return 0;
  449. }
  450. /* shift the rest */
  451. if(rp != erp)
  452. memmove(c->dirrock, rp, erp-rp);
  453. c->nrock = erp - rp;
  454. *nn = p - op;
  455. qunlock(&c->rockqlock);
  456. return 1;
  457. }
  458. static void
  459. mountrewind(Chan *c)
  460. {
  461. c->nrock = 0;
  462. }
  463. /*
  464. * Rewrite the results of a directory read to reflect current
  465. * name space bindings and mounts. Specifically, replace
  466. * directory entries for bind and mount points with the results
  467. * of statting what is mounted there. Except leave the old names.
  468. */
  469. static long
  470. mountfix(Chan *c, uchar *op, long n, long maxn)
  471. {
  472. char *name;
  473. int nbuf, nname;
  474. Chan *nc;
  475. Mhead *mh;
  476. Mount *m;
  477. uchar *p;
  478. int dirlen, rest;
  479. long l;
  480. uchar *buf, *e;
  481. Dir d;
  482. p = op;
  483. buf = nil;
  484. nbuf = 0;
  485. for(e=&p[n]; p+BIT16SZ<e; p+=dirlen){
  486. dirlen = dirfixed(p, e, &d);
  487. if(dirlen < 0)
  488. break;
  489. nc = nil;
  490. mh = nil;
  491. if(findmount(&nc, &mh, d.type, d.dev, d.qid)){
  492. /*
  493. * If it's a union directory and the original is
  494. * in the union, don't rewrite anything.
  495. */
  496. for(m=mh->mount; m; m=m->next)
  497. if(eqchantdqid(m->to, d.type, d.dev, d.qid, 1))
  498. goto Norewrite;
  499. name = dirname(p, &nname);
  500. /*
  501. * Do the stat but fix the name. If it fails, leave old entry.
  502. * BUG: If it fails because there isn't room for the entry,
  503. * what can we do? Nothing, really. Might as well skip it.
  504. */
  505. if(buf == nil){
  506. buf = smalloc(4096);
  507. nbuf = 4096;
  508. }
  509. if(waserror())
  510. goto Norewrite;
  511. l = devtab[nc->type]->stat(nc, buf, nbuf);
  512. l = dirsetname(name, nname, buf, l, nbuf);
  513. if(l == BIT16SZ)
  514. error("dirsetname");
  515. poperror();
  516. /*
  517. * Shift data in buffer to accomodate new entry,
  518. * possibly overflowing into rock.
  519. */
  520. rest = e - (p+dirlen);
  521. if(l > dirlen){
  522. while(p+l+rest > op+maxn){
  523. mountrock(c, p, &e);
  524. if(e == p){
  525. dirlen = 0;
  526. goto Norewrite;
  527. }
  528. rest = e - (p+dirlen);
  529. }
  530. }
  531. if(l != dirlen){
  532. memmove(p+l, p+dirlen, rest);
  533. dirlen = l;
  534. e = p+dirlen+rest;
  535. }
  536. /*
  537. * Rewrite directory entry.
  538. */
  539. memmove(p, buf, l);
  540. Norewrite:
  541. cclose(nc);
  542. putmhead(mh);
  543. }
  544. }
  545. if(buf)
  546. free(buf);
  547. if(p != e)
  548. error("oops in rockfix");
  549. return e-op;
  550. }
  551. static long
  552. read(ulong *arg, vlong *offp)
  553. {
  554. long n, nn, nnn;
  555. uchar *p;
  556. Chan *c;
  557. vlong off;
  558. n = arg[2];
  559. validaddr(arg[1], n, 1);
  560. p = (void*)arg[1];
  561. c = fdtochan(arg[0], OREAD, 1, 1);
  562. if(waserror()){
  563. cclose(c);
  564. nexterror();
  565. }
  566. /*
  567. * The offset is passed through on directories, normally.
  568. * Sysseek complains, but pread is used by servers like exportfs,
  569. * that shouldn't need to worry about this issue.
  570. *
  571. * Notice that c->devoffset is the offset that c's dev is seeing.
  572. * The number of bytes read on this fd (c->offset) may be different
  573. * due to rewritings in rockfix.
  574. */
  575. if(offp == nil) /* use and maintain channel's offset */
  576. off = c->offset;
  577. else
  578. off = *offp;
  579. if(off < 0)
  580. error(Enegoff);
  581. if(off == 0){ /* rewind to the beginning of the directory */
  582. if(offp == nil){
  583. c->offset = 0;
  584. c->devoffset = 0;
  585. }
  586. mountrewind(c);
  587. unionrewind(c);
  588. }
  589. if(c->qid.type & QTDIR){
  590. if(mountrockread(c, p, n, &nn)){
  591. /* do nothing: mountrockread filled buffer */
  592. }else if(c->umh)
  593. nn = unionread(c, p, n);
  594. else{
  595. if(off != c->offset)
  596. error(Edirseek);
  597. nn = devtab[c->type]->read(c, p, n, c->devoffset);
  598. }
  599. nnn = mountfix(c, p, nn, n);
  600. }else
  601. nnn = nn = devtab[c->type]->read(c, p, n, off);
  602. lock(c);
  603. c->devoffset += nn;
  604. c->offset += nnn;
  605. unlock(c);
  606. poperror();
  607. cclose(c);
  608. return nnn;
  609. }
  610. long
  611. sys_read(ulong *arg)
  612. {
  613. return read(arg, nil);
  614. }
  615. long
  616. syspread(ulong *arg)
  617. {
  618. vlong v;
  619. va_list list;
  620. /* use varargs to guarantee alignment of vlong */
  621. va_start(list, arg[2]);
  622. v = va_arg(list, vlong);
  623. va_end(list);
  624. if(v == ~0ULL)
  625. return read(arg, nil);
  626. return read(arg, &v);
  627. }
  628. static long
  629. write(ulong *arg, vlong *offp)
  630. {
  631. Chan *c;
  632. long m, n;
  633. vlong off;
  634. validaddr(arg[1], arg[2], 0);
  635. n = 0;
  636. c = fdtochan(arg[0], OWRITE, 1, 1);
  637. if(waserror()) {
  638. if(offp == nil){
  639. lock(c);
  640. c->offset -= n;
  641. unlock(c);
  642. }
  643. cclose(c);
  644. nexterror();
  645. }
  646. if(c->qid.type & QTDIR)
  647. error(Eisdir);
  648. n = arg[2];
  649. if(offp == nil){ /* use and maintain channel's offset */
  650. lock(c);
  651. off = c->offset;
  652. c->offset += n;
  653. unlock(c);
  654. }else
  655. off = *offp;
  656. if(off < 0)
  657. error(Enegoff);
  658. m = devtab[c->type]->write(c, (void*)arg[1], n, off);
  659. if(offp == nil && m < n){
  660. lock(c);
  661. c->offset -= n - m;
  662. unlock(c);
  663. }
  664. poperror();
  665. cclose(c);
  666. return m;
  667. }
  668. long
  669. sys_write(ulong *arg)
  670. {
  671. return write(arg, nil);
  672. }
  673. long
  674. syspwrite(ulong *arg)
  675. {
  676. vlong v;
  677. va_list list;
  678. /* use varargs to guarantee alignment of vlong */
  679. va_start(list, arg[2]);
  680. v = va_arg(list, vlong);
  681. va_end(list);
  682. if(v == ~0ULL)
  683. return write(arg, nil);
  684. return write(arg, &v);
  685. }
  686. static void
  687. sseek(ulong *arg)
  688. {
  689. Chan *c;
  690. uchar buf[sizeof(Dir)+100];
  691. Dir dir;
  692. int n;
  693. vlong off;
  694. union {
  695. vlong v;
  696. ulong u[2];
  697. } o;
  698. c = fdtochan(arg[1], -1, 1, 1);
  699. if(waserror()){
  700. cclose(c);
  701. nexterror();
  702. }
  703. if(devtab[c->type]->dc == '|')
  704. error(Eisstream);
  705. off = 0;
  706. o.u[0] = arg[2];
  707. o.u[1] = arg[3];
  708. switch(arg[4]){
  709. case 0:
  710. off = o.v;
  711. if((c->qid.type & QTDIR) && off != 0)
  712. error(Eisdir);
  713. if(off < 0)
  714. error(Enegoff);
  715. c->offset = off;
  716. break;
  717. case 1:
  718. if(c->qid.type & QTDIR)
  719. error(Eisdir);
  720. lock(c); /* lock for read/write update */
  721. off = o.v + c->offset;
  722. if(off < 0){
  723. unlock(c);
  724. error(Enegoff);
  725. }
  726. c->offset = off;
  727. unlock(c);
  728. break;
  729. case 2:
  730. if(c->qid.type & QTDIR)
  731. error(Eisdir);
  732. n = devtab[c->type]->stat(c, buf, sizeof buf);
  733. if(convM2D(buf, n, &dir, nil) == 0)
  734. error("internal error: stat error in seek");
  735. off = dir.length + o.v;
  736. if(off < 0)
  737. error(Enegoff);
  738. c->offset = off;
  739. break;
  740. default:
  741. error(Ebadarg);
  742. }
  743. *(vlong*)arg[0] = off;
  744. c->uri = 0;
  745. c->dri = 0;
  746. cclose(c);
  747. poperror();
  748. }
  749. long
  750. sysseek(ulong *arg)
  751. {
  752. validaddr(arg[0], BY2V, 1);
  753. sseek(arg);
  754. return 0;
  755. }
  756. long
  757. sysoseek(ulong *arg)
  758. {
  759. union {
  760. vlong v;
  761. ulong u[2];
  762. } o;
  763. ulong a[5];
  764. o.v = (long)arg[1];
  765. a[0] = (ulong)&o.v;
  766. a[1] = arg[0];
  767. a[2] = o.u[0];
  768. a[3] = o.u[1];
  769. a[4] = arg[2];
  770. sseek(a);
  771. return o.v;
  772. }
  773. void
  774. validstat(uchar *s, int n)
  775. {
  776. int m;
  777. char buf[64];
  778. if(statcheck(s, n) < 0)
  779. error(Ebadstat);
  780. /* verify that name entry is acceptable */
  781. s += STATFIXLEN - 4*BIT16SZ; /* location of first string */
  782. /*
  783. * s now points at count for first string.
  784. * if it's too long, let the server decide; this is
  785. * only for his protection anyway. otherwise
  786. * we'd have to allocate and waserror.
  787. */
  788. m = GBIT16(s);
  789. s += BIT16SZ;
  790. if(m+1 > sizeof buf)
  791. return;
  792. memmove(buf, s, m);
  793. buf[m] = '\0';
  794. /* name could be '/' */
  795. if(strcmp(buf, "/") != 0)
  796. validname(buf, 0);
  797. }
  798. static char*
  799. pathlast(Path *p)
  800. {
  801. char *s;
  802. if(p == nil)
  803. return nil;
  804. if(p->len == 0)
  805. return nil;
  806. s = strrchr(p->s, '/');
  807. if(s)
  808. return s+1;
  809. return p->s;
  810. }
  811. long
  812. sysfstat(ulong *arg)
  813. {
  814. Chan *c;
  815. uint l;
  816. l = arg[2];
  817. validaddr(arg[1], l, 1);
  818. c = fdtochan(arg[0], -1, 0, 1);
  819. if(waserror()) {
  820. cclose(c);
  821. nexterror();
  822. }
  823. l = devtab[c->type]->stat(c, (uchar*)arg[1], l);
  824. poperror();
  825. cclose(c);
  826. return l;
  827. }
  828. long
  829. sysstat(ulong *arg)
  830. {
  831. char *name;
  832. Chan *c;
  833. uint l;
  834. l = arg[2];
  835. validaddr(arg[1], l, 1);
  836. validaddr(arg[0], 1, 0);
  837. c = namec((char*)arg[0], Aaccess, 0, 0);
  838. if(waserror()){
  839. cclose(c);
  840. nexterror();
  841. }
  842. l = devtab[c->type]->stat(c, (uchar*)arg[1], l);
  843. name = pathlast(c->path);
  844. if(name)
  845. l = dirsetname(name, strlen(name), (uchar*)arg[1], l, arg[2]);
  846. poperror();
  847. cclose(c);
  848. return l;
  849. }
  850. long
  851. syschdir(ulong *arg)
  852. {
  853. Chan *c;
  854. validaddr(arg[0], 1, 0);
  855. c = namec((char*)arg[0], Atodir, 0, 0);
  856. cclose(up->dot);
  857. up->dot = c;
  858. return 0;
  859. }
  860. long
  861. bindmount(int ismount, int fd, int afd, char* arg0, char* arg1, ulong flag, char* spec)
  862. {
  863. int ret;
  864. Chan *c0, *c1, *ac, *bc;
  865. struct{
  866. Chan *chan;
  867. Chan *authchan;
  868. char *spec;
  869. int flags;
  870. }bogus;
  871. if((flag&~MMASK) || (flag&MORDER)==(MBEFORE|MAFTER))
  872. error(Ebadarg);
  873. if(ismount){
  874. validaddr((ulong)spec, 1, 0);
  875. spec = validnamedup(spec, 1);
  876. if(waserror()){
  877. free(spec);
  878. nexterror();
  879. }
  880. if(up->pgrp->noattach)
  881. error(Enoattach);
  882. ac = nil;
  883. bc = fdtochan(fd, ORDWR, 0, 1);
  884. if(waserror()) {
  885. if(ac)
  886. cclose(ac);
  887. cclose(bc);
  888. nexterror();
  889. }
  890. if(afd >= 0)
  891. ac = fdtochan(afd, ORDWR, 0, 1);
  892. bogus.flags = flag & MCACHE;
  893. bogus.chan = bc;
  894. bogus.authchan = ac;
  895. bogus.spec = spec;
  896. ret = devno('M', 0);
  897. c0 = devtab[ret]->attach((char*)&bogus);
  898. poperror(); /* ac bc */
  899. if(ac)
  900. cclose(ac);
  901. cclose(bc);
  902. }else{
  903. spec = 0;
  904. validaddr((ulong)arg0, 1, 0);
  905. c0 = namec(arg0, Abind, 0, 0);
  906. }
  907. if(waserror()){
  908. cclose(c0);
  909. nexterror();
  910. }
  911. validaddr((ulong)arg1, 1, 0);
  912. c1 = namec(arg1, Amount, 0, 0);
  913. if(waserror()){
  914. cclose(c1);
  915. nexterror();
  916. }
  917. ret = cmount(&c0, c1, flag, spec);
  918. poperror();
  919. cclose(c1);
  920. poperror();
  921. cclose(c0);
  922. if(ismount){
  923. fdclose(fd, 0);
  924. poperror();
  925. free(spec);
  926. }
  927. return ret;
  928. }
  929. long
  930. sysbind(ulong *arg)
  931. {
  932. return bindmount(0, -1, -1, (char*)arg[0], (char*)arg[1], arg[2], nil);
  933. }
  934. long
  935. sysmount(ulong *arg)
  936. {
  937. return bindmount(1, arg[0], arg[1], nil, (char*)arg[2], arg[3], (char*)arg[4]);
  938. }
  939. long
  940. sys_mount(ulong *arg)
  941. {
  942. return bindmount(1, arg[0], -1, nil, (char*)arg[1], arg[2], (char*)arg[3]);
  943. }
  944. long
  945. sysunmount(ulong *arg)
  946. {
  947. Chan *cmount, *cmounted;
  948. cmounted = 0;
  949. validaddr(arg[1], 1, 0);
  950. cmount = namec((char *)arg[1], Amount, 0, 0);
  951. if(waserror()) {
  952. cclose(cmount);
  953. if(cmounted)
  954. cclose(cmounted);
  955. nexterror();
  956. }
  957. if(arg[0]) {
  958. /*
  959. * This has to be namec(..., Aopen, ...) because
  960. * if arg[0] is something like /srv/cs or /fd/0,
  961. * opening it is the only way to get at the real
  962. * Chan underneath.
  963. */
  964. validaddr(arg[0], 1, 0);
  965. cmounted = namec((char*)arg[0], Aopen, OREAD, 0);
  966. }
  967. cunmount(cmount, cmounted);
  968. poperror();
  969. cclose(cmount);
  970. if(cmounted)
  971. cclose(cmounted);
  972. return 0;
  973. }
  974. long
  975. syscreate(ulong *arg)
  976. {
  977. int fd;
  978. Chan *c;
  979. openmode(arg[1]&~OEXCL); /* error check only; OEXCL okay here */
  980. validaddr(arg[0], 1, 0);
  981. c = namec((char*)arg[0], Acreate, arg[1], arg[2]);
  982. if(waserror()) {
  983. cclose(c);
  984. nexterror();
  985. }
  986. fd = newfd(c);
  987. if(fd < 0)
  988. error(Enofd);
  989. poperror();
  990. return fd;
  991. }
  992. long
  993. sysremove(ulong *arg)
  994. {
  995. Chan *c;
  996. validaddr(arg[0], 1, 0);
  997. c = namec((char*)arg[0], Aremove, 0, 0);
  998. /*
  999. * Removing mount points is disallowed to avoid surprises
  1000. * (which should be removed: the mount point or the mounted Chan?).
  1001. */
  1002. if(c->ismtpt){
  1003. cclose(c);
  1004. error(Eismtpt);
  1005. }
  1006. if(waserror()){
  1007. c->type = 0; /* see below */
  1008. cclose(c);
  1009. nexterror();
  1010. }
  1011. devtab[c->type]->remove(c);
  1012. /*
  1013. * Remove clunks the fid, but we need to recover the Chan
  1014. * so fake it up. rootclose() is known to be a nop.
  1015. */
  1016. c->type = 0;
  1017. poperror();
  1018. cclose(c);
  1019. return 0;
  1020. }
  1021. static long
  1022. wstat(Chan *c, uchar *d, int nd)
  1023. {
  1024. long l;
  1025. int namelen;
  1026. if(waserror()){
  1027. cclose(c);
  1028. nexterror();
  1029. }
  1030. if(c->ismtpt){
  1031. /*
  1032. * Renaming mount points is disallowed to avoid surprises
  1033. * (which should be renamed? the mount point or the mounted Chan?).
  1034. */
  1035. dirname(d, &namelen);
  1036. if(namelen)
  1037. nameerror(chanpath(c), Eismtpt);
  1038. }
  1039. l = devtab[c->type]->wstat(c, d, nd);
  1040. poperror();
  1041. cclose(c);
  1042. return l;
  1043. }
  1044. long
  1045. syswstat(ulong *arg)
  1046. {
  1047. Chan *c;
  1048. uint l;
  1049. l = arg[2];
  1050. validaddr(arg[1], l, 0);
  1051. validstat((uchar*)arg[1], l);
  1052. validaddr(arg[0], 1, 0);
  1053. c = namec((char*)arg[0], Aaccess, 0, 0);
  1054. return wstat(c, (uchar*)arg[1], l);
  1055. }
  1056. long
  1057. sysfwstat(ulong *arg)
  1058. {
  1059. Chan *c;
  1060. uint l;
  1061. l = arg[2];
  1062. validaddr(arg[1], l, 0);
  1063. validstat((uchar*)arg[1], l);
  1064. c = fdtochan(arg[0], -1, 1, 1);
  1065. return wstat(c, (uchar*)arg[1], l);
  1066. }
  1067. static void
  1068. packoldstat(uchar *buf, Dir *d)
  1069. {
  1070. uchar *p;
  1071. ulong q;
  1072. /* lay down old stat buffer - grotty code but it's temporary */
  1073. p = buf;
  1074. strncpy((char*)p, d->name, 28);
  1075. p += 28;
  1076. strncpy((char*)p, d->uid, 28);
  1077. p += 28;
  1078. strncpy((char*)p, d->gid, 28);
  1079. p += 28;
  1080. q = d->qid.path & ~DMDIR; /* make sure doesn't accidentally look like directory */
  1081. if(d->qid.type & QTDIR) /* this is the real test of a new directory */
  1082. q |= DMDIR;
  1083. PBIT32(p, q);
  1084. p += BIT32SZ;
  1085. PBIT32(p, d->qid.vers);
  1086. p += BIT32SZ;
  1087. PBIT32(p, d->mode);
  1088. p += BIT32SZ;
  1089. PBIT32(p, d->atime);
  1090. p += BIT32SZ;
  1091. PBIT32(p, d->mtime);
  1092. p += BIT32SZ;
  1093. PBIT64(p, d->length);
  1094. p += BIT64SZ;
  1095. PBIT16(p, d->type);
  1096. p += BIT16SZ;
  1097. PBIT16(p, d->dev);
  1098. }
  1099. long
  1100. sys_stat(ulong *arg)
  1101. {
  1102. Chan *c;
  1103. uint l;
  1104. uchar buf[128]; /* old DIRLEN plus a little should be plenty */
  1105. char strs[128], *name;
  1106. Dir d;
  1107. char old[] = "old stat system call - recompile";
  1108. validaddr(arg[1], 116, 1);
  1109. validaddr(arg[0], 1, 0);
  1110. c = namec((char*)arg[0], Aaccess, 0, 0);
  1111. if(waserror()){
  1112. cclose(c);
  1113. nexterror();
  1114. }
  1115. l = devtab[c->type]->stat(c, buf, sizeof buf);
  1116. /* buf contains a new stat buf; convert to old. yuck. */
  1117. if(l <= BIT16SZ) /* buffer too small; time to face reality */
  1118. error(old);
  1119. name = pathlast(c->path);
  1120. if(name)
  1121. l = dirsetname(name, strlen(name), buf, l, sizeof buf);
  1122. l = convM2D(buf, l, &d, strs);
  1123. if(l == 0)
  1124. error(old);
  1125. packoldstat((uchar*)arg[1], &d);
  1126. poperror();
  1127. cclose(c);
  1128. return 0;
  1129. }
  1130. long
  1131. sys_fstat(ulong *arg)
  1132. {
  1133. Chan *c;
  1134. char *name;
  1135. uint l;
  1136. uchar buf[128]; /* old DIRLEN plus a little should be plenty */
  1137. char strs[128];
  1138. Dir d;
  1139. char old[] = "old fstat system call - recompile";
  1140. validaddr(arg[1], 116, 1);
  1141. c = fdtochan(arg[0], -1, 0, 1);
  1142. if(waserror()){
  1143. cclose(c);
  1144. nexterror();
  1145. }
  1146. l = devtab[c->type]->stat(c, buf, sizeof buf);
  1147. /* buf contains a new stat buf; convert to old. yuck. */
  1148. if(l <= BIT16SZ) /* buffer too small; time to face reality */
  1149. error(old);
  1150. name = pathlast(c->path);
  1151. if(name)
  1152. l = dirsetname(name, strlen(name), buf, l, sizeof buf);
  1153. l = convM2D(buf, l, &d, strs);
  1154. if(l == 0)
  1155. error(old);
  1156. packoldstat((uchar*)arg[1], &d);
  1157. poperror();
  1158. cclose(c);
  1159. return 0;
  1160. }
  1161. long
  1162. sys_wstat(ulong *)
  1163. {
  1164. error("old wstat system call - recompile");
  1165. return -1;
  1166. }
  1167. long
  1168. sys_fwstat(ulong *)
  1169. {
  1170. error("old fwstat system call - recompile");
  1171. return -1;
  1172. }