nfs4state.c 175 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948
  1. /*
  2. * Copyright (c) 2001 The Regents of the University of Michigan.
  3. * All rights reserved.
  4. *
  5. * Kendrick Smith <kmsmith@umich.edu>
  6. * Andy Adamson <kandros@umich.edu>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. */
  34. #include <linux/file.h>
  35. #include <linux/fs.h>
  36. #include <linux/slab.h>
  37. #include <linux/namei.h>
  38. #include <linux/swap.h>
  39. #include <linux/pagemap.h>
  40. #include <linux/ratelimit.h>
  41. #include <linux/sunrpc/svcauth_gss.h>
  42. #include <linux/sunrpc/addr.h>
  43. #include <linux/jhash.h>
  44. #include "xdr4.h"
  45. #include "xdr4cb.h"
  46. #include "vfs.h"
  47. #include "current_stateid.h"
  48. #include "netns.h"
  49. #include "pnfs.h"
  50. #define NFSDDBG_FACILITY NFSDDBG_PROC
  51. #define all_ones {{~0,~0},~0}
  52. static const stateid_t one_stateid = {
  53. .si_generation = ~0,
  54. .si_opaque = all_ones,
  55. };
  56. static const stateid_t zero_stateid = {
  57. /* all fields zero */
  58. };
  59. static const stateid_t currentstateid = {
  60. .si_generation = 1,
  61. };
  62. static u64 current_sessionid = 1;
  63. #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
  64. #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
  65. #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
  66. /* forward declarations */
  67. static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
  68. static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
  69. /* Locking: */
  70. /*
  71. * Currently used for the del_recall_lru and file hash table. In an
  72. * effort to decrease the scope of the client_mutex, this spinlock may
  73. * eventually cover more:
  74. */
  75. static DEFINE_SPINLOCK(state_lock);
  76. /*
  77. * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
  78. * the refcount on the open stateid to drop.
  79. */
  80. static DECLARE_WAIT_QUEUE_HEAD(close_wq);
  81. static struct kmem_cache *openowner_slab;
  82. static struct kmem_cache *lockowner_slab;
  83. static struct kmem_cache *file_slab;
  84. static struct kmem_cache *stateid_slab;
  85. static struct kmem_cache *deleg_slab;
  86. static struct kmem_cache *odstate_slab;
  87. static void free_session(struct nfsd4_session *);
  88. static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
  89. static 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. int strdup_if_nonnull(char **target, char *source)
  1571. {
  1572. if (source) {
  1573. *target = kstrdup(source, GFP_KERNEL);
  1574. if (!*target)
  1575. return -ENOMEM;
  1576. } else
  1577. *target = NULL;
  1578. return 0;
  1579. }
  1580. static int copy_cred(struct svc_cred *target, struct svc_cred *source)
  1581. {
  1582. int ret;
  1583. ret = strdup_if_nonnull(&target->cr_principal, source->cr_principal);
  1584. if (ret)
  1585. return ret;
  1586. ret = strdup_if_nonnull(&target->cr_raw_principal,
  1587. source->cr_raw_principal);
  1588. if (ret)
  1589. return ret;
  1590. target->cr_flavor = source->cr_flavor;
  1591. target->cr_uid = source->cr_uid;
  1592. target->cr_gid = source->cr_gid;
  1593. target->cr_group_info = source->cr_group_info;
  1594. get_group_info(target->cr_group_info);
  1595. target->cr_gss_mech = source->cr_gss_mech;
  1596. if (source->cr_gss_mech)
  1597. gss_mech_get(source->cr_gss_mech);
  1598. return 0;
  1599. }
  1600. static int
  1601. compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
  1602. {
  1603. if (o1->len < o2->len)
  1604. return -1;
  1605. if (o1->len > o2->len)
  1606. return 1;
  1607. return memcmp(o1->data, o2->data, o1->len);
  1608. }
  1609. static int same_name(const char *n1, const char *n2)
  1610. {
  1611. return 0 == memcmp(n1, n2, HEXDIR_LEN);
  1612. }
  1613. static int
  1614. same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
  1615. {
  1616. return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
  1617. }
  1618. static int
  1619. same_clid(clientid_t *cl1, clientid_t *cl2)
  1620. {
  1621. return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
  1622. }
  1623. static bool groups_equal(struct group_info *g1, struct group_info *g2)
  1624. {
  1625. int i;
  1626. if (g1->ngroups != g2->ngroups)
  1627. return false;
  1628. for (i=0; i<g1->ngroups; i++)
  1629. if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
  1630. return false;
  1631. return true;
  1632. }
  1633. /*
  1634. * RFC 3530 language requires clid_inuse be returned when the
  1635. * "principal" associated with a requests differs from that previously
  1636. * used. We use uid, gid's, and gss principal string as our best
  1637. * approximation. We also don't want to allow non-gss use of a client
  1638. * established using gss: in theory cr_principal should catch that
  1639. * change, but in practice cr_principal can be null even in the gss case
  1640. * since gssd doesn't always pass down a principal string.
  1641. */
  1642. static bool is_gss_cred(struct svc_cred *cr)
  1643. {
  1644. /* Is cr_flavor one of the gss "pseudoflavors"?: */
  1645. return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
  1646. }
  1647. static bool
  1648. same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
  1649. {
  1650. if ((is_gss_cred(cr1) != is_gss_cred(cr2))
  1651. || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
  1652. || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
  1653. || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
  1654. return false;
  1655. if (cr1->cr_principal == cr2->cr_principal)
  1656. return true;
  1657. if (!cr1->cr_principal || !cr2->cr_principal)
  1658. return false;
  1659. return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
  1660. }
  1661. static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
  1662. {
  1663. struct svc_cred *cr = &rqstp->rq_cred;
  1664. u32 service;
  1665. if (!cr->cr_gss_mech)
  1666. return false;
  1667. service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
  1668. return service == RPC_GSS_SVC_INTEGRITY ||
  1669. service == RPC_GSS_SVC_PRIVACY;
  1670. }
  1671. static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
  1672. {
  1673. struct svc_cred *cr = &rqstp->rq_cred;
  1674. if (!cl->cl_mach_cred)
  1675. return true;
  1676. if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
  1677. return false;
  1678. if (!svc_rqst_integrity_protected(rqstp))
  1679. return false;
  1680. if (cl->cl_cred.cr_raw_principal)
  1681. return 0 == strcmp(cl->cl_cred.cr_raw_principal,
  1682. cr->cr_raw_principal);
  1683. if (!cr->cr_principal)
  1684. return false;
  1685. return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
  1686. }
  1687. static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
  1688. {
  1689. __be32 verf[2];
  1690. /*
  1691. * This is opaque to client, so no need to byte-swap. Use
  1692. * __force to keep sparse happy
  1693. */
  1694. verf[0] = (__force __be32)get_seconds();
  1695. verf[1] = (__force __be32)nn->clverifier_counter++;
  1696. memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
  1697. }
  1698. static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
  1699. {
  1700. clp->cl_clientid.cl_boot = nn->boot_time;
  1701. clp->cl_clientid.cl_id = nn->clientid_counter++;
  1702. gen_confirm(clp, nn);
  1703. }
  1704. static struct nfs4_stid *
  1705. find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
  1706. {
  1707. struct nfs4_stid *ret;
  1708. ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
  1709. if (!ret || !ret->sc_type)
  1710. return NULL;
  1711. return ret;
  1712. }
  1713. static struct nfs4_stid *
  1714. find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
  1715. {
  1716. struct nfs4_stid *s;
  1717. spin_lock(&cl->cl_lock);
  1718. s = find_stateid_locked(cl, t);
  1719. if (s != NULL) {
  1720. if (typemask & s->sc_type)
  1721. atomic_inc(&s->sc_count);
  1722. else
  1723. s = NULL;
  1724. }
  1725. spin_unlock(&cl->cl_lock);
  1726. return s;
  1727. }
  1728. static struct nfs4_client *create_client(struct xdr_netobj name,
  1729. struct svc_rqst *rqstp, nfs4_verifier *verf)
  1730. {
  1731. struct nfs4_client *clp;
  1732. struct sockaddr *sa = svc_addr(rqstp);
  1733. int ret;
  1734. struct net *net = SVC_NET(rqstp);
  1735. clp = alloc_client(name);
  1736. if (clp == NULL)
  1737. return NULL;
  1738. ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
  1739. if (ret) {
  1740. free_client(clp);
  1741. return NULL;
  1742. }
  1743. nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
  1744. clp->cl_time = get_seconds();
  1745. clear_bit(0, &clp->cl_cb_slot_busy);
  1746. copy_verf(clp, verf);
  1747. rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
  1748. clp->cl_cb_session = NULL;
  1749. clp->net = net;
  1750. return clp;
  1751. }
  1752. static void
  1753. add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
  1754. {
  1755. struct rb_node **new = &(root->rb_node), *parent = NULL;
  1756. struct nfs4_client *clp;
  1757. while (*new) {
  1758. clp = rb_entry(*new, struct nfs4_client, cl_namenode);
  1759. parent = *new;
  1760. if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
  1761. new = &((*new)->rb_left);
  1762. else
  1763. new = &((*new)->rb_right);
  1764. }
  1765. rb_link_node(&new_clp->cl_namenode, parent, new);
  1766. rb_insert_color(&new_clp->cl_namenode, root);
  1767. }
  1768. static struct nfs4_client *
  1769. find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
  1770. {
  1771. int cmp;
  1772. struct rb_node *node = root->rb_node;
  1773. struct nfs4_client *clp;
  1774. while (node) {
  1775. clp = rb_entry(node, struct nfs4_client, cl_namenode);
  1776. cmp = compare_blob(&clp->cl_name, name);
  1777. if (cmp > 0)
  1778. node = node->rb_left;
  1779. else if (cmp < 0)
  1780. node = node->rb_right;
  1781. else
  1782. return clp;
  1783. }
  1784. return NULL;
  1785. }
  1786. static void
  1787. add_to_unconfirmed(struct nfs4_client *clp)
  1788. {
  1789. unsigned int idhashval;
  1790. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1791. lockdep_assert_held(&nn->client_lock);
  1792. clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1793. add_clp_to_name_tree(clp, &nn->unconf_name_tree);
  1794. idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1795. list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
  1796. renew_client_locked(clp);
  1797. }
  1798. static void
  1799. move_to_confirmed(struct nfs4_client *clp)
  1800. {
  1801. unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  1802. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1803. lockdep_assert_held(&nn->client_lock);
  1804. dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
  1805. list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
  1806. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  1807. add_clp_to_name_tree(clp, &nn->conf_name_tree);
  1808. set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  1809. renew_client_locked(clp);
  1810. }
  1811. static struct nfs4_client *
  1812. find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
  1813. {
  1814. struct nfs4_client *clp;
  1815. unsigned int idhashval = clientid_hashval(clid->cl_id);
  1816. list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
  1817. if (same_clid(&clp->cl_clientid, clid)) {
  1818. if ((bool)clp->cl_minorversion != sessions)
  1819. return NULL;
  1820. renew_client_locked(clp);
  1821. return clp;
  1822. }
  1823. }
  1824. return NULL;
  1825. }
  1826. static struct nfs4_client *
  1827. find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1828. {
  1829. struct list_head *tbl = nn->conf_id_hashtbl;
  1830. lockdep_assert_held(&nn->client_lock);
  1831. return find_client_in_id_table(tbl, clid, sessions);
  1832. }
  1833. static struct nfs4_client *
  1834. find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  1835. {
  1836. struct list_head *tbl = nn->unconf_id_hashtbl;
  1837. lockdep_assert_held(&nn->client_lock);
  1838. return find_client_in_id_table(tbl, clid, sessions);
  1839. }
  1840. static bool clp_used_exchangeid(struct nfs4_client *clp)
  1841. {
  1842. return clp->cl_exchange_flags != 0;
  1843. }
  1844. static struct nfs4_client *
  1845. find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1846. {
  1847. lockdep_assert_held(&nn->client_lock);
  1848. return find_clp_in_name_tree(name, &nn->conf_name_tree);
  1849. }
  1850. static struct nfs4_client *
  1851. find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  1852. {
  1853. lockdep_assert_held(&nn->client_lock);
  1854. return find_clp_in_name_tree(name, &nn->unconf_name_tree);
  1855. }
  1856. static void
  1857. gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
  1858. {
  1859. struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
  1860. struct sockaddr *sa = svc_addr(rqstp);
  1861. u32 scopeid = rpc_get_scope_id(sa);
  1862. unsigned short expected_family;
  1863. /* Currently, we only support tcp and tcp6 for the callback channel */
  1864. if (se->se_callback_netid_len == 3 &&
  1865. !memcmp(se->se_callback_netid_val, "tcp", 3))
  1866. expected_family = AF_INET;
  1867. else if (se->se_callback_netid_len == 4 &&
  1868. !memcmp(se->se_callback_netid_val, "tcp6", 4))
  1869. expected_family = AF_INET6;
  1870. else
  1871. goto out_err;
  1872. conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
  1873. se->se_callback_addr_len,
  1874. (struct sockaddr *)&conn->cb_addr,
  1875. sizeof(conn->cb_addr));
  1876. if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
  1877. goto out_err;
  1878. if (conn->cb_addr.ss_family == AF_INET6)
  1879. ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
  1880. conn->cb_prog = se->se_callback_prog;
  1881. conn->cb_ident = se->se_callback_ident;
  1882. memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
  1883. return;
  1884. out_err:
  1885. conn->cb_addr.ss_family = AF_UNSPEC;
  1886. conn->cb_addrlen = 0;
  1887. dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
  1888. "will not receive delegations\n",
  1889. clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
  1890. return;
  1891. }
  1892. /*
  1893. * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
  1894. */
  1895. static void
  1896. nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
  1897. {
  1898. struct xdr_buf *buf = resp->xdr.buf;
  1899. struct nfsd4_slot *slot = resp->cstate.slot;
  1900. unsigned int base;
  1901. dprintk("--> %s slot %p\n", __func__, slot);
  1902. slot->sl_opcnt = resp->opcnt;
  1903. slot->sl_status = resp->cstate.status;
  1904. slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
  1905. if (nfsd4_not_cached(resp)) {
  1906. slot->sl_datalen = 0;
  1907. return;
  1908. }
  1909. base = resp->cstate.data_offset;
  1910. slot->sl_datalen = buf->len - base;
  1911. if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
  1912. WARN(1, "%s: sessions DRC could not cache compound\n",
  1913. __func__);
  1914. return;
  1915. }
  1916. /*
  1917. * Encode the replay sequence operation from the slot values.
  1918. * If cachethis is FALSE encode the uncached rep error on the next
  1919. * operation which sets resp->p and increments resp->opcnt for
  1920. * nfs4svc_encode_compoundres.
  1921. *
  1922. */
  1923. static __be32
  1924. nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
  1925. struct nfsd4_compoundres *resp)
  1926. {
  1927. struct nfsd4_op *op;
  1928. struct nfsd4_slot *slot = resp->cstate.slot;
  1929. /* Encode the replayed sequence operation */
  1930. op = &args->ops[resp->opcnt - 1];
  1931. nfsd4_encode_operation(resp, op);
  1932. /* Return nfserr_retry_uncached_rep in next operation. */
  1933. if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
  1934. op = &args->ops[resp->opcnt++];
  1935. op->status = nfserr_retry_uncached_rep;
  1936. nfsd4_encode_operation(resp, op);
  1937. }
  1938. return op->status;
  1939. }
  1940. /*
  1941. * The sequence operation is not cached because we can use the slot and
  1942. * session values.
  1943. */
  1944. static __be32
  1945. nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
  1946. struct nfsd4_sequence *seq)
  1947. {
  1948. struct nfsd4_slot *slot = resp->cstate.slot;
  1949. struct xdr_stream *xdr = &resp->xdr;
  1950. __be32 *p;
  1951. __be32 status;
  1952. dprintk("--> %s slot %p\n", __func__, slot);
  1953. status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
  1954. if (status)
  1955. return status;
  1956. p = xdr_reserve_space(xdr, slot->sl_datalen);
  1957. if (!p) {
  1958. WARN_ON_ONCE(1);
  1959. return nfserr_serverfault;
  1960. }
  1961. xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
  1962. xdr_commit_encode(xdr);
  1963. resp->opcnt = slot->sl_opcnt;
  1964. return slot->sl_status;
  1965. }
  1966. /*
  1967. * Set the exchange_id flags returned by the server.
  1968. */
  1969. static void
  1970. nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
  1971. {
  1972. #ifdef CONFIG_NFSD_PNFS
  1973. new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
  1974. #else
  1975. new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
  1976. #endif
  1977. /* Referrals are supported, Migration is not. */
  1978. new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
  1979. /* set the wire flags to return to client. */
  1980. clid->flags = new->cl_exchange_flags;
  1981. }
  1982. static bool client_has_openowners(struct nfs4_client *clp)
  1983. {
  1984. struct nfs4_openowner *oo;
  1985. list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
  1986. if (!list_empty(&oo->oo_owner.so_stateids))
  1987. return true;
  1988. }
  1989. return false;
  1990. }
  1991. static bool client_has_state(struct nfs4_client *clp)
  1992. {
  1993. return client_has_openowners(clp)
  1994. #ifdef CONFIG_NFSD_PNFS
  1995. || !list_empty(&clp->cl_lo_states)
  1996. #endif
  1997. || !list_empty(&clp->cl_delegations)
  1998. || !list_empty(&clp->cl_sessions);
  1999. }
  2000. __be32
  2001. nfsd4_exchange_id(struct svc_rqst *rqstp,
  2002. struct nfsd4_compound_state *cstate,
  2003. struct nfsd4_exchange_id *exid)
  2004. {
  2005. struct nfs4_client *conf, *new;
  2006. struct nfs4_client *unconf = NULL;
  2007. __be32 status;
  2008. char addr_str[INET6_ADDRSTRLEN];
  2009. nfs4_verifier verf = exid->verifier;
  2010. struct sockaddr *sa = svc_addr(rqstp);
  2011. bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
  2012. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2013. rpc_ntop(sa, addr_str, sizeof(addr_str));
  2014. dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
  2015. "ip_addr=%s flags %x, spa_how %d\n",
  2016. __func__, rqstp, exid, exid->clname.len, exid->clname.data,
  2017. addr_str, exid->flags, exid->spa_how);
  2018. if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
  2019. return nfserr_inval;
  2020. new = create_client(exid->clname, rqstp, &verf);
  2021. if (new == NULL)
  2022. return nfserr_jukebox;
  2023. switch (exid->spa_how) {
  2024. case SP4_MACH_CRED:
  2025. if (!svc_rqst_integrity_protected(rqstp)) {
  2026. status = nfserr_inval;
  2027. goto out_nolock;
  2028. }
  2029. /*
  2030. * Sometimes userspace doesn't give us a principal.
  2031. * Which is a bug, really. Anyway, we can't enforce
  2032. * MACH_CRED in that case, better to give up now:
  2033. */
  2034. if (!new->cl_cred.cr_principal &&
  2035. !new->cl_cred.cr_raw_principal) {
  2036. status = nfserr_serverfault;
  2037. goto out_nolock;
  2038. }
  2039. new->cl_mach_cred = true;
  2040. case SP4_NONE:
  2041. break;
  2042. default: /* checked by xdr code */
  2043. WARN_ON_ONCE(1);
  2044. case SP4_SSV:
  2045. status = nfserr_encr_alg_unsupp;
  2046. goto out_nolock;
  2047. }
  2048. /* Cases below refer to rfc 5661 section 18.35.4: */
  2049. spin_lock(&nn->client_lock);
  2050. conf = find_confirmed_client_by_name(&exid->clname, nn);
  2051. if (conf) {
  2052. bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
  2053. bool verfs_match = same_verf(&verf, &conf->cl_verifier);
  2054. if (update) {
  2055. if (!clp_used_exchangeid(conf)) { /* buggy client */
  2056. status = nfserr_inval;
  2057. goto out;
  2058. }
  2059. if (!mach_creds_match(conf, rqstp)) {
  2060. status = nfserr_wrong_cred;
  2061. goto out;
  2062. }
  2063. if (!creds_match) { /* case 9 */
  2064. status = nfserr_perm;
  2065. goto out;
  2066. }
  2067. if (!verfs_match) { /* case 8 */
  2068. status = nfserr_not_same;
  2069. goto out;
  2070. }
  2071. /* case 6 */
  2072. exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
  2073. goto out_copy;
  2074. }
  2075. if (!creds_match) { /* case 3 */
  2076. if (client_has_state(conf)) {
  2077. status = nfserr_clid_inuse;
  2078. goto out;
  2079. }
  2080. goto out_new;
  2081. }
  2082. if (verfs_match) { /* case 2 */
  2083. conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
  2084. goto out_copy;
  2085. }
  2086. /* case 5, client reboot */
  2087. conf = NULL;
  2088. goto out_new;
  2089. }
  2090. if (update) { /* case 7 */
  2091. status = nfserr_noent;
  2092. goto out;
  2093. }
  2094. unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
  2095. if (unconf) /* case 4, possible retry or client restart */
  2096. unhash_client_locked(unconf);
  2097. /* case 1 (normal case) */
  2098. out_new:
  2099. if (conf) {
  2100. status = mark_client_expired_locked(conf);
  2101. if (status)
  2102. goto out;
  2103. }
  2104. new->cl_minorversion = cstate->minorversion;
  2105. gen_clid(new, nn);
  2106. add_to_unconfirmed(new);
  2107. swap(new, conf);
  2108. out_copy:
  2109. exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
  2110. exid->clientid.cl_id = conf->cl_clientid.cl_id;
  2111. exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
  2112. nfsd4_set_ex_flags(conf, exid);
  2113. dprintk("nfsd4_exchange_id seqid %d flags %x\n",
  2114. conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
  2115. status = nfs_ok;
  2116. out:
  2117. spin_unlock(&nn->client_lock);
  2118. out_nolock:
  2119. if (new)
  2120. expire_client(new);
  2121. if (unconf)
  2122. expire_client(unconf);
  2123. return status;
  2124. }
  2125. static __be32
  2126. check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
  2127. {
  2128. dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
  2129. slot_seqid);
  2130. /* The slot is in use, and no response has been sent. */
  2131. if (slot_inuse) {
  2132. if (seqid == slot_seqid)
  2133. return nfserr_jukebox;
  2134. else
  2135. return nfserr_seq_misordered;
  2136. }
  2137. /* Note unsigned 32-bit arithmetic handles wraparound: */
  2138. if (likely(seqid == slot_seqid + 1))
  2139. return nfs_ok;
  2140. if (seqid == slot_seqid)
  2141. return nfserr_replay_cache;
  2142. return nfserr_seq_misordered;
  2143. }
  2144. /*
  2145. * Cache the create session result into the create session single DRC
  2146. * slot cache by saving the xdr structure. sl_seqid has been set.
  2147. * Do this for solo or embedded create session operations.
  2148. */
  2149. static void
  2150. nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
  2151. struct nfsd4_clid_slot *slot, __be32 nfserr)
  2152. {
  2153. slot->sl_status = nfserr;
  2154. memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
  2155. }
  2156. static __be32
  2157. nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
  2158. struct nfsd4_clid_slot *slot)
  2159. {
  2160. memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
  2161. return slot->sl_status;
  2162. }
  2163. #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
  2164. 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
  2165. 1 + /* MIN tag is length with zero, only length */ \
  2166. 3 + /* version, opcount, opcode */ \
  2167. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  2168. /* seqid, slotID, slotID, cache */ \
  2169. 4 ) * sizeof(__be32))
  2170. #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
  2171. 2 + /* verifier: AUTH_NULL, length 0 */\
  2172. 1 + /* status */ \
  2173. 1 + /* MIN tag is length with zero, only length */ \
  2174. 3 + /* opcount, opcode, opstatus*/ \
  2175. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  2176. /* seqid, slotID, slotID, slotID, status */ \
  2177. 5 ) * sizeof(__be32))
  2178. static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
  2179. {
  2180. u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
  2181. if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
  2182. return nfserr_toosmall;
  2183. if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
  2184. return nfserr_toosmall;
  2185. ca->headerpadsz = 0;
  2186. ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
  2187. ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
  2188. ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
  2189. ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
  2190. NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
  2191. ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
  2192. /*
  2193. * Note decreasing slot size below client's request may make it
  2194. * difficult for client to function correctly, whereas
  2195. * decreasing the number of slots will (just?) affect
  2196. * performance. When short on memory we therefore prefer to
  2197. * decrease number of slots instead of their size. Clients that
  2198. * request larger slots than they need will get poor results:
  2199. */
  2200. ca->maxreqs = nfsd4_get_drc_mem(ca);
  2201. if (!ca->maxreqs)
  2202. return nfserr_jukebox;
  2203. return nfs_ok;
  2204. }
  2205. /*
  2206. * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
  2207. * These are based on similar macros in linux/sunrpc/msg_prot.h .
  2208. */
  2209. #define RPC_MAX_HEADER_WITH_AUTH_SYS \
  2210. (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
  2211. #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
  2212. (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
  2213. #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
  2214. RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
  2215. #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
  2216. RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
  2217. sizeof(__be32))
  2218. static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
  2219. {
  2220. ca->headerpadsz = 0;
  2221. if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
  2222. return nfserr_toosmall;
  2223. if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
  2224. return nfserr_toosmall;
  2225. ca->maxresp_cached = 0;
  2226. if (ca->maxops < 2)
  2227. return nfserr_toosmall;
  2228. return nfs_ok;
  2229. }
  2230. static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
  2231. {
  2232. switch (cbs->flavor) {
  2233. case RPC_AUTH_NULL:
  2234. case RPC_AUTH_UNIX:
  2235. return nfs_ok;
  2236. default:
  2237. /*
  2238. * GSS case: the spec doesn't allow us to return this
  2239. * error. But it also doesn't allow us not to support
  2240. * GSS.
  2241. * I'd rather this fail hard than return some error the
  2242. * client might think it can already handle:
  2243. */
  2244. return nfserr_encr_alg_unsupp;
  2245. }
  2246. }
  2247. __be32
  2248. nfsd4_create_session(struct svc_rqst *rqstp,
  2249. struct nfsd4_compound_state *cstate,
  2250. struct nfsd4_create_session *cr_ses)
  2251. {
  2252. struct sockaddr *sa = svc_addr(rqstp);
  2253. struct nfs4_client *conf, *unconf;
  2254. struct nfs4_client *old = NULL;
  2255. struct nfsd4_session *new;
  2256. struct nfsd4_conn *conn;
  2257. struct nfsd4_clid_slot *cs_slot = NULL;
  2258. __be32 status = 0;
  2259. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2260. if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
  2261. return nfserr_inval;
  2262. status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
  2263. if (status)
  2264. return status;
  2265. status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
  2266. if (status)
  2267. return status;
  2268. status = check_backchannel_attrs(&cr_ses->back_channel);
  2269. if (status)
  2270. goto out_release_drc_mem;
  2271. status = nfserr_jukebox;
  2272. new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
  2273. if (!new)
  2274. goto out_release_drc_mem;
  2275. conn = alloc_conn_from_crses(rqstp, cr_ses);
  2276. if (!conn)
  2277. goto out_free_session;
  2278. spin_lock(&nn->client_lock);
  2279. unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
  2280. conf = find_confirmed_client(&cr_ses->clientid, true, nn);
  2281. WARN_ON_ONCE(conf && unconf);
  2282. if (conf) {
  2283. status = nfserr_wrong_cred;
  2284. if (!mach_creds_match(conf, rqstp))
  2285. goto out_free_conn;
  2286. cs_slot = &conf->cl_cs_slot;
  2287. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  2288. if (status) {
  2289. if (status == nfserr_replay_cache)
  2290. status = nfsd4_replay_create_session(cr_ses, cs_slot);
  2291. goto out_free_conn;
  2292. }
  2293. } else if (unconf) {
  2294. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
  2295. !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
  2296. status = nfserr_clid_inuse;
  2297. goto out_free_conn;
  2298. }
  2299. status = nfserr_wrong_cred;
  2300. if (!mach_creds_match(unconf, rqstp))
  2301. goto out_free_conn;
  2302. cs_slot = &unconf->cl_cs_slot;
  2303. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  2304. if (status) {
  2305. /* an unconfirmed replay returns misordered */
  2306. status = nfserr_seq_misordered;
  2307. goto out_free_conn;
  2308. }
  2309. old = find_confirmed_client_by_name(&unconf->cl_name, nn);
  2310. if (old) {
  2311. status = mark_client_expired_locked(old);
  2312. if (status) {
  2313. old = NULL;
  2314. goto out_free_conn;
  2315. }
  2316. }
  2317. move_to_confirmed(unconf);
  2318. conf = unconf;
  2319. } else {
  2320. status = nfserr_stale_clientid;
  2321. goto out_free_conn;
  2322. }
  2323. status = nfs_ok;
  2324. /* Persistent sessions are not supported */
  2325. cr_ses->flags &= ~SESSION4_PERSIST;
  2326. /* Upshifting from TCP to RDMA is not supported */
  2327. cr_ses->flags &= ~SESSION4_RDMA;
  2328. init_session(rqstp, new, conf, cr_ses);
  2329. nfsd4_get_session_locked(new);
  2330. memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
  2331. NFS4_MAX_SESSIONID_LEN);
  2332. cs_slot->sl_seqid++;
  2333. cr_ses->seqid = cs_slot->sl_seqid;
  2334. /* cache solo and embedded create sessions under the client_lock */
  2335. nfsd4_cache_create_session(cr_ses, cs_slot, status);
  2336. spin_unlock(&nn->client_lock);
  2337. /* init connection and backchannel */
  2338. nfsd4_init_conn(rqstp, conn, new);
  2339. nfsd4_put_session(new);
  2340. if (old)
  2341. expire_client(old);
  2342. return status;
  2343. out_free_conn:
  2344. spin_unlock(&nn->client_lock);
  2345. free_conn(conn);
  2346. if (old)
  2347. expire_client(old);
  2348. out_free_session:
  2349. __free_session(new);
  2350. out_release_drc_mem:
  2351. nfsd4_put_drc_mem(&cr_ses->fore_channel);
  2352. return status;
  2353. }
  2354. static __be32 nfsd4_map_bcts_dir(u32 *dir)
  2355. {
  2356. switch (*dir) {
  2357. case NFS4_CDFC4_FORE:
  2358. case NFS4_CDFC4_BACK:
  2359. return nfs_ok;
  2360. case NFS4_CDFC4_FORE_OR_BOTH:
  2361. case NFS4_CDFC4_BACK_OR_BOTH:
  2362. *dir = NFS4_CDFC4_BOTH;
  2363. return nfs_ok;
  2364. };
  2365. return nfserr_inval;
  2366. }
  2367. __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
  2368. {
  2369. struct nfsd4_session *session = cstate->session;
  2370. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2371. __be32 status;
  2372. status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
  2373. if (status)
  2374. return status;
  2375. spin_lock(&nn->client_lock);
  2376. session->se_cb_prog = bc->bc_cb_program;
  2377. session->se_cb_sec = bc->bc_cb_sec;
  2378. spin_unlock(&nn->client_lock);
  2379. nfsd4_probe_callback(session->se_client);
  2380. return nfs_ok;
  2381. }
  2382. __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
  2383. struct nfsd4_compound_state *cstate,
  2384. struct nfsd4_bind_conn_to_session *bcts)
  2385. {
  2386. __be32 status;
  2387. struct nfsd4_conn *conn;
  2388. struct nfsd4_session *session;
  2389. struct net *net = SVC_NET(rqstp);
  2390. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2391. if (!nfsd4_last_compound_op(rqstp))
  2392. return nfserr_not_only_op;
  2393. spin_lock(&nn->client_lock);
  2394. session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
  2395. spin_unlock(&nn->client_lock);
  2396. if (!session)
  2397. goto out_no_session;
  2398. status = nfserr_wrong_cred;
  2399. if (!mach_creds_match(session->se_client, rqstp))
  2400. goto out;
  2401. status = nfsd4_map_bcts_dir(&bcts->dir);
  2402. if (status)
  2403. goto out;
  2404. conn = alloc_conn(rqstp, bcts->dir);
  2405. status = nfserr_jukebox;
  2406. if (!conn)
  2407. goto out;
  2408. nfsd4_init_conn(rqstp, conn, session);
  2409. status = nfs_ok;
  2410. out:
  2411. nfsd4_put_session(session);
  2412. out_no_session:
  2413. return status;
  2414. }
  2415. static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
  2416. {
  2417. if (!session)
  2418. return 0;
  2419. return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
  2420. }
  2421. __be32
  2422. nfsd4_destroy_session(struct svc_rqst *r,
  2423. struct nfsd4_compound_state *cstate,
  2424. struct nfsd4_destroy_session *sessionid)
  2425. {
  2426. struct nfsd4_session *ses;
  2427. __be32 status;
  2428. int ref_held_by_me = 0;
  2429. struct net *net = SVC_NET(r);
  2430. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2431. status = nfserr_not_only_op;
  2432. if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
  2433. if (!nfsd4_last_compound_op(r))
  2434. goto out;
  2435. ref_held_by_me++;
  2436. }
  2437. dump_sessionid(__func__, &sessionid->sessionid);
  2438. spin_lock(&nn->client_lock);
  2439. ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
  2440. if (!ses)
  2441. goto out_client_lock;
  2442. status = nfserr_wrong_cred;
  2443. if (!mach_creds_match(ses->se_client, r))
  2444. goto out_put_session;
  2445. status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
  2446. if (status)
  2447. goto out_put_session;
  2448. unhash_session(ses);
  2449. spin_unlock(&nn->client_lock);
  2450. nfsd4_probe_callback_sync(ses->se_client);
  2451. spin_lock(&nn->client_lock);
  2452. status = nfs_ok;
  2453. out_put_session:
  2454. nfsd4_put_session_locked(ses);
  2455. out_client_lock:
  2456. spin_unlock(&nn->client_lock);
  2457. out:
  2458. return status;
  2459. }
  2460. static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
  2461. {
  2462. struct nfsd4_conn *c;
  2463. list_for_each_entry(c, &s->se_conns, cn_persession) {
  2464. if (c->cn_xprt == xpt) {
  2465. return c;
  2466. }
  2467. }
  2468. return NULL;
  2469. }
  2470. static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
  2471. {
  2472. struct nfs4_client *clp = ses->se_client;
  2473. struct nfsd4_conn *c;
  2474. __be32 status = nfs_ok;
  2475. int ret;
  2476. spin_lock(&clp->cl_lock);
  2477. c = __nfsd4_find_conn(new->cn_xprt, ses);
  2478. if (c)
  2479. goto out_free;
  2480. status = nfserr_conn_not_bound_to_session;
  2481. if (clp->cl_mach_cred)
  2482. goto out_free;
  2483. __nfsd4_hash_conn(new, ses);
  2484. spin_unlock(&clp->cl_lock);
  2485. ret = nfsd4_register_conn(new);
  2486. if (ret)
  2487. /* oops; xprt is already down: */
  2488. nfsd4_conn_lost(&new->cn_xpt_user);
  2489. return nfs_ok;
  2490. out_free:
  2491. spin_unlock(&clp->cl_lock);
  2492. free_conn(new);
  2493. return status;
  2494. }
  2495. static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
  2496. {
  2497. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  2498. return args->opcnt > session->se_fchannel.maxops;
  2499. }
  2500. static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
  2501. struct nfsd4_session *session)
  2502. {
  2503. struct xdr_buf *xb = &rqstp->rq_arg;
  2504. return xb->len > session->se_fchannel.maxreq_sz;
  2505. }
  2506. __be32
  2507. nfsd4_sequence(struct svc_rqst *rqstp,
  2508. struct nfsd4_compound_state *cstate,
  2509. struct nfsd4_sequence *seq)
  2510. {
  2511. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  2512. struct xdr_stream *xdr = &resp->xdr;
  2513. struct nfsd4_session *session;
  2514. struct nfs4_client *clp;
  2515. struct nfsd4_slot *slot;
  2516. struct nfsd4_conn *conn;
  2517. __be32 status;
  2518. int buflen;
  2519. struct net *net = SVC_NET(rqstp);
  2520. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2521. if (resp->opcnt != 1)
  2522. return nfserr_sequence_pos;
  2523. /*
  2524. * Will be either used or freed by nfsd4_sequence_check_conn
  2525. * below.
  2526. */
  2527. conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
  2528. if (!conn)
  2529. return nfserr_jukebox;
  2530. spin_lock(&nn->client_lock);
  2531. session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
  2532. if (!session)
  2533. goto out_no_session;
  2534. clp = session->se_client;
  2535. status = nfserr_too_many_ops;
  2536. if (nfsd4_session_too_many_ops(rqstp, session))
  2537. goto out_put_session;
  2538. status = nfserr_req_too_big;
  2539. if (nfsd4_request_too_big(rqstp, session))
  2540. goto out_put_session;
  2541. status = nfserr_badslot;
  2542. if (seq->slotid >= session->se_fchannel.maxreqs)
  2543. goto out_put_session;
  2544. slot = session->se_slots[seq->slotid];
  2545. dprintk("%s: slotid %d\n", __func__, seq->slotid);
  2546. /* We do not negotiate the number of slots yet, so set the
  2547. * maxslots to the session maxreqs which is used to encode
  2548. * sr_highest_slotid and the sr_target_slot id to maxslots */
  2549. seq->maxslots = session->se_fchannel.maxreqs;
  2550. status = check_slot_seqid(seq->seqid, slot->sl_seqid,
  2551. slot->sl_flags & NFSD4_SLOT_INUSE);
  2552. if (status == nfserr_replay_cache) {
  2553. status = nfserr_seq_misordered;
  2554. if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
  2555. goto out_put_session;
  2556. cstate->slot = slot;
  2557. cstate->session = session;
  2558. cstate->clp = clp;
  2559. /* Return the cached reply status and set cstate->status
  2560. * for nfsd4_proc_compound processing */
  2561. status = nfsd4_replay_cache_entry(resp, seq);
  2562. cstate->status = nfserr_replay_cache;
  2563. goto out;
  2564. }
  2565. if (status)
  2566. goto out_put_session;
  2567. status = nfsd4_sequence_check_conn(conn, session);
  2568. conn = NULL;
  2569. if (status)
  2570. goto out_put_session;
  2571. buflen = (seq->cachethis) ?
  2572. session->se_fchannel.maxresp_cached :
  2573. session->se_fchannel.maxresp_sz;
  2574. status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
  2575. nfserr_rep_too_big;
  2576. if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
  2577. goto out_put_session;
  2578. svc_reserve(rqstp, buflen);
  2579. status = nfs_ok;
  2580. /* Success! bump slot seqid */
  2581. slot->sl_seqid = seq->seqid;
  2582. slot->sl_flags |= NFSD4_SLOT_INUSE;
  2583. if (seq->cachethis)
  2584. slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
  2585. else
  2586. slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
  2587. cstate->slot = slot;
  2588. cstate->session = session;
  2589. cstate->clp = clp;
  2590. out:
  2591. switch (clp->cl_cb_state) {
  2592. case NFSD4_CB_DOWN:
  2593. seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
  2594. break;
  2595. case NFSD4_CB_FAULT:
  2596. seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
  2597. break;
  2598. default:
  2599. seq->status_flags = 0;
  2600. }
  2601. if (!list_empty(&clp->cl_revoked))
  2602. seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
  2603. out_no_session:
  2604. if (conn)
  2605. free_conn(conn);
  2606. spin_unlock(&nn->client_lock);
  2607. return status;
  2608. out_put_session:
  2609. nfsd4_put_session_locked(session);
  2610. goto out_no_session;
  2611. }
  2612. void
  2613. nfsd4_sequence_done(struct nfsd4_compoundres *resp)
  2614. {
  2615. struct nfsd4_compound_state *cs = &resp->cstate;
  2616. if (nfsd4_has_session(cs)) {
  2617. if (cs->status != nfserr_replay_cache) {
  2618. nfsd4_store_cache_entry(resp);
  2619. cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
  2620. }
  2621. /* Drop session reference that was taken in nfsd4_sequence() */
  2622. nfsd4_put_session(cs->session);
  2623. } else if (cs->clp)
  2624. put_client_renew(cs->clp);
  2625. }
  2626. __be32
  2627. nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
  2628. {
  2629. struct nfs4_client *conf, *unconf;
  2630. struct nfs4_client *clp = NULL;
  2631. __be32 status = 0;
  2632. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2633. spin_lock(&nn->client_lock);
  2634. unconf = find_unconfirmed_client(&dc->clientid, true, nn);
  2635. conf = find_confirmed_client(&dc->clientid, true, nn);
  2636. WARN_ON_ONCE(conf && unconf);
  2637. if (conf) {
  2638. if (client_has_state(conf)) {
  2639. status = nfserr_clientid_busy;
  2640. goto out;
  2641. }
  2642. status = mark_client_expired_locked(conf);
  2643. if (status)
  2644. goto out;
  2645. clp = conf;
  2646. } else if (unconf)
  2647. clp = unconf;
  2648. else {
  2649. status = nfserr_stale_clientid;
  2650. goto out;
  2651. }
  2652. if (!mach_creds_match(clp, rqstp)) {
  2653. clp = NULL;
  2654. status = nfserr_wrong_cred;
  2655. goto out;
  2656. }
  2657. unhash_client_locked(clp);
  2658. out:
  2659. spin_unlock(&nn->client_lock);
  2660. if (clp)
  2661. expire_client(clp);
  2662. return status;
  2663. }
  2664. __be32
  2665. nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
  2666. {
  2667. __be32 status = 0;
  2668. if (rc->rca_one_fs) {
  2669. if (!cstate->current_fh.fh_dentry)
  2670. return nfserr_nofilehandle;
  2671. /*
  2672. * We don't take advantage of the rca_one_fs case.
  2673. * That's OK, it's optional, we can safely ignore it.
  2674. */
  2675. return nfs_ok;
  2676. }
  2677. status = nfserr_complete_already;
  2678. if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
  2679. &cstate->session->se_client->cl_flags))
  2680. goto out;
  2681. status = nfserr_stale_clientid;
  2682. if (is_client_expired(cstate->session->se_client))
  2683. /*
  2684. * The following error isn't really legal.
  2685. * But we only get here if the client just explicitly
  2686. * destroyed the client. Surely it no longer cares what
  2687. * error it gets back on an operation for the dead
  2688. * client.
  2689. */
  2690. goto out;
  2691. status = nfs_ok;
  2692. nfsd4_client_record_create(cstate->session->se_client);
  2693. out:
  2694. return status;
  2695. }
  2696. __be32
  2697. nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  2698. struct nfsd4_setclientid *setclid)
  2699. {
  2700. struct xdr_netobj clname = setclid->se_name;
  2701. nfs4_verifier clverifier = setclid->se_verf;
  2702. struct nfs4_client *conf, *new;
  2703. struct nfs4_client *unconf = NULL;
  2704. __be32 status;
  2705. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2706. new = create_client(clname, rqstp, &clverifier);
  2707. if (new == NULL)
  2708. return nfserr_jukebox;
  2709. /* Cases below refer to rfc 3530 section 14.2.33: */
  2710. spin_lock(&nn->client_lock);
  2711. conf = find_confirmed_client_by_name(&clname, nn);
  2712. if (conf && client_has_state(conf)) {
  2713. /* case 0: */
  2714. status = nfserr_clid_inuse;
  2715. if (clp_used_exchangeid(conf))
  2716. goto out;
  2717. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
  2718. char addr_str[INET6_ADDRSTRLEN];
  2719. rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
  2720. sizeof(addr_str));
  2721. dprintk("NFSD: setclientid: string in use by client "
  2722. "at %s\n", addr_str);
  2723. goto out;
  2724. }
  2725. }
  2726. unconf = find_unconfirmed_client_by_name(&clname, nn);
  2727. if (unconf)
  2728. unhash_client_locked(unconf);
  2729. if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
  2730. /* case 1: probable callback update */
  2731. copy_clid(new, conf);
  2732. gen_confirm(new, nn);
  2733. } else /* case 4 (new client) or cases 2, 3 (client reboot): */
  2734. gen_clid(new, nn);
  2735. new->cl_minorversion = 0;
  2736. gen_callback(new, setclid, rqstp);
  2737. add_to_unconfirmed(new);
  2738. setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
  2739. setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
  2740. memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
  2741. new = NULL;
  2742. status = nfs_ok;
  2743. out:
  2744. spin_unlock(&nn->client_lock);
  2745. if (new)
  2746. free_client(new);
  2747. if (unconf)
  2748. expire_client(unconf);
  2749. return status;
  2750. }
  2751. __be32
  2752. nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
  2753. struct nfsd4_compound_state *cstate,
  2754. struct nfsd4_setclientid_confirm *setclientid_confirm)
  2755. {
  2756. struct nfs4_client *conf, *unconf;
  2757. struct nfs4_client *old = NULL;
  2758. nfs4_verifier confirm = setclientid_confirm->sc_confirm;
  2759. clientid_t * clid = &setclientid_confirm->sc_clientid;
  2760. __be32 status;
  2761. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  2762. if (STALE_CLIENTID(clid, nn))
  2763. return nfserr_stale_clientid;
  2764. spin_lock(&nn->client_lock);
  2765. conf = find_confirmed_client(clid, false, nn);
  2766. unconf = find_unconfirmed_client(clid, false, nn);
  2767. /*
  2768. * We try hard to give out unique clientid's, so if we get an
  2769. * attempt to confirm the same clientid with a different cred,
  2770. * the client may be buggy; this should never happen.
  2771. *
  2772. * Nevertheless, RFC 7530 recommends INUSE for this case:
  2773. */
  2774. status = nfserr_clid_inuse;
  2775. if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
  2776. goto out;
  2777. if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
  2778. goto out;
  2779. /* cases below refer to rfc 3530 section 14.2.34: */
  2780. if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
  2781. if (conf && !unconf) /* case 2: probable retransmit */
  2782. status = nfs_ok;
  2783. else /* case 4: client hasn't noticed we rebooted yet? */
  2784. status = nfserr_stale_clientid;
  2785. goto out;
  2786. }
  2787. status = nfs_ok;
  2788. if (conf) { /* case 1: callback update */
  2789. old = unconf;
  2790. unhash_client_locked(old);
  2791. nfsd4_change_callback(conf, &unconf->cl_cb_conn);
  2792. } else { /* case 3: normal case; new or rebooted client */
  2793. old = find_confirmed_client_by_name(&unconf->cl_name, nn);
  2794. if (old) {
  2795. status = nfserr_clid_inuse;
  2796. if (client_has_state(old)
  2797. && !same_creds(&unconf->cl_cred,
  2798. &old->cl_cred))
  2799. goto out;
  2800. status = mark_client_expired_locked(old);
  2801. if (status) {
  2802. old = NULL;
  2803. goto out;
  2804. }
  2805. }
  2806. move_to_confirmed(unconf);
  2807. conf = unconf;
  2808. }
  2809. get_client_locked(conf);
  2810. spin_unlock(&nn->client_lock);
  2811. nfsd4_probe_callback(conf);
  2812. spin_lock(&nn->client_lock);
  2813. put_client_renew_locked(conf);
  2814. out:
  2815. spin_unlock(&nn->client_lock);
  2816. if (old)
  2817. expire_client(old);
  2818. return status;
  2819. }
  2820. static struct nfs4_file *nfsd4_alloc_file(void)
  2821. {
  2822. return kmem_cache_alloc(file_slab, GFP_KERNEL);
  2823. }
  2824. /* OPEN Share state helper functions */
  2825. static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
  2826. struct nfs4_file *fp)
  2827. {
  2828. lockdep_assert_held(&state_lock);
  2829. atomic_set(&fp->fi_ref, 1);
  2830. spin_lock_init(&fp->fi_lock);
  2831. INIT_LIST_HEAD(&fp->fi_stateids);
  2832. INIT_LIST_HEAD(&fp->fi_delegations);
  2833. INIT_LIST_HEAD(&fp->fi_clnt_odstate);
  2834. fh_copy_shallow(&fp->fi_fhandle, fh);
  2835. fp->fi_deleg_file = NULL;
  2836. fp->fi_had_conflict = false;
  2837. fp->fi_share_deny = 0;
  2838. memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
  2839. memset(fp->fi_access, 0, sizeof(fp->fi_access));
  2840. #ifdef CONFIG_NFSD_PNFS
  2841. INIT_LIST_HEAD(&fp->fi_lo_states);
  2842. atomic_set(&fp->fi_lo_recalls, 0);
  2843. #endif
  2844. hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
  2845. }
  2846. void
  2847. nfsd4_free_slabs(void)
  2848. {
  2849. kmem_cache_destroy(odstate_slab);
  2850. kmem_cache_destroy(openowner_slab);
  2851. kmem_cache_destroy(lockowner_slab);
  2852. kmem_cache_destroy(file_slab);
  2853. kmem_cache_destroy(stateid_slab);
  2854. kmem_cache_destroy(deleg_slab);
  2855. }
  2856. int
  2857. nfsd4_init_slabs(void)
  2858. {
  2859. openowner_slab = kmem_cache_create("nfsd4_openowners",
  2860. sizeof(struct nfs4_openowner), 0, 0, NULL);
  2861. if (openowner_slab == NULL)
  2862. goto out;
  2863. lockowner_slab = kmem_cache_create("nfsd4_lockowners",
  2864. sizeof(struct nfs4_lockowner), 0, 0, NULL);
  2865. if (lockowner_slab == NULL)
  2866. goto out_free_openowner_slab;
  2867. file_slab = kmem_cache_create("nfsd4_files",
  2868. sizeof(struct nfs4_file), 0, 0, NULL);
  2869. if (file_slab == NULL)
  2870. goto out_free_lockowner_slab;
  2871. stateid_slab = kmem_cache_create("nfsd4_stateids",
  2872. sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
  2873. if (stateid_slab == NULL)
  2874. goto out_free_file_slab;
  2875. deleg_slab = kmem_cache_create("nfsd4_delegations",
  2876. sizeof(struct nfs4_delegation), 0, 0, NULL);
  2877. if (deleg_slab == NULL)
  2878. goto out_free_stateid_slab;
  2879. odstate_slab = kmem_cache_create("nfsd4_odstate",
  2880. sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
  2881. if (odstate_slab == NULL)
  2882. goto out_free_deleg_slab;
  2883. return 0;
  2884. out_free_deleg_slab:
  2885. kmem_cache_destroy(deleg_slab);
  2886. out_free_stateid_slab:
  2887. kmem_cache_destroy(stateid_slab);
  2888. out_free_file_slab:
  2889. kmem_cache_destroy(file_slab);
  2890. out_free_lockowner_slab:
  2891. kmem_cache_destroy(lockowner_slab);
  2892. out_free_openowner_slab:
  2893. kmem_cache_destroy(openowner_slab);
  2894. out:
  2895. dprintk("nfsd4: out of memory while initializing nfsv4\n");
  2896. return -ENOMEM;
  2897. }
  2898. static void init_nfs4_replay(struct nfs4_replay *rp)
  2899. {
  2900. rp->rp_status = nfserr_serverfault;
  2901. rp->rp_buflen = 0;
  2902. rp->rp_buf = rp->rp_ibuf;
  2903. mutex_init(&rp->rp_mutex);
  2904. }
  2905. static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
  2906. struct nfs4_stateowner *so)
  2907. {
  2908. if (!nfsd4_has_session(cstate)) {
  2909. mutex_lock(&so->so_replay.rp_mutex);
  2910. cstate->replay_owner = nfs4_get_stateowner(so);
  2911. }
  2912. }
  2913. void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
  2914. {
  2915. struct nfs4_stateowner *so = cstate->replay_owner;
  2916. if (so != NULL) {
  2917. cstate->replay_owner = NULL;
  2918. mutex_unlock(&so->so_replay.rp_mutex);
  2919. nfs4_put_stateowner(so);
  2920. }
  2921. }
  2922. static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
  2923. {
  2924. struct nfs4_stateowner *sop;
  2925. sop = kmem_cache_alloc(slab, GFP_KERNEL);
  2926. if (!sop)
  2927. return NULL;
  2928. sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
  2929. if (!sop->so_owner.data) {
  2930. kmem_cache_free(slab, sop);
  2931. return NULL;
  2932. }
  2933. sop->so_owner.len = owner->len;
  2934. INIT_LIST_HEAD(&sop->so_stateids);
  2935. sop->so_client = clp;
  2936. init_nfs4_replay(&sop->so_replay);
  2937. atomic_set(&sop->so_count, 1);
  2938. return sop;
  2939. }
  2940. static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
  2941. {
  2942. lockdep_assert_held(&clp->cl_lock);
  2943. list_add(&oo->oo_owner.so_strhash,
  2944. &clp->cl_ownerstr_hashtbl[strhashval]);
  2945. list_add(&oo->oo_perclient, &clp->cl_openowners);
  2946. }
  2947. static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
  2948. {
  2949. unhash_openowner_locked(openowner(so));
  2950. }
  2951. static void nfs4_free_openowner(struct nfs4_stateowner *so)
  2952. {
  2953. struct nfs4_openowner *oo = openowner(so);
  2954. kmem_cache_free(openowner_slab, oo);
  2955. }
  2956. static const struct nfs4_stateowner_operations openowner_ops = {
  2957. .so_unhash = nfs4_unhash_openowner,
  2958. .so_free = nfs4_free_openowner,
  2959. };
  2960. static struct nfs4_ol_stateid *
  2961. nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
  2962. {
  2963. struct nfs4_ol_stateid *local, *ret = NULL;
  2964. struct nfs4_openowner *oo = open->op_openowner;
  2965. lockdep_assert_held(&fp->fi_lock);
  2966. list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
  2967. /* ignore lock owners */
  2968. if (local->st_stateowner->so_is_open_owner == 0)
  2969. continue;
  2970. if (local->st_stateowner == &oo->oo_owner) {
  2971. ret = local;
  2972. atomic_inc(&ret->st_stid.sc_count);
  2973. break;
  2974. }
  2975. }
  2976. return ret;
  2977. }
  2978. static struct nfs4_openowner *
  2979. alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
  2980. struct nfsd4_compound_state *cstate)
  2981. {
  2982. struct nfs4_client *clp = cstate->clp;
  2983. struct nfs4_openowner *oo, *ret;
  2984. oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
  2985. if (!oo)
  2986. return NULL;
  2987. oo->oo_owner.so_ops = &openowner_ops;
  2988. oo->oo_owner.so_is_open_owner = 1;
  2989. oo->oo_owner.so_seqid = open->op_seqid;
  2990. oo->oo_flags = 0;
  2991. if (nfsd4_has_session(cstate))
  2992. oo->oo_flags |= NFS4_OO_CONFIRMED;
  2993. oo->oo_time = 0;
  2994. oo->oo_last_closed_stid = NULL;
  2995. INIT_LIST_HEAD(&oo->oo_close_lru);
  2996. spin_lock(&clp->cl_lock);
  2997. ret = find_openstateowner_str_locked(strhashval, open, clp);
  2998. if (ret == NULL) {
  2999. hash_openowner(oo, clp, strhashval);
  3000. ret = oo;
  3001. } else
  3002. nfs4_free_stateowner(&oo->oo_owner);
  3003. spin_unlock(&clp->cl_lock);
  3004. return ret;
  3005. }
  3006. static struct nfs4_ol_stateid *
  3007. init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp,
  3008. struct nfsd4_open *open)
  3009. {
  3010. struct nfs4_openowner *oo = open->op_openowner;
  3011. struct nfs4_ol_stateid *retstp = NULL;
  3012. spin_lock(&oo->oo_owner.so_client->cl_lock);
  3013. spin_lock(&fp->fi_lock);
  3014. retstp = nfsd4_find_existing_open(fp, open);
  3015. if (retstp)
  3016. goto out_unlock;
  3017. atomic_inc(&stp->st_stid.sc_count);
  3018. stp->st_stid.sc_type = NFS4_OPEN_STID;
  3019. INIT_LIST_HEAD(&stp->st_locks);
  3020. stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
  3021. get_nfs4_file(fp);
  3022. stp->st_stid.sc_file = fp;
  3023. stp->st_access_bmap = 0;
  3024. stp->st_deny_bmap = 0;
  3025. stp->st_openstp = NULL;
  3026. init_rwsem(&stp->st_rwsem);
  3027. list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
  3028. list_add(&stp->st_perfile, &fp->fi_stateids);
  3029. out_unlock:
  3030. spin_unlock(&fp->fi_lock);
  3031. spin_unlock(&oo->oo_owner.so_client->cl_lock);
  3032. return retstp;
  3033. }
  3034. /*
  3035. * In the 4.0 case we need to keep the owners around a little while to handle
  3036. * CLOSE replay. We still do need to release any file access that is held by
  3037. * them before returning however.
  3038. */
  3039. static void
  3040. move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
  3041. {
  3042. struct nfs4_ol_stateid *last;
  3043. struct nfs4_openowner *oo = openowner(s->st_stateowner);
  3044. struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
  3045. nfsd_net_id);
  3046. dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
  3047. /*
  3048. * We know that we hold one reference via nfsd4_close, and another
  3049. * "persistent" reference for the client. If the refcount is higher
  3050. * than 2, then there are still calls in progress that are using this
  3051. * stateid. We can't put the sc_file reference until they are finished.
  3052. * Wait for the refcount to drop to 2. Since it has been unhashed,
  3053. * there should be no danger of the refcount going back up again at
  3054. * this point.
  3055. */
  3056. wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
  3057. release_all_access(s);
  3058. if (s->st_stid.sc_file) {
  3059. put_nfs4_file(s->st_stid.sc_file);
  3060. s->st_stid.sc_file = NULL;
  3061. }
  3062. spin_lock(&nn->client_lock);
  3063. last = oo->oo_last_closed_stid;
  3064. oo->oo_last_closed_stid = s;
  3065. list_move_tail(&oo->oo_close_lru, &nn->close_lru);
  3066. oo->oo_time = get_seconds();
  3067. spin_unlock(&nn->client_lock);
  3068. if (last)
  3069. nfs4_put_stid(&last->st_stid);
  3070. }
  3071. /* search file_hashtbl[] for file */
  3072. static struct nfs4_file *
  3073. find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
  3074. {
  3075. struct nfs4_file *fp;
  3076. hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) {
  3077. if (fh_match(&fp->fi_fhandle, fh)) {
  3078. if (atomic_inc_not_zero(&fp->fi_ref))
  3079. return fp;
  3080. }
  3081. }
  3082. return NULL;
  3083. }
  3084. struct nfs4_file *
  3085. find_file(struct knfsd_fh *fh)
  3086. {
  3087. struct nfs4_file *fp;
  3088. unsigned int hashval = file_hashval(fh);
  3089. rcu_read_lock();
  3090. fp = find_file_locked(fh, hashval);
  3091. rcu_read_unlock();
  3092. return fp;
  3093. }
  3094. static struct nfs4_file *
  3095. find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
  3096. {
  3097. struct nfs4_file *fp;
  3098. unsigned int hashval = file_hashval(fh);
  3099. rcu_read_lock();
  3100. fp = find_file_locked(fh, hashval);
  3101. rcu_read_unlock();
  3102. if (fp)
  3103. return fp;
  3104. spin_lock(&state_lock);
  3105. fp = find_file_locked(fh, hashval);
  3106. if (likely(fp == NULL)) {
  3107. nfsd4_init_file(fh, hashval, new);
  3108. fp = new;
  3109. }
  3110. spin_unlock(&state_lock);
  3111. return fp;
  3112. }
  3113. /*
  3114. * Called to check deny when READ with all zero stateid or
  3115. * WRITE with all zero or all one stateid
  3116. */
  3117. static __be32
  3118. nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
  3119. {
  3120. struct nfs4_file *fp;
  3121. __be32 ret = nfs_ok;
  3122. fp = find_file(&current_fh->fh_handle);
  3123. if (!fp)
  3124. return ret;
  3125. /* Check for conflicting share reservations */
  3126. spin_lock(&fp->fi_lock);
  3127. if (fp->fi_share_deny & deny_type)
  3128. ret = nfserr_locked;
  3129. spin_unlock(&fp->fi_lock);
  3130. put_nfs4_file(fp);
  3131. return ret;
  3132. }
  3133. static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
  3134. {
  3135. struct nfs4_delegation *dp = cb_to_delegation(cb);
  3136. struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
  3137. nfsd_net_id);
  3138. block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
  3139. /*
  3140. * We can't do this in nfsd_break_deleg_cb because it is
  3141. * already holding inode->i_lock.
  3142. *
  3143. * If the dl_time != 0, then we know that it has already been
  3144. * queued for a lease break. Don't queue it again.
  3145. */
  3146. spin_lock(&state_lock);
  3147. if (dp->dl_time == 0) {
  3148. dp->dl_time = get_seconds();
  3149. list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
  3150. }
  3151. spin_unlock(&state_lock);
  3152. }
  3153. static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
  3154. struct rpc_task *task)
  3155. {
  3156. struct nfs4_delegation *dp = cb_to_delegation(cb);
  3157. if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID)
  3158. return 1;
  3159. switch (task->tk_status) {
  3160. case 0:
  3161. return 1;
  3162. case -EBADHANDLE:
  3163. case -NFS4ERR_BAD_STATEID:
  3164. /*
  3165. * Race: client probably got cb_recall before open reply
  3166. * granting delegation.
  3167. */
  3168. if (dp->dl_retries--) {
  3169. rpc_delay(task, 2 * HZ);
  3170. return 0;
  3171. }
  3172. /*FALLTHRU*/
  3173. default:
  3174. return -1;
  3175. }
  3176. }
  3177. static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
  3178. {
  3179. struct nfs4_delegation *dp = cb_to_delegation(cb);
  3180. nfs4_put_stid(&dp->dl_stid);
  3181. }
  3182. static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
  3183. .prepare = nfsd4_cb_recall_prepare,
  3184. .done = nfsd4_cb_recall_done,
  3185. .release = nfsd4_cb_recall_release,
  3186. };
  3187. static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
  3188. {
  3189. /*
  3190. * We're assuming the state code never drops its reference
  3191. * without first removing the lease. Since we're in this lease
  3192. * callback (and since the lease code is serialized by the kernel
  3193. * lock) we know the server hasn't removed the lease yet, we know
  3194. * it's safe to take a reference.
  3195. */
  3196. atomic_inc(&dp->dl_stid.sc_count);
  3197. nfsd4_run_cb(&dp->dl_recall);
  3198. }
  3199. /* Called from break_lease() with i_lock held. */
  3200. static bool
  3201. nfsd_break_deleg_cb(struct file_lock *fl)
  3202. {
  3203. bool ret = false;
  3204. struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
  3205. struct nfs4_delegation *dp;
  3206. if (!fp) {
  3207. WARN(1, "(%p)->fl_owner NULL\n", fl);
  3208. return ret;
  3209. }
  3210. if (fp->fi_had_conflict) {
  3211. WARN(1, "duplicate break on %p\n", fp);
  3212. return ret;
  3213. }
  3214. /*
  3215. * We don't want the locks code to timeout the lease for us;
  3216. * we'll remove it ourself if a delegation isn't returned
  3217. * in time:
  3218. */
  3219. fl->fl_break_time = 0;
  3220. spin_lock(&fp->fi_lock);
  3221. fp->fi_had_conflict = true;
  3222. /*
  3223. * If there are no delegations on the list, then return true
  3224. * so that the lease code will go ahead and delete it.
  3225. */
  3226. if (list_empty(&fp->fi_delegations))
  3227. ret = true;
  3228. else
  3229. list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
  3230. nfsd_break_one_deleg(dp);
  3231. spin_unlock(&fp->fi_lock);
  3232. return ret;
  3233. }
  3234. static int
  3235. nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
  3236. struct list_head *dispose)
  3237. {
  3238. if (arg & F_UNLCK)
  3239. return lease_modify(onlist, arg, dispose);
  3240. else
  3241. return -EAGAIN;
  3242. }
  3243. static const struct lock_manager_operations nfsd_lease_mng_ops = {
  3244. .lm_break = nfsd_break_deleg_cb,
  3245. .lm_change = nfsd_change_deleg_cb,
  3246. };
  3247. static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
  3248. {
  3249. if (nfsd4_has_session(cstate))
  3250. return nfs_ok;
  3251. if (seqid == so->so_seqid - 1)
  3252. return nfserr_replay_me;
  3253. if (seqid == so->so_seqid)
  3254. return nfs_ok;
  3255. return nfserr_bad_seqid;
  3256. }
  3257. static __be32 lookup_clientid(clientid_t *clid,
  3258. struct nfsd4_compound_state *cstate,
  3259. struct nfsd_net *nn)
  3260. {
  3261. struct nfs4_client *found;
  3262. if (cstate->clp) {
  3263. found = cstate->clp;
  3264. if (!same_clid(&found->cl_clientid, clid))
  3265. return nfserr_stale_clientid;
  3266. return nfs_ok;
  3267. }
  3268. if (STALE_CLIENTID(clid, nn))
  3269. return nfserr_stale_clientid;
  3270. /*
  3271. * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
  3272. * cached already then we know this is for is for v4.0 and "sessions"
  3273. * will be false.
  3274. */
  3275. WARN_ON_ONCE(cstate->session);
  3276. spin_lock(&nn->client_lock);
  3277. found = find_confirmed_client(clid, false, nn);
  3278. if (!found) {
  3279. spin_unlock(&nn->client_lock);
  3280. return nfserr_expired;
  3281. }
  3282. atomic_inc(&found->cl_refcount);
  3283. spin_unlock(&nn->client_lock);
  3284. /* Cache the nfs4_client in cstate! */
  3285. cstate->clp = found;
  3286. return nfs_ok;
  3287. }
  3288. __be32
  3289. nfsd4_process_open1(struct nfsd4_compound_state *cstate,
  3290. struct nfsd4_open *open, struct nfsd_net *nn)
  3291. {
  3292. clientid_t *clientid = &open->op_clientid;
  3293. struct nfs4_client *clp = NULL;
  3294. unsigned int strhashval;
  3295. struct nfs4_openowner *oo = NULL;
  3296. __be32 status;
  3297. if (STALE_CLIENTID(&open->op_clientid, nn))
  3298. return nfserr_stale_clientid;
  3299. /*
  3300. * In case we need it later, after we've already created the
  3301. * file and don't want to risk a further failure:
  3302. */
  3303. open->op_file = nfsd4_alloc_file();
  3304. if (open->op_file == NULL)
  3305. return nfserr_jukebox;
  3306. status = lookup_clientid(clientid, cstate, nn);
  3307. if (status)
  3308. return status;
  3309. clp = cstate->clp;
  3310. strhashval = ownerstr_hashval(&open->op_owner);
  3311. oo = find_openstateowner_str(strhashval, open, clp);
  3312. open->op_openowner = oo;
  3313. if (!oo) {
  3314. goto new_owner;
  3315. }
  3316. if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  3317. /* Replace unconfirmed owners without checking for replay. */
  3318. release_openowner(oo);
  3319. open->op_openowner = NULL;
  3320. goto new_owner;
  3321. }
  3322. status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
  3323. if (status)
  3324. return status;
  3325. goto alloc_stateid;
  3326. new_owner:
  3327. oo = alloc_init_open_stateowner(strhashval, open, cstate);
  3328. if (oo == NULL)
  3329. return nfserr_jukebox;
  3330. open->op_openowner = oo;
  3331. alloc_stateid:
  3332. open->op_stp = nfs4_alloc_open_stateid(clp);
  3333. if (!open->op_stp)
  3334. return nfserr_jukebox;
  3335. if (nfsd4_has_session(cstate) &&
  3336. (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
  3337. open->op_odstate = alloc_clnt_odstate(clp);
  3338. if (!open->op_odstate)
  3339. return nfserr_jukebox;
  3340. }
  3341. return nfs_ok;
  3342. }
  3343. static inline __be32
  3344. nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
  3345. {
  3346. if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
  3347. return nfserr_openmode;
  3348. else
  3349. return nfs_ok;
  3350. }
  3351. static int share_access_to_flags(u32 share_access)
  3352. {
  3353. return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
  3354. }
  3355. static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
  3356. {
  3357. struct nfs4_stid *ret;
  3358. ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
  3359. if (!ret)
  3360. return NULL;
  3361. return delegstateid(ret);
  3362. }
  3363. static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
  3364. {
  3365. return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
  3366. open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
  3367. }
  3368. static __be32
  3369. nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
  3370. struct nfs4_delegation **dp)
  3371. {
  3372. int flags;
  3373. __be32 status = nfserr_bad_stateid;
  3374. struct nfs4_delegation *deleg;
  3375. deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
  3376. if (deleg == NULL)
  3377. goto out;
  3378. flags = share_access_to_flags(open->op_share_access);
  3379. status = nfs4_check_delegmode(deleg, flags);
  3380. if (status) {
  3381. nfs4_put_stid(&deleg->dl_stid);
  3382. goto out;
  3383. }
  3384. *dp = deleg;
  3385. out:
  3386. if (!nfsd4_is_deleg_cur(open))
  3387. return nfs_ok;
  3388. if (status)
  3389. return status;
  3390. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  3391. return nfs_ok;
  3392. }
  3393. static inline int nfs4_access_to_access(u32 nfs4_access)
  3394. {
  3395. int flags = 0;
  3396. if (nfs4_access & NFS4_SHARE_ACCESS_READ)
  3397. flags |= NFSD_MAY_READ;
  3398. if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
  3399. flags |= NFSD_MAY_WRITE;
  3400. return flags;
  3401. }
  3402. static inline __be32
  3403. nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
  3404. struct nfsd4_open *open)
  3405. {
  3406. struct iattr iattr = {
  3407. .ia_valid = ATTR_SIZE,
  3408. .ia_size = 0,
  3409. };
  3410. if (!open->op_truncate)
  3411. return 0;
  3412. if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  3413. return nfserr_inval;
  3414. return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
  3415. }
  3416. static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
  3417. struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
  3418. struct nfsd4_open *open)
  3419. {
  3420. struct file *filp = NULL;
  3421. __be32 status;
  3422. int oflag = nfs4_access_to_omode(open->op_share_access);
  3423. int access = nfs4_access_to_access(open->op_share_access);
  3424. unsigned char old_access_bmap, old_deny_bmap;
  3425. spin_lock(&fp->fi_lock);
  3426. /*
  3427. * Are we trying to set a deny mode that would conflict with
  3428. * current access?
  3429. */
  3430. status = nfs4_file_check_deny(fp, open->op_share_deny);
  3431. if (status != nfs_ok) {
  3432. spin_unlock(&fp->fi_lock);
  3433. goto out;
  3434. }
  3435. /* set access to the file */
  3436. status = nfs4_file_get_access(fp, open->op_share_access);
  3437. if (status != nfs_ok) {
  3438. spin_unlock(&fp->fi_lock);
  3439. goto out;
  3440. }
  3441. /* Set access bits in stateid */
  3442. old_access_bmap = stp->st_access_bmap;
  3443. set_access(open->op_share_access, stp);
  3444. /* Set new deny mask */
  3445. old_deny_bmap = stp->st_deny_bmap;
  3446. set_deny(open->op_share_deny, stp);
  3447. fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
  3448. if (!fp->fi_fds[oflag]) {
  3449. spin_unlock(&fp->fi_lock);
  3450. status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
  3451. if (status)
  3452. goto out_put_access;
  3453. spin_lock(&fp->fi_lock);
  3454. if (!fp->fi_fds[oflag]) {
  3455. fp->fi_fds[oflag] = filp;
  3456. filp = NULL;
  3457. }
  3458. }
  3459. spin_unlock(&fp->fi_lock);
  3460. if (filp)
  3461. fput(filp);
  3462. status = nfsd4_truncate(rqstp, cur_fh, open);
  3463. if (status)
  3464. goto out_put_access;
  3465. out:
  3466. return status;
  3467. out_put_access:
  3468. stp->st_access_bmap = old_access_bmap;
  3469. nfs4_file_put_access(fp, open->op_share_access);
  3470. reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
  3471. goto out;
  3472. }
  3473. static __be32
  3474. 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)
  3475. {
  3476. __be32 status;
  3477. unsigned char old_deny_bmap = stp->st_deny_bmap;
  3478. if (!test_access(open->op_share_access, stp))
  3479. return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
  3480. /* test and set deny mode */
  3481. spin_lock(&fp->fi_lock);
  3482. status = nfs4_file_check_deny(fp, open->op_share_deny);
  3483. if (status == nfs_ok) {
  3484. set_deny(open->op_share_deny, stp);
  3485. fp->fi_share_deny |=
  3486. (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
  3487. }
  3488. spin_unlock(&fp->fi_lock);
  3489. if (status != nfs_ok)
  3490. return status;
  3491. status = nfsd4_truncate(rqstp, cur_fh, open);
  3492. if (status != nfs_ok)
  3493. reset_union_bmap_deny(old_deny_bmap, stp);
  3494. return status;
  3495. }
  3496. /* Should we give out recallable state?: */
  3497. static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
  3498. {
  3499. if (clp->cl_cb_state == NFSD4_CB_UP)
  3500. return true;
  3501. /*
  3502. * In the sessions case, since we don't have to establish a
  3503. * separate connection for callbacks, we assume it's OK
  3504. * until we hear otherwise:
  3505. */
  3506. return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
  3507. }
  3508. static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
  3509. {
  3510. struct file_lock *fl;
  3511. fl = locks_alloc_lock();
  3512. if (!fl)
  3513. return NULL;
  3514. fl->fl_lmops = &nfsd_lease_mng_ops;
  3515. fl->fl_flags = FL_DELEG;
  3516. fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
  3517. fl->fl_end = OFFSET_MAX;
  3518. fl->fl_owner = (fl_owner_t)fp;
  3519. fl->fl_pid = current->tgid;
  3520. return fl;
  3521. }
  3522. /**
  3523. * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
  3524. * @dp: a pointer to the nfs4_delegation we're adding.
  3525. *
  3526. * Return:
  3527. * On success: Return code will be 0 on success.
  3528. *
  3529. * On error: -EAGAIN if there was an existing delegation.
  3530. * nonzero if there is an error in other cases.
  3531. *
  3532. */
  3533. static int nfs4_setlease(struct nfs4_delegation *dp)
  3534. {
  3535. struct nfs4_file *fp = dp->dl_stid.sc_file;
  3536. struct file_lock *fl;
  3537. struct file *filp;
  3538. int status = 0;
  3539. fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
  3540. if (!fl)
  3541. return -ENOMEM;
  3542. filp = find_readable_file(fp);
  3543. if (!filp) {
  3544. /* We should always have a readable file here */
  3545. WARN_ON_ONCE(1);
  3546. locks_free_lock(fl);
  3547. return -EBADF;
  3548. }
  3549. fl->fl_file = filp;
  3550. status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
  3551. if (fl)
  3552. locks_free_lock(fl);
  3553. if (status)
  3554. goto out_fput;
  3555. spin_lock(&state_lock);
  3556. spin_lock(&fp->fi_lock);
  3557. /* Did the lease get broken before we took the lock? */
  3558. status = -EAGAIN;
  3559. if (fp->fi_had_conflict)
  3560. goto out_unlock;
  3561. /* Race breaker */
  3562. if (fp->fi_deleg_file) {
  3563. status = hash_delegation_locked(dp, fp);
  3564. goto out_unlock;
  3565. }
  3566. fp->fi_deleg_file = filp;
  3567. fp->fi_delegees = 0;
  3568. status = hash_delegation_locked(dp, fp);
  3569. spin_unlock(&fp->fi_lock);
  3570. spin_unlock(&state_lock);
  3571. if (status) {
  3572. /* Should never happen, this is a new fi_deleg_file */
  3573. WARN_ON_ONCE(1);
  3574. goto out_fput;
  3575. }
  3576. return 0;
  3577. out_unlock:
  3578. spin_unlock(&fp->fi_lock);
  3579. spin_unlock(&state_lock);
  3580. out_fput:
  3581. fput(filp);
  3582. return status;
  3583. }
  3584. static struct nfs4_delegation *
  3585. nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
  3586. struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
  3587. {
  3588. int status;
  3589. struct nfs4_delegation *dp;
  3590. if (fp->fi_had_conflict)
  3591. return ERR_PTR(-EAGAIN);
  3592. spin_lock(&state_lock);
  3593. spin_lock(&fp->fi_lock);
  3594. status = nfs4_get_existing_delegation(clp, fp);
  3595. spin_unlock(&fp->fi_lock);
  3596. spin_unlock(&state_lock);
  3597. if (status)
  3598. return ERR_PTR(status);
  3599. dp = alloc_init_deleg(clp, fh, odstate);
  3600. if (!dp)
  3601. return ERR_PTR(-ENOMEM);
  3602. get_nfs4_file(fp);
  3603. spin_lock(&state_lock);
  3604. spin_lock(&fp->fi_lock);
  3605. dp->dl_stid.sc_file = fp;
  3606. if (!fp->fi_deleg_file) {
  3607. spin_unlock(&fp->fi_lock);
  3608. spin_unlock(&state_lock);
  3609. status = nfs4_setlease(dp);
  3610. goto out;
  3611. }
  3612. if (fp->fi_had_conflict) {
  3613. status = -EAGAIN;
  3614. goto out_unlock;
  3615. }
  3616. status = hash_delegation_locked(dp, fp);
  3617. out_unlock:
  3618. spin_unlock(&fp->fi_lock);
  3619. spin_unlock(&state_lock);
  3620. out:
  3621. if (status) {
  3622. put_clnt_odstate(dp->dl_clnt_odstate);
  3623. nfs4_put_stid(&dp->dl_stid);
  3624. return ERR_PTR(status);
  3625. }
  3626. return dp;
  3627. }
  3628. static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
  3629. {
  3630. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  3631. if (status == -EAGAIN)
  3632. open->op_why_no_deleg = WND4_CONTENTION;
  3633. else {
  3634. open->op_why_no_deleg = WND4_RESOURCE;
  3635. switch (open->op_deleg_want) {
  3636. case NFS4_SHARE_WANT_READ_DELEG:
  3637. case NFS4_SHARE_WANT_WRITE_DELEG:
  3638. case NFS4_SHARE_WANT_ANY_DELEG:
  3639. break;
  3640. case NFS4_SHARE_WANT_CANCEL:
  3641. open->op_why_no_deleg = WND4_CANCELLED;
  3642. break;
  3643. case NFS4_SHARE_WANT_NO_DELEG:
  3644. WARN_ON_ONCE(1);
  3645. }
  3646. }
  3647. }
  3648. /*
  3649. * Attempt to hand out a delegation.
  3650. *
  3651. * Note we don't support write delegations, and won't until the vfs has
  3652. * proper support for them.
  3653. */
  3654. static void
  3655. nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
  3656. struct nfs4_ol_stateid *stp)
  3657. {
  3658. struct nfs4_delegation *dp;
  3659. struct nfs4_openowner *oo = openowner(stp->st_stateowner);
  3660. struct nfs4_client *clp = stp->st_stid.sc_client;
  3661. int cb_up;
  3662. int status = 0;
  3663. cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
  3664. open->op_recall = 0;
  3665. switch (open->op_claim_type) {
  3666. case NFS4_OPEN_CLAIM_PREVIOUS:
  3667. if (!cb_up)
  3668. open->op_recall = 1;
  3669. if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
  3670. goto out_no_deleg;
  3671. break;
  3672. case NFS4_OPEN_CLAIM_NULL:
  3673. case NFS4_OPEN_CLAIM_FH:
  3674. /*
  3675. * Let's not give out any delegations till everyone's
  3676. * had the chance to reclaim theirs, *and* until
  3677. * NLM locks have all been reclaimed:
  3678. */
  3679. if (locks_in_grace(clp->net))
  3680. goto out_no_deleg;
  3681. if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
  3682. goto out_no_deleg;
  3683. /*
  3684. * Also, if the file was opened for write or
  3685. * create, there's a good chance the client's
  3686. * about to write to it, resulting in an
  3687. * immediate recall (since we don't support
  3688. * write delegations):
  3689. */
  3690. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
  3691. goto out_no_deleg;
  3692. if (open->op_create == NFS4_OPEN_CREATE)
  3693. goto out_no_deleg;
  3694. break;
  3695. default:
  3696. goto out_no_deleg;
  3697. }
  3698. dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
  3699. if (IS_ERR(dp))
  3700. goto out_no_deleg;
  3701. memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
  3702. dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
  3703. STATEID_VAL(&dp->dl_stid.sc_stateid));
  3704. open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
  3705. nfs4_put_stid(&dp->dl_stid);
  3706. return;
  3707. out_no_deleg:
  3708. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
  3709. if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
  3710. open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
  3711. dprintk("NFSD: WARNING: refusing delegation reclaim\n");
  3712. open->op_recall = 1;
  3713. }
  3714. /* 4.1 client asking for a delegation? */
  3715. if (open->op_deleg_want)
  3716. nfsd4_open_deleg_none_ext(open, status);
  3717. return;
  3718. }
  3719. static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
  3720. struct nfs4_delegation *dp)
  3721. {
  3722. if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
  3723. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  3724. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  3725. open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
  3726. } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
  3727. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  3728. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  3729. open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
  3730. }
  3731. /* Otherwise the client must be confused wanting a delegation
  3732. * it already has, therefore we don't return
  3733. * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
  3734. */
  3735. }
  3736. __be32
  3737. nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  3738. {
  3739. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  3740. struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
  3741. struct nfs4_file *fp = NULL;
  3742. struct nfs4_ol_stateid *stp = NULL;
  3743. struct nfs4_ol_stateid *swapstp = NULL;
  3744. struct nfs4_delegation *dp = NULL;
  3745. __be32 status;
  3746. /*
  3747. * Lookup file; if found, lookup stateid and check open request,
  3748. * and check for delegations in the process of being recalled.
  3749. * If not found, create the nfs4_file struct
  3750. */
  3751. fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
  3752. if (fp != open->op_file) {
  3753. status = nfs4_check_deleg(cl, open, &dp);
  3754. if (status)
  3755. goto out;
  3756. spin_lock(&fp->fi_lock);
  3757. stp = nfsd4_find_existing_open(fp, open);
  3758. spin_unlock(&fp->fi_lock);
  3759. } else {
  3760. open->op_file = NULL;
  3761. status = nfserr_bad_stateid;
  3762. if (nfsd4_is_deleg_cur(open))
  3763. goto out;
  3764. }
  3765. /*
  3766. * OPEN the file, or upgrade an existing OPEN.
  3767. * If truncate fails, the OPEN fails.
  3768. */
  3769. if (stp) {
  3770. /* Stateid was found, this is an OPEN upgrade */
  3771. down_read(&stp->st_rwsem);
  3772. status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
  3773. if (status) {
  3774. up_read(&stp->st_rwsem);
  3775. goto out;
  3776. }
  3777. } else {
  3778. stp = open->op_stp;
  3779. open->op_stp = NULL;
  3780. swapstp = init_open_stateid(stp, fp, open);
  3781. if (swapstp) {
  3782. nfs4_put_stid(&stp->st_stid);
  3783. stp = swapstp;
  3784. down_read(&stp->st_rwsem);
  3785. status = nfs4_upgrade_open(rqstp, fp, current_fh,
  3786. stp, open);
  3787. if (status) {
  3788. up_read(&stp->st_rwsem);
  3789. goto out;
  3790. }
  3791. goto upgrade_out;
  3792. }
  3793. down_read(&stp->st_rwsem);
  3794. status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
  3795. if (status) {
  3796. up_read(&stp->st_rwsem);
  3797. release_open_stateid(stp);
  3798. goto out;
  3799. }
  3800. stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
  3801. open->op_odstate);
  3802. if (stp->st_clnt_odstate == open->op_odstate)
  3803. open->op_odstate = NULL;
  3804. }
  3805. upgrade_out:
  3806. nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
  3807. up_read(&stp->st_rwsem);
  3808. if (nfsd4_has_session(&resp->cstate)) {
  3809. if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
  3810. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  3811. open->op_why_no_deleg = WND4_NOT_WANTED;
  3812. goto nodeleg;
  3813. }
  3814. }
  3815. /*
  3816. * Attempt to hand out a delegation. No error return, because the
  3817. * OPEN succeeds even if we fail.
  3818. */
  3819. nfs4_open_delegation(current_fh, open, stp);
  3820. nodeleg:
  3821. status = nfs_ok;
  3822. dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
  3823. STATEID_VAL(&stp->st_stid.sc_stateid));
  3824. out:
  3825. /* 4.1 client trying to upgrade/downgrade delegation? */
  3826. if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
  3827. open->op_deleg_want)
  3828. nfsd4_deleg_xgrade_none_ext(open, dp);
  3829. if (fp)
  3830. put_nfs4_file(fp);
  3831. if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  3832. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  3833. /*
  3834. * To finish the open response, we just need to set the rflags.
  3835. */
  3836. open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
  3837. if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
  3838. !nfsd4_has_session(&resp->cstate))
  3839. open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
  3840. if (dp)
  3841. nfs4_put_stid(&dp->dl_stid);
  3842. if (stp)
  3843. nfs4_put_stid(&stp->st_stid);
  3844. return status;
  3845. }
  3846. void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
  3847. struct nfsd4_open *open)
  3848. {
  3849. if (open->op_openowner) {
  3850. struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
  3851. nfsd4_cstate_assign_replay(cstate, so);
  3852. nfs4_put_stateowner(so);
  3853. }
  3854. if (open->op_file)
  3855. kmem_cache_free(file_slab, open->op_file);
  3856. if (open->op_stp)
  3857. nfs4_put_stid(&open->op_stp->st_stid);
  3858. if (open->op_odstate)
  3859. kmem_cache_free(odstate_slab, open->op_odstate);
  3860. }
  3861. __be32
  3862. nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3863. clientid_t *clid)
  3864. {
  3865. struct nfs4_client *clp;
  3866. __be32 status;
  3867. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3868. dprintk("process_renew(%08x/%08x): starting\n",
  3869. clid->cl_boot, clid->cl_id);
  3870. status = lookup_clientid(clid, cstate, nn);
  3871. if (status)
  3872. goto out;
  3873. clp = cstate->clp;
  3874. status = nfserr_cb_path_down;
  3875. if (!list_empty(&clp->cl_delegations)
  3876. && clp->cl_cb_state != NFSD4_CB_UP)
  3877. goto out;
  3878. status = nfs_ok;
  3879. out:
  3880. return status;
  3881. }
  3882. void
  3883. nfsd4_end_grace(struct nfsd_net *nn)
  3884. {
  3885. /* do nothing if grace period already ended */
  3886. if (nn->grace_ended)
  3887. return;
  3888. dprintk("NFSD: end of grace period\n");
  3889. nn->grace_ended = true;
  3890. /*
  3891. * If the server goes down again right now, an NFSv4
  3892. * client will still be allowed to reclaim after it comes back up,
  3893. * even if it hasn't yet had a chance to reclaim state this time.
  3894. *
  3895. */
  3896. nfsd4_record_grace_done(nn);
  3897. /*
  3898. * At this point, NFSv4 clients can still reclaim. But if the
  3899. * server crashes, any that have not yet reclaimed will be out
  3900. * of luck on the next boot.
  3901. *
  3902. * (NFSv4.1+ clients are considered to have reclaimed once they
  3903. * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
  3904. * have reclaimed after their first OPEN.)
  3905. */
  3906. locks_end_grace(&nn->nfsd4_manager);
  3907. /*
  3908. * At this point, and once lockd and/or any other containers
  3909. * exit their grace period, further reclaims will fail and
  3910. * regular locking can resume.
  3911. */
  3912. }
  3913. static time_t
  3914. nfs4_laundromat(struct nfsd_net *nn)
  3915. {
  3916. struct nfs4_client *clp;
  3917. struct nfs4_openowner *oo;
  3918. struct nfs4_delegation *dp;
  3919. struct nfs4_ol_stateid *stp;
  3920. struct list_head *pos, *next, reaplist;
  3921. time_t cutoff = get_seconds() - nn->nfsd4_lease;
  3922. time_t t, new_timeo = nn->nfsd4_lease;
  3923. dprintk("NFSD: laundromat service - starting\n");
  3924. nfsd4_end_grace(nn);
  3925. INIT_LIST_HEAD(&reaplist);
  3926. spin_lock(&nn->client_lock);
  3927. list_for_each_safe(pos, next, &nn->client_lru) {
  3928. clp = list_entry(pos, struct nfs4_client, cl_lru);
  3929. if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
  3930. t = clp->cl_time - cutoff;
  3931. new_timeo = min(new_timeo, t);
  3932. break;
  3933. }
  3934. if (mark_client_expired_locked(clp)) {
  3935. dprintk("NFSD: client in use (clientid %08x)\n",
  3936. clp->cl_clientid.cl_id);
  3937. continue;
  3938. }
  3939. list_add(&clp->cl_lru, &reaplist);
  3940. }
  3941. spin_unlock(&nn->client_lock);
  3942. list_for_each_safe(pos, next, &reaplist) {
  3943. clp = list_entry(pos, struct nfs4_client, cl_lru);
  3944. dprintk("NFSD: purging unused client (clientid %08x)\n",
  3945. clp->cl_clientid.cl_id);
  3946. list_del_init(&clp->cl_lru);
  3947. expire_client(clp);
  3948. }
  3949. spin_lock(&state_lock);
  3950. list_for_each_safe(pos, next, &nn->del_recall_lru) {
  3951. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  3952. if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
  3953. t = dp->dl_time - cutoff;
  3954. new_timeo = min(new_timeo, t);
  3955. break;
  3956. }
  3957. WARN_ON(!unhash_delegation_locked(dp));
  3958. list_add(&dp->dl_recall_lru, &reaplist);
  3959. }
  3960. spin_unlock(&state_lock);
  3961. while (!list_empty(&reaplist)) {
  3962. dp = list_first_entry(&reaplist, struct nfs4_delegation,
  3963. dl_recall_lru);
  3964. list_del_init(&dp->dl_recall_lru);
  3965. revoke_delegation(dp);
  3966. }
  3967. spin_lock(&nn->client_lock);
  3968. while (!list_empty(&nn->close_lru)) {
  3969. oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
  3970. oo_close_lru);
  3971. if (time_after((unsigned long)oo->oo_time,
  3972. (unsigned long)cutoff)) {
  3973. t = oo->oo_time - cutoff;
  3974. new_timeo = min(new_timeo, t);
  3975. break;
  3976. }
  3977. list_del_init(&oo->oo_close_lru);
  3978. stp = oo->oo_last_closed_stid;
  3979. oo->oo_last_closed_stid = NULL;
  3980. spin_unlock(&nn->client_lock);
  3981. nfs4_put_stid(&stp->st_stid);
  3982. spin_lock(&nn->client_lock);
  3983. }
  3984. spin_unlock(&nn->client_lock);
  3985. new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
  3986. return new_timeo;
  3987. }
  3988. static struct workqueue_struct *laundry_wq;
  3989. static void laundromat_main(struct work_struct *);
  3990. static void
  3991. laundromat_main(struct work_struct *laundry)
  3992. {
  3993. time_t t;
  3994. struct delayed_work *dwork = to_delayed_work(laundry);
  3995. struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
  3996. laundromat_work);
  3997. t = nfs4_laundromat(nn);
  3998. dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
  3999. queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
  4000. }
  4001. static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
  4002. {
  4003. if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
  4004. return nfserr_bad_stateid;
  4005. return nfs_ok;
  4006. }
  4007. static inline int
  4008. access_permit_read(struct nfs4_ol_stateid *stp)
  4009. {
  4010. return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
  4011. test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
  4012. test_access(NFS4_SHARE_ACCESS_WRITE, stp);
  4013. }
  4014. static inline int
  4015. access_permit_write(struct nfs4_ol_stateid *stp)
  4016. {
  4017. return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
  4018. test_access(NFS4_SHARE_ACCESS_BOTH, stp);
  4019. }
  4020. static
  4021. __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
  4022. {
  4023. __be32 status = nfserr_openmode;
  4024. /* For lock stateid's, we test the parent open, not the lock: */
  4025. if (stp->st_openstp)
  4026. stp = stp->st_openstp;
  4027. if ((flags & WR_STATE) && !access_permit_write(stp))
  4028. goto out;
  4029. if ((flags & RD_STATE) && !access_permit_read(stp))
  4030. goto out;
  4031. status = nfs_ok;
  4032. out:
  4033. return status;
  4034. }
  4035. static inline __be32
  4036. check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
  4037. {
  4038. if (ONE_STATEID(stateid) && (flags & RD_STATE))
  4039. return nfs_ok;
  4040. else if (opens_in_grace(net)) {
  4041. /* Answer in remaining cases depends on existence of
  4042. * conflicting state; so we must wait out the grace period. */
  4043. return nfserr_grace;
  4044. } else if (flags & WR_STATE)
  4045. return nfs4_share_conflict(current_fh,
  4046. NFS4_SHARE_DENY_WRITE);
  4047. else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
  4048. return nfs4_share_conflict(current_fh,
  4049. NFS4_SHARE_DENY_READ);
  4050. }
  4051. /*
  4052. * Allow READ/WRITE during grace period on recovered state only for files
  4053. * that are not able to provide mandatory locking.
  4054. */
  4055. static inline int
  4056. grace_disallows_io(struct net *net, struct inode *inode)
  4057. {
  4058. return opens_in_grace(net) && mandatory_lock(inode);
  4059. }
  4060. static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
  4061. {
  4062. /*
  4063. * When sessions are used the stateid generation number is ignored
  4064. * when it is zero.
  4065. */
  4066. if (has_session && in->si_generation == 0)
  4067. return nfs_ok;
  4068. if (in->si_generation == ref->si_generation)
  4069. return nfs_ok;
  4070. /* If the client sends us a stateid from the future, it's buggy: */
  4071. if (nfsd4_stateid_generation_after(in, ref))
  4072. return nfserr_bad_stateid;
  4073. /*
  4074. * However, we could see a stateid from the past, even from a
  4075. * non-buggy client. For example, if the client sends a lock
  4076. * while some IO is outstanding, the lock may bump si_generation
  4077. * while the IO is still in flight. The client could avoid that
  4078. * situation by waiting for responses on all the IO requests,
  4079. * but better performance may result in retrying IO that
  4080. * receives an old_stateid error if requests are rarely
  4081. * reordered in flight:
  4082. */
  4083. return nfserr_old_stateid;
  4084. }
  4085. static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
  4086. {
  4087. if (ols->st_stateowner->so_is_open_owner &&
  4088. !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  4089. return nfserr_bad_stateid;
  4090. return nfs_ok;
  4091. }
  4092. static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
  4093. {
  4094. struct nfs4_stid *s;
  4095. __be32 status = nfserr_bad_stateid;
  4096. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  4097. return status;
  4098. /* Client debugging aid. */
  4099. if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
  4100. char addr_str[INET6_ADDRSTRLEN];
  4101. rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
  4102. sizeof(addr_str));
  4103. pr_warn_ratelimited("NFSD: client %s testing state ID "
  4104. "with incorrect client ID\n", addr_str);
  4105. return status;
  4106. }
  4107. spin_lock(&cl->cl_lock);
  4108. s = find_stateid_locked(cl, stateid);
  4109. if (!s)
  4110. goto out_unlock;
  4111. status = check_stateid_generation(stateid, &s->sc_stateid, 1);
  4112. if (status)
  4113. goto out_unlock;
  4114. switch (s->sc_type) {
  4115. case NFS4_DELEG_STID:
  4116. status = nfs_ok;
  4117. break;
  4118. case NFS4_REVOKED_DELEG_STID:
  4119. status = nfserr_deleg_revoked;
  4120. break;
  4121. case NFS4_OPEN_STID:
  4122. case NFS4_LOCK_STID:
  4123. status = nfsd4_check_openowner_confirmed(openlockstateid(s));
  4124. break;
  4125. default:
  4126. printk("unknown stateid type %x\n", s->sc_type);
  4127. /* Fallthrough */
  4128. case NFS4_CLOSED_STID:
  4129. case NFS4_CLOSED_DELEG_STID:
  4130. status = nfserr_bad_stateid;
  4131. }
  4132. out_unlock:
  4133. spin_unlock(&cl->cl_lock);
  4134. return status;
  4135. }
  4136. __be32
  4137. nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
  4138. stateid_t *stateid, unsigned char typemask,
  4139. struct nfs4_stid **s, struct nfsd_net *nn)
  4140. {
  4141. __be32 status;
  4142. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
  4143. return nfserr_bad_stateid;
  4144. status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
  4145. if (status == nfserr_stale_clientid) {
  4146. if (cstate->session)
  4147. return nfserr_bad_stateid;
  4148. return nfserr_stale_stateid;
  4149. }
  4150. if (status)
  4151. return status;
  4152. *s = find_stateid_by_type(cstate->clp, stateid, typemask);
  4153. if (!*s)
  4154. return nfserr_bad_stateid;
  4155. return nfs_ok;
  4156. }
  4157. static struct file *
  4158. nfs4_find_file(struct nfs4_stid *s, int flags)
  4159. {
  4160. if (!s)
  4161. return NULL;
  4162. switch (s->sc_type) {
  4163. case NFS4_DELEG_STID:
  4164. if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
  4165. return NULL;
  4166. return get_file(s->sc_file->fi_deleg_file);
  4167. case NFS4_OPEN_STID:
  4168. case NFS4_LOCK_STID:
  4169. if (flags & RD_STATE)
  4170. return find_readable_file(s->sc_file);
  4171. else
  4172. return find_writeable_file(s->sc_file);
  4173. break;
  4174. }
  4175. return NULL;
  4176. }
  4177. static __be32
  4178. nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
  4179. {
  4180. __be32 status;
  4181. status = nfsd4_check_openowner_confirmed(ols);
  4182. if (status)
  4183. return status;
  4184. return nfs4_check_openmode(ols, flags);
  4185. }
  4186. static __be32
  4187. nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
  4188. struct file **filpp, bool *tmp_file, int flags)
  4189. {
  4190. int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
  4191. struct file *file;
  4192. __be32 status;
  4193. file = nfs4_find_file(s, flags);
  4194. if (file) {
  4195. status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  4196. acc | NFSD_MAY_OWNER_OVERRIDE);
  4197. if (status) {
  4198. fput(file);
  4199. return status;
  4200. }
  4201. *filpp = file;
  4202. } else {
  4203. status = nfsd_open(rqstp, fhp, S_IFREG, acc, filpp);
  4204. if (status)
  4205. return status;
  4206. if (tmp_file)
  4207. *tmp_file = true;
  4208. }
  4209. return 0;
  4210. }
  4211. /*
  4212. * Checks for stateid operations
  4213. */
  4214. __be32
  4215. nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
  4216. struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
  4217. stateid_t *stateid, int flags, struct file **filpp, bool *tmp_file)
  4218. {
  4219. struct inode *ino = d_inode(fhp->fh_dentry);
  4220. struct net *net = SVC_NET(rqstp);
  4221. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4222. struct nfs4_stid *s = NULL;
  4223. __be32 status;
  4224. if (filpp)
  4225. *filpp = NULL;
  4226. if (tmp_file)
  4227. *tmp_file = false;
  4228. if (grace_disallows_io(net, ino))
  4229. return nfserr_grace;
  4230. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
  4231. status = check_special_stateids(net, fhp, stateid, flags);
  4232. goto done;
  4233. }
  4234. status = nfsd4_lookup_stateid(cstate, stateid,
  4235. NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
  4236. &s, nn);
  4237. if (status)
  4238. return status;
  4239. status = check_stateid_generation(stateid, &s->sc_stateid,
  4240. nfsd4_has_session(cstate));
  4241. if (status)
  4242. goto out;
  4243. switch (s->sc_type) {
  4244. case NFS4_DELEG_STID:
  4245. status = nfs4_check_delegmode(delegstateid(s), flags);
  4246. break;
  4247. case NFS4_OPEN_STID:
  4248. case NFS4_LOCK_STID:
  4249. status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
  4250. break;
  4251. default:
  4252. status = nfserr_bad_stateid;
  4253. break;
  4254. }
  4255. if (status)
  4256. goto out;
  4257. status = nfs4_check_fh(fhp, s);
  4258. done:
  4259. if (!status && filpp)
  4260. status = nfs4_check_file(rqstp, fhp, s, filpp, tmp_file, flags);
  4261. out:
  4262. if (s)
  4263. nfs4_put_stid(s);
  4264. return status;
  4265. }
  4266. /*
  4267. * Test if the stateid is valid
  4268. */
  4269. __be32
  4270. nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4271. struct nfsd4_test_stateid *test_stateid)
  4272. {
  4273. struct nfsd4_test_stateid_id *stateid;
  4274. struct nfs4_client *cl = cstate->session->se_client;
  4275. list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
  4276. stateid->ts_id_status =
  4277. nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
  4278. return nfs_ok;
  4279. }
  4280. __be32
  4281. nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4282. struct nfsd4_free_stateid *free_stateid)
  4283. {
  4284. stateid_t *stateid = &free_stateid->fr_stateid;
  4285. struct nfs4_stid *s;
  4286. struct nfs4_delegation *dp;
  4287. struct nfs4_ol_stateid *stp;
  4288. struct nfs4_client *cl = cstate->session->se_client;
  4289. __be32 ret = nfserr_bad_stateid;
  4290. spin_lock(&cl->cl_lock);
  4291. s = find_stateid_locked(cl, stateid);
  4292. if (!s)
  4293. goto out_unlock;
  4294. switch (s->sc_type) {
  4295. case NFS4_DELEG_STID:
  4296. ret = nfserr_locks_held;
  4297. break;
  4298. case NFS4_OPEN_STID:
  4299. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  4300. if (ret)
  4301. break;
  4302. ret = nfserr_locks_held;
  4303. break;
  4304. case NFS4_LOCK_STID:
  4305. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  4306. if (ret)
  4307. break;
  4308. stp = openlockstateid(s);
  4309. ret = nfserr_locks_held;
  4310. if (check_for_locks(stp->st_stid.sc_file,
  4311. lockowner(stp->st_stateowner)))
  4312. break;
  4313. WARN_ON(!unhash_lock_stateid(stp));
  4314. spin_unlock(&cl->cl_lock);
  4315. nfs4_put_stid(s);
  4316. ret = nfs_ok;
  4317. goto out;
  4318. case NFS4_REVOKED_DELEG_STID:
  4319. dp = delegstateid(s);
  4320. list_del_init(&dp->dl_recall_lru);
  4321. spin_unlock(&cl->cl_lock);
  4322. nfs4_put_stid(s);
  4323. ret = nfs_ok;
  4324. goto out;
  4325. /* Default falls through and returns nfserr_bad_stateid */
  4326. }
  4327. out_unlock:
  4328. spin_unlock(&cl->cl_lock);
  4329. out:
  4330. return ret;
  4331. }
  4332. static inline int
  4333. setlkflg (int type)
  4334. {
  4335. return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
  4336. RD_STATE : WR_STATE;
  4337. }
  4338. static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
  4339. {
  4340. struct svc_fh *current_fh = &cstate->current_fh;
  4341. struct nfs4_stateowner *sop = stp->st_stateowner;
  4342. __be32 status;
  4343. status = nfsd4_check_seqid(cstate, sop, seqid);
  4344. if (status)
  4345. return status;
  4346. if (stp->st_stid.sc_type == NFS4_CLOSED_STID
  4347. || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
  4348. /*
  4349. * "Closed" stateid's exist *only* to return
  4350. * nfserr_replay_me from the previous step, and
  4351. * revoked delegations are kept only for free_stateid.
  4352. */
  4353. return nfserr_bad_stateid;
  4354. down_write(&stp->st_rwsem);
  4355. status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
  4356. if (status == nfs_ok)
  4357. status = nfs4_check_fh(current_fh, &stp->st_stid);
  4358. if (status != nfs_ok)
  4359. up_write(&stp->st_rwsem);
  4360. return status;
  4361. }
  4362. /*
  4363. * Checks for sequence id mutating operations.
  4364. */
  4365. static __be32
  4366. nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  4367. stateid_t *stateid, char typemask,
  4368. struct nfs4_ol_stateid **stpp,
  4369. struct nfsd_net *nn)
  4370. {
  4371. __be32 status;
  4372. struct nfs4_stid *s;
  4373. struct nfs4_ol_stateid *stp = NULL;
  4374. dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
  4375. seqid, STATEID_VAL(stateid));
  4376. *stpp = NULL;
  4377. status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
  4378. if (status)
  4379. return status;
  4380. stp = openlockstateid(s);
  4381. nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
  4382. status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
  4383. if (!status)
  4384. *stpp = stp;
  4385. else
  4386. nfs4_put_stid(&stp->st_stid);
  4387. return status;
  4388. }
  4389. static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  4390. stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
  4391. {
  4392. __be32 status;
  4393. struct nfs4_openowner *oo;
  4394. struct nfs4_ol_stateid *stp;
  4395. status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
  4396. NFS4_OPEN_STID, &stp, nn);
  4397. if (status)
  4398. return status;
  4399. oo = openowner(stp->st_stateowner);
  4400. if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  4401. up_write(&stp->st_rwsem);
  4402. nfs4_put_stid(&stp->st_stid);
  4403. return nfserr_bad_stateid;
  4404. }
  4405. *stpp = stp;
  4406. return nfs_ok;
  4407. }
  4408. __be32
  4409. nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4410. struct nfsd4_open_confirm *oc)
  4411. {
  4412. __be32 status;
  4413. struct nfs4_openowner *oo;
  4414. struct nfs4_ol_stateid *stp;
  4415. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  4416. dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
  4417. cstate->current_fh.fh_dentry);
  4418. status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
  4419. if (status)
  4420. return status;
  4421. status = nfs4_preprocess_seqid_op(cstate,
  4422. oc->oc_seqid, &oc->oc_req_stateid,
  4423. NFS4_OPEN_STID, &stp, nn);
  4424. if (status)
  4425. goto out;
  4426. oo = openowner(stp->st_stateowner);
  4427. status = nfserr_bad_stateid;
  4428. if (oo->oo_flags & NFS4_OO_CONFIRMED) {
  4429. up_write(&stp->st_rwsem);
  4430. goto put_stateid;
  4431. }
  4432. oo->oo_flags |= NFS4_OO_CONFIRMED;
  4433. nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
  4434. up_write(&stp->st_rwsem);
  4435. dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
  4436. __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
  4437. nfsd4_client_record_create(oo->oo_owner.so_client);
  4438. status = nfs_ok;
  4439. put_stateid:
  4440. nfs4_put_stid(&stp->st_stid);
  4441. out:
  4442. nfsd4_bump_seqid(cstate, status);
  4443. return status;
  4444. }
  4445. static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
  4446. {
  4447. if (!test_access(access, stp))
  4448. return;
  4449. nfs4_file_put_access(stp->st_stid.sc_file, access);
  4450. clear_access(access, stp);
  4451. }
  4452. static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
  4453. {
  4454. switch (to_access) {
  4455. case NFS4_SHARE_ACCESS_READ:
  4456. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
  4457. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  4458. break;
  4459. case NFS4_SHARE_ACCESS_WRITE:
  4460. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
  4461. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  4462. break;
  4463. case NFS4_SHARE_ACCESS_BOTH:
  4464. break;
  4465. default:
  4466. WARN_ON_ONCE(1);
  4467. }
  4468. }
  4469. __be32
  4470. nfsd4_open_downgrade(struct svc_rqst *rqstp,
  4471. struct nfsd4_compound_state *cstate,
  4472. struct nfsd4_open_downgrade *od)
  4473. {
  4474. __be32 status;
  4475. struct nfs4_ol_stateid *stp;
  4476. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  4477. dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
  4478. cstate->current_fh.fh_dentry);
  4479. /* We don't yet support WANT bits: */
  4480. if (od->od_deleg_want)
  4481. dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
  4482. od->od_deleg_want);
  4483. status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
  4484. &od->od_stateid, &stp, nn);
  4485. if (status)
  4486. goto out;
  4487. status = nfserr_inval;
  4488. if (!test_access(od->od_share_access, stp)) {
  4489. dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
  4490. stp->st_access_bmap, od->od_share_access);
  4491. goto put_stateid;
  4492. }
  4493. if (!test_deny(od->od_share_deny, stp)) {
  4494. dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
  4495. stp->st_deny_bmap, od->od_share_deny);
  4496. goto put_stateid;
  4497. }
  4498. nfs4_stateid_downgrade(stp, od->od_share_access);
  4499. reset_union_bmap_deny(od->od_share_deny, stp);
  4500. nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
  4501. status = nfs_ok;
  4502. put_stateid:
  4503. up_write(&stp->st_rwsem);
  4504. nfs4_put_stid(&stp->st_stid);
  4505. out:
  4506. nfsd4_bump_seqid(cstate, status);
  4507. return status;
  4508. }
  4509. static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
  4510. {
  4511. struct nfs4_client *clp = s->st_stid.sc_client;
  4512. bool unhashed;
  4513. LIST_HEAD(reaplist);
  4514. s->st_stid.sc_type = NFS4_CLOSED_STID;
  4515. spin_lock(&clp->cl_lock);
  4516. unhashed = unhash_open_stateid(s, &reaplist);
  4517. if (clp->cl_minorversion) {
  4518. if (unhashed)
  4519. put_ol_stateid_locked(s, &reaplist);
  4520. spin_unlock(&clp->cl_lock);
  4521. free_ol_stateid_reaplist(&reaplist);
  4522. } else {
  4523. spin_unlock(&clp->cl_lock);
  4524. free_ol_stateid_reaplist(&reaplist);
  4525. if (unhashed)
  4526. move_to_close_lru(s, clp->net);
  4527. }
  4528. }
  4529. /*
  4530. * nfs4_unlock_state() called after encode
  4531. */
  4532. __be32
  4533. nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4534. struct nfsd4_close *close)
  4535. {
  4536. __be32 status;
  4537. struct nfs4_ol_stateid *stp;
  4538. struct net *net = SVC_NET(rqstp);
  4539. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4540. dprintk("NFSD: nfsd4_close on file %pd\n",
  4541. cstate->current_fh.fh_dentry);
  4542. status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
  4543. &close->cl_stateid,
  4544. NFS4_OPEN_STID|NFS4_CLOSED_STID,
  4545. &stp, nn);
  4546. nfsd4_bump_seqid(cstate, status);
  4547. if (status)
  4548. goto out;
  4549. nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
  4550. up_write(&stp->st_rwsem);
  4551. nfsd4_close_open_stateid(stp);
  4552. /* put reference from nfs4_preprocess_seqid_op */
  4553. nfs4_put_stid(&stp->st_stid);
  4554. out:
  4555. return status;
  4556. }
  4557. __be32
  4558. nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4559. struct nfsd4_delegreturn *dr)
  4560. {
  4561. struct nfs4_delegation *dp;
  4562. stateid_t *stateid = &dr->dr_stateid;
  4563. struct nfs4_stid *s;
  4564. __be32 status;
  4565. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  4566. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  4567. return status;
  4568. status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
  4569. if (status)
  4570. goto out;
  4571. dp = delegstateid(s);
  4572. status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
  4573. if (status)
  4574. goto put_stateid;
  4575. destroy_delegation(dp);
  4576. put_stateid:
  4577. nfs4_put_stid(&dp->dl_stid);
  4578. out:
  4579. return status;
  4580. }
  4581. static inline u64
  4582. end_offset(u64 start, u64 len)
  4583. {
  4584. u64 end;
  4585. end = start + len;
  4586. return end >= start ? end: NFS4_MAX_UINT64;
  4587. }
  4588. /* last octet in a range */
  4589. static inline u64
  4590. last_byte_offset(u64 start, u64 len)
  4591. {
  4592. u64 end;
  4593. WARN_ON_ONCE(!len);
  4594. end = start + len;
  4595. return end > start ? end - 1: NFS4_MAX_UINT64;
  4596. }
  4597. /*
  4598. * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
  4599. * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
  4600. * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
  4601. * locking, this prevents us from being completely protocol-compliant. The
  4602. * real solution to this problem is to start using unsigned file offsets in
  4603. * the VFS, but this is a very deep change!
  4604. */
  4605. static inline void
  4606. nfs4_transform_lock_offset(struct file_lock *lock)
  4607. {
  4608. if (lock->fl_start < 0)
  4609. lock->fl_start = OFFSET_MAX;
  4610. if (lock->fl_end < 0)
  4611. lock->fl_end = OFFSET_MAX;
  4612. }
  4613. static fl_owner_t
  4614. nfsd4_fl_get_owner(fl_owner_t owner)
  4615. {
  4616. struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
  4617. nfs4_get_stateowner(&lo->lo_owner);
  4618. return owner;
  4619. }
  4620. static void
  4621. nfsd4_fl_put_owner(fl_owner_t owner)
  4622. {
  4623. struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
  4624. if (lo)
  4625. nfs4_put_stateowner(&lo->lo_owner);
  4626. }
  4627. static const struct lock_manager_operations nfsd_posix_mng_ops = {
  4628. .lm_get_owner = nfsd4_fl_get_owner,
  4629. .lm_put_owner = nfsd4_fl_put_owner,
  4630. };
  4631. static inline void
  4632. nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
  4633. {
  4634. struct nfs4_lockowner *lo;
  4635. if (fl->fl_lmops == &nfsd_posix_mng_ops) {
  4636. lo = (struct nfs4_lockowner *) fl->fl_owner;
  4637. deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
  4638. lo->lo_owner.so_owner.len, GFP_KERNEL);
  4639. if (!deny->ld_owner.data)
  4640. /* We just don't care that much */
  4641. goto nevermind;
  4642. deny->ld_owner.len = lo->lo_owner.so_owner.len;
  4643. deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
  4644. } else {
  4645. nevermind:
  4646. deny->ld_owner.len = 0;
  4647. deny->ld_owner.data = NULL;
  4648. deny->ld_clientid.cl_boot = 0;
  4649. deny->ld_clientid.cl_id = 0;
  4650. }
  4651. deny->ld_start = fl->fl_start;
  4652. deny->ld_length = NFS4_MAX_UINT64;
  4653. if (fl->fl_end != NFS4_MAX_UINT64)
  4654. deny->ld_length = fl->fl_end - fl->fl_start + 1;
  4655. deny->ld_type = NFS4_READ_LT;
  4656. if (fl->fl_type != F_RDLCK)
  4657. deny->ld_type = NFS4_WRITE_LT;
  4658. }
  4659. static struct nfs4_lockowner *
  4660. find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
  4661. {
  4662. unsigned int strhashval = ownerstr_hashval(owner);
  4663. struct nfs4_stateowner *so;
  4664. lockdep_assert_held(&clp->cl_lock);
  4665. list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
  4666. so_strhash) {
  4667. if (so->so_is_open_owner)
  4668. continue;
  4669. if (same_owner_str(so, owner))
  4670. return lockowner(nfs4_get_stateowner(so));
  4671. }
  4672. return NULL;
  4673. }
  4674. static struct nfs4_lockowner *
  4675. find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
  4676. {
  4677. struct nfs4_lockowner *lo;
  4678. spin_lock(&clp->cl_lock);
  4679. lo = find_lockowner_str_locked(clp, owner);
  4680. spin_unlock(&clp->cl_lock);
  4681. return lo;
  4682. }
  4683. static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
  4684. {
  4685. unhash_lockowner_locked(lockowner(sop));
  4686. }
  4687. static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
  4688. {
  4689. struct nfs4_lockowner *lo = lockowner(sop);
  4690. kmem_cache_free(lockowner_slab, lo);
  4691. }
  4692. static const struct nfs4_stateowner_operations lockowner_ops = {
  4693. .so_unhash = nfs4_unhash_lockowner,
  4694. .so_free = nfs4_free_lockowner,
  4695. };
  4696. /*
  4697. * Alloc a lock owner structure.
  4698. * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
  4699. * occurred.
  4700. *
  4701. * strhashval = ownerstr_hashval
  4702. */
  4703. static struct nfs4_lockowner *
  4704. alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
  4705. struct nfs4_ol_stateid *open_stp,
  4706. struct nfsd4_lock *lock)
  4707. {
  4708. struct nfs4_lockowner *lo, *ret;
  4709. lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
  4710. if (!lo)
  4711. return NULL;
  4712. INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
  4713. lo->lo_owner.so_is_open_owner = 0;
  4714. lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
  4715. lo->lo_owner.so_ops = &lockowner_ops;
  4716. spin_lock(&clp->cl_lock);
  4717. ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
  4718. if (ret == NULL) {
  4719. list_add(&lo->lo_owner.so_strhash,
  4720. &clp->cl_ownerstr_hashtbl[strhashval]);
  4721. ret = lo;
  4722. } else
  4723. nfs4_free_stateowner(&lo->lo_owner);
  4724. spin_unlock(&clp->cl_lock);
  4725. return ret;
  4726. }
  4727. static void
  4728. init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
  4729. struct nfs4_file *fp, struct inode *inode,
  4730. struct nfs4_ol_stateid *open_stp)
  4731. {
  4732. struct nfs4_client *clp = lo->lo_owner.so_client;
  4733. lockdep_assert_held(&clp->cl_lock);
  4734. atomic_inc(&stp->st_stid.sc_count);
  4735. stp->st_stid.sc_type = NFS4_LOCK_STID;
  4736. stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
  4737. get_nfs4_file(fp);
  4738. stp->st_stid.sc_file = fp;
  4739. stp->st_stid.sc_free = nfs4_free_lock_stateid;
  4740. stp->st_access_bmap = 0;
  4741. stp->st_deny_bmap = open_stp->st_deny_bmap;
  4742. stp->st_openstp = open_stp;
  4743. init_rwsem(&stp->st_rwsem);
  4744. list_add(&stp->st_locks, &open_stp->st_locks);
  4745. list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
  4746. spin_lock(&fp->fi_lock);
  4747. list_add(&stp->st_perfile, &fp->fi_stateids);
  4748. spin_unlock(&fp->fi_lock);
  4749. }
  4750. static struct nfs4_ol_stateid *
  4751. find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
  4752. {
  4753. struct nfs4_ol_stateid *lst;
  4754. struct nfs4_client *clp = lo->lo_owner.so_client;
  4755. lockdep_assert_held(&clp->cl_lock);
  4756. list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
  4757. if (lst->st_stid.sc_file == fp) {
  4758. atomic_inc(&lst->st_stid.sc_count);
  4759. return lst;
  4760. }
  4761. }
  4762. return NULL;
  4763. }
  4764. static struct nfs4_ol_stateid *
  4765. find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
  4766. struct inode *inode, struct nfs4_ol_stateid *ost,
  4767. bool *new)
  4768. {
  4769. struct nfs4_stid *ns = NULL;
  4770. struct nfs4_ol_stateid *lst;
  4771. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  4772. struct nfs4_client *clp = oo->oo_owner.so_client;
  4773. spin_lock(&clp->cl_lock);
  4774. lst = find_lock_stateid(lo, fi);
  4775. if (lst == NULL) {
  4776. spin_unlock(&clp->cl_lock);
  4777. ns = nfs4_alloc_stid(clp, stateid_slab);
  4778. if (ns == NULL)
  4779. return NULL;
  4780. spin_lock(&clp->cl_lock);
  4781. lst = find_lock_stateid(lo, fi);
  4782. if (likely(!lst)) {
  4783. lst = openlockstateid(ns);
  4784. init_lock_stateid(lst, lo, fi, inode, ost);
  4785. ns = NULL;
  4786. *new = true;
  4787. }
  4788. }
  4789. spin_unlock(&clp->cl_lock);
  4790. if (ns)
  4791. nfs4_put_stid(ns);
  4792. return lst;
  4793. }
  4794. static int
  4795. check_lock_length(u64 offset, u64 length)
  4796. {
  4797. return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
  4798. (length > ~offset)));
  4799. }
  4800. static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
  4801. {
  4802. struct nfs4_file *fp = lock_stp->st_stid.sc_file;
  4803. lockdep_assert_held(&fp->fi_lock);
  4804. if (test_access(access, lock_stp))
  4805. return;
  4806. __nfs4_file_get_access(fp, access);
  4807. set_access(access, lock_stp);
  4808. }
  4809. static __be32
  4810. lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
  4811. struct nfs4_ol_stateid *ost,
  4812. struct nfsd4_lock *lock,
  4813. struct nfs4_ol_stateid **lst, bool *new)
  4814. {
  4815. __be32 status;
  4816. struct nfs4_file *fi = ost->st_stid.sc_file;
  4817. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  4818. struct nfs4_client *cl = oo->oo_owner.so_client;
  4819. struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
  4820. struct nfs4_lockowner *lo;
  4821. unsigned int strhashval;
  4822. lo = find_lockowner_str(cl, &lock->lk_new_owner);
  4823. if (!lo) {
  4824. strhashval = ownerstr_hashval(&lock->lk_new_owner);
  4825. lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
  4826. if (lo == NULL)
  4827. return nfserr_jukebox;
  4828. } else {
  4829. /* with an existing lockowner, seqids must be the same */
  4830. status = nfserr_bad_seqid;
  4831. if (!cstate->minorversion &&
  4832. lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
  4833. goto out;
  4834. }
  4835. *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
  4836. if (*lst == NULL) {
  4837. status = nfserr_jukebox;
  4838. goto out;
  4839. }
  4840. status = nfs_ok;
  4841. out:
  4842. nfs4_put_stateowner(&lo->lo_owner);
  4843. return status;
  4844. }
  4845. /*
  4846. * LOCK operation
  4847. */
  4848. __be32
  4849. nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  4850. struct nfsd4_lock *lock)
  4851. {
  4852. struct nfs4_openowner *open_sop = NULL;
  4853. struct nfs4_lockowner *lock_sop = NULL;
  4854. struct nfs4_ol_stateid *lock_stp = NULL;
  4855. struct nfs4_ol_stateid *open_stp = NULL;
  4856. struct nfs4_file *fp;
  4857. struct file *filp = NULL;
  4858. struct file_lock *file_lock = NULL;
  4859. struct file_lock *conflock = NULL;
  4860. __be32 status = 0;
  4861. int lkflg;
  4862. int err;
  4863. bool new = false;
  4864. struct net *net = SVC_NET(rqstp);
  4865. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  4866. dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
  4867. (long long) lock->lk_offset,
  4868. (long long) lock->lk_length);
  4869. if (check_lock_length(lock->lk_offset, lock->lk_length))
  4870. return nfserr_inval;
  4871. if ((status = fh_verify(rqstp, &cstate->current_fh,
  4872. S_IFREG, NFSD_MAY_LOCK))) {
  4873. dprintk("NFSD: nfsd4_lock: permission denied!\n");
  4874. return status;
  4875. }
  4876. if (lock->lk_is_new) {
  4877. if (nfsd4_has_session(cstate))
  4878. /* See rfc 5661 18.10.3: given clientid is ignored: */
  4879. memcpy(&lock->lk_new_clientid,
  4880. &cstate->session->se_client->cl_clientid,
  4881. sizeof(clientid_t));
  4882. status = nfserr_stale_clientid;
  4883. if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
  4884. goto out;
  4885. /* validate and update open stateid and open seqid */
  4886. status = nfs4_preprocess_confirmed_seqid_op(cstate,
  4887. lock->lk_new_open_seqid,
  4888. &lock->lk_new_open_stateid,
  4889. &open_stp, nn);
  4890. if (status)
  4891. goto out;
  4892. up_write(&open_stp->st_rwsem);
  4893. open_sop = openowner(open_stp->st_stateowner);
  4894. status = nfserr_bad_stateid;
  4895. if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
  4896. &lock->lk_new_clientid))
  4897. goto out;
  4898. status = lookup_or_create_lock_state(cstate, open_stp, lock,
  4899. &lock_stp, &new);
  4900. if (status == nfs_ok)
  4901. down_write(&lock_stp->st_rwsem);
  4902. } else {
  4903. status = nfs4_preprocess_seqid_op(cstate,
  4904. lock->lk_old_lock_seqid,
  4905. &lock->lk_old_lock_stateid,
  4906. NFS4_LOCK_STID, &lock_stp, nn);
  4907. }
  4908. if (status)
  4909. goto out;
  4910. lock_sop = lockowner(lock_stp->st_stateowner);
  4911. lkflg = setlkflg(lock->lk_type);
  4912. status = nfs4_check_openmode(lock_stp, lkflg);
  4913. if (status)
  4914. goto out;
  4915. status = nfserr_grace;
  4916. if (locks_in_grace(net) && !lock->lk_reclaim)
  4917. goto out;
  4918. status = nfserr_no_grace;
  4919. if (!locks_in_grace(net) && lock->lk_reclaim)
  4920. goto out;
  4921. file_lock = locks_alloc_lock();
  4922. if (!file_lock) {
  4923. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  4924. status = nfserr_jukebox;
  4925. goto out;
  4926. }
  4927. fp = lock_stp->st_stid.sc_file;
  4928. switch (lock->lk_type) {
  4929. case NFS4_READ_LT:
  4930. case NFS4_READW_LT:
  4931. spin_lock(&fp->fi_lock);
  4932. filp = find_readable_file_locked(fp);
  4933. if (filp)
  4934. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
  4935. spin_unlock(&fp->fi_lock);
  4936. file_lock->fl_type = F_RDLCK;
  4937. break;
  4938. case NFS4_WRITE_LT:
  4939. case NFS4_WRITEW_LT:
  4940. spin_lock(&fp->fi_lock);
  4941. filp = find_writeable_file_locked(fp);
  4942. if (filp)
  4943. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
  4944. spin_unlock(&fp->fi_lock);
  4945. file_lock->fl_type = F_WRLCK;
  4946. break;
  4947. default:
  4948. status = nfserr_inval;
  4949. goto out;
  4950. }
  4951. if (!filp) {
  4952. status = nfserr_openmode;
  4953. goto out;
  4954. }
  4955. file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
  4956. file_lock->fl_pid = current->tgid;
  4957. file_lock->fl_file = filp;
  4958. file_lock->fl_flags = FL_POSIX;
  4959. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  4960. file_lock->fl_start = lock->lk_offset;
  4961. file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
  4962. nfs4_transform_lock_offset(file_lock);
  4963. conflock = locks_alloc_lock();
  4964. if (!conflock) {
  4965. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  4966. status = nfserr_jukebox;
  4967. goto out;
  4968. }
  4969. err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
  4970. switch (-err) {
  4971. case 0: /* success! */
  4972. nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
  4973. status = 0;
  4974. break;
  4975. case (EAGAIN): /* conflock holds conflicting lock */
  4976. status = nfserr_denied;
  4977. dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
  4978. nfs4_set_lock_denied(conflock, &lock->lk_denied);
  4979. break;
  4980. case (EDEADLK):
  4981. status = nfserr_deadlock;
  4982. break;
  4983. default:
  4984. dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
  4985. status = nfserrno(err);
  4986. break;
  4987. }
  4988. out:
  4989. if (filp)
  4990. fput(filp);
  4991. if (lock_stp) {
  4992. /* Bump seqid manually if the 4.0 replay owner is openowner */
  4993. if (cstate->replay_owner &&
  4994. cstate->replay_owner != &lock_sop->lo_owner &&
  4995. seqid_mutating_err(ntohl(status)))
  4996. lock_sop->lo_owner.so_seqid++;
  4997. up_write(&lock_stp->st_rwsem);
  4998. /*
  4999. * If this is a new, never-before-used stateid, and we are
  5000. * returning an error, then just go ahead and release it.
  5001. */
  5002. if (status && new)
  5003. release_lock_stateid(lock_stp);
  5004. nfs4_put_stid(&lock_stp->st_stid);
  5005. }
  5006. if (open_stp)
  5007. nfs4_put_stid(&open_stp->st_stid);
  5008. nfsd4_bump_seqid(cstate, status);
  5009. if (file_lock)
  5010. locks_free_lock(file_lock);
  5011. if (conflock)
  5012. locks_free_lock(conflock);
  5013. return status;
  5014. }
  5015. /*
  5016. * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
  5017. * so we do a temporary open here just to get an open file to pass to
  5018. * vfs_test_lock. (Arguably perhaps test_lock should be done with an
  5019. * inode operation.)
  5020. */
  5021. static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
  5022. {
  5023. struct file *file;
  5024. __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  5025. if (!err) {
  5026. err = nfserrno(vfs_test_lock(file, lock));
  5027. fput(file);
  5028. }
  5029. return err;
  5030. }
  5031. /*
  5032. * LOCKT operation
  5033. */
  5034. __be32
  5035. nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  5036. struct nfsd4_lockt *lockt)
  5037. {
  5038. struct file_lock *file_lock = NULL;
  5039. struct nfs4_lockowner *lo = NULL;
  5040. __be32 status;
  5041. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  5042. if (locks_in_grace(SVC_NET(rqstp)))
  5043. return nfserr_grace;
  5044. if (check_lock_length(lockt->lt_offset, lockt->lt_length))
  5045. return nfserr_inval;
  5046. if (!nfsd4_has_session(cstate)) {
  5047. status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
  5048. if (status)
  5049. goto out;
  5050. }
  5051. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  5052. goto out;
  5053. file_lock = locks_alloc_lock();
  5054. if (!file_lock) {
  5055. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  5056. status = nfserr_jukebox;
  5057. goto out;
  5058. }
  5059. switch (lockt->lt_type) {
  5060. case NFS4_READ_LT:
  5061. case NFS4_READW_LT:
  5062. file_lock->fl_type = F_RDLCK;
  5063. break;
  5064. case NFS4_WRITE_LT:
  5065. case NFS4_WRITEW_LT:
  5066. file_lock->fl_type = F_WRLCK;
  5067. break;
  5068. default:
  5069. dprintk("NFSD: nfs4_lockt: bad lock type!\n");
  5070. status = nfserr_inval;
  5071. goto out;
  5072. }
  5073. lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
  5074. if (lo)
  5075. file_lock->fl_owner = (fl_owner_t)lo;
  5076. file_lock->fl_pid = current->tgid;
  5077. file_lock->fl_flags = FL_POSIX;
  5078. file_lock->fl_start = lockt->lt_offset;
  5079. file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
  5080. nfs4_transform_lock_offset(file_lock);
  5081. status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
  5082. if (status)
  5083. goto out;
  5084. if (file_lock->fl_type != F_UNLCK) {
  5085. status = nfserr_denied;
  5086. nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
  5087. }
  5088. out:
  5089. if (lo)
  5090. nfs4_put_stateowner(&lo->lo_owner);
  5091. if (file_lock)
  5092. locks_free_lock(file_lock);
  5093. return status;
  5094. }
  5095. __be32
  5096. nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  5097. struct nfsd4_locku *locku)
  5098. {
  5099. struct nfs4_ol_stateid *stp;
  5100. struct file *filp = NULL;
  5101. struct file_lock *file_lock = NULL;
  5102. __be32 status;
  5103. int err;
  5104. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  5105. dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
  5106. (long long) locku->lu_offset,
  5107. (long long) locku->lu_length);
  5108. if (check_lock_length(locku->lu_offset, locku->lu_length))
  5109. return nfserr_inval;
  5110. status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
  5111. &locku->lu_stateid, NFS4_LOCK_STID,
  5112. &stp, nn);
  5113. if (status)
  5114. goto out;
  5115. filp = find_any_file(stp->st_stid.sc_file);
  5116. if (!filp) {
  5117. status = nfserr_lock_range;
  5118. goto put_stateid;
  5119. }
  5120. file_lock = locks_alloc_lock();
  5121. if (!file_lock) {
  5122. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  5123. status = nfserr_jukebox;
  5124. goto fput;
  5125. }
  5126. file_lock->fl_type = F_UNLCK;
  5127. file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
  5128. file_lock->fl_pid = current->tgid;
  5129. file_lock->fl_file = filp;
  5130. file_lock->fl_flags = FL_POSIX;
  5131. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  5132. file_lock->fl_start = locku->lu_offset;
  5133. file_lock->fl_end = last_byte_offset(locku->lu_offset,
  5134. locku->lu_length);
  5135. nfs4_transform_lock_offset(file_lock);
  5136. err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
  5137. if (err) {
  5138. dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
  5139. goto out_nfserr;
  5140. }
  5141. nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
  5142. fput:
  5143. fput(filp);
  5144. put_stateid:
  5145. up_write(&stp->st_rwsem);
  5146. nfs4_put_stid(&stp->st_stid);
  5147. out:
  5148. nfsd4_bump_seqid(cstate, status);
  5149. if (file_lock)
  5150. locks_free_lock(file_lock);
  5151. return status;
  5152. out_nfserr:
  5153. status = nfserrno(err);
  5154. goto fput;
  5155. }
  5156. /*
  5157. * returns
  5158. * true: locks held by lockowner
  5159. * false: no locks held by lockowner
  5160. */
  5161. static bool
  5162. check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
  5163. {
  5164. struct file_lock *fl;
  5165. int status = false;
  5166. struct file *filp = find_any_file(fp);
  5167. struct inode *inode;
  5168. struct file_lock_context *flctx;
  5169. if (!filp) {
  5170. /* Any valid lock stateid should have some sort of access */
  5171. WARN_ON_ONCE(1);
  5172. return status;
  5173. }
  5174. inode = file_inode(filp);
  5175. flctx = inode->i_flctx;
  5176. if (flctx && !list_empty_careful(&flctx->flc_posix)) {
  5177. spin_lock(&flctx->flc_lock);
  5178. list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
  5179. if (fl->fl_owner == (fl_owner_t)lowner) {
  5180. status = true;
  5181. break;
  5182. }
  5183. }
  5184. spin_unlock(&flctx->flc_lock);
  5185. }
  5186. fput(filp);
  5187. return status;
  5188. }
  5189. __be32
  5190. nfsd4_release_lockowner(struct svc_rqst *rqstp,
  5191. struct nfsd4_compound_state *cstate,
  5192. struct nfsd4_release_lockowner *rlockowner)
  5193. {
  5194. clientid_t *clid = &rlockowner->rl_clientid;
  5195. struct nfs4_stateowner *sop;
  5196. struct nfs4_lockowner *lo = NULL;
  5197. struct nfs4_ol_stateid *stp;
  5198. struct xdr_netobj *owner = &rlockowner->rl_owner;
  5199. unsigned int hashval = ownerstr_hashval(owner);
  5200. __be32 status;
  5201. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  5202. struct nfs4_client *clp;
  5203. dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
  5204. clid->cl_boot, clid->cl_id);
  5205. status = lookup_clientid(clid, cstate, nn);
  5206. if (status)
  5207. return status;
  5208. clp = cstate->clp;
  5209. /* Find the matching lock stateowner */
  5210. spin_lock(&clp->cl_lock);
  5211. list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
  5212. so_strhash) {
  5213. if (sop->so_is_open_owner || !same_owner_str(sop, owner))
  5214. continue;
  5215. /* see if there are still any locks associated with it */
  5216. lo = lockowner(sop);
  5217. list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
  5218. if (check_for_locks(stp->st_stid.sc_file, lo)) {
  5219. status = nfserr_locks_held;
  5220. spin_unlock(&clp->cl_lock);
  5221. return status;
  5222. }
  5223. }
  5224. nfs4_get_stateowner(sop);
  5225. break;
  5226. }
  5227. spin_unlock(&clp->cl_lock);
  5228. if (lo)
  5229. release_lockowner(lo);
  5230. return status;
  5231. }
  5232. static inline struct nfs4_client_reclaim *
  5233. alloc_reclaim(void)
  5234. {
  5235. return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
  5236. }
  5237. bool
  5238. nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
  5239. {
  5240. struct nfs4_client_reclaim *crp;
  5241. crp = nfsd4_find_reclaim_client(name, nn);
  5242. return (crp && crp->cr_clp);
  5243. }
  5244. /*
  5245. * failure => all reset bets are off, nfserr_no_grace...
  5246. */
  5247. struct nfs4_client_reclaim *
  5248. nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
  5249. {
  5250. unsigned int strhashval;
  5251. struct nfs4_client_reclaim *crp;
  5252. dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
  5253. crp = alloc_reclaim();
  5254. if (crp) {
  5255. strhashval = clientstr_hashval(name);
  5256. INIT_LIST_HEAD(&crp->cr_strhash);
  5257. list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
  5258. memcpy(crp->cr_recdir, name, HEXDIR_LEN);
  5259. crp->cr_clp = NULL;
  5260. nn->reclaim_str_hashtbl_size++;
  5261. }
  5262. return crp;
  5263. }
  5264. void
  5265. nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
  5266. {
  5267. list_del(&crp->cr_strhash);
  5268. kfree(crp);
  5269. nn->reclaim_str_hashtbl_size--;
  5270. }
  5271. void
  5272. nfs4_release_reclaim(struct nfsd_net *nn)
  5273. {
  5274. struct nfs4_client_reclaim *crp = NULL;
  5275. int i;
  5276. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  5277. while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
  5278. crp = list_entry(nn->reclaim_str_hashtbl[i].next,
  5279. struct nfs4_client_reclaim, cr_strhash);
  5280. nfs4_remove_reclaim_record(crp, nn);
  5281. }
  5282. }
  5283. WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
  5284. }
  5285. /*
  5286. * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
  5287. struct nfs4_client_reclaim *
  5288. nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
  5289. {
  5290. unsigned int strhashval;
  5291. struct nfs4_client_reclaim *crp = NULL;
  5292. dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
  5293. strhashval = clientstr_hashval(recdir);
  5294. list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
  5295. if (same_name(crp->cr_recdir, recdir)) {
  5296. return crp;
  5297. }
  5298. }
  5299. return NULL;
  5300. }
  5301. /*
  5302. * Called from OPEN. Look for clientid in reclaim list.
  5303. */
  5304. __be32
  5305. nfs4_check_open_reclaim(clientid_t *clid,
  5306. struct nfsd4_compound_state *cstate,
  5307. struct nfsd_net *nn)
  5308. {
  5309. __be32 status;
  5310. /* find clientid in conf_id_hashtbl */
  5311. status = lookup_clientid(clid, cstate, nn);
  5312. if (status)
  5313. return nfserr_reclaim_bad;
  5314. if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
  5315. return nfserr_no_grace;
  5316. if (nfsd4_client_record_check(cstate->clp))
  5317. return nfserr_reclaim_bad;
  5318. return nfs_ok;
  5319. }
  5320. #ifdef CONFIG_NFSD_FAULT_INJECTION
  5321. static inline void
  5322. put_client(struct nfs4_client *clp)
  5323. {
  5324. atomic_dec(&clp->cl_refcount);
  5325. }
  5326. static struct nfs4_client *
  5327. nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
  5328. {
  5329. struct nfs4_client *clp;
  5330. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5331. nfsd_net_id);
  5332. if (!nfsd_netns_ready(nn))
  5333. return NULL;
  5334. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  5335. if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
  5336. return clp;
  5337. }
  5338. return NULL;
  5339. }
  5340. u64
  5341. nfsd_inject_print_clients(void)
  5342. {
  5343. struct nfs4_client *clp;
  5344. u64 count = 0;
  5345. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5346. nfsd_net_id);
  5347. char buf[INET6_ADDRSTRLEN];
  5348. if (!nfsd_netns_ready(nn))
  5349. return 0;
  5350. spin_lock(&nn->client_lock);
  5351. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  5352. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  5353. pr_info("NFS Client: %s\n", buf);
  5354. ++count;
  5355. }
  5356. spin_unlock(&nn->client_lock);
  5357. return count;
  5358. }
  5359. u64
  5360. nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
  5361. {
  5362. u64 count = 0;
  5363. struct nfs4_client *clp;
  5364. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5365. nfsd_net_id);
  5366. if (!nfsd_netns_ready(nn))
  5367. return count;
  5368. spin_lock(&nn->client_lock);
  5369. clp = nfsd_find_client(addr, addr_size);
  5370. if (clp) {
  5371. if (mark_client_expired_locked(clp) == nfs_ok)
  5372. ++count;
  5373. else
  5374. clp = NULL;
  5375. }
  5376. spin_unlock(&nn->client_lock);
  5377. if (clp)
  5378. expire_client(clp);
  5379. return count;
  5380. }
  5381. u64
  5382. nfsd_inject_forget_clients(u64 max)
  5383. {
  5384. u64 count = 0;
  5385. struct nfs4_client *clp, *next;
  5386. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5387. nfsd_net_id);
  5388. LIST_HEAD(reaplist);
  5389. if (!nfsd_netns_ready(nn))
  5390. return count;
  5391. spin_lock(&nn->client_lock);
  5392. list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
  5393. if (mark_client_expired_locked(clp) == nfs_ok) {
  5394. list_add(&clp->cl_lru, &reaplist);
  5395. if (max != 0 && ++count >= max)
  5396. break;
  5397. }
  5398. }
  5399. spin_unlock(&nn->client_lock);
  5400. list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
  5401. expire_client(clp);
  5402. return count;
  5403. }
  5404. static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
  5405. const char *type)
  5406. {
  5407. char buf[INET6_ADDRSTRLEN];
  5408. rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
  5409. printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
  5410. }
  5411. static void
  5412. nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
  5413. struct list_head *collect)
  5414. {
  5415. struct nfs4_client *clp = lst->st_stid.sc_client;
  5416. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5417. nfsd_net_id);
  5418. if (!collect)
  5419. return;
  5420. lockdep_assert_held(&nn->client_lock);
  5421. atomic_inc(&clp->cl_refcount);
  5422. list_add(&lst->st_locks, collect);
  5423. }
  5424. static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
  5425. struct list_head *collect,
  5426. bool (*func)(struct nfs4_ol_stateid *))
  5427. {
  5428. struct nfs4_openowner *oop;
  5429. struct nfs4_ol_stateid *stp, *st_next;
  5430. struct nfs4_ol_stateid *lst, *lst_next;
  5431. u64 count = 0;
  5432. spin_lock(&clp->cl_lock);
  5433. list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
  5434. list_for_each_entry_safe(stp, st_next,
  5435. &oop->oo_owner.so_stateids, st_perstateowner) {
  5436. list_for_each_entry_safe(lst, lst_next,
  5437. &stp->st_locks, st_locks) {
  5438. if (func) {
  5439. if (func(lst))
  5440. nfsd_inject_add_lock_to_list(lst,
  5441. collect);
  5442. }
  5443. ++count;
  5444. /*
  5445. * Despite the fact that these functions deal
  5446. * with 64-bit integers for "count", we must
  5447. * ensure that it doesn't blow up the
  5448. * clp->cl_refcount. Throw a warning if we
  5449. * start to approach INT_MAX here.
  5450. */
  5451. WARN_ON_ONCE(count == (INT_MAX / 2));
  5452. if (count == max)
  5453. goto out;
  5454. }
  5455. }
  5456. }
  5457. out:
  5458. spin_unlock(&clp->cl_lock);
  5459. return count;
  5460. }
  5461. static u64
  5462. nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
  5463. u64 max)
  5464. {
  5465. return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
  5466. }
  5467. static u64
  5468. nfsd_print_client_locks(struct nfs4_client *clp)
  5469. {
  5470. u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
  5471. nfsd_print_count(clp, count, "locked files");
  5472. return count;
  5473. }
  5474. u64
  5475. nfsd_inject_print_locks(void)
  5476. {
  5477. struct nfs4_client *clp;
  5478. u64 count = 0;
  5479. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5480. nfsd_net_id);
  5481. if (!nfsd_netns_ready(nn))
  5482. return 0;
  5483. spin_lock(&nn->client_lock);
  5484. list_for_each_entry(clp, &nn->client_lru, cl_lru)
  5485. count += nfsd_print_client_locks(clp);
  5486. spin_unlock(&nn->client_lock);
  5487. return count;
  5488. }
  5489. static void
  5490. nfsd_reap_locks(struct list_head *reaplist)
  5491. {
  5492. struct nfs4_client *clp;
  5493. struct nfs4_ol_stateid *stp, *next;
  5494. list_for_each_entry_safe(stp, next, reaplist, st_locks) {
  5495. list_del_init(&stp->st_locks);
  5496. clp = stp->st_stid.sc_client;
  5497. nfs4_put_stid(&stp->st_stid);
  5498. put_client(clp);
  5499. }
  5500. }
  5501. u64
  5502. nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
  5503. {
  5504. unsigned int count = 0;
  5505. struct nfs4_client *clp;
  5506. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5507. nfsd_net_id);
  5508. LIST_HEAD(reaplist);
  5509. if (!nfsd_netns_ready(nn))
  5510. return count;
  5511. spin_lock(&nn->client_lock);
  5512. clp = nfsd_find_client(addr, addr_size);
  5513. if (clp)
  5514. count = nfsd_collect_client_locks(clp, &reaplist, 0);
  5515. spin_unlock(&nn->client_lock);
  5516. nfsd_reap_locks(&reaplist);
  5517. return count;
  5518. }
  5519. u64
  5520. nfsd_inject_forget_locks(u64 max)
  5521. {
  5522. u64 count = 0;
  5523. struct nfs4_client *clp;
  5524. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5525. nfsd_net_id);
  5526. LIST_HEAD(reaplist);
  5527. if (!nfsd_netns_ready(nn))
  5528. return count;
  5529. spin_lock(&nn->client_lock);
  5530. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  5531. count += nfsd_collect_client_locks(clp, &reaplist, max - count);
  5532. if (max != 0 && count >= max)
  5533. break;
  5534. }
  5535. spin_unlock(&nn->client_lock);
  5536. nfsd_reap_locks(&reaplist);
  5537. return count;
  5538. }
  5539. static u64
  5540. nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
  5541. struct list_head *collect,
  5542. void (*func)(struct nfs4_openowner *))
  5543. {
  5544. struct nfs4_openowner *oop, *next;
  5545. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5546. nfsd_net_id);
  5547. u64 count = 0;
  5548. lockdep_assert_held(&nn->client_lock);
  5549. spin_lock(&clp->cl_lock);
  5550. list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
  5551. if (func) {
  5552. func(oop);
  5553. if (collect) {
  5554. atomic_inc(&clp->cl_refcount);
  5555. list_add(&oop->oo_perclient, collect);
  5556. }
  5557. }
  5558. ++count;
  5559. /*
  5560. * Despite the fact that these functions deal with
  5561. * 64-bit integers for "count", we must ensure that
  5562. * it doesn't blow up the clp->cl_refcount. Throw a
  5563. * warning if we start to approach INT_MAX here.
  5564. */
  5565. WARN_ON_ONCE(count == (INT_MAX / 2));
  5566. if (count == max)
  5567. break;
  5568. }
  5569. spin_unlock(&clp->cl_lock);
  5570. return count;
  5571. }
  5572. static u64
  5573. nfsd_print_client_openowners(struct nfs4_client *clp)
  5574. {
  5575. u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
  5576. nfsd_print_count(clp, count, "openowners");
  5577. return count;
  5578. }
  5579. static u64
  5580. nfsd_collect_client_openowners(struct nfs4_client *clp,
  5581. struct list_head *collect, u64 max)
  5582. {
  5583. return nfsd_foreach_client_openowner(clp, max, collect,
  5584. unhash_openowner_locked);
  5585. }
  5586. u64
  5587. nfsd_inject_print_openowners(void)
  5588. {
  5589. struct nfs4_client *clp;
  5590. u64 count = 0;
  5591. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5592. nfsd_net_id);
  5593. if (!nfsd_netns_ready(nn))
  5594. return 0;
  5595. spin_lock(&nn->client_lock);
  5596. list_for_each_entry(clp, &nn->client_lru, cl_lru)
  5597. count += nfsd_print_client_openowners(clp);
  5598. spin_unlock(&nn->client_lock);
  5599. return count;
  5600. }
  5601. static void
  5602. nfsd_reap_openowners(struct list_head *reaplist)
  5603. {
  5604. struct nfs4_client *clp;
  5605. struct nfs4_openowner *oop, *next;
  5606. list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
  5607. list_del_init(&oop->oo_perclient);
  5608. clp = oop->oo_owner.so_client;
  5609. release_openowner(oop);
  5610. put_client(clp);
  5611. }
  5612. }
  5613. u64
  5614. nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
  5615. size_t addr_size)
  5616. {
  5617. unsigned int count = 0;
  5618. struct nfs4_client *clp;
  5619. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5620. nfsd_net_id);
  5621. LIST_HEAD(reaplist);
  5622. if (!nfsd_netns_ready(nn))
  5623. return count;
  5624. spin_lock(&nn->client_lock);
  5625. clp = nfsd_find_client(addr, addr_size);
  5626. if (clp)
  5627. count = nfsd_collect_client_openowners(clp, &reaplist, 0);
  5628. spin_unlock(&nn->client_lock);
  5629. nfsd_reap_openowners(&reaplist);
  5630. return count;
  5631. }
  5632. u64
  5633. nfsd_inject_forget_openowners(u64 max)
  5634. {
  5635. u64 count = 0;
  5636. struct nfs4_client *clp;
  5637. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5638. nfsd_net_id);
  5639. LIST_HEAD(reaplist);
  5640. if (!nfsd_netns_ready(nn))
  5641. return count;
  5642. spin_lock(&nn->client_lock);
  5643. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  5644. count += nfsd_collect_client_openowners(clp, &reaplist,
  5645. max - count);
  5646. if (max != 0 && count >= max)
  5647. break;
  5648. }
  5649. spin_unlock(&nn->client_lock);
  5650. nfsd_reap_openowners(&reaplist);
  5651. return count;
  5652. }
  5653. static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
  5654. struct list_head *victims)
  5655. {
  5656. struct nfs4_delegation *dp, *next;
  5657. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5658. nfsd_net_id);
  5659. u64 count = 0;
  5660. lockdep_assert_held(&nn->client_lock);
  5661. spin_lock(&state_lock);
  5662. list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
  5663. if (victims) {
  5664. /*
  5665. * It's not safe to mess with delegations that have a
  5666. * non-zero dl_time. They might have already been broken
  5667. * and could be processed by the laundromat outside of
  5668. * the state_lock. Just leave them be.
  5669. */
  5670. if (dp->dl_time != 0)
  5671. continue;
  5672. atomic_inc(&clp->cl_refcount);
  5673. WARN_ON(!unhash_delegation_locked(dp));
  5674. list_add(&dp->dl_recall_lru, victims);
  5675. }
  5676. ++count;
  5677. /*
  5678. * Despite the fact that these functions deal with
  5679. * 64-bit integers for "count", we must ensure that
  5680. * it doesn't blow up the clp->cl_refcount. Throw a
  5681. * warning if we start to approach INT_MAX here.
  5682. */
  5683. WARN_ON_ONCE(count == (INT_MAX / 2));
  5684. if (count == max)
  5685. break;
  5686. }
  5687. spin_unlock(&state_lock);
  5688. return count;
  5689. }
  5690. static u64
  5691. nfsd_print_client_delegations(struct nfs4_client *clp)
  5692. {
  5693. u64 count = nfsd_find_all_delegations(clp, 0, NULL);
  5694. nfsd_print_count(clp, count, "delegations");
  5695. return count;
  5696. }
  5697. u64
  5698. nfsd_inject_print_delegations(void)
  5699. {
  5700. struct nfs4_client *clp;
  5701. u64 count = 0;
  5702. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5703. nfsd_net_id);
  5704. if (!nfsd_netns_ready(nn))
  5705. return 0;
  5706. spin_lock(&nn->client_lock);
  5707. list_for_each_entry(clp, &nn->client_lru, cl_lru)
  5708. count += nfsd_print_client_delegations(clp);
  5709. spin_unlock(&nn->client_lock);
  5710. return count;
  5711. }
  5712. static void
  5713. nfsd_forget_delegations(struct list_head *reaplist)
  5714. {
  5715. struct nfs4_client *clp;
  5716. struct nfs4_delegation *dp, *next;
  5717. list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
  5718. list_del_init(&dp->dl_recall_lru);
  5719. clp = dp->dl_stid.sc_client;
  5720. revoke_delegation(dp);
  5721. put_client(clp);
  5722. }
  5723. }
  5724. u64
  5725. nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
  5726. size_t addr_size)
  5727. {
  5728. u64 count = 0;
  5729. struct nfs4_client *clp;
  5730. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5731. nfsd_net_id);
  5732. LIST_HEAD(reaplist);
  5733. if (!nfsd_netns_ready(nn))
  5734. return count;
  5735. spin_lock(&nn->client_lock);
  5736. clp = nfsd_find_client(addr, addr_size);
  5737. if (clp)
  5738. count = nfsd_find_all_delegations(clp, 0, &reaplist);
  5739. spin_unlock(&nn->client_lock);
  5740. nfsd_forget_delegations(&reaplist);
  5741. return count;
  5742. }
  5743. u64
  5744. nfsd_inject_forget_delegations(u64 max)
  5745. {
  5746. u64 count = 0;
  5747. struct nfs4_client *clp;
  5748. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5749. nfsd_net_id);
  5750. LIST_HEAD(reaplist);
  5751. if (!nfsd_netns_ready(nn))
  5752. return count;
  5753. spin_lock(&nn->client_lock);
  5754. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  5755. count += nfsd_find_all_delegations(clp, max - count, &reaplist);
  5756. if (max != 0 && count >= max)
  5757. break;
  5758. }
  5759. spin_unlock(&nn->client_lock);
  5760. nfsd_forget_delegations(&reaplist);
  5761. return count;
  5762. }
  5763. static void
  5764. nfsd_recall_delegations(struct list_head *reaplist)
  5765. {
  5766. struct nfs4_client *clp;
  5767. struct nfs4_delegation *dp, *next;
  5768. list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
  5769. list_del_init(&dp->dl_recall_lru);
  5770. clp = dp->dl_stid.sc_client;
  5771. /*
  5772. * We skipped all entries that had a zero dl_time before,
  5773. * so we can now reset the dl_time back to 0. If a delegation
  5774. * break comes in now, then it won't make any difference since
  5775. * we're recalling it either way.
  5776. */
  5777. spin_lock(&state_lock);
  5778. dp->dl_time = 0;
  5779. spin_unlock(&state_lock);
  5780. nfsd_break_one_deleg(dp);
  5781. put_client(clp);
  5782. }
  5783. }
  5784. u64
  5785. nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
  5786. size_t addr_size)
  5787. {
  5788. u64 count = 0;
  5789. struct nfs4_client *clp;
  5790. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5791. nfsd_net_id);
  5792. LIST_HEAD(reaplist);
  5793. if (!nfsd_netns_ready(nn))
  5794. return count;
  5795. spin_lock(&nn->client_lock);
  5796. clp = nfsd_find_client(addr, addr_size);
  5797. if (clp)
  5798. count = nfsd_find_all_delegations(clp, 0, &reaplist);
  5799. spin_unlock(&nn->client_lock);
  5800. nfsd_recall_delegations(&reaplist);
  5801. return count;
  5802. }
  5803. u64
  5804. nfsd_inject_recall_delegations(u64 max)
  5805. {
  5806. u64 count = 0;
  5807. struct nfs4_client *clp, *next;
  5808. struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
  5809. nfsd_net_id);
  5810. LIST_HEAD(reaplist);
  5811. if (!nfsd_netns_ready(nn))
  5812. return count;
  5813. spin_lock(&nn->client_lock);
  5814. list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
  5815. count += nfsd_find_all_delegations(clp, max - count, &reaplist);
  5816. if (max != 0 && ++count >= max)
  5817. break;
  5818. }
  5819. spin_unlock(&nn->client_lock);
  5820. nfsd_recall_delegations(&reaplist);
  5821. return count;
  5822. }
  5823. #endif /* CONFIG_NFSD_FAULT_INJECTION */
  5824. /*
  5825. * Since the lifetime of a delegation isn't limited to that of an open, a
  5826. * client may quite reasonably hang on to a delegation as long as it has
  5827. * the inode cached. This becomes an obvious problem the first time a
  5828. * client's inode cache approaches the size of the server's total memory.
  5829. *
  5830. * For now we avoid this problem by imposing a hard limit on the number
  5831. * of delegations, which varies according to the server's memory size.
  5832. */
  5833. static void
  5834. set_max_delegations(void)
  5835. {
  5836. /*
  5837. * Allow at most 4 delegations per megabyte of RAM. Quick
  5838. * estimates suggest that in the worst case (where every delegation
  5839. * is for a different inode), a delegation could take about 1.5K,
  5840. * giving a worst case usage of about 6% of memory.
  5841. */
  5842. max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
  5843. }
  5844. static int nfs4_state_create_net(struct net *net)
  5845. {
  5846. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  5847. int i;
  5848. nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  5849. CLIENT_HASH_SIZE, GFP_KERNEL);
  5850. if (!nn->conf_id_hashtbl)
  5851. goto err;
  5852. nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
  5853. CLIENT_HASH_SIZE, GFP_KERNEL);
  5854. if (!nn->unconf_id_hashtbl)
  5855. goto err_unconf_id;
  5856. nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
  5857. SESSION_HASH_SIZE, GFP_KERNEL);
  5858. if (!nn->sessionid_hashtbl)
  5859. goto err_sessionid;
  5860. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  5861. INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
  5862. INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
  5863. }
  5864. for (i = 0; i < SESSION_HASH_SIZE; i++)
  5865. INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
  5866. nn->conf_name_tree = RB_ROOT;
  5867. nn->unconf_name_tree = RB_ROOT;
  5868. INIT_LIST_HEAD(&nn->client_lru);
  5869. INIT_LIST_HEAD(&nn->close_lru);
  5870. INIT_LIST_HEAD(&nn->del_recall_lru);
  5871. spin_lock_init(&nn->client_lock);
  5872. INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
  5873. get_net(net);
  5874. return 0;
  5875. err_sessionid:
  5876. kfree(nn->unconf_id_hashtbl);
  5877. err_unconf_id:
  5878. kfree(nn->conf_id_hashtbl);
  5879. err:
  5880. return -ENOMEM;
  5881. }
  5882. static void
  5883. nfs4_state_destroy_net(struct net *net)
  5884. {
  5885. int i;
  5886. struct nfs4_client *clp = NULL;
  5887. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  5888. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  5889. while (!list_empty(&nn->conf_id_hashtbl[i])) {
  5890. clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  5891. destroy_client(clp);
  5892. }
  5893. }
  5894. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  5895. while (!list_empty(&nn->unconf_id_hashtbl[i])) {
  5896. clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  5897. destroy_client(clp);
  5898. }
  5899. }
  5900. kfree(nn->sessionid_hashtbl);
  5901. kfree(nn->unconf_id_hashtbl);
  5902. kfree(nn->conf_id_hashtbl);
  5903. put_net(net);
  5904. }
  5905. int
  5906. nfs4_state_start_net(struct net *net)
  5907. {
  5908. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  5909. int ret;
  5910. ret = nfs4_state_create_net(net);
  5911. if (ret)
  5912. return ret;
  5913. nn->boot_time = get_seconds();
  5914. nn->grace_ended = false;
  5915. nn->nfsd4_manager.block_opens = true;
  5916. locks_start_grace(net, &nn->nfsd4_manager);
  5917. nfsd4_client_tracking_init(net);
  5918. printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
  5919. nn->nfsd4_grace, net);
  5920. queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
  5921. return 0;
  5922. }
  5923. /* initialization to perform when the nfsd service is started: */
  5924. int
  5925. nfs4_state_start(void)
  5926. {
  5927. int ret;
  5928. ret = set_callback_cred();
  5929. if (ret)
  5930. return -ENOMEM;
  5931. laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
  5932. if (laundry_wq == NULL) {
  5933. ret = -ENOMEM;
  5934. goto out_recovery;
  5935. }
  5936. ret = nfsd4_create_callback_queue();
  5937. if (ret)
  5938. goto out_free_laundry;
  5939. set_max_delegations();
  5940. return 0;
  5941. out_free_laundry:
  5942. destroy_workqueue(laundry_wq);
  5943. out_recovery:
  5944. return ret;
  5945. }
  5946. void
  5947. nfs4_state_shutdown_net(struct net *net)
  5948. {
  5949. struct nfs4_delegation *dp = NULL;
  5950. struct list_head *pos, *next, reaplist;
  5951. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  5952. cancel_delayed_work_sync(&nn->laundromat_work);
  5953. locks_end_grace(&nn->nfsd4_manager);
  5954. INIT_LIST_HEAD(&reaplist);
  5955. spin_lock(&state_lock);
  5956. list_for_each_safe(pos, next, &nn->del_recall_lru) {
  5957. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  5958. WARN_ON(!unhash_delegation_locked(dp));
  5959. list_add(&dp->dl_recall_lru, &reaplist);
  5960. }
  5961. spin_unlock(&state_lock);
  5962. list_for_each_safe(pos, next, &reaplist) {
  5963. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  5964. list_del_init(&dp->dl_recall_lru);
  5965. put_clnt_odstate(dp->dl_clnt_odstate);
  5966. nfs4_put_deleg_lease(dp->dl_stid.sc_file);
  5967. nfs4_put_stid(&dp->dl_stid);
  5968. }
  5969. nfsd4_client_tracking_exit(net);
  5970. nfs4_state_destroy_net(net);
  5971. }
  5972. void
  5973. nfs4_state_shutdown(void)
  5974. {
  5975. destroy_workqueue(laundry_wq);
  5976. nfsd4_destroy_callback_queue();
  5977. }
  5978. static void
  5979. get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  5980. {
  5981. if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
  5982. memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
  5983. }
  5984. static void
  5985. put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  5986. {
  5987. if (cstate->minorversion) {
  5988. memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
  5989. SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  5990. }
  5991. }
  5992. void
  5993. clear_current_stateid(struct nfsd4_compound_state *cstate)
  5994. {
  5995. CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
  5996. }
  5997. /*
  5998. * functions to set current state id
  5999. */
  6000. void
  6001. nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  6002. {
  6003. put_stateid(cstate, &odp->od_stateid);
  6004. }
  6005. void
  6006. nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
  6007. {
  6008. put_stateid(cstate, &open->op_stateid);
  6009. }
  6010. void
  6011. nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  6012. {
  6013. put_stateid(cstate, &close->cl_stateid);
  6014. }
  6015. void
  6016. nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
  6017. {
  6018. put_stateid(cstate, &lock->lk_resp_stateid);
  6019. }
  6020. /*
  6021. * functions to consume current state id
  6022. */
  6023. void
  6024. nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
  6025. {
  6026. get_stateid(cstate, &odp->od_stateid);
  6027. }
  6028. void
  6029. nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
  6030. {
  6031. get_stateid(cstate, &drp->dr_stateid);
  6032. }
  6033. void
  6034. nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
  6035. {
  6036. get_stateid(cstate, &fsp->fr_stateid);
  6037. }
  6038. void
  6039. nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
  6040. {
  6041. get_stateid(cstate, &setattr->sa_stateid);
  6042. }
  6043. void
  6044. nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
  6045. {
  6046. get_stateid(cstate, &close->cl_stateid);
  6047. }
  6048. void
  6049. nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
  6050. {
  6051. get_stateid(cstate, &locku->lu_stateid);
  6052. }
  6053. void
  6054. nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
  6055. {
  6056. get_stateid(cstate, &read->rd_stateid);
  6057. }
  6058. void
  6059. nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
  6060. {
  6061. get_stateid(cstate, &write->wr_stateid);
  6062. }