nfs4xdr.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516
  1. /*
  2. * Server-side XDR for NFSv4
  3. *
  4. * Copyright (c) 2002 The Regents of the University of Michigan.
  5. * All rights reserved.
  6. *
  7. * Kendrick Smith <kmsmith@umich.edu>
  8. * Andy Adamson <andros@umich.edu>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * TODO: Neil Brown made the following observation: We currently
  36. * initially reserve NFSD_BUFSIZE space on the transmit queue and
  37. * never release any of that until the request is complete.
  38. * It would be good to calculate a new maximum response size while
  39. * decoding the COMPOUND, and call svc_reserve with this number
  40. * at the end of nfs4svc_decode_compoundargs.
  41. */
  42. #include <linux/slab.h>
  43. #include <linux/namei.h>
  44. #include <linux/statfs.h>
  45. #include <linux/utsname.h>
  46. #include <linux/pagemap.h>
  47. #include <linux/sunrpc/svcauth_gss.h>
  48. #include "idmap.h"
  49. #include "acl.h"
  50. #include "xdr4.h"
  51. #include "vfs.h"
  52. #include "state.h"
  53. #include "cache.h"
  54. #define NFSDDBG_FACILITY NFSDDBG_XDR
  55. /*
  56. * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
  57. * directory in order to indicate to the client that a filesystem boundary is present
  58. * We use a fixed fsid for a referral
  59. */
  60. #define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
  61. #define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
  62. static __be32
  63. check_filename(char *str, int len, __be32 err)
  64. {
  65. int i;
  66. if (len == 0)
  67. return nfserr_inval;
  68. if (isdotent(str, len))
  69. return err;
  70. for (i = 0; i < len; i++)
  71. if (str[i] == '/')
  72. return err;
  73. return 0;
  74. }
  75. #define DECODE_HEAD \
  76. __be32 *p; \
  77. __be32 status
  78. #define DECODE_TAIL \
  79. status = 0; \
  80. out: \
  81. return status; \
  82. xdr_error: \
  83. dprintk("NFSD: xdr error (%s:%d)\n", \
  84. __FILE__, __LINE__); \
  85. status = nfserr_bad_xdr; \
  86. goto out
  87. #define READ32(x) (x) = ntohl(*p++)
  88. #define READ64(x) do { \
  89. (x) = (u64)ntohl(*p++) << 32; \
  90. (x) |= ntohl(*p++); \
  91. } while (0)
  92. #define READTIME(x) do { \
  93. p++; \
  94. (x) = ntohl(*p++); \
  95. p++; \
  96. } while (0)
  97. #define READMEM(x,nbytes) do { \
  98. x = (char *)p; \
  99. p += XDR_QUADLEN(nbytes); \
  100. } while (0)
  101. #define SAVEMEM(x,nbytes) do { \
  102. if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
  103. savemem(argp, p, nbytes) : \
  104. (char *)p)) { \
  105. dprintk("NFSD: xdr error (%s:%d)\n", \
  106. __FILE__, __LINE__); \
  107. goto xdr_error; \
  108. } \
  109. p += XDR_QUADLEN(nbytes); \
  110. } while (0)
  111. #define COPYMEM(x,nbytes) do { \
  112. memcpy((x), p, nbytes); \
  113. p += XDR_QUADLEN(nbytes); \
  114. } while (0)
  115. /* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
  116. #define READ_BUF(nbytes) do { \
  117. if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
  118. p = argp->p; \
  119. argp->p += XDR_QUADLEN(nbytes); \
  120. } else if (!(p = read_buf(argp, nbytes))) { \
  121. dprintk("NFSD: xdr error (%s:%d)\n", \
  122. __FILE__, __LINE__); \
  123. goto xdr_error; \
  124. } \
  125. } while (0)
  126. static void save_buf(struct nfsd4_compoundargs *argp, struct nfsd4_saved_compoundargs *savep)
  127. {
  128. savep->p = argp->p;
  129. savep->end = argp->end;
  130. savep->pagelen = argp->pagelen;
  131. savep->pagelist = argp->pagelist;
  132. }
  133. static void restore_buf(struct nfsd4_compoundargs *argp, struct nfsd4_saved_compoundargs *savep)
  134. {
  135. argp->p = savep->p;
  136. argp->end = savep->end;
  137. argp->pagelen = savep->pagelen;
  138. argp->pagelist = savep->pagelist;
  139. }
  140. static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
  141. {
  142. /* We want more bytes than seem to be available.
  143. * Maybe we need a new page, maybe we have just run out
  144. */
  145. unsigned int avail = (char *)argp->end - (char *)argp->p;
  146. __be32 *p;
  147. if (avail + argp->pagelen < nbytes)
  148. return NULL;
  149. if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
  150. return NULL;
  151. /* ok, we can do it with the current plus the next page */
  152. if (nbytes <= sizeof(argp->tmp))
  153. p = argp->tmp;
  154. else {
  155. kfree(argp->tmpp);
  156. p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
  157. if (!p)
  158. return NULL;
  159. }
  160. /*
  161. * The following memcpy is safe because read_buf is always
  162. * called with nbytes > avail, and the two cases above both
  163. * guarantee p points to at least nbytes bytes.
  164. */
  165. memcpy(p, argp->p, avail);
  166. /* step to next page */
  167. argp->p = page_address(argp->pagelist[0]);
  168. argp->pagelist++;
  169. if (argp->pagelen < PAGE_SIZE) {
  170. argp->end = argp->p + (argp->pagelen>>2);
  171. argp->pagelen = 0;
  172. } else {
  173. argp->end = argp->p + (PAGE_SIZE>>2);
  174. argp->pagelen -= PAGE_SIZE;
  175. }
  176. memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
  177. argp->p += XDR_QUADLEN(nbytes - avail);
  178. return p;
  179. }
  180. static int zero_clientid(clientid_t *clid)
  181. {
  182. return (clid->cl_boot == 0) && (clid->cl_id == 0);
  183. }
  184. static int
  185. defer_free(struct nfsd4_compoundargs *argp,
  186. void (*release)(const void *), void *p)
  187. {
  188. struct tmpbuf *tb;
  189. tb = kmalloc(sizeof(*tb), GFP_KERNEL);
  190. if (!tb)
  191. return -ENOMEM;
  192. tb->buf = p;
  193. tb->release = release;
  194. tb->next = argp->to_free;
  195. argp->to_free = tb;
  196. return 0;
  197. }
  198. static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
  199. {
  200. if (p == argp->tmp) {
  201. p = kmalloc(nbytes, GFP_KERNEL);
  202. if (!p)
  203. return NULL;
  204. memcpy(p, argp->tmp, nbytes);
  205. } else {
  206. BUG_ON(p != argp->tmpp);
  207. argp->tmpp = NULL;
  208. }
  209. if (defer_free(argp, kfree, p)) {
  210. kfree(p);
  211. return NULL;
  212. } else
  213. return (char *)p;
  214. }
  215. static __be32
  216. nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
  217. {
  218. u32 bmlen;
  219. DECODE_HEAD;
  220. bmval[0] = 0;
  221. bmval[1] = 0;
  222. bmval[2] = 0;
  223. READ_BUF(4);
  224. READ32(bmlen);
  225. if (bmlen > 1000)
  226. goto xdr_error;
  227. READ_BUF(bmlen << 2);
  228. if (bmlen > 0)
  229. READ32(bmval[0]);
  230. if (bmlen > 1)
  231. READ32(bmval[1]);
  232. if (bmlen > 2)
  233. READ32(bmval[2]);
  234. DECODE_TAIL;
  235. }
  236. static __be32
  237. nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
  238. struct iattr *iattr, struct nfs4_acl **acl)
  239. {
  240. int expected_len, len = 0;
  241. u32 dummy32;
  242. char *buf;
  243. int host_err;
  244. DECODE_HEAD;
  245. iattr->ia_valid = 0;
  246. if ((status = nfsd4_decode_bitmap(argp, bmval)))
  247. return status;
  248. READ_BUF(4);
  249. READ32(expected_len);
  250. if (bmval[0] & FATTR4_WORD0_SIZE) {
  251. READ_BUF(8);
  252. len += 8;
  253. READ64(iattr->ia_size);
  254. iattr->ia_valid |= ATTR_SIZE;
  255. }
  256. if (bmval[0] & FATTR4_WORD0_ACL) {
  257. int nace;
  258. struct nfs4_ace *ace;
  259. READ_BUF(4); len += 4;
  260. READ32(nace);
  261. if (nace > NFS4_ACL_MAX)
  262. return nfserr_resource;
  263. *acl = nfs4_acl_new(nace);
  264. if (*acl == NULL) {
  265. host_err = -ENOMEM;
  266. goto out_nfserr;
  267. }
  268. defer_free(argp, kfree, *acl);
  269. (*acl)->naces = nace;
  270. for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
  271. READ_BUF(16); len += 16;
  272. READ32(ace->type);
  273. READ32(ace->flag);
  274. READ32(ace->access_mask);
  275. READ32(dummy32);
  276. READ_BUF(dummy32);
  277. len += XDR_QUADLEN(dummy32) << 2;
  278. READMEM(buf, dummy32);
  279. ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
  280. status = nfs_ok;
  281. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  282. ace->who = 0;
  283. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  284. status = nfsd_map_name_to_gid(argp->rqstp,
  285. buf, dummy32, &ace->who);
  286. else
  287. status = nfsd_map_name_to_uid(argp->rqstp,
  288. buf, dummy32, &ace->who);
  289. if (status)
  290. return status;
  291. }
  292. } else
  293. *acl = NULL;
  294. if (bmval[1] & FATTR4_WORD1_MODE) {
  295. READ_BUF(4);
  296. len += 4;
  297. READ32(iattr->ia_mode);
  298. iattr->ia_mode &= (S_IFMT | S_IALLUGO);
  299. iattr->ia_valid |= ATTR_MODE;
  300. }
  301. if (bmval[1] & FATTR4_WORD1_OWNER) {
  302. READ_BUF(4);
  303. len += 4;
  304. READ32(dummy32);
  305. READ_BUF(dummy32);
  306. len += (XDR_QUADLEN(dummy32) << 2);
  307. READMEM(buf, dummy32);
  308. if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
  309. return status;
  310. iattr->ia_valid |= ATTR_UID;
  311. }
  312. if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
  313. READ_BUF(4);
  314. len += 4;
  315. READ32(dummy32);
  316. READ_BUF(dummy32);
  317. len += (XDR_QUADLEN(dummy32) << 2);
  318. READMEM(buf, dummy32);
  319. if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
  320. return status;
  321. iattr->ia_valid |= ATTR_GID;
  322. }
  323. if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
  324. READ_BUF(4);
  325. len += 4;
  326. READ32(dummy32);
  327. switch (dummy32) {
  328. case NFS4_SET_TO_CLIENT_TIME:
  329. /* We require the high 32 bits of 'seconds' to be 0, and we ignore
  330. all 32 bits of 'nseconds'. */
  331. READ_BUF(12);
  332. len += 12;
  333. READ32(dummy32);
  334. if (dummy32)
  335. return nfserr_inval;
  336. READ32(iattr->ia_atime.tv_sec);
  337. READ32(iattr->ia_atime.tv_nsec);
  338. if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
  339. return nfserr_inval;
  340. iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
  341. break;
  342. case NFS4_SET_TO_SERVER_TIME:
  343. iattr->ia_valid |= ATTR_ATIME;
  344. break;
  345. default:
  346. goto xdr_error;
  347. }
  348. }
  349. if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
  350. READ_BUF(4);
  351. len += 4;
  352. READ32(dummy32);
  353. switch (dummy32) {
  354. case NFS4_SET_TO_CLIENT_TIME:
  355. /* We require the high 32 bits of 'seconds' to be 0, and we ignore
  356. all 32 bits of 'nseconds'. */
  357. READ_BUF(12);
  358. len += 12;
  359. READ32(dummy32);
  360. if (dummy32)
  361. return nfserr_inval;
  362. READ32(iattr->ia_mtime.tv_sec);
  363. READ32(iattr->ia_mtime.tv_nsec);
  364. if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
  365. return nfserr_inval;
  366. iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
  367. break;
  368. case NFS4_SET_TO_SERVER_TIME:
  369. iattr->ia_valid |= ATTR_MTIME;
  370. break;
  371. default:
  372. goto xdr_error;
  373. }
  374. }
  375. if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
  376. || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
  377. || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
  378. READ_BUF(expected_len - len);
  379. else if (len != expected_len)
  380. goto xdr_error;
  381. DECODE_TAIL;
  382. out_nfserr:
  383. status = nfserrno(host_err);
  384. goto out;
  385. }
  386. static __be32
  387. nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
  388. {
  389. DECODE_HEAD;
  390. READ_BUF(sizeof(stateid_t));
  391. READ32(sid->si_generation);
  392. COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
  393. DECODE_TAIL;
  394. }
  395. static __be32
  396. nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
  397. {
  398. DECODE_HEAD;
  399. READ_BUF(4);
  400. READ32(access->ac_req_access);
  401. DECODE_TAIL;
  402. }
  403. static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
  404. {
  405. DECODE_HEAD;
  406. READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
  407. COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  408. READ32(bcts->dir);
  409. /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
  410. * could help us figure out we should be using it. */
  411. DECODE_TAIL;
  412. }
  413. static __be32
  414. nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
  415. {
  416. DECODE_HEAD;
  417. READ_BUF(4);
  418. READ32(close->cl_seqid);
  419. return nfsd4_decode_stateid(argp, &close->cl_stateid);
  420. DECODE_TAIL;
  421. }
  422. static __be32
  423. nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
  424. {
  425. DECODE_HEAD;
  426. READ_BUF(12);
  427. READ64(commit->co_offset);
  428. READ32(commit->co_count);
  429. DECODE_TAIL;
  430. }
  431. static __be32
  432. nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
  433. {
  434. DECODE_HEAD;
  435. READ_BUF(4);
  436. READ32(create->cr_type);
  437. switch (create->cr_type) {
  438. case NF4LNK:
  439. READ_BUF(4);
  440. READ32(create->cr_linklen);
  441. READ_BUF(create->cr_linklen);
  442. SAVEMEM(create->cr_linkname, create->cr_linklen);
  443. break;
  444. case NF4BLK:
  445. case NF4CHR:
  446. READ_BUF(8);
  447. READ32(create->cr_specdata1);
  448. READ32(create->cr_specdata2);
  449. break;
  450. case NF4SOCK:
  451. case NF4FIFO:
  452. case NF4DIR:
  453. default:
  454. break;
  455. }
  456. READ_BUF(4);
  457. READ32(create->cr_namelen);
  458. READ_BUF(create->cr_namelen);
  459. SAVEMEM(create->cr_name, create->cr_namelen);
  460. if ((status = check_filename(create->cr_name, create->cr_namelen, nfserr_inval)))
  461. return status;
  462. status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
  463. &create->cr_acl);
  464. if (status)
  465. goto out;
  466. DECODE_TAIL;
  467. }
  468. static inline __be32
  469. nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
  470. {
  471. return nfsd4_decode_stateid(argp, &dr->dr_stateid);
  472. }
  473. static inline __be32
  474. nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
  475. {
  476. return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
  477. }
  478. static __be32
  479. nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
  480. {
  481. DECODE_HEAD;
  482. READ_BUF(4);
  483. READ32(link->li_namelen);
  484. READ_BUF(link->li_namelen);
  485. SAVEMEM(link->li_name, link->li_namelen);
  486. if ((status = check_filename(link->li_name, link->li_namelen, nfserr_inval)))
  487. return status;
  488. DECODE_TAIL;
  489. }
  490. static __be32
  491. nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
  492. {
  493. DECODE_HEAD;
  494. /*
  495. * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
  496. */
  497. READ_BUF(28);
  498. READ32(lock->lk_type);
  499. if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
  500. goto xdr_error;
  501. READ32(lock->lk_reclaim);
  502. READ64(lock->lk_offset);
  503. READ64(lock->lk_length);
  504. READ32(lock->lk_is_new);
  505. if (lock->lk_is_new) {
  506. READ_BUF(4);
  507. READ32(lock->lk_new_open_seqid);
  508. status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
  509. if (status)
  510. return status;
  511. READ_BUF(8 + sizeof(clientid_t));
  512. READ32(lock->lk_new_lock_seqid);
  513. COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
  514. READ32(lock->lk_new_owner.len);
  515. READ_BUF(lock->lk_new_owner.len);
  516. READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
  517. } else {
  518. status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
  519. if (status)
  520. return status;
  521. READ_BUF(4);
  522. READ32(lock->lk_old_lock_seqid);
  523. }
  524. DECODE_TAIL;
  525. }
  526. static __be32
  527. nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
  528. {
  529. DECODE_HEAD;
  530. READ_BUF(32);
  531. READ32(lockt->lt_type);
  532. if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
  533. goto xdr_error;
  534. READ64(lockt->lt_offset);
  535. READ64(lockt->lt_length);
  536. COPYMEM(&lockt->lt_clientid, 8);
  537. READ32(lockt->lt_owner.len);
  538. READ_BUF(lockt->lt_owner.len);
  539. READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
  540. DECODE_TAIL;
  541. }
  542. static __be32
  543. nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
  544. {
  545. DECODE_HEAD;
  546. READ_BUF(8);
  547. READ32(locku->lu_type);
  548. if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
  549. goto xdr_error;
  550. READ32(locku->lu_seqid);
  551. status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
  552. if (status)
  553. return status;
  554. READ_BUF(16);
  555. READ64(locku->lu_offset);
  556. READ64(locku->lu_length);
  557. DECODE_TAIL;
  558. }
  559. static __be32
  560. nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
  561. {
  562. DECODE_HEAD;
  563. READ_BUF(4);
  564. READ32(lookup->lo_len);
  565. READ_BUF(lookup->lo_len);
  566. SAVEMEM(lookup->lo_name, lookup->lo_len);
  567. if ((status = check_filename(lookup->lo_name, lookup->lo_len, nfserr_noent)))
  568. return status;
  569. DECODE_TAIL;
  570. }
  571. static __be32
  572. nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
  573. {
  574. DECODE_HEAD;
  575. memset(open->op_bmval, 0, sizeof(open->op_bmval));
  576. open->op_iattr.ia_valid = 0;
  577. open->op_stateowner = NULL;
  578. /* seqid, share_access, share_deny, clientid, ownerlen */
  579. READ_BUF(16 + sizeof(clientid_t));
  580. READ32(open->op_seqid);
  581. READ32(open->op_share_access);
  582. READ32(open->op_share_deny);
  583. COPYMEM(&open->op_clientid, sizeof(clientid_t));
  584. READ32(open->op_owner.len);
  585. /* owner, open_flag */
  586. READ_BUF(open->op_owner.len + 4);
  587. SAVEMEM(open->op_owner.data, open->op_owner.len);
  588. READ32(open->op_create);
  589. switch (open->op_create) {
  590. case NFS4_OPEN_NOCREATE:
  591. break;
  592. case NFS4_OPEN_CREATE:
  593. READ_BUF(4);
  594. READ32(open->op_createmode);
  595. switch (open->op_createmode) {
  596. case NFS4_CREATE_UNCHECKED:
  597. case NFS4_CREATE_GUARDED:
  598. status = nfsd4_decode_fattr(argp, open->op_bmval,
  599. &open->op_iattr, &open->op_acl);
  600. if (status)
  601. goto out;
  602. break;
  603. case NFS4_CREATE_EXCLUSIVE:
  604. READ_BUF(8);
  605. COPYMEM(open->op_verf.data, 8);
  606. break;
  607. case NFS4_CREATE_EXCLUSIVE4_1:
  608. if (argp->minorversion < 1)
  609. goto xdr_error;
  610. READ_BUF(8);
  611. COPYMEM(open->op_verf.data, 8);
  612. status = nfsd4_decode_fattr(argp, open->op_bmval,
  613. &open->op_iattr, &open->op_acl);
  614. if (status)
  615. goto out;
  616. break;
  617. default:
  618. goto xdr_error;
  619. }
  620. break;
  621. default:
  622. goto xdr_error;
  623. }
  624. /* open_claim */
  625. READ_BUF(4);
  626. READ32(open->op_claim_type);
  627. switch (open->op_claim_type) {
  628. case NFS4_OPEN_CLAIM_NULL:
  629. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  630. READ_BUF(4);
  631. READ32(open->op_fname.len);
  632. READ_BUF(open->op_fname.len);
  633. SAVEMEM(open->op_fname.data, open->op_fname.len);
  634. if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
  635. return status;
  636. break;
  637. case NFS4_OPEN_CLAIM_PREVIOUS:
  638. READ_BUF(4);
  639. READ32(open->op_delegate_type);
  640. break;
  641. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  642. status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
  643. if (status)
  644. return status;
  645. READ_BUF(4);
  646. READ32(open->op_fname.len);
  647. READ_BUF(open->op_fname.len);
  648. SAVEMEM(open->op_fname.data, open->op_fname.len);
  649. if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
  650. return status;
  651. break;
  652. default:
  653. goto xdr_error;
  654. }
  655. DECODE_TAIL;
  656. }
  657. static __be32
  658. nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
  659. {
  660. DECODE_HEAD;
  661. status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
  662. if (status)
  663. return status;
  664. READ_BUF(4);
  665. READ32(open_conf->oc_seqid);
  666. DECODE_TAIL;
  667. }
  668. static __be32
  669. nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
  670. {
  671. DECODE_HEAD;
  672. status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
  673. if (status)
  674. return status;
  675. READ_BUF(12);
  676. READ32(open_down->od_seqid);
  677. READ32(open_down->od_share_access);
  678. READ32(open_down->od_share_deny);
  679. DECODE_TAIL;
  680. }
  681. static __be32
  682. nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
  683. {
  684. DECODE_HEAD;
  685. READ_BUF(4);
  686. READ32(putfh->pf_fhlen);
  687. if (putfh->pf_fhlen > NFS4_FHSIZE)
  688. goto xdr_error;
  689. READ_BUF(putfh->pf_fhlen);
  690. SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
  691. DECODE_TAIL;
  692. }
  693. static __be32
  694. nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
  695. {
  696. DECODE_HEAD;
  697. status = nfsd4_decode_stateid(argp, &read->rd_stateid);
  698. if (status)
  699. return status;
  700. READ_BUF(12);
  701. READ64(read->rd_offset);
  702. READ32(read->rd_length);
  703. DECODE_TAIL;
  704. }
  705. static __be32
  706. nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
  707. {
  708. DECODE_HEAD;
  709. READ_BUF(24);
  710. READ64(readdir->rd_cookie);
  711. COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
  712. READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
  713. READ32(readdir->rd_maxcount);
  714. if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
  715. goto out;
  716. DECODE_TAIL;
  717. }
  718. static __be32
  719. nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
  720. {
  721. DECODE_HEAD;
  722. READ_BUF(4);
  723. READ32(remove->rm_namelen);
  724. READ_BUF(remove->rm_namelen);
  725. SAVEMEM(remove->rm_name, remove->rm_namelen);
  726. if ((status = check_filename(remove->rm_name, remove->rm_namelen, nfserr_noent)))
  727. return status;
  728. DECODE_TAIL;
  729. }
  730. static __be32
  731. nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
  732. {
  733. DECODE_HEAD;
  734. READ_BUF(4);
  735. READ32(rename->rn_snamelen);
  736. READ_BUF(rename->rn_snamelen + 4);
  737. SAVEMEM(rename->rn_sname, rename->rn_snamelen);
  738. READ32(rename->rn_tnamelen);
  739. READ_BUF(rename->rn_tnamelen);
  740. SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
  741. if ((status = check_filename(rename->rn_sname, rename->rn_snamelen, nfserr_noent)))
  742. return status;
  743. if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen, nfserr_inval)))
  744. return status;
  745. DECODE_TAIL;
  746. }
  747. static __be32
  748. nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
  749. {
  750. DECODE_HEAD;
  751. READ_BUF(sizeof(clientid_t));
  752. COPYMEM(clientid, sizeof(clientid_t));
  753. DECODE_TAIL;
  754. }
  755. static __be32
  756. nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
  757. struct nfsd4_secinfo *secinfo)
  758. {
  759. DECODE_HEAD;
  760. READ_BUF(4);
  761. READ32(secinfo->si_namelen);
  762. READ_BUF(secinfo->si_namelen);
  763. SAVEMEM(secinfo->si_name, secinfo->si_namelen);
  764. status = check_filename(secinfo->si_name, secinfo->si_namelen,
  765. nfserr_noent);
  766. if (status)
  767. return status;
  768. DECODE_TAIL;
  769. }
  770. static __be32
  771. nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
  772. struct nfsd4_secinfo_no_name *sin)
  773. {
  774. DECODE_HEAD;
  775. READ_BUF(4);
  776. READ32(sin->sin_style);
  777. DECODE_TAIL;
  778. }
  779. static __be32
  780. nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
  781. {
  782. __be32 status;
  783. status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
  784. if (status)
  785. return status;
  786. return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
  787. &setattr->sa_acl);
  788. }
  789. static __be32
  790. nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
  791. {
  792. DECODE_HEAD;
  793. READ_BUF(12);
  794. COPYMEM(setclientid->se_verf.data, 8);
  795. READ32(setclientid->se_namelen);
  796. READ_BUF(setclientid->se_namelen + 8);
  797. SAVEMEM(setclientid->se_name, setclientid->se_namelen);
  798. READ32(setclientid->se_callback_prog);
  799. READ32(setclientid->se_callback_netid_len);
  800. READ_BUF(setclientid->se_callback_netid_len + 4);
  801. SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
  802. READ32(setclientid->se_callback_addr_len);
  803. READ_BUF(setclientid->se_callback_addr_len + 4);
  804. SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
  805. READ32(setclientid->se_callback_ident);
  806. DECODE_TAIL;
  807. }
  808. static __be32
  809. nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
  810. {
  811. DECODE_HEAD;
  812. READ_BUF(8 + sizeof(nfs4_verifier));
  813. COPYMEM(&scd_c->sc_clientid, 8);
  814. COPYMEM(&scd_c->sc_confirm, sizeof(nfs4_verifier));
  815. DECODE_TAIL;
  816. }
  817. /* Also used for NVERIFY */
  818. static __be32
  819. nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
  820. {
  821. #if 0
  822. struct nfsd4_compoundargs save = {
  823. .p = argp->p,
  824. .end = argp->end,
  825. .rqstp = argp->rqstp,
  826. };
  827. u32 ve_bmval[2];
  828. struct iattr ve_iattr; /* request */
  829. struct nfs4_acl *ve_acl; /* request */
  830. #endif
  831. DECODE_HEAD;
  832. if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
  833. goto out;
  834. /* For convenience's sake, we compare raw xdr'd attributes in
  835. * nfsd4_proc_verify; however we still decode here just to return
  836. * correct error in case of bad xdr. */
  837. #if 0
  838. status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
  839. if (status == nfserr_inval) {
  840. status = nfserrno(status);
  841. goto out;
  842. }
  843. #endif
  844. READ_BUF(4);
  845. READ32(verify->ve_attrlen);
  846. READ_BUF(verify->ve_attrlen);
  847. SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
  848. DECODE_TAIL;
  849. }
  850. static __be32
  851. nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
  852. {
  853. int avail;
  854. int v;
  855. int len;
  856. DECODE_HEAD;
  857. status = nfsd4_decode_stateid(argp, &write->wr_stateid);
  858. if (status)
  859. return status;
  860. READ_BUF(16);
  861. READ64(write->wr_offset);
  862. READ32(write->wr_stable_how);
  863. if (write->wr_stable_how > 2)
  864. goto xdr_error;
  865. READ32(write->wr_buflen);
  866. /* Sorry .. no magic macros for this.. *
  867. * READ_BUF(write->wr_buflen);
  868. * SAVEMEM(write->wr_buf, write->wr_buflen);
  869. */
  870. avail = (char*)argp->end - (char*)argp->p;
  871. if (avail + argp->pagelen < write->wr_buflen) {
  872. dprintk("NFSD: xdr error (%s:%d)\n",
  873. __FILE__, __LINE__);
  874. goto xdr_error;
  875. }
  876. argp->rqstp->rq_vec[0].iov_base = p;
  877. argp->rqstp->rq_vec[0].iov_len = avail;
  878. v = 0;
  879. len = write->wr_buflen;
  880. while (len > argp->rqstp->rq_vec[v].iov_len) {
  881. len -= argp->rqstp->rq_vec[v].iov_len;
  882. v++;
  883. argp->rqstp->rq_vec[v].iov_base = page_address(argp->pagelist[0]);
  884. argp->pagelist++;
  885. if (argp->pagelen >= PAGE_SIZE) {
  886. argp->rqstp->rq_vec[v].iov_len = PAGE_SIZE;
  887. argp->pagelen -= PAGE_SIZE;
  888. } else {
  889. argp->rqstp->rq_vec[v].iov_len = argp->pagelen;
  890. argp->pagelen -= len;
  891. }
  892. }
  893. argp->end = (__be32*) (argp->rqstp->rq_vec[v].iov_base + argp->rqstp->rq_vec[v].iov_len);
  894. argp->p = (__be32*) (argp->rqstp->rq_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
  895. argp->rqstp->rq_vec[v].iov_len = len;
  896. write->wr_vlen = v+1;
  897. DECODE_TAIL;
  898. }
  899. static __be32
  900. nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
  901. {
  902. DECODE_HEAD;
  903. READ_BUF(12);
  904. COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
  905. READ32(rlockowner->rl_owner.len);
  906. READ_BUF(rlockowner->rl_owner.len);
  907. READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
  908. if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
  909. return nfserr_inval;
  910. DECODE_TAIL;
  911. }
  912. static __be32
  913. nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
  914. struct nfsd4_exchange_id *exid)
  915. {
  916. int dummy, tmp;
  917. DECODE_HEAD;
  918. READ_BUF(NFS4_VERIFIER_SIZE);
  919. COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
  920. READ_BUF(4);
  921. READ32(exid->clname.len);
  922. READ_BUF(exid->clname.len);
  923. SAVEMEM(exid->clname.data, exid->clname.len);
  924. READ_BUF(4);
  925. READ32(exid->flags);
  926. /* Ignore state_protect4_a */
  927. READ_BUF(4);
  928. READ32(exid->spa_how);
  929. switch (exid->spa_how) {
  930. case SP4_NONE:
  931. break;
  932. case SP4_MACH_CRED:
  933. /* spo_must_enforce */
  934. READ_BUF(4);
  935. READ32(dummy);
  936. READ_BUF(dummy * 4);
  937. p += dummy;
  938. /* spo_must_allow */
  939. READ_BUF(4);
  940. READ32(dummy);
  941. READ_BUF(dummy * 4);
  942. p += dummy;
  943. break;
  944. case SP4_SSV:
  945. /* ssp_ops */
  946. READ_BUF(4);
  947. READ32(dummy);
  948. READ_BUF(dummy * 4);
  949. p += dummy;
  950. READ_BUF(4);
  951. READ32(dummy);
  952. READ_BUF(dummy * 4);
  953. p += dummy;
  954. /* ssp_hash_algs<> */
  955. READ_BUF(4);
  956. READ32(tmp);
  957. while (tmp--) {
  958. READ_BUF(4);
  959. READ32(dummy);
  960. READ_BUF(dummy);
  961. p += XDR_QUADLEN(dummy);
  962. }
  963. /* ssp_encr_algs<> */
  964. READ_BUF(4);
  965. READ32(tmp);
  966. while (tmp--) {
  967. READ_BUF(4);
  968. READ32(dummy);
  969. READ_BUF(dummy);
  970. p += XDR_QUADLEN(dummy);
  971. }
  972. /* ssp_window and ssp_num_gss_handles */
  973. READ_BUF(8);
  974. READ32(dummy);
  975. READ32(dummy);
  976. break;
  977. default:
  978. goto xdr_error;
  979. }
  980. /* Ignore Implementation ID */
  981. READ_BUF(4); /* nfs_impl_id4 array length */
  982. READ32(dummy);
  983. if (dummy > 1)
  984. goto xdr_error;
  985. if (dummy == 1) {
  986. /* nii_domain */
  987. READ_BUF(4);
  988. READ32(dummy);
  989. READ_BUF(dummy);
  990. p += XDR_QUADLEN(dummy);
  991. /* nii_name */
  992. READ_BUF(4);
  993. READ32(dummy);
  994. READ_BUF(dummy);
  995. p += XDR_QUADLEN(dummy);
  996. /* nii_date */
  997. READ_BUF(12);
  998. p += 3;
  999. }
  1000. DECODE_TAIL;
  1001. }
  1002. static __be32
  1003. nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
  1004. struct nfsd4_create_session *sess)
  1005. {
  1006. DECODE_HEAD;
  1007. u32 dummy;
  1008. char *machine_name;
  1009. int i;
  1010. int nr_secflavs;
  1011. READ_BUF(16);
  1012. COPYMEM(&sess->clientid, 8);
  1013. READ32(sess->seqid);
  1014. READ32(sess->flags);
  1015. /* Fore channel attrs */
  1016. READ_BUF(28);
  1017. READ32(dummy); /* headerpadsz is always 0 */
  1018. READ32(sess->fore_channel.maxreq_sz);
  1019. READ32(sess->fore_channel.maxresp_sz);
  1020. READ32(sess->fore_channel.maxresp_cached);
  1021. READ32(sess->fore_channel.maxops);
  1022. READ32(sess->fore_channel.maxreqs);
  1023. READ32(sess->fore_channel.nr_rdma_attrs);
  1024. if (sess->fore_channel.nr_rdma_attrs == 1) {
  1025. READ_BUF(4);
  1026. READ32(sess->fore_channel.rdma_attrs);
  1027. } else if (sess->fore_channel.nr_rdma_attrs > 1) {
  1028. dprintk("Too many fore channel attr bitmaps!\n");
  1029. goto xdr_error;
  1030. }
  1031. /* Back channel attrs */
  1032. READ_BUF(28);
  1033. READ32(dummy); /* headerpadsz is always 0 */
  1034. READ32(sess->back_channel.maxreq_sz);
  1035. READ32(sess->back_channel.maxresp_sz);
  1036. READ32(sess->back_channel.maxresp_cached);
  1037. READ32(sess->back_channel.maxops);
  1038. READ32(sess->back_channel.maxreqs);
  1039. READ32(sess->back_channel.nr_rdma_attrs);
  1040. if (sess->back_channel.nr_rdma_attrs == 1) {
  1041. READ_BUF(4);
  1042. READ32(sess->back_channel.rdma_attrs);
  1043. } else if (sess->back_channel.nr_rdma_attrs > 1) {
  1044. dprintk("Too many back channel attr bitmaps!\n");
  1045. goto xdr_error;
  1046. }
  1047. READ_BUF(8);
  1048. READ32(sess->callback_prog);
  1049. /* callback_sec_params4 */
  1050. READ32(nr_secflavs);
  1051. for (i = 0; i < nr_secflavs; ++i) {
  1052. READ_BUF(4);
  1053. READ32(dummy);
  1054. switch (dummy) {
  1055. case RPC_AUTH_NULL:
  1056. /* Nothing to read */
  1057. break;
  1058. case RPC_AUTH_UNIX:
  1059. READ_BUF(8);
  1060. /* stamp */
  1061. READ32(dummy);
  1062. /* machine name */
  1063. READ32(dummy);
  1064. READ_BUF(dummy);
  1065. SAVEMEM(machine_name, dummy);
  1066. /* uid, gid */
  1067. READ_BUF(8);
  1068. READ32(sess->uid);
  1069. READ32(sess->gid);
  1070. /* more gids */
  1071. READ_BUF(4);
  1072. READ32(dummy);
  1073. READ_BUF(dummy * 4);
  1074. break;
  1075. case RPC_AUTH_GSS:
  1076. dprintk("RPC_AUTH_GSS callback secflavor "
  1077. "not supported!\n");
  1078. READ_BUF(8);
  1079. /* gcbp_service */
  1080. READ32(dummy);
  1081. /* gcbp_handle_from_server */
  1082. READ32(dummy);
  1083. READ_BUF(dummy);
  1084. p += XDR_QUADLEN(dummy);
  1085. /* gcbp_handle_from_client */
  1086. READ_BUF(4);
  1087. READ32(dummy);
  1088. READ_BUF(dummy);
  1089. break;
  1090. default:
  1091. dprintk("Illegal callback secflavor\n");
  1092. return nfserr_inval;
  1093. }
  1094. }
  1095. DECODE_TAIL;
  1096. }
  1097. static __be32
  1098. nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
  1099. struct nfsd4_destroy_session *destroy_session)
  1100. {
  1101. DECODE_HEAD;
  1102. READ_BUF(NFS4_MAX_SESSIONID_LEN);
  1103. COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1104. DECODE_TAIL;
  1105. }
  1106. static __be32
  1107. nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
  1108. struct nfsd4_free_stateid *free_stateid)
  1109. {
  1110. DECODE_HEAD;
  1111. READ_BUF(sizeof(stateid_t));
  1112. READ32(free_stateid->fr_stateid.si_generation);
  1113. COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
  1114. DECODE_TAIL;
  1115. }
  1116. static __be32
  1117. nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
  1118. struct nfsd4_sequence *seq)
  1119. {
  1120. DECODE_HEAD;
  1121. READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
  1122. COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1123. READ32(seq->seqid);
  1124. READ32(seq->slotid);
  1125. READ32(seq->maxslots);
  1126. READ32(seq->cachethis);
  1127. DECODE_TAIL;
  1128. }
  1129. static __be32
  1130. nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
  1131. {
  1132. unsigned int nbytes;
  1133. stateid_t si;
  1134. int i;
  1135. __be32 *p;
  1136. __be32 status;
  1137. READ_BUF(4);
  1138. test_stateid->ts_num_ids = ntohl(*p++);
  1139. nbytes = test_stateid->ts_num_ids * sizeof(stateid_t);
  1140. if (nbytes > (u32)((char *)argp->end - (char *)argp->p))
  1141. goto xdr_error;
  1142. test_stateid->ts_saved_args = argp;
  1143. save_buf(argp, &test_stateid->ts_savedp);
  1144. for (i = 0; i < test_stateid->ts_num_ids; i++) {
  1145. status = nfsd4_decode_stateid(argp, &si);
  1146. if (status)
  1147. return status;
  1148. }
  1149. status = 0;
  1150. out:
  1151. return status;
  1152. xdr_error:
  1153. dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
  1154. status = nfserr_bad_xdr;
  1155. goto out;
  1156. }
  1157. static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
  1158. {
  1159. DECODE_HEAD;
  1160. READ_BUF(4);
  1161. READ32(rc->rca_one_fs);
  1162. DECODE_TAIL;
  1163. }
  1164. static __be32
  1165. nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
  1166. {
  1167. return nfs_ok;
  1168. }
  1169. static __be32
  1170. nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
  1171. {
  1172. return nfserr_notsupp;
  1173. }
  1174. typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
  1175. static nfsd4_dec nfsd4_dec_ops[] = {
  1176. [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
  1177. [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
  1178. [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
  1179. [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
  1180. [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1181. [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
  1182. [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
  1183. [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
  1184. [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
  1185. [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
  1186. [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
  1187. [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
  1188. [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
  1189. [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
  1190. [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1191. [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
  1192. [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1193. [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
  1194. [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
  1195. [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
  1196. [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
  1197. [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
  1198. [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
  1199. [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
  1200. [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
  1201. [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
  1202. [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
  1203. [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
  1204. [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
  1205. [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
  1206. [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
  1207. [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
  1208. [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
  1209. [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
  1210. [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1211. [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
  1212. [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
  1213. };
  1214. static nfsd4_dec nfsd41_dec_ops[] = {
  1215. [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
  1216. [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
  1217. [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
  1218. [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
  1219. [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1220. [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
  1221. [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
  1222. [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
  1223. [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
  1224. [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
  1225. [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
  1226. [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
  1227. [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
  1228. [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
  1229. [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1230. [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
  1231. [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1232. [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_notsupp,
  1233. [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
  1234. [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
  1235. [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_notsupp,
  1236. [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
  1237. [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
  1238. [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
  1239. [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
  1240. [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
  1241. [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
  1242. [OP_RENEW] = (nfsd4_dec)nfsd4_decode_notsupp,
  1243. [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
  1244. [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
  1245. [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
  1246. [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
  1247. [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
  1248. [OP_SETCLIENTID_CONFIRM]= (nfsd4_dec)nfsd4_decode_notsupp,
  1249. [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1250. [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
  1251. [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_notsupp,
  1252. /* new operations for NFSv4.1 */
  1253. [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_notsupp,
  1254. [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
  1255. [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
  1256. [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
  1257. [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
  1258. [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
  1259. [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1260. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
  1261. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  1262. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
  1263. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
  1264. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
  1265. [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
  1266. [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
  1267. [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
  1268. [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
  1269. [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1270. [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
  1271. [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
  1272. };
  1273. struct nfsd4_minorversion_ops {
  1274. nfsd4_dec *decoders;
  1275. int nops;
  1276. };
  1277. static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
  1278. [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
  1279. [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
  1280. };
  1281. static __be32
  1282. nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
  1283. {
  1284. DECODE_HEAD;
  1285. struct nfsd4_op *op;
  1286. struct nfsd4_minorversion_ops *ops;
  1287. bool cachethis = false;
  1288. int i;
  1289. /*
  1290. * XXX: According to spec, we should check the tag
  1291. * for UTF-8 compliance. I'm postponing this for
  1292. * now because it seems that some clients do use
  1293. * binary tags.
  1294. */
  1295. READ_BUF(4);
  1296. READ32(argp->taglen);
  1297. READ_BUF(argp->taglen + 8);
  1298. SAVEMEM(argp->tag, argp->taglen);
  1299. READ32(argp->minorversion);
  1300. READ32(argp->opcnt);
  1301. if (argp->taglen > NFSD4_MAX_TAGLEN)
  1302. goto xdr_error;
  1303. if (argp->opcnt > 100)
  1304. goto xdr_error;
  1305. if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
  1306. argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
  1307. if (!argp->ops) {
  1308. argp->ops = argp->iops;
  1309. dprintk("nfsd: couldn't allocate room for COMPOUND\n");
  1310. goto xdr_error;
  1311. }
  1312. }
  1313. if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
  1314. argp->opcnt = 0;
  1315. ops = &nfsd4_minorversion[argp->minorversion];
  1316. for (i = 0; i < argp->opcnt; i++) {
  1317. op = &argp->ops[i];
  1318. op->replay = NULL;
  1319. /*
  1320. * We can't use READ_BUF() here because we need to handle
  1321. * a missing opcode as an OP_WRITE + 1. So we need to check
  1322. * to see if we're truly at the end of our buffer or if there
  1323. * is another page we need to flip to.
  1324. */
  1325. if (argp->p == argp->end) {
  1326. if (argp->pagelen < 4) {
  1327. /* There isn't an opcode still on the wire */
  1328. op->opnum = OP_WRITE + 1;
  1329. op->status = nfserr_bad_xdr;
  1330. argp->opcnt = i+1;
  1331. break;
  1332. }
  1333. /*
  1334. * False alarm. We just hit a page boundary, but there
  1335. * is still data available. Move pointer across page
  1336. * boundary. *snip from READ_BUF*
  1337. */
  1338. argp->p = page_address(argp->pagelist[0]);
  1339. argp->pagelist++;
  1340. if (argp->pagelen < PAGE_SIZE) {
  1341. argp->end = argp->p + (argp->pagelen>>2);
  1342. argp->pagelen = 0;
  1343. } else {
  1344. argp->end = argp->p + (PAGE_SIZE>>2);
  1345. argp->pagelen -= PAGE_SIZE;
  1346. }
  1347. }
  1348. op->opnum = ntohl(*argp->p++);
  1349. if (op->opnum >= FIRST_NFS4_OP && op->opnum <= LAST_NFS4_OP)
  1350. op->status = ops->decoders[op->opnum](argp, &op->u);
  1351. else {
  1352. op->opnum = OP_ILLEGAL;
  1353. op->status = nfserr_op_illegal;
  1354. }
  1355. if (op->status) {
  1356. argp->opcnt = i+1;
  1357. break;
  1358. }
  1359. /*
  1360. * We'll try to cache the result in the DRC if any one
  1361. * op in the compound wants to be cached:
  1362. */
  1363. cachethis |= nfsd4_cache_this_op(op);
  1364. }
  1365. /* Sessions make the DRC unnecessary: */
  1366. if (argp->minorversion)
  1367. cachethis = false;
  1368. argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
  1369. DECODE_TAIL;
  1370. }
  1371. #define WRITE32(n) *p++ = htonl(n)
  1372. #define WRITE64(n) do { \
  1373. *p++ = htonl((u32)((n) >> 32)); \
  1374. *p++ = htonl((u32)(n)); \
  1375. } while (0)
  1376. #define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
  1377. *(p + XDR_QUADLEN(nbytes) -1) = 0; \
  1378. memcpy(p, ptr, nbytes); \
  1379. p += XDR_QUADLEN(nbytes); \
  1380. }} while (0)
  1381. static void write32(__be32 **p, u32 n)
  1382. {
  1383. *(*p)++ = n;
  1384. }
  1385. static void write64(__be32 **p, u64 n)
  1386. {
  1387. write32(p, (u32)(n >> 32));
  1388. write32(p, (u32)n);
  1389. }
  1390. static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
  1391. {
  1392. if (IS_I_VERSION(inode)) {
  1393. write64(p, inode->i_version);
  1394. } else {
  1395. write32(p, stat->ctime.tv_sec);
  1396. write32(p, stat->ctime.tv_nsec);
  1397. }
  1398. }
  1399. static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
  1400. {
  1401. write32(p, c->atomic);
  1402. if (c->change_supported) {
  1403. write64(p, c->before_change);
  1404. write64(p, c->after_change);
  1405. } else {
  1406. write32(p, c->before_ctime_sec);
  1407. write32(p, c->before_ctime_nsec);
  1408. write32(p, c->after_ctime_sec);
  1409. write32(p, c->after_ctime_nsec);
  1410. }
  1411. }
  1412. #define RESERVE_SPACE(nbytes) do { \
  1413. p = resp->p; \
  1414. BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
  1415. } while (0)
  1416. #define ADJUST_ARGS() resp->p = p
  1417. /*
  1418. * Header routine to setup seqid operation replay cache
  1419. */
  1420. #define ENCODE_SEQID_OP_HEAD \
  1421. __be32 *save; \
  1422. \
  1423. save = resp->p;
  1424. /*
  1425. * Routine for encoding the result of a "seqid-mutating" NFSv4 operation. This
  1426. * is where sequence id's are incremented, and the replay cache is filled.
  1427. * Note that we increment sequence id's here, at the last moment, so we're sure
  1428. * we know whether the error to be returned is a sequence id mutating error.
  1429. */
  1430. static void encode_seqid_op_tail(struct nfsd4_compoundres *resp, __be32 *save, __be32 nfserr)
  1431. {
  1432. struct nfs4_stateowner *stateowner = resp->cstate.replay_owner;
  1433. if (seqid_mutating_err(ntohl(nfserr)) && stateowner) {
  1434. stateowner->so_seqid++;
  1435. stateowner->so_replay.rp_status = nfserr;
  1436. stateowner->so_replay.rp_buflen =
  1437. (char *)resp->p - (char *)save;
  1438. memcpy(stateowner->so_replay.rp_buf, save,
  1439. stateowner->so_replay.rp_buflen);
  1440. }
  1441. }
  1442. /* Encode as an array of strings the string given with components
  1443. * separated @sep.
  1444. */
  1445. static __be32 nfsd4_encode_components(char sep, char *components,
  1446. __be32 **pp, int *buflen)
  1447. {
  1448. __be32 *p = *pp;
  1449. __be32 *countp = p;
  1450. int strlen, count=0;
  1451. char *str, *end;
  1452. dprintk("nfsd4_encode_components(%s)\n", components);
  1453. if ((*buflen -= 4) < 0)
  1454. return nfserr_resource;
  1455. WRITE32(0); /* We will fill this in with @count later */
  1456. end = str = components;
  1457. while (*end) {
  1458. for (; *end && (*end != sep); end++)
  1459. ; /* Point to end of component */
  1460. strlen = end - str;
  1461. if (strlen) {
  1462. if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
  1463. return nfserr_resource;
  1464. WRITE32(strlen);
  1465. WRITEMEM(str, strlen);
  1466. count++;
  1467. }
  1468. else
  1469. end++;
  1470. str = end;
  1471. }
  1472. *pp = p;
  1473. p = countp;
  1474. WRITE32(count);
  1475. return 0;
  1476. }
  1477. /*
  1478. * encode a location element of a fs_locations structure
  1479. */
  1480. static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
  1481. __be32 **pp, int *buflen)
  1482. {
  1483. __be32 status;
  1484. __be32 *p = *pp;
  1485. status = nfsd4_encode_components(':', location->hosts, &p, buflen);
  1486. if (status)
  1487. return status;
  1488. status = nfsd4_encode_components('/', location->path, &p, buflen);
  1489. if (status)
  1490. return status;
  1491. *pp = p;
  1492. return 0;
  1493. }
  1494. /*
  1495. * Return the path to an export point in the pseudo filesystem namespace
  1496. * Returned string is safe to use as long as the caller holds a reference
  1497. * to @exp.
  1498. */
  1499. static char *nfsd4_path(struct svc_rqst *rqstp, struct svc_export *exp, __be32 *stat)
  1500. {
  1501. struct svc_fh tmp_fh;
  1502. char *path = NULL, *rootpath;
  1503. size_t rootlen;
  1504. fh_init(&tmp_fh, NFS4_FHSIZE);
  1505. *stat = exp_pseudoroot(rqstp, &tmp_fh);
  1506. if (*stat)
  1507. return NULL;
  1508. rootpath = tmp_fh.fh_export->ex_pathname;
  1509. path = exp->ex_pathname;
  1510. rootlen = strlen(rootpath);
  1511. if (strncmp(path, rootpath, rootlen)) {
  1512. dprintk("nfsd: fs_locations failed;"
  1513. "%s is not contained in %s\n", path, rootpath);
  1514. *stat = nfserr_notsupp;
  1515. path = NULL;
  1516. goto out;
  1517. }
  1518. path += rootlen;
  1519. out:
  1520. fh_put(&tmp_fh);
  1521. return path;
  1522. }
  1523. /*
  1524. * encode a fs_locations structure
  1525. */
  1526. static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
  1527. struct svc_export *exp,
  1528. __be32 **pp, int *buflen)
  1529. {
  1530. __be32 status;
  1531. int i;
  1532. __be32 *p = *pp;
  1533. struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
  1534. char *root = nfsd4_path(rqstp, exp, &status);
  1535. if (status)
  1536. return status;
  1537. status = nfsd4_encode_components('/', root, &p, buflen);
  1538. if (status)
  1539. return status;
  1540. if ((*buflen -= 4) < 0)
  1541. return nfserr_resource;
  1542. WRITE32(fslocs->locations_count);
  1543. for (i=0; i<fslocs->locations_count; i++) {
  1544. status = nfsd4_encode_fs_location4(&fslocs->locations[i],
  1545. &p, buflen);
  1546. if (status)
  1547. return status;
  1548. }
  1549. *pp = p;
  1550. return 0;
  1551. }
  1552. static u32 nfs4_file_type(umode_t mode)
  1553. {
  1554. switch (mode & S_IFMT) {
  1555. case S_IFIFO: return NF4FIFO;
  1556. case S_IFCHR: return NF4CHR;
  1557. case S_IFDIR: return NF4DIR;
  1558. case S_IFBLK: return NF4BLK;
  1559. case S_IFLNK: return NF4LNK;
  1560. case S_IFREG: return NF4REG;
  1561. case S_IFSOCK: return NF4SOCK;
  1562. default: return NF4BAD;
  1563. };
  1564. }
  1565. static __be32
  1566. nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
  1567. __be32 **p, int *buflen)
  1568. {
  1569. int status;
  1570. if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
  1571. return nfserr_resource;
  1572. if (whotype != NFS4_ACL_WHO_NAMED)
  1573. status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
  1574. else if (group)
  1575. status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
  1576. else
  1577. status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
  1578. if (status < 0)
  1579. return nfserrno(status);
  1580. *p = xdr_encode_opaque(*p, NULL, status);
  1581. *buflen -= (XDR_QUADLEN(status) << 2) + 4;
  1582. BUG_ON(*buflen < 0);
  1583. return 0;
  1584. }
  1585. static inline __be32
  1586. nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, __be32 **p, int *buflen)
  1587. {
  1588. return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
  1589. }
  1590. static inline __be32
  1591. nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, __be32 **p, int *buflen)
  1592. {
  1593. return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
  1594. }
  1595. static inline __be32
  1596. nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
  1597. __be32 **p, int *buflen)
  1598. {
  1599. return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
  1600. }
  1601. #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
  1602. FATTR4_WORD0_RDATTR_ERROR)
  1603. #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
  1604. static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
  1605. {
  1606. /* As per referral draft: */
  1607. if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
  1608. *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
  1609. if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
  1610. *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
  1611. *rdattr_err = NFSERR_MOVED;
  1612. else
  1613. return nfserr_moved;
  1614. }
  1615. *bmval0 &= WORD0_ABSENT_FS_ATTRS;
  1616. *bmval1 &= WORD1_ABSENT_FS_ATTRS;
  1617. return 0;
  1618. }
  1619. /*
  1620. * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
  1621. * ourselves.
  1622. *
  1623. * @countp is the buffer size in _words_; upon successful return this becomes
  1624. * replaced with the number of words written.
  1625. */
  1626. __be32
  1627. nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
  1628. struct dentry *dentry, __be32 *buffer, int *countp, u32 *bmval,
  1629. struct svc_rqst *rqstp, int ignore_crossmnt)
  1630. {
  1631. u32 bmval0 = bmval[0];
  1632. u32 bmval1 = bmval[1];
  1633. u32 bmval2 = bmval[2];
  1634. struct kstat stat;
  1635. struct svc_fh tempfh;
  1636. struct kstatfs statfs;
  1637. int buflen = *countp << 2;
  1638. __be32 *attrlenp;
  1639. u32 dummy;
  1640. u64 dummy64;
  1641. u32 rdattr_err = 0;
  1642. __be32 *p = buffer;
  1643. __be32 status;
  1644. int err;
  1645. int aclsupport = 0;
  1646. struct nfs4_acl *acl = NULL;
  1647. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1648. u32 minorversion = resp->cstate.minorversion;
  1649. struct path path = {
  1650. .mnt = exp->ex_path.mnt,
  1651. .dentry = dentry,
  1652. };
  1653. BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
  1654. BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
  1655. BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
  1656. BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
  1657. if (exp->ex_fslocs.migrated) {
  1658. BUG_ON(bmval[2]);
  1659. status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
  1660. if (status)
  1661. goto out;
  1662. }
  1663. err = vfs_getattr(exp->ex_path.mnt, dentry, &stat);
  1664. if (err)
  1665. goto out_nfserr;
  1666. if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
  1667. FATTR4_WORD0_MAXNAME)) ||
  1668. (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
  1669. FATTR4_WORD1_SPACE_TOTAL))) {
  1670. err = vfs_statfs(&path, &statfs);
  1671. if (err)
  1672. goto out_nfserr;
  1673. }
  1674. if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
  1675. fh_init(&tempfh, NFS4_FHSIZE);
  1676. status = fh_compose(&tempfh, exp, dentry, NULL);
  1677. if (status)
  1678. goto out;
  1679. fhp = &tempfh;
  1680. }
  1681. if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
  1682. | FATTR4_WORD0_SUPPORTED_ATTRS)) {
  1683. err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
  1684. aclsupport = (err == 0);
  1685. if (bmval0 & FATTR4_WORD0_ACL) {
  1686. if (err == -EOPNOTSUPP)
  1687. bmval0 &= ~FATTR4_WORD0_ACL;
  1688. else if (err == -EINVAL) {
  1689. status = nfserr_attrnotsupp;
  1690. goto out;
  1691. } else if (err != 0)
  1692. goto out_nfserr;
  1693. }
  1694. }
  1695. if (bmval2) {
  1696. if ((buflen -= 16) < 0)
  1697. goto out_resource;
  1698. WRITE32(3);
  1699. WRITE32(bmval0);
  1700. WRITE32(bmval1);
  1701. WRITE32(bmval2);
  1702. } else if (bmval1) {
  1703. if ((buflen -= 12) < 0)
  1704. goto out_resource;
  1705. WRITE32(2);
  1706. WRITE32(bmval0);
  1707. WRITE32(bmval1);
  1708. } else {
  1709. if ((buflen -= 8) < 0)
  1710. goto out_resource;
  1711. WRITE32(1);
  1712. WRITE32(bmval0);
  1713. }
  1714. attrlenp = p++; /* to be backfilled later */
  1715. if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  1716. u32 word0 = nfsd_suppattrs0(minorversion);
  1717. u32 word1 = nfsd_suppattrs1(minorversion);
  1718. u32 word2 = nfsd_suppattrs2(minorversion);
  1719. if (!aclsupport)
  1720. word0 &= ~FATTR4_WORD0_ACL;
  1721. if (!word2) {
  1722. if ((buflen -= 12) < 0)
  1723. goto out_resource;
  1724. WRITE32(2);
  1725. WRITE32(word0);
  1726. WRITE32(word1);
  1727. } else {
  1728. if ((buflen -= 16) < 0)
  1729. goto out_resource;
  1730. WRITE32(3);
  1731. WRITE32(word0);
  1732. WRITE32(word1);
  1733. WRITE32(word2);
  1734. }
  1735. }
  1736. if (bmval0 & FATTR4_WORD0_TYPE) {
  1737. if ((buflen -= 4) < 0)
  1738. goto out_resource;
  1739. dummy = nfs4_file_type(stat.mode);
  1740. if (dummy == NF4BAD)
  1741. goto out_serverfault;
  1742. WRITE32(dummy);
  1743. }
  1744. if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
  1745. if ((buflen -= 4) < 0)
  1746. goto out_resource;
  1747. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  1748. WRITE32(NFS4_FH_PERSISTENT);
  1749. else
  1750. WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
  1751. }
  1752. if (bmval0 & FATTR4_WORD0_CHANGE) {
  1753. if ((buflen -= 8) < 0)
  1754. goto out_resource;
  1755. write_change(&p, &stat, dentry->d_inode);
  1756. }
  1757. if (bmval0 & FATTR4_WORD0_SIZE) {
  1758. if ((buflen -= 8) < 0)
  1759. goto out_resource;
  1760. WRITE64(stat.size);
  1761. }
  1762. if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
  1763. if ((buflen -= 4) < 0)
  1764. goto out_resource;
  1765. WRITE32(1);
  1766. }
  1767. if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
  1768. if ((buflen -= 4) < 0)
  1769. goto out_resource;
  1770. WRITE32(1);
  1771. }
  1772. if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
  1773. if ((buflen -= 4) < 0)
  1774. goto out_resource;
  1775. WRITE32(0);
  1776. }
  1777. if (bmval0 & FATTR4_WORD0_FSID) {
  1778. if ((buflen -= 16) < 0)
  1779. goto out_resource;
  1780. if (exp->ex_fslocs.migrated) {
  1781. WRITE64(NFS4_REFERRAL_FSID_MAJOR);
  1782. WRITE64(NFS4_REFERRAL_FSID_MINOR);
  1783. } else switch(fsid_source(fhp)) {
  1784. case FSIDSOURCE_FSID:
  1785. WRITE64((u64)exp->ex_fsid);
  1786. WRITE64((u64)0);
  1787. break;
  1788. case FSIDSOURCE_DEV:
  1789. WRITE32(0);
  1790. WRITE32(MAJOR(stat.dev));
  1791. WRITE32(0);
  1792. WRITE32(MINOR(stat.dev));
  1793. break;
  1794. case FSIDSOURCE_UUID:
  1795. WRITEMEM(exp->ex_uuid, 16);
  1796. break;
  1797. }
  1798. }
  1799. if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
  1800. if ((buflen -= 4) < 0)
  1801. goto out_resource;
  1802. WRITE32(0);
  1803. }
  1804. if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
  1805. if ((buflen -= 4) < 0)
  1806. goto out_resource;
  1807. WRITE32(nfsd4_lease);
  1808. }
  1809. if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
  1810. if ((buflen -= 4) < 0)
  1811. goto out_resource;
  1812. WRITE32(rdattr_err);
  1813. }
  1814. if (bmval0 & FATTR4_WORD0_ACL) {
  1815. struct nfs4_ace *ace;
  1816. if (acl == NULL) {
  1817. if ((buflen -= 4) < 0)
  1818. goto out_resource;
  1819. WRITE32(0);
  1820. goto out_acl;
  1821. }
  1822. if ((buflen -= 4) < 0)
  1823. goto out_resource;
  1824. WRITE32(acl->naces);
  1825. for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
  1826. if ((buflen -= 4*3) < 0)
  1827. goto out_resource;
  1828. WRITE32(ace->type);
  1829. WRITE32(ace->flag);
  1830. WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
  1831. status = nfsd4_encode_aclname(rqstp, ace->whotype,
  1832. ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
  1833. &p, &buflen);
  1834. if (status == nfserr_resource)
  1835. goto out_resource;
  1836. if (status)
  1837. goto out;
  1838. }
  1839. }
  1840. out_acl:
  1841. if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
  1842. if ((buflen -= 4) < 0)
  1843. goto out_resource;
  1844. WRITE32(aclsupport ?
  1845. ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
  1846. }
  1847. if (bmval0 & FATTR4_WORD0_CANSETTIME) {
  1848. if ((buflen -= 4) < 0)
  1849. goto out_resource;
  1850. WRITE32(1);
  1851. }
  1852. if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
  1853. if ((buflen -= 4) < 0)
  1854. goto out_resource;
  1855. WRITE32(1);
  1856. }
  1857. if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
  1858. if ((buflen -= 4) < 0)
  1859. goto out_resource;
  1860. WRITE32(1);
  1861. }
  1862. if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
  1863. if ((buflen -= 4) < 0)
  1864. goto out_resource;
  1865. WRITE32(1);
  1866. }
  1867. if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
  1868. buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
  1869. if (buflen < 0)
  1870. goto out_resource;
  1871. WRITE32(fhp->fh_handle.fh_size);
  1872. WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
  1873. }
  1874. if (bmval0 & FATTR4_WORD0_FILEID) {
  1875. if ((buflen -= 8) < 0)
  1876. goto out_resource;
  1877. WRITE64(stat.ino);
  1878. }
  1879. if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
  1880. if ((buflen -= 8) < 0)
  1881. goto out_resource;
  1882. WRITE64((u64) statfs.f_ffree);
  1883. }
  1884. if (bmval0 & FATTR4_WORD0_FILES_FREE) {
  1885. if ((buflen -= 8) < 0)
  1886. goto out_resource;
  1887. WRITE64((u64) statfs.f_ffree);
  1888. }
  1889. if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
  1890. if ((buflen -= 8) < 0)
  1891. goto out_resource;
  1892. WRITE64((u64) statfs.f_files);
  1893. }
  1894. if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
  1895. status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
  1896. if (status == nfserr_resource)
  1897. goto out_resource;
  1898. if (status)
  1899. goto out;
  1900. }
  1901. if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
  1902. if ((buflen -= 4) < 0)
  1903. goto out_resource;
  1904. WRITE32(1);
  1905. }
  1906. if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
  1907. if ((buflen -= 8) < 0)
  1908. goto out_resource;
  1909. WRITE64(~(u64)0);
  1910. }
  1911. if (bmval0 & FATTR4_WORD0_MAXLINK) {
  1912. if ((buflen -= 4) < 0)
  1913. goto out_resource;
  1914. WRITE32(255);
  1915. }
  1916. if (bmval0 & FATTR4_WORD0_MAXNAME) {
  1917. if ((buflen -= 4) < 0)
  1918. goto out_resource;
  1919. WRITE32(statfs.f_namelen);
  1920. }
  1921. if (bmval0 & FATTR4_WORD0_MAXREAD) {
  1922. if ((buflen -= 8) < 0)
  1923. goto out_resource;
  1924. WRITE64((u64) svc_max_payload(rqstp));
  1925. }
  1926. if (bmval0 & FATTR4_WORD0_MAXWRITE) {
  1927. if ((buflen -= 8) < 0)
  1928. goto out_resource;
  1929. WRITE64((u64) svc_max_payload(rqstp));
  1930. }
  1931. if (bmval1 & FATTR4_WORD1_MODE) {
  1932. if ((buflen -= 4) < 0)
  1933. goto out_resource;
  1934. WRITE32(stat.mode & S_IALLUGO);
  1935. }
  1936. if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
  1937. if ((buflen -= 4) < 0)
  1938. goto out_resource;
  1939. WRITE32(1);
  1940. }
  1941. if (bmval1 & FATTR4_WORD1_NUMLINKS) {
  1942. if ((buflen -= 4) < 0)
  1943. goto out_resource;
  1944. WRITE32(stat.nlink);
  1945. }
  1946. if (bmval1 & FATTR4_WORD1_OWNER) {
  1947. status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
  1948. if (status == nfserr_resource)
  1949. goto out_resource;
  1950. if (status)
  1951. goto out;
  1952. }
  1953. if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
  1954. status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
  1955. if (status == nfserr_resource)
  1956. goto out_resource;
  1957. if (status)
  1958. goto out;
  1959. }
  1960. if (bmval1 & FATTR4_WORD1_RAWDEV) {
  1961. if ((buflen -= 8) < 0)
  1962. goto out_resource;
  1963. WRITE32((u32) MAJOR(stat.rdev));
  1964. WRITE32((u32) MINOR(stat.rdev));
  1965. }
  1966. if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
  1967. if ((buflen -= 8) < 0)
  1968. goto out_resource;
  1969. dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
  1970. WRITE64(dummy64);
  1971. }
  1972. if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
  1973. if ((buflen -= 8) < 0)
  1974. goto out_resource;
  1975. dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
  1976. WRITE64(dummy64);
  1977. }
  1978. if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
  1979. if ((buflen -= 8) < 0)
  1980. goto out_resource;
  1981. dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
  1982. WRITE64(dummy64);
  1983. }
  1984. if (bmval1 & FATTR4_WORD1_SPACE_USED) {
  1985. if ((buflen -= 8) < 0)
  1986. goto out_resource;
  1987. dummy64 = (u64)stat.blocks << 9;
  1988. WRITE64(dummy64);
  1989. }
  1990. if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
  1991. if ((buflen -= 12) < 0)
  1992. goto out_resource;
  1993. WRITE32(0);
  1994. WRITE32(stat.atime.tv_sec);
  1995. WRITE32(stat.atime.tv_nsec);
  1996. }
  1997. if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
  1998. if ((buflen -= 12) < 0)
  1999. goto out_resource;
  2000. WRITE32(0);
  2001. WRITE32(1);
  2002. WRITE32(0);
  2003. }
  2004. if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
  2005. if ((buflen -= 12) < 0)
  2006. goto out_resource;
  2007. WRITE32(0);
  2008. WRITE32(stat.ctime.tv_sec);
  2009. WRITE32(stat.ctime.tv_nsec);
  2010. }
  2011. if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
  2012. if ((buflen -= 12) < 0)
  2013. goto out_resource;
  2014. WRITE32(0);
  2015. WRITE32(stat.mtime.tv_sec);
  2016. WRITE32(stat.mtime.tv_nsec);
  2017. }
  2018. if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
  2019. if ((buflen -= 8) < 0)
  2020. goto out_resource;
  2021. /*
  2022. * Get parent's attributes if not ignoring crossmount
  2023. * and this is the root of a cross-mounted filesystem.
  2024. */
  2025. if (ignore_crossmnt == 0 &&
  2026. dentry == exp->ex_path.mnt->mnt_root) {
  2027. struct path path = exp->ex_path;
  2028. path_get(&path);
  2029. while (follow_up(&path)) {
  2030. if (path.dentry != path.mnt->mnt_root)
  2031. break;
  2032. }
  2033. err = vfs_getattr(path.mnt, path.dentry, &stat);
  2034. path_put(&path);
  2035. if (err)
  2036. goto out_nfserr;
  2037. }
  2038. WRITE64(stat.ino);
  2039. }
  2040. if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
  2041. WRITE32(3);
  2042. WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
  2043. WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
  2044. WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
  2045. }
  2046. *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
  2047. *countp = p - buffer;
  2048. status = nfs_ok;
  2049. out:
  2050. kfree(acl);
  2051. if (fhp == &tempfh)
  2052. fh_put(&tempfh);
  2053. return status;
  2054. out_nfserr:
  2055. status = nfserrno(err);
  2056. goto out;
  2057. out_resource:
  2058. *countp = 0;
  2059. status = nfserr_resource;
  2060. goto out;
  2061. out_serverfault:
  2062. status = nfserr_serverfault;
  2063. goto out;
  2064. }
  2065. static inline int attributes_need_mount(u32 *bmval)
  2066. {
  2067. if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
  2068. return 1;
  2069. if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
  2070. return 1;
  2071. return 0;
  2072. }
  2073. static __be32
  2074. nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
  2075. const char *name, int namlen, __be32 *p, int *buflen)
  2076. {
  2077. struct svc_export *exp = cd->rd_fhp->fh_export;
  2078. struct dentry *dentry;
  2079. __be32 nfserr;
  2080. int ignore_crossmnt = 0;
  2081. dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
  2082. if (IS_ERR(dentry))
  2083. return nfserrno(PTR_ERR(dentry));
  2084. if (!dentry->d_inode) {
  2085. /*
  2086. * nfsd_buffered_readdir drops the i_mutex between
  2087. * readdir and calling this callback, leaving a window
  2088. * where this directory entry could have gone away.
  2089. */
  2090. dput(dentry);
  2091. return nfserr_noent;
  2092. }
  2093. exp_get(exp);
  2094. /*
  2095. * In the case of a mountpoint, the client may be asking for
  2096. * attributes that are only properties of the underlying filesystem
  2097. * as opposed to the cross-mounted file system. In such a case,
  2098. * we will not follow the cross mount and will fill the attribtutes
  2099. * directly from the mountpoint dentry.
  2100. */
  2101. if (nfsd_mountpoint(dentry, exp)) {
  2102. int err;
  2103. if (!(exp->ex_flags & NFSEXP_V4ROOT)
  2104. && !attributes_need_mount(cd->rd_bmval)) {
  2105. ignore_crossmnt = 1;
  2106. goto out_encode;
  2107. }
  2108. /*
  2109. * Why the heck aren't we just using nfsd_lookup??
  2110. * Different "."/".." handling? Something else?
  2111. * At least, add a comment here to explain....
  2112. */
  2113. err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
  2114. if (err) {
  2115. nfserr = nfserrno(err);
  2116. goto out_put;
  2117. }
  2118. nfserr = check_nfsd_access(exp, cd->rd_rqstp);
  2119. if (nfserr)
  2120. goto out_put;
  2121. }
  2122. out_encode:
  2123. nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
  2124. cd->rd_rqstp, ignore_crossmnt);
  2125. out_put:
  2126. dput(dentry);
  2127. exp_put(exp);
  2128. return nfserr;
  2129. }
  2130. static __be32 *
  2131. nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
  2132. {
  2133. __be32 *attrlenp;
  2134. if (buflen < 6)
  2135. return NULL;
  2136. *p++ = htonl(2);
  2137. *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
  2138. *p++ = htonl(0); /* bmval1 */
  2139. attrlenp = p++;
  2140. *p++ = nfserr; /* no htonl */
  2141. *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
  2142. return p;
  2143. }
  2144. static int
  2145. nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
  2146. loff_t offset, u64 ino, unsigned int d_type)
  2147. {
  2148. struct readdir_cd *ccd = ccdv;
  2149. struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
  2150. int buflen;
  2151. __be32 *p = cd->buffer;
  2152. __be32 *cookiep;
  2153. __be32 nfserr = nfserr_toosmall;
  2154. /* In nfsv4, "." and ".." never make it onto the wire.. */
  2155. if (name && isdotent(name, namlen)) {
  2156. cd->common.err = nfs_ok;
  2157. return 0;
  2158. }
  2159. if (cd->offset)
  2160. xdr_encode_hyper(cd->offset, (u64) offset);
  2161. buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
  2162. if (buflen < 0)
  2163. goto fail;
  2164. *p++ = xdr_one; /* mark entry present */
  2165. cookiep = p;
  2166. p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
  2167. p = xdr_encode_array(p, name, namlen); /* name length & name */
  2168. nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
  2169. switch (nfserr) {
  2170. case nfs_ok:
  2171. p += buflen;
  2172. break;
  2173. case nfserr_resource:
  2174. nfserr = nfserr_toosmall;
  2175. goto fail;
  2176. case nfserr_noent:
  2177. goto skip_entry;
  2178. default:
  2179. /*
  2180. * If the client requested the RDATTR_ERROR attribute,
  2181. * we stuff the error code into this attribute
  2182. * and continue. If this attribute was not requested,
  2183. * then in accordance with the spec, we fail the
  2184. * entire READDIR operation(!)
  2185. */
  2186. if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
  2187. goto fail;
  2188. p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
  2189. if (p == NULL) {
  2190. nfserr = nfserr_toosmall;
  2191. goto fail;
  2192. }
  2193. }
  2194. cd->buflen -= (p - cd->buffer);
  2195. cd->buffer = p;
  2196. cd->offset = cookiep;
  2197. skip_entry:
  2198. cd->common.err = nfs_ok;
  2199. return 0;
  2200. fail:
  2201. cd->common.err = nfserr;
  2202. return -EINVAL;
  2203. }
  2204. static void
  2205. nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
  2206. {
  2207. __be32 *p;
  2208. RESERVE_SPACE(sizeof(stateid_t));
  2209. WRITE32(sid->si_generation);
  2210. WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
  2211. ADJUST_ARGS();
  2212. }
  2213. static __be32
  2214. nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
  2215. {
  2216. __be32 *p;
  2217. if (!nfserr) {
  2218. RESERVE_SPACE(8);
  2219. WRITE32(access->ac_supported);
  2220. WRITE32(access->ac_resp_access);
  2221. ADJUST_ARGS();
  2222. }
  2223. return nfserr;
  2224. }
  2225. static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
  2226. {
  2227. __be32 *p;
  2228. if (!nfserr) {
  2229. RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
  2230. WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  2231. WRITE32(bcts->dir);
  2232. /* XXX: ? */
  2233. WRITE32(0);
  2234. ADJUST_ARGS();
  2235. }
  2236. return nfserr;
  2237. }
  2238. static __be32
  2239. nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
  2240. {
  2241. ENCODE_SEQID_OP_HEAD;
  2242. if (!nfserr)
  2243. nfsd4_encode_stateid(resp, &close->cl_stateid);
  2244. encode_seqid_op_tail(resp, save, nfserr);
  2245. return nfserr;
  2246. }
  2247. static __be32
  2248. nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
  2249. {
  2250. __be32 *p;
  2251. if (!nfserr) {
  2252. RESERVE_SPACE(8);
  2253. WRITEMEM(commit->co_verf.data, 8);
  2254. ADJUST_ARGS();
  2255. }
  2256. return nfserr;
  2257. }
  2258. static __be32
  2259. nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
  2260. {
  2261. __be32 *p;
  2262. if (!nfserr) {
  2263. RESERVE_SPACE(32);
  2264. write_cinfo(&p, &create->cr_cinfo);
  2265. WRITE32(2);
  2266. WRITE32(create->cr_bmval[0]);
  2267. WRITE32(create->cr_bmval[1]);
  2268. ADJUST_ARGS();
  2269. }
  2270. return nfserr;
  2271. }
  2272. static __be32
  2273. nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
  2274. {
  2275. struct svc_fh *fhp = getattr->ga_fhp;
  2276. int buflen;
  2277. if (nfserr)
  2278. return nfserr;
  2279. buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
  2280. nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
  2281. resp->p, &buflen, getattr->ga_bmval,
  2282. resp->rqstp, 0);
  2283. if (!nfserr)
  2284. resp->p += buflen;
  2285. return nfserr;
  2286. }
  2287. static __be32
  2288. nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
  2289. {
  2290. struct svc_fh *fhp = *fhpp;
  2291. unsigned int len;
  2292. __be32 *p;
  2293. if (!nfserr) {
  2294. len = fhp->fh_handle.fh_size;
  2295. RESERVE_SPACE(len + 4);
  2296. WRITE32(len);
  2297. WRITEMEM(&fhp->fh_handle.fh_base, len);
  2298. ADJUST_ARGS();
  2299. }
  2300. return nfserr;
  2301. }
  2302. /*
  2303. * Including all fields other than the name, a LOCK4denied structure requires
  2304. * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
  2305. */
  2306. static void
  2307. nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
  2308. {
  2309. __be32 *p;
  2310. RESERVE_SPACE(32 + XDR_LEN(ld->ld_sop ? ld->ld_sop->so_owner.len : 0));
  2311. WRITE64(ld->ld_start);
  2312. WRITE64(ld->ld_length);
  2313. WRITE32(ld->ld_type);
  2314. if (ld->ld_sop) {
  2315. WRITEMEM(&ld->ld_clientid, 8);
  2316. WRITE32(ld->ld_sop->so_owner.len);
  2317. WRITEMEM(ld->ld_sop->so_owner.data, ld->ld_sop->so_owner.len);
  2318. kref_put(&ld->ld_sop->so_ref, nfs4_free_stateowner);
  2319. } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
  2320. WRITE64((u64)0); /* clientid */
  2321. WRITE32(0); /* length of owner name */
  2322. }
  2323. ADJUST_ARGS();
  2324. }
  2325. static __be32
  2326. nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
  2327. {
  2328. ENCODE_SEQID_OP_HEAD;
  2329. if (!nfserr)
  2330. nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
  2331. else if (nfserr == nfserr_denied)
  2332. nfsd4_encode_lock_denied(resp, &lock->lk_denied);
  2333. encode_seqid_op_tail(resp, save, nfserr);
  2334. return nfserr;
  2335. }
  2336. static __be32
  2337. nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
  2338. {
  2339. if (nfserr == nfserr_denied)
  2340. nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
  2341. return nfserr;
  2342. }
  2343. static __be32
  2344. nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
  2345. {
  2346. ENCODE_SEQID_OP_HEAD;
  2347. if (!nfserr)
  2348. nfsd4_encode_stateid(resp, &locku->lu_stateid);
  2349. encode_seqid_op_tail(resp, save, nfserr);
  2350. return nfserr;
  2351. }
  2352. static __be32
  2353. nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
  2354. {
  2355. __be32 *p;
  2356. if (!nfserr) {
  2357. RESERVE_SPACE(20);
  2358. write_cinfo(&p, &link->li_cinfo);
  2359. ADJUST_ARGS();
  2360. }
  2361. return nfserr;
  2362. }
  2363. static __be32
  2364. nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
  2365. {
  2366. __be32 *p;
  2367. ENCODE_SEQID_OP_HEAD;
  2368. if (nfserr)
  2369. goto out;
  2370. nfsd4_encode_stateid(resp, &open->op_stateid);
  2371. RESERVE_SPACE(40);
  2372. write_cinfo(&p, &open->op_cinfo);
  2373. WRITE32(open->op_rflags);
  2374. WRITE32(2);
  2375. WRITE32(open->op_bmval[0]);
  2376. WRITE32(open->op_bmval[1]);
  2377. WRITE32(open->op_delegate_type);
  2378. ADJUST_ARGS();
  2379. switch (open->op_delegate_type) {
  2380. case NFS4_OPEN_DELEGATE_NONE:
  2381. break;
  2382. case NFS4_OPEN_DELEGATE_READ:
  2383. nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
  2384. RESERVE_SPACE(20);
  2385. WRITE32(open->op_recall);
  2386. /*
  2387. * TODO: ACE's in delegations
  2388. */
  2389. WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2390. WRITE32(0);
  2391. WRITE32(0);
  2392. WRITE32(0); /* XXX: is NULL principal ok? */
  2393. ADJUST_ARGS();
  2394. break;
  2395. case NFS4_OPEN_DELEGATE_WRITE:
  2396. nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
  2397. RESERVE_SPACE(32);
  2398. WRITE32(0);
  2399. /*
  2400. * TODO: space_limit's in delegations
  2401. */
  2402. WRITE32(NFS4_LIMIT_SIZE);
  2403. WRITE32(~(u32)0);
  2404. WRITE32(~(u32)0);
  2405. /*
  2406. * TODO: ACE's in delegations
  2407. */
  2408. WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2409. WRITE32(0);
  2410. WRITE32(0);
  2411. WRITE32(0); /* XXX: is NULL principal ok? */
  2412. ADJUST_ARGS();
  2413. break;
  2414. default:
  2415. BUG();
  2416. }
  2417. /* XXX save filehandle here */
  2418. out:
  2419. encode_seqid_op_tail(resp, save, nfserr);
  2420. return nfserr;
  2421. }
  2422. static __be32
  2423. nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
  2424. {
  2425. ENCODE_SEQID_OP_HEAD;
  2426. if (!nfserr)
  2427. nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
  2428. encode_seqid_op_tail(resp, save, nfserr);
  2429. return nfserr;
  2430. }
  2431. static __be32
  2432. nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
  2433. {
  2434. ENCODE_SEQID_OP_HEAD;
  2435. if (!nfserr)
  2436. nfsd4_encode_stateid(resp, &od->od_stateid);
  2437. encode_seqid_op_tail(resp, save, nfserr);
  2438. return nfserr;
  2439. }
  2440. static __be32
  2441. nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
  2442. struct nfsd4_read *read)
  2443. {
  2444. u32 eof;
  2445. int v, pn;
  2446. unsigned long maxcount;
  2447. long len;
  2448. __be32 *p;
  2449. if (nfserr)
  2450. return nfserr;
  2451. if (resp->xbuf->page_len)
  2452. return nfserr_resource;
  2453. RESERVE_SPACE(8); /* eof flag and byte count */
  2454. maxcount = svc_max_payload(resp->rqstp);
  2455. if (maxcount > read->rd_length)
  2456. maxcount = read->rd_length;
  2457. len = maxcount;
  2458. v = 0;
  2459. while (len > 0) {
  2460. pn = resp->rqstp->rq_resused++;
  2461. resp->rqstp->rq_vec[v].iov_base =
  2462. page_address(resp->rqstp->rq_respages[pn]);
  2463. resp->rqstp->rq_vec[v].iov_len =
  2464. len < PAGE_SIZE ? len : PAGE_SIZE;
  2465. v++;
  2466. len -= PAGE_SIZE;
  2467. }
  2468. read->rd_vlen = v;
  2469. nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
  2470. read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
  2471. &maxcount);
  2472. if (nfserr)
  2473. return nfserr;
  2474. eof = (read->rd_offset + maxcount >=
  2475. read->rd_fhp->fh_dentry->d_inode->i_size);
  2476. WRITE32(eof);
  2477. WRITE32(maxcount);
  2478. ADJUST_ARGS();
  2479. resp->xbuf->head[0].iov_len = (char*)p
  2480. - (char*)resp->xbuf->head[0].iov_base;
  2481. resp->xbuf->page_len = maxcount;
  2482. /* Use rest of head for padding and remaining ops: */
  2483. resp->xbuf->tail[0].iov_base = p;
  2484. resp->xbuf->tail[0].iov_len = 0;
  2485. if (maxcount&3) {
  2486. RESERVE_SPACE(4);
  2487. WRITE32(0);
  2488. resp->xbuf->tail[0].iov_base += maxcount&3;
  2489. resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
  2490. ADJUST_ARGS();
  2491. }
  2492. return 0;
  2493. }
  2494. static __be32
  2495. nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
  2496. {
  2497. int maxcount;
  2498. char *page;
  2499. __be32 *p;
  2500. if (nfserr)
  2501. return nfserr;
  2502. if (resp->xbuf->page_len)
  2503. return nfserr_resource;
  2504. page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
  2505. maxcount = PAGE_SIZE;
  2506. RESERVE_SPACE(4);
  2507. /*
  2508. * XXX: By default, the ->readlink() VFS op will truncate symlinks
  2509. * if they would overflow the buffer. Is this kosher in NFSv4? If
  2510. * not, one easy fix is: if ->readlink() precisely fills the buffer,
  2511. * assume that truncation occurred, and return NFS4ERR_RESOURCE.
  2512. */
  2513. nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
  2514. if (nfserr == nfserr_isdir)
  2515. return nfserr_inval;
  2516. if (nfserr)
  2517. return nfserr;
  2518. WRITE32(maxcount);
  2519. ADJUST_ARGS();
  2520. resp->xbuf->head[0].iov_len = (char*)p
  2521. - (char*)resp->xbuf->head[0].iov_base;
  2522. resp->xbuf->page_len = maxcount;
  2523. /* Use rest of head for padding and remaining ops: */
  2524. resp->xbuf->tail[0].iov_base = p;
  2525. resp->xbuf->tail[0].iov_len = 0;
  2526. if (maxcount&3) {
  2527. RESERVE_SPACE(4);
  2528. WRITE32(0);
  2529. resp->xbuf->tail[0].iov_base += maxcount&3;
  2530. resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
  2531. ADJUST_ARGS();
  2532. }
  2533. return 0;
  2534. }
  2535. static __be32
  2536. nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
  2537. {
  2538. int maxcount;
  2539. loff_t offset;
  2540. __be32 *page, *savep, *tailbase;
  2541. __be32 *p;
  2542. if (nfserr)
  2543. return nfserr;
  2544. if (resp->xbuf->page_len)
  2545. return nfserr_resource;
  2546. RESERVE_SPACE(8); /* verifier */
  2547. savep = p;
  2548. /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
  2549. WRITE32(0);
  2550. WRITE32(0);
  2551. ADJUST_ARGS();
  2552. resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
  2553. tailbase = p;
  2554. maxcount = PAGE_SIZE;
  2555. if (maxcount > readdir->rd_maxcount)
  2556. maxcount = readdir->rd_maxcount;
  2557. /*
  2558. * Convert from bytes to words, account for the two words already
  2559. * written, make sure to leave two words at the end for the next
  2560. * pointer and eof field.
  2561. */
  2562. maxcount = (maxcount >> 2) - 4;
  2563. if (maxcount < 0) {
  2564. nfserr = nfserr_toosmall;
  2565. goto err_no_verf;
  2566. }
  2567. page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
  2568. readdir->common.err = 0;
  2569. readdir->buflen = maxcount;
  2570. readdir->buffer = page;
  2571. readdir->offset = NULL;
  2572. offset = readdir->rd_cookie;
  2573. nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
  2574. &offset,
  2575. &readdir->common, nfsd4_encode_dirent);
  2576. if (nfserr == nfs_ok &&
  2577. readdir->common.err == nfserr_toosmall &&
  2578. readdir->buffer == page)
  2579. nfserr = nfserr_toosmall;
  2580. if (nfserr)
  2581. goto err_no_verf;
  2582. if (readdir->offset)
  2583. xdr_encode_hyper(readdir->offset, offset);
  2584. p = readdir->buffer;
  2585. *p++ = 0; /* no more entries */
  2586. *p++ = htonl(readdir->common.err == nfserr_eof);
  2587. resp->xbuf->page_len = ((char*)p) - (char*)page_address(
  2588. resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
  2589. /* Use rest of head for padding and remaining ops: */
  2590. resp->xbuf->tail[0].iov_base = tailbase;
  2591. resp->xbuf->tail[0].iov_len = 0;
  2592. resp->p = resp->xbuf->tail[0].iov_base;
  2593. resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
  2594. return 0;
  2595. err_no_verf:
  2596. p = savep;
  2597. ADJUST_ARGS();
  2598. return nfserr;
  2599. }
  2600. static __be32
  2601. nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
  2602. {
  2603. __be32 *p;
  2604. if (!nfserr) {
  2605. RESERVE_SPACE(20);
  2606. write_cinfo(&p, &remove->rm_cinfo);
  2607. ADJUST_ARGS();
  2608. }
  2609. return nfserr;
  2610. }
  2611. static __be32
  2612. nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
  2613. {
  2614. __be32 *p;
  2615. if (!nfserr) {
  2616. RESERVE_SPACE(40);
  2617. write_cinfo(&p, &rename->rn_sinfo);
  2618. write_cinfo(&p, &rename->rn_tinfo);
  2619. ADJUST_ARGS();
  2620. }
  2621. return nfserr;
  2622. }
  2623. static __be32
  2624. nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
  2625. __be32 nfserr,struct svc_export *exp)
  2626. {
  2627. int i = 0;
  2628. u32 nflavs;
  2629. struct exp_flavor_info *flavs;
  2630. struct exp_flavor_info def_flavs[2];
  2631. __be32 *p;
  2632. if (nfserr)
  2633. goto out;
  2634. if (exp->ex_nflavors) {
  2635. flavs = exp->ex_flavors;
  2636. nflavs = exp->ex_nflavors;
  2637. } else { /* Handling of some defaults in absence of real secinfo: */
  2638. flavs = def_flavs;
  2639. if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
  2640. nflavs = 2;
  2641. flavs[0].pseudoflavor = RPC_AUTH_UNIX;
  2642. flavs[1].pseudoflavor = RPC_AUTH_NULL;
  2643. } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
  2644. nflavs = 1;
  2645. flavs[0].pseudoflavor
  2646. = svcauth_gss_flavor(exp->ex_client);
  2647. } else {
  2648. nflavs = 1;
  2649. flavs[0].pseudoflavor
  2650. = exp->ex_client->flavour->flavour;
  2651. }
  2652. }
  2653. RESERVE_SPACE(4);
  2654. WRITE32(nflavs);
  2655. ADJUST_ARGS();
  2656. for (i = 0; i < nflavs; i++) {
  2657. u32 flav = flavs[i].pseudoflavor;
  2658. struct gss_api_mech *gm = gss_mech_get_by_pseudoflavor(flav);
  2659. if (gm) {
  2660. RESERVE_SPACE(4);
  2661. WRITE32(RPC_AUTH_GSS);
  2662. ADJUST_ARGS();
  2663. RESERVE_SPACE(4 + gm->gm_oid.len);
  2664. WRITE32(gm->gm_oid.len);
  2665. WRITEMEM(gm->gm_oid.data, gm->gm_oid.len);
  2666. ADJUST_ARGS();
  2667. RESERVE_SPACE(4);
  2668. WRITE32(0); /* qop */
  2669. ADJUST_ARGS();
  2670. RESERVE_SPACE(4);
  2671. WRITE32(gss_pseudoflavor_to_service(gm, flav));
  2672. ADJUST_ARGS();
  2673. gss_mech_put(gm);
  2674. } else {
  2675. RESERVE_SPACE(4);
  2676. WRITE32(flav);
  2677. ADJUST_ARGS();
  2678. }
  2679. }
  2680. out:
  2681. if (exp)
  2682. exp_put(exp);
  2683. return nfserr;
  2684. }
  2685. static __be32
  2686. nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  2687. struct nfsd4_secinfo *secinfo)
  2688. {
  2689. return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
  2690. }
  2691. static __be32
  2692. nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
  2693. struct nfsd4_secinfo_no_name *secinfo)
  2694. {
  2695. return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
  2696. }
  2697. /*
  2698. * The SETATTR encode routine is special -- it always encodes a bitmap,
  2699. * regardless of the error status.
  2700. */
  2701. static __be32
  2702. nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
  2703. {
  2704. __be32 *p;
  2705. RESERVE_SPACE(12);
  2706. if (nfserr) {
  2707. WRITE32(2);
  2708. WRITE32(0);
  2709. WRITE32(0);
  2710. }
  2711. else {
  2712. WRITE32(2);
  2713. WRITE32(setattr->sa_bmval[0]);
  2714. WRITE32(setattr->sa_bmval[1]);
  2715. }
  2716. ADJUST_ARGS();
  2717. return nfserr;
  2718. }
  2719. static __be32
  2720. nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
  2721. {
  2722. __be32 *p;
  2723. if (!nfserr) {
  2724. RESERVE_SPACE(8 + sizeof(nfs4_verifier));
  2725. WRITEMEM(&scd->se_clientid, 8);
  2726. WRITEMEM(&scd->se_confirm, sizeof(nfs4_verifier));
  2727. ADJUST_ARGS();
  2728. }
  2729. else if (nfserr == nfserr_clid_inuse) {
  2730. RESERVE_SPACE(8);
  2731. WRITE32(0);
  2732. WRITE32(0);
  2733. ADJUST_ARGS();
  2734. }
  2735. return nfserr;
  2736. }
  2737. static __be32
  2738. nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
  2739. {
  2740. __be32 *p;
  2741. if (!nfserr) {
  2742. RESERVE_SPACE(16);
  2743. WRITE32(write->wr_bytes_written);
  2744. WRITE32(write->wr_how_written);
  2745. WRITEMEM(write->wr_verifier.data, 8);
  2746. ADJUST_ARGS();
  2747. }
  2748. return nfserr;
  2749. }
  2750. static __be32
  2751. nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, int nfserr,
  2752. struct nfsd4_exchange_id *exid)
  2753. {
  2754. __be32 *p;
  2755. char *major_id;
  2756. char *server_scope;
  2757. int major_id_sz;
  2758. int server_scope_sz;
  2759. uint64_t minor_id = 0;
  2760. if (nfserr)
  2761. return nfserr;
  2762. major_id = utsname()->nodename;
  2763. major_id_sz = strlen(major_id);
  2764. server_scope = utsname()->nodename;
  2765. server_scope_sz = strlen(server_scope);
  2766. RESERVE_SPACE(
  2767. 8 /* eir_clientid */ +
  2768. 4 /* eir_sequenceid */ +
  2769. 4 /* eir_flags */ +
  2770. 4 /* spr_how (SP4_NONE) */ +
  2771. 8 /* so_minor_id */ +
  2772. 4 /* so_major_id.len */ +
  2773. (XDR_QUADLEN(major_id_sz) * 4) +
  2774. 4 /* eir_server_scope.len */ +
  2775. (XDR_QUADLEN(server_scope_sz) * 4) +
  2776. 4 /* eir_server_impl_id.count (0) */);
  2777. WRITEMEM(&exid->clientid, 8);
  2778. WRITE32(exid->seqid);
  2779. WRITE32(exid->flags);
  2780. /* state_protect4_r. Currently only support SP4_NONE */
  2781. BUG_ON(exid->spa_how != SP4_NONE);
  2782. WRITE32(exid->spa_how);
  2783. /* The server_owner struct */
  2784. WRITE64(minor_id); /* Minor id */
  2785. /* major id */
  2786. WRITE32(major_id_sz);
  2787. WRITEMEM(major_id, major_id_sz);
  2788. /* Server scope */
  2789. WRITE32(server_scope_sz);
  2790. WRITEMEM(server_scope, server_scope_sz);
  2791. /* Implementation id */
  2792. WRITE32(0); /* zero length nfs_impl_id4 array */
  2793. ADJUST_ARGS();
  2794. return 0;
  2795. }
  2796. static __be32
  2797. nfsd4_encode_create_session(struct nfsd4_compoundres *resp, int nfserr,
  2798. struct nfsd4_create_session *sess)
  2799. {
  2800. __be32 *p;
  2801. if (nfserr)
  2802. return nfserr;
  2803. RESERVE_SPACE(24);
  2804. WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  2805. WRITE32(sess->seqid);
  2806. WRITE32(sess->flags);
  2807. ADJUST_ARGS();
  2808. RESERVE_SPACE(28);
  2809. WRITE32(0); /* headerpadsz */
  2810. WRITE32(sess->fore_channel.maxreq_sz);
  2811. WRITE32(sess->fore_channel.maxresp_sz);
  2812. WRITE32(sess->fore_channel.maxresp_cached);
  2813. WRITE32(sess->fore_channel.maxops);
  2814. WRITE32(sess->fore_channel.maxreqs);
  2815. WRITE32(sess->fore_channel.nr_rdma_attrs);
  2816. ADJUST_ARGS();
  2817. if (sess->fore_channel.nr_rdma_attrs) {
  2818. RESERVE_SPACE(4);
  2819. WRITE32(sess->fore_channel.rdma_attrs);
  2820. ADJUST_ARGS();
  2821. }
  2822. RESERVE_SPACE(28);
  2823. WRITE32(0); /* headerpadsz */
  2824. WRITE32(sess->back_channel.maxreq_sz);
  2825. WRITE32(sess->back_channel.maxresp_sz);
  2826. WRITE32(sess->back_channel.maxresp_cached);
  2827. WRITE32(sess->back_channel.maxops);
  2828. WRITE32(sess->back_channel.maxreqs);
  2829. WRITE32(sess->back_channel.nr_rdma_attrs);
  2830. ADJUST_ARGS();
  2831. if (sess->back_channel.nr_rdma_attrs) {
  2832. RESERVE_SPACE(4);
  2833. WRITE32(sess->back_channel.rdma_attrs);
  2834. ADJUST_ARGS();
  2835. }
  2836. return 0;
  2837. }
  2838. static __be32
  2839. nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, int nfserr,
  2840. struct nfsd4_destroy_session *destroy_session)
  2841. {
  2842. return nfserr;
  2843. }
  2844. static __be32
  2845. nfsd4_encode_free_stateid(struct nfsd4_compoundres *resp, int nfserr,
  2846. struct nfsd4_free_stateid *free_stateid)
  2847. {
  2848. __be32 *p;
  2849. if (nfserr)
  2850. return nfserr;
  2851. RESERVE_SPACE(4);
  2852. WRITE32(nfserr);
  2853. ADJUST_ARGS();
  2854. return nfserr;
  2855. }
  2856. static __be32
  2857. nfsd4_encode_sequence(struct nfsd4_compoundres *resp, int nfserr,
  2858. struct nfsd4_sequence *seq)
  2859. {
  2860. __be32 *p;
  2861. if (nfserr)
  2862. return nfserr;
  2863. RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
  2864. WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  2865. WRITE32(seq->seqid);
  2866. WRITE32(seq->slotid);
  2867. /* Note slotid's are numbered from zero: */
  2868. WRITE32(seq->maxslots - 1); /* sr_highest_slotid */
  2869. WRITE32(seq->maxslots - 1); /* sr_target_highest_slotid */
  2870. WRITE32(seq->status_flags);
  2871. ADJUST_ARGS();
  2872. resp->cstate.datap = p; /* DRC cache data pointer */
  2873. return 0;
  2874. }
  2875. __be32
  2876. nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, int nfserr,
  2877. struct nfsd4_test_stateid *test_stateid)
  2878. {
  2879. struct nfsd4_compoundargs *argp;
  2880. stateid_t si;
  2881. __be32 *p;
  2882. int i;
  2883. int valid;
  2884. restore_buf(test_stateid->ts_saved_args, &test_stateid->ts_savedp);
  2885. argp = test_stateid->ts_saved_args;
  2886. RESERVE_SPACE(4);
  2887. *p++ = htonl(test_stateid->ts_num_ids);
  2888. resp->p = p;
  2889. nfs4_lock_state();
  2890. for (i = 0; i < test_stateid->ts_num_ids; i++) {
  2891. nfsd4_decode_stateid(argp, &si);
  2892. valid = nfs4_validate_stateid(&si, test_stateid->ts_has_session);
  2893. RESERVE_SPACE(4);
  2894. *p++ = htonl(valid);
  2895. resp->p = p;
  2896. }
  2897. nfs4_unlock_state();
  2898. return nfserr;
  2899. }
  2900. static __be32
  2901. nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
  2902. {
  2903. return nfserr;
  2904. }
  2905. typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
  2906. /*
  2907. * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
  2908. * since we don't need to filter out obsolete ops as this is
  2909. * done in the decoding phase.
  2910. */
  2911. static nfsd4_enc nfsd4_enc_ops[] = {
  2912. [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
  2913. [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
  2914. [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
  2915. [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
  2916. [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
  2917. [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  2918. [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
  2919. [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
  2920. [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
  2921. [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
  2922. [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
  2923. [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
  2924. [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
  2925. [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
  2926. [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  2927. [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
  2928. [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
  2929. [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
  2930. [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
  2931. [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
  2932. [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
  2933. [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
  2934. [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
  2935. [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
  2936. [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
  2937. [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
  2938. [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
  2939. [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
  2940. [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
  2941. [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
  2942. [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
  2943. [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
  2944. [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
  2945. [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
  2946. [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  2947. [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
  2948. [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
  2949. /* NFSv4.1 operations */
  2950. [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
  2951. [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
  2952. [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
  2953. [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
  2954. [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
  2955. [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_free_stateid,
  2956. [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  2957. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
  2958. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  2959. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
  2960. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
  2961. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  2962. [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
  2963. [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
  2964. [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
  2965. [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
  2966. [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  2967. [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
  2968. [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
  2969. };
  2970. /*
  2971. * Calculate the total amount of memory that the compound response has taken
  2972. * after encoding the current operation.
  2973. *
  2974. * pad: add on 8 bytes for the next operation's op_code and status so that
  2975. * there is room to cache a failure on the next operation.
  2976. *
  2977. * Compare this length to the session se_fmaxresp_cached.
  2978. *
  2979. * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
  2980. * will be at least a page and will therefore hold the xdr_buf head.
  2981. */
  2982. static int nfsd4_check_drc_limit(struct nfsd4_compoundres *resp)
  2983. {
  2984. int status = 0;
  2985. struct xdr_buf *xb = &resp->rqstp->rq_res;
  2986. struct nfsd4_compoundargs *args = resp->rqstp->rq_argp;
  2987. struct nfsd4_session *session = NULL;
  2988. struct nfsd4_slot *slot = resp->cstate.slot;
  2989. u32 length, tlen = 0, pad = 8;
  2990. if (!nfsd4_has_session(&resp->cstate))
  2991. return status;
  2992. session = resp->cstate.session;
  2993. if (session == NULL || slot->sl_cachethis == 0)
  2994. return status;
  2995. if (resp->opcnt >= args->opcnt)
  2996. pad = 0; /* this is the last operation */
  2997. if (xb->page_len == 0) {
  2998. length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
  2999. } else {
  3000. if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
  3001. tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
  3002. length = xb->head[0].iov_len + xb->page_len + tlen + pad;
  3003. }
  3004. dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
  3005. length, xb->page_len, tlen, pad);
  3006. if (length <= session->se_fchannel.maxresp_cached)
  3007. return status;
  3008. else
  3009. return nfserr_rep_too_big_to_cache;
  3010. }
  3011. void
  3012. nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  3013. {
  3014. __be32 *statp;
  3015. __be32 *p;
  3016. RESERVE_SPACE(8);
  3017. WRITE32(op->opnum);
  3018. statp = p++; /* to be backfilled at the end */
  3019. ADJUST_ARGS();
  3020. if (op->opnum == OP_ILLEGAL)
  3021. goto status;
  3022. BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
  3023. !nfsd4_enc_ops[op->opnum]);
  3024. op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
  3025. /* nfsd4_check_drc_limit guarantees enough room for error status */
  3026. if (!op->status && nfsd4_check_drc_limit(resp))
  3027. op->status = nfserr_rep_too_big_to_cache;
  3028. status:
  3029. /*
  3030. * Note: We write the status directly, instead of using WRITE32(),
  3031. * since it is already in network byte order.
  3032. */
  3033. *statp = op->status;
  3034. }
  3035. /*
  3036. * Encode the reply stored in the stateowner reply cache
  3037. *
  3038. * XDR note: do not encode rp->rp_buflen: the buffer contains the
  3039. * previously sent already encoded operation.
  3040. *
  3041. * called with nfs4_lock_state() held
  3042. */
  3043. void
  3044. nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  3045. {
  3046. __be32 *p;
  3047. struct nfs4_replay *rp = op->replay;
  3048. BUG_ON(!rp);
  3049. RESERVE_SPACE(8);
  3050. WRITE32(op->opnum);
  3051. *p++ = rp->rp_status; /* already xdr'ed */
  3052. ADJUST_ARGS();
  3053. RESERVE_SPACE(rp->rp_buflen);
  3054. WRITEMEM(rp->rp_buf, rp->rp_buflen);
  3055. ADJUST_ARGS();
  3056. }
  3057. int
  3058. nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  3059. {
  3060. return xdr_ressize_check(rqstp, p);
  3061. }
  3062. int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
  3063. {
  3064. struct svc_rqst *rqstp = rq;
  3065. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  3066. if (args->ops != args->iops) {
  3067. kfree(args->ops);
  3068. args->ops = args->iops;
  3069. }
  3070. kfree(args->tmpp);
  3071. args->tmpp = NULL;
  3072. while (args->to_free) {
  3073. struct tmpbuf *tb = args->to_free;
  3074. args->to_free = tb->next;
  3075. tb->release(tb->buf);
  3076. kfree(tb);
  3077. }
  3078. return 1;
  3079. }
  3080. int
  3081. nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
  3082. {
  3083. args->p = p;
  3084. args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
  3085. args->pagelist = rqstp->rq_arg.pages;
  3086. args->pagelen = rqstp->rq_arg.page_len;
  3087. args->tmpp = NULL;
  3088. args->to_free = NULL;
  3089. args->ops = args->iops;
  3090. args->rqstp = rqstp;
  3091. return !nfsd4_decode_compound(args);
  3092. }
  3093. int
  3094. nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
  3095. {
  3096. /*
  3097. * All that remains is to write the tag and operation count...
  3098. */
  3099. struct nfsd4_compound_state *cs = &resp->cstate;
  3100. struct kvec *iov;
  3101. p = resp->tagp;
  3102. *p++ = htonl(resp->taglen);
  3103. memcpy(p, resp->tag, resp->taglen);
  3104. p += XDR_QUADLEN(resp->taglen);
  3105. *p++ = htonl(resp->opcnt);
  3106. if (rqstp->rq_res.page_len)
  3107. iov = &rqstp->rq_res.tail[0];
  3108. else
  3109. iov = &rqstp->rq_res.head[0];
  3110. iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
  3111. BUG_ON(iov->iov_len > PAGE_SIZE);
  3112. if (nfsd4_has_session(cs)) {
  3113. if (cs->status != nfserr_replay_cache) {
  3114. nfsd4_store_cache_entry(resp);
  3115. dprintk("%s: SET SLOT STATE TO AVAILABLE\n", __func__);
  3116. cs->slot->sl_inuse = false;
  3117. }
  3118. /* Renew the clientid on success and on replay */
  3119. release_session_client(cs->session);
  3120. nfsd4_put_session(cs->session);
  3121. }
  3122. return 1;
  3123. }
  3124. /*
  3125. * Local variables:
  3126. * c-basic-offset: 8
  3127. * End:
  3128. */