osd_client.c 137 KB

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