nfs4state.c 136 KB

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