sysfile.c 27 KB

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