dm-snap.c 28 KB

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