dm-cache-target.c 85 KB

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