dm-snap-persistent.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*
  2. * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
  3. * Copyright (C) 2006-2008 Red Hat GmbH
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include "dm-exception-store.h"
  8. #include <linux/mm.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/slab.h>
  12. #include <linux/dm-io.h>
  13. #define DM_MSG_PREFIX "persistent snapshot"
  14. #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32 /* 16KB */
  15. /*-----------------------------------------------------------------
  16. * Persistent snapshots, by persistent we mean that the snapshot
  17. * will survive a reboot.
  18. *---------------------------------------------------------------*/
  19. /*
  20. * We need to store a record of which parts of the origin have
  21. * been copied to the snapshot device. The snapshot code
  22. * requires that we copy exception chunks to chunk aligned areas
  23. * of the COW store. It makes sense therefore, to store the
  24. * metadata in chunk size blocks.
  25. *
  26. * There is no backward or forward compatibility implemented,
  27. * snapshots with different disk versions than the kernel will
  28. * not be usable. It is expected that "lvcreate" will blank out
  29. * the start of a fresh COW device before calling the snapshot
  30. * constructor.
  31. *
  32. * The first chunk of the COW device just contains the header.
  33. * After this there is a chunk filled with exception metadata,
  34. * followed by as many exception chunks as can fit in the
  35. * metadata areas.
  36. *
  37. * All on disk structures are in little-endian format. The end
  38. * of the exceptions info is indicated by an exception with a
  39. * new_chunk of 0, which is invalid since it would point to the
  40. * header chunk.
  41. */
  42. /*
  43. * Magic for persistent snapshots: "SnAp" - Feeble isn't it.
  44. */
  45. #define SNAP_MAGIC 0x70416e53
  46. /*
  47. * The on-disk version of the metadata.
  48. */
  49. #define SNAPSHOT_DISK_VERSION 1
  50. #define NUM_SNAPSHOT_HDR_CHUNKS 1
  51. struct disk_header {
  52. __le32 magic;
  53. /*
  54. * Is this snapshot valid. There is no way of recovering
  55. * an invalid snapshot.
  56. */
  57. __le32 valid;
  58. /*
  59. * Simple, incrementing version. no backward
  60. * compatibility.
  61. */
  62. __le32 version;
  63. /* In sectors */
  64. __le32 chunk_size;
  65. } __packed;
  66. struct disk_exception {
  67. __le64 old_chunk;
  68. __le64 new_chunk;
  69. } __packed;
  70. struct core_exception {
  71. uint64_t old_chunk;
  72. uint64_t new_chunk;
  73. };
  74. struct commit_callback {
  75. void (*callback)(void *, int success);
  76. void *context;
  77. };
  78. /*
  79. * The top level structure for a persistent exception store.
  80. */
  81. struct pstore {
  82. struct dm_exception_store *store;
  83. int version;
  84. int valid;
  85. uint32_t exceptions_per_area;
  86. /*
  87. * Now that we have an asynchronous kcopyd there is no
  88. * need for large chunk sizes, so it wont hurt to have a
  89. * whole chunks worth of metadata in memory at once.
  90. */
  91. void *area;
  92. /*
  93. * An area of zeros used to clear the next area.
  94. */
  95. void *zero_area;
  96. /*
  97. * An area used for header. The header can be written
  98. * concurrently with metadata (when invalidating the snapshot),
  99. * so it needs a separate buffer.
  100. */
  101. void *header_area;
  102. /*
  103. * Used to keep track of which metadata area the data in
  104. * 'chunk' refers to.
  105. */
  106. chunk_t current_area;
  107. /*
  108. * The next free chunk for an exception.
  109. *
  110. * When creating exceptions, all the chunks here and above are
  111. * free. It holds the next chunk to be allocated. On rare
  112. * occasions (e.g. after a system crash) holes can be left in
  113. * the exception store because chunks can be committed out of
  114. * order.
  115. *
  116. * When merging exceptions, it does not necessarily mean all the
  117. * chunks here and above are free. It holds the value it would
  118. * have held if all chunks had been committed in order of
  119. * allocation. Consequently the value may occasionally be
  120. * slightly too low, but since it's only used for 'status' and
  121. * it can never reach its minimum value too early this doesn't
  122. * matter.
  123. */
  124. chunk_t next_free;
  125. /*
  126. * The index of next free exception in the current
  127. * metadata area.
  128. */
  129. uint32_t current_committed;
  130. atomic_t pending_count;
  131. uint32_t callback_count;
  132. struct commit_callback *callbacks;
  133. struct dm_io_client *io_client;
  134. struct workqueue_struct *metadata_wq;
  135. };
  136. static int alloc_area(struct pstore *ps)
  137. {
  138. int r = -ENOMEM;
  139. size_t len;
  140. len = ps->store->chunk_size << SECTOR_SHIFT;
  141. /*
  142. * Allocate the chunk_size block of memory that will hold
  143. * a single metadata area.
  144. */
  145. ps->area = vmalloc(len);
  146. if (!ps->area)
  147. goto err_area;
  148. ps->zero_area = vzalloc(len);
  149. if (!ps->zero_area)
  150. goto err_zero_area;
  151. ps->header_area = vmalloc(len);
  152. if (!ps->header_area)
  153. goto err_header_area;
  154. return 0;
  155. err_header_area:
  156. vfree(ps->zero_area);
  157. err_zero_area:
  158. vfree(ps->area);
  159. err_area:
  160. return r;
  161. }
  162. static void free_area(struct pstore *ps)
  163. {
  164. if (ps->area)
  165. vfree(ps->area);
  166. ps->area = NULL;
  167. if (ps->zero_area)
  168. vfree(ps->zero_area);
  169. ps->zero_area = NULL;
  170. if (ps->header_area)
  171. vfree(ps->header_area);
  172. ps->header_area = NULL;
  173. }
  174. struct mdata_req {
  175. struct dm_io_region *where;
  176. struct dm_io_request *io_req;
  177. struct work_struct work;
  178. int result;
  179. };
  180. static void do_metadata(struct work_struct *work)
  181. {
  182. struct mdata_req *req = container_of(work, struct mdata_req, work);
  183. req->result = dm_io(req->io_req, 1, req->where, NULL);
  184. }
  185. /*
  186. * Read or write a chunk aligned and sized block of data from a device.
  187. */
  188. static int chunk_io(struct pstore *ps, void *area, chunk_t chunk, int rw,
  189. int metadata)
  190. {
  191. struct dm_io_region where = {
  192. .bdev = dm_snap_cow(ps->store->snap)->bdev,
  193. .sector = ps->store->chunk_size * chunk,
  194. .count = ps->store->chunk_size,
  195. };
  196. struct dm_io_request io_req = {
  197. .bi_rw = rw,
  198. .mem.type = DM_IO_VMA,
  199. .mem.ptr.vma = area,
  200. .client = ps->io_client,
  201. .notify.fn = NULL,
  202. };
  203. struct mdata_req req;
  204. if (!metadata)
  205. return dm_io(&io_req, 1, &where, NULL);
  206. req.where = &where;
  207. req.io_req = &io_req;
  208. /*
  209. * Issue the synchronous I/O from a different thread
  210. * to avoid generic_make_request recursion.
  211. */
  212. INIT_WORK_ONSTACK(&req.work, do_metadata);
  213. queue_work(ps->metadata_wq, &req.work);
  214. flush_work(&req.work);
  215. return req.result;
  216. }
  217. /*
  218. * Convert a metadata area index to a chunk index.
  219. */
  220. static chunk_t area_location(struct pstore *ps, chunk_t area)
  221. {
  222. return NUM_SNAPSHOT_HDR_CHUNKS + ((ps->exceptions_per_area + 1) * area);
  223. }
  224. /*
  225. * Read or write a metadata area. Remembering to skip the first
  226. * chunk which holds the header.
  227. */
  228. static int area_io(struct pstore *ps, int rw)
  229. {
  230. int r;
  231. chunk_t chunk;
  232. chunk = area_location(ps, ps->current_area);
  233. r = chunk_io(ps, ps->area, chunk, rw, 0);
  234. if (r)
  235. return r;
  236. return 0;
  237. }
  238. static void zero_memory_area(struct pstore *ps)
  239. {
  240. memset(ps->area, 0, ps->store->chunk_size << SECTOR_SHIFT);
  241. }
  242. static int zero_disk_area(struct pstore *ps, chunk_t area)
  243. {
  244. return chunk_io(ps, ps->zero_area, area_location(ps, area), WRITE, 0);
  245. }
  246. static int read_header(struct pstore *ps, int *new_snapshot)
  247. {
  248. int r;
  249. struct disk_header *dh;
  250. unsigned chunk_size;
  251. int chunk_size_supplied = 1;
  252. char *chunk_err;
  253. /*
  254. * Use default chunk size (or logical_block_size, if larger)
  255. * if none supplied
  256. */
  257. if (!ps->store->chunk_size) {
  258. ps->store->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS,
  259. bdev_logical_block_size(dm_snap_cow(ps->store->snap)->
  260. bdev) >> 9);
  261. ps->store->chunk_mask = ps->store->chunk_size - 1;
  262. ps->store->chunk_shift = ffs(ps->store->chunk_size) - 1;
  263. chunk_size_supplied = 0;
  264. }
  265. ps->io_client = dm_io_client_create();
  266. if (IS_ERR(ps->io_client))
  267. return PTR_ERR(ps->io_client);
  268. r = alloc_area(ps);
  269. if (r)
  270. return r;
  271. r = chunk_io(ps, ps->header_area, 0, READ, 1);
  272. if (r)
  273. goto bad;
  274. dh = ps->header_area;
  275. if (le32_to_cpu(dh->magic) == 0) {
  276. *new_snapshot = 1;
  277. return 0;
  278. }
  279. if (le32_to_cpu(dh->magic) != SNAP_MAGIC) {
  280. DMWARN("Invalid or corrupt snapshot");
  281. r = -ENXIO;
  282. goto bad;
  283. }
  284. *new_snapshot = 0;
  285. ps->valid = le32_to_cpu(dh->valid);
  286. ps->version = le32_to_cpu(dh->version);
  287. chunk_size = le32_to_cpu(dh->chunk_size);
  288. if (ps->store->chunk_size == chunk_size)
  289. return 0;
  290. if (chunk_size_supplied)
  291. DMWARN("chunk size %u in device metadata overrides "
  292. "table chunk size of %u.",
  293. chunk_size, ps->store->chunk_size);
  294. /* We had a bogus chunk_size. Fix stuff up. */
  295. free_area(ps);
  296. r = dm_exception_store_set_chunk_size(ps->store, chunk_size,
  297. &chunk_err);
  298. if (r) {
  299. DMERR("invalid on-disk chunk size %u: %s.",
  300. chunk_size, chunk_err);
  301. return r;
  302. }
  303. r = alloc_area(ps);
  304. return r;
  305. bad:
  306. free_area(ps);
  307. return r;
  308. }
  309. static int write_header(struct pstore *ps)
  310. {
  311. struct disk_header *dh;
  312. memset(ps->header_area, 0, ps->store->chunk_size << SECTOR_SHIFT);
  313. dh = ps->header_area;
  314. dh->magic = cpu_to_le32(SNAP_MAGIC);
  315. dh->valid = cpu_to_le32(ps->valid);
  316. dh->version = cpu_to_le32(ps->version);
  317. dh->chunk_size = cpu_to_le32(ps->store->chunk_size);
  318. return chunk_io(ps, ps->header_area, 0, WRITE, 1);
  319. }
  320. /*
  321. * Access functions for the disk exceptions, these do the endian conversions.
  322. */
  323. static struct disk_exception *get_exception(struct pstore *ps, uint32_t index)
  324. {
  325. BUG_ON(index >= ps->exceptions_per_area);
  326. return ((struct disk_exception *) ps->area) + index;
  327. }
  328. static void read_exception(struct pstore *ps,
  329. uint32_t index, struct core_exception *result)
  330. {
  331. struct disk_exception *de = get_exception(ps, index);
  332. /* copy it */
  333. result->old_chunk = le64_to_cpu(de->old_chunk);
  334. result->new_chunk = le64_to_cpu(de->new_chunk);
  335. }
  336. static void write_exception(struct pstore *ps,
  337. uint32_t index, struct core_exception *e)
  338. {
  339. struct disk_exception *de = get_exception(ps, index);
  340. /* copy it */
  341. de->old_chunk = cpu_to_le64(e->old_chunk);
  342. de->new_chunk = cpu_to_le64(e->new_chunk);
  343. }
  344. static void clear_exception(struct pstore *ps, uint32_t index)
  345. {
  346. struct disk_exception *de = get_exception(ps, index);
  347. /* clear it */
  348. de->old_chunk = 0;
  349. de->new_chunk = 0;
  350. }
  351. /*
  352. * Registers the exceptions that are present in the current area.
  353. * 'full' is filled in to indicate if the area has been
  354. * filled.
  355. */
  356. static int insert_exceptions(struct pstore *ps,
  357. int (*callback)(void *callback_context,
  358. chunk_t old, chunk_t new),
  359. void *callback_context,
  360. int *full)
  361. {
  362. int r;
  363. unsigned int i;
  364. struct core_exception e;
  365. /* presume the area is full */
  366. *full = 1;
  367. for (i = 0; i < ps->exceptions_per_area; i++) {
  368. read_exception(ps, i, &e);
  369. /*
  370. * If the new_chunk is pointing at the start of
  371. * the COW device, where the first metadata area
  372. * is we know that we've hit the end of the
  373. * exceptions. Therefore the area is not full.
  374. */
  375. if (e.new_chunk == 0LL) {
  376. ps->current_committed = i;
  377. *full = 0;
  378. break;
  379. }
  380. /*
  381. * Keep track of the start of the free chunks.
  382. */
  383. if (ps->next_free <= e.new_chunk)
  384. ps->next_free = e.new_chunk + 1;
  385. /*
  386. * Otherwise we add the exception to the snapshot.
  387. */
  388. r = callback(callback_context, e.old_chunk, e.new_chunk);
  389. if (r)
  390. return r;
  391. }
  392. return 0;
  393. }
  394. static int read_exceptions(struct pstore *ps,
  395. int (*callback)(void *callback_context, chunk_t old,
  396. chunk_t new),
  397. void *callback_context)
  398. {
  399. int r, full = 1;
  400. /*
  401. * Keeping reading chunks and inserting exceptions until
  402. * we find a partially full area.
  403. */
  404. for (ps->current_area = 0; full; ps->current_area++) {
  405. r = area_io(ps, READ);
  406. if (r)
  407. return r;
  408. r = insert_exceptions(ps, callback, callback_context, &full);
  409. if (r)
  410. return r;
  411. }
  412. ps->current_area--;
  413. return 0;
  414. }
  415. static struct pstore *get_info(struct dm_exception_store *store)
  416. {
  417. return (struct pstore *) store->context;
  418. }
  419. static void persistent_usage(struct dm_exception_store *store,
  420. sector_t *total_sectors,
  421. sector_t *sectors_allocated,
  422. sector_t *metadata_sectors)
  423. {
  424. struct pstore *ps = get_info(store);
  425. *sectors_allocated = ps->next_free * store->chunk_size;
  426. *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
  427. /*
  428. * First chunk is the fixed header.
  429. * Then there are (ps->current_area + 1) metadata chunks, each one
  430. * separated from the next by ps->exceptions_per_area data chunks.
  431. */
  432. *metadata_sectors = (ps->current_area + 1 + NUM_SNAPSHOT_HDR_CHUNKS) *
  433. store->chunk_size;
  434. }
  435. static void persistent_dtr(struct dm_exception_store *store)
  436. {
  437. struct pstore *ps = get_info(store);
  438. destroy_workqueue(ps->metadata_wq);
  439. /* Created in read_header */
  440. if (ps->io_client)
  441. dm_io_client_destroy(ps->io_client);
  442. free_area(ps);
  443. /* Allocated in persistent_read_metadata */
  444. if (ps->callbacks)
  445. vfree(ps->callbacks);
  446. kfree(ps);
  447. }
  448. static int persistent_read_metadata(struct dm_exception_store *store,
  449. int (*callback)(void *callback_context,
  450. chunk_t old, chunk_t new),
  451. void *callback_context)
  452. {
  453. int r, uninitialized_var(new_snapshot);
  454. struct pstore *ps = get_info(store);
  455. /*
  456. * Read the snapshot header.
  457. */
  458. r = read_header(ps, &new_snapshot);
  459. if (r)
  460. return r;
  461. /*
  462. * Now we know correct chunk_size, complete the initialisation.
  463. */
  464. ps->exceptions_per_area = (ps->store->chunk_size << SECTOR_SHIFT) /
  465. sizeof(struct disk_exception);
  466. ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
  467. sizeof(*ps->callbacks));
  468. if (!ps->callbacks)
  469. return -ENOMEM;
  470. /*
  471. * Do we need to setup a new snapshot ?
  472. */
  473. if (new_snapshot) {
  474. r = write_header(ps);
  475. if (r) {
  476. DMWARN("write_header failed");
  477. return r;
  478. }
  479. ps->current_area = 0;
  480. zero_memory_area(ps);
  481. r = zero_disk_area(ps, 0);
  482. if (r)
  483. DMWARN("zero_disk_area(0) failed");
  484. return r;
  485. }
  486. /*
  487. * Sanity checks.
  488. */
  489. if (ps->version != SNAPSHOT_DISK_VERSION) {
  490. DMWARN("unable to handle snapshot disk version %d",
  491. ps->version);
  492. return -EINVAL;
  493. }
  494. /*
  495. * Metadata are valid, but snapshot is invalidated
  496. */
  497. if (!ps->valid)
  498. return 1;
  499. /*
  500. * Read the metadata.
  501. */
  502. r = read_exceptions(ps, callback, callback_context);
  503. return r;
  504. }
  505. static int persistent_prepare_exception(struct dm_exception_store *store,
  506. struct dm_exception *e)
  507. {
  508. struct pstore *ps = get_info(store);
  509. uint32_t stride;
  510. chunk_t next_free;
  511. sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
  512. /* Is there enough room ? */
  513. if (size < ((ps->next_free + 1) * store->chunk_size))
  514. return -ENOSPC;
  515. e->new_chunk = ps->next_free;
  516. /*
  517. * Move onto the next free pending, making sure to take
  518. * into account the location of the metadata chunks.
  519. */
  520. stride = (ps->exceptions_per_area + 1);
  521. next_free = ++ps->next_free;
  522. if (sector_div(next_free, stride) == 1)
  523. ps->next_free++;
  524. atomic_inc(&ps->pending_count);
  525. return 0;
  526. }
  527. static void persistent_commit_exception(struct dm_exception_store *store,
  528. struct dm_exception *e,
  529. void (*callback) (void *, int success),
  530. void *callback_context)
  531. {
  532. unsigned int i;
  533. struct pstore *ps = get_info(store);
  534. struct core_exception ce;
  535. struct commit_callback *cb;
  536. ce.old_chunk = e->old_chunk;
  537. ce.new_chunk = e->new_chunk;
  538. write_exception(ps, ps->current_committed++, &ce);
  539. /*
  540. * Add the callback to the back of the array. This code
  541. * is the only place where the callback array is
  542. * manipulated, and we know that it will never be called
  543. * multiple times concurrently.
  544. */
  545. cb = ps->callbacks + ps->callback_count++;
  546. cb->callback = callback;
  547. cb->context = callback_context;
  548. /*
  549. * If there are exceptions in flight and we have not yet
  550. * filled this metadata area there's nothing more to do.
  551. */
  552. if (!atomic_dec_and_test(&ps->pending_count) &&
  553. (ps->current_committed != ps->exceptions_per_area))
  554. return;
  555. /*
  556. * If we completely filled the current area, then wipe the next one.
  557. */
  558. if ((ps->current_committed == ps->exceptions_per_area) &&
  559. zero_disk_area(ps, ps->current_area + 1))
  560. ps->valid = 0;
  561. /*
  562. * Commit exceptions to disk.
  563. */
  564. if (ps->valid && area_io(ps, WRITE_FLUSH_FUA))
  565. ps->valid = 0;
  566. /*
  567. * Advance to the next area if this one is full.
  568. */
  569. if (ps->current_committed == ps->exceptions_per_area) {
  570. ps->current_committed = 0;
  571. ps->current_area++;
  572. zero_memory_area(ps);
  573. }
  574. for (i = 0; i < ps->callback_count; i++) {
  575. cb = ps->callbacks + i;
  576. cb->callback(cb->context, ps->valid);
  577. }
  578. ps->callback_count = 0;
  579. }
  580. static int persistent_prepare_merge(struct dm_exception_store *store,
  581. chunk_t *last_old_chunk,
  582. chunk_t *last_new_chunk)
  583. {
  584. struct pstore *ps = get_info(store);
  585. struct core_exception ce;
  586. int nr_consecutive;
  587. int r;
  588. /*
  589. * When current area is empty, move back to preceding area.
  590. */
  591. if (!ps->current_committed) {
  592. /*
  593. * Have we finished?
  594. */
  595. if (!ps->current_area)
  596. return 0;
  597. ps->current_area--;
  598. r = area_io(ps, READ);
  599. if (r < 0)
  600. return r;
  601. ps->current_committed = ps->exceptions_per_area;
  602. }
  603. read_exception(ps, ps->current_committed - 1, &ce);
  604. *last_old_chunk = ce.old_chunk;
  605. *last_new_chunk = ce.new_chunk;
  606. /*
  607. * Find number of consecutive chunks within the current area,
  608. * working backwards.
  609. */
  610. for (nr_consecutive = 1; nr_consecutive < ps->current_committed;
  611. nr_consecutive++) {
  612. read_exception(ps, ps->current_committed - 1 - nr_consecutive,
  613. &ce);
  614. if (ce.old_chunk != *last_old_chunk - nr_consecutive ||
  615. ce.new_chunk != *last_new_chunk - nr_consecutive)
  616. break;
  617. }
  618. return nr_consecutive;
  619. }
  620. static int persistent_commit_merge(struct dm_exception_store *store,
  621. int nr_merged)
  622. {
  623. int r, i;
  624. struct pstore *ps = get_info(store);
  625. BUG_ON(nr_merged > ps->current_committed);
  626. for (i = 0; i < nr_merged; i++)
  627. clear_exception(ps, ps->current_committed - 1 - i);
  628. r = area_io(ps, WRITE_FLUSH_FUA);
  629. if (r < 0)
  630. return r;
  631. ps->current_committed -= nr_merged;
  632. /*
  633. * At this stage, only persistent_usage() uses ps->next_free, so
  634. * we make no attempt to keep ps->next_free strictly accurate
  635. * as exceptions may have been committed out-of-order originally.
  636. * Once a snapshot has become merging, we set it to the value it
  637. * would have held had all the exceptions been committed in order.
  638. *
  639. * ps->current_area does not get reduced by prepare_merge() until
  640. * after commit_merge() has removed the nr_merged previous exceptions.
  641. */
  642. ps->next_free = area_location(ps, ps->current_area) +
  643. ps->current_committed + 1;
  644. return 0;
  645. }
  646. static void persistent_drop_snapshot(struct dm_exception_store *store)
  647. {
  648. struct pstore *ps = get_info(store);
  649. ps->valid = 0;
  650. if (write_header(ps))
  651. DMWARN("write header failed");
  652. }
  653. static int persistent_ctr(struct dm_exception_store *store,
  654. unsigned argc, char **argv)
  655. {
  656. struct pstore *ps;
  657. /* allocate the pstore */
  658. ps = kzalloc(sizeof(*ps), GFP_KERNEL);
  659. if (!ps)
  660. return -ENOMEM;
  661. ps->store = store;
  662. ps->valid = 1;
  663. ps->version = SNAPSHOT_DISK_VERSION;
  664. ps->area = NULL;
  665. ps->zero_area = NULL;
  666. ps->header_area = NULL;
  667. ps->next_free = NUM_SNAPSHOT_HDR_CHUNKS + 1; /* header and 1st area */
  668. ps->current_committed = 0;
  669. ps->callback_count = 0;
  670. atomic_set(&ps->pending_count, 0);
  671. ps->callbacks = NULL;
  672. ps->metadata_wq = alloc_workqueue("ksnaphd", WQ_MEM_RECLAIM, 0);
  673. if (!ps->metadata_wq) {
  674. kfree(ps);
  675. DMERR("couldn't start header metadata update thread");
  676. return -ENOMEM;
  677. }
  678. store->context = ps;
  679. return 0;
  680. }
  681. static unsigned persistent_status(struct dm_exception_store *store,
  682. status_type_t status, char *result,
  683. unsigned maxlen)
  684. {
  685. unsigned sz = 0;
  686. switch (status) {
  687. case STATUSTYPE_INFO:
  688. break;
  689. case STATUSTYPE_TABLE:
  690. DMEMIT(" P %llu", (unsigned long long)store->chunk_size);
  691. }
  692. return sz;
  693. }
  694. static struct dm_exception_store_type _persistent_type = {
  695. .name = "persistent",
  696. .module = THIS_MODULE,
  697. .ctr = persistent_ctr,
  698. .dtr = persistent_dtr,
  699. .read_metadata = persistent_read_metadata,
  700. .prepare_exception = persistent_prepare_exception,
  701. .commit_exception = persistent_commit_exception,
  702. .prepare_merge = persistent_prepare_merge,
  703. .commit_merge = persistent_commit_merge,
  704. .drop_snapshot = persistent_drop_snapshot,
  705. .usage = persistent_usage,
  706. .status = persistent_status,
  707. };
  708. static struct dm_exception_store_type _persistent_compat_type = {
  709. .name = "P",
  710. .module = THIS_MODULE,
  711. .ctr = persistent_ctr,
  712. .dtr = persistent_dtr,
  713. .read_metadata = persistent_read_metadata,
  714. .prepare_exception = persistent_prepare_exception,
  715. .commit_exception = persistent_commit_exception,
  716. .prepare_merge = persistent_prepare_merge,
  717. .commit_merge = persistent_commit_merge,
  718. .drop_snapshot = persistent_drop_snapshot,
  719. .usage = persistent_usage,
  720. .status = persistent_status,
  721. };
  722. int dm_persistent_snapshot_init(void)
  723. {
  724. int r;
  725. r = dm_exception_store_type_register(&_persistent_type);
  726. if (r) {
  727. DMERR("Unable to register persistent exception store type");
  728. return r;
  729. }
  730. r = dm_exception_store_type_register(&_persistent_compat_type);
  731. if (r) {
  732. DMERR("Unable to register old-style persistent exception "
  733. "store type");
  734. dm_exception_store_type_unregister(&_persistent_type);
  735. return r;
  736. }
  737. return r;
  738. }
  739. void dm_persistent_snapshot_exit(void)
  740. {
  741. dm_exception_store_type_unregister(&_persistent_type);
  742. dm_exception_store_type_unregister(&_persistent_compat_type);
  743. }