dm-snap.c 27 KB

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