dm-snap.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520
  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. #include "dm-bio-list.h"
  24. #define DM_MSG_PREFIX "snapshots"
  25. /*
  26. * The percentage increment we will wake up users at
  27. */
  28. #define WAKE_UP_PERCENT 5
  29. /*
  30. * kcopyd priority of snapshot operations
  31. */
  32. #define SNAPSHOT_COPY_PRIORITY 2
  33. /*
  34. * Reserve 1MB for each snapshot initially (with minimum of 1 page).
  35. */
  36. #define SNAPSHOT_PAGES (((1UL << 20) >> PAGE_SHIFT) ? : 1)
  37. /*
  38. * The size of the mempool used to track chunks in use.
  39. */
  40. #define MIN_IOS 256
  41. #define DM_TRACKED_CHUNK_HASH_SIZE 16
  42. #define DM_TRACKED_CHUNK_HASH(x) ((unsigned long)(x) & \
  43. (DM_TRACKED_CHUNK_HASH_SIZE - 1))
  44. struct exception_table {
  45. uint32_t hash_mask;
  46. unsigned hash_shift;
  47. struct list_head *table;
  48. };
  49. struct dm_snapshot {
  50. struct rw_semaphore lock;
  51. struct dm_dev *origin;
  52. /* List of snapshots per Origin */
  53. struct list_head list;
  54. /* You can't use a snapshot if this is 0 (e.g. if full) */
  55. int valid;
  56. /* Origin writes don't trigger exceptions until this is set */
  57. int active;
  58. mempool_t *pending_pool;
  59. atomic_t pending_exceptions_count;
  60. struct exception_table pending;
  61. struct exception_table complete;
  62. /*
  63. * pe_lock protects all pending_exception operations and access
  64. * as well as the snapshot_bios list.
  65. */
  66. spinlock_t pe_lock;
  67. /* The on disk metadata handler */
  68. struct dm_exception_store *store;
  69. struct dm_kcopyd_client *kcopyd_client;
  70. /* Queue of snapshot writes for ksnapd to flush */
  71. struct bio_list queued_bios;
  72. struct work_struct queued_bios_work;
  73. /* Chunks with outstanding reads */
  74. mempool_t *tracked_chunk_pool;
  75. spinlock_t tracked_chunk_lock;
  76. struct hlist_head tracked_chunk_hash[DM_TRACKED_CHUNK_HASH_SIZE];
  77. };
  78. static struct workqueue_struct *ksnapd;
  79. static void flush_queued_bios(struct work_struct *work);
  80. static sector_t chunk_to_sector(struct dm_exception_store *store,
  81. chunk_t chunk)
  82. {
  83. return chunk << store->chunk_shift;
  84. }
  85. static int bdev_equal(struct block_device *lhs, struct block_device *rhs)
  86. {
  87. /*
  88. * There is only ever one instance of a particular block
  89. * device so we can compare pointers safely.
  90. */
  91. return lhs == rhs;
  92. }
  93. struct dm_snap_pending_exception {
  94. struct dm_snap_exception e;
  95. /*
  96. * Origin buffers waiting for this to complete are held
  97. * in a bio list
  98. */
  99. struct bio_list origin_bios;
  100. struct bio_list snapshot_bios;
  101. /*
  102. * Short-term queue of pending exceptions prior to submission.
  103. */
  104. struct list_head list;
  105. /*
  106. * The primary pending_exception is the one that holds
  107. * the ref_count and the list of origin_bios for a
  108. * group of pending_exceptions. It is always last to get freed.
  109. * These fields get set up when writing to the origin.
  110. */
  111. struct dm_snap_pending_exception *primary_pe;
  112. /*
  113. * Number of pending_exceptions processing this chunk.
  114. * When this drops to zero we must complete the origin bios.
  115. * If incrementing or decrementing this, hold pe->snap->lock for
  116. * the sibling concerned and not pe->primary_pe->snap->lock unless
  117. * they are the same.
  118. */
  119. atomic_t ref_count;
  120. /* Pointer back to snapshot context */
  121. struct dm_snapshot *snap;
  122. /*
  123. * 1 indicates the exception has already been sent to
  124. * kcopyd.
  125. */
  126. int started;
  127. };
  128. /*
  129. * Hash table mapping origin volumes to lists of snapshots and
  130. * a lock to protect it
  131. */
  132. static struct kmem_cache *exception_cache;
  133. static struct kmem_cache *pending_cache;
  134. struct dm_snap_tracked_chunk {
  135. struct hlist_node node;
  136. chunk_t chunk;
  137. };
  138. static struct kmem_cache *tracked_chunk_cache;
  139. static struct dm_snap_tracked_chunk *track_chunk(struct dm_snapshot *s,
  140. chunk_t chunk)
  141. {
  142. struct dm_snap_tracked_chunk *c = mempool_alloc(s->tracked_chunk_pool,
  143. GFP_NOIO);
  144. unsigned long flags;
  145. c->chunk = chunk;
  146. spin_lock_irqsave(&s->tracked_chunk_lock, flags);
  147. hlist_add_head(&c->node,
  148. &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)]);
  149. spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
  150. return c;
  151. }
  152. static void stop_tracking_chunk(struct dm_snapshot *s,
  153. struct dm_snap_tracked_chunk *c)
  154. {
  155. unsigned long flags;
  156. spin_lock_irqsave(&s->tracked_chunk_lock, flags);
  157. hlist_del(&c->node);
  158. spin_unlock_irqrestore(&s->tracked_chunk_lock, flags);
  159. mempool_free(c, s->tracked_chunk_pool);
  160. }
  161. static int __chunk_is_tracked(struct dm_snapshot *s, chunk_t chunk)
  162. {
  163. struct dm_snap_tracked_chunk *c;
  164. struct hlist_node *hn;
  165. int found = 0;
  166. spin_lock_irq(&s->tracked_chunk_lock);
  167. hlist_for_each_entry(c, hn,
  168. &s->tracked_chunk_hash[DM_TRACKED_CHUNK_HASH(chunk)], node) {
  169. if (c->chunk == chunk) {
  170. found = 1;
  171. break;
  172. }
  173. }
  174. spin_unlock_irq(&s->tracked_chunk_lock);
  175. return found;
  176. }
  177. /*
  178. * One of these per registered origin, held in the snapshot_origins hash
  179. */
  180. struct origin {
  181. /* The origin device */
  182. struct block_device *bdev;
  183. struct list_head hash_list;
  184. /* List of snapshots for this origin */
  185. struct list_head snapshots;
  186. };
  187. /*
  188. * Size of the hash table for origin volumes. If we make this
  189. * the size of the minors list then it should be nearly perfect
  190. */
  191. #define ORIGIN_HASH_SIZE 256
  192. #define ORIGIN_MASK 0xFF
  193. static struct list_head *_origins;
  194. static struct rw_semaphore _origins_lock;
  195. static int init_origin_hash(void)
  196. {
  197. int i;
  198. _origins = kmalloc(ORIGIN_HASH_SIZE * sizeof(struct list_head),
  199. GFP_KERNEL);
  200. if (!_origins) {
  201. DMERR("unable to allocate memory");
  202. return -ENOMEM;
  203. }
  204. for (i = 0; i < ORIGIN_HASH_SIZE; i++)
  205. INIT_LIST_HEAD(_origins + i);
  206. init_rwsem(&_origins_lock);
  207. return 0;
  208. }
  209. static void exit_origin_hash(void)
  210. {
  211. kfree(_origins);
  212. }
  213. static unsigned origin_hash(struct block_device *bdev)
  214. {
  215. return bdev->bd_dev & ORIGIN_MASK;
  216. }
  217. static struct origin *__lookup_origin(struct block_device *origin)
  218. {
  219. struct list_head *ol;
  220. struct origin *o;
  221. ol = &_origins[origin_hash(origin)];
  222. list_for_each_entry (o, ol, hash_list)
  223. if (bdev_equal(o->bdev, origin))
  224. return o;
  225. return NULL;
  226. }
  227. static void __insert_origin(struct origin *o)
  228. {
  229. struct list_head *sl = &_origins[origin_hash(o->bdev)];
  230. list_add_tail(&o->hash_list, sl);
  231. }
  232. /*
  233. * Make a note of the snapshot and its origin so we can look it
  234. * up when the origin has a write on it.
  235. */
  236. static int register_snapshot(struct dm_snapshot *snap)
  237. {
  238. struct origin *o, *new_o;
  239. struct block_device *bdev = snap->origin->bdev;
  240. new_o = kmalloc(sizeof(*new_o), GFP_KERNEL);
  241. if (!new_o)
  242. return -ENOMEM;
  243. down_write(&_origins_lock);
  244. o = __lookup_origin(bdev);
  245. if (o)
  246. kfree(new_o);
  247. else {
  248. /* New origin */
  249. o = new_o;
  250. /* Initialise the struct */
  251. INIT_LIST_HEAD(&o->snapshots);
  252. o->bdev = bdev;
  253. __insert_origin(o);
  254. }
  255. list_add_tail(&snap->list, &o->snapshots);
  256. up_write(&_origins_lock);
  257. return 0;
  258. }
  259. static void unregister_snapshot(struct dm_snapshot *s)
  260. {
  261. struct origin *o;
  262. down_write(&_origins_lock);
  263. o = __lookup_origin(s->origin->bdev);
  264. list_del(&s->list);
  265. if (list_empty(&o->snapshots)) {
  266. list_del(&o->hash_list);
  267. kfree(o);
  268. }
  269. up_write(&_origins_lock);
  270. }
  271. /*
  272. * Implementation of the exception hash tables.
  273. * The lowest hash_shift bits of the chunk number are ignored, allowing
  274. * some consecutive chunks to be grouped together.
  275. */
  276. static int init_exception_table(struct exception_table *et, uint32_t size,
  277. unsigned hash_shift)
  278. {
  279. unsigned int i;
  280. et->hash_shift = hash_shift;
  281. et->hash_mask = size - 1;
  282. et->table = dm_vcalloc(size, sizeof(struct list_head));
  283. if (!et->table)
  284. return -ENOMEM;
  285. for (i = 0; i < size; i++)
  286. INIT_LIST_HEAD(et->table + i);
  287. return 0;
  288. }
  289. static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
  290. {
  291. struct list_head *slot;
  292. struct dm_snap_exception *ex, *next;
  293. int i, size;
  294. size = et->hash_mask + 1;
  295. for (i = 0; i < size; i++) {
  296. slot = et->table + i;
  297. list_for_each_entry_safe (ex, next, slot, hash_list)
  298. kmem_cache_free(mem, ex);
  299. }
  300. vfree(et->table);
  301. }
  302. static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
  303. {
  304. return (chunk >> et->hash_shift) & et->hash_mask;
  305. }
  306. static void insert_exception(struct exception_table *eh,
  307. struct dm_snap_exception *e)
  308. {
  309. struct list_head *l = &eh->table[exception_hash(eh, e->old_chunk)];
  310. list_add(&e->hash_list, l);
  311. }
  312. static void remove_exception(struct dm_snap_exception *e)
  313. {
  314. list_del(&e->hash_list);
  315. }
  316. /*
  317. * Return the exception data for a sector, or NULL if not
  318. * remapped.
  319. */
  320. static struct dm_snap_exception *lookup_exception(struct exception_table *et,
  321. chunk_t chunk)
  322. {
  323. struct list_head *slot;
  324. struct dm_snap_exception *e;
  325. slot = &et->table[exception_hash(et, chunk)];
  326. list_for_each_entry (e, slot, hash_list)
  327. if (chunk >= e->old_chunk &&
  328. chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
  329. return e;
  330. return NULL;
  331. }
  332. static struct dm_snap_exception *alloc_exception(void)
  333. {
  334. struct dm_snap_exception *e;
  335. e = kmem_cache_alloc(exception_cache, GFP_NOIO);
  336. if (!e)
  337. e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
  338. return e;
  339. }
  340. static void free_exception(struct dm_snap_exception *e)
  341. {
  342. kmem_cache_free(exception_cache, e);
  343. }
  344. static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
  345. {
  346. struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
  347. GFP_NOIO);
  348. atomic_inc(&s->pending_exceptions_count);
  349. pe->snap = s;
  350. return pe;
  351. }
  352. static void free_pending_exception(struct dm_snap_pending_exception *pe)
  353. {
  354. struct dm_snapshot *s = pe->snap;
  355. mempool_free(pe, s->pending_pool);
  356. smp_mb__before_atomic_dec();
  357. atomic_dec(&s->pending_exceptions_count);
  358. }
  359. static void insert_completed_exception(struct dm_snapshot *s,
  360. struct dm_snap_exception *new_e)
  361. {
  362. struct exception_table *eh = &s->complete;
  363. struct list_head *l;
  364. struct dm_snap_exception *e = NULL;
  365. l = &eh->table[exception_hash(eh, new_e->old_chunk)];
  366. /* Add immediately if this table doesn't support consecutive chunks */
  367. if (!eh->hash_shift)
  368. goto out;
  369. /* List is ordered by old_chunk */
  370. list_for_each_entry_reverse(e, l, hash_list) {
  371. /* Insert after an existing chunk? */
  372. if (new_e->old_chunk == (e->old_chunk +
  373. dm_consecutive_chunk_count(e) + 1) &&
  374. new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
  375. dm_consecutive_chunk_count(e) + 1)) {
  376. dm_consecutive_chunk_count_inc(e);
  377. free_exception(new_e);
  378. return;
  379. }
  380. /* Insert before an existing chunk? */
  381. if (new_e->old_chunk == (e->old_chunk - 1) &&
  382. new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
  383. dm_consecutive_chunk_count_inc(e);
  384. e->old_chunk--;
  385. e->new_chunk--;
  386. free_exception(new_e);
  387. return;
  388. }
  389. if (new_e->old_chunk > e->old_chunk)
  390. break;
  391. }
  392. out:
  393. list_add(&new_e->hash_list, e ? &e->hash_list : l);
  394. }
  395. /*
  396. * Callback used by the exception stores to load exceptions when
  397. * initialising.
  398. */
  399. static int dm_add_exception(void *context, chunk_t old, chunk_t new)
  400. {
  401. struct dm_snapshot *s = context;
  402. struct dm_snap_exception *e;
  403. e = alloc_exception();
  404. if (!e)
  405. return -ENOMEM;
  406. e->old_chunk = old;
  407. /* Consecutive_count is implicitly initialised to zero */
  408. e->new_chunk = new;
  409. insert_completed_exception(s, e);
  410. return 0;
  411. }
  412. /*
  413. * Hard coded magic.
  414. */
  415. static int calc_max_buckets(void)
  416. {
  417. /* use a fixed size of 2MB */
  418. unsigned long mem = 2 * 1024 * 1024;
  419. mem /= sizeof(struct list_head);
  420. return mem;
  421. }
  422. /*
  423. * Allocate room for a suitable hash table.
  424. */
  425. static int init_hash_tables(struct dm_snapshot *s)
  426. {
  427. sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
  428. /*
  429. * Calculate based on the size of the original volume or
  430. * the COW volume...
  431. */
  432. cow_dev_size = get_dev_size(s->store->cow->bdev);
  433. origin_dev_size = get_dev_size(s->origin->bdev);
  434. max_buckets = calc_max_buckets();
  435. hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
  436. hash_size = min(hash_size, max_buckets);
  437. hash_size = rounddown_pow_of_two(hash_size);
  438. if (init_exception_table(&s->complete, hash_size,
  439. DM_CHUNK_CONSECUTIVE_BITS))
  440. return -ENOMEM;
  441. /*
  442. * Allocate hash table for in-flight exceptions
  443. * Make this smaller than the real hash table
  444. */
  445. hash_size >>= 3;
  446. if (hash_size < 64)
  447. hash_size = 64;
  448. if (init_exception_table(&s->pending, hash_size, 0)) {
  449. exit_exception_table(&s->complete, exception_cache);
  450. return -ENOMEM;
  451. }
  452. return 0;
  453. }
  454. /*
  455. * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
  456. */
  457. static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  458. {
  459. struct dm_snapshot *s;
  460. int i;
  461. int r = -EINVAL;
  462. char *origin_path;
  463. struct dm_exception_store *store;
  464. unsigned args_used;
  465. if (argc != 4) {
  466. ti->error = "requires exactly 4 arguments";
  467. r = -EINVAL;
  468. goto bad_args;
  469. }
  470. origin_path = argv[0];
  471. argv++;
  472. argc--;
  473. r = dm_exception_store_create(ti, argc, argv, &args_used, &store);
  474. if (r) {
  475. ti->error = "Couldn't create exception store";
  476. r = -EINVAL;
  477. goto bad_args;
  478. }
  479. argv += args_used;
  480. argc -= args_used;
  481. s = kmalloc(sizeof(*s), GFP_KERNEL);
  482. if (!s) {
  483. ti->error = "Cannot allocate snapshot context private "
  484. "structure";
  485. r = -ENOMEM;
  486. goto bad_snap;
  487. }
  488. r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
  489. if (r) {
  490. ti->error = "Cannot get origin device";
  491. goto bad_origin;
  492. }
  493. s->store = store;
  494. s->valid = 1;
  495. s->active = 0;
  496. atomic_set(&s->pending_exceptions_count, 0);
  497. init_rwsem(&s->lock);
  498. spin_lock_init(&s->pe_lock);
  499. /* Allocate hash table for COW data */
  500. if (init_hash_tables(s)) {
  501. ti->error = "Unable to allocate hash table space";
  502. r = -ENOMEM;
  503. goto bad_hash_tables;
  504. }
  505. r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
  506. if (r) {
  507. ti->error = "Could not create kcopyd client";
  508. goto bad_kcopyd;
  509. }
  510. s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
  511. if (!s->pending_pool) {
  512. ti->error = "Could not allocate mempool for pending exceptions";
  513. goto bad_pending_pool;
  514. }
  515. s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
  516. tracked_chunk_cache);
  517. if (!s->tracked_chunk_pool) {
  518. ti->error = "Could not allocate tracked_chunk mempool for "
  519. "tracking reads";
  520. goto bad_tracked_chunk_pool;
  521. }
  522. for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
  523. INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
  524. spin_lock_init(&s->tracked_chunk_lock);
  525. /* Metadata must only be loaded into one table at once */
  526. r = s->store->type->read_metadata(s->store, dm_add_exception,
  527. (void *)s);
  528. if (r < 0) {
  529. ti->error = "Failed to read snapshot metadata";
  530. goto bad_load_and_register;
  531. } else if (r > 0) {
  532. s->valid = 0;
  533. DMWARN("Snapshot is marked invalid.");
  534. }
  535. bio_list_init(&s->queued_bios);
  536. INIT_WORK(&s->queued_bios_work, flush_queued_bios);
  537. /* Add snapshot to the list of snapshots for this origin */
  538. /* Exceptions aren't triggered till snapshot_resume() is called */
  539. if (register_snapshot(s)) {
  540. r = -EINVAL;
  541. ti->error = "Cannot register snapshot origin";
  542. goto bad_load_and_register;
  543. }
  544. ti->private = s;
  545. ti->split_io = s->store->chunk_size;
  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. chunk = sector_to_chunk(s->store, bio->bi_sector);
  827. /* Full snapshots are not usable */
  828. /* To get here the table must be live so s->active is always set. */
  829. if (!s->valid)
  830. return -EIO;
  831. /* FIXME: should only take write lock if we need
  832. * to copy an exception */
  833. down_write(&s->lock);
  834. if (!s->valid) {
  835. r = -EIO;
  836. goto out_unlock;
  837. }
  838. /* If the block is already remapped - use that, else remap it */
  839. e = lookup_exception(&s->complete, chunk);
  840. if (e) {
  841. remap_exception(s, e, bio, chunk);
  842. goto out_unlock;
  843. }
  844. /*
  845. * Write to snapshot - higher level takes care of RW/RO
  846. * flags so we should only get this if we are
  847. * writeable.
  848. */
  849. if (bio_rw(bio) == WRITE) {
  850. pe = __lookup_pending_exception(s, chunk);
  851. if (!pe) {
  852. up_write(&s->lock);
  853. pe = alloc_pending_exception(s);
  854. down_write(&s->lock);
  855. if (!s->valid) {
  856. free_pending_exception(pe);
  857. r = -EIO;
  858. goto out_unlock;
  859. }
  860. e = lookup_exception(&s->complete, chunk);
  861. if (e) {
  862. free_pending_exception(pe);
  863. remap_exception(s, e, bio, chunk);
  864. goto out_unlock;
  865. }
  866. pe = __find_pending_exception(s, pe, chunk);
  867. if (!pe) {
  868. __invalidate_snapshot(s, -ENOMEM);
  869. r = -EIO;
  870. goto out_unlock;
  871. }
  872. }
  873. remap_exception(s, &pe->e, bio, chunk);
  874. bio_list_add(&pe->snapshot_bios, bio);
  875. r = DM_MAPIO_SUBMITTED;
  876. if (!pe->started) {
  877. /* this is protected by snap->lock */
  878. pe->started = 1;
  879. up_write(&s->lock);
  880. start_copy(pe);
  881. goto out;
  882. }
  883. } else {
  884. bio->bi_bdev = s->origin->bdev;
  885. map_context->ptr = track_chunk(s, chunk);
  886. }
  887. out_unlock:
  888. up_write(&s->lock);
  889. out:
  890. return r;
  891. }
  892. static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
  893. int error, union map_info *map_context)
  894. {
  895. struct dm_snapshot *s = ti->private;
  896. struct dm_snap_tracked_chunk *c = map_context->ptr;
  897. if (c)
  898. stop_tracking_chunk(s, c);
  899. return 0;
  900. }
  901. static void snapshot_resume(struct dm_target *ti)
  902. {
  903. struct dm_snapshot *s = ti->private;
  904. down_write(&s->lock);
  905. s->active = 1;
  906. up_write(&s->lock);
  907. }
  908. static int snapshot_status(struct dm_target *ti, status_type_t type,
  909. char *result, unsigned int maxlen)
  910. {
  911. unsigned sz = 0;
  912. struct dm_snapshot *snap = ti->private;
  913. switch (type) {
  914. case STATUSTYPE_INFO:
  915. if (!snap->valid)
  916. DMEMIT("Invalid");
  917. else {
  918. if (snap->store->type->fraction_full) {
  919. sector_t numerator, denominator;
  920. snap->store->type->fraction_full(snap->store,
  921. &numerator,
  922. &denominator);
  923. DMEMIT("%llu/%llu",
  924. (unsigned long long)numerator,
  925. (unsigned long long)denominator);
  926. }
  927. else
  928. DMEMIT("Unknown");
  929. }
  930. break;
  931. case STATUSTYPE_TABLE:
  932. /*
  933. * kdevname returns a static pointer so we need
  934. * to make private copies if the output is to
  935. * make sense.
  936. */
  937. DMEMIT("%s", snap->origin->name);
  938. snap->store->type->status(snap->store, type, result + sz,
  939. maxlen - sz);
  940. break;
  941. }
  942. return 0;
  943. }
  944. /*-----------------------------------------------------------------
  945. * Origin methods
  946. *---------------------------------------------------------------*/
  947. static int __origin_write(struct list_head *snapshots, struct bio *bio)
  948. {
  949. int r = DM_MAPIO_REMAPPED, first = 0;
  950. struct dm_snapshot *snap;
  951. struct dm_snap_exception *e;
  952. struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
  953. chunk_t chunk;
  954. LIST_HEAD(pe_queue);
  955. /* Do all the snapshots on this origin */
  956. list_for_each_entry (snap, snapshots, list) {
  957. down_write(&snap->lock);
  958. /* Only deal with valid and active snapshots */
  959. if (!snap->valid || !snap->active)
  960. goto next_snapshot;
  961. /* Nothing to do if writing beyond end of snapshot */
  962. if (bio->bi_sector >= dm_table_get_size(snap->store->ti->table))
  963. goto next_snapshot;
  964. /*
  965. * Remember, different snapshots can have
  966. * different chunk sizes.
  967. */
  968. chunk = sector_to_chunk(snap->store, bio->bi_sector);
  969. /*
  970. * Check exception table to see if block
  971. * is already remapped in this snapshot
  972. * and trigger an exception if not.
  973. *
  974. * ref_count is initialised to 1 so pending_complete()
  975. * won't destroy the primary_pe while we're inside this loop.
  976. */
  977. e = lookup_exception(&snap->complete, chunk);
  978. if (e)
  979. goto next_snapshot;
  980. pe = __lookup_pending_exception(snap, chunk);
  981. if (!pe) {
  982. up_write(&snap->lock);
  983. pe = alloc_pending_exception(snap);
  984. down_write(&snap->lock);
  985. if (!snap->valid) {
  986. free_pending_exception(pe);
  987. goto next_snapshot;
  988. }
  989. e = lookup_exception(&snap->complete, chunk);
  990. if (e) {
  991. free_pending_exception(pe);
  992. goto next_snapshot;
  993. }
  994. pe = __find_pending_exception(snap, pe, chunk);
  995. if (!pe) {
  996. __invalidate_snapshot(snap, -ENOMEM);
  997. goto next_snapshot;
  998. }
  999. }
  1000. if (!primary_pe) {
  1001. /*
  1002. * Either every pe here has same
  1003. * primary_pe or none has one yet.
  1004. */
  1005. if (pe->primary_pe)
  1006. primary_pe = pe->primary_pe;
  1007. else {
  1008. primary_pe = pe;
  1009. first = 1;
  1010. }
  1011. bio_list_add(&primary_pe->origin_bios, bio);
  1012. r = DM_MAPIO_SUBMITTED;
  1013. }
  1014. if (!pe->primary_pe) {
  1015. pe->primary_pe = primary_pe;
  1016. get_pending_exception(primary_pe);
  1017. }
  1018. if (!pe->started) {
  1019. pe->started = 1;
  1020. list_add_tail(&pe->list, &pe_queue);
  1021. }
  1022. next_snapshot:
  1023. up_write(&snap->lock);
  1024. }
  1025. if (!primary_pe)
  1026. return r;
  1027. /*
  1028. * If this is the first time we're processing this chunk and
  1029. * ref_count is now 1 it means all the pending exceptions
  1030. * got completed while we were in the loop above, so it falls to
  1031. * us here to remove the primary_pe and submit any origin_bios.
  1032. */
  1033. if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
  1034. flush_bios(bio_list_get(&primary_pe->origin_bios));
  1035. free_pending_exception(primary_pe);
  1036. /* If we got here, pe_queue is necessarily empty. */
  1037. return r;
  1038. }
  1039. /*
  1040. * Now that we have a complete pe list we can start the copying.
  1041. */
  1042. list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
  1043. start_copy(pe);
  1044. return r;
  1045. }
  1046. /*
  1047. * Called on a write from the origin driver.
  1048. */
  1049. static int do_origin(struct dm_dev *origin, struct bio *bio)
  1050. {
  1051. struct origin *o;
  1052. int r = DM_MAPIO_REMAPPED;
  1053. down_read(&_origins_lock);
  1054. o = __lookup_origin(origin->bdev);
  1055. if (o)
  1056. r = __origin_write(&o->snapshots, bio);
  1057. up_read(&_origins_lock);
  1058. return r;
  1059. }
  1060. /*
  1061. * Origin: maps a linear range of a device, with hooks for snapshotting.
  1062. */
  1063. /*
  1064. * Construct an origin mapping: <dev_path>
  1065. * The context for an origin is merely a 'struct dm_dev *'
  1066. * pointing to the real device.
  1067. */
  1068. static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  1069. {
  1070. int r;
  1071. struct dm_dev *dev;
  1072. if (argc != 1) {
  1073. ti->error = "origin: incorrect number of arguments";
  1074. return -EINVAL;
  1075. }
  1076. r = dm_get_device(ti, argv[0], 0, ti->len,
  1077. dm_table_get_mode(ti->table), &dev);
  1078. if (r) {
  1079. ti->error = "Cannot get target device";
  1080. return r;
  1081. }
  1082. ti->private = dev;
  1083. return 0;
  1084. }
  1085. static void origin_dtr(struct dm_target *ti)
  1086. {
  1087. struct dm_dev *dev = ti->private;
  1088. dm_put_device(ti, dev);
  1089. }
  1090. static int origin_map(struct dm_target *ti, struct bio *bio,
  1091. union map_info *map_context)
  1092. {
  1093. struct dm_dev *dev = ti->private;
  1094. bio->bi_bdev = dev->bdev;
  1095. /* Only tell snapshots if this is a write */
  1096. return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
  1097. }
  1098. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  1099. /*
  1100. * Set the target "split_io" field to the minimum of all the snapshots'
  1101. * chunk sizes.
  1102. */
  1103. static void origin_resume(struct dm_target *ti)
  1104. {
  1105. struct dm_dev *dev = ti->private;
  1106. struct dm_snapshot *snap;
  1107. struct origin *o;
  1108. chunk_t chunk_size = 0;
  1109. down_read(&_origins_lock);
  1110. o = __lookup_origin(dev->bdev);
  1111. if (o)
  1112. list_for_each_entry (snap, &o->snapshots, list)
  1113. chunk_size = min_not_zero(chunk_size,
  1114. snap->store->chunk_size);
  1115. up_read(&_origins_lock);
  1116. ti->split_io = chunk_size;
  1117. }
  1118. static int origin_status(struct dm_target *ti, status_type_t type, char *result,
  1119. unsigned int maxlen)
  1120. {
  1121. struct dm_dev *dev = ti->private;
  1122. switch (type) {
  1123. case STATUSTYPE_INFO:
  1124. result[0] = '\0';
  1125. break;
  1126. case STATUSTYPE_TABLE:
  1127. snprintf(result, maxlen, "%s", dev->name);
  1128. break;
  1129. }
  1130. return 0;
  1131. }
  1132. static struct target_type origin_target = {
  1133. .name = "snapshot-origin",
  1134. .version = {1, 6, 0},
  1135. .module = THIS_MODULE,
  1136. .ctr = origin_ctr,
  1137. .dtr = origin_dtr,
  1138. .map = origin_map,
  1139. .resume = origin_resume,
  1140. .status = origin_status,
  1141. };
  1142. static struct target_type snapshot_target = {
  1143. .name = "snapshot",
  1144. .version = {1, 6, 0},
  1145. .module = THIS_MODULE,
  1146. .ctr = snapshot_ctr,
  1147. .dtr = snapshot_dtr,
  1148. .map = snapshot_map,
  1149. .end_io = snapshot_end_io,
  1150. .resume = snapshot_resume,
  1151. .status = snapshot_status,
  1152. };
  1153. static int __init dm_snapshot_init(void)
  1154. {
  1155. int r;
  1156. r = dm_exception_store_init();
  1157. if (r) {
  1158. DMERR("Failed to initialize exception stores");
  1159. return r;
  1160. }
  1161. r = dm_register_target(&snapshot_target);
  1162. if (r) {
  1163. DMERR("snapshot target register failed %d", r);
  1164. return r;
  1165. }
  1166. r = dm_register_target(&origin_target);
  1167. if (r < 0) {
  1168. DMERR("Origin target register failed %d", r);
  1169. goto bad1;
  1170. }
  1171. r = init_origin_hash();
  1172. if (r) {
  1173. DMERR("init_origin_hash failed.");
  1174. goto bad2;
  1175. }
  1176. exception_cache = KMEM_CACHE(dm_snap_exception, 0);
  1177. if (!exception_cache) {
  1178. DMERR("Couldn't create exception cache.");
  1179. r = -ENOMEM;
  1180. goto bad3;
  1181. }
  1182. pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
  1183. if (!pending_cache) {
  1184. DMERR("Couldn't create pending cache.");
  1185. r = -ENOMEM;
  1186. goto bad4;
  1187. }
  1188. tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
  1189. if (!tracked_chunk_cache) {
  1190. DMERR("Couldn't create cache to track chunks in use.");
  1191. r = -ENOMEM;
  1192. goto bad5;
  1193. }
  1194. ksnapd = create_singlethread_workqueue("ksnapd");
  1195. if (!ksnapd) {
  1196. DMERR("Failed to create ksnapd workqueue.");
  1197. r = -ENOMEM;
  1198. goto bad_pending_pool;
  1199. }
  1200. return 0;
  1201. bad_pending_pool:
  1202. kmem_cache_destroy(tracked_chunk_cache);
  1203. bad5:
  1204. kmem_cache_destroy(pending_cache);
  1205. bad4:
  1206. kmem_cache_destroy(exception_cache);
  1207. bad3:
  1208. exit_origin_hash();
  1209. bad2:
  1210. dm_unregister_target(&origin_target);
  1211. bad1:
  1212. dm_unregister_target(&snapshot_target);
  1213. return r;
  1214. }
  1215. static void __exit dm_snapshot_exit(void)
  1216. {
  1217. destroy_workqueue(ksnapd);
  1218. dm_unregister_target(&snapshot_target);
  1219. dm_unregister_target(&origin_target);
  1220. exit_origin_hash();
  1221. kmem_cache_destroy(pending_cache);
  1222. kmem_cache_destroy(exception_cache);
  1223. kmem_cache_destroy(tracked_chunk_cache);
  1224. dm_exception_store_exit();
  1225. }
  1226. /* Module hooks */
  1227. module_init(dm_snapshot_init);
  1228. module_exit(dm_snapshot_exit);
  1229. MODULE_DESCRIPTION(DM_NAME " snapshot target");
  1230. MODULE_AUTHOR("Joe Thornber");
  1231. MODULE_LICENSE("GPL");