dm-snap.c 32 KB

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