dm-cache-target.c 85 KB

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