nfs4xdr.c 68 KB

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