dm-cache-target.c 76 KB

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