dm-cache-target.c 76 KB

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