nfs4xdr.c 62 KB

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