nfs4state.c 166 KB

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