nfs4state.c 185 KB

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