sysfile.c 22 KB

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