nfs4xdr.c 91 KB

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