dm-cache-target.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865
  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/jiffies.h>
  13. #include <linux/init.h>
  14. #include <linux/mempool.h>
  15. #include <linux/module.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #define DM_MSG_PREFIX "cache"
  19. DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(cache_copy_throttle,
  20. "A percentage of time allocated for copying to and/or from cache");
  21. /*----------------------------------------------------------------*/
  22. #define IOT_RESOLUTION 4
  23. struct io_tracker {
  24. spinlock_t lock;
  25. /*
  26. * Sectors of in-flight IO.
  27. */
  28. sector_t in_flight;
  29. /*
  30. * The time, in jiffies, when this device became idle (if it is
  31. * indeed idle).
  32. */
  33. unsigned long idle_time;
  34. unsigned long last_update_time;
  35. };
  36. static void iot_init(struct io_tracker *iot)
  37. {
  38. spin_lock_init(&iot->lock);
  39. iot->in_flight = 0ul;
  40. iot->idle_time = 0ul;
  41. iot->last_update_time = jiffies;
  42. }
  43. static bool __iot_idle_for(struct io_tracker *iot, unsigned long jifs)
  44. {
  45. if (iot->in_flight)
  46. return false;
  47. return time_after(jiffies, iot->idle_time + jifs);
  48. }
  49. static bool iot_idle_for(struct io_tracker *iot, unsigned long jifs)
  50. {
  51. bool r;
  52. unsigned long flags;
  53. spin_lock_irqsave(&iot->lock, flags);
  54. r = __iot_idle_for(iot, jifs);
  55. spin_unlock_irqrestore(&iot->lock, flags);
  56. return r;
  57. }
  58. static void iot_io_begin(struct io_tracker *iot, sector_t len)
  59. {
  60. unsigned long flags;
  61. spin_lock_irqsave(&iot->lock, flags);
  62. iot->in_flight += len;
  63. spin_unlock_irqrestore(&iot->lock, flags);
  64. }
  65. static void __iot_io_end(struct io_tracker *iot, sector_t len)
  66. {
  67. iot->in_flight -= len;
  68. if (!iot->in_flight)
  69. iot->idle_time = jiffies;
  70. }
  71. static void iot_io_end(struct io_tracker *iot, sector_t len)
  72. {
  73. unsigned long flags;
  74. spin_lock_irqsave(&iot->lock, flags);
  75. __iot_io_end(iot, len);
  76. spin_unlock_irqrestore(&iot->lock, flags);
  77. }
  78. /*----------------------------------------------------------------*/
  79. /*
  80. * Glossary:
  81. *
  82. * oblock: index of an origin block
  83. * cblock: index of a cache block
  84. * promotion: movement of a block from origin to cache
  85. * demotion: movement of a block from cache to origin
  86. * migration: movement of a block between the origin and cache device,
  87. * either direction
  88. */
  89. /*----------------------------------------------------------------*/
  90. /*
  91. * There are a couple of places where we let a bio run, but want to do some
  92. * work before calling its endio function. We do this by temporarily
  93. * changing the endio fn.
  94. */
  95. struct dm_hook_info {
  96. bio_end_io_t *bi_end_io;
  97. };
  98. static void dm_hook_bio(struct dm_hook_info *h, struct bio *bio,
  99. bio_end_io_t *bi_end_io, void *bi_private)
  100. {
  101. h->bi_end_io = bio->bi_end_io;
  102. bio->bi_end_io = bi_end_io;
  103. bio->bi_private = bi_private;
  104. }
  105. static void dm_unhook_bio(struct dm_hook_info *h, struct bio *bio)
  106. {
  107. bio->bi_end_io = h->bi_end_io;
  108. }
  109. /*----------------------------------------------------------------*/
  110. #define MIGRATION_POOL_SIZE 128
  111. #define COMMIT_PERIOD HZ
  112. #define MIGRATION_COUNT_WINDOW 10
  113. /*
  114. * The block size of the device holding cache data must be
  115. * between 32KB and 1GB.
  116. */
  117. #define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
  118. #define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
  119. enum cache_metadata_mode {
  120. CM_WRITE, /* metadata may be changed */
  121. CM_READ_ONLY, /* metadata may not be changed */
  122. CM_FAIL
  123. };
  124. enum cache_io_mode {
  125. /*
  126. * Data is written to cached blocks only. These blocks are marked
  127. * dirty. If you lose the cache device you will lose data.
  128. * Potential performance increase for both reads and writes.
  129. */
  130. CM_IO_WRITEBACK,
  131. /*
  132. * Data is written to both cache and origin. Blocks are never
  133. * dirty. Potential performance benfit for reads only.
  134. */
  135. CM_IO_WRITETHROUGH,
  136. /*
  137. * A degraded mode useful for various cache coherency situations
  138. * (eg, rolling back snapshots). Reads and writes always go to the
  139. * origin. If a write goes to a cached oblock, then the cache
  140. * block is invalidated.
  141. */
  142. CM_IO_PASSTHROUGH
  143. };
  144. struct cache_features {
  145. enum cache_metadata_mode mode;
  146. enum cache_io_mode io_mode;
  147. };
  148. struct cache_stats {
  149. atomic_t read_hit;
  150. atomic_t read_miss;
  151. atomic_t write_hit;
  152. atomic_t write_miss;
  153. atomic_t demotion;
  154. atomic_t promotion;
  155. atomic_t copies_avoided;
  156. atomic_t cache_cell_clash;
  157. atomic_t commit_count;
  158. atomic_t discard_count;
  159. };
  160. /*
  161. * Defines a range of cblocks, begin to (end - 1) are in the range. end is
  162. * the one-past-the-end value.
  163. */
  164. struct cblock_range {
  165. dm_cblock_t begin;
  166. dm_cblock_t end;
  167. };
  168. struct invalidation_request {
  169. struct list_head list;
  170. struct cblock_range *cblocks;
  171. atomic_t complete;
  172. int err;
  173. wait_queue_head_t result_wait;
  174. };
  175. struct cache {
  176. struct dm_target *ti;
  177. struct dm_target_callbacks callbacks;
  178. struct dm_cache_metadata *cmd;
  179. /*
  180. * Metadata is written to this device.
  181. */
  182. struct dm_dev *metadata_dev;
  183. /*
  184. * The slower of the two data devices. Typically a spindle.
  185. */
  186. struct dm_dev *origin_dev;
  187. /*
  188. * The faster of the two data devices. Typically an SSD.
  189. */
  190. struct dm_dev *cache_dev;
  191. /*
  192. * Size of the origin device in _complete_ blocks and native sectors.
  193. */
  194. dm_oblock_t origin_blocks;
  195. sector_t origin_sectors;
  196. /*
  197. * Size of the cache device in blocks.
  198. */
  199. dm_cblock_t cache_size;
  200. /*
  201. * Fields for converting from sectors to blocks.
  202. */
  203. uint32_t sectors_per_block;
  204. int sectors_per_block_shift;
  205. spinlock_t lock;
  206. struct list_head deferred_cells;
  207. struct bio_list deferred_bios;
  208. struct bio_list deferred_flush_bios;
  209. struct bio_list deferred_writethrough_bios;
  210. struct list_head quiesced_migrations;
  211. struct list_head completed_migrations;
  212. struct list_head need_commit_migrations;
  213. sector_t migration_threshold;
  214. wait_queue_head_t migration_wait;
  215. atomic_t nr_allocated_migrations;
  216. /*
  217. * The number of in flight migrations that are performing
  218. * background io. eg, promotion, writeback.
  219. */
  220. atomic_t nr_io_migrations;
  221. wait_queue_head_t quiescing_wait;
  222. atomic_t quiescing;
  223. atomic_t quiescing_ack;
  224. /*
  225. * cache_size entries, dirty if set
  226. */
  227. atomic_t nr_dirty;
  228. unsigned long *dirty_bitset;
  229. /*
  230. * origin_blocks entries, discarded if set.
  231. */
  232. dm_dblock_t discard_nr_blocks;
  233. unsigned long *discard_bitset;
  234. uint32_t discard_block_size; /* a power of 2 times sectors per block */
  235. /*
  236. * Rather than reconstructing the table line for the status we just
  237. * save it and regurgitate.
  238. */
  239. unsigned nr_ctr_args;
  240. const char **ctr_args;
  241. struct dm_kcopyd_client *copier;
  242. struct workqueue_struct *wq;
  243. struct work_struct worker;
  244. struct delayed_work waker;
  245. unsigned long last_commit_jiffies;
  246. struct dm_bio_prison *prison;
  247. struct dm_deferred_set *all_io_ds;
  248. mempool_t *migration_pool;
  249. struct dm_cache_policy *policy;
  250. unsigned policy_nr_args;
  251. bool need_tick_bio:1;
  252. bool sized:1;
  253. bool invalidate:1;
  254. bool commit_requested:1;
  255. bool loaded_mappings:1;
  256. bool loaded_discards:1;
  257. /*
  258. * Cache features such as write-through.
  259. */
  260. struct cache_features features;
  261. struct cache_stats stats;
  262. /*
  263. * Invalidation fields.
  264. */
  265. spinlock_t invalidation_lock;
  266. struct list_head invalidation_requests;
  267. struct io_tracker origin_tracker;
  268. };
  269. struct per_bio_data {
  270. bool tick:1;
  271. unsigned req_nr:2;
  272. struct dm_deferred_entry *all_io_entry;
  273. struct dm_hook_info hook_info;
  274. sector_t len;
  275. /*
  276. * writethrough fields. These MUST remain at the end of this
  277. * structure and the 'cache' member must be the first as it
  278. * is used to determine the offset of the writethrough fields.
  279. */
  280. struct cache *cache;
  281. dm_cblock_t cblock;
  282. struct dm_bio_details bio_details;
  283. };
  284. struct dm_cache_migration {
  285. struct list_head list;
  286. struct cache *cache;
  287. unsigned long start_jiffies;
  288. dm_oblock_t old_oblock;
  289. dm_oblock_t new_oblock;
  290. dm_cblock_t cblock;
  291. bool err:1;
  292. bool discard:1;
  293. bool writeback:1;
  294. bool demote:1;
  295. bool promote:1;
  296. bool requeue_holder:1;
  297. bool invalidate:1;
  298. struct dm_bio_prison_cell *old_ocell;
  299. struct dm_bio_prison_cell *new_ocell;
  300. };
  301. /*
  302. * Processing a bio in the worker thread may require these memory
  303. * allocations. We prealloc to avoid deadlocks (the same worker thread
  304. * frees them back to the mempool).
  305. */
  306. struct prealloc {
  307. struct dm_cache_migration *mg;
  308. struct dm_bio_prison_cell *cell1;
  309. struct dm_bio_prison_cell *cell2;
  310. };
  311. static enum cache_metadata_mode get_cache_mode(struct cache *cache);
  312. static void wake_worker(struct cache *cache)
  313. {
  314. queue_work(cache->wq, &cache->worker);
  315. }
  316. /*----------------------------------------------------------------*/
  317. static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
  318. {
  319. /* FIXME: change to use a local slab. */
  320. return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
  321. }
  322. static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
  323. {
  324. dm_bio_prison_free_cell(cache->prison, cell);
  325. }
  326. static struct dm_cache_migration *alloc_migration(struct cache *cache)
  327. {
  328. struct dm_cache_migration *mg;
  329. mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
  330. if (mg) {
  331. mg->cache = cache;
  332. atomic_inc(&mg->cache->nr_allocated_migrations);
  333. }
  334. return mg;
  335. }
  336. static void free_migration(struct dm_cache_migration *mg)
  337. {
  338. struct cache *cache = mg->cache;
  339. if (atomic_dec_and_test(&cache->nr_allocated_migrations))
  340. wake_up(&cache->migration_wait);
  341. mempool_free(mg, cache->migration_pool);
  342. }
  343. static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
  344. {
  345. if (!p->mg) {
  346. p->mg = alloc_migration(cache);
  347. if (!p->mg)
  348. return -ENOMEM;
  349. }
  350. if (!p->cell1) {
  351. p->cell1 = alloc_prison_cell(cache);
  352. if (!p->cell1)
  353. return -ENOMEM;
  354. }
  355. if (!p->cell2) {
  356. p->cell2 = alloc_prison_cell(cache);
  357. if (!p->cell2)
  358. return -ENOMEM;
  359. }
  360. return 0;
  361. }
  362. static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
  363. {
  364. if (p->cell2)
  365. free_prison_cell(cache, p->cell2);
  366. if (p->cell1)
  367. free_prison_cell(cache, p->cell1);
  368. if (p->mg)
  369. free_migration(p->mg);
  370. }
  371. static struct dm_cache_migration *prealloc_get_migration(struct prealloc *p)
  372. {
  373. struct dm_cache_migration *mg = p->mg;
  374. BUG_ON(!mg);
  375. p->mg = NULL;
  376. return mg;
  377. }
  378. /*
  379. * You must have a cell within the prealloc struct to return. If not this
  380. * function will BUG() rather than returning NULL.
  381. */
  382. static struct dm_bio_prison_cell *prealloc_get_cell(struct prealloc *p)
  383. {
  384. struct dm_bio_prison_cell *r = NULL;
  385. if (p->cell1) {
  386. r = p->cell1;
  387. p->cell1 = NULL;
  388. } else if (p->cell2) {
  389. r = p->cell2;
  390. p->cell2 = NULL;
  391. } else
  392. BUG();
  393. return r;
  394. }
  395. /*
  396. * You can't have more than two cells in a prealloc struct. BUG() will be
  397. * called if you try and overfill.
  398. */
  399. static void prealloc_put_cell(struct prealloc *p, struct dm_bio_prison_cell *cell)
  400. {
  401. if (!p->cell2)
  402. p->cell2 = cell;
  403. else if (!p->cell1)
  404. p->cell1 = cell;
  405. else
  406. BUG();
  407. }
  408. /*----------------------------------------------------------------*/
  409. static void build_key(dm_oblock_t begin, dm_oblock_t end, struct dm_cell_key *key)
  410. {
  411. key->virtual = 0;
  412. key->dev = 0;
  413. key->block_begin = from_oblock(begin);
  414. key->block_end = from_oblock(end);
  415. }
  416. /*
  417. * The caller hands in a preallocated cell, and a free function for it.
  418. * The cell will be freed if there's an error, or if it wasn't used because
  419. * a cell with that key already exists.
  420. */
  421. typedef void (*cell_free_fn)(void *context, struct dm_bio_prison_cell *cell);
  422. static int bio_detain_range(struct cache *cache, dm_oblock_t oblock_begin, dm_oblock_t oblock_end,
  423. struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
  424. cell_free_fn free_fn, void *free_context,
  425. struct dm_bio_prison_cell **cell_result)
  426. {
  427. int r;
  428. struct dm_cell_key key;
  429. build_key(oblock_begin, oblock_end, &key);
  430. r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
  431. if (r)
  432. free_fn(free_context, cell_prealloc);
  433. return r;
  434. }
  435. static int bio_detain(struct cache *cache, dm_oblock_t oblock,
  436. struct bio *bio, struct dm_bio_prison_cell *cell_prealloc,
  437. cell_free_fn free_fn, void *free_context,
  438. struct dm_bio_prison_cell **cell_result)
  439. {
  440. dm_oblock_t end = to_oblock(from_oblock(oblock) + 1ULL);
  441. return bio_detain_range(cache, oblock, end, bio,
  442. cell_prealloc, free_fn, free_context, cell_result);
  443. }
  444. static int get_cell(struct cache *cache,
  445. dm_oblock_t oblock,
  446. struct prealloc *structs,
  447. struct dm_bio_prison_cell **cell_result)
  448. {
  449. int r;
  450. struct dm_cell_key key;
  451. struct dm_bio_prison_cell *cell_prealloc;
  452. cell_prealloc = prealloc_get_cell(structs);
  453. build_key(oblock, to_oblock(from_oblock(oblock) + 1ULL), &key);
  454. r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
  455. if (r)
  456. prealloc_put_cell(structs, cell_prealloc);
  457. return r;
  458. }
  459. /*----------------------------------------------------------------*/
  460. static bool is_dirty(struct cache *cache, dm_cblock_t b)
  461. {
  462. return test_bit(from_cblock(b), cache->dirty_bitset);
  463. }
  464. static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
  465. {
  466. if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
  467. atomic_inc(&cache->nr_dirty);
  468. policy_set_dirty(cache->policy, oblock);
  469. }
  470. }
  471. static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
  472. {
  473. if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
  474. policy_clear_dirty(cache->policy, oblock);
  475. if (atomic_dec_return(&cache->nr_dirty) == 0)
  476. dm_table_event(cache->ti->table);
  477. }
  478. }
  479. /*----------------------------------------------------------------*/
  480. static bool block_size_is_power_of_two(struct cache *cache)
  481. {
  482. return cache->sectors_per_block_shift >= 0;
  483. }
  484. /* gcc on ARM generates spurious references to __udivdi3 and __umoddi3 */
  485. #if defined(CONFIG_ARM) && __GNUC__ == 4 && __GNUC_MINOR__ <= 6
  486. __always_inline
  487. #endif
  488. static dm_block_t block_div(dm_block_t b, uint32_t n)
  489. {
  490. do_div(b, n);
  491. return b;
  492. }
  493. static dm_block_t oblocks_per_dblock(struct cache *cache)
  494. {
  495. dm_block_t oblocks = cache->discard_block_size;
  496. if (block_size_is_power_of_two(cache))
  497. oblocks >>= cache->sectors_per_block_shift;
  498. else
  499. oblocks = block_div(oblocks, cache->sectors_per_block);
  500. return oblocks;
  501. }
  502. static dm_dblock_t oblock_to_dblock(struct cache *cache, dm_oblock_t oblock)
  503. {
  504. return to_dblock(block_div(from_oblock(oblock),
  505. oblocks_per_dblock(cache)));
  506. }
  507. static dm_oblock_t dblock_to_oblock(struct cache *cache, dm_dblock_t dblock)
  508. {
  509. return to_oblock(from_dblock(dblock) * oblocks_per_dblock(cache));
  510. }
  511. static void set_discard(struct cache *cache, dm_dblock_t b)
  512. {
  513. unsigned long flags;
  514. BUG_ON(from_dblock(b) >= from_dblock(cache->discard_nr_blocks));
  515. atomic_inc(&cache->stats.discard_count);
  516. spin_lock_irqsave(&cache->lock, flags);
  517. set_bit(from_dblock(b), cache->discard_bitset);
  518. spin_unlock_irqrestore(&cache->lock, flags);
  519. }
  520. static void clear_discard(struct cache *cache, dm_dblock_t b)
  521. {
  522. unsigned long flags;
  523. spin_lock_irqsave(&cache->lock, flags);
  524. clear_bit(from_dblock(b), cache->discard_bitset);
  525. spin_unlock_irqrestore(&cache->lock, flags);
  526. }
  527. static bool is_discarded(struct cache *cache, dm_dblock_t b)
  528. {
  529. int r;
  530. unsigned long flags;
  531. spin_lock_irqsave(&cache->lock, flags);
  532. r = test_bit(from_dblock(b), cache->discard_bitset);
  533. spin_unlock_irqrestore(&cache->lock, flags);
  534. return r;
  535. }
  536. static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
  537. {
  538. int r;
  539. unsigned long flags;
  540. spin_lock_irqsave(&cache->lock, flags);
  541. r = test_bit(from_dblock(oblock_to_dblock(cache, b)),
  542. cache->discard_bitset);
  543. spin_unlock_irqrestore(&cache->lock, flags);
  544. return r;
  545. }
  546. /*----------------------------------------------------------------*/
  547. static void load_stats(struct cache *cache)
  548. {
  549. struct dm_cache_statistics stats;
  550. dm_cache_metadata_get_stats(cache->cmd, &stats);
  551. atomic_set(&cache->stats.read_hit, stats.read_hits);
  552. atomic_set(&cache->stats.read_miss, stats.read_misses);
  553. atomic_set(&cache->stats.write_hit, stats.write_hits);
  554. atomic_set(&cache->stats.write_miss, stats.write_misses);
  555. }
  556. static void save_stats(struct cache *cache)
  557. {
  558. struct dm_cache_statistics stats;
  559. if (get_cache_mode(cache) >= CM_READ_ONLY)
  560. return;
  561. stats.read_hits = atomic_read(&cache->stats.read_hit);
  562. stats.read_misses = atomic_read(&cache->stats.read_miss);
  563. stats.write_hits = atomic_read(&cache->stats.write_hit);
  564. stats.write_misses = atomic_read(&cache->stats.write_miss);
  565. dm_cache_metadata_set_stats(cache->cmd, &stats);
  566. }
  567. /*----------------------------------------------------------------
  568. * Per bio data
  569. *--------------------------------------------------------------*/
  570. /*
  571. * If using writeback, leave out struct per_bio_data's writethrough fields.
  572. */
  573. #define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
  574. #define PB_DATA_SIZE_WT (sizeof(struct per_bio_data))
  575. static bool writethrough_mode(struct cache_features *f)
  576. {
  577. return f->io_mode == CM_IO_WRITETHROUGH;
  578. }
  579. static bool writeback_mode(struct cache_features *f)
  580. {
  581. return f->io_mode == CM_IO_WRITEBACK;
  582. }
  583. static bool passthrough_mode(struct cache_features *f)
  584. {
  585. return f->io_mode == CM_IO_PASSTHROUGH;
  586. }
  587. static size_t get_per_bio_data_size(struct cache *cache)
  588. {
  589. return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
  590. }
  591. static struct per_bio_data *get_per_bio_data(struct bio *bio, size_t data_size)
  592. {
  593. struct per_bio_data *pb = dm_per_bio_data(bio, data_size);
  594. BUG_ON(!pb);
  595. return pb;
  596. }
  597. static struct per_bio_data *init_per_bio_data(struct bio *bio, size_t data_size)
  598. {
  599. struct per_bio_data *pb = get_per_bio_data(bio, data_size);
  600. pb->tick = false;
  601. pb->req_nr = dm_bio_get_target_bio_nr(bio);
  602. pb->all_io_entry = NULL;
  603. pb->len = 0;
  604. return pb;
  605. }
  606. /*----------------------------------------------------------------
  607. * Remapping
  608. *--------------------------------------------------------------*/
  609. static void remap_to_origin(struct cache *cache, struct bio *bio)
  610. {
  611. bio->bi_bdev = cache->origin_dev->bdev;
  612. }
  613. static void remap_to_cache(struct cache *cache, struct bio *bio,
  614. dm_cblock_t cblock)
  615. {
  616. sector_t bi_sector = bio->bi_iter.bi_sector;
  617. sector_t block = from_cblock(cblock);
  618. bio->bi_bdev = cache->cache_dev->bdev;
  619. if (!block_size_is_power_of_two(cache))
  620. bio->bi_iter.bi_sector =
  621. (block * cache->sectors_per_block) +
  622. sector_div(bi_sector, cache->sectors_per_block);
  623. else
  624. bio->bi_iter.bi_sector =
  625. (block << cache->sectors_per_block_shift) |
  626. (bi_sector & (cache->sectors_per_block - 1));
  627. }
  628. static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
  629. {
  630. unsigned long flags;
  631. size_t pb_data_size = get_per_bio_data_size(cache);
  632. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  633. spin_lock_irqsave(&cache->lock, flags);
  634. if (cache->need_tick_bio &&
  635. !(bio->bi_opf & (REQ_FUA | REQ_PREFLUSH)) &&
  636. bio_op(bio) != REQ_OP_DISCARD) {
  637. pb->tick = true;
  638. cache->need_tick_bio = false;
  639. }
  640. spin_unlock_irqrestore(&cache->lock, flags);
  641. }
  642. static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
  643. dm_oblock_t oblock)
  644. {
  645. check_if_tick_bio_needed(cache, bio);
  646. remap_to_origin(cache, bio);
  647. if (bio_data_dir(bio) == WRITE)
  648. clear_discard(cache, oblock_to_dblock(cache, oblock));
  649. }
  650. static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
  651. dm_oblock_t oblock, dm_cblock_t cblock)
  652. {
  653. check_if_tick_bio_needed(cache, bio);
  654. remap_to_cache(cache, bio, cblock);
  655. if (bio_data_dir(bio) == WRITE) {
  656. set_dirty(cache, oblock, cblock);
  657. clear_discard(cache, oblock_to_dblock(cache, oblock));
  658. }
  659. }
  660. static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
  661. {
  662. sector_t block_nr = bio->bi_iter.bi_sector;
  663. if (!block_size_is_power_of_two(cache))
  664. (void) sector_div(block_nr, cache->sectors_per_block);
  665. else
  666. block_nr >>= cache->sectors_per_block_shift;
  667. return to_oblock(block_nr);
  668. }
  669. static int bio_triggers_commit(struct cache *cache, struct bio *bio)
  670. {
  671. return bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
  672. }
  673. /*
  674. * You must increment the deferred set whilst the prison cell is held. To
  675. * encourage this, we ask for 'cell' to be passed in.
  676. */
  677. static void inc_ds(struct cache *cache, struct bio *bio,
  678. struct dm_bio_prison_cell *cell)
  679. {
  680. size_t pb_data_size = get_per_bio_data_size(cache);
  681. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  682. BUG_ON(!cell);
  683. BUG_ON(pb->all_io_entry);
  684. pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
  685. }
  686. static bool accountable_bio(struct cache *cache, struct bio *bio)
  687. {
  688. return ((bio->bi_bdev == cache->origin_dev->bdev) &&
  689. bio_op(bio) != REQ_OP_DISCARD);
  690. }
  691. static void accounted_begin(struct cache *cache, struct bio *bio)
  692. {
  693. size_t pb_data_size = get_per_bio_data_size(cache);
  694. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  695. if (accountable_bio(cache, bio)) {
  696. pb->len = bio_sectors(bio);
  697. iot_io_begin(&cache->origin_tracker, pb->len);
  698. }
  699. }
  700. static void accounted_complete(struct cache *cache, struct bio *bio)
  701. {
  702. size_t pb_data_size = get_per_bio_data_size(cache);
  703. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  704. iot_io_end(&cache->origin_tracker, pb->len);
  705. }
  706. static void accounted_request(struct cache *cache, struct bio *bio)
  707. {
  708. accounted_begin(cache, bio);
  709. generic_make_request(bio);
  710. }
  711. static void issue(struct cache *cache, struct bio *bio)
  712. {
  713. unsigned long flags;
  714. if (!bio_triggers_commit(cache, bio)) {
  715. accounted_request(cache, bio);
  716. return;
  717. }
  718. /*
  719. * Batch together any bios that trigger commits and then issue a
  720. * single commit for them in do_worker().
  721. */
  722. spin_lock_irqsave(&cache->lock, flags);
  723. cache->commit_requested = true;
  724. bio_list_add(&cache->deferred_flush_bios, bio);
  725. spin_unlock_irqrestore(&cache->lock, flags);
  726. }
  727. static void inc_and_issue(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell *cell)
  728. {
  729. inc_ds(cache, bio, cell);
  730. issue(cache, bio);
  731. }
  732. static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
  733. {
  734. unsigned long flags;
  735. spin_lock_irqsave(&cache->lock, flags);
  736. bio_list_add(&cache->deferred_writethrough_bios, bio);
  737. spin_unlock_irqrestore(&cache->lock, flags);
  738. wake_worker(cache);
  739. }
  740. static void writethrough_endio(struct bio *bio)
  741. {
  742. struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
  743. dm_unhook_bio(&pb->hook_info, bio);
  744. if (bio->bi_error) {
  745. bio_endio(bio);
  746. return;
  747. }
  748. dm_bio_restore(&pb->bio_details, bio);
  749. remap_to_cache(pb->cache, bio, pb->cblock);
  750. /*
  751. * We can't issue this bio directly, since we're in interrupt
  752. * context. So it gets put on a bio list for processing by the
  753. * worker thread.
  754. */
  755. defer_writethrough_bio(pb->cache, bio);
  756. }
  757. /*
  758. * When running in writethrough mode we need to send writes to clean blocks
  759. * to both the cache and origin devices. In future we'd like to clone the
  760. * bio and send them in parallel, but for now we're doing them in
  761. * series as this is easier.
  762. */
  763. static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
  764. dm_oblock_t oblock, dm_cblock_t cblock)
  765. {
  766. struct per_bio_data *pb = get_per_bio_data(bio, PB_DATA_SIZE_WT);
  767. pb->cache = cache;
  768. pb->cblock = cblock;
  769. dm_hook_bio(&pb->hook_info, bio, writethrough_endio, NULL);
  770. dm_bio_record(&pb->bio_details, bio);
  771. remap_to_origin_clear_discard(pb->cache, bio, oblock);
  772. }
  773. /*----------------------------------------------------------------
  774. * Failure modes
  775. *--------------------------------------------------------------*/
  776. static enum cache_metadata_mode get_cache_mode(struct cache *cache)
  777. {
  778. return cache->features.mode;
  779. }
  780. static const char *cache_device_name(struct cache *cache)
  781. {
  782. return dm_device_name(dm_table_get_md(cache->ti->table));
  783. }
  784. static void notify_mode_switch(struct cache *cache, enum cache_metadata_mode mode)
  785. {
  786. const char *descs[] = {
  787. "write",
  788. "read-only",
  789. "fail"
  790. };
  791. dm_table_event(cache->ti->table);
  792. DMINFO("%s: switching cache to %s mode",
  793. cache_device_name(cache), descs[(int)mode]);
  794. }
  795. static void set_cache_mode(struct cache *cache, enum cache_metadata_mode new_mode)
  796. {
  797. bool needs_check;
  798. enum cache_metadata_mode old_mode = get_cache_mode(cache);
  799. if (dm_cache_metadata_needs_check(cache->cmd, &needs_check)) {
  800. DMERR("%s: unable to read needs_check flag, setting failure mode.",
  801. cache_device_name(cache));
  802. new_mode = CM_FAIL;
  803. }
  804. if (new_mode == CM_WRITE && needs_check) {
  805. DMERR("%s: unable to switch cache to write mode until repaired.",
  806. cache_device_name(cache));
  807. if (old_mode != new_mode)
  808. new_mode = old_mode;
  809. else
  810. new_mode = CM_READ_ONLY;
  811. }
  812. /* Never move out of fail mode */
  813. if (old_mode == CM_FAIL)
  814. new_mode = CM_FAIL;
  815. switch (new_mode) {
  816. case CM_FAIL:
  817. case CM_READ_ONLY:
  818. dm_cache_metadata_set_read_only(cache->cmd);
  819. break;
  820. case CM_WRITE:
  821. dm_cache_metadata_set_read_write(cache->cmd);
  822. break;
  823. }
  824. cache->features.mode = new_mode;
  825. if (new_mode != old_mode)
  826. notify_mode_switch(cache, new_mode);
  827. }
  828. static void abort_transaction(struct cache *cache)
  829. {
  830. const char *dev_name = cache_device_name(cache);
  831. if (get_cache_mode(cache) >= CM_READ_ONLY)
  832. return;
  833. if (dm_cache_metadata_set_needs_check(cache->cmd)) {
  834. DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
  835. set_cache_mode(cache, CM_FAIL);
  836. }
  837. DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
  838. if (dm_cache_metadata_abort(cache->cmd)) {
  839. DMERR("%s: failed to abort metadata transaction", dev_name);
  840. set_cache_mode(cache, CM_FAIL);
  841. }
  842. }
  843. static void metadata_operation_failed(struct cache *cache, const char *op, int r)
  844. {
  845. DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
  846. cache_device_name(cache), op, r);
  847. abort_transaction(cache);
  848. set_cache_mode(cache, CM_READ_ONLY);
  849. }
  850. /*----------------------------------------------------------------
  851. * Migration processing
  852. *
  853. * Migration covers moving data from the origin device to the cache, or
  854. * vice versa.
  855. *--------------------------------------------------------------*/
  856. static void inc_io_migrations(struct cache *cache)
  857. {
  858. atomic_inc(&cache->nr_io_migrations);
  859. }
  860. static void dec_io_migrations(struct cache *cache)
  861. {
  862. atomic_dec(&cache->nr_io_migrations);
  863. }
  864. static bool discard_or_flush(struct bio *bio)
  865. {
  866. return bio_op(bio) == REQ_OP_DISCARD ||
  867. bio->bi_opf & (REQ_PREFLUSH | REQ_FUA);
  868. }
  869. static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell)
  870. {
  871. if (discard_or_flush(cell->holder)) {
  872. /*
  873. * We have to handle these bios individually.
  874. */
  875. dm_cell_release(cache->prison, cell, &cache->deferred_bios);
  876. free_prison_cell(cache, cell);
  877. } else
  878. list_add_tail(&cell->user_list, &cache->deferred_cells);
  879. }
  880. static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell, bool holder)
  881. {
  882. unsigned long flags;
  883. if (!holder && dm_cell_promote_or_release(cache->prison, cell)) {
  884. /*
  885. * There was no prisoner to promote to holder, the
  886. * cell has been released.
  887. */
  888. free_prison_cell(cache, cell);
  889. return;
  890. }
  891. spin_lock_irqsave(&cache->lock, flags);
  892. __cell_defer(cache, cell);
  893. spin_unlock_irqrestore(&cache->lock, flags);
  894. wake_worker(cache);
  895. }
  896. static void cell_error_with_code(struct cache *cache, struct dm_bio_prison_cell *cell, int err)
  897. {
  898. dm_cell_error(cache->prison, cell, err);
  899. free_prison_cell(cache, cell);
  900. }
  901. static void cell_requeue(struct cache *cache, struct dm_bio_prison_cell *cell)
  902. {
  903. cell_error_with_code(cache, cell, DM_ENDIO_REQUEUE);
  904. }
  905. static void free_io_migration(struct dm_cache_migration *mg)
  906. {
  907. struct cache *cache = mg->cache;
  908. dec_io_migrations(cache);
  909. free_migration(mg);
  910. wake_worker(cache);
  911. }
  912. static void migration_failure(struct dm_cache_migration *mg)
  913. {
  914. struct cache *cache = mg->cache;
  915. const char *dev_name = cache_device_name(cache);
  916. if (mg->writeback) {
  917. DMERR_LIMIT("%s: writeback failed; couldn't copy block", dev_name);
  918. set_dirty(cache, mg->old_oblock, mg->cblock);
  919. cell_defer(cache, mg->old_ocell, false);
  920. } else if (mg->demote) {
  921. DMERR_LIMIT("%s: demotion failed; couldn't copy block", dev_name);
  922. policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
  923. cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
  924. if (mg->promote)
  925. cell_defer(cache, mg->new_ocell, true);
  926. } else {
  927. DMERR_LIMIT("%s: promotion failed; couldn't copy block", dev_name);
  928. policy_remove_mapping(cache->policy, mg->new_oblock);
  929. cell_defer(cache, mg->new_ocell, true);
  930. }
  931. free_io_migration(mg);
  932. }
  933. static void migration_success_pre_commit(struct dm_cache_migration *mg)
  934. {
  935. int r;
  936. unsigned long flags;
  937. struct cache *cache = mg->cache;
  938. if (mg->writeback) {
  939. clear_dirty(cache, mg->old_oblock, mg->cblock);
  940. cell_defer(cache, mg->old_ocell, false);
  941. free_io_migration(mg);
  942. return;
  943. } else if (mg->demote) {
  944. r = dm_cache_remove_mapping(cache->cmd, mg->cblock);
  945. if (r) {
  946. DMERR_LIMIT("%s: demotion failed; couldn't update on disk metadata",
  947. cache_device_name(cache));
  948. metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
  949. policy_force_mapping(cache->policy, mg->new_oblock,
  950. mg->old_oblock);
  951. if (mg->promote)
  952. cell_defer(cache, mg->new_ocell, true);
  953. free_io_migration(mg);
  954. return;
  955. }
  956. } else {
  957. r = dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock);
  958. if (r) {
  959. DMERR_LIMIT("%s: promotion failed; couldn't update on disk metadata",
  960. cache_device_name(cache));
  961. metadata_operation_failed(cache, "dm_cache_insert_mapping", r);
  962. policy_remove_mapping(cache->policy, mg->new_oblock);
  963. free_io_migration(mg);
  964. return;
  965. }
  966. }
  967. spin_lock_irqsave(&cache->lock, flags);
  968. list_add_tail(&mg->list, &cache->need_commit_migrations);
  969. cache->commit_requested = true;
  970. spin_unlock_irqrestore(&cache->lock, flags);
  971. }
  972. static void migration_success_post_commit(struct dm_cache_migration *mg)
  973. {
  974. unsigned long flags;
  975. struct cache *cache = mg->cache;
  976. if (mg->writeback) {
  977. DMWARN_LIMIT("%s: writeback unexpectedly triggered commit",
  978. cache_device_name(cache));
  979. return;
  980. } else if (mg->demote) {
  981. cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
  982. if (mg->promote) {
  983. mg->demote = false;
  984. spin_lock_irqsave(&cache->lock, flags);
  985. list_add_tail(&mg->list, &cache->quiesced_migrations);
  986. spin_unlock_irqrestore(&cache->lock, flags);
  987. } else {
  988. if (mg->invalidate)
  989. policy_remove_mapping(cache->policy, mg->old_oblock);
  990. free_io_migration(mg);
  991. }
  992. } else {
  993. if (mg->requeue_holder) {
  994. clear_dirty(cache, mg->new_oblock, mg->cblock);
  995. cell_defer(cache, mg->new_ocell, true);
  996. } else {
  997. /*
  998. * The block was promoted via an overwrite, so it's dirty.
  999. */
  1000. set_dirty(cache, mg->new_oblock, mg->cblock);
  1001. bio_endio(mg->new_ocell->holder);
  1002. cell_defer(cache, mg->new_ocell, false);
  1003. }
  1004. free_io_migration(mg);
  1005. }
  1006. }
  1007. static void copy_complete(int read_err, unsigned long write_err, void *context)
  1008. {
  1009. unsigned long flags;
  1010. struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
  1011. struct cache *cache = mg->cache;
  1012. if (read_err || write_err)
  1013. mg->err = true;
  1014. spin_lock_irqsave(&cache->lock, flags);
  1015. list_add_tail(&mg->list, &cache->completed_migrations);
  1016. spin_unlock_irqrestore(&cache->lock, flags);
  1017. wake_worker(cache);
  1018. }
  1019. static void issue_copy(struct dm_cache_migration *mg)
  1020. {
  1021. int r;
  1022. struct dm_io_region o_region, c_region;
  1023. struct cache *cache = mg->cache;
  1024. sector_t cblock = from_cblock(mg->cblock);
  1025. o_region.bdev = cache->origin_dev->bdev;
  1026. o_region.count = cache->sectors_per_block;
  1027. c_region.bdev = cache->cache_dev->bdev;
  1028. c_region.sector = cblock * cache->sectors_per_block;
  1029. c_region.count = cache->sectors_per_block;
  1030. if (mg->writeback || mg->demote) {
  1031. /* demote */
  1032. o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
  1033. r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
  1034. } else {
  1035. /* promote */
  1036. o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
  1037. r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
  1038. }
  1039. if (r < 0) {
  1040. DMERR_LIMIT("%s: issuing migration failed", cache_device_name(cache));
  1041. migration_failure(mg);
  1042. }
  1043. }
  1044. static void overwrite_endio(struct bio *bio)
  1045. {
  1046. struct dm_cache_migration *mg = bio->bi_private;
  1047. struct cache *cache = mg->cache;
  1048. size_t pb_data_size = get_per_bio_data_size(cache);
  1049. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  1050. unsigned long flags;
  1051. dm_unhook_bio(&pb->hook_info, bio);
  1052. if (bio->bi_error)
  1053. mg->err = true;
  1054. mg->requeue_holder = false;
  1055. spin_lock_irqsave(&cache->lock, flags);
  1056. list_add_tail(&mg->list, &cache->completed_migrations);
  1057. spin_unlock_irqrestore(&cache->lock, flags);
  1058. wake_worker(cache);
  1059. }
  1060. static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio)
  1061. {
  1062. size_t pb_data_size = get_per_bio_data_size(mg->cache);
  1063. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  1064. dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
  1065. remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
  1066. /*
  1067. * No need to inc_ds() here, since the cell will be held for the
  1068. * duration of the io.
  1069. */
  1070. accounted_request(mg->cache, bio);
  1071. }
  1072. static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
  1073. {
  1074. return (bio_data_dir(bio) == WRITE) &&
  1075. (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
  1076. }
  1077. static void avoid_copy(struct dm_cache_migration *mg)
  1078. {
  1079. atomic_inc(&mg->cache->stats.copies_avoided);
  1080. migration_success_pre_commit(mg);
  1081. }
  1082. static void calc_discard_block_range(struct cache *cache, struct bio *bio,
  1083. dm_dblock_t *b, dm_dblock_t *e)
  1084. {
  1085. sector_t sb = bio->bi_iter.bi_sector;
  1086. sector_t se = bio_end_sector(bio);
  1087. *b = to_dblock(dm_sector_div_up(sb, cache->discard_block_size));
  1088. if (se - sb < cache->discard_block_size)
  1089. *e = *b;
  1090. else
  1091. *e = to_dblock(block_div(se, cache->discard_block_size));
  1092. }
  1093. static void issue_discard(struct dm_cache_migration *mg)
  1094. {
  1095. dm_dblock_t b, e;
  1096. struct bio *bio = mg->new_ocell->holder;
  1097. struct cache *cache = mg->cache;
  1098. calc_discard_block_range(cache, bio, &b, &e);
  1099. while (b != e) {
  1100. set_discard(cache, b);
  1101. b = to_dblock(from_dblock(b) + 1);
  1102. }
  1103. bio_endio(bio);
  1104. cell_defer(cache, mg->new_ocell, false);
  1105. free_migration(mg);
  1106. wake_worker(cache);
  1107. }
  1108. static void issue_copy_or_discard(struct dm_cache_migration *mg)
  1109. {
  1110. bool avoid;
  1111. struct cache *cache = mg->cache;
  1112. if (mg->discard) {
  1113. issue_discard(mg);
  1114. return;
  1115. }
  1116. if (mg->writeback || mg->demote)
  1117. avoid = !is_dirty(cache, mg->cblock) ||
  1118. is_discarded_oblock(cache, mg->old_oblock);
  1119. else {
  1120. struct bio *bio = mg->new_ocell->holder;
  1121. avoid = is_discarded_oblock(cache, mg->new_oblock);
  1122. if (writeback_mode(&cache->features) &&
  1123. !avoid && bio_writes_complete_block(cache, bio)) {
  1124. issue_overwrite(mg, bio);
  1125. return;
  1126. }
  1127. }
  1128. avoid ? avoid_copy(mg) : issue_copy(mg);
  1129. }
  1130. static void complete_migration(struct dm_cache_migration *mg)
  1131. {
  1132. if (mg->err)
  1133. migration_failure(mg);
  1134. else
  1135. migration_success_pre_commit(mg);
  1136. }
  1137. static void process_migrations(struct cache *cache, struct list_head *head,
  1138. void (*fn)(struct dm_cache_migration *))
  1139. {
  1140. unsigned long flags;
  1141. struct list_head list;
  1142. struct dm_cache_migration *mg, *tmp;
  1143. INIT_LIST_HEAD(&list);
  1144. spin_lock_irqsave(&cache->lock, flags);
  1145. list_splice_init(head, &list);
  1146. spin_unlock_irqrestore(&cache->lock, flags);
  1147. list_for_each_entry_safe(mg, tmp, &list, list)
  1148. fn(mg);
  1149. }
  1150. static void __queue_quiesced_migration(struct dm_cache_migration *mg)
  1151. {
  1152. list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
  1153. }
  1154. static void queue_quiesced_migration(struct dm_cache_migration *mg)
  1155. {
  1156. unsigned long flags;
  1157. struct cache *cache = mg->cache;
  1158. spin_lock_irqsave(&cache->lock, flags);
  1159. __queue_quiesced_migration(mg);
  1160. spin_unlock_irqrestore(&cache->lock, flags);
  1161. wake_worker(cache);
  1162. }
  1163. static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
  1164. {
  1165. unsigned long flags;
  1166. struct dm_cache_migration *mg, *tmp;
  1167. spin_lock_irqsave(&cache->lock, flags);
  1168. list_for_each_entry_safe(mg, tmp, work, list)
  1169. __queue_quiesced_migration(mg);
  1170. spin_unlock_irqrestore(&cache->lock, flags);
  1171. wake_worker(cache);
  1172. }
  1173. static void check_for_quiesced_migrations(struct cache *cache,
  1174. struct per_bio_data *pb)
  1175. {
  1176. struct list_head work;
  1177. if (!pb->all_io_entry)
  1178. return;
  1179. INIT_LIST_HEAD(&work);
  1180. dm_deferred_entry_dec(pb->all_io_entry, &work);
  1181. if (!list_empty(&work))
  1182. queue_quiesced_migrations(cache, &work);
  1183. }
  1184. static void quiesce_migration(struct dm_cache_migration *mg)
  1185. {
  1186. if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
  1187. queue_quiesced_migration(mg);
  1188. }
  1189. static void promote(struct cache *cache, struct prealloc *structs,
  1190. dm_oblock_t oblock, dm_cblock_t cblock,
  1191. struct dm_bio_prison_cell *cell)
  1192. {
  1193. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1194. mg->err = false;
  1195. mg->discard = false;
  1196. mg->writeback = false;
  1197. mg->demote = false;
  1198. mg->promote = true;
  1199. mg->requeue_holder = true;
  1200. mg->invalidate = false;
  1201. mg->cache = cache;
  1202. mg->new_oblock = oblock;
  1203. mg->cblock = cblock;
  1204. mg->old_ocell = NULL;
  1205. mg->new_ocell = cell;
  1206. mg->start_jiffies = jiffies;
  1207. inc_io_migrations(cache);
  1208. quiesce_migration(mg);
  1209. }
  1210. static void writeback(struct cache *cache, struct prealloc *structs,
  1211. dm_oblock_t oblock, dm_cblock_t cblock,
  1212. struct dm_bio_prison_cell *cell)
  1213. {
  1214. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1215. mg->err = false;
  1216. mg->discard = false;
  1217. mg->writeback = true;
  1218. mg->demote = false;
  1219. mg->promote = false;
  1220. mg->requeue_holder = true;
  1221. mg->invalidate = false;
  1222. mg->cache = cache;
  1223. mg->old_oblock = oblock;
  1224. mg->cblock = cblock;
  1225. mg->old_ocell = cell;
  1226. mg->new_ocell = NULL;
  1227. mg->start_jiffies = jiffies;
  1228. inc_io_migrations(cache);
  1229. quiesce_migration(mg);
  1230. }
  1231. static void demote_then_promote(struct cache *cache, struct prealloc *structs,
  1232. dm_oblock_t old_oblock, dm_oblock_t new_oblock,
  1233. dm_cblock_t cblock,
  1234. struct dm_bio_prison_cell *old_ocell,
  1235. struct dm_bio_prison_cell *new_ocell)
  1236. {
  1237. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1238. mg->err = false;
  1239. mg->discard = false;
  1240. mg->writeback = false;
  1241. mg->demote = true;
  1242. mg->promote = true;
  1243. mg->requeue_holder = true;
  1244. mg->invalidate = false;
  1245. mg->cache = cache;
  1246. mg->old_oblock = old_oblock;
  1247. mg->new_oblock = new_oblock;
  1248. mg->cblock = cblock;
  1249. mg->old_ocell = old_ocell;
  1250. mg->new_ocell = new_ocell;
  1251. mg->start_jiffies = jiffies;
  1252. inc_io_migrations(cache);
  1253. quiesce_migration(mg);
  1254. }
  1255. /*
  1256. * Invalidate a cache entry. No writeback occurs; any changes in the cache
  1257. * block are thrown away.
  1258. */
  1259. static void invalidate(struct cache *cache, struct prealloc *structs,
  1260. dm_oblock_t oblock, dm_cblock_t cblock,
  1261. struct dm_bio_prison_cell *cell)
  1262. {
  1263. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1264. mg->err = false;
  1265. mg->discard = false;
  1266. mg->writeback = false;
  1267. mg->demote = true;
  1268. mg->promote = false;
  1269. mg->requeue_holder = true;
  1270. mg->invalidate = true;
  1271. mg->cache = cache;
  1272. mg->old_oblock = oblock;
  1273. mg->cblock = cblock;
  1274. mg->old_ocell = cell;
  1275. mg->new_ocell = NULL;
  1276. mg->start_jiffies = jiffies;
  1277. inc_io_migrations(cache);
  1278. quiesce_migration(mg);
  1279. }
  1280. static void discard(struct cache *cache, struct prealloc *structs,
  1281. struct dm_bio_prison_cell *cell)
  1282. {
  1283. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1284. mg->err = false;
  1285. mg->discard = true;
  1286. mg->writeback = false;
  1287. mg->demote = false;
  1288. mg->promote = false;
  1289. mg->requeue_holder = false;
  1290. mg->invalidate = false;
  1291. mg->cache = cache;
  1292. mg->old_ocell = NULL;
  1293. mg->new_ocell = cell;
  1294. mg->start_jiffies = jiffies;
  1295. quiesce_migration(mg);
  1296. }
  1297. /*----------------------------------------------------------------
  1298. * bio processing
  1299. *--------------------------------------------------------------*/
  1300. static void defer_bio(struct cache *cache, struct bio *bio)
  1301. {
  1302. unsigned long flags;
  1303. spin_lock_irqsave(&cache->lock, flags);
  1304. bio_list_add(&cache->deferred_bios, bio);
  1305. spin_unlock_irqrestore(&cache->lock, flags);
  1306. wake_worker(cache);
  1307. }
  1308. static void process_flush_bio(struct cache *cache, struct bio *bio)
  1309. {
  1310. size_t pb_data_size = get_per_bio_data_size(cache);
  1311. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  1312. BUG_ON(bio->bi_iter.bi_size);
  1313. if (!pb->req_nr)
  1314. remap_to_origin(cache, bio);
  1315. else
  1316. remap_to_cache(cache, bio, 0);
  1317. /*
  1318. * REQ_PREFLUSH is not directed at any particular block so we don't
  1319. * need to inc_ds(). REQ_FUA's are split into a write + REQ_PREFLUSH
  1320. * by dm-core.
  1321. */
  1322. issue(cache, bio);
  1323. }
  1324. static void process_discard_bio(struct cache *cache, struct prealloc *structs,
  1325. struct bio *bio)
  1326. {
  1327. int r;
  1328. dm_dblock_t b, e;
  1329. struct dm_bio_prison_cell *cell_prealloc, *new_ocell;
  1330. calc_discard_block_range(cache, bio, &b, &e);
  1331. if (b == e) {
  1332. bio_endio(bio);
  1333. return;
  1334. }
  1335. cell_prealloc = prealloc_get_cell(structs);
  1336. r = bio_detain_range(cache, dblock_to_oblock(cache, b), dblock_to_oblock(cache, e), bio, cell_prealloc,
  1337. (cell_free_fn) prealloc_put_cell,
  1338. structs, &new_ocell);
  1339. if (r > 0)
  1340. return;
  1341. discard(cache, structs, new_ocell);
  1342. }
  1343. static bool spare_migration_bandwidth(struct cache *cache)
  1344. {
  1345. sector_t current_volume = (atomic_read(&cache->nr_io_migrations) + 1) *
  1346. cache->sectors_per_block;
  1347. return current_volume < cache->migration_threshold;
  1348. }
  1349. static void inc_hit_counter(struct cache *cache, struct bio *bio)
  1350. {
  1351. atomic_inc(bio_data_dir(bio) == READ ?
  1352. &cache->stats.read_hit : &cache->stats.write_hit);
  1353. }
  1354. static void inc_miss_counter(struct cache *cache, struct bio *bio)
  1355. {
  1356. atomic_inc(bio_data_dir(bio) == READ ?
  1357. &cache->stats.read_miss : &cache->stats.write_miss);
  1358. }
  1359. /*----------------------------------------------------------------*/
  1360. struct inc_detail {
  1361. struct cache *cache;
  1362. struct bio_list bios_for_issue;
  1363. struct bio_list unhandled_bios;
  1364. bool any_writes;
  1365. };
  1366. static void inc_fn(void *context, struct dm_bio_prison_cell *cell)
  1367. {
  1368. struct bio *bio;
  1369. struct inc_detail *detail = context;
  1370. struct cache *cache = detail->cache;
  1371. inc_ds(cache, cell->holder, cell);
  1372. if (bio_data_dir(cell->holder) == WRITE)
  1373. detail->any_writes = true;
  1374. while ((bio = bio_list_pop(&cell->bios))) {
  1375. if (discard_or_flush(bio)) {
  1376. bio_list_add(&detail->unhandled_bios, bio);
  1377. continue;
  1378. }
  1379. if (bio_data_dir(bio) == WRITE)
  1380. detail->any_writes = true;
  1381. bio_list_add(&detail->bios_for_issue, bio);
  1382. inc_ds(cache, bio, cell);
  1383. }
  1384. }
  1385. // FIXME: refactor these two
  1386. static void remap_cell_to_origin_clear_discard(struct cache *cache,
  1387. struct dm_bio_prison_cell *cell,
  1388. dm_oblock_t oblock, bool issue_holder)
  1389. {
  1390. struct bio *bio;
  1391. unsigned long flags;
  1392. struct inc_detail detail;
  1393. detail.cache = cache;
  1394. bio_list_init(&detail.bios_for_issue);
  1395. bio_list_init(&detail.unhandled_bios);
  1396. detail.any_writes = false;
  1397. spin_lock_irqsave(&cache->lock, flags);
  1398. dm_cell_visit_release(cache->prison, inc_fn, &detail, cell);
  1399. bio_list_merge(&cache->deferred_bios, &detail.unhandled_bios);
  1400. spin_unlock_irqrestore(&cache->lock, flags);
  1401. remap_to_origin(cache, cell->holder);
  1402. if (issue_holder)
  1403. issue(cache, cell->holder);
  1404. else
  1405. accounted_begin(cache, cell->holder);
  1406. if (detail.any_writes)
  1407. clear_discard(cache, oblock_to_dblock(cache, oblock));
  1408. while ((bio = bio_list_pop(&detail.bios_for_issue))) {
  1409. remap_to_origin(cache, bio);
  1410. issue(cache, bio);
  1411. }
  1412. free_prison_cell(cache, cell);
  1413. }
  1414. static void remap_cell_to_cache_dirty(struct cache *cache, struct dm_bio_prison_cell *cell,
  1415. dm_oblock_t oblock, dm_cblock_t cblock, bool issue_holder)
  1416. {
  1417. struct bio *bio;
  1418. unsigned long flags;
  1419. struct inc_detail detail;
  1420. detail.cache = cache;
  1421. bio_list_init(&detail.bios_for_issue);
  1422. bio_list_init(&detail.unhandled_bios);
  1423. detail.any_writes = false;
  1424. spin_lock_irqsave(&cache->lock, flags);
  1425. dm_cell_visit_release(cache->prison, inc_fn, &detail, cell);
  1426. bio_list_merge(&cache->deferred_bios, &detail.unhandled_bios);
  1427. spin_unlock_irqrestore(&cache->lock, flags);
  1428. remap_to_cache(cache, cell->holder, cblock);
  1429. if (issue_holder)
  1430. issue(cache, cell->holder);
  1431. else
  1432. accounted_begin(cache, cell->holder);
  1433. if (detail.any_writes) {
  1434. set_dirty(cache, oblock, cblock);
  1435. clear_discard(cache, oblock_to_dblock(cache, oblock));
  1436. }
  1437. while ((bio = bio_list_pop(&detail.bios_for_issue))) {
  1438. remap_to_cache(cache, bio, cblock);
  1439. issue(cache, bio);
  1440. }
  1441. free_prison_cell(cache, cell);
  1442. }
  1443. /*----------------------------------------------------------------*/
  1444. struct old_oblock_lock {
  1445. struct policy_locker locker;
  1446. struct cache *cache;
  1447. struct prealloc *structs;
  1448. struct dm_bio_prison_cell *cell;
  1449. };
  1450. static int null_locker(struct policy_locker *locker, dm_oblock_t b)
  1451. {
  1452. /* This should never be called */
  1453. BUG();
  1454. return 0;
  1455. }
  1456. static int cell_locker(struct policy_locker *locker, dm_oblock_t b)
  1457. {
  1458. struct old_oblock_lock *l = container_of(locker, struct old_oblock_lock, locker);
  1459. struct dm_bio_prison_cell *cell_prealloc = prealloc_get_cell(l->structs);
  1460. return bio_detain(l->cache, b, NULL, cell_prealloc,
  1461. (cell_free_fn) prealloc_put_cell,
  1462. l->structs, &l->cell);
  1463. }
  1464. static void process_cell(struct cache *cache, struct prealloc *structs,
  1465. struct dm_bio_prison_cell *new_ocell)
  1466. {
  1467. int r;
  1468. bool release_cell = true;
  1469. struct bio *bio = new_ocell->holder;
  1470. dm_oblock_t block = get_bio_block(cache, bio);
  1471. struct policy_result lookup_result;
  1472. bool passthrough = passthrough_mode(&cache->features);
  1473. bool fast_promotion, can_migrate;
  1474. struct old_oblock_lock ool;
  1475. fast_promotion = is_discarded_oblock(cache, block) || bio_writes_complete_block(cache, bio);
  1476. can_migrate = !passthrough && (fast_promotion || spare_migration_bandwidth(cache));
  1477. ool.locker.fn = cell_locker;
  1478. ool.cache = cache;
  1479. ool.structs = structs;
  1480. ool.cell = NULL;
  1481. r = policy_map(cache->policy, block, true, can_migrate, fast_promotion,
  1482. bio, &ool.locker, &lookup_result);
  1483. if (r == -EWOULDBLOCK)
  1484. /* migration has been denied */
  1485. lookup_result.op = POLICY_MISS;
  1486. switch (lookup_result.op) {
  1487. case POLICY_HIT:
  1488. if (passthrough) {
  1489. inc_miss_counter(cache, bio);
  1490. /*
  1491. * Passthrough always maps to the origin,
  1492. * invalidating any cache blocks that are written
  1493. * to.
  1494. */
  1495. if (bio_data_dir(bio) == WRITE) {
  1496. atomic_inc(&cache->stats.demotion);
  1497. invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
  1498. release_cell = false;
  1499. } else {
  1500. /* FIXME: factor out issue_origin() */
  1501. remap_to_origin_clear_discard(cache, bio, block);
  1502. inc_and_issue(cache, bio, new_ocell);
  1503. }
  1504. } else {
  1505. inc_hit_counter(cache, bio);
  1506. if (bio_data_dir(bio) == WRITE &&
  1507. writethrough_mode(&cache->features) &&
  1508. !is_dirty(cache, lookup_result.cblock)) {
  1509. remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
  1510. inc_and_issue(cache, bio, new_ocell);
  1511. } else {
  1512. remap_cell_to_cache_dirty(cache, new_ocell, block, lookup_result.cblock, true);
  1513. release_cell = false;
  1514. }
  1515. }
  1516. break;
  1517. case POLICY_MISS:
  1518. inc_miss_counter(cache, bio);
  1519. remap_cell_to_origin_clear_discard(cache, new_ocell, block, true);
  1520. release_cell = false;
  1521. break;
  1522. case POLICY_NEW:
  1523. atomic_inc(&cache->stats.promotion);
  1524. promote(cache, structs, block, lookup_result.cblock, new_ocell);
  1525. release_cell = false;
  1526. break;
  1527. case POLICY_REPLACE:
  1528. atomic_inc(&cache->stats.demotion);
  1529. atomic_inc(&cache->stats.promotion);
  1530. demote_then_promote(cache, structs, lookup_result.old_oblock,
  1531. block, lookup_result.cblock,
  1532. ool.cell, new_ocell);
  1533. release_cell = false;
  1534. break;
  1535. default:
  1536. DMERR_LIMIT("%s: %s: erroring bio, unknown policy op: %u",
  1537. cache_device_name(cache), __func__,
  1538. (unsigned) lookup_result.op);
  1539. bio_io_error(bio);
  1540. }
  1541. if (release_cell)
  1542. cell_defer(cache, new_ocell, false);
  1543. }
  1544. static void process_bio(struct cache *cache, struct prealloc *structs,
  1545. struct bio *bio)
  1546. {
  1547. int r;
  1548. dm_oblock_t block = get_bio_block(cache, bio);
  1549. struct dm_bio_prison_cell *cell_prealloc, *new_ocell;
  1550. /*
  1551. * Check to see if that block is currently migrating.
  1552. */
  1553. cell_prealloc = prealloc_get_cell(structs);
  1554. r = bio_detain(cache, block, bio, cell_prealloc,
  1555. (cell_free_fn) prealloc_put_cell,
  1556. structs, &new_ocell);
  1557. if (r > 0)
  1558. return;
  1559. process_cell(cache, structs, new_ocell);
  1560. }
  1561. static int need_commit_due_to_time(struct cache *cache)
  1562. {
  1563. return jiffies < cache->last_commit_jiffies ||
  1564. jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
  1565. }
  1566. /*
  1567. * A non-zero return indicates read_only or fail_io mode.
  1568. */
  1569. static int commit(struct cache *cache, bool clean_shutdown)
  1570. {
  1571. int r;
  1572. if (get_cache_mode(cache) >= CM_READ_ONLY)
  1573. return -EINVAL;
  1574. atomic_inc(&cache->stats.commit_count);
  1575. r = dm_cache_commit(cache->cmd, clean_shutdown);
  1576. if (r)
  1577. metadata_operation_failed(cache, "dm_cache_commit", r);
  1578. return r;
  1579. }
  1580. static int commit_if_needed(struct cache *cache)
  1581. {
  1582. int r = 0;
  1583. if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
  1584. dm_cache_changed_this_transaction(cache->cmd)) {
  1585. r = commit(cache, false);
  1586. cache->commit_requested = false;
  1587. cache->last_commit_jiffies = jiffies;
  1588. }
  1589. return r;
  1590. }
  1591. static void process_deferred_bios(struct cache *cache)
  1592. {
  1593. bool prealloc_used = false;
  1594. unsigned long flags;
  1595. struct bio_list bios;
  1596. struct bio *bio;
  1597. struct prealloc structs;
  1598. memset(&structs, 0, sizeof(structs));
  1599. bio_list_init(&bios);
  1600. spin_lock_irqsave(&cache->lock, flags);
  1601. bio_list_merge(&bios, &cache->deferred_bios);
  1602. bio_list_init(&cache->deferred_bios);
  1603. spin_unlock_irqrestore(&cache->lock, flags);
  1604. while (!bio_list_empty(&bios)) {
  1605. /*
  1606. * If we've got no free migration structs, and processing
  1607. * this bio might require one, we pause until there are some
  1608. * prepared mappings to process.
  1609. */
  1610. prealloc_used = true;
  1611. if (prealloc_data_structs(cache, &structs)) {
  1612. spin_lock_irqsave(&cache->lock, flags);
  1613. bio_list_merge(&cache->deferred_bios, &bios);
  1614. spin_unlock_irqrestore(&cache->lock, flags);
  1615. break;
  1616. }
  1617. bio = bio_list_pop(&bios);
  1618. if (bio->bi_opf & REQ_PREFLUSH)
  1619. process_flush_bio(cache, bio);
  1620. else if (bio_op(bio) == REQ_OP_DISCARD)
  1621. process_discard_bio(cache, &structs, bio);
  1622. else
  1623. process_bio(cache, &structs, bio);
  1624. }
  1625. if (prealloc_used)
  1626. prealloc_free_structs(cache, &structs);
  1627. }
  1628. static void process_deferred_cells(struct cache *cache)
  1629. {
  1630. bool prealloc_used = false;
  1631. unsigned long flags;
  1632. struct dm_bio_prison_cell *cell, *tmp;
  1633. struct list_head cells;
  1634. struct prealloc structs;
  1635. memset(&structs, 0, sizeof(structs));
  1636. INIT_LIST_HEAD(&cells);
  1637. spin_lock_irqsave(&cache->lock, flags);
  1638. list_splice_init(&cache->deferred_cells, &cells);
  1639. spin_unlock_irqrestore(&cache->lock, flags);
  1640. list_for_each_entry_safe(cell, tmp, &cells, user_list) {
  1641. /*
  1642. * If we've got no free migration structs, and processing
  1643. * this bio might require one, we pause until there are some
  1644. * prepared mappings to process.
  1645. */
  1646. prealloc_used = true;
  1647. if (prealloc_data_structs(cache, &structs)) {
  1648. spin_lock_irqsave(&cache->lock, flags);
  1649. list_splice(&cells, &cache->deferred_cells);
  1650. spin_unlock_irqrestore(&cache->lock, flags);
  1651. break;
  1652. }
  1653. process_cell(cache, &structs, cell);
  1654. }
  1655. if (prealloc_used)
  1656. prealloc_free_structs(cache, &structs);
  1657. }
  1658. static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
  1659. {
  1660. unsigned long flags;
  1661. struct bio_list bios;
  1662. struct bio *bio;
  1663. bio_list_init(&bios);
  1664. spin_lock_irqsave(&cache->lock, flags);
  1665. bio_list_merge(&bios, &cache->deferred_flush_bios);
  1666. bio_list_init(&cache->deferred_flush_bios);
  1667. spin_unlock_irqrestore(&cache->lock, flags);
  1668. /*
  1669. * These bios have already been through inc_ds()
  1670. */
  1671. while ((bio = bio_list_pop(&bios)))
  1672. submit_bios ? accounted_request(cache, bio) : bio_io_error(bio);
  1673. }
  1674. static void process_deferred_writethrough_bios(struct cache *cache)
  1675. {
  1676. unsigned long flags;
  1677. struct bio_list bios;
  1678. struct bio *bio;
  1679. bio_list_init(&bios);
  1680. spin_lock_irqsave(&cache->lock, flags);
  1681. bio_list_merge(&bios, &cache->deferred_writethrough_bios);
  1682. bio_list_init(&cache->deferred_writethrough_bios);
  1683. spin_unlock_irqrestore(&cache->lock, flags);
  1684. /*
  1685. * These bios have already been through inc_ds()
  1686. */
  1687. while ((bio = bio_list_pop(&bios)))
  1688. accounted_request(cache, bio);
  1689. }
  1690. static void writeback_some_dirty_blocks(struct cache *cache)
  1691. {
  1692. bool prealloc_used = false;
  1693. dm_oblock_t oblock;
  1694. dm_cblock_t cblock;
  1695. struct prealloc structs;
  1696. struct dm_bio_prison_cell *old_ocell;
  1697. bool busy = !iot_idle_for(&cache->origin_tracker, HZ);
  1698. memset(&structs, 0, sizeof(structs));
  1699. while (spare_migration_bandwidth(cache)) {
  1700. if (policy_writeback_work(cache->policy, &oblock, &cblock, busy))
  1701. break; /* no work to do */
  1702. prealloc_used = true;
  1703. if (prealloc_data_structs(cache, &structs) ||
  1704. get_cell(cache, oblock, &structs, &old_ocell)) {
  1705. policy_set_dirty(cache->policy, oblock);
  1706. break;
  1707. }
  1708. writeback(cache, &structs, oblock, cblock, old_ocell);
  1709. }
  1710. if (prealloc_used)
  1711. prealloc_free_structs(cache, &structs);
  1712. }
  1713. /*----------------------------------------------------------------
  1714. * Invalidations.
  1715. * Dropping something from the cache *without* writing back.
  1716. *--------------------------------------------------------------*/
  1717. static void process_invalidation_request(struct cache *cache, struct invalidation_request *req)
  1718. {
  1719. int r = 0;
  1720. uint64_t begin = from_cblock(req->cblocks->begin);
  1721. uint64_t end = from_cblock(req->cblocks->end);
  1722. while (begin != end) {
  1723. r = policy_remove_cblock(cache->policy, to_cblock(begin));
  1724. if (!r) {
  1725. r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin));
  1726. if (r) {
  1727. metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
  1728. break;
  1729. }
  1730. } else if (r == -ENODATA) {
  1731. /* harmless, already unmapped */
  1732. r = 0;
  1733. } else {
  1734. DMERR("%s: policy_remove_cblock failed", cache_device_name(cache));
  1735. break;
  1736. }
  1737. begin++;
  1738. }
  1739. cache->commit_requested = true;
  1740. req->err = r;
  1741. atomic_set(&req->complete, 1);
  1742. wake_up(&req->result_wait);
  1743. }
  1744. static void process_invalidation_requests(struct cache *cache)
  1745. {
  1746. struct list_head list;
  1747. struct invalidation_request *req, *tmp;
  1748. INIT_LIST_HEAD(&list);
  1749. spin_lock(&cache->invalidation_lock);
  1750. list_splice_init(&cache->invalidation_requests, &list);
  1751. spin_unlock(&cache->invalidation_lock);
  1752. list_for_each_entry_safe (req, tmp, &list, list)
  1753. process_invalidation_request(cache, req);
  1754. }
  1755. /*----------------------------------------------------------------
  1756. * Main worker loop
  1757. *--------------------------------------------------------------*/
  1758. static bool is_quiescing(struct cache *cache)
  1759. {
  1760. return atomic_read(&cache->quiescing);
  1761. }
  1762. static void ack_quiescing(struct cache *cache)
  1763. {
  1764. if (is_quiescing(cache)) {
  1765. atomic_inc(&cache->quiescing_ack);
  1766. wake_up(&cache->quiescing_wait);
  1767. }
  1768. }
  1769. static void wait_for_quiescing_ack(struct cache *cache)
  1770. {
  1771. wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
  1772. }
  1773. static void start_quiescing(struct cache *cache)
  1774. {
  1775. atomic_inc(&cache->quiescing);
  1776. wait_for_quiescing_ack(cache);
  1777. }
  1778. static void stop_quiescing(struct cache *cache)
  1779. {
  1780. atomic_set(&cache->quiescing, 0);
  1781. atomic_set(&cache->quiescing_ack, 0);
  1782. }
  1783. static void wait_for_migrations(struct cache *cache)
  1784. {
  1785. wait_event(cache->migration_wait, !atomic_read(&cache->nr_allocated_migrations));
  1786. }
  1787. static void stop_worker(struct cache *cache)
  1788. {
  1789. cancel_delayed_work(&cache->waker);
  1790. flush_workqueue(cache->wq);
  1791. }
  1792. static void requeue_deferred_cells(struct cache *cache)
  1793. {
  1794. unsigned long flags;
  1795. struct list_head cells;
  1796. struct dm_bio_prison_cell *cell, *tmp;
  1797. INIT_LIST_HEAD(&cells);
  1798. spin_lock_irqsave(&cache->lock, flags);
  1799. list_splice_init(&cache->deferred_cells, &cells);
  1800. spin_unlock_irqrestore(&cache->lock, flags);
  1801. list_for_each_entry_safe(cell, tmp, &cells, user_list)
  1802. cell_requeue(cache, cell);
  1803. }
  1804. static void requeue_deferred_bios(struct cache *cache)
  1805. {
  1806. struct bio *bio;
  1807. struct bio_list bios;
  1808. bio_list_init(&bios);
  1809. bio_list_merge(&bios, &cache->deferred_bios);
  1810. bio_list_init(&cache->deferred_bios);
  1811. while ((bio = bio_list_pop(&bios))) {
  1812. bio->bi_error = DM_ENDIO_REQUEUE;
  1813. bio_endio(bio);
  1814. }
  1815. }
  1816. static int more_work(struct cache *cache)
  1817. {
  1818. if (is_quiescing(cache))
  1819. return !list_empty(&cache->quiesced_migrations) ||
  1820. !list_empty(&cache->completed_migrations) ||
  1821. !list_empty(&cache->need_commit_migrations);
  1822. else
  1823. return !bio_list_empty(&cache->deferred_bios) ||
  1824. !list_empty(&cache->deferred_cells) ||
  1825. !bio_list_empty(&cache->deferred_flush_bios) ||
  1826. !bio_list_empty(&cache->deferred_writethrough_bios) ||
  1827. !list_empty(&cache->quiesced_migrations) ||
  1828. !list_empty(&cache->completed_migrations) ||
  1829. !list_empty(&cache->need_commit_migrations) ||
  1830. cache->invalidate;
  1831. }
  1832. static void do_worker(struct work_struct *ws)
  1833. {
  1834. struct cache *cache = container_of(ws, struct cache, worker);
  1835. do {
  1836. if (!is_quiescing(cache)) {
  1837. writeback_some_dirty_blocks(cache);
  1838. process_deferred_writethrough_bios(cache);
  1839. process_deferred_bios(cache);
  1840. process_deferred_cells(cache);
  1841. process_invalidation_requests(cache);
  1842. }
  1843. process_migrations(cache, &cache->quiesced_migrations, issue_copy_or_discard);
  1844. process_migrations(cache, &cache->completed_migrations, complete_migration);
  1845. if (commit_if_needed(cache)) {
  1846. process_deferred_flush_bios(cache, false);
  1847. process_migrations(cache, &cache->need_commit_migrations, migration_failure);
  1848. } else {
  1849. process_deferred_flush_bios(cache, true);
  1850. process_migrations(cache, &cache->need_commit_migrations,
  1851. migration_success_post_commit);
  1852. }
  1853. ack_quiescing(cache);
  1854. } while (more_work(cache));
  1855. }
  1856. /*
  1857. * We want to commit periodically so that not too much
  1858. * unwritten metadata builds up.
  1859. */
  1860. static void do_waker(struct work_struct *ws)
  1861. {
  1862. struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
  1863. policy_tick(cache->policy, true);
  1864. wake_worker(cache);
  1865. queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
  1866. }
  1867. /*----------------------------------------------------------------*/
  1868. static int is_congested(struct dm_dev *dev, int bdi_bits)
  1869. {
  1870. struct request_queue *q = bdev_get_queue(dev->bdev);
  1871. return bdi_congested(&q->backing_dev_info, bdi_bits);
  1872. }
  1873. static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
  1874. {
  1875. struct cache *cache = container_of(cb, struct cache, callbacks);
  1876. return is_congested(cache->origin_dev, bdi_bits) ||
  1877. is_congested(cache->cache_dev, bdi_bits);
  1878. }
  1879. /*----------------------------------------------------------------
  1880. * Target methods
  1881. *--------------------------------------------------------------*/
  1882. /*
  1883. * This function gets called on the error paths of the constructor, so we
  1884. * have to cope with a partially initialised struct.
  1885. */
  1886. static void destroy(struct cache *cache)
  1887. {
  1888. unsigned i;
  1889. mempool_destroy(cache->migration_pool);
  1890. if (cache->all_io_ds)
  1891. dm_deferred_set_destroy(cache->all_io_ds);
  1892. if (cache->prison)
  1893. dm_bio_prison_destroy(cache->prison);
  1894. if (cache->wq)
  1895. destroy_workqueue(cache->wq);
  1896. if (cache->dirty_bitset)
  1897. free_bitset(cache->dirty_bitset);
  1898. if (cache->discard_bitset)
  1899. free_bitset(cache->discard_bitset);
  1900. if (cache->copier)
  1901. dm_kcopyd_client_destroy(cache->copier);
  1902. if (cache->cmd)
  1903. dm_cache_metadata_close(cache->cmd);
  1904. if (cache->metadata_dev)
  1905. dm_put_device(cache->ti, cache->metadata_dev);
  1906. if (cache->origin_dev)
  1907. dm_put_device(cache->ti, cache->origin_dev);
  1908. if (cache->cache_dev)
  1909. dm_put_device(cache->ti, cache->cache_dev);
  1910. if (cache->policy)
  1911. dm_cache_policy_destroy(cache->policy);
  1912. for (i = 0; i < cache->nr_ctr_args ; i++)
  1913. kfree(cache->ctr_args[i]);
  1914. kfree(cache->ctr_args);
  1915. kfree(cache);
  1916. }
  1917. static void cache_dtr(struct dm_target *ti)
  1918. {
  1919. struct cache *cache = ti->private;
  1920. destroy(cache);
  1921. }
  1922. static sector_t get_dev_size(struct dm_dev *dev)
  1923. {
  1924. return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
  1925. }
  1926. /*----------------------------------------------------------------*/
  1927. /*
  1928. * Construct a cache device mapping.
  1929. *
  1930. * cache <metadata dev> <cache dev> <origin dev> <block size>
  1931. * <#feature args> [<feature arg>]*
  1932. * <policy> <#policy args> [<policy arg>]*
  1933. *
  1934. * metadata dev : fast device holding the persistent metadata
  1935. * cache dev : fast device holding cached data blocks
  1936. * origin dev : slow device holding original data blocks
  1937. * block size : cache unit size in sectors
  1938. *
  1939. * #feature args : number of feature arguments passed
  1940. * feature args : writethrough. (The default is writeback.)
  1941. *
  1942. * policy : the replacement policy to use
  1943. * #policy args : an even number of policy arguments corresponding
  1944. * to key/value pairs passed to the policy
  1945. * policy args : key/value pairs passed to the policy
  1946. * E.g. 'sequential_threshold 1024'
  1947. * See cache-policies.txt for details.
  1948. *
  1949. * Optional feature arguments are:
  1950. * writethrough : write through caching that prohibits cache block
  1951. * content from being different from origin block content.
  1952. * Without this argument, the default behaviour is to write
  1953. * back cache block contents later for performance reasons,
  1954. * so they may differ from the corresponding origin blocks.
  1955. */
  1956. struct cache_args {
  1957. struct dm_target *ti;
  1958. struct dm_dev *metadata_dev;
  1959. struct dm_dev *cache_dev;
  1960. sector_t cache_sectors;
  1961. struct dm_dev *origin_dev;
  1962. sector_t origin_sectors;
  1963. uint32_t block_size;
  1964. const char *policy_name;
  1965. int policy_argc;
  1966. const char **policy_argv;
  1967. struct cache_features features;
  1968. };
  1969. static void destroy_cache_args(struct cache_args *ca)
  1970. {
  1971. if (ca->metadata_dev)
  1972. dm_put_device(ca->ti, ca->metadata_dev);
  1973. if (ca->cache_dev)
  1974. dm_put_device(ca->ti, ca->cache_dev);
  1975. if (ca->origin_dev)
  1976. dm_put_device(ca->ti, ca->origin_dev);
  1977. kfree(ca);
  1978. }
  1979. static bool at_least_one_arg(struct dm_arg_set *as, char **error)
  1980. {
  1981. if (!as->argc) {
  1982. *error = "Insufficient args";
  1983. return false;
  1984. }
  1985. return true;
  1986. }
  1987. static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
  1988. char **error)
  1989. {
  1990. int r;
  1991. sector_t metadata_dev_size;
  1992. char b[BDEVNAME_SIZE];
  1993. if (!at_least_one_arg(as, error))
  1994. return -EINVAL;
  1995. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  1996. &ca->metadata_dev);
  1997. if (r) {
  1998. *error = "Error opening metadata device";
  1999. return r;
  2000. }
  2001. metadata_dev_size = get_dev_size(ca->metadata_dev);
  2002. if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
  2003. DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
  2004. bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
  2005. return 0;
  2006. }
  2007. static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
  2008. char **error)
  2009. {
  2010. int r;
  2011. if (!at_least_one_arg(as, error))
  2012. return -EINVAL;
  2013. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  2014. &ca->cache_dev);
  2015. if (r) {
  2016. *error = "Error opening cache device";
  2017. return r;
  2018. }
  2019. ca->cache_sectors = get_dev_size(ca->cache_dev);
  2020. return 0;
  2021. }
  2022. static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
  2023. char **error)
  2024. {
  2025. int r;
  2026. if (!at_least_one_arg(as, error))
  2027. return -EINVAL;
  2028. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  2029. &ca->origin_dev);
  2030. if (r) {
  2031. *error = "Error opening origin device";
  2032. return r;
  2033. }
  2034. ca->origin_sectors = get_dev_size(ca->origin_dev);
  2035. if (ca->ti->len > ca->origin_sectors) {
  2036. *error = "Device size larger than cached device";
  2037. return -EINVAL;
  2038. }
  2039. return 0;
  2040. }
  2041. static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
  2042. char **error)
  2043. {
  2044. unsigned long block_size;
  2045. if (!at_least_one_arg(as, error))
  2046. return -EINVAL;
  2047. if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
  2048. block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
  2049. block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
  2050. block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
  2051. *error = "Invalid data block size";
  2052. return -EINVAL;
  2053. }
  2054. if (block_size > ca->cache_sectors) {
  2055. *error = "Data block size is larger than the cache device";
  2056. return -EINVAL;
  2057. }
  2058. ca->block_size = block_size;
  2059. return 0;
  2060. }
  2061. static void init_features(struct cache_features *cf)
  2062. {
  2063. cf->mode = CM_WRITE;
  2064. cf->io_mode = CM_IO_WRITEBACK;
  2065. }
  2066. static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
  2067. char **error)
  2068. {
  2069. static struct dm_arg _args[] = {
  2070. {0, 1, "Invalid number of cache feature arguments"},
  2071. };
  2072. int r;
  2073. unsigned argc;
  2074. const char *arg;
  2075. struct cache_features *cf = &ca->features;
  2076. init_features(cf);
  2077. r = dm_read_arg_group(_args, as, &argc, error);
  2078. if (r)
  2079. return -EINVAL;
  2080. while (argc--) {
  2081. arg = dm_shift_arg(as);
  2082. if (!strcasecmp(arg, "writeback"))
  2083. cf->io_mode = CM_IO_WRITEBACK;
  2084. else if (!strcasecmp(arg, "writethrough"))
  2085. cf->io_mode = CM_IO_WRITETHROUGH;
  2086. else if (!strcasecmp(arg, "passthrough"))
  2087. cf->io_mode = CM_IO_PASSTHROUGH;
  2088. else {
  2089. *error = "Unrecognised cache feature requested";
  2090. return -EINVAL;
  2091. }
  2092. }
  2093. return 0;
  2094. }
  2095. static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
  2096. char **error)
  2097. {
  2098. static struct dm_arg _args[] = {
  2099. {0, 1024, "Invalid number of policy arguments"},
  2100. };
  2101. int r;
  2102. if (!at_least_one_arg(as, error))
  2103. return -EINVAL;
  2104. ca->policy_name = dm_shift_arg(as);
  2105. r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
  2106. if (r)
  2107. return -EINVAL;
  2108. ca->policy_argv = (const char **)as->argv;
  2109. dm_consume_args(as, ca->policy_argc);
  2110. return 0;
  2111. }
  2112. static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
  2113. char **error)
  2114. {
  2115. int r;
  2116. struct dm_arg_set as;
  2117. as.argc = argc;
  2118. as.argv = argv;
  2119. r = parse_metadata_dev(ca, &as, error);
  2120. if (r)
  2121. return r;
  2122. r = parse_cache_dev(ca, &as, error);
  2123. if (r)
  2124. return r;
  2125. r = parse_origin_dev(ca, &as, error);
  2126. if (r)
  2127. return r;
  2128. r = parse_block_size(ca, &as, error);
  2129. if (r)
  2130. return r;
  2131. r = parse_features(ca, &as, error);
  2132. if (r)
  2133. return r;
  2134. r = parse_policy(ca, &as, error);
  2135. if (r)
  2136. return r;
  2137. return 0;
  2138. }
  2139. /*----------------------------------------------------------------*/
  2140. static struct kmem_cache *migration_cache;
  2141. #define NOT_CORE_OPTION 1
  2142. static int process_config_option(struct cache *cache, const char *key, const char *value)
  2143. {
  2144. unsigned long tmp;
  2145. if (!strcasecmp(key, "migration_threshold")) {
  2146. if (kstrtoul(value, 10, &tmp))
  2147. return -EINVAL;
  2148. cache->migration_threshold = tmp;
  2149. return 0;
  2150. }
  2151. return NOT_CORE_OPTION;
  2152. }
  2153. static int set_config_value(struct cache *cache, const char *key, const char *value)
  2154. {
  2155. int r = process_config_option(cache, key, value);
  2156. if (r == NOT_CORE_OPTION)
  2157. r = policy_set_config_value(cache->policy, key, value);
  2158. if (r)
  2159. DMWARN("bad config value for %s: %s", key, value);
  2160. return r;
  2161. }
  2162. static int set_config_values(struct cache *cache, int argc, const char **argv)
  2163. {
  2164. int r = 0;
  2165. if (argc & 1) {
  2166. DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
  2167. return -EINVAL;
  2168. }
  2169. while (argc) {
  2170. r = set_config_value(cache, argv[0], argv[1]);
  2171. if (r)
  2172. break;
  2173. argc -= 2;
  2174. argv += 2;
  2175. }
  2176. return r;
  2177. }
  2178. static int create_cache_policy(struct cache *cache, struct cache_args *ca,
  2179. char **error)
  2180. {
  2181. struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
  2182. cache->cache_size,
  2183. cache->origin_sectors,
  2184. cache->sectors_per_block);
  2185. if (IS_ERR(p)) {
  2186. *error = "Error creating cache's policy";
  2187. return PTR_ERR(p);
  2188. }
  2189. cache->policy = p;
  2190. return 0;
  2191. }
  2192. /*
  2193. * We want the discard block size to be at least the size of the cache
  2194. * block size and have no more than 2^14 discard blocks across the origin.
  2195. */
  2196. #define MAX_DISCARD_BLOCKS (1 << 14)
  2197. static bool too_many_discard_blocks(sector_t discard_block_size,
  2198. sector_t origin_size)
  2199. {
  2200. (void) sector_div(origin_size, discard_block_size);
  2201. return origin_size > MAX_DISCARD_BLOCKS;
  2202. }
  2203. static sector_t calculate_discard_block_size(sector_t cache_block_size,
  2204. sector_t origin_size)
  2205. {
  2206. sector_t discard_block_size = cache_block_size;
  2207. if (origin_size)
  2208. while (too_many_discard_blocks(discard_block_size, origin_size))
  2209. discard_block_size *= 2;
  2210. return discard_block_size;
  2211. }
  2212. static void set_cache_size(struct cache *cache, dm_cblock_t size)
  2213. {
  2214. dm_block_t nr_blocks = from_cblock(size);
  2215. if (nr_blocks > (1 << 20) && cache->cache_size != size)
  2216. DMWARN_LIMIT("You have created a cache device with a lot of individual cache blocks (%llu)\n"
  2217. "All these mappings can consume a lot of kernel memory, and take some time to read/write.\n"
  2218. "Please consider increasing the cache block size to reduce the overall cache block count.",
  2219. (unsigned long long) nr_blocks);
  2220. cache->cache_size = size;
  2221. }
  2222. #define DEFAULT_MIGRATION_THRESHOLD 2048
  2223. static int cache_create(struct cache_args *ca, struct cache **result)
  2224. {
  2225. int r = 0;
  2226. char **error = &ca->ti->error;
  2227. struct cache *cache;
  2228. struct dm_target *ti = ca->ti;
  2229. dm_block_t origin_blocks;
  2230. struct dm_cache_metadata *cmd;
  2231. bool may_format = ca->features.mode == CM_WRITE;
  2232. cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  2233. if (!cache)
  2234. return -ENOMEM;
  2235. cache->ti = ca->ti;
  2236. ti->private = cache;
  2237. ti->num_flush_bios = 2;
  2238. ti->flush_supported = true;
  2239. ti->num_discard_bios = 1;
  2240. ti->discards_supported = true;
  2241. ti->discard_zeroes_data_unsupported = true;
  2242. ti->split_discard_bios = false;
  2243. cache->features = ca->features;
  2244. ti->per_io_data_size = get_per_bio_data_size(cache);
  2245. cache->callbacks.congested_fn = cache_is_congested;
  2246. dm_table_add_target_callbacks(ti->table, &cache->callbacks);
  2247. cache->metadata_dev = ca->metadata_dev;
  2248. cache->origin_dev = ca->origin_dev;
  2249. cache->cache_dev = ca->cache_dev;
  2250. ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
  2251. /* FIXME: factor out this whole section */
  2252. origin_blocks = cache->origin_sectors = ca->origin_sectors;
  2253. origin_blocks = block_div(origin_blocks, ca->block_size);
  2254. cache->origin_blocks = to_oblock(origin_blocks);
  2255. cache->sectors_per_block = ca->block_size;
  2256. if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
  2257. r = -EINVAL;
  2258. goto bad;
  2259. }
  2260. if (ca->block_size & (ca->block_size - 1)) {
  2261. dm_block_t cache_size = ca->cache_sectors;
  2262. cache->sectors_per_block_shift = -1;
  2263. cache_size = block_div(cache_size, ca->block_size);
  2264. set_cache_size(cache, to_cblock(cache_size));
  2265. } else {
  2266. cache->sectors_per_block_shift = __ffs(ca->block_size);
  2267. set_cache_size(cache, to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift));
  2268. }
  2269. r = create_cache_policy(cache, ca, error);
  2270. if (r)
  2271. goto bad;
  2272. cache->policy_nr_args = ca->policy_argc;
  2273. cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
  2274. r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
  2275. if (r) {
  2276. *error = "Error setting cache policy's config values";
  2277. goto bad;
  2278. }
  2279. cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
  2280. ca->block_size, may_format,
  2281. dm_cache_policy_get_hint_size(cache->policy));
  2282. if (IS_ERR(cmd)) {
  2283. *error = "Error creating metadata object";
  2284. r = PTR_ERR(cmd);
  2285. goto bad;
  2286. }
  2287. cache->cmd = cmd;
  2288. set_cache_mode(cache, CM_WRITE);
  2289. if (get_cache_mode(cache) != CM_WRITE) {
  2290. *error = "Unable to get write access to metadata, please check/repair metadata.";
  2291. r = -EINVAL;
  2292. goto bad;
  2293. }
  2294. if (passthrough_mode(&cache->features)) {
  2295. bool all_clean;
  2296. r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
  2297. if (r) {
  2298. *error = "dm_cache_metadata_all_clean() failed";
  2299. goto bad;
  2300. }
  2301. if (!all_clean) {
  2302. *error = "Cannot enter passthrough mode unless all blocks are clean";
  2303. r = -EINVAL;
  2304. goto bad;
  2305. }
  2306. }
  2307. spin_lock_init(&cache->lock);
  2308. INIT_LIST_HEAD(&cache->deferred_cells);
  2309. bio_list_init(&cache->deferred_bios);
  2310. bio_list_init(&cache->deferred_flush_bios);
  2311. bio_list_init(&cache->deferred_writethrough_bios);
  2312. INIT_LIST_HEAD(&cache->quiesced_migrations);
  2313. INIT_LIST_HEAD(&cache->completed_migrations);
  2314. INIT_LIST_HEAD(&cache->need_commit_migrations);
  2315. atomic_set(&cache->nr_allocated_migrations, 0);
  2316. atomic_set(&cache->nr_io_migrations, 0);
  2317. init_waitqueue_head(&cache->migration_wait);
  2318. init_waitqueue_head(&cache->quiescing_wait);
  2319. atomic_set(&cache->quiescing, 0);
  2320. atomic_set(&cache->quiescing_ack, 0);
  2321. r = -ENOMEM;
  2322. atomic_set(&cache->nr_dirty, 0);
  2323. cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
  2324. if (!cache->dirty_bitset) {
  2325. *error = "could not allocate dirty bitset";
  2326. goto bad;
  2327. }
  2328. clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
  2329. cache->discard_block_size =
  2330. calculate_discard_block_size(cache->sectors_per_block,
  2331. cache->origin_sectors);
  2332. cache->discard_nr_blocks = to_dblock(dm_sector_div_up(cache->origin_sectors,
  2333. cache->discard_block_size));
  2334. cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
  2335. if (!cache->discard_bitset) {
  2336. *error = "could not allocate discard bitset";
  2337. goto bad;
  2338. }
  2339. clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
  2340. cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  2341. if (IS_ERR(cache->copier)) {
  2342. *error = "could not create kcopyd client";
  2343. r = PTR_ERR(cache->copier);
  2344. goto bad;
  2345. }
  2346. cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
  2347. if (!cache->wq) {
  2348. *error = "could not create workqueue for metadata object";
  2349. goto bad;
  2350. }
  2351. INIT_WORK(&cache->worker, do_worker);
  2352. INIT_DELAYED_WORK(&cache->waker, do_waker);
  2353. cache->last_commit_jiffies = jiffies;
  2354. cache->prison = dm_bio_prison_create();
  2355. if (!cache->prison) {
  2356. *error = "could not create bio prison";
  2357. goto bad;
  2358. }
  2359. cache->all_io_ds = dm_deferred_set_create();
  2360. if (!cache->all_io_ds) {
  2361. *error = "could not create all_io deferred set";
  2362. goto bad;
  2363. }
  2364. cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
  2365. migration_cache);
  2366. if (!cache->migration_pool) {
  2367. *error = "Error creating cache's migration mempool";
  2368. goto bad;
  2369. }
  2370. cache->need_tick_bio = true;
  2371. cache->sized = false;
  2372. cache->invalidate = false;
  2373. cache->commit_requested = false;
  2374. cache->loaded_mappings = false;
  2375. cache->loaded_discards = false;
  2376. load_stats(cache);
  2377. atomic_set(&cache->stats.demotion, 0);
  2378. atomic_set(&cache->stats.promotion, 0);
  2379. atomic_set(&cache->stats.copies_avoided, 0);
  2380. atomic_set(&cache->stats.cache_cell_clash, 0);
  2381. atomic_set(&cache->stats.commit_count, 0);
  2382. atomic_set(&cache->stats.discard_count, 0);
  2383. spin_lock_init(&cache->invalidation_lock);
  2384. INIT_LIST_HEAD(&cache->invalidation_requests);
  2385. iot_init(&cache->origin_tracker);
  2386. *result = cache;
  2387. return 0;
  2388. bad:
  2389. destroy(cache);
  2390. return r;
  2391. }
  2392. static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
  2393. {
  2394. unsigned i;
  2395. const char **copy;
  2396. copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
  2397. if (!copy)
  2398. return -ENOMEM;
  2399. for (i = 0; i < argc; i++) {
  2400. copy[i] = kstrdup(argv[i], GFP_KERNEL);
  2401. if (!copy[i]) {
  2402. while (i--)
  2403. kfree(copy[i]);
  2404. kfree(copy);
  2405. return -ENOMEM;
  2406. }
  2407. }
  2408. cache->nr_ctr_args = argc;
  2409. cache->ctr_args = copy;
  2410. return 0;
  2411. }
  2412. static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
  2413. {
  2414. int r = -EINVAL;
  2415. struct cache_args *ca;
  2416. struct cache *cache = NULL;
  2417. ca = kzalloc(sizeof(*ca), GFP_KERNEL);
  2418. if (!ca) {
  2419. ti->error = "Error allocating memory for cache";
  2420. return -ENOMEM;
  2421. }
  2422. ca->ti = ti;
  2423. r = parse_cache_args(ca, argc, argv, &ti->error);
  2424. if (r)
  2425. goto out;
  2426. r = cache_create(ca, &cache);
  2427. if (r)
  2428. goto out;
  2429. r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
  2430. if (r) {
  2431. destroy(cache);
  2432. goto out;
  2433. }
  2434. ti->private = cache;
  2435. out:
  2436. destroy_cache_args(ca);
  2437. return r;
  2438. }
  2439. /*----------------------------------------------------------------*/
  2440. static int cache_map(struct dm_target *ti, struct bio *bio)
  2441. {
  2442. struct cache *cache = ti->private;
  2443. int r;
  2444. struct dm_bio_prison_cell *cell = NULL;
  2445. dm_oblock_t block = get_bio_block(cache, bio);
  2446. size_t pb_data_size = get_per_bio_data_size(cache);
  2447. bool can_migrate = false;
  2448. bool fast_promotion;
  2449. struct policy_result lookup_result;
  2450. struct per_bio_data *pb = init_per_bio_data(bio, pb_data_size);
  2451. struct old_oblock_lock ool;
  2452. ool.locker.fn = null_locker;
  2453. if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
  2454. /*
  2455. * This can only occur if the io goes to a partial block at
  2456. * the end of the origin device. We don't cache these.
  2457. * Just remap to the origin and carry on.
  2458. */
  2459. remap_to_origin(cache, bio);
  2460. accounted_begin(cache, bio);
  2461. return DM_MAPIO_REMAPPED;
  2462. }
  2463. if (discard_or_flush(bio)) {
  2464. defer_bio(cache, bio);
  2465. return DM_MAPIO_SUBMITTED;
  2466. }
  2467. /*
  2468. * Check to see if that block is currently migrating.
  2469. */
  2470. cell = alloc_prison_cell(cache);
  2471. if (!cell) {
  2472. defer_bio(cache, bio);
  2473. return DM_MAPIO_SUBMITTED;
  2474. }
  2475. r = bio_detain(cache, block, bio, cell,
  2476. (cell_free_fn) free_prison_cell,
  2477. cache, &cell);
  2478. if (r) {
  2479. if (r < 0)
  2480. defer_bio(cache, bio);
  2481. return DM_MAPIO_SUBMITTED;
  2482. }
  2483. fast_promotion = is_discarded_oblock(cache, block) || bio_writes_complete_block(cache, bio);
  2484. r = policy_map(cache->policy, block, false, can_migrate, fast_promotion,
  2485. bio, &ool.locker, &lookup_result);
  2486. if (r == -EWOULDBLOCK) {
  2487. cell_defer(cache, cell, true);
  2488. return DM_MAPIO_SUBMITTED;
  2489. } else if (r) {
  2490. DMERR_LIMIT("%s: Unexpected return from cache replacement policy: %d",
  2491. cache_device_name(cache), r);
  2492. cell_defer(cache, cell, false);
  2493. bio_io_error(bio);
  2494. return DM_MAPIO_SUBMITTED;
  2495. }
  2496. r = DM_MAPIO_REMAPPED;
  2497. switch (lookup_result.op) {
  2498. case POLICY_HIT:
  2499. if (passthrough_mode(&cache->features)) {
  2500. if (bio_data_dir(bio) == WRITE) {
  2501. /*
  2502. * We need to invalidate this block, so
  2503. * defer for the worker thread.
  2504. */
  2505. cell_defer(cache, cell, true);
  2506. r = DM_MAPIO_SUBMITTED;
  2507. } else {
  2508. inc_miss_counter(cache, bio);
  2509. remap_to_origin_clear_discard(cache, bio, block);
  2510. accounted_begin(cache, bio);
  2511. inc_ds(cache, bio, cell);
  2512. // FIXME: we want to remap hits or misses straight
  2513. // away rather than passing over to the worker.
  2514. cell_defer(cache, cell, false);
  2515. }
  2516. } else {
  2517. inc_hit_counter(cache, bio);
  2518. if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) &&
  2519. !is_dirty(cache, lookup_result.cblock)) {
  2520. remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
  2521. accounted_begin(cache, bio);
  2522. inc_ds(cache, bio, cell);
  2523. cell_defer(cache, cell, false);
  2524. } else
  2525. remap_cell_to_cache_dirty(cache, cell, block, lookup_result.cblock, false);
  2526. }
  2527. break;
  2528. case POLICY_MISS:
  2529. inc_miss_counter(cache, bio);
  2530. if (pb->req_nr != 0) {
  2531. /*
  2532. * This is a duplicate writethrough io that is no
  2533. * longer needed because the block has been demoted.
  2534. */
  2535. bio_endio(bio);
  2536. // FIXME: remap everything as a miss
  2537. cell_defer(cache, cell, false);
  2538. r = DM_MAPIO_SUBMITTED;
  2539. } else
  2540. remap_cell_to_origin_clear_discard(cache, cell, block, false);
  2541. break;
  2542. default:
  2543. DMERR_LIMIT("%s: %s: erroring bio: unknown policy op: %u",
  2544. cache_device_name(cache), __func__,
  2545. (unsigned) lookup_result.op);
  2546. cell_defer(cache, cell, false);
  2547. bio_io_error(bio);
  2548. r = DM_MAPIO_SUBMITTED;
  2549. }
  2550. return r;
  2551. }
  2552. static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
  2553. {
  2554. struct cache *cache = ti->private;
  2555. unsigned long flags;
  2556. size_t pb_data_size = get_per_bio_data_size(cache);
  2557. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  2558. if (pb->tick) {
  2559. policy_tick(cache->policy, false);
  2560. spin_lock_irqsave(&cache->lock, flags);
  2561. cache->need_tick_bio = true;
  2562. spin_unlock_irqrestore(&cache->lock, flags);
  2563. }
  2564. check_for_quiesced_migrations(cache, pb);
  2565. accounted_complete(cache, bio);
  2566. return 0;
  2567. }
  2568. static int write_dirty_bitset(struct cache *cache)
  2569. {
  2570. unsigned i, r;
  2571. if (get_cache_mode(cache) >= CM_READ_ONLY)
  2572. return -EINVAL;
  2573. for (i = 0; i < from_cblock(cache->cache_size); i++) {
  2574. r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
  2575. is_dirty(cache, to_cblock(i)));
  2576. if (r) {
  2577. metadata_operation_failed(cache, "dm_cache_set_dirty", r);
  2578. return r;
  2579. }
  2580. }
  2581. return 0;
  2582. }
  2583. static int write_discard_bitset(struct cache *cache)
  2584. {
  2585. unsigned i, r;
  2586. if (get_cache_mode(cache) >= CM_READ_ONLY)
  2587. return -EINVAL;
  2588. r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
  2589. cache->discard_nr_blocks);
  2590. if (r) {
  2591. DMERR("%s: could not resize on-disk discard bitset", cache_device_name(cache));
  2592. metadata_operation_failed(cache, "dm_cache_discard_bitset_resize", r);
  2593. return r;
  2594. }
  2595. for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
  2596. r = dm_cache_set_discard(cache->cmd, to_dblock(i),
  2597. is_discarded(cache, to_dblock(i)));
  2598. if (r) {
  2599. metadata_operation_failed(cache, "dm_cache_set_discard", r);
  2600. return r;
  2601. }
  2602. }
  2603. return 0;
  2604. }
  2605. static int write_hints(struct cache *cache)
  2606. {
  2607. int r;
  2608. if (get_cache_mode(cache) >= CM_READ_ONLY)
  2609. return -EINVAL;
  2610. r = dm_cache_write_hints(cache->cmd, cache->policy);
  2611. if (r) {
  2612. metadata_operation_failed(cache, "dm_cache_write_hints", r);
  2613. return r;
  2614. }
  2615. return 0;
  2616. }
  2617. /*
  2618. * returns true on success
  2619. */
  2620. static bool sync_metadata(struct cache *cache)
  2621. {
  2622. int r1, r2, r3, r4;
  2623. r1 = write_dirty_bitset(cache);
  2624. if (r1)
  2625. DMERR("%s: could not write dirty bitset", cache_device_name(cache));
  2626. r2 = write_discard_bitset(cache);
  2627. if (r2)
  2628. DMERR("%s: could not write discard bitset", cache_device_name(cache));
  2629. save_stats(cache);
  2630. r3 = write_hints(cache);
  2631. if (r3)
  2632. DMERR("%s: could not write hints", cache_device_name(cache));
  2633. /*
  2634. * If writing the above metadata failed, we still commit, but don't
  2635. * set the clean shutdown flag. This will effectively force every
  2636. * dirty bit to be set on reload.
  2637. */
  2638. r4 = commit(cache, !r1 && !r2 && !r3);
  2639. if (r4)
  2640. DMERR("%s: could not write cache metadata", cache_device_name(cache));
  2641. return !r1 && !r2 && !r3 && !r4;
  2642. }
  2643. static void cache_postsuspend(struct dm_target *ti)
  2644. {
  2645. struct cache *cache = ti->private;
  2646. start_quiescing(cache);
  2647. wait_for_migrations(cache);
  2648. stop_worker(cache);
  2649. requeue_deferred_bios(cache);
  2650. requeue_deferred_cells(cache);
  2651. stop_quiescing(cache);
  2652. if (get_cache_mode(cache) == CM_WRITE)
  2653. (void) sync_metadata(cache);
  2654. }
  2655. static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
  2656. bool dirty, uint32_t hint, bool hint_valid)
  2657. {
  2658. int r;
  2659. struct cache *cache = context;
  2660. r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
  2661. if (r)
  2662. return r;
  2663. if (dirty)
  2664. set_dirty(cache, oblock, cblock);
  2665. else
  2666. clear_dirty(cache, oblock, cblock);
  2667. return 0;
  2668. }
  2669. /*
  2670. * The discard block size in the on disk metadata is not
  2671. * neccessarily the same as we're currently using. So we have to
  2672. * be careful to only set the discarded attribute if we know it
  2673. * covers a complete block of the new size.
  2674. */
  2675. struct discard_load_info {
  2676. struct cache *cache;
  2677. /*
  2678. * These blocks are sized using the on disk dblock size, rather
  2679. * than the current one.
  2680. */
  2681. dm_block_t block_size;
  2682. dm_block_t discard_begin, discard_end;
  2683. };
  2684. static void discard_load_info_init(struct cache *cache,
  2685. struct discard_load_info *li)
  2686. {
  2687. li->cache = cache;
  2688. li->discard_begin = li->discard_end = 0;
  2689. }
  2690. static void set_discard_range(struct discard_load_info *li)
  2691. {
  2692. sector_t b, e;
  2693. if (li->discard_begin == li->discard_end)
  2694. return;
  2695. /*
  2696. * Convert to sectors.
  2697. */
  2698. b = li->discard_begin * li->block_size;
  2699. e = li->discard_end * li->block_size;
  2700. /*
  2701. * Then convert back to the current dblock size.
  2702. */
  2703. b = dm_sector_div_up(b, li->cache->discard_block_size);
  2704. sector_div(e, li->cache->discard_block_size);
  2705. /*
  2706. * The origin may have shrunk, so we need to check we're still in
  2707. * bounds.
  2708. */
  2709. if (e > from_dblock(li->cache->discard_nr_blocks))
  2710. e = from_dblock(li->cache->discard_nr_blocks);
  2711. for (; b < e; b++)
  2712. set_discard(li->cache, to_dblock(b));
  2713. }
  2714. static int load_discard(void *context, sector_t discard_block_size,
  2715. dm_dblock_t dblock, bool discard)
  2716. {
  2717. struct discard_load_info *li = context;
  2718. li->block_size = discard_block_size;
  2719. if (discard) {
  2720. if (from_dblock(dblock) == li->discard_end)
  2721. /*
  2722. * We're already in a discard range, just extend it.
  2723. */
  2724. li->discard_end = li->discard_end + 1ULL;
  2725. else {
  2726. /*
  2727. * Emit the old range and start a new one.
  2728. */
  2729. set_discard_range(li);
  2730. li->discard_begin = from_dblock(dblock);
  2731. li->discard_end = li->discard_begin + 1ULL;
  2732. }
  2733. } else {
  2734. set_discard_range(li);
  2735. li->discard_begin = li->discard_end = 0;
  2736. }
  2737. return 0;
  2738. }
  2739. static dm_cblock_t get_cache_dev_size(struct cache *cache)
  2740. {
  2741. sector_t size = get_dev_size(cache->cache_dev);
  2742. (void) sector_div(size, cache->sectors_per_block);
  2743. return to_cblock(size);
  2744. }
  2745. static bool can_resize(struct cache *cache, dm_cblock_t new_size)
  2746. {
  2747. if (from_cblock(new_size) > from_cblock(cache->cache_size))
  2748. return true;
  2749. /*
  2750. * We can't drop a dirty block when shrinking the cache.
  2751. */
  2752. while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
  2753. new_size = to_cblock(from_cblock(new_size) + 1);
  2754. if (is_dirty(cache, new_size)) {
  2755. DMERR("%s: unable to shrink cache; cache block %llu is dirty",
  2756. cache_device_name(cache),
  2757. (unsigned long long) from_cblock(new_size));
  2758. return false;
  2759. }
  2760. }
  2761. return true;
  2762. }
  2763. static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
  2764. {
  2765. int r;
  2766. r = dm_cache_resize(cache->cmd, new_size);
  2767. if (r) {
  2768. DMERR("%s: could not resize cache metadata", cache_device_name(cache));
  2769. metadata_operation_failed(cache, "dm_cache_resize", r);
  2770. return r;
  2771. }
  2772. set_cache_size(cache, new_size);
  2773. return 0;
  2774. }
  2775. static int cache_preresume(struct dm_target *ti)
  2776. {
  2777. int r = 0;
  2778. struct cache *cache = ti->private;
  2779. dm_cblock_t csize = get_cache_dev_size(cache);
  2780. /*
  2781. * Check to see if the cache has resized.
  2782. */
  2783. if (!cache->sized) {
  2784. r = resize_cache_dev(cache, csize);
  2785. if (r)
  2786. return r;
  2787. cache->sized = true;
  2788. } else if (csize != cache->cache_size) {
  2789. if (!can_resize(cache, csize))
  2790. return -EINVAL;
  2791. r = resize_cache_dev(cache, csize);
  2792. if (r)
  2793. return r;
  2794. }
  2795. if (!cache->loaded_mappings) {
  2796. r = dm_cache_load_mappings(cache->cmd, cache->policy,
  2797. load_mapping, cache);
  2798. if (r) {
  2799. DMERR("%s: could not load cache mappings", cache_device_name(cache));
  2800. metadata_operation_failed(cache, "dm_cache_load_mappings", r);
  2801. return r;
  2802. }
  2803. cache->loaded_mappings = true;
  2804. }
  2805. if (!cache->loaded_discards) {
  2806. struct discard_load_info li;
  2807. /*
  2808. * The discard bitset could have been resized, or the
  2809. * discard block size changed. To be safe we start by
  2810. * setting every dblock to not discarded.
  2811. */
  2812. clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
  2813. discard_load_info_init(cache, &li);
  2814. r = dm_cache_load_discards(cache->cmd, load_discard, &li);
  2815. if (r) {
  2816. DMERR("%s: could not load origin discards", cache_device_name(cache));
  2817. metadata_operation_failed(cache, "dm_cache_load_discards", r);
  2818. return r;
  2819. }
  2820. set_discard_range(&li);
  2821. cache->loaded_discards = true;
  2822. }
  2823. return r;
  2824. }
  2825. static void cache_resume(struct dm_target *ti)
  2826. {
  2827. struct cache *cache = ti->private;
  2828. cache->need_tick_bio = true;
  2829. do_waker(&cache->waker.work);
  2830. }
  2831. /*
  2832. * Status format:
  2833. *
  2834. * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
  2835. * <cache block size> <#used cache blocks>/<#total cache blocks>
  2836. * <#read hits> <#read misses> <#write hits> <#write misses>
  2837. * <#demotions> <#promotions> <#dirty>
  2838. * <#features> <features>*
  2839. * <#core args> <core args>
  2840. * <policy name> <#policy args> <policy args>* <cache metadata mode> <needs_check>
  2841. */
  2842. static void cache_status(struct dm_target *ti, status_type_t type,
  2843. unsigned status_flags, char *result, unsigned maxlen)
  2844. {
  2845. int r = 0;
  2846. unsigned i;
  2847. ssize_t sz = 0;
  2848. dm_block_t nr_free_blocks_metadata = 0;
  2849. dm_block_t nr_blocks_metadata = 0;
  2850. char buf[BDEVNAME_SIZE];
  2851. struct cache *cache = ti->private;
  2852. dm_cblock_t residency;
  2853. bool needs_check;
  2854. switch (type) {
  2855. case STATUSTYPE_INFO:
  2856. if (get_cache_mode(cache) == CM_FAIL) {
  2857. DMEMIT("Fail");
  2858. break;
  2859. }
  2860. /* Commit to ensure statistics aren't out-of-date */
  2861. if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
  2862. (void) commit(cache, false);
  2863. r = dm_cache_get_free_metadata_block_count(cache->cmd, &nr_free_blocks_metadata);
  2864. if (r) {
  2865. DMERR("%s: dm_cache_get_free_metadata_block_count returned %d",
  2866. cache_device_name(cache), r);
  2867. goto err;
  2868. }
  2869. r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
  2870. if (r) {
  2871. DMERR("%s: dm_cache_get_metadata_dev_size returned %d",
  2872. cache_device_name(cache), r);
  2873. goto err;
  2874. }
  2875. residency = policy_residency(cache->policy);
  2876. DMEMIT("%u %llu/%llu %u %llu/%llu %u %u %u %u %u %u %lu ",
  2877. (unsigned)DM_CACHE_METADATA_BLOCK_SIZE,
  2878. (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
  2879. (unsigned long long)nr_blocks_metadata,
  2880. cache->sectors_per_block,
  2881. (unsigned long long) from_cblock(residency),
  2882. (unsigned long long) from_cblock(cache->cache_size),
  2883. (unsigned) atomic_read(&cache->stats.read_hit),
  2884. (unsigned) atomic_read(&cache->stats.read_miss),
  2885. (unsigned) atomic_read(&cache->stats.write_hit),
  2886. (unsigned) atomic_read(&cache->stats.write_miss),
  2887. (unsigned) atomic_read(&cache->stats.demotion),
  2888. (unsigned) atomic_read(&cache->stats.promotion),
  2889. (unsigned long) atomic_read(&cache->nr_dirty));
  2890. if (writethrough_mode(&cache->features))
  2891. DMEMIT("1 writethrough ");
  2892. else if (passthrough_mode(&cache->features))
  2893. DMEMIT("1 passthrough ");
  2894. else if (writeback_mode(&cache->features))
  2895. DMEMIT("1 writeback ");
  2896. else {
  2897. DMERR("%s: internal error: unknown io mode: %d",
  2898. cache_device_name(cache), (int) cache->features.io_mode);
  2899. goto err;
  2900. }
  2901. DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
  2902. DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
  2903. if (sz < maxlen) {
  2904. r = policy_emit_config_values(cache->policy, result, maxlen, &sz);
  2905. if (r)
  2906. DMERR("%s: policy_emit_config_values returned %d",
  2907. cache_device_name(cache), r);
  2908. }
  2909. if (get_cache_mode(cache) == CM_READ_ONLY)
  2910. DMEMIT("ro ");
  2911. else
  2912. DMEMIT("rw ");
  2913. r = dm_cache_metadata_needs_check(cache->cmd, &needs_check);
  2914. if (r || needs_check)
  2915. DMEMIT("needs_check ");
  2916. else
  2917. DMEMIT("- ");
  2918. break;
  2919. case STATUSTYPE_TABLE:
  2920. format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
  2921. DMEMIT("%s ", buf);
  2922. format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
  2923. DMEMIT("%s ", buf);
  2924. format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
  2925. DMEMIT("%s", buf);
  2926. for (i = 0; i < cache->nr_ctr_args - 1; i++)
  2927. DMEMIT(" %s", cache->ctr_args[i]);
  2928. if (cache->nr_ctr_args)
  2929. DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
  2930. }
  2931. return;
  2932. err:
  2933. DMEMIT("Error");
  2934. }
  2935. /*
  2936. * A cache block range can take two forms:
  2937. *
  2938. * i) A single cblock, eg. '3456'
  2939. * ii) A begin and end cblock with dots between, eg. 123-234
  2940. */
  2941. static int parse_cblock_range(struct cache *cache, const char *str,
  2942. struct cblock_range *result)
  2943. {
  2944. char dummy;
  2945. uint64_t b, e;
  2946. int r;
  2947. /*
  2948. * Try and parse form (ii) first.
  2949. */
  2950. r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
  2951. if (r < 0)
  2952. return r;
  2953. if (r == 2) {
  2954. result->begin = to_cblock(b);
  2955. result->end = to_cblock(e);
  2956. return 0;
  2957. }
  2958. /*
  2959. * That didn't work, try form (i).
  2960. */
  2961. r = sscanf(str, "%llu%c", &b, &dummy);
  2962. if (r < 0)
  2963. return r;
  2964. if (r == 1) {
  2965. result->begin = to_cblock(b);
  2966. result->end = to_cblock(from_cblock(result->begin) + 1u);
  2967. return 0;
  2968. }
  2969. DMERR("%s: invalid cblock range '%s'", cache_device_name(cache), str);
  2970. return -EINVAL;
  2971. }
  2972. static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
  2973. {
  2974. uint64_t b = from_cblock(range->begin);
  2975. uint64_t e = from_cblock(range->end);
  2976. uint64_t n = from_cblock(cache->cache_size);
  2977. if (b >= n) {
  2978. DMERR("%s: begin cblock out of range: %llu >= %llu",
  2979. cache_device_name(cache), b, n);
  2980. return -EINVAL;
  2981. }
  2982. if (e > n) {
  2983. DMERR("%s: end cblock out of range: %llu > %llu",
  2984. cache_device_name(cache), e, n);
  2985. return -EINVAL;
  2986. }
  2987. if (b >= e) {
  2988. DMERR("%s: invalid cblock range: %llu >= %llu",
  2989. cache_device_name(cache), b, e);
  2990. return -EINVAL;
  2991. }
  2992. return 0;
  2993. }
  2994. static int request_invalidation(struct cache *cache, struct cblock_range *range)
  2995. {
  2996. struct invalidation_request req;
  2997. INIT_LIST_HEAD(&req.list);
  2998. req.cblocks = range;
  2999. atomic_set(&req.complete, 0);
  3000. req.err = 0;
  3001. init_waitqueue_head(&req.result_wait);
  3002. spin_lock(&cache->invalidation_lock);
  3003. list_add(&req.list, &cache->invalidation_requests);
  3004. spin_unlock(&cache->invalidation_lock);
  3005. wake_worker(cache);
  3006. wait_event(req.result_wait, atomic_read(&req.complete));
  3007. return req.err;
  3008. }
  3009. static int process_invalidate_cblocks_message(struct cache *cache, unsigned count,
  3010. const char **cblock_ranges)
  3011. {
  3012. int r = 0;
  3013. unsigned i;
  3014. struct cblock_range range;
  3015. if (!passthrough_mode(&cache->features)) {
  3016. DMERR("%s: cache has to be in passthrough mode for invalidation",
  3017. cache_device_name(cache));
  3018. return -EPERM;
  3019. }
  3020. for (i = 0; i < count; i++) {
  3021. r = parse_cblock_range(cache, cblock_ranges[i], &range);
  3022. if (r)
  3023. break;
  3024. r = validate_cblock_range(cache, &range);
  3025. if (r)
  3026. break;
  3027. /*
  3028. * Pass begin and end origin blocks to the worker and wake it.
  3029. */
  3030. r = request_invalidation(cache, &range);
  3031. if (r)
  3032. break;
  3033. }
  3034. return r;
  3035. }
  3036. /*
  3037. * Supports
  3038. * "<key> <value>"
  3039. * and
  3040. * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
  3041. *
  3042. * The key migration_threshold is supported by the cache target core.
  3043. */
  3044. static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
  3045. {
  3046. struct cache *cache = ti->private;
  3047. if (!argc)
  3048. return -EINVAL;
  3049. if (get_cache_mode(cache) >= CM_READ_ONLY) {
  3050. DMERR("%s: unable to service cache target messages in READ_ONLY or FAIL mode",
  3051. cache_device_name(cache));
  3052. return -EOPNOTSUPP;
  3053. }
  3054. if (!strcasecmp(argv[0], "invalidate_cblocks"))
  3055. return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
  3056. if (argc != 2)
  3057. return -EINVAL;
  3058. return set_config_value(cache, argv[0], argv[1]);
  3059. }
  3060. static int cache_iterate_devices(struct dm_target *ti,
  3061. iterate_devices_callout_fn fn, void *data)
  3062. {
  3063. int r = 0;
  3064. struct cache *cache = ti->private;
  3065. r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
  3066. if (!r)
  3067. r = fn(ti, cache->origin_dev, 0, ti->len, data);
  3068. return r;
  3069. }
  3070. static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
  3071. {
  3072. /*
  3073. * FIXME: these limits may be incompatible with the cache device
  3074. */
  3075. limits->max_discard_sectors = min_t(sector_t, cache->discard_block_size * 1024,
  3076. cache->origin_sectors);
  3077. limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
  3078. }
  3079. static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
  3080. {
  3081. struct cache *cache = ti->private;
  3082. uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
  3083. /*
  3084. * If the system-determined stacked limits are compatible with the
  3085. * cache's blocksize (io_opt is a factor) do not override them.
  3086. */
  3087. if (io_opt_sectors < cache->sectors_per_block ||
  3088. do_div(io_opt_sectors, cache->sectors_per_block)) {
  3089. blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT);
  3090. blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
  3091. }
  3092. set_discard_limits(cache, limits);
  3093. }
  3094. /*----------------------------------------------------------------*/
  3095. static struct target_type cache_target = {
  3096. .name = "cache",
  3097. .version = {1, 9, 0},
  3098. .module = THIS_MODULE,
  3099. .ctr = cache_ctr,
  3100. .dtr = cache_dtr,
  3101. .map = cache_map,
  3102. .end_io = cache_end_io,
  3103. .postsuspend = cache_postsuspend,
  3104. .preresume = cache_preresume,
  3105. .resume = cache_resume,
  3106. .status = cache_status,
  3107. .message = cache_message,
  3108. .iterate_devices = cache_iterate_devices,
  3109. .io_hints = cache_io_hints,
  3110. };
  3111. static int __init dm_cache_init(void)
  3112. {
  3113. int r;
  3114. r = dm_register_target(&cache_target);
  3115. if (r) {
  3116. DMERR("cache target registration failed: %d", r);
  3117. return r;
  3118. }
  3119. migration_cache = KMEM_CACHE(dm_cache_migration, 0);
  3120. if (!migration_cache) {
  3121. dm_unregister_target(&cache_target);
  3122. return -ENOMEM;
  3123. }
  3124. return 0;
  3125. }
  3126. static void __exit dm_cache_exit(void)
  3127. {
  3128. dm_unregister_target(&cache_target);
  3129. kmem_cache_destroy(migration_cache);
  3130. }
  3131. module_init(dm_cache_init);
  3132. module_exit(dm_cache_exit);
  3133. MODULE_DESCRIPTION(DM_NAME " cache target");
  3134. MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
  3135. MODULE_LICENSE("GPL");