nfs4xdr.c 90 KB

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