dm-cache-target.c 65 KB

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