dm-cache-target.c 83 KB

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