nfs4xdr.c 91 KB

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