nfs4state.c 172 KB

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