nfs4xdr.c 90 KB

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