dm-cache-target.c 77 KB

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