dm-cache-target.c 75 KB

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