dm-cache-target.c 65 KB

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