nfs4state.c 134 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172
  1. /*
  2. * Copyright (c) 2001 The Regents of the University of Michigan.
  3. * All rights reserved.
  4. *
  5. * Kendrick Smith <kmsmith@umich.edu>
  6. * Andy Adamson <kandros@umich.edu>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. #include <linux/file.h>
  35. #include <linux/fs.h>
  36. #include <linux/slab.h>
  37. #include <linux/namei.h>
  38. #include <linux/swap.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/ratelimit.h>
  41. #include <linux/sunrpc/svcauth_gss.h>
  42. #include <linux/sunrpc/addr.h>
  43. #include "xdr4.h"
  44. #include "xdr4cb.h"
  45. #include "vfs.h"
  46. #include "current_stateid.h"
  47. #include "netns.h"
  48. #define NFSDDBG_FACILITY NFSDDBG_PROC
  49. #define all_ones {{~0,~0},~0}
  50. static const stateid_t one_stateid = {
  51. .si_generation = ~0,
  52. .si_opaque = all_ones,
  53. };
  54. static const stateid_t zero_stateid = {
  55. /* all fields zero */
  56. };
  57. static const stateid_t currentstateid = {
  58. .si_generation = 1,
  59. };
  60. static u64 current_sessionid = 1;
  61. #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
  62. #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
  63. #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
  64. /* forward declarations */
  65. static int check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner);
  66. /* Locking: */
  67. /* Currently used for almost all code touching nfsv4 state: */
  68. static DEFINE_MUTEX(client_mutex);
  69. /*
  70. * Currently used for the del_recall_lru and file hash table. In an
  71. * effort to decrease the scope of the client_mutex, this spinlock may
  72. * eventually cover more:
  73. */
  74. static DEFINE_SPINLOCK(recall_lock);
  75. static struct kmem_cache *openowner_slab = NULL;
  76. static struct kmem_cache *lockowner_slab = NULL;
  77. static struct kmem_cache *file_slab = NULL;
  78. static struct kmem_cache *stateid_slab = NULL;
  79. static struct kmem_cache *deleg_slab = NULL;
  80. void
  81. nfs4_lock_state(void)
  82. {
  83. mutex_lock(&client_mutex);
  84. }
  85. static void free_session(struct nfsd4_session *);
  86. void nfsd4_put_session(struct nfsd4_session *ses)
  87. {
  88. atomic_dec(&ses->se_ref);
  89. }
  90. static bool is_session_dead(struct nfsd4_session *ses)
  91. {
  92. return ses->se_flags & NFS4_SESSION_DEAD;
  93. }
  94. static __be32 mark_session_dead_locked(struct nfsd4_session *ses)
  95. {
  96. if (atomic_read(&ses->se_ref))
  97. return nfserr_jukebox;
  98. ses->se_flags |= NFS4_SESSION_DEAD;
  99. return nfs_ok;
  100. }
  101. static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
  102. {
  103. if (is_session_dead(ses))
  104. return nfserr_badsession;
  105. atomic_inc(&ses->se_ref);
  106. return nfs_ok;
  107. }
  108. void
  109. nfs4_unlock_state(void)
  110. {
  111. mutex_unlock(&client_mutex);
  112. }
  113. static bool is_client_expired(struct nfs4_client *clp)
  114. {
  115. return clp->cl_time == 0;
  116. }
  117. static __be32 mark_client_expired_locked(struct nfs4_client *clp)
  118. {
  119. if (atomic_read(&clp->cl_refcount))
  120. return nfserr_jukebox;
  121. clp->cl_time = 0;
  122. return nfs_ok;
  123. }
  124. static __be32 mark_client_expired(struct nfs4_client *clp)
  125. {
  126. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  127. __be32 ret;
  128. spin_lock(&nn->client_lock);
  129. ret = mark_client_expired_locked(clp);
  130. spin_unlock(&nn->client_lock);
  131. return ret;
  132. }
  133. static __be32 get_client_locked(struct nfs4_client *clp)
  134. {
  135. if (is_client_expired(clp))
  136. return nfserr_expired;
  137. atomic_inc(&clp->cl_refcount);
  138. return nfs_ok;
  139. }
  140. /* must be called under the client_lock */
  141. static inline void
  142. renew_client_locked(struct nfs4_client *clp)
  143. {
  144. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  145. if (is_client_expired(clp)) {
  146. WARN_ON(1);
  147. printk("%s: client (clientid %08x/%08x) already expired\n",
  148. __func__,
  149. clp->cl_clientid.cl_boot,
  150. clp->cl_clientid.cl_id);
  151. return;
  152. }
  153. dprintk("renewing client (clientid %08x/%08x)\n",
  154. clp->cl_clientid.cl_boot,
  155. clp->cl_clientid.cl_id);
  156. list_move_tail(&clp->cl_lru, &nn->client_lru);
  157. clp->cl_time = get_seconds();
  158. }
  159. static inline void
  160. renew_client(struct nfs4_client *clp)
  161. {
  162. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  163. spin_lock(&nn->client_lock);
  164. renew_client_locked(clp);
  165. spin_unlock(&nn->client_lock);
  166. }
  167. void put_client_renew_locked(struct nfs4_client *clp)
  168. {
  169. if (!atomic_dec_and_test(&clp->cl_refcount))
  170. return;
  171. if (!is_client_expired(clp))
  172. renew_client_locked(clp);
  173. }
  174. void put_client_renew(struct nfs4_client *clp)
  175. {
  176. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  177. if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
  178. return;
  179. if (!is_client_expired(clp))
  180. renew_client_locked(clp);
  181. spin_unlock(&nn->client_lock);
  182. }
  183. static inline u32
  184. opaque_hashval(const void *ptr, int nbytes)
  185. {
  186. unsigned char *cptr = (unsigned char *) ptr;
  187. u32 x = 0;
  188. while (nbytes--) {
  189. x *= 37;
  190. x += *cptr++;
  191. }
  192. return x;
  193. }
  194. static void nfsd4_free_file(struct nfs4_file *f)
  195. {
  196. kmem_cache_free(file_slab, f);
  197. }
  198. static inline void
  199. put_nfs4_file(struct nfs4_file *fi)
  200. {
  201. if (atomic_dec_and_lock(&fi->fi_ref, &recall_lock)) {
  202. hlist_del(&fi->fi_hash);
  203. spin_unlock(&recall_lock);
  204. iput(fi->fi_inode);
  205. nfsd4_free_file(fi);
  206. }
  207. }
  208. static inline void
  209. get_nfs4_file(struct nfs4_file *fi)
  210. {
  211. atomic_inc(&fi->fi_ref);
  212. }
  213. static int num_delegations;
  214. unsigned long max_delegations;
  215. /*
  216. * Open owner state (share locks)
  217. */
  218. /* hash tables for lock and open owners */
  219. #define OWNER_HASH_BITS 8
  220. #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
  221. #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
  222. static unsigned int ownerstr_hashval(u32 clientid, struct xdr_netobj *ownername)
  223. {
  224. unsigned int ret;
  225. ret = opaque_hashval(ownername->data, ownername->len);
  226. ret += clientid;
  227. return ret & OWNER_HASH_MASK;
  228. }
  229. /* hash table for nfs4_file */
  230. #define FILE_HASH_BITS 8
  231. #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
  232. static unsigned int file_hashval(struct inode *ino)
  233. {
  234. /* XXX: why are we hashing on inode pointer, anyway? */
  235. return hash_ptr(ino, FILE_HASH_BITS);
  236. }
  237. static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
  238. static void __nfs4_file_get_access(struct nfs4_file *fp, int oflag)
  239. {
  240. WARN_ON_ONCE(!(fp->fi_fds[oflag] || fp->fi_fds[O_RDWR]));
  241. atomic_inc(&fp->fi_access[oflag]);
  242. }
  243. static void nfs4_file_get_access(struct nfs4_file *fp, int oflag)
  244. {
  245. if (oflag == O_RDWR) {
  246. __nfs4_file_get_access(fp, O_RDONLY);
  247. __nfs4_file_get_access(fp, O_WRONLY);
  248. } else
  249. __nfs4_file_get_access(fp, oflag);
  250. }
  251. static void nfs4_file_put_fd(struct nfs4_file *fp, int oflag)
  252. {
  253. if (fp->fi_fds[oflag]) {
  254. fput(fp->fi_fds[oflag]);
  255. fp->fi_fds[oflag] = NULL;
  256. }
  257. }
  258. static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  259. {
  260. if (atomic_dec_and_test(&fp->fi_access[oflag])) {
  261. nfs4_file_put_fd(fp, oflag);
  262. if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
  263. nfs4_file_put_fd(fp, O_RDWR);
  264. }
  265. }
  266. static void nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  267. {
  268. if (oflag == O_RDWR) {
  269. __nfs4_file_put_access(fp, O_RDONLY);
  270. __nfs4_file_put_access(fp, O_WRONLY);
  271. } else
  272. __nfs4_file_put_access(fp, oflag);
  273. }
  274. static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct
  275. kmem_cache *slab)
  276. {
  277. struct idr *stateids = &cl->cl_stateids;
  278. static int min_stateid = 0;
  279. struct nfs4_stid *stid;
  280. int new_id;
  281. stid = kmem_cache_alloc(slab, GFP_KERNEL);
  282. if (!stid)
  283. return NULL;
  284. new_id = idr_alloc(stateids, stid, min_stateid, 0, GFP_KERNEL);
  285. if (new_id < 0)
  286. goto out_free;
  287. stid->sc_client = cl;
  288. stid->sc_type = 0;
  289. stid->sc_stateid.si_opaque.so_id = new_id;
  290. stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
  291. /* Will be incremented before return to client: */
  292. stid->sc_stateid.si_generation = 0;
  293. /*
  294. * It shouldn't be a problem to reuse an opaque stateid value.
  295. * I don't think it is for 4.1. But with 4.0 I worry that, for
  296. * example, a stray write retransmission could be accepted by
  297. * the server when it should have been rejected. Therefore,
  298. * adopt a trick from the sctp code to attempt to maximize the
  299. * amount of time until an id is reused, by ensuring they always
  300. * "increase" (mod INT_MAX):
  301. */
  302. min_stateid = new_id+1;
  303. if (min_stateid == INT_MAX)
  304. min_stateid = 0;
  305. return stid;
  306. out_free:
  307. kmem_cache_free(slab, stid);
  308. return NULL;
  309. }
  310. static struct nfs4_ol_stateid * nfs4_alloc_stateid(struct nfs4_client *clp)
  311. {
  312. return openlockstateid(nfs4_alloc_stid(clp, stateid_slab));
  313. }
  314. static struct nfs4_delegation *
  315. alloc_init_deleg(struct nfs4_client *clp, struct nfs4_ol_stateid *stp, struct svc_fh *current_fh, u32 type)
  316. {
  317. struct nfs4_delegation *dp;
  318. struct nfs4_file *fp = stp->st_file;
  319. dprintk("NFSD alloc_init_deleg\n");
  320. /*
  321. * Major work on the lease subsystem (for example, to support
  322. * calbacks on stat) will be required before we can support
  323. * write delegations properly.
  324. */
  325. if (type != NFS4_OPEN_DELEGATE_READ)
  326. return NULL;
  327. if (fp->fi_had_conflict)
  328. return NULL;
  329. if (num_delegations > max_delegations)
  330. return NULL;
  331. dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
  332. if (dp == NULL)
  333. return dp;
  334. dp->dl_stid.sc_type = NFS4_DELEG_STID;
  335. /*
  336. * delegation seqid's are never incremented. The 4.1 special
  337. * meaning of seqid 0 isn't meaningful, really, but let's avoid
  338. * 0 anyway just for consistency and use 1:
  339. */
  340. dp->dl_stid.sc_stateid.si_generation = 1;
  341. num_delegations++;
  342. INIT_LIST_HEAD(&dp->dl_perfile);
  343. INIT_LIST_HEAD(&dp->dl_perclnt);
  344. INIT_LIST_HEAD(&dp->dl_recall_lru);
  345. get_nfs4_file(fp);
  346. dp->dl_file = fp;
  347. dp->dl_type = type;
  348. fh_copy_shallow(&dp->dl_fh, &current_fh->fh_handle);
  349. dp->dl_time = 0;
  350. atomic_set(&dp->dl_count, 1);
  351. nfsd4_init_callback(&dp->dl_recall);
  352. return dp;
  353. }
  354. static void remove_stid(struct nfs4_stid *s)
  355. {
  356. struct idr *stateids = &s->sc_client->cl_stateids;
  357. idr_remove(stateids, s->sc_stateid.si_opaque.so_id);
  358. }
  359. void
  360. nfs4_put_delegation(struct nfs4_delegation *dp)
  361. {
  362. if (atomic_dec_and_test(&dp->dl_count)) {
  363. kmem_cache_free(deleg_slab, dp);
  364. num_delegations--;
  365. }
  366. }
  367. static void nfs4_put_deleg_lease(struct nfs4_file *fp)
  368. {
  369. if (atomic_dec_and_test(&fp->fi_delegees)) {
  370. vfs_setlease(fp->fi_deleg_file, F_UNLCK, &fp->fi_lease);
  371. fp->fi_lease = NULL;
  372. fput(fp->fi_deleg_file);
  373. fp->fi_deleg_file = NULL;
  374. }
  375. }
  376. static void unhash_stid(struct nfs4_stid *s)
  377. {
  378. s->sc_type = 0;
  379. }
  380. /* Called under the state lock. */
  381. static void
  382. unhash_delegation(struct nfs4_delegation *dp)
  383. {
  384. list_del_init(&dp->dl_perclnt);
  385. spin_lock(&recall_lock);
  386. list_del_init(&dp->dl_perfile);
  387. list_del_init(&dp->dl_recall_lru);
  388. spin_unlock(&recall_lock);
  389. nfs4_put_deleg_lease(dp->dl_file);
  390. put_nfs4_file(dp->dl_file);
  391. dp->dl_file = NULL;
  392. }
  393. static void destroy_revoked_delegation(struct nfs4_delegation *dp)
  394. {
  395. list_del_init(&dp->dl_recall_lru);
  396. remove_stid(&dp->dl_stid);
  397. nfs4_put_delegation(dp);
  398. }
  399. static void destroy_delegation(struct nfs4_delegation *dp)
  400. {
  401. unhash_delegation(dp);
  402. remove_stid(&dp->dl_stid);
  403. nfs4_put_delegation(dp);
  404. }
  405. static void revoke_delegation(struct nfs4_delegation *dp)
  406. {
  407. struct nfs4_client *clp = dp->dl_stid.sc_client;
  408. if (clp->cl_minorversion == 0)
  409. destroy_delegation(dp);
  410. else {
  411. unhash_delegation(dp);
  412. dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
  413. list_add(&dp->dl_recall_lru, &clp->cl_revoked);
  414. }
  415. }
  416. /*
  417. * SETCLIENTID state
  418. */
  419. static unsigned int clientid_hashval(u32 id)
  420. {
  421. return id & CLIENT_HASH_MASK;
  422. }
  423. static unsigned int clientstr_hashval(const char *name)
  424. {
  425. return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
  426. }
  427. /*
  428. * We store the NONE, READ, WRITE, and BOTH bits separately in the
  429. * st_{access,deny}_bmap field of the stateid, in order to track not
  430. * only what share bits are currently in force, but also what
  431. * combinations of share bits previous opens have used. This allows us
  432. * to enforce the recommendation of rfc 3530 14.2.19 that the server
  433. * return an error if the client attempt to downgrade to a combination
  434. * of share bits not explicable by closing some of its previous opens.
  435. *
  436. * XXX: This enforcement is actually incomplete, since we don't keep
  437. * track of access/deny bit combinations; so, e.g., we allow:
  438. *
  439. * OPEN allow read, deny write
  440. * OPEN allow both, deny none
  441. * DOWNGRADE allow read, deny none
  442. *
  443. * which we should reject.
  444. */
  445. static unsigned int
  446. bmap_to_share_mode(unsigned long bmap) {
  447. int i;
  448. unsigned int access = 0;
  449. for (i = 1; i < 4; i++) {
  450. if (test_bit(i, &bmap))
  451. access |= i;
  452. }
  453. return access;
  454. }
  455. static bool
  456. test_share(struct nfs4_ol_stateid *stp, struct nfsd4_open *open) {
  457. unsigned int access, deny;
  458. access = bmap_to_share_mode(stp->st_access_bmap);
  459. deny = bmap_to_share_mode(stp->st_deny_bmap);
  460. if ((access & open->op_share_deny) || (deny & open->op_share_access))
  461. return false;
  462. return true;
  463. }
  464. /* set share access for a given stateid */
  465. static inline void
  466. set_access(u32 access, struct nfs4_ol_stateid *stp)
  467. {
  468. __set_bit(access, &stp->st_access_bmap);
  469. }
  470. /* clear share access for a given stateid */
  471. static inline void
  472. clear_access(u32 access, struct nfs4_ol_stateid *stp)
  473. {
  474. __clear_bit(access, &stp->st_access_bmap);
  475. }
  476. /* test whether a given stateid has access */
  477. static inline bool
  478. test_access(u32 access, struct nfs4_ol_stateid *stp)
  479. {
  480. return test_bit(access, &stp->st_access_bmap);
  481. }
  482. /* set share deny for a given stateid */
  483. static inline void
  484. set_deny(u32 access, struct nfs4_ol_stateid *stp)
  485. {
  486. __set_bit(access, &stp->st_deny_bmap);
  487. }
  488. /* clear share deny for a given stateid */
  489. static inline void
  490. clear_deny(u32 access, struct nfs4_ol_stateid *stp)
  491. {
  492. __clear_bit(access, &stp->st_deny_bmap);
  493. }
  494. /* test whether a given stateid is denying specific access */
  495. static inline bool
  496. test_deny(u32 access, struct nfs4_ol_stateid *stp)
  497. {
  498. return test_bit(access, &stp->st_deny_bmap);
  499. }
  500. static int nfs4_access_to_omode(u32 access)
  501. {
  502. switch (access & NFS4_SHARE_ACCESS_BOTH) {
  503. case NFS4_SHARE_ACCESS_READ:
  504. return O_RDONLY;
  505. case NFS4_SHARE_ACCESS_WRITE:
  506. return O_WRONLY;
  507. case NFS4_SHARE_ACCESS_BOTH:
  508. return O_RDWR;
  509. }
  510. WARN_ON_ONCE(1);
  511. return O_RDONLY;
  512. }
  513. /* release all access and file references for a given stateid */
  514. static void
  515. release_all_access(struct nfs4_ol_stateid *stp)
  516. {
  517. int i;
  518. for (i = 1; i < 4; i++) {
  519. if (test_access(i, stp))
  520. nfs4_file_put_access(stp->st_file,
  521. nfs4_access_to_omode(i));
  522. clear_access(i, stp);
  523. }
  524. }
  525. static void unhash_generic_stateid(struct nfs4_ol_stateid *stp)
  526. {
  527. list_del(&stp->st_perfile);
  528. list_del(&stp->st_perstateowner);
  529. }
  530. static void close_generic_stateid(struct nfs4_ol_stateid *stp)
  531. {
  532. release_all_access(stp);
  533. put_nfs4_file(stp->st_file);
  534. stp->st_file = NULL;
  535. }
  536. static void free_generic_stateid(struct nfs4_ol_stateid *stp)
  537. {
  538. remove_stid(&stp->st_stid);
  539. kmem_cache_free(stateid_slab, stp);
  540. }
  541. static void release_lock_stateid(struct nfs4_ol_stateid *stp)
  542. {
  543. struct file *file;
  544. unhash_generic_stateid(stp);
  545. unhash_stid(&stp->st_stid);
  546. file = find_any_file(stp->st_file);
  547. if (file)
  548. locks_remove_posix(file, (fl_owner_t)lockowner(stp->st_stateowner));
  549. close_generic_stateid(stp);
  550. free_generic_stateid(stp);
  551. }
  552. static void unhash_lockowner(struct nfs4_lockowner *lo)
  553. {
  554. struct nfs4_ol_stateid *stp;
  555. list_del(&lo->lo_owner.so_strhash);
  556. list_del(&lo->lo_perstateid);
  557. list_del(&lo->lo_owner_ino_hash);
  558. while (!list_empty(&lo->lo_owner.so_stateids)) {
  559. stp = list_first_entry(&lo->lo_owner.so_stateids,
  560. struct nfs4_ol_stateid, st_perstateowner);
  561. release_lock_stateid(stp);
  562. }
  563. }
  564. static void release_lockowner(struct nfs4_lockowner *lo)
  565. {
  566. unhash_lockowner(lo);
  567. nfs4_free_lockowner(lo);
  568. }
  569. static void
  570. release_stateid_lockowners(struct nfs4_ol_stateid *open_stp)
  571. {
  572. struct nfs4_lockowner *lo;
  573. while (!list_empty(&open_stp->st_lockowners)) {
  574. lo = list_entry(open_stp->st_lockowners.next,
  575. struct nfs4_lockowner, lo_perstateid);
  576. release_lockowner(lo);
  577. }
  578. }
  579. static void unhash_open_stateid(struct nfs4_ol_stateid *stp)
  580. {
  581. unhash_generic_stateid(stp);
  582. release_stateid_lockowners(stp);
  583. close_generic_stateid(stp);
  584. }
  585. static void release_open_stateid(struct nfs4_ol_stateid *stp)
  586. {
  587. unhash_open_stateid(stp);
  588. unhash_stid(&stp->st_stid);
  589. free_generic_stateid(stp);
  590. }
  591. static void unhash_openowner(struct nfs4_openowner *oo)
  592. {
  593. struct nfs4_ol_stateid *stp;
  594. list_del(&oo->oo_owner.so_strhash);
  595. list_del(&oo->oo_perclient);
  596. while (!list_empty(&oo->oo_owner.so_stateids)) {
  597. stp = list_first_entry(&oo->oo_owner.so_stateids,
  598. struct nfs4_ol_stateid, st_perstateowner);
  599. release_open_stateid(stp);
  600. }
  601. }
  602. static void release_last_closed_stateid(struct nfs4_openowner *oo)
  603. {
  604. struct nfs4_ol_stateid *s = oo->oo_last_closed_stid;
  605. if (s) {
  606. unhash_stid(&s->st_stid);
  607. free_generic_stateid(s);
  608. oo->oo_last_closed_stid = NULL;
  609. }
  610. }
  611. static void release_openowner(struct nfs4_openowner *oo)
  612. {
  613. unhash_openowner(oo);
  614. list_del(&oo->oo_close_lru);
  615. release_last_closed_stateid(oo);
  616. nfs4_free_openowner(oo);
  617. }
  618. static inline int
  619. hash_sessionid(struct nfs4_sessionid *sessionid)
  620. {
  621. struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
  622. return sid->sequence % SESSION_HASH_SIZE;
  623. }
  624. #ifdef NFSD_DEBUG
  625. static inline void
  626. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  627. {
  628. u32 *ptr = (u32 *)(&sessionid->data[0]);
  629. dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
  630. }
  631. #else
  632. static inline void
  633. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  634. {
  635. }
  636. #endif
  637. /*
  638. * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
  639. * won't be used for replay.
  640. */
  641. void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
  642. {
  643. struct nfs4_stateowner *so = cstate->replay_owner;
  644. if (nfserr == nfserr_replay_me)
  645. return;
  646. if (!seqid_mutating_err(ntohl(nfserr))) {
  647. cstate->replay_owner = NULL;
  648. return;
  649. }
  650. if (!so)
  651. return;
  652. if (so->so_is_open_owner)
  653. release_last_closed_stateid(openowner(so));
  654. so->so_seqid++;
  655. return;
  656. }
  657. static void
  658. gen_sessionid(struct nfsd4_session *ses)
  659. {
  660. struct nfs4_client *clp = ses->se_client;
  661. struct nfsd4_sessionid *sid;
  662. sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
  663. sid->clientid = clp->cl_clientid;
  664. sid->sequence = current_sessionid++;
  665. sid->reserved = 0;
  666. }
  667. /*
  668. * The protocol defines ca_maxresponssize_cached to include the size of
  669. * the rpc header, but all we need to cache is the data starting after
  670. * the end of the initial SEQUENCE operation--the rest we regenerate
  671. * each time. Therefore we can advertise a ca_maxresponssize_cached
  672. * value that is the number of bytes in our cache plus a few additional
  673. * bytes. In order to stay on the safe side, and not promise more than
  674. * we can cache, those additional bytes must be the minimum possible: 24
  675. * bytes of rpc header (xid through accept state, with AUTH_NULL
  676. * verifier), 12 for the compound header (with zero-length tag), and 44
  677. * for the SEQUENCE op response:
  678. */
  679. #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
  680. static void
  681. free_session_slots(struct nfsd4_session *ses)
  682. {
  683. int i;
  684. for (i = 0; i < ses->se_fchannel.maxreqs; i++)
  685. kfree(ses->se_slots[i]);
  686. }
  687. /*
  688. * We don't actually need to cache the rpc and session headers, so we
  689. * can allocate a little less for each slot:
  690. */
  691. static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
  692. {
  693. u32 size;
  694. if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
  695. size = 0;
  696. else
  697. size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
  698. return size + sizeof(struct nfsd4_slot);
  699. }
  700. /*
  701. * XXX: If we run out of reserved DRC memory we could (up to a point)
  702. * re-negotiate active sessions and reduce their slot usage to make
  703. * room for new connections. For now we just fail the create session.
  704. */
  705. static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
  706. {
  707. u32 slotsize = slot_bytes(ca);
  708. u32 num = ca->maxreqs;
  709. int avail;
  710. spin_lock(&nfsd_drc_lock);
  711. avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
  712. nfsd_drc_max_mem - nfsd_drc_mem_used);
  713. num = min_t(int, num, avail / slotsize);
  714. nfsd_drc_mem_used += num * slotsize;
  715. spin_unlock(&nfsd_drc_lock);
  716. return num;
  717. }
  718. static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
  719. {
  720. int slotsize = slot_bytes(ca);
  721. spin_lock(&nfsd_drc_lock);
  722. nfsd_drc_mem_used -= slotsize * ca->maxreqs;
  723. spin_unlock(&nfsd_drc_lock);
  724. }
  725. static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *attrs)
  726. {
  727. int numslots = attrs->maxreqs;
  728. int slotsize = slot_bytes(attrs);
  729. struct nfsd4_session *new;
  730. int mem, i;
  731. BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
  732. + sizeof(struct nfsd4_session) > PAGE_SIZE);
  733. mem = numslots * sizeof(struct nfsd4_slot *);
  734. new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
  735. if (!new)
  736. return NULL;
  737. /* allocate each struct nfsd4_slot and data cache in one piece */
  738. for (i = 0; i < numslots; i++) {
  739. new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
  740. if (!new->se_slots[i])
  741. goto out_free;
  742. }
  743. return new;
  744. out_free:
  745. while (i--)
  746. kfree(new->se_slots[i]);
  747. kfree(new);
  748. return NULL;
  749. }
  750. static void free_conn(struct nfsd4_conn *c)
  751. {
  752. svc_xprt_put(c->cn_xprt);
  753. kfree(c);
  754. }
  755. static void nfsd4_conn_lost(struct svc_xpt_user *u)
  756. {
  757. struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
  758. struct nfs4_client *clp = c->cn_session->se_client;
  759. spin_lock(&clp->cl_lock);
  760. if (!list_empty(&c->cn_persession)) {
  761. list_del(&c->cn_persession);
  762. free_conn(c);
  763. }
  764. nfsd4_probe_callback(clp);
  765. spin_unlock(&clp->cl_lock);
  766. }
  767. static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
  768. {
  769. struct nfsd4_conn *conn;
  770. conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
  771. if (!conn)
  772. return NULL;
  773. svc_xprt_get(rqstp->rq_xprt);
  774. conn->cn_xprt = rqstp->rq_xprt;
  775. conn->cn_flags = flags;
  776. INIT_LIST_HEAD(&conn->cn_xpt_user.list);
  777. return conn;
  778. }
  779. static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  780. {
  781. conn->cn_session = ses;
  782. list_add(&conn->cn_persession, &ses->se_conns);
  783. }
  784. static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  785. {
  786. struct nfs4_client *clp = ses->se_client;
  787. spin_lock(&clp->cl_lock);
  788. __nfsd4_hash_conn(conn, ses);
  789. spin_unlock(&clp->cl_lock);
  790. }
  791. static int nfsd4_register_conn(struct nfsd4_conn *conn)
  792. {
  793. conn->cn_xpt_user.callback = nfsd4_conn_lost;
  794. return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
  795. }
  796. static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
  797. {
  798. int ret;
  799. nfsd4_hash_conn(conn, ses);
  800. ret = nfsd4_register_conn(conn);
  801. if (ret)
  802. /* oops; xprt is already down: */
  803. nfsd4_conn_lost(&conn->cn_xpt_user);
  804. if (conn->cn_flags & NFS4_CDFC4_BACK) {
  805. /* callback channel may be back up */
  806. nfsd4_probe_callback(ses->se_client);
  807. }
  808. }
  809. static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
  810. {
  811. u32 dir = NFS4_CDFC4_FORE;
  812. if (cses->flags & SESSION4_BACK_CHAN)
  813. dir |= NFS4_CDFC4_BACK;
  814. return alloc_conn(rqstp, dir);
  815. }
  816. /* must be called under client_lock */
  817. static void nfsd4_del_conns(struct nfsd4_session *s)
  818. {
  819. struct nfs4_client *clp = s->se_client;
  820. struct nfsd4_conn *c;
  821. spin_lock(&clp->cl_lock);
  822. while (!list_empty(&s->se_conns)) {
  823. c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
  824. list_del_init(&c->cn_persession);
  825. spin_unlock(&clp->cl_lock);
  826. unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
  827. free_conn(c);
  828. spin_lock(&clp->cl_lock);
  829. }
  830. spin_unlock(&clp->cl_lock);
  831. }
  832. static void __free_session(struct nfsd4_session *ses)
  833. {
  834. free_session_slots(ses);
  835. kfree(ses);
  836. }
  837. static void free_session(struct nfsd4_session *ses)
  838. {
  839. struct nfsd_net *nn = net_generic(ses->se_client->net, nfsd_net_id);
  840. lockdep_assert_held(&nn->client_lock);
  841. nfsd4_del_conns(ses);
  842. nfsd4_put_drc_mem(&ses->se_fchannel);
  843. __free_session(ses);
  844. }
  845. static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
  846. {
  847. int idx;
  848. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  849. new->se_client = clp;
  850. gen_sessionid(new);
  851. INIT_LIST_HEAD(&new->se_conns);
  852. new->se_cb_seq_nr = 1;
  853. new->se_flags = cses->flags;
  854. new->se_cb_prog = cses->callback_prog;
  855. new->se_cb_sec = cses->cb_sec;
  856. atomic_set(&new->se_ref, 0);
  857. idx = hash_sessionid(&new->se_sessionid);
  858. spin_lock(&nn->client_lock);
  859. list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
  860. spin_lock(&clp->cl_lock);
  861. list_add(&new->se_perclnt, &clp->cl_sessions);
  862. spin_unlock(&clp->cl_lock);
  863. spin_unlock(&nn->client_lock);
  864. memcpy(&new->se_fchannel, &cses->fore_channel,
  865. sizeof(struct nfsd4_channel_attrs));
  866. if (cses->flags & SESSION4_BACK_CHAN) {
  867. struct sockaddr *sa = svc_addr(rqstp);
  868. /*
  869. * This is a little silly; with sessions there's no real
  870. * use for the callback address. Use the peer address
  871. * as a reasonable default for now, but consider fixing
  872. * the rpc client not to require an address in the
  873. * future:
  874. */
  875. rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
  876. clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
  877. }
  878. }
  879. /* caller must hold client_lock */
  880. static struct nfsd4_session *
  881. find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
  882. {
  883. struct nfsd4_session *elem;
  884. int idx;
  885. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  886. dump_sessionid(__func__, sessionid);
  887. idx = hash_sessionid(sessionid);
  888. /* Search in the appropriate list */
  889. list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
  890. if (!memcmp(elem->se_sessionid.data, sessionid->data,
  891. NFS4_MAX_SESSIONID_LEN)) {
  892. return elem;
  893. }
  894. }
  895. dprintk("%s: session not found\n", __func__);
  896. return NULL;
  897. }
  898. /* caller must hold client_lock */
  899. static void
  900. unhash_session(struct nfsd4_session *ses)
  901. {
  902. list_del(&ses->se_hash);
  903. spin_lock(&ses->se_client->cl_lock);
  904. list_del(&ses->se_perclnt);
  905. spin_unlock(&ses->se_client->cl_lock);
  906. }
  907. /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
  908. static int
  909. STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
  910. {
  911. if (clid->cl_boot == nn->boot_time)
  912. return 0;
  913. dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
  914. clid->cl_boot, clid->cl_id, nn->boot_time);
  915. return 1;
  916. }
  917. /*
  918. * XXX Should we use a slab cache ?
  919. * This type of memory management is somewhat inefficient, but we use it
  920. * anyway since SETCLIENTID is not a common operation.
  921. */
  922. static struct nfs4_client *alloc_client(struct xdr_netobj name)
  923. {
  924. struct nfs4_client *clp;
  925. clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
  926. if (clp == NULL)
  927. return NULL;
  928. clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
  929. if (clp->cl_name.data == NULL) {
  930. kfree(clp);
  931. return NULL;
  932. }
  933. clp->cl_name.len = name.len;
  934. return clp;
  935. }
  936. static inline void
  937. free_client(struct nfs4_client *clp)
  938. {
  939. struct nfsd_net __maybe_unused *nn = net_generic(clp->net, nfsd_net_id);
  940. lockdep_assert_held(&nn->client_lock);
  941. while (!list_empty(&clp->cl_sessions)) {
  942. struct nfsd4_session *ses;
  943. ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
  944. se_perclnt);
  945. list_del(&ses->se_perclnt);
  946. WARN_ON_ONCE(atomic_read(&ses->se_ref));
  947. free_session(ses);
  948. }
  949. free_svc_cred(&clp->cl_cred);
  950. kfree(clp->cl_name.data);
  951. idr_destroy(&clp->cl_stateids);
  952. kfree(clp);
  953. }
  954. /* must be called under the client_lock */
  955. static inline void
  956. unhash_client_locked(struct nfs4_client *clp)
  957. {
  958. struct nfsd4_session *ses;
  959. list_del(&clp->cl_lru);
  960. spin_lock(&clp->cl_lock);
  961. list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
  962. list_del_init(&ses->se_hash);
  963. spin_unlock(&clp->cl_lock);
  964. }
  965. static void
  966. destroy_client(struct nfs4_client *clp)
  967. {
  968. struct nfs4_openowner *oo;
  969. struct nfs4_delegation *dp;
  970. struct list_head reaplist;
  971. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  972. INIT_LIST_HEAD(&reaplist);
  973. spin_lock(&recall_lock);
  974. while (!list_empty(&clp->cl_delegations)) {
  975. dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
  976. list_del_init(&dp->dl_perclnt);
  977. list_move(&dp->dl_recall_lru, &reaplist);
  978. }
  979. spin_unlock(&recall_lock);
  980. while (!list_empty(&reaplist)) {
  981. dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
  982. destroy_delegation(dp);
  983. }
  984. while (!list_empty(&clp->cl_openowners)) {
  985. oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
  986. release_openowner(oo);
  987. }
  988. nfsd4_shutdown_callback(clp);
  989. if (clp->cl_cb_conn.cb_xprt)
  990. svc_xprt_put(clp->cl_cb_conn.cb_xprt);
  991. list_del(&clp->cl_idhash);
  992. if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
  993. rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
  994. else
  995. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  996. spin_lock(&nn->client_lock);
  997. unhash_client_locked(clp);
  998. WARN_ON_ONCE(atomic_read(&clp->cl_refcount));
  999. free_client(clp);
  1000. spin_unlock(&nn->client_lock);
  1001. }
  1002. static void expire_client(struct nfs4_client *clp)
  1003. {
  1004. nfsd4_client_record_remove(clp);
  1005. destroy_client(clp);
  1006. }
  1007. static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
  1008. {
  1009. memcpy(target->cl_verifier.data, source->data,
  1010. sizeof(target->cl_verifier.data));
  1011. }
  1012. static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
  1013. {
  1014. target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
  1015. target->cl_clientid.cl_id = source->cl_clientid.cl_id;
  1016. }
  1017. static int copy_cred(struct svc_cred *target, struct svc_cred *source)
  1018. {
  1019. if (source->cr_principal) {
  1020. target->cr_principal =
  1021. kstrdup(source->cr_principal, GFP_KERNEL);
  1022. if (target->cr_principal == NULL)
  1023. return -ENOMEM;
  1024. } else
  1025. target->cr_principal = NULL;
  1026. target->cr_flavor = source->cr_flavor;
  1027. target->cr_uid = source->cr_uid;
  1028. target->cr_gid = source->cr_gid;
  1029. target->cr_group_info = source->cr_group_info;
  1030. get_group_info(target->cr_group_info);
  1031. return 0;
  1032. }
  1033. static long long
  1034. compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
  1035. {
  1036. long long res;
  1037. res = o1->len - o2->len;
  1038. if (res)
  1039. return res;
  1040. return (long long)memcmp(o1->data, o2->data, o1->len);
  1041. }
  1042. static int same_name(const char *n1, const char *n2)
  1043. {
  1044. return 0 == memcmp(n1, n2, HEXDIR_LEN);
  1045. }
  1046. static int
  1047. same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
  1048. {
  1049. return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
  1050. }
  1051. static int
  1052. same_clid(clientid_t *cl1, clientid_t *cl2)
  1053. {
  1054. return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
  1055. }
  1056. static bool groups_equal(struct group_info *g1, struct group_info *g2)
  1057. {
  1058. int i;
  1059. if (g1->ngroups != g2->ngroups)
  1060. return false;
  1061. for (i=0; i<g1->ngroups; i++)
  1062. if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
  1063. return false;
  1064. return true;
  1065. }
  1066. /*
  1067. * RFC 3530 language requires clid_inuse be returned when the
  1068. * "principal" associated with a requests differs from that previously
  1069. * used. We use uid, gid's, and gss principal string as our best
  1070. * approximation. We also don't want to allow non-gss use of a client
  1071. * established using gss: in theory cr_principal should catch that
  1072. * change, but in practice cr_principal can be null even in the gss case
  1073. * since gssd doesn't always pass down a principal string.
  1074. */
  1075. static bool is_gss_cred(struct svc_cred *cr)
  1076. {
  1077. /* Is cr_flavor one of the gss "pseudoflavors"?: */
  1078. return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
  1079. }
  1080. static bool
  1081. same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
  1082. {
  1083. if ((is_gss_cred(cr1) != is_gss_cred(cr2))
  1084. || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
  1085. || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
  1086. || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
  1087. return false;
  1088. if (cr1->cr_principal == cr2->cr_principal)
  1089. return true;
  1090. if (!cr1->cr_principal || !cr2->cr_principal)
  1091. return false;
  1092. return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
  1093. }
  1094. static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
  1095. {
  1096. static u32 current_clientid = 1;
  1097. clp->cl_clientid.cl_boot = nn->boot_time;
  1098. clp->cl_clientid.cl_id = current_clientid++;
  1099. }
  1100. static void gen_confirm(struct nfs4_client *clp)
  1101. {
  1102. __be32 verf[2];
  1103. static u32 i;
  1104. verf[0] = (__be32)get_seconds();
  1105. verf[1] = (__be32)i++;
  1106. memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
  1107. }
  1108. static struct nfs4_stid *find_stateid(struct nfs4_client *cl, stateid_t *t)
  1109. {
  1110. struct nfs4_stid *ret;
  1111. ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
  1112. if (!ret || !ret->sc_type)
  1113. return NULL;
  1114. return ret;
  1115. }
  1116. static struct nfs4_stid *find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
  1117. {
  1118. struct nfs4_stid *s;
  1119. s = find_stateid(cl, t);
  1120. if (!s)
  1121. return NULL;
  1122. if (typemask & s->sc_type)
  1123. return s;
  1124. return NULL;
  1125. }
  1126. static struct nfs4_client *create_client(struct xdr_netobj name,
  1127. struct svc_rqst *rqstp, nfs4_verifier *verf)
  1128. {
  1129. struct nfs4_client *clp;
  1130. struct sockaddr *sa = svc_addr(rqstp);
  1131. int ret;
  1132. struct net *net = SVC_NET(rqstp);
  1133. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1134. clp = alloc_client(name);
  1135. if (clp == NULL)
  1136. return NULL;
  1137. INIT_LIST_HEAD(&clp->cl_sessions);
  1138. ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
  1139. if (ret) {
  1140. spin_lock(&nn->client_lock);
  1141. free_client(clp);
  1142. spin_unlock(&nn->client_lock);
  1143. return NULL;
  1144. }
  1145. idr_init(&clp->cl_stateids);
  1146. atomic_set(&clp->cl_refcount, 0);
  1147. clp->cl_cb_state = NFSD4_CB_UNKNOWN;
  1148. INIT_LIST_HEAD(&clp->cl_idhash);
  1149. INIT_LIST_HEAD(&clp->cl_openowners);
  1150. INIT_LIST_HEAD(&clp->cl_delegations);
  1151. INIT_LIST_HEAD(&clp->cl_lru);
  1152. INIT_LIST_HEAD(&clp->cl_callbacks);
  1153. INIT_LIST_HEAD(&clp->cl_revoked);
  1154. spin_lock_init(&clp->cl_lock);
  1155. nfsd4_init_callback(&clp->cl_cb_null);
  1156. clp->cl_time = get_seconds();
  1157. clear_bit(0, &clp->cl_cb_slot_busy);
  1158. rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
  1159. copy_verf(clp, verf);
  1160. rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
  1161. gen_confirm(clp);
  1162. clp->cl_cb_session = NULL;
  1163. clp->net = net;
  1164. return clp;
  1165. }
  1166. static void
  1167. add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
  1168. {
  1169. struct rb_node **new = &(root->rb_node), *parent = NULL;
  1170. struct nfs4_client *clp;
  1171. while (*new) {
  1172. clp = rb_entry(*new, struct nfs4_client, cl_namenode);
  1173. parent = *new;
  1174. if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
  1175. new = &((*new)->rb_left);
  1176. else
  1177. new = &((*new)->rb_right);
  1178. }
  1179. rb_link_node(&new_clp->cl_namenode, parent, new);
  1180. rb_insert_color(&new_clp->cl_namenode, root);
  1181. }
  1182. static struct nfs4_client *
  1183. find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
  1184. {
  1185. long long cmp;
  1186. struct rb_node *node = root->rb_node;
  1187. struct nfs4_client *clp;
  1188. while (node) {
  1189. clp = rb_entry(node, struct nfs4_client, cl_namenode);
  1190. cmp = compare_blob(&clp->cl_name, name);
  1191. if (cmp > 0)
  1192. node = node->rb_left;
  1193. else if (cmp < 0)
  1194. node = node->rb_right;
  1195. else
  1196. return clp;
  1197. }
  1198. return NULL;
  1199. }
  1200. static void
  1201. add_to_unconfirmed(struct nfs4_client *clp)
  1202. {
  1203. unsigned int idhashval;
  1204. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1205. clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1206. add_clp_to_name_tree(clp, &nn->unconf_name_tree);
  1207. idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1208. list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
  1209. renew_client(clp);
  1210. }
  1211. static void
  1212. move_to_confirmed(struct nfs4_client *clp)
  1213. {
  1214. unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1215. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1216. dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
  1217. list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
  1218. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  1219. add_clp_to_name_tree(clp, &nn->conf_name_tree);
  1220. set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1221. renew_client(clp);
  1222. }
  1223. static struct nfs4_client *
  1224. find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
  1225. {
  1226. struct nfs4_client *clp;
  1227. unsigned int idhashval = clientid_hashval(clid->cl_id);
  1228. list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
  1229. if (same_clid(&clp->cl_clientid, clid)) {
  1230. if ((bool)clp->cl_minorversion != sessions)
  1231. return NULL;
  1232. renew_client(clp);
  1233. return clp;
  1234. }
  1235. }
  1236. return NULL;
  1237. }
  1238. static struct nfs4_client *
  1239. find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1240. {
  1241. struct list_head *tbl = nn->conf_id_hashtbl;
  1242. return find_client_in_id_table(tbl, clid, sessions);
  1243. }
  1244. static struct nfs4_client *
  1245. find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1246. {
  1247. struct list_head *tbl = nn->unconf_id_hashtbl;
  1248. return find_client_in_id_table(tbl, clid, sessions);
  1249. }
  1250. static bool clp_used_exchangeid(struct nfs4_client *clp)
  1251. {
  1252. return clp->cl_exchange_flags != 0;
  1253. }
  1254. static struct nfs4_client *
  1255. find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1256. {
  1257. return find_clp_in_name_tree(name, &nn->conf_name_tree);
  1258. }
  1259. static struct nfs4_client *
  1260. find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1261. {
  1262. return find_clp_in_name_tree(name, &nn->unconf_name_tree);
  1263. }
  1264. static void
  1265. gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
  1266. {
  1267. struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
  1268. struct sockaddr *sa = svc_addr(rqstp);
  1269. u32 scopeid = rpc_get_scope_id(sa);
  1270. unsigned short expected_family;
  1271. /* Currently, we only support tcp and tcp6 for the callback channel */
  1272. if (se->se_callback_netid_len == 3 &&
  1273. !memcmp(se->se_callback_netid_val, "tcp", 3))
  1274. expected_family = AF_INET;
  1275. else if (se->se_callback_netid_len == 4 &&
  1276. !memcmp(se->se_callback_netid_val, "tcp6", 4))
  1277. expected_family = AF_INET6;
  1278. else
  1279. goto out_err;
  1280. conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
  1281. se->se_callback_addr_len,
  1282. (struct sockaddr *)&conn->cb_addr,
  1283. sizeof(conn->cb_addr));
  1284. if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
  1285. goto out_err;
  1286. if (conn->cb_addr.ss_family == AF_INET6)
  1287. ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
  1288. conn->cb_prog = se->se_callback_prog;
  1289. conn->cb_ident = se->se_callback_ident;
  1290. memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
  1291. return;
  1292. out_err:
  1293. conn->cb_addr.ss_family = AF_UNSPEC;
  1294. conn->cb_addrlen = 0;
  1295. dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
  1296. "will not receive delegations\n",
  1297. clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
  1298. return;
  1299. }
  1300. /*
  1301. * Cache a reply. nfsd4_check_drc_limit() has bounded the cache size.
  1302. */
  1303. void
  1304. nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
  1305. {
  1306. struct nfsd4_slot *slot = resp->cstate.slot;
  1307. unsigned int base;
  1308. dprintk("--> %s slot %p\n", __func__, slot);
  1309. slot->sl_opcnt = resp->opcnt;
  1310. slot->sl_status = resp->cstate.status;
  1311. slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
  1312. if (nfsd4_not_cached(resp)) {
  1313. slot->sl_datalen = 0;
  1314. return;
  1315. }
  1316. slot->sl_datalen = (char *)resp->p - (char *)resp->cstate.datap;
  1317. base = (char *)resp->cstate.datap -
  1318. (char *)resp->xbuf->head[0].iov_base;
  1319. if (read_bytes_from_xdr_buf(resp->xbuf, base, slot->sl_data,
  1320. slot->sl_datalen))
  1321. WARN("%s: sessions DRC could not cache compound\n", __func__);
  1322. return;
  1323. }
  1324. /*
  1325. * Encode the replay sequence operation from the slot values.
  1326. * If cachethis is FALSE encode the uncached rep error on the next
  1327. * operation which sets resp->p and increments resp->opcnt for
  1328. * nfs4svc_encode_compoundres.
  1329. *
  1330. */
  1331. static __be32
  1332. nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
  1333. struct nfsd4_compoundres *resp)
  1334. {
  1335. struct nfsd4_op *op;
  1336. struct nfsd4_slot *slot = resp->cstate.slot;
  1337. /* Encode the replayed sequence operation */
  1338. op = &args->ops[resp->opcnt - 1];
  1339. nfsd4_encode_operation(resp, op);
  1340. /* Return nfserr_retry_uncached_rep in next operation. */
  1341. if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
  1342. op = &args->ops[resp->opcnt++];
  1343. op->status = nfserr_retry_uncached_rep;
  1344. nfsd4_encode_operation(resp, op);
  1345. }
  1346. return op->status;
  1347. }
  1348. /*
  1349. * The sequence operation is not cached because we can use the slot and
  1350. * session values.
  1351. */
  1352. __be32
  1353. nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
  1354. struct nfsd4_sequence *seq)
  1355. {
  1356. struct nfsd4_slot *slot = resp->cstate.slot;
  1357. __be32 status;
  1358. dprintk("--> %s slot %p\n", __func__, slot);
  1359. /* Either returns 0 or nfserr_retry_uncached */
  1360. status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
  1361. if (status == nfserr_retry_uncached_rep)
  1362. return status;
  1363. /* The sequence operation has been encoded, cstate->datap set. */
  1364. memcpy(resp->cstate.datap, slot->sl_data, slot->sl_datalen);
  1365. resp->opcnt = slot->sl_opcnt;
  1366. resp->p = resp->cstate.datap + XDR_QUADLEN(slot->sl_datalen);
  1367. status = slot->sl_status;
  1368. return status;
  1369. }
  1370. /*
  1371. * Set the exchange_id flags returned by the server.
  1372. */
  1373. static void
  1374. nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
  1375. {
  1376. /* pNFS is not supported */
  1377. new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
  1378. /* Referrals are supported, Migration is not. */
  1379. new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
  1380. /* set the wire flags to return to client. */
  1381. clid->flags = new->cl_exchange_flags;
  1382. }
  1383. static bool client_has_state(struct nfs4_client *clp)
  1384. {
  1385. /*
  1386. * Note clp->cl_openowners check isn't quite right: there's no
  1387. * need to count owners without stateid's.
  1388. *
  1389. * Also note we should probably be using this in 4.0 case too.
  1390. */
  1391. return !list_empty(&clp->cl_openowners)
  1392. || !list_empty(&clp->cl_delegations)
  1393. || !list_empty(&clp->cl_sessions);
  1394. }
  1395. __be32
  1396. nfsd4_exchange_id(struct svc_rqst *rqstp,
  1397. struct nfsd4_compound_state *cstate,
  1398. struct nfsd4_exchange_id *exid)
  1399. {
  1400. struct nfs4_client *unconf, *conf, *new;
  1401. __be32 status;
  1402. char addr_str[INET6_ADDRSTRLEN];
  1403. nfs4_verifier verf = exid->verifier;
  1404. struct sockaddr *sa = svc_addr(rqstp);
  1405. bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
  1406. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1407. rpc_ntop(sa, addr_str, sizeof(addr_str));
  1408. dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
  1409. "ip_addr=%s flags %x, spa_how %d\n",
  1410. __func__, rqstp, exid, exid->clname.len, exid->clname.data,
  1411. addr_str, exid->flags, exid->spa_how);
  1412. if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
  1413. return nfserr_inval;
  1414. /* Currently only support SP4_NONE */
  1415. switch (exid->spa_how) {
  1416. case SP4_NONE:
  1417. break;
  1418. default: /* checked by xdr code */
  1419. WARN_ON_ONCE(1);
  1420. case SP4_SSV:
  1421. case SP4_MACH_CRED:
  1422. return nfserr_serverfault; /* no excuse :-/ */
  1423. }
  1424. /* Cases below refer to rfc 5661 section 18.35.4: */
  1425. nfs4_lock_state();
  1426. conf = find_confirmed_client_by_name(&exid->clname, nn);
  1427. if (conf) {
  1428. bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
  1429. bool verfs_match = same_verf(&verf, &conf->cl_verifier);
  1430. if (update) {
  1431. if (!clp_used_exchangeid(conf)) { /* buggy client */
  1432. status = nfserr_inval;
  1433. goto out;
  1434. }
  1435. if (!creds_match) { /* case 9 */
  1436. status = nfserr_perm;
  1437. goto out;
  1438. }
  1439. if (!verfs_match) { /* case 8 */
  1440. status = nfserr_not_same;
  1441. goto out;
  1442. }
  1443. /* case 6 */
  1444. exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1445. new = conf;
  1446. goto out_copy;
  1447. }
  1448. if (!creds_match) { /* case 3 */
  1449. if (client_has_state(conf)) {
  1450. status = nfserr_clid_inuse;
  1451. goto out;
  1452. }
  1453. expire_client(conf);
  1454. goto out_new;
  1455. }
  1456. if (verfs_match) { /* case 2 */
  1457. conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1458. new = conf;
  1459. goto out_copy;
  1460. }
  1461. /* case 5, client reboot */
  1462. goto out_new;
  1463. }
  1464. if (update) { /* case 7 */
  1465. status = nfserr_noent;
  1466. goto out;
  1467. }
  1468. unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
  1469. if (unconf) /* case 4, possible retry or client restart */
  1470. expire_client(unconf);
  1471. /* case 1 (normal case) */
  1472. out_new:
  1473. new = create_client(exid->clname, rqstp, &verf);
  1474. if (new == NULL) {
  1475. status = nfserr_jukebox;
  1476. goto out;
  1477. }
  1478. new->cl_minorversion = 1;
  1479. gen_clid(new, nn);
  1480. add_to_unconfirmed(new);
  1481. out_copy:
  1482. exid->clientid.cl_boot = new->cl_clientid.cl_boot;
  1483. exid->clientid.cl_id = new->cl_clientid.cl_id;
  1484. exid->seqid = new->cl_cs_slot.sl_seqid + 1;
  1485. nfsd4_set_ex_flags(new, exid);
  1486. dprintk("nfsd4_exchange_id seqid %d flags %x\n",
  1487. new->cl_cs_slot.sl_seqid, new->cl_exchange_flags);
  1488. status = nfs_ok;
  1489. out:
  1490. nfs4_unlock_state();
  1491. return status;
  1492. }
  1493. static __be32
  1494. check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
  1495. {
  1496. dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
  1497. slot_seqid);
  1498. /* The slot is in use, and no response has been sent. */
  1499. if (slot_inuse) {
  1500. if (seqid == slot_seqid)
  1501. return nfserr_jukebox;
  1502. else
  1503. return nfserr_seq_misordered;
  1504. }
  1505. /* Note unsigned 32-bit arithmetic handles wraparound: */
  1506. if (likely(seqid == slot_seqid + 1))
  1507. return nfs_ok;
  1508. if (seqid == slot_seqid)
  1509. return nfserr_replay_cache;
  1510. return nfserr_seq_misordered;
  1511. }
  1512. /*
  1513. * Cache the create session result into the create session single DRC
  1514. * slot cache by saving the xdr structure. sl_seqid has been set.
  1515. * Do this for solo or embedded create session operations.
  1516. */
  1517. static void
  1518. nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
  1519. struct nfsd4_clid_slot *slot, __be32 nfserr)
  1520. {
  1521. slot->sl_status = nfserr;
  1522. memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
  1523. }
  1524. static __be32
  1525. nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
  1526. struct nfsd4_clid_slot *slot)
  1527. {
  1528. memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
  1529. return slot->sl_status;
  1530. }
  1531. #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
  1532. 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
  1533. 1 + /* MIN tag is length with zero, only length */ \
  1534. 3 + /* version, opcount, opcode */ \
  1535. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1536. /* seqid, slotID, slotID, cache */ \
  1537. 4 ) * sizeof(__be32))
  1538. #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
  1539. 2 + /* verifier: AUTH_NULL, length 0 */\
  1540. 1 + /* status */ \
  1541. 1 + /* MIN tag is length with zero, only length */ \
  1542. 3 + /* opcount, opcode, opstatus*/ \
  1543. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1544. /* seqid, slotID, slotID, slotID, status */ \
  1545. 5 ) * sizeof(__be32))
  1546. static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
  1547. {
  1548. u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
  1549. if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
  1550. return nfserr_toosmall;
  1551. if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
  1552. return nfserr_toosmall;
  1553. ca->headerpadsz = 0;
  1554. ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
  1555. ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
  1556. ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
  1557. ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
  1558. NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
  1559. ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
  1560. /*
  1561. * Note decreasing slot size below client's request may make it
  1562. * difficult for client to function correctly, whereas
  1563. * decreasing the number of slots will (just?) affect
  1564. * performance. When short on memory we therefore prefer to
  1565. * decrease number of slots instead of their size. Clients that
  1566. * request larger slots than they need will get poor results:
  1567. */
  1568. ca->maxreqs = nfsd4_get_drc_mem(ca);
  1569. if (!ca->maxreqs)
  1570. return nfserr_jukebox;
  1571. return nfs_ok;
  1572. }
  1573. static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
  1574. {
  1575. ca->headerpadsz = 0;
  1576. /*
  1577. * These RPC_MAX_HEADER macros are overkill, especially since we
  1578. * don't even do gss on the backchannel yet. But this is still
  1579. * less than 1k. Tighten up this estimate in the unlikely event
  1580. * it turns out to be a problem for some client:
  1581. */
  1582. if (ca->maxreq_sz < NFS4_enc_cb_recall_sz + RPC_MAX_HEADER_WITH_AUTH)
  1583. return nfserr_toosmall;
  1584. if (ca->maxresp_sz < NFS4_dec_cb_recall_sz + RPC_MAX_REPHEADER_WITH_AUTH)
  1585. return nfserr_toosmall;
  1586. ca->maxresp_cached = 0;
  1587. if (ca->maxops < 2)
  1588. return nfserr_toosmall;
  1589. return nfs_ok;
  1590. }
  1591. __be32
  1592. nfsd4_create_session(struct svc_rqst *rqstp,
  1593. struct nfsd4_compound_state *cstate,
  1594. struct nfsd4_create_session *cr_ses)
  1595. {
  1596. struct sockaddr *sa = svc_addr(rqstp);
  1597. struct nfs4_client *conf, *unconf;
  1598. struct nfsd4_session *new;
  1599. struct nfsd4_conn *conn;
  1600. struct nfsd4_clid_slot *cs_slot = NULL;
  1601. __be32 status = 0;
  1602. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1603. if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
  1604. return nfserr_inval;
  1605. status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
  1606. if (status)
  1607. return status;
  1608. status = check_backchannel_attrs(&cr_ses->back_channel);
  1609. if (status)
  1610. return status;
  1611. status = nfserr_jukebox;
  1612. new = alloc_session(&cr_ses->fore_channel);
  1613. if (!new)
  1614. goto out_release_drc_mem;
  1615. conn = alloc_conn_from_crses(rqstp, cr_ses);
  1616. if (!conn)
  1617. goto out_free_session;
  1618. nfs4_lock_state();
  1619. unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
  1620. conf = find_confirmed_client(&cr_ses->clientid, true, nn);
  1621. WARN_ON_ONCE(conf && unconf);
  1622. if (conf) {
  1623. cs_slot = &conf->cl_cs_slot;
  1624. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  1625. if (status == nfserr_replay_cache) {
  1626. status = nfsd4_replay_create_session(cr_ses, cs_slot);
  1627. goto out_free_conn;
  1628. } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
  1629. status = nfserr_seq_misordered;
  1630. goto out_free_conn;
  1631. }
  1632. } else if (unconf) {
  1633. struct nfs4_client *old;
  1634. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
  1635. !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
  1636. status = nfserr_clid_inuse;
  1637. goto out_free_conn;
  1638. }
  1639. cs_slot = &unconf->cl_cs_slot;
  1640. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  1641. if (status) {
  1642. /* an unconfirmed replay returns misordered */
  1643. status = nfserr_seq_misordered;
  1644. goto out_free_conn;
  1645. }
  1646. old = find_confirmed_client_by_name(&unconf->cl_name, nn);
  1647. if (old) {
  1648. status = mark_client_expired(old);
  1649. if (status)
  1650. goto out_free_conn;
  1651. expire_client(old);
  1652. }
  1653. move_to_confirmed(unconf);
  1654. conf = unconf;
  1655. } else {
  1656. status = nfserr_stale_clientid;
  1657. goto out_free_conn;
  1658. }
  1659. status = nfs_ok;
  1660. /*
  1661. * We do not support RDMA or persistent sessions
  1662. */
  1663. cr_ses->flags &= ~SESSION4_PERSIST;
  1664. cr_ses->flags &= ~SESSION4_RDMA;
  1665. init_session(rqstp, new, conf, cr_ses);
  1666. nfsd4_init_conn(rqstp, conn, new);
  1667. memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
  1668. NFS4_MAX_SESSIONID_LEN);
  1669. cs_slot->sl_seqid++;
  1670. cr_ses->seqid = cs_slot->sl_seqid;
  1671. /* cache solo and embedded create sessions under the state lock */
  1672. nfsd4_cache_create_session(cr_ses, cs_slot, status);
  1673. nfs4_unlock_state();
  1674. return status;
  1675. out_free_conn:
  1676. nfs4_unlock_state();
  1677. free_conn(conn);
  1678. out_free_session:
  1679. __free_session(new);
  1680. out_release_drc_mem:
  1681. nfsd4_put_drc_mem(&cr_ses->fore_channel);
  1682. return status;
  1683. }
  1684. static __be32 nfsd4_map_bcts_dir(u32 *dir)
  1685. {
  1686. switch (*dir) {
  1687. case NFS4_CDFC4_FORE:
  1688. case NFS4_CDFC4_BACK:
  1689. return nfs_ok;
  1690. case NFS4_CDFC4_FORE_OR_BOTH:
  1691. case NFS4_CDFC4_BACK_OR_BOTH:
  1692. *dir = NFS4_CDFC4_BOTH;
  1693. return nfs_ok;
  1694. };
  1695. return nfserr_inval;
  1696. }
  1697. __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
  1698. {
  1699. struct nfsd4_session *session = cstate->session;
  1700. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1701. spin_lock(&nn->client_lock);
  1702. session->se_cb_prog = bc->bc_cb_program;
  1703. session->se_cb_sec = bc->bc_cb_sec;
  1704. spin_unlock(&nn->client_lock);
  1705. nfsd4_probe_callback(session->se_client);
  1706. return nfs_ok;
  1707. }
  1708. __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
  1709. struct nfsd4_compound_state *cstate,
  1710. struct nfsd4_bind_conn_to_session *bcts)
  1711. {
  1712. __be32 status;
  1713. struct nfsd4_conn *conn;
  1714. struct nfsd4_session *session;
  1715. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1716. if (!nfsd4_last_compound_op(rqstp))
  1717. return nfserr_not_only_op;
  1718. nfs4_lock_state();
  1719. spin_lock(&nn->client_lock);
  1720. session = find_in_sessionid_hashtbl(&bcts->sessionid, SVC_NET(rqstp));
  1721. spin_unlock(&nn->client_lock);
  1722. status = nfserr_badsession;
  1723. if (!session)
  1724. goto out;
  1725. status = nfsd4_map_bcts_dir(&bcts->dir);
  1726. if (status)
  1727. goto out;
  1728. conn = alloc_conn(rqstp, bcts->dir);
  1729. status = nfserr_jukebox;
  1730. if (!conn)
  1731. goto out;
  1732. nfsd4_init_conn(rqstp, conn, session);
  1733. status = nfs_ok;
  1734. out:
  1735. nfs4_unlock_state();
  1736. return status;
  1737. }
  1738. static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
  1739. {
  1740. if (!session)
  1741. return 0;
  1742. return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
  1743. }
  1744. __be32
  1745. nfsd4_destroy_session(struct svc_rqst *r,
  1746. struct nfsd4_compound_state *cstate,
  1747. struct nfsd4_destroy_session *sessionid)
  1748. {
  1749. struct nfsd4_session *ses;
  1750. __be32 status;
  1751. struct nfsd_net *nn = net_generic(SVC_NET(r), nfsd_net_id);
  1752. nfs4_lock_state();
  1753. status = nfserr_not_only_op;
  1754. if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
  1755. if (!nfsd4_last_compound_op(r))
  1756. goto out;
  1757. }
  1758. dump_sessionid(__func__, &sessionid->sessionid);
  1759. spin_lock(&nn->client_lock);
  1760. ses = find_in_sessionid_hashtbl(&sessionid->sessionid, SVC_NET(r));
  1761. status = nfserr_badsession;
  1762. if (!ses)
  1763. goto out_client_lock;
  1764. status = mark_session_dead_locked(ses);
  1765. if (status)
  1766. goto out_client_lock;
  1767. unhash_session(ses);
  1768. spin_unlock(&nn->client_lock);
  1769. nfsd4_probe_callback_sync(ses->se_client);
  1770. spin_lock(&nn->client_lock);
  1771. free_session(ses);
  1772. status = nfs_ok;
  1773. out_client_lock:
  1774. spin_unlock(&nn->client_lock);
  1775. out:
  1776. nfs4_unlock_state();
  1777. return status;
  1778. }
  1779. static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
  1780. {
  1781. struct nfsd4_conn *c;
  1782. list_for_each_entry(c, &s->se_conns, cn_persession) {
  1783. if (c->cn_xprt == xpt) {
  1784. return c;
  1785. }
  1786. }
  1787. return NULL;
  1788. }
  1789. static void nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
  1790. {
  1791. struct nfs4_client *clp = ses->se_client;
  1792. struct nfsd4_conn *c;
  1793. int ret;
  1794. spin_lock(&clp->cl_lock);
  1795. c = __nfsd4_find_conn(new->cn_xprt, ses);
  1796. if (c) {
  1797. spin_unlock(&clp->cl_lock);
  1798. free_conn(new);
  1799. return;
  1800. }
  1801. __nfsd4_hash_conn(new, ses);
  1802. spin_unlock(&clp->cl_lock);
  1803. ret = nfsd4_register_conn(new);
  1804. if (ret)
  1805. /* oops; xprt is already down: */
  1806. nfsd4_conn_lost(&new->cn_xpt_user);
  1807. return;
  1808. }
  1809. static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
  1810. {
  1811. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  1812. return args->opcnt > session->se_fchannel.maxops;
  1813. }
  1814. static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
  1815. struct nfsd4_session *session)
  1816. {
  1817. struct xdr_buf *xb = &rqstp->rq_arg;
  1818. return xb->len > session->se_fchannel.maxreq_sz;
  1819. }
  1820. __be32
  1821. nfsd4_sequence(struct svc_rqst *rqstp,
  1822. struct nfsd4_compound_state *cstate,
  1823. struct nfsd4_sequence *seq)
  1824. {
  1825. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  1826. struct nfsd4_session *session;
  1827. struct nfs4_client *clp;
  1828. struct nfsd4_slot *slot;
  1829. struct nfsd4_conn *conn;
  1830. __be32 status;
  1831. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1832. if (resp->opcnt != 1)
  1833. return nfserr_sequence_pos;
  1834. /*
  1835. * Will be either used or freed by nfsd4_sequence_check_conn
  1836. * below.
  1837. */
  1838. conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
  1839. if (!conn)
  1840. return nfserr_jukebox;
  1841. spin_lock(&nn->client_lock);
  1842. status = nfserr_badsession;
  1843. session = find_in_sessionid_hashtbl(&seq->sessionid, SVC_NET(rqstp));
  1844. if (!session)
  1845. goto out_no_session;
  1846. clp = session->se_client;
  1847. status = get_client_locked(clp);
  1848. if (status)
  1849. goto out_no_session;
  1850. status = nfsd4_get_session_locked(session);
  1851. if (status)
  1852. goto out_put_client;
  1853. status = nfserr_too_many_ops;
  1854. if (nfsd4_session_too_many_ops(rqstp, session))
  1855. goto out_put_session;
  1856. status = nfserr_req_too_big;
  1857. if (nfsd4_request_too_big(rqstp, session))
  1858. goto out_put_session;
  1859. status = nfserr_badslot;
  1860. if (seq->slotid >= session->se_fchannel.maxreqs)
  1861. goto out_put_session;
  1862. slot = session->se_slots[seq->slotid];
  1863. dprintk("%s: slotid %d\n", __func__, seq->slotid);
  1864. /* We do not negotiate the number of slots yet, so set the
  1865. * maxslots to the session maxreqs which is used to encode
  1866. * sr_highest_slotid and the sr_target_slot id to maxslots */
  1867. seq->maxslots = session->se_fchannel.maxreqs;
  1868. status = check_slot_seqid(seq->seqid, slot->sl_seqid,
  1869. slot->sl_flags & NFSD4_SLOT_INUSE);
  1870. if (status == nfserr_replay_cache) {
  1871. status = nfserr_seq_misordered;
  1872. if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
  1873. goto out_put_session;
  1874. cstate->slot = slot;
  1875. cstate->session = session;
  1876. /* Return the cached reply status and set cstate->status
  1877. * for nfsd4_proc_compound processing */
  1878. status = nfsd4_replay_cache_entry(resp, seq);
  1879. cstate->status = nfserr_replay_cache;
  1880. goto out;
  1881. }
  1882. if (status)
  1883. goto out_put_session;
  1884. nfsd4_sequence_check_conn(conn, session);
  1885. conn = NULL;
  1886. /* Success! bump slot seqid */
  1887. slot->sl_seqid = seq->seqid;
  1888. slot->sl_flags |= NFSD4_SLOT_INUSE;
  1889. if (seq->cachethis)
  1890. slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
  1891. else
  1892. slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
  1893. cstate->slot = slot;
  1894. cstate->session = session;
  1895. out:
  1896. switch (clp->cl_cb_state) {
  1897. case NFSD4_CB_DOWN:
  1898. seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
  1899. break;
  1900. case NFSD4_CB_FAULT:
  1901. seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
  1902. break;
  1903. default:
  1904. seq->status_flags = 0;
  1905. }
  1906. if (!list_empty(&clp->cl_revoked))
  1907. seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
  1908. out_no_session:
  1909. kfree(conn);
  1910. spin_unlock(&nn->client_lock);
  1911. return status;
  1912. out_put_session:
  1913. nfsd4_put_session(session);
  1914. out_put_client:
  1915. put_client_renew_locked(clp);
  1916. goto out_no_session;
  1917. }
  1918. __be32
  1919. nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
  1920. {
  1921. struct nfs4_client *conf, *unconf, *clp;
  1922. __be32 status = 0;
  1923. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1924. nfs4_lock_state();
  1925. unconf = find_unconfirmed_client(&dc->clientid, true, nn);
  1926. conf = find_confirmed_client(&dc->clientid, true, nn);
  1927. WARN_ON_ONCE(conf && unconf);
  1928. if (conf) {
  1929. clp = conf;
  1930. if (client_has_state(conf)) {
  1931. status = nfserr_clientid_busy;
  1932. goto out;
  1933. }
  1934. } else if (unconf)
  1935. clp = unconf;
  1936. else {
  1937. status = nfserr_stale_clientid;
  1938. goto out;
  1939. }
  1940. expire_client(clp);
  1941. out:
  1942. nfs4_unlock_state();
  1943. return status;
  1944. }
  1945. __be32
  1946. nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
  1947. {
  1948. __be32 status = 0;
  1949. if (rc->rca_one_fs) {
  1950. if (!cstate->current_fh.fh_dentry)
  1951. return nfserr_nofilehandle;
  1952. /*
  1953. * We don't take advantage of the rca_one_fs case.
  1954. * That's OK, it's optional, we can safely ignore it.
  1955. */
  1956. return nfs_ok;
  1957. }
  1958. nfs4_lock_state();
  1959. status = nfserr_complete_already;
  1960. if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  1961. &cstate->session->se_client->cl_flags))
  1962. goto out;
  1963. status = nfserr_stale_clientid;
  1964. if (is_client_expired(cstate->session->se_client))
  1965. /*
  1966. * The following error isn't really legal.
  1967. * But we only get here if the client just explicitly
  1968. * destroyed the client. Surely it no longer cares what
  1969. * error it gets back on an operation for the dead
  1970. * client.
  1971. */
  1972. goto out;
  1973. status = nfs_ok;
  1974. nfsd4_client_record_create(cstate->session->se_client);
  1975. out:
  1976. nfs4_unlock_state();
  1977. return status;
  1978. }
  1979. __be32
  1980. nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  1981. struct nfsd4_setclientid *setclid)
  1982. {
  1983. struct xdr_netobj clname = setclid->se_name;
  1984. nfs4_verifier clverifier = setclid->se_verf;
  1985. struct nfs4_client *conf, *unconf, *new;
  1986. __be32 status;
  1987. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1988. /* Cases below refer to rfc 3530 section 14.2.33: */
  1989. nfs4_lock_state();
  1990. conf = find_confirmed_client_by_name(&clname, nn);
  1991. if (conf) {
  1992. /* case 0: */
  1993. status = nfserr_clid_inuse;
  1994. if (clp_used_exchangeid(conf))
  1995. goto out;
  1996. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
  1997. char addr_str[INET6_ADDRSTRLEN];
  1998. rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
  1999. sizeof(addr_str));
  2000. dprintk("NFSD: setclientid: string in use by client "
  2001. "at %s\n", addr_str);
  2002. goto out;
  2003. }
  2004. }
  2005. unconf = find_unconfirmed_client_by_name(&clname, nn);
  2006. if (unconf)
  2007. expire_client(unconf);
  2008. status = nfserr_jukebox;
  2009. new = create_client(clname, rqstp, &clverifier);
  2010. if (new == NULL)
  2011. goto out;
  2012. if (conf && same_verf(&conf->cl_verifier, &clverifier))
  2013. /* case 1: probable callback update */
  2014. copy_clid(new, conf);
  2015. else /* case 4 (new client) or cases 2, 3 (client reboot): */
  2016. gen_clid(new, nn);
  2017. new->cl_minorversion = 0;
  2018. gen_callback(new, setclid, rqstp);
  2019. add_to_unconfirmed(new);
  2020. setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
  2021. setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
  2022. memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
  2023. status = nfs_ok;
  2024. out:
  2025. nfs4_unlock_state();
  2026. return status;
  2027. }
  2028. __be32
  2029. nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
  2030. struct nfsd4_compound_state *cstate,
  2031. struct nfsd4_setclientid_confirm *setclientid_confirm)
  2032. {
  2033. struct nfs4_client *conf, *unconf;
  2034. nfs4_verifier confirm = setclientid_confirm->sc_confirm;
  2035. clientid_t * clid = &setclientid_confirm->sc_clientid;
  2036. __be32 status;
  2037. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2038. if (STALE_CLIENTID(clid, nn))
  2039. return nfserr_stale_clientid;
  2040. nfs4_lock_state();
  2041. conf = find_confirmed_client(clid, false, nn);
  2042. unconf = find_unconfirmed_client(clid, false, nn);
  2043. /*
  2044. * We try hard to give out unique clientid's, so if we get an
  2045. * attempt to confirm the same clientid with a different cred,
  2046. * there's a bug somewhere. Let's charitably assume it's our
  2047. * bug.
  2048. */
  2049. status = nfserr_serverfault;
  2050. if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
  2051. goto out;
  2052. if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
  2053. goto out;
  2054. /* cases below refer to rfc 3530 section 14.2.34: */
  2055. if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
  2056. if (conf && !unconf) /* case 2: probable retransmit */
  2057. status = nfs_ok;
  2058. else /* case 4: client hasn't noticed we rebooted yet? */
  2059. status = nfserr_stale_clientid;
  2060. goto out;
  2061. }
  2062. status = nfs_ok;
  2063. if (conf) { /* case 1: callback update */
  2064. nfsd4_change_callback(conf, &unconf->cl_cb_conn);
  2065. nfsd4_probe_callback(conf);
  2066. expire_client(unconf);
  2067. } else { /* case 3: normal case; new or rebooted client */
  2068. conf = find_confirmed_client_by_name(&unconf->cl_name, nn);
  2069. if (conf) {
  2070. status = mark_client_expired(conf);
  2071. if (status)
  2072. goto out;
  2073. expire_client(conf);
  2074. }
  2075. move_to_confirmed(unconf);
  2076. nfsd4_probe_callback(unconf);
  2077. }
  2078. out:
  2079. nfs4_unlock_state();
  2080. return status;
  2081. }
  2082. static struct nfs4_file *nfsd4_alloc_file(void)
  2083. {
  2084. return kmem_cache_alloc(file_slab, GFP_KERNEL);
  2085. }
  2086. /* OPEN Share state helper functions */
  2087. static void nfsd4_init_file(struct nfs4_file *fp, struct inode *ino)
  2088. {
  2089. unsigned int hashval = file_hashval(ino);
  2090. atomic_set(&fp->fi_ref, 1);
  2091. INIT_LIST_HEAD(&fp->fi_stateids);
  2092. INIT_LIST_HEAD(&fp->fi_delegations);
  2093. fp->fi_inode = igrab(ino);
  2094. fp->fi_had_conflict = false;
  2095. fp->fi_lease = NULL;
  2096. memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
  2097. memset(fp->fi_access, 0, sizeof(fp->fi_access));
  2098. spin_lock(&recall_lock);
  2099. hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
  2100. spin_unlock(&recall_lock);
  2101. }
  2102. static void
  2103. nfsd4_free_slab(struct kmem_cache **slab)
  2104. {
  2105. if (*slab == NULL)
  2106. return;
  2107. kmem_cache_destroy(*slab);
  2108. *slab = NULL;
  2109. }
  2110. void
  2111. nfsd4_free_slabs(void)
  2112. {
  2113. nfsd4_free_slab(&openowner_slab);
  2114. nfsd4_free_slab(&lockowner_slab);
  2115. nfsd4_free_slab(&file_slab);
  2116. nfsd4_free_slab(&stateid_slab);
  2117. nfsd4_free_slab(&deleg_slab);
  2118. }
  2119. int
  2120. nfsd4_init_slabs(void)
  2121. {
  2122. openowner_slab = kmem_cache_create("nfsd4_openowners",
  2123. sizeof(struct nfs4_openowner), 0, 0, NULL);
  2124. if (openowner_slab == NULL)
  2125. goto out_nomem;
  2126. lockowner_slab = kmem_cache_create("nfsd4_lockowners",
  2127. sizeof(struct nfs4_lockowner), 0, 0, NULL);
  2128. if (lockowner_slab == NULL)
  2129. goto out_nomem;
  2130. file_slab = kmem_cache_create("nfsd4_files",
  2131. sizeof(struct nfs4_file), 0, 0, NULL);
  2132. if (file_slab == NULL)
  2133. goto out_nomem;
  2134. stateid_slab = kmem_cache_create("nfsd4_stateids",
  2135. sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
  2136. if (stateid_slab == NULL)
  2137. goto out_nomem;
  2138. deleg_slab = kmem_cache_create("nfsd4_delegations",
  2139. sizeof(struct nfs4_delegation), 0, 0, NULL);
  2140. if (deleg_slab == NULL)
  2141. goto out_nomem;
  2142. return 0;
  2143. out_nomem:
  2144. nfsd4_free_slabs();
  2145. dprintk("nfsd4: out of memory while initializing nfsv4\n");
  2146. return -ENOMEM;
  2147. }
  2148. void nfs4_free_openowner(struct nfs4_openowner *oo)
  2149. {
  2150. kfree(oo->oo_owner.so_owner.data);
  2151. kmem_cache_free(openowner_slab, oo);
  2152. }
  2153. void nfs4_free_lockowner(struct nfs4_lockowner *lo)
  2154. {
  2155. kfree(lo->lo_owner.so_owner.data);
  2156. kmem_cache_free(lockowner_slab, lo);
  2157. }
  2158. static void init_nfs4_replay(struct nfs4_replay *rp)
  2159. {
  2160. rp->rp_status = nfserr_serverfault;
  2161. rp->rp_buflen = 0;
  2162. rp->rp_buf = rp->rp_ibuf;
  2163. }
  2164. static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
  2165. {
  2166. struct nfs4_stateowner *sop;
  2167. sop = kmem_cache_alloc(slab, GFP_KERNEL);
  2168. if (!sop)
  2169. return NULL;
  2170. sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
  2171. if (!sop->so_owner.data) {
  2172. kmem_cache_free(slab, sop);
  2173. return NULL;
  2174. }
  2175. sop->so_owner.len = owner->len;
  2176. INIT_LIST_HEAD(&sop->so_stateids);
  2177. sop->so_client = clp;
  2178. init_nfs4_replay(&sop->so_replay);
  2179. return sop;
  2180. }
  2181. static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
  2182. {
  2183. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2184. list_add(&oo->oo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
  2185. list_add(&oo->oo_perclient, &clp->cl_openowners);
  2186. }
  2187. static struct nfs4_openowner *
  2188. alloc_init_open_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfsd4_open *open) {
  2189. struct nfs4_openowner *oo;
  2190. oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
  2191. if (!oo)
  2192. return NULL;
  2193. oo->oo_owner.so_is_open_owner = 1;
  2194. oo->oo_owner.so_seqid = open->op_seqid;
  2195. oo->oo_flags = NFS4_OO_NEW;
  2196. oo->oo_time = 0;
  2197. oo->oo_last_closed_stid = NULL;
  2198. INIT_LIST_HEAD(&oo->oo_close_lru);
  2199. hash_openowner(oo, clp, strhashval);
  2200. return oo;
  2201. }
  2202. static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
  2203. struct nfs4_openowner *oo = open->op_openowner;
  2204. stp->st_stid.sc_type = NFS4_OPEN_STID;
  2205. INIT_LIST_HEAD(&stp->st_lockowners);
  2206. list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
  2207. list_add(&stp->st_perfile, &fp->fi_stateids);
  2208. stp->st_stateowner = &oo->oo_owner;
  2209. get_nfs4_file(fp);
  2210. stp->st_file = fp;
  2211. stp->st_access_bmap = 0;
  2212. stp->st_deny_bmap = 0;
  2213. set_access(open->op_share_access, stp);
  2214. set_deny(open->op_share_deny, stp);
  2215. stp->st_openstp = NULL;
  2216. }
  2217. static void
  2218. move_to_close_lru(struct nfs4_openowner *oo, struct net *net)
  2219. {
  2220. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2221. dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
  2222. list_move_tail(&oo->oo_close_lru, &nn->close_lru);
  2223. oo->oo_time = get_seconds();
  2224. }
  2225. static int
  2226. same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner,
  2227. clientid_t *clid)
  2228. {
  2229. return (sop->so_owner.len == owner->len) &&
  2230. 0 == memcmp(sop->so_owner.data, owner->data, owner->len) &&
  2231. (sop->so_client->cl_clientid.cl_id == clid->cl_id);
  2232. }
  2233. static struct nfs4_openowner *
  2234. find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
  2235. bool sessions, struct nfsd_net *nn)
  2236. {
  2237. struct nfs4_stateowner *so;
  2238. struct nfs4_openowner *oo;
  2239. struct nfs4_client *clp;
  2240. list_for_each_entry(so, &nn->ownerstr_hashtbl[hashval], so_strhash) {
  2241. if (!so->so_is_open_owner)
  2242. continue;
  2243. if (same_owner_str(so, &open->op_owner, &open->op_clientid)) {
  2244. oo = openowner(so);
  2245. clp = oo->oo_owner.so_client;
  2246. if ((bool)clp->cl_minorversion != sessions)
  2247. return NULL;
  2248. renew_client(oo->oo_owner.so_client);
  2249. return oo;
  2250. }
  2251. }
  2252. return NULL;
  2253. }
  2254. /* search file_hashtbl[] for file */
  2255. static struct nfs4_file *
  2256. find_file(struct inode *ino)
  2257. {
  2258. unsigned int hashval = file_hashval(ino);
  2259. struct nfs4_file *fp;
  2260. spin_lock(&recall_lock);
  2261. hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
  2262. if (fp->fi_inode == ino) {
  2263. get_nfs4_file(fp);
  2264. spin_unlock(&recall_lock);
  2265. return fp;
  2266. }
  2267. }
  2268. spin_unlock(&recall_lock);
  2269. return NULL;
  2270. }
  2271. /*
  2272. * Called to check deny when READ with all zero stateid or
  2273. * WRITE with all zero or all one stateid
  2274. */
  2275. static __be32
  2276. nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
  2277. {
  2278. struct inode *ino = current_fh->fh_dentry->d_inode;
  2279. struct nfs4_file *fp;
  2280. struct nfs4_ol_stateid *stp;
  2281. __be32 ret;
  2282. fp = find_file(ino);
  2283. if (!fp)
  2284. return nfs_ok;
  2285. ret = nfserr_locked;
  2286. /* Search for conflicting share reservations */
  2287. list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
  2288. if (test_deny(deny_type, stp) ||
  2289. test_deny(NFS4_SHARE_DENY_BOTH, stp))
  2290. goto out;
  2291. }
  2292. ret = nfs_ok;
  2293. out:
  2294. put_nfs4_file(fp);
  2295. return ret;
  2296. }
  2297. static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
  2298. {
  2299. struct nfs4_client *clp = dp->dl_stid.sc_client;
  2300. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2301. /* We're assuming the state code never drops its reference
  2302. * without first removing the lease. Since we're in this lease
  2303. * callback (and since the lease code is serialized by the kernel
  2304. * lock) we know the server hasn't removed the lease yet, we know
  2305. * it's safe to take a reference: */
  2306. atomic_inc(&dp->dl_count);
  2307. list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
  2308. /* only place dl_time is set. protected by lock_flocks*/
  2309. dp->dl_time = get_seconds();
  2310. nfsd4_cb_recall(dp);
  2311. }
  2312. /* Called from break_lease() with lock_flocks() held. */
  2313. static void nfsd_break_deleg_cb(struct file_lock *fl)
  2314. {
  2315. struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
  2316. struct nfs4_delegation *dp;
  2317. if (!fp) {
  2318. WARN(1, "(%p)->fl_owner NULL\n", fl);
  2319. return;
  2320. }
  2321. if (fp->fi_had_conflict) {
  2322. WARN(1, "duplicate break on %p\n", fp);
  2323. return;
  2324. }
  2325. /*
  2326. * We don't want the locks code to timeout the lease for us;
  2327. * we'll remove it ourself if a delegation isn't returned
  2328. * in time:
  2329. */
  2330. fl->fl_break_time = 0;
  2331. spin_lock(&recall_lock);
  2332. fp->fi_had_conflict = true;
  2333. list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
  2334. nfsd_break_one_deleg(dp);
  2335. spin_unlock(&recall_lock);
  2336. }
  2337. static
  2338. int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
  2339. {
  2340. if (arg & F_UNLCK)
  2341. return lease_modify(onlist, arg);
  2342. else
  2343. return -EAGAIN;
  2344. }
  2345. static const struct lock_manager_operations nfsd_lease_mng_ops = {
  2346. .lm_break = nfsd_break_deleg_cb,
  2347. .lm_change = nfsd_change_deleg_cb,
  2348. };
  2349. static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
  2350. {
  2351. if (nfsd4_has_session(cstate))
  2352. return nfs_ok;
  2353. if (seqid == so->so_seqid - 1)
  2354. return nfserr_replay_me;
  2355. if (seqid == so->so_seqid)
  2356. return nfs_ok;
  2357. return nfserr_bad_seqid;
  2358. }
  2359. __be32
  2360. nfsd4_process_open1(struct nfsd4_compound_state *cstate,
  2361. struct nfsd4_open *open, struct nfsd_net *nn)
  2362. {
  2363. clientid_t *clientid = &open->op_clientid;
  2364. struct nfs4_client *clp = NULL;
  2365. unsigned int strhashval;
  2366. struct nfs4_openowner *oo = NULL;
  2367. __be32 status;
  2368. if (STALE_CLIENTID(&open->op_clientid, nn))
  2369. return nfserr_stale_clientid;
  2370. /*
  2371. * In case we need it later, after we've already created the
  2372. * file and don't want to risk a further failure:
  2373. */
  2374. open->op_file = nfsd4_alloc_file();
  2375. if (open->op_file == NULL)
  2376. return nfserr_jukebox;
  2377. strhashval = ownerstr_hashval(clientid->cl_id, &open->op_owner);
  2378. oo = find_openstateowner_str(strhashval, open, cstate->minorversion, nn);
  2379. open->op_openowner = oo;
  2380. if (!oo) {
  2381. clp = find_confirmed_client(clientid, cstate->minorversion,
  2382. nn);
  2383. if (clp == NULL)
  2384. return nfserr_expired;
  2385. goto new_owner;
  2386. }
  2387. if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  2388. /* Replace unconfirmed owners without checking for replay. */
  2389. clp = oo->oo_owner.so_client;
  2390. release_openowner(oo);
  2391. open->op_openowner = NULL;
  2392. goto new_owner;
  2393. }
  2394. status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
  2395. if (status)
  2396. return status;
  2397. clp = oo->oo_owner.so_client;
  2398. goto alloc_stateid;
  2399. new_owner:
  2400. oo = alloc_init_open_stateowner(strhashval, clp, open);
  2401. if (oo == NULL)
  2402. return nfserr_jukebox;
  2403. open->op_openowner = oo;
  2404. alloc_stateid:
  2405. open->op_stp = nfs4_alloc_stateid(clp);
  2406. if (!open->op_stp)
  2407. return nfserr_jukebox;
  2408. return nfs_ok;
  2409. }
  2410. static inline __be32
  2411. nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
  2412. {
  2413. if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
  2414. return nfserr_openmode;
  2415. else
  2416. return nfs_ok;
  2417. }
  2418. static int share_access_to_flags(u32 share_access)
  2419. {
  2420. return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
  2421. }
  2422. static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
  2423. {
  2424. struct nfs4_stid *ret;
  2425. ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
  2426. if (!ret)
  2427. return NULL;
  2428. return delegstateid(ret);
  2429. }
  2430. static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
  2431. {
  2432. return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
  2433. open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
  2434. }
  2435. static __be32
  2436. nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
  2437. struct nfs4_delegation **dp)
  2438. {
  2439. int flags;
  2440. __be32 status = nfserr_bad_stateid;
  2441. *dp = find_deleg_stateid(cl, &open->op_delegate_stateid);
  2442. if (*dp == NULL)
  2443. goto out;
  2444. flags = share_access_to_flags(open->op_share_access);
  2445. status = nfs4_check_delegmode(*dp, flags);
  2446. if (status)
  2447. *dp = NULL;
  2448. out:
  2449. if (!nfsd4_is_deleg_cur(open))
  2450. return nfs_ok;
  2451. if (status)
  2452. return status;
  2453. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2454. return nfs_ok;
  2455. }
  2456. static __be32
  2457. nfs4_check_open(struct nfs4_file *fp, struct nfsd4_open *open, struct nfs4_ol_stateid **stpp)
  2458. {
  2459. struct nfs4_ol_stateid *local;
  2460. struct nfs4_openowner *oo = open->op_openowner;
  2461. list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
  2462. /* ignore lock owners */
  2463. if (local->st_stateowner->so_is_open_owner == 0)
  2464. continue;
  2465. /* remember if we have seen this open owner */
  2466. if (local->st_stateowner == &oo->oo_owner)
  2467. *stpp = local;
  2468. /* check for conflicting share reservations */
  2469. if (!test_share(local, open))
  2470. return nfserr_share_denied;
  2471. }
  2472. return nfs_ok;
  2473. }
  2474. static inline int nfs4_access_to_access(u32 nfs4_access)
  2475. {
  2476. int flags = 0;
  2477. if (nfs4_access & NFS4_SHARE_ACCESS_READ)
  2478. flags |= NFSD_MAY_READ;
  2479. if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
  2480. flags |= NFSD_MAY_WRITE;
  2481. return flags;
  2482. }
  2483. static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
  2484. struct svc_fh *cur_fh, struct nfsd4_open *open)
  2485. {
  2486. __be32 status;
  2487. int oflag = nfs4_access_to_omode(open->op_share_access);
  2488. int access = nfs4_access_to_access(open->op_share_access);
  2489. if (!fp->fi_fds[oflag]) {
  2490. status = nfsd_open(rqstp, cur_fh, S_IFREG, access,
  2491. &fp->fi_fds[oflag]);
  2492. if (status)
  2493. return status;
  2494. }
  2495. nfs4_file_get_access(fp, oflag);
  2496. return nfs_ok;
  2497. }
  2498. static inline __be32
  2499. nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
  2500. struct nfsd4_open *open)
  2501. {
  2502. struct iattr iattr = {
  2503. .ia_valid = ATTR_SIZE,
  2504. .ia_size = 0,
  2505. };
  2506. if (!open->op_truncate)
  2507. return 0;
  2508. if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  2509. return nfserr_inval;
  2510. return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
  2511. }
  2512. static __be32
  2513. nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open)
  2514. {
  2515. u32 op_share_access = open->op_share_access;
  2516. bool new_access;
  2517. __be32 status;
  2518. new_access = !test_access(op_share_access, stp);
  2519. if (new_access) {
  2520. status = nfs4_get_vfs_file(rqstp, fp, cur_fh, open);
  2521. if (status)
  2522. return status;
  2523. }
  2524. status = nfsd4_truncate(rqstp, cur_fh, open);
  2525. if (status) {
  2526. if (new_access) {
  2527. int oflag = nfs4_access_to_omode(op_share_access);
  2528. nfs4_file_put_access(fp, oflag);
  2529. }
  2530. return status;
  2531. }
  2532. /* remember the open */
  2533. set_access(op_share_access, stp);
  2534. set_deny(open->op_share_deny, stp);
  2535. return nfs_ok;
  2536. }
  2537. static void
  2538. nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
  2539. {
  2540. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2541. }
  2542. /* Should we give out recallable state?: */
  2543. static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
  2544. {
  2545. if (clp->cl_cb_state == NFSD4_CB_UP)
  2546. return true;
  2547. /*
  2548. * In the sessions case, since we don't have to establish a
  2549. * separate connection for callbacks, we assume it's OK
  2550. * until we hear otherwise:
  2551. */
  2552. return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
  2553. }
  2554. static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, int flag)
  2555. {
  2556. struct file_lock *fl;
  2557. fl = locks_alloc_lock();
  2558. if (!fl)
  2559. return NULL;
  2560. locks_init_lock(fl);
  2561. fl->fl_lmops = &nfsd_lease_mng_ops;
  2562. fl->fl_flags = FL_LEASE;
  2563. fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
  2564. fl->fl_end = OFFSET_MAX;
  2565. fl->fl_owner = (fl_owner_t)(dp->dl_file);
  2566. fl->fl_pid = current->tgid;
  2567. return fl;
  2568. }
  2569. static int nfs4_setlease(struct nfs4_delegation *dp, int flag)
  2570. {
  2571. struct nfs4_file *fp = dp->dl_file;
  2572. struct file_lock *fl;
  2573. int status;
  2574. fl = nfs4_alloc_init_lease(dp, flag);
  2575. if (!fl)
  2576. return -ENOMEM;
  2577. fl->fl_file = find_readable_file(fp);
  2578. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  2579. status = vfs_setlease(fl->fl_file, fl->fl_type, &fl);
  2580. if (status) {
  2581. list_del_init(&dp->dl_perclnt);
  2582. locks_free_lock(fl);
  2583. return -ENOMEM;
  2584. }
  2585. fp->fi_lease = fl;
  2586. fp->fi_deleg_file = get_file(fl->fl_file);
  2587. atomic_set(&fp->fi_delegees, 1);
  2588. list_add(&dp->dl_perfile, &fp->fi_delegations);
  2589. return 0;
  2590. }
  2591. static int nfs4_set_delegation(struct nfs4_delegation *dp, int flag)
  2592. {
  2593. struct nfs4_file *fp = dp->dl_file;
  2594. if (!fp->fi_lease)
  2595. return nfs4_setlease(dp, flag);
  2596. spin_lock(&recall_lock);
  2597. if (fp->fi_had_conflict) {
  2598. spin_unlock(&recall_lock);
  2599. return -EAGAIN;
  2600. }
  2601. atomic_inc(&fp->fi_delegees);
  2602. list_add(&dp->dl_perfile, &fp->fi_delegations);
  2603. spin_unlock(&recall_lock);
  2604. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  2605. return 0;
  2606. }
  2607. static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
  2608. {
  2609. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2610. if (status == -EAGAIN)
  2611. open->op_why_no_deleg = WND4_CONTENTION;
  2612. else {
  2613. open->op_why_no_deleg = WND4_RESOURCE;
  2614. switch (open->op_deleg_want) {
  2615. case NFS4_SHARE_WANT_READ_DELEG:
  2616. case NFS4_SHARE_WANT_WRITE_DELEG:
  2617. case NFS4_SHARE_WANT_ANY_DELEG:
  2618. break;
  2619. case NFS4_SHARE_WANT_CANCEL:
  2620. open->op_why_no_deleg = WND4_CANCELLED;
  2621. break;
  2622. case NFS4_SHARE_WANT_NO_DELEG:
  2623. WARN_ON_ONCE(1);
  2624. }
  2625. }
  2626. }
  2627. /*
  2628. * Attempt to hand out a delegation.
  2629. */
  2630. static void
  2631. nfs4_open_delegation(struct net *net, struct svc_fh *fh,
  2632. struct nfsd4_open *open, struct nfs4_ol_stateid *stp)
  2633. {
  2634. struct nfs4_delegation *dp;
  2635. struct nfs4_openowner *oo = container_of(stp->st_stateowner, struct nfs4_openowner, oo_owner);
  2636. int cb_up;
  2637. int status = 0, flag = 0;
  2638. cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
  2639. flag = NFS4_OPEN_DELEGATE_NONE;
  2640. open->op_recall = 0;
  2641. switch (open->op_claim_type) {
  2642. case NFS4_OPEN_CLAIM_PREVIOUS:
  2643. if (!cb_up)
  2644. open->op_recall = 1;
  2645. flag = open->op_delegate_type;
  2646. if (flag == NFS4_OPEN_DELEGATE_NONE)
  2647. goto out;
  2648. break;
  2649. case NFS4_OPEN_CLAIM_NULL:
  2650. /* Let's not give out any delegations till everyone's
  2651. * had the chance to reclaim theirs.... */
  2652. if (locks_in_grace(net))
  2653. goto out;
  2654. if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
  2655. goto out;
  2656. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  2657. flag = NFS4_OPEN_DELEGATE_WRITE;
  2658. else
  2659. flag = NFS4_OPEN_DELEGATE_READ;
  2660. break;
  2661. default:
  2662. goto out;
  2663. }
  2664. dp = alloc_init_deleg(oo->oo_owner.so_client, stp, fh, flag);
  2665. if (dp == NULL)
  2666. goto out_no_deleg;
  2667. status = nfs4_set_delegation(dp, flag);
  2668. if (status)
  2669. goto out_free;
  2670. memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
  2671. dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
  2672. STATEID_VAL(&dp->dl_stid.sc_stateid));
  2673. out:
  2674. open->op_delegate_type = flag;
  2675. if (flag == NFS4_OPEN_DELEGATE_NONE) {
  2676. if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
  2677. open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE)
  2678. dprintk("NFSD: WARNING: refusing delegation reclaim\n");
  2679. /* 4.1 client asking for a delegation? */
  2680. if (open->op_deleg_want)
  2681. nfsd4_open_deleg_none_ext(open, status);
  2682. }
  2683. return;
  2684. out_free:
  2685. unhash_stid(&dp->dl_stid);
  2686. nfs4_put_delegation(dp);
  2687. out_no_deleg:
  2688. flag = NFS4_OPEN_DELEGATE_NONE;
  2689. goto out;
  2690. }
  2691. static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
  2692. struct nfs4_delegation *dp)
  2693. {
  2694. if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
  2695. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  2696. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2697. open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
  2698. } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
  2699. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  2700. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2701. open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
  2702. }
  2703. /* Otherwise the client must be confused wanting a delegation
  2704. * it already has, therefore we don't return
  2705. * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
  2706. */
  2707. }
  2708. /*
  2709. * called with nfs4_lock_state() held.
  2710. */
  2711. __be32
  2712. nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  2713. {
  2714. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2715. struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
  2716. struct nfs4_file *fp = NULL;
  2717. struct inode *ino = current_fh->fh_dentry->d_inode;
  2718. struct nfs4_ol_stateid *stp = NULL;
  2719. struct nfs4_delegation *dp = NULL;
  2720. __be32 status;
  2721. /*
  2722. * Lookup file; if found, lookup stateid and check open request,
  2723. * and check for delegations in the process of being recalled.
  2724. * If not found, create the nfs4_file struct
  2725. */
  2726. fp = find_file(ino);
  2727. if (fp) {
  2728. if ((status = nfs4_check_open(fp, open, &stp)))
  2729. goto out;
  2730. status = nfs4_check_deleg(cl, open, &dp);
  2731. if (status)
  2732. goto out;
  2733. } else {
  2734. status = nfserr_bad_stateid;
  2735. if (nfsd4_is_deleg_cur(open))
  2736. goto out;
  2737. status = nfserr_jukebox;
  2738. fp = open->op_file;
  2739. open->op_file = NULL;
  2740. nfsd4_init_file(fp, ino);
  2741. }
  2742. /*
  2743. * OPEN the file, or upgrade an existing OPEN.
  2744. * If truncate fails, the OPEN fails.
  2745. */
  2746. if (stp) {
  2747. /* Stateid was found, this is an OPEN upgrade */
  2748. status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
  2749. if (status)
  2750. goto out;
  2751. } else {
  2752. status = nfs4_get_vfs_file(rqstp, fp, current_fh, open);
  2753. if (status)
  2754. goto out;
  2755. status = nfsd4_truncate(rqstp, current_fh, open);
  2756. if (status)
  2757. goto out;
  2758. stp = open->op_stp;
  2759. open->op_stp = NULL;
  2760. init_open_stateid(stp, fp, open);
  2761. }
  2762. update_stateid(&stp->st_stid.sc_stateid);
  2763. memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  2764. if (nfsd4_has_session(&resp->cstate)) {
  2765. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  2766. if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
  2767. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  2768. open->op_why_no_deleg = WND4_NOT_WANTED;
  2769. goto nodeleg;
  2770. }
  2771. }
  2772. /*
  2773. * Attempt to hand out a delegation. No error return, because the
  2774. * OPEN succeeds even if we fail.
  2775. */
  2776. nfs4_open_delegation(SVC_NET(rqstp), current_fh, open, stp);
  2777. nodeleg:
  2778. status = nfs_ok;
  2779. dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
  2780. STATEID_VAL(&stp->st_stid.sc_stateid));
  2781. out:
  2782. /* 4.1 client trying to upgrade/downgrade delegation? */
  2783. if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
  2784. open->op_deleg_want)
  2785. nfsd4_deleg_xgrade_none_ext(open, dp);
  2786. if (fp)
  2787. put_nfs4_file(fp);
  2788. if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  2789. nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
  2790. /*
  2791. * To finish the open response, we just need to set the rflags.
  2792. */
  2793. open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
  2794. if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
  2795. !nfsd4_has_session(&resp->cstate))
  2796. open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
  2797. return status;
  2798. }
  2799. void nfsd4_cleanup_open_state(struct nfsd4_open *open, __be32 status)
  2800. {
  2801. if (open->op_openowner) {
  2802. struct nfs4_openowner *oo = open->op_openowner;
  2803. if (!list_empty(&oo->oo_owner.so_stateids))
  2804. list_del_init(&oo->oo_close_lru);
  2805. if (oo->oo_flags & NFS4_OO_NEW) {
  2806. if (status) {
  2807. release_openowner(oo);
  2808. open->op_openowner = NULL;
  2809. } else
  2810. oo->oo_flags &= ~NFS4_OO_NEW;
  2811. }
  2812. }
  2813. if (open->op_file)
  2814. nfsd4_free_file(open->op_file);
  2815. if (open->op_stp)
  2816. free_generic_stateid(open->op_stp);
  2817. }
  2818. static __be32 lookup_clientid(clientid_t *clid, bool session, struct nfsd_net *nn, struct nfs4_client **clp)
  2819. {
  2820. struct nfs4_client *found;
  2821. if (STALE_CLIENTID(clid, nn))
  2822. return nfserr_stale_clientid;
  2823. found = find_confirmed_client(clid, session, nn);
  2824. if (clp)
  2825. *clp = found;
  2826. return found ? nfs_ok : nfserr_expired;
  2827. }
  2828. __be32
  2829. nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2830. clientid_t *clid)
  2831. {
  2832. struct nfs4_client *clp;
  2833. __be32 status;
  2834. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2835. nfs4_lock_state();
  2836. dprintk("process_renew(%08x/%08x): starting\n",
  2837. clid->cl_boot, clid->cl_id);
  2838. status = lookup_clientid(clid, cstate->minorversion, nn, &clp);
  2839. if (status)
  2840. goto out;
  2841. status = nfserr_cb_path_down;
  2842. if (!list_empty(&clp->cl_delegations)
  2843. && clp->cl_cb_state != NFSD4_CB_UP)
  2844. goto out;
  2845. status = nfs_ok;
  2846. out:
  2847. nfs4_unlock_state();
  2848. return status;
  2849. }
  2850. static void
  2851. nfsd4_end_grace(struct nfsd_net *nn)
  2852. {
  2853. /* do nothing if grace period already ended */
  2854. if (nn->grace_ended)
  2855. return;
  2856. dprintk("NFSD: end of grace period\n");
  2857. nn->grace_ended = true;
  2858. nfsd4_record_grace_done(nn, nn->boot_time);
  2859. locks_end_grace(&nn->nfsd4_manager);
  2860. /*
  2861. * Now that every NFSv4 client has had the chance to recover and
  2862. * to see the (possibly new, possibly shorter) lease time, we
  2863. * can safely set the next grace time to the current lease time:
  2864. */
  2865. nn->nfsd4_grace = nn->nfsd4_lease;
  2866. }
  2867. static time_t
  2868. nfs4_laundromat(struct nfsd_net *nn)
  2869. {
  2870. struct nfs4_client *clp;
  2871. struct nfs4_openowner *oo;
  2872. struct nfs4_delegation *dp;
  2873. struct list_head *pos, *next, reaplist;
  2874. time_t cutoff = get_seconds() - nn->nfsd4_lease;
  2875. time_t t, clientid_val = nn->nfsd4_lease;
  2876. time_t u, test_val = nn->nfsd4_lease;
  2877. nfs4_lock_state();
  2878. dprintk("NFSD: laundromat service - starting\n");
  2879. nfsd4_end_grace(nn);
  2880. INIT_LIST_HEAD(&reaplist);
  2881. spin_lock(&nn->client_lock);
  2882. list_for_each_safe(pos, next, &nn->client_lru) {
  2883. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2884. if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
  2885. t = clp->cl_time - cutoff;
  2886. if (clientid_val > t)
  2887. clientid_val = t;
  2888. break;
  2889. }
  2890. if (mark_client_expired_locked(clp)) {
  2891. dprintk("NFSD: client in use (clientid %08x)\n",
  2892. clp->cl_clientid.cl_id);
  2893. continue;
  2894. }
  2895. list_move(&clp->cl_lru, &reaplist);
  2896. }
  2897. spin_unlock(&nn->client_lock);
  2898. list_for_each_safe(pos, next, &reaplist) {
  2899. clp = list_entry(pos, struct nfs4_client, cl_lru);
  2900. dprintk("NFSD: purging unused client (clientid %08x)\n",
  2901. clp->cl_clientid.cl_id);
  2902. expire_client(clp);
  2903. }
  2904. spin_lock(&recall_lock);
  2905. list_for_each_safe(pos, next, &nn->del_recall_lru) {
  2906. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2907. if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
  2908. continue;
  2909. if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
  2910. u = dp->dl_time - cutoff;
  2911. if (test_val > u)
  2912. test_val = u;
  2913. break;
  2914. }
  2915. list_move(&dp->dl_recall_lru, &reaplist);
  2916. }
  2917. spin_unlock(&recall_lock);
  2918. list_for_each_safe(pos, next, &reaplist) {
  2919. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  2920. revoke_delegation(dp);
  2921. }
  2922. test_val = nn->nfsd4_lease;
  2923. list_for_each_safe(pos, next, &nn->close_lru) {
  2924. oo = container_of(pos, struct nfs4_openowner, oo_close_lru);
  2925. if (time_after((unsigned long)oo->oo_time, (unsigned long)cutoff)) {
  2926. u = oo->oo_time - cutoff;
  2927. if (test_val > u)
  2928. test_val = u;
  2929. break;
  2930. }
  2931. release_openowner(oo);
  2932. }
  2933. if (clientid_val < NFSD_LAUNDROMAT_MINTIMEOUT)
  2934. clientid_val = NFSD_LAUNDROMAT_MINTIMEOUT;
  2935. nfs4_unlock_state();
  2936. return clientid_val;
  2937. }
  2938. static struct workqueue_struct *laundry_wq;
  2939. static void laundromat_main(struct work_struct *);
  2940. static void
  2941. laundromat_main(struct work_struct *laundry)
  2942. {
  2943. time_t t;
  2944. struct delayed_work *dwork = container_of(laundry, struct delayed_work,
  2945. work);
  2946. struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
  2947. laundromat_work);
  2948. t = nfs4_laundromat(nn);
  2949. dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
  2950. queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
  2951. }
  2952. static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
  2953. {
  2954. if (fhp->fh_dentry->d_inode != stp->st_file->fi_inode)
  2955. return nfserr_bad_stateid;
  2956. return nfs_ok;
  2957. }
  2958. static inline int
  2959. access_permit_read(struct nfs4_ol_stateid *stp)
  2960. {
  2961. return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
  2962. test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
  2963. test_access(NFS4_SHARE_ACCESS_WRITE, stp);
  2964. }
  2965. static inline int
  2966. access_permit_write(struct nfs4_ol_stateid *stp)
  2967. {
  2968. return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
  2969. test_access(NFS4_SHARE_ACCESS_BOTH, stp);
  2970. }
  2971. static
  2972. __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
  2973. {
  2974. __be32 status = nfserr_openmode;
  2975. /* For lock stateid's, we test the parent open, not the lock: */
  2976. if (stp->st_openstp)
  2977. stp = stp->st_openstp;
  2978. if ((flags & WR_STATE) && !access_permit_write(stp))
  2979. goto out;
  2980. if ((flags & RD_STATE) && !access_permit_read(stp))
  2981. goto out;
  2982. status = nfs_ok;
  2983. out:
  2984. return status;
  2985. }
  2986. static inline __be32
  2987. check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
  2988. {
  2989. if (ONE_STATEID(stateid) && (flags & RD_STATE))
  2990. return nfs_ok;
  2991. else if (locks_in_grace(net)) {
  2992. /* Answer in remaining cases depends on existence of
  2993. * conflicting state; so we must wait out the grace period. */
  2994. return nfserr_grace;
  2995. } else if (flags & WR_STATE)
  2996. return nfs4_share_conflict(current_fh,
  2997. NFS4_SHARE_DENY_WRITE);
  2998. else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
  2999. return nfs4_share_conflict(current_fh,
  3000. NFS4_SHARE_DENY_READ);
  3001. }
  3002. /*
  3003. * Allow READ/WRITE during grace period on recovered state only for files
  3004. * that are not able to provide mandatory locking.
  3005. */
  3006. static inline int
  3007. grace_disallows_io(struct net *net, struct inode *inode)
  3008. {
  3009. return locks_in_grace(net) && mandatory_lock(inode);
  3010. }
  3011. /* Returns true iff a is later than b: */
  3012. static bool stateid_generation_after(stateid_t *a, stateid_t *b)
  3013. {
  3014. return (s32)a->si_generation - (s32)b->si_generation > 0;
  3015. }
  3016. static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
  3017. {
  3018. /*
  3019. * When sessions are used the stateid generation number is ignored
  3020. * when it is zero.
  3021. */
  3022. if (has_session && in->si_generation == 0)
  3023. return nfs_ok;
  3024. if (in->si_generation == ref->si_generation)
  3025. return nfs_ok;
  3026. /* If the client sends us a stateid from the future, it's buggy: */
  3027. if (stateid_generation_after(in, ref))
  3028. return nfserr_bad_stateid;
  3029. /*
  3030. * However, we could see a stateid from the past, even from a
  3031. * non-buggy client. For example, if the client sends a lock
  3032. * while some IO is outstanding, the lock may bump si_generation
  3033. * while the IO is still in flight. The client could avoid that
  3034. * situation by waiting for responses on all the IO requests,
  3035. * but better performance may result in retrying IO that
  3036. * receives an old_stateid error if requests are rarely
  3037. * reordered in flight:
  3038. */
  3039. return nfserr_old_stateid;
  3040. }
  3041. static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
  3042. {
  3043. struct nfs4_stid *s;
  3044. struct nfs4_ol_stateid *ols;
  3045. __be32 status;
  3046. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3047. return nfserr_bad_stateid;
  3048. /* Client debugging aid. */
  3049. if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
  3050. char addr_str[INET6_ADDRSTRLEN];
  3051. rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
  3052. sizeof(addr_str));
  3053. pr_warn_ratelimited("NFSD: client %s testing state ID "
  3054. "with incorrect client ID\n", addr_str);
  3055. return nfserr_bad_stateid;
  3056. }
  3057. s = find_stateid(cl, stateid);
  3058. if (!s)
  3059. return nfserr_bad_stateid;
  3060. status = check_stateid_generation(stateid, &s->sc_stateid, 1);
  3061. if (status)
  3062. return status;
  3063. switch (s->sc_type) {
  3064. case NFS4_DELEG_STID:
  3065. return nfs_ok;
  3066. case NFS4_REVOKED_DELEG_STID:
  3067. return nfserr_deleg_revoked;
  3068. case NFS4_OPEN_STID:
  3069. case NFS4_LOCK_STID:
  3070. ols = openlockstateid(s);
  3071. if (ols->st_stateowner->so_is_open_owner
  3072. && !(openowner(ols->st_stateowner)->oo_flags
  3073. & NFS4_OO_CONFIRMED))
  3074. return nfserr_bad_stateid;
  3075. return nfs_ok;
  3076. default:
  3077. printk("unknown stateid type %x\n", s->sc_type);
  3078. case NFS4_CLOSED_STID:
  3079. return nfserr_bad_stateid;
  3080. }
  3081. }
  3082. static __be32 nfsd4_lookup_stateid(stateid_t *stateid, unsigned char typemask,
  3083. struct nfs4_stid **s, bool sessions,
  3084. struct nfsd_net *nn)
  3085. {
  3086. struct nfs4_client *cl;
  3087. __be32 status;
  3088. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3089. return nfserr_bad_stateid;
  3090. status = lookup_clientid(&stateid->si_opaque.so_clid, sessions,
  3091. nn, &cl);
  3092. if (status == nfserr_stale_clientid)
  3093. return nfserr_stale_stateid;
  3094. if (status)
  3095. return status;
  3096. *s = find_stateid_by_type(cl, stateid, typemask);
  3097. if (!*s)
  3098. return nfserr_bad_stateid;
  3099. return nfs_ok;
  3100. }
  3101. /*
  3102. * Checks for stateid operations
  3103. */
  3104. __be32
  3105. nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
  3106. stateid_t *stateid, int flags, struct file **filpp)
  3107. {
  3108. struct nfs4_stid *s;
  3109. struct nfs4_ol_stateid *stp = NULL;
  3110. struct nfs4_delegation *dp = NULL;
  3111. struct svc_fh *current_fh = &cstate->current_fh;
  3112. struct inode *ino = current_fh->fh_dentry->d_inode;
  3113. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3114. __be32 status;
  3115. if (filpp)
  3116. *filpp = NULL;
  3117. if (grace_disallows_io(net, ino))
  3118. return nfserr_grace;
  3119. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3120. return check_special_stateids(net, current_fh, stateid, flags);
  3121. status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
  3122. &s, cstate->minorversion, nn);
  3123. if (status)
  3124. return status;
  3125. status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
  3126. if (status)
  3127. goto out;
  3128. switch (s->sc_type) {
  3129. case NFS4_DELEG_STID:
  3130. dp = delegstateid(s);
  3131. status = nfs4_check_delegmode(dp, flags);
  3132. if (status)
  3133. goto out;
  3134. if (filpp) {
  3135. *filpp = dp->dl_file->fi_deleg_file;
  3136. if (!*filpp) {
  3137. WARN_ON_ONCE(1);
  3138. status = nfserr_serverfault;
  3139. goto out;
  3140. }
  3141. }
  3142. break;
  3143. case NFS4_OPEN_STID:
  3144. case NFS4_LOCK_STID:
  3145. stp = openlockstateid(s);
  3146. status = nfs4_check_fh(current_fh, stp);
  3147. if (status)
  3148. goto out;
  3149. if (stp->st_stateowner->so_is_open_owner
  3150. && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  3151. goto out;
  3152. status = nfs4_check_openmode(stp, flags);
  3153. if (status)
  3154. goto out;
  3155. if (filpp) {
  3156. if (flags & RD_STATE)
  3157. *filpp = find_readable_file(stp->st_file);
  3158. else
  3159. *filpp = find_writeable_file(stp->st_file);
  3160. }
  3161. break;
  3162. default:
  3163. return nfserr_bad_stateid;
  3164. }
  3165. status = nfs_ok;
  3166. out:
  3167. return status;
  3168. }
  3169. static __be32
  3170. nfsd4_free_lock_stateid(struct nfs4_ol_stateid *stp)
  3171. {
  3172. if (check_for_locks(stp->st_file, lockowner(stp->st_stateowner)))
  3173. return nfserr_locks_held;
  3174. release_lock_stateid(stp);
  3175. return nfs_ok;
  3176. }
  3177. /*
  3178. * Test if the stateid is valid
  3179. */
  3180. __be32
  3181. nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3182. struct nfsd4_test_stateid *test_stateid)
  3183. {
  3184. struct nfsd4_test_stateid_id *stateid;
  3185. struct nfs4_client *cl = cstate->session->se_client;
  3186. nfs4_lock_state();
  3187. list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
  3188. stateid->ts_id_status =
  3189. nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
  3190. nfs4_unlock_state();
  3191. return nfs_ok;
  3192. }
  3193. __be32
  3194. nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3195. struct nfsd4_free_stateid *free_stateid)
  3196. {
  3197. stateid_t *stateid = &free_stateid->fr_stateid;
  3198. struct nfs4_stid *s;
  3199. struct nfs4_delegation *dp;
  3200. struct nfs4_client *cl = cstate->session->se_client;
  3201. __be32 ret = nfserr_bad_stateid;
  3202. nfs4_lock_state();
  3203. s = find_stateid(cl, stateid);
  3204. if (!s)
  3205. goto out;
  3206. switch (s->sc_type) {
  3207. case NFS4_DELEG_STID:
  3208. ret = nfserr_locks_held;
  3209. goto out;
  3210. case NFS4_OPEN_STID:
  3211. case NFS4_LOCK_STID:
  3212. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  3213. if (ret)
  3214. goto out;
  3215. if (s->sc_type == NFS4_LOCK_STID)
  3216. ret = nfsd4_free_lock_stateid(openlockstateid(s));
  3217. else
  3218. ret = nfserr_locks_held;
  3219. break;
  3220. case NFS4_REVOKED_DELEG_STID:
  3221. dp = delegstateid(s);
  3222. destroy_revoked_delegation(dp);
  3223. ret = nfs_ok;
  3224. break;
  3225. default:
  3226. ret = nfserr_bad_stateid;
  3227. }
  3228. out:
  3229. nfs4_unlock_state();
  3230. return ret;
  3231. }
  3232. static inline int
  3233. setlkflg (int type)
  3234. {
  3235. return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
  3236. RD_STATE : WR_STATE;
  3237. }
  3238. static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
  3239. {
  3240. struct svc_fh *current_fh = &cstate->current_fh;
  3241. struct nfs4_stateowner *sop = stp->st_stateowner;
  3242. __be32 status;
  3243. status = nfsd4_check_seqid(cstate, sop, seqid);
  3244. if (status)
  3245. return status;
  3246. if (stp->st_stid.sc_type == NFS4_CLOSED_STID
  3247. || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
  3248. /*
  3249. * "Closed" stateid's exist *only* to return
  3250. * nfserr_replay_me from the previous step, and
  3251. * revoked delegations are kept only for free_stateid.
  3252. */
  3253. return nfserr_bad_stateid;
  3254. status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
  3255. if (status)
  3256. return status;
  3257. return nfs4_check_fh(current_fh, stp);
  3258. }
  3259. /*
  3260. * Checks for sequence id mutating operations.
  3261. */
  3262. static __be32
  3263. nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  3264. stateid_t *stateid, char typemask,
  3265. struct nfs4_ol_stateid **stpp,
  3266. struct nfsd_net *nn)
  3267. {
  3268. __be32 status;
  3269. struct nfs4_stid *s;
  3270. dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
  3271. seqid, STATEID_VAL(stateid));
  3272. *stpp = NULL;
  3273. status = nfsd4_lookup_stateid(stateid, typemask, &s,
  3274. cstate->minorversion, nn);
  3275. if (status)
  3276. return status;
  3277. *stpp = openlockstateid(s);
  3278. if (!nfsd4_has_session(cstate))
  3279. cstate->replay_owner = (*stpp)->st_stateowner;
  3280. return nfs4_seqid_op_checks(cstate, stateid, seqid, *stpp);
  3281. }
  3282. static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  3283. stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
  3284. {
  3285. __be32 status;
  3286. struct nfs4_openowner *oo;
  3287. status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
  3288. NFS4_OPEN_STID, stpp, nn);
  3289. if (status)
  3290. return status;
  3291. oo = openowner((*stpp)->st_stateowner);
  3292. if (!(oo->oo_flags & NFS4_OO_CONFIRMED))
  3293. return nfserr_bad_stateid;
  3294. return nfs_ok;
  3295. }
  3296. __be32
  3297. nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3298. struct nfsd4_open_confirm *oc)
  3299. {
  3300. __be32 status;
  3301. struct nfs4_openowner *oo;
  3302. struct nfs4_ol_stateid *stp;
  3303. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3304. dprintk("NFSD: nfsd4_open_confirm on file %.*s\n",
  3305. (int)cstate->current_fh.fh_dentry->d_name.len,
  3306. cstate->current_fh.fh_dentry->d_name.name);
  3307. status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
  3308. if (status)
  3309. return status;
  3310. nfs4_lock_state();
  3311. status = nfs4_preprocess_seqid_op(cstate,
  3312. oc->oc_seqid, &oc->oc_req_stateid,
  3313. NFS4_OPEN_STID, &stp, nn);
  3314. if (status)
  3315. goto out;
  3316. oo = openowner(stp->st_stateowner);
  3317. status = nfserr_bad_stateid;
  3318. if (oo->oo_flags & NFS4_OO_CONFIRMED)
  3319. goto out;
  3320. oo->oo_flags |= NFS4_OO_CONFIRMED;
  3321. update_stateid(&stp->st_stid.sc_stateid);
  3322. memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3323. dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
  3324. __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
  3325. nfsd4_client_record_create(oo->oo_owner.so_client);
  3326. status = nfs_ok;
  3327. out:
  3328. nfsd4_bump_seqid(cstate, status);
  3329. if (!cstate->replay_owner)
  3330. nfs4_unlock_state();
  3331. return status;
  3332. }
  3333. static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
  3334. {
  3335. if (!test_access(access, stp))
  3336. return;
  3337. nfs4_file_put_access(stp->st_file, nfs4_access_to_omode(access));
  3338. clear_access(access, stp);
  3339. }
  3340. static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
  3341. {
  3342. switch (to_access) {
  3343. case NFS4_SHARE_ACCESS_READ:
  3344. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
  3345. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  3346. break;
  3347. case NFS4_SHARE_ACCESS_WRITE:
  3348. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
  3349. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  3350. break;
  3351. case NFS4_SHARE_ACCESS_BOTH:
  3352. break;
  3353. default:
  3354. WARN_ON_ONCE(1);
  3355. }
  3356. }
  3357. static void
  3358. reset_union_bmap_deny(unsigned long deny, struct nfs4_ol_stateid *stp)
  3359. {
  3360. int i;
  3361. for (i = 0; i < 4; i++) {
  3362. if ((i & deny) != i)
  3363. clear_deny(i, stp);
  3364. }
  3365. }
  3366. __be32
  3367. nfsd4_open_downgrade(struct svc_rqst *rqstp,
  3368. struct nfsd4_compound_state *cstate,
  3369. struct nfsd4_open_downgrade *od)
  3370. {
  3371. __be32 status;
  3372. struct nfs4_ol_stateid *stp;
  3373. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3374. dprintk("NFSD: nfsd4_open_downgrade on file %.*s\n",
  3375. (int)cstate->current_fh.fh_dentry->d_name.len,
  3376. cstate->current_fh.fh_dentry->d_name.name);
  3377. /* We don't yet support WANT bits: */
  3378. if (od->od_deleg_want)
  3379. dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
  3380. od->od_deleg_want);
  3381. nfs4_lock_state();
  3382. status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
  3383. &od->od_stateid, &stp, nn);
  3384. if (status)
  3385. goto out;
  3386. status = nfserr_inval;
  3387. if (!test_access(od->od_share_access, stp)) {
  3388. dprintk("NFSD: access not a subset current bitmap: 0x%lx, input access=%08x\n",
  3389. stp->st_access_bmap, od->od_share_access);
  3390. goto out;
  3391. }
  3392. if (!test_deny(od->od_share_deny, stp)) {
  3393. dprintk("NFSD:deny not a subset current bitmap: 0x%lx, input deny=%08x\n",
  3394. stp->st_deny_bmap, od->od_share_deny);
  3395. goto out;
  3396. }
  3397. nfs4_stateid_downgrade(stp, od->od_share_access);
  3398. reset_union_bmap_deny(od->od_share_deny, stp);
  3399. update_stateid(&stp->st_stid.sc_stateid);
  3400. memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3401. status = nfs_ok;
  3402. out:
  3403. nfsd4_bump_seqid(cstate, status);
  3404. if (!cstate->replay_owner)
  3405. nfs4_unlock_state();
  3406. return status;
  3407. }
  3408. static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
  3409. {
  3410. unhash_open_stateid(s);
  3411. s->st_stid.sc_type = NFS4_CLOSED_STID;
  3412. }
  3413. /*
  3414. * nfs4_unlock_state() called after encode
  3415. */
  3416. __be32
  3417. nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3418. struct nfsd4_close *close)
  3419. {
  3420. __be32 status;
  3421. struct nfs4_openowner *oo;
  3422. struct nfs4_ol_stateid *stp;
  3423. struct net *net = SVC_NET(rqstp);
  3424. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3425. dprintk("NFSD: nfsd4_close on file %.*s\n",
  3426. (int)cstate->current_fh.fh_dentry->d_name.len,
  3427. cstate->current_fh.fh_dentry->d_name.name);
  3428. nfs4_lock_state();
  3429. status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
  3430. &close->cl_stateid,
  3431. NFS4_OPEN_STID|NFS4_CLOSED_STID,
  3432. &stp, nn);
  3433. nfsd4_bump_seqid(cstate, status);
  3434. if (status)
  3435. goto out;
  3436. oo = openowner(stp->st_stateowner);
  3437. update_stateid(&stp->st_stid.sc_stateid);
  3438. memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3439. nfsd4_close_open_stateid(stp);
  3440. if (cstate->minorversion) {
  3441. unhash_stid(&stp->st_stid);
  3442. free_generic_stateid(stp);
  3443. } else
  3444. oo->oo_last_closed_stid = stp;
  3445. if (list_empty(&oo->oo_owner.so_stateids)) {
  3446. if (cstate->minorversion)
  3447. release_openowner(oo);
  3448. else {
  3449. /*
  3450. * In the 4.0 case we need to keep the owners around a
  3451. * little while to handle CLOSE replay.
  3452. */
  3453. move_to_close_lru(oo, SVC_NET(rqstp));
  3454. }
  3455. }
  3456. out:
  3457. if (!cstate->replay_owner)
  3458. nfs4_unlock_state();
  3459. return status;
  3460. }
  3461. __be32
  3462. nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3463. struct nfsd4_delegreturn *dr)
  3464. {
  3465. struct nfs4_delegation *dp;
  3466. stateid_t *stateid = &dr->dr_stateid;
  3467. struct nfs4_stid *s;
  3468. __be32 status;
  3469. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3470. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  3471. return status;
  3472. nfs4_lock_state();
  3473. status = nfsd4_lookup_stateid(stateid, NFS4_DELEG_STID, &s,
  3474. cstate->minorversion, nn);
  3475. if (status)
  3476. goto out;
  3477. dp = delegstateid(s);
  3478. status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
  3479. if (status)
  3480. goto out;
  3481. destroy_delegation(dp);
  3482. out:
  3483. nfs4_unlock_state();
  3484. return status;
  3485. }
  3486. #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
  3487. #define LOCKOWNER_INO_HASH_MASK (LOCKOWNER_INO_HASH_SIZE - 1)
  3488. static inline u64
  3489. end_offset(u64 start, u64 len)
  3490. {
  3491. u64 end;
  3492. end = start + len;
  3493. return end >= start ? end: NFS4_MAX_UINT64;
  3494. }
  3495. /* last octet in a range */
  3496. static inline u64
  3497. last_byte_offset(u64 start, u64 len)
  3498. {
  3499. u64 end;
  3500. WARN_ON_ONCE(!len);
  3501. end = start + len;
  3502. return end > start ? end - 1: NFS4_MAX_UINT64;
  3503. }
  3504. static unsigned int lockowner_ino_hashval(struct inode *inode, u32 cl_id, struct xdr_netobj *ownername)
  3505. {
  3506. return (file_hashval(inode) + cl_id
  3507. + opaque_hashval(ownername->data, ownername->len))
  3508. & LOCKOWNER_INO_HASH_MASK;
  3509. }
  3510. /*
  3511. * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
  3512. * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
  3513. * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
  3514. * locking, this prevents us from being completely protocol-compliant. The
  3515. * real solution to this problem is to start using unsigned file offsets in
  3516. * the VFS, but this is a very deep change!
  3517. */
  3518. static inline void
  3519. nfs4_transform_lock_offset(struct file_lock *lock)
  3520. {
  3521. if (lock->fl_start < 0)
  3522. lock->fl_start = OFFSET_MAX;
  3523. if (lock->fl_end < 0)
  3524. lock->fl_end = OFFSET_MAX;
  3525. }
  3526. /* Hack!: For now, we're defining this just so we can use a pointer to it
  3527. * as a unique cookie to identify our (NFSv4's) posix locks. */
  3528. static const struct lock_manager_operations nfsd_posix_mng_ops = {
  3529. };
  3530. static inline void
  3531. nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
  3532. {
  3533. struct nfs4_lockowner *lo;
  3534. if (fl->fl_lmops == &nfsd_posix_mng_ops) {
  3535. lo = (struct nfs4_lockowner *) fl->fl_owner;
  3536. deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
  3537. lo->lo_owner.so_owner.len, GFP_KERNEL);
  3538. if (!deny->ld_owner.data)
  3539. /* We just don't care that much */
  3540. goto nevermind;
  3541. deny->ld_owner.len = lo->lo_owner.so_owner.len;
  3542. deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
  3543. } else {
  3544. nevermind:
  3545. deny->ld_owner.len = 0;
  3546. deny->ld_owner.data = NULL;
  3547. deny->ld_clientid.cl_boot = 0;
  3548. deny->ld_clientid.cl_id = 0;
  3549. }
  3550. deny->ld_start = fl->fl_start;
  3551. deny->ld_length = NFS4_MAX_UINT64;
  3552. if (fl->fl_end != NFS4_MAX_UINT64)
  3553. deny->ld_length = fl->fl_end - fl->fl_start + 1;
  3554. deny->ld_type = NFS4_READ_LT;
  3555. if (fl->fl_type != F_RDLCK)
  3556. deny->ld_type = NFS4_WRITE_LT;
  3557. }
  3558. static bool same_lockowner_ino(struct nfs4_lockowner *lo, struct inode *inode, clientid_t *clid, struct xdr_netobj *owner)
  3559. {
  3560. struct nfs4_ol_stateid *lst;
  3561. if (!same_owner_str(&lo->lo_owner, owner, clid))
  3562. return false;
  3563. lst = list_first_entry(&lo->lo_owner.so_stateids,
  3564. struct nfs4_ol_stateid, st_perstateowner);
  3565. return lst->st_file->fi_inode == inode;
  3566. }
  3567. static struct nfs4_lockowner *
  3568. find_lockowner_str(struct inode *inode, clientid_t *clid,
  3569. struct xdr_netobj *owner, struct nfsd_net *nn)
  3570. {
  3571. unsigned int hashval = lockowner_ino_hashval(inode, clid->cl_id, owner);
  3572. struct nfs4_lockowner *lo;
  3573. list_for_each_entry(lo, &nn->lockowner_ino_hashtbl[hashval], lo_owner_ino_hash) {
  3574. if (same_lockowner_ino(lo, inode, clid, owner))
  3575. return lo;
  3576. }
  3577. return NULL;
  3578. }
  3579. static void hash_lockowner(struct nfs4_lockowner *lo, unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp)
  3580. {
  3581. struct inode *inode = open_stp->st_file->fi_inode;
  3582. unsigned int inohash = lockowner_ino_hashval(inode,
  3583. clp->cl_clientid.cl_id, &lo->lo_owner.so_owner);
  3584. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  3585. list_add(&lo->lo_owner.so_strhash, &nn->ownerstr_hashtbl[strhashval]);
  3586. list_add(&lo->lo_owner_ino_hash, &nn->lockowner_ino_hashtbl[inohash]);
  3587. list_add(&lo->lo_perstateid, &open_stp->st_lockowners);
  3588. }
  3589. /*
  3590. * Alloc a lock owner structure.
  3591. * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
  3592. * occurred.
  3593. *
  3594. * strhashval = ownerstr_hashval
  3595. */
  3596. static struct nfs4_lockowner *
  3597. alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, struct nfs4_ol_stateid *open_stp, struct nfsd4_lock *lock) {
  3598. struct nfs4_lockowner *lo;
  3599. lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
  3600. if (!lo)
  3601. return NULL;
  3602. INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
  3603. lo->lo_owner.so_is_open_owner = 0;
  3604. /* It is the openowner seqid that will be incremented in encode in the
  3605. * case of new lockowners; so increment the lock seqid manually: */
  3606. lo->lo_owner.so_seqid = lock->lk_new_lock_seqid + 1;
  3607. hash_lockowner(lo, strhashval, clp, open_stp);
  3608. return lo;
  3609. }
  3610. static struct nfs4_ol_stateid *
  3611. alloc_init_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp, struct nfs4_ol_stateid *open_stp)
  3612. {
  3613. struct nfs4_ol_stateid *stp;
  3614. struct nfs4_client *clp = lo->lo_owner.so_client;
  3615. stp = nfs4_alloc_stateid(clp);
  3616. if (stp == NULL)
  3617. return NULL;
  3618. stp->st_stid.sc_type = NFS4_LOCK_STID;
  3619. list_add(&stp->st_perfile, &fp->fi_stateids);
  3620. list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
  3621. stp->st_stateowner = &lo->lo_owner;
  3622. get_nfs4_file(fp);
  3623. stp->st_file = fp;
  3624. stp->st_access_bmap = 0;
  3625. stp->st_deny_bmap = open_stp->st_deny_bmap;
  3626. stp->st_openstp = open_stp;
  3627. return stp;
  3628. }
  3629. static int
  3630. check_lock_length(u64 offset, u64 length)
  3631. {
  3632. return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
  3633. LOFF_OVERFLOW(offset, length)));
  3634. }
  3635. static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
  3636. {
  3637. struct nfs4_file *fp = lock_stp->st_file;
  3638. int oflag = nfs4_access_to_omode(access);
  3639. if (test_access(access, lock_stp))
  3640. return;
  3641. nfs4_file_get_access(fp, oflag);
  3642. set_access(access, lock_stp);
  3643. }
  3644. static __be32 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, struct nfs4_ol_stateid *ost, struct nfsd4_lock *lock, struct nfs4_ol_stateid **lst, bool *new)
  3645. {
  3646. struct nfs4_file *fi = ost->st_file;
  3647. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  3648. struct nfs4_client *cl = oo->oo_owner.so_client;
  3649. struct nfs4_lockowner *lo;
  3650. unsigned int strhashval;
  3651. struct nfsd_net *nn = net_generic(cl->net, nfsd_net_id);
  3652. lo = find_lockowner_str(fi->fi_inode, &cl->cl_clientid,
  3653. &lock->v.new.owner, nn);
  3654. if (lo) {
  3655. if (!cstate->minorversion)
  3656. return nfserr_bad_seqid;
  3657. /* XXX: a lockowner always has exactly one stateid: */
  3658. *lst = list_first_entry(&lo->lo_owner.so_stateids,
  3659. struct nfs4_ol_stateid, st_perstateowner);
  3660. return nfs_ok;
  3661. }
  3662. strhashval = ownerstr_hashval(cl->cl_clientid.cl_id,
  3663. &lock->v.new.owner);
  3664. lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
  3665. if (lo == NULL)
  3666. return nfserr_jukebox;
  3667. *lst = alloc_init_lock_stateid(lo, fi, ost);
  3668. if (*lst == NULL) {
  3669. release_lockowner(lo);
  3670. return nfserr_jukebox;
  3671. }
  3672. *new = true;
  3673. return nfs_ok;
  3674. }
  3675. /*
  3676. * LOCK operation
  3677. */
  3678. __be32
  3679. nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3680. struct nfsd4_lock *lock)
  3681. {
  3682. struct nfs4_openowner *open_sop = NULL;
  3683. struct nfs4_lockowner *lock_sop = NULL;
  3684. struct nfs4_ol_stateid *lock_stp;
  3685. struct file *filp = NULL;
  3686. struct file_lock *file_lock = NULL;
  3687. struct file_lock *conflock = NULL;
  3688. __be32 status = 0;
  3689. bool new_state = false;
  3690. int lkflg;
  3691. int err;
  3692. struct net *net = SVC_NET(rqstp);
  3693. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3694. dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
  3695. (long long) lock->lk_offset,
  3696. (long long) lock->lk_length);
  3697. if (check_lock_length(lock->lk_offset, lock->lk_length))
  3698. return nfserr_inval;
  3699. if ((status = fh_verify(rqstp, &cstate->current_fh,
  3700. S_IFREG, NFSD_MAY_LOCK))) {
  3701. dprintk("NFSD: nfsd4_lock: permission denied!\n");
  3702. return status;
  3703. }
  3704. nfs4_lock_state();
  3705. if (lock->lk_is_new) {
  3706. struct nfs4_ol_stateid *open_stp = NULL;
  3707. if (nfsd4_has_session(cstate))
  3708. /* See rfc 5661 18.10.3: given clientid is ignored: */
  3709. memcpy(&lock->v.new.clientid,
  3710. &cstate->session->se_client->cl_clientid,
  3711. sizeof(clientid_t));
  3712. status = nfserr_stale_clientid;
  3713. if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
  3714. goto out;
  3715. /* validate and update open stateid and open seqid */
  3716. status = nfs4_preprocess_confirmed_seqid_op(cstate,
  3717. lock->lk_new_open_seqid,
  3718. &lock->lk_new_open_stateid,
  3719. &open_stp, nn);
  3720. if (status)
  3721. goto out;
  3722. open_sop = openowner(open_stp->st_stateowner);
  3723. status = nfserr_bad_stateid;
  3724. if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
  3725. &lock->v.new.clientid))
  3726. goto out;
  3727. status = lookup_or_create_lock_state(cstate, open_stp, lock,
  3728. &lock_stp, &new_state);
  3729. } else
  3730. status = nfs4_preprocess_seqid_op(cstate,
  3731. lock->lk_old_lock_seqid,
  3732. &lock->lk_old_lock_stateid,
  3733. NFS4_LOCK_STID, &lock_stp, nn);
  3734. if (status)
  3735. goto out;
  3736. lock_sop = lockowner(lock_stp->st_stateowner);
  3737. lkflg = setlkflg(lock->lk_type);
  3738. status = nfs4_check_openmode(lock_stp, lkflg);
  3739. if (status)
  3740. goto out;
  3741. status = nfserr_grace;
  3742. if (locks_in_grace(net) && !lock->lk_reclaim)
  3743. goto out;
  3744. status = nfserr_no_grace;
  3745. if (!locks_in_grace(net) && lock->lk_reclaim)
  3746. goto out;
  3747. file_lock = locks_alloc_lock();
  3748. if (!file_lock) {
  3749. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3750. status = nfserr_jukebox;
  3751. goto out;
  3752. }
  3753. locks_init_lock(file_lock);
  3754. switch (lock->lk_type) {
  3755. case NFS4_READ_LT:
  3756. case NFS4_READW_LT:
  3757. filp = find_readable_file(lock_stp->st_file);
  3758. if (filp)
  3759. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
  3760. file_lock->fl_type = F_RDLCK;
  3761. break;
  3762. case NFS4_WRITE_LT:
  3763. case NFS4_WRITEW_LT:
  3764. filp = find_writeable_file(lock_stp->st_file);
  3765. if (filp)
  3766. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
  3767. file_lock->fl_type = F_WRLCK;
  3768. break;
  3769. default:
  3770. status = nfserr_inval;
  3771. goto out;
  3772. }
  3773. if (!filp) {
  3774. status = nfserr_openmode;
  3775. goto out;
  3776. }
  3777. file_lock->fl_owner = (fl_owner_t)lock_sop;
  3778. file_lock->fl_pid = current->tgid;
  3779. file_lock->fl_file = filp;
  3780. file_lock->fl_flags = FL_POSIX;
  3781. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  3782. file_lock->fl_start = lock->lk_offset;
  3783. file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
  3784. nfs4_transform_lock_offset(file_lock);
  3785. conflock = locks_alloc_lock();
  3786. if (!conflock) {
  3787. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3788. status = nfserr_jukebox;
  3789. goto out;
  3790. }
  3791. err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
  3792. switch (-err) {
  3793. case 0: /* success! */
  3794. update_stateid(&lock_stp->st_stid.sc_stateid);
  3795. memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
  3796. sizeof(stateid_t));
  3797. status = 0;
  3798. break;
  3799. case (EAGAIN): /* conflock holds conflicting lock */
  3800. status = nfserr_denied;
  3801. dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
  3802. nfs4_set_lock_denied(conflock, &lock->lk_denied);
  3803. break;
  3804. case (EDEADLK):
  3805. status = nfserr_deadlock;
  3806. break;
  3807. default:
  3808. dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
  3809. status = nfserrno(err);
  3810. break;
  3811. }
  3812. out:
  3813. if (status && new_state)
  3814. release_lockowner(lock_sop);
  3815. nfsd4_bump_seqid(cstate, status);
  3816. if (!cstate->replay_owner)
  3817. nfs4_unlock_state();
  3818. if (file_lock)
  3819. locks_free_lock(file_lock);
  3820. if (conflock)
  3821. locks_free_lock(conflock);
  3822. return status;
  3823. }
  3824. /*
  3825. * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
  3826. * so we do a temporary open here just to get an open file to pass to
  3827. * vfs_test_lock. (Arguably perhaps test_lock should be done with an
  3828. * inode operation.)
  3829. */
  3830. static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
  3831. {
  3832. struct file *file;
  3833. __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  3834. if (!err) {
  3835. err = nfserrno(vfs_test_lock(file, lock));
  3836. nfsd_close(file);
  3837. }
  3838. return err;
  3839. }
  3840. /*
  3841. * LOCKT operation
  3842. */
  3843. __be32
  3844. nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3845. struct nfsd4_lockt *lockt)
  3846. {
  3847. struct inode *inode;
  3848. struct file_lock *file_lock = NULL;
  3849. struct nfs4_lockowner *lo;
  3850. __be32 status;
  3851. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3852. if (locks_in_grace(SVC_NET(rqstp)))
  3853. return nfserr_grace;
  3854. if (check_lock_length(lockt->lt_offset, lockt->lt_length))
  3855. return nfserr_inval;
  3856. nfs4_lock_state();
  3857. if (!nfsd4_has_session(cstate)) {
  3858. status = lookup_clientid(&lockt->lt_clientid, false, nn, NULL);
  3859. if (status)
  3860. goto out;
  3861. }
  3862. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  3863. goto out;
  3864. inode = cstate->current_fh.fh_dentry->d_inode;
  3865. file_lock = locks_alloc_lock();
  3866. if (!file_lock) {
  3867. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3868. status = nfserr_jukebox;
  3869. goto out;
  3870. }
  3871. locks_init_lock(file_lock);
  3872. switch (lockt->lt_type) {
  3873. case NFS4_READ_LT:
  3874. case NFS4_READW_LT:
  3875. file_lock->fl_type = F_RDLCK;
  3876. break;
  3877. case NFS4_WRITE_LT:
  3878. case NFS4_WRITEW_LT:
  3879. file_lock->fl_type = F_WRLCK;
  3880. break;
  3881. default:
  3882. dprintk("NFSD: nfs4_lockt: bad lock type!\n");
  3883. status = nfserr_inval;
  3884. goto out;
  3885. }
  3886. lo = find_lockowner_str(inode, &lockt->lt_clientid, &lockt->lt_owner, nn);
  3887. if (lo)
  3888. file_lock->fl_owner = (fl_owner_t)lo;
  3889. file_lock->fl_pid = current->tgid;
  3890. file_lock->fl_flags = FL_POSIX;
  3891. file_lock->fl_start = lockt->lt_offset;
  3892. file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
  3893. nfs4_transform_lock_offset(file_lock);
  3894. status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
  3895. if (status)
  3896. goto out;
  3897. if (file_lock->fl_type != F_UNLCK) {
  3898. status = nfserr_denied;
  3899. nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
  3900. }
  3901. out:
  3902. nfs4_unlock_state();
  3903. if (file_lock)
  3904. locks_free_lock(file_lock);
  3905. return status;
  3906. }
  3907. __be32
  3908. nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3909. struct nfsd4_locku *locku)
  3910. {
  3911. struct nfs4_lockowner *lo;
  3912. struct nfs4_ol_stateid *stp;
  3913. struct file *filp = NULL;
  3914. struct file_lock *file_lock = NULL;
  3915. __be32 status;
  3916. int err;
  3917. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3918. dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
  3919. (long long) locku->lu_offset,
  3920. (long long) locku->lu_length);
  3921. if (check_lock_length(locku->lu_offset, locku->lu_length))
  3922. return nfserr_inval;
  3923. nfs4_lock_state();
  3924. status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
  3925. &locku->lu_stateid, NFS4_LOCK_STID,
  3926. &stp, nn);
  3927. if (status)
  3928. goto out;
  3929. filp = find_any_file(stp->st_file);
  3930. if (!filp) {
  3931. status = nfserr_lock_range;
  3932. goto out;
  3933. }
  3934. file_lock = locks_alloc_lock();
  3935. if (!file_lock) {
  3936. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  3937. status = nfserr_jukebox;
  3938. goto out;
  3939. }
  3940. lo = lockowner(stp->st_stateowner);
  3941. locks_init_lock(file_lock);
  3942. file_lock->fl_type = F_UNLCK;
  3943. file_lock->fl_owner = (fl_owner_t)lo;
  3944. file_lock->fl_pid = current->tgid;
  3945. file_lock->fl_file = filp;
  3946. file_lock->fl_flags = FL_POSIX;
  3947. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  3948. file_lock->fl_start = locku->lu_offset;
  3949. file_lock->fl_end = last_byte_offset(locku->lu_offset,
  3950. locku->lu_length);
  3951. nfs4_transform_lock_offset(file_lock);
  3952. err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
  3953. if (err) {
  3954. dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
  3955. goto out_nfserr;
  3956. }
  3957. update_stateid(&stp->st_stid.sc_stateid);
  3958. memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3959. if (nfsd4_has_session(cstate) && !check_for_locks(stp->st_file, lo)) {
  3960. WARN_ON_ONCE(cstate->replay_owner);
  3961. release_lockowner(lo);
  3962. }
  3963. out:
  3964. nfsd4_bump_seqid(cstate, status);
  3965. if (!cstate->replay_owner)
  3966. nfs4_unlock_state();
  3967. if (file_lock)
  3968. locks_free_lock(file_lock);
  3969. return status;
  3970. out_nfserr:
  3971. status = nfserrno(err);
  3972. goto out;
  3973. }
  3974. /*
  3975. * returns
  3976. * 1: locks held by lockowner
  3977. * 0: no locks held by lockowner
  3978. */
  3979. static int
  3980. check_for_locks(struct nfs4_file *filp, struct nfs4_lockowner *lowner)
  3981. {
  3982. struct file_lock **flpp;
  3983. struct inode *inode = filp->fi_inode;
  3984. int status = 0;
  3985. lock_flocks();
  3986. for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
  3987. if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
  3988. status = 1;
  3989. goto out;
  3990. }
  3991. }
  3992. out:
  3993. unlock_flocks();
  3994. return status;
  3995. }
  3996. __be32
  3997. nfsd4_release_lockowner(struct svc_rqst *rqstp,
  3998. struct nfsd4_compound_state *cstate,
  3999. struct nfsd4_release_lockowner *rlockowner)
  4000. {
  4001. clientid_t *clid = &rlockowner->rl_clientid;
  4002. struct nfs4_stateowner *sop;
  4003. struct nfs4_lockowner *lo;
  4004. struct nfs4_ol_stateid *stp;
  4005. struct xdr_netobj *owner = &rlockowner->rl_owner;
  4006. struct list_head matches;
  4007. unsigned int hashval = ownerstr_hashval(clid->cl_id, owner);
  4008. __be32 status;
  4009. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  4010. dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
  4011. clid->cl_boot, clid->cl_id);
  4012. nfs4_lock_state();
  4013. status = lookup_clientid(clid, cstate->minorversion, nn, NULL);
  4014. if (status)
  4015. goto out;
  4016. status = nfserr_locks_held;
  4017. INIT_LIST_HEAD(&matches);
  4018. list_for_each_entry(sop, &nn->ownerstr_hashtbl[hashval], so_strhash) {
  4019. if (sop->so_is_open_owner)
  4020. continue;
  4021. if (!same_owner_str(sop, owner, clid))
  4022. continue;
  4023. list_for_each_entry(stp, &sop->so_stateids,
  4024. st_perstateowner) {
  4025. lo = lockowner(sop);
  4026. if (check_for_locks(stp->st_file, lo))
  4027. goto out;
  4028. list_add(&lo->lo_list, &matches);
  4029. }
  4030. }
  4031. /* Clients probably won't expect us to return with some (but not all)
  4032. * of the lockowner state released; so don't release any until all
  4033. * have been checked. */
  4034. status = nfs_ok;
  4035. while (!list_empty(&matches)) {
  4036. lo = list_entry(matches.next, struct nfs4_lockowner,
  4037. lo_list);
  4038. /* unhash_stateowner deletes so_perclient only
  4039. * for openowners. */
  4040. list_del(&lo->lo_list);
  4041. release_lockowner(lo);
  4042. }
  4043. out:
  4044. nfs4_unlock_state();
  4045. return status;
  4046. }
  4047. static inline struct nfs4_client_reclaim *
  4048. alloc_reclaim(void)
  4049. {
  4050. return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
  4051. }
  4052. bool
  4053. nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
  4054. {
  4055. struct nfs4_client_reclaim *crp;
  4056. crp = nfsd4_find_reclaim_client(name, nn);
  4057. return (crp && crp->cr_clp);
  4058. }
  4059. /*
  4060. * failure => all reset bets are off, nfserr_no_grace...
  4061. */
  4062. struct nfs4_client_reclaim *
  4063. nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
  4064. {
  4065. unsigned int strhashval;
  4066. struct nfs4_client_reclaim *crp;
  4067. dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
  4068. crp = alloc_reclaim();
  4069. if (crp) {
  4070. strhashval = clientstr_hashval(name);
  4071. INIT_LIST_HEAD(&crp->cr_strhash);
  4072. list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
  4073. memcpy(crp->cr_recdir, name, HEXDIR_LEN);
  4074. crp->cr_clp = NULL;
  4075. nn->reclaim_str_hashtbl_size++;
  4076. }
  4077. return crp;
  4078. }
  4079. void
  4080. nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
  4081. {
  4082. list_del(&crp->cr_strhash);
  4083. kfree(crp);
  4084. nn->reclaim_str_hashtbl_size--;
  4085. }
  4086. void
  4087. nfs4_release_reclaim(struct nfsd_net *nn)
  4088. {
  4089. struct nfs4_client_reclaim *crp = NULL;
  4090. int i;
  4091. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4092. while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
  4093. crp = list_entry(nn->reclaim_str_hashtbl[i].next,
  4094. struct nfs4_client_reclaim, cr_strhash);
  4095. nfs4_remove_reclaim_record(crp, nn);
  4096. }
  4097. }
  4098. WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
  4099. }
  4100. /*
  4101. * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
  4102. struct nfs4_client_reclaim *
  4103. nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
  4104. {
  4105. unsigned int strhashval;
  4106. struct nfs4_client_reclaim *crp = NULL;
  4107. dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
  4108. strhashval = clientstr_hashval(recdir);
  4109. list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
  4110. if (same_name(crp->cr_recdir, recdir)) {
  4111. return crp;
  4112. }
  4113. }
  4114. return NULL;
  4115. }
  4116. /*
  4117. * Called from OPEN. Look for clientid in reclaim list.
  4118. */
  4119. __be32
  4120. nfs4_check_open_reclaim(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  4121. {
  4122. struct nfs4_client *clp;
  4123. /* find clientid in conf_id_hashtbl */
  4124. clp = find_confirmed_client(clid, sessions, nn);
  4125. if (clp == NULL)
  4126. return nfserr_reclaim_bad;
  4127. return nfsd4_client_record_check(clp) ? nfserr_reclaim_bad : nfs_ok;
  4128. }
  4129. #ifdef CONFIG_NFSD_FAULT_INJECTION
  4130. u64 nfsd_forget_client(struct nfs4_client *clp, u64 max)
  4131. {
  4132. if (mark_client_expired(clp))
  4133. return 0;
  4134. expire_client(clp);
  4135. return 1;
  4136. }
  4137. u64 nfsd_print_client(struct nfs4_client *clp, u64 num)
  4138. {
  4139. char buf[INET6_ADDRSTRLEN];
  4140. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  4141. printk(KERN_INFO "NFS Client: %s\n", buf);
  4142. return 1;
  4143. }
  4144. static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
  4145. const char *type)
  4146. {
  4147. char buf[INET6_ADDRSTRLEN];
  4148. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  4149. printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
  4150. }
  4151. static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_lockowner *))
  4152. {
  4153. struct nfs4_openowner *oop;
  4154. struct nfs4_lockowner *lop, *lo_next;
  4155. struct nfs4_ol_stateid *stp, *st_next;
  4156. u64 count = 0;
  4157. list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
  4158. list_for_each_entry_safe(stp, st_next, &oop->oo_owner.so_stateids, st_perstateowner) {
  4159. list_for_each_entry_safe(lop, lo_next, &stp->st_lockowners, lo_perstateid) {
  4160. if (func)
  4161. func(lop);
  4162. if (++count == max)
  4163. return count;
  4164. }
  4165. }
  4166. }
  4167. return count;
  4168. }
  4169. u64 nfsd_forget_client_locks(struct nfs4_client *clp, u64 max)
  4170. {
  4171. return nfsd_foreach_client_lock(clp, max, release_lockowner);
  4172. }
  4173. u64 nfsd_print_client_locks(struct nfs4_client *clp, u64 max)
  4174. {
  4175. u64 count = nfsd_foreach_client_lock(clp, max, NULL);
  4176. nfsd_print_count(clp, count, "locked files");
  4177. return count;
  4178. }
  4179. static u64 nfsd_foreach_client_open(struct nfs4_client *clp, u64 max, void (*func)(struct nfs4_openowner *))
  4180. {
  4181. struct nfs4_openowner *oop, *next;
  4182. u64 count = 0;
  4183. list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
  4184. if (func)
  4185. func(oop);
  4186. if (++count == max)
  4187. break;
  4188. }
  4189. return count;
  4190. }
  4191. u64 nfsd_forget_client_openowners(struct nfs4_client *clp, u64 max)
  4192. {
  4193. return nfsd_foreach_client_open(clp, max, release_openowner);
  4194. }
  4195. u64 nfsd_print_client_openowners(struct nfs4_client *clp, u64 max)
  4196. {
  4197. u64 count = nfsd_foreach_client_open(clp, max, NULL);
  4198. nfsd_print_count(clp, count, "open files");
  4199. return count;
  4200. }
  4201. static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
  4202. struct list_head *victims)
  4203. {
  4204. struct nfs4_delegation *dp, *next;
  4205. u64 count = 0;
  4206. list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
  4207. if (victims)
  4208. list_move(&dp->dl_recall_lru, victims);
  4209. if (++count == max)
  4210. break;
  4211. }
  4212. return count;
  4213. }
  4214. u64 nfsd_forget_client_delegations(struct nfs4_client *clp, u64 max)
  4215. {
  4216. struct nfs4_delegation *dp, *next;
  4217. LIST_HEAD(victims);
  4218. u64 count;
  4219. spin_lock(&recall_lock);
  4220. count = nfsd_find_all_delegations(clp, max, &victims);
  4221. spin_unlock(&recall_lock);
  4222. list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
  4223. revoke_delegation(dp);
  4224. return count;
  4225. }
  4226. u64 nfsd_recall_client_delegations(struct nfs4_client *clp, u64 max)
  4227. {
  4228. struct nfs4_delegation *dp, *next;
  4229. LIST_HEAD(victims);
  4230. u64 count;
  4231. spin_lock(&recall_lock);
  4232. count = nfsd_find_all_delegations(clp, max, &victims);
  4233. list_for_each_entry_safe(dp, next, &victims, dl_recall_lru)
  4234. nfsd_break_one_deleg(dp);
  4235. spin_unlock(&recall_lock);
  4236. return count;
  4237. }
  4238. u64 nfsd_print_client_delegations(struct nfs4_client *clp, u64 max)
  4239. {
  4240. u64 count = 0;
  4241. spin_lock(&recall_lock);
  4242. count = nfsd_find_all_delegations(clp, max, NULL);
  4243. spin_unlock(&recall_lock);
  4244. nfsd_print_count(clp, count, "delegations");
  4245. return count;
  4246. }
  4247. u64 nfsd_for_n_state(u64 max, u64 (*func)(struct nfs4_client *, u64))
  4248. {
  4249. struct nfs4_client *clp, *next;
  4250. u64 count = 0;
  4251. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
  4252. if (!nfsd_netns_ready(nn))
  4253. return 0;
  4254. list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
  4255. count += func(clp, max - count);
  4256. if ((max != 0) && (count >= max))
  4257. break;
  4258. }
  4259. return count;
  4260. }
  4261. struct nfs4_client *nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
  4262. {
  4263. struct nfs4_client *clp;
  4264. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, nfsd_net_id);
  4265. if (!nfsd_netns_ready(nn))
  4266. return NULL;
  4267. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  4268. if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
  4269. return clp;
  4270. }
  4271. return NULL;
  4272. }
  4273. #endif /* CONFIG_NFSD_FAULT_INJECTION */
  4274. /* initialization to perform at module load time: */
  4275. void
  4276. nfs4_state_init(void)
  4277. {
  4278. }
  4279. /*
  4280. * Since the lifetime of a delegation isn't limited to that of an open, a
  4281. * client may quite reasonably hang on to a delegation as long as it has
  4282. * the inode cached. This becomes an obvious problem the first time a
  4283. * client's inode cache approaches the size of the server's total memory.
  4284. *
  4285. * For now we avoid this problem by imposing a hard limit on the number
  4286. * of delegations, which varies according to the server's memory size.
  4287. */
  4288. static void
  4289. set_max_delegations(void)
  4290. {
  4291. /*
  4292. * Allow at most 4 delegations per megabyte of RAM. Quick
  4293. * estimates suggest that in the worst case (where every delegation
  4294. * is for a different inode), a delegation could take about 1.5K,
  4295. * giving a worst case usage of about 6% of memory.
  4296. */
  4297. max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
  4298. }
  4299. static int nfs4_state_create_net(struct net *net)
  4300. {
  4301. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4302. int i;
  4303. nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  4304. CLIENT_HASH_SIZE, GFP_KERNEL);
  4305. if (!nn->conf_id_hashtbl)
  4306. goto err;
  4307. nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  4308. CLIENT_HASH_SIZE, GFP_KERNEL);
  4309. if (!nn->unconf_id_hashtbl)
  4310. goto err_unconf_id;
  4311. nn->ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
  4312. OWNER_HASH_SIZE, GFP_KERNEL);
  4313. if (!nn->ownerstr_hashtbl)
  4314. goto err_ownerstr;
  4315. nn->lockowner_ino_hashtbl = kmalloc(sizeof(struct list_head) *
  4316. LOCKOWNER_INO_HASH_SIZE, GFP_KERNEL);
  4317. if (!nn->lockowner_ino_hashtbl)
  4318. goto err_lockowner_ino;
  4319. nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
  4320. SESSION_HASH_SIZE, GFP_KERNEL);
  4321. if (!nn->sessionid_hashtbl)
  4322. goto err_sessionid;
  4323. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4324. INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
  4325. INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
  4326. }
  4327. for (i = 0; i < OWNER_HASH_SIZE; i++)
  4328. INIT_LIST_HEAD(&nn->ownerstr_hashtbl[i]);
  4329. for (i = 0; i < LOCKOWNER_INO_HASH_SIZE; i++)
  4330. INIT_LIST_HEAD(&nn->lockowner_ino_hashtbl[i]);
  4331. for (i = 0; i < SESSION_HASH_SIZE; i++)
  4332. INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
  4333. nn->conf_name_tree = RB_ROOT;
  4334. nn->unconf_name_tree = RB_ROOT;
  4335. INIT_LIST_HEAD(&nn->client_lru);
  4336. INIT_LIST_HEAD(&nn->close_lru);
  4337. INIT_LIST_HEAD(&nn->del_recall_lru);
  4338. spin_lock_init(&nn->client_lock);
  4339. INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
  4340. get_net(net);
  4341. return 0;
  4342. err_sessionid:
  4343. kfree(nn->lockowner_ino_hashtbl);
  4344. err_lockowner_ino:
  4345. kfree(nn->ownerstr_hashtbl);
  4346. err_ownerstr:
  4347. kfree(nn->unconf_id_hashtbl);
  4348. err_unconf_id:
  4349. kfree(nn->conf_id_hashtbl);
  4350. err:
  4351. return -ENOMEM;
  4352. }
  4353. static void
  4354. nfs4_state_destroy_net(struct net *net)
  4355. {
  4356. int i;
  4357. struct nfs4_client *clp = NULL;
  4358. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4359. struct rb_node *node, *tmp;
  4360. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4361. while (!list_empty(&nn->conf_id_hashtbl[i])) {
  4362. clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  4363. destroy_client(clp);
  4364. }
  4365. }
  4366. node = rb_first(&nn->unconf_name_tree);
  4367. while (node != NULL) {
  4368. tmp = node;
  4369. node = rb_next(tmp);
  4370. clp = rb_entry(tmp, struct nfs4_client, cl_namenode);
  4371. rb_erase(tmp, &nn->unconf_name_tree);
  4372. destroy_client(clp);
  4373. }
  4374. kfree(nn->sessionid_hashtbl);
  4375. kfree(nn->lockowner_ino_hashtbl);
  4376. kfree(nn->ownerstr_hashtbl);
  4377. kfree(nn->unconf_id_hashtbl);
  4378. kfree(nn->conf_id_hashtbl);
  4379. put_net(net);
  4380. }
  4381. int
  4382. nfs4_state_start_net(struct net *net)
  4383. {
  4384. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4385. int ret;
  4386. ret = nfs4_state_create_net(net);
  4387. if (ret)
  4388. return ret;
  4389. nfsd4_client_tracking_init(net);
  4390. nn->boot_time = get_seconds();
  4391. locks_start_grace(net, &nn->nfsd4_manager);
  4392. nn->grace_ended = false;
  4393. printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
  4394. nn->nfsd4_grace, net);
  4395. queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
  4396. return 0;
  4397. }
  4398. /* initialization to perform when the nfsd service is started: */
  4399. int
  4400. nfs4_state_start(void)
  4401. {
  4402. int ret;
  4403. ret = set_callback_cred();
  4404. if (ret)
  4405. return -ENOMEM;
  4406. laundry_wq = create_singlethread_workqueue("nfsd4");
  4407. if (laundry_wq == NULL) {
  4408. ret = -ENOMEM;
  4409. goto out_recovery;
  4410. }
  4411. ret = nfsd4_create_callback_queue();
  4412. if (ret)
  4413. goto out_free_laundry;
  4414. set_max_delegations();
  4415. return 0;
  4416. out_free_laundry:
  4417. destroy_workqueue(laundry_wq);
  4418. out_recovery:
  4419. return ret;
  4420. }
  4421. /* should be called with the state lock held */
  4422. void
  4423. nfs4_state_shutdown_net(struct net *net)
  4424. {
  4425. struct nfs4_delegation *dp = NULL;
  4426. struct list_head *pos, *next, reaplist;
  4427. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4428. cancel_delayed_work_sync(&nn->laundromat_work);
  4429. locks_end_grace(&nn->nfsd4_manager);
  4430. INIT_LIST_HEAD(&reaplist);
  4431. spin_lock(&recall_lock);
  4432. list_for_each_safe(pos, next, &nn->del_recall_lru) {
  4433. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4434. list_move(&dp->dl_recall_lru, &reaplist);
  4435. }
  4436. spin_unlock(&recall_lock);
  4437. list_for_each_safe(pos, next, &reaplist) {
  4438. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  4439. destroy_delegation(dp);
  4440. }
  4441. nfsd4_client_tracking_exit(net);
  4442. nfs4_state_destroy_net(net);
  4443. }
  4444. void
  4445. nfs4_state_shutdown(void)
  4446. {
  4447. destroy_workqueue(laundry_wq);
  4448. nfsd4_destroy_callback_queue();
  4449. }
  4450. static void
  4451. get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  4452. {
  4453. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
  4454. memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
  4455. }
  4456. static void
  4457. put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  4458. {
  4459. if (cstate->minorversion) {
  4460. memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
  4461. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  4462. }
  4463. }
  4464. void
  4465. clear_current_stateid(struct nfsd4_compound_state *cstate)
  4466. {
  4467. CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  4468. }
  4469. /*
  4470. * functions to set current state id
  4471. */
  4472. void
  4473. nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  4474. {
  4475. put_stateid(cstate, &odp->od_stateid);
  4476. }
  4477. void
  4478. nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  4479. {
  4480. put_stateid(cstate, &open->op_stateid);
  4481. }
  4482. void
  4483. nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  4484. {
  4485. put_stateid(cstate, &close->cl_stateid);
  4486. }
  4487. void
  4488. nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
  4489. {
  4490. put_stateid(cstate, &lock->lk_resp_stateid);
  4491. }
  4492. /*
  4493. * functions to consume current state id
  4494. */
  4495. void
  4496. nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  4497. {
  4498. get_stateid(cstate, &odp->od_stateid);
  4499. }
  4500. void
  4501. nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
  4502. {
  4503. get_stateid(cstate, &drp->dr_stateid);
  4504. }
  4505. void
  4506. nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
  4507. {
  4508. get_stateid(cstate, &fsp->fr_stateid);
  4509. }
  4510. void
  4511. nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
  4512. {
  4513. get_stateid(cstate, &setattr->sa_stateid);
  4514. }
  4515. void
  4516. nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  4517. {
  4518. get_stateid(cstate, &close->cl_stateid);
  4519. }
  4520. void
  4521. nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
  4522. {
  4523. get_stateid(cstate, &locku->lu_stateid);
  4524. }
  4525. void
  4526. nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
  4527. {
  4528. get_stateid(cstate, &read->rd_stateid);
  4529. }
  4530. void
  4531. nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
  4532. {
  4533. get_stateid(cstate, &write->wr_stateid);
  4534. }