nfs4xdr.c 80 KB

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