nfs4state.c 186 KB

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