dm-snap.c 25 KB

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