nfs4state.c 175 KB

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