nfs4state.c 168 KB

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