osd_client.c 153 KB

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