nfs4state.c 235 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023
  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 <linux/string_helpers.h>
  45. #include <linux/fsnotify.h>
  46. #include <linux/rhashtable.h>
  47. #include <linux/nfs_ssc.h>
  48. #include "xdr4.h"
  49. #include "xdr4cb.h"
  50. #include "vfs.h"
  51. #include "current_stateid.h"
  52. #include "netns.h"
  53. #include "pnfs.h"
  54. #include "filecache.h"
  55. #include "trace.h"
  56. #define NFSDDBG_FACILITY NFSDDBG_PROC
  57. #define all_ones {{ ~0, ~0}, ~0}
  58. static const stateid_t one_stateid = {
  59. .si_generation = ~0,
  60. .si_opaque = all_ones,
  61. };
  62. static const stateid_t zero_stateid = {
  63. /* all fields zero */
  64. };
  65. static const stateid_t currentstateid = {
  66. .si_generation = 1,
  67. };
  68. static const stateid_t close_stateid = {
  69. .si_generation = 0xffffffffU,
  70. };
  71. static u64 current_sessionid = 1;
  72. #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
  73. #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
  74. #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
  75. #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
  76. /* forward declarations */
  77. static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
  78. static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
  79. void nfsd4_end_grace(struct nfsd_net *nn);
  80. static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
  81. static void nfsd4_file_hash_remove(struct nfs4_file *fi);
  82. static void deleg_reaper(struct nfsd_net *nn);
  83. /* Locking: */
  84. /*
  85. * Currently used for the del_recall_lru and file hash table. In an
  86. * effort to decrease the scope of the client_mutex, this spinlock may
  87. * eventually cover more:
  88. */
  89. static DEFINE_SPINLOCK(state_lock);
  90. enum nfsd4_st_mutex_lock_subclass {
  91. OPEN_STATEID_MUTEX = 0,
  92. LOCK_STATEID_MUTEX = 1,
  93. };
  94. /*
  95. * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
  96. * the refcount on the open stateid to drop.
  97. */
  98. static DECLARE_WAIT_QUEUE_HEAD(close_wq);
  99. /*
  100. * A waitqueue where a writer to clients/#/ctl destroying a client can
  101. * wait for cl_rpc_users to drop to 0 and then for the client to be
  102. * unhashed.
  103. */
  104. static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
  105. static struct kmem_cache *client_slab;
  106. static struct kmem_cache *openowner_slab;
  107. static struct kmem_cache *lockowner_slab;
  108. static struct kmem_cache *file_slab;
  109. static struct kmem_cache *stateid_slab;
  110. static struct kmem_cache *deleg_slab;
  111. static struct kmem_cache *odstate_slab;
  112. static void free_session(struct nfsd4_session *);
  113. static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
  114. static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
  115. static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops;
  116. static struct workqueue_struct *laundry_wq;
  117. int nfsd4_create_laundry_wq(void)
  118. {
  119. int rc = 0;
  120. laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
  121. if (laundry_wq == NULL)
  122. rc = -ENOMEM;
  123. return rc;
  124. }
  125. void nfsd4_destroy_laundry_wq(void)
  126. {
  127. destroy_workqueue(laundry_wq);
  128. }
  129. static bool is_session_dead(struct nfsd4_session *ses)
  130. {
  131. return ses->se_flags & NFS4_SESSION_DEAD;
  132. }
  133. static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
  134. {
  135. if (atomic_read(&ses->se_ref) > ref_held_by_me)
  136. return nfserr_jukebox;
  137. ses->se_flags |= NFS4_SESSION_DEAD;
  138. return nfs_ok;
  139. }
  140. static bool is_client_expired(struct nfs4_client *clp)
  141. {
  142. return clp->cl_time == 0;
  143. }
  144. static void nfsd4_dec_courtesy_client_count(struct nfsd_net *nn,
  145. struct nfs4_client *clp)
  146. {
  147. if (clp->cl_state != NFSD4_ACTIVE)
  148. atomic_add_unless(&nn->nfsd_courtesy_clients, -1, 0);
  149. }
  150. static __be32 get_client_locked(struct nfs4_client *clp)
  151. {
  152. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  153. lockdep_assert_held(&nn->client_lock);
  154. if (is_client_expired(clp))
  155. return nfserr_expired;
  156. atomic_inc(&clp->cl_rpc_users);
  157. nfsd4_dec_courtesy_client_count(nn, clp);
  158. clp->cl_state = NFSD4_ACTIVE;
  159. return nfs_ok;
  160. }
  161. /* must be called under the client_lock */
  162. static inline void
  163. renew_client_locked(struct nfs4_client *clp)
  164. {
  165. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  166. if (is_client_expired(clp)) {
  167. WARN_ON(1);
  168. printk("%s: client (clientid %08x/%08x) already expired\n",
  169. __func__,
  170. clp->cl_clientid.cl_boot,
  171. clp->cl_clientid.cl_id);
  172. return;
  173. }
  174. list_move_tail(&clp->cl_lru, &nn->client_lru);
  175. clp->cl_time = ktime_get_boottime_seconds();
  176. nfsd4_dec_courtesy_client_count(nn, clp);
  177. clp->cl_state = NFSD4_ACTIVE;
  178. }
  179. static void put_client_renew_locked(struct nfs4_client *clp)
  180. {
  181. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  182. lockdep_assert_held(&nn->client_lock);
  183. if (!atomic_dec_and_test(&clp->cl_rpc_users))
  184. return;
  185. if (!is_client_expired(clp))
  186. renew_client_locked(clp);
  187. else
  188. wake_up_all(&expiry_wq);
  189. }
  190. static void put_client_renew(struct nfs4_client *clp)
  191. {
  192. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  193. if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
  194. return;
  195. if (!is_client_expired(clp))
  196. renew_client_locked(clp);
  197. else
  198. wake_up_all(&expiry_wq);
  199. spin_unlock(&nn->client_lock);
  200. }
  201. static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
  202. {
  203. __be32 status;
  204. if (is_session_dead(ses))
  205. return nfserr_badsession;
  206. status = get_client_locked(ses->se_client);
  207. if (status)
  208. return status;
  209. atomic_inc(&ses->se_ref);
  210. return nfs_ok;
  211. }
  212. static void nfsd4_put_session_locked(struct nfsd4_session *ses)
  213. {
  214. struct nfs4_client *clp = ses->se_client;
  215. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  216. lockdep_assert_held(&nn->client_lock);
  217. if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
  218. free_session(ses);
  219. put_client_renew_locked(clp);
  220. }
  221. static void nfsd4_put_session(struct nfsd4_session *ses)
  222. {
  223. struct nfs4_client *clp = ses->se_client;
  224. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  225. spin_lock(&nn->client_lock);
  226. nfsd4_put_session_locked(ses);
  227. spin_unlock(&nn->client_lock);
  228. }
  229. static struct nfsd4_blocked_lock *
  230. find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
  231. struct nfsd_net *nn)
  232. {
  233. struct nfsd4_blocked_lock *cur, *found = NULL;
  234. spin_lock(&nn->blocked_locks_lock);
  235. list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
  236. if (fh_match(fh, &cur->nbl_fh)) {
  237. list_del_init(&cur->nbl_list);
  238. WARN_ON(list_empty(&cur->nbl_lru));
  239. list_del_init(&cur->nbl_lru);
  240. found = cur;
  241. break;
  242. }
  243. }
  244. spin_unlock(&nn->blocked_locks_lock);
  245. if (found)
  246. locks_delete_block(&found->nbl_lock);
  247. return found;
  248. }
  249. static struct nfsd4_blocked_lock *
  250. find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
  251. struct nfsd_net *nn)
  252. {
  253. struct nfsd4_blocked_lock *nbl;
  254. nbl = find_blocked_lock(lo, fh, nn);
  255. if (!nbl) {
  256. nbl = kmalloc(sizeof(*nbl), GFP_KERNEL);
  257. if (nbl) {
  258. INIT_LIST_HEAD(&nbl->nbl_list);
  259. INIT_LIST_HEAD(&nbl->nbl_lru);
  260. fh_copy_shallow(&nbl->nbl_fh, fh);
  261. locks_init_lock(&nbl->nbl_lock);
  262. kref_init(&nbl->nbl_kref);
  263. nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
  264. &nfsd4_cb_notify_lock_ops,
  265. NFSPROC4_CLNT_CB_NOTIFY_LOCK);
  266. }
  267. }
  268. return nbl;
  269. }
  270. static void
  271. free_nbl(struct kref *kref)
  272. {
  273. struct nfsd4_blocked_lock *nbl;
  274. nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref);
  275. locks_release_private(&nbl->nbl_lock);
  276. kfree(nbl);
  277. }
  278. static void
  279. free_blocked_lock(struct nfsd4_blocked_lock *nbl)
  280. {
  281. locks_delete_block(&nbl->nbl_lock);
  282. kref_put(&nbl->nbl_kref, free_nbl);
  283. }
  284. static void
  285. remove_blocked_locks(struct nfs4_lockowner *lo)
  286. {
  287. struct nfs4_client *clp = lo->lo_owner.so_client;
  288. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  289. struct nfsd4_blocked_lock *nbl;
  290. LIST_HEAD(reaplist);
  291. /* Dequeue all blocked locks */
  292. spin_lock(&nn->blocked_locks_lock);
  293. while (!list_empty(&lo->lo_blocked)) {
  294. nbl = list_first_entry(&lo->lo_blocked,
  295. struct nfsd4_blocked_lock,
  296. nbl_list);
  297. list_del_init(&nbl->nbl_list);
  298. WARN_ON(list_empty(&nbl->nbl_lru));
  299. list_move(&nbl->nbl_lru, &reaplist);
  300. }
  301. spin_unlock(&nn->blocked_locks_lock);
  302. /* Now free them */
  303. while (!list_empty(&reaplist)) {
  304. nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
  305. nbl_lru);
  306. list_del_init(&nbl->nbl_lru);
  307. free_blocked_lock(nbl);
  308. }
  309. }
  310. static void
  311. nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
  312. {
  313. struct nfsd4_blocked_lock *nbl = container_of(cb,
  314. struct nfsd4_blocked_lock, nbl_cb);
  315. locks_delete_block(&nbl->nbl_lock);
  316. }
  317. static int
  318. nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
  319. {
  320. trace_nfsd_cb_notify_lock_done(&zero_stateid, task);
  321. /*
  322. * Since this is just an optimization, we don't try very hard if it
  323. * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
  324. * just quit trying on anything else.
  325. */
  326. switch (task->tk_status) {
  327. case -NFS4ERR_DELAY:
  328. rpc_delay(task, 1 * HZ);
  329. return 0;
  330. default:
  331. return 1;
  332. }
  333. }
  334. static void
  335. nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
  336. {
  337. struct nfsd4_blocked_lock *nbl = container_of(cb,
  338. struct nfsd4_blocked_lock, nbl_cb);
  339. free_blocked_lock(nbl);
  340. }
  341. static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
  342. .prepare = nfsd4_cb_notify_lock_prepare,
  343. .done = nfsd4_cb_notify_lock_done,
  344. .release = nfsd4_cb_notify_lock_release,
  345. .opcode = OP_CB_NOTIFY_LOCK,
  346. };
  347. /*
  348. * We store the NONE, READ, WRITE, and BOTH bits separately in the
  349. * st_{access,deny}_bmap field of the stateid, in order to track not
  350. * only what share bits are currently in force, but also what
  351. * combinations of share bits previous opens have used. This allows us
  352. * to enforce the recommendation in
  353. * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that
  354. * the server return an error if the client attempt to downgrade to a
  355. * combination of share bits not explicable by closing some of its
  356. * previous opens.
  357. *
  358. * This enforcement is arguably incomplete, since we don't keep
  359. * track of access/deny bit combinations; so, e.g., we allow:
  360. *
  361. * OPEN allow read, deny write
  362. * OPEN allow both, deny none
  363. * DOWNGRADE allow read, deny none
  364. *
  365. * which we should reject.
  366. *
  367. * But you could also argue that our current code is already overkill,
  368. * since it only exists to return NFS4ERR_INVAL on incorrect client
  369. * behavior.
  370. */
  371. static unsigned int
  372. bmap_to_share_mode(unsigned long bmap)
  373. {
  374. int i;
  375. unsigned int access = 0;
  376. for (i = 1; i < 4; i++) {
  377. if (test_bit(i, &bmap))
  378. access |= i;
  379. }
  380. return access;
  381. }
  382. /* set share access for a given stateid */
  383. static inline void
  384. set_access(u32 access, struct nfs4_ol_stateid *stp)
  385. {
  386. unsigned char mask = 1 << access;
  387. WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
  388. stp->st_access_bmap |= mask;
  389. }
  390. /* clear share access for a given stateid */
  391. static inline void
  392. clear_access(u32 access, struct nfs4_ol_stateid *stp)
  393. {
  394. unsigned char mask = 1 << access;
  395. WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
  396. stp->st_access_bmap &= ~mask;
  397. }
  398. /* test whether a given stateid has access */
  399. static inline bool
  400. test_access(u32 access, struct nfs4_ol_stateid *stp)
  401. {
  402. unsigned char mask = 1 << access;
  403. return (bool)(stp->st_access_bmap & mask);
  404. }
  405. /* set share deny for a given stateid */
  406. static inline void
  407. set_deny(u32 deny, struct nfs4_ol_stateid *stp)
  408. {
  409. unsigned char mask = 1 << deny;
  410. WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
  411. stp->st_deny_bmap |= mask;
  412. }
  413. /* clear share deny for a given stateid */
  414. static inline void
  415. clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
  416. {
  417. unsigned char mask = 1 << deny;
  418. WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
  419. stp->st_deny_bmap &= ~mask;
  420. }
  421. /* test whether a given stateid is denying specific access */
  422. static inline bool
  423. test_deny(u32 deny, struct nfs4_ol_stateid *stp)
  424. {
  425. unsigned char mask = 1 << deny;
  426. return (bool)(stp->st_deny_bmap & mask);
  427. }
  428. static int nfs4_access_to_omode(u32 access)
  429. {
  430. switch (access & NFS4_SHARE_ACCESS_BOTH) {
  431. case NFS4_SHARE_ACCESS_READ:
  432. return O_RDONLY;
  433. case NFS4_SHARE_ACCESS_WRITE:
  434. return O_WRONLY;
  435. case NFS4_SHARE_ACCESS_BOTH:
  436. return O_RDWR;
  437. }
  438. WARN_ON_ONCE(1);
  439. return O_RDONLY;
  440. }
  441. static inline int
  442. access_permit_read(struct nfs4_ol_stateid *stp)
  443. {
  444. return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
  445. test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
  446. test_access(NFS4_SHARE_ACCESS_WRITE, stp);
  447. }
  448. static inline int
  449. access_permit_write(struct nfs4_ol_stateid *stp)
  450. {
  451. return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
  452. test_access(NFS4_SHARE_ACCESS_BOTH, stp);
  453. }
  454. static inline struct nfs4_stateowner *
  455. nfs4_get_stateowner(struct nfs4_stateowner *sop)
  456. {
  457. atomic_inc(&sop->so_count);
  458. return sop;
  459. }
  460. static int
  461. same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
  462. {
  463. return (sop->so_owner.len == owner->len) &&
  464. 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
  465. }
  466. static struct nfs4_openowner *
  467. find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
  468. struct nfs4_client *clp)
  469. {
  470. struct nfs4_stateowner *so;
  471. lockdep_assert_held(&clp->cl_lock);
  472. list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
  473. so_strhash) {
  474. if (!so->so_is_open_owner)
  475. continue;
  476. if (same_owner_str(so, &open->op_owner))
  477. return openowner(nfs4_get_stateowner(so));
  478. }
  479. return NULL;
  480. }
  481. static inline u32
  482. opaque_hashval(const void *ptr, int nbytes)
  483. {
  484. unsigned char *cptr = (unsigned char *) ptr;
  485. u32 x = 0;
  486. while (nbytes--) {
  487. x *= 37;
  488. x += *cptr++;
  489. }
  490. return x;
  491. }
  492. static void nfsd4_free_file_rcu(struct rcu_head *rcu)
  493. {
  494. struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
  495. kmem_cache_free(file_slab, fp);
  496. }
  497. void
  498. put_nfs4_file(struct nfs4_file *fi)
  499. {
  500. if (refcount_dec_and_test(&fi->fi_ref)) {
  501. nfsd4_file_hash_remove(fi);
  502. WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
  503. WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
  504. call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
  505. }
  506. }
  507. static struct nfsd_file *
  508. find_writeable_file_locked(struct nfs4_file *f)
  509. {
  510. struct nfsd_file *ret;
  511. lockdep_assert_held(&f->fi_lock);
  512. ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
  513. if (!ret)
  514. ret = nfsd_file_get(f->fi_fds[O_RDWR]);
  515. return ret;
  516. }
  517. static struct nfsd_file *
  518. find_writeable_file(struct nfs4_file *f)
  519. {
  520. struct nfsd_file *ret;
  521. spin_lock(&f->fi_lock);
  522. ret = find_writeable_file_locked(f);
  523. spin_unlock(&f->fi_lock);
  524. return ret;
  525. }
  526. static struct nfsd_file *
  527. find_readable_file_locked(struct nfs4_file *f)
  528. {
  529. struct nfsd_file *ret;
  530. lockdep_assert_held(&f->fi_lock);
  531. ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
  532. if (!ret)
  533. ret = nfsd_file_get(f->fi_fds[O_RDWR]);
  534. return ret;
  535. }
  536. static struct nfsd_file *
  537. find_readable_file(struct nfs4_file *f)
  538. {
  539. struct nfsd_file *ret;
  540. spin_lock(&f->fi_lock);
  541. ret = find_readable_file_locked(f);
  542. spin_unlock(&f->fi_lock);
  543. return ret;
  544. }
  545. static struct nfsd_file *
  546. find_rw_file(struct nfs4_file *f)
  547. {
  548. struct nfsd_file *ret;
  549. spin_lock(&f->fi_lock);
  550. ret = nfsd_file_get(f->fi_fds[O_RDWR]);
  551. spin_unlock(&f->fi_lock);
  552. return ret;
  553. }
  554. struct nfsd_file *
  555. find_any_file(struct nfs4_file *f)
  556. {
  557. struct nfsd_file *ret;
  558. if (!f)
  559. return NULL;
  560. spin_lock(&f->fi_lock);
  561. ret = nfsd_file_get(f->fi_fds[O_RDWR]);
  562. if (!ret) {
  563. ret = nfsd_file_get(f->fi_fds[O_WRONLY]);
  564. if (!ret)
  565. ret = nfsd_file_get(f->fi_fds[O_RDONLY]);
  566. }
  567. spin_unlock(&f->fi_lock);
  568. return ret;
  569. }
  570. static struct nfsd_file *find_any_file_locked(struct nfs4_file *f)
  571. {
  572. lockdep_assert_held(&f->fi_lock);
  573. if (f->fi_fds[O_RDWR])
  574. return f->fi_fds[O_RDWR];
  575. if (f->fi_fds[O_WRONLY])
  576. return f->fi_fds[O_WRONLY];
  577. if (f->fi_fds[O_RDONLY])
  578. return f->fi_fds[O_RDONLY];
  579. return NULL;
  580. }
  581. static atomic_long_t num_delegations;
  582. unsigned long max_delegations;
  583. /*
  584. * Open owner state (share locks)
  585. */
  586. /* hash tables for lock and open owners */
  587. #define OWNER_HASH_BITS 8
  588. #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
  589. #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
  590. static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
  591. {
  592. unsigned int ret;
  593. ret = opaque_hashval(ownername->data, ownername->len);
  594. return ret & OWNER_HASH_MASK;
  595. }
  596. static struct rhltable nfs4_file_rhltable ____cacheline_aligned_in_smp;
  597. static const struct rhashtable_params nfs4_file_rhash_params = {
  598. .key_len = sizeof_field(struct nfs4_file, fi_inode),
  599. .key_offset = offsetof(struct nfs4_file, fi_inode),
  600. .head_offset = offsetof(struct nfs4_file, fi_rlist),
  601. /*
  602. * Start with a single page hash table to reduce resizing churn
  603. * on light workloads.
  604. */
  605. .min_size = 256,
  606. .automatic_shrinking = true,
  607. };
  608. /*
  609. * Check if courtesy clients have conflicting access and resolve it if possible
  610. *
  611. * access: is op_share_access if share_access is true.
  612. * Check if access mode, op_share_access, would conflict with
  613. * the current deny mode of the file 'fp'.
  614. * access: is op_share_deny if share_access is false.
  615. * Check if the deny mode, op_share_deny, would conflict with
  616. * current access of the file 'fp'.
  617. * stp: skip checking this entry.
  618. * new_stp: normal open, not open upgrade.
  619. *
  620. * Function returns:
  621. * false - access/deny mode conflict with normal client.
  622. * true - no conflict or conflict with courtesy client(s) is resolved.
  623. */
  624. static bool
  625. nfs4_resolve_deny_conflicts_locked(struct nfs4_file *fp, bool new_stp,
  626. struct nfs4_ol_stateid *stp, u32 access, bool share_access)
  627. {
  628. struct nfs4_ol_stateid *st;
  629. bool resolvable = true;
  630. unsigned char bmap;
  631. struct nfsd_net *nn;
  632. struct nfs4_client *clp;
  633. lockdep_assert_held(&fp->fi_lock);
  634. list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
  635. /* ignore lock stateid */
  636. if (st->st_openstp)
  637. continue;
  638. if (st == stp && new_stp)
  639. continue;
  640. /* check file access against deny mode or vice versa */
  641. bmap = share_access ? st->st_deny_bmap : st->st_access_bmap;
  642. if (!(access & bmap_to_share_mode(bmap)))
  643. continue;
  644. clp = st->st_stid.sc_client;
  645. if (try_to_expire_client(clp))
  646. continue;
  647. resolvable = false;
  648. break;
  649. }
  650. if (resolvable) {
  651. clp = stp->st_stid.sc_client;
  652. nn = net_generic(clp->net, nfsd_net_id);
  653. mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
  654. }
  655. return resolvable;
  656. }
  657. static void
  658. __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
  659. {
  660. lockdep_assert_held(&fp->fi_lock);
  661. if (access & NFS4_SHARE_ACCESS_WRITE)
  662. atomic_inc(&fp->fi_access[O_WRONLY]);
  663. if (access & NFS4_SHARE_ACCESS_READ)
  664. atomic_inc(&fp->fi_access[O_RDONLY]);
  665. }
  666. static __be32
  667. nfs4_file_get_access(struct nfs4_file *fp, u32 access)
  668. {
  669. lockdep_assert_held(&fp->fi_lock);
  670. /* Does this access mode make sense? */
  671. if (access & ~NFS4_SHARE_ACCESS_BOTH)
  672. return nfserr_inval;
  673. /* Does it conflict with a deny mode already set? */
  674. if ((access & fp->fi_share_deny) != 0)
  675. return nfserr_share_denied;
  676. __nfs4_file_get_access(fp, access);
  677. return nfs_ok;
  678. }
  679. static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
  680. {
  681. /* Common case is that there is no deny mode. */
  682. if (deny) {
  683. /* Does this deny mode make sense? */
  684. if (deny & ~NFS4_SHARE_DENY_BOTH)
  685. return nfserr_inval;
  686. if ((deny & NFS4_SHARE_DENY_READ) &&
  687. atomic_read(&fp->fi_access[O_RDONLY]))
  688. return nfserr_share_denied;
  689. if ((deny & NFS4_SHARE_DENY_WRITE) &&
  690. atomic_read(&fp->fi_access[O_WRONLY]))
  691. return nfserr_share_denied;
  692. }
  693. return nfs_ok;
  694. }
  695. static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
  696. {
  697. might_lock(&fp->fi_lock);
  698. if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
  699. struct nfsd_file *f1 = NULL;
  700. struct nfsd_file *f2 = NULL;
  701. swap(f1, fp->fi_fds[oflag]);
  702. if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
  703. swap(f2, fp->fi_fds[O_RDWR]);
  704. spin_unlock(&fp->fi_lock);
  705. if (f1)
  706. nfsd_file_put(f1);
  707. if (f2)
  708. nfsd_file_put(f2);
  709. }
  710. }
  711. static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
  712. {
  713. WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
  714. if (access & NFS4_SHARE_ACCESS_WRITE)
  715. __nfs4_file_put_access(fp, O_WRONLY);
  716. if (access & NFS4_SHARE_ACCESS_READ)
  717. __nfs4_file_put_access(fp, O_RDONLY);
  718. }
  719. /*
  720. * Allocate a new open/delegation state counter. This is needed for
  721. * pNFS for proper return on close semantics.
  722. *
  723. * Note that we only allocate it for pNFS-enabled exports, otherwise
  724. * all pointers to struct nfs4_clnt_odstate are always NULL.
  725. */
  726. static struct nfs4_clnt_odstate *
  727. alloc_clnt_odstate(struct nfs4_client *clp)
  728. {
  729. struct nfs4_clnt_odstate *co;
  730. co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
  731. if (co) {
  732. co->co_client = clp;
  733. refcount_set(&co->co_odcount, 1);
  734. }
  735. return co;
  736. }
  737. static void
  738. hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
  739. {
  740. struct nfs4_file *fp = co->co_file;
  741. lockdep_assert_held(&fp->fi_lock);
  742. list_add(&co->co_perfile, &fp->fi_clnt_odstate);
  743. }
  744. static inline void
  745. get_clnt_odstate(struct nfs4_clnt_odstate *co)
  746. {
  747. if (co)
  748. refcount_inc(&co->co_odcount);
  749. }
  750. static void
  751. put_clnt_odstate(struct nfs4_clnt_odstate *co)
  752. {
  753. struct nfs4_file *fp;
  754. if (!co)
  755. return;
  756. fp = co->co_file;
  757. if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
  758. list_del(&co->co_perfile);
  759. spin_unlock(&fp->fi_lock);
  760. nfsd4_return_all_file_layouts(co->co_client, fp);
  761. kmem_cache_free(odstate_slab, co);
  762. }
  763. }
  764. static struct nfs4_clnt_odstate *
  765. find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
  766. {
  767. struct nfs4_clnt_odstate *co;
  768. struct nfs4_client *cl;
  769. if (!new)
  770. return NULL;
  771. cl = new->co_client;
  772. spin_lock(&fp->fi_lock);
  773. list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
  774. if (co->co_client == cl) {
  775. get_clnt_odstate(co);
  776. goto out;
  777. }
  778. }
  779. co = new;
  780. co->co_file = fp;
  781. hash_clnt_odstate_locked(new);
  782. out:
  783. spin_unlock(&fp->fi_lock);
  784. return co;
  785. }
  786. struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
  787. void (*sc_free)(struct nfs4_stid *))
  788. {
  789. struct nfs4_stid *stid;
  790. int new_id;
  791. stid = kmem_cache_zalloc(slab, GFP_KERNEL);
  792. if (!stid)
  793. return NULL;
  794. idr_preload(GFP_KERNEL);
  795. spin_lock(&cl->cl_lock);
  796. /* Reserving 0 for start of file in nfsdfs "states" file: */
  797. new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
  798. spin_unlock(&cl->cl_lock);
  799. idr_preload_end();
  800. if (new_id < 0)
  801. goto out_free;
  802. stid->sc_free = sc_free;
  803. stid->sc_client = cl;
  804. stid->sc_stateid.si_opaque.so_id = new_id;
  805. stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
  806. /* Will be incremented before return to client: */
  807. refcount_set(&stid->sc_count, 1);
  808. spin_lock_init(&stid->sc_lock);
  809. INIT_LIST_HEAD(&stid->sc_cp_list);
  810. /*
  811. * It shouldn't be a problem to reuse an opaque stateid value.
  812. * I don't think it is for 4.1. But with 4.0 I worry that, for
  813. * example, a stray write retransmission could be accepted by
  814. * the server when it should have been rejected. Therefore,
  815. * adopt a trick from the sctp code to attempt to maximize the
  816. * amount of time until an id is reused, by ensuring they always
  817. * "increase" (mod INT_MAX):
  818. */
  819. return stid;
  820. out_free:
  821. kmem_cache_free(slab, stid);
  822. return NULL;
  823. }
  824. /*
  825. * Create a unique stateid_t to represent each COPY.
  826. */
  827. static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
  828. unsigned char cs_type)
  829. {
  830. int new_id;
  831. stid->cs_stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
  832. stid->cs_stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
  833. idr_preload(GFP_KERNEL);
  834. spin_lock(&nn->s2s_cp_lock);
  835. new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
  836. stid->cs_stid.si_opaque.so_id = new_id;
  837. stid->cs_stid.si_generation = 1;
  838. spin_unlock(&nn->s2s_cp_lock);
  839. idr_preload_end();
  840. if (new_id < 0)
  841. return 0;
  842. stid->cs_type = cs_type;
  843. return 1;
  844. }
  845. int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
  846. {
  847. return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
  848. }
  849. struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
  850. struct nfs4_stid *p_stid)
  851. {
  852. struct nfs4_cpntf_state *cps;
  853. cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
  854. if (!cps)
  855. return NULL;
  856. cps->cpntf_time = ktime_get_boottime_seconds();
  857. refcount_set(&cps->cp_stateid.cs_count, 1);
  858. if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
  859. goto out_free;
  860. spin_lock(&nn->s2s_cp_lock);
  861. list_add(&cps->cp_list, &p_stid->sc_cp_list);
  862. spin_unlock(&nn->s2s_cp_lock);
  863. return cps;
  864. out_free:
  865. kfree(cps);
  866. return NULL;
  867. }
  868. void nfs4_free_copy_state(struct nfsd4_copy *copy)
  869. {
  870. struct nfsd_net *nn;
  871. if (copy->cp_stateid.cs_type != NFS4_COPY_STID)
  872. return;
  873. nn = net_generic(copy->cp_clp->net, nfsd_net_id);
  874. spin_lock(&nn->s2s_cp_lock);
  875. idr_remove(&nn->s2s_cp_stateids,
  876. copy->cp_stateid.cs_stid.si_opaque.so_id);
  877. spin_unlock(&nn->s2s_cp_lock);
  878. }
  879. static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
  880. {
  881. struct nfs4_cpntf_state *cps;
  882. struct nfsd_net *nn;
  883. nn = net_generic(net, nfsd_net_id);
  884. spin_lock(&nn->s2s_cp_lock);
  885. while (!list_empty(&stid->sc_cp_list)) {
  886. cps = list_first_entry(&stid->sc_cp_list,
  887. struct nfs4_cpntf_state, cp_list);
  888. _free_cpntf_state_locked(nn, cps);
  889. }
  890. spin_unlock(&nn->s2s_cp_lock);
  891. }
  892. static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
  893. {
  894. struct nfs4_stid *stid;
  895. stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
  896. if (!stid)
  897. return NULL;
  898. return openlockstateid(stid);
  899. }
  900. /*
  901. * As the sc_free callback of deleg, this may be called by nfs4_put_stid
  902. * in nfsd_break_one_deleg.
  903. * Considering nfsd_break_one_deleg is called with the flc->flc_lock held,
  904. * this function mustn't ever sleep.
  905. */
  906. static void nfs4_free_deleg(struct nfs4_stid *stid)
  907. {
  908. struct nfs4_delegation *dp = delegstateid(stid);
  909. WARN_ON_ONCE(!list_empty(&stid->sc_cp_list));
  910. WARN_ON_ONCE(!list_empty(&dp->dl_perfile));
  911. WARN_ON_ONCE(!list_empty(&dp->dl_perclnt));
  912. WARN_ON_ONCE(!list_empty(&dp->dl_recall_lru));
  913. kmem_cache_free(deleg_slab, stid);
  914. atomic_long_dec(&num_delegations);
  915. }
  916. /*
  917. * When we recall a delegation, we should be careful not to hand it
  918. * out again straight away.
  919. * To ensure this we keep a pair of bloom filters ('new' and 'old')
  920. * in which the filehandles of recalled delegations are "stored".
  921. * If a filehandle appear in either filter, a delegation is blocked.
  922. * When a delegation is recalled, the filehandle is stored in the "new"
  923. * filter.
  924. * Every 30 seconds we swap the filters and clear the "new" one,
  925. * unless both are empty of course. This results in delegations for a
  926. * given filehandle being blocked for between 30 and 60 seconds.
  927. *
  928. * Each filter is 256 bits. We hash the filehandle to 32bit and use the
  929. * low 3 bytes as hash-table indices.
  930. *
  931. * 'blocked_delegations_lock', which is always taken in block_delegations(),
  932. * is used to manage concurrent access. Testing does not need the lock
  933. * except when swapping the two filters.
  934. */
  935. static DEFINE_SPINLOCK(blocked_delegations_lock);
  936. static struct bloom_pair {
  937. int entries, old_entries;
  938. time64_t swap_time;
  939. int new; /* index into 'set' */
  940. DECLARE_BITMAP(set[2], 256);
  941. } blocked_delegations;
  942. static int delegation_blocked(struct knfsd_fh *fh)
  943. {
  944. u32 hash;
  945. struct bloom_pair *bd = &blocked_delegations;
  946. if (bd->entries == 0)
  947. return 0;
  948. if (ktime_get_seconds() - bd->swap_time > 30) {
  949. spin_lock(&blocked_delegations_lock);
  950. if (ktime_get_seconds() - bd->swap_time > 30) {
  951. bd->entries -= bd->old_entries;
  952. bd->old_entries = bd->entries;
  953. bd->new = 1-bd->new;
  954. memset(bd->set[bd->new], 0,
  955. sizeof(bd->set[0]));
  956. bd->swap_time = ktime_get_seconds();
  957. }
  958. spin_unlock(&blocked_delegations_lock);
  959. }
  960. hash = jhash(&fh->fh_raw, fh->fh_size, 0);
  961. if (test_bit(hash&255, bd->set[0]) &&
  962. test_bit((hash>>8)&255, bd->set[0]) &&
  963. test_bit((hash>>16)&255, bd->set[0]))
  964. return 1;
  965. if (test_bit(hash&255, bd->set[1]) &&
  966. test_bit((hash>>8)&255, bd->set[1]) &&
  967. test_bit((hash>>16)&255, bd->set[1]))
  968. return 1;
  969. return 0;
  970. }
  971. static void block_delegations(struct knfsd_fh *fh)
  972. {
  973. u32 hash;
  974. struct bloom_pair *bd = &blocked_delegations;
  975. hash = jhash(&fh->fh_raw, fh->fh_size, 0);
  976. spin_lock(&blocked_delegations_lock);
  977. __set_bit(hash&255, bd->set[bd->new]);
  978. __set_bit((hash>>8)&255, bd->set[bd->new]);
  979. __set_bit((hash>>16)&255, bd->set[bd->new]);
  980. if (bd->entries == 0)
  981. bd->swap_time = ktime_get_seconds();
  982. bd->entries += 1;
  983. spin_unlock(&blocked_delegations_lock);
  984. }
  985. static struct nfs4_delegation *
  986. alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
  987. struct nfs4_clnt_odstate *odstate, u32 dl_type)
  988. {
  989. struct nfs4_delegation *dp;
  990. struct nfs4_stid *stid;
  991. long n;
  992. dprintk("NFSD alloc_init_deleg\n");
  993. n = atomic_long_inc_return(&num_delegations);
  994. if (n < 0 || n > max_delegations)
  995. goto out_dec;
  996. if (delegation_blocked(&fp->fi_fhandle))
  997. goto out_dec;
  998. stid = nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg);
  999. if (stid == NULL)
  1000. goto out_dec;
  1001. dp = delegstateid(stid);
  1002. /*
  1003. * delegation seqid's are never incremented. The 4.1 special
  1004. * meaning of seqid 0 isn't meaningful, really, but let's avoid
  1005. * 0 anyway just for consistency and use 1:
  1006. */
  1007. dp->dl_stid.sc_stateid.si_generation = 1;
  1008. INIT_LIST_HEAD(&dp->dl_perfile);
  1009. INIT_LIST_HEAD(&dp->dl_perclnt);
  1010. INIT_LIST_HEAD(&dp->dl_recall_lru);
  1011. dp->dl_clnt_odstate = odstate;
  1012. get_clnt_odstate(odstate);
  1013. dp->dl_type = dl_type;
  1014. dp->dl_retries = 1;
  1015. dp->dl_recalled = false;
  1016. nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
  1017. &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
  1018. nfsd4_init_cb(&dp->dl_cb_fattr.ncf_getattr, dp->dl_stid.sc_client,
  1019. &nfsd4_cb_getattr_ops, NFSPROC4_CLNT_CB_GETATTR);
  1020. dp->dl_cb_fattr.ncf_file_modified = false;
  1021. dp->dl_cb_fattr.ncf_cb_bmap[0] = FATTR4_WORD0_CHANGE | FATTR4_WORD0_SIZE;
  1022. get_nfs4_file(fp);
  1023. dp->dl_stid.sc_file = fp;
  1024. return dp;
  1025. out_dec:
  1026. atomic_long_dec(&num_delegations);
  1027. return NULL;
  1028. }
  1029. void
  1030. nfs4_put_stid(struct nfs4_stid *s)
  1031. {
  1032. struct nfs4_file *fp = s->sc_file;
  1033. struct nfs4_client *clp = s->sc_client;
  1034. might_lock(&clp->cl_lock);
  1035. if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
  1036. wake_up_all(&close_wq);
  1037. return;
  1038. }
  1039. idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
  1040. if (s->sc_status & SC_STATUS_ADMIN_REVOKED)
  1041. atomic_dec(&s->sc_client->cl_admin_revoked);
  1042. nfs4_free_cpntf_statelist(clp->net, s);
  1043. spin_unlock(&clp->cl_lock);
  1044. s->sc_free(s);
  1045. if (fp)
  1046. put_nfs4_file(fp);
  1047. }
  1048. void
  1049. nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
  1050. {
  1051. stateid_t *src = &stid->sc_stateid;
  1052. spin_lock(&stid->sc_lock);
  1053. if (unlikely(++src->si_generation == 0))
  1054. src->si_generation = 1;
  1055. memcpy(dst, src, sizeof(*dst));
  1056. spin_unlock(&stid->sc_lock);
  1057. }
  1058. static void put_deleg_file(struct nfs4_file *fp)
  1059. {
  1060. struct nfsd_file *nf = NULL;
  1061. spin_lock(&fp->fi_lock);
  1062. if (--fp->fi_delegees == 0)
  1063. swap(nf, fp->fi_deleg_file);
  1064. spin_unlock(&fp->fi_lock);
  1065. if (nf)
  1066. nfsd_file_put(nf);
  1067. }
  1068. static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
  1069. {
  1070. struct nfs4_file *fp = dp->dl_stid.sc_file;
  1071. struct nfsd_file *nf = fp->fi_deleg_file;
  1072. WARN_ON_ONCE(!fp->fi_delegees);
  1073. kernel_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
  1074. put_deleg_file(fp);
  1075. }
  1076. static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
  1077. {
  1078. put_clnt_odstate(dp->dl_clnt_odstate);
  1079. nfs4_unlock_deleg_lease(dp);
  1080. nfs4_put_stid(&dp->dl_stid);
  1081. }
  1082. /**
  1083. * nfs4_delegation_exists - Discover if this delegation already exists
  1084. * @clp: a pointer to the nfs4_client we're granting a delegation to
  1085. * @fp: a pointer to the nfs4_file we're granting a delegation on
  1086. *
  1087. * Return:
  1088. * On success: true iff an existing delegation is found
  1089. */
  1090. static bool
  1091. nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
  1092. {
  1093. struct nfs4_delegation *searchdp = NULL;
  1094. struct nfs4_client *searchclp = NULL;
  1095. lockdep_assert_held(&state_lock);
  1096. lockdep_assert_held(&fp->fi_lock);
  1097. list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
  1098. searchclp = searchdp->dl_stid.sc_client;
  1099. if (clp == searchclp) {
  1100. return true;
  1101. }
  1102. }
  1103. return false;
  1104. }
  1105. /**
  1106. * hash_delegation_locked - Add a delegation to the appropriate lists
  1107. * @dp: a pointer to the nfs4_delegation we are adding.
  1108. * @fp: a pointer to the nfs4_file we're granting a delegation on
  1109. *
  1110. * Return:
  1111. * On success: NULL if the delegation was successfully hashed.
  1112. *
  1113. * On error: -EAGAIN if one was previously granted to this
  1114. * nfs4_client for this nfs4_file. Delegation is not hashed.
  1115. *
  1116. */
  1117. static int
  1118. hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
  1119. {
  1120. struct nfs4_client *clp = dp->dl_stid.sc_client;
  1121. lockdep_assert_held(&state_lock);
  1122. lockdep_assert_held(&fp->fi_lock);
  1123. lockdep_assert_held(&clp->cl_lock);
  1124. if (nfs4_delegation_exists(clp, fp))
  1125. return -EAGAIN;
  1126. refcount_inc(&dp->dl_stid.sc_count);
  1127. dp->dl_stid.sc_type = SC_TYPE_DELEG;
  1128. list_add(&dp->dl_perfile, &fp->fi_delegations);
  1129. list_add(&dp->dl_perclnt, &clp->cl_delegations);
  1130. return 0;
  1131. }
  1132. static bool delegation_hashed(struct nfs4_delegation *dp)
  1133. {
  1134. return !(list_empty(&dp->dl_perfile));
  1135. }
  1136. static bool
  1137. unhash_delegation_locked(struct nfs4_delegation *dp, unsigned short statusmask)
  1138. {
  1139. struct nfs4_file *fp = dp->dl_stid.sc_file;
  1140. lockdep_assert_held(&state_lock);
  1141. if (!delegation_hashed(dp))
  1142. return false;
  1143. if (statusmask == SC_STATUS_REVOKED &&
  1144. dp->dl_stid.sc_client->cl_minorversion == 0)
  1145. statusmask = SC_STATUS_CLOSED;
  1146. dp->dl_stid.sc_status |= statusmask;
  1147. if (statusmask & SC_STATUS_ADMIN_REVOKED)
  1148. atomic_inc(&dp->dl_stid.sc_client->cl_admin_revoked);
  1149. /* Ensure that deleg break won't try to requeue it */
  1150. ++dp->dl_time;
  1151. spin_lock(&fp->fi_lock);
  1152. list_del_init(&dp->dl_perclnt);
  1153. list_del_init(&dp->dl_recall_lru);
  1154. list_del_init(&dp->dl_perfile);
  1155. spin_unlock(&fp->fi_lock);
  1156. return true;
  1157. }
  1158. static void destroy_delegation(struct nfs4_delegation *dp)
  1159. {
  1160. bool unhashed;
  1161. spin_lock(&state_lock);
  1162. unhashed = unhash_delegation_locked(dp, SC_STATUS_CLOSED);
  1163. spin_unlock(&state_lock);
  1164. if (unhashed)
  1165. destroy_unhashed_deleg(dp);
  1166. }
  1167. /**
  1168. * revoke_delegation - perform nfs4 delegation structure cleanup
  1169. * @dp: pointer to the delegation
  1170. *
  1171. * This function assumes that it's called either from the administrative
  1172. * interface (nfsd4_revoke_states()) that's revoking a specific delegation
  1173. * stateid or it's called from a laundromat thread (nfsd4_landromat()) that
  1174. * determined that this specific state has expired and needs to be revoked
  1175. * (both mark state with the appropriate stid sc_status mode). It is also
  1176. * assumed that a reference was taken on the @dp state.
  1177. *
  1178. * If this function finds that the @dp state is SC_STATUS_FREED it means
  1179. * that a FREE_STATEID operation for this stateid has been processed and
  1180. * we can proceed to removing it from recalled list. However, if @dp state
  1181. * isn't marked SC_STATUS_FREED, it means we need place it on the cl_revoked
  1182. * list and wait for the FREE_STATEID to arrive from the client. At the same
  1183. * time, we need to mark it as SC_STATUS_FREEABLE to indicate to the
  1184. * nfsd4_free_stateid() function that this stateid has already been added
  1185. * to the cl_revoked list and that nfsd4_free_stateid() is now responsible
  1186. * for removing it from the list. Inspection of where the delegation state
  1187. * in the revocation process is protected by the clp->cl_lock.
  1188. */
  1189. static void revoke_delegation(struct nfs4_delegation *dp)
  1190. {
  1191. struct nfs4_client *clp = dp->dl_stid.sc_client;
  1192. WARN_ON(!list_empty(&dp->dl_recall_lru));
  1193. WARN_ON_ONCE(!(dp->dl_stid.sc_status &
  1194. (SC_STATUS_REVOKED | SC_STATUS_ADMIN_REVOKED)));
  1195. trace_nfsd_stid_revoke(&dp->dl_stid);
  1196. spin_lock(&clp->cl_lock);
  1197. if (dp->dl_stid.sc_status & SC_STATUS_FREED) {
  1198. list_del_init(&dp->dl_recall_lru);
  1199. goto out;
  1200. }
  1201. list_add(&dp->dl_recall_lru, &clp->cl_revoked);
  1202. dp->dl_stid.sc_status |= SC_STATUS_FREEABLE;
  1203. out:
  1204. spin_unlock(&clp->cl_lock);
  1205. destroy_unhashed_deleg(dp);
  1206. }
  1207. /*
  1208. * SETCLIENTID state
  1209. */
  1210. static unsigned int clientid_hashval(u32 id)
  1211. {
  1212. return id & CLIENT_HASH_MASK;
  1213. }
  1214. static unsigned int clientstr_hashval(struct xdr_netobj name)
  1215. {
  1216. return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
  1217. }
  1218. /*
  1219. * A stateid that had a deny mode associated with it is being released
  1220. * or downgraded. Recalculate the deny mode on the file.
  1221. */
  1222. static void
  1223. recalculate_deny_mode(struct nfs4_file *fp)
  1224. {
  1225. struct nfs4_ol_stateid *stp;
  1226. u32 old_deny;
  1227. spin_lock(&fp->fi_lock);
  1228. old_deny = fp->fi_share_deny;
  1229. fp->fi_share_deny = 0;
  1230. list_for_each_entry(stp, &fp->fi_stateids, st_perfile) {
  1231. fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
  1232. if (fp->fi_share_deny == old_deny)
  1233. break;
  1234. }
  1235. spin_unlock(&fp->fi_lock);
  1236. }
  1237. static void
  1238. reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
  1239. {
  1240. int i;
  1241. bool change = false;
  1242. for (i = 1; i < 4; i++) {
  1243. if ((i & deny) != i) {
  1244. change = true;
  1245. clear_deny(i, stp);
  1246. }
  1247. }
  1248. /* Recalculate per-file deny mode if there was a change */
  1249. if (change)
  1250. recalculate_deny_mode(stp->st_stid.sc_file);
  1251. }
  1252. /* release all access and file references for a given stateid */
  1253. static void
  1254. release_all_access(struct nfs4_ol_stateid *stp)
  1255. {
  1256. int i;
  1257. struct nfs4_file *fp = stp->st_stid.sc_file;
  1258. if (fp && stp->st_deny_bmap != 0)
  1259. recalculate_deny_mode(fp);
  1260. for (i = 1; i < 4; i++) {
  1261. if (test_access(i, stp))
  1262. nfs4_file_put_access(stp->st_stid.sc_file, i);
  1263. clear_access(i, stp);
  1264. }
  1265. }
  1266. static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
  1267. {
  1268. kfree(sop->so_owner.data);
  1269. sop->so_ops->so_free(sop);
  1270. }
  1271. static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
  1272. {
  1273. struct nfs4_client *clp = sop->so_client;
  1274. might_lock(&clp->cl_lock);
  1275. if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
  1276. return;
  1277. sop->so_ops->so_unhash(sop);
  1278. spin_unlock(&clp->cl_lock);
  1279. nfs4_free_stateowner(sop);
  1280. }
  1281. static bool
  1282. nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
  1283. {
  1284. return list_empty(&stp->st_perfile);
  1285. }
  1286. static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
  1287. {
  1288. struct nfs4_file *fp = stp->st_stid.sc_file;
  1289. lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
  1290. if (list_empty(&stp->st_perfile))
  1291. return false;
  1292. spin_lock(&fp->fi_lock);
  1293. list_del_init(&stp->st_perfile);
  1294. spin_unlock(&fp->fi_lock);
  1295. list_del(&stp->st_perstateowner);
  1296. return true;
  1297. }
  1298. static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
  1299. {
  1300. struct nfs4_ol_stateid *stp = openlockstateid(stid);
  1301. put_clnt_odstate(stp->st_clnt_odstate);
  1302. release_all_access(stp);
  1303. if (stp->st_stateowner)
  1304. nfs4_put_stateowner(stp->st_stateowner);
  1305. WARN_ON(!list_empty(&stid->sc_cp_list));
  1306. kmem_cache_free(stateid_slab, stid);
  1307. }
  1308. static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
  1309. {
  1310. struct nfs4_ol_stateid *stp = openlockstateid(stid);
  1311. struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
  1312. struct nfsd_file *nf;
  1313. nf = find_any_file(stp->st_stid.sc_file);
  1314. if (nf) {
  1315. get_file(nf->nf_file);
  1316. filp_close(nf->nf_file, (fl_owner_t)lo);
  1317. nfsd_file_put(nf);
  1318. }
  1319. nfs4_free_ol_stateid(stid);
  1320. }
  1321. /*
  1322. * Put the persistent reference to an already unhashed generic stateid, while
  1323. * holding the cl_lock. If it's the last reference, then put it onto the
  1324. * reaplist for later destruction.
  1325. */
  1326. static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
  1327. struct list_head *reaplist)
  1328. {
  1329. struct nfs4_stid *s = &stp->st_stid;
  1330. struct nfs4_client *clp = s->sc_client;
  1331. lockdep_assert_held(&clp->cl_lock);
  1332. WARN_ON_ONCE(!list_empty(&stp->st_locks));
  1333. if (!refcount_dec_and_test(&s->sc_count)) {
  1334. wake_up_all(&close_wq);
  1335. return;
  1336. }
  1337. idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
  1338. if (s->sc_status & SC_STATUS_ADMIN_REVOKED)
  1339. atomic_dec(&s->sc_client->cl_admin_revoked);
  1340. list_add(&stp->st_locks, reaplist);
  1341. }
  1342. static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
  1343. {
  1344. lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
  1345. if (!unhash_ol_stateid(stp))
  1346. return false;
  1347. list_del_init(&stp->st_locks);
  1348. stp->st_stid.sc_status |= SC_STATUS_CLOSED;
  1349. return true;
  1350. }
  1351. static void release_lock_stateid(struct nfs4_ol_stateid *stp)
  1352. {
  1353. struct nfs4_client *clp = stp->st_stid.sc_client;
  1354. bool unhashed;
  1355. spin_lock(&clp->cl_lock);
  1356. unhashed = unhash_lock_stateid(stp);
  1357. spin_unlock(&clp->cl_lock);
  1358. if (unhashed)
  1359. nfs4_put_stid(&stp->st_stid);
  1360. }
  1361. static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
  1362. {
  1363. struct nfs4_client *clp = lo->lo_owner.so_client;
  1364. lockdep_assert_held(&clp->cl_lock);
  1365. list_del_init(&lo->lo_owner.so_strhash);
  1366. }
  1367. /*
  1368. * Free a list of generic stateids that were collected earlier after being
  1369. * fully unhashed.
  1370. */
  1371. static void
  1372. free_ol_stateid_reaplist(struct list_head *reaplist)
  1373. {
  1374. struct nfs4_ol_stateid *stp;
  1375. struct nfs4_file *fp;
  1376. might_sleep();
  1377. while (!list_empty(reaplist)) {
  1378. stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
  1379. st_locks);
  1380. list_del(&stp->st_locks);
  1381. fp = stp->st_stid.sc_file;
  1382. stp->st_stid.sc_free(&stp->st_stid);
  1383. if (fp)
  1384. put_nfs4_file(fp);
  1385. }
  1386. }
  1387. static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
  1388. struct list_head *reaplist)
  1389. {
  1390. struct nfs4_ol_stateid *stp;
  1391. lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
  1392. while (!list_empty(&open_stp->st_locks)) {
  1393. stp = list_entry(open_stp->st_locks.next,
  1394. struct nfs4_ol_stateid, st_locks);
  1395. unhash_lock_stateid(stp);
  1396. put_ol_stateid_locked(stp, reaplist);
  1397. }
  1398. }
  1399. static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
  1400. struct list_head *reaplist)
  1401. {
  1402. lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
  1403. if (!unhash_ol_stateid(stp))
  1404. return false;
  1405. release_open_stateid_locks(stp, reaplist);
  1406. return true;
  1407. }
  1408. static void release_open_stateid(struct nfs4_ol_stateid *stp)
  1409. {
  1410. LIST_HEAD(reaplist);
  1411. spin_lock(&stp->st_stid.sc_client->cl_lock);
  1412. stp->st_stid.sc_status |= SC_STATUS_CLOSED;
  1413. if (unhash_open_stateid(stp, &reaplist))
  1414. put_ol_stateid_locked(stp, &reaplist);
  1415. spin_unlock(&stp->st_stid.sc_client->cl_lock);
  1416. free_ol_stateid_reaplist(&reaplist);
  1417. }
  1418. static bool nfs4_openowner_unhashed(struct nfs4_openowner *oo)
  1419. {
  1420. lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
  1421. return list_empty(&oo->oo_owner.so_strhash) &&
  1422. list_empty(&oo->oo_perclient);
  1423. }
  1424. static void unhash_openowner_locked(struct nfs4_openowner *oo)
  1425. {
  1426. struct nfs4_client *clp = oo->oo_owner.so_client;
  1427. lockdep_assert_held(&clp->cl_lock);
  1428. list_del_init(&oo->oo_owner.so_strhash);
  1429. list_del_init(&oo->oo_perclient);
  1430. }
  1431. static void release_last_closed_stateid(struct nfs4_openowner *oo)
  1432. {
  1433. struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
  1434. nfsd_net_id);
  1435. struct nfs4_ol_stateid *s;
  1436. spin_lock(&nn->client_lock);
  1437. s = oo->oo_last_closed_stid;
  1438. if (s) {
  1439. list_del_init(&oo->oo_close_lru);
  1440. oo->oo_last_closed_stid = NULL;
  1441. }
  1442. spin_unlock(&nn->client_lock);
  1443. if (s)
  1444. nfs4_put_stid(&s->st_stid);
  1445. }
  1446. static void release_openowner(struct nfs4_openowner *oo)
  1447. {
  1448. struct nfs4_ol_stateid *stp;
  1449. struct nfs4_client *clp = oo->oo_owner.so_client;
  1450. LIST_HEAD(reaplist);
  1451. spin_lock(&clp->cl_lock);
  1452. unhash_openowner_locked(oo);
  1453. while (!list_empty(&oo->oo_owner.so_stateids)) {
  1454. stp = list_first_entry(&oo->oo_owner.so_stateids,
  1455. struct nfs4_ol_stateid, st_perstateowner);
  1456. if (unhash_open_stateid(stp, &reaplist))
  1457. put_ol_stateid_locked(stp, &reaplist);
  1458. }
  1459. spin_unlock(&clp->cl_lock);
  1460. free_ol_stateid_reaplist(&reaplist);
  1461. release_last_closed_stateid(oo);
  1462. nfs4_put_stateowner(&oo->oo_owner);
  1463. }
  1464. static struct nfs4_stid *find_one_sb_stid(struct nfs4_client *clp,
  1465. struct super_block *sb,
  1466. unsigned int sc_types)
  1467. {
  1468. unsigned long id, tmp;
  1469. struct nfs4_stid *stid;
  1470. spin_lock(&clp->cl_lock);
  1471. idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id)
  1472. if ((stid->sc_type & sc_types) &&
  1473. stid->sc_status == 0 &&
  1474. stid->sc_file->fi_inode->i_sb == sb) {
  1475. refcount_inc(&stid->sc_count);
  1476. break;
  1477. }
  1478. spin_unlock(&clp->cl_lock);
  1479. return stid;
  1480. }
  1481. /**
  1482. * nfsd4_revoke_states - revoke all nfsv4 states associated with given filesystem
  1483. * @net: used to identify instance of nfsd (there is one per net namespace)
  1484. * @sb: super_block used to identify target filesystem
  1485. *
  1486. * All nfs4 states (open, lock, delegation, layout) held by the server instance
  1487. * and associated with a file on the given filesystem will be revoked resulting
  1488. * in any files being closed and so all references from nfsd to the filesystem
  1489. * being released. Thus nfsd will no longer prevent the filesystem from being
  1490. * unmounted.
  1491. *
  1492. * The clients which own the states will subsequently being notified that the
  1493. * states have been "admin-revoked".
  1494. */
  1495. void nfsd4_revoke_states(struct net *net, struct super_block *sb)
  1496. {
  1497. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1498. unsigned int idhashval;
  1499. unsigned int sc_types;
  1500. sc_types = SC_TYPE_OPEN | SC_TYPE_LOCK | SC_TYPE_DELEG | SC_TYPE_LAYOUT;
  1501. spin_lock(&nn->client_lock);
  1502. for (idhashval = 0; idhashval < CLIENT_HASH_MASK; idhashval++) {
  1503. struct list_head *head = &nn->conf_id_hashtbl[idhashval];
  1504. struct nfs4_client *clp;
  1505. retry:
  1506. list_for_each_entry(clp, head, cl_idhash) {
  1507. struct nfs4_stid *stid = find_one_sb_stid(clp, sb,
  1508. sc_types);
  1509. if (stid) {
  1510. struct nfs4_ol_stateid *stp;
  1511. struct nfs4_delegation *dp;
  1512. struct nfs4_layout_stateid *ls;
  1513. spin_unlock(&nn->client_lock);
  1514. switch (stid->sc_type) {
  1515. case SC_TYPE_OPEN:
  1516. stp = openlockstateid(stid);
  1517. mutex_lock_nested(&stp->st_mutex,
  1518. OPEN_STATEID_MUTEX);
  1519. spin_lock(&clp->cl_lock);
  1520. if (stid->sc_status == 0) {
  1521. stid->sc_status |=
  1522. SC_STATUS_ADMIN_REVOKED;
  1523. atomic_inc(&clp->cl_admin_revoked);
  1524. spin_unlock(&clp->cl_lock);
  1525. release_all_access(stp);
  1526. } else
  1527. spin_unlock(&clp->cl_lock);
  1528. mutex_unlock(&stp->st_mutex);
  1529. break;
  1530. case SC_TYPE_LOCK:
  1531. stp = openlockstateid(stid);
  1532. mutex_lock_nested(&stp->st_mutex,
  1533. LOCK_STATEID_MUTEX);
  1534. spin_lock(&clp->cl_lock);
  1535. if (stid->sc_status == 0) {
  1536. struct nfs4_lockowner *lo =
  1537. lockowner(stp->st_stateowner);
  1538. struct nfsd_file *nf;
  1539. stid->sc_status |=
  1540. SC_STATUS_ADMIN_REVOKED;
  1541. atomic_inc(&clp->cl_admin_revoked);
  1542. spin_unlock(&clp->cl_lock);
  1543. nf = find_any_file(stp->st_stid.sc_file);
  1544. if (nf) {
  1545. get_file(nf->nf_file);
  1546. filp_close(nf->nf_file,
  1547. (fl_owner_t)lo);
  1548. nfsd_file_put(nf);
  1549. }
  1550. release_all_access(stp);
  1551. } else
  1552. spin_unlock(&clp->cl_lock);
  1553. mutex_unlock(&stp->st_mutex);
  1554. break;
  1555. case SC_TYPE_DELEG:
  1556. refcount_inc(&stid->sc_count);
  1557. dp = delegstateid(stid);
  1558. spin_lock(&state_lock);
  1559. if (!unhash_delegation_locked(
  1560. dp, SC_STATUS_ADMIN_REVOKED))
  1561. dp = NULL;
  1562. spin_unlock(&state_lock);
  1563. if (dp)
  1564. revoke_delegation(dp);
  1565. break;
  1566. case SC_TYPE_LAYOUT:
  1567. ls = layoutstateid(stid);
  1568. nfsd4_close_layout(ls);
  1569. break;
  1570. }
  1571. nfs4_put_stid(stid);
  1572. spin_lock(&nn->client_lock);
  1573. if (clp->cl_minorversion == 0)
  1574. /* Allow cleanup after a lease period.
  1575. * store_release ensures cleanup will
  1576. * see any newly revoked states if it
  1577. * sees the time updated.
  1578. */
  1579. nn->nfs40_last_revoke =
  1580. ktime_get_boottime_seconds();
  1581. goto retry;
  1582. }
  1583. }
  1584. }
  1585. spin_unlock(&nn->client_lock);
  1586. }
  1587. static inline int
  1588. hash_sessionid(struct nfs4_sessionid *sessionid)
  1589. {
  1590. struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
  1591. return sid->sequence % SESSION_HASH_SIZE;
  1592. }
  1593. #ifdef CONFIG_SUNRPC_DEBUG
  1594. static inline void
  1595. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  1596. {
  1597. u32 *ptr = (u32 *)(&sessionid->data[0]);
  1598. dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
  1599. }
  1600. #else
  1601. static inline void
  1602. dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
  1603. {
  1604. }
  1605. #endif
  1606. /*
  1607. * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
  1608. * won't be used for replay.
  1609. */
  1610. void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
  1611. {
  1612. struct nfs4_stateowner *so = cstate->replay_owner;
  1613. if (nfserr == nfserr_replay_me)
  1614. return;
  1615. if (!seqid_mutating_err(ntohl(nfserr))) {
  1616. nfsd4_cstate_clear_replay(cstate);
  1617. return;
  1618. }
  1619. if (!so)
  1620. return;
  1621. if (so->so_is_open_owner)
  1622. release_last_closed_stateid(openowner(so));
  1623. so->so_seqid++;
  1624. return;
  1625. }
  1626. static void
  1627. gen_sessionid(struct nfsd4_session *ses)
  1628. {
  1629. struct nfs4_client *clp = ses->se_client;
  1630. struct nfsd4_sessionid *sid;
  1631. sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
  1632. sid->clientid = clp->cl_clientid;
  1633. sid->sequence = current_sessionid++;
  1634. sid->reserved = 0;
  1635. }
  1636. /*
  1637. * The protocol defines ca_maxresponssize_cached to include the size of
  1638. * the rpc header, but all we need to cache is the data starting after
  1639. * the end of the initial SEQUENCE operation--the rest we regenerate
  1640. * each time. Therefore we can advertise a ca_maxresponssize_cached
  1641. * value that is the number of bytes in our cache plus a few additional
  1642. * bytes. In order to stay on the safe side, and not promise more than
  1643. * we can cache, those additional bytes must be the minimum possible: 24
  1644. * bytes of rpc header (xid through accept state, with AUTH_NULL
  1645. * verifier), 12 for the compound header (with zero-length tag), and 44
  1646. * for the SEQUENCE op response:
  1647. */
  1648. #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
  1649. static void
  1650. free_session_slots(struct nfsd4_session *ses)
  1651. {
  1652. int i;
  1653. for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
  1654. free_svc_cred(&ses->se_slots[i]->sl_cred);
  1655. kfree(ses->se_slots[i]);
  1656. }
  1657. }
  1658. /*
  1659. * We don't actually need to cache the rpc and session headers, so we
  1660. * can allocate a little less for each slot:
  1661. */
  1662. static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
  1663. {
  1664. u32 size;
  1665. if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
  1666. size = 0;
  1667. else
  1668. size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
  1669. return size + sizeof(struct nfsd4_slot);
  1670. }
  1671. /*
  1672. * XXX: If we run out of reserved DRC memory we could (up to a point)
  1673. * re-negotiate active sessions and reduce their slot usage to make
  1674. * room for new connections. For now we just fail the create session.
  1675. */
  1676. static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
  1677. {
  1678. u32 slotsize = slot_bytes(ca);
  1679. u32 num = ca->maxreqs;
  1680. unsigned long avail, total_avail;
  1681. unsigned int scale_factor;
  1682. spin_lock(&nfsd_drc_lock);
  1683. if (nfsd_drc_max_mem > nfsd_drc_mem_used)
  1684. total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
  1685. else
  1686. /* We have handed out more space than we chose in
  1687. * set_max_drc() to allow. That isn't really a
  1688. * problem as long as that doesn't make us think we
  1689. * have lots more due to integer overflow.
  1690. */
  1691. total_avail = 0;
  1692. avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
  1693. /*
  1694. * Never use more than a fraction of the remaining memory,
  1695. * unless it's the only way to give this client a slot.
  1696. * The chosen fraction is either 1/8 or 1/number of threads,
  1697. * whichever is smaller. This ensures there are adequate
  1698. * slots to support multiple clients per thread.
  1699. * Give the client one slot even if that would require
  1700. * over-allocation--it is better than failure.
  1701. */
  1702. scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
  1703. avail = clamp_t(unsigned long, avail, slotsize,
  1704. total_avail/scale_factor);
  1705. num = min_t(int, num, avail / slotsize);
  1706. num = max_t(int, num, 1);
  1707. nfsd_drc_mem_used += num * slotsize;
  1708. spin_unlock(&nfsd_drc_lock);
  1709. return num;
  1710. }
  1711. static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
  1712. {
  1713. int slotsize = slot_bytes(ca);
  1714. spin_lock(&nfsd_drc_lock);
  1715. nfsd_drc_mem_used -= slotsize * ca->maxreqs;
  1716. spin_unlock(&nfsd_drc_lock);
  1717. }
  1718. static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
  1719. struct nfsd4_channel_attrs *battrs)
  1720. {
  1721. int numslots = fattrs->maxreqs;
  1722. int slotsize = slot_bytes(fattrs);
  1723. struct nfsd4_session *new;
  1724. int i;
  1725. BUILD_BUG_ON(struct_size(new, se_slots, NFSD_MAX_SLOTS_PER_SESSION)
  1726. > PAGE_SIZE);
  1727. new = kzalloc(struct_size(new, se_slots, numslots), GFP_KERNEL);
  1728. if (!new)
  1729. return NULL;
  1730. /* allocate each struct nfsd4_slot and data cache in one piece */
  1731. for (i = 0; i < numslots; i++) {
  1732. new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
  1733. if (!new->se_slots[i])
  1734. goto out_free;
  1735. }
  1736. memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
  1737. memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
  1738. return new;
  1739. out_free:
  1740. while (i--)
  1741. kfree(new->se_slots[i]);
  1742. kfree(new);
  1743. return NULL;
  1744. }
  1745. static void free_conn(struct nfsd4_conn *c)
  1746. {
  1747. svc_xprt_put(c->cn_xprt);
  1748. kfree(c);
  1749. }
  1750. static void nfsd4_conn_lost(struct svc_xpt_user *u)
  1751. {
  1752. struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
  1753. struct nfs4_client *clp = c->cn_session->se_client;
  1754. trace_nfsd_cb_lost(clp);
  1755. spin_lock(&clp->cl_lock);
  1756. if (!list_empty(&c->cn_persession)) {
  1757. list_del(&c->cn_persession);
  1758. free_conn(c);
  1759. }
  1760. nfsd4_probe_callback(clp);
  1761. spin_unlock(&clp->cl_lock);
  1762. }
  1763. static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
  1764. {
  1765. struct nfsd4_conn *conn;
  1766. conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
  1767. if (!conn)
  1768. return NULL;
  1769. svc_xprt_get(rqstp->rq_xprt);
  1770. conn->cn_xprt = rqstp->rq_xprt;
  1771. conn->cn_flags = flags;
  1772. INIT_LIST_HEAD(&conn->cn_xpt_user.list);
  1773. return conn;
  1774. }
  1775. static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  1776. {
  1777. conn->cn_session = ses;
  1778. list_add(&conn->cn_persession, &ses->se_conns);
  1779. }
  1780. static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
  1781. {
  1782. struct nfs4_client *clp = ses->se_client;
  1783. spin_lock(&clp->cl_lock);
  1784. __nfsd4_hash_conn(conn, ses);
  1785. spin_unlock(&clp->cl_lock);
  1786. }
  1787. static int nfsd4_register_conn(struct nfsd4_conn *conn)
  1788. {
  1789. conn->cn_xpt_user.callback = nfsd4_conn_lost;
  1790. return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
  1791. }
  1792. static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
  1793. {
  1794. int ret;
  1795. nfsd4_hash_conn(conn, ses);
  1796. ret = nfsd4_register_conn(conn);
  1797. if (ret)
  1798. /* oops; xprt is already down: */
  1799. nfsd4_conn_lost(&conn->cn_xpt_user);
  1800. /* We may have gained or lost a callback channel: */
  1801. nfsd4_probe_callback_sync(ses->se_client);
  1802. }
  1803. static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
  1804. {
  1805. u32 dir = NFS4_CDFC4_FORE;
  1806. if (cses->flags & SESSION4_BACK_CHAN)
  1807. dir |= NFS4_CDFC4_BACK;
  1808. return alloc_conn(rqstp, dir);
  1809. }
  1810. /* must be called under client_lock */
  1811. static void nfsd4_del_conns(struct nfsd4_session *s)
  1812. {
  1813. struct nfs4_client *clp = s->se_client;
  1814. struct nfsd4_conn *c;
  1815. spin_lock(&clp->cl_lock);
  1816. while (!list_empty(&s->se_conns)) {
  1817. c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
  1818. list_del_init(&c->cn_persession);
  1819. spin_unlock(&clp->cl_lock);
  1820. unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
  1821. free_conn(c);
  1822. spin_lock(&clp->cl_lock);
  1823. }
  1824. spin_unlock(&clp->cl_lock);
  1825. }
  1826. static void __free_session(struct nfsd4_session *ses)
  1827. {
  1828. free_session_slots(ses);
  1829. kfree(ses);
  1830. }
  1831. static void free_session(struct nfsd4_session *ses)
  1832. {
  1833. nfsd4_del_conns(ses);
  1834. nfsd4_put_drc_mem(&ses->se_fchannel);
  1835. __free_session(ses);
  1836. }
  1837. static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
  1838. {
  1839. int idx;
  1840. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  1841. new->se_client = clp;
  1842. gen_sessionid(new);
  1843. INIT_LIST_HEAD(&new->se_conns);
  1844. new->se_cb_seq_nr = 1;
  1845. new->se_flags = cses->flags;
  1846. new->se_cb_prog = cses->callback_prog;
  1847. new->se_cb_sec = cses->cb_sec;
  1848. atomic_set(&new->se_ref, 0);
  1849. idx = hash_sessionid(&new->se_sessionid);
  1850. list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
  1851. spin_lock(&clp->cl_lock);
  1852. list_add(&new->se_perclnt, &clp->cl_sessions);
  1853. spin_unlock(&clp->cl_lock);
  1854. {
  1855. struct sockaddr *sa = svc_addr(rqstp);
  1856. /*
  1857. * This is a little silly; with sessions there's no real
  1858. * use for the callback address. Use the peer address
  1859. * as a reasonable default for now, but consider fixing
  1860. * the rpc client not to require an address in the
  1861. * future:
  1862. */
  1863. rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
  1864. clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
  1865. }
  1866. }
  1867. /* caller must hold client_lock */
  1868. static struct nfsd4_session *
  1869. __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
  1870. {
  1871. struct nfsd4_session *elem;
  1872. int idx;
  1873. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  1874. lockdep_assert_held(&nn->client_lock);
  1875. dump_sessionid(__func__, sessionid);
  1876. idx = hash_sessionid(sessionid);
  1877. /* Search in the appropriate list */
  1878. list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
  1879. if (!memcmp(elem->se_sessionid.data, sessionid->data,
  1880. NFS4_MAX_SESSIONID_LEN)) {
  1881. return elem;
  1882. }
  1883. }
  1884. dprintk("%s: session not found\n", __func__);
  1885. return NULL;
  1886. }
  1887. static struct nfsd4_session *
  1888. find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
  1889. __be32 *ret)
  1890. {
  1891. struct nfsd4_session *session;
  1892. __be32 status = nfserr_badsession;
  1893. session = __find_in_sessionid_hashtbl(sessionid, net);
  1894. if (!session)
  1895. goto out;
  1896. status = nfsd4_get_session_locked(session);
  1897. if (status)
  1898. session = NULL;
  1899. out:
  1900. *ret = status;
  1901. return session;
  1902. }
  1903. /* caller must hold client_lock */
  1904. static void
  1905. unhash_session(struct nfsd4_session *ses)
  1906. {
  1907. struct nfs4_client *clp = ses->se_client;
  1908. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  1909. lockdep_assert_held(&nn->client_lock);
  1910. list_del(&ses->se_hash);
  1911. spin_lock(&ses->se_client->cl_lock);
  1912. list_del(&ses->se_perclnt);
  1913. spin_unlock(&ses->se_client->cl_lock);
  1914. }
  1915. /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
  1916. static int
  1917. STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
  1918. {
  1919. /*
  1920. * We're assuming the clid was not given out from a boot
  1921. * precisely 2^32 (about 136 years) before this one. That seems
  1922. * a safe assumption:
  1923. */
  1924. if (clid->cl_boot == (u32)nn->boot_time)
  1925. return 0;
  1926. trace_nfsd_clid_stale(clid);
  1927. return 1;
  1928. }
  1929. /*
  1930. * XXX Should we use a slab cache ?
  1931. * This type of memory management is somewhat inefficient, but we use it
  1932. * anyway since SETCLIENTID is not a common operation.
  1933. */
  1934. static struct nfs4_client *alloc_client(struct xdr_netobj name,
  1935. struct nfsd_net *nn)
  1936. {
  1937. struct nfs4_client *clp;
  1938. int i;
  1939. if (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) {
  1940. mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
  1941. return NULL;
  1942. }
  1943. clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
  1944. if (clp == NULL)
  1945. return NULL;
  1946. xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
  1947. if (clp->cl_name.data == NULL)
  1948. goto err_no_name;
  1949. clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
  1950. sizeof(struct list_head),
  1951. GFP_KERNEL);
  1952. if (!clp->cl_ownerstr_hashtbl)
  1953. goto err_no_hashtbl;
  1954. clp->cl_callback_wq = alloc_ordered_workqueue("nfsd4_callbacks", 0);
  1955. if (!clp->cl_callback_wq)
  1956. goto err_no_callback_wq;
  1957. for (i = 0; i < OWNER_HASH_SIZE; i++)
  1958. INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
  1959. INIT_LIST_HEAD(&clp->cl_sessions);
  1960. idr_init(&clp->cl_stateids);
  1961. atomic_set(&clp->cl_rpc_users, 0);
  1962. clp->cl_cb_state = NFSD4_CB_UNKNOWN;
  1963. clp->cl_state = NFSD4_ACTIVE;
  1964. atomic_inc(&nn->nfs4_client_count);
  1965. atomic_set(&clp->cl_delegs_in_recall, 0);
  1966. INIT_LIST_HEAD(&clp->cl_idhash);
  1967. INIT_LIST_HEAD(&clp->cl_openowners);
  1968. INIT_LIST_HEAD(&clp->cl_delegations);
  1969. INIT_LIST_HEAD(&clp->cl_lru);
  1970. INIT_LIST_HEAD(&clp->cl_revoked);
  1971. #ifdef CONFIG_NFSD_PNFS
  1972. INIT_LIST_HEAD(&clp->cl_lo_states);
  1973. #endif
  1974. INIT_LIST_HEAD(&clp->async_copies);
  1975. spin_lock_init(&clp->async_lock);
  1976. spin_lock_init(&clp->cl_lock);
  1977. rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
  1978. return clp;
  1979. err_no_callback_wq:
  1980. kfree(clp->cl_ownerstr_hashtbl);
  1981. err_no_hashtbl:
  1982. kfree(clp->cl_name.data);
  1983. err_no_name:
  1984. kmem_cache_free(client_slab, clp);
  1985. return NULL;
  1986. }
  1987. static void __free_client(struct kref *k)
  1988. {
  1989. struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
  1990. struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
  1991. free_svc_cred(&clp->cl_cred);
  1992. destroy_workqueue(clp->cl_callback_wq);
  1993. kfree(clp->cl_ownerstr_hashtbl);
  1994. kfree(clp->cl_name.data);
  1995. kfree(clp->cl_nii_domain.data);
  1996. kfree(clp->cl_nii_name.data);
  1997. idr_destroy(&clp->cl_stateids);
  1998. kfree(clp->cl_ra);
  1999. kmem_cache_free(client_slab, clp);
  2000. }
  2001. static void drop_client(struct nfs4_client *clp)
  2002. {
  2003. kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
  2004. }
  2005. static void
  2006. free_client(struct nfs4_client *clp)
  2007. {
  2008. while (!list_empty(&clp->cl_sessions)) {
  2009. struct nfsd4_session *ses;
  2010. ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
  2011. se_perclnt);
  2012. list_del(&ses->se_perclnt);
  2013. WARN_ON_ONCE(atomic_read(&ses->se_ref));
  2014. free_session(ses);
  2015. }
  2016. rpc_destroy_wait_queue(&clp->cl_cb_waitq);
  2017. if (clp->cl_nfsd_dentry) {
  2018. nfsd_client_rmdir(clp->cl_nfsd_dentry);
  2019. clp->cl_nfsd_dentry = NULL;
  2020. wake_up_all(&expiry_wq);
  2021. }
  2022. drop_client(clp);
  2023. }
  2024. /* must be called under the client_lock */
  2025. static void
  2026. unhash_client_locked(struct nfs4_client *clp)
  2027. {
  2028. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2029. struct nfsd4_session *ses;
  2030. lockdep_assert_held(&nn->client_lock);
  2031. /* Mark the client as expired! */
  2032. clp->cl_time = 0;
  2033. /* Make it invisible */
  2034. if (!list_empty(&clp->cl_idhash)) {
  2035. list_del_init(&clp->cl_idhash);
  2036. if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
  2037. rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
  2038. else
  2039. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  2040. }
  2041. list_del_init(&clp->cl_lru);
  2042. spin_lock(&clp->cl_lock);
  2043. list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
  2044. list_del_init(&ses->se_hash);
  2045. spin_unlock(&clp->cl_lock);
  2046. }
  2047. static void
  2048. unhash_client(struct nfs4_client *clp)
  2049. {
  2050. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2051. spin_lock(&nn->client_lock);
  2052. unhash_client_locked(clp);
  2053. spin_unlock(&nn->client_lock);
  2054. }
  2055. static __be32 mark_client_expired_locked(struct nfs4_client *clp)
  2056. {
  2057. int users = atomic_read(&clp->cl_rpc_users);
  2058. trace_nfsd_mark_client_expired(clp, users);
  2059. if (users)
  2060. return nfserr_jukebox;
  2061. unhash_client_locked(clp);
  2062. return nfs_ok;
  2063. }
  2064. static void
  2065. __destroy_client(struct nfs4_client *clp)
  2066. {
  2067. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2068. int i;
  2069. struct nfs4_openowner *oo;
  2070. struct nfs4_delegation *dp;
  2071. LIST_HEAD(reaplist);
  2072. spin_lock(&state_lock);
  2073. while (!list_empty(&clp->cl_delegations)) {
  2074. dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
  2075. unhash_delegation_locked(dp, SC_STATUS_CLOSED);
  2076. list_add(&dp->dl_recall_lru, &reaplist);
  2077. }
  2078. spin_unlock(&state_lock);
  2079. while (!list_empty(&reaplist)) {
  2080. dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
  2081. list_del_init(&dp->dl_recall_lru);
  2082. destroy_unhashed_deleg(dp);
  2083. }
  2084. while (!list_empty(&clp->cl_revoked)) {
  2085. dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
  2086. list_del_init(&dp->dl_recall_lru);
  2087. nfs4_put_stid(&dp->dl_stid);
  2088. }
  2089. while (!list_empty(&clp->cl_openowners)) {
  2090. oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
  2091. nfs4_get_stateowner(&oo->oo_owner);
  2092. release_openowner(oo);
  2093. }
  2094. for (i = 0; i < OWNER_HASH_SIZE; i++) {
  2095. struct nfs4_stateowner *so, *tmp;
  2096. list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
  2097. so_strhash) {
  2098. /* Should be no openowners at this point */
  2099. WARN_ON_ONCE(so->so_is_open_owner);
  2100. remove_blocked_locks(lockowner(so));
  2101. }
  2102. }
  2103. nfsd4_return_all_client_layouts(clp);
  2104. nfsd4_shutdown_copy(clp);
  2105. nfsd4_shutdown_callback(clp);
  2106. if (clp->cl_cb_conn.cb_xprt)
  2107. svc_xprt_put(clp->cl_cb_conn.cb_xprt);
  2108. atomic_add_unless(&nn->nfs4_client_count, -1, 0);
  2109. nfsd4_dec_courtesy_client_count(nn, clp);
  2110. free_client(clp);
  2111. wake_up_all(&expiry_wq);
  2112. }
  2113. static void
  2114. destroy_client(struct nfs4_client *clp)
  2115. {
  2116. unhash_client(clp);
  2117. __destroy_client(clp);
  2118. }
  2119. static void inc_reclaim_complete(struct nfs4_client *clp)
  2120. {
  2121. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2122. if (!nn->track_reclaim_completes)
  2123. return;
  2124. if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
  2125. return;
  2126. if (atomic_inc_return(&nn->nr_reclaim_complete) ==
  2127. nn->reclaim_str_hashtbl_size) {
  2128. printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
  2129. clp->net->ns.inum);
  2130. nfsd4_end_grace(nn);
  2131. }
  2132. }
  2133. static void expire_client(struct nfs4_client *clp)
  2134. {
  2135. unhash_client(clp);
  2136. nfsd4_client_record_remove(clp);
  2137. __destroy_client(clp);
  2138. }
  2139. static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
  2140. {
  2141. memcpy(target->cl_verifier.data, source->data,
  2142. sizeof(target->cl_verifier.data));
  2143. }
  2144. static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
  2145. {
  2146. target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
  2147. target->cl_clientid.cl_id = source->cl_clientid.cl_id;
  2148. }
  2149. static int copy_cred(struct svc_cred *target, struct svc_cred *source)
  2150. {
  2151. target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
  2152. target->cr_raw_principal = kstrdup(source->cr_raw_principal,
  2153. GFP_KERNEL);
  2154. target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
  2155. if ((source->cr_principal && !target->cr_principal) ||
  2156. (source->cr_raw_principal && !target->cr_raw_principal) ||
  2157. (source->cr_targ_princ && !target->cr_targ_princ))
  2158. return -ENOMEM;
  2159. target->cr_flavor = source->cr_flavor;
  2160. target->cr_uid = source->cr_uid;
  2161. target->cr_gid = source->cr_gid;
  2162. target->cr_group_info = source->cr_group_info;
  2163. get_group_info(target->cr_group_info);
  2164. target->cr_gss_mech = source->cr_gss_mech;
  2165. if (source->cr_gss_mech)
  2166. gss_mech_get(source->cr_gss_mech);
  2167. return 0;
  2168. }
  2169. static int
  2170. compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
  2171. {
  2172. if (o1->len < o2->len)
  2173. return -1;
  2174. if (o1->len > o2->len)
  2175. return 1;
  2176. return memcmp(o1->data, o2->data, o1->len);
  2177. }
  2178. static int
  2179. same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
  2180. {
  2181. return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
  2182. }
  2183. static int
  2184. same_clid(clientid_t *cl1, clientid_t *cl2)
  2185. {
  2186. return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
  2187. }
  2188. static bool groups_equal(struct group_info *g1, struct group_info *g2)
  2189. {
  2190. int i;
  2191. if (g1->ngroups != g2->ngroups)
  2192. return false;
  2193. for (i=0; i<g1->ngroups; i++)
  2194. if (!gid_eq(g1->gid[i], g2->gid[i]))
  2195. return false;
  2196. return true;
  2197. }
  2198. /*
  2199. * RFC 3530 language requires clid_inuse be returned when the
  2200. * "principal" associated with a requests differs from that previously
  2201. * used. We use uid, gid's, and gss principal string as our best
  2202. * approximation. We also don't want to allow non-gss use of a client
  2203. * established using gss: in theory cr_principal should catch that
  2204. * change, but in practice cr_principal can be null even in the gss case
  2205. * since gssd doesn't always pass down a principal string.
  2206. */
  2207. static bool is_gss_cred(struct svc_cred *cr)
  2208. {
  2209. /* Is cr_flavor one of the gss "pseudoflavors"?: */
  2210. return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
  2211. }
  2212. static bool
  2213. same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
  2214. {
  2215. if ((is_gss_cred(cr1) != is_gss_cred(cr2))
  2216. || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
  2217. || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
  2218. || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
  2219. return false;
  2220. /* XXX: check that cr_targ_princ fields match ? */
  2221. if (cr1->cr_principal == cr2->cr_principal)
  2222. return true;
  2223. if (!cr1->cr_principal || !cr2->cr_principal)
  2224. return false;
  2225. return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
  2226. }
  2227. static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
  2228. {
  2229. struct svc_cred *cr = &rqstp->rq_cred;
  2230. u32 service;
  2231. if (!cr->cr_gss_mech)
  2232. return false;
  2233. service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
  2234. return service == RPC_GSS_SVC_INTEGRITY ||
  2235. service == RPC_GSS_SVC_PRIVACY;
  2236. }
  2237. bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
  2238. {
  2239. struct svc_cred *cr = &rqstp->rq_cred;
  2240. if (!cl->cl_mach_cred)
  2241. return true;
  2242. if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
  2243. return false;
  2244. if (!svc_rqst_integrity_protected(rqstp))
  2245. return false;
  2246. if (cl->cl_cred.cr_raw_principal)
  2247. return 0 == strcmp(cl->cl_cred.cr_raw_principal,
  2248. cr->cr_raw_principal);
  2249. if (!cr->cr_principal)
  2250. return false;
  2251. return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
  2252. }
  2253. static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
  2254. {
  2255. __be32 verf[2];
  2256. /*
  2257. * This is opaque to client, so no need to byte-swap. Use
  2258. * __force to keep sparse happy
  2259. */
  2260. verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
  2261. verf[1] = (__force __be32)nn->clverifier_counter++;
  2262. memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
  2263. }
  2264. static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
  2265. {
  2266. clp->cl_clientid.cl_boot = (u32)nn->boot_time;
  2267. clp->cl_clientid.cl_id = nn->clientid_counter++;
  2268. gen_confirm(clp, nn);
  2269. }
  2270. static struct nfs4_stid *
  2271. find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
  2272. {
  2273. struct nfs4_stid *ret;
  2274. ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
  2275. if (!ret || !ret->sc_type)
  2276. return NULL;
  2277. return ret;
  2278. }
  2279. static struct nfs4_stid *
  2280. find_stateid_by_type(struct nfs4_client *cl, stateid_t *t,
  2281. unsigned short typemask, unsigned short ok_states)
  2282. {
  2283. struct nfs4_stid *s;
  2284. spin_lock(&cl->cl_lock);
  2285. s = find_stateid_locked(cl, t);
  2286. if (s != NULL) {
  2287. if ((s->sc_status & ~ok_states) == 0 &&
  2288. (typemask & s->sc_type))
  2289. refcount_inc(&s->sc_count);
  2290. else
  2291. s = NULL;
  2292. }
  2293. spin_unlock(&cl->cl_lock);
  2294. return s;
  2295. }
  2296. static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
  2297. {
  2298. struct nfsdfs_client *nc;
  2299. nc = get_nfsdfs_client(inode);
  2300. if (!nc)
  2301. return NULL;
  2302. return container_of(nc, struct nfs4_client, cl_nfsdfs);
  2303. }
  2304. static void seq_quote_mem(struct seq_file *m, char *data, int len)
  2305. {
  2306. seq_puts(m, "\"");
  2307. seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\");
  2308. seq_puts(m, "\"");
  2309. }
  2310. static const char *cb_state2str(int state)
  2311. {
  2312. switch (state) {
  2313. case NFSD4_CB_UP:
  2314. return "UP";
  2315. case NFSD4_CB_UNKNOWN:
  2316. return "UNKNOWN";
  2317. case NFSD4_CB_DOWN:
  2318. return "DOWN";
  2319. case NFSD4_CB_FAULT:
  2320. return "FAULT";
  2321. }
  2322. return "UNDEFINED";
  2323. }
  2324. static int client_info_show(struct seq_file *m, void *v)
  2325. {
  2326. struct inode *inode = file_inode(m->file);
  2327. struct nfs4_client *clp;
  2328. u64 clid;
  2329. clp = get_nfsdfs_clp(inode);
  2330. if (!clp)
  2331. return -ENXIO;
  2332. memcpy(&clid, &clp->cl_clientid, sizeof(clid));
  2333. seq_printf(m, "clientid: 0x%llx\n", clid);
  2334. seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
  2335. if (clp->cl_state == NFSD4_COURTESY)
  2336. seq_puts(m, "status: courtesy\n");
  2337. else if (clp->cl_state == NFSD4_EXPIRABLE)
  2338. seq_puts(m, "status: expirable\n");
  2339. else if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
  2340. seq_puts(m, "status: confirmed\n");
  2341. else
  2342. seq_puts(m, "status: unconfirmed\n");
  2343. seq_printf(m, "seconds from last renew: %lld\n",
  2344. ktime_get_boottime_seconds() - clp->cl_time);
  2345. seq_puts(m, "name: ");
  2346. seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
  2347. seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
  2348. if (clp->cl_nii_domain.data) {
  2349. seq_puts(m, "Implementation domain: ");
  2350. seq_quote_mem(m, clp->cl_nii_domain.data,
  2351. clp->cl_nii_domain.len);
  2352. seq_puts(m, "\nImplementation name: ");
  2353. seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
  2354. seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
  2355. clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
  2356. }
  2357. seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state));
  2358. seq_printf(m, "callback address: \"%pISpc\"\n", &clp->cl_cb_conn.cb_addr);
  2359. seq_printf(m, "admin-revoked states: %d\n",
  2360. atomic_read(&clp->cl_admin_revoked));
  2361. drop_client(clp);
  2362. return 0;
  2363. }
  2364. DEFINE_SHOW_ATTRIBUTE(client_info);
  2365. static void *states_start(struct seq_file *s, loff_t *pos)
  2366. __acquires(&clp->cl_lock)
  2367. {
  2368. struct nfs4_client *clp = s->private;
  2369. unsigned long id = *pos;
  2370. void *ret;
  2371. spin_lock(&clp->cl_lock);
  2372. ret = idr_get_next_ul(&clp->cl_stateids, &id);
  2373. *pos = id;
  2374. return ret;
  2375. }
  2376. static void *states_next(struct seq_file *s, void *v, loff_t *pos)
  2377. {
  2378. struct nfs4_client *clp = s->private;
  2379. unsigned long id = *pos;
  2380. void *ret;
  2381. id = *pos;
  2382. id++;
  2383. ret = idr_get_next_ul(&clp->cl_stateids, &id);
  2384. *pos = id;
  2385. return ret;
  2386. }
  2387. static void states_stop(struct seq_file *s, void *v)
  2388. __releases(&clp->cl_lock)
  2389. {
  2390. struct nfs4_client *clp = s->private;
  2391. spin_unlock(&clp->cl_lock);
  2392. }
  2393. static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
  2394. {
  2395. seq_printf(s, "filename: \"%pD2\"", f->nf_file);
  2396. }
  2397. static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
  2398. {
  2399. struct inode *inode = file_inode(f->nf_file);
  2400. seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
  2401. MAJOR(inode->i_sb->s_dev),
  2402. MINOR(inode->i_sb->s_dev),
  2403. inode->i_ino);
  2404. }
  2405. static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
  2406. {
  2407. seq_puts(s, "owner: ");
  2408. seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
  2409. }
  2410. static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
  2411. {
  2412. seq_printf(s, "0x%.8x", stid->si_generation);
  2413. seq_printf(s, "%12phN", &stid->si_opaque);
  2414. }
  2415. static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
  2416. {
  2417. struct nfs4_ol_stateid *ols;
  2418. struct nfs4_file *nf;
  2419. struct nfsd_file *file;
  2420. struct nfs4_stateowner *oo;
  2421. unsigned int access, deny;
  2422. ols = openlockstateid(st);
  2423. oo = ols->st_stateowner;
  2424. nf = st->sc_file;
  2425. seq_puts(s, "- ");
  2426. nfs4_show_stateid(s, &st->sc_stateid);
  2427. seq_puts(s, ": { type: open, ");
  2428. access = bmap_to_share_mode(ols->st_access_bmap);
  2429. deny = bmap_to_share_mode(ols->st_deny_bmap);
  2430. seq_printf(s, "access: %s%s, ",
  2431. access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
  2432. access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
  2433. seq_printf(s, "deny: %s%s, ",
  2434. deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
  2435. deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
  2436. if (nf) {
  2437. spin_lock(&nf->fi_lock);
  2438. file = find_any_file_locked(nf);
  2439. if (file) {
  2440. nfs4_show_superblock(s, file);
  2441. seq_puts(s, ", ");
  2442. nfs4_show_fname(s, file);
  2443. seq_puts(s, ", ");
  2444. }
  2445. spin_unlock(&nf->fi_lock);
  2446. } else
  2447. seq_puts(s, "closed, ");
  2448. nfs4_show_owner(s, oo);
  2449. if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
  2450. seq_puts(s, ", admin-revoked");
  2451. seq_puts(s, " }\n");
  2452. return 0;
  2453. }
  2454. static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
  2455. {
  2456. struct nfs4_ol_stateid *ols;
  2457. struct nfs4_file *nf;
  2458. struct nfsd_file *file;
  2459. struct nfs4_stateowner *oo;
  2460. ols = openlockstateid(st);
  2461. oo = ols->st_stateowner;
  2462. nf = st->sc_file;
  2463. seq_puts(s, "- ");
  2464. nfs4_show_stateid(s, &st->sc_stateid);
  2465. seq_puts(s, ": { type: lock, ");
  2466. spin_lock(&nf->fi_lock);
  2467. file = find_any_file_locked(nf);
  2468. if (file) {
  2469. /*
  2470. * Note: a lock stateid isn't really the same thing as a lock,
  2471. * it's the locking state held by one owner on a file, and there
  2472. * may be multiple (or no) lock ranges associated with it.
  2473. * (Same for the matter is true of open stateids.)
  2474. */
  2475. nfs4_show_superblock(s, file);
  2476. /* XXX: open stateid? */
  2477. seq_puts(s, ", ");
  2478. nfs4_show_fname(s, file);
  2479. seq_puts(s, ", ");
  2480. }
  2481. nfs4_show_owner(s, oo);
  2482. if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
  2483. seq_puts(s, ", admin-revoked");
  2484. seq_puts(s, " }\n");
  2485. spin_unlock(&nf->fi_lock);
  2486. return 0;
  2487. }
  2488. static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
  2489. {
  2490. struct nfs4_delegation *ds;
  2491. struct nfs4_file *nf;
  2492. struct nfsd_file *file;
  2493. ds = delegstateid(st);
  2494. nf = st->sc_file;
  2495. seq_puts(s, "- ");
  2496. nfs4_show_stateid(s, &st->sc_stateid);
  2497. seq_puts(s, ": { type: deleg, ");
  2498. seq_printf(s, "access: %s",
  2499. ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
  2500. /* XXX: lease time, whether it's being recalled. */
  2501. spin_lock(&nf->fi_lock);
  2502. file = nf->fi_deleg_file;
  2503. if (file) {
  2504. seq_puts(s, ", ");
  2505. nfs4_show_superblock(s, file);
  2506. seq_puts(s, ", ");
  2507. nfs4_show_fname(s, file);
  2508. }
  2509. spin_unlock(&nf->fi_lock);
  2510. if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
  2511. seq_puts(s, ", admin-revoked");
  2512. seq_puts(s, " }\n");
  2513. return 0;
  2514. }
  2515. static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
  2516. {
  2517. struct nfs4_layout_stateid *ls;
  2518. struct nfsd_file *file;
  2519. ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
  2520. seq_puts(s, "- ");
  2521. nfs4_show_stateid(s, &st->sc_stateid);
  2522. seq_puts(s, ": { type: layout");
  2523. /* XXX: What else would be useful? */
  2524. spin_lock(&ls->ls_stid.sc_file->fi_lock);
  2525. file = ls->ls_file;
  2526. if (file) {
  2527. seq_puts(s, ", ");
  2528. nfs4_show_superblock(s, file);
  2529. seq_puts(s, ", ");
  2530. nfs4_show_fname(s, file);
  2531. }
  2532. spin_unlock(&ls->ls_stid.sc_file->fi_lock);
  2533. if (st->sc_status & SC_STATUS_ADMIN_REVOKED)
  2534. seq_puts(s, ", admin-revoked");
  2535. seq_puts(s, " }\n");
  2536. return 0;
  2537. }
  2538. static int states_show(struct seq_file *s, void *v)
  2539. {
  2540. struct nfs4_stid *st = v;
  2541. switch (st->sc_type) {
  2542. case SC_TYPE_OPEN:
  2543. return nfs4_show_open(s, st);
  2544. case SC_TYPE_LOCK:
  2545. return nfs4_show_lock(s, st);
  2546. case SC_TYPE_DELEG:
  2547. return nfs4_show_deleg(s, st);
  2548. case SC_TYPE_LAYOUT:
  2549. return nfs4_show_layout(s, st);
  2550. default:
  2551. return 0; /* XXX: or SEQ_SKIP? */
  2552. }
  2553. /* XXX: copy stateids? */
  2554. }
  2555. static struct seq_operations states_seq_ops = {
  2556. .start = states_start,
  2557. .next = states_next,
  2558. .stop = states_stop,
  2559. .show = states_show
  2560. };
  2561. static int client_states_open(struct inode *inode, struct file *file)
  2562. {
  2563. struct seq_file *s;
  2564. struct nfs4_client *clp;
  2565. int ret;
  2566. clp = get_nfsdfs_clp(inode);
  2567. if (!clp)
  2568. return -ENXIO;
  2569. ret = seq_open(file, &states_seq_ops);
  2570. if (ret)
  2571. return ret;
  2572. s = file->private_data;
  2573. s->private = clp;
  2574. return 0;
  2575. }
  2576. static int client_opens_release(struct inode *inode, struct file *file)
  2577. {
  2578. struct seq_file *m = file->private_data;
  2579. struct nfs4_client *clp = m->private;
  2580. /* XXX: alternatively, we could get/drop in seq start/stop */
  2581. drop_client(clp);
  2582. return seq_release(inode, file);
  2583. }
  2584. static const struct file_operations client_states_fops = {
  2585. .open = client_states_open,
  2586. .read = seq_read,
  2587. .llseek = seq_lseek,
  2588. .release = client_opens_release,
  2589. };
  2590. /*
  2591. * Normally we refuse to destroy clients that are in use, but here the
  2592. * administrator is telling us to just do it. We also want to wait
  2593. * so the caller has a guarantee that the client's locks are gone by
  2594. * the time the write returns:
  2595. */
  2596. static void force_expire_client(struct nfs4_client *clp)
  2597. {
  2598. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2599. bool already_expired;
  2600. trace_nfsd_clid_admin_expired(&clp->cl_clientid);
  2601. spin_lock(&nn->client_lock);
  2602. clp->cl_time = 0;
  2603. spin_unlock(&nn->client_lock);
  2604. wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
  2605. spin_lock(&nn->client_lock);
  2606. already_expired = list_empty(&clp->cl_lru);
  2607. if (!already_expired)
  2608. unhash_client_locked(clp);
  2609. spin_unlock(&nn->client_lock);
  2610. if (!already_expired)
  2611. expire_client(clp);
  2612. else
  2613. wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
  2614. }
  2615. static ssize_t client_ctl_write(struct file *file, const char __user *buf,
  2616. size_t size, loff_t *pos)
  2617. {
  2618. char *data;
  2619. struct nfs4_client *clp;
  2620. data = simple_transaction_get(file, buf, size);
  2621. if (IS_ERR(data))
  2622. return PTR_ERR(data);
  2623. if (size != 7 || 0 != memcmp(data, "expire\n", 7))
  2624. return -EINVAL;
  2625. clp = get_nfsdfs_clp(file_inode(file));
  2626. if (!clp)
  2627. return -ENXIO;
  2628. force_expire_client(clp);
  2629. drop_client(clp);
  2630. return 7;
  2631. }
  2632. static const struct file_operations client_ctl_fops = {
  2633. .write = client_ctl_write,
  2634. .release = simple_transaction_release,
  2635. };
  2636. static const struct tree_descr client_files[] = {
  2637. [0] = {"info", &client_info_fops, S_IRUSR},
  2638. [1] = {"states", &client_states_fops, S_IRUSR},
  2639. [2] = {"ctl", &client_ctl_fops, S_IWUSR},
  2640. [3] = {""},
  2641. };
  2642. static int
  2643. nfsd4_cb_recall_any_done(struct nfsd4_callback *cb,
  2644. struct rpc_task *task)
  2645. {
  2646. trace_nfsd_cb_recall_any_done(cb, task);
  2647. switch (task->tk_status) {
  2648. case -NFS4ERR_DELAY:
  2649. rpc_delay(task, 2 * HZ);
  2650. return 0;
  2651. default:
  2652. return 1;
  2653. }
  2654. }
  2655. static void
  2656. nfsd4_cb_recall_any_release(struct nfsd4_callback *cb)
  2657. {
  2658. struct nfs4_client *clp = cb->cb_clp;
  2659. clear_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags);
  2660. drop_client(clp);
  2661. }
  2662. static int
  2663. nfsd4_cb_getattr_done(struct nfsd4_callback *cb, struct rpc_task *task)
  2664. {
  2665. struct nfs4_cb_fattr *ncf =
  2666. container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
  2667. struct nfs4_delegation *dp =
  2668. container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
  2669. trace_nfsd_cb_getattr_done(&dp->dl_stid.sc_stateid, task);
  2670. ncf->ncf_cb_status = task->tk_status;
  2671. switch (task->tk_status) {
  2672. case -NFS4ERR_DELAY:
  2673. rpc_delay(task, 2 * HZ);
  2674. return 0;
  2675. default:
  2676. return 1;
  2677. }
  2678. }
  2679. static void
  2680. nfsd4_cb_getattr_release(struct nfsd4_callback *cb)
  2681. {
  2682. struct nfs4_cb_fattr *ncf =
  2683. container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
  2684. struct nfs4_delegation *dp =
  2685. container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
  2686. clear_and_wake_up_bit(CB_GETATTR_BUSY, &ncf->ncf_cb_flags);
  2687. nfs4_put_stid(&dp->dl_stid);
  2688. }
  2689. static const struct nfsd4_callback_ops nfsd4_cb_recall_any_ops = {
  2690. .done = nfsd4_cb_recall_any_done,
  2691. .release = nfsd4_cb_recall_any_release,
  2692. .opcode = OP_CB_RECALL_ANY,
  2693. };
  2694. static const struct nfsd4_callback_ops nfsd4_cb_getattr_ops = {
  2695. .done = nfsd4_cb_getattr_done,
  2696. .release = nfsd4_cb_getattr_release,
  2697. .opcode = OP_CB_GETATTR,
  2698. };
  2699. static void nfs4_cb_getattr(struct nfs4_cb_fattr *ncf)
  2700. {
  2701. struct nfs4_delegation *dp =
  2702. container_of(ncf, struct nfs4_delegation, dl_cb_fattr);
  2703. if (test_and_set_bit(CB_GETATTR_BUSY, &ncf->ncf_cb_flags))
  2704. return;
  2705. /* set to proper status when nfsd4_cb_getattr_done runs */
  2706. ncf->ncf_cb_status = NFS4ERR_IO;
  2707. refcount_inc(&dp->dl_stid.sc_count);
  2708. nfsd4_run_cb(&ncf->ncf_getattr);
  2709. }
  2710. static struct nfs4_client *create_client(struct xdr_netobj name,
  2711. struct svc_rqst *rqstp, nfs4_verifier *verf)
  2712. {
  2713. struct nfs4_client *clp;
  2714. struct sockaddr *sa = svc_addr(rqstp);
  2715. int ret;
  2716. struct net *net = SVC_NET(rqstp);
  2717. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  2718. struct dentry *dentries[ARRAY_SIZE(client_files)];
  2719. clp = alloc_client(name, nn);
  2720. if (clp == NULL)
  2721. return NULL;
  2722. ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
  2723. if (ret) {
  2724. free_client(clp);
  2725. return NULL;
  2726. }
  2727. gen_clid(clp, nn);
  2728. kref_init(&clp->cl_nfsdfs.cl_ref);
  2729. nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
  2730. clp->cl_time = ktime_get_boottime_seconds();
  2731. clear_bit(0, &clp->cl_cb_slot_busy);
  2732. copy_verf(clp, verf);
  2733. memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
  2734. clp->cl_cb_session = NULL;
  2735. clp->net = net;
  2736. clp->cl_nfsd_dentry = nfsd_client_mkdir(
  2737. nn, &clp->cl_nfsdfs,
  2738. clp->cl_clientid.cl_id - nn->clientid_base,
  2739. client_files, dentries);
  2740. clp->cl_nfsd_info_dentry = dentries[0];
  2741. if (!clp->cl_nfsd_dentry) {
  2742. free_client(clp);
  2743. return NULL;
  2744. }
  2745. clp->cl_ra = kzalloc(sizeof(*clp->cl_ra), GFP_KERNEL);
  2746. if (!clp->cl_ra) {
  2747. free_client(clp);
  2748. return NULL;
  2749. }
  2750. clp->cl_ra_time = 0;
  2751. nfsd4_init_cb(&clp->cl_ra->ra_cb, clp, &nfsd4_cb_recall_any_ops,
  2752. NFSPROC4_CLNT_CB_RECALL_ANY);
  2753. return clp;
  2754. }
  2755. static void
  2756. add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
  2757. {
  2758. struct rb_node **new = &(root->rb_node), *parent = NULL;
  2759. struct nfs4_client *clp;
  2760. while (*new) {
  2761. clp = rb_entry(*new, struct nfs4_client, cl_namenode);
  2762. parent = *new;
  2763. if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
  2764. new = &((*new)->rb_left);
  2765. else
  2766. new = &((*new)->rb_right);
  2767. }
  2768. rb_link_node(&new_clp->cl_namenode, parent, new);
  2769. rb_insert_color(&new_clp->cl_namenode, root);
  2770. }
  2771. static struct nfs4_client *
  2772. find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
  2773. {
  2774. int cmp;
  2775. struct rb_node *node = root->rb_node;
  2776. struct nfs4_client *clp;
  2777. while (node) {
  2778. clp = rb_entry(node, struct nfs4_client, cl_namenode);
  2779. cmp = compare_blob(&clp->cl_name, name);
  2780. if (cmp > 0)
  2781. node = node->rb_left;
  2782. else if (cmp < 0)
  2783. node = node->rb_right;
  2784. else
  2785. return clp;
  2786. }
  2787. return NULL;
  2788. }
  2789. static void
  2790. add_to_unconfirmed(struct nfs4_client *clp)
  2791. {
  2792. unsigned int idhashval;
  2793. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2794. lockdep_assert_held(&nn->client_lock);
  2795. clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  2796. add_clp_to_name_tree(clp, &nn->unconf_name_tree);
  2797. idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  2798. list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
  2799. renew_client_locked(clp);
  2800. }
  2801. static void
  2802. move_to_confirmed(struct nfs4_client *clp)
  2803. {
  2804. unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
  2805. struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
  2806. lockdep_assert_held(&nn->client_lock);
  2807. list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
  2808. rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
  2809. add_clp_to_name_tree(clp, &nn->conf_name_tree);
  2810. set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
  2811. trace_nfsd_clid_confirmed(&clp->cl_clientid);
  2812. renew_client_locked(clp);
  2813. }
  2814. static struct nfs4_client *
  2815. find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
  2816. {
  2817. struct nfs4_client *clp;
  2818. unsigned int idhashval = clientid_hashval(clid->cl_id);
  2819. list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
  2820. if (same_clid(&clp->cl_clientid, clid)) {
  2821. if ((bool)clp->cl_minorversion != sessions)
  2822. return NULL;
  2823. renew_client_locked(clp);
  2824. return clp;
  2825. }
  2826. }
  2827. return NULL;
  2828. }
  2829. static struct nfs4_client *
  2830. find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  2831. {
  2832. struct list_head *tbl = nn->conf_id_hashtbl;
  2833. lockdep_assert_held(&nn->client_lock);
  2834. return find_client_in_id_table(tbl, clid, sessions);
  2835. }
  2836. static struct nfs4_client *
  2837. find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
  2838. {
  2839. struct list_head *tbl = nn->unconf_id_hashtbl;
  2840. lockdep_assert_held(&nn->client_lock);
  2841. return find_client_in_id_table(tbl, clid, sessions);
  2842. }
  2843. static bool clp_used_exchangeid(struct nfs4_client *clp)
  2844. {
  2845. return clp->cl_exchange_flags != 0;
  2846. }
  2847. static struct nfs4_client *
  2848. find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  2849. {
  2850. lockdep_assert_held(&nn->client_lock);
  2851. return find_clp_in_name_tree(name, &nn->conf_name_tree);
  2852. }
  2853. static struct nfs4_client *
  2854. find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
  2855. {
  2856. lockdep_assert_held(&nn->client_lock);
  2857. return find_clp_in_name_tree(name, &nn->unconf_name_tree);
  2858. }
  2859. static void
  2860. gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
  2861. {
  2862. struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
  2863. struct sockaddr *sa = svc_addr(rqstp);
  2864. u32 scopeid = rpc_get_scope_id(sa);
  2865. unsigned short expected_family;
  2866. /* Currently, we only support tcp and tcp6 for the callback channel */
  2867. if (se->se_callback_netid_len == 3 &&
  2868. !memcmp(se->se_callback_netid_val, "tcp", 3))
  2869. expected_family = AF_INET;
  2870. else if (se->se_callback_netid_len == 4 &&
  2871. !memcmp(se->se_callback_netid_val, "tcp6", 4))
  2872. expected_family = AF_INET6;
  2873. else
  2874. goto out_err;
  2875. conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
  2876. se->se_callback_addr_len,
  2877. (struct sockaddr *)&conn->cb_addr,
  2878. sizeof(conn->cb_addr));
  2879. if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
  2880. goto out_err;
  2881. if (conn->cb_addr.ss_family == AF_INET6)
  2882. ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
  2883. conn->cb_prog = se->se_callback_prog;
  2884. conn->cb_ident = se->se_callback_ident;
  2885. memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
  2886. trace_nfsd_cb_args(clp, conn);
  2887. return;
  2888. out_err:
  2889. conn->cb_addr.ss_family = AF_UNSPEC;
  2890. conn->cb_addrlen = 0;
  2891. trace_nfsd_cb_nodelegs(clp);
  2892. return;
  2893. }
  2894. /*
  2895. * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
  2896. */
  2897. static void
  2898. nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
  2899. {
  2900. struct xdr_buf *buf = resp->xdr->buf;
  2901. struct nfsd4_slot *slot = resp->cstate.slot;
  2902. unsigned int base;
  2903. dprintk("--> %s slot %p\n", __func__, slot);
  2904. slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
  2905. slot->sl_opcnt = resp->opcnt;
  2906. slot->sl_status = resp->cstate.status;
  2907. free_svc_cred(&slot->sl_cred);
  2908. copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
  2909. if (!nfsd4_cache_this(resp)) {
  2910. slot->sl_flags &= ~NFSD4_SLOT_CACHED;
  2911. return;
  2912. }
  2913. slot->sl_flags |= NFSD4_SLOT_CACHED;
  2914. base = resp->cstate.data_offset;
  2915. slot->sl_datalen = buf->len - base;
  2916. if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
  2917. WARN(1, "%s: sessions DRC could not cache compound\n",
  2918. __func__);
  2919. return;
  2920. }
  2921. /*
  2922. * Encode the replay sequence operation from the slot values.
  2923. * If cachethis is FALSE encode the uncached rep error on the next
  2924. * operation which sets resp->p and increments resp->opcnt for
  2925. * nfs4svc_encode_compoundres.
  2926. *
  2927. */
  2928. static __be32
  2929. nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
  2930. struct nfsd4_compoundres *resp)
  2931. {
  2932. struct nfsd4_op *op;
  2933. struct nfsd4_slot *slot = resp->cstate.slot;
  2934. /* Encode the replayed sequence operation */
  2935. op = &args->ops[resp->opcnt - 1];
  2936. nfsd4_encode_operation(resp, op);
  2937. if (slot->sl_flags & NFSD4_SLOT_CACHED)
  2938. return op->status;
  2939. if (args->opcnt == 1) {
  2940. /*
  2941. * The original operation wasn't a solo sequence--we
  2942. * always cache those--so this retry must not match the
  2943. * original:
  2944. */
  2945. op->status = nfserr_seq_false_retry;
  2946. } else {
  2947. op = &args->ops[resp->opcnt++];
  2948. op->status = nfserr_retry_uncached_rep;
  2949. nfsd4_encode_operation(resp, op);
  2950. }
  2951. return op->status;
  2952. }
  2953. /*
  2954. * The sequence operation is not cached because we can use the slot and
  2955. * session values.
  2956. */
  2957. static __be32
  2958. nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
  2959. struct nfsd4_sequence *seq)
  2960. {
  2961. struct nfsd4_slot *slot = resp->cstate.slot;
  2962. struct xdr_stream *xdr = resp->xdr;
  2963. __be32 *p;
  2964. __be32 status;
  2965. dprintk("--> %s slot %p\n", __func__, slot);
  2966. status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
  2967. if (status)
  2968. return status;
  2969. p = xdr_reserve_space(xdr, slot->sl_datalen);
  2970. if (!p) {
  2971. WARN_ON_ONCE(1);
  2972. return nfserr_serverfault;
  2973. }
  2974. xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
  2975. xdr_commit_encode(xdr);
  2976. resp->opcnt = slot->sl_opcnt;
  2977. return slot->sl_status;
  2978. }
  2979. /*
  2980. * Set the exchange_id flags returned by the server.
  2981. */
  2982. static void
  2983. nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
  2984. {
  2985. #ifdef CONFIG_NFSD_PNFS
  2986. new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
  2987. #else
  2988. new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
  2989. #endif
  2990. /* Referrals are supported, Migration is not. */
  2991. new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
  2992. /* set the wire flags to return to client. */
  2993. clid->flags = new->cl_exchange_flags;
  2994. }
  2995. static bool client_has_openowners(struct nfs4_client *clp)
  2996. {
  2997. struct nfs4_openowner *oo;
  2998. list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
  2999. if (!list_empty(&oo->oo_owner.so_stateids))
  3000. return true;
  3001. }
  3002. return false;
  3003. }
  3004. static bool client_has_state(struct nfs4_client *clp)
  3005. {
  3006. return client_has_openowners(clp)
  3007. #ifdef CONFIG_NFSD_PNFS
  3008. || !list_empty(&clp->cl_lo_states)
  3009. #endif
  3010. || !list_empty(&clp->cl_delegations)
  3011. || !list_empty(&clp->cl_sessions)
  3012. || !list_empty(&clp->async_copies);
  3013. }
  3014. static __be32 copy_impl_id(struct nfs4_client *clp,
  3015. struct nfsd4_exchange_id *exid)
  3016. {
  3017. if (!exid->nii_domain.data)
  3018. return 0;
  3019. xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
  3020. if (!clp->cl_nii_domain.data)
  3021. return nfserr_jukebox;
  3022. xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
  3023. if (!clp->cl_nii_name.data)
  3024. return nfserr_jukebox;
  3025. clp->cl_nii_time = exid->nii_time;
  3026. return 0;
  3027. }
  3028. __be32
  3029. nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3030. union nfsd4_op_u *u)
  3031. {
  3032. struct nfsd4_exchange_id *exid = &u->exchange_id;
  3033. struct nfs4_client *conf, *new;
  3034. struct nfs4_client *unconf = NULL;
  3035. __be32 status;
  3036. char addr_str[INET6_ADDRSTRLEN];
  3037. nfs4_verifier verf = exid->verifier;
  3038. struct sockaddr *sa = svc_addr(rqstp);
  3039. bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
  3040. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3041. rpc_ntop(sa, addr_str, sizeof(addr_str));
  3042. dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
  3043. "ip_addr=%s flags %x, spa_how %u\n",
  3044. __func__, rqstp, exid, exid->clname.len, exid->clname.data,
  3045. addr_str, exid->flags, exid->spa_how);
  3046. if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
  3047. return nfserr_inval;
  3048. new = create_client(exid->clname, rqstp, &verf);
  3049. if (new == NULL)
  3050. return nfserr_jukebox;
  3051. status = copy_impl_id(new, exid);
  3052. if (status)
  3053. goto out_nolock;
  3054. switch (exid->spa_how) {
  3055. case SP4_MACH_CRED:
  3056. exid->spo_must_enforce[0] = 0;
  3057. exid->spo_must_enforce[1] = (
  3058. 1 << (OP_BIND_CONN_TO_SESSION - 32) |
  3059. 1 << (OP_EXCHANGE_ID - 32) |
  3060. 1 << (OP_CREATE_SESSION - 32) |
  3061. 1 << (OP_DESTROY_SESSION - 32) |
  3062. 1 << (OP_DESTROY_CLIENTID - 32));
  3063. exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
  3064. 1 << (OP_OPEN_DOWNGRADE) |
  3065. 1 << (OP_LOCKU) |
  3066. 1 << (OP_DELEGRETURN));
  3067. exid->spo_must_allow[1] &= (
  3068. 1 << (OP_TEST_STATEID - 32) |
  3069. 1 << (OP_FREE_STATEID - 32));
  3070. if (!svc_rqst_integrity_protected(rqstp)) {
  3071. status = nfserr_inval;
  3072. goto out_nolock;
  3073. }
  3074. /*
  3075. * Sometimes userspace doesn't give us a principal.
  3076. * Which is a bug, really. Anyway, we can't enforce
  3077. * MACH_CRED in that case, better to give up now:
  3078. */
  3079. if (!new->cl_cred.cr_principal &&
  3080. !new->cl_cred.cr_raw_principal) {
  3081. status = nfserr_serverfault;
  3082. goto out_nolock;
  3083. }
  3084. new->cl_mach_cred = true;
  3085. break;
  3086. case SP4_NONE:
  3087. break;
  3088. default: /* checked by xdr code */
  3089. WARN_ON_ONCE(1);
  3090. fallthrough;
  3091. case SP4_SSV:
  3092. status = nfserr_encr_alg_unsupp;
  3093. goto out_nolock;
  3094. }
  3095. /* Cases below refer to rfc 5661 section 18.35.4: */
  3096. spin_lock(&nn->client_lock);
  3097. conf = find_confirmed_client_by_name(&exid->clname, nn);
  3098. if (conf) {
  3099. bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
  3100. bool verfs_match = same_verf(&verf, &conf->cl_verifier);
  3101. if (update) {
  3102. if (!clp_used_exchangeid(conf)) { /* buggy client */
  3103. status = nfserr_inval;
  3104. goto out;
  3105. }
  3106. if (!nfsd4_mach_creds_match(conf, rqstp)) {
  3107. status = nfserr_wrong_cred;
  3108. goto out;
  3109. }
  3110. if (!creds_match) { /* case 9 */
  3111. status = nfserr_perm;
  3112. goto out;
  3113. }
  3114. if (!verfs_match) { /* case 8 */
  3115. status = nfserr_not_same;
  3116. goto out;
  3117. }
  3118. /* case 6 */
  3119. exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
  3120. trace_nfsd_clid_confirmed_r(conf);
  3121. goto out_copy;
  3122. }
  3123. if (!creds_match) { /* case 3 */
  3124. if (client_has_state(conf)) {
  3125. status = nfserr_clid_inuse;
  3126. trace_nfsd_clid_cred_mismatch(conf, rqstp);
  3127. goto out;
  3128. }
  3129. goto out_new;
  3130. }
  3131. if (verfs_match) { /* case 2 */
  3132. conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
  3133. trace_nfsd_clid_confirmed_r(conf);
  3134. goto out_copy;
  3135. }
  3136. /* case 5, client reboot */
  3137. trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf);
  3138. conf = NULL;
  3139. goto out_new;
  3140. }
  3141. if (update) { /* case 7 */
  3142. status = nfserr_noent;
  3143. goto out;
  3144. }
  3145. unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
  3146. if (unconf) /* case 4, possible retry or client restart */
  3147. unhash_client_locked(unconf);
  3148. /* case 1, new owner ID */
  3149. trace_nfsd_clid_fresh(new);
  3150. out_new:
  3151. if (conf) {
  3152. status = mark_client_expired_locked(conf);
  3153. if (status)
  3154. goto out;
  3155. trace_nfsd_clid_replaced(&conf->cl_clientid);
  3156. }
  3157. new->cl_minorversion = cstate->minorversion;
  3158. new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
  3159. new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
  3160. /* Contrived initial CREATE_SESSION response */
  3161. new->cl_cs_slot.sl_status = nfserr_seq_misordered;
  3162. add_to_unconfirmed(new);
  3163. swap(new, conf);
  3164. out_copy:
  3165. exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
  3166. exid->clientid.cl_id = conf->cl_clientid.cl_id;
  3167. exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
  3168. nfsd4_set_ex_flags(conf, exid);
  3169. dprintk("nfsd4_exchange_id seqid %d flags %x\n",
  3170. conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
  3171. status = nfs_ok;
  3172. out:
  3173. spin_unlock(&nn->client_lock);
  3174. out_nolock:
  3175. if (new)
  3176. expire_client(new);
  3177. if (unconf) {
  3178. trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
  3179. expire_client(unconf);
  3180. }
  3181. return status;
  3182. }
  3183. static __be32 check_slot_seqid(u32 seqid, u32 slot_seqid, bool slot_inuse)
  3184. {
  3185. /* The slot is in use, and no response has been sent. */
  3186. if (slot_inuse) {
  3187. if (seqid == slot_seqid)
  3188. return nfserr_jukebox;
  3189. else
  3190. return nfserr_seq_misordered;
  3191. }
  3192. /* Note unsigned 32-bit arithmetic handles wraparound: */
  3193. if (likely(seqid == slot_seqid + 1))
  3194. return nfs_ok;
  3195. if (seqid == slot_seqid)
  3196. return nfserr_replay_cache;
  3197. return nfserr_seq_misordered;
  3198. }
  3199. /*
  3200. * Cache the create session result into the create session single DRC
  3201. * slot cache by saving the xdr structure. sl_seqid has been set.
  3202. * Do this for solo or embedded create session operations.
  3203. */
  3204. static void
  3205. nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
  3206. struct nfsd4_clid_slot *slot, __be32 nfserr)
  3207. {
  3208. slot->sl_status = nfserr;
  3209. memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
  3210. }
  3211. static __be32
  3212. nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
  3213. struct nfsd4_clid_slot *slot)
  3214. {
  3215. memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
  3216. return slot->sl_status;
  3217. }
  3218. #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
  3219. 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
  3220. 1 + /* MIN tag is length with zero, only length */ \
  3221. 3 + /* version, opcount, opcode */ \
  3222. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  3223. /* seqid, slotID, slotID, cache */ \
  3224. 4 ) * sizeof(__be32))
  3225. #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
  3226. 2 + /* verifier: AUTH_NULL, length 0 */\
  3227. 1 + /* status */ \
  3228. 1 + /* MIN tag is length with zero, only length */ \
  3229. 3 + /* opcount, opcode, opstatus*/ \
  3230. XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
  3231. /* seqid, slotID, slotID, slotID, status */ \
  3232. 5 ) * sizeof(__be32))
  3233. static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
  3234. {
  3235. u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
  3236. if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
  3237. return nfserr_toosmall;
  3238. if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
  3239. return nfserr_toosmall;
  3240. ca->headerpadsz = 0;
  3241. ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
  3242. ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
  3243. ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
  3244. ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
  3245. NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
  3246. ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
  3247. /*
  3248. * Note decreasing slot size below client's request may make it
  3249. * difficult for client to function correctly, whereas
  3250. * decreasing the number of slots will (just?) affect
  3251. * performance. When short on memory we therefore prefer to
  3252. * decrease number of slots instead of their size. Clients that
  3253. * request larger slots than they need will get poor results:
  3254. * Note that we always allow at least one slot, because our
  3255. * accounting is soft and provides no guarantees either way.
  3256. */
  3257. ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
  3258. return nfs_ok;
  3259. }
  3260. /*
  3261. * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
  3262. * These are based on similar macros in linux/sunrpc/msg_prot.h .
  3263. */
  3264. #define RPC_MAX_HEADER_WITH_AUTH_SYS \
  3265. (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
  3266. #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
  3267. (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
  3268. #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
  3269. RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
  3270. #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
  3271. RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
  3272. sizeof(__be32))
  3273. static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
  3274. {
  3275. ca->headerpadsz = 0;
  3276. if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
  3277. return nfserr_toosmall;
  3278. if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
  3279. return nfserr_toosmall;
  3280. ca->maxresp_cached = 0;
  3281. if (ca->maxops < 2)
  3282. return nfserr_toosmall;
  3283. return nfs_ok;
  3284. }
  3285. static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
  3286. {
  3287. switch (cbs->flavor) {
  3288. case RPC_AUTH_NULL:
  3289. case RPC_AUTH_UNIX:
  3290. return nfs_ok;
  3291. default:
  3292. /*
  3293. * GSS case: the spec doesn't allow us to return this
  3294. * error. But it also doesn't allow us not to support
  3295. * GSS.
  3296. * I'd rather this fail hard than return some error the
  3297. * client might think it can already handle:
  3298. */
  3299. return nfserr_encr_alg_unsupp;
  3300. }
  3301. }
  3302. __be32
  3303. nfsd4_create_session(struct svc_rqst *rqstp,
  3304. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  3305. {
  3306. struct nfsd4_create_session *cr_ses = &u->create_session;
  3307. struct sockaddr *sa = svc_addr(rqstp);
  3308. struct nfs4_client *conf, *unconf;
  3309. struct nfsd4_clid_slot *cs_slot;
  3310. struct nfs4_client *old = NULL;
  3311. struct nfsd4_session *new;
  3312. struct nfsd4_conn *conn;
  3313. __be32 status = 0;
  3314. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3315. if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
  3316. return nfserr_inval;
  3317. status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
  3318. if (status)
  3319. return status;
  3320. status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
  3321. if (status)
  3322. return status;
  3323. status = check_backchannel_attrs(&cr_ses->back_channel);
  3324. if (status)
  3325. goto out_release_drc_mem;
  3326. status = nfserr_jukebox;
  3327. new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
  3328. if (!new)
  3329. goto out_release_drc_mem;
  3330. conn = alloc_conn_from_crses(rqstp, cr_ses);
  3331. if (!conn)
  3332. goto out_free_session;
  3333. spin_lock(&nn->client_lock);
  3334. /* RFC 8881 Section 18.36.4 Phase 1: Client record look-up. */
  3335. unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
  3336. conf = find_confirmed_client(&cr_ses->clientid, true, nn);
  3337. if (!conf && !unconf) {
  3338. status = nfserr_stale_clientid;
  3339. goto out_free_conn;
  3340. }
  3341. /* RFC 8881 Section 18.36.4 Phase 2: Sequence ID processing. */
  3342. if (conf) {
  3343. cs_slot = &conf->cl_cs_slot;
  3344. trace_nfsd_slot_seqid_conf(conf, cr_ses);
  3345. } else {
  3346. cs_slot = &unconf->cl_cs_slot;
  3347. trace_nfsd_slot_seqid_unconf(unconf, cr_ses);
  3348. }
  3349. status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
  3350. switch (status) {
  3351. case nfs_ok:
  3352. cs_slot->sl_seqid++;
  3353. cr_ses->seqid = cs_slot->sl_seqid;
  3354. break;
  3355. case nfserr_replay_cache:
  3356. status = nfsd4_replay_create_session(cr_ses, cs_slot);
  3357. fallthrough;
  3358. case nfserr_jukebox:
  3359. /* The server MUST NOT cache NFS4ERR_DELAY */
  3360. goto out_free_conn;
  3361. default:
  3362. goto out_cache_error;
  3363. }
  3364. /* RFC 8881 Section 18.36.4 Phase 3: Client ID confirmation. */
  3365. if (conf) {
  3366. status = nfserr_wrong_cred;
  3367. if (!nfsd4_mach_creds_match(conf, rqstp))
  3368. goto out_cache_error;
  3369. } else {
  3370. status = nfserr_clid_inuse;
  3371. if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
  3372. !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
  3373. trace_nfsd_clid_cred_mismatch(unconf, rqstp);
  3374. goto out_cache_error;
  3375. }
  3376. status = nfserr_wrong_cred;
  3377. if (!nfsd4_mach_creds_match(unconf, rqstp))
  3378. goto out_cache_error;
  3379. old = find_confirmed_client_by_name(&unconf->cl_name, nn);
  3380. if (old) {
  3381. status = mark_client_expired_locked(old);
  3382. if (status)
  3383. goto out_expired_error;
  3384. trace_nfsd_clid_replaced(&old->cl_clientid);
  3385. }
  3386. move_to_confirmed(unconf);
  3387. conf = unconf;
  3388. }
  3389. /* RFC 8881 Section 18.36.4 Phase 4: Session creation. */
  3390. status = nfs_ok;
  3391. /* Persistent sessions are not supported */
  3392. cr_ses->flags &= ~SESSION4_PERSIST;
  3393. /* Upshifting from TCP to RDMA is not supported */
  3394. cr_ses->flags &= ~SESSION4_RDMA;
  3395. init_session(rqstp, new, conf, cr_ses);
  3396. nfsd4_get_session_locked(new);
  3397. memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
  3398. NFS4_MAX_SESSIONID_LEN);
  3399. /* cache solo and embedded create sessions under the client_lock */
  3400. nfsd4_cache_create_session(cr_ses, cs_slot, status);
  3401. spin_unlock(&nn->client_lock);
  3402. if (conf == unconf)
  3403. fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
  3404. /* init connection and backchannel */
  3405. nfsd4_init_conn(rqstp, conn, new);
  3406. nfsd4_put_session(new);
  3407. if (old)
  3408. expire_client(old);
  3409. return status;
  3410. out_expired_error:
  3411. old = NULL;
  3412. /*
  3413. * Revert the slot seq_nr change so the server will process
  3414. * the client's resend instead of returning a cached response.
  3415. */
  3416. if (status == nfserr_jukebox) {
  3417. cs_slot->sl_seqid--;
  3418. cr_ses->seqid = cs_slot->sl_seqid;
  3419. goto out_free_conn;
  3420. }
  3421. out_cache_error:
  3422. nfsd4_cache_create_session(cr_ses, cs_slot, status);
  3423. out_free_conn:
  3424. spin_unlock(&nn->client_lock);
  3425. free_conn(conn);
  3426. if (old)
  3427. expire_client(old);
  3428. out_free_session:
  3429. __free_session(new);
  3430. out_release_drc_mem:
  3431. nfsd4_put_drc_mem(&cr_ses->fore_channel);
  3432. return status;
  3433. }
  3434. static __be32 nfsd4_map_bcts_dir(u32 *dir)
  3435. {
  3436. switch (*dir) {
  3437. case NFS4_CDFC4_FORE:
  3438. case NFS4_CDFC4_BACK:
  3439. return nfs_ok;
  3440. case NFS4_CDFC4_FORE_OR_BOTH:
  3441. case NFS4_CDFC4_BACK_OR_BOTH:
  3442. *dir = NFS4_CDFC4_BOTH;
  3443. return nfs_ok;
  3444. }
  3445. return nfserr_inval;
  3446. }
  3447. __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
  3448. struct nfsd4_compound_state *cstate,
  3449. union nfsd4_op_u *u)
  3450. {
  3451. struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
  3452. struct nfsd4_session *session = cstate->session;
  3453. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3454. __be32 status;
  3455. status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
  3456. if (status)
  3457. return status;
  3458. spin_lock(&nn->client_lock);
  3459. session->se_cb_prog = bc->bc_cb_program;
  3460. session->se_cb_sec = bc->bc_cb_sec;
  3461. spin_unlock(&nn->client_lock);
  3462. nfsd4_probe_callback(session->se_client);
  3463. return nfs_ok;
  3464. }
  3465. static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
  3466. {
  3467. struct nfsd4_conn *c;
  3468. list_for_each_entry(c, &s->se_conns, cn_persession) {
  3469. if (c->cn_xprt == xpt) {
  3470. return c;
  3471. }
  3472. }
  3473. return NULL;
  3474. }
  3475. static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
  3476. struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn)
  3477. {
  3478. struct nfs4_client *clp = session->se_client;
  3479. struct svc_xprt *xpt = rqst->rq_xprt;
  3480. struct nfsd4_conn *c;
  3481. __be32 status;
  3482. /* Following the last paragraph of RFC 5661 Section 18.34.3: */
  3483. spin_lock(&clp->cl_lock);
  3484. c = __nfsd4_find_conn(xpt, session);
  3485. if (!c)
  3486. status = nfserr_noent;
  3487. else if (req == c->cn_flags)
  3488. status = nfs_ok;
  3489. else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
  3490. c->cn_flags != NFS4_CDFC4_BACK)
  3491. status = nfs_ok;
  3492. else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
  3493. c->cn_flags != NFS4_CDFC4_FORE)
  3494. status = nfs_ok;
  3495. else
  3496. status = nfserr_inval;
  3497. spin_unlock(&clp->cl_lock);
  3498. if (status == nfs_ok && conn)
  3499. *conn = c;
  3500. return status;
  3501. }
  3502. __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
  3503. struct nfsd4_compound_state *cstate,
  3504. union nfsd4_op_u *u)
  3505. {
  3506. struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
  3507. __be32 status;
  3508. struct nfsd4_conn *conn;
  3509. struct nfsd4_session *session;
  3510. struct net *net = SVC_NET(rqstp);
  3511. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3512. if (!nfsd4_last_compound_op(rqstp))
  3513. return nfserr_not_only_op;
  3514. spin_lock(&nn->client_lock);
  3515. session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
  3516. spin_unlock(&nn->client_lock);
  3517. if (!session)
  3518. goto out_no_session;
  3519. status = nfserr_wrong_cred;
  3520. if (!nfsd4_mach_creds_match(session->se_client, rqstp))
  3521. goto out;
  3522. status = nfsd4_match_existing_connection(rqstp, session,
  3523. bcts->dir, &conn);
  3524. if (status == nfs_ok) {
  3525. if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH ||
  3526. bcts->dir == NFS4_CDFC4_BACK)
  3527. conn->cn_flags |= NFS4_CDFC4_BACK;
  3528. nfsd4_probe_callback(session->se_client);
  3529. goto out;
  3530. }
  3531. if (status == nfserr_inval)
  3532. goto out;
  3533. status = nfsd4_map_bcts_dir(&bcts->dir);
  3534. if (status)
  3535. goto out;
  3536. conn = alloc_conn(rqstp, bcts->dir);
  3537. status = nfserr_jukebox;
  3538. if (!conn)
  3539. goto out;
  3540. nfsd4_init_conn(rqstp, conn, session);
  3541. status = nfs_ok;
  3542. out:
  3543. nfsd4_put_session(session);
  3544. out_no_session:
  3545. return status;
  3546. }
  3547. static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
  3548. {
  3549. if (!cstate->session)
  3550. return false;
  3551. return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
  3552. }
  3553. __be32
  3554. nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
  3555. union nfsd4_op_u *u)
  3556. {
  3557. struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
  3558. struct nfsd4_session *ses;
  3559. __be32 status;
  3560. int ref_held_by_me = 0;
  3561. struct net *net = SVC_NET(r);
  3562. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3563. status = nfserr_not_only_op;
  3564. if (nfsd4_compound_in_session(cstate, sessionid)) {
  3565. if (!nfsd4_last_compound_op(r))
  3566. goto out;
  3567. ref_held_by_me++;
  3568. }
  3569. dump_sessionid(__func__, sessionid);
  3570. spin_lock(&nn->client_lock);
  3571. ses = find_in_sessionid_hashtbl(sessionid, net, &status);
  3572. if (!ses)
  3573. goto out_client_lock;
  3574. status = nfserr_wrong_cred;
  3575. if (!nfsd4_mach_creds_match(ses->se_client, r))
  3576. goto out_put_session;
  3577. status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
  3578. if (status)
  3579. goto out_put_session;
  3580. unhash_session(ses);
  3581. spin_unlock(&nn->client_lock);
  3582. nfsd4_probe_callback_sync(ses->se_client);
  3583. spin_lock(&nn->client_lock);
  3584. status = nfs_ok;
  3585. out_put_session:
  3586. nfsd4_put_session_locked(ses);
  3587. out_client_lock:
  3588. spin_unlock(&nn->client_lock);
  3589. out:
  3590. return status;
  3591. }
  3592. static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
  3593. {
  3594. struct nfs4_client *clp = ses->se_client;
  3595. struct nfsd4_conn *c;
  3596. __be32 status = nfs_ok;
  3597. int ret;
  3598. spin_lock(&clp->cl_lock);
  3599. c = __nfsd4_find_conn(new->cn_xprt, ses);
  3600. if (c)
  3601. goto out_free;
  3602. status = nfserr_conn_not_bound_to_session;
  3603. if (clp->cl_mach_cred)
  3604. goto out_free;
  3605. __nfsd4_hash_conn(new, ses);
  3606. spin_unlock(&clp->cl_lock);
  3607. ret = nfsd4_register_conn(new);
  3608. if (ret)
  3609. /* oops; xprt is already down: */
  3610. nfsd4_conn_lost(&new->cn_xpt_user);
  3611. return nfs_ok;
  3612. out_free:
  3613. spin_unlock(&clp->cl_lock);
  3614. free_conn(new);
  3615. return status;
  3616. }
  3617. static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
  3618. {
  3619. struct nfsd4_compoundargs *args = rqstp->rq_argp;
  3620. return args->opcnt > session->se_fchannel.maxops;
  3621. }
  3622. static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
  3623. struct nfsd4_session *session)
  3624. {
  3625. struct xdr_buf *xb = &rqstp->rq_arg;
  3626. return xb->len > session->se_fchannel.maxreq_sz;
  3627. }
  3628. static bool replay_matches_cache(struct svc_rqst *rqstp,
  3629. struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
  3630. {
  3631. struct nfsd4_compoundargs *argp = rqstp->rq_argp;
  3632. if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
  3633. (bool)seq->cachethis)
  3634. return false;
  3635. /*
  3636. * If there's an error then the reply can have fewer ops than
  3637. * the call.
  3638. */
  3639. if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
  3640. return false;
  3641. /*
  3642. * But if we cached a reply with *more* ops than the call you're
  3643. * sending us now, then this new call is clearly not really a
  3644. * replay of the old one:
  3645. */
  3646. if (slot->sl_opcnt > argp->opcnt)
  3647. return false;
  3648. /* This is the only check explicitly called by spec: */
  3649. if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
  3650. return false;
  3651. /*
  3652. * There may be more comparisons we could actually do, but the
  3653. * spec doesn't require us to catch every case where the calls
  3654. * don't match (that would require caching the call as well as
  3655. * the reply), so we don't bother.
  3656. */
  3657. return true;
  3658. }
  3659. __be32
  3660. nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3661. union nfsd4_op_u *u)
  3662. {
  3663. struct nfsd4_sequence *seq = &u->sequence;
  3664. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  3665. struct xdr_stream *xdr = resp->xdr;
  3666. struct nfsd4_session *session;
  3667. struct nfs4_client *clp;
  3668. struct nfsd4_slot *slot;
  3669. struct nfsd4_conn *conn;
  3670. __be32 status;
  3671. int buflen;
  3672. struct net *net = SVC_NET(rqstp);
  3673. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  3674. if (resp->opcnt != 1)
  3675. return nfserr_sequence_pos;
  3676. /*
  3677. * Will be either used or freed by nfsd4_sequence_check_conn
  3678. * below.
  3679. */
  3680. conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
  3681. if (!conn)
  3682. return nfserr_jukebox;
  3683. spin_lock(&nn->client_lock);
  3684. session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
  3685. if (!session)
  3686. goto out_no_session;
  3687. clp = session->se_client;
  3688. status = nfserr_too_many_ops;
  3689. if (nfsd4_session_too_many_ops(rqstp, session))
  3690. goto out_put_session;
  3691. status = nfserr_req_too_big;
  3692. if (nfsd4_request_too_big(rqstp, session))
  3693. goto out_put_session;
  3694. status = nfserr_badslot;
  3695. if (seq->slotid >= session->se_fchannel.maxreqs)
  3696. goto out_put_session;
  3697. slot = session->se_slots[seq->slotid];
  3698. dprintk("%s: slotid %d\n", __func__, seq->slotid);
  3699. /* We do not negotiate the number of slots yet, so set the
  3700. * maxslots to the session maxreqs which is used to encode
  3701. * sr_highest_slotid and the sr_target_slot id to maxslots */
  3702. seq->maxslots = session->se_fchannel.maxreqs;
  3703. trace_nfsd_slot_seqid_sequence(clp, seq, slot);
  3704. status = check_slot_seqid(seq->seqid, slot->sl_seqid,
  3705. slot->sl_flags & NFSD4_SLOT_INUSE);
  3706. if (status == nfserr_replay_cache) {
  3707. status = nfserr_seq_misordered;
  3708. if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
  3709. goto out_put_session;
  3710. status = nfserr_seq_false_retry;
  3711. if (!replay_matches_cache(rqstp, seq, slot))
  3712. goto out_put_session;
  3713. cstate->slot = slot;
  3714. cstate->session = session;
  3715. cstate->clp = clp;
  3716. /* Return the cached reply status and set cstate->status
  3717. * for nfsd4_proc_compound processing */
  3718. status = nfsd4_replay_cache_entry(resp, seq);
  3719. cstate->status = nfserr_replay_cache;
  3720. goto out;
  3721. }
  3722. if (status)
  3723. goto out_put_session;
  3724. status = nfsd4_sequence_check_conn(conn, session);
  3725. conn = NULL;
  3726. if (status)
  3727. goto out_put_session;
  3728. buflen = (seq->cachethis) ?
  3729. session->se_fchannel.maxresp_cached :
  3730. session->se_fchannel.maxresp_sz;
  3731. status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
  3732. nfserr_rep_too_big;
  3733. if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
  3734. goto out_put_session;
  3735. svc_reserve(rqstp, buflen);
  3736. status = nfs_ok;
  3737. /* Success! bump slot seqid */
  3738. slot->sl_seqid = seq->seqid;
  3739. slot->sl_flags |= NFSD4_SLOT_INUSE;
  3740. if (seq->cachethis)
  3741. slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
  3742. else
  3743. slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
  3744. cstate->slot = slot;
  3745. cstate->session = session;
  3746. cstate->clp = clp;
  3747. out:
  3748. switch (clp->cl_cb_state) {
  3749. case NFSD4_CB_DOWN:
  3750. seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
  3751. break;
  3752. case NFSD4_CB_FAULT:
  3753. seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
  3754. break;
  3755. default:
  3756. seq->status_flags = 0;
  3757. }
  3758. if (!list_empty(&clp->cl_revoked))
  3759. seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
  3760. if (atomic_read(&clp->cl_admin_revoked))
  3761. seq->status_flags |= SEQ4_STATUS_ADMIN_STATE_REVOKED;
  3762. trace_nfsd_seq4_status(rqstp, seq);
  3763. out_no_session:
  3764. if (conn)
  3765. free_conn(conn);
  3766. spin_unlock(&nn->client_lock);
  3767. return status;
  3768. out_put_session:
  3769. nfsd4_put_session_locked(session);
  3770. goto out_no_session;
  3771. }
  3772. void
  3773. nfsd4_sequence_done(struct nfsd4_compoundres *resp)
  3774. {
  3775. struct nfsd4_compound_state *cs = &resp->cstate;
  3776. if (nfsd4_has_session(cs)) {
  3777. if (cs->status != nfserr_replay_cache) {
  3778. nfsd4_store_cache_entry(resp);
  3779. cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
  3780. }
  3781. /* Drop session reference that was taken in nfsd4_sequence() */
  3782. nfsd4_put_session(cs->session);
  3783. } else if (cs->clp)
  3784. put_client_renew(cs->clp);
  3785. }
  3786. __be32
  3787. nfsd4_destroy_clientid(struct svc_rqst *rqstp,
  3788. struct nfsd4_compound_state *cstate,
  3789. union nfsd4_op_u *u)
  3790. {
  3791. struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
  3792. struct nfs4_client *conf, *unconf;
  3793. struct nfs4_client *clp = NULL;
  3794. __be32 status = 0;
  3795. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3796. spin_lock(&nn->client_lock);
  3797. unconf = find_unconfirmed_client(&dc->clientid, true, nn);
  3798. conf = find_confirmed_client(&dc->clientid, true, nn);
  3799. WARN_ON_ONCE(conf && unconf);
  3800. if (conf) {
  3801. if (client_has_state(conf)) {
  3802. status = nfserr_clientid_busy;
  3803. goto out;
  3804. }
  3805. status = mark_client_expired_locked(conf);
  3806. if (status)
  3807. goto out;
  3808. clp = conf;
  3809. } else if (unconf)
  3810. clp = unconf;
  3811. else {
  3812. status = nfserr_stale_clientid;
  3813. goto out;
  3814. }
  3815. if (!nfsd4_mach_creds_match(clp, rqstp)) {
  3816. clp = NULL;
  3817. status = nfserr_wrong_cred;
  3818. goto out;
  3819. }
  3820. trace_nfsd_clid_destroyed(&clp->cl_clientid);
  3821. unhash_client_locked(clp);
  3822. out:
  3823. spin_unlock(&nn->client_lock);
  3824. if (clp)
  3825. expire_client(clp);
  3826. return status;
  3827. }
  3828. __be32
  3829. nfsd4_reclaim_complete(struct svc_rqst *rqstp,
  3830. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  3831. {
  3832. struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
  3833. struct nfs4_client *clp = cstate->clp;
  3834. __be32 status = 0;
  3835. if (rc->rca_one_fs) {
  3836. if (!cstate->current_fh.fh_dentry)
  3837. return nfserr_nofilehandle;
  3838. /*
  3839. * We don't take advantage of the rca_one_fs case.
  3840. * That's OK, it's optional, we can safely ignore it.
  3841. */
  3842. return nfs_ok;
  3843. }
  3844. status = nfserr_complete_already;
  3845. if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
  3846. goto out;
  3847. status = nfserr_stale_clientid;
  3848. if (is_client_expired(clp))
  3849. /*
  3850. * The following error isn't really legal.
  3851. * But we only get here if the client just explicitly
  3852. * destroyed the client. Surely it no longer cares what
  3853. * error it gets back on an operation for the dead
  3854. * client.
  3855. */
  3856. goto out;
  3857. status = nfs_ok;
  3858. trace_nfsd_clid_reclaim_complete(&clp->cl_clientid);
  3859. nfsd4_client_record_create(clp);
  3860. inc_reclaim_complete(clp);
  3861. out:
  3862. return status;
  3863. }
  3864. __be32
  3865. nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  3866. union nfsd4_op_u *u)
  3867. {
  3868. struct nfsd4_setclientid *setclid = &u->setclientid;
  3869. struct xdr_netobj clname = setclid->se_name;
  3870. nfs4_verifier clverifier = setclid->se_verf;
  3871. struct nfs4_client *conf, *new;
  3872. struct nfs4_client *unconf = NULL;
  3873. __be32 status;
  3874. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3875. new = create_client(clname, rqstp, &clverifier);
  3876. if (new == NULL)
  3877. return nfserr_jukebox;
  3878. spin_lock(&nn->client_lock);
  3879. conf = find_confirmed_client_by_name(&clname, nn);
  3880. if (conf && client_has_state(conf)) {
  3881. status = nfserr_clid_inuse;
  3882. if (clp_used_exchangeid(conf))
  3883. goto out;
  3884. if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
  3885. trace_nfsd_clid_cred_mismatch(conf, rqstp);
  3886. goto out;
  3887. }
  3888. }
  3889. unconf = find_unconfirmed_client_by_name(&clname, nn);
  3890. if (unconf)
  3891. unhash_client_locked(unconf);
  3892. if (conf) {
  3893. if (same_verf(&conf->cl_verifier, &clverifier)) {
  3894. copy_clid(new, conf);
  3895. gen_confirm(new, nn);
  3896. } else
  3897. trace_nfsd_clid_verf_mismatch(conf, rqstp,
  3898. &clverifier);
  3899. } else
  3900. trace_nfsd_clid_fresh(new);
  3901. new->cl_minorversion = 0;
  3902. gen_callback(new, setclid, rqstp);
  3903. add_to_unconfirmed(new);
  3904. setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
  3905. setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
  3906. memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
  3907. new = NULL;
  3908. status = nfs_ok;
  3909. out:
  3910. spin_unlock(&nn->client_lock);
  3911. if (new)
  3912. free_client(new);
  3913. if (unconf) {
  3914. trace_nfsd_clid_expire_unconf(&unconf->cl_clientid);
  3915. expire_client(unconf);
  3916. }
  3917. return status;
  3918. }
  3919. __be32
  3920. nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
  3921. struct nfsd4_compound_state *cstate,
  3922. union nfsd4_op_u *u)
  3923. {
  3924. struct nfsd4_setclientid_confirm *setclientid_confirm =
  3925. &u->setclientid_confirm;
  3926. struct nfs4_client *conf, *unconf;
  3927. struct nfs4_client *old = NULL;
  3928. nfs4_verifier confirm = setclientid_confirm->sc_confirm;
  3929. clientid_t * clid = &setclientid_confirm->sc_clientid;
  3930. __be32 status;
  3931. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  3932. if (STALE_CLIENTID(clid, nn))
  3933. return nfserr_stale_clientid;
  3934. spin_lock(&nn->client_lock);
  3935. conf = find_confirmed_client(clid, false, nn);
  3936. unconf = find_unconfirmed_client(clid, false, nn);
  3937. /*
  3938. * We try hard to give out unique clientid's, so if we get an
  3939. * attempt to confirm the same clientid with a different cred,
  3940. * the client may be buggy; this should never happen.
  3941. *
  3942. * Nevertheless, RFC 7530 recommends INUSE for this case:
  3943. */
  3944. status = nfserr_clid_inuse;
  3945. if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) {
  3946. trace_nfsd_clid_cred_mismatch(unconf, rqstp);
  3947. goto out;
  3948. }
  3949. if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
  3950. trace_nfsd_clid_cred_mismatch(conf, rqstp);
  3951. goto out;
  3952. }
  3953. if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
  3954. if (conf && same_verf(&confirm, &conf->cl_confirm)) {
  3955. status = nfs_ok;
  3956. } else
  3957. status = nfserr_stale_clientid;
  3958. goto out;
  3959. }
  3960. status = nfs_ok;
  3961. if (conf) {
  3962. if (get_client_locked(conf) == nfs_ok) {
  3963. old = unconf;
  3964. unhash_client_locked(old);
  3965. nfsd4_change_callback(conf, &unconf->cl_cb_conn);
  3966. } else {
  3967. conf = NULL;
  3968. }
  3969. }
  3970. if (!conf) {
  3971. old = find_confirmed_client_by_name(&unconf->cl_name, nn);
  3972. if (old) {
  3973. status = nfserr_clid_inuse;
  3974. if (client_has_state(old)
  3975. && !same_creds(&unconf->cl_cred,
  3976. &old->cl_cred)) {
  3977. old = NULL;
  3978. goto out;
  3979. }
  3980. status = mark_client_expired_locked(old);
  3981. if (status) {
  3982. old = NULL;
  3983. goto out;
  3984. }
  3985. trace_nfsd_clid_replaced(&old->cl_clientid);
  3986. }
  3987. status = get_client_locked(unconf);
  3988. if (status != nfs_ok) {
  3989. old = NULL;
  3990. goto out;
  3991. }
  3992. move_to_confirmed(unconf);
  3993. conf = unconf;
  3994. }
  3995. spin_unlock(&nn->client_lock);
  3996. if (conf == unconf)
  3997. fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY);
  3998. nfsd4_probe_callback(conf);
  3999. spin_lock(&nn->client_lock);
  4000. put_client_renew_locked(conf);
  4001. out:
  4002. spin_unlock(&nn->client_lock);
  4003. if (old)
  4004. expire_client(old);
  4005. return status;
  4006. }
  4007. static struct nfs4_file *nfsd4_alloc_file(void)
  4008. {
  4009. return kmem_cache_alloc(file_slab, GFP_KERNEL);
  4010. }
  4011. /* OPEN Share state helper functions */
  4012. static void nfsd4_file_init(const struct svc_fh *fh, struct nfs4_file *fp)
  4013. {
  4014. refcount_set(&fp->fi_ref, 1);
  4015. spin_lock_init(&fp->fi_lock);
  4016. INIT_LIST_HEAD(&fp->fi_stateids);
  4017. INIT_LIST_HEAD(&fp->fi_delegations);
  4018. INIT_LIST_HEAD(&fp->fi_clnt_odstate);
  4019. fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle);
  4020. fp->fi_deleg_file = NULL;
  4021. fp->fi_had_conflict = false;
  4022. fp->fi_share_deny = 0;
  4023. memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
  4024. memset(fp->fi_access, 0, sizeof(fp->fi_access));
  4025. fp->fi_aliased = false;
  4026. fp->fi_inode = d_inode(fh->fh_dentry);
  4027. #ifdef CONFIG_NFSD_PNFS
  4028. INIT_LIST_HEAD(&fp->fi_lo_states);
  4029. atomic_set(&fp->fi_lo_recalls, 0);
  4030. #endif
  4031. }
  4032. void
  4033. nfsd4_free_slabs(void)
  4034. {
  4035. kmem_cache_destroy(client_slab);
  4036. kmem_cache_destroy(openowner_slab);
  4037. kmem_cache_destroy(lockowner_slab);
  4038. kmem_cache_destroy(file_slab);
  4039. kmem_cache_destroy(stateid_slab);
  4040. kmem_cache_destroy(deleg_slab);
  4041. kmem_cache_destroy(odstate_slab);
  4042. }
  4043. int
  4044. nfsd4_init_slabs(void)
  4045. {
  4046. client_slab = KMEM_CACHE(nfs4_client, 0);
  4047. if (client_slab == NULL)
  4048. goto out;
  4049. openowner_slab = KMEM_CACHE(nfs4_openowner, 0);
  4050. if (openowner_slab == NULL)
  4051. goto out_free_client_slab;
  4052. lockowner_slab = KMEM_CACHE(nfs4_lockowner, 0);
  4053. if (lockowner_slab == NULL)
  4054. goto out_free_openowner_slab;
  4055. file_slab = KMEM_CACHE(nfs4_file, 0);
  4056. if (file_slab == NULL)
  4057. goto out_free_lockowner_slab;
  4058. stateid_slab = KMEM_CACHE(nfs4_ol_stateid, 0);
  4059. if (stateid_slab == NULL)
  4060. goto out_free_file_slab;
  4061. deleg_slab = KMEM_CACHE(nfs4_delegation, 0);
  4062. if (deleg_slab == NULL)
  4063. goto out_free_stateid_slab;
  4064. odstate_slab = KMEM_CACHE(nfs4_clnt_odstate, 0);
  4065. if (odstate_slab == NULL)
  4066. goto out_free_deleg_slab;
  4067. return 0;
  4068. out_free_deleg_slab:
  4069. kmem_cache_destroy(deleg_slab);
  4070. out_free_stateid_slab:
  4071. kmem_cache_destroy(stateid_slab);
  4072. out_free_file_slab:
  4073. kmem_cache_destroy(file_slab);
  4074. out_free_lockowner_slab:
  4075. kmem_cache_destroy(lockowner_slab);
  4076. out_free_openowner_slab:
  4077. kmem_cache_destroy(openowner_slab);
  4078. out_free_client_slab:
  4079. kmem_cache_destroy(client_slab);
  4080. out:
  4081. return -ENOMEM;
  4082. }
  4083. static unsigned long
  4084. nfsd4_state_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
  4085. {
  4086. int count;
  4087. struct nfsd_net *nn = shrink->private_data;
  4088. count = atomic_read(&nn->nfsd_courtesy_clients);
  4089. if (!count)
  4090. count = atomic_long_read(&num_delegations);
  4091. if (count)
  4092. queue_work(laundry_wq, &nn->nfsd_shrinker_work);
  4093. return (unsigned long)count;
  4094. }
  4095. static unsigned long
  4096. nfsd4_state_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
  4097. {
  4098. return SHRINK_STOP;
  4099. }
  4100. void
  4101. nfsd4_init_leases_net(struct nfsd_net *nn)
  4102. {
  4103. struct sysinfo si;
  4104. u64 max_clients;
  4105. nn->nfsd4_lease = 90; /* default lease time */
  4106. nn->nfsd4_grace = 90;
  4107. nn->somebody_reclaimed = false;
  4108. nn->track_reclaim_completes = false;
  4109. nn->clverifier_counter = get_random_u32();
  4110. nn->clientid_base = get_random_u32();
  4111. nn->clientid_counter = nn->clientid_base + 1;
  4112. nn->s2s_cp_cl_id = nn->clientid_counter++;
  4113. atomic_set(&nn->nfs4_client_count, 0);
  4114. si_meminfo(&si);
  4115. max_clients = (u64)si.totalram * si.mem_unit / (1024 * 1024 * 1024);
  4116. max_clients *= NFS4_CLIENTS_PER_GB;
  4117. nn->nfs4_max_clients = max_t(int, max_clients, NFS4_CLIENTS_PER_GB);
  4118. atomic_set(&nn->nfsd_courtesy_clients, 0);
  4119. }
  4120. enum rp_lock {
  4121. RP_UNLOCKED,
  4122. RP_LOCKED,
  4123. RP_UNHASHED,
  4124. };
  4125. static void init_nfs4_replay(struct nfs4_replay *rp)
  4126. {
  4127. rp->rp_status = nfserr_serverfault;
  4128. rp->rp_buflen = 0;
  4129. rp->rp_buf = rp->rp_ibuf;
  4130. atomic_set(&rp->rp_locked, RP_UNLOCKED);
  4131. }
  4132. static int nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
  4133. struct nfs4_stateowner *so)
  4134. {
  4135. if (!nfsd4_has_session(cstate)) {
  4136. wait_var_event(&so->so_replay.rp_locked,
  4137. atomic_cmpxchg(&so->so_replay.rp_locked,
  4138. RP_UNLOCKED, RP_LOCKED) != RP_LOCKED);
  4139. if (atomic_read(&so->so_replay.rp_locked) == RP_UNHASHED)
  4140. return -EAGAIN;
  4141. cstate->replay_owner = nfs4_get_stateowner(so);
  4142. }
  4143. return 0;
  4144. }
  4145. void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
  4146. {
  4147. struct nfs4_stateowner *so = cstate->replay_owner;
  4148. if (so != NULL) {
  4149. cstate->replay_owner = NULL;
  4150. atomic_set(&so->so_replay.rp_locked, RP_UNLOCKED);
  4151. smp_mb__after_atomic();
  4152. wake_up_var(&so->so_replay.rp_locked);
  4153. nfs4_put_stateowner(so);
  4154. }
  4155. }
  4156. static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
  4157. {
  4158. struct nfs4_stateowner *sop;
  4159. sop = kmem_cache_alloc(slab, GFP_KERNEL);
  4160. if (!sop)
  4161. return NULL;
  4162. xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
  4163. if (!sop->so_owner.data) {
  4164. kmem_cache_free(slab, sop);
  4165. return NULL;
  4166. }
  4167. INIT_LIST_HEAD(&sop->so_stateids);
  4168. sop->so_client = clp;
  4169. init_nfs4_replay(&sop->so_replay);
  4170. atomic_set(&sop->so_count, 1);
  4171. return sop;
  4172. }
  4173. static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
  4174. {
  4175. lockdep_assert_held(&clp->cl_lock);
  4176. list_add(&oo->oo_owner.so_strhash,
  4177. &clp->cl_ownerstr_hashtbl[strhashval]);
  4178. list_add(&oo->oo_perclient, &clp->cl_openowners);
  4179. }
  4180. static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
  4181. {
  4182. unhash_openowner_locked(openowner(so));
  4183. }
  4184. static void nfs4_free_openowner(struct nfs4_stateowner *so)
  4185. {
  4186. struct nfs4_openowner *oo = openowner(so);
  4187. kmem_cache_free(openowner_slab, oo);
  4188. }
  4189. static const struct nfs4_stateowner_operations openowner_ops = {
  4190. .so_unhash = nfs4_unhash_openowner,
  4191. .so_free = nfs4_free_openowner,
  4192. };
  4193. static struct nfs4_ol_stateid *
  4194. nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
  4195. {
  4196. struct nfs4_ol_stateid *local, *ret = NULL;
  4197. struct nfs4_openowner *oo = open->op_openowner;
  4198. lockdep_assert_held(&fp->fi_lock);
  4199. list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
  4200. /* ignore lock owners */
  4201. if (local->st_stateowner->so_is_open_owner == 0)
  4202. continue;
  4203. if (local->st_stateowner != &oo->oo_owner)
  4204. continue;
  4205. if (local->st_stid.sc_type == SC_TYPE_OPEN &&
  4206. !local->st_stid.sc_status) {
  4207. ret = local;
  4208. refcount_inc(&ret->st_stid.sc_count);
  4209. break;
  4210. }
  4211. }
  4212. return ret;
  4213. }
  4214. static void nfsd4_drop_revoked_stid(struct nfs4_stid *s)
  4215. __releases(&s->sc_client->cl_lock)
  4216. {
  4217. struct nfs4_client *cl = s->sc_client;
  4218. LIST_HEAD(reaplist);
  4219. struct nfs4_ol_stateid *stp;
  4220. struct nfs4_delegation *dp;
  4221. bool unhashed;
  4222. switch (s->sc_type) {
  4223. case SC_TYPE_OPEN:
  4224. stp = openlockstateid(s);
  4225. if (unhash_open_stateid(stp, &reaplist))
  4226. put_ol_stateid_locked(stp, &reaplist);
  4227. spin_unlock(&cl->cl_lock);
  4228. free_ol_stateid_reaplist(&reaplist);
  4229. break;
  4230. case SC_TYPE_LOCK:
  4231. stp = openlockstateid(s);
  4232. unhashed = unhash_lock_stateid(stp);
  4233. spin_unlock(&cl->cl_lock);
  4234. if (unhashed)
  4235. nfs4_put_stid(s);
  4236. break;
  4237. case SC_TYPE_DELEG:
  4238. dp = delegstateid(s);
  4239. list_del_init(&dp->dl_recall_lru);
  4240. spin_unlock(&cl->cl_lock);
  4241. nfs4_put_stid(s);
  4242. break;
  4243. default:
  4244. spin_unlock(&cl->cl_lock);
  4245. }
  4246. }
  4247. static void nfsd40_drop_revoked_stid(struct nfs4_client *cl,
  4248. stateid_t *stid)
  4249. {
  4250. /* NFSv4.0 has no way for the client to tell the server
  4251. * that it can forget an admin-revoked stateid.
  4252. * So we keep it around until the first time that the
  4253. * client uses it, and drop it the first time
  4254. * nfserr_admin_revoked is returned.
  4255. * For v4.1 and later we wait until explicitly told
  4256. * to free the stateid.
  4257. */
  4258. if (cl->cl_minorversion == 0) {
  4259. struct nfs4_stid *st;
  4260. spin_lock(&cl->cl_lock);
  4261. st = find_stateid_locked(cl, stid);
  4262. if (st)
  4263. nfsd4_drop_revoked_stid(st);
  4264. else
  4265. spin_unlock(&cl->cl_lock);
  4266. }
  4267. }
  4268. static __be32
  4269. nfsd4_verify_open_stid(struct nfs4_stid *s)
  4270. {
  4271. __be32 ret = nfs_ok;
  4272. if (s->sc_status & SC_STATUS_ADMIN_REVOKED)
  4273. ret = nfserr_admin_revoked;
  4274. else if (s->sc_status & SC_STATUS_REVOKED)
  4275. ret = nfserr_deleg_revoked;
  4276. else if (s->sc_status & SC_STATUS_CLOSED)
  4277. ret = nfserr_bad_stateid;
  4278. return ret;
  4279. }
  4280. /* Lock the stateid st_mutex, and deal with races with CLOSE */
  4281. static __be32
  4282. nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
  4283. {
  4284. __be32 ret;
  4285. mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
  4286. ret = nfsd4_verify_open_stid(&stp->st_stid);
  4287. if (ret == nfserr_admin_revoked)
  4288. nfsd40_drop_revoked_stid(stp->st_stid.sc_client,
  4289. &stp->st_stid.sc_stateid);
  4290. if (ret != nfs_ok)
  4291. mutex_unlock(&stp->st_mutex);
  4292. return ret;
  4293. }
  4294. static struct nfs4_ol_stateid *
  4295. nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
  4296. {
  4297. struct nfs4_ol_stateid *stp;
  4298. for (;;) {
  4299. spin_lock(&fp->fi_lock);
  4300. stp = nfsd4_find_existing_open(fp, open);
  4301. spin_unlock(&fp->fi_lock);
  4302. if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
  4303. break;
  4304. nfs4_put_stid(&stp->st_stid);
  4305. }
  4306. return stp;
  4307. }
  4308. static struct nfs4_openowner *
  4309. find_or_alloc_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
  4310. struct nfsd4_compound_state *cstate)
  4311. {
  4312. struct nfs4_client *clp = cstate->clp;
  4313. struct nfs4_openowner *oo, *new = NULL;
  4314. retry:
  4315. spin_lock(&clp->cl_lock);
  4316. oo = find_openstateowner_str(strhashval, open, clp);
  4317. if (!oo && new) {
  4318. hash_openowner(new, clp, strhashval);
  4319. spin_unlock(&clp->cl_lock);
  4320. return new;
  4321. }
  4322. spin_unlock(&clp->cl_lock);
  4323. if (oo && !(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  4324. /* Replace unconfirmed owners without checking for replay. */
  4325. release_openowner(oo);
  4326. oo = NULL;
  4327. }
  4328. if (oo) {
  4329. if (new)
  4330. nfs4_free_stateowner(&new->oo_owner);
  4331. return oo;
  4332. }
  4333. new = alloc_stateowner(openowner_slab, &open->op_owner, clp);
  4334. if (!new)
  4335. return NULL;
  4336. new->oo_owner.so_ops = &openowner_ops;
  4337. new->oo_owner.so_is_open_owner = 1;
  4338. new->oo_owner.so_seqid = open->op_seqid;
  4339. new->oo_flags = 0;
  4340. if (nfsd4_has_session(cstate))
  4341. new->oo_flags |= NFS4_OO_CONFIRMED;
  4342. new->oo_time = 0;
  4343. new->oo_last_closed_stid = NULL;
  4344. INIT_LIST_HEAD(&new->oo_close_lru);
  4345. goto retry;
  4346. }
  4347. static struct nfs4_ol_stateid *
  4348. init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
  4349. {
  4350. struct nfs4_openowner *oo = open->op_openowner;
  4351. struct nfs4_ol_stateid *retstp = NULL;
  4352. struct nfs4_ol_stateid *stp;
  4353. stp = open->op_stp;
  4354. /* We are moving these outside of the spinlocks to avoid the warnings */
  4355. mutex_init(&stp->st_mutex);
  4356. mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
  4357. retry:
  4358. spin_lock(&oo->oo_owner.so_client->cl_lock);
  4359. spin_lock(&fp->fi_lock);
  4360. if (nfs4_openowner_unhashed(oo)) {
  4361. mutex_unlock(&stp->st_mutex);
  4362. stp = NULL;
  4363. goto out_unlock;
  4364. }
  4365. retstp = nfsd4_find_existing_open(fp, open);
  4366. if (retstp)
  4367. goto out_unlock;
  4368. open->op_stp = NULL;
  4369. refcount_inc(&stp->st_stid.sc_count);
  4370. stp->st_stid.sc_type = SC_TYPE_OPEN;
  4371. INIT_LIST_HEAD(&stp->st_locks);
  4372. stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
  4373. get_nfs4_file(fp);
  4374. stp->st_stid.sc_file = fp;
  4375. stp->st_access_bmap = 0;
  4376. stp->st_deny_bmap = 0;
  4377. stp->st_openstp = NULL;
  4378. list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
  4379. list_add(&stp->st_perfile, &fp->fi_stateids);
  4380. out_unlock:
  4381. spin_unlock(&fp->fi_lock);
  4382. spin_unlock(&oo->oo_owner.so_client->cl_lock);
  4383. if (retstp) {
  4384. /* Handle races with CLOSE */
  4385. if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
  4386. nfs4_put_stid(&retstp->st_stid);
  4387. goto retry;
  4388. }
  4389. /* To keep mutex tracking happy */
  4390. mutex_unlock(&stp->st_mutex);
  4391. stp = retstp;
  4392. }
  4393. return stp;
  4394. }
  4395. /*
  4396. * In the 4.0 case we need to keep the owners around a little while to handle
  4397. * CLOSE replay. We still do need to release any file access that is held by
  4398. * them before returning however.
  4399. */
  4400. static void
  4401. move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
  4402. {
  4403. struct nfs4_ol_stateid *last;
  4404. struct nfs4_openowner *oo = openowner(s->st_stateowner);
  4405. struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
  4406. nfsd_net_id);
  4407. dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
  4408. /*
  4409. * We know that we hold one reference via nfsd4_close, and another
  4410. * "persistent" reference for the client. If the refcount is higher
  4411. * than 2, then there are still calls in progress that are using this
  4412. * stateid. We can't put the sc_file reference until they are finished.
  4413. * Wait for the refcount to drop to 2. Since it has been unhashed,
  4414. * there should be no danger of the refcount going back up again at
  4415. * this point.
  4416. * Some threads with a reference might be waiting for rp_locked,
  4417. * so tell them to stop waiting.
  4418. */
  4419. atomic_set(&oo->oo_owner.so_replay.rp_locked, RP_UNHASHED);
  4420. smp_mb__after_atomic();
  4421. wake_up_var(&oo->oo_owner.so_replay.rp_locked);
  4422. wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
  4423. release_all_access(s);
  4424. if (s->st_stid.sc_file) {
  4425. put_nfs4_file(s->st_stid.sc_file);
  4426. s->st_stid.sc_file = NULL;
  4427. }
  4428. spin_lock(&nn->client_lock);
  4429. last = oo->oo_last_closed_stid;
  4430. oo->oo_last_closed_stid = s;
  4431. list_move_tail(&oo->oo_close_lru, &nn->close_lru);
  4432. oo->oo_time = ktime_get_boottime_seconds();
  4433. spin_unlock(&nn->client_lock);
  4434. if (last)
  4435. nfs4_put_stid(&last->st_stid);
  4436. }
  4437. static noinline_for_stack struct nfs4_file *
  4438. nfsd4_file_hash_lookup(const struct svc_fh *fhp)
  4439. {
  4440. struct inode *inode = d_inode(fhp->fh_dentry);
  4441. struct rhlist_head *tmp, *list;
  4442. struct nfs4_file *fi;
  4443. rcu_read_lock();
  4444. list = rhltable_lookup(&nfs4_file_rhltable, &inode,
  4445. nfs4_file_rhash_params);
  4446. rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
  4447. if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
  4448. if (refcount_inc_not_zero(&fi->fi_ref)) {
  4449. rcu_read_unlock();
  4450. return fi;
  4451. }
  4452. }
  4453. }
  4454. rcu_read_unlock();
  4455. return NULL;
  4456. }
  4457. /*
  4458. * On hash insertion, identify entries with the same inode but
  4459. * distinct filehandles. They will all be on the list returned
  4460. * by rhltable_lookup().
  4461. *
  4462. * inode->i_lock prevents racing insertions from adding an entry
  4463. * for the same inode/fhp pair twice.
  4464. */
  4465. static noinline_for_stack struct nfs4_file *
  4466. nfsd4_file_hash_insert(struct nfs4_file *new, const struct svc_fh *fhp)
  4467. {
  4468. struct inode *inode = d_inode(fhp->fh_dentry);
  4469. struct rhlist_head *tmp, *list;
  4470. struct nfs4_file *ret = NULL;
  4471. bool alias_found = false;
  4472. struct nfs4_file *fi;
  4473. int err;
  4474. rcu_read_lock();
  4475. spin_lock(&inode->i_lock);
  4476. list = rhltable_lookup(&nfs4_file_rhltable, &inode,
  4477. nfs4_file_rhash_params);
  4478. rhl_for_each_entry_rcu(fi, tmp, list, fi_rlist) {
  4479. if (fh_match(&fi->fi_fhandle, &fhp->fh_handle)) {
  4480. if (refcount_inc_not_zero(&fi->fi_ref))
  4481. ret = fi;
  4482. } else
  4483. fi->fi_aliased = alias_found = true;
  4484. }
  4485. if (ret)
  4486. goto out_unlock;
  4487. nfsd4_file_init(fhp, new);
  4488. err = rhltable_insert(&nfs4_file_rhltable, &new->fi_rlist,
  4489. nfs4_file_rhash_params);
  4490. if (err)
  4491. goto out_unlock;
  4492. new->fi_aliased = alias_found;
  4493. ret = new;
  4494. out_unlock:
  4495. spin_unlock(&inode->i_lock);
  4496. rcu_read_unlock();
  4497. return ret;
  4498. }
  4499. static noinline_for_stack void nfsd4_file_hash_remove(struct nfs4_file *fi)
  4500. {
  4501. rhltable_remove(&nfs4_file_rhltable, &fi->fi_rlist,
  4502. nfs4_file_rhash_params);
  4503. }
  4504. /*
  4505. * Called to check deny when READ with all zero stateid or
  4506. * WRITE with all zero or all one stateid
  4507. */
  4508. static __be32
  4509. nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
  4510. {
  4511. struct nfs4_file *fp;
  4512. __be32 ret = nfs_ok;
  4513. fp = nfsd4_file_hash_lookup(current_fh);
  4514. if (!fp)
  4515. return ret;
  4516. /* Check for conflicting share reservations */
  4517. spin_lock(&fp->fi_lock);
  4518. if (fp->fi_share_deny & deny_type)
  4519. ret = nfserr_locked;
  4520. spin_unlock(&fp->fi_lock);
  4521. put_nfs4_file(fp);
  4522. return ret;
  4523. }
  4524. static bool nfsd4_deleg_present(const struct inode *inode)
  4525. {
  4526. struct file_lock_context *ctx = locks_inode_context(inode);
  4527. return ctx && !list_empty_careful(&ctx->flc_lease);
  4528. }
  4529. /**
  4530. * nfsd_wait_for_delegreturn - wait for delegations to be returned
  4531. * @rqstp: the RPC transaction being executed
  4532. * @inode: in-core inode of the file being waited for
  4533. *
  4534. * The timeout prevents deadlock if all nfsd threads happen to be
  4535. * tied up waiting for returning delegations.
  4536. *
  4537. * Return values:
  4538. * %true: delegation was returned
  4539. * %false: timed out waiting for delegreturn
  4540. */
  4541. bool nfsd_wait_for_delegreturn(struct svc_rqst *rqstp, struct inode *inode)
  4542. {
  4543. long __maybe_unused timeo;
  4544. timeo = wait_var_event_timeout(inode, !nfsd4_deleg_present(inode),
  4545. NFSD_DELEGRETURN_TIMEOUT);
  4546. trace_nfsd_delegret_wakeup(rqstp, inode, timeo);
  4547. return timeo > 0;
  4548. }
  4549. static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
  4550. {
  4551. struct nfs4_delegation *dp = cb_to_delegation(cb);
  4552. struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
  4553. nfsd_net_id);
  4554. block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
  4555. /*
  4556. * We can't do this in nfsd_break_deleg_cb because it is
  4557. * already holding inode->i_lock.
  4558. *
  4559. * If the dl_time != 0, then we know that it has already been
  4560. * queued for a lease break. Don't queue it again.
  4561. */
  4562. spin_lock(&state_lock);
  4563. if (delegation_hashed(dp) && dp->dl_time == 0) {
  4564. dp->dl_time = ktime_get_boottime_seconds();
  4565. list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
  4566. }
  4567. spin_unlock(&state_lock);
  4568. }
  4569. static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
  4570. struct rpc_task *task)
  4571. {
  4572. struct nfs4_delegation *dp = cb_to_delegation(cb);
  4573. trace_nfsd_cb_recall_done(&dp->dl_stid.sc_stateid, task);
  4574. if (dp->dl_stid.sc_status)
  4575. /* CLOSED or REVOKED */
  4576. return 1;
  4577. switch (task->tk_status) {
  4578. case 0:
  4579. return 1;
  4580. case -NFS4ERR_DELAY:
  4581. rpc_delay(task, 2 * HZ);
  4582. return 0;
  4583. case -EBADHANDLE:
  4584. case -NFS4ERR_BAD_STATEID:
  4585. /*
  4586. * Race: client probably got cb_recall before open reply
  4587. * granting delegation.
  4588. */
  4589. if (dp->dl_retries--) {
  4590. rpc_delay(task, 2 * HZ);
  4591. return 0;
  4592. }
  4593. fallthrough;
  4594. default:
  4595. return 1;
  4596. }
  4597. }
  4598. static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
  4599. {
  4600. struct nfs4_delegation *dp = cb_to_delegation(cb);
  4601. nfs4_put_stid(&dp->dl_stid);
  4602. }
  4603. static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
  4604. .prepare = nfsd4_cb_recall_prepare,
  4605. .done = nfsd4_cb_recall_done,
  4606. .release = nfsd4_cb_recall_release,
  4607. .opcode = OP_CB_RECALL,
  4608. };
  4609. static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
  4610. {
  4611. bool queued;
  4612. /*
  4613. * We're assuming the state code never drops its reference
  4614. * without first removing the lease. Since we're in this lease
  4615. * callback (and since the lease code is serialized by the
  4616. * flc_lock) we know the server hasn't removed the lease yet, and
  4617. * we know it's safe to take a reference.
  4618. */
  4619. refcount_inc(&dp->dl_stid.sc_count);
  4620. queued = nfsd4_run_cb(&dp->dl_recall);
  4621. WARN_ON_ONCE(!queued);
  4622. if (!queued)
  4623. refcount_dec(&dp->dl_stid.sc_count);
  4624. }
  4625. /* Called from break_lease() with flc_lock held. */
  4626. static bool
  4627. nfsd_break_deleg_cb(struct file_lease *fl)
  4628. {
  4629. struct nfs4_delegation *dp = (struct nfs4_delegation *) fl->c.flc_owner;
  4630. struct nfs4_file *fp = dp->dl_stid.sc_file;
  4631. struct nfs4_client *clp = dp->dl_stid.sc_client;
  4632. struct nfsd_net *nn;
  4633. trace_nfsd_cb_recall(&dp->dl_stid);
  4634. dp->dl_recalled = true;
  4635. atomic_inc(&clp->cl_delegs_in_recall);
  4636. if (try_to_expire_client(clp)) {
  4637. nn = net_generic(clp->net, nfsd_net_id);
  4638. mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
  4639. }
  4640. /*
  4641. * We don't want the locks code to timeout the lease for us;
  4642. * we'll remove it ourself if a delegation isn't returned
  4643. * in time:
  4644. */
  4645. fl->fl_break_time = 0;
  4646. fp->fi_had_conflict = true;
  4647. nfsd_break_one_deleg(dp);
  4648. return false;
  4649. }
  4650. /**
  4651. * nfsd_breaker_owns_lease - Check if lease conflict was resolved
  4652. * @fl: Lock state to check
  4653. *
  4654. * Return values:
  4655. * %true: Lease conflict was resolved
  4656. * %false: Lease conflict was not resolved.
  4657. */
  4658. static bool nfsd_breaker_owns_lease(struct file_lease *fl)
  4659. {
  4660. struct nfs4_delegation *dl = fl->c.flc_owner;
  4661. struct svc_rqst *rqst;
  4662. struct nfs4_client *clp;
  4663. rqst = nfsd_current_rqst();
  4664. if (!nfsd_v4client(rqst))
  4665. return false;
  4666. clp = *(rqst->rq_lease_breaker);
  4667. return dl->dl_stid.sc_client == clp;
  4668. }
  4669. static int
  4670. nfsd_change_deleg_cb(struct file_lease *onlist, int arg,
  4671. struct list_head *dispose)
  4672. {
  4673. struct nfs4_delegation *dp = (struct nfs4_delegation *) onlist->c.flc_owner;
  4674. struct nfs4_client *clp = dp->dl_stid.sc_client;
  4675. if (arg & F_UNLCK) {
  4676. if (dp->dl_recalled)
  4677. atomic_dec(&clp->cl_delegs_in_recall);
  4678. return lease_modify(onlist, arg, dispose);
  4679. } else
  4680. return -EAGAIN;
  4681. }
  4682. static const struct lease_manager_operations nfsd_lease_mng_ops = {
  4683. .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
  4684. .lm_break = nfsd_break_deleg_cb,
  4685. .lm_change = nfsd_change_deleg_cb,
  4686. };
  4687. static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
  4688. {
  4689. if (nfsd4_has_session(cstate))
  4690. return nfs_ok;
  4691. if (seqid == so->so_seqid - 1)
  4692. return nfserr_replay_me;
  4693. if (seqid == so->so_seqid)
  4694. return nfs_ok;
  4695. return nfserr_bad_seqid;
  4696. }
  4697. static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions,
  4698. struct nfsd_net *nn)
  4699. {
  4700. struct nfs4_client *found;
  4701. spin_lock(&nn->client_lock);
  4702. found = find_confirmed_client(clid, sessions, nn);
  4703. if (found)
  4704. atomic_inc(&found->cl_rpc_users);
  4705. spin_unlock(&nn->client_lock);
  4706. return found;
  4707. }
  4708. static __be32 set_client(clientid_t *clid,
  4709. struct nfsd4_compound_state *cstate,
  4710. struct nfsd_net *nn)
  4711. {
  4712. if (cstate->clp) {
  4713. if (!same_clid(&cstate->clp->cl_clientid, clid))
  4714. return nfserr_stale_clientid;
  4715. return nfs_ok;
  4716. }
  4717. if (STALE_CLIENTID(clid, nn))
  4718. return nfserr_stale_clientid;
  4719. /*
  4720. * We're in the 4.0 case (otherwise the SEQUENCE op would have
  4721. * set cstate->clp), so session = false:
  4722. */
  4723. cstate->clp = lookup_clientid(clid, false, nn);
  4724. if (!cstate->clp)
  4725. return nfserr_expired;
  4726. return nfs_ok;
  4727. }
  4728. __be32
  4729. nfsd4_process_open1(struct nfsd4_compound_state *cstate,
  4730. struct nfsd4_open *open, struct nfsd_net *nn)
  4731. {
  4732. clientid_t *clientid = &open->op_clientid;
  4733. struct nfs4_client *clp = NULL;
  4734. unsigned int strhashval;
  4735. struct nfs4_openowner *oo = NULL;
  4736. __be32 status;
  4737. /*
  4738. * In case we need it later, after we've already created the
  4739. * file and don't want to risk a further failure:
  4740. */
  4741. open->op_file = nfsd4_alloc_file();
  4742. if (open->op_file == NULL)
  4743. return nfserr_jukebox;
  4744. status = set_client(clientid, cstate, nn);
  4745. if (status)
  4746. return status;
  4747. clp = cstate->clp;
  4748. strhashval = ownerstr_hashval(&open->op_owner);
  4749. retry:
  4750. oo = find_or_alloc_open_stateowner(strhashval, open, cstate);
  4751. open->op_openowner = oo;
  4752. if (!oo)
  4753. return nfserr_jukebox;
  4754. if (nfsd4_cstate_assign_replay(cstate, &oo->oo_owner) == -EAGAIN) {
  4755. nfs4_put_stateowner(&oo->oo_owner);
  4756. goto retry;
  4757. }
  4758. status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
  4759. if (status)
  4760. return status;
  4761. open->op_stp = nfs4_alloc_open_stateid(clp);
  4762. if (!open->op_stp)
  4763. return nfserr_jukebox;
  4764. if (nfsd4_has_session(cstate) &&
  4765. (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
  4766. open->op_odstate = alloc_clnt_odstate(clp);
  4767. if (!open->op_odstate)
  4768. return nfserr_jukebox;
  4769. }
  4770. return nfs_ok;
  4771. }
  4772. static inline __be32
  4773. nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
  4774. {
  4775. if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
  4776. return nfserr_openmode;
  4777. else
  4778. return nfs_ok;
  4779. }
  4780. static int share_access_to_flags(u32 share_access)
  4781. {
  4782. return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
  4783. }
  4784. static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl,
  4785. stateid_t *s)
  4786. {
  4787. struct nfs4_stid *ret;
  4788. ret = find_stateid_by_type(cl, s, SC_TYPE_DELEG, SC_STATUS_REVOKED);
  4789. if (!ret)
  4790. return NULL;
  4791. return delegstateid(ret);
  4792. }
  4793. static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
  4794. {
  4795. return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
  4796. open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
  4797. }
  4798. static __be32
  4799. nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
  4800. struct nfs4_delegation **dp)
  4801. {
  4802. int flags;
  4803. __be32 status = nfserr_bad_stateid;
  4804. struct nfs4_delegation *deleg;
  4805. deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
  4806. if (deleg == NULL)
  4807. goto out;
  4808. if (deleg->dl_stid.sc_status & SC_STATUS_ADMIN_REVOKED) {
  4809. nfs4_put_stid(&deleg->dl_stid);
  4810. status = nfserr_admin_revoked;
  4811. goto out;
  4812. }
  4813. if (deleg->dl_stid.sc_status & SC_STATUS_REVOKED) {
  4814. nfs4_put_stid(&deleg->dl_stid);
  4815. nfsd40_drop_revoked_stid(cl, &open->op_delegate_stateid);
  4816. status = nfserr_deleg_revoked;
  4817. goto out;
  4818. }
  4819. flags = share_access_to_flags(open->op_share_access);
  4820. status = nfs4_check_delegmode(deleg, flags);
  4821. if (status) {
  4822. nfs4_put_stid(&deleg->dl_stid);
  4823. goto out;
  4824. }
  4825. *dp = deleg;
  4826. out:
  4827. if (!nfsd4_is_deleg_cur(open))
  4828. return nfs_ok;
  4829. if (status)
  4830. return status;
  4831. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  4832. return nfs_ok;
  4833. }
  4834. static inline int nfs4_access_to_access(u32 nfs4_access)
  4835. {
  4836. int flags = 0;
  4837. if (nfs4_access & NFS4_SHARE_ACCESS_READ)
  4838. flags |= NFSD_MAY_READ;
  4839. if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
  4840. flags |= NFSD_MAY_WRITE;
  4841. return flags;
  4842. }
  4843. static inline __be32
  4844. nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
  4845. struct nfsd4_open *open)
  4846. {
  4847. struct iattr iattr = {
  4848. .ia_valid = ATTR_SIZE,
  4849. .ia_size = 0,
  4850. };
  4851. struct nfsd_attrs attrs = {
  4852. .na_iattr = &iattr,
  4853. };
  4854. if (!open->op_truncate)
  4855. return 0;
  4856. if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
  4857. return nfserr_inval;
  4858. return nfsd_setattr(rqstp, fh, &attrs, NULL);
  4859. }
  4860. static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
  4861. struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
  4862. struct nfsd4_open *open, bool new_stp)
  4863. {
  4864. struct nfsd_file *nf = NULL;
  4865. __be32 status;
  4866. int oflag = nfs4_access_to_omode(open->op_share_access);
  4867. int access = nfs4_access_to_access(open->op_share_access);
  4868. unsigned char old_access_bmap, old_deny_bmap;
  4869. spin_lock(&fp->fi_lock);
  4870. /*
  4871. * Are we trying to set a deny mode that would conflict with
  4872. * current access?
  4873. */
  4874. status = nfs4_file_check_deny(fp, open->op_share_deny);
  4875. if (status != nfs_ok) {
  4876. if (status != nfserr_share_denied) {
  4877. spin_unlock(&fp->fi_lock);
  4878. goto out;
  4879. }
  4880. if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
  4881. stp, open->op_share_deny, false))
  4882. status = nfserr_jukebox;
  4883. spin_unlock(&fp->fi_lock);
  4884. goto out;
  4885. }
  4886. /* set access to the file */
  4887. status = nfs4_file_get_access(fp, open->op_share_access);
  4888. if (status != nfs_ok) {
  4889. if (status != nfserr_share_denied) {
  4890. spin_unlock(&fp->fi_lock);
  4891. goto out;
  4892. }
  4893. if (nfs4_resolve_deny_conflicts_locked(fp, new_stp,
  4894. stp, open->op_share_access, true))
  4895. status = nfserr_jukebox;
  4896. spin_unlock(&fp->fi_lock);
  4897. goto out;
  4898. }
  4899. /* Set access bits in stateid */
  4900. old_access_bmap = stp->st_access_bmap;
  4901. set_access(open->op_share_access, stp);
  4902. /* Set new deny mask */
  4903. old_deny_bmap = stp->st_deny_bmap;
  4904. set_deny(open->op_share_deny, stp);
  4905. fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
  4906. if (!fp->fi_fds[oflag]) {
  4907. spin_unlock(&fp->fi_lock);
  4908. status = nfsd_file_acquire_opened(rqstp, cur_fh, access,
  4909. open->op_filp, &nf);
  4910. if (status != nfs_ok)
  4911. goto out_put_access;
  4912. spin_lock(&fp->fi_lock);
  4913. if (!fp->fi_fds[oflag]) {
  4914. fp->fi_fds[oflag] = nf;
  4915. nf = NULL;
  4916. }
  4917. }
  4918. spin_unlock(&fp->fi_lock);
  4919. if (nf)
  4920. nfsd_file_put(nf);
  4921. status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode,
  4922. access));
  4923. if (status)
  4924. goto out_put_access;
  4925. status = nfsd4_truncate(rqstp, cur_fh, open);
  4926. if (status)
  4927. goto out_put_access;
  4928. out:
  4929. return status;
  4930. out_put_access:
  4931. stp->st_access_bmap = old_access_bmap;
  4932. nfs4_file_put_access(fp, open->op_share_access);
  4933. reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
  4934. goto out;
  4935. }
  4936. static __be32
  4937. nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp,
  4938. struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
  4939. struct nfsd4_open *open)
  4940. {
  4941. __be32 status;
  4942. unsigned char old_deny_bmap = stp->st_deny_bmap;
  4943. if (!test_access(open->op_share_access, stp))
  4944. return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open, false);
  4945. /* test and set deny mode */
  4946. spin_lock(&fp->fi_lock);
  4947. status = nfs4_file_check_deny(fp, open->op_share_deny);
  4948. switch (status) {
  4949. case nfs_ok:
  4950. set_deny(open->op_share_deny, stp);
  4951. fp->fi_share_deny |=
  4952. (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
  4953. break;
  4954. case nfserr_share_denied:
  4955. if (nfs4_resolve_deny_conflicts_locked(fp, false,
  4956. stp, open->op_share_deny, false))
  4957. status = nfserr_jukebox;
  4958. break;
  4959. }
  4960. spin_unlock(&fp->fi_lock);
  4961. if (status != nfs_ok)
  4962. return status;
  4963. status = nfsd4_truncate(rqstp, cur_fh, open);
  4964. if (status != nfs_ok)
  4965. reset_union_bmap_deny(old_deny_bmap, stp);
  4966. return status;
  4967. }
  4968. /* Should we give out recallable state?: */
  4969. static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
  4970. {
  4971. if (clp->cl_cb_state == NFSD4_CB_UP)
  4972. return true;
  4973. /*
  4974. * In the sessions case, since we don't have to establish a
  4975. * separate connection for callbacks, we assume it's OK
  4976. * until we hear otherwise:
  4977. */
  4978. return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
  4979. }
  4980. static struct file_lease *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
  4981. int flag)
  4982. {
  4983. struct file_lease *fl;
  4984. fl = locks_alloc_lease();
  4985. if (!fl)
  4986. return NULL;
  4987. fl->fl_lmops = &nfsd_lease_mng_ops;
  4988. fl->c.flc_flags = FL_DELEG;
  4989. fl->c.flc_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
  4990. fl->c.flc_owner = (fl_owner_t)dp;
  4991. fl->c.flc_pid = current->tgid;
  4992. fl->c.flc_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
  4993. return fl;
  4994. }
  4995. static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
  4996. struct nfs4_file *fp)
  4997. {
  4998. struct nfs4_ol_stateid *st;
  4999. struct file *f = fp->fi_deleg_file->nf_file;
  5000. struct inode *ino = file_inode(f);
  5001. int writes;
  5002. writes = atomic_read(&ino->i_writecount);
  5003. if (!writes)
  5004. return 0;
  5005. /*
  5006. * There could be multiple filehandles (hence multiple
  5007. * nfs4_files) referencing this file, but that's not too
  5008. * common; let's just give up in that case rather than
  5009. * trying to go look up all the clients using that other
  5010. * nfs4_file as well:
  5011. */
  5012. if (fp->fi_aliased)
  5013. return -EAGAIN;
  5014. /*
  5015. * If there's a close in progress, make sure that we see it
  5016. * clear any fi_fds[] entries before we see it decrement
  5017. * i_writecount:
  5018. */
  5019. smp_mb__after_atomic();
  5020. if (fp->fi_fds[O_WRONLY])
  5021. writes--;
  5022. if (fp->fi_fds[O_RDWR])
  5023. writes--;
  5024. if (writes > 0)
  5025. return -EAGAIN; /* There may be non-NFSv4 writers */
  5026. /*
  5027. * It's possible there are non-NFSv4 write opens in progress,
  5028. * but if they haven't incremented i_writecount yet then they
  5029. * also haven't called break lease yet; so, they'll break this
  5030. * lease soon enough. So, all that's left to check for is NFSv4
  5031. * opens:
  5032. */
  5033. spin_lock(&fp->fi_lock);
  5034. list_for_each_entry(st, &fp->fi_stateids, st_perfile) {
  5035. if (st->st_openstp == NULL /* it's an open */ &&
  5036. access_permit_write(st) &&
  5037. st->st_stid.sc_client != clp) {
  5038. spin_unlock(&fp->fi_lock);
  5039. return -EAGAIN;
  5040. }
  5041. }
  5042. spin_unlock(&fp->fi_lock);
  5043. /*
  5044. * There's a small chance that we could be racing with another
  5045. * NFSv4 open. However, any open that hasn't added itself to
  5046. * the fi_stateids list also hasn't called break_lease yet; so,
  5047. * they'll break this lease soon enough.
  5048. */
  5049. return 0;
  5050. }
  5051. /*
  5052. * It's possible that between opening the dentry and setting the delegation,
  5053. * that it has been renamed or unlinked. Redo the lookup to verify that this
  5054. * hasn't happened.
  5055. */
  5056. static int
  5057. nfsd4_verify_deleg_dentry(struct nfsd4_open *open, struct nfs4_file *fp,
  5058. struct svc_fh *parent)
  5059. {
  5060. struct svc_export *exp;
  5061. struct dentry *child;
  5062. __be32 err;
  5063. err = nfsd_lookup_dentry(open->op_rqstp, parent,
  5064. open->op_fname, open->op_fnamelen,
  5065. &exp, &child);
  5066. if (err)
  5067. return -EAGAIN;
  5068. exp_put(exp);
  5069. dput(child);
  5070. if (child != file_dentry(fp->fi_deleg_file->nf_file))
  5071. return -EAGAIN;
  5072. return 0;
  5073. }
  5074. /*
  5075. * We avoid breaking delegations held by a client due to its own activity, but
  5076. * clearing setuid/setgid bits on a write is an implicit activity and the client
  5077. * may not notice and continue using the old mode. Avoid giving out a delegation
  5078. * on setuid/setgid files when the client is requesting an open for write.
  5079. */
  5080. static int
  5081. nfsd4_verify_setuid_write(struct nfsd4_open *open, struct nfsd_file *nf)
  5082. {
  5083. struct inode *inode = file_inode(nf->nf_file);
  5084. if ((open->op_share_access & NFS4_SHARE_ACCESS_WRITE) &&
  5085. (inode->i_mode & (S_ISUID|S_ISGID)))
  5086. return -EAGAIN;
  5087. return 0;
  5088. }
  5089. static struct nfs4_delegation *
  5090. nfs4_set_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
  5091. struct svc_fh *parent)
  5092. {
  5093. int status = 0;
  5094. struct nfs4_client *clp = stp->st_stid.sc_client;
  5095. struct nfs4_file *fp = stp->st_stid.sc_file;
  5096. struct nfs4_clnt_odstate *odstate = stp->st_clnt_odstate;
  5097. struct nfs4_delegation *dp;
  5098. struct nfsd_file *nf = NULL;
  5099. struct file_lease *fl;
  5100. u32 dl_type;
  5101. /*
  5102. * The fi_had_conflict and nfs_get_existing_delegation checks
  5103. * here are just optimizations; we'll need to recheck them at
  5104. * the end:
  5105. */
  5106. if (fp->fi_had_conflict)
  5107. return ERR_PTR(-EAGAIN);
  5108. /*
  5109. * Try for a write delegation first. RFC8881 section 10.4 says:
  5110. *
  5111. * "An OPEN_DELEGATE_WRITE delegation allows the client to handle,
  5112. * on its own, all opens."
  5113. *
  5114. * Furthermore the client can use a write delegation for most READ
  5115. * operations as well, so we require a O_RDWR file here.
  5116. *
  5117. * Offer a write delegation in the case of a BOTH open, and ensure
  5118. * we get the O_RDWR descriptor.
  5119. */
  5120. if ((open->op_share_access & NFS4_SHARE_ACCESS_BOTH) == NFS4_SHARE_ACCESS_BOTH) {
  5121. nf = find_rw_file(fp);
  5122. dl_type = NFS4_OPEN_DELEGATE_WRITE;
  5123. }
  5124. /*
  5125. * If the file is being opened O_RDONLY or we couldn't get a O_RDWR
  5126. * file for some reason, then try for a read delegation instead.
  5127. */
  5128. if (!nf && (open->op_share_access & NFS4_SHARE_ACCESS_READ)) {
  5129. nf = find_readable_file(fp);
  5130. dl_type = NFS4_OPEN_DELEGATE_READ;
  5131. }
  5132. if (!nf)
  5133. return ERR_PTR(-EAGAIN);
  5134. spin_lock(&state_lock);
  5135. spin_lock(&fp->fi_lock);
  5136. if (nfs4_delegation_exists(clp, fp))
  5137. status = -EAGAIN;
  5138. else if (nfsd4_verify_setuid_write(open, nf))
  5139. status = -EAGAIN;
  5140. else if (!fp->fi_deleg_file) {
  5141. fp->fi_deleg_file = nf;
  5142. /* increment early to prevent fi_deleg_file from being
  5143. * cleared */
  5144. fp->fi_delegees = 1;
  5145. nf = NULL;
  5146. } else
  5147. fp->fi_delegees++;
  5148. spin_unlock(&fp->fi_lock);
  5149. spin_unlock(&state_lock);
  5150. if (nf)
  5151. nfsd_file_put(nf);
  5152. if (status)
  5153. return ERR_PTR(status);
  5154. status = -ENOMEM;
  5155. dp = alloc_init_deleg(clp, fp, odstate, dl_type);
  5156. if (!dp)
  5157. goto out_delegees;
  5158. fl = nfs4_alloc_init_lease(dp, dl_type);
  5159. if (!fl)
  5160. goto out_clnt_odstate;
  5161. status = kernel_setlease(fp->fi_deleg_file->nf_file,
  5162. fl->c.flc_type, &fl, NULL);
  5163. if (fl)
  5164. locks_free_lease(fl);
  5165. if (status)
  5166. goto out_clnt_odstate;
  5167. if (parent) {
  5168. status = nfsd4_verify_deleg_dentry(open, fp, parent);
  5169. if (status)
  5170. goto out_unlock;
  5171. }
  5172. status = nfsd4_check_conflicting_opens(clp, fp);
  5173. if (status)
  5174. goto out_unlock;
  5175. /*
  5176. * Now that the deleg is set, check again to ensure that nothing
  5177. * raced in and changed the mode while we weren't looking.
  5178. */
  5179. status = nfsd4_verify_setuid_write(open, fp->fi_deleg_file);
  5180. if (status)
  5181. goto out_unlock;
  5182. status = -EAGAIN;
  5183. if (fp->fi_had_conflict)
  5184. goto out_unlock;
  5185. spin_lock(&state_lock);
  5186. spin_lock(&clp->cl_lock);
  5187. spin_lock(&fp->fi_lock);
  5188. status = hash_delegation_locked(dp, fp);
  5189. spin_unlock(&fp->fi_lock);
  5190. spin_unlock(&clp->cl_lock);
  5191. spin_unlock(&state_lock);
  5192. if (status)
  5193. goto out_unlock;
  5194. return dp;
  5195. out_unlock:
  5196. kernel_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
  5197. out_clnt_odstate:
  5198. put_clnt_odstate(dp->dl_clnt_odstate);
  5199. nfs4_put_stid(&dp->dl_stid);
  5200. out_delegees:
  5201. put_deleg_file(fp);
  5202. return ERR_PTR(status);
  5203. }
  5204. static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
  5205. {
  5206. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  5207. if (status == -EAGAIN)
  5208. open->op_why_no_deleg = WND4_CONTENTION;
  5209. else {
  5210. open->op_why_no_deleg = WND4_RESOURCE;
  5211. switch (open->op_deleg_want) {
  5212. case NFS4_SHARE_WANT_READ_DELEG:
  5213. case NFS4_SHARE_WANT_WRITE_DELEG:
  5214. case NFS4_SHARE_WANT_ANY_DELEG:
  5215. break;
  5216. case NFS4_SHARE_WANT_CANCEL:
  5217. open->op_why_no_deleg = WND4_CANCELLED;
  5218. break;
  5219. case NFS4_SHARE_WANT_NO_DELEG:
  5220. WARN_ON_ONCE(1);
  5221. }
  5222. }
  5223. }
  5224. static bool
  5225. nfs4_delegation_stat(struct nfs4_delegation *dp, struct svc_fh *currentfh,
  5226. struct kstat *stat)
  5227. {
  5228. struct nfsd_file *nf = find_rw_file(dp->dl_stid.sc_file);
  5229. struct path path;
  5230. int rc;
  5231. if (!nf)
  5232. return false;
  5233. path.mnt = currentfh->fh_export->ex_path.mnt;
  5234. path.dentry = file_dentry(nf->nf_file);
  5235. rc = vfs_getattr(&path, stat,
  5236. (STATX_MODE | STATX_SIZE | STATX_CTIME | STATX_CHANGE_COOKIE),
  5237. AT_STATX_SYNC_AS_STAT);
  5238. nfsd_file_put(nf);
  5239. return rc == 0;
  5240. }
  5241. /*
  5242. * The Linux NFS server does not offer write delegations to NFSv4.0
  5243. * clients in order to avoid conflicts between write delegations and
  5244. * GETATTRs requesting CHANGE or SIZE attributes.
  5245. *
  5246. * With NFSv4.1 and later minorversions, the SEQUENCE operation that
  5247. * begins each COMPOUND contains a client ID. Delegation recall can
  5248. * be avoided when the server recognizes the client sending a
  5249. * GETATTR also holds write delegation it conflicts with.
  5250. *
  5251. * However, the NFSv4.0 protocol does not enable a server to
  5252. * determine that a GETATTR originated from the client holding the
  5253. * conflicting delegation versus coming from some other client. Per
  5254. * RFC 7530 Section 16.7.5, the server must recall or send a
  5255. * CB_GETATTR even when the GETATTR originates from the client that
  5256. * holds the conflicting delegation.
  5257. *
  5258. * An NFSv4.0 client can trigger a pathological situation if it
  5259. * always sends a DELEGRETURN preceded by a conflicting GETATTR in
  5260. * the same COMPOUND. COMPOUND execution will always stop at the
  5261. * GETATTR and the DELEGRETURN will never get executed. The server
  5262. * eventually revokes the delegation, which can result in loss of
  5263. * open or lock state.
  5264. */
  5265. static void
  5266. nfs4_open_delegation(struct nfsd4_open *open, struct nfs4_ol_stateid *stp,
  5267. struct svc_fh *currentfh)
  5268. {
  5269. struct nfs4_delegation *dp;
  5270. struct nfs4_openowner *oo = openowner(stp->st_stateowner);
  5271. struct nfs4_client *clp = stp->st_stid.sc_client;
  5272. struct svc_fh *parent = NULL;
  5273. int cb_up;
  5274. int status = 0;
  5275. struct kstat stat;
  5276. cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
  5277. open->op_recall = false;
  5278. switch (open->op_claim_type) {
  5279. case NFS4_OPEN_CLAIM_PREVIOUS:
  5280. if (!cb_up)
  5281. open->op_recall = true;
  5282. break;
  5283. case NFS4_OPEN_CLAIM_NULL:
  5284. parent = currentfh;
  5285. fallthrough;
  5286. case NFS4_OPEN_CLAIM_FH:
  5287. /*
  5288. * Let's not give out any delegations till everyone's
  5289. * had the chance to reclaim theirs, *and* until
  5290. * NLM locks have all been reclaimed:
  5291. */
  5292. if (locks_in_grace(clp->net))
  5293. goto out_no_deleg;
  5294. if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
  5295. goto out_no_deleg;
  5296. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE &&
  5297. !clp->cl_minorversion)
  5298. goto out_no_deleg;
  5299. break;
  5300. default:
  5301. goto out_no_deleg;
  5302. }
  5303. dp = nfs4_set_delegation(open, stp, parent);
  5304. if (IS_ERR(dp))
  5305. goto out_no_deleg;
  5306. memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
  5307. if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) {
  5308. if (!nfs4_delegation_stat(dp, currentfh, &stat)) {
  5309. nfs4_put_stid(&dp->dl_stid);
  5310. destroy_delegation(dp);
  5311. goto out_no_deleg;
  5312. }
  5313. open->op_delegate_type = NFS4_OPEN_DELEGATE_WRITE;
  5314. dp->dl_cb_fattr.ncf_cur_fsize = stat.size;
  5315. dp->dl_cb_fattr.ncf_initial_cinfo = nfsd4_change_attribute(&stat);
  5316. trace_nfsd_deleg_write(&dp->dl_stid.sc_stateid);
  5317. } else {
  5318. open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
  5319. trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
  5320. }
  5321. nfs4_put_stid(&dp->dl_stid);
  5322. return;
  5323. out_no_deleg:
  5324. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
  5325. if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
  5326. open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
  5327. dprintk("NFSD: WARNING: refusing delegation reclaim\n");
  5328. open->op_recall = true;
  5329. }
  5330. /* 4.1 client asking for a delegation? */
  5331. if (open->op_deleg_want)
  5332. nfsd4_open_deleg_none_ext(open, status);
  5333. return;
  5334. }
  5335. static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
  5336. struct nfs4_delegation *dp)
  5337. {
  5338. if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
  5339. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  5340. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  5341. open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
  5342. } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
  5343. dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
  5344. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  5345. open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
  5346. }
  5347. /* Otherwise the client must be confused wanting a delegation
  5348. * it already has, therefore we don't return
  5349. * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
  5350. */
  5351. }
  5352. /**
  5353. * nfsd4_process_open2 - finish open processing
  5354. * @rqstp: the RPC transaction being executed
  5355. * @current_fh: NFSv4 COMPOUND's current filehandle
  5356. * @open: OPEN arguments
  5357. *
  5358. * If successful, (1) truncate the file if open->op_truncate was
  5359. * set, (2) set open->op_stateid, (3) set open->op_delegation.
  5360. *
  5361. * Returns %nfs_ok on success; otherwise an nfs4stat value in
  5362. * network byte order is returned.
  5363. */
  5364. __be32
  5365. nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
  5366. {
  5367. struct nfsd4_compoundres *resp = rqstp->rq_resp;
  5368. struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
  5369. struct nfs4_file *fp = NULL;
  5370. struct nfs4_ol_stateid *stp = NULL;
  5371. struct nfs4_delegation *dp = NULL;
  5372. __be32 status;
  5373. bool new_stp = false;
  5374. /*
  5375. * Lookup file; if found, lookup stateid and check open request,
  5376. * and check for delegations in the process of being recalled.
  5377. * If not found, create the nfs4_file struct
  5378. */
  5379. fp = nfsd4_file_hash_insert(open->op_file, current_fh);
  5380. if (unlikely(!fp))
  5381. return nfserr_jukebox;
  5382. if (fp != open->op_file) {
  5383. status = nfs4_check_deleg(cl, open, &dp);
  5384. if (status)
  5385. goto out;
  5386. if (dp && nfsd4_is_deleg_cur(open) &&
  5387. (dp->dl_stid.sc_file != fp)) {
  5388. /*
  5389. * RFC8881 section 8.2.4 mandates the server to return
  5390. * NFS4ERR_BAD_STATEID if the selected table entry does
  5391. * not match the current filehandle. However returning
  5392. * NFS4ERR_BAD_STATEID in the OPEN can cause the client
  5393. * to repeatedly retry the operation with the same
  5394. * stateid, since the stateid itself is valid. To avoid
  5395. * this situation NFSD returns NFS4ERR_INVAL instead.
  5396. */
  5397. status = nfserr_inval;
  5398. goto out;
  5399. }
  5400. stp = nfsd4_find_and_lock_existing_open(fp, open);
  5401. } else {
  5402. open->op_file = NULL;
  5403. status = nfserr_bad_stateid;
  5404. if (nfsd4_is_deleg_cur(open))
  5405. goto out;
  5406. }
  5407. if (!stp) {
  5408. stp = init_open_stateid(fp, open);
  5409. if (!stp) {
  5410. status = nfserr_jukebox;
  5411. goto out;
  5412. }
  5413. if (!open->op_stp)
  5414. new_stp = true;
  5415. }
  5416. /*
  5417. * OPEN the file, or upgrade an existing OPEN.
  5418. * If truncate fails, the OPEN fails.
  5419. *
  5420. * stp is already locked.
  5421. */
  5422. if (!new_stp) {
  5423. /* Stateid was found, this is an OPEN upgrade */
  5424. status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
  5425. if (status) {
  5426. mutex_unlock(&stp->st_mutex);
  5427. goto out;
  5428. }
  5429. } else {
  5430. status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open, true);
  5431. if (status) {
  5432. release_open_stateid(stp);
  5433. mutex_unlock(&stp->st_mutex);
  5434. goto out;
  5435. }
  5436. stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
  5437. open->op_odstate);
  5438. if (stp->st_clnt_odstate == open->op_odstate)
  5439. open->op_odstate = NULL;
  5440. }
  5441. nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
  5442. mutex_unlock(&stp->st_mutex);
  5443. if (nfsd4_has_session(&resp->cstate)) {
  5444. if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
  5445. open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
  5446. open->op_why_no_deleg = WND4_NOT_WANTED;
  5447. goto nodeleg;
  5448. }
  5449. }
  5450. /*
  5451. * Attempt to hand out a delegation. No error return, because the
  5452. * OPEN succeeds even if we fail.
  5453. */
  5454. nfs4_open_delegation(open, stp, &resp->cstate.current_fh);
  5455. nodeleg:
  5456. status = nfs_ok;
  5457. trace_nfsd_open(&stp->st_stid.sc_stateid);
  5458. out:
  5459. /* 4.1 client trying to upgrade/downgrade delegation? */
  5460. if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
  5461. open->op_deleg_want)
  5462. nfsd4_deleg_xgrade_none_ext(open, dp);
  5463. if (fp)
  5464. put_nfs4_file(fp);
  5465. if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
  5466. open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
  5467. /*
  5468. * To finish the open response, we just need to set the rflags.
  5469. */
  5470. open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
  5471. if (nfsd4_has_session(&resp->cstate))
  5472. open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
  5473. else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
  5474. open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
  5475. if (dp)
  5476. nfs4_put_stid(&dp->dl_stid);
  5477. if (stp)
  5478. nfs4_put_stid(&stp->st_stid);
  5479. return status;
  5480. }
  5481. void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
  5482. struct nfsd4_open *open)
  5483. {
  5484. if (open->op_openowner)
  5485. nfs4_put_stateowner(&open->op_openowner->oo_owner);
  5486. if (open->op_file)
  5487. kmem_cache_free(file_slab, open->op_file);
  5488. if (open->op_stp)
  5489. nfs4_put_stid(&open->op_stp->st_stid);
  5490. if (open->op_odstate)
  5491. kmem_cache_free(odstate_slab, open->op_odstate);
  5492. }
  5493. __be32
  5494. nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  5495. union nfsd4_op_u *u)
  5496. {
  5497. clientid_t *clid = &u->renew;
  5498. struct nfs4_client *clp;
  5499. __be32 status;
  5500. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  5501. trace_nfsd_clid_renew(clid);
  5502. status = set_client(clid, cstate, nn);
  5503. if (status)
  5504. return status;
  5505. clp = cstate->clp;
  5506. if (!list_empty(&clp->cl_delegations)
  5507. && clp->cl_cb_state != NFSD4_CB_UP)
  5508. return nfserr_cb_path_down;
  5509. return nfs_ok;
  5510. }
  5511. void
  5512. nfsd4_end_grace(struct nfsd_net *nn)
  5513. {
  5514. /* do nothing if grace period already ended */
  5515. if (nn->grace_ended)
  5516. return;
  5517. trace_nfsd_grace_complete(nn);
  5518. nn->grace_ended = true;
  5519. /*
  5520. * If the server goes down again right now, an NFSv4
  5521. * client will still be allowed to reclaim after it comes back up,
  5522. * even if it hasn't yet had a chance to reclaim state this time.
  5523. *
  5524. */
  5525. nfsd4_record_grace_done(nn);
  5526. /*
  5527. * At this point, NFSv4 clients can still reclaim. But if the
  5528. * server crashes, any that have not yet reclaimed will be out
  5529. * of luck on the next boot.
  5530. *
  5531. * (NFSv4.1+ clients are considered to have reclaimed once they
  5532. * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
  5533. * have reclaimed after their first OPEN.)
  5534. */
  5535. locks_end_grace(&nn->nfsd4_manager);
  5536. /*
  5537. * At this point, and once lockd and/or any other containers
  5538. * exit their grace period, further reclaims will fail and
  5539. * regular locking can resume.
  5540. */
  5541. }
  5542. /*
  5543. * If we've waited a lease period but there are still clients trying to
  5544. * reclaim, wait a little longer to give them a chance to finish.
  5545. */
  5546. static bool clients_still_reclaiming(struct nfsd_net *nn)
  5547. {
  5548. time64_t double_grace_period_end = nn->boot_time +
  5549. 2 * nn->nfsd4_lease;
  5550. if (nn->track_reclaim_completes &&
  5551. atomic_read(&nn->nr_reclaim_complete) ==
  5552. nn->reclaim_str_hashtbl_size)
  5553. return false;
  5554. if (!nn->somebody_reclaimed)
  5555. return false;
  5556. nn->somebody_reclaimed = false;
  5557. /*
  5558. * If we've given them *two* lease times to reclaim, and they're
  5559. * still not done, give up:
  5560. */
  5561. if (ktime_get_boottime_seconds() > double_grace_period_end)
  5562. return false;
  5563. return true;
  5564. }
  5565. struct laundry_time {
  5566. time64_t cutoff;
  5567. time64_t new_timeo;
  5568. };
  5569. static bool state_expired(struct laundry_time *lt, time64_t last_refresh)
  5570. {
  5571. time64_t time_remaining;
  5572. if (last_refresh < lt->cutoff)
  5573. return true;
  5574. time_remaining = last_refresh - lt->cutoff;
  5575. lt->new_timeo = min(lt->new_timeo, time_remaining);
  5576. return false;
  5577. }
  5578. #ifdef CONFIG_NFSD_V4_2_INTER_SSC
  5579. void nfsd4_ssc_init_umount_work(struct nfsd_net *nn)
  5580. {
  5581. spin_lock_init(&nn->nfsd_ssc_lock);
  5582. INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list);
  5583. init_waitqueue_head(&nn->nfsd_ssc_waitq);
  5584. }
  5585. /*
  5586. * This is called when nfsd is being shutdown, after all inter_ssc
  5587. * cleanup were done, to destroy the ssc delayed unmount list.
  5588. */
  5589. static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn)
  5590. {
  5591. struct nfsd4_ssc_umount_item *ni = NULL;
  5592. struct nfsd4_ssc_umount_item *tmp;
  5593. spin_lock(&nn->nfsd_ssc_lock);
  5594. list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
  5595. list_del(&ni->nsui_list);
  5596. spin_unlock(&nn->nfsd_ssc_lock);
  5597. mntput(ni->nsui_vfsmount);
  5598. kfree(ni);
  5599. spin_lock(&nn->nfsd_ssc_lock);
  5600. }
  5601. spin_unlock(&nn->nfsd_ssc_lock);
  5602. }
  5603. static void nfsd4_ssc_expire_umount(struct nfsd_net *nn)
  5604. {
  5605. bool do_wakeup = false;
  5606. struct nfsd4_ssc_umount_item *ni = NULL;
  5607. struct nfsd4_ssc_umount_item *tmp;
  5608. spin_lock(&nn->nfsd_ssc_lock);
  5609. list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) {
  5610. if (time_after(jiffies, ni->nsui_expire)) {
  5611. if (refcount_read(&ni->nsui_refcnt) > 1)
  5612. continue;
  5613. /* mark being unmount */
  5614. ni->nsui_busy = true;
  5615. spin_unlock(&nn->nfsd_ssc_lock);
  5616. mntput(ni->nsui_vfsmount);
  5617. spin_lock(&nn->nfsd_ssc_lock);
  5618. /* waiters need to start from begin of list */
  5619. list_del(&ni->nsui_list);
  5620. kfree(ni);
  5621. /* wakeup ssc_connect waiters */
  5622. do_wakeup = true;
  5623. continue;
  5624. }
  5625. break;
  5626. }
  5627. if (do_wakeup)
  5628. wake_up_all(&nn->nfsd_ssc_waitq);
  5629. spin_unlock(&nn->nfsd_ssc_lock);
  5630. }
  5631. #endif
  5632. /* Check if any lock belonging to this lockowner has any blockers */
  5633. static bool
  5634. nfs4_lockowner_has_blockers(struct nfs4_lockowner *lo)
  5635. {
  5636. struct file_lock_context *ctx;
  5637. struct nfs4_ol_stateid *stp;
  5638. struct nfs4_file *nf;
  5639. list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
  5640. nf = stp->st_stid.sc_file;
  5641. ctx = locks_inode_context(nf->fi_inode);
  5642. if (!ctx)
  5643. continue;
  5644. if (locks_owner_has_blockers(ctx, lo))
  5645. return true;
  5646. }
  5647. return false;
  5648. }
  5649. static bool
  5650. nfs4_anylock_blockers(struct nfs4_client *clp)
  5651. {
  5652. int i;
  5653. struct nfs4_stateowner *so;
  5654. struct nfs4_lockowner *lo;
  5655. if (atomic_read(&clp->cl_delegs_in_recall))
  5656. return true;
  5657. spin_lock(&clp->cl_lock);
  5658. for (i = 0; i < OWNER_HASH_SIZE; i++) {
  5659. list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[i],
  5660. so_strhash) {
  5661. if (so->so_is_open_owner)
  5662. continue;
  5663. lo = lockowner(so);
  5664. if (nfs4_lockowner_has_blockers(lo)) {
  5665. spin_unlock(&clp->cl_lock);
  5666. return true;
  5667. }
  5668. }
  5669. }
  5670. spin_unlock(&clp->cl_lock);
  5671. return false;
  5672. }
  5673. static void
  5674. nfs4_get_client_reaplist(struct nfsd_net *nn, struct list_head *reaplist,
  5675. struct laundry_time *lt)
  5676. {
  5677. unsigned int maxreap, reapcnt = 0;
  5678. struct list_head *pos, *next;
  5679. struct nfs4_client *clp;
  5680. maxreap = (atomic_read(&nn->nfs4_client_count) >= nn->nfs4_max_clients) ?
  5681. NFSD_CLIENT_MAX_TRIM_PER_RUN : 0;
  5682. INIT_LIST_HEAD(reaplist);
  5683. spin_lock(&nn->client_lock);
  5684. list_for_each_safe(pos, next, &nn->client_lru) {
  5685. clp = list_entry(pos, struct nfs4_client, cl_lru);
  5686. if (clp->cl_state == NFSD4_EXPIRABLE)
  5687. goto exp_client;
  5688. if (!state_expired(lt, clp->cl_time))
  5689. break;
  5690. if (!atomic_read(&clp->cl_rpc_users)) {
  5691. if (clp->cl_state == NFSD4_ACTIVE)
  5692. atomic_inc(&nn->nfsd_courtesy_clients);
  5693. clp->cl_state = NFSD4_COURTESY;
  5694. }
  5695. if (!client_has_state(clp))
  5696. goto exp_client;
  5697. if (!nfs4_anylock_blockers(clp))
  5698. if (reapcnt >= maxreap)
  5699. continue;
  5700. exp_client:
  5701. if (!mark_client_expired_locked(clp)) {
  5702. list_add(&clp->cl_lru, reaplist);
  5703. reapcnt++;
  5704. }
  5705. }
  5706. spin_unlock(&nn->client_lock);
  5707. }
  5708. static void
  5709. nfs4_get_courtesy_client_reaplist(struct nfsd_net *nn,
  5710. struct list_head *reaplist)
  5711. {
  5712. unsigned int maxreap = 0, reapcnt = 0;
  5713. struct list_head *pos, *next;
  5714. struct nfs4_client *clp;
  5715. maxreap = NFSD_CLIENT_MAX_TRIM_PER_RUN;
  5716. INIT_LIST_HEAD(reaplist);
  5717. spin_lock(&nn->client_lock);
  5718. list_for_each_safe(pos, next, &nn->client_lru) {
  5719. clp = list_entry(pos, struct nfs4_client, cl_lru);
  5720. if (clp->cl_state == NFSD4_ACTIVE)
  5721. break;
  5722. if (reapcnt >= maxreap)
  5723. break;
  5724. if (!mark_client_expired_locked(clp)) {
  5725. list_add(&clp->cl_lru, reaplist);
  5726. reapcnt++;
  5727. }
  5728. }
  5729. spin_unlock(&nn->client_lock);
  5730. }
  5731. static void
  5732. nfs4_process_client_reaplist(struct list_head *reaplist)
  5733. {
  5734. struct list_head *pos, *next;
  5735. struct nfs4_client *clp;
  5736. list_for_each_safe(pos, next, reaplist) {
  5737. clp = list_entry(pos, struct nfs4_client, cl_lru);
  5738. trace_nfsd_clid_purged(&clp->cl_clientid);
  5739. list_del_init(&clp->cl_lru);
  5740. expire_client(clp);
  5741. }
  5742. }
  5743. static void nfs40_clean_admin_revoked(struct nfsd_net *nn,
  5744. struct laundry_time *lt)
  5745. {
  5746. struct nfs4_client *clp;
  5747. spin_lock(&nn->client_lock);
  5748. if (nn->nfs40_last_revoke == 0 ||
  5749. nn->nfs40_last_revoke > lt->cutoff) {
  5750. spin_unlock(&nn->client_lock);
  5751. return;
  5752. }
  5753. nn->nfs40_last_revoke = 0;
  5754. retry:
  5755. list_for_each_entry(clp, &nn->client_lru, cl_lru) {
  5756. unsigned long id, tmp;
  5757. struct nfs4_stid *stid;
  5758. if (atomic_read(&clp->cl_admin_revoked) == 0)
  5759. continue;
  5760. spin_lock(&clp->cl_lock);
  5761. idr_for_each_entry_ul(&clp->cl_stateids, stid, tmp, id)
  5762. if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) {
  5763. refcount_inc(&stid->sc_count);
  5764. spin_unlock(&nn->client_lock);
  5765. /* this function drops ->cl_lock */
  5766. nfsd4_drop_revoked_stid(stid);
  5767. nfs4_put_stid(stid);
  5768. spin_lock(&nn->client_lock);
  5769. goto retry;
  5770. }
  5771. spin_unlock(&clp->cl_lock);
  5772. }
  5773. spin_unlock(&nn->client_lock);
  5774. }
  5775. static time64_t
  5776. nfs4_laundromat(struct nfsd_net *nn)
  5777. {
  5778. struct nfs4_openowner *oo;
  5779. struct nfs4_delegation *dp;
  5780. struct nfs4_ol_stateid *stp;
  5781. struct nfsd4_blocked_lock *nbl;
  5782. struct list_head *pos, *next, reaplist;
  5783. struct laundry_time lt = {
  5784. .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease,
  5785. .new_timeo = nn->nfsd4_lease
  5786. };
  5787. struct nfs4_cpntf_state *cps;
  5788. copy_stateid_t *cps_t;
  5789. int i;
  5790. if (clients_still_reclaiming(nn)) {
  5791. lt.new_timeo = 0;
  5792. goto out;
  5793. }
  5794. nfsd4_end_grace(nn);
  5795. spin_lock(&nn->s2s_cp_lock);
  5796. idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
  5797. cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
  5798. if (cps->cp_stateid.cs_type == NFS4_COPYNOTIFY_STID &&
  5799. state_expired(&lt, cps->cpntf_time))
  5800. _free_cpntf_state_locked(nn, cps);
  5801. }
  5802. spin_unlock(&nn->s2s_cp_lock);
  5803. nfs4_get_client_reaplist(nn, &reaplist, &lt);
  5804. nfs4_process_client_reaplist(&reaplist);
  5805. nfs40_clean_admin_revoked(nn, &lt);
  5806. spin_lock(&state_lock);
  5807. list_for_each_safe(pos, next, &nn->del_recall_lru) {
  5808. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  5809. if (!state_expired(&lt, dp->dl_time))
  5810. break;
  5811. refcount_inc(&dp->dl_stid.sc_count);
  5812. unhash_delegation_locked(dp, SC_STATUS_REVOKED);
  5813. list_add(&dp->dl_recall_lru, &reaplist);
  5814. }
  5815. spin_unlock(&state_lock);
  5816. while (!list_empty(&reaplist)) {
  5817. dp = list_first_entry(&reaplist, struct nfs4_delegation,
  5818. dl_recall_lru);
  5819. list_del_init(&dp->dl_recall_lru);
  5820. revoke_delegation(dp);
  5821. }
  5822. spin_lock(&nn->client_lock);
  5823. while (!list_empty(&nn->close_lru)) {
  5824. oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
  5825. oo_close_lru);
  5826. if (!state_expired(&lt, oo->oo_time))
  5827. break;
  5828. list_del_init(&oo->oo_close_lru);
  5829. stp = oo->oo_last_closed_stid;
  5830. oo->oo_last_closed_stid = NULL;
  5831. spin_unlock(&nn->client_lock);
  5832. nfs4_put_stid(&stp->st_stid);
  5833. spin_lock(&nn->client_lock);
  5834. }
  5835. spin_unlock(&nn->client_lock);
  5836. /*
  5837. * It's possible for a client to try and acquire an already held lock
  5838. * that is being held for a long time, and then lose interest in it.
  5839. * So, we clean out any un-revisited request after a lease period
  5840. * under the assumption that the client is no longer interested.
  5841. *
  5842. * RFC5661, sec. 9.6 states that the client must not rely on getting
  5843. * notifications and must continue to poll for locks, even when the
  5844. * server supports them. Thus this shouldn't lead to clients blocking
  5845. * indefinitely once the lock does become free.
  5846. */
  5847. BUG_ON(!list_empty(&reaplist));
  5848. spin_lock(&nn->blocked_locks_lock);
  5849. while (!list_empty(&nn->blocked_locks_lru)) {
  5850. nbl = list_first_entry(&nn->blocked_locks_lru,
  5851. struct nfsd4_blocked_lock, nbl_lru);
  5852. if (!state_expired(&lt, nbl->nbl_time))
  5853. break;
  5854. list_move(&nbl->nbl_lru, &reaplist);
  5855. list_del_init(&nbl->nbl_list);
  5856. }
  5857. spin_unlock(&nn->blocked_locks_lock);
  5858. while (!list_empty(&reaplist)) {
  5859. nbl = list_first_entry(&reaplist,
  5860. struct nfsd4_blocked_lock, nbl_lru);
  5861. list_del_init(&nbl->nbl_lru);
  5862. free_blocked_lock(nbl);
  5863. }
  5864. #ifdef CONFIG_NFSD_V4_2_INTER_SSC
  5865. /* service the server-to-server copy delayed unmount list */
  5866. nfsd4_ssc_expire_umount(nn);
  5867. #endif
  5868. if (atomic_long_read(&num_delegations) >= max_delegations)
  5869. deleg_reaper(nn);
  5870. out:
  5871. return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
  5872. }
  5873. static void laundromat_main(struct work_struct *);
  5874. static void
  5875. laundromat_main(struct work_struct *laundry)
  5876. {
  5877. time64_t t;
  5878. struct delayed_work *dwork = to_delayed_work(laundry);
  5879. struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
  5880. laundromat_work);
  5881. t = nfs4_laundromat(nn);
  5882. queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
  5883. }
  5884. static void
  5885. courtesy_client_reaper(struct nfsd_net *nn)
  5886. {
  5887. struct list_head reaplist;
  5888. nfs4_get_courtesy_client_reaplist(nn, &reaplist);
  5889. nfs4_process_client_reaplist(&reaplist);
  5890. }
  5891. static void
  5892. deleg_reaper(struct nfsd_net *nn)
  5893. {
  5894. struct list_head *pos, *next;
  5895. struct nfs4_client *clp;
  5896. LIST_HEAD(cblist);
  5897. spin_lock(&nn->client_lock);
  5898. list_for_each_safe(pos, next, &nn->client_lru) {
  5899. clp = list_entry(pos, struct nfs4_client, cl_lru);
  5900. if (clp->cl_state != NFSD4_ACTIVE)
  5901. continue;
  5902. if (list_empty(&clp->cl_delegations))
  5903. continue;
  5904. if (atomic_read(&clp->cl_delegs_in_recall))
  5905. continue;
  5906. if (test_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags))
  5907. continue;
  5908. if (ktime_get_boottime_seconds() - clp->cl_ra_time < 5)
  5909. continue;
  5910. if (clp->cl_cb_state != NFSD4_CB_UP)
  5911. continue;
  5912. list_add(&clp->cl_ra_cblist, &cblist);
  5913. /* release in nfsd4_cb_recall_any_release */
  5914. kref_get(&clp->cl_nfsdfs.cl_ref);
  5915. set_bit(NFSD4_CLIENT_CB_RECALL_ANY, &clp->cl_flags);
  5916. clp->cl_ra_time = ktime_get_boottime_seconds();
  5917. }
  5918. spin_unlock(&nn->client_lock);
  5919. while (!list_empty(&cblist)) {
  5920. clp = list_first_entry(&cblist, struct nfs4_client,
  5921. cl_ra_cblist);
  5922. list_del_init(&clp->cl_ra_cblist);
  5923. clp->cl_ra->ra_keep = 0;
  5924. clp->cl_ra->ra_bmval[0] = BIT(RCA4_TYPE_MASK_RDATA_DLG) |
  5925. BIT(RCA4_TYPE_MASK_WDATA_DLG);
  5926. trace_nfsd_cb_recall_any(clp->cl_ra);
  5927. nfsd4_run_cb(&clp->cl_ra->ra_cb);
  5928. }
  5929. }
  5930. static void
  5931. nfsd4_state_shrinker_worker(struct work_struct *work)
  5932. {
  5933. struct nfsd_net *nn = container_of(work, struct nfsd_net,
  5934. nfsd_shrinker_work);
  5935. courtesy_client_reaper(nn);
  5936. deleg_reaper(nn);
  5937. }
  5938. static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
  5939. {
  5940. if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
  5941. return nfserr_bad_stateid;
  5942. return nfs_ok;
  5943. }
  5944. static
  5945. __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
  5946. {
  5947. __be32 status = nfserr_openmode;
  5948. /* For lock stateid's, we test the parent open, not the lock: */
  5949. if (stp->st_openstp)
  5950. stp = stp->st_openstp;
  5951. if ((flags & WR_STATE) && !access_permit_write(stp))
  5952. goto out;
  5953. if ((flags & RD_STATE) && !access_permit_read(stp))
  5954. goto out;
  5955. status = nfs_ok;
  5956. out:
  5957. return status;
  5958. }
  5959. static inline __be32
  5960. check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
  5961. {
  5962. if (ONE_STATEID(stateid) && (flags & RD_STATE))
  5963. return nfs_ok;
  5964. else if (opens_in_grace(net)) {
  5965. /* Answer in remaining cases depends on existence of
  5966. * conflicting state; so we must wait out the grace period. */
  5967. return nfserr_grace;
  5968. } else if (flags & WR_STATE)
  5969. return nfs4_share_conflict(current_fh,
  5970. NFS4_SHARE_DENY_WRITE);
  5971. else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
  5972. return nfs4_share_conflict(current_fh,
  5973. NFS4_SHARE_DENY_READ);
  5974. }
  5975. static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
  5976. {
  5977. /*
  5978. * When sessions are used the stateid generation number is ignored
  5979. * when it is zero.
  5980. */
  5981. if (has_session && in->si_generation == 0)
  5982. return nfs_ok;
  5983. if (in->si_generation == ref->si_generation)
  5984. return nfs_ok;
  5985. /* If the client sends us a stateid from the future, it's buggy: */
  5986. if (nfsd4_stateid_generation_after(in, ref))
  5987. return nfserr_bad_stateid;
  5988. /*
  5989. * However, we could see a stateid from the past, even from a
  5990. * non-buggy client. For example, if the client sends a lock
  5991. * while some IO is outstanding, the lock may bump si_generation
  5992. * while the IO is still in flight. The client could avoid that
  5993. * situation by waiting for responses on all the IO requests,
  5994. * but better performance may result in retrying IO that
  5995. * receives an old_stateid error if requests are rarely
  5996. * reordered in flight:
  5997. */
  5998. return nfserr_old_stateid;
  5999. }
  6000. static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
  6001. {
  6002. __be32 ret;
  6003. spin_lock(&s->sc_lock);
  6004. ret = nfsd4_verify_open_stid(s);
  6005. if (ret == nfs_ok)
  6006. ret = check_stateid_generation(in, &s->sc_stateid, has_session);
  6007. spin_unlock(&s->sc_lock);
  6008. if (ret == nfserr_admin_revoked)
  6009. nfsd40_drop_revoked_stid(s->sc_client,
  6010. &s->sc_stateid);
  6011. return ret;
  6012. }
  6013. static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
  6014. {
  6015. if (ols->st_stateowner->so_is_open_owner &&
  6016. !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
  6017. return nfserr_bad_stateid;
  6018. return nfs_ok;
  6019. }
  6020. static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
  6021. {
  6022. struct nfs4_stid *s;
  6023. __be32 status = nfserr_bad_stateid;
  6024. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
  6025. CLOSE_STATEID(stateid))
  6026. return status;
  6027. spin_lock(&cl->cl_lock);
  6028. s = find_stateid_locked(cl, stateid);
  6029. if (!s)
  6030. goto out_unlock;
  6031. status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
  6032. if (status)
  6033. goto out_unlock;
  6034. status = nfsd4_verify_open_stid(s);
  6035. if (status)
  6036. goto out_unlock;
  6037. switch (s->sc_type) {
  6038. case SC_TYPE_DELEG:
  6039. status = nfs_ok;
  6040. break;
  6041. case SC_TYPE_OPEN:
  6042. case SC_TYPE_LOCK:
  6043. status = nfsd4_check_openowner_confirmed(openlockstateid(s));
  6044. break;
  6045. default:
  6046. printk("unknown stateid type %x\n", s->sc_type);
  6047. status = nfserr_bad_stateid;
  6048. }
  6049. out_unlock:
  6050. spin_unlock(&cl->cl_lock);
  6051. if (status == nfserr_admin_revoked)
  6052. nfsd40_drop_revoked_stid(cl, stateid);
  6053. return status;
  6054. }
  6055. __be32
  6056. nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
  6057. stateid_t *stateid,
  6058. unsigned short typemask, unsigned short statusmask,
  6059. struct nfs4_stid **s, struct nfsd_net *nn)
  6060. {
  6061. __be32 status;
  6062. struct nfs4_stid *stid;
  6063. bool return_revoked = false;
  6064. /*
  6065. * only return revoked delegations if explicitly asked.
  6066. * otherwise we report revoked or bad_stateid status.
  6067. */
  6068. if (statusmask & SC_STATUS_REVOKED)
  6069. return_revoked = true;
  6070. if (typemask & SC_TYPE_DELEG)
  6071. /* Always allow REVOKED for DELEG so we can
  6072. * retturn the appropriate error.
  6073. */
  6074. statusmask |= SC_STATUS_REVOKED;
  6075. statusmask |= SC_STATUS_ADMIN_REVOKED | SC_STATUS_FREEABLE;
  6076. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
  6077. CLOSE_STATEID(stateid))
  6078. return nfserr_bad_stateid;
  6079. status = set_client(&stateid->si_opaque.so_clid, cstate, nn);
  6080. if (status == nfserr_stale_clientid) {
  6081. if (cstate->session)
  6082. return nfserr_bad_stateid;
  6083. return nfserr_stale_stateid;
  6084. }
  6085. if (status)
  6086. return status;
  6087. stid = find_stateid_by_type(cstate->clp, stateid, typemask, statusmask);
  6088. if (!stid)
  6089. return nfserr_bad_stateid;
  6090. if ((stid->sc_status & SC_STATUS_REVOKED) && !return_revoked) {
  6091. nfs4_put_stid(stid);
  6092. return nfserr_deleg_revoked;
  6093. }
  6094. if (stid->sc_status & SC_STATUS_ADMIN_REVOKED) {
  6095. nfsd40_drop_revoked_stid(cstate->clp, stateid);
  6096. nfs4_put_stid(stid);
  6097. return nfserr_admin_revoked;
  6098. }
  6099. *s = stid;
  6100. return nfs_ok;
  6101. }
  6102. static struct nfsd_file *
  6103. nfs4_find_file(struct nfs4_stid *s, int flags)
  6104. {
  6105. struct nfsd_file *ret = NULL;
  6106. if (!s || s->sc_status)
  6107. return NULL;
  6108. switch (s->sc_type) {
  6109. case SC_TYPE_DELEG:
  6110. spin_lock(&s->sc_file->fi_lock);
  6111. ret = nfsd_file_get(s->sc_file->fi_deleg_file);
  6112. spin_unlock(&s->sc_file->fi_lock);
  6113. break;
  6114. case SC_TYPE_OPEN:
  6115. case SC_TYPE_LOCK:
  6116. if (flags & RD_STATE)
  6117. ret = find_readable_file(s->sc_file);
  6118. else
  6119. ret = find_writeable_file(s->sc_file);
  6120. }
  6121. return ret;
  6122. }
  6123. static __be32
  6124. nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
  6125. {
  6126. __be32 status;
  6127. status = nfsd4_check_openowner_confirmed(ols);
  6128. if (status)
  6129. return status;
  6130. return nfs4_check_openmode(ols, flags);
  6131. }
  6132. static __be32
  6133. nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
  6134. struct nfsd_file **nfp, int flags)
  6135. {
  6136. int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
  6137. struct nfsd_file *nf;
  6138. __be32 status;
  6139. nf = nfs4_find_file(s, flags);
  6140. if (nf) {
  6141. status = nfsd_permission(&rqstp->rq_cred,
  6142. fhp->fh_export, fhp->fh_dentry,
  6143. acc | NFSD_MAY_OWNER_OVERRIDE);
  6144. if (status) {
  6145. nfsd_file_put(nf);
  6146. goto out;
  6147. }
  6148. } else {
  6149. status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
  6150. if (status)
  6151. return status;
  6152. }
  6153. *nfp = nf;
  6154. out:
  6155. return status;
  6156. }
  6157. static void
  6158. _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
  6159. {
  6160. WARN_ON_ONCE(cps->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID);
  6161. if (!refcount_dec_and_test(&cps->cp_stateid.cs_count))
  6162. return;
  6163. list_del(&cps->cp_list);
  6164. idr_remove(&nn->s2s_cp_stateids,
  6165. cps->cp_stateid.cs_stid.si_opaque.so_id);
  6166. kfree(cps);
  6167. }
  6168. /*
  6169. * A READ from an inter server to server COPY will have a
  6170. * copy stateid. Look up the copy notify stateid from the
  6171. * idr structure and take a reference on it.
  6172. */
  6173. __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
  6174. struct nfs4_client *clp,
  6175. struct nfs4_cpntf_state **cps)
  6176. {
  6177. copy_stateid_t *cps_t;
  6178. struct nfs4_cpntf_state *state = NULL;
  6179. if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
  6180. return nfserr_bad_stateid;
  6181. spin_lock(&nn->s2s_cp_lock);
  6182. cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
  6183. if (cps_t) {
  6184. state = container_of(cps_t, struct nfs4_cpntf_state,
  6185. cp_stateid);
  6186. if (state->cp_stateid.cs_type != NFS4_COPYNOTIFY_STID) {
  6187. state = NULL;
  6188. goto unlock;
  6189. }
  6190. if (!clp)
  6191. refcount_inc(&state->cp_stateid.cs_count);
  6192. else
  6193. _free_cpntf_state_locked(nn, state);
  6194. }
  6195. unlock:
  6196. spin_unlock(&nn->s2s_cp_lock);
  6197. if (!state)
  6198. return nfserr_bad_stateid;
  6199. if (!clp)
  6200. *cps = state;
  6201. return 0;
  6202. }
  6203. static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
  6204. struct nfs4_stid **stid)
  6205. {
  6206. __be32 status;
  6207. struct nfs4_cpntf_state *cps = NULL;
  6208. struct nfs4_client *found;
  6209. status = manage_cpntf_state(nn, st, NULL, &cps);
  6210. if (status)
  6211. return status;
  6212. cps->cpntf_time = ktime_get_boottime_seconds();
  6213. status = nfserr_expired;
  6214. found = lookup_clientid(&cps->cp_p_clid, true, nn);
  6215. if (!found)
  6216. goto out;
  6217. *stid = find_stateid_by_type(found, &cps->cp_p_stateid,
  6218. SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK,
  6219. 0);
  6220. if (*stid)
  6221. status = nfs_ok;
  6222. else
  6223. status = nfserr_bad_stateid;
  6224. put_client_renew(found);
  6225. out:
  6226. nfs4_put_cpntf_state(nn, cps);
  6227. return status;
  6228. }
  6229. void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
  6230. {
  6231. spin_lock(&nn->s2s_cp_lock);
  6232. _free_cpntf_state_locked(nn, cps);
  6233. spin_unlock(&nn->s2s_cp_lock);
  6234. }
  6235. /**
  6236. * nfs4_preprocess_stateid_op - find and prep stateid for an operation
  6237. * @rqstp: incoming request from client
  6238. * @cstate: current compound state
  6239. * @fhp: filehandle associated with requested stateid
  6240. * @stateid: stateid (provided by client)
  6241. * @flags: flags describing type of operation to be done
  6242. * @nfp: optional nfsd_file return pointer (may be NULL)
  6243. * @cstid: optional returned nfs4_stid pointer (may be NULL)
  6244. *
  6245. * Given info from the client, look up a nfs4_stid for the operation. On
  6246. * success, it returns a reference to the nfs4_stid and/or the nfsd_file
  6247. * associated with it.
  6248. */
  6249. __be32
  6250. nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
  6251. struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
  6252. stateid_t *stateid, int flags, struct nfsd_file **nfp,
  6253. struct nfs4_stid **cstid)
  6254. {
  6255. struct net *net = SVC_NET(rqstp);
  6256. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  6257. struct nfs4_stid *s = NULL;
  6258. __be32 status;
  6259. if (nfp)
  6260. *nfp = NULL;
  6261. if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
  6262. status = check_special_stateids(net, fhp, stateid, flags);
  6263. goto done;
  6264. }
  6265. status = nfsd4_lookup_stateid(cstate, stateid,
  6266. SC_TYPE_DELEG|SC_TYPE_OPEN|SC_TYPE_LOCK,
  6267. 0, &s, nn);
  6268. if (status == nfserr_bad_stateid)
  6269. status = find_cpntf_state(nn, stateid, &s);
  6270. if (status)
  6271. return status;
  6272. status = nfsd4_stid_check_stateid_generation(stateid, s,
  6273. nfsd4_has_session(cstate));
  6274. if (status)
  6275. goto out;
  6276. switch (s->sc_type) {
  6277. case SC_TYPE_DELEG:
  6278. status = nfs4_check_delegmode(delegstateid(s), flags);
  6279. break;
  6280. case SC_TYPE_OPEN:
  6281. case SC_TYPE_LOCK:
  6282. status = nfs4_check_olstateid(openlockstateid(s), flags);
  6283. break;
  6284. }
  6285. if (status)
  6286. goto out;
  6287. status = nfs4_check_fh(fhp, s);
  6288. done:
  6289. if (status == nfs_ok && nfp)
  6290. status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
  6291. out:
  6292. if (s) {
  6293. if (!status && cstid)
  6294. *cstid = s;
  6295. else
  6296. nfs4_put_stid(s);
  6297. }
  6298. return status;
  6299. }
  6300. /*
  6301. * Test if the stateid is valid
  6302. */
  6303. __be32
  6304. nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  6305. union nfsd4_op_u *u)
  6306. {
  6307. struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
  6308. struct nfsd4_test_stateid_id *stateid;
  6309. struct nfs4_client *cl = cstate->clp;
  6310. list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
  6311. stateid->ts_id_status =
  6312. nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
  6313. return nfs_ok;
  6314. }
  6315. static __be32
  6316. nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
  6317. {
  6318. struct nfs4_ol_stateid *stp = openlockstateid(s);
  6319. __be32 ret;
  6320. ret = nfsd4_lock_ol_stateid(stp);
  6321. if (ret)
  6322. goto out_put_stid;
  6323. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  6324. if (ret)
  6325. goto out;
  6326. ret = nfserr_locks_held;
  6327. if (check_for_locks(stp->st_stid.sc_file,
  6328. lockowner(stp->st_stateowner)))
  6329. goto out;
  6330. release_lock_stateid(stp);
  6331. ret = nfs_ok;
  6332. out:
  6333. mutex_unlock(&stp->st_mutex);
  6334. out_put_stid:
  6335. nfs4_put_stid(s);
  6336. return ret;
  6337. }
  6338. __be32
  6339. nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  6340. union nfsd4_op_u *u)
  6341. {
  6342. struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
  6343. stateid_t *stateid = &free_stateid->fr_stateid;
  6344. struct nfs4_stid *s;
  6345. struct nfs4_delegation *dp;
  6346. struct nfs4_client *cl = cstate->clp;
  6347. __be32 ret = nfserr_bad_stateid;
  6348. spin_lock(&cl->cl_lock);
  6349. s = find_stateid_locked(cl, stateid);
  6350. if (!s || s->sc_status & SC_STATUS_CLOSED)
  6351. goto out_unlock;
  6352. if (s->sc_status & SC_STATUS_ADMIN_REVOKED) {
  6353. nfsd4_drop_revoked_stid(s);
  6354. ret = nfs_ok;
  6355. goto out;
  6356. }
  6357. spin_lock(&s->sc_lock);
  6358. switch (s->sc_type) {
  6359. case SC_TYPE_DELEG:
  6360. if (s->sc_status & SC_STATUS_REVOKED) {
  6361. s->sc_status |= SC_STATUS_CLOSED;
  6362. spin_unlock(&s->sc_lock);
  6363. dp = delegstateid(s);
  6364. if (s->sc_status & SC_STATUS_FREEABLE)
  6365. list_del_init(&dp->dl_recall_lru);
  6366. s->sc_status |= SC_STATUS_FREED;
  6367. spin_unlock(&cl->cl_lock);
  6368. nfs4_put_stid(s);
  6369. ret = nfs_ok;
  6370. goto out;
  6371. }
  6372. ret = nfserr_locks_held;
  6373. break;
  6374. case SC_TYPE_OPEN:
  6375. ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
  6376. if (ret)
  6377. break;
  6378. ret = nfserr_locks_held;
  6379. break;
  6380. case SC_TYPE_LOCK:
  6381. spin_unlock(&s->sc_lock);
  6382. refcount_inc(&s->sc_count);
  6383. spin_unlock(&cl->cl_lock);
  6384. ret = nfsd4_free_lock_stateid(stateid, s);
  6385. goto out;
  6386. }
  6387. spin_unlock(&s->sc_lock);
  6388. out_unlock:
  6389. spin_unlock(&cl->cl_lock);
  6390. out:
  6391. return ret;
  6392. }
  6393. static inline int
  6394. setlkflg (int type)
  6395. {
  6396. return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
  6397. RD_STATE : WR_STATE;
  6398. }
  6399. static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
  6400. {
  6401. struct svc_fh *current_fh = &cstate->current_fh;
  6402. struct nfs4_stateowner *sop = stp->st_stateowner;
  6403. __be32 status;
  6404. status = nfsd4_check_seqid(cstate, sop, seqid);
  6405. if (status)
  6406. return status;
  6407. status = nfsd4_lock_ol_stateid(stp);
  6408. if (status != nfs_ok)
  6409. return status;
  6410. status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
  6411. if (status == nfs_ok)
  6412. status = nfs4_check_fh(current_fh, &stp->st_stid);
  6413. if (status != nfs_ok)
  6414. mutex_unlock(&stp->st_mutex);
  6415. return status;
  6416. }
  6417. /**
  6418. * nfs4_preprocess_seqid_op - find and prep an ol_stateid for a seqid-morphing op
  6419. * @cstate: compund state
  6420. * @seqid: seqid (provided by client)
  6421. * @stateid: stateid (provided by client)
  6422. * @typemask: mask of allowable types for this operation
  6423. * @statusmask: mask of allowed states: 0 or STID_CLOSED
  6424. * @stpp: return pointer for the stateid found
  6425. * @nn: net namespace for request
  6426. *
  6427. * Given a stateid+seqid from a client, look up an nfs4_ol_stateid and
  6428. * return it in @stpp. On a nfs_ok return, the returned stateid will
  6429. * have its st_mutex locked.
  6430. */
  6431. static __be32
  6432. nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  6433. stateid_t *stateid,
  6434. unsigned short typemask, unsigned short statusmask,
  6435. struct nfs4_ol_stateid **stpp,
  6436. struct nfsd_net *nn)
  6437. {
  6438. __be32 status;
  6439. struct nfs4_stid *s;
  6440. struct nfs4_ol_stateid *stp = NULL;
  6441. trace_nfsd_preprocess(seqid, stateid);
  6442. *stpp = NULL;
  6443. retry:
  6444. status = nfsd4_lookup_stateid(cstate, stateid,
  6445. typemask, statusmask, &s, nn);
  6446. if (status)
  6447. return status;
  6448. stp = openlockstateid(s);
  6449. if (nfsd4_cstate_assign_replay(cstate, stp->st_stateowner) == -EAGAIN) {
  6450. nfs4_put_stateowner(stp->st_stateowner);
  6451. goto retry;
  6452. }
  6453. status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
  6454. if (!status)
  6455. *stpp = stp;
  6456. else
  6457. nfs4_put_stid(&stp->st_stid);
  6458. return status;
  6459. }
  6460. static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
  6461. stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
  6462. {
  6463. __be32 status;
  6464. struct nfs4_openowner *oo;
  6465. struct nfs4_ol_stateid *stp;
  6466. status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
  6467. SC_TYPE_OPEN, 0, &stp, nn);
  6468. if (status)
  6469. return status;
  6470. oo = openowner(stp->st_stateowner);
  6471. if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
  6472. mutex_unlock(&stp->st_mutex);
  6473. nfs4_put_stid(&stp->st_stid);
  6474. return nfserr_bad_stateid;
  6475. }
  6476. *stpp = stp;
  6477. return nfs_ok;
  6478. }
  6479. __be32
  6480. nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  6481. union nfsd4_op_u *u)
  6482. {
  6483. struct nfsd4_open_confirm *oc = &u->open_confirm;
  6484. __be32 status;
  6485. struct nfs4_openowner *oo;
  6486. struct nfs4_ol_stateid *stp;
  6487. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  6488. dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
  6489. cstate->current_fh.fh_dentry);
  6490. status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
  6491. if (status)
  6492. return status;
  6493. status = nfs4_preprocess_seqid_op(cstate,
  6494. oc->oc_seqid, &oc->oc_req_stateid,
  6495. SC_TYPE_OPEN, 0, &stp, nn);
  6496. if (status)
  6497. goto out;
  6498. oo = openowner(stp->st_stateowner);
  6499. status = nfserr_bad_stateid;
  6500. if (oo->oo_flags & NFS4_OO_CONFIRMED) {
  6501. mutex_unlock(&stp->st_mutex);
  6502. goto put_stateid;
  6503. }
  6504. oo->oo_flags |= NFS4_OO_CONFIRMED;
  6505. nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
  6506. mutex_unlock(&stp->st_mutex);
  6507. trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
  6508. nfsd4_client_record_create(oo->oo_owner.so_client);
  6509. status = nfs_ok;
  6510. put_stateid:
  6511. nfs4_put_stid(&stp->st_stid);
  6512. out:
  6513. nfsd4_bump_seqid(cstate, status);
  6514. return status;
  6515. }
  6516. static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
  6517. {
  6518. if (!test_access(access, stp))
  6519. return;
  6520. nfs4_file_put_access(stp->st_stid.sc_file, access);
  6521. clear_access(access, stp);
  6522. }
  6523. static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
  6524. {
  6525. switch (to_access) {
  6526. case NFS4_SHARE_ACCESS_READ:
  6527. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
  6528. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  6529. break;
  6530. case NFS4_SHARE_ACCESS_WRITE:
  6531. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
  6532. nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
  6533. break;
  6534. case NFS4_SHARE_ACCESS_BOTH:
  6535. break;
  6536. default:
  6537. WARN_ON_ONCE(1);
  6538. }
  6539. }
  6540. __be32
  6541. nfsd4_open_downgrade(struct svc_rqst *rqstp,
  6542. struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
  6543. {
  6544. struct nfsd4_open_downgrade *od = &u->open_downgrade;
  6545. __be32 status;
  6546. struct nfs4_ol_stateid *stp;
  6547. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  6548. dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
  6549. cstate->current_fh.fh_dentry);
  6550. /* We don't yet support WANT bits: */
  6551. if (od->od_deleg_want)
  6552. dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
  6553. od->od_deleg_want);
  6554. status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
  6555. &od->od_stateid, &stp, nn);
  6556. if (status)
  6557. goto out;
  6558. status = nfserr_inval;
  6559. if (!test_access(od->od_share_access, stp)) {
  6560. dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
  6561. stp->st_access_bmap, od->od_share_access);
  6562. goto put_stateid;
  6563. }
  6564. if (!test_deny(od->od_share_deny, stp)) {
  6565. dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
  6566. stp->st_deny_bmap, od->od_share_deny);
  6567. goto put_stateid;
  6568. }
  6569. nfs4_stateid_downgrade(stp, od->od_share_access);
  6570. reset_union_bmap_deny(od->od_share_deny, stp);
  6571. nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
  6572. status = nfs_ok;
  6573. put_stateid:
  6574. mutex_unlock(&stp->st_mutex);
  6575. nfs4_put_stid(&stp->st_stid);
  6576. out:
  6577. nfsd4_bump_seqid(cstate, status);
  6578. return status;
  6579. }
  6580. static bool nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
  6581. {
  6582. struct nfs4_client *clp = s->st_stid.sc_client;
  6583. bool unhashed;
  6584. LIST_HEAD(reaplist);
  6585. struct nfs4_ol_stateid *stp;
  6586. spin_lock(&clp->cl_lock);
  6587. unhashed = unhash_open_stateid(s, &reaplist);
  6588. if (clp->cl_minorversion) {
  6589. if (unhashed)
  6590. put_ol_stateid_locked(s, &reaplist);
  6591. spin_unlock(&clp->cl_lock);
  6592. list_for_each_entry(stp, &reaplist, st_locks)
  6593. nfs4_free_cpntf_statelist(clp->net, &stp->st_stid);
  6594. free_ol_stateid_reaplist(&reaplist);
  6595. return false;
  6596. } else {
  6597. spin_unlock(&clp->cl_lock);
  6598. free_ol_stateid_reaplist(&reaplist);
  6599. return unhashed;
  6600. }
  6601. }
  6602. /*
  6603. * nfs4_unlock_state() called after encode
  6604. */
  6605. __be32
  6606. nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  6607. union nfsd4_op_u *u)
  6608. {
  6609. struct nfsd4_close *close = &u->close;
  6610. __be32 status;
  6611. struct nfs4_ol_stateid *stp;
  6612. struct net *net = SVC_NET(rqstp);
  6613. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  6614. bool need_move_to_close_list;
  6615. dprintk("NFSD: nfsd4_close on file %pd\n",
  6616. cstate->current_fh.fh_dentry);
  6617. status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
  6618. &close->cl_stateid,
  6619. SC_TYPE_OPEN, SC_STATUS_CLOSED,
  6620. &stp, nn);
  6621. nfsd4_bump_seqid(cstate, status);
  6622. if (status)
  6623. goto out;
  6624. spin_lock(&stp->st_stid.sc_client->cl_lock);
  6625. stp->st_stid.sc_status |= SC_STATUS_CLOSED;
  6626. spin_unlock(&stp->st_stid.sc_client->cl_lock);
  6627. /*
  6628. * Technically we don't _really_ have to increment or copy it, since
  6629. * it should just be gone after this operation and we clobber the
  6630. * copied value below, but we continue to do so here just to ensure
  6631. * that racing ops see that there was a state change.
  6632. */
  6633. nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
  6634. need_move_to_close_list = nfsd4_close_open_stateid(stp);
  6635. mutex_unlock(&stp->st_mutex);
  6636. if (need_move_to_close_list)
  6637. move_to_close_lru(stp, net);
  6638. /* v4.1+ suggests that we send a special stateid in here, since the
  6639. * clients should just ignore this anyway. Since this is not useful
  6640. * for v4.0 clients either, we set it to the special close_stateid
  6641. * universally.
  6642. *
  6643. * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
  6644. */
  6645. memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
  6646. /* put reference from nfs4_preprocess_seqid_op */
  6647. nfs4_put_stid(&stp->st_stid);
  6648. out:
  6649. return status;
  6650. }
  6651. __be32
  6652. nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  6653. union nfsd4_op_u *u)
  6654. {
  6655. struct nfsd4_delegreturn *dr = &u->delegreturn;
  6656. struct nfs4_delegation *dp;
  6657. stateid_t *stateid = &dr->dr_stateid;
  6658. struct nfs4_stid *s;
  6659. __be32 status;
  6660. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  6661. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  6662. return status;
  6663. status = nfsd4_lookup_stateid(cstate, stateid, SC_TYPE_DELEG, SC_STATUS_REVOKED, &s, nn);
  6664. if (status)
  6665. goto out;
  6666. dp = delegstateid(s);
  6667. status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
  6668. if (status)
  6669. goto put_stateid;
  6670. trace_nfsd_deleg_return(stateid);
  6671. destroy_delegation(dp);
  6672. smp_mb__after_atomic();
  6673. wake_up_var(d_inode(cstate->current_fh.fh_dentry));
  6674. put_stateid:
  6675. nfs4_put_stid(&dp->dl_stid);
  6676. out:
  6677. return status;
  6678. }
  6679. /* last octet in a range */
  6680. static inline u64
  6681. last_byte_offset(u64 start, u64 len)
  6682. {
  6683. u64 end;
  6684. WARN_ON_ONCE(!len);
  6685. end = start + len;
  6686. return end > start ? end - 1: NFS4_MAX_UINT64;
  6687. }
  6688. /*
  6689. * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
  6690. * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
  6691. * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
  6692. * locking, this prevents us from being completely protocol-compliant. The
  6693. * real solution to this problem is to start using unsigned file offsets in
  6694. * the VFS, but this is a very deep change!
  6695. */
  6696. static inline void
  6697. nfs4_transform_lock_offset(struct file_lock *lock)
  6698. {
  6699. if (lock->fl_start < 0)
  6700. lock->fl_start = OFFSET_MAX;
  6701. if (lock->fl_end < 0)
  6702. lock->fl_end = OFFSET_MAX;
  6703. }
  6704. static fl_owner_t
  6705. nfsd4_lm_get_owner(fl_owner_t owner)
  6706. {
  6707. struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
  6708. nfs4_get_stateowner(&lo->lo_owner);
  6709. return owner;
  6710. }
  6711. static void
  6712. nfsd4_lm_put_owner(fl_owner_t owner)
  6713. {
  6714. struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
  6715. if (lo)
  6716. nfs4_put_stateowner(&lo->lo_owner);
  6717. }
  6718. /* return pointer to struct nfs4_client if client is expirable */
  6719. static bool
  6720. nfsd4_lm_lock_expirable(struct file_lock *cfl)
  6721. {
  6722. struct nfs4_lockowner *lo = (struct nfs4_lockowner *) cfl->c.flc_owner;
  6723. struct nfs4_client *clp = lo->lo_owner.so_client;
  6724. struct nfsd_net *nn;
  6725. if (try_to_expire_client(clp)) {
  6726. nn = net_generic(clp->net, nfsd_net_id);
  6727. mod_delayed_work(laundry_wq, &nn->laundromat_work, 0);
  6728. return true;
  6729. }
  6730. return false;
  6731. }
  6732. /* schedule laundromat to run immediately and wait for it to complete */
  6733. static void
  6734. nfsd4_lm_expire_lock(void)
  6735. {
  6736. flush_workqueue(laundry_wq);
  6737. }
  6738. static void
  6739. nfsd4_lm_notify(struct file_lock *fl)
  6740. {
  6741. struct nfs4_lockowner *lo = (struct nfs4_lockowner *) fl->c.flc_owner;
  6742. struct net *net = lo->lo_owner.so_client->net;
  6743. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  6744. struct nfsd4_blocked_lock *nbl = container_of(fl,
  6745. struct nfsd4_blocked_lock, nbl_lock);
  6746. bool queue = false;
  6747. /* An empty list means that something else is going to be using it */
  6748. spin_lock(&nn->blocked_locks_lock);
  6749. if (!list_empty(&nbl->nbl_list)) {
  6750. list_del_init(&nbl->nbl_list);
  6751. list_del_init(&nbl->nbl_lru);
  6752. queue = true;
  6753. }
  6754. spin_unlock(&nn->blocked_locks_lock);
  6755. if (queue) {
  6756. trace_nfsd_cb_notify_lock(lo, nbl);
  6757. nfsd4_run_cb(&nbl->nbl_cb);
  6758. }
  6759. }
  6760. static const struct lock_manager_operations nfsd_posix_mng_ops = {
  6761. .lm_mod_owner = THIS_MODULE,
  6762. .lm_notify = nfsd4_lm_notify,
  6763. .lm_get_owner = nfsd4_lm_get_owner,
  6764. .lm_put_owner = nfsd4_lm_put_owner,
  6765. .lm_lock_expirable = nfsd4_lm_lock_expirable,
  6766. .lm_expire_lock = nfsd4_lm_expire_lock,
  6767. };
  6768. static inline void
  6769. nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
  6770. {
  6771. struct nfs4_lockowner *lo;
  6772. if (fl->fl_lmops == &nfsd_posix_mng_ops) {
  6773. lo = (struct nfs4_lockowner *) fl->c.flc_owner;
  6774. xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
  6775. GFP_KERNEL);
  6776. if (!deny->ld_owner.data)
  6777. /* We just don't care that much */
  6778. goto nevermind;
  6779. deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
  6780. } else {
  6781. nevermind:
  6782. deny->ld_owner.len = 0;
  6783. deny->ld_owner.data = NULL;
  6784. deny->ld_clientid.cl_boot = 0;
  6785. deny->ld_clientid.cl_id = 0;
  6786. }
  6787. deny->ld_start = fl->fl_start;
  6788. deny->ld_length = NFS4_MAX_UINT64;
  6789. if (fl->fl_end != NFS4_MAX_UINT64)
  6790. deny->ld_length = fl->fl_end - fl->fl_start + 1;
  6791. deny->ld_type = NFS4_READ_LT;
  6792. if (fl->c.flc_type != F_RDLCK)
  6793. deny->ld_type = NFS4_WRITE_LT;
  6794. }
  6795. static struct nfs4_lockowner *
  6796. find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
  6797. {
  6798. unsigned int strhashval = ownerstr_hashval(owner);
  6799. struct nfs4_stateowner *so;
  6800. lockdep_assert_held(&clp->cl_lock);
  6801. list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
  6802. so_strhash) {
  6803. if (so->so_is_open_owner)
  6804. continue;
  6805. if (same_owner_str(so, owner))
  6806. return lockowner(nfs4_get_stateowner(so));
  6807. }
  6808. return NULL;
  6809. }
  6810. static struct nfs4_lockowner *
  6811. find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
  6812. {
  6813. struct nfs4_lockowner *lo;
  6814. spin_lock(&clp->cl_lock);
  6815. lo = find_lockowner_str_locked(clp, owner);
  6816. spin_unlock(&clp->cl_lock);
  6817. return lo;
  6818. }
  6819. static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
  6820. {
  6821. unhash_lockowner_locked(lockowner(sop));
  6822. }
  6823. static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
  6824. {
  6825. struct nfs4_lockowner *lo = lockowner(sop);
  6826. kmem_cache_free(lockowner_slab, lo);
  6827. }
  6828. static const struct nfs4_stateowner_operations lockowner_ops = {
  6829. .so_unhash = nfs4_unhash_lockowner,
  6830. .so_free = nfs4_free_lockowner,
  6831. };
  6832. /*
  6833. * Alloc a lock owner structure.
  6834. * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
  6835. * occurred.
  6836. *
  6837. * strhashval = ownerstr_hashval
  6838. */
  6839. static struct nfs4_lockowner *
  6840. alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
  6841. struct nfs4_ol_stateid *open_stp,
  6842. struct nfsd4_lock *lock)
  6843. {
  6844. struct nfs4_lockowner *lo, *ret;
  6845. lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
  6846. if (!lo)
  6847. return NULL;
  6848. INIT_LIST_HEAD(&lo->lo_blocked);
  6849. INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
  6850. lo->lo_owner.so_is_open_owner = 0;
  6851. lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
  6852. lo->lo_owner.so_ops = &lockowner_ops;
  6853. spin_lock(&clp->cl_lock);
  6854. ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
  6855. if (ret == NULL) {
  6856. list_add(&lo->lo_owner.so_strhash,
  6857. &clp->cl_ownerstr_hashtbl[strhashval]);
  6858. ret = lo;
  6859. } else
  6860. nfs4_free_stateowner(&lo->lo_owner);
  6861. spin_unlock(&clp->cl_lock);
  6862. return ret;
  6863. }
  6864. static struct nfs4_ol_stateid *
  6865. find_lock_stateid(const struct nfs4_lockowner *lo,
  6866. const struct nfs4_ol_stateid *ost)
  6867. {
  6868. struct nfs4_ol_stateid *lst;
  6869. lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
  6870. /* If ost is not hashed, ost->st_locks will not be valid */
  6871. if (!nfs4_ol_stateid_unhashed(ost))
  6872. list_for_each_entry(lst, &ost->st_locks, st_locks) {
  6873. if (lst->st_stateowner == &lo->lo_owner) {
  6874. refcount_inc(&lst->st_stid.sc_count);
  6875. return lst;
  6876. }
  6877. }
  6878. return NULL;
  6879. }
  6880. static struct nfs4_ol_stateid *
  6881. init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
  6882. struct nfs4_file *fp, struct inode *inode,
  6883. struct nfs4_ol_stateid *open_stp)
  6884. {
  6885. struct nfs4_client *clp = lo->lo_owner.so_client;
  6886. struct nfs4_ol_stateid *retstp;
  6887. mutex_init(&stp->st_mutex);
  6888. mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
  6889. retry:
  6890. spin_lock(&clp->cl_lock);
  6891. if (nfs4_ol_stateid_unhashed(open_stp))
  6892. goto out_close;
  6893. retstp = find_lock_stateid(lo, open_stp);
  6894. if (retstp)
  6895. goto out_found;
  6896. refcount_inc(&stp->st_stid.sc_count);
  6897. stp->st_stid.sc_type = SC_TYPE_LOCK;
  6898. stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
  6899. get_nfs4_file(fp);
  6900. stp->st_stid.sc_file = fp;
  6901. stp->st_access_bmap = 0;
  6902. stp->st_deny_bmap = open_stp->st_deny_bmap;
  6903. stp->st_openstp = open_stp;
  6904. spin_lock(&fp->fi_lock);
  6905. list_add(&stp->st_locks, &open_stp->st_locks);
  6906. list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
  6907. list_add(&stp->st_perfile, &fp->fi_stateids);
  6908. spin_unlock(&fp->fi_lock);
  6909. spin_unlock(&clp->cl_lock);
  6910. return stp;
  6911. out_found:
  6912. spin_unlock(&clp->cl_lock);
  6913. if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
  6914. nfs4_put_stid(&retstp->st_stid);
  6915. goto retry;
  6916. }
  6917. /* To keep mutex tracking happy */
  6918. mutex_unlock(&stp->st_mutex);
  6919. return retstp;
  6920. out_close:
  6921. spin_unlock(&clp->cl_lock);
  6922. mutex_unlock(&stp->st_mutex);
  6923. return NULL;
  6924. }
  6925. static struct nfs4_ol_stateid *
  6926. find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
  6927. struct inode *inode, struct nfs4_ol_stateid *ost,
  6928. bool *new)
  6929. {
  6930. struct nfs4_stid *ns = NULL;
  6931. struct nfs4_ol_stateid *lst;
  6932. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  6933. struct nfs4_client *clp = oo->oo_owner.so_client;
  6934. *new = false;
  6935. spin_lock(&clp->cl_lock);
  6936. lst = find_lock_stateid(lo, ost);
  6937. spin_unlock(&clp->cl_lock);
  6938. if (lst != NULL) {
  6939. if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
  6940. goto out;
  6941. nfs4_put_stid(&lst->st_stid);
  6942. }
  6943. ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
  6944. if (ns == NULL)
  6945. return NULL;
  6946. lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
  6947. if (lst == openlockstateid(ns))
  6948. *new = true;
  6949. else
  6950. nfs4_put_stid(ns);
  6951. out:
  6952. return lst;
  6953. }
  6954. static int
  6955. check_lock_length(u64 offset, u64 length)
  6956. {
  6957. return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
  6958. (length > ~offset)));
  6959. }
  6960. static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
  6961. {
  6962. struct nfs4_file *fp = lock_stp->st_stid.sc_file;
  6963. lockdep_assert_held(&fp->fi_lock);
  6964. if (test_access(access, lock_stp))
  6965. return;
  6966. __nfs4_file_get_access(fp, access);
  6967. set_access(access, lock_stp);
  6968. }
  6969. static __be32
  6970. lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
  6971. struct nfs4_ol_stateid *ost,
  6972. struct nfsd4_lock *lock,
  6973. struct nfs4_ol_stateid **plst, bool *new)
  6974. {
  6975. __be32 status;
  6976. struct nfs4_file *fi = ost->st_stid.sc_file;
  6977. struct nfs4_openowner *oo = openowner(ost->st_stateowner);
  6978. struct nfs4_client *cl = oo->oo_owner.so_client;
  6979. struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
  6980. struct nfs4_lockowner *lo;
  6981. struct nfs4_ol_stateid *lst;
  6982. unsigned int strhashval;
  6983. lo = find_lockowner_str(cl, &lock->lk_new_owner);
  6984. if (!lo) {
  6985. strhashval = ownerstr_hashval(&lock->lk_new_owner);
  6986. lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
  6987. if (lo == NULL)
  6988. return nfserr_jukebox;
  6989. } else {
  6990. /* with an existing lockowner, seqids must be the same */
  6991. status = nfserr_bad_seqid;
  6992. if (!cstate->minorversion &&
  6993. lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
  6994. goto out;
  6995. }
  6996. lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
  6997. if (lst == NULL) {
  6998. status = nfserr_jukebox;
  6999. goto out;
  7000. }
  7001. status = nfs_ok;
  7002. *plst = lst;
  7003. out:
  7004. nfs4_put_stateowner(&lo->lo_owner);
  7005. return status;
  7006. }
  7007. /*
  7008. * LOCK operation
  7009. */
  7010. __be32
  7011. nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  7012. union nfsd4_op_u *u)
  7013. {
  7014. struct nfsd4_lock *lock = &u->lock;
  7015. struct nfs4_openowner *open_sop = NULL;
  7016. struct nfs4_lockowner *lock_sop = NULL;
  7017. struct nfs4_ol_stateid *lock_stp = NULL;
  7018. struct nfs4_ol_stateid *open_stp = NULL;
  7019. struct nfs4_file *fp;
  7020. struct nfsd_file *nf = NULL;
  7021. struct nfsd4_blocked_lock *nbl = NULL;
  7022. struct file_lock *file_lock = NULL;
  7023. struct file_lock *conflock = NULL;
  7024. struct super_block *sb;
  7025. __be32 status = 0;
  7026. int lkflg;
  7027. int err;
  7028. bool new = false;
  7029. unsigned char type;
  7030. unsigned int flags = FL_POSIX;
  7031. struct net *net = SVC_NET(rqstp);
  7032. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  7033. dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
  7034. (long long) lock->lk_offset,
  7035. (long long) lock->lk_length);
  7036. if (check_lock_length(lock->lk_offset, lock->lk_length))
  7037. return nfserr_inval;
  7038. status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
  7039. if (status != nfs_ok)
  7040. return status;
  7041. sb = cstate->current_fh.fh_dentry->d_sb;
  7042. if (lock->lk_is_new) {
  7043. if (nfsd4_has_session(cstate))
  7044. /* See rfc 5661 18.10.3: given clientid is ignored: */
  7045. memcpy(&lock->lk_new_clientid,
  7046. &cstate->clp->cl_clientid,
  7047. sizeof(clientid_t));
  7048. /* validate and update open stateid and open seqid */
  7049. status = nfs4_preprocess_confirmed_seqid_op(cstate,
  7050. lock->lk_new_open_seqid,
  7051. &lock->lk_new_open_stateid,
  7052. &open_stp, nn);
  7053. if (status)
  7054. goto out;
  7055. mutex_unlock(&open_stp->st_mutex);
  7056. open_sop = openowner(open_stp->st_stateowner);
  7057. status = nfserr_bad_stateid;
  7058. if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
  7059. &lock->lk_new_clientid))
  7060. goto out;
  7061. status = lookup_or_create_lock_state(cstate, open_stp, lock,
  7062. &lock_stp, &new);
  7063. } else {
  7064. status = nfs4_preprocess_seqid_op(cstate,
  7065. lock->lk_old_lock_seqid,
  7066. &lock->lk_old_lock_stateid,
  7067. SC_TYPE_LOCK, 0, &lock_stp,
  7068. nn);
  7069. }
  7070. if (status)
  7071. goto out;
  7072. lock_sop = lockowner(lock_stp->st_stateowner);
  7073. lkflg = setlkflg(lock->lk_type);
  7074. status = nfs4_check_openmode(lock_stp, lkflg);
  7075. if (status)
  7076. goto out;
  7077. status = nfserr_grace;
  7078. if (locks_in_grace(net) && !lock->lk_reclaim)
  7079. goto out;
  7080. status = nfserr_no_grace;
  7081. if (!locks_in_grace(net) && lock->lk_reclaim)
  7082. goto out;
  7083. if (lock->lk_reclaim)
  7084. flags |= FL_RECLAIM;
  7085. fp = lock_stp->st_stid.sc_file;
  7086. switch (lock->lk_type) {
  7087. case NFS4_READW_LT:
  7088. if (nfsd4_has_session(cstate) ||
  7089. exportfs_lock_op_is_async(sb->s_export_op))
  7090. flags |= FL_SLEEP;
  7091. fallthrough;
  7092. case NFS4_READ_LT:
  7093. spin_lock(&fp->fi_lock);
  7094. nf = find_readable_file_locked(fp);
  7095. if (nf)
  7096. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
  7097. spin_unlock(&fp->fi_lock);
  7098. type = F_RDLCK;
  7099. break;
  7100. case NFS4_WRITEW_LT:
  7101. if (nfsd4_has_session(cstate) ||
  7102. exportfs_lock_op_is_async(sb->s_export_op))
  7103. flags |= FL_SLEEP;
  7104. fallthrough;
  7105. case NFS4_WRITE_LT:
  7106. spin_lock(&fp->fi_lock);
  7107. nf = find_writeable_file_locked(fp);
  7108. if (nf)
  7109. get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
  7110. spin_unlock(&fp->fi_lock);
  7111. type = F_WRLCK;
  7112. break;
  7113. default:
  7114. status = nfserr_inval;
  7115. goto out;
  7116. }
  7117. if (!nf) {
  7118. status = nfserr_openmode;
  7119. goto out;
  7120. }
  7121. /*
  7122. * Most filesystems with their own ->lock operations will block
  7123. * the nfsd thread waiting to acquire the lock. That leads to
  7124. * deadlocks (we don't want every nfsd thread tied up waiting
  7125. * for file locks), so don't attempt blocking lock notifications
  7126. * on those filesystems:
  7127. */
  7128. if (!exportfs_lock_op_is_async(sb->s_export_op))
  7129. flags &= ~FL_SLEEP;
  7130. nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
  7131. if (!nbl) {
  7132. dprintk("NFSD: %s: unable to allocate block!\n", __func__);
  7133. status = nfserr_jukebox;
  7134. goto out;
  7135. }
  7136. file_lock = &nbl->nbl_lock;
  7137. file_lock->c.flc_type = type;
  7138. file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
  7139. file_lock->c.flc_pid = current->tgid;
  7140. file_lock->c.flc_file = nf->nf_file;
  7141. file_lock->c.flc_flags = flags;
  7142. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  7143. file_lock->fl_start = lock->lk_offset;
  7144. file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
  7145. nfs4_transform_lock_offset(file_lock);
  7146. conflock = locks_alloc_lock();
  7147. if (!conflock) {
  7148. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  7149. status = nfserr_jukebox;
  7150. goto out;
  7151. }
  7152. if (flags & FL_SLEEP) {
  7153. nbl->nbl_time = ktime_get_boottime_seconds();
  7154. spin_lock(&nn->blocked_locks_lock);
  7155. list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
  7156. list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
  7157. kref_get(&nbl->nbl_kref);
  7158. spin_unlock(&nn->blocked_locks_lock);
  7159. }
  7160. err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
  7161. switch (err) {
  7162. case 0: /* success! */
  7163. nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
  7164. status = 0;
  7165. if (lock->lk_reclaim)
  7166. nn->somebody_reclaimed = true;
  7167. break;
  7168. case FILE_LOCK_DEFERRED:
  7169. kref_put(&nbl->nbl_kref, free_nbl);
  7170. nbl = NULL;
  7171. fallthrough;
  7172. case -EAGAIN: /* conflock holds conflicting lock */
  7173. status = nfserr_denied;
  7174. dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
  7175. nfs4_set_lock_denied(conflock, &lock->lk_denied);
  7176. break;
  7177. case -EDEADLK:
  7178. status = nfserr_deadlock;
  7179. break;
  7180. default:
  7181. dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
  7182. status = nfserrno(err);
  7183. break;
  7184. }
  7185. out:
  7186. if (nbl) {
  7187. /* dequeue it if we queued it before */
  7188. if (flags & FL_SLEEP) {
  7189. spin_lock(&nn->blocked_locks_lock);
  7190. if (!list_empty(&nbl->nbl_list) &&
  7191. !list_empty(&nbl->nbl_lru)) {
  7192. list_del_init(&nbl->nbl_list);
  7193. list_del_init(&nbl->nbl_lru);
  7194. kref_put(&nbl->nbl_kref, free_nbl);
  7195. }
  7196. /* nbl can use one of lists to be linked to reaplist */
  7197. spin_unlock(&nn->blocked_locks_lock);
  7198. }
  7199. free_blocked_lock(nbl);
  7200. }
  7201. if (nf)
  7202. nfsd_file_put(nf);
  7203. if (lock_stp) {
  7204. /* Bump seqid manually if the 4.0 replay owner is openowner */
  7205. if (cstate->replay_owner &&
  7206. cstate->replay_owner != &lock_sop->lo_owner &&
  7207. seqid_mutating_err(ntohl(status)))
  7208. lock_sop->lo_owner.so_seqid++;
  7209. /*
  7210. * If this is a new, never-before-used stateid, and we are
  7211. * returning an error, then just go ahead and release it.
  7212. */
  7213. if (status && new)
  7214. release_lock_stateid(lock_stp);
  7215. mutex_unlock(&lock_stp->st_mutex);
  7216. nfs4_put_stid(&lock_stp->st_stid);
  7217. }
  7218. if (open_stp)
  7219. nfs4_put_stid(&open_stp->st_stid);
  7220. nfsd4_bump_seqid(cstate, status);
  7221. if (conflock)
  7222. locks_free_lock(conflock);
  7223. return status;
  7224. }
  7225. void nfsd4_lock_release(union nfsd4_op_u *u)
  7226. {
  7227. struct nfsd4_lock *lock = &u->lock;
  7228. struct nfsd4_lock_denied *deny = &lock->lk_denied;
  7229. kfree(deny->ld_owner.data);
  7230. }
  7231. /*
  7232. * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
  7233. * so we do a temporary open here just to get an open file to pass to
  7234. * vfs_test_lock.
  7235. */
  7236. static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
  7237. {
  7238. struct nfsd_file *nf;
  7239. struct inode *inode;
  7240. __be32 err;
  7241. err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
  7242. if (err)
  7243. return err;
  7244. inode = fhp->fh_dentry->d_inode;
  7245. inode_lock(inode); /* to block new leases till after test_lock: */
  7246. err = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
  7247. if (err)
  7248. goto out;
  7249. lock->c.flc_file = nf->nf_file;
  7250. err = nfserrno(vfs_test_lock(nf->nf_file, lock));
  7251. lock->c.flc_file = NULL;
  7252. out:
  7253. inode_unlock(inode);
  7254. nfsd_file_put(nf);
  7255. return err;
  7256. }
  7257. /*
  7258. * LOCKT operation
  7259. */
  7260. __be32
  7261. nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  7262. union nfsd4_op_u *u)
  7263. {
  7264. struct nfsd4_lockt *lockt = &u->lockt;
  7265. struct file_lock *file_lock = NULL;
  7266. struct nfs4_lockowner *lo = NULL;
  7267. __be32 status;
  7268. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  7269. if (locks_in_grace(SVC_NET(rqstp)))
  7270. return nfserr_grace;
  7271. if (check_lock_length(lockt->lt_offset, lockt->lt_length))
  7272. return nfserr_inval;
  7273. if (!nfsd4_has_session(cstate)) {
  7274. status = set_client(&lockt->lt_clientid, cstate, nn);
  7275. if (status)
  7276. goto out;
  7277. }
  7278. if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
  7279. goto out;
  7280. file_lock = locks_alloc_lock();
  7281. if (!file_lock) {
  7282. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  7283. status = nfserr_jukebox;
  7284. goto out;
  7285. }
  7286. switch (lockt->lt_type) {
  7287. case NFS4_READ_LT:
  7288. case NFS4_READW_LT:
  7289. file_lock->c.flc_type = F_RDLCK;
  7290. break;
  7291. case NFS4_WRITE_LT:
  7292. case NFS4_WRITEW_LT:
  7293. file_lock->c.flc_type = F_WRLCK;
  7294. break;
  7295. default:
  7296. dprintk("NFSD: nfs4_lockt: bad lock type!\n");
  7297. status = nfserr_inval;
  7298. goto out;
  7299. }
  7300. lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
  7301. if (lo)
  7302. file_lock->c.flc_owner = (fl_owner_t)lo;
  7303. file_lock->c.flc_pid = current->tgid;
  7304. file_lock->c.flc_flags = FL_POSIX;
  7305. file_lock->fl_start = lockt->lt_offset;
  7306. file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
  7307. nfs4_transform_lock_offset(file_lock);
  7308. status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
  7309. if (status)
  7310. goto out;
  7311. if (file_lock->c.flc_type != F_UNLCK) {
  7312. status = nfserr_denied;
  7313. nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
  7314. }
  7315. out:
  7316. if (lo)
  7317. nfs4_put_stateowner(&lo->lo_owner);
  7318. if (file_lock)
  7319. locks_free_lock(file_lock);
  7320. return status;
  7321. }
  7322. void nfsd4_lockt_release(union nfsd4_op_u *u)
  7323. {
  7324. struct nfsd4_lockt *lockt = &u->lockt;
  7325. struct nfsd4_lock_denied *deny = &lockt->lt_denied;
  7326. kfree(deny->ld_owner.data);
  7327. }
  7328. __be32
  7329. nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
  7330. union nfsd4_op_u *u)
  7331. {
  7332. struct nfsd4_locku *locku = &u->locku;
  7333. struct nfs4_ol_stateid *stp;
  7334. struct nfsd_file *nf = NULL;
  7335. struct file_lock *file_lock = NULL;
  7336. __be32 status;
  7337. int err;
  7338. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  7339. dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
  7340. (long long) locku->lu_offset,
  7341. (long long) locku->lu_length);
  7342. if (check_lock_length(locku->lu_offset, locku->lu_length))
  7343. return nfserr_inval;
  7344. status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
  7345. &locku->lu_stateid, SC_TYPE_LOCK, 0,
  7346. &stp, nn);
  7347. if (status)
  7348. goto out;
  7349. nf = find_any_file(stp->st_stid.sc_file);
  7350. if (!nf) {
  7351. status = nfserr_lock_range;
  7352. goto put_stateid;
  7353. }
  7354. file_lock = locks_alloc_lock();
  7355. if (!file_lock) {
  7356. dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
  7357. status = nfserr_jukebox;
  7358. goto put_file;
  7359. }
  7360. file_lock->c.flc_type = F_UNLCK;
  7361. file_lock->c.flc_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
  7362. file_lock->c.flc_pid = current->tgid;
  7363. file_lock->c.flc_file = nf->nf_file;
  7364. file_lock->c.flc_flags = FL_POSIX;
  7365. file_lock->fl_lmops = &nfsd_posix_mng_ops;
  7366. file_lock->fl_start = locku->lu_offset;
  7367. file_lock->fl_end = last_byte_offset(locku->lu_offset,
  7368. locku->lu_length);
  7369. nfs4_transform_lock_offset(file_lock);
  7370. err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
  7371. if (err) {
  7372. dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
  7373. goto out_nfserr;
  7374. }
  7375. nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
  7376. put_file:
  7377. nfsd_file_put(nf);
  7378. put_stateid:
  7379. mutex_unlock(&stp->st_mutex);
  7380. nfs4_put_stid(&stp->st_stid);
  7381. out:
  7382. nfsd4_bump_seqid(cstate, status);
  7383. if (file_lock)
  7384. locks_free_lock(file_lock);
  7385. return status;
  7386. out_nfserr:
  7387. status = nfserrno(err);
  7388. goto put_file;
  7389. }
  7390. /*
  7391. * returns
  7392. * true: locks held by lockowner
  7393. * false: no locks held by lockowner
  7394. */
  7395. static bool
  7396. check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
  7397. {
  7398. struct file_lock *fl;
  7399. int status = false;
  7400. struct nfsd_file *nf;
  7401. struct inode *inode;
  7402. struct file_lock_context *flctx;
  7403. spin_lock(&fp->fi_lock);
  7404. nf = find_any_file_locked(fp);
  7405. if (!nf) {
  7406. /* Any valid lock stateid should have some sort of access */
  7407. WARN_ON_ONCE(1);
  7408. goto out;
  7409. }
  7410. inode = file_inode(nf->nf_file);
  7411. flctx = locks_inode_context(inode);
  7412. if (flctx && !list_empty_careful(&flctx->flc_posix)) {
  7413. spin_lock(&flctx->flc_lock);
  7414. for_each_file_lock(fl, &flctx->flc_posix) {
  7415. if (fl->c.flc_owner == (fl_owner_t)lowner) {
  7416. status = true;
  7417. break;
  7418. }
  7419. }
  7420. spin_unlock(&flctx->flc_lock);
  7421. }
  7422. out:
  7423. spin_unlock(&fp->fi_lock);
  7424. return status;
  7425. }
  7426. /**
  7427. * nfsd4_release_lockowner - process NFSv4.0 RELEASE_LOCKOWNER operations
  7428. * @rqstp: RPC transaction
  7429. * @cstate: NFSv4 COMPOUND state
  7430. * @u: RELEASE_LOCKOWNER arguments
  7431. *
  7432. * Check if there are any locks still held and if not, free the lockowner
  7433. * and any lock state that is owned.
  7434. *
  7435. * Return values:
  7436. * %nfs_ok: lockowner released or not found
  7437. * %nfserr_locks_held: lockowner still in use
  7438. * %nfserr_stale_clientid: clientid no longer active
  7439. * %nfserr_expired: clientid not recognized
  7440. */
  7441. __be32
  7442. nfsd4_release_lockowner(struct svc_rqst *rqstp,
  7443. struct nfsd4_compound_state *cstate,
  7444. union nfsd4_op_u *u)
  7445. {
  7446. struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
  7447. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  7448. clientid_t *clid = &rlockowner->rl_clientid;
  7449. struct nfs4_ol_stateid *stp;
  7450. struct nfs4_lockowner *lo;
  7451. struct nfs4_client *clp;
  7452. LIST_HEAD(reaplist);
  7453. __be32 status;
  7454. dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
  7455. clid->cl_boot, clid->cl_id);
  7456. status = set_client(clid, cstate, nn);
  7457. if (status)
  7458. return status;
  7459. clp = cstate->clp;
  7460. spin_lock(&clp->cl_lock);
  7461. lo = find_lockowner_str_locked(clp, &rlockowner->rl_owner);
  7462. if (!lo) {
  7463. spin_unlock(&clp->cl_lock);
  7464. return nfs_ok;
  7465. }
  7466. list_for_each_entry(stp, &lo->lo_owner.so_stateids, st_perstateowner) {
  7467. if (check_for_locks(stp->st_stid.sc_file, lo)) {
  7468. spin_unlock(&clp->cl_lock);
  7469. nfs4_put_stateowner(&lo->lo_owner);
  7470. return nfserr_locks_held;
  7471. }
  7472. }
  7473. unhash_lockowner_locked(lo);
  7474. while (!list_empty(&lo->lo_owner.so_stateids)) {
  7475. stp = list_first_entry(&lo->lo_owner.so_stateids,
  7476. struct nfs4_ol_stateid,
  7477. st_perstateowner);
  7478. unhash_lock_stateid(stp);
  7479. put_ol_stateid_locked(stp, &reaplist);
  7480. }
  7481. spin_unlock(&clp->cl_lock);
  7482. free_ol_stateid_reaplist(&reaplist);
  7483. remove_blocked_locks(lo);
  7484. nfs4_put_stateowner(&lo->lo_owner);
  7485. return nfs_ok;
  7486. }
  7487. static inline struct nfs4_client_reclaim *
  7488. alloc_reclaim(void)
  7489. {
  7490. return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
  7491. }
  7492. bool
  7493. nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
  7494. {
  7495. struct nfs4_client_reclaim *crp;
  7496. crp = nfsd4_find_reclaim_client(name, nn);
  7497. return (crp && crp->cr_clp);
  7498. }
  7499. /*
  7500. * failure => all reset bets are off, nfserr_no_grace...
  7501. *
  7502. * The caller is responsible for freeing name.data if NULL is returned (it
  7503. * will be freed in nfs4_remove_reclaim_record in the normal case).
  7504. */
  7505. struct nfs4_client_reclaim *
  7506. nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
  7507. struct nfsd_net *nn)
  7508. {
  7509. unsigned int strhashval;
  7510. struct nfs4_client_reclaim *crp;
  7511. crp = alloc_reclaim();
  7512. if (crp) {
  7513. strhashval = clientstr_hashval(name);
  7514. INIT_LIST_HEAD(&crp->cr_strhash);
  7515. list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
  7516. crp->cr_name.data = name.data;
  7517. crp->cr_name.len = name.len;
  7518. crp->cr_princhash.data = princhash.data;
  7519. crp->cr_princhash.len = princhash.len;
  7520. crp->cr_clp = NULL;
  7521. nn->reclaim_str_hashtbl_size++;
  7522. }
  7523. return crp;
  7524. }
  7525. void
  7526. nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
  7527. {
  7528. list_del(&crp->cr_strhash);
  7529. kfree(crp->cr_name.data);
  7530. kfree(crp->cr_princhash.data);
  7531. kfree(crp);
  7532. nn->reclaim_str_hashtbl_size--;
  7533. }
  7534. void
  7535. nfs4_release_reclaim(struct nfsd_net *nn)
  7536. {
  7537. struct nfs4_client_reclaim *crp = NULL;
  7538. int i;
  7539. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  7540. while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
  7541. crp = list_entry(nn->reclaim_str_hashtbl[i].next,
  7542. struct nfs4_client_reclaim, cr_strhash);
  7543. nfs4_remove_reclaim_record(crp, nn);
  7544. }
  7545. }
  7546. WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
  7547. }
  7548. /*
  7549. * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
  7550. struct nfs4_client_reclaim *
  7551. nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
  7552. {
  7553. unsigned int strhashval;
  7554. struct nfs4_client_reclaim *crp = NULL;
  7555. strhashval = clientstr_hashval(name);
  7556. list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
  7557. if (compare_blob(&crp->cr_name, &name) == 0) {
  7558. return crp;
  7559. }
  7560. }
  7561. return NULL;
  7562. }
  7563. __be32
  7564. nfs4_check_open_reclaim(struct nfs4_client *clp)
  7565. {
  7566. if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags))
  7567. return nfserr_no_grace;
  7568. if (nfsd4_client_record_check(clp))
  7569. return nfserr_reclaim_bad;
  7570. return nfs_ok;
  7571. }
  7572. /*
  7573. * Since the lifetime of a delegation isn't limited to that of an open, a
  7574. * client may quite reasonably hang on to a delegation as long as it has
  7575. * the inode cached. This becomes an obvious problem the first time a
  7576. * client's inode cache approaches the size of the server's total memory.
  7577. *
  7578. * For now we avoid this problem by imposing a hard limit on the number
  7579. * of delegations, which varies according to the server's memory size.
  7580. */
  7581. static void
  7582. set_max_delegations(void)
  7583. {
  7584. /*
  7585. * Allow at most 4 delegations per megabyte of RAM. Quick
  7586. * estimates suggest that in the worst case (where every delegation
  7587. * is for a different inode), a delegation could take about 1.5K,
  7588. * giving a worst case usage of about 6% of memory.
  7589. */
  7590. max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
  7591. }
  7592. static int nfs4_state_create_net(struct net *net)
  7593. {
  7594. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  7595. int i;
  7596. nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
  7597. sizeof(struct list_head),
  7598. GFP_KERNEL);
  7599. if (!nn->conf_id_hashtbl)
  7600. goto err;
  7601. nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
  7602. sizeof(struct list_head),
  7603. GFP_KERNEL);
  7604. if (!nn->unconf_id_hashtbl)
  7605. goto err_unconf_id;
  7606. nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
  7607. sizeof(struct list_head),
  7608. GFP_KERNEL);
  7609. if (!nn->sessionid_hashtbl)
  7610. goto err_sessionid;
  7611. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  7612. INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
  7613. INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
  7614. }
  7615. for (i = 0; i < SESSION_HASH_SIZE; i++)
  7616. INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
  7617. nn->conf_name_tree = RB_ROOT;
  7618. nn->unconf_name_tree = RB_ROOT;
  7619. nn->boot_time = ktime_get_real_seconds();
  7620. nn->grace_ended = false;
  7621. nn->nfsd4_manager.block_opens = true;
  7622. INIT_LIST_HEAD(&nn->nfsd4_manager.list);
  7623. INIT_LIST_HEAD(&nn->client_lru);
  7624. INIT_LIST_HEAD(&nn->close_lru);
  7625. INIT_LIST_HEAD(&nn->del_recall_lru);
  7626. spin_lock_init(&nn->client_lock);
  7627. spin_lock_init(&nn->s2s_cp_lock);
  7628. idr_init(&nn->s2s_cp_stateids);
  7629. atomic_set(&nn->pending_async_copies, 0);
  7630. spin_lock_init(&nn->blocked_locks_lock);
  7631. INIT_LIST_HEAD(&nn->blocked_locks_lru);
  7632. INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
  7633. INIT_WORK(&nn->nfsd_shrinker_work, nfsd4_state_shrinker_worker);
  7634. get_net(net);
  7635. nn->nfsd_client_shrinker = shrinker_alloc(0, "nfsd-client");
  7636. if (!nn->nfsd_client_shrinker)
  7637. goto err_shrinker;
  7638. nn->nfsd_client_shrinker->scan_objects = nfsd4_state_shrinker_scan;
  7639. nn->nfsd_client_shrinker->count_objects = nfsd4_state_shrinker_count;
  7640. nn->nfsd_client_shrinker->private_data = nn;
  7641. shrinker_register(nn->nfsd_client_shrinker);
  7642. return 0;
  7643. err_shrinker:
  7644. put_net(net);
  7645. kfree(nn->sessionid_hashtbl);
  7646. err_sessionid:
  7647. kfree(nn->unconf_id_hashtbl);
  7648. err_unconf_id:
  7649. kfree(nn->conf_id_hashtbl);
  7650. err:
  7651. return -ENOMEM;
  7652. }
  7653. static void
  7654. nfs4_state_destroy_net(struct net *net)
  7655. {
  7656. int i;
  7657. struct nfs4_client *clp = NULL;
  7658. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  7659. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  7660. while (!list_empty(&nn->conf_id_hashtbl[i])) {
  7661. clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  7662. destroy_client(clp);
  7663. }
  7664. }
  7665. WARN_ON(!list_empty(&nn->blocked_locks_lru));
  7666. for (i = 0; i < CLIENT_HASH_SIZE; i++) {
  7667. while (!list_empty(&nn->unconf_id_hashtbl[i])) {
  7668. clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
  7669. destroy_client(clp);
  7670. }
  7671. }
  7672. kfree(nn->sessionid_hashtbl);
  7673. kfree(nn->unconf_id_hashtbl);
  7674. kfree(nn->conf_id_hashtbl);
  7675. put_net(net);
  7676. }
  7677. int
  7678. nfs4_state_start_net(struct net *net)
  7679. {
  7680. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  7681. int ret;
  7682. ret = nfs4_state_create_net(net);
  7683. if (ret)
  7684. return ret;
  7685. locks_start_grace(net, &nn->nfsd4_manager);
  7686. nfsd4_client_tracking_init(net);
  7687. if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
  7688. goto skip_grace;
  7689. printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
  7690. nn->nfsd4_grace, net->ns.inum);
  7691. trace_nfsd_grace_start(nn);
  7692. queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
  7693. return 0;
  7694. skip_grace:
  7695. printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
  7696. net->ns.inum);
  7697. queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
  7698. nfsd4_end_grace(nn);
  7699. return 0;
  7700. }
  7701. /* initialization to perform when the nfsd service is started: */
  7702. int
  7703. nfs4_state_start(void)
  7704. {
  7705. int ret;
  7706. ret = rhltable_init(&nfs4_file_rhltable, &nfs4_file_rhash_params);
  7707. if (ret)
  7708. return ret;
  7709. set_max_delegations();
  7710. return 0;
  7711. }
  7712. void
  7713. nfs4_state_shutdown_net(struct net *net)
  7714. {
  7715. struct nfs4_delegation *dp = NULL;
  7716. struct list_head *pos, *next, reaplist;
  7717. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  7718. shrinker_free(nn->nfsd_client_shrinker);
  7719. cancel_work_sync(&nn->nfsd_shrinker_work);
  7720. cancel_delayed_work_sync(&nn->laundromat_work);
  7721. locks_end_grace(&nn->nfsd4_manager);
  7722. INIT_LIST_HEAD(&reaplist);
  7723. spin_lock(&state_lock);
  7724. list_for_each_safe(pos, next, &nn->del_recall_lru) {
  7725. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  7726. unhash_delegation_locked(dp, SC_STATUS_CLOSED);
  7727. list_add(&dp->dl_recall_lru, &reaplist);
  7728. }
  7729. spin_unlock(&state_lock);
  7730. list_for_each_safe(pos, next, &reaplist) {
  7731. dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
  7732. list_del_init(&dp->dl_recall_lru);
  7733. destroy_unhashed_deleg(dp);
  7734. }
  7735. nfsd4_client_tracking_exit(net);
  7736. nfs4_state_destroy_net(net);
  7737. #ifdef CONFIG_NFSD_V4_2_INTER_SSC
  7738. nfsd4_ssc_shutdown_umount(nn);
  7739. #endif
  7740. }
  7741. void
  7742. nfs4_state_shutdown(void)
  7743. {
  7744. rhltable_destroy(&nfs4_file_rhltable);
  7745. }
  7746. static void
  7747. get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  7748. {
  7749. if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
  7750. CURRENT_STATEID(stateid))
  7751. memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
  7752. }
  7753. static void
  7754. put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
  7755. {
  7756. if (cstate->minorversion) {
  7757. memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
  7758. SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
  7759. }
  7760. }
  7761. void
  7762. clear_current_stateid(struct nfsd4_compound_state *cstate)
  7763. {
  7764. CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
  7765. }
  7766. /*
  7767. * functions to set current state id
  7768. */
  7769. void
  7770. nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
  7771. union nfsd4_op_u *u)
  7772. {
  7773. put_stateid(cstate, &u->open_downgrade.od_stateid);
  7774. }
  7775. void
  7776. nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
  7777. union nfsd4_op_u *u)
  7778. {
  7779. put_stateid(cstate, &u->open.op_stateid);
  7780. }
  7781. void
  7782. nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
  7783. union nfsd4_op_u *u)
  7784. {
  7785. put_stateid(cstate, &u->close.cl_stateid);
  7786. }
  7787. void
  7788. nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
  7789. union nfsd4_op_u *u)
  7790. {
  7791. put_stateid(cstate, &u->lock.lk_resp_stateid);
  7792. }
  7793. /*
  7794. * functions to consume current state id
  7795. */
  7796. void
  7797. nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
  7798. union nfsd4_op_u *u)
  7799. {
  7800. get_stateid(cstate, &u->open_downgrade.od_stateid);
  7801. }
  7802. void
  7803. nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
  7804. union nfsd4_op_u *u)
  7805. {
  7806. get_stateid(cstate, &u->delegreturn.dr_stateid);
  7807. }
  7808. void
  7809. nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
  7810. union nfsd4_op_u *u)
  7811. {
  7812. get_stateid(cstate, &u->free_stateid.fr_stateid);
  7813. }
  7814. void
  7815. nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
  7816. union nfsd4_op_u *u)
  7817. {
  7818. get_stateid(cstate, &u->setattr.sa_stateid);
  7819. }
  7820. void
  7821. nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
  7822. union nfsd4_op_u *u)
  7823. {
  7824. get_stateid(cstate, &u->close.cl_stateid);
  7825. }
  7826. void
  7827. nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
  7828. union nfsd4_op_u *u)
  7829. {
  7830. get_stateid(cstate, &u->locku.lu_stateid);
  7831. }
  7832. void
  7833. nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
  7834. union nfsd4_op_u *u)
  7835. {
  7836. get_stateid(cstate, &u->read.rd_stateid);
  7837. }
  7838. void
  7839. nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
  7840. union nfsd4_op_u *u)
  7841. {
  7842. get_stateid(cstate, &u->write.wr_stateid);
  7843. }
  7844. /**
  7845. * nfsd4_deleg_getattr_conflict - Recall if GETATTR causes conflict
  7846. * @rqstp: RPC transaction context
  7847. * @dentry: dentry of inode to be checked for a conflict
  7848. * @modified: return true if file was modified
  7849. * @size: new size of file if modified is true
  7850. *
  7851. * This function is called when there is a conflict between a write
  7852. * delegation and a change/size GETATTR from another client. The server
  7853. * must either use the CB_GETATTR to get the current values of the
  7854. * attributes from the client that holds the delegation or recall the
  7855. * delegation before replying to the GETATTR. See RFC 8881 section
  7856. * 18.7.4.
  7857. *
  7858. * Returns 0 if there is no conflict; otherwise an nfs_stat
  7859. * code is returned.
  7860. */
  7861. __be32
  7862. nfsd4_deleg_getattr_conflict(struct svc_rqst *rqstp, struct dentry *dentry,
  7863. bool *modified, u64 *size)
  7864. {
  7865. __be32 status;
  7866. struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
  7867. struct file_lock_context *ctx;
  7868. struct nfs4_delegation *dp = NULL;
  7869. struct file_lease *fl;
  7870. struct iattr attrs;
  7871. struct nfs4_cb_fattr *ncf;
  7872. struct inode *inode = d_inode(dentry);
  7873. *modified = false;
  7874. ctx = locks_inode_context(inode);
  7875. if (!ctx)
  7876. return 0;
  7877. #define NON_NFSD_LEASE ((void *)1)
  7878. spin_lock(&ctx->flc_lock);
  7879. for_each_file_lock(fl, &ctx->flc_lease) {
  7880. if (fl->c.flc_flags == FL_LAYOUT)
  7881. continue;
  7882. if (fl->c.flc_type == F_WRLCK) {
  7883. if (fl->fl_lmops == &nfsd_lease_mng_ops)
  7884. dp = fl->c.flc_owner;
  7885. else
  7886. dp = NON_NFSD_LEASE;
  7887. }
  7888. break;
  7889. }
  7890. if (dp == NULL || dp == NON_NFSD_LEASE ||
  7891. dp->dl_recall.cb_clp == *(rqstp->rq_lease_breaker)) {
  7892. spin_unlock(&ctx->flc_lock);
  7893. if (dp == NON_NFSD_LEASE) {
  7894. status = nfserrno(nfsd_open_break_lease(inode,
  7895. NFSD_MAY_READ));
  7896. if (status != nfserr_jukebox ||
  7897. !nfsd_wait_for_delegreturn(rqstp, inode))
  7898. return status;
  7899. }
  7900. return 0;
  7901. }
  7902. nfsd_stats_wdeleg_getattr_inc(nn);
  7903. refcount_inc(&dp->dl_stid.sc_count);
  7904. ncf = &dp->dl_cb_fattr;
  7905. nfs4_cb_getattr(&dp->dl_cb_fattr);
  7906. spin_unlock(&ctx->flc_lock);
  7907. wait_on_bit_timeout(&ncf->ncf_cb_flags, CB_GETATTR_BUSY,
  7908. TASK_INTERRUPTIBLE, NFSD_CB_GETATTR_TIMEOUT);
  7909. if (ncf->ncf_cb_status) {
  7910. /* Recall delegation only if client didn't respond */
  7911. status = nfserrno(nfsd_open_break_lease(inode, NFSD_MAY_READ));
  7912. if (status != nfserr_jukebox ||
  7913. !nfsd_wait_for_delegreturn(rqstp, inode))
  7914. goto out_status;
  7915. }
  7916. if (!ncf->ncf_file_modified &&
  7917. (ncf->ncf_initial_cinfo != ncf->ncf_cb_change ||
  7918. ncf->ncf_cur_fsize != ncf->ncf_cb_fsize))
  7919. ncf->ncf_file_modified = true;
  7920. if (ncf->ncf_file_modified) {
  7921. int err;
  7922. /*
  7923. * Per section 10.4.3 of RFC 8881, the server would
  7924. * not update the file's metadata with the client's
  7925. * modified size
  7926. */
  7927. attrs.ia_mtime = attrs.ia_ctime = current_time(inode);
  7928. attrs.ia_valid = ATTR_MTIME | ATTR_CTIME | ATTR_DELEG;
  7929. inode_lock(inode);
  7930. err = notify_change(&nop_mnt_idmap, dentry, &attrs, NULL);
  7931. inode_unlock(inode);
  7932. if (err) {
  7933. status = nfserrno(err);
  7934. goto out_status;
  7935. }
  7936. ncf->ncf_cur_fsize = ncf->ncf_cb_fsize;
  7937. *size = ncf->ncf_cur_fsize;
  7938. *modified = true;
  7939. }
  7940. status = 0;
  7941. out_status:
  7942. nfs4_put_stid(&dp->dl_stid);
  7943. return status;
  7944. }