dm-snap.c 28 KB

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