nfs4xdr.c 68 KB

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