nfs4xdr.c 90 KB

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