9p2.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  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. }