nfs4xdr.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437
  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. #include <linux/slab.h>
  36. #include <linux/namei.h>
  37. #include <linux/statfs.h>
  38. #include <linux/utsname.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/sunrpc/svcauth_gss.h>
  41. #include "idmap.h"
  42. #include "acl.h"
  43. #include "xdr4.h"
  44. #include "vfs.h"
  45. #include "state.h"
  46. #include "cache.h"
  47. #include "netns.h"
  48. #include "pnfs.h"
  49. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  50. #include <linux/security.h>
  51. #endif
  52. #define NFSDDBG_FACILITY NFSDDBG_XDR
  53. /*
  54. * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
  55. * directory in order to indicate to the client that a filesystem boundary is present
  56. * We use a fixed fsid for a referral
  57. */
  58. #define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
  59. #define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
  60. static __be32
  61. check_filename(char *str, int len)
  62. {
  63. int i;
  64. if (len == 0)
  65. return nfserr_inval;
  66. if (isdotent(str, len))
  67. return nfserr_badname;
  68. for (i = 0; i < len; i++)
  69. if (str[i] == '/')
  70. return nfserr_badname;
  71. return 0;
  72. }
  73. #define DECODE_HEAD \
  74. __be32 *p; \
  75. __be32 status
  76. #define DECODE_TAIL \
  77. status = 0; \
  78. out: \
  79. return status; \
  80. xdr_error: \
  81. dprintk("NFSD: xdr error (%s:%d)\n", \
  82. __FILE__, __LINE__); \
  83. status = nfserr_bad_xdr; \
  84. goto out
  85. #define READMEM(x,nbytes) do { \
  86. x = (char *)p; \
  87. p += XDR_QUADLEN(nbytes); \
  88. } while (0)
  89. #define SAVEMEM(x,nbytes) do { \
  90. if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
  91. savemem(argp, p, nbytes) : \
  92. (char *)p)) { \
  93. dprintk("NFSD: xdr error (%s:%d)\n", \
  94. __FILE__, __LINE__); \
  95. goto xdr_error; \
  96. } \
  97. p += XDR_QUADLEN(nbytes); \
  98. } while (0)
  99. #define COPYMEM(x,nbytes) do { \
  100. memcpy((x), p, nbytes); \
  101. p += XDR_QUADLEN(nbytes); \
  102. } while (0)
  103. /* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
  104. #define READ_BUF(nbytes) do { \
  105. if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
  106. p = argp->p; \
  107. argp->p += XDR_QUADLEN(nbytes); \
  108. } else if (!(p = read_buf(argp, nbytes))) { \
  109. dprintk("NFSD: xdr error (%s:%d)\n", \
  110. __FILE__, __LINE__); \
  111. goto xdr_error; \
  112. } \
  113. } while (0)
  114. static void next_decode_page(struct nfsd4_compoundargs *argp)
  115. {
  116. argp->p = page_address(argp->pagelist[0]);
  117. argp->pagelist++;
  118. if (argp->pagelen < PAGE_SIZE) {
  119. argp->end = argp->p + (argp->pagelen>>2);
  120. argp->pagelen = 0;
  121. } else {
  122. argp->end = argp->p + (PAGE_SIZE>>2);
  123. argp->pagelen -= PAGE_SIZE;
  124. }
  125. }
  126. static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
  127. {
  128. /* We want more bytes than seem to be available.
  129. * Maybe we need a new page, maybe we have just run out
  130. */
  131. unsigned int avail = (char *)argp->end - (char *)argp->p;
  132. __be32 *p;
  133. if (avail + argp->pagelen < nbytes)
  134. return NULL;
  135. if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
  136. return NULL;
  137. /* ok, we can do it with the current plus the next page */
  138. if (nbytes <= sizeof(argp->tmp))
  139. p = argp->tmp;
  140. else {
  141. kfree(argp->tmpp);
  142. p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
  143. if (!p)
  144. return NULL;
  145. }
  146. /*
  147. * The following memcpy is safe because read_buf is always
  148. * called with nbytes > avail, and the two cases above both
  149. * guarantee p points to at least nbytes bytes.
  150. */
  151. memcpy(p, argp->p, avail);
  152. next_decode_page(argp);
  153. memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
  154. argp->p += XDR_QUADLEN(nbytes - avail);
  155. return p;
  156. }
  157. static int zero_clientid(clientid_t *clid)
  158. {
  159. return (clid->cl_boot == 0) && (clid->cl_id == 0);
  160. }
  161. /**
  162. * svcxdr_tmpalloc - allocate memory to be freed after compound processing
  163. * @argp: NFSv4 compound argument structure
  164. * @p: pointer to be freed (with kfree())
  165. *
  166. * Marks @p to be freed when processing the compound operation
  167. * described in @argp finishes.
  168. */
  169. static void *
  170. svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, u32 len)
  171. {
  172. struct svcxdr_tmpbuf *tb;
  173. tb = kmalloc(sizeof(*tb) + len, GFP_KERNEL);
  174. if (!tb)
  175. return NULL;
  176. tb->next = argp->to_free;
  177. argp->to_free = tb;
  178. return tb->buf;
  179. }
  180. /*
  181. * For xdr strings that need to be passed to other kernel api's
  182. * as null-terminated strings.
  183. *
  184. * Note null-terminating in place usually isn't safe since the
  185. * buffer might end on a page boundary.
  186. */
  187. static char *
  188. svcxdr_dupstr(struct nfsd4_compoundargs *argp, void *buf, u32 len)
  189. {
  190. char *p = svcxdr_tmpalloc(argp, len + 1);
  191. if (!p)
  192. return NULL;
  193. memcpy(p, buf, len);
  194. p[len] = '\0';
  195. return p;
  196. }
  197. /**
  198. * savemem - duplicate a chunk of memory for later processing
  199. * @argp: NFSv4 compound argument structure to be freed with
  200. * @p: pointer to be duplicated
  201. * @nbytes: length to be duplicated
  202. *
  203. * Returns a pointer to a copy of @nbytes bytes of memory at @p
  204. * that are preserved until processing of the NFSv4 compound
  205. * operation described by @argp finishes.
  206. */
  207. static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
  208. {
  209. void *ret;
  210. ret = svcxdr_tmpalloc(argp, nbytes);
  211. if (!ret)
  212. return NULL;
  213. memcpy(ret, p, nbytes);
  214. return ret;
  215. }
  216. /*
  217. * We require the high 32 bits of 'seconds' to be 0, and
  218. * we ignore all 32 bits of 'nseconds'.
  219. */
  220. static __be32
  221. nfsd4_decode_time(struct nfsd4_compoundargs *argp, struct timespec *tv)
  222. {
  223. DECODE_HEAD;
  224. u64 sec;
  225. READ_BUF(12);
  226. p = xdr_decode_hyper(p, &sec);
  227. tv->tv_sec = sec;
  228. tv->tv_nsec = be32_to_cpup(p++);
  229. if (tv->tv_nsec >= (u32)1000000000)
  230. return nfserr_inval;
  231. DECODE_TAIL;
  232. }
  233. static __be32
  234. nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
  235. {
  236. u32 bmlen;
  237. DECODE_HEAD;
  238. bmval[0] = 0;
  239. bmval[1] = 0;
  240. bmval[2] = 0;
  241. READ_BUF(4);
  242. bmlen = be32_to_cpup(p++);
  243. if (bmlen > 1000)
  244. goto xdr_error;
  245. READ_BUF(bmlen << 2);
  246. if (bmlen > 0)
  247. bmval[0] = be32_to_cpup(p++);
  248. if (bmlen > 1)
  249. bmval[1] = be32_to_cpup(p++);
  250. if (bmlen > 2)
  251. bmval[2] = be32_to_cpup(p++);
  252. DECODE_TAIL;
  253. }
  254. static __be32
  255. nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
  256. struct iattr *iattr, struct nfs4_acl **acl,
  257. struct xdr_netobj *label)
  258. {
  259. int expected_len, len = 0;
  260. u32 dummy32;
  261. char *buf;
  262. DECODE_HEAD;
  263. iattr->ia_valid = 0;
  264. if ((status = nfsd4_decode_bitmap(argp, bmval)))
  265. return status;
  266. READ_BUF(4);
  267. expected_len = be32_to_cpup(p++);
  268. if (bmval[0] & FATTR4_WORD0_SIZE) {
  269. READ_BUF(8);
  270. len += 8;
  271. p = xdr_decode_hyper(p, &iattr->ia_size);
  272. iattr->ia_valid |= ATTR_SIZE;
  273. }
  274. if (bmval[0] & FATTR4_WORD0_ACL) {
  275. u32 nace;
  276. struct nfs4_ace *ace;
  277. READ_BUF(4); len += 4;
  278. nace = be32_to_cpup(p++);
  279. if (nace > NFS4_ACL_MAX)
  280. return nfserr_fbig;
  281. *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(nace));
  282. if (*acl == NULL)
  283. return nfserr_jukebox;
  284. (*acl)->naces = nace;
  285. for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
  286. READ_BUF(16); len += 16;
  287. ace->type = be32_to_cpup(p++);
  288. ace->flag = be32_to_cpup(p++);
  289. ace->access_mask = be32_to_cpup(p++);
  290. dummy32 = be32_to_cpup(p++);
  291. READ_BUF(dummy32);
  292. len += XDR_QUADLEN(dummy32) << 2;
  293. READMEM(buf, dummy32);
  294. ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
  295. status = nfs_ok;
  296. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  297. ;
  298. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  299. status = nfsd_map_name_to_gid(argp->rqstp,
  300. buf, dummy32, &ace->who_gid);
  301. else
  302. status = nfsd_map_name_to_uid(argp->rqstp,
  303. buf, dummy32, &ace->who_uid);
  304. if (status)
  305. return status;
  306. }
  307. } else
  308. *acl = NULL;
  309. if (bmval[1] & FATTR4_WORD1_MODE) {
  310. READ_BUF(4);
  311. len += 4;
  312. iattr->ia_mode = be32_to_cpup(p++);
  313. iattr->ia_mode &= (S_IFMT | S_IALLUGO);
  314. iattr->ia_valid |= ATTR_MODE;
  315. }
  316. if (bmval[1] & FATTR4_WORD1_OWNER) {
  317. READ_BUF(4);
  318. len += 4;
  319. dummy32 = be32_to_cpup(p++);
  320. READ_BUF(dummy32);
  321. len += (XDR_QUADLEN(dummy32) << 2);
  322. READMEM(buf, dummy32);
  323. if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
  324. return status;
  325. iattr->ia_valid |= ATTR_UID;
  326. }
  327. if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
  328. READ_BUF(4);
  329. len += 4;
  330. dummy32 = be32_to_cpup(p++);
  331. READ_BUF(dummy32);
  332. len += (XDR_QUADLEN(dummy32) << 2);
  333. READMEM(buf, dummy32);
  334. if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
  335. return status;
  336. iattr->ia_valid |= ATTR_GID;
  337. }
  338. if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
  339. READ_BUF(4);
  340. len += 4;
  341. dummy32 = be32_to_cpup(p++);
  342. switch (dummy32) {
  343. case NFS4_SET_TO_CLIENT_TIME:
  344. len += 12;
  345. status = nfsd4_decode_time(argp, &iattr->ia_atime);
  346. if (status)
  347. return status;
  348. iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
  349. break;
  350. case NFS4_SET_TO_SERVER_TIME:
  351. iattr->ia_valid |= ATTR_ATIME;
  352. break;
  353. default:
  354. goto xdr_error;
  355. }
  356. }
  357. if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
  358. READ_BUF(4);
  359. len += 4;
  360. dummy32 = be32_to_cpup(p++);
  361. switch (dummy32) {
  362. case NFS4_SET_TO_CLIENT_TIME:
  363. len += 12;
  364. status = nfsd4_decode_time(argp, &iattr->ia_mtime);
  365. if (status)
  366. return status;
  367. iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
  368. break;
  369. case NFS4_SET_TO_SERVER_TIME:
  370. iattr->ia_valid |= ATTR_MTIME;
  371. break;
  372. default:
  373. goto xdr_error;
  374. }
  375. }
  376. label->len = 0;
  377. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  378. if (bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
  379. READ_BUF(4);
  380. len += 4;
  381. dummy32 = be32_to_cpup(p++); /* lfs: we don't use it */
  382. READ_BUF(4);
  383. len += 4;
  384. dummy32 = be32_to_cpup(p++); /* pi: we don't use it either */
  385. READ_BUF(4);
  386. len += 4;
  387. dummy32 = be32_to_cpup(p++);
  388. READ_BUF(dummy32);
  389. if (dummy32 > NFSD4_MAX_SEC_LABEL_LEN)
  390. return nfserr_badlabel;
  391. len += (XDR_QUADLEN(dummy32) << 2);
  392. READMEM(buf, dummy32);
  393. label->len = dummy32;
  394. label->data = svcxdr_dupstr(argp, buf, dummy32);
  395. if (!label->data)
  396. return nfserr_jukebox;
  397. }
  398. #endif
  399. if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
  400. || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
  401. || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
  402. READ_BUF(expected_len - len);
  403. else if (len != expected_len)
  404. goto xdr_error;
  405. DECODE_TAIL;
  406. }
  407. static __be32
  408. nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
  409. {
  410. DECODE_HEAD;
  411. READ_BUF(sizeof(stateid_t));
  412. sid->si_generation = be32_to_cpup(p++);
  413. COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
  414. DECODE_TAIL;
  415. }
  416. static __be32
  417. nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
  418. {
  419. DECODE_HEAD;
  420. READ_BUF(4);
  421. access->ac_req_access = be32_to_cpup(p++);
  422. DECODE_TAIL;
  423. }
  424. static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
  425. {
  426. DECODE_HEAD;
  427. u32 dummy, uid, gid;
  428. char *machine_name;
  429. int i;
  430. int nr_secflavs;
  431. /* callback_sec_params4 */
  432. READ_BUF(4);
  433. nr_secflavs = be32_to_cpup(p++);
  434. if (nr_secflavs)
  435. cbs->flavor = (u32)(-1);
  436. else
  437. /* Is this legal? Be generous, take it to mean AUTH_NONE: */
  438. cbs->flavor = 0;
  439. for (i = 0; i < nr_secflavs; ++i) {
  440. READ_BUF(4);
  441. dummy = be32_to_cpup(p++);
  442. switch (dummy) {
  443. case RPC_AUTH_NULL:
  444. /* Nothing to read */
  445. if (cbs->flavor == (u32)(-1))
  446. cbs->flavor = RPC_AUTH_NULL;
  447. break;
  448. case RPC_AUTH_UNIX:
  449. READ_BUF(8);
  450. /* stamp */
  451. dummy = be32_to_cpup(p++);
  452. /* machine name */
  453. dummy = be32_to_cpup(p++);
  454. READ_BUF(dummy);
  455. SAVEMEM(machine_name, dummy);
  456. /* uid, gid */
  457. READ_BUF(8);
  458. uid = be32_to_cpup(p++);
  459. gid = be32_to_cpup(p++);
  460. /* more gids */
  461. READ_BUF(4);
  462. dummy = be32_to_cpup(p++);
  463. READ_BUF(dummy * 4);
  464. if (cbs->flavor == (u32)(-1)) {
  465. kuid_t kuid = make_kuid(&init_user_ns, uid);
  466. kgid_t kgid = make_kgid(&init_user_ns, gid);
  467. if (uid_valid(kuid) && gid_valid(kgid)) {
  468. cbs->uid = kuid;
  469. cbs->gid = kgid;
  470. cbs->flavor = RPC_AUTH_UNIX;
  471. } else {
  472. dprintk("RPC_AUTH_UNIX with invalid"
  473. "uid or gid ignoring!\n");
  474. }
  475. }
  476. break;
  477. case RPC_AUTH_GSS:
  478. dprintk("RPC_AUTH_GSS callback secflavor "
  479. "not supported!\n");
  480. READ_BUF(8);
  481. /* gcbp_service */
  482. dummy = be32_to_cpup(p++);
  483. /* gcbp_handle_from_server */
  484. dummy = be32_to_cpup(p++);
  485. READ_BUF(dummy);
  486. p += XDR_QUADLEN(dummy);
  487. /* gcbp_handle_from_client */
  488. READ_BUF(4);
  489. dummy = be32_to_cpup(p++);
  490. READ_BUF(dummy);
  491. break;
  492. default:
  493. dprintk("Illegal callback secflavor\n");
  494. return nfserr_inval;
  495. }
  496. }
  497. DECODE_TAIL;
  498. }
  499. static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
  500. {
  501. DECODE_HEAD;
  502. READ_BUF(4);
  503. bc->bc_cb_program = be32_to_cpup(p++);
  504. nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
  505. DECODE_TAIL;
  506. }
  507. static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
  508. {
  509. DECODE_HEAD;
  510. READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
  511. COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  512. bcts->dir = be32_to_cpup(p++);
  513. /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
  514. * could help us figure out we should be using it. */
  515. DECODE_TAIL;
  516. }
  517. static __be32
  518. nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
  519. {
  520. DECODE_HEAD;
  521. READ_BUF(4);
  522. close->cl_seqid = be32_to_cpup(p++);
  523. return nfsd4_decode_stateid(argp, &close->cl_stateid);
  524. DECODE_TAIL;
  525. }
  526. static __be32
  527. nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
  528. {
  529. DECODE_HEAD;
  530. READ_BUF(12);
  531. p = xdr_decode_hyper(p, &commit->co_offset);
  532. commit->co_count = be32_to_cpup(p++);
  533. DECODE_TAIL;
  534. }
  535. static __be32
  536. nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
  537. {
  538. DECODE_HEAD;
  539. READ_BUF(4);
  540. create->cr_type = be32_to_cpup(p++);
  541. switch (create->cr_type) {
  542. case NF4LNK:
  543. READ_BUF(4);
  544. create->cr_datalen = be32_to_cpup(p++);
  545. READ_BUF(create->cr_datalen);
  546. create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
  547. if (!create->cr_data)
  548. return nfserr_jukebox;
  549. break;
  550. case NF4BLK:
  551. case NF4CHR:
  552. READ_BUF(8);
  553. create->cr_specdata1 = be32_to_cpup(p++);
  554. create->cr_specdata2 = be32_to_cpup(p++);
  555. break;
  556. case NF4SOCK:
  557. case NF4FIFO:
  558. case NF4DIR:
  559. default:
  560. break;
  561. }
  562. READ_BUF(4);
  563. create->cr_namelen = be32_to_cpup(p++);
  564. READ_BUF(create->cr_namelen);
  565. SAVEMEM(create->cr_name, create->cr_namelen);
  566. if ((status = check_filename(create->cr_name, create->cr_namelen)))
  567. return status;
  568. status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
  569. &create->cr_acl, &create->cr_label);
  570. if (status)
  571. goto out;
  572. DECODE_TAIL;
  573. }
  574. static inline __be32
  575. nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
  576. {
  577. return nfsd4_decode_stateid(argp, &dr->dr_stateid);
  578. }
  579. static inline __be32
  580. nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
  581. {
  582. return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
  583. }
  584. static __be32
  585. nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
  586. {
  587. DECODE_HEAD;
  588. READ_BUF(4);
  589. link->li_namelen = be32_to_cpup(p++);
  590. READ_BUF(link->li_namelen);
  591. SAVEMEM(link->li_name, link->li_namelen);
  592. if ((status = check_filename(link->li_name, link->li_namelen)))
  593. return status;
  594. DECODE_TAIL;
  595. }
  596. static __be32
  597. nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
  598. {
  599. DECODE_HEAD;
  600. /*
  601. * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
  602. */
  603. READ_BUF(28);
  604. lock->lk_type = be32_to_cpup(p++);
  605. if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
  606. goto xdr_error;
  607. lock->lk_reclaim = be32_to_cpup(p++);
  608. p = xdr_decode_hyper(p, &lock->lk_offset);
  609. p = xdr_decode_hyper(p, &lock->lk_length);
  610. lock->lk_is_new = be32_to_cpup(p++);
  611. if (lock->lk_is_new) {
  612. READ_BUF(4);
  613. lock->lk_new_open_seqid = be32_to_cpup(p++);
  614. status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
  615. if (status)
  616. return status;
  617. READ_BUF(8 + sizeof(clientid_t));
  618. lock->lk_new_lock_seqid = be32_to_cpup(p++);
  619. COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
  620. lock->lk_new_owner.len = be32_to_cpup(p++);
  621. READ_BUF(lock->lk_new_owner.len);
  622. READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
  623. } else {
  624. status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
  625. if (status)
  626. return status;
  627. READ_BUF(4);
  628. lock->lk_old_lock_seqid = be32_to_cpup(p++);
  629. }
  630. DECODE_TAIL;
  631. }
  632. static __be32
  633. nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
  634. {
  635. DECODE_HEAD;
  636. READ_BUF(32);
  637. lockt->lt_type = be32_to_cpup(p++);
  638. if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
  639. goto xdr_error;
  640. p = xdr_decode_hyper(p, &lockt->lt_offset);
  641. p = xdr_decode_hyper(p, &lockt->lt_length);
  642. COPYMEM(&lockt->lt_clientid, 8);
  643. lockt->lt_owner.len = be32_to_cpup(p++);
  644. READ_BUF(lockt->lt_owner.len);
  645. READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
  646. DECODE_TAIL;
  647. }
  648. static __be32
  649. nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
  650. {
  651. DECODE_HEAD;
  652. READ_BUF(8);
  653. locku->lu_type = be32_to_cpup(p++);
  654. if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
  655. goto xdr_error;
  656. locku->lu_seqid = be32_to_cpup(p++);
  657. status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
  658. if (status)
  659. return status;
  660. READ_BUF(16);
  661. p = xdr_decode_hyper(p, &locku->lu_offset);
  662. p = xdr_decode_hyper(p, &locku->lu_length);
  663. DECODE_TAIL;
  664. }
  665. static __be32
  666. nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
  667. {
  668. DECODE_HEAD;
  669. READ_BUF(4);
  670. lookup->lo_len = be32_to_cpup(p++);
  671. READ_BUF(lookup->lo_len);
  672. SAVEMEM(lookup->lo_name, lookup->lo_len);
  673. if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
  674. return status;
  675. DECODE_TAIL;
  676. }
  677. static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
  678. {
  679. __be32 *p;
  680. u32 w;
  681. READ_BUF(4);
  682. w = be32_to_cpup(p++);
  683. *share_access = w & NFS4_SHARE_ACCESS_MASK;
  684. *deleg_want = w & NFS4_SHARE_WANT_MASK;
  685. if (deleg_when)
  686. *deleg_when = w & NFS4_SHARE_WHEN_MASK;
  687. switch (w & NFS4_SHARE_ACCESS_MASK) {
  688. case NFS4_SHARE_ACCESS_READ:
  689. case NFS4_SHARE_ACCESS_WRITE:
  690. case NFS4_SHARE_ACCESS_BOTH:
  691. break;
  692. default:
  693. return nfserr_bad_xdr;
  694. }
  695. w &= ~NFS4_SHARE_ACCESS_MASK;
  696. if (!w)
  697. return nfs_ok;
  698. if (!argp->minorversion)
  699. return nfserr_bad_xdr;
  700. switch (w & NFS4_SHARE_WANT_MASK) {
  701. case NFS4_SHARE_WANT_NO_PREFERENCE:
  702. case NFS4_SHARE_WANT_READ_DELEG:
  703. case NFS4_SHARE_WANT_WRITE_DELEG:
  704. case NFS4_SHARE_WANT_ANY_DELEG:
  705. case NFS4_SHARE_WANT_NO_DELEG:
  706. case NFS4_SHARE_WANT_CANCEL:
  707. break;
  708. default:
  709. return nfserr_bad_xdr;
  710. }
  711. w &= ~NFS4_SHARE_WANT_MASK;
  712. if (!w)
  713. return nfs_ok;
  714. if (!deleg_when) /* open_downgrade */
  715. return nfserr_inval;
  716. switch (w) {
  717. case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
  718. case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
  719. case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
  720. NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
  721. return nfs_ok;
  722. }
  723. xdr_error:
  724. return nfserr_bad_xdr;
  725. }
  726. static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
  727. {
  728. __be32 *p;
  729. READ_BUF(4);
  730. *x = be32_to_cpup(p++);
  731. /* Note: unlinke access bits, deny bits may be zero. */
  732. if (*x & ~NFS4_SHARE_DENY_BOTH)
  733. return nfserr_bad_xdr;
  734. return nfs_ok;
  735. xdr_error:
  736. return nfserr_bad_xdr;
  737. }
  738. static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
  739. {
  740. __be32 *p;
  741. READ_BUF(4);
  742. o->len = be32_to_cpup(p++);
  743. if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
  744. return nfserr_bad_xdr;
  745. READ_BUF(o->len);
  746. SAVEMEM(o->data, o->len);
  747. return nfs_ok;
  748. xdr_error:
  749. return nfserr_bad_xdr;
  750. }
  751. static __be32
  752. nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
  753. {
  754. DECODE_HEAD;
  755. u32 dummy;
  756. memset(open->op_bmval, 0, sizeof(open->op_bmval));
  757. open->op_iattr.ia_valid = 0;
  758. open->op_openowner = NULL;
  759. open->op_xdr_error = 0;
  760. /* seqid, share_access, share_deny, clientid, ownerlen */
  761. READ_BUF(4);
  762. open->op_seqid = be32_to_cpup(p++);
  763. /* decode, yet ignore deleg_when until supported */
  764. status = nfsd4_decode_share_access(argp, &open->op_share_access,
  765. &open->op_deleg_want, &dummy);
  766. if (status)
  767. goto xdr_error;
  768. status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
  769. if (status)
  770. goto xdr_error;
  771. READ_BUF(sizeof(clientid_t));
  772. COPYMEM(&open->op_clientid, sizeof(clientid_t));
  773. status = nfsd4_decode_opaque(argp, &open->op_owner);
  774. if (status)
  775. goto xdr_error;
  776. READ_BUF(4);
  777. open->op_create = be32_to_cpup(p++);
  778. switch (open->op_create) {
  779. case NFS4_OPEN_NOCREATE:
  780. break;
  781. case NFS4_OPEN_CREATE:
  782. READ_BUF(4);
  783. open->op_createmode = be32_to_cpup(p++);
  784. switch (open->op_createmode) {
  785. case NFS4_CREATE_UNCHECKED:
  786. case NFS4_CREATE_GUARDED:
  787. status = nfsd4_decode_fattr(argp, open->op_bmval,
  788. &open->op_iattr, &open->op_acl, &open->op_label);
  789. if (status)
  790. goto out;
  791. break;
  792. case NFS4_CREATE_EXCLUSIVE:
  793. READ_BUF(NFS4_VERIFIER_SIZE);
  794. COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
  795. break;
  796. case NFS4_CREATE_EXCLUSIVE4_1:
  797. if (argp->minorversion < 1)
  798. goto xdr_error;
  799. READ_BUF(NFS4_VERIFIER_SIZE);
  800. COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
  801. status = nfsd4_decode_fattr(argp, open->op_bmval,
  802. &open->op_iattr, &open->op_acl, &open->op_label);
  803. if (status)
  804. goto out;
  805. break;
  806. default:
  807. goto xdr_error;
  808. }
  809. break;
  810. default:
  811. goto xdr_error;
  812. }
  813. /* open_claim */
  814. READ_BUF(4);
  815. open->op_claim_type = be32_to_cpup(p++);
  816. switch (open->op_claim_type) {
  817. case NFS4_OPEN_CLAIM_NULL:
  818. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  819. READ_BUF(4);
  820. open->op_fname.len = be32_to_cpup(p++);
  821. READ_BUF(open->op_fname.len);
  822. SAVEMEM(open->op_fname.data, open->op_fname.len);
  823. if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
  824. return status;
  825. break;
  826. case NFS4_OPEN_CLAIM_PREVIOUS:
  827. READ_BUF(4);
  828. open->op_delegate_type = be32_to_cpup(p++);
  829. break;
  830. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  831. status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
  832. if (status)
  833. return status;
  834. READ_BUF(4);
  835. open->op_fname.len = be32_to_cpup(p++);
  836. READ_BUF(open->op_fname.len);
  837. SAVEMEM(open->op_fname.data, open->op_fname.len);
  838. if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
  839. return status;
  840. break;
  841. case NFS4_OPEN_CLAIM_FH:
  842. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  843. if (argp->minorversion < 1)
  844. goto xdr_error;
  845. /* void */
  846. break;
  847. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  848. if (argp->minorversion < 1)
  849. goto xdr_error;
  850. status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
  851. if (status)
  852. return status;
  853. break;
  854. default:
  855. goto xdr_error;
  856. }
  857. DECODE_TAIL;
  858. }
  859. static __be32
  860. nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
  861. {
  862. DECODE_HEAD;
  863. if (argp->minorversion >= 1)
  864. return nfserr_notsupp;
  865. status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
  866. if (status)
  867. return status;
  868. READ_BUF(4);
  869. open_conf->oc_seqid = be32_to_cpup(p++);
  870. DECODE_TAIL;
  871. }
  872. static __be32
  873. nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
  874. {
  875. DECODE_HEAD;
  876. status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
  877. if (status)
  878. return status;
  879. READ_BUF(4);
  880. open_down->od_seqid = be32_to_cpup(p++);
  881. status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
  882. &open_down->od_deleg_want, NULL);
  883. if (status)
  884. return status;
  885. status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
  886. if (status)
  887. return status;
  888. DECODE_TAIL;
  889. }
  890. static __be32
  891. nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
  892. {
  893. DECODE_HEAD;
  894. READ_BUF(4);
  895. putfh->pf_fhlen = be32_to_cpup(p++);
  896. if (putfh->pf_fhlen > NFS4_FHSIZE)
  897. goto xdr_error;
  898. READ_BUF(putfh->pf_fhlen);
  899. SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
  900. DECODE_TAIL;
  901. }
  902. static __be32
  903. nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
  904. {
  905. if (argp->minorversion == 0)
  906. return nfs_ok;
  907. return nfserr_notsupp;
  908. }
  909. static __be32
  910. nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
  911. {
  912. DECODE_HEAD;
  913. status = nfsd4_decode_stateid(argp, &read->rd_stateid);
  914. if (status)
  915. return status;
  916. READ_BUF(12);
  917. p = xdr_decode_hyper(p, &read->rd_offset);
  918. read->rd_length = be32_to_cpup(p++);
  919. DECODE_TAIL;
  920. }
  921. static __be32
  922. nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
  923. {
  924. DECODE_HEAD;
  925. READ_BUF(24);
  926. p = xdr_decode_hyper(p, &readdir->rd_cookie);
  927. COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
  928. readdir->rd_dircount = be32_to_cpup(p++);
  929. readdir->rd_maxcount = be32_to_cpup(p++);
  930. if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
  931. goto out;
  932. DECODE_TAIL;
  933. }
  934. static __be32
  935. nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
  936. {
  937. DECODE_HEAD;
  938. READ_BUF(4);
  939. remove->rm_namelen = be32_to_cpup(p++);
  940. READ_BUF(remove->rm_namelen);
  941. SAVEMEM(remove->rm_name, remove->rm_namelen);
  942. if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
  943. return status;
  944. DECODE_TAIL;
  945. }
  946. static __be32
  947. nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
  948. {
  949. DECODE_HEAD;
  950. READ_BUF(4);
  951. rename->rn_snamelen = be32_to_cpup(p++);
  952. READ_BUF(rename->rn_snamelen + 4);
  953. SAVEMEM(rename->rn_sname, rename->rn_snamelen);
  954. rename->rn_tnamelen = be32_to_cpup(p++);
  955. READ_BUF(rename->rn_tnamelen);
  956. SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
  957. if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
  958. return status;
  959. if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
  960. return status;
  961. DECODE_TAIL;
  962. }
  963. static __be32
  964. nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
  965. {
  966. DECODE_HEAD;
  967. if (argp->minorversion >= 1)
  968. return nfserr_notsupp;
  969. READ_BUF(sizeof(clientid_t));
  970. COPYMEM(clientid, sizeof(clientid_t));
  971. DECODE_TAIL;
  972. }
  973. static __be32
  974. nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
  975. struct nfsd4_secinfo *secinfo)
  976. {
  977. DECODE_HEAD;
  978. READ_BUF(4);
  979. secinfo->si_namelen = be32_to_cpup(p++);
  980. READ_BUF(secinfo->si_namelen);
  981. SAVEMEM(secinfo->si_name, secinfo->si_namelen);
  982. status = check_filename(secinfo->si_name, secinfo->si_namelen);
  983. if (status)
  984. return status;
  985. DECODE_TAIL;
  986. }
  987. static __be32
  988. nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
  989. struct nfsd4_secinfo_no_name *sin)
  990. {
  991. DECODE_HEAD;
  992. READ_BUF(4);
  993. sin->sin_style = be32_to_cpup(p++);
  994. DECODE_TAIL;
  995. }
  996. static __be32
  997. nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
  998. {
  999. __be32 status;
  1000. status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
  1001. if (status)
  1002. return status;
  1003. return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
  1004. &setattr->sa_acl, &setattr->sa_label);
  1005. }
  1006. static __be32
  1007. nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
  1008. {
  1009. DECODE_HEAD;
  1010. if (argp->minorversion >= 1)
  1011. return nfserr_notsupp;
  1012. READ_BUF(NFS4_VERIFIER_SIZE);
  1013. COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
  1014. status = nfsd4_decode_opaque(argp, &setclientid->se_name);
  1015. if (status)
  1016. return nfserr_bad_xdr;
  1017. READ_BUF(8);
  1018. setclientid->se_callback_prog = be32_to_cpup(p++);
  1019. setclientid->se_callback_netid_len = be32_to_cpup(p++);
  1020. READ_BUF(setclientid->se_callback_netid_len + 4);
  1021. SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
  1022. setclientid->se_callback_addr_len = be32_to_cpup(p++);
  1023. READ_BUF(setclientid->se_callback_addr_len + 4);
  1024. SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
  1025. setclientid->se_callback_ident = be32_to_cpup(p++);
  1026. DECODE_TAIL;
  1027. }
  1028. static __be32
  1029. nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
  1030. {
  1031. DECODE_HEAD;
  1032. if (argp->minorversion >= 1)
  1033. return nfserr_notsupp;
  1034. READ_BUF(8 + NFS4_VERIFIER_SIZE);
  1035. COPYMEM(&scd_c->sc_clientid, 8);
  1036. COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
  1037. DECODE_TAIL;
  1038. }
  1039. /* Also used for NVERIFY */
  1040. static __be32
  1041. nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
  1042. {
  1043. DECODE_HEAD;
  1044. if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
  1045. goto out;
  1046. /* For convenience's sake, we compare raw xdr'd attributes in
  1047. * nfsd4_proc_verify */
  1048. READ_BUF(4);
  1049. verify->ve_attrlen = be32_to_cpup(p++);
  1050. READ_BUF(verify->ve_attrlen);
  1051. SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
  1052. DECODE_TAIL;
  1053. }
  1054. static __be32
  1055. nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
  1056. {
  1057. int avail;
  1058. int len;
  1059. DECODE_HEAD;
  1060. status = nfsd4_decode_stateid(argp, &write->wr_stateid);
  1061. if (status)
  1062. return status;
  1063. READ_BUF(16);
  1064. p = xdr_decode_hyper(p, &write->wr_offset);
  1065. write->wr_stable_how = be32_to_cpup(p++);
  1066. if (write->wr_stable_how > 2)
  1067. goto xdr_error;
  1068. write->wr_buflen = be32_to_cpup(p++);
  1069. /* Sorry .. no magic macros for this.. *
  1070. * READ_BUF(write->wr_buflen);
  1071. * SAVEMEM(write->wr_buf, write->wr_buflen);
  1072. */
  1073. avail = (char*)argp->end - (char*)argp->p;
  1074. if (avail + argp->pagelen < write->wr_buflen) {
  1075. dprintk("NFSD: xdr error (%s:%d)\n",
  1076. __FILE__, __LINE__);
  1077. goto xdr_error;
  1078. }
  1079. write->wr_head.iov_base = p;
  1080. write->wr_head.iov_len = avail;
  1081. write->wr_pagelist = argp->pagelist;
  1082. len = XDR_QUADLEN(write->wr_buflen) << 2;
  1083. if (len >= avail) {
  1084. int pages;
  1085. len -= avail;
  1086. pages = len >> PAGE_SHIFT;
  1087. argp->pagelist += pages;
  1088. argp->pagelen -= pages * PAGE_SIZE;
  1089. len -= pages * PAGE_SIZE;
  1090. argp->p = (__be32 *)page_address(argp->pagelist[0]);
  1091. argp->pagelist++;
  1092. argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
  1093. }
  1094. argp->p += XDR_QUADLEN(len);
  1095. DECODE_TAIL;
  1096. }
  1097. static __be32
  1098. nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
  1099. {
  1100. DECODE_HEAD;
  1101. if (argp->minorversion >= 1)
  1102. return nfserr_notsupp;
  1103. READ_BUF(12);
  1104. COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
  1105. rlockowner->rl_owner.len = be32_to_cpup(p++);
  1106. READ_BUF(rlockowner->rl_owner.len);
  1107. READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
  1108. if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
  1109. return nfserr_inval;
  1110. DECODE_TAIL;
  1111. }
  1112. static __be32
  1113. nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
  1114. struct nfsd4_exchange_id *exid)
  1115. {
  1116. int dummy, tmp;
  1117. DECODE_HEAD;
  1118. READ_BUF(NFS4_VERIFIER_SIZE);
  1119. COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
  1120. status = nfsd4_decode_opaque(argp, &exid->clname);
  1121. if (status)
  1122. return nfserr_bad_xdr;
  1123. READ_BUF(4);
  1124. exid->flags = be32_to_cpup(p++);
  1125. /* Ignore state_protect4_a */
  1126. READ_BUF(4);
  1127. exid->spa_how = be32_to_cpup(p++);
  1128. switch (exid->spa_how) {
  1129. case SP4_NONE:
  1130. break;
  1131. case SP4_MACH_CRED:
  1132. /* spo_must_enforce */
  1133. READ_BUF(4);
  1134. dummy = be32_to_cpup(p++);
  1135. READ_BUF(dummy * 4);
  1136. p += dummy;
  1137. /* spo_must_allow */
  1138. READ_BUF(4);
  1139. dummy = be32_to_cpup(p++);
  1140. READ_BUF(dummy * 4);
  1141. p += dummy;
  1142. break;
  1143. case SP4_SSV:
  1144. /* ssp_ops */
  1145. READ_BUF(4);
  1146. dummy = be32_to_cpup(p++);
  1147. READ_BUF(dummy * 4);
  1148. p += dummy;
  1149. READ_BUF(4);
  1150. dummy = be32_to_cpup(p++);
  1151. READ_BUF(dummy * 4);
  1152. p += dummy;
  1153. /* ssp_hash_algs<> */
  1154. READ_BUF(4);
  1155. tmp = be32_to_cpup(p++);
  1156. while (tmp--) {
  1157. READ_BUF(4);
  1158. dummy = be32_to_cpup(p++);
  1159. READ_BUF(dummy);
  1160. p += XDR_QUADLEN(dummy);
  1161. }
  1162. /* ssp_encr_algs<> */
  1163. READ_BUF(4);
  1164. tmp = be32_to_cpup(p++);
  1165. while (tmp--) {
  1166. READ_BUF(4);
  1167. dummy = be32_to_cpup(p++);
  1168. READ_BUF(dummy);
  1169. p += XDR_QUADLEN(dummy);
  1170. }
  1171. /* ssp_window and ssp_num_gss_handles */
  1172. READ_BUF(8);
  1173. dummy = be32_to_cpup(p++);
  1174. dummy = be32_to_cpup(p++);
  1175. break;
  1176. default:
  1177. goto xdr_error;
  1178. }
  1179. /* Ignore Implementation ID */
  1180. READ_BUF(4); /* nfs_impl_id4 array length */
  1181. dummy = be32_to_cpup(p++);
  1182. if (dummy > 1)
  1183. goto xdr_error;
  1184. if (dummy == 1) {
  1185. /* nii_domain */
  1186. READ_BUF(4);
  1187. dummy = be32_to_cpup(p++);
  1188. READ_BUF(dummy);
  1189. p += XDR_QUADLEN(dummy);
  1190. /* nii_name */
  1191. READ_BUF(4);
  1192. dummy = be32_to_cpup(p++);
  1193. READ_BUF(dummy);
  1194. p += XDR_QUADLEN(dummy);
  1195. /* nii_date */
  1196. READ_BUF(12);
  1197. p += 3;
  1198. }
  1199. DECODE_TAIL;
  1200. }
  1201. static __be32
  1202. nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
  1203. struct nfsd4_create_session *sess)
  1204. {
  1205. DECODE_HEAD;
  1206. u32 dummy;
  1207. READ_BUF(16);
  1208. COPYMEM(&sess->clientid, 8);
  1209. sess->seqid = be32_to_cpup(p++);
  1210. sess->flags = be32_to_cpup(p++);
  1211. /* Fore channel attrs */
  1212. READ_BUF(28);
  1213. dummy = be32_to_cpup(p++); /* headerpadsz is always 0 */
  1214. sess->fore_channel.maxreq_sz = be32_to_cpup(p++);
  1215. sess->fore_channel.maxresp_sz = be32_to_cpup(p++);
  1216. sess->fore_channel.maxresp_cached = be32_to_cpup(p++);
  1217. sess->fore_channel.maxops = be32_to_cpup(p++);
  1218. sess->fore_channel.maxreqs = be32_to_cpup(p++);
  1219. sess->fore_channel.nr_rdma_attrs = be32_to_cpup(p++);
  1220. if (sess->fore_channel.nr_rdma_attrs == 1) {
  1221. READ_BUF(4);
  1222. sess->fore_channel.rdma_attrs = be32_to_cpup(p++);
  1223. } else if (sess->fore_channel.nr_rdma_attrs > 1) {
  1224. dprintk("Too many fore channel attr bitmaps!\n");
  1225. goto xdr_error;
  1226. }
  1227. /* Back channel attrs */
  1228. READ_BUF(28);
  1229. dummy = be32_to_cpup(p++); /* headerpadsz is always 0 */
  1230. sess->back_channel.maxreq_sz = be32_to_cpup(p++);
  1231. sess->back_channel.maxresp_sz = be32_to_cpup(p++);
  1232. sess->back_channel.maxresp_cached = be32_to_cpup(p++);
  1233. sess->back_channel.maxops = be32_to_cpup(p++);
  1234. sess->back_channel.maxreqs = be32_to_cpup(p++);
  1235. sess->back_channel.nr_rdma_attrs = be32_to_cpup(p++);
  1236. if (sess->back_channel.nr_rdma_attrs == 1) {
  1237. READ_BUF(4);
  1238. sess->back_channel.rdma_attrs = be32_to_cpup(p++);
  1239. } else if (sess->back_channel.nr_rdma_attrs > 1) {
  1240. dprintk("Too many back channel attr bitmaps!\n");
  1241. goto xdr_error;
  1242. }
  1243. READ_BUF(4);
  1244. sess->callback_prog = be32_to_cpup(p++);
  1245. nfsd4_decode_cb_sec(argp, &sess->cb_sec);
  1246. DECODE_TAIL;
  1247. }
  1248. static __be32
  1249. nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
  1250. struct nfsd4_destroy_session *destroy_session)
  1251. {
  1252. DECODE_HEAD;
  1253. READ_BUF(NFS4_MAX_SESSIONID_LEN);
  1254. COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1255. DECODE_TAIL;
  1256. }
  1257. static __be32
  1258. nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
  1259. struct nfsd4_free_stateid *free_stateid)
  1260. {
  1261. DECODE_HEAD;
  1262. READ_BUF(sizeof(stateid_t));
  1263. free_stateid->fr_stateid.si_generation = be32_to_cpup(p++);
  1264. COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
  1265. DECODE_TAIL;
  1266. }
  1267. static __be32
  1268. nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
  1269. struct nfsd4_sequence *seq)
  1270. {
  1271. DECODE_HEAD;
  1272. READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
  1273. COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1274. seq->seqid = be32_to_cpup(p++);
  1275. seq->slotid = be32_to_cpup(p++);
  1276. seq->maxslots = be32_to_cpup(p++);
  1277. seq->cachethis = be32_to_cpup(p++);
  1278. DECODE_TAIL;
  1279. }
  1280. static __be32
  1281. nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
  1282. {
  1283. int i;
  1284. __be32 *p, status;
  1285. struct nfsd4_test_stateid_id *stateid;
  1286. READ_BUF(4);
  1287. test_stateid->ts_num_ids = ntohl(*p++);
  1288. INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
  1289. for (i = 0; i < test_stateid->ts_num_ids; i++) {
  1290. stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
  1291. if (!stateid) {
  1292. status = nfserrno(-ENOMEM);
  1293. goto out;
  1294. }
  1295. INIT_LIST_HEAD(&stateid->ts_id_list);
  1296. list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
  1297. status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
  1298. if (status)
  1299. goto out;
  1300. }
  1301. status = 0;
  1302. out:
  1303. return status;
  1304. xdr_error:
  1305. dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
  1306. status = nfserr_bad_xdr;
  1307. goto out;
  1308. }
  1309. static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
  1310. {
  1311. DECODE_HEAD;
  1312. READ_BUF(8);
  1313. COPYMEM(&dc->clientid, 8);
  1314. DECODE_TAIL;
  1315. }
  1316. static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
  1317. {
  1318. DECODE_HEAD;
  1319. READ_BUF(4);
  1320. rc->rca_one_fs = be32_to_cpup(p++);
  1321. DECODE_TAIL;
  1322. }
  1323. #ifdef CONFIG_NFSD_PNFS
  1324. static __be32
  1325. nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
  1326. struct nfsd4_getdeviceinfo *gdev)
  1327. {
  1328. DECODE_HEAD;
  1329. u32 num, i;
  1330. READ_BUF(sizeof(struct nfsd4_deviceid) + 3 * 4);
  1331. COPYMEM(&gdev->gd_devid, sizeof(struct nfsd4_deviceid));
  1332. gdev->gd_layout_type = be32_to_cpup(p++);
  1333. gdev->gd_maxcount = be32_to_cpup(p++);
  1334. num = be32_to_cpup(p++);
  1335. if (num) {
  1336. READ_BUF(4 * num);
  1337. gdev->gd_notify_types = be32_to_cpup(p++);
  1338. for (i = 1; i < num; i++) {
  1339. if (be32_to_cpup(p++)) {
  1340. status = nfserr_inval;
  1341. goto out;
  1342. }
  1343. }
  1344. }
  1345. DECODE_TAIL;
  1346. }
  1347. static __be32
  1348. nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
  1349. struct nfsd4_layoutget *lgp)
  1350. {
  1351. DECODE_HEAD;
  1352. READ_BUF(36);
  1353. lgp->lg_signal = be32_to_cpup(p++);
  1354. lgp->lg_layout_type = be32_to_cpup(p++);
  1355. lgp->lg_seg.iomode = be32_to_cpup(p++);
  1356. p = xdr_decode_hyper(p, &lgp->lg_seg.offset);
  1357. p = xdr_decode_hyper(p, &lgp->lg_seg.length);
  1358. p = xdr_decode_hyper(p, &lgp->lg_minlength);
  1359. nfsd4_decode_stateid(argp, &lgp->lg_sid);
  1360. READ_BUF(4);
  1361. lgp->lg_maxcount = be32_to_cpup(p++);
  1362. DECODE_TAIL;
  1363. }
  1364. static __be32
  1365. nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
  1366. struct nfsd4_layoutcommit *lcp)
  1367. {
  1368. DECODE_HEAD;
  1369. u32 timechange;
  1370. READ_BUF(20);
  1371. p = xdr_decode_hyper(p, &lcp->lc_seg.offset);
  1372. p = xdr_decode_hyper(p, &lcp->lc_seg.length);
  1373. lcp->lc_reclaim = be32_to_cpup(p++);
  1374. nfsd4_decode_stateid(argp, &lcp->lc_sid);
  1375. READ_BUF(4);
  1376. lcp->lc_newoffset = be32_to_cpup(p++);
  1377. if (lcp->lc_newoffset) {
  1378. READ_BUF(8);
  1379. p = xdr_decode_hyper(p, &lcp->lc_last_wr);
  1380. } else
  1381. lcp->lc_last_wr = 0;
  1382. READ_BUF(4);
  1383. timechange = be32_to_cpup(p++);
  1384. if (timechange) {
  1385. status = nfsd4_decode_time(argp, &lcp->lc_mtime);
  1386. if (status)
  1387. return status;
  1388. } else {
  1389. lcp->lc_mtime.tv_nsec = UTIME_NOW;
  1390. }
  1391. READ_BUF(8);
  1392. lcp->lc_layout_type = be32_to_cpup(p++);
  1393. /*
  1394. * Save the layout update in XDR format and let the layout driver deal
  1395. * with it later.
  1396. */
  1397. lcp->lc_up_len = be32_to_cpup(p++);
  1398. if (lcp->lc_up_len > 0) {
  1399. READ_BUF(lcp->lc_up_len);
  1400. READMEM(lcp->lc_up_layout, lcp->lc_up_len);
  1401. }
  1402. DECODE_TAIL;
  1403. }
  1404. static __be32
  1405. nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
  1406. struct nfsd4_layoutreturn *lrp)
  1407. {
  1408. DECODE_HEAD;
  1409. READ_BUF(16);
  1410. lrp->lr_reclaim = be32_to_cpup(p++);
  1411. lrp->lr_layout_type = be32_to_cpup(p++);
  1412. lrp->lr_seg.iomode = be32_to_cpup(p++);
  1413. lrp->lr_return_type = be32_to_cpup(p++);
  1414. if (lrp->lr_return_type == RETURN_FILE) {
  1415. READ_BUF(16);
  1416. p = xdr_decode_hyper(p, &lrp->lr_seg.offset);
  1417. p = xdr_decode_hyper(p, &lrp->lr_seg.length);
  1418. nfsd4_decode_stateid(argp, &lrp->lr_sid);
  1419. READ_BUF(4);
  1420. lrp->lrf_body_len = be32_to_cpup(p++);
  1421. if (lrp->lrf_body_len > 0) {
  1422. READ_BUF(lrp->lrf_body_len);
  1423. READMEM(lrp->lrf_body, lrp->lrf_body_len);
  1424. }
  1425. } else {
  1426. lrp->lr_seg.offset = 0;
  1427. lrp->lr_seg.length = NFS4_MAX_UINT64;
  1428. }
  1429. DECODE_TAIL;
  1430. }
  1431. #endif /* CONFIG_NFSD_PNFS */
  1432. static __be32
  1433. nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
  1434. struct nfsd4_fallocate *fallocate)
  1435. {
  1436. DECODE_HEAD;
  1437. status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
  1438. if (status)
  1439. return status;
  1440. READ_BUF(16);
  1441. p = xdr_decode_hyper(p, &fallocate->falloc_offset);
  1442. xdr_decode_hyper(p, &fallocate->falloc_length);
  1443. DECODE_TAIL;
  1444. }
  1445. static __be32
  1446. nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
  1447. {
  1448. DECODE_HEAD;
  1449. status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
  1450. if (status)
  1451. return status;
  1452. READ_BUF(8 + 4);
  1453. p = xdr_decode_hyper(p, &seek->seek_offset);
  1454. seek->seek_whence = be32_to_cpup(p);
  1455. DECODE_TAIL;
  1456. }
  1457. static __be32
  1458. nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
  1459. {
  1460. return nfs_ok;
  1461. }
  1462. static __be32
  1463. nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
  1464. {
  1465. return nfserr_notsupp;
  1466. }
  1467. typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
  1468. static nfsd4_dec nfsd4_dec_ops[] = {
  1469. [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
  1470. [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
  1471. [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
  1472. [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
  1473. [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1474. [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
  1475. [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
  1476. [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
  1477. [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
  1478. [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
  1479. [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
  1480. [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
  1481. [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
  1482. [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
  1483. [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1484. [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
  1485. [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1486. [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
  1487. [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
  1488. [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
  1489. [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
  1490. [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
  1491. [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
  1492. [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
  1493. [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
  1494. [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
  1495. [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
  1496. [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
  1497. [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
  1498. [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
  1499. [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
  1500. [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
  1501. [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
  1502. [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
  1503. [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1504. [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
  1505. [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
  1506. /* new operations for NFSv4.1 */
  1507. [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
  1508. [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
  1509. [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
  1510. [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
  1511. [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
  1512. [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
  1513. [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1514. #ifdef CONFIG_NFSD_PNFS
  1515. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
  1516. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  1517. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
  1518. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
  1519. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
  1520. #else
  1521. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
  1522. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  1523. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
  1524. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
  1525. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
  1526. #endif
  1527. [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
  1528. [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
  1529. [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
  1530. [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
  1531. [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1532. [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
  1533. [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
  1534. /* new operations for NFSv4.2 */
  1535. [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
  1536. [OP_COPY] = (nfsd4_dec)nfsd4_decode_notsupp,
  1537. [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_notsupp,
  1538. [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
  1539. [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1540. [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1541. [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1542. [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_notsupp,
  1543. [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1544. [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1545. [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
  1546. [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
  1547. };
  1548. static inline bool
  1549. nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
  1550. {
  1551. if (op->opnum < FIRST_NFS4_OP)
  1552. return false;
  1553. else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
  1554. return false;
  1555. else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
  1556. return false;
  1557. else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
  1558. return false;
  1559. return true;
  1560. }
  1561. static __be32
  1562. nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
  1563. {
  1564. DECODE_HEAD;
  1565. struct nfsd4_op *op;
  1566. bool cachethis = false;
  1567. int auth_slack= argp->rqstp->rq_auth_slack;
  1568. int max_reply = auth_slack + 8; /* opcnt, status */
  1569. int readcount = 0;
  1570. int readbytes = 0;
  1571. int i;
  1572. READ_BUF(4);
  1573. argp->taglen = be32_to_cpup(p++);
  1574. READ_BUF(argp->taglen + 8);
  1575. SAVEMEM(argp->tag, argp->taglen);
  1576. argp->minorversion = be32_to_cpup(p++);
  1577. argp->opcnt = be32_to_cpup(p++);
  1578. max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
  1579. if (argp->taglen > NFSD4_MAX_TAGLEN)
  1580. goto xdr_error;
  1581. if (argp->opcnt > 100)
  1582. goto xdr_error;
  1583. if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
  1584. argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
  1585. if (!argp->ops) {
  1586. argp->ops = argp->iops;
  1587. dprintk("nfsd: couldn't allocate room for COMPOUND\n");
  1588. goto xdr_error;
  1589. }
  1590. }
  1591. if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
  1592. argp->opcnt = 0;
  1593. for (i = 0; i < argp->opcnt; i++) {
  1594. op = &argp->ops[i];
  1595. op->replay = NULL;
  1596. READ_BUF(4);
  1597. op->opnum = be32_to_cpup(p++);
  1598. if (nfsd4_opnum_in_range(argp, op))
  1599. op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
  1600. else {
  1601. op->opnum = OP_ILLEGAL;
  1602. op->status = nfserr_op_illegal;
  1603. }
  1604. /*
  1605. * We'll try to cache the result in the DRC if any one
  1606. * op in the compound wants to be cached:
  1607. */
  1608. cachethis |= nfsd4_cache_this_op(op);
  1609. if (op->opnum == OP_READ) {
  1610. readcount++;
  1611. readbytes += nfsd4_max_reply(argp->rqstp, op);
  1612. } else
  1613. max_reply += nfsd4_max_reply(argp->rqstp, op);
  1614. /*
  1615. * OP_LOCK may return a conflicting lock. (Special case
  1616. * because it will just skip encoding this if it runs
  1617. * out of xdr buffer space, and it is the only operation
  1618. * that behaves this way.)
  1619. */
  1620. if (op->opnum == OP_LOCK)
  1621. max_reply += NFS4_OPAQUE_LIMIT;
  1622. if (op->status) {
  1623. argp->opcnt = i+1;
  1624. break;
  1625. }
  1626. }
  1627. /* Sessions make the DRC unnecessary: */
  1628. if (argp->minorversion)
  1629. cachethis = false;
  1630. svc_reserve(argp->rqstp, max_reply + readbytes);
  1631. argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
  1632. if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
  1633. clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
  1634. DECODE_TAIL;
  1635. }
  1636. static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode)
  1637. {
  1638. if (IS_I_VERSION(inode)) {
  1639. p = xdr_encode_hyper(p, inode->i_version);
  1640. } else {
  1641. *p++ = cpu_to_be32(stat->ctime.tv_sec);
  1642. *p++ = cpu_to_be32(stat->ctime.tv_nsec);
  1643. }
  1644. return p;
  1645. }
  1646. static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
  1647. {
  1648. *p++ = cpu_to_be32(c->atomic);
  1649. if (c->change_supported) {
  1650. p = xdr_encode_hyper(p, c->before_change);
  1651. p = xdr_encode_hyper(p, c->after_change);
  1652. } else {
  1653. *p++ = cpu_to_be32(c->before_ctime_sec);
  1654. *p++ = cpu_to_be32(c->before_ctime_nsec);
  1655. *p++ = cpu_to_be32(c->after_ctime_sec);
  1656. *p++ = cpu_to_be32(c->after_ctime_nsec);
  1657. }
  1658. return p;
  1659. }
  1660. /* Encode as an array of strings the string given with components
  1661. * separated @sep, escaped with esc_enter and esc_exit.
  1662. */
  1663. static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
  1664. char *components, char esc_enter,
  1665. char esc_exit)
  1666. {
  1667. __be32 *p;
  1668. __be32 pathlen;
  1669. int pathlen_offset;
  1670. int strlen, count=0;
  1671. char *str, *end, *next;
  1672. dprintk("nfsd4_encode_components(%s)\n", components);
  1673. pathlen_offset = xdr->buf->len;
  1674. p = xdr_reserve_space(xdr, 4);
  1675. if (!p)
  1676. return nfserr_resource;
  1677. p++; /* We will fill this in with @count later */
  1678. end = str = components;
  1679. while (*end) {
  1680. bool found_esc = false;
  1681. /* try to parse as esc_start, ..., esc_end, sep */
  1682. if (*str == esc_enter) {
  1683. for (; *end && (*end != esc_exit); end++)
  1684. /* find esc_exit or end of string */;
  1685. next = end + 1;
  1686. if (*end && (!*next || *next == sep)) {
  1687. str++;
  1688. found_esc = true;
  1689. }
  1690. }
  1691. if (!found_esc)
  1692. for (; *end && (*end != sep); end++)
  1693. /* find sep or end of string */;
  1694. strlen = end - str;
  1695. if (strlen) {
  1696. p = xdr_reserve_space(xdr, strlen + 4);
  1697. if (!p)
  1698. return nfserr_resource;
  1699. p = xdr_encode_opaque(p, str, strlen);
  1700. count++;
  1701. }
  1702. else
  1703. end++;
  1704. if (found_esc)
  1705. end = next;
  1706. str = end;
  1707. }
  1708. pathlen = htonl(count);
  1709. write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
  1710. return 0;
  1711. }
  1712. /* Encode as an array of strings the string given with components
  1713. * separated @sep.
  1714. */
  1715. static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
  1716. char *components)
  1717. {
  1718. return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
  1719. }
  1720. /*
  1721. * encode a location element of a fs_locations structure
  1722. */
  1723. static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
  1724. struct nfsd4_fs_location *location)
  1725. {
  1726. __be32 status;
  1727. status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
  1728. '[', ']');
  1729. if (status)
  1730. return status;
  1731. status = nfsd4_encode_components(xdr, '/', location->path);
  1732. if (status)
  1733. return status;
  1734. return 0;
  1735. }
  1736. /*
  1737. * Encode a path in RFC3530 'pathname4' format
  1738. */
  1739. static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
  1740. const struct path *root,
  1741. const struct path *path)
  1742. {
  1743. struct path cur = *path;
  1744. __be32 *p;
  1745. struct dentry **components = NULL;
  1746. unsigned int ncomponents = 0;
  1747. __be32 err = nfserr_jukebox;
  1748. dprintk("nfsd4_encode_components(");
  1749. path_get(&cur);
  1750. /* First walk the path up to the nfsd root, and store the
  1751. * dentries/path components in an array.
  1752. */
  1753. for (;;) {
  1754. if (cur.dentry == root->dentry && cur.mnt == root->mnt)
  1755. break;
  1756. if (cur.dentry == cur.mnt->mnt_root) {
  1757. if (follow_up(&cur))
  1758. continue;
  1759. goto out_free;
  1760. }
  1761. if ((ncomponents & 15) == 0) {
  1762. struct dentry **new;
  1763. new = krealloc(components,
  1764. sizeof(*new) * (ncomponents + 16),
  1765. GFP_KERNEL);
  1766. if (!new)
  1767. goto out_free;
  1768. components = new;
  1769. }
  1770. components[ncomponents++] = cur.dentry;
  1771. cur.dentry = dget_parent(cur.dentry);
  1772. }
  1773. err = nfserr_resource;
  1774. p = xdr_reserve_space(xdr, 4);
  1775. if (!p)
  1776. goto out_free;
  1777. *p++ = cpu_to_be32(ncomponents);
  1778. while (ncomponents) {
  1779. struct dentry *dentry = components[ncomponents - 1];
  1780. unsigned int len;
  1781. spin_lock(&dentry->d_lock);
  1782. len = dentry->d_name.len;
  1783. p = xdr_reserve_space(xdr, len + 4);
  1784. if (!p) {
  1785. spin_unlock(&dentry->d_lock);
  1786. goto out_free;
  1787. }
  1788. p = xdr_encode_opaque(p, dentry->d_name.name, len);
  1789. dprintk("/%pd", dentry);
  1790. spin_unlock(&dentry->d_lock);
  1791. dput(dentry);
  1792. ncomponents--;
  1793. }
  1794. err = 0;
  1795. out_free:
  1796. dprintk(")\n");
  1797. while (ncomponents)
  1798. dput(components[--ncomponents]);
  1799. kfree(components);
  1800. path_put(&cur);
  1801. return err;
  1802. }
  1803. static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
  1804. struct svc_rqst *rqstp, const struct path *path)
  1805. {
  1806. struct svc_export *exp_ps;
  1807. __be32 res;
  1808. exp_ps = rqst_find_fsidzero_export(rqstp);
  1809. if (IS_ERR(exp_ps))
  1810. return nfserrno(PTR_ERR(exp_ps));
  1811. res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
  1812. exp_put(exp_ps);
  1813. return res;
  1814. }
  1815. /*
  1816. * encode a fs_locations structure
  1817. */
  1818. static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
  1819. struct svc_rqst *rqstp, struct svc_export *exp)
  1820. {
  1821. __be32 status;
  1822. int i;
  1823. __be32 *p;
  1824. struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
  1825. status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
  1826. if (status)
  1827. return status;
  1828. p = xdr_reserve_space(xdr, 4);
  1829. if (!p)
  1830. return nfserr_resource;
  1831. *p++ = cpu_to_be32(fslocs->locations_count);
  1832. for (i=0; i<fslocs->locations_count; i++) {
  1833. status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
  1834. if (status)
  1835. return status;
  1836. }
  1837. return 0;
  1838. }
  1839. static u32 nfs4_file_type(umode_t mode)
  1840. {
  1841. switch (mode & S_IFMT) {
  1842. case S_IFIFO: return NF4FIFO;
  1843. case S_IFCHR: return NF4CHR;
  1844. case S_IFDIR: return NF4DIR;
  1845. case S_IFBLK: return NF4BLK;
  1846. case S_IFLNK: return NF4LNK;
  1847. case S_IFREG: return NF4REG;
  1848. case S_IFSOCK: return NF4SOCK;
  1849. default: return NF4BAD;
  1850. };
  1851. }
  1852. static inline __be32
  1853. nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  1854. struct nfs4_ace *ace)
  1855. {
  1856. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  1857. return nfs4_acl_write_who(xdr, ace->whotype);
  1858. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  1859. return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
  1860. else
  1861. return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
  1862. }
  1863. #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
  1864. FATTR4_WORD0_RDATTR_ERROR)
  1865. #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
  1866. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  1867. static inline __be32
  1868. nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  1869. void *context, int len)
  1870. {
  1871. __be32 *p;
  1872. p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
  1873. if (!p)
  1874. return nfserr_resource;
  1875. /*
  1876. * For now we use a 0 here to indicate the null translation; in
  1877. * the future we may place a call to translation code here.
  1878. */
  1879. *p++ = cpu_to_be32(0); /* lfs */
  1880. *p++ = cpu_to_be32(0); /* pi */
  1881. p = xdr_encode_opaque(p, context, len);
  1882. return 0;
  1883. }
  1884. #else
  1885. static inline __be32
  1886. nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  1887. void *context, int len)
  1888. { return 0; }
  1889. #endif
  1890. static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
  1891. {
  1892. /* As per referral draft: */
  1893. if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
  1894. *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
  1895. if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
  1896. *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
  1897. *rdattr_err = NFSERR_MOVED;
  1898. else
  1899. return nfserr_moved;
  1900. }
  1901. *bmval0 &= WORD0_ABSENT_FS_ATTRS;
  1902. *bmval1 &= WORD1_ABSENT_FS_ATTRS;
  1903. return 0;
  1904. }
  1905. static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
  1906. {
  1907. struct path path = exp->ex_path;
  1908. int err;
  1909. path_get(&path);
  1910. while (follow_up(&path)) {
  1911. if (path.dentry != path.mnt->mnt_root)
  1912. break;
  1913. }
  1914. err = vfs_getattr(&path, stat);
  1915. path_put(&path);
  1916. return err;
  1917. }
  1918. /*
  1919. * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
  1920. * ourselves.
  1921. */
  1922. static __be32
  1923. nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
  1924. struct svc_export *exp,
  1925. struct dentry *dentry, u32 *bmval,
  1926. struct svc_rqst *rqstp, int ignore_crossmnt)
  1927. {
  1928. u32 bmval0 = bmval[0];
  1929. u32 bmval1 = bmval[1];
  1930. u32 bmval2 = bmval[2];
  1931. struct kstat stat;
  1932. struct svc_fh *tempfh = NULL;
  1933. struct kstatfs statfs;
  1934. __be32 *p;
  1935. int starting_len = xdr->buf->len;
  1936. int attrlen_offset;
  1937. __be32 attrlen;
  1938. u32 dummy;
  1939. u64 dummy64;
  1940. u32 rdattr_err = 0;
  1941. __be32 status;
  1942. int err;
  1943. int aclsupport = 0;
  1944. struct nfs4_acl *acl = NULL;
  1945. void *context = NULL;
  1946. int contextlen;
  1947. bool contextsupport = false;
  1948. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1949. u32 minorversion = resp->cstate.minorversion;
  1950. struct path path = {
  1951. .mnt = exp->ex_path.mnt,
  1952. .dentry = dentry,
  1953. };
  1954. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1955. BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
  1956. BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
  1957. BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
  1958. BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
  1959. if (exp->ex_fslocs.migrated) {
  1960. BUG_ON(bmval[2]);
  1961. status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
  1962. if (status)
  1963. goto out;
  1964. }
  1965. err = vfs_getattr(&path, &stat);
  1966. if (err)
  1967. goto out_nfserr;
  1968. if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
  1969. FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
  1970. (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
  1971. FATTR4_WORD1_SPACE_TOTAL))) {
  1972. err = vfs_statfs(&path, &statfs);
  1973. if (err)
  1974. goto out_nfserr;
  1975. }
  1976. if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
  1977. tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
  1978. status = nfserr_jukebox;
  1979. if (!tempfh)
  1980. goto out;
  1981. fh_init(tempfh, NFS4_FHSIZE);
  1982. status = fh_compose(tempfh, exp, dentry, NULL);
  1983. if (status)
  1984. goto out;
  1985. fhp = tempfh;
  1986. }
  1987. if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
  1988. | FATTR4_WORD0_SUPPORTED_ATTRS)) {
  1989. err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
  1990. aclsupport = (err == 0);
  1991. if (bmval0 & FATTR4_WORD0_ACL) {
  1992. if (err == -EOPNOTSUPP)
  1993. bmval0 &= ~FATTR4_WORD0_ACL;
  1994. else if (err == -EINVAL) {
  1995. status = nfserr_attrnotsupp;
  1996. goto out;
  1997. } else if (err != 0)
  1998. goto out_nfserr;
  1999. }
  2000. }
  2001. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2002. if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
  2003. bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
  2004. err = security_inode_getsecctx(dentry->d_inode,
  2005. &context, &contextlen);
  2006. contextsupport = (err == 0);
  2007. if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
  2008. if (err == -EOPNOTSUPP)
  2009. bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  2010. else if (err)
  2011. goto out_nfserr;
  2012. }
  2013. }
  2014. #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
  2015. if (bmval2) {
  2016. p = xdr_reserve_space(xdr, 16);
  2017. if (!p)
  2018. goto out_resource;
  2019. *p++ = cpu_to_be32(3);
  2020. *p++ = cpu_to_be32(bmval0);
  2021. *p++ = cpu_to_be32(bmval1);
  2022. *p++ = cpu_to_be32(bmval2);
  2023. } else if (bmval1) {
  2024. p = xdr_reserve_space(xdr, 12);
  2025. if (!p)
  2026. goto out_resource;
  2027. *p++ = cpu_to_be32(2);
  2028. *p++ = cpu_to_be32(bmval0);
  2029. *p++ = cpu_to_be32(bmval1);
  2030. } else {
  2031. p = xdr_reserve_space(xdr, 8);
  2032. if (!p)
  2033. goto out_resource;
  2034. *p++ = cpu_to_be32(1);
  2035. *p++ = cpu_to_be32(bmval0);
  2036. }
  2037. attrlen_offset = xdr->buf->len;
  2038. p = xdr_reserve_space(xdr, 4);
  2039. if (!p)
  2040. goto out_resource;
  2041. p++; /* to be backfilled later */
  2042. if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  2043. u32 word0 = nfsd_suppattrs0(minorversion);
  2044. u32 word1 = nfsd_suppattrs1(minorversion);
  2045. u32 word2 = nfsd_suppattrs2(minorversion);
  2046. if (!aclsupport)
  2047. word0 &= ~FATTR4_WORD0_ACL;
  2048. if (!contextsupport)
  2049. word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  2050. if (!word2) {
  2051. p = xdr_reserve_space(xdr, 12);
  2052. if (!p)
  2053. goto out_resource;
  2054. *p++ = cpu_to_be32(2);
  2055. *p++ = cpu_to_be32(word0);
  2056. *p++ = cpu_to_be32(word1);
  2057. } else {
  2058. p = xdr_reserve_space(xdr, 16);
  2059. if (!p)
  2060. goto out_resource;
  2061. *p++ = cpu_to_be32(3);
  2062. *p++ = cpu_to_be32(word0);
  2063. *p++ = cpu_to_be32(word1);
  2064. *p++ = cpu_to_be32(word2);
  2065. }
  2066. }
  2067. if (bmval0 & FATTR4_WORD0_TYPE) {
  2068. p = xdr_reserve_space(xdr, 4);
  2069. if (!p)
  2070. goto out_resource;
  2071. dummy = nfs4_file_type(stat.mode);
  2072. if (dummy == NF4BAD) {
  2073. status = nfserr_serverfault;
  2074. goto out;
  2075. }
  2076. *p++ = cpu_to_be32(dummy);
  2077. }
  2078. if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
  2079. p = xdr_reserve_space(xdr, 4);
  2080. if (!p)
  2081. goto out_resource;
  2082. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  2083. *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
  2084. else
  2085. *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
  2086. NFS4_FH_VOL_RENAME);
  2087. }
  2088. if (bmval0 & FATTR4_WORD0_CHANGE) {
  2089. p = xdr_reserve_space(xdr, 8);
  2090. if (!p)
  2091. goto out_resource;
  2092. p = encode_change(p, &stat, dentry->d_inode);
  2093. }
  2094. if (bmval0 & FATTR4_WORD0_SIZE) {
  2095. p = xdr_reserve_space(xdr, 8);
  2096. if (!p)
  2097. goto out_resource;
  2098. p = xdr_encode_hyper(p, stat.size);
  2099. }
  2100. if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
  2101. p = xdr_reserve_space(xdr, 4);
  2102. if (!p)
  2103. goto out_resource;
  2104. *p++ = cpu_to_be32(1);
  2105. }
  2106. if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
  2107. p = xdr_reserve_space(xdr, 4);
  2108. if (!p)
  2109. goto out_resource;
  2110. *p++ = cpu_to_be32(1);
  2111. }
  2112. if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
  2113. p = xdr_reserve_space(xdr, 4);
  2114. if (!p)
  2115. goto out_resource;
  2116. *p++ = cpu_to_be32(0);
  2117. }
  2118. if (bmval0 & FATTR4_WORD0_FSID) {
  2119. p = xdr_reserve_space(xdr, 16);
  2120. if (!p)
  2121. goto out_resource;
  2122. if (exp->ex_fslocs.migrated) {
  2123. p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
  2124. p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
  2125. } else switch(fsid_source(fhp)) {
  2126. case FSIDSOURCE_FSID:
  2127. p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
  2128. p = xdr_encode_hyper(p, (u64)0);
  2129. break;
  2130. case FSIDSOURCE_DEV:
  2131. *p++ = cpu_to_be32(0);
  2132. *p++ = cpu_to_be32(MAJOR(stat.dev));
  2133. *p++ = cpu_to_be32(0);
  2134. *p++ = cpu_to_be32(MINOR(stat.dev));
  2135. break;
  2136. case FSIDSOURCE_UUID:
  2137. p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
  2138. EX_UUID_LEN);
  2139. break;
  2140. }
  2141. }
  2142. if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
  2143. p = xdr_reserve_space(xdr, 4);
  2144. if (!p)
  2145. goto out_resource;
  2146. *p++ = cpu_to_be32(0);
  2147. }
  2148. if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
  2149. p = xdr_reserve_space(xdr, 4);
  2150. if (!p)
  2151. goto out_resource;
  2152. *p++ = cpu_to_be32(nn->nfsd4_lease);
  2153. }
  2154. if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
  2155. p = xdr_reserve_space(xdr, 4);
  2156. if (!p)
  2157. goto out_resource;
  2158. *p++ = cpu_to_be32(rdattr_err);
  2159. }
  2160. if (bmval0 & FATTR4_WORD0_ACL) {
  2161. struct nfs4_ace *ace;
  2162. if (acl == NULL) {
  2163. p = xdr_reserve_space(xdr, 4);
  2164. if (!p)
  2165. goto out_resource;
  2166. *p++ = cpu_to_be32(0);
  2167. goto out_acl;
  2168. }
  2169. p = xdr_reserve_space(xdr, 4);
  2170. if (!p)
  2171. goto out_resource;
  2172. *p++ = cpu_to_be32(acl->naces);
  2173. for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
  2174. p = xdr_reserve_space(xdr, 4*3);
  2175. if (!p)
  2176. goto out_resource;
  2177. *p++ = cpu_to_be32(ace->type);
  2178. *p++ = cpu_to_be32(ace->flag);
  2179. *p++ = cpu_to_be32(ace->access_mask &
  2180. NFS4_ACE_MASK_ALL);
  2181. status = nfsd4_encode_aclname(xdr, rqstp, ace);
  2182. if (status)
  2183. goto out;
  2184. }
  2185. }
  2186. out_acl:
  2187. if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
  2188. p = xdr_reserve_space(xdr, 4);
  2189. if (!p)
  2190. goto out_resource;
  2191. *p++ = cpu_to_be32(aclsupport ?
  2192. ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
  2193. }
  2194. if (bmval0 & FATTR4_WORD0_CANSETTIME) {
  2195. p = xdr_reserve_space(xdr, 4);
  2196. if (!p)
  2197. goto out_resource;
  2198. *p++ = cpu_to_be32(1);
  2199. }
  2200. if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
  2201. p = xdr_reserve_space(xdr, 4);
  2202. if (!p)
  2203. goto out_resource;
  2204. *p++ = cpu_to_be32(0);
  2205. }
  2206. if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
  2207. p = xdr_reserve_space(xdr, 4);
  2208. if (!p)
  2209. goto out_resource;
  2210. *p++ = cpu_to_be32(1);
  2211. }
  2212. if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
  2213. p = xdr_reserve_space(xdr, 4);
  2214. if (!p)
  2215. goto out_resource;
  2216. *p++ = cpu_to_be32(1);
  2217. }
  2218. if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
  2219. p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
  2220. if (!p)
  2221. goto out_resource;
  2222. p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
  2223. fhp->fh_handle.fh_size);
  2224. }
  2225. if (bmval0 & FATTR4_WORD0_FILEID) {
  2226. p = xdr_reserve_space(xdr, 8);
  2227. if (!p)
  2228. goto out_resource;
  2229. p = xdr_encode_hyper(p, stat.ino);
  2230. }
  2231. if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
  2232. p = xdr_reserve_space(xdr, 8);
  2233. if (!p)
  2234. goto out_resource;
  2235. p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
  2236. }
  2237. if (bmval0 & FATTR4_WORD0_FILES_FREE) {
  2238. p = xdr_reserve_space(xdr, 8);
  2239. if (!p)
  2240. goto out_resource;
  2241. p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
  2242. }
  2243. if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
  2244. p = xdr_reserve_space(xdr, 8);
  2245. if (!p)
  2246. goto out_resource;
  2247. p = xdr_encode_hyper(p, (u64) statfs.f_files);
  2248. }
  2249. if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
  2250. status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
  2251. if (status)
  2252. goto out;
  2253. }
  2254. if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
  2255. p = xdr_reserve_space(xdr, 4);
  2256. if (!p)
  2257. goto out_resource;
  2258. *p++ = cpu_to_be32(1);
  2259. }
  2260. if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
  2261. p = xdr_reserve_space(xdr, 8);
  2262. if (!p)
  2263. goto out_resource;
  2264. p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
  2265. }
  2266. if (bmval0 & FATTR4_WORD0_MAXLINK) {
  2267. p = xdr_reserve_space(xdr, 4);
  2268. if (!p)
  2269. goto out_resource;
  2270. *p++ = cpu_to_be32(255);
  2271. }
  2272. if (bmval0 & FATTR4_WORD0_MAXNAME) {
  2273. p = xdr_reserve_space(xdr, 4);
  2274. if (!p)
  2275. goto out_resource;
  2276. *p++ = cpu_to_be32(statfs.f_namelen);
  2277. }
  2278. if (bmval0 & FATTR4_WORD0_MAXREAD) {
  2279. p = xdr_reserve_space(xdr, 8);
  2280. if (!p)
  2281. goto out_resource;
  2282. p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
  2283. }
  2284. if (bmval0 & FATTR4_WORD0_MAXWRITE) {
  2285. p = xdr_reserve_space(xdr, 8);
  2286. if (!p)
  2287. goto out_resource;
  2288. p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
  2289. }
  2290. if (bmval1 & FATTR4_WORD1_MODE) {
  2291. p = xdr_reserve_space(xdr, 4);
  2292. if (!p)
  2293. goto out_resource;
  2294. *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
  2295. }
  2296. if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
  2297. p = xdr_reserve_space(xdr, 4);
  2298. if (!p)
  2299. goto out_resource;
  2300. *p++ = cpu_to_be32(1);
  2301. }
  2302. if (bmval1 & FATTR4_WORD1_NUMLINKS) {
  2303. p = xdr_reserve_space(xdr, 4);
  2304. if (!p)
  2305. goto out_resource;
  2306. *p++ = cpu_to_be32(stat.nlink);
  2307. }
  2308. if (bmval1 & FATTR4_WORD1_OWNER) {
  2309. status = nfsd4_encode_user(xdr, rqstp, stat.uid);
  2310. if (status)
  2311. goto out;
  2312. }
  2313. if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
  2314. status = nfsd4_encode_group(xdr, rqstp, stat.gid);
  2315. if (status)
  2316. goto out;
  2317. }
  2318. if (bmval1 & FATTR4_WORD1_RAWDEV) {
  2319. p = xdr_reserve_space(xdr, 8);
  2320. if (!p)
  2321. goto out_resource;
  2322. *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
  2323. *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
  2324. }
  2325. if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
  2326. p = xdr_reserve_space(xdr, 8);
  2327. if (!p)
  2328. goto out_resource;
  2329. dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
  2330. p = xdr_encode_hyper(p, dummy64);
  2331. }
  2332. if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
  2333. p = xdr_reserve_space(xdr, 8);
  2334. if (!p)
  2335. goto out_resource;
  2336. dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
  2337. p = xdr_encode_hyper(p, dummy64);
  2338. }
  2339. if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
  2340. p = xdr_reserve_space(xdr, 8);
  2341. if (!p)
  2342. goto out_resource;
  2343. dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
  2344. p = xdr_encode_hyper(p, dummy64);
  2345. }
  2346. if (bmval1 & FATTR4_WORD1_SPACE_USED) {
  2347. p = xdr_reserve_space(xdr, 8);
  2348. if (!p)
  2349. goto out_resource;
  2350. dummy64 = (u64)stat.blocks << 9;
  2351. p = xdr_encode_hyper(p, dummy64);
  2352. }
  2353. if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
  2354. p = xdr_reserve_space(xdr, 12);
  2355. if (!p)
  2356. goto out_resource;
  2357. p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
  2358. *p++ = cpu_to_be32(stat.atime.tv_nsec);
  2359. }
  2360. if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
  2361. p = xdr_reserve_space(xdr, 12);
  2362. if (!p)
  2363. goto out_resource;
  2364. *p++ = cpu_to_be32(0);
  2365. *p++ = cpu_to_be32(1);
  2366. *p++ = cpu_to_be32(0);
  2367. }
  2368. if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
  2369. p = xdr_reserve_space(xdr, 12);
  2370. if (!p)
  2371. goto out_resource;
  2372. p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
  2373. *p++ = cpu_to_be32(stat.ctime.tv_nsec);
  2374. }
  2375. if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
  2376. p = xdr_reserve_space(xdr, 12);
  2377. if (!p)
  2378. goto out_resource;
  2379. p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
  2380. *p++ = cpu_to_be32(stat.mtime.tv_nsec);
  2381. }
  2382. if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
  2383. p = xdr_reserve_space(xdr, 8);
  2384. if (!p)
  2385. goto out_resource;
  2386. /*
  2387. * Get parent's attributes if not ignoring crossmount
  2388. * and this is the root of a cross-mounted filesystem.
  2389. */
  2390. if (ignore_crossmnt == 0 &&
  2391. dentry == exp->ex_path.mnt->mnt_root)
  2392. get_parent_attributes(exp, &stat);
  2393. p = xdr_encode_hyper(p, stat.ino);
  2394. }
  2395. #ifdef CONFIG_NFSD_PNFS
  2396. if ((bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) ||
  2397. (bmval2 & FATTR4_WORD2_LAYOUT_TYPES)) {
  2398. if (exp->ex_layout_type) {
  2399. p = xdr_reserve_space(xdr, 8);
  2400. if (!p)
  2401. goto out_resource;
  2402. *p++ = cpu_to_be32(1);
  2403. *p++ = cpu_to_be32(exp->ex_layout_type);
  2404. } else {
  2405. p = xdr_reserve_space(xdr, 4);
  2406. if (!p)
  2407. goto out_resource;
  2408. *p++ = cpu_to_be32(0);
  2409. }
  2410. }
  2411. if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
  2412. p = xdr_reserve_space(xdr, 4);
  2413. if (!p)
  2414. goto out_resource;
  2415. *p++ = cpu_to_be32(stat.blksize);
  2416. }
  2417. #endif /* CONFIG_NFSD_PNFS */
  2418. if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
  2419. status = nfsd4_encode_security_label(xdr, rqstp, context,
  2420. contextlen);
  2421. if (status)
  2422. goto out;
  2423. }
  2424. if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
  2425. p = xdr_reserve_space(xdr, 16);
  2426. if (!p)
  2427. goto out_resource;
  2428. *p++ = cpu_to_be32(3);
  2429. *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
  2430. *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
  2431. *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
  2432. }
  2433. attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
  2434. write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
  2435. status = nfs_ok;
  2436. out:
  2437. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2438. if (context)
  2439. security_release_secctx(context, contextlen);
  2440. #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
  2441. kfree(acl);
  2442. if (tempfh) {
  2443. fh_put(tempfh);
  2444. kfree(tempfh);
  2445. }
  2446. if (status)
  2447. xdr_truncate_encode(xdr, starting_len);
  2448. return status;
  2449. out_nfserr:
  2450. status = nfserrno(err);
  2451. goto out;
  2452. out_resource:
  2453. status = nfserr_resource;
  2454. goto out;
  2455. }
  2456. static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
  2457. struct xdr_buf *buf, __be32 *p, int bytes)
  2458. {
  2459. xdr->scratch.iov_len = 0;
  2460. memset(buf, 0, sizeof(struct xdr_buf));
  2461. buf->head[0].iov_base = p;
  2462. buf->head[0].iov_len = 0;
  2463. buf->len = 0;
  2464. xdr->buf = buf;
  2465. xdr->iov = buf->head;
  2466. xdr->p = p;
  2467. xdr->end = (void *)p + bytes;
  2468. buf->buflen = bytes;
  2469. }
  2470. __be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
  2471. struct svc_fh *fhp, struct svc_export *exp,
  2472. struct dentry *dentry, u32 *bmval,
  2473. struct svc_rqst *rqstp, int ignore_crossmnt)
  2474. {
  2475. struct xdr_buf dummy;
  2476. struct xdr_stream xdr;
  2477. __be32 ret;
  2478. svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
  2479. ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
  2480. ignore_crossmnt);
  2481. *p = xdr.p;
  2482. return ret;
  2483. }
  2484. static inline int attributes_need_mount(u32 *bmval)
  2485. {
  2486. if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
  2487. return 1;
  2488. if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
  2489. return 1;
  2490. return 0;
  2491. }
  2492. static __be32
  2493. nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
  2494. const char *name, int namlen)
  2495. {
  2496. struct svc_export *exp = cd->rd_fhp->fh_export;
  2497. struct dentry *dentry;
  2498. __be32 nfserr;
  2499. int ignore_crossmnt = 0;
  2500. dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
  2501. if (IS_ERR(dentry))
  2502. return nfserrno(PTR_ERR(dentry));
  2503. if (!dentry->d_inode) {
  2504. /*
  2505. * nfsd_buffered_readdir drops the i_mutex between
  2506. * readdir and calling this callback, leaving a window
  2507. * where this directory entry could have gone away.
  2508. */
  2509. dput(dentry);
  2510. return nfserr_noent;
  2511. }
  2512. exp_get(exp);
  2513. /*
  2514. * In the case of a mountpoint, the client may be asking for
  2515. * attributes that are only properties of the underlying filesystem
  2516. * as opposed to the cross-mounted file system. In such a case,
  2517. * we will not follow the cross mount and will fill the attribtutes
  2518. * directly from the mountpoint dentry.
  2519. */
  2520. if (nfsd_mountpoint(dentry, exp)) {
  2521. int err;
  2522. if (!(exp->ex_flags & NFSEXP_V4ROOT)
  2523. && !attributes_need_mount(cd->rd_bmval)) {
  2524. ignore_crossmnt = 1;
  2525. goto out_encode;
  2526. }
  2527. /*
  2528. * Why the heck aren't we just using nfsd_lookup??
  2529. * Different "."/".." handling? Something else?
  2530. * At least, add a comment here to explain....
  2531. */
  2532. err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
  2533. if (err) {
  2534. nfserr = nfserrno(err);
  2535. goto out_put;
  2536. }
  2537. nfserr = check_nfsd_access(exp, cd->rd_rqstp);
  2538. if (nfserr)
  2539. goto out_put;
  2540. }
  2541. out_encode:
  2542. nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
  2543. cd->rd_rqstp, ignore_crossmnt);
  2544. out_put:
  2545. dput(dentry);
  2546. exp_put(exp);
  2547. return nfserr;
  2548. }
  2549. static __be32 *
  2550. nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
  2551. {
  2552. __be32 *p;
  2553. p = xdr_reserve_space(xdr, 20);
  2554. if (!p)
  2555. return NULL;
  2556. *p++ = htonl(2);
  2557. *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
  2558. *p++ = htonl(0); /* bmval1 */
  2559. *p++ = htonl(4); /* attribute length */
  2560. *p++ = nfserr; /* no htonl */
  2561. return p;
  2562. }
  2563. static int
  2564. nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
  2565. loff_t offset, u64 ino, unsigned int d_type)
  2566. {
  2567. struct readdir_cd *ccd = ccdv;
  2568. struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
  2569. struct xdr_stream *xdr = cd->xdr;
  2570. int start_offset = xdr->buf->len;
  2571. int cookie_offset;
  2572. u32 name_and_cookie;
  2573. int entry_bytes;
  2574. __be32 nfserr = nfserr_toosmall;
  2575. __be64 wire_offset;
  2576. __be32 *p;
  2577. /* In nfsv4, "." and ".." never make it onto the wire.. */
  2578. if (name && isdotent(name, namlen)) {
  2579. cd->common.err = nfs_ok;
  2580. return 0;
  2581. }
  2582. if (cd->cookie_offset) {
  2583. wire_offset = cpu_to_be64(offset);
  2584. write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
  2585. &wire_offset, 8);
  2586. }
  2587. p = xdr_reserve_space(xdr, 4);
  2588. if (!p)
  2589. goto fail;
  2590. *p++ = xdr_one; /* mark entry present */
  2591. cookie_offset = xdr->buf->len;
  2592. p = xdr_reserve_space(xdr, 3*4 + namlen);
  2593. if (!p)
  2594. goto fail;
  2595. p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
  2596. p = xdr_encode_array(p, name, namlen); /* name length & name */
  2597. nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
  2598. switch (nfserr) {
  2599. case nfs_ok:
  2600. break;
  2601. case nfserr_resource:
  2602. nfserr = nfserr_toosmall;
  2603. goto fail;
  2604. case nfserr_noent:
  2605. xdr_truncate_encode(xdr, start_offset);
  2606. goto skip_entry;
  2607. default:
  2608. /*
  2609. * If the client requested the RDATTR_ERROR attribute,
  2610. * we stuff the error code into this attribute
  2611. * and continue. If this attribute was not requested,
  2612. * then in accordance with the spec, we fail the
  2613. * entire READDIR operation(!)
  2614. */
  2615. if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
  2616. goto fail;
  2617. p = nfsd4_encode_rdattr_error(xdr, nfserr);
  2618. if (p == NULL) {
  2619. nfserr = nfserr_toosmall;
  2620. goto fail;
  2621. }
  2622. }
  2623. nfserr = nfserr_toosmall;
  2624. entry_bytes = xdr->buf->len - start_offset;
  2625. if (entry_bytes > cd->rd_maxcount)
  2626. goto fail;
  2627. cd->rd_maxcount -= entry_bytes;
  2628. /*
  2629. * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
  2630. * let's always let through the first entry, at least:
  2631. */
  2632. if (!cd->rd_dircount)
  2633. goto fail;
  2634. name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
  2635. if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
  2636. goto fail;
  2637. cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
  2638. cd->cookie_offset = cookie_offset;
  2639. skip_entry:
  2640. cd->common.err = nfs_ok;
  2641. return 0;
  2642. fail:
  2643. xdr_truncate_encode(xdr, start_offset);
  2644. cd->common.err = nfserr;
  2645. return -EINVAL;
  2646. }
  2647. static __be32
  2648. nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
  2649. {
  2650. __be32 *p;
  2651. p = xdr_reserve_space(xdr, sizeof(stateid_t));
  2652. if (!p)
  2653. return nfserr_resource;
  2654. *p++ = cpu_to_be32(sid->si_generation);
  2655. p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
  2656. sizeof(stateid_opaque_t));
  2657. return 0;
  2658. }
  2659. static __be32
  2660. nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
  2661. {
  2662. struct xdr_stream *xdr = &resp->xdr;
  2663. __be32 *p;
  2664. if (!nfserr) {
  2665. p = xdr_reserve_space(xdr, 8);
  2666. if (!p)
  2667. return nfserr_resource;
  2668. *p++ = cpu_to_be32(access->ac_supported);
  2669. *p++ = cpu_to_be32(access->ac_resp_access);
  2670. }
  2671. return nfserr;
  2672. }
  2673. static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
  2674. {
  2675. struct xdr_stream *xdr = &resp->xdr;
  2676. __be32 *p;
  2677. if (!nfserr) {
  2678. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
  2679. if (!p)
  2680. return nfserr_resource;
  2681. p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
  2682. NFS4_MAX_SESSIONID_LEN);
  2683. *p++ = cpu_to_be32(bcts->dir);
  2684. /* Sorry, we do not yet support RDMA over 4.1: */
  2685. *p++ = cpu_to_be32(0);
  2686. }
  2687. return nfserr;
  2688. }
  2689. static __be32
  2690. nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
  2691. {
  2692. struct xdr_stream *xdr = &resp->xdr;
  2693. if (!nfserr)
  2694. nfserr = nfsd4_encode_stateid(xdr, &close->cl_stateid);
  2695. return nfserr;
  2696. }
  2697. static __be32
  2698. nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
  2699. {
  2700. struct xdr_stream *xdr = &resp->xdr;
  2701. __be32 *p;
  2702. if (!nfserr) {
  2703. p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
  2704. if (!p)
  2705. return nfserr_resource;
  2706. p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
  2707. NFS4_VERIFIER_SIZE);
  2708. }
  2709. return nfserr;
  2710. }
  2711. static __be32
  2712. nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
  2713. {
  2714. struct xdr_stream *xdr = &resp->xdr;
  2715. __be32 *p;
  2716. if (!nfserr) {
  2717. p = xdr_reserve_space(xdr, 32);
  2718. if (!p)
  2719. return nfserr_resource;
  2720. p = encode_cinfo(p, &create->cr_cinfo);
  2721. *p++ = cpu_to_be32(2);
  2722. *p++ = cpu_to_be32(create->cr_bmval[0]);
  2723. *p++ = cpu_to_be32(create->cr_bmval[1]);
  2724. }
  2725. return nfserr;
  2726. }
  2727. static __be32
  2728. nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
  2729. {
  2730. struct svc_fh *fhp = getattr->ga_fhp;
  2731. struct xdr_stream *xdr = &resp->xdr;
  2732. if (nfserr)
  2733. return nfserr;
  2734. nfserr = nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
  2735. getattr->ga_bmval,
  2736. resp->rqstp, 0);
  2737. return nfserr;
  2738. }
  2739. static __be32
  2740. nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
  2741. {
  2742. struct xdr_stream *xdr = &resp->xdr;
  2743. struct svc_fh *fhp = *fhpp;
  2744. unsigned int len;
  2745. __be32 *p;
  2746. if (!nfserr) {
  2747. len = fhp->fh_handle.fh_size;
  2748. p = xdr_reserve_space(xdr, len + 4);
  2749. if (!p)
  2750. return nfserr_resource;
  2751. p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
  2752. }
  2753. return nfserr;
  2754. }
  2755. /*
  2756. * Including all fields other than the name, a LOCK4denied structure requires
  2757. * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
  2758. */
  2759. static __be32
  2760. nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
  2761. {
  2762. struct xdr_netobj *conf = &ld->ld_owner;
  2763. __be32 *p;
  2764. again:
  2765. p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
  2766. if (!p) {
  2767. /*
  2768. * Don't fail to return the result just because we can't
  2769. * return the conflicting open:
  2770. */
  2771. if (conf->len) {
  2772. kfree(conf->data);
  2773. conf->len = 0;
  2774. conf->data = NULL;
  2775. goto again;
  2776. }
  2777. return nfserr_resource;
  2778. }
  2779. p = xdr_encode_hyper(p, ld->ld_start);
  2780. p = xdr_encode_hyper(p, ld->ld_length);
  2781. *p++ = cpu_to_be32(ld->ld_type);
  2782. if (conf->len) {
  2783. p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
  2784. p = xdr_encode_opaque(p, conf->data, conf->len);
  2785. kfree(conf->data);
  2786. } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
  2787. p = xdr_encode_hyper(p, (u64)0); /* clientid */
  2788. *p++ = cpu_to_be32(0); /* length of owner name */
  2789. }
  2790. return nfserr_denied;
  2791. }
  2792. static __be32
  2793. nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
  2794. {
  2795. struct xdr_stream *xdr = &resp->xdr;
  2796. if (!nfserr)
  2797. nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
  2798. else if (nfserr == nfserr_denied)
  2799. nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
  2800. return nfserr;
  2801. }
  2802. static __be32
  2803. nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
  2804. {
  2805. struct xdr_stream *xdr = &resp->xdr;
  2806. if (nfserr == nfserr_denied)
  2807. nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
  2808. return nfserr;
  2809. }
  2810. static __be32
  2811. nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
  2812. {
  2813. struct xdr_stream *xdr = &resp->xdr;
  2814. if (!nfserr)
  2815. nfserr = nfsd4_encode_stateid(xdr, &locku->lu_stateid);
  2816. return nfserr;
  2817. }
  2818. static __be32
  2819. nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
  2820. {
  2821. struct xdr_stream *xdr = &resp->xdr;
  2822. __be32 *p;
  2823. if (!nfserr) {
  2824. p = xdr_reserve_space(xdr, 20);
  2825. if (!p)
  2826. return nfserr_resource;
  2827. p = encode_cinfo(p, &link->li_cinfo);
  2828. }
  2829. return nfserr;
  2830. }
  2831. static __be32
  2832. nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
  2833. {
  2834. struct xdr_stream *xdr = &resp->xdr;
  2835. __be32 *p;
  2836. if (nfserr)
  2837. goto out;
  2838. nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
  2839. if (nfserr)
  2840. goto out;
  2841. p = xdr_reserve_space(xdr, 40);
  2842. if (!p)
  2843. return nfserr_resource;
  2844. p = encode_cinfo(p, &open->op_cinfo);
  2845. *p++ = cpu_to_be32(open->op_rflags);
  2846. *p++ = cpu_to_be32(2);
  2847. *p++ = cpu_to_be32(open->op_bmval[0]);
  2848. *p++ = cpu_to_be32(open->op_bmval[1]);
  2849. *p++ = cpu_to_be32(open->op_delegate_type);
  2850. switch (open->op_delegate_type) {
  2851. case NFS4_OPEN_DELEGATE_NONE:
  2852. break;
  2853. case NFS4_OPEN_DELEGATE_READ:
  2854. nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
  2855. if (nfserr)
  2856. return nfserr;
  2857. p = xdr_reserve_space(xdr, 20);
  2858. if (!p)
  2859. return nfserr_resource;
  2860. *p++ = cpu_to_be32(open->op_recall);
  2861. /*
  2862. * TODO: ACE's in delegations
  2863. */
  2864. *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2865. *p++ = cpu_to_be32(0);
  2866. *p++ = cpu_to_be32(0);
  2867. *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
  2868. break;
  2869. case NFS4_OPEN_DELEGATE_WRITE:
  2870. nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
  2871. if (nfserr)
  2872. return nfserr;
  2873. p = xdr_reserve_space(xdr, 32);
  2874. if (!p)
  2875. return nfserr_resource;
  2876. *p++ = cpu_to_be32(0);
  2877. /*
  2878. * TODO: space_limit's in delegations
  2879. */
  2880. *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
  2881. *p++ = cpu_to_be32(~(u32)0);
  2882. *p++ = cpu_to_be32(~(u32)0);
  2883. /*
  2884. * TODO: ACE's in delegations
  2885. */
  2886. *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2887. *p++ = cpu_to_be32(0);
  2888. *p++ = cpu_to_be32(0);
  2889. *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
  2890. break;
  2891. case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
  2892. switch (open->op_why_no_deleg) {
  2893. case WND4_CONTENTION:
  2894. case WND4_RESOURCE:
  2895. p = xdr_reserve_space(xdr, 8);
  2896. if (!p)
  2897. return nfserr_resource;
  2898. *p++ = cpu_to_be32(open->op_why_no_deleg);
  2899. /* deleg signaling not supported yet: */
  2900. *p++ = cpu_to_be32(0);
  2901. break;
  2902. default:
  2903. p = xdr_reserve_space(xdr, 4);
  2904. if (!p)
  2905. return nfserr_resource;
  2906. *p++ = cpu_to_be32(open->op_why_no_deleg);
  2907. }
  2908. break;
  2909. default:
  2910. BUG();
  2911. }
  2912. /* XXX save filehandle here */
  2913. out:
  2914. return nfserr;
  2915. }
  2916. static __be32
  2917. nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
  2918. {
  2919. struct xdr_stream *xdr = &resp->xdr;
  2920. if (!nfserr)
  2921. nfserr = nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
  2922. return nfserr;
  2923. }
  2924. static __be32
  2925. nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
  2926. {
  2927. struct xdr_stream *xdr = &resp->xdr;
  2928. if (!nfserr)
  2929. nfserr = nfsd4_encode_stateid(xdr, &od->od_stateid);
  2930. return nfserr;
  2931. }
  2932. static __be32 nfsd4_encode_splice_read(
  2933. struct nfsd4_compoundres *resp,
  2934. struct nfsd4_read *read,
  2935. struct file *file, unsigned long maxcount)
  2936. {
  2937. struct xdr_stream *xdr = &resp->xdr;
  2938. struct xdr_buf *buf = xdr->buf;
  2939. u32 eof;
  2940. int space_left;
  2941. __be32 nfserr;
  2942. __be32 *p = xdr->p - 2;
  2943. /* Make sure there will be room for padding if needed */
  2944. if (xdr->end - xdr->p < 1)
  2945. return nfserr_resource;
  2946. nfserr = nfsd_splice_read(read->rd_rqstp, file,
  2947. read->rd_offset, &maxcount);
  2948. if (nfserr) {
  2949. /*
  2950. * nfsd_splice_actor may have already messed with the
  2951. * page length; reset it so as not to confuse
  2952. * xdr_truncate_encode:
  2953. */
  2954. buf->page_len = 0;
  2955. return nfserr;
  2956. }
  2957. eof = (read->rd_offset + maxcount >=
  2958. read->rd_fhp->fh_dentry->d_inode->i_size);
  2959. *(p++) = htonl(eof);
  2960. *(p++) = htonl(maxcount);
  2961. buf->page_len = maxcount;
  2962. buf->len += maxcount;
  2963. xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
  2964. / PAGE_SIZE;
  2965. /* Use rest of head for padding and remaining ops: */
  2966. buf->tail[0].iov_base = xdr->p;
  2967. buf->tail[0].iov_len = 0;
  2968. xdr->iov = buf->tail;
  2969. if (maxcount&3) {
  2970. int pad = 4 - (maxcount&3);
  2971. *(xdr->p++) = 0;
  2972. buf->tail[0].iov_base += maxcount&3;
  2973. buf->tail[0].iov_len = pad;
  2974. buf->len += pad;
  2975. }
  2976. space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
  2977. buf->buflen - buf->len);
  2978. buf->buflen = buf->len + space_left;
  2979. xdr->end = (__be32 *)((void *)xdr->end + space_left);
  2980. return 0;
  2981. }
  2982. static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
  2983. struct nfsd4_read *read,
  2984. struct file *file, unsigned long maxcount)
  2985. {
  2986. struct xdr_stream *xdr = &resp->xdr;
  2987. u32 eof;
  2988. int v;
  2989. int starting_len = xdr->buf->len - 8;
  2990. long len;
  2991. int thislen;
  2992. __be32 nfserr;
  2993. __be32 tmp;
  2994. __be32 *p;
  2995. u32 zzz = 0;
  2996. int pad;
  2997. len = maxcount;
  2998. v = 0;
  2999. thislen = min_t(long, len, ((void *)xdr->end - (void *)xdr->p));
  3000. p = xdr_reserve_space(xdr, (thislen+3)&~3);
  3001. WARN_ON_ONCE(!p);
  3002. resp->rqstp->rq_vec[v].iov_base = p;
  3003. resp->rqstp->rq_vec[v].iov_len = thislen;
  3004. v++;
  3005. len -= thislen;
  3006. while (len) {
  3007. thislen = min_t(long, len, PAGE_SIZE);
  3008. p = xdr_reserve_space(xdr, (thislen+3)&~3);
  3009. WARN_ON_ONCE(!p);
  3010. resp->rqstp->rq_vec[v].iov_base = p;
  3011. resp->rqstp->rq_vec[v].iov_len = thislen;
  3012. v++;
  3013. len -= thislen;
  3014. }
  3015. read->rd_vlen = v;
  3016. nfserr = nfsd_readv(file, read->rd_offset, resp->rqstp->rq_vec,
  3017. read->rd_vlen, &maxcount);
  3018. if (nfserr)
  3019. return nfserr;
  3020. xdr_truncate_encode(xdr, starting_len + 8 + ((maxcount+3)&~3));
  3021. eof = (read->rd_offset + maxcount >=
  3022. read->rd_fhp->fh_dentry->d_inode->i_size);
  3023. tmp = htonl(eof);
  3024. write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
  3025. tmp = htonl(maxcount);
  3026. write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
  3027. pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
  3028. write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
  3029. &zzz, pad);
  3030. return 0;
  3031. }
  3032. static __be32
  3033. nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
  3034. struct nfsd4_read *read)
  3035. {
  3036. unsigned long maxcount;
  3037. struct xdr_stream *xdr = &resp->xdr;
  3038. struct file *file = read->rd_filp;
  3039. int starting_len = xdr->buf->len;
  3040. struct raparms *ra;
  3041. __be32 *p;
  3042. __be32 err;
  3043. if (nfserr)
  3044. return nfserr;
  3045. p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
  3046. if (!p) {
  3047. WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
  3048. return nfserr_resource;
  3049. }
  3050. if (resp->xdr.buf->page_len && test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
  3051. WARN_ON_ONCE(1);
  3052. return nfserr_resource;
  3053. }
  3054. xdr_commit_encode(xdr);
  3055. maxcount = svc_max_payload(resp->rqstp);
  3056. maxcount = min_t(unsigned long, maxcount, (xdr->buf->buflen - xdr->buf->len));
  3057. maxcount = min_t(unsigned long, maxcount, read->rd_length);
  3058. if (!read->rd_filp) {
  3059. err = nfsd_get_tmp_read_open(resp->rqstp, read->rd_fhp,
  3060. &file, &ra);
  3061. if (err)
  3062. goto err_truncate;
  3063. }
  3064. if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
  3065. err = nfsd4_encode_splice_read(resp, read, file, maxcount);
  3066. else
  3067. err = nfsd4_encode_readv(resp, read, file, maxcount);
  3068. if (!read->rd_filp)
  3069. nfsd_put_tmp_read_open(file, ra);
  3070. err_truncate:
  3071. if (err)
  3072. xdr_truncate_encode(xdr, starting_len);
  3073. return err;
  3074. }
  3075. static __be32
  3076. nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
  3077. {
  3078. int maxcount;
  3079. __be32 wire_count;
  3080. int zero = 0;
  3081. struct xdr_stream *xdr = &resp->xdr;
  3082. int length_offset = xdr->buf->len;
  3083. __be32 *p;
  3084. if (nfserr)
  3085. return nfserr;
  3086. p = xdr_reserve_space(xdr, 4);
  3087. if (!p)
  3088. return nfserr_resource;
  3089. maxcount = PAGE_SIZE;
  3090. p = xdr_reserve_space(xdr, maxcount);
  3091. if (!p)
  3092. return nfserr_resource;
  3093. /*
  3094. * XXX: By default, the ->readlink() VFS op will truncate symlinks
  3095. * if they would overflow the buffer. Is this kosher in NFSv4? If
  3096. * not, one easy fix is: if ->readlink() precisely fills the buffer,
  3097. * assume that truncation occurred, and return NFS4ERR_RESOURCE.
  3098. */
  3099. nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
  3100. (char *)p, &maxcount);
  3101. if (nfserr == nfserr_isdir)
  3102. nfserr = nfserr_inval;
  3103. if (nfserr) {
  3104. xdr_truncate_encode(xdr, length_offset);
  3105. return nfserr;
  3106. }
  3107. wire_count = htonl(maxcount);
  3108. write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
  3109. xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
  3110. if (maxcount & 3)
  3111. write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
  3112. &zero, 4 - (maxcount&3));
  3113. return 0;
  3114. }
  3115. static __be32
  3116. nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
  3117. {
  3118. int maxcount;
  3119. int bytes_left;
  3120. loff_t offset;
  3121. __be64 wire_offset;
  3122. struct xdr_stream *xdr = &resp->xdr;
  3123. int starting_len = xdr->buf->len;
  3124. __be32 *p;
  3125. if (nfserr)
  3126. return nfserr;
  3127. p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
  3128. if (!p)
  3129. return nfserr_resource;
  3130. /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
  3131. *p++ = cpu_to_be32(0);
  3132. *p++ = cpu_to_be32(0);
  3133. resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
  3134. - (char *)resp->xdr.buf->head[0].iov_base;
  3135. /*
  3136. * Number of bytes left for directory entries allowing for the
  3137. * final 8 bytes of the readdir and a following failed op:
  3138. */
  3139. bytes_left = xdr->buf->buflen - xdr->buf->len
  3140. - COMPOUND_ERR_SLACK_SPACE - 8;
  3141. if (bytes_left < 0) {
  3142. nfserr = nfserr_resource;
  3143. goto err_no_verf;
  3144. }
  3145. maxcount = min_t(u32, readdir->rd_maxcount, INT_MAX);
  3146. /*
  3147. * Note the rfc defines rd_maxcount as the size of the
  3148. * READDIR4resok structure, which includes the verifier above
  3149. * and the 8 bytes encoded at the end of this function:
  3150. */
  3151. if (maxcount < 16) {
  3152. nfserr = nfserr_toosmall;
  3153. goto err_no_verf;
  3154. }
  3155. maxcount = min_t(int, maxcount-16, bytes_left);
  3156. /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
  3157. if (!readdir->rd_dircount)
  3158. readdir->rd_dircount = INT_MAX;
  3159. readdir->xdr = xdr;
  3160. readdir->rd_maxcount = maxcount;
  3161. readdir->common.err = 0;
  3162. readdir->cookie_offset = 0;
  3163. offset = readdir->rd_cookie;
  3164. nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
  3165. &offset,
  3166. &readdir->common, nfsd4_encode_dirent);
  3167. if (nfserr == nfs_ok &&
  3168. readdir->common.err == nfserr_toosmall &&
  3169. xdr->buf->len == starting_len + 8) {
  3170. /* nothing encoded; which limit did we hit?: */
  3171. if (maxcount - 16 < bytes_left)
  3172. /* It was the fault of rd_maxcount: */
  3173. nfserr = nfserr_toosmall;
  3174. else
  3175. /* We ran out of buffer space: */
  3176. nfserr = nfserr_resource;
  3177. }
  3178. if (nfserr)
  3179. goto err_no_verf;
  3180. if (readdir->cookie_offset) {
  3181. wire_offset = cpu_to_be64(offset);
  3182. write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
  3183. &wire_offset, 8);
  3184. }
  3185. p = xdr_reserve_space(xdr, 8);
  3186. if (!p) {
  3187. WARN_ON_ONCE(1);
  3188. goto err_no_verf;
  3189. }
  3190. *p++ = 0; /* no more entries */
  3191. *p++ = htonl(readdir->common.err == nfserr_eof);
  3192. return 0;
  3193. err_no_verf:
  3194. xdr_truncate_encode(xdr, starting_len);
  3195. return nfserr;
  3196. }
  3197. static __be32
  3198. nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
  3199. {
  3200. struct xdr_stream *xdr = &resp->xdr;
  3201. __be32 *p;
  3202. if (!nfserr) {
  3203. p = xdr_reserve_space(xdr, 20);
  3204. if (!p)
  3205. return nfserr_resource;
  3206. p = encode_cinfo(p, &remove->rm_cinfo);
  3207. }
  3208. return nfserr;
  3209. }
  3210. static __be32
  3211. nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
  3212. {
  3213. struct xdr_stream *xdr = &resp->xdr;
  3214. __be32 *p;
  3215. if (!nfserr) {
  3216. p = xdr_reserve_space(xdr, 40);
  3217. if (!p)
  3218. return nfserr_resource;
  3219. p = encode_cinfo(p, &rename->rn_sinfo);
  3220. p = encode_cinfo(p, &rename->rn_tinfo);
  3221. }
  3222. return nfserr;
  3223. }
  3224. static __be32
  3225. nfsd4_do_encode_secinfo(struct xdr_stream *xdr,
  3226. __be32 nfserr, struct svc_export *exp)
  3227. {
  3228. u32 i, nflavs, supported;
  3229. struct exp_flavor_info *flavs;
  3230. struct exp_flavor_info def_flavs[2];
  3231. __be32 *p, *flavorsp;
  3232. static bool report = true;
  3233. if (nfserr)
  3234. goto out;
  3235. nfserr = nfserr_resource;
  3236. if (exp->ex_nflavors) {
  3237. flavs = exp->ex_flavors;
  3238. nflavs = exp->ex_nflavors;
  3239. } else { /* Handling of some defaults in absence of real secinfo: */
  3240. flavs = def_flavs;
  3241. if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
  3242. nflavs = 2;
  3243. flavs[0].pseudoflavor = RPC_AUTH_UNIX;
  3244. flavs[1].pseudoflavor = RPC_AUTH_NULL;
  3245. } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
  3246. nflavs = 1;
  3247. flavs[0].pseudoflavor
  3248. = svcauth_gss_flavor(exp->ex_client);
  3249. } else {
  3250. nflavs = 1;
  3251. flavs[0].pseudoflavor
  3252. = exp->ex_client->flavour->flavour;
  3253. }
  3254. }
  3255. supported = 0;
  3256. p = xdr_reserve_space(xdr, 4);
  3257. if (!p)
  3258. goto out;
  3259. flavorsp = p++; /* to be backfilled later */
  3260. for (i = 0; i < nflavs; i++) {
  3261. rpc_authflavor_t pf = flavs[i].pseudoflavor;
  3262. struct rpcsec_gss_info info;
  3263. if (rpcauth_get_gssinfo(pf, &info) == 0) {
  3264. supported++;
  3265. p = xdr_reserve_space(xdr, 4 + 4 +
  3266. XDR_LEN(info.oid.len) + 4 + 4);
  3267. if (!p)
  3268. goto out;
  3269. *p++ = cpu_to_be32(RPC_AUTH_GSS);
  3270. p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
  3271. *p++ = cpu_to_be32(info.qop);
  3272. *p++ = cpu_to_be32(info.service);
  3273. } else if (pf < RPC_AUTH_MAXFLAVOR) {
  3274. supported++;
  3275. p = xdr_reserve_space(xdr, 4);
  3276. if (!p)
  3277. goto out;
  3278. *p++ = cpu_to_be32(pf);
  3279. } else {
  3280. if (report)
  3281. pr_warn("NFS: SECINFO: security flavor %u "
  3282. "is not supported\n", pf);
  3283. }
  3284. }
  3285. if (nflavs != supported)
  3286. report = false;
  3287. *flavorsp = htonl(supported);
  3288. nfserr = 0;
  3289. out:
  3290. if (exp)
  3291. exp_put(exp);
  3292. return nfserr;
  3293. }
  3294. static __be32
  3295. nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  3296. struct nfsd4_secinfo *secinfo)
  3297. {
  3298. struct xdr_stream *xdr = &resp->xdr;
  3299. return nfsd4_do_encode_secinfo(xdr, nfserr, secinfo->si_exp);
  3300. }
  3301. static __be32
  3302. nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
  3303. struct nfsd4_secinfo_no_name *secinfo)
  3304. {
  3305. struct xdr_stream *xdr = &resp->xdr;
  3306. return nfsd4_do_encode_secinfo(xdr, nfserr, secinfo->sin_exp);
  3307. }
  3308. /*
  3309. * The SETATTR encode routine is special -- it always encodes a bitmap,
  3310. * regardless of the error status.
  3311. */
  3312. static __be32
  3313. nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
  3314. {
  3315. struct xdr_stream *xdr = &resp->xdr;
  3316. __be32 *p;
  3317. p = xdr_reserve_space(xdr, 16);
  3318. if (!p)
  3319. return nfserr_resource;
  3320. if (nfserr) {
  3321. *p++ = cpu_to_be32(3);
  3322. *p++ = cpu_to_be32(0);
  3323. *p++ = cpu_to_be32(0);
  3324. *p++ = cpu_to_be32(0);
  3325. }
  3326. else {
  3327. *p++ = cpu_to_be32(3);
  3328. *p++ = cpu_to_be32(setattr->sa_bmval[0]);
  3329. *p++ = cpu_to_be32(setattr->sa_bmval[1]);
  3330. *p++ = cpu_to_be32(setattr->sa_bmval[2]);
  3331. }
  3332. return nfserr;
  3333. }
  3334. static __be32
  3335. nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
  3336. {
  3337. struct xdr_stream *xdr = &resp->xdr;
  3338. __be32 *p;
  3339. if (!nfserr) {
  3340. p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
  3341. if (!p)
  3342. return nfserr_resource;
  3343. p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
  3344. p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
  3345. NFS4_VERIFIER_SIZE);
  3346. }
  3347. else if (nfserr == nfserr_clid_inuse) {
  3348. p = xdr_reserve_space(xdr, 8);
  3349. if (!p)
  3350. return nfserr_resource;
  3351. *p++ = cpu_to_be32(0);
  3352. *p++ = cpu_to_be32(0);
  3353. }
  3354. return nfserr;
  3355. }
  3356. static __be32
  3357. nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
  3358. {
  3359. struct xdr_stream *xdr = &resp->xdr;
  3360. __be32 *p;
  3361. if (!nfserr) {
  3362. p = xdr_reserve_space(xdr, 16);
  3363. if (!p)
  3364. return nfserr_resource;
  3365. *p++ = cpu_to_be32(write->wr_bytes_written);
  3366. *p++ = cpu_to_be32(write->wr_how_written);
  3367. p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
  3368. NFS4_VERIFIER_SIZE);
  3369. }
  3370. return nfserr;
  3371. }
  3372. static const u32 nfs4_minimal_spo_must_enforce[2] = {
  3373. [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  3374. 1 << (OP_EXCHANGE_ID - 32) |
  3375. 1 << (OP_CREATE_SESSION - 32) |
  3376. 1 << (OP_DESTROY_SESSION - 32) |
  3377. 1 << (OP_DESTROY_CLIENTID - 32)
  3378. };
  3379. static __be32
  3380. nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
  3381. struct nfsd4_exchange_id *exid)
  3382. {
  3383. struct xdr_stream *xdr = &resp->xdr;
  3384. __be32 *p;
  3385. char *major_id;
  3386. char *server_scope;
  3387. int major_id_sz;
  3388. int server_scope_sz;
  3389. uint64_t minor_id = 0;
  3390. if (nfserr)
  3391. return nfserr;
  3392. major_id = utsname()->nodename;
  3393. major_id_sz = strlen(major_id);
  3394. server_scope = utsname()->nodename;
  3395. server_scope_sz = strlen(server_scope);
  3396. p = xdr_reserve_space(xdr,
  3397. 8 /* eir_clientid */ +
  3398. 4 /* eir_sequenceid */ +
  3399. 4 /* eir_flags */ +
  3400. 4 /* spr_how */);
  3401. if (!p)
  3402. return nfserr_resource;
  3403. p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
  3404. *p++ = cpu_to_be32(exid->seqid);
  3405. *p++ = cpu_to_be32(exid->flags);
  3406. *p++ = cpu_to_be32(exid->spa_how);
  3407. switch (exid->spa_how) {
  3408. case SP4_NONE:
  3409. break;
  3410. case SP4_MACH_CRED:
  3411. /* spo_must_enforce, spo_must_allow */
  3412. p = xdr_reserve_space(xdr, 16);
  3413. if (!p)
  3414. return nfserr_resource;
  3415. /* spo_must_enforce bitmap: */
  3416. *p++ = cpu_to_be32(2);
  3417. *p++ = cpu_to_be32(nfs4_minimal_spo_must_enforce[0]);
  3418. *p++ = cpu_to_be32(nfs4_minimal_spo_must_enforce[1]);
  3419. /* empty spo_must_allow bitmap: */
  3420. *p++ = cpu_to_be32(0);
  3421. break;
  3422. default:
  3423. WARN_ON_ONCE(1);
  3424. }
  3425. p = xdr_reserve_space(xdr,
  3426. 8 /* so_minor_id */ +
  3427. 4 /* so_major_id.len */ +
  3428. (XDR_QUADLEN(major_id_sz) * 4) +
  3429. 4 /* eir_server_scope.len */ +
  3430. (XDR_QUADLEN(server_scope_sz) * 4) +
  3431. 4 /* eir_server_impl_id.count (0) */);
  3432. if (!p)
  3433. return nfserr_resource;
  3434. /* The server_owner struct */
  3435. p = xdr_encode_hyper(p, minor_id); /* Minor id */
  3436. /* major id */
  3437. p = xdr_encode_opaque(p, major_id, major_id_sz);
  3438. /* Server scope */
  3439. p = xdr_encode_opaque(p, server_scope, server_scope_sz);
  3440. /* Implementation id */
  3441. *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
  3442. return 0;
  3443. }
  3444. static __be32
  3445. nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
  3446. struct nfsd4_create_session *sess)
  3447. {
  3448. struct xdr_stream *xdr = &resp->xdr;
  3449. __be32 *p;
  3450. if (nfserr)
  3451. return nfserr;
  3452. p = xdr_reserve_space(xdr, 24);
  3453. if (!p)
  3454. return nfserr_resource;
  3455. p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
  3456. NFS4_MAX_SESSIONID_LEN);
  3457. *p++ = cpu_to_be32(sess->seqid);
  3458. *p++ = cpu_to_be32(sess->flags);
  3459. p = xdr_reserve_space(xdr, 28);
  3460. if (!p)
  3461. return nfserr_resource;
  3462. *p++ = cpu_to_be32(0); /* headerpadsz */
  3463. *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
  3464. *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
  3465. *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
  3466. *p++ = cpu_to_be32(sess->fore_channel.maxops);
  3467. *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
  3468. *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
  3469. if (sess->fore_channel.nr_rdma_attrs) {
  3470. p = xdr_reserve_space(xdr, 4);
  3471. if (!p)
  3472. return nfserr_resource;
  3473. *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
  3474. }
  3475. p = xdr_reserve_space(xdr, 28);
  3476. if (!p)
  3477. return nfserr_resource;
  3478. *p++ = cpu_to_be32(0); /* headerpadsz */
  3479. *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
  3480. *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
  3481. *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
  3482. *p++ = cpu_to_be32(sess->back_channel.maxops);
  3483. *p++ = cpu_to_be32(sess->back_channel.maxreqs);
  3484. *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
  3485. if (sess->back_channel.nr_rdma_attrs) {
  3486. p = xdr_reserve_space(xdr, 4);
  3487. if (!p)
  3488. return nfserr_resource;
  3489. *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
  3490. }
  3491. return 0;
  3492. }
  3493. static __be32
  3494. nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
  3495. struct nfsd4_sequence *seq)
  3496. {
  3497. struct xdr_stream *xdr = &resp->xdr;
  3498. __be32 *p;
  3499. if (nfserr)
  3500. return nfserr;
  3501. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
  3502. if (!p)
  3503. return nfserr_resource;
  3504. p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
  3505. NFS4_MAX_SESSIONID_LEN);
  3506. *p++ = cpu_to_be32(seq->seqid);
  3507. *p++ = cpu_to_be32(seq->slotid);
  3508. /* Note slotid's are numbered from zero: */
  3509. *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
  3510. *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
  3511. *p++ = cpu_to_be32(seq->status_flags);
  3512. resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
  3513. return 0;
  3514. }
  3515. static __be32
  3516. nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
  3517. struct nfsd4_test_stateid *test_stateid)
  3518. {
  3519. struct xdr_stream *xdr = &resp->xdr;
  3520. struct nfsd4_test_stateid_id *stateid, *next;
  3521. __be32 *p;
  3522. if (nfserr)
  3523. return nfserr;
  3524. p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
  3525. if (!p)
  3526. return nfserr_resource;
  3527. *p++ = htonl(test_stateid->ts_num_ids);
  3528. list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
  3529. *p++ = stateid->ts_id_status;
  3530. }
  3531. return nfserr;
  3532. }
  3533. #ifdef CONFIG_NFSD_PNFS
  3534. static __be32
  3535. nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  3536. struct nfsd4_getdeviceinfo *gdev)
  3537. {
  3538. struct xdr_stream *xdr = &resp->xdr;
  3539. const struct nfsd4_layout_ops *ops =
  3540. nfsd4_layout_ops[gdev->gd_layout_type];
  3541. u32 starting_len = xdr->buf->len, needed_len;
  3542. __be32 *p;
  3543. dprintk("%s: err %d\n", __func__, nfserr);
  3544. if (nfserr)
  3545. goto out;
  3546. nfserr = nfserr_resource;
  3547. p = xdr_reserve_space(xdr, 4);
  3548. if (!p)
  3549. goto out;
  3550. *p++ = cpu_to_be32(gdev->gd_layout_type);
  3551. /* If maxcount is 0 then just update notifications */
  3552. if (gdev->gd_maxcount != 0) {
  3553. nfserr = ops->encode_getdeviceinfo(xdr, gdev);
  3554. if (nfserr) {
  3555. /*
  3556. * We don't bother to burden the layout drivers with
  3557. * enforcing gd_maxcount, just tell the client to
  3558. * come back with a bigger buffer if it's not enough.
  3559. */
  3560. if (xdr->buf->len + 4 > gdev->gd_maxcount)
  3561. goto toosmall;
  3562. goto out;
  3563. }
  3564. }
  3565. nfserr = nfserr_resource;
  3566. if (gdev->gd_notify_types) {
  3567. p = xdr_reserve_space(xdr, 4 + 4);
  3568. if (!p)
  3569. goto out;
  3570. *p++ = cpu_to_be32(1); /* bitmap length */
  3571. *p++ = cpu_to_be32(gdev->gd_notify_types);
  3572. } else {
  3573. p = xdr_reserve_space(xdr, 4);
  3574. if (!p)
  3575. goto out;
  3576. *p++ = 0;
  3577. }
  3578. nfserr = 0;
  3579. out:
  3580. kfree(gdev->gd_device);
  3581. dprintk("%s: done: %d\n", __func__, be32_to_cpu(nfserr));
  3582. return nfserr;
  3583. toosmall:
  3584. dprintk("%s: maxcount too small\n", __func__);
  3585. needed_len = xdr->buf->len + 4 /* notifications */;
  3586. xdr_truncate_encode(xdr, starting_len);
  3587. p = xdr_reserve_space(xdr, 4);
  3588. if (!p) {
  3589. nfserr = nfserr_resource;
  3590. } else {
  3591. *p++ = cpu_to_be32(needed_len);
  3592. nfserr = nfserr_toosmall;
  3593. }
  3594. goto out;
  3595. }
  3596. static __be32
  3597. nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
  3598. struct nfsd4_layoutget *lgp)
  3599. {
  3600. struct xdr_stream *xdr = &resp->xdr;
  3601. const struct nfsd4_layout_ops *ops =
  3602. nfsd4_layout_ops[lgp->lg_layout_type];
  3603. __be32 *p;
  3604. dprintk("%s: err %d\n", __func__, nfserr);
  3605. if (nfserr)
  3606. goto out;
  3607. nfserr = nfserr_resource;
  3608. p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
  3609. if (!p)
  3610. goto out;
  3611. *p++ = cpu_to_be32(1); /* we always set return-on-close */
  3612. *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
  3613. p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
  3614. sizeof(stateid_opaque_t));
  3615. *p++ = cpu_to_be32(1); /* we always return a single layout */
  3616. p = xdr_encode_hyper(p, lgp->lg_seg.offset);
  3617. p = xdr_encode_hyper(p, lgp->lg_seg.length);
  3618. *p++ = cpu_to_be32(lgp->lg_seg.iomode);
  3619. *p++ = cpu_to_be32(lgp->lg_layout_type);
  3620. nfserr = ops->encode_layoutget(xdr, lgp);
  3621. out:
  3622. kfree(lgp->lg_content);
  3623. return nfserr;
  3624. }
  3625. static __be32
  3626. nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
  3627. struct nfsd4_layoutcommit *lcp)
  3628. {
  3629. struct xdr_stream *xdr = &resp->xdr;
  3630. __be32 *p;
  3631. if (nfserr)
  3632. return nfserr;
  3633. p = xdr_reserve_space(xdr, 4);
  3634. if (!p)
  3635. return nfserr_resource;
  3636. *p++ = cpu_to_be32(lcp->lc_size_chg);
  3637. if (lcp->lc_size_chg) {
  3638. p = xdr_reserve_space(xdr, 8);
  3639. if (!p)
  3640. return nfserr_resource;
  3641. p = xdr_encode_hyper(p, lcp->lc_newsize);
  3642. }
  3643. return nfs_ok;
  3644. }
  3645. static __be32
  3646. nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
  3647. struct nfsd4_layoutreturn *lrp)
  3648. {
  3649. struct xdr_stream *xdr = &resp->xdr;
  3650. __be32 *p;
  3651. if (nfserr)
  3652. return nfserr;
  3653. p = xdr_reserve_space(xdr, 4);
  3654. if (!p)
  3655. return nfserr_resource;
  3656. *p++ = cpu_to_be32(lrp->lrs_present);
  3657. if (lrp->lrs_present)
  3658. nfsd4_encode_stateid(xdr, &lrp->lr_sid);
  3659. return nfs_ok;
  3660. }
  3661. #endif /* CONFIG_NFSD_PNFS */
  3662. static __be32
  3663. nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
  3664. struct nfsd4_seek *seek)
  3665. {
  3666. __be32 *p;
  3667. if (nfserr)
  3668. return nfserr;
  3669. p = xdr_reserve_space(&resp->xdr, 4 + 8);
  3670. *p++ = cpu_to_be32(seek->seek_eof);
  3671. p = xdr_encode_hyper(p, seek->seek_pos);
  3672. return nfserr;
  3673. }
  3674. static __be32
  3675. nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
  3676. {
  3677. return nfserr;
  3678. }
  3679. typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
  3680. /*
  3681. * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
  3682. * since we don't need to filter out obsolete ops as this is
  3683. * done in the decoding phase.
  3684. */
  3685. static nfsd4_enc nfsd4_enc_ops[] = {
  3686. [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
  3687. [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
  3688. [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
  3689. [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
  3690. [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
  3691. [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  3692. [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
  3693. [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
  3694. [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
  3695. [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
  3696. [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
  3697. [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
  3698. [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
  3699. [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
  3700. [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3701. [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
  3702. [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
  3703. [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
  3704. [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
  3705. [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
  3706. [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
  3707. [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
  3708. [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
  3709. [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
  3710. [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
  3711. [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
  3712. [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
  3713. [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
  3714. [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
  3715. [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
  3716. [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
  3717. [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
  3718. [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
  3719. [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
  3720. [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3721. [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
  3722. [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
  3723. /* NFSv4.1 operations */
  3724. [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
  3725. [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
  3726. [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
  3727. [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
  3728. [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
  3729. [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
  3730. [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  3731. #ifdef CONFIG_NFSD_PNFS
  3732. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
  3733. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  3734. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
  3735. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
  3736. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
  3737. #else
  3738. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
  3739. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  3740. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
  3741. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
  3742. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  3743. #endif
  3744. [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
  3745. [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
  3746. [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
  3747. [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
  3748. [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  3749. [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
  3750. [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
  3751. /* NFSv4.2 operations */
  3752. [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
  3753. [OP_COPY] = (nfsd4_enc)nfsd4_encode_noop,
  3754. [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3755. [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
  3756. [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
  3757. [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
  3758. [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
  3759. [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
  3760. [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_noop,
  3761. [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_noop,
  3762. [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
  3763. [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
  3764. };
  3765. /*
  3766. * Calculate whether we still have space to encode repsize bytes.
  3767. * There are two considerations:
  3768. * - For NFS versions >=4.1, the size of the reply must stay within
  3769. * session limits
  3770. * - For all NFS versions, we must stay within limited preallocated
  3771. * buffer space.
  3772. *
  3773. * This is called before the operation is processed, so can only provide
  3774. * an upper estimate. For some nonidempotent operations (such as
  3775. * getattr), it's not necessarily a problem if that estimate is wrong,
  3776. * as we can fail it after processing without significant side effects.
  3777. */
  3778. __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
  3779. {
  3780. struct xdr_buf *buf = &resp->rqstp->rq_res;
  3781. struct nfsd4_slot *slot = resp->cstate.slot;
  3782. if (buf->len + respsize <= buf->buflen)
  3783. return nfs_ok;
  3784. if (!nfsd4_has_session(&resp->cstate))
  3785. return nfserr_resource;
  3786. if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
  3787. WARN_ON_ONCE(1);
  3788. return nfserr_rep_too_big_to_cache;
  3789. }
  3790. return nfserr_rep_too_big;
  3791. }
  3792. void
  3793. nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  3794. {
  3795. struct xdr_stream *xdr = &resp->xdr;
  3796. struct nfs4_stateowner *so = resp->cstate.replay_owner;
  3797. struct svc_rqst *rqstp = resp->rqstp;
  3798. int post_err_offset;
  3799. nfsd4_enc encoder;
  3800. __be32 *p;
  3801. p = xdr_reserve_space(xdr, 8);
  3802. if (!p) {
  3803. WARN_ON_ONCE(1);
  3804. return;
  3805. }
  3806. *p++ = cpu_to_be32(op->opnum);
  3807. post_err_offset = xdr->buf->len;
  3808. if (op->opnum == OP_ILLEGAL)
  3809. goto status;
  3810. BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
  3811. !nfsd4_enc_ops[op->opnum]);
  3812. encoder = nfsd4_enc_ops[op->opnum];
  3813. op->status = encoder(resp, op->status, &op->u);
  3814. xdr_commit_encode(xdr);
  3815. /* nfsd4_check_resp_size guarantees enough room for error status */
  3816. if (!op->status) {
  3817. int space_needed = 0;
  3818. if (!nfsd4_last_compound_op(rqstp))
  3819. space_needed = COMPOUND_ERR_SLACK_SPACE;
  3820. op->status = nfsd4_check_resp_size(resp, space_needed);
  3821. }
  3822. if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
  3823. struct nfsd4_slot *slot = resp->cstate.slot;
  3824. if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
  3825. op->status = nfserr_rep_too_big_to_cache;
  3826. else
  3827. op->status = nfserr_rep_too_big;
  3828. }
  3829. if (op->status == nfserr_resource ||
  3830. op->status == nfserr_rep_too_big ||
  3831. op->status == nfserr_rep_too_big_to_cache) {
  3832. /*
  3833. * The operation may have already been encoded or
  3834. * partially encoded. No op returns anything additional
  3835. * in the case of one of these three errors, so we can
  3836. * just truncate back to after the status. But it's a
  3837. * bug if we had to do this on a non-idempotent op:
  3838. */
  3839. warn_on_nonidempotent_op(op);
  3840. xdr_truncate_encode(xdr, post_err_offset);
  3841. }
  3842. if (so) {
  3843. int len = xdr->buf->len - post_err_offset;
  3844. so->so_replay.rp_status = op->status;
  3845. so->so_replay.rp_buflen = len;
  3846. read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
  3847. so->so_replay.rp_buf, len);
  3848. }
  3849. status:
  3850. /* Note that op->status is already in network byte order: */
  3851. write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
  3852. }
  3853. /*
  3854. * Encode the reply stored in the stateowner reply cache
  3855. *
  3856. * XDR note: do not encode rp->rp_buflen: the buffer contains the
  3857. * previously sent already encoded operation.
  3858. */
  3859. void
  3860. nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
  3861. {
  3862. __be32 *p;
  3863. struct nfs4_replay *rp = op->replay;
  3864. BUG_ON(!rp);
  3865. p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
  3866. if (!p) {
  3867. WARN_ON_ONCE(1);
  3868. return;
  3869. }
  3870. *p++ = cpu_to_be32(op->opnum);
  3871. *p++ = rp->rp_status; /* already xdr'ed */
  3872. p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
  3873. }
  3874. int
  3875. nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  3876. {
  3877. return xdr_ressize_check(rqstp, p);
  3878. }
  3879. int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
  3880. {
  3881. struct svc_rqst *rqstp = rq;
  3882. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  3883. if (args->ops != args->iops) {
  3884. kfree(args->ops);
  3885. args->ops = args->iops;
  3886. }
  3887. kfree(args->tmpp);
  3888. args->tmpp = NULL;
  3889. while (args->to_free) {
  3890. struct svcxdr_tmpbuf *tb = args->to_free;
  3891. args->to_free = tb->next;
  3892. kfree(tb);
  3893. }
  3894. return 1;
  3895. }
  3896. int
  3897. nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
  3898. {
  3899. if (rqstp->rq_arg.head[0].iov_len % 4) {
  3900. /* client is nuts */
  3901. dprintk("%s: compound not properly padded! (peeraddr=%pISc xid=0x%x)",
  3902. __func__, svc_addr(rqstp), be32_to_cpu(rqstp->rq_xid));
  3903. return 0;
  3904. }
  3905. args->p = p;
  3906. args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
  3907. args->pagelist = rqstp->rq_arg.pages;
  3908. args->pagelen = rqstp->rq_arg.page_len;
  3909. args->tmpp = NULL;
  3910. args->to_free = NULL;
  3911. args->ops = args->iops;
  3912. args->rqstp = rqstp;
  3913. return !nfsd4_decode_compound(args);
  3914. }
  3915. int
  3916. nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
  3917. {
  3918. /*
  3919. * All that remains is to write the tag and operation count...
  3920. */
  3921. struct xdr_buf *buf = resp->xdr.buf;
  3922. WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
  3923. buf->tail[0].iov_len);
  3924. rqstp->rq_next_page = resp->xdr.page_ptr + 1;
  3925. p = resp->tagp;
  3926. *p++ = htonl(resp->taglen);
  3927. memcpy(p, resp->tag, resp->taglen);
  3928. p += XDR_QUADLEN(resp->taglen);
  3929. *p++ = htonl(resp->opcnt);
  3930. nfsd4_sequence_done(resp);
  3931. return 1;
  3932. }
  3933. /*
  3934. * Local variables:
  3935. * c-basic-offset: 8
  3936. * End:
  3937. */