nfs4xdr.c 92 KB

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