osd_client.c 117 KB

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