nfs4state.c 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537
  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 <linux/hash.h>
  44. #include "xdr4.h"
  45. #include "xdr4cb.h"
  46. #include "vfs.h"
  47. #include "current_stateid.h"
  48. #include "netns.h"
  49. #define NFSDDBG_FACILITY NFSDDBG_PROC
  50. #define all_ones {{~0,~0},~0}
  51. static const stateid_t one_stateid = {
  52. .si_generation = ~0,
  53. .si_opaque = all_ones,
  54. };
  55. static const stateid_t zero_stateid = {
  56. /* all fields zero */
  57. };
  58. static const stateid_t currentstateid = {
  59. .si_generation = 1,
  60. };
  61. static u64 current_sessionid = 1;
  62. #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
  63. #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
  64. #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
  65. /* forward declarations */
  66. static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
  67. static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
  68. /* Locking: */
  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(state_lock);
  75. /*
  76. * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
  77. * the refcount on the open stateid to drop.
  78. */
  79. static DECLARE_WAIT_QUEUE_HEAD(close_wq);
  80. static struct kmem_cache *openowner_slab;
  81. static struct kmem_cache *lockowner_slab;
  82. static struct kmem_cache *file_slab;
  83. static struct kmem_cache *stateid_slab;
  84. static struct kmem_cache *deleg_slab;
  85. static void free_session(struct nfsd4_session *);
  86. static bool is_session_dead(struct nfsd4_session *ses)
  87. {
  88. return ses->se_flags & NFS4_SESSION_DEAD;
  89. }
  90. static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
  91. {
  92. if (atomic_read(&ses->se_ref) > ref_held_by_me)
  93. return nfserr_jukebox;
  94. ses->se_flags |= NFS4_SESSION_DEAD;
  95. return nfs_ok;
  96. }
  97. static bool is_client_expired(struct nfs4_client *clp)
  98. {
  99. return clp->cl_time == 0;
  100. }
  101. static __be32 get_client_locked(struct nfs4_client *clp)
  102. {
  103. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  104. lockdep_assert_held(&nn->client_lock);
  105. if (is_client_expired(clp))
  106. return nfserr_expired;
  107. atomic_inc(&clp->cl_refcount);
  108. return nfs_ok;
  109. }
  110. /* must be called under the client_lock */
  111. static inline void
  112. renew_client_locked(struct nfs4_client *clp)
  113. {
  114. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  115. if (is_client_expired(clp)) {
  116. WARN_ON(1);
  117. printk("%s: client (clientid %08x/%08x) already expired\n",
  118. __func__,
  119. clp->cl_clientid.cl_boot,
  120. clp->cl_clientid.cl_id);
  121. return;
  122. }
  123. dprintk("renewing client (clientid %08x/%08x)\n",
  124. clp->cl_clientid.cl_boot,
  125. clp->cl_clientid.cl_id);
  126. list_move_tail(&clp->cl_lru, &nn->client_lru);
  127. clp->cl_time = get_seconds();
  128. }
  129. static inline void
  130. renew_client(struct nfs4_client *clp)
  131. {
  132. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  133. spin_lock(&nn->client_lock);
  134. renew_client_locked(clp);
  135. spin_unlock(&nn->client_lock);
  136. }
  137. static void put_client_renew_locked(struct nfs4_client *clp)
  138. {
  139. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  140. lockdep_assert_held(&nn->client_lock);
  141. if (!atomic_dec_and_test(&clp->cl_refcount))
  142. return;
  143. if (!is_client_expired(clp))
  144. renew_client_locked(clp);
  145. }
  146. static void put_client_renew(struct nfs4_client *clp)
  147. {
  148. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  149. if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
  150. return;
  151. if (!is_client_expired(clp))
  152. renew_client_locked(clp);
  153. spin_unlock(&nn->client_lock);
  154. }
  155. static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
  156. {
  157. __be32 status;
  158. if (is_session_dead(ses))
  159. return nfserr_badsession;
  160. status = get_client_locked(ses->se_client);
  161. if (status)
  162. return status;
  163. atomic_inc(&ses->se_ref);
  164. return nfs_ok;
  165. }
  166. static void nfsd4_put_session_locked(struct nfsd4_session *ses)
  167. {
  168. struct nfs4_client *clp = ses->se_client;
  169. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  170. lockdep_assert_held(&nn->client_lock);
  171. if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
  172. free_session(ses);
  173. put_client_renew_locked(clp);
  174. }
  175. static void nfsd4_put_session(struct nfsd4_session *ses)
  176. {
  177. struct nfs4_client *clp = ses->se_client;
  178. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  179. spin_lock(&nn->client_lock);
  180. nfsd4_put_session_locked(ses);
  181. spin_unlock(&nn->client_lock);
  182. }
  183. static inline struct nfs4_stateowner *
  184. nfs4_get_stateowner(struct nfs4_stateowner *sop)
  185. {
  186. atomic_inc(&sop->so_count);
  187. return sop;
  188. }
  189. static int
  190. same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
  191. {
  192. return (sop->so_owner.len == owner->len) &&
  193. 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
  194. }
  195. static struct nfs4_openowner *
  196. find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
  197. struct nfs4_client *clp)
  198. {
  199. struct nfs4_stateowner *so;
  200. lockdep_assert_held(&clp->cl_lock);
  201. list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
  202. so_strhash) {
  203. if (!so->so_is_open_owner)
  204. continue;
  205. if (same_owner_str(so, &open->op_owner))
  206. return openowner(nfs4_get_stateowner(so));
  207. }
  208. return NULL;
  209. }
  210. static struct nfs4_openowner *
  211. find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
  212. struct nfs4_client *clp)
  213. {
  214. struct nfs4_openowner *oo;
  215. spin_lock(&clp->cl_lock);
  216. oo = find_openstateowner_str_locked(hashval, open, clp);
  217. spin_unlock(&clp->cl_lock);
  218. return oo;
  219. }
  220. static inline u32
  221. opaque_hashval(const void *ptr, int nbytes)
  222. {
  223. unsigned char *cptr = (unsigned char *) ptr;
  224. u32 x = 0;
  225. while (nbytes--) {
  226. x *= 37;
  227. x += *cptr++;
  228. }
  229. return x;
  230. }
  231. static void nfsd4_free_file(struct nfs4_file *f)
  232. {
  233. kmem_cache_free(file_slab, f);
  234. }
  235. static inline void
  236. put_nfs4_file(struct nfs4_file *fi)
  237. {
  238. might_lock(&state_lock);
  239. if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
  240. hlist_del(&fi->fi_hash);
  241. spin_unlock(&state_lock);
  242. nfsd4_free_file(fi);
  243. }
  244. }
  245. static inline void
  246. get_nfs4_file(struct nfs4_file *fi)
  247. {
  248. atomic_inc(&fi->fi_ref);
  249. }
  250. static struct file *
  251. __nfs4_get_fd(struct nfs4_file *f, int oflag)
  252. {
  253. if (f->fi_fds[oflag])
  254. return get_file(f->fi_fds[oflag]);
  255. return NULL;
  256. }
  257. static struct file *
  258. find_writeable_file_locked(struct nfs4_file *f)
  259. {
  260. struct file *ret;
  261. lockdep_assert_held(&f->fi_lock);
  262. ret = __nfs4_get_fd(f, O_WRONLY);
  263. if (!ret)
  264. ret = __nfs4_get_fd(f, O_RDWR);
  265. return ret;
  266. }
  267. static struct file *
  268. find_writeable_file(struct nfs4_file *f)
  269. {
  270. struct file *ret;
  271. spin_lock(&f->fi_lock);
  272. ret = find_writeable_file_locked(f);
  273. spin_unlock(&f->fi_lock);
  274. return ret;
  275. }
  276. static struct file *find_readable_file_locked(struct nfs4_file *f)
  277. {
  278. struct file *ret;
  279. lockdep_assert_held(&f->fi_lock);
  280. ret = __nfs4_get_fd(f, O_RDONLY);
  281. if (!ret)
  282. ret = __nfs4_get_fd(f, O_RDWR);
  283. return ret;
  284. }
  285. static struct file *
  286. find_readable_file(struct nfs4_file *f)
  287. {
  288. struct file *ret;
  289. spin_lock(&f->fi_lock);
  290. ret = find_readable_file_locked(f);
  291. spin_unlock(&f->fi_lock);
  292. return ret;
  293. }
  294. static struct file *
  295. find_any_file(struct nfs4_file *f)
  296. {
  297. struct file *ret;
  298. spin_lock(&f->fi_lock);
  299. ret = __nfs4_get_fd(f, O_RDWR);
  300. if (!ret) {
  301. ret = __nfs4_get_fd(f, O_WRONLY);
  302. if (!ret)
  303. ret = __nfs4_get_fd(f, O_RDONLY);
  304. }
  305. spin_unlock(&f->fi_lock);
  306. return ret;
  307. }
  308. static atomic_long_t num_delegations;
  309. unsigned long max_delegations;
  310. /*
  311. * Open owner state (share locks)
  312. */
  313. /* hash tables for lock and open owners */
  314. #define OWNER_HASH_BITS 8
  315. #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
  316. #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
  317. static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
  318. {
  319. unsigned int ret;
  320. ret = opaque_hashval(ownername->data, ownername->len);
  321. return ret & OWNER_HASH_MASK;
  322. }
  323. /* hash table for nfs4_file */
  324. #define FILE_HASH_BITS 8
  325. #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
  326. static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
  327. {
  328. return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
  329. }
  330. static unsigned int file_hashval(struct knfsd_fh *fh)
  331. {
  332. return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
  333. }
  334. static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
  335. {
  336. return fh1->fh_size == fh2->fh_size &&
  337. !memcmp(fh1->fh_base.fh_pad,
  338. fh2->fh_base.fh_pad,
  339. fh1->fh_size);
  340. }
  341. static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
  342. static void
  343. __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
  344. {
  345. lockdep_assert_held(&fp->fi_lock);
  346. if (access & NFS4_SHARE_ACCESS_WRITE)
  347. atomic_inc(&fp->fi_access[O_WRONLY]);
  348. if (access & NFS4_SHARE_ACCESS_READ)
  349. atomic_inc(&fp->fi_access[O_RDONLY]);
  350. }
  351. static __be32
  352. nfs4_file_get_access(struct nfs4_file *fp, u32 access)
  353. {
  354. lockdep_assert_held(&fp->fi_lock);
  355. /* Does this access mode make sense? */
  356. if (access & ~NFS4_SHARE_ACCESS_BOTH)
  357. return nfserr_inval;
  358. /* Does it conflict with a deny mode already set? */
  359. if ((access & fp->fi_share_deny) != 0)
  360. return nfserr_share_denied;
  361. __nfs4_file_get_access(fp, access);
  362. return nfs_ok;
  363. }
  364. static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
  365. {
  366. /* Common case is that there is no deny mode. */
  367. if (deny) {
  368. /* Does this deny mode make sense? */
  369. if (deny & ~NFS4_SHARE_DENY_BOTH)
  370. return nfserr_inval;
  371. if ((deny & NFS4_SHARE_DENY_READ) &&
  372. atomic_read(&fp->fi_access[O_RDONLY]))
  373. return nfserr_share_denied;
  374. if ((deny & NFS4_SHARE_DENY_WRITE) &&
  375. atomic_read(&fp->fi_access[O_WRONLY]))
  376. return nfserr_share_denied;
  377. }
  378. return nfs_ok;
  379. }
  380. static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  381. {
  382. might_lock(&fp->fi_lock);
  383. if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
  384. struct file *f1 = NULL;
  385. struct file *f2 = NULL;
  386. swap(f1, fp->fi_fds[oflag]);
  387. if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
  388. swap(f2, fp->fi_fds[O_RDWR]);
  389. spin_unlock(&fp->fi_lock);
  390. if (f1)
  391. fput(f1);
  392. if (f2)
  393. fput(f2);
  394. }
  395. }
  396. static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
  397. {
  398. WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
  399. if (access & NFS4_SHARE_ACCESS_WRITE)
  400. __nfs4_file_put_access(fp, O_WRONLY);
  401. if (access & NFS4_SHARE_ACCESS_READ)
  402. __nfs4_file_put_access(fp, O_RDONLY);
  403. }
  404. static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
  405. struct kmem_cache *slab)
  406. {
  407. struct nfs4_stid *stid;
  408. int new_id;
  409. stid = kmem_cache_zalloc(slab, GFP_KERNEL);
  410. if (!stid)
  411. return NULL;
  412. idr_preload(GFP_KERNEL);
  413. spin_lock(&cl->cl_lock);
  414. new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
  415. spin_unlock(&cl->cl_lock);
  416. idr_preload_end();
  417. if (new_id < 0)
  418. goto out_free;
  419. stid->sc_client = cl;
  420. stid->sc_stateid.si_opaque.so_id = new_id;
  421. stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
  422. /* Will be incremented before return to client: */
  423. atomic_set(&stid->sc_count, 1);
  424. /*
  425. * It shouldn't be a problem to reuse an opaque stateid value.
  426. * I don't think it is for 4.1. But with 4.0 I worry that, for
  427. * example, a stray write retransmission could be accepted by
  428. * the server when it should have been rejected. Therefore,
  429. * adopt a trick from the sctp code to attempt to maximize the
  430. * amount of time until an id is reused, by ensuring they always
  431. * "increase" (mod INT_MAX):
  432. */
  433. return stid;
  434. out_free:
  435. kmem_cache_free(slab, stid);
  436. return NULL;
  437. }
  438. static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
  439. {
  440. struct nfs4_stid *stid;
  441. struct nfs4_ol_stateid *stp;
  442. stid = nfs4_alloc_stid(clp, stateid_slab);
  443. if (!stid)
  444. return NULL;
  445. stp = openlockstateid(stid);
  446. stp->st_stid.sc_free = nfs4_free_ol_stateid;
  447. return stp;
  448. }
  449. static void nfs4_free_deleg(struct nfs4_stid *stid)
  450. {
  451. kmem_cache_free(deleg_slab, stid);
  452. atomic_long_dec(&num_delegations);
  453. }
  454. /*
  455. * When we recall a delegation, we should be careful not to hand it
  456. * out again straight away.
  457. * To ensure this we keep a pair of bloom filters ('new' and 'old')
  458. * in which the filehandles of recalled delegations are "stored".
  459. * If a filehandle appear in either filter, a delegation is blocked.
  460. * When a delegation is recalled, the filehandle is stored in the "new"
  461. * filter.
  462. * Every 30 seconds we swap the filters and clear the "new" one,
  463. * unless both are empty of course.
  464. *
  465. * Each filter is 256 bits. We hash the filehandle to 32bit and use the
  466. * low 3 bytes as hash-table indices.
  467. *
  468. * 'blocked_delegations_lock', which is always taken in block_delegations(),
  469. * is used to manage concurrent access. Testing does not need the lock
  470. * except when swapping the two filters.
  471. */
  472. static DEFINE_SPINLOCK(blocked_delegations_lock);
  473. static struct bloom_pair {
  474. int entries, old_entries;
  475. time_t swap_time;
  476. int new; /* index into 'set' */
  477. DECLARE_BITMAP(set[2], 256);
  478. } blocked_delegations;
  479. static int delegation_blocked(struct knfsd_fh *fh)
  480. {
  481. u32 hash;
  482. struct bloom_pair *bd = &blocked_delegations;
  483. if (bd->entries == 0)
  484. return 0;
  485. if (seconds_since_boot() - bd->swap_time > 30) {
  486. spin_lock(&blocked_delegations_lock);
  487. if (seconds_since_boot() - bd->swap_time > 30) {
  488. bd->entries -= bd->old_entries;
  489. bd->old_entries = bd->entries;
  490. memset(bd->set[bd->new], 0,
  491. sizeof(bd->set[0]));
  492. bd->new = 1-bd->new;
  493. bd->swap_time = seconds_since_boot();
  494. }
  495. spin_unlock(&blocked_delegations_lock);
  496. }
  497. hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
  498. if (test_bit(hash&255, bd->set[0]) &&
  499. test_bit((hash>>8)&255, bd->set[0]) &&
  500. test_bit((hash>>16)&255, bd->set[0]))
  501. return 1;
  502. if (test_bit(hash&255, bd->set[1]) &&
  503. test_bit((hash>>8)&255, bd->set[1]) &&
  504. test_bit((hash>>16)&255, bd->set[1]))
  505. return 1;
  506. return 0;
  507. }
  508. static void block_delegations(struct knfsd_fh *fh)
  509. {
  510. u32 hash;
  511. struct bloom_pair *bd = &blocked_delegations;
  512. hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
  513. spin_lock(&blocked_delegations_lock);
  514. __set_bit(hash&255, bd->set[bd->new]);
  515. __set_bit((hash>>8)&255, bd->set[bd->new]);
  516. __set_bit((hash>>16)&255, bd->set[bd->new]);
  517. if (bd->entries == 0)
  518. bd->swap_time = seconds_since_boot();
  519. bd->entries += 1;
  520. spin_unlock(&blocked_delegations_lock);
  521. }
  522. static struct nfs4_delegation *
  523. alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
  524. {
  525. struct nfs4_delegation *dp;
  526. long n;
  527. dprintk("NFSD alloc_init_deleg\n");
  528. n = atomic_long_inc_return(&num_delegations);
  529. if (n < 0 || n > max_delegations)
  530. goto out_dec;
  531. if (delegation_blocked(&current_fh->fh_handle))
  532. goto out_dec;
  533. dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
  534. if (dp == NULL)
  535. goto out_dec;
  536. dp->dl_stid.sc_free = nfs4_free_deleg;
  537. /*
  538. * delegation seqid's are never incremented. The 4.1 special
  539. * meaning of seqid 0 isn't meaningful, really, but let's avoid
  540. * 0 anyway just for consistency and use 1:
  541. */
  542. dp->dl_stid.sc_stateid.si_generation = 1;
  543. INIT_LIST_HEAD(&dp->dl_perfile);
  544. INIT_LIST_HEAD(&dp->dl_perclnt);
  545. INIT_LIST_HEAD(&dp->dl_recall_lru);
  546. dp->dl_type = NFS4_OPEN_DELEGATE_READ;
  547. INIT_WORK(&dp->dl_recall.cb_work, nfsd4_run_cb_recall);
  548. return dp;
  549. out_dec:
  550. atomic_long_dec(&num_delegations);
  551. return NULL;
  552. }
  553. void
  554. nfs4_put_stid(struct nfs4_stid *s)
  555. {
  556. struct nfs4_file *fp = s->sc_file;
  557. struct nfs4_client *clp = s->sc_client;
  558. might_lock(&clp->cl_lock);
  559. if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
  560. wake_up_all(&close_wq);
  561. return;
  562. }
  563. idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
  564. spin_unlock(&clp->cl_lock);
  565. s->sc_free(s);
  566. if (fp)
  567. put_nfs4_file(fp);
  568. }
  569. static void nfs4_put_deleg_lease(struct nfs4_file *fp)
  570. {
  571. struct file *filp = NULL;
  572. spin_lock(&fp->fi_lock);
  573. if (fp->fi_deleg_file && atomic_dec_and_test(&fp->fi_delegees))
  574. swap(filp, fp->fi_deleg_file);
  575. spin_unlock(&fp->fi_lock);
  576. if (filp) {
  577. vfs_setlease(filp, F_UNLCK, NULL, NULL);
  578. fput(filp);
  579. }
  580. }
  581. static void unhash_stid(struct nfs4_stid *s)
  582. {
  583. s->sc_type = 0;
  584. }
  585. static void
  586. hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
  587. {
  588. lockdep_assert_held(&state_lock);
  589. lockdep_assert_held(&fp->fi_lock);
  590. atomic_inc(&dp->dl_stid.sc_count);
  591. dp->dl_stid.sc_type = NFS4_DELEG_STID;
  592. list_add(&dp->dl_perfile, &fp->fi_delegations);
  593. list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
  594. }
  595. static void
  596. unhash_delegation_locked(struct nfs4_delegation *dp)
  597. {
  598. struct nfs4_file *fp = dp->dl_stid.sc_file;
  599. lockdep_assert_held(&state_lock);
  600. dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
  601. /* Ensure that deleg break won't try to requeue it */
  602. ++dp->dl_time;
  603. spin_lock(&fp->fi_lock);
  604. list_del_init(&dp->dl_perclnt);
  605. list_del_init(&dp->dl_recall_lru);
  606. list_del_init(&dp->dl_perfile);
  607. spin_unlock(&fp->fi_lock);
  608. }
  609. static void destroy_delegation(struct nfs4_delegation *dp)
  610. {
  611. spin_lock(&state_lock);
  612. unhash_delegation_locked(dp);
  613. spin_unlock(&state_lock);
  614. nfs4_put_deleg_lease(dp->dl_stid.sc_file);
  615. nfs4_put_stid(&dp->dl_stid);
  616. }
  617. static void revoke_delegation(struct nfs4_delegation *dp)
  618. {
  619. struct nfs4_client *clp = dp->dl_stid.sc_client;
  620. WARN_ON(!list_empty(&dp->dl_recall_lru));
  621. nfs4_put_deleg_lease(dp->dl_stid.sc_file);
  622. if (clp->cl_minorversion == 0)
  623. nfs4_put_stid(&dp->dl_stid);
  624. else {
  625. dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
  626. spin_lock(&clp->cl_lock);
  627. list_add(&dp->dl_recall_lru, &clp->cl_revoked);
  628. spin_unlock(&clp->cl_lock);
  629. }
  630. }
  631. /*
  632. * SETCLIENTID state
  633. */
  634. static unsigned int clientid_hashval(u32 id)
  635. {
  636. return id & CLIENT_HASH_MASK;
  637. }
  638. static unsigned int clientstr_hashval(const char *name)
  639. {
  640. return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
  641. }
  642. /*
  643. * We store the NONE, READ, WRITE, and BOTH bits separately in the
  644. * st_{access,deny}_bmap field of the stateid, in order to track not
  645. * only what share bits are currently in force, but also what
  646. * combinations of share bits previous opens have used. This allows us
  647. * to enforce the recommendation of rfc 3530 14.2.19 that the server
  648. * return an error if the client attempt to downgrade to a combination
  649. * of share bits not explicable by closing some of its previous opens.
  650. *
  651. * XXX: This enforcement is actually incomplete, since we don't keep
  652. * track of access/deny bit combinations; so, e.g., we allow:
  653. *
  654. * OPEN allow read, deny write
  655. * OPEN allow both, deny none
  656. * DOWNGRADE allow read, deny none
  657. *
  658. * which we should reject.
  659. */
  660. static unsigned int
  661. bmap_to_share_mode(unsigned long bmap) {
  662. int i;
  663. unsigned int access = 0;
  664. for (i = 1; i < 4; i++) {
  665. if (test_bit(i, &bmap))
  666. access |= i;
  667. }
  668. return access;
  669. }
  670. /* set share access for a given stateid */
  671. static inline void
  672. set_access(u32 access, struct nfs4_ol_stateid *stp)
  673. {
  674. unsigned char mask = 1 << access;
  675. WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
  676. stp->st_access_bmap |= mask;
  677. }
  678. /* clear share access for a given stateid */
  679. static inline void
  680. clear_access(u32 access, struct nfs4_ol_stateid *stp)
  681. {
  682. unsigned char mask = 1 << access;
  683. WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
  684. stp->st_access_bmap &= ~mask;
  685. }
  686. /* test whether a given stateid has access */
  687. static inline bool
  688. test_access(u32 access, struct nfs4_ol_stateid *stp)
  689. {
  690. unsigned char mask = 1 << access;
  691. return (bool)(stp->st_access_bmap & mask);
  692. }
  693. /* set share deny for a given stateid */
  694. static inline void
  695. set_deny(u32 deny, struct nfs4_ol_stateid *stp)
  696. {
  697. unsigned char mask = 1 << deny;
  698. WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
  699. stp->st_deny_bmap |= mask;
  700. }
  701. /* clear share deny for a given stateid */
  702. static inline void
  703. clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
  704. {
  705. unsigned char mask = 1 << deny;
  706. WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
  707. stp->st_deny_bmap &= ~mask;
  708. }
  709. /* test whether a given stateid is denying specific access */
  710. static inline bool
  711. test_deny(u32 deny, struct nfs4_ol_stateid *stp)
  712. {
  713. unsigned char mask = 1 << deny;
  714. return (bool)(stp->st_deny_bmap & mask);
  715. }
  716. static int nfs4_access_to_omode(u32 access)
  717. {
  718. switch (access & NFS4_SHARE_ACCESS_BOTH) {
  719. case NFS4_SHARE_ACCESS_READ:
  720. return O_RDONLY;
  721. case NFS4_SHARE_ACCESS_WRITE:
  722. return O_WRONLY;
  723. case NFS4_SHARE_ACCESS_BOTH:
  724. return O_RDWR;
  725. }
  726. WARN_ON_ONCE(1);
  727. return O_RDONLY;
  728. }
  729. /*
  730. * A stateid that had a deny mode associated with it is being released
  731. * or downgraded. Recalculate the deny mode on the file.
  732. */
  733. static void
  734. recalculate_deny_mode(struct nfs4_file *fp)
  735. {
  736. struct nfs4_ol_stateid *stp;
  737. spin_lock(&fp->fi_lock);
  738. fp->fi_share_deny = 0;
  739. list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
  740. fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
  741. spin_unlock(&fp->fi_lock);
  742. }
  743. static void
  744. reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
  745. {
  746. int i;
  747. bool change = false;
  748. for (i = 1; i < 4; i++) {
  749. if ((i & deny) != i) {
  750. change = true;
  751. clear_deny(i, stp);
  752. }
  753. }
  754. /* Recalculate per-file deny mode if there was a change */
  755. if (change)
  756. recalculate_deny_mode(stp->st_stid.sc_file);
  757. }
  758. /* release all access and file references for a given stateid */
  759. static void
  760. release_all_access(struct nfs4_ol_stateid *stp)
  761. {
  762. int i;
  763. struct nfs4_file *fp = stp->st_stid.sc_file;
  764. if (fp && stp->st_deny_bmap != 0)
  765. recalculate_deny_mode(fp);
  766. for (i = 1; i < 4; i++) {
  767. if (test_access(i, stp))
  768. nfs4_file_put_access(stp->st_stid.sc_file, i);
  769. clear_access(i, stp);
  770. }
  771. }
  772. static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
  773. {
  774. struct nfs4_client *clp = sop->so_client;
  775. might_lock(&clp->cl_lock);
  776. if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
  777. return;
  778. sop->so_ops->so_unhash(sop);
  779. spin_unlock(&clp->cl_lock);
  780. kfree(sop->so_owner.data);
  781. sop->so_ops->so_free(sop);
  782. }
  783. static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
  784. {
  785. struct nfs4_file *fp = stp->st_stid.sc_file;
  786. lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
  787. spin_lock(&fp->fi_lock);
  788. list_del(&stp->st_perfile);
  789. spin_unlock(&fp->fi_lock);
  790. list_del(&stp->st_perstateowner);
  791. }
  792. static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
  793. {
  794. struct nfs4_ol_stateid *stp = openlockstateid(stid);
  795. release_all_access(stp);
  796. if (stp->st_stateowner)
  797. nfs4_put_stateowner(stp->st_stateowner);
  798. kmem_cache_free(stateid_slab, stid);
  799. }
  800. static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
  801. {
  802. struct nfs4_ol_stateid *stp = openlockstateid(stid);
  803. struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
  804. struct file *file;
  805. file = find_any_file(stp->st_stid.sc_file);
  806. if (file)
  807. filp_close(file, (fl_owner_t)lo);
  808. nfs4_free_ol_stateid(stid);
  809. }
  810. /*
  811. * Put the persistent reference to an already unhashed generic stateid, while
  812. * holding the cl_lock. If it's the last reference, then put it onto the
  813. * reaplist for later destruction.
  814. */
  815. static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
  816. struct list_head *reaplist)
  817. {
  818. struct nfs4_stid *s = &stp->st_stid;
  819. struct nfs4_client *clp = s->sc_client;
  820. lockdep_assert_held(&clp->cl_lock);
  821. WARN_ON_ONCE(!list_empty(&stp->st_locks));
  822. if (!atomic_dec_and_test(&s->sc_count)) {
  823. wake_up_all(&close_wq);
  824. return;
  825. }
  826. idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
  827. list_add(&stp->st_locks, reaplist);
  828. }
  829. static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
  830. {
  831. struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
  832. lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
  833. list_del_init(&stp->st_locks);
  834. unhash_ol_stateid(stp);
  835. unhash_stid(&stp->st_stid);
  836. }
  837. static void release_lock_stateid(struct nfs4_ol_stateid *stp)
  838. {
  839. struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
  840. spin_lock(&oo->oo_owner.so_client->cl_lock);
  841. unhash_lock_stateid(stp);
  842. spin_unlock(&oo->oo_owner.so_client->cl_lock);
  843. nfs4_put_stid(&stp->st_stid);
  844. }
  845. static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
  846. {
  847. struct nfs4_client *clp = lo->lo_owner.so_client;
  848. lockdep_assert_held(&clp->cl_lock);
  849. list_del_init(&lo->lo_owner.so_strhash);
  850. }
  851. /*
  852. * Free a list of generic stateids that were collected earlier after being
  853. * fully unhashed.
  854. */
  855. static void
  856. free_ol_stateid_reaplist(struct list_head *reaplist)
  857. {
  858. struct nfs4_ol_stateid *stp;
  859. struct nfs4_file *fp;
  860. might_sleep();
  861. while (!list_empty(reaplist)) {
  862. stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
  863. st_locks);
  864. list_del(&stp->st_locks);
  865. fp = stp->st_stid.sc_file;
  866. stp->st_stid.sc_free(&stp->st_stid);
  867. if (fp)
  868. put_nfs4_file(fp);
  869. }
  870. }
  871. static void release_lockowner(struct nfs4_lockowner *lo)
  872. {
  873. struct nfs4_client *clp = lo->lo_owner.so_client;
  874. struct nfs4_ol_stateid *stp;
  875. struct list_head reaplist;
  876. INIT_LIST_HEAD(&reaplist);
  877. spin_lock(&clp->cl_lock);
  878. unhash_lockowner_locked(lo);
  879. while (!list_empty(&lo->lo_owner.so_stateids)) {
  880. stp = list_first_entry(&lo->lo_owner.so_stateids,
  881. struct nfs4_ol_stateid, st_perstateowner);
  882. unhash_lock_stateid(stp);
  883. put_ol_stateid_locked(stp, &reaplist);
  884. }
  885. spin_unlock(&clp->cl_lock);
  886. free_ol_stateid_reaplist(&reaplist);
  887. nfs4_put_stateowner(&lo->lo_owner);
  888. }
  889. static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
  890. struct list_head *reaplist)
  891. {
  892. struct nfs4_ol_stateid *stp;
  893. while (!list_empty(&open_stp->st_locks)) {
  894. stp = list_entry(open_stp->st_locks.next,
  895. struct nfs4_ol_stateid, st_locks);
  896. unhash_lock_stateid(stp);
  897. put_ol_stateid_locked(stp, reaplist);
  898. }
  899. }
  900. static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
  901. struct list_head *reaplist)
  902. {
  903. lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
  904. unhash_ol_stateid(stp);
  905. release_open_stateid_locks(stp, reaplist);
  906. }
  907. static void release_open_stateid(struct nfs4_ol_stateid *stp)
  908. {
  909. LIST_HEAD(reaplist);
  910. spin_lock(&stp->st_stid.sc_client->cl_lock);
  911. unhash_open_stateid(stp, &reaplist);
  912. put_ol_stateid_locked(stp, &reaplist);
  913. spin_unlock(&stp->st_stid.sc_client->cl_lock);
  914. free_ol_stateid_reaplist(&reaplist);
  915. }
  916. static void unhash_openowner_locked(struct nfs4_openowner *oo)
  917. {
  918. struct nfs4_client *clp = oo->oo_owner.so_client;
  919. lockdep_assert_held(&clp->cl_lock);
  920. list_del_init(&oo->oo_owner.so_strhash);
  921. list_del_init(&oo->oo_perclient);
  922. }
  923. static void release_last_closed_stateid(struct nfs4_openowner *oo)
  924. {
  925. struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
  926. nfsd_net_id);
  927. struct nfs4_ol_stateid *s;
  928. spin_lock(&nn->client_lock);
  929. s = oo->oo_last_closed_stid;
  930. if (s) {
  931. list_del_init(&oo->oo_close_lru);
  932. oo->oo_last_closed_stid = NULL;
  933. }
  934. spin_unlock(&nn->client_lock);
  935. if (s)
  936. nfs4_put_stid(&s->st_stid);
  937. }
  938. static void release_openowner(struct nfs4_openowner *oo)
  939. {
  940. struct nfs4_ol_stateid *stp;
  941. struct nfs4_client *clp = oo->oo_owner.so_client;
  942. struct list_head reaplist;
  943. INIT_LIST_HEAD(&reaplist);
  944. spin_lock(&clp->cl_lock);
  945. unhash_openowner_locked(oo);
  946. while (!list_empty(&oo->oo_owner.so_stateids)) {
  947. stp = list_first_entry(&oo->oo_owner.so_stateids,
  948. struct nfs4_ol_stateid, st_perstateowner);
  949. unhash_open_stateid(stp, &reaplist);
  950. put_ol_stateid_locked(stp, &reaplist);
  951. }
  952. spin_unlock(&clp->cl_lock);
  953. free_ol_stateid_reaplist(&reaplist);
  954. release_last_closed_stateid(oo);
  955. nfs4_put_stateowner(&oo->oo_owner);
  956. }
  957. static inline int
  958. hash_sessionid(struct nfs4_sessionid *sessionid)
  959. {
  960. struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
  961. return sid->sequence % SESSION_HASH_SIZE;
  962. }
  963. #ifdef NFSD_DEBUG
  964. static inline void
  965. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  966. {
  967. u32 *ptr = (u32 *)(&sessionid->data[0]);
  968. dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
  969. }
  970. #else
  971. static inline void
  972. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  973. {
  974. }
  975. #endif
  976. /*
  977. * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
  978. * won't be used for replay.
  979. */
  980. void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
  981. {
  982. struct nfs4_stateowner *so = cstate->replay_owner;
  983. if (nfserr == nfserr_replay_me)
  984. return;
  985. if (!seqid_mutating_err(ntohl(nfserr))) {
  986. nfsd4_cstate_clear_replay(cstate);
  987. return;
  988. }
  989. if (!so)
  990. return;
  991. if (so->so_is_open_owner)
  992. release_last_closed_stateid(openowner(so));
  993. so->so_seqid++;
  994. return;
  995. }
  996. static void
  997. gen_sessionid(struct nfsd4_session *ses)
  998. {
  999. struct nfs4_client *clp = ses->se_client;
  1000. struct nfsd4_sessionid *sid;
  1001. sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
  1002. sid->clientid = clp->cl_clientid;
  1003. sid->sequence = current_sessionid++;
  1004. sid->reserved = 0;
  1005. }
  1006. /*
  1007. * The protocol defines ca_maxresponssize_cached to include the size of
  1008. * the rpc header, but all we need to cache is the data starting after
  1009. * the end of the initial SEQUENCE operation--the rest we regenerate
  1010. * each time. Therefore we can advertise a ca_maxresponssize_cached
  1011. * value that is the number of bytes in our cache plus a few additional
  1012. * bytes. In order to stay on the safe side, and not promise more than
  1013. * we can cache, those additional bytes must be the minimum possible: 24
  1014. * bytes of rpc header (xid through accept state, with AUTH_NULL
  1015. * verifier), 12 for the compound header (with zero-length tag), and 44
  1016. * for the SEQUENCE op response:
  1017. */
  1018. #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
  1019. static void
  1020. free_session_slots(struct nfsd4_session *ses)
  1021. {
  1022. int i;
  1023. for (i = 0; i < ses->se_fchannel.maxreqs; i++)
  1024. kfree(ses->se_slots[i]);
  1025. }
  1026. /*
  1027. * We don't actually need to cache the rpc and session headers, so we
  1028. * can allocate a little less for each slot:
  1029. */
  1030. static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
  1031. {
  1032. u32 size;
  1033. if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
  1034. size = 0;
  1035. else
  1036. size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
  1037. return size + sizeof(struct nfsd4_slot);
  1038. }
  1039. /*
  1040. * XXX: If we run out of reserved DRC memory we could (up to a point)
  1041. * re-negotiate active sessions and reduce their slot usage to make
  1042. * room for new connections. For now we just fail the create session.
  1043. */
  1044. static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
  1045. {
  1046. u32 slotsize = slot_bytes(ca);
  1047. u32 num = ca->maxreqs;
  1048. int avail;
  1049. spin_lock(&nfsd_drc_lock);
  1050. avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
  1051. nfsd_drc_max_mem - nfsd_drc_mem_used);
  1052. num = min_t(int, num, avail / slotsize);
  1053. nfsd_drc_mem_used += num * slotsize;
  1054. spin_unlock(&nfsd_drc_lock);
  1055. return num;
  1056. }
  1057. static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
  1058. {
  1059. int slotsize = slot_bytes(ca);
  1060. spin_lock(&nfsd_drc_lock);
  1061. nfsd_drc_mem_used -= slotsize * ca->maxreqs;
  1062. spin_unlock(&nfsd_drc_lock);
  1063. }
  1064. static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
  1065. struct nfsd4_channel_attrs *battrs)
  1066. {
  1067. int numslots = fattrs->maxreqs;
  1068. int slotsize = slot_bytes(fattrs);
  1069. struct nfsd4_session *new;
  1070. int mem, i;
  1071. BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
  1072. + sizeof(struct nfsd4_session) > PAGE_SIZE);
  1073. mem = numslots * sizeof(struct nfsd4_slot *);
  1074. new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
  1075. if (!new)
  1076. return NULL;
  1077. /* allocate each struct nfsd4_slot and data cache in one piece */
  1078. for (i = 0; i < numslots; i++) {
  1079. new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
  1080. if (!new->se_slots[i])
  1081. goto out_free;
  1082. }
  1083. memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
  1084. memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
  1085. return new;
  1086. out_free:
  1087. while (i--)
  1088. kfree(new->se_slots[i]);
  1089. kfree(new);
  1090. return NULL;
  1091. }
  1092. static void free_conn(struct nfsd4_conn *c)
  1093. {
  1094. svc_xprt_put(c->cn_xprt);
  1095. kfree(c);
  1096. }
  1097. static void nfsd4_conn_lost(struct svc_xpt_user *u)
  1098. {
  1099. struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
  1100. struct nfs4_client *clp = c->cn_session->se_client;
  1101. spin_lock(&clp->cl_lock);
  1102. if (!list_empty(&c->cn_persession)) {
  1103. list_del(&c->cn_persession);
  1104. free_conn(c);
  1105. }
  1106. nfsd4_probe_callback(clp);
  1107. spin_unlock(&clp->cl_lock);
  1108. }
  1109. static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
  1110. {
  1111. struct nfsd4_conn *conn;
  1112. conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
  1113. if (!conn)
  1114. return NULL;
  1115. svc_xprt_get(rqstp->rq_xprt);
  1116. conn->cn_xprt = rqstp->rq_xprt;
  1117. conn->cn_flags = flags;
  1118. INIT_LIST_HEAD(&conn->cn_xpt_user.list);
  1119. return conn;
  1120. }
  1121. static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  1122. {
  1123. conn->cn_session = ses;
  1124. list_add(&conn->cn_persession, &ses->se_conns);
  1125. }
  1126. static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  1127. {
  1128. struct nfs4_client *clp = ses->se_client;
  1129. spin_lock(&clp->cl_lock);
  1130. __nfsd4_hash_conn(conn, ses);
  1131. spin_unlock(&clp->cl_lock);
  1132. }
  1133. static int nfsd4_register_conn(struct nfsd4_conn *conn)
  1134. {
  1135. conn->cn_xpt_user.callback = nfsd4_conn_lost;
  1136. return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
  1137. }
  1138. static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
  1139. {
  1140. int ret;
  1141. nfsd4_hash_conn(conn, ses);
  1142. ret = nfsd4_register_conn(conn);
  1143. if (ret)
  1144. /* oops; xprt is already down: */
  1145. nfsd4_conn_lost(&conn->cn_xpt_user);
  1146. /* We may have gained or lost a callback channel: */
  1147. nfsd4_probe_callback_sync(ses->se_client);
  1148. }
  1149. static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
  1150. {
  1151. u32 dir = NFS4_CDFC4_FORE;
  1152. if (cses->flags & SESSION4_BACK_CHAN)
  1153. dir |= NFS4_CDFC4_BACK;
  1154. return alloc_conn(rqstp, dir);
  1155. }
  1156. /* must be called under client_lock */
  1157. static void nfsd4_del_conns(struct nfsd4_session *s)
  1158. {
  1159. struct nfs4_client *clp = s->se_client;
  1160. struct nfsd4_conn *c;
  1161. spin_lock(&clp->cl_lock);
  1162. while (!list_empty(&s->se_conns)) {
  1163. c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
  1164. list_del_init(&c->cn_persession);
  1165. spin_unlock(&clp->cl_lock);
  1166. unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
  1167. free_conn(c);
  1168. spin_lock(&clp->cl_lock);
  1169. }
  1170. spin_unlock(&clp->cl_lock);
  1171. }
  1172. static void __free_session(struct nfsd4_session *ses)
  1173. {
  1174. free_session_slots(ses);
  1175. kfree(ses);
  1176. }
  1177. static void free_session(struct nfsd4_session *ses)
  1178. {
  1179. nfsd4_del_conns(ses);
  1180. nfsd4_put_drc_mem(&ses->se_fchannel);
  1181. __free_session(ses);
  1182. }
  1183. static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
  1184. {
  1185. int idx;
  1186. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1187. new->se_client = clp;
  1188. gen_sessionid(new);
  1189. INIT_LIST_HEAD(&new->se_conns);
  1190. new->se_cb_seq_nr = 1;
  1191. new->se_flags = cses->flags;
  1192. new->se_cb_prog = cses->callback_prog;
  1193. new->se_cb_sec = cses->cb_sec;
  1194. atomic_set(&new->se_ref, 0);
  1195. idx = hash_sessionid(&new->se_sessionid);
  1196. list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
  1197. spin_lock(&clp->cl_lock);
  1198. list_add(&new->se_perclnt, &clp->cl_sessions);
  1199. spin_unlock(&clp->cl_lock);
  1200. if (cses->flags & SESSION4_BACK_CHAN) {
  1201. struct sockaddr *sa = svc_addr(rqstp);
  1202. /*
  1203. * This is a little silly; with sessions there's no real
  1204. * use for the callback address. Use the peer address
  1205. * as a reasonable default for now, but consider fixing
  1206. * the rpc client not to require an address in the
  1207. * future:
  1208. */
  1209. rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
  1210. clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
  1211. }
  1212. }
  1213. /* caller must hold client_lock */
  1214. static struct nfsd4_session *
  1215. __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
  1216. {
  1217. struct nfsd4_session *elem;
  1218. int idx;
  1219. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1220. lockdep_assert_held(&nn->client_lock);
  1221. dump_sessionid(__func__, sessionid);
  1222. idx = hash_sessionid(sessionid);
  1223. /* Search in the appropriate list */
  1224. list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
  1225. if (!memcmp(elem->se_sessionid.data, sessionid->data,
  1226. NFS4_MAX_SESSIONID_LEN)) {
  1227. return elem;
  1228. }
  1229. }
  1230. dprintk("%s: session not found\n", __func__);
  1231. return NULL;
  1232. }
  1233. static struct nfsd4_session *
  1234. find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
  1235. __be32 *ret)
  1236. {
  1237. struct nfsd4_session *session;
  1238. __be32 status = nfserr_badsession;
  1239. session = __find_in_sessionid_hashtbl(sessionid, net);
  1240. if (!session)
  1241. goto out;
  1242. status = nfsd4_get_session_locked(session);
  1243. if (status)
  1244. session = NULL;
  1245. out:
  1246. *ret = status;
  1247. return session;
  1248. }
  1249. /* caller must hold client_lock */
  1250. static void
  1251. unhash_session(struct nfsd4_session *ses)
  1252. {
  1253. struct nfs4_client *clp = ses->se_client;
  1254. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1255. lockdep_assert_held(&nn->client_lock);
  1256. list_del(&ses->se_hash);
  1257. spin_lock(&ses->se_client->cl_lock);
  1258. list_del(&ses->se_perclnt);
  1259. spin_unlock(&ses->se_client->cl_lock);
  1260. }
  1261. /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
  1262. static int
  1263. STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
  1264. {
  1265. if (clid->cl_boot == nn->boot_time)
  1266. return 0;
  1267. dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
  1268. clid->cl_boot, clid->cl_id, nn->boot_time);
  1269. return 1;
  1270. }
  1271. /*
  1272. * XXX Should we use a slab cache ?
  1273. * This type of memory management is somewhat inefficient, but we use it
  1274. * anyway since SETCLIENTID is not a common operation.
  1275. */
  1276. static struct nfs4_client *alloc_client(struct xdr_netobj name)
  1277. {
  1278. struct nfs4_client *clp;
  1279. int i;
  1280. clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
  1281. if (clp == NULL)
  1282. return NULL;
  1283. clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
  1284. if (clp->cl_name.data == NULL)
  1285. goto err_no_name;
  1286. clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
  1287. OWNER_HASH_SIZE, GFP_KERNEL);
  1288. if (!clp->cl_ownerstr_hashtbl)
  1289. goto err_no_hashtbl;
  1290. for (i = 0; i < OWNER_HASH_SIZE; i++)
  1291. INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
  1292. clp->cl_name.len = name.len;
  1293. INIT_LIST_HEAD(&clp->cl_sessions);
  1294. idr_init(&clp->cl_stateids);
  1295. atomic_set(&clp->cl_refcount, 0);
  1296. clp->cl_cb_state = NFSD4_CB_UNKNOWN;
  1297. INIT_LIST_HEAD(&clp->cl_idhash);
  1298. INIT_LIST_HEAD(&clp->cl_openowners);
  1299. INIT_LIST_HEAD(&clp->cl_delegations);
  1300. INIT_LIST_HEAD(&clp->cl_lru);
  1301. INIT_LIST_HEAD(&clp->cl_callbacks);
  1302. INIT_LIST_HEAD(&clp->cl_revoked);
  1303. spin_lock_init(&clp->cl_lock);
  1304. rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
  1305. return clp;
  1306. err_no_hashtbl:
  1307. kfree(clp->cl_name.data);
  1308. err_no_name:
  1309. kfree(clp);
  1310. return NULL;
  1311. }
  1312. static void
  1313. free_client(struct nfs4_client *clp)
  1314. {
  1315. while (!list_empty(&clp->cl_sessions)) {
  1316. struct nfsd4_session *ses;
  1317. ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
  1318. se_perclnt);
  1319. list_del(&ses->se_perclnt);
  1320. WARN_ON_ONCE(atomic_read(&ses->se_ref));
  1321. free_session(ses);
  1322. }
  1323. rpc_destroy_wait_queue(&clp->cl_cb_waitq);
  1324. free_svc_cred(&clp->cl_cred);
  1325. kfree(clp->cl_ownerstr_hashtbl);
  1326. kfree(clp->cl_name.data);
  1327. idr_destroy(&clp->cl_stateids);
  1328. kfree(clp);
  1329. }
  1330. /* must be called under the client_lock */
  1331. static void
  1332. unhash_client_locked(struct nfs4_client *clp)
  1333. {
  1334. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1335. struct nfsd4_session *ses;
  1336. lockdep_assert_held(&nn->client_lock);
  1337. /* Mark the client as expired! */
  1338. clp->cl_time = 0;
  1339. /* Make it invisible */
  1340. if (!list_empty(&clp->cl_idhash)) {
  1341. list_del_init(&clp->cl_idhash);
  1342. if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
  1343. rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
  1344. else
  1345. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  1346. }
  1347. list_del_init(&clp->cl_lru);
  1348. spin_lock(&clp->cl_lock);
  1349. list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
  1350. list_del_init(&ses->se_hash);
  1351. spin_unlock(&clp->cl_lock);
  1352. }
  1353. static void
  1354. unhash_client(struct nfs4_client *clp)
  1355. {
  1356. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1357. spin_lock(&nn->client_lock);
  1358. unhash_client_locked(clp);
  1359. spin_unlock(&nn->client_lock);
  1360. }
  1361. static __be32 mark_client_expired_locked(struct nfs4_client *clp)
  1362. {
  1363. if (atomic_read(&clp->cl_refcount))
  1364. return nfserr_jukebox;
  1365. unhash_client_locked(clp);
  1366. return nfs_ok;
  1367. }
  1368. static void
  1369. __destroy_client(struct nfs4_client *clp)
  1370. {
  1371. struct nfs4_openowner *oo;
  1372. struct nfs4_delegation *dp;
  1373. struct list_head reaplist;
  1374. INIT_LIST_HEAD(&reaplist);
  1375. spin_lock(&state_lock);
  1376. while (!list_empty(&clp->cl_delegations)) {
  1377. dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
  1378. unhash_delegation_locked(dp);
  1379. list_add(&dp->dl_recall_lru, &reaplist);
  1380. }
  1381. spin_unlock(&state_lock);
  1382. while (!list_empty(&reaplist)) {
  1383. dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
  1384. list_del_init(&dp->dl_recall_lru);
  1385. nfs4_put_deleg_lease(dp->dl_stid.sc_file);
  1386. nfs4_put_stid(&dp->dl_stid);
  1387. }
  1388. while (!list_empty(&clp->cl_revoked)) {
  1389. dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
  1390. list_del_init(&dp->dl_recall_lru);
  1391. nfs4_put_stid(&dp->dl_stid);
  1392. }
  1393. while (!list_empty(&clp->cl_openowners)) {
  1394. oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
  1395. nfs4_get_stateowner(&oo->oo_owner);
  1396. release_openowner(oo);
  1397. }
  1398. nfsd4_shutdown_callback(clp);
  1399. if (clp->cl_cb_conn.cb_xprt)
  1400. svc_xprt_put(clp->cl_cb_conn.cb_xprt);
  1401. free_client(clp);
  1402. }
  1403. static void
  1404. destroy_client(struct nfs4_client *clp)
  1405. {
  1406. unhash_client(clp);
  1407. __destroy_client(clp);
  1408. }
  1409. static void expire_client(struct nfs4_client *clp)
  1410. {
  1411. unhash_client(clp);
  1412. nfsd4_client_record_remove(clp);
  1413. __destroy_client(clp);
  1414. }
  1415. static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
  1416. {
  1417. memcpy(target->cl_verifier.data, source->data,
  1418. sizeof(target->cl_verifier.data));
  1419. }
  1420. static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
  1421. {
  1422. target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
  1423. target->cl_clientid.cl_id = source->cl_clientid.cl_id;
  1424. }
  1425. static int copy_cred(struct svc_cred *target, struct svc_cred *source)
  1426. {
  1427. if (source->cr_principal) {
  1428. target->cr_principal =
  1429. kstrdup(source->cr_principal, GFP_KERNEL);
  1430. if (target->cr_principal == NULL)
  1431. return -ENOMEM;
  1432. } else
  1433. target->cr_principal = NULL;
  1434. target->cr_flavor = source->cr_flavor;
  1435. target->cr_uid = source->cr_uid;
  1436. target->cr_gid = source->cr_gid;
  1437. target->cr_group_info = source->cr_group_info;
  1438. get_group_info(target->cr_group_info);
  1439. target->cr_gss_mech = source->cr_gss_mech;
  1440. if (source->cr_gss_mech)
  1441. gss_mech_get(source->cr_gss_mech);
  1442. return 0;
  1443. }
  1444. static long long
  1445. compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
  1446. {
  1447. long long res;
  1448. res = o1->len - o2->len;
  1449. if (res)
  1450. return res;
  1451. return (long long)memcmp(o1->data, o2->data, o1->len);
  1452. }
  1453. static int same_name(const char *n1, const char *n2)
  1454. {
  1455. return 0 == memcmp(n1, n2, HEXDIR_LEN);
  1456. }
  1457. static int
  1458. same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
  1459. {
  1460. return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
  1461. }
  1462. static int
  1463. same_clid(clientid_t *cl1, clientid_t *cl2)
  1464. {
  1465. return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
  1466. }
  1467. static bool groups_equal(struct group_info *g1, struct group_info *g2)
  1468. {
  1469. int i;
  1470. if (g1->ngroups != g2->ngroups)
  1471. return false;
  1472. for (i=0; i<g1->ngroups; i++)
  1473. if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
  1474. return false;
  1475. return true;
  1476. }
  1477. /*
  1478. * RFC 3530 language requires clid_inuse be returned when the
  1479. * "principal" associated with a requests differs from that previously
  1480. * used. We use uid, gid's, and gss principal string as our best
  1481. * approximation. We also don't want to allow non-gss use of a client
  1482. * established using gss: in theory cr_principal should catch that
  1483. * change, but in practice cr_principal can be null even in the gss case
  1484. * since gssd doesn't always pass down a principal string.
  1485. */
  1486. static bool is_gss_cred(struct svc_cred *cr)
  1487. {
  1488. /* Is cr_flavor one of the gss "pseudoflavors"?: */
  1489. return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
  1490. }
  1491. static bool
  1492. same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
  1493. {
  1494. if ((is_gss_cred(cr1) != is_gss_cred(cr2))
  1495. || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
  1496. || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
  1497. || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
  1498. return false;
  1499. if (cr1->cr_principal == cr2->cr_principal)
  1500. return true;
  1501. if (!cr1->cr_principal || !cr2->cr_principal)
  1502. return false;
  1503. return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
  1504. }
  1505. static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
  1506. {
  1507. struct svc_cred *cr = &rqstp->rq_cred;
  1508. u32 service;
  1509. if (!cr->cr_gss_mech)
  1510. return false;
  1511. service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
  1512. return service == RPC_GSS_SVC_INTEGRITY ||
  1513. service == RPC_GSS_SVC_PRIVACY;
  1514. }
  1515. static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
  1516. {
  1517. struct svc_cred *cr = &rqstp->rq_cred;
  1518. if (!cl->cl_mach_cred)
  1519. return true;
  1520. if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
  1521. return false;
  1522. if (!svc_rqst_integrity_protected(rqstp))
  1523. return false;
  1524. if (!cr->cr_principal)
  1525. return false;
  1526. return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
  1527. }
  1528. static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
  1529. {
  1530. __be32 verf[2];
  1531. /*
  1532. * This is opaque to client, so no need to byte-swap. Use
  1533. * __force to keep sparse happy
  1534. */
  1535. verf[0] = (__force __be32)get_seconds();
  1536. verf[1] = (__force __be32)nn->clientid_counter;
  1537. memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
  1538. }
  1539. static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
  1540. {
  1541. clp->cl_clientid.cl_boot = nn->boot_time;
  1542. clp->cl_clientid.cl_id = nn->clientid_counter++;
  1543. gen_confirm(clp, nn);
  1544. }
  1545. static struct nfs4_stid *
  1546. find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
  1547. {
  1548. struct nfs4_stid *ret;
  1549. ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
  1550. if (!ret || !ret->sc_type)
  1551. return NULL;
  1552. return ret;
  1553. }
  1554. static struct nfs4_stid *
  1555. find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
  1556. {
  1557. struct nfs4_stid *s;
  1558. spin_lock(&cl->cl_lock);
  1559. s = find_stateid_locked(cl, t);
  1560. if (s != NULL) {
  1561. if (typemask & s->sc_type)
  1562. atomic_inc(&s->sc_count);
  1563. else
  1564. s = NULL;
  1565. }
  1566. spin_unlock(&cl->cl_lock);
  1567. return s;
  1568. }
  1569. static struct nfs4_client *create_client(struct xdr_netobj name,
  1570. struct svc_rqst *rqstp, nfs4_verifier *verf)
  1571. {
  1572. struct nfs4_client *clp;
  1573. struct sockaddr *sa = svc_addr(rqstp);
  1574. int ret;
  1575. struct net *net = SVC_NET(rqstp);
  1576. clp = alloc_client(name);
  1577. if (clp == NULL)
  1578. return NULL;
  1579. ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
  1580. if (ret) {
  1581. free_client(clp);
  1582. return NULL;
  1583. }
  1584. INIT_WORK(&clp->cl_cb_null.cb_work, nfsd4_run_cb_null);
  1585. clp->cl_time = get_seconds();
  1586. clear_bit(0, &clp->cl_cb_slot_busy);
  1587. copy_verf(clp, verf);
  1588. rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
  1589. clp->cl_cb_session = NULL;
  1590. clp->net = net;
  1591. return clp;
  1592. }
  1593. static void
  1594. add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
  1595. {
  1596. struct rb_node **new = &(root->rb_node), *parent = NULL;
  1597. struct nfs4_client *clp;
  1598. while (*new) {
  1599. clp = rb_entry(*new, struct nfs4_client, cl_namenode);
  1600. parent = *new;
  1601. if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
  1602. new = &((*new)->rb_left);
  1603. else
  1604. new = &((*new)->rb_right);
  1605. }
  1606. rb_link_node(&new_clp->cl_namenode, parent, new);
  1607. rb_insert_color(&new_clp->cl_namenode, root);
  1608. }
  1609. static struct nfs4_client *
  1610. find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
  1611. {
  1612. long long cmp;
  1613. struct rb_node *node = root->rb_node;
  1614. struct nfs4_client *clp;
  1615. while (node) {
  1616. clp = rb_entry(node, struct nfs4_client, cl_namenode);
  1617. cmp = compare_blob(&clp->cl_name, name);
  1618. if (cmp > 0)
  1619. node = node->rb_left;
  1620. else if (cmp < 0)
  1621. node = node->rb_right;
  1622. else
  1623. return clp;
  1624. }
  1625. return NULL;
  1626. }
  1627. static void
  1628. add_to_unconfirmed(struct nfs4_client *clp)
  1629. {
  1630. unsigned int idhashval;
  1631. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1632. lockdep_assert_held(&nn->client_lock);
  1633. clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1634. add_clp_to_name_tree(clp, &nn->unconf_name_tree);
  1635. idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1636. list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
  1637. renew_client_locked(clp);
  1638. }
  1639. static void
  1640. move_to_confirmed(struct nfs4_client *clp)
  1641. {
  1642. unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1643. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1644. lockdep_assert_held(&nn->client_lock);
  1645. dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
  1646. list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
  1647. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  1648. add_clp_to_name_tree(clp, &nn->conf_name_tree);
  1649. set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1650. renew_client_locked(clp);
  1651. }
  1652. static struct nfs4_client *
  1653. find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
  1654. {
  1655. struct nfs4_client *clp;
  1656. unsigned int idhashval = clientid_hashval(clid->cl_id);
  1657. list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
  1658. if (same_clid(&clp->cl_clientid, clid)) {
  1659. if ((bool)clp->cl_minorversion != sessions)
  1660. return NULL;
  1661. renew_client_locked(clp);
  1662. return clp;
  1663. }
  1664. }
  1665. return NULL;
  1666. }
  1667. static struct nfs4_client *
  1668. find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1669. {
  1670. struct list_head *tbl = nn->conf_id_hashtbl;
  1671. lockdep_assert_held(&nn->client_lock);
  1672. return find_client_in_id_table(tbl, clid, sessions);
  1673. }
  1674. static struct nfs4_client *
  1675. find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1676. {
  1677. struct list_head *tbl = nn->unconf_id_hashtbl;
  1678. lockdep_assert_held(&nn->client_lock);
  1679. return find_client_in_id_table(tbl, clid, sessions);
  1680. }
  1681. static bool clp_used_exchangeid(struct nfs4_client *clp)
  1682. {
  1683. return clp->cl_exchange_flags != 0;
  1684. }
  1685. static struct nfs4_client *
  1686. find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1687. {
  1688. lockdep_assert_held(&nn->client_lock);
  1689. return find_clp_in_name_tree(name, &nn->conf_name_tree);
  1690. }
  1691. static struct nfs4_client *
  1692. find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1693. {
  1694. lockdep_assert_held(&nn->client_lock);
  1695. return find_clp_in_name_tree(name, &nn->unconf_name_tree);
  1696. }
  1697. static void
  1698. gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
  1699. {
  1700. struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
  1701. struct sockaddr *sa = svc_addr(rqstp);
  1702. u32 scopeid = rpc_get_scope_id(sa);
  1703. unsigned short expected_family;
  1704. /* Currently, we only support tcp and tcp6 for the callback channel */
  1705. if (se->se_callback_netid_len == 3 &&
  1706. !memcmp(se->se_callback_netid_val, "tcp", 3))
  1707. expected_family = AF_INET;
  1708. else if (se->se_callback_netid_len == 4 &&
  1709. !memcmp(se->se_callback_netid_val, "tcp6", 4))
  1710. expected_family = AF_INET6;
  1711. else
  1712. goto out_err;
  1713. conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
  1714. se->se_callback_addr_len,
  1715. (struct sockaddr *)&conn->cb_addr,
  1716. sizeof(conn->cb_addr));
  1717. if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
  1718. goto out_err;
  1719. if (conn->cb_addr.ss_family == AF_INET6)
  1720. ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
  1721. conn->cb_prog = se->se_callback_prog;
  1722. conn->cb_ident = se->se_callback_ident;
  1723. memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
  1724. return;
  1725. out_err:
  1726. conn->cb_addr.ss_family = AF_UNSPEC;
  1727. conn->cb_addrlen = 0;
  1728. dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
  1729. "will not receive delegations\n",
  1730. clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
  1731. return;
  1732. }
  1733. /*
  1734. * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
  1735. */
  1736. static void
  1737. nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
  1738. {
  1739. struct xdr_buf *buf = resp->xdr.buf;
  1740. struct nfsd4_slot *slot = resp->cstate.slot;
  1741. unsigned int base;
  1742. dprintk("--> %s slot %p\n", __func__, slot);
  1743. slot->sl_opcnt = resp->opcnt;
  1744. slot->sl_status = resp->cstate.status;
  1745. slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
  1746. if (nfsd4_not_cached(resp)) {
  1747. slot->sl_datalen = 0;
  1748. return;
  1749. }
  1750. base = resp->cstate.data_offset;
  1751. slot->sl_datalen = buf->len - base;
  1752. if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
  1753. WARN("%s: sessions DRC could not cache compound\n", __func__);
  1754. return;
  1755. }
  1756. /*
  1757. * Encode the replay sequence operation from the slot values.
  1758. * If cachethis is FALSE encode the uncached rep error on the next
  1759. * operation which sets resp->p and increments resp->opcnt for
  1760. * nfs4svc_encode_compoundres.
  1761. *
  1762. */
  1763. static __be32
  1764. nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
  1765. struct nfsd4_compoundres *resp)
  1766. {
  1767. struct nfsd4_op *op;
  1768. struct nfsd4_slot *slot = resp->cstate.slot;
  1769. /* Encode the replayed sequence operation */
  1770. op = &args->ops[resp->opcnt - 1];
  1771. nfsd4_encode_operation(resp, op);
  1772. /* Return nfserr_retry_uncached_rep in next operation. */
  1773. if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
  1774. op = &args->ops[resp->opcnt++];
  1775. op->status = nfserr_retry_uncached_rep;
  1776. nfsd4_encode_operation(resp, op);
  1777. }
  1778. return op->status;
  1779. }
  1780. /*
  1781. * The sequence operation is not cached because we can use the slot and
  1782. * session values.
  1783. */
  1784. static __be32
  1785. nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
  1786. struct nfsd4_sequence *seq)
  1787. {
  1788. struct nfsd4_slot *slot = resp->cstate.slot;
  1789. struct xdr_stream *xdr = &resp->xdr;
  1790. __be32 *p;
  1791. __be32 status;
  1792. dprintk("--> %s slot %p\n", __func__, slot);
  1793. status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
  1794. if (status)
  1795. return status;
  1796. p = xdr_reserve_space(xdr, slot->sl_datalen);
  1797. if (!p) {
  1798. WARN_ON_ONCE(1);
  1799. return nfserr_serverfault;
  1800. }
  1801. xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
  1802. xdr_commit_encode(xdr);
  1803. resp->opcnt = slot->sl_opcnt;
  1804. return slot->sl_status;
  1805. }
  1806. /*
  1807. * Set the exchange_id flags returned by the server.
  1808. */
  1809. static void
  1810. nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
  1811. {
  1812. /* pNFS is not supported */
  1813. new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
  1814. /* Referrals are supported, Migration is not. */
  1815. new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
  1816. /* set the wire flags to return to client. */
  1817. clid->flags = new->cl_exchange_flags;
  1818. }
  1819. static bool client_has_state(struct nfs4_client *clp)
  1820. {
  1821. /*
  1822. * Note clp->cl_openowners check isn't quite right: there's no
  1823. * need to count owners without stateid's.
  1824. *
  1825. * Also note we should probably be using this in 4.0 case too.
  1826. */
  1827. return !list_empty(&clp->cl_openowners)
  1828. || !list_empty(&clp->cl_delegations)
  1829. || !list_empty(&clp->cl_sessions);
  1830. }
  1831. __be32
  1832. nfsd4_exchange_id(struct svc_rqst *rqstp,
  1833. struct nfsd4_compound_state *cstate,
  1834. struct nfsd4_exchange_id *exid)
  1835. {
  1836. struct nfs4_client *conf, *new;
  1837. struct nfs4_client *unconf = NULL;
  1838. __be32 status;
  1839. char addr_str[INET6_ADDRSTRLEN];
  1840. nfs4_verifier verf = exid->verifier;
  1841. struct sockaddr *sa = svc_addr(rqstp);
  1842. bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
  1843. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1844. rpc_ntop(sa, addr_str, sizeof(addr_str));
  1845. dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
  1846. "ip_addr=%s flags %x, spa_how %d\n",
  1847. __func__, rqstp, exid, exid->clname.len, exid->clname.data,
  1848. addr_str, exid->flags, exid->spa_how);
  1849. if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
  1850. return nfserr_inval;
  1851. switch (exid->spa_how) {
  1852. case SP4_MACH_CRED:
  1853. if (!svc_rqst_integrity_protected(rqstp))
  1854. return nfserr_inval;
  1855. case SP4_NONE:
  1856. break;
  1857. default: /* checked by xdr code */
  1858. WARN_ON_ONCE(1);
  1859. case SP4_SSV:
  1860. return nfserr_encr_alg_unsupp;
  1861. }
  1862. new = create_client(exid->clname, rqstp, &verf);
  1863. if (new == NULL)
  1864. return nfserr_jukebox;
  1865. /* Cases below refer to rfc 5661 section 18.35.4: */
  1866. spin_lock(&nn->client_lock);
  1867. conf = find_confirmed_client_by_name(&exid->clname, nn);
  1868. if (conf) {
  1869. bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
  1870. bool verfs_match = same_verf(&verf, &conf->cl_verifier);
  1871. if (update) {
  1872. if (!clp_used_exchangeid(conf)) { /* buggy client */
  1873. status = nfserr_inval;
  1874. goto out;
  1875. }
  1876. if (!mach_creds_match(conf, rqstp)) {
  1877. status = nfserr_wrong_cred;
  1878. goto out;
  1879. }
  1880. if (!creds_match) { /* case 9 */
  1881. status = nfserr_perm;
  1882. goto out;
  1883. }
  1884. if (!verfs_match) { /* case 8 */
  1885. status = nfserr_not_same;
  1886. goto out;
  1887. }
  1888. /* case 6 */
  1889. exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1890. goto out_copy;
  1891. }
  1892. if (!creds_match) { /* case 3 */
  1893. if (client_has_state(conf)) {
  1894. status = nfserr_clid_inuse;
  1895. goto out;
  1896. }
  1897. goto out_new;
  1898. }
  1899. if (verfs_match) { /* case 2 */
  1900. conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
  1901. goto out_copy;
  1902. }
  1903. /* case 5, client reboot */
  1904. conf = NULL;
  1905. goto out_new;
  1906. }
  1907. if (update) { /* case 7 */
  1908. status = nfserr_noent;
  1909. goto out;
  1910. }
  1911. unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
  1912. if (unconf) /* case 4, possible retry or client restart */
  1913. unhash_client_locked(unconf);
  1914. /* case 1 (normal case) */
  1915. out_new:
  1916. if (conf) {
  1917. status = mark_client_expired_locked(conf);
  1918. if (status)
  1919. goto out;
  1920. }
  1921. new->cl_minorversion = cstate->minorversion;
  1922. new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
  1923. gen_clid(new, nn);
  1924. add_to_unconfirmed(new);
  1925. swap(new, conf);
  1926. out_copy:
  1927. exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
  1928. exid->clientid.cl_id = conf->cl_clientid.cl_id;
  1929. exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
  1930. nfsd4_set_ex_flags(conf, exid);
  1931. dprintk("nfsd4_exchange_id seqid %d flags %x\n",
  1932. conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
  1933. status = nfs_ok;
  1934. out:
  1935. spin_unlock(&nn->client_lock);
  1936. if (new)
  1937. expire_client(new);
  1938. if (unconf)
  1939. expire_client(unconf);
  1940. return status;
  1941. }
  1942. static __be32
  1943. check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
  1944. {
  1945. dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
  1946. slot_seqid);
  1947. /* The slot is in use, and no response has been sent. */
  1948. if (slot_inuse) {
  1949. if (seqid == slot_seqid)
  1950. return nfserr_jukebox;
  1951. else
  1952. return nfserr_seq_misordered;
  1953. }
  1954. /* Note unsigned 32-bit arithmetic handles wraparound: */
  1955. if (likely(seqid == slot_seqid + 1))
  1956. return nfs_ok;
  1957. if (seqid == slot_seqid)
  1958. return nfserr_replay_cache;
  1959. return nfserr_seq_misordered;
  1960. }
  1961. /*
  1962. * Cache the create session result into the create session single DRC
  1963. * slot cache by saving the xdr structure. sl_seqid has been set.
  1964. * Do this for solo or embedded create session operations.
  1965. */
  1966. static void
  1967. nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
  1968. struct nfsd4_clid_slot *slot, __be32 nfserr)
  1969. {
  1970. slot->sl_status = nfserr;
  1971. memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
  1972. }
  1973. static __be32
  1974. nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
  1975. struct nfsd4_clid_slot *slot)
  1976. {
  1977. memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
  1978. return slot->sl_status;
  1979. }
  1980. #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
  1981. 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
  1982. 1 + /* MIN tag is length with zero, only length */ \
  1983. 3 + /* version, opcount, opcode */ \
  1984. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1985. /* seqid, slotID, slotID, cache */ \
  1986. 4 ) * sizeof(__be32))
  1987. #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
  1988. 2 + /* verifier: AUTH_NULL, length 0 */\
  1989. 1 + /* status */ \
  1990. 1 + /* MIN tag is length with zero, only length */ \
  1991. 3 + /* opcount, opcode, opstatus*/ \
  1992. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  1993. /* seqid, slotID, slotID, slotID, status */ \
  1994. 5 ) * sizeof(__be32))
  1995. static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
  1996. {
  1997. u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
  1998. if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
  1999. return nfserr_toosmall;
  2000. if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
  2001. return nfserr_toosmall;
  2002. ca->headerpadsz = 0;
  2003. ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
  2004. ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
  2005. ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
  2006. ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
  2007. NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
  2008. ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
  2009. /*
  2010. * Note decreasing slot size below client's request may make it
  2011. * difficult for client to function correctly, whereas
  2012. * decreasing the number of slots will (just?) affect
  2013. * performance. When short on memory we therefore prefer to
  2014. * decrease number of slots instead of their size. Clients that
  2015. * request larger slots than they need will get poor results:
  2016. */
  2017. ca->maxreqs = nfsd4_get_drc_mem(ca);
  2018. if (!ca->maxreqs)
  2019. return nfserr_jukebox;
  2020. return nfs_ok;
  2021. }
  2022. #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
  2023. RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
  2024. #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
  2025. RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
  2026. static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
  2027. {
  2028. ca->headerpadsz = 0;
  2029. /*
  2030. * These RPC_MAX_HEADER macros are overkill, especially since we
  2031. * don't even do gss on the backchannel yet. But this is still
  2032. * less than 1k. Tighten up this estimate in the unlikely event
  2033. * it turns out to be a problem for some client:
  2034. */
  2035. if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
  2036. return nfserr_toosmall;
  2037. if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
  2038. return nfserr_toosmall;
  2039. ca->maxresp_cached = 0;
  2040. if (ca->maxops < 2)
  2041. return nfserr_toosmall;
  2042. return nfs_ok;
  2043. }
  2044. static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
  2045. {
  2046. switch (cbs->flavor) {
  2047. case RPC_AUTH_NULL:
  2048. case RPC_AUTH_UNIX:
  2049. return nfs_ok;
  2050. default:
  2051. /*
  2052. * GSS case: the spec doesn't allow us to return this
  2053. * error. But it also doesn't allow us not to support
  2054. * GSS.
  2055. * I'd rather this fail hard than return some error the
  2056. * client might think it can already handle:
  2057. */
  2058. return nfserr_encr_alg_unsupp;
  2059. }
  2060. }
  2061. __be32
  2062. nfsd4_create_session(struct svc_rqst *rqstp,
  2063. struct nfsd4_compound_state *cstate,
  2064. struct nfsd4_create_session *cr_ses)
  2065. {
  2066. struct sockaddr *sa = svc_addr(rqstp);
  2067. struct nfs4_client *conf, *unconf;
  2068. struct nfs4_client *old = NULL;
  2069. struct nfsd4_session *new;
  2070. struct nfsd4_conn *conn;
  2071. struct nfsd4_clid_slot *cs_slot = NULL;
  2072. __be32 status = 0;
  2073. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2074. if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
  2075. return nfserr_inval;
  2076. status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
  2077. if (status)
  2078. return status;
  2079. status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
  2080. if (status)
  2081. return status;
  2082. status = check_backchannel_attrs(&cr_ses->back_channel);
  2083. if (status)
  2084. goto out_release_drc_mem;
  2085. status = nfserr_jukebox;
  2086. new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
  2087. if (!new)
  2088. goto out_release_drc_mem;
  2089. conn = alloc_conn_from_crses(rqstp, cr_ses);
  2090. if (!conn)
  2091. goto out_free_session;
  2092. spin_lock(&nn->client_lock);
  2093. unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
  2094. conf = find_confirmed_client(&cr_ses->clientid, true, nn);
  2095. WARN_ON_ONCE(conf && unconf);
  2096. if (conf) {
  2097. status = nfserr_wrong_cred;
  2098. if (!mach_creds_match(conf, rqstp))
  2099. goto out_free_conn;
  2100. cs_slot = &conf->cl_cs_slot;
  2101. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  2102. if (status == nfserr_replay_cache) {
  2103. status = nfsd4_replay_create_session(cr_ses, cs_slot);
  2104. goto out_free_conn;
  2105. } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
  2106. status = nfserr_seq_misordered;
  2107. goto out_free_conn;
  2108. }
  2109. } else if (unconf) {
  2110. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
  2111. !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
  2112. status = nfserr_clid_inuse;
  2113. goto out_free_conn;
  2114. }
  2115. status = nfserr_wrong_cred;
  2116. if (!mach_creds_match(unconf, rqstp))
  2117. goto out_free_conn;
  2118. cs_slot = &unconf->cl_cs_slot;
  2119. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  2120. if (status) {
  2121. /* an unconfirmed replay returns misordered */
  2122. status = nfserr_seq_misordered;
  2123. goto out_free_conn;
  2124. }
  2125. old = find_confirmed_client_by_name(&unconf->cl_name, nn);
  2126. if (old) {
  2127. status = mark_client_expired_locked(old);
  2128. if (status) {
  2129. old = NULL;
  2130. goto out_free_conn;
  2131. }
  2132. }
  2133. move_to_confirmed(unconf);
  2134. conf = unconf;
  2135. } else {
  2136. status = nfserr_stale_clientid;
  2137. goto out_free_conn;
  2138. }
  2139. status = nfs_ok;
  2140. /*
  2141. * We do not support RDMA or persistent sessions
  2142. */
  2143. cr_ses->flags &= ~SESSION4_PERSIST;
  2144. cr_ses->flags &= ~SESSION4_RDMA;
  2145. init_session(rqstp, new, conf, cr_ses);
  2146. nfsd4_get_session_locked(new);
  2147. memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
  2148. NFS4_MAX_SESSIONID_LEN);
  2149. cs_slot->sl_seqid++;
  2150. cr_ses->seqid = cs_slot->sl_seqid;
  2151. /* cache solo and embedded create sessions under the client_lock */
  2152. nfsd4_cache_create_session(cr_ses, cs_slot, status);
  2153. spin_unlock(&nn->client_lock);
  2154. /* init connection and backchannel */
  2155. nfsd4_init_conn(rqstp, conn, new);
  2156. nfsd4_put_session(new);
  2157. if (old)
  2158. expire_client(old);
  2159. return status;
  2160. out_free_conn:
  2161. spin_unlock(&nn->client_lock);
  2162. free_conn(conn);
  2163. if (old)
  2164. expire_client(old);
  2165. out_free_session:
  2166. __free_session(new);
  2167. out_release_drc_mem:
  2168. nfsd4_put_drc_mem(&cr_ses->fore_channel);
  2169. return status;
  2170. }
  2171. static __be32 nfsd4_map_bcts_dir(u32 *dir)
  2172. {
  2173. switch (*dir) {
  2174. case NFS4_CDFC4_FORE:
  2175. case NFS4_CDFC4_BACK:
  2176. return nfs_ok;
  2177. case NFS4_CDFC4_FORE_OR_BOTH:
  2178. case NFS4_CDFC4_BACK_OR_BOTH:
  2179. *dir = NFS4_CDFC4_BOTH;
  2180. return nfs_ok;
  2181. };
  2182. return nfserr_inval;
  2183. }
  2184. __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
  2185. {
  2186. struct nfsd4_session *session = cstate->session;
  2187. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2188. __be32 status;
  2189. status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
  2190. if (status)
  2191. return status;
  2192. spin_lock(&nn->client_lock);
  2193. session->se_cb_prog = bc->bc_cb_program;
  2194. session->se_cb_sec = bc->bc_cb_sec;
  2195. spin_unlock(&nn->client_lock);
  2196. nfsd4_probe_callback(session->se_client);
  2197. return nfs_ok;
  2198. }
  2199. __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
  2200. struct nfsd4_compound_state *cstate,
  2201. struct nfsd4_bind_conn_to_session *bcts)
  2202. {
  2203. __be32 status;
  2204. struct nfsd4_conn *conn;
  2205. struct nfsd4_session *session;
  2206. struct net *net = SVC_NET(rqstp);
  2207. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2208. if (!nfsd4_last_compound_op(rqstp))
  2209. return nfserr_not_only_op;
  2210. spin_lock(&nn->client_lock);
  2211. session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
  2212. spin_unlock(&nn->client_lock);
  2213. if (!session)
  2214. goto out_no_session;
  2215. status = nfserr_wrong_cred;
  2216. if (!mach_creds_match(session->se_client, rqstp))
  2217. goto out;
  2218. status = nfsd4_map_bcts_dir(&bcts->dir);
  2219. if (status)
  2220. goto out;
  2221. conn = alloc_conn(rqstp, bcts->dir);
  2222. status = nfserr_jukebox;
  2223. if (!conn)
  2224. goto out;
  2225. nfsd4_init_conn(rqstp, conn, session);
  2226. status = nfs_ok;
  2227. out:
  2228. nfsd4_put_session(session);
  2229. out_no_session:
  2230. return status;
  2231. }
  2232. static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
  2233. {
  2234. if (!session)
  2235. return 0;
  2236. return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
  2237. }
  2238. __be32
  2239. nfsd4_destroy_session(struct svc_rqst *r,
  2240. struct nfsd4_compound_state *cstate,
  2241. struct nfsd4_destroy_session *sessionid)
  2242. {
  2243. struct nfsd4_session *ses;
  2244. __be32 status;
  2245. int ref_held_by_me = 0;
  2246. struct net *net = SVC_NET(r);
  2247. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2248. status = nfserr_not_only_op;
  2249. if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
  2250. if (!nfsd4_last_compound_op(r))
  2251. goto out;
  2252. ref_held_by_me++;
  2253. }
  2254. dump_sessionid(__func__, &sessionid->sessionid);
  2255. spin_lock(&nn->client_lock);
  2256. ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
  2257. if (!ses)
  2258. goto out_client_lock;
  2259. status = nfserr_wrong_cred;
  2260. if (!mach_creds_match(ses->se_client, r))
  2261. goto out_put_session;
  2262. status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
  2263. if (status)
  2264. goto out_put_session;
  2265. unhash_session(ses);
  2266. spin_unlock(&nn->client_lock);
  2267. nfsd4_probe_callback_sync(ses->se_client);
  2268. spin_lock(&nn->client_lock);
  2269. status = nfs_ok;
  2270. out_put_session:
  2271. nfsd4_put_session_locked(ses);
  2272. out_client_lock:
  2273. spin_unlock(&nn->client_lock);
  2274. out:
  2275. return status;
  2276. }
  2277. static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
  2278. {
  2279. struct nfsd4_conn *c;
  2280. list_for_each_entry(c, &s->se_conns, cn_persession) {
  2281. if (c->cn_xprt == xpt) {
  2282. return c;
  2283. }
  2284. }
  2285. return NULL;
  2286. }
  2287. static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
  2288. {
  2289. struct nfs4_client *clp = ses->se_client;
  2290. struct nfsd4_conn *c;
  2291. __be32 status = nfs_ok;
  2292. int ret;
  2293. spin_lock(&clp->cl_lock);
  2294. c = __nfsd4_find_conn(new->cn_xprt, ses);
  2295. if (c)
  2296. goto out_free;
  2297. status = nfserr_conn_not_bound_to_session;
  2298. if (clp->cl_mach_cred)
  2299. goto out_free;
  2300. __nfsd4_hash_conn(new, ses);
  2301. spin_unlock(&clp->cl_lock);
  2302. ret = nfsd4_register_conn(new);
  2303. if (ret)
  2304. /* oops; xprt is already down: */
  2305. nfsd4_conn_lost(&new->cn_xpt_user);
  2306. return nfs_ok;
  2307. out_free:
  2308. spin_unlock(&clp->cl_lock);
  2309. free_conn(new);
  2310. return status;
  2311. }
  2312. static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
  2313. {
  2314. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  2315. return args->opcnt > session->se_fchannel.maxops;
  2316. }
  2317. static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
  2318. struct nfsd4_session *session)
  2319. {
  2320. struct xdr_buf *xb = &rqstp->rq_arg;
  2321. return xb->len > session->se_fchannel.maxreq_sz;
  2322. }
  2323. __be32
  2324. nfsd4_sequence(struct svc_rqst *rqstp,
  2325. struct nfsd4_compound_state *cstate,
  2326. struct nfsd4_sequence *seq)
  2327. {
  2328. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2329. struct xdr_stream *xdr = &resp->xdr;
  2330. struct nfsd4_session *session;
  2331. struct nfs4_client *clp;
  2332. struct nfsd4_slot *slot;
  2333. struct nfsd4_conn *conn;
  2334. __be32 status;
  2335. int buflen;
  2336. struct net *net = SVC_NET(rqstp);
  2337. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2338. if (resp->opcnt != 1)
  2339. return nfserr_sequence_pos;
  2340. /*
  2341. * Will be either used or freed by nfsd4_sequence_check_conn
  2342. * below.
  2343. */
  2344. conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
  2345. if (!conn)
  2346. return nfserr_jukebox;
  2347. spin_lock(&nn->client_lock);
  2348. session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
  2349. if (!session)
  2350. goto out_no_session;
  2351. clp = session->se_client;
  2352. status = nfserr_too_many_ops;
  2353. if (nfsd4_session_too_many_ops(rqstp, session))
  2354. goto out_put_session;
  2355. status = nfserr_req_too_big;
  2356. if (nfsd4_request_too_big(rqstp, session))
  2357. goto out_put_session;
  2358. status = nfserr_badslot;
  2359. if (seq->slotid >= session->se_fchannel.maxreqs)
  2360. goto out_put_session;
  2361. slot = session->se_slots[seq->slotid];
  2362. dprintk("%s: slotid %d\n", __func__, seq->slotid);
  2363. /* We do not negotiate the number of slots yet, so set the
  2364. * maxslots to the session maxreqs which is used to encode
  2365. * sr_highest_slotid and the sr_target_slot id to maxslots */
  2366. seq->maxslots = session->se_fchannel.maxreqs;
  2367. status = check_slot_seqid(seq->seqid, slot->sl_seqid,
  2368. slot->sl_flags & NFSD4_SLOT_INUSE);
  2369. if (status == nfserr_replay_cache) {
  2370. status = nfserr_seq_misordered;
  2371. if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
  2372. goto out_put_session;
  2373. cstate->slot = slot;
  2374. cstate->session = session;
  2375. cstate->clp = clp;
  2376. /* Return the cached reply status and set cstate->status
  2377. * for nfsd4_proc_compound processing */
  2378. status = nfsd4_replay_cache_entry(resp, seq);
  2379. cstate->status = nfserr_replay_cache;
  2380. goto out;
  2381. }
  2382. if (status)
  2383. goto out_put_session;
  2384. status = nfsd4_sequence_check_conn(conn, session);
  2385. conn = NULL;
  2386. if (status)
  2387. goto out_put_session;
  2388. buflen = (seq->cachethis) ?
  2389. session->se_fchannel.maxresp_cached :
  2390. session->se_fchannel.maxresp_sz;
  2391. status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
  2392. nfserr_rep_too_big;
  2393. if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
  2394. goto out_put_session;
  2395. svc_reserve(rqstp, buflen);
  2396. status = nfs_ok;
  2397. /* Success! bump slot seqid */
  2398. slot->sl_seqid = seq->seqid;
  2399. slot->sl_flags |= NFSD4_SLOT_INUSE;
  2400. if (seq->cachethis)
  2401. slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
  2402. else
  2403. slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
  2404. cstate->slot = slot;
  2405. cstate->session = session;
  2406. cstate->clp = clp;
  2407. out:
  2408. switch (clp->cl_cb_state) {
  2409. case NFSD4_CB_DOWN:
  2410. seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
  2411. break;
  2412. case NFSD4_CB_FAULT:
  2413. seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
  2414. break;
  2415. default:
  2416. seq->status_flags = 0;
  2417. }
  2418. if (!list_empty(&clp->cl_revoked))
  2419. seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
  2420. out_no_session:
  2421. if (conn)
  2422. free_conn(conn);
  2423. spin_unlock(&nn->client_lock);
  2424. return status;
  2425. out_put_session:
  2426. nfsd4_put_session_locked(session);
  2427. goto out_no_session;
  2428. }
  2429. void
  2430. nfsd4_sequence_done(struct nfsd4_compoundres *resp)
  2431. {
  2432. struct nfsd4_compound_state *cs = &resp->cstate;
  2433. if (nfsd4_has_session(cs)) {
  2434. if (cs->status != nfserr_replay_cache) {
  2435. nfsd4_store_cache_entry(resp);
  2436. cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
  2437. }
  2438. /* Drop session reference that was taken in nfsd4_sequence() */
  2439. nfsd4_put_session(cs->session);
  2440. } else if (cs->clp)
  2441. put_client_renew(cs->clp);
  2442. }
  2443. __be32
  2444. nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
  2445. {
  2446. struct nfs4_client *conf, *unconf;
  2447. struct nfs4_client *clp = NULL;
  2448. __be32 status = 0;
  2449. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2450. spin_lock(&nn->client_lock);
  2451. unconf = find_unconfirmed_client(&dc->clientid, true, nn);
  2452. conf = find_confirmed_client(&dc->clientid, true, nn);
  2453. WARN_ON_ONCE(conf && unconf);
  2454. if (conf) {
  2455. if (client_has_state(conf)) {
  2456. status = nfserr_clientid_busy;
  2457. goto out;
  2458. }
  2459. status = mark_client_expired_locked(conf);
  2460. if (status)
  2461. goto out;
  2462. clp = conf;
  2463. } else if (unconf)
  2464. clp = unconf;
  2465. else {
  2466. status = nfserr_stale_clientid;
  2467. goto out;
  2468. }
  2469. if (!mach_creds_match(clp, rqstp)) {
  2470. clp = NULL;
  2471. status = nfserr_wrong_cred;
  2472. goto out;
  2473. }
  2474. unhash_client_locked(clp);
  2475. out:
  2476. spin_unlock(&nn->client_lock);
  2477. if (clp)
  2478. expire_client(clp);
  2479. return status;
  2480. }
  2481. __be32
  2482. nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
  2483. {
  2484. __be32 status = 0;
  2485. if (rc->rca_one_fs) {
  2486. if (!cstate->current_fh.fh_dentry)
  2487. return nfserr_nofilehandle;
  2488. /*
  2489. * We don't take advantage of the rca_one_fs case.
  2490. * That's OK, it's optional, we can safely ignore it.
  2491. */
  2492. return nfs_ok;
  2493. }
  2494. status = nfserr_complete_already;
  2495. if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  2496. &cstate->session->se_client->cl_flags))
  2497. goto out;
  2498. status = nfserr_stale_clientid;
  2499. if (is_client_expired(cstate->session->se_client))
  2500. /*
  2501. * The following error isn't really legal.
  2502. * But we only get here if the client just explicitly
  2503. * destroyed the client. Surely it no longer cares what
  2504. * error it gets back on an operation for the dead
  2505. * client.
  2506. */
  2507. goto out;
  2508. status = nfs_ok;
  2509. nfsd4_client_record_create(cstate->session->se_client);
  2510. out:
  2511. return status;
  2512. }
  2513. __be32
  2514. nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2515. struct nfsd4_setclientid *setclid)
  2516. {
  2517. struct xdr_netobj clname = setclid->se_name;
  2518. nfs4_verifier clverifier = setclid->se_verf;
  2519. struct nfs4_client *conf, *new;
  2520. struct nfs4_client *unconf = NULL;
  2521. __be32 status;
  2522. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2523. new = create_client(clname, rqstp, &clverifier);
  2524. if (new == NULL)
  2525. return nfserr_jukebox;
  2526. /* Cases below refer to rfc 3530 section 14.2.33: */
  2527. spin_lock(&nn->client_lock);
  2528. conf = find_confirmed_client_by_name(&clname, nn);
  2529. if (conf) {
  2530. /* case 0: */
  2531. status = nfserr_clid_inuse;
  2532. if (clp_used_exchangeid(conf))
  2533. goto out;
  2534. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
  2535. char addr_str[INET6_ADDRSTRLEN];
  2536. rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
  2537. sizeof(addr_str));
  2538. dprintk("NFSD: setclientid: string in use by client "
  2539. "at %s\n", addr_str);
  2540. goto out;
  2541. }
  2542. }
  2543. unconf = find_unconfirmed_client_by_name(&clname, nn);
  2544. if (unconf)
  2545. unhash_client_locked(unconf);
  2546. if (conf && same_verf(&conf->cl_verifier, &clverifier))
  2547. /* case 1: probable callback update */
  2548. copy_clid(new, conf);
  2549. else /* case 4 (new client) or cases 2, 3 (client reboot): */
  2550. gen_clid(new, nn);
  2551. new->cl_minorversion = 0;
  2552. gen_callback(new, setclid, rqstp);
  2553. add_to_unconfirmed(new);
  2554. setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
  2555. setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
  2556. memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
  2557. new = NULL;
  2558. status = nfs_ok;
  2559. out:
  2560. spin_unlock(&nn->client_lock);
  2561. if (new)
  2562. free_client(new);
  2563. if (unconf)
  2564. expire_client(unconf);
  2565. return status;
  2566. }
  2567. __be32
  2568. nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
  2569. struct nfsd4_compound_state *cstate,
  2570. struct nfsd4_setclientid_confirm *setclientid_confirm)
  2571. {
  2572. struct nfs4_client *conf, *unconf;
  2573. struct nfs4_client *old = NULL;
  2574. nfs4_verifier confirm = setclientid_confirm->sc_confirm;
  2575. clientid_t * clid = &setclientid_confirm->sc_clientid;
  2576. __be32 status;
  2577. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2578. if (STALE_CLIENTID(clid, nn))
  2579. return nfserr_stale_clientid;
  2580. spin_lock(&nn->client_lock);
  2581. conf = find_confirmed_client(clid, false, nn);
  2582. unconf = find_unconfirmed_client(clid, false, nn);
  2583. /*
  2584. * We try hard to give out unique clientid's, so if we get an
  2585. * attempt to confirm the same clientid with a different cred,
  2586. * there's a bug somewhere. Let's charitably assume it's our
  2587. * bug.
  2588. */
  2589. status = nfserr_serverfault;
  2590. if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
  2591. goto out;
  2592. if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
  2593. goto out;
  2594. /* cases below refer to rfc 3530 section 14.2.34: */
  2595. if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
  2596. if (conf && !unconf) /* case 2: probable retransmit */
  2597. status = nfs_ok;
  2598. else /* case 4: client hasn't noticed we rebooted yet? */
  2599. status = nfserr_stale_clientid;
  2600. goto out;
  2601. }
  2602. status = nfs_ok;
  2603. if (conf) { /* case 1: callback update */
  2604. old = unconf;
  2605. unhash_client_locked(old);
  2606. nfsd4_change_callback(conf, &unconf->cl_cb_conn);
  2607. } else { /* case 3: normal case; new or rebooted client */
  2608. old = find_confirmed_client_by_name(&unconf->cl_name, nn);
  2609. if (old) {
  2610. status = mark_client_expired_locked(old);
  2611. if (status) {
  2612. old = NULL;
  2613. goto out;
  2614. }
  2615. }
  2616. move_to_confirmed(unconf);
  2617. conf = unconf;
  2618. }
  2619. get_client_locked(conf);
  2620. spin_unlock(&nn->client_lock);
  2621. nfsd4_probe_callback(conf);
  2622. spin_lock(&nn->client_lock);
  2623. put_client_renew_locked(conf);
  2624. out:
  2625. spin_unlock(&nn->client_lock);
  2626. if (old)
  2627. expire_client(old);
  2628. return status;
  2629. }
  2630. static struct nfs4_file *nfsd4_alloc_file(void)
  2631. {
  2632. return kmem_cache_alloc(file_slab, GFP_KERNEL);
  2633. }
  2634. /* OPEN Share state helper functions */
  2635. static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
  2636. {
  2637. unsigned int hashval = file_hashval(fh);
  2638. lockdep_assert_held(&state_lock);
  2639. atomic_set(&fp->fi_ref, 1);
  2640. spin_lock_init(&fp->fi_lock);
  2641. INIT_LIST_HEAD(&fp->fi_stateids);
  2642. INIT_LIST_HEAD(&fp->fi_delegations);
  2643. fh_copy_shallow(&fp->fi_fhandle, fh);
  2644. fp->fi_deleg_file = NULL;
  2645. fp->fi_had_conflict = false;
  2646. fp->fi_share_deny = 0;
  2647. memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
  2648. memset(fp->fi_access, 0, sizeof(fp->fi_access));
  2649. hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
  2650. }
  2651. void
  2652. nfsd4_free_slabs(void)
  2653. {
  2654. kmem_cache_destroy(openowner_slab);
  2655. kmem_cache_destroy(lockowner_slab);
  2656. kmem_cache_destroy(file_slab);
  2657. kmem_cache_destroy(stateid_slab);
  2658. kmem_cache_destroy(deleg_slab);
  2659. }
  2660. int
  2661. nfsd4_init_slabs(void)
  2662. {
  2663. openowner_slab = kmem_cache_create("nfsd4_openowners",
  2664. sizeof(struct nfs4_openowner), 0, 0, NULL);
  2665. if (openowner_slab == NULL)
  2666. goto out;
  2667. lockowner_slab = kmem_cache_create("nfsd4_lockowners",
  2668. sizeof(struct nfs4_lockowner), 0, 0, NULL);
  2669. if (lockowner_slab == NULL)
  2670. goto out_free_openowner_slab;
  2671. file_slab = kmem_cache_create("nfsd4_files",
  2672. sizeof(struct nfs4_file), 0, 0, NULL);
  2673. if (file_slab == NULL)
  2674. goto out_free_lockowner_slab;
  2675. stateid_slab = kmem_cache_create("nfsd4_stateids",
  2676. sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
  2677. if (stateid_slab == NULL)
  2678. goto out_free_file_slab;
  2679. deleg_slab = kmem_cache_create("nfsd4_delegations",
  2680. sizeof(struct nfs4_delegation), 0, 0, NULL);
  2681. if (deleg_slab == NULL)
  2682. goto out_free_stateid_slab;
  2683. return 0;
  2684. out_free_stateid_slab:
  2685. kmem_cache_destroy(stateid_slab);
  2686. out_free_file_slab:
  2687. kmem_cache_destroy(file_slab);
  2688. out_free_lockowner_slab:
  2689. kmem_cache_destroy(lockowner_slab);
  2690. out_free_openowner_slab:
  2691. kmem_cache_destroy(openowner_slab);
  2692. out:
  2693. dprintk("nfsd4: out of memory while initializing nfsv4\n");
  2694. return -ENOMEM;
  2695. }
  2696. static void init_nfs4_replay(struct nfs4_replay *rp)
  2697. {
  2698. rp->rp_status = nfserr_serverfault;
  2699. rp->rp_buflen = 0;
  2700. rp->rp_buf = rp->rp_ibuf;
  2701. mutex_init(&rp->rp_mutex);
  2702. }
  2703. static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
  2704. struct nfs4_stateowner *so)
  2705. {
  2706. if (!nfsd4_has_session(cstate)) {
  2707. mutex_lock(&so->so_replay.rp_mutex);
  2708. cstate->replay_owner = nfs4_get_stateowner(so);
  2709. }
  2710. }
  2711. void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
  2712. {
  2713. struct nfs4_stateowner *so = cstate->replay_owner;
  2714. if (so != NULL) {
  2715. cstate->replay_owner = NULL;
  2716. mutex_unlock(&so->so_replay.rp_mutex);
  2717. nfs4_put_stateowner(so);
  2718. }
  2719. }
  2720. static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
  2721. {
  2722. struct nfs4_stateowner *sop;
  2723. sop = kmem_cache_alloc(slab, GFP_KERNEL);
  2724. if (!sop)
  2725. return NULL;
  2726. sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
  2727. if (!sop->so_owner.data) {
  2728. kmem_cache_free(slab, sop);
  2729. return NULL;
  2730. }
  2731. sop->so_owner.len = owner->len;
  2732. INIT_LIST_HEAD(&sop->so_stateids);
  2733. sop->so_client = clp;
  2734. init_nfs4_replay(&sop->so_replay);
  2735. atomic_set(&sop->so_count, 1);
  2736. return sop;
  2737. }
  2738. static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
  2739. {
  2740. lockdep_assert_held(&clp->cl_lock);
  2741. list_add(&oo->oo_owner.so_strhash,
  2742. &clp->cl_ownerstr_hashtbl[strhashval]);
  2743. list_add(&oo->oo_perclient, &clp->cl_openowners);
  2744. }
  2745. static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
  2746. {
  2747. unhash_openowner_locked(openowner(so));
  2748. }
  2749. static void nfs4_free_openowner(struct nfs4_stateowner *so)
  2750. {
  2751. struct nfs4_openowner *oo = openowner(so);
  2752. kmem_cache_free(openowner_slab, oo);
  2753. }
  2754. static const struct nfs4_stateowner_operations openowner_ops = {
  2755. .so_unhash = nfs4_unhash_openowner,
  2756. .so_free = nfs4_free_openowner,
  2757. };
  2758. static struct nfs4_openowner *
  2759. alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
  2760. struct nfsd4_compound_state *cstate)
  2761. {
  2762. struct nfs4_client *clp = cstate->clp;
  2763. struct nfs4_openowner *oo, *ret;
  2764. oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
  2765. if (!oo)
  2766. return NULL;
  2767. oo->oo_owner.so_ops = &openowner_ops;
  2768. oo->oo_owner.so_is_open_owner = 1;
  2769. oo->oo_owner.so_seqid = open->op_seqid;
  2770. oo->oo_flags = 0;
  2771. if (nfsd4_has_session(cstate))
  2772. oo->oo_flags |= NFS4_OO_CONFIRMED;
  2773. oo->oo_time = 0;
  2774. oo->oo_last_closed_stid = NULL;
  2775. INIT_LIST_HEAD(&oo->oo_close_lru);
  2776. spin_lock(&clp->cl_lock);
  2777. ret = find_openstateowner_str_locked(strhashval, open, clp);
  2778. if (ret == NULL) {
  2779. hash_openowner(oo, clp, strhashval);
  2780. ret = oo;
  2781. } else
  2782. nfs4_free_openowner(&oo->oo_owner);
  2783. spin_unlock(&clp->cl_lock);
  2784. return oo;
  2785. }
  2786. static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
  2787. struct nfs4_openowner *oo = open->op_openowner;
  2788. atomic_inc(&stp->st_stid.sc_count);
  2789. stp->st_stid.sc_type = NFS4_OPEN_STID;
  2790. INIT_LIST_HEAD(&stp->st_locks);
  2791. stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
  2792. get_nfs4_file(fp);
  2793. stp->st_stid.sc_file = fp;
  2794. stp->st_access_bmap = 0;
  2795. stp->st_deny_bmap = 0;
  2796. stp->st_openstp = NULL;
  2797. spin_lock(&oo->oo_owner.so_client->cl_lock);
  2798. list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
  2799. spin_lock(&fp->fi_lock);
  2800. list_add(&stp->st_perfile, &fp->fi_stateids);
  2801. spin_unlock(&fp->fi_lock);
  2802. spin_unlock(&oo->oo_owner.so_client->cl_lock);
  2803. }
  2804. /*
  2805. * In the 4.0 case we need to keep the owners around a little while to handle
  2806. * CLOSE replay. We still do need to release any file access that is held by
  2807. * them before returning however.
  2808. */
  2809. static void
  2810. move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
  2811. {
  2812. struct nfs4_ol_stateid *last;
  2813. struct nfs4_openowner *oo = openowner(s->st_stateowner);
  2814. struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
  2815. nfsd_net_id);
  2816. dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
  2817. /*
  2818. * We know that we hold one reference via nfsd4_close, and another
  2819. * "persistent" reference for the client. If the refcount is higher
  2820. * than 2, then there are still calls in progress that are using this
  2821. * stateid. We can't put the sc_file reference until they are finished.
  2822. * Wait for the refcount to drop to 2. Since it has been unhashed,
  2823. * there should be no danger of the refcount going back up again at
  2824. * this point.
  2825. */
  2826. wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
  2827. release_all_access(s);
  2828. if (s->st_stid.sc_file) {
  2829. put_nfs4_file(s->st_stid.sc_file);
  2830. s->st_stid.sc_file = NULL;
  2831. }
  2832. spin_lock(&nn->client_lock);
  2833. last = oo->oo_last_closed_stid;
  2834. oo->oo_last_closed_stid = s;
  2835. list_move_tail(&oo->oo_close_lru, &nn->close_lru);
  2836. oo->oo_time = get_seconds();
  2837. spin_unlock(&nn->client_lock);
  2838. if (last)
  2839. nfs4_put_stid(&last->st_stid);
  2840. }
  2841. /* search file_hashtbl[] for file */
  2842. static struct nfs4_file *
  2843. find_file_locked(struct knfsd_fh *fh)
  2844. {
  2845. unsigned int hashval = file_hashval(fh);
  2846. struct nfs4_file *fp;
  2847. lockdep_assert_held(&state_lock);
  2848. hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
  2849. if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
  2850. get_nfs4_file(fp);
  2851. return fp;
  2852. }
  2853. }
  2854. return NULL;
  2855. }
  2856. static struct nfs4_file *
  2857. find_file(struct knfsd_fh *fh)
  2858. {
  2859. struct nfs4_file *fp;
  2860. spin_lock(&state_lock);
  2861. fp = find_file_locked(fh);
  2862. spin_unlock(&state_lock);
  2863. return fp;
  2864. }
  2865. static struct nfs4_file *
  2866. find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
  2867. {
  2868. struct nfs4_file *fp;
  2869. spin_lock(&state_lock);
  2870. fp = find_file_locked(fh);
  2871. if (fp == NULL) {
  2872. nfsd4_init_file(new, fh);
  2873. fp = new;
  2874. }
  2875. spin_unlock(&state_lock);
  2876. return fp;
  2877. }
  2878. /*
  2879. * Called to check deny when READ with all zero stateid or
  2880. * WRITE with all zero or all one stateid
  2881. */
  2882. static __be32
  2883. nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
  2884. {
  2885. struct nfs4_file *fp;
  2886. __be32 ret = nfs_ok;
  2887. fp = find_file(&current_fh->fh_handle);
  2888. if (!fp)
  2889. return ret;
  2890. /* Check for conflicting share reservations */
  2891. spin_lock(&fp->fi_lock);
  2892. if (fp->fi_share_deny & deny_type)
  2893. ret = nfserr_locked;
  2894. spin_unlock(&fp->fi_lock);
  2895. put_nfs4_file(fp);
  2896. return ret;
  2897. }
  2898. void nfsd4_prepare_cb_recall(struct nfs4_delegation *dp)
  2899. {
  2900. struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
  2901. nfsd_net_id);
  2902. block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
  2903. /*
  2904. * We can't do this in nfsd_break_deleg_cb because it is
  2905. * already holding inode->i_lock.
  2906. *
  2907. * If the dl_time != 0, then we know that it has already been
  2908. * queued for a lease break. Don't queue it again.
  2909. */
  2910. spin_lock(&state_lock);
  2911. if (dp->dl_time == 0) {
  2912. dp->dl_time = get_seconds();
  2913. list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
  2914. }
  2915. spin_unlock(&state_lock);
  2916. }
  2917. static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
  2918. {
  2919. /*
  2920. * We're assuming the state code never drops its reference
  2921. * without first removing the lease. Since we're in this lease
  2922. * callback (and since the lease code is serialized by the kernel
  2923. * lock) we know the server hasn't removed the lease yet, we know
  2924. * it's safe to take a reference.
  2925. */
  2926. atomic_inc(&dp->dl_stid.sc_count);
  2927. nfsd4_cb_recall(dp);
  2928. }
  2929. /* Called from break_lease() with i_lock held. */
  2930. static void nfsd_break_deleg_cb(struct file_lock *fl)
  2931. {
  2932. struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
  2933. struct nfs4_delegation *dp;
  2934. if (!fp) {
  2935. WARN(1, "(%p)->fl_owner NULL\n", fl);
  2936. return;
  2937. }
  2938. if (fp->fi_had_conflict) {
  2939. WARN(1, "duplicate break on %p\n", fp);
  2940. return;
  2941. }
  2942. /*
  2943. * We don't want the locks code to timeout the lease for us;
  2944. * we'll remove it ourself if a delegation isn't returned
  2945. * in time:
  2946. */
  2947. fl->fl_break_time = 0;
  2948. spin_lock(&fp->fi_lock);
  2949. fp->fi_had_conflict = true;
  2950. /*
  2951. * If there are no delegations on the list, then we can't count on this
  2952. * lease ever being cleaned up. Set the fl_break_time to jiffies so that
  2953. * time_out_leases will do it ASAP. The fact that fi_had_conflict is now
  2954. * true should keep any new delegations from being hashed.
  2955. */
  2956. if (list_empty(&fp->fi_delegations))
  2957. fl->fl_break_time = jiffies;
  2958. else
  2959. list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
  2960. nfsd_break_one_deleg(dp);
  2961. spin_unlock(&fp->fi_lock);
  2962. }
  2963. static
  2964. int nfsd_change_deleg_cb(struct file_lock **onlist, int arg)
  2965. {
  2966. if (arg & F_UNLCK)
  2967. return lease_modify(onlist, arg);
  2968. else
  2969. return -EAGAIN;
  2970. }
  2971. static const struct lock_manager_operations nfsd_lease_mng_ops = {
  2972. .lm_break = nfsd_break_deleg_cb,
  2973. .lm_change = nfsd_change_deleg_cb,
  2974. };
  2975. static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
  2976. {
  2977. if (nfsd4_has_session(cstate))
  2978. return nfs_ok;
  2979. if (seqid == so->so_seqid - 1)
  2980. return nfserr_replay_me;
  2981. if (seqid == so->so_seqid)
  2982. return nfs_ok;
  2983. return nfserr_bad_seqid;
  2984. }
  2985. static __be32 lookup_clientid(clientid_t *clid,
  2986. struct nfsd4_compound_state *cstate,
  2987. struct nfsd_net *nn)
  2988. {
  2989. struct nfs4_client *found;
  2990. if (cstate->clp) {
  2991. found = cstate->clp;
  2992. if (!same_clid(&found->cl_clientid, clid))
  2993. return nfserr_stale_clientid;
  2994. return nfs_ok;
  2995. }
  2996. if (STALE_CLIENTID(clid, nn))
  2997. return nfserr_stale_clientid;
  2998. /*
  2999. * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
  3000. * cached already then we know this is for is for v4.0 and "sessions"
  3001. * will be false.
  3002. */
  3003. WARN_ON_ONCE(cstate->session);
  3004. spin_lock(&nn->client_lock);
  3005. found = find_confirmed_client(clid, false, nn);
  3006. if (!found) {
  3007. spin_unlock(&nn->client_lock);
  3008. return nfserr_expired;
  3009. }
  3010. atomic_inc(&found->cl_refcount);
  3011. spin_unlock(&nn->client_lock);
  3012. /* Cache the nfs4_client in cstate! */
  3013. cstate->clp = found;
  3014. return nfs_ok;
  3015. }
  3016. __be32
  3017. nfsd4_process_open1(struct nfsd4_compound_state *cstate,
  3018. struct nfsd4_open *open, struct nfsd_net *nn)
  3019. {
  3020. clientid_t *clientid = &open->op_clientid;
  3021. struct nfs4_client *clp = NULL;
  3022. unsigned int strhashval;
  3023. struct nfs4_openowner *oo = NULL;
  3024. __be32 status;
  3025. if (STALE_CLIENTID(&open->op_clientid, nn))
  3026. return nfserr_stale_clientid;
  3027. /*
  3028. * In case we need it later, after we've already created the
  3029. * file and don't want to risk a further failure:
  3030. */
  3031. open->op_file = nfsd4_alloc_file();
  3032. if (open->op_file == NULL)
  3033. return nfserr_jukebox;
  3034. status = lookup_clientid(clientid, cstate, nn);
  3035. if (status)
  3036. return status;
  3037. clp = cstate->clp;
  3038. strhashval = ownerstr_hashval(&open->op_owner);
  3039. oo = find_openstateowner_str(strhashval, open, clp);
  3040. open->op_openowner = oo;
  3041. if (!oo) {
  3042. goto new_owner;
  3043. }
  3044. if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  3045. /* Replace unconfirmed owners without checking for replay. */
  3046. release_openowner(oo);
  3047. open->op_openowner = NULL;
  3048. goto new_owner;
  3049. }
  3050. status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
  3051. if (status)
  3052. return status;
  3053. goto alloc_stateid;
  3054. new_owner:
  3055. oo = alloc_init_open_stateowner(strhashval, open, cstate);
  3056. if (oo == NULL)
  3057. return nfserr_jukebox;
  3058. open->op_openowner = oo;
  3059. alloc_stateid:
  3060. open->op_stp = nfs4_alloc_open_stateid(clp);
  3061. if (!open->op_stp)
  3062. return nfserr_jukebox;
  3063. return nfs_ok;
  3064. }
  3065. static inline __be32
  3066. nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
  3067. {
  3068. if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
  3069. return nfserr_openmode;
  3070. else
  3071. return nfs_ok;
  3072. }
  3073. static int share_access_to_flags(u32 share_access)
  3074. {
  3075. return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
  3076. }
  3077. static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
  3078. {
  3079. struct nfs4_stid *ret;
  3080. ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
  3081. if (!ret)
  3082. return NULL;
  3083. return delegstateid(ret);
  3084. }
  3085. static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
  3086. {
  3087. return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
  3088. open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
  3089. }
  3090. static __be32
  3091. nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
  3092. struct nfs4_delegation **dp)
  3093. {
  3094. int flags;
  3095. __be32 status = nfserr_bad_stateid;
  3096. struct nfs4_delegation *deleg;
  3097. deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
  3098. if (deleg == NULL)
  3099. goto out;
  3100. flags = share_access_to_flags(open->op_share_access);
  3101. status = nfs4_check_delegmode(deleg, flags);
  3102. if (status) {
  3103. nfs4_put_stid(&deleg->dl_stid);
  3104. goto out;
  3105. }
  3106. *dp = deleg;
  3107. out:
  3108. if (!nfsd4_is_deleg_cur(open))
  3109. return nfs_ok;
  3110. if (status)
  3111. return status;
  3112. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  3113. return nfs_ok;
  3114. }
  3115. static struct nfs4_ol_stateid *
  3116. nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
  3117. {
  3118. struct nfs4_ol_stateid *local, *ret = NULL;
  3119. struct nfs4_openowner *oo = open->op_openowner;
  3120. spin_lock(&fp->fi_lock);
  3121. list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
  3122. /* ignore lock owners */
  3123. if (local->st_stateowner->so_is_open_owner == 0)
  3124. continue;
  3125. if (local->st_stateowner == &oo->oo_owner) {
  3126. ret = local;
  3127. atomic_inc(&ret->st_stid.sc_count);
  3128. break;
  3129. }
  3130. }
  3131. spin_unlock(&fp->fi_lock);
  3132. return ret;
  3133. }
  3134. static inline int nfs4_access_to_access(u32 nfs4_access)
  3135. {
  3136. int flags = 0;
  3137. if (nfs4_access & NFS4_SHARE_ACCESS_READ)
  3138. flags |= NFSD_MAY_READ;
  3139. if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
  3140. flags |= NFSD_MAY_WRITE;
  3141. return flags;
  3142. }
  3143. static inline __be32
  3144. nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
  3145. struct nfsd4_open *open)
  3146. {
  3147. struct iattr iattr = {
  3148. .ia_valid = ATTR_SIZE,
  3149. .ia_size = 0,
  3150. };
  3151. if (!open->op_truncate)
  3152. return 0;
  3153. if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  3154. return nfserr_inval;
  3155. return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
  3156. }
  3157. static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
  3158. struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
  3159. struct nfsd4_open *open)
  3160. {
  3161. struct file *filp = NULL;
  3162. __be32 status;
  3163. int oflag = nfs4_access_to_omode(open->op_share_access);
  3164. int access = nfs4_access_to_access(open->op_share_access);
  3165. unsigned char old_access_bmap, old_deny_bmap;
  3166. spin_lock(&fp->fi_lock);
  3167. /*
  3168. * Are we trying to set a deny mode that would conflict with
  3169. * current access?
  3170. */
  3171. status = nfs4_file_check_deny(fp, open->op_share_deny);
  3172. if (status != nfs_ok) {
  3173. spin_unlock(&fp->fi_lock);
  3174. goto out;
  3175. }
  3176. /* set access to the file */
  3177. status = nfs4_file_get_access(fp, open->op_share_access);
  3178. if (status != nfs_ok) {
  3179. spin_unlock(&fp->fi_lock);
  3180. goto out;
  3181. }
  3182. /* Set access bits in stateid */
  3183. old_access_bmap = stp->st_access_bmap;
  3184. set_access(open->op_share_access, stp);
  3185. /* Set new deny mask */
  3186. old_deny_bmap = stp->st_deny_bmap;
  3187. set_deny(open->op_share_deny, stp);
  3188. fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
  3189. if (!fp->fi_fds[oflag]) {
  3190. spin_unlock(&fp->fi_lock);
  3191. status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
  3192. if (status)
  3193. goto out_put_access;
  3194. spin_lock(&fp->fi_lock);
  3195. if (!fp->fi_fds[oflag]) {
  3196. fp->fi_fds[oflag] = filp;
  3197. filp = NULL;
  3198. }
  3199. }
  3200. spin_unlock(&fp->fi_lock);
  3201. if (filp)
  3202. fput(filp);
  3203. status = nfsd4_truncate(rqstp, cur_fh, open);
  3204. if (status)
  3205. goto out_put_access;
  3206. out:
  3207. return status;
  3208. out_put_access:
  3209. stp->st_access_bmap = old_access_bmap;
  3210. nfs4_file_put_access(fp, open->op_share_access);
  3211. reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
  3212. goto out;
  3213. }
  3214. static __be32
  3215. 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)
  3216. {
  3217. __be32 status;
  3218. unsigned char old_deny_bmap;
  3219. if (!test_access(open->op_share_access, stp))
  3220. return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
  3221. /* test and set deny mode */
  3222. spin_lock(&fp->fi_lock);
  3223. status = nfs4_file_check_deny(fp, open->op_share_deny);
  3224. if (status == nfs_ok) {
  3225. old_deny_bmap = stp->st_deny_bmap;
  3226. set_deny(open->op_share_deny, stp);
  3227. fp->fi_share_deny |=
  3228. (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
  3229. }
  3230. spin_unlock(&fp->fi_lock);
  3231. if (status != nfs_ok)
  3232. return status;
  3233. status = nfsd4_truncate(rqstp, cur_fh, open);
  3234. if (status != nfs_ok)
  3235. reset_union_bmap_deny(old_deny_bmap, stp);
  3236. return status;
  3237. }
  3238. static void
  3239. nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
  3240. {
  3241. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  3242. }
  3243. /* Should we give out recallable state?: */
  3244. static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
  3245. {
  3246. if (clp->cl_cb_state == NFSD4_CB_UP)
  3247. return true;
  3248. /*
  3249. * In the sessions case, since we don't have to establish a
  3250. * separate connection for callbacks, we assume it's OK
  3251. * until we hear otherwise:
  3252. */
  3253. return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
  3254. }
  3255. static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
  3256. {
  3257. struct file_lock *fl;
  3258. fl = locks_alloc_lock();
  3259. if (!fl)
  3260. return NULL;
  3261. fl->fl_lmops = &nfsd_lease_mng_ops;
  3262. fl->fl_flags = FL_DELEG;
  3263. fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
  3264. fl->fl_end = OFFSET_MAX;
  3265. fl->fl_owner = (fl_owner_t)fp;
  3266. fl->fl_pid = current->tgid;
  3267. return fl;
  3268. }
  3269. static int nfs4_setlease(struct nfs4_delegation *dp)
  3270. {
  3271. struct nfs4_file *fp = dp->dl_stid.sc_file;
  3272. struct file_lock *fl, *ret;
  3273. struct file *filp;
  3274. int status = 0;
  3275. fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
  3276. if (!fl)
  3277. return -ENOMEM;
  3278. filp = find_readable_file(fp);
  3279. if (!filp) {
  3280. /* We should always have a readable file here */
  3281. WARN_ON_ONCE(1);
  3282. return -EBADF;
  3283. }
  3284. fl->fl_file = filp;
  3285. ret = fl;
  3286. status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
  3287. if (fl)
  3288. locks_free_lock(fl);
  3289. if (status)
  3290. goto out_fput;
  3291. spin_lock(&state_lock);
  3292. spin_lock(&fp->fi_lock);
  3293. /* Did the lease get broken before we took the lock? */
  3294. status = -EAGAIN;
  3295. if (fp->fi_had_conflict)
  3296. goto out_unlock;
  3297. /* Race breaker */
  3298. if (fp->fi_deleg_file) {
  3299. status = 0;
  3300. atomic_inc(&fp->fi_delegees);
  3301. hash_delegation_locked(dp, fp);
  3302. goto out_unlock;
  3303. }
  3304. fp->fi_deleg_file = filp;
  3305. atomic_set(&fp->fi_delegees, 1);
  3306. hash_delegation_locked(dp, fp);
  3307. spin_unlock(&fp->fi_lock);
  3308. spin_unlock(&state_lock);
  3309. return 0;
  3310. out_unlock:
  3311. spin_unlock(&fp->fi_lock);
  3312. spin_unlock(&state_lock);
  3313. out_fput:
  3314. fput(filp);
  3315. return status;
  3316. }
  3317. static struct nfs4_delegation *
  3318. nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
  3319. struct nfs4_file *fp)
  3320. {
  3321. int status;
  3322. struct nfs4_delegation *dp;
  3323. if (fp->fi_had_conflict)
  3324. return ERR_PTR(-EAGAIN);
  3325. dp = alloc_init_deleg(clp, fh);
  3326. if (!dp)
  3327. return ERR_PTR(-ENOMEM);
  3328. get_nfs4_file(fp);
  3329. spin_lock(&state_lock);
  3330. spin_lock(&fp->fi_lock);
  3331. dp->dl_stid.sc_file = fp;
  3332. if (!fp->fi_deleg_file) {
  3333. spin_unlock(&fp->fi_lock);
  3334. spin_unlock(&state_lock);
  3335. status = nfs4_setlease(dp);
  3336. goto out;
  3337. }
  3338. atomic_inc(&fp->fi_delegees);
  3339. if (fp->fi_had_conflict) {
  3340. status = -EAGAIN;
  3341. goto out_unlock;
  3342. }
  3343. hash_delegation_locked(dp, fp);
  3344. status = 0;
  3345. out_unlock:
  3346. spin_unlock(&fp->fi_lock);
  3347. spin_unlock(&state_lock);
  3348. out:
  3349. if (status) {
  3350. nfs4_put_stid(&dp->dl_stid);
  3351. return ERR_PTR(status);
  3352. }
  3353. return dp;
  3354. }
  3355. static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
  3356. {
  3357. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  3358. if (status == -EAGAIN)
  3359. open->op_why_no_deleg = WND4_CONTENTION;
  3360. else {
  3361. open->op_why_no_deleg = WND4_RESOURCE;
  3362. switch (open->op_deleg_want) {
  3363. case NFS4_SHARE_WANT_READ_DELEG:
  3364. case NFS4_SHARE_WANT_WRITE_DELEG:
  3365. case NFS4_SHARE_WANT_ANY_DELEG:
  3366. break;
  3367. case NFS4_SHARE_WANT_CANCEL:
  3368. open->op_why_no_deleg = WND4_CANCELLED;
  3369. break;
  3370. case NFS4_SHARE_WANT_NO_DELEG:
  3371. WARN_ON_ONCE(1);
  3372. }
  3373. }
  3374. }
  3375. /*
  3376. * Attempt to hand out a delegation.
  3377. *
  3378. * Note we don't support write delegations, and won't until the vfs has
  3379. * proper support for them.
  3380. */
  3381. static void
  3382. nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
  3383. struct nfs4_ol_stateid *stp)
  3384. {
  3385. struct nfs4_delegation *dp;
  3386. struct nfs4_openowner *oo = openowner(stp->st_stateowner);
  3387. struct nfs4_client *clp = stp->st_stid.sc_client;
  3388. int cb_up;
  3389. int status = 0;
  3390. cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
  3391. open->op_recall = 0;
  3392. switch (open->op_claim_type) {
  3393. case NFS4_OPEN_CLAIM_PREVIOUS:
  3394. if (!cb_up)
  3395. open->op_recall = 1;
  3396. if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
  3397. goto out_no_deleg;
  3398. break;
  3399. case NFS4_OPEN_CLAIM_NULL:
  3400. case NFS4_OPEN_CLAIM_FH:
  3401. /*
  3402. * Let's not give out any delegations till everyone's
  3403. * had the chance to reclaim theirs....
  3404. */
  3405. if (locks_in_grace(clp->net))
  3406. goto out_no_deleg;
  3407. if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
  3408. goto out_no_deleg;
  3409. /*
  3410. * Also, if the file was opened for write or
  3411. * create, there's a good chance the client's
  3412. * about to write to it, resulting in an
  3413. * immediate recall (since we don't support
  3414. * write delegations):
  3415. */
  3416. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  3417. goto out_no_deleg;
  3418. if (open->op_create == NFS4_OPEN_CREATE)
  3419. goto out_no_deleg;
  3420. break;
  3421. default:
  3422. goto out_no_deleg;
  3423. }
  3424. dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
  3425. if (IS_ERR(dp))
  3426. goto out_no_deleg;
  3427. memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
  3428. dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
  3429. STATEID_VAL(&dp->dl_stid.sc_stateid));
  3430. open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
  3431. nfs4_put_stid(&dp->dl_stid);
  3432. return;
  3433. out_no_deleg:
  3434. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
  3435. if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
  3436. open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
  3437. dprintk("NFSD: WARNING: refusing delegation reclaim\n");
  3438. open->op_recall = 1;
  3439. }
  3440. /* 4.1 client asking for a delegation? */
  3441. if (open->op_deleg_want)
  3442. nfsd4_open_deleg_none_ext(open, status);
  3443. return;
  3444. }
  3445. static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
  3446. struct nfs4_delegation *dp)
  3447. {
  3448. if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
  3449. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  3450. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  3451. open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
  3452. } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
  3453. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  3454. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  3455. open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
  3456. }
  3457. /* Otherwise the client must be confused wanting a delegation
  3458. * it already has, therefore we don't return
  3459. * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
  3460. */
  3461. }
  3462. __be32
  3463. nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  3464. {
  3465. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  3466. struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
  3467. struct nfs4_file *fp = NULL;
  3468. struct nfs4_ol_stateid *stp = NULL;
  3469. struct nfs4_delegation *dp = NULL;
  3470. __be32 status;
  3471. /*
  3472. * Lookup file; if found, lookup stateid and check open request,
  3473. * and check for delegations in the process of being recalled.
  3474. * If not found, create the nfs4_file struct
  3475. */
  3476. fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
  3477. if (fp != open->op_file) {
  3478. status = nfs4_check_deleg(cl, open, &dp);
  3479. if (status)
  3480. goto out;
  3481. stp = nfsd4_find_existing_open(fp, open);
  3482. } else {
  3483. open->op_file = NULL;
  3484. status = nfserr_bad_stateid;
  3485. if (nfsd4_is_deleg_cur(open))
  3486. goto out;
  3487. status = nfserr_jukebox;
  3488. }
  3489. /*
  3490. * OPEN the file, or upgrade an existing OPEN.
  3491. * If truncate fails, the OPEN fails.
  3492. */
  3493. if (stp) {
  3494. /* Stateid was found, this is an OPEN upgrade */
  3495. status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
  3496. if (status)
  3497. goto out;
  3498. } else {
  3499. stp = open->op_stp;
  3500. open->op_stp = NULL;
  3501. init_open_stateid(stp, fp, open);
  3502. status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
  3503. if (status) {
  3504. release_open_stateid(stp);
  3505. goto out;
  3506. }
  3507. }
  3508. update_stateid(&stp->st_stid.sc_stateid);
  3509. memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  3510. if (nfsd4_has_session(&resp->cstate)) {
  3511. if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
  3512. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  3513. open->op_why_no_deleg = WND4_NOT_WANTED;
  3514. goto nodeleg;
  3515. }
  3516. }
  3517. /*
  3518. * Attempt to hand out a delegation. No error return, because the
  3519. * OPEN succeeds even if we fail.
  3520. */
  3521. nfs4_open_delegation(current_fh, open, stp);
  3522. nodeleg:
  3523. status = nfs_ok;
  3524. dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
  3525. STATEID_VAL(&stp->st_stid.sc_stateid));
  3526. out:
  3527. /* 4.1 client trying to upgrade/downgrade delegation? */
  3528. if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
  3529. open->op_deleg_want)
  3530. nfsd4_deleg_xgrade_none_ext(open, dp);
  3531. if (fp)
  3532. put_nfs4_file(fp);
  3533. if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  3534. nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
  3535. /*
  3536. * To finish the open response, we just need to set the rflags.
  3537. */
  3538. open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
  3539. if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
  3540. !nfsd4_has_session(&resp->cstate))
  3541. open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
  3542. if (dp)
  3543. nfs4_put_stid(&dp->dl_stid);
  3544. if (stp)
  3545. nfs4_put_stid(&stp->st_stid);
  3546. return status;
  3547. }
  3548. void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
  3549. struct nfsd4_open *open, __be32 status)
  3550. {
  3551. if (open->op_openowner) {
  3552. struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
  3553. nfsd4_cstate_assign_replay(cstate, so);
  3554. nfs4_put_stateowner(so);
  3555. }
  3556. if (open->op_file)
  3557. nfsd4_free_file(open->op_file);
  3558. if (open->op_stp)
  3559. nfs4_put_stid(&open->op_stp->st_stid);
  3560. }
  3561. __be32
  3562. nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3563. clientid_t *clid)
  3564. {
  3565. struct nfs4_client *clp;
  3566. __be32 status;
  3567. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3568. dprintk("process_renew(%08x/%08x): starting\n",
  3569. clid->cl_boot, clid->cl_id);
  3570. status = lookup_clientid(clid, cstate, nn);
  3571. if (status)
  3572. goto out;
  3573. clp = cstate->clp;
  3574. status = nfserr_cb_path_down;
  3575. if (!list_empty(&clp->cl_delegations)
  3576. && clp->cl_cb_state != NFSD4_CB_UP)
  3577. goto out;
  3578. status = nfs_ok;
  3579. out:
  3580. return status;
  3581. }
  3582. static void
  3583. nfsd4_end_grace(struct nfsd_net *nn)
  3584. {
  3585. /* do nothing if grace period already ended */
  3586. if (nn->grace_ended)
  3587. return;
  3588. dprintk("NFSD: end of grace period\n");
  3589. nn->grace_ended = true;
  3590. nfsd4_record_grace_done(nn, nn->boot_time);
  3591. locks_end_grace(&nn->nfsd4_manager);
  3592. /*
  3593. * Now that every NFSv4 client has had the chance to recover and
  3594. * to see the (possibly new, possibly shorter) lease time, we
  3595. * can safely set the next grace time to the current lease time:
  3596. */
  3597. nn->nfsd4_grace = nn->nfsd4_lease;
  3598. }
  3599. static time_t
  3600. nfs4_laundromat(struct nfsd_net *nn)
  3601. {
  3602. struct nfs4_client *clp;
  3603. struct nfs4_openowner *oo;
  3604. struct nfs4_delegation *dp;
  3605. struct nfs4_ol_stateid *stp;
  3606. struct list_head *pos, *next, reaplist;
  3607. time_t cutoff = get_seconds() - nn->nfsd4_lease;
  3608. time_t t, new_timeo = nn->nfsd4_lease;
  3609. dprintk("NFSD: laundromat service - starting\n");
  3610. nfsd4_end_grace(nn);
  3611. INIT_LIST_HEAD(&reaplist);
  3612. spin_lock(&nn->client_lock);
  3613. list_for_each_safe(pos, next, &nn->client_lru) {
  3614. clp = list_entry(pos, struct nfs4_client, cl_lru);
  3615. if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
  3616. t = clp->cl_time - cutoff;
  3617. new_timeo = min(new_timeo, t);
  3618. break;
  3619. }
  3620. if (mark_client_expired_locked(clp)) {
  3621. dprintk("NFSD: client in use (clientid %08x)\n",
  3622. clp->cl_clientid.cl_id);
  3623. continue;
  3624. }
  3625. list_add(&clp->cl_lru, &reaplist);
  3626. }
  3627. spin_unlock(&nn->client_lock);
  3628. list_for_each_safe(pos, next, &reaplist) {
  3629. clp = list_entry(pos, struct nfs4_client, cl_lru);
  3630. dprintk("NFSD: purging unused client (clientid %08x)\n",
  3631. clp->cl_clientid.cl_id);
  3632. list_del_init(&clp->cl_lru);
  3633. expire_client(clp);
  3634. }
  3635. spin_lock(&state_lock);
  3636. list_for_each_safe(pos, next, &nn->del_recall_lru) {
  3637. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  3638. if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
  3639. continue;
  3640. if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
  3641. t = dp->dl_time - cutoff;
  3642. new_timeo = min(new_timeo, t);
  3643. break;
  3644. }
  3645. unhash_delegation_locked(dp);
  3646. list_add(&dp->dl_recall_lru, &reaplist);
  3647. }
  3648. spin_unlock(&state_lock);
  3649. while (!list_empty(&reaplist)) {
  3650. dp = list_first_entry(&reaplist, struct nfs4_delegation,
  3651. dl_recall_lru);
  3652. list_del_init(&dp->dl_recall_lru);
  3653. revoke_delegation(dp);
  3654. }
  3655. spin_lock(&nn->client_lock);
  3656. while (!list_empty(&nn->close_lru)) {
  3657. oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
  3658. oo_close_lru);
  3659. if (time_after((unsigned long)oo->oo_time,
  3660. (unsigned long)cutoff)) {
  3661. t = oo->oo_time - cutoff;
  3662. new_timeo = min(new_timeo, t);
  3663. break;
  3664. }
  3665. list_del_init(&oo->oo_close_lru);
  3666. stp = oo->oo_last_closed_stid;
  3667. oo->oo_last_closed_stid = NULL;
  3668. spin_unlock(&nn->client_lock);
  3669. nfs4_put_stid(&stp->st_stid);
  3670. spin_lock(&nn->client_lock);
  3671. }
  3672. spin_unlock(&nn->client_lock);
  3673. new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
  3674. return new_timeo;
  3675. }
  3676. static struct workqueue_struct *laundry_wq;
  3677. static void laundromat_main(struct work_struct *);
  3678. static void
  3679. laundromat_main(struct work_struct *laundry)
  3680. {
  3681. time_t t;
  3682. struct delayed_work *dwork = container_of(laundry, struct delayed_work,
  3683. work);
  3684. struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
  3685. laundromat_work);
  3686. t = nfs4_laundromat(nn);
  3687. dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
  3688. queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
  3689. }
  3690. static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
  3691. {
  3692. if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
  3693. return nfserr_bad_stateid;
  3694. return nfs_ok;
  3695. }
  3696. static inline int
  3697. access_permit_read(struct nfs4_ol_stateid *stp)
  3698. {
  3699. return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
  3700. test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
  3701. test_access(NFS4_SHARE_ACCESS_WRITE, stp);
  3702. }
  3703. static inline int
  3704. access_permit_write(struct nfs4_ol_stateid *stp)
  3705. {
  3706. return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
  3707. test_access(NFS4_SHARE_ACCESS_BOTH, stp);
  3708. }
  3709. static
  3710. __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
  3711. {
  3712. __be32 status = nfserr_openmode;
  3713. /* For lock stateid's, we test the parent open, not the lock: */
  3714. if (stp->st_openstp)
  3715. stp = stp->st_openstp;
  3716. if ((flags & WR_STATE) && !access_permit_write(stp))
  3717. goto out;
  3718. if ((flags & RD_STATE) && !access_permit_read(stp))
  3719. goto out;
  3720. status = nfs_ok;
  3721. out:
  3722. return status;
  3723. }
  3724. static inline __be32
  3725. check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
  3726. {
  3727. if (ONE_STATEID(stateid) && (flags & RD_STATE))
  3728. return nfs_ok;
  3729. else if (locks_in_grace(net)) {
  3730. /* Answer in remaining cases depends on existence of
  3731. * conflicting state; so we must wait out the grace period. */
  3732. return nfserr_grace;
  3733. } else if (flags & WR_STATE)
  3734. return nfs4_share_conflict(current_fh,
  3735. NFS4_SHARE_DENY_WRITE);
  3736. else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
  3737. return nfs4_share_conflict(current_fh,
  3738. NFS4_SHARE_DENY_READ);
  3739. }
  3740. /*
  3741. * Allow READ/WRITE during grace period on recovered state only for files
  3742. * that are not able to provide mandatory locking.
  3743. */
  3744. static inline int
  3745. grace_disallows_io(struct net *net, struct inode *inode)
  3746. {
  3747. return locks_in_grace(net) && mandatory_lock(inode);
  3748. }
  3749. /* Returns true iff a is later than b: */
  3750. static bool stateid_generation_after(stateid_t *a, stateid_t *b)
  3751. {
  3752. return (s32)(a->si_generation - b->si_generation) > 0;
  3753. }
  3754. static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
  3755. {
  3756. /*
  3757. * When sessions are used the stateid generation number is ignored
  3758. * when it is zero.
  3759. */
  3760. if (has_session && in->si_generation == 0)
  3761. return nfs_ok;
  3762. if (in->si_generation == ref->si_generation)
  3763. return nfs_ok;
  3764. /* If the client sends us a stateid from the future, it's buggy: */
  3765. if (stateid_generation_after(in, ref))
  3766. return nfserr_bad_stateid;
  3767. /*
  3768. * However, we could see a stateid from the past, even from a
  3769. * non-buggy client. For example, if the client sends a lock
  3770. * while some IO is outstanding, the lock may bump si_generation
  3771. * while the IO is still in flight. The client could avoid that
  3772. * situation by waiting for responses on all the IO requests,
  3773. * but better performance may result in retrying IO that
  3774. * receives an old_stateid error if requests are rarely
  3775. * reordered in flight:
  3776. */
  3777. return nfserr_old_stateid;
  3778. }
  3779. static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
  3780. {
  3781. struct nfs4_stid *s;
  3782. struct nfs4_ol_stateid *ols;
  3783. __be32 status = nfserr_bad_stateid;
  3784. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3785. return status;
  3786. /* Client debugging aid. */
  3787. if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
  3788. char addr_str[INET6_ADDRSTRLEN];
  3789. rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
  3790. sizeof(addr_str));
  3791. pr_warn_ratelimited("NFSD: client %s testing state ID "
  3792. "with incorrect client ID\n", addr_str);
  3793. return status;
  3794. }
  3795. spin_lock(&cl->cl_lock);
  3796. s = find_stateid_locked(cl, stateid);
  3797. if (!s)
  3798. goto out_unlock;
  3799. status = check_stateid_generation(stateid, &s->sc_stateid, 1);
  3800. if (status)
  3801. goto out_unlock;
  3802. switch (s->sc_type) {
  3803. case NFS4_DELEG_STID:
  3804. status = nfs_ok;
  3805. break;
  3806. case NFS4_REVOKED_DELEG_STID:
  3807. status = nfserr_deleg_revoked;
  3808. break;
  3809. case NFS4_OPEN_STID:
  3810. case NFS4_LOCK_STID:
  3811. ols = openlockstateid(s);
  3812. if (ols->st_stateowner->so_is_open_owner
  3813. && !(openowner(ols->st_stateowner)->oo_flags
  3814. & NFS4_OO_CONFIRMED))
  3815. status = nfserr_bad_stateid;
  3816. else
  3817. status = nfs_ok;
  3818. break;
  3819. default:
  3820. printk("unknown stateid type %x\n", s->sc_type);
  3821. /* Fallthrough */
  3822. case NFS4_CLOSED_STID:
  3823. case NFS4_CLOSED_DELEG_STID:
  3824. status = nfserr_bad_stateid;
  3825. }
  3826. out_unlock:
  3827. spin_unlock(&cl->cl_lock);
  3828. return status;
  3829. }
  3830. static __be32
  3831. nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
  3832. stateid_t *stateid, unsigned char typemask,
  3833. struct nfs4_stid **s, struct nfsd_net *nn)
  3834. {
  3835. __be32 status;
  3836. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3837. return nfserr_bad_stateid;
  3838. status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
  3839. if (status == nfserr_stale_clientid) {
  3840. if (cstate->session)
  3841. return nfserr_bad_stateid;
  3842. return nfserr_stale_stateid;
  3843. }
  3844. if (status)
  3845. return status;
  3846. *s = find_stateid_by_type(cstate->clp, stateid, typemask);
  3847. if (!*s)
  3848. return nfserr_bad_stateid;
  3849. return nfs_ok;
  3850. }
  3851. /*
  3852. * Checks for stateid operations
  3853. */
  3854. __be32
  3855. nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
  3856. stateid_t *stateid, int flags, struct file **filpp)
  3857. {
  3858. struct nfs4_stid *s;
  3859. struct nfs4_ol_stateid *stp = NULL;
  3860. struct nfs4_delegation *dp = NULL;
  3861. struct svc_fh *current_fh = &cstate->current_fh;
  3862. struct inode *ino = current_fh->fh_dentry->d_inode;
  3863. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3864. struct file *file = NULL;
  3865. __be32 status;
  3866. if (filpp)
  3867. *filpp = NULL;
  3868. if (grace_disallows_io(net, ino))
  3869. return nfserr_grace;
  3870. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  3871. return check_special_stateids(net, current_fh, stateid, flags);
  3872. status = nfsd4_lookup_stateid(cstate, stateid,
  3873. NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
  3874. &s, nn);
  3875. if (status)
  3876. return status;
  3877. status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
  3878. if (status)
  3879. goto out;
  3880. switch (s->sc_type) {
  3881. case NFS4_DELEG_STID:
  3882. dp = delegstateid(s);
  3883. status = nfs4_check_delegmode(dp, flags);
  3884. if (status)
  3885. goto out;
  3886. if (filpp) {
  3887. file = dp->dl_stid.sc_file->fi_deleg_file;
  3888. if (!file) {
  3889. WARN_ON_ONCE(1);
  3890. status = nfserr_serverfault;
  3891. goto out;
  3892. }
  3893. get_file(file);
  3894. }
  3895. break;
  3896. case NFS4_OPEN_STID:
  3897. case NFS4_LOCK_STID:
  3898. stp = openlockstateid(s);
  3899. status = nfs4_check_fh(current_fh, stp);
  3900. if (status)
  3901. goto out;
  3902. if (stp->st_stateowner->so_is_open_owner
  3903. && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  3904. goto out;
  3905. status = nfs4_check_openmode(stp, flags);
  3906. if (status)
  3907. goto out;
  3908. if (filpp) {
  3909. struct nfs4_file *fp = stp->st_stid.sc_file;
  3910. if (flags & RD_STATE)
  3911. file = find_readable_file(fp);
  3912. else
  3913. file = find_writeable_file(fp);
  3914. }
  3915. break;
  3916. default:
  3917. status = nfserr_bad_stateid;
  3918. goto out;
  3919. }
  3920. status = nfs_ok;
  3921. if (file)
  3922. *filpp = file;
  3923. out:
  3924. nfs4_put_stid(s);
  3925. return status;
  3926. }
  3927. /*
  3928. * Test if the stateid is valid
  3929. */
  3930. __be32
  3931. nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3932. struct nfsd4_test_stateid *test_stateid)
  3933. {
  3934. struct nfsd4_test_stateid_id *stateid;
  3935. struct nfs4_client *cl = cstate->session->se_client;
  3936. list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
  3937. stateid->ts_id_status =
  3938. nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
  3939. return nfs_ok;
  3940. }
  3941. __be32
  3942. nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3943. struct nfsd4_free_stateid *free_stateid)
  3944. {
  3945. stateid_t *stateid = &free_stateid->fr_stateid;
  3946. struct nfs4_stid *s;
  3947. struct nfs4_delegation *dp;
  3948. struct nfs4_ol_stateid *stp;
  3949. struct nfs4_client *cl = cstate->session->se_client;
  3950. __be32 ret = nfserr_bad_stateid;
  3951. spin_lock(&cl->cl_lock);
  3952. s = find_stateid_locked(cl, stateid);
  3953. if (!s)
  3954. goto out_unlock;
  3955. switch (s->sc_type) {
  3956. case NFS4_DELEG_STID:
  3957. ret = nfserr_locks_held;
  3958. break;
  3959. case NFS4_OPEN_STID:
  3960. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  3961. if (ret)
  3962. break;
  3963. ret = nfserr_locks_held;
  3964. break;
  3965. case NFS4_LOCK_STID:
  3966. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  3967. if (ret)
  3968. break;
  3969. stp = openlockstateid(s);
  3970. ret = nfserr_locks_held;
  3971. if (check_for_locks(stp->st_stid.sc_file,
  3972. lockowner(stp->st_stateowner)))
  3973. break;
  3974. unhash_lock_stateid(stp);
  3975. spin_unlock(&cl->cl_lock);
  3976. nfs4_put_stid(s);
  3977. ret = nfs_ok;
  3978. goto out;
  3979. case NFS4_REVOKED_DELEG_STID:
  3980. dp = delegstateid(s);
  3981. list_del_init(&dp->dl_recall_lru);
  3982. spin_unlock(&cl->cl_lock);
  3983. nfs4_put_stid(s);
  3984. ret = nfs_ok;
  3985. goto out;
  3986. /* Default falls through and returns nfserr_bad_stateid */
  3987. }
  3988. out_unlock:
  3989. spin_unlock(&cl->cl_lock);
  3990. out:
  3991. return ret;
  3992. }
  3993. static inline int
  3994. setlkflg (int type)
  3995. {
  3996. return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
  3997. RD_STATE : WR_STATE;
  3998. }
  3999. static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
  4000. {
  4001. struct svc_fh *current_fh = &cstate->current_fh;
  4002. struct nfs4_stateowner *sop = stp->st_stateowner;
  4003. __be32 status;
  4004. status = nfsd4_check_seqid(cstate, sop, seqid);
  4005. if (status)
  4006. return status;
  4007. if (stp->st_stid.sc_type == NFS4_CLOSED_STID
  4008. || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
  4009. /*
  4010. * "Closed" stateid's exist *only* to return
  4011. * nfserr_replay_me from the previous step, and
  4012. * revoked delegations are kept only for free_stateid.
  4013. */
  4014. return nfserr_bad_stateid;
  4015. status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
  4016. if (status)
  4017. return status;
  4018. return nfs4_check_fh(current_fh, stp);
  4019. }
  4020. /*
  4021. * Checks for sequence id mutating operations.
  4022. */
  4023. static __be32
  4024. nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  4025. stateid_t *stateid, char typemask,
  4026. struct nfs4_ol_stateid **stpp,
  4027. struct nfsd_net *nn)
  4028. {
  4029. __be32 status;
  4030. struct nfs4_stid *s;
  4031. struct nfs4_ol_stateid *stp = NULL;
  4032. dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
  4033. seqid, STATEID_VAL(stateid));
  4034. *stpp = NULL;
  4035. status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
  4036. if (status)
  4037. return status;
  4038. stp = openlockstateid(s);
  4039. nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
  4040. status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
  4041. if (!status)
  4042. *stpp = stp;
  4043. else
  4044. nfs4_put_stid(&stp->st_stid);
  4045. return status;
  4046. }
  4047. static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  4048. stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
  4049. {
  4050. __be32 status;
  4051. struct nfs4_openowner *oo;
  4052. struct nfs4_ol_stateid *stp;
  4053. status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
  4054. NFS4_OPEN_STID, &stp, nn);
  4055. if (status)
  4056. return status;
  4057. oo = openowner(stp->st_stateowner);
  4058. if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  4059. nfs4_put_stid(&stp->st_stid);
  4060. return nfserr_bad_stateid;
  4061. }
  4062. *stpp = stp;
  4063. return nfs_ok;
  4064. }
  4065. __be32
  4066. nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4067. struct nfsd4_open_confirm *oc)
  4068. {
  4069. __be32 status;
  4070. struct nfs4_openowner *oo;
  4071. struct nfs4_ol_stateid *stp;
  4072. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  4073. dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
  4074. cstate->current_fh.fh_dentry);
  4075. status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
  4076. if (status)
  4077. return status;
  4078. status = nfs4_preprocess_seqid_op(cstate,
  4079. oc->oc_seqid, &oc->oc_req_stateid,
  4080. NFS4_OPEN_STID, &stp, nn);
  4081. if (status)
  4082. goto out;
  4083. oo = openowner(stp->st_stateowner);
  4084. status = nfserr_bad_stateid;
  4085. if (oo->oo_flags & NFS4_OO_CONFIRMED)
  4086. goto put_stateid;
  4087. oo->oo_flags |= NFS4_OO_CONFIRMED;
  4088. update_stateid(&stp->st_stid.sc_stateid);
  4089. memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  4090. dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
  4091. __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
  4092. nfsd4_client_record_create(oo->oo_owner.so_client);
  4093. status = nfs_ok;
  4094. put_stateid:
  4095. nfs4_put_stid(&stp->st_stid);
  4096. out:
  4097. nfsd4_bump_seqid(cstate, status);
  4098. return status;
  4099. }
  4100. static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
  4101. {
  4102. if (!test_access(access, stp))
  4103. return;
  4104. nfs4_file_put_access(stp->st_stid.sc_file, access);
  4105. clear_access(access, stp);
  4106. }
  4107. static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
  4108. {
  4109. switch (to_access) {
  4110. case NFS4_SHARE_ACCESS_READ:
  4111. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
  4112. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  4113. break;
  4114. case NFS4_SHARE_ACCESS_WRITE:
  4115. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
  4116. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  4117. break;
  4118. case NFS4_SHARE_ACCESS_BOTH:
  4119. break;
  4120. default:
  4121. WARN_ON_ONCE(1);
  4122. }
  4123. }
  4124. __be32
  4125. nfsd4_open_downgrade(struct svc_rqst *rqstp,
  4126. struct nfsd4_compound_state *cstate,
  4127. struct nfsd4_open_downgrade *od)
  4128. {
  4129. __be32 status;
  4130. struct nfs4_ol_stateid *stp;
  4131. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  4132. dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
  4133. cstate->current_fh.fh_dentry);
  4134. /* We don't yet support WANT bits: */
  4135. if (od->od_deleg_want)
  4136. dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
  4137. od->od_deleg_want);
  4138. status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
  4139. &od->od_stateid, &stp, nn);
  4140. if (status)
  4141. goto out;
  4142. status = nfserr_inval;
  4143. if (!test_access(od->od_share_access, stp)) {
  4144. dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
  4145. stp->st_access_bmap, od->od_share_access);
  4146. goto put_stateid;
  4147. }
  4148. if (!test_deny(od->od_share_deny, stp)) {
  4149. dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
  4150. stp->st_deny_bmap, od->od_share_deny);
  4151. goto put_stateid;
  4152. }
  4153. nfs4_stateid_downgrade(stp, od->od_share_access);
  4154. reset_union_bmap_deny(od->od_share_deny, stp);
  4155. update_stateid(&stp->st_stid.sc_stateid);
  4156. memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  4157. status = nfs_ok;
  4158. put_stateid:
  4159. nfs4_put_stid(&stp->st_stid);
  4160. out:
  4161. nfsd4_bump_seqid(cstate, status);
  4162. return status;
  4163. }
  4164. static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
  4165. {
  4166. struct nfs4_client *clp = s->st_stid.sc_client;
  4167. LIST_HEAD(reaplist);
  4168. s->st_stid.sc_type = NFS4_CLOSED_STID;
  4169. spin_lock(&clp->cl_lock);
  4170. unhash_open_stateid(s, &reaplist);
  4171. if (clp->cl_minorversion) {
  4172. put_ol_stateid_locked(s, &reaplist);
  4173. spin_unlock(&clp->cl_lock);
  4174. free_ol_stateid_reaplist(&reaplist);
  4175. } else {
  4176. spin_unlock(&clp->cl_lock);
  4177. free_ol_stateid_reaplist(&reaplist);
  4178. move_to_close_lru(s, clp->net);
  4179. }
  4180. }
  4181. /*
  4182. * nfs4_unlock_state() called after encode
  4183. */
  4184. __be32
  4185. nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4186. struct nfsd4_close *close)
  4187. {
  4188. __be32 status;
  4189. struct nfs4_ol_stateid *stp;
  4190. struct net *net = SVC_NET(rqstp);
  4191. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4192. dprintk("NFSD: nfsd4_close on file %pd\n",
  4193. cstate->current_fh.fh_dentry);
  4194. status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
  4195. &close->cl_stateid,
  4196. NFS4_OPEN_STID|NFS4_CLOSED_STID,
  4197. &stp, nn);
  4198. nfsd4_bump_seqid(cstate, status);
  4199. if (status)
  4200. goto out;
  4201. update_stateid(&stp->st_stid.sc_stateid);
  4202. memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  4203. nfsd4_close_open_stateid(stp);
  4204. /* put reference from nfs4_preprocess_seqid_op */
  4205. nfs4_put_stid(&stp->st_stid);
  4206. out:
  4207. return status;
  4208. }
  4209. __be32
  4210. nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4211. struct nfsd4_delegreturn *dr)
  4212. {
  4213. struct nfs4_delegation *dp;
  4214. stateid_t *stateid = &dr->dr_stateid;
  4215. struct nfs4_stid *s;
  4216. __be32 status;
  4217. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  4218. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  4219. return status;
  4220. status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
  4221. if (status)
  4222. goto out;
  4223. dp = delegstateid(s);
  4224. status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
  4225. if (status)
  4226. goto put_stateid;
  4227. destroy_delegation(dp);
  4228. put_stateid:
  4229. nfs4_put_stid(&dp->dl_stid);
  4230. out:
  4231. return status;
  4232. }
  4233. #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
  4234. static inline u64
  4235. end_offset(u64 start, u64 len)
  4236. {
  4237. u64 end;
  4238. end = start + len;
  4239. return end >= start ? end: NFS4_MAX_UINT64;
  4240. }
  4241. /* last octet in a range */
  4242. static inline u64
  4243. last_byte_offset(u64 start, u64 len)
  4244. {
  4245. u64 end;
  4246. WARN_ON_ONCE(!len);
  4247. end = start + len;
  4248. return end > start ? end - 1: NFS4_MAX_UINT64;
  4249. }
  4250. /*
  4251. * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
  4252. * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
  4253. * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
  4254. * locking, this prevents us from being completely protocol-compliant. The
  4255. * real solution to this problem is to start using unsigned file offsets in
  4256. * the VFS, but this is a very deep change!
  4257. */
  4258. static inline void
  4259. nfs4_transform_lock_offset(struct file_lock *lock)
  4260. {
  4261. if (lock->fl_start < 0)
  4262. lock->fl_start = OFFSET_MAX;
  4263. if (lock->fl_end < 0)
  4264. lock->fl_end = OFFSET_MAX;
  4265. }
  4266. static void nfsd4_fl_get_owner(struct file_lock *dst, struct file_lock *src)
  4267. {
  4268. struct nfs4_lockowner *lo = (struct nfs4_lockowner *)src->fl_owner;
  4269. dst->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lo->lo_owner));
  4270. }
  4271. static void nfsd4_fl_put_owner(struct file_lock *fl)
  4272. {
  4273. struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
  4274. if (lo) {
  4275. nfs4_put_stateowner(&lo->lo_owner);
  4276. fl->fl_owner = NULL;
  4277. }
  4278. }
  4279. static const struct lock_manager_operations nfsd_posix_mng_ops = {
  4280. .lm_get_owner = nfsd4_fl_get_owner,
  4281. .lm_put_owner = nfsd4_fl_put_owner,
  4282. };
  4283. static inline void
  4284. nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
  4285. {
  4286. struct nfs4_lockowner *lo;
  4287. if (fl->fl_lmops == &nfsd_posix_mng_ops) {
  4288. lo = (struct nfs4_lockowner *) fl->fl_owner;
  4289. deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
  4290. lo->lo_owner.so_owner.len, GFP_KERNEL);
  4291. if (!deny->ld_owner.data)
  4292. /* We just don't care that much */
  4293. goto nevermind;
  4294. deny->ld_owner.len = lo->lo_owner.so_owner.len;
  4295. deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
  4296. } else {
  4297. nevermind:
  4298. deny->ld_owner.len = 0;
  4299. deny->ld_owner.data = NULL;
  4300. deny->ld_clientid.cl_boot = 0;
  4301. deny->ld_clientid.cl_id = 0;
  4302. }
  4303. deny->ld_start = fl->fl_start;
  4304. deny->ld_length = NFS4_MAX_UINT64;
  4305. if (fl->fl_end != NFS4_MAX_UINT64)
  4306. deny->ld_length = fl->fl_end - fl->fl_start + 1;
  4307. deny->ld_type = NFS4_READ_LT;
  4308. if (fl->fl_type != F_RDLCK)
  4309. deny->ld_type = NFS4_WRITE_LT;
  4310. }
  4311. static struct nfs4_lockowner *
  4312. find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
  4313. struct nfs4_client *clp)
  4314. {
  4315. unsigned int strhashval = ownerstr_hashval(owner);
  4316. struct nfs4_stateowner *so;
  4317. lockdep_assert_held(&clp->cl_lock);
  4318. list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
  4319. so_strhash) {
  4320. if (so->so_is_open_owner)
  4321. continue;
  4322. if (same_owner_str(so, owner))
  4323. return lockowner(nfs4_get_stateowner(so));
  4324. }
  4325. return NULL;
  4326. }
  4327. static struct nfs4_lockowner *
  4328. find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
  4329. struct nfs4_client *clp)
  4330. {
  4331. struct nfs4_lockowner *lo;
  4332. spin_lock(&clp->cl_lock);
  4333. lo = find_lockowner_str_locked(clid, owner, clp);
  4334. spin_unlock(&clp->cl_lock);
  4335. return lo;
  4336. }
  4337. static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
  4338. {
  4339. unhash_lockowner_locked(lockowner(sop));
  4340. }
  4341. static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
  4342. {
  4343. struct nfs4_lockowner *lo = lockowner(sop);
  4344. kmem_cache_free(lockowner_slab, lo);
  4345. }
  4346. static const struct nfs4_stateowner_operations lockowner_ops = {
  4347. .so_unhash = nfs4_unhash_lockowner,
  4348. .so_free = nfs4_free_lockowner,
  4349. };
  4350. /*
  4351. * Alloc a lock owner structure.
  4352. * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
  4353. * occurred.
  4354. *
  4355. * strhashval = ownerstr_hashval
  4356. */
  4357. static struct nfs4_lockowner *
  4358. alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
  4359. struct nfs4_ol_stateid *open_stp,
  4360. struct nfsd4_lock *lock)
  4361. {
  4362. struct nfs4_lockowner *lo, *ret;
  4363. lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
  4364. if (!lo)
  4365. return NULL;
  4366. INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
  4367. lo->lo_owner.so_is_open_owner = 0;
  4368. lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
  4369. lo->lo_owner.so_ops = &lockowner_ops;
  4370. spin_lock(&clp->cl_lock);
  4371. ret = find_lockowner_str_locked(&clp->cl_clientid,
  4372. &lock->lk_new_owner, clp);
  4373. if (ret == NULL) {
  4374. list_add(&lo->lo_owner.so_strhash,
  4375. &clp->cl_ownerstr_hashtbl[strhashval]);
  4376. ret = lo;
  4377. } else
  4378. nfs4_free_lockowner(&lo->lo_owner);
  4379. spin_unlock(&clp->cl_lock);
  4380. return lo;
  4381. }
  4382. static void
  4383. init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
  4384. struct nfs4_file *fp, struct inode *inode,
  4385. struct nfs4_ol_stateid *open_stp)
  4386. {
  4387. struct nfs4_client *clp = lo->lo_owner.so_client;
  4388. lockdep_assert_held(&clp->cl_lock);
  4389. atomic_inc(&stp->st_stid.sc_count);
  4390. stp->st_stid.sc_type = NFS4_LOCK_STID;
  4391. stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
  4392. get_nfs4_file(fp);
  4393. stp->st_stid.sc_file = fp;
  4394. stp->st_stid.sc_free = nfs4_free_lock_stateid;
  4395. stp->st_access_bmap = 0;
  4396. stp->st_deny_bmap = open_stp->st_deny_bmap;
  4397. stp->st_openstp = open_stp;
  4398. list_add(&stp->st_locks, &open_stp->st_locks);
  4399. list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
  4400. spin_lock(&fp->fi_lock);
  4401. list_add(&stp->st_perfile, &fp->fi_stateids);
  4402. spin_unlock(&fp->fi_lock);
  4403. }
  4404. static struct nfs4_ol_stateid *
  4405. find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
  4406. {
  4407. struct nfs4_ol_stateid *lst;
  4408. struct nfs4_client *clp = lo->lo_owner.so_client;
  4409. lockdep_assert_held(&clp->cl_lock);
  4410. list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
  4411. if (lst->st_stid.sc_file == fp) {
  4412. atomic_inc(&lst->st_stid.sc_count);
  4413. return lst;
  4414. }
  4415. }
  4416. return NULL;
  4417. }
  4418. static struct nfs4_ol_stateid *
  4419. find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
  4420. struct inode *inode, struct nfs4_ol_stateid *ost,
  4421. bool *new)
  4422. {
  4423. struct nfs4_stid *ns = NULL;
  4424. struct nfs4_ol_stateid *lst;
  4425. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  4426. struct nfs4_client *clp = oo->oo_owner.so_client;
  4427. spin_lock(&clp->cl_lock);
  4428. lst = find_lock_stateid(lo, fi);
  4429. if (lst == NULL) {
  4430. spin_unlock(&clp->cl_lock);
  4431. ns = nfs4_alloc_stid(clp, stateid_slab);
  4432. if (ns == NULL)
  4433. return NULL;
  4434. spin_lock(&clp->cl_lock);
  4435. lst = find_lock_stateid(lo, fi);
  4436. if (likely(!lst)) {
  4437. lst = openlockstateid(ns);
  4438. init_lock_stateid(lst, lo, fi, inode, ost);
  4439. ns = NULL;
  4440. *new = true;
  4441. }
  4442. }
  4443. spin_unlock(&clp->cl_lock);
  4444. if (ns)
  4445. nfs4_put_stid(ns);
  4446. return lst;
  4447. }
  4448. static int
  4449. check_lock_length(u64 offset, u64 length)
  4450. {
  4451. return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
  4452. LOFF_OVERFLOW(offset, length)));
  4453. }
  4454. static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
  4455. {
  4456. struct nfs4_file *fp = lock_stp->st_stid.sc_file;
  4457. lockdep_assert_held(&fp->fi_lock);
  4458. if (test_access(access, lock_stp))
  4459. return;
  4460. __nfs4_file_get_access(fp, access);
  4461. set_access(access, lock_stp);
  4462. }
  4463. static __be32
  4464. lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
  4465. struct nfs4_ol_stateid *ost,
  4466. struct nfsd4_lock *lock,
  4467. struct nfs4_ol_stateid **lst, bool *new)
  4468. {
  4469. __be32 status;
  4470. struct nfs4_file *fi = ost->st_stid.sc_file;
  4471. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  4472. struct nfs4_client *cl = oo->oo_owner.so_client;
  4473. struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
  4474. struct nfs4_lockowner *lo;
  4475. unsigned int strhashval;
  4476. lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
  4477. if (!lo) {
  4478. strhashval = ownerstr_hashval(&lock->v.new.owner);
  4479. lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
  4480. if (lo == NULL)
  4481. return nfserr_jukebox;
  4482. } else {
  4483. /* with an existing lockowner, seqids must be the same */
  4484. status = nfserr_bad_seqid;
  4485. if (!cstate->minorversion &&
  4486. lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
  4487. goto out;
  4488. }
  4489. *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
  4490. if (*lst == NULL) {
  4491. status = nfserr_jukebox;
  4492. goto out;
  4493. }
  4494. status = nfs_ok;
  4495. out:
  4496. nfs4_put_stateowner(&lo->lo_owner);
  4497. return status;
  4498. }
  4499. /*
  4500. * LOCK operation
  4501. */
  4502. __be32
  4503. nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4504. struct nfsd4_lock *lock)
  4505. {
  4506. struct nfs4_openowner *open_sop = NULL;
  4507. struct nfs4_lockowner *lock_sop = NULL;
  4508. struct nfs4_ol_stateid *lock_stp = NULL;
  4509. struct nfs4_ol_stateid *open_stp = NULL;
  4510. struct nfs4_file *fp;
  4511. struct file *filp = NULL;
  4512. struct file_lock *file_lock = NULL;
  4513. struct file_lock *conflock = NULL;
  4514. __be32 status = 0;
  4515. int lkflg;
  4516. int err;
  4517. bool new = false;
  4518. struct net *net = SVC_NET(rqstp);
  4519. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4520. dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
  4521. (long long) lock->lk_offset,
  4522. (long long) lock->lk_length);
  4523. if (check_lock_length(lock->lk_offset, lock->lk_length))
  4524. return nfserr_inval;
  4525. if ((status = fh_verify(rqstp, &cstate->current_fh,
  4526. S_IFREG, NFSD_MAY_LOCK))) {
  4527. dprintk("NFSD: nfsd4_lock: permission denied!\n");
  4528. return status;
  4529. }
  4530. if (lock->lk_is_new) {
  4531. if (nfsd4_has_session(cstate))
  4532. /* See rfc 5661 18.10.3: given clientid is ignored: */
  4533. memcpy(&lock->v.new.clientid,
  4534. &cstate->session->se_client->cl_clientid,
  4535. sizeof(clientid_t));
  4536. status = nfserr_stale_clientid;
  4537. if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
  4538. goto out;
  4539. /* validate and update open stateid and open seqid */
  4540. status = nfs4_preprocess_confirmed_seqid_op(cstate,
  4541. lock->lk_new_open_seqid,
  4542. &lock->lk_new_open_stateid,
  4543. &open_stp, nn);
  4544. if (status)
  4545. goto out;
  4546. open_sop = openowner(open_stp->st_stateowner);
  4547. status = nfserr_bad_stateid;
  4548. if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
  4549. &lock->v.new.clientid))
  4550. goto out;
  4551. status = lookup_or_create_lock_state(cstate, open_stp, lock,
  4552. &lock_stp, &new);
  4553. } else {
  4554. status = nfs4_preprocess_seqid_op(cstate,
  4555. lock->lk_old_lock_seqid,
  4556. &lock->lk_old_lock_stateid,
  4557. NFS4_LOCK_STID, &lock_stp, nn);
  4558. }
  4559. if (status)
  4560. goto out;
  4561. lock_sop = lockowner(lock_stp->st_stateowner);
  4562. lkflg = setlkflg(lock->lk_type);
  4563. status = nfs4_check_openmode(lock_stp, lkflg);
  4564. if (status)
  4565. goto out;
  4566. status = nfserr_grace;
  4567. if (locks_in_grace(net) && !lock->lk_reclaim)
  4568. goto out;
  4569. status = nfserr_no_grace;
  4570. if (!locks_in_grace(net) && lock->lk_reclaim)
  4571. goto out;
  4572. file_lock = locks_alloc_lock();
  4573. if (!file_lock) {
  4574. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  4575. status = nfserr_jukebox;
  4576. goto out;
  4577. }
  4578. fp = lock_stp->st_stid.sc_file;
  4579. switch (lock->lk_type) {
  4580. case NFS4_READ_LT:
  4581. case NFS4_READW_LT:
  4582. spin_lock(&fp->fi_lock);
  4583. filp = find_readable_file_locked(fp);
  4584. if (filp)
  4585. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
  4586. spin_unlock(&fp->fi_lock);
  4587. file_lock->fl_type = F_RDLCK;
  4588. break;
  4589. case NFS4_WRITE_LT:
  4590. case NFS4_WRITEW_LT:
  4591. spin_lock(&fp->fi_lock);
  4592. filp = find_writeable_file_locked(fp);
  4593. if (filp)
  4594. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
  4595. spin_unlock(&fp->fi_lock);
  4596. file_lock->fl_type = F_WRLCK;
  4597. break;
  4598. default:
  4599. status = nfserr_inval;
  4600. goto out;
  4601. }
  4602. if (!filp) {
  4603. status = nfserr_openmode;
  4604. goto out;
  4605. }
  4606. file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
  4607. file_lock->fl_pid = current->tgid;
  4608. file_lock->fl_file = filp;
  4609. file_lock->fl_flags = FL_POSIX;
  4610. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  4611. file_lock->fl_start = lock->lk_offset;
  4612. file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
  4613. nfs4_transform_lock_offset(file_lock);
  4614. conflock = locks_alloc_lock();
  4615. if (!conflock) {
  4616. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  4617. status = nfserr_jukebox;
  4618. goto out;
  4619. }
  4620. err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
  4621. switch (-err) {
  4622. case 0: /* success! */
  4623. update_stateid(&lock_stp->st_stid.sc_stateid);
  4624. memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
  4625. sizeof(stateid_t));
  4626. status = 0;
  4627. break;
  4628. case (EAGAIN): /* conflock holds conflicting lock */
  4629. status = nfserr_denied;
  4630. dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
  4631. nfs4_set_lock_denied(conflock, &lock->lk_denied);
  4632. break;
  4633. case (EDEADLK):
  4634. status = nfserr_deadlock;
  4635. break;
  4636. default:
  4637. dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
  4638. status = nfserrno(err);
  4639. break;
  4640. }
  4641. out:
  4642. if (filp)
  4643. fput(filp);
  4644. if (lock_stp) {
  4645. /* Bump seqid manually if the 4.0 replay owner is openowner */
  4646. if (cstate->replay_owner &&
  4647. cstate->replay_owner != &lock_sop->lo_owner &&
  4648. seqid_mutating_err(ntohl(status)))
  4649. lock_sop->lo_owner.so_seqid++;
  4650. /*
  4651. * If this is a new, never-before-used stateid, and we are
  4652. * returning an error, then just go ahead and release it.
  4653. */
  4654. if (status && new)
  4655. release_lock_stateid(lock_stp);
  4656. nfs4_put_stid(&lock_stp->st_stid);
  4657. }
  4658. if (open_stp)
  4659. nfs4_put_stid(&open_stp->st_stid);
  4660. nfsd4_bump_seqid(cstate, status);
  4661. if (file_lock)
  4662. locks_free_lock(file_lock);
  4663. if (conflock)
  4664. locks_free_lock(conflock);
  4665. return status;
  4666. }
  4667. /*
  4668. * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
  4669. * so we do a temporary open here just to get an open file to pass to
  4670. * vfs_test_lock. (Arguably perhaps test_lock should be done with an
  4671. * inode operation.)
  4672. */
  4673. static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
  4674. {
  4675. struct file *file;
  4676. __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  4677. if (!err) {
  4678. err = nfserrno(vfs_test_lock(file, lock));
  4679. nfsd_close(file);
  4680. }
  4681. return err;
  4682. }
  4683. /*
  4684. * LOCKT operation
  4685. */
  4686. __be32
  4687. nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4688. struct nfsd4_lockt *lockt)
  4689. {
  4690. struct file_lock *file_lock = NULL;
  4691. struct nfs4_lockowner *lo = NULL;
  4692. __be32 status;
  4693. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  4694. if (locks_in_grace(SVC_NET(rqstp)))
  4695. return nfserr_grace;
  4696. if (check_lock_length(lockt->lt_offset, lockt->lt_length))
  4697. return nfserr_inval;
  4698. if (!nfsd4_has_session(cstate)) {
  4699. status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
  4700. if (status)
  4701. goto out;
  4702. }
  4703. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  4704. goto out;
  4705. file_lock = locks_alloc_lock();
  4706. if (!file_lock) {
  4707. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  4708. status = nfserr_jukebox;
  4709. goto out;
  4710. }
  4711. switch (lockt->lt_type) {
  4712. case NFS4_READ_LT:
  4713. case NFS4_READW_LT:
  4714. file_lock->fl_type = F_RDLCK;
  4715. break;
  4716. case NFS4_WRITE_LT:
  4717. case NFS4_WRITEW_LT:
  4718. file_lock->fl_type = F_WRLCK;
  4719. break;
  4720. default:
  4721. dprintk("NFSD: nfs4_lockt: bad lock type!\n");
  4722. status = nfserr_inval;
  4723. goto out;
  4724. }
  4725. lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
  4726. cstate->clp);
  4727. if (lo)
  4728. file_lock->fl_owner = (fl_owner_t)lo;
  4729. file_lock->fl_pid = current->tgid;
  4730. file_lock->fl_flags = FL_POSIX;
  4731. file_lock->fl_start = lockt->lt_offset;
  4732. file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
  4733. nfs4_transform_lock_offset(file_lock);
  4734. status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
  4735. if (status)
  4736. goto out;
  4737. if (file_lock->fl_type != F_UNLCK) {
  4738. status = nfserr_denied;
  4739. nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
  4740. }
  4741. out:
  4742. if (lo)
  4743. nfs4_put_stateowner(&lo->lo_owner);
  4744. if (file_lock)
  4745. locks_free_lock(file_lock);
  4746. return status;
  4747. }
  4748. __be32
  4749. nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4750. struct nfsd4_locku *locku)
  4751. {
  4752. struct nfs4_ol_stateid *stp;
  4753. struct file *filp = NULL;
  4754. struct file_lock *file_lock = NULL;
  4755. __be32 status;
  4756. int err;
  4757. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  4758. dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
  4759. (long long) locku->lu_offset,
  4760. (long long) locku->lu_length);
  4761. if (check_lock_length(locku->lu_offset, locku->lu_length))
  4762. return nfserr_inval;
  4763. status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
  4764. &locku->lu_stateid, NFS4_LOCK_STID,
  4765. &stp, nn);
  4766. if (status)
  4767. goto out;
  4768. filp = find_any_file(stp->st_stid.sc_file);
  4769. if (!filp) {
  4770. status = nfserr_lock_range;
  4771. goto put_stateid;
  4772. }
  4773. file_lock = locks_alloc_lock();
  4774. if (!file_lock) {
  4775. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  4776. status = nfserr_jukebox;
  4777. goto fput;
  4778. }
  4779. file_lock->fl_type = F_UNLCK;
  4780. file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
  4781. file_lock->fl_pid = current->tgid;
  4782. file_lock->fl_file = filp;
  4783. file_lock->fl_flags = FL_POSIX;
  4784. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  4785. file_lock->fl_start = locku->lu_offset;
  4786. file_lock->fl_end = last_byte_offset(locku->lu_offset,
  4787. locku->lu_length);
  4788. nfs4_transform_lock_offset(file_lock);
  4789. err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
  4790. if (err) {
  4791. dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
  4792. goto out_nfserr;
  4793. }
  4794. update_stateid(&stp->st_stid.sc_stateid);
  4795. memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
  4796. fput:
  4797. fput(filp);
  4798. put_stateid:
  4799. nfs4_put_stid(&stp->st_stid);
  4800. out:
  4801. nfsd4_bump_seqid(cstate, status);
  4802. if (file_lock)
  4803. locks_free_lock(file_lock);
  4804. return status;
  4805. out_nfserr:
  4806. status = nfserrno(err);
  4807. goto fput;
  4808. }
  4809. /*
  4810. * returns
  4811. * true: locks held by lockowner
  4812. * false: no locks held by lockowner
  4813. */
  4814. static bool
  4815. check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
  4816. {
  4817. struct file_lock **flpp;
  4818. int status = false;
  4819. struct file *filp = find_any_file(fp);
  4820. struct inode *inode;
  4821. if (!filp) {
  4822. /* Any valid lock stateid should have some sort of access */
  4823. WARN_ON_ONCE(1);
  4824. return status;
  4825. }
  4826. inode = file_inode(filp);
  4827. spin_lock(&inode->i_lock);
  4828. for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
  4829. if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
  4830. status = true;
  4831. break;
  4832. }
  4833. }
  4834. spin_unlock(&inode->i_lock);
  4835. fput(filp);
  4836. return status;
  4837. }
  4838. __be32
  4839. nfsd4_release_lockowner(struct svc_rqst *rqstp,
  4840. struct nfsd4_compound_state *cstate,
  4841. struct nfsd4_release_lockowner *rlockowner)
  4842. {
  4843. clientid_t *clid = &rlockowner->rl_clientid;
  4844. struct nfs4_stateowner *sop;
  4845. struct nfs4_lockowner *lo = NULL;
  4846. struct nfs4_ol_stateid *stp;
  4847. struct xdr_netobj *owner = &rlockowner->rl_owner;
  4848. unsigned int hashval = ownerstr_hashval(owner);
  4849. __be32 status;
  4850. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  4851. struct nfs4_client *clp;
  4852. dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
  4853. clid->cl_boot, clid->cl_id);
  4854. status = lookup_clientid(clid, cstate, nn);
  4855. if (status)
  4856. return status;
  4857. clp = cstate->clp;
  4858. /* Find the matching lock stateowner */
  4859. spin_lock(&clp->cl_lock);
  4860. list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
  4861. so_strhash) {
  4862. if (sop->so_is_open_owner || !same_owner_str(sop, owner))
  4863. continue;
  4864. /* see if there are still any locks associated with it */
  4865. lo = lockowner(sop);
  4866. list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
  4867. if (check_for_locks(stp->st_stid.sc_file, lo)) {
  4868. status = nfserr_locks_held;
  4869. spin_unlock(&clp->cl_lock);
  4870. return status;
  4871. }
  4872. }
  4873. nfs4_get_stateowner(sop);
  4874. break;
  4875. }
  4876. spin_unlock(&clp->cl_lock);
  4877. if (lo)
  4878. release_lockowner(lo);
  4879. return status;
  4880. }
  4881. static inline struct nfs4_client_reclaim *
  4882. alloc_reclaim(void)
  4883. {
  4884. return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
  4885. }
  4886. bool
  4887. nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
  4888. {
  4889. struct nfs4_client_reclaim *crp;
  4890. crp = nfsd4_find_reclaim_client(name, nn);
  4891. return (crp && crp->cr_clp);
  4892. }
  4893. /*
  4894. * failure => all reset bets are off, nfserr_no_grace...
  4895. */
  4896. struct nfs4_client_reclaim *
  4897. nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
  4898. {
  4899. unsigned int strhashval;
  4900. struct nfs4_client_reclaim *crp;
  4901. dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
  4902. crp = alloc_reclaim();
  4903. if (crp) {
  4904. strhashval = clientstr_hashval(name);
  4905. INIT_LIST_HEAD(&crp->cr_strhash);
  4906. list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
  4907. memcpy(crp->cr_recdir, name, HEXDIR_LEN);
  4908. crp->cr_clp = NULL;
  4909. nn->reclaim_str_hashtbl_size++;
  4910. }
  4911. return crp;
  4912. }
  4913. void
  4914. nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
  4915. {
  4916. list_del(&crp->cr_strhash);
  4917. kfree(crp);
  4918. nn->reclaim_str_hashtbl_size--;
  4919. }
  4920. void
  4921. nfs4_release_reclaim(struct nfsd_net *nn)
  4922. {
  4923. struct nfs4_client_reclaim *crp = NULL;
  4924. int i;
  4925. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  4926. while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
  4927. crp = list_entry(nn->reclaim_str_hashtbl[i].next,
  4928. struct nfs4_client_reclaim, cr_strhash);
  4929. nfs4_remove_reclaim_record(crp, nn);
  4930. }
  4931. }
  4932. WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
  4933. }
  4934. /*
  4935. * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
  4936. struct nfs4_client_reclaim *
  4937. nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
  4938. {
  4939. unsigned int strhashval;
  4940. struct nfs4_client_reclaim *crp = NULL;
  4941. dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
  4942. strhashval = clientstr_hashval(recdir);
  4943. list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
  4944. if (same_name(crp->cr_recdir, recdir)) {
  4945. return crp;
  4946. }
  4947. }
  4948. return NULL;
  4949. }
  4950. /*
  4951. * Called from OPEN. Look for clientid in reclaim list.
  4952. */
  4953. __be32
  4954. nfs4_check_open_reclaim(clientid_t *clid,
  4955. struct nfsd4_compound_state *cstate,
  4956. struct nfsd_net *nn)
  4957. {
  4958. __be32 status;
  4959. /* find clientid in conf_id_hashtbl */
  4960. status = lookup_clientid(clid, cstate, nn);
  4961. if (status)
  4962. return nfserr_reclaim_bad;
  4963. if (nfsd4_client_record_check(cstate->clp))
  4964. return nfserr_reclaim_bad;
  4965. return nfs_ok;
  4966. }
  4967. #ifdef CONFIG_NFSD_FAULT_INJECTION
  4968. static inline void
  4969. put_client(struct nfs4_client *clp)
  4970. {
  4971. atomic_dec(&clp->cl_refcount);
  4972. }
  4973. static struct nfs4_client *
  4974. nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
  4975. {
  4976. struct nfs4_client *clp;
  4977. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  4978. nfsd_net_id);
  4979. if (!nfsd_netns_ready(nn))
  4980. return NULL;
  4981. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  4982. if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
  4983. return clp;
  4984. }
  4985. return NULL;
  4986. }
  4987. u64
  4988. nfsd_inject_print_clients(void)
  4989. {
  4990. struct nfs4_client *clp;
  4991. u64 count = 0;
  4992. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  4993. nfsd_net_id);
  4994. char buf[INET6_ADDRSTRLEN];
  4995. if (!nfsd_netns_ready(nn))
  4996. return 0;
  4997. spin_lock(&nn->client_lock);
  4998. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  4999. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  5000. pr_info("NFS Client: %s\n", buf);
  5001. ++count;
  5002. }
  5003. spin_unlock(&nn->client_lock);
  5004. return count;
  5005. }
  5006. u64
  5007. nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
  5008. {
  5009. u64 count = 0;
  5010. struct nfs4_client *clp;
  5011. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5012. nfsd_net_id);
  5013. if (!nfsd_netns_ready(nn))
  5014. return count;
  5015. spin_lock(&nn->client_lock);
  5016. clp = nfsd_find_client(addr, addr_size);
  5017. if (clp) {
  5018. if (mark_client_expired_locked(clp) == nfs_ok)
  5019. ++count;
  5020. else
  5021. clp = NULL;
  5022. }
  5023. spin_unlock(&nn->client_lock);
  5024. if (clp)
  5025. expire_client(clp);
  5026. return count;
  5027. }
  5028. u64
  5029. nfsd_inject_forget_clients(u64 max)
  5030. {
  5031. u64 count = 0;
  5032. struct nfs4_client *clp, *next;
  5033. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5034. nfsd_net_id);
  5035. LIST_HEAD(reaplist);
  5036. if (!nfsd_netns_ready(nn))
  5037. return count;
  5038. spin_lock(&nn->client_lock);
  5039. list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
  5040. if (mark_client_expired_locked(clp) == nfs_ok) {
  5041. list_add(&clp->cl_lru, &reaplist);
  5042. if (max != 0 && ++count >= max)
  5043. break;
  5044. }
  5045. }
  5046. spin_unlock(&nn->client_lock);
  5047. list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
  5048. expire_client(clp);
  5049. return count;
  5050. }
  5051. static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
  5052. const char *type)
  5053. {
  5054. char buf[INET6_ADDRSTRLEN];
  5055. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  5056. printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
  5057. }
  5058. static void
  5059. nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
  5060. struct list_head *collect)
  5061. {
  5062. struct nfs4_client *clp = lst->st_stid.sc_client;
  5063. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5064. nfsd_net_id);
  5065. if (!collect)
  5066. return;
  5067. lockdep_assert_held(&nn->client_lock);
  5068. atomic_inc(&clp->cl_refcount);
  5069. list_add(&lst->st_locks, collect);
  5070. }
  5071. static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
  5072. struct list_head *collect,
  5073. void (*func)(struct nfs4_ol_stateid *))
  5074. {
  5075. struct nfs4_openowner *oop;
  5076. struct nfs4_ol_stateid *stp, *st_next;
  5077. struct nfs4_ol_stateid *lst, *lst_next;
  5078. u64 count = 0;
  5079. spin_lock(&clp->cl_lock);
  5080. list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
  5081. list_for_each_entry_safe(stp, st_next,
  5082. &oop->oo_owner.so_stateids, st_perstateowner) {
  5083. list_for_each_entry_safe(lst, lst_next,
  5084. &stp->st_locks, st_locks) {
  5085. if (func) {
  5086. func(lst);
  5087. nfsd_inject_add_lock_to_list(lst,
  5088. collect);
  5089. }
  5090. ++count;
  5091. /*
  5092. * Despite the fact that these functions deal
  5093. * with 64-bit integers for "count", we must
  5094. * ensure that it doesn't blow up the
  5095. * clp->cl_refcount. Throw a warning if we
  5096. * start to approach INT_MAX here.
  5097. */
  5098. WARN_ON_ONCE(count == (INT_MAX / 2));
  5099. if (count == max)
  5100. goto out;
  5101. }
  5102. }
  5103. }
  5104. out:
  5105. spin_unlock(&clp->cl_lock);
  5106. return count;
  5107. }
  5108. static u64
  5109. nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
  5110. u64 max)
  5111. {
  5112. return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
  5113. }
  5114. static u64
  5115. nfsd_print_client_locks(struct nfs4_client *clp)
  5116. {
  5117. u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
  5118. nfsd_print_count(clp, count, "locked files");
  5119. return count;
  5120. }
  5121. u64
  5122. nfsd_inject_print_locks(void)
  5123. {
  5124. struct nfs4_client *clp;
  5125. u64 count = 0;
  5126. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5127. nfsd_net_id);
  5128. if (!nfsd_netns_ready(nn))
  5129. return 0;
  5130. spin_lock(&nn->client_lock);
  5131. list_for_each_entry(clp, &nn->client_lru, cl_lru)
  5132. count += nfsd_print_client_locks(clp);
  5133. spin_unlock(&nn->client_lock);
  5134. return count;
  5135. }
  5136. static void
  5137. nfsd_reap_locks(struct list_head *reaplist)
  5138. {
  5139. struct nfs4_client *clp;
  5140. struct nfs4_ol_stateid *stp, *next;
  5141. list_for_each_entry_safe(stp, next, reaplist, st_locks) {
  5142. list_del_init(&stp->st_locks);
  5143. clp = stp->st_stid.sc_client;
  5144. nfs4_put_stid(&stp->st_stid);
  5145. put_client(clp);
  5146. }
  5147. }
  5148. u64
  5149. nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
  5150. {
  5151. unsigned int count = 0;
  5152. struct nfs4_client *clp;
  5153. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5154. nfsd_net_id);
  5155. LIST_HEAD(reaplist);
  5156. if (!nfsd_netns_ready(nn))
  5157. return count;
  5158. spin_lock(&nn->client_lock);
  5159. clp = nfsd_find_client(addr, addr_size);
  5160. if (clp)
  5161. count = nfsd_collect_client_locks(clp, &reaplist, 0);
  5162. spin_unlock(&nn->client_lock);
  5163. nfsd_reap_locks(&reaplist);
  5164. return count;
  5165. }
  5166. u64
  5167. nfsd_inject_forget_locks(u64 max)
  5168. {
  5169. u64 count = 0;
  5170. struct nfs4_client *clp;
  5171. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5172. nfsd_net_id);
  5173. LIST_HEAD(reaplist);
  5174. if (!nfsd_netns_ready(nn))
  5175. return count;
  5176. spin_lock(&nn->client_lock);
  5177. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  5178. count += nfsd_collect_client_locks(clp, &reaplist, max - count);
  5179. if (max != 0 && count >= max)
  5180. break;
  5181. }
  5182. spin_unlock(&nn->client_lock);
  5183. nfsd_reap_locks(&reaplist);
  5184. return count;
  5185. }
  5186. static u64
  5187. nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
  5188. struct list_head *collect,
  5189. void (*func)(struct nfs4_openowner *))
  5190. {
  5191. struct nfs4_openowner *oop, *next;
  5192. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5193. nfsd_net_id);
  5194. u64 count = 0;
  5195. lockdep_assert_held(&nn->client_lock);
  5196. spin_lock(&clp->cl_lock);
  5197. list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
  5198. if (func) {
  5199. func(oop);
  5200. if (collect) {
  5201. atomic_inc(&clp->cl_refcount);
  5202. list_add(&oop->oo_perclient, collect);
  5203. }
  5204. }
  5205. ++count;
  5206. /*
  5207. * Despite the fact that these functions deal with
  5208. * 64-bit integers for "count", we must ensure that
  5209. * it doesn't blow up the clp->cl_refcount. Throw a
  5210. * warning if we start to approach INT_MAX here.
  5211. */
  5212. WARN_ON_ONCE(count == (INT_MAX / 2));
  5213. if (count == max)
  5214. break;
  5215. }
  5216. spin_unlock(&clp->cl_lock);
  5217. return count;
  5218. }
  5219. static u64
  5220. nfsd_print_client_openowners(struct nfs4_client *clp)
  5221. {
  5222. u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
  5223. nfsd_print_count(clp, count, "openowners");
  5224. return count;
  5225. }
  5226. static u64
  5227. nfsd_collect_client_openowners(struct nfs4_client *clp,
  5228. struct list_head *collect, u64 max)
  5229. {
  5230. return nfsd_foreach_client_openowner(clp, max, collect,
  5231. unhash_openowner_locked);
  5232. }
  5233. u64
  5234. nfsd_inject_print_openowners(void)
  5235. {
  5236. struct nfs4_client *clp;
  5237. u64 count = 0;
  5238. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5239. nfsd_net_id);
  5240. if (!nfsd_netns_ready(nn))
  5241. return 0;
  5242. spin_lock(&nn->client_lock);
  5243. list_for_each_entry(clp, &nn->client_lru, cl_lru)
  5244. count += nfsd_print_client_openowners(clp);
  5245. spin_unlock(&nn->client_lock);
  5246. return count;
  5247. }
  5248. static void
  5249. nfsd_reap_openowners(struct list_head *reaplist)
  5250. {
  5251. struct nfs4_client *clp;
  5252. struct nfs4_openowner *oop, *next;
  5253. list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
  5254. list_del_init(&oop->oo_perclient);
  5255. clp = oop->oo_owner.so_client;
  5256. release_openowner(oop);
  5257. put_client(clp);
  5258. }
  5259. }
  5260. u64
  5261. nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
  5262. size_t addr_size)
  5263. {
  5264. unsigned int count = 0;
  5265. struct nfs4_client *clp;
  5266. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5267. nfsd_net_id);
  5268. LIST_HEAD(reaplist);
  5269. if (!nfsd_netns_ready(nn))
  5270. return count;
  5271. spin_lock(&nn->client_lock);
  5272. clp = nfsd_find_client(addr, addr_size);
  5273. if (clp)
  5274. count = nfsd_collect_client_openowners(clp, &reaplist, 0);
  5275. spin_unlock(&nn->client_lock);
  5276. nfsd_reap_openowners(&reaplist);
  5277. return count;
  5278. }
  5279. u64
  5280. nfsd_inject_forget_openowners(u64 max)
  5281. {
  5282. u64 count = 0;
  5283. struct nfs4_client *clp;
  5284. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5285. nfsd_net_id);
  5286. LIST_HEAD(reaplist);
  5287. if (!nfsd_netns_ready(nn))
  5288. return count;
  5289. spin_lock(&nn->client_lock);
  5290. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  5291. count += nfsd_collect_client_openowners(clp, &reaplist,
  5292. max - count);
  5293. if (max != 0 && count >= max)
  5294. break;
  5295. }
  5296. spin_unlock(&nn->client_lock);
  5297. nfsd_reap_openowners(&reaplist);
  5298. return count;
  5299. }
  5300. static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
  5301. struct list_head *victims)
  5302. {
  5303. struct nfs4_delegation *dp, *next;
  5304. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5305. nfsd_net_id);
  5306. u64 count = 0;
  5307. lockdep_assert_held(&nn->client_lock);
  5308. spin_lock(&state_lock);
  5309. list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
  5310. if (victims) {
  5311. /*
  5312. * It's not safe to mess with delegations that have a
  5313. * non-zero dl_time. They might have already been broken
  5314. * and could be processed by the laundromat outside of
  5315. * the state_lock. Just leave them be.
  5316. */
  5317. if (dp->dl_time != 0)
  5318. continue;
  5319. atomic_inc(&clp->cl_refcount);
  5320. unhash_delegation_locked(dp);
  5321. list_add(&dp->dl_recall_lru, victims);
  5322. }
  5323. ++count;
  5324. /*
  5325. * Despite the fact that these functions deal with
  5326. * 64-bit integers for "count", we must ensure that
  5327. * it doesn't blow up the clp->cl_refcount. Throw a
  5328. * warning if we start to approach INT_MAX here.
  5329. */
  5330. WARN_ON_ONCE(count == (INT_MAX / 2));
  5331. if (count == max)
  5332. break;
  5333. }
  5334. spin_unlock(&state_lock);
  5335. return count;
  5336. }
  5337. static u64
  5338. nfsd_print_client_delegations(struct nfs4_client *clp)
  5339. {
  5340. u64 count = nfsd_find_all_delegations(clp, 0, NULL);
  5341. nfsd_print_count(clp, count, "delegations");
  5342. return count;
  5343. }
  5344. u64
  5345. nfsd_inject_print_delegations(void)
  5346. {
  5347. struct nfs4_client *clp;
  5348. u64 count = 0;
  5349. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5350. nfsd_net_id);
  5351. if (!nfsd_netns_ready(nn))
  5352. return 0;
  5353. spin_lock(&nn->client_lock);
  5354. list_for_each_entry(clp, &nn->client_lru, cl_lru)
  5355. count += nfsd_print_client_delegations(clp);
  5356. spin_unlock(&nn->client_lock);
  5357. return count;
  5358. }
  5359. static void
  5360. nfsd_forget_delegations(struct list_head *reaplist)
  5361. {
  5362. struct nfs4_client *clp;
  5363. struct nfs4_delegation *dp, *next;
  5364. list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
  5365. list_del_init(&dp->dl_recall_lru);
  5366. clp = dp->dl_stid.sc_client;
  5367. revoke_delegation(dp);
  5368. put_client(clp);
  5369. }
  5370. }
  5371. u64
  5372. nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
  5373. size_t addr_size)
  5374. {
  5375. u64 count = 0;
  5376. struct nfs4_client *clp;
  5377. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5378. nfsd_net_id);
  5379. LIST_HEAD(reaplist);
  5380. if (!nfsd_netns_ready(nn))
  5381. return count;
  5382. spin_lock(&nn->client_lock);
  5383. clp = nfsd_find_client(addr, addr_size);
  5384. if (clp)
  5385. count = nfsd_find_all_delegations(clp, 0, &reaplist);
  5386. spin_unlock(&nn->client_lock);
  5387. nfsd_forget_delegations(&reaplist);
  5388. return count;
  5389. }
  5390. u64
  5391. nfsd_inject_forget_delegations(u64 max)
  5392. {
  5393. u64 count = 0;
  5394. struct nfs4_client *clp;
  5395. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5396. nfsd_net_id);
  5397. LIST_HEAD(reaplist);
  5398. if (!nfsd_netns_ready(nn))
  5399. return count;
  5400. spin_lock(&nn->client_lock);
  5401. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  5402. count += nfsd_find_all_delegations(clp, max - count, &reaplist);
  5403. if (max != 0 && count >= max)
  5404. break;
  5405. }
  5406. spin_unlock(&nn->client_lock);
  5407. nfsd_forget_delegations(&reaplist);
  5408. return count;
  5409. }
  5410. static void
  5411. nfsd_recall_delegations(struct list_head *reaplist)
  5412. {
  5413. struct nfs4_client *clp;
  5414. struct nfs4_delegation *dp, *next;
  5415. list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
  5416. list_del_init(&dp->dl_recall_lru);
  5417. clp = dp->dl_stid.sc_client;
  5418. /*
  5419. * We skipped all entries that had a zero dl_time before,
  5420. * so we can now reset the dl_time back to 0. If a delegation
  5421. * break comes in now, then it won't make any difference since
  5422. * we're recalling it either way.
  5423. */
  5424. spin_lock(&state_lock);
  5425. dp->dl_time = 0;
  5426. spin_unlock(&state_lock);
  5427. nfsd_break_one_deleg(dp);
  5428. put_client(clp);
  5429. }
  5430. }
  5431. u64
  5432. nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
  5433. size_t addr_size)
  5434. {
  5435. u64 count = 0;
  5436. struct nfs4_client *clp;
  5437. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5438. nfsd_net_id);
  5439. LIST_HEAD(reaplist);
  5440. if (!nfsd_netns_ready(nn))
  5441. return count;
  5442. spin_lock(&nn->client_lock);
  5443. clp = nfsd_find_client(addr, addr_size);
  5444. if (clp)
  5445. count = nfsd_find_all_delegations(clp, 0, &reaplist);
  5446. spin_unlock(&nn->client_lock);
  5447. nfsd_recall_delegations(&reaplist);
  5448. return count;
  5449. }
  5450. u64
  5451. nfsd_inject_recall_delegations(u64 max)
  5452. {
  5453. u64 count = 0;
  5454. struct nfs4_client *clp, *next;
  5455. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5456. nfsd_net_id);
  5457. LIST_HEAD(reaplist);
  5458. if (!nfsd_netns_ready(nn))
  5459. return count;
  5460. spin_lock(&nn->client_lock);
  5461. list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
  5462. count += nfsd_find_all_delegations(clp, max - count, &reaplist);
  5463. if (max != 0 && ++count >= max)
  5464. break;
  5465. }
  5466. spin_unlock(&nn->client_lock);
  5467. nfsd_recall_delegations(&reaplist);
  5468. return count;
  5469. }
  5470. #endif /* CONFIG_NFSD_FAULT_INJECTION */
  5471. /*
  5472. * Since the lifetime of a delegation isn't limited to that of an open, a
  5473. * client may quite reasonably hang on to a delegation as long as it has
  5474. * the inode cached. This becomes an obvious problem the first time a
  5475. * client's inode cache approaches the size of the server's total memory.
  5476. *
  5477. * For now we avoid this problem by imposing a hard limit on the number
  5478. * of delegations, which varies according to the server's memory size.
  5479. */
  5480. static void
  5481. set_max_delegations(void)
  5482. {
  5483. /*
  5484. * Allow at most 4 delegations per megabyte of RAM. Quick
  5485. * estimates suggest that in the worst case (where every delegation
  5486. * is for a different inode), a delegation could take about 1.5K,
  5487. * giving a worst case usage of about 6% of memory.
  5488. */
  5489. max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
  5490. }
  5491. static int nfs4_state_create_net(struct net *net)
  5492. {
  5493. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  5494. int i;
  5495. nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  5496. CLIENT_HASH_SIZE, GFP_KERNEL);
  5497. if (!nn->conf_id_hashtbl)
  5498. goto err;
  5499. nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  5500. CLIENT_HASH_SIZE, GFP_KERNEL);
  5501. if (!nn->unconf_id_hashtbl)
  5502. goto err_unconf_id;
  5503. nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
  5504. SESSION_HASH_SIZE, GFP_KERNEL);
  5505. if (!nn->sessionid_hashtbl)
  5506. goto err_sessionid;
  5507. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  5508. INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
  5509. INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
  5510. }
  5511. for (i = 0; i < SESSION_HASH_SIZE; i++)
  5512. INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
  5513. nn->conf_name_tree = RB_ROOT;
  5514. nn->unconf_name_tree = RB_ROOT;
  5515. INIT_LIST_HEAD(&nn->client_lru);
  5516. INIT_LIST_HEAD(&nn->close_lru);
  5517. INIT_LIST_HEAD(&nn->del_recall_lru);
  5518. spin_lock_init(&nn->client_lock);
  5519. INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
  5520. get_net(net);
  5521. return 0;
  5522. err_sessionid:
  5523. kfree(nn->unconf_id_hashtbl);
  5524. err_unconf_id:
  5525. kfree(nn->conf_id_hashtbl);
  5526. err:
  5527. return -ENOMEM;
  5528. }
  5529. static void
  5530. nfs4_state_destroy_net(struct net *net)
  5531. {
  5532. int i;
  5533. struct nfs4_client *clp = NULL;
  5534. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  5535. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  5536. while (!list_empty(&nn->conf_id_hashtbl[i])) {
  5537. clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  5538. destroy_client(clp);
  5539. }
  5540. }
  5541. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  5542. while (!list_empty(&nn->unconf_id_hashtbl[i])) {
  5543. clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  5544. destroy_client(clp);
  5545. }
  5546. }
  5547. kfree(nn->sessionid_hashtbl);
  5548. kfree(nn->unconf_id_hashtbl);
  5549. kfree(nn->conf_id_hashtbl);
  5550. put_net(net);
  5551. }
  5552. int
  5553. nfs4_state_start_net(struct net *net)
  5554. {
  5555. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  5556. int ret;
  5557. ret = nfs4_state_create_net(net);
  5558. if (ret)
  5559. return ret;
  5560. nfsd4_client_tracking_init(net);
  5561. nn->boot_time = get_seconds();
  5562. locks_start_grace(net, &nn->nfsd4_manager);
  5563. nn->grace_ended = false;
  5564. printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
  5565. nn->nfsd4_grace, net);
  5566. queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
  5567. return 0;
  5568. }
  5569. /* initialization to perform when the nfsd service is started: */
  5570. int
  5571. nfs4_state_start(void)
  5572. {
  5573. int ret;
  5574. ret = set_callback_cred();
  5575. if (ret)
  5576. return -ENOMEM;
  5577. laundry_wq = create_singlethread_workqueue("nfsd4");
  5578. if (laundry_wq == NULL) {
  5579. ret = -ENOMEM;
  5580. goto out_recovery;
  5581. }
  5582. ret = nfsd4_create_callback_queue();
  5583. if (ret)
  5584. goto out_free_laundry;
  5585. set_max_delegations();
  5586. return 0;
  5587. out_free_laundry:
  5588. destroy_workqueue(laundry_wq);
  5589. out_recovery:
  5590. return ret;
  5591. }
  5592. void
  5593. nfs4_state_shutdown_net(struct net *net)
  5594. {
  5595. struct nfs4_delegation *dp = NULL;
  5596. struct list_head *pos, *next, reaplist;
  5597. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  5598. cancel_delayed_work_sync(&nn->laundromat_work);
  5599. locks_end_grace(&nn->nfsd4_manager);
  5600. INIT_LIST_HEAD(&reaplist);
  5601. spin_lock(&state_lock);
  5602. list_for_each_safe(pos, next, &nn->del_recall_lru) {
  5603. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  5604. unhash_delegation_locked(dp);
  5605. list_add(&dp->dl_recall_lru, &reaplist);
  5606. }
  5607. spin_unlock(&state_lock);
  5608. list_for_each_safe(pos, next, &reaplist) {
  5609. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  5610. list_del_init(&dp->dl_recall_lru);
  5611. nfs4_put_deleg_lease(dp->dl_stid.sc_file);
  5612. nfs4_put_stid(&dp->dl_stid);
  5613. }
  5614. nfsd4_client_tracking_exit(net);
  5615. nfs4_state_destroy_net(net);
  5616. }
  5617. void
  5618. nfs4_state_shutdown(void)
  5619. {
  5620. destroy_workqueue(laundry_wq);
  5621. nfsd4_destroy_callback_queue();
  5622. }
  5623. static void
  5624. get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  5625. {
  5626. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
  5627. memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
  5628. }
  5629. static void
  5630. put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  5631. {
  5632. if (cstate->minorversion) {
  5633. memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
  5634. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  5635. }
  5636. }
  5637. void
  5638. clear_current_stateid(struct nfsd4_compound_state *cstate)
  5639. {
  5640. CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  5641. }
  5642. /*
  5643. * functions to set current state id
  5644. */
  5645. void
  5646. nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  5647. {
  5648. put_stateid(cstate, &odp->od_stateid);
  5649. }
  5650. void
  5651. nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  5652. {
  5653. put_stateid(cstate, &open->op_stateid);
  5654. }
  5655. void
  5656. nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  5657. {
  5658. put_stateid(cstate, &close->cl_stateid);
  5659. }
  5660. void
  5661. nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
  5662. {
  5663. put_stateid(cstate, &lock->lk_resp_stateid);
  5664. }
  5665. /*
  5666. * functions to consume current state id
  5667. */
  5668. void
  5669. nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  5670. {
  5671. get_stateid(cstate, &odp->od_stateid);
  5672. }
  5673. void
  5674. nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
  5675. {
  5676. get_stateid(cstate, &drp->dr_stateid);
  5677. }
  5678. void
  5679. nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
  5680. {
  5681. get_stateid(cstate, &fsp->fr_stateid);
  5682. }
  5683. void
  5684. nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
  5685. {
  5686. get_stateid(cstate, &setattr->sa_stateid);
  5687. }
  5688. void
  5689. nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  5690. {
  5691. get_stateid(cstate, &close->cl_stateid);
  5692. }
  5693. void
  5694. nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
  5695. {
  5696. get_stateid(cstate, &locku->lu_stateid);
  5697. }
  5698. void
  5699. nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
  5700. {
  5701. get_stateid(cstate, &read->rd_stateid);
  5702. }
  5703. void
  5704. nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
  5705. {
  5706. get_stateid(cstate, &write->wr_stateid);
  5707. }