dm-cache-target.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858
  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. uint32_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 = dm_cache_metadata_needs_check(cache->cmd);
  800. enum cache_metadata_mode old_mode = get_cache_mode(cache);
  801. if (new_mode == CM_WRITE && needs_check) {
  802. DMERR("%s: unable to switch cache to write mode until repaired.",
  803. cache_device_name(cache));
  804. if (old_mode != new_mode)
  805. new_mode = old_mode;
  806. else
  807. new_mode = CM_READ_ONLY;
  808. }
  809. /* Never move out of fail mode */
  810. if (old_mode == CM_FAIL)
  811. new_mode = CM_FAIL;
  812. switch (new_mode) {
  813. case CM_FAIL:
  814. case CM_READ_ONLY:
  815. dm_cache_metadata_set_read_only(cache->cmd);
  816. break;
  817. case CM_WRITE:
  818. dm_cache_metadata_set_read_write(cache->cmd);
  819. break;
  820. }
  821. cache->features.mode = new_mode;
  822. if (new_mode != old_mode)
  823. notify_mode_switch(cache, new_mode);
  824. }
  825. static void abort_transaction(struct cache *cache)
  826. {
  827. const char *dev_name = cache_device_name(cache);
  828. if (get_cache_mode(cache) >= CM_READ_ONLY)
  829. return;
  830. if (dm_cache_metadata_set_needs_check(cache->cmd)) {
  831. DMERR("%s: failed to set 'needs_check' flag in metadata", dev_name);
  832. set_cache_mode(cache, CM_FAIL);
  833. }
  834. DMERR_LIMIT("%s: aborting current metadata transaction", dev_name);
  835. if (dm_cache_metadata_abort(cache->cmd)) {
  836. DMERR("%s: failed to abort metadata transaction", dev_name);
  837. set_cache_mode(cache, CM_FAIL);
  838. }
  839. }
  840. static void metadata_operation_failed(struct cache *cache, const char *op, int r)
  841. {
  842. DMERR_LIMIT("%s: metadata operation '%s' failed: error = %d",
  843. cache_device_name(cache), op, r);
  844. abort_transaction(cache);
  845. set_cache_mode(cache, CM_READ_ONLY);
  846. }
  847. /*----------------------------------------------------------------
  848. * Migration processing
  849. *
  850. * Migration covers moving data from the origin device to the cache, or
  851. * vice versa.
  852. *--------------------------------------------------------------*/
  853. static void inc_io_migrations(struct cache *cache)
  854. {
  855. atomic_inc(&cache->nr_io_migrations);
  856. }
  857. static void dec_io_migrations(struct cache *cache)
  858. {
  859. atomic_dec(&cache->nr_io_migrations);
  860. }
  861. static bool discard_or_flush(struct bio *bio)
  862. {
  863. return bio->bi_rw & (REQ_FLUSH | REQ_FUA | REQ_DISCARD);
  864. }
  865. static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell)
  866. {
  867. if (discard_or_flush(cell->holder)) {
  868. /*
  869. * We have to handle these bios individually.
  870. */
  871. dm_cell_release(cache->prison, cell, &cache->deferred_bios);
  872. free_prison_cell(cache, cell);
  873. } else
  874. list_add_tail(&cell->user_list, &cache->deferred_cells);
  875. }
  876. static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell, bool holder)
  877. {
  878. unsigned long flags;
  879. if (!holder && dm_cell_promote_or_release(cache->prison, cell)) {
  880. /*
  881. * There was no prisoner to promote to holder, the
  882. * cell has been released.
  883. */
  884. free_prison_cell(cache, cell);
  885. return;
  886. }
  887. spin_lock_irqsave(&cache->lock, flags);
  888. __cell_defer(cache, cell);
  889. spin_unlock_irqrestore(&cache->lock, flags);
  890. wake_worker(cache);
  891. }
  892. static void cell_error_with_code(struct cache *cache, struct dm_bio_prison_cell *cell, int err)
  893. {
  894. dm_cell_error(cache->prison, cell, err);
  895. free_prison_cell(cache, cell);
  896. }
  897. static void cell_requeue(struct cache *cache, struct dm_bio_prison_cell *cell)
  898. {
  899. cell_error_with_code(cache, cell, DM_ENDIO_REQUEUE);
  900. }
  901. static void free_io_migration(struct dm_cache_migration *mg)
  902. {
  903. struct cache *cache = mg->cache;
  904. dec_io_migrations(cache);
  905. free_migration(mg);
  906. wake_worker(cache);
  907. }
  908. static void migration_failure(struct dm_cache_migration *mg)
  909. {
  910. struct cache *cache = mg->cache;
  911. const char *dev_name = cache_device_name(cache);
  912. if (mg->writeback) {
  913. DMERR_LIMIT("%s: writeback failed; couldn't copy block", dev_name);
  914. set_dirty(cache, mg->old_oblock, mg->cblock);
  915. cell_defer(cache, mg->old_ocell, false);
  916. } else if (mg->demote) {
  917. DMERR_LIMIT("%s: demotion failed; couldn't copy block", dev_name);
  918. policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
  919. cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
  920. if (mg->promote)
  921. cell_defer(cache, mg->new_ocell, true);
  922. } else {
  923. DMERR_LIMIT("%s: promotion failed; couldn't copy block", dev_name);
  924. policy_remove_mapping(cache->policy, mg->new_oblock);
  925. cell_defer(cache, mg->new_ocell, true);
  926. }
  927. free_io_migration(mg);
  928. }
  929. static void migration_success_pre_commit(struct dm_cache_migration *mg)
  930. {
  931. int r;
  932. unsigned long flags;
  933. struct cache *cache = mg->cache;
  934. if (mg->writeback) {
  935. clear_dirty(cache, mg->old_oblock, mg->cblock);
  936. cell_defer(cache, mg->old_ocell, false);
  937. free_io_migration(mg);
  938. return;
  939. } else if (mg->demote) {
  940. r = dm_cache_remove_mapping(cache->cmd, mg->cblock);
  941. if (r) {
  942. DMERR_LIMIT("%s: demotion failed; couldn't update on disk metadata",
  943. cache_device_name(cache));
  944. metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
  945. policy_force_mapping(cache->policy, mg->new_oblock,
  946. mg->old_oblock);
  947. if (mg->promote)
  948. cell_defer(cache, mg->new_ocell, true);
  949. free_io_migration(mg);
  950. return;
  951. }
  952. } else {
  953. r = dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock);
  954. if (r) {
  955. DMERR_LIMIT("%s: promotion failed; couldn't update on disk metadata",
  956. cache_device_name(cache));
  957. metadata_operation_failed(cache, "dm_cache_insert_mapping", r);
  958. policy_remove_mapping(cache->policy, mg->new_oblock);
  959. free_io_migration(mg);
  960. return;
  961. }
  962. }
  963. spin_lock_irqsave(&cache->lock, flags);
  964. list_add_tail(&mg->list, &cache->need_commit_migrations);
  965. cache->commit_requested = true;
  966. spin_unlock_irqrestore(&cache->lock, flags);
  967. }
  968. static void migration_success_post_commit(struct dm_cache_migration *mg)
  969. {
  970. unsigned long flags;
  971. struct cache *cache = mg->cache;
  972. if (mg->writeback) {
  973. DMWARN_LIMIT("%s: writeback unexpectedly triggered commit",
  974. cache_device_name(cache));
  975. return;
  976. } else if (mg->demote) {
  977. cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
  978. if (mg->promote) {
  979. mg->demote = false;
  980. spin_lock_irqsave(&cache->lock, flags);
  981. list_add_tail(&mg->list, &cache->quiesced_migrations);
  982. spin_unlock_irqrestore(&cache->lock, flags);
  983. } else {
  984. if (mg->invalidate)
  985. policy_remove_mapping(cache->policy, mg->old_oblock);
  986. free_io_migration(mg);
  987. }
  988. } else {
  989. if (mg->requeue_holder) {
  990. clear_dirty(cache, mg->new_oblock, mg->cblock);
  991. cell_defer(cache, mg->new_ocell, true);
  992. } else {
  993. /*
  994. * The block was promoted via an overwrite, so it's dirty.
  995. */
  996. set_dirty(cache, mg->new_oblock, mg->cblock);
  997. bio_endio(mg->new_ocell->holder);
  998. cell_defer(cache, mg->new_ocell, false);
  999. }
  1000. free_io_migration(mg);
  1001. }
  1002. }
  1003. static void copy_complete(int read_err, unsigned long write_err, void *context)
  1004. {
  1005. unsigned long flags;
  1006. struct dm_cache_migration *mg = (struct dm_cache_migration *) context;
  1007. struct cache *cache = mg->cache;
  1008. if (read_err || write_err)
  1009. mg->err = true;
  1010. spin_lock_irqsave(&cache->lock, flags);
  1011. list_add_tail(&mg->list, &cache->completed_migrations);
  1012. spin_unlock_irqrestore(&cache->lock, flags);
  1013. wake_worker(cache);
  1014. }
  1015. static void issue_copy(struct dm_cache_migration *mg)
  1016. {
  1017. int r;
  1018. struct dm_io_region o_region, c_region;
  1019. struct cache *cache = mg->cache;
  1020. sector_t cblock = from_cblock(mg->cblock);
  1021. o_region.bdev = cache->origin_dev->bdev;
  1022. o_region.count = cache->sectors_per_block;
  1023. c_region.bdev = cache->cache_dev->bdev;
  1024. c_region.sector = cblock * cache->sectors_per_block;
  1025. c_region.count = cache->sectors_per_block;
  1026. if (mg->writeback || mg->demote) {
  1027. /* demote */
  1028. o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
  1029. r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
  1030. } else {
  1031. /* promote */
  1032. o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
  1033. r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
  1034. }
  1035. if (r < 0) {
  1036. DMERR_LIMIT("%s: issuing migration failed", cache_device_name(cache));
  1037. migration_failure(mg);
  1038. }
  1039. }
  1040. static void overwrite_endio(struct bio *bio)
  1041. {
  1042. struct dm_cache_migration *mg = bio->bi_private;
  1043. struct cache *cache = mg->cache;
  1044. size_t pb_data_size = get_per_bio_data_size(cache);
  1045. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  1046. unsigned long flags;
  1047. dm_unhook_bio(&pb->hook_info, bio);
  1048. if (bio->bi_error)
  1049. mg->err = true;
  1050. mg->requeue_holder = false;
  1051. spin_lock_irqsave(&cache->lock, flags);
  1052. list_add_tail(&mg->list, &cache->completed_migrations);
  1053. spin_unlock_irqrestore(&cache->lock, flags);
  1054. wake_worker(cache);
  1055. }
  1056. static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio)
  1057. {
  1058. size_t pb_data_size = get_per_bio_data_size(mg->cache);
  1059. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  1060. dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
  1061. remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
  1062. /*
  1063. * No need to inc_ds() here, since the cell will be held for the
  1064. * duration of the io.
  1065. */
  1066. accounted_request(mg->cache, bio);
  1067. }
  1068. static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
  1069. {
  1070. return (bio_data_dir(bio) == WRITE) &&
  1071. (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
  1072. }
  1073. static void avoid_copy(struct dm_cache_migration *mg)
  1074. {
  1075. atomic_inc(&mg->cache->stats.copies_avoided);
  1076. migration_success_pre_commit(mg);
  1077. }
  1078. static void calc_discard_block_range(struct cache *cache, struct bio *bio,
  1079. dm_dblock_t *b, dm_dblock_t *e)
  1080. {
  1081. sector_t sb = bio->bi_iter.bi_sector;
  1082. sector_t se = bio_end_sector(bio);
  1083. *b = to_dblock(dm_sector_div_up(sb, cache->discard_block_size));
  1084. if (se - sb < cache->discard_block_size)
  1085. *e = *b;
  1086. else
  1087. *e = to_dblock(block_div(se, cache->discard_block_size));
  1088. }
  1089. static void issue_discard(struct dm_cache_migration *mg)
  1090. {
  1091. dm_dblock_t b, e;
  1092. struct bio *bio = mg->new_ocell->holder;
  1093. struct cache *cache = mg->cache;
  1094. calc_discard_block_range(cache, bio, &b, &e);
  1095. while (b != e) {
  1096. set_discard(cache, b);
  1097. b = to_dblock(from_dblock(b) + 1);
  1098. }
  1099. bio_endio(bio);
  1100. cell_defer(cache, mg->new_ocell, false);
  1101. free_migration(mg);
  1102. wake_worker(cache);
  1103. }
  1104. static void issue_copy_or_discard(struct dm_cache_migration *mg)
  1105. {
  1106. bool avoid;
  1107. struct cache *cache = mg->cache;
  1108. if (mg->discard) {
  1109. issue_discard(mg);
  1110. return;
  1111. }
  1112. if (mg->writeback || mg->demote)
  1113. avoid = !is_dirty(cache, mg->cblock) ||
  1114. is_discarded_oblock(cache, mg->old_oblock);
  1115. else {
  1116. struct bio *bio = mg->new_ocell->holder;
  1117. avoid = is_discarded_oblock(cache, mg->new_oblock);
  1118. if (writeback_mode(&cache->features) &&
  1119. !avoid && bio_writes_complete_block(cache, bio)) {
  1120. issue_overwrite(mg, bio);
  1121. return;
  1122. }
  1123. }
  1124. avoid ? avoid_copy(mg) : issue_copy(mg);
  1125. }
  1126. static void complete_migration(struct dm_cache_migration *mg)
  1127. {
  1128. if (mg->err)
  1129. migration_failure(mg);
  1130. else
  1131. migration_success_pre_commit(mg);
  1132. }
  1133. static void process_migrations(struct cache *cache, struct list_head *head,
  1134. void (*fn)(struct dm_cache_migration *))
  1135. {
  1136. unsigned long flags;
  1137. struct list_head list;
  1138. struct dm_cache_migration *mg, *tmp;
  1139. INIT_LIST_HEAD(&list);
  1140. spin_lock_irqsave(&cache->lock, flags);
  1141. list_splice_init(head, &list);
  1142. spin_unlock_irqrestore(&cache->lock, flags);
  1143. list_for_each_entry_safe(mg, tmp, &list, list)
  1144. fn(mg);
  1145. }
  1146. static void __queue_quiesced_migration(struct dm_cache_migration *mg)
  1147. {
  1148. list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
  1149. }
  1150. static void queue_quiesced_migration(struct dm_cache_migration *mg)
  1151. {
  1152. unsigned long flags;
  1153. struct cache *cache = mg->cache;
  1154. spin_lock_irqsave(&cache->lock, flags);
  1155. __queue_quiesced_migration(mg);
  1156. spin_unlock_irqrestore(&cache->lock, flags);
  1157. wake_worker(cache);
  1158. }
  1159. static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
  1160. {
  1161. unsigned long flags;
  1162. struct dm_cache_migration *mg, *tmp;
  1163. spin_lock_irqsave(&cache->lock, flags);
  1164. list_for_each_entry_safe(mg, tmp, work, list)
  1165. __queue_quiesced_migration(mg);
  1166. spin_unlock_irqrestore(&cache->lock, flags);
  1167. wake_worker(cache);
  1168. }
  1169. static void check_for_quiesced_migrations(struct cache *cache,
  1170. struct per_bio_data *pb)
  1171. {
  1172. struct list_head work;
  1173. if (!pb->all_io_entry)
  1174. return;
  1175. INIT_LIST_HEAD(&work);
  1176. dm_deferred_entry_dec(pb->all_io_entry, &work);
  1177. if (!list_empty(&work))
  1178. queue_quiesced_migrations(cache, &work);
  1179. }
  1180. static void quiesce_migration(struct dm_cache_migration *mg)
  1181. {
  1182. if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
  1183. queue_quiesced_migration(mg);
  1184. }
  1185. static void promote(struct cache *cache, struct prealloc *structs,
  1186. dm_oblock_t oblock, dm_cblock_t cblock,
  1187. struct dm_bio_prison_cell *cell)
  1188. {
  1189. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1190. mg->err = false;
  1191. mg->discard = false;
  1192. mg->writeback = false;
  1193. mg->demote = false;
  1194. mg->promote = true;
  1195. mg->requeue_holder = true;
  1196. mg->invalidate = false;
  1197. mg->cache = cache;
  1198. mg->new_oblock = oblock;
  1199. mg->cblock = cblock;
  1200. mg->old_ocell = NULL;
  1201. mg->new_ocell = cell;
  1202. mg->start_jiffies = jiffies;
  1203. inc_io_migrations(cache);
  1204. quiesce_migration(mg);
  1205. }
  1206. static void writeback(struct cache *cache, struct prealloc *structs,
  1207. dm_oblock_t oblock, dm_cblock_t cblock,
  1208. struct dm_bio_prison_cell *cell)
  1209. {
  1210. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1211. mg->err = false;
  1212. mg->discard = false;
  1213. mg->writeback = true;
  1214. mg->demote = false;
  1215. mg->promote = false;
  1216. mg->requeue_holder = true;
  1217. mg->invalidate = false;
  1218. mg->cache = cache;
  1219. mg->old_oblock = oblock;
  1220. mg->cblock = cblock;
  1221. mg->old_ocell = cell;
  1222. mg->new_ocell = NULL;
  1223. mg->start_jiffies = jiffies;
  1224. inc_io_migrations(cache);
  1225. quiesce_migration(mg);
  1226. }
  1227. static void demote_then_promote(struct cache *cache, struct prealloc *structs,
  1228. dm_oblock_t old_oblock, dm_oblock_t new_oblock,
  1229. dm_cblock_t cblock,
  1230. struct dm_bio_prison_cell *old_ocell,
  1231. struct dm_bio_prison_cell *new_ocell)
  1232. {
  1233. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1234. mg->err = false;
  1235. mg->discard = false;
  1236. mg->writeback = false;
  1237. mg->demote = true;
  1238. mg->promote = true;
  1239. mg->requeue_holder = true;
  1240. mg->invalidate = false;
  1241. mg->cache = cache;
  1242. mg->old_oblock = old_oblock;
  1243. mg->new_oblock = new_oblock;
  1244. mg->cblock = cblock;
  1245. mg->old_ocell = old_ocell;
  1246. mg->new_ocell = new_ocell;
  1247. mg->start_jiffies = jiffies;
  1248. inc_io_migrations(cache);
  1249. quiesce_migration(mg);
  1250. }
  1251. /*
  1252. * Invalidate a cache entry. No writeback occurs; any changes in the cache
  1253. * block are thrown away.
  1254. */
  1255. static void invalidate(struct cache *cache, struct prealloc *structs,
  1256. dm_oblock_t oblock, dm_cblock_t cblock,
  1257. struct dm_bio_prison_cell *cell)
  1258. {
  1259. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1260. mg->err = false;
  1261. mg->discard = false;
  1262. mg->writeback = false;
  1263. mg->demote = true;
  1264. mg->promote = false;
  1265. mg->requeue_holder = true;
  1266. mg->invalidate = true;
  1267. mg->cache = cache;
  1268. mg->old_oblock = oblock;
  1269. mg->cblock = cblock;
  1270. mg->old_ocell = cell;
  1271. mg->new_ocell = NULL;
  1272. mg->start_jiffies = jiffies;
  1273. inc_io_migrations(cache);
  1274. quiesce_migration(mg);
  1275. }
  1276. static void discard(struct cache *cache, struct prealloc *structs,
  1277. struct dm_bio_prison_cell *cell)
  1278. {
  1279. struct dm_cache_migration *mg = prealloc_get_migration(structs);
  1280. mg->err = false;
  1281. mg->discard = true;
  1282. mg->writeback = false;
  1283. mg->demote = false;
  1284. mg->promote = false;
  1285. mg->requeue_holder = false;
  1286. mg->invalidate = false;
  1287. mg->cache = cache;
  1288. mg->old_ocell = NULL;
  1289. mg->new_ocell = cell;
  1290. mg->start_jiffies = jiffies;
  1291. quiesce_migration(mg);
  1292. }
  1293. /*----------------------------------------------------------------
  1294. * bio processing
  1295. *--------------------------------------------------------------*/
  1296. static void defer_bio(struct cache *cache, struct bio *bio)
  1297. {
  1298. unsigned long flags;
  1299. spin_lock_irqsave(&cache->lock, flags);
  1300. bio_list_add(&cache->deferred_bios, bio);
  1301. spin_unlock_irqrestore(&cache->lock, flags);
  1302. wake_worker(cache);
  1303. }
  1304. static void process_flush_bio(struct cache *cache, struct bio *bio)
  1305. {
  1306. size_t pb_data_size = get_per_bio_data_size(cache);
  1307. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  1308. BUG_ON(bio->bi_iter.bi_size);
  1309. if (!pb->req_nr)
  1310. remap_to_origin(cache, bio);
  1311. else
  1312. remap_to_cache(cache, bio, 0);
  1313. /*
  1314. * REQ_FLUSH is not directed at any particular block so we don't
  1315. * need to inc_ds(). REQ_FUA's are split into a write + REQ_FLUSH
  1316. * by dm-core.
  1317. */
  1318. issue(cache, bio);
  1319. }
  1320. static void process_discard_bio(struct cache *cache, struct prealloc *structs,
  1321. struct bio *bio)
  1322. {
  1323. int r;
  1324. dm_dblock_t b, e;
  1325. struct dm_bio_prison_cell *cell_prealloc, *new_ocell;
  1326. calc_discard_block_range(cache, bio, &b, &e);
  1327. if (b == e) {
  1328. bio_endio(bio);
  1329. return;
  1330. }
  1331. cell_prealloc = prealloc_get_cell(structs);
  1332. r = bio_detain_range(cache, dblock_to_oblock(cache, b), dblock_to_oblock(cache, e), bio, cell_prealloc,
  1333. (cell_free_fn) prealloc_put_cell,
  1334. structs, &new_ocell);
  1335. if (r > 0)
  1336. return;
  1337. discard(cache, structs, new_ocell);
  1338. }
  1339. static bool spare_migration_bandwidth(struct cache *cache)
  1340. {
  1341. sector_t current_volume = (atomic_read(&cache->nr_io_migrations) + 1) *
  1342. cache->sectors_per_block;
  1343. return current_volume < cache->migration_threshold;
  1344. }
  1345. static void inc_hit_counter(struct cache *cache, struct bio *bio)
  1346. {
  1347. atomic_inc(bio_data_dir(bio) == READ ?
  1348. &cache->stats.read_hit : &cache->stats.write_hit);
  1349. }
  1350. static void inc_miss_counter(struct cache *cache, struct bio *bio)
  1351. {
  1352. atomic_inc(bio_data_dir(bio) == READ ?
  1353. &cache->stats.read_miss : &cache->stats.write_miss);
  1354. }
  1355. /*----------------------------------------------------------------*/
  1356. struct inc_detail {
  1357. struct cache *cache;
  1358. struct bio_list bios_for_issue;
  1359. struct bio_list unhandled_bios;
  1360. bool any_writes;
  1361. };
  1362. static void inc_fn(void *context, struct dm_bio_prison_cell *cell)
  1363. {
  1364. struct bio *bio;
  1365. struct inc_detail *detail = context;
  1366. struct cache *cache = detail->cache;
  1367. inc_ds(cache, cell->holder, cell);
  1368. if (bio_data_dir(cell->holder) == WRITE)
  1369. detail->any_writes = true;
  1370. while ((bio = bio_list_pop(&cell->bios))) {
  1371. if (discard_or_flush(bio)) {
  1372. bio_list_add(&detail->unhandled_bios, bio);
  1373. continue;
  1374. }
  1375. if (bio_data_dir(bio) == WRITE)
  1376. detail->any_writes = true;
  1377. bio_list_add(&detail->bios_for_issue, bio);
  1378. inc_ds(cache, bio, cell);
  1379. }
  1380. }
  1381. // FIXME: refactor these two
  1382. static void remap_cell_to_origin_clear_discard(struct cache *cache,
  1383. struct dm_bio_prison_cell *cell,
  1384. dm_oblock_t oblock, bool issue_holder)
  1385. {
  1386. struct bio *bio;
  1387. unsigned long flags;
  1388. struct inc_detail detail;
  1389. detail.cache = cache;
  1390. bio_list_init(&detail.bios_for_issue);
  1391. bio_list_init(&detail.unhandled_bios);
  1392. detail.any_writes = false;
  1393. spin_lock_irqsave(&cache->lock, flags);
  1394. dm_cell_visit_release(cache->prison, inc_fn, &detail, cell);
  1395. bio_list_merge(&cache->deferred_bios, &detail.unhandled_bios);
  1396. spin_unlock_irqrestore(&cache->lock, flags);
  1397. remap_to_origin(cache, cell->holder);
  1398. if (issue_holder)
  1399. issue(cache, cell->holder);
  1400. else
  1401. accounted_begin(cache, cell->holder);
  1402. if (detail.any_writes)
  1403. clear_discard(cache, oblock_to_dblock(cache, oblock));
  1404. while ((bio = bio_list_pop(&detail.bios_for_issue))) {
  1405. remap_to_origin(cache, bio);
  1406. issue(cache, bio);
  1407. }
  1408. free_prison_cell(cache, cell);
  1409. }
  1410. static void remap_cell_to_cache_dirty(struct cache *cache, struct dm_bio_prison_cell *cell,
  1411. dm_oblock_t oblock, dm_cblock_t cblock, bool issue_holder)
  1412. {
  1413. struct bio *bio;
  1414. unsigned long flags;
  1415. struct inc_detail detail;
  1416. detail.cache = cache;
  1417. bio_list_init(&detail.bios_for_issue);
  1418. bio_list_init(&detail.unhandled_bios);
  1419. detail.any_writes = false;
  1420. spin_lock_irqsave(&cache->lock, flags);
  1421. dm_cell_visit_release(cache->prison, inc_fn, &detail, cell);
  1422. bio_list_merge(&cache->deferred_bios, &detail.unhandled_bios);
  1423. spin_unlock_irqrestore(&cache->lock, flags);
  1424. remap_to_cache(cache, cell->holder, cblock);
  1425. if (issue_holder)
  1426. issue(cache, cell->holder);
  1427. else
  1428. accounted_begin(cache, cell->holder);
  1429. if (detail.any_writes) {
  1430. set_dirty(cache, oblock, cblock);
  1431. clear_discard(cache, oblock_to_dblock(cache, oblock));
  1432. }
  1433. while ((bio = bio_list_pop(&detail.bios_for_issue))) {
  1434. remap_to_cache(cache, bio, cblock);
  1435. issue(cache, bio);
  1436. }
  1437. free_prison_cell(cache, cell);
  1438. }
  1439. /*----------------------------------------------------------------*/
  1440. struct old_oblock_lock {
  1441. struct policy_locker locker;
  1442. struct cache *cache;
  1443. struct prealloc *structs;
  1444. struct dm_bio_prison_cell *cell;
  1445. };
  1446. static int null_locker(struct policy_locker *locker, dm_oblock_t b)
  1447. {
  1448. /* This should never be called */
  1449. BUG();
  1450. return 0;
  1451. }
  1452. static int cell_locker(struct policy_locker *locker, dm_oblock_t b)
  1453. {
  1454. struct old_oblock_lock *l = container_of(locker, struct old_oblock_lock, locker);
  1455. struct dm_bio_prison_cell *cell_prealloc = prealloc_get_cell(l->structs);
  1456. return bio_detain(l->cache, b, NULL, cell_prealloc,
  1457. (cell_free_fn) prealloc_put_cell,
  1458. l->structs, &l->cell);
  1459. }
  1460. static void process_cell(struct cache *cache, struct prealloc *structs,
  1461. struct dm_bio_prison_cell *new_ocell)
  1462. {
  1463. int r;
  1464. bool release_cell = true;
  1465. struct bio *bio = new_ocell->holder;
  1466. dm_oblock_t block = get_bio_block(cache, bio);
  1467. struct policy_result lookup_result;
  1468. bool passthrough = passthrough_mode(&cache->features);
  1469. bool fast_promotion, can_migrate;
  1470. struct old_oblock_lock ool;
  1471. fast_promotion = is_discarded_oblock(cache, block) || bio_writes_complete_block(cache, bio);
  1472. can_migrate = !passthrough && (fast_promotion || spare_migration_bandwidth(cache));
  1473. ool.locker.fn = cell_locker;
  1474. ool.cache = cache;
  1475. ool.structs = structs;
  1476. ool.cell = NULL;
  1477. r = policy_map(cache->policy, block, true, can_migrate, fast_promotion,
  1478. bio, &ool.locker, &lookup_result);
  1479. if (r == -EWOULDBLOCK)
  1480. /* migration has been denied */
  1481. lookup_result.op = POLICY_MISS;
  1482. switch (lookup_result.op) {
  1483. case POLICY_HIT:
  1484. if (passthrough) {
  1485. inc_miss_counter(cache, bio);
  1486. /*
  1487. * Passthrough always maps to the origin,
  1488. * invalidating any cache blocks that are written
  1489. * to.
  1490. */
  1491. if (bio_data_dir(bio) == WRITE) {
  1492. atomic_inc(&cache->stats.demotion);
  1493. invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
  1494. release_cell = false;
  1495. } else {
  1496. /* FIXME: factor out issue_origin() */
  1497. remap_to_origin_clear_discard(cache, bio, block);
  1498. inc_and_issue(cache, bio, new_ocell);
  1499. }
  1500. } else {
  1501. inc_hit_counter(cache, bio);
  1502. if (bio_data_dir(bio) == WRITE &&
  1503. writethrough_mode(&cache->features) &&
  1504. !is_dirty(cache, lookup_result.cblock)) {
  1505. remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
  1506. inc_and_issue(cache, bio, new_ocell);
  1507. } else {
  1508. remap_cell_to_cache_dirty(cache, new_ocell, block, lookup_result.cblock, true);
  1509. release_cell = false;
  1510. }
  1511. }
  1512. break;
  1513. case POLICY_MISS:
  1514. inc_miss_counter(cache, bio);
  1515. remap_cell_to_origin_clear_discard(cache, new_ocell, block, true);
  1516. release_cell = false;
  1517. break;
  1518. case POLICY_NEW:
  1519. atomic_inc(&cache->stats.promotion);
  1520. promote(cache, structs, block, lookup_result.cblock, new_ocell);
  1521. release_cell = false;
  1522. break;
  1523. case POLICY_REPLACE:
  1524. atomic_inc(&cache->stats.demotion);
  1525. atomic_inc(&cache->stats.promotion);
  1526. demote_then_promote(cache, structs, lookup_result.old_oblock,
  1527. block, lookup_result.cblock,
  1528. ool.cell, new_ocell);
  1529. release_cell = false;
  1530. break;
  1531. default:
  1532. DMERR_LIMIT("%s: %s: erroring bio, unknown policy op: %u",
  1533. cache_device_name(cache), __func__,
  1534. (unsigned) lookup_result.op);
  1535. bio_io_error(bio);
  1536. }
  1537. if (release_cell)
  1538. cell_defer(cache, new_ocell, false);
  1539. }
  1540. static void process_bio(struct cache *cache, struct prealloc *structs,
  1541. struct bio *bio)
  1542. {
  1543. int r;
  1544. dm_oblock_t block = get_bio_block(cache, bio);
  1545. struct dm_bio_prison_cell *cell_prealloc, *new_ocell;
  1546. /*
  1547. * Check to see if that block is currently migrating.
  1548. */
  1549. cell_prealloc = prealloc_get_cell(structs);
  1550. r = bio_detain(cache, block, bio, cell_prealloc,
  1551. (cell_free_fn) prealloc_put_cell,
  1552. structs, &new_ocell);
  1553. if (r > 0)
  1554. return;
  1555. process_cell(cache, structs, new_ocell);
  1556. }
  1557. static int need_commit_due_to_time(struct cache *cache)
  1558. {
  1559. return jiffies < cache->last_commit_jiffies ||
  1560. jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
  1561. }
  1562. /*
  1563. * A non-zero return indicates read_only or fail_io mode.
  1564. */
  1565. static int commit(struct cache *cache, bool clean_shutdown)
  1566. {
  1567. int r;
  1568. if (get_cache_mode(cache) >= CM_READ_ONLY)
  1569. return -EINVAL;
  1570. atomic_inc(&cache->stats.commit_count);
  1571. r = dm_cache_commit(cache->cmd, clean_shutdown);
  1572. if (r)
  1573. metadata_operation_failed(cache, "dm_cache_commit", r);
  1574. return r;
  1575. }
  1576. static int commit_if_needed(struct cache *cache)
  1577. {
  1578. int r = 0;
  1579. if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
  1580. dm_cache_changed_this_transaction(cache->cmd)) {
  1581. r = commit(cache, false);
  1582. cache->commit_requested = false;
  1583. cache->last_commit_jiffies = jiffies;
  1584. }
  1585. return r;
  1586. }
  1587. static void process_deferred_bios(struct cache *cache)
  1588. {
  1589. bool prealloc_used = false;
  1590. unsigned long flags;
  1591. struct bio_list bios;
  1592. struct bio *bio;
  1593. struct prealloc structs;
  1594. memset(&structs, 0, sizeof(structs));
  1595. bio_list_init(&bios);
  1596. spin_lock_irqsave(&cache->lock, flags);
  1597. bio_list_merge(&bios, &cache->deferred_bios);
  1598. bio_list_init(&cache->deferred_bios);
  1599. spin_unlock_irqrestore(&cache->lock, flags);
  1600. while (!bio_list_empty(&bios)) {
  1601. /*
  1602. * If we've got no free migration structs, and processing
  1603. * this bio might require one, we pause until there are some
  1604. * prepared mappings to process.
  1605. */
  1606. prealloc_used = true;
  1607. if (prealloc_data_structs(cache, &structs)) {
  1608. spin_lock_irqsave(&cache->lock, flags);
  1609. bio_list_merge(&cache->deferred_bios, &bios);
  1610. spin_unlock_irqrestore(&cache->lock, flags);
  1611. break;
  1612. }
  1613. bio = bio_list_pop(&bios);
  1614. if (bio->bi_rw & REQ_FLUSH)
  1615. process_flush_bio(cache, bio);
  1616. else if (bio->bi_rw & REQ_DISCARD)
  1617. process_discard_bio(cache, &structs, bio);
  1618. else
  1619. process_bio(cache, &structs, bio);
  1620. }
  1621. if (prealloc_used)
  1622. prealloc_free_structs(cache, &structs);
  1623. }
  1624. static void process_deferred_cells(struct cache *cache)
  1625. {
  1626. bool prealloc_used = false;
  1627. unsigned long flags;
  1628. struct dm_bio_prison_cell *cell, *tmp;
  1629. struct list_head cells;
  1630. struct prealloc structs;
  1631. memset(&structs, 0, sizeof(structs));
  1632. INIT_LIST_HEAD(&cells);
  1633. spin_lock_irqsave(&cache->lock, flags);
  1634. list_splice_init(&cache->deferred_cells, &cells);
  1635. spin_unlock_irqrestore(&cache->lock, flags);
  1636. list_for_each_entry_safe(cell, tmp, &cells, user_list) {
  1637. /*
  1638. * If we've got no free migration structs, and processing
  1639. * this bio might require one, we pause until there are some
  1640. * prepared mappings to process.
  1641. */
  1642. prealloc_used = true;
  1643. if (prealloc_data_structs(cache, &structs)) {
  1644. spin_lock_irqsave(&cache->lock, flags);
  1645. list_splice(&cells, &cache->deferred_cells);
  1646. spin_unlock_irqrestore(&cache->lock, flags);
  1647. break;
  1648. }
  1649. process_cell(cache, &structs, cell);
  1650. }
  1651. if (prealloc_used)
  1652. prealloc_free_structs(cache, &structs);
  1653. }
  1654. static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
  1655. {
  1656. unsigned long flags;
  1657. struct bio_list bios;
  1658. struct bio *bio;
  1659. bio_list_init(&bios);
  1660. spin_lock_irqsave(&cache->lock, flags);
  1661. bio_list_merge(&bios, &cache->deferred_flush_bios);
  1662. bio_list_init(&cache->deferred_flush_bios);
  1663. spin_unlock_irqrestore(&cache->lock, flags);
  1664. /*
  1665. * These bios have already been through inc_ds()
  1666. */
  1667. while ((bio = bio_list_pop(&bios)))
  1668. submit_bios ? accounted_request(cache, bio) : bio_io_error(bio);
  1669. }
  1670. static void process_deferred_writethrough_bios(struct cache *cache)
  1671. {
  1672. unsigned long flags;
  1673. struct bio_list bios;
  1674. struct bio *bio;
  1675. bio_list_init(&bios);
  1676. spin_lock_irqsave(&cache->lock, flags);
  1677. bio_list_merge(&bios, &cache->deferred_writethrough_bios);
  1678. bio_list_init(&cache->deferred_writethrough_bios);
  1679. spin_unlock_irqrestore(&cache->lock, flags);
  1680. /*
  1681. * These bios have already been through inc_ds()
  1682. */
  1683. while ((bio = bio_list_pop(&bios)))
  1684. accounted_request(cache, bio);
  1685. }
  1686. static void writeback_some_dirty_blocks(struct cache *cache)
  1687. {
  1688. bool prealloc_used = false;
  1689. dm_oblock_t oblock;
  1690. dm_cblock_t cblock;
  1691. struct prealloc structs;
  1692. struct dm_bio_prison_cell *old_ocell;
  1693. bool busy = !iot_idle_for(&cache->origin_tracker, HZ);
  1694. memset(&structs, 0, sizeof(structs));
  1695. while (spare_migration_bandwidth(cache)) {
  1696. if (policy_writeback_work(cache->policy, &oblock, &cblock, busy))
  1697. break; /* no work to do */
  1698. prealloc_used = true;
  1699. if (prealloc_data_structs(cache, &structs) ||
  1700. get_cell(cache, oblock, &structs, &old_ocell)) {
  1701. policy_set_dirty(cache->policy, oblock);
  1702. break;
  1703. }
  1704. writeback(cache, &structs, oblock, cblock, old_ocell);
  1705. }
  1706. if (prealloc_used)
  1707. prealloc_free_structs(cache, &structs);
  1708. }
  1709. /*----------------------------------------------------------------
  1710. * Invalidations.
  1711. * Dropping something from the cache *without* writing back.
  1712. *--------------------------------------------------------------*/
  1713. static void process_invalidation_request(struct cache *cache, struct invalidation_request *req)
  1714. {
  1715. int r = 0;
  1716. uint64_t begin = from_cblock(req->cblocks->begin);
  1717. uint64_t end = from_cblock(req->cblocks->end);
  1718. while (begin != end) {
  1719. r = policy_remove_cblock(cache->policy, to_cblock(begin));
  1720. if (!r) {
  1721. r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin));
  1722. if (r) {
  1723. metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
  1724. break;
  1725. }
  1726. } else if (r == -ENODATA) {
  1727. /* harmless, already unmapped */
  1728. r = 0;
  1729. } else {
  1730. DMERR("%s: policy_remove_cblock failed", cache_device_name(cache));
  1731. break;
  1732. }
  1733. begin++;
  1734. }
  1735. cache->commit_requested = true;
  1736. req->err = r;
  1737. atomic_set(&req->complete, 1);
  1738. wake_up(&req->result_wait);
  1739. }
  1740. static void process_invalidation_requests(struct cache *cache)
  1741. {
  1742. struct list_head list;
  1743. struct invalidation_request *req, *tmp;
  1744. INIT_LIST_HEAD(&list);
  1745. spin_lock(&cache->invalidation_lock);
  1746. list_splice_init(&cache->invalidation_requests, &list);
  1747. spin_unlock(&cache->invalidation_lock);
  1748. list_for_each_entry_safe (req, tmp, &list, list)
  1749. process_invalidation_request(cache, req);
  1750. }
  1751. /*----------------------------------------------------------------
  1752. * Main worker loop
  1753. *--------------------------------------------------------------*/
  1754. static bool is_quiescing(struct cache *cache)
  1755. {
  1756. return atomic_read(&cache->quiescing);
  1757. }
  1758. static void ack_quiescing(struct cache *cache)
  1759. {
  1760. if (is_quiescing(cache)) {
  1761. atomic_inc(&cache->quiescing_ack);
  1762. wake_up(&cache->quiescing_wait);
  1763. }
  1764. }
  1765. static void wait_for_quiescing_ack(struct cache *cache)
  1766. {
  1767. wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
  1768. }
  1769. static void start_quiescing(struct cache *cache)
  1770. {
  1771. atomic_inc(&cache->quiescing);
  1772. wait_for_quiescing_ack(cache);
  1773. }
  1774. static void stop_quiescing(struct cache *cache)
  1775. {
  1776. atomic_set(&cache->quiescing, 0);
  1777. atomic_set(&cache->quiescing_ack, 0);
  1778. }
  1779. static void wait_for_migrations(struct cache *cache)
  1780. {
  1781. wait_event(cache->migration_wait, !atomic_read(&cache->nr_allocated_migrations));
  1782. }
  1783. static void stop_worker(struct cache *cache)
  1784. {
  1785. cancel_delayed_work(&cache->waker);
  1786. flush_workqueue(cache->wq);
  1787. }
  1788. static void requeue_deferred_cells(struct cache *cache)
  1789. {
  1790. unsigned long flags;
  1791. struct list_head cells;
  1792. struct dm_bio_prison_cell *cell, *tmp;
  1793. INIT_LIST_HEAD(&cells);
  1794. spin_lock_irqsave(&cache->lock, flags);
  1795. list_splice_init(&cache->deferred_cells, &cells);
  1796. spin_unlock_irqrestore(&cache->lock, flags);
  1797. list_for_each_entry_safe(cell, tmp, &cells, user_list)
  1798. cell_requeue(cache, cell);
  1799. }
  1800. static void requeue_deferred_bios(struct cache *cache)
  1801. {
  1802. struct bio *bio;
  1803. struct bio_list bios;
  1804. bio_list_init(&bios);
  1805. bio_list_merge(&bios, &cache->deferred_bios);
  1806. bio_list_init(&cache->deferred_bios);
  1807. while ((bio = bio_list_pop(&bios))) {
  1808. bio->bi_error = DM_ENDIO_REQUEUE;
  1809. bio_endio(bio);
  1810. }
  1811. }
  1812. static int more_work(struct cache *cache)
  1813. {
  1814. if (is_quiescing(cache))
  1815. return !list_empty(&cache->quiesced_migrations) ||
  1816. !list_empty(&cache->completed_migrations) ||
  1817. !list_empty(&cache->need_commit_migrations);
  1818. else
  1819. return !bio_list_empty(&cache->deferred_bios) ||
  1820. !list_empty(&cache->deferred_cells) ||
  1821. !bio_list_empty(&cache->deferred_flush_bios) ||
  1822. !bio_list_empty(&cache->deferred_writethrough_bios) ||
  1823. !list_empty(&cache->quiesced_migrations) ||
  1824. !list_empty(&cache->completed_migrations) ||
  1825. !list_empty(&cache->need_commit_migrations) ||
  1826. cache->invalidate;
  1827. }
  1828. static void do_worker(struct work_struct *ws)
  1829. {
  1830. struct cache *cache = container_of(ws, struct cache, worker);
  1831. do {
  1832. if (!is_quiescing(cache)) {
  1833. writeback_some_dirty_blocks(cache);
  1834. process_deferred_writethrough_bios(cache);
  1835. process_deferred_bios(cache);
  1836. process_deferred_cells(cache);
  1837. process_invalidation_requests(cache);
  1838. }
  1839. process_migrations(cache, &cache->quiesced_migrations, issue_copy_or_discard);
  1840. process_migrations(cache, &cache->completed_migrations, complete_migration);
  1841. if (commit_if_needed(cache)) {
  1842. process_deferred_flush_bios(cache, false);
  1843. process_migrations(cache, &cache->need_commit_migrations, migration_failure);
  1844. } else {
  1845. process_deferred_flush_bios(cache, true);
  1846. process_migrations(cache, &cache->need_commit_migrations,
  1847. migration_success_post_commit);
  1848. }
  1849. ack_quiescing(cache);
  1850. } while (more_work(cache));
  1851. }
  1852. /*
  1853. * We want to commit periodically so that not too much
  1854. * unwritten metadata builds up.
  1855. */
  1856. static void do_waker(struct work_struct *ws)
  1857. {
  1858. struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
  1859. policy_tick(cache->policy, true);
  1860. wake_worker(cache);
  1861. queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
  1862. }
  1863. /*----------------------------------------------------------------*/
  1864. static int is_congested(struct dm_dev *dev, int bdi_bits)
  1865. {
  1866. struct request_queue *q = bdev_get_queue(dev->bdev);
  1867. return bdi_congested(&q->backing_dev_info, bdi_bits);
  1868. }
  1869. static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
  1870. {
  1871. struct cache *cache = container_of(cb, struct cache, callbacks);
  1872. return is_congested(cache->origin_dev, bdi_bits) ||
  1873. is_congested(cache->cache_dev, bdi_bits);
  1874. }
  1875. /*----------------------------------------------------------------
  1876. * Target methods
  1877. *--------------------------------------------------------------*/
  1878. /*
  1879. * This function gets called on the error paths of the constructor, so we
  1880. * have to cope with a partially initialised struct.
  1881. */
  1882. static void destroy(struct cache *cache)
  1883. {
  1884. unsigned i;
  1885. if (cache->migration_pool)
  1886. mempool_destroy(cache->migration_pool);
  1887. if (cache->all_io_ds)
  1888. dm_deferred_set_destroy(cache->all_io_ds);
  1889. if (cache->prison)
  1890. dm_bio_prison_destroy(cache->prison);
  1891. if (cache->wq)
  1892. destroy_workqueue(cache->wq);
  1893. if (cache->dirty_bitset)
  1894. free_bitset(cache->dirty_bitset);
  1895. if (cache->discard_bitset)
  1896. free_bitset(cache->discard_bitset);
  1897. if (cache->copier)
  1898. dm_kcopyd_client_destroy(cache->copier);
  1899. if (cache->cmd)
  1900. dm_cache_metadata_close(cache->cmd);
  1901. if (cache->metadata_dev)
  1902. dm_put_device(cache->ti, cache->metadata_dev);
  1903. if (cache->origin_dev)
  1904. dm_put_device(cache->ti, cache->origin_dev);
  1905. if (cache->cache_dev)
  1906. dm_put_device(cache->ti, cache->cache_dev);
  1907. if (cache->policy)
  1908. dm_cache_policy_destroy(cache->policy);
  1909. for (i = 0; i < cache->nr_ctr_args ; i++)
  1910. kfree(cache->ctr_args[i]);
  1911. kfree(cache->ctr_args);
  1912. kfree(cache);
  1913. }
  1914. static void cache_dtr(struct dm_target *ti)
  1915. {
  1916. struct cache *cache = ti->private;
  1917. destroy(cache);
  1918. }
  1919. static sector_t get_dev_size(struct dm_dev *dev)
  1920. {
  1921. return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
  1922. }
  1923. /*----------------------------------------------------------------*/
  1924. /*
  1925. * Construct a cache device mapping.
  1926. *
  1927. * cache <metadata dev> <cache dev> <origin dev> <block size>
  1928. * <#feature args> [<feature arg>]*
  1929. * <policy> <#policy args> [<policy arg>]*
  1930. *
  1931. * metadata dev : fast device holding the persistent metadata
  1932. * cache dev : fast device holding cached data blocks
  1933. * origin dev : slow device holding original data blocks
  1934. * block size : cache unit size in sectors
  1935. *
  1936. * #feature args : number of feature arguments passed
  1937. * feature args : writethrough. (The default is writeback.)
  1938. *
  1939. * policy : the replacement policy to use
  1940. * #policy args : an even number of policy arguments corresponding
  1941. * to key/value pairs passed to the policy
  1942. * policy args : key/value pairs passed to the policy
  1943. * E.g. 'sequential_threshold 1024'
  1944. * See cache-policies.txt for details.
  1945. *
  1946. * Optional feature arguments are:
  1947. * writethrough : write through caching that prohibits cache block
  1948. * content from being different from origin block content.
  1949. * Without this argument, the default behaviour is to write
  1950. * back cache block contents later for performance reasons,
  1951. * so they may differ from the corresponding origin blocks.
  1952. */
  1953. struct cache_args {
  1954. struct dm_target *ti;
  1955. struct dm_dev *metadata_dev;
  1956. struct dm_dev *cache_dev;
  1957. sector_t cache_sectors;
  1958. struct dm_dev *origin_dev;
  1959. sector_t origin_sectors;
  1960. uint32_t block_size;
  1961. const char *policy_name;
  1962. int policy_argc;
  1963. const char **policy_argv;
  1964. struct cache_features features;
  1965. };
  1966. static void destroy_cache_args(struct cache_args *ca)
  1967. {
  1968. if (ca->metadata_dev)
  1969. dm_put_device(ca->ti, ca->metadata_dev);
  1970. if (ca->cache_dev)
  1971. dm_put_device(ca->ti, ca->cache_dev);
  1972. if (ca->origin_dev)
  1973. dm_put_device(ca->ti, ca->origin_dev);
  1974. kfree(ca);
  1975. }
  1976. static bool at_least_one_arg(struct dm_arg_set *as, char **error)
  1977. {
  1978. if (!as->argc) {
  1979. *error = "Insufficient args";
  1980. return false;
  1981. }
  1982. return true;
  1983. }
  1984. static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
  1985. char **error)
  1986. {
  1987. int r;
  1988. sector_t metadata_dev_size;
  1989. char b[BDEVNAME_SIZE];
  1990. if (!at_least_one_arg(as, error))
  1991. return -EINVAL;
  1992. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  1993. &ca->metadata_dev);
  1994. if (r) {
  1995. *error = "Error opening metadata device";
  1996. return r;
  1997. }
  1998. metadata_dev_size = get_dev_size(ca->metadata_dev);
  1999. if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
  2000. DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
  2001. bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
  2002. return 0;
  2003. }
  2004. static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
  2005. char **error)
  2006. {
  2007. int r;
  2008. if (!at_least_one_arg(as, error))
  2009. return -EINVAL;
  2010. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  2011. &ca->cache_dev);
  2012. if (r) {
  2013. *error = "Error opening cache device";
  2014. return r;
  2015. }
  2016. ca->cache_sectors = get_dev_size(ca->cache_dev);
  2017. return 0;
  2018. }
  2019. static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
  2020. char **error)
  2021. {
  2022. int r;
  2023. if (!at_least_one_arg(as, error))
  2024. return -EINVAL;
  2025. r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
  2026. &ca->origin_dev);
  2027. if (r) {
  2028. *error = "Error opening origin device";
  2029. return r;
  2030. }
  2031. ca->origin_sectors = get_dev_size(ca->origin_dev);
  2032. if (ca->ti->len > ca->origin_sectors) {
  2033. *error = "Device size larger than cached device";
  2034. return -EINVAL;
  2035. }
  2036. return 0;
  2037. }
  2038. static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
  2039. char **error)
  2040. {
  2041. unsigned long block_size;
  2042. if (!at_least_one_arg(as, error))
  2043. return -EINVAL;
  2044. if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
  2045. block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
  2046. block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
  2047. block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
  2048. *error = "Invalid data block size";
  2049. return -EINVAL;
  2050. }
  2051. if (block_size > ca->cache_sectors) {
  2052. *error = "Data block size is larger than the cache device";
  2053. return -EINVAL;
  2054. }
  2055. ca->block_size = block_size;
  2056. return 0;
  2057. }
  2058. static void init_features(struct cache_features *cf)
  2059. {
  2060. cf->mode = CM_WRITE;
  2061. cf->io_mode = CM_IO_WRITEBACK;
  2062. }
  2063. static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
  2064. char **error)
  2065. {
  2066. static struct dm_arg _args[] = {
  2067. {0, 1, "Invalid number of cache feature arguments"},
  2068. };
  2069. int r;
  2070. unsigned argc;
  2071. const char *arg;
  2072. struct cache_features *cf = &ca->features;
  2073. init_features(cf);
  2074. r = dm_read_arg_group(_args, as, &argc, error);
  2075. if (r)
  2076. return -EINVAL;
  2077. while (argc--) {
  2078. arg = dm_shift_arg(as);
  2079. if (!strcasecmp(arg, "writeback"))
  2080. cf->io_mode = CM_IO_WRITEBACK;
  2081. else if (!strcasecmp(arg, "writethrough"))
  2082. cf->io_mode = CM_IO_WRITETHROUGH;
  2083. else if (!strcasecmp(arg, "passthrough"))
  2084. cf->io_mode = CM_IO_PASSTHROUGH;
  2085. else {
  2086. *error = "Unrecognised cache feature requested";
  2087. return -EINVAL;
  2088. }
  2089. }
  2090. return 0;
  2091. }
  2092. static int parse_policy(struct cache_args *ca, struct dm_arg_set *as,
  2093. char **error)
  2094. {
  2095. static struct dm_arg _args[] = {
  2096. {0, 1024, "Invalid number of policy arguments"},
  2097. };
  2098. int r;
  2099. if (!at_least_one_arg(as, error))
  2100. return -EINVAL;
  2101. ca->policy_name = dm_shift_arg(as);
  2102. r = dm_read_arg_group(_args, as, &ca->policy_argc, error);
  2103. if (r)
  2104. return -EINVAL;
  2105. ca->policy_argv = (const char **)as->argv;
  2106. dm_consume_args(as, ca->policy_argc);
  2107. return 0;
  2108. }
  2109. static int parse_cache_args(struct cache_args *ca, int argc, char **argv,
  2110. char **error)
  2111. {
  2112. int r;
  2113. struct dm_arg_set as;
  2114. as.argc = argc;
  2115. as.argv = argv;
  2116. r = parse_metadata_dev(ca, &as, error);
  2117. if (r)
  2118. return r;
  2119. r = parse_cache_dev(ca, &as, error);
  2120. if (r)
  2121. return r;
  2122. r = parse_origin_dev(ca, &as, error);
  2123. if (r)
  2124. return r;
  2125. r = parse_block_size(ca, &as, error);
  2126. if (r)
  2127. return r;
  2128. r = parse_features(ca, &as, error);
  2129. if (r)
  2130. return r;
  2131. r = parse_policy(ca, &as, error);
  2132. if (r)
  2133. return r;
  2134. return 0;
  2135. }
  2136. /*----------------------------------------------------------------*/
  2137. static struct kmem_cache *migration_cache;
  2138. #define NOT_CORE_OPTION 1
  2139. static int process_config_option(struct cache *cache, const char *key, const char *value)
  2140. {
  2141. unsigned long tmp;
  2142. if (!strcasecmp(key, "migration_threshold")) {
  2143. if (kstrtoul(value, 10, &tmp))
  2144. return -EINVAL;
  2145. cache->migration_threshold = tmp;
  2146. return 0;
  2147. }
  2148. return NOT_CORE_OPTION;
  2149. }
  2150. static int set_config_value(struct cache *cache, const char *key, const char *value)
  2151. {
  2152. int r = process_config_option(cache, key, value);
  2153. if (r == NOT_CORE_OPTION)
  2154. r = policy_set_config_value(cache->policy, key, value);
  2155. if (r)
  2156. DMWARN("bad config value for %s: %s", key, value);
  2157. return r;
  2158. }
  2159. static int set_config_values(struct cache *cache, int argc, const char **argv)
  2160. {
  2161. int r = 0;
  2162. if (argc & 1) {
  2163. DMWARN("Odd number of policy arguments given but they should be <key> <value> pairs.");
  2164. return -EINVAL;
  2165. }
  2166. while (argc) {
  2167. r = set_config_value(cache, argv[0], argv[1]);
  2168. if (r)
  2169. break;
  2170. argc -= 2;
  2171. argv += 2;
  2172. }
  2173. return r;
  2174. }
  2175. static int create_cache_policy(struct cache *cache, struct cache_args *ca,
  2176. char **error)
  2177. {
  2178. struct dm_cache_policy *p = dm_cache_policy_create(ca->policy_name,
  2179. cache->cache_size,
  2180. cache->origin_sectors,
  2181. cache->sectors_per_block);
  2182. if (IS_ERR(p)) {
  2183. *error = "Error creating cache's policy";
  2184. return PTR_ERR(p);
  2185. }
  2186. cache->policy = p;
  2187. return 0;
  2188. }
  2189. /*
  2190. * We want the discard block size to be at least the size of the cache
  2191. * block size and have no more than 2^14 discard blocks across the origin.
  2192. */
  2193. #define MAX_DISCARD_BLOCKS (1 << 14)
  2194. static bool too_many_discard_blocks(sector_t discard_block_size,
  2195. sector_t origin_size)
  2196. {
  2197. (void) sector_div(origin_size, discard_block_size);
  2198. return origin_size > MAX_DISCARD_BLOCKS;
  2199. }
  2200. static sector_t calculate_discard_block_size(sector_t cache_block_size,
  2201. sector_t origin_size)
  2202. {
  2203. sector_t discard_block_size = cache_block_size;
  2204. if (origin_size)
  2205. while (too_many_discard_blocks(discard_block_size, origin_size))
  2206. discard_block_size *= 2;
  2207. return discard_block_size;
  2208. }
  2209. static void set_cache_size(struct cache *cache, dm_cblock_t size)
  2210. {
  2211. dm_block_t nr_blocks = from_cblock(size);
  2212. if (nr_blocks > (1 << 20) && cache->cache_size != size)
  2213. DMWARN_LIMIT("You have created a cache device with a lot of individual cache blocks (%llu)\n"
  2214. "All these mappings can consume a lot of kernel memory, and take some time to read/write.\n"
  2215. "Please consider increasing the cache block size to reduce the overall cache block count.",
  2216. (unsigned long long) nr_blocks);
  2217. cache->cache_size = size;
  2218. }
  2219. #define DEFAULT_MIGRATION_THRESHOLD 2048
  2220. static int cache_create(struct cache_args *ca, struct cache **result)
  2221. {
  2222. int r = 0;
  2223. char **error = &ca->ti->error;
  2224. struct cache *cache;
  2225. struct dm_target *ti = ca->ti;
  2226. dm_block_t origin_blocks;
  2227. struct dm_cache_metadata *cmd;
  2228. bool may_format = ca->features.mode == CM_WRITE;
  2229. cache = kzalloc(sizeof(*cache), GFP_KERNEL);
  2230. if (!cache)
  2231. return -ENOMEM;
  2232. cache->ti = ca->ti;
  2233. ti->private = cache;
  2234. ti->num_flush_bios = 2;
  2235. ti->flush_supported = true;
  2236. ti->num_discard_bios = 1;
  2237. ti->discards_supported = true;
  2238. ti->discard_zeroes_data_unsupported = true;
  2239. ti->split_discard_bios = false;
  2240. cache->features = ca->features;
  2241. ti->per_bio_data_size = get_per_bio_data_size(cache);
  2242. cache->callbacks.congested_fn = cache_is_congested;
  2243. dm_table_add_target_callbacks(ti->table, &cache->callbacks);
  2244. cache->metadata_dev = ca->metadata_dev;
  2245. cache->origin_dev = ca->origin_dev;
  2246. cache->cache_dev = ca->cache_dev;
  2247. ca->metadata_dev = ca->origin_dev = ca->cache_dev = NULL;
  2248. /* FIXME: factor out this whole section */
  2249. origin_blocks = cache->origin_sectors = ca->origin_sectors;
  2250. origin_blocks = block_div(origin_blocks, ca->block_size);
  2251. cache->origin_blocks = to_oblock(origin_blocks);
  2252. cache->sectors_per_block = ca->block_size;
  2253. if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
  2254. r = -EINVAL;
  2255. goto bad;
  2256. }
  2257. if (ca->block_size & (ca->block_size - 1)) {
  2258. dm_block_t cache_size = ca->cache_sectors;
  2259. cache->sectors_per_block_shift = -1;
  2260. cache_size = block_div(cache_size, ca->block_size);
  2261. set_cache_size(cache, to_cblock(cache_size));
  2262. } else {
  2263. cache->sectors_per_block_shift = __ffs(ca->block_size);
  2264. set_cache_size(cache, to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift));
  2265. }
  2266. r = create_cache_policy(cache, ca, error);
  2267. if (r)
  2268. goto bad;
  2269. cache->policy_nr_args = ca->policy_argc;
  2270. cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
  2271. r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
  2272. if (r) {
  2273. *error = "Error setting cache policy's config values";
  2274. goto bad;
  2275. }
  2276. cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
  2277. ca->block_size, may_format,
  2278. dm_cache_policy_get_hint_size(cache->policy));
  2279. if (IS_ERR(cmd)) {
  2280. *error = "Error creating metadata object";
  2281. r = PTR_ERR(cmd);
  2282. goto bad;
  2283. }
  2284. cache->cmd = cmd;
  2285. set_cache_mode(cache, CM_WRITE);
  2286. if (get_cache_mode(cache) != CM_WRITE) {
  2287. *error = "Unable to get write access to metadata, please check/repair metadata.";
  2288. r = -EINVAL;
  2289. goto bad;
  2290. }
  2291. if (passthrough_mode(&cache->features)) {
  2292. bool all_clean;
  2293. r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
  2294. if (r) {
  2295. *error = "dm_cache_metadata_all_clean() failed";
  2296. goto bad;
  2297. }
  2298. if (!all_clean) {
  2299. *error = "Cannot enter passthrough mode unless all blocks are clean";
  2300. r = -EINVAL;
  2301. goto bad;
  2302. }
  2303. }
  2304. spin_lock_init(&cache->lock);
  2305. INIT_LIST_HEAD(&cache->deferred_cells);
  2306. bio_list_init(&cache->deferred_bios);
  2307. bio_list_init(&cache->deferred_flush_bios);
  2308. bio_list_init(&cache->deferred_writethrough_bios);
  2309. INIT_LIST_HEAD(&cache->quiesced_migrations);
  2310. INIT_LIST_HEAD(&cache->completed_migrations);
  2311. INIT_LIST_HEAD(&cache->need_commit_migrations);
  2312. atomic_set(&cache->nr_allocated_migrations, 0);
  2313. atomic_set(&cache->nr_io_migrations, 0);
  2314. init_waitqueue_head(&cache->migration_wait);
  2315. init_waitqueue_head(&cache->quiescing_wait);
  2316. atomic_set(&cache->quiescing, 0);
  2317. atomic_set(&cache->quiescing_ack, 0);
  2318. r = -ENOMEM;
  2319. atomic_set(&cache->nr_dirty, 0);
  2320. cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
  2321. if (!cache->dirty_bitset) {
  2322. *error = "could not allocate dirty bitset";
  2323. goto bad;
  2324. }
  2325. clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
  2326. cache->discard_block_size =
  2327. calculate_discard_block_size(cache->sectors_per_block,
  2328. cache->origin_sectors);
  2329. cache->discard_nr_blocks = to_dblock(dm_sector_div_up(cache->origin_sectors,
  2330. cache->discard_block_size));
  2331. cache->discard_bitset = alloc_bitset(from_dblock(cache->discard_nr_blocks));
  2332. if (!cache->discard_bitset) {
  2333. *error = "could not allocate discard bitset";
  2334. goto bad;
  2335. }
  2336. clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
  2337. cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
  2338. if (IS_ERR(cache->copier)) {
  2339. *error = "could not create kcopyd client";
  2340. r = PTR_ERR(cache->copier);
  2341. goto bad;
  2342. }
  2343. cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
  2344. if (!cache->wq) {
  2345. *error = "could not create workqueue for metadata object";
  2346. goto bad;
  2347. }
  2348. INIT_WORK(&cache->worker, do_worker);
  2349. INIT_DELAYED_WORK(&cache->waker, do_waker);
  2350. cache->last_commit_jiffies = jiffies;
  2351. cache->prison = dm_bio_prison_create();
  2352. if (!cache->prison) {
  2353. *error = "could not create bio prison";
  2354. goto bad;
  2355. }
  2356. cache->all_io_ds = dm_deferred_set_create();
  2357. if (!cache->all_io_ds) {
  2358. *error = "could not create all_io deferred set";
  2359. goto bad;
  2360. }
  2361. cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
  2362. migration_cache);
  2363. if (!cache->migration_pool) {
  2364. *error = "Error creating cache's migration mempool";
  2365. goto bad;
  2366. }
  2367. cache->need_tick_bio = true;
  2368. cache->sized = false;
  2369. cache->invalidate = false;
  2370. cache->commit_requested = false;
  2371. cache->loaded_mappings = false;
  2372. cache->loaded_discards = false;
  2373. load_stats(cache);
  2374. atomic_set(&cache->stats.demotion, 0);
  2375. atomic_set(&cache->stats.promotion, 0);
  2376. atomic_set(&cache->stats.copies_avoided, 0);
  2377. atomic_set(&cache->stats.cache_cell_clash, 0);
  2378. atomic_set(&cache->stats.commit_count, 0);
  2379. atomic_set(&cache->stats.discard_count, 0);
  2380. spin_lock_init(&cache->invalidation_lock);
  2381. INIT_LIST_HEAD(&cache->invalidation_requests);
  2382. iot_init(&cache->origin_tracker);
  2383. *result = cache;
  2384. return 0;
  2385. bad:
  2386. destroy(cache);
  2387. return r;
  2388. }
  2389. static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
  2390. {
  2391. unsigned i;
  2392. const char **copy;
  2393. copy = kcalloc(argc, sizeof(*copy), GFP_KERNEL);
  2394. if (!copy)
  2395. return -ENOMEM;
  2396. for (i = 0; i < argc; i++) {
  2397. copy[i] = kstrdup(argv[i], GFP_KERNEL);
  2398. if (!copy[i]) {
  2399. while (i--)
  2400. kfree(copy[i]);
  2401. kfree(copy);
  2402. return -ENOMEM;
  2403. }
  2404. }
  2405. cache->nr_ctr_args = argc;
  2406. cache->ctr_args = copy;
  2407. return 0;
  2408. }
  2409. static int cache_ctr(struct dm_target *ti, unsigned argc, char **argv)
  2410. {
  2411. int r = -EINVAL;
  2412. struct cache_args *ca;
  2413. struct cache *cache = NULL;
  2414. ca = kzalloc(sizeof(*ca), GFP_KERNEL);
  2415. if (!ca) {
  2416. ti->error = "Error allocating memory for cache";
  2417. return -ENOMEM;
  2418. }
  2419. ca->ti = ti;
  2420. r = parse_cache_args(ca, argc, argv, &ti->error);
  2421. if (r)
  2422. goto out;
  2423. r = cache_create(ca, &cache);
  2424. if (r)
  2425. goto out;
  2426. r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
  2427. if (r) {
  2428. destroy(cache);
  2429. goto out;
  2430. }
  2431. ti->private = cache;
  2432. out:
  2433. destroy_cache_args(ca);
  2434. return r;
  2435. }
  2436. /*----------------------------------------------------------------*/
  2437. static int cache_map(struct dm_target *ti, struct bio *bio)
  2438. {
  2439. struct cache *cache = ti->private;
  2440. int r;
  2441. struct dm_bio_prison_cell *cell = NULL;
  2442. dm_oblock_t block = get_bio_block(cache, bio);
  2443. size_t pb_data_size = get_per_bio_data_size(cache);
  2444. bool can_migrate = false;
  2445. bool fast_promotion;
  2446. struct policy_result lookup_result;
  2447. struct per_bio_data *pb = init_per_bio_data(bio, pb_data_size);
  2448. struct old_oblock_lock ool;
  2449. ool.locker.fn = null_locker;
  2450. if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
  2451. /*
  2452. * This can only occur if the io goes to a partial block at
  2453. * the end of the origin device. We don't cache these.
  2454. * Just remap to the origin and carry on.
  2455. */
  2456. remap_to_origin(cache, bio);
  2457. accounted_begin(cache, bio);
  2458. return DM_MAPIO_REMAPPED;
  2459. }
  2460. if (discard_or_flush(bio)) {
  2461. defer_bio(cache, bio);
  2462. return DM_MAPIO_SUBMITTED;
  2463. }
  2464. /*
  2465. * Check to see if that block is currently migrating.
  2466. */
  2467. cell = alloc_prison_cell(cache);
  2468. if (!cell) {
  2469. defer_bio(cache, bio);
  2470. return DM_MAPIO_SUBMITTED;
  2471. }
  2472. r = bio_detain(cache, block, bio, cell,
  2473. (cell_free_fn) free_prison_cell,
  2474. cache, &cell);
  2475. if (r) {
  2476. if (r < 0)
  2477. defer_bio(cache, bio);
  2478. return DM_MAPIO_SUBMITTED;
  2479. }
  2480. fast_promotion = is_discarded_oblock(cache, block) || bio_writes_complete_block(cache, bio);
  2481. r = policy_map(cache->policy, block, false, can_migrate, fast_promotion,
  2482. bio, &ool.locker, &lookup_result);
  2483. if (r == -EWOULDBLOCK) {
  2484. cell_defer(cache, cell, true);
  2485. return DM_MAPIO_SUBMITTED;
  2486. } else if (r) {
  2487. DMERR_LIMIT("%s: Unexpected return from cache replacement policy: %d",
  2488. cache_device_name(cache), r);
  2489. cell_defer(cache, cell, false);
  2490. bio_io_error(bio);
  2491. return DM_MAPIO_SUBMITTED;
  2492. }
  2493. r = DM_MAPIO_REMAPPED;
  2494. switch (lookup_result.op) {
  2495. case POLICY_HIT:
  2496. if (passthrough_mode(&cache->features)) {
  2497. if (bio_data_dir(bio) == WRITE) {
  2498. /*
  2499. * We need to invalidate this block, so
  2500. * defer for the worker thread.
  2501. */
  2502. cell_defer(cache, cell, true);
  2503. r = DM_MAPIO_SUBMITTED;
  2504. } else {
  2505. inc_miss_counter(cache, bio);
  2506. remap_to_origin_clear_discard(cache, bio, block);
  2507. accounted_begin(cache, bio);
  2508. inc_ds(cache, bio, cell);
  2509. // FIXME: we want to remap hits or misses straight
  2510. // away rather than passing over to the worker.
  2511. cell_defer(cache, cell, false);
  2512. }
  2513. } else {
  2514. inc_hit_counter(cache, bio);
  2515. if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) &&
  2516. !is_dirty(cache, lookup_result.cblock)) {
  2517. remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
  2518. accounted_begin(cache, bio);
  2519. inc_ds(cache, bio, cell);
  2520. cell_defer(cache, cell, false);
  2521. } else
  2522. remap_cell_to_cache_dirty(cache, cell, block, lookup_result.cblock, false);
  2523. }
  2524. break;
  2525. case POLICY_MISS:
  2526. inc_miss_counter(cache, bio);
  2527. if (pb->req_nr != 0) {
  2528. /*
  2529. * This is a duplicate writethrough io that is no
  2530. * longer needed because the block has been demoted.
  2531. */
  2532. bio_endio(bio);
  2533. // FIXME: remap everything as a miss
  2534. cell_defer(cache, cell, false);
  2535. r = DM_MAPIO_SUBMITTED;
  2536. } else
  2537. remap_cell_to_origin_clear_discard(cache, cell, block, false);
  2538. break;
  2539. default:
  2540. DMERR_LIMIT("%s: %s: erroring bio: unknown policy op: %u",
  2541. cache_device_name(cache), __func__,
  2542. (unsigned) lookup_result.op);
  2543. cell_defer(cache, cell, false);
  2544. bio_io_error(bio);
  2545. r = DM_MAPIO_SUBMITTED;
  2546. }
  2547. return r;
  2548. }
  2549. static int cache_end_io(struct dm_target *ti, struct bio *bio, int error)
  2550. {
  2551. struct cache *cache = ti->private;
  2552. unsigned long flags;
  2553. size_t pb_data_size = get_per_bio_data_size(cache);
  2554. struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
  2555. if (pb->tick) {
  2556. policy_tick(cache->policy, false);
  2557. spin_lock_irqsave(&cache->lock, flags);
  2558. cache->need_tick_bio = true;
  2559. spin_unlock_irqrestore(&cache->lock, flags);
  2560. }
  2561. check_for_quiesced_migrations(cache, pb);
  2562. accounted_complete(cache, bio);
  2563. return 0;
  2564. }
  2565. static int write_dirty_bitset(struct cache *cache)
  2566. {
  2567. unsigned i, r;
  2568. if (get_cache_mode(cache) >= CM_READ_ONLY)
  2569. return -EINVAL;
  2570. for (i = 0; i < from_cblock(cache->cache_size); i++) {
  2571. r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
  2572. is_dirty(cache, to_cblock(i)));
  2573. if (r) {
  2574. metadata_operation_failed(cache, "dm_cache_set_dirty", r);
  2575. return r;
  2576. }
  2577. }
  2578. return 0;
  2579. }
  2580. static int write_discard_bitset(struct cache *cache)
  2581. {
  2582. unsigned i, r;
  2583. if (get_cache_mode(cache) >= CM_READ_ONLY)
  2584. return -EINVAL;
  2585. r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
  2586. cache->discard_nr_blocks);
  2587. if (r) {
  2588. DMERR("%s: could not resize on-disk discard bitset", cache_device_name(cache));
  2589. metadata_operation_failed(cache, "dm_cache_discard_bitset_resize", r);
  2590. return r;
  2591. }
  2592. for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
  2593. r = dm_cache_set_discard(cache->cmd, to_dblock(i),
  2594. is_discarded(cache, to_dblock(i)));
  2595. if (r) {
  2596. metadata_operation_failed(cache, "dm_cache_set_discard", r);
  2597. return r;
  2598. }
  2599. }
  2600. return 0;
  2601. }
  2602. static int write_hints(struct cache *cache)
  2603. {
  2604. int r;
  2605. if (get_cache_mode(cache) >= CM_READ_ONLY)
  2606. return -EINVAL;
  2607. r = dm_cache_write_hints(cache->cmd, cache->policy);
  2608. if (r) {
  2609. metadata_operation_failed(cache, "dm_cache_write_hints", r);
  2610. return r;
  2611. }
  2612. return 0;
  2613. }
  2614. /*
  2615. * returns true on success
  2616. */
  2617. static bool sync_metadata(struct cache *cache)
  2618. {
  2619. int r1, r2, r3, r4;
  2620. r1 = write_dirty_bitset(cache);
  2621. if (r1)
  2622. DMERR("%s: could not write dirty bitset", cache_device_name(cache));
  2623. r2 = write_discard_bitset(cache);
  2624. if (r2)
  2625. DMERR("%s: could not write discard bitset", cache_device_name(cache));
  2626. save_stats(cache);
  2627. r3 = write_hints(cache);
  2628. if (r3)
  2629. DMERR("%s: could not write hints", cache_device_name(cache));
  2630. /*
  2631. * If writing the above metadata failed, we still commit, but don't
  2632. * set the clean shutdown flag. This will effectively force every
  2633. * dirty bit to be set on reload.
  2634. */
  2635. r4 = commit(cache, !r1 && !r2 && !r3);
  2636. if (r4)
  2637. DMERR("%s: could not write cache metadata", cache_device_name(cache));
  2638. return !r1 && !r2 && !r3 && !r4;
  2639. }
  2640. static void cache_postsuspend(struct dm_target *ti)
  2641. {
  2642. struct cache *cache = ti->private;
  2643. start_quiescing(cache);
  2644. wait_for_migrations(cache);
  2645. stop_worker(cache);
  2646. requeue_deferred_bios(cache);
  2647. requeue_deferred_cells(cache);
  2648. stop_quiescing(cache);
  2649. if (get_cache_mode(cache) == CM_WRITE)
  2650. (void) sync_metadata(cache);
  2651. }
  2652. static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
  2653. bool dirty, uint32_t hint, bool hint_valid)
  2654. {
  2655. int r;
  2656. struct cache *cache = context;
  2657. r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
  2658. if (r)
  2659. return r;
  2660. if (dirty)
  2661. set_dirty(cache, oblock, cblock);
  2662. else
  2663. clear_dirty(cache, oblock, cblock);
  2664. return 0;
  2665. }
  2666. /*
  2667. * The discard block size in the on disk metadata is not
  2668. * neccessarily the same as we're currently using. So we have to
  2669. * be careful to only set the discarded attribute if we know it
  2670. * covers a complete block of the new size.
  2671. */
  2672. struct discard_load_info {
  2673. struct cache *cache;
  2674. /*
  2675. * These blocks are sized using the on disk dblock size, rather
  2676. * than the current one.
  2677. */
  2678. dm_block_t block_size;
  2679. dm_block_t discard_begin, discard_end;
  2680. };
  2681. static void discard_load_info_init(struct cache *cache,
  2682. struct discard_load_info *li)
  2683. {
  2684. li->cache = cache;
  2685. li->discard_begin = li->discard_end = 0;
  2686. }
  2687. static void set_discard_range(struct discard_load_info *li)
  2688. {
  2689. sector_t b, e;
  2690. if (li->discard_begin == li->discard_end)
  2691. return;
  2692. /*
  2693. * Convert to sectors.
  2694. */
  2695. b = li->discard_begin * li->block_size;
  2696. e = li->discard_end * li->block_size;
  2697. /*
  2698. * Then convert back to the current dblock size.
  2699. */
  2700. b = dm_sector_div_up(b, li->cache->discard_block_size);
  2701. sector_div(e, li->cache->discard_block_size);
  2702. /*
  2703. * The origin may have shrunk, so we need to check we're still in
  2704. * bounds.
  2705. */
  2706. if (e > from_dblock(li->cache->discard_nr_blocks))
  2707. e = from_dblock(li->cache->discard_nr_blocks);
  2708. for (; b < e; b++)
  2709. set_discard(li->cache, to_dblock(b));
  2710. }
  2711. static int load_discard(void *context, sector_t discard_block_size,
  2712. dm_dblock_t dblock, bool discard)
  2713. {
  2714. struct discard_load_info *li = context;
  2715. li->block_size = discard_block_size;
  2716. if (discard) {
  2717. if (from_dblock(dblock) == li->discard_end)
  2718. /*
  2719. * We're already in a discard range, just extend it.
  2720. */
  2721. li->discard_end = li->discard_end + 1ULL;
  2722. else {
  2723. /*
  2724. * Emit the old range and start a new one.
  2725. */
  2726. set_discard_range(li);
  2727. li->discard_begin = from_dblock(dblock);
  2728. li->discard_end = li->discard_begin + 1ULL;
  2729. }
  2730. } else {
  2731. set_discard_range(li);
  2732. li->discard_begin = li->discard_end = 0;
  2733. }
  2734. return 0;
  2735. }
  2736. static dm_cblock_t get_cache_dev_size(struct cache *cache)
  2737. {
  2738. sector_t size = get_dev_size(cache->cache_dev);
  2739. (void) sector_div(size, cache->sectors_per_block);
  2740. return to_cblock(size);
  2741. }
  2742. static bool can_resize(struct cache *cache, dm_cblock_t new_size)
  2743. {
  2744. if (from_cblock(new_size) > from_cblock(cache->cache_size))
  2745. return true;
  2746. /*
  2747. * We can't drop a dirty block when shrinking the cache.
  2748. */
  2749. while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
  2750. new_size = to_cblock(from_cblock(new_size) + 1);
  2751. if (is_dirty(cache, new_size)) {
  2752. DMERR("%s: unable to shrink cache; cache block %llu is dirty",
  2753. cache_device_name(cache),
  2754. (unsigned long long) from_cblock(new_size));
  2755. return false;
  2756. }
  2757. }
  2758. return true;
  2759. }
  2760. static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
  2761. {
  2762. int r;
  2763. r = dm_cache_resize(cache->cmd, new_size);
  2764. if (r) {
  2765. DMERR("%s: could not resize cache metadata", cache_device_name(cache));
  2766. metadata_operation_failed(cache, "dm_cache_resize", r);
  2767. return r;
  2768. }
  2769. set_cache_size(cache, new_size);
  2770. return 0;
  2771. }
  2772. static int cache_preresume(struct dm_target *ti)
  2773. {
  2774. int r = 0;
  2775. struct cache *cache = ti->private;
  2776. dm_cblock_t csize = get_cache_dev_size(cache);
  2777. /*
  2778. * Check to see if the cache has resized.
  2779. */
  2780. if (!cache->sized) {
  2781. r = resize_cache_dev(cache, csize);
  2782. if (r)
  2783. return r;
  2784. cache->sized = true;
  2785. } else if (csize != cache->cache_size) {
  2786. if (!can_resize(cache, csize))
  2787. return -EINVAL;
  2788. r = resize_cache_dev(cache, csize);
  2789. if (r)
  2790. return r;
  2791. }
  2792. if (!cache->loaded_mappings) {
  2793. r = dm_cache_load_mappings(cache->cmd, cache->policy,
  2794. load_mapping, cache);
  2795. if (r) {
  2796. DMERR("%s: could not load cache mappings", cache_device_name(cache));
  2797. metadata_operation_failed(cache, "dm_cache_load_mappings", r);
  2798. return r;
  2799. }
  2800. cache->loaded_mappings = true;
  2801. }
  2802. if (!cache->loaded_discards) {
  2803. struct discard_load_info li;
  2804. /*
  2805. * The discard bitset could have been resized, or the
  2806. * discard block size changed. To be safe we start by
  2807. * setting every dblock to not discarded.
  2808. */
  2809. clear_bitset(cache->discard_bitset, from_dblock(cache->discard_nr_blocks));
  2810. discard_load_info_init(cache, &li);
  2811. r = dm_cache_load_discards(cache->cmd, load_discard, &li);
  2812. if (r) {
  2813. DMERR("%s: could not load origin discards", cache_device_name(cache));
  2814. metadata_operation_failed(cache, "dm_cache_load_discards", r);
  2815. return r;
  2816. }
  2817. set_discard_range(&li);
  2818. cache->loaded_discards = true;
  2819. }
  2820. return r;
  2821. }
  2822. static void cache_resume(struct dm_target *ti)
  2823. {
  2824. struct cache *cache = ti->private;
  2825. cache->need_tick_bio = true;
  2826. do_waker(&cache->waker.work);
  2827. }
  2828. /*
  2829. * Status format:
  2830. *
  2831. * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
  2832. * <cache block size> <#used cache blocks>/<#total cache blocks>
  2833. * <#read hits> <#read misses> <#write hits> <#write misses>
  2834. * <#demotions> <#promotions> <#dirty>
  2835. * <#features> <features>*
  2836. * <#core args> <core args>
  2837. * <policy name> <#policy args> <policy args>* <cache metadata mode> <needs_check>
  2838. */
  2839. static void cache_status(struct dm_target *ti, status_type_t type,
  2840. unsigned status_flags, char *result, unsigned maxlen)
  2841. {
  2842. int r = 0;
  2843. unsigned i;
  2844. ssize_t sz = 0;
  2845. dm_block_t nr_free_blocks_metadata = 0;
  2846. dm_block_t nr_blocks_metadata = 0;
  2847. char buf[BDEVNAME_SIZE];
  2848. struct cache *cache = ti->private;
  2849. dm_cblock_t residency;
  2850. switch (type) {
  2851. case STATUSTYPE_INFO:
  2852. if (get_cache_mode(cache) == CM_FAIL) {
  2853. DMEMIT("Fail");
  2854. break;
  2855. }
  2856. /* Commit to ensure statistics aren't out-of-date */
  2857. if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
  2858. (void) commit(cache, false);
  2859. r = dm_cache_get_free_metadata_block_count(cache->cmd, &nr_free_blocks_metadata);
  2860. if (r) {
  2861. DMERR("%s: dm_cache_get_free_metadata_block_count returned %d",
  2862. cache_device_name(cache), r);
  2863. goto err;
  2864. }
  2865. r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
  2866. if (r) {
  2867. DMERR("%s: dm_cache_get_metadata_dev_size returned %d",
  2868. cache_device_name(cache), r);
  2869. goto err;
  2870. }
  2871. residency = policy_residency(cache->policy);
  2872. DMEMIT("%u %llu/%llu %u %llu/%llu %u %u %u %u %u %u %lu ",
  2873. (unsigned)DM_CACHE_METADATA_BLOCK_SIZE,
  2874. (unsigned long long)(nr_blocks_metadata - nr_free_blocks_metadata),
  2875. (unsigned long long)nr_blocks_metadata,
  2876. cache->sectors_per_block,
  2877. (unsigned long long) from_cblock(residency),
  2878. (unsigned long long) from_cblock(cache->cache_size),
  2879. (unsigned) atomic_read(&cache->stats.read_hit),
  2880. (unsigned) atomic_read(&cache->stats.read_miss),
  2881. (unsigned) atomic_read(&cache->stats.write_hit),
  2882. (unsigned) atomic_read(&cache->stats.write_miss),
  2883. (unsigned) atomic_read(&cache->stats.demotion),
  2884. (unsigned) atomic_read(&cache->stats.promotion),
  2885. (unsigned long) atomic_read(&cache->nr_dirty));
  2886. if (writethrough_mode(&cache->features))
  2887. DMEMIT("1 writethrough ");
  2888. else if (passthrough_mode(&cache->features))
  2889. DMEMIT("1 passthrough ");
  2890. else if (writeback_mode(&cache->features))
  2891. DMEMIT("1 writeback ");
  2892. else {
  2893. DMERR("%s: internal error: unknown io mode: %d",
  2894. cache_device_name(cache), (int) cache->features.io_mode);
  2895. goto err;
  2896. }
  2897. DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
  2898. DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
  2899. if (sz < maxlen) {
  2900. r = policy_emit_config_values(cache->policy, result, maxlen, &sz);
  2901. if (r)
  2902. DMERR("%s: policy_emit_config_values returned %d",
  2903. cache_device_name(cache), r);
  2904. }
  2905. if (get_cache_mode(cache) == CM_READ_ONLY)
  2906. DMEMIT("ro ");
  2907. else
  2908. DMEMIT("rw ");
  2909. if (dm_cache_metadata_needs_check(cache->cmd))
  2910. DMEMIT("needs_check ");
  2911. else
  2912. DMEMIT("- ");
  2913. break;
  2914. case STATUSTYPE_TABLE:
  2915. format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
  2916. DMEMIT("%s ", buf);
  2917. format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
  2918. DMEMIT("%s ", buf);
  2919. format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
  2920. DMEMIT("%s", buf);
  2921. for (i = 0; i < cache->nr_ctr_args - 1; i++)
  2922. DMEMIT(" %s", cache->ctr_args[i]);
  2923. if (cache->nr_ctr_args)
  2924. DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
  2925. }
  2926. return;
  2927. err:
  2928. DMEMIT("Error");
  2929. }
  2930. /*
  2931. * A cache block range can take two forms:
  2932. *
  2933. * i) A single cblock, eg. '3456'
  2934. * ii) A begin and end cblock with dots between, eg. 123-234
  2935. */
  2936. static int parse_cblock_range(struct cache *cache, const char *str,
  2937. struct cblock_range *result)
  2938. {
  2939. char dummy;
  2940. uint64_t b, e;
  2941. int r;
  2942. /*
  2943. * Try and parse form (ii) first.
  2944. */
  2945. r = sscanf(str, "%llu-%llu%c", &b, &e, &dummy);
  2946. if (r < 0)
  2947. return r;
  2948. if (r == 2) {
  2949. result->begin = to_cblock(b);
  2950. result->end = to_cblock(e);
  2951. return 0;
  2952. }
  2953. /*
  2954. * That didn't work, try form (i).
  2955. */
  2956. r = sscanf(str, "%llu%c", &b, &dummy);
  2957. if (r < 0)
  2958. return r;
  2959. if (r == 1) {
  2960. result->begin = to_cblock(b);
  2961. result->end = to_cblock(from_cblock(result->begin) + 1u);
  2962. return 0;
  2963. }
  2964. DMERR("%s: invalid cblock range '%s'", cache_device_name(cache), str);
  2965. return -EINVAL;
  2966. }
  2967. static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
  2968. {
  2969. uint64_t b = from_cblock(range->begin);
  2970. uint64_t e = from_cblock(range->end);
  2971. uint64_t n = from_cblock(cache->cache_size);
  2972. if (b >= n) {
  2973. DMERR("%s: begin cblock out of range: %llu >= %llu",
  2974. cache_device_name(cache), b, n);
  2975. return -EINVAL;
  2976. }
  2977. if (e > n) {
  2978. DMERR("%s: end cblock out of range: %llu > %llu",
  2979. cache_device_name(cache), e, n);
  2980. return -EINVAL;
  2981. }
  2982. if (b >= e) {
  2983. DMERR("%s: invalid cblock range: %llu >= %llu",
  2984. cache_device_name(cache), b, e);
  2985. return -EINVAL;
  2986. }
  2987. return 0;
  2988. }
  2989. static int request_invalidation(struct cache *cache, struct cblock_range *range)
  2990. {
  2991. struct invalidation_request req;
  2992. INIT_LIST_HEAD(&req.list);
  2993. req.cblocks = range;
  2994. atomic_set(&req.complete, 0);
  2995. req.err = 0;
  2996. init_waitqueue_head(&req.result_wait);
  2997. spin_lock(&cache->invalidation_lock);
  2998. list_add(&req.list, &cache->invalidation_requests);
  2999. spin_unlock(&cache->invalidation_lock);
  3000. wake_worker(cache);
  3001. wait_event(req.result_wait, atomic_read(&req.complete));
  3002. return req.err;
  3003. }
  3004. static int process_invalidate_cblocks_message(struct cache *cache, unsigned count,
  3005. const char **cblock_ranges)
  3006. {
  3007. int r = 0;
  3008. unsigned i;
  3009. struct cblock_range range;
  3010. if (!passthrough_mode(&cache->features)) {
  3011. DMERR("%s: cache has to be in passthrough mode for invalidation",
  3012. cache_device_name(cache));
  3013. return -EPERM;
  3014. }
  3015. for (i = 0; i < count; i++) {
  3016. r = parse_cblock_range(cache, cblock_ranges[i], &range);
  3017. if (r)
  3018. break;
  3019. r = validate_cblock_range(cache, &range);
  3020. if (r)
  3021. break;
  3022. /*
  3023. * Pass begin and end origin blocks to the worker and wake it.
  3024. */
  3025. r = request_invalidation(cache, &range);
  3026. if (r)
  3027. break;
  3028. }
  3029. return r;
  3030. }
  3031. /*
  3032. * Supports
  3033. * "<key> <value>"
  3034. * and
  3035. * "invalidate_cblocks [(<begin>)|(<begin>-<end>)]*
  3036. *
  3037. * The key migration_threshold is supported by the cache target core.
  3038. */
  3039. static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
  3040. {
  3041. struct cache *cache = ti->private;
  3042. if (!argc)
  3043. return -EINVAL;
  3044. if (get_cache_mode(cache) >= CM_READ_ONLY) {
  3045. DMERR("%s: unable to service cache target messages in READ_ONLY or FAIL mode",
  3046. cache_device_name(cache));
  3047. return -EOPNOTSUPP;
  3048. }
  3049. if (!strcasecmp(argv[0], "invalidate_cblocks"))
  3050. return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
  3051. if (argc != 2)
  3052. return -EINVAL;
  3053. return set_config_value(cache, argv[0], argv[1]);
  3054. }
  3055. static int cache_iterate_devices(struct dm_target *ti,
  3056. iterate_devices_callout_fn fn, void *data)
  3057. {
  3058. int r = 0;
  3059. struct cache *cache = ti->private;
  3060. r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
  3061. if (!r)
  3062. r = fn(ti, cache->origin_dev, 0, ti->len, data);
  3063. return r;
  3064. }
  3065. static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
  3066. {
  3067. /*
  3068. * FIXME: these limits may be incompatible with the cache device
  3069. */
  3070. limits->max_discard_sectors = min_t(sector_t, cache->discard_block_size * 1024,
  3071. cache->origin_sectors);
  3072. limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
  3073. }
  3074. static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
  3075. {
  3076. struct cache *cache = ti->private;
  3077. uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
  3078. /*
  3079. * If the system-determined stacked limits are compatible with the
  3080. * cache's blocksize (io_opt is a factor) do not override them.
  3081. */
  3082. if (io_opt_sectors < cache->sectors_per_block ||
  3083. do_div(io_opt_sectors, cache->sectors_per_block)) {
  3084. blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT);
  3085. blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
  3086. }
  3087. set_discard_limits(cache, limits);
  3088. }
  3089. /*----------------------------------------------------------------*/
  3090. static struct target_type cache_target = {
  3091. .name = "cache",
  3092. .version = {1, 8, 0},
  3093. .module = THIS_MODULE,
  3094. .ctr = cache_ctr,
  3095. .dtr = cache_dtr,
  3096. .map = cache_map,
  3097. .end_io = cache_end_io,
  3098. .postsuspend = cache_postsuspend,
  3099. .preresume = cache_preresume,
  3100. .resume = cache_resume,
  3101. .status = cache_status,
  3102. .message = cache_message,
  3103. .iterate_devices = cache_iterate_devices,
  3104. .io_hints = cache_io_hints,
  3105. };
  3106. static int __init dm_cache_init(void)
  3107. {
  3108. int r;
  3109. r = dm_register_target(&cache_target);
  3110. if (r) {
  3111. DMERR("cache target registration failed: %d", r);
  3112. return r;
  3113. }
  3114. migration_cache = KMEM_CACHE(dm_cache_migration, 0);
  3115. if (!migration_cache) {
  3116. dm_unregister_target(&cache_target);
  3117. return -ENOMEM;
  3118. }
  3119. return 0;
  3120. }
  3121. static void __exit dm_cache_exit(void)
  3122. {
  3123. dm_unregister_target(&cache_target);
  3124. kmem_cache_destroy(migration_cache);
  3125. }
  3126. module_init(dm_cache_init);
  3127. module_exit(dm_cache_exit);
  3128. MODULE_DESCRIPTION(DM_NAME " cache target");
  3129. MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
  3130. MODULE_LICENSE("GPL");