nfs4state.c 184 KB

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