nfs4state.c 171 KB

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