dm-exception-store.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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 "dm.h"
  9. #include "dm-snap.h"
  10. #include "dm-io.h"
  11. #include "kcopyd.h"
  12. #include <linux/mm.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/slab.h>
  16. #define DM_MSG_PREFIX "snapshots"
  17. #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32 /* 16KB */
  18. /*-----------------------------------------------------------------
  19. * Persistent snapshots, by persistent we mean that the snapshot
  20. * will survive a reboot.
  21. *---------------------------------------------------------------*/
  22. /*
  23. * We need to store a record of which parts of the origin have
  24. * been copied to the snapshot device. The snapshot code
  25. * requires that we copy exception chunks to chunk aligned areas
  26. * of the COW store. It makes sense therefore, to store the
  27. * metadata in chunk size blocks.
  28. *
  29. * There is no backward or forward compatibility implemented,
  30. * snapshots with different disk versions than the kernel will
  31. * not be usable. It is expected that "lvcreate" will blank out
  32. * the start of a fresh COW device before calling the snapshot
  33. * constructor.
  34. *
  35. * The first chunk of the COW device just contains the header.
  36. * After this there is a chunk filled with exception metadata,
  37. * followed by as many exception chunks as can fit in the
  38. * metadata areas.
  39. *
  40. * All on disk structures are in little-endian format. The end
  41. * of the exceptions info is indicated by an exception with a
  42. * new_chunk of 0, which is invalid since it would point to the
  43. * header chunk.
  44. */
  45. /*
  46. * Magic for persistent snapshots: "SnAp" - Feeble isn't it.
  47. */
  48. #define SNAP_MAGIC 0x70416e53
  49. /*
  50. * The on-disk version of the metadata.
  51. */
  52. #define SNAPSHOT_DISK_VERSION 1
  53. struct disk_header {
  54. uint32_t magic;
  55. /*
  56. * Is this snapshot valid. There is no way of recovering
  57. * an invalid snapshot.
  58. */
  59. uint32_t valid;
  60. /*
  61. * Simple, incrementing version. no backward
  62. * compatibility.
  63. */
  64. uint32_t version;
  65. /* In sectors */
  66. uint32_t chunk_size;
  67. };
  68. struct disk_exception {
  69. uint64_t old_chunk;
  70. uint64_t new_chunk;
  71. };
  72. struct commit_callback {
  73. void (*callback)(void *, int success);
  74. void *context;
  75. };
  76. /*
  77. * The top level structure for a persistent exception store.
  78. */
  79. struct pstore {
  80. struct dm_snapshot *snap; /* up pointer to my snapshot */
  81. int version;
  82. int valid;
  83. uint32_t exceptions_per_area;
  84. /*
  85. * Now that we have an asynchronous kcopyd there is no
  86. * need for large chunk sizes, so it wont hurt to have a
  87. * whole chunks worth of metadata in memory at once.
  88. */
  89. void *area;
  90. /*
  91. * Used to keep track of which metadata area the data in
  92. * 'chunk' refers to.
  93. */
  94. uint32_t current_area;
  95. /*
  96. * The next free chunk for an exception.
  97. */
  98. uint32_t next_free;
  99. /*
  100. * The index of next free exception in the current
  101. * metadata area.
  102. */
  103. uint32_t current_committed;
  104. atomic_t pending_count;
  105. uint32_t callback_count;
  106. struct commit_callback *callbacks;
  107. };
  108. static inline unsigned int sectors_to_pages(unsigned int sectors)
  109. {
  110. return sectors / (PAGE_SIZE >> 9);
  111. }
  112. static int alloc_area(struct pstore *ps)
  113. {
  114. int r = -ENOMEM;
  115. size_t len;
  116. len = ps->snap->chunk_size << SECTOR_SHIFT;
  117. /*
  118. * Allocate the chunk_size block of memory that will hold
  119. * a single metadata area.
  120. */
  121. ps->area = vmalloc(len);
  122. if (!ps->area)
  123. return r;
  124. return 0;
  125. }
  126. static void free_area(struct pstore *ps)
  127. {
  128. vfree(ps->area);
  129. ps->area = NULL;
  130. }
  131. /*
  132. * Read or write a chunk aligned and sized block of data from a device.
  133. */
  134. static int chunk_io(struct pstore *ps, uint32_t chunk, int rw)
  135. {
  136. struct io_region where;
  137. unsigned long bits;
  138. where.bdev = ps->snap->cow->bdev;
  139. where.sector = ps->snap->chunk_size * chunk;
  140. where.count = ps->snap->chunk_size;
  141. return dm_io_sync_vm(1, &where, rw, ps->area, &bits);
  142. }
  143. /*
  144. * Read or write a metadata area. Remembering to skip the first
  145. * chunk which holds the header.
  146. */
  147. static int area_io(struct pstore *ps, uint32_t area, int rw)
  148. {
  149. int r;
  150. uint32_t chunk;
  151. /* convert a metadata area index to a chunk index */
  152. chunk = 1 + ((ps->exceptions_per_area + 1) * area);
  153. r = chunk_io(ps, chunk, rw);
  154. if (r)
  155. return r;
  156. ps->current_area = area;
  157. return 0;
  158. }
  159. static int zero_area(struct pstore *ps, uint32_t area)
  160. {
  161. memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT);
  162. return area_io(ps, area, WRITE);
  163. }
  164. static int read_header(struct pstore *ps, int *new_snapshot)
  165. {
  166. int r;
  167. struct disk_header *dh;
  168. chunk_t chunk_size;
  169. int chunk_size_supplied = 1;
  170. /*
  171. * Use default chunk size (or hardsect_size, if larger) if none supplied
  172. */
  173. if (!ps->snap->chunk_size) {
  174. ps->snap->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS,
  175. bdev_hardsect_size(ps->snap->cow->bdev) >> 9);
  176. ps->snap->chunk_mask = ps->snap->chunk_size - 1;
  177. ps->snap->chunk_shift = ffs(ps->snap->chunk_size) - 1;
  178. chunk_size_supplied = 0;
  179. }
  180. r = dm_io_get(sectors_to_pages(ps->snap->chunk_size));
  181. if (r)
  182. return r;
  183. r = alloc_area(ps);
  184. if (r)
  185. goto bad1;
  186. r = chunk_io(ps, 0, READ);
  187. if (r)
  188. goto bad2;
  189. dh = (struct disk_header *) ps->area;
  190. if (le32_to_cpu(dh->magic) == 0) {
  191. *new_snapshot = 1;
  192. return 0;
  193. }
  194. if (le32_to_cpu(dh->magic) != SNAP_MAGIC) {
  195. DMWARN("Invalid or corrupt snapshot");
  196. r = -ENXIO;
  197. goto bad2;
  198. }
  199. *new_snapshot = 0;
  200. ps->valid = le32_to_cpu(dh->valid);
  201. ps->version = le32_to_cpu(dh->version);
  202. chunk_size = le32_to_cpu(dh->chunk_size);
  203. if (!chunk_size_supplied || ps->snap->chunk_size == chunk_size)
  204. return 0;
  205. DMWARN("chunk size %llu in device metadata overrides "
  206. "table chunk size of %llu.",
  207. (unsigned long long)chunk_size,
  208. (unsigned long long)ps->snap->chunk_size);
  209. /* We had a bogus chunk_size. Fix stuff up. */
  210. dm_io_put(sectors_to_pages(ps->snap->chunk_size));
  211. free_area(ps);
  212. ps->snap->chunk_size = chunk_size;
  213. ps->snap->chunk_mask = chunk_size - 1;
  214. ps->snap->chunk_shift = ffs(chunk_size) - 1;
  215. r = dm_io_get(sectors_to_pages(chunk_size));
  216. if (r)
  217. return r;
  218. r = alloc_area(ps);
  219. if (r)
  220. goto bad1;
  221. return 0;
  222. bad2:
  223. free_area(ps);
  224. bad1:
  225. dm_io_put(sectors_to_pages(ps->snap->chunk_size));
  226. return r;
  227. }
  228. static int write_header(struct pstore *ps)
  229. {
  230. struct disk_header *dh;
  231. memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT);
  232. dh = (struct disk_header *) ps->area;
  233. dh->magic = cpu_to_le32(SNAP_MAGIC);
  234. dh->valid = cpu_to_le32(ps->valid);
  235. dh->version = cpu_to_le32(ps->version);
  236. dh->chunk_size = cpu_to_le32(ps->snap->chunk_size);
  237. return chunk_io(ps, 0, WRITE);
  238. }
  239. /*
  240. * Access functions for the disk exceptions, these do the endian conversions.
  241. */
  242. static struct disk_exception *get_exception(struct pstore *ps, uint32_t index)
  243. {
  244. BUG_ON(index >= ps->exceptions_per_area);
  245. return ((struct disk_exception *) ps->area) + index;
  246. }
  247. static void read_exception(struct pstore *ps,
  248. uint32_t index, struct disk_exception *result)
  249. {
  250. struct disk_exception *e = get_exception(ps, index);
  251. /* copy it */
  252. result->old_chunk = le64_to_cpu(e->old_chunk);
  253. result->new_chunk = le64_to_cpu(e->new_chunk);
  254. }
  255. static void write_exception(struct pstore *ps,
  256. uint32_t index, struct disk_exception *de)
  257. {
  258. struct disk_exception *e = get_exception(ps, index);
  259. /* copy it */
  260. e->old_chunk = cpu_to_le64(de->old_chunk);
  261. e->new_chunk = cpu_to_le64(de->new_chunk);
  262. }
  263. /*
  264. * Registers the exceptions that are present in the current area.
  265. * 'full' is filled in to indicate if the area has been
  266. * filled.
  267. */
  268. static int insert_exceptions(struct pstore *ps, int *full)
  269. {
  270. int r;
  271. unsigned int i;
  272. struct disk_exception de;
  273. /* presume the area is full */
  274. *full = 1;
  275. for (i = 0; i < ps->exceptions_per_area; i++) {
  276. read_exception(ps, i, &de);
  277. /*
  278. * If the new_chunk is pointing at the start of
  279. * the COW device, where the first metadata area
  280. * is we know that we've hit the end of the
  281. * exceptions. Therefore the area is not full.
  282. */
  283. if (de.new_chunk == 0LL) {
  284. ps->current_committed = i;
  285. *full = 0;
  286. break;
  287. }
  288. /*
  289. * Keep track of the start of the free chunks.
  290. */
  291. if (ps->next_free <= de.new_chunk)
  292. ps->next_free = de.new_chunk + 1;
  293. /*
  294. * Otherwise we add the exception to the snapshot.
  295. */
  296. r = dm_add_exception(ps->snap, de.old_chunk, de.new_chunk);
  297. if (r)
  298. return r;
  299. }
  300. return 0;
  301. }
  302. static int read_exceptions(struct pstore *ps)
  303. {
  304. uint32_t area;
  305. int r, full = 1;
  306. /*
  307. * Keeping reading chunks and inserting exceptions until
  308. * we find a partially full area.
  309. */
  310. for (area = 0; full; area++) {
  311. r = area_io(ps, area, READ);
  312. if (r)
  313. return r;
  314. r = insert_exceptions(ps, &full);
  315. if (r)
  316. return r;
  317. }
  318. return 0;
  319. }
  320. static inline struct pstore *get_info(struct exception_store *store)
  321. {
  322. return (struct pstore *) store->context;
  323. }
  324. static void persistent_fraction_full(struct exception_store *store,
  325. sector_t *numerator, sector_t *denominator)
  326. {
  327. *numerator = get_info(store)->next_free * store->snap->chunk_size;
  328. *denominator = get_dev_size(store->snap->cow->bdev);
  329. }
  330. static void persistent_destroy(struct exception_store *store)
  331. {
  332. struct pstore *ps = get_info(store);
  333. dm_io_put(sectors_to_pages(ps->snap->chunk_size));
  334. vfree(ps->callbacks);
  335. free_area(ps);
  336. kfree(ps);
  337. }
  338. static int persistent_read_metadata(struct exception_store *store)
  339. {
  340. int r, new_snapshot;
  341. struct pstore *ps = get_info(store);
  342. /*
  343. * Read the snapshot header.
  344. */
  345. r = read_header(ps, &new_snapshot);
  346. if (r)
  347. return r;
  348. /*
  349. * Now we know correct chunk_size, complete the initialisation.
  350. */
  351. ps->exceptions_per_area = (ps->snap->chunk_size << SECTOR_SHIFT) /
  352. sizeof(struct disk_exception);
  353. ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
  354. sizeof(*ps->callbacks));
  355. if (!ps->callbacks)
  356. return -ENOMEM;
  357. /*
  358. * Do we need to setup a new snapshot ?
  359. */
  360. if (new_snapshot) {
  361. r = write_header(ps);
  362. if (r) {
  363. DMWARN("write_header failed");
  364. return r;
  365. }
  366. r = zero_area(ps, 0);
  367. if (r) {
  368. DMWARN("zero_area(0) failed");
  369. return r;
  370. }
  371. } else {
  372. /*
  373. * Sanity checks.
  374. */
  375. if (!ps->valid) {
  376. DMWARN("snapshot is marked invalid");
  377. return -EINVAL;
  378. }
  379. if (ps->version != SNAPSHOT_DISK_VERSION) {
  380. DMWARN("unable to handle snapshot disk version %d",
  381. ps->version);
  382. return -EINVAL;
  383. }
  384. /*
  385. * Read the metadata.
  386. */
  387. r = read_exceptions(ps);
  388. if (r)
  389. return r;
  390. }
  391. return 0;
  392. }
  393. static int persistent_prepare(struct exception_store *store,
  394. struct exception *e)
  395. {
  396. struct pstore *ps = get_info(store);
  397. uint32_t stride;
  398. sector_t size = get_dev_size(store->snap->cow->bdev);
  399. /* Is there enough room ? */
  400. if (size < ((ps->next_free + 1) * store->snap->chunk_size))
  401. return -ENOSPC;
  402. e->new_chunk = ps->next_free;
  403. /*
  404. * Move onto the next free pending, making sure to take
  405. * into account the location of the metadata chunks.
  406. */
  407. stride = (ps->exceptions_per_area + 1);
  408. if ((++ps->next_free % stride) == 1)
  409. ps->next_free++;
  410. atomic_inc(&ps->pending_count);
  411. return 0;
  412. }
  413. static void persistent_commit(struct exception_store *store,
  414. struct exception *e,
  415. void (*callback) (void *, int success),
  416. void *callback_context)
  417. {
  418. int r;
  419. unsigned int i;
  420. struct pstore *ps = get_info(store);
  421. struct disk_exception de;
  422. struct commit_callback *cb;
  423. de.old_chunk = e->old_chunk;
  424. de.new_chunk = e->new_chunk;
  425. write_exception(ps, ps->current_committed++, &de);
  426. /*
  427. * Add the callback to the back of the array. This code
  428. * is the only place where the callback array is
  429. * manipulated, and we know that it will never be called
  430. * multiple times concurrently.
  431. */
  432. cb = ps->callbacks + ps->callback_count++;
  433. cb->callback = callback;
  434. cb->context = callback_context;
  435. /*
  436. * If there are no more exceptions in flight, or we have
  437. * filled this metadata area we commit the exceptions to
  438. * disk.
  439. */
  440. if (atomic_dec_and_test(&ps->pending_count) ||
  441. (ps->current_committed == ps->exceptions_per_area)) {
  442. r = area_io(ps, ps->current_area, WRITE);
  443. if (r)
  444. ps->valid = 0;
  445. /*
  446. * Have we completely filled the current area ?
  447. */
  448. if (ps->current_committed == ps->exceptions_per_area) {
  449. ps->current_committed = 0;
  450. r = zero_area(ps, ps->current_area + 1);
  451. if (r)
  452. ps->valid = 0;
  453. }
  454. for (i = 0; i < ps->callback_count; i++) {
  455. cb = ps->callbacks + i;
  456. cb->callback(cb->context, r == 0 ? 1 : 0);
  457. }
  458. ps->callback_count = 0;
  459. }
  460. }
  461. static void persistent_drop(struct exception_store *store)
  462. {
  463. struct pstore *ps = get_info(store);
  464. ps->valid = 0;
  465. if (write_header(ps))
  466. DMWARN("write header failed");
  467. }
  468. int dm_create_persistent(struct exception_store *store)
  469. {
  470. struct pstore *ps;
  471. /* allocate the pstore */
  472. ps = kmalloc(sizeof(*ps), GFP_KERNEL);
  473. if (!ps)
  474. return -ENOMEM;
  475. ps->snap = store->snap;
  476. ps->valid = 1;
  477. ps->version = SNAPSHOT_DISK_VERSION;
  478. ps->area = NULL;
  479. ps->next_free = 2; /* skipping the header and first area */
  480. ps->current_committed = 0;
  481. ps->callback_count = 0;
  482. atomic_set(&ps->pending_count, 0);
  483. ps->callbacks = NULL;
  484. store->destroy = persistent_destroy;
  485. store->read_metadata = persistent_read_metadata;
  486. store->prepare_exception = persistent_prepare;
  487. store->commit_exception = persistent_commit;
  488. store->drop_snapshot = persistent_drop;
  489. store->fraction_full = persistent_fraction_full;
  490. store->context = ps;
  491. return 0;
  492. }
  493. /*-----------------------------------------------------------------
  494. * Implementation of the store for non-persistent snapshots.
  495. *---------------------------------------------------------------*/
  496. struct transient_c {
  497. sector_t next_free;
  498. };
  499. static void transient_destroy(struct exception_store *store)
  500. {
  501. kfree(store->context);
  502. }
  503. static int transient_read_metadata(struct exception_store *store)
  504. {
  505. return 0;
  506. }
  507. static int transient_prepare(struct exception_store *store, struct exception *e)
  508. {
  509. struct transient_c *tc = (struct transient_c *) store->context;
  510. sector_t size = get_dev_size(store->snap->cow->bdev);
  511. if (size < (tc->next_free + store->snap->chunk_size))
  512. return -1;
  513. e->new_chunk = sector_to_chunk(store->snap, tc->next_free);
  514. tc->next_free += store->snap->chunk_size;
  515. return 0;
  516. }
  517. static void transient_commit(struct exception_store *store,
  518. struct exception *e,
  519. void (*callback) (void *, int success),
  520. void *callback_context)
  521. {
  522. /* Just succeed */
  523. callback(callback_context, 1);
  524. }
  525. static void transient_fraction_full(struct exception_store *store,
  526. sector_t *numerator, sector_t *denominator)
  527. {
  528. *numerator = ((struct transient_c *) store->context)->next_free;
  529. *denominator = get_dev_size(store->snap->cow->bdev);
  530. }
  531. int dm_create_transient(struct exception_store *store)
  532. {
  533. struct transient_c *tc;
  534. store->destroy = transient_destroy;
  535. store->read_metadata = transient_read_metadata;
  536. store->prepare_exception = transient_prepare;
  537. store->commit_exception = transient_commit;
  538. store->drop_snapshot = NULL;
  539. store->fraction_full = transient_fraction_full;
  540. tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
  541. if (!tc)
  542. return -ENOMEM;
  543. tc->next_free = 0;
  544. store->context = tc;
  545. return 0;
  546. }