nfs4xdr.c 90 KB

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