dm-cache-target.c 77 KB

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