nfs4state.c 175 KB

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