nfs4state.c 152 KB

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