nfs4state.c 211 KB

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