mds_client.c 171 KB

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