nfs4xdr.c 86 KB

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