nfs4xdr.c 104 KB

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