dm-cache-target.c 67 KB

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