nfs4state.c 166 KB

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