dm-cache-target.c 84 KB

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