dm-cache-target.c 63 KB

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