dm-snap.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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 dm_snapshot *l;
  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. /* Sort the list according to chunk size, largest-first smallest-last */
  256. list_for_each_entry(l, &o->snapshots, list)
  257. if (l->store->chunk_size < snap->store->chunk_size)
  258. break;
  259. list_add_tail(&snap->list, &l->list);
  260. up_write(&_origins_lock);
  261. return 0;
  262. }
  263. static void unregister_snapshot(struct dm_snapshot *s)
  264. {
  265. struct origin *o;
  266. down_write(&_origins_lock);
  267. o = __lookup_origin(s->origin->bdev);
  268. list_del(&s->list);
  269. if (list_empty(&o->snapshots)) {
  270. list_del(&o->hash_list);
  271. kfree(o);
  272. }
  273. up_write(&_origins_lock);
  274. }
  275. /*
  276. * Implementation of the exception hash tables.
  277. * The lowest hash_shift bits of the chunk number are ignored, allowing
  278. * some consecutive chunks to be grouped together.
  279. */
  280. static int init_exception_table(struct exception_table *et, uint32_t size,
  281. unsigned hash_shift)
  282. {
  283. unsigned int i;
  284. et->hash_shift = hash_shift;
  285. et->hash_mask = size - 1;
  286. et->table = dm_vcalloc(size, sizeof(struct list_head));
  287. if (!et->table)
  288. return -ENOMEM;
  289. for (i = 0; i < size; i++)
  290. INIT_LIST_HEAD(et->table + i);
  291. return 0;
  292. }
  293. static void exit_exception_table(struct exception_table *et, struct kmem_cache *mem)
  294. {
  295. struct list_head *slot;
  296. struct dm_snap_exception *ex, *next;
  297. int i, size;
  298. size = et->hash_mask + 1;
  299. for (i = 0; i < size; i++) {
  300. slot = et->table + i;
  301. list_for_each_entry_safe (ex, next, slot, hash_list)
  302. kmem_cache_free(mem, ex);
  303. }
  304. vfree(et->table);
  305. }
  306. static uint32_t exception_hash(struct exception_table *et, chunk_t chunk)
  307. {
  308. return (chunk >> et->hash_shift) & et->hash_mask;
  309. }
  310. static void remove_exception(struct dm_snap_exception *e)
  311. {
  312. list_del(&e->hash_list);
  313. }
  314. /*
  315. * Return the exception data for a sector, or NULL if not
  316. * remapped.
  317. */
  318. static struct dm_snap_exception *lookup_exception(struct exception_table *et,
  319. chunk_t chunk)
  320. {
  321. struct list_head *slot;
  322. struct dm_snap_exception *e;
  323. slot = &et->table[exception_hash(et, chunk)];
  324. list_for_each_entry (e, slot, hash_list)
  325. if (chunk >= e->old_chunk &&
  326. chunk <= e->old_chunk + dm_consecutive_chunk_count(e))
  327. return e;
  328. return NULL;
  329. }
  330. static struct dm_snap_exception *alloc_exception(void)
  331. {
  332. struct dm_snap_exception *e;
  333. e = kmem_cache_alloc(exception_cache, GFP_NOIO);
  334. if (!e)
  335. e = kmem_cache_alloc(exception_cache, GFP_ATOMIC);
  336. return e;
  337. }
  338. static void free_exception(struct dm_snap_exception *e)
  339. {
  340. kmem_cache_free(exception_cache, e);
  341. }
  342. static struct dm_snap_pending_exception *alloc_pending_exception(struct dm_snapshot *s)
  343. {
  344. struct dm_snap_pending_exception *pe = mempool_alloc(s->pending_pool,
  345. GFP_NOIO);
  346. atomic_inc(&s->pending_exceptions_count);
  347. pe->snap = s;
  348. return pe;
  349. }
  350. static void free_pending_exception(struct dm_snap_pending_exception *pe)
  351. {
  352. struct dm_snapshot *s = pe->snap;
  353. mempool_free(pe, s->pending_pool);
  354. smp_mb__before_atomic_dec();
  355. atomic_dec(&s->pending_exceptions_count);
  356. }
  357. static void insert_exception(struct exception_table *eh,
  358. struct dm_snap_exception *new_e)
  359. {
  360. struct list_head *l;
  361. struct dm_snap_exception *e = NULL;
  362. l = &eh->table[exception_hash(eh, new_e->old_chunk)];
  363. /* Add immediately if this table doesn't support consecutive chunks */
  364. if (!eh->hash_shift)
  365. goto out;
  366. /* List is ordered by old_chunk */
  367. list_for_each_entry_reverse(e, l, hash_list) {
  368. /* Insert after an existing chunk? */
  369. if (new_e->old_chunk == (e->old_chunk +
  370. dm_consecutive_chunk_count(e) + 1) &&
  371. new_e->new_chunk == (dm_chunk_number(e->new_chunk) +
  372. dm_consecutive_chunk_count(e) + 1)) {
  373. dm_consecutive_chunk_count_inc(e);
  374. free_exception(new_e);
  375. return;
  376. }
  377. /* Insert before an existing chunk? */
  378. if (new_e->old_chunk == (e->old_chunk - 1) &&
  379. new_e->new_chunk == (dm_chunk_number(e->new_chunk) - 1)) {
  380. dm_consecutive_chunk_count_inc(e);
  381. e->old_chunk--;
  382. e->new_chunk--;
  383. free_exception(new_e);
  384. return;
  385. }
  386. if (new_e->old_chunk > e->old_chunk)
  387. break;
  388. }
  389. out:
  390. list_add(&new_e->hash_list, e ? &e->hash_list : l);
  391. }
  392. /*
  393. * Callback used by the exception stores to load exceptions when
  394. * initialising.
  395. */
  396. static int dm_add_exception(void *context, chunk_t old, chunk_t new)
  397. {
  398. struct dm_snapshot *s = context;
  399. struct dm_snap_exception *e;
  400. e = alloc_exception();
  401. if (!e)
  402. return -ENOMEM;
  403. e->old_chunk = old;
  404. /* Consecutive_count is implicitly initialised to zero */
  405. e->new_chunk = new;
  406. insert_exception(&s->complete, e);
  407. return 0;
  408. }
  409. #define min_not_zero(l, r) (((l) == 0) ? (r) : (((r) == 0) ? (l) : min(l, r)))
  410. /*
  411. * Return a minimum chunk size of all snapshots that have the specified origin.
  412. * Return zero if the origin has no snapshots.
  413. */
  414. static sector_t __minimum_chunk_size(struct origin *o)
  415. {
  416. struct dm_snapshot *snap;
  417. unsigned chunk_size = 0;
  418. if (o)
  419. list_for_each_entry(snap, &o->snapshots, list)
  420. chunk_size = min_not_zero(chunk_size,
  421. snap->store->chunk_size);
  422. return chunk_size;
  423. }
  424. /*
  425. * Hard coded magic.
  426. */
  427. static int calc_max_buckets(void)
  428. {
  429. /* use a fixed size of 2MB */
  430. unsigned long mem = 2 * 1024 * 1024;
  431. mem /= sizeof(struct list_head);
  432. return mem;
  433. }
  434. /*
  435. * Allocate room for a suitable hash table.
  436. */
  437. static int init_hash_tables(struct dm_snapshot *s)
  438. {
  439. sector_t hash_size, cow_dev_size, origin_dev_size, max_buckets;
  440. /*
  441. * Calculate based on the size of the original volume or
  442. * the COW volume...
  443. */
  444. cow_dev_size = get_dev_size(s->store->cow->bdev);
  445. origin_dev_size = get_dev_size(s->origin->bdev);
  446. max_buckets = calc_max_buckets();
  447. hash_size = min(origin_dev_size, cow_dev_size) >> s->store->chunk_shift;
  448. hash_size = min(hash_size, max_buckets);
  449. if (hash_size < 64)
  450. hash_size = 64;
  451. hash_size = rounddown_pow_of_two(hash_size);
  452. if (init_exception_table(&s->complete, hash_size,
  453. DM_CHUNK_CONSECUTIVE_BITS))
  454. return -ENOMEM;
  455. /*
  456. * Allocate hash table for in-flight exceptions
  457. * Make this smaller than the real hash table
  458. */
  459. hash_size >>= 3;
  460. if (hash_size < 64)
  461. hash_size = 64;
  462. if (init_exception_table(&s->pending, hash_size, 0)) {
  463. exit_exception_table(&s->complete, exception_cache);
  464. return -ENOMEM;
  465. }
  466. return 0;
  467. }
  468. /*
  469. * Construct a snapshot mapping: <origin_dev> <COW-dev> <p/n> <chunk-size>
  470. */
  471. static int snapshot_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  472. {
  473. struct dm_snapshot *s;
  474. int i;
  475. int r = -EINVAL;
  476. char *origin_path;
  477. struct dm_exception_store *store;
  478. unsigned args_used;
  479. if (argc != 4) {
  480. ti->error = "requires exactly 4 arguments";
  481. r = -EINVAL;
  482. goto bad_args;
  483. }
  484. origin_path = argv[0];
  485. argv++;
  486. argc--;
  487. r = dm_exception_store_create(ti, argc, argv, &args_used, &store);
  488. if (r) {
  489. ti->error = "Couldn't create exception store";
  490. r = -EINVAL;
  491. goto bad_args;
  492. }
  493. argv += args_used;
  494. argc -= args_used;
  495. s = kmalloc(sizeof(*s), GFP_KERNEL);
  496. if (!s) {
  497. ti->error = "Cannot allocate snapshot context private "
  498. "structure";
  499. r = -ENOMEM;
  500. goto bad_snap;
  501. }
  502. r = dm_get_device(ti, origin_path, 0, ti->len, FMODE_READ, &s->origin);
  503. if (r) {
  504. ti->error = "Cannot get origin device";
  505. goto bad_origin;
  506. }
  507. s->store = store;
  508. s->valid = 1;
  509. s->active = 0;
  510. atomic_set(&s->pending_exceptions_count, 0);
  511. init_rwsem(&s->lock);
  512. spin_lock_init(&s->pe_lock);
  513. /* Allocate hash table for COW data */
  514. if (init_hash_tables(s)) {
  515. ti->error = "Unable to allocate hash table space";
  516. r = -ENOMEM;
  517. goto bad_hash_tables;
  518. }
  519. r = dm_kcopyd_client_create(SNAPSHOT_PAGES, &s->kcopyd_client);
  520. if (r) {
  521. ti->error = "Could not create kcopyd client";
  522. goto bad_kcopyd;
  523. }
  524. s->pending_pool = mempool_create_slab_pool(MIN_IOS, pending_cache);
  525. if (!s->pending_pool) {
  526. ti->error = "Could not allocate mempool for pending exceptions";
  527. goto bad_pending_pool;
  528. }
  529. s->tracked_chunk_pool = mempool_create_slab_pool(MIN_IOS,
  530. tracked_chunk_cache);
  531. if (!s->tracked_chunk_pool) {
  532. ti->error = "Could not allocate tracked_chunk mempool for "
  533. "tracking reads";
  534. goto bad_tracked_chunk_pool;
  535. }
  536. for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
  537. INIT_HLIST_HEAD(&s->tracked_chunk_hash[i]);
  538. spin_lock_init(&s->tracked_chunk_lock);
  539. /* Metadata must only be loaded into one table at once */
  540. r = s->store->type->read_metadata(s->store, dm_add_exception,
  541. (void *)s);
  542. if (r < 0) {
  543. ti->error = "Failed to read snapshot metadata";
  544. goto bad_load_and_register;
  545. } else if (r > 0) {
  546. s->valid = 0;
  547. DMWARN("Snapshot is marked invalid.");
  548. }
  549. bio_list_init(&s->queued_bios);
  550. INIT_WORK(&s->queued_bios_work, flush_queued_bios);
  551. if (!s->store->chunk_size) {
  552. ti->error = "Chunk size not set";
  553. goto bad_load_and_register;
  554. }
  555. /* Add snapshot to the list of snapshots for this origin */
  556. /* Exceptions aren't triggered till snapshot_resume() is called */
  557. if (register_snapshot(s)) {
  558. r = -EINVAL;
  559. ti->error = "Cannot register snapshot origin";
  560. goto bad_load_and_register;
  561. }
  562. ti->private = s;
  563. ti->split_io = s->store->chunk_size;
  564. ti->num_flush_requests = 1;
  565. return 0;
  566. bad_load_and_register:
  567. mempool_destroy(s->tracked_chunk_pool);
  568. bad_tracked_chunk_pool:
  569. mempool_destroy(s->pending_pool);
  570. bad_pending_pool:
  571. dm_kcopyd_client_destroy(s->kcopyd_client);
  572. bad_kcopyd:
  573. exit_exception_table(&s->pending, pending_cache);
  574. exit_exception_table(&s->complete, exception_cache);
  575. bad_hash_tables:
  576. dm_put_device(ti, s->origin);
  577. bad_origin:
  578. kfree(s);
  579. bad_snap:
  580. dm_exception_store_destroy(store);
  581. bad_args:
  582. return r;
  583. }
  584. static void __free_exceptions(struct dm_snapshot *s)
  585. {
  586. dm_kcopyd_client_destroy(s->kcopyd_client);
  587. s->kcopyd_client = NULL;
  588. exit_exception_table(&s->pending, pending_cache);
  589. exit_exception_table(&s->complete, exception_cache);
  590. }
  591. static void snapshot_dtr(struct dm_target *ti)
  592. {
  593. #ifdef CONFIG_DM_DEBUG
  594. int i;
  595. #endif
  596. struct dm_snapshot *s = ti->private;
  597. flush_workqueue(ksnapd);
  598. /* Prevent further origin writes from using this snapshot. */
  599. /* After this returns there can be no new kcopyd jobs. */
  600. unregister_snapshot(s);
  601. while (atomic_read(&s->pending_exceptions_count))
  602. msleep(1);
  603. /*
  604. * Ensure instructions in mempool_destroy aren't reordered
  605. * before atomic_read.
  606. */
  607. smp_mb();
  608. #ifdef CONFIG_DM_DEBUG
  609. for (i = 0; i < DM_TRACKED_CHUNK_HASH_SIZE; i++)
  610. BUG_ON(!hlist_empty(&s->tracked_chunk_hash[i]));
  611. #endif
  612. mempool_destroy(s->tracked_chunk_pool);
  613. __free_exceptions(s);
  614. mempool_destroy(s->pending_pool);
  615. dm_put_device(ti, s->origin);
  616. dm_exception_store_destroy(s->store);
  617. kfree(s);
  618. }
  619. /*
  620. * Flush a list of buffers.
  621. */
  622. static void flush_bios(struct bio *bio)
  623. {
  624. struct bio *n;
  625. while (bio) {
  626. n = bio->bi_next;
  627. bio->bi_next = NULL;
  628. generic_make_request(bio);
  629. bio = n;
  630. }
  631. }
  632. static void flush_queued_bios(struct work_struct *work)
  633. {
  634. struct dm_snapshot *s =
  635. container_of(work, struct dm_snapshot, queued_bios_work);
  636. struct bio *queued_bios;
  637. unsigned long flags;
  638. spin_lock_irqsave(&s->pe_lock, flags);
  639. queued_bios = bio_list_get(&s->queued_bios);
  640. spin_unlock_irqrestore(&s->pe_lock, flags);
  641. flush_bios(queued_bios);
  642. }
  643. /*
  644. * Error a list of buffers.
  645. */
  646. static void error_bios(struct bio *bio)
  647. {
  648. struct bio *n;
  649. while (bio) {
  650. n = bio->bi_next;
  651. bio->bi_next = NULL;
  652. bio_io_error(bio);
  653. bio = n;
  654. }
  655. }
  656. static void __invalidate_snapshot(struct dm_snapshot *s, int err)
  657. {
  658. if (!s->valid)
  659. return;
  660. if (err == -EIO)
  661. DMERR("Invalidating snapshot: Error reading/writing.");
  662. else if (err == -ENOMEM)
  663. DMERR("Invalidating snapshot: Unable to allocate exception.");
  664. if (s->store->type->drop_snapshot)
  665. s->store->type->drop_snapshot(s->store);
  666. s->valid = 0;
  667. dm_table_event(s->store->ti->table);
  668. }
  669. static void get_pending_exception(struct dm_snap_pending_exception *pe)
  670. {
  671. atomic_inc(&pe->ref_count);
  672. }
  673. static struct bio *put_pending_exception(struct dm_snap_pending_exception *pe)
  674. {
  675. struct dm_snap_pending_exception *primary_pe;
  676. struct bio *origin_bios = NULL;
  677. primary_pe = pe->primary_pe;
  678. /*
  679. * If this pe is involved in a write to the origin and
  680. * it is the last sibling to complete then release
  681. * the bios for the original write to the origin.
  682. */
  683. if (primary_pe &&
  684. atomic_dec_and_test(&primary_pe->ref_count)) {
  685. origin_bios = bio_list_get(&primary_pe->origin_bios);
  686. free_pending_exception(primary_pe);
  687. }
  688. /*
  689. * Free the pe if it's not linked to an origin write or if
  690. * it's not itself a primary pe.
  691. */
  692. if (!primary_pe || primary_pe != pe)
  693. free_pending_exception(pe);
  694. return origin_bios;
  695. }
  696. static void pending_complete(struct dm_snap_pending_exception *pe, int success)
  697. {
  698. struct dm_snap_exception *e;
  699. struct dm_snapshot *s = pe->snap;
  700. struct bio *origin_bios = NULL;
  701. struct bio *snapshot_bios = NULL;
  702. int error = 0;
  703. if (!success) {
  704. /* Read/write error - snapshot is unusable */
  705. down_write(&s->lock);
  706. __invalidate_snapshot(s, -EIO);
  707. error = 1;
  708. goto out;
  709. }
  710. e = alloc_exception();
  711. if (!e) {
  712. down_write(&s->lock);
  713. __invalidate_snapshot(s, -ENOMEM);
  714. error = 1;
  715. goto out;
  716. }
  717. *e = pe->e;
  718. down_write(&s->lock);
  719. if (!s->valid) {
  720. free_exception(e);
  721. error = 1;
  722. goto out;
  723. }
  724. /*
  725. * Check for conflicting reads. This is extremely improbable,
  726. * so msleep(1) is sufficient and there is no need for a wait queue.
  727. */
  728. while (__chunk_is_tracked(s, pe->e.old_chunk))
  729. msleep(1);
  730. /*
  731. * Add a proper exception, and remove the
  732. * in-flight exception from the list.
  733. */
  734. insert_exception(&s->complete, e);
  735. out:
  736. remove_exception(&pe->e);
  737. snapshot_bios = bio_list_get(&pe->snapshot_bios);
  738. origin_bios = put_pending_exception(pe);
  739. up_write(&s->lock);
  740. /* Submit any pending write bios */
  741. if (error)
  742. error_bios(snapshot_bios);
  743. else
  744. flush_bios(snapshot_bios);
  745. flush_bios(origin_bios);
  746. }
  747. static void commit_callback(void *context, int success)
  748. {
  749. struct dm_snap_pending_exception *pe = context;
  750. pending_complete(pe, success);
  751. }
  752. /*
  753. * Called when the copy I/O has finished. kcopyd actually runs
  754. * this code so don't block.
  755. */
  756. static void copy_callback(int read_err, unsigned long write_err, void *context)
  757. {
  758. struct dm_snap_pending_exception *pe = context;
  759. struct dm_snapshot *s = pe->snap;
  760. if (read_err || write_err)
  761. pending_complete(pe, 0);
  762. else
  763. /* Update the metadata if we are persistent */
  764. s->store->type->commit_exception(s->store, &pe->e,
  765. commit_callback, pe);
  766. }
  767. /*
  768. * Dispatches the copy operation to kcopyd.
  769. */
  770. static void start_copy(struct dm_snap_pending_exception *pe)
  771. {
  772. struct dm_snapshot *s = pe->snap;
  773. struct dm_io_region src, dest;
  774. struct block_device *bdev = s->origin->bdev;
  775. sector_t dev_size;
  776. dev_size = get_dev_size(bdev);
  777. src.bdev = bdev;
  778. src.sector = chunk_to_sector(s->store, pe->e.old_chunk);
  779. src.count = min((sector_t)s->store->chunk_size, dev_size - src.sector);
  780. dest.bdev = s->store->cow->bdev;
  781. dest.sector = chunk_to_sector(s->store, pe->e.new_chunk);
  782. dest.count = src.count;
  783. /* Hand over to kcopyd */
  784. dm_kcopyd_copy(s->kcopyd_client,
  785. &src, 1, &dest, 0, copy_callback, pe);
  786. }
  787. static struct dm_snap_pending_exception *
  788. __lookup_pending_exception(struct dm_snapshot *s, chunk_t chunk)
  789. {
  790. struct dm_snap_exception *e = lookup_exception(&s->pending, chunk);
  791. if (!e)
  792. return NULL;
  793. return container_of(e, struct dm_snap_pending_exception, e);
  794. }
  795. /*
  796. * Looks to see if this snapshot already has a pending exception
  797. * for this chunk, otherwise it allocates a new one and inserts
  798. * it into the pending table.
  799. *
  800. * NOTE: a write lock must be held on snap->lock before calling
  801. * this.
  802. */
  803. static struct dm_snap_pending_exception *
  804. __find_pending_exception(struct dm_snapshot *s,
  805. struct dm_snap_pending_exception *pe, chunk_t chunk)
  806. {
  807. struct dm_snap_pending_exception *pe2;
  808. pe2 = __lookup_pending_exception(s, chunk);
  809. if (pe2) {
  810. free_pending_exception(pe);
  811. return pe2;
  812. }
  813. pe->e.old_chunk = chunk;
  814. bio_list_init(&pe->origin_bios);
  815. bio_list_init(&pe->snapshot_bios);
  816. pe->primary_pe = NULL;
  817. atomic_set(&pe->ref_count, 0);
  818. pe->started = 0;
  819. if (s->store->type->prepare_exception(s->store, &pe->e)) {
  820. free_pending_exception(pe);
  821. return NULL;
  822. }
  823. get_pending_exception(pe);
  824. insert_exception(&s->pending, &pe->e);
  825. return pe;
  826. }
  827. static void remap_exception(struct dm_snapshot *s, struct dm_snap_exception *e,
  828. struct bio *bio, chunk_t chunk)
  829. {
  830. bio->bi_bdev = s->store->cow->bdev;
  831. bio->bi_sector = chunk_to_sector(s->store,
  832. dm_chunk_number(e->new_chunk) +
  833. (chunk - e->old_chunk)) +
  834. (bio->bi_sector &
  835. s->store->chunk_mask);
  836. }
  837. static int snapshot_map(struct dm_target *ti, struct bio *bio,
  838. union map_info *map_context)
  839. {
  840. struct dm_snap_exception *e;
  841. struct dm_snapshot *s = ti->private;
  842. int r = DM_MAPIO_REMAPPED;
  843. chunk_t chunk;
  844. struct dm_snap_pending_exception *pe = NULL;
  845. if (unlikely(bio_empty_barrier(bio))) {
  846. bio->bi_bdev = s->store->cow->bdev;
  847. return DM_MAPIO_REMAPPED;
  848. }
  849. chunk = sector_to_chunk(s->store, bio->bi_sector);
  850. /* Full snapshots are not usable */
  851. /* To get here the table must be live so s->active is always set. */
  852. if (!s->valid)
  853. return -EIO;
  854. /* FIXME: should only take write lock if we need
  855. * to copy an exception */
  856. down_write(&s->lock);
  857. if (!s->valid) {
  858. r = -EIO;
  859. goto out_unlock;
  860. }
  861. /* If the block is already remapped - use that, else remap it */
  862. e = lookup_exception(&s->complete, chunk);
  863. if (e) {
  864. remap_exception(s, e, bio, chunk);
  865. goto out_unlock;
  866. }
  867. /*
  868. * Write to snapshot - higher level takes care of RW/RO
  869. * flags so we should only get this if we are
  870. * writeable.
  871. */
  872. if (bio_rw(bio) == WRITE) {
  873. pe = __lookup_pending_exception(s, chunk);
  874. if (!pe) {
  875. up_write(&s->lock);
  876. pe = alloc_pending_exception(s);
  877. down_write(&s->lock);
  878. if (!s->valid) {
  879. free_pending_exception(pe);
  880. r = -EIO;
  881. goto out_unlock;
  882. }
  883. e = lookup_exception(&s->complete, chunk);
  884. if (e) {
  885. free_pending_exception(pe);
  886. remap_exception(s, e, bio, chunk);
  887. goto out_unlock;
  888. }
  889. pe = __find_pending_exception(s, pe, chunk);
  890. if (!pe) {
  891. __invalidate_snapshot(s, -ENOMEM);
  892. r = -EIO;
  893. goto out_unlock;
  894. }
  895. }
  896. remap_exception(s, &pe->e, bio, chunk);
  897. bio_list_add(&pe->snapshot_bios, bio);
  898. r = DM_MAPIO_SUBMITTED;
  899. if (!pe->started) {
  900. /* this is protected by snap->lock */
  901. pe->started = 1;
  902. up_write(&s->lock);
  903. start_copy(pe);
  904. goto out;
  905. }
  906. } else {
  907. bio->bi_bdev = s->origin->bdev;
  908. map_context->ptr = track_chunk(s, chunk);
  909. }
  910. out_unlock:
  911. up_write(&s->lock);
  912. out:
  913. return r;
  914. }
  915. static int snapshot_end_io(struct dm_target *ti, struct bio *bio,
  916. int error, union map_info *map_context)
  917. {
  918. struct dm_snapshot *s = ti->private;
  919. struct dm_snap_tracked_chunk *c = map_context->ptr;
  920. if (c)
  921. stop_tracking_chunk(s, c);
  922. return 0;
  923. }
  924. static void snapshot_resume(struct dm_target *ti)
  925. {
  926. struct dm_snapshot *s = ti->private;
  927. down_write(&s->lock);
  928. s->active = 1;
  929. up_write(&s->lock);
  930. }
  931. static int snapshot_status(struct dm_target *ti, status_type_t type,
  932. char *result, unsigned int maxlen)
  933. {
  934. unsigned sz = 0;
  935. struct dm_snapshot *snap = ti->private;
  936. switch (type) {
  937. case STATUSTYPE_INFO:
  938. down_write(&snap->lock);
  939. if (!snap->valid)
  940. DMEMIT("Invalid");
  941. else {
  942. if (snap->store->type->fraction_full) {
  943. sector_t numerator, denominator;
  944. snap->store->type->fraction_full(snap->store,
  945. &numerator,
  946. &denominator);
  947. DMEMIT("%llu/%llu",
  948. (unsigned long long)numerator,
  949. (unsigned long long)denominator);
  950. }
  951. else
  952. DMEMIT("Unknown");
  953. }
  954. up_write(&snap->lock);
  955. break;
  956. case STATUSTYPE_TABLE:
  957. /*
  958. * kdevname returns a static pointer so we need
  959. * to make private copies if the output is to
  960. * make sense.
  961. */
  962. DMEMIT("%s", snap->origin->name);
  963. snap->store->type->status(snap->store, type, result + sz,
  964. maxlen - sz);
  965. break;
  966. }
  967. return 0;
  968. }
  969. static int snapshot_iterate_devices(struct dm_target *ti,
  970. iterate_devices_callout_fn fn, void *data)
  971. {
  972. struct dm_snapshot *snap = ti->private;
  973. return fn(ti, snap->origin, 0, ti->len, data);
  974. }
  975. /*-----------------------------------------------------------------
  976. * Origin methods
  977. *---------------------------------------------------------------*/
  978. static int __origin_write(struct list_head *snapshots, struct bio *bio)
  979. {
  980. int r = DM_MAPIO_REMAPPED, first = 0;
  981. struct dm_snapshot *snap;
  982. struct dm_snap_exception *e;
  983. struct dm_snap_pending_exception *pe, *next_pe, *primary_pe = NULL;
  984. chunk_t chunk;
  985. LIST_HEAD(pe_queue);
  986. /* Do all the snapshots on this origin */
  987. list_for_each_entry (snap, snapshots, list) {
  988. down_write(&snap->lock);
  989. /* Only deal with valid and active snapshots */
  990. if (!snap->valid || !snap->active)
  991. goto next_snapshot;
  992. /* Nothing to do if writing beyond end of snapshot */
  993. if (bio->bi_sector >= dm_table_get_size(snap->store->ti->table))
  994. goto next_snapshot;
  995. /*
  996. * Remember, different snapshots can have
  997. * different chunk sizes.
  998. */
  999. chunk = sector_to_chunk(snap->store, bio->bi_sector);
  1000. /*
  1001. * Check exception table to see if block
  1002. * is already remapped in this snapshot
  1003. * and trigger an exception if not.
  1004. *
  1005. * ref_count is initialised to 1 so pending_complete()
  1006. * won't destroy the primary_pe while we're inside this loop.
  1007. */
  1008. e = lookup_exception(&snap->complete, chunk);
  1009. if (e)
  1010. goto next_snapshot;
  1011. pe = __lookup_pending_exception(snap, chunk);
  1012. if (!pe) {
  1013. up_write(&snap->lock);
  1014. pe = alloc_pending_exception(snap);
  1015. down_write(&snap->lock);
  1016. if (!snap->valid) {
  1017. free_pending_exception(pe);
  1018. goto next_snapshot;
  1019. }
  1020. e = lookup_exception(&snap->complete, chunk);
  1021. if (e) {
  1022. free_pending_exception(pe);
  1023. goto next_snapshot;
  1024. }
  1025. pe = __find_pending_exception(snap, pe, chunk);
  1026. if (!pe) {
  1027. __invalidate_snapshot(snap, -ENOMEM);
  1028. goto next_snapshot;
  1029. }
  1030. }
  1031. if (!primary_pe) {
  1032. /*
  1033. * Either every pe here has same
  1034. * primary_pe or none has one yet.
  1035. */
  1036. if (pe->primary_pe)
  1037. primary_pe = pe->primary_pe;
  1038. else {
  1039. primary_pe = pe;
  1040. first = 1;
  1041. }
  1042. bio_list_add(&primary_pe->origin_bios, bio);
  1043. r = DM_MAPIO_SUBMITTED;
  1044. }
  1045. if (!pe->primary_pe) {
  1046. pe->primary_pe = primary_pe;
  1047. get_pending_exception(primary_pe);
  1048. }
  1049. if (!pe->started) {
  1050. pe->started = 1;
  1051. list_add_tail(&pe->list, &pe_queue);
  1052. }
  1053. next_snapshot:
  1054. up_write(&snap->lock);
  1055. }
  1056. if (!primary_pe)
  1057. return r;
  1058. /*
  1059. * If this is the first time we're processing this chunk and
  1060. * ref_count is now 1 it means all the pending exceptions
  1061. * got completed while we were in the loop above, so it falls to
  1062. * us here to remove the primary_pe and submit any origin_bios.
  1063. */
  1064. if (first && atomic_dec_and_test(&primary_pe->ref_count)) {
  1065. flush_bios(bio_list_get(&primary_pe->origin_bios));
  1066. free_pending_exception(primary_pe);
  1067. /* If we got here, pe_queue is necessarily empty. */
  1068. return r;
  1069. }
  1070. /*
  1071. * Now that we have a complete pe list we can start the copying.
  1072. */
  1073. list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
  1074. start_copy(pe);
  1075. return r;
  1076. }
  1077. /*
  1078. * Called on a write from the origin driver.
  1079. */
  1080. static int do_origin(struct dm_dev *origin, struct bio *bio)
  1081. {
  1082. struct origin *o;
  1083. int r = DM_MAPIO_REMAPPED;
  1084. down_read(&_origins_lock);
  1085. o = __lookup_origin(origin->bdev);
  1086. if (o)
  1087. r = __origin_write(&o->snapshots, bio);
  1088. up_read(&_origins_lock);
  1089. return r;
  1090. }
  1091. /*
  1092. * Origin: maps a linear range of a device, with hooks for snapshotting.
  1093. */
  1094. /*
  1095. * Construct an origin mapping: <dev_path>
  1096. * The context for an origin is merely a 'struct dm_dev *'
  1097. * pointing to the real device.
  1098. */
  1099. static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  1100. {
  1101. int r;
  1102. struct dm_dev *dev;
  1103. if (argc != 1) {
  1104. ti->error = "origin: incorrect number of arguments";
  1105. return -EINVAL;
  1106. }
  1107. r = dm_get_device(ti, argv[0], 0, ti->len,
  1108. dm_table_get_mode(ti->table), &dev);
  1109. if (r) {
  1110. ti->error = "Cannot get target device";
  1111. return r;
  1112. }
  1113. ti->private = dev;
  1114. ti->num_flush_requests = 1;
  1115. return 0;
  1116. }
  1117. static void origin_dtr(struct dm_target *ti)
  1118. {
  1119. struct dm_dev *dev = ti->private;
  1120. dm_put_device(ti, dev);
  1121. }
  1122. static int origin_map(struct dm_target *ti, struct bio *bio,
  1123. union map_info *map_context)
  1124. {
  1125. struct dm_dev *dev = ti->private;
  1126. bio->bi_bdev = dev->bdev;
  1127. if (unlikely(bio_empty_barrier(bio)))
  1128. return DM_MAPIO_REMAPPED;
  1129. /* Only tell snapshots if this is a write */
  1130. return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : DM_MAPIO_REMAPPED;
  1131. }
  1132. /*
  1133. * Set the target "split_io" field to the minimum of all the snapshots'
  1134. * chunk sizes.
  1135. */
  1136. static void origin_resume(struct dm_target *ti)
  1137. {
  1138. struct dm_dev *dev = ti->private;
  1139. down_read(&_origins_lock);
  1140. ti->split_io = __minimum_chunk_size(__lookup_origin(dev->bdev));
  1141. up_read(&_origins_lock);
  1142. }
  1143. static int origin_status(struct dm_target *ti, status_type_t type, char *result,
  1144. unsigned int maxlen)
  1145. {
  1146. struct dm_dev *dev = ti->private;
  1147. switch (type) {
  1148. case STATUSTYPE_INFO:
  1149. result[0] = '\0';
  1150. break;
  1151. case STATUSTYPE_TABLE:
  1152. snprintf(result, maxlen, "%s", dev->name);
  1153. break;
  1154. }
  1155. return 0;
  1156. }
  1157. static int origin_iterate_devices(struct dm_target *ti,
  1158. iterate_devices_callout_fn fn, void *data)
  1159. {
  1160. struct dm_dev *dev = ti->private;
  1161. return fn(ti, dev, 0, ti->len, data);
  1162. }
  1163. static struct target_type origin_target = {
  1164. .name = "snapshot-origin",
  1165. .version = {1, 7, 0},
  1166. .module = THIS_MODULE,
  1167. .ctr = origin_ctr,
  1168. .dtr = origin_dtr,
  1169. .map = origin_map,
  1170. .resume = origin_resume,
  1171. .status = origin_status,
  1172. .iterate_devices = origin_iterate_devices,
  1173. };
  1174. static struct target_type snapshot_target = {
  1175. .name = "snapshot",
  1176. .version = {1, 7, 0},
  1177. .module = THIS_MODULE,
  1178. .ctr = snapshot_ctr,
  1179. .dtr = snapshot_dtr,
  1180. .map = snapshot_map,
  1181. .end_io = snapshot_end_io,
  1182. .resume = snapshot_resume,
  1183. .status = snapshot_status,
  1184. .iterate_devices = snapshot_iterate_devices,
  1185. };
  1186. static int __init dm_snapshot_init(void)
  1187. {
  1188. int r;
  1189. r = dm_exception_store_init();
  1190. if (r) {
  1191. DMERR("Failed to initialize exception stores");
  1192. return r;
  1193. }
  1194. r = dm_register_target(&snapshot_target);
  1195. if (r) {
  1196. DMERR("snapshot target register failed %d", r);
  1197. goto bad_register_snapshot_target;
  1198. }
  1199. r = dm_register_target(&origin_target);
  1200. if (r < 0) {
  1201. DMERR("Origin target register failed %d", r);
  1202. goto bad1;
  1203. }
  1204. r = init_origin_hash();
  1205. if (r) {
  1206. DMERR("init_origin_hash failed.");
  1207. goto bad2;
  1208. }
  1209. exception_cache = KMEM_CACHE(dm_snap_exception, 0);
  1210. if (!exception_cache) {
  1211. DMERR("Couldn't create exception cache.");
  1212. r = -ENOMEM;
  1213. goto bad3;
  1214. }
  1215. pending_cache = KMEM_CACHE(dm_snap_pending_exception, 0);
  1216. if (!pending_cache) {
  1217. DMERR("Couldn't create pending cache.");
  1218. r = -ENOMEM;
  1219. goto bad4;
  1220. }
  1221. tracked_chunk_cache = KMEM_CACHE(dm_snap_tracked_chunk, 0);
  1222. if (!tracked_chunk_cache) {
  1223. DMERR("Couldn't create cache to track chunks in use.");
  1224. r = -ENOMEM;
  1225. goto bad5;
  1226. }
  1227. ksnapd = create_singlethread_workqueue("ksnapd");
  1228. if (!ksnapd) {
  1229. DMERR("Failed to create ksnapd workqueue.");
  1230. r = -ENOMEM;
  1231. goto bad_pending_pool;
  1232. }
  1233. return 0;
  1234. bad_pending_pool:
  1235. kmem_cache_destroy(tracked_chunk_cache);
  1236. bad5:
  1237. kmem_cache_destroy(pending_cache);
  1238. bad4:
  1239. kmem_cache_destroy(exception_cache);
  1240. bad3:
  1241. exit_origin_hash();
  1242. bad2:
  1243. dm_unregister_target(&origin_target);
  1244. bad1:
  1245. dm_unregister_target(&snapshot_target);
  1246. bad_register_snapshot_target:
  1247. dm_exception_store_exit();
  1248. return r;
  1249. }
  1250. static void __exit dm_snapshot_exit(void)
  1251. {
  1252. destroy_workqueue(ksnapd);
  1253. dm_unregister_target(&snapshot_target);
  1254. dm_unregister_target(&origin_target);
  1255. exit_origin_hash();
  1256. kmem_cache_destroy(pending_cache);
  1257. kmem_cache_destroy(exception_cache);
  1258. kmem_cache_destroy(tracked_chunk_cache);
  1259. dm_exception_store_exit();
  1260. }
  1261. /* Module hooks */
  1262. module_init(dm_snapshot_init);
  1263. module_exit(dm_snapshot_exit);
  1264. MODULE_DESCRIPTION(DM_NAME " snapshot target");
  1265. MODULE_AUTHOR("Joe Thornber");
  1266. MODULE_LICENSE("GPL");