dm-snap.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. /*
  2. * dm-snapshot.c
  3. *
  4. * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
  5. *
  6. * This file is released under the GPL.
  7. */
  8. #include <linux/blkdev.h>
  9. #include <linux/device-mapper.h>
  10. #include <linux/delay.h>
  11. #include <linux/fs.h>
  12. #include <linux/init.h>
  13. #include <linux/kdev_t.h>
  14. #include <linux/list.h>
  15. #include <linux/mempool.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/log2.h>
  20. #include <linux/dm-kcopyd.h>
  21. #include <linux/workqueue.h>
  22. #include "dm-exception-store.h"
  23. #define DM_MSG_PREFIX "snapshots"
  24. /*
  25. * The percentage increment we will wake up users at
  26. */
  27. #define WAKE_UP_PERCENT 5
  28. /*
  29. * kcopyd priority of snapshot operations
  30. */
  31. #define SNAPSHOT_COPY_PRIORITY 2
  32. /*
  33. * Reserve 1MB for each snapshot initially (with minimum of 1 page).
  34. */
  35. #define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
  36. /*
  37. * The size of the mempool used to track chunks in use.
  38. */
  39. #define MIN_IOS 256
  40. #define DM_TRACKED_CHUNK_HASH_SIZE 16
  41. #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
  42. (DM_TRACKED_CHUNK_HASH_SIZE - 1))
  43. struct exception_table {
  44. uint32_t hash_mask;
  45. unsigned hash_shift;
  46. struct list_head *table;
  47. };
  48. struct dm_snapshot {
  49. struct rw_semaphore lock;
  50. struct dm_dev *origin;
  51. /* List of snapshots per Origin */
  52. struct list_head list;
  53. /* You can't use a snapshot if this is 0 (e.g. if full) */
  54. int valid;
  55. /* Origin writes don't trigger exceptions until this is set */
  56. int active;
  57. mempool_t *pending_pool;
  58. atomic_t pending_exceptions_count;
  59. struct exception_table pending;
  60. struct exception_table complete;
  61. /*
  62. * pe_lock protects all pending_exception operations and access
  63. * as well as the snapshot_bios list.
  64. */
  65. spinlock_t pe_lock;
  66. /* The on disk metadata handler */
  67. struct dm_exception_store *store;
  68. struct dm_kcopyd_client *kcopyd_client;
  69. /* Queue of snapshot writes for ksnapd to flush */
  70. struct bio_list queued_bios;
  71. struct work_struct queued_bios_work;
  72. /* Chunks with outstanding reads */
  73. mempool_t *tracked_chunk_pool;
  74. spinlock_t tracked_chunk_lock;
  75. struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
  76. };
  77. static struct workqueue_struct *ksnapd;
  78. static void flush_queued_bios(struct work_struct *work);
  79. static sector_t chunk_to_sector(struct dm_exception_store *store,
  80. chunk_t chunk)
  81. {
  82. return chunk << store->chunk_shift;
  83. }
  84. static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
  85. {
  86. /*
  87. * There is only ever one instance of a particular block
  88. * device so we can compare pointers safely.
  89. */
  90. return lhs == rhs;
  91. }
  92. struct dm_snap_pending_exception {
  93. struct dm_snap_exception e;
  94. /*
  95. * Origin buffers waiting for this to complete are held
  96. * in a bio list
  97. */
  98. struct bio_list origin_bios;
  99. struct bio_list snapshot_bios;
  100. /*
  101. * Short-term queue of pending exceptions prior to submission.
  102. */
  103. struct list_head list;
  104. /*
  105. * The primary pending_exception is the one that holds
  106. * the ref_count and the list of origin_bios for a
  107. * group of pending_exceptions. It is always last to get freed.
  108. * These fields get set up when writing to the origin.
  109. */
  110. struct dm_snap_pending_exception *primary_pe;
  111. /*
  112. * Number of pending_exceptions processing this chunk.
  113. * When this drops to zero we must complete the origin bios.
  114. * If incrementing or decrementing this, hold pe->snap->lock for
  115. * the sibling concerned and not pe->primary_pe->snap->lock unless
  116. * they are the same.
  117. */
  118. atomic_t ref_count;
  119. /* Pointer back to snapshot context */
  120. struct dm_snapshot *snap;
  121. /*
  122. * 1 indicates the exception has already been sent to
  123. * kcopyd.
  124. */
  125. int started;
  126. };
  127. /*
  128. * Hash table mapping origin volumes to lists of snapshots and
  129. * a lock to protect it
  130. */
  131. static struct kmem_cache *exception_cache;
  132. static struct kmem_cache *pending_cache;
  133. struct dm_snap_tracked_chunk {
  134. struct hlist_node node;
  135. chunk_t chunk;
  136. };
  137. static struct kmem_cache *tracked_chunk_cache;
  138. static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
  139. chunk_t chunk)
  140. {
  141. struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
  142. GFP_NOIO);
  143. unsigned long flags;
  144. c->chunk = chunk;
  145. spin_lock_irqsave(&s->tracked_chunk_lock, flags);
  146. hlist_add_head(&c->node,
  147. &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
  148. spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
  149. return c;
  150. }
  151. static void stop_tracking_chunk(struct dm_snapshot *s,
  152. struct dm_snap_tracked_chunk *c)
  153. {
  154. unsigned long flags;
  155. spin_lock_irqsave(&s->tracked_chunk_lock, flags);
  156. hlist_del(&c->node);
  157. spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
  158. mempool_free(c, s->tracked_chunk_pool);
  159. }
  160. static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
  161. {
  162. struct dm_snap_tracked_chunk *c;
  163. struct hlist_node *hn;
  164. int found = 0;
  165. spin_lock_irq(&s->tracked_chunk_lock);
  166. hlist_for_each_entry(c, hn,
  167. &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
  168. if (c->chunk == chunk) {
  169. found = 1;
  170. break;
  171. }
  172. }
  173. spin_unlock_irq(&s->tracked_chunk_lock);
  174. return found;
  175. }
  176. /*
  177. * One of these per registered origin, held in the snapshot_origins hash
  178. */
  179. struct origin {
  180. /* The origin device */
  181. struct block_device *bdev;
  182. struct list_head hash_list;
  183. /* List of snapshots for this origin */
  184. struct list_head snapshots;
  185. };
  186. /*
  187. * Size of the hash table for origin volumes. If we make this
  188. * the size of the minors list then it should be nearly perfect
  189. */
  190. #define ORIGIN_HASH_SIZE 256
  191. #define ORIGIN_MASK 0xFF
  192. static struct list_head *_origins;
  193. static struct rw_semaphore _origins_lock;
  194. static int init_origin_hash(void)
  195. {
  196. int i;
  197. _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
  198. GFP_KERNEL);
  199. if (!_origins) {
  200. DMERR("unable to allocate memory");
  201. return -ENOMEM;
  202. }
  203. for (i = 0; i < ORIGIN_HASH_SIZE; i++)
  204. INIT_LIST_HEAD(_origins + i);
  205. init_rwsem(&_origins_lock);
  206. return 0;
  207. }
  208. static void exit_origin_hash(void)
  209. {
  210. kfree(_origins);
  211. }
  212. static unsigned origin_hash(struct block_device *bdev)
  213. {
  214. return bdev->bd_dev & ORIGIN_MASK;
  215. }
  216. static struct origin *__lookup_origin(struct block_device *origin)
  217. {
  218. struct list_head *ol;
  219. struct origin *o;
  220. ol = &_origins[origin_hash(origin)];
  221. list_for_each_entry (o, ol, hash_list)
  222. if (bdev_equal(o->bdev, origin))
  223. return o;
  224. return NULL;
  225. }
  226. static void __insert_origin(struct origin *o)
  227. {
  228. struct list_head *sl = &_origins[origin_hash(o->bdev)];
  229. list_add_tail(&o->hash_list, sl);
  230. }
  231. /*
  232. * Make a note of the snapshot and its origin so we can look it
  233. * up when the origin has a write on it.
  234. */
  235. static int register_snapshot(struct dm_snapshot *snap)
  236. {
  237. struct origin *o, *new_o;
  238. struct block_device *bdev = snap->origin->bdev;
  239. new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
  240. if (!new_o)
  241. return -ENOMEM;
  242. down_write(&_origins_lock);
  243. o = __lookup_origin(bdev);
  244. if (o)
  245. kfree(new_o);
  246. else {
  247. /* New origin */
  248. o = new_o;
  249. /* Initialise the struct */
  250. INIT_LIST_HEAD(&o->snapshots);
  251. o->bdev = bdev;
  252. __insert_origin(o);
  253. }
  254. list_add_tail(&snap->list, &o->snapshots);
  255. up_write(&_origins_lock);
  256. return 0;
  257. }
  258. static void unregister_snapshot(struct dm_snapshot *s)
  259. {
  260. struct origin *o;
  261. down_write(&_origins_lock);
  262. o = __lookup_origin(s->origin->bdev);
  263. list_del(&s->list);
  264. if (list_empty(&o->snapshots)) {
  265. list_del(&o->hash_list);
  266. kfree(o);
  267. }
  268. up_write(&_origins_lock);
  269. }
  270. /*
  271. * Implementation of the exception hash tables.
  272. * The lowest hash_shift bits of the chunk number are ignored, allowing
  273. * some consecutive chunks to be grouped together.
  274. */
  275. static int init_exception_table(struct exception_table *et, uint32_t size,
  276. unsigned hash_shift)
  277. {
  278. unsigned int i;
  279. et->hash_shift = hash_shift;
  280. et->hash_mask = size - 1;
  281. et->table = dm_vcalloc(size, sizeof(struct list_head));
  282. if (!et->table)
  283. return -ENOMEM;
  284. for (i = 0; i < size; i++)
  285. INIT_LIST_HEAD(et->table + i);
  286. return 0;
  287. }
  288. static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
  289. {
  290. struct list_head *slot;
  291. struct dm_snap_exception *ex, *next;
  292. int i, size;
  293. size = et->hash_mask + 1;
  294. for (i = 0; i < size; i++) {
  295. slot = et->table + i;
  296. list_for_each_entry_safe (ex, next, slot, hash_list)
  297. kmem_cache_free(mem, ex);
  298. }
  299. vfree(et->table);
  300. }
  301. static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
  302. {
  303. return (chunk >> et->hash_shift) & et->hash_mask;
  304. }
  305. static void insert_exception(struct exception_table *eh,
  306. struct dm_snap_exception *e)
  307. {
  308. struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
  309. list_add(&e->hash_list, l);
  310. }
  311. static void remove_exception(struct dm_snap_exception *e)
  312. {
  313. list_del(&e->hash_list);
  314. }
  315. /*
  316. * Return the exception data for a sector, or NULL if not
  317. * remapped.
  318. */
  319. static struct dm_snap_exception *lookup_exception(struct exception_table *et,
  320. chunk_t chunk)
  321. {
  322. struct list_head *slot;
  323. struct dm_snap_exception *e;
  324. slot = &et->table[exception_hash(et, chunk)];
  325. list_for_each_entry (e, slot, hash_list)
  326. if (chunk >= e->old_chunk &&
  327. chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
  328. return e;
  329. return NULL;
  330. }
  331. static struct dm_snap_exception *alloc_exception(void)
  332. {
  333. struct dm_snap_exception *e;
  334. e = kmem_cache_alloc(exception_cache, GFP_NOIO);
  335. if (!e)
  336. e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
  337. return e;
  338. }
  339. static void free_exception(struct dm_snap_exception *e)
  340. {
  341. kmem_cache_free(exception_cache, e);
  342. }
  343. static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
  344. {
  345. struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
  346. GFP_NOIO);
  347. atomic_inc(&s->pending_exceptions_count);
  348. pe->snap = s;
  349. return pe;
  350. }
  351. static void free_pending_exception(struct dm_snap_pending_exception *pe)
  352. {
  353. struct dm_snapshot *s = pe->snap;
  354. mempool_free(pe, s->pending_pool);
  355. smp_mb__before_atomic_dec();
  356. atomic_dec(&s->pending_exceptions_count);
  357. }
  358. static void insert_completed_exception(struct dm_snapshot *s,
  359. struct dm_snap_exception *new_e)
  360. {
  361. struct exception_table *eh = &s->complete;
  362. struct list_head *l;
  363. struct dm_snap_exception *e = NULL;
  364. l = &eh->table[exception_hash(eh, new_e->old_chunk)];
  365. /* Add immediately if this table doesn't support consecutive chunks */
  366. if (!eh->hash_shift)
  367. goto out;
  368. /* List is ordered by old_chunk */
  369. list_for_each_entry_reverse(e, l, hash_list) {
  370. /* Insert after an existing chunk? */
  371. if (new_e->old_chunk == (e->old_chunk +
  372. dm_consecutive_chunk_count(e) + 1) &&
  373. new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
  374. dm_consecutive_chunk_count(e) + 1)) {
  375. dm_consecutive_chunk_count_inc(e);
  376. free_exception(new_e);
  377. return;
  378. }
  379. /* Insert before an existing chunk? */
  380. if (new_e->old_chunk == (e->old_chunk - 1) &&
  381. new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
  382. dm_consecutive_chunk_count_inc(e);
  383. e->old_chunk--;
  384. e->new_chunk--;
  385. free_exception(new_e);
  386. return;
  387. }
  388. if (new_e->old_chunk > e->old_chunk)
  389. break;
  390. }
  391. out:
  392. list_add(&new_e->hash_list, e ? &e->hash_list : l);
  393. }
  394. /*
  395. * Callback used by the exception stores to load exceptions when
  396. * initialising.
  397. */
  398. static int dm_add_exception(void *context, chunk_t old, chunk_t new)
  399. {
  400. struct dm_snapshot *s = context;
  401. struct dm_snap_exception *e;
  402. e = alloc_exception();
  403. if (!e)
  404. return -ENOMEM;
  405. e->old_chunk = old;
  406. /* Consecutive_count is implicitly initialised to zero */
  407. e->new_chunk = new;
  408. insert_completed_exception(s, e);
  409. return 0;
  410. }
  411. /*
  412. * Hard coded magic.
  413. */
  414. static int calc_max_buckets(void)
  415. {
  416. /* use a fixed size of 2MB */
  417. unsigned long mem = 2 * 1024 * 1024;
  418. mem /= sizeof(struct list_head);
  419. return mem;
  420. }
  421. /*
  422. * Allocate room for a suitable hash table.
  423. */
  424. static int init_hash_tables(struct dm_snapshot *s)
  425. {
  426. sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
  427. /*
  428. * Calculate based on the size of the original volume or
  429. * the COW volume...
  430. */
  431. cow_dev_size = get_dev_size(s->store->cow->bdev);
  432. origin_dev_size = get_dev_size(s->origin->bdev);
  433. max_buckets = calc_max_buckets();
  434. hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
  435. hash_size = min(hash_size, max_buckets);
  436. hash_size = rounddown_pow_of_two(hash_size);
  437. if (init_exception_table(&s->complete, hash_size,
  438. DM_CHUNK_CONSECUTIVE_BITS))
  439. return -ENOMEM;
  440. /*
  441. * Allocate hash table for in-flight exceptions
  442. * Make this smaller than the real hash table
  443. */
  444. hash_size >>= 3;
  445. if (hash_size < 64)
  446. hash_size = 64;
  447. if (init_exception_table(&s->pending, hash_size, 0)) {
  448. exit_exception_table(&s->complete, exception_cache);
  449. return -ENOMEM;
  450. }
  451. return 0;
  452. }
  453. /*
  454. * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
  455. */
  456. static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  457. {
  458. struct dm_snapshot *s;
  459. int i;
  460. int r = -EINVAL;
  461. char *origin_path;
  462. struct dm_exception_store *store;
  463. unsigned args_used;
  464. if (argc != 4) {
  465. ti->error = "requires exactly 4 arguments";
  466. r = -EINVAL;
  467. goto bad_args;
  468. }
  469. origin_path = argv[0];
  470. argv++;
  471. argc--;
  472. r = dm_exception_store_create(ti, argc, argv, &args_used, &store);
  473. if (r) {
  474. ti->error = "Couldn't create exception store";
  475. r = -EINVAL;
  476. goto bad_args;
  477. }
  478. argv += args_used;
  479. argc -= args_used;
  480. s = kmalloc(sizeof(*s), GFP_KERNEL);
  481. if (!s) {
  482. ti->error = "Cannot allocate snapshot context private "
  483. "structure";
  484. r = -ENOMEM;
  485. goto bad_snap;
  486. }
  487. r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
  488. if (r) {
  489. ti->error = "Cannot get origin device";
  490. goto bad_origin;
  491. }
  492. s->store = store;
  493. s->valid = 1;
  494. s->active = 0;
  495. atomic_set(&s->pending_exceptions_count, 0);
  496. init_rwsem(&s->lock);
  497. spin_lock_init(&s->pe_lock);
  498. /* Allocate hash table for COW data */
  499. if (init_hash_tables(s)) {
  500. ti->error = "Unable to allocate hash table space";
  501. r = -ENOMEM;
  502. goto bad_hash_tables;
  503. }
  504. r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
  505. if (r) {
  506. ti->error = "Could not create kcopyd client";
  507. goto bad_kcopyd;
  508. }
  509. s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
  510. if (!s->pending_pool) {
  511. ti->error = "Could not allocate mempool for pending exceptions";
  512. goto bad_pending_pool;
  513. }
  514. s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
  515. tracked_chunk_cache);
  516. if (!s->tracked_chunk_pool) {
  517. ti->error = "Could not allocate tracked_chunk mempool for "
  518. "tracking reads";
  519. goto bad_tracked_chunk_pool;
  520. }
  521. for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
  522. INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
  523. spin_lock_init(&s->tracked_chunk_lock);
  524. /* Metadata must only be loaded into one table at once */
  525. r = s->store->type->read_metadata(s->store, dm_add_exception,
  526. (void *)s);
  527. if (r < 0) {
  528. ti->error = "Failed to read snapshot metadata";
  529. goto bad_load_and_register;
  530. } else if (r > 0) {
  531. s->valid = 0;
  532. DMWARN("Snapshot is marked invalid.");
  533. }
  534. bio_list_init(&s->queued_bios);
  535. INIT_WORK(&s->queued_bios_work, flush_queued_bios);
  536. /* Add snapshot to the list of snapshots for this origin */
  537. /* Exceptions aren't triggered till snapshot_resume() is called */
  538. if (register_snapshot(s)) {
  539. r = -EINVAL;
  540. ti->error = "Cannot register snapshot origin";
  541. goto bad_load_and_register;
  542. }
  543. ti->private = s;
  544. ti->split_io = s->store->chunk_size;
  545. ti->num_flush_requests = 1;
  546. return 0;
  547. bad_load_and_register:
  548. mempool_destroy(s->tracked_chunk_pool);
  549. bad_tracked_chunk_pool:
  550. mempool_destroy(s->pending_pool);
  551. bad_pending_pool:
  552. dm_kcopyd_client_destroy(s->kcopyd_client);
  553. bad_kcopyd:
  554. exit_exception_table(&s->pending, pending_cache);
  555. exit_exception_table(&s->complete, exception_cache);
  556. bad_hash_tables:
  557. dm_put_device(ti, s->origin);
  558. bad_origin:
  559. kfree(s);
  560. bad_snap:
  561. dm_exception_store_destroy(store);
  562. bad_args:
  563. return r;
  564. }
  565. static void __free_exceptions(struct dm_snapshot *s)
  566. {
  567. dm_kcopyd_client_destroy(s->kcopyd_client);
  568. s->kcopyd_client = NULL;
  569. exit_exception_table(&s->pending, pending_cache);
  570. exit_exception_table(&s->complete, exception_cache);
  571. }
  572. static void snapshot_dtr(struct dm_target *ti)
  573. {
  574. #ifdef CONFIG_DM_DEBUG
  575. int i;
  576. #endif
  577. struct dm_snapshot *s = ti->private;
  578. flush_workqueue(ksnapd);
  579. /* Prevent further origin writes from using this snapshot. */
  580. /* After this returns there can be no new kcopyd jobs. */
  581. unregister_snapshot(s);
  582. while (atomic_read(&s->pending_exceptions_count))
  583. msleep(1);
  584. /*
  585. * Ensure instructions in mempool_destroy aren't reordered
  586. * before atomic_read.
  587. */
  588. smp_mb();
  589. #ifdef CONFIG_DM_DEBUG
  590. for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
  591. BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
  592. #endif
  593. mempool_destroy(s->tracked_chunk_pool);
  594. __free_exceptions(s);
  595. mempool_destroy(s->pending_pool);
  596. dm_put_device(ti, s->origin);
  597. dm_exception_store_destroy(s->store);
  598. kfree(s);
  599. }
  600. /*
  601. * Flush a list of buffers.
  602. */
  603. static void flush_bios(struct bio *bio)
  604. {
  605. struct bio *n;
  606. while (bio) {
  607. n = bio->bi_next;
  608. bio->bi_next = NULL;
  609. generic_make_request(bio);
  610. bio = n;
  611. }
  612. }
  613. static void flush_queued_bios(struct work_struct *work)
  614. {
  615. struct dm_snapshot *s =
  616. container_of(work, struct dm_snapshot, queued_bios_work);
  617. struct bio *queued_bios;
  618. unsigned long flags;
  619. spin_lock_irqsave(&s->pe_lock, flags);
  620. queued_bios = bio_list_get(&s->queued_bios);
  621. spin_unlock_irqrestore(&s->pe_lock, flags);
  622. flush_bios(queued_bios);
  623. }
  624. /*
  625. * Error a list of buffers.
  626. */
  627. static void error_bios(struct bio *bio)
  628. {
  629. struct bio *n;
  630. while (bio) {
  631. n = bio->bi_next;
  632. bio->bi_next = NULL;
  633. bio_io_error(bio);
  634. bio = n;
  635. }
  636. }
  637. static void __invalidate_snapshot(struct dm_snapshot *s, int err)
  638. {
  639. if (!s->valid)
  640. return;
  641. if (err == -EIO)
  642. DMERR("Invalidating snapshot: Error reading/writing.");
  643. else if (err == -ENOMEM)
  644. DMERR("Invalidating snapshot: Unable to allocate exception.");
  645. if (s->store->type->drop_snapshot)
  646. s->store->type->drop_snapshot(s->store);
  647. s->valid = 0;
  648. dm_table_event(s->store->ti->table);
  649. }
  650. static void get_pending_exception(struct dm_snap_pending_exception *pe)
  651. {
  652. atomic_inc(&pe->ref_count);
  653. }
  654. static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
  655. {
  656. struct dm_snap_pending_exception *primary_pe;
  657. struct bio *origin_bios = NULL;
  658. primary_pe = pe->primary_pe;
  659. /*
  660. * If this pe is involved in a write to the origin and
  661. * it is the last sibling to complete then release
  662. * the bios for the original write to the origin.
  663. */
  664. if (primary_pe &&
  665. atomic_dec_and_test(&primary_pe->ref_count)) {
  666. origin_bios = bio_list_get(&primary_pe->origin_bios);
  667. free_pending_exception(primary_pe);
  668. }
  669. /*
  670. * Free the pe if it's not linked to an origin write or if
  671. * it's not itself a primary pe.
  672. */
  673. if (!primary_pe || primary_pe != pe)
  674. free_pending_exception(pe);
  675. return origin_bios;
  676. }
  677. static void pending_complete(struct dm_snap_pending_exception *pe, int success)
  678. {
  679. struct dm_snap_exception *e;
  680. struct dm_snapshot *s = pe->snap;
  681. struct bio *origin_bios = NULL;
  682. struct bio *snapshot_bios = NULL;
  683. int error = 0;
  684. if (!success) {
  685. /* Read/write error - snapshot is unusable */
  686. down_write(&s->lock);
  687. __invalidate_snapshot(s, -EIO);
  688. error = 1;
  689. goto out;
  690. }
  691. e = alloc_exception();
  692. if (!e) {
  693. down_write(&s->lock);
  694. __invalidate_snapshot(s, -ENOMEM);
  695. error = 1;
  696. goto out;
  697. }
  698. *e = pe->e;
  699. down_write(&s->lock);
  700. if (!s->valid) {
  701. free_exception(e);
  702. error = 1;
  703. goto out;
  704. }
  705. /*
  706. * Check for conflicting reads. This is extremely improbable,
  707. * so msleep(1) is sufficient and there is no need for a wait queue.
  708. */
  709. while (__chunk_is_tracked(s, pe->e.old_chunk))
  710. msleep(1);
  711. /*
  712. * Add a proper exception, and remove the
  713. * in-flight exception from the list.
  714. */
  715. insert_completed_exception(s, e);
  716. out:
  717. remove_exception(&pe->e);
  718. snapshot_bios = bio_list_get(&pe->snapshot_bios);
  719. origin_bios = put_pending_exception(pe);
  720. up_write(&s->lock);
  721. /* Submit any pending write bios */
  722. if (error)
  723. error_bios(snapshot_bios);
  724. else
  725. flush_bios(snapshot_bios);
  726. flush_bios(origin_bios);
  727. }
  728. static void commit_callback(void *context, int success)
  729. {
  730. struct dm_snap_pending_exception *pe = context;
  731. pending_complete(pe, success);
  732. }
  733. /*
  734. * Called when the copy I/O has finished. kcopyd actually runs
  735. * this code so don't block.
  736. */
  737. static void copy_callback(int read_err, unsigned long write_err, void *context)
  738. {
  739. struct dm_snap_pending_exception *pe = context;
  740. struct dm_snapshot *s = pe->snap;
  741. if (read_err || write_err)
  742. pending_complete(pe, 0);
  743. else
  744. /* Update the metadata if we are persistent */
  745. s->store->type->commit_exception(s->store, &pe->e,
  746. commit_callback, pe);
  747. }
  748. /*
  749. * Dispatches the copy operation to kcopyd.
  750. */
  751. static void start_copy(struct dm_snap_pending_exception *pe)
  752. {
  753. struct dm_snapshot *s = pe->snap;
  754. struct dm_io_region src, dest;
  755. struct block_device *bdev = s->origin->bdev;
  756. sector_t dev_size;
  757. dev_size = get_dev_size(bdev);
  758. src.bdev = bdev;
  759. src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
  760. src.count = min(s->store->chunk_size, dev_size - src.sector);
  761. dest.bdev = s->store->cow->bdev;
  762. dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
  763. dest.count = src.count;
  764. /* Hand over to kcopyd */
  765. dm_kcopyd_copy(s->kcopyd_client,
  766. &src, 1, &dest, 0, copy_callback, pe);
  767. }
  768. static struct dm_snap_pending_exception *
  769. __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
  770. {
  771. struct dm_snap_exception *e = lookup_exception(&s->pending, chunk);
  772. if (!e)
  773. return NULL;
  774. return container_of(e, struct dm_snap_pending_exception, e);
  775. }
  776. /*
  777. * Looks to see if this snapshot already has a pending exception
  778. * for this chunk, otherwise it allocates a new one and inserts
  779. * it into the pending table.
  780. *
  781. * NOTE: a write lock must be held on snap->lock before calling
  782. * this.
  783. */
  784. static struct dm_snap_pending_exception *
  785. __find_pending_exception(struct dm_snapshot *s,
  786. struct dm_snap_pending_exception *pe, chunk_t chunk)
  787. {
  788. struct dm_snap_pending_exception *pe2;
  789. pe2 = __lookup_pending_exception(s, chunk);
  790. if (pe2) {
  791. free_pending_exception(pe);
  792. return pe2;
  793. }
  794. pe->e.old_chunk = chunk;
  795. bio_list_init(&pe->origin_bios);
  796. bio_list_init(&pe->snapshot_bios);
  797. pe->primary_pe = NULL;
  798. atomic_set(&pe->ref_count, 0);
  799. pe->started = 0;
  800. if (s->store->type->prepare_exception(s->store, &pe->e)) {
  801. free_pending_exception(pe);
  802. return NULL;
  803. }
  804. get_pending_exception(pe);
  805. insert_exception(&s->pending, &pe->e);
  806. return pe;
  807. }
  808. static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
  809. struct bio *bio, chunk_t chunk)
  810. {
  811. bio->bi_bdev = s->store->cow->bdev;
  812. bio->bi_sector = chunk_to_sector(s->store,
  813. dm_chunk_number(e->new_chunk) +
  814. (chunk - e->old_chunk)) +
  815. (bio->bi_sector &
  816. s->store->chunk_mask);
  817. }
  818. static int snapshot_map(struct dm_target *ti, struct bio *bio,
  819. union map_info *map_context)
  820. {
  821. struct dm_snap_exception *e;
  822. struct dm_snapshot *s = ti->private;
  823. int r = DM_MAPIO_REMAPPED;
  824. chunk_t chunk;
  825. struct dm_snap_pending_exception *pe = NULL;
  826. if (unlikely(bio_empty_barrier(bio))) {
  827. bio->bi_bdev = s->store->cow->bdev;
  828. return DM_MAPIO_REMAPPED;
  829. }
  830. chunk = sector_to_chunk(s->store, bio->bi_sector);
  831. /* Full snapshots are not usable */
  832. /* To get here the table must be live so s->active is always set. */
  833. if (!s->valid)
  834. return -EIO;
  835. /* FIXME: should only take write lock if we need
  836. * to copy an exception */
  837. down_write(&s->lock);
  838. if (!s->valid) {
  839. r = -EIO;
  840. goto out_unlock;
  841. }
  842. /* If the block is already remapped - use that, else remap it */
  843. e = lookup_exception(&s->complete, chunk);
  844. if (e) {
  845. remap_exception(s, e, bio, chunk);
  846. goto out_unlock;
  847. }
  848. /*
  849. * Write to snapshot - higher level takes care of RW/RO
  850. * flags so we should only get this if we are
  851. * writeable.
  852. */
  853. if (bio_rw(bio) == WRITE) {
  854. pe = __lookup_pending_exception(s, chunk);
  855. if (!pe) {
  856. up_write(&s->lock);
  857. pe = alloc_pending_exception(s);
  858. down_write(&s->lock);
  859. if (!s->valid) {
  860. free_pending_exception(pe);
  861. r = -EIO;
  862. goto out_unlock;
  863. }
  864. e = lookup_exception(&s->complete, chunk);
  865. if (e) {
  866. free_pending_exception(pe);
  867. remap_exception(s, e, bio, chunk);
  868. goto out_unlock;
  869. }
  870. pe = __find_pending_exception(s, pe, chunk);
  871. if (!pe) {
  872. __invalidate_snapshot(s, -ENOMEM);
  873. r = -EIO;
  874. goto out_unlock;
  875. }
  876. }
  877. remap_exception(s, &pe->e, bio, chunk);
  878. bio_list_add(&pe->snapshot_bios, bio);
  879. r = DM_MAPIO_SUBMITTED;
  880. if (!pe->started) {
  881. /* this is protected by snap->lock */
  882. pe->started = 1;
  883. up_write(&s->lock);
  884. start_copy(pe);
  885. goto out;
  886. }
  887. } else {
  888. bio->bi_bdev = s->origin->bdev;
  889. map_context->ptr = track_chunk(s, chunk);
  890. }
  891. out_unlock:
  892. up_write(&s->lock);
  893. out:
  894. return r;
  895. }
  896. static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
  897. int error, union map_info *map_context)
  898. {
  899. struct dm_snapshot *s = ti->private;
  900. struct dm_snap_tracked_chunk *c = map_context->ptr;
  901. if (c)
  902. stop_tracking_chunk(s, c);
  903. return 0;
  904. }
  905. static void snapshot_resume(struct dm_target *ti)
  906. {
  907. struct dm_snapshot *s = ti->private;
  908. down_write(&s->lock);
  909. s->active = 1;
  910. up_write(&s->lock);
  911. }
  912. static int snapshot_status(struct dm_target *ti, status_type_t type,
  913. char *result, unsigned int maxlen)
  914. {
  915. unsigned sz = 0;
  916. struct dm_snapshot *snap = ti->private;
  917. switch (type) {
  918. case STATUSTYPE_INFO:
  919. if (!snap->valid)
  920. DMEMIT("Invalid");
  921. else {
  922. if (snap->store->type->fraction_full) {
  923. sector_t numerator, denominator;
  924. snap->store->type->fraction_full(snap->store,
  925. &numerator,
  926. &denominator);
  927. DMEMIT("%llu/%llu",
  928. (unsigned long long)numerator,
  929. (unsigned long long)denominator);
  930. }
  931. else
  932. DMEMIT("Unknown");
  933. }
  934. break;
  935. case STATUSTYPE_TABLE:
  936. /*
  937. * kdevname returns a static pointer so we need
  938. * to make private copies if the output is to
  939. * make sense.
  940. */
  941. DMEMIT("%s", snap->origin->name);
  942. snap->store->type->status(snap->store, type, result + sz,
  943. maxlen - sz);
  944. break;
  945. }
  946. return 0;
  947. }
  948. /*-----------------------------------------------------------------
  949. * Origin methods
  950. *---------------------------------------------------------------*/
  951. static int __origin_write(struct list_head *snapshots, struct bio *bio)
  952. {
  953. int r = DM_MAPIO_REMAPPED, first = 0;
  954. struct dm_snapshot *snap;
  955. struct dm_snap_exception *e;
  956. struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
  957. chunk_t chunk;
  958. LIST_HEAD(pe_queue);
  959. /* Do all the snapshots on this origin */
  960. list_for_each_entry (snap, snapshots, list) {
  961. down_write(&snap->lock);
  962. /* Only deal with valid and active snapshots */
  963. if (!snap->valid || !snap->active)
  964. goto next_snapshot;
  965. /* Nothing to do if writing beyond end of snapshot */
  966. if (bio->bi_sector >= dm_table_get_size(snap->store->ti->table))
  967. goto next_snapshot;
  968. /*
  969. * Remember, different snapshots can have
  970. * different chunk sizes.
  971. */
  972. chunk = sector_to_chunk(snap->store, bio->bi_sector);
  973. /*
  974. * Check exception table to see if block
  975. * is already remapped in this snapshot
  976. * and trigger an exception if not.
  977. *
  978. * ref_count is initialised to 1 so pending_complete()
  979. * won't destroy the primary_pe while we're inside this loop.
  980. */
  981. e = lookup_exception(&snap->complete, chunk);
  982. if (e)
  983. goto next_snapshot;
  984. pe = __lookup_pending_exception(snap, chunk);
  985. if (!pe) {
  986. up_write(&snap->lock);
  987. pe = alloc_pending_exception(snap);
  988. down_write(&snap->lock);
  989. if (!snap->valid) {
  990. free_pending_exception(pe);
  991. goto next_snapshot;
  992. }
  993. e = lookup_exception(&snap->complete, chunk);
  994. if (e) {
  995. free_pending_exception(pe);
  996. goto next_snapshot;
  997. }
  998. pe = __find_pending_exception(snap, pe, chunk);
  999. if (!pe) {
  1000. __invalidate_snapshot(snap, -ENOMEM);
  1001. goto next_snapshot;
  1002. }
  1003. }
  1004. if (!primary_pe) {
  1005. /*
  1006. * Either every pe here has same
  1007. * primary_pe or none has one yet.
  1008. */
  1009. if (pe->primary_pe)
  1010. primary_pe = pe->primary_pe;
  1011. else {
  1012. primary_pe = pe;
  1013. first = 1;
  1014. }
  1015. bio_list_add(&primary_pe->origin_bios, bio);
  1016. r = DM_MAPIO_SUBMITTED;
  1017. }
  1018. if (!pe->primary_pe) {
  1019. pe->primary_pe = primary_pe;
  1020. get_pending_exception(primary_pe);
  1021. }
  1022. if (!pe->started) {
  1023. pe->started = 1;
  1024. list_add_tail(&pe->list, &pe_queue);
  1025. }
  1026. next_snapshot:
  1027. up_write(&snap->lock);
  1028. }
  1029. if (!primary_pe)
  1030. return r;
  1031. /*
  1032. * If this is the first time we're processing this chunk and
  1033. * ref_count is now 1 it means all the pending exceptions
  1034. * got completed while we were in the loop above, so it falls to
  1035. * us here to remove the primary_pe and submit any origin_bios.
  1036. */
  1037. if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
  1038. flush_bios(bio_list_get(&primary_pe->origin_bios));
  1039. free_pending_exception(primary_pe);
  1040. /* If we got here, pe_queue is necessarily empty. */
  1041. return r;
  1042. }
  1043. /*
  1044. * Now that we have a complete pe list we can start the copying.
  1045. */
  1046. list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
  1047. start_copy(pe);
  1048. return r;
  1049. }
  1050. /*
  1051. * Called on a write from the origin driver.
  1052. */
  1053. static int do_origin(struct dm_dev *origin, struct bio *bio)
  1054. {
  1055. struct origin *o;
  1056. int r = DM_MAPIO_REMAPPED;
  1057. down_read(&_origins_lock);
  1058. o = __lookup_origin(origin->bdev);
  1059. if (o)
  1060. r = __origin_write(&o->snapshots, bio);
  1061. up_read(&_origins_lock);
  1062. return r;
  1063. }
  1064. /*
  1065. * Origin: maps a linear range of a device, with hooks for snapshotting.
  1066. */
  1067. /*
  1068. * Construct an origin mapping: <dev_path>
  1069. * The context for an origin is merely a 'struct dm_dev *'
  1070. * pointing to the real device.
  1071. */
  1072. static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  1073. {
  1074. int r;
  1075. struct dm_dev *dev;
  1076. if (argc != 1) {
  1077. ti->error = "origin: incorrect number of arguments";
  1078. return -EINVAL;
  1079. }
  1080. r = dm_get_device(ti, argv[0], 0, ti->len,
  1081. dm_table_get_mode(ti->table), &dev);
  1082. if (r) {
  1083. ti->error = "Cannot get target device";
  1084. return r;
  1085. }
  1086. ti->private = dev;
  1087. ti->num_flush_requests = 1;
  1088. return 0;
  1089. }
  1090. static void origin_dtr(struct dm_target *ti)
  1091. {
  1092. struct dm_dev *dev = ti->private;
  1093. dm_put_device(ti, dev);
  1094. }
  1095. static int origin_map(struct dm_target *ti, struct bio *bio,
  1096. union map_info *map_context)
  1097. {
  1098. struct dm_dev *dev = ti->private;
  1099. bio->bi_bdev = dev->bdev;
  1100. if (unlikely(bio_empty_barrier(bio)))
  1101. return DM_MAPIO_REMAPPED;
  1102. /* Only tell snapshots if this is a write */
  1103. return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
  1104. }
  1105. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  1106. /*
  1107. * Set the target "split_io" field to the minimum of all the snapshots'
  1108. * chunk sizes.
  1109. */
  1110. static void origin_resume(struct dm_target *ti)
  1111. {
  1112. struct dm_dev *dev = ti->private;
  1113. struct dm_snapshot *snap;
  1114. struct origin *o;
  1115. chunk_t chunk_size = 0;
  1116. down_read(&_origins_lock);
  1117. o = __lookup_origin(dev->bdev);
  1118. if (o)
  1119. list_for_each_entry (snap, &o->snapshots, list)
  1120. chunk_size = min_not_zero(chunk_size,
  1121. snap->store->chunk_size);
  1122. up_read(&_origins_lock);
  1123. ti->split_io = chunk_size;
  1124. }
  1125. static int origin_status(struct dm_target *ti, status_type_t type, char *result,
  1126. unsigned int maxlen)
  1127. {
  1128. struct dm_dev *dev = ti->private;
  1129. switch (type) {
  1130. case STATUSTYPE_INFO:
  1131. result[0] = '\0';
  1132. break;
  1133. case STATUSTYPE_TABLE:
  1134. snprintf(result, maxlen, "%s", dev->name);
  1135. break;
  1136. }
  1137. return 0;
  1138. }
  1139. static struct target_type origin_target = {
  1140. .name = "snapshot-origin",
  1141. .version = {1, 6, 0},
  1142. .module = THIS_MODULE,
  1143. .ctr = origin_ctr,
  1144. .dtr = origin_dtr,
  1145. .map = origin_map,
  1146. .resume = origin_resume,
  1147. .status = origin_status,
  1148. };
  1149. static struct target_type snapshot_target = {
  1150. .name = "snapshot",
  1151. .version = {1, 6, 0},
  1152. .module = THIS_MODULE,
  1153. .ctr = snapshot_ctr,
  1154. .dtr = snapshot_dtr,
  1155. .map = snapshot_map,
  1156. .end_io = snapshot_end_io,
  1157. .resume = snapshot_resume,
  1158. .status = snapshot_status,
  1159. };
  1160. static int __init dm_snapshot_init(void)
  1161. {
  1162. int r;
  1163. r = dm_exception_store_init();
  1164. if (r) {
  1165. DMERR("Failed to initialize exception stores");
  1166. return r;
  1167. }
  1168. r = dm_register_target(&snapshot_target);
  1169. if (r) {
  1170. DMERR("snapshot target register failed %d", r);
  1171. return r;
  1172. }
  1173. r = dm_register_target(&origin_target);
  1174. if (r < 0) {
  1175. DMERR("Origin target register failed %d", r);
  1176. goto bad1;
  1177. }
  1178. r = init_origin_hash();
  1179. if (r) {
  1180. DMERR("init_origin_hash failed.");
  1181. goto bad2;
  1182. }
  1183. exception_cache = KMEM_CACHE(dm_snap_exception, 0);
  1184. if (!exception_cache) {
  1185. DMERR("Couldn't create exception cache.");
  1186. r = -ENOMEM;
  1187. goto bad3;
  1188. }
  1189. pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
  1190. if (!pending_cache) {
  1191. DMERR("Couldn't create pending cache.");
  1192. r = -ENOMEM;
  1193. goto bad4;
  1194. }
  1195. tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
  1196. if (!tracked_chunk_cache) {
  1197. DMERR("Couldn't create cache to track chunks in use.");
  1198. r = -ENOMEM;
  1199. goto bad5;
  1200. }
  1201. ksnapd = create_singlethread_workqueue("ksnapd");
  1202. if (!ksnapd) {
  1203. DMERR("Failed to create ksnapd workqueue.");
  1204. r = -ENOMEM;
  1205. goto bad_pending_pool;
  1206. }
  1207. return 0;
  1208. bad_pending_pool:
  1209. kmem_cache_destroy(tracked_chunk_cache);
  1210. bad5:
  1211. kmem_cache_destroy(pending_cache);
  1212. bad4:
  1213. kmem_cache_destroy(exception_cache);
  1214. bad3:
  1215. exit_origin_hash();
  1216. bad2:
  1217. dm_unregister_target(&origin_target);
  1218. bad1:
  1219. dm_unregister_target(&snapshot_target);
  1220. return r;
  1221. }
  1222. static void __exit dm_snapshot_exit(void)
  1223. {
  1224. destroy_workqueue(ksnapd);
  1225. dm_unregister_target(&snapshot_target);
  1226. dm_unregister_target(&origin_target);
  1227. exit_origin_hash();
  1228. kmem_cache_destroy(pending_cache);
  1229. kmem_cache_destroy(exception_cache);
  1230. kmem_cache_destroy(tracked_chunk_cache);
  1231. dm_exception_store_exit();
  1232. }
  1233. /* Module hooks */
  1234. module_init(dm_snapshot_init);
  1235. module_exit(dm_snapshot_exit);
  1236. MODULE_DESCRIPTION(DM_NAME " snapshot target");
  1237. MODULE_AUTHOR("Joe Thornber");
  1238. MODULE_LICENSE("GPL");