dm-cache-target.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865
  1. /*
  2. * Copyright (C) 2012 Red Hat. All rights reserved.
  3. *
  4. * This file is released under the GPL.
  5. */
  6. #include "dm.h"
  7. #include "dm-bio-prison.h"
  8. #include "dm-bio-record.h"
  9. #include "dm-cache-metadata.h"
  10. #include <linux/dm-io.h>
  11. #include <linux/dm-kcopyd.h>
  12. #include <linux/jiffies.h>
  13. #include <linux/init.h>
  14. #include <linux/mempool.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #define DM_MSG_PREFIX "cache"
  19. DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(cache_copy_throttle,
  20. "A percentage of time allocated for copying to and/or from cache");
  21. /*----------------------------------------------------------------*/
  22. #define IOT_RESOLUTION 4
  23. struct io_tracker {
  24. spinlock_t lock;
  25. /*
  26. * Sectors of in-flight IO.
  27. */
  28. sector_t in_flight;
  29. /*
  30. * The time, in jiffies, when this device became idle (if it is
  31. * indeed idle).
  32. */
  33. unsigned long idle_time;
  34. unsigned long last_update_time;
  35. };
  36. static void iot_init(struct io_tracker *iot)
  37. {
  38. spin_lock_init(&iot->lock);
  39. iot->in_flight = 0ul;
  40. iot->idle_time = 0ul;
  41. iot->last_update_time = jiffies;
  42. }
  43. static bool __iot_idle_for(struct io_tracker *iot, unsigned long jifs)
  44. {
  45. if (iot->in_flight)
  46. return false;
  47. return time_after(jiffies, iot->idle_time + jifs);
  48. }
  49. static bool iot_idle_for(struct io_tracker *iot, unsigned long jifs)
  50. {
  51. bool r;
  52. unsigned long flags;
  53. spin_lock_irqsave(&iot->lock, flags);
  54. r = __iot_idle_for(iot, jifs);
  55. spin_unlock_irqrestore(&iot->lock, flags);
  56. return r;
  57. }
  58. static void iot_io_begin(struct io_tracker *iot, sector_t len)
  59. {
  60. unsigned long flags;
  61. spin_lock_irqsave(&iot->lock, flags);
  62. iot->in_flight += len;
  63. spin_unlock_irqrestore(&iot->lock, flags);
  64. }
  65. static void __iot_io_end(struct io_tracker *iot, sector_t len)
  66. {
  67. iot->in_flight -= len;
  68. if (!iot->in_flight)
  69. iot->idle_time = jiffies;
  70. }
  71. static void iot_io_end(struct io_tracker *iot, sector_t len)
  72. {
  73. unsigned long flags;
  74. spin_lock_irqsave(&iot->lock, flags);
  75. __iot_io_end(iot, len);
  76. spin_unlock_irqrestore(&iot->lock, flags);
  77. }
  78. /*----------------------------------------------------------------*/
  79. /*
  80. * Glossary:
  81. *
  82. * oblock: index of an origin block
  83. * cblock: index of a cache block
  84. * promotion: movement of a block from origin to cache
  85. * demotion: movement of a block from cache to origin
  86. * migration: movement of a block between the origin and cache device,
  87. * either direction
  88. */
  89. /*----------------------------------------------------------------*/
  90. /*
  91. * There are a couple of places where we let a bio run, but want to do some
  92. * work before calling its endio function. We do this by temporarily
  93. * changing the endio fn.
  94. */
  95. struct dm_hook_info {
  96. bio_end_io_t *bi_end_io;
  97. };
  98. static void dm_hook_bio(struct dm_hook_info *h, struct bio *bio,
  99. bio_end_io_t *bi_end_io, void *bi_private)
  100. {
  101. h->bi_end_io = bio->bi_end_io;
  102. bio->bi_end_io = bi_end_io;
  103. bio->bi_private = bi_private;
  104. }
  105. static void dm_unhook_bio(struct dm_hook_info *h, struct bio *bio)
  106. {
  107. bio->bi_end_io = h->bi_end_io;
  108. }
  109. /*----------------------------------------------------------------*/
  110. #define MIGRATION_POOL_SIZE 128
  111. #define COMMIT_PERIOD HZ
  112. #define MIGRATION_COUNT_WINDOW 10
  113. /*
  114. * The block size of the device holding cache data must be
  115. * between 32KB and 1GB.
  116. */
  117. #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
  118. #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
  119. enum cache_metadata_mode {
  120. CM_WRITE, /* metadata may be changed */
  121. CM_READ_ONLY, /* metadata may not be changed */
  122. CM_FAIL
  123. };
  124. enum cache_io_mode {
  125. /*
  126. * Data is written to cached blocks only. These blocks are marked
  127. * dirty. If you lose the cache device you will lose data.
  128. * Potential performance increase for both reads and writes.
  129. */
  130. CM_IO_WRITEBACK,
  131. /*
  132. * Data is written to both cache and origin. Blocks are never
  133. * dirty. Potential performance benfit for reads only.
  134. */
  135. CM_IO_WRITETHROUGH,
  136. /*
  137. * A degraded mode useful for various cache coherency situations
  138. * (eg, rolling back snapshots). Reads and writes always go to the
  139. * origin. If a write goes to a cached oblock, then the cache
  140. * block is invalidated.
  141. */
  142. CM_IO_PASSTHROUGH
  143. };
  144. struct cache_features {
  145. enum cache_metadata_mode mode;
  146. enum cache_io_mode io_mode;
  147. };
  148. struct cache_stats {
  149. atomic_t read_hit;
  150. atomic_t read_miss;
  151. atomic_t write_hit;
  152. atomic_t write_miss;
  153. atomic_t demotion;
  154. atomic_t promotion;
  155. atomic_t copies_avoided;
  156. atomic_t cache_cell_clash;
  157. atomic_t commit_count;
  158. atomic_t discard_count;
  159. };
  160. /*
  161. * Defines a range of cblocks, begin to (end - 1) are in the range. end is
  162. * the one-past-the-end value.
  163. */
  164. struct cblock_range {
  165. dm_cblock_t begin;
  166. dm_cblock_t end;
  167. };
  168. struct invalidation_request {
  169. struct list_head list;
  170. struct cblock_range *cblocks;
  171. atomic_t complete;
  172. int err;
  173. wait_queue_head_t result_wait;
  174. };
  175. struct cache {
  176. struct dm_target *ti;
  177. struct dm_target_callbacks callbacks;
  178. struct dm_cache_metadata *cmd;
  179. /*
  180. * Metadata is written to this device.
  181. */
  182. struct dm_dev *metadata_dev;
  183. /*
  184. * The slower of the two data devices. Typically a spindle.
  185. */
  186. struct dm_dev *origin_dev;
  187. /*
  188. * The faster of the two data devices. Typically an SSD.
  189. */
  190. struct dm_dev *cache_dev;
  191. /*
  192. * Size of the origin device in _complete_ blocks and native sectors.
  193. */
  194. dm_oblock_t origin_blocks;
  195. sector_t origin_sectors;
  196. /*
  197. * Size of the cache device in blocks.
  198. */
  199. dm_cblock_t cache_size;
  200. /*
  201. * Fields for converting from sectors to blocks.
  202. */
  203. sector_t sectors_per_block;
  204. int sectors_per_block_shift;
  205. spinlock_t lock;
  206. struct list_head deferred_cells;
  207. struct bio_list deferred_bios;
  208. struct bio_list deferred_flush_bios;
  209. struct bio_list deferred_writethrough_bios;
  210. struct list_head quiesced_migrations;
  211. struct list_head completed_migrations;
  212. struct list_head need_commit_migrations;
  213. sector_t migration_threshold;
  214. wait_queue_head_t migration_wait;
  215. atomic_t nr_allocated_migrations;
  216. /*
  217. * The number of in flight migrations that are performing
  218. * background io. eg, promotion, writeback.
  219. */
  220. atomic_t nr_io_migrations;
  221. wait_queue_head_t quiescing_wait;
  222. atomic_t quiescing;
  223. atomic_t quiescing_ack;
  224. /*
  225. * cache_size entries, dirty if set
  226. */
  227. atomic_t nr_dirty;
  228. unsigned long *dirty_bitset;
  229. /*
  230. * origin_blocks entries, discarded if set.
  231. */
  232. dm_dblock_t discard_nr_blocks;
  233. unsigned long *discard_bitset;
  234. uint32_t discard_block_size; /* a power of 2 times sectors per block */
  235. /*
  236. * Rather than reconstructing the table line for the status we just
  237. * save it and regurgitate.
  238. */
  239. unsigned nr_ctr_args;
  240. const char **ctr_args;
  241. struct dm_kcopyd_client *copier;
  242. struct workqueue_struct *wq;
  243. struct work_struct worker;
  244. struct delayed_work waker;
  245. unsigned long last_commit_jiffies;
  246. struct dm_bio_prison *prison;
  247. struct dm_deferred_set *all_io_ds;
  248. mempool_t *migration_pool;
  249. struct dm_cache_policy *policy;
  250. unsigned policy_nr_args;
  251. bool need_tick_bio:1;
  252. bool sized:1;
  253. bool invalidate:1;
  254. bool commit_requested:1;
  255. bool loaded_mappings:1;
  256. bool loaded_discards:1;
  257. /*
  258. * Cache features such as write-through.
  259. */
  260. struct cache_features features;
  261. struct cache_stats stats;
  262. /*
  263. * Invalidation fields.
  264. */
  265. spinlock_t invalidation_lock;
  266. struct list_head invalidation_requests;
  267. struct io_tracker origin_tracker;
  268. };
  269. struct per_bio_data {
  270. bool tick:1;
  271. unsigned req_nr:2;
  272. struct dm_deferred_entry *all_io_entry;
  273. struct dm_hook_info hook_info;
  274. sector_t len;
  275. /*
  276. * writethrough fields. These MUST remain at the end of this
  277. * structure and the 'cache' member must be the first as it
  278. * is used to determine the offset of the writethrough fields.
  279. */
  280. struct cache *cache;
  281. dm_cblock_t cblock;
  282. struct dm_bio_details bio_details;
  283. };
  284. struct dm_cache_migration {
  285. struct list_head list;
  286. struct cache *cache;
  287. unsigned long start_jiffies;
  288. dm_oblock_t old_oblock;
  289. dm_oblock_t new_oblock;
  290. dm_cblock_t cblock;
  291. bool err:1;
  292. bool discard:1;
  293. bool writeback:1;
  294. bool demote:1;
  295. bool promote:1;
  296. bool requeue_holder:1;
  297. bool invalidate:1;
  298. struct dm_bio_prison_cell *old_ocell;
  299. struct dm_bio_prison_cell *new_ocell;
  300. };
  301. /*
  302. * Processing a bio in the worker thread may require these memory
  303. * allocations. We prealloc to avoid deadlocks (the same worker thread
  304. * frees them back to the mempool).
  305. */
  306. struct prealloc {
  307. struct dm_cache_migration *mg;
  308. struct dm_bio_prison_cell *cell1;
  309. struct dm_bio_prison_cell *cell2;
  310. };
  311. static enum cache_metadata_mode get_cache_mode(struct cache *cache);
  312. static void wake_worker(struct cache *cache)
  313. {
  314. queue_work(cache->wq, &cache->worker);
  315. }
  316. /*----------------------------------------------------------------*/
  317. static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
  318. {
  319. /* FIXME: change to use a local slab. */
  320. return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
  321. }
  322. static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
  323. {
  324. dm_bio_prison_free_cell(cache->prison, cell);
  325. }
  326. static struct dm_cache_migration *alloc_migration(struct cache *cache)
  327. {
  328. struct dm_cache_migration *mg;
  329. mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
  330. if (mg) {
  331. mg->cache = cache;
  332. atomic_inc(&mg->cache->nr_allocated_migrations);
  333. }
  334. return mg;
  335. }
  336. static void free_migration(struct dm_cache_migration *mg)
  337. {
  338. struct cache *cache = mg->cache;
  339. if (atomic_dec_and_test(&cache->nr_allocated_migrations))
  340. wake_up(&cache->migration_wait);
  341. mempool_free(mg, cache->migration_pool);
  342. }
  343. static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
  344. {
  345. if (!p->mg) {
  346. p->mg = alloc_migration(cache);
  347. if (!p->mg)
  348. return -ENOMEM;
  349. }
  350. if (!p->cell1) {
  351. p->cell1 = alloc_prison_cell(cache);
  352. if (!p->cell1)
  353. return -ENOMEM;
  354. }
  355. if (!p->cell2) {
  356. p->cell2 = alloc_prison_cell(cache);
  357. if (!p->cell2)
  358. return -ENOMEM;
  359. }
  360. return 0;
  361. }
  362. static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
  363. {
  364. if (p->cell2)
  365. free_prison_cell(cache, p->cell2);
  366. if (p->cell1)
  367. free_prison_cell(cache, p->cell1);
  368. if (p->mg)
  369. free_migration(p->mg);
  370. }
  371. static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
  372. {
  373. struct dm_cache_migration *mg = p->mg;
  374. BUG_ON(!mg);
  375. p->mg = NULL;
  376. return mg;
  377. }
  378. /*
  379. * You must have a cell within the prealloc struct to return. If not this
  380. * function will BUG() rather than returning NULL.
  381. */
  382. static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
  383. {
  384. struct dm_bio_prison_cell *r = NULL;
  385. if (p->cell1) {
  386. r = p->cell1;
  387. p->cell1 = NULL;
  388. } else if (p->cell2) {
  389. r = p->cell2;
  390. p->cell2 = NULL;
  391. } else
  392. BUG();
  393. return r;
  394. }
  395. /*
  396. * You can't have more than two cells in a prealloc struct. BUG() will be
  397. * called if you try and overfill.
  398. */
  399. static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
  400. {
  401. if (!p->cell2)
  402. p->cell2 = cell;
  403. else if (!p->cell1)
  404. p->cell1 = cell;
  405. else
  406. BUG();
  407. }
  408. /*----------------------------------------------------------------*/
  409. static void build_key(dm_oblock_t begin, dm_oblock_t end, struct dm_cell_key *key)
  410. {
  411. key->virtual = 0;
  412. key->dev = 0;
  413. key->block_begin = from_oblock(begin);
  414. key->block_end = from_oblock(end);
  415. }
  416. /*
  417. * The caller hands in a preallocated cell, and a free function for it.
  418. * The cell will be freed if there's an error, or if it wasn't used because
  419. * a cell with that key already exists.
  420. */
  421. typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
  422. static int bio_detain_range(struct cache *cache, dm_oblock_t oblock_begin, dm_oblock_t oblock_end,
  423. struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
  424. cell_free_fn free_fn, void *free_context,
  425. struct dm_bio_prison_cell **cell_result)
  426. {
  427. int r;
  428. struct dm_cell_key key;
  429. build_key(oblock_begin, oblock_end, &key);
  430. r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
  431. if (r)
  432. free_fn(free_context, cell_prealloc);
  433. return r;
  434. }
  435. static int bio_detain(struct cache *cache, dm_oblock_t oblock,
  436. struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
  437. cell_free_fn free_fn, void *free_context,
  438. struct dm_bio_prison_cell **cell_result)
  439. {
  440. dm_oblock_t end = to_oblock(from_oblock(oblock) + 1ULL);
  441. return bio_detain_range(cache, oblock, end, bio,
  442. cell_prealloc, free_fn, free_context, cell_result);
  443. }
  444. static int get_cell(struct cache *cache,
  445. dm_oblock_t oblock,
  446. struct prealloc *structs,
  447. struct dm_bio_prison_cell **cell_result)
  448. {
  449. int r;
  450. struct dm_cell_key key;
  451. struct dm_bio_prison_cell *cell_prealloc;
  452. cell_prealloc = prealloc_get_cell(structs);
  453. build_key(oblock, to_oblock(from_oblock(oblock) + 1ULL), &key);
  454. r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
  455. if (r)
  456. prealloc_put_cell(structs, cell_prealloc);
  457. return r;
  458. }
  459. /*----------------------------------------------------------------*/
  460. static bool is_dirty(struct cache *cache, dm_cblock_t b)
  461. {
  462. return test_bit(from_cblock(b), cache->dirty_bitset);
  463. }
  464. static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
  465. {
  466. if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
  467. atomic_inc(&cache->nr_dirty);
  468. policy_set_dirty(cache->policy, oblock);
  469. }
  470. }
  471. static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
  472. {
  473. if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
  474. policy_clear_dirty(cache->policy, oblock);
  475. if (atomic_dec_return(&cache->nr_dirty) == 0)
  476. dm_table_event(cache->ti->table);
  477. }
  478. }
  479. /*----------------------------------------------------------------*/
  480. static bool block_size_is_power_of_two(struct cache *cache)
  481. {
  482. return cache->sectors_per_block_shift >= 0;
  483. }
  484. /* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
  485. #if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
  486. __always_inline
  487. #endif
  488. static dm_block_t block_div(dm_block_t b, uint32_t n)
  489. {
  490. do_div(b, n);
  491. return b;
  492. }
  493. static dm_block_t oblocks_per_dblock(struct cache *cache)
  494. {
  495. dm_block_t oblocks = cache->discard_block_size;
  496. if (block_size_is_power_of_two(cache))
  497. oblocks >>= cache->sectors_per_block_shift;
  498. else
  499. oblocks = block_div(oblocks, cache->sectors_per_block);
  500. return oblocks;
  501. }
  502. static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock)
  503. {
  504. return to_dblock(block_div(from_oblock(oblock),
  505. oblocks_per_dblock(cache)));
  506. }
  507. static dm_oblock_t dblock_to_oblock(struct cache *cache, dm_dblock_t dblock)
  508. {
  509. return to_oblock(from_dblock(dblock) * oblocks_per_dblock(cache));
  510. }
  511. static void set_discard(struct cache *cache, dm_dblock_t b)
  512. {
  513. unsigned long flags;
  514. BUG_ON(from_dblock(b) >= from_dblock(cache->discard_nr_blocks));
  515. atomic_inc(&cache->stats.discard_count);
  516. spin_lock_irqsave(&cache->lock, flags);
  517. set_bit(from_dblock(b), cache->discard_bitset);
  518. spin_unlock_irqrestore(&cache->lock, flags);
  519. }
  520. static void clear_discard(struct cache *cache, dm_dblock_t b)
  521. {
  522. unsigned long flags;
  523. spin_lock_irqsave(&cache->lock, flags);
  524. clear_bit(from_dblock(b), cache->discard_bitset);
  525. spin_unlock_irqrestore(&cache->lock, flags);
  526. }
  527. static bool is_discarded(struct cache *cache, dm_dblock_t b)
  528. {
  529. int r;
  530. unsigned long flags;
  531. spin_lock_irqsave(&cache->lock, flags);
  532. r = test_bit(from_dblock(b), cache->discard_bitset);
  533. spin_unlock_irqrestore(&cache->lock, flags);
  534. return r;
  535. }
  536. static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
  537. {
  538. int r;
  539. unsigned long flags;
  540. spin_lock_irqsave(&cache->lock, flags);
  541. r = test_bit(from_dblock(oblock_to_dblock(cache, b)),
  542. cache->discard_bitset);
  543. spin_unlock_irqrestore(&cache->lock, flags);
  544. return r;
  545. }
  546. /*----------------------------------------------------------------*/
  547. static void load_stats(struct cache *cache)
  548. {
  549. struct dm_cache_statistics stats;
  550. dm_cache_metadata_get_stats(cache->cmd, &stats);
  551. atomic_set(&cache->stats.read_hit, stats.read_hits);
  552. atomic_set(&cache->stats.read_miss, stats.read_misses);
  553. atomic_set(&cache->stats.write_hit, stats.write_hits);
  554. atomic_set(&cache->stats.write_miss, stats.write_misses);
  555. }
  556. static void save_stats(struct cache *cache)
  557. {
  558. struct dm_cache_statistics stats;
  559. if (get_cache_mode(cache) >= CM_READ_ONLY)
  560. return;
  561. stats.read_hits = atomic_read(&cache->stats.read_hit);
  562. stats.read_misses = atomic_read(&cache->stats.read_miss);
  563. stats.write_hits = atomic_read(&cache->stats.write_hit);
  564. stats.write_misses = atomic_read(&cache->stats.write_miss);
  565. dm_cache_metadata_set_stats(cache->cmd, &stats);
  566. }
  567. /*----------------------------------------------------------------
  568. * Per bio data
  569. *--------------------------------------------------------------*/
  570. /*
  571. * If using writeback, leave out struct per_bio_data's writethrough fields.
  572. */
  573. #define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
  574. #define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
  575. static bool writethrough_mode(struct cache_features *f)
  576. {
  577. return f->io_mode == CM_IO_WRITETHROUGH;
  578. }
  579. static bool writeback_mode(struct cache_features *f)
  580. {
  581. return f->io_mode == CM_IO_WRITEBACK;
  582. }
  583. static bool passthrough_mode(struct cache_features *f)
  584. {
  585. return f->io_mode == CM_IO_PASSTHROUGH;
  586. }
  587. static size_t get_per_bio_data_size(struct cache *cache)
  588. {
  589. return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
  590. }
  591. static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
  592. {
  593. struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
  594. BUG_ON(!pb);
  595. return pb;
  596. }
  597. static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
  598. {
  599. struct per_bio_data *pb = get_per_bio_data(bio, data_size);
  600. pb->tick = false;
  601. pb->req_nr = dm_bio_get_target_bio_nr(bio);
  602. pb->all_io_entry = NULL;
  603. pb->len = 0;
  604. return pb;
  605. }
  606. /*----------------------------------------------------------------
  607. * Remapping
  608. *--------------------------------------------------------------*/
  609. static void remap_to_origin(struct cache *cache, struct bio *bio)
  610. {
  611. bio->bi_bdev = cache->origin_dev->bdev;
  612. }
  613. static void remap_to_cache(struct cache *cache, struct bio *bio,
  614. dm_cblock_t cblock)
  615. {
  616. sector_t bi_sector = bio->bi_iter.bi_sector;
  617. sector_t block = from_cblock(cblock);
  618. bio->bi_bdev = cache->cache_dev->bdev;
  619. if (!block_size_is_power_of_two(cache))
  620. bio->bi_iter.bi_sector =
  621. (block * cache->sectors_per_block) +
  622. sector_div(bi_sector, cache->sectors_per_block);
  623. else
  624. bio->bi_iter.bi_sector =
  625. (block << cache->sectors_per_block_shift) |
  626. (bi_sector & (cache->sectors_per_block - 1));
  627. }
  628. static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
  629. {
  630. unsigned long flags;
  631. size_t pb_data_size = get_per_bio_data_size(cache);
  632. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  633. spin_lock_irqsave(&cache->lock, flags);
  634. if (cache->need_tick_bio &&
  635. !(bio->bi_opf & (REQ_FUA | REQ_PREFLUSH)) &&
  636. bio_op(bio) != REQ_OP_DISCARD) {
  637. pb->tick = true;
  638. cache->need_tick_bio = false;
  639. }
  640. spin_unlock_irqrestore(&cache->lock, flags);
  641. }
  642. static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
  643. dm_oblock_t oblock)
  644. {
  645. check_if_tick_bio_needed(cache, bio);
  646. remap_to_origin(cache, bio);
  647. if (bio_data_dir(bio) == WRITE)
  648. clear_discard(cache, oblock_to_dblock(cache, oblock));
  649. }
  650. static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
  651. dm_oblock_t oblock, dm_cblock_t cblock)
  652. {
  653. check_if_tick_bio_needed(cache, bio);
  654. remap_to_cache(cache, bio, cblock);
  655. if (bio_data_dir(bio) == WRITE) {
  656. set_dirty(cache, oblock, cblock);
  657. clear_discard(cache, oblock_to_dblock(cache, oblock));
  658. }
  659. }
  660. static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
  661. {
  662. sector_t block_nr = bio->bi_iter.bi_sector;
  663. if (!block_size_is_power_of_two(cache))
  664. (void) sector_div(block_nr, cache->sectors_per_block);
  665. else
  666. block_nr >>= cache->sectors_per_block_shift;
  667. return to_oblock(block_nr);
  668. }
  669. static int bio_triggers_commit(struct cache *cache, struct bio *bio)
  670. {
  671. return bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
  672. }
  673. /*
  674. * You must increment the deferred set whilst the prison cell is held. To
  675. * encourage this, we ask for 'cell' to be passed in.
  676. */
  677. static void inc_ds(struct cache *cache, struct bio *bio,
  678. struct dm_bio_prison_cell *cell)
  679. {
  680. size_t pb_data_size = get_per_bio_data_size(cache);
  681. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  682. BUG_ON(!cell);
  683. BUG_ON(pb->all_io_entry);
  684. pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
  685. }
  686. static bool accountable_bio(struct cache *cache, struct bio *bio)
  687. {
  688. return ((bio->bi_bdev == cache->origin_dev->bdev) &&
  689. bio_op(bio) != REQ_OP_DISCARD);
  690. }
  691. static void accounted_begin(struct cache *cache, struct bio *bio)
  692. {
  693. size_t pb_data_size = get_per_bio_data_size(cache);
  694. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  695. if (accountable_bio(cache, bio)) {
  696. pb->len = bio_sectors(bio);
  697. iot_io_begin(&cache->origin_tracker, pb->len);
  698. }
  699. }
  700. static void accounted_complete(struct cache *cache, struct bio *bio)
  701. {
  702. size_t pb_data_size = get_per_bio_data_size(cache);
  703. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  704. iot_io_end(&cache->origin_tracker, pb->len);
  705. }
  706. static void accounted_request(struct cache *cache, struct bio *bio)
  707. {
  708. accounted_begin(cache, bio);
  709. generic_make_request(bio);
  710. }
  711. static void issue(struct cache *cache, struct bio *bio)
  712. {
  713. unsigned long flags;
  714. if (!bio_triggers_commit(cache, bio)) {
  715. accounted_request(cache, bio);
  716. return;
  717. }
  718. /*
  719. * Batch together any bios that trigger commits and then issue a
  720. * single commit for them in do_worker().
  721. */
  722. spin_lock_irqsave(&cache->lock, flags);
  723. cache->commit_requested = true;
  724. bio_list_add(&cache->deferred_flush_bios, bio);
  725. spin_unlock_irqrestore(&cache->lock, flags);
  726. }
  727. static void inc_and_issue(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell *cell)
  728. {
  729. inc_ds(cache, bio, cell);
  730. issue(cache, bio);
  731. }
  732. static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
  733. {
  734. unsigned long flags;
  735. spin_lock_irqsave(&cache->lock, flags);
  736. bio_list_add(&cache->deferred_writethrough_bios, bio);
  737. spin_unlock_irqrestore(&cache->lock, flags);
  738. wake_worker(cache);
  739. }
  740. static void writethrough_endio(struct bio *bio)
  741. {
  742. struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
  743. dm_unhook_bio(&pb->hook_info, bio);
  744. if (bio->bi_error) {
  745. bio_endio(bio);
  746. return;
  747. }
  748. dm_bio_restore(&pb->bio_details, bio);
  749. remap_to_cache(pb->cache, bio, pb->cblock);
  750. /*
  751. * We can't issue this bio directly, since we're in interrupt
  752. * context. So it gets put on a bio list for processing by the
  753. * worker thread.
  754. */
  755. defer_writethrough_bio(pb->cache, bio);
  756. }
  757. /*
  758. * When running in writethrough mode we need to send writes to clean blocks
  759. * to both the cache and origin devices. In future we'd like to clone the
  760. * bio and send them in parallel, but for now we're doing them in
  761. * series as this is easier.
  762. */
  763. static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
  764. dm_oblock_t oblock, dm_cblock_t cblock)
  765. {
  766. struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
  767. pb->cache = cache;
  768. pb->cblock = cblock;
  769. dm_hook_bio(&pb->hook_info, bio, writethrough_endio, NULL);
  770. dm_bio_record(&pb->bio_details, bio);
  771. remap_to_origin_clear_discard(pb->cache, bio, oblock);
  772. }
  773. /*----------------------------------------------------------------
  774. * Failure modes
  775. *--------------------------------------------------------------*/
  776. static enum cache_metadata_mode get_cache_mode(struct cache *cache)
  777. {
  778. return cache->features.mode;
  779. }
  780. static const char *cache_device_name(struct cache *cache)
  781. {
  782. return dm_device_name(dm_table_get_md(cache->ti->table));
  783. }
  784. static void notify_mode_switch(struct cache *cache, enum cache_metadata_mode mode)
  785. {
  786. const char *descs[] = {
  787. "write",
  788. "read-only",
  789. "fail"
  790. };
  791. dm_table_event(cache->ti->table);
  792. DMINFO("%s: switching cache to %s mode",
  793. cache_device_name(cache), descs[(int)mode]);
  794. }
  795. static void set_cache_mode(struct cache *cache, enum cache_metadata_mode new_mode)
  796. {
  797. bool needs_check;
  798. enum cache_metadata_mode old_mode = get_cache_mode(cache);
  799. if (dm_cache_metadata_needs_check(cache->cmd, &needs_check)) {
  800. DMERR("unable to read needs_check flag, setting failure mode");
  801. new_mode = CM_FAIL;
  802. }
  803. if (new_mode == CM_WRITE && needs_check) {
  804. DMERR("%s: unable to switch cache to write mode until repaired.",
  805. cache_device_name(cache));
  806. if (old_mode != new_mode)
  807. new_mode = old_mode;
  808. else
  809. new_mode = CM_READ_ONLY;
  810. }
  811. /* Never move out of fail mode */
  812. if (old_mode == CM_FAIL)
  813. new_mode = CM_FAIL;
  814. switch (new_mode) {
  815. case CM_FAIL:
  816. case CM_READ_ONLY:
  817. dm_cache_metadata_set_read_only(cache->cmd);
  818. break;
  819. case CM_WRITE:
  820. dm_cache_metadata_set_read_write(cache->cmd);
  821. break;
  822. }
  823. cache->features.mode = new_mode;
  824. if (new_mode != old_mode)
  825. notify_mode_switch(cache, new_mode);
  826. }
  827. static void abort_transaction(struct cache *cache)
  828. {
  829. const char *dev_name = cache_device_name(cache);
  830. if (get_cache_mode(cache) >= CM_READ_ONLY)
  831. return;
  832. if (dm_cache_metadata_set_needs_check(cache->cmd)) {
  833. DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
  834. set_cache_mode(cache, CM_FAIL);
  835. }
  836. DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
  837. if (dm_cache_metadata_abort(cache->cmd)) {
  838. DMERR("%s: failed to abort metadata transaction", dev_name);
  839. set_cache_mode(cache, CM_FAIL);
  840. }
  841. }
  842. static void metadata_operation_failed(struct cache *cache, const char *op, int r)
  843. {
  844. DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
  845. cache_device_name(cache), op, r);
  846. abort_transaction(cache);
  847. set_cache_mode(cache, CM_READ_ONLY);
  848. }
  849. /*----------------------------------------------------------------
  850. * Migration processing
  851. *
  852. * Migration covers moving data from the origin device to the cache, or
  853. * vice versa.
  854. *--------------------------------------------------------------*/
  855. static void inc_io_migrations(struct cache *cache)
  856. {
  857. atomic_inc(&cache->nr_io_migrations);
  858. }
  859. static void dec_io_migrations(struct cache *cache)
  860. {
  861. atomic_dec(&cache->nr_io_migrations);
  862. }
  863. static bool discard_or_flush(struct bio *bio)
  864. {
  865. return bio_op(bio) == REQ_OP_DISCARD ||
  866. bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
  867. }
  868. static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell)
  869. {
  870. if (discard_or_flush(cell->holder)) {
  871. /*
  872. * We have to handle these bios individually.
  873. */
  874. dm_cell_release(cache->prison, cell, &cache->deferred_bios);
  875. free_prison_cell(cache, cell);
  876. } else
  877. list_add_tail(&cell->user_list, &cache->deferred_cells);
  878. }
  879. static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell, bool holder)
  880. {
  881. unsigned long flags;
  882. if (!holder && dm_cell_promote_or_release(cache->prison, cell)) {
  883. /*
  884. * There was no prisoner to promote to holder, the
  885. * cell has been released.
  886. */
  887. free_prison_cell(cache, cell);
  888. return;
  889. }
  890. spin_lock_irqsave(&cache->lock, flags);
  891. __cell_defer(cache, cell);
  892. spin_unlock_irqrestore(&cache->lock, flags);
  893. wake_worker(cache);
  894. }
  895. static void cell_error_with_code(struct cache *cache, struct dm_bio_prison_cell *cell, int err)
  896. {
  897. dm_cell_error(cache->prison, cell, err);
  898. free_prison_cell(cache, cell);
  899. }
  900. static void cell_requeue(struct cache *cache, struct dm_bio_prison_cell *cell)
  901. {
  902. cell_error_with_code(cache, cell, DM_ENDIO_REQUEUE);
  903. }
  904. static void free_io_migration(struct dm_cache_migration *mg)
  905. {
  906. struct cache *cache = mg->cache;
  907. dec_io_migrations(cache);
  908. free_migration(mg);
  909. wake_worker(cache);
  910. }
  911. static void migration_failure(struct dm_cache_migration *mg)
  912. {
  913. struct cache *cache = mg->cache;
  914. const char *dev_name = cache_device_name(cache);
  915. if (mg->writeback) {
  916. DMERR_LIMIT("%s: writeback failed; couldn't copy block", dev_name);
  917. set_dirty(cache, mg->old_oblock, mg->cblock);
  918. cell_defer(cache, mg->old_ocell, false);
  919. } else if (mg->demote) {
  920. DMERR_LIMIT("%s: demotion failed; couldn't copy block", dev_name);
  921. policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
  922. cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
  923. if (mg->promote)
  924. cell_defer(cache, mg->new_ocell, true);
  925. } else {
  926. DMERR_LIMIT("%s: promotion failed; couldn't copy block", dev_name);
  927. policy_remove_mapping(cache->policy, mg->new_oblock);
  928. cell_defer(cache, mg->new_ocell, true);
  929. }
  930. free_io_migration(mg);
  931. }
  932. static void migration_success_pre_commit(struct dm_cache_migration *mg)
  933. {
  934. int r;
  935. unsigned long flags;
  936. struct cache *cache = mg->cache;
  937. if (mg->writeback) {
  938. clear_dirty(cache, mg->old_oblock, mg->cblock);
  939. cell_defer(cache, mg->old_ocell, false);
  940. free_io_migration(mg);
  941. return;
  942. } else if (mg->demote) {
  943. r = dm_cache_remove_mapping(cache->cmd, mg->cblock);
  944. if (r) {
  945. DMERR_LIMIT("%s: demotion failed; couldn't update on disk metadata",
  946. cache_device_name(cache));
  947. metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
  948. policy_force_mapping(cache->policy, mg->new_oblock,
  949. mg->old_oblock);
  950. if (mg->promote)
  951. cell_defer(cache, mg->new_ocell, true);
  952. free_io_migration(mg);
  953. return;
  954. }
  955. } else {
  956. r = dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock);
  957. if (r) {
  958. DMERR_LIMIT("%s: promotion failed; couldn't update on disk metadata",
  959. cache_device_name(cache));
  960. metadata_operation_failed(cache, "dm_cache_insert_mapping", r);
  961. policy_remove_mapping(cache->policy, mg->new_oblock);
  962. free_io_migration(mg);
  963. return;
  964. }
  965. }
  966. spin_lock_irqsave(&cache->lock, flags);
  967. list_add_tail(&mg->list, &cache->need_commit_migrations);
  968. cache->commit_requested = true;
  969. spin_unlock_irqrestore(&cache->lock, flags);
  970. }
  971. static void migration_success_post_commit(struct dm_cache_migration *mg)
  972. {
  973. unsigned long flags;
  974. struct cache *cache = mg->cache;
  975. if (mg->writeback) {
  976. DMWARN_LIMIT("%s: writeback unexpectedly triggered commit",
  977. cache_device_name(cache));
  978. return;
  979. } else if (mg->demote) {
  980. cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
  981. if (mg->promote) {
  982. mg->demote = false;
  983. spin_lock_irqsave(&cache->lock, flags);
  984. list_add_tail(&mg->list, &cache->quiesced_migrations);
  985. spin_unlock_irqrestore(&cache->lock, flags);
  986. } else {
  987. if (mg->invalidate)
  988. policy_remove_mapping(cache->policy, mg->old_oblock);
  989. free_io_migration(mg);
  990. }
  991. } else {
  992. if (mg->requeue_holder) {
  993. clear_dirty(cache, mg->new_oblock, mg->cblock);
  994. cell_defer(cache, mg->new_ocell, true);
  995. } else {
  996. /*
  997. * The block was promoted via an overwrite, so it's dirty.
  998. */
  999. set_dirty(cache, mg->new_oblock, mg->cblock);
  1000. bio_endio(mg->new_ocell->holder);
  1001. cell_defer(cache, mg->new_ocell, false);
  1002. }
  1003. free_io_migration(mg);
  1004. }
  1005. }
  1006. static void copy_complete(int read_err, unsigned long write_err, void *context)
  1007. {
  1008. unsigned long flags;
  1009. struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
  1010. struct cache *cache = mg->cache;
  1011. if (read_err || write_err)
  1012. mg->err = true;
  1013. spin_lock_irqsave(&cache->lock, flags);
  1014. list_add_tail(&mg->list, &cache->completed_migrations);
  1015. spin_unlock_irqrestore(&cache->lock, flags);
  1016. wake_worker(cache);
  1017. }
  1018. static void issue_copy(struct dm_cache_migration *mg)
  1019. {
  1020. int r;
  1021. struct dm_io_region o_region, c_region;
  1022. struct cache *cache = mg->cache;
  1023. sector_t cblock = from_cblock(mg->cblock);
  1024. o_region.bdev = cache->origin_dev->bdev;
  1025. o_region.count = cache->sectors_per_block;
  1026. c_region.bdev = cache->cache_dev->bdev;
  1027. c_region.sector = cblock * cache->sectors_per_block;
  1028. c_region.count = cache->sectors_per_block;
  1029. if (mg->writeback || mg->demote) {
  1030. /* demote */
  1031. o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
  1032. r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
  1033. } else {
  1034. /* promote */
  1035. o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
  1036. r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
  1037. }
  1038. if (r < 0) {
  1039. DMERR_LIMIT("%s: issuing migration failed", cache_device_name(cache));
  1040. migration_failure(mg);
  1041. }
  1042. }
  1043. static void overwrite_endio(struct bio *bio)
  1044. {
  1045. struct dm_cache_migration *mg = bio->bi_private;
  1046. struct cache *cache = mg->cache;
  1047. size_t pb_data_size = get_per_bio_data_size(cache);
  1048. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  1049. unsigned long flags;
  1050. dm_unhook_bio(&pb->hook_info, bio);
  1051. if (bio->bi_error)
  1052. mg->err = true;
  1053. mg->requeue_holder = false;
  1054. spin_lock_irqsave(&cache->lock, flags);
  1055. list_add_tail(&mg->list, &cache->completed_migrations);
  1056. spin_unlock_irqrestore(&cache->lock, flags);
  1057. wake_worker(cache);
  1058. }
  1059. static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio)
  1060. {
  1061. size_t pb_data_size = get_per_bio_data_size(mg->cache);
  1062. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  1063. dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
  1064. remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
  1065. /*
  1066. * No need to inc_ds() here, since the cell will be held for the
  1067. * duration of the io.
  1068. */
  1069. accounted_request(mg->cache, bio);
  1070. }
  1071. static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
  1072. {
  1073. return (bio_data_dir(bio) == WRITE) &&
  1074. (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
  1075. }
  1076. static void avoid_copy(struct dm_cache_migration *mg)
  1077. {
  1078. atomic_inc(&mg->cache->stats.copies_avoided);
  1079. migration_success_pre_commit(mg);
  1080. }
  1081. static void calc_discard_block_range(struct cache *cache, struct bio *bio,
  1082. dm_dblock_t *b, dm_dblock_t *e)
  1083. {
  1084. sector_t sb = bio->bi_iter.bi_sector;
  1085. sector_t se = bio_end_sector(bio);
  1086. *b = to_dblock(dm_sector_div_up(sb, cache->discard_block_size));
  1087. if (se - sb < cache->discard_block_size)
  1088. *e = *b;
  1089. else
  1090. *e = to_dblock(block_div(se, cache->discard_block_size));
  1091. }
  1092. static void issue_discard(struct dm_cache_migration *mg)
  1093. {
  1094. dm_dblock_t b, e;
  1095. struct bio *bio = mg->new_ocell->holder;
  1096. struct cache *cache = mg->cache;
  1097. calc_discard_block_range(cache, bio, &b, &e);
  1098. while (b != e) {
  1099. set_discard(cache, b);
  1100. b = to_dblock(from_dblock(b) + 1);
  1101. }
  1102. bio_endio(bio);
  1103. cell_defer(cache, mg->new_ocell, false);
  1104. free_migration(mg);
  1105. wake_worker(cache);
  1106. }
  1107. static void issue_copy_or_discard(struct dm_cache_migration *mg)
  1108. {
  1109. bool avoid;
  1110. struct cache *cache = mg->cache;
  1111. if (mg->discard) {
  1112. issue_discard(mg);
  1113. return;
  1114. }
  1115. if (mg->writeback || mg->demote)
  1116. avoid = !is_dirty(cache, mg->cblock) ||
  1117. is_discarded_oblock(cache, mg->old_oblock);
  1118. else {
  1119. struct bio *bio = mg->new_ocell->holder;
  1120. avoid = is_discarded_oblock(cache, mg->new_oblock);
  1121. if (writeback_mode(&cache->features) &&
  1122. !avoid && bio_writes_complete_block(cache, bio)) {
  1123. issue_overwrite(mg, bio);
  1124. return;
  1125. }
  1126. }
  1127. avoid ? avoid_copy(mg) : issue_copy(mg);
  1128. }
  1129. static void complete_migration(struct dm_cache_migration *mg)
  1130. {
  1131. if (mg->err)
  1132. migration_failure(mg);
  1133. else
  1134. migration_success_pre_commit(mg);
  1135. }
  1136. static void process_migrations(struct cache *cache, struct list_head *head,
  1137. void (*fn)(struct dm_cache_migration *))
  1138. {
  1139. unsigned long flags;
  1140. struct list_head list;
  1141. struct dm_cache_migration *mg, *tmp;
  1142. INIT_LIST_HEAD(&list);
  1143. spin_lock_irqsave(&cache->lock, flags);
  1144. list_splice_init(head, &list);
  1145. spin_unlock_irqrestore(&cache->lock, flags);
  1146. list_for_each_entry_safe(mg, tmp, &list, list)
  1147. fn(mg);
  1148. }
  1149. static void __queue_quiesced_migration(struct dm_cache_migration *mg)
  1150. {
  1151. list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
  1152. }
  1153. static void queue_quiesced_migration(struct dm_cache_migration *mg)
  1154. {
  1155. unsigned long flags;
  1156. struct cache *cache = mg->cache;
  1157. spin_lock_irqsave(&cache->lock, flags);
  1158. __queue_quiesced_migration(mg);
  1159. spin_unlock_irqrestore(&cache->lock, flags);
  1160. wake_worker(cache);
  1161. }
  1162. static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
  1163. {
  1164. unsigned long flags;
  1165. struct dm_cache_migration *mg, *tmp;
  1166. spin_lock_irqsave(&cache->lock, flags);
  1167. list_for_each_entry_safe(mg, tmp, work, list)
  1168. __queue_quiesced_migration(mg);
  1169. spin_unlock_irqrestore(&cache->lock, flags);
  1170. wake_worker(cache);
  1171. }
  1172. static void check_for_quiesced_migrations(struct cache *cache,
  1173. struct per_bio_data *pb)
  1174. {
  1175. struct list_head work;
  1176. if (!pb->all_io_entry)
  1177. return;
  1178. INIT_LIST_HEAD(&work);
  1179. dm_deferred_entry_dec(pb->all_io_entry, &work);
  1180. if (!list_empty(&work))
  1181. queue_quiesced_migrations(cache, &work);
  1182. }
  1183. static void quiesce_migration(struct dm_cache_migration *mg)
  1184. {
  1185. if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
  1186. queue_quiesced_migration(mg);
  1187. }
  1188. static void promote(struct cache *cache, struct prealloc *structs,
  1189. dm_oblock_t oblock, dm_cblock_t cblock,
  1190. struct dm_bio_prison_cell *cell)
  1191. {
  1192. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1193. mg->err = false;
  1194. mg->discard = false;
  1195. mg->writeback = false;
  1196. mg->demote = false;
  1197. mg->promote = true;
  1198. mg->requeue_holder = true;
  1199. mg->invalidate = false;
  1200. mg->cache = cache;
  1201. mg->new_oblock = oblock;
  1202. mg->cblock = cblock;
  1203. mg->old_ocell = NULL;
  1204. mg->new_ocell = cell;
  1205. mg->start_jiffies = jiffies;
  1206. inc_io_migrations(cache);
  1207. quiesce_migration(mg);
  1208. }
  1209. static void writeback(struct cache *cache, struct prealloc *structs,
  1210. dm_oblock_t oblock, dm_cblock_t cblock,
  1211. struct dm_bio_prison_cell *cell)
  1212. {
  1213. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1214. mg->err = false;
  1215. mg->discard = false;
  1216. mg->writeback = true;
  1217. mg->demote = false;
  1218. mg->promote = false;
  1219. mg->requeue_holder = true;
  1220. mg->invalidate = false;
  1221. mg->cache = cache;
  1222. mg->old_oblock = oblock;
  1223. mg->cblock = cblock;
  1224. mg->old_ocell = cell;
  1225. mg->new_ocell = NULL;
  1226. mg->start_jiffies = jiffies;
  1227. inc_io_migrations(cache);
  1228. quiesce_migration(mg);
  1229. }
  1230. static void demote_then_promote(struct cache *cache, struct prealloc *structs,
  1231. dm_oblock_t old_oblock, dm_oblock_t new_oblock,
  1232. dm_cblock_t cblock,
  1233. struct dm_bio_prison_cell *old_ocell,
  1234. struct dm_bio_prison_cell *new_ocell)
  1235. {
  1236. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1237. mg->err = false;
  1238. mg->discard = false;
  1239. mg->writeback = false;
  1240. mg->demote = true;
  1241. mg->promote = true;
  1242. mg->requeue_holder = true;
  1243. mg->invalidate = false;
  1244. mg->cache = cache;
  1245. mg->old_oblock = old_oblock;
  1246. mg->new_oblock = new_oblock;
  1247. mg->cblock = cblock;
  1248. mg->old_ocell = old_ocell;
  1249. mg->new_ocell = new_ocell;
  1250. mg->start_jiffies = jiffies;
  1251. inc_io_migrations(cache);
  1252. quiesce_migration(mg);
  1253. }
  1254. /*
  1255. * Invalidate a cache entry. No writeback occurs; any changes in the cache
  1256. * block are thrown away.
  1257. */
  1258. static void invalidate(struct cache *cache, struct prealloc *structs,
  1259. dm_oblock_t oblock, dm_cblock_t cblock,
  1260. struct dm_bio_prison_cell *cell)
  1261. {
  1262. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1263. mg->err = false;
  1264. mg->discard = false;
  1265. mg->writeback = false;
  1266. mg->demote = true;
  1267. mg->promote = false;
  1268. mg->requeue_holder = true;
  1269. mg->invalidate = true;
  1270. mg->cache = cache;
  1271. mg->old_oblock = oblock;
  1272. mg->cblock = cblock;
  1273. mg->old_ocell = cell;
  1274. mg->new_ocell = NULL;
  1275. mg->start_jiffies = jiffies;
  1276. inc_io_migrations(cache);
  1277. quiesce_migration(mg);
  1278. }
  1279. static void discard(struct cache *cache, struct prealloc *structs,
  1280. struct dm_bio_prison_cell *cell)
  1281. {
  1282. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1283. mg->err = false;
  1284. mg->discard = true;
  1285. mg->writeback = false;
  1286. mg->demote = false;
  1287. mg->promote = false;
  1288. mg->requeue_holder = false;
  1289. mg->invalidate = false;
  1290. mg->cache = cache;
  1291. mg->old_ocell = NULL;
  1292. mg->new_ocell = cell;
  1293. mg->start_jiffies = jiffies;
  1294. quiesce_migration(mg);
  1295. }
  1296. /*----------------------------------------------------------------
  1297. * bio processing
  1298. *--------------------------------------------------------------*/
  1299. static void defer_bio(struct cache *cache, struct bio *bio)
  1300. {
  1301. unsigned long flags;
  1302. spin_lock_irqsave(&cache->lock, flags);
  1303. bio_list_add(&cache->deferred_bios, bio);
  1304. spin_unlock_irqrestore(&cache->lock, flags);
  1305. wake_worker(cache);
  1306. }
  1307. static void process_flush_bio(struct cache *cache, struct bio *bio)
  1308. {
  1309. size_t pb_data_size = get_per_bio_data_size(cache);
  1310. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  1311. BUG_ON(bio->bi_iter.bi_size);
  1312. if (!pb->req_nr)
  1313. remap_to_origin(cache, bio);
  1314. else
  1315. remap_to_cache(cache, bio, 0);
  1316. /*
  1317. * REQ_PREFLUSH is not directed at any particular block so we don't
  1318. * need to inc_ds(). REQ_FUA's are split into a write + REQ_PREFLUSH
  1319. * by dm-core.
  1320. */
  1321. issue(cache, bio);
  1322. }
  1323. static void process_discard_bio(struct cache *cache, struct prealloc *structs,
  1324. struct bio *bio)
  1325. {
  1326. int r;
  1327. dm_dblock_t b, e;
  1328. struct dm_bio_prison_cell *cell_prealloc, *new_ocell;
  1329. calc_discard_block_range(cache, bio, &b, &e);
  1330. if (b == e) {
  1331. bio_endio(bio);
  1332. return;
  1333. }
  1334. cell_prealloc = prealloc_get_cell(structs);
  1335. r = bio_detain_range(cache, dblock_to_oblock(cache, b), dblock_to_oblock(cache, e), bio, cell_prealloc,
  1336. (cell_free_fn) prealloc_put_cell,
  1337. structs, &new_ocell);
  1338. if (r > 0)
  1339. return;
  1340. discard(cache, structs, new_ocell);
  1341. }
  1342. static bool spare_migration_bandwidth(struct cache *cache)
  1343. {
  1344. sector_t current_volume = (atomic_read(&cache->nr_io_migrations) + 1) *
  1345. cache->sectors_per_block;
  1346. return current_volume < cache->migration_threshold;
  1347. }
  1348. static void inc_hit_counter(struct cache *cache, struct bio *bio)
  1349. {
  1350. atomic_inc(bio_data_dir(bio) == READ ?
  1351. &cache->stats.read_hit : &cache->stats.write_hit);
  1352. }
  1353. static void inc_miss_counter(struct cache *cache, struct bio *bio)
  1354. {
  1355. atomic_inc(bio_data_dir(bio) == READ ?
  1356. &cache->stats.read_miss : &cache->stats.write_miss);
  1357. }
  1358. /*----------------------------------------------------------------*/
  1359. struct inc_detail {
  1360. struct cache *cache;
  1361. struct bio_list bios_for_issue;
  1362. struct bio_list unhandled_bios;
  1363. bool any_writes;
  1364. };
  1365. static void inc_fn(void *context, struct dm_bio_prison_cell *cell)
  1366. {
  1367. struct bio *bio;
  1368. struct inc_detail *detail = context;
  1369. struct cache *cache = detail->cache;
  1370. inc_ds(cache, cell->holder, cell);
  1371. if (bio_data_dir(cell->holder) == WRITE)
  1372. detail->any_writes = true;
  1373. while ((bio = bio_list_pop(&cell->bios))) {
  1374. if (discard_or_flush(bio)) {
  1375. bio_list_add(&detail->unhandled_bios, bio);
  1376. continue;
  1377. }
  1378. if (bio_data_dir(bio) == WRITE)
  1379. detail->any_writes = true;
  1380. bio_list_add(&detail->bios_for_issue, bio);
  1381. inc_ds(cache, bio, cell);
  1382. }
  1383. }
  1384. // FIXME: refactor these two
  1385. static void remap_cell_to_origin_clear_discard(struct cache *cache,
  1386. struct dm_bio_prison_cell *cell,
  1387. dm_oblock_t oblock, bool issue_holder)
  1388. {
  1389. struct bio *bio;
  1390. unsigned long flags;
  1391. struct inc_detail detail;
  1392. detail.cache = cache;
  1393. bio_list_init(&detail.bios_for_issue);
  1394. bio_list_init(&detail.unhandled_bios);
  1395. detail.any_writes = false;
  1396. spin_lock_irqsave(&cache->lock, flags);
  1397. dm_cell_visit_release(cache->prison, inc_fn, &detail, cell);
  1398. bio_list_merge(&cache->deferred_bios, &detail.unhandled_bios);
  1399. spin_unlock_irqrestore(&cache->lock, flags);
  1400. remap_to_origin(cache, cell->holder);
  1401. if (issue_holder)
  1402. issue(cache, cell->holder);
  1403. else
  1404. accounted_begin(cache, cell->holder);
  1405. if (detail.any_writes)
  1406. clear_discard(cache, oblock_to_dblock(cache, oblock));
  1407. while ((bio = bio_list_pop(&detail.bios_for_issue))) {
  1408. remap_to_origin(cache, bio);
  1409. issue(cache, bio);
  1410. }
  1411. free_prison_cell(cache, cell);
  1412. }
  1413. static void remap_cell_to_cache_dirty(struct cache *cache, struct dm_bio_prison_cell *cell,
  1414. dm_oblock_t oblock, dm_cblock_t cblock, bool issue_holder)
  1415. {
  1416. struct bio *bio;
  1417. unsigned long flags;
  1418. struct inc_detail detail;
  1419. detail.cache = cache;
  1420. bio_list_init(&detail.bios_for_issue);
  1421. bio_list_init(&detail.unhandled_bios);
  1422. detail.any_writes = false;
  1423. spin_lock_irqsave(&cache->lock, flags);
  1424. dm_cell_visit_release(cache->prison, inc_fn, &detail, cell);
  1425. bio_list_merge(&cache->deferred_bios, &detail.unhandled_bios);
  1426. spin_unlock_irqrestore(&cache->lock, flags);
  1427. remap_to_cache(cache, cell->holder, cblock);
  1428. if (issue_holder)
  1429. issue(cache, cell->holder);
  1430. else
  1431. accounted_begin(cache, cell->holder);
  1432. if (detail.any_writes) {
  1433. set_dirty(cache, oblock, cblock);
  1434. clear_discard(cache, oblock_to_dblock(cache, oblock));
  1435. }
  1436. while ((bio = bio_list_pop(&detail.bios_for_issue))) {
  1437. remap_to_cache(cache, bio, cblock);
  1438. issue(cache, bio);
  1439. }
  1440. free_prison_cell(cache, cell);
  1441. }
  1442. /*----------------------------------------------------------------*/
  1443. struct old_oblock_lock {
  1444. struct policy_locker locker;
  1445. struct cache *cache;
  1446. struct prealloc *structs;
  1447. struct dm_bio_prison_cell *cell;
  1448. };
  1449. static int null_locker(struct policy_locker *locker, dm_oblock_t b)
  1450. {
  1451. /* This should never be called */
  1452. BUG();
  1453. return 0;
  1454. }
  1455. static int cell_locker(struct policy_locker *locker, dm_oblock_t b)
  1456. {
  1457. struct old_oblock_lock *l = container_of(locker, struct old_oblock_lock, locker);
  1458. struct dm_bio_prison_cell *cell_prealloc = prealloc_get_cell(l->structs);
  1459. return bio_detain(l->cache, b, NULL, cell_prealloc,
  1460. (cell_free_fn) prealloc_put_cell,
  1461. l->structs, &l->cell);
  1462. }
  1463. static void process_cell(struct cache *cache, struct prealloc *structs,
  1464. struct dm_bio_prison_cell *new_ocell)
  1465. {
  1466. int r;
  1467. bool release_cell = true;
  1468. struct bio *bio = new_ocell->holder;
  1469. dm_oblock_t block = get_bio_block(cache, bio);
  1470. struct policy_result lookup_result;
  1471. bool passthrough = passthrough_mode(&cache->features);
  1472. bool fast_promotion, can_migrate;
  1473. struct old_oblock_lock ool;
  1474. fast_promotion = is_discarded_oblock(cache, block) || bio_writes_complete_block(cache, bio);
  1475. can_migrate = !passthrough && (fast_promotion || spare_migration_bandwidth(cache));
  1476. ool.locker.fn = cell_locker;
  1477. ool.cache = cache;
  1478. ool.structs = structs;
  1479. ool.cell = NULL;
  1480. r = policy_map(cache->policy, block, true, can_migrate, fast_promotion,
  1481. bio, &ool.locker, &lookup_result);
  1482. if (r == -EWOULDBLOCK)
  1483. /* migration has been denied */
  1484. lookup_result.op = POLICY_MISS;
  1485. switch (lookup_result.op) {
  1486. case POLICY_HIT:
  1487. if (passthrough) {
  1488. inc_miss_counter(cache, bio);
  1489. /*
  1490. * Passthrough always maps to the origin,
  1491. * invalidating any cache blocks that are written
  1492. * to.
  1493. */
  1494. if (bio_data_dir(bio) == WRITE) {
  1495. atomic_inc(&cache->stats.demotion);
  1496. invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
  1497. release_cell = false;
  1498. } else {
  1499. /* FIXME: factor out issue_origin() */
  1500. remap_to_origin_clear_discard(cache, bio, block);
  1501. inc_and_issue(cache, bio, new_ocell);
  1502. }
  1503. } else {
  1504. inc_hit_counter(cache, bio);
  1505. if (bio_data_dir(bio) == WRITE &&
  1506. writethrough_mode(&cache->features) &&
  1507. !is_dirty(cache, lookup_result.cblock)) {
  1508. remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
  1509. inc_and_issue(cache, bio, new_ocell);
  1510. } else {
  1511. remap_cell_to_cache_dirty(cache, new_ocell, block, lookup_result.cblock, true);
  1512. release_cell = false;
  1513. }
  1514. }
  1515. break;
  1516. case POLICY_MISS:
  1517. inc_miss_counter(cache, bio);
  1518. remap_cell_to_origin_clear_discard(cache, new_ocell, block, true);
  1519. release_cell = false;
  1520. break;
  1521. case POLICY_NEW:
  1522. atomic_inc(&cache->stats.promotion);
  1523. promote(cache, structs, block, lookup_result.cblock, new_ocell);
  1524. release_cell = false;
  1525. break;
  1526. case POLICY_REPLACE:
  1527. atomic_inc(&cache->stats.demotion);
  1528. atomic_inc(&cache->stats.promotion);
  1529. demote_then_promote(cache, structs, lookup_result.old_oblock,
  1530. block, lookup_result.cblock,
  1531. ool.cell, new_ocell);
  1532. release_cell = false;
  1533. break;
  1534. default:
  1535. DMERR_LIMIT("%s: %s: erroring bio, unknown policy op: %u",
  1536. cache_device_name(cache), __func__,
  1537. (unsigned) lookup_result.op);
  1538. bio_io_error(bio);
  1539. }
  1540. if (release_cell)
  1541. cell_defer(cache, new_ocell, false);
  1542. }
  1543. static void process_bio(struct cache *cache, struct prealloc *structs,
  1544. struct bio *bio)
  1545. {
  1546. int r;
  1547. dm_oblock_t block = get_bio_block(cache, bio);
  1548. struct dm_bio_prison_cell *cell_prealloc, *new_ocell;
  1549. /*
  1550. * Check to see if that block is currently migrating.
  1551. */
  1552. cell_prealloc = prealloc_get_cell(structs);
  1553. r = bio_detain(cache, block, bio, cell_prealloc,
  1554. (cell_free_fn) prealloc_put_cell,
  1555. structs, &new_ocell);
  1556. if (r > 0)
  1557. return;
  1558. process_cell(cache, structs, new_ocell);
  1559. }
  1560. static int need_commit_due_to_time(struct cache *cache)
  1561. {
  1562. return jiffies < cache->last_commit_jiffies ||
  1563. jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
  1564. }
  1565. /*
  1566. * A non-zero return indicates read_only or fail_io mode.
  1567. */
  1568. static int commit(struct cache *cache, bool clean_shutdown)
  1569. {
  1570. int r;
  1571. if (get_cache_mode(cache) >= CM_READ_ONLY)
  1572. return -EINVAL;
  1573. atomic_inc(&cache->stats.commit_count);
  1574. r = dm_cache_commit(cache->cmd, clean_shutdown);
  1575. if (r)
  1576. metadata_operation_failed(cache, "dm_cache_commit", r);
  1577. return r;
  1578. }
  1579. static int commit_if_needed(struct cache *cache)
  1580. {
  1581. int r = 0;
  1582. if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
  1583. dm_cache_changed_this_transaction(cache->cmd)) {
  1584. r = commit(cache, false);
  1585. cache->commit_requested = false;
  1586. cache->last_commit_jiffies = jiffies;
  1587. }
  1588. return r;
  1589. }
  1590. static void process_deferred_bios(struct cache *cache)
  1591. {
  1592. bool prealloc_used = false;
  1593. unsigned long flags;
  1594. struct bio_list bios;
  1595. struct bio *bio;
  1596. struct prealloc structs;
  1597. memset(&structs, 0, sizeof(structs));
  1598. bio_list_init(&bios);
  1599. spin_lock_irqsave(&cache->lock, flags);
  1600. bio_list_merge(&bios, &cache->deferred_bios);
  1601. bio_list_init(&cache->deferred_bios);
  1602. spin_unlock_irqrestore(&cache->lock, flags);
  1603. while (!bio_list_empty(&bios)) {
  1604. /*
  1605. * If we've got no free migration structs, and processing
  1606. * this bio might require one, we pause until there are some
  1607. * prepared mappings to process.
  1608. */
  1609. prealloc_used = true;
  1610. if (prealloc_data_structs(cache, &structs)) {
  1611. spin_lock_irqsave(&cache->lock, flags);
  1612. bio_list_merge(&cache->deferred_bios, &bios);
  1613. spin_unlock_irqrestore(&cache->lock, flags);
  1614. break;
  1615. }
  1616. bio = bio_list_pop(&bios);
  1617. if (bio->bi_opf & REQ_PREFLUSH)
  1618. process_flush_bio(cache, bio);
  1619. else if (bio_op(bio) == REQ_OP_DISCARD)
  1620. process_discard_bio(cache, &structs, bio);
  1621. else
  1622. process_bio(cache, &structs, bio);
  1623. }
  1624. if (prealloc_used)
  1625. prealloc_free_structs(cache, &structs);
  1626. }
  1627. static void process_deferred_cells(struct cache *cache)
  1628. {
  1629. bool prealloc_used = false;
  1630. unsigned long flags;
  1631. struct dm_bio_prison_cell *cell, *tmp;
  1632. struct list_head cells;
  1633. struct prealloc structs;
  1634. memset(&structs, 0, sizeof(structs));
  1635. INIT_LIST_HEAD(&cells);
  1636. spin_lock_irqsave(&cache->lock, flags);
  1637. list_splice_init(&cache->deferred_cells, &cells);
  1638. spin_unlock_irqrestore(&cache->lock, flags);
  1639. list_for_each_entry_safe(cell, tmp, &cells, user_list) {
  1640. /*
  1641. * If we've got no free migration structs, and processing
  1642. * this bio might require one, we pause until there are some
  1643. * prepared mappings to process.
  1644. */
  1645. prealloc_used = true;
  1646. if (prealloc_data_structs(cache, &structs)) {
  1647. spin_lock_irqsave(&cache->lock, flags);
  1648. list_splice(&cells, &cache->deferred_cells);
  1649. spin_unlock_irqrestore(&cache->lock, flags);
  1650. break;
  1651. }
  1652. process_cell(cache, &structs, cell);
  1653. }
  1654. if (prealloc_used)
  1655. prealloc_free_structs(cache, &structs);
  1656. }
  1657. static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
  1658. {
  1659. unsigned long flags;
  1660. struct bio_list bios;
  1661. struct bio *bio;
  1662. bio_list_init(&bios);
  1663. spin_lock_irqsave(&cache->lock, flags);
  1664. bio_list_merge(&bios, &cache->deferred_flush_bios);
  1665. bio_list_init(&cache->deferred_flush_bios);
  1666. spin_unlock_irqrestore(&cache->lock, flags);
  1667. /*
  1668. * These bios have already been through inc_ds()
  1669. */
  1670. while ((bio = bio_list_pop(&bios)))
  1671. submit_bios ? accounted_request(cache, bio) : bio_io_error(bio);
  1672. }
  1673. static void process_deferred_writethrough_bios(struct cache *cache)
  1674. {
  1675. unsigned long flags;
  1676. struct bio_list bios;
  1677. struct bio *bio;
  1678. bio_list_init(&bios);
  1679. spin_lock_irqsave(&cache->lock, flags);
  1680. bio_list_merge(&bios, &cache->deferred_writethrough_bios);
  1681. bio_list_init(&cache->deferred_writethrough_bios);
  1682. spin_unlock_irqrestore(&cache->lock, flags);
  1683. /*
  1684. * These bios have already been through inc_ds()
  1685. */
  1686. while ((bio = bio_list_pop(&bios)))
  1687. accounted_request(cache, bio);
  1688. }
  1689. static void writeback_some_dirty_blocks(struct cache *cache)
  1690. {
  1691. bool prealloc_used = false;
  1692. dm_oblock_t oblock;
  1693. dm_cblock_t cblock;
  1694. struct prealloc structs;
  1695. struct dm_bio_prison_cell *old_ocell;
  1696. bool busy = !iot_idle_for(&cache->origin_tracker, HZ);
  1697. memset(&structs, 0, sizeof(structs));
  1698. while (spare_migration_bandwidth(cache)) {
  1699. if (policy_writeback_work(cache->policy, &oblock, &cblock, busy))
  1700. break; /* no work to do */
  1701. prealloc_used = true;
  1702. if (prealloc_data_structs(cache, &structs) ||
  1703. get_cell(cache, oblock, &structs, &old_ocell)) {
  1704. policy_set_dirty(cache->policy, oblock);
  1705. break;
  1706. }
  1707. writeback(cache, &structs, oblock, cblock, old_ocell);
  1708. }
  1709. if (prealloc_used)
  1710. prealloc_free_structs(cache, &structs);
  1711. }
  1712. /*----------------------------------------------------------------
  1713. * Invalidations.
  1714. * Dropping something from the cache *without* writing back.
  1715. *--------------------------------------------------------------*/
  1716. static void process_invalidation_request(struct cache *cache, struct invalidation_request *req)
  1717. {
  1718. int r = 0;
  1719. uint64_t begin = from_cblock(req->cblocks->begin);
  1720. uint64_t end = from_cblock(req->cblocks->end);
  1721. while (begin != end) {
  1722. r = policy_remove_cblock(cache->policy, to_cblock(begin));
  1723. if (!r) {
  1724. r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin));
  1725. if (r) {
  1726. metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
  1727. break;
  1728. }
  1729. } else if (r == -ENODATA) {
  1730. /* harmless, already unmapped */
  1731. r = 0;
  1732. } else {
  1733. DMERR("%s: policy_remove_cblock failed", cache_device_name(cache));
  1734. break;
  1735. }
  1736. begin++;
  1737. }
  1738. cache->commit_requested = true;
  1739. req->err = r;
  1740. atomic_set(&req->complete, 1);
  1741. wake_up(&req->result_wait);
  1742. }
  1743. static void process_invalidation_requests(struct cache *cache)
  1744. {
  1745. struct list_head list;
  1746. struct invalidation_request *req, *tmp;
  1747. INIT_LIST_HEAD(&list);
  1748. spin_lock(&cache->invalidation_lock);
  1749. list_splice_init(&cache->invalidation_requests, &list);
  1750. spin_unlock(&cache->invalidation_lock);
  1751. list_for_each_entry_safe (req, tmp, &list, list)
  1752. process_invalidation_request(cache, req);
  1753. }
  1754. /*----------------------------------------------------------------
  1755. * Main worker loop
  1756. *--------------------------------------------------------------*/
  1757. static bool is_quiescing(struct cache *cache)
  1758. {
  1759. return atomic_read(&cache->quiescing);
  1760. }
  1761. static void ack_quiescing(struct cache *cache)
  1762. {
  1763. if (is_quiescing(cache)) {
  1764. atomic_inc(&cache->quiescing_ack);
  1765. wake_up(&cache->quiescing_wait);
  1766. }
  1767. }
  1768. static void wait_for_quiescing_ack(struct cache *cache)
  1769. {
  1770. wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
  1771. }
  1772. static void start_quiescing(struct cache *cache)
  1773. {
  1774. atomic_inc(&cache->quiescing);
  1775. wait_for_quiescing_ack(cache);
  1776. }
  1777. static void stop_quiescing(struct cache *cache)
  1778. {
  1779. atomic_set(&cache->quiescing, 0);
  1780. atomic_set(&cache->quiescing_ack, 0);
  1781. }
  1782. static void wait_for_migrations(struct cache *cache)
  1783. {
  1784. wait_event(cache->migration_wait, !atomic_read(&cache->nr_allocated_migrations));
  1785. }
  1786. static void stop_worker(struct cache *cache)
  1787. {
  1788. cancel_delayed_work(&cache->waker);
  1789. flush_workqueue(cache->wq);
  1790. }
  1791. static void requeue_deferred_cells(struct cache *cache)
  1792. {
  1793. unsigned long flags;
  1794. struct list_head cells;
  1795. struct dm_bio_prison_cell *cell, *tmp;
  1796. INIT_LIST_HEAD(&cells);
  1797. spin_lock_irqsave(&cache->lock, flags);
  1798. list_splice_init(&cache->deferred_cells, &cells);
  1799. spin_unlock_irqrestore(&cache->lock, flags);
  1800. list_for_each_entry_safe(cell, tmp, &cells, user_list)
  1801. cell_requeue(cache, cell);
  1802. }
  1803. static void requeue_deferred_bios(struct cache *cache)
  1804. {
  1805. struct bio *bio;
  1806. struct bio_list bios;
  1807. bio_list_init(&bios);
  1808. bio_list_merge(&bios, &cache->deferred_bios);
  1809. bio_list_init(&cache->deferred_bios);
  1810. while ((bio = bio_list_pop(&bios))) {
  1811. bio->bi_error = DM_ENDIO_REQUEUE;
  1812. bio_endio(bio);
  1813. }
  1814. }
  1815. static int more_work(struct cache *cache)
  1816. {
  1817. if (is_quiescing(cache))
  1818. return !list_empty(&cache->quiesced_migrations) ||
  1819. !list_empty(&cache->completed_migrations) ||
  1820. !list_empty(&cache->need_commit_migrations);
  1821. else
  1822. return !bio_list_empty(&cache->deferred_bios) ||
  1823. !list_empty(&cache->deferred_cells) ||
  1824. !bio_list_empty(&cache->deferred_flush_bios) ||
  1825. !bio_list_empty(&cache->deferred_writethrough_bios) ||
  1826. !list_empty(&cache->quiesced_migrations) ||
  1827. !list_empty(&cache->completed_migrations) ||
  1828. !list_empty(&cache->need_commit_migrations) ||
  1829. cache->invalidate;
  1830. }
  1831. static void do_worker(struct work_struct *ws)
  1832. {
  1833. struct cache *cache = container_of(ws, struct cache, worker);
  1834. do {
  1835. if (!is_quiescing(cache)) {
  1836. writeback_some_dirty_blocks(cache);
  1837. process_deferred_writethrough_bios(cache);
  1838. process_deferred_bios(cache);
  1839. process_deferred_cells(cache);
  1840. process_invalidation_requests(cache);
  1841. }
  1842. process_migrations(cache, &cache->quiesced_migrations, issue_copy_or_discard);
  1843. process_migrations(cache, &cache->completed_migrations, complete_migration);
  1844. if (commit_if_needed(cache)) {
  1845. process_deferred_flush_bios(cache, false);
  1846. process_migrations(cache, &cache->need_commit_migrations, migration_failure);
  1847. } else {
  1848. process_deferred_flush_bios(cache, true);
  1849. process_migrations(cache, &cache->need_commit_migrations,
  1850. migration_success_post_commit);
  1851. }
  1852. ack_quiescing(cache);
  1853. } while (more_work(cache));
  1854. }
  1855. /*
  1856. * We want to commit periodically so that not too much
  1857. * unwritten metadata builds up.
  1858. */
  1859. static void do_waker(struct work_struct *ws)
  1860. {
  1861. struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
  1862. policy_tick(cache->policy, true);
  1863. wake_worker(cache);
  1864. queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
  1865. }
  1866. /*----------------------------------------------------------------*/
  1867. static int is_congested(struct dm_dev *dev, int bdi_bits)
  1868. {
  1869. struct request_queue *q = bdev_get_queue(dev->bdev);
  1870. return bdi_congested(&q->backing_dev_info, bdi_bits);
  1871. }
  1872. static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
  1873. {
  1874. struct cache *cache = container_of(cb, struct cache, callbacks);
  1875. return is_congested(cache->origin_dev, bdi_bits) ||
  1876. is_congested(cache->cache_dev, bdi_bits);
  1877. }
  1878. /*----------------------------------------------------------------
  1879. * Target methods
  1880. *--------------------------------------------------------------*/
  1881. /*
  1882. * This function gets called on the error paths of the constructor, so we
  1883. * have to cope with a partially initialised struct.
  1884. */
  1885. static void destroy(struct cache *cache)
  1886. {
  1887. unsigned i;
  1888. mempool_destroy(cache->migration_pool);
  1889. if (cache->all_io_ds)
  1890. dm_deferred_set_destroy(cache->all_io_ds);
  1891. if (cache->prison)
  1892. dm_bio_prison_destroy(cache->prison);
  1893. if (cache->wq)
  1894. destroy_workqueue(cache->wq);
  1895. if (cache->dirty_bitset)
  1896. free_bitset(cache->dirty_bitset);
  1897. if (cache->discard_bitset)
  1898. free_bitset(cache->discard_bitset);
  1899. if (cache->copier)
  1900. dm_kcopyd_client_destroy(cache->copier);
  1901. if (cache->cmd)
  1902. dm_cache_metadata_close(cache->cmd);
  1903. if (cache->metadata_dev)
  1904. dm_put_device(cache->ti, cache->metadata_dev);
  1905. if (cache->origin_dev)
  1906. dm_put_device(cache->ti, cache->origin_dev);
  1907. if (cache->cache_dev)
  1908. dm_put_device(cache->ti, cache->cache_dev);
  1909. if (cache->policy)
  1910. dm_cache_policy_destroy(cache->policy);
  1911. for (i = 0; i < cache->nr_ctr_args ; i++)
  1912. kfree(cache->ctr_args[i]);
  1913. kfree(cache->ctr_args);
  1914. kfree(cache);
  1915. }
  1916. static void cache_dtr(struct dm_target *ti)
  1917. {
  1918. struct cache *cache = ti->private;
  1919. destroy(cache);
  1920. }
  1921. static sector_t get_dev_size(struct dm_dev *dev)
  1922. {
  1923. return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
  1924. }
  1925. /*----------------------------------------------------------------*/
  1926. /*
  1927. * Construct a cache device mapping.
  1928. *
  1929. * cache <metadata dev> <cache dev> <origin dev> <block size>
  1930. * <#feature args> [<feature arg>]*
  1931. * <policy> <#policy args> [<policy arg>]*
  1932. *
  1933. * metadata dev : fast device holding the persistent metadata
  1934. * cache dev : fast device holding cached data blocks
  1935. * origin dev : slow device holding original data blocks
  1936. * block size : cache unit size in sectors
  1937. *
  1938. * #feature args : number of feature arguments passed
  1939. * feature args : writethrough. (The default is writeback.)
  1940. *
  1941. * policy : the replacement policy to use
  1942. * #policy args : an even number of policy arguments corresponding
  1943. * to key/value pairs passed to the policy
  1944. * policy args : key/value pairs passed to the policy
  1945. * E.g. 'sequential_threshold 1024'
  1946. * See cache-policies.txt for details.
  1947. *
  1948. * Optional feature arguments are:
  1949. * writethrough : write through caching that prohibits cache block
  1950. * content from being different from origin block content.
  1951. * Without this argument, the default behaviour is to write
  1952. * back cache block contents later for performance reasons,
  1953. * so they may differ from the corresponding origin blocks.
  1954. */
  1955. struct cache_args {
  1956. struct dm_target *ti;
  1957. struct dm_dev *metadata_dev;
  1958. struct dm_dev *cache_dev;
  1959. sector_t cache_sectors;
  1960. struct dm_dev *origin_dev;
  1961. sector_t origin_sectors;
  1962. uint32_t block_size;
  1963. const char *policy_name;
  1964. int policy_argc;
  1965. const char **policy_argv;
  1966. struct cache_features features;
  1967. };
  1968. static void destroy_cache_args(struct cache_args *ca)
  1969. {
  1970. if (ca->metadata_dev)
  1971. dm_put_device(ca->ti, ca->metadata_dev);
  1972. if (ca->cache_dev)
  1973. dm_put_device(ca->ti, ca->cache_dev);
  1974. if (ca->origin_dev)
  1975. dm_put_device(ca->ti, ca->origin_dev);
  1976. kfree(ca);
  1977. }
  1978. static bool at_least_one_arg(struct dm_arg_set *as, char **error)
  1979. {
  1980. if (!as->argc) {
  1981. *error = "Insufficient args";
  1982. return false;
  1983. }
  1984. return true;
  1985. }
  1986. static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
  1987. char **error)
  1988. {
  1989. int r;
  1990. sector_t metadata_dev_size;
  1991. char b[BDEVNAME_SIZE];
  1992. if (!at_least_one_arg(as, error))
  1993. return -EINVAL;
  1994. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  1995. &ca->metadata_dev);
  1996. if (r) {
  1997. *error = "Error opening metadata device";
  1998. return r;
  1999. }
  2000. metadata_dev_size = get_dev_size(ca->metadata_dev);
  2001. if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
  2002. DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
  2003. bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
  2004. return 0;
  2005. }
  2006. static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
  2007. char **error)
  2008. {
  2009. int r;
  2010. if (!at_least_one_arg(as, error))
  2011. return -EINVAL;
  2012. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  2013. &ca->cache_dev);
  2014. if (r) {
  2015. *error = "Error opening cache device";
  2016. return r;
  2017. }
  2018. ca->cache_sectors = get_dev_size(ca->cache_dev);
  2019. return 0;
  2020. }
  2021. static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
  2022. char **error)
  2023. {
  2024. int r;
  2025. if (!at_least_one_arg(as, error))
  2026. return -EINVAL;
  2027. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  2028. &ca->origin_dev);
  2029. if (r) {
  2030. *error = "Error opening origin device";
  2031. return r;
  2032. }
  2033. ca->origin_sectors = get_dev_size(ca->origin_dev);
  2034. if (ca->ti->len > ca->origin_sectors) {
  2035. *error = "Device size larger than cached device";
  2036. return -EINVAL;
  2037. }
  2038. return 0;
  2039. }
  2040. static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
  2041. char **error)
  2042. {
  2043. unsigned long block_size;
  2044. if (!at_least_one_arg(as, error))
  2045. return -EINVAL;
  2046. if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
  2047. block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
  2048. block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
  2049. block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
  2050. *error = "Invalid data block size";
  2051. return -EINVAL;
  2052. }
  2053. if (block_size > ca->cache_sectors) {
  2054. *error = "Data block size is larger than the cache device";
  2055. return -EINVAL;
  2056. }
  2057. ca->block_size = block_size;
  2058. return 0;
  2059. }
  2060. static void init_features(struct cache_features *cf)
  2061. {
  2062. cf->mode = CM_WRITE;
  2063. cf->io_mode = CM_IO_WRITEBACK;
  2064. }
  2065. static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
  2066. char **error)
  2067. {
  2068. static struct dm_arg _args[] = {
  2069. {0, 1, "Invalid number of cache feature arguments"},
  2070. };
  2071. int r;
  2072. unsigned argc;
  2073. const char *arg;
  2074. struct cache_features *cf = &ca->features;
  2075. init_features(cf);
  2076. r = dm_read_arg_group(_args, as, &argc, error);
  2077. if (r)
  2078. return -EINVAL;
  2079. while (argc--) {
  2080. arg = dm_shift_arg(as);
  2081. if (!strcasecmp(arg, "writeback"))
  2082. cf->io_mode = CM_IO_WRITEBACK;
  2083. else if (!strcasecmp(arg, "writethrough"))
  2084. cf->io_mode = CM_IO_WRITETHROUGH;
  2085. else if (!strcasecmp(arg, "passthrough"))
  2086. cf->io_mode = CM_IO_PASSTHROUGH;
  2087. else {
  2088. *error = "Unrecognised cache feature requested";
  2089. return -EINVAL;
  2090. }
  2091. }
  2092. return 0;
  2093. }
  2094. static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
  2095. char **error)
  2096. {
  2097. static struct dm_arg _args[] = {
  2098. {0, 1024, "Invalid number of policy arguments"},
  2099. };
  2100. int r;
  2101. if (!at_least_one_arg(as, error))
  2102. return -EINVAL;
  2103. ca->policy_name = dm_shift_arg(as);
  2104. r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
  2105. if (r)
  2106. return -EINVAL;
  2107. ca->policy_argv = (const char **)as->argv;
  2108. dm_consume_args(as, ca->policy_argc);
  2109. return 0;
  2110. }
  2111. static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
  2112. char **error)
  2113. {
  2114. int r;
  2115. struct dm_arg_set as;
  2116. as.argc = argc;
  2117. as.argv = argv;
  2118. r = parse_metadata_dev(ca, &as, error);
  2119. if (r)
  2120. return r;
  2121. r = parse_cache_dev(ca, &as, error);
  2122. if (r)
  2123. return r;
  2124. r = parse_origin_dev(ca, &as, error);
  2125. if (r)
  2126. return r;
  2127. r = parse_block_size(ca, &as, error);
  2128. if (r)
  2129. return r;
  2130. r = parse_features(ca, &as, error);
  2131. if (r)
  2132. return r;
  2133. r = parse_policy(ca, &as, error);
  2134. if (r)
  2135. return r;
  2136. return 0;
  2137. }
  2138. /*----------------------------------------------------------------*/
  2139. static struct kmem_cache *migration_cache;
  2140. #define NOT_CORE_OPTION 1
  2141. static int process_config_option(struct cache *cache, const char *key, const char *value)
  2142. {
  2143. unsigned long tmp;
  2144. if (!strcasecmp(key, "migration_threshold")) {
  2145. if (kstrtoul(value, 10, &tmp))
  2146. return -EINVAL;
  2147. cache->migration_threshold = tmp;
  2148. return 0;
  2149. }
  2150. return NOT_CORE_OPTION;
  2151. }
  2152. static int set_config_value(struct cache *cache, const char *key, const char *value)
  2153. {
  2154. int r = process_config_option(cache, key, value);
  2155. if (r == NOT_CORE_OPTION)
  2156. r = policy_set_config_value(cache->policy, key, value);
  2157. if (r)
  2158. DMWARN("bad config value for %s: %s", key, value);
  2159. return r;
  2160. }
  2161. static int set_config_values(struct cache *cache, int argc, const char **argv)
  2162. {
  2163. int r = 0;
  2164. if (argc & 1) {
  2165. DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
  2166. return -EINVAL;
  2167. }
  2168. while (argc) {
  2169. r = set_config_value(cache, argv[0], argv[1]);
  2170. if (r)
  2171. break;
  2172. argc -= 2;
  2173. argv += 2;
  2174. }
  2175. return r;
  2176. }
  2177. static int create_cache_policy(struct cache *cache, struct cache_args *ca,
  2178. char **error)
  2179. {
  2180. struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
  2181. cache->cache_size,
  2182. cache->origin_sectors,
  2183. cache->sectors_per_block);
  2184. if (IS_ERR(p)) {
  2185. *error = "Error creating cache's policy";
  2186. return PTR_ERR(p);
  2187. }
  2188. cache->policy = p;
  2189. return 0;
  2190. }
  2191. /*
  2192. * We want the discard block size to be at least the size of the cache
  2193. * block size and have no more than 2^14 discard blocks across the origin.
  2194. */
  2195. #define MAX_DISCARD_BLOCKS (1 << 14)
  2196. static bool too_many_discard_blocks(sector_t discard_block_size,
  2197. sector_t origin_size)
  2198. {
  2199. (void) sector_div(origin_size, discard_block_size);
  2200. return origin_size > MAX_DISCARD_BLOCKS;
  2201. }
  2202. static sector_t calculate_discard_block_size(sector_t cache_block_size,
  2203. sector_t origin_size)
  2204. {
  2205. sector_t discard_block_size = cache_block_size;
  2206. if (origin_size)
  2207. while (too_many_discard_blocks(discard_block_size, origin_size))
  2208. discard_block_size *= 2;
  2209. return discard_block_size;
  2210. }
  2211. static void set_cache_size(struct cache *cache, dm_cblock_t size)
  2212. {
  2213. dm_block_t nr_blocks = from_cblock(size);
  2214. if (nr_blocks > (1 << 20) && cache->cache_size != size)
  2215. DMWARN_LIMIT("You have created a cache device with a lot of individual cache blocks (%llu)\n"
  2216. "All these mappings can consume a lot of kernel memory, and take some time to read/write.\n"
  2217. "Please consider increasing the cache block size to reduce the overall cache block count.",
  2218. (unsigned long long) nr_blocks);
  2219. cache->cache_size = size;
  2220. }
  2221. #define DEFAULT_MIGRATION_THRESHOLD 2048
  2222. static int cache_create(struct cache_args *ca, struct cache **result)
  2223. {
  2224. int r = 0;
  2225. char **error = &ca->ti->error;
  2226. struct cache *cache;
  2227. struct dm_target *ti = ca->ti;
  2228. dm_block_t origin_blocks;
  2229. struct dm_cache_metadata *cmd;
  2230. bool may_format = ca->features.mode == CM_WRITE;
  2231. cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  2232. if (!cache)
  2233. return -ENOMEM;
  2234. cache->ti = ca->ti;
  2235. ti->private = cache;
  2236. ti->num_flush_bios = 2;
  2237. ti->flush_supported = true;
  2238. ti->num_discard_bios = 1;
  2239. ti->discards_supported = true;
  2240. ti->discard_zeroes_data_unsupported = true;
  2241. ti->split_discard_bios = false;
  2242. cache->features = ca->features;
  2243. ti->per_io_data_size = get_per_bio_data_size(cache);
  2244. cache->callbacks.congested_fn = cache_is_congested;
  2245. dm_table_add_target_callbacks(ti->table, &cache->callbacks);
  2246. cache->metadata_dev = ca->metadata_dev;
  2247. cache->origin_dev = ca->origin_dev;
  2248. cache->cache_dev = ca->cache_dev;
  2249. ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
  2250. /* FIXME: factor out this whole section */
  2251. origin_blocks = cache->origin_sectors = ca->origin_sectors;
  2252. origin_blocks = block_div(origin_blocks, ca->block_size);
  2253. cache->origin_blocks = to_oblock(origin_blocks);
  2254. cache->sectors_per_block = ca->block_size;
  2255. if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
  2256. r = -EINVAL;
  2257. goto bad;
  2258. }
  2259. if (ca->block_size & (ca->block_size - 1)) {
  2260. dm_block_t cache_size = ca->cache_sectors;
  2261. cache->sectors_per_block_shift = -1;
  2262. cache_size = block_div(cache_size, ca->block_size);
  2263. set_cache_size(cache, to_cblock(cache_size));
  2264. } else {
  2265. cache->sectors_per_block_shift = __ffs(ca->block_size);
  2266. set_cache_size(cache, to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift));
  2267. }
  2268. r = create_cache_policy(cache, ca, error);
  2269. if (r)
  2270. goto bad;
  2271. cache->policy_nr_args = ca->policy_argc;
  2272. cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
  2273. r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
  2274. if (r) {
  2275. *error = "Error setting cache policy's config values";
  2276. goto bad;
  2277. }
  2278. cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
  2279. ca->block_size, may_format,
  2280. dm_cache_policy_get_hint_size(cache->policy));
  2281. if (IS_ERR(cmd)) {
  2282. *error = "Error creating metadata object";
  2283. r = PTR_ERR(cmd);
  2284. goto bad;
  2285. }
  2286. cache->cmd = cmd;
  2287. set_cache_mode(cache, CM_WRITE);
  2288. if (get_cache_mode(cache) != CM_WRITE) {
  2289. *error = "Unable to get write access to metadata, please check/repair metadata.";
  2290. r = -EINVAL;
  2291. goto bad;
  2292. }
  2293. if (passthrough_mode(&cache->features)) {
  2294. bool all_clean;
  2295. r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
  2296. if (r) {
  2297. *error = "dm_cache_metadata_all_clean() failed";
  2298. goto bad;
  2299. }
  2300. if (!all_clean) {
  2301. *error = "Cannot enter passthrough mode unless all blocks are clean";
  2302. r = -EINVAL;
  2303. goto bad;
  2304. }
  2305. }
  2306. spin_lock_init(&cache->lock);
  2307. INIT_LIST_HEAD(&cache->deferred_cells);
  2308. bio_list_init(&cache->deferred_bios);
  2309. bio_list_init(&cache->deferred_flush_bios);
  2310. bio_list_init(&cache->deferred_writethrough_bios);
  2311. INIT_LIST_HEAD(&cache->quiesced_migrations);
  2312. INIT_LIST_HEAD(&cache->completed_migrations);
  2313. INIT_LIST_HEAD(&cache->need_commit_migrations);
  2314. atomic_set(&cache->nr_allocated_migrations, 0);
  2315. atomic_set(&cache->nr_io_migrations, 0);
  2316. init_waitqueue_head(&cache->migration_wait);
  2317. init_waitqueue_head(&cache->quiescing_wait);
  2318. atomic_set(&cache->quiescing, 0);
  2319. atomic_set(&cache->quiescing_ack, 0);
  2320. r = -ENOMEM;
  2321. atomic_set(&cache->nr_dirty, 0);
  2322. cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
  2323. if (!cache->dirty_bitset) {
  2324. *error = "could not allocate dirty bitset";
  2325. goto bad;
  2326. }
  2327. clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
  2328. cache->discard_block_size =
  2329. calculate_discard_block_size(cache->sectors_per_block,
  2330. cache->origin_sectors);
  2331. cache->discard_nr_blocks = to_dblock(dm_sector_div_up(cache->origin_sectors,
  2332. cache->discard_block_size));
  2333. cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
  2334. if (!cache->discard_bitset) {
  2335. *error = "could not allocate discard bitset";
  2336. goto bad;
  2337. }
  2338. clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
  2339. cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  2340. if (IS_ERR(cache->copier)) {
  2341. *error = "could not create kcopyd client";
  2342. r = PTR_ERR(cache->copier);
  2343. goto bad;
  2344. }
  2345. cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
  2346. if (!cache->wq) {
  2347. *error = "could not create workqueue for metadata object";
  2348. goto bad;
  2349. }
  2350. INIT_WORK(&cache->worker, do_worker);
  2351. INIT_DELAYED_WORK(&cache->waker, do_waker);
  2352. cache->last_commit_jiffies = jiffies;
  2353. cache->prison = dm_bio_prison_create();
  2354. if (!cache->prison) {
  2355. *error = "could not create bio prison";
  2356. goto bad;
  2357. }
  2358. cache->all_io_ds = dm_deferred_set_create();
  2359. if (!cache->all_io_ds) {
  2360. *error = "could not create all_io deferred set";
  2361. goto bad;
  2362. }
  2363. cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
  2364. migration_cache);
  2365. if (!cache->migration_pool) {
  2366. *error = "Error creating cache's migration mempool";
  2367. goto bad;
  2368. }
  2369. cache->need_tick_bio = true;
  2370. cache->sized = false;
  2371. cache->invalidate = false;
  2372. cache->commit_requested = false;
  2373. cache->loaded_mappings = false;
  2374. cache->loaded_discards = false;
  2375. load_stats(cache);
  2376. atomic_set(&cache->stats.demotion, 0);
  2377. atomic_set(&cache->stats.promotion, 0);
  2378. atomic_set(&cache->stats.copies_avoided, 0);
  2379. atomic_set(&cache->stats.cache_cell_clash, 0);
  2380. atomic_set(&cache->stats.commit_count, 0);
  2381. atomic_set(&cache->stats.discard_count, 0);
  2382. spin_lock_init(&cache->invalidation_lock);
  2383. INIT_LIST_HEAD(&cache->invalidation_requests);
  2384. iot_init(&cache->origin_tracker);
  2385. *result = cache;
  2386. return 0;
  2387. bad:
  2388. destroy(cache);
  2389. return r;
  2390. }
  2391. static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
  2392. {
  2393. unsigned i;
  2394. const char **copy;
  2395. copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
  2396. if (!copy)
  2397. return -ENOMEM;
  2398. for (i = 0; i < argc; i++) {
  2399. copy[i] = kstrdup(argv[i], GFP_KERNEL);
  2400. if (!copy[i]) {
  2401. while (i--)
  2402. kfree(copy[i]);
  2403. kfree(copy);
  2404. return -ENOMEM;
  2405. }
  2406. }
  2407. cache->nr_ctr_args = argc;
  2408. cache->ctr_args = copy;
  2409. return 0;
  2410. }
  2411. static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
  2412. {
  2413. int r = -EINVAL;
  2414. struct cache_args *ca;
  2415. struct cache *cache = NULL;
  2416. ca = kzalloc(sizeof(*ca), GFP_KERNEL);
  2417. if (!ca) {
  2418. ti->error = "Error allocating memory for cache";
  2419. return -ENOMEM;
  2420. }
  2421. ca->ti = ti;
  2422. r = parse_cache_args(ca, argc, argv, &ti->error);
  2423. if (r)
  2424. goto out;
  2425. r = cache_create(ca, &cache);
  2426. if (r)
  2427. goto out;
  2428. r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
  2429. if (r) {
  2430. destroy(cache);
  2431. goto out;
  2432. }
  2433. ti->private = cache;
  2434. out:
  2435. destroy_cache_args(ca);
  2436. return r;
  2437. }
  2438. /*----------------------------------------------------------------*/
  2439. static int cache_map(struct dm_target *ti, struct bio *bio)
  2440. {
  2441. struct cache *cache = ti->private;
  2442. int r;
  2443. struct dm_bio_prison_cell *cell = NULL;
  2444. dm_oblock_t block = get_bio_block(cache, bio);
  2445. size_t pb_data_size = get_per_bio_data_size(cache);
  2446. bool can_migrate = false;
  2447. bool fast_promotion;
  2448. struct policy_result lookup_result;
  2449. struct per_bio_data *pb = init_per_bio_data(bio, pb_data_size);
  2450. struct old_oblock_lock ool;
  2451. ool.locker.fn = null_locker;
  2452. if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
  2453. /*
  2454. * This can only occur if the io goes to a partial block at
  2455. * the end of the origin device. We don't cache these.
  2456. * Just remap to the origin and carry on.
  2457. */
  2458. remap_to_origin(cache, bio);
  2459. accounted_begin(cache, bio);
  2460. return DM_MAPIO_REMAPPED;
  2461. }
  2462. if (discard_or_flush(bio)) {
  2463. defer_bio(cache, bio);
  2464. return DM_MAPIO_SUBMITTED;
  2465. }
  2466. /*
  2467. * Check to see if that block is currently migrating.
  2468. */
  2469. cell = alloc_prison_cell(cache);
  2470. if (!cell) {
  2471. defer_bio(cache, bio);
  2472. return DM_MAPIO_SUBMITTED;
  2473. }
  2474. r = bio_detain(cache, block, bio, cell,
  2475. (cell_free_fn) free_prison_cell,
  2476. cache, &cell);
  2477. if (r) {
  2478. if (r < 0)
  2479. defer_bio(cache, bio);
  2480. return DM_MAPIO_SUBMITTED;
  2481. }
  2482. fast_promotion = is_discarded_oblock(cache, block) || bio_writes_complete_block(cache, bio);
  2483. r = policy_map(cache->policy, block, false, can_migrate, fast_promotion,
  2484. bio, &ool.locker, &lookup_result);
  2485. if (r == -EWOULDBLOCK) {
  2486. cell_defer(cache, cell, true);
  2487. return DM_MAPIO_SUBMITTED;
  2488. } else if (r) {
  2489. DMERR_LIMIT("%s: Unexpected return from cache replacement policy: %d",
  2490. cache_device_name(cache), r);
  2491. cell_defer(cache, cell, false);
  2492. bio_io_error(bio);
  2493. return DM_MAPIO_SUBMITTED;
  2494. }
  2495. r = DM_MAPIO_REMAPPED;
  2496. switch (lookup_result.op) {
  2497. case POLICY_HIT:
  2498. if (passthrough_mode(&cache->features)) {
  2499. if (bio_data_dir(bio) == WRITE) {
  2500. /*
  2501. * We need to invalidate this block, so
  2502. * defer for the worker thread.
  2503. */
  2504. cell_defer(cache, cell, true);
  2505. r = DM_MAPIO_SUBMITTED;
  2506. } else {
  2507. inc_miss_counter(cache, bio);
  2508. remap_to_origin_clear_discard(cache, bio, block);
  2509. accounted_begin(cache, bio);
  2510. inc_ds(cache, bio, cell);
  2511. // FIXME: we want to remap hits or misses straight
  2512. // away rather than passing over to the worker.
  2513. cell_defer(cache, cell, false);
  2514. }
  2515. } else {
  2516. inc_hit_counter(cache, bio);
  2517. if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) &&
  2518. !is_dirty(cache, lookup_result.cblock)) {
  2519. remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
  2520. accounted_begin(cache, bio);
  2521. inc_ds(cache, bio, cell);
  2522. cell_defer(cache, cell, false);
  2523. } else
  2524. remap_cell_to_cache_dirty(cache, cell, block, lookup_result.cblock, false);
  2525. }
  2526. break;
  2527. case POLICY_MISS:
  2528. inc_miss_counter(cache, bio);
  2529. if (pb->req_nr != 0) {
  2530. /*
  2531. * This is a duplicate writethrough io that is no
  2532. * longer needed because the block has been demoted.
  2533. */
  2534. bio_endio(bio);
  2535. // FIXME: remap everything as a miss
  2536. cell_defer(cache, cell, false);
  2537. r = DM_MAPIO_SUBMITTED;
  2538. } else
  2539. remap_cell_to_origin_clear_discard(cache, cell, block, false);
  2540. break;
  2541. default:
  2542. DMERR_LIMIT("%s: %s: erroring bio: unknown policy op: %u",
  2543. cache_device_name(cache), __func__,
  2544. (unsigned) lookup_result.op);
  2545. cell_defer(cache, cell, false);
  2546. bio_io_error(bio);
  2547. r = DM_MAPIO_SUBMITTED;
  2548. }
  2549. return r;
  2550. }
  2551. static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
  2552. {
  2553. struct cache *cache = ti->private;
  2554. unsigned long flags;
  2555. size_t pb_data_size = get_per_bio_data_size(cache);
  2556. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  2557. if (pb->tick) {
  2558. policy_tick(cache->policy, false);
  2559. spin_lock_irqsave(&cache->lock, flags);
  2560. cache->need_tick_bio = true;
  2561. spin_unlock_irqrestore(&cache->lock, flags);
  2562. }
  2563. check_for_quiesced_migrations(cache, pb);
  2564. accounted_complete(cache, bio);
  2565. return 0;
  2566. }
  2567. static int write_dirty_bitset(struct cache *cache)
  2568. {
  2569. unsigned i, r;
  2570. if (get_cache_mode(cache) >= CM_READ_ONLY)
  2571. return -EINVAL;
  2572. for (i = 0; i < from_cblock(cache->cache_size); i++) {
  2573. r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
  2574. is_dirty(cache, to_cblock(i)));
  2575. if (r) {
  2576. metadata_operation_failed(cache, "dm_cache_set_dirty", r);
  2577. return r;
  2578. }
  2579. }
  2580. return 0;
  2581. }
  2582. static int write_discard_bitset(struct cache *cache)
  2583. {
  2584. unsigned i, r;
  2585. if (get_cache_mode(cache) >= CM_READ_ONLY)
  2586. return -EINVAL;
  2587. r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
  2588. cache->discard_nr_blocks);
  2589. if (r) {
  2590. DMERR("%s: could not resize on-disk discard bitset", cache_device_name(cache));
  2591. metadata_operation_failed(cache, "dm_cache_discard_bitset_resize", r);
  2592. return r;
  2593. }
  2594. for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
  2595. r = dm_cache_set_discard(cache->cmd, to_dblock(i),
  2596. is_discarded(cache, to_dblock(i)));
  2597. if (r) {
  2598. metadata_operation_failed(cache, "dm_cache_set_discard", r);
  2599. return r;
  2600. }
  2601. }
  2602. return 0;
  2603. }
  2604. static int write_hints(struct cache *cache)
  2605. {
  2606. int r;
  2607. if (get_cache_mode(cache) >= CM_READ_ONLY)
  2608. return -EINVAL;
  2609. r = dm_cache_write_hints(cache->cmd, cache->policy);
  2610. if (r) {
  2611. metadata_operation_failed(cache, "dm_cache_write_hints", r);
  2612. return r;
  2613. }
  2614. return 0;
  2615. }
  2616. /*
  2617. * returns true on success
  2618. */
  2619. static bool sync_metadata(struct cache *cache)
  2620. {
  2621. int r1, r2, r3, r4;
  2622. r1 = write_dirty_bitset(cache);
  2623. if (r1)
  2624. DMERR("%s: could not write dirty bitset", cache_device_name(cache));
  2625. r2 = write_discard_bitset(cache);
  2626. if (r2)
  2627. DMERR("%s: could not write discard bitset", cache_device_name(cache));
  2628. save_stats(cache);
  2629. r3 = write_hints(cache);
  2630. if (r3)
  2631. DMERR("%s: could not write hints", cache_device_name(cache));
  2632. /*
  2633. * If writing the above metadata failed, we still commit, but don't
  2634. * set the clean shutdown flag. This will effectively force every
  2635. * dirty bit to be set on reload.
  2636. */
  2637. r4 = commit(cache, !r1 && !r2 && !r3);
  2638. if (r4)
  2639. DMERR("%s: could not write cache metadata", cache_device_name(cache));
  2640. return !r1 && !r2 && !r3 && !r4;
  2641. }
  2642. static void cache_postsuspend(struct dm_target *ti)
  2643. {
  2644. struct cache *cache = ti->private;
  2645. start_quiescing(cache);
  2646. wait_for_migrations(cache);
  2647. stop_worker(cache);
  2648. requeue_deferred_bios(cache);
  2649. requeue_deferred_cells(cache);
  2650. stop_quiescing(cache);
  2651. if (get_cache_mode(cache) == CM_WRITE)
  2652. (void) sync_metadata(cache);
  2653. }
  2654. static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
  2655. bool dirty, uint32_t hint, bool hint_valid)
  2656. {
  2657. int r;
  2658. struct cache *cache = context;
  2659. r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
  2660. if (r)
  2661. return r;
  2662. if (dirty)
  2663. set_dirty(cache, oblock, cblock);
  2664. else
  2665. clear_dirty(cache, oblock, cblock);
  2666. return 0;
  2667. }
  2668. /*
  2669. * The discard block size in the on disk metadata is not
  2670. * neccessarily the same as we're currently using. So we have to
  2671. * be careful to only set the discarded attribute if we know it
  2672. * covers a complete block of the new size.
  2673. */
  2674. struct discard_load_info {
  2675. struct cache *cache;
  2676. /*
  2677. * These blocks are sized using the on disk dblock size, rather
  2678. * than the current one.
  2679. */
  2680. dm_block_t block_size;
  2681. dm_block_t discard_begin, discard_end;
  2682. };
  2683. static void discard_load_info_init(struct cache *cache,
  2684. struct discard_load_info *li)
  2685. {
  2686. li->cache = cache;
  2687. li->discard_begin = li->discard_end = 0;
  2688. }
  2689. static void set_discard_range(struct discard_load_info *li)
  2690. {
  2691. sector_t b, e;
  2692. if (li->discard_begin == li->discard_end)
  2693. return;
  2694. /*
  2695. * Convert to sectors.
  2696. */
  2697. b = li->discard_begin * li->block_size;
  2698. e = li->discard_end * li->block_size;
  2699. /*
  2700. * Then convert back to the current dblock size.
  2701. */
  2702. b = dm_sector_div_up(b, li->cache->discard_block_size);
  2703. sector_div(e, li->cache->discard_block_size);
  2704. /*
  2705. * The origin may have shrunk, so we need to check we're still in
  2706. * bounds.
  2707. */
  2708. if (e > from_dblock(li->cache->discard_nr_blocks))
  2709. e = from_dblock(li->cache->discard_nr_blocks);
  2710. for (; b < e; b++)
  2711. set_discard(li->cache, to_dblock(b));
  2712. }
  2713. static int load_discard(void *context, sector_t discard_block_size,
  2714. dm_dblock_t dblock, bool discard)
  2715. {
  2716. struct discard_load_info *li = context;
  2717. li->block_size = discard_block_size;
  2718. if (discard) {
  2719. if (from_dblock(dblock) == li->discard_end)
  2720. /*
  2721. * We're already in a discard range, just extend it.
  2722. */
  2723. li->discard_end = li->discard_end + 1ULL;
  2724. else {
  2725. /*
  2726. * Emit the old range and start a new one.
  2727. */
  2728. set_discard_range(li);
  2729. li->discard_begin = from_dblock(dblock);
  2730. li->discard_end = li->discard_begin + 1ULL;
  2731. }
  2732. } else {
  2733. set_discard_range(li);
  2734. li->discard_begin = li->discard_end = 0;
  2735. }
  2736. return 0;
  2737. }
  2738. static dm_cblock_t get_cache_dev_size(struct cache *cache)
  2739. {
  2740. sector_t size = get_dev_size(cache->cache_dev);
  2741. (void) sector_div(size, cache->sectors_per_block);
  2742. return to_cblock(size);
  2743. }
  2744. static bool can_resize(struct cache *cache, dm_cblock_t new_size)
  2745. {
  2746. if (from_cblock(new_size) > from_cblock(cache->cache_size))
  2747. return true;
  2748. /*
  2749. * We can't drop a dirty block when shrinking the cache.
  2750. */
  2751. while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
  2752. new_size = to_cblock(from_cblock(new_size) + 1);
  2753. if (is_dirty(cache, new_size)) {
  2754. DMERR("%s: unable to shrink cache; cache block %llu is dirty",
  2755. cache_device_name(cache),
  2756. (unsigned long long) from_cblock(new_size));
  2757. return false;
  2758. }
  2759. }
  2760. return true;
  2761. }
  2762. static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
  2763. {
  2764. int r;
  2765. r = dm_cache_resize(cache->cmd, new_size);
  2766. if (r) {
  2767. DMERR("%s: could not resize cache metadata", cache_device_name(cache));
  2768. metadata_operation_failed(cache, "dm_cache_resize", r);
  2769. return r;
  2770. }
  2771. set_cache_size(cache, new_size);
  2772. return 0;
  2773. }
  2774. static int cache_preresume(struct dm_target *ti)
  2775. {
  2776. int r = 0;
  2777. struct cache *cache = ti->private;
  2778. dm_cblock_t csize = get_cache_dev_size(cache);
  2779. /*
  2780. * Check to see if the cache has resized.
  2781. */
  2782. if (!cache->sized) {
  2783. r = resize_cache_dev(cache, csize);
  2784. if (r)
  2785. return r;
  2786. cache->sized = true;
  2787. } else if (csize != cache->cache_size) {
  2788. if (!can_resize(cache, csize))
  2789. return -EINVAL;
  2790. r = resize_cache_dev(cache, csize);
  2791. if (r)
  2792. return r;
  2793. }
  2794. if (!cache->loaded_mappings) {
  2795. r = dm_cache_load_mappings(cache->cmd, cache->policy,
  2796. load_mapping, cache);
  2797. if (r) {
  2798. DMERR("%s: could not load cache mappings", cache_device_name(cache));
  2799. metadata_operation_failed(cache, "dm_cache_load_mappings", r);
  2800. return r;
  2801. }
  2802. cache->loaded_mappings = true;
  2803. }
  2804. if (!cache->loaded_discards) {
  2805. struct discard_load_info li;
  2806. /*
  2807. * The discard bitset could have been resized, or the
  2808. * discard block size changed. To be safe we start by
  2809. * setting every dblock to not discarded.
  2810. */
  2811. clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
  2812. discard_load_info_init(cache, &li);
  2813. r = dm_cache_load_discards(cache->cmd, load_discard, &li);
  2814. if (r) {
  2815. DMERR("%s: could not load origin discards", cache_device_name(cache));
  2816. metadata_operation_failed(cache, "dm_cache_load_discards", r);
  2817. return r;
  2818. }
  2819. set_discard_range(&li);
  2820. cache->loaded_discards = true;
  2821. }
  2822. return r;
  2823. }
  2824. static void cache_resume(struct dm_target *ti)
  2825. {
  2826. struct cache *cache = ti->private;
  2827. cache->need_tick_bio = true;
  2828. do_waker(&cache->waker.work);
  2829. }
  2830. /*
  2831. * Status format:
  2832. *
  2833. * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
  2834. * <cache block size> <#used cache blocks>/<#total cache blocks>
  2835. * <#read hits> <#read misses> <#write hits> <#write misses>
  2836. * <#demotions> <#promotions> <#dirty>
  2837. * <#features> <features>*
  2838. * <#core args> <core args>
  2839. * <policy name> <#policy args> <policy args>* <cache metadata mode> <needs_check>
  2840. */
  2841. static void cache_status(struct dm_target *ti, status_type_t type,
  2842. unsigned status_flags, char *result, unsigned maxlen)
  2843. {
  2844. int r = 0;
  2845. unsigned i;
  2846. ssize_t sz = 0;
  2847. dm_block_t nr_free_blocks_metadata = 0;
  2848. dm_block_t nr_blocks_metadata = 0;
  2849. char buf[BDEVNAME_SIZE];
  2850. struct cache *cache = ti->private;
  2851. dm_cblock_t residency;
  2852. bool needs_check;
  2853. switch (type) {
  2854. case STATUSTYPE_INFO:
  2855. if (get_cache_mode(cache) == CM_FAIL) {
  2856. DMEMIT("Fail");
  2857. break;
  2858. }
  2859. /* Commit to ensure statistics aren't out-of-date */
  2860. if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
  2861. (void) commit(cache, false);
  2862. r = dm_cache_get_free_metadata_block_count(cache->cmd, &nr_free_blocks_metadata);
  2863. if (r) {
  2864. DMERR("%s: dm_cache_get_free_metadata_block_count returned %d",
  2865. cache_device_name(cache), r);
  2866. goto err;
  2867. }
  2868. r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
  2869. if (r) {
  2870. DMERR("%s: dm_cache_get_metadata_dev_size returned %d",
  2871. cache_device_name(cache), r);
  2872. goto err;
  2873. }
  2874. residency = policy_residency(cache->policy);
  2875. DMEMIT("%u %llu/%llu %llu %llu/%llu %u %u %u %u %u %u %lu ",
  2876. (unsigned)DM_CACHE_METADATA_BLOCK_SIZE,
  2877. (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
  2878. (unsigned long long)nr_blocks_metadata,
  2879. (unsigned long long)cache->sectors_per_block,
  2880. (unsigned long long) from_cblock(residency),
  2881. (unsigned long long) from_cblock(cache->cache_size),
  2882. (unsigned) atomic_read(&cache->stats.read_hit),
  2883. (unsigned) atomic_read(&cache->stats.read_miss),
  2884. (unsigned) atomic_read(&cache->stats.write_hit),
  2885. (unsigned) atomic_read(&cache->stats.write_miss),
  2886. (unsigned) atomic_read(&cache->stats.demotion),
  2887. (unsigned) atomic_read(&cache->stats.promotion),
  2888. (unsigned long) atomic_read(&cache->nr_dirty));
  2889. if (writethrough_mode(&cache->features))
  2890. DMEMIT("1 writethrough ");
  2891. else if (passthrough_mode(&cache->features))
  2892. DMEMIT("1 passthrough ");
  2893. else if (writeback_mode(&cache->features))
  2894. DMEMIT("1 writeback ");
  2895. else {
  2896. DMERR("%s: internal error: unknown io mode: %d",
  2897. cache_device_name(cache), (int) cache->features.io_mode);
  2898. goto err;
  2899. }
  2900. DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
  2901. DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
  2902. if (sz < maxlen) {
  2903. r = policy_emit_config_values(cache->policy, result, maxlen, &sz);
  2904. if (r)
  2905. DMERR("%s: policy_emit_config_values returned %d",
  2906. cache_device_name(cache), r);
  2907. }
  2908. if (get_cache_mode(cache) == CM_READ_ONLY)
  2909. DMEMIT("ro ");
  2910. else
  2911. DMEMIT("rw ");
  2912. r = dm_cache_metadata_needs_check(cache->cmd, &needs_check);
  2913. if (r || needs_check)
  2914. DMEMIT("needs_check ");
  2915. else
  2916. DMEMIT("- ");
  2917. break;
  2918. case STATUSTYPE_TABLE:
  2919. format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
  2920. DMEMIT("%s ", buf);
  2921. format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
  2922. DMEMIT("%s ", buf);
  2923. format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
  2924. DMEMIT("%s", buf);
  2925. for (i = 0; i < cache->nr_ctr_args - 1; i++)
  2926. DMEMIT(" %s", cache->ctr_args[i]);
  2927. if (cache->nr_ctr_args)
  2928. DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
  2929. }
  2930. return;
  2931. err:
  2932. DMEMIT("Error");
  2933. }
  2934. /*
  2935. * A cache block range can take two forms:
  2936. *
  2937. * i) A single cblock, eg. '3456'
  2938. * ii) A begin and end cblock with dots between, eg. 123-234
  2939. */
  2940. static int parse_cblock_range(struct cache *cache, const char *str,
  2941. struct cblock_range *result)
  2942. {
  2943. char dummy;
  2944. uint64_t b, e;
  2945. int r;
  2946. /*
  2947. * Try and parse form (ii) first.
  2948. */
  2949. r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
  2950. if (r < 0)
  2951. return r;
  2952. if (r == 2) {
  2953. result->begin = to_cblock(b);
  2954. result->end = to_cblock(e);
  2955. return 0;
  2956. }
  2957. /*
  2958. * That didn't work, try form (i).
  2959. */
  2960. r = sscanf(str, "%llu%c", &b, &dummy);
  2961. if (r < 0)
  2962. return r;
  2963. if (r == 1) {
  2964. result->begin = to_cblock(b);
  2965. result->end = to_cblock(from_cblock(result->begin) + 1u);
  2966. return 0;
  2967. }
  2968. DMERR("%s: invalid cblock range '%s'", cache_device_name(cache), str);
  2969. return -EINVAL;
  2970. }
  2971. static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
  2972. {
  2973. uint64_t b = from_cblock(range->begin);
  2974. uint64_t e = from_cblock(range->end);
  2975. uint64_t n = from_cblock(cache->cache_size);
  2976. if (b >= n) {
  2977. DMERR("%s: begin cblock out of range: %llu >= %llu",
  2978. cache_device_name(cache), b, n);
  2979. return -EINVAL;
  2980. }
  2981. if (e > n) {
  2982. DMERR("%s: end cblock out of range: %llu > %llu",
  2983. cache_device_name(cache), e, n);
  2984. return -EINVAL;
  2985. }
  2986. if (b >= e) {
  2987. DMERR("%s: invalid cblock range: %llu >= %llu",
  2988. cache_device_name(cache), b, e);
  2989. return -EINVAL;
  2990. }
  2991. return 0;
  2992. }
  2993. static int request_invalidation(struct cache *cache, struct cblock_range *range)
  2994. {
  2995. struct invalidation_request req;
  2996. INIT_LIST_HEAD(&req.list);
  2997. req.cblocks = range;
  2998. atomic_set(&req.complete, 0);
  2999. req.err = 0;
  3000. init_waitqueue_head(&req.result_wait);
  3001. spin_lock(&cache->invalidation_lock);
  3002. list_add(&req.list, &cache->invalidation_requests);
  3003. spin_unlock(&cache->invalidation_lock);
  3004. wake_worker(cache);
  3005. wait_event(req.result_wait, atomic_read(&req.complete));
  3006. return req.err;
  3007. }
  3008. static int process_invalidate_cblocks_message(struct cache *cache, unsigned count,
  3009. const char **cblock_ranges)
  3010. {
  3011. int r = 0;
  3012. unsigned i;
  3013. struct cblock_range range;
  3014. if (!passthrough_mode(&cache->features)) {
  3015. DMERR("%s: cache has to be in passthrough mode for invalidation",
  3016. cache_device_name(cache));
  3017. return -EPERM;
  3018. }
  3019. for (i = 0; i < count; i++) {
  3020. r = parse_cblock_range(cache, cblock_ranges[i], &range);
  3021. if (r)
  3022. break;
  3023. r = validate_cblock_range(cache, &range);
  3024. if (r)
  3025. break;
  3026. /*
  3027. * Pass begin and end origin blocks to the worker and wake it.
  3028. */
  3029. r = request_invalidation(cache, &range);
  3030. if (r)
  3031. break;
  3032. }
  3033. return r;
  3034. }
  3035. /*
  3036. * Supports
  3037. * "<key> <value>"
  3038. * and
  3039. * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
  3040. *
  3041. * The key migration_threshold is supported by the cache target core.
  3042. */
  3043. static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
  3044. {
  3045. struct cache *cache = ti->private;
  3046. if (!argc)
  3047. return -EINVAL;
  3048. if (get_cache_mode(cache) >= CM_READ_ONLY) {
  3049. DMERR("%s: unable to service cache target messages in READ_ONLY or FAIL mode",
  3050. cache_device_name(cache));
  3051. return -EOPNOTSUPP;
  3052. }
  3053. if (!strcasecmp(argv[0], "invalidate_cblocks"))
  3054. return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
  3055. if (argc != 2)
  3056. return -EINVAL;
  3057. return set_config_value(cache, argv[0], argv[1]);
  3058. }
  3059. static int cache_iterate_devices(struct dm_target *ti,
  3060. iterate_devices_callout_fn fn, void *data)
  3061. {
  3062. int r = 0;
  3063. struct cache *cache = ti->private;
  3064. r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
  3065. if (!r)
  3066. r = fn(ti, cache->origin_dev, 0, ti->len, data);
  3067. return r;
  3068. }
  3069. static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
  3070. {
  3071. /*
  3072. * FIXME: these limits may be incompatible with the cache device
  3073. */
  3074. limits->max_discard_sectors = min_t(sector_t, cache->discard_block_size * 1024,
  3075. cache->origin_sectors);
  3076. limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
  3077. }
  3078. static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
  3079. {
  3080. struct cache *cache = ti->private;
  3081. uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
  3082. /*
  3083. * If the system-determined stacked limits are compatible with the
  3084. * cache's blocksize (io_opt is a factor) do not override them.
  3085. */
  3086. if (io_opt_sectors < cache->sectors_per_block ||
  3087. do_div(io_opt_sectors, cache->sectors_per_block)) {
  3088. blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT);
  3089. blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
  3090. }
  3091. set_discard_limits(cache, limits);
  3092. }
  3093. /*----------------------------------------------------------------*/
  3094. static struct target_type cache_target = {
  3095. .name = "cache",
  3096. .version = {1, 9, 0},
  3097. .module = THIS_MODULE,
  3098. .ctr = cache_ctr,
  3099. .dtr = cache_dtr,
  3100. .map = cache_map,
  3101. .end_io = cache_end_io,
  3102. .postsuspend = cache_postsuspend,
  3103. .preresume = cache_preresume,
  3104. .resume = cache_resume,
  3105. .status = cache_status,
  3106. .message = cache_message,
  3107. .iterate_devices = cache_iterate_devices,
  3108. .io_hints = cache_io_hints,
  3109. };
  3110. static int __init dm_cache_init(void)
  3111. {
  3112. int r;
  3113. r = dm_register_target(&cache_target);
  3114. if (r) {
  3115. DMERR("cache target registration failed: %d", r);
  3116. return r;
  3117. }
  3118. migration_cache = KMEM_CACHE(dm_cache_migration, 0);
  3119. if (!migration_cache) {
  3120. dm_unregister_target(&cache_target);
  3121. return -ENOMEM;
  3122. }
  3123. return 0;
  3124. }
  3125. static void __exit dm_cache_exit(void)
  3126. {
  3127. dm_unregister_target(&cache_target);
  3128. kmem_cache_destroy(migration_cache);
  3129. }
  3130. module_init(dm_cache_init);
  3131. module_exit(dm_cache_exit);
  3132. MODULE_DESCRIPTION(DM_NAME " cache target");
  3133. MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
  3134. MODULE_LICENSE("GPL");