dm-exception-store.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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 <linux/mm.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/slab.h>
  15. #include <linux/dm-io.h>
  16. #include <linux/dm-kcopyd.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. chunk_t current_area;
  96. /*
  97. * The next free chunk for an exception.
  98. */
  99. chunk_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 DIV_ROUND_UP(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 dm_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, chunk_t chunk, int rw, int metadata)
  149. {
  150. struct dm_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. * Convert a metadata area index to a chunk index.
  178. */
  179. static chunk_t area_location(struct pstore *ps, chunk_t area)
  180. {
  181. return 1 + ((ps->exceptions_per_area + 1) * area);
  182. }
  183. /*
  184. * Read or write a metadata area. Remembering to skip the first
  185. * chunk which holds the header.
  186. */
  187. static int area_io(struct pstore *ps, chunk_t area, int rw)
  188. {
  189. int r;
  190. chunk_t chunk;
  191. chunk = area_location(ps, area);
  192. r = chunk_io(ps, chunk, rw, 0);
  193. if (r)
  194. return r;
  195. ps->current_area = area;
  196. return 0;
  197. }
  198. static int zero_area(struct pstore *ps, chunk_t area)
  199. {
  200. memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT);
  201. return area_io(ps, area, WRITE);
  202. }
  203. static int read_header(struct pstore *ps, int *new_snapshot)
  204. {
  205. int r;
  206. struct disk_header *dh;
  207. chunk_t chunk_size;
  208. int chunk_size_supplied = 1;
  209. /*
  210. * Use default chunk size (or hardsect_size, if larger) if none supplied
  211. */
  212. if (!ps->snap->chunk_size) {
  213. ps->snap->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS,
  214. bdev_hardsect_size(ps->snap->cow->bdev) >> 9);
  215. ps->snap->chunk_mask = ps->snap->chunk_size - 1;
  216. ps->snap->chunk_shift = ffs(ps->snap->chunk_size) - 1;
  217. chunk_size_supplied = 0;
  218. }
  219. ps->io_client = dm_io_client_create(sectors_to_pages(ps->snap->
  220. chunk_size));
  221. if (IS_ERR(ps->io_client))
  222. return PTR_ERR(ps->io_client);
  223. r = alloc_area(ps);
  224. if (r)
  225. return r;
  226. r = chunk_io(ps, 0, READ, 1);
  227. if (r)
  228. goto bad;
  229. dh = (struct disk_header *) ps->area;
  230. if (le32_to_cpu(dh->magic) == 0) {
  231. *new_snapshot = 1;
  232. return 0;
  233. }
  234. if (le32_to_cpu(dh->magic) != SNAP_MAGIC) {
  235. DMWARN("Invalid or corrupt snapshot");
  236. r = -ENXIO;
  237. goto bad;
  238. }
  239. *new_snapshot = 0;
  240. ps->valid = le32_to_cpu(dh->valid);
  241. ps->version = le32_to_cpu(dh->version);
  242. chunk_size = le32_to_cpu(dh->chunk_size);
  243. if (!chunk_size_supplied || ps->snap->chunk_size == chunk_size)
  244. return 0;
  245. DMWARN("chunk size %llu in device metadata overrides "
  246. "table chunk size of %llu.",
  247. (unsigned long long)chunk_size,
  248. (unsigned long long)ps->snap->chunk_size);
  249. /* We had a bogus chunk_size. Fix stuff up. */
  250. free_area(ps);
  251. ps->snap->chunk_size = chunk_size;
  252. ps->snap->chunk_mask = chunk_size - 1;
  253. ps->snap->chunk_shift = ffs(chunk_size) - 1;
  254. r = dm_io_client_resize(sectors_to_pages(ps->snap->chunk_size),
  255. ps->io_client);
  256. if (r)
  257. return r;
  258. r = alloc_area(ps);
  259. return r;
  260. bad:
  261. free_area(ps);
  262. return r;
  263. }
  264. static int write_header(struct pstore *ps)
  265. {
  266. struct disk_header *dh;
  267. memset(ps->area, 0, ps->snap->chunk_size << SECTOR_SHIFT);
  268. dh = (struct disk_header *) ps->area;
  269. dh->magic = cpu_to_le32(SNAP_MAGIC);
  270. dh->valid = cpu_to_le32(ps->valid);
  271. dh->version = cpu_to_le32(ps->version);
  272. dh->chunk_size = cpu_to_le32(ps->snap->chunk_size);
  273. return chunk_io(ps, 0, WRITE, 1);
  274. }
  275. /*
  276. * Access functions for the disk exceptions, these do the endian conversions.
  277. */
  278. static struct disk_exception *get_exception(struct pstore *ps, uint32_t index)
  279. {
  280. BUG_ON(index >= ps->exceptions_per_area);
  281. return ((struct disk_exception *) ps->area) + index;
  282. }
  283. static void read_exception(struct pstore *ps,
  284. uint32_t index, struct disk_exception *result)
  285. {
  286. struct disk_exception *e = get_exception(ps, index);
  287. /* copy it */
  288. result->old_chunk = le64_to_cpu(e->old_chunk);
  289. result->new_chunk = le64_to_cpu(e->new_chunk);
  290. }
  291. static void write_exception(struct pstore *ps,
  292. uint32_t index, struct disk_exception *de)
  293. {
  294. struct disk_exception *e = get_exception(ps, index);
  295. /* copy it */
  296. e->old_chunk = cpu_to_le64(de->old_chunk);
  297. e->new_chunk = cpu_to_le64(de->new_chunk);
  298. }
  299. /*
  300. * Registers the exceptions that are present in the current area.
  301. * 'full' is filled in to indicate if the area has been
  302. * filled.
  303. */
  304. static int insert_exceptions(struct pstore *ps, int *full)
  305. {
  306. int r;
  307. unsigned int i;
  308. struct disk_exception de;
  309. /* presume the area is full */
  310. *full = 1;
  311. for (i = 0; i < ps->exceptions_per_area; i++) {
  312. read_exception(ps, i, &de);
  313. /*
  314. * If the new_chunk is pointing at the start of
  315. * the COW device, where the first metadata area
  316. * is we know that we've hit the end of the
  317. * exceptions. Therefore the area is not full.
  318. */
  319. if (de.new_chunk == 0LL) {
  320. ps->current_committed = i;
  321. *full = 0;
  322. break;
  323. }
  324. /*
  325. * Keep track of the start of the free chunks.
  326. */
  327. if (ps->next_free <= de.new_chunk)
  328. ps->next_free = de.new_chunk + 1;
  329. /*
  330. * Otherwise we add the exception to the snapshot.
  331. */
  332. r = dm_add_exception(ps->snap, de.old_chunk, de.new_chunk);
  333. if (r)
  334. return r;
  335. }
  336. return 0;
  337. }
  338. static int read_exceptions(struct pstore *ps)
  339. {
  340. chunk_t area;
  341. int r, full = 1;
  342. /*
  343. * Keeping reading chunks and inserting exceptions until
  344. * we find a partially full area.
  345. */
  346. for (area = 0; full; area++) {
  347. r = area_io(ps, area, READ);
  348. if (r)
  349. return r;
  350. r = insert_exceptions(ps, &full);
  351. if (r)
  352. return r;
  353. }
  354. return 0;
  355. }
  356. static struct pstore *get_info(struct exception_store *store)
  357. {
  358. return (struct pstore *) store->context;
  359. }
  360. static void persistent_fraction_full(struct exception_store *store,
  361. sector_t *numerator, sector_t *denominator)
  362. {
  363. *numerator = get_info(store)->next_free * store->snap->chunk_size;
  364. *denominator = get_dev_size(store->snap->cow->bdev);
  365. }
  366. static void persistent_destroy(struct exception_store *store)
  367. {
  368. struct pstore *ps = get_info(store);
  369. destroy_workqueue(ps->metadata_wq);
  370. dm_io_client_destroy(ps->io_client);
  371. vfree(ps->callbacks);
  372. free_area(ps);
  373. kfree(ps);
  374. }
  375. static int persistent_read_metadata(struct exception_store *store)
  376. {
  377. int r, uninitialized_var(new_snapshot);
  378. struct pstore *ps = get_info(store);
  379. /*
  380. * Read the snapshot header.
  381. */
  382. r = read_header(ps, &new_snapshot);
  383. if (r)
  384. return r;
  385. /*
  386. * Now we know correct chunk_size, complete the initialisation.
  387. */
  388. ps->exceptions_per_area = (ps->snap->chunk_size << SECTOR_SHIFT) /
  389. sizeof(struct disk_exception);
  390. ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
  391. sizeof(*ps->callbacks));
  392. if (!ps->callbacks)
  393. return -ENOMEM;
  394. /*
  395. * Do we need to setup a new snapshot ?
  396. */
  397. if (new_snapshot) {
  398. r = write_header(ps);
  399. if (r) {
  400. DMWARN("write_header failed");
  401. return r;
  402. }
  403. r = zero_area(ps, 0);
  404. if (r) {
  405. DMWARN("zero_area(0) failed");
  406. return r;
  407. }
  408. } else {
  409. /*
  410. * Sanity checks.
  411. */
  412. if (ps->version != SNAPSHOT_DISK_VERSION) {
  413. DMWARN("unable to handle snapshot disk version %d",
  414. ps->version);
  415. return -EINVAL;
  416. }
  417. /*
  418. * Metadata are valid, but snapshot is invalidated
  419. */
  420. if (!ps->valid)
  421. return 1;
  422. /*
  423. * Read the metadata.
  424. */
  425. r = read_exceptions(ps);
  426. if (r)
  427. return r;
  428. }
  429. return 0;
  430. }
  431. static int persistent_prepare(struct exception_store *store,
  432. struct dm_snap_exception *e)
  433. {
  434. struct pstore *ps = get_info(store);
  435. uint32_t stride;
  436. chunk_t next_free;
  437. sector_t size = get_dev_size(store->snap->cow->bdev);
  438. /* Is there enough room ? */
  439. if (size < ((ps->next_free + 1) * store->snap->chunk_size))
  440. return -ENOSPC;
  441. e->new_chunk = ps->next_free;
  442. /*
  443. * Move onto the next free pending, making sure to take
  444. * into account the location of the metadata chunks.
  445. */
  446. stride = (ps->exceptions_per_area + 1);
  447. next_free = ++ps->next_free;
  448. if (sector_div(next_free, stride) == 1)
  449. ps->next_free++;
  450. atomic_inc(&ps->pending_count);
  451. return 0;
  452. }
  453. static void persistent_commit(struct exception_store *store,
  454. struct dm_snap_exception *e,
  455. void (*callback) (void *, int success),
  456. void *callback_context)
  457. {
  458. int r;
  459. unsigned int i;
  460. struct pstore *ps = get_info(store);
  461. struct disk_exception de;
  462. struct commit_callback *cb;
  463. de.old_chunk = e->old_chunk;
  464. de.new_chunk = e->new_chunk;
  465. write_exception(ps, ps->current_committed++, &de);
  466. /*
  467. * Add the callback to the back of the array. This code
  468. * is the only place where the callback array is
  469. * manipulated, and we know that it will never be called
  470. * multiple times concurrently.
  471. */
  472. cb = ps->callbacks + ps->callback_count++;
  473. cb->callback = callback;
  474. cb->context = callback_context;
  475. /*
  476. * If there are no more exceptions in flight, or we have
  477. * filled this metadata area we commit the exceptions to
  478. * disk.
  479. */
  480. if (atomic_dec_and_test(&ps->pending_count) ||
  481. (ps->current_committed == ps->exceptions_per_area)) {
  482. r = area_io(ps, ps->current_area, WRITE);
  483. if (r)
  484. ps->valid = 0;
  485. /*
  486. * Have we completely filled the current area ?
  487. */
  488. if (ps->current_committed == ps->exceptions_per_area) {
  489. ps->current_committed = 0;
  490. r = zero_area(ps, ps->current_area + 1);
  491. if (r)
  492. ps->valid = 0;
  493. }
  494. for (i = 0; i < ps->callback_count; i++) {
  495. cb = ps->callbacks + i;
  496. cb->callback(cb->context, r == 0 ? 1 : 0);
  497. }
  498. ps->callback_count = 0;
  499. }
  500. }
  501. static void persistent_drop(struct exception_store *store)
  502. {
  503. struct pstore *ps = get_info(store);
  504. ps->valid = 0;
  505. if (write_header(ps))
  506. DMWARN("write header failed");
  507. }
  508. int dm_create_persistent(struct exception_store *store)
  509. {
  510. struct pstore *ps;
  511. /* allocate the pstore */
  512. ps = kmalloc(sizeof(*ps), GFP_KERNEL);
  513. if (!ps)
  514. return -ENOMEM;
  515. ps->snap = store->snap;
  516. ps->valid = 1;
  517. ps->version = SNAPSHOT_DISK_VERSION;
  518. ps->area = NULL;
  519. ps->next_free = 2; /* skipping the header and first area */
  520. ps->current_committed = 0;
  521. ps->callback_count = 0;
  522. atomic_set(&ps->pending_count, 0);
  523. ps->callbacks = NULL;
  524. ps->metadata_wq = create_singlethread_workqueue("ksnaphd");
  525. if (!ps->metadata_wq) {
  526. kfree(ps);
  527. DMERR("couldn't start header metadata update thread");
  528. return -ENOMEM;
  529. }
  530. store->destroy = persistent_destroy;
  531. store->read_metadata = persistent_read_metadata;
  532. store->prepare_exception = persistent_prepare;
  533. store->commit_exception = persistent_commit;
  534. store->drop_snapshot = persistent_drop;
  535. store->fraction_full = persistent_fraction_full;
  536. store->context = ps;
  537. return 0;
  538. }
  539. /*-----------------------------------------------------------------
  540. * Implementation of the store for non-persistent snapshots.
  541. *---------------------------------------------------------------*/
  542. struct transient_c {
  543. sector_t next_free;
  544. };
  545. static void transient_destroy(struct exception_store *store)
  546. {
  547. kfree(store->context);
  548. }
  549. static int transient_read_metadata(struct exception_store *store)
  550. {
  551. return 0;
  552. }
  553. static int transient_prepare(struct exception_store *store,
  554. struct dm_snap_exception *e)
  555. {
  556. struct transient_c *tc = (struct transient_c *) store->context;
  557. sector_t size = get_dev_size(store->snap->cow->bdev);
  558. if (size < (tc->next_free + store->snap->chunk_size))
  559. return -1;
  560. e->new_chunk = sector_to_chunk(store->snap, tc->next_free);
  561. tc->next_free += store->snap->chunk_size;
  562. return 0;
  563. }
  564. static void transient_commit(struct exception_store *store,
  565. struct dm_snap_exception *e,
  566. void (*callback) (void *, int success),
  567. void *callback_context)
  568. {
  569. /* Just succeed */
  570. callback(callback_context, 1);
  571. }
  572. static void transient_fraction_full(struct exception_store *store,
  573. sector_t *numerator, sector_t *denominator)
  574. {
  575. *numerator = ((struct transient_c *) store->context)->next_free;
  576. *denominator = get_dev_size(store->snap->cow->bdev);
  577. }
  578. int dm_create_transient(struct exception_store *store)
  579. {
  580. struct transient_c *tc;
  581. store->destroy = transient_destroy;
  582. store->read_metadata = transient_read_metadata;
  583. store->prepare_exception = transient_prepare;
  584. store->commit_exception = transient_commit;
  585. store->drop_snapshot = NULL;
  586. store->fraction_full = transient_fraction_full;
  587. tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
  588. if (!tc)
  589. return -ENOMEM;
  590. tc->next_free = 0;
  591. store->context = tc;
  592. return 0;
  593. }