nfs4xdr.c 114 KB

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