osd_client.c 138 KB

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