nfs4xdr.c 82 KB

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