nfs4xdr.c 62 KB

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