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