mds_client.c 172 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/fs.h>
  4. #include <linux/wait.h>
  5. #include <linux/slab.h>
  6. #include <linux/gfp.h>
  7. #include <linux/sched.h>
  8. #include <linux/debugfs.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/ratelimit.h>
  11. #include <linux/bits.h>
  12. #include <linux/ktime.h>
  13. #include <linux/bitmap.h>
  14. #include <linux/mnt_idmapping.h>
  15. #include "super.h"
  16. #include "mds_client.h"
  17. #include "crypto.h"
  18. #include <linux/ceph/ceph_features.h>
  19. #include <linux/ceph/messenger.h>
  20. #include <linux/ceph/decode.h>
  21. #include <linux/ceph/pagelist.h>
  22. #include <linux/ceph/auth.h>
  23. #include <linux/ceph/debugfs.h>
  24. #include <trace/events/ceph.h>
  25. #define RECONNECT_MAX_SIZE (INT_MAX - PAGE_SIZE)
  26. /*
  27. * A cluster of MDS (metadata server) daemons is responsible for
  28. * managing the file system namespace (the directory hierarchy and
  29. * inodes) and for coordinating shared access to storage. Metadata is
  30. * partitioning hierarchically across a number of servers, and that
  31. * partition varies over time as the cluster adjusts the distribution
  32. * in order to balance load.
  33. *
  34. * The MDS client is primarily responsible to managing synchronous
  35. * metadata requests for operations like open, unlink, and so forth.
  36. * If there is a MDS failure, we find out about it when we (possibly
  37. * request and) receive a new MDS map, and can resubmit affected
  38. * requests.
  39. *
  40. * For the most part, though, we take advantage of a lossless
  41. * communications channel to the MDS, and do not need to worry about
  42. * timing out or resubmitting requests.
  43. *
  44. * We maintain a stateful "session" with each MDS we interact with.
  45. * Within each session, we sent periodic heartbeat messages to ensure
  46. * any capabilities or leases we have been issues remain valid. If
  47. * the session times out and goes stale, our leases and capabilities
  48. * are no longer valid.
  49. */
  50. struct ceph_reconnect_state {
  51. struct ceph_mds_session *session;
  52. int nr_caps, nr_realms;
  53. struct ceph_pagelist *pagelist;
  54. unsigned msg_version;
  55. bool allow_multi;
  56. };
  57. static void __wake_requests(struct ceph_mds_client *mdsc,
  58. struct list_head *head);
  59. static void ceph_cap_release_work(struct work_struct *work);
  60. static void ceph_cap_reclaim_work(struct work_struct *work);
  61. static const struct ceph_connection_operations mds_con_ops;
  62. /*
  63. * mds reply parsing
  64. */
  65. static int parse_reply_info_quota(void **p, void *end,
  66. struct ceph_mds_reply_info_in *info)
  67. {
  68. u8 struct_v, struct_compat;
  69. u32 struct_len;
  70. ceph_decode_8_safe(p, end, struct_v, bad);
  71. ceph_decode_8_safe(p, end, struct_compat, bad);
  72. /* struct_v is expected to be >= 1. we only
  73. * understand encoding with struct_compat == 1. */
  74. if (!struct_v || struct_compat != 1)
  75. goto bad;
  76. ceph_decode_32_safe(p, end, struct_len, bad);
  77. ceph_decode_need(p, end, struct_len, bad);
  78. end = *p + struct_len;
  79. ceph_decode_64_safe(p, end, info->max_bytes, bad);
  80. ceph_decode_64_safe(p, end, info->max_files, bad);
  81. *p = end;
  82. return 0;
  83. bad:
  84. return -EIO;
  85. }
  86. /*
  87. * parse individual inode info
  88. */
  89. static int parse_reply_info_in(void **p, void *end,
  90. struct ceph_mds_reply_info_in *info,
  91. u64 features)
  92. {
  93. int err = 0;
  94. u8 struct_v = 0;
  95. if (features == (u64)-1) {
  96. u32 struct_len;
  97. u8 struct_compat;
  98. ceph_decode_8_safe(p, end, struct_v, bad);
  99. ceph_decode_8_safe(p, end, struct_compat, bad);
  100. /* struct_v is expected to be >= 1. we only understand
  101. * encoding with struct_compat == 1. */
  102. if (!struct_v || struct_compat != 1)
  103. goto bad;
  104. ceph_decode_32_safe(p, end, struct_len, bad);
  105. ceph_decode_need(p, end, struct_len, bad);
  106. end = *p + struct_len;
  107. }
  108. ceph_decode_need(p, end, sizeof(struct ceph_mds_reply_inode), bad);
  109. info->in = *p;
  110. *p += sizeof(struct ceph_mds_reply_inode) +
  111. sizeof(*info->in->fragtree.splits) *
  112. le32_to_cpu(info->in->fragtree.nsplits);
  113. ceph_decode_32_safe(p, end, info->symlink_len, bad);
  114. ceph_decode_need(p, end, info->symlink_len, bad);
  115. info->symlink = *p;
  116. *p += info->symlink_len;
  117. ceph_decode_copy_safe(p, end, &info->dir_layout,
  118. sizeof(info->dir_layout), bad);
  119. ceph_decode_32_safe(p, end, info->xattr_len, bad);
  120. ceph_decode_need(p, end, info->xattr_len, bad);
  121. info->xattr_data = *p;
  122. *p += info->xattr_len;
  123. if (features == (u64)-1) {
  124. /* inline data */
  125. ceph_decode_64_safe(p, end, info->inline_version, bad);
  126. ceph_decode_32_safe(p, end, info->inline_len, bad);
  127. ceph_decode_need(p, end, info->inline_len, bad);
  128. info->inline_data = *p;
  129. *p += info->inline_len;
  130. /* quota */
  131. err = parse_reply_info_quota(p, end, info);
  132. if (err < 0)
  133. goto out_bad;
  134. /* pool namespace */
  135. ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
  136. if (info->pool_ns_len > 0) {
  137. ceph_decode_need(p, end, info->pool_ns_len, bad);
  138. info->pool_ns_data = *p;
  139. *p += info->pool_ns_len;
  140. }
  141. /* btime */
  142. ceph_decode_need(p, end, sizeof(info->btime), bad);
  143. ceph_decode_copy(p, &info->btime, sizeof(info->btime));
  144. /* change attribute */
  145. ceph_decode_64_safe(p, end, info->change_attr, bad);
  146. /* dir pin */
  147. if (struct_v >= 2) {
  148. ceph_decode_32_safe(p, end, info->dir_pin, bad);
  149. } else {
  150. info->dir_pin = -ENODATA;
  151. }
  152. /* snapshot birth time, remains zero for v<=2 */
  153. if (struct_v >= 3) {
  154. ceph_decode_need(p, end, sizeof(info->snap_btime), bad);
  155. ceph_decode_copy(p, &info->snap_btime,
  156. sizeof(info->snap_btime));
  157. } else {
  158. memset(&info->snap_btime, 0, sizeof(info->snap_btime));
  159. }
  160. /* snapshot count, remains zero for v<=3 */
  161. if (struct_v >= 4) {
  162. ceph_decode_64_safe(p, end, info->rsnaps, bad);
  163. } else {
  164. info->rsnaps = 0;
  165. }
  166. if (struct_v >= 5) {
  167. u32 alen;
  168. ceph_decode_32_safe(p, end, alen, bad);
  169. while (alen--) {
  170. u32 len;
  171. /* key */
  172. ceph_decode_32_safe(p, end, len, bad);
  173. ceph_decode_skip_n(p, end, len, bad);
  174. /* value */
  175. ceph_decode_32_safe(p, end, len, bad);
  176. ceph_decode_skip_n(p, end, len, bad);
  177. }
  178. }
  179. /* fscrypt flag -- ignore */
  180. if (struct_v >= 6)
  181. ceph_decode_skip_8(p, end, bad);
  182. info->fscrypt_auth = NULL;
  183. info->fscrypt_auth_len = 0;
  184. info->fscrypt_file = NULL;
  185. info->fscrypt_file_len = 0;
  186. if (struct_v >= 7) {
  187. ceph_decode_32_safe(p, end, info->fscrypt_auth_len, bad);
  188. if (info->fscrypt_auth_len) {
  189. info->fscrypt_auth = kmalloc(info->fscrypt_auth_len,
  190. GFP_KERNEL);
  191. if (!info->fscrypt_auth)
  192. return -ENOMEM;
  193. ceph_decode_copy_safe(p, end, info->fscrypt_auth,
  194. info->fscrypt_auth_len, bad);
  195. }
  196. ceph_decode_32_safe(p, end, info->fscrypt_file_len, bad);
  197. if (info->fscrypt_file_len) {
  198. info->fscrypt_file = kmalloc(info->fscrypt_file_len,
  199. GFP_KERNEL);
  200. if (!info->fscrypt_file)
  201. return -ENOMEM;
  202. ceph_decode_copy_safe(p, end, info->fscrypt_file,
  203. info->fscrypt_file_len, bad);
  204. }
  205. }
  206. *p = end;
  207. } else {
  208. /* legacy (unversioned) struct */
  209. if (features & CEPH_FEATURE_MDS_INLINE_DATA) {
  210. ceph_decode_64_safe(p, end, info->inline_version, bad);
  211. ceph_decode_32_safe(p, end, info->inline_len, bad);
  212. ceph_decode_need(p, end, info->inline_len, bad);
  213. info->inline_data = *p;
  214. *p += info->inline_len;
  215. } else
  216. info->inline_version = CEPH_INLINE_NONE;
  217. if (features & CEPH_FEATURE_MDS_QUOTA) {
  218. err = parse_reply_info_quota(p, end, info);
  219. if (err < 0)
  220. goto out_bad;
  221. } else {
  222. info->max_bytes = 0;
  223. info->max_files = 0;
  224. }
  225. info->pool_ns_len = 0;
  226. info->pool_ns_data = NULL;
  227. if (features & CEPH_FEATURE_FS_FILE_LAYOUT_V2) {
  228. ceph_decode_32_safe(p, end, info->pool_ns_len, bad);
  229. if (info->pool_ns_len > 0) {
  230. ceph_decode_need(p, end, info->pool_ns_len, bad);
  231. info->pool_ns_data = *p;
  232. *p += info->pool_ns_len;
  233. }
  234. }
  235. if (features & CEPH_FEATURE_FS_BTIME) {
  236. ceph_decode_need(p, end, sizeof(info->btime), bad);
  237. ceph_decode_copy(p, &info->btime, sizeof(info->btime));
  238. ceph_decode_64_safe(p, end, info->change_attr, bad);
  239. }
  240. info->dir_pin = -ENODATA;
  241. /* info->snap_btime and info->rsnaps remain zero */
  242. }
  243. return 0;
  244. bad:
  245. err = -EIO;
  246. out_bad:
  247. return err;
  248. }
  249. static int parse_reply_info_dir(void **p, void *end,
  250. struct ceph_mds_reply_dirfrag **dirfrag,
  251. u64 features)
  252. {
  253. if (features == (u64)-1) {
  254. u8 struct_v, struct_compat;
  255. u32 struct_len;
  256. ceph_decode_8_safe(p, end, struct_v, bad);
  257. ceph_decode_8_safe(p, end, struct_compat, bad);
  258. /* struct_v is expected to be >= 1. we only understand
  259. * encoding whose struct_compat == 1. */
  260. if (!struct_v || struct_compat != 1)
  261. goto bad;
  262. ceph_decode_32_safe(p, end, struct_len, bad);
  263. ceph_decode_need(p, end, struct_len, bad);
  264. end = *p + struct_len;
  265. }
  266. ceph_decode_need(p, end, sizeof(**dirfrag), bad);
  267. *dirfrag = *p;
  268. *p += sizeof(**dirfrag) + sizeof(u32) * le32_to_cpu((*dirfrag)->ndist);
  269. if (unlikely(*p > end))
  270. goto bad;
  271. if (features == (u64)-1)
  272. *p = end;
  273. return 0;
  274. bad:
  275. return -EIO;
  276. }
  277. static int parse_reply_info_lease(void **p, void *end,
  278. struct ceph_mds_reply_lease **lease,
  279. u64 features, u32 *altname_len, u8 **altname)
  280. {
  281. u8 struct_v;
  282. u32 struct_len;
  283. void *lend;
  284. if (features == (u64)-1) {
  285. u8 struct_compat;
  286. ceph_decode_8_safe(p, end, struct_v, bad);
  287. ceph_decode_8_safe(p, end, struct_compat, bad);
  288. /* struct_v is expected to be >= 1. we only understand
  289. * encoding whose struct_compat == 1. */
  290. if (!struct_v || struct_compat != 1)
  291. goto bad;
  292. ceph_decode_32_safe(p, end, struct_len, bad);
  293. } else {
  294. struct_len = sizeof(**lease);
  295. *altname_len = 0;
  296. *altname = NULL;
  297. }
  298. lend = *p + struct_len;
  299. ceph_decode_need(p, end, struct_len, bad);
  300. *lease = *p;
  301. *p += sizeof(**lease);
  302. if (features == (u64)-1) {
  303. if (struct_v >= 2) {
  304. ceph_decode_32_safe(p, end, *altname_len, bad);
  305. ceph_decode_need(p, end, *altname_len, bad);
  306. *altname = *p;
  307. *p += *altname_len;
  308. } else {
  309. *altname = NULL;
  310. *altname_len = 0;
  311. }
  312. }
  313. *p = lend;
  314. return 0;
  315. bad:
  316. return -EIO;
  317. }
  318. /*
  319. * parse a normal reply, which may contain a (dir+)dentry and/or a
  320. * target inode.
  321. */
  322. static int parse_reply_info_trace(void **p, void *end,
  323. struct ceph_mds_reply_info_parsed *info,
  324. u64 features)
  325. {
  326. int err;
  327. if (info->head->is_dentry) {
  328. err = parse_reply_info_in(p, end, &info->diri, features);
  329. if (err < 0)
  330. goto out_bad;
  331. err = parse_reply_info_dir(p, end, &info->dirfrag, features);
  332. if (err < 0)
  333. goto out_bad;
  334. ceph_decode_32_safe(p, end, info->dname_len, bad);
  335. ceph_decode_need(p, end, info->dname_len, bad);
  336. info->dname = *p;
  337. *p += info->dname_len;
  338. err = parse_reply_info_lease(p, end, &info->dlease, features,
  339. &info->altname_len, &info->altname);
  340. if (err < 0)
  341. goto out_bad;
  342. }
  343. if (info->head->is_target) {
  344. err = parse_reply_info_in(p, end, &info->targeti, features);
  345. if (err < 0)
  346. goto out_bad;
  347. }
  348. if (unlikely(*p != end))
  349. goto bad;
  350. return 0;
  351. bad:
  352. err = -EIO;
  353. out_bad:
  354. pr_err("problem parsing mds trace %d\n", err);
  355. return err;
  356. }
  357. /*
  358. * parse readdir results
  359. */
  360. static int parse_reply_info_readdir(void **p, void *end,
  361. struct ceph_mds_request *req,
  362. u64 features)
  363. {
  364. struct ceph_mds_reply_info_parsed *info = &req->r_reply_info;
  365. struct ceph_client *cl = req->r_mdsc->fsc->client;
  366. u32 num, i = 0;
  367. int err;
  368. err = parse_reply_info_dir(p, end, &info->dir_dir, features);
  369. if (err < 0)
  370. goto out_bad;
  371. ceph_decode_need(p, end, sizeof(num) + 2, bad);
  372. num = ceph_decode_32(p);
  373. {
  374. u16 flags = ceph_decode_16(p);
  375. info->dir_end = !!(flags & CEPH_READDIR_FRAG_END);
  376. info->dir_complete = !!(flags & CEPH_READDIR_FRAG_COMPLETE);
  377. info->hash_order = !!(flags & CEPH_READDIR_HASH_ORDER);
  378. info->offset_hash = !!(flags & CEPH_READDIR_OFFSET_HASH);
  379. }
  380. if (num == 0)
  381. goto done;
  382. BUG_ON(!info->dir_entries);
  383. if ((unsigned long)(info->dir_entries + num) >
  384. (unsigned long)info->dir_entries + info->dir_buf_size) {
  385. pr_err_client(cl, "dir contents are larger than expected\n");
  386. WARN_ON(1);
  387. goto bad;
  388. }
  389. info->dir_nr = num;
  390. while (num) {
  391. struct inode *inode = d_inode(req->r_dentry);
  392. struct ceph_inode_info *ci = ceph_inode(inode);
  393. struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
  394. struct fscrypt_str tname = FSTR_INIT(NULL, 0);
  395. struct fscrypt_str oname = FSTR_INIT(NULL, 0);
  396. struct ceph_fname fname;
  397. u32 altname_len, _name_len;
  398. u8 *altname, *_name;
  399. /* dentry */
  400. ceph_decode_32_safe(p, end, _name_len, bad);
  401. ceph_decode_need(p, end, _name_len, bad);
  402. _name = *p;
  403. *p += _name_len;
  404. doutc(cl, "parsed dir dname '%.*s'\n", _name_len, _name);
  405. if (info->hash_order)
  406. rde->raw_hash = ceph_str_hash(ci->i_dir_layout.dl_dir_hash,
  407. _name, _name_len);
  408. /* dentry lease */
  409. err = parse_reply_info_lease(p, end, &rde->lease, features,
  410. &altname_len, &altname);
  411. if (err)
  412. goto out_bad;
  413. /*
  414. * Try to dencrypt the dentry names and update them
  415. * in the ceph_mds_reply_dir_entry struct.
  416. */
  417. fname.dir = inode;
  418. fname.name = _name;
  419. fname.name_len = _name_len;
  420. fname.ctext = altname;
  421. fname.ctext_len = altname_len;
  422. /*
  423. * The _name_len maybe larger than altname_len, such as
  424. * when the human readable name length is in range of
  425. * (CEPH_NOHASH_NAME_MAX, CEPH_NOHASH_NAME_MAX + SHA256_DIGEST_SIZE),
  426. * then the copy in ceph_fname_to_usr will corrupt the
  427. * data if there has no encryption key.
  428. *
  429. * Just set the no_copy flag and then if there has no
  430. * encryption key the oname.name will be assigned to
  431. * _name always.
  432. */
  433. fname.no_copy = true;
  434. if (altname_len == 0) {
  435. /*
  436. * Set tname to _name, and this will be used
  437. * to do the base64_decode in-place. It's
  438. * safe because the decoded string should
  439. * always be shorter, which is 3/4 of origin
  440. * string.
  441. */
  442. tname.name = _name;
  443. /*
  444. * Set oname to _name too, and this will be
  445. * used to do the dencryption in-place.
  446. */
  447. oname.name = _name;
  448. oname.len = _name_len;
  449. } else {
  450. /*
  451. * This will do the decryption only in-place
  452. * from altname cryptext directly.
  453. */
  454. oname.name = altname;
  455. oname.len = altname_len;
  456. }
  457. rde->is_nokey = false;
  458. err = ceph_fname_to_usr(&fname, &tname, &oname, &rde->is_nokey);
  459. if (err) {
  460. pr_err_client(cl, "unable to decode %.*s, got %d\n",
  461. _name_len, _name, err);
  462. goto out_bad;
  463. }
  464. rde->name = oname.name;
  465. rde->name_len = oname.len;
  466. /* inode */
  467. err = parse_reply_info_in(p, end, &rde->inode, features);
  468. if (err < 0)
  469. goto out_bad;
  470. /* ceph_readdir_prepopulate() will update it */
  471. rde->offset = 0;
  472. i++;
  473. num--;
  474. }
  475. done:
  476. /* Skip over any unrecognized fields */
  477. *p = end;
  478. return 0;
  479. bad:
  480. err = -EIO;
  481. out_bad:
  482. pr_err_client(cl, "problem parsing dir contents %d\n", err);
  483. return err;
  484. }
  485. /*
  486. * parse fcntl F_GETLK results
  487. */
  488. static int parse_reply_info_filelock(void **p, void *end,
  489. struct ceph_mds_reply_info_parsed *info,
  490. u64 features)
  491. {
  492. if (*p + sizeof(*info->filelock_reply) > end)
  493. goto bad;
  494. info->filelock_reply = *p;
  495. /* Skip over any unrecognized fields */
  496. *p = end;
  497. return 0;
  498. bad:
  499. return -EIO;
  500. }
  501. #if BITS_PER_LONG == 64
  502. #define DELEGATED_INO_AVAILABLE xa_mk_value(1)
  503. static int ceph_parse_deleg_inos(void **p, void *end,
  504. struct ceph_mds_session *s)
  505. {
  506. struct ceph_client *cl = s->s_mdsc->fsc->client;
  507. u32 sets;
  508. ceph_decode_32_safe(p, end, sets, bad);
  509. doutc(cl, "got %u sets of delegated inodes\n", sets);
  510. while (sets--) {
  511. u64 start, len;
  512. ceph_decode_64_safe(p, end, start, bad);
  513. ceph_decode_64_safe(p, end, len, bad);
  514. /* Don't accept a delegation of system inodes */
  515. if (start < CEPH_INO_SYSTEM_BASE) {
  516. pr_warn_ratelimited_client(cl,
  517. "ignoring reserved inode range delegation (start=0x%llx len=0x%llx)\n",
  518. start, len);
  519. continue;
  520. }
  521. while (len--) {
  522. int err = xa_insert(&s->s_delegated_inos, start++,
  523. DELEGATED_INO_AVAILABLE,
  524. GFP_KERNEL);
  525. if (!err) {
  526. doutc(cl, "added delegated inode 0x%llx\n", start - 1);
  527. } else if (err == -EBUSY) {
  528. pr_warn_client(cl,
  529. "MDS delegated inode 0x%llx more than once.\n",
  530. start - 1);
  531. } else {
  532. return err;
  533. }
  534. }
  535. }
  536. return 0;
  537. bad:
  538. return -EIO;
  539. }
  540. u64 ceph_get_deleg_ino(struct ceph_mds_session *s)
  541. {
  542. unsigned long ino;
  543. void *val;
  544. xa_for_each(&s->s_delegated_inos, ino, val) {
  545. val = xa_erase(&s->s_delegated_inos, ino);
  546. if (val == DELEGATED_INO_AVAILABLE)
  547. return ino;
  548. }
  549. return 0;
  550. }
  551. int ceph_restore_deleg_ino(struct ceph_mds_session *s, u64 ino)
  552. {
  553. return xa_insert(&s->s_delegated_inos, ino, DELEGATED_INO_AVAILABLE,
  554. GFP_KERNEL);
  555. }
  556. #else /* BITS_PER_LONG == 64 */
  557. /*
  558. * FIXME: xarrays can't handle 64-bit indexes on a 32-bit arch. For now, just
  559. * ignore delegated_inos on 32 bit arch. Maybe eventually add xarrays for top
  560. * and bottom words?
  561. */
  562. static int ceph_parse_deleg_inos(void **p, void *end,
  563. struct ceph_mds_session *s)
  564. {
  565. u32 sets;
  566. ceph_decode_32_safe(p, end, sets, bad);
  567. if (sets)
  568. ceph_decode_skip_n(p, end, sets * 2 * sizeof(__le64), bad);
  569. return 0;
  570. bad:
  571. return -EIO;
  572. }
  573. u64 ceph_get_deleg_ino(struct ceph_mds_session *s)
  574. {
  575. return 0;
  576. }
  577. int ceph_restore_deleg_ino(struct ceph_mds_session *s, u64 ino)
  578. {
  579. return 0;
  580. }
  581. #endif /* BITS_PER_LONG == 64 */
  582. /*
  583. * parse create results
  584. */
  585. static int parse_reply_info_create(void **p, void *end,
  586. struct ceph_mds_reply_info_parsed *info,
  587. u64 features, struct ceph_mds_session *s)
  588. {
  589. int ret;
  590. if (features == (u64)-1 ||
  591. (features & CEPH_FEATURE_REPLY_CREATE_INODE)) {
  592. if (*p == end) {
  593. /* Malformed reply? */
  594. info->has_create_ino = false;
  595. } else if (test_bit(CEPHFS_FEATURE_DELEG_INO, &s->s_features)) {
  596. info->has_create_ino = true;
  597. /* struct_v, struct_compat, and len */
  598. ceph_decode_skip_n(p, end, 2 + sizeof(u32), bad);
  599. ceph_decode_64_safe(p, end, info->ino, bad);
  600. ret = ceph_parse_deleg_inos(p, end, s);
  601. if (ret)
  602. return ret;
  603. } else {
  604. /* legacy */
  605. ceph_decode_64_safe(p, end, info->ino, bad);
  606. info->has_create_ino = true;
  607. }
  608. } else {
  609. if (*p != end)
  610. goto bad;
  611. }
  612. /* Skip over any unrecognized fields */
  613. *p = end;
  614. return 0;
  615. bad:
  616. return -EIO;
  617. }
  618. static int parse_reply_info_getvxattr(void **p, void *end,
  619. struct ceph_mds_reply_info_parsed *info,
  620. u64 features)
  621. {
  622. u32 value_len;
  623. ceph_decode_skip_8(p, end, bad); /* skip current version: 1 */
  624. ceph_decode_skip_8(p, end, bad); /* skip first version: 1 */
  625. ceph_decode_skip_32(p, end, bad); /* skip payload length */
  626. ceph_decode_32_safe(p, end, value_len, bad);
  627. if (value_len == end - *p) {
  628. info->xattr_info.xattr_value = *p;
  629. info->xattr_info.xattr_value_len = value_len;
  630. *p = end;
  631. return value_len;
  632. }
  633. bad:
  634. return -EIO;
  635. }
  636. /*
  637. * parse extra results
  638. */
  639. static int parse_reply_info_extra(void **p, void *end,
  640. struct ceph_mds_request *req,
  641. u64 features, struct ceph_mds_session *s)
  642. {
  643. struct ceph_mds_reply_info_parsed *info = &req->r_reply_info;
  644. u32 op = le32_to_cpu(info->head->op);
  645. if (op == CEPH_MDS_OP_GETFILELOCK)
  646. return parse_reply_info_filelock(p, end, info, features);
  647. else if (op == CEPH_MDS_OP_READDIR || op == CEPH_MDS_OP_LSSNAP)
  648. return parse_reply_info_readdir(p, end, req, features);
  649. else if (op == CEPH_MDS_OP_CREATE)
  650. return parse_reply_info_create(p, end, info, features, s);
  651. else if (op == CEPH_MDS_OP_GETVXATTR)
  652. return parse_reply_info_getvxattr(p, end, info, features);
  653. else
  654. return -EIO;
  655. }
  656. /*
  657. * parse entire mds reply
  658. */
  659. static int parse_reply_info(struct ceph_mds_session *s, struct ceph_msg *msg,
  660. struct ceph_mds_request *req, u64 features)
  661. {
  662. struct ceph_mds_reply_info_parsed *info = &req->r_reply_info;
  663. struct ceph_client *cl = s->s_mdsc->fsc->client;
  664. void *p, *end;
  665. u32 len;
  666. int err;
  667. info->head = msg->front.iov_base;
  668. p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head);
  669. end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head);
  670. /* trace */
  671. ceph_decode_32_safe(&p, end, len, bad);
  672. if (len > 0) {
  673. ceph_decode_need(&p, end, len, bad);
  674. err = parse_reply_info_trace(&p, p+len, info, features);
  675. if (err < 0)
  676. goto out_bad;
  677. }
  678. /* extra */
  679. ceph_decode_32_safe(&p, end, len, bad);
  680. if (len > 0) {
  681. ceph_decode_need(&p, end, len, bad);
  682. err = parse_reply_info_extra(&p, p+len, req, features, s);
  683. if (err < 0)
  684. goto out_bad;
  685. }
  686. /* snap blob */
  687. ceph_decode_32_safe(&p, end, len, bad);
  688. info->snapblob_len = len;
  689. info->snapblob = p;
  690. p += len;
  691. if (p != end)
  692. goto bad;
  693. return 0;
  694. bad:
  695. err = -EIO;
  696. out_bad:
  697. pr_err_client(cl, "mds parse_reply err %d\n", err);
  698. ceph_msg_dump(msg);
  699. return err;
  700. }
  701. static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info)
  702. {
  703. int i;
  704. kfree(info->diri.fscrypt_auth);
  705. kfree(info->diri.fscrypt_file);
  706. kfree(info->targeti.fscrypt_auth);
  707. kfree(info->targeti.fscrypt_file);
  708. if (!info->dir_entries)
  709. return;
  710. for (i = 0; i < info->dir_nr; i++) {
  711. struct ceph_mds_reply_dir_entry *rde = info->dir_entries + i;
  712. kfree(rde->inode.fscrypt_auth);
  713. kfree(rde->inode.fscrypt_file);
  714. }
  715. free_pages((unsigned long)info->dir_entries, get_order(info->dir_buf_size));
  716. }
  717. /*
  718. * In async unlink case the kclient won't wait for the first reply
  719. * from MDS and just drop all the links and unhash the dentry and then
  720. * succeeds immediately.
  721. *
  722. * For any new create/link/rename,etc requests followed by using the
  723. * same file names we must wait for the first reply of the inflight
  724. * unlink request, or the MDS possibly will fail these following
  725. * requests with -EEXIST if the inflight async unlink request was
  726. * delayed for some reasons.
  727. *
  728. * And the worst case is that for the none async openc request it will
  729. * successfully open the file if the CDentry hasn't been unlinked yet,
  730. * but later the previous delayed async unlink request will remove the
  731. * CDentry. That means the just created file is possibly deleted later
  732. * by accident.
  733. *
  734. * We need to wait for the inflight async unlink requests to finish
  735. * when creating new files/directories by using the same file names.
  736. */
  737. int ceph_wait_on_conflict_unlink(struct dentry *dentry)
  738. {
  739. struct ceph_fs_client *fsc = ceph_sb_to_fs_client(dentry->d_sb);
  740. struct ceph_client *cl = fsc->client;
  741. struct dentry *pdentry = dentry->d_parent;
  742. struct dentry *udentry, *found = NULL;
  743. struct ceph_dentry_info *di;
  744. struct qstr dname;
  745. u32 hash = dentry->d_name.hash;
  746. int err;
  747. dname.name = dentry->d_name.name;
  748. dname.len = dentry->d_name.len;
  749. rcu_read_lock();
  750. hash_for_each_possible_rcu(fsc->async_unlink_conflict, di,
  751. hnode, hash) {
  752. udentry = di->dentry;
  753. spin_lock(&udentry->d_lock);
  754. if (udentry->d_name.hash != hash)
  755. goto next;
  756. if (unlikely(udentry->d_parent != pdentry))
  757. goto next;
  758. if (!hash_hashed(&di->hnode))
  759. goto next;
  760. if (!test_bit(CEPH_DENTRY_ASYNC_UNLINK_BIT, &di->flags))
  761. pr_warn_client(cl, "dentry %p:%pd async unlink bit is not set\n",
  762. dentry, dentry);
  763. if (!d_same_name(udentry, pdentry, &dname))
  764. goto next;
  765. found = dget_dlock(udentry);
  766. spin_unlock(&udentry->d_lock);
  767. break;
  768. next:
  769. spin_unlock(&udentry->d_lock);
  770. }
  771. rcu_read_unlock();
  772. if (likely(!found))
  773. return 0;
  774. doutc(cl, "dentry %p:%pd conflict with old %p:%pd\n", dentry, dentry,
  775. found, found);
  776. err = wait_on_bit(&di->flags, CEPH_DENTRY_ASYNC_UNLINK_BIT,
  777. TASK_KILLABLE);
  778. dput(found);
  779. return err;
  780. }
  781. /*
  782. * sessions
  783. */
  784. const char *ceph_session_state_name(int s)
  785. {
  786. switch (s) {
  787. case CEPH_MDS_SESSION_NEW: return "new";
  788. case CEPH_MDS_SESSION_OPENING: return "opening";
  789. case CEPH_MDS_SESSION_OPEN: return "open";
  790. case CEPH_MDS_SESSION_HUNG: return "hung";
  791. case CEPH_MDS_SESSION_CLOSING: return "closing";
  792. case CEPH_MDS_SESSION_CLOSED: return "closed";
  793. case CEPH_MDS_SESSION_RESTARTING: return "restarting";
  794. case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting";
  795. case CEPH_MDS_SESSION_REJECTED: return "rejected";
  796. default: return "???";
  797. }
  798. }
  799. struct ceph_mds_session *ceph_get_mds_session(struct ceph_mds_session *s)
  800. {
  801. if (refcount_inc_not_zero(&s->s_ref))
  802. return s;
  803. return NULL;
  804. }
  805. void ceph_put_mds_session(struct ceph_mds_session *s)
  806. {
  807. if (IS_ERR_OR_NULL(s))
  808. return;
  809. if (refcount_dec_and_test(&s->s_ref)) {
  810. if (s->s_auth.authorizer)
  811. ceph_auth_destroy_authorizer(s->s_auth.authorizer);
  812. WARN_ON(mutex_is_locked(&s->s_mutex));
  813. xa_destroy(&s->s_delegated_inos);
  814. kfree(s);
  815. }
  816. }
  817. /*
  818. * called under mdsc->mutex
  819. */
  820. struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc,
  821. int mds)
  822. {
  823. if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
  824. return NULL;
  825. return ceph_get_mds_session(mdsc->sessions[mds]);
  826. }
  827. static bool __have_session(struct ceph_mds_client *mdsc, int mds)
  828. {
  829. if (mds >= mdsc->max_sessions || !mdsc->sessions[mds])
  830. return false;
  831. else
  832. return true;
  833. }
  834. static int __verify_registered_session(struct ceph_mds_client *mdsc,
  835. struct ceph_mds_session *s)
  836. {
  837. if (s->s_mds >= mdsc->max_sessions ||
  838. mdsc->sessions[s->s_mds] != s)
  839. return -ENOENT;
  840. return 0;
  841. }
  842. /*
  843. * create+register a new session for given mds.
  844. * called under mdsc->mutex.
  845. */
  846. static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc,
  847. int mds)
  848. {
  849. struct ceph_client *cl = mdsc->fsc->client;
  850. struct ceph_mds_session *s;
  851. if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_FENCE_IO)
  852. return ERR_PTR(-EIO);
  853. if (mds >= mdsc->mdsmap->possible_max_rank)
  854. return ERR_PTR(-EINVAL);
  855. s = kzalloc_obj(*s, GFP_NOFS);
  856. if (!s)
  857. return ERR_PTR(-ENOMEM);
  858. if (mds >= mdsc->max_sessions) {
  859. int newmax = 1 << get_count_order(mds + 1);
  860. struct ceph_mds_session **sa;
  861. size_t ptr_size = sizeof(struct ceph_mds_session *);
  862. doutc(cl, "realloc to %d\n", newmax);
  863. sa = kcalloc(newmax, ptr_size, GFP_NOFS);
  864. if (!sa)
  865. goto fail_realloc;
  866. if (mdsc->sessions) {
  867. memcpy(sa, mdsc->sessions,
  868. mdsc->max_sessions * ptr_size);
  869. kfree(mdsc->sessions);
  870. }
  871. mdsc->sessions = sa;
  872. mdsc->max_sessions = newmax;
  873. }
  874. doutc(cl, "mds%d\n", mds);
  875. s->s_mdsc = mdsc;
  876. s->s_mds = mds;
  877. s->s_state = CEPH_MDS_SESSION_NEW;
  878. mutex_init(&s->s_mutex);
  879. ceph_con_init(&s->s_con, s, &mds_con_ops, &mdsc->fsc->client->msgr);
  880. atomic_set(&s->s_cap_gen, 1);
  881. s->s_cap_ttl = jiffies - 1;
  882. spin_lock_init(&s->s_cap_lock);
  883. INIT_LIST_HEAD(&s->s_caps);
  884. refcount_set(&s->s_ref, 1);
  885. INIT_LIST_HEAD(&s->s_waiting);
  886. INIT_LIST_HEAD(&s->s_unsafe);
  887. xa_init(&s->s_delegated_inos);
  888. INIT_LIST_HEAD(&s->s_cap_releases);
  889. INIT_WORK(&s->s_cap_release_work, ceph_cap_release_work);
  890. INIT_LIST_HEAD(&s->s_cap_dirty);
  891. INIT_LIST_HEAD(&s->s_cap_flushing);
  892. mdsc->sessions[mds] = s;
  893. atomic_inc(&mdsc->num_sessions);
  894. refcount_inc(&s->s_ref); /* one ref to sessions[], one to caller */
  895. ceph_con_open(&s->s_con, CEPH_ENTITY_TYPE_MDS, mds,
  896. ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
  897. return s;
  898. fail_realloc:
  899. kfree(s);
  900. return ERR_PTR(-ENOMEM);
  901. }
  902. /*
  903. * called under mdsc->mutex
  904. */
  905. static void __unregister_session(struct ceph_mds_client *mdsc,
  906. struct ceph_mds_session *s)
  907. {
  908. doutc(mdsc->fsc->client, "mds%d %p\n", s->s_mds, s);
  909. BUG_ON(mdsc->sessions[s->s_mds] != s);
  910. mdsc->sessions[s->s_mds] = NULL;
  911. ceph_con_close(&s->s_con);
  912. ceph_put_mds_session(s);
  913. atomic_dec(&mdsc->num_sessions);
  914. }
  915. /*
  916. * drop session refs in request.
  917. *
  918. * should be last request ref, or hold mdsc->mutex
  919. */
  920. static void put_request_session(struct ceph_mds_request *req)
  921. {
  922. if (req->r_session) {
  923. ceph_put_mds_session(req->r_session);
  924. req->r_session = NULL;
  925. }
  926. }
  927. void ceph_mdsc_iterate_sessions(struct ceph_mds_client *mdsc,
  928. void (*cb)(struct ceph_mds_session *),
  929. bool check_state)
  930. {
  931. int mds;
  932. mutex_lock(&mdsc->mutex);
  933. for (mds = 0; mds < mdsc->max_sessions; ++mds) {
  934. struct ceph_mds_session *s;
  935. s = __ceph_lookup_mds_session(mdsc, mds);
  936. if (!s)
  937. continue;
  938. if (check_state && !check_session_state(s)) {
  939. ceph_put_mds_session(s);
  940. continue;
  941. }
  942. mutex_unlock(&mdsc->mutex);
  943. cb(s);
  944. ceph_put_mds_session(s);
  945. mutex_lock(&mdsc->mutex);
  946. }
  947. mutex_unlock(&mdsc->mutex);
  948. }
  949. void ceph_mdsc_release_request(struct kref *kref)
  950. {
  951. struct ceph_mds_request *req = container_of(kref,
  952. struct ceph_mds_request,
  953. r_kref);
  954. ceph_mdsc_release_dir_caps_async(req);
  955. destroy_reply_info(&req->r_reply_info);
  956. if (req->r_request)
  957. ceph_msg_put(req->r_request);
  958. if (req->r_reply)
  959. ceph_msg_put(req->r_reply);
  960. if (req->r_inode) {
  961. ceph_put_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
  962. iput(req->r_inode);
  963. }
  964. if (req->r_parent) {
  965. ceph_put_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
  966. iput(req->r_parent);
  967. }
  968. iput(req->r_target_inode);
  969. iput(req->r_new_inode);
  970. if (req->r_dentry)
  971. dput(req->r_dentry);
  972. if (req->r_old_dentry)
  973. dput(req->r_old_dentry);
  974. if (req->r_old_dentry_dir) {
  975. /*
  976. * track (and drop pins for) r_old_dentry_dir
  977. * separately, since r_old_dentry's d_parent may have
  978. * changed between the dir mutex being dropped and
  979. * this request being freed.
  980. */
  981. ceph_put_cap_refs(ceph_inode(req->r_old_dentry_dir),
  982. CEPH_CAP_PIN);
  983. iput(req->r_old_dentry_dir);
  984. }
  985. kfree(req->r_path1);
  986. kfree(req->r_path2);
  987. put_cred(req->r_cred);
  988. if (req->r_mnt_idmap)
  989. mnt_idmap_put(req->r_mnt_idmap);
  990. if (req->r_pagelist)
  991. ceph_pagelist_release(req->r_pagelist);
  992. kfree(req->r_fscrypt_auth);
  993. kfree(req->r_altname);
  994. put_request_session(req);
  995. ceph_unreserve_caps(req->r_mdsc, &req->r_caps_reservation);
  996. WARN_ON_ONCE(!list_empty(&req->r_wait));
  997. kmem_cache_free(ceph_mds_request_cachep, req);
  998. }
  999. DEFINE_RB_FUNCS(request, struct ceph_mds_request, r_tid, r_node)
  1000. /*
  1001. * lookup session, bump ref if found.
  1002. *
  1003. * called under mdsc->mutex.
  1004. */
  1005. static struct ceph_mds_request *
  1006. lookup_get_request(struct ceph_mds_client *mdsc, u64 tid)
  1007. {
  1008. struct ceph_mds_request *req;
  1009. req = lookup_request(&mdsc->request_tree, tid);
  1010. if (req)
  1011. ceph_mdsc_get_request(req);
  1012. return req;
  1013. }
  1014. /*
  1015. * Register an in-flight request, and assign a tid. Link to directory
  1016. * are modifying (if any).
  1017. *
  1018. * Called under mdsc->mutex.
  1019. */
  1020. static void __register_request(struct ceph_mds_client *mdsc,
  1021. struct ceph_mds_request *req,
  1022. struct inode *dir)
  1023. {
  1024. struct ceph_client *cl = mdsc->fsc->client;
  1025. int ret = 0;
  1026. req->r_tid = ++mdsc->last_tid;
  1027. if (req->r_num_caps) {
  1028. ret = ceph_reserve_caps(mdsc, &req->r_caps_reservation,
  1029. req->r_num_caps);
  1030. if (ret < 0) {
  1031. pr_err_client(cl, "%p failed to reserve caps: %d\n",
  1032. req, ret);
  1033. /* set req->r_err to fail early from __do_request */
  1034. req->r_err = ret;
  1035. return;
  1036. }
  1037. }
  1038. doutc(cl, "%p tid %lld\n", req, req->r_tid);
  1039. ceph_mdsc_get_request(req);
  1040. insert_request(&mdsc->request_tree, req);
  1041. req->r_cred = get_current_cred();
  1042. if (!req->r_mnt_idmap)
  1043. req->r_mnt_idmap = &nop_mnt_idmap;
  1044. if (mdsc->oldest_tid == 0 && req->r_op != CEPH_MDS_OP_SETFILELOCK)
  1045. mdsc->oldest_tid = req->r_tid;
  1046. if (dir) {
  1047. struct ceph_inode_info *ci = ceph_inode(dir);
  1048. ihold(dir);
  1049. req->r_unsafe_dir = dir;
  1050. spin_lock(&ci->i_unsafe_lock);
  1051. list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops);
  1052. spin_unlock(&ci->i_unsafe_lock);
  1053. }
  1054. }
  1055. static void __unregister_request(struct ceph_mds_client *mdsc,
  1056. struct ceph_mds_request *req)
  1057. {
  1058. doutc(mdsc->fsc->client, "%p tid %lld\n", req, req->r_tid);
  1059. /* Never leave an unregistered request on an unsafe list! */
  1060. list_del_init(&req->r_unsafe_item);
  1061. if (req->r_tid == mdsc->oldest_tid) {
  1062. struct rb_node *p = rb_next(&req->r_node);
  1063. mdsc->oldest_tid = 0;
  1064. while (p) {
  1065. struct ceph_mds_request *next_req =
  1066. rb_entry(p, struct ceph_mds_request, r_node);
  1067. if (next_req->r_op != CEPH_MDS_OP_SETFILELOCK) {
  1068. mdsc->oldest_tid = next_req->r_tid;
  1069. break;
  1070. }
  1071. p = rb_next(p);
  1072. }
  1073. }
  1074. erase_request(&mdsc->request_tree, req);
  1075. if (req->r_unsafe_dir) {
  1076. struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir);
  1077. spin_lock(&ci->i_unsafe_lock);
  1078. list_del_init(&req->r_unsafe_dir_item);
  1079. spin_unlock(&ci->i_unsafe_lock);
  1080. }
  1081. if (req->r_target_inode &&
  1082. test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
  1083. struct ceph_inode_info *ci = ceph_inode(req->r_target_inode);
  1084. spin_lock(&ci->i_unsafe_lock);
  1085. list_del_init(&req->r_unsafe_target_item);
  1086. spin_unlock(&ci->i_unsafe_lock);
  1087. }
  1088. if (req->r_unsafe_dir) {
  1089. iput(req->r_unsafe_dir);
  1090. req->r_unsafe_dir = NULL;
  1091. }
  1092. complete_all(&req->r_safe_completion);
  1093. ceph_mdsc_put_request(req);
  1094. }
  1095. /*
  1096. * Walk back up the dentry tree until we hit a dentry representing a
  1097. * non-snapshot inode. We do this using the rcu_read_lock (which must be held
  1098. * when calling this) to ensure that the objects won't disappear while we're
  1099. * working with them. Once we hit a candidate dentry, we attempt to take a
  1100. * reference to it, and return that as the result.
  1101. */
  1102. static struct inode *get_nonsnap_parent(struct dentry *dentry)
  1103. {
  1104. struct inode *inode = NULL;
  1105. while (dentry && !IS_ROOT(dentry)) {
  1106. inode = d_inode_rcu(dentry);
  1107. if (!inode || ceph_snap(inode) == CEPH_NOSNAP)
  1108. break;
  1109. dentry = dentry->d_parent;
  1110. }
  1111. if (inode)
  1112. inode = igrab(inode);
  1113. return inode;
  1114. }
  1115. /*
  1116. * Choose mds to send request to next. If there is a hint set in the
  1117. * request (e.g., due to a prior forward hint from the mds), use that.
  1118. * Otherwise, consult frag tree and/or caps to identify the
  1119. * appropriate mds. If all else fails, choose randomly.
  1120. *
  1121. * Called under mdsc->mutex.
  1122. */
  1123. static int __choose_mds(struct ceph_mds_client *mdsc,
  1124. struct ceph_mds_request *req,
  1125. bool *random)
  1126. {
  1127. struct inode *inode;
  1128. struct ceph_inode_info *ci;
  1129. struct ceph_cap *cap;
  1130. int mode = req->r_direct_mode;
  1131. int mds = -1;
  1132. u32 hash = req->r_direct_hash;
  1133. bool is_hash = test_bit(CEPH_MDS_R_DIRECT_IS_HASH, &req->r_req_flags);
  1134. struct ceph_client *cl = mdsc->fsc->client;
  1135. if (random)
  1136. *random = false;
  1137. /*
  1138. * is there a specific mds we should try? ignore hint if we have
  1139. * no session and the mds is not up (active or recovering).
  1140. */
  1141. if (req->r_resend_mds >= 0 &&
  1142. (__have_session(mdsc, req->r_resend_mds) ||
  1143. ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) {
  1144. doutc(cl, "using resend_mds mds%d\n", req->r_resend_mds);
  1145. return req->r_resend_mds;
  1146. }
  1147. if (mode == USE_RANDOM_MDS)
  1148. goto random;
  1149. inode = NULL;
  1150. if (req->r_inode) {
  1151. if (ceph_snap(req->r_inode) != CEPH_SNAPDIR) {
  1152. inode = req->r_inode;
  1153. ihold(inode);
  1154. } else {
  1155. /* req->r_dentry is non-null for LSSNAP request */
  1156. rcu_read_lock();
  1157. inode = get_nonsnap_parent(req->r_dentry);
  1158. rcu_read_unlock();
  1159. doutc(cl, "using snapdir's parent %p %llx.%llx\n",
  1160. inode, ceph_vinop(inode));
  1161. }
  1162. } else if (req->r_dentry) {
  1163. /* ignore race with rename; old or new d_parent is okay */
  1164. struct dentry *parent;
  1165. struct inode *dir;
  1166. rcu_read_lock();
  1167. parent = READ_ONCE(req->r_dentry->d_parent);
  1168. dir = req->r_parent ? : d_inode_rcu(parent);
  1169. if (!dir || dir->i_sb != mdsc->fsc->sb) {
  1170. /* not this fs or parent went negative */
  1171. inode = d_inode(req->r_dentry);
  1172. if (inode)
  1173. ihold(inode);
  1174. } else if (ceph_snap(dir) != CEPH_NOSNAP) {
  1175. /* direct snapped/virtual snapdir requests
  1176. * based on parent dir inode */
  1177. inode = get_nonsnap_parent(parent);
  1178. doutc(cl, "using nonsnap parent %p %llx.%llx\n",
  1179. inode, ceph_vinop(inode));
  1180. } else {
  1181. /* dentry target */
  1182. inode = d_inode(req->r_dentry);
  1183. if (!inode || mode == USE_AUTH_MDS) {
  1184. /* dir + name */
  1185. inode = igrab(dir);
  1186. hash = ceph_dentry_hash(dir, req->r_dentry);
  1187. is_hash = true;
  1188. } else {
  1189. ihold(inode);
  1190. }
  1191. }
  1192. rcu_read_unlock();
  1193. }
  1194. if (!inode)
  1195. goto random;
  1196. doutc(cl, "%p %llx.%llx is_hash=%d (0x%x) mode %d\n", inode,
  1197. ceph_vinop(inode), (int)is_hash, hash, mode);
  1198. ci = ceph_inode(inode);
  1199. if (is_hash && S_ISDIR(inode->i_mode)) {
  1200. struct ceph_inode_frag frag;
  1201. int found;
  1202. ceph_choose_frag(ci, hash, &frag, &found);
  1203. if (found) {
  1204. if (mode == USE_ANY_MDS && frag.ndist > 0) {
  1205. u8 r;
  1206. /* choose a random replica */
  1207. get_random_bytes(&r, 1);
  1208. r %= frag.ndist;
  1209. mds = frag.dist[r];
  1210. doutc(cl, "%p %llx.%llx frag %u mds%d (%d/%d)\n",
  1211. inode, ceph_vinop(inode), frag.frag,
  1212. mds, (int)r, frag.ndist);
  1213. if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
  1214. CEPH_MDS_STATE_ACTIVE &&
  1215. !ceph_mdsmap_is_laggy(mdsc->mdsmap, mds))
  1216. goto out;
  1217. }
  1218. /* since this file/dir wasn't known to be
  1219. * replicated, then we want to look for the
  1220. * authoritative mds. */
  1221. if (frag.mds >= 0) {
  1222. /* choose auth mds */
  1223. mds = frag.mds;
  1224. doutc(cl, "%p %llx.%llx frag %u mds%d (auth)\n",
  1225. inode, ceph_vinop(inode), frag.frag, mds);
  1226. if (ceph_mdsmap_get_state(mdsc->mdsmap, mds) >=
  1227. CEPH_MDS_STATE_ACTIVE) {
  1228. if (!ceph_mdsmap_is_laggy(mdsc->mdsmap,
  1229. mds))
  1230. goto out;
  1231. }
  1232. }
  1233. mode = USE_AUTH_MDS;
  1234. }
  1235. }
  1236. spin_lock(&ci->i_ceph_lock);
  1237. cap = NULL;
  1238. if (mode == USE_AUTH_MDS)
  1239. cap = ci->i_auth_cap;
  1240. if (!cap && !RB_EMPTY_ROOT(&ci->i_caps))
  1241. cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node);
  1242. if (!cap) {
  1243. spin_unlock(&ci->i_ceph_lock);
  1244. iput(inode);
  1245. goto random;
  1246. }
  1247. mds = cap->session->s_mds;
  1248. doutc(cl, "%p %llx.%llx mds%d (%scap %p)\n", inode,
  1249. ceph_vinop(inode), mds,
  1250. cap == ci->i_auth_cap ? "auth " : "", cap);
  1251. spin_unlock(&ci->i_ceph_lock);
  1252. out:
  1253. iput(inode);
  1254. return mds;
  1255. random:
  1256. if (random)
  1257. *random = true;
  1258. mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap);
  1259. doutc(cl, "chose random mds%d\n", mds);
  1260. return mds;
  1261. }
  1262. /*
  1263. * session messages
  1264. */
  1265. struct ceph_msg *ceph_create_session_msg(u32 op, u64 seq)
  1266. {
  1267. struct ceph_msg *msg;
  1268. struct ceph_mds_session_head *h;
  1269. msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), GFP_NOFS,
  1270. false);
  1271. if (!msg) {
  1272. pr_err("ENOMEM creating session %s msg\n",
  1273. ceph_session_op_name(op));
  1274. return NULL;
  1275. }
  1276. h = msg->front.iov_base;
  1277. h->op = cpu_to_le32(op);
  1278. h->seq = cpu_to_le64(seq);
  1279. return msg;
  1280. }
  1281. static const unsigned char feature_bits[] = CEPHFS_FEATURES_CLIENT_SUPPORTED;
  1282. #define FEATURE_BYTES(c) (DIV_ROUND_UP((size_t)feature_bits[c - 1] + 1, 64) * 8)
  1283. static int encode_supported_features(void **p, void *end)
  1284. {
  1285. static const size_t count = ARRAY_SIZE(feature_bits);
  1286. if (count > 0) {
  1287. size_t i;
  1288. size_t size = FEATURE_BYTES(count);
  1289. unsigned long bit;
  1290. if (WARN_ON_ONCE(*p + 4 + size > end))
  1291. return -ERANGE;
  1292. ceph_encode_32(p, size);
  1293. memset(*p, 0, size);
  1294. for (i = 0; i < count; i++) {
  1295. bit = feature_bits[i];
  1296. ((unsigned char *)(*p))[bit / 8] |= BIT(bit % 8);
  1297. }
  1298. *p += size;
  1299. } else {
  1300. if (WARN_ON_ONCE(*p + 4 > end))
  1301. return -ERANGE;
  1302. ceph_encode_32(p, 0);
  1303. }
  1304. return 0;
  1305. }
  1306. static const unsigned char metric_bits[] = CEPHFS_METRIC_SPEC_CLIENT_SUPPORTED;
  1307. #define METRIC_BYTES(cnt) (DIV_ROUND_UP((size_t)metric_bits[cnt - 1] + 1, 64) * 8)
  1308. static int encode_metric_spec(void **p, void *end)
  1309. {
  1310. static const size_t count = ARRAY_SIZE(metric_bits);
  1311. /* header */
  1312. if (WARN_ON_ONCE(*p + 2 > end))
  1313. return -ERANGE;
  1314. ceph_encode_8(p, 1); /* version */
  1315. ceph_encode_8(p, 1); /* compat */
  1316. if (count > 0) {
  1317. size_t i;
  1318. size_t size = METRIC_BYTES(count);
  1319. if (WARN_ON_ONCE(*p + 4 + 4 + size > end))
  1320. return -ERANGE;
  1321. /* metric spec info length */
  1322. ceph_encode_32(p, 4 + size);
  1323. /* metric spec */
  1324. ceph_encode_32(p, size);
  1325. memset(*p, 0, size);
  1326. for (i = 0; i < count; i++)
  1327. ((unsigned char *)(*p))[i / 8] |= BIT(metric_bits[i] % 8);
  1328. *p += size;
  1329. } else {
  1330. if (WARN_ON_ONCE(*p + 4 + 4 > end))
  1331. return -ERANGE;
  1332. /* metric spec info length */
  1333. ceph_encode_32(p, 4);
  1334. /* metric spec */
  1335. ceph_encode_32(p, 0);
  1336. }
  1337. return 0;
  1338. }
  1339. /*
  1340. * session message, specialization for CEPH_SESSION_REQUEST_OPEN
  1341. * to include additional client metadata fields.
  1342. */
  1343. static struct ceph_msg *
  1344. create_session_full_msg(struct ceph_mds_client *mdsc, int op, u64 seq)
  1345. {
  1346. struct ceph_msg *msg;
  1347. struct ceph_mds_session_head *h;
  1348. int i;
  1349. int extra_bytes = 0;
  1350. int metadata_key_count = 0;
  1351. struct ceph_options *opt = mdsc->fsc->client->options;
  1352. struct ceph_mount_options *fsopt = mdsc->fsc->mount_options;
  1353. struct ceph_client *cl = mdsc->fsc->client;
  1354. size_t size, count;
  1355. void *p, *end;
  1356. int ret;
  1357. const char* metadata[][2] = {
  1358. {"hostname", mdsc->nodename},
  1359. {"kernel_version", init_utsname()->release},
  1360. {"entity_id", opt->name ? : ""},
  1361. {"root", fsopt->server_path ? : "/"},
  1362. {NULL, NULL}
  1363. };
  1364. /* Calculate serialized length of metadata */
  1365. extra_bytes = 4; /* map length */
  1366. for (i = 0; metadata[i][0]; ++i) {
  1367. extra_bytes += 8 + strlen(metadata[i][0]) +
  1368. strlen(metadata[i][1]);
  1369. metadata_key_count++;
  1370. }
  1371. /* supported feature */
  1372. size = 0;
  1373. count = ARRAY_SIZE(feature_bits);
  1374. if (count > 0)
  1375. size = FEATURE_BYTES(count);
  1376. extra_bytes += 4 + size;
  1377. /* metric spec */
  1378. size = 0;
  1379. count = ARRAY_SIZE(metric_bits);
  1380. if (count > 0)
  1381. size = METRIC_BYTES(count);
  1382. extra_bytes += 2 + 4 + 4 + size;
  1383. /* flags, mds auth caps and oldest_client_tid */
  1384. extra_bytes += 4 + 4 + 8;
  1385. /* Allocate the message */
  1386. msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h) + extra_bytes,
  1387. GFP_NOFS, false);
  1388. if (!msg) {
  1389. pr_err_client(cl, "ENOMEM creating session open msg\n");
  1390. return ERR_PTR(-ENOMEM);
  1391. }
  1392. p = msg->front.iov_base;
  1393. end = p + msg->front.iov_len;
  1394. h = p;
  1395. h->op = cpu_to_le32(op);
  1396. h->seq = cpu_to_le64(seq);
  1397. /*
  1398. * Serialize client metadata into waiting buffer space, using
  1399. * the format that userspace expects for map<string, string>
  1400. *
  1401. * ClientSession messages with metadata are v7
  1402. */
  1403. msg->hdr.version = cpu_to_le16(7);
  1404. msg->hdr.compat_version = cpu_to_le16(1);
  1405. /* The write pointer, following the session_head structure */
  1406. p += sizeof(*h);
  1407. /* Number of entries in the map */
  1408. ceph_encode_32(&p, metadata_key_count);
  1409. /* Two length-prefixed strings for each entry in the map */
  1410. for (i = 0; metadata[i][0]; ++i) {
  1411. size_t const key_len = strlen(metadata[i][0]);
  1412. size_t const val_len = strlen(metadata[i][1]);
  1413. ceph_encode_32(&p, key_len);
  1414. memcpy(p, metadata[i][0], key_len);
  1415. p += key_len;
  1416. ceph_encode_32(&p, val_len);
  1417. memcpy(p, metadata[i][1], val_len);
  1418. p += val_len;
  1419. }
  1420. ret = encode_supported_features(&p, end);
  1421. if (ret) {
  1422. pr_err_client(cl, "encode_supported_features failed!\n");
  1423. ceph_msg_put(msg);
  1424. return ERR_PTR(ret);
  1425. }
  1426. ret = encode_metric_spec(&p, end);
  1427. if (ret) {
  1428. pr_err_client(cl, "encode_metric_spec failed!\n");
  1429. ceph_msg_put(msg);
  1430. return ERR_PTR(ret);
  1431. }
  1432. /* version == 5, flags */
  1433. ceph_encode_32(&p, 0);
  1434. /* version == 6, mds auth caps */
  1435. ceph_encode_32(&p, 0);
  1436. /* version == 7, oldest_client_tid */
  1437. ceph_encode_64(&p, mdsc->oldest_tid);
  1438. msg->front.iov_len = p - msg->front.iov_base;
  1439. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1440. return msg;
  1441. }
  1442. /*
  1443. * send session open request.
  1444. *
  1445. * called under mdsc->mutex
  1446. */
  1447. static int __open_session(struct ceph_mds_client *mdsc,
  1448. struct ceph_mds_session *session)
  1449. {
  1450. struct ceph_msg *msg;
  1451. int mstate;
  1452. int mds = session->s_mds;
  1453. if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_FENCE_IO)
  1454. return -EIO;
  1455. /* wait for mds to go active? */
  1456. mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds);
  1457. doutc(mdsc->fsc->client, "open_session to mds%d (%s)\n", mds,
  1458. ceph_mds_state_name(mstate));
  1459. session->s_state = CEPH_MDS_SESSION_OPENING;
  1460. session->s_renew_requested = jiffies;
  1461. /* send connect message */
  1462. msg = create_session_full_msg(mdsc, CEPH_SESSION_REQUEST_OPEN,
  1463. session->s_seq);
  1464. if (IS_ERR(msg))
  1465. return PTR_ERR(msg);
  1466. ceph_con_send(&session->s_con, msg);
  1467. return 0;
  1468. }
  1469. /*
  1470. * open sessions for any export targets for the given mds
  1471. *
  1472. * called under mdsc->mutex
  1473. */
  1474. static struct ceph_mds_session *
  1475. __open_export_target_session(struct ceph_mds_client *mdsc, int target)
  1476. {
  1477. struct ceph_mds_session *session;
  1478. int ret;
  1479. session = __ceph_lookup_mds_session(mdsc, target);
  1480. if (!session) {
  1481. session = register_session(mdsc, target);
  1482. if (IS_ERR(session))
  1483. return session;
  1484. }
  1485. if (session->s_state == CEPH_MDS_SESSION_NEW ||
  1486. session->s_state == CEPH_MDS_SESSION_CLOSING) {
  1487. ret = __open_session(mdsc, session);
  1488. if (ret)
  1489. return ERR_PTR(ret);
  1490. }
  1491. return session;
  1492. }
  1493. struct ceph_mds_session *
  1494. ceph_mdsc_open_export_target_session(struct ceph_mds_client *mdsc, int target)
  1495. {
  1496. struct ceph_mds_session *session;
  1497. struct ceph_client *cl = mdsc->fsc->client;
  1498. doutc(cl, "to mds%d\n", target);
  1499. mutex_lock(&mdsc->mutex);
  1500. session = __open_export_target_session(mdsc, target);
  1501. mutex_unlock(&mdsc->mutex);
  1502. return session;
  1503. }
  1504. static void __open_export_target_sessions(struct ceph_mds_client *mdsc,
  1505. struct ceph_mds_session *session)
  1506. {
  1507. struct ceph_mds_info *mi;
  1508. struct ceph_mds_session *ts;
  1509. int i, mds = session->s_mds;
  1510. struct ceph_client *cl = mdsc->fsc->client;
  1511. if (mds >= mdsc->mdsmap->possible_max_rank)
  1512. return;
  1513. mi = &mdsc->mdsmap->m_info[mds];
  1514. doutc(cl, "for mds%d (%d targets)\n", session->s_mds,
  1515. mi->num_export_targets);
  1516. for (i = 0; i < mi->num_export_targets; i++) {
  1517. ts = __open_export_target_session(mdsc, mi->export_targets[i]);
  1518. ceph_put_mds_session(ts);
  1519. }
  1520. }
  1521. /*
  1522. * session caps
  1523. */
  1524. static void detach_cap_releases(struct ceph_mds_session *session,
  1525. struct list_head *target)
  1526. {
  1527. struct ceph_client *cl = session->s_mdsc->fsc->client;
  1528. lockdep_assert_held(&session->s_cap_lock);
  1529. list_splice_init(&session->s_cap_releases, target);
  1530. session->s_num_cap_releases = 0;
  1531. doutc(cl, "mds%d\n", session->s_mds);
  1532. }
  1533. static void dispose_cap_releases(struct ceph_mds_client *mdsc,
  1534. struct list_head *dispose)
  1535. {
  1536. while (!list_empty(dispose)) {
  1537. struct ceph_cap *cap;
  1538. /* zero out the in-progress message */
  1539. cap = list_first_entry(dispose, struct ceph_cap, session_caps);
  1540. list_del(&cap->session_caps);
  1541. ceph_put_cap(mdsc, cap);
  1542. }
  1543. }
  1544. static void cleanup_session_requests(struct ceph_mds_client *mdsc,
  1545. struct ceph_mds_session *session)
  1546. {
  1547. struct ceph_client *cl = mdsc->fsc->client;
  1548. struct ceph_mds_request *req;
  1549. struct rb_node *p;
  1550. doutc(cl, "mds%d\n", session->s_mds);
  1551. mutex_lock(&mdsc->mutex);
  1552. while (!list_empty(&session->s_unsafe)) {
  1553. req = list_first_entry(&session->s_unsafe,
  1554. struct ceph_mds_request, r_unsafe_item);
  1555. pr_warn_ratelimited_client(cl, " dropping unsafe request %llu\n",
  1556. req->r_tid);
  1557. if (req->r_target_inode)
  1558. mapping_set_error(req->r_target_inode->i_mapping, -EIO);
  1559. if (req->r_unsafe_dir)
  1560. mapping_set_error(req->r_unsafe_dir->i_mapping, -EIO);
  1561. __unregister_request(mdsc, req);
  1562. }
  1563. /* zero r_attempts, so kick_requests() will re-send requests */
  1564. p = rb_first(&mdsc->request_tree);
  1565. while (p) {
  1566. req = rb_entry(p, struct ceph_mds_request, r_node);
  1567. p = rb_next(p);
  1568. if (req->r_session &&
  1569. req->r_session->s_mds == session->s_mds)
  1570. req->r_attempts = 0;
  1571. }
  1572. mutex_unlock(&mdsc->mutex);
  1573. }
  1574. /*
  1575. * Helper to safely iterate over all caps associated with a session, with
  1576. * special care taken to handle a racing __ceph_remove_cap().
  1577. *
  1578. * Caller must hold session s_mutex.
  1579. */
  1580. int ceph_iterate_session_caps(struct ceph_mds_session *session,
  1581. int (*cb)(struct inode *, int mds, void *),
  1582. void *arg)
  1583. {
  1584. struct ceph_client *cl = session->s_mdsc->fsc->client;
  1585. struct list_head *p;
  1586. struct ceph_cap *cap;
  1587. struct inode *inode, *last_inode = NULL;
  1588. struct ceph_cap *old_cap = NULL;
  1589. int ret;
  1590. doutc(cl, "%p mds%d\n", session, session->s_mds);
  1591. spin_lock(&session->s_cap_lock);
  1592. p = session->s_caps.next;
  1593. while (p != &session->s_caps) {
  1594. int mds;
  1595. cap = list_entry(p, struct ceph_cap, session_caps);
  1596. inode = igrab(&cap->ci->netfs.inode);
  1597. if (!inode) {
  1598. p = p->next;
  1599. continue;
  1600. }
  1601. session->s_cap_iterator = cap;
  1602. mds = cap->mds;
  1603. spin_unlock(&session->s_cap_lock);
  1604. if (last_inode) {
  1605. iput(last_inode);
  1606. last_inode = NULL;
  1607. }
  1608. if (old_cap) {
  1609. ceph_put_cap(session->s_mdsc, old_cap);
  1610. old_cap = NULL;
  1611. }
  1612. ret = cb(inode, mds, arg);
  1613. last_inode = inode;
  1614. spin_lock(&session->s_cap_lock);
  1615. p = p->next;
  1616. if (!cap->ci) {
  1617. doutc(cl, "finishing cap %p removal\n", cap);
  1618. BUG_ON(cap->session != session);
  1619. cap->session = NULL;
  1620. list_del_init(&cap->session_caps);
  1621. session->s_nr_caps--;
  1622. atomic64_dec(&session->s_mdsc->metric.total_caps);
  1623. if (cap->queue_release)
  1624. __ceph_queue_cap_release(session, cap);
  1625. else
  1626. old_cap = cap; /* put_cap it w/o locks held */
  1627. }
  1628. if (ret < 0)
  1629. goto out;
  1630. }
  1631. ret = 0;
  1632. out:
  1633. session->s_cap_iterator = NULL;
  1634. spin_unlock(&session->s_cap_lock);
  1635. iput(last_inode);
  1636. if (old_cap)
  1637. ceph_put_cap(session->s_mdsc, old_cap);
  1638. return ret;
  1639. }
  1640. static int remove_session_caps_cb(struct inode *inode, int mds, void *arg)
  1641. {
  1642. struct ceph_inode_info *ci = ceph_inode(inode);
  1643. struct ceph_client *cl = ceph_inode_to_client(inode);
  1644. bool invalidate = false;
  1645. struct ceph_cap *cap;
  1646. int iputs = 0;
  1647. spin_lock(&ci->i_ceph_lock);
  1648. cap = __get_cap_for_mds(ci, mds);
  1649. if (cap) {
  1650. doutc(cl, " removing cap %p, ci is %p, inode is %p\n",
  1651. cap, ci, &ci->netfs.inode);
  1652. iputs = ceph_purge_inode_cap(inode, cap, &invalidate);
  1653. }
  1654. spin_unlock(&ci->i_ceph_lock);
  1655. if (cap)
  1656. wake_up_all(&ci->i_cap_wq);
  1657. if (invalidate)
  1658. ceph_queue_invalidate(inode);
  1659. while (iputs--)
  1660. iput(inode);
  1661. return 0;
  1662. }
  1663. /*
  1664. * caller must hold session s_mutex
  1665. */
  1666. static void remove_session_caps(struct ceph_mds_session *session)
  1667. {
  1668. struct ceph_fs_client *fsc = session->s_mdsc->fsc;
  1669. struct super_block *sb = fsc->sb;
  1670. LIST_HEAD(dispose);
  1671. doutc(fsc->client, "on %p\n", session);
  1672. ceph_iterate_session_caps(session, remove_session_caps_cb, fsc);
  1673. wake_up_all(&fsc->mdsc->cap_flushing_wq);
  1674. spin_lock(&session->s_cap_lock);
  1675. if (session->s_nr_caps > 0) {
  1676. struct inode *inode;
  1677. struct ceph_cap *cap, *prev = NULL;
  1678. struct ceph_vino vino;
  1679. /*
  1680. * iterate_session_caps() skips inodes that are being
  1681. * deleted, we need to wait until deletions are complete.
  1682. * __wait_on_freeing_inode() is designed for the job,
  1683. * but it is not exported, so use lookup inode function
  1684. * to access it.
  1685. */
  1686. while (!list_empty(&session->s_caps)) {
  1687. cap = list_entry(session->s_caps.next,
  1688. struct ceph_cap, session_caps);
  1689. if (cap == prev)
  1690. break;
  1691. prev = cap;
  1692. vino = cap->ci->i_vino;
  1693. spin_unlock(&session->s_cap_lock);
  1694. inode = ceph_find_inode(sb, vino);
  1695. iput(inode);
  1696. spin_lock(&session->s_cap_lock);
  1697. }
  1698. }
  1699. // drop cap expires and unlock s_cap_lock
  1700. detach_cap_releases(session, &dispose);
  1701. BUG_ON(session->s_nr_caps > 0);
  1702. BUG_ON(!list_empty(&session->s_cap_flushing));
  1703. spin_unlock(&session->s_cap_lock);
  1704. dispose_cap_releases(session->s_mdsc, &dispose);
  1705. }
  1706. enum {
  1707. RECONNECT,
  1708. RENEWCAPS,
  1709. FORCE_RO,
  1710. };
  1711. /*
  1712. * wake up any threads waiting on this session's caps. if the cap is
  1713. * old (didn't get renewed on the client reconnect), remove it now.
  1714. *
  1715. * caller must hold s_mutex.
  1716. */
  1717. static int wake_up_session_cb(struct inode *inode, int mds, void *arg)
  1718. {
  1719. struct ceph_inode_info *ci = ceph_inode(inode);
  1720. unsigned long ev = (unsigned long)arg;
  1721. if (ev == RECONNECT) {
  1722. spin_lock(&ci->i_ceph_lock);
  1723. ci->i_wanted_max_size = 0;
  1724. ci->i_requested_max_size = 0;
  1725. spin_unlock(&ci->i_ceph_lock);
  1726. } else if (ev == RENEWCAPS) {
  1727. struct ceph_cap *cap;
  1728. spin_lock(&ci->i_ceph_lock);
  1729. cap = __get_cap_for_mds(ci, mds);
  1730. /* mds did not re-issue stale cap */
  1731. if (cap && cap->cap_gen < atomic_read(&cap->session->s_cap_gen))
  1732. cap->issued = cap->implemented = CEPH_CAP_PIN;
  1733. spin_unlock(&ci->i_ceph_lock);
  1734. } else if (ev == FORCE_RO) {
  1735. }
  1736. wake_up_all(&ci->i_cap_wq);
  1737. return 0;
  1738. }
  1739. static void wake_up_session_caps(struct ceph_mds_session *session, int ev)
  1740. {
  1741. struct ceph_client *cl = session->s_mdsc->fsc->client;
  1742. doutc(cl, "session %p mds%d\n", session, session->s_mds);
  1743. ceph_iterate_session_caps(session, wake_up_session_cb,
  1744. (void *)(unsigned long)ev);
  1745. }
  1746. /*
  1747. * Send periodic message to MDS renewing all currently held caps. The
  1748. * ack will reset the expiration for all caps from this session.
  1749. *
  1750. * caller holds s_mutex
  1751. */
  1752. static int send_renew_caps(struct ceph_mds_client *mdsc,
  1753. struct ceph_mds_session *session)
  1754. {
  1755. struct ceph_client *cl = mdsc->fsc->client;
  1756. struct ceph_msg *msg;
  1757. int state;
  1758. if (time_after_eq(jiffies, session->s_cap_ttl) &&
  1759. time_after_eq(session->s_cap_ttl, session->s_renew_requested))
  1760. pr_info_client(cl, "mds%d caps stale\n", session->s_mds);
  1761. session->s_renew_requested = jiffies;
  1762. /* do not try to renew caps until a recovering mds has reconnected
  1763. * with its clients. */
  1764. state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds);
  1765. if (state < CEPH_MDS_STATE_RECONNECT) {
  1766. doutc(cl, "ignoring mds%d (%s)\n", session->s_mds,
  1767. ceph_mds_state_name(state));
  1768. return 0;
  1769. }
  1770. doutc(cl, "to mds%d (%s)\n", session->s_mds,
  1771. ceph_mds_state_name(state));
  1772. msg = create_session_full_msg(mdsc, CEPH_SESSION_REQUEST_RENEWCAPS,
  1773. ++session->s_renew_seq);
  1774. if (IS_ERR(msg))
  1775. return PTR_ERR(msg);
  1776. ceph_con_send(&session->s_con, msg);
  1777. return 0;
  1778. }
  1779. static int send_flushmsg_ack(struct ceph_mds_client *mdsc,
  1780. struct ceph_mds_session *session, u64 seq)
  1781. {
  1782. struct ceph_client *cl = mdsc->fsc->client;
  1783. struct ceph_msg *msg;
  1784. doutc(cl, "to mds%d (%s)s seq %lld\n", session->s_mds,
  1785. ceph_session_state_name(session->s_state), seq);
  1786. msg = ceph_create_session_msg(CEPH_SESSION_FLUSHMSG_ACK, seq);
  1787. if (!msg)
  1788. return -ENOMEM;
  1789. ceph_con_send(&session->s_con, msg);
  1790. return 0;
  1791. }
  1792. /*
  1793. * Note new cap ttl, and any transition from stale -> not stale (fresh?).
  1794. *
  1795. * Called under session->s_mutex
  1796. */
  1797. static void renewed_caps(struct ceph_mds_client *mdsc,
  1798. struct ceph_mds_session *session, int is_renew)
  1799. {
  1800. struct ceph_client *cl = mdsc->fsc->client;
  1801. int was_stale;
  1802. int wake = 0;
  1803. spin_lock(&session->s_cap_lock);
  1804. was_stale = is_renew && time_after_eq(jiffies, session->s_cap_ttl);
  1805. session->s_cap_ttl = session->s_renew_requested +
  1806. mdsc->mdsmap->m_session_timeout*HZ;
  1807. if (was_stale) {
  1808. if (time_before(jiffies, session->s_cap_ttl)) {
  1809. pr_info_client(cl, "mds%d caps renewed\n",
  1810. session->s_mds);
  1811. wake = 1;
  1812. } else {
  1813. pr_info_client(cl, "mds%d caps still stale\n",
  1814. session->s_mds);
  1815. }
  1816. }
  1817. doutc(cl, "mds%d ttl now %lu, was %s, now %s\n", session->s_mds,
  1818. session->s_cap_ttl, was_stale ? "stale" : "fresh",
  1819. time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh");
  1820. spin_unlock(&session->s_cap_lock);
  1821. if (wake)
  1822. wake_up_session_caps(session, RENEWCAPS);
  1823. }
  1824. /*
  1825. * send a session close request
  1826. */
  1827. static int request_close_session(struct ceph_mds_session *session)
  1828. {
  1829. struct ceph_client *cl = session->s_mdsc->fsc->client;
  1830. struct ceph_msg *msg;
  1831. doutc(cl, "mds%d state %s seq %lld\n", session->s_mds,
  1832. ceph_session_state_name(session->s_state), session->s_seq);
  1833. msg = ceph_create_session_msg(CEPH_SESSION_REQUEST_CLOSE,
  1834. session->s_seq);
  1835. if (!msg)
  1836. return -ENOMEM;
  1837. ceph_con_send(&session->s_con, msg);
  1838. return 1;
  1839. }
  1840. /*
  1841. * Called with s_mutex held.
  1842. */
  1843. static int __close_session(struct ceph_mds_client *mdsc,
  1844. struct ceph_mds_session *session)
  1845. {
  1846. if (session->s_state >= CEPH_MDS_SESSION_CLOSING)
  1847. return 0;
  1848. session->s_state = CEPH_MDS_SESSION_CLOSING;
  1849. return request_close_session(session);
  1850. }
  1851. static bool drop_negative_children(struct dentry *dentry)
  1852. {
  1853. struct dentry *child;
  1854. bool all_negative = true;
  1855. if (!d_is_dir(dentry))
  1856. goto out;
  1857. spin_lock(&dentry->d_lock);
  1858. hlist_for_each_entry(child, &dentry->d_children, d_sib) {
  1859. if (d_really_is_positive(child)) {
  1860. all_negative = false;
  1861. break;
  1862. }
  1863. }
  1864. spin_unlock(&dentry->d_lock);
  1865. if (all_negative)
  1866. shrink_dcache_parent(dentry);
  1867. out:
  1868. return all_negative;
  1869. }
  1870. /*
  1871. * Trim old(er) caps.
  1872. *
  1873. * Because we can't cache an inode without one or more caps, we do
  1874. * this indirectly: if a cap is unused, we prune its aliases, at which
  1875. * point the inode will hopefully get dropped to.
  1876. *
  1877. * Yes, this is a bit sloppy. Our only real goal here is to respond to
  1878. * memory pressure from the MDS, though, so it needn't be perfect.
  1879. */
  1880. static int trim_caps_cb(struct inode *inode, int mds, void *arg)
  1881. {
  1882. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
  1883. struct ceph_client *cl = mdsc->fsc->client;
  1884. int *remaining = arg;
  1885. struct ceph_inode_info *ci = ceph_inode(inode);
  1886. int used, wanted, oissued, mine;
  1887. struct ceph_cap *cap;
  1888. if (*remaining <= 0)
  1889. return -1;
  1890. spin_lock(&ci->i_ceph_lock);
  1891. cap = __get_cap_for_mds(ci, mds);
  1892. if (!cap) {
  1893. spin_unlock(&ci->i_ceph_lock);
  1894. return 0;
  1895. }
  1896. mine = cap->issued | cap->implemented;
  1897. used = __ceph_caps_used(ci);
  1898. wanted = __ceph_caps_file_wanted(ci);
  1899. oissued = __ceph_caps_issued_other(ci, cap);
  1900. doutc(cl, "%p %llx.%llx cap %p mine %s oissued %s used %s wanted %s\n",
  1901. inode, ceph_vinop(inode), cap, ceph_cap_string(mine),
  1902. ceph_cap_string(oissued), ceph_cap_string(used),
  1903. ceph_cap_string(wanted));
  1904. if (cap == ci->i_auth_cap) {
  1905. if (ci->i_dirty_caps || ci->i_flushing_caps ||
  1906. !list_empty(&ci->i_cap_snaps))
  1907. goto out;
  1908. if ((used | wanted) & CEPH_CAP_ANY_WR)
  1909. goto out;
  1910. /* Note: it's possible that i_filelock_ref becomes non-zero
  1911. * after dropping auth caps. It doesn't hurt because reply
  1912. * of lock mds request will re-add auth caps. */
  1913. if (atomic_read(&ci->i_filelock_ref) > 0)
  1914. goto out;
  1915. }
  1916. /* The inode has cached pages, but it's no longer used.
  1917. * we can safely drop it */
  1918. if (S_ISREG(inode->i_mode) &&
  1919. wanted == 0 && used == CEPH_CAP_FILE_CACHE &&
  1920. !(oissued & CEPH_CAP_FILE_CACHE)) {
  1921. used = 0;
  1922. oissued = 0;
  1923. }
  1924. if ((used | wanted) & ~oissued & mine)
  1925. goto out; /* we need these caps */
  1926. if (oissued) {
  1927. /* we aren't the only cap.. just remove us */
  1928. ceph_remove_cap(mdsc, cap, true);
  1929. (*remaining)--;
  1930. } else {
  1931. struct dentry *dentry;
  1932. /* try dropping referring dentries */
  1933. spin_unlock(&ci->i_ceph_lock);
  1934. dentry = d_find_any_alias(inode);
  1935. if (dentry && drop_negative_children(dentry)) {
  1936. int count;
  1937. dput(dentry);
  1938. d_prune_aliases(inode);
  1939. count = icount_read(inode);
  1940. if (count == 1)
  1941. (*remaining)--;
  1942. doutc(cl, "%p %llx.%llx cap %p pruned, count now %d\n",
  1943. inode, ceph_vinop(inode), cap, count);
  1944. } else {
  1945. dput(dentry);
  1946. }
  1947. return 0;
  1948. }
  1949. out:
  1950. spin_unlock(&ci->i_ceph_lock);
  1951. return 0;
  1952. }
  1953. /*
  1954. * Trim session cap count down to some max number.
  1955. */
  1956. int ceph_trim_caps(struct ceph_mds_client *mdsc,
  1957. struct ceph_mds_session *session,
  1958. int max_caps)
  1959. {
  1960. struct ceph_client *cl = mdsc->fsc->client;
  1961. int trim_caps = session->s_nr_caps - max_caps;
  1962. doutc(cl, "mds%d start: %d / %d, trim %d\n", session->s_mds,
  1963. session->s_nr_caps, max_caps, trim_caps);
  1964. if (trim_caps > 0) {
  1965. int remaining = trim_caps;
  1966. ceph_iterate_session_caps(session, trim_caps_cb, &remaining);
  1967. doutc(cl, "mds%d done: %d / %d, trimmed %d\n",
  1968. session->s_mds, session->s_nr_caps, max_caps,
  1969. trim_caps - remaining);
  1970. }
  1971. ceph_flush_session_cap_releases(mdsc, session);
  1972. return 0;
  1973. }
  1974. static int check_caps_flush(struct ceph_mds_client *mdsc,
  1975. u64 want_flush_tid)
  1976. {
  1977. struct ceph_client *cl = mdsc->fsc->client;
  1978. int ret = 1;
  1979. spin_lock(&mdsc->cap_dirty_lock);
  1980. if (!list_empty(&mdsc->cap_flush_list)) {
  1981. struct ceph_cap_flush *cf =
  1982. list_first_entry(&mdsc->cap_flush_list,
  1983. struct ceph_cap_flush, g_list);
  1984. if (cf->tid <= want_flush_tid) {
  1985. doutc(cl, "still flushing tid %llu <= %llu\n",
  1986. cf->tid, want_flush_tid);
  1987. ret = 0;
  1988. }
  1989. }
  1990. spin_unlock(&mdsc->cap_dirty_lock);
  1991. return ret;
  1992. }
  1993. /*
  1994. * flush all dirty inode data to disk.
  1995. *
  1996. * returns true if we've flushed through want_flush_tid
  1997. */
  1998. static void wait_caps_flush(struct ceph_mds_client *mdsc,
  1999. u64 want_flush_tid)
  2000. {
  2001. struct ceph_client *cl = mdsc->fsc->client;
  2002. doutc(cl, "want %llu\n", want_flush_tid);
  2003. wait_event(mdsc->cap_flushing_wq,
  2004. check_caps_flush(mdsc, want_flush_tid));
  2005. doutc(cl, "ok, flushed thru %llu\n", want_flush_tid);
  2006. }
  2007. /*
  2008. * called under s_mutex
  2009. */
  2010. static void ceph_send_cap_releases(struct ceph_mds_client *mdsc,
  2011. struct ceph_mds_session *session)
  2012. {
  2013. struct ceph_client *cl = mdsc->fsc->client;
  2014. struct ceph_msg *msg = NULL;
  2015. struct ceph_mds_cap_release *head;
  2016. struct ceph_mds_cap_item *item;
  2017. struct ceph_osd_client *osdc = &mdsc->fsc->client->osdc;
  2018. struct ceph_cap *cap;
  2019. LIST_HEAD(tmp_list);
  2020. int num_cap_releases;
  2021. __le32 barrier, *cap_barrier;
  2022. down_read(&osdc->lock);
  2023. barrier = cpu_to_le32(osdc->epoch_barrier);
  2024. up_read(&osdc->lock);
  2025. spin_lock(&session->s_cap_lock);
  2026. again:
  2027. list_splice_init(&session->s_cap_releases, &tmp_list);
  2028. num_cap_releases = session->s_num_cap_releases;
  2029. session->s_num_cap_releases = 0;
  2030. spin_unlock(&session->s_cap_lock);
  2031. while (!list_empty(&tmp_list)) {
  2032. if (!msg) {
  2033. msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE,
  2034. PAGE_SIZE, GFP_NOFS, false);
  2035. if (!msg)
  2036. goto out_err;
  2037. head = msg->front.iov_base;
  2038. head->num = cpu_to_le32(0);
  2039. msg->front.iov_len = sizeof(*head);
  2040. msg->hdr.version = cpu_to_le16(2);
  2041. msg->hdr.compat_version = cpu_to_le16(1);
  2042. }
  2043. cap = list_first_entry(&tmp_list, struct ceph_cap,
  2044. session_caps);
  2045. list_del(&cap->session_caps);
  2046. num_cap_releases--;
  2047. head = msg->front.iov_base;
  2048. put_unaligned_le32(get_unaligned_le32(&head->num) + 1,
  2049. &head->num);
  2050. item = msg->front.iov_base + msg->front.iov_len;
  2051. item->ino = cpu_to_le64(cap->cap_ino);
  2052. item->cap_id = cpu_to_le64(cap->cap_id);
  2053. item->migrate_seq = cpu_to_le32(cap->mseq);
  2054. item->issue_seq = cpu_to_le32(cap->issue_seq);
  2055. msg->front.iov_len += sizeof(*item);
  2056. ceph_put_cap(mdsc, cap);
  2057. if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) {
  2058. // Append cap_barrier field
  2059. cap_barrier = msg->front.iov_base + msg->front.iov_len;
  2060. *cap_barrier = barrier;
  2061. msg->front.iov_len += sizeof(*cap_barrier);
  2062. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  2063. doutc(cl, "mds%d %p\n", session->s_mds, msg);
  2064. ceph_con_send(&session->s_con, msg);
  2065. msg = NULL;
  2066. }
  2067. }
  2068. BUG_ON(num_cap_releases != 0);
  2069. spin_lock(&session->s_cap_lock);
  2070. if (!list_empty(&session->s_cap_releases))
  2071. goto again;
  2072. spin_unlock(&session->s_cap_lock);
  2073. if (msg) {
  2074. // Append cap_barrier field
  2075. cap_barrier = msg->front.iov_base + msg->front.iov_len;
  2076. *cap_barrier = barrier;
  2077. msg->front.iov_len += sizeof(*cap_barrier);
  2078. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  2079. doutc(cl, "mds%d %p\n", session->s_mds, msg);
  2080. ceph_con_send(&session->s_con, msg);
  2081. }
  2082. return;
  2083. out_err:
  2084. pr_err_client(cl, "mds%d, failed to allocate message\n",
  2085. session->s_mds);
  2086. spin_lock(&session->s_cap_lock);
  2087. list_splice(&tmp_list, &session->s_cap_releases);
  2088. session->s_num_cap_releases += num_cap_releases;
  2089. spin_unlock(&session->s_cap_lock);
  2090. }
  2091. static void ceph_cap_release_work(struct work_struct *work)
  2092. {
  2093. struct ceph_mds_session *session =
  2094. container_of(work, struct ceph_mds_session, s_cap_release_work);
  2095. mutex_lock(&session->s_mutex);
  2096. if (session->s_state == CEPH_MDS_SESSION_OPEN ||
  2097. session->s_state == CEPH_MDS_SESSION_HUNG)
  2098. ceph_send_cap_releases(session->s_mdsc, session);
  2099. mutex_unlock(&session->s_mutex);
  2100. ceph_put_mds_session(session);
  2101. }
  2102. void ceph_flush_session_cap_releases(struct ceph_mds_client *mdsc,
  2103. struct ceph_mds_session *session)
  2104. {
  2105. struct ceph_client *cl = mdsc->fsc->client;
  2106. if (mdsc->stopping)
  2107. return;
  2108. ceph_get_mds_session(session);
  2109. if (queue_work(mdsc->fsc->cap_wq,
  2110. &session->s_cap_release_work)) {
  2111. doutc(cl, "cap release work queued\n");
  2112. } else {
  2113. ceph_put_mds_session(session);
  2114. doutc(cl, "failed to queue cap release work\n");
  2115. }
  2116. }
  2117. /*
  2118. * caller holds session->s_cap_lock
  2119. */
  2120. void __ceph_queue_cap_release(struct ceph_mds_session *session,
  2121. struct ceph_cap *cap)
  2122. {
  2123. list_add_tail(&cap->session_caps, &session->s_cap_releases);
  2124. session->s_num_cap_releases++;
  2125. if (!(session->s_num_cap_releases % CEPH_CAPS_PER_RELEASE))
  2126. ceph_flush_session_cap_releases(session->s_mdsc, session);
  2127. }
  2128. static void ceph_cap_reclaim_work(struct work_struct *work)
  2129. {
  2130. struct ceph_mds_client *mdsc =
  2131. container_of(work, struct ceph_mds_client, cap_reclaim_work);
  2132. int ret = ceph_trim_dentries(mdsc);
  2133. if (ret == -EAGAIN)
  2134. ceph_queue_cap_reclaim_work(mdsc);
  2135. }
  2136. void ceph_queue_cap_reclaim_work(struct ceph_mds_client *mdsc)
  2137. {
  2138. struct ceph_client *cl = mdsc->fsc->client;
  2139. if (mdsc->stopping)
  2140. return;
  2141. if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_reclaim_work)) {
  2142. doutc(cl, "caps reclaim work queued\n");
  2143. } else {
  2144. doutc(cl, "failed to queue caps release work\n");
  2145. }
  2146. }
  2147. void ceph_reclaim_caps_nr(struct ceph_mds_client *mdsc, int nr)
  2148. {
  2149. int val;
  2150. if (!nr)
  2151. return;
  2152. val = atomic_add_return(nr, &mdsc->cap_reclaim_pending);
  2153. if ((val % CEPH_CAPS_PER_RELEASE) < nr) {
  2154. atomic_set(&mdsc->cap_reclaim_pending, 0);
  2155. ceph_queue_cap_reclaim_work(mdsc);
  2156. }
  2157. }
  2158. void ceph_queue_cap_unlink_work(struct ceph_mds_client *mdsc)
  2159. {
  2160. struct ceph_client *cl = mdsc->fsc->client;
  2161. if (mdsc->stopping)
  2162. return;
  2163. if (queue_work(mdsc->fsc->cap_wq, &mdsc->cap_unlink_work)) {
  2164. doutc(cl, "caps unlink work queued\n");
  2165. } else {
  2166. doutc(cl, "failed to queue caps unlink work\n");
  2167. }
  2168. }
  2169. static void ceph_cap_unlink_work(struct work_struct *work)
  2170. {
  2171. struct ceph_mds_client *mdsc =
  2172. container_of(work, struct ceph_mds_client, cap_unlink_work);
  2173. struct ceph_client *cl = mdsc->fsc->client;
  2174. doutc(cl, "begin\n");
  2175. spin_lock(&mdsc->cap_delay_lock);
  2176. while (!list_empty(&mdsc->cap_unlink_delay_list)) {
  2177. struct ceph_inode_info *ci;
  2178. struct inode *inode;
  2179. ci = list_first_entry(&mdsc->cap_unlink_delay_list,
  2180. struct ceph_inode_info,
  2181. i_cap_delay_list);
  2182. list_del_init(&ci->i_cap_delay_list);
  2183. inode = igrab(&ci->netfs.inode);
  2184. if (inode) {
  2185. spin_unlock(&mdsc->cap_delay_lock);
  2186. doutc(cl, "on %p %llx.%llx\n", inode,
  2187. ceph_vinop(inode));
  2188. ceph_check_caps(ci, CHECK_CAPS_FLUSH);
  2189. iput(inode);
  2190. spin_lock(&mdsc->cap_delay_lock);
  2191. }
  2192. }
  2193. spin_unlock(&mdsc->cap_delay_lock);
  2194. doutc(cl, "done\n");
  2195. }
  2196. /*
  2197. * requests
  2198. */
  2199. int ceph_alloc_readdir_reply_buffer(struct ceph_mds_request *req,
  2200. struct inode *dir)
  2201. {
  2202. struct ceph_inode_info *ci = ceph_inode(dir);
  2203. struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info;
  2204. struct ceph_mount_options *opt = req->r_mdsc->fsc->mount_options;
  2205. size_t size = sizeof(struct ceph_mds_reply_dir_entry);
  2206. unsigned int num_entries;
  2207. u64 bytes_count;
  2208. int order;
  2209. spin_lock(&ci->i_ceph_lock);
  2210. num_entries = ci->i_files + ci->i_subdirs;
  2211. spin_unlock(&ci->i_ceph_lock);
  2212. num_entries = max(num_entries, 1U);
  2213. num_entries = min(num_entries, opt->max_readdir);
  2214. bytes_count = (u64)size * num_entries;
  2215. if (unlikely(bytes_count > ULONG_MAX))
  2216. bytes_count = ULONG_MAX;
  2217. order = get_order((unsigned long)bytes_count);
  2218. while (order >= 0) {
  2219. rinfo->dir_entries = (void*)__get_free_pages(GFP_KERNEL |
  2220. __GFP_NOWARN |
  2221. __GFP_ZERO,
  2222. order);
  2223. if (rinfo->dir_entries)
  2224. break;
  2225. order--;
  2226. }
  2227. if (!rinfo->dir_entries || unlikely(order < 0))
  2228. return -ENOMEM;
  2229. num_entries = (PAGE_SIZE << order) / size;
  2230. num_entries = min(num_entries, opt->max_readdir);
  2231. rinfo->dir_buf_size = PAGE_SIZE << order;
  2232. req->r_num_caps = num_entries + 1;
  2233. req->r_args.readdir.max_entries = cpu_to_le32(num_entries);
  2234. req->r_args.readdir.max_bytes = cpu_to_le32(opt->max_readdir_bytes);
  2235. return 0;
  2236. }
  2237. /*
  2238. * Create an mds request.
  2239. */
  2240. struct ceph_mds_request *
  2241. ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode)
  2242. {
  2243. struct ceph_mds_request *req;
  2244. req = kmem_cache_zalloc(ceph_mds_request_cachep, GFP_NOFS);
  2245. if (!req)
  2246. return ERR_PTR(-ENOMEM);
  2247. mutex_init(&req->r_fill_mutex);
  2248. req->r_mdsc = mdsc;
  2249. req->r_started = jiffies;
  2250. req->r_start_latency = ktime_get();
  2251. req->r_resend_mds = -1;
  2252. INIT_LIST_HEAD(&req->r_unsafe_dir_item);
  2253. INIT_LIST_HEAD(&req->r_unsafe_target_item);
  2254. req->r_fmode = -1;
  2255. req->r_feature_needed = -1;
  2256. kref_init(&req->r_kref);
  2257. RB_CLEAR_NODE(&req->r_node);
  2258. INIT_LIST_HEAD(&req->r_wait);
  2259. init_completion(&req->r_completion);
  2260. init_completion(&req->r_safe_completion);
  2261. INIT_LIST_HEAD(&req->r_unsafe_item);
  2262. ktime_get_coarse_real_ts64(&req->r_stamp);
  2263. req->r_op = op;
  2264. req->r_direct_mode = mode;
  2265. return req;
  2266. }
  2267. /*
  2268. * return oldest (lowest) request, tid in request tree, 0 if none.
  2269. *
  2270. * called under mdsc->mutex.
  2271. */
  2272. static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc)
  2273. {
  2274. if (RB_EMPTY_ROOT(&mdsc->request_tree))
  2275. return NULL;
  2276. return rb_entry(rb_first(&mdsc->request_tree),
  2277. struct ceph_mds_request, r_node);
  2278. }
  2279. static inline u64 __get_oldest_tid(struct ceph_mds_client *mdsc)
  2280. {
  2281. return mdsc->oldest_tid;
  2282. }
  2283. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  2284. static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen)
  2285. {
  2286. struct inode *dir = req->r_parent;
  2287. struct dentry *dentry = req->r_dentry;
  2288. const struct qstr *name = req->r_dname;
  2289. u8 *cryptbuf = NULL;
  2290. u32 len = 0;
  2291. int ret = 0;
  2292. /* only encode if we have parent and dentry */
  2293. if (!dir || !dentry)
  2294. goto success;
  2295. /* No-op unless this is encrypted */
  2296. if (!IS_ENCRYPTED(dir))
  2297. goto success;
  2298. ret = ceph_fscrypt_prepare_readdir(dir);
  2299. if (ret < 0)
  2300. return ERR_PTR(ret);
  2301. /* No key? Just ignore it. */
  2302. if (!fscrypt_has_encryption_key(dir))
  2303. goto success;
  2304. if (!name)
  2305. name = &dentry->d_name;
  2306. if (!fscrypt_fname_encrypted_size(dir, name->len, NAME_MAX, &len)) {
  2307. WARN_ON_ONCE(1);
  2308. return ERR_PTR(-ENAMETOOLONG);
  2309. }
  2310. /* No need to append altname if name is short enough */
  2311. if (len <= CEPH_NOHASH_NAME_MAX) {
  2312. len = 0;
  2313. goto success;
  2314. }
  2315. cryptbuf = kmalloc(len, GFP_KERNEL);
  2316. if (!cryptbuf)
  2317. return ERR_PTR(-ENOMEM);
  2318. ret = fscrypt_fname_encrypt(dir, name, cryptbuf, len);
  2319. if (ret) {
  2320. kfree(cryptbuf);
  2321. return ERR_PTR(ret);
  2322. }
  2323. success:
  2324. *plen = len;
  2325. return cryptbuf;
  2326. }
  2327. #else
  2328. static u8 *get_fscrypt_altname(const struct ceph_mds_request *req, u32 *plen)
  2329. {
  2330. *plen = 0;
  2331. return NULL;
  2332. }
  2333. #endif
  2334. /**
  2335. * ceph_mdsc_build_path - build a path string to a given dentry
  2336. * @mdsc: mds client
  2337. * @dentry: dentry to which path should be built
  2338. * @path_info: output path, length, base ino+snap, and freepath ownership flag
  2339. * @for_wire: is this path going to be sent to the MDS?
  2340. *
  2341. * Build a string that represents the path to the dentry. This is mostly called
  2342. * for two different purposes:
  2343. *
  2344. * 1) we need to build a path string to send to the MDS (for_wire == true)
  2345. * 2) we need a path string for local presentation (e.g. debugfs)
  2346. * (for_wire == false)
  2347. *
  2348. * The path is built in reverse, starting with the dentry. Walk back up toward
  2349. * the root, building the path until the first non-snapped inode is reached
  2350. * (for_wire) or the root inode is reached (!for_wire).
  2351. *
  2352. * Encode hidden .snap dirs as a double /, i.e.
  2353. * foo/.snap/bar -> foo//bar
  2354. */
  2355. char *ceph_mdsc_build_path(struct ceph_mds_client *mdsc, struct dentry *dentry,
  2356. struct ceph_path_info *path_info, int for_wire)
  2357. {
  2358. struct ceph_client *cl = mdsc->fsc->client;
  2359. struct dentry *cur;
  2360. struct inode *inode;
  2361. char *path;
  2362. int pos;
  2363. unsigned seq;
  2364. u64 base;
  2365. if (!dentry)
  2366. return ERR_PTR(-EINVAL);
  2367. path = __getname();
  2368. if (!path)
  2369. return ERR_PTR(-ENOMEM);
  2370. retry:
  2371. pos = PATH_MAX - 1;
  2372. path[pos] = '\0';
  2373. seq = read_seqbegin(&rename_lock);
  2374. cur = dget(dentry);
  2375. for (;;) {
  2376. struct dentry *parent;
  2377. spin_lock(&cur->d_lock);
  2378. inode = d_inode(cur);
  2379. if (inode && ceph_snap(inode) == CEPH_SNAPDIR) {
  2380. doutc(cl, "path+%d: %p SNAPDIR\n", pos, cur);
  2381. spin_unlock(&cur->d_lock);
  2382. parent = dget_parent(cur);
  2383. } else if (for_wire && inode && dentry != cur &&
  2384. ceph_snap(inode) == CEPH_NOSNAP) {
  2385. spin_unlock(&cur->d_lock);
  2386. pos++; /* get rid of any prepended '/' */
  2387. break;
  2388. } else if (!for_wire || !IS_ENCRYPTED(d_inode(cur->d_parent))) {
  2389. pos -= cur->d_name.len;
  2390. if (pos < 0) {
  2391. spin_unlock(&cur->d_lock);
  2392. break;
  2393. }
  2394. memcpy(path + pos, cur->d_name.name, cur->d_name.len);
  2395. spin_unlock(&cur->d_lock);
  2396. parent = dget_parent(cur);
  2397. } else {
  2398. int len, ret;
  2399. char buf[NAME_MAX];
  2400. /*
  2401. * Proactively copy name into buf, in case we need to
  2402. * present it as-is.
  2403. */
  2404. memcpy(buf, cur->d_name.name, cur->d_name.len);
  2405. len = cur->d_name.len;
  2406. spin_unlock(&cur->d_lock);
  2407. parent = dget_parent(cur);
  2408. ret = ceph_fscrypt_prepare_readdir(d_inode(parent));
  2409. if (ret < 0) {
  2410. dput(parent);
  2411. dput(cur);
  2412. __putname(path);
  2413. return ERR_PTR(ret);
  2414. }
  2415. if (fscrypt_has_encryption_key(d_inode(parent))) {
  2416. len = ceph_encode_encrypted_dname(d_inode(parent),
  2417. buf, len);
  2418. if (len < 0) {
  2419. dput(parent);
  2420. dput(cur);
  2421. __putname(path);
  2422. return ERR_PTR(len);
  2423. }
  2424. }
  2425. pos -= len;
  2426. if (pos < 0) {
  2427. dput(parent);
  2428. break;
  2429. }
  2430. memcpy(path + pos, buf, len);
  2431. }
  2432. dput(cur);
  2433. cur = parent;
  2434. /* Are we at the root? */
  2435. if (IS_ROOT(cur))
  2436. break;
  2437. /* Are we out of buffer? */
  2438. if (--pos < 0)
  2439. break;
  2440. path[pos] = '/';
  2441. }
  2442. inode = d_inode(cur);
  2443. base = inode ? ceph_ino(inode) : 0;
  2444. dput(cur);
  2445. if (read_seqretry(&rename_lock, seq))
  2446. goto retry;
  2447. if (pos < 0) {
  2448. /*
  2449. * The path is longer than PATH_MAX and this function
  2450. * cannot ever succeed. Creating paths that long is
  2451. * possible with Ceph, but Linux cannot use them.
  2452. */
  2453. __putname(path);
  2454. return ERR_PTR(-ENAMETOOLONG);
  2455. }
  2456. /* Initialize the output structure */
  2457. memset(path_info, 0, sizeof(*path_info));
  2458. path_info->vino.ino = base;
  2459. path_info->pathlen = PATH_MAX - 1 - pos;
  2460. path_info->path = path + pos;
  2461. path_info->freepath = true;
  2462. /* Set snap from dentry if available */
  2463. if (d_inode(dentry))
  2464. path_info->vino.snap = ceph_snap(d_inode(dentry));
  2465. else
  2466. path_info->vino.snap = CEPH_NOSNAP;
  2467. doutc(cl, "on %p %d built %llx '%.*s'\n", dentry, d_count(dentry),
  2468. base, PATH_MAX - 1 - pos, path + pos);
  2469. return path + pos;
  2470. }
  2471. static int build_dentry_path(struct ceph_mds_client *mdsc, struct dentry *dentry,
  2472. struct inode *dir, struct ceph_path_info *path_info,
  2473. bool parent_locked)
  2474. {
  2475. char *path;
  2476. rcu_read_lock();
  2477. if (!dir)
  2478. dir = d_inode_rcu(dentry->d_parent);
  2479. if (dir && parent_locked && ceph_snap(dir) == CEPH_NOSNAP &&
  2480. !IS_ENCRYPTED(dir)) {
  2481. path_info->vino.ino = ceph_ino(dir);
  2482. path_info->vino.snap = ceph_snap(dir);
  2483. rcu_read_unlock();
  2484. path_info->path = dentry->d_name.name;
  2485. path_info->pathlen = dentry->d_name.len;
  2486. path_info->freepath = false;
  2487. return 0;
  2488. }
  2489. rcu_read_unlock();
  2490. path = ceph_mdsc_build_path(mdsc, dentry, path_info, 1);
  2491. if (IS_ERR(path))
  2492. return PTR_ERR(path);
  2493. /*
  2494. * ceph_mdsc_build_path already fills path_info, including snap handling.
  2495. */
  2496. return 0;
  2497. }
  2498. static int build_inode_path(struct inode *inode, struct ceph_path_info *path_info)
  2499. {
  2500. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
  2501. struct dentry *dentry;
  2502. char *path;
  2503. if (ceph_snap(inode) == CEPH_NOSNAP) {
  2504. path_info->vino.ino = ceph_ino(inode);
  2505. path_info->vino.snap = ceph_snap(inode);
  2506. path_info->pathlen = 0;
  2507. path_info->freepath = false;
  2508. return 0;
  2509. }
  2510. dentry = d_find_alias(inode);
  2511. path = ceph_mdsc_build_path(mdsc, dentry, path_info, 1);
  2512. dput(dentry);
  2513. if (IS_ERR(path))
  2514. return PTR_ERR(path);
  2515. /*
  2516. * ceph_mdsc_build_path already fills path_info, including snap from dentry.
  2517. * Override with inode's snap since that's what this function is for.
  2518. */
  2519. path_info->vino.snap = ceph_snap(inode);
  2520. return 0;
  2521. }
  2522. /*
  2523. * request arguments may be specified via an inode *, a dentry *, or
  2524. * an explicit ino+path.
  2525. */
  2526. static int set_request_path_attr(struct ceph_mds_client *mdsc, struct inode *rinode,
  2527. struct dentry *rdentry, struct inode *rdiri,
  2528. const char *rpath, u64 rino,
  2529. struct ceph_path_info *path_info,
  2530. bool parent_locked)
  2531. {
  2532. struct ceph_client *cl = mdsc->fsc->client;
  2533. int r = 0;
  2534. /* Initialize the output structure */
  2535. memset(path_info, 0, sizeof(*path_info));
  2536. if (rinode) {
  2537. r = build_inode_path(rinode, path_info);
  2538. doutc(cl, " inode %p %llx.%llx\n", rinode, ceph_ino(rinode),
  2539. ceph_snap(rinode));
  2540. } else if (rdentry) {
  2541. r = build_dentry_path(mdsc, rdentry, rdiri, path_info, parent_locked);
  2542. doutc(cl, " dentry %p %llx/%.*s\n", rdentry, path_info->vino.ino,
  2543. path_info->pathlen, path_info->path);
  2544. } else if (rpath || rino) {
  2545. path_info->vino.ino = rino;
  2546. path_info->vino.snap = CEPH_NOSNAP;
  2547. path_info->path = rpath;
  2548. path_info->pathlen = rpath ? strlen(rpath) : 0;
  2549. path_info->freepath = false;
  2550. doutc(cl, " path %.*s\n", path_info->pathlen, rpath);
  2551. }
  2552. return r;
  2553. }
  2554. static void encode_mclientrequest_tail(void **p,
  2555. const struct ceph_mds_request *req)
  2556. {
  2557. struct ceph_timespec ts;
  2558. int i;
  2559. ceph_encode_timespec64(&ts, &req->r_stamp);
  2560. ceph_encode_copy(p, &ts, sizeof(ts));
  2561. /* v4: gid_list */
  2562. ceph_encode_32(p, req->r_cred->group_info->ngroups);
  2563. for (i = 0; i < req->r_cred->group_info->ngroups; i++)
  2564. ceph_encode_64(p, from_kgid(&init_user_ns,
  2565. req->r_cred->group_info->gid[i]));
  2566. /* v5: altname */
  2567. ceph_encode_32(p, req->r_altname_len);
  2568. ceph_encode_copy(p, req->r_altname, req->r_altname_len);
  2569. /* v6: fscrypt_auth and fscrypt_file */
  2570. if (req->r_fscrypt_auth) {
  2571. u32 authlen = ceph_fscrypt_auth_len(req->r_fscrypt_auth);
  2572. ceph_encode_32(p, authlen);
  2573. ceph_encode_copy(p, req->r_fscrypt_auth, authlen);
  2574. } else {
  2575. ceph_encode_32(p, 0);
  2576. }
  2577. if (test_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags)) {
  2578. ceph_encode_32(p, sizeof(__le64));
  2579. ceph_encode_64(p, req->r_fscrypt_file);
  2580. } else {
  2581. ceph_encode_32(p, 0);
  2582. }
  2583. }
  2584. static inline u16 mds_supported_head_version(struct ceph_mds_session *session)
  2585. {
  2586. if (!test_bit(CEPHFS_FEATURE_32BITS_RETRY_FWD, &session->s_features))
  2587. return 1;
  2588. if (!test_bit(CEPHFS_FEATURE_HAS_OWNER_UIDGID, &session->s_features))
  2589. return 2;
  2590. return CEPH_MDS_REQUEST_HEAD_VERSION;
  2591. }
  2592. static struct ceph_mds_request_head_legacy *
  2593. find_legacy_request_head(void *p, u64 features)
  2594. {
  2595. bool legacy = !(features & CEPH_FEATURE_FS_BTIME);
  2596. struct ceph_mds_request_head *head;
  2597. if (legacy)
  2598. return (struct ceph_mds_request_head_legacy *)p;
  2599. head = (struct ceph_mds_request_head *)p;
  2600. return (struct ceph_mds_request_head_legacy *)&head->oldest_client_tid;
  2601. }
  2602. /*
  2603. * called under mdsc->mutex
  2604. */
  2605. static struct ceph_msg *create_request_message(struct ceph_mds_session *session,
  2606. struct ceph_mds_request *req,
  2607. bool drop_cap_releases)
  2608. {
  2609. int mds = session->s_mds;
  2610. struct ceph_mds_client *mdsc = session->s_mdsc;
  2611. struct ceph_client *cl = mdsc->fsc->client;
  2612. struct ceph_msg *msg;
  2613. struct ceph_mds_request_head_legacy *lhead;
  2614. struct ceph_path_info path_info1 = {0};
  2615. struct ceph_path_info path_info2 = {0};
  2616. struct dentry *old_dentry = NULL;
  2617. int len;
  2618. u16 releases;
  2619. void *p, *end;
  2620. int ret;
  2621. bool legacy = !(session->s_con.peer_features & CEPH_FEATURE_FS_BTIME);
  2622. u16 request_head_version = mds_supported_head_version(session);
  2623. kuid_t caller_fsuid = req->r_cred->fsuid;
  2624. kgid_t caller_fsgid = req->r_cred->fsgid;
  2625. bool parent_locked = test_bit(CEPH_MDS_R_PARENT_LOCKED, &req->r_req_flags);
  2626. ret = set_request_path_attr(mdsc, req->r_inode, req->r_dentry,
  2627. req->r_parent, req->r_path1, req->r_ino1.ino,
  2628. &path_info1, parent_locked);
  2629. if (ret < 0) {
  2630. msg = ERR_PTR(ret);
  2631. goto out;
  2632. }
  2633. /*
  2634. * When the parent directory's i_rwsem is *not* locked, req->r_parent may
  2635. * have become stale (e.g. after a concurrent rename) between the time the
  2636. * dentry was looked up and now. If we detect that the stored r_parent
  2637. * does not match the inode number we just encoded for the request, switch
  2638. * to the correct inode so that the MDS receives a valid parent reference.
  2639. */
  2640. if (!parent_locked && req->r_parent && path_info1.vino.ino &&
  2641. ceph_ino(req->r_parent) != path_info1.vino.ino) {
  2642. struct inode *old_parent = req->r_parent;
  2643. struct inode *correct_dir = ceph_get_inode(mdsc->fsc->sb, path_info1.vino, NULL);
  2644. if (!IS_ERR(correct_dir)) {
  2645. WARN_ONCE(1, "ceph: r_parent mismatch (had %llx wanted %llx) - updating\n",
  2646. ceph_ino(old_parent), path_info1.vino.ino);
  2647. /*
  2648. * Transfer CEPH_CAP_PIN from the old parent to the new one.
  2649. * The pin was taken earlier in ceph_mdsc_submit_request().
  2650. */
  2651. ceph_put_cap_refs(ceph_inode(old_parent), CEPH_CAP_PIN);
  2652. iput(old_parent);
  2653. req->r_parent = correct_dir;
  2654. ceph_get_cap_refs(ceph_inode(req->r_parent), CEPH_CAP_PIN);
  2655. }
  2656. }
  2657. /* If r_old_dentry is set, then assume that its parent is locked */
  2658. if (req->r_old_dentry &&
  2659. !(req->r_old_dentry->d_flags & DCACHE_DISCONNECTED))
  2660. old_dentry = req->r_old_dentry;
  2661. ret = set_request_path_attr(mdsc, NULL, old_dentry,
  2662. req->r_old_dentry_dir,
  2663. req->r_path2, req->r_ino2.ino,
  2664. &path_info2, true);
  2665. if (ret < 0) {
  2666. msg = ERR_PTR(ret);
  2667. goto out_free1;
  2668. }
  2669. req->r_altname = get_fscrypt_altname(req, &req->r_altname_len);
  2670. if (IS_ERR(req->r_altname)) {
  2671. msg = ERR_CAST(req->r_altname);
  2672. req->r_altname = NULL;
  2673. goto out_free2;
  2674. }
  2675. /*
  2676. * For old cephs without supporting the 32bit retry/fwd feature
  2677. * it will copy the raw memories directly when decoding the
  2678. * requests. While new cephs will decode the head depending the
  2679. * version member, so we need to make sure it will be compatible
  2680. * with them both.
  2681. */
  2682. if (legacy)
  2683. len = sizeof(struct ceph_mds_request_head_legacy);
  2684. else if (request_head_version == 1)
  2685. len = offsetofend(struct ceph_mds_request_head, args);
  2686. else if (request_head_version == 2)
  2687. len = offsetofend(struct ceph_mds_request_head, ext_num_fwd);
  2688. else
  2689. len = sizeof(struct ceph_mds_request_head);
  2690. /* filepaths */
  2691. len += 2 * (1 + sizeof(u32) + sizeof(u64));
  2692. len += path_info1.pathlen + path_info2.pathlen;
  2693. /* cap releases */
  2694. len += sizeof(struct ceph_mds_request_release) *
  2695. (!!req->r_inode_drop + !!req->r_dentry_drop +
  2696. !!req->r_old_inode_drop + !!req->r_old_dentry_drop);
  2697. if (req->r_dentry_drop)
  2698. len += path_info1.pathlen;
  2699. if (req->r_old_dentry_drop)
  2700. len += path_info2.pathlen;
  2701. /* MClientRequest tail */
  2702. /* req->r_stamp */
  2703. len += sizeof(struct ceph_timespec);
  2704. /* gid list */
  2705. len += sizeof(u32) + (sizeof(u64) * req->r_cred->group_info->ngroups);
  2706. /* alternate name */
  2707. len += sizeof(u32) + req->r_altname_len;
  2708. /* fscrypt_auth */
  2709. len += sizeof(u32); // fscrypt_auth
  2710. if (req->r_fscrypt_auth)
  2711. len += ceph_fscrypt_auth_len(req->r_fscrypt_auth);
  2712. /* fscrypt_file */
  2713. len += sizeof(u32);
  2714. if (test_bit(CEPH_MDS_R_FSCRYPT_FILE, &req->r_req_flags))
  2715. len += sizeof(__le64);
  2716. msg = ceph_msg_new2(CEPH_MSG_CLIENT_REQUEST, len, 1, GFP_NOFS, false);
  2717. if (!msg) {
  2718. msg = ERR_PTR(-ENOMEM);
  2719. goto out_free2;
  2720. }
  2721. msg->hdr.tid = cpu_to_le64(req->r_tid);
  2722. lhead = find_legacy_request_head(msg->front.iov_base,
  2723. session->s_con.peer_features);
  2724. if ((req->r_mnt_idmap != &nop_mnt_idmap) &&
  2725. !test_bit(CEPHFS_FEATURE_HAS_OWNER_UIDGID, &session->s_features)) {
  2726. WARN_ON_ONCE(!IS_CEPH_MDS_OP_NEWINODE(req->r_op));
  2727. if (enable_unsafe_idmap) {
  2728. pr_warn_once_client(cl,
  2729. "idmapped mount is used and CEPHFS_FEATURE_HAS_OWNER_UIDGID"
  2730. " is not supported by MDS. UID/GID-based restrictions may"
  2731. " not work properly.\n");
  2732. caller_fsuid = from_vfsuid(req->r_mnt_idmap, &init_user_ns,
  2733. VFSUIDT_INIT(req->r_cred->fsuid));
  2734. caller_fsgid = from_vfsgid(req->r_mnt_idmap, &init_user_ns,
  2735. VFSGIDT_INIT(req->r_cred->fsgid));
  2736. } else {
  2737. pr_err_ratelimited_client(cl,
  2738. "idmapped mount is used and CEPHFS_FEATURE_HAS_OWNER_UIDGID"
  2739. " is not supported by MDS. Fail request with -EIO.\n");
  2740. ret = -EIO;
  2741. goto out_err;
  2742. }
  2743. }
  2744. /*
  2745. * The ceph_mds_request_head_legacy didn't contain a version field, and
  2746. * one was added when we moved the message version from 3->4.
  2747. */
  2748. if (legacy) {
  2749. msg->hdr.version = cpu_to_le16(3);
  2750. p = msg->front.iov_base + sizeof(*lhead);
  2751. } else if (request_head_version == 1) {
  2752. struct ceph_mds_request_head *nhead = msg->front.iov_base;
  2753. msg->hdr.version = cpu_to_le16(4);
  2754. nhead->version = cpu_to_le16(1);
  2755. p = msg->front.iov_base + offsetofend(struct ceph_mds_request_head, args);
  2756. } else if (request_head_version == 2) {
  2757. struct ceph_mds_request_head *nhead = msg->front.iov_base;
  2758. msg->hdr.version = cpu_to_le16(6);
  2759. nhead->version = cpu_to_le16(2);
  2760. p = msg->front.iov_base + offsetofend(struct ceph_mds_request_head, ext_num_fwd);
  2761. } else {
  2762. struct ceph_mds_request_head *nhead = msg->front.iov_base;
  2763. kuid_t owner_fsuid;
  2764. kgid_t owner_fsgid;
  2765. msg->hdr.version = cpu_to_le16(6);
  2766. nhead->version = cpu_to_le16(CEPH_MDS_REQUEST_HEAD_VERSION);
  2767. nhead->struct_len = cpu_to_le32(sizeof(struct ceph_mds_request_head));
  2768. if (IS_CEPH_MDS_OP_NEWINODE(req->r_op)) {
  2769. owner_fsuid = from_vfsuid(req->r_mnt_idmap, &init_user_ns,
  2770. VFSUIDT_INIT(req->r_cred->fsuid));
  2771. owner_fsgid = from_vfsgid(req->r_mnt_idmap, &init_user_ns,
  2772. VFSGIDT_INIT(req->r_cred->fsgid));
  2773. nhead->owner_uid = cpu_to_le32(from_kuid(&init_user_ns, owner_fsuid));
  2774. nhead->owner_gid = cpu_to_le32(from_kgid(&init_user_ns, owner_fsgid));
  2775. } else {
  2776. nhead->owner_uid = cpu_to_le32(-1);
  2777. nhead->owner_gid = cpu_to_le32(-1);
  2778. }
  2779. p = msg->front.iov_base + sizeof(*nhead);
  2780. }
  2781. end = msg->front.iov_base + msg->front.iov_len;
  2782. lhead->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch);
  2783. lhead->op = cpu_to_le32(req->r_op);
  2784. lhead->caller_uid = cpu_to_le32(from_kuid(&init_user_ns,
  2785. caller_fsuid));
  2786. lhead->caller_gid = cpu_to_le32(from_kgid(&init_user_ns,
  2787. caller_fsgid));
  2788. lhead->ino = cpu_to_le64(req->r_deleg_ino);
  2789. lhead->args = req->r_args;
  2790. ceph_encode_filepath(&p, end, path_info1.vino.ino, path_info1.path);
  2791. ceph_encode_filepath(&p, end, path_info2.vino.ino, path_info2.path);
  2792. /* make note of release offset, in case we need to replay */
  2793. req->r_request_release_offset = p - msg->front.iov_base;
  2794. /* cap releases */
  2795. releases = 0;
  2796. if (req->r_inode_drop)
  2797. releases += ceph_encode_inode_release(&p,
  2798. req->r_inode ? req->r_inode : d_inode(req->r_dentry),
  2799. mds, req->r_inode_drop, req->r_inode_unless,
  2800. req->r_op == CEPH_MDS_OP_READDIR);
  2801. if (req->r_dentry_drop) {
  2802. ret = ceph_encode_dentry_release(&p, req->r_dentry,
  2803. req->r_parent, mds, req->r_dentry_drop,
  2804. req->r_dentry_unless);
  2805. if (ret < 0)
  2806. goto out_err;
  2807. releases += ret;
  2808. }
  2809. if (req->r_old_dentry_drop) {
  2810. ret = ceph_encode_dentry_release(&p, req->r_old_dentry,
  2811. req->r_old_dentry_dir, mds,
  2812. req->r_old_dentry_drop,
  2813. req->r_old_dentry_unless);
  2814. if (ret < 0)
  2815. goto out_err;
  2816. releases += ret;
  2817. }
  2818. if (req->r_old_inode_drop)
  2819. releases += ceph_encode_inode_release(&p,
  2820. d_inode(req->r_old_dentry),
  2821. mds, req->r_old_inode_drop, req->r_old_inode_unless, 0);
  2822. if (drop_cap_releases) {
  2823. releases = 0;
  2824. p = msg->front.iov_base + req->r_request_release_offset;
  2825. }
  2826. lhead->num_releases = cpu_to_le16(releases);
  2827. encode_mclientrequest_tail(&p, req);
  2828. if (WARN_ON_ONCE(p > end)) {
  2829. ceph_msg_put(msg);
  2830. msg = ERR_PTR(-ERANGE);
  2831. goto out_free2;
  2832. }
  2833. msg->front.iov_len = p - msg->front.iov_base;
  2834. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  2835. if (req->r_pagelist) {
  2836. struct ceph_pagelist *pagelist = req->r_pagelist;
  2837. ceph_msg_data_add_pagelist(msg, pagelist);
  2838. msg->hdr.data_len = cpu_to_le32(pagelist->length);
  2839. } else {
  2840. msg->hdr.data_len = 0;
  2841. }
  2842. msg->hdr.data_off = cpu_to_le16(0);
  2843. out_free2:
  2844. ceph_mdsc_free_path_info(&path_info2);
  2845. out_free1:
  2846. ceph_mdsc_free_path_info(&path_info1);
  2847. out:
  2848. return msg;
  2849. out_err:
  2850. ceph_msg_put(msg);
  2851. msg = ERR_PTR(ret);
  2852. goto out_free2;
  2853. }
  2854. /*
  2855. * called under mdsc->mutex if error, under no mutex if
  2856. * success.
  2857. */
  2858. static void complete_request(struct ceph_mds_client *mdsc,
  2859. struct ceph_mds_request *req)
  2860. {
  2861. req->r_end_latency = ktime_get();
  2862. trace_ceph_mdsc_complete_request(mdsc, req);
  2863. if (req->r_callback)
  2864. req->r_callback(mdsc, req);
  2865. complete_all(&req->r_completion);
  2866. }
  2867. /*
  2868. * called under mdsc->mutex
  2869. */
  2870. static int __prepare_send_request(struct ceph_mds_session *session,
  2871. struct ceph_mds_request *req,
  2872. bool drop_cap_releases)
  2873. {
  2874. int mds = session->s_mds;
  2875. struct ceph_mds_client *mdsc = session->s_mdsc;
  2876. struct ceph_client *cl = mdsc->fsc->client;
  2877. struct ceph_mds_request_head_legacy *lhead;
  2878. struct ceph_mds_request_head *nhead;
  2879. struct ceph_msg *msg;
  2880. int flags = 0, old_max_retry;
  2881. bool old_version = !test_bit(CEPHFS_FEATURE_32BITS_RETRY_FWD,
  2882. &session->s_features);
  2883. /*
  2884. * Avoid infinite retrying after overflow. The client will
  2885. * increase the retry count and if the MDS is old version,
  2886. * so we limit to retry at most 256 times.
  2887. */
  2888. if (req->r_attempts) {
  2889. old_max_retry = sizeof_field(struct ceph_mds_request_head,
  2890. num_retry);
  2891. old_max_retry = 1 << (old_max_retry * BITS_PER_BYTE);
  2892. if ((old_version && req->r_attempts >= old_max_retry) ||
  2893. ((uint32_t)req->r_attempts >= U32_MAX)) {
  2894. pr_warn_ratelimited_client(cl, "request tid %llu seq overflow\n",
  2895. req->r_tid);
  2896. return -EMULTIHOP;
  2897. }
  2898. }
  2899. req->r_attempts++;
  2900. if (req->r_inode) {
  2901. struct ceph_cap *cap =
  2902. ceph_get_cap_for_mds(ceph_inode(req->r_inode), mds);
  2903. if (cap)
  2904. req->r_sent_on_mseq = cap->mseq;
  2905. else
  2906. req->r_sent_on_mseq = -1;
  2907. }
  2908. doutc(cl, "%p tid %lld %s (attempt %d)\n", req, req->r_tid,
  2909. ceph_mds_op_name(req->r_op), req->r_attempts);
  2910. if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
  2911. void *p;
  2912. /*
  2913. * Replay. Do not regenerate message (and rebuild
  2914. * paths, etc.); just use the original message.
  2915. * Rebuilding paths will break for renames because
  2916. * d_move mangles the src name.
  2917. */
  2918. msg = req->r_request;
  2919. lhead = find_legacy_request_head(msg->front.iov_base,
  2920. session->s_con.peer_features);
  2921. flags = le32_to_cpu(lhead->flags);
  2922. flags |= CEPH_MDS_FLAG_REPLAY;
  2923. lhead->flags = cpu_to_le32(flags);
  2924. if (req->r_target_inode)
  2925. lhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode));
  2926. lhead->num_retry = req->r_attempts - 1;
  2927. if (!old_version) {
  2928. nhead = (struct ceph_mds_request_head*)msg->front.iov_base;
  2929. nhead->ext_num_retry = cpu_to_le32(req->r_attempts - 1);
  2930. }
  2931. /* remove cap/dentry releases from message */
  2932. lhead->num_releases = 0;
  2933. p = msg->front.iov_base + req->r_request_release_offset;
  2934. encode_mclientrequest_tail(&p, req);
  2935. msg->front.iov_len = p - msg->front.iov_base;
  2936. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  2937. return 0;
  2938. }
  2939. if (req->r_request) {
  2940. ceph_msg_put(req->r_request);
  2941. req->r_request = NULL;
  2942. }
  2943. msg = create_request_message(session, req, drop_cap_releases);
  2944. if (IS_ERR(msg)) {
  2945. req->r_err = PTR_ERR(msg);
  2946. return PTR_ERR(msg);
  2947. }
  2948. req->r_request = msg;
  2949. lhead = find_legacy_request_head(msg->front.iov_base,
  2950. session->s_con.peer_features);
  2951. lhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc));
  2952. if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
  2953. flags |= CEPH_MDS_FLAG_REPLAY;
  2954. if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags))
  2955. flags |= CEPH_MDS_FLAG_ASYNC;
  2956. if (req->r_parent)
  2957. flags |= CEPH_MDS_FLAG_WANT_DENTRY;
  2958. lhead->flags = cpu_to_le32(flags);
  2959. lhead->num_fwd = req->r_num_fwd;
  2960. lhead->num_retry = req->r_attempts - 1;
  2961. if (!old_version) {
  2962. nhead = (struct ceph_mds_request_head*)msg->front.iov_base;
  2963. nhead->ext_num_fwd = cpu_to_le32(req->r_num_fwd);
  2964. nhead->ext_num_retry = cpu_to_le32(req->r_attempts - 1);
  2965. }
  2966. doutc(cl, " r_parent = %p\n", req->r_parent);
  2967. return 0;
  2968. }
  2969. /*
  2970. * called under mdsc->mutex
  2971. */
  2972. static int __send_request(struct ceph_mds_session *session,
  2973. struct ceph_mds_request *req,
  2974. bool drop_cap_releases)
  2975. {
  2976. int err;
  2977. trace_ceph_mdsc_send_request(session, req);
  2978. err = __prepare_send_request(session, req, drop_cap_releases);
  2979. if (!err) {
  2980. ceph_msg_get(req->r_request);
  2981. ceph_con_send(&session->s_con, req->r_request);
  2982. }
  2983. return err;
  2984. }
  2985. /*
  2986. * send request, or put it on the appropriate wait list.
  2987. */
  2988. static void __do_request(struct ceph_mds_client *mdsc,
  2989. struct ceph_mds_request *req)
  2990. {
  2991. struct ceph_client *cl = mdsc->fsc->client;
  2992. struct ceph_mds_session *session = NULL;
  2993. int mds = -1;
  2994. int err = 0;
  2995. bool random;
  2996. if (req->r_err || test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
  2997. if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags))
  2998. __unregister_request(mdsc, req);
  2999. return;
  3000. }
  3001. if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_FENCE_IO) {
  3002. doutc(cl, "metadata corrupted\n");
  3003. err = -EIO;
  3004. goto finish;
  3005. }
  3006. if (req->r_timeout &&
  3007. time_after_eq(jiffies, req->r_started + req->r_timeout)) {
  3008. doutc(cl, "timed out\n");
  3009. err = -ETIMEDOUT;
  3010. goto finish;
  3011. }
  3012. if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
  3013. doutc(cl, "forced umount\n");
  3014. err = -EIO;
  3015. goto finish;
  3016. }
  3017. if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_MOUNTING) {
  3018. if (mdsc->mdsmap_err) {
  3019. err = mdsc->mdsmap_err;
  3020. doutc(cl, "mdsmap err %d\n", err);
  3021. goto finish;
  3022. }
  3023. if (mdsc->mdsmap->m_epoch == 0) {
  3024. doutc(cl, "no mdsmap, waiting for map\n");
  3025. trace_ceph_mdsc_suspend_request(mdsc, session, req,
  3026. ceph_mdsc_suspend_reason_no_mdsmap);
  3027. list_add(&req->r_wait, &mdsc->waiting_for_map);
  3028. return;
  3029. }
  3030. if (!(mdsc->fsc->mount_options->flags &
  3031. CEPH_MOUNT_OPT_MOUNTWAIT) &&
  3032. !ceph_mdsmap_is_cluster_available(mdsc->mdsmap)) {
  3033. err = -EHOSTUNREACH;
  3034. goto finish;
  3035. }
  3036. }
  3037. put_request_session(req);
  3038. mds = __choose_mds(mdsc, req, &random);
  3039. if (mds < 0 ||
  3040. ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) {
  3041. if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
  3042. err = -EJUKEBOX;
  3043. goto finish;
  3044. }
  3045. doutc(cl, "no mds or not active, waiting for map\n");
  3046. trace_ceph_mdsc_suspend_request(mdsc, session, req,
  3047. ceph_mdsc_suspend_reason_no_active_mds);
  3048. list_add(&req->r_wait, &mdsc->waiting_for_map);
  3049. return;
  3050. }
  3051. /* get, open session */
  3052. session = __ceph_lookup_mds_session(mdsc, mds);
  3053. if (!session) {
  3054. session = register_session(mdsc, mds);
  3055. if (IS_ERR(session)) {
  3056. err = PTR_ERR(session);
  3057. goto finish;
  3058. }
  3059. }
  3060. req->r_session = ceph_get_mds_session(session);
  3061. doutc(cl, "mds%d session %p state %s\n", mds, session,
  3062. ceph_session_state_name(session->s_state));
  3063. /*
  3064. * The old ceph will crash the MDSs when see unknown OPs
  3065. */
  3066. if (req->r_feature_needed > 0 &&
  3067. !test_bit(req->r_feature_needed, &session->s_features)) {
  3068. err = -EOPNOTSUPP;
  3069. goto out_session;
  3070. }
  3071. if (session->s_state != CEPH_MDS_SESSION_OPEN &&
  3072. session->s_state != CEPH_MDS_SESSION_HUNG) {
  3073. /*
  3074. * We cannot queue async requests since the caps and delegated
  3075. * inodes are bound to the session. Just return -EJUKEBOX and
  3076. * let the caller retry a sync request in that case.
  3077. */
  3078. if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags)) {
  3079. err = -EJUKEBOX;
  3080. goto out_session;
  3081. }
  3082. /*
  3083. * If the session has been REJECTED, then return a hard error,
  3084. * unless it's a CLEANRECOVER mount, in which case we'll queue
  3085. * it to the mdsc queue.
  3086. */
  3087. if (session->s_state == CEPH_MDS_SESSION_REJECTED) {
  3088. if (ceph_test_mount_opt(mdsc->fsc, CLEANRECOVER)) {
  3089. trace_ceph_mdsc_suspend_request(mdsc, session, req,
  3090. ceph_mdsc_suspend_reason_rejected);
  3091. list_add(&req->r_wait, &mdsc->waiting_for_map);
  3092. } else
  3093. err = -EACCES;
  3094. goto out_session;
  3095. }
  3096. if (session->s_state == CEPH_MDS_SESSION_NEW ||
  3097. session->s_state == CEPH_MDS_SESSION_CLOSING) {
  3098. err = __open_session(mdsc, session);
  3099. if (err)
  3100. goto out_session;
  3101. /* retry the same mds later */
  3102. if (random)
  3103. req->r_resend_mds = mds;
  3104. }
  3105. trace_ceph_mdsc_suspend_request(mdsc, session, req,
  3106. ceph_mdsc_suspend_reason_session);
  3107. list_add(&req->r_wait, &session->s_waiting);
  3108. goto out_session;
  3109. }
  3110. /* send request */
  3111. req->r_resend_mds = -1; /* forget any previous mds hint */
  3112. if (req->r_request_started == 0) /* note request start time */
  3113. req->r_request_started = jiffies;
  3114. /*
  3115. * For async create we will choose the auth MDS of frag in parent
  3116. * directory to send the request and usually this works fine, but
  3117. * if the migrated the dirtory to another MDS before it could handle
  3118. * it the request will be forwarded.
  3119. *
  3120. * And then the auth cap will be changed.
  3121. */
  3122. if (test_bit(CEPH_MDS_R_ASYNC, &req->r_req_flags) && req->r_num_fwd) {
  3123. struct ceph_dentry_info *di = ceph_dentry(req->r_dentry);
  3124. struct ceph_inode_info *ci;
  3125. struct ceph_cap *cap;
  3126. /*
  3127. * The request maybe handled very fast and the new inode
  3128. * hasn't been linked to the dentry yet. We need to wait
  3129. * for the ceph_finish_async_create(), which shouldn't be
  3130. * stuck too long or fail in thoery, to finish when forwarding
  3131. * the request.
  3132. */
  3133. if (!d_inode(req->r_dentry)) {
  3134. err = wait_on_bit(&di->flags, CEPH_DENTRY_ASYNC_CREATE_BIT,
  3135. TASK_KILLABLE);
  3136. if (err) {
  3137. mutex_lock(&req->r_fill_mutex);
  3138. set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
  3139. mutex_unlock(&req->r_fill_mutex);
  3140. goto out_session;
  3141. }
  3142. }
  3143. ci = ceph_inode(d_inode(req->r_dentry));
  3144. spin_lock(&ci->i_ceph_lock);
  3145. cap = ci->i_auth_cap;
  3146. if (ci->i_ceph_flags & CEPH_I_ASYNC_CREATE && mds != cap->mds) {
  3147. doutc(cl, "session changed for auth cap %d -> %d\n",
  3148. cap->session->s_mds, session->s_mds);
  3149. /* Remove the auth cap from old session */
  3150. spin_lock(&cap->session->s_cap_lock);
  3151. cap->session->s_nr_caps--;
  3152. list_del_init(&cap->session_caps);
  3153. spin_unlock(&cap->session->s_cap_lock);
  3154. /* Add the auth cap to the new session */
  3155. cap->mds = mds;
  3156. cap->session = session;
  3157. spin_lock(&session->s_cap_lock);
  3158. session->s_nr_caps++;
  3159. list_add_tail(&cap->session_caps, &session->s_caps);
  3160. spin_unlock(&session->s_cap_lock);
  3161. change_auth_cap_ses(ci, session);
  3162. }
  3163. spin_unlock(&ci->i_ceph_lock);
  3164. }
  3165. err = __send_request(session, req, false);
  3166. out_session:
  3167. ceph_put_mds_session(session);
  3168. finish:
  3169. if (err) {
  3170. doutc(cl, "early error %d\n", err);
  3171. req->r_err = err;
  3172. complete_request(mdsc, req);
  3173. __unregister_request(mdsc, req);
  3174. }
  3175. return;
  3176. }
  3177. /*
  3178. * called under mdsc->mutex
  3179. */
  3180. static void __wake_requests(struct ceph_mds_client *mdsc,
  3181. struct list_head *head)
  3182. {
  3183. struct ceph_client *cl = mdsc->fsc->client;
  3184. struct ceph_mds_request *req;
  3185. LIST_HEAD(tmp_list);
  3186. list_splice_init(head, &tmp_list);
  3187. while (!list_empty(&tmp_list)) {
  3188. req = list_entry(tmp_list.next,
  3189. struct ceph_mds_request, r_wait);
  3190. list_del_init(&req->r_wait);
  3191. doutc(cl, " wake request %p tid %llu\n", req,
  3192. req->r_tid);
  3193. trace_ceph_mdsc_resume_request(mdsc, req);
  3194. __do_request(mdsc, req);
  3195. }
  3196. }
  3197. /*
  3198. * Wake up threads with requests pending for @mds, so that they can
  3199. * resubmit their requests to a possibly different mds.
  3200. */
  3201. static void kick_requests(struct ceph_mds_client *mdsc, int mds)
  3202. {
  3203. struct ceph_client *cl = mdsc->fsc->client;
  3204. struct ceph_mds_request *req;
  3205. struct rb_node *p = rb_first(&mdsc->request_tree);
  3206. doutc(cl, "kick_requests mds%d\n", mds);
  3207. while (p) {
  3208. req = rb_entry(p, struct ceph_mds_request, r_node);
  3209. p = rb_next(p);
  3210. if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
  3211. continue;
  3212. if (req->r_attempts > 0)
  3213. continue; /* only new requests */
  3214. if (req->r_session &&
  3215. req->r_session->s_mds == mds) {
  3216. doutc(cl, " kicking tid %llu\n", req->r_tid);
  3217. list_del_init(&req->r_wait);
  3218. trace_ceph_mdsc_resume_request(mdsc, req);
  3219. __do_request(mdsc, req);
  3220. }
  3221. }
  3222. }
  3223. int ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, struct inode *dir,
  3224. struct ceph_mds_request *req)
  3225. {
  3226. struct ceph_client *cl = mdsc->fsc->client;
  3227. int err = 0;
  3228. /* take CAP_PIN refs for r_inode, r_parent, r_old_dentry */
  3229. if (req->r_inode)
  3230. ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN);
  3231. if (req->r_parent) {
  3232. struct ceph_inode_info *ci = ceph_inode(req->r_parent);
  3233. int fmode = (req->r_op & CEPH_MDS_OP_WRITE) ?
  3234. CEPH_FILE_MODE_WR : CEPH_FILE_MODE_RD;
  3235. spin_lock(&ci->i_ceph_lock);
  3236. ceph_take_cap_refs(ci, CEPH_CAP_PIN, false);
  3237. __ceph_touch_fmode(ci, mdsc, fmode);
  3238. spin_unlock(&ci->i_ceph_lock);
  3239. }
  3240. if (req->r_old_dentry_dir)
  3241. ceph_get_cap_refs(ceph_inode(req->r_old_dentry_dir),
  3242. CEPH_CAP_PIN);
  3243. if (req->r_inode) {
  3244. err = ceph_wait_on_async_create(req->r_inode);
  3245. if (err) {
  3246. doutc(cl, "wait for async create returned: %d\n", err);
  3247. return err;
  3248. }
  3249. }
  3250. if (!err && req->r_old_inode) {
  3251. err = ceph_wait_on_async_create(req->r_old_inode);
  3252. if (err) {
  3253. doutc(cl, "wait for async create returned: %d\n", err);
  3254. return err;
  3255. }
  3256. }
  3257. doutc(cl, "submit_request on %p for inode %p\n", req, dir);
  3258. mutex_lock(&mdsc->mutex);
  3259. __register_request(mdsc, req, dir);
  3260. trace_ceph_mdsc_submit_request(mdsc, req);
  3261. __do_request(mdsc, req);
  3262. err = req->r_err;
  3263. mutex_unlock(&mdsc->mutex);
  3264. return err;
  3265. }
  3266. int ceph_mdsc_wait_request(struct ceph_mds_client *mdsc,
  3267. struct ceph_mds_request *req,
  3268. ceph_mds_request_wait_callback_t wait_func)
  3269. {
  3270. struct ceph_client *cl = mdsc->fsc->client;
  3271. int err;
  3272. /* wait */
  3273. doutc(cl, "do_request waiting\n");
  3274. if (wait_func) {
  3275. err = wait_func(mdsc, req);
  3276. } else {
  3277. long timeleft = wait_for_completion_killable_timeout(
  3278. &req->r_completion,
  3279. ceph_timeout_jiffies(req->r_timeout));
  3280. if (timeleft > 0)
  3281. err = 0;
  3282. else if (!timeleft)
  3283. err = -ETIMEDOUT; /* timed out */
  3284. else
  3285. err = timeleft; /* killed */
  3286. }
  3287. doutc(cl, "do_request waited, got %d\n", err);
  3288. mutex_lock(&mdsc->mutex);
  3289. /* only abort if we didn't race with a real reply */
  3290. if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
  3291. err = le32_to_cpu(req->r_reply_info.head->result);
  3292. } else if (err < 0) {
  3293. doutc(cl, "aborted request %lld with %d\n", req->r_tid, err);
  3294. /*
  3295. * ensure we aren't running concurrently with
  3296. * ceph_fill_trace or ceph_readdir_prepopulate, which
  3297. * rely on locks (dir mutex) held by our caller.
  3298. */
  3299. mutex_lock(&req->r_fill_mutex);
  3300. req->r_err = err;
  3301. set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
  3302. mutex_unlock(&req->r_fill_mutex);
  3303. if (req->r_parent &&
  3304. (req->r_op & CEPH_MDS_OP_WRITE))
  3305. ceph_invalidate_dir_request(req);
  3306. } else {
  3307. err = req->r_err;
  3308. }
  3309. mutex_unlock(&mdsc->mutex);
  3310. return err;
  3311. }
  3312. /*
  3313. * Synchrously perform an mds request. Take care of all of the
  3314. * session setup, forwarding, retry details.
  3315. */
  3316. int ceph_mdsc_do_request(struct ceph_mds_client *mdsc,
  3317. struct inode *dir,
  3318. struct ceph_mds_request *req)
  3319. {
  3320. struct ceph_client *cl = mdsc->fsc->client;
  3321. int err;
  3322. doutc(cl, "do_request on %p\n", req);
  3323. /* issue */
  3324. err = ceph_mdsc_submit_request(mdsc, dir, req);
  3325. if (!err)
  3326. err = ceph_mdsc_wait_request(mdsc, req, NULL);
  3327. doutc(cl, "do_request %p done, result %d\n", req, err);
  3328. return err;
  3329. }
  3330. /*
  3331. * Invalidate dir's completeness, dentry lease state on an aborted MDS
  3332. * namespace request.
  3333. */
  3334. void ceph_invalidate_dir_request(struct ceph_mds_request *req)
  3335. {
  3336. struct inode *dir = req->r_parent;
  3337. struct inode *old_dir = req->r_old_dentry_dir;
  3338. struct ceph_client *cl = req->r_mdsc->fsc->client;
  3339. doutc(cl, "invalidate_dir_request %p %p (complete, lease(s))\n",
  3340. dir, old_dir);
  3341. ceph_dir_clear_complete(dir);
  3342. if (old_dir)
  3343. ceph_dir_clear_complete(old_dir);
  3344. if (req->r_dentry)
  3345. ceph_invalidate_dentry_lease(req->r_dentry);
  3346. if (req->r_old_dentry)
  3347. ceph_invalidate_dentry_lease(req->r_old_dentry);
  3348. }
  3349. /*
  3350. * Handle mds reply.
  3351. *
  3352. * We take the session mutex and parse and process the reply immediately.
  3353. * This preserves the logical ordering of replies, capabilities, etc., sent
  3354. * by the MDS as they are applied to our local cache.
  3355. */
  3356. static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg)
  3357. {
  3358. struct ceph_mds_client *mdsc = session->s_mdsc;
  3359. struct ceph_client *cl = mdsc->fsc->client;
  3360. struct ceph_mds_request *req;
  3361. struct ceph_mds_reply_head *head = msg->front.iov_base;
  3362. struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */
  3363. struct ceph_snap_realm *realm;
  3364. u64 tid;
  3365. int err, result;
  3366. int mds = session->s_mds;
  3367. bool close_sessions = false;
  3368. if (msg->front.iov_len < sizeof(*head)) {
  3369. pr_err_client(cl, "got corrupt (short) reply\n");
  3370. ceph_msg_dump(msg);
  3371. return;
  3372. }
  3373. /* get request, session */
  3374. tid = le64_to_cpu(msg->hdr.tid);
  3375. mutex_lock(&mdsc->mutex);
  3376. req = lookup_get_request(mdsc, tid);
  3377. if (!req) {
  3378. doutc(cl, "on unknown tid %llu\n", tid);
  3379. mutex_unlock(&mdsc->mutex);
  3380. return;
  3381. }
  3382. doutc(cl, "handle_reply %p\n", req);
  3383. /* correct session? */
  3384. if (req->r_session != session) {
  3385. pr_err_client(cl, "got %llu on session mds%d not mds%d\n",
  3386. tid, session->s_mds,
  3387. req->r_session ? req->r_session->s_mds : -1);
  3388. mutex_unlock(&mdsc->mutex);
  3389. goto out;
  3390. }
  3391. /* dup? */
  3392. if ((test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags) && !head->safe) ||
  3393. (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags) && head->safe)) {
  3394. pr_warn_client(cl, "got a dup %s reply on %llu from mds%d\n",
  3395. head->safe ? "safe" : "unsafe", tid, mds);
  3396. mutex_unlock(&mdsc->mutex);
  3397. goto out;
  3398. }
  3399. if (test_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags)) {
  3400. pr_warn_client(cl, "got unsafe after safe on %llu from mds%d\n",
  3401. tid, mds);
  3402. mutex_unlock(&mdsc->mutex);
  3403. goto out;
  3404. }
  3405. result = le32_to_cpu(head->result);
  3406. if (head->safe) {
  3407. set_bit(CEPH_MDS_R_GOT_SAFE, &req->r_req_flags);
  3408. __unregister_request(mdsc, req);
  3409. /* last request during umount? */
  3410. if (mdsc->stopping && !__get_oldest_req(mdsc))
  3411. complete_all(&mdsc->safe_umount_waiters);
  3412. if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
  3413. /*
  3414. * We already handled the unsafe response, now do the
  3415. * cleanup. No need to examine the response; the MDS
  3416. * doesn't include any result info in the safe
  3417. * response. And even if it did, there is nothing
  3418. * useful we could do with a revised return value.
  3419. */
  3420. doutc(cl, "got safe reply %llu, mds%d\n", tid, mds);
  3421. mutex_unlock(&mdsc->mutex);
  3422. goto out;
  3423. }
  3424. } else {
  3425. set_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags);
  3426. list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe);
  3427. }
  3428. doutc(cl, "tid %lld result %d\n", tid, result);
  3429. if (test_bit(CEPHFS_FEATURE_REPLY_ENCODING, &session->s_features))
  3430. err = parse_reply_info(session, msg, req, (u64)-1);
  3431. else
  3432. err = parse_reply_info(session, msg, req,
  3433. session->s_con.peer_features);
  3434. mutex_unlock(&mdsc->mutex);
  3435. /* Must find target inode outside of mutexes to avoid deadlocks */
  3436. rinfo = &req->r_reply_info;
  3437. if ((err >= 0) && rinfo->head->is_target) {
  3438. struct inode *in = xchg(&req->r_new_inode, NULL);
  3439. struct ceph_vino tvino = {
  3440. .ino = le64_to_cpu(rinfo->targeti.in->ino),
  3441. .snap = le64_to_cpu(rinfo->targeti.in->snapid)
  3442. };
  3443. /*
  3444. * If we ended up opening an existing inode, discard
  3445. * r_new_inode
  3446. */
  3447. if (req->r_op == CEPH_MDS_OP_CREATE &&
  3448. !req->r_reply_info.has_create_ino) {
  3449. /* This should never happen on an async create */
  3450. WARN_ON_ONCE(req->r_deleg_ino);
  3451. iput(in);
  3452. in = NULL;
  3453. }
  3454. in = ceph_get_inode(mdsc->fsc->sb, tvino, in);
  3455. if (IS_ERR(in)) {
  3456. err = PTR_ERR(in);
  3457. mutex_lock(&session->s_mutex);
  3458. goto out_err;
  3459. }
  3460. req->r_target_inode = in;
  3461. }
  3462. mutex_lock(&session->s_mutex);
  3463. if (err < 0) {
  3464. pr_err_client(cl, "got corrupt reply mds%d(tid:%lld)\n",
  3465. mds, tid);
  3466. ceph_msg_dump(msg);
  3467. goto out_err;
  3468. }
  3469. /* snap trace */
  3470. realm = NULL;
  3471. if (rinfo->snapblob_len) {
  3472. down_write(&mdsc->snap_rwsem);
  3473. err = ceph_update_snap_trace(mdsc, rinfo->snapblob,
  3474. rinfo->snapblob + rinfo->snapblob_len,
  3475. le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP,
  3476. &realm);
  3477. if (err) {
  3478. up_write(&mdsc->snap_rwsem);
  3479. close_sessions = true;
  3480. if (err == -EIO)
  3481. ceph_msg_dump(msg);
  3482. goto out_err;
  3483. }
  3484. downgrade_write(&mdsc->snap_rwsem);
  3485. } else {
  3486. down_read(&mdsc->snap_rwsem);
  3487. }
  3488. /* insert trace into our cache */
  3489. mutex_lock(&req->r_fill_mutex);
  3490. current->journal_info = req;
  3491. err = ceph_fill_trace(mdsc->fsc->sb, req);
  3492. if (err == 0) {
  3493. if (result == 0 && (req->r_op == CEPH_MDS_OP_READDIR ||
  3494. req->r_op == CEPH_MDS_OP_LSSNAP))
  3495. err = ceph_readdir_prepopulate(req, req->r_session);
  3496. }
  3497. current->journal_info = NULL;
  3498. mutex_unlock(&req->r_fill_mutex);
  3499. up_read(&mdsc->snap_rwsem);
  3500. if (realm)
  3501. ceph_put_snap_realm(mdsc, realm);
  3502. if (err == 0) {
  3503. if (req->r_target_inode &&
  3504. test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags)) {
  3505. struct ceph_inode_info *ci =
  3506. ceph_inode(req->r_target_inode);
  3507. spin_lock(&ci->i_unsafe_lock);
  3508. list_add_tail(&req->r_unsafe_target_item,
  3509. &ci->i_unsafe_iops);
  3510. spin_unlock(&ci->i_unsafe_lock);
  3511. }
  3512. ceph_unreserve_caps(mdsc, &req->r_caps_reservation);
  3513. }
  3514. out_err:
  3515. mutex_lock(&mdsc->mutex);
  3516. if (!test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
  3517. if (err) {
  3518. req->r_err = err;
  3519. } else {
  3520. req->r_reply = ceph_msg_get(msg);
  3521. set_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags);
  3522. }
  3523. } else {
  3524. doutc(cl, "reply arrived after request %lld was aborted\n", tid);
  3525. }
  3526. mutex_unlock(&mdsc->mutex);
  3527. mutex_unlock(&session->s_mutex);
  3528. /* kick calling process */
  3529. complete_request(mdsc, req);
  3530. ceph_update_metadata_metrics(&mdsc->metric, req->r_start_latency,
  3531. req->r_end_latency, err);
  3532. out:
  3533. ceph_mdsc_put_request(req);
  3534. /* Defer closing the sessions after s_mutex lock being released */
  3535. if (close_sessions)
  3536. ceph_mdsc_close_sessions(mdsc);
  3537. return;
  3538. }
  3539. /*
  3540. * handle mds notification that our request has been forwarded.
  3541. */
  3542. static void handle_forward(struct ceph_mds_client *mdsc,
  3543. struct ceph_mds_session *session,
  3544. struct ceph_msg *msg)
  3545. {
  3546. struct ceph_client *cl = mdsc->fsc->client;
  3547. struct ceph_mds_request *req;
  3548. u64 tid = le64_to_cpu(msg->hdr.tid);
  3549. u32 next_mds;
  3550. u32 fwd_seq;
  3551. int err = -EINVAL;
  3552. void *p = msg->front.iov_base;
  3553. void *end = p + msg->front.iov_len;
  3554. bool aborted = false;
  3555. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  3556. next_mds = ceph_decode_32(&p);
  3557. fwd_seq = ceph_decode_32(&p);
  3558. mutex_lock(&mdsc->mutex);
  3559. req = lookup_get_request(mdsc, tid);
  3560. if (!req) {
  3561. mutex_unlock(&mdsc->mutex);
  3562. doutc(cl, "forward tid %llu to mds%d - req dne\n", tid, next_mds);
  3563. return; /* dup reply? */
  3564. }
  3565. if (test_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags)) {
  3566. doutc(cl, "forward tid %llu aborted, unregistering\n", tid);
  3567. __unregister_request(mdsc, req);
  3568. } else if (fwd_seq <= req->r_num_fwd || (uint32_t)fwd_seq >= U32_MAX) {
  3569. /*
  3570. * Avoid infinite retrying after overflow.
  3571. *
  3572. * The MDS will increase the fwd count and in client side
  3573. * if the num_fwd is less than the one saved in request
  3574. * that means the MDS is an old version and overflowed of
  3575. * 8 bits.
  3576. */
  3577. mutex_lock(&req->r_fill_mutex);
  3578. req->r_err = -EMULTIHOP;
  3579. set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
  3580. mutex_unlock(&req->r_fill_mutex);
  3581. aborted = true;
  3582. pr_warn_ratelimited_client(cl, "forward tid %llu seq overflow\n",
  3583. tid);
  3584. } else {
  3585. /* resend. forward race not possible; mds would drop */
  3586. doutc(cl, "forward tid %llu to mds%d (we resend)\n", tid, next_mds);
  3587. BUG_ON(req->r_err);
  3588. BUG_ON(test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags));
  3589. req->r_attempts = 0;
  3590. req->r_num_fwd = fwd_seq;
  3591. req->r_resend_mds = next_mds;
  3592. put_request_session(req);
  3593. __do_request(mdsc, req);
  3594. }
  3595. mutex_unlock(&mdsc->mutex);
  3596. /* kick calling process */
  3597. if (aborted)
  3598. complete_request(mdsc, req);
  3599. ceph_mdsc_put_request(req);
  3600. return;
  3601. bad:
  3602. pr_err_client(cl, "decode error err=%d\n", err);
  3603. ceph_msg_dump(msg);
  3604. }
  3605. static int __decode_session_metadata(void **p, void *end,
  3606. bool *blocklisted)
  3607. {
  3608. /* map<string,string> */
  3609. u32 n;
  3610. bool err_str;
  3611. ceph_decode_32_safe(p, end, n, bad);
  3612. while (n-- > 0) {
  3613. u32 len;
  3614. ceph_decode_32_safe(p, end, len, bad);
  3615. ceph_decode_need(p, end, len, bad);
  3616. err_str = !strncmp(*p, "error_string", len);
  3617. *p += len;
  3618. ceph_decode_32_safe(p, end, len, bad);
  3619. ceph_decode_need(p, end, len, bad);
  3620. /*
  3621. * Match "blocklisted (blacklisted)" from newer MDSes,
  3622. * or "blacklisted" from older MDSes.
  3623. */
  3624. if (err_str && strnstr(*p, "blacklisted", len))
  3625. *blocklisted = true;
  3626. *p += len;
  3627. }
  3628. return 0;
  3629. bad:
  3630. return -1;
  3631. }
  3632. /*
  3633. * handle a mds session control message
  3634. */
  3635. static void handle_session(struct ceph_mds_session *session,
  3636. struct ceph_msg *msg)
  3637. {
  3638. struct ceph_mds_client *mdsc = session->s_mdsc;
  3639. struct ceph_client *cl = mdsc->fsc->client;
  3640. int mds = session->s_mds;
  3641. int msg_version = le16_to_cpu(msg->hdr.version);
  3642. void *p = msg->front.iov_base;
  3643. void *end = p + msg->front.iov_len;
  3644. struct ceph_mds_session_head *h;
  3645. struct ceph_mds_cap_auth *cap_auths = NULL;
  3646. u32 op, cap_auths_num = 0;
  3647. u64 seq, features = 0;
  3648. int wake = 0;
  3649. bool blocklisted = false;
  3650. u32 i;
  3651. /* decode */
  3652. ceph_decode_need(&p, end, sizeof(*h), bad);
  3653. h = p;
  3654. p += sizeof(*h);
  3655. op = le32_to_cpu(h->op);
  3656. seq = le64_to_cpu(h->seq);
  3657. if (msg_version >= 3) {
  3658. u32 len;
  3659. /* version >= 2 and < 5, decode metadata, skip otherwise
  3660. * as it's handled via flags.
  3661. */
  3662. if (msg_version >= 5)
  3663. ceph_decode_skip_map(&p, end, string, string, bad);
  3664. else if (__decode_session_metadata(&p, end, &blocklisted) < 0)
  3665. goto bad;
  3666. /* version >= 3, feature bits */
  3667. ceph_decode_32_safe(&p, end, len, bad);
  3668. if (len) {
  3669. ceph_decode_64_safe(&p, end, features, bad);
  3670. p += len - sizeof(features);
  3671. }
  3672. }
  3673. if (msg_version >= 5) {
  3674. u32 flags, len;
  3675. /* version >= 4 */
  3676. ceph_decode_skip_16(&p, end, bad); /* struct_v, struct_cv */
  3677. ceph_decode_32_safe(&p, end, len, bad); /* len */
  3678. ceph_decode_skip_n(&p, end, len, bad); /* metric_spec */
  3679. /* version >= 5, flags */
  3680. ceph_decode_32_safe(&p, end, flags, bad);
  3681. if (flags & CEPH_SESSION_BLOCKLISTED) {
  3682. pr_warn_client(cl, "mds%d session blocklisted\n",
  3683. session->s_mds);
  3684. blocklisted = true;
  3685. }
  3686. }
  3687. if (msg_version >= 6) {
  3688. ceph_decode_32_safe(&p, end, cap_auths_num, bad);
  3689. doutc(cl, "cap_auths_num %d\n", cap_auths_num);
  3690. if (cap_auths_num && op != CEPH_SESSION_OPEN) {
  3691. WARN_ON_ONCE(op != CEPH_SESSION_OPEN);
  3692. goto skip_cap_auths;
  3693. }
  3694. cap_auths = kzalloc_objs(struct ceph_mds_cap_auth,
  3695. cap_auths_num);
  3696. if (!cap_auths) {
  3697. pr_err_client(cl, "No memory for cap_auths\n");
  3698. return;
  3699. }
  3700. for (i = 0; i < cap_auths_num; i++) {
  3701. u32 _len, j;
  3702. /* struct_v, struct_compat, and struct_len in MDSCapAuth */
  3703. ceph_decode_skip_n(&p, end, 2 + sizeof(u32), bad);
  3704. /* struct_v, struct_compat, and struct_len in MDSCapMatch */
  3705. ceph_decode_skip_n(&p, end, 2 + sizeof(u32), bad);
  3706. ceph_decode_64_safe(&p, end, cap_auths[i].match.uid, bad);
  3707. ceph_decode_32_safe(&p, end, _len, bad);
  3708. if (_len) {
  3709. cap_auths[i].match.gids = kcalloc(_len, sizeof(u32),
  3710. GFP_KERNEL);
  3711. if (!cap_auths[i].match.gids) {
  3712. pr_err_client(cl, "No memory for gids\n");
  3713. goto fail;
  3714. }
  3715. cap_auths[i].match.num_gids = _len;
  3716. for (j = 0; j < _len; j++)
  3717. ceph_decode_32_safe(&p, end,
  3718. cap_auths[i].match.gids[j],
  3719. bad);
  3720. }
  3721. ceph_decode_32_safe(&p, end, _len, bad);
  3722. if (_len) {
  3723. cap_auths[i].match.path = kcalloc(_len + 1, sizeof(char),
  3724. GFP_KERNEL);
  3725. if (!cap_auths[i].match.path) {
  3726. pr_err_client(cl, "No memory for path\n");
  3727. goto fail;
  3728. }
  3729. ceph_decode_copy(&p, cap_auths[i].match.path, _len);
  3730. /* Remove the tailing '/' */
  3731. while (_len && cap_auths[i].match.path[_len - 1] == '/') {
  3732. cap_auths[i].match.path[_len - 1] = '\0';
  3733. _len -= 1;
  3734. }
  3735. }
  3736. ceph_decode_32_safe(&p, end, _len, bad);
  3737. if (_len) {
  3738. cap_auths[i].match.fs_name = kcalloc(_len + 1, sizeof(char),
  3739. GFP_KERNEL);
  3740. if (!cap_auths[i].match.fs_name) {
  3741. pr_err_client(cl, "No memory for fs_name\n");
  3742. goto fail;
  3743. }
  3744. ceph_decode_copy(&p, cap_auths[i].match.fs_name, _len);
  3745. }
  3746. ceph_decode_8_safe(&p, end, cap_auths[i].match.root_squash, bad);
  3747. ceph_decode_8_safe(&p, end, cap_auths[i].readable, bad);
  3748. ceph_decode_8_safe(&p, end, cap_auths[i].writeable, bad);
  3749. doutc(cl, "uid %lld, num_gids %u, path %s, fs_name %s, root_squash %d, readable %d, writeable %d\n",
  3750. cap_auths[i].match.uid, cap_auths[i].match.num_gids,
  3751. cap_auths[i].match.path, cap_auths[i].match.fs_name,
  3752. cap_auths[i].match.root_squash,
  3753. cap_auths[i].readable, cap_auths[i].writeable);
  3754. }
  3755. }
  3756. skip_cap_auths:
  3757. mutex_lock(&mdsc->mutex);
  3758. if (op == CEPH_SESSION_OPEN) {
  3759. if (mdsc->s_cap_auths) {
  3760. for (i = 0; i < mdsc->s_cap_auths_num; i++) {
  3761. kfree(mdsc->s_cap_auths[i].match.gids);
  3762. kfree(mdsc->s_cap_auths[i].match.path);
  3763. kfree(mdsc->s_cap_auths[i].match.fs_name);
  3764. }
  3765. kfree(mdsc->s_cap_auths);
  3766. }
  3767. mdsc->s_cap_auths_num = cap_auths_num;
  3768. mdsc->s_cap_auths = cap_auths;
  3769. }
  3770. if (op == CEPH_SESSION_CLOSE) {
  3771. ceph_get_mds_session(session);
  3772. __unregister_session(mdsc, session);
  3773. }
  3774. /* FIXME: this ttl calculation is generous */
  3775. session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose;
  3776. mutex_unlock(&mdsc->mutex);
  3777. mutex_lock(&session->s_mutex);
  3778. doutc(cl, "mds%d %s %p state %s seq %llu\n", mds,
  3779. ceph_session_op_name(op), session,
  3780. ceph_session_state_name(session->s_state), seq);
  3781. if (session->s_state == CEPH_MDS_SESSION_HUNG) {
  3782. session->s_state = CEPH_MDS_SESSION_OPEN;
  3783. pr_info_client(cl, "mds%d came back\n", session->s_mds);
  3784. }
  3785. switch (op) {
  3786. case CEPH_SESSION_OPEN:
  3787. if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
  3788. pr_info_client(cl, "mds%d reconnect success\n",
  3789. session->s_mds);
  3790. session->s_features = features;
  3791. if (session->s_state == CEPH_MDS_SESSION_OPEN) {
  3792. pr_notice_client(cl, "mds%d is already opened\n",
  3793. session->s_mds);
  3794. } else {
  3795. session->s_state = CEPH_MDS_SESSION_OPEN;
  3796. renewed_caps(mdsc, session, 0);
  3797. if (test_bit(CEPHFS_FEATURE_METRIC_COLLECT,
  3798. &session->s_features))
  3799. metric_schedule_delayed(&mdsc->metric);
  3800. }
  3801. /*
  3802. * The connection maybe broken and the session in client
  3803. * side has been reinitialized, need to update the seq
  3804. * anyway.
  3805. */
  3806. if (!session->s_seq && seq)
  3807. session->s_seq = seq;
  3808. wake = 1;
  3809. if (mdsc->stopping)
  3810. __close_session(mdsc, session);
  3811. break;
  3812. case CEPH_SESSION_RENEWCAPS:
  3813. if (session->s_renew_seq == seq)
  3814. renewed_caps(mdsc, session, 1);
  3815. break;
  3816. case CEPH_SESSION_CLOSE:
  3817. if (session->s_state == CEPH_MDS_SESSION_RECONNECTING)
  3818. pr_info_client(cl, "mds%d reconnect denied\n",
  3819. session->s_mds);
  3820. session->s_state = CEPH_MDS_SESSION_CLOSED;
  3821. cleanup_session_requests(mdsc, session);
  3822. remove_session_caps(session);
  3823. wake = 2; /* for good measure */
  3824. wake_up_all(&mdsc->session_close_wq);
  3825. break;
  3826. case CEPH_SESSION_STALE:
  3827. pr_info_client(cl, "mds%d caps went stale, renewing\n",
  3828. session->s_mds);
  3829. atomic_inc(&session->s_cap_gen);
  3830. session->s_cap_ttl = jiffies - 1;
  3831. send_renew_caps(mdsc, session);
  3832. break;
  3833. case CEPH_SESSION_RECALL_STATE:
  3834. ceph_trim_caps(mdsc, session, le32_to_cpu(h->max_caps));
  3835. break;
  3836. case CEPH_SESSION_FLUSHMSG:
  3837. /* flush cap releases */
  3838. spin_lock(&session->s_cap_lock);
  3839. if (session->s_num_cap_releases)
  3840. ceph_flush_session_cap_releases(mdsc, session);
  3841. spin_unlock(&session->s_cap_lock);
  3842. send_flushmsg_ack(mdsc, session, seq);
  3843. break;
  3844. case CEPH_SESSION_FORCE_RO:
  3845. doutc(cl, "force_session_readonly %p\n", session);
  3846. spin_lock(&session->s_cap_lock);
  3847. session->s_readonly = true;
  3848. spin_unlock(&session->s_cap_lock);
  3849. wake_up_session_caps(session, FORCE_RO);
  3850. break;
  3851. case CEPH_SESSION_REJECT:
  3852. WARN_ON(session->s_state != CEPH_MDS_SESSION_OPENING);
  3853. pr_info_client(cl, "mds%d rejected session\n",
  3854. session->s_mds);
  3855. session->s_state = CEPH_MDS_SESSION_REJECTED;
  3856. cleanup_session_requests(mdsc, session);
  3857. remove_session_caps(session);
  3858. if (blocklisted)
  3859. mdsc->fsc->blocklisted = true;
  3860. wake = 2; /* for good measure */
  3861. break;
  3862. default:
  3863. pr_err_client(cl, "bad op %d mds%d\n", op, mds);
  3864. WARN_ON(1);
  3865. }
  3866. mutex_unlock(&session->s_mutex);
  3867. if (wake) {
  3868. mutex_lock(&mdsc->mutex);
  3869. __wake_requests(mdsc, &session->s_waiting);
  3870. if (wake == 2)
  3871. kick_requests(mdsc, mds);
  3872. mutex_unlock(&mdsc->mutex);
  3873. }
  3874. if (op == CEPH_SESSION_CLOSE)
  3875. ceph_put_mds_session(session);
  3876. return;
  3877. bad:
  3878. pr_err_client(cl, "corrupt message mds%d len %d\n", mds,
  3879. (int)msg->front.iov_len);
  3880. ceph_msg_dump(msg);
  3881. fail:
  3882. for (i = 0; i < cap_auths_num; i++) {
  3883. kfree(cap_auths[i].match.gids);
  3884. kfree(cap_auths[i].match.path);
  3885. kfree(cap_auths[i].match.fs_name);
  3886. }
  3887. kfree(cap_auths);
  3888. return;
  3889. }
  3890. void ceph_mdsc_release_dir_caps(struct ceph_mds_request *req)
  3891. {
  3892. struct ceph_client *cl = req->r_mdsc->fsc->client;
  3893. int dcaps;
  3894. dcaps = xchg(&req->r_dir_caps, 0);
  3895. if (dcaps) {
  3896. doutc(cl, "releasing r_dir_caps=%s\n", ceph_cap_string(dcaps));
  3897. ceph_put_cap_refs(ceph_inode(req->r_parent), dcaps);
  3898. }
  3899. }
  3900. void ceph_mdsc_release_dir_caps_async(struct ceph_mds_request *req)
  3901. {
  3902. struct ceph_client *cl = req->r_mdsc->fsc->client;
  3903. int dcaps;
  3904. dcaps = xchg(&req->r_dir_caps, 0);
  3905. if (dcaps) {
  3906. doutc(cl, "releasing r_dir_caps=%s\n", ceph_cap_string(dcaps));
  3907. ceph_put_cap_refs_async(ceph_inode(req->r_parent), dcaps);
  3908. }
  3909. }
  3910. /*
  3911. * called under session->mutex.
  3912. */
  3913. static void replay_unsafe_requests(struct ceph_mds_client *mdsc,
  3914. struct ceph_mds_session *session)
  3915. {
  3916. struct ceph_mds_request *req, *nreq;
  3917. struct rb_node *p;
  3918. doutc(mdsc->fsc->client, "mds%d\n", session->s_mds);
  3919. mutex_lock(&mdsc->mutex);
  3920. list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item)
  3921. __send_request(session, req, true);
  3922. /*
  3923. * also re-send old requests when MDS enters reconnect stage. So that MDS
  3924. * can process completed request in clientreplay stage.
  3925. */
  3926. p = rb_first(&mdsc->request_tree);
  3927. while (p) {
  3928. req = rb_entry(p, struct ceph_mds_request, r_node);
  3929. p = rb_next(p);
  3930. if (test_bit(CEPH_MDS_R_GOT_UNSAFE, &req->r_req_flags))
  3931. continue;
  3932. if (req->r_attempts == 0)
  3933. continue; /* only old requests */
  3934. if (!req->r_session)
  3935. continue;
  3936. if (req->r_session->s_mds != session->s_mds)
  3937. continue;
  3938. ceph_mdsc_release_dir_caps_async(req);
  3939. __send_request(session, req, true);
  3940. }
  3941. mutex_unlock(&mdsc->mutex);
  3942. }
  3943. static int send_reconnect_partial(struct ceph_reconnect_state *recon_state)
  3944. {
  3945. struct ceph_msg *reply;
  3946. struct ceph_pagelist *_pagelist;
  3947. struct page *page;
  3948. __le32 *addr;
  3949. int err = -ENOMEM;
  3950. if (!recon_state->allow_multi)
  3951. return -ENOSPC;
  3952. /* can't handle message that contains both caps and realm */
  3953. BUG_ON(!recon_state->nr_caps == !recon_state->nr_realms);
  3954. /* pre-allocate new pagelist */
  3955. _pagelist = ceph_pagelist_alloc(GFP_NOFS);
  3956. if (!_pagelist)
  3957. return -ENOMEM;
  3958. reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
  3959. if (!reply)
  3960. goto fail_msg;
  3961. /* placeholder for nr_caps */
  3962. err = ceph_pagelist_encode_32(_pagelist, 0);
  3963. if (err < 0)
  3964. goto fail;
  3965. if (recon_state->nr_caps) {
  3966. /* currently encoding caps */
  3967. err = ceph_pagelist_encode_32(recon_state->pagelist, 0);
  3968. if (err)
  3969. goto fail;
  3970. } else {
  3971. /* placeholder for nr_realms (currently encoding relams) */
  3972. err = ceph_pagelist_encode_32(_pagelist, 0);
  3973. if (err < 0)
  3974. goto fail;
  3975. }
  3976. err = ceph_pagelist_encode_8(recon_state->pagelist, 1);
  3977. if (err)
  3978. goto fail;
  3979. page = list_first_entry(&recon_state->pagelist->head, struct page, lru);
  3980. addr = kmap_atomic(page);
  3981. if (recon_state->nr_caps) {
  3982. /* currently encoding caps */
  3983. *addr = cpu_to_le32(recon_state->nr_caps);
  3984. } else {
  3985. /* currently encoding relams */
  3986. *(addr + 1) = cpu_to_le32(recon_state->nr_realms);
  3987. }
  3988. kunmap_atomic(addr);
  3989. reply->hdr.version = cpu_to_le16(5);
  3990. reply->hdr.compat_version = cpu_to_le16(4);
  3991. reply->hdr.data_len = cpu_to_le32(recon_state->pagelist->length);
  3992. ceph_msg_data_add_pagelist(reply, recon_state->pagelist);
  3993. ceph_con_send(&recon_state->session->s_con, reply);
  3994. ceph_pagelist_release(recon_state->pagelist);
  3995. recon_state->pagelist = _pagelist;
  3996. recon_state->nr_caps = 0;
  3997. recon_state->nr_realms = 0;
  3998. recon_state->msg_version = 5;
  3999. return 0;
  4000. fail:
  4001. ceph_msg_put(reply);
  4002. fail_msg:
  4003. ceph_pagelist_release(_pagelist);
  4004. return err;
  4005. }
  4006. static struct dentry* d_find_primary(struct inode *inode)
  4007. {
  4008. struct dentry *alias, *dn = NULL;
  4009. if (hlist_empty(&inode->i_dentry))
  4010. return NULL;
  4011. spin_lock(&inode->i_lock);
  4012. if (hlist_empty(&inode->i_dentry))
  4013. goto out_unlock;
  4014. if (S_ISDIR(inode->i_mode)) {
  4015. alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
  4016. if (!IS_ROOT(alias))
  4017. dn = dget(alias);
  4018. goto out_unlock;
  4019. }
  4020. hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
  4021. spin_lock(&alias->d_lock);
  4022. if (!d_unhashed(alias) &&
  4023. (ceph_dentry(alias)->flags & CEPH_DENTRY_PRIMARY_LINK)) {
  4024. dn = dget_dlock(alias);
  4025. }
  4026. spin_unlock(&alias->d_lock);
  4027. if (dn)
  4028. break;
  4029. }
  4030. out_unlock:
  4031. spin_unlock(&inode->i_lock);
  4032. return dn;
  4033. }
  4034. /*
  4035. * Encode information about a cap for a reconnect with the MDS.
  4036. */
  4037. static int reconnect_caps_cb(struct inode *inode, int mds, void *arg)
  4038. {
  4039. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
  4040. struct ceph_client *cl = ceph_inode_to_client(inode);
  4041. union {
  4042. struct ceph_mds_cap_reconnect v2;
  4043. struct ceph_mds_cap_reconnect_v1 v1;
  4044. } rec;
  4045. struct ceph_inode_info *ci = ceph_inode(inode);
  4046. struct ceph_reconnect_state *recon_state = arg;
  4047. struct ceph_pagelist *pagelist = recon_state->pagelist;
  4048. struct dentry *dentry;
  4049. struct ceph_cap *cap;
  4050. struct ceph_path_info path_info = {0};
  4051. int err;
  4052. u64 snap_follows;
  4053. dentry = d_find_primary(inode);
  4054. if (dentry) {
  4055. /* set pathbase to parent dir when msg_version >= 2 */
  4056. char *path = ceph_mdsc_build_path(mdsc, dentry, &path_info,
  4057. recon_state->msg_version >= 2);
  4058. dput(dentry);
  4059. if (IS_ERR(path)) {
  4060. err = PTR_ERR(path);
  4061. goto out_err;
  4062. }
  4063. }
  4064. spin_lock(&ci->i_ceph_lock);
  4065. cap = __get_cap_for_mds(ci, mds);
  4066. if (!cap) {
  4067. spin_unlock(&ci->i_ceph_lock);
  4068. err = 0;
  4069. goto out_err;
  4070. }
  4071. doutc(cl, " adding %p ino %llx.%llx cap %p %lld %s\n", inode,
  4072. ceph_vinop(inode), cap, cap->cap_id,
  4073. ceph_cap_string(cap->issued));
  4074. cap->seq = 0; /* reset cap seq */
  4075. cap->issue_seq = 0; /* and issue_seq */
  4076. cap->mseq = 0; /* and migrate_seq */
  4077. cap->cap_gen = atomic_read(&cap->session->s_cap_gen);
  4078. /* These are lost when the session goes away */
  4079. if (S_ISDIR(inode->i_mode)) {
  4080. if (cap->issued & CEPH_CAP_DIR_CREATE) {
  4081. ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
  4082. memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
  4083. }
  4084. cap->issued &= ~CEPH_CAP_ANY_DIR_OPS;
  4085. }
  4086. if (recon_state->msg_version >= 2) {
  4087. rec.v2.cap_id = cpu_to_le64(cap->cap_id);
  4088. rec.v2.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
  4089. rec.v2.issued = cpu_to_le32(cap->issued);
  4090. rec.v2.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
  4091. rec.v2.pathbase = cpu_to_le64(path_info.vino.ino);
  4092. rec.v2.flock_len = (__force __le32)
  4093. ((ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) ? 0 : 1);
  4094. } else {
  4095. struct timespec64 ts;
  4096. rec.v1.cap_id = cpu_to_le64(cap->cap_id);
  4097. rec.v1.wanted = cpu_to_le32(__ceph_caps_wanted(ci));
  4098. rec.v1.issued = cpu_to_le32(cap->issued);
  4099. rec.v1.size = cpu_to_le64(i_size_read(inode));
  4100. ts = inode_get_mtime(inode);
  4101. ceph_encode_timespec64(&rec.v1.mtime, &ts);
  4102. ts = inode_get_atime(inode);
  4103. ceph_encode_timespec64(&rec.v1.atime, &ts);
  4104. rec.v1.snaprealm = cpu_to_le64(ci->i_snap_realm->ino);
  4105. rec.v1.pathbase = cpu_to_le64(path_info.vino.ino);
  4106. }
  4107. if (list_empty(&ci->i_cap_snaps)) {
  4108. snap_follows = ci->i_head_snapc ? ci->i_head_snapc->seq : 0;
  4109. } else {
  4110. struct ceph_cap_snap *capsnap =
  4111. list_first_entry(&ci->i_cap_snaps,
  4112. struct ceph_cap_snap, ci_item);
  4113. snap_follows = capsnap->follows;
  4114. }
  4115. spin_unlock(&ci->i_ceph_lock);
  4116. if (recon_state->msg_version >= 2) {
  4117. int num_fcntl_locks, num_flock_locks;
  4118. struct ceph_filelock *flocks = NULL;
  4119. size_t struct_len, total_len = sizeof(u64);
  4120. u8 struct_v = 0;
  4121. encode_again:
  4122. if (rec.v2.flock_len) {
  4123. ceph_count_locks(inode, &num_fcntl_locks, &num_flock_locks);
  4124. } else {
  4125. num_fcntl_locks = 0;
  4126. num_flock_locks = 0;
  4127. }
  4128. if (num_fcntl_locks + num_flock_locks > 0) {
  4129. flocks = kmalloc_objs(struct ceph_filelock,
  4130. num_fcntl_locks + num_flock_locks,
  4131. GFP_NOFS);
  4132. if (!flocks) {
  4133. err = -ENOMEM;
  4134. goto out_err;
  4135. }
  4136. err = ceph_encode_locks_to_buffer(inode, flocks,
  4137. num_fcntl_locks,
  4138. num_flock_locks);
  4139. if (err) {
  4140. kfree(flocks);
  4141. flocks = NULL;
  4142. if (err == -ENOSPC)
  4143. goto encode_again;
  4144. goto out_err;
  4145. }
  4146. } else {
  4147. kfree(flocks);
  4148. flocks = NULL;
  4149. }
  4150. if (recon_state->msg_version >= 3) {
  4151. /* version, compat_version and struct_len */
  4152. total_len += 2 * sizeof(u8) + sizeof(u32);
  4153. struct_v = 2;
  4154. }
  4155. /*
  4156. * number of encoded locks is stable, so copy to pagelist
  4157. */
  4158. struct_len = 2 * sizeof(u32) +
  4159. (num_fcntl_locks + num_flock_locks) *
  4160. sizeof(struct ceph_filelock);
  4161. rec.v2.flock_len = cpu_to_le32(struct_len);
  4162. struct_len += sizeof(u32) + path_info.pathlen + sizeof(rec.v2);
  4163. if (struct_v >= 2)
  4164. struct_len += sizeof(u64); /* snap_follows */
  4165. total_len += struct_len;
  4166. if (pagelist->length + total_len > RECONNECT_MAX_SIZE) {
  4167. err = send_reconnect_partial(recon_state);
  4168. if (err)
  4169. goto out_freeflocks;
  4170. pagelist = recon_state->pagelist;
  4171. }
  4172. err = ceph_pagelist_reserve(pagelist, total_len);
  4173. if (err)
  4174. goto out_freeflocks;
  4175. ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
  4176. if (recon_state->msg_version >= 3) {
  4177. ceph_pagelist_encode_8(pagelist, struct_v);
  4178. ceph_pagelist_encode_8(pagelist, 1);
  4179. ceph_pagelist_encode_32(pagelist, struct_len);
  4180. }
  4181. ceph_pagelist_encode_string(pagelist, (char *)path_info.path, path_info.pathlen);
  4182. ceph_pagelist_append(pagelist, &rec, sizeof(rec.v2));
  4183. ceph_locks_to_pagelist(flocks, pagelist,
  4184. num_fcntl_locks, num_flock_locks);
  4185. if (struct_v >= 2)
  4186. ceph_pagelist_encode_64(pagelist, snap_follows);
  4187. out_freeflocks:
  4188. kfree(flocks);
  4189. } else {
  4190. err = ceph_pagelist_reserve(pagelist,
  4191. sizeof(u64) + sizeof(u32) +
  4192. path_info.pathlen + sizeof(rec.v1));
  4193. if (err)
  4194. goto out_err;
  4195. ceph_pagelist_encode_64(pagelist, ceph_ino(inode));
  4196. ceph_pagelist_encode_string(pagelist, (char *)path_info.path, path_info.pathlen);
  4197. ceph_pagelist_append(pagelist, &rec, sizeof(rec.v1));
  4198. }
  4199. out_err:
  4200. ceph_mdsc_free_path_info(&path_info);
  4201. if (!err)
  4202. recon_state->nr_caps++;
  4203. return err;
  4204. }
  4205. static int encode_snap_realms(struct ceph_mds_client *mdsc,
  4206. struct ceph_reconnect_state *recon_state)
  4207. {
  4208. struct rb_node *p;
  4209. struct ceph_pagelist *pagelist = recon_state->pagelist;
  4210. struct ceph_client *cl = mdsc->fsc->client;
  4211. int err = 0;
  4212. if (recon_state->msg_version >= 4) {
  4213. err = ceph_pagelist_encode_32(pagelist, mdsc->num_snap_realms);
  4214. if (err < 0)
  4215. goto fail;
  4216. }
  4217. /*
  4218. * snaprealms. we provide mds with the ino, seq (version), and
  4219. * parent for all of our realms. If the mds has any newer info,
  4220. * it will tell us.
  4221. */
  4222. for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) {
  4223. struct ceph_snap_realm *realm =
  4224. rb_entry(p, struct ceph_snap_realm, node);
  4225. struct ceph_mds_snaprealm_reconnect sr_rec;
  4226. if (recon_state->msg_version >= 4) {
  4227. size_t need = sizeof(u8) * 2 + sizeof(u32) +
  4228. sizeof(sr_rec);
  4229. if (pagelist->length + need > RECONNECT_MAX_SIZE) {
  4230. err = send_reconnect_partial(recon_state);
  4231. if (err)
  4232. goto fail;
  4233. pagelist = recon_state->pagelist;
  4234. }
  4235. err = ceph_pagelist_reserve(pagelist, need);
  4236. if (err)
  4237. goto fail;
  4238. ceph_pagelist_encode_8(pagelist, 1);
  4239. ceph_pagelist_encode_8(pagelist, 1);
  4240. ceph_pagelist_encode_32(pagelist, sizeof(sr_rec));
  4241. }
  4242. doutc(cl, " adding snap realm %llx seq %lld parent %llx\n",
  4243. realm->ino, realm->seq, realm->parent_ino);
  4244. sr_rec.ino = cpu_to_le64(realm->ino);
  4245. sr_rec.seq = cpu_to_le64(realm->seq);
  4246. sr_rec.parent = cpu_to_le64(realm->parent_ino);
  4247. err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec));
  4248. if (err)
  4249. goto fail;
  4250. recon_state->nr_realms++;
  4251. }
  4252. fail:
  4253. return err;
  4254. }
  4255. /*
  4256. * If an MDS fails and recovers, clients need to reconnect in order to
  4257. * reestablish shared state. This includes all caps issued through
  4258. * this session _and_ the snap_realm hierarchy. Because it's not
  4259. * clear which snap realms the mds cares about, we send everything we
  4260. * know about.. that ensures we'll then get any new info the
  4261. * recovering MDS might have.
  4262. *
  4263. * This is a relatively heavyweight operation, but it's rare.
  4264. */
  4265. static void send_mds_reconnect(struct ceph_mds_client *mdsc,
  4266. struct ceph_mds_session *session)
  4267. {
  4268. struct ceph_client *cl = mdsc->fsc->client;
  4269. struct ceph_msg *reply;
  4270. int mds = session->s_mds;
  4271. int err = -ENOMEM;
  4272. struct ceph_reconnect_state recon_state = {
  4273. .session = session,
  4274. };
  4275. LIST_HEAD(dispose);
  4276. pr_info_client(cl, "mds%d reconnect start\n", mds);
  4277. recon_state.pagelist = ceph_pagelist_alloc(GFP_NOFS);
  4278. if (!recon_state.pagelist)
  4279. goto fail_nopagelist;
  4280. reply = ceph_msg_new2(CEPH_MSG_CLIENT_RECONNECT, 0, 1, GFP_NOFS, false);
  4281. if (!reply)
  4282. goto fail_nomsg;
  4283. xa_destroy(&session->s_delegated_inos);
  4284. mutex_lock(&session->s_mutex);
  4285. session->s_state = CEPH_MDS_SESSION_RECONNECTING;
  4286. session->s_seq = 0;
  4287. doutc(cl, "session %p state %s\n", session,
  4288. ceph_session_state_name(session->s_state));
  4289. atomic_inc(&session->s_cap_gen);
  4290. spin_lock(&session->s_cap_lock);
  4291. /* don't know if session is readonly */
  4292. session->s_readonly = 0;
  4293. /*
  4294. * notify __ceph_remove_cap() that we are composing cap reconnect.
  4295. * If a cap get released before being added to the cap reconnect,
  4296. * __ceph_remove_cap() should skip queuing cap release.
  4297. */
  4298. session->s_cap_reconnect = 1;
  4299. /* drop old cap expires; we're about to reestablish that state */
  4300. detach_cap_releases(session, &dispose);
  4301. spin_unlock(&session->s_cap_lock);
  4302. dispose_cap_releases(mdsc, &dispose);
  4303. /* trim unused caps to reduce MDS's cache rejoin time */
  4304. if (mdsc->fsc->sb->s_root)
  4305. shrink_dcache_parent(mdsc->fsc->sb->s_root);
  4306. ceph_con_close(&session->s_con);
  4307. ceph_con_open(&session->s_con,
  4308. CEPH_ENTITY_TYPE_MDS, mds,
  4309. ceph_mdsmap_get_addr(mdsc->mdsmap, mds));
  4310. /* replay unsafe requests */
  4311. replay_unsafe_requests(mdsc, session);
  4312. ceph_early_kick_flushing_caps(mdsc, session);
  4313. down_read(&mdsc->snap_rwsem);
  4314. /* placeholder for nr_caps */
  4315. err = ceph_pagelist_encode_32(recon_state.pagelist, 0);
  4316. if (err)
  4317. goto fail;
  4318. if (test_bit(CEPHFS_FEATURE_MULTI_RECONNECT, &session->s_features)) {
  4319. recon_state.msg_version = 3;
  4320. recon_state.allow_multi = true;
  4321. } else if (session->s_con.peer_features & CEPH_FEATURE_MDSENC) {
  4322. recon_state.msg_version = 3;
  4323. } else {
  4324. recon_state.msg_version = 2;
  4325. }
  4326. /* traverse this session's caps */
  4327. err = ceph_iterate_session_caps(session, reconnect_caps_cb, &recon_state);
  4328. spin_lock(&session->s_cap_lock);
  4329. session->s_cap_reconnect = 0;
  4330. spin_unlock(&session->s_cap_lock);
  4331. if (err < 0)
  4332. goto fail;
  4333. /* check if all realms can be encoded into current message */
  4334. if (mdsc->num_snap_realms) {
  4335. size_t total_len =
  4336. recon_state.pagelist->length +
  4337. mdsc->num_snap_realms *
  4338. sizeof(struct ceph_mds_snaprealm_reconnect);
  4339. if (recon_state.msg_version >= 4) {
  4340. /* number of realms */
  4341. total_len += sizeof(u32);
  4342. /* version, compat_version and struct_len */
  4343. total_len += mdsc->num_snap_realms *
  4344. (2 * sizeof(u8) + sizeof(u32));
  4345. }
  4346. if (total_len > RECONNECT_MAX_SIZE) {
  4347. if (!recon_state.allow_multi) {
  4348. err = -ENOSPC;
  4349. goto fail;
  4350. }
  4351. if (recon_state.nr_caps) {
  4352. err = send_reconnect_partial(&recon_state);
  4353. if (err)
  4354. goto fail;
  4355. }
  4356. recon_state.msg_version = 5;
  4357. }
  4358. }
  4359. err = encode_snap_realms(mdsc, &recon_state);
  4360. if (err < 0)
  4361. goto fail;
  4362. if (recon_state.msg_version >= 5) {
  4363. err = ceph_pagelist_encode_8(recon_state.pagelist, 0);
  4364. if (err < 0)
  4365. goto fail;
  4366. }
  4367. if (recon_state.nr_caps || recon_state.nr_realms) {
  4368. struct page *page =
  4369. list_first_entry(&recon_state.pagelist->head,
  4370. struct page, lru);
  4371. __le32 *addr = kmap_atomic(page);
  4372. if (recon_state.nr_caps) {
  4373. WARN_ON(recon_state.nr_realms != mdsc->num_snap_realms);
  4374. *addr = cpu_to_le32(recon_state.nr_caps);
  4375. } else if (recon_state.msg_version >= 4) {
  4376. *(addr + 1) = cpu_to_le32(recon_state.nr_realms);
  4377. }
  4378. kunmap_atomic(addr);
  4379. }
  4380. reply->hdr.version = cpu_to_le16(recon_state.msg_version);
  4381. if (recon_state.msg_version >= 4)
  4382. reply->hdr.compat_version = cpu_to_le16(4);
  4383. reply->hdr.data_len = cpu_to_le32(recon_state.pagelist->length);
  4384. ceph_msg_data_add_pagelist(reply, recon_state.pagelist);
  4385. ceph_con_send(&session->s_con, reply);
  4386. mutex_unlock(&session->s_mutex);
  4387. mutex_lock(&mdsc->mutex);
  4388. __wake_requests(mdsc, &session->s_waiting);
  4389. mutex_unlock(&mdsc->mutex);
  4390. up_read(&mdsc->snap_rwsem);
  4391. ceph_pagelist_release(recon_state.pagelist);
  4392. return;
  4393. fail:
  4394. ceph_msg_put(reply);
  4395. up_read(&mdsc->snap_rwsem);
  4396. mutex_unlock(&session->s_mutex);
  4397. fail_nomsg:
  4398. ceph_pagelist_release(recon_state.pagelist);
  4399. fail_nopagelist:
  4400. pr_err_client(cl, "error %d preparing reconnect for mds%d\n",
  4401. err, mds);
  4402. return;
  4403. }
  4404. /*
  4405. * compare old and new mdsmaps, kicking requests
  4406. * and closing out old connections as necessary
  4407. *
  4408. * called under mdsc->mutex.
  4409. */
  4410. static void check_new_map(struct ceph_mds_client *mdsc,
  4411. struct ceph_mdsmap *newmap,
  4412. struct ceph_mdsmap *oldmap)
  4413. {
  4414. int i, j, err;
  4415. int oldstate, newstate;
  4416. struct ceph_mds_session *s;
  4417. unsigned long targets[DIV_ROUND_UP(CEPH_MAX_MDS, sizeof(unsigned long))] = {0};
  4418. struct ceph_client *cl = mdsc->fsc->client;
  4419. doutc(cl, "new %u old %u\n", newmap->m_epoch, oldmap->m_epoch);
  4420. if (newmap->m_info) {
  4421. for (i = 0; i < newmap->possible_max_rank; i++) {
  4422. for (j = 0; j < newmap->m_info[i].num_export_targets; j++)
  4423. set_bit(newmap->m_info[i].export_targets[j], targets);
  4424. }
  4425. }
  4426. for (i = 0; i < oldmap->possible_max_rank && i < mdsc->max_sessions; i++) {
  4427. if (!mdsc->sessions[i])
  4428. continue;
  4429. s = mdsc->sessions[i];
  4430. oldstate = ceph_mdsmap_get_state(oldmap, i);
  4431. newstate = ceph_mdsmap_get_state(newmap, i);
  4432. doutc(cl, "mds%d state %s%s -> %s%s (session %s)\n",
  4433. i, ceph_mds_state_name(oldstate),
  4434. ceph_mdsmap_is_laggy(oldmap, i) ? " (laggy)" : "",
  4435. ceph_mds_state_name(newstate),
  4436. ceph_mdsmap_is_laggy(newmap, i) ? " (laggy)" : "",
  4437. ceph_session_state_name(s->s_state));
  4438. if (i >= newmap->possible_max_rank) {
  4439. /* force close session for stopped mds */
  4440. ceph_get_mds_session(s);
  4441. __unregister_session(mdsc, s);
  4442. __wake_requests(mdsc, &s->s_waiting);
  4443. mutex_unlock(&mdsc->mutex);
  4444. mutex_lock(&s->s_mutex);
  4445. cleanup_session_requests(mdsc, s);
  4446. remove_session_caps(s);
  4447. mutex_unlock(&s->s_mutex);
  4448. ceph_put_mds_session(s);
  4449. mutex_lock(&mdsc->mutex);
  4450. kick_requests(mdsc, i);
  4451. continue;
  4452. }
  4453. if (memcmp(ceph_mdsmap_get_addr(oldmap, i),
  4454. ceph_mdsmap_get_addr(newmap, i),
  4455. sizeof(struct ceph_entity_addr))) {
  4456. /* just close it */
  4457. mutex_unlock(&mdsc->mutex);
  4458. mutex_lock(&s->s_mutex);
  4459. mutex_lock(&mdsc->mutex);
  4460. ceph_con_close(&s->s_con);
  4461. mutex_unlock(&s->s_mutex);
  4462. s->s_state = CEPH_MDS_SESSION_RESTARTING;
  4463. } else if (oldstate == newstate) {
  4464. continue; /* nothing new with this mds */
  4465. }
  4466. /*
  4467. * send reconnect?
  4468. */
  4469. if (s->s_state == CEPH_MDS_SESSION_RESTARTING &&
  4470. newstate >= CEPH_MDS_STATE_RECONNECT) {
  4471. mutex_unlock(&mdsc->mutex);
  4472. clear_bit(i, targets);
  4473. send_mds_reconnect(mdsc, s);
  4474. mutex_lock(&mdsc->mutex);
  4475. }
  4476. /*
  4477. * kick request on any mds that has gone active.
  4478. */
  4479. if (oldstate < CEPH_MDS_STATE_ACTIVE &&
  4480. newstate >= CEPH_MDS_STATE_ACTIVE) {
  4481. if (oldstate != CEPH_MDS_STATE_CREATING &&
  4482. oldstate != CEPH_MDS_STATE_STARTING)
  4483. pr_info_client(cl, "mds%d recovery completed\n",
  4484. s->s_mds);
  4485. kick_requests(mdsc, i);
  4486. mutex_unlock(&mdsc->mutex);
  4487. mutex_lock(&s->s_mutex);
  4488. mutex_lock(&mdsc->mutex);
  4489. ceph_kick_flushing_caps(mdsc, s);
  4490. mutex_unlock(&s->s_mutex);
  4491. wake_up_session_caps(s, RECONNECT);
  4492. }
  4493. }
  4494. /*
  4495. * Only open and reconnect sessions that don't exist yet.
  4496. */
  4497. for (i = 0; i < newmap->possible_max_rank; i++) {
  4498. /*
  4499. * In case the import MDS is crashed just after
  4500. * the EImportStart journal is flushed, so when
  4501. * a standby MDS takes over it and is replaying
  4502. * the EImportStart journal the new MDS daemon
  4503. * will wait the client to reconnect it, but the
  4504. * client may never register/open the session yet.
  4505. *
  4506. * Will try to reconnect that MDS daemon if the
  4507. * rank number is in the export targets array and
  4508. * is the up:reconnect state.
  4509. */
  4510. newstate = ceph_mdsmap_get_state(newmap, i);
  4511. if (!test_bit(i, targets) || newstate != CEPH_MDS_STATE_RECONNECT)
  4512. continue;
  4513. /*
  4514. * The session maybe registered and opened by some
  4515. * requests which were choosing random MDSes during
  4516. * the mdsc->mutex's unlock/lock gap below in rare
  4517. * case. But the related MDS daemon will just queue
  4518. * that requests and be still waiting for the client's
  4519. * reconnection request in up:reconnect state.
  4520. */
  4521. s = __ceph_lookup_mds_session(mdsc, i);
  4522. if (likely(!s)) {
  4523. s = __open_export_target_session(mdsc, i);
  4524. if (IS_ERR(s)) {
  4525. err = PTR_ERR(s);
  4526. pr_err_client(cl,
  4527. "failed to open export target session, err %d\n",
  4528. err);
  4529. continue;
  4530. }
  4531. }
  4532. doutc(cl, "send reconnect to export target mds.%d\n", i);
  4533. mutex_unlock(&mdsc->mutex);
  4534. send_mds_reconnect(mdsc, s);
  4535. ceph_put_mds_session(s);
  4536. mutex_lock(&mdsc->mutex);
  4537. }
  4538. for (i = 0; i < newmap->possible_max_rank && i < mdsc->max_sessions; i++) {
  4539. s = mdsc->sessions[i];
  4540. if (!s)
  4541. continue;
  4542. if (!ceph_mdsmap_is_laggy(newmap, i))
  4543. continue;
  4544. if (s->s_state == CEPH_MDS_SESSION_OPEN ||
  4545. s->s_state == CEPH_MDS_SESSION_HUNG ||
  4546. s->s_state == CEPH_MDS_SESSION_CLOSING) {
  4547. doutc(cl, " connecting to export targets of laggy mds%d\n", i);
  4548. __open_export_target_sessions(mdsc, s);
  4549. }
  4550. }
  4551. }
  4552. /*
  4553. * leases
  4554. */
  4555. /*
  4556. * caller must hold session s_mutex, dentry->d_lock
  4557. */
  4558. void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry)
  4559. {
  4560. struct ceph_dentry_info *di = ceph_dentry(dentry);
  4561. ceph_put_mds_session(di->lease_session);
  4562. di->lease_session = NULL;
  4563. }
  4564. static void handle_lease(struct ceph_mds_client *mdsc,
  4565. struct ceph_mds_session *session,
  4566. struct ceph_msg *msg)
  4567. {
  4568. struct ceph_client *cl = mdsc->fsc->client;
  4569. struct super_block *sb = mdsc->fsc->sb;
  4570. struct inode *inode;
  4571. struct dentry *parent, *dentry;
  4572. struct ceph_dentry_info *di;
  4573. int mds = session->s_mds;
  4574. struct ceph_mds_lease *h = msg->front.iov_base;
  4575. u32 seq;
  4576. struct ceph_vino vino;
  4577. struct qstr dname;
  4578. int release = 0;
  4579. doutc(cl, "from mds%d\n", mds);
  4580. if (!ceph_inc_mds_stopping_blocker(mdsc, session))
  4581. return;
  4582. /* decode */
  4583. if (msg->front.iov_len < sizeof(*h) + sizeof(u32))
  4584. goto bad;
  4585. vino.ino = le64_to_cpu(h->ino);
  4586. vino.snap = CEPH_NOSNAP;
  4587. seq = le32_to_cpu(h->seq);
  4588. dname.len = get_unaligned_le32(h + 1);
  4589. if (msg->front.iov_len < sizeof(*h) + sizeof(u32) + dname.len)
  4590. goto bad;
  4591. dname.name = (void *)(h + 1) + sizeof(u32);
  4592. /* lookup inode */
  4593. inode = ceph_find_inode(sb, vino);
  4594. doutc(cl, "%s, ino %llx %p %.*s\n", ceph_lease_op_name(h->action),
  4595. vino.ino, inode, dname.len, dname.name);
  4596. mutex_lock(&session->s_mutex);
  4597. if (!inode) {
  4598. doutc(cl, "no inode %llx\n", vino.ino);
  4599. goto release;
  4600. }
  4601. /* dentry */
  4602. parent = d_find_alias(inode);
  4603. if (!parent) {
  4604. doutc(cl, "no parent dentry on inode %p\n", inode);
  4605. WARN_ON(1);
  4606. goto release; /* hrm... */
  4607. }
  4608. dname.hash = full_name_hash(parent, dname.name, dname.len);
  4609. dentry = d_lookup(parent, &dname);
  4610. dput(parent);
  4611. if (!dentry)
  4612. goto release;
  4613. spin_lock(&dentry->d_lock);
  4614. di = ceph_dentry(dentry);
  4615. switch (h->action) {
  4616. case CEPH_MDS_LEASE_REVOKE:
  4617. if (di->lease_session == session) {
  4618. if (ceph_seq_cmp(di->lease_seq, seq) > 0)
  4619. h->seq = cpu_to_le32(di->lease_seq);
  4620. __ceph_mdsc_drop_dentry_lease(dentry);
  4621. }
  4622. release = 1;
  4623. break;
  4624. case CEPH_MDS_LEASE_RENEW:
  4625. if (di->lease_session == session &&
  4626. di->lease_gen == atomic_read(&session->s_cap_gen) &&
  4627. di->lease_renew_from &&
  4628. di->lease_renew_after == 0) {
  4629. unsigned long duration =
  4630. msecs_to_jiffies(le32_to_cpu(h->duration_ms));
  4631. di->lease_seq = seq;
  4632. di->time = di->lease_renew_from + duration;
  4633. di->lease_renew_after = di->lease_renew_from +
  4634. (duration >> 1);
  4635. di->lease_renew_from = 0;
  4636. }
  4637. break;
  4638. }
  4639. spin_unlock(&dentry->d_lock);
  4640. dput(dentry);
  4641. if (!release)
  4642. goto out;
  4643. release:
  4644. /* let's just reuse the same message */
  4645. h->action = CEPH_MDS_LEASE_REVOKE_ACK;
  4646. ceph_msg_get(msg);
  4647. ceph_con_send(&session->s_con, msg);
  4648. out:
  4649. mutex_unlock(&session->s_mutex);
  4650. iput(inode);
  4651. ceph_dec_mds_stopping_blocker(mdsc);
  4652. return;
  4653. bad:
  4654. ceph_dec_mds_stopping_blocker(mdsc);
  4655. pr_err_client(cl, "corrupt lease message\n");
  4656. ceph_msg_dump(msg);
  4657. }
  4658. void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session,
  4659. struct dentry *dentry, char action,
  4660. u32 seq)
  4661. {
  4662. struct ceph_client *cl = session->s_mdsc->fsc->client;
  4663. struct ceph_msg *msg;
  4664. struct ceph_mds_lease *lease;
  4665. struct inode *dir;
  4666. int len = sizeof(*lease) + sizeof(u32) + NAME_MAX;
  4667. doutc(cl, "identry %p %s to mds%d\n", dentry, ceph_lease_op_name(action),
  4668. session->s_mds);
  4669. msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, GFP_NOFS, false);
  4670. if (!msg)
  4671. return;
  4672. lease = msg->front.iov_base;
  4673. lease->action = action;
  4674. lease->seq = cpu_to_le32(seq);
  4675. spin_lock(&dentry->d_lock);
  4676. dir = d_inode(dentry->d_parent);
  4677. lease->ino = cpu_to_le64(ceph_ino(dir));
  4678. lease->first = lease->last = cpu_to_le64(ceph_snap(dir));
  4679. put_unaligned_le32(dentry->d_name.len, lease + 1);
  4680. memcpy((void *)(lease + 1) + 4,
  4681. dentry->d_name.name, dentry->d_name.len);
  4682. spin_unlock(&dentry->d_lock);
  4683. ceph_con_send(&session->s_con, msg);
  4684. }
  4685. /*
  4686. * lock unlock the session, to wait ongoing session activities
  4687. */
  4688. static void lock_unlock_session(struct ceph_mds_session *s)
  4689. {
  4690. mutex_lock(&s->s_mutex);
  4691. mutex_unlock(&s->s_mutex);
  4692. }
  4693. static void maybe_recover_session(struct ceph_mds_client *mdsc)
  4694. {
  4695. struct ceph_client *cl = mdsc->fsc->client;
  4696. struct ceph_fs_client *fsc = mdsc->fsc;
  4697. if (!ceph_test_mount_opt(fsc, CLEANRECOVER))
  4698. return;
  4699. if (READ_ONCE(fsc->mount_state) != CEPH_MOUNT_MOUNTED)
  4700. return;
  4701. if (!READ_ONCE(fsc->blocklisted))
  4702. return;
  4703. pr_info_client(cl, "auto reconnect after blocklisted\n");
  4704. ceph_force_reconnect(fsc->sb);
  4705. }
  4706. bool check_session_state(struct ceph_mds_session *s)
  4707. {
  4708. struct ceph_client *cl = s->s_mdsc->fsc->client;
  4709. switch (s->s_state) {
  4710. case CEPH_MDS_SESSION_OPEN:
  4711. if (s->s_ttl && time_after(jiffies, s->s_ttl)) {
  4712. s->s_state = CEPH_MDS_SESSION_HUNG;
  4713. pr_info_client(cl, "mds%d hung\n", s->s_mds);
  4714. }
  4715. break;
  4716. case CEPH_MDS_SESSION_CLOSING:
  4717. case CEPH_MDS_SESSION_NEW:
  4718. case CEPH_MDS_SESSION_RESTARTING:
  4719. case CEPH_MDS_SESSION_CLOSED:
  4720. case CEPH_MDS_SESSION_REJECTED:
  4721. return false;
  4722. }
  4723. return true;
  4724. }
  4725. /*
  4726. * If the sequence is incremented while we're waiting on a REQUEST_CLOSE reply,
  4727. * then we need to retransmit that request.
  4728. */
  4729. void inc_session_sequence(struct ceph_mds_session *s)
  4730. {
  4731. struct ceph_client *cl = s->s_mdsc->fsc->client;
  4732. lockdep_assert_held(&s->s_mutex);
  4733. s->s_seq++;
  4734. if (s->s_state == CEPH_MDS_SESSION_CLOSING) {
  4735. int ret;
  4736. doutc(cl, "resending session close request for mds%d\n", s->s_mds);
  4737. ret = request_close_session(s);
  4738. if (ret < 0)
  4739. pr_err_client(cl, "unable to close session to mds%d: %d\n",
  4740. s->s_mds, ret);
  4741. }
  4742. }
  4743. /*
  4744. * delayed work -- periodically trim expired leases, renew caps with mds. If
  4745. * the @delay parameter is set to 0 or if it's more than 5 secs, the default
  4746. * workqueue delay value of 5 secs will be used.
  4747. */
  4748. static void schedule_delayed(struct ceph_mds_client *mdsc, unsigned long delay)
  4749. {
  4750. unsigned long max_delay = HZ * 5;
  4751. /* 5 secs default delay */
  4752. if (!delay || (delay > max_delay))
  4753. delay = max_delay;
  4754. schedule_delayed_work(&mdsc->delayed_work,
  4755. round_jiffies_relative(delay));
  4756. }
  4757. static void delayed_work(struct work_struct *work)
  4758. {
  4759. struct ceph_mds_client *mdsc =
  4760. container_of(work, struct ceph_mds_client, delayed_work.work);
  4761. unsigned long delay;
  4762. int renew_interval;
  4763. int renew_caps;
  4764. int i;
  4765. doutc(mdsc->fsc->client, "mdsc delayed_work\n");
  4766. if (mdsc->stopping >= CEPH_MDSC_STOPPING_FLUSHED)
  4767. return;
  4768. mutex_lock(&mdsc->mutex);
  4769. renew_interval = mdsc->mdsmap->m_session_timeout >> 2;
  4770. renew_caps = time_after_eq(jiffies, HZ*renew_interval +
  4771. mdsc->last_renew_caps);
  4772. if (renew_caps)
  4773. mdsc->last_renew_caps = jiffies;
  4774. for (i = 0; i < mdsc->max_sessions; i++) {
  4775. struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i);
  4776. if (!s)
  4777. continue;
  4778. if (!check_session_state(s)) {
  4779. ceph_put_mds_session(s);
  4780. continue;
  4781. }
  4782. mutex_unlock(&mdsc->mutex);
  4783. ceph_flush_session_cap_releases(mdsc, s);
  4784. mutex_lock(&s->s_mutex);
  4785. if (renew_caps)
  4786. send_renew_caps(mdsc, s);
  4787. else
  4788. ceph_con_keepalive(&s->s_con);
  4789. if (s->s_state == CEPH_MDS_SESSION_OPEN ||
  4790. s->s_state == CEPH_MDS_SESSION_HUNG)
  4791. ceph_send_cap_releases(mdsc, s);
  4792. mutex_unlock(&s->s_mutex);
  4793. ceph_put_mds_session(s);
  4794. mutex_lock(&mdsc->mutex);
  4795. }
  4796. mutex_unlock(&mdsc->mutex);
  4797. delay = ceph_check_delayed_caps(mdsc);
  4798. ceph_queue_cap_reclaim_work(mdsc);
  4799. ceph_trim_snapid_map(mdsc);
  4800. maybe_recover_session(mdsc);
  4801. schedule_delayed(mdsc, delay);
  4802. }
  4803. int ceph_mdsc_init(struct ceph_fs_client *fsc)
  4804. {
  4805. struct ceph_mds_client *mdsc;
  4806. int err;
  4807. mdsc = kzalloc_obj(struct ceph_mds_client, GFP_NOFS);
  4808. if (!mdsc)
  4809. return -ENOMEM;
  4810. mdsc->fsc = fsc;
  4811. mutex_init(&mdsc->mutex);
  4812. mdsc->mdsmap = kzalloc_obj(*mdsc->mdsmap, GFP_NOFS);
  4813. if (!mdsc->mdsmap) {
  4814. err = -ENOMEM;
  4815. goto err_mdsc;
  4816. }
  4817. init_completion(&mdsc->safe_umount_waiters);
  4818. spin_lock_init(&mdsc->stopping_lock);
  4819. atomic_set(&mdsc->stopping_blockers, 0);
  4820. init_completion(&mdsc->stopping_waiter);
  4821. atomic64_set(&mdsc->dirty_folios, 0);
  4822. init_waitqueue_head(&mdsc->flush_end_wq);
  4823. init_waitqueue_head(&mdsc->session_close_wq);
  4824. INIT_LIST_HEAD(&mdsc->waiting_for_map);
  4825. mdsc->quotarealms_inodes = RB_ROOT;
  4826. mutex_init(&mdsc->quotarealms_inodes_mutex);
  4827. init_rwsem(&mdsc->snap_rwsem);
  4828. mdsc->snap_realms = RB_ROOT;
  4829. INIT_LIST_HEAD(&mdsc->snap_empty);
  4830. spin_lock_init(&mdsc->snap_empty_lock);
  4831. mdsc->request_tree = RB_ROOT;
  4832. INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work);
  4833. mdsc->last_renew_caps = jiffies;
  4834. INIT_LIST_HEAD(&mdsc->cap_delay_list);
  4835. #ifdef CONFIG_DEBUG_FS
  4836. INIT_LIST_HEAD(&mdsc->cap_wait_list);
  4837. #endif
  4838. spin_lock_init(&mdsc->cap_delay_lock);
  4839. INIT_LIST_HEAD(&mdsc->cap_unlink_delay_list);
  4840. INIT_LIST_HEAD(&mdsc->snap_flush_list);
  4841. spin_lock_init(&mdsc->snap_flush_lock);
  4842. mdsc->last_cap_flush_tid = 1;
  4843. INIT_LIST_HEAD(&mdsc->cap_flush_list);
  4844. INIT_LIST_HEAD(&mdsc->cap_dirty_migrating);
  4845. spin_lock_init(&mdsc->cap_dirty_lock);
  4846. init_waitqueue_head(&mdsc->cap_flushing_wq);
  4847. INIT_WORK(&mdsc->cap_reclaim_work, ceph_cap_reclaim_work);
  4848. INIT_WORK(&mdsc->cap_unlink_work, ceph_cap_unlink_work);
  4849. err = ceph_metric_init(&mdsc->metric);
  4850. if (err)
  4851. goto err_mdsmap;
  4852. spin_lock_init(&mdsc->dentry_list_lock);
  4853. INIT_LIST_HEAD(&mdsc->dentry_leases);
  4854. INIT_LIST_HEAD(&mdsc->dentry_dir_leases);
  4855. ceph_caps_init(mdsc);
  4856. ceph_adjust_caps_max_min(mdsc, fsc->mount_options);
  4857. spin_lock_init(&mdsc->snapid_map_lock);
  4858. mdsc->snapid_map_tree = RB_ROOT;
  4859. INIT_LIST_HEAD(&mdsc->snapid_map_lru);
  4860. init_rwsem(&mdsc->pool_perm_rwsem);
  4861. mdsc->pool_perm_tree = RB_ROOT;
  4862. strscpy(mdsc->nodename, utsname()->nodename,
  4863. sizeof(mdsc->nodename));
  4864. fsc->mdsc = mdsc;
  4865. return 0;
  4866. err_mdsmap:
  4867. kfree(mdsc->mdsmap);
  4868. err_mdsc:
  4869. kfree(mdsc);
  4870. return err;
  4871. }
  4872. /*
  4873. * Wait for safe replies on open mds requests. If we time out, drop
  4874. * all requests from the tree to avoid dangling dentry refs.
  4875. */
  4876. static void wait_requests(struct ceph_mds_client *mdsc)
  4877. {
  4878. struct ceph_client *cl = mdsc->fsc->client;
  4879. struct ceph_options *opts = mdsc->fsc->client->options;
  4880. struct ceph_mds_request *req;
  4881. mutex_lock(&mdsc->mutex);
  4882. if (__get_oldest_req(mdsc)) {
  4883. mutex_unlock(&mdsc->mutex);
  4884. doutc(cl, "waiting for requests\n");
  4885. wait_for_completion_timeout(&mdsc->safe_umount_waiters,
  4886. ceph_timeout_jiffies(opts->mount_timeout));
  4887. /* tear down remaining requests */
  4888. mutex_lock(&mdsc->mutex);
  4889. while ((req = __get_oldest_req(mdsc))) {
  4890. doutc(cl, "timed out on tid %llu\n", req->r_tid);
  4891. list_del_init(&req->r_wait);
  4892. __unregister_request(mdsc, req);
  4893. }
  4894. }
  4895. mutex_unlock(&mdsc->mutex);
  4896. doutc(cl, "done\n");
  4897. }
  4898. void send_flush_mdlog(struct ceph_mds_session *s)
  4899. {
  4900. struct ceph_client *cl = s->s_mdsc->fsc->client;
  4901. struct ceph_msg *msg;
  4902. /*
  4903. * Pre-luminous MDS crashes when it sees an unknown session request
  4904. */
  4905. if (!CEPH_HAVE_FEATURE(s->s_con.peer_features, SERVER_LUMINOUS))
  4906. return;
  4907. mutex_lock(&s->s_mutex);
  4908. doutc(cl, "request mdlog flush to mds%d (%s)s seq %lld\n",
  4909. s->s_mds, ceph_session_state_name(s->s_state), s->s_seq);
  4910. msg = ceph_create_session_msg(CEPH_SESSION_REQUEST_FLUSH_MDLOG,
  4911. s->s_seq);
  4912. if (!msg) {
  4913. pr_err_client(cl, "failed to request mdlog flush to mds%d (%s) seq %lld\n",
  4914. s->s_mds, ceph_session_state_name(s->s_state), s->s_seq);
  4915. } else {
  4916. ceph_con_send(&s->s_con, msg);
  4917. }
  4918. mutex_unlock(&s->s_mutex);
  4919. }
  4920. static int ceph_mds_auth_match(struct ceph_mds_client *mdsc,
  4921. struct ceph_mds_cap_auth *auth,
  4922. const struct cred *cred,
  4923. char *tpath)
  4924. {
  4925. u32 caller_uid = from_kuid(&init_user_ns, cred->fsuid);
  4926. u32 caller_gid = from_kgid(&init_user_ns, cred->fsgid);
  4927. struct ceph_client *cl = mdsc->fsc->client;
  4928. const char *fs_name = mdsc->mdsmap->m_fs_name;
  4929. const char *spath = mdsc->fsc->mount_options->server_path;
  4930. bool gid_matched = false;
  4931. u32 gid, tlen, len;
  4932. int i, j;
  4933. doutc(cl, "fsname check fs_name=%s match.fs_name=%s\n",
  4934. fs_name, auth->match.fs_name ? auth->match.fs_name : "");
  4935. if (!ceph_namespace_match(auth->match.fs_name, fs_name)) {
  4936. /* fsname mismatch, try next one */
  4937. return 0;
  4938. }
  4939. doutc(cl, "match.uid %lld\n", auth->match.uid);
  4940. if (auth->match.uid != MDS_AUTH_UID_ANY) {
  4941. if (auth->match.uid != caller_uid)
  4942. return 0;
  4943. if (auth->match.num_gids) {
  4944. for (i = 0; i < auth->match.num_gids; i++) {
  4945. if (caller_gid == auth->match.gids[i])
  4946. gid_matched = true;
  4947. }
  4948. if (!gid_matched && cred->group_info->ngroups) {
  4949. for (i = 0; i < cred->group_info->ngroups; i++) {
  4950. gid = from_kgid(&init_user_ns,
  4951. cred->group_info->gid[i]);
  4952. for (j = 0; j < auth->match.num_gids; j++) {
  4953. if (gid == auth->match.gids[j]) {
  4954. gid_matched = true;
  4955. break;
  4956. }
  4957. }
  4958. if (gid_matched)
  4959. break;
  4960. }
  4961. }
  4962. if (!gid_matched)
  4963. return 0;
  4964. }
  4965. }
  4966. /* path match */
  4967. if (auth->match.path) {
  4968. if (!tpath)
  4969. return 0;
  4970. tlen = strlen(tpath);
  4971. len = strlen(auth->match.path);
  4972. if (len) {
  4973. char *_tpath = tpath;
  4974. bool free_tpath = false;
  4975. int m, n;
  4976. doutc(cl, "server path %s, tpath %s, match.path %s\n",
  4977. spath, tpath, auth->match.path);
  4978. if (spath && (m = strlen(spath)) != 1) {
  4979. /* mount path + '/' + tpath + an extra space */
  4980. n = m + 1 + tlen + 1;
  4981. _tpath = kmalloc(n, GFP_NOFS);
  4982. if (!_tpath)
  4983. return -ENOMEM;
  4984. /* remove the leading '/' */
  4985. snprintf(_tpath, n, "%s/%s", spath + 1, tpath);
  4986. free_tpath = true;
  4987. tlen = strlen(_tpath);
  4988. }
  4989. /*
  4990. * Please note the tailing '/' for match.path has already
  4991. * been removed when parsing.
  4992. *
  4993. * Remove the tailing '/' for the target path.
  4994. */
  4995. while (tlen && _tpath[tlen - 1] == '/') {
  4996. _tpath[tlen - 1] = '\0';
  4997. tlen -= 1;
  4998. }
  4999. doutc(cl, "_tpath %s\n", _tpath);
  5000. /*
  5001. * In case first == _tpath && tlen == len:
  5002. * match.path=/foo --> /foo _path=/foo --> match
  5003. * match.path=/foo/ --> /foo _path=/foo --> match
  5004. *
  5005. * In case first == _tmatch.path && tlen > len:
  5006. * match.path=/foo/ --> /foo _path=/foo/ --> match
  5007. * match.path=/foo --> /foo _path=/foo/ --> match
  5008. * match.path=/foo/ --> /foo _path=/foo/d --> match
  5009. * match.path=/foo --> /foo _path=/food --> mismatch
  5010. *
  5011. * All the other cases --> mismatch
  5012. */
  5013. bool path_matched = true;
  5014. char *first = strstr(_tpath, auth->match.path);
  5015. if (first != _tpath ||
  5016. (tlen > len && _tpath[len] != '/')) {
  5017. path_matched = false;
  5018. }
  5019. if (free_tpath)
  5020. kfree(_tpath);
  5021. if (!path_matched)
  5022. return 0;
  5023. }
  5024. }
  5025. doutc(cl, "matched\n");
  5026. return 1;
  5027. }
  5028. int ceph_mds_check_access(struct ceph_mds_client *mdsc, char *tpath, int mask)
  5029. {
  5030. const struct cred *cred = get_current_cred();
  5031. u32 caller_uid = from_kuid(&init_user_ns, cred->fsuid);
  5032. u32 caller_gid = from_kgid(&init_user_ns, cred->fsgid);
  5033. struct ceph_mds_cap_auth *rw_perms_s = NULL;
  5034. struct ceph_client *cl = mdsc->fsc->client;
  5035. bool root_squash_perms = true;
  5036. int i, err;
  5037. doutc(cl, "tpath '%s', mask %d, caller_uid %d, caller_gid %d\n",
  5038. tpath, mask, caller_uid, caller_gid);
  5039. for (i = 0; i < mdsc->s_cap_auths_num; i++) {
  5040. struct ceph_mds_cap_auth *s = &mdsc->s_cap_auths[i];
  5041. err = ceph_mds_auth_match(mdsc, s, cred, tpath);
  5042. if (err < 0) {
  5043. put_cred(cred);
  5044. return err;
  5045. } else if (err > 0) {
  5046. /* always follow the last auth caps' permission */
  5047. root_squash_perms = true;
  5048. rw_perms_s = NULL;
  5049. if ((mask & MAY_WRITE) && s->writeable &&
  5050. s->match.root_squash && (!caller_uid || !caller_gid))
  5051. root_squash_perms = false;
  5052. if (((mask & MAY_WRITE) && !s->writeable) ||
  5053. ((mask & MAY_READ) && !s->readable))
  5054. rw_perms_s = s;
  5055. }
  5056. }
  5057. put_cred(cred);
  5058. doutc(cl, "root_squash_perms %d, rw_perms_s %p\n", root_squash_perms,
  5059. rw_perms_s);
  5060. if (root_squash_perms && rw_perms_s == NULL) {
  5061. doutc(cl, "access allowed\n");
  5062. return 0;
  5063. }
  5064. if (!root_squash_perms) {
  5065. doutc(cl, "root_squash is enabled and user(%d %d) isn't allowed to write",
  5066. caller_uid, caller_gid);
  5067. }
  5068. if (rw_perms_s) {
  5069. doutc(cl, "mds auth caps readable/writeable %d/%d while request r/w %d/%d",
  5070. rw_perms_s->readable, rw_perms_s->writeable,
  5071. !!(mask & MAY_READ), !!(mask & MAY_WRITE));
  5072. }
  5073. doutc(cl, "access denied\n");
  5074. return -EACCES;
  5075. }
  5076. /*
  5077. * called before mount is ro, and before dentries are torn down.
  5078. * (hmm, does this still race with new lookups?)
  5079. */
  5080. void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc)
  5081. {
  5082. doutc(mdsc->fsc->client, "begin\n");
  5083. mdsc->stopping = CEPH_MDSC_STOPPING_BEGIN;
  5084. ceph_mdsc_iterate_sessions(mdsc, send_flush_mdlog, true);
  5085. ceph_mdsc_iterate_sessions(mdsc, lock_unlock_session, false);
  5086. ceph_flush_dirty_caps(mdsc);
  5087. wait_requests(mdsc);
  5088. /*
  5089. * wait for reply handlers to drop their request refs and
  5090. * their inode/dcache refs
  5091. */
  5092. ceph_msgr_flush();
  5093. ceph_cleanup_quotarealms_inodes(mdsc);
  5094. doutc(mdsc->fsc->client, "done\n");
  5095. }
  5096. /*
  5097. * flush the mdlog and wait for all write mds requests to flush.
  5098. */
  5099. static void flush_mdlog_and_wait_mdsc_unsafe_requests(struct ceph_mds_client *mdsc,
  5100. u64 want_tid)
  5101. {
  5102. struct ceph_client *cl = mdsc->fsc->client;
  5103. struct ceph_mds_request *req = NULL, *nextreq;
  5104. struct ceph_mds_session *last_session = NULL;
  5105. struct rb_node *n;
  5106. mutex_lock(&mdsc->mutex);
  5107. doutc(cl, "want %lld\n", want_tid);
  5108. restart:
  5109. req = __get_oldest_req(mdsc);
  5110. while (req && req->r_tid <= want_tid) {
  5111. /* find next request */
  5112. n = rb_next(&req->r_node);
  5113. if (n)
  5114. nextreq = rb_entry(n, struct ceph_mds_request, r_node);
  5115. else
  5116. nextreq = NULL;
  5117. if (req->r_op != CEPH_MDS_OP_SETFILELOCK &&
  5118. (req->r_op & CEPH_MDS_OP_WRITE)) {
  5119. struct ceph_mds_session *s = req->r_session;
  5120. if (!s) {
  5121. req = nextreq;
  5122. continue;
  5123. }
  5124. /* write op */
  5125. ceph_mdsc_get_request(req);
  5126. if (nextreq)
  5127. ceph_mdsc_get_request(nextreq);
  5128. s = ceph_get_mds_session(s);
  5129. mutex_unlock(&mdsc->mutex);
  5130. /* send flush mdlog request to MDS */
  5131. if (last_session != s) {
  5132. send_flush_mdlog(s);
  5133. ceph_put_mds_session(last_session);
  5134. last_session = s;
  5135. } else {
  5136. ceph_put_mds_session(s);
  5137. }
  5138. doutc(cl, "wait on %llu (want %llu)\n",
  5139. req->r_tid, want_tid);
  5140. wait_for_completion(&req->r_safe_completion);
  5141. mutex_lock(&mdsc->mutex);
  5142. ceph_mdsc_put_request(req);
  5143. if (!nextreq)
  5144. break; /* next dne before, so we're done! */
  5145. if (RB_EMPTY_NODE(&nextreq->r_node)) {
  5146. /* next request was removed from tree */
  5147. ceph_mdsc_put_request(nextreq);
  5148. goto restart;
  5149. }
  5150. ceph_mdsc_put_request(nextreq); /* won't go away */
  5151. }
  5152. req = nextreq;
  5153. }
  5154. mutex_unlock(&mdsc->mutex);
  5155. ceph_put_mds_session(last_session);
  5156. doutc(cl, "done\n");
  5157. }
  5158. void ceph_mdsc_sync(struct ceph_mds_client *mdsc)
  5159. {
  5160. struct ceph_client *cl = mdsc->fsc->client;
  5161. u64 want_tid, want_flush;
  5162. if (READ_ONCE(mdsc->fsc->mount_state) >= CEPH_MOUNT_SHUTDOWN)
  5163. return;
  5164. doutc(cl, "sync\n");
  5165. mutex_lock(&mdsc->mutex);
  5166. want_tid = mdsc->last_tid;
  5167. mutex_unlock(&mdsc->mutex);
  5168. ceph_flush_dirty_caps(mdsc);
  5169. ceph_flush_cap_releases(mdsc);
  5170. spin_lock(&mdsc->cap_dirty_lock);
  5171. want_flush = mdsc->last_cap_flush_tid;
  5172. if (!list_empty(&mdsc->cap_flush_list)) {
  5173. struct ceph_cap_flush *cf =
  5174. list_last_entry(&mdsc->cap_flush_list,
  5175. struct ceph_cap_flush, g_list);
  5176. cf->wake = true;
  5177. }
  5178. spin_unlock(&mdsc->cap_dirty_lock);
  5179. doutc(cl, "sync want tid %lld flush_seq %lld\n", want_tid, want_flush);
  5180. flush_mdlog_and_wait_mdsc_unsafe_requests(mdsc, want_tid);
  5181. wait_caps_flush(mdsc, want_flush);
  5182. }
  5183. /*
  5184. * true if all sessions are closed, or we force unmount
  5185. */
  5186. static bool done_closing_sessions(struct ceph_mds_client *mdsc, int skipped)
  5187. {
  5188. if (READ_ONCE(mdsc->fsc->mount_state) == CEPH_MOUNT_SHUTDOWN)
  5189. return true;
  5190. return atomic_read(&mdsc->num_sessions) <= skipped;
  5191. }
  5192. /*
  5193. * called after sb is ro or when metadata corrupted.
  5194. */
  5195. void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc)
  5196. {
  5197. struct ceph_options *opts = mdsc->fsc->client->options;
  5198. struct ceph_client *cl = mdsc->fsc->client;
  5199. struct ceph_mds_session *session;
  5200. int i;
  5201. int skipped = 0;
  5202. doutc(cl, "begin\n");
  5203. /* close sessions */
  5204. mutex_lock(&mdsc->mutex);
  5205. for (i = 0; i < mdsc->max_sessions; i++) {
  5206. session = __ceph_lookup_mds_session(mdsc, i);
  5207. if (!session)
  5208. continue;
  5209. mutex_unlock(&mdsc->mutex);
  5210. mutex_lock(&session->s_mutex);
  5211. if (__close_session(mdsc, session) <= 0)
  5212. skipped++;
  5213. mutex_unlock(&session->s_mutex);
  5214. ceph_put_mds_session(session);
  5215. mutex_lock(&mdsc->mutex);
  5216. }
  5217. mutex_unlock(&mdsc->mutex);
  5218. doutc(cl, "waiting for sessions to close\n");
  5219. wait_event_timeout(mdsc->session_close_wq,
  5220. done_closing_sessions(mdsc, skipped),
  5221. ceph_timeout_jiffies(opts->mount_timeout));
  5222. /* tear down remaining sessions */
  5223. mutex_lock(&mdsc->mutex);
  5224. for (i = 0; i < mdsc->max_sessions; i++) {
  5225. if (mdsc->sessions[i]) {
  5226. session = ceph_get_mds_session(mdsc->sessions[i]);
  5227. __unregister_session(mdsc, session);
  5228. mutex_unlock(&mdsc->mutex);
  5229. mutex_lock(&session->s_mutex);
  5230. remove_session_caps(session);
  5231. mutex_unlock(&session->s_mutex);
  5232. ceph_put_mds_session(session);
  5233. mutex_lock(&mdsc->mutex);
  5234. }
  5235. }
  5236. WARN_ON(!list_empty(&mdsc->cap_delay_list));
  5237. mutex_unlock(&mdsc->mutex);
  5238. ceph_cleanup_snapid_map(mdsc);
  5239. ceph_cleanup_global_and_empty_realms(mdsc);
  5240. cancel_work_sync(&mdsc->cap_reclaim_work);
  5241. cancel_work_sync(&mdsc->cap_unlink_work);
  5242. cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */
  5243. doutc(cl, "done\n");
  5244. }
  5245. void ceph_mdsc_force_umount(struct ceph_mds_client *mdsc)
  5246. {
  5247. struct ceph_mds_session *session;
  5248. int mds;
  5249. doutc(mdsc->fsc->client, "force umount\n");
  5250. mutex_lock(&mdsc->mutex);
  5251. for (mds = 0; mds < mdsc->max_sessions; mds++) {
  5252. session = __ceph_lookup_mds_session(mdsc, mds);
  5253. if (!session)
  5254. continue;
  5255. if (session->s_state == CEPH_MDS_SESSION_REJECTED)
  5256. __unregister_session(mdsc, session);
  5257. __wake_requests(mdsc, &session->s_waiting);
  5258. mutex_unlock(&mdsc->mutex);
  5259. mutex_lock(&session->s_mutex);
  5260. __close_session(mdsc, session);
  5261. if (session->s_state == CEPH_MDS_SESSION_CLOSING) {
  5262. cleanup_session_requests(mdsc, session);
  5263. remove_session_caps(session);
  5264. }
  5265. mutex_unlock(&session->s_mutex);
  5266. ceph_put_mds_session(session);
  5267. mutex_lock(&mdsc->mutex);
  5268. kick_requests(mdsc, mds);
  5269. }
  5270. __wake_requests(mdsc, &mdsc->waiting_for_map);
  5271. mutex_unlock(&mdsc->mutex);
  5272. }
  5273. static void ceph_mdsc_stop(struct ceph_mds_client *mdsc)
  5274. {
  5275. doutc(mdsc->fsc->client, "stop\n");
  5276. /*
  5277. * Make sure the delayed work stopped before releasing
  5278. * the resources.
  5279. *
  5280. * Because the cancel_delayed_work_sync() will only
  5281. * guarantee that the work finishes executing. But the
  5282. * delayed work will re-arm itself again after that.
  5283. */
  5284. flush_delayed_work(&mdsc->delayed_work);
  5285. if (mdsc->mdsmap)
  5286. ceph_mdsmap_destroy(mdsc->mdsmap);
  5287. kfree(mdsc->sessions);
  5288. ceph_caps_finalize(mdsc);
  5289. if (mdsc->s_cap_auths) {
  5290. int i;
  5291. for (i = 0; i < mdsc->s_cap_auths_num; i++) {
  5292. kfree(mdsc->s_cap_auths[i].match.gids);
  5293. kfree(mdsc->s_cap_auths[i].match.path);
  5294. kfree(mdsc->s_cap_auths[i].match.fs_name);
  5295. }
  5296. kfree(mdsc->s_cap_auths);
  5297. }
  5298. ceph_pool_perm_destroy(mdsc);
  5299. }
  5300. void ceph_mdsc_destroy(struct ceph_fs_client *fsc)
  5301. {
  5302. struct ceph_mds_client *mdsc = fsc->mdsc;
  5303. doutc(fsc->client, "%p\n", mdsc);
  5304. if (!mdsc)
  5305. return;
  5306. /* flush out any connection work with references to us */
  5307. ceph_msgr_flush();
  5308. ceph_mdsc_stop(mdsc);
  5309. ceph_metric_destroy(&mdsc->metric);
  5310. fsc->mdsc = NULL;
  5311. kfree(mdsc);
  5312. doutc(fsc->client, "%p done\n", mdsc);
  5313. }
  5314. void ceph_mdsc_handle_fsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
  5315. {
  5316. struct ceph_fs_client *fsc = mdsc->fsc;
  5317. struct ceph_client *cl = fsc->client;
  5318. const char *mds_namespace = fsc->mount_options->mds_namespace;
  5319. void *p = msg->front.iov_base;
  5320. void *end = p + msg->front.iov_len;
  5321. u32 epoch;
  5322. u32 num_fs;
  5323. u32 mount_fscid = (u32)-1;
  5324. int err = -EINVAL;
  5325. ceph_decode_need(&p, end, sizeof(u32), bad);
  5326. epoch = ceph_decode_32(&p);
  5327. doutc(cl, "epoch %u\n", epoch);
  5328. /* struct_v, struct_cv, map_len, epoch, legacy_client_fscid */
  5329. ceph_decode_skip_n(&p, end, 2 + sizeof(u32) * 3, bad);
  5330. ceph_decode_32_safe(&p, end, num_fs, bad);
  5331. while (num_fs-- > 0) {
  5332. void *info_p, *info_end;
  5333. u32 info_len;
  5334. u32 fscid, namelen;
  5335. ceph_decode_need(&p, end, 2 + sizeof(u32), bad);
  5336. p += 2; // info_v, info_cv
  5337. info_len = ceph_decode_32(&p);
  5338. ceph_decode_need(&p, end, info_len, bad);
  5339. info_p = p;
  5340. info_end = p + info_len;
  5341. p = info_end;
  5342. ceph_decode_need(&info_p, info_end, sizeof(u32) * 2, bad);
  5343. fscid = ceph_decode_32(&info_p);
  5344. namelen = ceph_decode_32(&info_p);
  5345. ceph_decode_need(&info_p, info_end, namelen, bad);
  5346. if (mds_namespace &&
  5347. strlen(mds_namespace) == namelen &&
  5348. !strncmp(mds_namespace, (char *)info_p, namelen)) {
  5349. mount_fscid = fscid;
  5350. break;
  5351. }
  5352. }
  5353. ceph_monc_got_map(&fsc->client->monc, CEPH_SUB_FSMAP, epoch);
  5354. if (mount_fscid != (u32)-1) {
  5355. fsc->client->monc.fs_cluster_id = mount_fscid;
  5356. ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
  5357. 0, true);
  5358. ceph_monc_renew_subs(&fsc->client->monc);
  5359. } else {
  5360. err = -ENOENT;
  5361. goto err_out;
  5362. }
  5363. return;
  5364. bad:
  5365. pr_err_client(cl, "error decoding fsmap %d. Shutting down mount.\n",
  5366. err);
  5367. ceph_umount_begin(mdsc->fsc->sb);
  5368. ceph_msg_dump(msg);
  5369. err_out:
  5370. mutex_lock(&mdsc->mutex);
  5371. mdsc->mdsmap_err = err;
  5372. __wake_requests(mdsc, &mdsc->waiting_for_map);
  5373. mutex_unlock(&mdsc->mutex);
  5374. }
  5375. /*
  5376. * handle mds map update.
  5377. */
  5378. void ceph_mdsc_handle_mdsmap(struct ceph_mds_client *mdsc, struct ceph_msg *msg)
  5379. {
  5380. struct ceph_client *cl = mdsc->fsc->client;
  5381. u32 epoch;
  5382. u32 maplen;
  5383. void *p = msg->front.iov_base;
  5384. void *end = p + msg->front.iov_len;
  5385. struct ceph_mdsmap *newmap, *oldmap;
  5386. struct ceph_fsid fsid;
  5387. int err = -EINVAL;
  5388. ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad);
  5389. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  5390. if (ceph_check_fsid(mdsc->fsc->client, &fsid) < 0)
  5391. return;
  5392. epoch = ceph_decode_32(&p);
  5393. maplen = ceph_decode_32(&p);
  5394. doutc(cl, "epoch %u len %d\n", epoch, (int)maplen);
  5395. /* do we need it? */
  5396. mutex_lock(&mdsc->mutex);
  5397. if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) {
  5398. doutc(cl, "epoch %u <= our %u\n", epoch, mdsc->mdsmap->m_epoch);
  5399. mutex_unlock(&mdsc->mutex);
  5400. return;
  5401. }
  5402. newmap = ceph_mdsmap_decode(mdsc, &p, end, ceph_msgr2(mdsc->fsc->client));
  5403. if (IS_ERR(newmap)) {
  5404. err = PTR_ERR(newmap);
  5405. goto bad_unlock;
  5406. }
  5407. /* swap into place */
  5408. if (mdsc->mdsmap) {
  5409. oldmap = mdsc->mdsmap;
  5410. mdsc->mdsmap = newmap;
  5411. check_new_map(mdsc, newmap, oldmap);
  5412. ceph_mdsmap_destroy(oldmap);
  5413. } else {
  5414. mdsc->mdsmap = newmap; /* first mds map */
  5415. }
  5416. mdsc->fsc->max_file_size = min((loff_t)mdsc->mdsmap->m_max_file_size,
  5417. MAX_LFS_FILESIZE);
  5418. __wake_requests(mdsc, &mdsc->waiting_for_map);
  5419. ceph_monc_got_map(&mdsc->fsc->client->monc, CEPH_SUB_MDSMAP,
  5420. mdsc->mdsmap->m_epoch);
  5421. mutex_unlock(&mdsc->mutex);
  5422. schedule_delayed(mdsc, 0);
  5423. return;
  5424. bad_unlock:
  5425. mutex_unlock(&mdsc->mutex);
  5426. bad:
  5427. pr_err_client(cl, "error decoding mdsmap %d. Shutting down mount.\n",
  5428. err);
  5429. ceph_umount_begin(mdsc->fsc->sb);
  5430. ceph_msg_dump(msg);
  5431. return;
  5432. }
  5433. static struct ceph_connection *mds_get_con(struct ceph_connection *con)
  5434. {
  5435. struct ceph_mds_session *s = con->private;
  5436. if (ceph_get_mds_session(s))
  5437. return con;
  5438. return NULL;
  5439. }
  5440. static void mds_put_con(struct ceph_connection *con)
  5441. {
  5442. struct ceph_mds_session *s = con->private;
  5443. ceph_put_mds_session(s);
  5444. }
  5445. /*
  5446. * if the client is unresponsive for long enough, the mds will kill
  5447. * the session entirely.
  5448. */
  5449. static void mds_peer_reset(struct ceph_connection *con)
  5450. {
  5451. struct ceph_mds_session *s = con->private;
  5452. struct ceph_mds_client *mdsc = s->s_mdsc;
  5453. pr_warn_client(mdsc->fsc->client, "mds%d closed our session\n",
  5454. s->s_mds);
  5455. if (READ_ONCE(mdsc->fsc->mount_state) != CEPH_MOUNT_FENCE_IO &&
  5456. ceph_mdsmap_get_state(mdsc->mdsmap, s->s_mds) >= CEPH_MDS_STATE_RECONNECT)
  5457. send_mds_reconnect(mdsc, s);
  5458. }
  5459. static void mds_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  5460. {
  5461. struct ceph_mds_session *s = con->private;
  5462. struct ceph_mds_client *mdsc = s->s_mdsc;
  5463. struct ceph_client *cl = mdsc->fsc->client;
  5464. int type = le16_to_cpu(msg->hdr.type);
  5465. mutex_lock(&mdsc->mutex);
  5466. if (__verify_registered_session(mdsc, s) < 0) {
  5467. mutex_unlock(&mdsc->mutex);
  5468. goto out;
  5469. }
  5470. mutex_unlock(&mdsc->mutex);
  5471. switch (type) {
  5472. case CEPH_MSG_MDS_MAP:
  5473. ceph_mdsc_handle_mdsmap(mdsc, msg);
  5474. break;
  5475. case CEPH_MSG_FS_MAP_USER:
  5476. ceph_mdsc_handle_fsmap(mdsc, msg);
  5477. break;
  5478. case CEPH_MSG_CLIENT_SESSION:
  5479. handle_session(s, msg);
  5480. break;
  5481. case CEPH_MSG_CLIENT_REPLY:
  5482. handle_reply(s, msg);
  5483. break;
  5484. case CEPH_MSG_CLIENT_REQUEST_FORWARD:
  5485. handle_forward(mdsc, s, msg);
  5486. break;
  5487. case CEPH_MSG_CLIENT_CAPS:
  5488. ceph_handle_caps(s, msg);
  5489. break;
  5490. case CEPH_MSG_CLIENT_SNAP:
  5491. ceph_handle_snap(mdsc, s, msg);
  5492. break;
  5493. case CEPH_MSG_CLIENT_LEASE:
  5494. handle_lease(mdsc, s, msg);
  5495. break;
  5496. case CEPH_MSG_CLIENT_QUOTA:
  5497. ceph_handle_quota(mdsc, s, msg);
  5498. break;
  5499. default:
  5500. pr_err_client(cl, "received unknown message type %d %s\n",
  5501. type, ceph_msg_type_name(type));
  5502. }
  5503. out:
  5504. ceph_msg_put(msg);
  5505. }
  5506. /*
  5507. * authentication
  5508. */
  5509. /*
  5510. * Note: returned pointer is the address of a structure that's
  5511. * managed separately. Caller must *not* attempt to free it.
  5512. */
  5513. static struct ceph_auth_handshake *
  5514. mds_get_authorizer(struct ceph_connection *con, int *proto, int force_new)
  5515. {
  5516. struct ceph_mds_session *s = con->private;
  5517. struct ceph_mds_client *mdsc = s->s_mdsc;
  5518. struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
  5519. struct ceph_auth_handshake *auth = &s->s_auth;
  5520. int ret;
  5521. ret = __ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_MDS,
  5522. force_new, proto, NULL, NULL);
  5523. if (ret)
  5524. return ERR_PTR(ret);
  5525. return auth;
  5526. }
  5527. static int mds_add_authorizer_challenge(struct ceph_connection *con,
  5528. void *challenge_buf, int challenge_buf_len)
  5529. {
  5530. struct ceph_mds_session *s = con->private;
  5531. struct ceph_mds_client *mdsc = s->s_mdsc;
  5532. struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
  5533. return ceph_auth_add_authorizer_challenge(ac, s->s_auth.authorizer,
  5534. challenge_buf, challenge_buf_len);
  5535. }
  5536. static int mds_verify_authorizer_reply(struct ceph_connection *con)
  5537. {
  5538. struct ceph_mds_session *s = con->private;
  5539. struct ceph_mds_client *mdsc = s->s_mdsc;
  5540. struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
  5541. struct ceph_auth_handshake *auth = &s->s_auth;
  5542. return ceph_auth_verify_authorizer_reply(ac, auth->authorizer,
  5543. auth->authorizer_reply_buf, auth->authorizer_reply_buf_len,
  5544. NULL, NULL, NULL, NULL);
  5545. }
  5546. static int mds_invalidate_authorizer(struct ceph_connection *con)
  5547. {
  5548. struct ceph_mds_session *s = con->private;
  5549. struct ceph_mds_client *mdsc = s->s_mdsc;
  5550. struct ceph_auth_client *ac = mdsc->fsc->client->monc.auth;
  5551. ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS);
  5552. return ceph_monc_validate_auth(&mdsc->fsc->client->monc);
  5553. }
  5554. static int mds_get_auth_request(struct ceph_connection *con,
  5555. void *buf, int *buf_len,
  5556. void **authorizer, int *authorizer_len)
  5557. {
  5558. struct ceph_mds_session *s = con->private;
  5559. struct ceph_auth_client *ac = s->s_mdsc->fsc->client->monc.auth;
  5560. struct ceph_auth_handshake *auth = &s->s_auth;
  5561. int ret;
  5562. ret = ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_MDS,
  5563. buf, buf_len);
  5564. if (ret)
  5565. return ret;
  5566. *authorizer = auth->authorizer_buf;
  5567. *authorizer_len = auth->authorizer_buf_len;
  5568. return 0;
  5569. }
  5570. static int mds_handle_auth_reply_more(struct ceph_connection *con,
  5571. void *reply, int reply_len,
  5572. void *buf, int *buf_len,
  5573. void **authorizer, int *authorizer_len)
  5574. {
  5575. struct ceph_mds_session *s = con->private;
  5576. struct ceph_auth_client *ac = s->s_mdsc->fsc->client->monc.auth;
  5577. struct ceph_auth_handshake *auth = &s->s_auth;
  5578. int ret;
  5579. ret = ceph_auth_handle_svc_reply_more(ac, auth, reply, reply_len,
  5580. buf, buf_len);
  5581. if (ret)
  5582. return ret;
  5583. *authorizer = auth->authorizer_buf;
  5584. *authorizer_len = auth->authorizer_buf_len;
  5585. return 0;
  5586. }
  5587. static int mds_handle_auth_done(struct ceph_connection *con,
  5588. u64 global_id, void *reply, int reply_len,
  5589. u8 *session_key, int *session_key_len,
  5590. u8 *con_secret, int *con_secret_len)
  5591. {
  5592. struct ceph_mds_session *s = con->private;
  5593. struct ceph_auth_client *ac = s->s_mdsc->fsc->client->monc.auth;
  5594. struct ceph_auth_handshake *auth = &s->s_auth;
  5595. return ceph_auth_handle_svc_reply_done(ac, auth, reply, reply_len,
  5596. session_key, session_key_len,
  5597. con_secret, con_secret_len);
  5598. }
  5599. static int mds_handle_auth_bad_method(struct ceph_connection *con,
  5600. int used_proto, int result,
  5601. const int *allowed_protos, int proto_cnt,
  5602. const int *allowed_modes, int mode_cnt)
  5603. {
  5604. struct ceph_mds_session *s = con->private;
  5605. struct ceph_mon_client *monc = &s->s_mdsc->fsc->client->monc;
  5606. int ret;
  5607. if (ceph_auth_handle_bad_authorizer(monc->auth, CEPH_ENTITY_TYPE_MDS,
  5608. used_proto, result,
  5609. allowed_protos, proto_cnt,
  5610. allowed_modes, mode_cnt)) {
  5611. ret = ceph_monc_validate_auth(monc);
  5612. if (ret)
  5613. return ret;
  5614. }
  5615. return -EACCES;
  5616. }
  5617. static struct ceph_msg *mds_alloc_msg(struct ceph_connection *con,
  5618. struct ceph_msg_header *hdr, int *skip)
  5619. {
  5620. struct ceph_msg *msg;
  5621. int type = (int) le16_to_cpu(hdr->type);
  5622. int front_len = (int) le32_to_cpu(hdr->front_len);
  5623. if (con->in_msg)
  5624. return con->in_msg;
  5625. *skip = 0;
  5626. msg = ceph_msg_new(type, front_len, GFP_NOFS, false);
  5627. if (!msg) {
  5628. pr_err("unable to allocate msg type %d len %d\n",
  5629. type, front_len);
  5630. return NULL;
  5631. }
  5632. return msg;
  5633. }
  5634. static int mds_sign_message(struct ceph_msg *msg)
  5635. {
  5636. struct ceph_mds_session *s = msg->con->private;
  5637. struct ceph_auth_handshake *auth = &s->s_auth;
  5638. return ceph_auth_sign_message(auth, msg);
  5639. }
  5640. static int mds_check_message_signature(struct ceph_msg *msg)
  5641. {
  5642. struct ceph_mds_session *s = msg->con->private;
  5643. struct ceph_auth_handshake *auth = &s->s_auth;
  5644. return ceph_auth_check_message_signature(auth, msg);
  5645. }
  5646. static const struct ceph_connection_operations mds_con_ops = {
  5647. .get = mds_get_con,
  5648. .put = mds_put_con,
  5649. .alloc_msg = mds_alloc_msg,
  5650. .dispatch = mds_dispatch,
  5651. .peer_reset = mds_peer_reset,
  5652. .get_authorizer = mds_get_authorizer,
  5653. .add_authorizer_challenge = mds_add_authorizer_challenge,
  5654. .verify_authorizer_reply = mds_verify_authorizer_reply,
  5655. .invalidate_authorizer = mds_invalidate_authorizer,
  5656. .sign_message = mds_sign_message,
  5657. .check_message_signature = mds_check_message_signature,
  5658. .get_auth_request = mds_get_auth_request,
  5659. .handle_auth_reply_more = mds_handle_auth_reply_more,
  5660. .handle_auth_done = mds_handle_auth_done,
  5661. .handle_auth_bad_method = mds_handle_auth_bad_method,
  5662. };
  5663. /* eof */