dm-snap-persistent.c 22 KB

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