dm-snap-persistent.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
  4. * Copyright (C) 2006-2008 Red Hat GmbH
  5. *
  6. * This file is released under the GPL.
  7. */
  8. #include "dm-exception-store.h"
  9. #include <linux/ctype.h>
  10. #include <linux/mm.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/export.h>
  14. #include <linux/slab.h>
  15. #include <linux/dm-io.h>
  16. #include <linux/dm-bufio.h>
  17. #define DM_MSG_PREFIX "persistent snapshot"
  18. #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32U /* 16KB */
  19. #define DM_PREFETCH_CHUNKS 12
  20. /*
  21. *---------------------------------------------------------------
  22. * Persistent snapshots, by persistent we mean that the snapshot
  23. * will survive a reboot.
  24. *---------------------------------------------------------------
  25. */
  26. /*
  27. * We need to store a record of which parts of the origin have
  28. * been copied to the snapshot device. The snapshot code
  29. * requires that we copy exception chunks to chunk aligned areas
  30. * of the COW store. It makes sense therefore, to store the
  31. * metadata in chunk size blocks.
  32. *
  33. * There is no backward or forward compatibility implemented,
  34. * snapshots with different disk versions than the kernel will
  35. * not be usable. It is expected that "lvcreate" will blank out
  36. * the start of a fresh COW device before calling the snapshot
  37. * constructor.
  38. *
  39. * The first chunk of the COW device just contains the header.
  40. * After this there is a chunk filled with exception metadata,
  41. * followed by as many exception chunks as can fit in the
  42. * metadata areas.
  43. *
  44. * All on disk structures are in little-endian format. The end
  45. * of the exceptions info is indicated by an exception with a
  46. * new_chunk of 0, which is invalid since it would point to the
  47. * header chunk.
  48. */
  49. /*
  50. * Magic for persistent snapshots: "SnAp" - Feeble isn't it.
  51. */
  52. #define SNAP_MAGIC 0x70416e53
  53. /*
  54. * The on-disk version of the metadata.
  55. */
  56. #define SNAPSHOT_DISK_VERSION 1
  57. #define NUM_SNAPSHOT_HDR_CHUNKS 1
  58. struct disk_header {
  59. __le32 magic;
  60. /*
  61. * Is this snapshot valid. There is no way of recovering
  62. * an invalid snapshot.
  63. */
  64. __le32 valid;
  65. /*
  66. * Simple, incrementing version. no backward
  67. * compatibility.
  68. */
  69. __le32 version;
  70. /* In sectors */
  71. __le32 chunk_size;
  72. } __packed;
  73. struct disk_exception {
  74. __le64 old_chunk;
  75. __le64 new_chunk;
  76. } __packed;
  77. struct core_exception {
  78. uint64_t old_chunk;
  79. uint64_t new_chunk;
  80. };
  81. struct commit_callback {
  82. void (*callback)(void *ref, int success);
  83. void *context;
  84. };
  85. /*
  86. * The top level structure for a persistent exception store.
  87. */
  88. struct pstore {
  89. struct dm_exception_store *store;
  90. int version;
  91. int valid;
  92. uint32_t exceptions_per_area;
  93. /*
  94. * Now that we have an asynchronous kcopyd there is no
  95. * need for large chunk sizes, so it wont hurt to have a
  96. * whole chunks worth of metadata in memory at once.
  97. */
  98. void *area;
  99. /*
  100. * An area of zeros used to clear the next area.
  101. */
  102. void *zero_area;
  103. /*
  104. * An area used for header. The header can be written
  105. * concurrently with metadata (when invalidating the snapshot),
  106. * so it needs a separate buffer.
  107. */
  108. void *header_area;
  109. /*
  110. * Used to keep track of which metadata area the data in
  111. * 'chunk' refers to.
  112. */
  113. chunk_t current_area;
  114. /*
  115. * The next free chunk for an exception.
  116. *
  117. * When creating exceptions, all the chunks here and above are
  118. * free. It holds the next chunk to be allocated. On rare
  119. * occasions (e.g. after a system crash) holes can be left in
  120. * the exception store because chunks can be committed out of
  121. * order.
  122. *
  123. * When merging exceptions, it does not necessarily mean all the
  124. * chunks here and above are free. It holds the value it would
  125. * have held if all chunks had been committed in order of
  126. * allocation. Consequently the value may occasionally be
  127. * slightly too low, but since it's only used for 'status' and
  128. * it can never reach its minimum value too early this doesn't
  129. * matter.
  130. */
  131. chunk_t next_free;
  132. /*
  133. * The index of next free exception in the current
  134. * metadata area.
  135. */
  136. uint32_t current_committed;
  137. atomic_t pending_count;
  138. uint32_t callback_count;
  139. struct commit_callback *callbacks;
  140. struct dm_io_client *io_client;
  141. struct workqueue_struct *metadata_wq;
  142. };
  143. static int alloc_area(struct pstore *ps)
  144. {
  145. int r = -ENOMEM;
  146. size_t len;
  147. len = ps->store->chunk_size << SECTOR_SHIFT;
  148. /*
  149. * Allocate the chunk_size block of memory that will hold
  150. * a single metadata area.
  151. */
  152. ps->area = vmalloc(len);
  153. if (!ps->area)
  154. goto err_area;
  155. ps->zero_area = vzalloc(len);
  156. if (!ps->zero_area)
  157. goto err_zero_area;
  158. ps->header_area = vmalloc(len);
  159. if (!ps->header_area)
  160. goto err_header_area;
  161. return 0;
  162. err_header_area:
  163. vfree(ps->zero_area);
  164. err_zero_area:
  165. vfree(ps->area);
  166. err_area:
  167. return r;
  168. }
  169. static void free_area(struct pstore *ps)
  170. {
  171. vfree(ps->area);
  172. ps->area = NULL;
  173. vfree(ps->zero_area);
  174. ps->zero_area = NULL;
  175. vfree(ps->header_area);
  176. ps->header_area = NULL;
  177. }
  178. struct mdata_req {
  179. struct dm_io_region *where;
  180. struct dm_io_request *io_req;
  181. struct work_struct work;
  182. int result;
  183. };
  184. static void do_metadata(struct work_struct *work)
  185. {
  186. struct mdata_req *req = container_of(work, struct mdata_req, work);
  187. req->result = dm_io(req->io_req, 1, req->where, NULL, IOPRIO_DEFAULT);
  188. }
  189. /*
  190. * Read or write a chunk aligned and sized block of data from a device.
  191. */
  192. static int chunk_io(struct pstore *ps, void *area, chunk_t chunk, blk_opf_t opf,
  193. int metadata)
  194. {
  195. struct dm_io_region where = {
  196. .bdev = dm_snap_cow(ps->store->snap)->bdev,
  197. .sector = ps->store->chunk_size * chunk,
  198. .count = ps->store->chunk_size,
  199. };
  200. struct dm_io_request io_req = {
  201. .bi_opf = opf,
  202. .mem.type = DM_IO_VMA,
  203. .mem.ptr.vma = area,
  204. .client = ps->io_client,
  205. .notify.fn = NULL,
  206. };
  207. struct mdata_req req;
  208. if (!metadata)
  209. return dm_io(&io_req, 1, &where, NULL, IOPRIO_DEFAULT);
  210. req.where = &where;
  211. req.io_req = &io_req;
  212. /*
  213. * Issue the synchronous I/O from a different thread
  214. * to avoid submit_bio_noacct recursion.
  215. */
  216. INIT_WORK_ONSTACK(&req.work, do_metadata);
  217. queue_work(ps->metadata_wq, &req.work);
  218. flush_workqueue(ps->metadata_wq);
  219. destroy_work_on_stack(&req.work);
  220. return req.result;
  221. }
  222. /*
  223. * Convert a metadata area index to a chunk index.
  224. */
  225. static chunk_t area_location(struct pstore *ps, chunk_t area)
  226. {
  227. return NUM_SNAPSHOT_HDR_CHUNKS + ((ps->exceptions_per_area + 1) * area);
  228. }
  229. static void skip_metadata(struct pstore *ps)
  230. {
  231. uint32_t stride = ps->exceptions_per_area + 1;
  232. chunk_t next_free = ps->next_free;
  233. if (sector_div(next_free, stride) == NUM_SNAPSHOT_HDR_CHUNKS)
  234. ps->next_free++;
  235. }
  236. /*
  237. * Read or write a metadata area. Remembering to skip the first
  238. * chunk which holds the header.
  239. */
  240. static int area_io(struct pstore *ps, blk_opf_t opf)
  241. {
  242. chunk_t chunk = area_location(ps, ps->current_area);
  243. return chunk_io(ps, ps->area, chunk, opf, 0);
  244. }
  245. static void zero_memory_area(struct pstore *ps)
  246. {
  247. memset(ps->area, 0, ps->store->chunk_size << SECTOR_SHIFT);
  248. }
  249. static int zero_disk_area(struct pstore *ps, chunk_t area)
  250. {
  251. return chunk_io(ps, ps->zero_area, area_location(ps, area),
  252. REQ_OP_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 int 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);
  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, REQ_OP_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 table chunk size of %u.",
  300. chunk_size, ps->store->chunk_size);
  301. /* We had a bogus chunk_size. Fix stuff up. */
  302. free_area(ps);
  303. r = dm_exception_store_set_chunk_size(ps->store, chunk_size,
  304. &chunk_err);
  305. if (r) {
  306. DMERR("invalid on-disk chunk size %u: %s.",
  307. chunk_size, chunk_err);
  308. return r;
  309. }
  310. r = alloc_area(ps);
  311. return r;
  312. bad:
  313. free_area(ps);
  314. return r;
  315. }
  316. static int write_header(struct pstore *ps)
  317. {
  318. struct disk_header *dh;
  319. memset(ps->header_area, 0, ps->store->chunk_size << SECTOR_SHIFT);
  320. dh = ps->header_area;
  321. dh->magic = cpu_to_le32(SNAP_MAGIC);
  322. dh->valid = cpu_to_le32(ps->valid);
  323. dh->version = cpu_to_le32(ps->version);
  324. dh->chunk_size = cpu_to_le32(ps->store->chunk_size);
  325. return chunk_io(ps, ps->header_area, 0, REQ_OP_WRITE, 1);
  326. }
  327. /*
  328. * Access functions for the disk exceptions, these do the endian conversions.
  329. */
  330. static struct disk_exception *get_exception(struct pstore *ps, void *ps_area,
  331. uint32_t index)
  332. {
  333. BUG_ON(index >= ps->exceptions_per_area);
  334. return ((struct disk_exception *) ps_area) + index;
  335. }
  336. static void read_exception(struct pstore *ps, void *ps_area,
  337. uint32_t index, struct core_exception *result)
  338. {
  339. struct disk_exception *de = get_exception(ps, ps_area, index);
  340. /* copy it */
  341. result->old_chunk = le64_to_cpu(de->old_chunk);
  342. result->new_chunk = le64_to_cpu(de->new_chunk);
  343. }
  344. static void write_exception(struct pstore *ps,
  345. uint32_t index, struct core_exception *e)
  346. {
  347. struct disk_exception *de = get_exception(ps, ps->area, index);
  348. /* copy it */
  349. de->old_chunk = cpu_to_le64(e->old_chunk);
  350. de->new_chunk = cpu_to_le64(e->new_chunk);
  351. }
  352. static void clear_exception(struct pstore *ps, uint32_t index)
  353. {
  354. struct disk_exception *de = get_exception(ps, ps->area, index);
  355. /* clear it */
  356. de->old_chunk = 0;
  357. de->new_chunk = 0;
  358. }
  359. /*
  360. * Registers the exceptions that are present in the current area.
  361. * 'full' is filled in to indicate if the area has been
  362. * filled.
  363. */
  364. static int insert_exceptions(struct pstore *ps, void *ps_area,
  365. int (*callback)(void *callback_context,
  366. chunk_t old, chunk_t new),
  367. void *callback_context,
  368. int *full)
  369. {
  370. int r;
  371. unsigned int i;
  372. struct core_exception e;
  373. /* presume the area is full */
  374. *full = 1;
  375. for (i = 0; i < ps->exceptions_per_area; i++) {
  376. read_exception(ps, ps_area, i, &e);
  377. /*
  378. * If the new_chunk is pointing at the start of
  379. * the COW device, where the first metadata area
  380. * is we know that we've hit the end of the
  381. * exceptions. Therefore the area is not full.
  382. */
  383. if (e.new_chunk == 0LL) {
  384. ps->current_committed = i;
  385. *full = 0;
  386. break;
  387. }
  388. /*
  389. * Keep track of the start of the free chunks.
  390. */
  391. if (ps->next_free <= e.new_chunk)
  392. ps->next_free = e.new_chunk + 1;
  393. /*
  394. * Otherwise we add the exception to the snapshot.
  395. */
  396. r = callback(callback_context, e.old_chunk, e.new_chunk);
  397. if (r)
  398. return r;
  399. }
  400. return 0;
  401. }
  402. static int read_exceptions(struct pstore *ps,
  403. int (*callback)(void *callback_context, chunk_t old,
  404. chunk_t new),
  405. void *callback_context)
  406. {
  407. int r, full = 1;
  408. struct dm_bufio_client *client;
  409. chunk_t prefetch_area = 0;
  410. client = dm_bufio_client_create(dm_snap_cow(ps->store->snap)->bdev,
  411. ps->store->chunk_size << SECTOR_SHIFT,
  412. 1, 0, NULL, NULL, 0);
  413. if (IS_ERR(client))
  414. return PTR_ERR(client);
  415. /*
  416. * Setup for one current buffer + desired readahead buffers.
  417. */
  418. dm_bufio_set_minimum_buffers(client, 1 + DM_PREFETCH_CHUNKS);
  419. /*
  420. * Keeping reading chunks and inserting exceptions until
  421. * we find a partially full area.
  422. */
  423. for (ps->current_area = 0; full; ps->current_area++) {
  424. struct dm_buffer *bp;
  425. void *area;
  426. chunk_t chunk;
  427. if (unlikely(prefetch_area < ps->current_area))
  428. prefetch_area = ps->current_area;
  429. if (DM_PREFETCH_CHUNKS) {
  430. 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. }
  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 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. kvfree(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, 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 = kvcalloc(ps->exceptions_per_area,
  513. sizeof(*ps->callbacks), GFP_KERNEL);
  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, REQ_OP_WRITE | REQ_PREFLUSH | REQ_FUA |
  609. REQ_SYNC))
  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, REQ_OP_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, REQ_OP_WRITE | REQ_PREFLUSH | REQ_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, char *options)
  699. {
  700. struct pstore *ps;
  701. int r;
  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. DMERR("couldn't start header metadata update thread");
  720. r = -ENOMEM;
  721. goto err_workqueue;
  722. }
  723. if (options) {
  724. char overflow = toupper(options[0]);
  725. if (overflow == 'O')
  726. store->userspace_supports_overflow = true;
  727. else {
  728. DMERR("Unsupported persistent store option: %s", options);
  729. r = -EINVAL;
  730. goto err_options;
  731. }
  732. }
  733. store->context = ps;
  734. return 0;
  735. err_options:
  736. destroy_workqueue(ps->metadata_wq);
  737. err_workqueue:
  738. kfree(ps);
  739. return r;
  740. }
  741. static unsigned int persistent_status(struct dm_exception_store *store,
  742. status_type_t status, char *result,
  743. unsigned int maxlen)
  744. {
  745. unsigned int sz = 0;
  746. switch (status) {
  747. case STATUSTYPE_INFO:
  748. break;
  749. case STATUSTYPE_TABLE:
  750. DMEMIT(" %s %llu", store->userspace_supports_overflow ? "PO" : "P",
  751. (unsigned long long)store->chunk_size);
  752. break;
  753. case STATUSTYPE_IMA:
  754. *result = '\0';
  755. break;
  756. }
  757. return sz;
  758. }
  759. static struct dm_exception_store_type _persistent_type = {
  760. .name = "persistent",
  761. .module = THIS_MODULE,
  762. .ctr = persistent_ctr,
  763. .dtr = persistent_dtr,
  764. .read_metadata = persistent_read_metadata,
  765. .prepare_exception = persistent_prepare_exception,
  766. .commit_exception = persistent_commit_exception,
  767. .prepare_merge = persistent_prepare_merge,
  768. .commit_merge = persistent_commit_merge,
  769. .drop_snapshot = persistent_drop_snapshot,
  770. .usage = persistent_usage,
  771. .status = persistent_status,
  772. };
  773. static struct dm_exception_store_type _persistent_compat_type = {
  774. .name = "P",
  775. .module = THIS_MODULE,
  776. .ctr = persistent_ctr,
  777. .dtr = persistent_dtr,
  778. .read_metadata = persistent_read_metadata,
  779. .prepare_exception = persistent_prepare_exception,
  780. .commit_exception = persistent_commit_exception,
  781. .prepare_merge = persistent_prepare_merge,
  782. .commit_merge = persistent_commit_merge,
  783. .drop_snapshot = persistent_drop_snapshot,
  784. .usage = persistent_usage,
  785. .status = persistent_status,
  786. };
  787. int dm_persistent_snapshot_init(void)
  788. {
  789. int r;
  790. r = dm_exception_store_type_register(&_persistent_type);
  791. if (r) {
  792. DMERR("Unable to register persistent exception store type");
  793. return r;
  794. }
  795. r = dm_exception_store_type_register(&_persistent_compat_type);
  796. if (r) {
  797. DMERR("Unable to register old-style persistent exception store type");
  798. dm_exception_store_type_unregister(&_persistent_type);
  799. return r;
  800. }
  801. return r;
  802. }
  803. void dm_persistent_snapshot_exit(void)
  804. {
  805. dm_exception_store_type_unregister(&_persistent_type);
  806. dm_exception_store_type_unregister(&_persistent_compat_type);
  807. }