dm-cache-target.c 83 KB

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