dm-snap.c 28 KB

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