dm-snap-persistent.c 22 KB

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