nfs4state.c 185 KB

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