nfs4state.c 156 KB

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