9p2.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874
  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 "all.h"
  10. #define MSIZE (MAXDAT+128)
  11. static void
  12. seterror(Fcall *ou, int err)
  13. {
  14. if(0 <= err && err < MAXERR)
  15. ou->ename = errstring[err];
  16. else
  17. ou->ename = "unknown error";
  18. }
  19. static int
  20. fsversion(Chan* chan, Fcall* f, Fcall* r)
  21. {
  22. if(f->msize < MSIZE)
  23. r->msize = f->msize;
  24. else
  25. r->msize = MSIZE;
  26. /*
  27. * Should check the '.' stuff here.
  28. * What happens if Tversion has already been seen?
  29. */
  30. if(strcmp(f->version, VERSION9P) == 0){
  31. r->version = VERSION9P;
  32. chan->msize = r->msize;
  33. }else
  34. r->version = "unknown";
  35. fileinit(chan);
  36. return 0;
  37. }
  38. char *keyspec = "proto=p9any role=server";
  39. static int
  40. fsauth(Chan *chan, Fcall *f, Fcall *r)
  41. {
  42. int err, fd;
  43. char *aname;
  44. File *file;
  45. int afd;
  46. AuthRpc *rpc;
  47. err = 0;
  48. if(chan == cons.srvchan)
  49. return Eauthmsg;
  50. file = filep(chan, f->afid, 1);
  51. if(file == nil)
  52. return Efidinuse;
  53. /* forget any previous authentication */
  54. file->cuid = 0;
  55. if(access("/mnt/factotum", 0) < 0)
  56. if((fd = open("/srv/factotum", ORDWR)) >= 0)
  57. mount(fd, -1, "/mnt", MBEFORE, "", 'M');
  58. afd = open("/mnt/factotum/rpc", ORDWR);
  59. if(afd < 0){
  60. err = Esystem;
  61. goto out;
  62. }
  63. rpc = auth_allocrpc(afd);
  64. if(rpc == nil){
  65. close(afd);
  66. err = Esystem;
  67. goto out;
  68. }
  69. file->rpc = rpc;
  70. if(auth_rpc(rpc, "start", keyspec, strlen(keyspec)) != ARok){
  71. err = Esystem;
  72. goto out;
  73. }
  74. aname = f->aname;
  75. if(!aname[0])
  76. aname = "main";
  77. file->fs = fsstr(aname);
  78. if(file->fs == nil){
  79. err = Ebadspc;
  80. goto out;
  81. }
  82. file->uid = strtouid(f->uname);
  83. if(file->uid < 0){
  84. err = Ebadu;
  85. goto out;
  86. }
  87. file->qid.path = 0;
  88. file->qid.vers = 0;
  89. file->qid.type = QTAUTH;
  90. r->qid = file->qid;
  91. out:
  92. if(file != nil){
  93. qunlock(file);
  94. if(err != 0)
  95. freefp(file);
  96. }
  97. return err;
  98. }
  99. int
  100. authread(File *file, uint8_t *data, int count)
  101. {
  102. AuthInfo *ai;
  103. AuthRpc *rpc;
  104. int rv;
  105. rpc = file->rpc;
  106. if(rpc == nil)
  107. return -1;
  108. rv = auth_rpc(rpc, "read", nil, 0);
  109. switch(rv){
  110. case ARdone:
  111. ai = auth_getinfo(rpc);
  112. if(ai == nil)
  113. return -1;
  114. if(chat)
  115. print("authread identifies user as %s\n", ai->cuid);
  116. file->cuid = strtouid(ai->cuid);
  117. auth_freeAI(ai);
  118. if(file->cuid == 0)
  119. return -1;
  120. if(chat)
  121. print("%s is a known user\n", ai->cuid);
  122. return 0;
  123. case ARok:
  124. if(count < rpc->narg)
  125. return -1;
  126. memmove(data, rpc->arg, rpc->narg);
  127. return rpc->narg;
  128. case ARphase:
  129. return -1;
  130. default:
  131. return -1;
  132. }
  133. }
  134. int
  135. authwrite(File *file, uint8_t *data, int count)
  136. {
  137. int ret;
  138. ret = auth_rpc(file->rpc, "write", data, count);
  139. if(ret != ARok)
  140. return -1;
  141. return count;
  142. }
  143. void
  144. mkqid9p1(Qid9p1* qid9p1, Qid* qid)
  145. {
  146. if(qid->path & 0xFFFFFFFF00000000LL)
  147. panic("mkqid9p1: path %lluX\n", qid->path);
  148. qid9p1->path = qid->path & 0xFFFFFFFF;
  149. if(qid->type & QTDIR)
  150. qid9p1->path |= QPDIR;
  151. qid9p1->version = qid->vers;
  152. }
  153. void
  154. authfree(File *fp)
  155. {
  156. if(fp->rpc != nil){
  157. close(fp->rpc->afd);
  158. free(fp->rpc);
  159. fp->rpc = nil;
  160. }
  161. }
  162. void
  163. mkqid9p2(Qid* qid, Qid9p1* qid9p1, int mode)
  164. {
  165. qid->path = (uint32_t)(qid9p1->path & ~QPDIR);
  166. qid->vers = qid9p1->version;
  167. qid->type = 0;
  168. if(mode & DDIR)
  169. qid->type |= QTDIR;
  170. if(mode & DAPND)
  171. qid->type |= QTAPPEND;
  172. if(mode & DLOCK)
  173. qid->type |= QTEXCL;
  174. }
  175. static int
  176. checkattach(Chan *chan, File *afile, File *file, Filsys *fs)
  177. {
  178. uint8_t buf[1];
  179. if(chan == cons.srvchan || chan == cons.chan)
  180. return 0;
  181. /* if no afile, this had better be none */
  182. if(afile == nil){
  183. if(file->uid == 0){
  184. if(!allownone && !chan->authed)
  185. return Eauth;
  186. return 0;
  187. }
  188. return Eauth;
  189. }
  190. /* otherwise, we'ld better have a usable cuid */
  191. if(!(afile->qid.type&QTAUTH))
  192. return Eauth;
  193. if(afile->uid != file->uid || afile->fs != fs)
  194. return Eauth;
  195. if(afile->cuid <= 0){
  196. if(authread(afile, buf, 0) != 0)
  197. return Eauth;
  198. if(afile->cuid <= 0)
  199. return Eauth;
  200. }
  201. file->uid = afile->cuid;
  202. /* once someone has authenticated on the channel, others can become none */
  203. chan->authed = 1;
  204. return 0;
  205. }
  206. static int
  207. fsattach(Chan* chan, Fcall* f, Fcall* r)
  208. {
  209. char *aname;
  210. Iobuf *p;
  211. Dentry *d;
  212. File *file;
  213. File *afile;
  214. Filsys *fs;
  215. int32_t raddr;
  216. int error, u;
  217. aname = f->aname;
  218. if(!aname[0]) /* default */
  219. aname = "main";
  220. p = nil;
  221. afile = filep(chan, f->afid, 0);
  222. file = filep(chan, f->fid, 1);
  223. if(file == nil){
  224. error = Efidinuse;
  225. goto out;
  226. }
  227. u = -1;
  228. if(chan != cons.chan){
  229. if(strcmp(f->uname, "adm") == 0){
  230. error = Eauth;
  231. goto out;
  232. }
  233. u = strtouid(f->uname);
  234. if(u < 0){
  235. error = Ebadu;
  236. goto out;
  237. }
  238. }
  239. file->uid = u;
  240. fs = fsstr(aname);
  241. if(fs == nil){
  242. error = Ebadspc;
  243. goto out;
  244. }
  245. if(error = checkattach(chan, afile, file, fs))
  246. goto out;
  247. raddr = getraddr(fs->dev);
  248. p = getbuf(fs->dev, raddr, Bread);
  249. d = getdir(p, 0);
  250. if(d == nil || checktag(p, Tdir, QPROOT) || !(d->mode & DALLOC)){
  251. error = Ealloc;
  252. goto out;
  253. }
  254. if(iaccess(file, d, DEXEC)){
  255. error = Eaccess;
  256. goto out;
  257. }
  258. if(file->uid == 0 && isro(fs->dev)) {
  259. /*
  260. * 'none' not allowed on dump
  261. */
  262. error = Eaccess;
  263. goto out;
  264. }
  265. accessdir(p, d, FREAD);
  266. mkqid(&file->qid, d, 1);
  267. file->fs = fs;
  268. file->addr = raddr;
  269. file->slot = 0;
  270. file->open = 0;
  271. freewp(file->wpath);
  272. file->wpath = 0;
  273. r->qid = file->qid;
  274. // if(cons.flags & attachflag)
  275. // print("9p2: attach %s %T to \"%s\" C%d\n",
  276. // chan->whoname, chan->whotime, fs->name, chan->chan);
  277. out:
  278. // if((cons.flags & attachflag) && error)
  279. // print("9p2: attach %s %T SUCK EGGS --- %s\n",
  280. // f->uname, time(), errstr[error]);
  281. if(p != nil)
  282. putbuf(p);
  283. if(afile != nil)
  284. qunlock(afile);
  285. if(file != nil){
  286. qunlock(file);
  287. if(error)
  288. freefp(file);
  289. }
  290. return error;
  291. }
  292. static int
  293. fsflush(Chan* chan, Fcall* f, Fcall* r)
  294. {
  295. runlock(&chan->reflock);
  296. wlock(&chan->reflock);
  297. wunlock(&chan->reflock);
  298. rlock(&chan->reflock);
  299. return 0;
  300. }
  301. static void
  302. clone(File* nfile, File* file)
  303. {
  304. Wpath *wpath;
  305. nfile->qid = file->qid;
  306. lock(&wpathlock);
  307. nfile->wpath = file->wpath;
  308. for(wpath = nfile->wpath; wpath != nil; wpath = wpath->up)
  309. wpath->refs++;
  310. unlock(&wpathlock);
  311. nfile->fs = file->fs;
  312. nfile->addr = file->addr;
  313. nfile->slot = file->slot;
  314. nfile->uid = file->uid;
  315. nfile->cuid = 0;
  316. nfile->open = file->open & ~FREMOV;
  317. }
  318. static int
  319. walkname(File* file, char* wname, Qid* wqid)
  320. {
  321. Wpath *w;
  322. Iobuf *p, *p1;
  323. Dentry *d, *d1;
  324. int error, slot;
  325. int32_t addr, qpath;
  326. p = p1 = nil;
  327. /*
  328. * File must not have been opened for I/O by an open
  329. * or create message and must represent a directory.
  330. */
  331. if(file->open != 0){
  332. error = Emode;
  333. goto out;
  334. }
  335. p = getbuf(file->fs->dev, file->addr, Bread);
  336. if(p == nil || checktag(p, Tdir, QPNONE)){
  337. error = Edir1;
  338. goto out;
  339. }
  340. if((d = getdir(p, file->slot)) == nil || !(d->mode & DALLOC)){
  341. error = Ealloc;
  342. goto out;
  343. }
  344. if(!(d->mode & DDIR)){
  345. error = Edir1;
  346. goto out;
  347. }
  348. if(error = mkqidcmp(&file->qid, d))
  349. goto out;
  350. /*
  351. * For walked elements the implied user must
  352. * have permission to search the directory.
  353. */
  354. if(file->cp != cons.chan && iaccess(file, d, DEXEC)){
  355. error = Eaccess;
  356. goto out;
  357. }
  358. accessdir(p, d, FREAD);
  359. if(strcmp(wname, ".") == 0){
  360. setdot:
  361. if(wqid != nil)
  362. *wqid = file->qid;
  363. goto out;
  364. }
  365. if(strcmp(wname, "..") == 0){
  366. if(file->wpath == 0)
  367. goto setdot;
  368. putbuf(p);
  369. p = nil;
  370. addr = file->wpath->addr;
  371. slot = file->wpath->slot;
  372. p1 = getbuf(file->fs->dev, addr, Bread);
  373. if(p1 == nil || checktag(p1, Tdir, QPNONE)){
  374. error = Edir1;
  375. goto out;
  376. }
  377. if((d1 = getdir(p1, slot)) == nil || !(d1->mode & DALLOC)){
  378. error = Ephase;
  379. goto out;
  380. }
  381. lock(&wpathlock);
  382. file->wpath->refs--;
  383. file->wpath = file->wpath->up;
  384. unlock(&wpathlock);
  385. goto found;
  386. }
  387. for(addr = 0; ; addr++){
  388. if(p == nil){
  389. p = getbuf(file->fs->dev, file->addr, Bread);
  390. if(p == nil || checktag(p, Tdir, QPNONE)){
  391. error = Ealloc;
  392. goto out;
  393. }
  394. d = getdir(p, file->slot);
  395. if(d == nil || !(d->mode & DALLOC)){
  396. error = Ealloc;
  397. goto out;
  398. }
  399. }
  400. qpath = d->qid.path;
  401. p1 = dnodebuf1(p, d, addr, 0);
  402. p = nil;
  403. if(p1 == nil || checktag(p1, Tdir, qpath)){
  404. error = Eentry;
  405. goto out;
  406. }
  407. for(slot = 0; slot < DIRPERBUF; slot++){
  408. d1 = getdir(p1, slot);
  409. if(!(d1->mode & DALLOC))
  410. continue;
  411. if(strncmp(wname, d1->name, NAMELEN) != 0)
  412. continue;
  413. /*
  414. * update walk path
  415. */
  416. if((w = newwp()) == nil){
  417. error = Ewalk;
  418. goto out;
  419. }
  420. w->addr = file->addr;
  421. w->slot = file->slot;
  422. w->up = file->wpath;
  423. file->wpath = w;
  424. slot += DIRPERBUF*addr;
  425. goto found;
  426. }
  427. putbuf(p1);
  428. p1 = nil;
  429. }
  430. found:
  431. file->addr = p1->addr;
  432. mkqid(&file->qid, d1, 1);
  433. putbuf(p1);
  434. p1 = nil;
  435. file->slot = slot;
  436. if(wqid != nil)
  437. *wqid = file->qid;
  438. out:
  439. if(p1 != nil)
  440. putbuf(p1);
  441. if(p != nil)
  442. putbuf(p);
  443. return error;
  444. }
  445. static int
  446. fswalk(Chan* chan, Fcall* f, Fcall* r)
  447. {
  448. int error, nwname;
  449. File *file, *nfile, tfile;
  450. /*
  451. * The file identified by f->fid must be valid in the
  452. * current session and must not have been opened for I/O
  453. * by an open or create message.
  454. */
  455. if((file = filep(chan, f->fid, 0)) == nil)
  456. return Efid;
  457. if(file->open != 0){
  458. qunlock(file);
  459. return Emode;
  460. }
  461. /*
  462. * If newfid is not the same as fid, allocate a new file;
  463. * a side effect is checking newfid is not already in use (error);
  464. * if there are no names to walk this will be equivalent to a
  465. * simple 'clone' operation.
  466. * Otherwise, fid and newfid are the same and if there are names
  467. * to walk make a copy of 'file' to be used during the walk as
  468. * 'file' must only be updated on success.
  469. * Finally, it's a no-op if newfid is the same as fid and f->nwname
  470. * is 0.
  471. */
  472. r->nwqid = 0;
  473. if(f->newfid != f->fid){
  474. if((nfile = filep(chan, f->newfid, 1)) == nil){
  475. qunlock(file);
  476. return Efidinuse;
  477. }
  478. }
  479. else if(f->nwname != 0){
  480. nfile = &tfile;
  481. memset(nfile, 0, sizeof(File));
  482. nfile->cp = chan;
  483. nfile->fid = ~0;
  484. }
  485. else{
  486. qunlock(file);
  487. return 0;
  488. }
  489. clone(nfile, file);
  490. /*
  491. * Should check name is not too long.
  492. */
  493. error = 0;
  494. for(nwname = 0; nwname < f->nwname; nwname++){
  495. error = walkname(nfile, f->wname[nwname], &r->wqid[r->nwqid]);
  496. if(error != 0 || ++r->nwqid >= MAXDAT/sizeof(Qid))
  497. break;
  498. }
  499. if(f->nwname == 0){
  500. /*
  501. * Newfid must be different to fid (see above)
  502. * so this is a simple 'clone' operation - there's
  503. * nothing to do except unlock unless there's
  504. * an error.
  505. */
  506. if(error){
  507. freewp(nfile->wpath);
  508. qunlock(nfile);
  509. freefp(nfile);
  510. }
  511. else
  512. qunlock(nfile);
  513. }
  514. else if(r->nwqid < f->nwname){
  515. /*
  516. * Didn't walk all elements, 'clunk' nfile
  517. * and leave 'file' alone.
  518. * Clear error if some of the elements were
  519. * walked OK.
  520. */
  521. freewp(nfile->wpath);
  522. if(nfile != &tfile){
  523. qunlock(nfile);
  524. freefp(nfile);
  525. }
  526. if(r->nwqid != 0)
  527. error = 0;
  528. }
  529. else{
  530. /*
  531. * Walked all elements. If newfid is the same
  532. * as fid must update 'file' from the temporary
  533. * copy used during the walk.
  534. * Otherwise just unlock (when using tfile there's
  535. * no need to unlock as it's a local).
  536. */
  537. if(nfile == &tfile){
  538. file->qid = nfile->qid;
  539. freewp(file->wpath);
  540. file->wpath = nfile->wpath;
  541. file->addr = nfile->addr;
  542. file->slot = nfile->slot;
  543. }
  544. else
  545. qunlock(nfile);
  546. }
  547. qunlock(file);
  548. return error;
  549. }
  550. static int
  551. fsopen(Chan* chan, Fcall* f, Fcall* r)
  552. {
  553. Iobuf *p;
  554. Dentry *d;
  555. File *file;
  556. Tlock *t;
  557. Qid qid;
  558. int error, ro, fmod, wok;
  559. wok = 0;
  560. p = nil;
  561. if(chan == cons.chan || writeallow)
  562. wok = 1;
  563. if((file = filep(chan, f->fid, 0)) == nil){
  564. error = Efid;
  565. goto out;
  566. }
  567. /*
  568. * if remove on close, check access here
  569. */
  570. ro = isro(file->fs->dev) || (writegroup && !ingroup(file->uid, writegroup));
  571. if(f->mode & ORCLOSE){
  572. if(ro){
  573. error = Eronly;
  574. goto out;
  575. }
  576. /*
  577. * check on parent directory of file to be deleted
  578. */
  579. if(file->wpath == 0 || file->wpath->addr == file->addr){
  580. error = Ephase;
  581. goto out;
  582. }
  583. p = getbuf(file->fs->dev, file->wpath->addr, Bread);
  584. if(p == nil || checktag(p, Tdir, QPNONE)){
  585. error = Ephase;
  586. goto out;
  587. }
  588. if((d = getdir(p, file->wpath->slot)) == nil || !(d->mode & DALLOC)){
  589. error = Ephase;
  590. goto out;
  591. }
  592. if(iaccess(file, d, DWRITE)){
  593. error = Eaccess;
  594. goto out;
  595. }
  596. putbuf(p);
  597. }
  598. p = getbuf(file->fs->dev, file->addr, Bread);
  599. if(p == nil || checktag(p, Tdir, QPNONE)){
  600. error = Ealloc;
  601. goto out;
  602. }
  603. if((d = getdir(p, file->slot)) == nil || !(d->mode & DALLOC)){
  604. error = Ealloc;
  605. goto out;
  606. }
  607. if(error = mkqidcmp(&file->qid, d))
  608. goto out;
  609. mkqid(&qid, d, 1);
  610. switch(f->mode & 7){
  611. case OREAD:
  612. if(iaccess(file, d, DREAD) && !wok)
  613. goto badaccess;
  614. fmod = FREAD;
  615. break;
  616. case OWRITE:
  617. if((d->mode & DDIR) || (iaccess(file, d, DWRITE) && !wok))
  618. goto badaccess;
  619. if(ro){
  620. error = Eronly;
  621. goto out;
  622. }
  623. fmod = FWRITE;
  624. break;
  625. case ORDWR:
  626. if((d->mode & DDIR)
  627. || (iaccess(file, d, DREAD) && !wok)
  628. || (iaccess(file, d, DWRITE) && !wok))
  629. goto badaccess;
  630. if(ro){
  631. error = Eronly;
  632. goto out;
  633. }
  634. fmod = FREAD+FWRITE;
  635. break;
  636. case OEXEC:
  637. if((d->mode & DDIR) || (iaccess(file, d, DEXEC) && !wok))
  638. goto badaccess;
  639. fmod = FREAD;
  640. break;
  641. default:
  642. error = Emode;
  643. goto out;
  644. }
  645. if(f->mode & OTRUNC){
  646. if((d->mode & DDIR) || (iaccess(file, d, DWRITE) && !wok))
  647. goto badaccess;
  648. if(ro){
  649. error = Eronly;
  650. goto out;
  651. }
  652. }
  653. t = 0;
  654. if(d->mode & DLOCK){
  655. if((t = tlocked(p, d)) == nil){
  656. error = Elocked;
  657. goto out;
  658. }
  659. }
  660. if(f->mode & ORCLOSE)
  661. fmod |= FREMOV;
  662. file->open = fmod;
  663. if((f->mode & OTRUNC) && !(d->mode & DAPND)){
  664. dtrunc(p, d);
  665. qid.vers = d->qid.version;
  666. }
  667. r->qid = qid;
  668. file->tlock = t;
  669. if(t != nil)
  670. t->file = file;
  671. file->lastra = 1;
  672. goto out;
  673. badaccess:
  674. error = Eaccess;
  675. file->open = 0;
  676. out:
  677. if(p != nil)
  678. putbuf(p);
  679. if(file != nil)
  680. qunlock(file);
  681. r->iounit = chan->msize-IOHDRSZ;
  682. return error;
  683. }
  684. static int
  685. dir9p2(Dir* dir, Dentry* dentry, void* strs)
  686. {
  687. char *op, *p;
  688. memset(dir, 0, sizeof(Dir));
  689. mkqid(&dir->qid, dentry, 1);
  690. dir->mode = (dir->qid.type<<24)|(dentry->mode & 0777);
  691. dir->atime = dentry->atime;
  692. dir->mtime = dentry->mtime;
  693. dir->length = dentry->size;
  694. op = p = strs;
  695. dir->name = p;
  696. p += sprint(p, "%s", dentry->name)+1;
  697. dir->uid = p;
  698. uidtostr(p, dentry->uid);
  699. p += strlen(p)+1;
  700. dir->gid = p;
  701. uidtostr(p, dentry->gid);
  702. p += strlen(p)+1;
  703. dir->muid = p;
  704. strcpy(p, "");
  705. p += strlen(p)+1;
  706. return p-op;
  707. }
  708. static int
  709. checkname9p2(char* name)
  710. {
  711. char *p;
  712. /*
  713. * Return length of string if valid, 0 if not.
  714. */
  715. if(name == nil)
  716. return 0;
  717. for(p = name; *p != 0; p++){
  718. if((*p & 0xFF) <= 040)
  719. return 0;
  720. }
  721. return p-name;
  722. }
  723. static int
  724. fscreate(Chan* chan, Fcall* f, Fcall* r)
  725. {
  726. Iobuf *p, *p1;
  727. Dentry *d, *d1;
  728. File *file;
  729. int error, slot, slot1, fmod, wok, l;
  730. int32_t addr, addr1, path;
  731. Tlock *t;
  732. Wpath *w;
  733. wok = 0;
  734. p = nil;
  735. if(chan == cons.chan || writeallow)
  736. wok = 1;
  737. if((file = filep(chan, f->fid, 0)) == nil){
  738. error = Efid;
  739. goto out;
  740. }
  741. if(isro(file->fs->dev) || (writegroup && !ingroup(file->uid, writegroup))){
  742. error = Eronly;
  743. goto out;
  744. }
  745. p = getbuf(file->fs->dev, file->addr, Bread);
  746. if(p == nil || checktag(p, Tdir, QPNONE)){
  747. error = Ealloc;
  748. goto out;
  749. }
  750. if((d = getdir(p, file->slot)) == nil || !(d->mode & DALLOC)){
  751. error = Ealloc;
  752. goto out;
  753. }
  754. if(error = mkqidcmp(&file->qid, d))
  755. goto out;
  756. if(!(d->mode & DDIR)){
  757. error = Edir2;
  758. goto out;
  759. }
  760. if(iaccess(file, d, DWRITE) && !wok) {
  761. error = Eaccess;
  762. goto out;
  763. }
  764. accessdir(p, d, FREAD);
  765. /*
  766. * Check the name is valid and will fit in an old
  767. * directory entry.
  768. */
  769. if((l = checkname9p2(f->name)) == 0){
  770. error = Ename;
  771. goto out;
  772. }
  773. if(l+1 > NAMELEN){
  774. error = Etoolong;
  775. goto out;
  776. }
  777. if(strcmp(f->name, ".") == 0 || strcmp(f->name, "..") == 0){
  778. error = Edot;
  779. goto out;
  780. }
  781. addr1 = 0;
  782. slot1 = 0; /* set */
  783. for(addr = 0; ; addr++){
  784. if((p1 = dnodebuf(p, d, addr, 0)) == nil){
  785. if(addr1 != 0)
  786. break;
  787. p1 = dnodebuf(p, d, addr, Tdir);
  788. }
  789. if(p1 == nil){
  790. error = Efull;
  791. goto out;
  792. }
  793. if(checktag(p1, Tdir, d->qid.path)){
  794. putbuf(p1);
  795. goto phase;
  796. }
  797. for(slot = 0; slot < DIRPERBUF; slot++){
  798. d1 = getdir(p1, slot);
  799. if(!(d1->mode & DALLOC)){
  800. if(addr1 == 0){
  801. addr1 = p1->addr;
  802. slot1 = slot + addr*DIRPERBUF;
  803. }
  804. continue;
  805. }
  806. if(strncmp(f->name, d1->name, sizeof(d1->name)) == 0){
  807. putbuf(p1);
  808. error = Eexist;
  809. goto out;
  810. }
  811. }
  812. putbuf(p1);
  813. }
  814. switch(f->mode & 7){
  815. case OEXEC:
  816. case OREAD: /* seems only useful to make directories */
  817. fmod = FREAD;
  818. break;
  819. case OWRITE:
  820. fmod = FWRITE;
  821. break;
  822. case ORDWR:
  823. fmod = FREAD+FWRITE;
  824. break;
  825. default:
  826. error = Emode;
  827. goto out;
  828. }
  829. if(f->perm & PDIR)
  830. if((f->mode & OTRUNC) || (f->perm & PAPND) || (fmod & FWRITE))
  831. goto badaccess;
  832. /*
  833. * do it
  834. */
  835. path = qidpathgen(&file->fs->dev);
  836. if((p1 = getbuf(file->fs->dev, addr1, Bread|Bimm|Bmod)) == nil)
  837. goto phase;
  838. d1 = getdir(p1, slot1);
  839. if(d1 == nil || checktag(p1, Tdir, d->qid.path)) {
  840. putbuf(p1);
  841. goto phase;
  842. }
  843. if(d1->mode & DALLOC){
  844. putbuf(p1);
  845. goto phase;
  846. }
  847. strncpy(d1->name, f->name, sizeof(d1->name));
  848. if(chan == cons.chan){
  849. d1->uid = cons.uid;
  850. d1->gid = cons.gid;
  851. }
  852. else{
  853. d1->uid = file->uid;
  854. d1->gid = d->gid;
  855. f->perm &= d->mode | ~0666;
  856. if(f->perm & PDIR)
  857. f->perm &= d->mode | ~0777;
  858. }
  859. d1->qid.path = path;
  860. d1->qid.version = 0;
  861. d1->mode = DALLOC | (f->perm & 0777);
  862. if(f->perm & PDIR) {
  863. d1->mode |= DDIR;
  864. d1->qid.path |= QPDIR;
  865. }
  866. if(f->perm & PAPND)
  867. d1->mode |= DAPND;
  868. t = nil;
  869. if(f->perm & PLOCK){
  870. d1->mode |= DLOCK;
  871. t = tlocked(p1, d1);
  872. /* if nil, out of tlock structures */
  873. }
  874. accessdir(p1, d1, FWRITE);
  875. mkqid(&r->qid, d1, 0);
  876. putbuf(p1);
  877. accessdir(p, d, FWRITE);
  878. /*
  879. * do a walk to new directory entry
  880. */
  881. if((w = newwp()) == nil){
  882. error = Ewalk;
  883. goto out;
  884. }
  885. w->addr = file->addr;
  886. w->slot = file->slot;
  887. w->up = file->wpath;
  888. file->wpath = w;
  889. file->qid = r->qid;
  890. file->tlock = t;
  891. if(t != nil)
  892. t->file = file;
  893. file->lastra = 1;
  894. if(f->mode & ORCLOSE)
  895. fmod |= FREMOV;
  896. file->open = fmod;
  897. file->addr = addr1;
  898. file->slot = slot1;
  899. goto out;
  900. badaccess:
  901. error = Eaccess;
  902. goto out;
  903. phase:
  904. error = Ephase;
  905. out:
  906. if(p != nil)
  907. putbuf(p);
  908. if(file != nil)
  909. qunlock(file);
  910. r->iounit = chan->msize-IOHDRSZ;
  911. return error;
  912. }
  913. static int
  914. fsread(Chan* chan, Fcall* f, Fcall* r)
  915. {
  916. uint8_t *data;
  917. Iobuf *p, *p1;
  918. File *file;
  919. Dentry *d, *d1;
  920. Tlock *t;
  921. int32_t addr, offset, start, tim;
  922. int error, iounit, nread, count, n, o, slot;
  923. Dir dir;
  924. char strdata[28*10];
  925. p = nil;
  926. data = (uint8_t*)r->data;
  927. count = f->count;
  928. offset = f->offset;
  929. nread = 0;
  930. if((file = filep(chan, f->fid, 0)) == nil){
  931. error = Efid;
  932. goto out;
  933. }
  934. if(file->qid.type & QTAUTH){
  935. nread = authread(file, data, count);
  936. if(nread < 0)
  937. error = Esystem;
  938. else
  939. error = 0;
  940. goto out;
  941. }
  942. if(!(file->open & FREAD)){
  943. error = Eopen;
  944. goto out;
  945. }
  946. iounit = chan->msize-IOHDRSZ;
  947. if(count < 0 || count > iounit){
  948. error = Ecount;
  949. goto out;
  950. }
  951. if(offset < 0){
  952. error = Eoffset;
  953. goto out;
  954. }
  955. p = getbuf(file->fs->dev, file->addr, Bread);
  956. if(p == nil || checktag(p, Tdir, QPNONE)){
  957. error = Ealloc;
  958. goto out;
  959. }
  960. if((d = getdir(p, file->slot)) == nil || !(d->mode & DALLOC)){
  961. error = Ealloc;
  962. goto out;
  963. }
  964. if(error = mkqidcmp(&file->qid, d))
  965. goto out;
  966. if(t = file->tlock){
  967. tim = time(0);
  968. if(t->time < tim || t->file != file){
  969. error = Ebroken;
  970. goto out;
  971. }
  972. /* renew the lock */
  973. t->time = tim + TLOCK;
  974. }
  975. accessdir(p, d, FREAD);
  976. if(d->mode & DDIR)
  977. goto dread;
  978. if(offset+count > d->size)
  979. count = d->size - offset;
  980. while(count > 0){
  981. if(p == nil){
  982. p = getbuf(file->fs->dev, file->addr, Bread);
  983. if(p == nil || checktag(p, Tdir, QPNONE)){
  984. error = Ealloc;
  985. goto out;
  986. }
  987. if((d = getdir(p, file->slot)) == nil || !(d->mode & DALLOC)){
  988. error = Ealloc;
  989. goto out;
  990. }
  991. }
  992. addr = offset / BUFSIZE;
  993. o = offset % BUFSIZE;
  994. n = BUFSIZE - o;
  995. if(n > count)
  996. n = count;
  997. p1 = dnodebuf1(p, d, addr, 0);
  998. p = nil;
  999. if(p1 != nil){
  1000. if(checktag(p1, Tfile, QPNONE)){
  1001. error = Ephase;
  1002. putbuf(p1);
  1003. goto out;
  1004. }
  1005. memmove(data+nread, p1->iobuf+o, n);
  1006. putbuf(p1);
  1007. }
  1008. else
  1009. memset(data+nread, 0, n);
  1010. count -= n;
  1011. nread += n;
  1012. offset += n;
  1013. }
  1014. goto out;
  1015. dread:
  1016. /*
  1017. * Pick up where we left off last time if nothing has changed,
  1018. * otherwise must scan from the beginning.
  1019. */
  1020. if(offset == file->doffset /*&& file->qid.vers == file->dvers*/){
  1021. addr = file->dslot/DIRPERBUF;
  1022. slot = file->dslot%DIRPERBUF;
  1023. start = offset;
  1024. }
  1025. else{
  1026. addr = 0;
  1027. slot = 0;
  1028. start = 0;
  1029. }
  1030. dread1:
  1031. if(p == nil){
  1032. /*
  1033. * This is just a check to ensure the entry hasn't
  1034. * gone away during the read of each directory block.
  1035. */
  1036. p = getbuf(file->fs->dev, file->addr, Bread);
  1037. if(p == nil || checktag(p, Tdir, QPNONE)){
  1038. error = Ealloc;
  1039. goto out1;
  1040. }
  1041. if((d = getdir(p, file->slot)) == nil || !(d->mode & DALLOC)){
  1042. error = Ealloc;
  1043. goto out1;
  1044. }
  1045. }
  1046. p1 = dnodebuf1(p, d, addr, 0);
  1047. p = nil;
  1048. if(p1 == nil)
  1049. goto out1;
  1050. if(checktag(p1, Tdir, QPNONE)){
  1051. error = Ephase;
  1052. putbuf(p1);
  1053. goto out1;
  1054. }
  1055. for(; slot < DIRPERBUF; slot++){
  1056. d1 = getdir(p1, slot);
  1057. if(!(d1->mode & DALLOC))
  1058. continue;
  1059. dir9p2(&dir, d1, strdata);
  1060. if((n = convD2M(&dir, data+nread, iounit - nread)) <= BIT16SZ){
  1061. putbuf(p1);
  1062. goto out1;
  1063. }
  1064. start += n;
  1065. if(start < offset)
  1066. continue;
  1067. if(count < n){
  1068. putbuf(p1);
  1069. goto out1;
  1070. }
  1071. count -= n;
  1072. nread += n;
  1073. offset += n;
  1074. }
  1075. putbuf(p1);
  1076. slot = 0;
  1077. addr++;
  1078. goto dread1;
  1079. out1:
  1080. if(error == 0){
  1081. file->doffset = offset;
  1082. file->dvers = file->qid.vers;
  1083. file->dslot = slot+DIRPERBUF*addr;
  1084. }
  1085. out:
  1086. /*
  1087. * Do we need this any more?
  1088. count = f->count - nread;
  1089. if(count > 0)
  1090. memset(data+nread, 0, count);
  1091. */
  1092. if(p != nil)
  1093. putbuf(p);
  1094. if(file != nil)
  1095. qunlock(file);
  1096. r->count = nread;
  1097. r->data = (char*)data;
  1098. return error;
  1099. }
  1100. static int
  1101. fswrite(Chan* chan, Fcall* f, Fcall* r)
  1102. {
  1103. Iobuf *p, *p1;
  1104. Dentry *d;
  1105. File *file;
  1106. Tlock *t;
  1107. int32_t offset, addr, tim, qpath;
  1108. int count, error, nwrite, o, n;
  1109. offset = f->offset;
  1110. count = f->count;
  1111. nwrite = 0;
  1112. p = nil;
  1113. if((file = filep(chan, f->fid, 0)) == nil){
  1114. error = Efid;
  1115. goto out;
  1116. }
  1117. if(file->qid.type & QTAUTH){
  1118. nwrite = authwrite(file, (uint8_t*)f->data, count);
  1119. if(nwrite < 0)
  1120. error = Esystem;
  1121. else
  1122. error = 0;
  1123. goto out;
  1124. }
  1125. if(!(file->open & FWRITE)){
  1126. error = Eopen;
  1127. goto out;
  1128. }
  1129. if(isro(file->fs->dev) || (writegroup && !ingroup(file->uid, writegroup))){
  1130. error = Eronly;
  1131. goto out;
  1132. }
  1133. if(count < 0 || count > chan->msize-IOHDRSZ){
  1134. error = Ecount;
  1135. goto out;
  1136. }
  1137. if(offset < 0) {
  1138. error = Eoffset;
  1139. goto out;
  1140. }
  1141. if((p = getbuf(file->fs->dev, file->addr, Bread|Bmod)) == nil){
  1142. error = Ealloc;
  1143. goto out;
  1144. }
  1145. if((d = getdir(p, file->slot)) == nil || !(d->mode & DALLOC)){
  1146. error = Ealloc;
  1147. goto out;
  1148. }
  1149. if(error = mkqidcmp(&file->qid, d))
  1150. goto out;
  1151. if(t = file->tlock) {
  1152. tim = time(0);
  1153. if(t->time < tim || t->file != file){
  1154. error = Ebroken;
  1155. goto out;
  1156. }
  1157. /* renew the lock */
  1158. t->time = tim + TLOCK;
  1159. }
  1160. accessdir(p, d, FWRITE);
  1161. if(d->mode & DAPND)
  1162. offset = d->size;
  1163. if(offset+count > d->size)
  1164. d->size = offset+count;
  1165. while(count > 0){
  1166. if(p == nil){
  1167. p = getbuf(file->fs->dev, file->addr, Bread|Bmod);
  1168. if((d = getdir(p, file->slot)) == nil || !(d->mode & DALLOC)){
  1169. error = Ealloc;
  1170. goto out;
  1171. }
  1172. }
  1173. addr = offset / BUFSIZE;
  1174. o = offset % BUFSIZE;
  1175. n = BUFSIZE - o;
  1176. if(n > count)
  1177. n = count;
  1178. qpath = d->qid.path;
  1179. p1 = dnodebuf1(p, d, addr, Tfile);
  1180. p = nil;
  1181. if(p1 == nil) {
  1182. error = Efull;
  1183. goto out;
  1184. }
  1185. if(checktag(p1, Tfile, qpath)){
  1186. putbuf(p1);
  1187. error = Ephase;
  1188. goto out;
  1189. }
  1190. memmove(p1->iobuf+o, f->data+nwrite, n);
  1191. p1->flags |= Bmod;
  1192. putbuf(p1);
  1193. count -= n;
  1194. nwrite += n;
  1195. offset += n;
  1196. }
  1197. out:
  1198. if(p != nil)
  1199. putbuf(p);
  1200. if(file != nil)
  1201. qunlock(file);
  1202. r->count = nwrite;
  1203. return error;
  1204. }
  1205. static int
  1206. _clunk(File* file, int remove, int wok)
  1207. {
  1208. Tlock *t;
  1209. int error;
  1210. error = 0;
  1211. if(t = file->tlock){
  1212. if(t->file == file)
  1213. t->time = 0; /* free the lock */
  1214. file->tlock = 0;
  1215. }
  1216. if(remove)
  1217. error = doremove(file, wok);
  1218. file->open = 0;
  1219. freewp(file->wpath);
  1220. freefp(file);
  1221. qunlock(file);
  1222. return error;
  1223. }
  1224. static int
  1225. fsclunk(Chan* chan, Fcall* f, Fcall* r)
  1226. {
  1227. File *file;
  1228. if((file = filep(chan, f->fid, 0)) == nil)
  1229. return Efid;
  1230. _clunk(file, file->open & FREMOV, 0);
  1231. return 0;
  1232. }
  1233. static int
  1234. fsremove(Chan* chan, Fcall* f, Fcall* r)
  1235. {
  1236. File *file;
  1237. if((file = filep(chan, f->fid, 0)) == nil)
  1238. return Efid;
  1239. return _clunk(file, 1, chan == cons.chan);
  1240. }
  1241. static int
  1242. fsstat(Chan* chan, Fcall* f, Fcall* r, uint8_t* data)
  1243. {
  1244. Dir dir;
  1245. Iobuf *p;
  1246. Dentry *d;
  1247. File *file;
  1248. int error, len;
  1249. if((file = filep(chan, f->fid, 0)) == nil)
  1250. return Efid;
  1251. p = getbuf(file->fs->dev, file->addr, Bread);
  1252. if(p == nil || checktag(p, Tdir, QPNONE)){
  1253. error = Edir1;
  1254. goto out;
  1255. }
  1256. if((d = getdir(p, file->slot)) == nil || !(d->mode & DALLOC)){
  1257. error = Ealloc;
  1258. goto out;
  1259. }
  1260. if(error = mkqidcmp(&file->qid, d))
  1261. goto out;
  1262. if(d->qid.path == QPROOT) /* stat of root gives time */
  1263. d->atime = time(0);
  1264. len = dir9p2(&dir, d, data);
  1265. data += len;
  1266. if((r->nstat = convD2M(&dir, data, chan->msize - len)) == 0)
  1267. error = Ersc;
  1268. else
  1269. r->stat = data;
  1270. out:
  1271. if(p != nil)
  1272. putbuf(p);
  1273. if(file != nil)
  1274. qunlock(file);
  1275. return error;
  1276. }
  1277. static int
  1278. fswstat(Chan* chan, Fcall* f, Fcall* r, char *strs)
  1279. {
  1280. Iobuf *p, *p1;
  1281. Dentry *d, *d1, xd;
  1282. File *file;
  1283. int error, slot, uid, gid, l;
  1284. int32_t addr;
  1285. Dir dir;
  1286. uint32_t mode;
  1287. p = p1 = nil;
  1288. d1 = nil;
  1289. if((file = filep(chan, f->fid, 0)) == nil){
  1290. error = Efid;
  1291. goto out;
  1292. }
  1293. /*
  1294. * if user none,
  1295. * can't do anything
  1296. * unless allow.
  1297. */
  1298. if(file->uid == 0 && !wstatallow){
  1299. error = Eaccess;
  1300. goto out;
  1301. }
  1302. if(isro(file->fs->dev) || (writegroup && !ingroup(file->uid, writegroup))){
  1303. error = Eronly;
  1304. goto out;
  1305. }
  1306. /*
  1307. * first get parent
  1308. */
  1309. if(file->wpath){
  1310. p1 = getbuf(file->fs->dev, file->wpath->addr, Bread);
  1311. if(p1 == nil){
  1312. error = Ephase;
  1313. goto out;
  1314. }
  1315. d1 = getdir(p1, file->wpath->slot);
  1316. if(d1 == nil || checktag(p1, Tdir, QPNONE) || !(d1->mode & DALLOC)){
  1317. error = Ephase;
  1318. goto out;
  1319. }
  1320. }
  1321. if((p = getbuf(file->fs->dev, file->addr, Bread)) == nil){
  1322. error = Ealloc;
  1323. goto out;
  1324. }
  1325. d = getdir(p, file->slot);
  1326. if(d == nil || checktag(p, Tdir, QPNONE) || !(d->mode & DALLOC)){
  1327. error = Ealloc;
  1328. goto out;
  1329. }
  1330. if(error = mkqidcmp(&file->qid, d))
  1331. goto out;
  1332. /*
  1333. * Convert the message and fix up
  1334. * fields not to be changed.
  1335. */
  1336. if(convM2D(f->stat, f->nstat, &dir, strs) == 0){
  1337. print("9p2: convM2D returns 0\n");
  1338. error = Econvert;
  1339. goto out;
  1340. }
  1341. if(dir.uid == nil || strlen(dir.uid) == 0)
  1342. uid = d->uid;
  1343. else
  1344. uid = strtouid(dir.uid);
  1345. if(dir.gid == nil || strlen(dir.gid) == 0)
  1346. gid = d->gid;
  1347. else
  1348. gid = strtouid(dir.gid);
  1349. if(dir.name == nil || strlen(dir.name) == 0)
  1350. dir.name = d->name;
  1351. else{
  1352. if((l = checkname9p2(dir.name)) == 0){
  1353. error = Ename;
  1354. goto out;
  1355. }
  1356. if(l > NAMELEN){
  1357. error = Etoolong;
  1358. goto out;
  1359. }
  1360. }
  1361. /*
  1362. * Before doing sanity checks, find out what the
  1363. * new 'mode' should be:
  1364. * if 'type' and 'mode' are both defaults, take the
  1365. * new mode from the old directory entry;
  1366. * else if 'type' is the default, use the new mode entry;
  1367. * else if 'mode' is the default, create the new mode from
  1368. * 'type' or'ed with the old directory mode;
  1369. * else neither are defaults, use the new mode but check
  1370. * it agrees with 'type'.
  1371. */
  1372. if(dir.qid.type == 0xFF && dir.mode == ~0){
  1373. dir.mode = d->mode & 0777;
  1374. if(d->mode & DLOCK)
  1375. dir.mode |= DMEXCL;
  1376. if(d->mode & DAPND)
  1377. dir.mode |= DMAPPEND;
  1378. if(d->mode & DDIR)
  1379. dir.mode |= DMDIR;
  1380. }
  1381. else if(dir.qid.type == 0xFF){
  1382. /* nothing to do */
  1383. }
  1384. else if(dir.mode == ~0)
  1385. dir.mode = (dir.qid.type<<24)|(d->mode & 0777);
  1386. else if(dir.qid.type != ((dir.mode>>24) & 0xFF)){
  1387. error = Eqidmode;
  1388. goto out;
  1389. }
  1390. /*
  1391. * Check for unknown type/mode bits
  1392. * and an attempt to change the directory bit.
  1393. */
  1394. if(dir.mode & ~(DMDIR|DMAPPEND|DMEXCL|0777)){
  1395. error = Enotm;
  1396. goto out;
  1397. }
  1398. if(d->mode & DDIR)
  1399. mode = DMDIR;
  1400. else
  1401. mode = 0;
  1402. if((dir.mode^mode) & DMDIR){
  1403. error = Enotd;
  1404. goto out;
  1405. }
  1406. if(dir.mtime == ~0)
  1407. dir.mtime = d->mtime;
  1408. if(dir.length == ~0)
  1409. dir.length = d->size;
  1410. /*
  1411. * Currently, can't change length.
  1412. */
  1413. if(dir.length != d->size){
  1414. error = Enotl;
  1415. goto out;
  1416. }
  1417. /*
  1418. * if chown,
  1419. * must be god
  1420. * wstatallow set to allow chown during boot
  1421. */
  1422. if(uid != d->uid && !wstatallow) {
  1423. error = Enotu;
  1424. goto out;
  1425. }
  1426. /*
  1427. * if chgroup,
  1428. * must be either
  1429. * a) owner and in new group
  1430. * b) leader of both groups
  1431. * wstatallow and writeallow are set to allow chgrp during boot
  1432. */
  1433. while(gid != d->gid) {
  1434. if(wstatallow || writeallow)
  1435. break;
  1436. if(d->uid == file->uid && ingroup(file->uid, gid))
  1437. break;
  1438. if(leadgroup(file->uid, gid))
  1439. if(leadgroup(file->uid, d->gid))
  1440. break;
  1441. error = Enotg;
  1442. goto out;
  1443. }
  1444. /*
  1445. * if rename,
  1446. * must have write permission in parent
  1447. */
  1448. while(strncmp(d->name, dir.name, sizeof(d->name)) != 0) {
  1449. if(checkname(dir.name) || d1 == nil) {
  1450. error = Ename;
  1451. goto out;
  1452. }
  1453. if(strcmp(dir.name, ".") == 0 || strcmp(xd.name, "..") == 0) {
  1454. error = Ename;
  1455. goto out;
  1456. }
  1457. /*
  1458. * drop entry to prevent lock, then
  1459. * check that destination name is unique,
  1460. */
  1461. putbuf(p);
  1462. for(addr = 0; ; addr++) {
  1463. if((p = dnodebuf(p1, d1, addr, 0)) == nil)
  1464. break;
  1465. if(checktag(p, Tdir, d1->qid.path)) {
  1466. putbuf(p);
  1467. continue;
  1468. }
  1469. for(slot = 0; slot < DIRPERBUF; slot++) {
  1470. d = getdir(p, slot);
  1471. if(!(d->mode & DALLOC))
  1472. continue;
  1473. if(strncmp(dir.name, d->name, sizeof(d->name)) == 0) {
  1474. error = Eexist;
  1475. goto out;
  1476. }
  1477. }
  1478. putbuf(p);
  1479. }
  1480. /*
  1481. * reacquire entry
  1482. */
  1483. if((p = getbuf(file->fs->dev, file->addr, Bread)) == nil){
  1484. error = Ephase;
  1485. goto out;
  1486. }
  1487. d = getdir(p, file->slot);
  1488. if(d == nil || checktag(p, Tdir, QPNONE) || !(d->mode & DALLOC)) {
  1489. error = Ephase;
  1490. goto out;
  1491. }
  1492. if(wstatallow || writeallow) /* set to allow rename during boot */
  1493. break;
  1494. if(d1 == nil || iaccess(file, d1, DWRITE)) {
  1495. error = Eaccess;
  1496. goto out;
  1497. }
  1498. break;
  1499. }
  1500. /*
  1501. * if mode/time, either
  1502. * a) owner
  1503. * b) leader of either group
  1504. */
  1505. mode = dir.mode & 0777;
  1506. if(dir.mode & DMAPPEND)
  1507. mode |= DAPND;
  1508. if(dir.mode & DMEXCL)
  1509. mode |= DLOCK;
  1510. while(d->mtime != dir.mtime || ((d->mode^mode) & (DAPND|DLOCK|0777))) {
  1511. if(wstatallow) /* set to allow chmod during boot */
  1512. break;
  1513. if(d->uid == file->uid)
  1514. break;
  1515. if(leadgroup(file->uid, gid))
  1516. break;
  1517. if(leadgroup(file->uid, d->gid))
  1518. break;
  1519. error = Enotu;
  1520. goto out;
  1521. }
  1522. d->mtime = dir.mtime;
  1523. d->uid = uid;
  1524. d->gid = gid;
  1525. d->mode = (mode & (DAPND|DLOCK|0777)) | (d->mode & (DALLOC|DDIR));
  1526. strncpy(d->name, dir.name, sizeof(d->name));
  1527. accessdir(p, d, FWSTAT);
  1528. out:
  1529. if(p != nil)
  1530. putbuf(p);
  1531. if(p1 != nil)
  1532. putbuf(p1);
  1533. if(file != nil)
  1534. qunlock(file);
  1535. return error;
  1536. }
  1537. static int
  1538. recv(Chan *c, uint8_t *buf, int n)
  1539. {
  1540. int fd, m, len;
  1541. fd = c->chan;
  1542. /* read count */
  1543. qlock(&c->rlock);
  1544. m = readn(fd, buf, BIT32SZ);
  1545. if(m != BIT32SZ){
  1546. qunlock(&c->rlock);
  1547. if(m < 0){
  1548. print("readn(BIT32SZ) fails: %r\n");
  1549. return -1;
  1550. }
  1551. print("readn(BIT32SZ) returns %d: %r\n", m);
  1552. return 0;
  1553. }
  1554. len = GBIT32(buf);
  1555. if(len <= BIT32SZ || len > n){
  1556. print("recv bad length %d\n", len);
  1557. werrstr("bad length in 9P2000 message header");
  1558. qunlock(&c->rlock);
  1559. return -1;
  1560. }
  1561. len -= BIT32SZ;
  1562. m = readn(fd, buf+BIT32SZ, len);
  1563. qunlock(&c->rlock);
  1564. if(m < len){
  1565. print("recv wanted %d got %d\n", len, m);
  1566. return 0;
  1567. }
  1568. return BIT32SZ+m;
  1569. }
  1570. static void
  1571. send(Chan *c, uint8_t *buf, int n)
  1572. {
  1573. int fd, m;
  1574. fd = c->chan;
  1575. qlock(&c->wlock);
  1576. m = write(fd, buf, n);
  1577. qunlock(&c->wlock);
  1578. if(m == n)
  1579. return;
  1580. panic("write failed");
  1581. }
  1582. void
  1583. serve9p2(Chan *chan, uint8_t *ib, int nib)
  1584. {
  1585. uint8_t inbuf[MSIZE+IOHDRSZ], outbuf[MSIZE+IOHDRSZ];
  1586. Fcall f, r;
  1587. char ename[64];
  1588. int error, n, type;
  1589. chan->msize = MSIZE;
  1590. fmtinstall('F', fcallfmt);
  1591. for(;;){
  1592. if(nib){
  1593. memmove(inbuf, ib, nib);
  1594. n = nib;
  1595. nib = 0;
  1596. }else
  1597. n = recv(chan, inbuf, sizeof inbuf);
  1598. if(chat){
  1599. print("read msg %d (fd %d)\n", n, chan->chan);
  1600. if(n <= 0)
  1601. print("\terr: %r\n");
  1602. }
  1603. if(n == 0 && (chan == cons.srvchan || chan == cons.chan))
  1604. continue;
  1605. if(n <= 0)
  1606. break;
  1607. if(convM2S(inbuf, n, &f) != n){
  1608. print("9p2: cannot decode\n");
  1609. continue;
  1610. }
  1611. type = f.type;
  1612. if(type < Tversion || type >= Tmax || (type&1) || type == Terror){
  1613. print("9p2: bad message type %d\n", type);
  1614. continue;
  1615. }
  1616. if(CHAT(chan))
  1617. print("9p2: f %F\n", &f);
  1618. r.type = type+1;
  1619. r.tag = f.tag;
  1620. error = 0;
  1621. rlock(&mainlock);
  1622. rlock(&chan->reflock);
  1623. switch(type){
  1624. default:
  1625. r.type = Rerror;
  1626. snprint(ename, sizeof ename, "unknown message: %F", &f);
  1627. r.ename = ename;
  1628. break;
  1629. case Tversion:
  1630. error = fsversion(chan, &f, &r);
  1631. break;
  1632. case Tauth:
  1633. error = fsauth(chan, &f, &r);
  1634. break;
  1635. case Tattach:
  1636. error = fsattach(chan, &f, &r);
  1637. break;
  1638. case Tflush:
  1639. error = fsflush(chan, &f, &r);
  1640. break;
  1641. case Twalk:
  1642. error = fswalk(chan, &f, &r);
  1643. break;
  1644. case Topen:
  1645. error = fsopen(chan, &f, &r);
  1646. break;
  1647. case Tcreate:
  1648. error = fscreate(chan, &f, &r);
  1649. break;
  1650. case Tread:
  1651. r.data = (char*)inbuf;
  1652. error = fsread(chan, &f, &r);
  1653. break;
  1654. case Twrite:
  1655. error = fswrite(chan, &f, &r);
  1656. break;
  1657. case Tclunk:
  1658. error = fsclunk(chan, &f, &r);
  1659. break;
  1660. case Tremove:
  1661. error = fsremove(chan, &f, &r);
  1662. break;
  1663. case Tstat:
  1664. error = fsstat(chan, &f, &r, inbuf);
  1665. break;
  1666. case Twstat:
  1667. error = fswstat(chan, &f, &r, (char*)outbuf);
  1668. break;
  1669. }
  1670. runlock(&chan->reflock);
  1671. runlock(&mainlock);
  1672. if(error != 0){
  1673. r.type = Rerror;
  1674. if(error >= MAXERR){
  1675. snprint(ename, sizeof(ename), "error %d", error);
  1676. r.ename = ename;
  1677. }
  1678. else
  1679. r.ename = errstring[error];
  1680. }
  1681. if(CHAT(chan))
  1682. print("9p2: r %F\n", &r);
  1683. n = convS2M(&r, outbuf, sizeof outbuf);
  1684. if(n == 0){
  1685. type = r.type;
  1686. r.type = Rerror;
  1687. snprint(ename, sizeof(ename), "9p2: convS2M: type %d", type);
  1688. r.ename = ename;
  1689. print(ename);
  1690. n = convS2M(&r, outbuf, sizeof outbuf);
  1691. if(n == 0){
  1692. /*
  1693. * What to do here, the failure notification failed?
  1694. */
  1695. panic("can't write anything at all");
  1696. }
  1697. }
  1698. send(chan, outbuf, n);
  1699. }
  1700. fileinit(chan);
  1701. close(chan->chan);
  1702. if(chan == cons.srvchan || chan == cons.chan)
  1703. print("console chan read error");
  1704. }