dm-cache-target.c 81 KB

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