dm-snap-persistent.c 20 KB

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