dm-cache-target.c 75 KB

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