nfs4state.c 188 KB

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