nfs4xdr.c 66 KB

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