nfs4xdr.c 115 KB

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