dm-exception-store.c 15 KB

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