nfs4state.c 153 KB

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