dm-snap-persistent.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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 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, WRITE_FLUSH_FUA))
  611. ps->valid = 0;
  612. /*
  613. * Advance to the next area if this one is full.
  614. */
  615. if (ps->current_committed == ps->exceptions_per_area) {
  616. ps->current_committed = 0;
  617. ps->current_area++;
  618. zero_memory_area(ps);
  619. }
  620. for (i = 0; i < ps->callback_count; i++) {
  621. cb = ps->callbacks + i;
  622. cb->callback(cb->context, ps->valid);
  623. }
  624. ps->callback_count = 0;
  625. }
  626. static int persistent_prepare_merge(struct dm_exception_store *store,
  627. chunk_t *last_old_chunk,
  628. chunk_t *last_new_chunk)
  629. {
  630. struct pstore *ps = get_info(store);
  631. struct core_exception ce;
  632. int nr_consecutive;
  633. int r;
  634. /*
  635. * When current area is empty, move back to preceding area.
  636. */
  637. if (!ps->current_committed) {
  638. /*
  639. * Have we finished?
  640. */
  641. if (!ps->current_area)
  642. return 0;
  643. ps->current_area--;
  644. r = area_io(ps, REQ_OP_READ, 0);
  645. if (r < 0)
  646. return r;
  647. ps->current_committed = ps->exceptions_per_area;
  648. }
  649. read_exception(ps, ps->area, ps->current_committed - 1, &ce);
  650. *last_old_chunk = ce.old_chunk;
  651. *last_new_chunk = ce.new_chunk;
  652. /*
  653. * Find number of consecutive chunks within the current area,
  654. * working backwards.
  655. */
  656. for (nr_consecutive = 1; nr_consecutive < ps->current_committed;
  657. nr_consecutive++) {
  658. read_exception(ps, ps->area,
  659. ps->current_committed - 1 - nr_consecutive, &ce);
  660. if (ce.old_chunk != *last_old_chunk - nr_consecutive ||
  661. ce.new_chunk != *last_new_chunk - nr_consecutive)
  662. break;
  663. }
  664. return nr_consecutive;
  665. }
  666. static int persistent_commit_merge(struct dm_exception_store *store,
  667. int nr_merged)
  668. {
  669. int r, i;
  670. struct pstore *ps = get_info(store);
  671. BUG_ON(nr_merged > ps->current_committed);
  672. for (i = 0; i < nr_merged; i++)
  673. clear_exception(ps, ps->current_committed - 1 - i);
  674. r = area_io(ps, REQ_OP_WRITE, WRITE_FLUSH_FUA);
  675. if (r < 0)
  676. return r;
  677. ps->current_committed -= nr_merged;
  678. /*
  679. * At this stage, only persistent_usage() uses ps->next_free, so
  680. * we make no attempt to keep ps->next_free strictly accurate
  681. * as exceptions may have been committed out-of-order originally.
  682. * Once a snapshot has become merging, we set it to the value it
  683. * would have held had all the exceptions been committed in order.
  684. *
  685. * ps->current_area does not get reduced by prepare_merge() until
  686. * after commit_merge() has removed the nr_merged previous exceptions.
  687. */
  688. ps->next_free = area_location(ps, ps->current_area) +
  689. ps->current_committed + 1;
  690. return 0;
  691. }
  692. static void persistent_drop_snapshot(struct dm_exception_store *store)
  693. {
  694. struct pstore *ps = get_info(store);
  695. ps->valid = 0;
  696. if (write_header(ps))
  697. DMWARN("write header failed");
  698. }
  699. static int persistent_ctr(struct dm_exception_store *store, char *options)
  700. {
  701. struct pstore *ps;
  702. int r;
  703. /* allocate the pstore */
  704. ps = kzalloc(sizeof(*ps), GFP_KERNEL);
  705. if (!ps)
  706. return -ENOMEM;
  707. ps->store = store;
  708. ps->valid = 1;
  709. ps->version = SNAPSHOT_DISK_VERSION;
  710. ps->area = NULL;
  711. ps->zero_area = NULL;
  712. ps->header_area = NULL;
  713. ps->next_free = NUM_SNAPSHOT_HDR_CHUNKS + 1; /* header and 1st area */
  714. ps->current_committed = 0;
  715. ps->callback_count = 0;
  716. atomic_set(&ps->pending_count, 0);
  717. ps->callbacks = NULL;
  718. ps->metadata_wq = alloc_workqueue("ksnaphd", WQ_MEM_RECLAIM, 0);
  719. if (!ps->metadata_wq) {
  720. DMERR("couldn't start header metadata update thread");
  721. r = -ENOMEM;
  722. goto err_workqueue;
  723. }
  724. if (options) {
  725. char overflow = toupper(options[0]);
  726. if (overflow == 'O')
  727. store->userspace_supports_overflow = true;
  728. else {
  729. DMERR("Unsupported persistent store option: %s", options);
  730. r = -EINVAL;
  731. goto err_options;
  732. }
  733. }
  734. store->context = ps;
  735. return 0;
  736. err_options:
  737. destroy_workqueue(ps->metadata_wq);
  738. err_workqueue:
  739. kfree(ps);
  740. return r;
  741. }
  742. static unsigned persistent_status(struct dm_exception_store *store,
  743. status_type_t status, char *result,
  744. unsigned maxlen)
  745. {
  746. unsigned sz = 0;
  747. switch (status) {
  748. case STATUSTYPE_INFO:
  749. break;
  750. case STATUSTYPE_TABLE:
  751. DMEMIT(" %s %llu", store->userspace_supports_overflow ? "PO" : "P",
  752. (unsigned long long)store->chunk_size);
  753. }
  754. return sz;
  755. }
  756. static struct dm_exception_store_type _persistent_type = {
  757. .name = "persistent",
  758. .module = THIS_MODULE,
  759. .ctr = persistent_ctr,
  760. .dtr = persistent_dtr,
  761. .read_metadata = persistent_read_metadata,
  762. .prepare_exception = persistent_prepare_exception,
  763. .commit_exception = persistent_commit_exception,
  764. .prepare_merge = persistent_prepare_merge,
  765. .commit_merge = persistent_commit_merge,
  766. .drop_snapshot = persistent_drop_snapshot,
  767. .usage = persistent_usage,
  768. .status = persistent_status,
  769. };
  770. static struct dm_exception_store_type _persistent_compat_type = {
  771. .name = "P",
  772. .module = THIS_MODULE,
  773. .ctr = persistent_ctr,
  774. .dtr = persistent_dtr,
  775. .read_metadata = persistent_read_metadata,
  776. .prepare_exception = persistent_prepare_exception,
  777. .commit_exception = persistent_commit_exception,
  778. .prepare_merge = persistent_prepare_merge,
  779. .commit_merge = persistent_commit_merge,
  780. .drop_snapshot = persistent_drop_snapshot,
  781. .usage = persistent_usage,
  782. .status = persistent_status,
  783. };
  784. int dm_persistent_snapshot_init(void)
  785. {
  786. int r;
  787. r = dm_exception_store_type_register(&_persistent_type);
  788. if (r) {
  789. DMERR("Unable to register persistent exception store type");
  790. return r;
  791. }
  792. r = dm_exception_store_type_register(&_persistent_compat_type);
  793. if (r) {
  794. DMERR("Unable to register old-style persistent exception "
  795. "store type");
  796. dm_exception_store_type_unregister(&_persistent_type);
  797. return r;
  798. }
  799. return r;
  800. }
  801. void dm_persistent_snapshot_exit(void)
  802. {
  803. dm_exception_store_type_unregister(&_persistent_type);
  804. dm_exception_store_type_unregister(&_persistent_compat_type);
  805. }