nfs4xdr.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409
  1. /*
  2. * Server-side XDR for NFSv4
  3. *
  4. * Copyright (c) 2002 The Regents of the University of Michigan.
  5. * All rights reserved.
  6. *
  7. * Kendrick Smith <kmsmith@umich.edu>
  8. * Andy Adamson <andros@umich.edu>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * TODO: Neil Brown made the following observation: We currently
  36. * initially reserve NFSD_BUFSIZE space on the transmit queue and
  37. * never release any of that until the request is complete.
  38. * It would be good to calculate a new maximum response size while
  39. * decoding the COMPOUND, and call svc_reserve with this number
  40. * at the end of nfs4svc_decode_compoundargs.
  41. */
  42. #include <linux/slab.h>
  43. #include <linux/namei.h>
  44. #include <linux/statfs.h>
  45. #include <linux/utsname.h>
  46. #include <linux/sunrpc/svcauth_gss.h>
  47. #include "idmap.h"
  48. #include "acl.h"
  49. #include "xdr4.h"
  50. #include "vfs.h"
  51. #define NFSDDBG_FACILITY NFSDDBG_XDR
  52. /*
  53. * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
  54. * directory in order to indicate to the client that a filesystem boundary is present
  55. * We use a fixed fsid for a referral
  56. */
  57. #define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
  58. #define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
  59. static __be32
  60. check_filename(char *str, int len, __be32 err)
  61. {
  62. int i;
  63. if (len == 0)
  64. return nfserr_inval;
  65. if (isdotent(str, len))
  66. return err;
  67. for (i = 0; i < len; i++)
  68. if (str[i] == '/')
  69. return err;
  70. return 0;
  71. }
  72. #define DECODE_HEAD \
  73. __be32 *p; \
  74. __be32 status
  75. #define DECODE_TAIL \
  76. status = 0; \
  77. out: \
  78. return status; \
  79. xdr_error: \
  80. dprintk("NFSD: xdr error (%s:%d)\n", \
  81. __FILE__, __LINE__); \
  82. status = nfserr_bad_xdr; \
  83. goto out
  84. #define READ32(x) (x) = ntohl(*p++)
  85. #define READ64(x) do { \
  86. (x) = (u64)ntohl(*p++) << 32; \
  87. (x) |= ntohl(*p++); \
  88. } while (0)
  89. #define READTIME(x) do { \
  90. p++; \
  91. (x) = ntohl(*p++); \
  92. p++; \
  93. } while (0)
  94. #define READMEM(x,nbytes) do { \
  95. x = (char *)p; \
  96. p += XDR_QUADLEN(nbytes); \
  97. } while (0)
  98. #define SAVEMEM(x,nbytes) do { \
  99. if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
  100. savemem(argp, p, nbytes) : \
  101. (char *)p)) { \
  102. dprintk("NFSD: xdr error (%s:%d)\n", \
  103. __FILE__, __LINE__); \
  104. goto xdr_error; \
  105. } \
  106. p += XDR_QUADLEN(nbytes); \
  107. } while (0)
  108. #define COPYMEM(x,nbytes) do { \
  109. memcpy((x), p, nbytes); \
  110. p += XDR_QUADLEN(nbytes); \
  111. } while (0)
  112. /* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
  113. #define READ_BUF(nbytes) do { \
  114. if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
  115. p = argp->p; \
  116. argp->p += XDR_QUADLEN(nbytes); \
  117. } else if (!(p = read_buf(argp, nbytes))) { \
  118. dprintk("NFSD: xdr error (%s:%d)\n", \
  119. __FILE__, __LINE__); \
  120. goto xdr_error; \
  121. } \
  122. } while (0)
  123. static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
  124. {
  125. /* We want more bytes than seem to be available.
  126. * Maybe we need a new page, maybe we have just run out
  127. */
  128. unsigned int avail = (char *)argp->end - (char *)argp->p;
  129. __be32 *p;
  130. if (avail + argp->pagelen < nbytes)
  131. return NULL;
  132. if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
  133. return NULL;
  134. /* ok, we can do it with the current plus the next page */
  135. if (nbytes <= sizeof(argp->tmp))
  136. p = argp->tmp;
  137. else {
  138. kfree(argp->tmpp);
  139. p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
  140. if (!p)
  141. return NULL;
  142. }
  143. /*
  144. * The following memcpy is safe because read_buf is always
  145. * called with nbytes > avail, and the two cases above both
  146. * guarantee p points to at least nbytes bytes.
  147. */
  148. memcpy(p, argp->p, avail);
  149. /* step to next page */
  150. argp->p = page_address(argp->pagelist[0]);
  151. argp->pagelist++;
  152. if (argp->pagelen < PAGE_SIZE) {
  153. argp->end = argp->p + (argp->pagelen>>2);
  154. argp->pagelen = 0;
  155. } else {
  156. argp->end = argp->p + (PAGE_SIZE>>2);
  157. argp->pagelen -= PAGE_SIZE;
  158. }
  159. memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
  160. argp->p += XDR_QUADLEN(nbytes - avail);
  161. return p;
  162. }
  163. static int zero_clientid(clientid_t *clid)
  164. {
  165. return (clid->cl_boot == 0) && (clid->cl_id == 0);
  166. }
  167. static int
  168. defer_free(struct nfsd4_compoundargs *argp,
  169. void (*release)(const void *), void *p)
  170. {
  171. struct tmpbuf *tb;
  172. tb = kmalloc(sizeof(*tb), GFP_KERNEL);
  173. if (!tb)
  174. return -ENOMEM;
  175. tb->buf = p;
  176. tb->release = release;
  177. tb->next = argp->to_free;
  178. argp->to_free = tb;
  179. return 0;
  180. }
  181. static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
  182. {
  183. if (p == argp->tmp) {
  184. p = kmalloc(nbytes, GFP_KERNEL);
  185. if (!p)
  186. return NULL;
  187. memcpy(p, argp->tmp, nbytes);
  188. } else {
  189. BUG_ON(p != argp->tmpp);
  190. argp->tmpp = NULL;
  191. }
  192. if (defer_free(argp, kfree, p)) {
  193. kfree(p);
  194. return NULL;
  195. } else
  196. return (char *)p;
  197. }
  198. static __be32
  199. nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
  200. {
  201. u32 bmlen;
  202. DECODE_HEAD;
  203. bmval[0] = 0;
  204. bmval[1] = 0;
  205. bmval[2] = 0;
  206. READ_BUF(4);
  207. READ32(bmlen);
  208. if (bmlen > 1000)
  209. goto xdr_error;
  210. READ_BUF(bmlen << 2);
  211. if (bmlen > 0)
  212. READ32(bmval[0]);
  213. if (bmlen > 1)
  214. READ32(bmval[1]);
  215. if (bmlen > 2)
  216. READ32(bmval[2]);
  217. DECODE_TAIL;
  218. }
  219. static __be32
  220. nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
  221. struct iattr *iattr, struct nfs4_acl **acl)
  222. {
  223. int expected_len, len = 0;
  224. u32 dummy32;
  225. char *buf;
  226. int host_err;
  227. DECODE_HEAD;
  228. iattr->ia_valid = 0;
  229. if ((status = nfsd4_decode_bitmap(argp, bmval)))
  230. return status;
  231. READ_BUF(4);
  232. READ32(expected_len);
  233. if (bmval[0] & FATTR4_WORD0_SIZE) {
  234. READ_BUF(8);
  235. len += 8;
  236. READ64(iattr->ia_size);
  237. iattr->ia_valid |= ATTR_SIZE;
  238. }
  239. if (bmval[0] & FATTR4_WORD0_ACL) {
  240. int nace;
  241. struct nfs4_ace *ace;
  242. READ_BUF(4); len += 4;
  243. READ32(nace);
  244. if (nace > NFS4_ACL_MAX)
  245. return nfserr_resource;
  246. *acl = nfs4_acl_new(nace);
  247. if (*acl == NULL) {
  248. host_err = -ENOMEM;
  249. goto out_nfserr;
  250. }
  251. defer_free(argp, kfree, *acl);
  252. (*acl)->naces = nace;
  253. for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
  254. READ_BUF(16); len += 16;
  255. READ32(ace->type);
  256. READ32(ace->flag);
  257. READ32(ace->access_mask);
  258. READ32(dummy32);
  259. READ_BUF(dummy32);
  260. len += XDR_QUADLEN(dummy32) << 2;
  261. READMEM(buf, dummy32);
  262. ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
  263. status = nfs_ok;
  264. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  265. ace->who = 0;
  266. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  267. status = nfsd_map_name_to_gid(argp->rqstp,
  268. buf, dummy32, &ace->who);
  269. else
  270. status = nfsd_map_name_to_uid(argp->rqstp,
  271. buf, dummy32, &ace->who);
  272. if (status)
  273. return status;
  274. }
  275. } else
  276. *acl = NULL;
  277. if (bmval[1] & FATTR4_WORD1_MODE) {
  278. READ_BUF(4);
  279. len += 4;
  280. READ32(iattr->ia_mode);
  281. iattr->ia_mode &= (S_IFMT | S_IALLUGO);
  282. iattr->ia_valid |= ATTR_MODE;
  283. }
  284. if (bmval[1] & FATTR4_WORD1_OWNER) {
  285. READ_BUF(4);
  286. len += 4;
  287. READ32(dummy32);
  288. READ_BUF(dummy32);
  289. len += (XDR_QUADLEN(dummy32) << 2);
  290. READMEM(buf, dummy32);
  291. if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
  292. return status;
  293. iattr->ia_valid |= ATTR_UID;
  294. }
  295. if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
  296. READ_BUF(4);
  297. len += 4;
  298. READ32(dummy32);
  299. READ_BUF(dummy32);
  300. len += (XDR_QUADLEN(dummy32) << 2);
  301. READMEM(buf, dummy32);
  302. if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
  303. return status;
  304. iattr->ia_valid |= ATTR_GID;
  305. }
  306. if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
  307. READ_BUF(4);
  308. len += 4;
  309. READ32(dummy32);
  310. switch (dummy32) {
  311. case NFS4_SET_TO_CLIENT_TIME:
  312. /* We require the high 32 bits of 'seconds' to be 0, and we ignore
  313. all 32 bits of 'nseconds'. */
  314. READ_BUF(12);
  315. len += 12;
  316. READ32(dummy32);
  317. if (dummy32)
  318. return nfserr_inval;
  319. READ32(iattr->ia_atime.tv_sec);
  320. READ32(iattr->ia_atime.tv_nsec);
  321. if (iattr->ia_atime.tv_nsec >= (u32)1000000000)
  322. return nfserr_inval;
  323. iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
  324. break;
  325. case NFS4_SET_TO_SERVER_TIME:
  326. iattr->ia_valid |= ATTR_ATIME;
  327. break;
  328. default:
  329. goto xdr_error;
  330. }
  331. }
  332. if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
  333. READ_BUF(4);
  334. len += 4;
  335. READ32(dummy32);
  336. switch (dummy32) {
  337. case NFS4_SET_TO_CLIENT_TIME:
  338. /* We require the high 32 bits of 'seconds' to be 0, and we ignore
  339. all 32 bits of 'nseconds'. */
  340. READ_BUF(12);
  341. len += 12;
  342. READ32(dummy32);
  343. if (dummy32)
  344. return nfserr_inval;
  345. READ32(iattr->ia_mtime.tv_sec);
  346. READ32(iattr->ia_mtime.tv_nsec);
  347. if (iattr->ia_mtime.tv_nsec >= (u32)1000000000)
  348. return nfserr_inval;
  349. iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
  350. break;
  351. case NFS4_SET_TO_SERVER_TIME:
  352. iattr->ia_valid |= ATTR_MTIME;
  353. break;
  354. default:
  355. goto xdr_error;
  356. }
  357. }
  358. if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
  359. || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
  360. || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
  361. READ_BUF(expected_len - len);
  362. else if (len != expected_len)
  363. goto xdr_error;
  364. DECODE_TAIL;
  365. out_nfserr:
  366. status = nfserrno(host_err);
  367. goto out;
  368. }
  369. static __be32
  370. nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
  371. {
  372. DECODE_HEAD;
  373. READ_BUF(sizeof(stateid_t));
  374. READ32(sid->si_generation);
  375. COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
  376. DECODE_TAIL;
  377. }
  378. static __be32
  379. nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
  380. {
  381. DECODE_HEAD;
  382. READ_BUF(4);
  383. READ32(access->ac_req_access);
  384. DECODE_TAIL;
  385. }
  386. static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
  387. {
  388. DECODE_HEAD;
  389. READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
  390. COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  391. READ32(bcts->dir);
  392. /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
  393. * could help us figure out we should be using it. */
  394. DECODE_TAIL;
  395. }
  396. static __be32
  397. nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
  398. {
  399. DECODE_HEAD;
  400. close->cl_stateowner = NULL;
  401. READ_BUF(4);
  402. READ32(close->cl_seqid);
  403. return nfsd4_decode_stateid(argp, &close->cl_stateid);
  404. DECODE_TAIL;
  405. }
  406. static __be32
  407. nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
  408. {
  409. DECODE_HEAD;
  410. READ_BUF(12);
  411. READ64(commit->co_offset);
  412. READ32(commit->co_count);
  413. DECODE_TAIL;
  414. }
  415. static __be32
  416. nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
  417. {
  418. DECODE_HEAD;
  419. READ_BUF(4);
  420. READ32(create->cr_type);
  421. switch (create->cr_type) {
  422. case NF4LNK:
  423. READ_BUF(4);
  424. READ32(create->cr_linklen);
  425. READ_BUF(create->cr_linklen);
  426. SAVEMEM(create->cr_linkname, create->cr_linklen);
  427. break;
  428. case NF4BLK:
  429. case NF4CHR:
  430. READ_BUF(8);
  431. READ32(create->cr_specdata1);
  432. READ32(create->cr_specdata2);
  433. break;
  434. case NF4SOCK:
  435. case NF4FIFO:
  436. case NF4DIR:
  437. default:
  438. break;
  439. }
  440. READ_BUF(4);
  441. READ32(create->cr_namelen);
  442. READ_BUF(create->cr_namelen);
  443. SAVEMEM(create->cr_name, create->cr_namelen);
  444. if ((status = check_filename(create->cr_name, create->cr_namelen, nfserr_inval)))
  445. return status;
  446. status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
  447. &create->cr_acl);
  448. if (status)
  449. goto out;
  450. DECODE_TAIL;
  451. }
  452. static inline __be32
  453. nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
  454. {
  455. return nfsd4_decode_stateid(argp, &dr->dr_stateid);
  456. }
  457. static inline __be32
  458. nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
  459. {
  460. return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
  461. }
  462. static __be32
  463. nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
  464. {
  465. DECODE_HEAD;
  466. READ_BUF(4);
  467. READ32(link->li_namelen);
  468. READ_BUF(link->li_namelen);
  469. SAVEMEM(link->li_name, link->li_namelen);
  470. if ((status = check_filename(link->li_name, link->li_namelen, nfserr_inval)))
  471. return status;
  472. DECODE_TAIL;
  473. }
  474. static __be32
  475. nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
  476. {
  477. DECODE_HEAD;
  478. lock->lk_replay_owner = NULL;
  479. /*
  480. * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
  481. */
  482. READ_BUF(28);
  483. READ32(lock->lk_type);
  484. if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
  485. goto xdr_error;
  486. READ32(lock->lk_reclaim);
  487. READ64(lock->lk_offset);
  488. READ64(lock->lk_length);
  489. READ32(lock->lk_is_new);
  490. if (lock->lk_is_new) {
  491. READ_BUF(4);
  492. READ32(lock->lk_new_open_seqid);
  493. status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
  494. if (status)
  495. return status;
  496. READ_BUF(8 + sizeof(clientid_t));
  497. READ32(lock->lk_new_lock_seqid);
  498. COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
  499. READ32(lock->lk_new_owner.len);
  500. READ_BUF(lock->lk_new_owner.len);
  501. READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
  502. } else {
  503. status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
  504. if (status)
  505. return status;
  506. READ_BUF(4);
  507. READ32(lock->lk_old_lock_seqid);
  508. }
  509. DECODE_TAIL;
  510. }
  511. static __be32
  512. nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
  513. {
  514. DECODE_HEAD;
  515. READ_BUF(32);
  516. READ32(lockt->lt_type);
  517. if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
  518. goto xdr_error;
  519. READ64(lockt->lt_offset);
  520. READ64(lockt->lt_length);
  521. COPYMEM(&lockt->lt_clientid, 8);
  522. READ32(lockt->lt_owner.len);
  523. READ_BUF(lockt->lt_owner.len);
  524. READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
  525. DECODE_TAIL;
  526. }
  527. static __be32
  528. nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
  529. {
  530. DECODE_HEAD;
  531. locku->lu_stateowner = NULL;
  532. READ_BUF(8);
  533. READ32(locku->lu_type);
  534. if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
  535. goto xdr_error;
  536. READ32(locku->lu_seqid);
  537. status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
  538. if (status)
  539. return status;
  540. READ_BUF(16);
  541. READ64(locku->lu_offset);
  542. READ64(locku->lu_length);
  543. DECODE_TAIL;
  544. }
  545. static __be32
  546. nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
  547. {
  548. DECODE_HEAD;
  549. READ_BUF(4);
  550. READ32(lookup->lo_len);
  551. READ_BUF(lookup->lo_len);
  552. SAVEMEM(lookup->lo_name, lookup->lo_len);
  553. if ((status = check_filename(lookup->lo_name, lookup->lo_len, nfserr_noent)))
  554. return status;
  555. DECODE_TAIL;
  556. }
  557. static __be32
  558. nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
  559. {
  560. DECODE_HEAD;
  561. memset(open->op_bmval, 0, sizeof(open->op_bmval));
  562. open->op_iattr.ia_valid = 0;
  563. open->op_stateowner = NULL;
  564. /* seqid, share_access, share_deny, clientid, ownerlen */
  565. READ_BUF(16 + sizeof(clientid_t));
  566. READ32(open->op_seqid);
  567. READ32(open->op_share_access);
  568. READ32(open->op_share_deny);
  569. COPYMEM(&open->op_clientid, sizeof(clientid_t));
  570. READ32(open->op_owner.len);
  571. /* owner, open_flag */
  572. READ_BUF(open->op_owner.len + 4);
  573. SAVEMEM(open->op_owner.data, open->op_owner.len);
  574. READ32(open->op_create);
  575. switch (open->op_create) {
  576. case NFS4_OPEN_NOCREATE:
  577. break;
  578. case NFS4_OPEN_CREATE:
  579. READ_BUF(4);
  580. READ32(open->op_createmode);
  581. switch (open->op_createmode) {
  582. case NFS4_CREATE_UNCHECKED:
  583. case NFS4_CREATE_GUARDED:
  584. status = nfsd4_decode_fattr(argp, open->op_bmval,
  585. &open->op_iattr, &open->op_acl);
  586. if (status)
  587. goto out;
  588. break;
  589. case NFS4_CREATE_EXCLUSIVE:
  590. READ_BUF(8);
  591. COPYMEM(open->op_verf.data, 8);
  592. break;
  593. case NFS4_CREATE_EXCLUSIVE4_1:
  594. if (argp->minorversion < 1)
  595. goto xdr_error;
  596. READ_BUF(8);
  597. COPYMEM(open->op_verf.data, 8);
  598. status = nfsd4_decode_fattr(argp, open->op_bmval,
  599. &open->op_iattr, &open->op_acl);
  600. if (status)
  601. goto out;
  602. break;
  603. default:
  604. goto xdr_error;
  605. }
  606. break;
  607. default:
  608. goto xdr_error;
  609. }
  610. /* open_claim */
  611. READ_BUF(4);
  612. READ32(open->op_claim_type);
  613. switch (open->op_claim_type) {
  614. case NFS4_OPEN_CLAIM_NULL:
  615. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  616. READ_BUF(4);
  617. READ32(open->op_fname.len);
  618. READ_BUF(open->op_fname.len);
  619. SAVEMEM(open->op_fname.data, open->op_fname.len);
  620. if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
  621. return status;
  622. break;
  623. case NFS4_OPEN_CLAIM_PREVIOUS:
  624. READ_BUF(4);
  625. READ32(open->op_delegate_type);
  626. break;
  627. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  628. status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
  629. if (status)
  630. return status;
  631. READ_BUF(4);
  632. READ32(open->op_fname.len);
  633. READ_BUF(open->op_fname.len);
  634. SAVEMEM(open->op_fname.data, open->op_fname.len);
  635. if ((status = check_filename(open->op_fname.data, open->op_fname.len, nfserr_inval)))
  636. return status;
  637. break;
  638. default:
  639. goto xdr_error;
  640. }
  641. DECODE_TAIL;
  642. }
  643. static __be32
  644. nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
  645. {
  646. DECODE_HEAD;
  647. open_conf->oc_stateowner = NULL;
  648. status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
  649. if (status)
  650. return status;
  651. READ_BUF(4);
  652. READ32(open_conf->oc_seqid);
  653. DECODE_TAIL;
  654. }
  655. static __be32
  656. nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
  657. {
  658. DECODE_HEAD;
  659. open_down->od_stateowner = NULL;
  660. status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
  661. if (status)
  662. return status;
  663. READ_BUF(12);
  664. READ32(open_down->od_seqid);
  665. READ32(open_down->od_share_access);
  666. READ32(open_down->od_share_deny);
  667. DECODE_TAIL;
  668. }
  669. static __be32
  670. nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
  671. {
  672. DECODE_HEAD;
  673. READ_BUF(4);
  674. READ32(putfh->pf_fhlen);
  675. if (putfh->pf_fhlen > NFS4_FHSIZE)
  676. goto xdr_error;
  677. READ_BUF(putfh->pf_fhlen);
  678. SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
  679. DECODE_TAIL;
  680. }
  681. static __be32
  682. nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
  683. {
  684. DECODE_HEAD;
  685. status = nfsd4_decode_stateid(argp, &read->rd_stateid);
  686. if (status)
  687. return status;
  688. READ_BUF(12);
  689. READ64(read->rd_offset);
  690. READ32(read->rd_length);
  691. DECODE_TAIL;
  692. }
  693. static __be32
  694. nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
  695. {
  696. DECODE_HEAD;
  697. READ_BUF(24);
  698. READ64(readdir->rd_cookie);
  699. COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
  700. READ32(readdir->rd_dircount); /* just in case you needed a useless field... */
  701. READ32(readdir->rd_maxcount);
  702. if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
  703. goto out;
  704. DECODE_TAIL;
  705. }
  706. static __be32
  707. nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
  708. {
  709. DECODE_HEAD;
  710. READ_BUF(4);
  711. READ32(remove->rm_namelen);
  712. READ_BUF(remove->rm_namelen);
  713. SAVEMEM(remove->rm_name, remove->rm_namelen);
  714. if ((status = check_filename(remove->rm_name, remove->rm_namelen, nfserr_noent)))
  715. return status;
  716. DECODE_TAIL;
  717. }
  718. static __be32
  719. nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
  720. {
  721. DECODE_HEAD;
  722. READ_BUF(4);
  723. READ32(rename->rn_snamelen);
  724. READ_BUF(rename->rn_snamelen + 4);
  725. SAVEMEM(rename->rn_sname, rename->rn_snamelen);
  726. READ32(rename->rn_tnamelen);
  727. READ_BUF(rename->rn_tnamelen);
  728. SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
  729. if ((status = check_filename(rename->rn_sname, rename->rn_snamelen, nfserr_noent)))
  730. return status;
  731. if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen, nfserr_inval)))
  732. return status;
  733. DECODE_TAIL;
  734. }
  735. static __be32
  736. nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
  737. {
  738. DECODE_HEAD;
  739. READ_BUF(sizeof(clientid_t));
  740. COPYMEM(clientid, sizeof(clientid_t));
  741. DECODE_TAIL;
  742. }
  743. static __be32
  744. nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
  745. struct nfsd4_secinfo *secinfo)
  746. {
  747. DECODE_HEAD;
  748. READ_BUF(4);
  749. READ32(secinfo->si_namelen);
  750. READ_BUF(secinfo->si_namelen);
  751. SAVEMEM(secinfo->si_name, secinfo->si_namelen);
  752. status = check_filename(secinfo->si_name, secinfo->si_namelen,
  753. nfserr_noent);
  754. if (status)
  755. return status;
  756. DECODE_TAIL;
  757. }
  758. static __be32
  759. nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
  760. struct nfsd4_secinfo_no_name *sin)
  761. {
  762. DECODE_HEAD;
  763. READ_BUF(4);
  764. READ32(sin->sin_style);
  765. DECODE_TAIL;
  766. }
  767. static __be32
  768. nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
  769. {
  770. __be32 status;
  771. status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
  772. if (status)
  773. return status;
  774. return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
  775. &setattr->sa_acl);
  776. }
  777. static __be32
  778. nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
  779. {
  780. DECODE_HEAD;
  781. READ_BUF(12);
  782. COPYMEM(setclientid->se_verf.data, 8);
  783. READ32(setclientid->se_namelen);
  784. READ_BUF(setclientid->se_namelen + 8);
  785. SAVEMEM(setclientid->se_name, setclientid->se_namelen);
  786. READ32(setclientid->se_callback_prog);
  787. READ32(setclientid->se_callback_netid_len);
  788. READ_BUF(setclientid->se_callback_netid_len + 4);
  789. SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
  790. READ32(setclientid->se_callback_addr_len);
  791. READ_BUF(setclientid->se_callback_addr_len + 4);
  792. SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
  793. READ32(setclientid->se_callback_ident);
  794. DECODE_TAIL;
  795. }
  796. static __be32
  797. nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
  798. {
  799. DECODE_HEAD;
  800. READ_BUF(8 + sizeof(nfs4_verifier));
  801. COPYMEM(&scd_c->sc_clientid, 8);
  802. COPYMEM(&scd_c->sc_confirm, sizeof(nfs4_verifier));
  803. DECODE_TAIL;
  804. }
  805. /* Also used for NVERIFY */
  806. static __be32
  807. nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
  808. {
  809. #if 0
  810. struct nfsd4_compoundargs save = {
  811. .p = argp->p,
  812. .end = argp->end,
  813. .rqstp = argp->rqstp,
  814. };
  815. u32 ve_bmval[2];
  816. struct iattr ve_iattr; /* request */
  817. struct nfs4_acl *ve_acl; /* request */
  818. #endif
  819. DECODE_HEAD;
  820. if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
  821. goto out;
  822. /* For convenience's sake, we compare raw xdr'd attributes in
  823. * nfsd4_proc_verify; however we still decode here just to return
  824. * correct error in case of bad xdr. */
  825. #if 0
  826. status = nfsd4_decode_fattr(ve_bmval, &ve_iattr, &ve_acl);
  827. if (status == nfserr_inval) {
  828. status = nfserrno(status);
  829. goto out;
  830. }
  831. #endif
  832. READ_BUF(4);
  833. READ32(verify->ve_attrlen);
  834. READ_BUF(verify->ve_attrlen);
  835. SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
  836. DECODE_TAIL;
  837. }
  838. static __be32
  839. nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
  840. {
  841. int avail;
  842. int v;
  843. int len;
  844. DECODE_HEAD;
  845. status = nfsd4_decode_stateid(argp, &write->wr_stateid);
  846. if (status)
  847. return status;
  848. READ_BUF(16);
  849. READ64(write->wr_offset);
  850. READ32(write->wr_stable_how);
  851. if (write->wr_stable_how > 2)
  852. goto xdr_error;
  853. READ32(write->wr_buflen);
  854. /* Sorry .. no magic macros for this.. *
  855. * READ_BUF(write->wr_buflen);
  856. * SAVEMEM(write->wr_buf, write->wr_buflen);
  857. */
  858. avail = (char*)argp->end - (char*)argp->p;
  859. if (avail + argp->pagelen < write->wr_buflen) {
  860. dprintk("NFSD: xdr error (%s:%d)\n",
  861. __FILE__, __LINE__);
  862. goto xdr_error;
  863. }
  864. argp->rqstp->rq_vec[0].iov_base = p;
  865. argp->rqstp->rq_vec[0].iov_len = avail;
  866. v = 0;
  867. len = write->wr_buflen;
  868. while (len > argp->rqstp->rq_vec[v].iov_len) {
  869. len -= argp->rqstp->rq_vec[v].iov_len;
  870. v++;
  871. argp->rqstp->rq_vec[v].iov_base = page_address(argp->pagelist[0]);
  872. argp->pagelist++;
  873. if (argp->pagelen >= PAGE_SIZE) {
  874. argp->rqstp->rq_vec[v].iov_len = PAGE_SIZE;
  875. argp->pagelen -= PAGE_SIZE;
  876. } else {
  877. argp->rqstp->rq_vec[v].iov_len = argp->pagelen;
  878. argp->pagelen -= len;
  879. }
  880. }
  881. argp->end = (__be32*) (argp->rqstp->rq_vec[v].iov_base + argp->rqstp->rq_vec[v].iov_len);
  882. argp->p = (__be32*) (argp->rqstp->rq_vec[v].iov_base + (XDR_QUADLEN(len) << 2));
  883. argp->rqstp->rq_vec[v].iov_len = len;
  884. write->wr_vlen = v+1;
  885. DECODE_TAIL;
  886. }
  887. static __be32
  888. nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
  889. {
  890. DECODE_HEAD;
  891. READ_BUF(12);
  892. COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
  893. READ32(rlockowner->rl_owner.len);
  894. READ_BUF(rlockowner->rl_owner.len);
  895. READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
  896. if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
  897. return nfserr_inval;
  898. DECODE_TAIL;
  899. }
  900. static __be32
  901. nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
  902. struct nfsd4_exchange_id *exid)
  903. {
  904. int dummy, tmp;
  905. DECODE_HEAD;
  906. READ_BUF(NFS4_VERIFIER_SIZE);
  907. COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
  908. READ_BUF(4);
  909. READ32(exid->clname.len);
  910. READ_BUF(exid->clname.len);
  911. SAVEMEM(exid->clname.data, exid->clname.len);
  912. READ_BUF(4);
  913. READ32(exid->flags);
  914. /* Ignore state_protect4_a */
  915. READ_BUF(4);
  916. READ32(exid->spa_how);
  917. switch (exid->spa_how) {
  918. case SP4_NONE:
  919. break;
  920. case SP4_MACH_CRED:
  921. /* spo_must_enforce */
  922. READ_BUF(4);
  923. READ32(dummy);
  924. READ_BUF(dummy * 4);
  925. p += dummy;
  926. /* spo_must_allow */
  927. READ_BUF(4);
  928. READ32(dummy);
  929. READ_BUF(dummy * 4);
  930. p += dummy;
  931. break;
  932. case SP4_SSV:
  933. /* ssp_ops */
  934. READ_BUF(4);
  935. READ32(dummy);
  936. READ_BUF(dummy * 4);
  937. p += dummy;
  938. READ_BUF(4);
  939. READ32(dummy);
  940. READ_BUF(dummy * 4);
  941. p += dummy;
  942. /* ssp_hash_algs<> */
  943. READ_BUF(4);
  944. READ32(tmp);
  945. while (tmp--) {
  946. READ_BUF(4);
  947. READ32(dummy);
  948. READ_BUF(dummy);
  949. p += XDR_QUADLEN(dummy);
  950. }
  951. /* ssp_encr_algs<> */
  952. READ_BUF(4);
  953. READ32(tmp);
  954. while (tmp--) {
  955. READ_BUF(4);
  956. READ32(dummy);
  957. READ_BUF(dummy);
  958. p += XDR_QUADLEN(dummy);
  959. }
  960. /* ssp_window and ssp_num_gss_handles */
  961. READ_BUF(8);
  962. READ32(dummy);
  963. READ32(dummy);
  964. break;
  965. default:
  966. goto xdr_error;
  967. }
  968. /* Ignore Implementation ID */
  969. READ_BUF(4); /* nfs_impl_id4 array length */
  970. READ32(dummy);
  971. if (dummy > 1)
  972. goto xdr_error;
  973. if (dummy == 1) {
  974. /* nii_domain */
  975. READ_BUF(4);
  976. READ32(dummy);
  977. READ_BUF(dummy);
  978. p += XDR_QUADLEN(dummy);
  979. /* nii_name */
  980. READ_BUF(4);
  981. READ32(dummy);
  982. READ_BUF(dummy);
  983. p += XDR_QUADLEN(dummy);
  984. /* nii_date */
  985. READ_BUF(12);
  986. p += 3;
  987. }
  988. DECODE_TAIL;
  989. }
  990. static __be32
  991. nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
  992. struct nfsd4_create_session *sess)
  993. {
  994. DECODE_HEAD;
  995. u32 dummy;
  996. char *machine_name;
  997. int i;
  998. int nr_secflavs;
  999. READ_BUF(16);
  1000. COPYMEM(&sess->clientid, 8);
  1001. READ32(sess->seqid);
  1002. READ32(sess->flags);
  1003. /* Fore channel attrs */
  1004. READ_BUF(28);
  1005. READ32(dummy); /* headerpadsz is always 0 */
  1006. READ32(sess->fore_channel.maxreq_sz);
  1007. READ32(sess->fore_channel.maxresp_sz);
  1008. READ32(sess->fore_channel.maxresp_cached);
  1009. READ32(sess->fore_channel.maxops);
  1010. READ32(sess->fore_channel.maxreqs);
  1011. READ32(sess->fore_channel.nr_rdma_attrs);
  1012. if (sess->fore_channel.nr_rdma_attrs == 1) {
  1013. READ_BUF(4);
  1014. READ32(sess->fore_channel.rdma_attrs);
  1015. } else if (sess->fore_channel.nr_rdma_attrs > 1) {
  1016. dprintk("Too many fore channel attr bitmaps!\n");
  1017. goto xdr_error;
  1018. }
  1019. /* Back channel attrs */
  1020. READ_BUF(28);
  1021. READ32(dummy); /* headerpadsz is always 0 */
  1022. READ32(sess->back_channel.maxreq_sz);
  1023. READ32(sess->back_channel.maxresp_sz);
  1024. READ32(sess->back_channel.maxresp_cached);
  1025. READ32(sess->back_channel.maxops);
  1026. READ32(sess->back_channel.maxreqs);
  1027. READ32(sess->back_channel.nr_rdma_attrs);
  1028. if (sess->back_channel.nr_rdma_attrs == 1) {
  1029. READ_BUF(4);
  1030. READ32(sess->back_channel.rdma_attrs);
  1031. } else if (sess->back_channel.nr_rdma_attrs > 1) {
  1032. dprintk("Too many back channel attr bitmaps!\n");
  1033. goto xdr_error;
  1034. }
  1035. READ_BUF(8);
  1036. READ32(sess->callback_prog);
  1037. /* callback_sec_params4 */
  1038. READ32(nr_secflavs);
  1039. for (i = 0; i < nr_secflavs; ++i) {
  1040. READ_BUF(4);
  1041. READ32(dummy);
  1042. switch (dummy) {
  1043. case RPC_AUTH_NULL:
  1044. /* Nothing to read */
  1045. break;
  1046. case RPC_AUTH_UNIX:
  1047. READ_BUF(8);
  1048. /* stamp */
  1049. READ32(dummy);
  1050. /* machine name */
  1051. READ32(dummy);
  1052. READ_BUF(dummy);
  1053. SAVEMEM(machine_name, dummy);
  1054. /* uid, gid */
  1055. READ_BUF(8);
  1056. READ32(sess->uid);
  1057. READ32(sess->gid);
  1058. /* more gids */
  1059. READ_BUF(4);
  1060. READ32(dummy);
  1061. READ_BUF(dummy * 4);
  1062. break;
  1063. case RPC_AUTH_GSS:
  1064. dprintk("RPC_AUTH_GSS callback secflavor "
  1065. "not supported!\n");
  1066. READ_BUF(8);
  1067. /* gcbp_service */
  1068. READ32(dummy);
  1069. /* gcbp_handle_from_server */
  1070. READ32(dummy);
  1071. READ_BUF(dummy);
  1072. p += XDR_QUADLEN(dummy);
  1073. /* gcbp_handle_from_client */
  1074. READ_BUF(4);
  1075. READ32(dummy);
  1076. READ_BUF(dummy);
  1077. break;
  1078. default:
  1079. dprintk("Illegal callback secflavor\n");
  1080. return nfserr_inval;
  1081. }
  1082. }
  1083. DECODE_TAIL;
  1084. }
  1085. static __be32
  1086. nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
  1087. struct nfsd4_destroy_session *destroy_session)
  1088. {
  1089. DECODE_HEAD;
  1090. READ_BUF(NFS4_MAX_SESSIONID_LEN);
  1091. COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1092. DECODE_TAIL;
  1093. }
  1094. static __be32
  1095. nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
  1096. struct nfsd4_sequence *seq)
  1097. {
  1098. DECODE_HEAD;
  1099. READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
  1100. COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1101. READ32(seq->seqid);
  1102. READ32(seq->slotid);
  1103. READ32(seq->maxslots);
  1104. READ32(seq->cachethis);
  1105. DECODE_TAIL;
  1106. }
  1107. static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
  1108. {
  1109. DECODE_HEAD;
  1110. READ_BUF(4);
  1111. READ32(rc->rca_one_fs);
  1112. DECODE_TAIL;
  1113. }
  1114. static __be32
  1115. nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
  1116. {
  1117. return nfs_ok;
  1118. }
  1119. static __be32
  1120. nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
  1121. {
  1122. return nfserr_notsupp;
  1123. }
  1124. typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
  1125. static nfsd4_dec nfsd4_dec_ops[] = {
  1126. [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
  1127. [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
  1128. [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
  1129. [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
  1130. [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1131. [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
  1132. [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
  1133. [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
  1134. [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
  1135. [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
  1136. [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
  1137. [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
  1138. [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
  1139. [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
  1140. [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1141. [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
  1142. [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1143. [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
  1144. [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
  1145. [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
  1146. [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_noop,
  1147. [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
  1148. [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
  1149. [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
  1150. [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
  1151. [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
  1152. [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
  1153. [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
  1154. [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
  1155. [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
  1156. [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
  1157. [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
  1158. [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
  1159. [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
  1160. [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1161. [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
  1162. [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
  1163. };
  1164. static nfsd4_dec nfsd41_dec_ops[] = {
  1165. [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
  1166. [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
  1167. [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
  1168. [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
  1169. [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1170. [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
  1171. [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
  1172. [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
  1173. [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
  1174. [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
  1175. [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
  1176. [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
  1177. [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
  1178. [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
  1179. [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1180. [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
  1181. [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1182. [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_notsupp,
  1183. [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
  1184. [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
  1185. [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_notsupp,
  1186. [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
  1187. [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
  1188. [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
  1189. [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
  1190. [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
  1191. [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
  1192. [OP_RENEW] = (nfsd4_dec)nfsd4_decode_notsupp,
  1193. [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
  1194. [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
  1195. [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
  1196. [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
  1197. [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
  1198. [OP_SETCLIENTID_CONFIRM]= (nfsd4_dec)nfsd4_decode_notsupp,
  1199. [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1200. [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
  1201. [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_notsupp,
  1202. /* new operations for NFSv4.1 */
  1203. [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_notsupp,
  1204. [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
  1205. [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
  1206. [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
  1207. [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
  1208. [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_notsupp,
  1209. [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1210. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
  1211. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  1212. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
  1213. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
  1214. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
  1215. [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
  1216. [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
  1217. [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
  1218. [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_notsupp,
  1219. [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1220. [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_notsupp,
  1221. [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
  1222. };
  1223. struct nfsd4_minorversion_ops {
  1224. nfsd4_dec *decoders;
  1225. int nops;
  1226. };
  1227. static struct nfsd4_minorversion_ops nfsd4_minorversion[] = {
  1228. [0] = { nfsd4_dec_ops, ARRAY_SIZE(nfsd4_dec_ops) },
  1229. [1] = { nfsd41_dec_ops, ARRAY_SIZE(nfsd41_dec_ops) },
  1230. };
  1231. static __be32
  1232. nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
  1233. {
  1234. DECODE_HEAD;
  1235. struct nfsd4_op *op;
  1236. struct nfsd4_minorversion_ops *ops;
  1237. int i;
  1238. /*
  1239. * XXX: According to spec, we should check the tag
  1240. * for UTF-8 compliance. I'm postponing this for
  1241. * now because it seems that some clients do use
  1242. * binary tags.
  1243. */
  1244. READ_BUF(4);
  1245. READ32(argp->taglen);
  1246. READ_BUF(argp->taglen + 8);
  1247. SAVEMEM(argp->tag, argp->taglen);
  1248. READ32(argp->minorversion);
  1249. READ32(argp->opcnt);
  1250. if (argp->taglen > NFSD4_MAX_TAGLEN)
  1251. goto xdr_error;
  1252. if (argp->opcnt > 100)
  1253. goto xdr_error;
  1254. if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
  1255. argp->ops = kmalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
  1256. if (!argp->ops) {
  1257. argp->ops = argp->iops;
  1258. dprintk("nfsd: couldn't allocate room for COMPOUND\n");
  1259. goto xdr_error;
  1260. }
  1261. }
  1262. if (argp->minorversion >= ARRAY_SIZE(nfsd4_minorversion))
  1263. argp->opcnt = 0;
  1264. ops = &nfsd4_minorversion[argp->minorversion];
  1265. for (i = 0; i < argp->opcnt; i++) {
  1266. op = &argp->ops[i];
  1267. op->replay = NULL;
  1268. /*
  1269. * We can't use READ_BUF() here because we need to handle
  1270. * a missing opcode as an OP_WRITE + 1. So we need to check
  1271. * to see if we're truly at the end of our buffer or if there
  1272. * is another page we need to flip to.
  1273. */
  1274. if (argp->p == argp->end) {
  1275. if (argp->pagelen < 4) {
  1276. /* There isn't an opcode still on the wire */
  1277. op->opnum = OP_WRITE + 1;
  1278. op->status = nfserr_bad_xdr;
  1279. argp->opcnt = i+1;
  1280. break;
  1281. }
  1282. /*
  1283. * False alarm. We just hit a page boundary, but there
  1284. * is still data available. Move pointer across page
  1285. * boundary. *snip from READ_BUF*
  1286. */
  1287. argp->p = page_address(argp->pagelist[0]);
  1288. argp->pagelist++;
  1289. if (argp->pagelen < PAGE_SIZE) {
  1290. argp->end = argp->p + (argp->pagelen>>2);
  1291. argp->pagelen = 0;
  1292. } else {
  1293. argp->end = argp->p + (PAGE_SIZE>>2);
  1294. argp->pagelen -= PAGE_SIZE;
  1295. }
  1296. }
  1297. op->opnum = ntohl(*argp->p++);
  1298. if (op->opnum >= FIRST_NFS4_OP && op->opnum <= LAST_NFS4_OP)
  1299. op->status = ops->decoders[op->opnum](argp, &op->u);
  1300. else {
  1301. op->opnum = OP_ILLEGAL;
  1302. op->status = nfserr_op_illegal;
  1303. }
  1304. if (op->status) {
  1305. argp->opcnt = i+1;
  1306. break;
  1307. }
  1308. }
  1309. DECODE_TAIL;
  1310. }
  1311. #define WRITE32(n) *p++ = htonl(n)
  1312. #define WRITE64(n) do { \
  1313. *p++ = htonl((u32)((n) >> 32)); \
  1314. *p++ = htonl((u32)(n)); \
  1315. } while (0)
  1316. #define WRITEMEM(ptr,nbytes) do { if (nbytes > 0) { \
  1317. *(p + XDR_QUADLEN(nbytes) -1) = 0; \
  1318. memcpy(p, ptr, nbytes); \
  1319. p += XDR_QUADLEN(nbytes); \
  1320. }} while (0)
  1321. static void write32(__be32 **p, u32 n)
  1322. {
  1323. *(*p)++ = n;
  1324. }
  1325. static void write64(__be32 **p, u64 n)
  1326. {
  1327. write32(p, (u32)(n >> 32));
  1328. write32(p, (u32)n);
  1329. }
  1330. static void write_change(__be32 **p, struct kstat *stat, struct inode *inode)
  1331. {
  1332. if (IS_I_VERSION(inode)) {
  1333. write64(p, inode->i_version);
  1334. } else {
  1335. write32(p, stat->ctime.tv_sec);
  1336. write32(p, stat->ctime.tv_nsec);
  1337. }
  1338. }
  1339. static void write_cinfo(__be32 **p, struct nfsd4_change_info *c)
  1340. {
  1341. write32(p, c->atomic);
  1342. if (c->change_supported) {
  1343. write64(p, c->before_change);
  1344. write64(p, c->after_change);
  1345. } else {
  1346. write32(p, c->before_ctime_sec);
  1347. write32(p, c->before_ctime_nsec);
  1348. write32(p, c->after_ctime_sec);
  1349. write32(p, c->after_ctime_nsec);
  1350. }
  1351. }
  1352. #define RESERVE_SPACE(nbytes) do { \
  1353. p = resp->p; \
  1354. BUG_ON(p + XDR_QUADLEN(nbytes) > resp->end); \
  1355. } while (0)
  1356. #define ADJUST_ARGS() resp->p = p
  1357. /*
  1358. * Header routine to setup seqid operation replay cache
  1359. */
  1360. #define ENCODE_SEQID_OP_HEAD \
  1361. __be32 *save; \
  1362. \
  1363. save = resp->p;
  1364. static bool seqid_mutating_err(__be32 err)
  1365. {
  1366. /* rfc 3530 section 8.1.5: */
  1367. return err != nfserr_stale_clientid &&
  1368. err != nfserr_stale_stateid &&
  1369. err != nfserr_bad_stateid &&
  1370. err != nfserr_bad_seqid &&
  1371. err != nfserr_bad_xdr &&
  1372. err != nfserr_resource &&
  1373. err != nfserr_nofilehandle;
  1374. }
  1375. /*
  1376. * Routine for encoding the result of a "seqid-mutating" NFSv4 operation. This
  1377. * is where sequence id's are incremented, and the replay cache is filled.
  1378. * Note that we increment sequence id's here, at the last moment, so we're sure
  1379. * we know whether the error to be returned is a sequence id mutating error.
  1380. */
  1381. #define ENCODE_SEQID_OP_TAIL(stateowner) do { \
  1382. if (seqid_mutating_err(nfserr) && stateowner) { \
  1383. stateowner->so_seqid++; \
  1384. stateowner->so_replay.rp_status = nfserr; \
  1385. stateowner->so_replay.rp_buflen = \
  1386. (((char *)(resp)->p - (char *)save)); \
  1387. memcpy(stateowner->so_replay.rp_buf, save, \
  1388. stateowner->so_replay.rp_buflen); \
  1389. } } while (0);
  1390. /* Encode as an array of strings the string given with components
  1391. * separated @sep.
  1392. */
  1393. static __be32 nfsd4_encode_components(char sep, char *components,
  1394. __be32 **pp, int *buflen)
  1395. {
  1396. __be32 *p = *pp;
  1397. __be32 *countp = p;
  1398. int strlen, count=0;
  1399. char *str, *end;
  1400. dprintk("nfsd4_encode_components(%s)\n", components);
  1401. if ((*buflen -= 4) < 0)
  1402. return nfserr_resource;
  1403. WRITE32(0); /* We will fill this in with @count later */
  1404. end = str = components;
  1405. while (*end) {
  1406. for (; *end && (*end != sep); end++)
  1407. ; /* Point to end of component */
  1408. strlen = end - str;
  1409. if (strlen) {
  1410. if ((*buflen -= ((XDR_QUADLEN(strlen) << 2) + 4)) < 0)
  1411. return nfserr_resource;
  1412. WRITE32(strlen);
  1413. WRITEMEM(str, strlen);
  1414. count++;
  1415. }
  1416. else
  1417. end++;
  1418. str = end;
  1419. }
  1420. *pp = p;
  1421. p = countp;
  1422. WRITE32(count);
  1423. return 0;
  1424. }
  1425. /*
  1426. * encode a location element of a fs_locations structure
  1427. */
  1428. static __be32 nfsd4_encode_fs_location4(struct nfsd4_fs_location *location,
  1429. __be32 **pp, int *buflen)
  1430. {
  1431. __be32 status;
  1432. __be32 *p = *pp;
  1433. status = nfsd4_encode_components(':', location->hosts, &p, buflen);
  1434. if (status)
  1435. return status;
  1436. status = nfsd4_encode_components('/', location->path, &p, buflen);
  1437. if (status)
  1438. return status;
  1439. *pp = p;
  1440. return 0;
  1441. }
  1442. /*
  1443. * Return the path to an export point in the pseudo filesystem namespace
  1444. * Returned string is safe to use as long as the caller holds a reference
  1445. * to @exp.
  1446. */
  1447. static char *nfsd4_path(struct svc_rqst *rqstp, struct svc_export *exp, __be32 *stat)
  1448. {
  1449. struct svc_fh tmp_fh;
  1450. char *path = NULL, *rootpath;
  1451. size_t rootlen;
  1452. fh_init(&tmp_fh, NFS4_FHSIZE);
  1453. *stat = exp_pseudoroot(rqstp, &tmp_fh);
  1454. if (*stat)
  1455. return NULL;
  1456. rootpath = tmp_fh.fh_export->ex_pathname;
  1457. path = exp->ex_pathname;
  1458. rootlen = strlen(rootpath);
  1459. if (strncmp(path, rootpath, rootlen)) {
  1460. dprintk("nfsd: fs_locations failed;"
  1461. "%s is not contained in %s\n", path, rootpath);
  1462. *stat = nfserr_notsupp;
  1463. path = NULL;
  1464. goto out;
  1465. }
  1466. path += rootlen;
  1467. out:
  1468. fh_put(&tmp_fh);
  1469. return path;
  1470. }
  1471. /*
  1472. * encode a fs_locations structure
  1473. */
  1474. static __be32 nfsd4_encode_fs_locations(struct svc_rqst *rqstp,
  1475. struct svc_export *exp,
  1476. __be32 **pp, int *buflen)
  1477. {
  1478. __be32 status;
  1479. int i;
  1480. __be32 *p = *pp;
  1481. struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
  1482. char *root = nfsd4_path(rqstp, exp, &status);
  1483. if (status)
  1484. return status;
  1485. status = nfsd4_encode_components('/', root, &p, buflen);
  1486. if (status)
  1487. return status;
  1488. if ((*buflen -= 4) < 0)
  1489. return nfserr_resource;
  1490. WRITE32(fslocs->locations_count);
  1491. for (i=0; i<fslocs->locations_count; i++) {
  1492. status = nfsd4_encode_fs_location4(&fslocs->locations[i],
  1493. &p, buflen);
  1494. if (status)
  1495. return status;
  1496. }
  1497. *pp = p;
  1498. return 0;
  1499. }
  1500. static u32 nfs4_ftypes[16] = {
  1501. NF4BAD, NF4FIFO, NF4CHR, NF4BAD,
  1502. NF4DIR, NF4BAD, NF4BLK, NF4BAD,
  1503. NF4REG, NF4BAD, NF4LNK, NF4BAD,
  1504. NF4SOCK, NF4BAD, NF4LNK, NF4BAD,
  1505. };
  1506. static __be32
  1507. nfsd4_encode_name(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
  1508. __be32 **p, int *buflen)
  1509. {
  1510. int status;
  1511. if (*buflen < (XDR_QUADLEN(IDMAP_NAMESZ) << 2) + 4)
  1512. return nfserr_resource;
  1513. if (whotype != NFS4_ACL_WHO_NAMED)
  1514. status = nfs4_acl_write_who(whotype, (u8 *)(*p + 1));
  1515. else if (group)
  1516. status = nfsd_map_gid_to_name(rqstp, id, (u8 *)(*p + 1));
  1517. else
  1518. status = nfsd_map_uid_to_name(rqstp, id, (u8 *)(*p + 1));
  1519. if (status < 0)
  1520. return nfserrno(status);
  1521. *p = xdr_encode_opaque(*p, NULL, status);
  1522. *buflen -= (XDR_QUADLEN(status) << 2) + 4;
  1523. BUG_ON(*buflen < 0);
  1524. return 0;
  1525. }
  1526. static inline __be32
  1527. nfsd4_encode_user(struct svc_rqst *rqstp, uid_t uid, __be32 **p, int *buflen)
  1528. {
  1529. return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, uid, 0, p, buflen);
  1530. }
  1531. static inline __be32
  1532. nfsd4_encode_group(struct svc_rqst *rqstp, uid_t gid, __be32 **p, int *buflen)
  1533. {
  1534. return nfsd4_encode_name(rqstp, NFS4_ACL_WHO_NAMED, gid, 1, p, buflen);
  1535. }
  1536. static inline __be32
  1537. nfsd4_encode_aclname(struct svc_rqst *rqstp, int whotype, uid_t id, int group,
  1538. __be32 **p, int *buflen)
  1539. {
  1540. return nfsd4_encode_name(rqstp, whotype, id, group, p, buflen);
  1541. }
  1542. #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
  1543. FATTR4_WORD0_RDATTR_ERROR)
  1544. #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
  1545. static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
  1546. {
  1547. /* As per referral draft: */
  1548. if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
  1549. *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
  1550. if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
  1551. *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
  1552. *rdattr_err = NFSERR_MOVED;
  1553. else
  1554. return nfserr_moved;
  1555. }
  1556. *bmval0 &= WORD0_ABSENT_FS_ATTRS;
  1557. *bmval1 &= WORD1_ABSENT_FS_ATTRS;
  1558. return 0;
  1559. }
  1560. /*
  1561. * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
  1562. * ourselves.
  1563. *
  1564. * @countp is the buffer size in _words_; upon successful return this becomes
  1565. * replaced with the number of words written.
  1566. */
  1567. __be32
  1568. nfsd4_encode_fattr(struct svc_fh *fhp, struct svc_export *exp,
  1569. struct dentry *dentry, __be32 *buffer, int *countp, u32 *bmval,
  1570. struct svc_rqst *rqstp, int ignore_crossmnt)
  1571. {
  1572. u32 bmval0 = bmval[0];
  1573. u32 bmval1 = bmval[1];
  1574. u32 bmval2 = bmval[2];
  1575. struct kstat stat;
  1576. struct svc_fh tempfh;
  1577. struct kstatfs statfs;
  1578. int buflen = *countp << 2;
  1579. __be32 *attrlenp;
  1580. u32 dummy;
  1581. u64 dummy64;
  1582. u32 rdattr_err = 0;
  1583. __be32 *p = buffer;
  1584. __be32 status;
  1585. int err;
  1586. int aclsupport = 0;
  1587. struct nfs4_acl *acl = NULL;
  1588. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1589. u32 minorversion = resp->cstate.minorversion;
  1590. struct path path = {
  1591. .mnt = exp->ex_path.mnt,
  1592. .dentry = dentry,
  1593. };
  1594. BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
  1595. BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
  1596. BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
  1597. BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
  1598. if (exp->ex_fslocs.migrated) {
  1599. BUG_ON(bmval[2]);
  1600. status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
  1601. if (status)
  1602. goto out;
  1603. }
  1604. err = vfs_getattr(exp->ex_path.mnt, dentry, &stat);
  1605. if (err)
  1606. goto out_nfserr;
  1607. if ((bmval0 & (FATTR4_WORD0_FILES_FREE | FATTR4_WORD0_FILES_TOTAL |
  1608. FATTR4_WORD0_MAXNAME)) ||
  1609. (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
  1610. FATTR4_WORD1_SPACE_TOTAL))) {
  1611. err = vfs_statfs(&path, &statfs);
  1612. if (err)
  1613. goto out_nfserr;
  1614. }
  1615. if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
  1616. fh_init(&tempfh, NFS4_FHSIZE);
  1617. status = fh_compose(&tempfh, exp, dentry, NULL);
  1618. if (status)
  1619. goto out;
  1620. fhp = &tempfh;
  1621. }
  1622. if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
  1623. | FATTR4_WORD0_SUPPORTED_ATTRS)) {
  1624. err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
  1625. aclsupport = (err == 0);
  1626. if (bmval0 & FATTR4_WORD0_ACL) {
  1627. if (err == -EOPNOTSUPP)
  1628. bmval0 &= ~FATTR4_WORD0_ACL;
  1629. else if (err == -EINVAL) {
  1630. status = nfserr_attrnotsupp;
  1631. goto out;
  1632. } else if (err != 0)
  1633. goto out_nfserr;
  1634. }
  1635. }
  1636. if (bmval2) {
  1637. if ((buflen -= 16) < 0)
  1638. goto out_resource;
  1639. WRITE32(3);
  1640. WRITE32(bmval0);
  1641. WRITE32(bmval1);
  1642. WRITE32(bmval2);
  1643. } else if (bmval1) {
  1644. if ((buflen -= 12) < 0)
  1645. goto out_resource;
  1646. WRITE32(2);
  1647. WRITE32(bmval0);
  1648. WRITE32(bmval1);
  1649. } else {
  1650. if ((buflen -= 8) < 0)
  1651. goto out_resource;
  1652. WRITE32(1);
  1653. WRITE32(bmval0);
  1654. }
  1655. attrlenp = p++; /* to be backfilled later */
  1656. if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  1657. u32 word0 = nfsd_suppattrs0(minorversion);
  1658. u32 word1 = nfsd_suppattrs1(minorversion);
  1659. u32 word2 = nfsd_suppattrs2(minorversion);
  1660. if (!aclsupport)
  1661. word0 &= ~FATTR4_WORD0_ACL;
  1662. if (!word2) {
  1663. if ((buflen -= 12) < 0)
  1664. goto out_resource;
  1665. WRITE32(2);
  1666. WRITE32(word0);
  1667. WRITE32(word1);
  1668. } else {
  1669. if ((buflen -= 16) < 0)
  1670. goto out_resource;
  1671. WRITE32(3);
  1672. WRITE32(word0);
  1673. WRITE32(word1);
  1674. WRITE32(word2);
  1675. }
  1676. }
  1677. if (bmval0 & FATTR4_WORD0_TYPE) {
  1678. if ((buflen -= 4) < 0)
  1679. goto out_resource;
  1680. dummy = nfs4_ftypes[(stat.mode & S_IFMT) >> 12];
  1681. if (dummy == NF4BAD)
  1682. goto out_serverfault;
  1683. WRITE32(dummy);
  1684. }
  1685. if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
  1686. if ((buflen -= 4) < 0)
  1687. goto out_resource;
  1688. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  1689. WRITE32(NFS4_FH_PERSISTENT);
  1690. else
  1691. WRITE32(NFS4_FH_PERSISTENT|NFS4_FH_VOL_RENAME);
  1692. }
  1693. if (bmval0 & FATTR4_WORD0_CHANGE) {
  1694. if ((buflen -= 8) < 0)
  1695. goto out_resource;
  1696. write_change(&p, &stat, dentry->d_inode);
  1697. }
  1698. if (bmval0 & FATTR4_WORD0_SIZE) {
  1699. if ((buflen -= 8) < 0)
  1700. goto out_resource;
  1701. WRITE64(stat.size);
  1702. }
  1703. if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
  1704. if ((buflen -= 4) < 0)
  1705. goto out_resource;
  1706. WRITE32(1);
  1707. }
  1708. if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
  1709. if ((buflen -= 4) < 0)
  1710. goto out_resource;
  1711. WRITE32(1);
  1712. }
  1713. if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
  1714. if ((buflen -= 4) < 0)
  1715. goto out_resource;
  1716. WRITE32(0);
  1717. }
  1718. if (bmval0 & FATTR4_WORD0_FSID) {
  1719. if ((buflen -= 16) < 0)
  1720. goto out_resource;
  1721. if (exp->ex_fslocs.migrated) {
  1722. WRITE64(NFS4_REFERRAL_FSID_MAJOR);
  1723. WRITE64(NFS4_REFERRAL_FSID_MINOR);
  1724. } else switch(fsid_source(fhp)) {
  1725. case FSIDSOURCE_FSID:
  1726. WRITE64((u64)exp->ex_fsid);
  1727. WRITE64((u64)0);
  1728. break;
  1729. case FSIDSOURCE_DEV:
  1730. WRITE32(0);
  1731. WRITE32(MAJOR(stat.dev));
  1732. WRITE32(0);
  1733. WRITE32(MINOR(stat.dev));
  1734. break;
  1735. case FSIDSOURCE_UUID:
  1736. WRITEMEM(exp->ex_uuid, 16);
  1737. break;
  1738. }
  1739. }
  1740. if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
  1741. if ((buflen -= 4) < 0)
  1742. goto out_resource;
  1743. WRITE32(0);
  1744. }
  1745. if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
  1746. if ((buflen -= 4) < 0)
  1747. goto out_resource;
  1748. WRITE32(nfsd4_lease);
  1749. }
  1750. if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
  1751. if ((buflen -= 4) < 0)
  1752. goto out_resource;
  1753. WRITE32(rdattr_err);
  1754. }
  1755. if (bmval0 & FATTR4_WORD0_ACL) {
  1756. struct nfs4_ace *ace;
  1757. if (acl == NULL) {
  1758. if ((buflen -= 4) < 0)
  1759. goto out_resource;
  1760. WRITE32(0);
  1761. goto out_acl;
  1762. }
  1763. if ((buflen -= 4) < 0)
  1764. goto out_resource;
  1765. WRITE32(acl->naces);
  1766. for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
  1767. if ((buflen -= 4*3) < 0)
  1768. goto out_resource;
  1769. WRITE32(ace->type);
  1770. WRITE32(ace->flag);
  1771. WRITE32(ace->access_mask & NFS4_ACE_MASK_ALL);
  1772. status = nfsd4_encode_aclname(rqstp, ace->whotype,
  1773. ace->who, ace->flag & NFS4_ACE_IDENTIFIER_GROUP,
  1774. &p, &buflen);
  1775. if (status == nfserr_resource)
  1776. goto out_resource;
  1777. if (status)
  1778. goto out;
  1779. }
  1780. }
  1781. out_acl:
  1782. if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
  1783. if ((buflen -= 4) < 0)
  1784. goto out_resource;
  1785. WRITE32(aclsupport ?
  1786. ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
  1787. }
  1788. if (bmval0 & FATTR4_WORD0_CANSETTIME) {
  1789. if ((buflen -= 4) < 0)
  1790. goto out_resource;
  1791. WRITE32(1);
  1792. }
  1793. if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
  1794. if ((buflen -= 4) < 0)
  1795. goto out_resource;
  1796. WRITE32(1);
  1797. }
  1798. if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
  1799. if ((buflen -= 4) < 0)
  1800. goto out_resource;
  1801. WRITE32(1);
  1802. }
  1803. if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
  1804. if ((buflen -= 4) < 0)
  1805. goto out_resource;
  1806. WRITE32(1);
  1807. }
  1808. if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
  1809. buflen -= (XDR_QUADLEN(fhp->fh_handle.fh_size) << 2) + 4;
  1810. if (buflen < 0)
  1811. goto out_resource;
  1812. WRITE32(fhp->fh_handle.fh_size);
  1813. WRITEMEM(&fhp->fh_handle.fh_base, fhp->fh_handle.fh_size);
  1814. }
  1815. if (bmval0 & FATTR4_WORD0_FILEID) {
  1816. if ((buflen -= 8) < 0)
  1817. goto out_resource;
  1818. WRITE64(stat.ino);
  1819. }
  1820. if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
  1821. if ((buflen -= 8) < 0)
  1822. goto out_resource;
  1823. WRITE64((u64) statfs.f_ffree);
  1824. }
  1825. if (bmval0 & FATTR4_WORD0_FILES_FREE) {
  1826. if ((buflen -= 8) < 0)
  1827. goto out_resource;
  1828. WRITE64((u64) statfs.f_ffree);
  1829. }
  1830. if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
  1831. if ((buflen -= 8) < 0)
  1832. goto out_resource;
  1833. WRITE64((u64) statfs.f_files);
  1834. }
  1835. if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
  1836. status = nfsd4_encode_fs_locations(rqstp, exp, &p, &buflen);
  1837. if (status == nfserr_resource)
  1838. goto out_resource;
  1839. if (status)
  1840. goto out;
  1841. }
  1842. if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
  1843. if ((buflen -= 4) < 0)
  1844. goto out_resource;
  1845. WRITE32(1);
  1846. }
  1847. if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
  1848. if ((buflen -= 8) < 0)
  1849. goto out_resource;
  1850. WRITE64(~(u64)0);
  1851. }
  1852. if (bmval0 & FATTR4_WORD0_MAXLINK) {
  1853. if ((buflen -= 4) < 0)
  1854. goto out_resource;
  1855. WRITE32(255);
  1856. }
  1857. if (bmval0 & FATTR4_WORD0_MAXNAME) {
  1858. if ((buflen -= 4) < 0)
  1859. goto out_resource;
  1860. WRITE32(statfs.f_namelen);
  1861. }
  1862. if (bmval0 & FATTR4_WORD0_MAXREAD) {
  1863. if ((buflen -= 8) < 0)
  1864. goto out_resource;
  1865. WRITE64((u64) svc_max_payload(rqstp));
  1866. }
  1867. if (bmval0 & FATTR4_WORD0_MAXWRITE) {
  1868. if ((buflen -= 8) < 0)
  1869. goto out_resource;
  1870. WRITE64((u64) svc_max_payload(rqstp));
  1871. }
  1872. if (bmval1 & FATTR4_WORD1_MODE) {
  1873. if ((buflen -= 4) < 0)
  1874. goto out_resource;
  1875. WRITE32(stat.mode & S_IALLUGO);
  1876. }
  1877. if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
  1878. if ((buflen -= 4) < 0)
  1879. goto out_resource;
  1880. WRITE32(1);
  1881. }
  1882. if (bmval1 & FATTR4_WORD1_NUMLINKS) {
  1883. if ((buflen -= 4) < 0)
  1884. goto out_resource;
  1885. WRITE32(stat.nlink);
  1886. }
  1887. if (bmval1 & FATTR4_WORD1_OWNER) {
  1888. status = nfsd4_encode_user(rqstp, stat.uid, &p, &buflen);
  1889. if (status == nfserr_resource)
  1890. goto out_resource;
  1891. if (status)
  1892. goto out;
  1893. }
  1894. if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
  1895. status = nfsd4_encode_group(rqstp, stat.gid, &p, &buflen);
  1896. if (status == nfserr_resource)
  1897. goto out_resource;
  1898. if (status)
  1899. goto out;
  1900. }
  1901. if (bmval1 & FATTR4_WORD1_RAWDEV) {
  1902. if ((buflen -= 8) < 0)
  1903. goto out_resource;
  1904. WRITE32((u32) MAJOR(stat.rdev));
  1905. WRITE32((u32) MINOR(stat.rdev));
  1906. }
  1907. if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
  1908. if ((buflen -= 8) < 0)
  1909. goto out_resource;
  1910. dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
  1911. WRITE64(dummy64);
  1912. }
  1913. if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
  1914. if ((buflen -= 8) < 0)
  1915. goto out_resource;
  1916. dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
  1917. WRITE64(dummy64);
  1918. }
  1919. if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
  1920. if ((buflen -= 8) < 0)
  1921. goto out_resource;
  1922. dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
  1923. WRITE64(dummy64);
  1924. }
  1925. if (bmval1 & FATTR4_WORD1_SPACE_USED) {
  1926. if ((buflen -= 8) < 0)
  1927. goto out_resource;
  1928. dummy64 = (u64)stat.blocks << 9;
  1929. WRITE64(dummy64);
  1930. }
  1931. if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
  1932. if ((buflen -= 12) < 0)
  1933. goto out_resource;
  1934. WRITE32(0);
  1935. WRITE32(stat.atime.tv_sec);
  1936. WRITE32(stat.atime.tv_nsec);
  1937. }
  1938. if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
  1939. if ((buflen -= 12) < 0)
  1940. goto out_resource;
  1941. WRITE32(0);
  1942. WRITE32(1);
  1943. WRITE32(0);
  1944. }
  1945. if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
  1946. if ((buflen -= 12) < 0)
  1947. goto out_resource;
  1948. WRITE32(0);
  1949. WRITE32(stat.ctime.tv_sec);
  1950. WRITE32(stat.ctime.tv_nsec);
  1951. }
  1952. if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
  1953. if ((buflen -= 12) < 0)
  1954. goto out_resource;
  1955. WRITE32(0);
  1956. WRITE32(stat.mtime.tv_sec);
  1957. WRITE32(stat.mtime.tv_nsec);
  1958. }
  1959. if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
  1960. if ((buflen -= 8) < 0)
  1961. goto out_resource;
  1962. /*
  1963. * Get parent's attributes if not ignoring crossmount
  1964. * and this is the root of a cross-mounted filesystem.
  1965. */
  1966. if (ignore_crossmnt == 0 &&
  1967. dentry == exp->ex_path.mnt->mnt_root) {
  1968. struct path path = exp->ex_path;
  1969. path_get(&path);
  1970. while (follow_up(&path)) {
  1971. if (path.dentry != path.mnt->mnt_root)
  1972. break;
  1973. }
  1974. err = vfs_getattr(path.mnt, path.dentry, &stat);
  1975. path_put(&path);
  1976. if (err)
  1977. goto out_nfserr;
  1978. }
  1979. WRITE64(stat.ino);
  1980. }
  1981. if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
  1982. WRITE32(3);
  1983. WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
  1984. WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
  1985. WRITE32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
  1986. }
  1987. *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
  1988. *countp = p - buffer;
  1989. status = nfs_ok;
  1990. out:
  1991. kfree(acl);
  1992. if (fhp == &tempfh)
  1993. fh_put(&tempfh);
  1994. return status;
  1995. out_nfserr:
  1996. status = nfserrno(err);
  1997. goto out;
  1998. out_resource:
  1999. *countp = 0;
  2000. status = nfserr_resource;
  2001. goto out;
  2002. out_serverfault:
  2003. status = nfserr_serverfault;
  2004. goto out;
  2005. }
  2006. static inline int attributes_need_mount(u32 *bmval)
  2007. {
  2008. if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
  2009. return 1;
  2010. if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
  2011. return 1;
  2012. return 0;
  2013. }
  2014. static __be32
  2015. nfsd4_encode_dirent_fattr(struct nfsd4_readdir *cd,
  2016. const char *name, int namlen, __be32 *p, int *buflen)
  2017. {
  2018. struct svc_export *exp = cd->rd_fhp->fh_export;
  2019. struct dentry *dentry;
  2020. __be32 nfserr;
  2021. int ignore_crossmnt = 0;
  2022. dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
  2023. if (IS_ERR(dentry))
  2024. return nfserrno(PTR_ERR(dentry));
  2025. if (!dentry->d_inode) {
  2026. /*
  2027. * nfsd_buffered_readdir drops the i_mutex between
  2028. * readdir and calling this callback, leaving a window
  2029. * where this directory entry could have gone away.
  2030. */
  2031. dput(dentry);
  2032. return nfserr_noent;
  2033. }
  2034. exp_get(exp);
  2035. /*
  2036. * In the case of a mountpoint, the client may be asking for
  2037. * attributes that are only properties of the underlying filesystem
  2038. * as opposed to the cross-mounted file system. In such a case,
  2039. * we will not follow the cross mount and will fill the attribtutes
  2040. * directly from the mountpoint dentry.
  2041. */
  2042. if (nfsd_mountpoint(dentry, exp)) {
  2043. int err;
  2044. if (!(exp->ex_flags & NFSEXP_V4ROOT)
  2045. && !attributes_need_mount(cd->rd_bmval)) {
  2046. ignore_crossmnt = 1;
  2047. goto out_encode;
  2048. }
  2049. /*
  2050. * Why the heck aren't we just using nfsd_lookup??
  2051. * Different "."/".." handling? Something else?
  2052. * At least, add a comment here to explain....
  2053. */
  2054. err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
  2055. if (err) {
  2056. nfserr = nfserrno(err);
  2057. goto out_put;
  2058. }
  2059. nfserr = check_nfsd_access(exp, cd->rd_rqstp);
  2060. if (nfserr)
  2061. goto out_put;
  2062. }
  2063. out_encode:
  2064. nfserr = nfsd4_encode_fattr(NULL, exp, dentry, p, buflen, cd->rd_bmval,
  2065. cd->rd_rqstp, ignore_crossmnt);
  2066. out_put:
  2067. dput(dentry);
  2068. exp_put(exp);
  2069. return nfserr;
  2070. }
  2071. static __be32 *
  2072. nfsd4_encode_rdattr_error(__be32 *p, int buflen, __be32 nfserr)
  2073. {
  2074. __be32 *attrlenp;
  2075. if (buflen < 6)
  2076. return NULL;
  2077. *p++ = htonl(2);
  2078. *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
  2079. *p++ = htonl(0); /* bmval1 */
  2080. attrlenp = p++;
  2081. *p++ = nfserr; /* no htonl */
  2082. *attrlenp = htonl((char *)p - (char *)attrlenp - 4);
  2083. return p;
  2084. }
  2085. static int
  2086. nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
  2087. loff_t offset, u64 ino, unsigned int d_type)
  2088. {
  2089. struct readdir_cd *ccd = ccdv;
  2090. struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
  2091. int buflen;
  2092. __be32 *p = cd->buffer;
  2093. __be32 *cookiep;
  2094. __be32 nfserr = nfserr_toosmall;
  2095. /* In nfsv4, "." and ".." never make it onto the wire.. */
  2096. if (name && isdotent(name, namlen)) {
  2097. cd->common.err = nfs_ok;
  2098. return 0;
  2099. }
  2100. if (cd->offset)
  2101. xdr_encode_hyper(cd->offset, (u64) offset);
  2102. buflen = cd->buflen - 4 - XDR_QUADLEN(namlen);
  2103. if (buflen < 0)
  2104. goto fail;
  2105. *p++ = xdr_one; /* mark entry present */
  2106. cookiep = p;
  2107. p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
  2108. p = xdr_encode_array(p, name, namlen); /* name length & name */
  2109. nfserr = nfsd4_encode_dirent_fattr(cd, name, namlen, p, &buflen);
  2110. switch (nfserr) {
  2111. case nfs_ok:
  2112. p += buflen;
  2113. break;
  2114. case nfserr_resource:
  2115. nfserr = nfserr_toosmall;
  2116. goto fail;
  2117. case nfserr_noent:
  2118. goto skip_entry;
  2119. default:
  2120. /*
  2121. * If the client requested the RDATTR_ERROR attribute,
  2122. * we stuff the error code into this attribute
  2123. * and continue. If this attribute was not requested,
  2124. * then in accordance with the spec, we fail the
  2125. * entire READDIR operation(!)
  2126. */
  2127. if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
  2128. goto fail;
  2129. p = nfsd4_encode_rdattr_error(p, buflen, nfserr);
  2130. if (p == NULL) {
  2131. nfserr = nfserr_toosmall;
  2132. goto fail;
  2133. }
  2134. }
  2135. cd->buflen -= (p - cd->buffer);
  2136. cd->buffer = p;
  2137. cd->offset = cookiep;
  2138. skip_entry:
  2139. cd->common.err = nfs_ok;
  2140. return 0;
  2141. fail:
  2142. cd->common.err = nfserr;
  2143. return -EINVAL;
  2144. }
  2145. static void
  2146. nfsd4_encode_stateid(struct nfsd4_compoundres *resp, stateid_t *sid)
  2147. {
  2148. __be32 *p;
  2149. RESERVE_SPACE(sizeof(stateid_t));
  2150. WRITE32(sid->si_generation);
  2151. WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
  2152. ADJUST_ARGS();
  2153. }
  2154. static __be32
  2155. nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
  2156. {
  2157. __be32 *p;
  2158. if (!nfserr) {
  2159. RESERVE_SPACE(8);
  2160. WRITE32(access->ac_supported);
  2161. WRITE32(access->ac_resp_access);
  2162. ADJUST_ARGS();
  2163. }
  2164. return nfserr;
  2165. }
  2166. static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
  2167. {
  2168. __be32 *p;
  2169. if (!nfserr) {
  2170. RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 8);
  2171. WRITEMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  2172. WRITE32(bcts->dir);
  2173. /* XXX: ? */
  2174. WRITE32(0);
  2175. ADJUST_ARGS();
  2176. }
  2177. return nfserr;
  2178. }
  2179. static __be32
  2180. nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
  2181. {
  2182. ENCODE_SEQID_OP_HEAD;
  2183. if (!nfserr)
  2184. nfsd4_encode_stateid(resp, &close->cl_stateid);
  2185. ENCODE_SEQID_OP_TAIL(close->cl_stateowner);
  2186. return nfserr;
  2187. }
  2188. static __be32
  2189. nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
  2190. {
  2191. __be32 *p;
  2192. if (!nfserr) {
  2193. RESERVE_SPACE(8);
  2194. WRITEMEM(commit->co_verf.data, 8);
  2195. ADJUST_ARGS();
  2196. }
  2197. return nfserr;
  2198. }
  2199. static __be32
  2200. nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
  2201. {
  2202. __be32 *p;
  2203. if (!nfserr) {
  2204. RESERVE_SPACE(32);
  2205. write_cinfo(&p, &create->cr_cinfo);
  2206. WRITE32(2);
  2207. WRITE32(create->cr_bmval[0]);
  2208. WRITE32(create->cr_bmval[1]);
  2209. ADJUST_ARGS();
  2210. }
  2211. return nfserr;
  2212. }
  2213. static __be32
  2214. nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
  2215. {
  2216. struct svc_fh *fhp = getattr->ga_fhp;
  2217. int buflen;
  2218. if (nfserr)
  2219. return nfserr;
  2220. buflen = resp->end - resp->p - (COMPOUND_ERR_SLACK_SPACE >> 2);
  2221. nfserr = nfsd4_encode_fattr(fhp, fhp->fh_export, fhp->fh_dentry,
  2222. resp->p, &buflen, getattr->ga_bmval,
  2223. resp->rqstp, 0);
  2224. if (!nfserr)
  2225. resp->p += buflen;
  2226. return nfserr;
  2227. }
  2228. static __be32
  2229. nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
  2230. {
  2231. struct svc_fh *fhp = *fhpp;
  2232. unsigned int len;
  2233. __be32 *p;
  2234. if (!nfserr) {
  2235. len = fhp->fh_handle.fh_size;
  2236. RESERVE_SPACE(len + 4);
  2237. WRITE32(len);
  2238. WRITEMEM(&fhp->fh_handle.fh_base, len);
  2239. ADJUST_ARGS();
  2240. }
  2241. return nfserr;
  2242. }
  2243. /*
  2244. * Including all fields other than the name, a LOCK4denied structure requires
  2245. * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
  2246. */
  2247. static void
  2248. nfsd4_encode_lock_denied(struct nfsd4_compoundres *resp, struct nfsd4_lock_denied *ld)
  2249. {
  2250. __be32 *p;
  2251. RESERVE_SPACE(32 + XDR_LEN(ld->ld_sop ? ld->ld_sop->so_owner.len : 0));
  2252. WRITE64(ld->ld_start);
  2253. WRITE64(ld->ld_length);
  2254. WRITE32(ld->ld_type);
  2255. if (ld->ld_sop) {
  2256. WRITEMEM(&ld->ld_clientid, 8);
  2257. WRITE32(ld->ld_sop->so_owner.len);
  2258. WRITEMEM(ld->ld_sop->so_owner.data, ld->ld_sop->so_owner.len);
  2259. kref_put(&ld->ld_sop->so_ref, nfs4_free_stateowner);
  2260. } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
  2261. WRITE64((u64)0); /* clientid */
  2262. WRITE32(0); /* length of owner name */
  2263. }
  2264. ADJUST_ARGS();
  2265. }
  2266. static __be32
  2267. nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
  2268. {
  2269. ENCODE_SEQID_OP_HEAD;
  2270. if (!nfserr)
  2271. nfsd4_encode_stateid(resp, &lock->lk_resp_stateid);
  2272. else if (nfserr == nfserr_denied)
  2273. nfsd4_encode_lock_denied(resp, &lock->lk_denied);
  2274. ENCODE_SEQID_OP_TAIL(lock->lk_replay_owner);
  2275. return nfserr;
  2276. }
  2277. static __be32
  2278. nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
  2279. {
  2280. if (nfserr == nfserr_denied)
  2281. nfsd4_encode_lock_denied(resp, &lockt->lt_denied);
  2282. return nfserr;
  2283. }
  2284. static __be32
  2285. nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
  2286. {
  2287. ENCODE_SEQID_OP_HEAD;
  2288. if (!nfserr)
  2289. nfsd4_encode_stateid(resp, &locku->lu_stateid);
  2290. ENCODE_SEQID_OP_TAIL(locku->lu_stateowner);
  2291. return nfserr;
  2292. }
  2293. static __be32
  2294. nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
  2295. {
  2296. __be32 *p;
  2297. if (!nfserr) {
  2298. RESERVE_SPACE(20);
  2299. write_cinfo(&p, &link->li_cinfo);
  2300. ADJUST_ARGS();
  2301. }
  2302. return nfserr;
  2303. }
  2304. static __be32
  2305. nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
  2306. {
  2307. __be32 *p;
  2308. ENCODE_SEQID_OP_HEAD;
  2309. if (nfserr)
  2310. goto out;
  2311. nfsd4_encode_stateid(resp, &open->op_stateid);
  2312. RESERVE_SPACE(40);
  2313. write_cinfo(&p, &open->op_cinfo);
  2314. WRITE32(open->op_rflags);
  2315. WRITE32(2);
  2316. WRITE32(open->op_bmval[0]);
  2317. WRITE32(open->op_bmval[1]);
  2318. WRITE32(open->op_delegate_type);
  2319. ADJUST_ARGS();
  2320. switch (open->op_delegate_type) {
  2321. case NFS4_OPEN_DELEGATE_NONE:
  2322. break;
  2323. case NFS4_OPEN_DELEGATE_READ:
  2324. nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
  2325. RESERVE_SPACE(20);
  2326. WRITE32(open->op_recall);
  2327. /*
  2328. * TODO: ACE's in delegations
  2329. */
  2330. WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2331. WRITE32(0);
  2332. WRITE32(0);
  2333. WRITE32(0); /* XXX: is NULL principal ok? */
  2334. ADJUST_ARGS();
  2335. break;
  2336. case NFS4_OPEN_DELEGATE_WRITE:
  2337. nfsd4_encode_stateid(resp, &open->op_delegate_stateid);
  2338. RESERVE_SPACE(32);
  2339. WRITE32(0);
  2340. /*
  2341. * TODO: space_limit's in delegations
  2342. */
  2343. WRITE32(NFS4_LIMIT_SIZE);
  2344. WRITE32(~(u32)0);
  2345. WRITE32(~(u32)0);
  2346. /*
  2347. * TODO: ACE's in delegations
  2348. */
  2349. WRITE32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2350. WRITE32(0);
  2351. WRITE32(0);
  2352. WRITE32(0); /* XXX: is NULL principal ok? */
  2353. ADJUST_ARGS();
  2354. break;
  2355. default:
  2356. BUG();
  2357. }
  2358. /* XXX save filehandle here */
  2359. out:
  2360. ENCODE_SEQID_OP_TAIL(open->op_stateowner);
  2361. return nfserr;
  2362. }
  2363. static __be32
  2364. nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
  2365. {
  2366. ENCODE_SEQID_OP_HEAD;
  2367. if (!nfserr)
  2368. nfsd4_encode_stateid(resp, &oc->oc_resp_stateid);
  2369. ENCODE_SEQID_OP_TAIL(oc->oc_stateowner);
  2370. return nfserr;
  2371. }
  2372. static __be32
  2373. nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
  2374. {
  2375. ENCODE_SEQID_OP_HEAD;
  2376. if (!nfserr)
  2377. nfsd4_encode_stateid(resp, &od->od_stateid);
  2378. ENCODE_SEQID_OP_TAIL(od->od_stateowner);
  2379. return nfserr;
  2380. }
  2381. static __be32
  2382. nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
  2383. struct nfsd4_read *read)
  2384. {
  2385. u32 eof;
  2386. int v, pn;
  2387. unsigned long maxcount;
  2388. long len;
  2389. __be32 *p;
  2390. if (nfserr)
  2391. return nfserr;
  2392. if (resp->xbuf->page_len)
  2393. return nfserr_resource;
  2394. RESERVE_SPACE(8); /* eof flag and byte count */
  2395. maxcount = svc_max_payload(resp->rqstp);
  2396. if (maxcount > read->rd_length)
  2397. maxcount = read->rd_length;
  2398. len = maxcount;
  2399. v = 0;
  2400. while (len > 0) {
  2401. pn = resp->rqstp->rq_resused++;
  2402. resp->rqstp->rq_vec[v].iov_base =
  2403. page_address(resp->rqstp->rq_respages[pn]);
  2404. resp->rqstp->rq_vec[v].iov_len =
  2405. len < PAGE_SIZE ? len : PAGE_SIZE;
  2406. v++;
  2407. len -= PAGE_SIZE;
  2408. }
  2409. read->rd_vlen = v;
  2410. nfserr = nfsd_read_file(read->rd_rqstp, read->rd_fhp, read->rd_filp,
  2411. read->rd_offset, resp->rqstp->rq_vec, read->rd_vlen,
  2412. &maxcount);
  2413. if (nfserr == nfserr_symlink)
  2414. nfserr = nfserr_inval;
  2415. if (nfserr)
  2416. return nfserr;
  2417. eof = (read->rd_offset + maxcount >=
  2418. read->rd_fhp->fh_dentry->d_inode->i_size);
  2419. WRITE32(eof);
  2420. WRITE32(maxcount);
  2421. ADJUST_ARGS();
  2422. resp->xbuf->head[0].iov_len = (char*)p
  2423. - (char*)resp->xbuf->head[0].iov_base;
  2424. resp->xbuf->page_len = maxcount;
  2425. /* Use rest of head for padding and remaining ops: */
  2426. resp->xbuf->tail[0].iov_base = p;
  2427. resp->xbuf->tail[0].iov_len = 0;
  2428. if (maxcount&3) {
  2429. RESERVE_SPACE(4);
  2430. WRITE32(0);
  2431. resp->xbuf->tail[0].iov_base += maxcount&3;
  2432. resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
  2433. ADJUST_ARGS();
  2434. }
  2435. return 0;
  2436. }
  2437. static __be32
  2438. nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
  2439. {
  2440. int maxcount;
  2441. char *page;
  2442. __be32 *p;
  2443. if (nfserr)
  2444. return nfserr;
  2445. if (resp->xbuf->page_len)
  2446. return nfserr_resource;
  2447. page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
  2448. maxcount = PAGE_SIZE;
  2449. RESERVE_SPACE(4);
  2450. /*
  2451. * XXX: By default, the ->readlink() VFS op will truncate symlinks
  2452. * if they would overflow the buffer. Is this kosher in NFSv4? If
  2453. * not, one easy fix is: if ->readlink() precisely fills the buffer,
  2454. * assume that truncation occurred, and return NFS4ERR_RESOURCE.
  2455. */
  2456. nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp, page, &maxcount);
  2457. if (nfserr == nfserr_isdir)
  2458. return nfserr_inval;
  2459. if (nfserr)
  2460. return nfserr;
  2461. WRITE32(maxcount);
  2462. ADJUST_ARGS();
  2463. resp->xbuf->head[0].iov_len = (char*)p
  2464. - (char*)resp->xbuf->head[0].iov_base;
  2465. resp->xbuf->page_len = maxcount;
  2466. /* Use rest of head for padding and remaining ops: */
  2467. resp->xbuf->tail[0].iov_base = p;
  2468. resp->xbuf->tail[0].iov_len = 0;
  2469. if (maxcount&3) {
  2470. RESERVE_SPACE(4);
  2471. WRITE32(0);
  2472. resp->xbuf->tail[0].iov_base += maxcount&3;
  2473. resp->xbuf->tail[0].iov_len = 4 - (maxcount&3);
  2474. ADJUST_ARGS();
  2475. }
  2476. return 0;
  2477. }
  2478. static __be32
  2479. nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
  2480. {
  2481. int maxcount;
  2482. loff_t offset;
  2483. __be32 *page, *savep, *tailbase;
  2484. __be32 *p;
  2485. if (nfserr)
  2486. return nfserr;
  2487. if (resp->xbuf->page_len)
  2488. return nfserr_resource;
  2489. RESERVE_SPACE(8); /* verifier */
  2490. savep = p;
  2491. /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
  2492. WRITE32(0);
  2493. WRITE32(0);
  2494. ADJUST_ARGS();
  2495. resp->xbuf->head[0].iov_len = ((char*)resp->p) - (char*)resp->xbuf->head[0].iov_base;
  2496. tailbase = p;
  2497. maxcount = PAGE_SIZE;
  2498. if (maxcount > readdir->rd_maxcount)
  2499. maxcount = readdir->rd_maxcount;
  2500. /*
  2501. * Convert from bytes to words, account for the two words already
  2502. * written, make sure to leave two words at the end for the next
  2503. * pointer and eof field.
  2504. */
  2505. maxcount = (maxcount >> 2) - 4;
  2506. if (maxcount < 0) {
  2507. nfserr = nfserr_toosmall;
  2508. goto err_no_verf;
  2509. }
  2510. page = page_address(resp->rqstp->rq_respages[resp->rqstp->rq_resused++]);
  2511. readdir->common.err = 0;
  2512. readdir->buflen = maxcount;
  2513. readdir->buffer = page;
  2514. readdir->offset = NULL;
  2515. offset = readdir->rd_cookie;
  2516. nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
  2517. &offset,
  2518. &readdir->common, nfsd4_encode_dirent);
  2519. if (nfserr == nfs_ok &&
  2520. readdir->common.err == nfserr_toosmall &&
  2521. readdir->buffer == page)
  2522. nfserr = nfserr_toosmall;
  2523. if (nfserr == nfserr_symlink)
  2524. nfserr = nfserr_notdir;
  2525. if (nfserr)
  2526. goto err_no_verf;
  2527. if (readdir->offset)
  2528. xdr_encode_hyper(readdir->offset, offset);
  2529. p = readdir->buffer;
  2530. *p++ = 0; /* no more entries */
  2531. *p++ = htonl(readdir->common.err == nfserr_eof);
  2532. resp->xbuf->page_len = ((char*)p) - (char*)page_address(
  2533. resp->rqstp->rq_respages[resp->rqstp->rq_resused-1]);
  2534. /* Use rest of head for padding and remaining ops: */
  2535. resp->xbuf->tail[0].iov_base = tailbase;
  2536. resp->xbuf->tail[0].iov_len = 0;
  2537. resp->p = resp->xbuf->tail[0].iov_base;
  2538. resp->end = resp->p + (PAGE_SIZE - resp->xbuf->head[0].iov_len)/4;
  2539. return 0;
  2540. err_no_verf:
  2541. p = savep;
  2542. ADJUST_ARGS();
  2543. return nfserr;
  2544. }
  2545. static __be32
  2546. nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
  2547. {
  2548. __be32 *p;
  2549. if (!nfserr) {
  2550. RESERVE_SPACE(20);
  2551. write_cinfo(&p, &remove->rm_cinfo);
  2552. ADJUST_ARGS();
  2553. }
  2554. return nfserr;
  2555. }
  2556. static __be32
  2557. nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
  2558. {
  2559. __be32 *p;
  2560. if (!nfserr) {
  2561. RESERVE_SPACE(40);
  2562. write_cinfo(&p, &rename->rn_sinfo);
  2563. write_cinfo(&p, &rename->rn_tinfo);
  2564. ADJUST_ARGS();
  2565. }
  2566. return nfserr;
  2567. }
  2568. static __be32
  2569. nfsd4_do_encode_secinfo(struct nfsd4_compoundres *resp,
  2570. __be32 nfserr,struct svc_export *exp)
  2571. {
  2572. int i = 0;
  2573. u32 nflavs;
  2574. struct exp_flavor_info *flavs;
  2575. struct exp_flavor_info def_flavs[2];
  2576. __be32 *p;
  2577. if (nfserr)
  2578. goto out;
  2579. if (exp->ex_nflavors) {
  2580. flavs = exp->ex_flavors;
  2581. nflavs = exp->ex_nflavors;
  2582. } else { /* Handling of some defaults in absence of real secinfo: */
  2583. flavs = def_flavs;
  2584. if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
  2585. nflavs = 2;
  2586. flavs[0].pseudoflavor = RPC_AUTH_UNIX;
  2587. flavs[1].pseudoflavor = RPC_AUTH_NULL;
  2588. } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
  2589. nflavs = 1;
  2590. flavs[0].pseudoflavor
  2591. = svcauth_gss_flavor(exp->ex_client);
  2592. } else {
  2593. nflavs = 1;
  2594. flavs[0].pseudoflavor
  2595. = exp->ex_client->flavour->flavour;
  2596. }
  2597. }
  2598. RESERVE_SPACE(4);
  2599. WRITE32(nflavs);
  2600. ADJUST_ARGS();
  2601. for (i = 0; i < nflavs; i++) {
  2602. u32 flav = flavs[i].pseudoflavor;
  2603. struct gss_api_mech *gm = gss_mech_get_by_pseudoflavor(flav);
  2604. if (gm) {
  2605. RESERVE_SPACE(4);
  2606. WRITE32(RPC_AUTH_GSS);
  2607. ADJUST_ARGS();
  2608. RESERVE_SPACE(4 + gm->gm_oid.len);
  2609. WRITE32(gm->gm_oid.len);
  2610. WRITEMEM(gm->gm_oid.data, gm->gm_oid.len);
  2611. ADJUST_ARGS();
  2612. RESERVE_SPACE(4);
  2613. WRITE32(0); /* qop */
  2614. ADJUST_ARGS();
  2615. RESERVE_SPACE(4);
  2616. WRITE32(gss_pseudoflavor_to_service(gm, flav));
  2617. ADJUST_ARGS();
  2618. gss_mech_put(gm);
  2619. } else {
  2620. RESERVE_SPACE(4);
  2621. WRITE32(flav);
  2622. ADJUST_ARGS();
  2623. }
  2624. }
  2625. out:
  2626. if (exp)
  2627. exp_put(exp);
  2628. return nfserr;
  2629. }
  2630. static __be32
  2631. nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  2632. struct nfsd4_secinfo *secinfo)
  2633. {
  2634. return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->si_exp);
  2635. }
  2636. static __be32
  2637. nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
  2638. struct nfsd4_secinfo_no_name *secinfo)
  2639. {
  2640. return nfsd4_do_encode_secinfo(resp, nfserr, secinfo->sin_exp);
  2641. }
  2642. /*
  2643. * The SETATTR encode routine is special -- it always encodes a bitmap,
  2644. * regardless of the error status.
  2645. */
  2646. static __be32
  2647. nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
  2648. {
  2649. __be32 *p;
  2650. RESERVE_SPACE(12);
  2651. if (nfserr) {
  2652. WRITE32(2);
  2653. WRITE32(0);
  2654. WRITE32(0);
  2655. }
  2656. else {
  2657. WRITE32(2);
  2658. WRITE32(setattr->sa_bmval[0]);
  2659. WRITE32(setattr->sa_bmval[1]);
  2660. }
  2661. ADJUST_ARGS();
  2662. return nfserr;
  2663. }
  2664. static __be32
  2665. nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
  2666. {
  2667. __be32 *p;
  2668. if (!nfserr) {
  2669. RESERVE_SPACE(8 + sizeof(nfs4_verifier));
  2670. WRITEMEM(&scd->se_clientid, 8);
  2671. WRITEMEM(&scd->se_confirm, sizeof(nfs4_verifier));
  2672. ADJUST_ARGS();
  2673. }
  2674. else if (nfserr == nfserr_clid_inuse) {
  2675. RESERVE_SPACE(8);
  2676. WRITE32(0);
  2677. WRITE32(0);
  2678. ADJUST_ARGS();
  2679. }
  2680. return nfserr;
  2681. }
  2682. static __be32
  2683. nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
  2684. {
  2685. __be32 *p;
  2686. if (!nfserr) {
  2687. RESERVE_SPACE(16);
  2688. WRITE32(write->wr_bytes_written);
  2689. WRITE32(write->wr_how_written);
  2690. WRITEMEM(write->wr_verifier.data, 8);
  2691. ADJUST_ARGS();
  2692. }
  2693. return nfserr;
  2694. }
  2695. static __be32
  2696. nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, int nfserr,
  2697. struct nfsd4_exchange_id *exid)
  2698. {
  2699. __be32 *p;
  2700. char *major_id;
  2701. char *server_scope;
  2702. int major_id_sz;
  2703. int server_scope_sz;
  2704. uint64_t minor_id = 0;
  2705. if (nfserr)
  2706. return nfserr;
  2707. major_id = utsname()->nodename;
  2708. major_id_sz = strlen(major_id);
  2709. server_scope = utsname()->nodename;
  2710. server_scope_sz = strlen(server_scope);
  2711. RESERVE_SPACE(
  2712. 8 /* eir_clientid */ +
  2713. 4 /* eir_sequenceid */ +
  2714. 4 /* eir_flags */ +
  2715. 4 /* spr_how (SP4_NONE) */ +
  2716. 8 /* so_minor_id */ +
  2717. 4 /* so_major_id.len */ +
  2718. (XDR_QUADLEN(major_id_sz) * 4) +
  2719. 4 /* eir_server_scope.len */ +
  2720. (XDR_QUADLEN(server_scope_sz) * 4) +
  2721. 4 /* eir_server_impl_id.count (0) */);
  2722. WRITEMEM(&exid->clientid, 8);
  2723. WRITE32(exid->seqid);
  2724. WRITE32(exid->flags);
  2725. /* state_protect4_r. Currently only support SP4_NONE */
  2726. BUG_ON(exid->spa_how != SP4_NONE);
  2727. WRITE32(exid->spa_how);
  2728. /* The server_owner struct */
  2729. WRITE64(minor_id); /* Minor id */
  2730. /* major id */
  2731. WRITE32(major_id_sz);
  2732. WRITEMEM(major_id, major_id_sz);
  2733. /* Server scope */
  2734. WRITE32(server_scope_sz);
  2735. WRITEMEM(server_scope, server_scope_sz);
  2736. /* Implementation id */
  2737. WRITE32(0); /* zero length nfs_impl_id4 array */
  2738. ADJUST_ARGS();
  2739. return 0;
  2740. }
  2741. static __be32
  2742. nfsd4_encode_create_session(struct nfsd4_compoundres *resp, int nfserr,
  2743. struct nfsd4_create_session *sess)
  2744. {
  2745. __be32 *p;
  2746. if (nfserr)
  2747. return nfserr;
  2748. RESERVE_SPACE(24);
  2749. WRITEMEM(sess->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  2750. WRITE32(sess->seqid);
  2751. WRITE32(sess->flags);
  2752. ADJUST_ARGS();
  2753. RESERVE_SPACE(28);
  2754. WRITE32(0); /* headerpadsz */
  2755. WRITE32(sess->fore_channel.maxreq_sz);
  2756. WRITE32(sess->fore_channel.maxresp_sz);
  2757. WRITE32(sess->fore_channel.maxresp_cached);
  2758. WRITE32(sess->fore_channel.maxops);
  2759. WRITE32(sess->fore_channel.maxreqs);
  2760. WRITE32(sess->fore_channel.nr_rdma_attrs);
  2761. ADJUST_ARGS();
  2762. if (sess->fore_channel.nr_rdma_attrs) {
  2763. RESERVE_SPACE(4);
  2764. WRITE32(sess->fore_channel.rdma_attrs);
  2765. ADJUST_ARGS();
  2766. }
  2767. RESERVE_SPACE(28);
  2768. WRITE32(0); /* headerpadsz */
  2769. WRITE32(sess->back_channel.maxreq_sz);
  2770. WRITE32(sess->back_channel.maxresp_sz);
  2771. WRITE32(sess->back_channel.maxresp_cached);
  2772. WRITE32(sess->back_channel.maxops);
  2773. WRITE32(sess->back_channel.maxreqs);
  2774. WRITE32(sess->back_channel.nr_rdma_attrs);
  2775. ADJUST_ARGS();
  2776. if (sess->back_channel.nr_rdma_attrs) {
  2777. RESERVE_SPACE(4);
  2778. WRITE32(sess->back_channel.rdma_attrs);
  2779. ADJUST_ARGS();
  2780. }
  2781. return 0;
  2782. }
  2783. static __be32
  2784. nfsd4_encode_destroy_session(struct nfsd4_compoundres *resp, int nfserr,
  2785. struct nfsd4_destroy_session *destroy_session)
  2786. {
  2787. return nfserr;
  2788. }
  2789. static __be32
  2790. nfsd4_encode_sequence(struct nfsd4_compoundres *resp, int nfserr,
  2791. struct nfsd4_sequence *seq)
  2792. {
  2793. __be32 *p;
  2794. if (nfserr)
  2795. return nfserr;
  2796. RESERVE_SPACE(NFS4_MAX_SESSIONID_LEN + 20);
  2797. WRITEMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  2798. WRITE32(seq->seqid);
  2799. WRITE32(seq->slotid);
  2800. WRITE32(seq->maxslots);
  2801. /* For now: target_maxslots = maxslots */
  2802. WRITE32(seq->maxslots);
  2803. WRITE32(seq->status_flags);
  2804. ADJUST_ARGS();
  2805. resp->cstate.datap = p; /* DRC cache data pointer */
  2806. return 0;
  2807. }
  2808. static __be32
  2809. nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
  2810. {
  2811. return nfserr;
  2812. }
  2813. typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
  2814. /*
  2815. * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
  2816. * since we don't need to filter out obsolete ops as this is
  2817. * done in the decoding phase.
  2818. */
  2819. static nfsd4_enc nfsd4_enc_ops[] = {
  2820. [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
  2821. [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
  2822. [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
  2823. [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
  2824. [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
  2825. [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  2826. [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
  2827. [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
  2828. [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
  2829. [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
  2830. [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
  2831. [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
  2832. [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
  2833. [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
  2834. [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  2835. [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
  2836. [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
  2837. [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
  2838. [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
  2839. [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
  2840. [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
  2841. [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
  2842. [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
  2843. [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
  2844. [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
  2845. [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
  2846. [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
  2847. [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
  2848. [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
  2849. [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
  2850. [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
  2851. [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
  2852. [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
  2853. [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
  2854. [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  2855. [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
  2856. [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
  2857. /* NFSv4.1 operations */
  2858. [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
  2859. [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
  2860. [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
  2861. [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
  2862. [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_destroy_session,
  2863. [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
  2864. [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  2865. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
  2866. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  2867. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
  2868. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
  2869. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  2870. [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
  2871. [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
  2872. [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
  2873. [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
  2874. [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  2875. [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
  2876. [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
  2877. };
  2878. /*
  2879. * Calculate the total amount of memory that the compound response has taken
  2880. * after encoding the current operation.
  2881. *
  2882. * pad: add on 8 bytes for the next operation's op_code and status so that
  2883. * there is room to cache a failure on the next operation.
  2884. *
  2885. * Compare this length to the session se_fmaxresp_cached.
  2886. *
  2887. * Our se_fmaxresp_cached will always be a multiple of PAGE_SIZE, and so
  2888. * will be at least a page and will therefore hold the xdr_buf head.
  2889. */
  2890. static int nfsd4_check_drc_limit(struct nfsd4_compoundres *resp)
  2891. {
  2892. int status = 0;
  2893. struct xdr_buf *xb = &resp->rqstp->rq_res;
  2894. struct nfsd4_compoundargs *args = resp->rqstp->rq_argp;
  2895. struct nfsd4_session *session = NULL;
  2896. struct nfsd4_slot *slot = resp->cstate.slot;
  2897. u32 length, tlen = 0, pad = 8;
  2898. if (!nfsd4_has_session(&resp->cstate))
  2899. return status;
  2900. session = resp->cstate.session;
  2901. if (session == NULL || slot->sl_cachethis == 0)
  2902. return status;
  2903. if (resp->opcnt >= args->opcnt)
  2904. pad = 0; /* this is the last operation */
  2905. if (xb->page_len == 0) {
  2906. length = (char *)resp->p - (char *)xb->head[0].iov_base + pad;
  2907. } else {
  2908. if (xb->tail[0].iov_base && xb->tail[0].iov_len > 0)
  2909. tlen = (char *)resp->p - (char *)xb->tail[0].iov_base;
  2910. length = xb->head[0].iov_len + xb->page_len + tlen + pad;
  2911. }
  2912. dprintk("%s length %u, xb->page_len %u tlen %u pad %u\n", __func__,
  2913. length, xb->page_len, tlen, pad);
  2914. if (length <= session->se_fchannel.maxresp_cached)
  2915. return status;
  2916. else
  2917. return nfserr_rep_too_big_to_cache;
  2918. }
  2919. void
  2920. nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  2921. {
  2922. __be32 *statp;
  2923. __be32 *p;
  2924. RESERVE_SPACE(8);
  2925. WRITE32(op->opnum);
  2926. statp = p++; /* to be backfilled at the end */
  2927. ADJUST_ARGS();
  2928. if (op->opnum == OP_ILLEGAL)
  2929. goto status;
  2930. BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
  2931. !nfsd4_enc_ops[op->opnum]);
  2932. op->status = nfsd4_enc_ops[op->opnum](resp, op->status, &op->u);
  2933. /* nfsd4_check_drc_limit guarantees enough room for error status */
  2934. if (!op->status && nfsd4_check_drc_limit(resp))
  2935. op->status = nfserr_rep_too_big_to_cache;
  2936. status:
  2937. /*
  2938. * Note: We write the status directly, instead of using WRITE32(),
  2939. * since it is already in network byte order.
  2940. */
  2941. *statp = op->status;
  2942. }
  2943. /*
  2944. * Encode the reply stored in the stateowner reply cache
  2945. *
  2946. * XDR note: do not encode rp->rp_buflen: the buffer contains the
  2947. * previously sent already encoded operation.
  2948. *
  2949. * called with nfs4_lock_state() held
  2950. */
  2951. void
  2952. nfsd4_encode_replay(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  2953. {
  2954. __be32 *p;
  2955. struct nfs4_replay *rp = op->replay;
  2956. BUG_ON(!rp);
  2957. RESERVE_SPACE(8);
  2958. WRITE32(op->opnum);
  2959. *p++ = rp->rp_status; /* already xdr'ed */
  2960. ADJUST_ARGS();
  2961. RESERVE_SPACE(rp->rp_buflen);
  2962. WRITEMEM(rp->rp_buf, rp->rp_buflen);
  2963. ADJUST_ARGS();
  2964. }
  2965. int
  2966. nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  2967. {
  2968. return xdr_ressize_check(rqstp, p);
  2969. }
  2970. void nfsd4_release_compoundargs(struct nfsd4_compoundargs *args)
  2971. {
  2972. if (args->ops != args->iops) {
  2973. kfree(args->ops);
  2974. args->ops = args->iops;
  2975. }
  2976. kfree(args->tmpp);
  2977. args->tmpp = NULL;
  2978. while (args->to_free) {
  2979. struct tmpbuf *tb = args->to_free;
  2980. args->to_free = tb->next;
  2981. tb->release(tb->buf);
  2982. kfree(tb);
  2983. }
  2984. }
  2985. int
  2986. nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
  2987. {
  2988. __be32 status;
  2989. args->p = p;
  2990. args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
  2991. args->pagelist = rqstp->rq_arg.pages;
  2992. args->pagelen = rqstp->rq_arg.page_len;
  2993. args->tmpp = NULL;
  2994. args->to_free = NULL;
  2995. args->ops = args->iops;
  2996. args->rqstp = rqstp;
  2997. status = nfsd4_decode_compound(args);
  2998. if (status) {
  2999. nfsd4_release_compoundargs(args);
  3000. }
  3001. return !status;
  3002. }
  3003. int
  3004. nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
  3005. {
  3006. /*
  3007. * All that remains is to write the tag and operation count...
  3008. */
  3009. struct nfsd4_compound_state *cs = &resp->cstate;
  3010. struct kvec *iov;
  3011. p = resp->tagp;
  3012. *p++ = htonl(resp->taglen);
  3013. memcpy(p, resp->tag, resp->taglen);
  3014. p += XDR_QUADLEN(resp->taglen);
  3015. *p++ = htonl(resp->opcnt);
  3016. if (rqstp->rq_res.page_len)
  3017. iov = &rqstp->rq_res.tail[0];
  3018. else
  3019. iov = &rqstp->rq_res.head[0];
  3020. iov->iov_len = ((char*)resp->p) - (char*)iov->iov_base;
  3021. BUG_ON(iov->iov_len > PAGE_SIZE);
  3022. if (nfsd4_has_session(cs)) {
  3023. if (cs->status != nfserr_replay_cache) {
  3024. nfsd4_store_cache_entry(resp);
  3025. dprintk("%s: SET SLOT STATE TO AVAILABLE\n", __func__);
  3026. cs->slot->sl_inuse = false;
  3027. }
  3028. /* Renew the clientid on success and on replay */
  3029. release_session_client(cs->session);
  3030. nfsd4_put_session(cs->session);
  3031. }
  3032. return 1;
  3033. }
  3034. /*
  3035. * Local variables:
  3036. * c-basic-offset: 8
  3037. * End:
  3038. */