nfs4state.c 184 KB

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