dm-snap.c 34 KB

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