nfs4state.c 186 KB

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