nfs4xdr.c 114 KB

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