nfs4xdr.c 91 KB

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