nfs4xdr.c 133 KB

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