dm-cache-target.c 85 KB

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