nfs4xdr.c 141 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490
  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 <[email protected]>
  8. * Andy Adamson <[email protected]>
  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 <linux/sunrpc/addr.h>
  43. #include <linux/xattr.h>
  44. #include <linux/vmalloc.h>
  45. #include <uapi/linux/xattr.h>
  46. #include "idmap.h"
  47. #include "acl.h"
  48. #include "xdr4.h"
  49. #include "vfs.h"
  50. #include "state.h"
  51. #include "cache.h"
  52. #include "netns.h"
  53. #include "pnfs.h"
  54. #include "filecache.h"
  55. #include "trace.h"
  56. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  57. #include <linux/security.h>
  58. #endif
  59. #define NFSDDBG_FACILITY NFSDDBG_XDR
  60. const u32 nfsd_suppattrs[3][3] = {
  61. {NFSD4_SUPPORTED_ATTRS_WORD0,
  62. NFSD4_SUPPORTED_ATTRS_WORD1,
  63. NFSD4_SUPPORTED_ATTRS_WORD2},
  64. {NFSD4_1_SUPPORTED_ATTRS_WORD0,
  65. NFSD4_1_SUPPORTED_ATTRS_WORD1,
  66. NFSD4_1_SUPPORTED_ATTRS_WORD2},
  67. {NFSD4_1_SUPPORTED_ATTRS_WORD0,
  68. NFSD4_1_SUPPORTED_ATTRS_WORD1,
  69. NFSD4_2_SUPPORTED_ATTRS_WORD2},
  70. };
  71. /*
  72. * As per referral draft, the fsid for a referral MUST be different from the fsid of the containing
  73. * directory in order to indicate to the client that a filesystem boundary is present
  74. * We use a fixed fsid for a referral
  75. */
  76. #define NFS4_REFERRAL_FSID_MAJOR 0x8000000ULL
  77. #define NFS4_REFERRAL_FSID_MINOR 0x8000000ULL
  78. static __be32
  79. check_filename(char *str, int len)
  80. {
  81. int i;
  82. if (len == 0)
  83. return nfserr_inval;
  84. if (len > NFS4_MAXNAMLEN)
  85. return nfserr_nametoolong;
  86. if (isdotent(str, len))
  87. return nfserr_badname;
  88. for (i = 0; i < len; i++)
  89. if (str[i] == '/')
  90. return nfserr_badname;
  91. return 0;
  92. }
  93. static int zero_clientid(clientid_t *clid)
  94. {
  95. return (clid->cl_boot == 0) && (clid->cl_id == 0);
  96. }
  97. /**
  98. * svcxdr_tmpalloc - allocate memory to be freed after compound processing
  99. * @argp: NFSv4 compound argument structure
  100. * @len: length of buffer to allocate
  101. *
  102. * Allocates a buffer of size @len to be freed when processing the compound
  103. * operation described in @argp finishes.
  104. */
  105. static void *
  106. svcxdr_tmpalloc(struct nfsd4_compoundargs *argp, u32 len)
  107. {
  108. struct svcxdr_tmpbuf *tb;
  109. tb = kmalloc(sizeof(*tb) + len, GFP_KERNEL);
  110. if (!tb)
  111. return NULL;
  112. tb->next = argp->to_free;
  113. argp->to_free = tb;
  114. return tb->buf;
  115. }
  116. /*
  117. * For xdr strings that need to be passed to other kernel api's
  118. * as null-terminated strings.
  119. *
  120. * Note null-terminating in place usually isn't safe since the
  121. * buffer might end on a page boundary.
  122. */
  123. static char *
  124. svcxdr_dupstr(struct nfsd4_compoundargs *argp, void *buf, u32 len)
  125. {
  126. char *p = svcxdr_tmpalloc(argp, len + 1);
  127. if (!p)
  128. return NULL;
  129. memcpy(p, buf, len);
  130. p[len] = '\0';
  131. return p;
  132. }
  133. static void *
  134. svcxdr_savemem(struct nfsd4_compoundargs *argp, __be32 *p, u32 len)
  135. {
  136. __be32 *tmp;
  137. /*
  138. * The location of the decoded data item is stable,
  139. * so @p is OK to use. This is the common case.
  140. */
  141. if (p != argp->xdr->scratch.iov_base)
  142. return p;
  143. tmp = svcxdr_tmpalloc(argp, len);
  144. if (!tmp)
  145. return NULL;
  146. memcpy(tmp, p, len);
  147. return tmp;
  148. }
  149. /*
  150. * NFSv4 basic data type decoders
  151. */
  152. /*
  153. * This helper handles variable-length opaques which belong to protocol
  154. * elements that this implementation does not support.
  155. */
  156. static __be32
  157. nfsd4_decode_ignored_string(struct nfsd4_compoundargs *argp, u32 maxlen)
  158. {
  159. u32 len;
  160. if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
  161. return nfserr_bad_xdr;
  162. if (maxlen && len > maxlen)
  163. return nfserr_bad_xdr;
  164. if (!xdr_inline_decode(argp->xdr, len))
  165. return nfserr_bad_xdr;
  166. return nfs_ok;
  167. }
  168. static __be32
  169. nfsd4_decode_opaque(struct nfsd4_compoundargs *argp, struct xdr_netobj *o)
  170. {
  171. __be32 *p;
  172. u32 len;
  173. if (xdr_stream_decode_u32(argp->xdr, &len) < 0)
  174. return nfserr_bad_xdr;
  175. if (len == 0 || len > NFS4_OPAQUE_LIMIT)
  176. return nfserr_bad_xdr;
  177. p = xdr_inline_decode(argp->xdr, len);
  178. if (!p)
  179. return nfserr_bad_xdr;
  180. o->data = svcxdr_savemem(argp, p, len);
  181. if (!o->data)
  182. return nfserr_jukebox;
  183. o->len = len;
  184. return nfs_ok;
  185. }
  186. static __be32
  187. nfsd4_decode_component4(struct nfsd4_compoundargs *argp, char **namp, u32 *lenp)
  188. {
  189. __be32 *p, status;
  190. if (xdr_stream_decode_u32(argp->xdr, lenp) < 0)
  191. return nfserr_bad_xdr;
  192. p = xdr_inline_decode(argp->xdr, *lenp);
  193. if (!p)
  194. return nfserr_bad_xdr;
  195. status = check_filename((char *)p, *lenp);
  196. if (status)
  197. return status;
  198. *namp = svcxdr_savemem(argp, p, *lenp);
  199. if (!*namp)
  200. return nfserr_jukebox;
  201. return nfs_ok;
  202. }
  203. static __be32
  204. nfsd4_decode_nfstime4(struct nfsd4_compoundargs *argp, struct timespec64 *tv)
  205. {
  206. __be32 *p;
  207. p = xdr_inline_decode(argp->xdr, XDR_UNIT * 3);
  208. if (!p)
  209. return nfserr_bad_xdr;
  210. p = xdr_decode_hyper(p, &tv->tv_sec);
  211. tv->tv_nsec = be32_to_cpup(p++);
  212. if (tv->tv_nsec >= (u32)1000000000)
  213. return nfserr_inval;
  214. return nfs_ok;
  215. }
  216. static __be32
  217. nfsd4_decode_verifier4(struct nfsd4_compoundargs *argp, nfs4_verifier *verf)
  218. {
  219. __be32 *p;
  220. p = xdr_inline_decode(argp->xdr, NFS4_VERIFIER_SIZE);
  221. if (!p)
  222. return nfserr_bad_xdr;
  223. memcpy(verf->data, p, sizeof(verf->data));
  224. return nfs_ok;
  225. }
  226. /**
  227. * nfsd4_decode_bitmap4 - Decode an NFSv4 bitmap4
  228. * @argp: NFSv4 compound argument structure
  229. * @bmval: pointer to an array of u32's to decode into
  230. * @bmlen: size of the @bmval array
  231. *
  232. * The server needs to return nfs_ok rather than nfserr_bad_xdr when
  233. * encountering bitmaps containing bits it does not recognize. This
  234. * includes bits in bitmap words past WORDn, where WORDn is the last
  235. * bitmap WORD the implementation currently supports. Thus we are
  236. * careful here to simply ignore bits in bitmap words that this
  237. * implementation has yet to support explicitly.
  238. *
  239. * Return values:
  240. * %nfs_ok: @bmval populated successfully
  241. * %nfserr_bad_xdr: the encoded bitmap was invalid
  242. */
  243. static __be32
  244. nfsd4_decode_bitmap4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen)
  245. {
  246. ssize_t status;
  247. status = xdr_stream_decode_uint32_array(argp->xdr, bmval, bmlen);
  248. return status == -EBADMSG ? nfserr_bad_xdr : nfs_ok;
  249. }
  250. static __be32
  251. nfsd4_decode_nfsace4(struct nfsd4_compoundargs *argp, struct nfs4_ace *ace)
  252. {
  253. __be32 *p, status;
  254. u32 length;
  255. if (xdr_stream_decode_u32(argp->xdr, &ace->type) < 0)
  256. return nfserr_bad_xdr;
  257. if (xdr_stream_decode_u32(argp->xdr, &ace->flag) < 0)
  258. return nfserr_bad_xdr;
  259. if (xdr_stream_decode_u32(argp->xdr, &ace->access_mask) < 0)
  260. return nfserr_bad_xdr;
  261. if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
  262. return nfserr_bad_xdr;
  263. p = xdr_inline_decode(argp->xdr, length);
  264. if (!p)
  265. return nfserr_bad_xdr;
  266. ace->whotype = nfs4_acl_get_whotype((char *)p, length);
  267. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  268. status = nfs_ok;
  269. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  270. status = nfsd_map_name_to_gid(argp->rqstp,
  271. (char *)p, length, &ace->who_gid);
  272. else
  273. status = nfsd_map_name_to_uid(argp->rqstp,
  274. (char *)p, length, &ace->who_uid);
  275. return status;
  276. }
  277. /* A counted array of nfsace4's */
  278. static noinline __be32
  279. nfsd4_decode_acl(struct nfsd4_compoundargs *argp, struct nfs4_acl **acl)
  280. {
  281. struct nfs4_ace *ace;
  282. __be32 status;
  283. u32 count;
  284. if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
  285. return nfserr_bad_xdr;
  286. if (count > xdr_stream_remaining(argp->xdr) / 20)
  287. /*
  288. * Even with 4-byte names there wouldn't be
  289. * space for that many aces; something fishy is
  290. * going on:
  291. */
  292. return nfserr_fbig;
  293. *acl = svcxdr_tmpalloc(argp, nfs4_acl_bytes(count));
  294. if (*acl == NULL)
  295. return nfserr_jukebox;
  296. (*acl)->naces = count;
  297. for (ace = (*acl)->aces; ace < (*acl)->aces + count; ace++) {
  298. status = nfsd4_decode_nfsace4(argp, ace);
  299. if (status)
  300. return status;
  301. }
  302. return nfs_ok;
  303. }
  304. static noinline __be32
  305. nfsd4_decode_security_label(struct nfsd4_compoundargs *argp,
  306. struct xdr_netobj *label)
  307. {
  308. u32 lfs, pi, length;
  309. __be32 *p;
  310. if (xdr_stream_decode_u32(argp->xdr, &lfs) < 0)
  311. return nfserr_bad_xdr;
  312. if (xdr_stream_decode_u32(argp->xdr, &pi) < 0)
  313. return nfserr_bad_xdr;
  314. if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
  315. return nfserr_bad_xdr;
  316. if (length > NFS4_MAXLABELLEN)
  317. return nfserr_badlabel;
  318. p = xdr_inline_decode(argp->xdr, length);
  319. if (!p)
  320. return nfserr_bad_xdr;
  321. label->len = length;
  322. label->data = svcxdr_dupstr(argp, p, length);
  323. if (!label->data)
  324. return nfserr_jukebox;
  325. return nfs_ok;
  326. }
  327. static __be32
  328. nfsd4_decode_fattr4(struct nfsd4_compoundargs *argp, u32 *bmval, u32 bmlen,
  329. struct iattr *iattr, struct nfs4_acl **acl,
  330. struct xdr_netobj *label, int *umask)
  331. {
  332. unsigned int starting_pos;
  333. u32 attrlist4_count;
  334. __be32 *p, status;
  335. iattr->ia_valid = 0;
  336. status = nfsd4_decode_bitmap4(argp, bmval, bmlen);
  337. if (status)
  338. return nfserr_bad_xdr;
  339. if (bmval[0] & ~NFSD_WRITEABLE_ATTRS_WORD0
  340. || bmval[1] & ~NFSD_WRITEABLE_ATTRS_WORD1
  341. || bmval[2] & ~NFSD_WRITEABLE_ATTRS_WORD2) {
  342. if (nfsd_attrs_supported(argp->minorversion, bmval))
  343. return nfserr_inval;
  344. return nfserr_attrnotsupp;
  345. }
  346. if (xdr_stream_decode_u32(argp->xdr, &attrlist4_count) < 0)
  347. return nfserr_bad_xdr;
  348. starting_pos = xdr_stream_pos(argp->xdr);
  349. if (bmval[0] & FATTR4_WORD0_SIZE) {
  350. u64 size;
  351. if (xdr_stream_decode_u64(argp->xdr, &size) < 0)
  352. return nfserr_bad_xdr;
  353. iattr->ia_size = size;
  354. iattr->ia_valid |= ATTR_SIZE;
  355. }
  356. if (bmval[0] & FATTR4_WORD0_ACL) {
  357. status = nfsd4_decode_acl(argp, acl);
  358. if (status)
  359. return status;
  360. } else
  361. *acl = NULL;
  362. if (bmval[1] & FATTR4_WORD1_MODE) {
  363. u32 mode;
  364. if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
  365. return nfserr_bad_xdr;
  366. iattr->ia_mode = mode;
  367. iattr->ia_mode &= (S_IFMT | S_IALLUGO);
  368. iattr->ia_valid |= ATTR_MODE;
  369. }
  370. if (bmval[1] & FATTR4_WORD1_OWNER) {
  371. u32 length;
  372. if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
  373. return nfserr_bad_xdr;
  374. p = xdr_inline_decode(argp->xdr, length);
  375. if (!p)
  376. return nfserr_bad_xdr;
  377. status = nfsd_map_name_to_uid(argp->rqstp, (char *)p, length,
  378. &iattr->ia_uid);
  379. if (status)
  380. return status;
  381. iattr->ia_valid |= ATTR_UID;
  382. }
  383. if (bmval[1] & FATTR4_WORD1_OWNER_GROUP) {
  384. u32 length;
  385. if (xdr_stream_decode_u32(argp->xdr, &length) < 0)
  386. return nfserr_bad_xdr;
  387. p = xdr_inline_decode(argp->xdr, length);
  388. if (!p)
  389. return nfserr_bad_xdr;
  390. status = nfsd_map_name_to_gid(argp->rqstp, (char *)p, length,
  391. &iattr->ia_gid);
  392. if (status)
  393. return status;
  394. iattr->ia_valid |= ATTR_GID;
  395. }
  396. if (bmval[1] & FATTR4_WORD1_TIME_ACCESS_SET) {
  397. u32 set_it;
  398. if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
  399. return nfserr_bad_xdr;
  400. switch (set_it) {
  401. case NFS4_SET_TO_CLIENT_TIME:
  402. status = nfsd4_decode_nfstime4(argp, &iattr->ia_atime);
  403. if (status)
  404. return status;
  405. iattr->ia_valid |= (ATTR_ATIME | ATTR_ATIME_SET);
  406. break;
  407. case NFS4_SET_TO_SERVER_TIME:
  408. iattr->ia_valid |= ATTR_ATIME;
  409. break;
  410. default:
  411. return nfserr_bad_xdr;
  412. }
  413. }
  414. if (bmval[1] & FATTR4_WORD1_TIME_CREATE) {
  415. struct timespec64 ts;
  416. /* No Linux filesystem supports setting this attribute. */
  417. bmval[1] &= ~FATTR4_WORD1_TIME_CREATE;
  418. status = nfsd4_decode_nfstime4(argp, &ts);
  419. if (status)
  420. return status;
  421. }
  422. if (bmval[1] & FATTR4_WORD1_TIME_MODIFY_SET) {
  423. u32 set_it;
  424. if (xdr_stream_decode_u32(argp->xdr, &set_it) < 0)
  425. return nfserr_bad_xdr;
  426. switch (set_it) {
  427. case NFS4_SET_TO_CLIENT_TIME:
  428. status = nfsd4_decode_nfstime4(argp, &iattr->ia_mtime);
  429. if (status)
  430. return status;
  431. iattr->ia_valid |= (ATTR_MTIME | ATTR_MTIME_SET);
  432. break;
  433. case NFS4_SET_TO_SERVER_TIME:
  434. iattr->ia_valid |= ATTR_MTIME;
  435. break;
  436. default:
  437. return nfserr_bad_xdr;
  438. }
  439. }
  440. label->len = 0;
  441. if (IS_ENABLED(CONFIG_NFSD_V4_SECURITY_LABEL) &&
  442. bmval[2] & FATTR4_WORD2_SECURITY_LABEL) {
  443. status = nfsd4_decode_security_label(argp, label);
  444. if (status)
  445. return status;
  446. }
  447. if (bmval[2] & FATTR4_WORD2_MODE_UMASK) {
  448. u32 mode, mask;
  449. if (!umask)
  450. return nfserr_bad_xdr;
  451. if (xdr_stream_decode_u32(argp->xdr, &mode) < 0)
  452. return nfserr_bad_xdr;
  453. iattr->ia_mode = mode & (S_IFMT | S_IALLUGO);
  454. if (xdr_stream_decode_u32(argp->xdr, &mask) < 0)
  455. return nfserr_bad_xdr;
  456. *umask = mask & S_IRWXUGO;
  457. iattr->ia_valid |= ATTR_MODE;
  458. }
  459. /* request sanity: did attrlist4 contain the expected number of words? */
  460. if (attrlist4_count != xdr_stream_pos(argp->xdr) - starting_pos)
  461. return nfserr_bad_xdr;
  462. return nfs_ok;
  463. }
  464. static __be32
  465. nfsd4_decode_stateid4(struct nfsd4_compoundargs *argp, stateid_t *sid)
  466. {
  467. __be32 *p;
  468. p = xdr_inline_decode(argp->xdr, NFS4_STATEID_SIZE);
  469. if (!p)
  470. return nfserr_bad_xdr;
  471. sid->si_generation = be32_to_cpup(p++);
  472. memcpy(&sid->si_opaque, p, sizeof(sid->si_opaque));
  473. return nfs_ok;
  474. }
  475. static __be32
  476. nfsd4_decode_clientid4(struct nfsd4_compoundargs *argp, clientid_t *clientid)
  477. {
  478. __be32 *p;
  479. p = xdr_inline_decode(argp->xdr, sizeof(__be64));
  480. if (!p)
  481. return nfserr_bad_xdr;
  482. memcpy(clientid, p, sizeof(*clientid));
  483. return nfs_ok;
  484. }
  485. static __be32
  486. nfsd4_decode_state_owner4(struct nfsd4_compoundargs *argp,
  487. clientid_t *clientid, struct xdr_netobj *owner)
  488. {
  489. __be32 status;
  490. status = nfsd4_decode_clientid4(argp, clientid);
  491. if (status)
  492. return status;
  493. return nfsd4_decode_opaque(argp, owner);
  494. }
  495. #ifdef CONFIG_NFSD_PNFS
  496. static __be32
  497. nfsd4_decode_deviceid4(struct nfsd4_compoundargs *argp,
  498. struct nfsd4_deviceid *devid)
  499. {
  500. __be32 *p;
  501. p = xdr_inline_decode(argp->xdr, NFS4_DEVICEID4_SIZE);
  502. if (!p)
  503. return nfserr_bad_xdr;
  504. memcpy(devid, p, sizeof(*devid));
  505. return nfs_ok;
  506. }
  507. static __be32
  508. nfsd4_decode_layoutupdate4(struct nfsd4_compoundargs *argp,
  509. struct nfsd4_layoutcommit *lcp)
  510. {
  511. if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_layout_type) < 0)
  512. return nfserr_bad_xdr;
  513. if (lcp->lc_layout_type < LAYOUT_NFSV4_1_FILES)
  514. return nfserr_bad_xdr;
  515. if (lcp->lc_layout_type >= LAYOUT_TYPE_MAX)
  516. return nfserr_bad_xdr;
  517. if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_up_len) < 0)
  518. return nfserr_bad_xdr;
  519. if (lcp->lc_up_len > 0) {
  520. lcp->lc_up_layout = xdr_inline_decode(argp->xdr, lcp->lc_up_len);
  521. if (!lcp->lc_up_layout)
  522. return nfserr_bad_xdr;
  523. }
  524. return nfs_ok;
  525. }
  526. static __be32
  527. nfsd4_decode_layoutreturn4(struct nfsd4_compoundargs *argp,
  528. struct nfsd4_layoutreturn *lrp)
  529. {
  530. __be32 status;
  531. if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_return_type) < 0)
  532. return nfserr_bad_xdr;
  533. switch (lrp->lr_return_type) {
  534. case RETURN_FILE:
  535. if (xdr_stream_decode_u64(argp->xdr, &lrp->lr_seg.offset) < 0)
  536. return nfserr_bad_xdr;
  537. if (xdr_stream_decode_u64(argp->xdr, &lrp->lr_seg.length) < 0)
  538. return nfserr_bad_xdr;
  539. status = nfsd4_decode_stateid4(argp, &lrp->lr_sid);
  540. if (status)
  541. return status;
  542. if (xdr_stream_decode_u32(argp->xdr, &lrp->lrf_body_len) < 0)
  543. return nfserr_bad_xdr;
  544. if (lrp->lrf_body_len > 0) {
  545. lrp->lrf_body = xdr_inline_decode(argp->xdr, lrp->lrf_body_len);
  546. if (!lrp->lrf_body)
  547. return nfserr_bad_xdr;
  548. }
  549. break;
  550. case RETURN_FSID:
  551. case RETURN_ALL:
  552. lrp->lr_seg.offset = 0;
  553. lrp->lr_seg.length = NFS4_MAX_UINT64;
  554. break;
  555. default:
  556. return nfserr_bad_xdr;
  557. }
  558. return nfs_ok;
  559. }
  560. #endif /* CONFIG_NFSD_PNFS */
  561. static __be32
  562. nfsd4_decode_sessionid4(struct nfsd4_compoundargs *argp,
  563. struct nfs4_sessionid *sessionid)
  564. {
  565. __be32 *p;
  566. p = xdr_inline_decode(argp->xdr, NFS4_MAX_SESSIONID_LEN);
  567. if (!p)
  568. return nfserr_bad_xdr;
  569. memcpy(sessionid->data, p, sizeof(sessionid->data));
  570. return nfs_ok;
  571. }
  572. /* Defined in Appendix A of RFC 5531 */
  573. static __be32
  574. nfsd4_decode_authsys_parms(struct nfsd4_compoundargs *argp,
  575. struct nfsd4_cb_sec *cbs)
  576. {
  577. u32 stamp, gidcount, uid, gid;
  578. __be32 *p, status;
  579. if (xdr_stream_decode_u32(argp->xdr, &stamp) < 0)
  580. return nfserr_bad_xdr;
  581. /* machine name */
  582. status = nfsd4_decode_ignored_string(argp, 255);
  583. if (status)
  584. return status;
  585. if (xdr_stream_decode_u32(argp->xdr, &uid) < 0)
  586. return nfserr_bad_xdr;
  587. if (xdr_stream_decode_u32(argp->xdr, &gid) < 0)
  588. return nfserr_bad_xdr;
  589. if (xdr_stream_decode_u32(argp->xdr, &gidcount) < 0)
  590. return nfserr_bad_xdr;
  591. if (gidcount > 16)
  592. return nfserr_bad_xdr;
  593. p = xdr_inline_decode(argp->xdr, gidcount << 2);
  594. if (!p)
  595. return nfserr_bad_xdr;
  596. if (cbs->flavor == (u32)(-1)) {
  597. struct user_namespace *userns = nfsd_user_namespace(argp->rqstp);
  598. kuid_t kuid = make_kuid(userns, uid);
  599. kgid_t kgid = make_kgid(userns, gid);
  600. if (uid_valid(kuid) && gid_valid(kgid)) {
  601. cbs->uid = kuid;
  602. cbs->gid = kgid;
  603. cbs->flavor = RPC_AUTH_UNIX;
  604. } else {
  605. dprintk("RPC_AUTH_UNIX with invalid uid or gid, ignoring!\n");
  606. }
  607. }
  608. return nfs_ok;
  609. }
  610. static __be32
  611. nfsd4_decode_gss_cb_handles4(struct nfsd4_compoundargs *argp,
  612. struct nfsd4_cb_sec *cbs)
  613. {
  614. __be32 status;
  615. u32 service;
  616. dprintk("RPC_AUTH_GSS callback secflavor not supported!\n");
  617. if (xdr_stream_decode_u32(argp->xdr, &service) < 0)
  618. return nfserr_bad_xdr;
  619. if (service < RPC_GSS_SVC_NONE || service > RPC_GSS_SVC_PRIVACY)
  620. return nfserr_bad_xdr;
  621. /* gcbp_handle_from_server */
  622. status = nfsd4_decode_ignored_string(argp, 0);
  623. if (status)
  624. return status;
  625. /* gcbp_handle_from_client */
  626. status = nfsd4_decode_ignored_string(argp, 0);
  627. if (status)
  628. return status;
  629. return nfs_ok;
  630. }
  631. /* a counted array of callback_sec_parms4 items */
  632. static __be32
  633. nfsd4_decode_cb_sec(struct nfsd4_compoundargs *argp, struct nfsd4_cb_sec *cbs)
  634. {
  635. u32 i, secflavor, nr_secflavs;
  636. __be32 status;
  637. /* callback_sec_params4 */
  638. if (xdr_stream_decode_u32(argp->xdr, &nr_secflavs) < 0)
  639. return nfserr_bad_xdr;
  640. if (nr_secflavs)
  641. cbs->flavor = (u32)(-1);
  642. else
  643. /* Is this legal? Be generous, take it to mean AUTH_NONE: */
  644. cbs->flavor = 0;
  645. for (i = 0; i < nr_secflavs; ++i) {
  646. if (xdr_stream_decode_u32(argp->xdr, &secflavor) < 0)
  647. return nfserr_bad_xdr;
  648. switch (secflavor) {
  649. case RPC_AUTH_NULL:
  650. /* void */
  651. if (cbs->flavor == (u32)(-1))
  652. cbs->flavor = RPC_AUTH_NULL;
  653. break;
  654. case RPC_AUTH_UNIX:
  655. status = nfsd4_decode_authsys_parms(argp, cbs);
  656. if (status)
  657. return status;
  658. break;
  659. case RPC_AUTH_GSS:
  660. status = nfsd4_decode_gss_cb_handles4(argp, cbs);
  661. if (status)
  662. return status;
  663. break;
  664. default:
  665. return nfserr_inval;
  666. }
  667. }
  668. return nfs_ok;
  669. }
  670. /*
  671. * NFSv4 operation argument decoders
  672. */
  673. static __be32
  674. nfsd4_decode_access(struct nfsd4_compoundargs *argp,
  675. struct nfsd4_access *access)
  676. {
  677. if (xdr_stream_decode_u32(argp->xdr, &access->ac_req_access) < 0)
  678. return nfserr_bad_xdr;
  679. return nfs_ok;
  680. }
  681. static __be32
  682. nfsd4_decode_close(struct nfsd4_compoundargs *argp, struct nfsd4_close *close)
  683. {
  684. if (xdr_stream_decode_u32(argp->xdr, &close->cl_seqid) < 0)
  685. return nfserr_bad_xdr;
  686. return nfsd4_decode_stateid4(argp, &close->cl_stateid);
  687. }
  688. static __be32
  689. nfsd4_decode_commit(struct nfsd4_compoundargs *argp, struct nfsd4_commit *commit)
  690. {
  691. if (xdr_stream_decode_u64(argp->xdr, &commit->co_offset) < 0)
  692. return nfserr_bad_xdr;
  693. if (xdr_stream_decode_u32(argp->xdr, &commit->co_count) < 0)
  694. return nfserr_bad_xdr;
  695. memset(&commit->co_verf, 0, sizeof(commit->co_verf));
  696. return nfs_ok;
  697. }
  698. static __be32
  699. nfsd4_decode_create(struct nfsd4_compoundargs *argp, struct nfsd4_create *create)
  700. {
  701. __be32 *p, status;
  702. memset(create, 0, sizeof(*create));
  703. if (xdr_stream_decode_u32(argp->xdr, &create->cr_type) < 0)
  704. return nfserr_bad_xdr;
  705. switch (create->cr_type) {
  706. case NF4LNK:
  707. if (xdr_stream_decode_u32(argp->xdr, &create->cr_datalen) < 0)
  708. return nfserr_bad_xdr;
  709. p = xdr_inline_decode(argp->xdr, create->cr_datalen);
  710. if (!p)
  711. return nfserr_bad_xdr;
  712. create->cr_data = svcxdr_dupstr(argp, p, create->cr_datalen);
  713. if (!create->cr_data)
  714. return nfserr_jukebox;
  715. break;
  716. case NF4BLK:
  717. case NF4CHR:
  718. if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata1) < 0)
  719. return nfserr_bad_xdr;
  720. if (xdr_stream_decode_u32(argp->xdr, &create->cr_specdata2) < 0)
  721. return nfserr_bad_xdr;
  722. break;
  723. case NF4SOCK:
  724. case NF4FIFO:
  725. case NF4DIR:
  726. default:
  727. break;
  728. }
  729. status = nfsd4_decode_component4(argp, &create->cr_name,
  730. &create->cr_namelen);
  731. if (status)
  732. return status;
  733. status = nfsd4_decode_fattr4(argp, create->cr_bmval,
  734. ARRAY_SIZE(create->cr_bmval),
  735. &create->cr_iattr, &create->cr_acl,
  736. &create->cr_label, &create->cr_umask);
  737. if (status)
  738. return status;
  739. return nfs_ok;
  740. }
  741. static inline __be32
  742. nfsd4_decode_delegreturn(struct nfsd4_compoundargs *argp, struct nfsd4_delegreturn *dr)
  743. {
  744. return nfsd4_decode_stateid4(argp, &dr->dr_stateid);
  745. }
  746. static inline __be32
  747. nfsd4_decode_getattr(struct nfsd4_compoundargs *argp, struct nfsd4_getattr *getattr)
  748. {
  749. memset(getattr, 0, sizeof(*getattr));
  750. return nfsd4_decode_bitmap4(argp, getattr->ga_bmval,
  751. ARRAY_SIZE(getattr->ga_bmval));
  752. }
  753. static __be32
  754. nfsd4_decode_link(struct nfsd4_compoundargs *argp, struct nfsd4_link *link)
  755. {
  756. memset(link, 0, sizeof(*link));
  757. return nfsd4_decode_component4(argp, &link->li_name, &link->li_namelen);
  758. }
  759. static __be32
  760. nfsd4_decode_open_to_lock_owner4(struct nfsd4_compoundargs *argp,
  761. struct nfsd4_lock *lock)
  762. {
  763. __be32 status;
  764. if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_open_seqid) < 0)
  765. return nfserr_bad_xdr;
  766. status = nfsd4_decode_stateid4(argp, &lock->lk_new_open_stateid);
  767. if (status)
  768. return status;
  769. if (xdr_stream_decode_u32(argp->xdr, &lock->lk_new_lock_seqid) < 0)
  770. return nfserr_bad_xdr;
  771. return nfsd4_decode_state_owner4(argp, &lock->lk_new_clientid,
  772. &lock->lk_new_owner);
  773. }
  774. static __be32
  775. nfsd4_decode_exist_lock_owner4(struct nfsd4_compoundargs *argp,
  776. struct nfsd4_lock *lock)
  777. {
  778. __be32 status;
  779. status = nfsd4_decode_stateid4(argp, &lock->lk_old_lock_stateid);
  780. if (status)
  781. return status;
  782. if (xdr_stream_decode_u32(argp->xdr, &lock->lk_old_lock_seqid) < 0)
  783. return nfserr_bad_xdr;
  784. return nfs_ok;
  785. }
  786. static __be32
  787. nfsd4_decode_locker4(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
  788. {
  789. if (xdr_stream_decode_bool(argp->xdr, &lock->lk_is_new) < 0)
  790. return nfserr_bad_xdr;
  791. if (lock->lk_is_new)
  792. return nfsd4_decode_open_to_lock_owner4(argp, lock);
  793. return nfsd4_decode_exist_lock_owner4(argp, lock);
  794. }
  795. static __be32
  796. nfsd4_decode_lock(struct nfsd4_compoundargs *argp, struct nfsd4_lock *lock)
  797. {
  798. memset(lock, 0, sizeof(*lock));
  799. if (xdr_stream_decode_u32(argp->xdr, &lock->lk_type) < 0)
  800. return nfserr_bad_xdr;
  801. if ((lock->lk_type < NFS4_READ_LT) || (lock->lk_type > NFS4_WRITEW_LT))
  802. return nfserr_bad_xdr;
  803. if (xdr_stream_decode_bool(argp->xdr, &lock->lk_reclaim) < 0)
  804. return nfserr_bad_xdr;
  805. if (xdr_stream_decode_u64(argp->xdr, &lock->lk_offset) < 0)
  806. return nfserr_bad_xdr;
  807. if (xdr_stream_decode_u64(argp->xdr, &lock->lk_length) < 0)
  808. return nfserr_bad_xdr;
  809. return nfsd4_decode_locker4(argp, lock);
  810. }
  811. static __be32
  812. nfsd4_decode_lockt(struct nfsd4_compoundargs *argp, struct nfsd4_lockt *lockt)
  813. {
  814. memset(lockt, 0, sizeof(*lockt));
  815. if (xdr_stream_decode_u32(argp->xdr, &lockt->lt_type) < 0)
  816. return nfserr_bad_xdr;
  817. if ((lockt->lt_type < NFS4_READ_LT) || (lockt->lt_type > NFS4_WRITEW_LT))
  818. return nfserr_bad_xdr;
  819. if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_offset) < 0)
  820. return nfserr_bad_xdr;
  821. if (xdr_stream_decode_u64(argp->xdr, &lockt->lt_length) < 0)
  822. return nfserr_bad_xdr;
  823. return nfsd4_decode_state_owner4(argp, &lockt->lt_clientid,
  824. &lockt->lt_owner);
  825. }
  826. static __be32
  827. nfsd4_decode_locku(struct nfsd4_compoundargs *argp, struct nfsd4_locku *locku)
  828. {
  829. __be32 status;
  830. if (xdr_stream_decode_u32(argp->xdr, &locku->lu_type) < 0)
  831. return nfserr_bad_xdr;
  832. if ((locku->lu_type < NFS4_READ_LT) || (locku->lu_type > NFS4_WRITEW_LT))
  833. return nfserr_bad_xdr;
  834. if (xdr_stream_decode_u32(argp->xdr, &locku->lu_seqid) < 0)
  835. return nfserr_bad_xdr;
  836. status = nfsd4_decode_stateid4(argp, &locku->lu_stateid);
  837. if (status)
  838. return status;
  839. if (xdr_stream_decode_u64(argp->xdr, &locku->lu_offset) < 0)
  840. return nfserr_bad_xdr;
  841. if (xdr_stream_decode_u64(argp->xdr, &locku->lu_length) < 0)
  842. return nfserr_bad_xdr;
  843. return nfs_ok;
  844. }
  845. static __be32
  846. nfsd4_decode_lookup(struct nfsd4_compoundargs *argp, struct nfsd4_lookup *lookup)
  847. {
  848. return nfsd4_decode_component4(argp, &lookup->lo_name, &lookup->lo_len);
  849. }
  850. static __be32
  851. nfsd4_decode_createhow4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
  852. {
  853. __be32 status;
  854. if (xdr_stream_decode_u32(argp->xdr, &open->op_createmode) < 0)
  855. return nfserr_bad_xdr;
  856. switch (open->op_createmode) {
  857. case NFS4_CREATE_UNCHECKED:
  858. case NFS4_CREATE_GUARDED:
  859. status = nfsd4_decode_fattr4(argp, open->op_bmval,
  860. ARRAY_SIZE(open->op_bmval),
  861. &open->op_iattr, &open->op_acl,
  862. &open->op_label, &open->op_umask);
  863. if (status)
  864. return status;
  865. break;
  866. case NFS4_CREATE_EXCLUSIVE:
  867. status = nfsd4_decode_verifier4(argp, &open->op_verf);
  868. if (status)
  869. return status;
  870. break;
  871. case NFS4_CREATE_EXCLUSIVE4_1:
  872. if (argp->minorversion < 1)
  873. return nfserr_bad_xdr;
  874. status = nfsd4_decode_verifier4(argp, &open->op_verf);
  875. if (status)
  876. return status;
  877. status = nfsd4_decode_fattr4(argp, open->op_bmval,
  878. ARRAY_SIZE(open->op_bmval),
  879. &open->op_iattr, &open->op_acl,
  880. &open->op_label, &open->op_umask);
  881. if (status)
  882. return status;
  883. break;
  884. default:
  885. return nfserr_bad_xdr;
  886. }
  887. return nfs_ok;
  888. }
  889. static __be32
  890. nfsd4_decode_openflag4(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
  891. {
  892. __be32 status;
  893. if (xdr_stream_decode_u32(argp->xdr, &open->op_create) < 0)
  894. return nfserr_bad_xdr;
  895. switch (open->op_create) {
  896. case NFS4_OPEN_NOCREATE:
  897. break;
  898. case NFS4_OPEN_CREATE:
  899. status = nfsd4_decode_createhow4(argp, open);
  900. if (status)
  901. return status;
  902. break;
  903. default:
  904. return nfserr_bad_xdr;
  905. }
  906. return nfs_ok;
  907. }
  908. static __be32 nfsd4_decode_share_access(struct nfsd4_compoundargs *argp, u32 *share_access, u32 *deleg_want, u32 *deleg_when)
  909. {
  910. u32 w;
  911. if (xdr_stream_decode_u32(argp->xdr, &w) < 0)
  912. return nfserr_bad_xdr;
  913. *share_access = w & NFS4_SHARE_ACCESS_MASK;
  914. *deleg_want = w & NFS4_SHARE_WANT_MASK;
  915. if (deleg_when)
  916. *deleg_when = w & NFS4_SHARE_WHEN_MASK;
  917. switch (w & NFS4_SHARE_ACCESS_MASK) {
  918. case NFS4_SHARE_ACCESS_READ:
  919. case NFS4_SHARE_ACCESS_WRITE:
  920. case NFS4_SHARE_ACCESS_BOTH:
  921. break;
  922. default:
  923. return nfserr_bad_xdr;
  924. }
  925. w &= ~NFS4_SHARE_ACCESS_MASK;
  926. if (!w)
  927. return nfs_ok;
  928. if (!argp->minorversion)
  929. return nfserr_bad_xdr;
  930. switch (w & NFS4_SHARE_WANT_MASK) {
  931. case NFS4_SHARE_WANT_NO_PREFERENCE:
  932. case NFS4_SHARE_WANT_READ_DELEG:
  933. case NFS4_SHARE_WANT_WRITE_DELEG:
  934. case NFS4_SHARE_WANT_ANY_DELEG:
  935. case NFS4_SHARE_WANT_NO_DELEG:
  936. case NFS4_SHARE_WANT_CANCEL:
  937. break;
  938. default:
  939. return nfserr_bad_xdr;
  940. }
  941. w &= ~NFS4_SHARE_WANT_MASK;
  942. if (!w)
  943. return nfs_ok;
  944. if (!deleg_when) /* open_downgrade */
  945. return nfserr_inval;
  946. switch (w) {
  947. case NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL:
  948. case NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED:
  949. case (NFS4_SHARE_SIGNAL_DELEG_WHEN_RESRC_AVAIL |
  950. NFS4_SHARE_PUSH_DELEG_WHEN_UNCONTENDED):
  951. return nfs_ok;
  952. }
  953. return nfserr_bad_xdr;
  954. }
  955. static __be32 nfsd4_decode_share_deny(struct nfsd4_compoundargs *argp, u32 *x)
  956. {
  957. if (xdr_stream_decode_u32(argp->xdr, x) < 0)
  958. return nfserr_bad_xdr;
  959. /* Note: unlike access bits, deny bits may be zero. */
  960. if (*x & ~NFS4_SHARE_DENY_BOTH)
  961. return nfserr_bad_xdr;
  962. return nfs_ok;
  963. }
  964. static __be32
  965. nfsd4_decode_open_claim4(struct nfsd4_compoundargs *argp,
  966. struct nfsd4_open *open)
  967. {
  968. __be32 status;
  969. if (xdr_stream_decode_u32(argp->xdr, &open->op_claim_type) < 0)
  970. return nfserr_bad_xdr;
  971. switch (open->op_claim_type) {
  972. case NFS4_OPEN_CLAIM_NULL:
  973. case NFS4_OPEN_CLAIM_DELEGATE_PREV:
  974. status = nfsd4_decode_component4(argp, &open->op_fname,
  975. &open->op_fnamelen);
  976. if (status)
  977. return status;
  978. break;
  979. case NFS4_OPEN_CLAIM_PREVIOUS:
  980. if (xdr_stream_decode_u32(argp->xdr, &open->op_delegate_type) < 0)
  981. return nfserr_bad_xdr;
  982. break;
  983. case NFS4_OPEN_CLAIM_DELEGATE_CUR:
  984. status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
  985. if (status)
  986. return status;
  987. status = nfsd4_decode_component4(argp, &open->op_fname,
  988. &open->op_fnamelen);
  989. if (status)
  990. return status;
  991. break;
  992. case NFS4_OPEN_CLAIM_FH:
  993. case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
  994. if (argp->minorversion < 1)
  995. return nfserr_bad_xdr;
  996. /* void */
  997. break;
  998. case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
  999. if (argp->minorversion < 1)
  1000. return nfserr_bad_xdr;
  1001. status = nfsd4_decode_stateid4(argp, &open->op_delegate_stateid);
  1002. if (status)
  1003. return status;
  1004. break;
  1005. default:
  1006. return nfserr_bad_xdr;
  1007. }
  1008. return nfs_ok;
  1009. }
  1010. static __be32
  1011. nfsd4_decode_open(struct nfsd4_compoundargs *argp, struct nfsd4_open *open)
  1012. {
  1013. __be32 status;
  1014. u32 dummy;
  1015. memset(open, 0, sizeof(*open));
  1016. if (xdr_stream_decode_u32(argp->xdr, &open->op_seqid) < 0)
  1017. return nfserr_bad_xdr;
  1018. /* deleg_want is ignored */
  1019. status = nfsd4_decode_share_access(argp, &open->op_share_access,
  1020. &open->op_deleg_want, &dummy);
  1021. if (status)
  1022. return status;
  1023. status = nfsd4_decode_share_deny(argp, &open->op_share_deny);
  1024. if (status)
  1025. return status;
  1026. status = nfsd4_decode_state_owner4(argp, &open->op_clientid,
  1027. &open->op_owner);
  1028. if (status)
  1029. return status;
  1030. status = nfsd4_decode_openflag4(argp, open);
  1031. if (status)
  1032. return status;
  1033. return nfsd4_decode_open_claim4(argp, open);
  1034. }
  1035. static __be32
  1036. nfsd4_decode_open_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_open_confirm *open_conf)
  1037. {
  1038. __be32 status;
  1039. if (argp->minorversion >= 1)
  1040. return nfserr_notsupp;
  1041. status = nfsd4_decode_stateid4(argp, &open_conf->oc_req_stateid);
  1042. if (status)
  1043. return status;
  1044. if (xdr_stream_decode_u32(argp->xdr, &open_conf->oc_seqid) < 0)
  1045. return nfserr_bad_xdr;
  1046. memset(&open_conf->oc_resp_stateid, 0,
  1047. sizeof(open_conf->oc_resp_stateid));
  1048. return nfs_ok;
  1049. }
  1050. static __be32
  1051. nfsd4_decode_open_downgrade(struct nfsd4_compoundargs *argp, struct nfsd4_open_downgrade *open_down)
  1052. {
  1053. __be32 status;
  1054. memset(open_down, 0, sizeof(*open_down));
  1055. status = nfsd4_decode_stateid4(argp, &open_down->od_stateid);
  1056. if (status)
  1057. return status;
  1058. if (xdr_stream_decode_u32(argp->xdr, &open_down->od_seqid) < 0)
  1059. return nfserr_bad_xdr;
  1060. /* deleg_want is ignored */
  1061. status = nfsd4_decode_share_access(argp, &open_down->od_share_access,
  1062. &open_down->od_deleg_want, NULL);
  1063. if (status)
  1064. return status;
  1065. return nfsd4_decode_share_deny(argp, &open_down->od_share_deny);
  1066. }
  1067. static __be32
  1068. nfsd4_decode_putfh(struct nfsd4_compoundargs *argp, struct nfsd4_putfh *putfh)
  1069. {
  1070. __be32 *p;
  1071. if (xdr_stream_decode_u32(argp->xdr, &putfh->pf_fhlen) < 0)
  1072. return nfserr_bad_xdr;
  1073. if (putfh->pf_fhlen > NFS4_FHSIZE)
  1074. return nfserr_bad_xdr;
  1075. p = xdr_inline_decode(argp->xdr, putfh->pf_fhlen);
  1076. if (!p)
  1077. return nfserr_bad_xdr;
  1078. putfh->pf_fhval = svcxdr_savemem(argp, p, putfh->pf_fhlen);
  1079. if (!putfh->pf_fhval)
  1080. return nfserr_jukebox;
  1081. putfh->no_verify = false;
  1082. return nfs_ok;
  1083. }
  1084. static __be32
  1085. nfsd4_decode_putpubfh(struct nfsd4_compoundargs *argp, void *p)
  1086. {
  1087. if (argp->minorversion == 0)
  1088. return nfs_ok;
  1089. return nfserr_notsupp;
  1090. }
  1091. static __be32
  1092. nfsd4_decode_read(struct nfsd4_compoundargs *argp, struct nfsd4_read *read)
  1093. {
  1094. __be32 status;
  1095. memset(read, 0, sizeof(*read));
  1096. status = nfsd4_decode_stateid4(argp, &read->rd_stateid);
  1097. if (status)
  1098. return status;
  1099. if (xdr_stream_decode_u64(argp->xdr, &read->rd_offset) < 0)
  1100. return nfserr_bad_xdr;
  1101. if (xdr_stream_decode_u32(argp->xdr, &read->rd_length) < 0)
  1102. return nfserr_bad_xdr;
  1103. return nfs_ok;
  1104. }
  1105. static __be32
  1106. nfsd4_decode_readdir(struct nfsd4_compoundargs *argp, struct nfsd4_readdir *readdir)
  1107. {
  1108. __be32 status;
  1109. memset(readdir, 0, sizeof(*readdir));
  1110. if (xdr_stream_decode_u64(argp->xdr, &readdir->rd_cookie) < 0)
  1111. return nfserr_bad_xdr;
  1112. status = nfsd4_decode_verifier4(argp, &readdir->rd_verf);
  1113. if (status)
  1114. return status;
  1115. if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_dircount) < 0)
  1116. return nfserr_bad_xdr;
  1117. if (xdr_stream_decode_u32(argp->xdr, &readdir->rd_maxcount) < 0)
  1118. return nfserr_bad_xdr;
  1119. if (xdr_stream_decode_uint32_array(argp->xdr, readdir->rd_bmval,
  1120. ARRAY_SIZE(readdir->rd_bmval)) < 0)
  1121. return nfserr_bad_xdr;
  1122. return nfs_ok;
  1123. }
  1124. static __be32
  1125. nfsd4_decode_remove(struct nfsd4_compoundargs *argp, struct nfsd4_remove *remove)
  1126. {
  1127. memset(&remove->rm_cinfo, 0, sizeof(remove->rm_cinfo));
  1128. return nfsd4_decode_component4(argp, &remove->rm_name, &remove->rm_namelen);
  1129. }
  1130. static __be32
  1131. nfsd4_decode_rename(struct nfsd4_compoundargs *argp, struct nfsd4_rename *rename)
  1132. {
  1133. __be32 status;
  1134. memset(rename, 0, sizeof(*rename));
  1135. status = nfsd4_decode_component4(argp, &rename->rn_sname, &rename->rn_snamelen);
  1136. if (status)
  1137. return status;
  1138. return nfsd4_decode_component4(argp, &rename->rn_tname, &rename->rn_tnamelen);
  1139. }
  1140. static __be32
  1141. nfsd4_decode_renew(struct nfsd4_compoundargs *argp, clientid_t *clientid)
  1142. {
  1143. return nfsd4_decode_clientid4(argp, clientid);
  1144. }
  1145. static __be32
  1146. nfsd4_decode_secinfo(struct nfsd4_compoundargs *argp,
  1147. struct nfsd4_secinfo *secinfo)
  1148. {
  1149. secinfo->si_exp = NULL;
  1150. return nfsd4_decode_component4(argp, &secinfo->si_name, &secinfo->si_namelen);
  1151. }
  1152. static __be32
  1153. nfsd4_decode_setattr(struct nfsd4_compoundargs *argp, struct nfsd4_setattr *setattr)
  1154. {
  1155. __be32 status;
  1156. memset(setattr, 0, sizeof(*setattr));
  1157. status = nfsd4_decode_stateid4(argp, &setattr->sa_stateid);
  1158. if (status)
  1159. return status;
  1160. return nfsd4_decode_fattr4(argp, setattr->sa_bmval,
  1161. ARRAY_SIZE(setattr->sa_bmval),
  1162. &setattr->sa_iattr, &setattr->sa_acl,
  1163. &setattr->sa_label, NULL);
  1164. }
  1165. static __be32
  1166. nfsd4_decode_setclientid(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid *setclientid)
  1167. {
  1168. __be32 *p, status;
  1169. memset(setclientid, 0, sizeof(*setclientid));
  1170. if (argp->minorversion >= 1)
  1171. return nfserr_notsupp;
  1172. status = nfsd4_decode_verifier4(argp, &setclientid->se_verf);
  1173. if (status)
  1174. return status;
  1175. status = nfsd4_decode_opaque(argp, &setclientid->se_name);
  1176. if (status)
  1177. return status;
  1178. if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_prog) < 0)
  1179. return nfserr_bad_xdr;
  1180. if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_netid_len) < 0)
  1181. return nfserr_bad_xdr;
  1182. p = xdr_inline_decode(argp->xdr, setclientid->se_callback_netid_len);
  1183. if (!p)
  1184. return nfserr_bad_xdr;
  1185. setclientid->se_callback_netid_val = svcxdr_savemem(argp, p,
  1186. setclientid->se_callback_netid_len);
  1187. if (!setclientid->se_callback_netid_val)
  1188. return nfserr_jukebox;
  1189. if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_addr_len) < 0)
  1190. return nfserr_bad_xdr;
  1191. p = xdr_inline_decode(argp->xdr, setclientid->se_callback_addr_len);
  1192. if (!p)
  1193. return nfserr_bad_xdr;
  1194. setclientid->se_callback_addr_val = svcxdr_savemem(argp, p,
  1195. setclientid->se_callback_addr_len);
  1196. if (!setclientid->se_callback_addr_val)
  1197. return nfserr_jukebox;
  1198. if (xdr_stream_decode_u32(argp->xdr, &setclientid->se_callback_ident) < 0)
  1199. return nfserr_bad_xdr;
  1200. return nfs_ok;
  1201. }
  1202. static __be32
  1203. nfsd4_decode_setclientid_confirm(struct nfsd4_compoundargs *argp, struct nfsd4_setclientid_confirm *scd_c)
  1204. {
  1205. __be32 status;
  1206. if (argp->minorversion >= 1)
  1207. return nfserr_notsupp;
  1208. status = nfsd4_decode_clientid4(argp, &scd_c->sc_clientid);
  1209. if (status)
  1210. return status;
  1211. return nfsd4_decode_verifier4(argp, &scd_c->sc_confirm);
  1212. }
  1213. /* Also used for NVERIFY */
  1214. static __be32
  1215. nfsd4_decode_verify(struct nfsd4_compoundargs *argp, struct nfsd4_verify *verify)
  1216. {
  1217. __be32 *p, status;
  1218. memset(verify, 0, sizeof(*verify));
  1219. status = nfsd4_decode_bitmap4(argp, verify->ve_bmval,
  1220. ARRAY_SIZE(verify->ve_bmval));
  1221. if (status)
  1222. return status;
  1223. /* For convenience's sake, we compare raw xdr'd attributes in
  1224. * nfsd4_proc_verify */
  1225. if (xdr_stream_decode_u32(argp->xdr, &verify->ve_attrlen) < 0)
  1226. return nfserr_bad_xdr;
  1227. p = xdr_inline_decode(argp->xdr, verify->ve_attrlen);
  1228. if (!p)
  1229. return nfserr_bad_xdr;
  1230. verify->ve_attrval = svcxdr_savemem(argp, p, verify->ve_attrlen);
  1231. if (!verify->ve_attrval)
  1232. return nfserr_jukebox;
  1233. return nfs_ok;
  1234. }
  1235. static __be32
  1236. nfsd4_decode_write(struct nfsd4_compoundargs *argp, struct nfsd4_write *write)
  1237. {
  1238. __be32 status;
  1239. status = nfsd4_decode_stateid4(argp, &write->wr_stateid);
  1240. if (status)
  1241. return status;
  1242. if (xdr_stream_decode_u64(argp->xdr, &write->wr_offset) < 0)
  1243. return nfserr_bad_xdr;
  1244. if (xdr_stream_decode_u32(argp->xdr, &write->wr_stable_how) < 0)
  1245. return nfserr_bad_xdr;
  1246. if (write->wr_stable_how > NFS_FILE_SYNC)
  1247. return nfserr_bad_xdr;
  1248. if (xdr_stream_decode_u32(argp->xdr, &write->wr_buflen) < 0)
  1249. return nfserr_bad_xdr;
  1250. if (!xdr_stream_subsegment(argp->xdr, &write->wr_payload, write->wr_buflen))
  1251. return nfserr_bad_xdr;
  1252. write->wr_bytes_written = 0;
  1253. write->wr_how_written = 0;
  1254. memset(&write->wr_verifier, 0, sizeof(write->wr_verifier));
  1255. return nfs_ok;
  1256. }
  1257. static __be32
  1258. nfsd4_decode_release_lockowner(struct nfsd4_compoundargs *argp, struct nfsd4_release_lockowner *rlockowner)
  1259. {
  1260. __be32 status;
  1261. if (argp->minorversion >= 1)
  1262. return nfserr_notsupp;
  1263. status = nfsd4_decode_state_owner4(argp, &rlockowner->rl_clientid,
  1264. &rlockowner->rl_owner);
  1265. if (status)
  1266. return status;
  1267. if (argp->minorversion && !zero_clientid(&rlockowner->rl_clientid))
  1268. return nfserr_inval;
  1269. return nfs_ok;
  1270. }
  1271. static __be32 nfsd4_decode_backchannel_ctl(struct nfsd4_compoundargs *argp, struct nfsd4_backchannel_ctl *bc)
  1272. {
  1273. memset(bc, 0, sizeof(*bc));
  1274. if (xdr_stream_decode_u32(argp->xdr, &bc->bc_cb_program) < 0)
  1275. return nfserr_bad_xdr;
  1276. return nfsd4_decode_cb_sec(argp, &bc->bc_cb_sec);
  1277. }
  1278. static __be32 nfsd4_decode_bind_conn_to_session(struct nfsd4_compoundargs *argp, struct nfsd4_bind_conn_to_session *bcts)
  1279. {
  1280. u32 use_conn_in_rdma_mode;
  1281. __be32 status;
  1282. memset(bcts, 0, sizeof(*bcts));
  1283. status = nfsd4_decode_sessionid4(argp, &bcts->sessionid);
  1284. if (status)
  1285. return status;
  1286. if (xdr_stream_decode_u32(argp->xdr, &bcts->dir) < 0)
  1287. return nfserr_bad_xdr;
  1288. if (xdr_stream_decode_u32(argp->xdr, &use_conn_in_rdma_mode) < 0)
  1289. return nfserr_bad_xdr;
  1290. return nfs_ok;
  1291. }
  1292. static __be32
  1293. nfsd4_decode_state_protect_ops(struct nfsd4_compoundargs *argp,
  1294. struct nfsd4_exchange_id *exid)
  1295. {
  1296. __be32 status;
  1297. status = nfsd4_decode_bitmap4(argp, exid->spo_must_enforce,
  1298. ARRAY_SIZE(exid->spo_must_enforce));
  1299. if (status)
  1300. return nfserr_bad_xdr;
  1301. status = nfsd4_decode_bitmap4(argp, exid->spo_must_allow,
  1302. ARRAY_SIZE(exid->spo_must_allow));
  1303. if (status)
  1304. return nfserr_bad_xdr;
  1305. return nfs_ok;
  1306. }
  1307. /*
  1308. * This implementation currently does not support SP4_SSV.
  1309. * This decoder simply skips over these arguments.
  1310. */
  1311. static noinline __be32
  1312. nfsd4_decode_ssv_sp_parms(struct nfsd4_compoundargs *argp,
  1313. struct nfsd4_exchange_id *exid)
  1314. {
  1315. u32 count, window, num_gss_handles;
  1316. __be32 status;
  1317. /* ssp_ops */
  1318. status = nfsd4_decode_state_protect_ops(argp, exid);
  1319. if (status)
  1320. return status;
  1321. /* ssp_hash_algs<> */
  1322. if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
  1323. return nfserr_bad_xdr;
  1324. while (count--) {
  1325. status = nfsd4_decode_ignored_string(argp, 0);
  1326. if (status)
  1327. return status;
  1328. }
  1329. /* ssp_encr_algs<> */
  1330. if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
  1331. return nfserr_bad_xdr;
  1332. while (count--) {
  1333. status = nfsd4_decode_ignored_string(argp, 0);
  1334. if (status)
  1335. return status;
  1336. }
  1337. if (xdr_stream_decode_u32(argp->xdr, &window) < 0)
  1338. return nfserr_bad_xdr;
  1339. if (xdr_stream_decode_u32(argp->xdr, &num_gss_handles) < 0)
  1340. return nfserr_bad_xdr;
  1341. return nfs_ok;
  1342. }
  1343. static __be32
  1344. nfsd4_decode_state_protect4_a(struct nfsd4_compoundargs *argp,
  1345. struct nfsd4_exchange_id *exid)
  1346. {
  1347. __be32 status;
  1348. if (xdr_stream_decode_u32(argp->xdr, &exid->spa_how) < 0)
  1349. return nfserr_bad_xdr;
  1350. switch (exid->spa_how) {
  1351. case SP4_NONE:
  1352. break;
  1353. case SP4_MACH_CRED:
  1354. status = nfsd4_decode_state_protect_ops(argp, exid);
  1355. if (status)
  1356. return status;
  1357. break;
  1358. case SP4_SSV:
  1359. status = nfsd4_decode_ssv_sp_parms(argp, exid);
  1360. if (status)
  1361. return status;
  1362. break;
  1363. default:
  1364. return nfserr_bad_xdr;
  1365. }
  1366. return nfs_ok;
  1367. }
  1368. static __be32
  1369. nfsd4_decode_nfs_impl_id4(struct nfsd4_compoundargs *argp,
  1370. struct nfsd4_exchange_id *exid)
  1371. {
  1372. __be32 status;
  1373. u32 count;
  1374. if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
  1375. return nfserr_bad_xdr;
  1376. switch (count) {
  1377. case 0:
  1378. break;
  1379. case 1:
  1380. /* Note that RFC 8881 places no length limit on
  1381. * nii_domain, but this implementation permits no
  1382. * more than NFS4_OPAQUE_LIMIT bytes */
  1383. status = nfsd4_decode_opaque(argp, &exid->nii_domain);
  1384. if (status)
  1385. return status;
  1386. /* Note that RFC 8881 places no length limit on
  1387. * nii_name, but this implementation permits no
  1388. * more than NFS4_OPAQUE_LIMIT bytes */
  1389. status = nfsd4_decode_opaque(argp, &exid->nii_name);
  1390. if (status)
  1391. return status;
  1392. status = nfsd4_decode_nfstime4(argp, &exid->nii_time);
  1393. if (status)
  1394. return status;
  1395. break;
  1396. default:
  1397. return nfserr_bad_xdr;
  1398. }
  1399. return nfs_ok;
  1400. }
  1401. static __be32
  1402. nfsd4_decode_exchange_id(struct nfsd4_compoundargs *argp,
  1403. struct nfsd4_exchange_id *exid)
  1404. {
  1405. __be32 status;
  1406. memset(exid, 0, sizeof(*exid));
  1407. status = nfsd4_decode_verifier4(argp, &exid->verifier);
  1408. if (status)
  1409. return status;
  1410. status = nfsd4_decode_opaque(argp, &exid->clname);
  1411. if (status)
  1412. return status;
  1413. if (xdr_stream_decode_u32(argp->xdr, &exid->flags) < 0)
  1414. return nfserr_bad_xdr;
  1415. status = nfsd4_decode_state_protect4_a(argp, exid);
  1416. if (status)
  1417. return status;
  1418. return nfsd4_decode_nfs_impl_id4(argp, exid);
  1419. }
  1420. static __be32
  1421. nfsd4_decode_channel_attrs4(struct nfsd4_compoundargs *argp,
  1422. struct nfsd4_channel_attrs *ca)
  1423. {
  1424. __be32 *p;
  1425. p = xdr_inline_decode(argp->xdr, XDR_UNIT * 7);
  1426. if (!p)
  1427. return nfserr_bad_xdr;
  1428. /* headerpadsz is ignored */
  1429. p++;
  1430. ca->maxreq_sz = be32_to_cpup(p++);
  1431. ca->maxresp_sz = be32_to_cpup(p++);
  1432. ca->maxresp_cached = be32_to_cpup(p++);
  1433. ca->maxops = be32_to_cpup(p++);
  1434. ca->maxreqs = be32_to_cpup(p++);
  1435. ca->nr_rdma_attrs = be32_to_cpup(p);
  1436. switch (ca->nr_rdma_attrs) {
  1437. case 0:
  1438. break;
  1439. case 1:
  1440. if (xdr_stream_decode_u32(argp->xdr, &ca->rdma_attrs) < 0)
  1441. return nfserr_bad_xdr;
  1442. break;
  1443. default:
  1444. return nfserr_bad_xdr;
  1445. }
  1446. return nfs_ok;
  1447. }
  1448. static __be32
  1449. nfsd4_decode_create_session(struct nfsd4_compoundargs *argp,
  1450. struct nfsd4_create_session *sess)
  1451. {
  1452. __be32 status;
  1453. memset(sess, 0, sizeof(*sess));
  1454. status = nfsd4_decode_clientid4(argp, &sess->clientid);
  1455. if (status)
  1456. return status;
  1457. if (xdr_stream_decode_u32(argp->xdr, &sess->seqid) < 0)
  1458. return nfserr_bad_xdr;
  1459. if (xdr_stream_decode_u32(argp->xdr, &sess->flags) < 0)
  1460. return nfserr_bad_xdr;
  1461. status = nfsd4_decode_channel_attrs4(argp, &sess->fore_channel);
  1462. if (status)
  1463. return status;
  1464. status = nfsd4_decode_channel_attrs4(argp, &sess->back_channel);
  1465. if (status)
  1466. return status;
  1467. if (xdr_stream_decode_u32(argp->xdr, &sess->callback_prog) < 0)
  1468. return nfserr_bad_xdr;
  1469. return nfsd4_decode_cb_sec(argp, &sess->cb_sec);
  1470. }
  1471. static __be32
  1472. nfsd4_decode_destroy_session(struct nfsd4_compoundargs *argp,
  1473. struct nfsd4_destroy_session *destroy_session)
  1474. {
  1475. return nfsd4_decode_sessionid4(argp, &destroy_session->sessionid);
  1476. }
  1477. static __be32
  1478. nfsd4_decode_free_stateid(struct nfsd4_compoundargs *argp,
  1479. struct nfsd4_free_stateid *free_stateid)
  1480. {
  1481. return nfsd4_decode_stateid4(argp, &free_stateid->fr_stateid);
  1482. }
  1483. #ifdef CONFIG_NFSD_PNFS
  1484. static __be32
  1485. nfsd4_decode_getdeviceinfo(struct nfsd4_compoundargs *argp,
  1486. struct nfsd4_getdeviceinfo *gdev)
  1487. {
  1488. __be32 status;
  1489. memset(gdev, 0, sizeof(*gdev));
  1490. status = nfsd4_decode_deviceid4(argp, &gdev->gd_devid);
  1491. if (status)
  1492. return status;
  1493. if (xdr_stream_decode_u32(argp->xdr, &gdev->gd_layout_type) < 0)
  1494. return nfserr_bad_xdr;
  1495. if (xdr_stream_decode_u32(argp->xdr, &gdev->gd_maxcount) < 0)
  1496. return nfserr_bad_xdr;
  1497. if (xdr_stream_decode_uint32_array(argp->xdr,
  1498. &gdev->gd_notify_types, 1) < 0)
  1499. return nfserr_bad_xdr;
  1500. return nfs_ok;
  1501. }
  1502. static __be32
  1503. nfsd4_decode_layoutcommit(struct nfsd4_compoundargs *argp,
  1504. struct nfsd4_layoutcommit *lcp)
  1505. {
  1506. __be32 *p, status;
  1507. memset(lcp, 0, sizeof(*lcp));
  1508. if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_seg.offset) < 0)
  1509. return nfserr_bad_xdr;
  1510. if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_seg.length) < 0)
  1511. return nfserr_bad_xdr;
  1512. if (xdr_stream_decode_bool(argp->xdr, &lcp->lc_reclaim) < 0)
  1513. return nfserr_bad_xdr;
  1514. status = nfsd4_decode_stateid4(argp, &lcp->lc_sid);
  1515. if (status)
  1516. return status;
  1517. if (xdr_stream_decode_u32(argp->xdr, &lcp->lc_newoffset) < 0)
  1518. return nfserr_bad_xdr;
  1519. if (lcp->lc_newoffset) {
  1520. if (xdr_stream_decode_u64(argp->xdr, &lcp->lc_last_wr) < 0)
  1521. return nfserr_bad_xdr;
  1522. } else
  1523. lcp->lc_last_wr = 0;
  1524. p = xdr_inline_decode(argp->xdr, XDR_UNIT);
  1525. if (!p)
  1526. return nfserr_bad_xdr;
  1527. if (xdr_item_is_present(p)) {
  1528. status = nfsd4_decode_nfstime4(argp, &lcp->lc_mtime);
  1529. if (status)
  1530. return status;
  1531. } else {
  1532. lcp->lc_mtime.tv_nsec = UTIME_NOW;
  1533. }
  1534. return nfsd4_decode_layoutupdate4(argp, lcp);
  1535. }
  1536. static __be32
  1537. nfsd4_decode_layoutget(struct nfsd4_compoundargs *argp,
  1538. struct nfsd4_layoutget *lgp)
  1539. {
  1540. __be32 status;
  1541. memset(lgp, 0, sizeof(*lgp));
  1542. if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_signal) < 0)
  1543. return nfserr_bad_xdr;
  1544. if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_layout_type) < 0)
  1545. return nfserr_bad_xdr;
  1546. if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_seg.iomode) < 0)
  1547. return nfserr_bad_xdr;
  1548. if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_seg.offset) < 0)
  1549. return nfserr_bad_xdr;
  1550. if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_seg.length) < 0)
  1551. return nfserr_bad_xdr;
  1552. if (xdr_stream_decode_u64(argp->xdr, &lgp->lg_minlength) < 0)
  1553. return nfserr_bad_xdr;
  1554. status = nfsd4_decode_stateid4(argp, &lgp->lg_sid);
  1555. if (status)
  1556. return status;
  1557. if (xdr_stream_decode_u32(argp->xdr, &lgp->lg_maxcount) < 0)
  1558. return nfserr_bad_xdr;
  1559. return nfs_ok;
  1560. }
  1561. static __be32
  1562. nfsd4_decode_layoutreturn(struct nfsd4_compoundargs *argp,
  1563. struct nfsd4_layoutreturn *lrp)
  1564. {
  1565. memset(lrp, 0, sizeof(*lrp));
  1566. if (xdr_stream_decode_bool(argp->xdr, &lrp->lr_reclaim) < 0)
  1567. return nfserr_bad_xdr;
  1568. if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_layout_type) < 0)
  1569. return nfserr_bad_xdr;
  1570. if (xdr_stream_decode_u32(argp->xdr, &lrp->lr_seg.iomode) < 0)
  1571. return nfserr_bad_xdr;
  1572. return nfsd4_decode_layoutreturn4(argp, lrp);
  1573. }
  1574. #endif /* CONFIG_NFSD_PNFS */
  1575. static __be32 nfsd4_decode_secinfo_no_name(struct nfsd4_compoundargs *argp,
  1576. struct nfsd4_secinfo_no_name *sin)
  1577. {
  1578. if (xdr_stream_decode_u32(argp->xdr, &sin->sin_style) < 0)
  1579. return nfserr_bad_xdr;
  1580. sin->sin_exp = NULL;
  1581. return nfs_ok;
  1582. }
  1583. static __be32
  1584. nfsd4_decode_sequence(struct nfsd4_compoundargs *argp,
  1585. struct nfsd4_sequence *seq)
  1586. {
  1587. __be32 *p, status;
  1588. status = nfsd4_decode_sessionid4(argp, &seq->sessionid);
  1589. if (status)
  1590. return status;
  1591. p = xdr_inline_decode(argp->xdr, XDR_UNIT * 4);
  1592. if (!p)
  1593. return nfserr_bad_xdr;
  1594. seq->seqid = be32_to_cpup(p++);
  1595. seq->slotid = be32_to_cpup(p++);
  1596. seq->maxslots = be32_to_cpup(p++);
  1597. seq->cachethis = be32_to_cpup(p);
  1598. seq->status_flags = 0;
  1599. return nfs_ok;
  1600. }
  1601. static __be32
  1602. nfsd4_decode_test_stateid(struct nfsd4_compoundargs *argp, struct nfsd4_test_stateid *test_stateid)
  1603. {
  1604. struct nfsd4_test_stateid_id *stateid;
  1605. __be32 status;
  1606. u32 i;
  1607. memset(test_stateid, 0, sizeof(*test_stateid));
  1608. if (xdr_stream_decode_u32(argp->xdr, &test_stateid->ts_num_ids) < 0)
  1609. return nfserr_bad_xdr;
  1610. INIT_LIST_HEAD(&test_stateid->ts_stateid_list);
  1611. for (i = 0; i < test_stateid->ts_num_ids; i++) {
  1612. stateid = svcxdr_tmpalloc(argp, sizeof(*stateid));
  1613. if (!stateid)
  1614. return nfserr_jukebox;
  1615. INIT_LIST_HEAD(&stateid->ts_id_list);
  1616. list_add_tail(&stateid->ts_id_list, &test_stateid->ts_stateid_list);
  1617. status = nfsd4_decode_stateid4(argp, &stateid->ts_id_stateid);
  1618. if (status)
  1619. return status;
  1620. }
  1621. return nfs_ok;
  1622. }
  1623. static __be32 nfsd4_decode_destroy_clientid(struct nfsd4_compoundargs *argp,
  1624. struct nfsd4_destroy_clientid *dc)
  1625. {
  1626. return nfsd4_decode_clientid4(argp, &dc->clientid);
  1627. }
  1628. static __be32 nfsd4_decode_reclaim_complete(struct nfsd4_compoundargs *argp,
  1629. struct nfsd4_reclaim_complete *rc)
  1630. {
  1631. if (xdr_stream_decode_bool(argp->xdr, &rc->rca_one_fs) < 0)
  1632. return nfserr_bad_xdr;
  1633. return nfs_ok;
  1634. }
  1635. static __be32
  1636. nfsd4_decode_fallocate(struct nfsd4_compoundargs *argp,
  1637. struct nfsd4_fallocate *fallocate)
  1638. {
  1639. __be32 status;
  1640. status = nfsd4_decode_stateid4(argp, &fallocate->falloc_stateid);
  1641. if (status)
  1642. return status;
  1643. if (xdr_stream_decode_u64(argp->xdr, &fallocate->falloc_offset) < 0)
  1644. return nfserr_bad_xdr;
  1645. if (xdr_stream_decode_u64(argp->xdr, &fallocate->falloc_length) < 0)
  1646. return nfserr_bad_xdr;
  1647. return nfs_ok;
  1648. }
  1649. static __be32 nfsd4_decode_nl4_server(struct nfsd4_compoundargs *argp,
  1650. struct nl4_server *ns)
  1651. {
  1652. struct nfs42_netaddr *naddr;
  1653. __be32 *p;
  1654. if (xdr_stream_decode_u32(argp->xdr, &ns->nl4_type) < 0)
  1655. return nfserr_bad_xdr;
  1656. /* currently support for 1 inter-server source server */
  1657. switch (ns->nl4_type) {
  1658. case NL4_NETADDR:
  1659. naddr = &ns->u.nl4_addr;
  1660. if (xdr_stream_decode_u32(argp->xdr, &naddr->netid_len) < 0)
  1661. return nfserr_bad_xdr;
  1662. if (naddr->netid_len > RPCBIND_MAXNETIDLEN)
  1663. return nfserr_bad_xdr;
  1664. p = xdr_inline_decode(argp->xdr, naddr->netid_len);
  1665. if (!p)
  1666. return nfserr_bad_xdr;
  1667. memcpy(naddr->netid, p, naddr->netid_len);
  1668. if (xdr_stream_decode_u32(argp->xdr, &naddr->addr_len) < 0)
  1669. return nfserr_bad_xdr;
  1670. if (naddr->addr_len > RPCBIND_MAXUADDRLEN)
  1671. return nfserr_bad_xdr;
  1672. p = xdr_inline_decode(argp->xdr, naddr->addr_len);
  1673. if (!p)
  1674. return nfserr_bad_xdr;
  1675. memcpy(naddr->addr, p, naddr->addr_len);
  1676. break;
  1677. default:
  1678. return nfserr_bad_xdr;
  1679. }
  1680. return nfs_ok;
  1681. }
  1682. static __be32
  1683. nfsd4_decode_copy(struct nfsd4_compoundargs *argp, struct nfsd4_copy *copy)
  1684. {
  1685. u32 consecutive, i, count, sync;
  1686. struct nl4_server *ns_dummy;
  1687. __be32 status;
  1688. memset(copy, 0, sizeof(*copy));
  1689. status = nfsd4_decode_stateid4(argp, &copy->cp_src_stateid);
  1690. if (status)
  1691. return status;
  1692. status = nfsd4_decode_stateid4(argp, &copy->cp_dst_stateid);
  1693. if (status)
  1694. return status;
  1695. if (xdr_stream_decode_u64(argp->xdr, &copy->cp_src_pos) < 0)
  1696. return nfserr_bad_xdr;
  1697. if (xdr_stream_decode_u64(argp->xdr, &copy->cp_dst_pos) < 0)
  1698. return nfserr_bad_xdr;
  1699. if (xdr_stream_decode_u64(argp->xdr, &copy->cp_count) < 0)
  1700. return nfserr_bad_xdr;
  1701. /* ca_consecutive: we always do consecutive copies */
  1702. if (xdr_stream_decode_u32(argp->xdr, &consecutive) < 0)
  1703. return nfserr_bad_xdr;
  1704. if (xdr_stream_decode_bool(argp->xdr, &sync) < 0)
  1705. return nfserr_bad_xdr;
  1706. nfsd4_copy_set_sync(copy, sync);
  1707. if (xdr_stream_decode_u32(argp->xdr, &count) < 0)
  1708. return nfserr_bad_xdr;
  1709. copy->cp_src = svcxdr_tmpalloc(argp, sizeof(*copy->cp_src));
  1710. if (copy->cp_src == NULL)
  1711. return nfserr_jukebox;
  1712. if (count == 0) { /* intra-server copy */
  1713. __set_bit(NFSD4_COPY_F_INTRA, &copy->cp_flags);
  1714. return nfs_ok;
  1715. }
  1716. /* decode all the supplied server addresses but use only the first */
  1717. status = nfsd4_decode_nl4_server(argp, copy->cp_src);
  1718. if (status)
  1719. return status;
  1720. ns_dummy = kmalloc(sizeof(struct nl4_server), GFP_KERNEL);
  1721. if (ns_dummy == NULL)
  1722. return nfserr_jukebox;
  1723. for (i = 0; i < count - 1; i++) {
  1724. status = nfsd4_decode_nl4_server(argp, ns_dummy);
  1725. if (status) {
  1726. kfree(ns_dummy);
  1727. return status;
  1728. }
  1729. }
  1730. kfree(ns_dummy);
  1731. return nfs_ok;
  1732. }
  1733. static __be32
  1734. nfsd4_decode_copy_notify(struct nfsd4_compoundargs *argp,
  1735. struct nfsd4_copy_notify *cn)
  1736. {
  1737. __be32 status;
  1738. memset(cn, 0, sizeof(*cn));
  1739. cn->cpn_src = svcxdr_tmpalloc(argp, sizeof(*cn->cpn_src));
  1740. if (cn->cpn_src == NULL)
  1741. return nfserr_jukebox;
  1742. cn->cpn_dst = svcxdr_tmpalloc(argp, sizeof(*cn->cpn_dst));
  1743. if (cn->cpn_dst == NULL)
  1744. return nfserr_jukebox;
  1745. status = nfsd4_decode_stateid4(argp, &cn->cpn_src_stateid);
  1746. if (status)
  1747. return status;
  1748. return nfsd4_decode_nl4_server(argp, cn->cpn_dst);
  1749. }
  1750. static __be32
  1751. nfsd4_decode_offload_status(struct nfsd4_compoundargs *argp,
  1752. struct nfsd4_offload_status *os)
  1753. {
  1754. os->count = 0;
  1755. os->status = 0;
  1756. return nfsd4_decode_stateid4(argp, &os->stateid);
  1757. }
  1758. static __be32
  1759. nfsd4_decode_seek(struct nfsd4_compoundargs *argp, struct nfsd4_seek *seek)
  1760. {
  1761. __be32 status;
  1762. status = nfsd4_decode_stateid4(argp, &seek->seek_stateid);
  1763. if (status)
  1764. return status;
  1765. if (xdr_stream_decode_u64(argp->xdr, &seek->seek_offset) < 0)
  1766. return nfserr_bad_xdr;
  1767. if (xdr_stream_decode_u32(argp->xdr, &seek->seek_whence) < 0)
  1768. return nfserr_bad_xdr;
  1769. seek->seek_eof = 0;
  1770. seek->seek_pos = 0;
  1771. return nfs_ok;
  1772. }
  1773. static __be32
  1774. nfsd4_decode_clone(struct nfsd4_compoundargs *argp, struct nfsd4_clone *clone)
  1775. {
  1776. __be32 status;
  1777. status = nfsd4_decode_stateid4(argp, &clone->cl_src_stateid);
  1778. if (status)
  1779. return status;
  1780. status = nfsd4_decode_stateid4(argp, &clone->cl_dst_stateid);
  1781. if (status)
  1782. return status;
  1783. if (xdr_stream_decode_u64(argp->xdr, &clone->cl_src_pos) < 0)
  1784. return nfserr_bad_xdr;
  1785. if (xdr_stream_decode_u64(argp->xdr, &clone->cl_dst_pos) < 0)
  1786. return nfserr_bad_xdr;
  1787. if (xdr_stream_decode_u64(argp->xdr, &clone->cl_count) < 0)
  1788. return nfserr_bad_xdr;
  1789. return nfs_ok;
  1790. }
  1791. /*
  1792. * XDR data that is more than PAGE_SIZE in size is normally part of a
  1793. * read or write. However, the size of extended attributes is limited
  1794. * by the maximum request size, and then further limited by the underlying
  1795. * filesystem limits. This can exceed PAGE_SIZE (currently, XATTR_SIZE_MAX
  1796. * is 64k). Since there is no kvec- or page-based interface to xattrs,
  1797. * and we're not dealing with contiguous pages, we need to do some copying.
  1798. */
  1799. /*
  1800. * Decode data into buffer.
  1801. */
  1802. static __be32
  1803. nfsd4_vbuf_from_vector(struct nfsd4_compoundargs *argp, struct xdr_buf *xdr,
  1804. char **bufp, u32 buflen)
  1805. {
  1806. struct page **pages = xdr->pages;
  1807. struct kvec *head = xdr->head;
  1808. char *tmp, *dp;
  1809. u32 len;
  1810. if (buflen <= head->iov_len) {
  1811. /*
  1812. * We're in luck, the head has enough space. Just return
  1813. * the head, no need for copying.
  1814. */
  1815. *bufp = head->iov_base;
  1816. return 0;
  1817. }
  1818. tmp = svcxdr_tmpalloc(argp, buflen);
  1819. if (tmp == NULL)
  1820. return nfserr_jukebox;
  1821. dp = tmp;
  1822. memcpy(dp, head->iov_base, head->iov_len);
  1823. buflen -= head->iov_len;
  1824. dp += head->iov_len;
  1825. while (buflen > 0) {
  1826. len = min_t(u32, buflen, PAGE_SIZE);
  1827. memcpy(dp, page_address(*pages), len);
  1828. buflen -= len;
  1829. dp += len;
  1830. pages++;
  1831. }
  1832. *bufp = tmp;
  1833. return 0;
  1834. }
  1835. /*
  1836. * Get a user extended attribute name from the XDR buffer.
  1837. * It will not have the "user." prefix, so prepend it.
  1838. * Lastly, check for nul characters in the name.
  1839. */
  1840. static __be32
  1841. nfsd4_decode_xattr_name(struct nfsd4_compoundargs *argp, char **namep)
  1842. {
  1843. char *name, *sp, *dp;
  1844. u32 namelen, cnt;
  1845. __be32 *p;
  1846. if (xdr_stream_decode_u32(argp->xdr, &namelen) < 0)
  1847. return nfserr_bad_xdr;
  1848. if (namelen > (XATTR_NAME_MAX - XATTR_USER_PREFIX_LEN))
  1849. return nfserr_nametoolong;
  1850. if (namelen == 0)
  1851. return nfserr_bad_xdr;
  1852. p = xdr_inline_decode(argp->xdr, namelen);
  1853. if (!p)
  1854. return nfserr_bad_xdr;
  1855. name = svcxdr_tmpalloc(argp, namelen + XATTR_USER_PREFIX_LEN + 1);
  1856. if (!name)
  1857. return nfserr_jukebox;
  1858. memcpy(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN);
  1859. /*
  1860. * Copy the extended attribute name over while checking for 0
  1861. * characters.
  1862. */
  1863. sp = (char *)p;
  1864. dp = name + XATTR_USER_PREFIX_LEN;
  1865. cnt = namelen;
  1866. while (cnt-- > 0) {
  1867. if (*sp == '\0')
  1868. return nfserr_bad_xdr;
  1869. *dp++ = *sp++;
  1870. }
  1871. *dp = '\0';
  1872. *namep = name;
  1873. return nfs_ok;
  1874. }
  1875. /*
  1876. * A GETXATTR op request comes without a length specifier. We just set the
  1877. * maximum length for the reply based on XATTR_SIZE_MAX and the maximum
  1878. * channel reply size. nfsd_getxattr will probe the length of the xattr,
  1879. * check it against getxa_len, and allocate + return the value.
  1880. */
  1881. static __be32
  1882. nfsd4_decode_getxattr(struct nfsd4_compoundargs *argp,
  1883. struct nfsd4_getxattr *getxattr)
  1884. {
  1885. __be32 status;
  1886. u32 maxcount;
  1887. memset(getxattr, 0, sizeof(*getxattr));
  1888. status = nfsd4_decode_xattr_name(argp, &getxattr->getxa_name);
  1889. if (status)
  1890. return status;
  1891. maxcount = svc_max_payload(argp->rqstp);
  1892. maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
  1893. getxattr->getxa_len = maxcount;
  1894. return nfs_ok;
  1895. }
  1896. static __be32
  1897. nfsd4_decode_setxattr(struct nfsd4_compoundargs *argp,
  1898. struct nfsd4_setxattr *setxattr)
  1899. {
  1900. u32 flags, maxcount, size;
  1901. __be32 status;
  1902. memset(setxattr, 0, sizeof(*setxattr));
  1903. if (xdr_stream_decode_u32(argp->xdr, &flags) < 0)
  1904. return nfserr_bad_xdr;
  1905. if (flags > SETXATTR4_REPLACE)
  1906. return nfserr_inval;
  1907. setxattr->setxa_flags = flags;
  1908. status = nfsd4_decode_xattr_name(argp, &setxattr->setxa_name);
  1909. if (status)
  1910. return status;
  1911. maxcount = svc_max_payload(argp->rqstp);
  1912. maxcount = min_t(u32, XATTR_SIZE_MAX, maxcount);
  1913. if (xdr_stream_decode_u32(argp->xdr, &size) < 0)
  1914. return nfserr_bad_xdr;
  1915. if (size > maxcount)
  1916. return nfserr_xattr2big;
  1917. setxattr->setxa_len = size;
  1918. if (size > 0) {
  1919. struct xdr_buf payload;
  1920. if (!xdr_stream_subsegment(argp->xdr, &payload, size))
  1921. return nfserr_bad_xdr;
  1922. status = nfsd4_vbuf_from_vector(argp, &payload,
  1923. &setxattr->setxa_buf, size);
  1924. }
  1925. return nfs_ok;
  1926. }
  1927. static __be32
  1928. nfsd4_decode_listxattrs(struct nfsd4_compoundargs *argp,
  1929. struct nfsd4_listxattrs *listxattrs)
  1930. {
  1931. u32 maxcount;
  1932. memset(listxattrs, 0, sizeof(*listxattrs));
  1933. if (xdr_stream_decode_u64(argp->xdr, &listxattrs->lsxa_cookie) < 0)
  1934. return nfserr_bad_xdr;
  1935. /*
  1936. * If the cookie is too large to have even one user.x attribute
  1937. * plus trailing '\0' left in a maximum size buffer, it's invalid.
  1938. */
  1939. if (listxattrs->lsxa_cookie >=
  1940. (XATTR_LIST_MAX / (XATTR_USER_PREFIX_LEN + 2)))
  1941. return nfserr_badcookie;
  1942. if (xdr_stream_decode_u32(argp->xdr, &maxcount) < 0)
  1943. return nfserr_bad_xdr;
  1944. if (maxcount < 8)
  1945. /* Always need at least 2 words (length and one character) */
  1946. return nfserr_inval;
  1947. maxcount = min(maxcount, svc_max_payload(argp->rqstp));
  1948. listxattrs->lsxa_maxcount = maxcount;
  1949. return nfs_ok;
  1950. }
  1951. static __be32
  1952. nfsd4_decode_removexattr(struct nfsd4_compoundargs *argp,
  1953. struct nfsd4_removexattr *removexattr)
  1954. {
  1955. memset(removexattr, 0, sizeof(*removexattr));
  1956. return nfsd4_decode_xattr_name(argp, &removexattr->rmxa_name);
  1957. }
  1958. static __be32
  1959. nfsd4_decode_noop(struct nfsd4_compoundargs *argp, void *p)
  1960. {
  1961. return nfs_ok;
  1962. }
  1963. static __be32
  1964. nfsd4_decode_notsupp(struct nfsd4_compoundargs *argp, void *p)
  1965. {
  1966. return nfserr_notsupp;
  1967. }
  1968. typedef __be32(*nfsd4_dec)(struct nfsd4_compoundargs *argp, void *);
  1969. static const nfsd4_dec nfsd4_dec_ops[] = {
  1970. [OP_ACCESS] = (nfsd4_dec)nfsd4_decode_access,
  1971. [OP_CLOSE] = (nfsd4_dec)nfsd4_decode_close,
  1972. [OP_COMMIT] = (nfsd4_dec)nfsd4_decode_commit,
  1973. [OP_CREATE] = (nfsd4_dec)nfsd4_decode_create,
  1974. [OP_DELEGPURGE] = (nfsd4_dec)nfsd4_decode_notsupp,
  1975. [OP_DELEGRETURN] = (nfsd4_dec)nfsd4_decode_delegreturn,
  1976. [OP_GETATTR] = (nfsd4_dec)nfsd4_decode_getattr,
  1977. [OP_GETFH] = (nfsd4_dec)nfsd4_decode_noop,
  1978. [OP_LINK] = (nfsd4_dec)nfsd4_decode_link,
  1979. [OP_LOCK] = (nfsd4_dec)nfsd4_decode_lock,
  1980. [OP_LOCKT] = (nfsd4_dec)nfsd4_decode_lockt,
  1981. [OP_LOCKU] = (nfsd4_dec)nfsd4_decode_locku,
  1982. [OP_LOOKUP] = (nfsd4_dec)nfsd4_decode_lookup,
  1983. [OP_LOOKUPP] = (nfsd4_dec)nfsd4_decode_noop,
  1984. [OP_NVERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  1985. [OP_OPEN] = (nfsd4_dec)nfsd4_decode_open,
  1986. [OP_OPENATTR] = (nfsd4_dec)nfsd4_decode_notsupp,
  1987. [OP_OPEN_CONFIRM] = (nfsd4_dec)nfsd4_decode_open_confirm,
  1988. [OP_OPEN_DOWNGRADE] = (nfsd4_dec)nfsd4_decode_open_downgrade,
  1989. [OP_PUTFH] = (nfsd4_dec)nfsd4_decode_putfh,
  1990. [OP_PUTPUBFH] = (nfsd4_dec)nfsd4_decode_putpubfh,
  1991. [OP_PUTROOTFH] = (nfsd4_dec)nfsd4_decode_noop,
  1992. [OP_READ] = (nfsd4_dec)nfsd4_decode_read,
  1993. [OP_READDIR] = (nfsd4_dec)nfsd4_decode_readdir,
  1994. [OP_READLINK] = (nfsd4_dec)nfsd4_decode_noop,
  1995. [OP_REMOVE] = (nfsd4_dec)nfsd4_decode_remove,
  1996. [OP_RENAME] = (nfsd4_dec)nfsd4_decode_rename,
  1997. [OP_RENEW] = (nfsd4_dec)nfsd4_decode_renew,
  1998. [OP_RESTOREFH] = (nfsd4_dec)nfsd4_decode_noop,
  1999. [OP_SAVEFH] = (nfsd4_dec)nfsd4_decode_noop,
  2000. [OP_SECINFO] = (nfsd4_dec)nfsd4_decode_secinfo,
  2001. [OP_SETATTR] = (nfsd4_dec)nfsd4_decode_setattr,
  2002. [OP_SETCLIENTID] = (nfsd4_dec)nfsd4_decode_setclientid,
  2003. [OP_SETCLIENTID_CONFIRM] = (nfsd4_dec)nfsd4_decode_setclientid_confirm,
  2004. [OP_VERIFY] = (nfsd4_dec)nfsd4_decode_verify,
  2005. [OP_WRITE] = (nfsd4_dec)nfsd4_decode_write,
  2006. [OP_RELEASE_LOCKOWNER] = (nfsd4_dec)nfsd4_decode_release_lockowner,
  2007. /* new operations for NFSv4.1 */
  2008. [OP_BACKCHANNEL_CTL] = (nfsd4_dec)nfsd4_decode_backchannel_ctl,
  2009. [OP_BIND_CONN_TO_SESSION]= (nfsd4_dec)nfsd4_decode_bind_conn_to_session,
  2010. [OP_EXCHANGE_ID] = (nfsd4_dec)nfsd4_decode_exchange_id,
  2011. [OP_CREATE_SESSION] = (nfsd4_dec)nfsd4_decode_create_session,
  2012. [OP_DESTROY_SESSION] = (nfsd4_dec)nfsd4_decode_destroy_session,
  2013. [OP_FREE_STATEID] = (nfsd4_dec)nfsd4_decode_free_stateid,
  2014. [OP_GET_DIR_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  2015. #ifdef CONFIG_NFSD_PNFS
  2016. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_getdeviceinfo,
  2017. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  2018. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_layoutcommit,
  2019. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_layoutget,
  2020. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_layoutreturn,
  2021. #else
  2022. [OP_GETDEVICEINFO] = (nfsd4_dec)nfsd4_decode_notsupp,
  2023. [OP_GETDEVICELIST] = (nfsd4_dec)nfsd4_decode_notsupp,
  2024. [OP_LAYOUTCOMMIT] = (nfsd4_dec)nfsd4_decode_notsupp,
  2025. [OP_LAYOUTGET] = (nfsd4_dec)nfsd4_decode_notsupp,
  2026. [OP_LAYOUTRETURN] = (nfsd4_dec)nfsd4_decode_notsupp,
  2027. #endif
  2028. [OP_SECINFO_NO_NAME] = (nfsd4_dec)nfsd4_decode_secinfo_no_name,
  2029. [OP_SEQUENCE] = (nfsd4_dec)nfsd4_decode_sequence,
  2030. [OP_SET_SSV] = (nfsd4_dec)nfsd4_decode_notsupp,
  2031. [OP_TEST_STATEID] = (nfsd4_dec)nfsd4_decode_test_stateid,
  2032. [OP_WANT_DELEGATION] = (nfsd4_dec)nfsd4_decode_notsupp,
  2033. [OP_DESTROY_CLIENTID] = (nfsd4_dec)nfsd4_decode_destroy_clientid,
  2034. [OP_RECLAIM_COMPLETE] = (nfsd4_dec)nfsd4_decode_reclaim_complete,
  2035. /* new operations for NFSv4.2 */
  2036. [OP_ALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
  2037. [OP_COPY] = (nfsd4_dec)nfsd4_decode_copy,
  2038. [OP_COPY_NOTIFY] = (nfsd4_dec)nfsd4_decode_copy_notify,
  2039. [OP_DEALLOCATE] = (nfsd4_dec)nfsd4_decode_fallocate,
  2040. [OP_IO_ADVISE] = (nfsd4_dec)nfsd4_decode_notsupp,
  2041. [OP_LAYOUTERROR] = (nfsd4_dec)nfsd4_decode_notsupp,
  2042. [OP_LAYOUTSTATS] = (nfsd4_dec)nfsd4_decode_notsupp,
  2043. [OP_OFFLOAD_CANCEL] = (nfsd4_dec)nfsd4_decode_offload_status,
  2044. [OP_OFFLOAD_STATUS] = (nfsd4_dec)nfsd4_decode_offload_status,
  2045. [OP_READ_PLUS] = (nfsd4_dec)nfsd4_decode_read,
  2046. [OP_SEEK] = (nfsd4_dec)nfsd4_decode_seek,
  2047. [OP_WRITE_SAME] = (nfsd4_dec)nfsd4_decode_notsupp,
  2048. [OP_CLONE] = (nfsd4_dec)nfsd4_decode_clone,
  2049. /* RFC 8276 extended atributes operations */
  2050. [OP_GETXATTR] = (nfsd4_dec)nfsd4_decode_getxattr,
  2051. [OP_SETXATTR] = (nfsd4_dec)nfsd4_decode_setxattr,
  2052. [OP_LISTXATTRS] = (nfsd4_dec)nfsd4_decode_listxattrs,
  2053. [OP_REMOVEXATTR] = (nfsd4_dec)nfsd4_decode_removexattr,
  2054. };
  2055. static inline bool
  2056. nfsd4_opnum_in_range(struct nfsd4_compoundargs *argp, struct nfsd4_op *op)
  2057. {
  2058. if (op->opnum < FIRST_NFS4_OP)
  2059. return false;
  2060. else if (argp->minorversion == 0 && op->opnum > LAST_NFS40_OP)
  2061. return false;
  2062. else if (argp->minorversion == 1 && op->opnum > LAST_NFS41_OP)
  2063. return false;
  2064. else if (argp->minorversion == 2 && op->opnum > LAST_NFS42_OP)
  2065. return false;
  2066. return true;
  2067. }
  2068. static bool
  2069. nfsd4_decode_compound(struct nfsd4_compoundargs *argp)
  2070. {
  2071. struct nfsd4_op *op;
  2072. bool cachethis = false;
  2073. int auth_slack= argp->rqstp->rq_auth_slack;
  2074. int max_reply = auth_slack + 8; /* opcnt, status */
  2075. int readcount = 0;
  2076. int readbytes = 0;
  2077. __be32 *p;
  2078. int i;
  2079. if (xdr_stream_decode_u32(argp->xdr, &argp->taglen) < 0)
  2080. return false;
  2081. max_reply += XDR_UNIT;
  2082. argp->tag = NULL;
  2083. if (unlikely(argp->taglen)) {
  2084. if (argp->taglen > NFSD4_MAX_TAGLEN)
  2085. return false;
  2086. p = xdr_inline_decode(argp->xdr, argp->taglen);
  2087. if (!p)
  2088. return false;
  2089. argp->tag = svcxdr_savemem(argp, p, argp->taglen);
  2090. if (!argp->tag)
  2091. return false;
  2092. max_reply += xdr_align_size(argp->taglen);
  2093. }
  2094. if (xdr_stream_decode_u32(argp->xdr, &argp->minorversion) < 0)
  2095. return false;
  2096. if (xdr_stream_decode_u32(argp->xdr, &argp->client_opcnt) < 0)
  2097. return false;
  2098. argp->opcnt = min_t(u32, argp->client_opcnt,
  2099. NFSD_MAX_OPS_PER_COMPOUND);
  2100. if (argp->opcnt > ARRAY_SIZE(argp->iops)) {
  2101. argp->ops = vcalloc(argp->opcnt, sizeof(*argp->ops));
  2102. if (!argp->ops) {
  2103. argp->ops = argp->iops;
  2104. return false;
  2105. }
  2106. }
  2107. if (argp->minorversion > NFSD_SUPPORTED_MINOR_VERSION)
  2108. argp->opcnt = 0;
  2109. for (i = 0; i < argp->opcnt; i++) {
  2110. op = &argp->ops[i];
  2111. op->replay = NULL;
  2112. op->opdesc = NULL;
  2113. if (xdr_stream_decode_u32(argp->xdr, &op->opnum) < 0)
  2114. return false;
  2115. if (nfsd4_opnum_in_range(argp, op)) {
  2116. op->opdesc = OPDESC(op);
  2117. op->status = nfsd4_dec_ops[op->opnum](argp, &op->u);
  2118. if (op->status != nfs_ok)
  2119. trace_nfsd_compound_decode_err(argp->rqstp,
  2120. argp->opcnt, i,
  2121. op->opnum,
  2122. op->status);
  2123. } else {
  2124. op->opnum = OP_ILLEGAL;
  2125. op->status = nfserr_op_illegal;
  2126. }
  2127. /*
  2128. * We'll try to cache the result in the DRC if any one
  2129. * op in the compound wants to be cached:
  2130. */
  2131. cachethis |= nfsd4_cache_this_op(op);
  2132. if (op->opnum == OP_READ || op->opnum == OP_READ_PLUS) {
  2133. readcount++;
  2134. readbytes += nfsd4_max_reply(argp->rqstp, op);
  2135. } else
  2136. max_reply += nfsd4_max_reply(argp->rqstp, op);
  2137. /*
  2138. * OP_LOCK and OP_LOCKT may return a conflicting lock.
  2139. * (Special case because it will just skip encoding this
  2140. * if it runs out of xdr buffer space, and it is the only
  2141. * operation that behaves this way.)
  2142. */
  2143. if (op->opnum == OP_LOCK || op->opnum == OP_LOCKT)
  2144. max_reply += NFS4_OPAQUE_LIMIT;
  2145. if (op->status) {
  2146. argp->opcnt = i+1;
  2147. break;
  2148. }
  2149. }
  2150. /* Sessions make the DRC unnecessary: */
  2151. if (argp->minorversion)
  2152. cachethis = false;
  2153. svc_reserve(argp->rqstp, max_reply + readbytes);
  2154. argp->rqstp->rq_cachetype = cachethis ? RC_REPLBUFF : RC_NOCACHE;
  2155. if (readcount > 1 || max_reply > PAGE_SIZE - auth_slack)
  2156. clear_bit(RQ_SPLICE_OK, &argp->rqstp->rq_flags);
  2157. return true;
  2158. }
  2159. static __be32 *encode_change(__be32 *p, struct kstat *stat, struct inode *inode,
  2160. struct svc_export *exp)
  2161. {
  2162. if (exp->ex_flags & NFSEXP_V4ROOT) {
  2163. *p++ = cpu_to_be32(convert_to_wallclock(exp->cd->flush_time));
  2164. *p++ = 0;
  2165. } else
  2166. p = xdr_encode_hyper(p, nfsd4_change_attribute(stat, inode));
  2167. return p;
  2168. }
  2169. /*
  2170. * ctime (in NFSv4, time_metadata) is not writeable, and the client
  2171. * doesn't really care what resolution could theoretically be stored by
  2172. * the filesystem.
  2173. *
  2174. * The client cares how close together changes can be while still
  2175. * guaranteeing ctime changes. For most filesystems (which have
  2176. * timestamps with nanosecond fields) that is limited by the resolution
  2177. * of the time returned from current_time() (which I'm assuming to be
  2178. * 1/HZ).
  2179. */
  2180. static __be32 *encode_time_delta(__be32 *p, struct inode *inode)
  2181. {
  2182. struct timespec64 ts;
  2183. u32 ns;
  2184. ns = max_t(u32, NSEC_PER_SEC/HZ, inode->i_sb->s_time_gran);
  2185. ts = ns_to_timespec64(ns);
  2186. p = xdr_encode_hyper(p, ts.tv_sec);
  2187. *p++ = cpu_to_be32(ts.tv_nsec);
  2188. return p;
  2189. }
  2190. static __be32 *encode_cinfo(__be32 *p, struct nfsd4_change_info *c)
  2191. {
  2192. *p++ = cpu_to_be32(c->atomic);
  2193. p = xdr_encode_hyper(p, c->before_change);
  2194. p = xdr_encode_hyper(p, c->after_change);
  2195. return p;
  2196. }
  2197. /* Encode as an array of strings the string given with components
  2198. * separated @sep, escaped with esc_enter and esc_exit.
  2199. */
  2200. static __be32 nfsd4_encode_components_esc(struct xdr_stream *xdr, char sep,
  2201. char *components, char esc_enter,
  2202. char esc_exit)
  2203. {
  2204. __be32 *p;
  2205. __be32 pathlen;
  2206. int pathlen_offset;
  2207. int strlen, count=0;
  2208. char *str, *end, *next;
  2209. dprintk("nfsd4_encode_components(%s)\n", components);
  2210. pathlen_offset = xdr->buf->len;
  2211. p = xdr_reserve_space(xdr, 4);
  2212. if (!p)
  2213. return nfserr_resource;
  2214. p++; /* We will fill this in with @count later */
  2215. end = str = components;
  2216. while (*end) {
  2217. bool found_esc = false;
  2218. /* try to parse as esc_start, ..., esc_end, sep */
  2219. if (*str == esc_enter) {
  2220. for (; *end && (*end != esc_exit); end++)
  2221. /* find esc_exit or end of string */;
  2222. next = end + 1;
  2223. if (*end && (!*next || *next == sep)) {
  2224. str++;
  2225. found_esc = true;
  2226. }
  2227. }
  2228. if (!found_esc)
  2229. for (; *end && (*end != sep); end++)
  2230. /* find sep or end of string */;
  2231. strlen = end - str;
  2232. if (strlen) {
  2233. p = xdr_reserve_space(xdr, strlen + 4);
  2234. if (!p)
  2235. return nfserr_resource;
  2236. p = xdr_encode_opaque(p, str, strlen);
  2237. count++;
  2238. }
  2239. else
  2240. end++;
  2241. if (found_esc)
  2242. end = next;
  2243. str = end;
  2244. }
  2245. pathlen = htonl(count);
  2246. write_bytes_to_xdr_buf(xdr->buf, pathlen_offset, &pathlen, 4);
  2247. return 0;
  2248. }
  2249. /* Encode as an array of strings the string given with components
  2250. * separated @sep.
  2251. */
  2252. static __be32 nfsd4_encode_components(struct xdr_stream *xdr, char sep,
  2253. char *components)
  2254. {
  2255. return nfsd4_encode_components_esc(xdr, sep, components, 0, 0);
  2256. }
  2257. /*
  2258. * encode a location element of a fs_locations structure
  2259. */
  2260. static __be32 nfsd4_encode_fs_location4(struct xdr_stream *xdr,
  2261. struct nfsd4_fs_location *location)
  2262. {
  2263. __be32 status;
  2264. status = nfsd4_encode_components_esc(xdr, ':', location->hosts,
  2265. '[', ']');
  2266. if (status)
  2267. return status;
  2268. status = nfsd4_encode_components(xdr, '/', location->path);
  2269. if (status)
  2270. return status;
  2271. return 0;
  2272. }
  2273. /*
  2274. * Encode a path in RFC3530 'pathname4' format
  2275. */
  2276. static __be32 nfsd4_encode_path(struct xdr_stream *xdr,
  2277. const struct path *root,
  2278. const struct path *path)
  2279. {
  2280. struct path cur = *path;
  2281. __be32 *p;
  2282. struct dentry **components = NULL;
  2283. unsigned int ncomponents = 0;
  2284. __be32 err = nfserr_jukebox;
  2285. dprintk("nfsd4_encode_components(");
  2286. path_get(&cur);
  2287. /* First walk the path up to the nfsd root, and store the
  2288. * dentries/path components in an array.
  2289. */
  2290. for (;;) {
  2291. if (path_equal(&cur, root))
  2292. break;
  2293. if (cur.dentry == cur.mnt->mnt_root) {
  2294. if (follow_up(&cur))
  2295. continue;
  2296. goto out_free;
  2297. }
  2298. if ((ncomponents & 15) == 0) {
  2299. struct dentry **new;
  2300. new = krealloc(components,
  2301. sizeof(*new) * (ncomponents + 16),
  2302. GFP_KERNEL);
  2303. if (!new)
  2304. goto out_free;
  2305. components = new;
  2306. }
  2307. components[ncomponents++] = cur.dentry;
  2308. cur.dentry = dget_parent(cur.dentry);
  2309. }
  2310. err = nfserr_resource;
  2311. p = xdr_reserve_space(xdr, 4);
  2312. if (!p)
  2313. goto out_free;
  2314. *p++ = cpu_to_be32(ncomponents);
  2315. while (ncomponents) {
  2316. struct dentry *dentry = components[ncomponents - 1];
  2317. unsigned int len;
  2318. spin_lock(&dentry->d_lock);
  2319. len = dentry->d_name.len;
  2320. p = xdr_reserve_space(xdr, len + 4);
  2321. if (!p) {
  2322. spin_unlock(&dentry->d_lock);
  2323. goto out_free;
  2324. }
  2325. p = xdr_encode_opaque(p, dentry->d_name.name, len);
  2326. dprintk("/%pd", dentry);
  2327. spin_unlock(&dentry->d_lock);
  2328. dput(dentry);
  2329. ncomponents--;
  2330. }
  2331. err = 0;
  2332. out_free:
  2333. dprintk(")\n");
  2334. while (ncomponents)
  2335. dput(components[--ncomponents]);
  2336. kfree(components);
  2337. path_put(&cur);
  2338. return err;
  2339. }
  2340. static __be32 nfsd4_encode_fsloc_fsroot(struct xdr_stream *xdr,
  2341. struct svc_rqst *rqstp, const struct path *path)
  2342. {
  2343. struct svc_export *exp_ps;
  2344. __be32 res;
  2345. exp_ps = rqst_find_fsidzero_export(rqstp);
  2346. if (IS_ERR(exp_ps))
  2347. return nfserrno(PTR_ERR(exp_ps));
  2348. res = nfsd4_encode_path(xdr, &exp_ps->ex_path, path);
  2349. exp_put(exp_ps);
  2350. return res;
  2351. }
  2352. /*
  2353. * encode a fs_locations structure
  2354. */
  2355. static __be32 nfsd4_encode_fs_locations(struct xdr_stream *xdr,
  2356. struct svc_rqst *rqstp, struct svc_export *exp)
  2357. {
  2358. __be32 status;
  2359. int i;
  2360. __be32 *p;
  2361. struct nfsd4_fs_locations *fslocs = &exp->ex_fslocs;
  2362. status = nfsd4_encode_fsloc_fsroot(xdr, rqstp, &exp->ex_path);
  2363. if (status)
  2364. return status;
  2365. p = xdr_reserve_space(xdr, 4);
  2366. if (!p)
  2367. return nfserr_resource;
  2368. *p++ = cpu_to_be32(fslocs->locations_count);
  2369. for (i=0; i<fslocs->locations_count; i++) {
  2370. status = nfsd4_encode_fs_location4(xdr, &fslocs->locations[i]);
  2371. if (status)
  2372. return status;
  2373. }
  2374. return 0;
  2375. }
  2376. static u32 nfs4_file_type(umode_t mode)
  2377. {
  2378. switch (mode & S_IFMT) {
  2379. case S_IFIFO: return NF4FIFO;
  2380. case S_IFCHR: return NF4CHR;
  2381. case S_IFDIR: return NF4DIR;
  2382. case S_IFBLK: return NF4BLK;
  2383. case S_IFLNK: return NF4LNK;
  2384. case S_IFREG: return NF4REG;
  2385. case S_IFSOCK: return NF4SOCK;
  2386. default: return NF4BAD;
  2387. }
  2388. }
  2389. static inline __be32
  2390. nfsd4_encode_aclname(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  2391. struct nfs4_ace *ace)
  2392. {
  2393. if (ace->whotype != NFS4_ACL_WHO_NAMED)
  2394. return nfs4_acl_write_who(xdr, ace->whotype);
  2395. else if (ace->flag & NFS4_ACE_IDENTIFIER_GROUP)
  2396. return nfsd4_encode_group(xdr, rqstp, ace->who_gid);
  2397. else
  2398. return nfsd4_encode_user(xdr, rqstp, ace->who_uid);
  2399. }
  2400. static inline __be32
  2401. nfsd4_encode_layout_types(struct xdr_stream *xdr, u32 layout_types)
  2402. {
  2403. __be32 *p;
  2404. unsigned long i = hweight_long(layout_types);
  2405. p = xdr_reserve_space(xdr, 4 + 4 * i);
  2406. if (!p)
  2407. return nfserr_resource;
  2408. *p++ = cpu_to_be32(i);
  2409. for (i = LAYOUT_NFSV4_1_FILES; i < LAYOUT_TYPE_MAX; ++i)
  2410. if (layout_types & (1 << i))
  2411. *p++ = cpu_to_be32(i);
  2412. return 0;
  2413. }
  2414. #define WORD0_ABSENT_FS_ATTRS (FATTR4_WORD0_FS_LOCATIONS | FATTR4_WORD0_FSID | \
  2415. FATTR4_WORD0_RDATTR_ERROR)
  2416. #define WORD1_ABSENT_FS_ATTRS FATTR4_WORD1_MOUNTED_ON_FILEID
  2417. #define WORD2_ABSENT_FS_ATTRS 0
  2418. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2419. static inline __be32
  2420. nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  2421. void *context, int len)
  2422. {
  2423. __be32 *p;
  2424. p = xdr_reserve_space(xdr, len + 4 + 4 + 4);
  2425. if (!p)
  2426. return nfserr_resource;
  2427. /*
  2428. * For now we use a 0 here to indicate the null translation; in
  2429. * the future we may place a call to translation code here.
  2430. */
  2431. *p++ = cpu_to_be32(0); /* lfs */
  2432. *p++ = cpu_to_be32(0); /* pi */
  2433. p = xdr_encode_opaque(p, context, len);
  2434. return 0;
  2435. }
  2436. #else
  2437. static inline __be32
  2438. nfsd4_encode_security_label(struct xdr_stream *xdr, struct svc_rqst *rqstp,
  2439. void *context, int len)
  2440. { return 0; }
  2441. #endif
  2442. static __be32 fattr_handle_absent_fs(u32 *bmval0, u32 *bmval1, u32 *bmval2, u32 *rdattr_err)
  2443. {
  2444. /* As per referral draft: */
  2445. if (*bmval0 & ~WORD0_ABSENT_FS_ATTRS ||
  2446. *bmval1 & ~WORD1_ABSENT_FS_ATTRS) {
  2447. if (*bmval0 & FATTR4_WORD0_RDATTR_ERROR ||
  2448. *bmval0 & FATTR4_WORD0_FS_LOCATIONS)
  2449. *rdattr_err = NFSERR_MOVED;
  2450. else
  2451. return nfserr_moved;
  2452. }
  2453. *bmval0 &= WORD0_ABSENT_FS_ATTRS;
  2454. *bmval1 &= WORD1_ABSENT_FS_ATTRS;
  2455. *bmval2 &= WORD2_ABSENT_FS_ATTRS;
  2456. return 0;
  2457. }
  2458. static int nfsd4_get_mounted_on_ino(struct svc_export *exp, u64 *pino)
  2459. {
  2460. struct path path = exp->ex_path;
  2461. struct kstat stat;
  2462. int err;
  2463. path_get(&path);
  2464. while (follow_up(&path)) {
  2465. if (path.dentry != path.mnt->mnt_root)
  2466. break;
  2467. }
  2468. err = vfs_getattr(&path, &stat, STATX_INO, AT_STATX_SYNC_AS_STAT);
  2469. path_put(&path);
  2470. if (!err)
  2471. *pino = stat.ino;
  2472. return err;
  2473. }
  2474. static __be32
  2475. nfsd4_encode_bitmap(struct xdr_stream *xdr, u32 bmval0, u32 bmval1, u32 bmval2)
  2476. {
  2477. __be32 *p;
  2478. if (bmval2) {
  2479. p = xdr_reserve_space(xdr, 16);
  2480. if (!p)
  2481. goto out_resource;
  2482. *p++ = cpu_to_be32(3);
  2483. *p++ = cpu_to_be32(bmval0);
  2484. *p++ = cpu_to_be32(bmval1);
  2485. *p++ = cpu_to_be32(bmval2);
  2486. } else if (bmval1) {
  2487. p = xdr_reserve_space(xdr, 12);
  2488. if (!p)
  2489. goto out_resource;
  2490. *p++ = cpu_to_be32(2);
  2491. *p++ = cpu_to_be32(bmval0);
  2492. *p++ = cpu_to_be32(bmval1);
  2493. } else {
  2494. p = xdr_reserve_space(xdr, 8);
  2495. if (!p)
  2496. goto out_resource;
  2497. *p++ = cpu_to_be32(1);
  2498. *p++ = cpu_to_be32(bmval0);
  2499. }
  2500. return 0;
  2501. out_resource:
  2502. return nfserr_resource;
  2503. }
  2504. /*
  2505. * Note: @fhp can be NULL; in this case, we might have to compose the filehandle
  2506. * ourselves.
  2507. */
  2508. static __be32
  2509. nfsd4_encode_fattr(struct xdr_stream *xdr, struct svc_fh *fhp,
  2510. struct svc_export *exp,
  2511. struct dentry *dentry, u32 *bmval,
  2512. struct svc_rqst *rqstp, int ignore_crossmnt)
  2513. {
  2514. u32 bmval0 = bmval[0];
  2515. u32 bmval1 = bmval[1];
  2516. u32 bmval2 = bmval[2];
  2517. struct kstat stat;
  2518. struct svc_fh *tempfh = NULL;
  2519. struct kstatfs statfs;
  2520. __be32 *p, *attrlen_p;
  2521. int starting_len = xdr->buf->len;
  2522. int attrlen_offset;
  2523. u32 dummy;
  2524. u64 dummy64;
  2525. u32 rdattr_err = 0;
  2526. __be32 status;
  2527. int err;
  2528. struct nfs4_acl *acl = NULL;
  2529. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2530. void *context = NULL;
  2531. int contextlen;
  2532. #endif
  2533. bool contextsupport = false;
  2534. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2535. u32 minorversion = resp->cstate.minorversion;
  2536. struct path path = {
  2537. .mnt = exp->ex_path.mnt,
  2538. .dentry = dentry,
  2539. };
  2540. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2541. BUG_ON(bmval1 & NFSD_WRITEONLY_ATTRS_WORD1);
  2542. BUG_ON(!nfsd_attrs_supported(minorversion, bmval));
  2543. if (exp->ex_fslocs.migrated) {
  2544. status = fattr_handle_absent_fs(&bmval0, &bmval1, &bmval2, &rdattr_err);
  2545. if (status)
  2546. goto out;
  2547. }
  2548. err = vfs_getattr(&path, &stat, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
  2549. if (err)
  2550. goto out_nfserr;
  2551. if (!(stat.result_mask & STATX_BTIME))
  2552. /* underlying FS does not offer btime so we can't share it */
  2553. bmval1 &= ~FATTR4_WORD1_TIME_CREATE;
  2554. if ((bmval0 & (FATTR4_WORD0_FILES_AVAIL | FATTR4_WORD0_FILES_FREE |
  2555. FATTR4_WORD0_FILES_TOTAL | FATTR4_WORD0_MAXNAME)) ||
  2556. (bmval1 & (FATTR4_WORD1_SPACE_AVAIL | FATTR4_WORD1_SPACE_FREE |
  2557. FATTR4_WORD1_SPACE_TOTAL))) {
  2558. err = vfs_statfs(&path, &statfs);
  2559. if (err)
  2560. goto out_nfserr;
  2561. }
  2562. if ((bmval0 & (FATTR4_WORD0_FILEHANDLE | FATTR4_WORD0_FSID)) && !fhp) {
  2563. tempfh = kmalloc(sizeof(struct svc_fh), GFP_KERNEL);
  2564. status = nfserr_jukebox;
  2565. if (!tempfh)
  2566. goto out;
  2567. fh_init(tempfh, NFS4_FHSIZE);
  2568. status = fh_compose(tempfh, exp, dentry, NULL);
  2569. if (status)
  2570. goto out;
  2571. fhp = tempfh;
  2572. }
  2573. if (bmval0 & FATTR4_WORD0_ACL) {
  2574. err = nfsd4_get_nfs4_acl(rqstp, dentry, &acl);
  2575. if (err == -EOPNOTSUPP)
  2576. bmval0 &= ~FATTR4_WORD0_ACL;
  2577. else if (err == -EINVAL) {
  2578. status = nfserr_attrnotsupp;
  2579. goto out;
  2580. } else if (err != 0)
  2581. goto out_nfserr;
  2582. }
  2583. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2584. if ((bmval2 & FATTR4_WORD2_SECURITY_LABEL) ||
  2585. bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  2586. if (exp->ex_flags & NFSEXP_SECURITY_LABEL)
  2587. err = security_inode_getsecctx(d_inode(dentry),
  2588. &context, &contextlen);
  2589. else
  2590. err = -EOPNOTSUPP;
  2591. contextsupport = (err == 0);
  2592. if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
  2593. if (err == -EOPNOTSUPP)
  2594. bmval2 &= ~FATTR4_WORD2_SECURITY_LABEL;
  2595. else if (err)
  2596. goto out_nfserr;
  2597. }
  2598. }
  2599. #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
  2600. status = nfsd4_encode_bitmap(xdr, bmval0, bmval1, bmval2);
  2601. if (status)
  2602. goto out;
  2603. attrlen_offset = xdr->buf->len;
  2604. attrlen_p = xdr_reserve_space(xdr, XDR_UNIT);
  2605. if (!attrlen_p)
  2606. goto out_resource;
  2607. if (bmval0 & FATTR4_WORD0_SUPPORTED_ATTRS) {
  2608. u32 supp[3];
  2609. memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
  2610. if (!IS_POSIXACL(dentry->d_inode))
  2611. supp[0] &= ~FATTR4_WORD0_ACL;
  2612. if (!contextsupport)
  2613. supp[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
  2614. if (!supp[2]) {
  2615. p = xdr_reserve_space(xdr, 12);
  2616. if (!p)
  2617. goto out_resource;
  2618. *p++ = cpu_to_be32(2);
  2619. *p++ = cpu_to_be32(supp[0]);
  2620. *p++ = cpu_to_be32(supp[1]);
  2621. } else {
  2622. p = xdr_reserve_space(xdr, 16);
  2623. if (!p)
  2624. goto out_resource;
  2625. *p++ = cpu_to_be32(3);
  2626. *p++ = cpu_to_be32(supp[0]);
  2627. *p++ = cpu_to_be32(supp[1]);
  2628. *p++ = cpu_to_be32(supp[2]);
  2629. }
  2630. }
  2631. if (bmval0 & FATTR4_WORD0_TYPE) {
  2632. p = xdr_reserve_space(xdr, 4);
  2633. if (!p)
  2634. goto out_resource;
  2635. dummy = nfs4_file_type(stat.mode);
  2636. if (dummy == NF4BAD) {
  2637. status = nfserr_serverfault;
  2638. goto out;
  2639. }
  2640. *p++ = cpu_to_be32(dummy);
  2641. }
  2642. if (bmval0 & FATTR4_WORD0_FH_EXPIRE_TYPE) {
  2643. p = xdr_reserve_space(xdr, 4);
  2644. if (!p)
  2645. goto out_resource;
  2646. if (exp->ex_flags & NFSEXP_NOSUBTREECHECK)
  2647. *p++ = cpu_to_be32(NFS4_FH_PERSISTENT);
  2648. else
  2649. *p++ = cpu_to_be32(NFS4_FH_PERSISTENT|
  2650. NFS4_FH_VOL_RENAME);
  2651. }
  2652. if (bmval0 & FATTR4_WORD0_CHANGE) {
  2653. p = xdr_reserve_space(xdr, 8);
  2654. if (!p)
  2655. goto out_resource;
  2656. p = encode_change(p, &stat, d_inode(dentry), exp);
  2657. }
  2658. if (bmval0 & FATTR4_WORD0_SIZE) {
  2659. p = xdr_reserve_space(xdr, 8);
  2660. if (!p)
  2661. goto out_resource;
  2662. p = xdr_encode_hyper(p, stat.size);
  2663. }
  2664. if (bmval0 & FATTR4_WORD0_LINK_SUPPORT) {
  2665. p = xdr_reserve_space(xdr, 4);
  2666. if (!p)
  2667. goto out_resource;
  2668. *p++ = cpu_to_be32(1);
  2669. }
  2670. if (bmval0 & FATTR4_WORD0_SYMLINK_SUPPORT) {
  2671. p = xdr_reserve_space(xdr, 4);
  2672. if (!p)
  2673. goto out_resource;
  2674. *p++ = cpu_to_be32(1);
  2675. }
  2676. if (bmval0 & FATTR4_WORD0_NAMED_ATTR) {
  2677. p = xdr_reserve_space(xdr, 4);
  2678. if (!p)
  2679. goto out_resource;
  2680. *p++ = cpu_to_be32(0);
  2681. }
  2682. if (bmval0 & FATTR4_WORD0_FSID) {
  2683. p = xdr_reserve_space(xdr, 16);
  2684. if (!p)
  2685. goto out_resource;
  2686. if (exp->ex_fslocs.migrated) {
  2687. p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MAJOR);
  2688. p = xdr_encode_hyper(p, NFS4_REFERRAL_FSID_MINOR);
  2689. } else switch(fsid_source(fhp)) {
  2690. case FSIDSOURCE_FSID:
  2691. p = xdr_encode_hyper(p, (u64)exp->ex_fsid);
  2692. p = xdr_encode_hyper(p, (u64)0);
  2693. break;
  2694. case FSIDSOURCE_DEV:
  2695. *p++ = cpu_to_be32(0);
  2696. *p++ = cpu_to_be32(MAJOR(stat.dev));
  2697. *p++ = cpu_to_be32(0);
  2698. *p++ = cpu_to_be32(MINOR(stat.dev));
  2699. break;
  2700. case FSIDSOURCE_UUID:
  2701. p = xdr_encode_opaque_fixed(p, exp->ex_uuid,
  2702. EX_UUID_LEN);
  2703. break;
  2704. }
  2705. }
  2706. if (bmval0 & FATTR4_WORD0_UNIQUE_HANDLES) {
  2707. p = xdr_reserve_space(xdr, 4);
  2708. if (!p)
  2709. goto out_resource;
  2710. *p++ = cpu_to_be32(0);
  2711. }
  2712. if (bmval0 & FATTR4_WORD0_LEASE_TIME) {
  2713. p = xdr_reserve_space(xdr, 4);
  2714. if (!p)
  2715. goto out_resource;
  2716. *p++ = cpu_to_be32(nn->nfsd4_lease);
  2717. }
  2718. if (bmval0 & FATTR4_WORD0_RDATTR_ERROR) {
  2719. p = xdr_reserve_space(xdr, 4);
  2720. if (!p)
  2721. goto out_resource;
  2722. *p++ = cpu_to_be32(rdattr_err);
  2723. }
  2724. if (bmval0 & FATTR4_WORD0_ACL) {
  2725. struct nfs4_ace *ace;
  2726. if (acl == NULL) {
  2727. p = xdr_reserve_space(xdr, 4);
  2728. if (!p)
  2729. goto out_resource;
  2730. *p++ = cpu_to_be32(0);
  2731. goto out_acl;
  2732. }
  2733. p = xdr_reserve_space(xdr, 4);
  2734. if (!p)
  2735. goto out_resource;
  2736. *p++ = cpu_to_be32(acl->naces);
  2737. for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) {
  2738. p = xdr_reserve_space(xdr, 4*3);
  2739. if (!p)
  2740. goto out_resource;
  2741. *p++ = cpu_to_be32(ace->type);
  2742. *p++ = cpu_to_be32(ace->flag);
  2743. *p++ = cpu_to_be32(ace->access_mask &
  2744. NFS4_ACE_MASK_ALL);
  2745. status = nfsd4_encode_aclname(xdr, rqstp, ace);
  2746. if (status)
  2747. goto out;
  2748. }
  2749. }
  2750. out_acl:
  2751. if (bmval0 & FATTR4_WORD0_ACLSUPPORT) {
  2752. p = xdr_reserve_space(xdr, 4);
  2753. if (!p)
  2754. goto out_resource;
  2755. *p++ = cpu_to_be32(IS_POSIXACL(dentry->d_inode) ?
  2756. ACL4_SUPPORT_ALLOW_ACL|ACL4_SUPPORT_DENY_ACL : 0);
  2757. }
  2758. if (bmval0 & FATTR4_WORD0_CANSETTIME) {
  2759. p = xdr_reserve_space(xdr, 4);
  2760. if (!p)
  2761. goto out_resource;
  2762. *p++ = cpu_to_be32(1);
  2763. }
  2764. if (bmval0 & FATTR4_WORD0_CASE_INSENSITIVE) {
  2765. p = xdr_reserve_space(xdr, 4);
  2766. if (!p)
  2767. goto out_resource;
  2768. *p++ = cpu_to_be32(0);
  2769. }
  2770. if (bmval0 & FATTR4_WORD0_CASE_PRESERVING) {
  2771. p = xdr_reserve_space(xdr, 4);
  2772. if (!p)
  2773. goto out_resource;
  2774. *p++ = cpu_to_be32(1);
  2775. }
  2776. if (bmval0 & FATTR4_WORD0_CHOWN_RESTRICTED) {
  2777. p = xdr_reserve_space(xdr, 4);
  2778. if (!p)
  2779. goto out_resource;
  2780. *p++ = cpu_to_be32(1);
  2781. }
  2782. if (bmval0 & FATTR4_WORD0_FILEHANDLE) {
  2783. p = xdr_reserve_space(xdr, fhp->fh_handle.fh_size + 4);
  2784. if (!p)
  2785. goto out_resource;
  2786. p = xdr_encode_opaque(p, &fhp->fh_handle.fh_raw,
  2787. fhp->fh_handle.fh_size);
  2788. }
  2789. if (bmval0 & FATTR4_WORD0_FILEID) {
  2790. p = xdr_reserve_space(xdr, 8);
  2791. if (!p)
  2792. goto out_resource;
  2793. p = xdr_encode_hyper(p, stat.ino);
  2794. }
  2795. if (bmval0 & FATTR4_WORD0_FILES_AVAIL) {
  2796. p = xdr_reserve_space(xdr, 8);
  2797. if (!p)
  2798. goto out_resource;
  2799. p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
  2800. }
  2801. if (bmval0 & FATTR4_WORD0_FILES_FREE) {
  2802. p = xdr_reserve_space(xdr, 8);
  2803. if (!p)
  2804. goto out_resource;
  2805. p = xdr_encode_hyper(p, (u64) statfs.f_ffree);
  2806. }
  2807. if (bmval0 & FATTR4_WORD0_FILES_TOTAL) {
  2808. p = xdr_reserve_space(xdr, 8);
  2809. if (!p)
  2810. goto out_resource;
  2811. p = xdr_encode_hyper(p, (u64) statfs.f_files);
  2812. }
  2813. if (bmval0 & FATTR4_WORD0_FS_LOCATIONS) {
  2814. status = nfsd4_encode_fs_locations(xdr, rqstp, exp);
  2815. if (status)
  2816. goto out;
  2817. }
  2818. if (bmval0 & FATTR4_WORD0_HOMOGENEOUS) {
  2819. p = xdr_reserve_space(xdr, 4);
  2820. if (!p)
  2821. goto out_resource;
  2822. *p++ = cpu_to_be32(1);
  2823. }
  2824. if (bmval0 & FATTR4_WORD0_MAXFILESIZE) {
  2825. p = xdr_reserve_space(xdr, 8);
  2826. if (!p)
  2827. goto out_resource;
  2828. p = xdr_encode_hyper(p, exp->ex_path.mnt->mnt_sb->s_maxbytes);
  2829. }
  2830. if (bmval0 & FATTR4_WORD0_MAXLINK) {
  2831. p = xdr_reserve_space(xdr, 4);
  2832. if (!p)
  2833. goto out_resource;
  2834. *p++ = cpu_to_be32(255);
  2835. }
  2836. if (bmval0 & FATTR4_WORD0_MAXNAME) {
  2837. p = xdr_reserve_space(xdr, 4);
  2838. if (!p)
  2839. goto out_resource;
  2840. *p++ = cpu_to_be32(statfs.f_namelen);
  2841. }
  2842. if (bmval0 & FATTR4_WORD0_MAXREAD) {
  2843. p = xdr_reserve_space(xdr, 8);
  2844. if (!p)
  2845. goto out_resource;
  2846. p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
  2847. }
  2848. if (bmval0 & FATTR4_WORD0_MAXWRITE) {
  2849. p = xdr_reserve_space(xdr, 8);
  2850. if (!p)
  2851. goto out_resource;
  2852. p = xdr_encode_hyper(p, (u64) svc_max_payload(rqstp));
  2853. }
  2854. if (bmval1 & FATTR4_WORD1_MODE) {
  2855. p = xdr_reserve_space(xdr, 4);
  2856. if (!p)
  2857. goto out_resource;
  2858. *p++ = cpu_to_be32(stat.mode & S_IALLUGO);
  2859. }
  2860. if (bmval1 & FATTR4_WORD1_NO_TRUNC) {
  2861. p = xdr_reserve_space(xdr, 4);
  2862. if (!p)
  2863. goto out_resource;
  2864. *p++ = cpu_to_be32(1);
  2865. }
  2866. if (bmval1 & FATTR4_WORD1_NUMLINKS) {
  2867. p = xdr_reserve_space(xdr, 4);
  2868. if (!p)
  2869. goto out_resource;
  2870. *p++ = cpu_to_be32(stat.nlink);
  2871. }
  2872. if (bmval1 & FATTR4_WORD1_OWNER) {
  2873. status = nfsd4_encode_user(xdr, rqstp, stat.uid);
  2874. if (status)
  2875. goto out;
  2876. }
  2877. if (bmval1 & FATTR4_WORD1_OWNER_GROUP) {
  2878. status = nfsd4_encode_group(xdr, rqstp, stat.gid);
  2879. if (status)
  2880. goto out;
  2881. }
  2882. if (bmval1 & FATTR4_WORD1_RAWDEV) {
  2883. p = xdr_reserve_space(xdr, 8);
  2884. if (!p)
  2885. goto out_resource;
  2886. *p++ = cpu_to_be32((u32) MAJOR(stat.rdev));
  2887. *p++ = cpu_to_be32((u32) MINOR(stat.rdev));
  2888. }
  2889. if (bmval1 & FATTR4_WORD1_SPACE_AVAIL) {
  2890. p = xdr_reserve_space(xdr, 8);
  2891. if (!p)
  2892. goto out_resource;
  2893. dummy64 = (u64)statfs.f_bavail * (u64)statfs.f_bsize;
  2894. p = xdr_encode_hyper(p, dummy64);
  2895. }
  2896. if (bmval1 & FATTR4_WORD1_SPACE_FREE) {
  2897. p = xdr_reserve_space(xdr, 8);
  2898. if (!p)
  2899. goto out_resource;
  2900. dummy64 = (u64)statfs.f_bfree * (u64)statfs.f_bsize;
  2901. p = xdr_encode_hyper(p, dummy64);
  2902. }
  2903. if (bmval1 & FATTR4_WORD1_SPACE_TOTAL) {
  2904. p = xdr_reserve_space(xdr, 8);
  2905. if (!p)
  2906. goto out_resource;
  2907. dummy64 = (u64)statfs.f_blocks * (u64)statfs.f_bsize;
  2908. p = xdr_encode_hyper(p, dummy64);
  2909. }
  2910. if (bmval1 & FATTR4_WORD1_SPACE_USED) {
  2911. p = xdr_reserve_space(xdr, 8);
  2912. if (!p)
  2913. goto out_resource;
  2914. dummy64 = (u64)stat.blocks << 9;
  2915. p = xdr_encode_hyper(p, dummy64);
  2916. }
  2917. if (bmval1 & FATTR4_WORD1_TIME_ACCESS) {
  2918. p = xdr_reserve_space(xdr, 12);
  2919. if (!p)
  2920. goto out_resource;
  2921. p = xdr_encode_hyper(p, (s64)stat.atime.tv_sec);
  2922. *p++ = cpu_to_be32(stat.atime.tv_nsec);
  2923. }
  2924. if (bmval1 & FATTR4_WORD1_TIME_DELTA) {
  2925. p = xdr_reserve_space(xdr, 12);
  2926. if (!p)
  2927. goto out_resource;
  2928. p = encode_time_delta(p, d_inode(dentry));
  2929. }
  2930. if (bmval1 & FATTR4_WORD1_TIME_METADATA) {
  2931. p = xdr_reserve_space(xdr, 12);
  2932. if (!p)
  2933. goto out_resource;
  2934. p = xdr_encode_hyper(p, (s64)stat.ctime.tv_sec);
  2935. *p++ = cpu_to_be32(stat.ctime.tv_nsec);
  2936. }
  2937. if (bmval1 & FATTR4_WORD1_TIME_MODIFY) {
  2938. p = xdr_reserve_space(xdr, 12);
  2939. if (!p)
  2940. goto out_resource;
  2941. p = xdr_encode_hyper(p, (s64)stat.mtime.tv_sec);
  2942. *p++ = cpu_to_be32(stat.mtime.tv_nsec);
  2943. }
  2944. if (bmval1 & FATTR4_WORD1_TIME_CREATE) {
  2945. p = xdr_reserve_space(xdr, 12);
  2946. if (!p)
  2947. goto out_resource;
  2948. p = xdr_encode_hyper(p, (s64)stat.btime.tv_sec);
  2949. *p++ = cpu_to_be32(stat.btime.tv_nsec);
  2950. }
  2951. if (bmval1 & FATTR4_WORD1_MOUNTED_ON_FILEID) {
  2952. u64 ino = stat.ino;
  2953. p = xdr_reserve_space(xdr, 8);
  2954. if (!p)
  2955. goto out_resource;
  2956. /*
  2957. * Get ino of mountpoint in parent filesystem, if not ignoring
  2958. * crossmount and this is the root of a cross-mounted
  2959. * filesystem.
  2960. */
  2961. if (ignore_crossmnt == 0 &&
  2962. dentry == exp->ex_path.mnt->mnt_root) {
  2963. err = nfsd4_get_mounted_on_ino(exp, &ino);
  2964. if (err)
  2965. goto out_nfserr;
  2966. }
  2967. p = xdr_encode_hyper(p, ino);
  2968. }
  2969. #ifdef CONFIG_NFSD_PNFS
  2970. if (bmval1 & FATTR4_WORD1_FS_LAYOUT_TYPES) {
  2971. status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
  2972. if (status)
  2973. goto out;
  2974. }
  2975. if (bmval2 & FATTR4_WORD2_LAYOUT_TYPES) {
  2976. status = nfsd4_encode_layout_types(xdr, exp->ex_layout_types);
  2977. if (status)
  2978. goto out;
  2979. }
  2980. if (bmval2 & FATTR4_WORD2_LAYOUT_BLKSIZE) {
  2981. p = xdr_reserve_space(xdr, 4);
  2982. if (!p)
  2983. goto out_resource;
  2984. *p++ = cpu_to_be32(stat.blksize);
  2985. }
  2986. #endif /* CONFIG_NFSD_PNFS */
  2987. if (bmval2 & FATTR4_WORD2_SUPPATTR_EXCLCREAT) {
  2988. u32 supp[3];
  2989. memcpy(supp, nfsd_suppattrs[minorversion], sizeof(supp));
  2990. supp[0] &= NFSD_SUPPATTR_EXCLCREAT_WORD0;
  2991. supp[1] &= NFSD_SUPPATTR_EXCLCREAT_WORD1;
  2992. supp[2] &= NFSD_SUPPATTR_EXCLCREAT_WORD2;
  2993. status = nfsd4_encode_bitmap(xdr, supp[0], supp[1], supp[2]);
  2994. if (status)
  2995. goto out;
  2996. }
  2997. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  2998. if (bmval2 & FATTR4_WORD2_SECURITY_LABEL) {
  2999. status = nfsd4_encode_security_label(xdr, rqstp, context,
  3000. contextlen);
  3001. if (status)
  3002. goto out;
  3003. }
  3004. #endif
  3005. if (bmval2 & FATTR4_WORD2_XATTR_SUPPORT) {
  3006. p = xdr_reserve_space(xdr, 4);
  3007. if (!p)
  3008. goto out_resource;
  3009. err = xattr_supported_namespace(d_inode(dentry),
  3010. XATTR_USER_PREFIX);
  3011. *p++ = cpu_to_be32(err == 0);
  3012. }
  3013. *attrlen_p = cpu_to_be32(xdr->buf->len - attrlen_offset - XDR_UNIT);
  3014. status = nfs_ok;
  3015. out:
  3016. #ifdef CONFIG_NFSD_V4_SECURITY_LABEL
  3017. if (context)
  3018. security_release_secctx(context, contextlen);
  3019. #endif /* CONFIG_NFSD_V4_SECURITY_LABEL */
  3020. kfree(acl);
  3021. if (tempfh) {
  3022. fh_put(tempfh);
  3023. kfree(tempfh);
  3024. }
  3025. if (status)
  3026. xdr_truncate_encode(xdr, starting_len);
  3027. return status;
  3028. out_nfserr:
  3029. status = nfserrno(err);
  3030. goto out;
  3031. out_resource:
  3032. status = nfserr_resource;
  3033. goto out;
  3034. }
  3035. static void svcxdr_init_encode_from_buffer(struct xdr_stream *xdr,
  3036. struct xdr_buf *buf, __be32 *p, int bytes)
  3037. {
  3038. xdr->scratch.iov_len = 0;
  3039. memset(buf, 0, sizeof(struct xdr_buf));
  3040. buf->head[0].iov_base = p;
  3041. buf->head[0].iov_len = 0;
  3042. buf->len = 0;
  3043. xdr->buf = buf;
  3044. xdr->iov = buf->head;
  3045. xdr->p = p;
  3046. xdr->end = (void *)p + bytes;
  3047. buf->buflen = bytes;
  3048. }
  3049. __be32 nfsd4_encode_fattr_to_buf(__be32 **p, int words,
  3050. struct svc_fh *fhp, struct svc_export *exp,
  3051. struct dentry *dentry, u32 *bmval,
  3052. struct svc_rqst *rqstp, int ignore_crossmnt)
  3053. {
  3054. struct xdr_buf dummy;
  3055. struct xdr_stream xdr;
  3056. __be32 ret;
  3057. svcxdr_init_encode_from_buffer(&xdr, &dummy, *p, words << 2);
  3058. ret = nfsd4_encode_fattr(&xdr, fhp, exp, dentry, bmval, rqstp,
  3059. ignore_crossmnt);
  3060. *p = xdr.p;
  3061. return ret;
  3062. }
  3063. static inline int attributes_need_mount(u32 *bmval)
  3064. {
  3065. if (bmval[0] & ~(FATTR4_WORD0_RDATTR_ERROR | FATTR4_WORD0_LEASE_TIME))
  3066. return 1;
  3067. if (bmval[1] & ~FATTR4_WORD1_MOUNTED_ON_FILEID)
  3068. return 1;
  3069. return 0;
  3070. }
  3071. static __be32
  3072. nfsd4_encode_dirent_fattr(struct xdr_stream *xdr, struct nfsd4_readdir *cd,
  3073. const char *name, int namlen)
  3074. {
  3075. struct svc_export *exp = cd->rd_fhp->fh_export;
  3076. struct dentry *dentry;
  3077. __be32 nfserr;
  3078. int ignore_crossmnt = 0;
  3079. dentry = lookup_positive_unlocked(name, cd->rd_fhp->fh_dentry, namlen);
  3080. if (IS_ERR(dentry))
  3081. return nfserrno(PTR_ERR(dentry));
  3082. exp_get(exp);
  3083. /*
  3084. * In the case of a mountpoint, the client may be asking for
  3085. * attributes that are only properties of the underlying filesystem
  3086. * as opposed to the cross-mounted file system. In such a case,
  3087. * we will not follow the cross mount and will fill the attribtutes
  3088. * directly from the mountpoint dentry.
  3089. */
  3090. if (nfsd_mountpoint(dentry, exp)) {
  3091. int err;
  3092. if (!(exp->ex_flags & NFSEXP_V4ROOT)
  3093. && !attributes_need_mount(cd->rd_bmval)) {
  3094. ignore_crossmnt = 1;
  3095. goto out_encode;
  3096. }
  3097. /*
  3098. * Why the heck aren't we just using nfsd_lookup??
  3099. * Different "."/".." handling? Something else?
  3100. * At least, add a comment here to explain....
  3101. */
  3102. err = nfsd_cross_mnt(cd->rd_rqstp, &dentry, &exp);
  3103. if (err) {
  3104. nfserr = nfserrno(err);
  3105. goto out_put;
  3106. }
  3107. nfserr = check_nfsd_access(exp, cd->rd_rqstp);
  3108. if (nfserr)
  3109. goto out_put;
  3110. }
  3111. out_encode:
  3112. nfserr = nfsd4_encode_fattr(xdr, NULL, exp, dentry, cd->rd_bmval,
  3113. cd->rd_rqstp, ignore_crossmnt);
  3114. out_put:
  3115. dput(dentry);
  3116. exp_put(exp);
  3117. return nfserr;
  3118. }
  3119. static __be32 *
  3120. nfsd4_encode_rdattr_error(struct xdr_stream *xdr, __be32 nfserr)
  3121. {
  3122. __be32 *p;
  3123. p = xdr_reserve_space(xdr, 20);
  3124. if (!p)
  3125. return NULL;
  3126. *p++ = htonl(2);
  3127. *p++ = htonl(FATTR4_WORD0_RDATTR_ERROR); /* bmval0 */
  3128. *p++ = htonl(0); /* bmval1 */
  3129. *p++ = htonl(4); /* attribute length */
  3130. *p++ = nfserr; /* no htonl */
  3131. return p;
  3132. }
  3133. static int
  3134. nfsd4_encode_dirent(void *ccdv, const char *name, int namlen,
  3135. loff_t offset, u64 ino, unsigned int d_type)
  3136. {
  3137. struct readdir_cd *ccd = ccdv;
  3138. struct nfsd4_readdir *cd = container_of(ccd, struct nfsd4_readdir, common);
  3139. struct xdr_stream *xdr = cd->xdr;
  3140. int start_offset = xdr->buf->len;
  3141. int cookie_offset;
  3142. u32 name_and_cookie;
  3143. int entry_bytes;
  3144. __be32 nfserr = nfserr_toosmall;
  3145. __be64 wire_offset;
  3146. __be32 *p;
  3147. /* In nfsv4, "." and ".." never make it onto the wire.. */
  3148. if (name && isdotent(name, namlen)) {
  3149. cd->common.err = nfs_ok;
  3150. return 0;
  3151. }
  3152. if (cd->cookie_offset) {
  3153. wire_offset = cpu_to_be64(offset);
  3154. write_bytes_to_xdr_buf(xdr->buf, cd->cookie_offset,
  3155. &wire_offset, 8);
  3156. }
  3157. p = xdr_reserve_space(xdr, 4);
  3158. if (!p)
  3159. goto fail;
  3160. *p++ = xdr_one; /* mark entry present */
  3161. cookie_offset = xdr->buf->len;
  3162. p = xdr_reserve_space(xdr, 3*4 + namlen);
  3163. if (!p)
  3164. goto fail;
  3165. p = xdr_encode_hyper(p, OFFSET_MAX); /* offset of next entry */
  3166. p = xdr_encode_array(p, name, namlen); /* name length & name */
  3167. nfserr = nfsd4_encode_dirent_fattr(xdr, cd, name, namlen);
  3168. switch (nfserr) {
  3169. case nfs_ok:
  3170. break;
  3171. case nfserr_resource:
  3172. nfserr = nfserr_toosmall;
  3173. goto fail;
  3174. case nfserr_noent:
  3175. xdr_truncate_encode(xdr, start_offset);
  3176. goto skip_entry;
  3177. case nfserr_jukebox:
  3178. /*
  3179. * The pseudoroot should only display dentries that lead to
  3180. * exports. If we get EJUKEBOX here, then we can't tell whether
  3181. * this entry should be included. Just fail the whole READDIR
  3182. * with NFS4ERR_DELAY in that case, and hope that the situation
  3183. * will resolve itself by the client's next attempt.
  3184. */
  3185. if (cd->rd_fhp->fh_export->ex_flags & NFSEXP_V4ROOT)
  3186. goto fail;
  3187. fallthrough;
  3188. default:
  3189. /*
  3190. * If the client requested the RDATTR_ERROR attribute,
  3191. * we stuff the error code into this attribute
  3192. * and continue. If this attribute was not requested,
  3193. * then in accordance with the spec, we fail the
  3194. * entire READDIR operation(!)
  3195. */
  3196. if (!(cd->rd_bmval[0] & FATTR4_WORD0_RDATTR_ERROR))
  3197. goto fail;
  3198. p = nfsd4_encode_rdattr_error(xdr, nfserr);
  3199. if (p == NULL) {
  3200. nfserr = nfserr_toosmall;
  3201. goto fail;
  3202. }
  3203. }
  3204. nfserr = nfserr_toosmall;
  3205. entry_bytes = xdr->buf->len - start_offset;
  3206. if (entry_bytes > cd->rd_maxcount)
  3207. goto fail;
  3208. cd->rd_maxcount -= entry_bytes;
  3209. /*
  3210. * RFC 3530 14.2.24 describes rd_dircount as only a "hint", and
  3211. * notes that it could be zero. If it is zero, then the server
  3212. * should enforce only the rd_maxcount value.
  3213. */
  3214. if (cd->rd_dircount) {
  3215. name_and_cookie = 4 + 4 * XDR_QUADLEN(namlen) + 8;
  3216. if (name_and_cookie > cd->rd_dircount && cd->cookie_offset)
  3217. goto fail;
  3218. cd->rd_dircount -= min(cd->rd_dircount, name_and_cookie);
  3219. if (!cd->rd_dircount)
  3220. cd->rd_maxcount = 0;
  3221. }
  3222. cd->cookie_offset = cookie_offset;
  3223. skip_entry:
  3224. cd->common.err = nfs_ok;
  3225. return 0;
  3226. fail:
  3227. xdr_truncate_encode(xdr, start_offset);
  3228. cd->common.err = nfserr;
  3229. return -EINVAL;
  3230. }
  3231. static __be32
  3232. nfsd4_encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
  3233. {
  3234. __be32 *p;
  3235. p = xdr_reserve_space(xdr, sizeof(stateid_t));
  3236. if (!p)
  3237. return nfserr_resource;
  3238. *p++ = cpu_to_be32(sid->si_generation);
  3239. p = xdr_encode_opaque_fixed(p, &sid->si_opaque,
  3240. sizeof(stateid_opaque_t));
  3241. return 0;
  3242. }
  3243. static __be32
  3244. nfsd4_encode_access(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_access *access)
  3245. {
  3246. struct xdr_stream *xdr = resp->xdr;
  3247. __be32 *p;
  3248. p = xdr_reserve_space(xdr, 8);
  3249. if (!p)
  3250. return nfserr_resource;
  3251. *p++ = cpu_to_be32(access->ac_supported);
  3252. *p++ = cpu_to_be32(access->ac_resp_access);
  3253. return 0;
  3254. }
  3255. static __be32 nfsd4_encode_bind_conn_to_session(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_bind_conn_to_session *bcts)
  3256. {
  3257. struct xdr_stream *xdr = resp->xdr;
  3258. __be32 *p;
  3259. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 8);
  3260. if (!p)
  3261. return nfserr_resource;
  3262. p = xdr_encode_opaque_fixed(p, bcts->sessionid.data,
  3263. NFS4_MAX_SESSIONID_LEN);
  3264. *p++ = cpu_to_be32(bcts->dir);
  3265. /* Upshifting from TCP to RDMA is not supported */
  3266. *p++ = cpu_to_be32(0);
  3267. return 0;
  3268. }
  3269. static __be32
  3270. nfsd4_encode_close(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_close *close)
  3271. {
  3272. struct xdr_stream *xdr = resp->xdr;
  3273. return nfsd4_encode_stateid(xdr, &close->cl_stateid);
  3274. }
  3275. static __be32
  3276. nfsd4_encode_commit(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_commit *commit)
  3277. {
  3278. struct xdr_stream *xdr = resp->xdr;
  3279. __be32 *p;
  3280. p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
  3281. if (!p)
  3282. return nfserr_resource;
  3283. p = xdr_encode_opaque_fixed(p, commit->co_verf.data,
  3284. NFS4_VERIFIER_SIZE);
  3285. return 0;
  3286. }
  3287. static __be32
  3288. nfsd4_encode_create(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_create *create)
  3289. {
  3290. struct xdr_stream *xdr = resp->xdr;
  3291. __be32 *p;
  3292. p = xdr_reserve_space(xdr, 20);
  3293. if (!p)
  3294. return nfserr_resource;
  3295. encode_cinfo(p, &create->cr_cinfo);
  3296. return nfsd4_encode_bitmap(xdr, create->cr_bmval[0],
  3297. create->cr_bmval[1], create->cr_bmval[2]);
  3298. }
  3299. static __be32
  3300. nfsd4_encode_getattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_getattr *getattr)
  3301. {
  3302. struct svc_fh *fhp = getattr->ga_fhp;
  3303. struct xdr_stream *xdr = resp->xdr;
  3304. return nfsd4_encode_fattr(xdr, fhp, fhp->fh_export, fhp->fh_dentry,
  3305. getattr->ga_bmval, resp->rqstp, 0);
  3306. }
  3307. static __be32
  3308. nfsd4_encode_getfh(struct nfsd4_compoundres *resp, __be32 nfserr, struct svc_fh **fhpp)
  3309. {
  3310. struct xdr_stream *xdr = resp->xdr;
  3311. struct svc_fh *fhp = *fhpp;
  3312. unsigned int len;
  3313. __be32 *p;
  3314. len = fhp->fh_handle.fh_size;
  3315. p = xdr_reserve_space(xdr, len + 4);
  3316. if (!p)
  3317. return nfserr_resource;
  3318. p = xdr_encode_opaque(p, &fhp->fh_handle.fh_raw, len);
  3319. return 0;
  3320. }
  3321. /*
  3322. * Including all fields other than the name, a LOCK4denied structure requires
  3323. * 8(clientid) + 4(namelen) + 8(offset) + 8(length) + 4(type) = 32 bytes.
  3324. */
  3325. static __be32
  3326. nfsd4_encode_lock_denied(struct xdr_stream *xdr, struct nfsd4_lock_denied *ld)
  3327. {
  3328. struct xdr_netobj *conf = &ld->ld_owner;
  3329. __be32 *p;
  3330. again:
  3331. p = xdr_reserve_space(xdr, 32 + XDR_LEN(conf->len));
  3332. if (!p) {
  3333. /*
  3334. * Don't fail to return the result just because we can't
  3335. * return the conflicting open:
  3336. */
  3337. if (conf->len) {
  3338. kfree(conf->data);
  3339. conf->len = 0;
  3340. conf->data = NULL;
  3341. goto again;
  3342. }
  3343. return nfserr_resource;
  3344. }
  3345. p = xdr_encode_hyper(p, ld->ld_start);
  3346. p = xdr_encode_hyper(p, ld->ld_length);
  3347. *p++ = cpu_to_be32(ld->ld_type);
  3348. if (conf->len) {
  3349. p = xdr_encode_opaque_fixed(p, &ld->ld_clientid, 8);
  3350. p = xdr_encode_opaque(p, conf->data, conf->len);
  3351. kfree(conf->data);
  3352. } else { /* non - nfsv4 lock in conflict, no clientid nor owner */
  3353. p = xdr_encode_hyper(p, (u64)0); /* clientid */
  3354. *p++ = cpu_to_be32(0); /* length of owner name */
  3355. }
  3356. return nfserr_denied;
  3357. }
  3358. static __be32
  3359. nfsd4_encode_lock(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lock *lock)
  3360. {
  3361. struct xdr_stream *xdr = resp->xdr;
  3362. if (!nfserr)
  3363. nfserr = nfsd4_encode_stateid(xdr, &lock->lk_resp_stateid);
  3364. else if (nfserr == nfserr_denied)
  3365. nfserr = nfsd4_encode_lock_denied(xdr, &lock->lk_denied);
  3366. return nfserr;
  3367. }
  3368. static __be32
  3369. nfsd4_encode_lockt(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_lockt *lockt)
  3370. {
  3371. struct xdr_stream *xdr = resp->xdr;
  3372. if (nfserr == nfserr_denied)
  3373. nfsd4_encode_lock_denied(xdr, &lockt->lt_denied);
  3374. return nfserr;
  3375. }
  3376. static __be32
  3377. nfsd4_encode_locku(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_locku *locku)
  3378. {
  3379. struct xdr_stream *xdr = resp->xdr;
  3380. return nfsd4_encode_stateid(xdr, &locku->lu_stateid);
  3381. }
  3382. static __be32
  3383. nfsd4_encode_link(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_link *link)
  3384. {
  3385. struct xdr_stream *xdr = resp->xdr;
  3386. __be32 *p;
  3387. p = xdr_reserve_space(xdr, 20);
  3388. if (!p)
  3389. return nfserr_resource;
  3390. p = encode_cinfo(p, &link->li_cinfo);
  3391. return 0;
  3392. }
  3393. static __be32
  3394. nfsd4_encode_open(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open *open)
  3395. {
  3396. struct xdr_stream *xdr = resp->xdr;
  3397. __be32 *p;
  3398. nfserr = nfsd4_encode_stateid(xdr, &open->op_stateid);
  3399. if (nfserr)
  3400. return nfserr;
  3401. p = xdr_reserve_space(xdr, 24);
  3402. if (!p)
  3403. return nfserr_resource;
  3404. p = encode_cinfo(p, &open->op_cinfo);
  3405. *p++ = cpu_to_be32(open->op_rflags);
  3406. nfserr = nfsd4_encode_bitmap(xdr, open->op_bmval[0], open->op_bmval[1],
  3407. open->op_bmval[2]);
  3408. if (nfserr)
  3409. return nfserr;
  3410. p = xdr_reserve_space(xdr, 4);
  3411. if (!p)
  3412. return nfserr_resource;
  3413. *p++ = cpu_to_be32(open->op_delegate_type);
  3414. switch (open->op_delegate_type) {
  3415. case NFS4_OPEN_DELEGATE_NONE:
  3416. break;
  3417. case NFS4_OPEN_DELEGATE_READ:
  3418. nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
  3419. if (nfserr)
  3420. return nfserr;
  3421. p = xdr_reserve_space(xdr, 20);
  3422. if (!p)
  3423. return nfserr_resource;
  3424. *p++ = cpu_to_be32(open->op_recall);
  3425. /*
  3426. * TODO: ACE's in delegations
  3427. */
  3428. *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  3429. *p++ = cpu_to_be32(0);
  3430. *p++ = cpu_to_be32(0);
  3431. *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
  3432. break;
  3433. case NFS4_OPEN_DELEGATE_WRITE:
  3434. nfserr = nfsd4_encode_stateid(xdr, &open->op_delegate_stateid);
  3435. if (nfserr)
  3436. return nfserr;
  3437. p = xdr_reserve_space(xdr, 32);
  3438. if (!p)
  3439. return nfserr_resource;
  3440. *p++ = cpu_to_be32(open->op_recall);
  3441. /*
  3442. * TODO: space_limit's in delegations
  3443. */
  3444. *p++ = cpu_to_be32(NFS4_LIMIT_SIZE);
  3445. *p++ = cpu_to_be32(~(u32)0);
  3446. *p++ = cpu_to_be32(~(u32)0);
  3447. /*
  3448. * TODO: ACE's in delegations
  3449. */
  3450. *p++ = cpu_to_be32(NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE);
  3451. *p++ = cpu_to_be32(0);
  3452. *p++ = cpu_to_be32(0);
  3453. *p++ = cpu_to_be32(0); /* XXX: is NULL principal ok? */
  3454. break;
  3455. case NFS4_OPEN_DELEGATE_NONE_EXT: /* 4.1 */
  3456. switch (open->op_why_no_deleg) {
  3457. case WND4_CONTENTION:
  3458. case WND4_RESOURCE:
  3459. p = xdr_reserve_space(xdr, 8);
  3460. if (!p)
  3461. return nfserr_resource;
  3462. *p++ = cpu_to_be32(open->op_why_no_deleg);
  3463. /* deleg signaling not supported yet: */
  3464. *p++ = cpu_to_be32(0);
  3465. break;
  3466. default:
  3467. p = xdr_reserve_space(xdr, 4);
  3468. if (!p)
  3469. return nfserr_resource;
  3470. *p++ = cpu_to_be32(open->op_why_no_deleg);
  3471. }
  3472. break;
  3473. default:
  3474. BUG();
  3475. }
  3476. /* XXX save filehandle here */
  3477. return 0;
  3478. }
  3479. static __be32
  3480. nfsd4_encode_open_confirm(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_confirm *oc)
  3481. {
  3482. struct xdr_stream *xdr = resp->xdr;
  3483. return nfsd4_encode_stateid(xdr, &oc->oc_resp_stateid);
  3484. }
  3485. static __be32
  3486. nfsd4_encode_open_downgrade(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_open_downgrade *od)
  3487. {
  3488. struct xdr_stream *xdr = resp->xdr;
  3489. return nfsd4_encode_stateid(xdr, &od->od_stateid);
  3490. }
  3491. static __be32 nfsd4_encode_splice_read(
  3492. struct nfsd4_compoundres *resp,
  3493. struct nfsd4_read *read,
  3494. struct file *file, unsigned long maxcount)
  3495. {
  3496. struct xdr_stream *xdr = resp->xdr;
  3497. struct xdr_buf *buf = xdr->buf;
  3498. int status, space_left;
  3499. __be32 nfserr;
  3500. /* Make sure there will be room for padding if needed */
  3501. if (xdr->end - xdr->p < 1)
  3502. return nfserr_resource;
  3503. nfserr = nfsd_splice_read(read->rd_rqstp, read->rd_fhp,
  3504. file, read->rd_offset, &maxcount,
  3505. &read->rd_eof);
  3506. read->rd_length = maxcount;
  3507. if (nfserr)
  3508. goto out_err;
  3509. status = svc_encode_result_payload(read->rd_rqstp,
  3510. buf->head[0].iov_len, maxcount);
  3511. if (status) {
  3512. nfserr = nfserrno(status);
  3513. goto out_err;
  3514. }
  3515. buf->page_len = maxcount;
  3516. buf->len += maxcount;
  3517. xdr->page_ptr += (buf->page_base + maxcount + PAGE_SIZE - 1)
  3518. / PAGE_SIZE;
  3519. /* Use rest of head for padding and remaining ops: */
  3520. buf->tail[0].iov_base = xdr->p;
  3521. buf->tail[0].iov_len = 0;
  3522. xdr->iov = buf->tail;
  3523. if (maxcount&3) {
  3524. int pad = 4 - (maxcount&3);
  3525. *(xdr->p++) = 0;
  3526. buf->tail[0].iov_base += maxcount&3;
  3527. buf->tail[0].iov_len = pad;
  3528. buf->len += pad;
  3529. }
  3530. space_left = min_t(int, (void *)xdr->end - (void *)xdr->p,
  3531. buf->buflen - buf->len);
  3532. buf->buflen = buf->len + space_left;
  3533. xdr->end = (__be32 *)((void *)xdr->end + space_left);
  3534. return 0;
  3535. out_err:
  3536. /*
  3537. * nfsd_splice_actor may have already messed with the
  3538. * page length; reset it so as not to confuse
  3539. * xdr_truncate_encode in our caller.
  3540. */
  3541. buf->page_len = 0;
  3542. return nfserr;
  3543. }
  3544. static __be32 nfsd4_encode_readv(struct nfsd4_compoundres *resp,
  3545. struct nfsd4_read *read,
  3546. struct file *file, unsigned long maxcount)
  3547. {
  3548. struct xdr_stream *xdr = resp->xdr;
  3549. unsigned int starting_len = xdr->buf->len;
  3550. __be32 zero = xdr_zero;
  3551. __be32 nfserr;
  3552. read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, maxcount);
  3553. if (read->rd_vlen < 0)
  3554. return nfserr_resource;
  3555. nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
  3556. resp->rqstp->rq_vec, read->rd_vlen, &maxcount,
  3557. &read->rd_eof);
  3558. read->rd_length = maxcount;
  3559. if (nfserr)
  3560. return nfserr;
  3561. if (svc_encode_result_payload(resp->rqstp, starting_len, maxcount))
  3562. return nfserr_io;
  3563. xdr_truncate_encode(xdr, starting_len + xdr_align_size(maxcount));
  3564. write_bytes_to_xdr_buf(xdr->buf, starting_len + maxcount, &zero,
  3565. xdr_pad_size(maxcount));
  3566. return nfs_ok;
  3567. }
  3568. static __be32
  3569. nfsd4_encode_read(struct nfsd4_compoundres *resp, __be32 nfserr,
  3570. struct nfsd4_read *read)
  3571. {
  3572. bool splice_ok = test_bit(RQ_SPLICE_OK, &resp->rqstp->rq_flags);
  3573. unsigned long maxcount;
  3574. struct xdr_stream *xdr = resp->xdr;
  3575. struct file *file;
  3576. int starting_len = xdr->buf->len;
  3577. __be32 *p;
  3578. if (nfserr)
  3579. return nfserr;
  3580. file = read->rd_nf->nf_file;
  3581. p = xdr_reserve_space(xdr, 8); /* eof flag and byte count */
  3582. if (!p) {
  3583. WARN_ON_ONCE(splice_ok);
  3584. return nfserr_resource;
  3585. }
  3586. if (resp->xdr->buf->page_len && splice_ok) {
  3587. WARN_ON_ONCE(1);
  3588. return nfserr_serverfault;
  3589. }
  3590. xdr_commit_encode(xdr);
  3591. maxcount = min_t(unsigned long, read->rd_length,
  3592. (xdr->buf->buflen - xdr->buf->len));
  3593. if (file->f_op->splice_read && splice_ok)
  3594. nfserr = nfsd4_encode_splice_read(resp, read, file, maxcount);
  3595. else
  3596. nfserr = nfsd4_encode_readv(resp, read, file, maxcount);
  3597. if (nfserr) {
  3598. xdr_truncate_encode(xdr, starting_len);
  3599. return nfserr;
  3600. }
  3601. p = xdr_encode_bool(p, read->rd_eof);
  3602. *p = cpu_to_be32(read->rd_length);
  3603. return nfs_ok;
  3604. }
  3605. static __be32
  3606. nfsd4_encode_readlink(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readlink *readlink)
  3607. {
  3608. __be32 *p, *maxcount_p, zero = xdr_zero;
  3609. struct xdr_stream *xdr = resp->xdr;
  3610. int length_offset = xdr->buf->len;
  3611. int maxcount, status;
  3612. maxcount_p = xdr_reserve_space(xdr, XDR_UNIT);
  3613. if (!maxcount_p)
  3614. return nfserr_resource;
  3615. maxcount = PAGE_SIZE;
  3616. p = xdr_reserve_space(xdr, maxcount);
  3617. if (!p)
  3618. return nfserr_resource;
  3619. /*
  3620. * XXX: By default, vfs_readlink() will truncate symlinks if they
  3621. * would overflow the buffer. Is this kosher in NFSv4? If not, one
  3622. * easy fix is: if vfs_readlink() precisely fills the buffer, assume
  3623. * that truncation occurred, and return NFS4ERR_RESOURCE.
  3624. */
  3625. nfserr = nfsd_readlink(readlink->rl_rqstp, readlink->rl_fhp,
  3626. (char *)p, &maxcount);
  3627. if (nfserr == nfserr_isdir)
  3628. nfserr = nfserr_inval;
  3629. if (nfserr)
  3630. goto out_err;
  3631. status = svc_encode_result_payload(readlink->rl_rqstp, length_offset,
  3632. maxcount);
  3633. if (status) {
  3634. nfserr = nfserrno(status);
  3635. goto out_err;
  3636. }
  3637. *maxcount_p = cpu_to_be32(maxcount);
  3638. xdr_truncate_encode(xdr, length_offset + 4 + xdr_align_size(maxcount));
  3639. write_bytes_to_xdr_buf(xdr->buf, length_offset + 4 + maxcount, &zero,
  3640. xdr_pad_size(maxcount));
  3641. return nfs_ok;
  3642. out_err:
  3643. xdr_truncate_encode(xdr, length_offset);
  3644. return nfserr;
  3645. }
  3646. static __be32
  3647. nfsd4_encode_readdir(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_readdir *readdir)
  3648. {
  3649. int maxcount;
  3650. int bytes_left;
  3651. loff_t offset;
  3652. __be64 wire_offset;
  3653. struct xdr_stream *xdr = resp->xdr;
  3654. int starting_len = xdr->buf->len;
  3655. __be32 *p;
  3656. p = xdr_reserve_space(xdr, NFS4_VERIFIER_SIZE);
  3657. if (!p)
  3658. return nfserr_resource;
  3659. /* XXX: Following NFSv3, we ignore the READDIR verifier for now. */
  3660. *p++ = cpu_to_be32(0);
  3661. *p++ = cpu_to_be32(0);
  3662. xdr->buf->head[0].iov_len = (char *)xdr->p -
  3663. (char *)xdr->buf->head[0].iov_base;
  3664. /*
  3665. * Number of bytes left for directory entries allowing for the
  3666. * final 8 bytes of the readdir and a following failed op:
  3667. */
  3668. bytes_left = xdr->buf->buflen - xdr->buf->len
  3669. - COMPOUND_ERR_SLACK_SPACE - 8;
  3670. if (bytes_left < 0) {
  3671. nfserr = nfserr_resource;
  3672. goto err_no_verf;
  3673. }
  3674. maxcount = svc_max_payload(resp->rqstp);
  3675. maxcount = min_t(u32, readdir->rd_maxcount, maxcount);
  3676. /*
  3677. * Note the rfc defines rd_maxcount as the size of the
  3678. * READDIR4resok structure, which includes the verifier above
  3679. * and the 8 bytes encoded at the end of this function:
  3680. */
  3681. if (maxcount < 16) {
  3682. nfserr = nfserr_toosmall;
  3683. goto err_no_verf;
  3684. }
  3685. maxcount = min_t(int, maxcount-16, bytes_left);
  3686. /* RFC 3530 14.2.24 allows us to ignore dircount when it's 0: */
  3687. if (!readdir->rd_dircount)
  3688. readdir->rd_dircount = svc_max_payload(resp->rqstp);
  3689. readdir->xdr = xdr;
  3690. readdir->rd_maxcount = maxcount;
  3691. readdir->common.err = 0;
  3692. readdir->cookie_offset = 0;
  3693. offset = readdir->rd_cookie;
  3694. nfserr = nfsd_readdir(readdir->rd_rqstp, readdir->rd_fhp,
  3695. &offset,
  3696. &readdir->common, nfsd4_encode_dirent);
  3697. if (nfserr == nfs_ok &&
  3698. readdir->common.err == nfserr_toosmall &&
  3699. xdr->buf->len == starting_len + 8) {
  3700. /* nothing encoded; which limit did we hit?: */
  3701. if (maxcount - 16 < bytes_left)
  3702. /* It was the fault of rd_maxcount: */
  3703. nfserr = nfserr_toosmall;
  3704. else
  3705. /* We ran out of buffer space: */
  3706. nfserr = nfserr_resource;
  3707. }
  3708. if (nfserr)
  3709. goto err_no_verf;
  3710. if (readdir->cookie_offset) {
  3711. wire_offset = cpu_to_be64(offset);
  3712. write_bytes_to_xdr_buf(xdr->buf, readdir->cookie_offset,
  3713. &wire_offset, 8);
  3714. }
  3715. p = xdr_reserve_space(xdr, 8);
  3716. if (!p) {
  3717. WARN_ON_ONCE(1);
  3718. goto err_no_verf;
  3719. }
  3720. *p++ = 0; /* no more entries */
  3721. *p++ = htonl(readdir->common.err == nfserr_eof);
  3722. return 0;
  3723. err_no_verf:
  3724. xdr_truncate_encode(xdr, starting_len);
  3725. return nfserr;
  3726. }
  3727. static __be32
  3728. nfsd4_encode_remove(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_remove *remove)
  3729. {
  3730. struct xdr_stream *xdr = resp->xdr;
  3731. __be32 *p;
  3732. p = xdr_reserve_space(xdr, 20);
  3733. if (!p)
  3734. return nfserr_resource;
  3735. p = encode_cinfo(p, &remove->rm_cinfo);
  3736. return 0;
  3737. }
  3738. static __be32
  3739. nfsd4_encode_rename(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_rename *rename)
  3740. {
  3741. struct xdr_stream *xdr = resp->xdr;
  3742. __be32 *p;
  3743. p = xdr_reserve_space(xdr, 40);
  3744. if (!p)
  3745. return nfserr_resource;
  3746. p = encode_cinfo(p, &rename->rn_sinfo);
  3747. p = encode_cinfo(p, &rename->rn_tinfo);
  3748. return 0;
  3749. }
  3750. static __be32
  3751. nfsd4_do_encode_secinfo(struct xdr_stream *xdr, struct svc_export *exp)
  3752. {
  3753. u32 i, nflavs, supported;
  3754. struct exp_flavor_info *flavs;
  3755. struct exp_flavor_info def_flavs[2];
  3756. __be32 *p, *flavorsp;
  3757. static bool report = true;
  3758. if (exp->ex_nflavors) {
  3759. flavs = exp->ex_flavors;
  3760. nflavs = exp->ex_nflavors;
  3761. } else { /* Handling of some defaults in absence of real secinfo: */
  3762. flavs = def_flavs;
  3763. if (exp->ex_client->flavour->flavour == RPC_AUTH_UNIX) {
  3764. nflavs = 2;
  3765. flavs[0].pseudoflavor = RPC_AUTH_UNIX;
  3766. flavs[1].pseudoflavor = RPC_AUTH_NULL;
  3767. } else if (exp->ex_client->flavour->flavour == RPC_AUTH_GSS) {
  3768. nflavs = 1;
  3769. flavs[0].pseudoflavor
  3770. = svcauth_gss_flavor(exp->ex_client);
  3771. } else {
  3772. nflavs = 1;
  3773. flavs[0].pseudoflavor
  3774. = exp->ex_client->flavour->flavour;
  3775. }
  3776. }
  3777. supported = 0;
  3778. p = xdr_reserve_space(xdr, 4);
  3779. if (!p)
  3780. return nfserr_resource;
  3781. flavorsp = p++; /* to be backfilled later */
  3782. for (i = 0; i < nflavs; i++) {
  3783. rpc_authflavor_t pf = flavs[i].pseudoflavor;
  3784. struct rpcsec_gss_info info;
  3785. if (rpcauth_get_gssinfo(pf, &info) == 0) {
  3786. supported++;
  3787. p = xdr_reserve_space(xdr, 4 + 4 +
  3788. XDR_LEN(info.oid.len) + 4 + 4);
  3789. if (!p)
  3790. return nfserr_resource;
  3791. *p++ = cpu_to_be32(RPC_AUTH_GSS);
  3792. p = xdr_encode_opaque(p, info.oid.data, info.oid.len);
  3793. *p++ = cpu_to_be32(info.qop);
  3794. *p++ = cpu_to_be32(info.service);
  3795. } else if (pf < RPC_AUTH_MAXFLAVOR) {
  3796. supported++;
  3797. p = xdr_reserve_space(xdr, 4);
  3798. if (!p)
  3799. return nfserr_resource;
  3800. *p++ = cpu_to_be32(pf);
  3801. } else {
  3802. if (report)
  3803. pr_warn("NFS: SECINFO: security flavor %u "
  3804. "is not supported\n", pf);
  3805. }
  3806. }
  3807. if (nflavs != supported)
  3808. report = false;
  3809. *flavorsp = htonl(supported);
  3810. return 0;
  3811. }
  3812. static __be32
  3813. nfsd4_encode_secinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  3814. struct nfsd4_secinfo *secinfo)
  3815. {
  3816. struct xdr_stream *xdr = resp->xdr;
  3817. return nfsd4_do_encode_secinfo(xdr, secinfo->si_exp);
  3818. }
  3819. static __be32
  3820. nfsd4_encode_secinfo_no_name(struct nfsd4_compoundres *resp, __be32 nfserr,
  3821. struct nfsd4_secinfo_no_name *secinfo)
  3822. {
  3823. struct xdr_stream *xdr = resp->xdr;
  3824. return nfsd4_do_encode_secinfo(xdr, secinfo->sin_exp);
  3825. }
  3826. /*
  3827. * The SETATTR encode routine is special -- it always encodes a bitmap,
  3828. * regardless of the error status.
  3829. */
  3830. static __be32
  3831. nfsd4_encode_setattr(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setattr *setattr)
  3832. {
  3833. struct xdr_stream *xdr = resp->xdr;
  3834. __be32 *p;
  3835. p = xdr_reserve_space(xdr, 16);
  3836. if (!p)
  3837. return nfserr_resource;
  3838. if (nfserr) {
  3839. *p++ = cpu_to_be32(3);
  3840. *p++ = cpu_to_be32(0);
  3841. *p++ = cpu_to_be32(0);
  3842. *p++ = cpu_to_be32(0);
  3843. }
  3844. else {
  3845. *p++ = cpu_to_be32(3);
  3846. *p++ = cpu_to_be32(setattr->sa_bmval[0]);
  3847. *p++ = cpu_to_be32(setattr->sa_bmval[1]);
  3848. *p++ = cpu_to_be32(setattr->sa_bmval[2]);
  3849. }
  3850. return nfserr;
  3851. }
  3852. static __be32
  3853. nfsd4_encode_setclientid(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_setclientid *scd)
  3854. {
  3855. struct xdr_stream *xdr = resp->xdr;
  3856. __be32 *p;
  3857. if (!nfserr) {
  3858. p = xdr_reserve_space(xdr, 8 + NFS4_VERIFIER_SIZE);
  3859. if (!p)
  3860. return nfserr_resource;
  3861. p = xdr_encode_opaque_fixed(p, &scd->se_clientid, 8);
  3862. p = xdr_encode_opaque_fixed(p, &scd->se_confirm,
  3863. NFS4_VERIFIER_SIZE);
  3864. }
  3865. else if (nfserr == nfserr_clid_inuse) {
  3866. p = xdr_reserve_space(xdr, 8);
  3867. if (!p)
  3868. return nfserr_resource;
  3869. *p++ = cpu_to_be32(0);
  3870. *p++ = cpu_to_be32(0);
  3871. }
  3872. return nfserr;
  3873. }
  3874. static __be32
  3875. nfsd4_encode_write(struct nfsd4_compoundres *resp, __be32 nfserr, struct nfsd4_write *write)
  3876. {
  3877. struct xdr_stream *xdr = resp->xdr;
  3878. __be32 *p;
  3879. p = xdr_reserve_space(xdr, 16);
  3880. if (!p)
  3881. return nfserr_resource;
  3882. *p++ = cpu_to_be32(write->wr_bytes_written);
  3883. *p++ = cpu_to_be32(write->wr_how_written);
  3884. p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
  3885. NFS4_VERIFIER_SIZE);
  3886. return 0;
  3887. }
  3888. static __be32
  3889. nfsd4_encode_exchange_id(struct nfsd4_compoundres *resp, __be32 nfserr,
  3890. struct nfsd4_exchange_id *exid)
  3891. {
  3892. struct xdr_stream *xdr = resp->xdr;
  3893. __be32 *p;
  3894. char *major_id;
  3895. char *server_scope;
  3896. int major_id_sz;
  3897. int server_scope_sz;
  3898. uint64_t minor_id = 0;
  3899. struct nfsd_net *nn = net_generic(SVC_NET(resp->rqstp), nfsd_net_id);
  3900. major_id = nn->nfsd_name;
  3901. major_id_sz = strlen(nn->nfsd_name);
  3902. server_scope = nn->nfsd_name;
  3903. server_scope_sz = strlen(nn->nfsd_name);
  3904. p = xdr_reserve_space(xdr,
  3905. 8 /* eir_clientid */ +
  3906. 4 /* eir_sequenceid */ +
  3907. 4 /* eir_flags */ +
  3908. 4 /* spr_how */);
  3909. if (!p)
  3910. return nfserr_resource;
  3911. p = xdr_encode_opaque_fixed(p, &exid->clientid, 8);
  3912. *p++ = cpu_to_be32(exid->seqid);
  3913. *p++ = cpu_to_be32(exid->flags);
  3914. *p++ = cpu_to_be32(exid->spa_how);
  3915. switch (exid->spa_how) {
  3916. case SP4_NONE:
  3917. break;
  3918. case SP4_MACH_CRED:
  3919. /* spo_must_enforce bitmap: */
  3920. nfserr = nfsd4_encode_bitmap(xdr,
  3921. exid->spo_must_enforce[0],
  3922. exid->spo_must_enforce[1],
  3923. exid->spo_must_enforce[2]);
  3924. if (nfserr)
  3925. return nfserr;
  3926. /* spo_must_allow bitmap: */
  3927. nfserr = nfsd4_encode_bitmap(xdr,
  3928. exid->spo_must_allow[0],
  3929. exid->spo_must_allow[1],
  3930. exid->spo_must_allow[2]);
  3931. if (nfserr)
  3932. return nfserr;
  3933. break;
  3934. default:
  3935. WARN_ON_ONCE(1);
  3936. }
  3937. p = xdr_reserve_space(xdr,
  3938. 8 /* so_minor_id */ +
  3939. 4 /* so_major_id.len */ +
  3940. (XDR_QUADLEN(major_id_sz) * 4) +
  3941. 4 /* eir_server_scope.len */ +
  3942. (XDR_QUADLEN(server_scope_sz) * 4) +
  3943. 4 /* eir_server_impl_id.count (0) */);
  3944. if (!p)
  3945. return nfserr_resource;
  3946. /* The server_owner struct */
  3947. p = xdr_encode_hyper(p, minor_id); /* Minor id */
  3948. /* major id */
  3949. p = xdr_encode_opaque(p, major_id, major_id_sz);
  3950. /* Server scope */
  3951. p = xdr_encode_opaque(p, server_scope, server_scope_sz);
  3952. /* Implementation id */
  3953. *p++ = cpu_to_be32(0); /* zero length nfs_impl_id4 array */
  3954. return 0;
  3955. }
  3956. static __be32
  3957. nfsd4_encode_create_session(struct nfsd4_compoundres *resp, __be32 nfserr,
  3958. struct nfsd4_create_session *sess)
  3959. {
  3960. struct xdr_stream *xdr = resp->xdr;
  3961. __be32 *p;
  3962. p = xdr_reserve_space(xdr, 24);
  3963. if (!p)
  3964. return nfserr_resource;
  3965. p = xdr_encode_opaque_fixed(p, sess->sessionid.data,
  3966. NFS4_MAX_SESSIONID_LEN);
  3967. *p++ = cpu_to_be32(sess->seqid);
  3968. *p++ = cpu_to_be32(sess->flags);
  3969. p = xdr_reserve_space(xdr, 28);
  3970. if (!p)
  3971. return nfserr_resource;
  3972. *p++ = cpu_to_be32(0); /* headerpadsz */
  3973. *p++ = cpu_to_be32(sess->fore_channel.maxreq_sz);
  3974. *p++ = cpu_to_be32(sess->fore_channel.maxresp_sz);
  3975. *p++ = cpu_to_be32(sess->fore_channel.maxresp_cached);
  3976. *p++ = cpu_to_be32(sess->fore_channel.maxops);
  3977. *p++ = cpu_to_be32(sess->fore_channel.maxreqs);
  3978. *p++ = cpu_to_be32(sess->fore_channel.nr_rdma_attrs);
  3979. if (sess->fore_channel.nr_rdma_attrs) {
  3980. p = xdr_reserve_space(xdr, 4);
  3981. if (!p)
  3982. return nfserr_resource;
  3983. *p++ = cpu_to_be32(sess->fore_channel.rdma_attrs);
  3984. }
  3985. p = xdr_reserve_space(xdr, 28);
  3986. if (!p)
  3987. return nfserr_resource;
  3988. *p++ = cpu_to_be32(0); /* headerpadsz */
  3989. *p++ = cpu_to_be32(sess->back_channel.maxreq_sz);
  3990. *p++ = cpu_to_be32(sess->back_channel.maxresp_sz);
  3991. *p++ = cpu_to_be32(sess->back_channel.maxresp_cached);
  3992. *p++ = cpu_to_be32(sess->back_channel.maxops);
  3993. *p++ = cpu_to_be32(sess->back_channel.maxreqs);
  3994. *p++ = cpu_to_be32(sess->back_channel.nr_rdma_attrs);
  3995. if (sess->back_channel.nr_rdma_attrs) {
  3996. p = xdr_reserve_space(xdr, 4);
  3997. if (!p)
  3998. return nfserr_resource;
  3999. *p++ = cpu_to_be32(sess->back_channel.rdma_attrs);
  4000. }
  4001. return 0;
  4002. }
  4003. static __be32
  4004. nfsd4_encode_sequence(struct nfsd4_compoundres *resp, __be32 nfserr,
  4005. struct nfsd4_sequence *seq)
  4006. {
  4007. struct xdr_stream *xdr = resp->xdr;
  4008. __be32 *p;
  4009. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN + 20);
  4010. if (!p)
  4011. return nfserr_resource;
  4012. p = xdr_encode_opaque_fixed(p, seq->sessionid.data,
  4013. NFS4_MAX_SESSIONID_LEN);
  4014. *p++ = cpu_to_be32(seq->seqid);
  4015. *p++ = cpu_to_be32(seq->slotid);
  4016. /* Note slotid's are numbered from zero: */
  4017. *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_highest_slotid */
  4018. *p++ = cpu_to_be32(seq->maxslots - 1); /* sr_target_highest_slotid */
  4019. *p++ = cpu_to_be32(seq->status_flags);
  4020. resp->cstate.data_offset = xdr->buf->len; /* DRC cache data pointer */
  4021. return 0;
  4022. }
  4023. static __be32
  4024. nfsd4_encode_test_stateid(struct nfsd4_compoundres *resp, __be32 nfserr,
  4025. struct nfsd4_test_stateid *test_stateid)
  4026. {
  4027. struct xdr_stream *xdr = resp->xdr;
  4028. struct nfsd4_test_stateid_id *stateid, *next;
  4029. __be32 *p;
  4030. p = xdr_reserve_space(xdr, 4 + (4 * test_stateid->ts_num_ids));
  4031. if (!p)
  4032. return nfserr_resource;
  4033. *p++ = htonl(test_stateid->ts_num_ids);
  4034. list_for_each_entry_safe(stateid, next, &test_stateid->ts_stateid_list, ts_id_list) {
  4035. *p++ = stateid->ts_id_status;
  4036. }
  4037. return 0;
  4038. }
  4039. #ifdef CONFIG_NFSD_PNFS
  4040. static __be32
  4041. nfsd4_encode_getdeviceinfo(struct nfsd4_compoundres *resp, __be32 nfserr,
  4042. struct nfsd4_getdeviceinfo *gdev)
  4043. {
  4044. struct xdr_stream *xdr = resp->xdr;
  4045. const struct nfsd4_layout_ops *ops;
  4046. u32 starting_len = xdr->buf->len, needed_len;
  4047. __be32 *p;
  4048. p = xdr_reserve_space(xdr, 4);
  4049. if (!p)
  4050. return nfserr_resource;
  4051. *p++ = cpu_to_be32(gdev->gd_layout_type);
  4052. ops = nfsd4_layout_ops[gdev->gd_layout_type];
  4053. nfserr = ops->encode_getdeviceinfo(xdr, gdev);
  4054. if (nfserr) {
  4055. /*
  4056. * We don't bother to burden the layout drivers with
  4057. * enforcing gd_maxcount, just tell the client to
  4058. * come back with a bigger buffer if it's not enough.
  4059. */
  4060. if (xdr->buf->len + 4 > gdev->gd_maxcount)
  4061. goto toosmall;
  4062. return nfserr;
  4063. }
  4064. if (gdev->gd_notify_types) {
  4065. p = xdr_reserve_space(xdr, 4 + 4);
  4066. if (!p)
  4067. return nfserr_resource;
  4068. *p++ = cpu_to_be32(1); /* bitmap length */
  4069. *p++ = cpu_to_be32(gdev->gd_notify_types);
  4070. } else {
  4071. p = xdr_reserve_space(xdr, 4);
  4072. if (!p)
  4073. return nfserr_resource;
  4074. *p++ = 0;
  4075. }
  4076. return 0;
  4077. toosmall:
  4078. dprintk("%s: maxcount too small\n", __func__);
  4079. needed_len = xdr->buf->len + 4 /* notifications */;
  4080. xdr_truncate_encode(xdr, starting_len);
  4081. p = xdr_reserve_space(xdr, 4);
  4082. if (!p)
  4083. return nfserr_resource;
  4084. *p++ = cpu_to_be32(needed_len);
  4085. return nfserr_toosmall;
  4086. }
  4087. static __be32
  4088. nfsd4_encode_layoutget(struct nfsd4_compoundres *resp, __be32 nfserr,
  4089. struct nfsd4_layoutget *lgp)
  4090. {
  4091. struct xdr_stream *xdr = resp->xdr;
  4092. const struct nfsd4_layout_ops *ops;
  4093. __be32 *p;
  4094. p = xdr_reserve_space(xdr, 36 + sizeof(stateid_opaque_t));
  4095. if (!p)
  4096. return nfserr_resource;
  4097. *p++ = cpu_to_be32(1); /* we always set return-on-close */
  4098. *p++ = cpu_to_be32(lgp->lg_sid.si_generation);
  4099. p = xdr_encode_opaque_fixed(p, &lgp->lg_sid.si_opaque,
  4100. sizeof(stateid_opaque_t));
  4101. *p++ = cpu_to_be32(1); /* we always return a single layout */
  4102. p = xdr_encode_hyper(p, lgp->lg_seg.offset);
  4103. p = xdr_encode_hyper(p, lgp->lg_seg.length);
  4104. *p++ = cpu_to_be32(lgp->lg_seg.iomode);
  4105. *p++ = cpu_to_be32(lgp->lg_layout_type);
  4106. ops = nfsd4_layout_ops[lgp->lg_layout_type];
  4107. return ops->encode_layoutget(xdr, lgp);
  4108. }
  4109. static __be32
  4110. nfsd4_encode_layoutcommit(struct nfsd4_compoundres *resp, __be32 nfserr,
  4111. struct nfsd4_layoutcommit *lcp)
  4112. {
  4113. struct xdr_stream *xdr = resp->xdr;
  4114. __be32 *p;
  4115. p = xdr_reserve_space(xdr, 4);
  4116. if (!p)
  4117. return nfserr_resource;
  4118. *p++ = cpu_to_be32(lcp->lc_size_chg);
  4119. if (lcp->lc_size_chg) {
  4120. p = xdr_reserve_space(xdr, 8);
  4121. if (!p)
  4122. return nfserr_resource;
  4123. p = xdr_encode_hyper(p, lcp->lc_newsize);
  4124. }
  4125. return 0;
  4126. }
  4127. static __be32
  4128. nfsd4_encode_layoutreturn(struct nfsd4_compoundres *resp, __be32 nfserr,
  4129. struct nfsd4_layoutreturn *lrp)
  4130. {
  4131. struct xdr_stream *xdr = resp->xdr;
  4132. __be32 *p;
  4133. p = xdr_reserve_space(xdr, 4);
  4134. if (!p)
  4135. return nfserr_resource;
  4136. *p++ = cpu_to_be32(lrp->lrs_present);
  4137. if (lrp->lrs_present)
  4138. return nfsd4_encode_stateid(xdr, &lrp->lr_sid);
  4139. return 0;
  4140. }
  4141. #endif /* CONFIG_NFSD_PNFS */
  4142. static __be32
  4143. nfsd42_encode_write_res(struct nfsd4_compoundres *resp,
  4144. struct nfsd42_write_res *write, bool sync)
  4145. {
  4146. __be32 *p;
  4147. p = xdr_reserve_space(resp->xdr, 4);
  4148. if (!p)
  4149. return nfserr_resource;
  4150. if (sync)
  4151. *p++ = cpu_to_be32(0);
  4152. else {
  4153. __be32 nfserr;
  4154. *p++ = cpu_to_be32(1);
  4155. nfserr = nfsd4_encode_stateid(resp->xdr, &write->cb_stateid);
  4156. if (nfserr)
  4157. return nfserr;
  4158. }
  4159. p = xdr_reserve_space(resp->xdr, 8 + 4 + NFS4_VERIFIER_SIZE);
  4160. if (!p)
  4161. return nfserr_resource;
  4162. p = xdr_encode_hyper(p, write->wr_bytes_written);
  4163. *p++ = cpu_to_be32(write->wr_stable_how);
  4164. p = xdr_encode_opaque_fixed(p, write->wr_verifier.data,
  4165. NFS4_VERIFIER_SIZE);
  4166. return nfs_ok;
  4167. }
  4168. static __be32
  4169. nfsd42_encode_nl4_server(struct nfsd4_compoundres *resp, struct nl4_server *ns)
  4170. {
  4171. struct xdr_stream *xdr = resp->xdr;
  4172. struct nfs42_netaddr *addr;
  4173. __be32 *p;
  4174. p = xdr_reserve_space(xdr, 4);
  4175. *p++ = cpu_to_be32(ns->nl4_type);
  4176. switch (ns->nl4_type) {
  4177. case NL4_NETADDR:
  4178. addr = &ns->u.nl4_addr;
  4179. /* netid_len, netid, uaddr_len, uaddr (port included
  4180. * in RPCBIND_MAXUADDRLEN)
  4181. */
  4182. p = xdr_reserve_space(xdr,
  4183. 4 /* netid len */ +
  4184. (XDR_QUADLEN(addr->netid_len) * 4) +
  4185. 4 /* uaddr len */ +
  4186. (XDR_QUADLEN(addr->addr_len) * 4));
  4187. if (!p)
  4188. return nfserr_resource;
  4189. *p++ = cpu_to_be32(addr->netid_len);
  4190. p = xdr_encode_opaque_fixed(p, addr->netid,
  4191. addr->netid_len);
  4192. *p++ = cpu_to_be32(addr->addr_len);
  4193. p = xdr_encode_opaque_fixed(p, addr->addr,
  4194. addr->addr_len);
  4195. break;
  4196. default:
  4197. WARN_ON_ONCE(ns->nl4_type != NL4_NETADDR);
  4198. return nfserr_inval;
  4199. }
  4200. return 0;
  4201. }
  4202. static __be32
  4203. nfsd4_encode_copy(struct nfsd4_compoundres *resp, __be32 nfserr,
  4204. struct nfsd4_copy *copy)
  4205. {
  4206. __be32 *p;
  4207. nfserr = nfsd42_encode_write_res(resp, &copy->cp_res,
  4208. nfsd4_copy_is_sync(copy));
  4209. if (nfserr)
  4210. return nfserr;
  4211. p = xdr_reserve_space(resp->xdr, 4 + 4);
  4212. *p++ = xdr_one; /* cr_consecutive */
  4213. *p = nfsd4_copy_is_sync(copy) ? xdr_one : xdr_zero;
  4214. return 0;
  4215. }
  4216. static __be32
  4217. nfsd4_encode_offload_status(struct nfsd4_compoundres *resp, __be32 nfserr,
  4218. struct nfsd4_offload_status *os)
  4219. {
  4220. struct xdr_stream *xdr = resp->xdr;
  4221. __be32 *p;
  4222. p = xdr_reserve_space(xdr, 8 + 4);
  4223. if (!p)
  4224. return nfserr_resource;
  4225. p = xdr_encode_hyper(p, os->count);
  4226. *p++ = cpu_to_be32(0);
  4227. return nfserr;
  4228. }
  4229. static __be32
  4230. nfsd4_encode_read_plus_data(struct nfsd4_compoundres *resp,
  4231. struct nfsd4_read *read,
  4232. unsigned long *maxcount, u32 *eof,
  4233. loff_t *pos)
  4234. {
  4235. struct xdr_stream *xdr = resp->xdr;
  4236. struct file *file = read->rd_nf->nf_file;
  4237. int starting_len = xdr->buf->len;
  4238. loff_t hole_pos;
  4239. __be32 nfserr;
  4240. __be32 *p, tmp;
  4241. __be64 tmp64;
  4242. hole_pos = pos ? *pos : vfs_llseek(file, read->rd_offset, SEEK_HOLE);
  4243. if (hole_pos > read->rd_offset)
  4244. *maxcount = min_t(unsigned long, *maxcount, hole_pos - read->rd_offset);
  4245. *maxcount = min_t(unsigned long, *maxcount, (xdr->buf->buflen - xdr->buf->len));
  4246. /* Content type, offset, byte count */
  4247. p = xdr_reserve_space(xdr, 4 + 8 + 4);
  4248. if (!p)
  4249. return nfserr_resource;
  4250. read->rd_vlen = xdr_reserve_space_vec(xdr, resp->rqstp->rq_vec, *maxcount);
  4251. if (read->rd_vlen < 0)
  4252. return nfserr_resource;
  4253. nfserr = nfsd_readv(resp->rqstp, read->rd_fhp, file, read->rd_offset,
  4254. resp->rqstp->rq_vec, read->rd_vlen, maxcount, eof);
  4255. if (nfserr)
  4256. return nfserr;
  4257. xdr_truncate_encode(xdr, starting_len + 16 + xdr_align_size(*maxcount));
  4258. tmp = htonl(NFS4_CONTENT_DATA);
  4259. write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
  4260. tmp64 = cpu_to_be64(read->rd_offset);
  4261. write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp64, 8);
  4262. tmp = htonl(*maxcount);
  4263. write_bytes_to_xdr_buf(xdr->buf, starting_len + 12, &tmp, 4);
  4264. tmp = xdr_zero;
  4265. write_bytes_to_xdr_buf(xdr->buf, starting_len + 16 + *maxcount, &tmp,
  4266. xdr_pad_size(*maxcount));
  4267. return nfs_ok;
  4268. }
  4269. static __be32
  4270. nfsd4_encode_read_plus_hole(struct nfsd4_compoundres *resp,
  4271. struct nfsd4_read *read,
  4272. unsigned long *maxcount, u32 *eof)
  4273. {
  4274. struct file *file = read->rd_nf->nf_file;
  4275. loff_t data_pos = vfs_llseek(file, read->rd_offset, SEEK_DATA);
  4276. loff_t f_size = i_size_read(file_inode(file));
  4277. unsigned long count;
  4278. __be32 *p;
  4279. if (data_pos == -ENXIO)
  4280. data_pos = f_size;
  4281. else if (data_pos <= read->rd_offset || (data_pos < f_size && data_pos % PAGE_SIZE))
  4282. return nfsd4_encode_read_plus_data(resp, read, maxcount, eof, &f_size);
  4283. count = data_pos - read->rd_offset;
  4284. /* Content type, offset, byte count */
  4285. p = xdr_reserve_space(resp->xdr, 4 + 8 + 8);
  4286. if (!p)
  4287. return nfserr_resource;
  4288. *p++ = htonl(NFS4_CONTENT_HOLE);
  4289. p = xdr_encode_hyper(p, read->rd_offset);
  4290. p = xdr_encode_hyper(p, count);
  4291. *eof = (read->rd_offset + count) >= f_size;
  4292. *maxcount = min_t(unsigned long, count, *maxcount);
  4293. return nfs_ok;
  4294. }
  4295. static __be32
  4296. nfsd4_encode_read_plus(struct nfsd4_compoundres *resp, __be32 nfserr,
  4297. struct nfsd4_read *read)
  4298. {
  4299. unsigned long maxcount, count;
  4300. struct xdr_stream *xdr = resp->xdr;
  4301. struct file *file;
  4302. int starting_len = xdr->buf->len;
  4303. int last_segment = xdr->buf->len;
  4304. int segments = 0;
  4305. __be32 *p, tmp;
  4306. bool is_data;
  4307. loff_t pos;
  4308. u32 eof;
  4309. if (nfserr)
  4310. return nfserr;
  4311. file = read->rd_nf->nf_file;
  4312. /* eof flag, segment count */
  4313. p = xdr_reserve_space(xdr, 4 + 4);
  4314. if (!p)
  4315. return nfserr_resource;
  4316. xdr_commit_encode(xdr);
  4317. maxcount = min_t(unsigned long, read->rd_length,
  4318. (xdr->buf->buflen - xdr->buf->len));
  4319. count = maxcount;
  4320. eof = read->rd_offset >= i_size_read(file_inode(file));
  4321. if (eof)
  4322. goto out;
  4323. pos = vfs_llseek(file, read->rd_offset, SEEK_HOLE);
  4324. is_data = pos > read->rd_offset;
  4325. while (count > 0 && !eof) {
  4326. maxcount = count;
  4327. if (is_data)
  4328. nfserr = nfsd4_encode_read_plus_data(resp, read, &maxcount, &eof,
  4329. segments == 0 ? &pos : NULL);
  4330. else
  4331. nfserr = nfsd4_encode_read_plus_hole(resp, read, &maxcount, &eof);
  4332. if (nfserr)
  4333. goto out;
  4334. count -= maxcount;
  4335. read->rd_offset += maxcount;
  4336. is_data = !is_data;
  4337. last_segment = xdr->buf->len;
  4338. segments++;
  4339. }
  4340. out:
  4341. if (nfserr && segments == 0)
  4342. xdr_truncate_encode(xdr, starting_len);
  4343. else {
  4344. if (nfserr) {
  4345. xdr_truncate_encode(xdr, last_segment);
  4346. nfserr = nfs_ok;
  4347. eof = 0;
  4348. }
  4349. tmp = htonl(eof);
  4350. write_bytes_to_xdr_buf(xdr->buf, starting_len, &tmp, 4);
  4351. tmp = htonl(segments);
  4352. write_bytes_to_xdr_buf(xdr->buf, starting_len + 4, &tmp, 4);
  4353. }
  4354. return nfserr;
  4355. }
  4356. static __be32
  4357. nfsd4_encode_copy_notify(struct nfsd4_compoundres *resp, __be32 nfserr,
  4358. struct nfsd4_copy_notify *cn)
  4359. {
  4360. struct xdr_stream *xdr = resp->xdr;
  4361. __be32 *p;
  4362. if (nfserr)
  4363. return nfserr;
  4364. /* 8 sec, 4 nsec */
  4365. p = xdr_reserve_space(xdr, 12);
  4366. if (!p)
  4367. return nfserr_resource;
  4368. /* cnr_lease_time */
  4369. p = xdr_encode_hyper(p, cn->cpn_sec);
  4370. *p++ = cpu_to_be32(cn->cpn_nsec);
  4371. /* cnr_stateid */
  4372. nfserr = nfsd4_encode_stateid(xdr, &cn->cpn_cnr_stateid);
  4373. if (nfserr)
  4374. return nfserr;
  4375. /* cnr_src.nl_nsvr */
  4376. p = xdr_reserve_space(xdr, 4);
  4377. if (!p)
  4378. return nfserr_resource;
  4379. *p++ = cpu_to_be32(1);
  4380. nfserr = nfsd42_encode_nl4_server(resp, cn->cpn_src);
  4381. return nfserr;
  4382. }
  4383. static __be32
  4384. nfsd4_encode_seek(struct nfsd4_compoundres *resp, __be32 nfserr,
  4385. struct nfsd4_seek *seek)
  4386. {
  4387. __be32 *p;
  4388. p = xdr_reserve_space(resp->xdr, 4 + 8);
  4389. *p++ = cpu_to_be32(seek->seek_eof);
  4390. p = xdr_encode_hyper(p, seek->seek_pos);
  4391. return 0;
  4392. }
  4393. static __be32
  4394. nfsd4_encode_noop(struct nfsd4_compoundres *resp, __be32 nfserr, void *p)
  4395. {
  4396. return nfserr;
  4397. }
  4398. /*
  4399. * Encode kmalloc-ed buffer in to XDR stream.
  4400. */
  4401. static __be32
  4402. nfsd4_vbuf_to_stream(struct xdr_stream *xdr, char *buf, u32 buflen)
  4403. {
  4404. u32 cplen;
  4405. __be32 *p;
  4406. cplen = min_t(unsigned long, buflen,
  4407. ((void *)xdr->end - (void *)xdr->p));
  4408. p = xdr_reserve_space(xdr, cplen);
  4409. if (!p)
  4410. return nfserr_resource;
  4411. memcpy(p, buf, cplen);
  4412. buf += cplen;
  4413. buflen -= cplen;
  4414. while (buflen) {
  4415. cplen = min_t(u32, buflen, PAGE_SIZE);
  4416. p = xdr_reserve_space(xdr, cplen);
  4417. if (!p)
  4418. return nfserr_resource;
  4419. memcpy(p, buf, cplen);
  4420. if (cplen < PAGE_SIZE) {
  4421. /*
  4422. * We're done, with a length that wasn't page
  4423. * aligned, so possibly not word aligned. Pad
  4424. * any trailing bytes with 0.
  4425. */
  4426. xdr_encode_opaque_fixed(p, NULL, cplen);
  4427. break;
  4428. }
  4429. buflen -= PAGE_SIZE;
  4430. buf += PAGE_SIZE;
  4431. }
  4432. return 0;
  4433. }
  4434. static __be32
  4435. nfsd4_encode_getxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
  4436. struct nfsd4_getxattr *getxattr)
  4437. {
  4438. struct xdr_stream *xdr = resp->xdr;
  4439. __be32 *p, err;
  4440. p = xdr_reserve_space(xdr, 4);
  4441. if (!p)
  4442. return nfserr_resource;
  4443. *p = cpu_to_be32(getxattr->getxa_len);
  4444. if (getxattr->getxa_len == 0)
  4445. return 0;
  4446. err = nfsd4_vbuf_to_stream(xdr, getxattr->getxa_buf,
  4447. getxattr->getxa_len);
  4448. kvfree(getxattr->getxa_buf);
  4449. return err;
  4450. }
  4451. static __be32
  4452. nfsd4_encode_setxattr(struct nfsd4_compoundres *resp, __be32 nfserr,
  4453. struct nfsd4_setxattr *setxattr)
  4454. {
  4455. struct xdr_stream *xdr = resp->xdr;
  4456. __be32 *p;
  4457. p = xdr_reserve_space(xdr, 20);
  4458. if (!p)
  4459. return nfserr_resource;
  4460. encode_cinfo(p, &setxattr->setxa_cinfo);
  4461. return 0;
  4462. }
  4463. /*
  4464. * See if there are cookie values that can be rejected outright.
  4465. */
  4466. static __be32
  4467. nfsd4_listxattr_validate_cookie(struct nfsd4_listxattrs *listxattrs,
  4468. u32 *offsetp)
  4469. {
  4470. u64 cookie = listxattrs->lsxa_cookie;
  4471. /*
  4472. * If the cookie is larger than the maximum number we can fit
  4473. * in either the buffer we just got back from vfs_listxattr, or,
  4474. * XDR-encoded, in the return buffer, it's invalid.
  4475. */
  4476. if (cookie > (listxattrs->lsxa_len) / (XATTR_USER_PREFIX_LEN + 2))
  4477. return nfserr_badcookie;
  4478. if (cookie > (listxattrs->lsxa_maxcount /
  4479. (XDR_QUADLEN(XATTR_USER_PREFIX_LEN + 2) + 4)))
  4480. return nfserr_badcookie;
  4481. *offsetp = (u32)cookie;
  4482. return 0;
  4483. }
  4484. static __be32
  4485. nfsd4_encode_listxattrs(struct nfsd4_compoundres *resp, __be32 nfserr,
  4486. struct nfsd4_listxattrs *listxattrs)
  4487. {
  4488. struct xdr_stream *xdr = resp->xdr;
  4489. u32 cookie_offset, count_offset, eof;
  4490. u32 left, xdrleft, slen, count;
  4491. u32 xdrlen, offset;
  4492. u64 cookie;
  4493. char *sp;
  4494. __be32 status, tmp;
  4495. __be32 *p;
  4496. u32 nuser;
  4497. eof = 1;
  4498. status = nfsd4_listxattr_validate_cookie(listxattrs, &offset);
  4499. if (status)
  4500. goto out;
  4501. /*
  4502. * Reserve space for the cookie and the name array count. Record
  4503. * the offsets to save them later.
  4504. */
  4505. cookie_offset = xdr->buf->len;
  4506. count_offset = cookie_offset + 8;
  4507. p = xdr_reserve_space(xdr, 12);
  4508. if (!p) {
  4509. status = nfserr_resource;
  4510. goto out;
  4511. }
  4512. count = 0;
  4513. left = listxattrs->lsxa_len;
  4514. sp = listxattrs->lsxa_buf;
  4515. nuser = 0;
  4516. xdrleft = listxattrs->lsxa_maxcount;
  4517. while (left > 0 && xdrleft > 0) {
  4518. slen = strlen(sp);
  4519. /*
  4520. * Check if this is a "user." attribute, skip it if not.
  4521. */
  4522. if (strncmp(sp, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
  4523. goto contloop;
  4524. slen -= XATTR_USER_PREFIX_LEN;
  4525. xdrlen = 4 + ((slen + 3) & ~3);
  4526. if (xdrlen > xdrleft) {
  4527. if (count == 0) {
  4528. /*
  4529. * Can't even fit the first attribute name.
  4530. */
  4531. status = nfserr_toosmall;
  4532. goto out;
  4533. }
  4534. eof = 0;
  4535. goto wreof;
  4536. }
  4537. left -= XATTR_USER_PREFIX_LEN;
  4538. sp += XATTR_USER_PREFIX_LEN;
  4539. if (nuser++ < offset)
  4540. goto contloop;
  4541. p = xdr_reserve_space(xdr, xdrlen);
  4542. if (!p) {
  4543. status = nfserr_resource;
  4544. goto out;
  4545. }
  4546. xdr_encode_opaque(p, sp, slen);
  4547. xdrleft -= xdrlen;
  4548. count++;
  4549. contloop:
  4550. sp += slen + 1;
  4551. left -= slen + 1;
  4552. }
  4553. /*
  4554. * If there were user attributes to copy, but we didn't copy
  4555. * any, the offset was too large (e.g. the cookie was invalid).
  4556. */
  4557. if (nuser > 0 && count == 0) {
  4558. status = nfserr_badcookie;
  4559. goto out;
  4560. }
  4561. wreof:
  4562. p = xdr_reserve_space(xdr, 4);
  4563. if (!p) {
  4564. status = nfserr_resource;
  4565. goto out;
  4566. }
  4567. *p = cpu_to_be32(eof);
  4568. cookie = offset + count;
  4569. write_bytes_to_xdr_buf(xdr->buf, cookie_offset, &cookie, 8);
  4570. tmp = cpu_to_be32(count);
  4571. write_bytes_to_xdr_buf(xdr->buf, count_offset, &tmp, 4);
  4572. out:
  4573. if (listxattrs->lsxa_len)
  4574. kvfree(listxattrs->lsxa_buf);
  4575. return status;
  4576. }
  4577. static __be32
  4578. nfsd4_encode_removexattr(struct nfsd4_compoundres *resp, __be32 nfserr,
  4579. struct nfsd4_removexattr *removexattr)
  4580. {
  4581. struct xdr_stream *xdr = resp->xdr;
  4582. __be32 *p;
  4583. p = xdr_reserve_space(xdr, 20);
  4584. if (!p)
  4585. return nfserr_resource;
  4586. p = encode_cinfo(p, &removexattr->rmxa_cinfo);
  4587. return 0;
  4588. }
  4589. typedef __be32(* nfsd4_enc)(struct nfsd4_compoundres *, __be32, void *);
  4590. /*
  4591. * Note: nfsd4_enc_ops vector is shared for v4.0 and v4.1
  4592. * since we don't need to filter out obsolete ops as this is
  4593. * done in the decoding phase.
  4594. */
  4595. static const nfsd4_enc nfsd4_enc_ops[] = {
  4596. [OP_ACCESS] = (nfsd4_enc)nfsd4_encode_access,
  4597. [OP_CLOSE] = (nfsd4_enc)nfsd4_encode_close,
  4598. [OP_COMMIT] = (nfsd4_enc)nfsd4_encode_commit,
  4599. [OP_CREATE] = (nfsd4_enc)nfsd4_encode_create,
  4600. [OP_DELEGPURGE] = (nfsd4_enc)nfsd4_encode_noop,
  4601. [OP_DELEGRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  4602. [OP_GETATTR] = (nfsd4_enc)nfsd4_encode_getattr,
  4603. [OP_GETFH] = (nfsd4_enc)nfsd4_encode_getfh,
  4604. [OP_LINK] = (nfsd4_enc)nfsd4_encode_link,
  4605. [OP_LOCK] = (nfsd4_enc)nfsd4_encode_lock,
  4606. [OP_LOCKT] = (nfsd4_enc)nfsd4_encode_lockt,
  4607. [OP_LOCKU] = (nfsd4_enc)nfsd4_encode_locku,
  4608. [OP_LOOKUP] = (nfsd4_enc)nfsd4_encode_noop,
  4609. [OP_LOOKUPP] = (nfsd4_enc)nfsd4_encode_noop,
  4610. [OP_NVERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  4611. [OP_OPEN] = (nfsd4_enc)nfsd4_encode_open,
  4612. [OP_OPENATTR] = (nfsd4_enc)nfsd4_encode_noop,
  4613. [OP_OPEN_CONFIRM] = (nfsd4_enc)nfsd4_encode_open_confirm,
  4614. [OP_OPEN_DOWNGRADE] = (nfsd4_enc)nfsd4_encode_open_downgrade,
  4615. [OP_PUTFH] = (nfsd4_enc)nfsd4_encode_noop,
  4616. [OP_PUTPUBFH] = (nfsd4_enc)nfsd4_encode_noop,
  4617. [OP_PUTROOTFH] = (nfsd4_enc)nfsd4_encode_noop,
  4618. [OP_READ] = (nfsd4_enc)nfsd4_encode_read,
  4619. [OP_READDIR] = (nfsd4_enc)nfsd4_encode_readdir,
  4620. [OP_READLINK] = (nfsd4_enc)nfsd4_encode_readlink,
  4621. [OP_REMOVE] = (nfsd4_enc)nfsd4_encode_remove,
  4622. [OP_RENAME] = (nfsd4_enc)nfsd4_encode_rename,
  4623. [OP_RENEW] = (nfsd4_enc)nfsd4_encode_noop,
  4624. [OP_RESTOREFH] = (nfsd4_enc)nfsd4_encode_noop,
  4625. [OP_SAVEFH] = (nfsd4_enc)nfsd4_encode_noop,
  4626. [OP_SECINFO] = (nfsd4_enc)nfsd4_encode_secinfo,
  4627. [OP_SETATTR] = (nfsd4_enc)nfsd4_encode_setattr,
  4628. [OP_SETCLIENTID] = (nfsd4_enc)nfsd4_encode_setclientid,
  4629. [OP_SETCLIENTID_CONFIRM] = (nfsd4_enc)nfsd4_encode_noop,
  4630. [OP_VERIFY] = (nfsd4_enc)nfsd4_encode_noop,
  4631. [OP_WRITE] = (nfsd4_enc)nfsd4_encode_write,
  4632. [OP_RELEASE_LOCKOWNER] = (nfsd4_enc)nfsd4_encode_noop,
  4633. /* NFSv4.1 operations */
  4634. [OP_BACKCHANNEL_CTL] = (nfsd4_enc)nfsd4_encode_noop,
  4635. [OP_BIND_CONN_TO_SESSION] = (nfsd4_enc)nfsd4_encode_bind_conn_to_session,
  4636. [OP_EXCHANGE_ID] = (nfsd4_enc)nfsd4_encode_exchange_id,
  4637. [OP_CREATE_SESSION] = (nfsd4_enc)nfsd4_encode_create_session,
  4638. [OP_DESTROY_SESSION] = (nfsd4_enc)nfsd4_encode_noop,
  4639. [OP_FREE_STATEID] = (nfsd4_enc)nfsd4_encode_noop,
  4640. [OP_GET_DIR_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  4641. #ifdef CONFIG_NFSD_PNFS
  4642. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_getdeviceinfo,
  4643. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  4644. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_layoutcommit,
  4645. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_layoutget,
  4646. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_layoutreturn,
  4647. #else
  4648. [OP_GETDEVICEINFO] = (nfsd4_enc)nfsd4_encode_noop,
  4649. [OP_GETDEVICELIST] = (nfsd4_enc)nfsd4_encode_noop,
  4650. [OP_LAYOUTCOMMIT] = (nfsd4_enc)nfsd4_encode_noop,
  4651. [OP_LAYOUTGET] = (nfsd4_enc)nfsd4_encode_noop,
  4652. [OP_LAYOUTRETURN] = (nfsd4_enc)nfsd4_encode_noop,
  4653. #endif
  4654. [OP_SECINFO_NO_NAME] = (nfsd4_enc)nfsd4_encode_secinfo_no_name,
  4655. [OP_SEQUENCE] = (nfsd4_enc)nfsd4_encode_sequence,
  4656. [OP_SET_SSV] = (nfsd4_enc)nfsd4_encode_noop,
  4657. [OP_TEST_STATEID] = (nfsd4_enc)nfsd4_encode_test_stateid,
  4658. [OP_WANT_DELEGATION] = (nfsd4_enc)nfsd4_encode_noop,
  4659. [OP_DESTROY_CLIENTID] = (nfsd4_enc)nfsd4_encode_noop,
  4660. [OP_RECLAIM_COMPLETE] = (nfsd4_enc)nfsd4_encode_noop,
  4661. /* NFSv4.2 operations */
  4662. [OP_ALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
  4663. [OP_COPY] = (nfsd4_enc)nfsd4_encode_copy,
  4664. [OP_COPY_NOTIFY] = (nfsd4_enc)nfsd4_encode_copy_notify,
  4665. [OP_DEALLOCATE] = (nfsd4_enc)nfsd4_encode_noop,
  4666. [OP_IO_ADVISE] = (nfsd4_enc)nfsd4_encode_noop,
  4667. [OP_LAYOUTERROR] = (nfsd4_enc)nfsd4_encode_noop,
  4668. [OP_LAYOUTSTATS] = (nfsd4_enc)nfsd4_encode_noop,
  4669. [OP_OFFLOAD_CANCEL] = (nfsd4_enc)nfsd4_encode_noop,
  4670. [OP_OFFLOAD_STATUS] = (nfsd4_enc)nfsd4_encode_offload_status,
  4671. [OP_READ_PLUS] = (nfsd4_enc)nfsd4_encode_read_plus,
  4672. [OP_SEEK] = (nfsd4_enc)nfsd4_encode_seek,
  4673. [OP_WRITE_SAME] = (nfsd4_enc)nfsd4_encode_noop,
  4674. [OP_CLONE] = (nfsd4_enc)nfsd4_encode_noop,
  4675. /* RFC 8276 extended atributes operations */
  4676. [OP_GETXATTR] = (nfsd4_enc)nfsd4_encode_getxattr,
  4677. [OP_SETXATTR] = (nfsd4_enc)nfsd4_encode_setxattr,
  4678. [OP_LISTXATTRS] = (nfsd4_enc)nfsd4_encode_listxattrs,
  4679. [OP_REMOVEXATTR] = (nfsd4_enc)nfsd4_encode_removexattr,
  4680. };
  4681. /*
  4682. * Calculate whether we still have space to encode repsize bytes.
  4683. * There are two considerations:
  4684. * - For NFS versions >=4.1, the size of the reply must stay within
  4685. * session limits
  4686. * - For all NFS versions, we must stay within limited preallocated
  4687. * buffer space.
  4688. *
  4689. * This is called before the operation is processed, so can only provide
  4690. * an upper estimate. For some nonidempotent operations (such as
  4691. * getattr), it's not necessarily a problem if that estimate is wrong,
  4692. * as we can fail it after processing without significant side effects.
  4693. */
  4694. __be32 nfsd4_check_resp_size(struct nfsd4_compoundres *resp, u32 respsize)
  4695. {
  4696. struct xdr_buf *buf = &resp->rqstp->rq_res;
  4697. struct nfsd4_slot *slot = resp->cstate.slot;
  4698. if (buf->len + respsize <= buf->buflen)
  4699. return nfs_ok;
  4700. if (!nfsd4_has_session(&resp->cstate))
  4701. return nfserr_resource;
  4702. if (slot->sl_flags & NFSD4_SLOT_CACHETHIS) {
  4703. WARN_ON_ONCE(1);
  4704. return nfserr_rep_too_big_to_cache;
  4705. }
  4706. return nfserr_rep_too_big;
  4707. }
  4708. void
  4709. nfsd4_encode_operation(struct nfsd4_compoundres *resp, struct nfsd4_op *op)
  4710. {
  4711. struct xdr_stream *xdr = resp->xdr;
  4712. struct nfs4_stateowner *so = resp->cstate.replay_owner;
  4713. struct svc_rqst *rqstp = resp->rqstp;
  4714. const struct nfsd4_operation *opdesc = op->opdesc;
  4715. int post_err_offset;
  4716. nfsd4_enc encoder;
  4717. __be32 *p;
  4718. p = xdr_reserve_space(xdr, 8);
  4719. if (!p)
  4720. goto release;
  4721. *p++ = cpu_to_be32(op->opnum);
  4722. post_err_offset = xdr->buf->len;
  4723. if (op->opnum == OP_ILLEGAL)
  4724. goto status;
  4725. if (op->status && opdesc &&
  4726. !(opdesc->op_flags & OP_NONTRIVIAL_ERROR_ENCODE))
  4727. goto status;
  4728. BUG_ON(op->opnum >= ARRAY_SIZE(nfsd4_enc_ops) ||
  4729. !nfsd4_enc_ops[op->opnum]);
  4730. encoder = nfsd4_enc_ops[op->opnum];
  4731. op->status = encoder(resp, op->status, &op->u);
  4732. if (op->status)
  4733. trace_nfsd_compound_encode_err(rqstp, op->opnum, op->status);
  4734. xdr_commit_encode(xdr);
  4735. /* nfsd4_check_resp_size guarantees enough room for error status */
  4736. if (!op->status) {
  4737. int space_needed = 0;
  4738. if (!nfsd4_last_compound_op(rqstp))
  4739. space_needed = COMPOUND_ERR_SLACK_SPACE;
  4740. op->status = nfsd4_check_resp_size(resp, space_needed);
  4741. }
  4742. if (op->status == nfserr_resource && nfsd4_has_session(&resp->cstate)) {
  4743. struct nfsd4_slot *slot = resp->cstate.slot;
  4744. if (slot->sl_flags & NFSD4_SLOT_CACHETHIS)
  4745. op->status = nfserr_rep_too_big_to_cache;
  4746. else
  4747. op->status = nfserr_rep_too_big;
  4748. }
  4749. if (op->status == nfserr_resource ||
  4750. op->status == nfserr_rep_too_big ||
  4751. op->status == nfserr_rep_too_big_to_cache) {
  4752. /*
  4753. * The operation may have already been encoded or
  4754. * partially encoded. No op returns anything additional
  4755. * in the case of one of these three errors, so we can
  4756. * just truncate back to after the status. But it's a
  4757. * bug if we had to do this on a non-idempotent op:
  4758. */
  4759. warn_on_nonidempotent_op(op);
  4760. xdr_truncate_encode(xdr, post_err_offset);
  4761. }
  4762. if (so) {
  4763. int len = xdr->buf->len - post_err_offset;
  4764. so->so_replay.rp_status = op->status;
  4765. so->so_replay.rp_buflen = len;
  4766. read_bytes_from_xdr_buf(xdr->buf, post_err_offset,
  4767. so->so_replay.rp_buf, len);
  4768. }
  4769. status:
  4770. *p = op->status;
  4771. release:
  4772. if (opdesc && opdesc->op_release)
  4773. opdesc->op_release(&op->u);
  4774. }
  4775. /*
  4776. * Encode the reply stored in the stateowner reply cache
  4777. *
  4778. * XDR note: do not encode rp->rp_buflen: the buffer contains the
  4779. * previously sent already encoded operation.
  4780. */
  4781. void
  4782. nfsd4_encode_replay(struct xdr_stream *xdr, struct nfsd4_op *op)
  4783. {
  4784. __be32 *p;
  4785. struct nfs4_replay *rp = op->replay;
  4786. p = xdr_reserve_space(xdr, 8 + rp->rp_buflen);
  4787. if (!p) {
  4788. WARN_ON_ONCE(1);
  4789. return;
  4790. }
  4791. *p++ = cpu_to_be32(op->opnum);
  4792. *p++ = rp->rp_status; /* already xdr'ed */
  4793. p = xdr_encode_opaque_fixed(p, rp->rp_buf, rp->rp_buflen);
  4794. }
  4795. void nfsd4_release_compoundargs(struct svc_rqst *rqstp)
  4796. {
  4797. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  4798. if (args->ops != args->iops) {
  4799. vfree(args->ops);
  4800. args->ops = args->iops;
  4801. }
  4802. while (args->to_free) {
  4803. struct svcxdr_tmpbuf *tb = args->to_free;
  4804. args->to_free = tb->next;
  4805. kfree(tb);
  4806. }
  4807. }
  4808. bool
  4809. nfs4svc_decode_compoundargs(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  4810. {
  4811. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  4812. /* svcxdr_tmp_alloc */
  4813. args->to_free = NULL;
  4814. args->xdr = xdr;
  4815. args->ops = args->iops;
  4816. args->rqstp = rqstp;
  4817. return nfsd4_decode_compound(args);
  4818. }
  4819. bool
  4820. nfs4svc_encode_compoundres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
  4821. {
  4822. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  4823. __be32 *p;
  4824. /*
  4825. * Send buffer space for the following items is reserved
  4826. * at the top of nfsd4_proc_compound().
  4827. */
  4828. p = resp->statusp;
  4829. *p++ = resp->cstate.status;
  4830. rqstp->rq_next_page = xdr->page_ptr + 1;
  4831. *p++ = htonl(resp->taglen);
  4832. memcpy(p, resp->tag, resp->taglen);
  4833. p += XDR_QUADLEN(resp->taglen);
  4834. *p++ = htonl(resp->opcnt);
  4835. nfsd4_sequence_done(resp);
  4836. return true;
  4837. }