dm-cache-target.c 83 KB

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