dm-cache-target.c 94 KB

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