nfs4xdr.c 92 KB

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