osd_client.c 113 KB

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