nfs4xdr.c 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453
  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. #include <linux/slab.h>
  36. #include <linux/namei.h>
  37. #include <linux/statfs.h>
  38. #include <linux/utsname.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/sunrpc/svcauth_gss.h>
  41. #include "idmap.h"
  42. #include "acl.h"
  43. #include "xdr4.h"
  44. #include "vfs.h"
  45. #include "state.h"
  46. #include "cache.h"
  47. #include "netns.h"
  48. #include "pnfs.h"
  49. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  50. #include <linux/security.h>
  51. #endif
  52. #define NFSDDBG_FACILITY NFSDDBG_XDR
  53. /*
  54. * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
  55. * directory in order to indicate to the client that a filesystem boundary is present
  56. * We use a fixed fsid for a referral
  57. */
  58. #define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
  59. #define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
  60. static __be32
  61. check_filename(char *str, int len)
  62. {
  63. int i;
  64. if (len == 0)
  65. return nfserr_inval;
  66. if (isdotent(str, len))
  67. return nfserr_badname;
  68. for (i = 0; i < len; i++)
  69. if (str[i] == '/')
  70. return nfserr_badname;
  71. return 0;
  72. }
  73. #define DECODE_HEAD \
  74. __be32 *p; \
  75. __be32 status
  76. #define DECODE_TAIL \
  77. status = 0; \
  78. out: \
  79. return status; \
  80. xdr_error: \
  81. dprintk("NFSD: xdr error (%s:%d)\n", \
  82. __FILE__, __LINE__); \
  83. status = nfserr_bad_xdr; \
  84. goto out
  85. #define READMEM(x,nbytes) do { \
  86. x = (char *)p; \
  87. p += XDR_QUADLEN(nbytes); \
  88. } while (0)
  89. #define SAVEMEM(x,nbytes) do { \
  90. if (!(x = (p==argp->tmp || p == argp->tmpp) ? \
  91. savemem(argp, p, nbytes) : \
  92. (char *)p)) { \
  93. dprintk("NFSD: xdr error (%s:%d)\n", \
  94. __FILE__, __LINE__); \
  95. goto xdr_error; \
  96. } \
  97. p += XDR_QUADLEN(nbytes); \
  98. } while (0)
  99. #define COPYMEM(x,nbytes) do { \
  100. memcpy((x), p, nbytes); \
  101. p += XDR_QUADLEN(nbytes); \
  102. } while (0)
  103. /* READ_BUF, read_buf(): nbytes must be <= PAGE_SIZE */
  104. #define READ_BUF(nbytes) do { \
  105. if (nbytes <= (u32)((char *)argp->end - (char *)argp->p)) { \
  106. p = argp->p; \
  107. argp->p += XDR_QUADLEN(nbytes); \
  108. } else if (!(p = read_buf(argp, nbytes))) { \
  109. dprintk("NFSD: xdr error (%s:%d)\n", \
  110. __FILE__, __LINE__); \
  111. goto xdr_error; \
  112. } \
  113. } while (0)
  114. static void next_decode_page(struct nfsd4_compoundargs *argp)
  115. {
  116. argp->p = page_address(argp->pagelist[0]);
  117. argp->pagelist++;
  118. if (argp->pagelen < PAGE_SIZE) {
  119. argp->end = argp->p + (argp->pagelen>>2);
  120. argp->pagelen = 0;
  121. } else {
  122. argp->end = argp->p + (PAGE_SIZE>>2);
  123. argp->pagelen -= PAGE_SIZE;
  124. }
  125. }
  126. static __be32 *read_buf(struct nfsd4_compoundargs *argp, u32 nbytes)
  127. {
  128. /* We want more bytes than seem to be available.
  129. * Maybe we need a new page, maybe we have just run out
  130. */
  131. unsigned int avail = (char *)argp->end - (char *)argp->p;
  132. __be32 *p;
  133. if (avail + argp->pagelen < nbytes)
  134. return NULL;
  135. if (avail + PAGE_SIZE < nbytes) /* need more than a page !! */
  136. return NULL;
  137. /* ok, we can do it with the current plus the next page */
  138. if (nbytes <= sizeof(argp->tmp))
  139. p = argp->tmp;
  140. else {
  141. kfree(argp->tmpp);
  142. p = argp->tmpp = kmalloc(nbytes, GFP_KERNEL);
  143. if (!p)
  144. return NULL;
  145. }
  146. /*
  147. * The following memcpy is safe because read_buf is always
  148. * called with nbytes > avail, and the two cases above both
  149. * guarantee p points to at least nbytes bytes.
  150. */
  151. memcpy(p, argp->p, avail);
  152. next_decode_page(argp);
  153. memcpy(((char*)p)+avail, argp->p, (nbytes - avail));
  154. argp->p += XDR_QUADLEN(nbytes - avail);
  155. return p;
  156. }
  157. static int zero_clientid(clientid_t *clid)
  158. {
  159. return (clid->cl_boot == 0) && (clid->cl_id == 0);
  160. }
  161. /**
  162. * svcxdr_tmpalloc - allocate memory to be freed after compound processing
  163. * @argp: NFSv4 compound argument structure
  164. * @p: pointer to be freed (with kfree())
  165. *
  166. * Marks @p to be freed when processing the compound operation
  167. * described in @argp finishes.
  168. */
  169. static void *
  170. svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, u32 len)
  171. {
  172. struct svcxdr_tmpbuf *tb;
  173. tb = kmalloc(sizeof(*tb) + len, GFP_KERNEL);
  174. if (!tb)
  175. return NULL;
  176. tb->next = argp->to_free;
  177. argp->to_free = tb;
  178. return tb->buf;
  179. }
  180. /*
  181. * For xdr strings that need to be passed to other kernel api's
  182. * as null-terminated strings.
  183. *
  184. * Note null-terminating in place usually isn't safe since the
  185. * buffer might end on a page boundary.
  186. */
  187. static char *
  188. svcxdr_dupstr(struct nfsd4_compoundargs *argp, void *buf, u32 len)
  189. {
  190. char *p = svcxdr_tmpalloc(argp, len + 1);
  191. if (!p)
  192. return NULL;
  193. memcpy(p, buf, len);
  194. p[len] = '\0';
  195. return p;
  196. }
  197. /**
  198. * savemem - duplicate a chunk of memory for later processing
  199. * @argp: NFSv4 compound argument structure to be freed with
  200. * @p: pointer to be duplicated
  201. * @nbytes: length to be duplicated
  202. *
  203. * Returns a pointer to a copy of @nbytes bytes of memory at @p
  204. * that are preserved until processing of the NFSv4 compound
  205. * operation described by @argp finishes.
  206. */
  207. static char *savemem(struct nfsd4_compoundargs *argp, __be32 *p, int nbytes)
  208. {
  209. void *ret;
  210. ret = svcxdr_tmpalloc(argp, nbytes);
  211. if (!ret)
  212. return NULL;
  213. memcpy(ret, p, nbytes);
  214. return ret;
  215. }
  216. /*
  217. * We require the high 32 bits of 'seconds' to be 0, and
  218. * we ignore all 32 bits of 'nseconds'.
  219. */
  220. static __be32
  221. nfsd4_decode_time(struct nfsd4_compoundargs *argp, struct timespec *tv)
  222. {
  223. DECODE_HEAD;
  224. u64 sec;
  225. READ_BUF(12);
  226. p = xdr_decode_hyper(p, &sec);
  227. tv->tv_sec = sec;
  228. tv->tv_nsec = be32_to_cpup(p++);
  229. if (tv->tv_nsec >= (u32)1000000000)
  230. return nfserr_inval;
  231. DECODE_TAIL;
  232. }
  233. static __be32
  234. nfsd4_decode_bitmap(struct nfsd4_compoundargs *argp, u32 *bmval)
  235. {
  236. u32 bmlen;
  237. DECODE_HEAD;
  238. bmval[0] = 0;
  239. bmval[1] = 0;
  240. bmval[2] = 0;
  241. READ_BUF(4);
  242. bmlen = be32_to_cpup(p++);
  243. if (bmlen > 1000)
  244. goto xdr_error;
  245. READ_BUF(bmlen << 2);
  246. if (bmlen > 0)
  247. bmval[0] = be32_to_cpup(p++);
  248. if (bmlen > 1)
  249. bmval[1] = be32_to_cpup(p++);
  250. if (bmlen > 2)
  251. bmval[2] = be32_to_cpup(p++);
  252. DECODE_TAIL;
  253. }
  254. static __be32
  255. nfsd4_decode_fattr(struct nfsd4_compoundargs *argp, u32 *bmval,
  256. struct iattr *iattr, struct nfs4_acl **acl,
  257. struct xdr_netobj *label)
  258. {
  259. int expected_len, len = 0;
  260. u32 dummy32;
  261. char *buf;
  262. DECODE_HEAD;
  263. iattr->ia_valid = 0;
  264. if ((status = nfsd4_decode_bitmap(argp, bmval)))
  265. return status;
  266. READ_BUF(4);
  267. expected_len = be32_to_cpup(p++);
  268. if (bmval[0] & FATTR4_WORD0_SIZE) {
  269. READ_BUF(8);
  270. len += 8;
  271. p = xdr_decode_hyper(p, &iattr->ia_size);
  272. iattr->ia_valid |= ATTR_SIZE;
  273. }
  274. if (bmval[0] & FATTR4_WORD0_ACL) {
  275. u32 nace;
  276. struct nfs4_ace *ace;
  277. READ_BUF(4); len += 4;
  278. nace = be32_to_cpup(p++);
  279. if (nace > NFS4_ACL_MAX)
  280. return nfserr_fbig;
  281. *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(nace));
  282. if (*acl == NULL)
  283. return nfserr_jukebox;
  284. (*acl)->naces = nace;
  285. for (ace = (*acl)->aces; ace < (*acl)->aces + nace; ace++) {
  286. READ_BUF(16); len += 16;
  287. ace->type = be32_to_cpup(p++);
  288. ace->flag = be32_to_cpup(p++);
  289. ace->access_mask = be32_to_cpup(p++);
  290. dummy32 = be32_to_cpup(p++);
  291. READ_BUF(dummy32);
  292. len += XDR_QUADLEN(dummy32) << 2;
  293. READMEM(buf, dummy32);
  294. ace->whotype = nfs4_acl_get_whotype(buf, dummy32);
  295. status = nfs_ok;
  296. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  297. ;
  298. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  299. status = nfsd_map_name_to_gid(argp->rqstp,
  300. buf, dummy32, &ace->who_gid);
  301. else
  302. status = nfsd_map_name_to_uid(argp->rqstp,
  303. buf, dummy32, &ace->who_uid);
  304. if (status)
  305. return status;
  306. }
  307. } else
  308. *acl = NULL;
  309. if (bmval[1] & FATTR4_WORD1_MODE) {
  310. READ_BUF(4);
  311. len += 4;
  312. iattr->ia_mode = be32_to_cpup(p++);
  313. iattr->ia_mode &= (S_IFMT | S_IALLUGO);
  314. iattr->ia_valid |= ATTR_MODE;
  315. }
  316. if (bmval[1] & FATTR4_WORD1_OWNER) {
  317. READ_BUF(4);
  318. len += 4;
  319. dummy32 = be32_to_cpup(p++);
  320. READ_BUF(dummy32);
  321. len += (XDR_QUADLEN(dummy32) << 2);
  322. READMEM(buf, dummy32);
  323. if ((status = nfsd_map_name_to_uid(argp->rqstp, buf, dummy32, &iattr->ia_uid)))
  324. return status;
  325. iattr->ia_valid |= ATTR_UID;
  326. }
  327. if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
  328. READ_BUF(4);
  329. len += 4;
  330. dummy32 = be32_to_cpup(p++);
  331. READ_BUF(dummy32);
  332. len += (XDR_QUADLEN(dummy32) << 2);
  333. READMEM(buf, dummy32);
  334. if ((status = nfsd_map_name_to_gid(argp->rqstp, buf, dummy32, &iattr->ia_gid)))
  335. return status;
  336. iattr->ia_valid |= ATTR_GID;
  337. }
  338. if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
  339. READ_BUF(4);
  340. len += 4;
  341. dummy32 = be32_to_cpup(p++);
  342. switch (dummy32) {
  343. case NFS4_SET_TO_CLIENT_TIME:
  344. len += 12;
  345. status = nfsd4_decode_time(argp, &iattr->ia_atime);
  346. if (status)
  347. return status;
  348. iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
  349. break;
  350. case NFS4_SET_TO_SERVER_TIME:
  351. iattr->ia_valid |= ATTR_ATIME;
  352. break;
  353. default:
  354. goto xdr_error;
  355. }
  356. }
  357. if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
  358. READ_BUF(4);
  359. len += 4;
  360. dummy32 = be32_to_cpup(p++);
  361. switch (dummy32) {
  362. case NFS4_SET_TO_CLIENT_TIME:
  363. len += 12;
  364. status = nfsd4_decode_time(argp, &iattr->ia_mtime);
  365. if (status)
  366. return status;
  367. iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
  368. break;
  369. case NFS4_SET_TO_SERVER_TIME:
  370. iattr->ia_valid |= ATTR_MTIME;
  371. break;
  372. default:
  373. goto xdr_error;
  374. }
  375. }
  376. label->len = 0;
  377. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  378. if (bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
  379. READ_BUF(4);
  380. len += 4;
  381. dummy32 = be32_to_cpup(p++); /* lfs: we don't use it */
  382. READ_BUF(4);
  383. len += 4;
  384. dummy32 = be32_to_cpup(p++); /* pi: we don't use it either */
  385. READ_BUF(4);
  386. len += 4;
  387. dummy32 = be32_to_cpup(p++);
  388. READ_BUF(dummy32);
  389. if (dummy32 > NFS4_MAXLABELLEN)
  390. return nfserr_badlabel;
  391. len += (XDR_QUADLEN(dummy32) << 2);
  392. READMEM(buf, dummy32);
  393. label->len = dummy32;
  394. label->data = svcxdr_dupstr(argp, buf, dummy32);
  395. if (!label->data)
  396. return nfserr_jukebox;
  397. }
  398. #endif
  399. if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
  400. || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
  401. || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2)
  402. READ_BUF(expected_len - len);
  403. else if (len != expected_len)
  404. goto xdr_error;
  405. DECODE_TAIL;
  406. }
  407. static __be32
  408. nfsd4_decode_stateid(struct nfsd4_compoundargs *argp, stateid_t *sid)
  409. {
  410. DECODE_HEAD;
  411. READ_BUF(sizeof(stateid_t));
  412. sid->si_generation = be32_to_cpup(p++);
  413. COPYMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
  414. DECODE_TAIL;
  415. }
  416. static __be32
  417. nfsd4_decode_access(struct nfsd4_compoundargs *argp, struct nfsd4_access *access)
  418. {
  419. DECODE_HEAD;
  420. READ_BUF(4);
  421. access->ac_req_access = be32_to_cpup(p++);
  422. DECODE_TAIL;
  423. }
  424. static __be32 nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
  425. {
  426. DECODE_HEAD;
  427. u32 dummy, uid, gid;
  428. char *machine_name;
  429. int i;
  430. int nr_secflavs;
  431. /* callback_sec_params4 */
  432. READ_BUF(4);
  433. nr_secflavs = be32_to_cpup(p++);
  434. if (nr_secflavs)
  435. cbs->flavor = (u32)(-1);
  436. else
  437. /* Is this legal? Be generous, take it to mean AUTH_NONE: */
  438. cbs->flavor = 0;
  439. for (i = 0; i < nr_secflavs; ++i) {
  440. READ_BUF(4);
  441. dummy = be32_to_cpup(p++);
  442. switch (dummy) {
  443. case RPC_AUTH_NULL:
  444. /* Nothing to read */
  445. if (cbs->flavor == (u32)(-1))
  446. cbs->flavor = RPC_AUTH_NULL;
  447. break;
  448. case RPC_AUTH_UNIX:
  449. READ_BUF(8);
  450. /* stamp */
  451. dummy = be32_to_cpup(p++);
  452. /* machine name */
  453. dummy = be32_to_cpup(p++);
  454. READ_BUF(dummy);
  455. SAVEMEM(machine_name, dummy);
  456. /* uid, gid */
  457. READ_BUF(8);
  458. uid = be32_to_cpup(p++);
  459. gid = be32_to_cpup(p++);
  460. /* more gids */
  461. READ_BUF(4);
  462. dummy = be32_to_cpup(p++);
  463. READ_BUF(dummy * 4);
  464. if (cbs->flavor == (u32)(-1)) {
  465. kuid_t kuid = make_kuid(&init_user_ns, uid);
  466. kgid_t kgid = make_kgid(&init_user_ns, gid);
  467. if (uid_valid(kuid) && gid_valid(kgid)) {
  468. cbs->uid = kuid;
  469. cbs->gid = kgid;
  470. cbs->flavor = RPC_AUTH_UNIX;
  471. } else {
  472. dprintk("RPC_AUTH_UNIX with invalid"
  473. "uid or gid ignoring!\n");
  474. }
  475. }
  476. break;
  477. case RPC_AUTH_GSS:
  478. dprintk("RPC_AUTH_GSS callback secflavor "
  479. "not supported!\n");
  480. READ_BUF(8);
  481. /* gcbp_service */
  482. dummy = be32_to_cpup(p++);
  483. /* gcbp_handle_from_server */
  484. dummy = be32_to_cpup(p++);
  485. READ_BUF(dummy);
  486. p += XDR_QUADLEN(dummy);
  487. /* gcbp_handle_from_client */
  488. READ_BUF(4);
  489. dummy = be32_to_cpup(p++);
  490. READ_BUF(dummy);
  491. break;
  492. default:
  493. dprintk("Illegal callback secflavor\n");
  494. return nfserr_inval;
  495. }
  496. }
  497. DECODE_TAIL;
  498. }
  499. static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
  500. {
  501. DECODE_HEAD;
  502. READ_BUF(4);
  503. bc->bc_cb_program = be32_to_cpup(p++);
  504. nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
  505. DECODE_TAIL;
  506. }
  507. static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
  508. {
  509. DECODE_HEAD;
  510. READ_BUF(NFS4_MAX_SESSIONID_LEN + 8);
  511. COPYMEM(bcts->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  512. bcts->dir = be32_to_cpup(p++);
  513. /* XXX: skipping ctsa_use_conn_in_rdma_mode. Perhaps Tom Tucker
  514. * could help us figure out we should be using it. */
  515. DECODE_TAIL;
  516. }
  517. static __be32
  518. nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
  519. {
  520. DECODE_HEAD;
  521. READ_BUF(4);
  522. close->cl_seqid = be32_to_cpup(p++);
  523. return nfsd4_decode_stateid(argp, &close->cl_stateid);
  524. DECODE_TAIL;
  525. }
  526. static __be32
  527. nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
  528. {
  529. DECODE_HEAD;
  530. READ_BUF(12);
  531. p = xdr_decode_hyper(p, &commit->co_offset);
  532. commit->co_count = be32_to_cpup(p++);
  533. DECODE_TAIL;
  534. }
  535. static __be32
  536. nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
  537. {
  538. DECODE_HEAD;
  539. READ_BUF(4);
  540. create->cr_type = be32_to_cpup(p++);
  541. switch (create->cr_type) {
  542. case NF4LNK:
  543. READ_BUF(4);
  544. create->cr_datalen = be32_to_cpup(p++);
  545. READ_BUF(create->cr_datalen);
  546. create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
  547. if (!create->cr_data)
  548. return nfserr_jukebox;
  549. break;
  550. case NF4BLK:
  551. case NF4CHR:
  552. READ_BUF(8);
  553. create->cr_specdata1 = be32_to_cpup(p++);
  554. create->cr_specdata2 = be32_to_cpup(p++);
  555. break;
  556. case NF4SOCK:
  557. case NF4FIFO:
  558. case NF4DIR:
  559. default:
  560. break;
  561. }
  562. READ_BUF(4);
  563. create->cr_namelen = be32_to_cpup(p++);
  564. READ_BUF(create->cr_namelen);
  565. SAVEMEM(create->cr_name, create->cr_namelen);
  566. if ((status = check_filename(create->cr_name, create->cr_namelen)))
  567. return status;
  568. status = nfsd4_decode_fattr(argp, create->cr_bmval, &create->cr_iattr,
  569. &create->cr_acl, &create->cr_label);
  570. if (status)
  571. goto out;
  572. DECODE_TAIL;
  573. }
  574. static inline __be32
  575. nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
  576. {
  577. return nfsd4_decode_stateid(argp, &dr->dr_stateid);
  578. }
  579. static inline __be32
  580. nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
  581. {
  582. return nfsd4_decode_bitmap(argp, getattr->ga_bmval);
  583. }
  584. static __be32
  585. nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
  586. {
  587. DECODE_HEAD;
  588. READ_BUF(4);
  589. link->li_namelen = be32_to_cpup(p++);
  590. READ_BUF(link->li_namelen);
  591. SAVEMEM(link->li_name, link->li_namelen);
  592. if ((status = check_filename(link->li_name, link->li_namelen)))
  593. return status;
  594. DECODE_TAIL;
  595. }
  596. static __be32
  597. nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
  598. {
  599. DECODE_HEAD;
  600. /*
  601. * type, reclaim(boolean), offset, length, new_lock_owner(boolean)
  602. */
  603. READ_BUF(28);
  604. lock->lk_type = be32_to_cpup(p++);
  605. if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
  606. goto xdr_error;
  607. lock->lk_reclaim = be32_to_cpup(p++);
  608. p = xdr_decode_hyper(p, &lock->lk_offset);
  609. p = xdr_decode_hyper(p, &lock->lk_length);
  610. lock->lk_is_new = be32_to_cpup(p++);
  611. if (lock->lk_is_new) {
  612. READ_BUF(4);
  613. lock->lk_new_open_seqid = be32_to_cpup(p++);
  614. status = nfsd4_decode_stateid(argp, &lock->lk_new_open_stateid);
  615. if (status)
  616. return status;
  617. READ_BUF(8 + sizeof(clientid_t));
  618. lock->lk_new_lock_seqid = be32_to_cpup(p++);
  619. COPYMEM(&lock->lk_new_clientid, sizeof(clientid_t));
  620. lock->lk_new_owner.len = be32_to_cpup(p++);
  621. READ_BUF(lock->lk_new_owner.len);
  622. READMEM(lock->lk_new_owner.data, lock->lk_new_owner.len);
  623. } else {
  624. status = nfsd4_decode_stateid(argp, &lock->lk_old_lock_stateid);
  625. if (status)
  626. return status;
  627. READ_BUF(4);
  628. lock->lk_old_lock_seqid = be32_to_cpup(p++);
  629. }
  630. DECODE_TAIL;
  631. }
  632. static __be32
  633. nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
  634. {
  635. DECODE_HEAD;
  636. READ_BUF(32);
  637. lockt->lt_type = be32_to_cpup(p++);
  638. if((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
  639. goto xdr_error;
  640. p = xdr_decode_hyper(p, &lockt->lt_offset);
  641. p = xdr_decode_hyper(p, &lockt->lt_length);
  642. COPYMEM(&lockt->lt_clientid, 8);
  643. lockt->lt_owner.len = be32_to_cpup(p++);
  644. READ_BUF(lockt->lt_owner.len);
  645. READMEM(lockt->lt_owner.data, lockt->lt_owner.len);
  646. DECODE_TAIL;
  647. }
  648. static __be32
  649. nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
  650. {
  651. DECODE_HEAD;
  652. READ_BUF(8);
  653. locku->lu_type = be32_to_cpup(p++);
  654. if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
  655. goto xdr_error;
  656. locku->lu_seqid = be32_to_cpup(p++);
  657. status = nfsd4_decode_stateid(argp, &locku->lu_stateid);
  658. if (status)
  659. return status;
  660. READ_BUF(16);
  661. p = xdr_decode_hyper(p, &locku->lu_offset);
  662. p = xdr_decode_hyper(p, &locku->lu_length);
  663. DECODE_TAIL;
  664. }
  665. static __be32
  666. nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
  667. {
  668. DECODE_HEAD;
  669. READ_BUF(4);
  670. lookup->lo_len = be32_to_cpup(p++);
  671. READ_BUF(lookup->lo_len);
  672. SAVEMEM(lookup->lo_name, lookup->lo_len);
  673. if ((status = check_filename(lookup->lo_name, lookup->lo_len)))
  674. return status;
  675. DECODE_TAIL;
  676. }
  677. static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
  678. {
  679. __be32 *p;
  680. u32 w;
  681. READ_BUF(4);
  682. w = be32_to_cpup(p++);
  683. *share_access = w & NFS4_SHARE_ACCESS_MASK;
  684. *deleg_want = w & NFS4_SHARE_WANT_MASK;
  685. if (deleg_when)
  686. *deleg_when = w & NFS4_SHARE_WHEN_MASK;
  687. switch (w & NFS4_SHARE_ACCESS_MASK) {
  688. case NFS4_SHARE_ACCESS_READ:
  689. case NFS4_SHARE_ACCESS_WRITE:
  690. case NFS4_SHARE_ACCESS_BOTH:
  691. break;
  692. default:
  693. return nfserr_bad_xdr;
  694. }
  695. w &= ~NFS4_SHARE_ACCESS_MASK;
  696. if (!w)
  697. return nfs_ok;
  698. if (!argp->minorversion)
  699. return nfserr_bad_xdr;
  700. switch (w & NFS4_SHARE_WANT_MASK) {
  701. case NFS4_SHARE_WANT_NO_PREFERENCE:
  702. case NFS4_SHARE_WANT_READ_DELEG:
  703. case NFS4_SHARE_WANT_WRITE_DELEG:
  704. case NFS4_SHARE_WANT_ANY_DELEG:
  705. case NFS4_SHARE_WANT_NO_DELEG:
  706. case NFS4_SHARE_WANT_CANCEL:
  707. break;
  708. default:
  709. return nfserr_bad_xdr;
  710. }
  711. w &= ~NFS4_SHARE_WANT_MASK;
  712. if (!w)
  713. return nfs_ok;
  714. if (!deleg_when) /* open_downgrade */
  715. return nfserr_inval;
  716. switch (w) {
  717. case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
  718. case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
  719. case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
  720. NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
  721. return nfs_ok;
  722. }
  723. xdr_error:
  724. return nfserr_bad_xdr;
  725. }
  726. static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
  727. {
  728. __be32 *p;
  729. READ_BUF(4);
  730. *x = be32_to_cpup(p++);
  731. /* Note: unlinke access bits, deny bits may be zero. */
  732. if (*x & ~NFS4_SHARE_DENY_BOTH)
  733. return nfserr_bad_xdr;
  734. return nfs_ok;
  735. xdr_error:
  736. return nfserr_bad_xdr;
  737. }
  738. static __be32 nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
  739. {
  740. __be32 *p;
  741. READ_BUF(4);
  742. o->len = be32_to_cpup(p++);
  743. if (o->len == 0 || o->len > NFS4_OPAQUE_LIMIT)
  744. return nfserr_bad_xdr;
  745. READ_BUF(o->len);
  746. SAVEMEM(o->data, o->len);
  747. return nfs_ok;
  748. xdr_error:
  749. return nfserr_bad_xdr;
  750. }
  751. static __be32
  752. nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
  753. {
  754. DECODE_HEAD;
  755. u32 dummy;
  756. memset(open->op_bmval, 0, sizeof(open->op_bmval));
  757. open->op_iattr.ia_valid = 0;
  758. open->op_openowner = NULL;
  759. open->op_xdr_error = 0;
  760. /* seqid, share_access, share_deny, clientid, ownerlen */
  761. READ_BUF(4);
  762. open->op_seqid = be32_to_cpup(p++);
  763. /* decode, yet ignore deleg_when until supported */
  764. status = nfsd4_decode_share_access(argp, &open->op_share_access,
  765. &open->op_deleg_want, &dummy);
  766. if (status)
  767. goto xdr_error;
  768. status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
  769. if (status)
  770. goto xdr_error;
  771. READ_BUF(sizeof(clientid_t));
  772. COPYMEM(&open->op_clientid, sizeof(clientid_t));
  773. status = nfsd4_decode_opaque(argp, &open->op_owner);
  774. if (status)
  775. goto xdr_error;
  776. READ_BUF(4);
  777. open->op_create = be32_to_cpup(p++);
  778. switch (open->op_create) {
  779. case NFS4_OPEN_NOCREATE:
  780. break;
  781. case NFS4_OPEN_CREATE:
  782. READ_BUF(4);
  783. open->op_createmode = be32_to_cpup(p++);
  784. switch (open->op_createmode) {
  785. case NFS4_CREATE_UNCHECKED:
  786. case NFS4_CREATE_GUARDED:
  787. status = nfsd4_decode_fattr(argp, open->op_bmval,
  788. &open->op_iattr, &open->op_acl, &open->op_label);
  789. if (status)
  790. goto out;
  791. break;
  792. case NFS4_CREATE_EXCLUSIVE:
  793. READ_BUF(NFS4_VERIFIER_SIZE);
  794. COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
  795. break;
  796. case NFS4_CREATE_EXCLUSIVE4_1:
  797. if (argp->minorversion < 1)
  798. goto xdr_error;
  799. READ_BUF(NFS4_VERIFIER_SIZE);
  800. COPYMEM(open->op_verf.data, NFS4_VERIFIER_SIZE);
  801. status = nfsd4_decode_fattr(argp, open->op_bmval,
  802. &open->op_iattr, &open->op_acl, &open->op_label);
  803. if (status)
  804. goto out;
  805. break;
  806. default:
  807. goto xdr_error;
  808. }
  809. break;
  810. default:
  811. goto xdr_error;
  812. }
  813. /* open_claim */
  814. READ_BUF(4);
  815. open->op_claim_type = be32_to_cpup(p++);
  816. switch (open->op_claim_type) {
  817. case NFS4_OPEN_CLAIM_NULL:
  818. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  819. READ_BUF(4);
  820. open->op_fname.len = be32_to_cpup(p++);
  821. READ_BUF(open->op_fname.len);
  822. SAVEMEM(open->op_fname.data, open->op_fname.len);
  823. if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
  824. return status;
  825. break;
  826. case NFS4_OPEN_CLAIM_PREVIOUS:
  827. READ_BUF(4);
  828. open->op_delegate_type = be32_to_cpup(p++);
  829. break;
  830. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  831. status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
  832. if (status)
  833. return status;
  834. READ_BUF(4);
  835. open->op_fname.len = be32_to_cpup(p++);
  836. READ_BUF(open->op_fname.len);
  837. SAVEMEM(open->op_fname.data, open->op_fname.len);
  838. if ((status = check_filename(open->op_fname.data, open->op_fname.len)))
  839. return status;
  840. break;
  841. case NFS4_OPEN_CLAIM_FH:
  842. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  843. if (argp->minorversion < 1)
  844. goto xdr_error;
  845. /* void */
  846. break;
  847. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  848. if (argp->minorversion < 1)
  849. goto xdr_error;
  850. status = nfsd4_decode_stateid(argp, &open->op_delegate_stateid);
  851. if (status)
  852. return status;
  853. break;
  854. default:
  855. goto xdr_error;
  856. }
  857. DECODE_TAIL;
  858. }
  859. static __be32
  860. nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
  861. {
  862. DECODE_HEAD;
  863. if (argp->minorversion >= 1)
  864. return nfserr_notsupp;
  865. status = nfsd4_decode_stateid(argp, &open_conf->oc_req_stateid);
  866. if (status)
  867. return status;
  868. READ_BUF(4);
  869. open_conf->oc_seqid = be32_to_cpup(p++);
  870. DECODE_TAIL;
  871. }
  872. static __be32
  873. nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
  874. {
  875. DECODE_HEAD;
  876. status = nfsd4_decode_stateid(argp, &open_down->od_stateid);
  877. if (status)
  878. return status;
  879. READ_BUF(4);
  880. open_down->od_seqid = be32_to_cpup(p++);
  881. status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
  882. &open_down->od_deleg_want, NULL);
  883. if (status)
  884. return status;
  885. status = nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
  886. if (status)
  887. return status;
  888. DECODE_TAIL;
  889. }
  890. static __be32
  891. nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
  892. {
  893. DECODE_HEAD;
  894. READ_BUF(4);
  895. putfh->pf_fhlen = be32_to_cpup(p++);
  896. if (putfh->pf_fhlen > NFS4_FHSIZE)
  897. goto xdr_error;
  898. READ_BUF(putfh->pf_fhlen);
  899. SAVEMEM(putfh->pf_fhval, putfh->pf_fhlen);
  900. DECODE_TAIL;
  901. }
  902. static __be32
  903. nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
  904. {
  905. if (argp->minorversion == 0)
  906. return nfs_ok;
  907. return nfserr_notsupp;
  908. }
  909. static __be32
  910. nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
  911. {
  912. DECODE_HEAD;
  913. status = nfsd4_decode_stateid(argp, &read->rd_stateid);
  914. if (status)
  915. return status;
  916. READ_BUF(12);
  917. p = xdr_decode_hyper(p, &read->rd_offset);
  918. read->rd_length = be32_to_cpup(p++);
  919. DECODE_TAIL;
  920. }
  921. static __be32
  922. nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
  923. {
  924. DECODE_HEAD;
  925. READ_BUF(24);
  926. p = xdr_decode_hyper(p, &readdir->rd_cookie);
  927. COPYMEM(readdir->rd_verf.data, sizeof(readdir->rd_verf.data));
  928. readdir->rd_dircount = be32_to_cpup(p++);
  929. readdir->rd_maxcount = be32_to_cpup(p++);
  930. if ((status = nfsd4_decode_bitmap(argp, readdir->rd_bmval)))
  931. goto out;
  932. DECODE_TAIL;
  933. }
  934. static __be32
  935. nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
  936. {
  937. DECODE_HEAD;
  938. READ_BUF(4);
  939. remove->rm_namelen = be32_to_cpup(p++);
  940. READ_BUF(remove->rm_namelen);
  941. SAVEMEM(remove->rm_name, remove->rm_namelen);
  942. if ((status = check_filename(remove->rm_name, remove->rm_namelen)))
  943. return status;
  944. DECODE_TAIL;
  945. }
  946. static __be32
  947. nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
  948. {
  949. DECODE_HEAD;
  950. READ_BUF(4);
  951. rename->rn_snamelen = be32_to_cpup(p++);
  952. READ_BUF(rename->rn_snamelen + 4);
  953. SAVEMEM(rename->rn_sname, rename->rn_snamelen);
  954. rename->rn_tnamelen = be32_to_cpup(p++);
  955. READ_BUF(rename->rn_tnamelen);
  956. SAVEMEM(rename->rn_tname, rename->rn_tnamelen);
  957. if ((status = check_filename(rename->rn_sname, rename->rn_snamelen)))
  958. return status;
  959. if ((status = check_filename(rename->rn_tname, rename->rn_tnamelen)))
  960. return status;
  961. DECODE_TAIL;
  962. }
  963. static __be32
  964. nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
  965. {
  966. DECODE_HEAD;
  967. if (argp->minorversion >= 1)
  968. return nfserr_notsupp;
  969. READ_BUF(sizeof(clientid_t));
  970. COPYMEM(clientid, sizeof(clientid_t));
  971. DECODE_TAIL;
  972. }
  973. static __be32
  974. nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
  975. struct nfsd4_secinfo *secinfo)
  976. {
  977. DECODE_HEAD;
  978. READ_BUF(4);
  979. secinfo->si_namelen = be32_to_cpup(p++);
  980. READ_BUF(secinfo->si_namelen);
  981. SAVEMEM(secinfo->si_name, secinfo->si_namelen);
  982. status = check_filename(secinfo->si_name, secinfo->si_namelen);
  983. if (status)
  984. return status;
  985. DECODE_TAIL;
  986. }
  987. static __be32
  988. nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
  989. struct nfsd4_secinfo_no_name *sin)
  990. {
  991. DECODE_HEAD;
  992. READ_BUF(4);
  993. sin->sin_style = be32_to_cpup(p++);
  994. DECODE_TAIL;
  995. }
  996. static __be32
  997. nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
  998. {
  999. __be32 status;
  1000. status = nfsd4_decode_stateid(argp, &setattr->sa_stateid);
  1001. if (status)
  1002. return status;
  1003. return nfsd4_decode_fattr(argp, setattr->sa_bmval, &setattr->sa_iattr,
  1004. &setattr->sa_acl, &setattr->sa_label);
  1005. }
  1006. static __be32
  1007. nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
  1008. {
  1009. DECODE_HEAD;
  1010. if (argp->minorversion >= 1)
  1011. return nfserr_notsupp;
  1012. READ_BUF(NFS4_VERIFIER_SIZE);
  1013. COPYMEM(setclientid->se_verf.data, NFS4_VERIFIER_SIZE);
  1014. status = nfsd4_decode_opaque(argp, &setclientid->se_name);
  1015. if (status)
  1016. return nfserr_bad_xdr;
  1017. READ_BUF(8);
  1018. setclientid->se_callback_prog = be32_to_cpup(p++);
  1019. setclientid->se_callback_netid_len = be32_to_cpup(p++);
  1020. READ_BUF(setclientid->se_callback_netid_len + 4);
  1021. SAVEMEM(setclientid->se_callback_netid_val, setclientid->se_callback_netid_len);
  1022. setclientid->se_callback_addr_len = be32_to_cpup(p++);
  1023. READ_BUF(setclientid->se_callback_addr_len + 4);
  1024. SAVEMEM(setclientid->se_callback_addr_val, setclientid->se_callback_addr_len);
  1025. setclientid->se_callback_ident = be32_to_cpup(p++);
  1026. DECODE_TAIL;
  1027. }
  1028. static __be32
  1029. nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
  1030. {
  1031. DECODE_HEAD;
  1032. if (argp->minorversion >= 1)
  1033. return nfserr_notsupp;
  1034. READ_BUF(8 + NFS4_VERIFIER_SIZE);
  1035. COPYMEM(&scd_c->sc_clientid, 8);
  1036. COPYMEM(&scd_c->sc_confirm, NFS4_VERIFIER_SIZE);
  1037. DECODE_TAIL;
  1038. }
  1039. /* Also used for NVERIFY */
  1040. static __be32
  1041. nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
  1042. {
  1043. DECODE_HEAD;
  1044. if ((status = nfsd4_decode_bitmap(argp, verify->ve_bmval)))
  1045. goto out;
  1046. /* For convenience's sake, we compare raw xdr'd attributes in
  1047. * nfsd4_proc_verify */
  1048. READ_BUF(4);
  1049. verify->ve_attrlen = be32_to_cpup(p++);
  1050. READ_BUF(verify->ve_attrlen);
  1051. SAVEMEM(verify->ve_attrval, verify->ve_attrlen);
  1052. DECODE_TAIL;
  1053. }
  1054. static __be32
  1055. nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
  1056. {
  1057. int avail;
  1058. int len;
  1059. DECODE_HEAD;
  1060. status = nfsd4_decode_stateid(argp, &write->wr_stateid);
  1061. if (status)
  1062. return status;
  1063. READ_BUF(16);
  1064. p = xdr_decode_hyper(p, &write->wr_offset);
  1065. write->wr_stable_how = be32_to_cpup(p++);
  1066. if (write->wr_stable_how > 2)
  1067. goto xdr_error;
  1068. write->wr_buflen = be32_to_cpup(p++);
  1069. /* Sorry .. no magic macros for this.. *
  1070. * READ_BUF(write->wr_buflen);
  1071. * SAVEMEM(write->wr_buf, write->wr_buflen);
  1072. */
  1073. avail = (char*)argp->end - (char*)argp->p;
  1074. if (avail + argp->pagelen < write->wr_buflen) {
  1075. dprintk("NFSD: xdr error (%s:%d)\n",
  1076. __FILE__, __LINE__);
  1077. goto xdr_error;
  1078. }
  1079. write->wr_head.iov_base = p;
  1080. write->wr_head.iov_len = avail;
  1081. write->wr_pagelist = argp->pagelist;
  1082. len = XDR_QUADLEN(write->wr_buflen) << 2;
  1083. if (len >= avail) {
  1084. int pages;
  1085. len -= avail;
  1086. pages = len >> PAGE_SHIFT;
  1087. argp->pagelist += pages;
  1088. argp->pagelen -= pages * PAGE_SIZE;
  1089. len -= pages * PAGE_SIZE;
  1090. argp->p = (__be32 *)page_address(argp->pagelist[0]);
  1091. argp->pagelist++;
  1092. argp->end = argp->p + XDR_QUADLEN(PAGE_SIZE);
  1093. }
  1094. argp->p += XDR_QUADLEN(len);
  1095. DECODE_TAIL;
  1096. }
  1097. static __be32
  1098. nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
  1099. {
  1100. DECODE_HEAD;
  1101. if (argp->minorversion >= 1)
  1102. return nfserr_notsupp;
  1103. READ_BUF(12);
  1104. COPYMEM(&rlockowner->rl_clientid, sizeof(clientid_t));
  1105. rlockowner->rl_owner.len = be32_to_cpup(p++);
  1106. READ_BUF(rlockowner->rl_owner.len);
  1107. READMEM(rlockowner->rl_owner.data, rlockowner->rl_owner.len);
  1108. if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
  1109. return nfserr_inval;
  1110. DECODE_TAIL;
  1111. }
  1112. static __be32
  1113. nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
  1114. struct nfsd4_exchange_id *exid)
  1115. {
  1116. int dummy, tmp;
  1117. DECODE_HEAD;
  1118. READ_BUF(NFS4_VERIFIER_SIZE);
  1119. COPYMEM(exid->verifier.data, NFS4_VERIFIER_SIZE);
  1120. status = nfsd4_decode_opaque(argp, &exid->clname);
  1121. if (status)
  1122. return nfserr_bad_xdr;
  1123. READ_BUF(4);
  1124. exid->flags = be32_to_cpup(p++);
  1125. /* Ignore state_protect4_a */
  1126. READ_BUF(4);
  1127. exid->spa_how = be32_to_cpup(p++);
  1128. switch (exid->spa_how) {
  1129. case SP4_NONE:
  1130. break;
  1131. case SP4_MACH_CRED:
  1132. /* spo_must_enforce */
  1133. READ_BUF(4);
  1134. dummy = be32_to_cpup(p++);
  1135. READ_BUF(dummy * 4);
  1136. p += dummy;
  1137. /* spo_must_allow */
  1138. READ_BUF(4);
  1139. dummy = be32_to_cpup(p++);
  1140. READ_BUF(dummy * 4);
  1141. p += dummy;
  1142. break;
  1143. case SP4_SSV:
  1144. /* ssp_ops */
  1145. READ_BUF(4);
  1146. dummy = be32_to_cpup(p++);
  1147. READ_BUF(dummy * 4);
  1148. p += dummy;
  1149. READ_BUF(4);
  1150. dummy = be32_to_cpup(p++);
  1151. READ_BUF(dummy * 4);
  1152. p += dummy;
  1153. /* ssp_hash_algs<> */
  1154. READ_BUF(4);
  1155. tmp = be32_to_cpup(p++);
  1156. while (tmp--) {
  1157. READ_BUF(4);
  1158. dummy = be32_to_cpup(p++);
  1159. READ_BUF(dummy);
  1160. p += XDR_QUADLEN(dummy);
  1161. }
  1162. /* ssp_encr_algs<> */
  1163. READ_BUF(4);
  1164. tmp = be32_to_cpup(p++);
  1165. while (tmp--) {
  1166. READ_BUF(4);
  1167. dummy = be32_to_cpup(p++);
  1168. READ_BUF(dummy);
  1169. p += XDR_QUADLEN(dummy);
  1170. }
  1171. /* ssp_window and ssp_num_gss_handles */
  1172. READ_BUF(8);
  1173. dummy = be32_to_cpup(p++);
  1174. dummy = be32_to_cpup(p++);
  1175. break;
  1176. default:
  1177. goto xdr_error;
  1178. }
  1179. /* Ignore Implementation ID */
  1180. READ_BUF(4); /* nfs_impl_id4 array length */
  1181. dummy = be32_to_cpup(p++);
  1182. if (dummy > 1)
  1183. goto xdr_error;
  1184. if (dummy == 1) {
  1185. /* nii_domain */
  1186. READ_BUF(4);
  1187. dummy = be32_to_cpup(p++);
  1188. READ_BUF(dummy);
  1189. p += XDR_QUADLEN(dummy);
  1190. /* nii_name */
  1191. READ_BUF(4);
  1192. dummy = be32_to_cpup(p++);
  1193. READ_BUF(dummy);
  1194. p += XDR_QUADLEN(dummy);
  1195. /* nii_date */
  1196. READ_BUF(12);
  1197. p += 3;
  1198. }
  1199. DECODE_TAIL;
  1200. }
  1201. static __be32
  1202. nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
  1203. struct nfsd4_create_session *sess)
  1204. {
  1205. DECODE_HEAD;
  1206. u32 dummy;
  1207. READ_BUF(16);
  1208. COPYMEM(&sess->clientid, 8);
  1209. sess->seqid = be32_to_cpup(p++);
  1210. sess->flags = be32_to_cpup(p++);
  1211. /* Fore channel attrs */
  1212. READ_BUF(28);
  1213. dummy = be32_to_cpup(p++); /* headerpadsz is always 0 */
  1214. sess->fore_channel.maxreq_sz = be32_to_cpup(p++);
  1215. sess->fore_channel.maxresp_sz = be32_to_cpup(p++);
  1216. sess->fore_channel.maxresp_cached = be32_to_cpup(p++);
  1217. sess->fore_channel.maxops = be32_to_cpup(p++);
  1218. sess->fore_channel.maxreqs = be32_to_cpup(p++);
  1219. sess->fore_channel.nr_rdma_attrs = be32_to_cpup(p++);
  1220. if (sess->fore_channel.nr_rdma_attrs == 1) {
  1221. READ_BUF(4);
  1222. sess->fore_channel.rdma_attrs = be32_to_cpup(p++);
  1223. } else if (sess->fore_channel.nr_rdma_attrs > 1) {
  1224. dprintk("Too many fore channel attr bitmaps!\n");
  1225. goto xdr_error;
  1226. }
  1227. /* Back channel attrs */
  1228. READ_BUF(28);
  1229. dummy = be32_to_cpup(p++); /* headerpadsz is always 0 */
  1230. sess->back_channel.maxreq_sz = be32_to_cpup(p++);
  1231. sess->back_channel.maxresp_sz = be32_to_cpup(p++);
  1232. sess->back_channel.maxresp_cached = be32_to_cpup(p++);
  1233. sess->back_channel.maxops = be32_to_cpup(p++);
  1234. sess->back_channel.maxreqs = be32_to_cpup(p++);
  1235. sess->back_channel.nr_rdma_attrs = be32_to_cpup(p++);
  1236. if (sess->back_channel.nr_rdma_attrs == 1) {
  1237. READ_BUF(4);
  1238. sess->back_channel.rdma_attrs = be32_to_cpup(p++);
  1239. } else if (sess->back_channel.nr_rdma_attrs > 1) {
  1240. dprintk("Too many back channel attr bitmaps!\n");
  1241. goto xdr_error;
  1242. }
  1243. READ_BUF(4);
  1244. sess->callback_prog = be32_to_cpup(p++);
  1245. nfsd4_decode_cb_sec(argp, &sess->cb_sec);
  1246. DECODE_TAIL;
  1247. }
  1248. static __be32
  1249. nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
  1250. struct nfsd4_destroy_session *destroy_session)
  1251. {
  1252. DECODE_HEAD;
  1253. READ_BUF(NFS4_MAX_SESSIONID_LEN);
  1254. COPYMEM(destroy_session->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1255. DECODE_TAIL;
  1256. }
  1257. static __be32
  1258. nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
  1259. struct nfsd4_free_stateid *free_stateid)
  1260. {
  1261. DECODE_HEAD;
  1262. READ_BUF(sizeof(stateid_t));
  1263. free_stateid->fr_stateid.si_generation = be32_to_cpup(p++);
  1264. COPYMEM(&free_stateid->fr_stateid.si_opaque, sizeof(stateid_opaque_t));
  1265. DECODE_TAIL;
  1266. }
  1267. static __be32
  1268. nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
  1269. struct nfsd4_sequence *seq)
  1270. {
  1271. DECODE_HEAD;
  1272. READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
  1273. COPYMEM(seq->sessionid.data, NFS4_MAX_SESSIONID_LEN);
  1274. seq->seqid = be32_to_cpup(p++);
  1275. seq->slotid = be32_to_cpup(p++);
  1276. seq->maxslots = be32_to_cpup(p++);
  1277. seq->cachethis = be32_to_cpup(p++);
  1278. DECODE_TAIL;
  1279. }
  1280. static __be32
  1281. nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
  1282. {
  1283. int i;
  1284. __be32 *p, status;
  1285. struct nfsd4_test_stateid_id *stateid;
  1286. READ_BUF(4);
  1287. test_stateid->ts_num_ids = ntohl(*p++);
  1288. INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
  1289. for (i = 0; i < test_stateid->ts_num_ids; i++) {
  1290. stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
  1291. if (!stateid) {
  1292. status = nfserrno(-ENOMEM);
  1293. goto out;
  1294. }
  1295. INIT_LIST_HEAD(&stateid->ts_id_list);
  1296. list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
  1297. status = nfsd4_decode_stateid(argp, &stateid->ts_id_stateid);
  1298. if (status)
  1299. goto out;
  1300. }
  1301. status = 0;
  1302. out:
  1303. return status;
  1304. xdr_error:
  1305. dprintk("NFSD: xdr error (%s:%d)\n", __FILE__, __LINE__);
  1306. status = nfserr_bad_xdr;
  1307. goto out;
  1308. }
  1309. static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp, struct nfsd4_destroy_clientid *dc)
  1310. {
  1311. DECODE_HEAD;
  1312. READ_BUF(8);
  1313. COPYMEM(&dc->clientid, 8);
  1314. DECODE_TAIL;
  1315. }
  1316. static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp, struct nfsd4_reclaim_complete *rc)
  1317. {
  1318. DECODE_HEAD;
  1319. READ_BUF(4);
  1320. rc->rca_one_fs = be32_to_cpup(p++);
  1321. DECODE_TAIL;
  1322. }
  1323. #ifdef CONFIG_NFSD_PNFS
  1324. static __be32
  1325. nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
  1326. struct nfsd4_getdeviceinfo *gdev)
  1327. {
  1328. DECODE_HEAD;
  1329. u32 num, i;
  1330. READ_BUF(sizeof(struct nfsd4_deviceid) + 3 * 4);
  1331. COPYMEM(&gdev->gd_devid, sizeof(struct nfsd4_deviceid));
  1332. gdev->gd_layout_type = be32_to_cpup(p++);
  1333. gdev->gd_maxcount = be32_to_cpup(p++);
  1334. num = be32_to_cpup(p++);
  1335. if (num) {
  1336. READ_BUF(4 * num);
  1337. gdev->gd_notify_types = be32_to_cpup(p++);
  1338. for (i = 1; i < num; i++) {
  1339. if (be32_to_cpup(p++)) {
  1340. status = nfserr_inval;
  1341. goto out;
  1342. }
  1343. }
  1344. }
  1345. DECODE_TAIL;
  1346. }
  1347. static __be32
  1348. nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
  1349. struct nfsd4_layoutget *lgp)
  1350. {
  1351. DECODE_HEAD;
  1352. READ_BUF(36);
  1353. lgp->lg_signal = be32_to_cpup(p++);
  1354. lgp->lg_layout_type = be32_to_cpup(p++);
  1355. lgp->lg_seg.iomode = be32_to_cpup(p++);
  1356. p = xdr_decode_hyper(p, &lgp->lg_seg.offset);
  1357. p = xdr_decode_hyper(p, &lgp->lg_seg.length);
  1358. p = xdr_decode_hyper(p, &lgp->lg_minlength);
  1359. status = nfsd4_decode_stateid(argp, &lgp->lg_sid);
  1360. if (status)
  1361. return status;
  1362. READ_BUF(4);
  1363. lgp->lg_maxcount = be32_to_cpup(p++);
  1364. DECODE_TAIL;
  1365. }
  1366. static __be32
  1367. nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
  1368. struct nfsd4_layoutcommit *lcp)
  1369. {
  1370. DECODE_HEAD;
  1371. u32 timechange;
  1372. READ_BUF(20);
  1373. p = xdr_decode_hyper(p, &lcp->lc_seg.offset);
  1374. p = xdr_decode_hyper(p, &lcp->lc_seg.length);
  1375. lcp->lc_reclaim = be32_to_cpup(p++);
  1376. status = nfsd4_decode_stateid(argp, &lcp->lc_sid);
  1377. if (status)
  1378. return status;
  1379. READ_BUF(4);
  1380. lcp->lc_newoffset = be32_to_cpup(p++);
  1381. if (lcp->lc_newoffset) {
  1382. READ_BUF(8);
  1383. p = xdr_decode_hyper(p, &lcp->lc_last_wr);
  1384. } else
  1385. lcp->lc_last_wr = 0;
  1386. READ_BUF(4);
  1387. timechange = be32_to_cpup(p++);
  1388. if (timechange) {
  1389. status = nfsd4_decode_time(argp, &lcp->lc_mtime);
  1390. if (status)
  1391. return status;
  1392. } else {
  1393. lcp->lc_mtime.tv_nsec = UTIME_NOW;
  1394. }
  1395. READ_BUF(8);
  1396. lcp->lc_layout_type = be32_to_cpup(p++);
  1397. /*
  1398. * Save the layout update in XDR format and let the layout driver deal
  1399. * with it later.
  1400. */
  1401. lcp->lc_up_len = be32_to_cpup(p++);
  1402. if (lcp->lc_up_len > 0) {
  1403. READ_BUF(lcp->lc_up_len);
  1404. READMEM(lcp->lc_up_layout, lcp->lc_up_len);
  1405. }
  1406. DECODE_TAIL;
  1407. }
  1408. static __be32
  1409. nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
  1410. struct nfsd4_layoutreturn *lrp)
  1411. {
  1412. DECODE_HEAD;
  1413. READ_BUF(16);
  1414. lrp->lr_reclaim = be32_to_cpup(p++);
  1415. lrp->lr_layout_type = be32_to_cpup(p++);
  1416. lrp->lr_seg.iomode = be32_to_cpup(p++);
  1417. lrp->lr_return_type = be32_to_cpup(p++);
  1418. if (lrp->lr_return_type == RETURN_FILE) {
  1419. READ_BUF(16);
  1420. p = xdr_decode_hyper(p, &lrp->lr_seg.offset);
  1421. p = xdr_decode_hyper(p, &lrp->lr_seg.length);
  1422. status = nfsd4_decode_stateid(argp, &lrp->lr_sid);
  1423. if (status)
  1424. return status;
  1425. READ_BUF(4);
  1426. lrp->lrf_body_len = be32_to_cpup(p++);
  1427. if (lrp->lrf_body_len > 0) {
  1428. READ_BUF(lrp->lrf_body_len);
  1429. READMEM(lrp->lrf_body, lrp->lrf_body_len);
  1430. }
  1431. } else {
  1432. lrp->lr_seg.offset = 0;
  1433. lrp->lr_seg.length = NFS4_MAX_UINT64;
  1434. }
  1435. DECODE_TAIL;
  1436. }
  1437. #endif /* CONFIG_NFSD_PNFS */
  1438. static __be32
  1439. nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
  1440. struct nfsd4_fallocate *fallocate)
  1441. {
  1442. DECODE_HEAD;
  1443. status = nfsd4_decode_stateid(argp, &fallocate->falloc_stateid);
  1444. if (status)
  1445. return status;
  1446. READ_BUF(16);
  1447. p = xdr_decode_hyper(p, &fallocate->falloc_offset);
  1448. xdr_decode_hyper(p, &fallocate->falloc_length);
  1449. DECODE_TAIL;
  1450. }
  1451. static __be32
  1452. nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
  1453. {
  1454. DECODE_HEAD;
  1455. status = nfsd4_decode_stateid(argp, &seek->seek_stateid);
  1456. if (status)
  1457. return status;
  1458. READ_BUF(8 + 4);
  1459. p = xdr_decode_hyper(p, &seek->seek_offset);
  1460. seek->seek_whence = be32_to_cpup(p);
  1461. DECODE_TAIL;
  1462. }
  1463. static __be32
  1464. nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
  1465. {
  1466. return nfs_ok;
  1467. }
  1468. static __be32
  1469. nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
  1470. {
  1471. return nfserr_notsupp;
  1472. }
  1473. typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
  1474. static nfsd4_dec nfsd4_dec_ops[] = {
  1475. [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
  1476. [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
  1477. [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
  1478. [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
  1479. [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1480. [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
  1481. [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
  1482. [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
  1483. [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
  1484. [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
  1485. [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
  1486. [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
  1487. [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
  1488. [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
  1489. [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1490. [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
  1491. [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1492. [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
  1493. [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
  1494. [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
  1495. [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
  1496. [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
  1497. [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
  1498. [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
  1499. [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
  1500. [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
  1501. [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
  1502. [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
  1503. [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
  1504. [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
  1505. [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
  1506. [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
  1507. [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
  1508. [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
  1509. [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1510. [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
  1511. [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
  1512. /* new operations for NFSv4.1 */
  1513. [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
  1514. [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
  1515. [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
  1516. [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
  1517. [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
  1518. [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
  1519. [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1520. #ifdef CONFIG_NFSD_PNFS
  1521. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
  1522. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  1523. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
  1524. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
  1525. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
  1526. #else
  1527. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
  1528. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  1529. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
  1530. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
  1531. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
  1532. #endif
  1533. [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
  1534. [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
  1535. [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
  1536. [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
  1537. [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  1538. [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
  1539. [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
  1540. /* new operations for NFSv4.2 */
  1541. [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
  1542. [OP_COPY] = (nfsd4_dec)nfsd4_decode_notsupp,
  1543. [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_notsupp,
  1544. [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
  1545. [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1546. [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1547. [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1548. [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_notsupp,
  1549. [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1550. [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_notsupp,
  1551. [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
  1552. [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
  1553. };
  1554. static inline bool
  1555. nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
  1556. {
  1557. if (op->opnum < FIRST_NFS4_OP)
  1558. return false;
  1559. else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
  1560. return false;
  1561. else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
  1562. return false;
  1563. else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
  1564. return false;
  1565. return true;
  1566. }
  1567. static __be32
  1568. nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
  1569. {
  1570. DECODE_HEAD;
  1571. struct nfsd4_op *op;
  1572. bool cachethis = false;
  1573. int auth_slack= argp->rqstp->rq_auth_slack;
  1574. int max_reply = auth_slack + 8; /* opcnt, status */
  1575. int readcount = 0;
  1576. int readbytes = 0;
  1577. int i;
  1578. READ_BUF(4);
  1579. argp->taglen = be32_to_cpup(p++);
  1580. READ_BUF(argp->taglen + 8);
  1581. SAVEMEM(argp->tag, argp->taglen);
  1582. argp->minorversion = be32_to_cpup(p++);
  1583. argp->opcnt = be32_to_cpup(p++);
  1584. max_reply += 4 + (XDR_QUADLEN(argp->taglen) << 2);
  1585. if (argp->taglen > NFSD4_MAX_TAGLEN)
  1586. goto xdr_error;
  1587. if (argp->opcnt > 100)
  1588. goto xdr_error;
  1589. if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
  1590. argp->ops = kzalloc(argp->opcnt * sizeof(*argp->ops), GFP_KERNEL);
  1591. if (!argp->ops) {
  1592. argp->ops = argp->iops;
  1593. dprintk("nfsd: couldn't allocate room for COMPOUND\n");
  1594. goto xdr_error;
  1595. }
  1596. }
  1597. if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
  1598. argp->opcnt = 0;
  1599. for (i = 0; i < argp->opcnt; i++) {
  1600. op = &argp->ops[i];
  1601. op->replay = NULL;
  1602. READ_BUF(4);
  1603. op->opnum = be32_to_cpup(p++);
  1604. if (nfsd4_opnum_in_range(argp, op))
  1605. op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
  1606. else {
  1607. op->opnum = OP_ILLEGAL;
  1608. op->status = nfserr_op_illegal;
  1609. }
  1610. /*
  1611. * We'll try to cache the result in the DRC if any one
  1612. * op in the compound wants to be cached:
  1613. */
  1614. cachethis |= nfsd4_cache_this_op(op);
  1615. if (op->opnum == OP_READ) {
  1616. readcount++;
  1617. readbytes += nfsd4_max_reply(argp->rqstp, op);
  1618. } else
  1619. max_reply += nfsd4_max_reply(argp->rqstp, op);
  1620. /*
  1621. * OP_LOCK may return a conflicting lock. (Special case
  1622. * because it will just skip encoding this if it runs
  1623. * out of xdr buffer space, and it is the only operation
  1624. * that behaves this way.)
  1625. */
  1626. if (op->opnum == OP_LOCK)
  1627. max_reply += NFS4_OPAQUE_LIMIT;
  1628. if (op->status) {
  1629. argp->opcnt = i+1;
  1630. break;
  1631. }
  1632. }
  1633. /* Sessions make the DRC unnecessary: */
  1634. if (argp->minorversion)
  1635. cachethis = false;
  1636. svc_reserve(argp->rqstp, max_reply + readbytes);
  1637. argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
  1638. if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
  1639. clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
  1640. DECODE_TAIL;
  1641. }
  1642. static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode)
  1643. {
  1644. if (IS_I_VERSION(inode)) {
  1645. p = xdr_encode_hyper(p, inode->i_version);
  1646. } else {
  1647. *p++ = cpu_to_be32(stat->ctime.tv_sec);
  1648. *p++ = cpu_to_be32(stat->ctime.tv_nsec);
  1649. }
  1650. return p;
  1651. }
  1652. static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
  1653. {
  1654. *p++ = cpu_to_be32(c->atomic);
  1655. if (c->change_supported) {
  1656. p = xdr_encode_hyper(p, c->before_change);
  1657. p = xdr_encode_hyper(p, c->after_change);
  1658. } else {
  1659. *p++ = cpu_to_be32(c->before_ctime_sec);
  1660. *p++ = cpu_to_be32(c->before_ctime_nsec);
  1661. *p++ = cpu_to_be32(c->after_ctime_sec);
  1662. *p++ = cpu_to_be32(c->after_ctime_nsec);
  1663. }
  1664. return p;
  1665. }
  1666. /* Encode as an array of strings the string given with components
  1667. * separated @sep, escaped with esc_enter and esc_exit.
  1668. */
  1669. static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
  1670. char *components, char esc_enter,
  1671. char esc_exit)
  1672. {
  1673. __be32 *p;
  1674. __be32 pathlen;
  1675. int pathlen_offset;
  1676. int strlen, count=0;
  1677. char *str, *end, *next;
  1678. dprintk("nfsd4_encode_components(%s)\n", components);
  1679. pathlen_offset = xdr->buf->len;
  1680. p = xdr_reserve_space(xdr, 4);
  1681. if (!p)
  1682. return nfserr_resource;
  1683. p++; /* We will fill this in with @count later */
  1684. end = str = components;
  1685. while (*end) {
  1686. bool found_esc = false;
  1687. /* try to parse as esc_start, ..., esc_end, sep */
  1688. if (*str == esc_enter) {
  1689. for (; *end && (*end != esc_exit); end++)
  1690. /* find esc_exit or end of string */;
  1691. next = end + 1;
  1692. if (*end && (!*next || *next == sep)) {
  1693. str++;
  1694. found_esc = true;
  1695. }
  1696. }
  1697. if (!found_esc)
  1698. for (; *end && (*end != sep); end++)
  1699. /* find sep or end of string */;
  1700. strlen = end - str;
  1701. if (strlen) {
  1702. p = xdr_reserve_space(xdr, strlen + 4);
  1703. if (!p)
  1704. return nfserr_resource;
  1705. p = xdr_encode_opaque(p, str, strlen);
  1706. count++;
  1707. }
  1708. else
  1709. end++;
  1710. if (found_esc)
  1711. end = next;
  1712. str = end;
  1713. }
  1714. pathlen = htonl(count);
  1715. write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
  1716. return 0;
  1717. }
  1718. /* Encode as an array of strings the string given with components
  1719. * separated @sep.
  1720. */
  1721. static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
  1722. char *components)
  1723. {
  1724. return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
  1725. }
  1726. /*
  1727. * encode a location element of a fs_locations structure
  1728. */
  1729. static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
  1730. struct nfsd4_fs_location *location)
  1731. {
  1732. __be32 status;
  1733. status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
  1734. '[', ']');
  1735. if (status)
  1736. return status;
  1737. status = nfsd4_encode_components(xdr, '/', location->path);
  1738. if (status)
  1739. return status;
  1740. return 0;
  1741. }
  1742. /*
  1743. * Encode a path in RFC3530 'pathname4' format
  1744. */
  1745. static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
  1746. const struct path *root,
  1747. const struct path *path)
  1748. {
  1749. struct path cur = *path;
  1750. __be32 *p;
  1751. struct dentry **components = NULL;
  1752. unsigned int ncomponents = 0;
  1753. __be32 err = nfserr_jukebox;
  1754. dprintk("nfsd4_encode_components(");
  1755. path_get(&cur);
  1756. /* First walk the path up to the nfsd root, and store the
  1757. * dentries/path components in an array.
  1758. */
  1759. for (;;) {
  1760. if (path_equal(&cur, root))
  1761. break;
  1762. if (cur.dentry == cur.mnt->mnt_root) {
  1763. if (follow_up(&cur))
  1764. continue;
  1765. goto out_free;
  1766. }
  1767. if ((ncomponents & 15) == 0) {
  1768. struct dentry **new;
  1769. new = krealloc(components,
  1770. sizeof(*new) * (ncomponents + 16),
  1771. GFP_KERNEL);
  1772. if (!new)
  1773. goto out_free;
  1774. components = new;
  1775. }
  1776. components[ncomponents++] = cur.dentry;
  1777. cur.dentry = dget_parent(cur.dentry);
  1778. }
  1779. err = nfserr_resource;
  1780. p = xdr_reserve_space(xdr, 4);
  1781. if (!p)
  1782. goto out_free;
  1783. *p++ = cpu_to_be32(ncomponents);
  1784. while (ncomponents) {
  1785. struct dentry *dentry = components[ncomponents - 1];
  1786. unsigned int len;
  1787. spin_lock(&dentry->d_lock);
  1788. len = dentry->d_name.len;
  1789. p = xdr_reserve_space(xdr, len + 4);
  1790. if (!p) {
  1791. spin_unlock(&dentry->d_lock);
  1792. goto out_free;
  1793. }
  1794. p = xdr_encode_opaque(p, dentry->d_name.name, len);
  1795. dprintk("/%pd", dentry);
  1796. spin_unlock(&dentry->d_lock);
  1797. dput(dentry);
  1798. ncomponents--;
  1799. }
  1800. err = 0;
  1801. out_free:
  1802. dprintk(")\n");
  1803. while (ncomponents)
  1804. dput(components[--ncomponents]);
  1805. kfree(components);
  1806. path_put(&cur);
  1807. return err;
  1808. }
  1809. static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
  1810. struct svc_rqst *rqstp, const struct path *path)
  1811. {
  1812. struct svc_export *exp_ps;
  1813. __be32 res;
  1814. exp_ps = rqst_find_fsidzero_export(rqstp);
  1815. if (IS_ERR(exp_ps))
  1816. return nfserrno(PTR_ERR(exp_ps));
  1817. res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
  1818. exp_put(exp_ps);
  1819. return res;
  1820. }
  1821. /*
  1822. * encode a fs_locations structure
  1823. */
  1824. static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
  1825. struct svc_rqst *rqstp, struct svc_export *exp)
  1826. {
  1827. __be32 status;
  1828. int i;
  1829. __be32 *p;
  1830. struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
  1831. status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
  1832. if (status)
  1833. return status;
  1834. p = xdr_reserve_space(xdr, 4);
  1835. if (!p)
  1836. return nfserr_resource;
  1837. *p++ = cpu_to_be32(fslocs->locations_count);
  1838. for (i=0; i<fslocs->locations_count; i++) {
  1839. status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
  1840. if (status)
  1841. return status;
  1842. }
  1843. return 0;
  1844. }
  1845. static u32 nfs4_file_type(umode_t mode)
  1846. {
  1847. switch (mode & S_IFMT) {
  1848. case S_IFIFO: return NF4FIFO;
  1849. case S_IFCHR: return NF4CHR;
  1850. case S_IFDIR: return NF4DIR;
  1851. case S_IFBLK: return NF4BLK;
  1852. case S_IFLNK: return NF4LNK;
  1853. case S_IFREG: return NF4REG;
  1854. case S_IFSOCK: return NF4SOCK;
  1855. default: return NF4BAD;
  1856. };
  1857. }
  1858. static inline __be32
  1859. nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  1860. struct nfs4_ace *ace)
  1861. {
  1862. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  1863. return nfs4_acl_write_who(xdr, ace->whotype);
  1864. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  1865. return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
  1866. else
  1867. return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
  1868. }
  1869. #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
  1870. FATTR4_WORD0_RDATTR_ERROR)
  1871. #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
  1872. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  1873. static inline __be32
  1874. nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  1875. void *context, int len)
  1876. {
  1877. __be32 *p;
  1878. p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
  1879. if (!p)
  1880. return nfserr_resource;
  1881. /*
  1882. * For now we use a 0 here to indicate the null translation; in
  1883. * the future we may place a call to translation code here.
  1884. */
  1885. *p++ = cpu_to_be32(0); /* lfs */
  1886. *p++ = cpu_to_be32(0); /* pi */
  1887. p = xdr_encode_opaque(p, context, len);
  1888. return 0;
  1889. }
  1890. #else
  1891. static inline __be32
  1892. nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  1893. void *context, int len)
  1894. { return 0; }
  1895. #endif
  1896. static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *rdattr_err)
  1897. {
  1898. /* As per referral draft: */
  1899. if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
  1900. *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
  1901. if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
  1902. *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
  1903. *rdattr_err = NFSERR_MOVED;
  1904. else
  1905. return nfserr_moved;
  1906. }
  1907. *bmval0 &= WORD0_ABSENT_FS_ATTRS;
  1908. *bmval1 &= WORD1_ABSENT_FS_ATTRS;
  1909. return 0;
  1910. }
  1911. static int get_parent_attributes(struct svc_export *exp, struct kstat *stat)
  1912. {
  1913. struct path path = exp->ex_path;
  1914. int err;
  1915. path_get(&path);
  1916. while (follow_up(&path)) {
  1917. if (path.dentry != path.mnt->mnt_root)
  1918. break;
  1919. }
  1920. err = vfs_getattr(&path, stat);
  1921. path_put(&path);
  1922. return err;
  1923. }
  1924. /*
  1925. * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
  1926. * ourselves.
  1927. */
  1928. static __be32
  1929. nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
  1930. struct svc_export *exp,
  1931. struct dentry *dentry, u32 *bmval,
  1932. struct svc_rqst *rqstp, int ignore_crossmnt)
  1933. {
  1934. u32 bmval0 = bmval[0];
  1935. u32 bmval1 = bmval[1];
  1936. u32 bmval2 = bmval[2];
  1937. struct kstat stat;
  1938. struct svc_fh *tempfh = NULL;
  1939. struct kstatfs statfs;
  1940. __be32 *p;
  1941. int starting_len = xdr->buf->len;
  1942. int attrlen_offset;
  1943. __be32 attrlen;
  1944. u32 dummy;
  1945. u64 dummy64;
  1946. u32 rdattr_err = 0;
  1947. __be32 status;
  1948. int err;
  1949. int aclsupport = 0;
  1950. struct nfs4_acl *acl = NULL;
  1951. void *context = NULL;
  1952. int contextlen;
  1953. bool contextsupport = false;
  1954. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1955. u32 minorversion = resp->cstate.minorversion;
  1956. struct path path = {
  1957. .mnt = exp->ex_path.mnt,
  1958. .dentry = dentry,
  1959. };
  1960. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1961. BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
  1962. BUG_ON(bmval0 & ~nfsd_suppattrs0(minorversion));
  1963. BUG_ON(bmval1 & ~nfsd_suppattrs1(minorversion));
  1964. BUG_ON(bmval2 & ~nfsd_suppattrs2(minorversion));
  1965. if (exp->ex_fslocs.migrated) {
  1966. BUG_ON(bmval[2]);
  1967. status = fattr_handle_absent_fs(&bmval0, &bmval1, &rdattr_err);
  1968. if (status)
  1969. goto out;
  1970. }
  1971. err = vfs_getattr(&path, &stat);
  1972. if (err)
  1973. goto out_nfserr;
  1974. if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
  1975. FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
  1976. (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
  1977. FATTR4_WORD1_SPACE_TOTAL))) {
  1978. err = vfs_statfs(&path, &statfs);
  1979. if (err)
  1980. goto out_nfserr;
  1981. }
  1982. if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
  1983. tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
  1984. status = nfserr_jukebox;
  1985. if (!tempfh)
  1986. goto out;
  1987. fh_init(tempfh, NFS4_FHSIZE);
  1988. status = fh_compose(tempfh, exp, dentry, NULL);
  1989. if (status)
  1990. goto out;
  1991. fhp = tempfh;
  1992. }
  1993. if (bmval0 & (FATTR4_WORD0_ACL | FATTR4_WORD0_ACLSUPPORT
  1994. | FATTR4_WORD0_SUPPORTED_ATTRS)) {
  1995. err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
  1996. aclsupport = (err == 0);
  1997. if (bmval0 & FATTR4_WORD0_ACL) {
  1998. if (err == -EOPNOTSUPP)
  1999. bmval0 &= ~FATTR4_WORD0_ACL;
  2000. else if (err == -EINVAL) {
  2001. status = nfserr_attrnotsupp;
  2002. goto out;
  2003. } else if (err != 0)
  2004. goto out_nfserr;
  2005. }
  2006. }
  2007. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2008. if ((bmval[2] & FATTR4_WORD2_SECURITY_LABEL) ||
  2009. bmval[0] & FATTR4_WORD0_SUPPORTED_ATTRS) {
  2010. err = security_inode_getsecctx(d_inode(dentry),
  2011. &context, &contextlen);
  2012. contextsupport = (err == 0);
  2013. if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
  2014. if (err == -EOPNOTSUPP)
  2015. bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  2016. else if (err)
  2017. goto out_nfserr;
  2018. }
  2019. }
  2020. #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
  2021. if (bmval2) {
  2022. p = xdr_reserve_space(xdr, 16);
  2023. if (!p)
  2024. goto out_resource;
  2025. *p++ = cpu_to_be32(3);
  2026. *p++ = cpu_to_be32(bmval0);
  2027. *p++ = cpu_to_be32(bmval1);
  2028. *p++ = cpu_to_be32(bmval2);
  2029. } else if (bmval1) {
  2030. p = xdr_reserve_space(xdr, 12);
  2031. if (!p)
  2032. goto out_resource;
  2033. *p++ = cpu_to_be32(2);
  2034. *p++ = cpu_to_be32(bmval0);
  2035. *p++ = cpu_to_be32(bmval1);
  2036. } else {
  2037. p = xdr_reserve_space(xdr, 8);
  2038. if (!p)
  2039. goto out_resource;
  2040. *p++ = cpu_to_be32(1);
  2041. *p++ = cpu_to_be32(bmval0);
  2042. }
  2043. attrlen_offset = xdr->buf->len;
  2044. p = xdr_reserve_space(xdr, 4);
  2045. if (!p)
  2046. goto out_resource;
  2047. p++; /* to be backfilled later */
  2048. if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  2049. u32 word0 = nfsd_suppattrs0(minorversion);
  2050. u32 word1 = nfsd_suppattrs1(minorversion);
  2051. u32 word2 = nfsd_suppattrs2(minorversion);
  2052. if (!aclsupport)
  2053. word0 &= ~FATTR4_WORD0_ACL;
  2054. if (!contextsupport)
  2055. word2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  2056. if (!word2) {
  2057. p = xdr_reserve_space(xdr, 12);
  2058. if (!p)
  2059. goto out_resource;
  2060. *p++ = cpu_to_be32(2);
  2061. *p++ = cpu_to_be32(word0);
  2062. *p++ = cpu_to_be32(word1);
  2063. } else {
  2064. p = xdr_reserve_space(xdr, 16);
  2065. if (!p)
  2066. goto out_resource;
  2067. *p++ = cpu_to_be32(3);
  2068. *p++ = cpu_to_be32(word0);
  2069. *p++ = cpu_to_be32(word1);
  2070. *p++ = cpu_to_be32(word2);
  2071. }
  2072. }
  2073. if (bmval0 & FATTR4_WORD0_TYPE) {
  2074. p = xdr_reserve_space(xdr, 4);
  2075. if (!p)
  2076. goto out_resource;
  2077. dummy = nfs4_file_type(stat.mode);
  2078. if (dummy == NF4BAD) {
  2079. status = nfserr_serverfault;
  2080. goto out;
  2081. }
  2082. *p++ = cpu_to_be32(dummy);
  2083. }
  2084. if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
  2085. p = xdr_reserve_space(xdr, 4);
  2086. if (!p)
  2087. goto out_resource;
  2088. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  2089. *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
  2090. else
  2091. *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
  2092. NFS4_FH_VOL_RENAME);
  2093. }
  2094. if (bmval0 & FATTR4_WORD0_CHANGE) {
  2095. p = xdr_reserve_space(xdr, 8);
  2096. if (!p)
  2097. goto out_resource;
  2098. p = encode_change(p, &stat, d_inode(dentry));
  2099. }
  2100. if (bmval0 & FATTR4_WORD0_SIZE) {
  2101. p = xdr_reserve_space(xdr, 8);
  2102. if (!p)
  2103. goto out_resource;
  2104. p = xdr_encode_hyper(p, stat.size);
  2105. }
  2106. if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
  2107. p = xdr_reserve_space(xdr, 4);
  2108. if (!p)
  2109. goto out_resource;
  2110. *p++ = cpu_to_be32(1);
  2111. }
  2112. if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
  2113. p = xdr_reserve_space(xdr, 4);
  2114. if (!p)
  2115. goto out_resource;
  2116. *p++ = cpu_to_be32(1);
  2117. }
  2118. if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
  2119. p = xdr_reserve_space(xdr, 4);
  2120. if (!p)
  2121. goto out_resource;
  2122. *p++ = cpu_to_be32(0);
  2123. }
  2124. if (bmval0 & FATTR4_WORD0_FSID) {
  2125. p = xdr_reserve_space(xdr, 16);
  2126. if (!p)
  2127. goto out_resource;
  2128. if (exp->ex_fslocs.migrated) {
  2129. p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
  2130. p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
  2131. } else switch(fsid_source(fhp)) {
  2132. case FSIDSOURCE_FSID:
  2133. p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
  2134. p = xdr_encode_hyper(p, (u64)0);
  2135. break;
  2136. case FSIDSOURCE_DEV:
  2137. *p++ = cpu_to_be32(0);
  2138. *p++ = cpu_to_be32(MAJOR(stat.dev));
  2139. *p++ = cpu_to_be32(0);
  2140. *p++ = cpu_to_be32(MINOR(stat.dev));
  2141. break;
  2142. case FSIDSOURCE_UUID:
  2143. p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
  2144. EX_UUID_LEN);
  2145. break;
  2146. }
  2147. }
  2148. if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
  2149. p = xdr_reserve_space(xdr, 4);
  2150. if (!p)
  2151. goto out_resource;
  2152. *p++ = cpu_to_be32(0);
  2153. }
  2154. if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
  2155. p = xdr_reserve_space(xdr, 4);
  2156. if (!p)
  2157. goto out_resource;
  2158. *p++ = cpu_to_be32(nn->nfsd4_lease);
  2159. }
  2160. if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
  2161. p = xdr_reserve_space(xdr, 4);
  2162. if (!p)
  2163. goto out_resource;
  2164. *p++ = cpu_to_be32(rdattr_err);
  2165. }
  2166. if (bmval0 & FATTR4_WORD0_ACL) {
  2167. struct nfs4_ace *ace;
  2168. if (acl == NULL) {
  2169. p = xdr_reserve_space(xdr, 4);
  2170. if (!p)
  2171. goto out_resource;
  2172. *p++ = cpu_to_be32(0);
  2173. goto out_acl;
  2174. }
  2175. p = xdr_reserve_space(xdr, 4);
  2176. if (!p)
  2177. goto out_resource;
  2178. *p++ = cpu_to_be32(acl->naces);
  2179. for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
  2180. p = xdr_reserve_space(xdr, 4*3);
  2181. if (!p)
  2182. goto out_resource;
  2183. *p++ = cpu_to_be32(ace->type);
  2184. *p++ = cpu_to_be32(ace->flag);
  2185. *p++ = cpu_to_be32(ace->access_mask &
  2186. NFS4_ACE_MASK_ALL);
  2187. status = nfsd4_encode_aclname(xdr, rqstp, ace);
  2188. if (status)
  2189. goto out;
  2190. }
  2191. }
  2192. out_acl:
  2193. if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
  2194. p = xdr_reserve_space(xdr, 4);
  2195. if (!p)
  2196. goto out_resource;
  2197. *p++ = cpu_to_be32(aclsupport ?
  2198. ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
  2199. }
  2200. if (bmval0 & FATTR4_WORD0_CANSETTIME) {
  2201. p = xdr_reserve_space(xdr, 4);
  2202. if (!p)
  2203. goto out_resource;
  2204. *p++ = cpu_to_be32(1);
  2205. }
  2206. if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
  2207. p = xdr_reserve_space(xdr, 4);
  2208. if (!p)
  2209. goto out_resource;
  2210. *p++ = cpu_to_be32(0);
  2211. }
  2212. if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
  2213. p = xdr_reserve_space(xdr, 4);
  2214. if (!p)
  2215. goto out_resource;
  2216. *p++ = cpu_to_be32(1);
  2217. }
  2218. if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
  2219. p = xdr_reserve_space(xdr, 4);
  2220. if (!p)
  2221. goto out_resource;
  2222. *p++ = cpu_to_be32(1);
  2223. }
  2224. if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
  2225. p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
  2226. if (!p)
  2227. goto out_resource;
  2228. p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base,
  2229. fhp->fh_handle.fh_size);
  2230. }
  2231. if (bmval0 & FATTR4_WORD0_FILEID) {
  2232. p = xdr_reserve_space(xdr, 8);
  2233. if (!p)
  2234. goto out_resource;
  2235. p = xdr_encode_hyper(p, stat.ino);
  2236. }
  2237. if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
  2238. p = xdr_reserve_space(xdr, 8);
  2239. if (!p)
  2240. goto out_resource;
  2241. p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
  2242. }
  2243. if (bmval0 & FATTR4_WORD0_FILES_FREE) {
  2244. p = xdr_reserve_space(xdr, 8);
  2245. if (!p)
  2246. goto out_resource;
  2247. p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
  2248. }
  2249. if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
  2250. p = xdr_reserve_space(xdr, 8);
  2251. if (!p)
  2252. goto out_resource;
  2253. p = xdr_encode_hyper(p, (u64) statfs.f_files);
  2254. }
  2255. if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
  2256. status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
  2257. if (status)
  2258. goto out;
  2259. }
  2260. if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
  2261. p = xdr_reserve_space(xdr, 4);
  2262. if (!p)
  2263. goto out_resource;
  2264. *p++ = cpu_to_be32(1);
  2265. }
  2266. if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
  2267. p = xdr_reserve_space(xdr, 8);
  2268. if (!p)
  2269. goto out_resource;
  2270. p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
  2271. }
  2272. if (bmval0 & FATTR4_WORD0_MAXLINK) {
  2273. p = xdr_reserve_space(xdr, 4);
  2274. if (!p)
  2275. goto out_resource;
  2276. *p++ = cpu_to_be32(255);
  2277. }
  2278. if (bmval0 & FATTR4_WORD0_MAXNAME) {
  2279. p = xdr_reserve_space(xdr, 4);
  2280. if (!p)
  2281. goto out_resource;
  2282. *p++ = cpu_to_be32(statfs.f_namelen);
  2283. }
  2284. if (bmval0 & FATTR4_WORD0_MAXREAD) {
  2285. p = xdr_reserve_space(xdr, 8);
  2286. if (!p)
  2287. goto out_resource;
  2288. p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
  2289. }
  2290. if (bmval0 & FATTR4_WORD0_MAXWRITE) {
  2291. p = xdr_reserve_space(xdr, 8);
  2292. if (!p)
  2293. goto out_resource;
  2294. p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
  2295. }
  2296. if (bmval1 & FATTR4_WORD1_MODE) {
  2297. p = xdr_reserve_space(xdr, 4);
  2298. if (!p)
  2299. goto out_resource;
  2300. *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
  2301. }
  2302. if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
  2303. p = xdr_reserve_space(xdr, 4);
  2304. if (!p)
  2305. goto out_resource;
  2306. *p++ = cpu_to_be32(1);
  2307. }
  2308. if (bmval1 & FATTR4_WORD1_NUMLINKS) {
  2309. p = xdr_reserve_space(xdr, 4);
  2310. if (!p)
  2311. goto out_resource;
  2312. *p++ = cpu_to_be32(stat.nlink);
  2313. }
  2314. if (bmval1 & FATTR4_WORD1_OWNER) {
  2315. status = nfsd4_encode_user(xdr, rqstp, stat.uid);
  2316. if (status)
  2317. goto out;
  2318. }
  2319. if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
  2320. status = nfsd4_encode_group(xdr, rqstp, stat.gid);
  2321. if (status)
  2322. goto out;
  2323. }
  2324. if (bmval1 & FATTR4_WORD1_RAWDEV) {
  2325. p = xdr_reserve_space(xdr, 8);
  2326. if (!p)
  2327. goto out_resource;
  2328. *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
  2329. *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
  2330. }
  2331. if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
  2332. p = xdr_reserve_space(xdr, 8);
  2333. if (!p)
  2334. goto out_resource;
  2335. dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
  2336. p = xdr_encode_hyper(p, dummy64);
  2337. }
  2338. if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
  2339. p = xdr_reserve_space(xdr, 8);
  2340. if (!p)
  2341. goto out_resource;
  2342. dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
  2343. p = xdr_encode_hyper(p, dummy64);
  2344. }
  2345. if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
  2346. p = xdr_reserve_space(xdr, 8);
  2347. if (!p)
  2348. goto out_resource;
  2349. dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
  2350. p = xdr_encode_hyper(p, dummy64);
  2351. }
  2352. if (bmval1 & FATTR4_WORD1_SPACE_USED) {
  2353. p = xdr_reserve_space(xdr, 8);
  2354. if (!p)
  2355. goto out_resource;
  2356. dummy64 = (u64)stat.blocks << 9;
  2357. p = xdr_encode_hyper(p, dummy64);
  2358. }
  2359. if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
  2360. p = xdr_reserve_space(xdr, 12);
  2361. if (!p)
  2362. goto out_resource;
  2363. p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
  2364. *p++ = cpu_to_be32(stat.atime.tv_nsec);
  2365. }
  2366. if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
  2367. p = xdr_reserve_space(xdr, 12);
  2368. if (!p)
  2369. goto out_resource;
  2370. *p++ = cpu_to_be32(0);
  2371. *p++ = cpu_to_be32(1);
  2372. *p++ = cpu_to_be32(0);
  2373. }
  2374. if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
  2375. p = xdr_reserve_space(xdr, 12);
  2376. if (!p)
  2377. goto out_resource;
  2378. p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
  2379. *p++ = cpu_to_be32(stat.ctime.tv_nsec);
  2380. }
  2381. if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
  2382. p = xdr_reserve_space(xdr, 12);
  2383. if (!p)
  2384. goto out_resource;
  2385. p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
  2386. *p++ = cpu_to_be32(stat.mtime.tv_nsec);
  2387. }
  2388. if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
  2389. p = xdr_reserve_space(xdr, 8);
  2390. if (!p)
  2391. goto out_resource;
  2392. /*
  2393. * Get parent's attributes if not ignoring crossmount
  2394. * and this is the root of a cross-mounted filesystem.
  2395. */
  2396. if (ignore_crossmnt == 0 &&
  2397. dentry == exp->ex_path.mnt->mnt_root)
  2398. get_parent_attributes(exp, &stat);
  2399. p = xdr_encode_hyper(p, stat.ino);
  2400. }
  2401. #ifdef CONFIG_NFSD_PNFS
  2402. if ((bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) ||
  2403. (bmval2 & FATTR4_WORD2_LAYOUT_TYPES)) {
  2404. if (exp->ex_layout_type) {
  2405. p = xdr_reserve_space(xdr, 8);
  2406. if (!p)
  2407. goto out_resource;
  2408. *p++ = cpu_to_be32(1);
  2409. *p++ = cpu_to_be32(exp->ex_layout_type);
  2410. } else {
  2411. p = xdr_reserve_space(xdr, 4);
  2412. if (!p)
  2413. goto out_resource;
  2414. *p++ = cpu_to_be32(0);
  2415. }
  2416. }
  2417. if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
  2418. p = xdr_reserve_space(xdr, 4);
  2419. if (!p)
  2420. goto out_resource;
  2421. *p++ = cpu_to_be32(stat.blksize);
  2422. }
  2423. #endif /* CONFIG_NFSD_PNFS */
  2424. if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
  2425. status = nfsd4_encode_security_label(xdr, rqstp, context,
  2426. contextlen);
  2427. if (status)
  2428. goto out;
  2429. }
  2430. if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
  2431. p = xdr_reserve_space(xdr, 16);
  2432. if (!p)
  2433. goto out_resource;
  2434. *p++ = cpu_to_be32(3);
  2435. *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD0);
  2436. *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD1);
  2437. *p++ = cpu_to_be32(NFSD_SUPPATTR_EXCLCREAT_WORD2);
  2438. }
  2439. attrlen = htonl(xdr->buf->len - attrlen_offset - 4);
  2440. write_bytes_to_xdr_buf(xdr->buf, attrlen_offset, &attrlen, 4);
  2441. status = nfs_ok;
  2442. out:
  2443. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2444. if (context)
  2445. security_release_secctx(context, contextlen);
  2446. #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
  2447. kfree(acl);
  2448. if (tempfh) {
  2449. fh_put(tempfh);
  2450. kfree(tempfh);
  2451. }
  2452. if (status)
  2453. xdr_truncate_encode(xdr, starting_len);
  2454. return status;
  2455. out_nfserr:
  2456. status = nfserrno(err);
  2457. goto out;
  2458. out_resource:
  2459. status = nfserr_resource;
  2460. goto out;
  2461. }
  2462. static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
  2463. struct xdr_buf *buf, __be32 *p, int bytes)
  2464. {
  2465. xdr->scratch.iov_len = 0;
  2466. memset(buf, 0, sizeof(struct xdr_buf));
  2467. buf->head[0].iov_base = p;
  2468. buf->head[0].iov_len = 0;
  2469. buf->len = 0;
  2470. xdr->buf = buf;
  2471. xdr->iov = buf->head;
  2472. xdr->p = p;
  2473. xdr->end = (void *)p + bytes;
  2474. buf->buflen = bytes;
  2475. }
  2476. __be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
  2477. struct svc_fh *fhp, struct svc_export *exp,
  2478. struct dentry *dentry, u32 *bmval,
  2479. struct svc_rqst *rqstp, int ignore_crossmnt)
  2480. {
  2481. struct xdr_buf dummy;
  2482. struct xdr_stream xdr;
  2483. __be32 ret;
  2484. svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
  2485. ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
  2486. ignore_crossmnt);
  2487. *p = xdr.p;
  2488. return ret;
  2489. }
  2490. static inline int attributes_need_mount(u32 *bmval)
  2491. {
  2492. if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
  2493. return 1;
  2494. if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
  2495. return 1;
  2496. return 0;
  2497. }
  2498. static __be32
  2499. nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
  2500. const char *name, int namlen)
  2501. {
  2502. struct svc_export *exp = cd->rd_fhp->fh_export;
  2503. struct dentry *dentry;
  2504. __be32 nfserr;
  2505. int ignore_crossmnt = 0;
  2506. dentry = lookup_one_len(name, cd->rd_fhp->fh_dentry, namlen);
  2507. if (IS_ERR(dentry))
  2508. return nfserrno(PTR_ERR(dentry));
  2509. if (d_really_is_negative(dentry)) {
  2510. /*
  2511. * nfsd_buffered_readdir drops the i_mutex between
  2512. * readdir and calling this callback, leaving a window
  2513. * where this directory entry could have gone away.
  2514. */
  2515. dput(dentry);
  2516. return nfserr_noent;
  2517. }
  2518. exp_get(exp);
  2519. /*
  2520. * In the case of a mountpoint, the client may be asking for
  2521. * attributes that are only properties of the underlying filesystem
  2522. * as opposed to the cross-mounted file system. In such a case,
  2523. * we will not follow the cross mount and will fill the attribtutes
  2524. * directly from the mountpoint dentry.
  2525. */
  2526. if (nfsd_mountpoint(dentry, exp)) {
  2527. int err;
  2528. if (!(exp->ex_flags & NFSEXP_V4ROOT)
  2529. && !attributes_need_mount(cd->rd_bmval)) {
  2530. ignore_crossmnt = 1;
  2531. goto out_encode;
  2532. }
  2533. /*
  2534. * Why the heck aren't we just using nfsd_lookup??
  2535. * Different "."/".." handling? Something else?
  2536. * At least, add a comment here to explain....
  2537. */
  2538. err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
  2539. if (err) {
  2540. nfserr = nfserrno(err);
  2541. goto out_put;
  2542. }
  2543. nfserr = check_nfsd_access(exp, cd->rd_rqstp);
  2544. if (nfserr)
  2545. goto out_put;
  2546. }
  2547. out_encode:
  2548. nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
  2549. cd->rd_rqstp, ignore_crossmnt);
  2550. out_put:
  2551. dput(dentry);
  2552. exp_put(exp);
  2553. return nfserr;
  2554. }
  2555. static __be32 *
  2556. nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
  2557. {
  2558. __be32 *p;
  2559. p = xdr_reserve_space(xdr, 20);
  2560. if (!p)
  2561. return NULL;
  2562. *p++ = htonl(2);
  2563. *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
  2564. *p++ = htonl(0); /* bmval1 */
  2565. *p++ = htonl(4); /* attribute length */
  2566. *p++ = nfserr; /* no htonl */
  2567. return p;
  2568. }
  2569. static int
  2570. nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
  2571. loff_t offset, u64 ino, unsigned int d_type)
  2572. {
  2573. struct readdir_cd *ccd = ccdv;
  2574. struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
  2575. struct xdr_stream *xdr = cd->xdr;
  2576. int start_offset = xdr->buf->len;
  2577. int cookie_offset;
  2578. u32 name_and_cookie;
  2579. int entry_bytes;
  2580. __be32 nfserr = nfserr_toosmall;
  2581. __be64 wire_offset;
  2582. __be32 *p;
  2583. /* In nfsv4, "." and ".." never make it onto the wire.. */
  2584. if (name && isdotent(name, namlen)) {
  2585. cd->common.err = nfs_ok;
  2586. return 0;
  2587. }
  2588. if (cd->cookie_offset) {
  2589. wire_offset = cpu_to_be64(offset);
  2590. write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
  2591. &wire_offset, 8);
  2592. }
  2593. p = xdr_reserve_space(xdr, 4);
  2594. if (!p)
  2595. goto fail;
  2596. *p++ = xdr_one; /* mark entry present */
  2597. cookie_offset = xdr->buf->len;
  2598. p = xdr_reserve_space(xdr, 3*4 + namlen);
  2599. if (!p)
  2600. goto fail;
  2601. p = xdr_encode_hyper(p, NFS_OFFSET_MAX); /* offset of next entry */
  2602. p = xdr_encode_array(p, name, namlen); /* name length & name */
  2603. nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
  2604. switch (nfserr) {
  2605. case nfs_ok:
  2606. break;
  2607. case nfserr_resource:
  2608. nfserr = nfserr_toosmall;
  2609. goto fail;
  2610. case nfserr_noent:
  2611. xdr_truncate_encode(xdr, start_offset);
  2612. goto skip_entry;
  2613. default:
  2614. /*
  2615. * If the client requested the RDATTR_ERROR attribute,
  2616. * we stuff the error code into this attribute
  2617. * and continue. If this attribute was not requested,
  2618. * then in accordance with the spec, we fail the
  2619. * entire READDIR operation(!)
  2620. */
  2621. if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
  2622. goto fail;
  2623. p = nfsd4_encode_rdattr_error(xdr, nfserr);
  2624. if (p == NULL) {
  2625. nfserr = nfserr_toosmall;
  2626. goto fail;
  2627. }
  2628. }
  2629. nfserr = nfserr_toosmall;
  2630. entry_bytes = xdr->buf->len - start_offset;
  2631. if (entry_bytes > cd->rd_maxcount)
  2632. goto fail;
  2633. cd->rd_maxcount -= entry_bytes;
  2634. /*
  2635. * RFC 3530 14.2.24 describes rd_dircount as only a "hint", so
  2636. * let's always let through the first entry, at least:
  2637. */
  2638. if (!cd->rd_dircount)
  2639. goto fail;
  2640. name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
  2641. if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
  2642. goto fail;
  2643. cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
  2644. cd->cookie_offset = cookie_offset;
  2645. skip_entry:
  2646. cd->common.err = nfs_ok;
  2647. return 0;
  2648. fail:
  2649. xdr_truncate_encode(xdr, start_offset);
  2650. cd->common.err = nfserr;
  2651. return -EINVAL;
  2652. }
  2653. static __be32
  2654. nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
  2655. {
  2656. __be32 *p;
  2657. p = xdr_reserve_space(xdr, sizeof(stateid_t));
  2658. if (!p)
  2659. return nfserr_resource;
  2660. *p++ = cpu_to_be32(sid->si_generation);
  2661. p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
  2662. sizeof(stateid_opaque_t));
  2663. return 0;
  2664. }
  2665. static __be32
  2666. nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
  2667. {
  2668. struct xdr_stream *xdr = &resp->xdr;
  2669. __be32 *p;
  2670. if (!nfserr) {
  2671. p = xdr_reserve_space(xdr, 8);
  2672. if (!p)
  2673. return nfserr_resource;
  2674. *p++ = cpu_to_be32(access->ac_supported);
  2675. *p++ = cpu_to_be32(access->ac_resp_access);
  2676. }
  2677. return nfserr;
  2678. }
  2679. static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
  2680. {
  2681. struct xdr_stream *xdr = &resp->xdr;
  2682. __be32 *p;
  2683. if (!nfserr) {
  2684. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
  2685. if (!p)
  2686. return nfserr_resource;
  2687. p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
  2688. NFS4_MAX_SESSIONID_LEN);
  2689. *p++ = cpu_to_be32(bcts->dir);
  2690. /* Sorry, we do not yet support RDMA over 4.1: */
  2691. *p++ = cpu_to_be32(0);
  2692. }
  2693. return nfserr;
  2694. }
  2695. static __be32
  2696. nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
  2697. {
  2698. struct xdr_stream *xdr = &resp->xdr;
  2699. if (!nfserr)
  2700. nfserr = nfsd4_encode_stateid(xdr, &close->cl_stateid);
  2701. return nfserr;
  2702. }
  2703. static __be32
  2704. nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
  2705. {
  2706. struct xdr_stream *xdr = &resp->xdr;
  2707. __be32 *p;
  2708. if (!nfserr) {
  2709. p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
  2710. if (!p)
  2711. return nfserr_resource;
  2712. p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
  2713. NFS4_VERIFIER_SIZE);
  2714. }
  2715. return nfserr;
  2716. }
  2717. static __be32
  2718. nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
  2719. {
  2720. struct xdr_stream *xdr = &resp->xdr;
  2721. __be32 *p;
  2722. if (!nfserr) {
  2723. p = xdr_reserve_space(xdr, 32);
  2724. if (!p)
  2725. return nfserr_resource;
  2726. p = encode_cinfo(p, &create->cr_cinfo);
  2727. *p++ = cpu_to_be32(2);
  2728. *p++ = cpu_to_be32(create->cr_bmval[0]);
  2729. *p++ = cpu_to_be32(create->cr_bmval[1]);
  2730. }
  2731. return nfserr;
  2732. }
  2733. static __be32
  2734. nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
  2735. {
  2736. struct svc_fh *fhp = getattr->ga_fhp;
  2737. struct xdr_stream *xdr = &resp->xdr;
  2738. if (nfserr)
  2739. return nfserr;
  2740. nfserr = nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
  2741. getattr->ga_bmval,
  2742. resp->rqstp, 0);
  2743. return nfserr;
  2744. }
  2745. static __be32
  2746. nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
  2747. {
  2748. struct xdr_stream *xdr = &resp->xdr;
  2749. struct svc_fh *fhp = *fhpp;
  2750. unsigned int len;
  2751. __be32 *p;
  2752. if (!nfserr) {
  2753. len = fhp->fh_handle.fh_size;
  2754. p = xdr_reserve_space(xdr, len + 4);
  2755. if (!p)
  2756. return nfserr_resource;
  2757. p = xdr_encode_opaque(p, &fhp->fh_handle.fh_base, len);
  2758. }
  2759. return nfserr;
  2760. }
  2761. /*
  2762. * Including all fields other than the name, a LOCK4denied structure requires
  2763. * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
  2764. */
  2765. static __be32
  2766. nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
  2767. {
  2768. struct xdr_netobj *conf = &ld->ld_owner;
  2769. __be32 *p;
  2770. again:
  2771. p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
  2772. if (!p) {
  2773. /*
  2774. * Don't fail to return the result just because we can't
  2775. * return the conflicting open:
  2776. */
  2777. if (conf->len) {
  2778. kfree(conf->data);
  2779. conf->len = 0;
  2780. conf->data = NULL;
  2781. goto again;
  2782. }
  2783. return nfserr_resource;
  2784. }
  2785. p = xdr_encode_hyper(p, ld->ld_start);
  2786. p = xdr_encode_hyper(p, ld->ld_length);
  2787. *p++ = cpu_to_be32(ld->ld_type);
  2788. if (conf->len) {
  2789. p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
  2790. p = xdr_encode_opaque(p, conf->data, conf->len);
  2791. kfree(conf->data);
  2792. } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
  2793. p = xdr_encode_hyper(p, (u64)0); /* clientid */
  2794. *p++ = cpu_to_be32(0); /* length of owner name */
  2795. }
  2796. return nfserr_denied;
  2797. }
  2798. static __be32
  2799. nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
  2800. {
  2801. struct xdr_stream *xdr = &resp->xdr;
  2802. if (!nfserr)
  2803. nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
  2804. else if (nfserr == nfserr_denied)
  2805. nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
  2806. return nfserr;
  2807. }
  2808. static __be32
  2809. nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
  2810. {
  2811. struct xdr_stream *xdr = &resp->xdr;
  2812. if (nfserr == nfserr_denied)
  2813. nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
  2814. return nfserr;
  2815. }
  2816. static __be32
  2817. nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
  2818. {
  2819. struct xdr_stream *xdr = &resp->xdr;
  2820. if (!nfserr)
  2821. nfserr = nfsd4_encode_stateid(xdr, &locku->lu_stateid);
  2822. return nfserr;
  2823. }
  2824. static __be32
  2825. nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
  2826. {
  2827. struct xdr_stream *xdr = &resp->xdr;
  2828. __be32 *p;
  2829. if (!nfserr) {
  2830. p = xdr_reserve_space(xdr, 20);
  2831. if (!p)
  2832. return nfserr_resource;
  2833. p = encode_cinfo(p, &link->li_cinfo);
  2834. }
  2835. return nfserr;
  2836. }
  2837. static __be32
  2838. nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
  2839. {
  2840. struct xdr_stream *xdr = &resp->xdr;
  2841. __be32 *p;
  2842. if (nfserr)
  2843. goto out;
  2844. nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
  2845. if (nfserr)
  2846. goto out;
  2847. p = xdr_reserve_space(xdr, 40);
  2848. if (!p)
  2849. return nfserr_resource;
  2850. p = encode_cinfo(p, &open->op_cinfo);
  2851. *p++ = cpu_to_be32(open->op_rflags);
  2852. *p++ = cpu_to_be32(2);
  2853. *p++ = cpu_to_be32(open->op_bmval[0]);
  2854. *p++ = cpu_to_be32(open->op_bmval[1]);
  2855. *p++ = cpu_to_be32(open->op_delegate_type);
  2856. switch (open->op_delegate_type) {
  2857. case NFS4_OPEN_DELEGATE_NONE:
  2858. break;
  2859. case NFS4_OPEN_DELEGATE_READ:
  2860. nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
  2861. if (nfserr)
  2862. return nfserr;
  2863. p = xdr_reserve_space(xdr, 20);
  2864. if (!p)
  2865. return nfserr_resource;
  2866. *p++ = cpu_to_be32(open->op_recall);
  2867. /*
  2868. * TODO: ACE's in delegations
  2869. */
  2870. *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2871. *p++ = cpu_to_be32(0);
  2872. *p++ = cpu_to_be32(0);
  2873. *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
  2874. break;
  2875. case NFS4_OPEN_DELEGATE_WRITE:
  2876. nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
  2877. if (nfserr)
  2878. return nfserr;
  2879. p = xdr_reserve_space(xdr, 32);
  2880. if (!p)
  2881. return nfserr_resource;
  2882. *p++ = cpu_to_be32(0);
  2883. /*
  2884. * TODO: space_limit's in delegations
  2885. */
  2886. *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
  2887. *p++ = cpu_to_be32(~(u32)0);
  2888. *p++ = cpu_to_be32(~(u32)0);
  2889. /*
  2890. * TODO: ACE's in delegations
  2891. */
  2892. *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  2893. *p++ = cpu_to_be32(0);
  2894. *p++ = cpu_to_be32(0);
  2895. *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
  2896. break;
  2897. case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
  2898. switch (open->op_why_no_deleg) {
  2899. case WND4_CONTENTION:
  2900. case WND4_RESOURCE:
  2901. p = xdr_reserve_space(xdr, 8);
  2902. if (!p)
  2903. return nfserr_resource;
  2904. *p++ = cpu_to_be32(open->op_why_no_deleg);
  2905. /* deleg signaling not supported yet: */
  2906. *p++ = cpu_to_be32(0);
  2907. break;
  2908. default:
  2909. p = xdr_reserve_space(xdr, 4);
  2910. if (!p)
  2911. return nfserr_resource;
  2912. *p++ = cpu_to_be32(open->op_why_no_deleg);
  2913. }
  2914. break;
  2915. default:
  2916. BUG();
  2917. }
  2918. /* XXX save filehandle here */
  2919. out:
  2920. return nfserr;
  2921. }
  2922. static __be32
  2923. nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
  2924. {
  2925. struct xdr_stream *xdr = &resp->xdr;
  2926. if (!nfserr)
  2927. nfserr = nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
  2928. return nfserr;
  2929. }
  2930. static __be32
  2931. nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
  2932. {
  2933. struct xdr_stream *xdr = &resp->xdr;
  2934. if (!nfserr)
  2935. nfserr = nfsd4_encode_stateid(xdr, &od->od_stateid);
  2936. return nfserr;
  2937. }
  2938. static __be32 nfsd4_encode_splice_read(
  2939. struct nfsd4_compoundres *resp,
  2940. struct nfsd4_read *read,
  2941. struct file *file, unsigned long maxcount)
  2942. {
  2943. struct xdr_stream *xdr = &resp->xdr;
  2944. struct xdr_buf *buf = xdr->buf;
  2945. u32 eof;
  2946. int space_left;
  2947. __be32 nfserr;
  2948. __be32 *p = xdr->p - 2;
  2949. /* Make sure there will be room for padding if needed */
  2950. if (xdr->end - xdr->p < 1)
  2951. return nfserr_resource;
  2952. nfserr = nfsd_splice_read(read->rd_rqstp, file,
  2953. read->rd_offset, &maxcount);
  2954. if (nfserr) {
  2955. /*
  2956. * nfsd_splice_actor may have already messed with the
  2957. * page length; reset it so as not to confuse
  2958. * xdr_truncate_encode:
  2959. */
  2960. buf->page_len = 0;
  2961. return nfserr;
  2962. }
  2963. eof = (read->rd_offset + maxcount >=
  2964. d_inode(read->rd_fhp->fh_dentry)->i_size);
  2965. *(p++) = htonl(eof);
  2966. *(p++) = htonl(maxcount);
  2967. buf->page_len = maxcount;
  2968. buf->len += maxcount;
  2969. xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
  2970. / PAGE_SIZE;
  2971. /* Use rest of head for padding and remaining ops: */
  2972. buf->tail[0].iov_base = xdr->p;
  2973. buf->tail[0].iov_len = 0;
  2974. xdr->iov = buf->tail;
  2975. if (maxcount&3) {
  2976. int pad = 4 - (maxcount&3);
  2977. *(xdr->p++) = 0;
  2978. buf->tail[0].iov_base += maxcount&3;
  2979. buf->tail[0].iov_len = pad;
  2980. buf->len += pad;
  2981. }
  2982. space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
  2983. buf->buflen - buf->len);
  2984. buf->buflen = buf->len + space_left;
  2985. xdr->end = (__be32 *)((void *)xdr->end + space_left);
  2986. return 0;
  2987. }
  2988. static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
  2989. struct nfsd4_read *read,
  2990. struct file *file, unsigned long maxcount)
  2991. {
  2992. struct xdr_stream *xdr = &resp->xdr;
  2993. u32 eof;
  2994. int v;
  2995. int starting_len = xdr->buf->len - 8;
  2996. long len;
  2997. int thislen;
  2998. __be32 nfserr;
  2999. __be32 tmp;
  3000. __be32 *p;
  3001. u32 zzz = 0;
  3002. int pad;
  3003. len = maxcount;
  3004. v = 0;
  3005. thislen = min_t(long, len, ((void *)xdr->end - (void *)xdr->p));
  3006. p = xdr_reserve_space(xdr, (thislen+3)&~3);
  3007. WARN_ON_ONCE(!p);
  3008. resp->rqstp->rq_vec[v].iov_base = p;
  3009. resp->rqstp->rq_vec[v].iov_len = thislen;
  3010. v++;
  3011. len -= thislen;
  3012. while (len) {
  3013. thislen = min_t(long, len, PAGE_SIZE);
  3014. p = xdr_reserve_space(xdr, (thislen+3)&~3);
  3015. WARN_ON_ONCE(!p);
  3016. resp->rqstp->rq_vec[v].iov_base = p;
  3017. resp->rqstp->rq_vec[v].iov_len = thislen;
  3018. v++;
  3019. len -= thislen;
  3020. }
  3021. read->rd_vlen = v;
  3022. nfserr = nfsd_readv(file, read->rd_offset, resp->rqstp->rq_vec,
  3023. read->rd_vlen, &maxcount);
  3024. if (nfserr)
  3025. return nfserr;
  3026. xdr_truncate_encode(xdr, starting_len + 8 + ((maxcount+3)&~3));
  3027. eof = (read->rd_offset + maxcount >=
  3028. d_inode(read->rd_fhp->fh_dentry)->i_size);
  3029. tmp = htonl(eof);
  3030. write_bytes_to_xdr_buf(xdr->buf, starting_len , &tmp, 4);
  3031. tmp = htonl(maxcount);
  3032. write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
  3033. pad = (maxcount&3) ? 4 - (maxcount&3) : 0;
  3034. write_bytes_to_xdr_buf(xdr->buf, starting_len + 8 + maxcount,
  3035. &zzz, pad);
  3036. return 0;
  3037. }
  3038. static __be32
  3039. nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
  3040. struct nfsd4_read *read)
  3041. {
  3042. unsigned long maxcount;
  3043. struct xdr_stream *xdr = &resp->xdr;
  3044. struct file *file = read->rd_filp;
  3045. struct svc_fh *fhp = read->rd_fhp;
  3046. int starting_len = xdr->buf->len;
  3047. struct raparms *ra;
  3048. __be32 *p;
  3049. __be32 err;
  3050. if (nfserr)
  3051. return nfserr;
  3052. p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
  3053. if (!p) {
  3054. WARN_ON_ONCE(test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags));
  3055. return nfserr_resource;
  3056. }
  3057. if (resp->xdr.buf->page_len && test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags)) {
  3058. WARN_ON_ONCE(1);
  3059. return nfserr_resource;
  3060. }
  3061. xdr_commit_encode(xdr);
  3062. maxcount = svc_max_payload(resp->rqstp);
  3063. maxcount = min_t(unsigned long, maxcount, (xdr->buf->buflen - xdr->buf->len));
  3064. maxcount = min_t(unsigned long, maxcount, read->rd_length);
  3065. if (read->rd_filp)
  3066. err = nfsd_permission(resp->rqstp, fhp->fh_export,
  3067. fhp->fh_dentry,
  3068. NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
  3069. else
  3070. err = nfsd_get_tmp_read_open(resp->rqstp, read->rd_fhp,
  3071. &file, &ra);
  3072. if (err)
  3073. goto err_truncate;
  3074. if (file->f_op->splice_read && test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags))
  3075. err = nfsd4_encode_splice_read(resp, read, file, maxcount);
  3076. else
  3077. err = nfsd4_encode_readv(resp, read, file, maxcount);
  3078. if (!read->rd_filp)
  3079. nfsd_put_tmp_read_open(file, ra);
  3080. err_truncate:
  3081. if (err)
  3082. xdr_truncate_encode(xdr, starting_len);
  3083. return err;
  3084. }
  3085. static __be32
  3086. nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
  3087. {
  3088. int maxcount;
  3089. __be32 wire_count;
  3090. int zero = 0;
  3091. struct xdr_stream *xdr = &resp->xdr;
  3092. int length_offset = xdr->buf->len;
  3093. __be32 *p;
  3094. if (nfserr)
  3095. return nfserr;
  3096. p = xdr_reserve_space(xdr, 4);
  3097. if (!p)
  3098. return nfserr_resource;
  3099. maxcount = PAGE_SIZE;
  3100. p = xdr_reserve_space(xdr, maxcount);
  3101. if (!p)
  3102. return nfserr_resource;
  3103. /*
  3104. * XXX: By default, the ->readlink() VFS op will truncate symlinks
  3105. * if they would overflow the buffer. Is this kosher in NFSv4? If
  3106. * not, one easy fix is: if ->readlink() precisely fills the buffer,
  3107. * assume that truncation occurred, and return NFS4ERR_RESOURCE.
  3108. */
  3109. nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
  3110. (char *)p, &maxcount);
  3111. if (nfserr == nfserr_isdir)
  3112. nfserr = nfserr_inval;
  3113. if (nfserr) {
  3114. xdr_truncate_encode(xdr, length_offset);
  3115. return nfserr;
  3116. }
  3117. wire_count = htonl(maxcount);
  3118. write_bytes_to_xdr_buf(xdr->buf, length_offset, &wire_count, 4);
  3119. xdr_truncate_encode(xdr, length_offset + 4 + ALIGN(maxcount, 4));
  3120. if (maxcount & 3)
  3121. write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount,
  3122. &zero, 4 - (maxcount&3));
  3123. return 0;
  3124. }
  3125. static __be32
  3126. nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
  3127. {
  3128. int maxcount;
  3129. int bytes_left;
  3130. loff_t offset;
  3131. __be64 wire_offset;
  3132. struct xdr_stream *xdr = &resp->xdr;
  3133. int starting_len = xdr->buf->len;
  3134. __be32 *p;
  3135. if (nfserr)
  3136. return nfserr;
  3137. p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
  3138. if (!p)
  3139. return nfserr_resource;
  3140. /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
  3141. *p++ = cpu_to_be32(0);
  3142. *p++ = cpu_to_be32(0);
  3143. resp->xdr.buf->head[0].iov_len = ((char *)resp->xdr.p)
  3144. - (char *)resp->xdr.buf->head[0].iov_base;
  3145. /*
  3146. * Number of bytes left for directory entries allowing for the
  3147. * final 8 bytes of the readdir and a following failed op:
  3148. */
  3149. bytes_left = xdr->buf->buflen - xdr->buf->len
  3150. - COMPOUND_ERR_SLACK_SPACE - 8;
  3151. if (bytes_left < 0) {
  3152. nfserr = nfserr_resource;
  3153. goto err_no_verf;
  3154. }
  3155. maxcount = min_t(u32, readdir->rd_maxcount, INT_MAX);
  3156. /*
  3157. * Note the rfc defines rd_maxcount as the size of the
  3158. * READDIR4resok structure, which includes the verifier above
  3159. * and the 8 bytes encoded at the end of this function:
  3160. */
  3161. if (maxcount < 16) {
  3162. nfserr = nfserr_toosmall;
  3163. goto err_no_verf;
  3164. }
  3165. maxcount = min_t(int, maxcount-16, bytes_left);
  3166. /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
  3167. if (!readdir->rd_dircount)
  3168. readdir->rd_dircount = INT_MAX;
  3169. readdir->xdr = xdr;
  3170. readdir->rd_maxcount = maxcount;
  3171. readdir->common.err = 0;
  3172. readdir->cookie_offset = 0;
  3173. offset = readdir->rd_cookie;
  3174. nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
  3175. &offset,
  3176. &readdir->common, nfsd4_encode_dirent);
  3177. if (nfserr == nfs_ok &&
  3178. readdir->common.err == nfserr_toosmall &&
  3179. xdr->buf->len == starting_len + 8) {
  3180. /* nothing encoded; which limit did we hit?: */
  3181. if (maxcount - 16 < bytes_left)
  3182. /* It was the fault of rd_maxcount: */
  3183. nfserr = nfserr_toosmall;
  3184. else
  3185. /* We ran out of buffer space: */
  3186. nfserr = nfserr_resource;
  3187. }
  3188. if (nfserr)
  3189. goto err_no_verf;
  3190. if (readdir->cookie_offset) {
  3191. wire_offset = cpu_to_be64(offset);
  3192. write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
  3193. &wire_offset, 8);
  3194. }
  3195. p = xdr_reserve_space(xdr, 8);
  3196. if (!p) {
  3197. WARN_ON_ONCE(1);
  3198. goto err_no_verf;
  3199. }
  3200. *p++ = 0; /* no more entries */
  3201. *p++ = htonl(readdir->common.err == nfserr_eof);
  3202. return 0;
  3203. err_no_verf:
  3204. xdr_truncate_encode(xdr, starting_len);
  3205. return nfserr;
  3206. }
  3207. static __be32
  3208. nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
  3209. {
  3210. struct xdr_stream *xdr = &resp->xdr;
  3211. __be32 *p;
  3212. if (!nfserr) {
  3213. p = xdr_reserve_space(xdr, 20);
  3214. if (!p)
  3215. return nfserr_resource;
  3216. p = encode_cinfo(p, &remove->rm_cinfo);
  3217. }
  3218. return nfserr;
  3219. }
  3220. static __be32
  3221. nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
  3222. {
  3223. struct xdr_stream *xdr = &resp->xdr;
  3224. __be32 *p;
  3225. if (!nfserr) {
  3226. p = xdr_reserve_space(xdr, 40);
  3227. if (!p)
  3228. return nfserr_resource;
  3229. p = encode_cinfo(p, &rename->rn_sinfo);
  3230. p = encode_cinfo(p, &rename->rn_tinfo);
  3231. }
  3232. return nfserr;
  3233. }
  3234. static __be32
  3235. nfsd4_do_encode_secinfo(struct xdr_stream *xdr,
  3236. __be32 nfserr, struct svc_export *exp)
  3237. {
  3238. u32 i, nflavs, supported;
  3239. struct exp_flavor_info *flavs;
  3240. struct exp_flavor_info def_flavs[2];
  3241. __be32 *p, *flavorsp;
  3242. static bool report = true;
  3243. if (nfserr)
  3244. goto out;
  3245. nfserr = nfserr_resource;
  3246. if (exp->ex_nflavors) {
  3247. flavs = exp->ex_flavors;
  3248. nflavs = exp->ex_nflavors;
  3249. } else { /* Handling of some defaults in absence of real secinfo: */
  3250. flavs = def_flavs;
  3251. if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
  3252. nflavs = 2;
  3253. flavs[0].pseudoflavor = RPC_AUTH_UNIX;
  3254. flavs[1].pseudoflavor = RPC_AUTH_NULL;
  3255. } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
  3256. nflavs = 1;
  3257. flavs[0].pseudoflavor
  3258. = svcauth_gss_flavor(exp->ex_client);
  3259. } else {
  3260. nflavs = 1;
  3261. flavs[0].pseudoflavor
  3262. = exp->ex_client->flavour->flavour;
  3263. }
  3264. }
  3265. supported = 0;
  3266. p = xdr_reserve_space(xdr, 4);
  3267. if (!p)
  3268. goto out;
  3269. flavorsp = p++; /* to be backfilled later */
  3270. for (i = 0; i < nflavs; i++) {
  3271. rpc_authflavor_t pf = flavs[i].pseudoflavor;
  3272. struct rpcsec_gss_info info;
  3273. if (rpcauth_get_gssinfo(pf, &info) == 0) {
  3274. supported++;
  3275. p = xdr_reserve_space(xdr, 4 + 4 +
  3276. XDR_LEN(info.oid.len) + 4 + 4);
  3277. if (!p)
  3278. goto out;
  3279. *p++ = cpu_to_be32(RPC_AUTH_GSS);
  3280. p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
  3281. *p++ = cpu_to_be32(info.qop);
  3282. *p++ = cpu_to_be32(info.service);
  3283. } else if (pf < RPC_AUTH_MAXFLAVOR) {
  3284. supported++;
  3285. p = xdr_reserve_space(xdr, 4);
  3286. if (!p)
  3287. goto out;
  3288. *p++ = cpu_to_be32(pf);
  3289. } else {
  3290. if (report)
  3291. pr_warn("NFS: SECINFO: security flavor %u "
  3292. "is not supported\n", pf);
  3293. }
  3294. }
  3295. if (nflavs != supported)
  3296. report = false;
  3297. *flavorsp = htonl(supported);
  3298. nfserr = 0;
  3299. out:
  3300. if (exp)
  3301. exp_put(exp);
  3302. return nfserr;
  3303. }
  3304. static __be32
  3305. nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  3306. struct nfsd4_secinfo *secinfo)
  3307. {
  3308. struct xdr_stream *xdr = &resp->xdr;
  3309. return nfsd4_do_encode_secinfo(xdr, nfserr, secinfo->si_exp);
  3310. }
  3311. static __be32
  3312. nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
  3313. struct nfsd4_secinfo_no_name *secinfo)
  3314. {
  3315. struct xdr_stream *xdr = &resp->xdr;
  3316. return nfsd4_do_encode_secinfo(xdr, nfserr, secinfo->sin_exp);
  3317. }
  3318. /*
  3319. * The SETATTR encode routine is special -- it always encodes a bitmap,
  3320. * regardless of the error status.
  3321. */
  3322. static __be32
  3323. nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
  3324. {
  3325. struct xdr_stream *xdr = &resp->xdr;
  3326. __be32 *p;
  3327. p = xdr_reserve_space(xdr, 16);
  3328. if (!p)
  3329. return nfserr_resource;
  3330. if (nfserr) {
  3331. *p++ = cpu_to_be32(3);
  3332. *p++ = cpu_to_be32(0);
  3333. *p++ = cpu_to_be32(0);
  3334. *p++ = cpu_to_be32(0);
  3335. }
  3336. else {
  3337. *p++ = cpu_to_be32(3);
  3338. *p++ = cpu_to_be32(setattr->sa_bmval[0]);
  3339. *p++ = cpu_to_be32(setattr->sa_bmval[1]);
  3340. *p++ = cpu_to_be32(setattr->sa_bmval[2]);
  3341. }
  3342. return nfserr;
  3343. }
  3344. static __be32
  3345. nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
  3346. {
  3347. struct xdr_stream *xdr = &resp->xdr;
  3348. __be32 *p;
  3349. if (!nfserr) {
  3350. p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
  3351. if (!p)
  3352. return nfserr_resource;
  3353. p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
  3354. p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
  3355. NFS4_VERIFIER_SIZE);
  3356. }
  3357. else if (nfserr == nfserr_clid_inuse) {
  3358. p = xdr_reserve_space(xdr, 8);
  3359. if (!p)
  3360. return nfserr_resource;
  3361. *p++ = cpu_to_be32(0);
  3362. *p++ = cpu_to_be32(0);
  3363. }
  3364. return nfserr;
  3365. }
  3366. static __be32
  3367. nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
  3368. {
  3369. struct xdr_stream *xdr = &resp->xdr;
  3370. __be32 *p;
  3371. if (!nfserr) {
  3372. p = xdr_reserve_space(xdr, 16);
  3373. if (!p)
  3374. return nfserr_resource;
  3375. *p++ = cpu_to_be32(write->wr_bytes_written);
  3376. *p++ = cpu_to_be32(write->wr_how_written);
  3377. p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
  3378. NFS4_VERIFIER_SIZE);
  3379. }
  3380. return nfserr;
  3381. }
  3382. static const u32 nfs4_minimal_spo_must_enforce[2] = {
  3383. [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  3384. 1 << (OP_EXCHANGE_ID - 32) |
  3385. 1 << (OP_CREATE_SESSION - 32) |
  3386. 1 << (OP_DESTROY_SESSION - 32) |
  3387. 1 << (OP_DESTROY_CLIENTID - 32)
  3388. };
  3389. static __be32
  3390. nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
  3391. struct nfsd4_exchange_id *exid)
  3392. {
  3393. struct xdr_stream *xdr = &resp->xdr;
  3394. __be32 *p;
  3395. char *major_id;
  3396. char *server_scope;
  3397. int major_id_sz;
  3398. int server_scope_sz;
  3399. uint64_t minor_id = 0;
  3400. if (nfserr)
  3401. return nfserr;
  3402. major_id = utsname()->nodename;
  3403. major_id_sz = strlen(major_id);
  3404. server_scope = utsname()->nodename;
  3405. server_scope_sz = strlen(server_scope);
  3406. p = xdr_reserve_space(xdr,
  3407. 8 /* eir_clientid */ +
  3408. 4 /* eir_sequenceid */ +
  3409. 4 /* eir_flags */ +
  3410. 4 /* spr_how */);
  3411. if (!p)
  3412. return nfserr_resource;
  3413. p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
  3414. *p++ = cpu_to_be32(exid->seqid);
  3415. *p++ = cpu_to_be32(exid->flags);
  3416. *p++ = cpu_to_be32(exid->spa_how);
  3417. switch (exid->spa_how) {
  3418. case SP4_NONE:
  3419. break;
  3420. case SP4_MACH_CRED:
  3421. /* spo_must_enforce, spo_must_allow */
  3422. p = xdr_reserve_space(xdr, 16);
  3423. if (!p)
  3424. return nfserr_resource;
  3425. /* spo_must_enforce bitmap: */
  3426. *p++ = cpu_to_be32(2);
  3427. *p++ = cpu_to_be32(nfs4_minimal_spo_must_enforce[0]);
  3428. *p++ = cpu_to_be32(nfs4_minimal_spo_must_enforce[1]);
  3429. /* empty spo_must_allow bitmap: */
  3430. *p++ = cpu_to_be32(0);
  3431. break;
  3432. default:
  3433. WARN_ON_ONCE(1);
  3434. }
  3435. p = xdr_reserve_space(xdr,
  3436. 8 /* so_minor_id */ +
  3437. 4 /* so_major_id.len */ +
  3438. (XDR_QUADLEN(major_id_sz) * 4) +
  3439. 4 /* eir_server_scope.len */ +
  3440. (XDR_QUADLEN(server_scope_sz) * 4) +
  3441. 4 /* eir_server_impl_id.count (0) */);
  3442. if (!p)
  3443. return nfserr_resource;
  3444. /* The server_owner struct */
  3445. p = xdr_encode_hyper(p, minor_id); /* Minor id */
  3446. /* major id */
  3447. p = xdr_encode_opaque(p, major_id, major_id_sz);
  3448. /* Server scope */
  3449. p = xdr_encode_opaque(p, server_scope, server_scope_sz);
  3450. /* Implementation id */
  3451. *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
  3452. return 0;
  3453. }
  3454. static __be32
  3455. nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
  3456. struct nfsd4_create_session *sess)
  3457. {
  3458. struct xdr_stream *xdr = &resp->xdr;
  3459. __be32 *p;
  3460. if (nfserr)
  3461. return nfserr;
  3462. p = xdr_reserve_space(xdr, 24);
  3463. if (!p)
  3464. return nfserr_resource;
  3465. p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
  3466. NFS4_MAX_SESSIONID_LEN);
  3467. *p++ = cpu_to_be32(sess->seqid);
  3468. *p++ = cpu_to_be32(sess->flags);
  3469. p = xdr_reserve_space(xdr, 28);
  3470. if (!p)
  3471. return nfserr_resource;
  3472. *p++ = cpu_to_be32(0); /* headerpadsz */
  3473. *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
  3474. *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
  3475. *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
  3476. *p++ = cpu_to_be32(sess->fore_channel.maxops);
  3477. *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
  3478. *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
  3479. if (sess->fore_channel.nr_rdma_attrs) {
  3480. p = xdr_reserve_space(xdr, 4);
  3481. if (!p)
  3482. return nfserr_resource;
  3483. *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
  3484. }
  3485. p = xdr_reserve_space(xdr, 28);
  3486. if (!p)
  3487. return nfserr_resource;
  3488. *p++ = cpu_to_be32(0); /* headerpadsz */
  3489. *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
  3490. *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
  3491. *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
  3492. *p++ = cpu_to_be32(sess->back_channel.maxops);
  3493. *p++ = cpu_to_be32(sess->back_channel.maxreqs);
  3494. *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
  3495. if (sess->back_channel.nr_rdma_attrs) {
  3496. p = xdr_reserve_space(xdr, 4);
  3497. if (!p)
  3498. return nfserr_resource;
  3499. *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
  3500. }
  3501. return 0;
  3502. }
  3503. static __be32
  3504. nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
  3505. struct nfsd4_sequence *seq)
  3506. {
  3507. struct xdr_stream *xdr = &resp->xdr;
  3508. __be32 *p;
  3509. if (nfserr)
  3510. return nfserr;
  3511. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
  3512. if (!p)
  3513. return nfserr_resource;
  3514. p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
  3515. NFS4_MAX_SESSIONID_LEN);
  3516. *p++ = cpu_to_be32(seq->seqid);
  3517. *p++ = cpu_to_be32(seq->slotid);
  3518. /* Note slotid's are numbered from zero: */
  3519. *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
  3520. *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
  3521. *p++ = cpu_to_be32(seq->status_flags);
  3522. resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
  3523. return 0;
  3524. }
  3525. static __be32
  3526. nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
  3527. struct nfsd4_test_stateid *test_stateid)
  3528. {
  3529. struct xdr_stream *xdr = &resp->xdr;
  3530. struct nfsd4_test_stateid_id *stateid, *next;
  3531. __be32 *p;
  3532. if (nfserr)
  3533. return nfserr;
  3534. p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
  3535. if (!p)
  3536. return nfserr_resource;
  3537. *p++ = htonl(test_stateid->ts_num_ids);
  3538. list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
  3539. *p++ = stateid->ts_id_status;
  3540. }
  3541. return nfserr;
  3542. }
  3543. #ifdef CONFIG_NFSD_PNFS
  3544. static __be32
  3545. nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  3546. struct nfsd4_getdeviceinfo *gdev)
  3547. {
  3548. struct xdr_stream *xdr = &resp->xdr;
  3549. const struct nfsd4_layout_ops *ops =
  3550. nfsd4_layout_ops[gdev->gd_layout_type];
  3551. u32 starting_len = xdr->buf->len, needed_len;
  3552. __be32 *p;
  3553. dprintk("%s: err %d\n", __func__, nfserr);
  3554. if (nfserr)
  3555. goto out;
  3556. nfserr = nfserr_resource;
  3557. p = xdr_reserve_space(xdr, 4);
  3558. if (!p)
  3559. goto out;
  3560. *p++ = cpu_to_be32(gdev->gd_layout_type);
  3561. /* If maxcount is 0 then just update notifications */
  3562. if (gdev->gd_maxcount != 0) {
  3563. nfserr = ops->encode_getdeviceinfo(xdr, gdev);
  3564. if (nfserr) {
  3565. /*
  3566. * We don't bother to burden the layout drivers with
  3567. * enforcing gd_maxcount, just tell the client to
  3568. * come back with a bigger buffer if it's not enough.
  3569. */
  3570. if (xdr->buf->len + 4 > gdev->gd_maxcount)
  3571. goto toosmall;
  3572. goto out;
  3573. }
  3574. }
  3575. nfserr = nfserr_resource;
  3576. if (gdev->gd_notify_types) {
  3577. p = xdr_reserve_space(xdr, 4 + 4);
  3578. if (!p)
  3579. goto out;
  3580. *p++ = cpu_to_be32(1); /* bitmap length */
  3581. *p++ = cpu_to_be32(gdev->gd_notify_types);
  3582. } else {
  3583. p = xdr_reserve_space(xdr, 4);
  3584. if (!p)
  3585. goto out;
  3586. *p++ = 0;
  3587. }
  3588. nfserr = 0;
  3589. out:
  3590. kfree(gdev->gd_device);
  3591. dprintk("%s: done: %d\n", __func__, be32_to_cpu(nfserr));
  3592. return nfserr;
  3593. toosmall:
  3594. dprintk("%s: maxcount too small\n", __func__);
  3595. needed_len = xdr->buf->len + 4 /* notifications */;
  3596. xdr_truncate_encode(xdr, starting_len);
  3597. p = xdr_reserve_space(xdr, 4);
  3598. if (!p) {
  3599. nfserr = nfserr_resource;
  3600. } else {
  3601. *p++ = cpu_to_be32(needed_len);
  3602. nfserr = nfserr_toosmall;
  3603. }
  3604. goto out;
  3605. }
  3606. static __be32
  3607. nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
  3608. struct nfsd4_layoutget *lgp)
  3609. {
  3610. struct xdr_stream *xdr = &resp->xdr;
  3611. const struct nfsd4_layout_ops *ops =
  3612. nfsd4_layout_ops[lgp->lg_layout_type];
  3613. __be32 *p;
  3614. dprintk("%s: err %d\n", __func__, nfserr);
  3615. if (nfserr)
  3616. goto out;
  3617. nfserr = nfserr_resource;
  3618. p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
  3619. if (!p)
  3620. goto out;
  3621. *p++ = cpu_to_be32(1); /* we always set return-on-close */
  3622. *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
  3623. p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
  3624. sizeof(stateid_opaque_t));
  3625. *p++ = cpu_to_be32(1); /* we always return a single layout */
  3626. p = xdr_encode_hyper(p, lgp->lg_seg.offset);
  3627. p = xdr_encode_hyper(p, lgp->lg_seg.length);
  3628. *p++ = cpu_to_be32(lgp->lg_seg.iomode);
  3629. *p++ = cpu_to_be32(lgp->lg_layout_type);
  3630. nfserr = ops->encode_layoutget(xdr, lgp);
  3631. out:
  3632. kfree(lgp->lg_content);
  3633. return nfserr;
  3634. }
  3635. static __be32
  3636. nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
  3637. struct nfsd4_layoutcommit *lcp)
  3638. {
  3639. struct xdr_stream *xdr = &resp->xdr;
  3640. __be32 *p;
  3641. if (nfserr)
  3642. return nfserr;
  3643. p = xdr_reserve_space(xdr, 4);
  3644. if (!p)
  3645. return nfserr_resource;
  3646. *p++ = cpu_to_be32(lcp->lc_size_chg);
  3647. if (lcp->lc_size_chg) {
  3648. p = xdr_reserve_space(xdr, 8);
  3649. if (!p)
  3650. return nfserr_resource;
  3651. p = xdr_encode_hyper(p, lcp->lc_newsize);
  3652. }
  3653. return nfs_ok;
  3654. }
  3655. static __be32
  3656. nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
  3657. struct nfsd4_layoutreturn *lrp)
  3658. {
  3659. struct xdr_stream *xdr = &resp->xdr;
  3660. __be32 *p;
  3661. if (nfserr)
  3662. return nfserr;
  3663. p = xdr_reserve_space(xdr, 4);
  3664. if (!p)
  3665. return nfserr_resource;
  3666. *p++ = cpu_to_be32(lrp->lrs_present);
  3667. if (lrp->lrs_present)
  3668. return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
  3669. return nfs_ok;
  3670. }
  3671. #endif /* CONFIG_NFSD_PNFS */
  3672. static __be32
  3673. nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
  3674. struct nfsd4_seek *seek)
  3675. {
  3676. __be32 *p;
  3677. if (nfserr)
  3678. return nfserr;
  3679. p = xdr_reserve_space(&resp->xdr, 4 + 8);
  3680. *p++ = cpu_to_be32(seek->seek_eof);
  3681. p = xdr_encode_hyper(p, seek->seek_pos);
  3682. return nfserr;
  3683. }
  3684. static __be32
  3685. nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
  3686. {
  3687. return nfserr;
  3688. }
  3689. typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
  3690. /*
  3691. * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
  3692. * since we don't need to filter out obsolete ops as this is
  3693. * done in the decoding phase.
  3694. */
  3695. static nfsd4_enc nfsd4_enc_ops[] = {
  3696. [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
  3697. [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
  3698. [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
  3699. [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
  3700. [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
  3701. [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  3702. [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
  3703. [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
  3704. [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
  3705. [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
  3706. [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
  3707. [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
  3708. [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
  3709. [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
  3710. [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3711. [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
  3712. [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
  3713. [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
  3714. [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
  3715. [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
  3716. [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
  3717. [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
  3718. [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
  3719. [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
  3720. [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
  3721. [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
  3722. [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
  3723. [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
  3724. [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
  3725. [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
  3726. [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
  3727. [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
  3728. [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
  3729. [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
  3730. [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3731. [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
  3732. [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
  3733. /* NFSv4.1 operations */
  3734. [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
  3735. [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
  3736. [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
  3737. [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
  3738. [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
  3739. [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
  3740. [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  3741. #ifdef CONFIG_NFSD_PNFS
  3742. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
  3743. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  3744. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
  3745. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
  3746. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
  3747. #else
  3748. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
  3749. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  3750. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
  3751. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
  3752. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  3753. #endif
  3754. [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
  3755. [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
  3756. [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
  3757. [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
  3758. [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  3759. [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
  3760. [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
  3761. /* NFSv4.2 operations */
  3762. [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
  3763. [OP_COPY] = (nfsd4_enc)nfsd4_encode_noop,
  3764. [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_noop,
  3765. [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
  3766. [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
  3767. [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
  3768. [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
  3769. [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
  3770. [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_noop,
  3771. [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_noop,
  3772. [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
  3773. [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
  3774. };
  3775. /*
  3776. * Calculate whether we still have space to encode repsize bytes.
  3777. * There are two considerations:
  3778. * - For NFS versions >=4.1, the size of the reply must stay within
  3779. * session limits
  3780. * - For all NFS versions, we must stay within limited preallocated
  3781. * buffer space.
  3782. *
  3783. * This is called before the operation is processed, so can only provide
  3784. * an upper estimate. For some nonidempotent operations (such as
  3785. * getattr), it's not necessarily a problem if that estimate is wrong,
  3786. * as we can fail it after processing without significant side effects.
  3787. */
  3788. __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
  3789. {
  3790. struct xdr_buf *buf = &resp->rqstp->rq_res;
  3791. struct nfsd4_slot *slot = resp->cstate.slot;
  3792. if (buf->len + respsize <= buf->buflen)
  3793. return nfs_ok;
  3794. if (!nfsd4_has_session(&resp->cstate))
  3795. return nfserr_resource;
  3796. if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
  3797. WARN_ON_ONCE(1);
  3798. return nfserr_rep_too_big_to_cache;
  3799. }
  3800. return nfserr_rep_too_big;
  3801. }
  3802. void
  3803. nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  3804. {
  3805. struct xdr_stream *xdr = &resp->xdr;
  3806. struct nfs4_stateowner *so = resp->cstate.replay_owner;
  3807. struct svc_rqst *rqstp = resp->rqstp;
  3808. int post_err_offset;
  3809. nfsd4_enc encoder;
  3810. __be32 *p;
  3811. p = xdr_reserve_space(xdr, 8);
  3812. if (!p) {
  3813. WARN_ON_ONCE(1);
  3814. return;
  3815. }
  3816. *p++ = cpu_to_be32(op->opnum);
  3817. post_err_offset = xdr->buf->len;
  3818. if (op->opnum == OP_ILLEGAL)
  3819. goto status;
  3820. BUG_ON(op->opnum < 0 || op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
  3821. !nfsd4_enc_ops[op->opnum]);
  3822. encoder = nfsd4_enc_ops[op->opnum];
  3823. op->status = encoder(resp, op->status, &op->u);
  3824. xdr_commit_encode(xdr);
  3825. /* nfsd4_check_resp_size guarantees enough room for error status */
  3826. if (!op->status) {
  3827. int space_needed = 0;
  3828. if (!nfsd4_last_compound_op(rqstp))
  3829. space_needed = COMPOUND_ERR_SLACK_SPACE;
  3830. op->status = nfsd4_check_resp_size(resp, space_needed);
  3831. }
  3832. if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
  3833. struct nfsd4_slot *slot = resp->cstate.slot;
  3834. if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
  3835. op->status = nfserr_rep_too_big_to_cache;
  3836. else
  3837. op->status = nfserr_rep_too_big;
  3838. }
  3839. if (op->status == nfserr_resource ||
  3840. op->status == nfserr_rep_too_big ||
  3841. op->status == nfserr_rep_too_big_to_cache) {
  3842. /*
  3843. * The operation may have already been encoded or
  3844. * partially encoded. No op returns anything additional
  3845. * in the case of one of these three errors, so we can
  3846. * just truncate back to after the status. But it's a
  3847. * bug if we had to do this on a non-idempotent op:
  3848. */
  3849. warn_on_nonidempotent_op(op);
  3850. xdr_truncate_encode(xdr, post_err_offset);
  3851. }
  3852. if (so) {
  3853. int len = xdr->buf->len - post_err_offset;
  3854. so->so_replay.rp_status = op->status;
  3855. so->so_replay.rp_buflen = len;
  3856. read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
  3857. so->so_replay.rp_buf, len);
  3858. }
  3859. status:
  3860. /* Note that op->status is already in network byte order: */
  3861. write_bytes_to_xdr_buf(xdr->buf, post_err_offset - 4, &op->status, 4);
  3862. }
  3863. /*
  3864. * Encode the reply stored in the stateowner reply cache
  3865. *
  3866. * XDR note: do not encode rp->rp_buflen: the buffer contains the
  3867. * previously sent already encoded operation.
  3868. */
  3869. void
  3870. nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
  3871. {
  3872. __be32 *p;
  3873. struct nfs4_replay *rp = op->replay;
  3874. BUG_ON(!rp);
  3875. p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
  3876. if (!p) {
  3877. WARN_ON_ONCE(1);
  3878. return;
  3879. }
  3880. *p++ = cpu_to_be32(op->opnum);
  3881. *p++ = rp->rp_status; /* already xdr'ed */
  3882. p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
  3883. }
  3884. int
  3885. nfs4svc_encode_voidres(struct svc_rqst *rqstp, __be32 *p, void *dummy)
  3886. {
  3887. return xdr_ressize_check(rqstp, p);
  3888. }
  3889. int nfsd4_release_compoundargs(void *rq, __be32 *p, void *resp)
  3890. {
  3891. struct svc_rqst *rqstp = rq;
  3892. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  3893. if (args->ops != args->iops) {
  3894. kfree(args->ops);
  3895. args->ops = args->iops;
  3896. }
  3897. kfree(args->tmpp);
  3898. args->tmpp = NULL;
  3899. while (args->to_free) {
  3900. struct svcxdr_tmpbuf *tb = args->to_free;
  3901. args->to_free = tb->next;
  3902. kfree(tb);
  3903. }
  3904. return 1;
  3905. }
  3906. int
  3907. nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundargs *args)
  3908. {
  3909. if (rqstp->rq_arg.head[0].iov_len % 4) {
  3910. /* client is nuts */
  3911. dprintk("%s: compound not properly padded! (peeraddr=%pISc xid=0x%x)",
  3912. __func__, svc_addr(rqstp), be32_to_cpu(rqstp->rq_xid));
  3913. return 0;
  3914. }
  3915. args->p = p;
  3916. args->end = rqstp->rq_arg.head[0].iov_base + rqstp->rq_arg.head[0].iov_len;
  3917. args->pagelist = rqstp->rq_arg.pages;
  3918. args->pagelen = rqstp->rq_arg.page_len;
  3919. args->tmpp = NULL;
  3920. args->to_free = NULL;
  3921. args->ops = args->iops;
  3922. args->rqstp = rqstp;
  3923. return !nfsd4_decode_compound(args);
  3924. }
  3925. int
  3926. nfs4svc_encode_compoundres(struct svc_rqst *rqstp, __be32 *p, struct nfsd4_compoundres *resp)
  3927. {
  3928. /*
  3929. * All that remains is to write the tag and operation count...
  3930. */
  3931. struct xdr_buf *buf = resp->xdr.buf;
  3932. WARN_ON_ONCE(buf->len != buf->head[0].iov_len + buf->page_len +
  3933. buf->tail[0].iov_len);
  3934. rqstp->rq_next_page = resp->xdr.page_ptr + 1;
  3935. p = resp->tagp;
  3936. *p++ = htonl(resp->taglen);
  3937. memcpy(p, resp->tag, resp->taglen);
  3938. p += XDR_QUADLEN(resp->taglen);
  3939. *p++ = htonl(resp->opcnt);
  3940. nfsd4_sequence_done(resp);
  3941. return 1;
  3942. }
  3943. /*
  3944. * Local variables:
  3945. * c-basic-offset: 8
  3946. * End:
  3947. */