dm-snap.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  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. struct workqueue_struct *ksnapd;
  36. static void flush_queued_bios(void *data);
  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 sibling_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 sibling_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 kmem_cache_t *exception_cache;
  77. static kmem_cache_t *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, kmem_cache_t *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, s);
  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 snapshot_dtr(struct dm_target *ti)
  459. {
  460. struct dm_snapshot *s = (struct dm_snapshot *) ti->private;
  461. flush_workqueue(ksnapd);
  462. /* Prevent further origin writes from using this snapshot. */
  463. /* After this returns there can be no new kcopyd jobs. */
  464. unregister_snapshot(s);
  465. kcopyd_client_destroy(s->kcopyd_client);
  466. exit_exception_table(&s->pending, pending_cache);
  467. exit_exception_table(&s->complete, exception_cache);
  468. /* Deallocate memory used */
  469. s->store.destroy(&s->store);
  470. dm_put_device(ti, s->origin);
  471. dm_put_device(ti, s->cow);
  472. kfree(s);
  473. }
  474. /*
  475. * Flush a list of buffers.
  476. */
  477. static void flush_bios(struct bio *bio)
  478. {
  479. struct bio *n;
  480. while (bio) {
  481. n = bio->bi_next;
  482. bio->bi_next = NULL;
  483. generic_make_request(bio);
  484. bio = n;
  485. }
  486. }
  487. static void flush_queued_bios(void *data)
  488. {
  489. struct dm_snapshot *s = (struct dm_snapshot *) data;
  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, bio->bi_size);
  507. bio = n;
  508. }
  509. }
  510. static void __invalidate_snapshot(struct dm_snapshot *s,
  511. struct pending_exception *pe, int err)
  512. {
  513. if (!s->valid)
  514. return;
  515. if (err == -EIO)
  516. DMERR("Invalidating snapshot: Error reading/writing.");
  517. else if (err == -ENOMEM)
  518. DMERR("Invalidating snapshot: Unable to allocate exception.");
  519. if (pe)
  520. remove_exception(&pe->e);
  521. if (s->store.drop_snapshot)
  522. s->store.drop_snapshot(&s->store);
  523. s->valid = 0;
  524. dm_table_event(s->table);
  525. }
  526. static void pending_complete(struct pending_exception *pe, int success)
  527. {
  528. struct exception *e;
  529. struct pending_exception *primary_pe;
  530. struct dm_snapshot *s = pe->snap;
  531. struct bio *origin_bios = NULL;
  532. struct bio *snapshot_bios = NULL;
  533. int error = 0;
  534. if (!success) {
  535. /* Read/write error - snapshot is unusable */
  536. down_write(&s->lock);
  537. __invalidate_snapshot(s, pe, -EIO);
  538. error = 1;
  539. goto out;
  540. }
  541. e = alloc_exception();
  542. if (!e) {
  543. down_write(&s->lock);
  544. __invalidate_snapshot(s, pe, -ENOMEM);
  545. error = 1;
  546. goto out;
  547. }
  548. *e = pe->e;
  549. down_write(&s->lock);
  550. if (!s->valid) {
  551. free_exception(e);
  552. error = 1;
  553. goto out;
  554. }
  555. /*
  556. * Add a proper exception, and remove the
  557. * in-flight exception from the list.
  558. */
  559. insert_exception(&s->complete, e);
  560. remove_exception(&pe->e);
  561. out:
  562. snapshot_bios = bio_list_get(&pe->snapshot_bios);
  563. primary_pe = pe->primary_pe;
  564. /*
  565. * If this pe is involved in a write to the origin and
  566. * it is the last sibling to complete then release
  567. * the bios for the original write to the origin.
  568. */
  569. if (primary_pe &&
  570. atomic_dec_and_test(&primary_pe->sibling_count))
  571. origin_bios = bio_list_get(&primary_pe->origin_bios);
  572. /*
  573. * Free the pe if it's not linked to an origin write or if
  574. * it's not itself a primary pe.
  575. */
  576. if (!primary_pe || primary_pe != pe)
  577. free_pending_exception(pe);
  578. /*
  579. * Free the primary pe if nothing references it.
  580. */
  581. if (primary_pe && !atomic_read(&primary_pe->sibling_count))
  582. free_pending_exception(primary_pe);
  583. up_write(&s->lock);
  584. /* Submit any pending write bios */
  585. if (error)
  586. error_bios(snapshot_bios);
  587. else
  588. flush_bios(snapshot_bios);
  589. flush_bios(origin_bios);
  590. }
  591. static void commit_callback(void *context, int success)
  592. {
  593. struct pending_exception *pe = (struct pending_exception *) context;
  594. pending_complete(pe, success);
  595. }
  596. /*
  597. * Called when the copy I/O has finished. kcopyd actually runs
  598. * this code so don't block.
  599. */
  600. static void copy_callback(int read_err, unsigned int write_err, void *context)
  601. {
  602. struct pending_exception *pe = (struct pending_exception *) context;
  603. struct dm_snapshot *s = pe->snap;
  604. if (read_err || write_err)
  605. pending_complete(pe, 0);
  606. else
  607. /* Update the metadata if we are persistent */
  608. s->store.commit_exception(&s->store, &pe->e, commit_callback,
  609. pe);
  610. }
  611. /*
  612. * Dispatches the copy operation to kcopyd.
  613. */
  614. static void start_copy(struct pending_exception *pe)
  615. {
  616. struct dm_snapshot *s = pe->snap;
  617. struct io_region src, dest;
  618. struct block_device *bdev = s->origin->bdev;
  619. sector_t dev_size;
  620. dev_size = get_dev_size(bdev);
  621. src.bdev = bdev;
  622. src.sector = chunk_to_sector(s, pe->e.old_chunk);
  623. src.count = min(s->chunk_size, dev_size - src.sector);
  624. dest.bdev = s->cow->bdev;
  625. dest.sector = chunk_to_sector(s, pe->e.new_chunk);
  626. dest.count = src.count;
  627. /* Hand over to kcopyd */
  628. kcopyd_copy(s->kcopyd_client,
  629. &src, 1, &dest, 0, copy_callback, pe);
  630. }
  631. /*
  632. * Looks to see if this snapshot already has a pending exception
  633. * for this chunk, otherwise it allocates a new one and inserts
  634. * it into the pending table.
  635. *
  636. * NOTE: a write lock must be held on snap->lock before calling
  637. * this.
  638. */
  639. static struct pending_exception *
  640. __find_pending_exception(struct dm_snapshot *s, struct bio *bio)
  641. {
  642. struct exception *e;
  643. struct pending_exception *pe;
  644. chunk_t chunk = sector_to_chunk(s, bio->bi_sector);
  645. /*
  646. * Is there a pending exception for this already ?
  647. */
  648. e = lookup_exception(&s->pending, chunk);
  649. if (e) {
  650. /* cast the exception to a pending exception */
  651. pe = container_of(e, struct pending_exception, e);
  652. goto out;
  653. }
  654. /*
  655. * Create a new pending exception, we don't want
  656. * to hold the lock while we do this.
  657. */
  658. up_write(&s->lock);
  659. pe = alloc_pending_exception();
  660. down_write(&s->lock);
  661. if (!s->valid) {
  662. free_pending_exception(pe);
  663. return NULL;
  664. }
  665. e = lookup_exception(&s->pending, chunk);
  666. if (e) {
  667. free_pending_exception(pe);
  668. pe = container_of(e, struct pending_exception, e);
  669. goto out;
  670. }
  671. pe->e.old_chunk = chunk;
  672. bio_list_init(&pe->origin_bios);
  673. bio_list_init(&pe->snapshot_bios);
  674. pe->primary_pe = NULL;
  675. atomic_set(&pe->sibling_count, 1);
  676. pe->snap = s;
  677. pe->started = 0;
  678. if (s->store.prepare_exception(&s->store, &pe->e)) {
  679. free_pending_exception(pe);
  680. return NULL;
  681. }
  682. insert_exception(&s->pending, &pe->e);
  683. out:
  684. return pe;
  685. }
  686. static inline void remap_exception(struct dm_snapshot *s, struct exception *e,
  687. struct bio *bio)
  688. {
  689. bio->bi_bdev = s->cow->bdev;
  690. bio->bi_sector = chunk_to_sector(s, e->new_chunk) +
  691. (bio->bi_sector & s->chunk_mask);
  692. }
  693. static int snapshot_map(struct dm_target *ti, struct bio *bio,
  694. union map_info *map_context)
  695. {
  696. struct exception *e;
  697. struct dm_snapshot *s = (struct dm_snapshot *) ti->private;
  698. int r = 1;
  699. chunk_t chunk;
  700. struct pending_exception *pe = NULL;
  701. chunk = sector_to_chunk(s, bio->bi_sector);
  702. /* Full snapshots are not usable */
  703. /* To get here the table must be live so s->active is always set. */
  704. if (!s->valid)
  705. return -EIO;
  706. if (unlikely(bio_barrier(bio)))
  707. return -EOPNOTSUPP;
  708. /* FIXME: should only take write lock if we need
  709. * to copy an exception */
  710. down_write(&s->lock);
  711. if (!s->valid) {
  712. r = -EIO;
  713. goto out_unlock;
  714. }
  715. /* If the block is already remapped - use that, else remap it */
  716. e = lookup_exception(&s->complete, chunk);
  717. if (e) {
  718. remap_exception(s, e, bio);
  719. goto out_unlock;
  720. }
  721. /*
  722. * Write to snapshot - higher level takes care of RW/RO
  723. * flags so we should only get this if we are
  724. * writeable.
  725. */
  726. if (bio_rw(bio) == WRITE) {
  727. pe = __find_pending_exception(s, bio);
  728. if (!pe) {
  729. __invalidate_snapshot(s, pe, -ENOMEM);
  730. r = -EIO;
  731. goto out_unlock;
  732. }
  733. remap_exception(s, &pe->e, bio);
  734. bio_list_add(&pe->snapshot_bios, bio);
  735. r = 0;
  736. if (!pe->started) {
  737. /* this is protected by snap->lock */
  738. pe->started = 1;
  739. up_write(&s->lock);
  740. start_copy(pe);
  741. goto out;
  742. }
  743. } else
  744. /*
  745. * FIXME: this read path scares me because we
  746. * always use the origin when we have a pending
  747. * exception. However I can't think of a
  748. * situation where this is wrong - ejt.
  749. */
  750. bio->bi_bdev = s->origin->bdev;
  751. out_unlock:
  752. up_write(&s->lock);
  753. out:
  754. return r;
  755. }
  756. static void snapshot_resume(struct dm_target *ti)
  757. {
  758. struct dm_snapshot *s = (struct dm_snapshot *) ti->private;
  759. down_write(&s->lock);
  760. s->active = 1;
  761. up_write(&s->lock);
  762. }
  763. static int snapshot_status(struct dm_target *ti, status_type_t type,
  764. char *result, unsigned int maxlen)
  765. {
  766. struct dm_snapshot *snap = (struct dm_snapshot *) ti->private;
  767. switch (type) {
  768. case STATUSTYPE_INFO:
  769. if (!snap->valid)
  770. snprintf(result, maxlen, "Invalid");
  771. else {
  772. if (snap->store.fraction_full) {
  773. sector_t numerator, denominator;
  774. snap->store.fraction_full(&snap->store,
  775. &numerator,
  776. &denominator);
  777. snprintf(result, maxlen, "%llu/%llu",
  778. (unsigned long long)numerator,
  779. (unsigned long long)denominator);
  780. }
  781. else
  782. snprintf(result, maxlen, "Unknown");
  783. }
  784. break;
  785. case STATUSTYPE_TABLE:
  786. /*
  787. * kdevname returns a static pointer so we need
  788. * to make private copies if the output is to
  789. * make sense.
  790. */
  791. snprintf(result, maxlen, "%s %s %c %llu",
  792. snap->origin->name, snap->cow->name,
  793. snap->type,
  794. (unsigned long long)snap->chunk_size);
  795. break;
  796. }
  797. return 0;
  798. }
  799. /*-----------------------------------------------------------------
  800. * Origin methods
  801. *---------------------------------------------------------------*/
  802. static int __origin_write(struct list_head *snapshots, struct bio *bio)
  803. {
  804. int r = 1, first = 0;
  805. struct dm_snapshot *snap;
  806. struct exception *e;
  807. struct pending_exception *pe, *next_pe, *primary_pe = NULL;
  808. chunk_t chunk;
  809. LIST_HEAD(pe_queue);
  810. /* Do all the snapshots on this origin */
  811. list_for_each_entry (snap, snapshots, list) {
  812. down_write(&snap->lock);
  813. /* Only deal with valid and active snapshots */
  814. if (!snap->valid || !snap->active)
  815. goto next_snapshot;
  816. /* Nothing to do if writing beyond end of snapshot */
  817. if (bio->bi_sector >= dm_table_get_size(snap->table))
  818. goto next_snapshot;
  819. /*
  820. * Remember, different snapshots can have
  821. * different chunk sizes.
  822. */
  823. chunk = sector_to_chunk(snap, bio->bi_sector);
  824. /*
  825. * Check exception table to see if block
  826. * is already remapped in this snapshot
  827. * and trigger an exception if not.
  828. *
  829. * sibling_count is initialised to 1 so pending_complete()
  830. * won't destroy the primary_pe while we're inside this loop.
  831. */
  832. e = lookup_exception(&snap->complete, chunk);
  833. if (e)
  834. goto next_snapshot;
  835. pe = __find_pending_exception(snap, bio);
  836. if (!pe) {
  837. __invalidate_snapshot(snap, pe, -ENOMEM);
  838. goto next_snapshot;
  839. }
  840. if (!primary_pe) {
  841. /*
  842. * Either every pe here has same
  843. * primary_pe or none has one yet.
  844. */
  845. if (pe->primary_pe)
  846. primary_pe = pe->primary_pe;
  847. else {
  848. primary_pe = pe;
  849. first = 1;
  850. }
  851. bio_list_add(&primary_pe->origin_bios, bio);
  852. r = 0;
  853. }
  854. if (!pe->primary_pe) {
  855. atomic_inc(&primary_pe->sibling_count);
  856. pe->primary_pe = primary_pe;
  857. }
  858. if (!pe->started) {
  859. pe->started = 1;
  860. list_add_tail(&pe->list, &pe_queue);
  861. }
  862. next_snapshot:
  863. up_write(&snap->lock);
  864. }
  865. if (!primary_pe)
  866. goto out;
  867. /*
  868. * If this is the first time we're processing this chunk and
  869. * sibling_count is now 1 it means all the pending exceptions
  870. * got completed while we were in the loop above, so it falls to
  871. * us here to remove the primary_pe and submit any origin_bios.
  872. */
  873. if (first && atomic_dec_and_test(&primary_pe->sibling_count)) {
  874. flush_bios(bio_list_get(&primary_pe->origin_bios));
  875. free_pending_exception(primary_pe);
  876. /* If we got here, pe_queue is necessarily empty. */
  877. goto out;
  878. }
  879. /*
  880. * Now that we have a complete pe list we can start the copying.
  881. */
  882. list_for_each_entry_safe(pe, next_pe, &pe_queue, list)
  883. start_copy(pe);
  884. out:
  885. return r;
  886. }
  887. /*
  888. * Called on a write from the origin driver.
  889. */
  890. static int do_origin(struct dm_dev *origin, struct bio *bio)
  891. {
  892. struct origin *o;
  893. int r = 1;
  894. down_read(&_origins_lock);
  895. o = __lookup_origin(origin->bdev);
  896. if (o)
  897. r = __origin_write(&o->snapshots, bio);
  898. up_read(&_origins_lock);
  899. return r;
  900. }
  901. /*
  902. * Origin: maps a linear range of a device, with hooks for snapshotting.
  903. */
  904. /*
  905. * Construct an origin mapping: <dev_path>
  906. * The context for an origin is merely a 'struct dm_dev *'
  907. * pointing to the real device.
  908. */
  909. static int origin_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  910. {
  911. int r;
  912. struct dm_dev *dev;
  913. if (argc != 1) {
  914. ti->error = "origin: incorrect number of arguments";
  915. return -EINVAL;
  916. }
  917. r = dm_get_device(ti, argv[0], 0, ti->len,
  918. dm_table_get_mode(ti->table), &dev);
  919. if (r) {
  920. ti->error = "Cannot get target device";
  921. return r;
  922. }
  923. ti->private = dev;
  924. return 0;
  925. }
  926. static void origin_dtr(struct dm_target *ti)
  927. {
  928. struct dm_dev *dev = (struct dm_dev *) ti->private;
  929. dm_put_device(ti, dev);
  930. }
  931. static int origin_map(struct dm_target *ti, struct bio *bio,
  932. union map_info *map_context)
  933. {
  934. struct dm_dev *dev = (struct dm_dev *) ti->private;
  935. bio->bi_bdev = dev->bdev;
  936. if (unlikely(bio_barrier(bio)))
  937. return -EOPNOTSUPP;
  938. /* Only tell snapshots if this is a write */
  939. return (bio_rw(bio) == WRITE) ? do_origin(dev, bio) : 1;
  940. }
  941. #define min_not_zero(l, r) (l == 0) ? r : ((r == 0) ? l : min(l, r))
  942. /*
  943. * Set the target "split_io" field to the minimum of all the snapshots'
  944. * chunk sizes.
  945. */
  946. static void origin_resume(struct dm_target *ti)
  947. {
  948. struct dm_dev *dev = (struct dm_dev *) ti->private;
  949. struct dm_snapshot *snap;
  950. struct origin *o;
  951. chunk_t chunk_size = 0;
  952. down_read(&_origins_lock);
  953. o = __lookup_origin(dev->bdev);
  954. if (o)
  955. list_for_each_entry (snap, &o->snapshots, list)
  956. chunk_size = min_not_zero(chunk_size, snap->chunk_size);
  957. up_read(&_origins_lock);
  958. ti->split_io = chunk_size;
  959. }
  960. static int origin_status(struct dm_target *ti, status_type_t type, char *result,
  961. unsigned int maxlen)
  962. {
  963. struct dm_dev *dev = (struct dm_dev *) ti->private;
  964. switch (type) {
  965. case STATUSTYPE_INFO:
  966. result[0] = '\0';
  967. break;
  968. case STATUSTYPE_TABLE:
  969. snprintf(result, maxlen, "%s", dev->name);
  970. break;
  971. }
  972. return 0;
  973. }
  974. static struct target_type origin_target = {
  975. .name = "snapshot-origin",
  976. .version = {1, 5, 0},
  977. .module = THIS_MODULE,
  978. .ctr = origin_ctr,
  979. .dtr = origin_dtr,
  980. .map = origin_map,
  981. .resume = origin_resume,
  982. .status = origin_status,
  983. };
  984. static struct target_type snapshot_target = {
  985. .name = "snapshot",
  986. .version = {1, 5, 0},
  987. .module = THIS_MODULE,
  988. .ctr = snapshot_ctr,
  989. .dtr = snapshot_dtr,
  990. .map = snapshot_map,
  991. .resume = snapshot_resume,
  992. .status = snapshot_status,
  993. };
  994. static int __init dm_snapshot_init(void)
  995. {
  996. int r;
  997. r = dm_register_target(&snapshot_target);
  998. if (r) {
  999. DMERR("snapshot target register failed %d", r);
  1000. return r;
  1001. }
  1002. r = dm_register_target(&origin_target);
  1003. if (r < 0) {
  1004. DMERR("Origin target register failed %d", r);
  1005. goto bad1;
  1006. }
  1007. r = init_origin_hash();
  1008. if (r) {
  1009. DMERR("init_origin_hash failed.");
  1010. goto bad2;
  1011. }
  1012. exception_cache = kmem_cache_create("dm-snapshot-ex",
  1013. sizeof(struct exception),
  1014. __alignof__(struct exception),
  1015. 0, NULL, NULL);
  1016. if (!exception_cache) {
  1017. DMERR("Couldn't create exception cache.");
  1018. r = -ENOMEM;
  1019. goto bad3;
  1020. }
  1021. pending_cache =
  1022. kmem_cache_create("dm-snapshot-in",
  1023. sizeof(struct pending_exception),
  1024. __alignof__(struct pending_exception),
  1025. 0, NULL, NULL);
  1026. if (!pending_cache) {
  1027. DMERR("Couldn't create pending cache.");
  1028. r = -ENOMEM;
  1029. goto bad4;
  1030. }
  1031. pending_pool = mempool_create_slab_pool(128, pending_cache);
  1032. if (!pending_pool) {
  1033. DMERR("Couldn't create pending pool.");
  1034. r = -ENOMEM;
  1035. goto bad5;
  1036. }
  1037. ksnapd = create_singlethread_workqueue("ksnapd");
  1038. if (!ksnapd) {
  1039. DMERR("Failed to create ksnapd workqueue.");
  1040. r = -ENOMEM;
  1041. goto bad6;
  1042. }
  1043. return 0;
  1044. bad6:
  1045. mempool_destroy(pending_pool);
  1046. bad5:
  1047. kmem_cache_destroy(pending_cache);
  1048. bad4:
  1049. kmem_cache_destroy(exception_cache);
  1050. bad3:
  1051. exit_origin_hash();
  1052. bad2:
  1053. dm_unregister_target(&origin_target);
  1054. bad1:
  1055. dm_unregister_target(&snapshot_target);
  1056. return r;
  1057. }
  1058. static void __exit dm_snapshot_exit(void)
  1059. {
  1060. int r;
  1061. destroy_workqueue(ksnapd);
  1062. r = dm_unregister_target(&snapshot_target);
  1063. if (r)
  1064. DMERR("snapshot unregister failed %d", r);
  1065. r = dm_unregister_target(&origin_target);
  1066. if (r)
  1067. DMERR("origin unregister failed %d", r);
  1068. exit_origin_hash();
  1069. mempool_destroy(pending_pool);
  1070. kmem_cache_destroy(pending_cache);
  1071. kmem_cache_destroy(exception_cache);
  1072. }
  1073. /* Module hooks */
  1074. module_init(dm_snapshot_init);
  1075. module_exit(dm_snapshot_exit);
  1076. MODULE_DESCRIPTION(DM_NAME " snapshot target");
  1077. MODULE_AUTHOR("Joe Thornber");
  1078. MODULE_LICENSE("GPL");