nfs4state.c 187 KB

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