nfs4xdr.c 68 KB

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