nfs4state.c 148 KB

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