dm-cache-target.c 64 KB

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