dm-snap-persistent.c 23 KB

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