]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/md/dm-exception-store.c
4 * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
6 * This file is released under the GPL.
15 #include <linux/pagemap.h>
16 #include <linux/vmalloc.h>
17 #include <linux/slab.h>
19 /*-----------------------------------------------------------------
20 * Persistent snapshots, by persistent we mean that the snapshot
21 * will survive a reboot.
22 *---------------------------------------------------------------*/
25 * We need to store a record of which parts of the origin have
26 * been copied to the snapshot device. The snapshot code
27 * requires that we copy exception chunks to chunk aligned areas
28 * of the COW store. It makes sense therefore, to store the
29 * metadata in chunk size blocks.
31 * There is no backward or forward compatibility implemented,
32 * snapshots with different disk versions than the kernel will
33 * not be usable. It is expected that "lvcreate" will blank out
34 * the start of a fresh COW device before calling the snapshot
37 * The first chunk of the COW device just contains the header.
38 * After this there is a chunk filled with exception metadata,
39 * followed by as many exception chunks as can fit in the
42 * All on disk structures are in little-endian format. The end
43 * of the exceptions info is indicated by an exception with a
44 * new_chunk of 0, which is invalid since it would point to the
49 * Magic for persistent snapshots: "SnAp" - Feeble isn't it.
51 #define SNAP_MAGIC 0x70416e53
54 * The on-disk version of the metadata.
56 #define SNAPSHOT_DISK_VERSION 1
62 * Is this snapshot valid. There is no way of recovering
63 * an invalid snapshot.
68 * Simple, incrementing version. no backward
77 struct disk_exception
{
82 struct commit_callback
{
83 void (*callback
)(void *, int success
);
88 * The top level structure for a persistent exception store.
91 struct dm_snapshot
*snap
; /* up pointer to my snapshot */
95 uint32_t exceptions_per_area
;
98 * Now that we have an asynchronous kcopyd there is no
99 * need for large chunk sizes, so it wont hurt to have a
100 * whole chunks worth of metadata in memory at once.
105 * Used to keep track of which metadata area the data in
108 uint32_t current_area
;
111 * The next free chunk for an exception.
116 * The index of next free exception in the current
119 uint32_t current_committed
;
121 atomic_t pending_count
;
122 uint32_t callback_count
;
123 struct commit_callback
*callbacks
;
126 static inline unsigned int sectors_to_pages(unsigned int sectors
)
128 return sectors
/ (PAGE_SIZE
>> 9);
131 static int alloc_area(struct pstore
*ps
)
136 len
= ps
->chunk_size
<< SECTOR_SHIFT
;
139 * Allocate the chunk_size block of memory that will hold
140 * a single metadata area.
142 ps
->area
= vmalloc(len
);
149 static void free_area(struct pstore
*ps
)
155 * Read or write a chunk aligned and sized block of data from a device.
157 static int chunk_io(struct pstore
*ps
, uint32_t chunk
, int rw
)
159 struct io_region where
;
162 where
.bdev
= ps
->snap
->cow
->bdev
;
163 where
.sector
= ps
->chunk_size
* chunk
;
164 where
.count
= ps
->chunk_size
;
166 return dm_io_sync_vm(1, &where
, rw
, ps
->area
, &bits
);
170 * Read or write a metadata area. Remembering to skip the first
171 * chunk which holds the header.
173 static int area_io(struct pstore
*ps
, uint32_t area
, int rw
)
178 /* convert a metadata area index to a chunk index */
179 chunk
= 1 + ((ps
->exceptions_per_area
+ 1) * area
);
181 r
= chunk_io(ps
, chunk
, rw
);
185 ps
->current_area
= area
;
189 static int zero_area(struct pstore
*ps
, uint32_t area
)
191 memset(ps
->area
, 0, ps
->chunk_size
<< SECTOR_SHIFT
);
192 return area_io(ps
, area
, WRITE
);
195 static int read_header(struct pstore
*ps
, int *new_snapshot
)
198 struct disk_header
*dh
;
200 r
= chunk_io(ps
, 0, READ
);
204 dh
= (struct disk_header
*) ps
->area
;
206 if (le32_to_cpu(dh
->magic
) == 0) {
209 } else if (le32_to_cpu(dh
->magic
) == SNAP_MAGIC
) {
211 ps
->valid
= le32_to_cpu(dh
->valid
);
212 ps
->version
= le32_to_cpu(dh
->version
);
213 ps
->chunk_size
= le32_to_cpu(dh
->chunk_size
);
216 DMWARN("Invalid/corrupt snapshot");
223 static int write_header(struct pstore
*ps
)
225 struct disk_header
*dh
;
227 memset(ps
->area
, 0, ps
->chunk_size
<< SECTOR_SHIFT
);
229 dh
= (struct disk_header
*) ps
->area
;
230 dh
->magic
= cpu_to_le32(SNAP_MAGIC
);
231 dh
->valid
= cpu_to_le32(ps
->valid
);
232 dh
->version
= cpu_to_le32(ps
->version
);
233 dh
->chunk_size
= cpu_to_le32(ps
->chunk_size
);
235 return chunk_io(ps
, 0, WRITE
);
239 * Access functions for the disk exceptions, these do the endian conversions.
241 static struct disk_exception
*get_exception(struct pstore
*ps
, uint32_t index
)
243 if (index
>= ps
->exceptions_per_area
)
246 return ((struct disk_exception
*) ps
->area
) + index
;
249 static int read_exception(struct pstore
*ps
,
250 uint32_t index
, struct disk_exception
*result
)
252 struct disk_exception
*e
;
254 e
= get_exception(ps
, index
);
259 result
->old_chunk
= le64_to_cpu(e
->old_chunk
);
260 result
->new_chunk
= le64_to_cpu(e
->new_chunk
);
265 static int write_exception(struct pstore
*ps
,
266 uint32_t index
, struct disk_exception
*de
)
268 struct disk_exception
*e
;
270 e
= get_exception(ps
, index
);
275 e
->old_chunk
= cpu_to_le64(de
->old_chunk
);
276 e
->new_chunk
= cpu_to_le64(de
->new_chunk
);
282 * Registers the exceptions that are present in the current area.
283 * 'full' is filled in to indicate if the area has been
286 static int insert_exceptions(struct pstore
*ps
, int *full
)
290 struct disk_exception de
;
292 /* presume the area is full */
295 for (i
= 0; i
< ps
->exceptions_per_area
; i
++) {
296 r
= read_exception(ps
, i
, &de
);
302 * If the new_chunk is pointing at the start of
303 * the COW device, where the first metadata area
304 * is we know that we've hit the end of the
305 * exceptions. Therefore the area is not full.
307 if (de
.new_chunk
== 0LL) {
308 ps
->current_committed
= i
;
314 * Keep track of the start of the free chunks.
316 if (ps
->next_free
<= de
.new_chunk
)
317 ps
->next_free
= de
.new_chunk
+ 1;
320 * Otherwise we add the exception to the snapshot.
322 r
= dm_add_exception(ps
->snap
, de
.old_chunk
, de
.new_chunk
);
330 static int read_exceptions(struct pstore
*ps
)
336 * Keeping reading chunks and inserting exceptions until
337 * we find a partially full area.
339 for (area
= 0; full
; area
++) {
340 r
= area_io(ps
, area
, READ
);
344 r
= insert_exceptions(ps
, &full
);
352 static inline struct pstore
*get_info(struct exception_store
*store
)
354 return (struct pstore
*) store
->context
;
357 static void persistent_fraction_full(struct exception_store
*store
,
358 sector_t
*numerator
, sector_t
*denominator
)
360 *numerator
= get_info(store
)->next_free
* store
->snap
->chunk_size
;
361 *denominator
= get_dev_size(store
->snap
->cow
->bdev
);
364 static void persistent_destroy(struct exception_store
*store
)
366 struct pstore
*ps
= get_info(store
);
368 dm_io_put(sectors_to_pages(ps
->chunk_size
));
369 vfree(ps
->callbacks
);
374 static int persistent_read_metadata(struct exception_store
*store
)
377 struct pstore
*ps
= get_info(store
);
380 * Read the snapshot header.
382 r
= read_header(ps
, &new_snapshot
);
387 * Do we need to setup a new snapshot ?
390 r
= write_header(ps
);
392 DMWARN("write_header failed");
396 r
= zero_area(ps
, 0);
398 DMWARN("zero_area(0) failed");
407 DMWARN("snapshot is marked invalid");
411 if (ps
->version
!= SNAPSHOT_DISK_VERSION
) {
412 DMWARN("unable to handle snapshot disk version %d",
420 r
= read_exceptions(ps
);
428 static int persistent_prepare(struct exception_store
*store
,
431 struct pstore
*ps
= get_info(store
);
433 sector_t size
= get_dev_size(store
->snap
->cow
->bdev
);
435 /* Is there enough room ? */
436 if (size
< ((ps
->next_free
+ 1) * store
->snap
->chunk_size
))
439 e
->new_chunk
= ps
->next_free
;
442 * Move onto the next free pending, making sure to take
443 * into account the location of the metadata chunks.
445 stride
= (ps
->exceptions_per_area
+ 1);
446 if ((++ps
->next_free
% stride
) == 1)
449 atomic_inc(&ps
->pending_count
);
453 static void persistent_commit(struct exception_store
*store
,
455 void (*callback
) (void *, int success
),
456 void *callback_context
)
460 struct pstore
*ps
= get_info(store
);
461 struct disk_exception de
;
462 struct commit_callback
*cb
;
464 de
.old_chunk
= e
->old_chunk
;
465 de
.new_chunk
= e
->new_chunk
;
466 write_exception(ps
, ps
->current_committed
++, &de
);
469 * Add the callback to the back of the array. This code
470 * is the only place where the callback array is
471 * manipulated, and we know that it will never be called
472 * multiple times concurrently.
474 cb
= ps
->callbacks
+ ps
->callback_count
++;
475 cb
->callback
= callback
;
476 cb
->context
= callback_context
;
479 * If there are no more exceptions in flight, or we have
480 * filled this metadata area we commit the exceptions to
483 if (atomic_dec_and_test(&ps
->pending_count
) ||
484 (ps
->current_committed
== ps
->exceptions_per_area
)) {
485 r
= area_io(ps
, ps
->current_area
, WRITE
);
489 for (i
= 0; i
< ps
->callback_count
; i
++) {
490 cb
= ps
->callbacks
+ i
;
491 cb
->callback(cb
->context
, r
== 0 ? 1 : 0);
494 ps
->callback_count
= 0;
498 * Have we completely filled the current area ?
500 if (ps
->current_committed
== ps
->exceptions_per_area
) {
501 ps
->current_committed
= 0;
502 r
= zero_area(ps
, ps
->current_area
+ 1);
508 static void persistent_drop(struct exception_store
*store
)
510 struct pstore
*ps
= get_info(store
);
513 if (write_header(ps
))
514 DMWARN("write header failed");
517 int dm_create_persistent(struct exception_store
*store
, uint32_t chunk_size
)
522 r
= dm_io_get(sectors_to_pages(chunk_size
));
526 /* allocate the pstore */
527 ps
= kmalloc(sizeof(*ps
), GFP_KERNEL
);
533 ps
->snap
= store
->snap
;
535 ps
->version
= SNAPSHOT_DISK_VERSION
;
536 ps
->chunk_size
= chunk_size
;
537 ps
->exceptions_per_area
= (chunk_size
<< SECTOR_SHIFT
) /
538 sizeof(struct disk_exception
);
539 ps
->next_free
= 2; /* skipping the header and first area */
540 ps
->current_committed
= 0;
547 * Allocate space for all the callbacks.
549 ps
->callback_count
= 0;
550 atomic_set(&ps
->pending_count
, 0);
551 ps
->callbacks
= dm_vcalloc(ps
->exceptions_per_area
,
552 sizeof(*ps
->callbacks
));
554 if (!ps
->callbacks
) {
559 store
->destroy
= persistent_destroy
;
560 store
->read_metadata
= persistent_read_metadata
;
561 store
->prepare_exception
= persistent_prepare
;
562 store
->commit_exception
= persistent_commit
;
563 store
->drop_snapshot
= persistent_drop
;
564 store
->fraction_full
= persistent_fraction_full
;
570 dm_io_put(sectors_to_pages(chunk_size
));
577 /*-----------------------------------------------------------------
578 * Implementation of the store for non-persistent snapshots.
579 *---------------------------------------------------------------*/
584 static void transient_destroy(struct exception_store
*store
)
586 kfree(store
->context
);
589 static int transient_read_metadata(struct exception_store
*store
)
594 static int transient_prepare(struct exception_store
*store
, struct exception
*e
)
596 struct transient_c
*tc
= (struct transient_c
*) store
->context
;
597 sector_t size
= get_dev_size(store
->snap
->cow
->bdev
);
599 if (size
< (tc
->next_free
+ store
->snap
->chunk_size
))
602 e
->new_chunk
= sector_to_chunk(store
->snap
, tc
->next_free
);
603 tc
->next_free
+= store
->snap
->chunk_size
;
608 static void transient_commit(struct exception_store
*store
,
610 void (*callback
) (void *, int success
),
611 void *callback_context
)
614 callback(callback_context
, 1);
617 static void transient_fraction_full(struct exception_store
*store
,
618 sector_t
*numerator
, sector_t
*denominator
)
620 *numerator
= ((struct transient_c
*) store
->context
)->next_free
;
621 *denominator
= get_dev_size(store
->snap
->cow
->bdev
);
624 int dm_create_transient(struct exception_store
*store
,
625 struct dm_snapshot
*s
, int blocksize
)
627 struct transient_c
*tc
;
629 memset(store
, 0, sizeof(*store
));
630 store
->destroy
= transient_destroy
;
631 store
->read_metadata
= transient_read_metadata
;
632 store
->prepare_exception
= transient_prepare
;
633 store
->commit_exception
= transient_commit
;
634 store
->fraction_full
= transient_fraction_full
;
637 tc
= kmalloc(sizeof(struct transient_c
), GFP_KERNEL
);