dm-snap.c 28 KB

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