nfs4xdr.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538
  1. /*
  2. * fs/nfs/nfs4xdr.c
  3. *
  4. * Server-side XDR for NFSv4
  5. *
  6. * Copyright (c) 2002 The Regents of the University of Michigan.
  7. * All rights reserved.
  8. *
  9. * Kendrick Smith <kmsmith@umich.edu>
  10. * Andy Adamson <andros@umich.edu>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of the University nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  26. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  27. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  32. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. * TODO: Neil Brown made the following observation: We currently
  38. * initially reserve NFSD_BUFSIZE space on the transmit queue and
  39. * never release any of that until the request is complete.
  40. * It would be good to calculate a new maximum response size while
  41. * decoding the COMPOUND, and call svc_reserve with this number
  42. * at the end of nfs4svc_decode_compoundargs.
  43. */
  44. #include <linux/param.h>
  45. #include <linux/smp.h>
  46. #include <linux/smp_lock.h>
  47. #include <linux/fs.h>
  48. #include <linux/namei.h>
  49. #include <linux/vfs.h>
  50. #include <linux/sunrpc/xdr.h>
  51. #include <linux/sunrpc/svc.h>
  52. #include <linux/sunrpc/clnt.h>
  53. #include <linux/nfsd/nfsd.h>
  54. #include <linux/nfsd/state.h>
  55. #include <linux/nfsd/xdr4.h>
  56. #include <linux/nfsd_idmap.h>
  57. #include <linux/nfs4.h>
  58. #include <linux/nfs4_acl.h>
  59. #define NFSDDBG_FACILITY NFSDDBG_XDR
  60. static int
  61. check_filename(char *str, int len, int err)
  62. {
  63. int i;
  64. if (len == 0)
  65. return nfserr_inval;
  66. if (isdotent(str, len))
  67. return err;
  68. for (i = 0; i < len; i++)
  69. if (str[i] == '/')
  70. return err;
  71. return 0;
  72. }
  73. /*
  74. * START OF "GENERIC" DECODE ROUTINES.
  75. * These may look a little ugly since they are imported from a "generic"
  76. * set of XDR encode/decode routines which are intended to be shared by
  77. * all of our NFSv4 implementations (OpenBSD, MacOS X...).
  78. *
  79. * If the pain of reading these is too great, it should be a straightforward
  80. * task to translate them into Linux-specific versions which are more
  81. * consistent with the style used in NFSv2/v3...
  82. */
  83. #define DECODE_HEAD \
  84. u32 *p; \
  85. int status
  86. #define DECODE_TAIL \
  87. status = 0; \
  88. out: \
  89. return status; \
  90. xdr_error: \
  91. printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__); \
  92. status = nfserr_bad_xdr; \
  93. goto out
  94. #define READ32(x) (x) = ntohl(*p++)
  95. #define READ64(x) do { \
  96. (x) = (u64)ntohl(*p++) << 32; \
  97. (x) |= ntohl(*p++); \
  98. } while (0)
  99. #define READTIME(x) do { \
  100. p++; \
  101. (x) = ntohl(*p++); \
  102. p++; \
  103. } while (0)
  104. #define READMEM(x,nbytes) do { \
  105. x = (char *)p; \
  106. p += XDR_QUADLEN(nbytes); \
  107. } while (0)
  108. #define SAVEMEM(x,nbytes) do { \
  109. if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
  110. savemem(argp, p, nbytes) : \
  111. (char *)p)) { \
  112. printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__); \
  113. goto xdr_error; \
  114. } \
  115. p += XDR_QUADLEN(nbytes); \
  116. } while (0)
  117. #define COPYMEM(x,nbytes) do { \
  118. memcpy((x), p, nbytes); \
  119. p += XDR_QUADLEN(nbytes); \
  120. } while (0)
  121. /* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
  122. #define READ_BUF(nbytes) do { \
  123. if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
  124. p = argp->p; \
  125. argp->p += XDR_QUADLEN(nbytes); \
  126. } else if (!(p = read_buf(argp, nbytes))) { \
  127. printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__); \
  128. goto xdr_error; \
  129. } \
  130. } while (0)
  131. static u32 *read_buf(struct nfsd4_compoundargs *argp, int nbytes)
  132. {
  133. /* We want more bytes than seem to be available.
  134. * Maybe we need a new page, maybe we have just run out
  135. */
  136. int avail = (char*)argp->end - (char*)argp->p;
  137. u32 *p;
  138. if (avail + argp->pagelen < nbytes)
  139. return NULL;
  140. if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
  141. return NULL;
  142. /* ok, we can do it with the current plus the next page */
  143. if (nbytes <= sizeof(argp->tmp))
  144. p = argp->tmp;
  145. else {
  146. if (argp->tmpp)
  147. kfree(argp->tmpp);
  148. p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
  149. if (!p)
  150. return NULL;
  151. }
  152. memcpy(p, argp->p, avail);
  153. /* step to next page */
  154. argp->p = page_address(argp->pagelist[0]);
  155. argp->pagelist++;
  156. if (argp->pagelen < PAGE_SIZE) {
  157. argp->end = p + (argp->pagelen>>2);
  158. argp->pagelen = 0;
  159. } else {
  160. argp->end = p + (PAGE_SIZE>>2);
  161. argp->pagelen -= PAGE_SIZE;
  162. }
  163. memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
  164. argp->p += XDR_QUADLEN(nbytes - avail);
  165. return p;
  166. }
  167. static int
  168. defer_free(struct nfsd4_compoundargs *argp,
  169. void (*release)(const void *), void *p)
  170. {
  171. struct tmpbuf *tb;
  172. tb = kmalloc(sizeof(*tb), GFP_KERNEL);
  173. if (!tb)
  174. return -ENOMEM;
  175. tb->buf = p;
  176. tb->release = release;
  177. tb->next = argp->to_free;
  178. argp->to_free = tb;
  179. return 0;
  180. }
  181. static char *savemem(struct nfsd4_compoundargs *argp, u32 *p, int nbytes)
  182. {
  183. void *new = NULL;
  184. if (p == argp->tmp) {
  185. new = kmalloc(nbytes, GFP_KERNEL);
  186. if (!new) return NULL;
  187. p = new;
  188. memcpy(p, argp->tmp, nbytes);
  189. } else {
  190. if (p != argp->tmpp)
  191. BUG();
  192. argp->tmpp = NULL;
  193. }
  194. if (defer_free(argp, kfree, p)) {
  195. kfree(new);
  196. return NULL;
  197. } else
  198. return (char *)p;
  199. }
  200. static int
  201. nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
  202. {
  203. u32 bmlen;
  204. DECODE_HEAD;
  205. bmval[0] = 0;
  206. bmval[1] = 0;
  207. READ_BUF(4);
  208. READ32(bmlen);
  209. if (bmlen > 1000)
  210. goto xdr_error;
  211. READ_BUF(bmlen << 2);
  212. if (bmlen > 0)
  213. READ32(bmval[0]);
  214. if (bmlen > 1)
  215. READ32(bmval[1]);
  216. DECODE_TAIL;
  217. }
  218. static int
  219. nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval, struct iattr *iattr,
  220. struct nfs4_acl **acl)
  221. {
  222. int expected_len, len = 0;
  223. u32 dummy32;
  224. char *buf;
  225. DECODE_HEAD;
  226. iattr->ia_valid = 0;
  227. if ((status = nfsd4_decode_bitmap(argp, bmval)))
  228. return status;
  229. /*
  230. * According to spec, unsupported attributes return ERR_NOTSUPP;
  231. * read-only attributes return ERR_INVAL.
  232. */
  233. if ((bmval[0] & ~NFSD_SUPPORTED_ATTRS_WORD0) || (bmval[1] & ~NFSD_SUPPORTED_ATTRS_WORD1))
  234. return nfserr_attrnotsupp;
  235. if ((bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0) || (bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1))
  236. return nfserr_inval;
  237. READ_BUF(4);
  238. READ32(expected_len);
  239. if (bmval[0] & FATTR4_WORD0_SIZE) {
  240. READ_BUF(8);
  241. len += 8;
  242. READ64(iattr->ia_size);
  243. iattr->ia_valid |= ATTR_SIZE;
  244. }
  245. if (bmval[0] & FATTR4_WORD0_ACL) {
  246. int nace, i;
  247. struct nfs4_ace ace;
  248. READ_BUF(4); len += 4;
  249. READ32(nace);
  250. *acl = nfs4_acl_new();
  251. if (*acl == NULL) {
  252. status = -ENOMEM;
  253. goto out_nfserr;
  254. }
  255. defer_free(argp, (void (*)(const void *))nfs4_acl_free, *acl);
  256. for (i = 0; i < nace; i++) {
  257. READ_BUF(16); len += 16;
  258. READ32(ace.type);
  259. READ32(ace.flag);
  260. READ32(ace.access_mask);
  261. READ32(dummy32);
  262. READ_BUF(dummy32);
  263. len += XDR_QUADLEN(dummy32) << 2;
  264. READMEM(buf, dummy32);
  265. ace.whotype = nfs4_acl_get_whotype(buf, dummy32);
  266. status = 0;
  267. if (ace.whotype != NFS4_ACL_WHO_NAMED)
  268. ace.who = 0;
  269. else if (ace.flag & NFS4_ACE_IDENTIFIER_GROUP)
  270. status = nfsd_map_name_to_gid(argp->rqstp,
  271. buf, dummy32, &ace.who);
  272. else
  273. status = nfsd_map_name_to_uid(argp->rqstp,
  274. buf, dummy32, &ace.who);
  275. if (status)
  276. goto out_nfserr;
  277. if (nfs4_acl_add_ace(*acl, ace.type, ace.flag,
  278. ace.access_mask, ace.whotype, ace.who) != 0) {
  279. status = -ENOMEM;
  280. goto out_nfserr;
  281. }
  282. }
  283. } else
  284. *acl = NULL;
  285. if (bmval[1] & FATTR4_WORD1_MODE) {
  286. READ_BUF(4);
  287. len += 4;
  288. READ32(iattr->ia_mode);
  289. iattr->ia_mode &= (S_IFMT | S_IALLUGO);
  290. iattr->ia_valid |= ATTR_MODE;
  291. }
  292. if (bmval[1] & FATTR4_WORD1_OWNER) {
  293. READ_BUF(4);
  294. len += 4;
  295. READ32(dummy32);
  296. READ_BUF(dummy32);
  297. len += (XDR_QUADLEN(dummy32) << 2);
  298. READMEM(buf, dummy32);
  299. if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
  300. goto out_nfserr;
  301. iattr->ia_valid |= ATTR_UID;
  302. }
  303. if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
  304. READ_BUF(4);
  305. len += 4;
  306. READ32(dummy32);
  307. READ_BUF(dummy32);
  308. len += (XDR_QUADLEN(dummy32) << 2);
  309. READMEM(buf, dummy32);
  310. if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
  311. goto out_nfserr;
  312. iattr->ia_valid |= ATTR_GID;
  313. }
  314. if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
  315. READ_BUF(4);
  316. len += 4;
  317. READ32(dummy32);
  318. switch (dummy32) {
  319. case NFS4_SET_TO_CLIENT_TIME:
  320. /* We require the high 32 bits of 'seconds' to be 0, and we ignore
  321. all 32 bits of 'nseconds'. */
  322. READ_BUF(12);
  323. len += 12;
  324. READ32(dummy32);
  325. if (dummy32)
  326. return nfserr_inval;
  327. READ32(iattr->ia_atime.tv_sec);
  328. READ32(iattr->ia_atime.tv_nsec);
  329. if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
  330. return nfserr_inval;
  331. iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
  332. break;
  333. case NFS4_SET_TO_SERVER_TIME:
  334. iattr->ia_valid |= ATTR_ATIME;
  335. break;
  336. default:
  337. goto xdr_error;
  338. }
  339. }
  340. if (bmval[1] & FATTR4_WORD1_TIME_METADATA) {
  341. /* We require the high 32 bits of 'seconds' to be 0, and we ignore
  342. all 32 bits of 'nseconds'. */
  343. READ_BUF(12);
  344. len += 12;
  345. READ32(dummy32);
  346. if (dummy32)
  347. return nfserr_inval;
  348. READ32(iattr->ia_ctime.tv_sec);
  349. READ32(iattr->ia_ctime.tv_nsec);
  350. if (iattr->ia_ctime.tv_nsec >= (u32)1000000000)
  351. return nfserr_inval;
  352. iattr->ia_valid |= ATTR_CTIME;
  353. }
  354. if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
  355. READ_BUF(4);
  356. len += 4;
  357. READ32(dummy32);
  358. switch (dummy32) {
  359. case NFS4_SET_TO_CLIENT_TIME:
  360. /* We require the high 32 bits of 'seconds' to be 0, and we ignore
  361. all 32 bits of 'nseconds'. */
  362. READ_BUF(12);
  363. len += 12;
  364. READ32(dummy32);
  365. if (dummy32)
  366. return nfserr_inval;
  367. READ32(iattr->ia_mtime.tv_sec);
  368. READ32(iattr->ia_mtime.tv_nsec);
  369. if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
  370. return nfserr_inval;
  371. iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
  372. break;
  373. case NFS4_SET_TO_SERVER_TIME:
  374. iattr->ia_valid |= ATTR_MTIME;
  375. break;
  376. default:
  377. goto xdr_error;
  378. }
  379. }
  380. if (len != expected_len)
  381. goto xdr_error;
  382. DECODE_TAIL;
  383. out_nfserr:
  384. status = nfserrno(status);
  385. goto out;
  386. }
  387. static int
  388. nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
  389. {
  390. DECODE_HEAD;
  391. READ_BUF(4);
  392. READ32(access->ac_req_access);
  393. DECODE_TAIL;
  394. }
  395. static int
  396. nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
  397. {
  398. DECODE_HEAD;
  399. close->cl_stateowner = NULL;
  400. READ_BUF(4 + sizeof(stateid_t));
  401. READ32(close->cl_seqid);
  402. READ32(close->cl_stateid.si_generation);
  403. COPYMEM(&close->cl_stateid.si_opaque, sizeof(stateid_opaque_t));
  404. DECODE_TAIL;
  405. }
  406. static int
  407. nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
  408. {
  409. DECODE_HEAD;
  410. READ_BUF(12);
  411. READ64(commit->co_offset);
  412. READ32(commit->co_count);
  413. DECODE_TAIL;
  414. }
  415. static int
  416. nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
  417. {
  418. DECODE_HEAD;
  419. READ_BUF(4);
  420. READ32(create->cr_type);
  421. switch (create->cr_type) {
  422. case NF4LNK:
  423. READ_BUF(4);
  424. READ32(create->cr_linklen);
  425. READ_BUF(create->cr_linklen);
  426. SAVEMEM(create->cr_linkname, create->cr_linklen);
  427. break;
  428. case NF4BLK:
  429. case NF4CHR:
  430. READ_BUF(8);
  431. READ32(create->cr_specdata1);
  432. READ32(create->cr_specdata2);
  433. break;
  434. case NF4SOCK:
  435. case NF4FIFO:
  436. case NF4DIR:
  437. default:
  438. break;
  439. }
  440. READ_BUF(4);
  441. READ32(create->cr_namelen);
  442. READ_BUF(create->cr_namelen);
  443. SAVEMEM(create->cr_name, create->cr_namelen);
  444. if ((status = check_filename(create->cr_name, create->cr_namelen, nfserr_inval)))
  445. return status;
  446. if ((status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr, &create->cr_acl)))
  447. goto out;
  448. DECODE_TAIL;
  449. }
  450. static inline int
  451. nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
  452. {
  453. DECODE_HEAD;
  454. READ_BUF(sizeof(stateid_t));
  455. READ32(dr->dr_stateid.si_generation);
  456. COPYMEM(&dr->dr_stateid.si_opaque, sizeof(stateid_opaque_t));
  457. DECODE_TAIL;
  458. }
  459. static inline int
  460. nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
  461. {
  462. return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
  463. }
  464. static int
  465. nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
  466. {
  467. DECODE_HEAD;
  468. READ_BUF(4);
  469. READ32(link->li_namelen);
  470. READ_BUF(link->li_namelen);
  471. SAVEMEM(link->li_name, link->li_namelen);
  472. if ((status = check_filename(link->li_name, link->li_namelen, nfserr_inval)))
  473. return status;
  474. DECODE_TAIL;
  475. }
  476. static int
  477. nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
  478. {
  479. DECODE_HEAD;
  480. lock->lk_stateowner = NULL;
  481. /*
  482. * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
  483. */
  484. READ_BUF(28);
  485. READ32(lock->lk_type);
  486. if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
  487. goto xdr_error;
  488. READ32(lock->lk_reclaim);
  489. READ64(lock->lk_offset);
  490. READ64(lock->lk_length);
  491. READ32(lock->lk_is_new);
  492. if (lock->lk_is_new) {
  493. READ_BUF(36);
  494. READ32(lock->lk_new_open_seqid);
  495. READ32(lock->lk_new_open_stateid.si_generation);
  496. COPYMEM(&lock->lk_new_open_stateid.si_opaque, sizeof(stateid_opaque_t));
  497. READ32(lock->lk_new_lock_seqid);
  498. COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
  499. READ32(lock->lk_new_owner.len);
  500. READ_BUF(lock->lk_new_owner.len);
  501. READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
  502. } else {
  503. READ_BUF(20);
  504. READ32(lock->lk_old_lock_stateid.si_generation);
  505. COPYMEM(&lock->lk_old_lock_stateid.si_opaque, sizeof(stateid_opaque_t));
  506. READ32(lock->lk_old_lock_seqid);
  507. }
  508. DECODE_TAIL;
  509. }
  510. static int
  511. nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
  512. {
  513. DECODE_HEAD;
  514. READ_BUF(32);
  515. READ32(lockt->lt_type);
  516. if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
  517. goto xdr_error;
  518. READ64(lockt->lt_offset);
  519. READ64(lockt->lt_length);
  520. COPYMEM(&lockt->lt_clientid, 8);
  521. READ32(lockt->lt_owner.len);
  522. READ_BUF(lockt->lt_owner.len);
  523. READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
  524. DECODE_TAIL;
  525. }
  526. static int
  527. nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
  528. {
  529. DECODE_HEAD;
  530. locku->lu_stateowner = NULL;
  531. READ_BUF(24 + sizeof(stateid_t));
  532. READ32(locku->lu_type);
  533. if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
  534. goto xdr_error;
  535. READ32(locku->lu_seqid);
  536. READ32(locku->lu_stateid.si_generation);
  537. COPYMEM(&locku->lu_stateid.si_opaque, sizeof(stateid_opaque_t));
  538. READ64(locku->lu_offset);
  539. READ64(locku->lu_length);
  540. DECODE_TAIL;
  541. }
  542. static int
  543. nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
  544. {
  545. DECODE_HEAD;
  546. READ_BUF(4);
  547. READ32(lookup->lo_len);
  548. READ_BUF(lookup->lo_len);
  549. SAVEMEM(lookup->lo_name, lookup->lo_len);
  550. if ((status = check_filename(lookup->lo_name, lookup->lo_len, nfserr_noent)))
  551. return status;
  552. DECODE_TAIL;
  553. }
  554. static int
  555. nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
  556. {
  557. DECODE_HEAD;
  558. memset(open->op_bmval, 0, sizeof(open->op_bmval));
  559. open->op_iattr.ia_valid = 0;
  560. open->op_stateowner = NULL;
  561. /* seqid, share_access, share_deny, clientid, ownerlen */
  562. READ_BUF(16 + sizeof(clientid_t));
  563. READ32(open->op_seqid);
  564. READ32(open->op_share_access);
  565. READ32(open->op_share_deny);
  566. COPYMEM(&open->op_clientid, sizeof(clientid_t));
  567. READ32(open->op_owner.len);
  568. /* owner, open_flag */
  569. READ_BUF(open->op_owner.len + 4);
  570. SAVEMEM(open->op_owner.data, open->op_owner.len);
  571. READ32(open->op_create);
  572. switch (open->op_create) {
  573. case NFS4_OPEN_NOCREATE:
  574. break;
  575. case NFS4_OPEN_CREATE:
  576. READ_BUF(4);
  577. READ32(open->op_createmode);
  578. switch (open->op_createmode) {
  579. case NFS4_CREATE_UNCHECKED:
  580. case NFS4_CREATE_GUARDED:
  581. if ((status = nfsd4_decode_fattr(argp, open->op_bmval, &open->op_iattr, &open->op_acl)))
  582. goto out;
  583. break;
  584. case NFS4_CREATE_EXCLUSIVE:
  585. READ_BUF(8);
  586. COPYMEM(open->op_verf.data, 8);
  587. break;
  588. default:
  589. goto xdr_error;
  590. }
  591. break;
  592. default:
  593. goto xdr_error;
  594. }
  595. /* open_claim */
  596. READ_BUF(4);
  597. READ32(open->op_claim_type);
  598. switch (open->op_claim_type) {
  599. case NFS4_OPEN_CLAIM_NULL:
  600. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  601. READ_BUF(4);
  602. READ32(open->op_fname.len);
  603. READ_BUF(open->op_fname.len);
  604. SAVEMEM(open->op_fname.data, open->op_fname.len);
  605. if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
  606. return status;
  607. break;
  608. case NFS4_OPEN_CLAIM_PREVIOUS:
  609. READ_BUF(4);
  610. READ32(open->op_delegate_type);
  611. break;
  612. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  613. READ_BUF(sizeof(stateid_t) + 4);
  614. COPYMEM(&open->op_delegate_stateid, sizeof(stateid_t));
  615. READ32(open->op_fname.len);
  616. READ_BUF(open->op_fname.len);
  617. SAVEMEM(open->op_fname.data, open->op_fname.len);
  618. if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
  619. return status;
  620. break;
  621. default:
  622. goto xdr_error;
  623. }
  624. DECODE_TAIL;
  625. }
  626. static int
  627. nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
  628. {
  629. DECODE_HEAD;
  630. open_conf->oc_stateowner = NULL;
  631. READ_BUF(4 + sizeof(stateid_t));
  632. READ32(open_conf->oc_req_stateid.si_generation);
  633. COPYMEM(&open_conf->oc_req_stateid.si_opaque, sizeof(stateid_opaque_t));
  634. READ32(open_conf->oc_seqid);
  635. DECODE_TAIL;
  636. }
  637. static int
  638. nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
  639. {
  640. DECODE_HEAD;
  641. open_down->od_stateowner = NULL;
  642. READ_BUF(12 + sizeof(stateid_t));
  643. READ32(open_down->od_stateid.si_generation);
  644. COPYMEM(&open_down->od_stateid.si_opaque, sizeof(stateid_opaque_t));
  645. READ32(open_down->od_seqid);
  646. READ32(open_down->od_share_access);
  647. READ32(open_down->od_share_deny);
  648. DECODE_TAIL;
  649. }
  650. static int
  651. nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
  652. {
  653. DECODE_HEAD;
  654. READ_BUF(4);
  655. READ32(putfh->pf_fhlen);
  656. if (putfh->pf_fhlen > NFS4_FHSIZE)
  657. goto xdr_error;
  658. READ_BUF(putfh->pf_fhlen);
  659. SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
  660. DECODE_TAIL;
  661. }
  662. static int
  663. nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
  664. {
  665. DECODE_HEAD;
  666. READ_BUF(sizeof(stateid_t) + 12);
  667. READ32(read->rd_stateid.si_generation);
  668. COPYMEM(&read->rd_stateid.si_opaque, sizeof(stateid_opaque_t));
  669. READ64(read->rd_offset);
  670. READ32(read->rd_length);
  671. DECODE_TAIL;
  672. }
  673. static int
  674. nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
  675. {
  676. DECODE_HEAD;
  677. READ_BUF(24);
  678. READ64(readdir->rd_cookie);
  679. COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
  680. READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
  681. READ32(readdir->rd_maxcount);
  682. if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
  683. goto out;
  684. DECODE_TAIL;
  685. }
  686. static int
  687. nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
  688. {
  689. DECODE_HEAD;
  690. READ_BUF(4);
  691. READ32(remove->rm_namelen);
  692. READ_BUF(remove->rm_namelen);
  693. SAVEMEM(remove->rm_name, remove->rm_namelen);
  694. if ((status = check_filename(remove->rm_name, remove->rm_namelen, nfserr_noent)))
  695. return status;
  696. DECODE_TAIL;
  697. }
  698. static int
  699. nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
  700. {
  701. DECODE_HEAD;
  702. READ_BUF(4);
  703. READ32(rename->rn_snamelen);
  704. READ_BUF(rename->rn_snamelen + 4);
  705. SAVEMEM(rename->rn_sname, rename->rn_snamelen);
  706. READ32(rename->rn_tnamelen);
  707. READ_BUF(rename->rn_tnamelen);
  708. SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
  709. if ((status = check_filename(rename->rn_sname, rename->rn_snamelen, nfserr_noent)))
  710. return status;
  711. if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen, nfserr_inval)))
  712. return status;
  713. DECODE_TAIL;
  714. }
  715. static int
  716. nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
  717. {
  718. DECODE_HEAD;
  719. READ_BUF(sizeof(clientid_t));
  720. COPYMEM(clientid, sizeof(clientid_t));
  721. DECODE_TAIL;
  722. }
  723. static int
  724. nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
  725. {
  726. DECODE_HEAD;
  727. READ_BUF(sizeof(stateid_t));
  728. READ32(setattr->sa_stateid.si_generation);
  729. COPYMEM(&setattr->sa_stateid.si_opaque, sizeof(stateid_opaque_t));
  730. if ((status = nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr, &setattr->sa_acl)))
  731. goto out;
  732. DECODE_TAIL;
  733. }
  734. static int
  735. nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
  736. {
  737. DECODE_HEAD;
  738. READ_BUF(12);
  739. COPYMEM(setclientid->se_verf.data, 8);
  740. READ32(setclientid->se_namelen);
  741. READ_BUF(setclientid->se_namelen + 8);
  742. SAVEMEM(setclientid->se_name, setclientid->se_namelen);
  743. READ32(setclientid->se_callback_prog);
  744. READ32(setclientid->se_callback_netid_len);
  745. READ_BUF(setclientid->se_callback_netid_len + 4);
  746. SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
  747. READ32(setclientid->se_callback_addr_len);
  748. READ_BUF(setclientid->se_callback_addr_len + 4);
  749. SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
  750. READ32(setclientid->se_callback_ident);
  751. DECODE_TAIL;
  752. }
  753. static int
  754. nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
  755. {
  756. DECODE_HEAD;
  757. READ_BUF(8 + sizeof(nfs4_verifier));
  758. COPYMEM(&scd_c->sc_clientid, 8);
  759. COPYMEM(&scd_c->sc_confirm, sizeof(nfs4_verifier));
  760. DECODE_TAIL;
  761. }
  762. /* Also used for NVERIFY */
  763. static int
  764. nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
  765. {
  766. #if 0
  767. struct nfsd4_compoundargs save = {
  768. .p = argp->p,
  769. .end = argp->end,
  770. .rqstp = argp->rqstp,
  771. };
  772. u32 ve_bmval[2];
  773. struct iattr ve_iattr; /* request */
  774. struct nfs4_acl *ve_acl; /* request */
  775. #endif
  776. DECODE_HEAD;
  777. if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
  778. goto out;
  779. /* For convenience's sake, we compare raw xdr'd attributes in
  780. * nfsd4_proc_verify; however we still decode here just to return
  781. * correct error in case of bad xdr. */
  782. #if 0
  783. status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
  784. if (status == nfserr_inval) {
  785. status = nfserrno(status);
  786. goto out;
  787. }
  788. #endif
  789. READ_BUF(4);
  790. READ32(verify->ve_attrlen);
  791. READ_BUF(verify->ve_attrlen);
  792. SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
  793. DECODE_TAIL;
  794. }
  795. static int
  796. nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
  797. {
  798. int avail;
  799. int v;
  800. int len;
  801. DECODE_HEAD;
  802. READ_BUF(sizeof(stateid_opaque_t) + 20);
  803. READ32(write->wr_stateid.si_generation);
  804. COPYMEM(&write->wr_stateid.si_opaque, sizeof(stateid_opaque_t));
  805. READ64(write->wr_offset);
  806. READ32(write->wr_stable_how);
  807. if (write->wr_stable_how > 2)
  808. goto xdr_error;
  809. READ32(write->wr_buflen);
  810. /* Sorry .. no magic macros for this.. *
  811. * READ_BUF(write->wr_buflen);
  812. * SAVEMEM(write->wr_buf, write->wr_buflen);
  813. */
  814. avail = (char*)argp->end - (char*)argp->p;
  815. if (avail + argp->pagelen < write->wr_buflen) {
  816. printk(KERN_NOTICE "xdr error! (%s:%d)\n", __FILE__, __LINE__);
  817. goto xdr_error;
  818. }
  819. write->wr_vec[0].iov_base = p;
  820. write->wr_vec[0].iov_len = avail;
  821. v = 0;
  822. len = write->wr_buflen;
  823. while (len > write->wr_vec[v].iov_len) {
  824. len -= write->wr_vec[v].iov_len;
  825. v++;
  826. write->wr_vec[v].iov_base = page_address(argp->pagelist[0]);
  827. argp->pagelist++;
  828. if (argp->pagelen >= PAGE_SIZE) {
  829. write->wr_vec[v].iov_len = PAGE_SIZE;
  830. argp->pagelen -= PAGE_SIZE;
  831. } else {
  832. write->wr_vec[v].iov_len = argp->pagelen;
  833. argp->pagelen -= len;
  834. }
  835. }
  836. argp->end = (u32*) (write->wr_vec[v].iov_base + write->wr_vec[v].iov_len);
  837. argp->p = (u32*) (write->wr_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
  838. write->wr_vec[v].iov_len = len;
  839. write->wr_vlen = v+1;
  840. DECODE_TAIL;
  841. }
  842. static int
  843. nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
  844. {
  845. DECODE_HEAD;
  846. READ_BUF(12);
  847. COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
  848. READ32(rlockowner->rl_owner.len);
  849. READ_BUF(rlockowner->rl_owner.len);
  850. READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
  851. DECODE_TAIL;
  852. }
  853. static int
  854. nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
  855. {
  856. DECODE_HEAD;
  857. struct nfsd4_op *op;
  858. int i;
  859. /*
  860. * XXX: According to spec, we should check the tag
  861. * for UTF-8 compliance. I'm postponing this for
  862. * now because it seems that some clients do use
  863. * binary tags.
  864. */
  865. READ_BUF(4);
  866. READ32(argp->taglen);
  867. READ_BUF(argp->taglen + 8);
  868. SAVEMEM(argp->tag, argp->taglen);
  869. READ32(argp->minorversion);
  870. READ32(argp->opcnt);
  871. if (argp->taglen > NFSD4_MAX_TAGLEN)
  872. goto xdr_error;
  873. if (argp->opcnt > 100)
  874. goto xdr_error;
  875. if (argp->opcnt > sizeof(argp->iops)/sizeof(argp->iops[0])) {
  876. argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
  877. if (!argp->ops) {
  878. argp->ops = argp->iops;
  879. printk(KERN_INFO "nfsd: couldn't allocate room for COMPOUND\n");
  880. goto xdr_error;
  881. }
  882. }
  883. for (i = 0; i < argp->opcnt; i++) {
  884. op = &argp->ops[i];
  885. op->replay = NULL;
  886. /*
  887. * We can't use READ_BUF() here because we need to handle
  888. * a missing opcode as an OP_WRITE + 1. So we need to check
  889. * to see if we're truly at the end of our buffer or if there
  890. * is another page we need to flip to.
  891. */
  892. if (argp->p == argp->end) {
  893. if (argp->pagelen < 4) {
  894. /* There isn't an opcode still on the wire */
  895. op->opnum = OP_WRITE + 1;
  896. op->status = nfserr_bad_xdr;
  897. argp->opcnt = i+1;
  898. break;
  899. }
  900. /*
  901. * False alarm. We just hit a page boundary, but there
  902. * is still data available. Move pointer across page
  903. * boundary. *snip from READ_BUF*
  904. */
  905. argp->p = page_address(argp->pagelist[0]);
  906. argp->pagelist++;
  907. if (argp->pagelen < PAGE_SIZE) {
  908. argp->end = p + (argp->pagelen>>2);
  909. argp->pagelen = 0;
  910. } else {
  911. argp->end = p + (PAGE_SIZE>>2);
  912. argp->pagelen -= PAGE_SIZE;
  913. }
  914. }
  915. op->opnum = ntohl(*argp->p++);
  916. switch (op->opnum) {
  917. case 2: /* Reserved operation */
  918. op->opnum = OP_ILLEGAL;
  919. if (argp->minorversion == 0)
  920. op->status = nfserr_op_illegal;
  921. else
  922. op->status = nfserr_minor_vers_mismatch;
  923. break;
  924. case OP_ACCESS:
  925. op->status = nfsd4_decode_access(argp, &op->u.access);
  926. break;
  927. case OP_CLOSE:
  928. op->status = nfsd4_decode_close(argp, &op->u.close);
  929. break;
  930. case OP_COMMIT:
  931. op->status = nfsd4_decode_commit(argp, &op->u.commit);
  932. break;
  933. case OP_CREATE:
  934. op->status = nfsd4_decode_create(argp, &op->u.create);
  935. break;
  936. case OP_DELEGRETURN:
  937. op->status = nfsd4_decode_delegreturn(argp, &op->u.delegreturn);
  938. break;
  939. case OP_GETATTR:
  940. op->status = nfsd4_decode_getattr(argp, &op->u.getattr);
  941. break;
  942. case OP_GETFH:
  943. op->status = nfs_ok;
  944. break;
  945. case OP_LINK:
  946. op->status = nfsd4_decode_link(argp, &op->u.link);
  947. break;
  948. case OP_LOCK:
  949. op->status = nfsd4_decode_lock(argp, &op->u.lock);
  950. break;
  951. case OP_LOCKT:
  952. op->status = nfsd4_decode_lockt(argp, &op->u.lockt);
  953. break;
  954. case OP_LOCKU:
  955. op->status = nfsd4_decode_locku(argp, &op->u.locku);
  956. break;
  957. case OP_LOOKUP:
  958. op->status = nfsd4_decode_lookup(argp, &op->u.lookup);
  959. break;
  960. case OP_LOOKUPP:
  961. op->status = nfs_ok;
  962. break;
  963. case OP_NVERIFY:
  964. op->status = nfsd4_decode_verify(argp, &op->u.nverify);
  965. break;
  966. case OP_OPEN:
  967. op->status = nfsd4_decode_open(argp, &op->u.open);
  968. break;
  969. case OP_OPEN_CONFIRM:
  970. op->status = nfsd4_decode_open_confirm(argp, &op->u.open_confirm);
  971. break;
  972. case OP_OPEN_DOWNGRADE:
  973. op->status = nfsd4_decode_open_downgrade(argp, &op->u.open_downgrade);
  974. break;
  975. case OP_PUTFH:
  976. op->status = nfsd4_decode_putfh(argp, &op->u.putfh);
  977. break;
  978. case OP_PUTROOTFH:
  979. op->status = nfs_ok;
  980. break;
  981. case OP_READ:
  982. op->status = nfsd4_decode_read(argp, &op->u.read);
  983. break;
  984. case OP_READDIR:
  985. op->status = nfsd4_decode_readdir(argp, &op->u.readdir);
  986. break;
  987. case OP_READLINK:
  988. op->status = nfs_ok;
  989. break;
  990. case OP_REMOVE:
  991. op->status = nfsd4_decode_remove(argp, &op->u.remove);
  992. break;
  993. case OP_RENAME:
  994. op->status = nfsd4_decode_rename(argp, &op->u.rename);
  995. break;
  996. case OP_RESTOREFH:
  997. op->status = nfs_ok;
  998. break;
  999. case OP_RENEW:
  1000. op->status = nfsd4_decode_renew(argp, &op->u.renew);
  1001. break;
  1002. case OP_SAVEFH:
  1003. op->status = nfs_ok;
  1004. break;
  1005. case OP_SETATTR:
  1006. op->status = nfsd4_decode_setattr(argp, &op->u.setattr);
  1007. break;
  1008. case OP_SETCLIENTID:
  1009. op->status = nfsd4_decode_setclientid(argp, &op->u.setclientid);
  1010. break;
  1011. case OP_SETCLIENTID_CONFIRM:
  1012. op->status = nfsd4_decode_setclientid_confirm(argp, &op->u.setclientid_confirm);
  1013. break;
  1014. case OP_VERIFY:
  1015. op->status = nfsd4_decode_verify(argp, &op->u.verify);
  1016. break;
  1017. case OP_WRITE:
  1018. op->status = nfsd4_decode_write(argp, &op->u.write);
  1019. break;
  1020. case OP_RELEASE_LOCKOWNER:
  1021. op->status = nfsd4_decode_release_lockowner(argp, &op->u.release_lockowner);
  1022. break;
  1023. default:
  1024. op->opnum = OP_ILLEGAL;
  1025. op->status = nfserr_op_illegal;
  1026. break;
  1027. }
  1028. if (op->status) {
  1029. argp->opcnt = i+1;
  1030. break;
  1031. }
  1032. }
  1033. DECODE_TAIL;
  1034. }
  1035. /*
  1036. * END OF "GENERIC" DECODE ROUTINES.
  1037. */
  1038. /*
  1039. * START OF "GENERIC" ENCODE ROUTINES.
  1040. * These may look a little ugly since they are imported from a "generic"
  1041. * set of XDR encode/decode routines which are intended to be shared by
  1042. * all of our NFSv4 implementations (OpenBSD, MacOS X...).
  1043. *
  1044. * If the pain of reading these is too great, it should be a straightforward
  1045. * task to translate them into Linux-specific versions which are more
  1046. * consistent with the style used in NFSv2/v3...
  1047. */
  1048. #define ENCODE_HEAD u32 *p
  1049. #define WRITE32(n) *p++ = htonl(n)
  1050. #define WRITE64(n) do { \
  1051. *p++ = htonl((u32)((n) >> 32)); \
  1052. *p++ = htonl((u32)(n)); \
  1053. } while (0)
  1054. #define WRITEMEM(ptr,nbytes) do { \
  1055. *(p + XDR_QUADLEN(nbytes) -1) = 0; \
  1056. memcpy(p, ptr, nbytes); \
  1057. p += XDR_QUADLEN(nbytes); \
  1058. } while (0)
  1059. #define WRITECINFO(c) do { \
  1060. *p++ = htonl(c.atomic); \
  1061. *p++ = htonl(c.before_ctime_sec); \
  1062. *p++ = htonl(c.before_ctime_nsec); \
  1063. *p++ = htonl(c.after_ctime_sec); \
  1064. *p++ = htonl(c.after_ctime_nsec); \
  1065. } while (0)
  1066. #define RESERVE_SPACE(nbytes) do { \
  1067. p = resp->p; \
  1068. BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
  1069. } while (0)
  1070. #define ADJUST_ARGS() resp->p = p
  1071. /*
  1072. * Header routine to setup seqid operation replay cache
  1073. */
  1074. #define ENCODE_SEQID_OP_HEAD \
  1075. u32 *p; \
  1076. u32 *save; \
  1077. \
  1078. save = resp->p;
  1079. /*
  1080. * Routine for encoding the result of a "seqid-mutating" NFSv4 operation. This
  1081. * is where sequence id's are incremented, and the replay cache is filled.
  1082. * Note that we increment sequence id's here, at the last moment, so we're sure
  1083. * we know whether the error to be returned is a sequence id mutating error.
  1084. */
  1085. #define ENCODE_SEQID_OP_TAIL(stateowner) do { \
  1086. if (seqid_mutating_err(nfserr) && stateowner) { \
  1087. stateowner->so_seqid++; \
  1088. stateowner->so_replay.rp_status = nfserr; \
  1089. stateowner->so_replay.rp_buflen = \
  1090. (((char *)(resp)->p - (char *)save)); \
  1091. memcpy(stateowner->so_replay.rp_buf, save, \
  1092. stateowner->so_replay.rp_buflen); \
  1093. } } while (0);
  1094. static u32 nfs4_ftypes[16] = {
  1095. NF4BAD, NF4FIFO, NF4CHR, NF4BAD,
  1096. NF4DIR, NF4BAD, NF4BLK, NF4BAD,
  1097. NF4REG, NF4BAD, NF4LNK, NF4BAD,
  1098. NF4SOCK, NF4BAD, NF4LNK, NF4BAD,
  1099. };
  1100. static int
  1101. nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
  1102. u32 **p, int *buflen)
  1103. {
  1104. int status;
  1105. if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
  1106. return nfserr_resource;
  1107. if (whotype != NFS4_ACL_WHO_NAMED)
  1108. status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
  1109. else if (group)
  1110. status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
  1111. else
  1112. status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
  1113. if (status < 0)
  1114. return nfserrno(status);
  1115. *p = xdr_encode_opaque(*p, NULL, status);
  1116. *buflen -= (XDR_QUADLEN(status) << 2) + 4;
  1117. BUG_ON(*buflen < 0);
  1118. return 0;
  1119. }
  1120. static inline int
  1121. nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, u32 **p, int *buflen)
  1122. {
  1123. return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
  1124. }
  1125. static inline int
  1126. nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, u32 **p, int *buflen)
  1127. {
  1128. return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
  1129. }
  1130. static inline int
  1131. nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
  1132. u32 **p, int *buflen)
  1133. {
  1134. return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
  1135. }
  1136. /*
  1137. * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
  1138. * ourselves.
  1139. *
  1140. * @countp is the buffer size in _words_; upon successful return this becomes
  1141. * replaced with the number of words written.
  1142. */
  1143. int
  1144. nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
  1145. struct dentry *dentry, u32 *buffer, int *countp, u32 *bmval,
  1146. struct svc_rqst *rqstp)
  1147. {
  1148. u32 bmval0 = bmval[0];
  1149. u32 bmval1 = bmval[1];
  1150. struct kstat stat;
  1151. struct svc_fh tempfh;
  1152. struct kstatfs statfs;
  1153. int buflen = *countp << 2;
  1154. u32 *attrlenp;
  1155. u32 dummy;
  1156. u64 dummy64;
  1157. u32 *p = buffer;
  1158. int status;
  1159. int aclsupport = 0;
  1160. struct nfs4_acl *acl = NULL;
  1161. BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
  1162. BUG_ON(bmval0 & ~NFSD_SUPPORTED_ATTRS_WORD0);
  1163. BUG_ON(bmval1 & ~NFSD_SUPPORTED_ATTRS_WORD1);
  1164. status = vfs_getattr(exp->ex_mnt, dentry, &stat);
  1165. if (status)
  1166. goto out_nfserr;
  1167. if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL)) ||
  1168. (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
  1169. FATTR4_WORD1_SPACE_TOTAL))) {
  1170. status = vfs_statfs(dentry->d_inode->i_sb, &statfs);
  1171. if (status)
  1172. goto out_nfserr;
  1173. }
  1174. if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
  1175. fh_init(&tempfh, NFS4_FHSIZE);
  1176. status = fh_compose(&tempfh, exp, dentry, NULL);
  1177. if (status)
  1178. goto out;
  1179. fhp = &tempfh;
  1180. }
  1181. if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
  1182. | FATTR4_WORD0_SUPPORTED_ATTRS)) {
  1183. status = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
  1184. aclsupport = (status == 0);
  1185. if (bmval0 & FATTR4_WORD0_ACL) {
  1186. if (status == -EOPNOTSUPP)
  1187. bmval0 &= ~FATTR4_WORD0_ACL;
  1188. else if (status == -EINVAL) {
  1189. status = nfserr_attrnotsupp;
  1190. goto out;
  1191. } else if (status != 0)
  1192. goto out_nfserr;
  1193. }
  1194. }
  1195. if ((buflen -= 16) < 0)
  1196. goto out_resource;
  1197. WRITE32(2);
  1198. WRITE32(bmval0);
  1199. WRITE32(bmval1);
  1200. attrlenp = p++; /* to be backfilled later */
  1201. if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  1202. if ((buflen -= 12) < 0)
  1203. goto out_resource;
  1204. WRITE32(2);
  1205. WRITE32(aclsupport ?
  1206. NFSD_SUPPORTED_ATTRS_WORD0 :
  1207. NFSD_SUPPORTED_ATTRS_WORD0 & ~FATTR4_WORD0_ACL);
  1208. WRITE32(NFSD_SUPPORTED_ATTRS_WORD1);
  1209. }
  1210. if (bmval0 & FATTR4_WORD0_TYPE) {
  1211. if ((buflen -= 4) < 0)
  1212. goto out_resource;
  1213. dummy = nfs4_ftypes[(stat.mode & S_IFMT) >> 12];
  1214. if (dummy == NF4BAD)
  1215. goto out_serverfault;
  1216. WRITE32(dummy);
  1217. }
  1218. if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
  1219. if ((buflen -= 4) < 0)
  1220. goto out_resource;
  1221. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  1222. WRITE32(NFS4_FH_PERSISTENT);
  1223. else
  1224. WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
  1225. }
  1226. if (bmval0 & FATTR4_WORD0_CHANGE) {
  1227. /*
  1228. * Note: This _must_ be consistent with the scheme for writing
  1229. * change_info, so any changes made here must be reflected there
  1230. * as well. (See xdr4.h:set_change_info() and the WRITECINFO()
  1231. * macro above.)
  1232. */
  1233. if ((buflen -= 8) < 0)
  1234. goto out_resource;
  1235. WRITE32(stat.ctime.tv_sec);
  1236. WRITE32(stat.ctime.tv_nsec);
  1237. }
  1238. if (bmval0 & FATTR4_WORD0_SIZE) {
  1239. if ((buflen -= 8) < 0)
  1240. goto out_resource;
  1241. WRITE64(stat.size);
  1242. }
  1243. if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
  1244. if ((buflen -= 4) < 0)
  1245. goto out_resource;
  1246. WRITE32(1);
  1247. }
  1248. if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
  1249. if ((buflen -= 4) < 0)
  1250. goto out_resource;
  1251. WRITE32(1);
  1252. }
  1253. if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
  1254. if ((buflen -= 4) < 0)
  1255. goto out_resource;
  1256. WRITE32(0);
  1257. }
  1258. if (bmval0 & FATTR4_WORD0_FSID) {
  1259. if ((buflen -= 16) < 0)
  1260. goto out_resource;
  1261. if (is_fsid(fhp, rqstp->rq_reffh)) {
  1262. WRITE64((u64)exp->ex_fsid);
  1263. WRITE64((u64)0);
  1264. } else {
  1265. WRITE32(0);
  1266. WRITE32(MAJOR(stat.dev));
  1267. WRITE32(0);
  1268. WRITE32(MINOR(stat.dev));
  1269. }
  1270. }
  1271. if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
  1272. if ((buflen -= 4) < 0)
  1273. goto out_resource;
  1274. WRITE32(0);
  1275. }
  1276. if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
  1277. if ((buflen -= 4) < 0)
  1278. goto out_resource;
  1279. WRITE32(NFSD_LEASE_TIME);
  1280. }
  1281. if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
  1282. if ((buflen -= 4) < 0)
  1283. goto out_resource;
  1284. WRITE32(0);
  1285. }
  1286. if (bmval0 & FATTR4_WORD0_ACL) {
  1287. struct nfs4_ace *ace;
  1288. struct list_head *h;
  1289. if (acl == NULL) {
  1290. if ((buflen -= 4) < 0)
  1291. goto out_resource;
  1292. WRITE32(0);
  1293. goto out_acl;
  1294. }
  1295. if ((buflen -= 4) < 0)
  1296. goto out_resource;
  1297. WRITE32(acl->naces);
  1298. list_for_each(h, &acl->ace_head) {
  1299. ace = list_entry(h, struct nfs4_ace, l_ace);
  1300. if ((buflen -= 4*3) < 0)
  1301. goto out_resource;
  1302. WRITE32(ace->type);
  1303. WRITE32(ace->flag);
  1304. WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
  1305. status = nfsd4_encode_aclname(rqstp, ace->whotype,
  1306. ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
  1307. &p, &buflen);
  1308. if (status == nfserr_resource)
  1309. goto out_resource;
  1310. if (status)
  1311. goto out;
  1312. }
  1313. }
  1314. out_acl:
  1315. if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
  1316. if ((buflen -= 4) < 0)
  1317. goto out_resource;
  1318. WRITE32(aclsupport ?
  1319. ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
  1320. }
  1321. if (bmval0 & FATTR4_WORD0_CANSETTIME) {
  1322. if ((buflen -= 4) < 0)
  1323. goto out_resource;
  1324. WRITE32(1);
  1325. }
  1326. if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
  1327. if ((buflen -= 4) < 0)
  1328. goto out_resource;
  1329. WRITE32(1);
  1330. }
  1331. if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
  1332. if ((buflen -= 4) < 0)
  1333. goto out_resource;
  1334. WRITE32(1);
  1335. }
  1336. if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
  1337. if ((buflen -= 4) < 0)
  1338. goto out_resource;
  1339. WRITE32(1);
  1340. }
  1341. if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
  1342. buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
  1343. if (buflen < 0)
  1344. goto out_resource;
  1345. WRITE32(fhp->fh_handle.fh_size);
  1346. WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
  1347. }
  1348. if (bmval0 & FATTR4_WORD0_FILEID) {
  1349. if ((buflen -= 8) < 0)
  1350. goto out_resource;
  1351. WRITE64((u64) stat.ino);
  1352. }
  1353. if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
  1354. if ((buflen -= 8) < 0)
  1355. goto out_resource;
  1356. WRITE64((u64) statfs.f_ffree);
  1357. }
  1358. if (bmval0 & FATTR4_WORD0_FILES_FREE) {
  1359. if ((buflen -= 8) < 0)
  1360. goto out_resource;
  1361. WRITE64((u64) statfs.f_ffree);
  1362. }
  1363. if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
  1364. if ((buflen -= 8) < 0)
  1365. goto out_resource;
  1366. WRITE64((u64) statfs.f_files);
  1367. }
  1368. if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
  1369. if ((buflen -= 4) < 0)
  1370. goto out_resource;
  1371. WRITE32(1);
  1372. }
  1373. if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
  1374. if ((buflen -= 8) < 0)
  1375. goto out_resource;
  1376. WRITE64(~(u64)0);
  1377. }
  1378. if (bmval0 & FATTR4_WORD0_MAXLINK) {
  1379. if ((buflen -= 4) < 0)
  1380. goto out_resource;
  1381. WRITE32(255);
  1382. }
  1383. if (bmval0 & FATTR4_WORD0_MAXNAME) {
  1384. if ((buflen -= 4) < 0)
  1385. goto out_resource;
  1386. WRITE32(~(u32) 0);
  1387. }
  1388. if (bmval0 & FATTR4_WORD0_MAXREAD) {
  1389. if ((buflen -= 8) < 0)
  1390. goto out_resource;
  1391. WRITE64((u64) NFSSVC_MAXBLKSIZE);
  1392. }
  1393. if (bmval0 & FATTR4_WORD0_MAXWRITE) {
  1394. if ((buflen -= 8) < 0)
  1395. goto out_resource;
  1396. WRITE64((u64) NFSSVC_MAXBLKSIZE);
  1397. }
  1398. if (bmval1 & FATTR4_WORD1_MODE) {
  1399. if ((buflen -= 4) < 0)
  1400. goto out_resource;
  1401. WRITE32(stat.mode & S_IALLUGO);
  1402. }
  1403. if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
  1404. if ((buflen -= 4) < 0)
  1405. goto out_resource;
  1406. WRITE32(1);
  1407. }
  1408. if (bmval1 & FATTR4_WORD1_NUMLINKS) {
  1409. if ((buflen -= 4) < 0)
  1410. goto out_resource;
  1411. WRITE32(stat.nlink);
  1412. }
  1413. if (bmval1 & FATTR4_WORD1_OWNER) {
  1414. status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
  1415. if (status == nfserr_resource)
  1416. goto out_resource;
  1417. if (status)
  1418. goto out;
  1419. }
  1420. if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
  1421. status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
  1422. if (status == nfserr_resource)
  1423. goto out_resource;
  1424. if (status)
  1425. goto out;
  1426. }
  1427. if (bmval1 & FATTR4_WORD1_RAWDEV) {
  1428. if ((buflen -= 8) < 0)
  1429. goto out_resource;
  1430. WRITE32((u32) MAJOR(stat.rdev));
  1431. WRITE32((u32) MINOR(stat.rdev));
  1432. }
  1433. if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
  1434. if ((buflen -= 8) < 0)
  1435. goto out_resource;
  1436. dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
  1437. WRITE64(dummy64);
  1438. }
  1439. if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
  1440. if ((buflen -= 8) < 0)
  1441. goto out_resource;
  1442. dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
  1443. WRITE64(dummy64);
  1444. }
  1445. if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
  1446. if ((buflen -= 8) < 0)
  1447. goto out_resource;
  1448. dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
  1449. WRITE64(dummy64);
  1450. }
  1451. if (bmval1 & FATTR4_WORD1_SPACE_USED) {
  1452. if ((buflen -= 8) < 0)
  1453. goto out_resource;
  1454. dummy64 = (u64)stat.blocks << 9;
  1455. WRITE64(dummy64);
  1456. }
  1457. if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
  1458. if ((buflen -= 12) < 0)
  1459. goto out_resource;
  1460. WRITE32(0);
  1461. WRITE32(stat.atime.tv_sec);
  1462. WRITE32(stat.atime.tv_nsec);
  1463. }
  1464. if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
  1465. if ((buflen -= 12) < 0)
  1466. goto out_resource;
  1467. WRITE32(0);
  1468. WRITE32(1);
  1469. WRITE32(0);
  1470. }
  1471. if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
  1472. if ((buflen -= 12) < 0)
  1473. goto out_resource;
  1474. WRITE32(0);
  1475. WRITE32(stat.ctime.tv_sec);
  1476. WRITE32(stat.ctime.tv_nsec);
  1477. }
  1478. if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
  1479. if ((buflen -= 12) < 0)
  1480. goto out_resource;
  1481. WRITE32(0);
  1482. WRITE32(stat.mtime.tv_sec);
  1483. WRITE32(stat.mtime.tv_nsec);
  1484. }
  1485. if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
  1486. struct dentry *mnt_pnt, *mnt_root;
  1487. if ((buflen -= 8) < 0)
  1488. goto out_resource;
  1489. mnt_root = exp->ex_mnt->mnt_root;
  1490. if (mnt_root->d_inode == dentry->d_inode) {
  1491. mnt_pnt = exp->ex_mnt->mnt_mountpoint;
  1492. WRITE64((u64) mnt_pnt->d_inode->i_ino);
  1493. } else
  1494. WRITE64((u64) stat.ino);
  1495. }
  1496. *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
  1497. *countp = p - buffer;
  1498. status = nfs_ok;
  1499. out:
  1500. nfs4_acl_free(acl);
  1501. if (fhp == &tempfh)
  1502. fh_put(&tempfh);
  1503. return status;
  1504. out_nfserr:
  1505. status = nfserrno(status);
  1506. goto out;
  1507. out_resource:
  1508. *countp = 0;
  1509. status = nfserr_resource;
  1510. goto out;
  1511. out_serverfault:
  1512. status = nfserr_serverfault;
  1513. goto out;
  1514. }
  1515. static int
  1516. nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
  1517. const char *name, int namlen, u32 *p, int *buflen)
  1518. {
  1519. struct svc_export *exp = cd->rd_fhp->fh_export;
  1520. struct dentry *dentry;
  1521. int nfserr;
  1522. dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
  1523. if (IS_ERR(dentry))
  1524. return nfserrno(PTR_ERR(dentry));
  1525. exp_get(exp);
  1526. if (d_mountpoint(dentry)) {
  1527. if (nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp)) {
  1528. /*
  1529. * -EAGAIN is the only error returned from
  1530. * nfsd_cross_mnt() and it indicates that an
  1531. * up-call has been initiated to fill in the export
  1532. * options on exp. When the answer comes back,
  1533. * this call will be retried.
  1534. */
  1535. nfserr = nfserr_dropit;
  1536. goto out_put;
  1537. }
  1538. }
  1539. nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
  1540. cd->rd_rqstp);
  1541. out_put:
  1542. dput(dentry);
  1543. exp_put(exp);
  1544. return nfserr;
  1545. }
  1546. static u32 *
  1547. nfsd4_encode_rdattr_error(u32 *p, int buflen, int nfserr)
  1548. {
  1549. u32 *attrlenp;
  1550. if (buflen < 6)
  1551. return NULL;
  1552. *p++ = htonl(2);
  1553. *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
  1554. *p++ = htonl(0); /* bmval1 */
  1555. attrlenp = p++;
  1556. *p++ = nfserr; /* no htonl */
  1557. *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
  1558. return p;
  1559. }
  1560. static int
  1561. nfsd4_encode_dirent(struct readdir_cd *ccd, const char *name, int namlen,
  1562. loff_t offset, ino_t ino, unsigned int d_type)
  1563. {
  1564. struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
  1565. int buflen;
  1566. u32 *p = cd->buffer;
  1567. int nfserr = nfserr_toosmall;
  1568. /* In nfsv4, "." and ".." never make it onto the wire.. */
  1569. if (name && isdotent(name, namlen)) {
  1570. cd->common.err = nfs_ok;
  1571. return 0;
  1572. }
  1573. if (cd->offset)
  1574. xdr_encode_hyper(cd->offset, (u64) offset);
  1575. buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
  1576. if (buflen < 0)
  1577. goto fail;
  1578. *p++ = xdr_one; /* mark entry present */
  1579. cd->offset = p; /* remember pointer */
  1580. p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
  1581. p = xdr_encode_array(p, name, namlen); /* name length & name */
  1582. nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
  1583. switch (nfserr) {
  1584. case nfs_ok:
  1585. p += buflen;
  1586. break;
  1587. case nfserr_resource:
  1588. nfserr = nfserr_toosmall;
  1589. goto fail;
  1590. case nfserr_dropit:
  1591. goto fail;
  1592. default:
  1593. /*
  1594. * If the client requested the RDATTR_ERROR attribute,
  1595. * we stuff the error code into this attribute
  1596. * and continue. If this attribute was not requested,
  1597. * then in accordance with the spec, we fail the
  1598. * entire READDIR operation(!)
  1599. */
  1600. if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
  1601. goto fail;
  1602. nfserr = nfserr_toosmall;
  1603. p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
  1604. if (p == NULL)
  1605. goto fail;
  1606. }
  1607. cd->buflen -= (p - cd->buffer);
  1608. cd->buffer = p;
  1609. cd->common.err = nfs_ok;
  1610. return 0;
  1611. fail:
  1612. cd->common.err = nfserr;
  1613. return -EINVAL;
  1614. }
  1615. static void
  1616. nfsd4_encode_access(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_access *access)
  1617. {
  1618. ENCODE_HEAD;
  1619. if (!nfserr) {
  1620. RESERVE_SPACE(8);
  1621. WRITE32(access->ac_supported);
  1622. WRITE32(access->ac_resp_access);
  1623. ADJUST_ARGS();
  1624. }
  1625. }
  1626. static void
  1627. nfsd4_encode_close(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_close *close)
  1628. {
  1629. ENCODE_SEQID_OP_HEAD;
  1630. if (!nfserr) {
  1631. RESERVE_SPACE(sizeof(stateid_t));
  1632. WRITE32(close->cl_stateid.si_generation);
  1633. WRITEMEM(&close->cl_stateid.si_opaque, sizeof(stateid_opaque_t));
  1634. ADJUST_ARGS();
  1635. }
  1636. ENCODE_SEQID_OP_TAIL(close->cl_stateowner);
  1637. }
  1638. static void
  1639. nfsd4_encode_commit(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_commit *commit)
  1640. {
  1641. ENCODE_HEAD;
  1642. if (!nfserr) {
  1643. RESERVE_SPACE(8);
  1644. WRITEMEM(commit->co_verf.data, 8);
  1645. ADJUST_ARGS();
  1646. }
  1647. }
  1648. static void
  1649. nfsd4_encode_create(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_create *create)
  1650. {
  1651. ENCODE_HEAD;
  1652. if (!nfserr) {
  1653. RESERVE_SPACE(32);
  1654. WRITECINFO(create->cr_cinfo);
  1655. WRITE32(2);
  1656. WRITE32(create->cr_bmval[0]);
  1657. WRITE32(create->cr_bmval[1]);
  1658. ADJUST_ARGS();
  1659. }
  1660. }
  1661. static int
  1662. nfsd4_encode_getattr(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_getattr *getattr)
  1663. {
  1664. struct svc_fh *fhp = getattr->ga_fhp;
  1665. int buflen;
  1666. if (nfserr)
  1667. return nfserr;
  1668. buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
  1669. nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
  1670. resp->p, &buflen, getattr->ga_bmval,
  1671. resp->rqstp);
  1672. if (!nfserr)
  1673. resp->p += buflen;
  1674. return nfserr;
  1675. }
  1676. static void
  1677. nfsd4_encode_getfh(struct nfsd4_compoundres *resp, int nfserr, struct svc_fh *fhp)
  1678. {
  1679. unsigned int len;
  1680. ENCODE_HEAD;
  1681. if (!nfserr) {
  1682. len = fhp->fh_handle.fh_size;
  1683. RESERVE_SPACE(len + 4);
  1684. WRITE32(len);
  1685. WRITEMEM(&fhp->fh_handle.fh_base, len);
  1686. ADJUST_ARGS();
  1687. }
  1688. }
  1689. /*
  1690. * Including all fields other than the name, a LOCK4denied structure requires
  1691. * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
  1692. */
  1693. static void
  1694. nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
  1695. {
  1696. ENCODE_HEAD;
  1697. RESERVE_SPACE(32 + XDR_LEN(ld->ld_sop ? ld->ld_sop->so_owner.len : 0));
  1698. WRITE64(ld->ld_start);
  1699. WRITE64(ld->ld_length);
  1700. WRITE32(ld->ld_type);
  1701. if (ld->ld_sop) {
  1702. WRITEMEM(&ld->ld_clientid, 8);
  1703. WRITE32(ld->ld_sop->so_owner.len);
  1704. WRITEMEM(ld->ld_sop->so_owner.data, ld->ld_sop->so_owner.len);
  1705. kref_put(&ld->ld_sop->so_ref, nfs4_free_stateowner);
  1706. } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
  1707. WRITE64((u64)0); /* clientid */
  1708. WRITE32(0); /* length of owner name */
  1709. }
  1710. ADJUST_ARGS();
  1711. }
  1712. static void
  1713. nfsd4_encode_lock(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_lock *lock)
  1714. {
  1715. ENCODE_SEQID_OP_HEAD;
  1716. if (!nfserr) {
  1717. RESERVE_SPACE(4 + sizeof(stateid_t));
  1718. WRITE32(lock->lk_resp_stateid.si_generation);
  1719. WRITEMEM(&lock->lk_resp_stateid.si_opaque, sizeof(stateid_opaque_t));
  1720. ADJUST_ARGS();
  1721. } else if (nfserr == nfserr_denied)
  1722. nfsd4_encode_lock_denied(resp, &lock->lk_denied);
  1723. ENCODE_SEQID_OP_TAIL(lock->lk_stateowner);
  1724. }
  1725. static void
  1726. nfsd4_encode_lockt(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_lockt *lockt)
  1727. {
  1728. if (nfserr == nfserr_denied)
  1729. nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
  1730. }
  1731. static void
  1732. nfsd4_encode_locku(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_locku *locku)
  1733. {
  1734. ENCODE_SEQID_OP_HEAD;
  1735. if (!nfserr) {
  1736. RESERVE_SPACE(sizeof(stateid_t));
  1737. WRITE32(locku->lu_stateid.si_generation);
  1738. WRITEMEM(&locku->lu_stateid.si_opaque, sizeof(stateid_opaque_t));
  1739. ADJUST_ARGS();
  1740. }
  1741. ENCODE_SEQID_OP_TAIL(locku->lu_stateowner);
  1742. }
  1743. static void
  1744. nfsd4_encode_link(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_link *link)
  1745. {
  1746. ENCODE_HEAD;
  1747. if (!nfserr) {
  1748. RESERVE_SPACE(20);
  1749. WRITECINFO(link->li_cinfo);
  1750. ADJUST_ARGS();
  1751. }
  1752. }
  1753. static void
  1754. nfsd4_encode_open(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_open *open)
  1755. {
  1756. ENCODE_SEQID_OP_HEAD;
  1757. if (nfserr)
  1758. goto out;
  1759. RESERVE_SPACE(36 + sizeof(stateid_t));
  1760. WRITE32(open->op_stateid.si_generation);
  1761. WRITEMEM(&open->op_stateid.si_opaque, sizeof(stateid_opaque_t));
  1762. WRITECINFO(open->op_cinfo);
  1763. WRITE32(open->op_rflags);
  1764. WRITE32(2);
  1765. WRITE32(open->op_bmval[0]);
  1766. WRITE32(open->op_bmval[1]);
  1767. WRITE32(open->op_delegate_type);
  1768. ADJUST_ARGS();
  1769. switch (open->op_delegate_type) {
  1770. case NFS4_OPEN_DELEGATE_NONE:
  1771. break;
  1772. case NFS4_OPEN_DELEGATE_READ:
  1773. RESERVE_SPACE(20 + sizeof(stateid_t));
  1774. WRITEMEM(&open->op_delegate_stateid, sizeof(stateid_t));
  1775. WRITE32(open->op_recall);
  1776. /*
  1777. * TODO: ACE's in delegations
  1778. */
  1779. WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  1780. WRITE32(0);
  1781. WRITE32(0);
  1782. WRITE32(0); /* XXX: is NULL principal ok? */
  1783. ADJUST_ARGS();
  1784. break;
  1785. case NFS4_OPEN_DELEGATE_WRITE:
  1786. RESERVE_SPACE(32 + sizeof(stateid_t));
  1787. WRITEMEM(&open->op_delegate_stateid, sizeof(stateid_t));
  1788. WRITE32(0);
  1789. /*
  1790. * TODO: space_limit's in delegations
  1791. */
  1792. WRITE32(NFS4_LIMIT_SIZE);
  1793. WRITE32(~(u32)0);
  1794. WRITE32(~(u32)0);
  1795. /*
  1796. * TODO: ACE's in delegations
  1797. */
  1798. WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  1799. WRITE32(0);
  1800. WRITE32(0);
  1801. WRITE32(0); /* XXX: is NULL principal ok? */
  1802. ADJUST_ARGS();
  1803. break;
  1804. default:
  1805. BUG();
  1806. }
  1807. /* XXX save filehandle here */
  1808. out:
  1809. ENCODE_SEQID_OP_TAIL(open->op_stateowner);
  1810. }
  1811. static void
  1812. nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_open_confirm *oc)
  1813. {
  1814. ENCODE_SEQID_OP_HEAD;
  1815. if (!nfserr) {
  1816. RESERVE_SPACE(sizeof(stateid_t));
  1817. WRITE32(oc->oc_resp_stateid.si_generation);
  1818. WRITEMEM(&oc->oc_resp_stateid.si_opaque, sizeof(stateid_opaque_t));
  1819. ADJUST_ARGS();
  1820. }
  1821. ENCODE_SEQID_OP_TAIL(oc->oc_stateowner);
  1822. }
  1823. static void
  1824. nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_open_downgrade *od)
  1825. {
  1826. ENCODE_SEQID_OP_HEAD;
  1827. if (!nfserr) {
  1828. RESERVE_SPACE(sizeof(stateid_t));
  1829. WRITE32(od->od_stateid.si_generation);
  1830. WRITEMEM(&od->od_stateid.si_opaque, sizeof(stateid_opaque_t));
  1831. ADJUST_ARGS();
  1832. }
  1833. ENCODE_SEQID_OP_TAIL(od->od_stateowner);
  1834. }
  1835. static int
  1836. nfsd4_encode_read(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_read *read)
  1837. {
  1838. u32 eof;
  1839. int v, pn;
  1840. unsigned long maxcount;
  1841. long len;
  1842. ENCODE_HEAD;
  1843. if (nfserr)
  1844. return nfserr;
  1845. if (resp->xbuf->page_len)
  1846. return nfserr_resource;
  1847. RESERVE_SPACE(8); /* eof flag and byte count */
  1848. maxcount = NFSSVC_MAXBLKSIZE;
  1849. if (maxcount > read->rd_length)
  1850. maxcount = read->rd_length;
  1851. len = maxcount;
  1852. v = 0;
  1853. while (len > 0) {
  1854. pn = resp->rqstp->rq_resused;
  1855. svc_take_page(resp->rqstp);
  1856. read->rd_iov[v].iov_base = page_address(resp->rqstp->rq_respages[pn]);
  1857. read->rd_iov[v].iov_len = len < PAGE_SIZE ? len : PAGE_SIZE;
  1858. v++;
  1859. len -= PAGE_SIZE;
  1860. }
  1861. read->rd_vlen = v;
  1862. nfserr = nfsd_read(read->rd_rqstp, read->rd_fhp, read->rd_filp,
  1863. read->rd_offset, read->rd_iov, read->rd_vlen,
  1864. &maxcount);
  1865. if (nfserr == nfserr_symlink)
  1866. nfserr = nfserr_inval;
  1867. if (nfserr)
  1868. return nfserr;
  1869. eof = (read->rd_offset + maxcount >= read->rd_fhp->fh_dentry->d_inode->i_size);
  1870. WRITE32(eof);
  1871. WRITE32(maxcount);
  1872. ADJUST_ARGS();
  1873. resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
  1874. resp->xbuf->page_len = maxcount;
  1875. /* read zero bytes -> don't set up tail */
  1876. if(!maxcount)
  1877. return 0;
  1878. /* set up page for remaining responses */
  1879. svc_take_page(resp->rqstp);
  1880. resp->xbuf->tail[0].iov_base =
  1881. page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
  1882. resp->rqstp->rq_restailpage = resp->rqstp->rq_resused-1;
  1883. resp->xbuf->tail[0].iov_len = 0;
  1884. resp->p = resp->xbuf->tail[0].iov_base;
  1885. resp->end = resp->p + PAGE_SIZE/4;
  1886. if (maxcount&3) {
  1887. *(resp->p)++ = 0;
  1888. resp->xbuf->tail[0].iov_base += maxcount&3;
  1889. resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
  1890. }
  1891. return 0;
  1892. }
  1893. static int
  1894. nfsd4_encode_readlink(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_readlink *readlink)
  1895. {
  1896. int maxcount;
  1897. char *page;
  1898. ENCODE_HEAD;
  1899. if (nfserr)
  1900. return nfserr;
  1901. if (resp->xbuf->page_len)
  1902. return nfserr_resource;
  1903. svc_take_page(resp->rqstp);
  1904. page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
  1905. maxcount = PAGE_SIZE;
  1906. RESERVE_SPACE(4);
  1907. /*
  1908. * XXX: By default, the ->readlink() VFS op will truncate symlinks
  1909. * if they would overflow the buffer. Is this kosher in NFSv4? If
  1910. * not, one easy fix is: if ->readlink() precisely fills the buffer,
  1911. * assume that truncation occurred, and return NFS4ERR_RESOURCE.
  1912. */
  1913. nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
  1914. if (nfserr == nfserr_isdir)
  1915. return nfserr_inval;
  1916. if (nfserr)
  1917. return nfserr;
  1918. WRITE32(maxcount);
  1919. ADJUST_ARGS();
  1920. resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
  1921. svc_take_page(resp->rqstp);
  1922. resp->xbuf->tail[0].iov_base =
  1923. page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
  1924. resp->rqstp->rq_restailpage = resp->rqstp->rq_resused-1;
  1925. resp->xbuf->tail[0].iov_len = 0;
  1926. resp->p = resp->xbuf->tail[0].iov_base;
  1927. resp->end = resp->p + PAGE_SIZE/4;
  1928. resp->xbuf->page_len = maxcount;
  1929. if (maxcount&3) {
  1930. *(resp->p)++ = 0;
  1931. resp->xbuf->tail[0].iov_base += maxcount&3;
  1932. resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
  1933. }
  1934. return 0;
  1935. }
  1936. static int
  1937. nfsd4_encode_readdir(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_readdir *readdir)
  1938. {
  1939. int maxcount;
  1940. loff_t offset;
  1941. u32 *page, *savep;
  1942. ENCODE_HEAD;
  1943. if (nfserr)
  1944. return nfserr;
  1945. if (resp->xbuf->page_len)
  1946. return nfserr_resource;
  1947. RESERVE_SPACE(8); /* verifier */
  1948. savep = p;
  1949. /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
  1950. WRITE32(0);
  1951. WRITE32(0);
  1952. ADJUST_ARGS();
  1953. resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
  1954. maxcount = PAGE_SIZE;
  1955. if (maxcount > readdir->rd_maxcount)
  1956. maxcount = readdir->rd_maxcount;
  1957. /*
  1958. * Convert from bytes to words, account for the two words already
  1959. * written, make sure to leave two words at the end for the next
  1960. * pointer and eof field.
  1961. */
  1962. maxcount = (maxcount >> 2) - 4;
  1963. if (maxcount < 0) {
  1964. nfserr = nfserr_toosmall;
  1965. goto err_no_verf;
  1966. }
  1967. svc_take_page(resp->rqstp);
  1968. page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
  1969. readdir->common.err = 0;
  1970. readdir->buflen = maxcount;
  1971. readdir->buffer = page;
  1972. readdir->offset = NULL;
  1973. offset = readdir->rd_cookie;
  1974. nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
  1975. &offset,
  1976. &readdir->common, nfsd4_encode_dirent);
  1977. if (nfserr == nfs_ok &&
  1978. readdir->common.err == nfserr_toosmall &&
  1979. readdir->buffer == page)
  1980. nfserr = nfserr_toosmall;
  1981. if (nfserr == nfserr_symlink)
  1982. nfserr = nfserr_notdir;
  1983. if (nfserr)
  1984. goto err_no_verf;
  1985. if (readdir->offset)
  1986. xdr_encode_hyper(readdir->offset, offset);
  1987. p = readdir->buffer;
  1988. *p++ = 0; /* no more entries */
  1989. *p++ = htonl(readdir->common.err == nfserr_eof);
  1990. resp->xbuf->page_len = ((char*)p) - (char*)page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
  1991. /* allocate a page for the tail */
  1992. svc_take_page(resp->rqstp);
  1993. resp->xbuf->tail[0].iov_base =
  1994. page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
  1995. resp->rqstp->rq_restailpage = resp->rqstp->rq_resused-1;
  1996. resp->xbuf->tail[0].iov_len = 0;
  1997. resp->p = resp->xbuf->tail[0].iov_base;
  1998. resp->end = resp->p + PAGE_SIZE/4;
  1999. return 0;
  2000. err_no_verf:
  2001. p = savep;
  2002. ADJUST_ARGS();
  2003. return nfserr;
  2004. }
  2005. static void
  2006. nfsd4_encode_remove(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_remove *remove)
  2007. {
  2008. ENCODE_HEAD;
  2009. if (!nfserr) {
  2010. RESERVE_SPACE(20);
  2011. WRITECINFO(remove->rm_cinfo);
  2012. ADJUST_ARGS();
  2013. }
  2014. }
  2015. static void
  2016. nfsd4_encode_rename(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_rename *rename)
  2017. {
  2018. ENCODE_HEAD;
  2019. if (!nfserr) {
  2020. RESERVE_SPACE(40);
  2021. WRITECINFO(rename->rn_sinfo);
  2022. WRITECINFO(rename->rn_tinfo);
  2023. ADJUST_ARGS();
  2024. }
  2025. }
  2026. /*
  2027. * The SETATTR encode routine is special -- it always encodes a bitmap,
  2028. * regardless of the error status.
  2029. */
  2030. static void
  2031. nfsd4_encode_setattr(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_setattr *setattr)
  2032. {
  2033. ENCODE_HEAD;
  2034. RESERVE_SPACE(12);
  2035. if (nfserr) {
  2036. WRITE32(2);
  2037. WRITE32(0);
  2038. WRITE32(0);
  2039. }
  2040. else {
  2041. WRITE32(2);
  2042. WRITE32(setattr->sa_bmval[0]);
  2043. WRITE32(setattr->sa_bmval[1]);
  2044. }
  2045. ADJUST_ARGS();
  2046. }
  2047. static void
  2048. nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_setclientid *scd)
  2049. {
  2050. ENCODE_HEAD;
  2051. if (!nfserr) {
  2052. RESERVE_SPACE(8 + sizeof(nfs4_verifier));
  2053. WRITEMEM(&scd->se_clientid, 8);
  2054. WRITEMEM(&scd->se_confirm, sizeof(nfs4_verifier));
  2055. ADJUST_ARGS();
  2056. }
  2057. else if (nfserr == nfserr_clid_inuse) {
  2058. RESERVE_SPACE(8);
  2059. WRITE32(0);
  2060. WRITE32(0);
  2061. ADJUST_ARGS();
  2062. }
  2063. }
  2064. static void
  2065. nfsd4_encode_write(struct nfsd4_compoundres *resp, int nfserr, struct nfsd4_write *write)
  2066. {
  2067. ENCODE_HEAD;
  2068. if (!nfserr) {
  2069. RESERVE_SPACE(16);
  2070. WRITE32(write->wr_bytes_written);
  2071. WRITE32(write->wr_how_written);
  2072. WRITEMEM(write->wr_verifier.data, 8);
  2073. ADJUST_ARGS();
  2074. }
  2075. }
  2076. void
  2077. nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  2078. {
  2079. u32 *statp;
  2080. ENCODE_HEAD;
  2081. RESERVE_SPACE(8);
  2082. WRITE32(op->opnum);
  2083. statp = p++; /* to be backfilled at the end */
  2084. ADJUST_ARGS();
  2085. switch (op->opnum) {
  2086. case OP_ACCESS:
  2087. nfsd4_encode_access(resp, op->status, &op->u.access);
  2088. break;
  2089. case OP_CLOSE:
  2090. nfsd4_encode_close(resp, op->status, &op->u.close);
  2091. break;
  2092. case OP_COMMIT:
  2093. nfsd4_encode_commit(resp, op->status, &op->u.commit);
  2094. break;
  2095. case OP_CREATE:
  2096. nfsd4_encode_create(resp, op->status, &op->u.create);
  2097. break;
  2098. case OP_DELEGRETURN:
  2099. break;
  2100. case OP_GETATTR:
  2101. op->status = nfsd4_encode_getattr(resp, op->status, &op->u.getattr);
  2102. break;
  2103. case OP_GETFH:
  2104. nfsd4_encode_getfh(resp, op->status, op->u.getfh);
  2105. break;
  2106. case OP_LINK:
  2107. nfsd4_encode_link(resp, op->status, &op->u.link);
  2108. break;
  2109. case OP_LOCK:
  2110. nfsd4_encode_lock(resp, op->status, &op->u.lock);
  2111. break;
  2112. case OP_LOCKT:
  2113. nfsd4_encode_lockt(resp, op->status, &op->u.lockt);
  2114. break;
  2115. case OP_LOCKU:
  2116. nfsd4_encode_locku(resp, op->status, &op->u.locku);
  2117. break;
  2118. case OP_LOOKUP:
  2119. break;
  2120. case OP_LOOKUPP:
  2121. break;
  2122. case OP_NVERIFY:
  2123. break;
  2124. case OP_OPEN:
  2125. nfsd4_encode_open(resp, op->status, &op->u.open);
  2126. break;
  2127. case OP_OPEN_CONFIRM:
  2128. nfsd4_encode_open_confirm(resp, op->status, &op->u.open_confirm);
  2129. break;
  2130. case OP_OPEN_DOWNGRADE:
  2131. nfsd4_encode_open_downgrade(resp, op->status, &op->u.open_downgrade);
  2132. break;
  2133. case OP_PUTFH:
  2134. break;
  2135. case OP_PUTROOTFH:
  2136. break;
  2137. case OP_READ:
  2138. op->status = nfsd4_encode_read(resp, op->status, &op->u.read);
  2139. break;
  2140. case OP_READDIR:
  2141. op->status = nfsd4_encode_readdir(resp, op->status, &op->u.readdir);
  2142. break;
  2143. case OP_READLINK:
  2144. op->status = nfsd4_encode_readlink(resp, op->status, &op->u.readlink);
  2145. break;
  2146. case OP_REMOVE:
  2147. nfsd4_encode_remove(resp, op->status, &op->u.remove);
  2148. break;
  2149. case OP_RENAME:
  2150. nfsd4_encode_rename(resp, op->status, &op->u.rename);
  2151. break;
  2152. case OP_RENEW:
  2153. break;
  2154. case OP_RESTOREFH:
  2155. break;
  2156. case OP_SAVEFH:
  2157. break;
  2158. case OP_SETATTR:
  2159. nfsd4_encode_setattr(resp, op->status, &op->u.setattr);
  2160. break;
  2161. case OP_SETCLIENTID:
  2162. nfsd4_encode_setclientid(resp, op->status, &op->u.setclientid);
  2163. break;
  2164. case OP_SETCLIENTID_CONFIRM:
  2165. break;
  2166. case OP_VERIFY:
  2167. break;
  2168. case OP_WRITE:
  2169. nfsd4_encode_write(resp, op->status, &op->u.write);
  2170. break;
  2171. case OP_RELEASE_LOCKOWNER:
  2172. break;
  2173. default:
  2174. break;
  2175. }
  2176. /*
  2177. * Note: We write the status directly, instead of using WRITE32(),
  2178. * since it is already in network byte order.
  2179. */
  2180. *statp = op->status;
  2181. }
  2182. /*
  2183. * Encode the reply stored in the stateowner reply cache
  2184. *
  2185. * XDR note: do not encode rp->rp_buflen: the buffer contains the
  2186. * previously sent already encoded operation.
  2187. *
  2188. * called with nfs4_lock_state() held
  2189. */
  2190. void
  2191. nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  2192. {
  2193. ENCODE_HEAD;
  2194. struct nfs4_replay *rp = op->replay;
  2195. BUG_ON(!rp);
  2196. RESERVE_SPACE(8);
  2197. WRITE32(op->opnum);
  2198. *p++ = rp->rp_status; /* already xdr'ed */
  2199. ADJUST_ARGS();
  2200. RESERVE_SPACE(rp->rp_buflen);
  2201. WRITEMEM(rp->rp_buf, rp->rp_buflen);
  2202. ADJUST_ARGS();
  2203. }
  2204. /*
  2205. * END OF "GENERIC" ENCODE ROUTINES.
  2206. */
  2207. int
  2208. nfs4svc_encode_voidres(struct svc_rqst *rqstp, u32 *p, void *dummy)
  2209. {
  2210. return xdr_ressize_check(rqstp, p);
  2211. }
  2212. void nfsd4_release_compoundargs(struct nfsd4_compoundargs *args)
  2213. {
  2214. if (args->ops != args->iops) {
  2215. kfree(args->ops);
  2216. args->ops = args->iops;
  2217. }
  2218. if (args->tmpp) {
  2219. kfree(args->tmpp);
  2220. args->tmpp = NULL;
  2221. }
  2222. while (args->to_free) {
  2223. struct tmpbuf *tb = args->to_free;
  2224. args->to_free = tb->next;
  2225. tb->release(tb->buf);
  2226. kfree(tb);
  2227. }
  2228. }
  2229. int
  2230. nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, u32 *p, struct nfsd4_compoundargs *args)
  2231. {
  2232. int status;
  2233. args->p = p;
  2234. args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
  2235. args->pagelist = rqstp->rq_arg.pages;
  2236. args->pagelen = rqstp->rq_arg.page_len;
  2237. args->tmpp = NULL;
  2238. args->to_free = NULL;
  2239. args->ops = args->iops;
  2240. args->rqstp = rqstp;
  2241. status = nfsd4_decode_compound(args);
  2242. if (status) {
  2243. nfsd4_release_compoundargs(args);
  2244. }
  2245. return !status;
  2246. }
  2247. int
  2248. nfs4svc_encode_compoundres(struct svc_rqst *rqstp, u32 *p, struct nfsd4_compoundres *resp)
  2249. {
  2250. /*
  2251. * All that remains is to write the tag and operation count...
  2252. */
  2253. struct kvec *iov;
  2254. p = resp->tagp;
  2255. *p++ = htonl(resp->taglen);
  2256. memcpy(p, resp->tag, resp->taglen);
  2257. p += XDR_QUADLEN(resp->taglen);
  2258. *p++ = htonl(resp->opcnt);
  2259. if (rqstp->rq_res.page_len)
  2260. iov = &rqstp->rq_res.tail[0];
  2261. else
  2262. iov = &rqstp->rq_res.head[0];
  2263. iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
  2264. BUG_ON(iov->iov_len > PAGE_SIZE);
  2265. return 1;
  2266. }
  2267. /*
  2268. * Local variables:
  2269. * c-basic-offset: 8
  2270. * End:
  2271. */