dm-cache-target.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692
  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. #define PRISON_CELLS 1024
  52. #define MIGRATION_POOL_SIZE 128
  53. #define COMMIT_PERIOD HZ
  54. #define MIGRATION_COUNT_WINDOW 10
  55. /*
  56. * The block size of the device holding cache data must be
  57. * between 32KB and 1GB.
  58. */
  59. #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
  60. #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
  61. /*
  62. * FIXME: the cache is read/write for the time being.
  63. */
  64. enum cache_mode {
  65. CM_WRITE, /* metadata may be changed */
  66. CM_READ_ONLY, /* metadata may not be changed */
  67. };
  68. struct cache_features {
  69. enum cache_mode mode;
  70. bool write_through:1;
  71. };
  72. struct cache_stats {
  73. atomic_t read_hit;
  74. atomic_t read_miss;
  75. atomic_t write_hit;
  76. atomic_t write_miss;
  77. atomic_t demotion;
  78. atomic_t promotion;
  79. atomic_t copies_avoided;
  80. atomic_t cache_cell_clash;
  81. atomic_t commit_count;
  82. atomic_t discard_count;
  83. };
  84. struct cache {
  85. struct dm_target *ti;
  86. struct dm_target_callbacks callbacks;
  87. struct dm_cache_metadata *cmd;
  88. /*
  89. * Metadata is written to this device.
  90. */
  91. struct dm_dev *metadata_dev;
  92. /*
  93. * The slower of the two data devices. Typically a spindle.
  94. */
  95. struct dm_dev *origin_dev;
  96. /*
  97. * The faster of the two data devices. Typically an SSD.
  98. */
  99. struct dm_dev *cache_dev;
  100. /*
  101. * Size of the origin device in _complete_ blocks and native sectors.
  102. */
  103. dm_oblock_t origin_blocks;
  104. sector_t origin_sectors;
  105. /*
  106. * Size of the cache device in blocks.
  107. */
  108. dm_cblock_t cache_size;
  109. /*
  110. * Fields for converting from sectors to blocks.
  111. */
  112. uint32_t sectors_per_block;
  113. int sectors_per_block_shift;
  114. spinlock_t lock;
  115. struct bio_list deferred_bios;
  116. struct bio_list deferred_flush_bios;
  117. struct bio_list deferred_writethrough_bios;
  118. struct list_head quiesced_migrations;
  119. struct list_head completed_migrations;
  120. struct list_head need_commit_migrations;
  121. sector_t migration_threshold;
  122. wait_queue_head_t migration_wait;
  123. atomic_t nr_migrations;
  124. wait_queue_head_t quiescing_wait;
  125. atomic_t quiescing;
  126. atomic_t quiescing_ack;
  127. /*
  128. * cache_size entries, dirty if set
  129. */
  130. dm_cblock_t nr_dirty;
  131. unsigned long *dirty_bitset;
  132. /*
  133. * origin_blocks entries, discarded if set.
  134. */
  135. dm_dblock_t discard_nr_blocks;
  136. unsigned long *discard_bitset;
  137. uint32_t discard_block_size; /* a power of 2 times sectors per block */
  138. /*
  139. * Rather than reconstructing the table line for the status we just
  140. * save it and regurgitate.
  141. */
  142. unsigned nr_ctr_args;
  143. const char **ctr_args;
  144. struct dm_kcopyd_client *copier;
  145. struct workqueue_struct *wq;
  146. struct work_struct worker;
  147. struct delayed_work waker;
  148. unsigned long last_commit_jiffies;
  149. struct dm_bio_prison *prison;
  150. struct dm_deferred_set *all_io_ds;
  151. mempool_t *migration_pool;
  152. struct dm_cache_migration *next_migration;
  153. struct dm_cache_policy *policy;
  154. unsigned policy_nr_args;
  155. bool need_tick_bio:1;
  156. bool sized:1;
  157. bool commit_requested:1;
  158. bool loaded_mappings:1;
  159. bool loaded_discards:1;
  160. /*
  161. * Cache features such as write-through.
  162. */
  163. struct cache_features features;
  164. struct cache_stats stats;
  165. };
  166. struct per_bio_data {
  167. bool tick:1;
  168. unsigned req_nr:2;
  169. struct dm_deferred_entry *all_io_entry;
  170. /*
  171. * writethrough fields. These MUST remain at the end of this
  172. * structure and the 'cache' member must be the first as it
  173. * is used to determine the offset of the writethrough fields.
  174. */
  175. struct cache *cache;
  176. dm_cblock_t cblock;
  177. bio_end_io_t *saved_bi_end_io;
  178. struct dm_bio_details bio_details;
  179. };
  180. struct dm_cache_migration {
  181. struct list_head list;
  182. struct cache *cache;
  183. unsigned long start_jiffies;
  184. dm_oblock_t old_oblock;
  185. dm_oblock_t new_oblock;
  186. dm_cblock_t cblock;
  187. bool err:1;
  188. bool writeback:1;
  189. bool demote:1;
  190. bool promote:1;
  191. struct dm_bio_prison_cell *old_ocell;
  192. struct dm_bio_prison_cell *new_ocell;
  193. };
  194. /*
  195. * Processing a bio in the worker thread may require these memory
  196. * allocations. We prealloc to avoid deadlocks (the same worker thread
  197. * frees them back to the mempool).
  198. */
  199. struct prealloc {
  200. struct dm_cache_migration *mg;
  201. struct dm_bio_prison_cell *cell1;
  202. struct dm_bio_prison_cell *cell2;
  203. };
  204. static void wake_worker(struct cache *cache)
  205. {
  206. queue_work(cache->wq, &cache->worker);
  207. }
  208. /*----------------------------------------------------------------*/
  209. static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
  210. {
  211. /* FIXME: change to use a local slab. */
  212. return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
  213. }
  214. static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
  215. {
  216. dm_bio_prison_free_cell(cache->prison, cell);
  217. }
  218. static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
  219. {
  220. if (!p->mg) {
  221. p->mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
  222. if (!p->mg)
  223. return -ENOMEM;
  224. }
  225. if (!p->cell1) {
  226. p->cell1 = alloc_prison_cell(cache);
  227. if (!p->cell1)
  228. return -ENOMEM;
  229. }
  230. if (!p->cell2) {
  231. p->cell2 = alloc_prison_cell(cache);
  232. if (!p->cell2)
  233. return -ENOMEM;
  234. }
  235. return 0;
  236. }
  237. static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
  238. {
  239. if (p->cell2)
  240. free_prison_cell(cache, p->cell2);
  241. if (p->cell1)
  242. free_prison_cell(cache, p->cell1);
  243. if (p->mg)
  244. mempool_free(p->mg, cache->migration_pool);
  245. }
  246. static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
  247. {
  248. struct dm_cache_migration *mg = p->mg;
  249. BUG_ON(!mg);
  250. p->mg = NULL;
  251. return mg;
  252. }
  253. /*
  254. * You must have a cell within the prealloc struct to return. If not this
  255. * function will BUG() rather than returning NULL.
  256. */
  257. static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
  258. {
  259. struct dm_bio_prison_cell *r = NULL;
  260. if (p->cell1) {
  261. r = p->cell1;
  262. p->cell1 = NULL;
  263. } else if (p->cell2) {
  264. r = p->cell2;
  265. p->cell2 = NULL;
  266. } else
  267. BUG();
  268. return r;
  269. }
  270. /*
  271. * You can't have more than two cells in a prealloc struct. BUG() will be
  272. * called if you try and overfill.
  273. */
  274. static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
  275. {
  276. if (!p->cell2)
  277. p->cell2 = cell;
  278. else if (!p->cell1)
  279. p->cell1 = cell;
  280. else
  281. BUG();
  282. }
  283. /*----------------------------------------------------------------*/
  284. static void build_key(dm_oblock_t oblock, struct dm_cell_key *key)
  285. {
  286. key->virtual = 0;
  287. key->dev = 0;
  288. key->block = from_oblock(oblock);
  289. }
  290. /*
  291. * The caller hands in a preallocated cell, and a free function for it.
  292. * The cell will be freed if there's an error, or if it wasn't used because
  293. * a cell with that key already exists.
  294. */
  295. typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
  296. static int bio_detain(struct cache *cache, dm_oblock_t oblock,
  297. struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
  298. cell_free_fn free_fn, void *free_context,
  299. struct dm_bio_prison_cell **cell_result)
  300. {
  301. int r;
  302. struct dm_cell_key key;
  303. build_key(oblock, &key);
  304. r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
  305. if (r)
  306. free_fn(free_context, cell_prealloc);
  307. return r;
  308. }
  309. static int get_cell(struct cache *cache,
  310. dm_oblock_t oblock,
  311. struct prealloc *structs,
  312. struct dm_bio_prison_cell **cell_result)
  313. {
  314. int r;
  315. struct dm_cell_key key;
  316. struct dm_bio_prison_cell *cell_prealloc;
  317. cell_prealloc = prealloc_get_cell(structs);
  318. build_key(oblock, &key);
  319. r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
  320. if (r)
  321. prealloc_put_cell(structs, cell_prealloc);
  322. return r;
  323. }
  324. /*----------------------------------------------------------------*/
  325. static bool is_dirty(struct cache *cache, dm_cblock_t b)
  326. {
  327. return test_bit(from_cblock(b), cache->dirty_bitset);
  328. }
  329. static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
  330. {
  331. if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
  332. cache->nr_dirty = to_cblock(from_cblock(cache->nr_dirty) + 1);
  333. policy_set_dirty(cache->policy, oblock);
  334. }
  335. }
  336. static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
  337. {
  338. if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
  339. policy_clear_dirty(cache->policy, oblock);
  340. cache->nr_dirty = to_cblock(from_cblock(cache->nr_dirty) - 1);
  341. if (!from_cblock(cache->nr_dirty))
  342. dm_table_event(cache->ti->table);
  343. }
  344. }
  345. /*----------------------------------------------------------------*/
  346. static bool block_size_is_power_of_two(struct cache *cache)
  347. {
  348. return cache->sectors_per_block_shift >= 0;
  349. }
  350. /* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
  351. #if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
  352. __always_inline
  353. #endif
  354. static dm_block_t block_div(dm_block_t b, uint32_t n)
  355. {
  356. do_div(b, n);
  357. return b;
  358. }
  359. static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock)
  360. {
  361. uint32_t discard_blocks = cache->discard_block_size;
  362. dm_block_t b = from_oblock(oblock);
  363. if (!block_size_is_power_of_two(cache))
  364. discard_blocks = discard_blocks / cache->sectors_per_block;
  365. else
  366. discard_blocks >>= cache->sectors_per_block_shift;
  367. b = block_div(b, discard_blocks);
  368. return to_dblock(b);
  369. }
  370. static void set_discard(struct cache *cache, dm_dblock_t b)
  371. {
  372. unsigned long flags;
  373. atomic_inc(&cache->stats.discard_count);
  374. spin_lock_irqsave(&cache->lock, flags);
  375. set_bit(from_dblock(b), cache->discard_bitset);
  376. spin_unlock_irqrestore(&cache->lock, flags);
  377. }
  378. static void clear_discard(struct cache *cache, dm_dblock_t b)
  379. {
  380. unsigned long flags;
  381. spin_lock_irqsave(&cache->lock, flags);
  382. clear_bit(from_dblock(b), cache->discard_bitset);
  383. spin_unlock_irqrestore(&cache->lock, flags);
  384. }
  385. static bool is_discarded(struct cache *cache, dm_dblock_t b)
  386. {
  387. int r;
  388. unsigned long flags;
  389. spin_lock_irqsave(&cache->lock, flags);
  390. r = test_bit(from_dblock(b), cache->discard_bitset);
  391. spin_unlock_irqrestore(&cache->lock, flags);
  392. return r;
  393. }
  394. static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
  395. {
  396. int r;
  397. unsigned long flags;
  398. spin_lock_irqsave(&cache->lock, flags);
  399. r = test_bit(from_dblock(oblock_to_dblock(cache, b)),
  400. cache->discard_bitset);
  401. spin_unlock_irqrestore(&cache->lock, flags);
  402. return r;
  403. }
  404. /*----------------------------------------------------------------*/
  405. static void load_stats(struct cache *cache)
  406. {
  407. struct dm_cache_statistics stats;
  408. dm_cache_metadata_get_stats(cache->cmd, &stats);
  409. atomic_set(&cache->stats.read_hit, stats.read_hits);
  410. atomic_set(&cache->stats.read_miss, stats.read_misses);
  411. atomic_set(&cache->stats.write_hit, stats.write_hits);
  412. atomic_set(&cache->stats.write_miss, stats.write_misses);
  413. }
  414. static void save_stats(struct cache *cache)
  415. {
  416. struct dm_cache_statistics stats;
  417. stats.read_hits = atomic_read(&cache->stats.read_hit);
  418. stats.read_misses = atomic_read(&cache->stats.read_miss);
  419. stats.write_hits = atomic_read(&cache->stats.write_hit);
  420. stats.write_misses = atomic_read(&cache->stats.write_miss);
  421. dm_cache_metadata_set_stats(cache->cmd, &stats);
  422. }
  423. /*----------------------------------------------------------------
  424. * Per bio data
  425. *--------------------------------------------------------------*/
  426. /*
  427. * If using writeback, leave out struct per_bio_data's writethrough fields.
  428. */
  429. #define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
  430. #define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
  431. static size_t get_per_bio_data_size(struct cache *cache)
  432. {
  433. return cache->features.write_through ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
  434. }
  435. static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
  436. {
  437. struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
  438. BUG_ON(!pb);
  439. return pb;
  440. }
  441. static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
  442. {
  443. struct per_bio_data *pb = get_per_bio_data(bio, data_size);
  444. pb->tick = false;
  445. pb->req_nr = dm_bio_get_target_bio_nr(bio);
  446. pb->all_io_entry = NULL;
  447. return pb;
  448. }
  449. /*----------------------------------------------------------------
  450. * Remapping
  451. *--------------------------------------------------------------*/
  452. static void remap_to_origin(struct cache *cache, struct bio *bio)
  453. {
  454. bio->bi_bdev = cache->origin_dev->bdev;
  455. }
  456. static void remap_to_cache(struct cache *cache, struct bio *bio,
  457. dm_cblock_t cblock)
  458. {
  459. sector_t bi_sector = bio->bi_sector;
  460. bio->bi_bdev = cache->cache_dev->bdev;
  461. if (!block_size_is_power_of_two(cache))
  462. bio->bi_sector = (from_cblock(cblock) * cache->sectors_per_block) +
  463. sector_div(bi_sector, cache->sectors_per_block);
  464. else
  465. bio->bi_sector = (from_cblock(cblock) << cache->sectors_per_block_shift) |
  466. (bi_sector & (cache->sectors_per_block - 1));
  467. }
  468. static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
  469. {
  470. unsigned long flags;
  471. size_t pb_data_size = get_per_bio_data_size(cache);
  472. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  473. spin_lock_irqsave(&cache->lock, flags);
  474. if (cache->need_tick_bio &&
  475. !(bio->bi_rw & (REQ_FUA | REQ_FLUSH | REQ_DISCARD))) {
  476. pb->tick = true;
  477. cache->need_tick_bio = false;
  478. }
  479. spin_unlock_irqrestore(&cache->lock, flags);
  480. }
  481. static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
  482. dm_oblock_t oblock)
  483. {
  484. check_if_tick_bio_needed(cache, bio);
  485. remap_to_origin(cache, bio);
  486. if (bio_data_dir(bio) == WRITE)
  487. clear_discard(cache, oblock_to_dblock(cache, oblock));
  488. }
  489. static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
  490. dm_oblock_t oblock, dm_cblock_t cblock)
  491. {
  492. check_if_tick_bio_needed(cache, bio);
  493. remap_to_cache(cache, bio, cblock);
  494. if (bio_data_dir(bio) == WRITE) {
  495. set_dirty(cache, oblock, cblock);
  496. clear_discard(cache, oblock_to_dblock(cache, oblock));
  497. }
  498. }
  499. static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
  500. {
  501. sector_t block_nr = bio->bi_sector;
  502. if (!block_size_is_power_of_two(cache))
  503. (void) sector_div(block_nr, cache->sectors_per_block);
  504. else
  505. block_nr >>= cache->sectors_per_block_shift;
  506. return to_oblock(block_nr);
  507. }
  508. static int bio_triggers_commit(struct cache *cache, struct bio *bio)
  509. {
  510. return bio->bi_rw & (REQ_FLUSH | REQ_FUA);
  511. }
  512. static void issue(struct cache *cache, struct bio *bio)
  513. {
  514. unsigned long flags;
  515. if (!bio_triggers_commit(cache, bio)) {
  516. generic_make_request(bio);
  517. return;
  518. }
  519. /*
  520. * Batch together any bios that trigger commits and then issue a
  521. * single commit for them in do_worker().
  522. */
  523. spin_lock_irqsave(&cache->lock, flags);
  524. cache->commit_requested = true;
  525. bio_list_add(&cache->deferred_flush_bios, bio);
  526. spin_unlock_irqrestore(&cache->lock, flags);
  527. }
  528. static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
  529. {
  530. unsigned long flags;
  531. spin_lock_irqsave(&cache->lock, flags);
  532. bio_list_add(&cache->deferred_writethrough_bios, bio);
  533. spin_unlock_irqrestore(&cache->lock, flags);
  534. wake_worker(cache);
  535. }
  536. static void writethrough_endio(struct bio *bio, int err)
  537. {
  538. struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
  539. bio->bi_end_io = pb->saved_bi_end_io;
  540. if (err) {
  541. bio_endio(bio, err);
  542. return;
  543. }
  544. dm_bio_restore(&pb->bio_details, bio);
  545. remap_to_cache(pb->cache, bio, pb->cblock);
  546. /*
  547. * We can't issue this bio directly, since we're in interrupt
  548. * context. So it gets put on a bio list for processing by the
  549. * worker thread.
  550. */
  551. defer_writethrough_bio(pb->cache, bio);
  552. }
  553. /*
  554. * When running in writethrough mode we need to send writes to clean blocks
  555. * to both the cache and origin devices. In future we'd like to clone the
  556. * bio and send them in parallel, but for now we're doing them in
  557. * series as this is easier.
  558. */
  559. static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
  560. dm_oblock_t oblock, dm_cblock_t cblock)
  561. {
  562. struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
  563. pb->cache = cache;
  564. pb->cblock = cblock;
  565. pb->saved_bi_end_io = bio->bi_end_io;
  566. dm_bio_record(&pb->bio_details, bio);
  567. bio->bi_end_io = writethrough_endio;
  568. remap_to_origin_clear_discard(pb->cache, bio, oblock);
  569. }
  570. /*----------------------------------------------------------------
  571. * Migration processing
  572. *
  573. * Migration covers moving data from the origin device to the cache, or
  574. * vice versa.
  575. *--------------------------------------------------------------*/
  576. static void free_migration(struct dm_cache_migration *mg)
  577. {
  578. mempool_free(mg, mg->cache->migration_pool);
  579. }
  580. static void inc_nr_migrations(struct cache *cache)
  581. {
  582. atomic_inc(&cache->nr_migrations);
  583. }
  584. static void dec_nr_migrations(struct cache *cache)
  585. {
  586. atomic_dec(&cache->nr_migrations);
  587. /*
  588. * Wake the worker in case we're suspending the target.
  589. */
  590. wake_up(&cache->migration_wait);
  591. }
  592. static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
  593. bool holder)
  594. {
  595. (holder ? dm_cell_release : dm_cell_release_no_holder)
  596. (cache->prison, cell, &cache->deferred_bios);
  597. free_prison_cell(cache, cell);
  598. }
  599. static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
  600. bool holder)
  601. {
  602. unsigned long flags;
  603. spin_lock_irqsave(&cache->lock, flags);
  604. __cell_defer(cache, cell, holder);
  605. spin_unlock_irqrestore(&cache->lock, flags);
  606. wake_worker(cache);
  607. }
  608. static void cleanup_migration(struct dm_cache_migration *mg)
  609. {
  610. struct cache *cache = mg->cache;
  611. free_migration(mg);
  612. dec_nr_migrations(cache);
  613. }
  614. static void migration_failure(struct dm_cache_migration *mg)
  615. {
  616. struct cache *cache = mg->cache;
  617. if (mg->writeback) {
  618. DMWARN_LIMIT("writeback failed; couldn't copy block");
  619. set_dirty(cache, mg->old_oblock, mg->cblock);
  620. cell_defer(cache, mg->old_ocell, false);
  621. } else if (mg->demote) {
  622. DMWARN_LIMIT("demotion failed; couldn't copy block");
  623. policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
  624. cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
  625. if (mg->promote)
  626. cell_defer(cache, mg->new_ocell, true);
  627. } else {
  628. DMWARN_LIMIT("promotion failed; couldn't copy block");
  629. policy_remove_mapping(cache->policy, mg->new_oblock);
  630. cell_defer(cache, mg->new_ocell, true);
  631. }
  632. cleanup_migration(mg);
  633. }
  634. static void migration_success_pre_commit(struct dm_cache_migration *mg)
  635. {
  636. unsigned long flags;
  637. struct cache *cache = mg->cache;
  638. if (mg->writeback) {
  639. cell_defer(cache, mg->old_ocell, false);
  640. clear_dirty(cache, mg->old_oblock, mg->cblock);
  641. cleanup_migration(mg);
  642. return;
  643. } else if (mg->demote) {
  644. if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) {
  645. DMWARN_LIMIT("demotion failed; couldn't update on disk metadata");
  646. policy_force_mapping(cache->policy, mg->new_oblock,
  647. mg->old_oblock);
  648. if (mg->promote)
  649. cell_defer(cache, mg->new_ocell, true);
  650. cleanup_migration(mg);
  651. return;
  652. }
  653. } else {
  654. if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) {
  655. DMWARN_LIMIT("promotion failed; couldn't update on disk metadata");
  656. policy_remove_mapping(cache->policy, mg->new_oblock);
  657. cleanup_migration(mg);
  658. return;
  659. }
  660. }
  661. spin_lock_irqsave(&cache->lock, flags);
  662. list_add_tail(&mg->list, &cache->need_commit_migrations);
  663. cache->commit_requested = true;
  664. spin_unlock_irqrestore(&cache->lock, flags);
  665. }
  666. static void migration_success_post_commit(struct dm_cache_migration *mg)
  667. {
  668. unsigned long flags;
  669. struct cache *cache = mg->cache;
  670. if (mg->writeback) {
  671. DMWARN("writeback unexpectedly triggered commit");
  672. return;
  673. } else if (mg->demote) {
  674. cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
  675. if (mg->promote) {
  676. mg->demote = false;
  677. spin_lock_irqsave(&cache->lock, flags);
  678. list_add_tail(&mg->list, &cache->quiesced_migrations);
  679. spin_unlock_irqrestore(&cache->lock, flags);
  680. } else
  681. cleanup_migration(mg);
  682. } else {
  683. cell_defer(cache, mg->new_ocell, true);
  684. clear_dirty(cache, mg->new_oblock, mg->cblock);
  685. cleanup_migration(mg);
  686. }
  687. }
  688. static void copy_complete(int read_err, unsigned long write_err, void *context)
  689. {
  690. unsigned long flags;
  691. struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
  692. struct cache *cache = mg->cache;
  693. if (read_err || write_err)
  694. mg->err = true;
  695. spin_lock_irqsave(&cache->lock, flags);
  696. list_add_tail(&mg->list, &cache->completed_migrations);
  697. spin_unlock_irqrestore(&cache->lock, flags);
  698. wake_worker(cache);
  699. }
  700. static void issue_copy_real(struct dm_cache_migration *mg)
  701. {
  702. int r;
  703. struct dm_io_region o_region, c_region;
  704. struct cache *cache = mg->cache;
  705. o_region.bdev = cache->origin_dev->bdev;
  706. o_region.count = cache->sectors_per_block;
  707. c_region.bdev = cache->cache_dev->bdev;
  708. c_region.sector = from_cblock(mg->cblock) * cache->sectors_per_block;
  709. c_region.count = cache->sectors_per_block;
  710. if (mg->writeback || mg->demote) {
  711. /* demote */
  712. o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
  713. r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
  714. } else {
  715. /* promote */
  716. o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
  717. r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
  718. }
  719. if (r < 0) {
  720. DMERR_LIMIT("issuing migration failed");
  721. migration_failure(mg);
  722. }
  723. }
  724. static void avoid_copy(struct dm_cache_migration *mg)
  725. {
  726. atomic_inc(&mg->cache->stats.copies_avoided);
  727. migration_success_pre_commit(mg);
  728. }
  729. static void issue_copy(struct dm_cache_migration *mg)
  730. {
  731. bool avoid;
  732. struct cache *cache = mg->cache;
  733. if (mg->writeback || mg->demote)
  734. avoid = !is_dirty(cache, mg->cblock) ||
  735. is_discarded_oblock(cache, mg->old_oblock);
  736. else
  737. avoid = is_discarded_oblock(cache, mg->new_oblock);
  738. avoid ? avoid_copy(mg) : issue_copy_real(mg);
  739. }
  740. static void complete_migration(struct dm_cache_migration *mg)
  741. {
  742. if (mg->err)
  743. migration_failure(mg);
  744. else
  745. migration_success_pre_commit(mg);
  746. }
  747. static void process_migrations(struct cache *cache, struct list_head *head,
  748. void (*fn)(struct dm_cache_migration *))
  749. {
  750. unsigned long flags;
  751. struct list_head list;
  752. struct dm_cache_migration *mg, *tmp;
  753. INIT_LIST_HEAD(&list);
  754. spin_lock_irqsave(&cache->lock, flags);
  755. list_splice_init(head, &list);
  756. spin_unlock_irqrestore(&cache->lock, flags);
  757. list_for_each_entry_safe(mg, tmp, &list, list)
  758. fn(mg);
  759. }
  760. static void __queue_quiesced_migration(struct dm_cache_migration *mg)
  761. {
  762. list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
  763. }
  764. static void queue_quiesced_migration(struct dm_cache_migration *mg)
  765. {
  766. unsigned long flags;
  767. struct cache *cache = mg->cache;
  768. spin_lock_irqsave(&cache->lock, flags);
  769. __queue_quiesced_migration(mg);
  770. spin_unlock_irqrestore(&cache->lock, flags);
  771. wake_worker(cache);
  772. }
  773. static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
  774. {
  775. unsigned long flags;
  776. struct dm_cache_migration *mg, *tmp;
  777. spin_lock_irqsave(&cache->lock, flags);
  778. list_for_each_entry_safe(mg, tmp, work, list)
  779. __queue_quiesced_migration(mg);
  780. spin_unlock_irqrestore(&cache->lock, flags);
  781. wake_worker(cache);
  782. }
  783. static void check_for_quiesced_migrations(struct cache *cache,
  784. struct per_bio_data *pb)
  785. {
  786. struct list_head work;
  787. if (!pb->all_io_entry)
  788. return;
  789. INIT_LIST_HEAD(&work);
  790. if (pb->all_io_entry)
  791. dm_deferred_entry_dec(pb->all_io_entry, &work);
  792. if (!list_empty(&work))
  793. queue_quiesced_migrations(cache, &work);
  794. }
  795. static void quiesce_migration(struct dm_cache_migration *mg)
  796. {
  797. if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
  798. queue_quiesced_migration(mg);
  799. }
  800. static void promote(struct cache *cache, struct prealloc *structs,
  801. dm_oblock_t oblock, dm_cblock_t cblock,
  802. struct dm_bio_prison_cell *cell)
  803. {
  804. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  805. mg->err = false;
  806. mg->writeback = false;
  807. mg->demote = false;
  808. mg->promote = true;
  809. mg->cache = cache;
  810. mg->new_oblock = oblock;
  811. mg->cblock = cblock;
  812. mg->old_ocell = NULL;
  813. mg->new_ocell = cell;
  814. mg->start_jiffies = jiffies;
  815. inc_nr_migrations(cache);
  816. quiesce_migration(mg);
  817. }
  818. static void writeback(struct cache *cache, struct prealloc *structs,
  819. dm_oblock_t oblock, dm_cblock_t cblock,
  820. struct dm_bio_prison_cell *cell)
  821. {
  822. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  823. mg->err = false;
  824. mg->writeback = true;
  825. mg->demote = false;
  826. mg->promote = false;
  827. mg->cache = cache;
  828. mg->old_oblock = oblock;
  829. mg->cblock = cblock;
  830. mg->old_ocell = cell;
  831. mg->new_ocell = NULL;
  832. mg->start_jiffies = jiffies;
  833. inc_nr_migrations(cache);
  834. quiesce_migration(mg);
  835. }
  836. static void demote_then_promote(struct cache *cache, struct prealloc *structs,
  837. dm_oblock_t old_oblock, dm_oblock_t new_oblock,
  838. dm_cblock_t cblock,
  839. struct dm_bio_prison_cell *old_ocell,
  840. struct dm_bio_prison_cell *new_ocell)
  841. {
  842. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  843. mg->err = false;
  844. mg->writeback = false;
  845. mg->demote = true;
  846. mg->promote = true;
  847. mg->cache = cache;
  848. mg->old_oblock = old_oblock;
  849. mg->new_oblock = new_oblock;
  850. mg->cblock = cblock;
  851. mg->old_ocell = old_ocell;
  852. mg->new_ocell = new_ocell;
  853. mg->start_jiffies = jiffies;
  854. inc_nr_migrations(cache);
  855. quiesce_migration(mg);
  856. }
  857. /*----------------------------------------------------------------
  858. * bio processing
  859. *--------------------------------------------------------------*/
  860. static void defer_bio(struct cache *cache, struct bio *bio)
  861. {
  862. unsigned long flags;
  863. spin_lock_irqsave(&cache->lock, flags);
  864. bio_list_add(&cache->deferred_bios, bio);
  865. spin_unlock_irqrestore(&cache->lock, flags);
  866. wake_worker(cache);
  867. }
  868. static void process_flush_bio(struct cache *cache, struct bio *bio)
  869. {
  870. size_t pb_data_size = get_per_bio_data_size(cache);
  871. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  872. BUG_ON(bio->bi_size);
  873. if (!pb->req_nr)
  874. remap_to_origin(cache, bio);
  875. else
  876. remap_to_cache(cache, bio, 0);
  877. issue(cache, bio);
  878. }
  879. /*
  880. * People generally discard large parts of a device, eg, the whole device
  881. * when formatting. Splitting these large discards up into cache block
  882. * sized ios and then quiescing (always neccessary for discard) takes too
  883. * long.
  884. *
  885. * We keep it simple, and allow any size of discard to come in, and just
  886. * mark off blocks on the discard bitset. No passdown occurs!
  887. *
  888. * To implement passdown we need to change the bio_prison such that a cell
  889. * can have a key that spans many blocks.
  890. */
  891. static void process_discard_bio(struct cache *cache, struct bio *bio)
  892. {
  893. dm_block_t start_block = dm_sector_div_up(bio->bi_sector,
  894. cache->discard_block_size);
  895. dm_block_t end_block = bio->bi_sector + bio_sectors(bio);
  896. dm_block_t b;
  897. end_block = block_div(end_block, cache->discard_block_size);
  898. for (b = start_block; b < end_block; b++)
  899. set_discard(cache, to_dblock(b));
  900. bio_endio(bio, 0);
  901. }
  902. static bool spare_migration_bandwidth(struct cache *cache)
  903. {
  904. sector_t current_volume = (atomic_read(&cache->nr_migrations) + 1) *
  905. cache->sectors_per_block;
  906. return current_volume < cache->migration_threshold;
  907. }
  908. static bool is_writethrough_io(struct cache *cache, struct bio *bio,
  909. dm_cblock_t cblock)
  910. {
  911. return bio_data_dir(bio) == WRITE &&
  912. cache->features.write_through && !is_dirty(cache, cblock);
  913. }
  914. static void inc_hit_counter(struct cache *cache, struct bio *bio)
  915. {
  916. atomic_inc(bio_data_dir(bio) == READ ?
  917. &cache->stats.read_hit : &cache->stats.write_hit);
  918. }
  919. static void inc_miss_counter(struct cache *cache, struct bio *bio)
  920. {
  921. atomic_inc(bio_data_dir(bio) == READ ?
  922. &cache->stats.read_miss : &cache->stats.write_miss);
  923. }
  924. static void process_bio(struct cache *cache, struct prealloc *structs,
  925. struct bio *bio)
  926. {
  927. int r;
  928. bool release_cell = true;
  929. dm_oblock_t block = get_bio_block(cache, bio);
  930. struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell;
  931. struct policy_result lookup_result;
  932. size_t pb_data_size = get_per_bio_data_size(cache);
  933. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  934. bool discarded_block = is_discarded_oblock(cache, block);
  935. bool can_migrate = discarded_block || spare_migration_bandwidth(cache);
  936. /*
  937. * Check to see if that block is currently migrating.
  938. */
  939. cell_prealloc = prealloc_get_cell(structs);
  940. r = bio_detain(cache, block, bio, cell_prealloc,
  941. (cell_free_fn) prealloc_put_cell,
  942. structs, &new_ocell);
  943. if (r > 0)
  944. return;
  945. r = policy_map(cache->policy, block, true, can_migrate, discarded_block,
  946. bio, &lookup_result);
  947. if (r == -EWOULDBLOCK)
  948. /* migration has been denied */
  949. lookup_result.op = POLICY_MISS;
  950. switch (lookup_result.op) {
  951. case POLICY_HIT:
  952. inc_hit_counter(cache, bio);
  953. pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
  954. if (is_writethrough_io(cache, bio, lookup_result.cblock))
  955. remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
  956. else
  957. remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
  958. issue(cache, bio);
  959. break;
  960. case POLICY_MISS:
  961. inc_miss_counter(cache, bio);
  962. pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
  963. remap_to_origin_clear_discard(cache, bio, block);
  964. issue(cache, bio);
  965. break;
  966. case POLICY_NEW:
  967. atomic_inc(&cache->stats.promotion);
  968. promote(cache, structs, block, lookup_result.cblock, new_ocell);
  969. release_cell = false;
  970. break;
  971. case POLICY_REPLACE:
  972. cell_prealloc = prealloc_get_cell(structs);
  973. r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc,
  974. (cell_free_fn) prealloc_put_cell,
  975. structs, &old_ocell);
  976. if (r > 0) {
  977. /*
  978. * We have to be careful to avoid lock inversion of
  979. * the cells. So we back off, and wait for the
  980. * old_ocell to become free.
  981. */
  982. policy_force_mapping(cache->policy, block,
  983. lookup_result.old_oblock);
  984. atomic_inc(&cache->stats.cache_cell_clash);
  985. break;
  986. }
  987. atomic_inc(&cache->stats.demotion);
  988. atomic_inc(&cache->stats.promotion);
  989. demote_then_promote(cache, structs, lookup_result.old_oblock,
  990. block, lookup_result.cblock,
  991. old_ocell, new_ocell);
  992. release_cell = false;
  993. break;
  994. default:
  995. DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__,
  996. (unsigned) lookup_result.op);
  997. bio_io_error(bio);
  998. }
  999. if (release_cell)
  1000. cell_defer(cache, new_ocell, false);
  1001. }
  1002. static int need_commit_due_to_time(struct cache *cache)
  1003. {
  1004. return jiffies < cache->last_commit_jiffies ||
  1005. jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
  1006. }
  1007. static int commit_if_needed(struct cache *cache)
  1008. {
  1009. if (dm_cache_changed_this_transaction(cache->cmd) &&
  1010. (cache->commit_requested || need_commit_due_to_time(cache))) {
  1011. atomic_inc(&cache->stats.commit_count);
  1012. cache->last_commit_jiffies = jiffies;
  1013. cache->commit_requested = false;
  1014. return dm_cache_commit(cache->cmd, false);
  1015. }
  1016. return 0;
  1017. }
  1018. static void process_deferred_bios(struct cache *cache)
  1019. {
  1020. unsigned long flags;
  1021. struct bio_list bios;
  1022. struct bio *bio;
  1023. struct prealloc structs;
  1024. memset(&structs, 0, sizeof(structs));
  1025. bio_list_init(&bios);
  1026. spin_lock_irqsave(&cache->lock, flags);
  1027. bio_list_merge(&bios, &cache->deferred_bios);
  1028. bio_list_init(&cache->deferred_bios);
  1029. spin_unlock_irqrestore(&cache->lock, flags);
  1030. while (!bio_list_empty(&bios)) {
  1031. /*
  1032. * If we've got no free migration structs, and processing
  1033. * this bio might require one, we pause until there are some
  1034. * prepared mappings to process.
  1035. */
  1036. if (prealloc_data_structs(cache, &structs)) {
  1037. spin_lock_irqsave(&cache->lock, flags);
  1038. bio_list_merge(&cache->deferred_bios, &bios);
  1039. spin_unlock_irqrestore(&cache->lock, flags);
  1040. break;
  1041. }
  1042. bio = bio_list_pop(&bios);
  1043. if (bio->bi_rw & REQ_FLUSH)
  1044. process_flush_bio(cache, bio);
  1045. else if (bio->bi_rw & REQ_DISCARD)
  1046. process_discard_bio(cache, bio);
  1047. else
  1048. process_bio(cache, &structs, bio);
  1049. }
  1050. prealloc_free_structs(cache, &structs);
  1051. }
  1052. static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
  1053. {
  1054. unsigned long flags;
  1055. struct bio_list bios;
  1056. struct bio *bio;
  1057. bio_list_init(&bios);
  1058. spin_lock_irqsave(&cache->lock, flags);
  1059. bio_list_merge(&bios, &cache->deferred_flush_bios);
  1060. bio_list_init(&cache->deferred_flush_bios);
  1061. spin_unlock_irqrestore(&cache->lock, flags);
  1062. while ((bio = bio_list_pop(&bios)))
  1063. submit_bios ? generic_make_request(bio) : bio_io_error(bio);
  1064. }
  1065. static void process_deferred_writethrough_bios(struct cache *cache)
  1066. {
  1067. unsigned long flags;
  1068. struct bio_list bios;
  1069. struct bio *bio;
  1070. bio_list_init(&bios);
  1071. spin_lock_irqsave(&cache->lock, flags);
  1072. bio_list_merge(&bios, &cache->deferred_writethrough_bios);
  1073. bio_list_init(&cache->deferred_writethrough_bios);
  1074. spin_unlock_irqrestore(&cache->lock, flags);
  1075. while ((bio = bio_list_pop(&bios)))
  1076. generic_make_request(bio);
  1077. }
  1078. static void writeback_some_dirty_blocks(struct cache *cache)
  1079. {
  1080. int r = 0;
  1081. dm_oblock_t oblock;
  1082. dm_cblock_t cblock;
  1083. struct prealloc structs;
  1084. struct dm_bio_prison_cell *old_ocell;
  1085. memset(&structs, 0, sizeof(structs));
  1086. while (spare_migration_bandwidth(cache)) {
  1087. if (prealloc_data_structs(cache, &structs))
  1088. break;
  1089. r = policy_writeback_work(cache->policy, &oblock, &cblock);
  1090. if (r)
  1091. break;
  1092. r = get_cell(cache, oblock, &structs, &old_ocell);
  1093. if (r) {
  1094. policy_set_dirty(cache->policy, oblock);
  1095. break;
  1096. }
  1097. writeback(cache, &structs, oblock, cblock, old_ocell);
  1098. }
  1099. prealloc_free_structs(cache, &structs);
  1100. }
  1101. /*----------------------------------------------------------------
  1102. * Main worker loop
  1103. *--------------------------------------------------------------*/
  1104. static bool is_quiescing(struct cache *cache)
  1105. {
  1106. return atomic_read(&cache->quiescing);
  1107. }
  1108. static void ack_quiescing(struct cache *cache)
  1109. {
  1110. if (is_quiescing(cache)) {
  1111. atomic_inc(&cache->quiescing_ack);
  1112. wake_up(&cache->quiescing_wait);
  1113. }
  1114. }
  1115. static void wait_for_quiescing_ack(struct cache *cache)
  1116. {
  1117. wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
  1118. }
  1119. static void start_quiescing(struct cache *cache)
  1120. {
  1121. atomic_inc(&cache->quiescing);
  1122. wait_for_quiescing_ack(cache);
  1123. }
  1124. static void stop_quiescing(struct cache *cache)
  1125. {
  1126. atomic_set(&cache->quiescing, 0);
  1127. atomic_set(&cache->quiescing_ack, 0);
  1128. }
  1129. static void wait_for_migrations(struct cache *cache)
  1130. {
  1131. wait_event(cache->migration_wait, !atomic_read(&cache->nr_migrations));
  1132. }
  1133. static void stop_worker(struct cache *cache)
  1134. {
  1135. cancel_delayed_work(&cache->waker);
  1136. flush_workqueue(cache->wq);
  1137. }
  1138. static void requeue_deferred_io(struct cache *cache)
  1139. {
  1140. struct bio *bio;
  1141. struct bio_list bios;
  1142. bio_list_init(&bios);
  1143. bio_list_merge(&bios, &cache->deferred_bios);
  1144. bio_list_init(&cache->deferred_bios);
  1145. while ((bio = bio_list_pop(&bios)))
  1146. bio_endio(bio, DM_ENDIO_REQUEUE);
  1147. }
  1148. static int more_work(struct cache *cache)
  1149. {
  1150. if (is_quiescing(cache))
  1151. return !list_empty(&cache->quiesced_migrations) ||
  1152. !list_empty(&cache->completed_migrations) ||
  1153. !list_empty(&cache->need_commit_migrations);
  1154. else
  1155. return !bio_list_empty(&cache->deferred_bios) ||
  1156. !bio_list_empty(&cache->deferred_flush_bios) ||
  1157. !bio_list_empty(&cache->deferred_writethrough_bios) ||
  1158. !list_empty(&cache->quiesced_migrations) ||
  1159. !list_empty(&cache->completed_migrations) ||
  1160. !list_empty(&cache->need_commit_migrations);
  1161. }
  1162. static void do_worker(struct work_struct *ws)
  1163. {
  1164. struct cache *cache = container_of(ws, struct cache, worker);
  1165. do {
  1166. if (!is_quiescing(cache)) {
  1167. writeback_some_dirty_blocks(cache);
  1168. process_deferred_writethrough_bios(cache);
  1169. process_deferred_bios(cache);
  1170. }
  1171. process_migrations(cache, &cache->quiesced_migrations, issue_copy);
  1172. process_migrations(cache, &cache->completed_migrations, complete_migration);
  1173. if (commit_if_needed(cache)) {
  1174. process_deferred_flush_bios(cache, false);
  1175. /*
  1176. * FIXME: rollback metadata or just go into a
  1177. * failure mode and error everything
  1178. */
  1179. } else {
  1180. process_deferred_flush_bios(cache, true);
  1181. process_migrations(cache, &cache->need_commit_migrations,
  1182. migration_success_post_commit);
  1183. }
  1184. ack_quiescing(cache);
  1185. } while (more_work(cache));
  1186. }
  1187. /*
  1188. * We want to commit periodically so that not too much
  1189. * unwritten metadata builds up.
  1190. */
  1191. static void do_waker(struct work_struct *ws)
  1192. {
  1193. struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
  1194. policy_tick(cache->policy);
  1195. wake_worker(cache);
  1196. queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
  1197. }
  1198. /*----------------------------------------------------------------*/
  1199. static int is_congested(struct dm_dev *dev, int bdi_bits)
  1200. {
  1201. struct request_queue *q = bdev_get_queue(dev->bdev);
  1202. return bdi_congested(&q->backing_dev_info, bdi_bits);
  1203. }
  1204. static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
  1205. {
  1206. struct cache *cache = container_of(cb, struct cache, callbacks);
  1207. return is_congested(cache->origin_dev, bdi_bits) ||
  1208. is_congested(cache->cache_dev, bdi_bits);
  1209. }
  1210. /*----------------------------------------------------------------
  1211. * Target methods
  1212. *--------------------------------------------------------------*/
  1213. /*
  1214. * This function gets called on the error paths of the constructor, so we
  1215. * have to cope with a partially initialised struct.
  1216. */
  1217. static void destroy(struct cache *cache)
  1218. {
  1219. unsigned i;
  1220. if (cache->next_migration)
  1221. mempool_free(cache->next_migration, cache->migration_pool);
  1222. if (cache->migration_pool)
  1223. mempool_destroy(cache->migration_pool);
  1224. if (cache->all_io_ds)
  1225. dm_deferred_set_destroy(cache->all_io_ds);
  1226. if (cache->prison)
  1227. dm_bio_prison_destroy(cache->prison);
  1228. if (cache->wq)
  1229. destroy_workqueue(cache->wq);
  1230. if (cache->dirty_bitset)
  1231. free_bitset(cache->dirty_bitset);
  1232. if (cache->discard_bitset)
  1233. free_bitset(cache->discard_bitset);
  1234. if (cache->copier)
  1235. dm_kcopyd_client_destroy(cache->copier);
  1236. if (cache->cmd)
  1237. dm_cache_metadata_close(cache->cmd);
  1238. if (cache->metadata_dev)
  1239. dm_put_device(cache->ti, cache->metadata_dev);
  1240. if (cache->origin_dev)
  1241. dm_put_device(cache->ti, cache->origin_dev);
  1242. if (cache->cache_dev)
  1243. dm_put_device(cache->ti, cache->cache_dev);
  1244. if (cache->policy)
  1245. dm_cache_policy_destroy(cache->policy);
  1246. for (i = 0; i < cache->nr_ctr_args ; i++)
  1247. kfree(cache->ctr_args[i]);
  1248. kfree(cache->ctr_args);
  1249. kfree(cache);
  1250. }
  1251. static void cache_dtr(struct dm_target *ti)
  1252. {
  1253. struct cache *cache = ti->private;
  1254. destroy(cache);
  1255. }
  1256. static sector_t get_dev_size(struct dm_dev *dev)
  1257. {
  1258. return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
  1259. }
  1260. /*----------------------------------------------------------------*/
  1261. /*
  1262. * Construct a cache device mapping.
  1263. *
  1264. * cache <metadata dev> <cache dev> <origin dev> <block size>
  1265. * <#feature args> [<feature arg>]*
  1266. * <policy> <#policy args> [<policy arg>]*
  1267. *
  1268. * metadata dev : fast device holding the persistent metadata
  1269. * cache dev : fast device holding cached data blocks
  1270. * origin dev : slow device holding original data blocks
  1271. * block size : cache unit size in sectors
  1272. *
  1273. * #feature args : number of feature arguments passed
  1274. * feature args : writethrough. (The default is writeback.)
  1275. *
  1276. * policy : the replacement policy to use
  1277. * #policy args : an even number of policy arguments corresponding
  1278. * to key/value pairs passed to the policy
  1279. * policy args : key/value pairs passed to the policy
  1280. * E.g. 'sequential_threshold 1024'
  1281. * See cache-policies.txt for details.
  1282. *
  1283. * Optional feature arguments are:
  1284. * writethrough : write through caching that prohibits cache block
  1285. * content from being different from origin block content.
  1286. * Without this argument, the default behaviour is to write
  1287. * back cache block contents later for performance reasons,
  1288. * so they may differ from the corresponding origin blocks.
  1289. */
  1290. struct cache_args {
  1291. struct dm_target *ti;
  1292. struct dm_dev *metadata_dev;
  1293. struct dm_dev *cache_dev;
  1294. sector_t cache_sectors;
  1295. struct dm_dev *origin_dev;
  1296. sector_t origin_sectors;
  1297. uint32_t block_size;
  1298. const char *policy_name;
  1299. int policy_argc;
  1300. const char **policy_argv;
  1301. struct cache_features features;
  1302. };
  1303. static void destroy_cache_args(struct cache_args *ca)
  1304. {
  1305. if (ca->metadata_dev)
  1306. dm_put_device(ca->ti, ca->metadata_dev);
  1307. if (ca->cache_dev)
  1308. dm_put_device(ca->ti, ca->cache_dev);
  1309. if (ca->origin_dev)
  1310. dm_put_device(ca->ti, ca->origin_dev);
  1311. kfree(ca);
  1312. }
  1313. static bool at_least_one_arg(struct dm_arg_set *as, char **error)
  1314. {
  1315. if (!as->argc) {
  1316. *error = "Insufficient args";
  1317. return false;
  1318. }
  1319. return true;
  1320. }
  1321. static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
  1322. char **error)
  1323. {
  1324. int r;
  1325. sector_t metadata_dev_size;
  1326. char b[BDEVNAME_SIZE];
  1327. if (!at_least_one_arg(as, error))
  1328. return -EINVAL;
  1329. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  1330. &ca->metadata_dev);
  1331. if (r) {
  1332. *error = "Error opening metadata device";
  1333. return r;
  1334. }
  1335. metadata_dev_size = get_dev_size(ca->metadata_dev);
  1336. if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
  1337. DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
  1338. bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
  1339. return 0;
  1340. }
  1341. static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
  1342. char **error)
  1343. {
  1344. int r;
  1345. if (!at_least_one_arg(as, error))
  1346. return -EINVAL;
  1347. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  1348. &ca->cache_dev);
  1349. if (r) {
  1350. *error = "Error opening cache device";
  1351. return r;
  1352. }
  1353. ca->cache_sectors = get_dev_size(ca->cache_dev);
  1354. return 0;
  1355. }
  1356. static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
  1357. char **error)
  1358. {
  1359. int r;
  1360. if (!at_least_one_arg(as, error))
  1361. return -EINVAL;
  1362. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  1363. &ca->origin_dev);
  1364. if (r) {
  1365. *error = "Error opening origin device";
  1366. return r;
  1367. }
  1368. ca->origin_sectors = get_dev_size(ca->origin_dev);
  1369. if (ca->ti->len > ca->origin_sectors) {
  1370. *error = "Device size larger than cached device";
  1371. return -EINVAL;
  1372. }
  1373. return 0;
  1374. }
  1375. static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
  1376. char **error)
  1377. {
  1378. unsigned long block_size;
  1379. if (!at_least_one_arg(as, error))
  1380. return -EINVAL;
  1381. if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
  1382. block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
  1383. block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
  1384. block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
  1385. *error = "Invalid data block size";
  1386. return -EINVAL;
  1387. }
  1388. if (block_size > ca->cache_sectors) {
  1389. *error = "Data block size is larger than the cache device";
  1390. return -EINVAL;
  1391. }
  1392. ca->block_size = block_size;
  1393. return 0;
  1394. }
  1395. static void init_features(struct cache_features *cf)
  1396. {
  1397. cf->mode = CM_WRITE;
  1398. cf->write_through = false;
  1399. }
  1400. static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
  1401. char **error)
  1402. {
  1403. static struct dm_arg _args[] = {
  1404. {0, 1, "Invalid number of cache feature arguments"},
  1405. };
  1406. int r;
  1407. unsigned argc;
  1408. const char *arg;
  1409. struct cache_features *cf = &ca->features;
  1410. init_features(cf);
  1411. r = dm_read_arg_group(_args, as, &argc, error);
  1412. if (r)
  1413. return -EINVAL;
  1414. while (argc--) {
  1415. arg = dm_shift_arg(as);
  1416. if (!strcasecmp(arg, "writeback"))
  1417. cf->write_through = false;
  1418. else if (!strcasecmp(arg, "writethrough"))
  1419. cf->write_through = true;
  1420. else {
  1421. *error = "Unrecognised cache feature requested";
  1422. return -EINVAL;
  1423. }
  1424. }
  1425. return 0;
  1426. }
  1427. static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
  1428. char **error)
  1429. {
  1430. static struct dm_arg _args[] = {
  1431. {0, 1024, "Invalid number of policy arguments"},
  1432. };
  1433. int r;
  1434. if (!at_least_one_arg(as, error))
  1435. return -EINVAL;
  1436. ca->policy_name = dm_shift_arg(as);
  1437. r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
  1438. if (r)
  1439. return -EINVAL;
  1440. ca->policy_argv = (const char **)as->argv;
  1441. dm_consume_args(as, ca->policy_argc);
  1442. return 0;
  1443. }
  1444. static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
  1445. char **error)
  1446. {
  1447. int r;
  1448. struct dm_arg_set as;
  1449. as.argc = argc;
  1450. as.argv = argv;
  1451. r = parse_metadata_dev(ca, &as, error);
  1452. if (r)
  1453. return r;
  1454. r = parse_cache_dev(ca, &as, error);
  1455. if (r)
  1456. return r;
  1457. r = parse_origin_dev(ca, &as, error);
  1458. if (r)
  1459. return r;
  1460. r = parse_block_size(ca, &as, error);
  1461. if (r)
  1462. return r;
  1463. r = parse_features(ca, &as, error);
  1464. if (r)
  1465. return r;
  1466. r = parse_policy(ca, &as, error);
  1467. if (r)
  1468. return r;
  1469. return 0;
  1470. }
  1471. /*----------------------------------------------------------------*/
  1472. static struct kmem_cache *migration_cache;
  1473. #define NOT_CORE_OPTION 1
  1474. static int process_config_option(struct cache *cache, const char *key, const char *value)
  1475. {
  1476. unsigned long tmp;
  1477. if (!strcasecmp(key, "migration_threshold")) {
  1478. if (kstrtoul(value, 10, &tmp))
  1479. return -EINVAL;
  1480. cache->migration_threshold = tmp;
  1481. return 0;
  1482. }
  1483. return NOT_CORE_OPTION;
  1484. }
  1485. static int set_config_value(struct cache *cache, const char *key, const char *value)
  1486. {
  1487. int r = process_config_option(cache, key, value);
  1488. if (r == NOT_CORE_OPTION)
  1489. r = policy_set_config_value(cache->policy, key, value);
  1490. if (r)
  1491. DMWARN("bad config value for %s: %s", key, value);
  1492. return r;
  1493. }
  1494. static int set_config_values(struct cache *cache, int argc, const char **argv)
  1495. {
  1496. int r = 0;
  1497. if (argc & 1) {
  1498. DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
  1499. return -EINVAL;
  1500. }
  1501. while (argc) {
  1502. r = set_config_value(cache, argv[0], argv[1]);
  1503. if (r)
  1504. break;
  1505. argc -= 2;
  1506. argv += 2;
  1507. }
  1508. return r;
  1509. }
  1510. static int create_cache_policy(struct cache *cache, struct cache_args *ca,
  1511. char **error)
  1512. {
  1513. struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
  1514. cache->cache_size,
  1515. cache->origin_sectors,
  1516. cache->sectors_per_block);
  1517. if (IS_ERR(p)) {
  1518. *error = "Error creating cache's policy";
  1519. return PTR_ERR(p);
  1520. }
  1521. cache->policy = p;
  1522. return 0;
  1523. }
  1524. /*
  1525. * We want the discard block size to be a power of two, at least the size
  1526. * of the cache block size, and have no more than 2^14 discard blocks
  1527. * across the origin.
  1528. */
  1529. #define MAX_DISCARD_BLOCKS (1 << 14)
  1530. static bool too_many_discard_blocks(sector_t discard_block_size,
  1531. sector_t origin_size)
  1532. {
  1533. (void) sector_div(origin_size, discard_block_size);
  1534. return origin_size > MAX_DISCARD_BLOCKS;
  1535. }
  1536. static sector_t calculate_discard_block_size(sector_t cache_block_size,
  1537. sector_t origin_size)
  1538. {
  1539. sector_t discard_block_size;
  1540. discard_block_size = roundup_pow_of_two(cache_block_size);
  1541. if (origin_size)
  1542. while (too_many_discard_blocks(discard_block_size, origin_size))
  1543. discard_block_size *= 2;
  1544. return discard_block_size;
  1545. }
  1546. #define DEFAULT_MIGRATION_THRESHOLD 2048
  1547. static int cache_create(struct cache_args *ca, struct cache **result)
  1548. {
  1549. int r = 0;
  1550. char **error = &ca->ti->error;
  1551. struct cache *cache;
  1552. struct dm_target *ti = ca->ti;
  1553. dm_block_t origin_blocks;
  1554. struct dm_cache_metadata *cmd;
  1555. bool may_format = ca->features.mode == CM_WRITE;
  1556. cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  1557. if (!cache)
  1558. return -ENOMEM;
  1559. cache->ti = ca->ti;
  1560. ti->private = cache;
  1561. ti->num_flush_bios = 2;
  1562. ti->flush_supported = true;
  1563. ti->num_discard_bios = 1;
  1564. ti->discards_supported = true;
  1565. ti->discard_zeroes_data_unsupported = true;
  1566. cache->features = ca->features;
  1567. ti->per_bio_data_size = get_per_bio_data_size(cache);
  1568. cache->callbacks.congested_fn = cache_is_congested;
  1569. dm_table_add_target_callbacks(ti->table, &cache->callbacks);
  1570. cache->metadata_dev = ca->metadata_dev;
  1571. cache->origin_dev = ca->origin_dev;
  1572. cache->cache_dev = ca->cache_dev;
  1573. ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
  1574. /* FIXME: factor out this whole section */
  1575. origin_blocks = cache->origin_sectors = ca->origin_sectors;
  1576. origin_blocks = block_div(origin_blocks, ca->block_size);
  1577. cache->origin_blocks = to_oblock(origin_blocks);
  1578. cache->sectors_per_block = ca->block_size;
  1579. if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
  1580. r = -EINVAL;
  1581. goto bad;
  1582. }
  1583. if (ca->block_size & (ca->block_size - 1)) {
  1584. dm_block_t cache_size = ca->cache_sectors;
  1585. cache->sectors_per_block_shift = -1;
  1586. cache_size = block_div(cache_size, ca->block_size);
  1587. cache->cache_size = to_cblock(cache_size);
  1588. } else {
  1589. cache->sectors_per_block_shift = __ffs(ca->block_size);
  1590. cache->cache_size = to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift);
  1591. }
  1592. r = create_cache_policy(cache, ca, error);
  1593. if (r)
  1594. goto bad;
  1595. cache->policy_nr_args = ca->policy_argc;
  1596. cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
  1597. r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
  1598. if (r) {
  1599. *error = "Error setting cache policy's config values";
  1600. goto bad;
  1601. }
  1602. cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
  1603. ca->block_size, may_format,
  1604. dm_cache_policy_get_hint_size(cache->policy));
  1605. if (IS_ERR(cmd)) {
  1606. *error = "Error creating metadata object";
  1607. r = PTR_ERR(cmd);
  1608. goto bad;
  1609. }
  1610. cache->cmd = cmd;
  1611. spin_lock_init(&cache->lock);
  1612. bio_list_init(&cache->deferred_bios);
  1613. bio_list_init(&cache->deferred_flush_bios);
  1614. bio_list_init(&cache->deferred_writethrough_bios);
  1615. INIT_LIST_HEAD(&cache->quiesced_migrations);
  1616. INIT_LIST_HEAD(&cache->completed_migrations);
  1617. INIT_LIST_HEAD(&cache->need_commit_migrations);
  1618. atomic_set(&cache->nr_migrations, 0);
  1619. init_waitqueue_head(&cache->migration_wait);
  1620. init_waitqueue_head(&cache->quiescing_wait);
  1621. atomic_set(&cache->quiescing, 0);
  1622. atomic_set(&cache->quiescing_ack, 0);
  1623. r = -ENOMEM;
  1624. cache->nr_dirty = 0;
  1625. cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
  1626. if (!cache->dirty_bitset) {
  1627. *error = "could not allocate dirty bitset";
  1628. goto bad;
  1629. }
  1630. clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
  1631. cache->discard_block_size =
  1632. calculate_discard_block_size(cache->sectors_per_block,
  1633. cache->origin_sectors);
  1634. cache->discard_nr_blocks = oblock_to_dblock(cache, cache->origin_blocks);
  1635. cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
  1636. if (!cache->discard_bitset) {
  1637. *error = "could not allocate discard bitset";
  1638. goto bad;
  1639. }
  1640. clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
  1641. cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  1642. if (IS_ERR(cache->copier)) {
  1643. *error = "could not create kcopyd client";
  1644. r = PTR_ERR(cache->copier);
  1645. goto bad;
  1646. }
  1647. cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
  1648. if (!cache->wq) {
  1649. *error = "could not create workqueue for metadata object";
  1650. goto bad;
  1651. }
  1652. INIT_WORK(&cache->worker, do_worker);
  1653. INIT_DELAYED_WORK(&cache->waker, do_waker);
  1654. cache->last_commit_jiffies = jiffies;
  1655. cache->prison = dm_bio_prison_create(PRISON_CELLS);
  1656. if (!cache->prison) {
  1657. *error = "could not create bio prison";
  1658. goto bad;
  1659. }
  1660. cache->all_io_ds = dm_deferred_set_create();
  1661. if (!cache->all_io_ds) {
  1662. *error = "could not create all_io deferred set";
  1663. goto bad;
  1664. }
  1665. cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
  1666. migration_cache);
  1667. if (!cache->migration_pool) {
  1668. *error = "Error creating cache's migration mempool";
  1669. goto bad;
  1670. }
  1671. cache->next_migration = NULL;
  1672. cache->need_tick_bio = true;
  1673. cache->sized = false;
  1674. cache->commit_requested = false;
  1675. cache->loaded_mappings = false;
  1676. cache->loaded_discards = false;
  1677. load_stats(cache);
  1678. atomic_set(&cache->stats.demotion, 0);
  1679. atomic_set(&cache->stats.promotion, 0);
  1680. atomic_set(&cache->stats.copies_avoided, 0);
  1681. atomic_set(&cache->stats.cache_cell_clash, 0);
  1682. atomic_set(&cache->stats.commit_count, 0);
  1683. atomic_set(&cache->stats.discard_count, 0);
  1684. *result = cache;
  1685. return 0;
  1686. bad:
  1687. destroy(cache);
  1688. return r;
  1689. }
  1690. static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
  1691. {
  1692. unsigned i;
  1693. const char **copy;
  1694. copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
  1695. if (!copy)
  1696. return -ENOMEM;
  1697. for (i = 0; i < argc; i++) {
  1698. copy[i] = kstrdup(argv[i], GFP_KERNEL);
  1699. if (!copy[i]) {
  1700. while (i--)
  1701. kfree(copy[i]);
  1702. kfree(copy);
  1703. return -ENOMEM;
  1704. }
  1705. }
  1706. cache->nr_ctr_args = argc;
  1707. cache->ctr_args = copy;
  1708. return 0;
  1709. }
  1710. static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
  1711. {
  1712. int r = -EINVAL;
  1713. struct cache_args *ca;
  1714. struct cache *cache = NULL;
  1715. ca = kzalloc(sizeof(*ca), GFP_KERNEL);
  1716. if (!ca) {
  1717. ti->error = "Error allocating memory for cache";
  1718. return -ENOMEM;
  1719. }
  1720. ca->ti = ti;
  1721. r = parse_cache_args(ca, argc, argv, &ti->error);
  1722. if (r)
  1723. goto out;
  1724. r = cache_create(ca, &cache);
  1725. if (r)
  1726. goto out;
  1727. r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
  1728. if (r) {
  1729. destroy(cache);
  1730. goto out;
  1731. }
  1732. ti->private = cache;
  1733. out:
  1734. destroy_cache_args(ca);
  1735. return r;
  1736. }
  1737. static int cache_map(struct dm_target *ti, struct bio *bio)
  1738. {
  1739. struct cache *cache = ti->private;
  1740. int r;
  1741. dm_oblock_t block = get_bio_block(cache, bio);
  1742. size_t pb_data_size = get_per_bio_data_size(cache);
  1743. bool can_migrate = false;
  1744. bool discarded_block;
  1745. struct dm_bio_prison_cell *cell;
  1746. struct policy_result lookup_result;
  1747. struct per_bio_data *pb;
  1748. if (from_oblock(block) > from_oblock(cache->origin_blocks)) {
  1749. /*
  1750. * This can only occur if the io goes to a partial block at
  1751. * the end of the origin device. We don't cache these.
  1752. * Just remap to the origin and carry on.
  1753. */
  1754. remap_to_origin_clear_discard(cache, bio, block);
  1755. return DM_MAPIO_REMAPPED;
  1756. }
  1757. pb = init_per_bio_data(bio, pb_data_size);
  1758. if (bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD)) {
  1759. defer_bio(cache, bio);
  1760. return DM_MAPIO_SUBMITTED;
  1761. }
  1762. /*
  1763. * Check to see if that block is currently migrating.
  1764. */
  1765. cell = alloc_prison_cell(cache);
  1766. if (!cell) {
  1767. defer_bio(cache, bio);
  1768. return DM_MAPIO_SUBMITTED;
  1769. }
  1770. r = bio_detain(cache, block, bio, cell,
  1771. (cell_free_fn) free_prison_cell,
  1772. cache, &cell);
  1773. if (r) {
  1774. if (r < 0)
  1775. defer_bio(cache, bio);
  1776. return DM_MAPIO_SUBMITTED;
  1777. }
  1778. discarded_block = is_discarded_oblock(cache, block);
  1779. r = policy_map(cache->policy, block, false, can_migrate, discarded_block,
  1780. bio, &lookup_result);
  1781. if (r == -EWOULDBLOCK) {
  1782. cell_defer(cache, cell, true);
  1783. return DM_MAPIO_SUBMITTED;
  1784. } else if (r) {
  1785. DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r);
  1786. bio_io_error(bio);
  1787. return DM_MAPIO_SUBMITTED;
  1788. }
  1789. switch (lookup_result.op) {
  1790. case POLICY_HIT:
  1791. inc_hit_counter(cache, bio);
  1792. pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
  1793. if (is_writethrough_io(cache, bio, lookup_result.cblock))
  1794. remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
  1795. else
  1796. remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
  1797. cell_defer(cache, cell, false);
  1798. break;
  1799. case POLICY_MISS:
  1800. inc_miss_counter(cache, bio);
  1801. pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
  1802. if (pb->req_nr != 0) {
  1803. /*
  1804. * This is a duplicate writethrough io that is no
  1805. * longer needed because the block has been demoted.
  1806. */
  1807. bio_endio(bio, 0);
  1808. cell_defer(cache, cell, false);
  1809. return DM_MAPIO_SUBMITTED;
  1810. } else {
  1811. remap_to_origin_clear_discard(cache, bio, block);
  1812. cell_defer(cache, cell, false);
  1813. }
  1814. break;
  1815. default:
  1816. DMERR_LIMIT("%s: erroring bio: unknown policy op: %u", __func__,
  1817. (unsigned) lookup_result.op);
  1818. bio_io_error(bio);
  1819. return DM_MAPIO_SUBMITTED;
  1820. }
  1821. return DM_MAPIO_REMAPPED;
  1822. }
  1823. static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
  1824. {
  1825. struct cache *cache = ti->private;
  1826. unsigned long flags;
  1827. size_t pb_data_size = get_per_bio_data_size(cache);
  1828. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  1829. if (pb->tick) {
  1830. policy_tick(cache->policy);
  1831. spin_lock_irqsave(&cache->lock, flags);
  1832. cache->need_tick_bio = true;
  1833. spin_unlock_irqrestore(&cache->lock, flags);
  1834. }
  1835. check_for_quiesced_migrations(cache, pb);
  1836. return 0;
  1837. }
  1838. static int write_dirty_bitset(struct cache *cache)
  1839. {
  1840. unsigned i, r;
  1841. for (i = 0; i < from_cblock(cache->cache_size); i++) {
  1842. r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
  1843. is_dirty(cache, to_cblock(i)));
  1844. if (r)
  1845. return r;
  1846. }
  1847. return 0;
  1848. }
  1849. static int write_discard_bitset(struct cache *cache)
  1850. {
  1851. unsigned i, r;
  1852. r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
  1853. cache->discard_nr_blocks);
  1854. if (r) {
  1855. DMERR("could not resize on-disk discard bitset");
  1856. return r;
  1857. }
  1858. for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
  1859. r = dm_cache_set_discard(cache->cmd, to_dblock(i),
  1860. is_discarded(cache, to_dblock(i)));
  1861. if (r)
  1862. return r;
  1863. }
  1864. return 0;
  1865. }
  1866. static int save_hint(void *context, dm_cblock_t cblock, dm_oblock_t oblock,
  1867. uint32_t hint)
  1868. {
  1869. struct cache *cache = context;
  1870. return dm_cache_save_hint(cache->cmd, cblock, hint);
  1871. }
  1872. static int write_hints(struct cache *cache)
  1873. {
  1874. int r;
  1875. r = dm_cache_begin_hints(cache->cmd, cache->policy);
  1876. if (r) {
  1877. DMERR("dm_cache_begin_hints failed");
  1878. return r;
  1879. }
  1880. r = policy_walk_mappings(cache->policy, save_hint, cache);
  1881. if (r)
  1882. DMERR("policy_walk_mappings failed");
  1883. return r;
  1884. }
  1885. /*
  1886. * returns true on success
  1887. */
  1888. static bool sync_metadata(struct cache *cache)
  1889. {
  1890. int r1, r2, r3, r4;
  1891. r1 = write_dirty_bitset(cache);
  1892. if (r1)
  1893. DMERR("could not write dirty bitset");
  1894. r2 = write_discard_bitset(cache);
  1895. if (r2)
  1896. DMERR("could not write discard bitset");
  1897. save_stats(cache);
  1898. r3 = write_hints(cache);
  1899. if (r3)
  1900. DMERR("could not write hints");
  1901. /*
  1902. * If writing the above metadata failed, we still commit, but don't
  1903. * set the clean shutdown flag. This will effectively force every
  1904. * dirty bit to be set on reload.
  1905. */
  1906. r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3);
  1907. if (r4)
  1908. DMERR("could not write cache metadata. Data loss may occur.");
  1909. return !r1 && !r2 && !r3 && !r4;
  1910. }
  1911. static void cache_postsuspend(struct dm_target *ti)
  1912. {
  1913. struct cache *cache = ti->private;
  1914. start_quiescing(cache);
  1915. wait_for_migrations(cache);
  1916. stop_worker(cache);
  1917. requeue_deferred_io(cache);
  1918. stop_quiescing(cache);
  1919. (void) sync_metadata(cache);
  1920. }
  1921. static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
  1922. bool dirty, uint32_t hint, bool hint_valid)
  1923. {
  1924. int r;
  1925. struct cache *cache = context;
  1926. r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
  1927. if (r)
  1928. return r;
  1929. if (dirty)
  1930. set_dirty(cache, oblock, cblock);
  1931. else
  1932. clear_dirty(cache, oblock, cblock);
  1933. return 0;
  1934. }
  1935. static int load_discard(void *context, sector_t discard_block_size,
  1936. dm_dblock_t dblock, bool discard)
  1937. {
  1938. struct cache *cache = context;
  1939. /* FIXME: handle mis-matched block size */
  1940. if (discard)
  1941. set_discard(cache, dblock);
  1942. else
  1943. clear_discard(cache, dblock);
  1944. return 0;
  1945. }
  1946. static int cache_preresume(struct dm_target *ti)
  1947. {
  1948. int r = 0;
  1949. struct cache *cache = ti->private;
  1950. sector_t actual_cache_size = get_dev_size(cache->cache_dev);
  1951. (void) sector_div(actual_cache_size, cache->sectors_per_block);
  1952. /*
  1953. * Check to see if the cache has resized.
  1954. */
  1955. if (from_cblock(cache->cache_size) != actual_cache_size || !cache->sized) {
  1956. cache->cache_size = to_cblock(actual_cache_size);
  1957. r = dm_cache_resize(cache->cmd, cache->cache_size);
  1958. if (r) {
  1959. DMERR("could not resize cache metadata");
  1960. return r;
  1961. }
  1962. cache->sized = true;
  1963. }
  1964. if (!cache->loaded_mappings) {
  1965. r = dm_cache_load_mappings(cache->cmd, cache->policy,
  1966. load_mapping, cache);
  1967. if (r) {
  1968. DMERR("could not load cache mappings");
  1969. return r;
  1970. }
  1971. cache->loaded_mappings = true;
  1972. }
  1973. if (!cache->loaded_discards) {
  1974. r = dm_cache_load_discards(cache->cmd, load_discard, cache);
  1975. if (r) {
  1976. DMERR("could not load origin discards");
  1977. return r;
  1978. }
  1979. cache->loaded_discards = true;
  1980. }
  1981. return r;
  1982. }
  1983. static void cache_resume(struct dm_target *ti)
  1984. {
  1985. struct cache *cache = ti->private;
  1986. cache->need_tick_bio = true;
  1987. do_waker(&cache->waker.work);
  1988. }
  1989. /*
  1990. * Status format:
  1991. *
  1992. * <#used metadata blocks>/<#total metadata blocks>
  1993. * <#read hits> <#read misses> <#write hits> <#write misses>
  1994. * <#demotions> <#promotions> <#blocks in cache> <#dirty>
  1995. * <#features> <features>*
  1996. * <#core args> <core args>
  1997. * <#policy args> <policy args>*
  1998. */
  1999. static void cache_status(struct dm_target *ti, status_type_t type,
  2000. unsigned status_flags, char *result, unsigned maxlen)
  2001. {
  2002. int r = 0;
  2003. unsigned i;
  2004. ssize_t sz = 0;
  2005. dm_block_t nr_free_blocks_metadata = 0;
  2006. dm_block_t nr_blocks_metadata = 0;
  2007. char buf[BDEVNAME_SIZE];
  2008. struct cache *cache = ti->private;
  2009. dm_cblock_t residency;
  2010. switch (type) {
  2011. case STATUSTYPE_INFO:
  2012. /* Commit to ensure statistics aren't out-of-date */
  2013. if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) {
  2014. r = dm_cache_commit(cache->cmd, false);
  2015. if (r)
  2016. DMERR("could not commit metadata for accurate status");
  2017. }
  2018. r = dm_cache_get_free_metadata_block_count(cache->cmd,
  2019. &nr_free_blocks_metadata);
  2020. if (r) {
  2021. DMERR("could not get metadata free block count");
  2022. goto err;
  2023. }
  2024. r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
  2025. if (r) {
  2026. DMERR("could not get metadata device size");
  2027. goto err;
  2028. }
  2029. residency = policy_residency(cache->policy);
  2030. DMEMIT("%llu/%llu %u %u %u %u %u %u %llu %u ",
  2031. (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
  2032. (unsigned long long)nr_blocks_metadata,
  2033. (unsigned) atomic_read(&cache->stats.read_hit),
  2034. (unsigned) atomic_read(&cache->stats.read_miss),
  2035. (unsigned) atomic_read(&cache->stats.write_hit),
  2036. (unsigned) atomic_read(&cache->stats.write_miss),
  2037. (unsigned) atomic_read(&cache->stats.demotion),
  2038. (unsigned) atomic_read(&cache->stats.promotion),
  2039. (unsigned long long) from_cblock(residency),
  2040. cache->nr_dirty);
  2041. if (cache->features.write_through)
  2042. DMEMIT("1 writethrough ");
  2043. else
  2044. DMEMIT("0 ");
  2045. DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
  2046. if (sz < maxlen) {
  2047. r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz);
  2048. if (r)
  2049. DMERR("policy_emit_config_values returned %d", r);
  2050. }
  2051. break;
  2052. case STATUSTYPE_TABLE:
  2053. format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
  2054. DMEMIT("%s ", buf);
  2055. format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
  2056. DMEMIT("%s ", buf);
  2057. format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
  2058. DMEMIT("%s", buf);
  2059. for (i = 0; i < cache->nr_ctr_args - 1; i++)
  2060. DMEMIT(" %s", cache->ctr_args[i]);
  2061. if (cache->nr_ctr_args)
  2062. DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
  2063. }
  2064. return;
  2065. err:
  2066. DMEMIT("Error");
  2067. }
  2068. /*
  2069. * Supports <key> <value>.
  2070. *
  2071. * The key migration_threshold is supported by the cache target core.
  2072. */
  2073. static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
  2074. {
  2075. struct cache *cache = ti->private;
  2076. if (argc != 2)
  2077. return -EINVAL;
  2078. return set_config_value(cache, argv[0], argv[1]);
  2079. }
  2080. static int cache_iterate_devices(struct dm_target *ti,
  2081. iterate_devices_callout_fn fn, void *data)
  2082. {
  2083. int r = 0;
  2084. struct cache *cache = ti->private;
  2085. r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
  2086. if (!r)
  2087. r = fn(ti, cache->origin_dev, 0, ti->len, data);
  2088. return r;
  2089. }
  2090. /*
  2091. * We assume I/O is going to the origin (which is the volume
  2092. * more likely to have restrictions e.g. by being striped).
  2093. * (Looking up the exact location of the data would be expensive
  2094. * and could always be out of date by the time the bio is submitted.)
  2095. */
  2096. static int cache_bvec_merge(struct dm_target *ti,
  2097. struct bvec_merge_data *bvm,
  2098. struct bio_vec *biovec, int max_size)
  2099. {
  2100. struct cache *cache = ti->private;
  2101. struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev);
  2102. if (!q->merge_bvec_fn)
  2103. return max_size;
  2104. bvm->bi_bdev = cache->origin_dev->bdev;
  2105. return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
  2106. }
  2107. static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
  2108. {
  2109. /*
  2110. * FIXME: these limits may be incompatible with the cache device
  2111. */
  2112. limits->max_discard_sectors = cache->discard_block_size * 1024;
  2113. limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
  2114. }
  2115. static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
  2116. {
  2117. struct cache *cache = ti->private;
  2118. uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
  2119. /*
  2120. * If the system-determined stacked limits are compatible with the
  2121. * cache's blocksize (io_opt is a factor) do not override them.
  2122. */
  2123. if (io_opt_sectors < cache->sectors_per_block ||
  2124. do_div(io_opt_sectors, cache->sectors_per_block)) {
  2125. blk_limits_io_min(limits, 0);
  2126. blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
  2127. }
  2128. set_discard_limits(cache, limits);
  2129. }
  2130. /*----------------------------------------------------------------*/
  2131. static struct target_type cache_target = {
  2132. .name = "cache",
  2133. .version = {1, 1, 1},
  2134. .module = THIS_MODULE,
  2135. .ctr = cache_ctr,
  2136. .dtr = cache_dtr,
  2137. .map = cache_map,
  2138. .end_io = cache_end_io,
  2139. .postsuspend = cache_postsuspend,
  2140. .preresume = cache_preresume,
  2141. .resume = cache_resume,
  2142. .status = cache_status,
  2143. .message = cache_message,
  2144. .iterate_devices = cache_iterate_devices,
  2145. .merge = cache_bvec_merge,
  2146. .io_hints = cache_io_hints,
  2147. };
  2148. static int __init dm_cache_init(void)
  2149. {
  2150. int r;
  2151. r = dm_register_target(&cache_target);
  2152. if (r) {
  2153. DMERR("cache target registration failed: %d", r);
  2154. return r;
  2155. }
  2156. migration_cache = KMEM_CACHE(dm_cache_migration, 0);
  2157. if (!migration_cache) {
  2158. dm_unregister_target(&cache_target);
  2159. return -ENOMEM;
  2160. }
  2161. return 0;
  2162. }
  2163. static void __exit dm_cache_exit(void)
  2164. {
  2165. dm_unregister_target(&cache_target);
  2166. kmem_cache_destroy(migration_cache);
  2167. }
  2168. module_init(dm_cache_init);
  2169. module_exit(dm_cache_exit);
  2170. MODULE_DESCRIPTION(DM_NAME " cache target");
  2171. MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
  2172. MODULE_LICENSE("GPL");