mds_client.c 144 KB

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