nfs4xdr.c 91 KB

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