nfs4xdr.c 69 KB

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