nfs4xdr.c 61 KB

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