nfs4state.c 136 KB

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