nfs4xdr.c 66 KB

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