nfs4state.c 188 KB

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