osd_client.c 154 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/module.h>
  4. #include <linux/err.h>
  5. #include <linux/highmem.h>
  6. #include <linux/mm.h>
  7. #include <linux/pagemap.h>
  8. #include <linux/slab.h>
  9. #include <linux/uaccess.h>
  10. #ifdef CONFIG_BLOCK
  11. #include <linux/bio.h>
  12. #endif
  13. #include <linux/ceph/ceph_features.h>
  14. #include <linux/ceph/libceph.h>
  15. #include <linux/ceph/osd_client.h>
  16. #include <linux/ceph/messenger.h>
  17. #include <linux/ceph/decode.h>
  18. #include <linux/ceph/auth.h>
  19. #include <linux/ceph/pagelist.h>
  20. #include <linux/ceph/striper.h>
  21. #define OSD_OPREPLY_FRONT_LEN 512
  22. static struct kmem_cache *ceph_osd_request_cache;
  23. static const struct ceph_connection_operations osd_con_ops;
  24. /*
  25. * Implement client access to distributed object storage cluster.
  26. *
  27. * All data objects are stored within a cluster/cloud of OSDs, or
  28. * "object storage devices." (Note that Ceph OSDs have _nothing_ to
  29. * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
  30. * remote daemons serving up and coordinating consistent and safe
  31. * access to storage.
  32. *
  33. * Cluster membership and the mapping of data objects onto storage devices
  34. * are described by the osd map.
  35. *
  36. * We keep track of pending OSD requests (read, write), resubmit
  37. * requests to different OSDs when the cluster topology/data layout
  38. * change, or retry the affected requests when the communications
  39. * channel with an OSD is reset.
  40. */
  41. static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req);
  42. static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req);
  43. static void link_linger(struct ceph_osd *osd,
  44. struct ceph_osd_linger_request *lreq);
  45. static void unlink_linger(struct ceph_osd *osd,
  46. struct ceph_osd_linger_request *lreq);
  47. static void clear_backoffs(struct ceph_osd *osd);
  48. #if 1
  49. static inline bool rwsem_is_wrlocked(struct rw_semaphore *sem)
  50. {
  51. bool wrlocked = true;
  52. if (unlikely(down_read_trylock(sem))) {
  53. wrlocked = false;
  54. up_read(sem);
  55. }
  56. return wrlocked;
  57. }
  58. static inline void verify_osdc_locked(struct ceph_osd_client *osdc)
  59. {
  60. WARN_ON(!rwsem_is_locked(&osdc->lock));
  61. }
  62. static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc)
  63. {
  64. WARN_ON(!rwsem_is_wrlocked(&osdc->lock));
  65. }
  66. static inline void verify_osd_locked(struct ceph_osd *osd)
  67. {
  68. struct ceph_osd_client *osdc = osd->o_osdc;
  69. WARN_ON(!(mutex_is_locked(&osd->lock) &&
  70. rwsem_is_locked(&osdc->lock)) &&
  71. !rwsem_is_wrlocked(&osdc->lock));
  72. }
  73. static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq)
  74. {
  75. WARN_ON(!mutex_is_locked(&lreq->lock));
  76. }
  77. #else
  78. static inline void verify_osdc_locked(struct ceph_osd_client *osdc) { }
  79. static inline void verify_osdc_wrlocked(struct ceph_osd_client *osdc) { }
  80. static inline void verify_osd_locked(struct ceph_osd *osd) { }
  81. static inline void verify_lreq_locked(struct ceph_osd_linger_request *lreq) { }
  82. #endif
  83. /*
  84. * calculate the mapping of a file extent onto an object, and fill out the
  85. * request accordingly. shorten extent as necessary if it crosses an
  86. * object boundary.
  87. *
  88. * fill osd op in request message.
  89. */
  90. static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
  91. u64 *objnum, u64 *objoff, u64 *objlen)
  92. {
  93. u64 orig_len = *plen;
  94. u32 xlen;
  95. /* object extent? */
  96. ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
  97. objoff, &xlen);
  98. *objlen = xlen;
  99. if (*objlen < orig_len) {
  100. *plen = *objlen;
  101. dout(" skipping last %llu, final file extent %llu~%llu\n",
  102. orig_len - *plen, off, *plen);
  103. }
  104. dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
  105. return 0;
  106. }
  107. static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
  108. {
  109. memset(osd_data, 0, sizeof (*osd_data));
  110. osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
  111. }
  112. /*
  113. * Consumes @pages if @own_pages is true.
  114. */
  115. static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
  116. struct page **pages, u64 length, u32 alignment,
  117. bool pages_from_pool, bool own_pages)
  118. {
  119. osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
  120. osd_data->pages = pages;
  121. osd_data->length = length;
  122. osd_data->alignment = alignment;
  123. osd_data->pages_from_pool = pages_from_pool;
  124. osd_data->own_pages = own_pages;
  125. }
  126. /*
  127. * Consumes a ref on @pagelist.
  128. */
  129. static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
  130. struct ceph_pagelist *pagelist)
  131. {
  132. osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
  133. osd_data->pagelist = pagelist;
  134. }
  135. #ifdef CONFIG_BLOCK
  136. static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
  137. struct ceph_bio_iter *bio_pos,
  138. u32 bio_length)
  139. {
  140. osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
  141. osd_data->bio_pos = *bio_pos;
  142. osd_data->bio_length = bio_length;
  143. }
  144. #endif /* CONFIG_BLOCK */
  145. static void ceph_osd_data_bvecs_init(struct ceph_osd_data *osd_data,
  146. struct ceph_bvec_iter *bvec_pos,
  147. u32 num_bvecs)
  148. {
  149. osd_data->type = CEPH_OSD_DATA_TYPE_BVECS;
  150. osd_data->bvec_pos = *bvec_pos;
  151. osd_data->num_bvecs = num_bvecs;
  152. }
  153. static void ceph_osd_iter_init(struct ceph_osd_data *osd_data,
  154. struct iov_iter *iter)
  155. {
  156. osd_data->type = CEPH_OSD_DATA_TYPE_ITER;
  157. osd_data->iter = *iter;
  158. }
  159. static struct ceph_osd_data *
  160. osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
  161. {
  162. BUG_ON(which >= osd_req->r_num_ops);
  163. return &osd_req->r_ops[which].raw_data_in;
  164. }
  165. struct ceph_osd_data *
  166. osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
  167. unsigned int which)
  168. {
  169. return osd_req_op_data(osd_req, which, extent, osd_data);
  170. }
  171. EXPORT_SYMBOL(osd_req_op_extent_osd_data);
  172. void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
  173. unsigned int which, struct page **pages,
  174. u64 length, u32 alignment,
  175. bool pages_from_pool, bool own_pages)
  176. {
  177. struct ceph_osd_data *osd_data;
  178. osd_data = osd_req_op_raw_data_in(osd_req, which);
  179. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  180. pages_from_pool, own_pages);
  181. }
  182. EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
  183. void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
  184. unsigned int which, struct page **pages,
  185. u64 length, u32 alignment,
  186. bool pages_from_pool, bool own_pages)
  187. {
  188. struct ceph_osd_data *osd_data;
  189. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  190. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  191. pages_from_pool, own_pages);
  192. }
  193. EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
  194. void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
  195. unsigned int which, struct ceph_pagelist *pagelist)
  196. {
  197. struct ceph_osd_data *osd_data;
  198. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  199. ceph_osd_data_pagelist_init(osd_data, pagelist);
  200. }
  201. EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
  202. #ifdef CONFIG_BLOCK
  203. void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
  204. unsigned int which,
  205. struct ceph_bio_iter *bio_pos,
  206. u32 bio_length)
  207. {
  208. struct ceph_osd_data *osd_data;
  209. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  210. ceph_osd_data_bio_init(osd_data, bio_pos, bio_length);
  211. }
  212. EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
  213. #endif /* CONFIG_BLOCK */
  214. void osd_req_op_extent_osd_data_bvecs(struct ceph_osd_request *osd_req,
  215. unsigned int which,
  216. struct bio_vec *bvecs, u32 num_bvecs,
  217. u32 bytes)
  218. {
  219. struct ceph_osd_data *osd_data;
  220. struct ceph_bvec_iter it = {
  221. .bvecs = bvecs,
  222. .iter = { .bi_size = bytes },
  223. };
  224. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  225. ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
  226. }
  227. EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvecs);
  228. void osd_req_op_extent_osd_data_bvec_pos(struct ceph_osd_request *osd_req,
  229. unsigned int which,
  230. struct ceph_bvec_iter *bvec_pos)
  231. {
  232. struct ceph_osd_data *osd_data;
  233. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  234. ceph_osd_data_bvecs_init(osd_data, bvec_pos, 0);
  235. }
  236. EXPORT_SYMBOL(osd_req_op_extent_osd_data_bvec_pos);
  237. /**
  238. * osd_req_op_extent_osd_iter - Set up an operation with an iterator buffer
  239. * @osd_req: The request to set up
  240. * @which: Index of the operation in which to set the iter
  241. * @iter: The buffer iterator
  242. */
  243. void osd_req_op_extent_osd_iter(struct ceph_osd_request *osd_req,
  244. unsigned int which, struct iov_iter *iter)
  245. {
  246. struct ceph_osd_data *osd_data;
  247. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  248. ceph_osd_iter_init(osd_data, iter);
  249. }
  250. EXPORT_SYMBOL(osd_req_op_extent_osd_iter);
  251. static void osd_req_op_cls_request_info_pagelist(
  252. struct ceph_osd_request *osd_req,
  253. unsigned int which, struct ceph_pagelist *pagelist)
  254. {
  255. struct ceph_osd_data *osd_data;
  256. osd_data = osd_req_op_data(osd_req, which, cls, request_info);
  257. ceph_osd_data_pagelist_init(osd_data, pagelist);
  258. }
  259. void osd_req_op_cls_request_data_pagelist(
  260. struct ceph_osd_request *osd_req,
  261. unsigned int which, struct ceph_pagelist *pagelist)
  262. {
  263. struct ceph_osd_data *osd_data;
  264. osd_data = osd_req_op_data(osd_req, which, cls, request_data);
  265. ceph_osd_data_pagelist_init(osd_data, pagelist);
  266. osd_req->r_ops[which].cls.indata_len += pagelist->length;
  267. osd_req->r_ops[which].indata_len += pagelist->length;
  268. }
  269. EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
  270. void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
  271. unsigned int which, struct page **pages, u64 length,
  272. u32 alignment, bool pages_from_pool, bool own_pages)
  273. {
  274. struct ceph_osd_data *osd_data;
  275. osd_data = osd_req_op_data(osd_req, which, cls, request_data);
  276. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  277. pages_from_pool, own_pages);
  278. osd_req->r_ops[which].cls.indata_len += length;
  279. osd_req->r_ops[which].indata_len += length;
  280. }
  281. EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
  282. void osd_req_op_cls_request_data_bvecs(struct ceph_osd_request *osd_req,
  283. unsigned int which,
  284. struct bio_vec *bvecs, u32 num_bvecs,
  285. u32 bytes)
  286. {
  287. struct ceph_osd_data *osd_data;
  288. struct ceph_bvec_iter it = {
  289. .bvecs = bvecs,
  290. .iter = { .bi_size = bytes },
  291. };
  292. osd_data = osd_req_op_data(osd_req, which, cls, request_data);
  293. ceph_osd_data_bvecs_init(osd_data, &it, num_bvecs);
  294. osd_req->r_ops[which].cls.indata_len += bytes;
  295. osd_req->r_ops[which].indata_len += bytes;
  296. }
  297. EXPORT_SYMBOL(osd_req_op_cls_request_data_bvecs);
  298. void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
  299. unsigned int which, struct page **pages, u64 length,
  300. u32 alignment, bool pages_from_pool, bool own_pages)
  301. {
  302. struct ceph_osd_data *osd_data;
  303. osd_data = osd_req_op_data(osd_req, which, cls, response_data);
  304. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  305. pages_from_pool, own_pages);
  306. }
  307. EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
  308. static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
  309. {
  310. switch (osd_data->type) {
  311. case CEPH_OSD_DATA_TYPE_NONE:
  312. return 0;
  313. case CEPH_OSD_DATA_TYPE_PAGES:
  314. return osd_data->length;
  315. case CEPH_OSD_DATA_TYPE_PAGELIST:
  316. return (u64)osd_data->pagelist->length;
  317. #ifdef CONFIG_BLOCK
  318. case CEPH_OSD_DATA_TYPE_BIO:
  319. return (u64)osd_data->bio_length;
  320. #endif /* CONFIG_BLOCK */
  321. case CEPH_OSD_DATA_TYPE_BVECS:
  322. return osd_data->bvec_pos.iter.bi_size;
  323. case CEPH_OSD_DATA_TYPE_ITER:
  324. return iov_iter_count(&osd_data->iter);
  325. default:
  326. WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
  327. return 0;
  328. }
  329. }
  330. static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
  331. {
  332. if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
  333. int num_pages;
  334. num_pages = calc_pages_for((u64)osd_data->alignment,
  335. (u64)osd_data->length);
  336. ceph_release_page_vector(osd_data->pages, num_pages);
  337. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
  338. ceph_pagelist_release(osd_data->pagelist);
  339. }
  340. ceph_osd_data_init(osd_data);
  341. }
  342. static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
  343. unsigned int which)
  344. {
  345. struct ceph_osd_req_op *op;
  346. BUG_ON(which >= osd_req->r_num_ops);
  347. op = &osd_req->r_ops[which];
  348. switch (op->op) {
  349. case CEPH_OSD_OP_READ:
  350. case CEPH_OSD_OP_SPARSE_READ:
  351. case CEPH_OSD_OP_WRITE:
  352. case CEPH_OSD_OP_WRITEFULL:
  353. kfree(op->extent.sparse_ext);
  354. ceph_osd_data_release(&op->extent.osd_data);
  355. break;
  356. case CEPH_OSD_OP_CALL:
  357. ceph_osd_data_release(&op->cls.request_info);
  358. ceph_osd_data_release(&op->cls.request_data);
  359. ceph_osd_data_release(&op->cls.response_data);
  360. break;
  361. case CEPH_OSD_OP_SETXATTR:
  362. case CEPH_OSD_OP_CMPXATTR:
  363. ceph_osd_data_release(&op->xattr.osd_data);
  364. break;
  365. case CEPH_OSD_OP_STAT:
  366. ceph_osd_data_release(&op->raw_data_in);
  367. break;
  368. case CEPH_OSD_OP_NOTIFY_ACK:
  369. ceph_osd_data_release(&op->notify_ack.request_data);
  370. break;
  371. case CEPH_OSD_OP_NOTIFY:
  372. ceph_osd_data_release(&op->notify.request_data);
  373. ceph_osd_data_release(&op->notify.response_data);
  374. break;
  375. case CEPH_OSD_OP_LIST_WATCHERS:
  376. ceph_osd_data_release(&op->list_watchers.response_data);
  377. break;
  378. case CEPH_OSD_OP_COPY_FROM2:
  379. ceph_osd_data_release(&op->copy_from.osd_data);
  380. break;
  381. default:
  382. break;
  383. }
  384. }
  385. /*
  386. * Assumes @t is zero-initialized.
  387. */
  388. static void target_init(struct ceph_osd_request_target *t)
  389. {
  390. ceph_oid_init(&t->base_oid);
  391. ceph_oloc_init(&t->base_oloc);
  392. ceph_oid_init(&t->target_oid);
  393. ceph_oloc_init(&t->target_oloc);
  394. ceph_osds_init(&t->acting);
  395. ceph_osds_init(&t->up);
  396. t->size = -1;
  397. t->min_size = -1;
  398. t->osd = CEPH_HOMELESS_OSD;
  399. }
  400. static void target_copy(struct ceph_osd_request_target *dest,
  401. const struct ceph_osd_request_target *src)
  402. {
  403. ceph_oid_copy(&dest->base_oid, &src->base_oid);
  404. ceph_oloc_copy(&dest->base_oloc, &src->base_oloc);
  405. ceph_oid_copy(&dest->target_oid, &src->target_oid);
  406. ceph_oloc_copy(&dest->target_oloc, &src->target_oloc);
  407. dest->pgid = src->pgid; /* struct */
  408. dest->spgid = src->spgid; /* struct */
  409. dest->pg_num = src->pg_num;
  410. dest->pg_num_mask = src->pg_num_mask;
  411. ceph_osds_copy(&dest->acting, &src->acting);
  412. ceph_osds_copy(&dest->up, &src->up);
  413. dest->size = src->size;
  414. dest->min_size = src->min_size;
  415. dest->sort_bitwise = src->sort_bitwise;
  416. dest->recovery_deletes = src->recovery_deletes;
  417. dest->flags = src->flags;
  418. dest->used_replica = src->used_replica;
  419. dest->paused = src->paused;
  420. dest->epoch = src->epoch;
  421. dest->last_force_resend = src->last_force_resend;
  422. dest->osd = src->osd;
  423. }
  424. static void target_destroy(struct ceph_osd_request_target *t)
  425. {
  426. ceph_oid_destroy(&t->base_oid);
  427. ceph_oloc_destroy(&t->base_oloc);
  428. ceph_oid_destroy(&t->target_oid);
  429. ceph_oloc_destroy(&t->target_oloc);
  430. }
  431. /*
  432. * requests
  433. */
  434. static void request_release_checks(struct ceph_osd_request *req)
  435. {
  436. WARN_ON(!RB_EMPTY_NODE(&req->r_node));
  437. WARN_ON(!RB_EMPTY_NODE(&req->r_mc_node));
  438. WARN_ON(!list_empty(&req->r_private_item));
  439. WARN_ON(req->r_osd);
  440. }
  441. static void ceph_osdc_release_request(struct kref *kref)
  442. {
  443. struct ceph_osd_request *req = container_of(kref,
  444. struct ceph_osd_request, r_kref);
  445. unsigned int which;
  446. dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
  447. req->r_request, req->r_reply);
  448. request_release_checks(req);
  449. if (req->r_request)
  450. ceph_msg_put(req->r_request);
  451. if (req->r_reply)
  452. ceph_msg_put(req->r_reply);
  453. for (which = 0; which < req->r_num_ops; which++)
  454. osd_req_op_data_release(req, which);
  455. target_destroy(&req->r_t);
  456. ceph_put_snap_context(req->r_snapc);
  457. if (req->r_mempool)
  458. mempool_free(req, req->r_osdc->req_mempool);
  459. else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
  460. kmem_cache_free(ceph_osd_request_cache, req);
  461. else
  462. kfree(req);
  463. }
  464. void ceph_osdc_get_request(struct ceph_osd_request *req)
  465. {
  466. dout("%s %p (was %d)\n", __func__, req,
  467. kref_read(&req->r_kref));
  468. kref_get(&req->r_kref);
  469. }
  470. EXPORT_SYMBOL(ceph_osdc_get_request);
  471. void ceph_osdc_put_request(struct ceph_osd_request *req)
  472. {
  473. if (req) {
  474. dout("%s %p (was %d)\n", __func__, req,
  475. kref_read(&req->r_kref));
  476. kref_put(&req->r_kref, ceph_osdc_release_request);
  477. }
  478. }
  479. EXPORT_SYMBOL(ceph_osdc_put_request);
  480. static void request_init(struct ceph_osd_request *req)
  481. {
  482. /* req only, each op is zeroed in osd_req_op_init() */
  483. memset(req, 0, sizeof(*req));
  484. kref_init(&req->r_kref);
  485. init_completion(&req->r_completion);
  486. RB_CLEAR_NODE(&req->r_node);
  487. RB_CLEAR_NODE(&req->r_mc_node);
  488. INIT_LIST_HEAD(&req->r_private_item);
  489. target_init(&req->r_t);
  490. }
  491. struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
  492. struct ceph_snap_context *snapc,
  493. unsigned int num_ops,
  494. bool use_mempool,
  495. gfp_t gfp_flags)
  496. {
  497. struct ceph_osd_request *req;
  498. if (use_mempool) {
  499. BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
  500. req = mempool_alloc(osdc->req_mempool, gfp_flags);
  501. } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
  502. req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
  503. } else {
  504. BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
  505. req = kmalloc(struct_size(req, r_ops, num_ops), gfp_flags);
  506. }
  507. if (unlikely(!req))
  508. return NULL;
  509. request_init(req);
  510. req->r_osdc = osdc;
  511. req->r_mempool = use_mempool;
  512. req->r_num_ops = num_ops;
  513. req->r_snapid = CEPH_NOSNAP;
  514. req->r_snapc = ceph_get_snap_context(snapc);
  515. dout("%s req %p\n", __func__, req);
  516. return req;
  517. }
  518. EXPORT_SYMBOL(ceph_osdc_alloc_request);
  519. static int ceph_oloc_encoding_size(const struct ceph_object_locator *oloc)
  520. {
  521. return 8 + 4 + 4 + 4 + (oloc->pool_ns ? oloc->pool_ns->len : 0);
  522. }
  523. static int __ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp,
  524. int num_request_data_items,
  525. int num_reply_data_items)
  526. {
  527. struct ceph_osd_client *osdc = req->r_osdc;
  528. struct ceph_msg *msg;
  529. int msg_size;
  530. WARN_ON(req->r_request || req->r_reply);
  531. WARN_ON(ceph_oid_empty(&req->r_base_oid));
  532. WARN_ON(ceph_oloc_empty(&req->r_base_oloc));
  533. /* create request message */
  534. msg_size = CEPH_ENCODING_START_BLK_LEN +
  535. CEPH_PGID_ENCODING_LEN + 1; /* spgid */
  536. msg_size += 4 + 4 + 4; /* hash, osdmap_epoch, flags */
  537. msg_size += CEPH_ENCODING_START_BLK_LEN +
  538. sizeof(struct ceph_osd_reqid); /* reqid */
  539. msg_size += sizeof(struct ceph_blkin_trace_info); /* trace */
  540. msg_size += 4 + sizeof(struct ceph_timespec); /* client_inc, mtime */
  541. msg_size += CEPH_ENCODING_START_BLK_LEN +
  542. ceph_oloc_encoding_size(&req->r_base_oloc); /* oloc */
  543. msg_size += 4 + req->r_base_oid.name_len; /* oid */
  544. msg_size += 2 + req->r_num_ops * sizeof(struct ceph_osd_op);
  545. msg_size += 8; /* snapid */
  546. msg_size += 8; /* snap_seq */
  547. msg_size += 4 + 8 * (req->r_snapc ? req->r_snapc->num_snaps : 0);
  548. msg_size += 4 + 8; /* retry_attempt, features */
  549. if (req->r_mempool)
  550. msg = ceph_msgpool_get(&osdc->msgpool_op, msg_size,
  551. num_request_data_items);
  552. else
  553. msg = ceph_msg_new2(CEPH_MSG_OSD_OP, msg_size,
  554. num_request_data_items, gfp, true);
  555. if (!msg)
  556. return -ENOMEM;
  557. memset(msg->front.iov_base, 0, msg->front.iov_len);
  558. req->r_request = msg;
  559. /* create reply message */
  560. msg_size = OSD_OPREPLY_FRONT_LEN;
  561. msg_size += req->r_base_oid.name_len;
  562. msg_size += req->r_num_ops * sizeof(struct ceph_osd_op);
  563. if (req->r_mempool)
  564. msg = ceph_msgpool_get(&osdc->msgpool_op_reply, msg_size,
  565. num_reply_data_items);
  566. else
  567. msg = ceph_msg_new2(CEPH_MSG_OSD_OPREPLY, msg_size,
  568. num_reply_data_items, gfp, true);
  569. if (!msg)
  570. return -ENOMEM;
  571. req->r_reply = msg;
  572. return 0;
  573. }
  574. static bool osd_req_opcode_valid(u16 opcode)
  575. {
  576. switch (opcode) {
  577. #define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
  578. __CEPH_FORALL_OSD_OPS(GENERATE_CASE)
  579. #undef GENERATE_CASE
  580. default:
  581. return false;
  582. }
  583. }
  584. static void get_num_data_items(struct ceph_osd_request *req,
  585. int *num_request_data_items,
  586. int *num_reply_data_items)
  587. {
  588. struct ceph_osd_req_op *op;
  589. *num_request_data_items = 0;
  590. *num_reply_data_items = 0;
  591. for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
  592. switch (op->op) {
  593. /* request */
  594. case CEPH_OSD_OP_WRITE:
  595. case CEPH_OSD_OP_WRITEFULL:
  596. case CEPH_OSD_OP_SETXATTR:
  597. case CEPH_OSD_OP_CMPXATTR:
  598. case CEPH_OSD_OP_NOTIFY_ACK:
  599. case CEPH_OSD_OP_COPY_FROM2:
  600. *num_request_data_items += 1;
  601. break;
  602. /* reply */
  603. case CEPH_OSD_OP_STAT:
  604. case CEPH_OSD_OP_READ:
  605. case CEPH_OSD_OP_SPARSE_READ:
  606. case CEPH_OSD_OP_LIST_WATCHERS:
  607. *num_reply_data_items += 1;
  608. break;
  609. /* both */
  610. case CEPH_OSD_OP_NOTIFY:
  611. *num_request_data_items += 1;
  612. *num_reply_data_items += 1;
  613. break;
  614. case CEPH_OSD_OP_CALL:
  615. *num_request_data_items += 2;
  616. *num_reply_data_items += 1;
  617. break;
  618. default:
  619. WARN_ON(!osd_req_opcode_valid(op->op));
  620. break;
  621. }
  622. }
  623. }
  624. /*
  625. * oid, oloc and OSD op opcode(s) must be filled in before this function
  626. * is called.
  627. */
  628. int ceph_osdc_alloc_messages(struct ceph_osd_request *req, gfp_t gfp)
  629. {
  630. int num_request_data_items, num_reply_data_items;
  631. get_num_data_items(req, &num_request_data_items, &num_reply_data_items);
  632. return __ceph_osdc_alloc_messages(req, gfp, num_request_data_items,
  633. num_reply_data_items);
  634. }
  635. EXPORT_SYMBOL(ceph_osdc_alloc_messages);
  636. /*
  637. * This is an osd op init function for opcodes that have no data or
  638. * other information associated with them. It also serves as a
  639. * common init routine for all the other init functions, below.
  640. */
  641. struct ceph_osd_req_op *
  642. osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
  643. u16 opcode, u32 flags)
  644. {
  645. struct ceph_osd_req_op *op;
  646. BUG_ON(which >= osd_req->r_num_ops);
  647. BUG_ON(!osd_req_opcode_valid(opcode));
  648. op = &osd_req->r_ops[which];
  649. memset(op, 0, sizeof (*op));
  650. op->op = opcode;
  651. op->flags = flags;
  652. return op;
  653. }
  654. EXPORT_SYMBOL(osd_req_op_init);
  655. void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
  656. unsigned int which, u16 opcode,
  657. u64 offset, u64 length,
  658. u64 truncate_size, u32 truncate_seq)
  659. {
  660. struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which,
  661. opcode, 0);
  662. size_t payload_len = 0;
  663. BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
  664. opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
  665. opcode != CEPH_OSD_OP_TRUNCATE && opcode != CEPH_OSD_OP_SPARSE_READ);
  666. op->extent.offset = offset;
  667. op->extent.length = length;
  668. op->extent.truncate_size = truncate_size;
  669. op->extent.truncate_seq = truncate_seq;
  670. if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
  671. payload_len += length;
  672. op->indata_len = payload_len;
  673. }
  674. EXPORT_SYMBOL(osd_req_op_extent_init);
  675. void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
  676. unsigned int which, u64 length)
  677. {
  678. struct ceph_osd_req_op *op;
  679. u64 previous;
  680. BUG_ON(which >= osd_req->r_num_ops);
  681. op = &osd_req->r_ops[which];
  682. previous = op->extent.length;
  683. if (length == previous)
  684. return; /* Nothing to do */
  685. BUG_ON(length > previous);
  686. op->extent.length = length;
  687. if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
  688. op->indata_len -= previous - length;
  689. }
  690. EXPORT_SYMBOL(osd_req_op_extent_update);
  691. void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
  692. unsigned int which, u64 offset_inc)
  693. {
  694. struct ceph_osd_req_op *op, *prev_op;
  695. BUG_ON(which + 1 >= osd_req->r_num_ops);
  696. prev_op = &osd_req->r_ops[which];
  697. op = osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
  698. /* dup previous one */
  699. op->indata_len = prev_op->indata_len;
  700. op->outdata_len = prev_op->outdata_len;
  701. op->extent = prev_op->extent;
  702. /* adjust offset */
  703. op->extent.offset += offset_inc;
  704. op->extent.length -= offset_inc;
  705. if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
  706. op->indata_len -= offset_inc;
  707. }
  708. EXPORT_SYMBOL(osd_req_op_extent_dup_last);
  709. int osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
  710. const char *class, const char *method)
  711. {
  712. struct ceph_osd_req_op *op;
  713. struct ceph_pagelist *pagelist;
  714. size_t payload_len = 0;
  715. size_t size;
  716. int ret;
  717. op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_CALL, 0);
  718. pagelist = ceph_pagelist_alloc(GFP_NOFS);
  719. if (!pagelist)
  720. return -ENOMEM;
  721. op->cls.class_name = class;
  722. size = strlen(class);
  723. BUG_ON(size > (size_t) U8_MAX);
  724. op->cls.class_len = size;
  725. ret = ceph_pagelist_append(pagelist, class, size);
  726. if (ret)
  727. goto err_pagelist_free;
  728. payload_len += size;
  729. op->cls.method_name = method;
  730. size = strlen(method);
  731. BUG_ON(size > (size_t) U8_MAX);
  732. op->cls.method_len = size;
  733. ret = ceph_pagelist_append(pagelist, method, size);
  734. if (ret)
  735. goto err_pagelist_free;
  736. payload_len += size;
  737. osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
  738. op->indata_len = payload_len;
  739. return 0;
  740. err_pagelist_free:
  741. ceph_pagelist_release(pagelist);
  742. return ret;
  743. }
  744. EXPORT_SYMBOL(osd_req_op_cls_init);
  745. int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
  746. u16 opcode, const char *name, const void *value,
  747. size_t size, u8 cmp_op, u8 cmp_mode)
  748. {
  749. struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which,
  750. opcode, 0);
  751. struct ceph_pagelist *pagelist;
  752. size_t payload_len;
  753. int ret;
  754. BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
  755. pagelist = ceph_pagelist_alloc(GFP_NOFS);
  756. if (!pagelist)
  757. return -ENOMEM;
  758. payload_len = strlen(name);
  759. op->xattr.name_len = payload_len;
  760. ret = ceph_pagelist_append(pagelist, name, payload_len);
  761. if (ret)
  762. goto err_pagelist_free;
  763. op->xattr.value_len = size;
  764. ret = ceph_pagelist_append(pagelist, value, size);
  765. if (ret)
  766. goto err_pagelist_free;
  767. payload_len += size;
  768. op->xattr.cmp_op = cmp_op;
  769. op->xattr.cmp_mode = cmp_mode;
  770. ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
  771. op->indata_len = payload_len;
  772. return 0;
  773. err_pagelist_free:
  774. ceph_pagelist_release(pagelist);
  775. return ret;
  776. }
  777. EXPORT_SYMBOL(osd_req_op_xattr_init);
  778. /*
  779. * @watch_opcode: CEPH_OSD_WATCH_OP_*
  780. */
  781. static void osd_req_op_watch_init(struct ceph_osd_request *req, int which,
  782. u8 watch_opcode, u64 cookie, u32 gen)
  783. {
  784. struct ceph_osd_req_op *op;
  785. op = osd_req_op_init(req, which, CEPH_OSD_OP_WATCH, 0);
  786. op->watch.cookie = cookie;
  787. op->watch.op = watch_opcode;
  788. op->watch.gen = gen;
  789. }
  790. /*
  791. * prot_ver, timeout and notify payload (may be empty) should already be
  792. * encoded in @request_pl
  793. */
  794. static void osd_req_op_notify_init(struct ceph_osd_request *req, int which,
  795. u64 cookie, struct ceph_pagelist *request_pl)
  796. {
  797. struct ceph_osd_req_op *op;
  798. op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY, 0);
  799. op->notify.cookie = cookie;
  800. ceph_osd_data_pagelist_init(&op->notify.request_data, request_pl);
  801. op->indata_len = request_pl->length;
  802. }
  803. /*
  804. * @flags: CEPH_OSD_OP_ALLOC_HINT_FLAG_*
  805. */
  806. void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
  807. unsigned int which,
  808. u64 expected_object_size,
  809. u64 expected_write_size,
  810. u32 flags)
  811. {
  812. struct ceph_osd_req_op *op;
  813. op = osd_req_op_init(osd_req, which, CEPH_OSD_OP_SETALLOCHINT, 0);
  814. op->alloc_hint.expected_object_size = expected_object_size;
  815. op->alloc_hint.expected_write_size = expected_write_size;
  816. op->alloc_hint.flags = flags;
  817. /*
  818. * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
  819. * not worth a feature bit. Set FAILOK per-op flag to make
  820. * sure older osds don't trip over an unsupported opcode.
  821. */
  822. op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
  823. }
  824. EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
  825. static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
  826. struct ceph_osd_data *osd_data)
  827. {
  828. u64 length = ceph_osd_data_length(osd_data);
  829. if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
  830. BUG_ON(length > (u64) SIZE_MAX);
  831. if (length)
  832. ceph_msg_data_add_pages(msg, osd_data->pages,
  833. length, osd_data->alignment, false);
  834. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
  835. BUG_ON(!length);
  836. ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
  837. #ifdef CONFIG_BLOCK
  838. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
  839. ceph_msg_data_add_bio(msg, &osd_data->bio_pos, length);
  840. #endif
  841. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BVECS) {
  842. ceph_msg_data_add_bvecs(msg, &osd_data->bvec_pos);
  843. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_ITER) {
  844. ceph_msg_data_add_iter(msg, &osd_data->iter);
  845. } else {
  846. BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
  847. }
  848. }
  849. static u32 osd_req_encode_op(struct ceph_osd_op *dst,
  850. const struct ceph_osd_req_op *src)
  851. {
  852. switch (src->op) {
  853. case CEPH_OSD_OP_STAT:
  854. break;
  855. case CEPH_OSD_OP_READ:
  856. case CEPH_OSD_OP_SPARSE_READ:
  857. case CEPH_OSD_OP_WRITE:
  858. case CEPH_OSD_OP_WRITEFULL:
  859. case CEPH_OSD_OP_ZERO:
  860. case CEPH_OSD_OP_TRUNCATE:
  861. dst->extent.offset = cpu_to_le64(src->extent.offset);
  862. dst->extent.length = cpu_to_le64(src->extent.length);
  863. dst->extent.truncate_size =
  864. cpu_to_le64(src->extent.truncate_size);
  865. dst->extent.truncate_seq =
  866. cpu_to_le32(src->extent.truncate_seq);
  867. break;
  868. case CEPH_OSD_OP_CALL:
  869. dst->cls.class_len = src->cls.class_len;
  870. dst->cls.method_len = src->cls.method_len;
  871. dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
  872. break;
  873. case CEPH_OSD_OP_WATCH:
  874. dst->watch.cookie = cpu_to_le64(src->watch.cookie);
  875. dst->watch.ver = cpu_to_le64(0);
  876. dst->watch.op = src->watch.op;
  877. dst->watch.gen = cpu_to_le32(src->watch.gen);
  878. break;
  879. case CEPH_OSD_OP_NOTIFY_ACK:
  880. break;
  881. case CEPH_OSD_OP_NOTIFY:
  882. dst->notify.cookie = cpu_to_le64(src->notify.cookie);
  883. break;
  884. case CEPH_OSD_OP_LIST_WATCHERS:
  885. break;
  886. case CEPH_OSD_OP_SETALLOCHINT:
  887. dst->alloc_hint.expected_object_size =
  888. cpu_to_le64(src->alloc_hint.expected_object_size);
  889. dst->alloc_hint.expected_write_size =
  890. cpu_to_le64(src->alloc_hint.expected_write_size);
  891. dst->alloc_hint.flags = cpu_to_le32(src->alloc_hint.flags);
  892. break;
  893. case CEPH_OSD_OP_SETXATTR:
  894. case CEPH_OSD_OP_CMPXATTR:
  895. dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
  896. dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
  897. dst->xattr.cmp_op = src->xattr.cmp_op;
  898. dst->xattr.cmp_mode = src->xattr.cmp_mode;
  899. break;
  900. case CEPH_OSD_OP_CREATE:
  901. case CEPH_OSD_OP_DELETE:
  902. break;
  903. case CEPH_OSD_OP_COPY_FROM2:
  904. dst->copy_from.snapid = cpu_to_le64(src->copy_from.snapid);
  905. dst->copy_from.src_version =
  906. cpu_to_le64(src->copy_from.src_version);
  907. dst->copy_from.flags = src->copy_from.flags;
  908. dst->copy_from.src_fadvise_flags =
  909. cpu_to_le32(src->copy_from.src_fadvise_flags);
  910. break;
  911. case CEPH_OSD_OP_ASSERT_VER:
  912. dst->assert_ver.unused = cpu_to_le64(0);
  913. dst->assert_ver.ver = cpu_to_le64(src->assert_ver.ver);
  914. break;
  915. default:
  916. pr_err("unsupported osd opcode %s\n",
  917. ceph_osd_op_name(src->op));
  918. WARN_ON(1);
  919. return 0;
  920. }
  921. dst->op = cpu_to_le16(src->op);
  922. dst->flags = cpu_to_le32(src->flags);
  923. dst->payload_len = cpu_to_le32(src->indata_len);
  924. return src->indata_len;
  925. }
  926. /*
  927. * build new request AND message, calculate layout, and adjust file
  928. * extent as needed.
  929. *
  930. * if the file was recently truncated, we include information about its
  931. * old and new size so that the object can be updated appropriately. (we
  932. * avoid synchronously deleting truncated objects because it's slow.)
  933. */
  934. struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
  935. struct ceph_file_layout *layout,
  936. struct ceph_vino vino,
  937. u64 off, u64 *plen,
  938. unsigned int which, int num_ops,
  939. int opcode, int flags,
  940. struct ceph_snap_context *snapc,
  941. u32 truncate_seq,
  942. u64 truncate_size,
  943. bool use_mempool)
  944. {
  945. struct ceph_osd_request *req;
  946. u64 objnum = 0;
  947. u64 objoff = 0;
  948. u64 objlen = 0;
  949. int r;
  950. BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
  951. opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
  952. opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE &&
  953. opcode != CEPH_OSD_OP_SPARSE_READ);
  954. req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
  955. GFP_NOFS);
  956. if (!req) {
  957. r = -ENOMEM;
  958. goto fail;
  959. }
  960. /* calculate max write size */
  961. r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
  962. if (r)
  963. goto fail;
  964. if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
  965. osd_req_op_init(req, which, opcode, 0);
  966. } else {
  967. u32 object_size = layout->object_size;
  968. u32 object_base = off - objoff;
  969. if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
  970. if (truncate_size <= object_base) {
  971. truncate_size = 0;
  972. } else {
  973. truncate_size -= object_base;
  974. if (truncate_size > object_size)
  975. truncate_size = object_size;
  976. }
  977. }
  978. osd_req_op_extent_init(req, which, opcode, objoff, objlen,
  979. truncate_size, truncate_seq);
  980. }
  981. req->r_base_oloc.pool = layout->pool_id;
  982. req->r_base_oloc.pool_ns = ceph_try_get_string(layout->pool_ns);
  983. ceph_oid_printf(&req->r_base_oid, "%llx.%08llx", vino.ino, objnum);
  984. req->r_flags = flags | osdc->client->options->read_from_replica;
  985. req->r_snapid = vino.snap;
  986. if (flags & CEPH_OSD_FLAG_WRITE)
  987. req->r_data_offset = off;
  988. if (num_ops > 1) {
  989. int num_req_ops, num_rep_ops;
  990. /*
  991. * If this is a multi-op write request, assume that we'll need
  992. * request ops. If it's a multi-op read then assume we'll need
  993. * reply ops. Anything else and call it -EINVAL.
  994. */
  995. if (flags & CEPH_OSD_FLAG_WRITE) {
  996. num_req_ops = num_ops;
  997. num_rep_ops = 0;
  998. } else if (flags & CEPH_OSD_FLAG_READ) {
  999. num_req_ops = 0;
  1000. num_rep_ops = num_ops;
  1001. } else {
  1002. r = -EINVAL;
  1003. goto fail;
  1004. }
  1005. r = __ceph_osdc_alloc_messages(req, GFP_NOFS, num_req_ops,
  1006. num_rep_ops);
  1007. } else {
  1008. r = ceph_osdc_alloc_messages(req, GFP_NOFS);
  1009. }
  1010. if (r)
  1011. goto fail;
  1012. return req;
  1013. fail:
  1014. ceph_osdc_put_request(req);
  1015. return ERR_PTR(r);
  1016. }
  1017. EXPORT_SYMBOL(ceph_osdc_new_request);
  1018. int __ceph_alloc_sparse_ext_map(struct ceph_osd_req_op *op, int cnt)
  1019. {
  1020. WARN_ON(op->op != CEPH_OSD_OP_SPARSE_READ);
  1021. op->extent.sparse_ext_cnt = cnt;
  1022. op->extent.sparse_ext = kmalloc_array(cnt,
  1023. sizeof(*op->extent.sparse_ext),
  1024. GFP_NOFS);
  1025. if (!op->extent.sparse_ext)
  1026. return -ENOMEM;
  1027. return 0;
  1028. }
  1029. EXPORT_SYMBOL(__ceph_alloc_sparse_ext_map);
  1030. /*
  1031. * We keep osd requests in an rbtree, sorted by ->r_tid.
  1032. */
  1033. DEFINE_RB_FUNCS(request, struct ceph_osd_request, r_tid, r_node)
  1034. DEFINE_RB_FUNCS(request_mc, struct ceph_osd_request, r_tid, r_mc_node)
  1035. /*
  1036. * Call @fn on each OSD request as long as @fn returns 0.
  1037. */
  1038. static void for_each_request(struct ceph_osd_client *osdc,
  1039. int (*fn)(struct ceph_osd_request *req, void *arg),
  1040. void *arg)
  1041. {
  1042. struct rb_node *n, *p;
  1043. for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
  1044. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  1045. for (p = rb_first(&osd->o_requests); p; ) {
  1046. struct ceph_osd_request *req =
  1047. rb_entry(p, struct ceph_osd_request, r_node);
  1048. p = rb_next(p);
  1049. if (fn(req, arg))
  1050. return;
  1051. }
  1052. }
  1053. for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
  1054. struct ceph_osd_request *req =
  1055. rb_entry(p, struct ceph_osd_request, r_node);
  1056. p = rb_next(p);
  1057. if (fn(req, arg))
  1058. return;
  1059. }
  1060. }
  1061. static bool osd_homeless(struct ceph_osd *osd)
  1062. {
  1063. return osd->o_osd == CEPH_HOMELESS_OSD;
  1064. }
  1065. static bool osd_registered(struct ceph_osd *osd)
  1066. {
  1067. verify_osdc_locked(osd->o_osdc);
  1068. return !RB_EMPTY_NODE(&osd->o_node);
  1069. }
  1070. /*
  1071. * Assumes @osd is zero-initialized.
  1072. */
  1073. static void osd_init(struct ceph_osd *osd)
  1074. {
  1075. refcount_set(&osd->o_ref, 1);
  1076. RB_CLEAR_NODE(&osd->o_node);
  1077. spin_lock_init(&osd->o_requests_lock);
  1078. osd->o_requests = RB_ROOT;
  1079. osd->o_linger_requests = RB_ROOT;
  1080. osd->o_backoff_mappings = RB_ROOT;
  1081. osd->o_backoffs_by_id = RB_ROOT;
  1082. INIT_LIST_HEAD(&osd->o_osd_lru);
  1083. INIT_LIST_HEAD(&osd->o_keepalive_item);
  1084. osd->o_incarnation = 1;
  1085. mutex_init(&osd->lock);
  1086. }
  1087. static void ceph_init_sparse_read(struct ceph_sparse_read *sr)
  1088. {
  1089. kfree(sr->sr_extent);
  1090. memset(sr, '\0', sizeof(*sr));
  1091. sr->sr_state = CEPH_SPARSE_READ_HDR;
  1092. }
  1093. static void osd_cleanup(struct ceph_osd *osd)
  1094. {
  1095. WARN_ON(!RB_EMPTY_NODE(&osd->o_node));
  1096. WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
  1097. WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
  1098. WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoff_mappings));
  1099. WARN_ON(!RB_EMPTY_ROOT(&osd->o_backoffs_by_id));
  1100. WARN_ON(!list_empty(&osd->o_osd_lru));
  1101. WARN_ON(!list_empty(&osd->o_keepalive_item));
  1102. ceph_init_sparse_read(&osd->o_sparse_read);
  1103. if (osd->o_auth.authorizer) {
  1104. WARN_ON(osd_homeless(osd));
  1105. ceph_auth_destroy_authorizer(osd->o_auth.authorizer);
  1106. }
  1107. }
  1108. /*
  1109. * Track open sessions with osds.
  1110. */
  1111. static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
  1112. {
  1113. struct ceph_osd *osd;
  1114. WARN_ON(onum == CEPH_HOMELESS_OSD);
  1115. osd = kzalloc(sizeof(*osd), GFP_NOIO | __GFP_NOFAIL);
  1116. osd_init(osd);
  1117. osd->o_osdc = osdc;
  1118. osd->o_osd = onum;
  1119. osd->o_sparse_op_idx = -1;
  1120. ceph_init_sparse_read(&osd->o_sparse_read);
  1121. ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
  1122. return osd;
  1123. }
  1124. static struct ceph_osd *get_osd(struct ceph_osd *osd)
  1125. {
  1126. if (refcount_inc_not_zero(&osd->o_ref)) {
  1127. dout("get_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref)-1,
  1128. refcount_read(&osd->o_ref));
  1129. return osd;
  1130. } else {
  1131. dout("get_osd %p FAIL\n", osd);
  1132. return NULL;
  1133. }
  1134. }
  1135. static void put_osd(struct ceph_osd *osd)
  1136. {
  1137. dout("put_osd %p %d -> %d\n", osd, refcount_read(&osd->o_ref),
  1138. refcount_read(&osd->o_ref) - 1);
  1139. if (refcount_dec_and_test(&osd->o_ref)) {
  1140. osd_cleanup(osd);
  1141. kfree(osd);
  1142. }
  1143. }
  1144. DEFINE_RB_FUNCS(osd, struct ceph_osd, o_osd, o_node)
  1145. static void __move_osd_to_lru(struct ceph_osd *osd)
  1146. {
  1147. struct ceph_osd_client *osdc = osd->o_osdc;
  1148. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  1149. BUG_ON(!list_empty(&osd->o_osd_lru));
  1150. spin_lock(&osdc->osd_lru_lock);
  1151. list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
  1152. spin_unlock(&osdc->osd_lru_lock);
  1153. osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
  1154. }
  1155. static void maybe_move_osd_to_lru(struct ceph_osd *osd)
  1156. {
  1157. if (RB_EMPTY_ROOT(&osd->o_requests) &&
  1158. RB_EMPTY_ROOT(&osd->o_linger_requests))
  1159. __move_osd_to_lru(osd);
  1160. }
  1161. static void __remove_osd_from_lru(struct ceph_osd *osd)
  1162. {
  1163. struct ceph_osd_client *osdc = osd->o_osdc;
  1164. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  1165. spin_lock(&osdc->osd_lru_lock);
  1166. if (!list_empty(&osd->o_osd_lru))
  1167. list_del_init(&osd->o_osd_lru);
  1168. spin_unlock(&osdc->osd_lru_lock);
  1169. }
  1170. /*
  1171. * Close the connection and assign any leftover requests to the
  1172. * homeless session.
  1173. */
  1174. static void close_osd(struct ceph_osd *osd)
  1175. {
  1176. struct ceph_osd_client *osdc = osd->o_osdc;
  1177. struct rb_node *n;
  1178. verify_osdc_wrlocked(osdc);
  1179. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  1180. ceph_con_close(&osd->o_con);
  1181. for (n = rb_first(&osd->o_requests); n; ) {
  1182. struct ceph_osd_request *req =
  1183. rb_entry(n, struct ceph_osd_request, r_node);
  1184. n = rb_next(n); /* unlink_request() */
  1185. dout(" reassigning req %p tid %llu\n", req, req->r_tid);
  1186. unlink_request(osd, req);
  1187. link_request(&osdc->homeless_osd, req);
  1188. }
  1189. for (n = rb_first(&osd->o_linger_requests); n; ) {
  1190. struct ceph_osd_linger_request *lreq =
  1191. rb_entry(n, struct ceph_osd_linger_request, node);
  1192. n = rb_next(n); /* unlink_linger() */
  1193. dout(" reassigning lreq %p linger_id %llu\n", lreq,
  1194. lreq->linger_id);
  1195. unlink_linger(osd, lreq);
  1196. link_linger(&osdc->homeless_osd, lreq);
  1197. }
  1198. clear_backoffs(osd);
  1199. __remove_osd_from_lru(osd);
  1200. erase_osd(&osdc->osds, osd);
  1201. put_osd(osd);
  1202. }
  1203. /*
  1204. * reset osd connect
  1205. */
  1206. static int reopen_osd(struct ceph_osd *osd)
  1207. {
  1208. struct ceph_entity_addr *peer_addr;
  1209. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  1210. if (RB_EMPTY_ROOT(&osd->o_requests) &&
  1211. RB_EMPTY_ROOT(&osd->o_linger_requests)) {
  1212. close_osd(osd);
  1213. return -ENODEV;
  1214. }
  1215. peer_addr = &osd->o_osdc->osdmap->osd_addr[osd->o_osd];
  1216. if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
  1217. !ceph_con_opened(&osd->o_con)) {
  1218. struct rb_node *n;
  1219. dout("osd addr hasn't changed and connection never opened, "
  1220. "letting msgr retry\n");
  1221. /* touch each r_stamp for handle_timeout()'s benfit */
  1222. for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
  1223. struct ceph_osd_request *req =
  1224. rb_entry(n, struct ceph_osd_request, r_node);
  1225. req->r_stamp = jiffies;
  1226. }
  1227. return -EAGAIN;
  1228. }
  1229. ceph_con_close(&osd->o_con);
  1230. ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
  1231. osd->o_incarnation++;
  1232. return 0;
  1233. }
  1234. static struct ceph_osd *lookup_create_osd(struct ceph_osd_client *osdc, int o,
  1235. bool wrlocked)
  1236. {
  1237. struct ceph_osd *osd;
  1238. if (wrlocked)
  1239. verify_osdc_wrlocked(osdc);
  1240. else
  1241. verify_osdc_locked(osdc);
  1242. if (o != CEPH_HOMELESS_OSD)
  1243. osd = lookup_osd(&osdc->osds, o);
  1244. else
  1245. osd = &osdc->homeless_osd;
  1246. if (!osd) {
  1247. if (!wrlocked)
  1248. return ERR_PTR(-EAGAIN);
  1249. osd = create_osd(osdc, o);
  1250. insert_osd(&osdc->osds, osd);
  1251. ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
  1252. &osdc->osdmap->osd_addr[osd->o_osd]);
  1253. }
  1254. dout("%s osdc %p osd%d -> osd %p\n", __func__, osdc, o, osd);
  1255. return osd;
  1256. }
  1257. /*
  1258. * Create request <-> OSD session relation.
  1259. *
  1260. * @req has to be assigned a tid, @osd may be homeless.
  1261. */
  1262. static void link_request(struct ceph_osd *osd, struct ceph_osd_request *req)
  1263. {
  1264. verify_osd_locked(osd);
  1265. WARN_ON(!req->r_tid || req->r_osd);
  1266. dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
  1267. req, req->r_tid);
  1268. if (!osd_homeless(osd))
  1269. __remove_osd_from_lru(osd);
  1270. else
  1271. atomic_inc(&osd->o_osdc->num_homeless);
  1272. get_osd(osd);
  1273. spin_lock(&osd->o_requests_lock);
  1274. insert_request(&osd->o_requests, req);
  1275. spin_unlock(&osd->o_requests_lock);
  1276. req->r_osd = osd;
  1277. }
  1278. static void unlink_request(struct ceph_osd *osd, struct ceph_osd_request *req)
  1279. {
  1280. verify_osd_locked(osd);
  1281. WARN_ON(req->r_osd != osd);
  1282. dout("%s osd %p osd%d req %p tid %llu\n", __func__, osd, osd->o_osd,
  1283. req, req->r_tid);
  1284. req->r_osd = NULL;
  1285. spin_lock(&osd->o_requests_lock);
  1286. erase_request(&osd->o_requests, req);
  1287. spin_unlock(&osd->o_requests_lock);
  1288. put_osd(osd);
  1289. if (!osd_homeless(osd))
  1290. maybe_move_osd_to_lru(osd);
  1291. else
  1292. atomic_dec(&osd->o_osdc->num_homeless);
  1293. }
  1294. static bool __pool_full(struct ceph_pg_pool_info *pi)
  1295. {
  1296. return pi->flags & CEPH_POOL_FLAG_FULL;
  1297. }
  1298. static bool have_pool_full(struct ceph_osd_client *osdc)
  1299. {
  1300. struct rb_node *n;
  1301. for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
  1302. struct ceph_pg_pool_info *pi =
  1303. rb_entry(n, struct ceph_pg_pool_info, node);
  1304. if (__pool_full(pi))
  1305. return true;
  1306. }
  1307. return false;
  1308. }
  1309. static bool pool_full(struct ceph_osd_client *osdc, s64 pool_id)
  1310. {
  1311. struct ceph_pg_pool_info *pi;
  1312. pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
  1313. if (!pi)
  1314. return false;
  1315. return __pool_full(pi);
  1316. }
  1317. /*
  1318. * Returns whether a request should be blocked from being sent
  1319. * based on the current osdmap and osd_client settings.
  1320. */
  1321. static bool target_should_be_paused(struct ceph_osd_client *osdc,
  1322. const struct ceph_osd_request_target *t,
  1323. struct ceph_pg_pool_info *pi)
  1324. {
  1325. bool pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
  1326. bool pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
  1327. ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  1328. __pool_full(pi);
  1329. WARN_ON(pi->id != t->target_oloc.pool);
  1330. return ((t->flags & CEPH_OSD_FLAG_READ) && pauserd) ||
  1331. ((t->flags & CEPH_OSD_FLAG_WRITE) && pausewr) ||
  1332. (osdc->osdmap->epoch < osdc->epoch_barrier);
  1333. }
  1334. static int pick_random_replica(const struct ceph_osds *acting)
  1335. {
  1336. int i = get_random_u32_below(acting->size);
  1337. dout("%s picked osd%d, primary osd%d\n", __func__,
  1338. acting->osds[i], acting->primary);
  1339. return i;
  1340. }
  1341. /*
  1342. * Picks the closest replica based on client's location given by
  1343. * crush_location option. Prefers the primary if the locality is
  1344. * the same.
  1345. */
  1346. static int pick_closest_replica(struct ceph_osd_client *osdc,
  1347. const struct ceph_osds *acting)
  1348. {
  1349. struct ceph_options *opt = osdc->client->options;
  1350. int best_i, best_locality;
  1351. int i = 0, locality;
  1352. do {
  1353. locality = ceph_get_crush_locality(osdc->osdmap,
  1354. acting->osds[i],
  1355. &opt->crush_locs);
  1356. if (i == 0 ||
  1357. (locality >= 0 && best_locality < 0) ||
  1358. (locality >= 0 && best_locality >= 0 &&
  1359. locality < best_locality)) {
  1360. best_i = i;
  1361. best_locality = locality;
  1362. }
  1363. } while (++i < acting->size);
  1364. dout("%s picked osd%d with locality %d, primary osd%d\n", __func__,
  1365. acting->osds[best_i], best_locality, acting->primary);
  1366. return best_i;
  1367. }
  1368. enum calc_target_result {
  1369. CALC_TARGET_NO_ACTION = 0,
  1370. CALC_TARGET_NEED_RESEND,
  1371. CALC_TARGET_POOL_DNE,
  1372. };
  1373. static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
  1374. struct ceph_osd_request_target *t,
  1375. bool any_change)
  1376. {
  1377. struct ceph_pg_pool_info *pi;
  1378. struct ceph_pg pgid, last_pgid;
  1379. struct ceph_osds up, acting;
  1380. bool is_read = t->flags & CEPH_OSD_FLAG_READ;
  1381. bool is_write = t->flags & CEPH_OSD_FLAG_WRITE;
  1382. bool force_resend = false;
  1383. bool unpaused = false;
  1384. bool legacy_change = false;
  1385. bool split = false;
  1386. bool sort_bitwise = ceph_osdmap_flag(osdc, CEPH_OSDMAP_SORTBITWISE);
  1387. bool recovery_deletes = ceph_osdmap_flag(osdc,
  1388. CEPH_OSDMAP_RECOVERY_DELETES);
  1389. enum calc_target_result ct_res;
  1390. t->epoch = osdc->osdmap->epoch;
  1391. pi = ceph_pg_pool_by_id(osdc->osdmap, t->base_oloc.pool);
  1392. if (!pi) {
  1393. t->osd = CEPH_HOMELESS_OSD;
  1394. ct_res = CALC_TARGET_POOL_DNE;
  1395. goto out;
  1396. }
  1397. if (osdc->osdmap->epoch == pi->last_force_request_resend) {
  1398. if (t->last_force_resend < pi->last_force_request_resend) {
  1399. t->last_force_resend = pi->last_force_request_resend;
  1400. force_resend = true;
  1401. } else if (t->last_force_resend == 0) {
  1402. force_resend = true;
  1403. }
  1404. }
  1405. /* apply tiering */
  1406. ceph_oid_copy(&t->target_oid, &t->base_oid);
  1407. ceph_oloc_copy(&t->target_oloc, &t->base_oloc);
  1408. if ((t->flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
  1409. if (is_read && pi->read_tier >= 0)
  1410. t->target_oloc.pool = pi->read_tier;
  1411. if (is_write && pi->write_tier >= 0)
  1412. t->target_oloc.pool = pi->write_tier;
  1413. pi = ceph_pg_pool_by_id(osdc->osdmap, t->target_oloc.pool);
  1414. if (!pi) {
  1415. t->osd = CEPH_HOMELESS_OSD;
  1416. ct_res = CALC_TARGET_POOL_DNE;
  1417. goto out;
  1418. }
  1419. }
  1420. __ceph_object_locator_to_pg(pi, &t->target_oid, &t->target_oloc, &pgid);
  1421. last_pgid.pool = pgid.pool;
  1422. last_pgid.seed = ceph_stable_mod(pgid.seed, t->pg_num, t->pg_num_mask);
  1423. ceph_pg_to_up_acting_osds(osdc->osdmap, pi, &pgid, &up, &acting);
  1424. if (any_change &&
  1425. ceph_is_new_interval(&t->acting,
  1426. &acting,
  1427. &t->up,
  1428. &up,
  1429. t->size,
  1430. pi->size,
  1431. t->min_size,
  1432. pi->min_size,
  1433. t->pg_num,
  1434. pi->pg_num,
  1435. t->sort_bitwise,
  1436. sort_bitwise,
  1437. t->recovery_deletes,
  1438. recovery_deletes,
  1439. &last_pgid))
  1440. force_resend = true;
  1441. if (t->paused && !target_should_be_paused(osdc, t, pi)) {
  1442. t->paused = false;
  1443. unpaused = true;
  1444. }
  1445. legacy_change = ceph_pg_compare(&t->pgid, &pgid) ||
  1446. ceph_osds_changed(&t->acting, &acting,
  1447. t->used_replica || any_change);
  1448. if (t->pg_num)
  1449. split = ceph_pg_is_split(&last_pgid, t->pg_num, pi->pg_num);
  1450. if (legacy_change || force_resend || split) {
  1451. t->pgid = pgid; /* struct */
  1452. ceph_pg_to_primary_shard(osdc->osdmap, pi, &pgid, &t->spgid);
  1453. ceph_osds_copy(&t->acting, &acting);
  1454. ceph_osds_copy(&t->up, &up);
  1455. t->size = pi->size;
  1456. t->min_size = pi->min_size;
  1457. t->pg_num = pi->pg_num;
  1458. t->pg_num_mask = pi->pg_num_mask;
  1459. t->sort_bitwise = sort_bitwise;
  1460. t->recovery_deletes = recovery_deletes;
  1461. if ((t->flags & (CEPH_OSD_FLAG_BALANCE_READS |
  1462. CEPH_OSD_FLAG_LOCALIZE_READS)) &&
  1463. !is_write && pi->type == CEPH_POOL_TYPE_REP &&
  1464. acting.size > 1) {
  1465. int pos;
  1466. WARN_ON(!is_read || acting.osds[0] != acting.primary);
  1467. if (t->flags & CEPH_OSD_FLAG_BALANCE_READS) {
  1468. pos = pick_random_replica(&acting);
  1469. } else {
  1470. pos = pick_closest_replica(osdc, &acting);
  1471. }
  1472. t->osd = acting.osds[pos];
  1473. t->used_replica = pos > 0;
  1474. } else {
  1475. t->osd = acting.primary;
  1476. t->used_replica = false;
  1477. }
  1478. }
  1479. if (unpaused || legacy_change || force_resend || split)
  1480. ct_res = CALC_TARGET_NEED_RESEND;
  1481. else
  1482. ct_res = CALC_TARGET_NO_ACTION;
  1483. out:
  1484. dout("%s t %p -> %d%d%d%d ct_res %d osd%d\n", __func__, t, unpaused,
  1485. legacy_change, force_resend, split, ct_res, t->osd);
  1486. return ct_res;
  1487. }
  1488. static struct ceph_spg_mapping *alloc_spg_mapping(void)
  1489. {
  1490. struct ceph_spg_mapping *spg;
  1491. spg = kmalloc(sizeof(*spg), GFP_NOIO);
  1492. if (!spg)
  1493. return NULL;
  1494. RB_CLEAR_NODE(&spg->node);
  1495. spg->backoffs = RB_ROOT;
  1496. return spg;
  1497. }
  1498. static void free_spg_mapping(struct ceph_spg_mapping *spg)
  1499. {
  1500. WARN_ON(!RB_EMPTY_NODE(&spg->node));
  1501. WARN_ON(!RB_EMPTY_ROOT(&spg->backoffs));
  1502. kfree(spg);
  1503. }
  1504. /*
  1505. * rbtree of ceph_spg_mapping for handling map<spg_t, ...>, similar to
  1506. * ceph_pg_mapping. Used to track OSD backoffs -- a backoff [range] is
  1507. * defined only within a specific spgid; it does not pass anything to
  1508. * children on split, or to another primary.
  1509. */
  1510. DEFINE_RB_FUNCS2(spg_mapping, struct ceph_spg_mapping, spgid, ceph_spg_compare,
  1511. RB_BYPTR, const struct ceph_spg *, node)
  1512. static u64 hoid_get_bitwise_key(const struct ceph_hobject_id *hoid)
  1513. {
  1514. return hoid->is_max ? 0x100000000ull : hoid->hash_reverse_bits;
  1515. }
  1516. static void hoid_get_effective_key(const struct ceph_hobject_id *hoid,
  1517. void **pkey, size_t *pkey_len)
  1518. {
  1519. if (hoid->key_len) {
  1520. *pkey = hoid->key;
  1521. *pkey_len = hoid->key_len;
  1522. } else {
  1523. *pkey = hoid->oid;
  1524. *pkey_len = hoid->oid_len;
  1525. }
  1526. }
  1527. static int compare_names(const void *name1, size_t name1_len,
  1528. const void *name2, size_t name2_len)
  1529. {
  1530. int ret;
  1531. ret = memcmp(name1, name2, min(name1_len, name2_len));
  1532. if (!ret) {
  1533. if (name1_len < name2_len)
  1534. ret = -1;
  1535. else if (name1_len > name2_len)
  1536. ret = 1;
  1537. }
  1538. return ret;
  1539. }
  1540. static int hoid_compare(const struct ceph_hobject_id *lhs,
  1541. const struct ceph_hobject_id *rhs)
  1542. {
  1543. void *effective_key1, *effective_key2;
  1544. size_t effective_key1_len, effective_key2_len;
  1545. int ret;
  1546. if (lhs->is_max < rhs->is_max)
  1547. return -1;
  1548. if (lhs->is_max > rhs->is_max)
  1549. return 1;
  1550. if (lhs->pool < rhs->pool)
  1551. return -1;
  1552. if (lhs->pool > rhs->pool)
  1553. return 1;
  1554. if (hoid_get_bitwise_key(lhs) < hoid_get_bitwise_key(rhs))
  1555. return -1;
  1556. if (hoid_get_bitwise_key(lhs) > hoid_get_bitwise_key(rhs))
  1557. return 1;
  1558. ret = compare_names(lhs->nspace, lhs->nspace_len,
  1559. rhs->nspace, rhs->nspace_len);
  1560. if (ret)
  1561. return ret;
  1562. hoid_get_effective_key(lhs, &effective_key1, &effective_key1_len);
  1563. hoid_get_effective_key(rhs, &effective_key2, &effective_key2_len);
  1564. ret = compare_names(effective_key1, effective_key1_len,
  1565. effective_key2, effective_key2_len);
  1566. if (ret)
  1567. return ret;
  1568. ret = compare_names(lhs->oid, lhs->oid_len, rhs->oid, rhs->oid_len);
  1569. if (ret)
  1570. return ret;
  1571. if (lhs->snapid < rhs->snapid)
  1572. return -1;
  1573. if (lhs->snapid > rhs->snapid)
  1574. return 1;
  1575. return 0;
  1576. }
  1577. /*
  1578. * For decoding ->begin and ->end of MOSDBackoff only -- no MIN/MAX
  1579. * compat stuff here.
  1580. *
  1581. * Assumes @hoid is zero-initialized.
  1582. */
  1583. static int decode_hoid(void **p, void *end, struct ceph_hobject_id *hoid)
  1584. {
  1585. u8 struct_v;
  1586. u32 struct_len;
  1587. int ret;
  1588. ret = ceph_start_decoding(p, end, 4, "hobject_t", &struct_v,
  1589. &struct_len);
  1590. if (ret)
  1591. return ret;
  1592. if (struct_v < 4) {
  1593. pr_err("got struct_v %d < 4 of hobject_t\n", struct_v);
  1594. goto e_inval;
  1595. }
  1596. hoid->key = ceph_extract_encoded_string(p, end, &hoid->key_len,
  1597. GFP_NOIO);
  1598. if (IS_ERR(hoid->key)) {
  1599. ret = PTR_ERR(hoid->key);
  1600. hoid->key = NULL;
  1601. return ret;
  1602. }
  1603. hoid->oid = ceph_extract_encoded_string(p, end, &hoid->oid_len,
  1604. GFP_NOIO);
  1605. if (IS_ERR(hoid->oid)) {
  1606. ret = PTR_ERR(hoid->oid);
  1607. hoid->oid = NULL;
  1608. return ret;
  1609. }
  1610. ceph_decode_64_safe(p, end, hoid->snapid, e_inval);
  1611. ceph_decode_32_safe(p, end, hoid->hash, e_inval);
  1612. ceph_decode_8_safe(p, end, hoid->is_max, e_inval);
  1613. hoid->nspace = ceph_extract_encoded_string(p, end, &hoid->nspace_len,
  1614. GFP_NOIO);
  1615. if (IS_ERR(hoid->nspace)) {
  1616. ret = PTR_ERR(hoid->nspace);
  1617. hoid->nspace = NULL;
  1618. return ret;
  1619. }
  1620. ceph_decode_64_safe(p, end, hoid->pool, e_inval);
  1621. ceph_hoid_build_hash_cache(hoid);
  1622. return 0;
  1623. e_inval:
  1624. return -EINVAL;
  1625. }
  1626. static int hoid_encoding_size(const struct ceph_hobject_id *hoid)
  1627. {
  1628. return 8 + 4 + 1 + 8 + /* snapid, hash, is_max, pool */
  1629. 4 + hoid->key_len + 4 + hoid->oid_len + 4 + hoid->nspace_len;
  1630. }
  1631. static void encode_hoid(void **p, void *end, const struct ceph_hobject_id *hoid)
  1632. {
  1633. ceph_start_encoding(p, 4, 3, hoid_encoding_size(hoid));
  1634. ceph_encode_string(p, end, hoid->key, hoid->key_len);
  1635. ceph_encode_string(p, end, hoid->oid, hoid->oid_len);
  1636. ceph_encode_64(p, hoid->snapid);
  1637. ceph_encode_32(p, hoid->hash);
  1638. ceph_encode_8(p, hoid->is_max);
  1639. ceph_encode_string(p, end, hoid->nspace, hoid->nspace_len);
  1640. ceph_encode_64(p, hoid->pool);
  1641. }
  1642. static void free_hoid(struct ceph_hobject_id *hoid)
  1643. {
  1644. if (hoid) {
  1645. kfree(hoid->key);
  1646. kfree(hoid->oid);
  1647. kfree(hoid->nspace);
  1648. kfree(hoid);
  1649. }
  1650. }
  1651. static struct ceph_osd_backoff *alloc_backoff(void)
  1652. {
  1653. struct ceph_osd_backoff *backoff;
  1654. backoff = kzalloc(sizeof(*backoff), GFP_NOIO);
  1655. if (!backoff)
  1656. return NULL;
  1657. RB_CLEAR_NODE(&backoff->spg_node);
  1658. RB_CLEAR_NODE(&backoff->id_node);
  1659. return backoff;
  1660. }
  1661. static void free_backoff(struct ceph_osd_backoff *backoff)
  1662. {
  1663. WARN_ON(!RB_EMPTY_NODE(&backoff->spg_node));
  1664. WARN_ON(!RB_EMPTY_NODE(&backoff->id_node));
  1665. free_hoid(backoff->begin);
  1666. free_hoid(backoff->end);
  1667. kfree(backoff);
  1668. }
  1669. /*
  1670. * Within a specific spgid, backoffs are managed by ->begin hoid.
  1671. */
  1672. DEFINE_RB_INSDEL_FUNCS2(backoff, struct ceph_osd_backoff, begin, hoid_compare,
  1673. RB_BYVAL, spg_node);
  1674. static struct ceph_osd_backoff *lookup_containing_backoff(struct rb_root *root,
  1675. const struct ceph_hobject_id *hoid)
  1676. {
  1677. struct rb_node *n = root->rb_node;
  1678. while (n) {
  1679. struct ceph_osd_backoff *cur =
  1680. rb_entry(n, struct ceph_osd_backoff, spg_node);
  1681. int cmp;
  1682. cmp = hoid_compare(hoid, cur->begin);
  1683. if (cmp < 0) {
  1684. n = n->rb_left;
  1685. } else if (cmp > 0) {
  1686. if (hoid_compare(hoid, cur->end) < 0)
  1687. return cur;
  1688. n = n->rb_right;
  1689. } else {
  1690. return cur;
  1691. }
  1692. }
  1693. return NULL;
  1694. }
  1695. /*
  1696. * Each backoff has a unique id within its OSD session.
  1697. */
  1698. DEFINE_RB_FUNCS(backoff_by_id, struct ceph_osd_backoff, id, id_node)
  1699. static void clear_backoffs(struct ceph_osd *osd)
  1700. {
  1701. while (!RB_EMPTY_ROOT(&osd->o_backoff_mappings)) {
  1702. struct ceph_spg_mapping *spg =
  1703. rb_entry(rb_first(&osd->o_backoff_mappings),
  1704. struct ceph_spg_mapping, node);
  1705. while (!RB_EMPTY_ROOT(&spg->backoffs)) {
  1706. struct ceph_osd_backoff *backoff =
  1707. rb_entry(rb_first(&spg->backoffs),
  1708. struct ceph_osd_backoff, spg_node);
  1709. erase_backoff(&spg->backoffs, backoff);
  1710. erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
  1711. free_backoff(backoff);
  1712. }
  1713. erase_spg_mapping(&osd->o_backoff_mappings, spg);
  1714. free_spg_mapping(spg);
  1715. }
  1716. }
  1717. /*
  1718. * Set up a temporary, non-owning view into @t.
  1719. */
  1720. static void hoid_fill_from_target(struct ceph_hobject_id *hoid,
  1721. const struct ceph_osd_request_target *t)
  1722. {
  1723. hoid->key = NULL;
  1724. hoid->key_len = 0;
  1725. hoid->oid = t->target_oid.name;
  1726. hoid->oid_len = t->target_oid.name_len;
  1727. hoid->snapid = CEPH_NOSNAP;
  1728. hoid->hash = t->pgid.seed;
  1729. hoid->is_max = false;
  1730. if (t->target_oloc.pool_ns) {
  1731. hoid->nspace = t->target_oloc.pool_ns->str;
  1732. hoid->nspace_len = t->target_oloc.pool_ns->len;
  1733. } else {
  1734. hoid->nspace = NULL;
  1735. hoid->nspace_len = 0;
  1736. }
  1737. hoid->pool = t->target_oloc.pool;
  1738. ceph_hoid_build_hash_cache(hoid);
  1739. }
  1740. static bool should_plug_request(struct ceph_osd_request *req)
  1741. {
  1742. struct ceph_osd *osd = req->r_osd;
  1743. struct ceph_spg_mapping *spg;
  1744. struct ceph_osd_backoff *backoff;
  1745. struct ceph_hobject_id hoid;
  1746. spg = lookup_spg_mapping(&osd->o_backoff_mappings, &req->r_t.spgid);
  1747. if (!spg)
  1748. return false;
  1749. hoid_fill_from_target(&hoid, &req->r_t);
  1750. backoff = lookup_containing_backoff(&spg->backoffs, &hoid);
  1751. if (!backoff)
  1752. return false;
  1753. dout("%s req %p tid %llu backoff osd%d spgid %llu.%xs%d id %llu\n",
  1754. __func__, req, req->r_tid, osd->o_osd, backoff->spgid.pgid.pool,
  1755. backoff->spgid.pgid.seed, backoff->spgid.shard, backoff->id);
  1756. return true;
  1757. }
  1758. /*
  1759. * Keep get_num_data_items() in sync with this function.
  1760. */
  1761. static void setup_request_data(struct ceph_osd_request *req)
  1762. {
  1763. struct ceph_msg *request_msg = req->r_request;
  1764. struct ceph_msg *reply_msg = req->r_reply;
  1765. struct ceph_osd_req_op *op;
  1766. if (req->r_request->num_data_items || req->r_reply->num_data_items)
  1767. return;
  1768. WARN_ON(request_msg->data_length || reply_msg->data_length);
  1769. for (op = req->r_ops; op != &req->r_ops[req->r_num_ops]; op++) {
  1770. switch (op->op) {
  1771. /* request */
  1772. case CEPH_OSD_OP_WRITE:
  1773. case CEPH_OSD_OP_WRITEFULL:
  1774. WARN_ON(op->indata_len != op->extent.length);
  1775. ceph_osdc_msg_data_add(request_msg,
  1776. &op->extent.osd_data);
  1777. break;
  1778. case CEPH_OSD_OP_SETXATTR:
  1779. case CEPH_OSD_OP_CMPXATTR:
  1780. WARN_ON(op->indata_len != op->xattr.name_len +
  1781. op->xattr.value_len);
  1782. ceph_osdc_msg_data_add(request_msg,
  1783. &op->xattr.osd_data);
  1784. break;
  1785. case CEPH_OSD_OP_NOTIFY_ACK:
  1786. ceph_osdc_msg_data_add(request_msg,
  1787. &op->notify_ack.request_data);
  1788. break;
  1789. case CEPH_OSD_OP_COPY_FROM2:
  1790. ceph_osdc_msg_data_add(request_msg,
  1791. &op->copy_from.osd_data);
  1792. break;
  1793. /* reply */
  1794. case CEPH_OSD_OP_STAT:
  1795. ceph_osdc_msg_data_add(reply_msg,
  1796. &op->raw_data_in);
  1797. break;
  1798. case CEPH_OSD_OP_READ:
  1799. case CEPH_OSD_OP_SPARSE_READ:
  1800. ceph_osdc_msg_data_add(reply_msg,
  1801. &op->extent.osd_data);
  1802. break;
  1803. case CEPH_OSD_OP_LIST_WATCHERS:
  1804. ceph_osdc_msg_data_add(reply_msg,
  1805. &op->list_watchers.response_data);
  1806. break;
  1807. /* both */
  1808. case CEPH_OSD_OP_CALL:
  1809. WARN_ON(op->indata_len != op->cls.class_len +
  1810. op->cls.method_len +
  1811. op->cls.indata_len);
  1812. ceph_osdc_msg_data_add(request_msg,
  1813. &op->cls.request_info);
  1814. /* optional, can be NONE */
  1815. ceph_osdc_msg_data_add(request_msg,
  1816. &op->cls.request_data);
  1817. /* optional, can be NONE */
  1818. ceph_osdc_msg_data_add(reply_msg,
  1819. &op->cls.response_data);
  1820. break;
  1821. case CEPH_OSD_OP_NOTIFY:
  1822. ceph_osdc_msg_data_add(request_msg,
  1823. &op->notify.request_data);
  1824. ceph_osdc_msg_data_add(reply_msg,
  1825. &op->notify.response_data);
  1826. break;
  1827. }
  1828. }
  1829. }
  1830. static void encode_pgid(void **p, const struct ceph_pg *pgid)
  1831. {
  1832. ceph_encode_8(p, 1);
  1833. ceph_encode_64(p, pgid->pool);
  1834. ceph_encode_32(p, pgid->seed);
  1835. ceph_encode_32(p, -1); /* preferred */
  1836. }
  1837. static void encode_spgid(void **p, const struct ceph_spg *spgid)
  1838. {
  1839. ceph_start_encoding(p, 1, 1, CEPH_PGID_ENCODING_LEN + 1);
  1840. encode_pgid(p, &spgid->pgid);
  1841. ceph_encode_8(p, spgid->shard);
  1842. }
  1843. static void encode_oloc(void **p, void *end,
  1844. const struct ceph_object_locator *oloc)
  1845. {
  1846. ceph_start_encoding(p, 5, 4, ceph_oloc_encoding_size(oloc));
  1847. ceph_encode_64(p, oloc->pool);
  1848. ceph_encode_32(p, -1); /* preferred */
  1849. ceph_encode_32(p, 0); /* key len */
  1850. if (oloc->pool_ns)
  1851. ceph_encode_string(p, end, oloc->pool_ns->str,
  1852. oloc->pool_ns->len);
  1853. else
  1854. ceph_encode_32(p, 0);
  1855. }
  1856. static void encode_request_partial(struct ceph_osd_request *req,
  1857. struct ceph_msg *msg)
  1858. {
  1859. void *p = msg->front.iov_base;
  1860. void *const end = p + msg->front_alloc_len;
  1861. u32 data_len = 0;
  1862. int i;
  1863. if (req->r_flags & CEPH_OSD_FLAG_WRITE) {
  1864. /* snapshots aren't writeable */
  1865. WARN_ON(req->r_snapid != CEPH_NOSNAP);
  1866. } else {
  1867. WARN_ON(req->r_mtime.tv_sec || req->r_mtime.tv_nsec ||
  1868. req->r_data_offset || req->r_snapc);
  1869. }
  1870. setup_request_data(req);
  1871. encode_spgid(&p, &req->r_t.spgid); /* actual spg */
  1872. ceph_encode_32(&p, req->r_t.pgid.seed); /* raw hash */
  1873. ceph_encode_32(&p, req->r_osdc->osdmap->epoch);
  1874. ceph_encode_32(&p, req->r_flags);
  1875. /* reqid */
  1876. ceph_start_encoding(&p, 2, 2, sizeof(struct ceph_osd_reqid));
  1877. memset(p, 0, sizeof(struct ceph_osd_reqid));
  1878. p += sizeof(struct ceph_osd_reqid);
  1879. /* trace */
  1880. memset(p, 0, sizeof(struct ceph_blkin_trace_info));
  1881. p += sizeof(struct ceph_blkin_trace_info);
  1882. ceph_encode_32(&p, 0); /* client_inc, always 0 */
  1883. ceph_encode_timespec64(p, &req->r_mtime);
  1884. p += sizeof(struct ceph_timespec);
  1885. encode_oloc(&p, end, &req->r_t.target_oloc);
  1886. ceph_encode_string(&p, end, req->r_t.target_oid.name,
  1887. req->r_t.target_oid.name_len);
  1888. /* ops, can imply data */
  1889. ceph_encode_16(&p, req->r_num_ops);
  1890. for (i = 0; i < req->r_num_ops; i++) {
  1891. data_len += osd_req_encode_op(p, &req->r_ops[i]);
  1892. p += sizeof(struct ceph_osd_op);
  1893. }
  1894. ceph_encode_64(&p, req->r_snapid); /* snapid */
  1895. if (req->r_snapc) {
  1896. ceph_encode_64(&p, req->r_snapc->seq);
  1897. ceph_encode_32(&p, req->r_snapc->num_snaps);
  1898. for (i = 0; i < req->r_snapc->num_snaps; i++)
  1899. ceph_encode_64(&p, req->r_snapc->snaps[i]);
  1900. } else {
  1901. ceph_encode_64(&p, 0); /* snap_seq */
  1902. ceph_encode_32(&p, 0); /* snaps len */
  1903. }
  1904. ceph_encode_32(&p, req->r_attempts); /* retry_attempt */
  1905. BUG_ON(p > end - 8); /* space for features */
  1906. msg->hdr.version = cpu_to_le16(8); /* MOSDOp v8 */
  1907. /* front_len is finalized in encode_request_finish() */
  1908. msg->front.iov_len = p - msg->front.iov_base;
  1909. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1910. msg->hdr.data_len = cpu_to_le32(data_len);
  1911. /*
  1912. * The header "data_off" is a hint to the receiver allowing it
  1913. * to align received data into its buffers such that there's no
  1914. * need to re-copy it before writing it to disk (direct I/O).
  1915. */
  1916. msg->hdr.data_off = cpu_to_le16(req->r_data_offset);
  1917. dout("%s req %p msg %p oid %s oid_len %d\n", __func__, req, msg,
  1918. req->r_t.target_oid.name, req->r_t.target_oid.name_len);
  1919. }
  1920. static void encode_request_finish(struct ceph_msg *msg)
  1921. {
  1922. void *p = msg->front.iov_base;
  1923. void *const partial_end = p + msg->front.iov_len;
  1924. void *const end = p + msg->front_alloc_len;
  1925. if (CEPH_HAVE_FEATURE(msg->con->peer_features, RESEND_ON_SPLIT)) {
  1926. /* luminous OSD -- encode features and be done */
  1927. p = partial_end;
  1928. ceph_encode_64(&p, msg->con->peer_features);
  1929. } else {
  1930. struct {
  1931. char spgid[CEPH_ENCODING_START_BLK_LEN +
  1932. CEPH_PGID_ENCODING_LEN + 1];
  1933. __le32 hash;
  1934. __le32 epoch;
  1935. __le32 flags;
  1936. char reqid[CEPH_ENCODING_START_BLK_LEN +
  1937. sizeof(struct ceph_osd_reqid)];
  1938. char trace[sizeof(struct ceph_blkin_trace_info)];
  1939. __le32 client_inc;
  1940. struct ceph_timespec mtime;
  1941. } __packed head;
  1942. struct ceph_pg pgid;
  1943. void *oloc, *oid, *tail;
  1944. int oloc_len, oid_len, tail_len;
  1945. int len;
  1946. /*
  1947. * Pre-luminous OSD -- reencode v8 into v4 using @head
  1948. * as a temporary buffer. Encode the raw PG; the rest
  1949. * is just a matter of moving oloc, oid and tail blobs
  1950. * around.
  1951. */
  1952. memcpy(&head, p, sizeof(head));
  1953. p += sizeof(head);
  1954. oloc = p;
  1955. p += CEPH_ENCODING_START_BLK_LEN;
  1956. pgid.pool = ceph_decode_64(&p);
  1957. p += 4 + 4; /* preferred, key len */
  1958. len = ceph_decode_32(&p);
  1959. p += len; /* nspace */
  1960. oloc_len = p - oloc;
  1961. oid = p;
  1962. len = ceph_decode_32(&p);
  1963. p += len;
  1964. oid_len = p - oid;
  1965. tail = p;
  1966. tail_len = partial_end - p;
  1967. p = msg->front.iov_base;
  1968. ceph_encode_copy(&p, &head.client_inc, sizeof(head.client_inc));
  1969. ceph_encode_copy(&p, &head.epoch, sizeof(head.epoch));
  1970. ceph_encode_copy(&p, &head.flags, sizeof(head.flags));
  1971. ceph_encode_copy(&p, &head.mtime, sizeof(head.mtime));
  1972. /* reassert_version */
  1973. memset(p, 0, sizeof(struct ceph_eversion));
  1974. p += sizeof(struct ceph_eversion);
  1975. BUG_ON(p >= oloc);
  1976. memmove(p, oloc, oloc_len);
  1977. p += oloc_len;
  1978. pgid.seed = le32_to_cpu(head.hash);
  1979. encode_pgid(&p, &pgid); /* raw pg */
  1980. BUG_ON(p >= oid);
  1981. memmove(p, oid, oid_len);
  1982. p += oid_len;
  1983. /* tail -- ops, snapid, snapc, retry_attempt */
  1984. BUG_ON(p >= tail);
  1985. memmove(p, tail, tail_len);
  1986. p += tail_len;
  1987. msg->hdr.version = cpu_to_le16(4); /* MOSDOp v4 */
  1988. }
  1989. BUG_ON(p > end);
  1990. msg->front.iov_len = p - msg->front.iov_base;
  1991. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  1992. dout("%s msg %p tid %llu %u+%u+%u v%d\n", __func__, msg,
  1993. le64_to_cpu(msg->hdr.tid), le32_to_cpu(msg->hdr.front_len),
  1994. le32_to_cpu(msg->hdr.middle_len), le32_to_cpu(msg->hdr.data_len),
  1995. le16_to_cpu(msg->hdr.version));
  1996. }
  1997. /*
  1998. * @req has to be assigned a tid and registered.
  1999. */
  2000. static void send_request(struct ceph_osd_request *req)
  2001. {
  2002. struct ceph_osd *osd = req->r_osd;
  2003. verify_osd_locked(osd);
  2004. WARN_ON(osd->o_osd != req->r_t.osd);
  2005. /* backoff? */
  2006. if (should_plug_request(req))
  2007. return;
  2008. /*
  2009. * We may have a previously queued request message hanging
  2010. * around. Cancel it to avoid corrupting the msgr.
  2011. */
  2012. if (req->r_sent)
  2013. ceph_msg_revoke(req->r_request);
  2014. req->r_flags |= CEPH_OSD_FLAG_KNOWN_REDIR;
  2015. if (req->r_attempts)
  2016. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  2017. else
  2018. WARN_ON(req->r_flags & CEPH_OSD_FLAG_RETRY);
  2019. encode_request_partial(req, req->r_request);
  2020. dout("%s req %p tid %llu to pgid %llu.%x spgid %llu.%xs%d osd%d e%u flags 0x%x attempt %d\n",
  2021. __func__, req, req->r_tid, req->r_t.pgid.pool, req->r_t.pgid.seed,
  2022. req->r_t.spgid.pgid.pool, req->r_t.spgid.pgid.seed,
  2023. req->r_t.spgid.shard, osd->o_osd, req->r_t.epoch, req->r_flags,
  2024. req->r_attempts);
  2025. req->r_t.paused = false;
  2026. req->r_stamp = jiffies;
  2027. req->r_attempts++;
  2028. req->r_sent = osd->o_incarnation;
  2029. req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
  2030. ceph_con_send(&osd->o_con, ceph_msg_get(req->r_request));
  2031. }
  2032. static void maybe_request_map(struct ceph_osd_client *osdc)
  2033. {
  2034. bool continuous = false;
  2035. verify_osdc_locked(osdc);
  2036. WARN_ON(!osdc->osdmap->epoch);
  2037. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  2038. ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD) ||
  2039. ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
  2040. dout("%s osdc %p continuous\n", __func__, osdc);
  2041. continuous = true;
  2042. } else {
  2043. dout("%s osdc %p onetime\n", __func__, osdc);
  2044. }
  2045. if (ceph_monc_want_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
  2046. osdc->osdmap->epoch + 1, continuous))
  2047. ceph_monc_renew_subs(&osdc->client->monc);
  2048. }
  2049. static void complete_request(struct ceph_osd_request *req, int err);
  2050. static void send_map_check(struct ceph_osd_request *req);
  2051. static void __submit_request(struct ceph_osd_request *req, bool wrlocked)
  2052. {
  2053. struct ceph_osd_client *osdc = req->r_osdc;
  2054. struct ceph_osd *osd;
  2055. enum calc_target_result ct_res;
  2056. int err = 0;
  2057. bool need_send = false;
  2058. bool promoted = false;
  2059. WARN_ON(req->r_tid);
  2060. dout("%s req %p wrlocked %d\n", __func__, req, wrlocked);
  2061. again:
  2062. ct_res = calc_target(osdc, &req->r_t, false);
  2063. if (ct_res == CALC_TARGET_POOL_DNE && !wrlocked)
  2064. goto promote;
  2065. osd = lookup_create_osd(osdc, req->r_t.osd, wrlocked);
  2066. if (IS_ERR(osd)) {
  2067. WARN_ON(PTR_ERR(osd) != -EAGAIN || wrlocked);
  2068. goto promote;
  2069. }
  2070. if (osdc->abort_err) {
  2071. dout("req %p abort_err %d\n", req, osdc->abort_err);
  2072. err = osdc->abort_err;
  2073. } else if (osdc->osdmap->epoch < osdc->epoch_barrier) {
  2074. dout("req %p epoch %u barrier %u\n", req, osdc->osdmap->epoch,
  2075. osdc->epoch_barrier);
  2076. req->r_t.paused = true;
  2077. maybe_request_map(osdc);
  2078. } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
  2079. ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR)) {
  2080. dout("req %p pausewr\n", req);
  2081. req->r_t.paused = true;
  2082. maybe_request_map(osdc);
  2083. } else if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
  2084. ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
  2085. dout("req %p pauserd\n", req);
  2086. req->r_t.paused = true;
  2087. maybe_request_map(osdc);
  2088. } else if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
  2089. !(req->r_flags & (CEPH_OSD_FLAG_FULL_TRY |
  2090. CEPH_OSD_FLAG_FULL_FORCE)) &&
  2091. (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  2092. pool_full(osdc, req->r_t.base_oloc.pool))) {
  2093. dout("req %p full/pool_full\n", req);
  2094. if (ceph_test_opt(osdc->client, ABORT_ON_FULL)) {
  2095. err = -ENOSPC;
  2096. } else {
  2097. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL))
  2098. pr_warn_ratelimited("cluster is full (osdmap FULL)\n");
  2099. else
  2100. pr_warn_ratelimited("pool %lld is full or reached quota\n",
  2101. req->r_t.base_oloc.pool);
  2102. req->r_t.paused = true;
  2103. maybe_request_map(osdc);
  2104. }
  2105. } else if (!osd_homeless(osd)) {
  2106. need_send = true;
  2107. } else {
  2108. maybe_request_map(osdc);
  2109. }
  2110. mutex_lock(&osd->lock);
  2111. /*
  2112. * Assign the tid atomically with send_request() to protect
  2113. * multiple writes to the same object from racing with each
  2114. * other, resulting in out of order ops on the OSDs.
  2115. */
  2116. req->r_tid = atomic64_inc_return(&osdc->last_tid);
  2117. link_request(osd, req);
  2118. if (need_send)
  2119. send_request(req);
  2120. else if (err)
  2121. complete_request(req, err);
  2122. mutex_unlock(&osd->lock);
  2123. if (!err && ct_res == CALC_TARGET_POOL_DNE)
  2124. send_map_check(req);
  2125. if (promoted)
  2126. downgrade_write(&osdc->lock);
  2127. return;
  2128. promote:
  2129. up_read(&osdc->lock);
  2130. down_write(&osdc->lock);
  2131. wrlocked = true;
  2132. promoted = true;
  2133. goto again;
  2134. }
  2135. static void account_request(struct ceph_osd_request *req)
  2136. {
  2137. WARN_ON(req->r_flags & (CEPH_OSD_FLAG_ACK | CEPH_OSD_FLAG_ONDISK));
  2138. WARN_ON(!(req->r_flags & (CEPH_OSD_FLAG_READ | CEPH_OSD_FLAG_WRITE)));
  2139. req->r_flags |= CEPH_OSD_FLAG_ONDISK;
  2140. atomic_inc(&req->r_osdc->num_requests);
  2141. req->r_start_stamp = jiffies;
  2142. req->r_start_latency = ktime_get();
  2143. }
  2144. static void submit_request(struct ceph_osd_request *req, bool wrlocked)
  2145. {
  2146. ceph_osdc_get_request(req);
  2147. account_request(req);
  2148. __submit_request(req, wrlocked);
  2149. }
  2150. static void finish_request(struct ceph_osd_request *req)
  2151. {
  2152. struct ceph_osd_client *osdc = req->r_osdc;
  2153. WARN_ON(lookup_request_mc(&osdc->map_checks, req->r_tid));
  2154. dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
  2155. req->r_end_latency = ktime_get();
  2156. if (req->r_osd) {
  2157. ceph_init_sparse_read(&req->r_osd->o_sparse_read);
  2158. unlink_request(req->r_osd, req);
  2159. }
  2160. atomic_dec(&osdc->num_requests);
  2161. /*
  2162. * If an OSD has failed or returned and a request has been sent
  2163. * twice, it's possible to get a reply and end up here while the
  2164. * request message is queued for delivery. We will ignore the
  2165. * reply, so not a big deal, but better to try and catch it.
  2166. */
  2167. ceph_msg_revoke(req->r_request);
  2168. ceph_msg_revoke_incoming(req->r_reply);
  2169. }
  2170. static void __complete_request(struct ceph_osd_request *req)
  2171. {
  2172. dout("%s req %p tid %llu cb %ps result %d\n", __func__, req,
  2173. req->r_tid, req->r_callback, req->r_result);
  2174. if (req->r_callback)
  2175. req->r_callback(req);
  2176. complete_all(&req->r_completion);
  2177. ceph_osdc_put_request(req);
  2178. }
  2179. static void complete_request_workfn(struct work_struct *work)
  2180. {
  2181. struct ceph_osd_request *req =
  2182. container_of(work, struct ceph_osd_request, r_complete_work);
  2183. __complete_request(req);
  2184. }
  2185. /*
  2186. * This is open-coded in handle_reply().
  2187. */
  2188. static void complete_request(struct ceph_osd_request *req, int err)
  2189. {
  2190. dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
  2191. req->r_result = err;
  2192. finish_request(req);
  2193. INIT_WORK(&req->r_complete_work, complete_request_workfn);
  2194. queue_work(req->r_osdc->completion_wq, &req->r_complete_work);
  2195. }
  2196. static void cancel_map_check(struct ceph_osd_request *req)
  2197. {
  2198. struct ceph_osd_client *osdc = req->r_osdc;
  2199. struct ceph_osd_request *lookup_req;
  2200. verify_osdc_wrlocked(osdc);
  2201. lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
  2202. if (!lookup_req)
  2203. return;
  2204. WARN_ON(lookup_req != req);
  2205. erase_request_mc(&osdc->map_checks, req);
  2206. ceph_osdc_put_request(req);
  2207. }
  2208. static void cancel_request(struct ceph_osd_request *req)
  2209. {
  2210. dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
  2211. cancel_map_check(req);
  2212. finish_request(req);
  2213. complete_all(&req->r_completion);
  2214. ceph_osdc_put_request(req);
  2215. }
  2216. static void abort_request(struct ceph_osd_request *req, int err)
  2217. {
  2218. dout("%s req %p tid %llu err %d\n", __func__, req, req->r_tid, err);
  2219. cancel_map_check(req);
  2220. complete_request(req, err);
  2221. }
  2222. static int abort_fn(struct ceph_osd_request *req, void *arg)
  2223. {
  2224. int err = *(int *)arg;
  2225. abort_request(req, err);
  2226. return 0; /* continue iteration */
  2227. }
  2228. /*
  2229. * Abort all in-flight requests with @err and arrange for all future
  2230. * requests to be failed immediately.
  2231. */
  2232. void ceph_osdc_abort_requests(struct ceph_osd_client *osdc, int err)
  2233. {
  2234. dout("%s osdc %p err %d\n", __func__, osdc, err);
  2235. down_write(&osdc->lock);
  2236. for_each_request(osdc, abort_fn, &err);
  2237. osdc->abort_err = err;
  2238. up_write(&osdc->lock);
  2239. }
  2240. EXPORT_SYMBOL(ceph_osdc_abort_requests);
  2241. void ceph_osdc_clear_abort_err(struct ceph_osd_client *osdc)
  2242. {
  2243. down_write(&osdc->lock);
  2244. osdc->abort_err = 0;
  2245. up_write(&osdc->lock);
  2246. }
  2247. EXPORT_SYMBOL(ceph_osdc_clear_abort_err);
  2248. static void update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
  2249. {
  2250. if (likely(eb > osdc->epoch_barrier)) {
  2251. dout("updating epoch_barrier from %u to %u\n",
  2252. osdc->epoch_barrier, eb);
  2253. osdc->epoch_barrier = eb;
  2254. /* Request map if we're not to the barrier yet */
  2255. if (eb > osdc->osdmap->epoch)
  2256. maybe_request_map(osdc);
  2257. }
  2258. }
  2259. void ceph_osdc_update_epoch_barrier(struct ceph_osd_client *osdc, u32 eb)
  2260. {
  2261. down_read(&osdc->lock);
  2262. if (unlikely(eb > osdc->epoch_barrier)) {
  2263. up_read(&osdc->lock);
  2264. down_write(&osdc->lock);
  2265. update_epoch_barrier(osdc, eb);
  2266. up_write(&osdc->lock);
  2267. } else {
  2268. up_read(&osdc->lock);
  2269. }
  2270. }
  2271. EXPORT_SYMBOL(ceph_osdc_update_epoch_barrier);
  2272. /*
  2273. * We can end up releasing caps as a result of abort_request().
  2274. * In that case, we probably want to ensure that the cap release message
  2275. * has an updated epoch barrier in it, so set the epoch barrier prior to
  2276. * aborting the first request.
  2277. */
  2278. static int abort_on_full_fn(struct ceph_osd_request *req, void *arg)
  2279. {
  2280. struct ceph_osd_client *osdc = req->r_osdc;
  2281. bool *victims = arg;
  2282. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
  2283. (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  2284. pool_full(osdc, req->r_t.base_oloc.pool))) {
  2285. if (!*victims) {
  2286. update_epoch_barrier(osdc, osdc->osdmap->epoch);
  2287. *victims = true;
  2288. }
  2289. abort_request(req, -ENOSPC);
  2290. }
  2291. return 0; /* continue iteration */
  2292. }
  2293. /*
  2294. * Drop all pending requests that are stalled waiting on a full condition to
  2295. * clear, and complete them with ENOSPC as the return code. Set the
  2296. * osdc->epoch_barrier to the latest map epoch that we've seen if any were
  2297. * cancelled.
  2298. */
  2299. static void ceph_osdc_abort_on_full(struct ceph_osd_client *osdc)
  2300. {
  2301. bool victims = false;
  2302. if (ceph_test_opt(osdc->client, ABORT_ON_FULL) &&
  2303. (ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) || have_pool_full(osdc)))
  2304. for_each_request(osdc, abort_on_full_fn, &victims);
  2305. }
  2306. static void check_pool_dne(struct ceph_osd_request *req)
  2307. {
  2308. struct ceph_osd_client *osdc = req->r_osdc;
  2309. struct ceph_osdmap *map = osdc->osdmap;
  2310. verify_osdc_wrlocked(osdc);
  2311. WARN_ON(!map->epoch);
  2312. if (req->r_attempts) {
  2313. /*
  2314. * We sent a request earlier, which means that
  2315. * previously the pool existed, and now it does not
  2316. * (i.e., it was deleted).
  2317. */
  2318. req->r_map_dne_bound = map->epoch;
  2319. dout("%s req %p tid %llu pool disappeared\n", __func__, req,
  2320. req->r_tid);
  2321. } else {
  2322. dout("%s req %p tid %llu map_dne_bound %u have %u\n", __func__,
  2323. req, req->r_tid, req->r_map_dne_bound, map->epoch);
  2324. }
  2325. if (req->r_map_dne_bound) {
  2326. if (map->epoch >= req->r_map_dne_bound) {
  2327. /* we had a new enough map */
  2328. pr_info_ratelimited("tid %llu pool does not exist\n",
  2329. req->r_tid);
  2330. complete_request(req, -ENOENT);
  2331. }
  2332. } else {
  2333. send_map_check(req);
  2334. }
  2335. }
  2336. static void map_check_cb(struct ceph_mon_generic_request *greq)
  2337. {
  2338. struct ceph_osd_client *osdc = &greq->monc->client->osdc;
  2339. struct ceph_osd_request *req;
  2340. u64 tid = greq->private_data;
  2341. WARN_ON(greq->result || !greq->u.newest);
  2342. down_write(&osdc->lock);
  2343. req = lookup_request_mc(&osdc->map_checks, tid);
  2344. if (!req) {
  2345. dout("%s tid %llu dne\n", __func__, tid);
  2346. goto out_unlock;
  2347. }
  2348. dout("%s req %p tid %llu map_dne_bound %u newest %llu\n", __func__,
  2349. req, req->r_tid, req->r_map_dne_bound, greq->u.newest);
  2350. if (!req->r_map_dne_bound)
  2351. req->r_map_dne_bound = greq->u.newest;
  2352. erase_request_mc(&osdc->map_checks, req);
  2353. check_pool_dne(req);
  2354. ceph_osdc_put_request(req);
  2355. out_unlock:
  2356. up_write(&osdc->lock);
  2357. }
  2358. static void send_map_check(struct ceph_osd_request *req)
  2359. {
  2360. struct ceph_osd_client *osdc = req->r_osdc;
  2361. struct ceph_osd_request *lookup_req;
  2362. int ret;
  2363. verify_osdc_wrlocked(osdc);
  2364. lookup_req = lookup_request_mc(&osdc->map_checks, req->r_tid);
  2365. if (lookup_req) {
  2366. WARN_ON(lookup_req != req);
  2367. return;
  2368. }
  2369. ceph_osdc_get_request(req);
  2370. insert_request_mc(&osdc->map_checks, req);
  2371. ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
  2372. map_check_cb, req->r_tid);
  2373. WARN_ON(ret);
  2374. }
  2375. /*
  2376. * lingering requests, watch/notify v2 infrastructure
  2377. */
  2378. static void linger_release(struct kref *kref)
  2379. {
  2380. struct ceph_osd_linger_request *lreq =
  2381. container_of(kref, struct ceph_osd_linger_request, kref);
  2382. dout("%s lreq %p reg_req %p ping_req %p\n", __func__, lreq,
  2383. lreq->reg_req, lreq->ping_req);
  2384. WARN_ON(!RB_EMPTY_NODE(&lreq->node));
  2385. WARN_ON(!RB_EMPTY_NODE(&lreq->osdc_node));
  2386. WARN_ON(!RB_EMPTY_NODE(&lreq->mc_node));
  2387. WARN_ON(!list_empty(&lreq->scan_item));
  2388. WARN_ON(!list_empty(&lreq->pending_lworks));
  2389. WARN_ON(lreq->osd);
  2390. if (lreq->request_pl)
  2391. ceph_pagelist_release(lreq->request_pl);
  2392. if (lreq->notify_id_pages)
  2393. ceph_release_page_vector(lreq->notify_id_pages, 1);
  2394. ceph_osdc_put_request(lreq->reg_req);
  2395. ceph_osdc_put_request(lreq->ping_req);
  2396. target_destroy(&lreq->t);
  2397. kfree(lreq);
  2398. }
  2399. static void linger_put(struct ceph_osd_linger_request *lreq)
  2400. {
  2401. if (lreq)
  2402. kref_put(&lreq->kref, linger_release);
  2403. }
  2404. static struct ceph_osd_linger_request *
  2405. linger_get(struct ceph_osd_linger_request *lreq)
  2406. {
  2407. kref_get(&lreq->kref);
  2408. return lreq;
  2409. }
  2410. static struct ceph_osd_linger_request *
  2411. linger_alloc(struct ceph_osd_client *osdc)
  2412. {
  2413. struct ceph_osd_linger_request *lreq;
  2414. lreq = kzalloc(sizeof(*lreq), GFP_NOIO);
  2415. if (!lreq)
  2416. return NULL;
  2417. kref_init(&lreq->kref);
  2418. mutex_init(&lreq->lock);
  2419. RB_CLEAR_NODE(&lreq->node);
  2420. RB_CLEAR_NODE(&lreq->osdc_node);
  2421. RB_CLEAR_NODE(&lreq->mc_node);
  2422. INIT_LIST_HEAD(&lreq->scan_item);
  2423. INIT_LIST_HEAD(&lreq->pending_lworks);
  2424. init_completion(&lreq->reg_commit_wait);
  2425. init_completion(&lreq->notify_finish_wait);
  2426. lreq->osdc = osdc;
  2427. target_init(&lreq->t);
  2428. dout("%s lreq %p\n", __func__, lreq);
  2429. return lreq;
  2430. }
  2431. DEFINE_RB_INSDEL_FUNCS(linger, struct ceph_osd_linger_request, linger_id, node)
  2432. DEFINE_RB_FUNCS(linger_osdc, struct ceph_osd_linger_request, linger_id, osdc_node)
  2433. DEFINE_RB_FUNCS(linger_mc, struct ceph_osd_linger_request, linger_id, mc_node)
  2434. /*
  2435. * Create linger request <-> OSD session relation.
  2436. *
  2437. * @lreq has to be registered, @osd may be homeless.
  2438. */
  2439. static void link_linger(struct ceph_osd *osd,
  2440. struct ceph_osd_linger_request *lreq)
  2441. {
  2442. verify_osd_locked(osd);
  2443. WARN_ON(!lreq->linger_id || lreq->osd);
  2444. dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
  2445. osd->o_osd, lreq, lreq->linger_id);
  2446. if (!osd_homeless(osd))
  2447. __remove_osd_from_lru(osd);
  2448. else
  2449. atomic_inc(&osd->o_osdc->num_homeless);
  2450. get_osd(osd);
  2451. insert_linger(&osd->o_linger_requests, lreq);
  2452. lreq->osd = osd;
  2453. }
  2454. static void unlink_linger(struct ceph_osd *osd,
  2455. struct ceph_osd_linger_request *lreq)
  2456. {
  2457. verify_osd_locked(osd);
  2458. WARN_ON(lreq->osd != osd);
  2459. dout("%s osd %p osd%d lreq %p linger_id %llu\n", __func__, osd,
  2460. osd->o_osd, lreq, lreq->linger_id);
  2461. lreq->osd = NULL;
  2462. erase_linger(&osd->o_linger_requests, lreq);
  2463. put_osd(osd);
  2464. if (!osd_homeless(osd))
  2465. maybe_move_osd_to_lru(osd);
  2466. else
  2467. atomic_dec(&osd->o_osdc->num_homeless);
  2468. }
  2469. static bool __linger_registered(struct ceph_osd_linger_request *lreq)
  2470. {
  2471. verify_osdc_locked(lreq->osdc);
  2472. return !RB_EMPTY_NODE(&lreq->osdc_node);
  2473. }
  2474. static bool linger_registered(struct ceph_osd_linger_request *lreq)
  2475. {
  2476. struct ceph_osd_client *osdc = lreq->osdc;
  2477. bool registered;
  2478. down_read(&osdc->lock);
  2479. registered = __linger_registered(lreq);
  2480. up_read(&osdc->lock);
  2481. return registered;
  2482. }
  2483. static void linger_register(struct ceph_osd_linger_request *lreq)
  2484. {
  2485. struct ceph_osd_client *osdc = lreq->osdc;
  2486. verify_osdc_wrlocked(osdc);
  2487. WARN_ON(lreq->linger_id);
  2488. linger_get(lreq);
  2489. lreq->linger_id = ++osdc->last_linger_id;
  2490. insert_linger_osdc(&osdc->linger_requests, lreq);
  2491. }
  2492. static void linger_unregister(struct ceph_osd_linger_request *lreq)
  2493. {
  2494. struct ceph_osd_client *osdc = lreq->osdc;
  2495. verify_osdc_wrlocked(osdc);
  2496. erase_linger_osdc(&osdc->linger_requests, lreq);
  2497. linger_put(lreq);
  2498. }
  2499. static void cancel_linger_request(struct ceph_osd_request *req)
  2500. {
  2501. struct ceph_osd_linger_request *lreq = req->r_priv;
  2502. WARN_ON(!req->r_linger);
  2503. cancel_request(req);
  2504. linger_put(lreq);
  2505. }
  2506. struct linger_work {
  2507. struct work_struct work;
  2508. struct ceph_osd_linger_request *lreq;
  2509. struct list_head pending_item;
  2510. unsigned long queued_stamp;
  2511. union {
  2512. struct {
  2513. u64 notify_id;
  2514. u64 notifier_id;
  2515. void *payload; /* points into @msg front */
  2516. size_t payload_len;
  2517. struct ceph_msg *msg; /* for ceph_msg_put() */
  2518. } notify;
  2519. struct {
  2520. int err;
  2521. } error;
  2522. };
  2523. };
  2524. static struct linger_work *lwork_alloc(struct ceph_osd_linger_request *lreq,
  2525. work_func_t workfn)
  2526. {
  2527. struct linger_work *lwork;
  2528. lwork = kzalloc(sizeof(*lwork), GFP_NOIO);
  2529. if (!lwork)
  2530. return NULL;
  2531. INIT_WORK(&lwork->work, workfn);
  2532. INIT_LIST_HEAD(&lwork->pending_item);
  2533. lwork->lreq = linger_get(lreq);
  2534. return lwork;
  2535. }
  2536. static void lwork_free(struct linger_work *lwork)
  2537. {
  2538. struct ceph_osd_linger_request *lreq = lwork->lreq;
  2539. mutex_lock(&lreq->lock);
  2540. list_del(&lwork->pending_item);
  2541. mutex_unlock(&lreq->lock);
  2542. linger_put(lreq);
  2543. kfree(lwork);
  2544. }
  2545. static void lwork_queue(struct linger_work *lwork)
  2546. {
  2547. struct ceph_osd_linger_request *lreq = lwork->lreq;
  2548. struct ceph_osd_client *osdc = lreq->osdc;
  2549. verify_lreq_locked(lreq);
  2550. WARN_ON(!list_empty(&lwork->pending_item));
  2551. lwork->queued_stamp = jiffies;
  2552. list_add_tail(&lwork->pending_item, &lreq->pending_lworks);
  2553. queue_work(osdc->notify_wq, &lwork->work);
  2554. }
  2555. static void do_watch_notify(struct work_struct *w)
  2556. {
  2557. struct linger_work *lwork = container_of(w, struct linger_work, work);
  2558. struct ceph_osd_linger_request *lreq = lwork->lreq;
  2559. if (!linger_registered(lreq)) {
  2560. dout("%s lreq %p not registered\n", __func__, lreq);
  2561. goto out;
  2562. }
  2563. WARN_ON(!lreq->is_watch);
  2564. dout("%s lreq %p notify_id %llu notifier_id %llu payload_len %zu\n",
  2565. __func__, lreq, lwork->notify.notify_id, lwork->notify.notifier_id,
  2566. lwork->notify.payload_len);
  2567. lreq->wcb(lreq->data, lwork->notify.notify_id, lreq->linger_id,
  2568. lwork->notify.notifier_id, lwork->notify.payload,
  2569. lwork->notify.payload_len);
  2570. out:
  2571. ceph_msg_put(lwork->notify.msg);
  2572. lwork_free(lwork);
  2573. }
  2574. static void do_watch_error(struct work_struct *w)
  2575. {
  2576. struct linger_work *lwork = container_of(w, struct linger_work, work);
  2577. struct ceph_osd_linger_request *lreq = lwork->lreq;
  2578. if (!linger_registered(lreq)) {
  2579. dout("%s lreq %p not registered\n", __func__, lreq);
  2580. goto out;
  2581. }
  2582. dout("%s lreq %p err %d\n", __func__, lreq, lwork->error.err);
  2583. lreq->errcb(lreq->data, lreq->linger_id, lwork->error.err);
  2584. out:
  2585. lwork_free(lwork);
  2586. }
  2587. static void queue_watch_error(struct ceph_osd_linger_request *lreq)
  2588. {
  2589. struct linger_work *lwork;
  2590. lwork = lwork_alloc(lreq, do_watch_error);
  2591. if (!lwork) {
  2592. pr_err("failed to allocate error-lwork\n");
  2593. return;
  2594. }
  2595. lwork->error.err = lreq->last_error;
  2596. lwork_queue(lwork);
  2597. }
  2598. static void linger_reg_commit_complete(struct ceph_osd_linger_request *lreq,
  2599. int result)
  2600. {
  2601. if (!completion_done(&lreq->reg_commit_wait)) {
  2602. lreq->reg_commit_error = (result <= 0 ? result : 0);
  2603. complete_all(&lreq->reg_commit_wait);
  2604. }
  2605. }
  2606. static void linger_commit_cb(struct ceph_osd_request *req)
  2607. {
  2608. struct ceph_osd_linger_request *lreq = req->r_priv;
  2609. mutex_lock(&lreq->lock);
  2610. if (req != lreq->reg_req) {
  2611. dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
  2612. __func__, lreq, lreq->linger_id, req, lreq->reg_req);
  2613. goto out;
  2614. }
  2615. dout("%s lreq %p linger_id %llu result %d\n", __func__, lreq,
  2616. lreq->linger_id, req->r_result);
  2617. linger_reg_commit_complete(lreq, req->r_result);
  2618. lreq->committed = true;
  2619. if (!lreq->is_watch) {
  2620. struct ceph_osd_data *osd_data =
  2621. osd_req_op_data(req, 0, notify, response_data);
  2622. void *p = page_address(osd_data->pages[0]);
  2623. WARN_ON(req->r_ops[0].op != CEPH_OSD_OP_NOTIFY ||
  2624. osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
  2625. /* make note of the notify_id */
  2626. if (req->r_ops[0].outdata_len >= sizeof(u64)) {
  2627. lreq->notify_id = ceph_decode_64(&p);
  2628. dout("lreq %p notify_id %llu\n", lreq,
  2629. lreq->notify_id);
  2630. } else {
  2631. dout("lreq %p no notify_id\n", lreq);
  2632. }
  2633. }
  2634. out:
  2635. mutex_unlock(&lreq->lock);
  2636. linger_put(lreq);
  2637. }
  2638. static int normalize_watch_error(int err)
  2639. {
  2640. /*
  2641. * Translate ENOENT -> ENOTCONN so that a delete->disconnection
  2642. * notification and a failure to reconnect because we raced with
  2643. * the delete appear the same to the user.
  2644. */
  2645. if (err == -ENOENT)
  2646. err = -ENOTCONN;
  2647. return err;
  2648. }
  2649. static void linger_reconnect_cb(struct ceph_osd_request *req)
  2650. {
  2651. struct ceph_osd_linger_request *lreq = req->r_priv;
  2652. mutex_lock(&lreq->lock);
  2653. if (req != lreq->reg_req) {
  2654. dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
  2655. __func__, lreq, lreq->linger_id, req, lreq->reg_req);
  2656. goto out;
  2657. }
  2658. dout("%s lreq %p linger_id %llu result %d last_error %d\n", __func__,
  2659. lreq, lreq->linger_id, req->r_result, lreq->last_error);
  2660. if (req->r_result < 0) {
  2661. if (!lreq->last_error) {
  2662. lreq->last_error = normalize_watch_error(req->r_result);
  2663. queue_watch_error(lreq);
  2664. }
  2665. }
  2666. out:
  2667. mutex_unlock(&lreq->lock);
  2668. linger_put(lreq);
  2669. }
  2670. static void send_linger(struct ceph_osd_linger_request *lreq)
  2671. {
  2672. struct ceph_osd_client *osdc = lreq->osdc;
  2673. struct ceph_osd_request *req;
  2674. int ret;
  2675. verify_osdc_wrlocked(osdc);
  2676. mutex_lock(&lreq->lock);
  2677. dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
  2678. if (lreq->reg_req) {
  2679. if (lreq->reg_req->r_osd)
  2680. cancel_linger_request(lreq->reg_req);
  2681. ceph_osdc_put_request(lreq->reg_req);
  2682. }
  2683. req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
  2684. BUG_ON(!req);
  2685. target_copy(&req->r_t, &lreq->t);
  2686. req->r_mtime = lreq->mtime;
  2687. if (lreq->is_watch && lreq->committed) {
  2688. osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_RECONNECT,
  2689. lreq->linger_id, ++lreq->register_gen);
  2690. dout("lreq %p reconnect register_gen %u\n", lreq,
  2691. req->r_ops[0].watch.gen);
  2692. req->r_callback = linger_reconnect_cb;
  2693. } else {
  2694. if (lreq->is_watch) {
  2695. osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_WATCH,
  2696. lreq->linger_id, 0);
  2697. } else {
  2698. lreq->notify_id = 0;
  2699. refcount_inc(&lreq->request_pl->refcnt);
  2700. osd_req_op_notify_init(req, 0, lreq->linger_id,
  2701. lreq->request_pl);
  2702. ceph_osd_data_pages_init(
  2703. osd_req_op_data(req, 0, notify, response_data),
  2704. lreq->notify_id_pages, PAGE_SIZE, 0, false, false);
  2705. }
  2706. dout("lreq %p register\n", lreq);
  2707. req->r_callback = linger_commit_cb;
  2708. }
  2709. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  2710. BUG_ON(ret);
  2711. req->r_priv = linger_get(lreq);
  2712. req->r_linger = true;
  2713. lreq->reg_req = req;
  2714. mutex_unlock(&lreq->lock);
  2715. submit_request(req, true);
  2716. }
  2717. static void linger_ping_cb(struct ceph_osd_request *req)
  2718. {
  2719. struct ceph_osd_linger_request *lreq = req->r_priv;
  2720. mutex_lock(&lreq->lock);
  2721. if (req != lreq->ping_req) {
  2722. dout("%s lreq %p linger_id %llu unknown req (%p != %p)\n",
  2723. __func__, lreq, lreq->linger_id, req, lreq->ping_req);
  2724. goto out;
  2725. }
  2726. dout("%s lreq %p linger_id %llu result %d ping_sent %lu last_error %d\n",
  2727. __func__, lreq, lreq->linger_id, req->r_result, lreq->ping_sent,
  2728. lreq->last_error);
  2729. if (lreq->register_gen == req->r_ops[0].watch.gen) {
  2730. if (!req->r_result) {
  2731. lreq->watch_valid_thru = lreq->ping_sent;
  2732. } else if (!lreq->last_error) {
  2733. lreq->last_error = normalize_watch_error(req->r_result);
  2734. queue_watch_error(lreq);
  2735. }
  2736. } else {
  2737. dout("lreq %p register_gen %u ignoring old pong %u\n", lreq,
  2738. lreq->register_gen, req->r_ops[0].watch.gen);
  2739. }
  2740. out:
  2741. mutex_unlock(&lreq->lock);
  2742. linger_put(lreq);
  2743. }
  2744. static void send_linger_ping(struct ceph_osd_linger_request *lreq)
  2745. {
  2746. struct ceph_osd_client *osdc = lreq->osdc;
  2747. struct ceph_osd_request *req;
  2748. int ret;
  2749. if (ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD)) {
  2750. dout("%s PAUSERD\n", __func__);
  2751. return;
  2752. }
  2753. lreq->ping_sent = jiffies;
  2754. dout("%s lreq %p linger_id %llu ping_sent %lu register_gen %u\n",
  2755. __func__, lreq, lreq->linger_id, lreq->ping_sent,
  2756. lreq->register_gen);
  2757. if (lreq->ping_req) {
  2758. if (lreq->ping_req->r_osd)
  2759. cancel_linger_request(lreq->ping_req);
  2760. ceph_osdc_put_request(lreq->ping_req);
  2761. }
  2762. req = ceph_osdc_alloc_request(osdc, NULL, 1, true, GFP_NOIO);
  2763. BUG_ON(!req);
  2764. target_copy(&req->r_t, &lreq->t);
  2765. osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_PING, lreq->linger_id,
  2766. lreq->register_gen);
  2767. req->r_callback = linger_ping_cb;
  2768. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  2769. BUG_ON(ret);
  2770. req->r_priv = linger_get(lreq);
  2771. req->r_linger = true;
  2772. lreq->ping_req = req;
  2773. ceph_osdc_get_request(req);
  2774. account_request(req);
  2775. req->r_tid = atomic64_inc_return(&osdc->last_tid);
  2776. link_request(lreq->osd, req);
  2777. send_request(req);
  2778. }
  2779. static void linger_submit(struct ceph_osd_linger_request *lreq)
  2780. {
  2781. struct ceph_osd_client *osdc = lreq->osdc;
  2782. struct ceph_osd *osd;
  2783. down_write(&osdc->lock);
  2784. linger_register(lreq);
  2785. calc_target(osdc, &lreq->t, false);
  2786. osd = lookup_create_osd(osdc, lreq->t.osd, true);
  2787. link_linger(osd, lreq);
  2788. send_linger(lreq);
  2789. up_write(&osdc->lock);
  2790. }
  2791. static void cancel_linger_map_check(struct ceph_osd_linger_request *lreq)
  2792. {
  2793. struct ceph_osd_client *osdc = lreq->osdc;
  2794. struct ceph_osd_linger_request *lookup_lreq;
  2795. verify_osdc_wrlocked(osdc);
  2796. lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
  2797. lreq->linger_id);
  2798. if (!lookup_lreq)
  2799. return;
  2800. WARN_ON(lookup_lreq != lreq);
  2801. erase_linger_mc(&osdc->linger_map_checks, lreq);
  2802. linger_put(lreq);
  2803. }
  2804. /*
  2805. * @lreq has to be both registered and linked.
  2806. */
  2807. static void __linger_cancel(struct ceph_osd_linger_request *lreq)
  2808. {
  2809. if (lreq->ping_req && lreq->ping_req->r_osd)
  2810. cancel_linger_request(lreq->ping_req);
  2811. if (lreq->reg_req && lreq->reg_req->r_osd)
  2812. cancel_linger_request(lreq->reg_req);
  2813. cancel_linger_map_check(lreq);
  2814. unlink_linger(lreq->osd, lreq);
  2815. linger_unregister(lreq);
  2816. }
  2817. static void linger_cancel(struct ceph_osd_linger_request *lreq)
  2818. {
  2819. struct ceph_osd_client *osdc = lreq->osdc;
  2820. down_write(&osdc->lock);
  2821. if (__linger_registered(lreq))
  2822. __linger_cancel(lreq);
  2823. up_write(&osdc->lock);
  2824. }
  2825. static void send_linger_map_check(struct ceph_osd_linger_request *lreq);
  2826. static void check_linger_pool_dne(struct ceph_osd_linger_request *lreq)
  2827. {
  2828. struct ceph_osd_client *osdc = lreq->osdc;
  2829. struct ceph_osdmap *map = osdc->osdmap;
  2830. verify_osdc_wrlocked(osdc);
  2831. WARN_ON(!map->epoch);
  2832. if (lreq->register_gen) {
  2833. lreq->map_dne_bound = map->epoch;
  2834. dout("%s lreq %p linger_id %llu pool disappeared\n", __func__,
  2835. lreq, lreq->linger_id);
  2836. } else {
  2837. dout("%s lreq %p linger_id %llu map_dne_bound %u have %u\n",
  2838. __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
  2839. map->epoch);
  2840. }
  2841. if (lreq->map_dne_bound) {
  2842. if (map->epoch >= lreq->map_dne_bound) {
  2843. /* we had a new enough map */
  2844. pr_info("linger_id %llu pool does not exist\n",
  2845. lreq->linger_id);
  2846. linger_reg_commit_complete(lreq, -ENOENT);
  2847. __linger_cancel(lreq);
  2848. }
  2849. } else {
  2850. send_linger_map_check(lreq);
  2851. }
  2852. }
  2853. static void linger_map_check_cb(struct ceph_mon_generic_request *greq)
  2854. {
  2855. struct ceph_osd_client *osdc = &greq->monc->client->osdc;
  2856. struct ceph_osd_linger_request *lreq;
  2857. u64 linger_id = greq->private_data;
  2858. WARN_ON(greq->result || !greq->u.newest);
  2859. down_write(&osdc->lock);
  2860. lreq = lookup_linger_mc(&osdc->linger_map_checks, linger_id);
  2861. if (!lreq) {
  2862. dout("%s linger_id %llu dne\n", __func__, linger_id);
  2863. goto out_unlock;
  2864. }
  2865. dout("%s lreq %p linger_id %llu map_dne_bound %u newest %llu\n",
  2866. __func__, lreq, lreq->linger_id, lreq->map_dne_bound,
  2867. greq->u.newest);
  2868. if (!lreq->map_dne_bound)
  2869. lreq->map_dne_bound = greq->u.newest;
  2870. erase_linger_mc(&osdc->linger_map_checks, lreq);
  2871. check_linger_pool_dne(lreq);
  2872. linger_put(lreq);
  2873. out_unlock:
  2874. up_write(&osdc->lock);
  2875. }
  2876. static void send_linger_map_check(struct ceph_osd_linger_request *lreq)
  2877. {
  2878. struct ceph_osd_client *osdc = lreq->osdc;
  2879. struct ceph_osd_linger_request *lookup_lreq;
  2880. int ret;
  2881. verify_osdc_wrlocked(osdc);
  2882. lookup_lreq = lookup_linger_mc(&osdc->linger_map_checks,
  2883. lreq->linger_id);
  2884. if (lookup_lreq) {
  2885. WARN_ON(lookup_lreq != lreq);
  2886. return;
  2887. }
  2888. linger_get(lreq);
  2889. insert_linger_mc(&osdc->linger_map_checks, lreq);
  2890. ret = ceph_monc_get_version_async(&osdc->client->monc, "osdmap",
  2891. linger_map_check_cb, lreq->linger_id);
  2892. WARN_ON(ret);
  2893. }
  2894. static int linger_reg_commit_wait(struct ceph_osd_linger_request *lreq)
  2895. {
  2896. int ret;
  2897. dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
  2898. ret = wait_for_completion_killable(&lreq->reg_commit_wait);
  2899. return ret ?: lreq->reg_commit_error;
  2900. }
  2901. static int linger_notify_finish_wait(struct ceph_osd_linger_request *lreq,
  2902. unsigned long timeout)
  2903. {
  2904. long left;
  2905. dout("%s lreq %p linger_id %llu\n", __func__, lreq, lreq->linger_id);
  2906. left = wait_for_completion_killable_timeout(&lreq->notify_finish_wait,
  2907. ceph_timeout_jiffies(timeout));
  2908. if (left <= 0)
  2909. left = left ?: -ETIMEDOUT;
  2910. else
  2911. left = lreq->notify_finish_error; /* completed */
  2912. return left;
  2913. }
  2914. /*
  2915. * Timeout callback, called every N seconds. When 1 or more OSD
  2916. * requests has been active for more than N seconds, we send a keepalive
  2917. * (tag + timestamp) to its OSD to ensure any communications channel
  2918. * reset is detected.
  2919. */
  2920. static void handle_timeout(struct work_struct *work)
  2921. {
  2922. struct ceph_osd_client *osdc =
  2923. container_of(work, struct ceph_osd_client, timeout_work.work);
  2924. struct ceph_options *opts = osdc->client->options;
  2925. unsigned long cutoff = jiffies - opts->osd_keepalive_timeout;
  2926. unsigned long expiry_cutoff = jiffies - opts->osd_request_timeout;
  2927. LIST_HEAD(slow_osds);
  2928. struct rb_node *n, *p;
  2929. dout("%s osdc %p\n", __func__, osdc);
  2930. down_write(&osdc->lock);
  2931. /*
  2932. * ping osds that are a bit slow. this ensures that if there
  2933. * is a break in the TCP connection we will notice, and reopen
  2934. * a connection with that osd (from the fault callback).
  2935. */
  2936. for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
  2937. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  2938. bool found = false;
  2939. for (p = rb_first(&osd->o_requests); p; ) {
  2940. struct ceph_osd_request *req =
  2941. rb_entry(p, struct ceph_osd_request, r_node);
  2942. p = rb_next(p); /* abort_request() */
  2943. if (time_before(req->r_stamp, cutoff)) {
  2944. dout(" req %p tid %llu on osd%d is laggy\n",
  2945. req, req->r_tid, osd->o_osd);
  2946. found = true;
  2947. }
  2948. if (opts->osd_request_timeout &&
  2949. time_before(req->r_start_stamp, expiry_cutoff)) {
  2950. pr_err_ratelimited("tid %llu on osd%d timeout\n",
  2951. req->r_tid, osd->o_osd);
  2952. abort_request(req, -ETIMEDOUT);
  2953. }
  2954. }
  2955. for (p = rb_first(&osd->o_linger_requests); p; p = rb_next(p)) {
  2956. struct ceph_osd_linger_request *lreq =
  2957. rb_entry(p, struct ceph_osd_linger_request, node);
  2958. dout(" lreq %p linger_id %llu is served by osd%d\n",
  2959. lreq, lreq->linger_id, osd->o_osd);
  2960. found = true;
  2961. mutex_lock(&lreq->lock);
  2962. if (lreq->is_watch && lreq->committed && !lreq->last_error)
  2963. send_linger_ping(lreq);
  2964. mutex_unlock(&lreq->lock);
  2965. }
  2966. if (found)
  2967. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  2968. }
  2969. if (opts->osd_request_timeout) {
  2970. for (p = rb_first(&osdc->homeless_osd.o_requests); p; ) {
  2971. struct ceph_osd_request *req =
  2972. rb_entry(p, struct ceph_osd_request, r_node);
  2973. p = rb_next(p); /* abort_request() */
  2974. if (time_before(req->r_start_stamp, expiry_cutoff)) {
  2975. pr_err_ratelimited("tid %llu on osd%d timeout\n",
  2976. req->r_tid, osdc->homeless_osd.o_osd);
  2977. abort_request(req, -ETIMEDOUT);
  2978. }
  2979. }
  2980. }
  2981. if (atomic_read(&osdc->num_homeless) || !list_empty(&slow_osds))
  2982. maybe_request_map(osdc);
  2983. while (!list_empty(&slow_osds)) {
  2984. struct ceph_osd *osd = list_first_entry(&slow_osds,
  2985. struct ceph_osd,
  2986. o_keepalive_item);
  2987. list_del_init(&osd->o_keepalive_item);
  2988. ceph_con_keepalive(&osd->o_con);
  2989. }
  2990. up_write(&osdc->lock);
  2991. schedule_delayed_work(&osdc->timeout_work,
  2992. osdc->client->options->osd_keepalive_timeout);
  2993. }
  2994. static void handle_osds_timeout(struct work_struct *work)
  2995. {
  2996. struct ceph_osd_client *osdc =
  2997. container_of(work, struct ceph_osd_client,
  2998. osds_timeout_work.work);
  2999. unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
  3000. struct ceph_osd *osd, *nosd;
  3001. dout("%s osdc %p\n", __func__, osdc);
  3002. down_write(&osdc->lock);
  3003. list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
  3004. if (time_before(jiffies, osd->lru_ttl))
  3005. break;
  3006. WARN_ON(!RB_EMPTY_ROOT(&osd->o_requests));
  3007. WARN_ON(!RB_EMPTY_ROOT(&osd->o_linger_requests));
  3008. close_osd(osd);
  3009. }
  3010. up_write(&osdc->lock);
  3011. schedule_delayed_work(&osdc->osds_timeout_work,
  3012. round_jiffies_relative(delay));
  3013. }
  3014. static int ceph_oloc_decode(void **p, void *end,
  3015. struct ceph_object_locator *oloc)
  3016. {
  3017. u8 struct_v, struct_cv;
  3018. u32 len;
  3019. void *struct_end;
  3020. int ret = 0;
  3021. ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
  3022. struct_v = ceph_decode_8(p);
  3023. struct_cv = ceph_decode_8(p);
  3024. if (struct_v < 3) {
  3025. pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
  3026. struct_v, struct_cv);
  3027. goto e_inval;
  3028. }
  3029. if (struct_cv > 6) {
  3030. pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
  3031. struct_v, struct_cv);
  3032. goto e_inval;
  3033. }
  3034. len = ceph_decode_32(p);
  3035. ceph_decode_need(p, end, len, e_inval);
  3036. struct_end = *p + len;
  3037. oloc->pool = ceph_decode_64(p);
  3038. *p += 4; /* skip preferred */
  3039. len = ceph_decode_32(p);
  3040. if (len > 0) {
  3041. pr_warn("ceph_object_locator::key is set\n");
  3042. goto e_inval;
  3043. }
  3044. if (struct_v >= 5) {
  3045. bool changed = false;
  3046. len = ceph_decode_32(p);
  3047. if (len > 0) {
  3048. ceph_decode_need(p, end, len, e_inval);
  3049. if (!oloc->pool_ns ||
  3050. ceph_compare_string(oloc->pool_ns, *p, len))
  3051. changed = true;
  3052. *p += len;
  3053. } else {
  3054. if (oloc->pool_ns)
  3055. changed = true;
  3056. }
  3057. if (changed) {
  3058. /* redirect changes namespace */
  3059. pr_warn("ceph_object_locator::nspace is changed\n");
  3060. goto e_inval;
  3061. }
  3062. }
  3063. if (struct_v >= 6) {
  3064. s64 hash = ceph_decode_64(p);
  3065. if (hash != -1) {
  3066. pr_warn("ceph_object_locator::hash is set\n");
  3067. goto e_inval;
  3068. }
  3069. }
  3070. /* skip the rest */
  3071. *p = struct_end;
  3072. out:
  3073. return ret;
  3074. e_inval:
  3075. ret = -EINVAL;
  3076. goto out;
  3077. }
  3078. static int ceph_redirect_decode(void **p, void *end,
  3079. struct ceph_request_redirect *redir)
  3080. {
  3081. u8 struct_v, struct_cv;
  3082. u32 len;
  3083. void *struct_end;
  3084. int ret;
  3085. ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
  3086. struct_v = ceph_decode_8(p);
  3087. struct_cv = ceph_decode_8(p);
  3088. if (struct_cv > 1) {
  3089. pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
  3090. struct_v, struct_cv);
  3091. goto e_inval;
  3092. }
  3093. len = ceph_decode_32(p);
  3094. ceph_decode_need(p, end, len, e_inval);
  3095. struct_end = *p + len;
  3096. ret = ceph_oloc_decode(p, end, &redir->oloc);
  3097. if (ret)
  3098. goto out;
  3099. len = ceph_decode_32(p);
  3100. if (len > 0) {
  3101. pr_warn("ceph_request_redirect::object_name is set\n");
  3102. goto e_inval;
  3103. }
  3104. /* skip the rest */
  3105. *p = struct_end;
  3106. out:
  3107. return ret;
  3108. e_inval:
  3109. ret = -EINVAL;
  3110. goto out;
  3111. }
  3112. struct MOSDOpReply {
  3113. struct ceph_pg pgid;
  3114. u64 flags;
  3115. int result;
  3116. u32 epoch;
  3117. int num_ops;
  3118. u32 outdata_len[CEPH_OSD_MAX_OPS];
  3119. s32 rval[CEPH_OSD_MAX_OPS];
  3120. int retry_attempt;
  3121. struct ceph_eversion replay_version;
  3122. u64 user_version;
  3123. struct ceph_request_redirect redirect;
  3124. };
  3125. static int decode_MOSDOpReply(const struct ceph_msg *msg, struct MOSDOpReply *m)
  3126. {
  3127. void *p = msg->front.iov_base;
  3128. void *const end = p + msg->front.iov_len;
  3129. u16 version = le16_to_cpu(msg->hdr.version);
  3130. struct ceph_eversion bad_replay_version;
  3131. u8 decode_redir;
  3132. u32 len;
  3133. int ret;
  3134. int i;
  3135. ceph_decode_32_safe(&p, end, len, e_inval);
  3136. ceph_decode_need(&p, end, len, e_inval);
  3137. p += len; /* skip oid */
  3138. ret = ceph_decode_pgid(&p, end, &m->pgid);
  3139. if (ret)
  3140. return ret;
  3141. ceph_decode_64_safe(&p, end, m->flags, e_inval);
  3142. ceph_decode_32_safe(&p, end, m->result, e_inval);
  3143. ceph_decode_need(&p, end, sizeof(bad_replay_version), e_inval);
  3144. memcpy(&bad_replay_version, p, sizeof(bad_replay_version));
  3145. p += sizeof(bad_replay_version);
  3146. ceph_decode_32_safe(&p, end, m->epoch, e_inval);
  3147. ceph_decode_32_safe(&p, end, m->num_ops, e_inval);
  3148. if (m->num_ops > ARRAY_SIZE(m->outdata_len))
  3149. goto e_inval;
  3150. ceph_decode_need(&p, end, m->num_ops * sizeof(struct ceph_osd_op),
  3151. e_inval);
  3152. for (i = 0; i < m->num_ops; i++) {
  3153. struct ceph_osd_op *op = p;
  3154. m->outdata_len[i] = le32_to_cpu(op->payload_len);
  3155. p += sizeof(*op);
  3156. }
  3157. ceph_decode_32_safe(&p, end, m->retry_attempt, e_inval);
  3158. for (i = 0; i < m->num_ops; i++)
  3159. ceph_decode_32_safe(&p, end, m->rval[i], e_inval);
  3160. if (version >= 5) {
  3161. ceph_decode_need(&p, end, sizeof(m->replay_version), e_inval);
  3162. memcpy(&m->replay_version, p, sizeof(m->replay_version));
  3163. p += sizeof(m->replay_version);
  3164. ceph_decode_64_safe(&p, end, m->user_version, e_inval);
  3165. } else {
  3166. m->replay_version = bad_replay_version; /* struct */
  3167. m->user_version = le64_to_cpu(m->replay_version.version);
  3168. }
  3169. if (version >= 6) {
  3170. if (version >= 7)
  3171. ceph_decode_8_safe(&p, end, decode_redir, e_inval);
  3172. else
  3173. decode_redir = 1;
  3174. } else {
  3175. decode_redir = 0;
  3176. }
  3177. if (decode_redir) {
  3178. ret = ceph_redirect_decode(&p, end, &m->redirect);
  3179. if (ret)
  3180. return ret;
  3181. } else {
  3182. ceph_oloc_init(&m->redirect.oloc);
  3183. }
  3184. return 0;
  3185. e_inval:
  3186. return -EINVAL;
  3187. }
  3188. /*
  3189. * Handle MOSDOpReply. Set ->r_result and call the callback if it is
  3190. * specified.
  3191. */
  3192. static void handle_reply(struct ceph_osd *osd, struct ceph_msg *msg)
  3193. {
  3194. struct ceph_osd_client *osdc = osd->o_osdc;
  3195. struct ceph_osd_request *req;
  3196. struct MOSDOpReply m;
  3197. u64 tid = le64_to_cpu(msg->hdr.tid);
  3198. u32 data_len = 0;
  3199. int ret;
  3200. int i;
  3201. dout("%s msg %p tid %llu\n", __func__, msg, tid);
  3202. down_read(&osdc->lock);
  3203. if (!osd_registered(osd)) {
  3204. dout("%s osd%d unknown\n", __func__, osd->o_osd);
  3205. goto out_unlock_osdc;
  3206. }
  3207. WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
  3208. mutex_lock(&osd->lock);
  3209. req = lookup_request(&osd->o_requests, tid);
  3210. if (!req) {
  3211. dout("%s osd%d tid %llu unknown\n", __func__, osd->o_osd, tid);
  3212. goto out_unlock_session;
  3213. }
  3214. m.redirect.oloc.pool_ns = req->r_t.target_oloc.pool_ns;
  3215. ret = decode_MOSDOpReply(msg, &m);
  3216. m.redirect.oloc.pool_ns = NULL;
  3217. if (ret) {
  3218. pr_err("failed to decode MOSDOpReply for tid %llu: %d\n",
  3219. req->r_tid, ret);
  3220. ceph_msg_dump(msg);
  3221. goto fail_request;
  3222. }
  3223. dout("%s req %p tid %llu flags 0x%llx pgid %llu.%x epoch %u attempt %d v %u'%llu uv %llu\n",
  3224. __func__, req, req->r_tid, m.flags, m.pgid.pool, m.pgid.seed,
  3225. m.epoch, m.retry_attempt, le32_to_cpu(m.replay_version.epoch),
  3226. le64_to_cpu(m.replay_version.version), m.user_version);
  3227. if (m.retry_attempt >= 0) {
  3228. if (m.retry_attempt != req->r_attempts - 1) {
  3229. dout("req %p tid %llu retry_attempt %d != %d, ignoring\n",
  3230. req, req->r_tid, m.retry_attempt,
  3231. req->r_attempts - 1);
  3232. goto out_unlock_session;
  3233. }
  3234. } else {
  3235. WARN_ON(1); /* MOSDOpReply v4 is assumed */
  3236. }
  3237. if (!ceph_oloc_empty(&m.redirect.oloc)) {
  3238. dout("req %p tid %llu redirect pool %lld\n", req, req->r_tid,
  3239. m.redirect.oloc.pool);
  3240. unlink_request(osd, req);
  3241. mutex_unlock(&osd->lock);
  3242. /*
  3243. * Not ceph_oloc_copy() - changing pool_ns is not
  3244. * supported.
  3245. */
  3246. req->r_t.target_oloc.pool = m.redirect.oloc.pool;
  3247. req->r_flags |= CEPH_OSD_FLAG_REDIRECTED |
  3248. CEPH_OSD_FLAG_IGNORE_OVERLAY |
  3249. CEPH_OSD_FLAG_IGNORE_CACHE;
  3250. req->r_tid = 0;
  3251. __submit_request(req, false);
  3252. goto out_unlock_osdc;
  3253. }
  3254. if (m.result == -EAGAIN) {
  3255. dout("req %p tid %llu EAGAIN\n", req, req->r_tid);
  3256. unlink_request(osd, req);
  3257. mutex_unlock(&osd->lock);
  3258. /*
  3259. * The object is missing on the replica or not (yet)
  3260. * readable. Clear pgid to force a resend to the primary
  3261. * via legacy_change.
  3262. */
  3263. req->r_t.pgid.pool = 0;
  3264. req->r_t.pgid.seed = 0;
  3265. WARN_ON(!req->r_t.used_replica);
  3266. req->r_flags &= ~(CEPH_OSD_FLAG_BALANCE_READS |
  3267. CEPH_OSD_FLAG_LOCALIZE_READS);
  3268. req->r_tid = 0;
  3269. __submit_request(req, false);
  3270. goto out_unlock_osdc;
  3271. }
  3272. if (m.num_ops != req->r_num_ops) {
  3273. pr_err("num_ops %d != %d for tid %llu\n", m.num_ops,
  3274. req->r_num_ops, req->r_tid);
  3275. goto fail_request;
  3276. }
  3277. for (i = 0; i < req->r_num_ops; i++) {
  3278. dout(" req %p tid %llu op %d rval %d len %u\n", req,
  3279. req->r_tid, i, m.rval[i], m.outdata_len[i]);
  3280. req->r_ops[i].rval = m.rval[i];
  3281. req->r_ops[i].outdata_len = m.outdata_len[i];
  3282. data_len += m.outdata_len[i];
  3283. }
  3284. if (data_len != le32_to_cpu(msg->hdr.data_len)) {
  3285. pr_err("sum of lens %u != %u for tid %llu\n", data_len,
  3286. le32_to_cpu(msg->hdr.data_len), req->r_tid);
  3287. goto fail_request;
  3288. }
  3289. dout("%s req %p tid %llu result %d data_len %u\n", __func__,
  3290. req, req->r_tid, m.result, data_len);
  3291. /*
  3292. * Since we only ever request ONDISK, we should only ever get
  3293. * one (type of) reply back.
  3294. */
  3295. WARN_ON(!(m.flags & CEPH_OSD_FLAG_ONDISK));
  3296. req->r_version = m.user_version;
  3297. req->r_result = m.result ?: data_len;
  3298. finish_request(req);
  3299. mutex_unlock(&osd->lock);
  3300. up_read(&osdc->lock);
  3301. __complete_request(req);
  3302. return;
  3303. fail_request:
  3304. complete_request(req, -EIO);
  3305. out_unlock_session:
  3306. mutex_unlock(&osd->lock);
  3307. out_unlock_osdc:
  3308. up_read(&osdc->lock);
  3309. }
  3310. static void set_pool_was_full(struct ceph_osd_client *osdc)
  3311. {
  3312. struct rb_node *n;
  3313. for (n = rb_first(&osdc->osdmap->pg_pools); n; n = rb_next(n)) {
  3314. struct ceph_pg_pool_info *pi =
  3315. rb_entry(n, struct ceph_pg_pool_info, node);
  3316. pi->was_full = __pool_full(pi);
  3317. }
  3318. }
  3319. static bool pool_cleared_full(struct ceph_osd_client *osdc, s64 pool_id)
  3320. {
  3321. struct ceph_pg_pool_info *pi;
  3322. pi = ceph_pg_pool_by_id(osdc->osdmap, pool_id);
  3323. if (!pi)
  3324. return false;
  3325. return pi->was_full && !__pool_full(pi);
  3326. }
  3327. static enum calc_target_result
  3328. recalc_linger_target(struct ceph_osd_linger_request *lreq)
  3329. {
  3330. struct ceph_osd_client *osdc = lreq->osdc;
  3331. enum calc_target_result ct_res;
  3332. ct_res = calc_target(osdc, &lreq->t, true);
  3333. if (ct_res == CALC_TARGET_NEED_RESEND) {
  3334. struct ceph_osd *osd;
  3335. osd = lookup_create_osd(osdc, lreq->t.osd, true);
  3336. if (osd != lreq->osd) {
  3337. unlink_linger(lreq->osd, lreq);
  3338. link_linger(osd, lreq);
  3339. }
  3340. }
  3341. return ct_res;
  3342. }
  3343. /*
  3344. * Requeue requests whose mapping to an OSD has changed.
  3345. */
  3346. static void scan_requests(struct ceph_osd *osd,
  3347. bool force_resend,
  3348. bool cleared_full,
  3349. bool check_pool_cleared_full,
  3350. struct rb_root *need_resend,
  3351. struct list_head *need_resend_linger)
  3352. {
  3353. struct ceph_osd_client *osdc = osd->o_osdc;
  3354. struct rb_node *n;
  3355. bool force_resend_writes;
  3356. for (n = rb_first(&osd->o_linger_requests); n; ) {
  3357. struct ceph_osd_linger_request *lreq =
  3358. rb_entry(n, struct ceph_osd_linger_request, node);
  3359. enum calc_target_result ct_res;
  3360. n = rb_next(n); /* recalc_linger_target() */
  3361. dout("%s lreq %p linger_id %llu\n", __func__, lreq,
  3362. lreq->linger_id);
  3363. ct_res = recalc_linger_target(lreq);
  3364. switch (ct_res) {
  3365. case CALC_TARGET_NO_ACTION:
  3366. force_resend_writes = cleared_full ||
  3367. (check_pool_cleared_full &&
  3368. pool_cleared_full(osdc, lreq->t.base_oloc.pool));
  3369. if (!force_resend && !force_resend_writes)
  3370. break;
  3371. fallthrough;
  3372. case CALC_TARGET_NEED_RESEND:
  3373. cancel_linger_map_check(lreq);
  3374. /*
  3375. * scan_requests() for the previous epoch(s)
  3376. * may have already added it to the list, since
  3377. * it's not unlinked here.
  3378. */
  3379. if (list_empty(&lreq->scan_item))
  3380. list_add_tail(&lreq->scan_item, need_resend_linger);
  3381. break;
  3382. case CALC_TARGET_POOL_DNE:
  3383. list_del_init(&lreq->scan_item);
  3384. check_linger_pool_dne(lreq);
  3385. break;
  3386. }
  3387. }
  3388. for (n = rb_first(&osd->o_requests); n; ) {
  3389. struct ceph_osd_request *req =
  3390. rb_entry(n, struct ceph_osd_request, r_node);
  3391. enum calc_target_result ct_res;
  3392. n = rb_next(n); /* unlink_request(), check_pool_dne() */
  3393. dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
  3394. ct_res = calc_target(osdc, &req->r_t, false);
  3395. switch (ct_res) {
  3396. case CALC_TARGET_NO_ACTION:
  3397. force_resend_writes = cleared_full ||
  3398. (check_pool_cleared_full &&
  3399. pool_cleared_full(osdc, req->r_t.base_oloc.pool));
  3400. if (!force_resend &&
  3401. (!(req->r_flags & CEPH_OSD_FLAG_WRITE) ||
  3402. !force_resend_writes))
  3403. break;
  3404. fallthrough;
  3405. case CALC_TARGET_NEED_RESEND:
  3406. cancel_map_check(req);
  3407. unlink_request(osd, req);
  3408. insert_request(need_resend, req);
  3409. break;
  3410. case CALC_TARGET_POOL_DNE:
  3411. check_pool_dne(req);
  3412. break;
  3413. }
  3414. }
  3415. }
  3416. static int handle_one_map(struct ceph_osd_client *osdc,
  3417. void *p, void *end, bool incremental,
  3418. struct rb_root *need_resend,
  3419. struct list_head *need_resend_linger)
  3420. {
  3421. struct ceph_osdmap *newmap;
  3422. struct rb_node *n;
  3423. bool skipped_map = false;
  3424. bool was_full;
  3425. was_full = ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
  3426. set_pool_was_full(osdc);
  3427. if (incremental)
  3428. newmap = osdmap_apply_incremental(&p, end,
  3429. ceph_msgr2(osdc->client),
  3430. osdc->osdmap);
  3431. else
  3432. newmap = ceph_osdmap_decode(&p, end, ceph_msgr2(osdc->client));
  3433. if (IS_ERR(newmap))
  3434. return PTR_ERR(newmap);
  3435. if (newmap != osdc->osdmap) {
  3436. /*
  3437. * Preserve ->was_full before destroying the old map.
  3438. * For pools that weren't in the old map, ->was_full
  3439. * should be false.
  3440. */
  3441. for (n = rb_first(&newmap->pg_pools); n; n = rb_next(n)) {
  3442. struct ceph_pg_pool_info *pi =
  3443. rb_entry(n, struct ceph_pg_pool_info, node);
  3444. struct ceph_pg_pool_info *old_pi;
  3445. old_pi = ceph_pg_pool_by_id(osdc->osdmap, pi->id);
  3446. if (old_pi)
  3447. pi->was_full = old_pi->was_full;
  3448. else
  3449. WARN_ON(pi->was_full);
  3450. }
  3451. if (osdc->osdmap->epoch &&
  3452. osdc->osdmap->epoch + 1 < newmap->epoch) {
  3453. WARN_ON(incremental);
  3454. skipped_map = true;
  3455. }
  3456. ceph_osdmap_destroy(osdc->osdmap);
  3457. osdc->osdmap = newmap;
  3458. }
  3459. was_full &= !ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL);
  3460. scan_requests(&osdc->homeless_osd, skipped_map, was_full, true,
  3461. need_resend, need_resend_linger);
  3462. for (n = rb_first(&osdc->osds); n; ) {
  3463. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  3464. n = rb_next(n); /* close_osd() */
  3465. scan_requests(osd, skipped_map, was_full, true, need_resend,
  3466. need_resend_linger);
  3467. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  3468. memcmp(&osd->o_con.peer_addr,
  3469. ceph_osd_addr(osdc->osdmap, osd->o_osd),
  3470. sizeof(struct ceph_entity_addr)))
  3471. close_osd(osd);
  3472. }
  3473. return 0;
  3474. }
  3475. static void kick_requests(struct ceph_osd_client *osdc,
  3476. struct rb_root *need_resend,
  3477. struct list_head *need_resend_linger)
  3478. {
  3479. struct ceph_osd_linger_request *lreq, *nlreq;
  3480. enum calc_target_result ct_res;
  3481. struct rb_node *n;
  3482. /* make sure need_resend targets reflect latest map */
  3483. for (n = rb_first(need_resend); n; ) {
  3484. struct ceph_osd_request *req =
  3485. rb_entry(n, struct ceph_osd_request, r_node);
  3486. n = rb_next(n);
  3487. if (req->r_t.epoch < osdc->osdmap->epoch) {
  3488. ct_res = calc_target(osdc, &req->r_t, false);
  3489. if (ct_res == CALC_TARGET_POOL_DNE) {
  3490. erase_request(need_resend, req);
  3491. check_pool_dne(req);
  3492. }
  3493. }
  3494. }
  3495. for (n = rb_first(need_resend); n; ) {
  3496. struct ceph_osd_request *req =
  3497. rb_entry(n, struct ceph_osd_request, r_node);
  3498. struct ceph_osd *osd;
  3499. n = rb_next(n);
  3500. erase_request(need_resend, req); /* before link_request() */
  3501. osd = lookup_create_osd(osdc, req->r_t.osd, true);
  3502. link_request(osd, req);
  3503. if (!req->r_linger) {
  3504. if (!osd_homeless(osd) && !req->r_t.paused)
  3505. send_request(req);
  3506. } else {
  3507. cancel_linger_request(req);
  3508. }
  3509. }
  3510. list_for_each_entry_safe(lreq, nlreq, need_resend_linger, scan_item) {
  3511. if (!osd_homeless(lreq->osd))
  3512. send_linger(lreq);
  3513. list_del_init(&lreq->scan_item);
  3514. }
  3515. }
  3516. /*
  3517. * Process updated osd map.
  3518. *
  3519. * The message contains any number of incremental and full maps, normally
  3520. * indicating some sort of topology change in the cluster. Kick requests
  3521. * off to different OSDs as needed.
  3522. */
  3523. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  3524. {
  3525. void *p = msg->front.iov_base;
  3526. void *const end = p + msg->front.iov_len;
  3527. u32 nr_maps, maplen;
  3528. u32 epoch;
  3529. struct ceph_fsid fsid;
  3530. struct rb_root need_resend = RB_ROOT;
  3531. LIST_HEAD(need_resend_linger);
  3532. bool handled_incremental = false;
  3533. bool was_pauserd, was_pausewr;
  3534. bool pauserd, pausewr;
  3535. int err;
  3536. dout("%s have %u\n", __func__, osdc->osdmap->epoch);
  3537. down_write(&osdc->lock);
  3538. /* verify fsid */
  3539. ceph_decode_need(&p, end, sizeof(fsid), bad);
  3540. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  3541. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  3542. goto bad;
  3543. was_pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
  3544. was_pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
  3545. ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  3546. have_pool_full(osdc);
  3547. /* incremental maps */
  3548. ceph_decode_32_safe(&p, end, nr_maps, bad);
  3549. dout(" %d inc maps\n", nr_maps);
  3550. while (nr_maps > 0) {
  3551. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  3552. epoch = ceph_decode_32(&p);
  3553. maplen = ceph_decode_32(&p);
  3554. ceph_decode_need(&p, end, maplen, bad);
  3555. if (osdc->osdmap->epoch &&
  3556. osdc->osdmap->epoch + 1 == epoch) {
  3557. dout("applying incremental map %u len %d\n",
  3558. epoch, maplen);
  3559. err = handle_one_map(osdc, p, p + maplen, true,
  3560. &need_resend, &need_resend_linger);
  3561. if (err)
  3562. goto bad;
  3563. handled_incremental = true;
  3564. } else {
  3565. dout("ignoring incremental map %u len %d\n",
  3566. epoch, maplen);
  3567. }
  3568. p += maplen;
  3569. nr_maps--;
  3570. }
  3571. if (handled_incremental)
  3572. goto done;
  3573. /* full maps */
  3574. ceph_decode_32_safe(&p, end, nr_maps, bad);
  3575. dout(" %d full maps\n", nr_maps);
  3576. while (nr_maps) {
  3577. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  3578. epoch = ceph_decode_32(&p);
  3579. maplen = ceph_decode_32(&p);
  3580. ceph_decode_need(&p, end, maplen, bad);
  3581. if (nr_maps > 1) {
  3582. dout("skipping non-latest full map %u len %d\n",
  3583. epoch, maplen);
  3584. } else if (osdc->osdmap->epoch >= epoch) {
  3585. dout("skipping full map %u len %d, "
  3586. "older than our %u\n", epoch, maplen,
  3587. osdc->osdmap->epoch);
  3588. } else {
  3589. dout("taking full map %u len %d\n", epoch, maplen);
  3590. err = handle_one_map(osdc, p, p + maplen, false,
  3591. &need_resend, &need_resend_linger);
  3592. if (err)
  3593. goto bad;
  3594. }
  3595. p += maplen;
  3596. nr_maps--;
  3597. }
  3598. done:
  3599. /*
  3600. * subscribe to subsequent osdmap updates if full to ensure
  3601. * we find out when we are no longer full and stop returning
  3602. * ENOSPC.
  3603. */
  3604. pauserd = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSERD);
  3605. pausewr = ceph_osdmap_flag(osdc, CEPH_OSDMAP_PAUSEWR) ||
  3606. ceph_osdmap_flag(osdc, CEPH_OSDMAP_FULL) ||
  3607. have_pool_full(osdc);
  3608. if (was_pauserd || was_pausewr || pauserd || pausewr ||
  3609. osdc->osdmap->epoch < osdc->epoch_barrier)
  3610. maybe_request_map(osdc);
  3611. kick_requests(osdc, &need_resend, &need_resend_linger);
  3612. ceph_osdc_abort_on_full(osdc);
  3613. ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
  3614. osdc->osdmap->epoch);
  3615. up_write(&osdc->lock);
  3616. wake_up_all(&osdc->client->auth_wq);
  3617. return;
  3618. bad:
  3619. pr_err("osdc handle_map corrupt msg\n");
  3620. ceph_msg_dump(msg);
  3621. up_write(&osdc->lock);
  3622. }
  3623. /*
  3624. * Resubmit requests pending on the given osd.
  3625. */
  3626. static void kick_osd_requests(struct ceph_osd *osd)
  3627. {
  3628. struct rb_node *n;
  3629. clear_backoffs(osd);
  3630. for (n = rb_first(&osd->o_requests); n; ) {
  3631. struct ceph_osd_request *req =
  3632. rb_entry(n, struct ceph_osd_request, r_node);
  3633. n = rb_next(n); /* cancel_linger_request() */
  3634. if (!req->r_linger) {
  3635. if (!req->r_t.paused)
  3636. send_request(req);
  3637. } else {
  3638. cancel_linger_request(req);
  3639. }
  3640. }
  3641. for (n = rb_first(&osd->o_linger_requests); n; n = rb_next(n)) {
  3642. struct ceph_osd_linger_request *lreq =
  3643. rb_entry(n, struct ceph_osd_linger_request, node);
  3644. send_linger(lreq);
  3645. }
  3646. }
  3647. /*
  3648. * If the osd connection drops, we need to resubmit all requests.
  3649. */
  3650. static void osd_fault(struct ceph_connection *con)
  3651. {
  3652. struct ceph_osd *osd = con->private;
  3653. struct ceph_osd_client *osdc = osd->o_osdc;
  3654. dout("%s osd %p osd%d\n", __func__, osd, osd->o_osd);
  3655. down_write(&osdc->lock);
  3656. if (!osd_registered(osd)) {
  3657. dout("%s osd%d unknown\n", __func__, osd->o_osd);
  3658. goto out_unlock;
  3659. }
  3660. if (!reopen_osd(osd))
  3661. kick_osd_requests(osd);
  3662. maybe_request_map(osdc);
  3663. out_unlock:
  3664. up_write(&osdc->lock);
  3665. }
  3666. struct MOSDBackoff {
  3667. struct ceph_spg spgid;
  3668. u32 map_epoch;
  3669. u8 op;
  3670. u64 id;
  3671. struct ceph_hobject_id *begin;
  3672. struct ceph_hobject_id *end;
  3673. };
  3674. static int decode_MOSDBackoff(const struct ceph_msg *msg, struct MOSDBackoff *m)
  3675. {
  3676. void *p = msg->front.iov_base;
  3677. void *const end = p + msg->front.iov_len;
  3678. u8 struct_v;
  3679. u32 struct_len;
  3680. int ret;
  3681. ret = ceph_start_decoding(&p, end, 1, "spg_t", &struct_v, &struct_len);
  3682. if (ret)
  3683. return ret;
  3684. ret = ceph_decode_pgid(&p, end, &m->spgid.pgid);
  3685. if (ret)
  3686. return ret;
  3687. ceph_decode_8_safe(&p, end, m->spgid.shard, e_inval);
  3688. ceph_decode_32_safe(&p, end, m->map_epoch, e_inval);
  3689. ceph_decode_8_safe(&p, end, m->op, e_inval);
  3690. ceph_decode_64_safe(&p, end, m->id, e_inval);
  3691. m->begin = kzalloc(sizeof(*m->begin), GFP_NOIO);
  3692. if (!m->begin)
  3693. return -ENOMEM;
  3694. ret = decode_hoid(&p, end, m->begin);
  3695. if (ret) {
  3696. free_hoid(m->begin);
  3697. return ret;
  3698. }
  3699. m->end = kzalloc(sizeof(*m->end), GFP_NOIO);
  3700. if (!m->end) {
  3701. free_hoid(m->begin);
  3702. return -ENOMEM;
  3703. }
  3704. ret = decode_hoid(&p, end, m->end);
  3705. if (ret) {
  3706. free_hoid(m->begin);
  3707. free_hoid(m->end);
  3708. return ret;
  3709. }
  3710. return 0;
  3711. e_inval:
  3712. return -EINVAL;
  3713. }
  3714. static struct ceph_msg *create_backoff_message(
  3715. const struct ceph_osd_backoff *backoff,
  3716. u32 map_epoch)
  3717. {
  3718. struct ceph_msg *msg;
  3719. void *p, *end;
  3720. int msg_size;
  3721. msg_size = CEPH_ENCODING_START_BLK_LEN +
  3722. CEPH_PGID_ENCODING_LEN + 1; /* spgid */
  3723. msg_size += 4 + 1 + 8; /* map_epoch, op, id */
  3724. msg_size += CEPH_ENCODING_START_BLK_LEN +
  3725. hoid_encoding_size(backoff->begin);
  3726. msg_size += CEPH_ENCODING_START_BLK_LEN +
  3727. hoid_encoding_size(backoff->end);
  3728. msg = ceph_msg_new(CEPH_MSG_OSD_BACKOFF, msg_size, GFP_NOIO, true);
  3729. if (!msg)
  3730. return NULL;
  3731. p = msg->front.iov_base;
  3732. end = p + msg->front_alloc_len;
  3733. encode_spgid(&p, &backoff->spgid);
  3734. ceph_encode_32(&p, map_epoch);
  3735. ceph_encode_8(&p, CEPH_OSD_BACKOFF_OP_ACK_BLOCK);
  3736. ceph_encode_64(&p, backoff->id);
  3737. encode_hoid(&p, end, backoff->begin);
  3738. encode_hoid(&p, end, backoff->end);
  3739. BUG_ON(p != end);
  3740. msg->front.iov_len = p - msg->front.iov_base;
  3741. msg->hdr.version = cpu_to_le16(1); /* MOSDBackoff v1 */
  3742. msg->hdr.front_len = cpu_to_le32(msg->front.iov_len);
  3743. return msg;
  3744. }
  3745. static void handle_backoff_block(struct ceph_osd *osd, struct MOSDBackoff *m)
  3746. {
  3747. struct ceph_spg_mapping *spg;
  3748. struct ceph_osd_backoff *backoff;
  3749. struct ceph_msg *msg;
  3750. dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
  3751. m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
  3752. spg = lookup_spg_mapping(&osd->o_backoff_mappings, &m->spgid);
  3753. if (!spg) {
  3754. spg = alloc_spg_mapping();
  3755. if (!spg) {
  3756. pr_err("%s failed to allocate spg\n", __func__);
  3757. return;
  3758. }
  3759. spg->spgid = m->spgid; /* struct */
  3760. insert_spg_mapping(&osd->o_backoff_mappings, spg);
  3761. }
  3762. backoff = alloc_backoff();
  3763. if (!backoff) {
  3764. pr_err("%s failed to allocate backoff\n", __func__);
  3765. return;
  3766. }
  3767. backoff->spgid = m->spgid; /* struct */
  3768. backoff->id = m->id;
  3769. backoff->begin = m->begin;
  3770. m->begin = NULL; /* backoff now owns this */
  3771. backoff->end = m->end;
  3772. m->end = NULL; /* ditto */
  3773. insert_backoff(&spg->backoffs, backoff);
  3774. insert_backoff_by_id(&osd->o_backoffs_by_id, backoff);
  3775. /*
  3776. * Ack with original backoff's epoch so that the OSD can
  3777. * discard this if there was a PG split.
  3778. */
  3779. msg = create_backoff_message(backoff, m->map_epoch);
  3780. if (!msg) {
  3781. pr_err("%s failed to allocate msg\n", __func__);
  3782. return;
  3783. }
  3784. ceph_con_send(&osd->o_con, msg);
  3785. }
  3786. static bool target_contained_by(const struct ceph_osd_request_target *t,
  3787. const struct ceph_hobject_id *begin,
  3788. const struct ceph_hobject_id *end)
  3789. {
  3790. struct ceph_hobject_id hoid;
  3791. int cmp;
  3792. hoid_fill_from_target(&hoid, t);
  3793. cmp = hoid_compare(&hoid, begin);
  3794. return !cmp || (cmp > 0 && hoid_compare(&hoid, end) < 0);
  3795. }
  3796. static void handle_backoff_unblock(struct ceph_osd *osd,
  3797. const struct MOSDBackoff *m)
  3798. {
  3799. struct ceph_spg_mapping *spg;
  3800. struct ceph_osd_backoff *backoff;
  3801. struct rb_node *n;
  3802. dout("%s osd%d spgid %llu.%xs%d id %llu\n", __func__, osd->o_osd,
  3803. m->spgid.pgid.pool, m->spgid.pgid.seed, m->spgid.shard, m->id);
  3804. backoff = lookup_backoff_by_id(&osd->o_backoffs_by_id, m->id);
  3805. if (!backoff) {
  3806. pr_err("%s osd%d spgid %llu.%xs%d id %llu backoff dne\n",
  3807. __func__, osd->o_osd, m->spgid.pgid.pool,
  3808. m->spgid.pgid.seed, m->spgid.shard, m->id);
  3809. return;
  3810. }
  3811. if (hoid_compare(backoff->begin, m->begin) &&
  3812. hoid_compare(backoff->end, m->end)) {
  3813. pr_err("%s osd%d spgid %llu.%xs%d id %llu bad range?\n",
  3814. __func__, osd->o_osd, m->spgid.pgid.pool,
  3815. m->spgid.pgid.seed, m->spgid.shard, m->id);
  3816. /* unblock it anyway... */
  3817. }
  3818. spg = lookup_spg_mapping(&osd->o_backoff_mappings, &backoff->spgid);
  3819. BUG_ON(!spg);
  3820. erase_backoff(&spg->backoffs, backoff);
  3821. erase_backoff_by_id(&osd->o_backoffs_by_id, backoff);
  3822. free_backoff(backoff);
  3823. if (RB_EMPTY_ROOT(&spg->backoffs)) {
  3824. erase_spg_mapping(&osd->o_backoff_mappings, spg);
  3825. free_spg_mapping(spg);
  3826. }
  3827. for (n = rb_first(&osd->o_requests); n; n = rb_next(n)) {
  3828. struct ceph_osd_request *req =
  3829. rb_entry(n, struct ceph_osd_request, r_node);
  3830. if (!ceph_spg_compare(&req->r_t.spgid, &m->spgid)) {
  3831. /*
  3832. * Match against @m, not @backoff -- the PG may
  3833. * have split on the OSD.
  3834. */
  3835. if (target_contained_by(&req->r_t, m->begin, m->end)) {
  3836. /*
  3837. * If no other installed backoff applies,
  3838. * resend.
  3839. */
  3840. send_request(req);
  3841. }
  3842. }
  3843. }
  3844. }
  3845. static void handle_backoff(struct ceph_osd *osd, struct ceph_msg *msg)
  3846. {
  3847. struct ceph_osd_client *osdc = osd->o_osdc;
  3848. struct MOSDBackoff m;
  3849. int ret;
  3850. down_read(&osdc->lock);
  3851. if (!osd_registered(osd)) {
  3852. dout("%s osd%d unknown\n", __func__, osd->o_osd);
  3853. up_read(&osdc->lock);
  3854. return;
  3855. }
  3856. WARN_ON(osd->o_osd != le64_to_cpu(msg->hdr.src.num));
  3857. mutex_lock(&osd->lock);
  3858. ret = decode_MOSDBackoff(msg, &m);
  3859. if (ret) {
  3860. pr_err("failed to decode MOSDBackoff: %d\n", ret);
  3861. ceph_msg_dump(msg);
  3862. goto out_unlock;
  3863. }
  3864. switch (m.op) {
  3865. case CEPH_OSD_BACKOFF_OP_BLOCK:
  3866. handle_backoff_block(osd, &m);
  3867. break;
  3868. case CEPH_OSD_BACKOFF_OP_UNBLOCK:
  3869. handle_backoff_unblock(osd, &m);
  3870. break;
  3871. default:
  3872. pr_err("%s osd%d unknown op %d\n", __func__, osd->o_osd, m.op);
  3873. }
  3874. free_hoid(m.begin);
  3875. free_hoid(m.end);
  3876. out_unlock:
  3877. mutex_unlock(&osd->lock);
  3878. up_read(&osdc->lock);
  3879. }
  3880. /*
  3881. * Process osd watch notifications
  3882. */
  3883. static void handle_watch_notify(struct ceph_osd_client *osdc,
  3884. struct ceph_msg *msg)
  3885. {
  3886. void *p = msg->front.iov_base;
  3887. void *const end = p + msg->front.iov_len;
  3888. struct ceph_osd_linger_request *lreq;
  3889. struct linger_work *lwork;
  3890. u8 proto_ver, opcode;
  3891. u64 cookie, notify_id;
  3892. u64 notifier_id = 0;
  3893. s32 return_code = 0;
  3894. void *payload = NULL;
  3895. u32 payload_len = 0;
  3896. ceph_decode_8_safe(&p, end, proto_ver, bad);
  3897. ceph_decode_8_safe(&p, end, opcode, bad);
  3898. ceph_decode_64_safe(&p, end, cookie, bad);
  3899. p += 8; /* skip ver */
  3900. ceph_decode_64_safe(&p, end, notify_id, bad);
  3901. if (proto_ver >= 1) {
  3902. ceph_decode_32_safe(&p, end, payload_len, bad);
  3903. ceph_decode_need(&p, end, payload_len, bad);
  3904. payload = p;
  3905. p += payload_len;
  3906. }
  3907. if (le16_to_cpu(msg->hdr.version) >= 2)
  3908. ceph_decode_32_safe(&p, end, return_code, bad);
  3909. if (le16_to_cpu(msg->hdr.version) >= 3)
  3910. ceph_decode_64_safe(&p, end, notifier_id, bad);
  3911. down_read(&osdc->lock);
  3912. lreq = lookup_linger_osdc(&osdc->linger_requests, cookie);
  3913. if (!lreq) {
  3914. dout("%s opcode %d cookie %llu dne\n", __func__, opcode,
  3915. cookie);
  3916. goto out_unlock_osdc;
  3917. }
  3918. mutex_lock(&lreq->lock);
  3919. dout("%s opcode %d cookie %llu lreq %p is_watch %d\n", __func__,
  3920. opcode, cookie, lreq, lreq->is_watch);
  3921. if (opcode == CEPH_WATCH_EVENT_DISCONNECT) {
  3922. if (!lreq->last_error) {
  3923. lreq->last_error = -ENOTCONN;
  3924. queue_watch_error(lreq);
  3925. }
  3926. } else if (!lreq->is_watch) {
  3927. /* CEPH_WATCH_EVENT_NOTIFY_COMPLETE */
  3928. if (lreq->notify_id && lreq->notify_id != notify_id) {
  3929. dout("lreq %p notify_id %llu != %llu, ignoring\n", lreq,
  3930. lreq->notify_id, notify_id);
  3931. } else if (!completion_done(&lreq->notify_finish_wait)) {
  3932. struct ceph_msg_data *data =
  3933. msg->num_data_items ? &msg->data[0] : NULL;
  3934. if (data) {
  3935. if (lreq->preply_pages) {
  3936. WARN_ON(data->type !=
  3937. CEPH_MSG_DATA_PAGES);
  3938. *lreq->preply_pages = data->pages;
  3939. *lreq->preply_len = data->length;
  3940. data->own_pages = false;
  3941. }
  3942. }
  3943. lreq->notify_finish_error = return_code;
  3944. complete_all(&lreq->notify_finish_wait);
  3945. }
  3946. } else {
  3947. /* CEPH_WATCH_EVENT_NOTIFY */
  3948. lwork = lwork_alloc(lreq, do_watch_notify);
  3949. if (!lwork) {
  3950. pr_err("failed to allocate notify-lwork\n");
  3951. goto out_unlock_lreq;
  3952. }
  3953. lwork->notify.notify_id = notify_id;
  3954. lwork->notify.notifier_id = notifier_id;
  3955. lwork->notify.payload = payload;
  3956. lwork->notify.payload_len = payload_len;
  3957. lwork->notify.msg = ceph_msg_get(msg);
  3958. lwork_queue(lwork);
  3959. }
  3960. out_unlock_lreq:
  3961. mutex_unlock(&lreq->lock);
  3962. out_unlock_osdc:
  3963. up_read(&osdc->lock);
  3964. return;
  3965. bad:
  3966. pr_err("osdc handle_watch_notify corrupt msg\n");
  3967. }
  3968. /*
  3969. * Register request, send initial attempt.
  3970. */
  3971. void ceph_osdc_start_request(struct ceph_osd_client *osdc,
  3972. struct ceph_osd_request *req)
  3973. {
  3974. down_read(&osdc->lock);
  3975. submit_request(req, false);
  3976. up_read(&osdc->lock);
  3977. }
  3978. EXPORT_SYMBOL(ceph_osdc_start_request);
  3979. /*
  3980. * Unregister request. If @req was registered, it isn't completed:
  3981. * r_result isn't set and __complete_request() isn't invoked.
  3982. *
  3983. * If @req wasn't registered, this call may have raced with
  3984. * handle_reply(), in which case r_result would already be set and
  3985. * __complete_request() would be getting invoked, possibly even
  3986. * concurrently with this call.
  3987. */
  3988. void ceph_osdc_cancel_request(struct ceph_osd_request *req)
  3989. {
  3990. struct ceph_osd_client *osdc = req->r_osdc;
  3991. down_write(&osdc->lock);
  3992. if (req->r_osd)
  3993. cancel_request(req);
  3994. up_write(&osdc->lock);
  3995. }
  3996. EXPORT_SYMBOL(ceph_osdc_cancel_request);
  3997. /*
  3998. * @timeout: in jiffies, 0 means "wait forever"
  3999. */
  4000. static int wait_request_timeout(struct ceph_osd_request *req,
  4001. unsigned long timeout)
  4002. {
  4003. long left;
  4004. dout("%s req %p tid %llu\n", __func__, req, req->r_tid);
  4005. left = wait_for_completion_killable_timeout(&req->r_completion,
  4006. ceph_timeout_jiffies(timeout));
  4007. if (left <= 0) {
  4008. left = left ?: -ETIMEDOUT;
  4009. ceph_osdc_cancel_request(req);
  4010. } else {
  4011. left = req->r_result; /* completed */
  4012. }
  4013. return left;
  4014. }
  4015. /*
  4016. * wait for a request to complete
  4017. */
  4018. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  4019. struct ceph_osd_request *req)
  4020. {
  4021. return wait_request_timeout(req, 0);
  4022. }
  4023. EXPORT_SYMBOL(ceph_osdc_wait_request);
  4024. /*
  4025. * sync - wait for all in-flight requests to flush. avoid starvation.
  4026. */
  4027. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  4028. {
  4029. struct rb_node *n, *p;
  4030. u64 last_tid = atomic64_read(&osdc->last_tid);
  4031. again:
  4032. down_read(&osdc->lock);
  4033. for (n = rb_first(&osdc->osds); n; n = rb_next(n)) {
  4034. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  4035. mutex_lock(&osd->lock);
  4036. for (p = rb_first(&osd->o_requests); p; p = rb_next(p)) {
  4037. struct ceph_osd_request *req =
  4038. rb_entry(p, struct ceph_osd_request, r_node);
  4039. if (req->r_tid > last_tid)
  4040. break;
  4041. if (!(req->r_flags & CEPH_OSD_FLAG_WRITE))
  4042. continue;
  4043. ceph_osdc_get_request(req);
  4044. mutex_unlock(&osd->lock);
  4045. up_read(&osdc->lock);
  4046. dout("%s waiting on req %p tid %llu last_tid %llu\n",
  4047. __func__, req, req->r_tid, last_tid);
  4048. wait_for_completion(&req->r_completion);
  4049. ceph_osdc_put_request(req);
  4050. goto again;
  4051. }
  4052. mutex_unlock(&osd->lock);
  4053. }
  4054. up_read(&osdc->lock);
  4055. dout("%s done last_tid %llu\n", __func__, last_tid);
  4056. }
  4057. EXPORT_SYMBOL(ceph_osdc_sync);
  4058. /*
  4059. * Returns a handle, caller owns a ref.
  4060. */
  4061. struct ceph_osd_linger_request *
  4062. ceph_osdc_watch(struct ceph_osd_client *osdc,
  4063. struct ceph_object_id *oid,
  4064. struct ceph_object_locator *oloc,
  4065. rados_watchcb2_t wcb,
  4066. rados_watcherrcb_t errcb,
  4067. void *data)
  4068. {
  4069. struct ceph_osd_linger_request *lreq;
  4070. int ret;
  4071. lreq = linger_alloc(osdc);
  4072. if (!lreq)
  4073. return ERR_PTR(-ENOMEM);
  4074. lreq->is_watch = true;
  4075. lreq->wcb = wcb;
  4076. lreq->errcb = errcb;
  4077. lreq->data = data;
  4078. lreq->watch_valid_thru = jiffies;
  4079. ceph_oid_copy(&lreq->t.base_oid, oid);
  4080. ceph_oloc_copy(&lreq->t.base_oloc, oloc);
  4081. lreq->t.flags = CEPH_OSD_FLAG_WRITE;
  4082. ktime_get_real_ts64(&lreq->mtime);
  4083. linger_submit(lreq);
  4084. ret = linger_reg_commit_wait(lreq);
  4085. if (ret) {
  4086. linger_cancel(lreq);
  4087. goto err_put_lreq;
  4088. }
  4089. return lreq;
  4090. err_put_lreq:
  4091. linger_put(lreq);
  4092. return ERR_PTR(ret);
  4093. }
  4094. EXPORT_SYMBOL(ceph_osdc_watch);
  4095. /*
  4096. * Releases a ref.
  4097. *
  4098. * Times out after mount_timeout to preserve rbd unmap behaviour
  4099. * introduced in 2894e1d76974 ("rbd: timeout watch teardown on unmap
  4100. * with mount_timeout").
  4101. */
  4102. int ceph_osdc_unwatch(struct ceph_osd_client *osdc,
  4103. struct ceph_osd_linger_request *lreq)
  4104. {
  4105. struct ceph_options *opts = osdc->client->options;
  4106. struct ceph_osd_request *req;
  4107. int ret;
  4108. req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
  4109. if (!req)
  4110. return -ENOMEM;
  4111. ceph_oid_copy(&req->r_base_oid, &lreq->t.base_oid);
  4112. ceph_oloc_copy(&req->r_base_oloc, &lreq->t.base_oloc);
  4113. req->r_flags = CEPH_OSD_FLAG_WRITE;
  4114. ktime_get_real_ts64(&req->r_mtime);
  4115. osd_req_op_watch_init(req, 0, CEPH_OSD_WATCH_OP_UNWATCH,
  4116. lreq->linger_id, 0);
  4117. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  4118. if (ret)
  4119. goto out_put_req;
  4120. ceph_osdc_start_request(osdc, req);
  4121. linger_cancel(lreq);
  4122. linger_put(lreq);
  4123. ret = wait_request_timeout(req, opts->mount_timeout);
  4124. out_put_req:
  4125. ceph_osdc_put_request(req);
  4126. return ret;
  4127. }
  4128. EXPORT_SYMBOL(ceph_osdc_unwatch);
  4129. static int osd_req_op_notify_ack_init(struct ceph_osd_request *req, int which,
  4130. u64 notify_id, u64 cookie, void *payload,
  4131. u32 payload_len)
  4132. {
  4133. struct ceph_osd_req_op *op;
  4134. struct ceph_pagelist *pl;
  4135. int ret;
  4136. op = osd_req_op_init(req, which, CEPH_OSD_OP_NOTIFY_ACK, 0);
  4137. pl = ceph_pagelist_alloc(GFP_NOIO);
  4138. if (!pl)
  4139. return -ENOMEM;
  4140. ret = ceph_pagelist_encode_64(pl, notify_id);
  4141. ret |= ceph_pagelist_encode_64(pl, cookie);
  4142. if (payload) {
  4143. ret |= ceph_pagelist_encode_32(pl, payload_len);
  4144. ret |= ceph_pagelist_append(pl, payload, payload_len);
  4145. } else {
  4146. ret |= ceph_pagelist_encode_32(pl, 0);
  4147. }
  4148. if (ret) {
  4149. ceph_pagelist_release(pl);
  4150. return -ENOMEM;
  4151. }
  4152. ceph_osd_data_pagelist_init(&op->notify_ack.request_data, pl);
  4153. op->indata_len = pl->length;
  4154. return 0;
  4155. }
  4156. int ceph_osdc_notify_ack(struct ceph_osd_client *osdc,
  4157. struct ceph_object_id *oid,
  4158. struct ceph_object_locator *oloc,
  4159. u64 notify_id,
  4160. u64 cookie,
  4161. void *payload,
  4162. u32 payload_len)
  4163. {
  4164. struct ceph_osd_request *req;
  4165. int ret;
  4166. req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
  4167. if (!req)
  4168. return -ENOMEM;
  4169. ceph_oid_copy(&req->r_base_oid, oid);
  4170. ceph_oloc_copy(&req->r_base_oloc, oloc);
  4171. req->r_flags = CEPH_OSD_FLAG_READ;
  4172. ret = osd_req_op_notify_ack_init(req, 0, notify_id, cookie, payload,
  4173. payload_len);
  4174. if (ret)
  4175. goto out_put_req;
  4176. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  4177. if (ret)
  4178. goto out_put_req;
  4179. ceph_osdc_start_request(osdc, req);
  4180. ret = ceph_osdc_wait_request(osdc, req);
  4181. out_put_req:
  4182. ceph_osdc_put_request(req);
  4183. return ret;
  4184. }
  4185. EXPORT_SYMBOL(ceph_osdc_notify_ack);
  4186. /*
  4187. * @timeout: in seconds
  4188. *
  4189. * @preply_{pages,len} are initialized both on success and error.
  4190. * The caller is responsible for:
  4191. *
  4192. * ceph_release_page_vector(reply_pages, calc_pages_for(0, reply_len))
  4193. */
  4194. int ceph_osdc_notify(struct ceph_osd_client *osdc,
  4195. struct ceph_object_id *oid,
  4196. struct ceph_object_locator *oloc,
  4197. void *payload,
  4198. u32 payload_len,
  4199. u32 timeout,
  4200. struct page ***preply_pages,
  4201. size_t *preply_len)
  4202. {
  4203. struct ceph_osd_linger_request *lreq;
  4204. int ret;
  4205. WARN_ON(!timeout);
  4206. if (preply_pages) {
  4207. *preply_pages = NULL;
  4208. *preply_len = 0;
  4209. }
  4210. lreq = linger_alloc(osdc);
  4211. if (!lreq)
  4212. return -ENOMEM;
  4213. lreq->request_pl = ceph_pagelist_alloc(GFP_NOIO);
  4214. if (!lreq->request_pl) {
  4215. ret = -ENOMEM;
  4216. goto out_put_lreq;
  4217. }
  4218. ret = ceph_pagelist_encode_32(lreq->request_pl, 1); /* prot_ver */
  4219. ret |= ceph_pagelist_encode_32(lreq->request_pl, timeout);
  4220. ret |= ceph_pagelist_encode_32(lreq->request_pl, payload_len);
  4221. ret |= ceph_pagelist_append(lreq->request_pl, payload, payload_len);
  4222. if (ret) {
  4223. ret = -ENOMEM;
  4224. goto out_put_lreq;
  4225. }
  4226. /* for notify_id */
  4227. lreq->notify_id_pages = ceph_alloc_page_vector(1, GFP_NOIO);
  4228. if (IS_ERR(lreq->notify_id_pages)) {
  4229. ret = PTR_ERR(lreq->notify_id_pages);
  4230. lreq->notify_id_pages = NULL;
  4231. goto out_put_lreq;
  4232. }
  4233. lreq->preply_pages = preply_pages;
  4234. lreq->preply_len = preply_len;
  4235. ceph_oid_copy(&lreq->t.base_oid, oid);
  4236. ceph_oloc_copy(&lreq->t.base_oloc, oloc);
  4237. lreq->t.flags = CEPH_OSD_FLAG_READ;
  4238. linger_submit(lreq);
  4239. ret = linger_reg_commit_wait(lreq);
  4240. if (!ret)
  4241. ret = linger_notify_finish_wait(lreq,
  4242. msecs_to_jiffies(2 * timeout * MSEC_PER_SEC));
  4243. else
  4244. dout("lreq %p failed to initiate notify %d\n", lreq, ret);
  4245. linger_cancel(lreq);
  4246. out_put_lreq:
  4247. linger_put(lreq);
  4248. return ret;
  4249. }
  4250. EXPORT_SYMBOL(ceph_osdc_notify);
  4251. /*
  4252. * Return the number of milliseconds since the watch was last
  4253. * confirmed, or an error. If there is an error, the watch is no
  4254. * longer valid, and should be destroyed with ceph_osdc_unwatch().
  4255. */
  4256. int ceph_osdc_watch_check(struct ceph_osd_client *osdc,
  4257. struct ceph_osd_linger_request *lreq)
  4258. {
  4259. unsigned long stamp, age;
  4260. int ret;
  4261. down_read(&osdc->lock);
  4262. mutex_lock(&lreq->lock);
  4263. stamp = lreq->watch_valid_thru;
  4264. if (!list_empty(&lreq->pending_lworks)) {
  4265. struct linger_work *lwork =
  4266. list_first_entry(&lreq->pending_lworks,
  4267. struct linger_work,
  4268. pending_item);
  4269. if (time_before(lwork->queued_stamp, stamp))
  4270. stamp = lwork->queued_stamp;
  4271. }
  4272. age = jiffies - stamp;
  4273. dout("%s lreq %p linger_id %llu age %lu last_error %d\n", __func__,
  4274. lreq, lreq->linger_id, age, lreq->last_error);
  4275. /* we are truncating to msecs, so return a safe upper bound */
  4276. ret = lreq->last_error ?: 1 + jiffies_to_msecs(age);
  4277. mutex_unlock(&lreq->lock);
  4278. up_read(&osdc->lock);
  4279. return ret;
  4280. }
  4281. static int decode_watcher(void **p, void *end, struct ceph_watch_item *item)
  4282. {
  4283. u8 struct_v;
  4284. u32 struct_len;
  4285. int ret;
  4286. ret = ceph_start_decoding(p, end, 2, "watch_item_t",
  4287. &struct_v, &struct_len);
  4288. if (ret)
  4289. goto bad;
  4290. ret = -EINVAL;
  4291. ceph_decode_copy_safe(p, end, &item->name, sizeof(item->name), bad);
  4292. ceph_decode_64_safe(p, end, item->cookie, bad);
  4293. ceph_decode_skip_32(p, end, bad); /* skip timeout seconds */
  4294. if (struct_v >= 2) {
  4295. ret = ceph_decode_entity_addr(p, end, &item->addr);
  4296. if (ret)
  4297. goto bad;
  4298. } else {
  4299. ret = 0;
  4300. }
  4301. dout("%s %s%llu cookie %llu addr %s\n", __func__,
  4302. ENTITY_NAME(item->name), item->cookie,
  4303. ceph_pr_addr(&item->addr));
  4304. bad:
  4305. return ret;
  4306. }
  4307. static int decode_watchers(void **p, void *end,
  4308. struct ceph_watch_item **watchers,
  4309. u32 *num_watchers)
  4310. {
  4311. u8 struct_v;
  4312. u32 struct_len;
  4313. int i;
  4314. int ret;
  4315. ret = ceph_start_decoding(p, end, 1, "obj_list_watch_response_t",
  4316. &struct_v, &struct_len);
  4317. if (ret)
  4318. return ret;
  4319. *num_watchers = ceph_decode_32(p);
  4320. *watchers = kcalloc(*num_watchers, sizeof(**watchers), GFP_NOIO);
  4321. if (!*watchers)
  4322. return -ENOMEM;
  4323. for (i = 0; i < *num_watchers; i++) {
  4324. ret = decode_watcher(p, end, *watchers + i);
  4325. if (ret) {
  4326. kfree(*watchers);
  4327. return ret;
  4328. }
  4329. }
  4330. return 0;
  4331. }
  4332. /*
  4333. * On success, the caller is responsible for:
  4334. *
  4335. * kfree(watchers);
  4336. */
  4337. int ceph_osdc_list_watchers(struct ceph_osd_client *osdc,
  4338. struct ceph_object_id *oid,
  4339. struct ceph_object_locator *oloc,
  4340. struct ceph_watch_item **watchers,
  4341. u32 *num_watchers)
  4342. {
  4343. struct ceph_osd_request *req;
  4344. struct page **pages;
  4345. int ret;
  4346. req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
  4347. if (!req)
  4348. return -ENOMEM;
  4349. ceph_oid_copy(&req->r_base_oid, oid);
  4350. ceph_oloc_copy(&req->r_base_oloc, oloc);
  4351. req->r_flags = CEPH_OSD_FLAG_READ;
  4352. pages = ceph_alloc_page_vector(1, GFP_NOIO);
  4353. if (IS_ERR(pages)) {
  4354. ret = PTR_ERR(pages);
  4355. goto out_put_req;
  4356. }
  4357. osd_req_op_init(req, 0, CEPH_OSD_OP_LIST_WATCHERS, 0);
  4358. ceph_osd_data_pages_init(osd_req_op_data(req, 0, list_watchers,
  4359. response_data),
  4360. pages, PAGE_SIZE, 0, false, true);
  4361. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  4362. if (ret)
  4363. goto out_put_req;
  4364. ceph_osdc_start_request(osdc, req);
  4365. ret = ceph_osdc_wait_request(osdc, req);
  4366. if (ret >= 0) {
  4367. void *p = page_address(pages[0]);
  4368. void *const end = p + req->r_ops[0].outdata_len;
  4369. ret = decode_watchers(&p, end, watchers, num_watchers);
  4370. }
  4371. out_put_req:
  4372. ceph_osdc_put_request(req);
  4373. return ret;
  4374. }
  4375. EXPORT_SYMBOL(ceph_osdc_list_watchers);
  4376. /*
  4377. * Call all pending notify callbacks - for use after a watch is
  4378. * unregistered, to make sure no more callbacks for it will be invoked
  4379. */
  4380. void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
  4381. {
  4382. dout("%s osdc %p\n", __func__, osdc);
  4383. flush_workqueue(osdc->notify_wq);
  4384. }
  4385. EXPORT_SYMBOL(ceph_osdc_flush_notifies);
  4386. void ceph_osdc_maybe_request_map(struct ceph_osd_client *osdc)
  4387. {
  4388. down_read(&osdc->lock);
  4389. maybe_request_map(osdc);
  4390. up_read(&osdc->lock);
  4391. }
  4392. EXPORT_SYMBOL(ceph_osdc_maybe_request_map);
  4393. /*
  4394. * Execute an OSD class method on an object.
  4395. *
  4396. * @flags: CEPH_OSD_FLAG_*
  4397. * @resp_len: in/out param for reply length
  4398. */
  4399. int ceph_osdc_call(struct ceph_osd_client *osdc,
  4400. struct ceph_object_id *oid,
  4401. struct ceph_object_locator *oloc,
  4402. const char *class, const char *method,
  4403. unsigned int flags,
  4404. struct page *req_page, size_t req_len,
  4405. struct page **resp_pages, size_t *resp_len)
  4406. {
  4407. struct ceph_osd_request *req;
  4408. int ret;
  4409. if (req_len > PAGE_SIZE)
  4410. return -E2BIG;
  4411. req = ceph_osdc_alloc_request(osdc, NULL, 1, false, GFP_NOIO);
  4412. if (!req)
  4413. return -ENOMEM;
  4414. ceph_oid_copy(&req->r_base_oid, oid);
  4415. ceph_oloc_copy(&req->r_base_oloc, oloc);
  4416. req->r_flags = flags;
  4417. ret = osd_req_op_cls_init(req, 0, class, method);
  4418. if (ret)
  4419. goto out_put_req;
  4420. if (req_page)
  4421. osd_req_op_cls_request_data_pages(req, 0, &req_page, req_len,
  4422. 0, false, false);
  4423. if (resp_pages)
  4424. osd_req_op_cls_response_data_pages(req, 0, resp_pages,
  4425. *resp_len, 0, false, false);
  4426. ret = ceph_osdc_alloc_messages(req, GFP_NOIO);
  4427. if (ret)
  4428. goto out_put_req;
  4429. ceph_osdc_start_request(osdc, req);
  4430. ret = ceph_osdc_wait_request(osdc, req);
  4431. if (ret >= 0) {
  4432. ret = req->r_ops[0].rval;
  4433. if (resp_pages)
  4434. *resp_len = req->r_ops[0].outdata_len;
  4435. }
  4436. out_put_req:
  4437. ceph_osdc_put_request(req);
  4438. return ret;
  4439. }
  4440. EXPORT_SYMBOL(ceph_osdc_call);
  4441. /*
  4442. * reset all osd connections
  4443. */
  4444. void ceph_osdc_reopen_osds(struct ceph_osd_client *osdc)
  4445. {
  4446. struct rb_node *n;
  4447. down_write(&osdc->lock);
  4448. for (n = rb_first(&osdc->osds); n; ) {
  4449. struct ceph_osd *osd = rb_entry(n, struct ceph_osd, o_node);
  4450. n = rb_next(n);
  4451. if (!reopen_osd(osd))
  4452. kick_osd_requests(osd);
  4453. }
  4454. up_write(&osdc->lock);
  4455. }
  4456. /*
  4457. * init, shutdown
  4458. */
  4459. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  4460. {
  4461. int err;
  4462. dout("init\n");
  4463. osdc->client = client;
  4464. init_rwsem(&osdc->lock);
  4465. osdc->osds = RB_ROOT;
  4466. INIT_LIST_HEAD(&osdc->osd_lru);
  4467. spin_lock_init(&osdc->osd_lru_lock);
  4468. osd_init(&osdc->homeless_osd);
  4469. osdc->homeless_osd.o_osdc = osdc;
  4470. osdc->homeless_osd.o_osd = CEPH_HOMELESS_OSD;
  4471. osdc->last_linger_id = CEPH_LINGER_ID_START;
  4472. osdc->linger_requests = RB_ROOT;
  4473. osdc->map_checks = RB_ROOT;
  4474. osdc->linger_map_checks = RB_ROOT;
  4475. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  4476. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  4477. err = -ENOMEM;
  4478. osdc->osdmap = ceph_osdmap_alloc();
  4479. if (!osdc->osdmap)
  4480. goto out;
  4481. osdc->req_mempool = mempool_create_slab_pool(10,
  4482. ceph_osd_request_cache);
  4483. if (!osdc->req_mempool)
  4484. goto out_map;
  4485. err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
  4486. PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10, "osd_op");
  4487. if (err < 0)
  4488. goto out_mempool;
  4489. err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
  4490. PAGE_SIZE, CEPH_OSD_SLAB_OPS, 10,
  4491. "osd_op_reply");
  4492. if (err < 0)
  4493. goto out_msgpool;
  4494. err = -ENOMEM;
  4495. osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
  4496. if (!osdc->notify_wq)
  4497. goto out_msgpool_reply;
  4498. osdc->completion_wq = create_singlethread_workqueue("ceph-completion");
  4499. if (!osdc->completion_wq)
  4500. goto out_notify_wq;
  4501. schedule_delayed_work(&osdc->timeout_work,
  4502. osdc->client->options->osd_keepalive_timeout);
  4503. schedule_delayed_work(&osdc->osds_timeout_work,
  4504. round_jiffies_relative(osdc->client->options->osd_idle_ttl));
  4505. return 0;
  4506. out_notify_wq:
  4507. destroy_workqueue(osdc->notify_wq);
  4508. out_msgpool_reply:
  4509. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  4510. out_msgpool:
  4511. ceph_msgpool_destroy(&osdc->msgpool_op);
  4512. out_mempool:
  4513. mempool_destroy(osdc->req_mempool);
  4514. out_map:
  4515. ceph_osdmap_destroy(osdc->osdmap);
  4516. out:
  4517. return err;
  4518. }
  4519. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  4520. {
  4521. destroy_workqueue(osdc->completion_wq);
  4522. destroy_workqueue(osdc->notify_wq);
  4523. cancel_delayed_work_sync(&osdc->timeout_work);
  4524. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  4525. down_write(&osdc->lock);
  4526. while (!RB_EMPTY_ROOT(&osdc->osds)) {
  4527. struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
  4528. struct ceph_osd, o_node);
  4529. close_osd(osd);
  4530. }
  4531. up_write(&osdc->lock);
  4532. WARN_ON(refcount_read(&osdc->homeless_osd.o_ref) != 1);
  4533. osd_cleanup(&osdc->homeless_osd);
  4534. WARN_ON(!list_empty(&osdc->osd_lru));
  4535. WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_requests));
  4536. WARN_ON(!RB_EMPTY_ROOT(&osdc->map_checks));
  4537. WARN_ON(!RB_EMPTY_ROOT(&osdc->linger_map_checks));
  4538. WARN_ON(atomic_read(&osdc->num_requests));
  4539. WARN_ON(atomic_read(&osdc->num_homeless));
  4540. ceph_osdmap_destroy(osdc->osdmap);
  4541. mempool_destroy(osdc->req_mempool);
  4542. ceph_msgpool_destroy(&osdc->msgpool_op);
  4543. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  4544. }
  4545. int osd_req_op_copy_from_init(struct ceph_osd_request *req,
  4546. u64 src_snapid, u64 src_version,
  4547. struct ceph_object_id *src_oid,
  4548. struct ceph_object_locator *src_oloc,
  4549. u32 src_fadvise_flags,
  4550. u32 dst_fadvise_flags,
  4551. u32 truncate_seq, u64 truncate_size,
  4552. u8 copy_from_flags)
  4553. {
  4554. struct ceph_osd_req_op *op;
  4555. struct page **pages;
  4556. void *p, *end;
  4557. pages = ceph_alloc_page_vector(1, GFP_KERNEL);
  4558. if (IS_ERR(pages))
  4559. return PTR_ERR(pages);
  4560. op = osd_req_op_init(req, 0, CEPH_OSD_OP_COPY_FROM2,
  4561. dst_fadvise_flags);
  4562. op->copy_from.snapid = src_snapid;
  4563. op->copy_from.src_version = src_version;
  4564. op->copy_from.flags = copy_from_flags;
  4565. op->copy_from.src_fadvise_flags = src_fadvise_flags;
  4566. p = page_address(pages[0]);
  4567. end = p + PAGE_SIZE;
  4568. ceph_encode_string(&p, end, src_oid->name, src_oid->name_len);
  4569. encode_oloc(&p, end, src_oloc);
  4570. ceph_encode_32(&p, truncate_seq);
  4571. ceph_encode_64(&p, truncate_size);
  4572. op->indata_len = PAGE_SIZE - (end - p);
  4573. ceph_osd_data_pages_init(&op->copy_from.osd_data, pages,
  4574. op->indata_len, 0, false, true);
  4575. return 0;
  4576. }
  4577. EXPORT_SYMBOL(osd_req_op_copy_from_init);
  4578. int __init ceph_osdc_setup(void)
  4579. {
  4580. size_t size = sizeof(struct ceph_osd_request) +
  4581. CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
  4582. BUG_ON(ceph_osd_request_cache);
  4583. ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
  4584. 0, 0, NULL);
  4585. return ceph_osd_request_cache ? 0 : -ENOMEM;
  4586. }
  4587. void ceph_osdc_cleanup(void)
  4588. {
  4589. BUG_ON(!ceph_osd_request_cache);
  4590. kmem_cache_destroy(ceph_osd_request_cache);
  4591. ceph_osd_request_cache = NULL;
  4592. }
  4593. /*
  4594. * handle incoming message
  4595. */
  4596. static void osd_dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  4597. {
  4598. struct ceph_osd *osd = con->private;
  4599. struct ceph_osd_client *osdc = osd->o_osdc;
  4600. int type = le16_to_cpu(msg->hdr.type);
  4601. switch (type) {
  4602. case CEPH_MSG_OSD_MAP:
  4603. ceph_osdc_handle_map(osdc, msg);
  4604. break;
  4605. case CEPH_MSG_OSD_OPREPLY:
  4606. handle_reply(osd, msg);
  4607. break;
  4608. case CEPH_MSG_OSD_BACKOFF:
  4609. handle_backoff(osd, msg);
  4610. break;
  4611. case CEPH_MSG_WATCH_NOTIFY:
  4612. handle_watch_notify(osdc, msg);
  4613. break;
  4614. default:
  4615. pr_err("received unknown message type %d %s\n", type,
  4616. ceph_msg_type_name(type));
  4617. }
  4618. ceph_msg_put(msg);
  4619. }
  4620. /* How much sparse data was requested? */
  4621. static u64 sparse_data_requested(struct ceph_osd_request *req)
  4622. {
  4623. u64 len = 0;
  4624. if (req->r_flags & CEPH_OSD_FLAG_READ) {
  4625. int i;
  4626. for (i = 0; i < req->r_num_ops; ++i) {
  4627. struct ceph_osd_req_op *op = &req->r_ops[i];
  4628. if (op->op == CEPH_OSD_OP_SPARSE_READ)
  4629. len += op->extent.length;
  4630. }
  4631. }
  4632. return len;
  4633. }
  4634. /*
  4635. * Lookup and return message for incoming reply. Don't try to do
  4636. * anything about a larger than preallocated data portion of the
  4637. * message at the moment - for now, just skip the message.
  4638. */
  4639. static struct ceph_msg *get_reply(struct ceph_connection *con,
  4640. struct ceph_msg_header *hdr,
  4641. int *skip)
  4642. {
  4643. struct ceph_osd *osd = con->private;
  4644. struct ceph_osd_client *osdc = osd->o_osdc;
  4645. struct ceph_msg *m = NULL;
  4646. struct ceph_osd_request *req;
  4647. int front_len = le32_to_cpu(hdr->front_len);
  4648. int data_len = le32_to_cpu(hdr->data_len);
  4649. u64 tid = le64_to_cpu(hdr->tid);
  4650. u64 srlen;
  4651. down_read(&osdc->lock);
  4652. if (!osd_registered(osd)) {
  4653. dout("%s osd%d unknown, skipping\n", __func__, osd->o_osd);
  4654. *skip = 1;
  4655. goto out_unlock_osdc;
  4656. }
  4657. WARN_ON(osd->o_osd != le64_to_cpu(hdr->src.num));
  4658. mutex_lock(&osd->lock);
  4659. req = lookup_request(&osd->o_requests, tid);
  4660. if (!req) {
  4661. dout("%s osd%d tid %llu unknown, skipping\n", __func__,
  4662. osd->o_osd, tid);
  4663. *skip = 1;
  4664. goto out_unlock_session;
  4665. }
  4666. ceph_msg_revoke_incoming(req->r_reply);
  4667. if (front_len > req->r_reply->front_alloc_len) {
  4668. pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
  4669. __func__, osd->o_osd, req->r_tid, front_len,
  4670. req->r_reply->front_alloc_len);
  4671. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
  4672. false);
  4673. if (!m)
  4674. goto out_unlock_session;
  4675. ceph_msg_put(req->r_reply);
  4676. req->r_reply = m;
  4677. }
  4678. srlen = sparse_data_requested(req);
  4679. if (!srlen && data_len > req->r_reply->data_length) {
  4680. pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
  4681. __func__, osd->o_osd, req->r_tid, data_len,
  4682. req->r_reply->data_length);
  4683. m = NULL;
  4684. *skip = 1;
  4685. goto out_unlock_session;
  4686. }
  4687. m = ceph_msg_get(req->r_reply);
  4688. m->sparse_read_total = srlen;
  4689. dout("get_reply tid %lld %p\n", tid, m);
  4690. out_unlock_session:
  4691. mutex_unlock(&osd->lock);
  4692. out_unlock_osdc:
  4693. up_read(&osdc->lock);
  4694. return m;
  4695. }
  4696. static struct ceph_msg *alloc_msg_with_page_vector(struct ceph_msg_header *hdr)
  4697. {
  4698. struct ceph_msg *m;
  4699. int type = le16_to_cpu(hdr->type);
  4700. u32 front_len = le32_to_cpu(hdr->front_len);
  4701. u32 data_len = le32_to_cpu(hdr->data_len);
  4702. m = ceph_msg_new2(type, front_len, 1, GFP_NOIO, false);
  4703. if (!m)
  4704. return NULL;
  4705. if (data_len) {
  4706. struct page **pages;
  4707. pages = ceph_alloc_page_vector(calc_pages_for(0, data_len),
  4708. GFP_NOIO);
  4709. if (IS_ERR(pages)) {
  4710. ceph_msg_put(m);
  4711. return NULL;
  4712. }
  4713. ceph_msg_data_add_pages(m, pages, data_len, 0, true);
  4714. }
  4715. return m;
  4716. }
  4717. static struct ceph_msg *osd_alloc_msg(struct ceph_connection *con,
  4718. struct ceph_msg_header *hdr,
  4719. int *skip)
  4720. {
  4721. struct ceph_osd *osd = con->private;
  4722. int type = le16_to_cpu(hdr->type);
  4723. *skip = 0;
  4724. switch (type) {
  4725. case CEPH_MSG_OSD_MAP:
  4726. case CEPH_MSG_OSD_BACKOFF:
  4727. case CEPH_MSG_WATCH_NOTIFY:
  4728. return alloc_msg_with_page_vector(hdr);
  4729. case CEPH_MSG_OSD_OPREPLY:
  4730. return get_reply(con, hdr, skip);
  4731. default:
  4732. pr_warn("%s osd%d unknown msg type %d, skipping\n", __func__,
  4733. osd->o_osd, type);
  4734. *skip = 1;
  4735. return NULL;
  4736. }
  4737. }
  4738. /*
  4739. * Wrappers to refcount containing ceph_osd struct
  4740. */
  4741. static struct ceph_connection *osd_get_con(struct ceph_connection *con)
  4742. {
  4743. struct ceph_osd *osd = con->private;
  4744. if (get_osd(osd))
  4745. return con;
  4746. return NULL;
  4747. }
  4748. static void osd_put_con(struct ceph_connection *con)
  4749. {
  4750. struct ceph_osd *osd = con->private;
  4751. put_osd(osd);
  4752. }
  4753. /*
  4754. * authentication
  4755. */
  4756. /*
  4757. * Note: returned pointer is the address of a structure that's
  4758. * managed separately. Caller must *not* attempt to free it.
  4759. */
  4760. static struct ceph_auth_handshake *
  4761. osd_get_authorizer(struct ceph_connection *con, int *proto, int force_new)
  4762. {
  4763. struct ceph_osd *o = con->private;
  4764. struct ceph_osd_client *osdc = o->o_osdc;
  4765. struct ceph_auth_client *ac = osdc->client->monc.auth;
  4766. struct ceph_auth_handshake *auth = &o->o_auth;
  4767. int ret;
  4768. ret = __ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
  4769. force_new, proto, NULL, NULL);
  4770. if (ret)
  4771. return ERR_PTR(ret);
  4772. return auth;
  4773. }
  4774. static int osd_add_authorizer_challenge(struct ceph_connection *con,
  4775. void *challenge_buf, int challenge_buf_len)
  4776. {
  4777. struct ceph_osd *o = con->private;
  4778. struct ceph_osd_client *osdc = o->o_osdc;
  4779. struct ceph_auth_client *ac = osdc->client->monc.auth;
  4780. return ceph_auth_add_authorizer_challenge(ac, o->o_auth.authorizer,
  4781. challenge_buf, challenge_buf_len);
  4782. }
  4783. static int osd_verify_authorizer_reply(struct ceph_connection *con)
  4784. {
  4785. struct ceph_osd *o = con->private;
  4786. struct ceph_osd_client *osdc = o->o_osdc;
  4787. struct ceph_auth_client *ac = osdc->client->monc.auth;
  4788. struct ceph_auth_handshake *auth = &o->o_auth;
  4789. return ceph_auth_verify_authorizer_reply(ac, auth->authorizer,
  4790. auth->authorizer_reply_buf, auth->authorizer_reply_buf_len,
  4791. NULL, NULL, NULL, NULL);
  4792. }
  4793. static int osd_invalidate_authorizer(struct ceph_connection *con)
  4794. {
  4795. struct ceph_osd *o = con->private;
  4796. struct ceph_osd_client *osdc = o->o_osdc;
  4797. struct ceph_auth_client *ac = osdc->client->monc.auth;
  4798. ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  4799. return ceph_monc_validate_auth(&osdc->client->monc);
  4800. }
  4801. static int osd_get_auth_request(struct ceph_connection *con,
  4802. void *buf, int *buf_len,
  4803. void **authorizer, int *authorizer_len)
  4804. {
  4805. struct ceph_osd *o = con->private;
  4806. struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
  4807. struct ceph_auth_handshake *auth = &o->o_auth;
  4808. int ret;
  4809. ret = ceph_auth_get_authorizer(ac, auth, CEPH_ENTITY_TYPE_OSD,
  4810. buf, buf_len);
  4811. if (ret)
  4812. return ret;
  4813. *authorizer = auth->authorizer_buf;
  4814. *authorizer_len = auth->authorizer_buf_len;
  4815. return 0;
  4816. }
  4817. static int osd_handle_auth_reply_more(struct ceph_connection *con,
  4818. void *reply, int reply_len,
  4819. void *buf, int *buf_len,
  4820. void **authorizer, int *authorizer_len)
  4821. {
  4822. struct ceph_osd *o = con->private;
  4823. struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
  4824. struct ceph_auth_handshake *auth = &o->o_auth;
  4825. int ret;
  4826. ret = ceph_auth_handle_svc_reply_more(ac, auth, reply, reply_len,
  4827. buf, buf_len);
  4828. if (ret)
  4829. return ret;
  4830. *authorizer = auth->authorizer_buf;
  4831. *authorizer_len = auth->authorizer_buf_len;
  4832. return 0;
  4833. }
  4834. static int osd_handle_auth_done(struct ceph_connection *con,
  4835. u64 global_id, void *reply, int reply_len,
  4836. u8 *session_key, int *session_key_len,
  4837. u8 *con_secret, int *con_secret_len)
  4838. {
  4839. struct ceph_osd *o = con->private;
  4840. struct ceph_auth_client *ac = o->o_osdc->client->monc.auth;
  4841. struct ceph_auth_handshake *auth = &o->o_auth;
  4842. return ceph_auth_handle_svc_reply_done(ac, auth, reply, reply_len,
  4843. session_key, session_key_len,
  4844. con_secret, con_secret_len);
  4845. }
  4846. static int osd_handle_auth_bad_method(struct ceph_connection *con,
  4847. int used_proto, int result,
  4848. const int *allowed_protos, int proto_cnt,
  4849. const int *allowed_modes, int mode_cnt)
  4850. {
  4851. struct ceph_osd *o = con->private;
  4852. struct ceph_mon_client *monc = &o->o_osdc->client->monc;
  4853. int ret;
  4854. if (ceph_auth_handle_bad_authorizer(monc->auth, CEPH_ENTITY_TYPE_OSD,
  4855. used_proto, result,
  4856. allowed_protos, proto_cnt,
  4857. allowed_modes, mode_cnt)) {
  4858. ret = ceph_monc_validate_auth(monc);
  4859. if (ret)
  4860. return ret;
  4861. }
  4862. return -EACCES;
  4863. }
  4864. static void osd_reencode_message(struct ceph_msg *msg)
  4865. {
  4866. int type = le16_to_cpu(msg->hdr.type);
  4867. if (type == CEPH_MSG_OSD_OP)
  4868. encode_request_finish(msg);
  4869. }
  4870. static int osd_sign_message(struct ceph_msg *msg)
  4871. {
  4872. struct ceph_osd *o = msg->con->private;
  4873. struct ceph_auth_handshake *auth = &o->o_auth;
  4874. return ceph_auth_sign_message(auth, msg);
  4875. }
  4876. static int osd_check_message_signature(struct ceph_msg *msg)
  4877. {
  4878. struct ceph_osd *o = msg->con->private;
  4879. struct ceph_auth_handshake *auth = &o->o_auth;
  4880. return ceph_auth_check_message_signature(auth, msg);
  4881. }
  4882. static void advance_cursor(struct ceph_msg_data_cursor *cursor, size_t len,
  4883. bool zero)
  4884. {
  4885. while (len) {
  4886. struct page *page;
  4887. size_t poff, plen;
  4888. page = ceph_msg_data_next(cursor, &poff, &plen);
  4889. if (plen > len)
  4890. plen = len;
  4891. if (zero)
  4892. zero_user_segment(page, poff, poff + plen);
  4893. len -= plen;
  4894. ceph_msg_data_advance(cursor, plen);
  4895. }
  4896. }
  4897. static int prep_next_sparse_read(struct ceph_connection *con,
  4898. struct ceph_msg_data_cursor *cursor)
  4899. {
  4900. struct ceph_osd *o = con->private;
  4901. struct ceph_sparse_read *sr = &o->o_sparse_read;
  4902. struct ceph_osd_request *req;
  4903. struct ceph_osd_req_op *op;
  4904. spin_lock(&o->o_requests_lock);
  4905. req = lookup_request(&o->o_requests, le64_to_cpu(con->in_msg->hdr.tid));
  4906. if (!req) {
  4907. spin_unlock(&o->o_requests_lock);
  4908. return -EBADR;
  4909. }
  4910. if (o->o_sparse_op_idx < 0) {
  4911. dout("%s: [%d] starting new sparse read req\n",
  4912. __func__, o->o_osd);
  4913. } else {
  4914. u64 end;
  4915. op = &req->r_ops[o->o_sparse_op_idx];
  4916. WARN_ON_ONCE(op->extent.sparse_ext);
  4917. /* hand back buffer we took earlier */
  4918. op->extent.sparse_ext = sr->sr_extent;
  4919. sr->sr_extent = NULL;
  4920. op->extent.sparse_ext_cnt = sr->sr_count;
  4921. sr->sr_ext_len = 0;
  4922. dout("%s: [%d] completed extent array len %d cursor->resid %zd\n",
  4923. __func__, o->o_osd, op->extent.sparse_ext_cnt, cursor->resid);
  4924. /* Advance to end of data for this operation */
  4925. end = ceph_sparse_ext_map_end(op);
  4926. if (end < sr->sr_req_len)
  4927. advance_cursor(cursor, sr->sr_req_len - end, false);
  4928. }
  4929. ceph_init_sparse_read(sr);
  4930. /* find next op in this request (if any) */
  4931. while (++o->o_sparse_op_idx < req->r_num_ops) {
  4932. op = &req->r_ops[o->o_sparse_op_idx];
  4933. if (op->op == CEPH_OSD_OP_SPARSE_READ)
  4934. goto found;
  4935. }
  4936. /* reset for next sparse read request */
  4937. spin_unlock(&o->o_requests_lock);
  4938. o->o_sparse_op_idx = -1;
  4939. return 0;
  4940. found:
  4941. sr->sr_req_off = op->extent.offset;
  4942. sr->sr_req_len = op->extent.length;
  4943. sr->sr_pos = sr->sr_req_off;
  4944. dout("%s: [%d] new sparse read op at idx %d 0x%llx~0x%llx\n", __func__,
  4945. o->o_osd, o->o_sparse_op_idx, sr->sr_req_off, sr->sr_req_len);
  4946. /* hand off request's sparse extent map buffer */
  4947. sr->sr_ext_len = op->extent.sparse_ext_cnt;
  4948. op->extent.sparse_ext_cnt = 0;
  4949. sr->sr_extent = op->extent.sparse_ext;
  4950. op->extent.sparse_ext = NULL;
  4951. spin_unlock(&o->o_requests_lock);
  4952. return 1;
  4953. }
  4954. #ifdef __BIG_ENDIAN
  4955. static inline void convert_extent_map(struct ceph_sparse_read *sr)
  4956. {
  4957. int i;
  4958. for (i = 0; i < sr->sr_count; i++) {
  4959. struct ceph_sparse_extent *ext = &sr->sr_extent[i];
  4960. ext->off = le64_to_cpu((__force __le64)ext->off);
  4961. ext->len = le64_to_cpu((__force __le64)ext->len);
  4962. }
  4963. }
  4964. #else
  4965. static inline void convert_extent_map(struct ceph_sparse_read *sr)
  4966. {
  4967. }
  4968. #endif
  4969. static int osd_sparse_read(struct ceph_connection *con,
  4970. struct ceph_msg_data_cursor *cursor,
  4971. char **pbuf)
  4972. {
  4973. struct ceph_osd *o = con->private;
  4974. struct ceph_sparse_read *sr = &o->o_sparse_read;
  4975. u32 count = sr->sr_count;
  4976. u64 eoff, elen, len = 0;
  4977. int i, ret;
  4978. switch (sr->sr_state) {
  4979. case CEPH_SPARSE_READ_HDR:
  4980. next_op:
  4981. ret = prep_next_sparse_read(con, cursor);
  4982. if (ret <= 0)
  4983. return ret;
  4984. /* number of extents */
  4985. ret = sizeof(sr->sr_count);
  4986. *pbuf = (char *)&sr->sr_count;
  4987. sr->sr_state = CEPH_SPARSE_READ_EXTENTS;
  4988. break;
  4989. case CEPH_SPARSE_READ_EXTENTS:
  4990. /* Convert sr_count to host-endian */
  4991. count = le32_to_cpu((__force __le32)sr->sr_count);
  4992. sr->sr_count = count;
  4993. dout("[%d] got %u extents\n", o->o_osd, count);
  4994. if (count > 0) {
  4995. if (!sr->sr_extent || count > sr->sr_ext_len) {
  4996. /* no extent array provided, or too short */
  4997. kfree(sr->sr_extent);
  4998. sr->sr_extent = kmalloc_array(count,
  4999. sizeof(*sr->sr_extent),
  5000. GFP_NOIO);
  5001. if (!sr->sr_extent) {
  5002. pr_err("%s: failed to allocate %u extents\n",
  5003. __func__, count);
  5004. return -ENOMEM;
  5005. }
  5006. sr->sr_ext_len = count;
  5007. }
  5008. ret = count * sizeof(*sr->sr_extent);
  5009. *pbuf = (char *)sr->sr_extent;
  5010. sr->sr_state = CEPH_SPARSE_READ_DATA_LEN;
  5011. break;
  5012. }
  5013. /* No extents? Read data len */
  5014. fallthrough;
  5015. case CEPH_SPARSE_READ_DATA_LEN:
  5016. convert_extent_map(sr);
  5017. ret = sizeof(sr->sr_datalen);
  5018. *pbuf = (char *)&sr->sr_datalen;
  5019. sr->sr_state = CEPH_SPARSE_READ_DATA_PRE;
  5020. break;
  5021. case CEPH_SPARSE_READ_DATA_PRE:
  5022. /* Convert sr_datalen to host-endian */
  5023. sr->sr_datalen = le32_to_cpu((__force __le32)sr->sr_datalen);
  5024. for (i = 0; i < count; i++)
  5025. len += sr->sr_extent[i].len;
  5026. if (sr->sr_datalen != len) {
  5027. pr_warn_ratelimited("data len %u != extent len %llu\n",
  5028. sr->sr_datalen, len);
  5029. return -EREMOTEIO;
  5030. }
  5031. sr->sr_state = CEPH_SPARSE_READ_DATA;
  5032. fallthrough;
  5033. case CEPH_SPARSE_READ_DATA:
  5034. if (sr->sr_index >= count) {
  5035. sr->sr_state = CEPH_SPARSE_READ_HDR;
  5036. goto next_op;
  5037. }
  5038. eoff = sr->sr_extent[sr->sr_index].off;
  5039. elen = sr->sr_extent[sr->sr_index].len;
  5040. dout("[%d] ext %d off 0x%llx len 0x%llx\n",
  5041. o->o_osd, sr->sr_index, eoff, elen);
  5042. if (elen > INT_MAX) {
  5043. dout("Sparse read extent length too long (0x%llx)\n",
  5044. elen);
  5045. return -EREMOTEIO;
  5046. }
  5047. /* zero out anything from sr_pos to start of extent */
  5048. if (sr->sr_pos < eoff)
  5049. advance_cursor(cursor, eoff - sr->sr_pos, true);
  5050. /* Set position to end of extent */
  5051. sr->sr_pos = eoff + elen;
  5052. /* send back the new length and nullify the ptr */
  5053. cursor->sr_resid = elen;
  5054. ret = elen;
  5055. *pbuf = NULL;
  5056. /* Bump the array index */
  5057. ++sr->sr_index;
  5058. break;
  5059. }
  5060. return ret;
  5061. }
  5062. static const struct ceph_connection_operations osd_con_ops = {
  5063. .get = osd_get_con,
  5064. .put = osd_put_con,
  5065. .sparse_read = osd_sparse_read,
  5066. .alloc_msg = osd_alloc_msg,
  5067. .dispatch = osd_dispatch,
  5068. .fault = osd_fault,
  5069. .reencode_message = osd_reencode_message,
  5070. .get_authorizer = osd_get_authorizer,
  5071. .add_authorizer_challenge = osd_add_authorizer_challenge,
  5072. .verify_authorizer_reply = osd_verify_authorizer_reply,
  5073. .invalidate_authorizer = osd_invalidate_authorizer,
  5074. .sign_message = osd_sign_message,
  5075. .check_message_signature = osd_check_message_signature,
  5076. .get_auth_request = osd_get_auth_request,
  5077. .handle_auth_reply_more = osd_handle_auth_reply_more,
  5078. .handle_auth_done = osd_handle_auth_done,
  5079. .handle_auth_bad_method = osd_handle_auth_bad_method,
  5080. };