osd_client.c 140 KB

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