dm-cache-target.c 82 KB

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