dm-exception-store.c 16 KB

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