nfs4state.c 182 KB

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