dm-exception-store.c 14 KB

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