nfs4xdr.c 103 KB

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