nfs.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. /* Copyright © 2003 Russ Cox, MIT; see /sys/src/libsunrpc/COPYING */
  2. #include <u.h>
  3. #include <libc.h>
  4. #include <bio.h>
  5. #include <fcall.h>
  6. #include <thread.h>
  7. #include <9p.h>
  8. #include <sunrpc.h>
  9. #include <nfs3.h>
  10. SunClient *nfscli;
  11. SunClient *mntcli;
  12. char *defaultpath = "/";
  13. Channel *fschan;
  14. char *sys;
  15. int verbose;
  16. int readplus = 0;
  17. typedef struct Auth Auth;
  18. struct Auth
  19. {
  20. int ref;
  21. uchar *data;
  22. int ndata;
  23. };
  24. typedef struct FidAux FidAux;
  25. struct FidAux
  26. {
  27. Nfs3Handle handle;
  28. u64int cookie; /* for continuing directory reads */
  29. char *name; /* botch: for remove and rename */
  30. Nfs3Handle parent; /* botch: for remove and rename */
  31. char err[ERRMAX]; /* for walk1 */
  32. Auth *auth;
  33. };
  34. /*
  35. * various RPCs. here is where we'd insert support for NFS v2
  36. */
  37. void
  38. portCall(SunCall *c, PortCallType type)
  39. {
  40. c->rpc.prog = PortProgram;
  41. c->rpc.vers = PortVersion;
  42. c->rpc.proc = type>>1;
  43. c->rpc.iscall = !(type&1);
  44. c->type = type;
  45. }
  46. int
  47. getport(SunClient *client, uint prog, uint vers, uint prot, uint *port)
  48. {
  49. PortTGetport tx;
  50. PortRGetport rx;
  51. memset(&tx, 0, sizeof tx);
  52. portCall(&tx.call, PortCallTGetport);
  53. tx.map.prog = prog;
  54. tx.map.vers = vers;
  55. tx.map.prot = prot;
  56. memset(&rx, 0, sizeof rx);
  57. portCall(&rx.call, PortCallRGetport);
  58. if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
  59. return -1;
  60. *port = rx.port;
  61. return 0;
  62. }
  63. void
  64. mountCall(Auth *a, SunCall *c, NfsMount3CallType type)
  65. {
  66. c->rpc.iscall = !(type&1);
  67. c->rpc.proc = type>>1;
  68. c->rpc.prog = NfsMount3Program;
  69. c->rpc.vers = NfsMount3Version;
  70. if(c->rpc.iscall && a){
  71. c->rpc.cred.flavor = SunAuthSys;
  72. c->rpc.cred.data = a->data;
  73. c->rpc.cred.ndata = a->ndata;
  74. }
  75. c->type = type;
  76. }
  77. int
  78. mountNull(ulong tag)
  79. {
  80. NfsMount3TNull tx;
  81. NfsMount3RNull rx;
  82. memset(&tx, 0, sizeof tx);
  83. mountCall(nil, &tx.call, NfsMount3CallTNull);
  84. memset(&rx, 0, sizeof rx);
  85. mountCall(nil, &rx.call, NfsMount3CallTNull);
  86. return sunClientRpc(mntcli, tag, &tx.call, &rx.call, nil);
  87. }
  88. int
  89. mountMnt(Auth *a, ulong tag, char *path, Nfs3Handle *h)
  90. {
  91. uchar *freeme;
  92. NfsMount3TMnt tx;
  93. NfsMount3RMnt rx;
  94. memset(&tx, 0, sizeof tx);
  95. mountCall(a, &tx.call, NfsMount3CallTMnt);
  96. tx.path = path;
  97. memset(&rx, 0, sizeof rx);
  98. mountCall(a, &rx.call, NfsMount3CallRMnt);
  99. if(sunClientRpc(mntcli, tag, &tx.call, &rx.call, &freeme) < 0)
  100. return -1;
  101. if(rx.status != Nfs3Ok){
  102. nfs3Errstr(rx.status);
  103. return -1;
  104. }
  105. if(verbose)print("handle %.*H\n", rx.len, rx.handle);
  106. if(rx.len >= Nfs3MaxHandleSize){
  107. free(freeme);
  108. werrstr("server-returned handle too long");
  109. return -1;
  110. }
  111. memmove(h->h, rx.handle, rx.len);
  112. h->len = rx.len;
  113. free(freeme);
  114. return 0;
  115. }
  116. void
  117. nfs3Call(Auth *a, SunCall *c, Nfs3CallType type)
  118. {
  119. c->rpc.iscall = !(type&1);
  120. c->rpc.proc = type>>1;
  121. c->rpc.prog = Nfs3Program;
  122. c->rpc.vers = Nfs3Version;
  123. if(c->rpc.iscall && a){
  124. c->rpc.cred.flavor = SunAuthSys;
  125. c->rpc.cred.data = a->data;
  126. c->rpc.cred.ndata = a->ndata;
  127. }
  128. c->type = type;
  129. }
  130. int
  131. nfsNull(ulong tag)
  132. {
  133. Nfs3TNull tx;
  134. Nfs3RNull rx;
  135. memset(&tx, 0, sizeof tx);
  136. nfs3Call(nil, &tx.call, Nfs3CallTNull);
  137. memset(&rx, 0, sizeof rx);
  138. nfs3Call(nil, &rx.call, Nfs3CallTNull);
  139. return sunClientRpc(nfscli, tag, &tx.call, &rx.call, nil);
  140. }
  141. int
  142. nfsGetattr(Auth *a, ulong tag, Nfs3Handle *h, Nfs3Attr *attr)
  143. {
  144. Nfs3TGetattr tx;
  145. Nfs3RGetattr rx;
  146. memset(&tx, 0, sizeof tx);
  147. nfs3Call(a, &tx.call, Nfs3CallTGetattr);
  148. tx.handle = *h;
  149. memset(&rx, 0, sizeof rx);
  150. nfs3Call(a, &rx.call, Nfs3CallRGetattr);
  151. if(sunClientRpc(nfscli, tag, &tx.call, &rx.call, nil) < 0)
  152. return -1;
  153. if(rx.status != Nfs3Ok){
  154. nfs3Errstr(rx.status);
  155. return -1;
  156. }
  157. *attr = rx.attr;
  158. return 0;
  159. }
  160. int
  161. nfsAccess(Auth *a, ulong tag, Nfs3Handle *h, ulong want, ulong *got, u1int *have, Nfs3Attr *attr)
  162. {
  163. Nfs3TAccess tx;
  164. Nfs3RAccess rx;
  165. memset(&tx, 0, sizeof tx);
  166. nfs3Call(a, &tx.call, Nfs3CallTAccess);
  167. tx.handle = *h;
  168. tx.access = want;
  169. memset(&rx, 0, sizeof rx);
  170. nfs3Call(a, &rx.call, Nfs3CallRAccess);
  171. if(sunClientRpc(nfscli, tag, &tx.call, &rx.call, nil) < 0)
  172. return -1;
  173. if(rx.status != Nfs3Ok){
  174. nfs3Errstr(rx.status);
  175. return -1;
  176. }
  177. *got = rx.access;
  178. *have = rx.haveAttr;
  179. if(rx.haveAttr)
  180. *attr = rx.attr;
  181. return 0;
  182. }
  183. int
  184. nfsMkdir(Auth *a, ulong tag, Nfs3Handle *h, char *name, Nfs3Handle *nh, ulong mode, uint gid,
  185. u1int *have, Nfs3Attr *attr)
  186. {
  187. Nfs3TMkdir tx;
  188. Nfs3RMkdir rx;
  189. memset(&tx, 0, sizeof tx);
  190. nfs3Call(a, &tx.call, Nfs3CallTMkdir);
  191. tx.handle = *h;
  192. tx.name = name;
  193. tx.attr.setMode = 1;
  194. tx.attr.mode = mode;
  195. tx.attr.setGid = 1;
  196. tx.attr.gid = gid;
  197. memset(&rx, 0, sizeof rx);
  198. nfs3Call(a, &rx.call, Nfs3CallRMkdir);
  199. if(sunClientRpc(nfscli, tag, &tx.call, &rx.call, nil) < 0)
  200. return -1;
  201. if(rx.status != Nfs3Ok){
  202. nfs3Errstr(rx.status);
  203. return -1;
  204. }
  205. if(!rx.haveHandle){
  206. werrstr("nfs mkdir did not return handle");
  207. return -1;
  208. }
  209. *nh = rx.handle;
  210. *have = rx.haveAttr;
  211. if(rx.haveAttr)
  212. *attr = rx.attr;
  213. return 0;
  214. }
  215. int
  216. nfsCreate(Auth *a, ulong tag, Nfs3Handle *h, char *name, Nfs3Handle *nh, ulong mode, uint gid,
  217. u1int *have, Nfs3Attr *attr)
  218. {
  219. Nfs3TCreate tx;
  220. Nfs3RCreate rx;
  221. memset(&tx, 0, sizeof tx);
  222. nfs3Call(a, &tx.call, Nfs3CallTCreate);
  223. tx.handle = *h;
  224. tx.name = name;
  225. tx.attr.setMode = 1;
  226. tx.attr.mode = mode;
  227. tx.attr.setGid = 1;
  228. tx.attr.gid = gid;
  229. memset(&rx, 0, sizeof rx);
  230. nfs3Call(a, &rx.call, Nfs3CallRCreate);
  231. if(sunClientRpc(nfscli, tag, &tx.call, &rx.call, nil) < 0)
  232. return -1;
  233. if(rx.status != Nfs3Ok){
  234. nfs3Errstr(rx.status);
  235. return -1;
  236. }
  237. if(!rx.haveHandle){
  238. werrstr("nfs create did not return handle");
  239. return -1;
  240. }
  241. *nh = rx.handle;
  242. *have = rx.haveAttr;
  243. if(rx.haveAttr)
  244. *attr = rx.attr;
  245. return 0;
  246. }
  247. int
  248. nfsRead(Auth *a, ulong tag, Nfs3Handle *h, u32int count, u64int offset,
  249. uchar **pp, u32int *pcount, uchar **pfreeme)
  250. {
  251. uchar *freeme;
  252. Nfs3TRead tx;
  253. Nfs3RRead rx;
  254. memset(&tx, 0, sizeof tx);
  255. nfs3Call(a, &tx.call, Nfs3CallTRead);
  256. tx.handle = *h;
  257. tx.count = count;
  258. tx.offset = offset;
  259. memset(&rx, 0, sizeof rx);
  260. nfs3Call(a, &rx.call, Nfs3CallRRead);
  261. if(sunClientRpc(nfscli, tag, &tx.call, &rx.call, &freeme) < 0)
  262. return -1;
  263. if(rx.status != Nfs3Ok){
  264. nfs3Errstr(rx.status);
  265. return -1;
  266. }
  267. if(rx.count != rx.ndata){
  268. werrstr("nfs read returned count=%ud ndata=%ud", (uint)rx.count, (uint)rx.ndata);
  269. free(freeme);
  270. return -1;
  271. }
  272. *pfreeme = freeme;
  273. *pcount = rx.count;
  274. *pp = rx.data;
  275. return 0;
  276. }
  277. int
  278. nfsWrite(Auth *a, ulong tag, Nfs3Handle *h, uchar *data, u32int count, u64int offset, u32int *pcount)
  279. {
  280. Nfs3TWrite tx;
  281. Nfs3RWrite rx;
  282. memset(&tx, 0, sizeof tx);
  283. nfs3Call(a, &tx.call, Nfs3CallTWrite);
  284. tx.handle = *h;
  285. tx.count = count;
  286. tx.offset = offset;
  287. tx.data = data;
  288. memset(&rx, 0, sizeof rx);
  289. nfs3Call(a, &rx.call, Nfs3CallRWrite);
  290. if(sunClientRpc(nfscli, tag, &tx.call, &rx.call, nil) < 0)
  291. return -1;
  292. if(rx.status != Nfs3Ok){
  293. nfs3Errstr(rx.status);
  294. return -1;
  295. }
  296. *pcount = rx.count;
  297. return 0;
  298. }
  299. int
  300. nfsRmdir(Auth *a, ulong tag, Nfs3Handle *h, char *name)
  301. {
  302. Nfs3TRmdir tx;
  303. Nfs3RRmdir rx;
  304. memset(&tx, 0, sizeof tx);
  305. nfs3Call(a, &tx.call, Nfs3CallTRmdir);
  306. tx.handle = *h;
  307. tx.name = name;
  308. memset(&rx, 0, sizeof rx);
  309. nfs3Call(a, &rx.call, Nfs3CallRRmdir);
  310. if(sunClientRpc(nfscli, tag, &tx.call, &rx.call, nil) < 0)
  311. return -1;
  312. if(rx.status != Nfs3Ok){
  313. nfs3Errstr(rx.status);
  314. return -1;
  315. }
  316. return 0;
  317. }
  318. int
  319. nfsRemove(Auth *a, ulong tag, Nfs3Handle *h, char *name)
  320. {
  321. Nfs3TRemove tx;
  322. Nfs3RRemove rx;
  323. memset(&tx, 0, sizeof tx);
  324. nfs3Call(a, &tx.call, Nfs3CallTRemove);
  325. tx.handle = *h;
  326. tx.name = name;
  327. memset(&rx, 0, sizeof rx);
  328. nfs3Call(a, &rx.call, Nfs3CallRRemove);
  329. if(sunClientRpc(nfscli, tag, &tx.call, &rx.call, nil) < 0)
  330. return -1;
  331. if(rx.status != Nfs3Ok){
  332. nfs3Errstr(rx.status);
  333. return -1;
  334. }
  335. return 0;
  336. }
  337. int
  338. nfsRename(Auth *a, ulong tag, Nfs3Handle *h, char *name, Nfs3Handle *th, char *tname)
  339. {
  340. Nfs3TRename tx;
  341. Nfs3RRename rx;
  342. memset(&tx, 0, sizeof tx);
  343. nfs3Call(a, &tx.call, Nfs3CallTRename);
  344. tx.from.handle = *h;
  345. tx.from.name = name;
  346. tx.to.handle = *th;
  347. tx.to.name = tname;
  348. memset(&rx, 0, sizeof rx);
  349. nfs3Call(a, &rx.call, Nfs3CallRRename);
  350. if(sunClientRpc(nfscli, tag, &tx.call, &rx.call, nil) < 0)
  351. return -1;
  352. if(rx.status != Nfs3Ok){
  353. nfs3Errstr(rx.status);
  354. return -1;
  355. }
  356. return 0;
  357. }
  358. int
  359. nfsSetattr(Auth *a, ulong tag, Nfs3Handle *h, Nfs3SetAttr *attr)
  360. {
  361. Nfs3TSetattr tx;
  362. Nfs3RSetattr rx;
  363. memset(&tx, 0, sizeof tx);
  364. nfs3Call(a, &tx.call, Nfs3CallTSetattr);
  365. tx.handle = *h;
  366. tx.attr = *attr;
  367. memset(&rx, 0, sizeof rx);
  368. nfs3Call(a, &rx.call, Nfs3CallRSetattr);
  369. if(sunClientRpc(nfscli, tag, &tx.call, &rx.call, nil) < 0)
  370. return -1;
  371. if(rx.status != Nfs3Ok){
  372. nfs3Errstr(rx.status);
  373. return -1;
  374. }
  375. return 0;
  376. }
  377. int
  378. nfsCommit(Auth *a, ulong tag, Nfs3Handle *h)
  379. {
  380. Nfs3TCommit tx;
  381. Nfs3RCommit rx;
  382. memset(&tx, 0, sizeof tx);
  383. nfs3Call(a, &tx.call, Nfs3CallTCommit);
  384. tx.handle = *h;
  385. memset(&rx, 0, sizeof rx);
  386. nfs3Call(a, &rx.call, Nfs3CallRCommit);
  387. if(sunClientRpc(nfscli, tag, &tx.call, &rx.call, nil) < 0)
  388. return -1;
  389. if(rx.status != Nfs3Ok){
  390. nfs3Errstr(rx.status);
  391. return -1;
  392. }
  393. return 0;
  394. }
  395. int
  396. nfsLookup(Auth *a, ulong tag, Nfs3Handle *h, char *name, Nfs3Handle *nh, u1int *have, Nfs3Attr *attr)
  397. {
  398. Nfs3TLookup tx;
  399. Nfs3RLookup rx;
  400. memset(&tx, 0, sizeof tx);
  401. nfs3Call(a, &tx.call, Nfs3CallTLookup);
  402. tx.handle = *h;
  403. tx.name = name;
  404. memset(&rx, 0, sizeof rx);
  405. nfs3Call(a, &rx.call, Nfs3CallRLookup);
  406. if(sunClientRpc(nfscli, tag, &tx.call, &rx.call, nil) < 0)
  407. return -1;
  408. if(rx.status != Nfs3Ok){
  409. nfs3Errstr(rx.status);
  410. return -1;
  411. }
  412. *nh = rx.handle;
  413. *have = rx.haveAttr;
  414. if(rx.haveAttr)
  415. *attr = rx.attr;
  416. return 0;
  417. }
  418. int
  419. nfsReadDirPlus(Auth *a, ulong tag, Nfs3Handle *h, u32int count, u64int cookie, uchar **pp,
  420. u32int *pcount, int (**unpack)(uchar*, uchar*, uchar**, Nfs3Entry*), uchar **pfreeme)
  421. {
  422. Nfs3TReadDirPlus tx;
  423. Nfs3RReadDirPlus rx;
  424. memset(&tx, 0, sizeof tx);
  425. nfs3Call(a, &tx.call, Nfs3CallTReadDirPlus);
  426. tx.handle = *h;
  427. tx.maxCount = count;
  428. tx.dirCount = 1000;
  429. tx.cookie = cookie;
  430. memset(&rx, 0, sizeof rx);
  431. nfs3Call(a, &rx.call, Nfs3CallRReadDirPlus);
  432. if(sunClientRpc(nfscli, tag, &tx.call, &rx.call, pfreeme) < 0)
  433. return -1;
  434. if(rx.status != Nfs3Ok){
  435. free(*pfreeme);
  436. *pfreeme = 0;
  437. nfs3Errstr(rx.status);
  438. return -1;
  439. }
  440. *unpack = nfs3EntryPlusUnpack;
  441. *pcount = rx.count;
  442. *pp = rx.data;
  443. return 0;
  444. }
  445. int
  446. nfsReadDir(Auth *a, ulong tag, Nfs3Handle *h, u32int count, u64int cookie, uchar **pp,
  447. u32int *pcount, int (**unpack)(uchar*, uchar*, uchar**, Nfs3Entry*), uchar **pfreeme)
  448. {
  449. /* BUG: try readdirplus */
  450. char e[ERRMAX];
  451. Nfs3TReadDir tx;
  452. Nfs3RReadDir rx;
  453. if(readplus!=-1){
  454. if(nfsReadDirPlus(a, tag, h, count, cookie, pp, pcount, unpack, pfreeme) == 0){
  455. readplus = 1;
  456. return 0;
  457. }
  458. fprint(2, "readdirplus: %r\n");
  459. if(readplus == 0){
  460. rerrstr(e, sizeof e);
  461. if(strstr(e, "procedure unavailable"))
  462. readplus = -1;
  463. }
  464. }
  465. if(readplus == 1)
  466. return -1;
  467. memset(&tx, 0, sizeof tx);
  468. nfs3Call(a, &tx.call, Nfs3CallTReadDir);
  469. tx.handle = *h;
  470. tx.count = count;
  471. tx.cookie = cookie;
  472. memset(&rx, 0, sizeof rx);
  473. nfs3Call(a, &rx.call, Nfs3CallRReadDir);
  474. if(sunClientRpc(nfscli, tag, &tx.call, &rx.call, pfreeme) < 0)
  475. return -1;
  476. if(rx.status != Nfs3Ok){
  477. free(*pfreeme);
  478. *pfreeme = 0;
  479. nfs3Errstr(rx.status);
  480. return -1;
  481. }
  482. /* readplus failed but read succeeded */
  483. readplus = -1;
  484. *unpack = nfs3EntryUnpack;
  485. *pcount = rx.count;
  486. *pp = rx.data;
  487. return 0;
  488. }
  489. /*
  490. * name <-> int translation
  491. */
  492. typedef struct Map Map;
  493. typedef struct User User;
  494. typedef struct Group Group;
  495. Map *map;
  496. Map emptymap;
  497. struct User
  498. {
  499. char *name;
  500. uint uid;
  501. uint gid;
  502. uint g[16];
  503. uint ng;
  504. uchar *auth;
  505. int nauth;
  506. };
  507. struct Group
  508. {
  509. char *name; /* same pos as in User struct */
  510. uint gid; /* same pos as in User struct */
  511. };
  512. struct Map
  513. {
  514. int nuser;
  515. int ngroup;
  516. User *user;
  517. User **ubyname;
  518. User **ubyid;
  519. Group *group;
  520. Group **gbyname;
  521. Group **gbyid;
  522. };
  523. User*
  524. finduser(User **u, int nu, char *s)
  525. {
  526. int lo, hi, mid, n;
  527. hi = nu;
  528. lo = 0;
  529. while(hi > lo){
  530. mid = (lo+hi)/2;
  531. n = strcmp(u[mid]->name, s);
  532. if(n == 0)
  533. return u[mid];
  534. if(n < 0)
  535. lo = mid+1;
  536. else
  537. hi = mid;
  538. }
  539. return nil;
  540. }
  541. int
  542. strtoid(User **u, int nu, char *s, u32int *id)
  543. {
  544. u32int x;
  545. char *p;
  546. User *uu;
  547. x = strtoul(s, &p, 10);
  548. if(*s != 0 && *p == 0){
  549. *id = x;
  550. return 0;
  551. }
  552. uu = finduser(u, nu, s);
  553. if(uu == nil)
  554. return -1;
  555. *id = uu->uid;
  556. return 0;
  557. }
  558. char*
  559. idtostr(User **u, int nu, u32int id)
  560. {
  561. char buf[32];
  562. int lo, hi, mid;
  563. hi = nu;
  564. lo = 0;
  565. while(hi > lo){
  566. mid = (lo+hi)/2;
  567. if(u[mid]->uid == id)
  568. return estrdup9p(u[mid]->name);
  569. if(u[mid]->uid < id)
  570. lo = mid+1;
  571. else
  572. hi = mid;
  573. }
  574. snprint(buf, sizeof buf, "%ud", id);
  575. return estrdup9p(buf);
  576. }
  577. char*
  578. uidtostr(u32int uid)
  579. {
  580. return idtostr(map->ubyid, map->nuser, uid);
  581. }
  582. char*
  583. gidtostr(u32int gid)
  584. {
  585. return idtostr((User**)map->gbyid, map->ngroup, gid);
  586. }
  587. int
  588. strtouid(char *s, u32int *id)
  589. {
  590. return strtoid(map->ubyname, map->nuser, s, id);
  591. }
  592. int
  593. strtogid(char *s, u32int *id)
  594. {
  595. return strtoid((User**)map->gbyid, map->ngroup, s, id);
  596. }
  597. int
  598. idcmp(const void *va, const void *vb)
  599. {
  600. User **a, **b;
  601. a = (User**)va;
  602. b = (User**)vb;
  603. return (*a)->uid - (*b)->uid;
  604. }
  605. int
  606. namecmp(const void *va, const void *vb)
  607. {
  608. User **a, **b;
  609. a = (User**)va;
  610. b = (User**)vb;
  611. return strcmp((*a)->name, (*b)->name);
  612. }
  613. void
  614. closemap(Map *m)
  615. {
  616. int i;
  617. for(i=0; i<m->nuser; i++){
  618. free(m->user[i].name);
  619. free(m->user[i].auth);
  620. }
  621. for(i=0; i<m->ngroup; i++)
  622. free(m->group[i].name);
  623. free(m->user);
  624. free(m->group);
  625. free(m->ubyid);
  626. free(m->ubyname);
  627. free(m->gbyid);
  628. free(m->gbyname);
  629. free(m);
  630. }
  631. Map*
  632. readmap(char *passwd, char *group)
  633. {
  634. char *s, *f[10], *p, *nextp, *name;
  635. uchar *q, *eq;
  636. int i, n, nf, line, uid, gid;
  637. Biobuf *b;
  638. Map *m;
  639. User *u;
  640. Group *g;
  641. SunAuthUnix au;
  642. m = emalloc(sizeof(Map));
  643. if((b = Bopen(passwd, OREAD)) == nil){
  644. free(m);
  645. return nil;
  646. }
  647. line = 0;
  648. for(; (s = Brdstr(b, '\n', 1)) != nil; free(s)){
  649. line++;
  650. if(s[0] == '#')
  651. continue;
  652. nf = getfields(s, f, nelem(f), 0, ":");
  653. if(nf < 4)
  654. continue;
  655. name = f[0];
  656. uid = strtol(f[2], &p, 10);
  657. if(f[2][0] == 0 || *p != 0){
  658. fprint(2, "%s:%d: non-numeric id in third field\n", passwd, line);
  659. continue;
  660. }
  661. gid = strtol(f[3], &p, 10);
  662. if(f[3][0] == 0 || *p != 0){
  663. fprint(2, "%s:%d: non-numeric id in fourth field\n", passwd, line);
  664. continue;
  665. }
  666. if(m->nuser%32 == 0)
  667. m->user = erealloc(m->user, (m->nuser+32)*sizeof(m->user[0]));
  668. u = &m->user[m->nuser++];
  669. u->name = estrdup9p(name);
  670. u->uid = uid;
  671. u->gid = gid;
  672. u->ng = 0;
  673. u->auth = 0;
  674. u->nauth = 0;
  675. }
  676. Bterm(b);
  677. m->ubyname = emalloc(m->nuser*sizeof(User*));
  678. m->ubyid = emalloc(m->nuser*sizeof(User*));
  679. for(i=0; i<m->nuser; i++){
  680. m->ubyname[i] = &m->user[i];
  681. m->ubyid[i] = &m->user[i];
  682. }
  683. qsort(m->ubyname, m->nuser, sizeof(m->ubyname[0]), namecmp);
  684. qsort(m->ubyid, m->nuser, sizeof(m->ubyid[0]), idcmp);
  685. if((b = Bopen(group, OREAD)) == nil){
  686. closemap(m);
  687. return nil;
  688. }
  689. line = 0;
  690. for(; (s = Brdstr(b, '\n', 1)) != nil; free(s)){
  691. line++;
  692. if(s[0] == '#')
  693. continue;
  694. nf = getfields(s, f, nelem(f), 0, ":");
  695. if(nf < 4)
  696. continue;
  697. name = f[0];
  698. gid = strtol(f[2], &p, 10);
  699. if(f[2][0] == 0 || *p != 0){
  700. fprint(2, "%s:%d: non-numeric id in third field\n", group, line);
  701. continue;
  702. }
  703. if(m->ngroup%32 == 0)
  704. m->group = erealloc(m->group, (m->ngroup+32)*sizeof(m->group[0]));
  705. g = &m->group[m->ngroup++];
  706. g->name = estrdup9p(name);
  707. g->gid = gid;
  708. for(p=f[3]; *p; p=nextp){
  709. if((nextp = strchr(p, ',')) != nil)
  710. *nextp++ = 0;
  711. else
  712. nextp = p+strlen(p);
  713. u = finduser(m->ubyname, m->nuser, p);
  714. if(u == nil){
  715. if(verbose)
  716. fprint(2, "%s:%d: unknown user %s\n", group, line, p);
  717. continue;
  718. }
  719. if(u->ng >= nelem(u->g)){
  720. fprint(2, "%s:%d: user %s is in too many groups; ignoring %s\n", group, line, p, name);
  721. continue;
  722. }
  723. u->g[u->ng++] = gid;
  724. }
  725. }
  726. Bterm(b);
  727. m->gbyname = emalloc(m->ngroup*sizeof(Group*));
  728. m->gbyid = emalloc(m->ngroup*sizeof(Group*));
  729. for(i=0; i<m->ngroup; i++){
  730. m->gbyname[i] = &m->group[i];
  731. m->gbyid[i] = &m->group[i];
  732. }
  733. qsort(m->gbyname, m->ngroup, sizeof(m->gbyname[0]), namecmp);
  734. qsort(m->gbyid, m->ngroup, sizeof(m->gbyid[0]), idcmp);
  735. for(i=0; i<m->nuser; i++){
  736. au.stamp = 0;
  737. au.sysname = sys;
  738. au.uid = m->user[i].uid;
  739. au.gid = m->user[i].gid;
  740. memmove(au.g, m->user[i].g, sizeof au.g);
  741. au.ng = m->user[i].ng;
  742. n = sunAuthUnixSize(&au);
  743. q = emalloc(n);
  744. eq = q+n;
  745. m->user[i].auth = q;
  746. m->user[i].nauth = n;
  747. if(sunAuthUnixPack(q, eq, &q, &au) < 0 || q != eq){
  748. fprint(2, "sunAuthUnixPack failed for %s\n", m->user[i].name);
  749. free(m->user[i].auth);
  750. m->user[i].auth = 0;
  751. m->user[i].nauth = 0;
  752. }
  753. }
  754. return m;
  755. }
  756. Auth*
  757. mkauth(char *user)
  758. {
  759. Auth *a;
  760. uchar *p;
  761. int n;
  762. SunAuthUnix au;
  763. User *u;
  764. u = finduser(map->ubyname, map->nuser, user);
  765. if(u == nil || u->nauth == 0){
  766. /* nobody */
  767. au.stamp = 0;
  768. au.uid = -1;
  769. au.gid = -1;
  770. au.ng = 0;
  771. au.sysname = sys;
  772. n = sunAuthUnixSize(&au);
  773. a = emalloc(sizeof(Auth)+n);
  774. a->data = (uchar*)&a[1];
  775. a->ndata = n;
  776. if(sunAuthUnixPack(a->data, a->data+a->ndata, &p, &au) < 0
  777. || p != a->data+a->ndata){
  778. free(a);
  779. return nil;
  780. }
  781. a->ref = 1;
  782. if(verbose)print("creds for %s: %.*H\n", user, a->ndata, a->data);
  783. return a;
  784. }
  785. a = emalloc(sizeof(Auth)+u->nauth);
  786. a->data = (uchar*)&a[1];
  787. a->ndata = u->nauth;
  788. memmove(a->data, u->auth, a->ndata);
  789. a->ref = 1;
  790. if(verbose)print("creds for %s: %.*H\n", user, a->ndata, a->data);
  791. return a;
  792. }
  793. void
  794. freeauth(Auth *a)
  795. {
  796. if(--a->ref > 0)
  797. return;
  798. free(a);
  799. }
  800. /*
  801. * 9P server
  802. */
  803. void
  804. responderrstr(Req *r)
  805. {
  806. char e[ERRMAX];
  807. rerrstr(e, sizeof e);
  808. respond(r, e);
  809. }
  810. void
  811. fsdestroyfid(Fid *fid)
  812. {
  813. FidAux *aux;
  814. aux = fid->aux;
  815. if(aux == nil)
  816. return;
  817. freeauth(aux->auth);
  818. free(aux->name);
  819. free(aux);
  820. }
  821. void
  822. attrToQid(Nfs3Attr *attr, Qid *qid)
  823. {
  824. qid->path = attr->fileid;
  825. qid->vers = attr->mtime.sec;
  826. qid->type = 0;
  827. if(attr->type == Nfs3FileDir)
  828. qid->type |= QTDIR;
  829. }
  830. void
  831. attrToDir(Nfs3Attr *attr, Dir *d)
  832. {
  833. d->mode = attr->mode & 0777;
  834. if(attr->type == Nfs3FileDir)
  835. d->mode |= DMDIR;
  836. d->uid = uidtostr(attr->uid);
  837. d->gid = gidtostr(attr->gid);
  838. d->length = attr->size;
  839. attrToQid(attr, &d->qid);
  840. d->mtime = attr->mtime.sec;
  841. d->atime = attr->atime.sec;
  842. d->muid = nil;
  843. }
  844. void
  845. fsattach(Req *r)
  846. {
  847. char *path;
  848. Auth *auth;
  849. FidAux *aux;
  850. Nfs3Attr attr;
  851. Nfs3Handle h;
  852. path = r->ifcall.aname;
  853. if(path==nil || path[0]==0)
  854. path = defaultpath;
  855. auth = mkauth(r->ifcall.uname);
  856. if(mountMnt(auth, r->tag, path, &h) < 0
  857. || nfsGetattr(auth, r->tag, &h, &attr) < 0){
  858. freeauth(auth);
  859. responderrstr(r);
  860. return;
  861. }
  862. aux = emalloc(sizeof(FidAux));
  863. aux->auth = auth;
  864. aux->handle = h;
  865. aux->cookie = 0;
  866. aux->name = nil;
  867. memset(&aux->parent, 0, sizeof aux->parent);
  868. r->fid->aux = aux;
  869. attrToQid(&attr, &r->fid->qid);
  870. r->ofcall.qid = r->fid->qid;
  871. respond(r, nil);
  872. }
  873. void
  874. fsopen(Req *r)
  875. {
  876. FidAux *aux;
  877. Nfs3Attr attr;
  878. u1int have;
  879. ulong a, b;
  880. aux = r->fid->aux;
  881. a = 0;
  882. switch(r->ifcall.mode&OMASK){
  883. case OREAD:
  884. a = 1;
  885. break;
  886. case OWRITE:
  887. a = 2;
  888. break;
  889. case ORDWR:
  890. a = 3;
  891. break;
  892. case OEXEC:
  893. a = 0x20;
  894. break;
  895. }
  896. if(r->ifcall.mode&OTRUNC)
  897. a = 0x10;
  898. if(nfsAccess(aux->auth, r->tag, &aux->handle, a, &b, &have, &attr) < 0
  899. || (!have && nfsGetattr(aux->auth, r->tag, &aux->handle, &attr) < 0)){
  900. responderrstr(r);
  901. return;
  902. }
  903. attrToQid(&attr, &r->fid->qid);
  904. r->ofcall.qid = r->fid->qid;
  905. respond(r, nil);
  906. }
  907. void
  908. fscreate(Req *r)
  909. {
  910. FidAux *aux;
  911. u1int have;
  912. Nfs3Attr attr;
  913. Nfs3Handle h;
  914. ulong mode;
  915. uint gid;
  916. int (*mk)(Auth*, ulong, Nfs3Handle*, char*, Nfs3Handle*, ulong, uint, u1int*, Nfs3Attr*);
  917. aux = r->fid->aux;
  918. /*
  919. * Plan 9 has no umask, so let's use the
  920. * parent directory bits like Plan 9 does.
  921. * What the heck, let's inherit the group too.
  922. * (Unix will let us set the group to anything
  923. * since we're the owner!)
  924. */
  925. if(nfsGetattr(aux->auth, r->tag, &aux->handle, &attr) < 0){
  926. responderrstr(r);
  927. return;
  928. }
  929. mode = r->ifcall.perm&0777;
  930. if(r->ifcall.perm&DMDIR)
  931. mode &= (attr.mode&0666) | ~0666;
  932. else
  933. mode &= (attr.mode&0777) | ~0777;
  934. gid = attr.gid;
  935. if(r->ifcall.perm&DMDIR)
  936. mk = nfsMkdir;
  937. else
  938. mk = nfsCreate;
  939. if((*mk)(aux->auth, r->tag, &aux->handle, r->ifcall.name, &h, mode, gid, &have, &attr) < 0
  940. || (!have && nfsGetattr(aux->auth, r->tag, &h, &attr) < 0)){
  941. responderrstr(r);
  942. return;
  943. }
  944. attrToQid(&attr, &r->fid->qid);
  945. aux->parent = aux->handle;
  946. aux->handle = h;
  947. free(aux->name);
  948. aux->name = estrdup9p(r->ifcall.name);
  949. r->ofcall.qid = r->fid->qid;
  950. respond(r, nil);
  951. }
  952. void
  953. fsreaddir(Req *r)
  954. {
  955. FidAux *aux;
  956. uchar *p, *freeme, *ep, *p9, *ep9;
  957. uint count;
  958. int n, (*unpack)(uchar*, uchar*, uchar**, Nfs3Entry*);
  959. Nfs3Entry e;
  960. u64int cookie;
  961. Dir d;
  962. aux = r->fid->aux;
  963. /*
  964. * r->ifcall.count seems a reasonable estimate to
  965. * how much NFS entry data we want. is it?
  966. */
  967. if(r->ifcall.offset)
  968. cookie = aux->cookie;
  969. else
  970. cookie = 0;
  971. if(nfsReadDir(aux->auth, r->tag, &aux->handle, r->ifcall.count, cookie,
  972. &p, &count, &unpack, &freeme) < 0){
  973. responderrstr(r);
  974. return;
  975. }
  976. ep = p+count;
  977. p9 = (uchar*)r->ofcall.data;
  978. ep9 = p9+r->ifcall.count;
  979. /*
  980. * BUG: Issue all of the stat requests in parallel.
  981. */
  982. while(p < ep && p9 < ep9){
  983. if((*unpack)(p, ep, &p, &e) < 0)
  984. break;
  985. aux->cookie = e.cookie;
  986. if(strcmp(e.name, ".") == 0 || strcmp(e.name, "..") == 0)
  987. continue;
  988. if(!e.haveAttr && !e.haveHandle)
  989. if(nfsLookup(aux->auth, r->tag, &aux->handle, e.name, &e.handle, &e.haveAttr, &e.attr) < 0)
  990. continue;
  991. if(!e.haveAttr)
  992. if(nfsGetattr(aux->auth, r->tag, &e.handle, &e.attr) < 0)
  993. continue;
  994. memset(&d, 0, sizeof d);
  995. attrToDir(&e.attr, &d);
  996. d.name = e.name;
  997. if((n = convD2M(&d, p9, ep9-p9)) <= BIT16SZ)
  998. break;
  999. p9 += n;
  1000. }
  1001. free(freeme);
  1002. r->ofcall.count = p9 - (uchar*)r->ofcall.data;
  1003. respond(r, nil);
  1004. }
  1005. void
  1006. fsread(Req *r)
  1007. {
  1008. uchar *p, *freeme;
  1009. uint count;
  1010. FidAux *aux;
  1011. if(r->fid->qid.type&QTDIR){
  1012. fsreaddir(r);
  1013. return;
  1014. }
  1015. aux = r->fid->aux;
  1016. if(nfsRead(aux->auth, r->tag, &aux->handle, r->ifcall.count, r->ifcall.offset, &p, &count, &freeme) < 0){
  1017. responderrstr(r);
  1018. return;
  1019. }
  1020. r->ofcall.data = (char*)p;
  1021. r->ofcall.count = count;
  1022. respond(r, nil);
  1023. free(freeme);
  1024. }
  1025. void
  1026. fswrite(Req *r)
  1027. {
  1028. uint count;
  1029. FidAux *aux;
  1030. aux = r->fid->aux;
  1031. if(nfsWrite(aux->auth, r->tag, &aux->handle, (uchar*)r->ifcall.data, r->ifcall.count, r->ifcall.offset, &count) < 0){
  1032. responderrstr(r);
  1033. return;
  1034. }
  1035. r->ofcall.count = count;
  1036. respond(r, nil);
  1037. }
  1038. void
  1039. fsremove(Req *r)
  1040. {
  1041. int n;
  1042. FidAux *aux;
  1043. aux = r->fid->aux;
  1044. if(aux->name == nil){
  1045. respond(r, "nfs3client botch -- don't know parent handle in remove");
  1046. return;
  1047. }
  1048. if(r->fid->qid.type&QTDIR)
  1049. n = nfsRmdir(aux->auth, r->tag, &aux->parent, aux->name);
  1050. else
  1051. n = nfsRemove(aux->auth, r->tag, &aux->parent, aux->name);
  1052. if(n < 0){
  1053. responderrstr(r);
  1054. return;
  1055. }
  1056. respond(r, nil);
  1057. }
  1058. void
  1059. fsstat(Req *r)
  1060. {
  1061. FidAux *aux;
  1062. Nfs3Attr attr;
  1063. aux = r->fid->aux;
  1064. if(nfsGetattr(aux->auth, r->tag, &aux->handle, &attr) < 0){
  1065. responderrstr(r);
  1066. return;
  1067. }
  1068. memset(&r->d, 0, sizeof r->d);
  1069. attrToDir(&attr, &r->d);
  1070. r->d.name = estrdup9p(aux->name ? aux->name : "???");
  1071. respond(r, nil);
  1072. }
  1073. void
  1074. fswstat(Req *r)
  1075. {
  1076. int op, sync;
  1077. FidAux *aux;
  1078. Nfs3SetAttr attr;
  1079. aux = r->fid->aux;
  1080. /* Fill out stat first to catch errors */
  1081. op = 0;
  1082. sync = 1;
  1083. if(~r->d.mode){
  1084. if(r->d.mode&(DMAPPEND|DMEXCL)){
  1085. respond(r, "wstat -- DMAPPEND and DMEXCL bits not supported");
  1086. return;
  1087. }
  1088. op = 1;
  1089. sync = 0;
  1090. attr.setMode = 1;
  1091. attr.mode = r->d.mode & 0777;
  1092. }
  1093. if(r->d.uid && r->d.uid[0]){
  1094. attr.setUid = 1;
  1095. if(strtouid(r->d.uid, &attr.uid) < 0){
  1096. respond(r, "wstat -- unknown uid");
  1097. return;
  1098. }
  1099. op = 1;
  1100. sync = 0;
  1101. }
  1102. if(r->d.gid && r->d.gid[0]){
  1103. attr.setGid = 1;
  1104. if(strtogid(r->d.gid, &attr.gid) < 0){
  1105. respond(r, "wstat -- unknown gid");
  1106. return;
  1107. }
  1108. op = 1;
  1109. sync = 0;
  1110. }
  1111. if(~r->d.length){
  1112. attr.setSize = 1;
  1113. attr.size = r->d.length;
  1114. }
  1115. if(~r->d.mtime){
  1116. attr.setMtime = 1;
  1117. attr.mtime.sec = r->d.mtime;
  1118. }
  1119. /* Try rename first because it's more likely to fail (?) */
  1120. if(r->d.name && r->d.name[0]){
  1121. if(aux->name == nil){
  1122. respond(r, "nfsclient botch -- don't know parent handle in rename");
  1123. return;
  1124. }
  1125. if(nfsRename(aux->auth, r->tag, &aux->parent, aux->name, &aux->parent, r->d.name) < 0){
  1126. responderrstr(r);
  1127. return;
  1128. }
  1129. free(aux->name);
  1130. aux->name = estrdup9p(r->d.name);
  1131. sync = 0;
  1132. }
  1133. /*
  1134. * Now we have a problem. The rename succeeded
  1135. * but the setattr could fail. Sic transit atomicity.
  1136. */
  1137. if(op){
  1138. if(nfsSetattr(aux->auth, r->tag, &aux->handle, &attr) < 0){
  1139. responderrstr(r);
  1140. return;
  1141. }
  1142. }
  1143. if(sync){
  1144. /* NFS commit */
  1145. if(nfsCommit(aux->auth, r->tag, &aux->handle) < 0){
  1146. responderrstr(r);
  1147. return;
  1148. }
  1149. }
  1150. respond(r, nil);
  1151. }
  1152. char*
  1153. fswalk1(Fid *fid, char *name, void *v)
  1154. {
  1155. u1int have;
  1156. ulong tag;
  1157. FidAux *aux;
  1158. Nfs3Attr attr;
  1159. Nfs3Handle h;
  1160. tag = *(ulong*)v;
  1161. aux = fid->aux;
  1162. if(nfsLookup(aux->auth, tag, &aux->handle, name, &h, &have, &attr) < 0
  1163. || (!have && nfsGetattr(aux->auth, tag, &h, &attr) < 0)){
  1164. rerrstr(aux->err, sizeof aux->err);
  1165. return aux->err;
  1166. }
  1167. aux->parent = aux->handle;
  1168. aux->handle = h;
  1169. free(aux->name);
  1170. if(strcmp(name, "..") == 0)
  1171. aux->name = nil;
  1172. else
  1173. aux->name = estrdup9p(name);
  1174. attrToQid(&attr, &fid->qid);
  1175. return nil;
  1176. }
  1177. char*
  1178. fsclone(Fid *fid, Fid *newfid, void*)
  1179. {
  1180. FidAux *a, *na;
  1181. a = fid->aux;
  1182. na = emalloc9p(sizeof(FidAux));
  1183. *na = *a;
  1184. if(na->name)
  1185. na->name = estrdup9p(na->name);
  1186. newfid->aux = na;
  1187. if(na->auth)
  1188. na->auth->ref++;
  1189. return nil;
  1190. }
  1191. void
  1192. fswalk(Req *r)
  1193. {
  1194. walkandclone(r, fswalk1, fsclone, &r->tag);
  1195. }
  1196. void
  1197. fsflush(Req *r)
  1198. {
  1199. Req *or;
  1200. /*
  1201. * Send on the flush channel(s).
  1202. * The library will make sure the response
  1203. * is delayed as necessary.
  1204. */
  1205. or = r->oldreq;
  1206. if(nfscli)
  1207. sendul(nfscli->flushchan, (ulong)or->tag);
  1208. if(mntcli)
  1209. sendul(mntcli->flushchan, (ulong)or->tag);
  1210. respond(r, nil);
  1211. }
  1212. void
  1213. fsdispatch(void *v)
  1214. {
  1215. Req *r;
  1216. r = v;
  1217. switch(r->ifcall.type){
  1218. default: respond(r, "unknown type"); break;
  1219. case Tattach: fsattach(r); break;
  1220. case Topen: fsopen(r); break;
  1221. case Tcreate: fscreate(r); break;
  1222. case Tread: fsread(r); break;
  1223. case Twrite: fswrite(r); break;
  1224. case Tremove: fsremove(r); break;
  1225. case Tflush: fsflush(r); break;
  1226. case Tstat: fsstat(r); break;
  1227. case Twstat: fswstat(r); break;
  1228. case Twalk: fswalk(r); break;
  1229. }
  1230. }
  1231. void
  1232. fsthread(void*)
  1233. {
  1234. Req *r;
  1235. while((r = recvp(fschan)) != nil)
  1236. threadcreate(fsdispatch, r, SunStackSize);
  1237. }
  1238. void
  1239. fssend(Req *r)
  1240. {
  1241. sendp(fschan, r);
  1242. }
  1243. void
  1244. fsdie(Srv*)
  1245. {
  1246. threadexitsall(nil);
  1247. }
  1248. Srv fs =
  1249. {
  1250. .destroyfid = fsdestroyfid,
  1251. .attach= fssend,
  1252. .open= fssend,
  1253. .create= fssend,
  1254. .read= fssend,
  1255. .write= fssend,
  1256. .remove= fssend,
  1257. .flush= fssend,
  1258. .stat= fssend,
  1259. .wstat= fssend,
  1260. .walk= fssend,
  1261. .end= fsdie
  1262. };
  1263. void
  1264. usage(void)
  1265. {
  1266. fprint(2, "usage: nfs [-DRv] [-p perm] [-s srvname] [-u passwd group] addr [addr]\n");
  1267. fprint(2, "\taddr - address of portmapper server\n");
  1268. fprint(2, "\taddr addr - addresses of mount server and nfs server\n");
  1269. exits("usage");
  1270. }
  1271. char*
  1272. netchangeport(char *addr, uint port, char *buf, uint nbuf)
  1273. {
  1274. char *r;
  1275. strecpy(buf, buf+nbuf, addr);
  1276. r = strrchr(buf, '!');
  1277. if(r == nil)
  1278. return nil;
  1279. r++;
  1280. seprint(r, buf+nbuf, "%ud", port);
  1281. return buf;
  1282. }
  1283. char mbuf[256], nbuf[256];
  1284. char *mountaddr, *nfsaddr;
  1285. Channel *csync;
  1286. int chattyrpc;
  1287. void dialproc(void*);
  1288. void
  1289. threadmain(int argc, char **argv)
  1290. {
  1291. char *srvname, *passwd, *group, *addr, *p;
  1292. SunClient *cli;
  1293. int proto;
  1294. uint mport, nport;
  1295. ulong perm;
  1296. Dir d;
  1297. perm = 0600;
  1298. passwd = nil;
  1299. group = nil;
  1300. srvname = nil;
  1301. sys = sysname();
  1302. if(sys == nil)
  1303. sys = "plan9";
  1304. ARGBEGIN{
  1305. default:
  1306. usage();
  1307. case 'D':
  1308. chatty9p++;
  1309. break;
  1310. case 'R':
  1311. chattyrpc++;
  1312. break;
  1313. case 'p':
  1314. perm = strtol(EARGF(usage()), &p, 8);
  1315. if(perm==0 || *p != 0)
  1316. usage();
  1317. break;
  1318. case 's':
  1319. srvname = EARGF(usage());
  1320. break;
  1321. case 'u':
  1322. passwd = EARGF(usage());
  1323. group = EARGF(usage());
  1324. break;
  1325. case 'v':
  1326. verbose++;
  1327. break;
  1328. }ARGEND
  1329. if(argc != 1 && argc != 2)
  1330. usage();
  1331. if(srvname == nil)
  1332. srvname = argv[0];
  1333. fmtinstall('B', sunRpcFmt);
  1334. fmtinstall('C', sunCallFmt);
  1335. fmtinstall('H', encodefmt);
  1336. sunFmtInstall(&portProg);
  1337. sunFmtInstall(&nfs3Prog);
  1338. sunFmtInstall(&nfsMount3Prog);
  1339. if(passwd && (map = readmap(passwd, group)) == nil)
  1340. fprint(2, "warning: reading %s and %s: %r\n", passwd, group);
  1341. if(map == nil)
  1342. map = &emptymap;
  1343. if(argc == 1){
  1344. addr = netmkaddr(argv[0], "udp", "portmap");
  1345. if((cli = sunDial(addr)) == nil)
  1346. sysfatal("dial %s: %r", addr);
  1347. cli->chatty = chattyrpc;
  1348. sunClientProg(cli, &portProg);
  1349. if(strstr(addr, "udp!"))
  1350. proto = PortProtoUdp;
  1351. else
  1352. proto = PortProtoTcp;
  1353. if(getport(cli, NfsMount3Program, NfsMount3Version, proto, &mport) < 0)
  1354. sysfatal("lookup mount program port: %r");
  1355. if(getport(cli, Nfs3Program, Nfs3Version, proto, &nport) < 0)
  1356. sysfatal("lookup nfs program port: %r");
  1357. sunClientClose(cli);
  1358. mountaddr = netchangeport(addr, mport, mbuf, sizeof mbuf);
  1359. nfsaddr = netchangeport(addr, nport, nbuf, sizeof nbuf);
  1360. strcat(mountaddr, "!r");
  1361. strcat(nfsaddr, "!r");
  1362. if(verbose)
  1363. fprint(2, "nfs %s %s\n", mountaddr, nfsaddr);
  1364. }else{
  1365. mountaddr = argv[0];
  1366. nfsaddr = argv[1];
  1367. }
  1368. /* have to dial in another proc because it creates threads */
  1369. csync = chancreate(sizeof(void*), 0);
  1370. proccreate(dialproc, nil, SunStackSize);
  1371. recvp(csync);
  1372. threadpostmountsrv(&fs, srvname, nil, 0);
  1373. if(perm != 0600){
  1374. p = smprint("/srv/%s", srvname);
  1375. if(p){
  1376. nulldir(&d);
  1377. d.mode = perm;
  1378. dirwstat(p, &d);
  1379. }
  1380. }
  1381. threadexits(nil);
  1382. }
  1383. void
  1384. dialproc(void*)
  1385. {
  1386. rfork(RFNAMEG);
  1387. rfork(RFNOTEG);
  1388. if((mntcli = sunDial(mountaddr)) == nil)
  1389. sysfatal("dial mount program at %s: %r", mountaddr);
  1390. mntcli->chatty = chattyrpc;
  1391. sunClientProg(mntcli, &nfsMount3Prog);
  1392. if(mountNull(0) < 0)
  1393. sysfatal("execute nop with mnt server at %s: %r", mountaddr);
  1394. if((nfscli = sunDial(nfsaddr)) == nil)
  1395. sysfatal("dial nfs program at %s: %r", nfsaddr);
  1396. nfscli->chatty = chattyrpc;
  1397. sunClientProg(nfscli, &nfs3Prog);
  1398. if(nfsNull(0) < 0)
  1399. sysfatal("execute nop with nfs server at %s: %r", nfsaddr);
  1400. fschan = chancreate(sizeof(Req*), 0);
  1401. threadcreate(fsthread, nil, SunStackSize);
  1402. sendp(csync, 0);
  1403. }