dm-cache-target.c 95 KB

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