dm-snap.c 34 KB

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