nfs4xdr.c 89 KB

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