nfs4xdr.c 84 KB

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