nfs4state.c 168 KB

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