sysfile.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  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. free(spec);
  905. poperror(); /* ac bc */
  906. if(ac)
  907. cclose(ac);
  908. cclose(bc);
  909. }else{
  910. bogus.spec = 0;
  911. validaddr((ulong)arg0, 1, 0);
  912. c0 = namec(arg0, Abind, 0, 0);
  913. }
  914. if(waserror()){
  915. cclose(c0);
  916. nexterror();
  917. }
  918. validaddr((ulong)arg1, 1, 0);
  919. c1 = namec(arg1, Amount, 0, 0);
  920. if(waserror()){
  921. cclose(c1);
  922. nexterror();
  923. }
  924. ret = cmount(&c0, c1, flag, bogus.spec);
  925. poperror();
  926. cclose(c1);
  927. poperror();
  928. cclose(c0);
  929. if(ismount)
  930. fdclose(fd, 0);
  931. return ret;
  932. }
  933. long
  934. sysbind(ulong *arg)
  935. {
  936. return bindmount(0, -1, -1, (char*)arg[0], (char*)arg[1], arg[2], nil);
  937. }
  938. long
  939. sysmount(ulong *arg)
  940. {
  941. return bindmount(1, arg[0], arg[1], nil, (char*)arg[2], arg[3], (char*)arg[4]);
  942. }
  943. long
  944. sys_mount(ulong *arg)
  945. {
  946. return bindmount(1, arg[0], -1, nil, (char*)arg[1], arg[2], (char*)arg[3]);
  947. }
  948. long
  949. sysunmount(ulong *arg)
  950. {
  951. Chan *cmount, *cmounted;
  952. cmounted = 0;
  953. validaddr(arg[1], 1, 0);
  954. cmount = namec((char *)arg[1], Amount, 0, 0);
  955. if(arg[0]) {
  956. if(waserror()) {
  957. cclose(cmount);
  958. nexterror();
  959. }
  960. validaddr(arg[0], 1, 0);
  961. /*
  962. * This has to be namec(..., Aopen, ...) because
  963. * if arg[0] is something like /srv/cs or /fd/0,
  964. * opening it is the only way to get at the real
  965. * Chan underneath.
  966. */
  967. cmounted = namec((char*)arg[0], Aopen, OREAD, 0);
  968. poperror();
  969. }
  970. if(waserror()) {
  971. cclose(cmount);
  972. if(cmounted)
  973. cclose(cmounted);
  974. nexterror();
  975. }
  976. cunmount(cmount, cmounted);
  977. cclose(cmount);
  978. if(cmounted)
  979. cclose(cmounted);
  980. poperror();
  981. return 0;
  982. }
  983. long
  984. syscreate(ulong *arg)
  985. {
  986. int fd;
  987. Chan *c = 0;
  988. openmode(arg[1]&~OEXCL); /* error check only; OEXCL okay here */
  989. if(waserror()) {
  990. if(c)
  991. cclose(c);
  992. nexterror();
  993. }
  994. validaddr(arg[0], 1, 0);
  995. c = namec((char*)arg[0], Acreate, arg[1], arg[2]);
  996. fd = newfd(c);
  997. if(fd < 0)
  998. error(Enofd);
  999. poperror();
  1000. return fd;
  1001. }
  1002. long
  1003. sysremove(ulong *arg)
  1004. {
  1005. Chan *c;
  1006. validaddr(arg[0], 1, 0);
  1007. c = namec((char*)arg[0], Aremove, 0, 0);
  1008. /*
  1009. * Removing mount points is disallowed to avoid surprises
  1010. * (which should be removed: the mount point or the mounted Chan?).
  1011. */
  1012. if(c->ismtpt){
  1013. cclose(c);
  1014. error(Eismtpt);
  1015. }
  1016. if(waserror()){
  1017. c->type = 0; /* see below */
  1018. cclose(c);
  1019. nexterror();
  1020. }
  1021. devtab[c->type]->remove(c);
  1022. /*
  1023. * Remove clunks the fid, but we need to recover the Chan
  1024. * so fake it up. rootclose() is known to be a nop.
  1025. */
  1026. c->type = 0;
  1027. poperror();
  1028. cclose(c);
  1029. return 0;
  1030. }
  1031. static long
  1032. wstat(Chan *c, uchar *d, int nd)
  1033. {
  1034. long l;
  1035. int namelen;
  1036. if(waserror()){
  1037. cclose(c);
  1038. nexterror();
  1039. }
  1040. if(c->ismtpt){
  1041. /*
  1042. * Renaming mount points is disallowed to avoid surprises
  1043. * (which should be renamed? the mount point or the mounted Chan?).
  1044. */
  1045. dirname(d, &namelen);
  1046. if(namelen)
  1047. nameerror(chanpath(c), Eismtpt);
  1048. }
  1049. l = devtab[c->type]->wstat(c, d, nd);
  1050. poperror();
  1051. cclose(c);
  1052. return l;
  1053. }
  1054. long
  1055. syswstat(ulong *arg)
  1056. {
  1057. Chan *c;
  1058. uint l;
  1059. l = arg[2];
  1060. validaddr(arg[1], l, 0);
  1061. validstat((uchar*)arg[1], l);
  1062. validaddr(arg[0], 1, 0);
  1063. c = namec((char*)arg[0], Aaccess, 0, 0);
  1064. return wstat(c, (uchar*)arg[1], l);
  1065. }
  1066. long
  1067. sysfwstat(ulong *arg)
  1068. {
  1069. Chan *c;
  1070. uint l;
  1071. l = arg[2];
  1072. validaddr(arg[1], l, 0);
  1073. validstat((uchar*)arg[1], l);
  1074. c = fdtochan(arg[0], -1, 1, 1);
  1075. return wstat(c, (uchar*)arg[1], l);
  1076. }
  1077. static void
  1078. packoldstat(uchar *buf, Dir *d)
  1079. {
  1080. uchar *p;
  1081. ulong q;
  1082. /* lay down old stat buffer - grotty code but it's temporary */
  1083. p = buf;
  1084. strncpy((char*)p, d->name, 28);
  1085. p += 28;
  1086. strncpy((char*)p, d->uid, 28);
  1087. p += 28;
  1088. strncpy((char*)p, d->gid, 28);
  1089. p += 28;
  1090. q = d->qid.path & ~DMDIR; /* make sure doesn't accidentally look like directory */
  1091. if(d->qid.type & QTDIR) /* this is the real test of a new directory */
  1092. q |= DMDIR;
  1093. PBIT32(p, q);
  1094. p += BIT32SZ;
  1095. PBIT32(p, d->qid.vers);
  1096. p += BIT32SZ;
  1097. PBIT32(p, d->mode);
  1098. p += BIT32SZ;
  1099. PBIT32(p, d->atime);
  1100. p += BIT32SZ;
  1101. PBIT32(p, d->mtime);
  1102. p += BIT32SZ;
  1103. PBIT64(p, d->length);
  1104. p += BIT64SZ;
  1105. PBIT16(p, d->type);
  1106. p += BIT16SZ;
  1107. PBIT16(p, d->dev);
  1108. }
  1109. long
  1110. sys_stat(ulong *arg)
  1111. {
  1112. Chan *c;
  1113. uint l;
  1114. uchar buf[128]; /* old DIRLEN plus a little should be plenty */
  1115. char strs[128], *name;
  1116. Dir d;
  1117. char old[] = "old stat system call - recompile";
  1118. validaddr(arg[1], 116, 1);
  1119. validaddr(arg[0], 1, 0);
  1120. c = namec((char*)arg[0], Aaccess, 0, 0);
  1121. if(waserror()){
  1122. cclose(c);
  1123. nexterror();
  1124. }
  1125. l = devtab[c->type]->stat(c, buf, sizeof buf);
  1126. /* buf contains a new stat buf; convert to old. yuck. */
  1127. if(l <= BIT16SZ) /* buffer too small; time to face reality */
  1128. error(old);
  1129. name = pathlast(c->path);
  1130. if(name)
  1131. l = dirsetname(name, strlen(name), buf, l, sizeof buf);
  1132. l = convM2D(buf, l, &d, strs);
  1133. if(l == 0)
  1134. error(old);
  1135. packoldstat((uchar*)arg[1], &d);
  1136. poperror();
  1137. cclose(c);
  1138. return 0;
  1139. }
  1140. long
  1141. sys_fstat(ulong *arg)
  1142. {
  1143. Chan *c;
  1144. char *name;
  1145. uint l;
  1146. uchar buf[128]; /* old DIRLEN plus a little should be plenty */
  1147. char strs[128];
  1148. Dir d;
  1149. char old[] = "old fstat system call - recompile";
  1150. validaddr(arg[1], 116, 1);
  1151. c = fdtochan(arg[0], -1, 0, 1);
  1152. if(waserror()){
  1153. cclose(c);
  1154. nexterror();
  1155. }
  1156. l = devtab[c->type]->stat(c, buf, sizeof buf);
  1157. /* buf contains a new stat buf; convert to old. yuck. */
  1158. if(l <= BIT16SZ) /* buffer too small; time to face reality */
  1159. error(old);
  1160. name = pathlast(c->path);
  1161. if(name)
  1162. l = dirsetname(name, strlen(name), buf, l, sizeof buf);
  1163. l = convM2D(buf, l, &d, strs);
  1164. if(l == 0)
  1165. error(old);
  1166. packoldstat((uchar*)arg[1], &d);
  1167. poperror();
  1168. cclose(c);
  1169. return 0;
  1170. }
  1171. long
  1172. sys_wstat(ulong *)
  1173. {
  1174. error("old wstat system call - recompile");
  1175. return -1;
  1176. }
  1177. long
  1178. sys_fwstat(ulong *)
  1179. {
  1180. error("old fwstat system call - recompile");
  1181. return -1;
  1182. }