dm-snap-persistent.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /*
  2. * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
  3. * Copyright (C) 2006-2008 Red Hat GmbH
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm-exception-store.h"
  8. #include <linux/mm.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/export.h>
  12. #include <linux/slab.h>
  13. #include <linux/dm-io.h>
  14. #include "dm-bufio.h"
  15. #define DM_MSG_PREFIX "persistent snapshot"
  16. #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32 /* 16KB */
  17. #define DM_PREFETCH_CHUNKS 12
  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. #define NUM_SNAPSHOT_HDR_CHUNKS 1
  54. struct disk_header {
  55. __le32 magic;
  56. /*
  57. * Is this snapshot valid. There is no way of recovering
  58. * an invalid snapshot.
  59. */
  60. __le32 valid;
  61. /*
  62. * Simple, incrementing version. no backward
  63. * compatibility.
  64. */
  65. __le32 version;
  66. /* In sectors */
  67. __le32 chunk_size;
  68. } __packed;
  69. struct disk_exception {
  70. __le64 old_chunk;
  71. __le64 new_chunk;
  72. } __packed;
  73. struct core_exception {
  74. uint64_t old_chunk;
  75. uint64_t new_chunk;
  76. };
  77. struct commit_callback {
  78. void (*callback)(void *, int success);
  79. void *context;
  80. };
  81. /*
  82. * The top level structure for a persistent exception store.
  83. */
  84. struct pstore {
  85. struct dm_exception_store *store;
  86. int version;
  87. int valid;
  88. uint32_t exceptions_per_area;
  89. /*
  90. * Now that we have an asynchronous kcopyd there is no
  91. * need for large chunk sizes, so it wont hurt to have a
  92. * whole chunks worth of metadata in memory at once.
  93. */
  94. void *area;
  95. /*
  96. * An area of zeros used to clear the next area.
  97. */
  98. void *zero_area;
  99. /*
  100. * An area used for header. The header can be written
  101. * concurrently with metadata (when invalidating the snapshot),
  102. * so it needs a separate buffer.
  103. */
  104. void *header_area;
  105. /*
  106. * Used to keep track of which metadata area the data in
  107. * 'chunk' refers to.
  108. */
  109. chunk_t current_area;
  110. /*
  111. * The next free chunk for an exception.
  112. *
  113. * When creating exceptions, all the chunks here and above are
  114. * free. It holds the next chunk to be allocated. On rare
  115. * occasions (e.g. after a system crash) holes can be left in
  116. * the exception store because chunks can be committed out of
  117. * order.
  118. *
  119. * When merging exceptions, it does not necessarily mean all the
  120. * chunks here and above are free. It holds the value it would
  121. * have held if all chunks had been committed in order of
  122. * allocation. Consequently the value may occasionally be
  123. * slightly too low, but since it's only used for 'status' and
  124. * it can never reach its minimum value too early this doesn't
  125. * matter.
  126. */
  127. chunk_t next_free;
  128. /*
  129. * The index of next free exception in the current
  130. * metadata area.
  131. */
  132. uint32_t current_committed;
  133. atomic_t pending_count;
  134. uint32_t callback_count;
  135. struct commit_callback *callbacks;
  136. struct dm_io_client *io_client;
  137. struct workqueue_struct *metadata_wq;
  138. };
  139. static int alloc_area(struct pstore *ps)
  140. {
  141. int r = -ENOMEM;
  142. size_t len;
  143. len = ps->store->chunk_size << SECTOR_SHIFT;
  144. /*
  145. * Allocate the chunk_size block of memory that will hold
  146. * a single metadata area.
  147. */
  148. ps->area = vmalloc(len);
  149. if (!ps->area)
  150. goto err_area;
  151. ps->zero_area = vzalloc(len);
  152. if (!ps->zero_area)
  153. goto err_zero_area;
  154. ps->header_area = vmalloc(len);
  155. if (!ps->header_area)
  156. goto err_header_area;
  157. return 0;
  158. err_header_area:
  159. vfree(ps->zero_area);
  160. err_zero_area:
  161. vfree(ps->area);
  162. err_area:
  163. return r;
  164. }
  165. static void free_area(struct pstore *ps)
  166. {
  167. vfree(ps->area);
  168. ps->area = NULL;
  169. vfree(ps->zero_area);
  170. ps->zero_area = NULL;
  171. vfree(ps->header_area);
  172. ps->header_area = NULL;
  173. }
  174. struct mdata_req {
  175. struct dm_io_region *where;
  176. struct dm_io_request *io_req;
  177. struct work_struct work;
  178. int result;
  179. };
  180. static void do_metadata(struct work_struct *work)
  181. {
  182. struct mdata_req *req = container_of(work, struct mdata_req, work);
  183. req->result = dm_io(req->io_req, 1, req->where, NULL);
  184. }
  185. /*
  186. * Read or write a chunk aligned and sized block of data from a device.
  187. */
  188. static int chunk_io(struct pstore *ps, void *area, chunk_t chunk, int rw,
  189. int metadata)
  190. {
  191. struct dm_io_region where = {
  192. .bdev = dm_snap_cow(ps->store->snap)->bdev,
  193. .sector = ps->store->chunk_size * chunk,
  194. .count = ps->store->chunk_size,
  195. };
  196. struct dm_io_request io_req = {
  197. .bi_rw = rw,
  198. .mem.type = DM_IO_VMA,
  199. .mem.ptr.vma = area,
  200. .client = ps->io_client,
  201. .notify.fn = NULL,
  202. };
  203. struct mdata_req req;
  204. if (!metadata)
  205. return dm_io(&io_req, 1, &where, NULL);
  206. req.where = &where;
  207. req.io_req = &io_req;
  208. /*
  209. * Issue the synchronous I/O from a different thread
  210. * to avoid generic_make_request recursion.
  211. */
  212. INIT_WORK_ONSTACK(&req.work, do_metadata);
  213. queue_work(ps->metadata_wq, &req.work);
  214. flush_workqueue(ps->metadata_wq);
  215. destroy_work_on_stack(&req.work);
  216. return req.result;
  217. }
  218. /*
  219. * Convert a metadata area index to a chunk index.
  220. */
  221. static chunk_t area_location(struct pstore *ps, chunk_t area)
  222. {
  223. return NUM_SNAPSHOT_HDR_CHUNKS + ((ps->exceptions_per_area + 1) * area);
  224. }
  225. static void skip_metadata(struct pstore *ps)
  226. {
  227. uint32_t stride = ps->exceptions_per_area + 1;
  228. chunk_t next_free = ps->next_free;
  229. if (sector_div(next_free, stride) == NUM_SNAPSHOT_HDR_CHUNKS)
  230. ps->next_free++;
  231. }
  232. /*
  233. * Read or write a metadata area. Remembering to skip the first
  234. * chunk which holds the header.
  235. */
  236. static int area_io(struct pstore *ps, int rw)
  237. {
  238. int r;
  239. chunk_t chunk;
  240. chunk = area_location(ps, ps->current_area);
  241. r = chunk_io(ps, ps->area, chunk, rw, 0);
  242. if (r)
  243. return r;
  244. return 0;
  245. }
  246. static void zero_memory_area(struct pstore *ps)
  247. {
  248. memset(ps->area, 0, ps->store->chunk_size << SECTOR_SHIFT);
  249. }
  250. static int zero_disk_area(struct pstore *ps, chunk_t area)
  251. {
  252. return chunk_io(ps, ps->zero_area, area_location(ps, area), WRITE, 0);
  253. }
  254. static int read_header(struct pstore *ps, int *new_snapshot)
  255. {
  256. int r;
  257. struct disk_header *dh;
  258. unsigned chunk_size;
  259. int chunk_size_supplied = 1;
  260. char *chunk_err;
  261. /*
  262. * Use default chunk size (or logical_block_size, if larger)
  263. * if none supplied
  264. */
  265. if (!ps->store->chunk_size) {
  266. ps->store->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS,
  267. bdev_logical_block_size(dm_snap_cow(ps->store->snap)->
  268. bdev) >> 9);
  269. ps->store->chunk_mask = ps->store->chunk_size - 1;
  270. ps->store->chunk_shift = ffs(ps->store->chunk_size) - 1;
  271. chunk_size_supplied = 0;
  272. }
  273. ps->io_client = dm_io_client_create();
  274. if (IS_ERR(ps->io_client))
  275. return PTR_ERR(ps->io_client);
  276. r = alloc_area(ps);
  277. if (r)
  278. return r;
  279. r = chunk_io(ps, ps->header_area, 0, READ, 1);
  280. if (r)
  281. goto bad;
  282. dh = ps->header_area;
  283. if (le32_to_cpu(dh->magic) == 0) {
  284. *new_snapshot = 1;
  285. return 0;
  286. }
  287. if (le32_to_cpu(dh->magic) != SNAP_MAGIC) {
  288. DMWARN("Invalid or corrupt snapshot");
  289. r = -ENXIO;
  290. goto bad;
  291. }
  292. *new_snapshot = 0;
  293. ps->valid = le32_to_cpu(dh->valid);
  294. ps->version = le32_to_cpu(dh->version);
  295. chunk_size = le32_to_cpu(dh->chunk_size);
  296. if (ps->store->chunk_size == chunk_size)
  297. return 0;
  298. if (chunk_size_supplied)
  299. DMWARN("chunk size %u in device metadata overrides "
  300. "table chunk size of %u.",
  301. chunk_size, ps->store->chunk_size);
  302. /* We had a bogus chunk_size. Fix stuff up. */
  303. free_area(ps);
  304. r = dm_exception_store_set_chunk_size(ps->store, chunk_size,
  305. &chunk_err);
  306. if (r) {
  307. DMERR("invalid on-disk chunk size %u: %s.",
  308. chunk_size, chunk_err);
  309. return r;
  310. }
  311. r = alloc_area(ps);
  312. return r;
  313. bad:
  314. free_area(ps);
  315. return r;
  316. }
  317. static int write_header(struct pstore *ps)
  318. {
  319. struct disk_header *dh;
  320. memset(ps->header_area, 0, ps->store->chunk_size << SECTOR_SHIFT);
  321. dh = ps->header_area;
  322. dh->magic = cpu_to_le32(SNAP_MAGIC);
  323. dh->valid = cpu_to_le32(ps->valid);
  324. dh->version = cpu_to_le32(ps->version);
  325. dh->chunk_size = cpu_to_le32(ps->store->chunk_size);
  326. return chunk_io(ps, ps->header_area, 0, WRITE, 1);
  327. }
  328. /*
  329. * Access functions for the disk exceptions, these do the endian conversions.
  330. */
  331. static struct disk_exception *get_exception(struct pstore *ps, void *ps_area,
  332. uint32_t index)
  333. {
  334. BUG_ON(index >= ps->exceptions_per_area);
  335. return ((struct disk_exception *) ps_area) + index;
  336. }
  337. static void read_exception(struct pstore *ps, void *ps_area,
  338. uint32_t index, struct core_exception *result)
  339. {
  340. struct disk_exception *de = get_exception(ps, ps_area, index);
  341. /* copy it */
  342. result->old_chunk = le64_to_cpu(de->old_chunk);
  343. result->new_chunk = le64_to_cpu(de->new_chunk);
  344. }
  345. static void write_exception(struct pstore *ps,
  346. uint32_t index, struct core_exception *e)
  347. {
  348. struct disk_exception *de = get_exception(ps, ps->area, index);
  349. /* copy it */
  350. de->old_chunk = cpu_to_le64(e->old_chunk);
  351. de->new_chunk = cpu_to_le64(e->new_chunk);
  352. }
  353. static void clear_exception(struct pstore *ps, uint32_t index)
  354. {
  355. struct disk_exception *de = get_exception(ps, ps->area, index);
  356. /* clear it */
  357. de->old_chunk = 0;
  358. de->new_chunk = 0;
  359. }
  360. /*
  361. * Registers the exceptions that are present in the current area.
  362. * 'full' is filled in to indicate if the area has been
  363. * filled.
  364. */
  365. static int insert_exceptions(struct pstore *ps, void *ps_area,
  366. int (*callback)(void *callback_context,
  367. chunk_t old, chunk_t new),
  368. void *callback_context,
  369. int *full)
  370. {
  371. int r;
  372. unsigned int i;
  373. struct core_exception e;
  374. /* presume the area is full */
  375. *full = 1;
  376. for (i = 0; i < ps->exceptions_per_area; i++) {
  377. read_exception(ps, ps_area, i, &e);
  378. /*
  379. * If the new_chunk is pointing at the start of
  380. * the COW device, where the first metadata area
  381. * is we know that we've hit the end of the
  382. * exceptions. Therefore the area is not full.
  383. */
  384. if (e.new_chunk == 0LL) {
  385. ps->current_committed = i;
  386. *full = 0;
  387. break;
  388. }
  389. /*
  390. * Keep track of the start of the free chunks.
  391. */
  392. if (ps->next_free <= e.new_chunk)
  393. ps->next_free = e.new_chunk + 1;
  394. /*
  395. * Otherwise we add the exception to the snapshot.
  396. */
  397. r = callback(callback_context, e.old_chunk, e.new_chunk);
  398. if (r)
  399. return r;
  400. }
  401. return 0;
  402. }
  403. static int read_exceptions(struct pstore *ps,
  404. int (*callback)(void *callback_context, chunk_t old,
  405. chunk_t new),
  406. void *callback_context)
  407. {
  408. int r, full = 1;
  409. struct dm_bufio_client *client;
  410. chunk_t prefetch_area = 0;
  411. client = dm_bufio_client_create(dm_snap_cow(ps->store->snap)->bdev,
  412. ps->store->chunk_size << SECTOR_SHIFT,
  413. 1, 0, NULL, NULL);
  414. if (IS_ERR(client))
  415. return PTR_ERR(client);
  416. /*
  417. * Setup for one current buffer + desired readahead buffers.
  418. */
  419. dm_bufio_set_minimum_buffers(client, 1 + DM_PREFETCH_CHUNKS);
  420. /*
  421. * Keeping reading chunks and inserting exceptions until
  422. * we find a partially full area.
  423. */
  424. for (ps->current_area = 0; full; ps->current_area++) {
  425. struct dm_buffer *bp;
  426. void *area;
  427. chunk_t chunk;
  428. if (unlikely(prefetch_area < ps->current_area))
  429. prefetch_area = ps->current_area;
  430. if (DM_PREFETCH_CHUNKS) do {
  431. chunk_t pf_chunk = area_location(ps, prefetch_area);
  432. if (unlikely(pf_chunk >= dm_bufio_get_device_size(client)))
  433. break;
  434. dm_bufio_prefetch(client, pf_chunk, 1);
  435. prefetch_area++;
  436. if (unlikely(!prefetch_area))
  437. break;
  438. } while (prefetch_area <= ps->current_area + DM_PREFETCH_CHUNKS);
  439. chunk = area_location(ps, ps->current_area);
  440. area = dm_bufio_read(client, chunk, &bp);
  441. if (unlikely(IS_ERR(area))) {
  442. r = PTR_ERR(area);
  443. goto ret_destroy_bufio;
  444. }
  445. r = insert_exceptions(ps, area, callback, callback_context,
  446. &full);
  447. if (!full)
  448. memcpy(ps->area, area, ps->store->chunk_size << SECTOR_SHIFT);
  449. dm_bufio_release(bp);
  450. dm_bufio_forget(client, chunk);
  451. if (unlikely(r))
  452. goto ret_destroy_bufio;
  453. }
  454. ps->current_area--;
  455. skip_metadata(ps);
  456. r = 0;
  457. ret_destroy_bufio:
  458. dm_bufio_client_destroy(client);
  459. return r;
  460. }
  461. static struct pstore *get_info(struct dm_exception_store *store)
  462. {
  463. return (struct pstore *) store->context;
  464. }
  465. static void persistent_usage(struct dm_exception_store *store,
  466. sector_t *total_sectors,
  467. sector_t *sectors_allocated,
  468. sector_t *metadata_sectors)
  469. {
  470. struct pstore *ps = get_info(store);
  471. *sectors_allocated = ps->next_free * store->chunk_size;
  472. *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
  473. /*
  474. * First chunk is the fixed header.
  475. * Then there are (ps->current_area + 1) metadata chunks, each one
  476. * separated from the next by ps->exceptions_per_area data chunks.
  477. */
  478. *metadata_sectors = (ps->current_area + 1 + NUM_SNAPSHOT_HDR_CHUNKS) *
  479. store->chunk_size;
  480. }
  481. static void persistent_dtr(struct dm_exception_store *store)
  482. {
  483. struct pstore *ps = get_info(store);
  484. destroy_workqueue(ps->metadata_wq);
  485. /* Created in read_header */
  486. if (ps->io_client)
  487. dm_io_client_destroy(ps->io_client);
  488. free_area(ps);
  489. /* Allocated in persistent_read_metadata */
  490. vfree(ps->callbacks);
  491. kfree(ps);
  492. }
  493. static int persistent_read_metadata(struct dm_exception_store *store,
  494. int (*callback)(void *callback_context,
  495. chunk_t old, chunk_t new),
  496. void *callback_context)
  497. {
  498. int r, uninitialized_var(new_snapshot);
  499. struct pstore *ps = get_info(store);
  500. /*
  501. * Read the snapshot header.
  502. */
  503. r = read_header(ps, &new_snapshot);
  504. if (r)
  505. return r;
  506. /*
  507. * Now we know correct chunk_size, complete the initialisation.
  508. */
  509. ps->exceptions_per_area = (ps->store->chunk_size << SECTOR_SHIFT) /
  510. sizeof(struct disk_exception);
  511. ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
  512. sizeof(*ps->callbacks));
  513. if (!ps->callbacks)
  514. return -ENOMEM;
  515. /*
  516. * Do we need to setup a new snapshot ?
  517. */
  518. if (new_snapshot) {
  519. r = write_header(ps);
  520. if (r) {
  521. DMWARN("write_header failed");
  522. return r;
  523. }
  524. ps->current_area = 0;
  525. zero_memory_area(ps);
  526. r = zero_disk_area(ps, 0);
  527. if (r)
  528. DMWARN("zero_disk_area(0) failed");
  529. return r;
  530. }
  531. /*
  532. * Sanity checks.
  533. */
  534. if (ps->version != SNAPSHOT_DISK_VERSION) {
  535. DMWARN("unable to handle snapshot disk version %d",
  536. ps->version);
  537. return -EINVAL;
  538. }
  539. /*
  540. * Metadata are valid, but snapshot is invalidated
  541. */
  542. if (!ps->valid)
  543. return 1;
  544. /*
  545. * Read the metadata.
  546. */
  547. r = read_exceptions(ps, callback, callback_context);
  548. return r;
  549. }
  550. static int persistent_prepare_exception(struct dm_exception_store *store,
  551. struct dm_exception *e)
  552. {
  553. struct pstore *ps = get_info(store);
  554. sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
  555. /* Is there enough room ? */
  556. if (size < ((ps->next_free + 1) * store->chunk_size))
  557. return -ENOSPC;
  558. e->new_chunk = ps->next_free;
  559. /*
  560. * Move onto the next free pending, making sure to take
  561. * into account the location of the metadata chunks.
  562. */
  563. ps->next_free++;
  564. skip_metadata(ps);
  565. atomic_inc(&ps->pending_count);
  566. return 0;
  567. }
  568. static void persistent_commit_exception(struct dm_exception_store *store,
  569. struct dm_exception *e,
  570. void (*callback) (void *, int success),
  571. void *callback_context)
  572. {
  573. unsigned int i;
  574. struct pstore *ps = get_info(store);
  575. struct core_exception ce;
  576. struct commit_callback *cb;
  577. ce.old_chunk = e->old_chunk;
  578. ce.new_chunk = e->new_chunk;
  579. write_exception(ps, ps->current_committed++, &ce);
  580. /*
  581. * Add the callback to the back of the array. This code
  582. * is the only place where the callback array is
  583. * manipulated, and we know that it will never be called
  584. * multiple times concurrently.
  585. */
  586. cb = ps->callbacks + ps->callback_count++;
  587. cb->callback = callback;
  588. cb->context = callback_context;
  589. /*
  590. * If there are exceptions in flight and we have not yet
  591. * filled this metadata area there's nothing more to do.
  592. */
  593. if (!atomic_dec_and_test(&ps->pending_count) &&
  594. (ps->current_committed != ps->exceptions_per_area))
  595. return;
  596. /*
  597. * If we completely filled the current area, then wipe the next one.
  598. */
  599. if ((ps->current_committed == ps->exceptions_per_area) &&
  600. zero_disk_area(ps, ps->current_area + 1))
  601. ps->valid = 0;
  602. /*
  603. * Commit exceptions to disk.
  604. */
  605. if (ps->valid && area_io(ps, WRITE_FLUSH_FUA))
  606. ps->valid = 0;
  607. /*
  608. * Advance to the next area if this one is full.
  609. */
  610. if (ps->current_committed == ps->exceptions_per_area) {
  611. ps->current_committed = 0;
  612. ps->current_area++;
  613. zero_memory_area(ps);
  614. }
  615. for (i = 0; i < ps->callback_count; i++) {
  616. cb = ps->callbacks + i;
  617. cb->callback(cb->context, ps->valid);
  618. }
  619. ps->callback_count = 0;
  620. }
  621. static int persistent_prepare_merge(struct dm_exception_store *store,
  622. chunk_t *last_old_chunk,
  623. chunk_t *last_new_chunk)
  624. {
  625. struct pstore *ps = get_info(store);
  626. struct core_exception ce;
  627. int nr_consecutive;
  628. int r;
  629. /*
  630. * When current area is empty, move back to preceding area.
  631. */
  632. if (!ps->current_committed) {
  633. /*
  634. * Have we finished?
  635. */
  636. if (!ps->current_area)
  637. return 0;
  638. ps->current_area--;
  639. r = area_io(ps, READ);
  640. if (r < 0)
  641. return r;
  642. ps->current_committed = ps->exceptions_per_area;
  643. }
  644. read_exception(ps, ps->area, ps->current_committed - 1, &ce);
  645. *last_old_chunk = ce.old_chunk;
  646. *last_new_chunk = ce.new_chunk;
  647. /*
  648. * Find number of consecutive chunks within the current area,
  649. * working backwards.
  650. */
  651. for (nr_consecutive = 1; nr_consecutive < ps->current_committed;
  652. nr_consecutive++) {
  653. read_exception(ps, ps->area,
  654. ps->current_committed - 1 - nr_consecutive, &ce);
  655. if (ce.old_chunk != *last_old_chunk - nr_consecutive ||
  656. ce.new_chunk != *last_new_chunk - nr_consecutive)
  657. break;
  658. }
  659. return nr_consecutive;
  660. }
  661. static int persistent_commit_merge(struct dm_exception_store *store,
  662. int nr_merged)
  663. {
  664. int r, i;
  665. struct pstore *ps = get_info(store);
  666. BUG_ON(nr_merged > ps->current_committed);
  667. for (i = 0; i < nr_merged; i++)
  668. clear_exception(ps, ps->current_committed - 1 - i);
  669. r = area_io(ps, WRITE_FLUSH_FUA);
  670. if (r < 0)
  671. return r;
  672. ps->current_committed -= nr_merged;
  673. /*
  674. * At this stage, only persistent_usage() uses ps->next_free, so
  675. * we make no attempt to keep ps->next_free strictly accurate
  676. * as exceptions may have been committed out-of-order originally.
  677. * Once a snapshot has become merging, we set it to the value it
  678. * would have held had all the exceptions been committed in order.
  679. *
  680. * ps->current_area does not get reduced by prepare_merge() until
  681. * after commit_merge() has removed the nr_merged previous exceptions.
  682. */
  683. ps->next_free = area_location(ps, ps->current_area) +
  684. ps->current_committed + 1;
  685. return 0;
  686. }
  687. static void persistent_drop_snapshot(struct dm_exception_store *store)
  688. {
  689. struct pstore *ps = get_info(store);
  690. ps->valid = 0;
  691. if (write_header(ps))
  692. DMWARN("write header failed");
  693. }
  694. static int persistent_ctr(struct dm_exception_store *store,
  695. unsigned argc, char **argv)
  696. {
  697. struct pstore *ps;
  698. /* allocate the pstore */
  699. ps = kzalloc(sizeof(*ps), GFP_KERNEL);
  700. if (!ps)
  701. return -ENOMEM;
  702. ps->store = store;
  703. ps->valid = 1;
  704. ps->version = SNAPSHOT_DISK_VERSION;
  705. ps->area = NULL;
  706. ps->zero_area = NULL;
  707. ps->header_area = NULL;
  708. ps->next_free = NUM_SNAPSHOT_HDR_CHUNKS + 1; /* header and 1st area */
  709. ps->current_committed = 0;
  710. ps->callback_count = 0;
  711. atomic_set(&ps->pending_count, 0);
  712. ps->callbacks = NULL;
  713. ps->metadata_wq = alloc_workqueue("ksnaphd", WQ_MEM_RECLAIM, 0);
  714. if (!ps->metadata_wq) {
  715. kfree(ps);
  716. DMERR("couldn't start header metadata update thread");
  717. return -ENOMEM;
  718. }
  719. store->context = ps;
  720. return 0;
  721. }
  722. static unsigned persistent_status(struct dm_exception_store *store,
  723. status_type_t status, char *result,
  724. unsigned maxlen)
  725. {
  726. unsigned sz = 0;
  727. switch (status) {
  728. case STATUSTYPE_INFO:
  729. break;
  730. case STATUSTYPE_TABLE:
  731. DMEMIT(" P %llu", (unsigned long long)store->chunk_size);
  732. }
  733. return sz;
  734. }
  735. static struct dm_exception_store_type _persistent_type = {
  736. .name = "persistent",
  737. .module = THIS_MODULE,
  738. .ctr = persistent_ctr,
  739. .dtr = persistent_dtr,
  740. .read_metadata = persistent_read_metadata,
  741. .prepare_exception = persistent_prepare_exception,
  742. .commit_exception = persistent_commit_exception,
  743. .prepare_merge = persistent_prepare_merge,
  744. .commit_merge = persistent_commit_merge,
  745. .drop_snapshot = persistent_drop_snapshot,
  746. .usage = persistent_usage,
  747. .status = persistent_status,
  748. };
  749. static struct dm_exception_store_type _persistent_compat_type = {
  750. .name = "P",
  751. .module = THIS_MODULE,
  752. .ctr = persistent_ctr,
  753. .dtr = persistent_dtr,
  754. .read_metadata = persistent_read_metadata,
  755. .prepare_exception = persistent_prepare_exception,
  756. .commit_exception = persistent_commit_exception,
  757. .prepare_merge = persistent_prepare_merge,
  758. .commit_merge = persistent_commit_merge,
  759. .drop_snapshot = persistent_drop_snapshot,
  760. .usage = persistent_usage,
  761. .status = persistent_status,
  762. };
  763. int dm_persistent_snapshot_init(void)
  764. {
  765. int r;
  766. r = dm_exception_store_type_register(&_persistent_type);
  767. if (r) {
  768. DMERR("Unable to register persistent exception store type");
  769. return r;
  770. }
  771. r = dm_exception_store_type_register(&_persistent_compat_type);
  772. if (r) {
  773. DMERR("Unable to register old-style persistent exception "
  774. "store type");
  775. dm_exception_store_type_unregister(&_persistent_type);
  776. return r;
  777. }
  778. return r;
  779. }
  780. void dm_persistent_snapshot_exit(void)
  781. {
  782. dm_exception_store_type_unregister(&_persistent_type);
  783. dm_exception_store_type_unregister(&_persistent_compat_type);
  784. }