nfs4xdr.c 66 KB

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