dm-cache-target.c 72 KB

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