nfs4xdr.c 113 KB

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