dm-cache-target.c 62 KB

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