dm-cache-target.c 86 KB

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