dm-cache-target.c 94 KB

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