nfs4state.c 186 KB

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