nfs4xdr.c 89 KB

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