nfs4state.c 148 KB

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