nfs.c 30 KB

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