sysfile.c 22 KB

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