dm-cache-target.c 83 KB

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