dm-snap-persistent.c 21 KB

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