dm-snap-persistent.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  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_work(&req.work);
  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. /*
  226. * Read or write a metadata area. Remembering to skip the first
  227. * chunk which holds the header.
  228. */
  229. static int area_io(struct pstore *ps, int rw)
  230. {
  231. int r;
  232. chunk_t chunk;
  233. chunk = area_location(ps, ps->current_area);
  234. r = chunk_io(ps, ps->area, chunk, rw, 0);
  235. if (r)
  236. return r;
  237. return 0;
  238. }
  239. static void zero_memory_area(struct pstore *ps)
  240. {
  241. memset(ps->area, 0, ps->store->chunk_size << SECTOR_SHIFT);
  242. }
  243. static int zero_disk_area(struct pstore *ps, chunk_t area)
  244. {
  245. return chunk_io(ps, ps->zero_area, area_location(ps, area), WRITE, 0);
  246. }
  247. static int read_header(struct pstore *ps, int *new_snapshot)
  248. {
  249. int r;
  250. struct disk_header *dh;
  251. unsigned chunk_size;
  252. int chunk_size_supplied = 1;
  253. char *chunk_err;
  254. /*
  255. * Use default chunk size (or logical_block_size, if larger)
  256. * if none supplied
  257. */
  258. if (!ps->store->chunk_size) {
  259. ps->store->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS,
  260. bdev_logical_block_size(dm_snap_cow(ps->store->snap)->
  261. bdev) >> 9);
  262. ps->store->chunk_mask = ps->store->chunk_size - 1;
  263. ps->store->chunk_shift = ffs(ps->store->chunk_size) - 1;
  264. chunk_size_supplied = 0;
  265. }
  266. ps->io_client = dm_io_client_create();
  267. if (IS_ERR(ps->io_client))
  268. return PTR_ERR(ps->io_client);
  269. r = alloc_area(ps);
  270. if (r)
  271. return r;
  272. r = chunk_io(ps, ps->header_area, 0, READ, 1);
  273. if (r)
  274. goto bad;
  275. dh = ps->header_area;
  276. if (le32_to_cpu(dh->magic) == 0) {
  277. *new_snapshot = 1;
  278. return 0;
  279. }
  280. if (le32_to_cpu(dh->magic) != SNAP_MAGIC) {
  281. DMWARN("Invalid or corrupt snapshot");
  282. r = -ENXIO;
  283. goto bad;
  284. }
  285. *new_snapshot = 0;
  286. ps->valid = le32_to_cpu(dh->valid);
  287. ps->version = le32_to_cpu(dh->version);
  288. chunk_size = le32_to_cpu(dh->chunk_size);
  289. if (ps->store->chunk_size == chunk_size)
  290. return 0;
  291. if (chunk_size_supplied)
  292. DMWARN("chunk size %u in device metadata overrides "
  293. "table chunk size of %u.",
  294. chunk_size, ps->store->chunk_size);
  295. /* We had a bogus chunk_size. Fix stuff up. */
  296. free_area(ps);
  297. r = dm_exception_store_set_chunk_size(ps->store, chunk_size,
  298. &chunk_err);
  299. if (r) {
  300. DMERR("invalid on-disk chunk size %u: %s.",
  301. chunk_size, chunk_err);
  302. return r;
  303. }
  304. r = alloc_area(ps);
  305. return r;
  306. bad:
  307. free_area(ps);
  308. return r;
  309. }
  310. static int write_header(struct pstore *ps)
  311. {
  312. struct disk_header *dh;
  313. memset(ps->header_area, 0, ps->store->chunk_size << SECTOR_SHIFT);
  314. dh = ps->header_area;
  315. dh->magic = cpu_to_le32(SNAP_MAGIC);
  316. dh->valid = cpu_to_le32(ps->valid);
  317. dh->version = cpu_to_le32(ps->version);
  318. dh->chunk_size = cpu_to_le32(ps->store->chunk_size);
  319. return chunk_io(ps, ps->header_area, 0, WRITE, 1);
  320. }
  321. /*
  322. * Access functions for the disk exceptions, these do the endian conversions.
  323. */
  324. static struct disk_exception *get_exception(struct pstore *ps, uint32_t index)
  325. {
  326. BUG_ON(index >= ps->exceptions_per_area);
  327. return ((struct disk_exception *) ps->area) + index;
  328. }
  329. static void read_exception(struct pstore *ps,
  330. uint32_t index, struct core_exception *result)
  331. {
  332. struct disk_exception *de = get_exception(ps, index);
  333. /* copy it */
  334. result->old_chunk = le64_to_cpu(de->old_chunk);
  335. result->new_chunk = le64_to_cpu(de->new_chunk);
  336. }
  337. static void write_exception(struct pstore *ps,
  338. uint32_t index, struct core_exception *e)
  339. {
  340. struct disk_exception *de = get_exception(ps, index);
  341. /* copy it */
  342. de->old_chunk = cpu_to_le64(e->old_chunk);
  343. de->new_chunk = cpu_to_le64(e->new_chunk);
  344. }
  345. static void clear_exception(struct pstore *ps, uint32_t index)
  346. {
  347. struct disk_exception *de = get_exception(ps, index);
  348. /* clear it */
  349. de->old_chunk = 0;
  350. de->new_chunk = 0;
  351. }
  352. /*
  353. * Registers the exceptions that are present in the current area.
  354. * 'full' is filled in to indicate if the area has been
  355. * filled.
  356. */
  357. static int insert_exceptions(struct pstore *ps,
  358. int (*callback)(void *callback_context,
  359. chunk_t old, chunk_t new),
  360. void *callback_context,
  361. int *full)
  362. {
  363. int r;
  364. unsigned int i;
  365. struct core_exception e;
  366. /* presume the area is full */
  367. *full = 1;
  368. for (i = 0; i < ps->exceptions_per_area; i++) {
  369. read_exception(ps, i, &e);
  370. /*
  371. * If the new_chunk is pointing at the start of
  372. * the COW device, where the first metadata area
  373. * is we know that we've hit the end of the
  374. * exceptions. Therefore the area is not full.
  375. */
  376. if (e.new_chunk == 0LL) {
  377. ps->current_committed = i;
  378. *full = 0;
  379. break;
  380. }
  381. /*
  382. * Keep track of the start of the free chunks.
  383. */
  384. if (ps->next_free <= e.new_chunk)
  385. ps->next_free = e.new_chunk + 1;
  386. /*
  387. * Otherwise we add the exception to the snapshot.
  388. */
  389. r = callback(callback_context, e.old_chunk, e.new_chunk);
  390. if (r)
  391. return r;
  392. }
  393. return 0;
  394. }
  395. static int read_exceptions(struct pstore *ps,
  396. int (*callback)(void *callback_context, chunk_t old,
  397. chunk_t new),
  398. void *callback_context)
  399. {
  400. int r, full = 1;
  401. /*
  402. * Keeping reading chunks and inserting exceptions until
  403. * we find a partially full area.
  404. */
  405. for (ps->current_area = 0; full; ps->current_area++) {
  406. r = area_io(ps, READ);
  407. if (r)
  408. return r;
  409. r = insert_exceptions(ps, callback, callback_context, &full);
  410. if (r)
  411. return r;
  412. }
  413. ps->current_area--;
  414. return 0;
  415. }
  416. static struct pstore *get_info(struct dm_exception_store *store)
  417. {
  418. return (struct pstore *) store->context;
  419. }
  420. static void persistent_usage(struct dm_exception_store *store,
  421. sector_t *total_sectors,
  422. sector_t *sectors_allocated,
  423. sector_t *metadata_sectors)
  424. {
  425. struct pstore *ps = get_info(store);
  426. *sectors_allocated = ps->next_free * store->chunk_size;
  427. *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
  428. /*
  429. * First chunk is the fixed header.
  430. * Then there are (ps->current_area + 1) metadata chunks, each one
  431. * separated from the next by ps->exceptions_per_area data chunks.
  432. */
  433. *metadata_sectors = (ps->current_area + 1 + NUM_SNAPSHOT_HDR_CHUNKS) *
  434. store->chunk_size;
  435. }
  436. static void persistent_dtr(struct dm_exception_store *store)
  437. {
  438. struct pstore *ps = get_info(store);
  439. destroy_workqueue(ps->metadata_wq);
  440. /* Created in read_header */
  441. if (ps->io_client)
  442. dm_io_client_destroy(ps->io_client);
  443. free_area(ps);
  444. /* Allocated in persistent_read_metadata */
  445. if (ps->callbacks)
  446. vfree(ps->callbacks);
  447. kfree(ps);
  448. }
  449. static int persistent_read_metadata(struct dm_exception_store *store,
  450. int (*callback)(void *callback_context,
  451. chunk_t old, chunk_t new),
  452. void *callback_context)
  453. {
  454. int r, uninitialized_var(new_snapshot);
  455. struct pstore *ps = get_info(store);
  456. /*
  457. * Read the snapshot header.
  458. */
  459. r = read_header(ps, &new_snapshot);
  460. if (r)
  461. return r;
  462. /*
  463. * Now we know correct chunk_size, complete the initialisation.
  464. */
  465. ps->exceptions_per_area = (ps->store->chunk_size << SECTOR_SHIFT) /
  466. sizeof(struct disk_exception);
  467. ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
  468. sizeof(*ps->callbacks));
  469. if (!ps->callbacks)
  470. return -ENOMEM;
  471. /*
  472. * Do we need to setup a new snapshot ?
  473. */
  474. if (new_snapshot) {
  475. r = write_header(ps);
  476. if (r) {
  477. DMWARN("write_header failed");
  478. return r;
  479. }
  480. ps->current_area = 0;
  481. zero_memory_area(ps);
  482. r = zero_disk_area(ps, 0);
  483. if (r)
  484. DMWARN("zero_disk_area(0) failed");
  485. return r;
  486. }
  487. /*
  488. * Sanity checks.
  489. */
  490. if (ps->version != SNAPSHOT_DISK_VERSION) {
  491. DMWARN("unable to handle snapshot disk version %d",
  492. ps->version);
  493. return -EINVAL;
  494. }
  495. /*
  496. * Metadata are valid, but snapshot is invalidated
  497. */
  498. if (!ps->valid)
  499. return 1;
  500. /*
  501. * Read the metadata.
  502. */
  503. r = read_exceptions(ps, callback, callback_context);
  504. return r;
  505. }
  506. static int persistent_prepare_exception(struct dm_exception_store *store,
  507. struct dm_exception *e)
  508. {
  509. struct pstore *ps = get_info(store);
  510. uint32_t stride;
  511. chunk_t next_free;
  512. sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
  513. /* Is there enough room ? */
  514. if (size < ((ps->next_free + 1) * store->chunk_size))
  515. return -ENOSPC;
  516. e->new_chunk = ps->next_free;
  517. /*
  518. * Move onto the next free pending, making sure to take
  519. * into account the location of the metadata chunks.
  520. */
  521. stride = (ps->exceptions_per_area + 1);
  522. next_free = ++ps->next_free;
  523. if (sector_div(next_free, stride) == 1)
  524. ps->next_free++;
  525. atomic_inc(&ps->pending_count);
  526. return 0;
  527. }
  528. static void persistent_commit_exception(struct dm_exception_store *store,
  529. struct dm_exception *e,
  530. void (*callback) (void *, int success),
  531. void *callback_context)
  532. {
  533. unsigned int i;
  534. struct pstore *ps = get_info(store);
  535. struct core_exception ce;
  536. struct commit_callback *cb;
  537. ce.old_chunk = e->old_chunk;
  538. ce.new_chunk = e->new_chunk;
  539. write_exception(ps, ps->current_committed++, &ce);
  540. /*
  541. * Add the callback to the back of the array. This code
  542. * is the only place where the callback array is
  543. * manipulated, and we know that it will never be called
  544. * multiple times concurrently.
  545. */
  546. cb = ps->callbacks + ps->callback_count++;
  547. cb->callback = callback;
  548. cb->context = callback_context;
  549. /*
  550. * If there are exceptions in flight and we have not yet
  551. * filled this metadata area there's nothing more to do.
  552. */
  553. if (!atomic_dec_and_test(&ps->pending_count) &&
  554. (ps->current_committed != ps->exceptions_per_area))
  555. return;
  556. /*
  557. * If we completely filled the current area, then wipe the next one.
  558. */
  559. if ((ps->current_committed == ps->exceptions_per_area) &&
  560. zero_disk_area(ps, ps->current_area + 1))
  561. ps->valid = 0;
  562. /*
  563. * Commit exceptions to disk.
  564. */
  565. if (ps->valid && area_io(ps, WRITE_FLUSH_FUA))
  566. ps->valid = 0;
  567. /*
  568. * Advance to the next area if this one is full.
  569. */
  570. if (ps->current_committed == ps->exceptions_per_area) {
  571. ps->current_committed = 0;
  572. ps->current_area++;
  573. zero_memory_area(ps);
  574. }
  575. for (i = 0; i < ps->callback_count; i++) {
  576. cb = ps->callbacks + i;
  577. cb->callback(cb->context, ps->valid);
  578. }
  579. ps->callback_count = 0;
  580. }
  581. static int persistent_prepare_merge(struct dm_exception_store *store,
  582. chunk_t *last_old_chunk,
  583. chunk_t *last_new_chunk)
  584. {
  585. struct pstore *ps = get_info(store);
  586. struct core_exception ce;
  587. int nr_consecutive;
  588. int r;
  589. /*
  590. * When current area is empty, move back to preceding area.
  591. */
  592. if (!ps->current_committed) {
  593. /*
  594. * Have we finished?
  595. */
  596. if (!ps->current_area)
  597. return 0;
  598. ps->current_area--;
  599. r = area_io(ps, READ);
  600. if (r < 0)
  601. return r;
  602. ps->current_committed = ps->exceptions_per_area;
  603. }
  604. read_exception(ps, ps->current_committed - 1, &ce);
  605. *last_old_chunk = ce.old_chunk;
  606. *last_new_chunk = ce.new_chunk;
  607. /*
  608. * Find number of consecutive chunks within the current area,
  609. * working backwards.
  610. */
  611. for (nr_consecutive = 1; nr_consecutive < ps->current_committed;
  612. nr_consecutive++) {
  613. read_exception(ps, ps->current_committed - 1 - nr_consecutive,
  614. &ce);
  615. if (ce.old_chunk != *last_old_chunk - nr_consecutive ||
  616. ce.new_chunk != *last_new_chunk - nr_consecutive)
  617. break;
  618. }
  619. return nr_consecutive;
  620. }
  621. static int persistent_commit_merge(struct dm_exception_store *store,
  622. int nr_merged)
  623. {
  624. int r, i;
  625. struct pstore *ps = get_info(store);
  626. BUG_ON(nr_merged > ps->current_committed);
  627. for (i = 0; i < nr_merged; i++)
  628. clear_exception(ps, ps->current_committed - 1 - i);
  629. r = area_io(ps, WRITE_FLUSH_FUA);
  630. if (r < 0)
  631. return r;
  632. ps->current_committed -= nr_merged;
  633. /*
  634. * At this stage, only persistent_usage() uses ps->next_free, so
  635. * we make no attempt to keep ps->next_free strictly accurate
  636. * as exceptions may have been committed out-of-order originally.
  637. * Once a snapshot has become merging, we set it to the value it
  638. * would have held had all the exceptions been committed in order.
  639. *
  640. * ps->current_area does not get reduced by prepare_merge() until
  641. * after commit_merge() has removed the nr_merged previous exceptions.
  642. */
  643. ps->next_free = area_location(ps, ps->current_area) +
  644. ps->current_committed + 1;
  645. return 0;
  646. }
  647. static void persistent_drop_snapshot(struct dm_exception_store *store)
  648. {
  649. struct pstore *ps = get_info(store);
  650. ps->valid = 0;
  651. if (write_header(ps))
  652. DMWARN("write header failed");
  653. }
  654. static int persistent_ctr(struct dm_exception_store *store,
  655. unsigned argc, char **argv)
  656. {
  657. struct pstore *ps;
  658. /* allocate the pstore */
  659. ps = kzalloc(sizeof(*ps), GFP_KERNEL);
  660. if (!ps)
  661. return -ENOMEM;
  662. ps->store = store;
  663. ps->valid = 1;
  664. ps->version = SNAPSHOT_DISK_VERSION;
  665. ps->area = NULL;
  666. ps->zero_area = NULL;
  667. ps->header_area = NULL;
  668. ps->next_free = NUM_SNAPSHOT_HDR_CHUNKS + 1; /* header and 1st area */
  669. ps->current_committed = 0;
  670. ps->callback_count = 0;
  671. atomic_set(&ps->pending_count, 0);
  672. ps->callbacks = NULL;
  673. ps->metadata_wq = alloc_workqueue("ksnaphd", WQ_MEM_RECLAIM, 0);
  674. if (!ps->metadata_wq) {
  675. kfree(ps);
  676. DMERR("couldn't start header metadata update thread");
  677. return -ENOMEM;
  678. }
  679. store->context = ps;
  680. return 0;
  681. }
  682. static unsigned persistent_status(struct dm_exception_store *store,
  683. status_type_t status, char *result,
  684. unsigned maxlen)
  685. {
  686. unsigned sz = 0;
  687. switch (status) {
  688. case STATUSTYPE_INFO:
  689. break;
  690. case STATUSTYPE_TABLE:
  691. DMEMIT(" P %llu", (unsigned long long)store->chunk_size);
  692. }
  693. return sz;
  694. }
  695. static struct dm_exception_store_type _persistent_type = {
  696. .name = "persistent",
  697. .module = THIS_MODULE,
  698. .ctr = persistent_ctr,
  699. .dtr = persistent_dtr,
  700. .read_metadata = persistent_read_metadata,
  701. .prepare_exception = persistent_prepare_exception,
  702. .commit_exception = persistent_commit_exception,
  703. .prepare_merge = persistent_prepare_merge,
  704. .commit_merge = persistent_commit_merge,
  705. .drop_snapshot = persistent_drop_snapshot,
  706. .usage = persistent_usage,
  707. .status = persistent_status,
  708. };
  709. static struct dm_exception_store_type _persistent_compat_type = {
  710. .name = "P",
  711. .module = THIS_MODULE,
  712. .ctr = persistent_ctr,
  713. .dtr = persistent_dtr,
  714. .read_metadata = persistent_read_metadata,
  715. .prepare_exception = persistent_prepare_exception,
  716. .commit_exception = persistent_commit_exception,
  717. .prepare_merge = persistent_prepare_merge,
  718. .commit_merge = persistent_commit_merge,
  719. .drop_snapshot = persistent_drop_snapshot,
  720. .usage = persistent_usage,
  721. .status = persistent_status,
  722. };
  723. int dm_persistent_snapshot_init(void)
  724. {
  725. int r;
  726. r = dm_exception_store_type_register(&_persistent_type);
  727. if (r) {
  728. DMERR("Unable to register persistent exception store type");
  729. return r;
  730. }
  731. r = dm_exception_store_type_register(&_persistent_compat_type);
  732. if (r) {
  733. DMERR("Unable to register old-style persistent exception "
  734. "store type");
  735. dm_exception_store_type_unregister(&_persistent_type);
  736. return r;
  737. }
  738. return r;
  739. }
  740. void dm_persistent_snapshot_exit(void)
  741. {
  742. dm_exception_store_type_unregister(&_persistent_type);
  743. dm_exception_store_type_unregister(&_persistent_compat_type);
  744. }