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