nfs4xdr.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449
  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 + 4);
  954. SAVEMEM(rename->rn_sname, rename->rn_snamelen);
  955. rename->rn_tnamelen = be32_to_cpup(p++);
  956. READ_BUF(rename->rn_tnamelen);
  957. SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
  958. if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
  959. return status;
  960. if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
  961. return status;
  962. DECODE_TAIL;
  963. }
  964. static __be32
  965. nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
  966. {
  967. DECODE_HEAD;
  968. if (argp->minorversion >= 1)
  969. return nfserr_notsupp;
  970. READ_BUF(sizeof(clientid_t));
  971. COPYMEM(clientid, sizeof(clientid_t));
  972. DECODE_TAIL;
  973. }
  974. static __be32
  975. nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
  976. struct nfsd4_secinfo *secinfo)
  977. {
  978. DECODE_HEAD;
  979. READ_BUF(4);
  980. secinfo->si_namelen = be32_to_cpup(p++);
  981. READ_BUF(secinfo->si_namelen);
  982. SAVEMEM(secinfo->si_name, secinfo->si_namelen);
  983. status = check_filename(secinfo->si_name, secinfo->si_namelen);
  984. if (status)
  985. return status;
  986. DECODE_TAIL;
  987. }
  988. static __be32
  989. nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
  990. struct nfsd4_secinfo_no_name *sin)
  991. {
  992. DECODE_HEAD;
  993. READ_BUF(4);
  994. sin->sin_style = be32_to_cpup(p++);
  995. DECODE_TAIL;
  996. }
  997. static __be32
  998. nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
  999. {
  1000. __be32 status;
  1001. status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
  1002. if (status)
  1003. return status;
  1004. return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
  1005. &setattr->sa_acl, &setattr->sa_label);
  1006. }
  1007. static __be32
  1008. nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
  1009. {
  1010. DECODE_HEAD;
  1011. if (argp->minorversion >= 1)
  1012. return nfserr_notsupp;
  1013. READ_BUF(NFS4_VERIFIER_SIZE);
  1014. COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
  1015. status = nfsd4_decode_opaque(argp, &setclientid->se_name);
  1016. if (status)
  1017. return nfserr_bad_xdr;
  1018. READ_BUF(8);
  1019. setclientid->se_callback_prog = be32_to_cpup(p++);
  1020. setclientid->se_callback_netid_len = be32_to_cpup(p++);
  1021. READ_BUF(setclientid->se_callback_netid_len + 4);
  1022. SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
  1023. setclientid->se_callback_addr_len = be32_to_cpup(p++);
  1024. READ_BUF(setclientid->se_callback_addr_len + 4);
  1025. SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
  1026. setclientid->se_callback_ident = be32_to_cpup(p++);
  1027. DECODE_TAIL;
  1028. }
  1029. static __be32
  1030. nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
  1031. {
  1032. DECODE_HEAD;
  1033. if (argp->minorversion >= 1)
  1034. return nfserr_notsupp;
  1035. READ_BUF(8 + NFS4_VERIFIER_SIZE);
  1036. COPYMEM(&scd_c->sc_clientid, 8);
  1037. COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
  1038. DECODE_TAIL;
  1039. }
  1040. /* Also used for NVERIFY */
  1041. static __be32
  1042. nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
  1043. {
  1044. DECODE_HEAD;
  1045. if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
  1046. goto out;
  1047. /* For convenience's sake, we compare raw xdr'd attributes in
  1048. * nfsd4_proc_verify */
  1049. READ_BUF(4);
  1050. verify->ve_attrlen = be32_to_cpup(p++);
  1051. READ_BUF(verify->ve_attrlen);
  1052. SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
  1053. DECODE_TAIL;
  1054. }
  1055. static __be32
  1056. nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
  1057. {
  1058. int avail;
  1059. int len;
  1060. DECODE_HEAD;
  1061. status = nfsd4_decode_stateid(argp, &write->wr_stateid);
  1062. if (status)
  1063. return status;
  1064. READ_BUF(16);
  1065. p = xdr_decode_hyper(p, &write->wr_offset);
  1066. write->wr_stable_how = be32_to_cpup(p++);
  1067. if (write->wr_stable_how > 2)
  1068. goto xdr_error;
  1069. write->wr_buflen = be32_to_cpup(p++);
  1070. /* Sorry .. no magic macros for this.. *
  1071. * READ_BUF(write->wr_buflen);
  1072. * SAVEMEM(write->wr_buf, write->wr_buflen);
  1073. */
  1074. avail = (char*)argp->end - (char*)argp->p;
  1075. if (avail + argp->pagelen < write->wr_buflen) {
  1076. dprintk("NFSD: xdr error (%s:%d)\n",
  1077. __FILE__, __LINE__);
  1078. goto xdr_error;
  1079. }
  1080. write->wr_head.iov_base = p;
  1081. write->wr_head.iov_len = avail;
  1082. write->wr_pagelist = argp->pagelist;
  1083. len = XDR_QUADLEN(write->wr_buflen) << 2;
  1084. if (len >= avail) {
  1085. int pages;
  1086. len -= avail;
  1087. pages = len >> PAGE_SHIFT;
  1088. argp->pagelist += pages;
  1089. argp->pagelen -= pages * PAGE_SIZE;
  1090. len -= pages * PAGE_SIZE;
  1091. argp->p = (__be32 *)page_address(argp->pagelist[0]);
  1092. argp->pagelist++;
  1093. argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
  1094. }
  1095. argp->p += XDR_QUADLEN(len);
  1096. DECODE_TAIL;
  1097. }
  1098. static __be32
  1099. nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
  1100. {
  1101. DECODE_HEAD;
  1102. if (argp->minorversion >= 1)
  1103. return nfserr_notsupp;
  1104. READ_BUF(12);
  1105. COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
  1106. rlockowner->rl_owner.len = be32_to_cpup(p++);
  1107. READ_BUF(rlockowner->rl_owner.len);
  1108. READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
  1109. if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
  1110. return nfserr_inval;
  1111. DECODE_TAIL;
  1112. }
  1113. static __be32
  1114. nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
  1115. struct nfsd4_exchange_id *exid)
  1116. {
  1117. int dummy, tmp;
  1118. DECODE_HEAD;
  1119. READ_BUF(NFS4_VERIFIER_SIZE);
  1120. COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
  1121. status = nfsd4_decode_opaque(argp, &exid->clname);
  1122. if (status)
  1123. return nfserr_bad_xdr;
  1124. READ_BUF(4);
  1125. exid->flags = be32_to_cpup(p++);
  1126. /* Ignore state_protect4_a */
  1127. READ_BUF(4);
  1128. exid->spa_how = be32_to_cpup(p++);
  1129. switch (exid->spa_how) {
  1130. case SP4_NONE:
  1131. break;
  1132. case SP4_MACH_CRED:
  1133. /* spo_must_enforce */
  1134. READ_BUF(4);
  1135. dummy = be32_to_cpup(p++);
  1136. READ_BUF(dummy * 4);
  1137. p += dummy;
  1138. /* spo_must_allow */
  1139. READ_BUF(4);
  1140. dummy = be32_to_cpup(p++);
  1141. READ_BUF(dummy * 4);
  1142. p += dummy;
  1143. break;
  1144. case SP4_SSV:
  1145. /* ssp_ops */
  1146. READ_BUF(4);
  1147. dummy = be32_to_cpup(p++);
  1148. READ_BUF(dummy * 4);
  1149. p += dummy;
  1150. READ_BUF(4);
  1151. dummy = be32_to_cpup(p++);
  1152. READ_BUF(dummy * 4);
  1153. p += dummy;
  1154. /* ssp_hash_algs<> */
  1155. READ_BUF(4);
  1156. tmp = be32_to_cpup(p++);
  1157. while (tmp--) {
  1158. READ_BUF(4);
  1159. dummy = be32_to_cpup(p++);
  1160. READ_BUF(dummy);
  1161. p += XDR_QUADLEN(dummy);
  1162. }
  1163. /* ssp_encr_algs<> */
  1164. READ_BUF(4);
  1165. tmp = be32_to_cpup(p++);
  1166. while (tmp--) {
  1167. READ_BUF(4);
  1168. dummy = be32_to_cpup(p++);
  1169. READ_BUF(dummy);
  1170. p += XDR_QUADLEN(dummy);
  1171. }
  1172. /* ssp_window and ssp_num_gss_handles */
  1173. READ_BUF(8);
  1174. dummy = be32_to_cpup(p++);
  1175. dummy = be32_to_cpup(p++);
  1176. break;
  1177. default:
  1178. goto xdr_error;
  1179. }
  1180. /* Ignore Implementation ID */
  1181. READ_BUF(4); /* nfs_impl_id4 array length */
  1182. dummy = be32_to_cpup(p++);
  1183. if (dummy > 1)
  1184. goto xdr_error;
  1185. if (dummy == 1) {
  1186. /* nii_domain */
  1187. READ_BUF(4);
  1188. dummy = be32_to_cpup(p++);
  1189. READ_BUF(dummy);
  1190. p += XDR_QUADLEN(dummy);
  1191. /* nii_name */
  1192. READ_BUF(4);
  1193. dummy = be32_to_cpup(p++);
  1194. READ_BUF(dummy);
  1195. p += XDR_QUADLEN(dummy);
  1196. /* nii_date */
  1197. READ_BUF(12);
  1198. p += 3;
  1199. }
  1200. DECODE_TAIL;
  1201. }
  1202. static __be32
  1203. nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
  1204. struct nfsd4_create_session *sess)
  1205. {
  1206. DECODE_HEAD;
  1207. u32 dummy;
  1208. READ_BUF(16);
  1209. COPYMEM(&sess->clientid, 8);
  1210. sess->seqid = be32_to_cpup(p++);
  1211. sess->flags = be32_to_cpup(p++);
  1212. /* Fore channel attrs */
  1213. READ_BUF(28);
  1214. dummy = be32_to_cpup(p++); /* headerpadsz is always 0 */
  1215. sess->fore_channel.maxreq_sz = be32_to_cpup(p++);
  1216. sess->fore_channel.maxresp_sz = be32_to_cpup(p++);
  1217. sess->fore_channel.maxresp_cached = be32_to_cpup(p++);
  1218. sess->fore_channel.maxops = be32_to_cpup(p++);
  1219. sess->fore_channel.maxreqs = be32_to_cpup(p++);
  1220. sess->fore_channel.nr_rdma_attrs = be32_to_cpup(p++);
  1221. if (sess->fore_channel.nr_rdma_attrs == 1) {
  1222. READ_BUF(4);
  1223. sess->fore_channel.rdma_attrs = be32_to_cpup(p++);
  1224. } else if (sess->fore_channel.nr_rdma_attrs > 1) {
  1225. dprintk("Too many fore channel attr bitmaps!\n");
  1226. goto xdr_error;
  1227. }
  1228. /* Back channel attrs */
  1229. READ_BUF(28);
  1230. dummy = be32_to_cpup(p++); /* headerpadsz is always 0 */
  1231. sess->back_channel.maxreq_sz = be32_to_cpup(p++);
  1232. sess->back_channel.maxresp_sz = be32_to_cpup(p++);
  1233. sess->back_channel.maxresp_cached = be32_to_cpup(p++);
  1234. sess->back_channel.maxops = be32_to_cpup(p++);
  1235. sess->back_channel.maxreqs = be32_to_cpup(p++);
  1236. sess->back_channel.nr_rdma_attrs = be32_to_cpup(p++);
  1237. if (sess->back_channel.nr_rdma_attrs == 1) {
  1238. READ_BUF(4);
  1239. sess->back_channel.rdma_attrs = be32_to_cpup(p++);
  1240. } else if (sess->back_channel.nr_rdma_attrs > 1) {
  1241. dprintk("Too many back channel attr bitmaps!\n");
  1242. goto xdr_error;
  1243. }
  1244. READ_BUF(4);
  1245. sess->callback_prog = be32_to_cpup(p++);
  1246. nfsd4_decode_cb_sec(argp, &sess->cb_sec);
  1247. DECODE_TAIL;
  1248. }
  1249. static __be32
  1250. nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
  1251. struct nfsd4_destroy_session *destroy_session)
  1252. {
  1253. DECODE_HEAD;
  1254. READ_BUF(NFS4_MAX_SESSIONID_LEN);
  1255. COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1256. DECODE_TAIL;
  1257. }
  1258. static __be32
  1259. nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
  1260. struct nfsd4_free_stateid *free_stateid)
  1261. {
  1262. DECODE_HEAD;
  1263. READ_BUF(sizeof(stateid_t));
  1264. free_stateid->fr_stateid.si_generation = be32_to_cpup(p++);
  1265. COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
  1266. DECODE_TAIL;
  1267. }
  1268. static __be32
  1269. nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
  1270. struct nfsd4_sequence *seq)
  1271. {
  1272. DECODE_HEAD;
  1273. READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
  1274. COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1275. seq->seqid = be32_to_cpup(p++);
  1276. seq->slotid = be32_to_cpup(p++);
  1277. seq->maxslots = be32_to_cpup(p++);
  1278. seq->cachethis = be32_to_cpup(p++);
  1279. DECODE_TAIL;
  1280. }
  1281. static __be32
  1282. nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
  1283. {
  1284. int i;
  1285. __be32 *p, status;
  1286. struct nfsd4_test_stateid_id *stateid;
  1287. READ_BUF(4);
  1288. test_stateid->ts_num_ids = ntohl(*p++);
  1289. INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
  1290. for (i = 0; i < test_stateid->ts_num_ids; i++) {
  1291. stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
  1292. if (!stateid) {
  1293. status = nfserrno(-ENOMEM);
  1294. goto out;
  1295. }
  1296. INIT_LIST_HEAD(&stateid->ts_id_list);
  1297. list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
  1298. status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
  1299. if (status)
  1300. goto out;
  1301. }
  1302. status = 0;
  1303. out:
  1304. return status;
  1305. xdr_error:
  1306. dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
  1307. status = nfserr_bad_xdr;
  1308. goto out;
  1309. }
  1310. static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
  1311. {
  1312. DECODE_HEAD;
  1313. READ_BUF(8);
  1314. COPYMEM(&dc->clientid, 8);
  1315. DECODE_TAIL;
  1316. }
  1317. static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
  1318. {
  1319. DECODE_HEAD;
  1320. READ_BUF(4);
  1321. rc->rca_one_fs = be32_to_cpup(p++);
  1322. DECODE_TAIL;
  1323. }
  1324. #ifdef CONFIG_NFSD_PNFS
  1325. static __be32
  1326. nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
  1327. struct nfsd4_getdeviceinfo *gdev)
  1328. {
  1329. DECODE_HEAD;
  1330. u32 num, i;
  1331. READ_BUF(sizeof(struct nfsd4_deviceid) + 3 * 4);
  1332. COPYMEM(&gdev->gd_devid, sizeof(struct nfsd4_deviceid));
  1333. gdev->gd_layout_type = be32_to_cpup(p++);
  1334. gdev->gd_maxcount = be32_to_cpup(p++);
  1335. num = be32_to_cpup(p++);
  1336. if (num) {
  1337. READ_BUF(4 * num);
  1338. gdev->gd_notify_types = be32_to_cpup(p++);
  1339. for (i = 1; i < num; i++) {
  1340. if (be32_to_cpup(p++)) {
  1341. status = nfserr_inval;
  1342. goto out;
  1343. }
  1344. }
  1345. }
  1346. DECODE_TAIL;
  1347. }
  1348. static __be32
  1349. nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
  1350. struct nfsd4_layoutget *lgp)
  1351. {
  1352. DECODE_HEAD;
  1353. READ_BUF(36);
  1354. lgp->lg_signal = be32_to_cpup(p++);
  1355. lgp->lg_layout_type = be32_to_cpup(p++);
  1356. lgp->lg_seg.iomode = be32_to_cpup(p++);
  1357. p = xdr_decode_hyper(p, &lgp->lg_seg.offset);
  1358. p = xdr_decode_hyper(p, &lgp->lg_seg.length);
  1359. p = xdr_decode_hyper(p, &lgp->lg_minlength);
  1360. status = nfsd4_decode_stateid(argp, &lgp->lg_sid);
  1361. if (status)
  1362. return status;
  1363. READ_BUF(4);
  1364. lgp->lg_maxcount = be32_to_cpup(p++);
  1365. DECODE_TAIL;
  1366. }
  1367. static __be32
  1368. nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
  1369. struct nfsd4_layoutcommit *lcp)
  1370. {
  1371. DECODE_HEAD;
  1372. u32 timechange;
  1373. READ_BUF(20);
  1374. p = xdr_decode_hyper(p, &lcp->lc_seg.offset);
  1375. p = xdr_decode_hyper(p, &lcp->lc_seg.length);
  1376. lcp->lc_reclaim = be32_to_cpup(p++);
  1377. status = nfsd4_decode_stateid(argp, &lcp->lc_sid);
  1378. if (status)
  1379. return status;
  1380. READ_BUF(4);
  1381. lcp->lc_newoffset = be32_to_cpup(p++);
  1382. if (lcp->lc_newoffset) {
  1383. READ_BUF(8);
  1384. p = xdr_decode_hyper(p, &lcp->lc_last_wr);
  1385. } else
  1386. lcp->lc_last_wr = 0;
  1387. READ_BUF(4);
  1388. timechange = be32_to_cpup(p++);
  1389. if (timechange) {
  1390. status = nfsd4_decode_time(argp, &lcp->lc_mtime);
  1391. if (status)
  1392. return status;
  1393. } else {
  1394. lcp->lc_mtime.tv_nsec = UTIME_NOW;
  1395. }
  1396. READ_BUF(8);
  1397. lcp->lc_layout_type = be32_to_cpup(p++);
  1398. /*
  1399. * Save the layout update in XDR format and let the layout driver deal
  1400. * with it later.
  1401. */
  1402. lcp->lc_up_len = be32_to_cpup(p++);
  1403. if (lcp->lc_up_len > 0) {
  1404. READ_BUF(lcp->lc_up_len);
  1405. READMEM(lcp->lc_up_layout, lcp->lc_up_len);
  1406. }
  1407. DECODE_TAIL;
  1408. }
  1409. static __be32
  1410. nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
  1411. struct nfsd4_layoutreturn *lrp)
  1412. {
  1413. DECODE_HEAD;
  1414. READ_BUF(16);
  1415. lrp->lr_reclaim = be32_to_cpup(p++);
  1416. lrp->lr_layout_type = be32_to_cpup(p++);
  1417. lrp->lr_seg.iomode = be32_to_cpup(p++);
  1418. lrp->lr_return_type = be32_to_cpup(p++);
  1419. if (lrp->lr_return_type == RETURN_FILE) {
  1420. READ_BUF(16);
  1421. p = xdr_decode_hyper(p, &lrp->lr_seg.offset);
  1422. p = xdr_decode_hyper(p, &lrp->lr_seg.length);
  1423. status = nfsd4_decode_stateid(argp, &lrp->lr_sid);
  1424. if (status)
  1425. return status;
  1426. READ_BUF(4);
  1427. lrp->lrf_body_len = be32_to_cpup(p++);
  1428. if (lrp->lrf_body_len > 0) {
  1429. READ_BUF(lrp->lrf_body_len);
  1430. READMEM(lrp->lrf_body, lrp->lrf_body_len);
  1431. }
  1432. } else {
  1433. lrp->lr_seg.offset = 0;
  1434. lrp->lr_seg.length = NFS4_MAX_UINT64;
  1435. }
  1436. DECODE_TAIL;
  1437. }
  1438. #endif /* CONFIG_NFSD_PNFS */
  1439. static __be32
  1440. nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
  1441. struct nfsd4_fallocate *fallocate)
  1442. {
  1443. DECODE_HEAD;
  1444. status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
  1445. if (status)
  1446. return status;
  1447. READ_BUF(16);
  1448. p = xdr_decode_hyper(p, &fallocate->falloc_offset);
  1449. xdr_decode_hyper(p, &fallocate->falloc_length);
  1450. DECODE_TAIL;
  1451. }
  1452. static __be32
  1453. nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
  1454. {
  1455. DECODE_HEAD;
  1456. status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
  1457. if (status)
  1458. return status;
  1459. READ_BUF(8 + 4);
  1460. p = xdr_decode_hyper(p, &seek->seek_offset);
  1461. seek->seek_whence = be32_to_cpup(p);
  1462. DECODE_TAIL;
  1463. }
  1464. static __be32
  1465. nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
  1466. {
  1467. return nfs_ok;
  1468. }
  1469. static __be32
  1470. nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
  1471. {
  1472. return nfserr_notsupp;
  1473. }
  1474. typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
  1475. static nfsd4_dec nfsd4_dec_ops[] = {
  1476. [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
  1477. [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
  1478. [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
  1479. [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
  1480. [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1481. [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
  1482. [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
  1483. [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
  1484. [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
  1485. [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
  1486. [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
  1487. [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
  1488. [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
  1489. [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
  1490. [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1491. [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
  1492. [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1493. [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
  1494. [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
  1495. [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
  1496. [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
  1497. [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
  1498. [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
  1499. [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
  1500. [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
  1501. [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
  1502. [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
  1503. [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
  1504. [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
  1505. [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
  1506. [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
  1507. [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
  1508. [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
  1509. [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
  1510. [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1511. [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
  1512. [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
  1513. /* new operations for NFSv4.1 */
  1514. [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
  1515. [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
  1516. [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
  1517. [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
  1518. [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
  1519. [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
  1520. [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1521. #ifdef CONFIG_NFSD_PNFS
  1522. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
  1523. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  1524. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
  1525. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
  1526. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
  1527. #else
  1528. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
  1529. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  1530. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
  1531. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
  1532. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
  1533. #endif
  1534. [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
  1535. [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
  1536. [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
  1537. [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
  1538. [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1539. [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
  1540. [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
  1541. /* new operations for NFSv4.2 */
  1542. [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
  1543. [OP_COPY] = (nfsd4_dec)nfsd4_decode_notsupp,
  1544. [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_notsupp,
  1545. [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
  1546. [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1547. [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1548. [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1549. [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_notsupp,
  1550. [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1551. [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1552. [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
  1553. [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
  1554. };
  1555. static inline bool
  1556. nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
  1557. {
  1558. if (op->opnum < FIRST_NFS4_OP)
  1559. return false;
  1560. else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
  1561. return false;
  1562. else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
  1563. return false;
  1564. else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
  1565. return false;
  1566. return true;
  1567. }
  1568. static __be32
  1569. nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
  1570. {
  1571. DECODE_HEAD;
  1572. struct nfsd4_op *op;
  1573. bool cachethis = false;
  1574. int auth_slack= argp->rqstp->rq_auth_slack;
  1575. int max_reply = auth_slack + 8; /* opcnt, status */
  1576. int readcount = 0;
  1577. int readbytes = 0;
  1578. int i;
  1579. READ_BUF(4);
  1580. argp->taglen = be32_to_cpup(p++);
  1581. READ_BUF(argp->taglen + 8);
  1582. SAVEMEM(argp->tag, argp->taglen);
  1583. argp->minorversion = be32_to_cpup(p++);
  1584. argp->opcnt = be32_to_cpup(p++);
  1585. max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
  1586. if (argp->taglen > NFSD4_MAX_TAGLEN)
  1587. goto xdr_error;
  1588. if (argp->opcnt > 100)
  1589. goto xdr_error;
  1590. if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
  1591. argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
  1592. if (!argp->ops) {
  1593. argp->ops = argp->iops;
  1594. dprintk("nfsd: couldn't allocate room for COMPOUND\n");
  1595. goto xdr_error;
  1596. }
  1597. }
  1598. if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
  1599. argp->opcnt = 0;
  1600. for (i = 0; i < argp->opcnt; i++) {
  1601. op = &argp->ops[i];
  1602. op->replay = NULL;
  1603. READ_BUF(4);
  1604. op->opnum = be32_to_cpup(p++);
  1605. if (nfsd4_opnum_in_range(argp, op))
  1606. op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
  1607. else {
  1608. op->opnum = OP_ILLEGAL;
  1609. op->status = nfserr_op_illegal;
  1610. }
  1611. /*
  1612. * We'll try to cache the result in the DRC if any one
  1613. * op in the compound wants to be cached:
  1614. */
  1615. cachethis |= nfsd4_cache_this_op(op);
  1616. if (op->opnum == OP_READ) {
  1617. readcount++;
  1618. readbytes += nfsd4_max_reply(argp->rqstp, op);
  1619. } else
  1620. max_reply += nfsd4_max_reply(argp->rqstp, op);
  1621. /*
  1622. * OP_LOCK may return a conflicting lock. (Special case
  1623. * because it will just skip encoding this if it runs
  1624. * out of xdr buffer space, and it is the only operation
  1625. * that behaves this way.)
  1626. */
  1627. if (op->opnum == OP_LOCK)
  1628. max_reply += NFS4_OPAQUE_LIMIT;
  1629. if (op->status) {
  1630. argp->opcnt = i+1;
  1631. break;
  1632. }
  1633. }
  1634. /* Sessions make the DRC unnecessary: */
  1635. if (argp->minorversion)
  1636. cachethis = false;
  1637. svc_reserve(argp->rqstp, max_reply + readbytes);
  1638. argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
  1639. if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
  1640. clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
  1641. DECODE_TAIL;
  1642. }
  1643. static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode)
  1644. {
  1645. if (IS_I_VERSION(inode)) {
  1646. p = xdr_encode_hyper(p, inode->i_version);
  1647. } else {
  1648. *p++ = cpu_to_be32(stat->ctime.tv_sec);
  1649. *p++ = cpu_to_be32(stat->ctime.tv_nsec);
  1650. }
  1651. return p;
  1652. }
  1653. static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
  1654. {
  1655. *p++ = cpu_to_be32(c->atomic);
  1656. if (c->change_supported) {
  1657. p = xdr_encode_hyper(p, c->before_change);
  1658. p = xdr_encode_hyper(p, c->after_change);
  1659. } else {
  1660. *p++ = cpu_to_be32(c->before_ctime_sec);
  1661. *p++ = cpu_to_be32(c->before_ctime_nsec);
  1662. *p++ = cpu_to_be32(c->after_ctime_sec);
  1663. *p++ = cpu_to_be32(c->after_ctime_nsec);
  1664. }
  1665. return p;
  1666. }
  1667. /* Encode as an array of strings the string given with components
  1668. * separated @sep, escaped with esc_enter and esc_exit.
  1669. */
  1670. static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
  1671. char *components, char esc_enter,
  1672. char esc_exit)
  1673. {
  1674. __be32 *p;
  1675. __be32 pathlen;
  1676. int pathlen_offset;
  1677. int strlen, count=0;
  1678. char *str, *end, *next;
  1679. dprintk("nfsd4_encode_components(%s)\n", components);
  1680. pathlen_offset = xdr->buf->len;
  1681. p = xdr_reserve_space(xdr, 4);
  1682. if (!p)
  1683. return nfserr_resource;
  1684. p++; /* We will fill this in with @count later */
  1685. end = str = components;
  1686. while (*end) {
  1687. bool found_esc = false;
  1688. /* try to parse as esc_start, ..., esc_end, sep */
  1689. if (*str == esc_enter) {
  1690. for (; *end && (*end != esc_exit); end++)
  1691. /* find esc_exit or end of string */;
  1692. next = end + 1;
  1693. if (*end && (!*next || *next == sep)) {
  1694. str++;
  1695. found_esc = true;
  1696. }
  1697. }
  1698. if (!found_esc)
  1699. for (; *end && (*end != sep); end++)
  1700. /* find sep or end of string */;
  1701. strlen = end - str;
  1702. if (strlen) {
  1703. p = xdr_reserve_space(xdr, strlen + 4);
  1704. if (!p)
  1705. return nfserr_resource;
  1706. p = xdr_encode_opaque(p, str, strlen);
  1707. count++;
  1708. }
  1709. else
  1710. end++;
  1711. if (found_esc)
  1712. end = next;
  1713. str = end;
  1714. }
  1715. pathlen = htonl(count);
  1716. write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
  1717. return 0;
  1718. }
  1719. /* Encode as an array of strings the string given with components
  1720. * separated @sep.
  1721. */
  1722. static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
  1723. char *components)
  1724. {
  1725. return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
  1726. }
  1727. /*
  1728. * encode a location element of a fs_locations structure
  1729. */
  1730. static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
  1731. struct nfsd4_fs_location *location)
  1732. {
  1733. __be32 status;
  1734. status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
  1735. '[', ']');
  1736. if (status)
  1737. return status;
  1738. status = nfsd4_encode_components(xdr, '/', location->path);
  1739. if (status)
  1740. return status;
  1741. return 0;
  1742. }
  1743. /*
  1744. * Encode a path in RFC3530 'pathname4' format
  1745. */
  1746. static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
  1747. const struct path *root,
  1748. const struct path *path)
  1749. {
  1750. struct path cur = *path;
  1751. __be32 *p;
  1752. struct dentry **components = NULL;
  1753. unsigned int ncomponents = 0;
  1754. __be32 err = nfserr_jukebox;
  1755. dprintk("nfsd4_encode_components(");
  1756. path_get(&cur);
  1757. /* First walk the path up to the nfsd root, and store the
  1758. * dentries/path components in an array.
  1759. */
  1760. for (;;) {
  1761. if (path_equal(&cur, root))
  1762. break;
  1763. if (cur.dentry == cur.mnt->mnt_root) {
  1764. if (follow_up(&cur))
  1765. continue;
  1766. goto out_free;
  1767. }
  1768. if ((ncomponents & 15) == 0) {
  1769. struct dentry **new;
  1770. new = krealloc(components,
  1771. sizeof(*new) * (ncomponents + 16),
  1772. GFP_KERNEL);
  1773. if (!new)
  1774. goto out_free;
  1775. components = new;
  1776. }
  1777. components[ncomponents++] = cur.dentry;
  1778. cur.dentry = dget_parent(cur.dentry);
  1779. }
  1780. err = nfserr_resource;
  1781. p = xdr_reserve_space(xdr, 4);
  1782. if (!p)
  1783. goto out_free;
  1784. *p++ = cpu_to_be32(ncomponents);
  1785. while (ncomponents) {
  1786. struct dentry *dentry = components[ncomponents - 1];
  1787. unsigned int len;
  1788. spin_lock(&dentry->d_lock);
  1789. len = dentry->d_name.len;
  1790. p = xdr_reserve_space(xdr, len + 4);
  1791. if (!p) {
  1792. spin_unlock(&dentry->d_lock);
  1793. goto out_free;
  1794. }
  1795. p = xdr_encode_opaque(p, dentry->d_name.name, len);
  1796. dprintk("/%pd", dentry);
  1797. spin_unlock(&dentry->d_lock);
  1798. dput(dentry);
  1799. ncomponents--;
  1800. }
  1801. err = 0;
  1802. out_free:
  1803. dprintk(")\n");
  1804. while (ncomponents)
  1805. dput(components[--ncomponents]);
  1806. kfree(components);
  1807. path_put(&cur);
  1808. return err;
  1809. }
  1810. static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
  1811. struct svc_rqst *rqstp, const struct path *path)
  1812. {
  1813. struct svc_export *exp_ps;
  1814. __be32 res;
  1815. exp_ps = rqst_find_fsidzero_export(rqstp);
  1816. if (IS_ERR(exp_ps))
  1817. return nfserrno(PTR_ERR(exp_ps));
  1818. res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
  1819. exp_put(exp_ps);
  1820. return res;
  1821. }
  1822. /*
  1823. * encode a fs_locations structure
  1824. */
  1825. static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
  1826. struct svc_rqst *rqstp, struct svc_export *exp)
  1827. {
  1828. __be32 status;
  1829. int i;
  1830. __be32 *p;
  1831. struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
  1832. status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
  1833. if (status)
  1834. return status;
  1835. p = xdr_reserve_space(xdr, 4);
  1836. if (!p)
  1837. return nfserr_resource;
  1838. *p++ = cpu_to_be32(fslocs->locations_count);
  1839. for (i=0; i<fslocs->locations_count; i++) {
  1840. status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
  1841. if (status)
  1842. return status;
  1843. }
  1844. return 0;
  1845. }
  1846. static u32 nfs4_file_type(umode_t mode)
  1847. {
  1848. switch (mode & S_IFMT) {
  1849. case S_IFIFO: return NF4FIFO;
  1850. case S_IFCHR: return NF4CHR;
  1851. case S_IFDIR: return NF4DIR;
  1852. case S_IFBLK: return NF4BLK;
  1853. case S_IFLNK: return NF4LNK;
  1854. case S_IFREG: return NF4REG;
  1855. case S_IFSOCK: return NF4SOCK;
  1856. default: return NF4BAD;
  1857. };
  1858. }
  1859. static inline __be32
  1860. nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  1861. struct nfs4_ace *ace)
  1862. {
  1863. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  1864. return nfs4_acl_write_who(xdr, ace->whotype);
  1865. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  1866. return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
  1867. else
  1868. return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
  1869. }
  1870. #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
  1871. FATTR4_WORD0_RDATTR_ERROR)
  1872. #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
  1873. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  1874. static inline __be32
  1875. nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  1876. void *context, int len)
  1877. {
  1878. __be32 *p;
  1879. p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
  1880. if (!p)
  1881. return nfserr_resource;
  1882. /*
  1883. * For now we use a 0 here to indicate the null translation; in
  1884. * the future we may place a call to translation code here.
  1885. */
  1886. *p++ = cpu_to_be32(0); /* lfs */
  1887. *p++ = cpu_to_be32(0); /* pi */
  1888. p = xdr_encode_opaque(p, context, len);
  1889. return 0;
  1890. }
  1891. #else
  1892. static inline __be32
  1893. nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  1894. void *context, int len)
  1895. { return 0; }
  1896. #endif
  1897. static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
  1898. {
  1899. /* As per referral draft: */
  1900. if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
  1901. *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
  1902. if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
  1903. *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
  1904. *rdattr_err = NFSERR_MOVED;
  1905. else
  1906. return nfserr_moved;
  1907. }
  1908. *bmval0 &= WORD0_ABSENT_FS_ATTRS;
  1909. *bmval1 &= WORD1_ABSENT_FS_ATTRS;
  1910. return 0;
  1911. }
  1912. static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
  1913. {
  1914. struct path path = exp->ex_path;
  1915. int err;
  1916. path_get(&path);
  1917. while (follow_up(&path)) {
  1918. if (path.dentry != path.mnt->mnt_root)
  1919. break;
  1920. }
  1921. err = vfs_getattr(&path, stat);
  1922. path_put(&path);
  1923. return err;
  1924. }
  1925. /*
  1926. * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
  1927. * ourselves.
  1928. */
  1929. static __be32
  1930. nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
  1931. struct svc_export *exp,
  1932. struct dentry *dentry, u32 *bmval,
  1933. struct svc_rqst *rqstp, int ignore_crossmnt)
  1934. {
  1935. u32 bmval0 = bmval[0];
  1936. u32 bmval1 = bmval[1];
  1937. u32 bmval2 = bmval[2];
  1938. struct kstat stat;
  1939. struct svc_fh *tempfh = NULL;
  1940. struct kstatfs statfs;
  1941. __be32 *p;
  1942. int starting_len = xdr->buf->len;
  1943. int attrlen_offset;
  1944. __be32 attrlen;
  1945. u32 dummy;
  1946. u64 dummy64;
  1947. u32 rdattr_err = 0;
  1948. __be32 status;
  1949. int err;
  1950. struct nfs4_acl *acl = NULL;
  1951. void *context = NULL;
  1952. int contextlen;
  1953. bool contextsupport = false;
  1954. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1955. u32 minorversion = resp->cstate.minorversion;
  1956. struct path path = {
  1957. .mnt = exp->ex_path.mnt,
  1958. .dentry = dentry,
  1959. };
  1960. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1961. BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
  1962. BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
  1963. BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
  1964. BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
  1965. if (exp->ex_fslocs.migrated) {
  1966. BUG_ON(bmval[2]);
  1967. status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
  1968. if (status)
  1969. goto out;
  1970. }
  1971. err = vfs_getattr(&path, &stat);
  1972. if (err)
  1973. goto out_nfserr;
  1974. if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
  1975. FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
  1976. (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
  1977. FATTR4_WORD1_SPACE_TOTAL))) {
  1978. err = vfs_statfs(&path, &statfs);
  1979. if (err)
  1980. goto out_nfserr;
  1981. }
  1982. if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
  1983. tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
  1984. status = nfserr_jukebox;
  1985. if (!tempfh)
  1986. goto out;
  1987. fh_init(tempfh, NFS4_FHSIZE);
  1988. status = fh_compose(tempfh, exp, dentry, NULL);
  1989. if (status)
  1990. goto out;
  1991. fhp = tempfh;
  1992. }
  1993. if (bmval0 & FATTR4_WORD0_ACL) {
  1994. err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
  1995. if (err == -EOPNOTSUPP)
  1996. bmval0 &= ~FATTR4_WORD0_ACL;
  1997. else if (err == -EINVAL) {
  1998. status = nfserr_attrnotsupp;
  1999. goto out;
  2000. } else if (err != 0)
  2001. goto out_nfserr;
  2002. }
  2003. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2004. if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
  2005. bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
  2006. err = security_inode_getsecctx(d_inode(dentry),
  2007. &context, &contextlen);
  2008. contextsupport = (err == 0);
  2009. if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
  2010. if (err == -EOPNOTSUPP)
  2011. bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  2012. else if (err)
  2013. goto out_nfserr;
  2014. }
  2015. }
  2016. #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
  2017. if (bmval2) {
  2018. p = xdr_reserve_space(xdr, 16);
  2019. if (!p)
  2020. goto out_resource;
  2021. *p++ = cpu_to_be32(3);
  2022. *p++ = cpu_to_be32(bmval0);
  2023. *p++ = cpu_to_be32(bmval1);
  2024. *p++ = cpu_to_be32(bmval2);
  2025. } else if (bmval1) {
  2026. p = xdr_reserve_space(xdr, 12);
  2027. if (!p)
  2028. goto out_resource;
  2029. *p++ = cpu_to_be32(2);
  2030. *p++ = cpu_to_be32(bmval0);
  2031. *p++ = cpu_to_be32(bmval1);
  2032. } else {
  2033. p = xdr_reserve_space(xdr, 8);
  2034. if (!p)
  2035. goto out_resource;
  2036. *p++ = cpu_to_be32(1);
  2037. *p++ = cpu_to_be32(bmval0);
  2038. }
  2039. attrlen_offset = xdr->buf->len;
  2040. p = xdr_reserve_space(xdr, 4);
  2041. if (!p)
  2042. goto out_resource;
  2043. p++; /* to be backfilled later */
  2044. if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  2045. u32 word0 = nfsd_suppattrs0(minorversion);
  2046. u32 word1 = nfsd_suppattrs1(minorversion);
  2047. u32 word2 = nfsd_suppattrs2(minorversion);
  2048. if (!IS_POSIXACL(dentry->d_inode))
  2049. word0 &= ~FATTR4_WORD0_ACL;
  2050. if (!contextsupport)
  2051. word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  2052. if (!word2) {
  2053. p = xdr_reserve_space(xdr, 12);
  2054. if (!p)
  2055. goto out_resource;
  2056. *p++ = cpu_to_be32(2);
  2057. *p++ = cpu_to_be32(word0);
  2058. *p++ = cpu_to_be32(word1);
  2059. } else {
  2060. p = xdr_reserve_space(xdr, 16);
  2061. if (!p)
  2062. goto out_resource;
  2063. *p++ = cpu_to_be32(3);
  2064. *p++ = cpu_to_be32(word0);
  2065. *p++ = cpu_to_be32(word1);
  2066. *p++ = cpu_to_be32(word2);
  2067. }
  2068. }
  2069. if (bmval0 & FATTR4_WORD0_TYPE) {
  2070. p = xdr_reserve_space(xdr, 4);
  2071. if (!p)
  2072. goto out_resource;
  2073. dummy = nfs4_file_type(stat.mode);
  2074. if (dummy == NF4BAD) {
  2075. status = nfserr_serverfault;
  2076. goto out;
  2077. }
  2078. *p++ = cpu_to_be32(dummy);
  2079. }
  2080. if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
  2081. p = xdr_reserve_space(xdr, 4);
  2082. if (!p)
  2083. goto out_resource;
  2084. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  2085. *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
  2086. else
  2087. *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
  2088. NFS4_FH_VOL_RENAME);
  2089. }
  2090. if (bmval0 & FATTR4_WORD0_CHANGE) {
  2091. p = xdr_reserve_space(xdr, 8);
  2092. if (!p)
  2093. goto out_resource;
  2094. p = encode_change(p, &stat, d_inode(dentry));
  2095. }
  2096. if (bmval0 & FATTR4_WORD0_SIZE) {
  2097. p = xdr_reserve_space(xdr, 8);
  2098. if (!p)
  2099. goto out_resource;
  2100. p = xdr_encode_hyper(p, stat.size);
  2101. }
  2102. if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
  2103. p = xdr_reserve_space(xdr, 4);
  2104. if (!p)
  2105. goto out_resource;
  2106. *p++ = cpu_to_be32(1);
  2107. }
  2108. if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
  2109. p = xdr_reserve_space(xdr, 4);
  2110. if (!p)
  2111. goto out_resource;
  2112. *p++ = cpu_to_be32(1);
  2113. }
  2114. if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
  2115. p = xdr_reserve_space(xdr, 4);
  2116. if (!p)
  2117. goto out_resource;
  2118. *p++ = cpu_to_be32(0);
  2119. }
  2120. if (bmval0 & FATTR4_WORD0_FSID) {
  2121. p = xdr_reserve_space(xdr, 16);
  2122. if (!p)
  2123. goto out_resource;
  2124. if (exp->ex_fslocs.migrated) {
  2125. p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
  2126. p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
  2127. } else switch(fsid_source(fhp)) {
  2128. case FSIDSOURCE_FSID:
  2129. p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
  2130. p = xdr_encode_hyper(p, (u64)0);
  2131. break;
  2132. case FSIDSOURCE_DEV:
  2133. *p++ = cpu_to_be32(0);
  2134. *p++ = cpu_to_be32(MAJOR(stat.dev));
  2135. *p++ = cpu_to_be32(0);
  2136. *p++ = cpu_to_be32(MINOR(stat.dev));
  2137. break;
  2138. case FSIDSOURCE_UUID:
  2139. p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
  2140. EX_UUID_LEN);
  2141. break;
  2142. }
  2143. }
  2144. if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
  2145. p = xdr_reserve_space(xdr, 4);
  2146. if (!p)
  2147. goto out_resource;
  2148. *p++ = cpu_to_be32(0);
  2149. }
  2150. if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
  2151. p = xdr_reserve_space(xdr, 4);
  2152. if (!p)
  2153. goto out_resource;
  2154. *p++ = cpu_to_be32(nn->nfsd4_lease);
  2155. }
  2156. if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
  2157. p = xdr_reserve_space(xdr, 4);
  2158. if (!p)
  2159. goto out_resource;
  2160. *p++ = cpu_to_be32(rdattr_err);
  2161. }
  2162. if (bmval0 & FATTR4_WORD0_ACL) {
  2163. struct nfs4_ace *ace;
  2164. if (acl == NULL) {
  2165. p = xdr_reserve_space(xdr, 4);
  2166. if (!p)
  2167. goto out_resource;
  2168. *p++ = cpu_to_be32(0);
  2169. goto out_acl;
  2170. }
  2171. p = xdr_reserve_space(xdr, 4);
  2172. if (!p)
  2173. goto out_resource;
  2174. *p++ = cpu_to_be32(acl->naces);
  2175. for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
  2176. p = xdr_reserve_space(xdr, 4*3);
  2177. if (!p)
  2178. goto out_resource;
  2179. *p++ = cpu_to_be32(ace->type);
  2180. *p++ = cpu_to_be32(ace->flag);
  2181. *p++ = cpu_to_be32(ace->access_mask &
  2182. NFS4_ACE_MASK_ALL);
  2183. status = nfsd4_encode_aclname(xdr, rqstp, ace);
  2184. if (status)
  2185. goto out;
  2186. }
  2187. }
  2188. out_acl:
  2189. if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
  2190. p = xdr_reserve_space(xdr, 4);
  2191. if (!p)
  2192. goto out_resource;
  2193. *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
  2194. ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
  2195. }
  2196. if (bmval0 & FATTR4_WORD0_CANSETTIME) {
  2197. p = xdr_reserve_space(xdr, 4);
  2198. if (!p)
  2199. goto out_resource;
  2200. *p++ = cpu_to_be32(1);
  2201. }
  2202. if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
  2203. p = xdr_reserve_space(xdr, 4);
  2204. if (!p)
  2205. goto out_resource;
  2206. *p++ = cpu_to_be32(0);
  2207. }
  2208. if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
  2209. p = xdr_reserve_space(xdr, 4);
  2210. if (!p)
  2211. goto out_resource;
  2212. *p++ = cpu_to_be32(1);
  2213. }
  2214. if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
  2215. p = xdr_reserve_space(xdr, 4);
  2216. if (!p)
  2217. goto out_resource;
  2218. *p++ = cpu_to_be32(1);
  2219. }
  2220. if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
  2221. p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
  2222. if (!p)
  2223. goto out_resource;
  2224. p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
  2225. fhp->fh_handle.fh_size);
  2226. }
  2227. if (bmval0 & FATTR4_WORD0_FILEID) {
  2228. p = xdr_reserve_space(xdr, 8);
  2229. if (!p)
  2230. goto out_resource;
  2231. p = xdr_encode_hyper(p, stat.ino);
  2232. }
  2233. if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
  2234. p = xdr_reserve_space(xdr, 8);
  2235. if (!p)
  2236. goto out_resource;
  2237. p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
  2238. }
  2239. if (bmval0 & FATTR4_WORD0_FILES_FREE) {
  2240. p = xdr_reserve_space(xdr, 8);
  2241. if (!p)
  2242. goto out_resource;
  2243. p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
  2244. }
  2245. if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
  2246. p = xdr_reserve_space(xdr, 8);
  2247. if (!p)
  2248. goto out_resource;
  2249. p = xdr_encode_hyper(p, (u64) statfs.f_files);
  2250. }
  2251. if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
  2252. status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
  2253. if (status)
  2254. goto out;
  2255. }
  2256. if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
  2257. p = xdr_reserve_space(xdr, 4);
  2258. if (!p)
  2259. goto out_resource;
  2260. *p++ = cpu_to_be32(1);
  2261. }
  2262. if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
  2263. p = xdr_reserve_space(xdr, 8);
  2264. if (!p)
  2265. goto out_resource;
  2266. p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
  2267. }
  2268. if (bmval0 & FATTR4_WORD0_MAXLINK) {
  2269. p = xdr_reserve_space(xdr, 4);
  2270. if (!p)
  2271. goto out_resource;
  2272. *p++ = cpu_to_be32(255);
  2273. }
  2274. if (bmval0 & FATTR4_WORD0_MAXNAME) {
  2275. p = xdr_reserve_space(xdr, 4);
  2276. if (!p)
  2277. goto out_resource;
  2278. *p++ = cpu_to_be32(statfs.f_namelen);
  2279. }
  2280. if (bmval0 & FATTR4_WORD0_MAXREAD) {
  2281. p = xdr_reserve_space(xdr, 8);
  2282. if (!p)
  2283. goto out_resource;
  2284. p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
  2285. }
  2286. if (bmval0 & FATTR4_WORD0_MAXWRITE) {
  2287. p = xdr_reserve_space(xdr, 8);
  2288. if (!p)
  2289. goto out_resource;
  2290. p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
  2291. }
  2292. if (bmval1 & FATTR4_WORD1_MODE) {
  2293. p = xdr_reserve_space(xdr, 4);
  2294. if (!p)
  2295. goto out_resource;
  2296. *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
  2297. }
  2298. if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
  2299. p = xdr_reserve_space(xdr, 4);
  2300. if (!p)
  2301. goto out_resource;
  2302. *p++ = cpu_to_be32(1);
  2303. }
  2304. if (bmval1 & FATTR4_WORD1_NUMLINKS) {
  2305. p = xdr_reserve_space(xdr, 4);
  2306. if (!p)
  2307. goto out_resource;
  2308. *p++ = cpu_to_be32(stat.nlink);
  2309. }
  2310. if (bmval1 & FATTR4_WORD1_OWNER) {
  2311. status = nfsd4_encode_user(xdr, rqstp, stat.uid);
  2312. if (status)
  2313. goto out;
  2314. }
  2315. if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
  2316. status = nfsd4_encode_group(xdr, rqstp, stat.gid);
  2317. if (status)
  2318. goto out;
  2319. }
  2320. if (bmval1 & FATTR4_WORD1_RAWDEV) {
  2321. p = xdr_reserve_space(xdr, 8);
  2322. if (!p)
  2323. goto out_resource;
  2324. *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
  2325. *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
  2326. }
  2327. if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
  2328. p = xdr_reserve_space(xdr, 8);
  2329. if (!p)
  2330. goto out_resource;
  2331. dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
  2332. p = xdr_encode_hyper(p, dummy64);
  2333. }
  2334. if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
  2335. p = xdr_reserve_space(xdr, 8);
  2336. if (!p)
  2337. goto out_resource;
  2338. dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
  2339. p = xdr_encode_hyper(p, dummy64);
  2340. }
  2341. if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
  2342. p = xdr_reserve_space(xdr, 8);
  2343. if (!p)
  2344. goto out_resource;
  2345. dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
  2346. p = xdr_encode_hyper(p, dummy64);
  2347. }
  2348. if (bmval1 & FATTR4_WORD1_SPACE_USED) {
  2349. p = xdr_reserve_space(xdr, 8);
  2350. if (!p)
  2351. goto out_resource;
  2352. dummy64 = (u64)stat.blocks << 9;
  2353. p = xdr_encode_hyper(p, dummy64);
  2354. }
  2355. if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
  2356. p = xdr_reserve_space(xdr, 12);
  2357. if (!p)
  2358. goto out_resource;
  2359. p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
  2360. *p++ = cpu_to_be32(stat.atime.tv_nsec);
  2361. }
  2362. if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
  2363. p = xdr_reserve_space(xdr, 12);
  2364. if (!p)
  2365. goto out_resource;
  2366. *p++ = cpu_to_be32(0);
  2367. *p++ = cpu_to_be32(1);
  2368. *p++ = cpu_to_be32(0);
  2369. }
  2370. if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
  2371. p = xdr_reserve_space(xdr, 12);
  2372. if (!p)
  2373. goto out_resource;
  2374. p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
  2375. *p++ = cpu_to_be32(stat.ctime.tv_nsec);
  2376. }
  2377. if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
  2378. p = xdr_reserve_space(xdr, 12);
  2379. if (!p)
  2380. goto out_resource;
  2381. p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
  2382. *p++ = cpu_to_be32(stat.mtime.tv_nsec);
  2383. }
  2384. if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
  2385. p = xdr_reserve_space(xdr, 8);
  2386. if (!p)
  2387. goto out_resource;
  2388. /*
  2389. * Get parent's attributes if not ignoring crossmount
  2390. * and this is the root of a cross-mounted filesystem.
  2391. */
  2392. if (ignore_crossmnt == 0 &&
  2393. dentry == exp->ex_path.mnt->mnt_root)
  2394. get_parent_attributes(exp, &stat);
  2395. p = xdr_encode_hyper(p, stat.ino);
  2396. }
  2397. #ifdef CONFIG_NFSD_PNFS
  2398. if ((bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) ||
  2399. (bmval2 & FATTR4_WORD2_LAYOUT_TYPES)) {
  2400. if (exp->ex_layout_type) {
  2401. p = xdr_reserve_space(xdr, 8);
  2402. if (!p)
  2403. goto out_resource;
  2404. *p++ = cpu_to_be32(1);
  2405. *p++ = cpu_to_be32(exp->ex_layout_type);
  2406. } else {
  2407. p = xdr_reserve_space(xdr, 4);
  2408. if (!p)
  2409. goto out_resource;
  2410. *p++ = cpu_to_be32(0);
  2411. }
  2412. }
  2413. if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
  2414. p = xdr_reserve_space(xdr, 4);
  2415. if (!p)
  2416. goto out_resource;
  2417. *p++ = cpu_to_be32(stat.blksize);
  2418. }
  2419. #endif /* CONFIG_NFSD_PNFS */
  2420. if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
  2421. status = nfsd4_encode_security_label(xdr, rqstp, context,
  2422. contextlen);
  2423. if (status)
  2424. goto out;
  2425. }
  2426. if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
  2427. p = xdr_reserve_space(xdr, 16);
  2428. if (!p)
  2429. goto out_resource;
  2430. *p++ = cpu_to_be32(3);
  2431. *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
  2432. *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
  2433. *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
  2434. }
  2435. attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
  2436. write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
  2437. status = nfs_ok;
  2438. out:
  2439. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2440. if (context)
  2441. security_release_secctx(context, contextlen);
  2442. #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
  2443. kfree(acl);
  2444. if (tempfh) {
  2445. fh_put(tempfh);
  2446. kfree(tempfh);
  2447. }
  2448. if (status)
  2449. xdr_truncate_encode(xdr, starting_len);
  2450. return status;
  2451. out_nfserr:
  2452. status = nfserrno(err);
  2453. goto out;
  2454. out_resource:
  2455. status = nfserr_resource;
  2456. goto out;
  2457. }
  2458. static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
  2459. struct xdr_buf *buf, __be32 *p, int bytes)
  2460. {
  2461. xdr->scratch.iov_len = 0;
  2462. memset(buf, 0, sizeof(struct xdr_buf));
  2463. buf->head[0].iov_base = p;
  2464. buf->head[0].iov_len = 0;
  2465. buf->len = 0;
  2466. xdr->buf = buf;
  2467. xdr->iov = buf->head;
  2468. xdr->p = p;
  2469. xdr->end = (void *)p + bytes;
  2470. buf->buflen = bytes;
  2471. }
  2472. __be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
  2473. struct svc_fh *fhp, struct svc_export *exp,
  2474. struct dentry *dentry, u32 *bmval,
  2475. struct svc_rqst *rqstp, int ignore_crossmnt)
  2476. {
  2477. struct xdr_buf dummy;
  2478. struct xdr_stream xdr;
  2479. __be32 ret;
  2480. svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
  2481. ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
  2482. ignore_crossmnt);
  2483. *p = xdr.p;
  2484. return ret;
  2485. }
  2486. static inline int attributes_need_mount(u32 *bmval)
  2487. {
  2488. if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
  2489. return 1;
  2490. if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
  2491. return 1;
  2492. return 0;
  2493. }
  2494. static __be32
  2495. nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
  2496. const char *name, int namlen)
  2497. {
  2498. struct svc_export *exp = cd->rd_fhp->fh_export;
  2499. struct dentry *dentry;
  2500. __be32 nfserr;
  2501. int ignore_crossmnt = 0;
  2502. dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
  2503. if (IS_ERR(dentry))
  2504. return nfserrno(PTR_ERR(dentry));
  2505. if (d_really_is_negative(dentry)) {
  2506. /*
  2507. * nfsd_buffered_readdir drops the i_mutex between
  2508. * readdir and calling this callback, leaving a window
  2509. * where this directory entry could have gone away.
  2510. */
  2511. dput(dentry);
  2512. return nfserr_noent;
  2513. }
  2514. exp_get(exp);
  2515. /*
  2516. * In the case of a mountpoint, the client may be asking for
  2517. * attributes that are only properties of the underlying filesystem
  2518. * as opposed to the cross-mounted file system. In such a case,
  2519. * we will not follow the cross mount and will fill the attribtutes
  2520. * directly from the mountpoint dentry.
  2521. */
  2522. if (nfsd_mountpoint(dentry, exp)) {
  2523. int err;
  2524. if (!(exp->ex_flags & NFSEXP_V4ROOT)
  2525. && !attributes_need_mount(cd->rd_bmval)) {
  2526. ignore_crossmnt = 1;
  2527. goto out_encode;
  2528. }
  2529. /*
  2530. * Why the heck aren't we just using nfsd_lookup??
  2531. * Different "."/".." handling? Something else?
  2532. * At least, add a comment here to explain....
  2533. */
  2534. err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
  2535. if (err) {
  2536. nfserr = nfserrno(err);
  2537. goto out_put;
  2538. }
  2539. nfserr = check_nfsd_access(exp, cd->rd_rqstp);
  2540. if (nfserr)
  2541. goto out_put;
  2542. }
  2543. out_encode:
  2544. nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
  2545. cd->rd_rqstp, ignore_crossmnt);
  2546. out_put:
  2547. dput(dentry);
  2548. exp_put(exp);
  2549. return nfserr;
  2550. }
  2551. static __be32 *
  2552. nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
  2553. {
  2554. __be32 *p;
  2555. p = xdr_reserve_space(xdr, 20);
  2556. if (!p)
  2557. return NULL;
  2558. *p++ = htonl(2);
  2559. *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
  2560. *p++ = htonl(0); /* bmval1 */
  2561. *p++ = htonl(4); /* attribute length */
  2562. *p++ = nfserr; /* no htonl */
  2563. return p;
  2564. }
  2565. static int
  2566. nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
  2567. loff_t offset, u64 ino, unsigned int d_type)
  2568. {
  2569. struct readdir_cd *ccd = ccdv;
  2570. struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
  2571. struct xdr_stream *xdr = cd->xdr;
  2572. int start_offset = xdr->buf->len;
  2573. int cookie_offset;
  2574. u32 name_and_cookie;
  2575. int entry_bytes;
  2576. __be32 nfserr = nfserr_toosmall;
  2577. __be64 wire_offset;
  2578. __be32 *p;
  2579. /* In nfsv4, "." and ".." never make it onto the wire.. */
  2580. if (name && isdotent(name, namlen)) {
  2581. cd->common.err = nfs_ok;
  2582. return 0;
  2583. }
  2584. if (cd->cookie_offset) {
  2585. wire_offset = cpu_to_be64(offset);
  2586. write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
  2587. &wire_offset, 8);
  2588. }
  2589. p = xdr_reserve_space(xdr, 4);
  2590. if (!p)
  2591. goto fail;
  2592. *p++ = xdr_one; /* mark entry present */
  2593. cookie_offset = xdr->buf->len;
  2594. p = xdr_reserve_space(xdr, 3*4 + namlen);
  2595. if (!p)
  2596. goto fail;
  2597. p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
  2598. p = xdr_encode_array(p, name, namlen); /* name length & name */
  2599. nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
  2600. switch (nfserr) {
  2601. case nfs_ok:
  2602. break;
  2603. case nfserr_resource:
  2604. nfserr = nfserr_toosmall;
  2605. goto fail;
  2606. case nfserr_noent:
  2607. xdr_truncate_encode(xdr, start_offset);
  2608. goto skip_entry;
  2609. default:
  2610. /*
  2611. * If the client requested the RDATTR_ERROR attribute,
  2612. * we stuff the error code into this attribute
  2613. * and continue. If this attribute was not requested,
  2614. * then in accordance with the spec, we fail the
  2615. * entire READDIR operation(!)
  2616. */
  2617. if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
  2618. goto fail;
  2619. p = nfsd4_encode_rdattr_error(xdr, nfserr);
  2620. if (p == NULL) {
  2621. nfserr = nfserr_toosmall;
  2622. goto fail;
  2623. }
  2624. }
  2625. nfserr = nfserr_toosmall;
  2626. entry_bytes = xdr->buf->len - start_offset;
  2627. if (entry_bytes > cd->rd_maxcount)
  2628. goto fail;
  2629. cd->rd_maxcount -= entry_bytes;
  2630. /*
  2631. * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
  2632. * let's always let through the first entry, at least:
  2633. */
  2634. if (!cd->rd_dircount)
  2635. goto fail;
  2636. name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
  2637. if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
  2638. goto fail;
  2639. cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
  2640. cd->cookie_offset = cookie_offset;
  2641. skip_entry:
  2642. cd->common.err = nfs_ok;
  2643. return 0;
  2644. fail:
  2645. xdr_truncate_encode(xdr, start_offset);
  2646. cd->common.err = nfserr;
  2647. return -EINVAL;
  2648. }
  2649. static __be32
  2650. nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
  2651. {
  2652. __be32 *p;
  2653. p = xdr_reserve_space(xdr, sizeof(stateid_t));
  2654. if (!p)
  2655. return nfserr_resource;
  2656. *p++ = cpu_to_be32(sid->si_generation);
  2657. p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
  2658. sizeof(stateid_opaque_t));
  2659. return 0;
  2660. }
  2661. static __be32
  2662. nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
  2663. {
  2664. struct xdr_stream *xdr = &resp->xdr;
  2665. __be32 *p;
  2666. if (!nfserr) {
  2667. p = xdr_reserve_space(xdr, 8);
  2668. if (!p)
  2669. return nfserr_resource;
  2670. *p++ = cpu_to_be32(access->ac_supported);
  2671. *p++ = cpu_to_be32(access->ac_resp_access);
  2672. }
  2673. return nfserr;
  2674. }
  2675. static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
  2676. {
  2677. struct xdr_stream *xdr = &resp->xdr;
  2678. __be32 *p;
  2679. if (!nfserr) {
  2680. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
  2681. if (!p)
  2682. return nfserr_resource;
  2683. p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
  2684. NFS4_MAX_SESSIONID_LEN);
  2685. *p++ = cpu_to_be32(bcts->dir);
  2686. /* Sorry, we do not yet support RDMA over 4.1: */
  2687. *p++ = cpu_to_be32(0);
  2688. }
  2689. return nfserr;
  2690. }
  2691. static __be32
  2692. nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
  2693. {
  2694. struct xdr_stream *xdr = &resp->xdr;
  2695. if (!nfserr)
  2696. nfserr = nfsd4_encode_stateid(xdr, &close->cl_stateid);
  2697. return nfserr;
  2698. }
  2699. static __be32
  2700. nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
  2701. {
  2702. struct xdr_stream *xdr = &resp->xdr;
  2703. __be32 *p;
  2704. if (!nfserr) {
  2705. p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
  2706. if (!p)
  2707. return nfserr_resource;
  2708. p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
  2709. NFS4_VERIFIER_SIZE);
  2710. }
  2711. return nfserr;
  2712. }
  2713. static __be32
  2714. nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
  2715. {
  2716. struct xdr_stream *xdr = &resp->xdr;
  2717. __be32 *p;
  2718. if (!nfserr) {
  2719. p = xdr_reserve_space(xdr, 32);
  2720. if (!p)
  2721. return nfserr_resource;
  2722. p = encode_cinfo(p, &create->cr_cinfo);
  2723. *p++ = cpu_to_be32(2);
  2724. *p++ = cpu_to_be32(create->cr_bmval[0]);
  2725. *p++ = cpu_to_be32(create->cr_bmval[1]);
  2726. }
  2727. return nfserr;
  2728. }
  2729. static __be32
  2730. nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
  2731. {
  2732. struct svc_fh *fhp = getattr->ga_fhp;
  2733. struct xdr_stream *xdr = &resp->xdr;
  2734. if (nfserr)
  2735. return nfserr;
  2736. nfserr = nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
  2737. getattr->ga_bmval,
  2738. resp->rqstp, 0);
  2739. return nfserr;
  2740. }
  2741. static __be32
  2742. nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
  2743. {
  2744. struct xdr_stream *xdr = &resp->xdr;
  2745. struct svc_fh *fhp = *fhpp;
  2746. unsigned int len;
  2747. __be32 *p;
  2748. if (!nfserr) {
  2749. len = fhp->fh_handle.fh_size;
  2750. p = xdr_reserve_space(xdr, len + 4);
  2751. if (!p)
  2752. return nfserr_resource;
  2753. p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
  2754. }
  2755. return nfserr;
  2756. }
  2757. /*
  2758. * Including all fields other than the name, a LOCK4denied structure requires
  2759. * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
  2760. */
  2761. static __be32
  2762. nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
  2763. {
  2764. struct xdr_netobj *conf = &ld->ld_owner;
  2765. __be32 *p;
  2766. again:
  2767. p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
  2768. if (!p) {
  2769. /*
  2770. * Don't fail to return the result just because we can't
  2771. * return the conflicting open:
  2772. */
  2773. if (conf->len) {
  2774. kfree(conf->data);
  2775. conf->len = 0;
  2776. conf->data = NULL;
  2777. goto again;
  2778. }
  2779. return nfserr_resource;
  2780. }
  2781. p = xdr_encode_hyper(p, ld->ld_start);
  2782. p = xdr_encode_hyper(p, ld->ld_length);
  2783. *p++ = cpu_to_be32(ld->ld_type);
  2784. if (conf->len) {
  2785. p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
  2786. p = xdr_encode_opaque(p, conf->data, conf->len);
  2787. kfree(conf->data);
  2788. } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
  2789. p = xdr_encode_hyper(p, (u64)0); /* clientid */
  2790. *p++ = cpu_to_be32(0); /* length of owner name */
  2791. }
  2792. return nfserr_denied;
  2793. }
  2794. static __be32
  2795. nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
  2796. {
  2797. struct xdr_stream *xdr = &resp->xdr;
  2798. if (!nfserr)
  2799. nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
  2800. else if (nfserr == nfserr_denied)
  2801. nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
  2802. return nfserr;
  2803. }
  2804. static __be32
  2805. nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
  2806. {
  2807. struct xdr_stream *xdr = &resp->xdr;
  2808. if (nfserr == nfserr_denied)
  2809. nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
  2810. return nfserr;
  2811. }
  2812. static __be32
  2813. nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
  2814. {
  2815. struct xdr_stream *xdr = &resp->xdr;
  2816. if (!nfserr)
  2817. nfserr = nfsd4_encode_stateid(xdr, &locku->lu_stateid);
  2818. return nfserr;
  2819. }
  2820. static __be32
  2821. nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
  2822. {
  2823. struct xdr_stream *xdr = &resp->xdr;
  2824. __be32 *p;
  2825. if (!nfserr) {
  2826. p = xdr_reserve_space(xdr, 20);
  2827. if (!p)
  2828. return nfserr_resource;
  2829. p = encode_cinfo(p, &link->li_cinfo);
  2830. }
  2831. return nfserr;
  2832. }
  2833. static __be32
  2834. nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
  2835. {
  2836. struct xdr_stream *xdr = &resp->xdr;
  2837. __be32 *p;
  2838. if (nfserr)
  2839. goto out;
  2840. nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
  2841. if (nfserr)
  2842. goto out;
  2843. p = xdr_reserve_space(xdr, 40);
  2844. if (!p)
  2845. return nfserr_resource;
  2846. p = encode_cinfo(p, &open->op_cinfo);
  2847. *p++ = cpu_to_be32(open->op_rflags);
  2848. *p++ = cpu_to_be32(2);
  2849. *p++ = cpu_to_be32(open->op_bmval[0]);
  2850. *p++ = cpu_to_be32(open->op_bmval[1]);
  2851. *p++ = cpu_to_be32(open->op_delegate_type);
  2852. switch (open->op_delegate_type) {
  2853. case NFS4_OPEN_DELEGATE_NONE:
  2854. break;
  2855. case NFS4_OPEN_DELEGATE_READ:
  2856. nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
  2857. if (nfserr)
  2858. return nfserr;
  2859. p = xdr_reserve_space(xdr, 20);
  2860. if (!p)
  2861. return nfserr_resource;
  2862. *p++ = cpu_to_be32(open->op_recall);
  2863. /*
  2864. * TODO: ACE's in delegations
  2865. */
  2866. *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2867. *p++ = cpu_to_be32(0);
  2868. *p++ = cpu_to_be32(0);
  2869. *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
  2870. break;
  2871. case NFS4_OPEN_DELEGATE_WRITE:
  2872. nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
  2873. if (nfserr)
  2874. return nfserr;
  2875. p = xdr_reserve_space(xdr, 32);
  2876. if (!p)
  2877. return nfserr_resource;
  2878. *p++ = cpu_to_be32(0);
  2879. /*
  2880. * TODO: space_limit's in delegations
  2881. */
  2882. *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
  2883. *p++ = cpu_to_be32(~(u32)0);
  2884. *p++ = cpu_to_be32(~(u32)0);
  2885. /*
  2886. * TODO: ACE's in delegations
  2887. */
  2888. *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2889. *p++ = cpu_to_be32(0);
  2890. *p++ = cpu_to_be32(0);
  2891. *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
  2892. break;
  2893. case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
  2894. switch (open->op_why_no_deleg) {
  2895. case WND4_CONTENTION:
  2896. case WND4_RESOURCE:
  2897. p = xdr_reserve_space(xdr, 8);
  2898. if (!p)
  2899. return nfserr_resource;
  2900. *p++ = cpu_to_be32(open->op_why_no_deleg);
  2901. /* deleg signaling not supported yet: */
  2902. *p++ = cpu_to_be32(0);
  2903. break;
  2904. default:
  2905. p = xdr_reserve_space(xdr, 4);
  2906. if (!p)
  2907. return nfserr_resource;
  2908. *p++ = cpu_to_be32(open->op_why_no_deleg);
  2909. }
  2910. break;
  2911. default:
  2912. BUG();
  2913. }
  2914. /* XXX save filehandle here */
  2915. out:
  2916. return nfserr;
  2917. }
  2918. static __be32
  2919. nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
  2920. {
  2921. struct xdr_stream *xdr = &resp->xdr;
  2922. if (!nfserr)
  2923. nfserr = nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
  2924. return nfserr;
  2925. }
  2926. static __be32
  2927. nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
  2928. {
  2929. struct xdr_stream *xdr = &resp->xdr;
  2930. if (!nfserr)
  2931. nfserr = nfsd4_encode_stateid(xdr, &od->od_stateid);
  2932. return nfserr;
  2933. }
  2934. static __be32 nfsd4_encode_splice_read(
  2935. struct nfsd4_compoundres *resp,
  2936. struct nfsd4_read *read,
  2937. struct file *file, unsigned long maxcount)
  2938. {
  2939. struct xdr_stream *xdr = &resp->xdr;
  2940. struct xdr_buf *buf = xdr->buf;
  2941. u32 eof;
  2942. int space_left;
  2943. __be32 nfserr;
  2944. __be32 *p = xdr->p - 2;
  2945. /* Make sure there will be room for padding if needed */
  2946. if (xdr->end - xdr->p < 1)
  2947. return nfserr_resource;
  2948. nfserr = nfsd_splice_read(read->rd_rqstp, file,
  2949. read->rd_offset, &maxcount);
  2950. if (nfserr) {
  2951. /*
  2952. * nfsd_splice_actor may have already messed with the
  2953. * page length; reset it so as not to confuse
  2954. * xdr_truncate_encode:
  2955. */
  2956. buf->page_len = 0;
  2957. return nfserr;
  2958. }
  2959. eof = (read->rd_offset + maxcount >=
  2960. d_inode(read->rd_fhp->fh_dentry)->i_size);
  2961. *(p++) = htonl(eof);
  2962. *(p++) = htonl(maxcount);
  2963. buf->page_len = maxcount;
  2964. buf->len += maxcount;
  2965. xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
  2966. / PAGE_SIZE;
  2967. /* Use rest of head for padding and remaining ops: */
  2968. buf->tail[0].iov_base = xdr->p;
  2969. buf->tail[0].iov_len = 0;
  2970. xdr->iov = buf->tail;
  2971. if (maxcount&3) {
  2972. int pad = 4 - (maxcount&3);
  2973. *(xdr->p++) = 0;
  2974. buf->tail[0].iov_base += maxcount&3;
  2975. buf->tail[0].iov_len = pad;
  2976. buf->len += pad;
  2977. }
  2978. space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
  2979. buf->buflen - buf->len);
  2980. buf->buflen = buf->len + space_left;
  2981. xdr->end = (__be32 *)((void *)xdr->end + space_left);
  2982. return 0;
  2983. }
  2984. static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
  2985. struct nfsd4_read *read,
  2986. struct file *file, unsigned long maxcount)
  2987. {
  2988. struct xdr_stream *xdr = &resp->xdr;
  2989. u32 eof;
  2990. int v;
  2991. int starting_len = xdr->buf->len - 8;
  2992. long len;
  2993. int thislen;
  2994. __be32 nfserr;
  2995. __be32 tmp;
  2996. __be32 *p;
  2997. u32 zzz = 0;
  2998. int pad;
  2999. len = maxcount;
  3000. v = 0;
  3001. thislen = min_t(long, len, ((void *)xdr->end - (void *)xdr->p));
  3002. p = xdr_reserve_space(xdr, (thislen+3)&~3);
  3003. WARN_ON_ONCE(!p);
  3004. resp->rqstp->rq_vec[v].iov_base = p;
  3005. resp->rqstp->rq_vec[v].iov_len = thislen;
  3006. v++;
  3007. len -= thislen;
  3008. while (len) {
  3009. thislen = min_t(long, len, PAGE_SIZE);
  3010. p = xdr_reserve_space(xdr, (thislen+3)&~3);
  3011. WARN_ON_ONCE(!p);
  3012. resp->rqstp->rq_vec[v].iov_base = p;
  3013. resp->rqstp->rq_vec[v].iov_len = thislen;
  3014. v++;
  3015. len -= thislen;
  3016. }
  3017. read->rd_vlen = v;
  3018. nfserr = nfsd_readv(file, read->rd_offset, resp->rqstp->rq_vec,
  3019. read->rd_vlen, &maxcount);
  3020. if (nfserr)
  3021. return nfserr;
  3022. xdr_truncate_encode(xdr, starting_len + 8 + ((maxcount+3)&~3));
  3023. eof = (read->rd_offset + maxcount >=
  3024. d_inode(read->rd_fhp->fh_dentry)->i_size);
  3025. tmp = htonl(eof);
  3026. write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
  3027. tmp = htonl(maxcount);
  3028. write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
  3029. pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
  3030. write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
  3031. &zzz, pad);
  3032. return 0;
  3033. }
  3034. static __be32
  3035. nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
  3036. struct nfsd4_read *read)
  3037. {
  3038. unsigned long maxcount;
  3039. struct xdr_stream *xdr = &resp->xdr;
  3040. struct file *file = read->rd_filp;
  3041. int starting_len = xdr->buf->len;
  3042. struct raparms *ra = NULL;
  3043. __be32 *p;
  3044. if (nfserr)
  3045. goto out;
  3046. p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
  3047. if (!p) {
  3048. WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
  3049. nfserr = nfserr_resource;
  3050. goto out;
  3051. }
  3052. if (resp->xdr.buf->page_len &&
  3053. test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
  3054. WARN_ON_ONCE(1);
  3055. nfserr = nfserr_resource;
  3056. goto out;
  3057. }
  3058. xdr_commit_encode(xdr);
  3059. maxcount = svc_max_payload(resp->rqstp);
  3060. maxcount = min_t(unsigned long, maxcount,
  3061. (xdr->buf->buflen - xdr->buf->len));
  3062. maxcount = min_t(unsigned long, maxcount, read->rd_length);
  3063. if (read->rd_tmp_file)
  3064. ra = nfsd_init_raparms(file);
  3065. if (file->f_op->splice_read &&
  3066. test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
  3067. nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
  3068. else
  3069. nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
  3070. if (ra)
  3071. nfsd_put_raparams(file, ra);
  3072. if (nfserr)
  3073. xdr_truncate_encode(xdr, starting_len);
  3074. out:
  3075. if (file)
  3076. fput(file);
  3077. return nfserr;
  3078. }
  3079. static __be32
  3080. nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
  3081. {
  3082. int maxcount;
  3083. __be32 wire_count;
  3084. int zero = 0;
  3085. struct xdr_stream *xdr = &resp->xdr;
  3086. int length_offset = xdr->buf->len;
  3087. __be32 *p;
  3088. if (nfserr)
  3089. return nfserr;
  3090. p = xdr_reserve_space(xdr, 4);
  3091. if (!p)
  3092. return nfserr_resource;
  3093. maxcount = PAGE_SIZE;
  3094. p = xdr_reserve_space(xdr, maxcount);
  3095. if (!p)
  3096. return nfserr_resource;
  3097. /*
  3098. * XXX: By default, the ->readlink() VFS op will truncate symlinks
  3099. * if they would overflow the buffer. Is this kosher in NFSv4? If
  3100. * not, one easy fix is: if ->readlink() precisely fills the buffer,
  3101. * assume that truncation occurred, and return NFS4ERR_RESOURCE.
  3102. */
  3103. nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
  3104. (char *)p, &maxcount);
  3105. if (nfserr == nfserr_isdir)
  3106. nfserr = nfserr_inval;
  3107. if (nfserr) {
  3108. xdr_truncate_encode(xdr, length_offset);
  3109. return nfserr;
  3110. }
  3111. wire_count = htonl(maxcount);
  3112. write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
  3113. xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
  3114. if (maxcount & 3)
  3115. write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
  3116. &zero, 4 - (maxcount&3));
  3117. return 0;
  3118. }
  3119. static __be32
  3120. nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
  3121. {
  3122. int maxcount;
  3123. int bytes_left;
  3124. loff_t offset;
  3125. __be64 wire_offset;
  3126. struct xdr_stream *xdr = &resp->xdr;
  3127. int starting_len = xdr->buf->len;
  3128. __be32 *p;
  3129. if (nfserr)
  3130. return nfserr;
  3131. p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
  3132. if (!p)
  3133. return nfserr_resource;
  3134. /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
  3135. *p++ = cpu_to_be32(0);
  3136. *p++ = cpu_to_be32(0);
  3137. resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
  3138. - (char *)resp->xdr.buf->head[0].iov_base;
  3139. /*
  3140. * Number of bytes left for directory entries allowing for the
  3141. * final 8 bytes of the readdir and a following failed op:
  3142. */
  3143. bytes_left = xdr->buf->buflen - xdr->buf->len
  3144. - COMPOUND_ERR_SLACK_SPACE - 8;
  3145. if (bytes_left < 0) {
  3146. nfserr = nfserr_resource;
  3147. goto err_no_verf;
  3148. }
  3149. maxcount = min_t(u32, readdir->rd_maxcount, INT_MAX);
  3150. /*
  3151. * Note the rfc defines rd_maxcount as the size of the
  3152. * READDIR4resok structure, which includes the verifier above
  3153. * and the 8 bytes encoded at the end of this function:
  3154. */
  3155. if (maxcount < 16) {
  3156. nfserr = nfserr_toosmall;
  3157. goto err_no_verf;
  3158. }
  3159. maxcount = min_t(int, maxcount-16, bytes_left);
  3160. /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
  3161. if (!readdir->rd_dircount)
  3162. readdir->rd_dircount = INT_MAX;
  3163. readdir->xdr = xdr;
  3164. readdir->rd_maxcount = maxcount;
  3165. readdir->common.err = 0;
  3166. readdir->cookie_offset = 0;
  3167. offset = readdir->rd_cookie;
  3168. nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
  3169. &offset,
  3170. &readdir->common, nfsd4_encode_dirent);
  3171. if (nfserr == nfs_ok &&
  3172. readdir->common.err == nfserr_toosmall &&
  3173. xdr->buf->len == starting_len + 8) {
  3174. /* nothing encoded; which limit did we hit?: */
  3175. if (maxcount - 16 < bytes_left)
  3176. /* It was the fault of rd_maxcount: */
  3177. nfserr = nfserr_toosmall;
  3178. else
  3179. /* We ran out of buffer space: */
  3180. nfserr = nfserr_resource;
  3181. }
  3182. if (nfserr)
  3183. goto err_no_verf;
  3184. if (readdir->cookie_offset) {
  3185. wire_offset = cpu_to_be64(offset);
  3186. write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
  3187. &wire_offset, 8);
  3188. }
  3189. p = xdr_reserve_space(xdr, 8);
  3190. if (!p) {
  3191. WARN_ON_ONCE(1);
  3192. goto err_no_verf;
  3193. }
  3194. *p++ = 0; /* no more entries */
  3195. *p++ = htonl(readdir->common.err == nfserr_eof);
  3196. return 0;
  3197. err_no_verf:
  3198. xdr_truncate_encode(xdr, starting_len);
  3199. return nfserr;
  3200. }
  3201. static __be32
  3202. nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
  3203. {
  3204. struct xdr_stream *xdr = &resp->xdr;
  3205. __be32 *p;
  3206. if (!nfserr) {
  3207. p = xdr_reserve_space(xdr, 20);
  3208. if (!p)
  3209. return nfserr_resource;
  3210. p = encode_cinfo(p, &remove->rm_cinfo);
  3211. }
  3212. return nfserr;
  3213. }
  3214. static __be32
  3215. nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
  3216. {
  3217. struct xdr_stream *xdr = &resp->xdr;
  3218. __be32 *p;
  3219. if (!nfserr) {
  3220. p = xdr_reserve_space(xdr, 40);
  3221. if (!p)
  3222. return nfserr_resource;
  3223. p = encode_cinfo(p, &rename->rn_sinfo);
  3224. p = encode_cinfo(p, &rename->rn_tinfo);
  3225. }
  3226. return nfserr;
  3227. }
  3228. static __be32
  3229. nfsd4_do_encode_secinfo(struct xdr_stream *xdr,
  3230. __be32 nfserr, struct svc_export *exp)
  3231. {
  3232. u32 i, nflavs, supported;
  3233. struct exp_flavor_info *flavs;
  3234. struct exp_flavor_info def_flavs[2];
  3235. __be32 *p, *flavorsp;
  3236. static bool report = true;
  3237. if (nfserr)
  3238. goto out;
  3239. nfserr = nfserr_resource;
  3240. if (exp->ex_nflavors) {
  3241. flavs = exp->ex_flavors;
  3242. nflavs = exp->ex_nflavors;
  3243. } else { /* Handling of some defaults in absence of real secinfo: */
  3244. flavs = def_flavs;
  3245. if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
  3246. nflavs = 2;
  3247. flavs[0].pseudoflavor = RPC_AUTH_UNIX;
  3248. flavs[1].pseudoflavor = RPC_AUTH_NULL;
  3249. } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
  3250. nflavs = 1;
  3251. flavs[0].pseudoflavor
  3252. = svcauth_gss_flavor(exp->ex_client);
  3253. } else {
  3254. nflavs = 1;
  3255. flavs[0].pseudoflavor
  3256. = exp->ex_client->flavour->flavour;
  3257. }
  3258. }
  3259. supported = 0;
  3260. p = xdr_reserve_space(xdr, 4);
  3261. if (!p)
  3262. goto out;
  3263. flavorsp = p++; /* to be backfilled later */
  3264. for (i = 0; i < nflavs; i++) {
  3265. rpc_authflavor_t pf = flavs[i].pseudoflavor;
  3266. struct rpcsec_gss_info info;
  3267. if (rpcauth_get_gssinfo(pf, &info) == 0) {
  3268. supported++;
  3269. p = xdr_reserve_space(xdr, 4 + 4 +
  3270. XDR_LEN(info.oid.len) + 4 + 4);
  3271. if (!p)
  3272. goto out;
  3273. *p++ = cpu_to_be32(RPC_AUTH_GSS);
  3274. p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
  3275. *p++ = cpu_to_be32(info.qop);
  3276. *p++ = cpu_to_be32(info.service);
  3277. } else if (pf < RPC_AUTH_MAXFLAVOR) {
  3278. supported++;
  3279. p = xdr_reserve_space(xdr, 4);
  3280. if (!p)
  3281. goto out;
  3282. *p++ = cpu_to_be32(pf);
  3283. } else {
  3284. if (report)
  3285. pr_warn("NFS: SECINFO: security flavor %u "
  3286. "is not supported\n", pf);
  3287. }
  3288. }
  3289. if (nflavs != supported)
  3290. report = false;
  3291. *flavorsp = htonl(supported);
  3292. nfserr = 0;
  3293. out:
  3294. if (exp)
  3295. exp_put(exp);
  3296. return nfserr;
  3297. }
  3298. static __be32
  3299. nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  3300. struct nfsd4_secinfo *secinfo)
  3301. {
  3302. struct xdr_stream *xdr = &resp->xdr;
  3303. return nfsd4_do_encode_secinfo(xdr, nfserr, secinfo->si_exp);
  3304. }
  3305. static __be32
  3306. nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
  3307. struct nfsd4_secinfo_no_name *secinfo)
  3308. {
  3309. struct xdr_stream *xdr = &resp->xdr;
  3310. return nfsd4_do_encode_secinfo(xdr, nfserr, secinfo->sin_exp);
  3311. }
  3312. /*
  3313. * The SETATTR encode routine is special -- it always encodes a bitmap,
  3314. * regardless of the error status.
  3315. */
  3316. static __be32
  3317. nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
  3318. {
  3319. struct xdr_stream *xdr = &resp->xdr;
  3320. __be32 *p;
  3321. p = xdr_reserve_space(xdr, 16);
  3322. if (!p)
  3323. return nfserr_resource;
  3324. if (nfserr) {
  3325. *p++ = cpu_to_be32(3);
  3326. *p++ = cpu_to_be32(0);
  3327. *p++ = cpu_to_be32(0);
  3328. *p++ = cpu_to_be32(0);
  3329. }
  3330. else {
  3331. *p++ = cpu_to_be32(3);
  3332. *p++ = cpu_to_be32(setattr->sa_bmval[0]);
  3333. *p++ = cpu_to_be32(setattr->sa_bmval[1]);
  3334. *p++ = cpu_to_be32(setattr->sa_bmval[2]);
  3335. }
  3336. return nfserr;
  3337. }
  3338. static __be32
  3339. nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
  3340. {
  3341. struct xdr_stream *xdr = &resp->xdr;
  3342. __be32 *p;
  3343. if (!nfserr) {
  3344. p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
  3345. if (!p)
  3346. return nfserr_resource;
  3347. p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
  3348. p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
  3349. NFS4_VERIFIER_SIZE);
  3350. }
  3351. else if (nfserr == nfserr_clid_inuse) {
  3352. p = xdr_reserve_space(xdr, 8);
  3353. if (!p)
  3354. return nfserr_resource;
  3355. *p++ = cpu_to_be32(0);
  3356. *p++ = cpu_to_be32(0);
  3357. }
  3358. return nfserr;
  3359. }
  3360. static __be32
  3361. nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
  3362. {
  3363. struct xdr_stream *xdr = &resp->xdr;
  3364. __be32 *p;
  3365. if (!nfserr) {
  3366. p = xdr_reserve_space(xdr, 16);
  3367. if (!p)
  3368. return nfserr_resource;
  3369. *p++ = cpu_to_be32(write->wr_bytes_written);
  3370. *p++ = cpu_to_be32(write->wr_how_written);
  3371. p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
  3372. NFS4_VERIFIER_SIZE);
  3373. }
  3374. return nfserr;
  3375. }
  3376. static const u32 nfs4_minimal_spo_must_enforce[2] = {
  3377. [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  3378. 1 << (OP_EXCHANGE_ID - 32) |
  3379. 1 << (OP_CREATE_SESSION - 32) |
  3380. 1 << (OP_DESTROY_SESSION - 32) |
  3381. 1 << (OP_DESTROY_CLIENTID - 32)
  3382. };
  3383. static __be32
  3384. nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
  3385. struct nfsd4_exchange_id *exid)
  3386. {
  3387. struct xdr_stream *xdr = &resp->xdr;
  3388. __be32 *p;
  3389. char *major_id;
  3390. char *server_scope;
  3391. int major_id_sz;
  3392. int server_scope_sz;
  3393. uint64_t minor_id = 0;
  3394. if (nfserr)
  3395. return nfserr;
  3396. major_id = utsname()->nodename;
  3397. major_id_sz = strlen(major_id);
  3398. server_scope = utsname()->nodename;
  3399. server_scope_sz = strlen(server_scope);
  3400. p = xdr_reserve_space(xdr,
  3401. 8 /* eir_clientid */ +
  3402. 4 /* eir_sequenceid */ +
  3403. 4 /* eir_flags */ +
  3404. 4 /* spr_how */);
  3405. if (!p)
  3406. return nfserr_resource;
  3407. p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
  3408. *p++ = cpu_to_be32(exid->seqid);
  3409. *p++ = cpu_to_be32(exid->flags);
  3410. *p++ = cpu_to_be32(exid->spa_how);
  3411. switch (exid->spa_how) {
  3412. case SP4_NONE:
  3413. break;
  3414. case SP4_MACH_CRED:
  3415. /* spo_must_enforce, spo_must_allow */
  3416. p = xdr_reserve_space(xdr, 16);
  3417. if (!p)
  3418. return nfserr_resource;
  3419. /* spo_must_enforce bitmap: */
  3420. *p++ = cpu_to_be32(2);
  3421. *p++ = cpu_to_be32(nfs4_minimal_spo_must_enforce[0]);
  3422. *p++ = cpu_to_be32(nfs4_minimal_spo_must_enforce[1]);
  3423. /* empty spo_must_allow bitmap: */
  3424. *p++ = cpu_to_be32(0);
  3425. break;
  3426. default:
  3427. WARN_ON_ONCE(1);
  3428. }
  3429. p = xdr_reserve_space(xdr,
  3430. 8 /* so_minor_id */ +
  3431. 4 /* so_major_id.len */ +
  3432. (XDR_QUADLEN(major_id_sz) * 4) +
  3433. 4 /* eir_server_scope.len */ +
  3434. (XDR_QUADLEN(server_scope_sz) * 4) +
  3435. 4 /* eir_server_impl_id.count (0) */);
  3436. if (!p)
  3437. return nfserr_resource;
  3438. /* The server_owner struct */
  3439. p = xdr_encode_hyper(p, minor_id); /* Minor id */
  3440. /* major id */
  3441. p = xdr_encode_opaque(p, major_id, major_id_sz);
  3442. /* Server scope */
  3443. p = xdr_encode_opaque(p, server_scope, server_scope_sz);
  3444. /* Implementation id */
  3445. *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
  3446. return 0;
  3447. }
  3448. static __be32
  3449. nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
  3450. struct nfsd4_create_session *sess)
  3451. {
  3452. struct xdr_stream *xdr = &resp->xdr;
  3453. __be32 *p;
  3454. if (nfserr)
  3455. return nfserr;
  3456. p = xdr_reserve_space(xdr, 24);
  3457. if (!p)
  3458. return nfserr_resource;
  3459. p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
  3460. NFS4_MAX_SESSIONID_LEN);
  3461. *p++ = cpu_to_be32(sess->seqid);
  3462. *p++ = cpu_to_be32(sess->flags);
  3463. p = xdr_reserve_space(xdr, 28);
  3464. if (!p)
  3465. return nfserr_resource;
  3466. *p++ = cpu_to_be32(0); /* headerpadsz */
  3467. *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
  3468. *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
  3469. *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
  3470. *p++ = cpu_to_be32(sess->fore_channel.maxops);
  3471. *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
  3472. *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
  3473. if (sess->fore_channel.nr_rdma_attrs) {
  3474. p = xdr_reserve_space(xdr, 4);
  3475. if (!p)
  3476. return nfserr_resource;
  3477. *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
  3478. }
  3479. p = xdr_reserve_space(xdr, 28);
  3480. if (!p)
  3481. return nfserr_resource;
  3482. *p++ = cpu_to_be32(0); /* headerpadsz */
  3483. *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
  3484. *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
  3485. *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
  3486. *p++ = cpu_to_be32(sess->back_channel.maxops);
  3487. *p++ = cpu_to_be32(sess->back_channel.maxreqs);
  3488. *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
  3489. if (sess->back_channel.nr_rdma_attrs) {
  3490. p = xdr_reserve_space(xdr, 4);
  3491. if (!p)
  3492. return nfserr_resource;
  3493. *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
  3494. }
  3495. return 0;
  3496. }
  3497. static __be32
  3498. nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
  3499. struct nfsd4_sequence *seq)
  3500. {
  3501. struct xdr_stream *xdr = &resp->xdr;
  3502. __be32 *p;
  3503. if (nfserr)
  3504. return nfserr;
  3505. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
  3506. if (!p)
  3507. return nfserr_resource;
  3508. p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
  3509. NFS4_MAX_SESSIONID_LEN);
  3510. *p++ = cpu_to_be32(seq->seqid);
  3511. *p++ = cpu_to_be32(seq->slotid);
  3512. /* Note slotid's are numbered from zero: */
  3513. *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
  3514. *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
  3515. *p++ = cpu_to_be32(seq->status_flags);
  3516. resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
  3517. return 0;
  3518. }
  3519. static __be32
  3520. nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
  3521. struct nfsd4_test_stateid *test_stateid)
  3522. {
  3523. struct xdr_stream *xdr = &resp->xdr;
  3524. struct nfsd4_test_stateid_id *stateid, *next;
  3525. __be32 *p;
  3526. if (nfserr)
  3527. return nfserr;
  3528. p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
  3529. if (!p)
  3530. return nfserr_resource;
  3531. *p++ = htonl(test_stateid->ts_num_ids);
  3532. list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
  3533. *p++ = stateid->ts_id_status;
  3534. }
  3535. return nfserr;
  3536. }
  3537. #ifdef CONFIG_NFSD_PNFS
  3538. static __be32
  3539. nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  3540. struct nfsd4_getdeviceinfo *gdev)
  3541. {
  3542. struct xdr_stream *xdr = &resp->xdr;
  3543. const struct nfsd4_layout_ops *ops =
  3544. nfsd4_layout_ops[gdev->gd_layout_type];
  3545. u32 starting_len = xdr->buf->len, needed_len;
  3546. __be32 *p;
  3547. dprintk("%s: err %d\n", __func__, nfserr);
  3548. if (nfserr)
  3549. goto out;
  3550. nfserr = nfserr_resource;
  3551. p = xdr_reserve_space(xdr, 4);
  3552. if (!p)
  3553. goto out;
  3554. *p++ = cpu_to_be32(gdev->gd_layout_type);
  3555. /* If maxcount is 0 then just update notifications */
  3556. if (gdev->gd_maxcount != 0) {
  3557. nfserr = ops->encode_getdeviceinfo(xdr, gdev);
  3558. if (nfserr) {
  3559. /*
  3560. * We don't bother to burden the layout drivers with
  3561. * enforcing gd_maxcount, just tell the client to
  3562. * come back with a bigger buffer if it's not enough.
  3563. */
  3564. if (xdr->buf->len + 4 > gdev->gd_maxcount)
  3565. goto toosmall;
  3566. goto out;
  3567. }
  3568. }
  3569. nfserr = nfserr_resource;
  3570. if (gdev->gd_notify_types) {
  3571. p = xdr_reserve_space(xdr, 4 + 4);
  3572. if (!p)
  3573. goto out;
  3574. *p++ = cpu_to_be32(1); /* bitmap length */
  3575. *p++ = cpu_to_be32(gdev->gd_notify_types);
  3576. } else {
  3577. p = xdr_reserve_space(xdr, 4);
  3578. if (!p)
  3579. goto out;
  3580. *p++ = 0;
  3581. }
  3582. nfserr = 0;
  3583. out:
  3584. kfree(gdev->gd_device);
  3585. dprintk("%s: done: %d\n", __func__, be32_to_cpu(nfserr));
  3586. return nfserr;
  3587. toosmall:
  3588. dprintk("%s: maxcount too small\n", __func__);
  3589. needed_len = xdr->buf->len + 4 /* notifications */;
  3590. xdr_truncate_encode(xdr, starting_len);
  3591. p = xdr_reserve_space(xdr, 4);
  3592. if (!p) {
  3593. nfserr = nfserr_resource;
  3594. } else {
  3595. *p++ = cpu_to_be32(needed_len);
  3596. nfserr = nfserr_toosmall;
  3597. }
  3598. goto out;
  3599. }
  3600. static __be32
  3601. nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
  3602. struct nfsd4_layoutget *lgp)
  3603. {
  3604. struct xdr_stream *xdr = &resp->xdr;
  3605. const struct nfsd4_layout_ops *ops =
  3606. nfsd4_layout_ops[lgp->lg_layout_type];
  3607. __be32 *p;
  3608. dprintk("%s: err %d\n", __func__, nfserr);
  3609. if (nfserr)
  3610. goto out;
  3611. nfserr = nfserr_resource;
  3612. p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
  3613. if (!p)
  3614. goto out;
  3615. *p++ = cpu_to_be32(1); /* we always set return-on-close */
  3616. *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
  3617. p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
  3618. sizeof(stateid_opaque_t));
  3619. *p++ = cpu_to_be32(1); /* we always return a single layout */
  3620. p = xdr_encode_hyper(p, lgp->lg_seg.offset);
  3621. p = xdr_encode_hyper(p, lgp->lg_seg.length);
  3622. *p++ = cpu_to_be32(lgp->lg_seg.iomode);
  3623. *p++ = cpu_to_be32(lgp->lg_layout_type);
  3624. nfserr = ops->encode_layoutget(xdr, lgp);
  3625. out:
  3626. kfree(lgp->lg_content);
  3627. return nfserr;
  3628. }
  3629. static __be32
  3630. nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
  3631. struct nfsd4_layoutcommit *lcp)
  3632. {
  3633. struct xdr_stream *xdr = &resp->xdr;
  3634. __be32 *p;
  3635. if (nfserr)
  3636. return nfserr;
  3637. p = xdr_reserve_space(xdr, 4);
  3638. if (!p)
  3639. return nfserr_resource;
  3640. *p++ = cpu_to_be32(lcp->lc_size_chg);
  3641. if (lcp->lc_size_chg) {
  3642. p = xdr_reserve_space(xdr, 8);
  3643. if (!p)
  3644. return nfserr_resource;
  3645. p = xdr_encode_hyper(p, lcp->lc_newsize);
  3646. }
  3647. return nfs_ok;
  3648. }
  3649. static __be32
  3650. nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
  3651. struct nfsd4_layoutreturn *lrp)
  3652. {
  3653. struct xdr_stream *xdr = &resp->xdr;
  3654. __be32 *p;
  3655. if (nfserr)
  3656. return nfserr;
  3657. p = xdr_reserve_space(xdr, 4);
  3658. if (!p)
  3659. return nfserr_resource;
  3660. *p++ = cpu_to_be32(lrp->lrs_present);
  3661. if (lrp->lrs_present)
  3662. return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
  3663. return nfs_ok;
  3664. }
  3665. #endif /* CONFIG_NFSD_PNFS */
  3666. static __be32
  3667. nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
  3668. struct nfsd4_seek *seek)
  3669. {
  3670. __be32 *p;
  3671. if (nfserr)
  3672. return nfserr;
  3673. p = xdr_reserve_space(&resp->xdr, 4 + 8);
  3674. *p++ = cpu_to_be32(seek->seek_eof);
  3675. p = xdr_encode_hyper(p, seek->seek_pos);
  3676. return nfserr;
  3677. }
  3678. static __be32
  3679. nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
  3680. {
  3681. return nfserr;
  3682. }
  3683. typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
  3684. /*
  3685. * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
  3686. * since we don't need to filter out obsolete ops as this is
  3687. * done in the decoding phase.
  3688. */
  3689. static nfsd4_enc nfsd4_enc_ops[] = {
  3690. [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
  3691. [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
  3692. [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
  3693. [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
  3694. [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
  3695. [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  3696. [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
  3697. [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
  3698. [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
  3699. [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
  3700. [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
  3701. [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
  3702. [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
  3703. [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
  3704. [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3705. [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
  3706. [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
  3707. [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
  3708. [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
  3709. [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
  3710. [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
  3711. [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
  3712. [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
  3713. [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
  3714. [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
  3715. [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
  3716. [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
  3717. [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
  3718. [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
  3719. [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
  3720. [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
  3721. [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
  3722. [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
  3723. [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
  3724. [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3725. [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
  3726. [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
  3727. /* NFSv4.1 operations */
  3728. [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
  3729. [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
  3730. [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
  3731. [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
  3732. [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
  3733. [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
  3734. [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  3735. #ifdef CONFIG_NFSD_PNFS
  3736. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
  3737. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  3738. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
  3739. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
  3740. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
  3741. #else
  3742. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
  3743. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  3744. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
  3745. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
  3746. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  3747. #endif
  3748. [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
  3749. [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
  3750. [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
  3751. [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
  3752. [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  3753. [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
  3754. [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
  3755. /* NFSv4.2 operations */
  3756. [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
  3757. [OP_COPY] = (nfsd4_enc)nfsd4_encode_noop,
  3758. [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3759. [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
  3760. [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
  3761. [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
  3762. [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
  3763. [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
  3764. [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_noop,
  3765. [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_noop,
  3766. [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
  3767. [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
  3768. };
  3769. /*
  3770. * Calculate whether we still have space to encode repsize bytes.
  3771. * There are two considerations:
  3772. * - For NFS versions >=4.1, the size of the reply must stay within
  3773. * session limits
  3774. * - For all NFS versions, we must stay within limited preallocated
  3775. * buffer space.
  3776. *
  3777. * This is called before the operation is processed, so can only provide
  3778. * an upper estimate. For some nonidempotent operations (such as
  3779. * getattr), it's not necessarily a problem if that estimate is wrong,
  3780. * as we can fail it after processing without significant side effects.
  3781. */
  3782. __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
  3783. {
  3784. struct xdr_buf *buf = &resp->rqstp->rq_res;
  3785. struct nfsd4_slot *slot = resp->cstate.slot;
  3786. if (buf->len + respsize <= buf->buflen)
  3787. return nfs_ok;
  3788. if (!nfsd4_has_session(&resp->cstate))
  3789. return nfserr_resource;
  3790. if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
  3791. WARN_ON_ONCE(1);
  3792. return nfserr_rep_too_big_to_cache;
  3793. }
  3794. return nfserr_rep_too_big;
  3795. }
  3796. void
  3797. nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  3798. {
  3799. struct xdr_stream *xdr = &resp->xdr;
  3800. struct nfs4_stateowner *so = resp->cstate.replay_owner;
  3801. struct svc_rqst *rqstp = resp->rqstp;
  3802. int post_err_offset;
  3803. nfsd4_enc encoder;
  3804. __be32 *p;
  3805. p = xdr_reserve_space(xdr, 8);
  3806. if (!p) {
  3807. WARN_ON_ONCE(1);
  3808. return;
  3809. }
  3810. *p++ = cpu_to_be32(op->opnum);
  3811. post_err_offset = xdr->buf->len;
  3812. if (op->opnum == OP_ILLEGAL)
  3813. goto status;
  3814. BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
  3815. !nfsd4_enc_ops[op->opnum]);
  3816. encoder = nfsd4_enc_ops[op->opnum];
  3817. op->status = encoder(resp, op->status, &op->u);
  3818. xdr_commit_encode(xdr);
  3819. /* nfsd4_check_resp_size guarantees enough room for error status */
  3820. if (!op->status) {
  3821. int space_needed = 0;
  3822. if (!nfsd4_last_compound_op(rqstp))
  3823. space_needed = COMPOUND_ERR_SLACK_SPACE;
  3824. op->status = nfsd4_check_resp_size(resp, space_needed);
  3825. }
  3826. if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
  3827. struct nfsd4_slot *slot = resp->cstate.slot;
  3828. if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
  3829. op->status = nfserr_rep_too_big_to_cache;
  3830. else
  3831. op->status = nfserr_rep_too_big;
  3832. }
  3833. if (op->status == nfserr_resource ||
  3834. op->status == nfserr_rep_too_big ||
  3835. op->status == nfserr_rep_too_big_to_cache) {
  3836. /*
  3837. * The operation may have already been encoded or
  3838. * partially encoded. No op returns anything additional
  3839. * in the case of one of these three errors, so we can
  3840. * just truncate back to after the status. But it's a
  3841. * bug if we had to do this on a non-idempotent op:
  3842. */
  3843. warn_on_nonidempotent_op(op);
  3844. xdr_truncate_encode(xdr, post_err_offset);
  3845. }
  3846. if (so) {
  3847. int len = xdr->buf->len - post_err_offset;
  3848. so->so_replay.rp_status = op->status;
  3849. so->so_replay.rp_buflen = len;
  3850. read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
  3851. so->so_replay.rp_buf, len);
  3852. }
  3853. status:
  3854. /* Note that op->status is already in network byte order: */
  3855. write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
  3856. }
  3857. /*
  3858. * Encode the reply stored in the stateowner reply cache
  3859. *
  3860. * XDR note: do not encode rp->rp_buflen: the buffer contains the
  3861. * previously sent already encoded operation.
  3862. */
  3863. void
  3864. nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
  3865. {
  3866. __be32 *p;
  3867. struct nfs4_replay *rp = op->replay;
  3868. BUG_ON(!rp);
  3869. p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
  3870. if (!p) {
  3871. WARN_ON_ONCE(1);
  3872. return;
  3873. }
  3874. *p++ = cpu_to_be32(op->opnum);
  3875. *p++ = rp->rp_status; /* already xdr'ed */
  3876. p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
  3877. }
  3878. int
  3879. nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  3880. {
  3881. return xdr_ressize_check(rqstp, p);
  3882. }
  3883. int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
  3884. {
  3885. struct svc_rqst *rqstp = rq;
  3886. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  3887. if (args->ops != args->iops) {
  3888. kfree(args->ops);
  3889. args->ops = args->iops;
  3890. }
  3891. kfree(args->tmpp);
  3892. args->tmpp = NULL;
  3893. while (args->to_free) {
  3894. struct svcxdr_tmpbuf *tb = args->to_free;
  3895. args->to_free = tb->next;
  3896. kfree(tb);
  3897. }
  3898. return 1;
  3899. }
  3900. int
  3901. nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
  3902. {
  3903. if (rqstp->rq_arg.head[0].iov_len % 4) {
  3904. /* client is nuts */
  3905. dprintk("%s: compound not properly padded! (peeraddr=%pISc xid=0x%x)",
  3906. __func__, svc_addr(rqstp), be32_to_cpu(rqstp->rq_xid));
  3907. return 0;
  3908. }
  3909. args->p = p;
  3910. args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
  3911. args->pagelist = rqstp->rq_arg.pages;
  3912. args->pagelen = rqstp->rq_arg.page_len;
  3913. args->tmpp = NULL;
  3914. args->to_free = NULL;
  3915. args->ops = args->iops;
  3916. args->rqstp = rqstp;
  3917. return !nfsd4_decode_compound(args);
  3918. }
  3919. int
  3920. nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
  3921. {
  3922. /*
  3923. * All that remains is to write the tag and operation count...
  3924. */
  3925. struct xdr_buf *buf = resp->xdr.buf;
  3926. WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
  3927. buf->tail[0].iov_len);
  3928. rqstp->rq_next_page = resp->xdr.page_ptr + 1;
  3929. p = resp->tagp;
  3930. *p++ = htonl(resp->taglen);
  3931. memcpy(p, resp->tag, resp->taglen);
  3932. p += XDR_QUADLEN(resp->taglen);
  3933. *p++ = htonl(resp->opcnt);
  3934. nfsd4_sequence_done(resp);
  3935. return 1;
  3936. }
  3937. /*
  3938. * Local variables:
  3939. * c-basic-offset: 8
  3940. * End:
  3941. */