dm-snap.c 29 KB

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