nfs4xdr.c 69 KB

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