dm-snap.c 27 KB

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