4 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
5 * Copyright (C) 2006 Red Hat GmbH
7 * This file is released under the GPL.
14 #include <linux/pagemap.h>
15 #include <linux/vmalloc.h>
16 #include <linux/slab.h>
17 #include <linux/dm-io.h>
18 #include <linux/dm-kcopyd.h>
20 #define DM_MSG_PREFIX "snapshots"
21 #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32 /* 16KB */
23 /*-----------------------------------------------------------------
24 * Persistent snapshots, by persistent we mean that the snapshot
25 * will survive a reboot.
26 *---------------------------------------------------------------*/
29 * We need to store a record of which parts of the origin have
30 * been copied to the snapshot device. The snapshot code
31 * requires that we copy exception chunks to chunk aligned areas
32 * of the COW store. It makes sense therefore, to store the
33 * metadata in chunk size blocks.
35 * There is no backward or forward compatibility implemented,
36 * snapshots with different disk versions than the kernel will
37 * not be usable. It is expected that "lvcreate" will blank out
38 * the start of a fresh COW device before calling the snapshot
41 * The first chunk of the COW device just contains the header.
42 * After this there is a chunk filled with exception metadata,
43 * followed by as many exception chunks as can fit in the
46 * All on disk structures are in little-endian format. The end
47 * of the exceptions info is indicated by an exception with a
48 * new_chunk of 0, which is invalid since it would point to the
53 * Magic for persistent snapshots: "SnAp" - Feeble isn't it.
55 #define SNAP_MAGIC 0x70416e53
58 * The on-disk version of the metadata.
60 #define SNAPSHOT_DISK_VERSION 1
66 * Is this snapshot valid. There is no way of recovering
67 * an invalid snapshot.
72 * Simple, incrementing version. no backward
81 struct disk_exception
{
86 struct commit_callback
{
87 void (*callback
)(void *, int success
);
92 * The top level structure for a persistent exception store.
95 struct dm_snapshot
*snap
; /* up pointer to my snapshot */
98 uint32_t exceptions_per_area
;
101 * Now that we have an asynchronous kcopyd there is no
102 * need for large chunk sizes, so it wont hurt to have a
103 * whole chunks worth of metadata in memory at once.
108 * Used to keep track of which metadata area the data in
111 uint32_t current_area
;
114 * The next free chunk for an exception.
119 * The index of next free exception in the current
122 uint32_t current_committed
;
124 atomic_t pending_count
;
125 uint32_t callback_count
;
126 struct commit_callback
*callbacks
;
127 struct dm_io_client
*io_client
;
129 struct workqueue_struct
*metadata_wq
;
132 static unsigned sectors_to_pages(unsigned sectors
)
134 return DIV_ROUND_UP(sectors
, PAGE_SIZE
>> 9);
137 static int alloc_area(struct pstore
*ps
)
142 len
= ps
->snap
->chunk_size
<< SECTOR_SHIFT
;
145 * Allocate the chunk_size block of memory that will hold
146 * a single metadata area.
148 ps
->area
= vmalloc(len
);
155 static void free_area(struct pstore
*ps
)
162 struct dm_io_region
*where
;
163 struct dm_io_request
*io_req
;
164 struct work_struct work
;
168 static void do_metadata(struct work_struct
*work
)
170 struct mdata_req
*req
= container_of(work
, struct mdata_req
, work
);
172 req
->result
= dm_io(req
->io_req
, 1, req
->where
, NULL
);
176 * Read or write a chunk aligned and sized block of data from a device.
178 static int chunk_io(struct pstore
*ps
, uint32_t chunk
, int rw
, int metadata
)
180 struct dm_io_region where
= {
181 .bdev
= ps
->snap
->cow
->bdev
,
182 .sector
= ps
->snap
->chunk_size
* chunk
,
183 .count
= ps
->snap
->chunk_size
,
185 struct dm_io_request io_req
= {
187 .mem
.type
= DM_IO_VMA
,
188 .mem
.ptr
.vma
= ps
->area
,
189 .client
= ps
->io_client
,
192 struct mdata_req req
;
195 return dm_io(&io_req
, 1, &where
, NULL
);
198 req
.io_req
= &io_req
;
201 * Issue the synchronous I/O from a different thread
202 * to avoid generic_make_request recursion.
204 INIT_WORK(&req
.work
, do_metadata
);
205 queue_work(ps
->metadata_wq
, &req
.work
);
206 flush_workqueue(ps
->metadata_wq
);
212 * Convert a metadata area index to a chunk index.
214 static chunk_t
area_location(struct pstore
*ps
, chunk_t area
)
216 return 1 + ((ps
->exceptions_per_area
+ 1) * area
);
220 * Read or write a metadata area. Remembering to skip the first
221 * chunk which holds the header.
223 static int area_io(struct pstore
*ps
, uint32_t area
, int rw
)
228 chunk
= area_location(ps
, area
);
230 r
= chunk_io(ps
, chunk
, rw
, 0);
234 ps
->current_area
= area
;
238 static int zero_area(struct pstore
*ps
, uint32_t area
)
240 memset(ps
->area
, 0, ps
->snap
->chunk_size
<< SECTOR_SHIFT
);
241 return area_io(ps
, area
, WRITE
);
244 static int read_header(struct pstore
*ps
, int *new_snapshot
)
247 struct disk_header
*dh
;
249 int chunk_size_supplied
= 1;
252 * Use default chunk size (or hardsect_size, if larger) if none supplied
254 if (!ps
->snap
->chunk_size
) {
255 ps
->snap
->chunk_size
= max(DM_CHUNK_SIZE_DEFAULT_SECTORS
,
256 bdev_hardsect_size(ps
->snap
->cow
->bdev
) >> 9);
257 ps
->snap
->chunk_mask
= ps
->snap
->chunk_size
- 1;
258 ps
->snap
->chunk_shift
= ffs(ps
->snap
->chunk_size
) - 1;
259 chunk_size_supplied
= 0;
262 ps
->io_client
= dm_io_client_create(sectors_to_pages(ps
->snap
->
264 if (IS_ERR(ps
->io_client
))
265 return PTR_ERR(ps
->io_client
);
271 r
= chunk_io(ps
, 0, READ
, 1);
275 dh
= (struct disk_header
*) ps
->area
;
277 if (le32_to_cpu(dh
->magic
) == 0) {
282 if (le32_to_cpu(dh
->magic
) != SNAP_MAGIC
) {
283 DMWARN("Invalid or corrupt snapshot");
289 ps
->valid
= le32_to_cpu(dh
->valid
);
290 ps
->version
= le32_to_cpu(dh
->version
);
291 chunk_size
= le32_to_cpu(dh
->chunk_size
);
293 if (!chunk_size_supplied
|| ps
->snap
->chunk_size
== chunk_size
)
296 DMWARN("chunk size %llu in device metadata overrides "
297 "table chunk size of %llu.",
298 (unsigned long long)chunk_size
,
299 (unsigned long long)ps
->snap
->chunk_size
);
301 /* We had a bogus chunk_size. Fix stuff up. */
304 ps
->snap
->chunk_size
= chunk_size
;
305 ps
->snap
->chunk_mask
= chunk_size
- 1;
306 ps
->snap
->chunk_shift
= ffs(chunk_size
) - 1;
308 r
= dm_io_client_resize(sectors_to_pages(ps
->snap
->chunk_size
),
321 static int write_header(struct pstore
*ps
)
323 struct disk_header
*dh
;
325 memset(ps
->area
, 0, ps
->snap
->chunk_size
<< SECTOR_SHIFT
);
327 dh
= (struct disk_header
*) ps
->area
;
328 dh
->magic
= cpu_to_le32(SNAP_MAGIC
);
329 dh
->valid
= cpu_to_le32(ps
->valid
);
330 dh
->version
= cpu_to_le32(ps
->version
);
331 dh
->chunk_size
= cpu_to_le32(ps
->snap
->chunk_size
);
333 return chunk_io(ps
, 0, WRITE
, 1);
337 * Access functions for the disk exceptions, these do the endian conversions.
339 static struct disk_exception
*get_exception(struct pstore
*ps
, uint32_t index
)
341 BUG_ON(index
>= ps
->exceptions_per_area
);
343 return ((struct disk_exception
*) ps
->area
) + index
;
346 static void read_exception(struct pstore
*ps
,
347 uint32_t index
, struct disk_exception
*result
)
349 struct disk_exception
*e
= get_exception(ps
, index
);
352 result
->old_chunk
= le64_to_cpu(e
->old_chunk
);
353 result
->new_chunk
= le64_to_cpu(e
->new_chunk
);
356 static void write_exception(struct pstore
*ps
,
357 uint32_t index
, struct disk_exception
*de
)
359 struct disk_exception
*e
= get_exception(ps
, index
);
362 e
->old_chunk
= cpu_to_le64(de
->old_chunk
);
363 e
->new_chunk
= cpu_to_le64(de
->new_chunk
);
367 * Registers the exceptions that are present in the current area.
368 * 'full' is filled in to indicate if the area has been
371 static int insert_exceptions(struct pstore
*ps
, int *full
)
375 struct disk_exception de
;
377 /* presume the area is full */
380 for (i
= 0; i
< ps
->exceptions_per_area
; i
++) {
381 read_exception(ps
, i
, &de
);
384 * If the new_chunk is pointing at the start of
385 * the COW device, where the first metadata area
386 * is we know that we've hit the end of the
387 * exceptions. Therefore the area is not full.
389 if (de
.new_chunk
== 0LL) {
390 ps
->current_committed
= i
;
396 * Keep track of the start of the free chunks.
398 if (ps
->next_free
<= de
.new_chunk
)
399 ps
->next_free
= de
.new_chunk
+ 1;
402 * Otherwise we add the exception to the snapshot.
404 r
= dm_add_exception(ps
->snap
, de
.old_chunk
, de
.new_chunk
);
412 static int read_exceptions(struct pstore
*ps
)
418 * Keeping reading chunks and inserting exceptions until
419 * we find a partially full area.
421 for (area
= 0; full
; area
++) {
422 r
= area_io(ps
, area
, READ
);
426 r
= insert_exceptions(ps
, &full
);
434 static struct pstore
*get_info(struct exception_store
*store
)
436 return (struct pstore
*) store
->context
;
439 static void persistent_fraction_full(struct exception_store
*store
,
440 sector_t
*numerator
, sector_t
*denominator
)
442 *numerator
= get_info(store
)->next_free
* store
->snap
->chunk_size
;
443 *denominator
= get_dev_size(store
->snap
->cow
->bdev
);
446 static void persistent_destroy(struct exception_store
*store
)
448 struct pstore
*ps
= get_info(store
);
450 destroy_workqueue(ps
->metadata_wq
);
451 dm_io_client_destroy(ps
->io_client
);
452 vfree(ps
->callbacks
);
457 static int persistent_read_metadata(struct exception_store
*store
)
459 int r
, uninitialized_var(new_snapshot
);
460 struct pstore
*ps
= get_info(store
);
463 * Read the snapshot header.
465 r
= read_header(ps
, &new_snapshot
);
470 * Now we know correct chunk_size, complete the initialisation.
472 ps
->exceptions_per_area
= (ps
->snap
->chunk_size
<< SECTOR_SHIFT
) /
473 sizeof(struct disk_exception
);
474 ps
->callbacks
= dm_vcalloc(ps
->exceptions_per_area
,
475 sizeof(*ps
->callbacks
));
480 * Do we need to setup a new snapshot ?
483 r
= write_header(ps
);
485 DMWARN("write_header failed");
489 r
= zero_area(ps
, 0);
491 DMWARN("zero_area(0) failed");
499 if (ps
->version
!= SNAPSHOT_DISK_VERSION
) {
500 DMWARN("unable to handle snapshot disk version %d",
506 * Metadata are valid, but snapshot is invalidated
514 r
= read_exceptions(ps
);
522 static int persistent_prepare(struct exception_store
*store
,
523 struct dm_snap_exception
*e
)
525 struct pstore
*ps
= get_info(store
);
527 sector_t size
= get_dev_size(store
->snap
->cow
->bdev
);
529 /* Is there enough room ? */
530 if (size
< ((ps
->next_free
+ 1) * store
->snap
->chunk_size
))
533 e
->new_chunk
= ps
->next_free
;
536 * Move onto the next free pending, making sure to take
537 * into account the location of the metadata chunks.
539 stride
= (ps
->exceptions_per_area
+ 1);
540 if ((++ps
->next_free
% stride
) == 1)
543 atomic_inc(&ps
->pending_count
);
547 static void persistent_commit(struct exception_store
*store
,
548 struct dm_snap_exception
*e
,
549 void (*callback
) (void *, int success
),
550 void *callback_context
)
554 struct pstore
*ps
= get_info(store
);
555 struct disk_exception de
;
556 struct commit_callback
*cb
;
558 de
.old_chunk
= e
->old_chunk
;
559 de
.new_chunk
= e
->new_chunk
;
560 write_exception(ps
, ps
->current_committed
++, &de
);
563 * Add the callback to the back of the array. This code
564 * is the only place where the callback array is
565 * manipulated, and we know that it will never be called
566 * multiple times concurrently.
568 cb
= ps
->callbacks
+ ps
->callback_count
++;
569 cb
->callback
= callback
;
570 cb
->context
= callback_context
;
573 * If there are no more exceptions in flight, or we have
574 * filled this metadata area we commit the exceptions to
577 if (atomic_dec_and_test(&ps
->pending_count
) ||
578 (ps
->current_committed
== ps
->exceptions_per_area
)) {
579 r
= area_io(ps
, ps
->current_area
, WRITE
);
584 * Have we completely filled the current area ?
586 if (ps
->current_committed
== ps
->exceptions_per_area
) {
587 ps
->current_committed
= 0;
588 r
= zero_area(ps
, ps
->current_area
+ 1);
593 for (i
= 0; i
< ps
->callback_count
; i
++) {
594 cb
= ps
->callbacks
+ i
;
595 cb
->callback(cb
->context
, r
== 0 ? 1 : 0);
598 ps
->callback_count
= 0;
602 static void persistent_drop(struct exception_store
*store
)
604 struct pstore
*ps
= get_info(store
);
607 if (write_header(ps
))
608 DMWARN("write header failed");
611 int dm_create_persistent(struct exception_store
*store
)
615 /* allocate the pstore */
616 ps
= kmalloc(sizeof(*ps
), GFP_KERNEL
);
620 ps
->snap
= store
->snap
;
622 ps
->version
= SNAPSHOT_DISK_VERSION
;
624 ps
->next_free
= 2; /* skipping the header and first area */
625 ps
->current_committed
= 0;
627 ps
->callback_count
= 0;
628 atomic_set(&ps
->pending_count
, 0);
629 ps
->callbacks
= NULL
;
631 ps
->metadata_wq
= create_singlethread_workqueue("ksnaphd");
632 if (!ps
->metadata_wq
) {
634 DMERR("couldn't start header metadata update thread");
638 store
->destroy
= persistent_destroy
;
639 store
->read_metadata
= persistent_read_metadata
;
640 store
->prepare_exception
= persistent_prepare
;
641 store
->commit_exception
= persistent_commit
;
642 store
->drop_snapshot
= persistent_drop
;
643 store
->fraction_full
= persistent_fraction_full
;
649 /*-----------------------------------------------------------------
650 * Implementation of the store for non-persistent snapshots.
651 *---------------------------------------------------------------*/
656 static void transient_destroy(struct exception_store
*store
)
658 kfree(store
->context
);
661 static int transient_read_metadata(struct exception_store
*store
)
666 static int transient_prepare(struct exception_store
*store
,
667 struct dm_snap_exception
*e
)
669 struct transient_c
*tc
= (struct transient_c
*) store
->context
;
670 sector_t size
= get_dev_size(store
->snap
->cow
->bdev
);
672 if (size
< (tc
->next_free
+ store
->snap
->chunk_size
))
675 e
->new_chunk
= sector_to_chunk(store
->snap
, tc
->next_free
);
676 tc
->next_free
+= store
->snap
->chunk_size
;
681 static void transient_commit(struct exception_store
*store
,
682 struct dm_snap_exception
*e
,
683 void (*callback
) (void *, int success
),
684 void *callback_context
)
687 callback(callback_context
, 1);
690 static void transient_fraction_full(struct exception_store
*store
,
691 sector_t
*numerator
, sector_t
*denominator
)
693 *numerator
= ((struct transient_c
*) store
->context
)->next_free
;
694 *denominator
= get_dev_size(store
->snap
->cow
->bdev
);
697 int dm_create_transient(struct exception_store
*store
)
699 struct transient_c
*tc
;
701 store
->destroy
= transient_destroy
;
702 store
->read_metadata
= transient_read_metadata
;
703 store
->prepare_exception
= transient_prepare
;
704 store
->commit_exception
= transient_commit
;
705 store
->drop_snapshot
= NULL
;
706 store
->fraction_full
= transient_fraction_full
;
708 tc
= kmalloc(sizeof(struct transient_c
), GFP_KERNEL
);