dm-exception-store.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /*
  2. * dm-exception-store.c
  3. *
  4. * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
  5. * Copyright (C) 2006 Red Hat GmbH
  6. *
  7. * This file is released under the GPL.
  8. */
  9. #include "dm-snap.h"
  10. #include <linux/mm.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/vmalloc.h>
  13. #include <linux/slab.h>
  14. #include <linux/dm-io.h>
  15. #include <linux/dm-kcopyd.h>
  16. #define DM_MSG_PREFIX "snapshots"
  17. #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32 /* 16KB */
  18. /*-----------------------------------------------------------------
  19. * Persistent snapshots, by persistent we mean that the snapshot
  20. * will survive a reboot.
  21. *---------------------------------------------------------------*/
  22. /*
  23. * We need to store a record of which parts of the origin have
  24. * been copied to the snapshot device. The snapshot code
  25. * requires that we copy exception chunks to chunk aligned areas
  26. * of the COW store. It makes sense therefore, to store the
  27. * metadata in chunk size blocks.
  28. *
  29. * There is no backward or forward compatibility implemented,
  30. * snapshots with different disk versions than the kernel will
  31. * not be usable. It is expected that "lvcreate" will blank out
  32. * the start of a fresh COW device before calling the snapshot
  33. * constructor.
  34. *
  35. * The first chunk of the COW device just contains the header.
  36. * After this there is a chunk filled with exception metadata,
  37. * followed by as many exception chunks as can fit in the
  38. * metadata areas.
  39. *
  40. * All on disk structures are in little-endian format. The end
  41. * of the exceptions info is indicated by an exception with a
  42. * new_chunk of 0, which is invalid since it would point to the
  43. * header chunk.
  44. */
  45. /*
  46. * Magic for persistent snapshots: "SnAp" - Feeble isn't it.
  47. */
  48. #define SNAP_MAGIC 0x70416e53
  49. /*
  50. * The on-disk version of the metadata.
  51. */
  52. #define SNAPSHOT_DISK_VERSION 1
  53. struct disk_header {
  54. uint32_t magic;
  55. /*
  56. * Is this snapshot valid. There is no way of recovering
  57. * an invalid snapshot.
  58. */
  59. uint32_t valid;
  60. /*
  61. * Simple, incrementing version. no backward
  62. * compatibility.
  63. */
  64. uint32_t version;
  65. /* In sectors */
  66. uint32_t chunk_size;
  67. };
  68. struct disk_exception {
  69. uint64_t old_chunk;
  70. uint64_t new_chunk;
  71. };
  72. struct commit_callback {
  73. void (*callback)(void *, int success);
  74. void *context;
  75. };
  76. /*
  77. * The top level structure for a persistent exception store.
  78. */
  79. struct pstore {
  80. struct dm_snapshot *snap; /* up pointer to my snapshot */
  81. int version;
  82. int valid;
  83. uint32_t exceptions_per_area;
  84. /*
  85. * Now that we have an asynchronous kcopyd there is no
  86. * need for large chunk sizes, so it wont hurt to have a
  87. * whole chunks worth of metadata in memory at once.
  88. */
  89. void *area;
  90. /*
  91. * An area of zeros used to clear the next area.
  92. */
  93. void *zero_area;
  94. /*
  95. * Used to keep track of which metadata area the data in
  96. * 'chunk' refers to.
  97. */
  98. chunk_t current_area;
  99. /*
  100. * The next free chunk for an exception.
  101. */
  102. chunk_t next_free;
  103. /*
  104. * The index of next free exception in the current
  105. * metadata area.
  106. */
  107. uint32_t current_committed;
  108. atomic_t pending_count;
  109. uint32_t callback_count;
  110. struct commit_callback *callbacks;
  111. struct dm_io_client *io_client;
  112. struct workqueue_struct *metadata_wq;
  113. };
  114. static unsigned sectors_to_pages(unsigned sectors)
  115. {
  116. return DIV_ROUND_UP(sectors, PAGE_SIZE >> 9);
  117. }
  118. static int alloc_area(struct pstore *ps)
  119. {
  120. int r = -ENOMEM;
  121. size_t len;
  122. len = ps->snap->chunk_size << SECTOR_SHIFT;
  123. /*
  124. * Allocate the chunk_size block of memory that will hold
  125. * a single metadata area.
  126. */
  127. ps->area = vmalloc(len);
  128. if (!ps->area)
  129. return r;
  130. ps->zero_area = vmalloc(len);
  131. if (!ps->zero_area) {
  132. vfree(ps->area);
  133. return r;
  134. }
  135. memset(ps->zero_area, 0, len);
  136. return 0;
  137. }
  138. static void free_area(struct pstore *ps)
  139. {
  140. vfree(ps->area);
  141. ps->area = NULL;
  142. vfree(ps->zero_area);
  143. ps->zero_area = NULL;
  144. }
  145. struct mdata_req {
  146. struct dm_io_region *where;
  147. struct dm_io_request *io_req;
  148. struct work_struct work;
  149. int result;
  150. };
  151. static void do_metadata(struct work_struct *work)
  152. {
  153. struct mdata_req *req = container_of(work, struct mdata_req, work);
  154. req->result = dm_io(req->io_req, 1, req->where, NULL);
  155. }
  156. /*
  157. * Read or write a chunk aligned and sized block of data from a device.
  158. */
  159. static int chunk_io(struct pstore *ps, chunk_t chunk, int rw, int metadata)
  160. {
  161. struct dm_io_region where = {
  162. .bdev = ps->snap->cow->bdev,
  163. .sector = ps->snap->chunk_size * chunk,
  164. .count = ps->snap->chunk_size,
  165. };
  166. struct dm_io_request io_req = {
  167. .bi_rw = rw,
  168. .mem.type = DM_IO_VMA,
  169. .mem.ptr.vma = ps->area,
  170. .client = ps->io_client,
  171. .notify.fn = NULL,
  172. };
  173. struct mdata_req req;
  174. if (!metadata)
  175. return dm_io(&io_req, 1, &where, NULL);
  176. req.where = &where;
  177. req.io_req = &io_req;
  178. /*
  179. * Issue the synchronous I/O from a different thread
  180. * to avoid generic_make_request recursion.
  181. */
  182. INIT_WORK(&req.work, do_metadata);
  183. queue_work(ps->metadata_wq, &req.work);
  184. flush_workqueue(ps->metadata_wq);
  185. return req.result;
  186. }
  187. /*
  188. * Convert a metadata area index to a chunk index.
  189. */
  190. static chunk_t area_location(struct pstore *ps, chunk_t area)
  191. {
  192. return 1 + ((ps->exceptions_per_area + 1) * area);
  193. }
  194. /*
  195. * Read or write a metadata area. Remembering to skip the first
  196. * chunk which holds the header.
  197. */
  198. static int area_io(struct pstore *ps, int rw)
  199. {
  200. int r;
  201. chunk_t chunk;
  202. chunk = area_location(ps, ps->current_area);
  203. r = chunk_io(ps, chunk, rw, 0);
  204. if (r)
  205. return r;
  206. return 0;
  207. }
  208. static void zero_memory_area(struct pstore *ps)
  209. {
  210. memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT);
  211. }
  212. static int zero_disk_area(struct pstore *ps, chunk_t area)
  213. {
  214. struct dm_io_region where = {
  215. .bdev = ps->snap->cow->bdev,
  216. .sector = ps->snap->chunk_size * area_location(ps, area),
  217. .count = ps->snap->chunk_size,
  218. };
  219. struct dm_io_request io_req = {
  220. .bi_rw = WRITE,
  221. .mem.type = DM_IO_VMA,
  222. .mem.ptr.vma = ps->zero_area,
  223. .client = ps->io_client,
  224. .notify.fn = NULL,
  225. };
  226. return dm_io(&io_req, 1, &where, NULL);
  227. }
  228. static int read_header(struct pstore *ps, int *new_snapshot)
  229. {
  230. int r;
  231. struct disk_header *dh;
  232. chunk_t chunk_size;
  233. int chunk_size_supplied = 1;
  234. /*
  235. * Use default chunk size (or hardsect_size, if larger) if none supplied
  236. */
  237. if (!ps->snap->chunk_size) {
  238. ps->snap->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS,
  239. bdev_hardsect_size(ps->snap->cow->bdev) >> 9);
  240. ps->snap->chunk_mask = ps->snap->chunk_size - 1;
  241. ps->snap->chunk_shift = ffs(ps->snap->chunk_size) - 1;
  242. chunk_size_supplied = 0;
  243. }
  244. ps->io_client = dm_io_client_create(sectors_to_pages(ps->snap->
  245. chunk_size));
  246. if (IS_ERR(ps->io_client))
  247. return PTR_ERR(ps->io_client);
  248. r = alloc_area(ps);
  249. if (r)
  250. return r;
  251. r = chunk_io(ps, 0, READ, 1);
  252. if (r)
  253. goto bad;
  254. dh = (struct disk_header *) ps->area;
  255. if (le32_to_cpu(dh->magic) == 0) {
  256. *new_snapshot = 1;
  257. return 0;
  258. }
  259. if (le32_to_cpu(dh->magic) != SNAP_MAGIC) {
  260. DMWARN("Invalid or corrupt snapshot");
  261. r = -ENXIO;
  262. goto bad;
  263. }
  264. *new_snapshot = 0;
  265. ps->valid = le32_to_cpu(dh->valid);
  266. ps->version = le32_to_cpu(dh->version);
  267. chunk_size = le32_to_cpu(dh->chunk_size);
  268. if (!chunk_size_supplied || ps->snap->chunk_size == chunk_size)
  269. return 0;
  270. DMWARN("chunk size %llu in device metadata overrides "
  271. "table chunk size of %llu.",
  272. (unsigned long long)chunk_size,
  273. (unsigned long long)ps->snap->chunk_size);
  274. /* We had a bogus chunk_size. Fix stuff up. */
  275. free_area(ps);
  276. ps->snap->chunk_size = chunk_size;
  277. ps->snap->chunk_mask = chunk_size - 1;
  278. ps->snap->chunk_shift = ffs(chunk_size) - 1;
  279. r = dm_io_client_resize(sectors_to_pages(ps->snap->chunk_size),
  280. ps->io_client);
  281. if (r)
  282. return r;
  283. r = alloc_area(ps);
  284. return r;
  285. bad:
  286. free_area(ps);
  287. return r;
  288. }
  289. static int write_header(struct pstore *ps)
  290. {
  291. struct disk_header *dh;
  292. memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT);
  293. dh = (struct disk_header *) ps->area;
  294. dh->magic = cpu_to_le32(SNAP_MAGIC);
  295. dh->valid = cpu_to_le32(ps->valid);
  296. dh->version = cpu_to_le32(ps->version);
  297. dh->chunk_size = cpu_to_le32(ps->snap->chunk_size);
  298. return chunk_io(ps, 0, WRITE, 1);
  299. }
  300. /*
  301. * Access functions for the disk exceptions, these do the endian conversions.
  302. */
  303. static struct disk_exception *get_exception(struct pstore *ps, uint32_t index)
  304. {
  305. BUG_ON(index >= ps->exceptions_per_area);
  306. return ((struct disk_exception *) ps->area) + index;
  307. }
  308. static void read_exception(struct pstore *ps,
  309. uint32_t index, struct disk_exception *result)
  310. {
  311. struct disk_exception *e = get_exception(ps, index);
  312. /* copy it */
  313. result->old_chunk = le64_to_cpu(e->old_chunk);
  314. result->new_chunk = le64_to_cpu(e->new_chunk);
  315. }
  316. static void write_exception(struct pstore *ps,
  317. uint32_t index, struct disk_exception *de)
  318. {
  319. struct disk_exception *e = get_exception(ps, index);
  320. /* copy it */
  321. e->old_chunk = cpu_to_le64(de->old_chunk);
  322. e->new_chunk = cpu_to_le64(de->new_chunk);
  323. }
  324. /*
  325. * Registers the exceptions that are present in the current area.
  326. * 'full' is filled in to indicate if the area has been
  327. * filled.
  328. */
  329. static int insert_exceptions(struct pstore *ps, int *full)
  330. {
  331. int r;
  332. unsigned int i;
  333. struct disk_exception de;
  334. /* presume the area is full */
  335. *full = 1;
  336. for (i = 0; i < ps->exceptions_per_area; i++) {
  337. read_exception(ps, i, &de);
  338. /*
  339. * If the new_chunk is pointing at the start of
  340. * the COW device, where the first metadata area
  341. * is we know that we've hit the end of the
  342. * exceptions. Therefore the area is not full.
  343. */
  344. if (de.new_chunk == 0LL) {
  345. ps->current_committed = i;
  346. *full = 0;
  347. break;
  348. }
  349. /*
  350. * Keep track of the start of the free chunks.
  351. */
  352. if (ps->next_free <= de.new_chunk)
  353. ps->next_free = de.new_chunk + 1;
  354. /*
  355. * Otherwise we add the exception to the snapshot.
  356. */
  357. r = dm_add_exception(ps->snap, de.old_chunk, de.new_chunk);
  358. if (r)
  359. return r;
  360. }
  361. return 0;
  362. }
  363. static int read_exceptions(struct pstore *ps)
  364. {
  365. int r, full = 1;
  366. /*
  367. * Keeping reading chunks and inserting exceptions until
  368. * we find a partially full area.
  369. */
  370. for (ps->current_area = 0; full; ps->current_area++) {
  371. r = area_io(ps, READ);
  372. if (r)
  373. return r;
  374. r = insert_exceptions(ps, &full);
  375. if (r)
  376. return r;
  377. }
  378. ps->current_area--;
  379. return 0;
  380. }
  381. static struct pstore *get_info(struct exception_store *store)
  382. {
  383. return (struct pstore *) store->context;
  384. }
  385. static void persistent_fraction_full(struct exception_store *store,
  386. sector_t *numerator, sector_t *denominator)
  387. {
  388. *numerator = get_info(store)->next_free * store->snap->chunk_size;
  389. *denominator = get_dev_size(store->snap->cow->bdev);
  390. }
  391. static void persistent_destroy(struct exception_store *store)
  392. {
  393. struct pstore *ps = get_info(store);
  394. destroy_workqueue(ps->metadata_wq);
  395. dm_io_client_destroy(ps->io_client);
  396. vfree(ps->callbacks);
  397. free_area(ps);
  398. kfree(ps);
  399. }
  400. static int persistent_read_metadata(struct exception_store *store)
  401. {
  402. int r, uninitialized_var(new_snapshot);
  403. struct pstore *ps = get_info(store);
  404. /*
  405. * Read the snapshot header.
  406. */
  407. r = read_header(ps, &new_snapshot);
  408. if (r)
  409. return r;
  410. /*
  411. * Now we know correct chunk_size, complete the initialisation.
  412. */
  413. ps->exceptions_per_area = (ps->snap->chunk_size << SECTOR_SHIFT) /
  414. sizeof(struct disk_exception);
  415. ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
  416. sizeof(*ps->callbacks));
  417. if (!ps->callbacks)
  418. return -ENOMEM;
  419. /*
  420. * Do we need to setup a new snapshot ?
  421. */
  422. if (new_snapshot) {
  423. r = write_header(ps);
  424. if (r) {
  425. DMWARN("write_header failed");
  426. return r;
  427. }
  428. ps->current_area = 0;
  429. zero_memory_area(ps);
  430. r = zero_disk_area(ps, 0);
  431. if (r) {
  432. DMWARN("zero_disk_area(0) failed");
  433. return r;
  434. }
  435. } else {
  436. /*
  437. * Sanity checks.
  438. */
  439. if (ps->version != SNAPSHOT_DISK_VERSION) {
  440. DMWARN("unable to handle snapshot disk version %d",
  441. ps->version);
  442. return -EINVAL;
  443. }
  444. /*
  445. * Metadata are valid, but snapshot is invalidated
  446. */
  447. if (!ps->valid)
  448. return 1;
  449. /*
  450. * Read the metadata.
  451. */
  452. r = read_exceptions(ps);
  453. if (r)
  454. return r;
  455. }
  456. return 0;
  457. }
  458. static int persistent_prepare(struct exception_store *store,
  459. struct dm_snap_exception *e)
  460. {
  461. struct pstore *ps = get_info(store);
  462. uint32_t stride;
  463. chunk_t next_free;
  464. sector_t size = get_dev_size(store->snap->cow->bdev);
  465. /* Is there enough room ? */
  466. if (size < ((ps->next_free + 1) * store->snap->chunk_size))
  467. return -ENOSPC;
  468. e->new_chunk = ps->next_free;
  469. /*
  470. * Move onto the next free pending, making sure to take
  471. * into account the location of the metadata chunks.
  472. */
  473. stride = (ps->exceptions_per_area + 1);
  474. next_free = ++ps->next_free;
  475. if (sector_div(next_free, stride) == 1)
  476. ps->next_free++;
  477. atomic_inc(&ps->pending_count);
  478. return 0;
  479. }
  480. static void persistent_commit(struct exception_store *store,
  481. struct dm_snap_exception *e,
  482. void (*callback) (void *, int success),
  483. void *callback_context)
  484. {
  485. unsigned int i;
  486. struct pstore *ps = get_info(store);
  487. struct disk_exception de;
  488. struct commit_callback *cb;
  489. de.old_chunk = e->old_chunk;
  490. de.new_chunk = e->new_chunk;
  491. write_exception(ps, ps->current_committed++, &de);
  492. /*
  493. * Add the callback to the back of the array. This code
  494. * is the only place where the callback array is
  495. * manipulated, and we know that it will never be called
  496. * multiple times concurrently.
  497. */
  498. cb = ps->callbacks + ps->callback_count++;
  499. cb->callback = callback;
  500. cb->context = callback_context;
  501. /*
  502. * If there are exceptions in flight and we have not yet
  503. * filled this metadata area there's nothing more to do.
  504. */
  505. if (!atomic_dec_and_test(&ps->pending_count) &&
  506. (ps->current_committed != ps->exceptions_per_area))
  507. return;
  508. /*
  509. * If we completely filled the current area, then wipe the next one.
  510. */
  511. if ((ps->current_committed == ps->exceptions_per_area) &&
  512. zero_disk_area(ps, ps->current_area + 1))
  513. ps->valid = 0;
  514. /*
  515. * Commit exceptions to disk.
  516. */
  517. if (ps->valid && area_io(ps, WRITE))
  518. ps->valid = 0;
  519. /*
  520. * Advance to the next area if this one is full.
  521. */
  522. if (ps->current_committed == ps->exceptions_per_area) {
  523. ps->current_committed = 0;
  524. ps->current_area++;
  525. zero_memory_area(ps);
  526. }
  527. for (i = 0; i < ps->callback_count; i++) {
  528. cb = ps->callbacks + i;
  529. cb->callback(cb->context, ps->valid);
  530. }
  531. ps->callback_count = 0;
  532. }
  533. static void persistent_drop(struct exception_store *store)
  534. {
  535. struct pstore *ps = get_info(store);
  536. ps->valid = 0;
  537. if (write_header(ps))
  538. DMWARN("write header failed");
  539. }
  540. int dm_create_persistent(struct exception_store *store)
  541. {
  542. struct pstore *ps;
  543. /* allocate the pstore */
  544. ps = kmalloc(sizeof(*ps), GFP_KERNEL);
  545. if (!ps)
  546. return -ENOMEM;
  547. ps->snap = store->snap;
  548. ps->valid = 1;
  549. ps->version = SNAPSHOT_DISK_VERSION;
  550. ps->area = NULL;
  551. ps->next_free = 2; /* skipping the header and first area */
  552. ps->current_committed = 0;
  553. ps->callback_count = 0;
  554. atomic_set(&ps->pending_count, 0);
  555. ps->callbacks = NULL;
  556. ps->metadata_wq = create_singlethread_workqueue("ksnaphd");
  557. if (!ps->metadata_wq) {
  558. kfree(ps);
  559. DMERR("couldn't start header metadata update thread");
  560. return -ENOMEM;
  561. }
  562. store->destroy = persistent_destroy;
  563. store->read_metadata = persistent_read_metadata;
  564. store->prepare_exception = persistent_prepare;
  565. store->commit_exception = persistent_commit;
  566. store->drop_snapshot = persistent_drop;
  567. store->fraction_full = persistent_fraction_full;
  568. store->context = ps;
  569. return 0;
  570. }
  571. /*-----------------------------------------------------------------
  572. * Implementation of the store for non-persistent snapshots.
  573. *---------------------------------------------------------------*/
  574. struct transient_c {
  575. sector_t next_free;
  576. };
  577. static void transient_destroy(struct exception_store *store)
  578. {
  579. kfree(store->context);
  580. }
  581. static int transient_read_metadata(struct exception_store *store)
  582. {
  583. return 0;
  584. }
  585. static int transient_prepare(struct exception_store *store,
  586. struct dm_snap_exception *e)
  587. {
  588. struct transient_c *tc = (struct transient_c *) store->context;
  589. sector_t size = get_dev_size(store->snap->cow->bdev);
  590. if (size < (tc->next_free + store->snap->chunk_size))
  591. return -1;
  592. e->new_chunk = sector_to_chunk(store->snap, tc->next_free);
  593. tc->next_free += store->snap->chunk_size;
  594. return 0;
  595. }
  596. static void transient_commit(struct exception_store *store,
  597. struct dm_snap_exception *e,
  598. void (*callback) (void *, int success),
  599. void *callback_context)
  600. {
  601. /* Just succeed */
  602. callback(callback_context, 1);
  603. }
  604. static void transient_fraction_full(struct exception_store *store,
  605. sector_t *numerator, sector_t *denominator)
  606. {
  607. *numerator = ((struct transient_c *) store->context)->next_free;
  608. *denominator = get_dev_size(store->snap->cow->bdev);
  609. }
  610. int dm_create_transient(struct exception_store *store)
  611. {
  612. struct transient_c *tc;
  613. store->destroy = transient_destroy;
  614. store->read_metadata = transient_read_metadata;
  615. store->prepare_exception = transient_prepare;
  616. store->commit_exception = transient_commit;
  617. store->drop_snapshot = NULL;
  618. store->fraction_full = transient_fraction_full;
  619. tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
  620. if (!tc)
  621. return -ENOMEM;
  622. tc->next_free = 0;
  623. store->context = tc;
  624. return 0;
  625. }