dm-cache-target.c 85 KB

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