dm-cache-target.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502
  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. struct dm_target_callbacks callbacks;
  311. struct dm_cache_metadata *cmd;
  312. /*
  313. * Metadata is written to this device.
  314. */
  315. struct dm_dev *metadata_dev;
  316. /*
  317. * The slower of the two data devices. Typically a spindle.
  318. */
  319. struct dm_dev *origin_dev;
  320. /*
  321. * The faster of the two data devices. Typically an SSD.
  322. */
  323. struct dm_dev *cache_dev;
  324. /*
  325. * Size of the origin device in _complete_ blocks and native sectors.
  326. */
  327. dm_oblock_t origin_blocks;
  328. sector_t origin_sectors;
  329. /*
  330. * Size of the cache device in blocks.
  331. */
  332. dm_cblock_t cache_size;
  333. /*
  334. * Fields for converting from sectors to blocks.
  335. */
  336. sector_t sectors_per_block;
  337. int sectors_per_block_shift;
  338. spinlock_t lock;
  339. struct bio_list deferred_bios;
  340. sector_t migration_threshold;
  341. wait_queue_head_t migration_wait;
  342. atomic_t nr_allocated_migrations;
  343. /*
  344. * The number of in flight migrations that are performing
  345. * background io. eg, promotion, writeback.
  346. */
  347. atomic_t nr_io_migrations;
  348. struct rw_semaphore quiesce_lock;
  349. /*
  350. * cache_size entries, dirty if set
  351. */
  352. atomic_t nr_dirty;
  353. unsigned long *dirty_bitset;
  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 workqueue_struct *wq;
  368. struct work_struct deferred_bio_worker;
  369. struct work_struct migration_worker;
  370. struct delayed_work waker;
  371. struct dm_bio_prison_v2 *prison;
  372. struct bio_set *bs;
  373. mempool_t *migration_pool;
  374. struct dm_cache_policy *policy;
  375. unsigned policy_nr_args;
  376. bool need_tick_bio:1;
  377. bool sized:1;
  378. bool invalidate:1;
  379. bool commit_requested:1;
  380. bool loaded_mappings:1;
  381. bool loaded_discards:1;
  382. /*
  383. * Cache features such as write-through.
  384. */
  385. struct cache_features features;
  386. struct cache_stats stats;
  387. /*
  388. * Invalidation fields.
  389. */
  390. spinlock_t invalidation_lock;
  391. struct list_head invalidation_requests;
  392. struct io_tracker tracker;
  393. struct work_struct commit_ws;
  394. struct batcher committer;
  395. struct rw_semaphore background_work_lock;
  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 int copy(struct dm_cache_migration *mg, bool promote)
  958. {
  959. int r;
  960. struct dm_io_region o_region, c_region;
  961. struct cache *cache = mg->cache;
  962. o_region.bdev = cache->origin_dev->bdev;
  963. o_region.sector = from_oblock(mg->op->oblock) * cache->sectors_per_block;
  964. o_region.count = cache->sectors_per_block;
  965. c_region.bdev = cache->cache_dev->bdev;
  966. c_region.sector = from_cblock(mg->op->cblock) * cache->sectors_per_block;
  967. c_region.count = cache->sectors_per_block;
  968. if (promote)
  969. r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, &mg->k);
  970. else
  971. r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, &mg->k);
  972. return r;
  973. }
  974. static void bio_drop_shared_lock(struct cache *cache, struct bio *bio)
  975. {
  976. struct per_bio_data *pb = get_per_bio_data(bio);
  977. if (pb->cell && dm_cell_put_v2(cache->prison, pb->cell))
  978. free_prison_cell(cache, pb->cell);
  979. pb->cell = NULL;
  980. }
  981. static void overwrite_endio(struct bio *bio)
  982. {
  983. struct dm_cache_migration *mg = bio->bi_private;
  984. struct cache *cache = mg->cache;
  985. struct per_bio_data *pb = get_per_bio_data(bio);
  986. dm_unhook_bio(&pb->hook_info, bio);
  987. if (bio->bi_status)
  988. mg->k.input = bio->bi_status;
  989. queue_continuation(cache->wq, &mg->k);
  990. }
  991. static void overwrite(struct dm_cache_migration *mg,
  992. void (*continuation)(struct work_struct *))
  993. {
  994. struct bio *bio = mg->overwrite_bio;
  995. struct per_bio_data *pb = get_per_bio_data(bio);
  996. dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
  997. /*
  998. * The overwrite bio is part of the copy operation, as such it does
  999. * not set/clear discard or dirty flags.
  1000. */
  1001. if (mg->op->op == POLICY_PROMOTE)
  1002. remap_to_cache(mg->cache, bio, mg->op->cblock);
  1003. else
  1004. remap_to_origin(mg->cache, bio);
  1005. init_continuation(&mg->k, continuation);
  1006. accounted_request(mg->cache, bio);
  1007. }
  1008. /*
  1009. * Migration steps:
  1010. *
  1011. * 1) exclusive lock preventing WRITEs
  1012. * 2) quiesce
  1013. * 3) copy or issue overwrite bio
  1014. * 4) upgrade to exclusive lock preventing READs and WRITEs
  1015. * 5) quiesce
  1016. * 6) update metadata and commit
  1017. * 7) unlock
  1018. */
  1019. static void mg_complete(struct dm_cache_migration *mg, bool success)
  1020. {
  1021. struct bio_list bios;
  1022. struct cache *cache = mg->cache;
  1023. struct policy_work *op = mg->op;
  1024. dm_cblock_t cblock = op->cblock;
  1025. if (success)
  1026. update_stats(&cache->stats, op->op);
  1027. switch (op->op) {
  1028. case POLICY_PROMOTE:
  1029. clear_discard(cache, oblock_to_dblock(cache, op->oblock));
  1030. policy_complete_background_work(cache->policy, op, success);
  1031. if (mg->overwrite_bio) {
  1032. if (success)
  1033. force_set_dirty(cache, cblock);
  1034. else if (mg->k.input)
  1035. mg->overwrite_bio->bi_status = mg->k.input;
  1036. else
  1037. mg->overwrite_bio->bi_status = BLK_STS_IOERR;
  1038. bio_endio(mg->overwrite_bio);
  1039. } else {
  1040. if (success)
  1041. force_clear_dirty(cache, cblock);
  1042. dec_io_migrations(cache);
  1043. }
  1044. break;
  1045. case POLICY_DEMOTE:
  1046. /*
  1047. * We clear dirty here to update the nr_dirty counter.
  1048. */
  1049. if (success)
  1050. force_clear_dirty(cache, cblock);
  1051. policy_complete_background_work(cache->policy, op, success);
  1052. dec_io_migrations(cache);
  1053. break;
  1054. case POLICY_WRITEBACK:
  1055. if (success)
  1056. force_clear_dirty(cache, cblock);
  1057. policy_complete_background_work(cache->policy, op, success);
  1058. dec_io_migrations(cache);
  1059. break;
  1060. }
  1061. bio_list_init(&bios);
  1062. if (mg->cell) {
  1063. if (dm_cell_unlock_v2(cache->prison, mg->cell, &bios))
  1064. free_prison_cell(cache, mg->cell);
  1065. }
  1066. free_migration(mg);
  1067. defer_bios(cache, &bios);
  1068. wake_migration_worker(cache);
  1069. background_work_end(cache);
  1070. }
  1071. static void mg_success(struct work_struct *ws)
  1072. {
  1073. struct dm_cache_migration *mg = ws_to_mg(ws);
  1074. mg_complete(mg, mg->k.input == 0);
  1075. }
  1076. static void mg_update_metadata(struct work_struct *ws)
  1077. {
  1078. int r;
  1079. struct dm_cache_migration *mg = ws_to_mg(ws);
  1080. struct cache *cache = mg->cache;
  1081. struct policy_work *op = mg->op;
  1082. switch (op->op) {
  1083. case POLICY_PROMOTE:
  1084. r = dm_cache_insert_mapping(cache->cmd, op->cblock, op->oblock);
  1085. if (r) {
  1086. DMERR_LIMIT("%s: migration failed; couldn't insert mapping",
  1087. cache_device_name(cache));
  1088. metadata_operation_failed(cache, "dm_cache_insert_mapping", r);
  1089. mg_complete(mg, false);
  1090. return;
  1091. }
  1092. mg_complete(mg, true);
  1093. break;
  1094. case POLICY_DEMOTE:
  1095. r = dm_cache_remove_mapping(cache->cmd, op->cblock);
  1096. if (r) {
  1097. DMERR_LIMIT("%s: migration failed; couldn't update on disk metadata",
  1098. cache_device_name(cache));
  1099. metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
  1100. mg_complete(mg, false);
  1101. return;
  1102. }
  1103. /*
  1104. * It would be nice if we only had to commit when a REQ_FLUSH
  1105. * comes through. But there's one scenario that we have to
  1106. * look out for:
  1107. *
  1108. * - vblock x in a cache block
  1109. * - domotion occurs
  1110. * - cache block gets reallocated and over written
  1111. * - crash
  1112. *
  1113. * When we recover, because there was no commit the cache will
  1114. * rollback to having the data for vblock x in the cache block.
  1115. * But the cache block has since been overwritten, so it'll end
  1116. * up pointing to data that was never in 'x' during the history
  1117. * of the device.
  1118. *
  1119. * To avoid this issue we require a commit as part of the
  1120. * demotion operation.
  1121. */
  1122. init_continuation(&mg->k, mg_success);
  1123. continue_after_commit(&cache->committer, &mg->k);
  1124. schedule_commit(&cache->committer);
  1125. break;
  1126. case POLICY_WRITEBACK:
  1127. mg_complete(mg, true);
  1128. break;
  1129. }
  1130. }
  1131. static void mg_update_metadata_after_copy(struct work_struct *ws)
  1132. {
  1133. struct dm_cache_migration *mg = ws_to_mg(ws);
  1134. /*
  1135. * Did the copy succeed?
  1136. */
  1137. if (mg->k.input)
  1138. mg_complete(mg, false);
  1139. else
  1140. mg_update_metadata(ws);
  1141. }
  1142. static void mg_upgrade_lock(struct work_struct *ws)
  1143. {
  1144. int r;
  1145. struct dm_cache_migration *mg = ws_to_mg(ws);
  1146. /*
  1147. * Did the copy succeed?
  1148. */
  1149. if (mg->k.input)
  1150. mg_complete(mg, false);
  1151. else {
  1152. /*
  1153. * Now we want the lock to prevent both reads and writes.
  1154. */
  1155. r = dm_cell_lock_promote_v2(mg->cache->prison, mg->cell,
  1156. READ_WRITE_LOCK_LEVEL);
  1157. if (r < 0)
  1158. mg_complete(mg, false);
  1159. else if (r)
  1160. quiesce(mg, mg_update_metadata);
  1161. else
  1162. mg_update_metadata(ws);
  1163. }
  1164. }
  1165. static void mg_full_copy(struct work_struct *ws)
  1166. {
  1167. struct dm_cache_migration *mg = ws_to_mg(ws);
  1168. struct cache *cache = mg->cache;
  1169. struct policy_work *op = mg->op;
  1170. bool is_policy_promote = (op->op == POLICY_PROMOTE);
  1171. if ((!is_policy_promote && !is_dirty(cache, op->cblock)) ||
  1172. is_discarded_oblock(cache, op->oblock)) {
  1173. mg_upgrade_lock(ws);
  1174. return;
  1175. }
  1176. init_continuation(&mg->k, mg_upgrade_lock);
  1177. if (copy(mg, is_policy_promote)) {
  1178. DMERR_LIMIT("%s: migration copy failed", cache_device_name(cache));
  1179. mg->k.input = BLK_STS_IOERR;
  1180. mg_complete(mg, false);
  1181. }
  1182. }
  1183. static void mg_copy(struct work_struct *ws)
  1184. {
  1185. struct dm_cache_migration *mg = ws_to_mg(ws);
  1186. if (mg->overwrite_bio) {
  1187. /*
  1188. * No exclusive lock was held when we last checked if the bio
  1189. * was optimisable. So we have to check again in case things
  1190. * have changed (eg, the block may no longer be discarded).
  1191. */
  1192. if (!optimisable_bio(mg->cache, mg->overwrite_bio, mg->op->oblock)) {
  1193. /*
  1194. * Fallback to a real full copy after doing some tidying up.
  1195. */
  1196. bool rb = bio_detain_shared(mg->cache, mg->op->oblock, mg->overwrite_bio);
  1197. BUG_ON(rb); /* An exclussive lock must _not_ be held for this block */
  1198. mg->overwrite_bio = NULL;
  1199. inc_io_migrations(mg->cache);
  1200. mg_full_copy(ws);
  1201. return;
  1202. }
  1203. /*
  1204. * It's safe to do this here, even though it's new data
  1205. * because all IO has been locked out of the block.
  1206. *
  1207. * mg_lock_writes() already took READ_WRITE_LOCK_LEVEL
  1208. * so _not_ using mg_upgrade_lock() as continutation.
  1209. */
  1210. overwrite(mg, mg_update_metadata_after_copy);
  1211. } else
  1212. mg_full_copy(ws);
  1213. }
  1214. static int mg_lock_writes(struct dm_cache_migration *mg)
  1215. {
  1216. int r;
  1217. struct dm_cell_key_v2 key;
  1218. struct cache *cache = mg->cache;
  1219. struct dm_bio_prison_cell_v2 *prealloc;
  1220. prealloc = alloc_prison_cell(cache);
  1221. if (!prealloc) {
  1222. DMERR_LIMIT("%s: alloc_prison_cell failed", cache_device_name(cache));
  1223. mg_complete(mg, false);
  1224. return -ENOMEM;
  1225. }
  1226. /*
  1227. * Prevent writes to the block, but allow reads to continue.
  1228. * Unless we're using an overwrite bio, in which case we lock
  1229. * everything.
  1230. */
  1231. build_key(mg->op->oblock, oblock_succ(mg->op->oblock), &key);
  1232. r = dm_cell_lock_v2(cache->prison, &key,
  1233. mg->overwrite_bio ? READ_WRITE_LOCK_LEVEL : WRITE_LOCK_LEVEL,
  1234. prealloc, &mg->cell);
  1235. if (r < 0) {
  1236. free_prison_cell(cache, prealloc);
  1237. mg_complete(mg, false);
  1238. return r;
  1239. }
  1240. if (mg->cell != prealloc)
  1241. free_prison_cell(cache, prealloc);
  1242. if (r == 0)
  1243. mg_copy(&mg->k.ws);
  1244. else
  1245. quiesce(mg, mg_copy);
  1246. return 0;
  1247. }
  1248. static int mg_start(struct cache *cache, struct policy_work *op, struct bio *bio)
  1249. {
  1250. struct dm_cache_migration *mg;
  1251. if (!background_work_begin(cache)) {
  1252. policy_complete_background_work(cache->policy, op, false);
  1253. return -EPERM;
  1254. }
  1255. mg = alloc_migration(cache);
  1256. if (!mg) {
  1257. policy_complete_background_work(cache->policy, op, false);
  1258. background_work_end(cache);
  1259. return -ENOMEM;
  1260. }
  1261. mg->op = op;
  1262. mg->overwrite_bio = bio;
  1263. if (!bio)
  1264. inc_io_migrations(cache);
  1265. return mg_lock_writes(mg);
  1266. }
  1267. /*----------------------------------------------------------------
  1268. * invalidation processing
  1269. *--------------------------------------------------------------*/
  1270. static void invalidate_complete(struct dm_cache_migration *mg, bool success)
  1271. {
  1272. struct bio_list bios;
  1273. struct cache *cache = mg->cache;
  1274. bio_list_init(&bios);
  1275. if (dm_cell_unlock_v2(cache->prison, mg->cell, &bios))
  1276. free_prison_cell(cache, mg->cell);
  1277. if (!success && mg->overwrite_bio)
  1278. bio_io_error(mg->overwrite_bio);
  1279. free_migration(mg);
  1280. defer_bios(cache, &bios);
  1281. background_work_end(cache);
  1282. }
  1283. static void invalidate_completed(struct work_struct *ws)
  1284. {
  1285. struct dm_cache_migration *mg = ws_to_mg(ws);
  1286. invalidate_complete(mg, !mg->k.input);
  1287. }
  1288. static int invalidate_cblock(struct cache *cache, dm_cblock_t cblock)
  1289. {
  1290. int r = policy_invalidate_mapping(cache->policy, cblock);
  1291. if (!r) {
  1292. r = dm_cache_remove_mapping(cache->cmd, cblock);
  1293. if (r) {
  1294. DMERR_LIMIT("%s: invalidation failed; couldn't update on disk metadata",
  1295. cache_device_name(cache));
  1296. metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
  1297. }
  1298. } else if (r == -ENODATA) {
  1299. /*
  1300. * Harmless, already unmapped.
  1301. */
  1302. r = 0;
  1303. } else
  1304. DMERR("%s: policy_invalidate_mapping failed", cache_device_name(cache));
  1305. return r;
  1306. }
  1307. static void invalidate_remove(struct work_struct *ws)
  1308. {
  1309. int r;
  1310. struct dm_cache_migration *mg = ws_to_mg(ws);
  1311. struct cache *cache = mg->cache;
  1312. r = invalidate_cblock(cache, mg->invalidate_cblock);
  1313. if (r) {
  1314. invalidate_complete(mg, false);
  1315. return;
  1316. }
  1317. init_continuation(&mg->k, invalidate_completed);
  1318. continue_after_commit(&cache->committer, &mg->k);
  1319. remap_to_origin_clear_discard(cache, mg->overwrite_bio, mg->invalidate_oblock);
  1320. mg->overwrite_bio = NULL;
  1321. schedule_commit(&cache->committer);
  1322. }
  1323. static int invalidate_lock(struct dm_cache_migration *mg)
  1324. {
  1325. int r;
  1326. struct dm_cell_key_v2 key;
  1327. struct cache *cache = mg->cache;
  1328. struct dm_bio_prison_cell_v2 *prealloc;
  1329. prealloc = alloc_prison_cell(cache);
  1330. if (!prealloc) {
  1331. invalidate_complete(mg, false);
  1332. return -ENOMEM;
  1333. }
  1334. build_key(mg->invalidate_oblock, oblock_succ(mg->invalidate_oblock), &key);
  1335. r = dm_cell_lock_v2(cache->prison, &key,
  1336. READ_WRITE_LOCK_LEVEL, prealloc, &mg->cell);
  1337. if (r < 0) {
  1338. free_prison_cell(cache, prealloc);
  1339. invalidate_complete(mg, false);
  1340. return r;
  1341. }
  1342. if (mg->cell != prealloc)
  1343. free_prison_cell(cache, prealloc);
  1344. if (r)
  1345. quiesce(mg, invalidate_remove);
  1346. else {
  1347. /*
  1348. * We can't call invalidate_remove() directly here because we
  1349. * might still be in request context.
  1350. */
  1351. init_continuation(&mg->k, invalidate_remove);
  1352. queue_work(cache->wq, &mg->k.ws);
  1353. }
  1354. return 0;
  1355. }
  1356. static int invalidate_start(struct cache *cache, dm_cblock_t cblock,
  1357. dm_oblock_t oblock, struct bio *bio)
  1358. {
  1359. struct dm_cache_migration *mg;
  1360. if (!background_work_begin(cache))
  1361. return -EPERM;
  1362. mg = alloc_migration(cache);
  1363. if (!mg) {
  1364. background_work_end(cache);
  1365. return -ENOMEM;
  1366. }
  1367. mg->overwrite_bio = bio;
  1368. mg->invalidate_cblock = cblock;
  1369. mg->invalidate_oblock = oblock;
  1370. return invalidate_lock(mg);
  1371. }
  1372. /*----------------------------------------------------------------
  1373. * bio processing
  1374. *--------------------------------------------------------------*/
  1375. enum busy {
  1376. IDLE,
  1377. BUSY
  1378. };
  1379. static enum busy spare_migration_bandwidth(struct cache *cache)
  1380. {
  1381. bool idle = iot_idle_for(&cache->tracker, HZ);
  1382. sector_t current_volume = (atomic_read(&cache->nr_io_migrations) + 1) *
  1383. cache->sectors_per_block;
  1384. if (idle && current_volume <= cache->migration_threshold)
  1385. return IDLE;
  1386. else
  1387. return BUSY;
  1388. }
  1389. static void inc_hit_counter(struct cache *cache, struct bio *bio)
  1390. {
  1391. atomic_inc(bio_data_dir(bio) == READ ?
  1392. &cache->stats.read_hit : &cache->stats.write_hit);
  1393. }
  1394. static void inc_miss_counter(struct cache *cache, struct bio *bio)
  1395. {
  1396. atomic_inc(bio_data_dir(bio) == READ ?
  1397. &cache->stats.read_miss : &cache->stats.write_miss);
  1398. }
  1399. /*----------------------------------------------------------------*/
  1400. static int map_bio(struct cache *cache, struct bio *bio, dm_oblock_t block,
  1401. bool *commit_needed)
  1402. {
  1403. int r, data_dir;
  1404. bool rb, background_queued;
  1405. dm_cblock_t cblock;
  1406. *commit_needed = false;
  1407. rb = bio_detain_shared(cache, block, bio);
  1408. if (!rb) {
  1409. /*
  1410. * An exclusive lock is held for this block, so we have to
  1411. * wait. We set the commit_needed flag so the current
  1412. * transaction will be committed asap, allowing this lock
  1413. * to be dropped.
  1414. */
  1415. *commit_needed = true;
  1416. return DM_MAPIO_SUBMITTED;
  1417. }
  1418. data_dir = bio_data_dir(bio);
  1419. if (optimisable_bio(cache, bio, block)) {
  1420. struct policy_work *op = NULL;
  1421. r = policy_lookup_with_work(cache->policy, block, &cblock, data_dir, true, &op);
  1422. if (unlikely(r && r != -ENOENT)) {
  1423. DMERR_LIMIT("%s: policy_lookup_with_work() failed with r = %d",
  1424. cache_device_name(cache), r);
  1425. bio_io_error(bio);
  1426. return DM_MAPIO_SUBMITTED;
  1427. }
  1428. if (r == -ENOENT && op) {
  1429. bio_drop_shared_lock(cache, bio);
  1430. BUG_ON(op->op != POLICY_PROMOTE);
  1431. mg_start(cache, op, bio);
  1432. return DM_MAPIO_SUBMITTED;
  1433. }
  1434. } else {
  1435. r = policy_lookup(cache->policy, block, &cblock, data_dir, false, &background_queued);
  1436. if (unlikely(r && r != -ENOENT)) {
  1437. DMERR_LIMIT("%s: policy_lookup() failed with r = %d",
  1438. cache_device_name(cache), r);
  1439. bio_io_error(bio);
  1440. return DM_MAPIO_SUBMITTED;
  1441. }
  1442. if (background_queued)
  1443. wake_migration_worker(cache);
  1444. }
  1445. if (r == -ENOENT) {
  1446. struct per_bio_data *pb = get_per_bio_data(bio);
  1447. /*
  1448. * Miss.
  1449. */
  1450. inc_miss_counter(cache, bio);
  1451. if (pb->req_nr == 0) {
  1452. accounted_begin(cache, bio);
  1453. remap_to_origin_clear_discard(cache, bio, block);
  1454. } else {
  1455. /*
  1456. * This is a duplicate writethrough io that is no
  1457. * longer needed because the block has been demoted.
  1458. */
  1459. bio_endio(bio);
  1460. return DM_MAPIO_SUBMITTED;
  1461. }
  1462. } else {
  1463. /*
  1464. * Hit.
  1465. */
  1466. inc_hit_counter(cache, bio);
  1467. /*
  1468. * Passthrough always maps to the origin, invalidating any
  1469. * cache blocks that are written to.
  1470. */
  1471. if (passthrough_mode(cache)) {
  1472. if (bio_data_dir(bio) == WRITE) {
  1473. bio_drop_shared_lock(cache, bio);
  1474. atomic_inc(&cache->stats.demotion);
  1475. invalidate_start(cache, cblock, block, bio);
  1476. } else
  1477. remap_to_origin_clear_discard(cache, bio, block);
  1478. } else {
  1479. if (bio_data_dir(bio) == WRITE && writethrough_mode(cache) &&
  1480. !is_dirty(cache, cblock)) {
  1481. remap_to_origin_and_cache(cache, bio, block, cblock);
  1482. accounted_begin(cache, bio);
  1483. } else
  1484. remap_to_cache_dirty(cache, bio, block, cblock);
  1485. }
  1486. }
  1487. /*
  1488. * dm core turns FUA requests into a separate payload and FLUSH req.
  1489. */
  1490. if (bio->bi_opf & REQ_FUA) {
  1491. /*
  1492. * issue_after_commit will call accounted_begin a second time. So
  1493. * we call accounted_complete() to avoid double accounting.
  1494. */
  1495. accounted_complete(cache, bio);
  1496. issue_after_commit(&cache->committer, bio);
  1497. *commit_needed = true;
  1498. return DM_MAPIO_SUBMITTED;
  1499. }
  1500. return DM_MAPIO_REMAPPED;
  1501. }
  1502. static bool process_bio(struct cache *cache, struct bio *bio)
  1503. {
  1504. bool commit_needed;
  1505. if (map_bio(cache, bio, get_bio_block(cache, bio), &commit_needed) == DM_MAPIO_REMAPPED)
  1506. generic_make_request(bio);
  1507. return commit_needed;
  1508. }
  1509. /*
  1510. * A non-zero return indicates read_only or fail_io mode.
  1511. */
  1512. static int commit(struct cache *cache, bool clean_shutdown)
  1513. {
  1514. int r;
  1515. if (get_cache_mode(cache) >= CM_READ_ONLY)
  1516. return -EINVAL;
  1517. atomic_inc(&cache->stats.commit_count);
  1518. r = dm_cache_commit(cache->cmd, clean_shutdown);
  1519. if (r)
  1520. metadata_operation_failed(cache, "dm_cache_commit", r);
  1521. return r;
  1522. }
  1523. /*
  1524. * Used by the batcher.
  1525. */
  1526. static blk_status_t commit_op(void *context)
  1527. {
  1528. struct cache *cache = context;
  1529. if (dm_cache_changed_this_transaction(cache->cmd))
  1530. return errno_to_blk_status(commit(cache, false));
  1531. return 0;
  1532. }
  1533. /*----------------------------------------------------------------*/
  1534. static bool process_flush_bio(struct cache *cache, struct bio *bio)
  1535. {
  1536. struct per_bio_data *pb = get_per_bio_data(bio);
  1537. if (!pb->req_nr)
  1538. remap_to_origin(cache, bio);
  1539. else
  1540. remap_to_cache(cache, bio, 0);
  1541. issue_after_commit(&cache->committer, bio);
  1542. return true;
  1543. }
  1544. static bool process_discard_bio(struct cache *cache, struct bio *bio)
  1545. {
  1546. dm_dblock_t b, e;
  1547. // FIXME: do we need to lock the region? Or can we just assume the
  1548. // user wont be so foolish as to issue discard concurrently with
  1549. // other IO?
  1550. calc_discard_block_range(cache, bio, &b, &e);
  1551. while (b != e) {
  1552. set_discard(cache, b);
  1553. b = to_dblock(from_dblock(b) + 1);
  1554. }
  1555. bio_endio(bio);
  1556. return false;
  1557. }
  1558. static void process_deferred_bios(struct work_struct *ws)
  1559. {
  1560. struct cache *cache = container_of(ws, struct cache, deferred_bio_worker);
  1561. unsigned long flags;
  1562. bool commit_needed = false;
  1563. struct bio_list bios;
  1564. struct bio *bio;
  1565. bio_list_init(&bios);
  1566. spin_lock_irqsave(&cache->lock, flags);
  1567. bio_list_merge(&bios, &cache->deferred_bios);
  1568. bio_list_init(&cache->deferred_bios);
  1569. spin_unlock_irqrestore(&cache->lock, flags);
  1570. while ((bio = bio_list_pop(&bios))) {
  1571. if (bio->bi_opf & REQ_PREFLUSH)
  1572. commit_needed = process_flush_bio(cache, bio) || commit_needed;
  1573. else if (bio_op(bio) == REQ_OP_DISCARD)
  1574. commit_needed = process_discard_bio(cache, bio) || commit_needed;
  1575. else
  1576. commit_needed = process_bio(cache, bio) || commit_needed;
  1577. }
  1578. if (commit_needed)
  1579. schedule_commit(&cache->committer);
  1580. }
  1581. /*----------------------------------------------------------------
  1582. * Main worker loop
  1583. *--------------------------------------------------------------*/
  1584. static void requeue_deferred_bios(struct cache *cache)
  1585. {
  1586. struct bio *bio;
  1587. struct bio_list bios;
  1588. bio_list_init(&bios);
  1589. bio_list_merge(&bios, &cache->deferred_bios);
  1590. bio_list_init(&cache->deferred_bios);
  1591. while ((bio = bio_list_pop(&bios))) {
  1592. bio->bi_status = BLK_STS_DM_REQUEUE;
  1593. bio_endio(bio);
  1594. }
  1595. }
  1596. /*
  1597. * We want to commit periodically so that not too much
  1598. * unwritten metadata builds up.
  1599. */
  1600. static void do_waker(struct work_struct *ws)
  1601. {
  1602. struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
  1603. policy_tick(cache->policy, true);
  1604. wake_migration_worker(cache);
  1605. schedule_commit(&cache->committer);
  1606. queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
  1607. }
  1608. static void check_migrations(struct work_struct *ws)
  1609. {
  1610. int r;
  1611. struct policy_work *op;
  1612. struct cache *cache = container_of(ws, struct cache, migration_worker);
  1613. enum busy b;
  1614. for (;;) {
  1615. b = spare_migration_bandwidth(cache);
  1616. r = policy_get_background_work(cache->policy, b == IDLE, &op);
  1617. if (r == -ENODATA)
  1618. break;
  1619. if (r) {
  1620. DMERR_LIMIT("%s: policy_background_work failed",
  1621. cache_device_name(cache));
  1622. break;
  1623. }
  1624. r = mg_start(cache, op, NULL);
  1625. if (r)
  1626. break;
  1627. }
  1628. }
  1629. /*----------------------------------------------------------------
  1630. * Target methods
  1631. *--------------------------------------------------------------*/
  1632. /*
  1633. * This function gets called on the error paths of the constructor, so we
  1634. * have to cope with a partially initialised struct.
  1635. */
  1636. static void destroy(struct cache *cache)
  1637. {
  1638. unsigned i;
  1639. mempool_destroy(cache->migration_pool);
  1640. if (cache->prison)
  1641. dm_bio_prison_destroy_v2(cache->prison);
  1642. if (cache->wq)
  1643. destroy_workqueue(cache->wq);
  1644. if (cache->dirty_bitset)
  1645. free_bitset(cache->dirty_bitset);
  1646. if (cache->discard_bitset)
  1647. free_bitset(cache->discard_bitset);
  1648. if (cache->copier)
  1649. dm_kcopyd_client_destroy(cache->copier);
  1650. if (cache->cmd)
  1651. dm_cache_metadata_close(cache->cmd);
  1652. if (cache->metadata_dev)
  1653. dm_put_device(cache->ti, cache->metadata_dev);
  1654. if (cache->origin_dev)
  1655. dm_put_device(cache->ti, cache->origin_dev);
  1656. if (cache->cache_dev)
  1657. dm_put_device(cache->ti, cache->cache_dev);
  1658. if (cache->policy)
  1659. dm_cache_policy_destroy(cache->policy);
  1660. for (i = 0; i < cache->nr_ctr_args ; i++)
  1661. kfree(cache->ctr_args[i]);
  1662. kfree(cache->ctr_args);
  1663. if (cache->bs)
  1664. bioset_free(cache->bs);
  1665. kfree(cache);
  1666. }
  1667. static void cache_dtr(struct dm_target *ti)
  1668. {
  1669. struct cache *cache = ti->private;
  1670. destroy(cache);
  1671. }
  1672. static sector_t get_dev_size(struct dm_dev *dev)
  1673. {
  1674. return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
  1675. }
  1676. /*----------------------------------------------------------------*/
  1677. /*
  1678. * Construct a cache device mapping.
  1679. *
  1680. * cache <metadata dev> <cache dev> <origin dev> <block size>
  1681. * <#feature args> [<feature arg>]*
  1682. * <policy> <#policy args> [<policy arg>]*
  1683. *
  1684. * metadata dev : fast device holding the persistent metadata
  1685. * cache dev : fast device holding cached data blocks
  1686. * origin dev : slow device holding original data blocks
  1687. * block size : cache unit size in sectors
  1688. *
  1689. * #feature args : number of feature arguments passed
  1690. * feature args : writethrough. (The default is writeback.)
  1691. *
  1692. * policy : the replacement policy to use
  1693. * #policy args : an even number of policy arguments corresponding
  1694. * to key/value pairs passed to the policy
  1695. * policy args : key/value pairs passed to the policy
  1696. * E.g. 'sequential_threshold 1024'
  1697. * See cache-policies.txt for details.
  1698. *
  1699. * Optional feature arguments are:
  1700. * writethrough : write through caching that prohibits cache block
  1701. * content from being different from origin block content.
  1702. * Without this argument, the default behaviour is to write
  1703. * back cache block contents later for performance reasons,
  1704. * so they may differ from the corresponding origin blocks.
  1705. */
  1706. struct cache_args {
  1707. struct dm_target *ti;
  1708. struct dm_dev *metadata_dev;
  1709. struct dm_dev *cache_dev;
  1710. sector_t cache_sectors;
  1711. struct dm_dev *origin_dev;
  1712. sector_t origin_sectors;
  1713. uint32_t block_size;
  1714. const char *policy_name;
  1715. int policy_argc;
  1716. const char **policy_argv;
  1717. struct cache_features features;
  1718. };
  1719. static void destroy_cache_args(struct cache_args *ca)
  1720. {
  1721. if (ca->metadata_dev)
  1722. dm_put_device(ca->ti, ca->metadata_dev);
  1723. if (ca->cache_dev)
  1724. dm_put_device(ca->ti, ca->cache_dev);
  1725. if (ca->origin_dev)
  1726. dm_put_device(ca->ti, ca->origin_dev);
  1727. kfree(ca);
  1728. }
  1729. static bool at_least_one_arg(struct dm_arg_set *as, char **error)
  1730. {
  1731. if (!as->argc) {
  1732. *error = "Insufficient args";
  1733. return false;
  1734. }
  1735. return true;
  1736. }
  1737. static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
  1738. char **error)
  1739. {
  1740. int r;
  1741. sector_t metadata_dev_size;
  1742. char b[BDEVNAME_SIZE];
  1743. if (!at_least_one_arg(as, error))
  1744. return -EINVAL;
  1745. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  1746. &ca->metadata_dev);
  1747. if (r) {
  1748. *error = "Error opening metadata device";
  1749. return r;
  1750. }
  1751. metadata_dev_size = get_dev_size(ca->metadata_dev);
  1752. if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
  1753. DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
  1754. bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
  1755. return 0;
  1756. }
  1757. static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
  1758. char **error)
  1759. {
  1760. int r;
  1761. if (!at_least_one_arg(as, error))
  1762. return -EINVAL;
  1763. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  1764. &ca->cache_dev);
  1765. if (r) {
  1766. *error = "Error opening cache device";
  1767. return r;
  1768. }
  1769. ca->cache_sectors = get_dev_size(ca->cache_dev);
  1770. return 0;
  1771. }
  1772. static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
  1773. char **error)
  1774. {
  1775. int r;
  1776. if (!at_least_one_arg(as, error))
  1777. return -EINVAL;
  1778. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  1779. &ca->origin_dev);
  1780. if (r) {
  1781. *error = "Error opening origin device";
  1782. return r;
  1783. }
  1784. ca->origin_sectors = get_dev_size(ca->origin_dev);
  1785. if (ca->ti->len > ca->origin_sectors) {
  1786. *error = "Device size larger than cached device";
  1787. return -EINVAL;
  1788. }
  1789. return 0;
  1790. }
  1791. static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
  1792. char **error)
  1793. {
  1794. unsigned long block_size;
  1795. if (!at_least_one_arg(as, error))
  1796. return -EINVAL;
  1797. if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
  1798. block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
  1799. block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
  1800. block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
  1801. *error = "Invalid data block size";
  1802. return -EINVAL;
  1803. }
  1804. if (block_size > ca->cache_sectors) {
  1805. *error = "Data block size is larger than the cache device";
  1806. return -EINVAL;
  1807. }
  1808. ca->block_size = block_size;
  1809. return 0;
  1810. }
  1811. static void init_features(struct cache_features *cf)
  1812. {
  1813. cf->mode = CM_WRITE;
  1814. cf->io_mode = CM_IO_WRITEBACK;
  1815. cf->metadata_version = 1;
  1816. }
  1817. static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
  1818. char **error)
  1819. {
  1820. static const struct dm_arg _args[] = {
  1821. {0, 2, "Invalid number of cache feature arguments"},
  1822. };
  1823. int r;
  1824. unsigned argc;
  1825. const char *arg;
  1826. struct cache_features *cf = &ca->features;
  1827. init_features(cf);
  1828. r = dm_read_arg_group(_args, as, &argc, error);
  1829. if (r)
  1830. return -EINVAL;
  1831. while (argc--) {
  1832. arg = dm_shift_arg(as);
  1833. if (!strcasecmp(arg, "writeback"))
  1834. cf->io_mode = CM_IO_WRITEBACK;
  1835. else if (!strcasecmp(arg, "writethrough"))
  1836. cf->io_mode = CM_IO_WRITETHROUGH;
  1837. else if (!strcasecmp(arg, "passthrough"))
  1838. cf->io_mode = CM_IO_PASSTHROUGH;
  1839. else if (!strcasecmp(arg, "metadata2"))
  1840. cf->metadata_version = 2;
  1841. else {
  1842. *error = "Unrecognised cache feature requested";
  1843. return -EINVAL;
  1844. }
  1845. }
  1846. return 0;
  1847. }
  1848. static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
  1849. char **error)
  1850. {
  1851. static const struct dm_arg _args[] = {
  1852. {0, 1024, "Invalid number of policy arguments"},
  1853. };
  1854. int r;
  1855. if (!at_least_one_arg(as, error))
  1856. return -EINVAL;
  1857. ca->policy_name = dm_shift_arg(as);
  1858. r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
  1859. if (r)
  1860. return -EINVAL;
  1861. ca->policy_argv = (const char **)as->argv;
  1862. dm_consume_args(as, ca->policy_argc);
  1863. return 0;
  1864. }
  1865. static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
  1866. char **error)
  1867. {
  1868. int r;
  1869. struct dm_arg_set as;
  1870. as.argc = argc;
  1871. as.argv = argv;
  1872. r = parse_metadata_dev(ca, &as, error);
  1873. if (r)
  1874. return r;
  1875. r = parse_cache_dev(ca, &as, error);
  1876. if (r)
  1877. return r;
  1878. r = parse_origin_dev(ca, &as, error);
  1879. if (r)
  1880. return r;
  1881. r = parse_block_size(ca, &as, error);
  1882. if (r)
  1883. return r;
  1884. r = parse_features(ca, &as, error);
  1885. if (r)
  1886. return r;
  1887. r = parse_policy(ca, &as, error);
  1888. if (r)
  1889. return r;
  1890. return 0;
  1891. }
  1892. /*----------------------------------------------------------------*/
  1893. static struct kmem_cache *migration_cache;
  1894. #define NOT_CORE_OPTION 1
  1895. static int process_config_option(struct cache *cache, const char *key, const char *value)
  1896. {
  1897. unsigned long tmp;
  1898. if (!strcasecmp(key, "migration_threshold")) {
  1899. if (kstrtoul(value, 10, &tmp))
  1900. return -EINVAL;
  1901. cache->migration_threshold = tmp;
  1902. return 0;
  1903. }
  1904. return NOT_CORE_OPTION;
  1905. }
  1906. static int set_config_value(struct cache *cache, const char *key, const char *value)
  1907. {
  1908. int r = process_config_option(cache, key, value);
  1909. if (r == NOT_CORE_OPTION)
  1910. r = policy_set_config_value(cache->policy, key, value);
  1911. if (r)
  1912. DMWARN("bad config value for %s: %s", key, value);
  1913. return r;
  1914. }
  1915. static int set_config_values(struct cache *cache, int argc, const char **argv)
  1916. {
  1917. int r = 0;
  1918. if (argc & 1) {
  1919. DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
  1920. return -EINVAL;
  1921. }
  1922. while (argc) {
  1923. r = set_config_value(cache, argv[0], argv[1]);
  1924. if (r)
  1925. break;
  1926. argc -= 2;
  1927. argv += 2;
  1928. }
  1929. return r;
  1930. }
  1931. static int create_cache_policy(struct cache *cache, struct cache_args *ca,
  1932. char **error)
  1933. {
  1934. struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
  1935. cache->cache_size,
  1936. cache->origin_sectors,
  1937. cache->sectors_per_block);
  1938. if (IS_ERR(p)) {
  1939. *error = "Error creating cache's policy";
  1940. return PTR_ERR(p);
  1941. }
  1942. cache->policy = p;
  1943. BUG_ON(!cache->policy);
  1944. return 0;
  1945. }
  1946. /*
  1947. * We want the discard block size to be at least the size of the cache
  1948. * block size and have no more than 2^14 discard blocks across the origin.
  1949. */
  1950. #define MAX_DISCARD_BLOCKS (1 << 14)
  1951. static bool too_many_discard_blocks(sector_t discard_block_size,
  1952. sector_t origin_size)
  1953. {
  1954. (void) sector_div(origin_size, discard_block_size);
  1955. return origin_size > MAX_DISCARD_BLOCKS;
  1956. }
  1957. static sector_t calculate_discard_block_size(sector_t cache_block_size,
  1958. sector_t origin_size)
  1959. {
  1960. sector_t discard_block_size = cache_block_size;
  1961. if (origin_size)
  1962. while (too_many_discard_blocks(discard_block_size, origin_size))
  1963. discard_block_size *= 2;
  1964. return discard_block_size;
  1965. }
  1966. static void set_cache_size(struct cache *cache, dm_cblock_t size)
  1967. {
  1968. dm_block_t nr_blocks = from_cblock(size);
  1969. if (nr_blocks > (1 << 20) && cache->cache_size != size)
  1970. DMWARN_LIMIT("You have created a cache device with a lot of individual cache blocks (%llu)\n"
  1971. "All these mappings can consume a lot of kernel memory, and take some time to read/write.\n"
  1972. "Please consider increasing the cache block size to reduce the overall cache block count.",
  1973. (unsigned long long) nr_blocks);
  1974. cache->cache_size = size;
  1975. }
  1976. static int is_congested(struct dm_dev *dev, int bdi_bits)
  1977. {
  1978. struct request_queue *q = bdev_get_queue(dev->bdev);
  1979. return bdi_congested(q->backing_dev_info, bdi_bits);
  1980. }
  1981. static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
  1982. {
  1983. struct cache *cache = container_of(cb, struct cache, callbacks);
  1984. return is_congested(cache->origin_dev, bdi_bits) ||
  1985. is_congested(cache->cache_dev, bdi_bits);
  1986. }
  1987. #define DEFAULT_MIGRATION_THRESHOLD 2048
  1988. static int cache_create(struct cache_args *ca, struct cache **result)
  1989. {
  1990. int r = 0;
  1991. char **error = &ca->ti->error;
  1992. struct cache *cache;
  1993. struct dm_target *ti = ca->ti;
  1994. dm_block_t origin_blocks;
  1995. struct dm_cache_metadata *cmd;
  1996. bool may_format = ca->features.mode == CM_WRITE;
  1997. cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  1998. if (!cache)
  1999. return -ENOMEM;
  2000. cache->ti = ca->ti;
  2001. ti->private = cache;
  2002. ti->num_flush_bios = 2;
  2003. ti->flush_supported = true;
  2004. ti->num_discard_bios = 1;
  2005. ti->discards_supported = true;
  2006. ti->split_discard_bios = false;
  2007. ti->per_io_data_size = sizeof(struct per_bio_data);
  2008. cache->features = ca->features;
  2009. if (writethrough_mode(cache)) {
  2010. /* Create bioset for writethrough bios issued to origin */
  2011. cache->bs = bioset_create(BIO_POOL_SIZE, 0, 0);
  2012. if (!cache->bs)
  2013. goto bad;
  2014. }
  2015. cache->callbacks.congested_fn = cache_is_congested;
  2016. dm_table_add_target_callbacks(ti->table, &cache->callbacks);
  2017. cache->metadata_dev = ca->metadata_dev;
  2018. cache->origin_dev = ca->origin_dev;
  2019. cache->cache_dev = ca->cache_dev;
  2020. ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
  2021. origin_blocks = cache->origin_sectors = ca->origin_sectors;
  2022. origin_blocks = block_div(origin_blocks, ca->block_size);
  2023. cache->origin_blocks = to_oblock(origin_blocks);
  2024. cache->sectors_per_block = ca->block_size;
  2025. if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
  2026. r = -EINVAL;
  2027. goto bad;
  2028. }
  2029. if (ca->block_size & (ca->block_size - 1)) {
  2030. dm_block_t cache_size = ca->cache_sectors;
  2031. cache->sectors_per_block_shift = -1;
  2032. cache_size = block_div(cache_size, ca->block_size);
  2033. set_cache_size(cache, to_cblock(cache_size));
  2034. } else {
  2035. cache->sectors_per_block_shift = __ffs(ca->block_size);
  2036. set_cache_size(cache, to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift));
  2037. }
  2038. r = create_cache_policy(cache, ca, error);
  2039. if (r)
  2040. goto bad;
  2041. cache->policy_nr_args = ca->policy_argc;
  2042. cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
  2043. r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
  2044. if (r) {
  2045. *error = "Error setting cache policy's config values";
  2046. goto bad;
  2047. }
  2048. cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
  2049. ca->block_size, may_format,
  2050. dm_cache_policy_get_hint_size(cache->policy),
  2051. ca->features.metadata_version);
  2052. if (IS_ERR(cmd)) {
  2053. *error = "Error creating metadata object";
  2054. r = PTR_ERR(cmd);
  2055. goto bad;
  2056. }
  2057. cache->cmd = cmd;
  2058. set_cache_mode(cache, CM_WRITE);
  2059. if (get_cache_mode(cache) != CM_WRITE) {
  2060. *error = "Unable to get write access to metadata, please check/repair metadata.";
  2061. r = -EINVAL;
  2062. goto bad;
  2063. }
  2064. if (passthrough_mode(cache)) {
  2065. bool all_clean;
  2066. r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
  2067. if (r) {
  2068. *error = "dm_cache_metadata_all_clean() failed";
  2069. goto bad;
  2070. }
  2071. if (!all_clean) {
  2072. *error = "Cannot enter passthrough mode unless all blocks are clean";
  2073. r = -EINVAL;
  2074. goto bad;
  2075. }
  2076. policy_allow_migrations(cache->policy, false);
  2077. }
  2078. spin_lock_init(&cache->lock);
  2079. bio_list_init(&cache->deferred_bios);
  2080. atomic_set(&cache->nr_allocated_migrations, 0);
  2081. atomic_set(&cache->nr_io_migrations, 0);
  2082. init_waitqueue_head(&cache->migration_wait);
  2083. r = -ENOMEM;
  2084. atomic_set(&cache->nr_dirty, 0);
  2085. cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
  2086. if (!cache->dirty_bitset) {
  2087. *error = "could not allocate dirty bitset";
  2088. goto bad;
  2089. }
  2090. clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
  2091. cache->discard_block_size =
  2092. calculate_discard_block_size(cache->sectors_per_block,
  2093. cache->origin_sectors);
  2094. cache->discard_nr_blocks = to_dblock(dm_sector_div_up(cache->origin_sectors,
  2095. cache->discard_block_size));
  2096. cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
  2097. if (!cache->discard_bitset) {
  2098. *error = "could not allocate discard bitset";
  2099. goto bad;
  2100. }
  2101. clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
  2102. cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  2103. if (IS_ERR(cache->copier)) {
  2104. *error = "could not create kcopyd client";
  2105. r = PTR_ERR(cache->copier);
  2106. goto bad;
  2107. }
  2108. cache->wq = alloc_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM, 0);
  2109. if (!cache->wq) {
  2110. *error = "could not create workqueue for metadata object";
  2111. goto bad;
  2112. }
  2113. INIT_WORK(&cache->deferred_bio_worker, process_deferred_bios);
  2114. INIT_WORK(&cache->migration_worker, check_migrations);
  2115. INIT_DELAYED_WORK(&cache->waker, do_waker);
  2116. cache->prison = dm_bio_prison_create_v2(cache->wq);
  2117. if (!cache->prison) {
  2118. *error = "could not create bio prison";
  2119. goto bad;
  2120. }
  2121. cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
  2122. migration_cache);
  2123. if (!cache->migration_pool) {
  2124. *error = "Error creating cache's migration mempool";
  2125. goto bad;
  2126. }
  2127. cache->need_tick_bio = true;
  2128. cache->sized = false;
  2129. cache->invalidate = false;
  2130. cache->commit_requested = false;
  2131. cache->loaded_mappings = false;
  2132. cache->loaded_discards = false;
  2133. load_stats(cache);
  2134. atomic_set(&cache->stats.demotion, 0);
  2135. atomic_set(&cache->stats.promotion, 0);
  2136. atomic_set(&cache->stats.copies_avoided, 0);
  2137. atomic_set(&cache->stats.cache_cell_clash, 0);
  2138. atomic_set(&cache->stats.commit_count, 0);
  2139. atomic_set(&cache->stats.discard_count, 0);
  2140. spin_lock_init(&cache->invalidation_lock);
  2141. INIT_LIST_HEAD(&cache->invalidation_requests);
  2142. batcher_init(&cache->committer, commit_op, cache,
  2143. issue_op, cache, cache->wq);
  2144. iot_init(&cache->tracker);
  2145. init_rwsem(&cache->background_work_lock);
  2146. prevent_background_work(cache);
  2147. *result = cache;
  2148. return 0;
  2149. bad:
  2150. destroy(cache);
  2151. return r;
  2152. }
  2153. static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
  2154. {
  2155. unsigned i;
  2156. const char **copy;
  2157. copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
  2158. if (!copy)
  2159. return -ENOMEM;
  2160. for (i = 0; i < argc; i++) {
  2161. copy[i] = kstrdup(argv[i], GFP_KERNEL);
  2162. if (!copy[i]) {
  2163. while (i--)
  2164. kfree(copy[i]);
  2165. kfree(copy);
  2166. return -ENOMEM;
  2167. }
  2168. }
  2169. cache->nr_ctr_args = argc;
  2170. cache->ctr_args = copy;
  2171. return 0;
  2172. }
  2173. static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
  2174. {
  2175. int r = -EINVAL;
  2176. struct cache_args *ca;
  2177. struct cache *cache = NULL;
  2178. ca = kzalloc(sizeof(*ca), GFP_KERNEL);
  2179. if (!ca) {
  2180. ti->error = "Error allocating memory for cache";
  2181. return -ENOMEM;
  2182. }
  2183. ca->ti = ti;
  2184. r = parse_cache_args(ca, argc, argv, &ti->error);
  2185. if (r)
  2186. goto out;
  2187. r = cache_create(ca, &cache);
  2188. if (r)
  2189. goto out;
  2190. r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
  2191. if (r) {
  2192. destroy(cache);
  2193. goto out;
  2194. }
  2195. ti->private = cache;
  2196. out:
  2197. destroy_cache_args(ca);
  2198. return r;
  2199. }
  2200. /*----------------------------------------------------------------*/
  2201. static int cache_map(struct dm_target *ti, struct bio *bio)
  2202. {
  2203. struct cache *cache = ti->private;
  2204. int r;
  2205. bool commit_needed;
  2206. dm_oblock_t block = get_bio_block(cache, bio);
  2207. init_per_bio_data(bio);
  2208. if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
  2209. /*
  2210. * This can only occur if the io goes to a partial block at
  2211. * the end of the origin device. We don't cache these.
  2212. * Just remap to the origin and carry on.
  2213. */
  2214. remap_to_origin(cache, bio);
  2215. accounted_begin(cache, bio);
  2216. return DM_MAPIO_REMAPPED;
  2217. }
  2218. if (discard_or_flush(bio)) {
  2219. defer_bio(cache, bio);
  2220. return DM_MAPIO_SUBMITTED;
  2221. }
  2222. r = map_bio(cache, bio, block, &commit_needed);
  2223. if (commit_needed)
  2224. schedule_commit(&cache->committer);
  2225. return r;
  2226. }
  2227. static int cache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *error)
  2228. {
  2229. struct cache *cache = ti->private;
  2230. unsigned long flags;
  2231. struct per_bio_data *pb = get_per_bio_data(bio);
  2232. if (pb->tick) {
  2233. policy_tick(cache->policy, false);
  2234. spin_lock_irqsave(&cache->lock, flags);
  2235. cache->need_tick_bio = true;
  2236. spin_unlock_irqrestore(&cache->lock, flags);
  2237. }
  2238. bio_drop_shared_lock(cache, bio);
  2239. accounted_complete(cache, bio);
  2240. return DM_ENDIO_DONE;
  2241. }
  2242. static int write_dirty_bitset(struct cache *cache)
  2243. {
  2244. int r;
  2245. if (get_cache_mode(cache) >= CM_READ_ONLY)
  2246. return -EINVAL;
  2247. r = dm_cache_set_dirty_bits(cache->cmd, from_cblock(cache->cache_size), cache->dirty_bitset);
  2248. if (r)
  2249. metadata_operation_failed(cache, "dm_cache_set_dirty_bits", r);
  2250. return r;
  2251. }
  2252. static int write_discard_bitset(struct cache *cache)
  2253. {
  2254. unsigned i, r;
  2255. if (get_cache_mode(cache) >= CM_READ_ONLY)
  2256. return -EINVAL;
  2257. r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
  2258. cache->discard_nr_blocks);
  2259. if (r) {
  2260. DMERR("%s: could not resize on-disk discard bitset", cache_device_name(cache));
  2261. metadata_operation_failed(cache, "dm_cache_discard_bitset_resize", r);
  2262. return r;
  2263. }
  2264. for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
  2265. r = dm_cache_set_discard(cache->cmd, to_dblock(i),
  2266. is_discarded(cache, to_dblock(i)));
  2267. if (r) {
  2268. metadata_operation_failed(cache, "dm_cache_set_discard", r);
  2269. return r;
  2270. }
  2271. }
  2272. return 0;
  2273. }
  2274. static int write_hints(struct cache *cache)
  2275. {
  2276. int r;
  2277. if (get_cache_mode(cache) >= CM_READ_ONLY)
  2278. return -EINVAL;
  2279. r = dm_cache_write_hints(cache->cmd, cache->policy);
  2280. if (r) {
  2281. metadata_operation_failed(cache, "dm_cache_write_hints", r);
  2282. return r;
  2283. }
  2284. return 0;
  2285. }
  2286. /*
  2287. * returns true on success
  2288. */
  2289. static bool sync_metadata(struct cache *cache)
  2290. {
  2291. int r1, r2, r3, r4;
  2292. r1 = write_dirty_bitset(cache);
  2293. if (r1)
  2294. DMERR("%s: could not write dirty bitset", cache_device_name(cache));
  2295. r2 = write_discard_bitset(cache);
  2296. if (r2)
  2297. DMERR("%s: could not write discard bitset", cache_device_name(cache));
  2298. save_stats(cache);
  2299. r3 = write_hints(cache);
  2300. if (r3)
  2301. DMERR("%s: could not write hints", cache_device_name(cache));
  2302. /*
  2303. * If writing the above metadata failed, we still commit, but don't
  2304. * set the clean shutdown flag. This will effectively force every
  2305. * dirty bit to be set on reload.
  2306. */
  2307. r4 = commit(cache, !r1 && !r2 && !r3);
  2308. if (r4)
  2309. DMERR("%s: could not write cache metadata", cache_device_name(cache));
  2310. return !r1 && !r2 && !r3 && !r4;
  2311. }
  2312. static void cache_postsuspend(struct dm_target *ti)
  2313. {
  2314. struct cache *cache = ti->private;
  2315. prevent_background_work(cache);
  2316. BUG_ON(atomic_read(&cache->nr_io_migrations));
  2317. cancel_delayed_work(&cache->waker);
  2318. flush_workqueue(cache->wq);
  2319. WARN_ON(cache->tracker.in_flight);
  2320. /*
  2321. * If it's a flush suspend there won't be any deferred bios, so this
  2322. * call is harmless.
  2323. */
  2324. requeue_deferred_bios(cache);
  2325. if (get_cache_mode(cache) == CM_WRITE)
  2326. (void) sync_metadata(cache);
  2327. }
  2328. static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
  2329. bool dirty, uint32_t hint, bool hint_valid)
  2330. {
  2331. int r;
  2332. struct cache *cache = context;
  2333. if (dirty) {
  2334. set_bit(from_cblock(cblock), cache->dirty_bitset);
  2335. atomic_inc(&cache->nr_dirty);
  2336. } else
  2337. clear_bit(from_cblock(cblock), cache->dirty_bitset);
  2338. r = policy_load_mapping(cache->policy, oblock, cblock, dirty, hint, hint_valid);
  2339. if (r)
  2340. return r;
  2341. return 0;
  2342. }
  2343. /*
  2344. * The discard block size in the on disk metadata is not
  2345. * neccessarily the same as we're currently using. So we have to
  2346. * be careful to only set the discarded attribute if we know it
  2347. * covers a complete block of the new size.
  2348. */
  2349. struct discard_load_info {
  2350. struct cache *cache;
  2351. /*
  2352. * These blocks are sized using the on disk dblock size, rather
  2353. * than the current one.
  2354. */
  2355. dm_block_t block_size;
  2356. dm_block_t discard_begin, discard_end;
  2357. };
  2358. static void discard_load_info_init(struct cache *cache,
  2359. struct discard_load_info *li)
  2360. {
  2361. li->cache = cache;
  2362. li->discard_begin = li->discard_end = 0;
  2363. }
  2364. static void set_discard_range(struct discard_load_info *li)
  2365. {
  2366. sector_t b, e;
  2367. if (li->discard_begin == li->discard_end)
  2368. return;
  2369. /*
  2370. * Convert to sectors.
  2371. */
  2372. b = li->discard_begin * li->block_size;
  2373. e = li->discard_end * li->block_size;
  2374. /*
  2375. * Then convert back to the current dblock size.
  2376. */
  2377. b = dm_sector_div_up(b, li->cache->discard_block_size);
  2378. sector_div(e, li->cache->discard_block_size);
  2379. /*
  2380. * The origin may have shrunk, so we need to check we're still in
  2381. * bounds.
  2382. */
  2383. if (e > from_dblock(li->cache->discard_nr_blocks))
  2384. e = from_dblock(li->cache->discard_nr_blocks);
  2385. for (; b < e; b++)
  2386. set_discard(li->cache, to_dblock(b));
  2387. }
  2388. static int load_discard(void *context, sector_t discard_block_size,
  2389. dm_dblock_t dblock, bool discard)
  2390. {
  2391. struct discard_load_info *li = context;
  2392. li->block_size = discard_block_size;
  2393. if (discard) {
  2394. if (from_dblock(dblock) == li->discard_end)
  2395. /*
  2396. * We're already in a discard range, just extend it.
  2397. */
  2398. li->discard_end = li->discard_end + 1ULL;
  2399. else {
  2400. /*
  2401. * Emit the old range and start a new one.
  2402. */
  2403. set_discard_range(li);
  2404. li->discard_begin = from_dblock(dblock);
  2405. li->discard_end = li->discard_begin + 1ULL;
  2406. }
  2407. } else {
  2408. set_discard_range(li);
  2409. li->discard_begin = li->discard_end = 0;
  2410. }
  2411. return 0;
  2412. }
  2413. static dm_cblock_t get_cache_dev_size(struct cache *cache)
  2414. {
  2415. sector_t size = get_dev_size(cache->cache_dev);
  2416. (void) sector_div(size, cache->sectors_per_block);
  2417. return to_cblock(size);
  2418. }
  2419. static bool can_resize(struct cache *cache, dm_cblock_t new_size)
  2420. {
  2421. if (from_cblock(new_size) > from_cblock(cache->cache_size))
  2422. return true;
  2423. /*
  2424. * We can't drop a dirty block when shrinking the cache.
  2425. */
  2426. while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
  2427. new_size = to_cblock(from_cblock(new_size) + 1);
  2428. if (is_dirty(cache, new_size)) {
  2429. DMERR("%s: unable to shrink cache; cache block %llu is dirty",
  2430. cache_device_name(cache),
  2431. (unsigned long long) from_cblock(new_size));
  2432. return false;
  2433. }
  2434. }
  2435. return true;
  2436. }
  2437. static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
  2438. {
  2439. int r;
  2440. r = dm_cache_resize(cache->cmd, new_size);
  2441. if (r) {
  2442. DMERR("%s: could not resize cache metadata", cache_device_name(cache));
  2443. metadata_operation_failed(cache, "dm_cache_resize", r);
  2444. return r;
  2445. }
  2446. set_cache_size(cache, new_size);
  2447. return 0;
  2448. }
  2449. static int cache_preresume(struct dm_target *ti)
  2450. {
  2451. int r = 0;
  2452. struct cache *cache = ti->private;
  2453. dm_cblock_t csize = get_cache_dev_size(cache);
  2454. /*
  2455. * Check to see if the cache has resized.
  2456. */
  2457. if (!cache->sized) {
  2458. r = resize_cache_dev(cache, csize);
  2459. if (r)
  2460. return r;
  2461. cache->sized = true;
  2462. } else if (csize != cache->cache_size) {
  2463. if (!can_resize(cache, csize))
  2464. return -EINVAL;
  2465. r = resize_cache_dev(cache, csize);
  2466. if (r)
  2467. return r;
  2468. }
  2469. if (!cache->loaded_mappings) {
  2470. r = dm_cache_load_mappings(cache->cmd, cache->policy,
  2471. load_mapping, cache);
  2472. if (r) {
  2473. DMERR("%s: could not load cache mappings", cache_device_name(cache));
  2474. metadata_operation_failed(cache, "dm_cache_load_mappings", r);
  2475. return r;
  2476. }
  2477. cache->loaded_mappings = true;
  2478. }
  2479. if (!cache->loaded_discards) {
  2480. struct discard_load_info li;
  2481. /*
  2482. * The discard bitset could have been resized, or the
  2483. * discard block size changed. To be safe we start by
  2484. * setting every dblock to not discarded.
  2485. */
  2486. clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
  2487. discard_load_info_init(cache, &li);
  2488. r = dm_cache_load_discards(cache->cmd, load_discard, &li);
  2489. if (r) {
  2490. DMERR("%s: could not load origin discards", cache_device_name(cache));
  2491. metadata_operation_failed(cache, "dm_cache_load_discards", r);
  2492. return r;
  2493. }
  2494. set_discard_range(&li);
  2495. cache->loaded_discards = true;
  2496. }
  2497. return r;
  2498. }
  2499. static void cache_resume(struct dm_target *ti)
  2500. {
  2501. struct cache *cache = ti->private;
  2502. cache->need_tick_bio = true;
  2503. allow_background_work(cache);
  2504. do_waker(&cache->waker.work);
  2505. }
  2506. /*
  2507. * Status format:
  2508. *
  2509. * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
  2510. * <cache block size> <#used cache blocks>/<#total cache blocks>
  2511. * <#read hits> <#read misses> <#write hits> <#write misses>
  2512. * <#demotions> <#promotions> <#dirty>
  2513. * <#features> <features>*
  2514. * <#core args> <core args>
  2515. * <policy name> <#policy args> <policy args>* <cache metadata mode> <needs_check>
  2516. */
  2517. static void cache_status(struct dm_target *ti, status_type_t type,
  2518. unsigned status_flags, char *result, unsigned maxlen)
  2519. {
  2520. int r = 0;
  2521. unsigned i;
  2522. ssize_t sz = 0;
  2523. dm_block_t nr_free_blocks_metadata = 0;
  2524. dm_block_t nr_blocks_metadata = 0;
  2525. char buf[BDEVNAME_SIZE];
  2526. struct cache *cache = ti->private;
  2527. dm_cblock_t residency;
  2528. bool needs_check;
  2529. switch (type) {
  2530. case STATUSTYPE_INFO:
  2531. if (get_cache_mode(cache) == CM_FAIL) {
  2532. DMEMIT("Fail");
  2533. break;
  2534. }
  2535. /* Commit to ensure statistics aren't out-of-date */
  2536. if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
  2537. (void) commit(cache, false);
  2538. r = dm_cache_get_free_metadata_block_count(cache->cmd, &nr_free_blocks_metadata);
  2539. if (r) {
  2540. DMERR("%s: dm_cache_get_free_metadata_block_count returned %d",
  2541. cache_device_name(cache), r);
  2542. goto err;
  2543. }
  2544. r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
  2545. if (r) {
  2546. DMERR("%s: dm_cache_get_metadata_dev_size returned %d",
  2547. cache_device_name(cache), r);
  2548. goto err;
  2549. }
  2550. residency = policy_residency(cache->policy);
  2551. DMEMIT("%u %llu/%llu %llu %llu/%llu %u %u %u %u %u %u %lu ",
  2552. (unsigned)DM_CACHE_METADATA_BLOCK_SIZE,
  2553. (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
  2554. (unsigned long long)nr_blocks_metadata,
  2555. (unsigned long long)cache->sectors_per_block,
  2556. (unsigned long long) from_cblock(residency),
  2557. (unsigned long long) from_cblock(cache->cache_size),
  2558. (unsigned) atomic_read(&cache->stats.read_hit),
  2559. (unsigned) atomic_read(&cache->stats.read_miss),
  2560. (unsigned) atomic_read(&cache->stats.write_hit),
  2561. (unsigned) atomic_read(&cache->stats.write_miss),
  2562. (unsigned) atomic_read(&cache->stats.demotion),
  2563. (unsigned) atomic_read(&cache->stats.promotion),
  2564. (unsigned long) atomic_read(&cache->nr_dirty));
  2565. if (cache->features.metadata_version == 2)
  2566. DMEMIT("2 metadata2 ");
  2567. else
  2568. DMEMIT("1 ");
  2569. if (writethrough_mode(cache))
  2570. DMEMIT("writethrough ");
  2571. else if (passthrough_mode(cache))
  2572. DMEMIT("passthrough ");
  2573. else if (writeback_mode(cache))
  2574. DMEMIT("writeback ");
  2575. else {
  2576. DMERR("%s: internal error: unknown io mode: %d",
  2577. cache_device_name(cache), (int) cache->features.io_mode);
  2578. goto err;
  2579. }
  2580. DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
  2581. DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
  2582. if (sz < maxlen) {
  2583. r = policy_emit_config_values(cache->policy, result, maxlen, &sz);
  2584. if (r)
  2585. DMERR("%s: policy_emit_config_values returned %d",
  2586. cache_device_name(cache), r);
  2587. }
  2588. if (get_cache_mode(cache) == CM_READ_ONLY)
  2589. DMEMIT("ro ");
  2590. else
  2591. DMEMIT("rw ");
  2592. r = dm_cache_metadata_needs_check(cache->cmd, &needs_check);
  2593. if (r || needs_check)
  2594. DMEMIT("needs_check ");
  2595. else
  2596. DMEMIT("- ");
  2597. break;
  2598. case STATUSTYPE_TABLE:
  2599. format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
  2600. DMEMIT("%s ", buf);
  2601. format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
  2602. DMEMIT("%s ", buf);
  2603. format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
  2604. DMEMIT("%s", buf);
  2605. for (i = 0; i < cache->nr_ctr_args - 1; i++)
  2606. DMEMIT(" %s", cache->ctr_args[i]);
  2607. if (cache->nr_ctr_args)
  2608. DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
  2609. }
  2610. return;
  2611. err:
  2612. DMEMIT("Error");
  2613. }
  2614. /*
  2615. * Defines a range of cblocks, begin to (end - 1) are in the range. end is
  2616. * the one-past-the-end value.
  2617. */
  2618. struct cblock_range {
  2619. dm_cblock_t begin;
  2620. dm_cblock_t end;
  2621. };
  2622. /*
  2623. * A cache block range can take two forms:
  2624. *
  2625. * i) A single cblock, eg. '3456'
  2626. * ii) A begin and end cblock with a dash between, eg. 123-234
  2627. */
  2628. static int parse_cblock_range(struct cache *cache, const char *str,
  2629. struct cblock_range *result)
  2630. {
  2631. char dummy;
  2632. uint64_t b, e;
  2633. int r;
  2634. /*
  2635. * Try and parse form (ii) first.
  2636. */
  2637. r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
  2638. if (r < 0)
  2639. return r;
  2640. if (r == 2) {
  2641. result->begin = to_cblock(b);
  2642. result->end = to_cblock(e);
  2643. return 0;
  2644. }
  2645. /*
  2646. * That didn't work, try form (i).
  2647. */
  2648. r = sscanf(str, "%llu%c", &b, &dummy);
  2649. if (r < 0)
  2650. return r;
  2651. if (r == 1) {
  2652. result->begin = to_cblock(b);
  2653. result->end = to_cblock(from_cblock(result->begin) + 1u);
  2654. return 0;
  2655. }
  2656. DMERR("%s: invalid cblock range '%s'", cache_device_name(cache), str);
  2657. return -EINVAL;
  2658. }
  2659. static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
  2660. {
  2661. uint64_t b = from_cblock(range->begin);
  2662. uint64_t e = from_cblock(range->end);
  2663. uint64_t n = from_cblock(cache->cache_size);
  2664. if (b >= n) {
  2665. DMERR("%s: begin cblock out of range: %llu >= %llu",
  2666. cache_device_name(cache), b, n);
  2667. return -EINVAL;
  2668. }
  2669. if (e > n) {
  2670. DMERR("%s: end cblock out of range: %llu > %llu",
  2671. cache_device_name(cache), e, n);
  2672. return -EINVAL;
  2673. }
  2674. if (b >= e) {
  2675. DMERR("%s: invalid cblock range: %llu >= %llu",
  2676. cache_device_name(cache), b, e);
  2677. return -EINVAL;
  2678. }
  2679. return 0;
  2680. }
  2681. static inline dm_cblock_t cblock_succ(dm_cblock_t b)
  2682. {
  2683. return to_cblock(from_cblock(b) + 1);
  2684. }
  2685. static int request_invalidation(struct cache *cache, struct cblock_range *range)
  2686. {
  2687. int r = 0;
  2688. /*
  2689. * We don't need to do any locking here because we know we're in
  2690. * passthrough mode. There's is potential for a race between an
  2691. * invalidation triggered by an io and an invalidation message. This
  2692. * is harmless, we must not worry if the policy call fails.
  2693. */
  2694. while (range->begin != range->end) {
  2695. r = invalidate_cblock(cache, range->begin);
  2696. if (r)
  2697. return r;
  2698. range->begin = cblock_succ(range->begin);
  2699. }
  2700. cache->commit_requested = true;
  2701. return r;
  2702. }
  2703. static int process_invalidate_cblocks_message(struct cache *cache, unsigned count,
  2704. const char **cblock_ranges)
  2705. {
  2706. int r = 0;
  2707. unsigned i;
  2708. struct cblock_range range;
  2709. if (!passthrough_mode(cache)) {
  2710. DMERR("%s: cache has to be in passthrough mode for invalidation",
  2711. cache_device_name(cache));
  2712. return -EPERM;
  2713. }
  2714. for (i = 0; i < count; i++) {
  2715. r = parse_cblock_range(cache, cblock_ranges[i], &range);
  2716. if (r)
  2717. break;
  2718. r = validate_cblock_range(cache, &range);
  2719. if (r)
  2720. break;
  2721. /*
  2722. * Pass begin and end origin blocks to the worker and wake it.
  2723. */
  2724. r = request_invalidation(cache, &range);
  2725. if (r)
  2726. break;
  2727. }
  2728. return r;
  2729. }
  2730. /*
  2731. * Supports
  2732. * "<key> <value>"
  2733. * and
  2734. * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
  2735. *
  2736. * The key migration_threshold is supported by the cache target core.
  2737. */
  2738. static int cache_message(struct dm_target *ti, unsigned argc, char **argv,
  2739. char *result, unsigned maxlen)
  2740. {
  2741. struct cache *cache = ti->private;
  2742. if (!argc)
  2743. return -EINVAL;
  2744. if (get_cache_mode(cache) >= CM_READ_ONLY) {
  2745. DMERR("%s: unable to service cache target messages in READ_ONLY or FAIL mode",
  2746. cache_device_name(cache));
  2747. return -EOPNOTSUPP;
  2748. }
  2749. if (!strcasecmp(argv[0], "invalidate_cblocks"))
  2750. return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
  2751. if (argc != 2)
  2752. return -EINVAL;
  2753. return set_config_value(cache, argv[0], argv[1]);
  2754. }
  2755. static int cache_iterate_devices(struct dm_target *ti,
  2756. iterate_devices_callout_fn fn, void *data)
  2757. {
  2758. int r = 0;
  2759. struct cache *cache = ti->private;
  2760. r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
  2761. if (!r)
  2762. r = fn(ti, cache->origin_dev, 0, ti->len, data);
  2763. return r;
  2764. }
  2765. static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
  2766. {
  2767. /*
  2768. * FIXME: these limits may be incompatible with the cache device
  2769. */
  2770. limits->max_discard_sectors = min_t(sector_t, cache->discard_block_size * 1024,
  2771. cache->origin_sectors);
  2772. limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
  2773. }
  2774. static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
  2775. {
  2776. struct cache *cache = ti->private;
  2777. uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
  2778. /*
  2779. * If the system-determined stacked limits are compatible with the
  2780. * cache's blocksize (io_opt is a factor) do not override them.
  2781. */
  2782. if (io_opt_sectors < cache->sectors_per_block ||
  2783. do_div(io_opt_sectors, cache->sectors_per_block)) {
  2784. blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT);
  2785. blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
  2786. }
  2787. set_discard_limits(cache, limits);
  2788. }
  2789. /*----------------------------------------------------------------*/
  2790. static struct target_type cache_target = {
  2791. .name = "cache",
  2792. .version = {2, 0, 0},
  2793. .module = THIS_MODULE,
  2794. .ctr = cache_ctr,
  2795. .dtr = cache_dtr,
  2796. .map = cache_map,
  2797. .end_io = cache_end_io,
  2798. .postsuspend = cache_postsuspend,
  2799. .preresume = cache_preresume,
  2800. .resume = cache_resume,
  2801. .status = cache_status,
  2802. .message = cache_message,
  2803. .iterate_devices = cache_iterate_devices,
  2804. .io_hints = cache_io_hints,
  2805. };
  2806. static int __init dm_cache_init(void)
  2807. {
  2808. int r;
  2809. migration_cache = KMEM_CACHE(dm_cache_migration, 0);
  2810. if (!migration_cache) {
  2811. dm_unregister_target(&cache_target);
  2812. return -ENOMEM;
  2813. }
  2814. r = dm_register_target(&cache_target);
  2815. if (r) {
  2816. DMERR("cache target registration failed: %d", r);
  2817. return r;
  2818. }
  2819. return 0;
  2820. }
  2821. static void __exit dm_cache_exit(void)
  2822. {
  2823. dm_unregister_target(&cache_target);
  2824. kmem_cache_destroy(migration_cache);
  2825. }
  2826. module_init(dm_cache_init);
  2827. module_exit(dm_cache_exit);
  2828. MODULE_DESCRIPTION(DM_NAME " cache target");
  2829. MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
  2830. MODULE_LICENSE("GPL");