dm-snap-persistent.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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/ctype.h>
  9. #include <linux/mm.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/vmalloc.h>
  12. #include <linux/export.h>
  13. #include <linux/slab.h>
  14. #include <linux/dm-io.h>
  15. #include <linux/dm-bufio.h>
  16. #define DM_MSG_PREFIX "persistent snapshot"
  17. #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32U /* 16KB */
  18. #define DM_PREFETCH_CHUNKS 12
  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. #define NUM_SNAPSHOT_HDR_CHUNKS 1
  55. struct disk_header {
  56. __le32 magic;
  57. /*
  58. * Is this snapshot valid. There is no way of recovering
  59. * an invalid snapshot.
  60. */
  61. __le32 valid;
  62. /*
  63. * Simple, incrementing version. no backward
  64. * compatibility.
  65. */
  66. __le32 version;
  67. /* In sectors */
  68. __le32 chunk_size;
  69. } __packed;
  70. struct disk_exception {
  71. __le64 old_chunk;
  72. __le64 new_chunk;
  73. } __packed;
  74. struct core_exception {
  75. uint64_t old_chunk;
  76. uint64_t new_chunk;
  77. };
  78. struct commit_callback {
  79. void (*callback)(void *, int success);
  80. void *context;
  81. };
  82. /*
  83. * The top level structure for a persistent exception store.
  84. */
  85. struct pstore {
  86. struct dm_exception_store *store;
  87. int version;
  88. int valid;
  89. uint32_t exceptions_per_area;
  90. /*
  91. * Now that we have an asynchronous kcopyd there is no
  92. * need for large chunk sizes, so it wont hurt to have a
  93. * whole chunks worth of metadata in memory at once.
  94. */
  95. void *area;
  96. /*
  97. * An area of zeros used to clear the next area.
  98. */
  99. void *zero_area;
  100. /*
  101. * An area used for header. The header can be written
  102. * concurrently with metadata (when invalidating the snapshot),
  103. * so it needs a separate buffer.
  104. */
  105. void *header_area;
  106. /*
  107. * Used to keep track of which metadata area the data in
  108. * 'chunk' refers to.
  109. */
  110. chunk_t current_area;
  111. /*
  112. * The next free chunk for an exception.
  113. *
  114. * When creating exceptions, all the chunks here and above are
  115. * free. It holds the next chunk to be allocated. On rare
  116. * occasions (e.g. after a system crash) holes can be left in
  117. * the exception store because chunks can be committed out of
  118. * order.
  119. *
  120. * When merging exceptions, it does not necessarily mean all the
  121. * chunks here and above are free. It holds the value it would
  122. * have held if all chunks had been committed in order of
  123. * allocation. Consequently the value may occasionally be
  124. * slightly too low, but since it's only used for 'status' and
  125. * it can never reach its minimum value too early this doesn't
  126. * matter.
  127. */
  128. chunk_t next_free;
  129. /*
  130. * The index of next free exception in the current
  131. * metadata area.
  132. */
  133. uint32_t current_committed;
  134. atomic_t pending_count;
  135. uint32_t callback_count;
  136. struct commit_callback *callbacks;
  137. struct dm_io_client *io_client;
  138. struct workqueue_struct *metadata_wq;
  139. };
  140. static int alloc_area(struct pstore *ps)
  141. {
  142. int r = -ENOMEM;
  143. size_t len;
  144. len = ps->store->chunk_size << SECTOR_SHIFT;
  145. /*
  146. * Allocate the chunk_size block of memory that will hold
  147. * a single metadata area.
  148. */
  149. ps->area = vmalloc(len);
  150. if (!ps->area)
  151. goto err_area;
  152. ps->zero_area = vzalloc(len);
  153. if (!ps->zero_area)
  154. goto err_zero_area;
  155. ps->header_area = vmalloc(len);
  156. if (!ps->header_area)
  157. goto err_header_area;
  158. return 0;
  159. err_header_area:
  160. vfree(ps->zero_area);
  161. err_zero_area:
  162. vfree(ps->area);
  163. err_area:
  164. return r;
  165. }
  166. static void free_area(struct pstore *ps)
  167. {
  168. vfree(ps->area);
  169. ps->area = NULL;
  170. vfree(ps->zero_area);
  171. ps->zero_area = NULL;
  172. vfree(ps->header_area);
  173. ps->header_area = NULL;
  174. }
  175. struct mdata_req {
  176. struct dm_io_region *where;
  177. struct dm_io_request *io_req;
  178. struct work_struct work;
  179. int result;
  180. };
  181. static void do_metadata(struct work_struct *work)
  182. {
  183. struct mdata_req *req = container_of(work, struct mdata_req, work);
  184. req->result = dm_io(req->io_req, 1, req->where, NULL);
  185. }
  186. /*
  187. * Read or write a chunk aligned and sized block of data from a device.
  188. */
  189. static int chunk_io(struct pstore *ps, void *area, chunk_t chunk, int op,
  190. int op_flags, int metadata)
  191. {
  192. struct dm_io_region where = {
  193. .bdev = dm_snap_cow(ps->store->snap)->bdev,
  194. .sector = ps->store->chunk_size * chunk,
  195. .count = ps->store->chunk_size,
  196. };
  197. struct dm_io_request io_req = {
  198. .bi_op = op,
  199. .bi_op_flags = op_flags,
  200. .mem.type = DM_IO_VMA,
  201. .mem.ptr.vma = area,
  202. .client = ps->io_client,
  203. .notify.fn = NULL,
  204. };
  205. struct mdata_req req;
  206. if (!metadata)
  207. return dm_io(&io_req, 1, &where, NULL);
  208. req.where = &where;
  209. req.io_req = &io_req;
  210. /*
  211. * Issue the synchronous I/O from a different thread
  212. * to avoid generic_make_request recursion.
  213. */
  214. INIT_WORK_ONSTACK(&req.work, do_metadata);
  215. queue_work(ps->metadata_wq, &req.work);
  216. flush_workqueue(ps->metadata_wq);
  217. destroy_work_on_stack(&req.work);
  218. return req.result;
  219. }
  220. /*
  221. * Convert a metadata area index to a chunk index.
  222. */
  223. static chunk_t area_location(struct pstore *ps, chunk_t area)
  224. {
  225. return NUM_SNAPSHOT_HDR_CHUNKS + ((ps->exceptions_per_area + 1) * area);
  226. }
  227. static void skip_metadata(struct pstore *ps)
  228. {
  229. uint32_t stride = ps->exceptions_per_area + 1;
  230. chunk_t next_free = ps->next_free;
  231. if (sector_div(next_free, stride) == NUM_SNAPSHOT_HDR_CHUNKS)
  232. ps->next_free++;
  233. }
  234. /*
  235. * Read or write a metadata area. Remembering to skip the first
  236. * chunk which holds the header.
  237. */
  238. static int area_io(struct pstore *ps, int op, int op_flags)
  239. {
  240. int r;
  241. chunk_t chunk;
  242. chunk = area_location(ps, ps->current_area);
  243. r = chunk_io(ps, ps->area, chunk, op, op_flags, 0);
  244. if (r)
  245. return r;
  246. return 0;
  247. }
  248. static void zero_memory_area(struct pstore *ps)
  249. {
  250. memset(ps->area, 0, ps->store->chunk_size << SECTOR_SHIFT);
  251. }
  252. static int zero_disk_area(struct pstore *ps, chunk_t area)
  253. {
  254. return chunk_io(ps, ps->zero_area, area_location(ps, area),
  255. REQ_OP_WRITE, 0, 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);
  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, REQ_OP_READ, 0, 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, REQ_OP_WRITE, 0, 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 (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. vfree(ps->callbacks);
  494. kfree(ps);
  495. }
  496. static int persistent_read_metadata(struct dm_exception_store *store,
  497. int (*callback)(void *callback_context,
  498. chunk_t old, chunk_t new),
  499. void *callback_context)
  500. {
  501. int r, uninitialized_var(new_snapshot);
  502. struct pstore *ps = get_info(store);
  503. /*
  504. * Read the snapshot header.
  505. */
  506. r = read_header(ps, &new_snapshot);
  507. if (r)
  508. return r;
  509. /*
  510. * Now we know correct chunk_size, complete the initialisation.
  511. */
  512. ps->exceptions_per_area = (ps->store->chunk_size << SECTOR_SHIFT) /
  513. sizeof(struct disk_exception);
  514. ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
  515. sizeof(*ps->callbacks));
  516. if (!ps->callbacks)
  517. return -ENOMEM;
  518. /*
  519. * Do we need to setup a new snapshot ?
  520. */
  521. if (new_snapshot) {
  522. r = write_header(ps);
  523. if (r) {
  524. DMWARN("write_header failed");
  525. return r;
  526. }
  527. ps->current_area = 0;
  528. zero_memory_area(ps);
  529. r = zero_disk_area(ps, 0);
  530. if (r)
  531. DMWARN("zero_disk_area(0) failed");
  532. return r;
  533. }
  534. /*
  535. * Sanity checks.
  536. */
  537. if (ps->version != SNAPSHOT_DISK_VERSION) {
  538. DMWARN("unable to handle snapshot disk version %d",
  539. ps->version);
  540. return -EINVAL;
  541. }
  542. /*
  543. * Metadata are valid, but snapshot is invalidated
  544. */
  545. if (!ps->valid)
  546. return 1;
  547. /*
  548. * Read the metadata.
  549. */
  550. r = read_exceptions(ps, callback, callback_context);
  551. return r;
  552. }
  553. static int persistent_prepare_exception(struct dm_exception_store *store,
  554. struct dm_exception *e)
  555. {
  556. struct pstore *ps = get_info(store);
  557. sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
  558. /* Is there enough room ? */
  559. if (size < ((ps->next_free + 1) * store->chunk_size))
  560. return -ENOSPC;
  561. e->new_chunk = ps->next_free;
  562. /*
  563. * Move onto the next free pending, making sure to take
  564. * into account the location of the metadata chunks.
  565. */
  566. ps->next_free++;
  567. skip_metadata(ps);
  568. atomic_inc(&ps->pending_count);
  569. return 0;
  570. }
  571. static void persistent_commit_exception(struct dm_exception_store *store,
  572. struct dm_exception *e, int valid,
  573. void (*callback) (void *, int success),
  574. void *callback_context)
  575. {
  576. unsigned int i;
  577. struct pstore *ps = get_info(store);
  578. struct core_exception ce;
  579. struct commit_callback *cb;
  580. if (!valid)
  581. ps->valid = 0;
  582. ce.old_chunk = e->old_chunk;
  583. ce.new_chunk = e->new_chunk;
  584. write_exception(ps, ps->current_committed++, &ce);
  585. /*
  586. * Add the callback to the back of the array. This code
  587. * is the only place where the callback array is
  588. * manipulated, and we know that it will never be called
  589. * multiple times concurrently.
  590. */
  591. cb = ps->callbacks + ps->callback_count++;
  592. cb->callback = callback;
  593. cb->context = callback_context;
  594. /*
  595. * If there are exceptions in flight and we have not yet
  596. * filled this metadata area there's nothing more to do.
  597. */
  598. if (!atomic_dec_and_test(&ps->pending_count) &&
  599. (ps->current_committed != ps->exceptions_per_area))
  600. return;
  601. /*
  602. * If we completely filled the current area, then wipe the next one.
  603. */
  604. if ((ps->current_committed == ps->exceptions_per_area) &&
  605. zero_disk_area(ps, ps->current_area + 1))
  606. ps->valid = 0;
  607. /*
  608. * Commit exceptions to disk.
  609. */
  610. if (ps->valid && area_io(ps, REQ_OP_WRITE,
  611. REQ_PREFLUSH | REQ_FUA | REQ_SYNC))
  612. ps->valid = 0;
  613. /*
  614. * Advance to the next area if this one is full.
  615. */
  616. if (ps->current_committed == ps->exceptions_per_area) {
  617. ps->current_committed = 0;
  618. ps->current_area++;
  619. zero_memory_area(ps);
  620. }
  621. for (i = 0; i < ps->callback_count; i++) {
  622. cb = ps->callbacks + i;
  623. cb->callback(cb->context, ps->valid);
  624. }
  625. ps->callback_count = 0;
  626. }
  627. static int persistent_prepare_merge(struct dm_exception_store *store,
  628. chunk_t *last_old_chunk,
  629. chunk_t *last_new_chunk)
  630. {
  631. struct pstore *ps = get_info(store);
  632. struct core_exception ce;
  633. int nr_consecutive;
  634. int r;
  635. /*
  636. * When current area is empty, move back to preceding area.
  637. */
  638. if (!ps->current_committed) {
  639. /*
  640. * Have we finished?
  641. */
  642. if (!ps->current_area)
  643. return 0;
  644. ps->current_area--;
  645. r = area_io(ps, REQ_OP_READ, 0);
  646. if (r < 0)
  647. return r;
  648. ps->current_committed = ps->exceptions_per_area;
  649. }
  650. read_exception(ps, ps->area, ps->current_committed - 1, &ce);
  651. *last_old_chunk = ce.old_chunk;
  652. *last_new_chunk = ce.new_chunk;
  653. /*
  654. * Find number of consecutive chunks within the current area,
  655. * working backwards.
  656. */
  657. for (nr_consecutive = 1; nr_consecutive < ps->current_committed;
  658. nr_consecutive++) {
  659. read_exception(ps, ps->area,
  660. ps->current_committed - 1 - nr_consecutive, &ce);
  661. if (ce.old_chunk != *last_old_chunk - nr_consecutive ||
  662. ce.new_chunk != *last_new_chunk - nr_consecutive)
  663. break;
  664. }
  665. return nr_consecutive;
  666. }
  667. static int persistent_commit_merge(struct dm_exception_store *store,
  668. int nr_merged)
  669. {
  670. int r, i;
  671. struct pstore *ps = get_info(store);
  672. BUG_ON(nr_merged > ps->current_committed);
  673. for (i = 0; i < nr_merged; i++)
  674. clear_exception(ps, ps->current_committed - 1 - i);
  675. r = area_io(ps, REQ_OP_WRITE, REQ_PREFLUSH | REQ_FUA);
  676. if (r < 0)
  677. return r;
  678. ps->current_committed -= nr_merged;
  679. /*
  680. * At this stage, only persistent_usage() uses ps->next_free, so
  681. * we make no attempt to keep ps->next_free strictly accurate
  682. * as exceptions may have been committed out-of-order originally.
  683. * Once a snapshot has become merging, we set it to the value it
  684. * would have held had all the exceptions been committed in order.
  685. *
  686. * ps->current_area does not get reduced by prepare_merge() until
  687. * after commit_merge() has removed the nr_merged previous exceptions.
  688. */
  689. ps->next_free = area_location(ps, ps->current_area) +
  690. ps->current_committed + 1;
  691. return 0;
  692. }
  693. static void persistent_drop_snapshot(struct dm_exception_store *store)
  694. {
  695. struct pstore *ps = get_info(store);
  696. ps->valid = 0;
  697. if (write_header(ps))
  698. DMWARN("write header failed");
  699. }
  700. static int persistent_ctr(struct dm_exception_store *store, char *options)
  701. {
  702. struct pstore *ps;
  703. int r;
  704. /* allocate the pstore */
  705. ps = kzalloc(sizeof(*ps), GFP_KERNEL);
  706. if (!ps)
  707. return -ENOMEM;
  708. ps->store = store;
  709. ps->valid = 1;
  710. ps->version = SNAPSHOT_DISK_VERSION;
  711. ps->area = NULL;
  712. ps->zero_area = NULL;
  713. ps->header_area = NULL;
  714. ps->next_free = NUM_SNAPSHOT_HDR_CHUNKS + 1; /* header and 1st area */
  715. ps->current_committed = 0;
  716. ps->callback_count = 0;
  717. atomic_set(&ps->pending_count, 0);
  718. ps->callbacks = NULL;
  719. ps->metadata_wq = alloc_workqueue("ksnaphd", WQ_MEM_RECLAIM, 0);
  720. if (!ps->metadata_wq) {
  721. DMERR("couldn't start header metadata update thread");
  722. r = -ENOMEM;
  723. goto err_workqueue;
  724. }
  725. if (options) {
  726. char overflow = toupper(options[0]);
  727. if (overflow == 'O')
  728. store->userspace_supports_overflow = true;
  729. else {
  730. DMERR("Unsupported persistent store option: %s", options);
  731. r = -EINVAL;
  732. goto err_options;
  733. }
  734. }
  735. store->context = ps;
  736. return 0;
  737. err_options:
  738. destroy_workqueue(ps->metadata_wq);
  739. err_workqueue:
  740. kfree(ps);
  741. return r;
  742. }
  743. static unsigned persistent_status(struct dm_exception_store *store,
  744. status_type_t status, char *result,
  745. unsigned maxlen)
  746. {
  747. unsigned sz = 0;
  748. switch (status) {
  749. case STATUSTYPE_INFO:
  750. break;
  751. case STATUSTYPE_TABLE:
  752. DMEMIT(" %s %llu", store->userspace_supports_overflow ? "PO" : "P",
  753. (unsigned long long)store->chunk_size);
  754. }
  755. return sz;
  756. }
  757. static struct dm_exception_store_type _persistent_type = {
  758. .name = "persistent",
  759. .module = THIS_MODULE,
  760. .ctr = persistent_ctr,
  761. .dtr = persistent_dtr,
  762. .read_metadata = persistent_read_metadata,
  763. .prepare_exception = persistent_prepare_exception,
  764. .commit_exception = persistent_commit_exception,
  765. .prepare_merge = persistent_prepare_merge,
  766. .commit_merge = persistent_commit_merge,
  767. .drop_snapshot = persistent_drop_snapshot,
  768. .usage = persistent_usage,
  769. .status = persistent_status,
  770. };
  771. static struct dm_exception_store_type _persistent_compat_type = {
  772. .name = "P",
  773. .module = THIS_MODULE,
  774. .ctr = persistent_ctr,
  775. .dtr = persistent_dtr,
  776. .read_metadata = persistent_read_metadata,
  777. .prepare_exception = persistent_prepare_exception,
  778. .commit_exception = persistent_commit_exception,
  779. .prepare_merge = persistent_prepare_merge,
  780. .commit_merge = persistent_commit_merge,
  781. .drop_snapshot = persistent_drop_snapshot,
  782. .usage = persistent_usage,
  783. .status = persistent_status,
  784. };
  785. int dm_persistent_snapshot_init(void)
  786. {
  787. int r;
  788. r = dm_exception_store_type_register(&_persistent_type);
  789. if (r) {
  790. DMERR("Unable to register persistent exception store type");
  791. return r;
  792. }
  793. r = dm_exception_store_type_register(&_persistent_compat_type);
  794. if (r) {
  795. DMERR("Unable to register old-style persistent exception "
  796. "store type");
  797. dm_exception_store_type_unregister(&_persistent_type);
  798. return r;
  799. }
  800. return r;
  801. }
  802. void dm_persistent_snapshot_exit(void)
  803. {
  804. dm_exception_store_type_unregister(&_persistent_type);
  805. dm_exception_store_type_unregister(&_persistent_compat_type);
  806. }