dm-cache-target.c 62 KB

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