nfs4state.c 166 KB

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