nfs4xdr.c 90 KB

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