dm snapshot: move cow ref from exception store to snap core
[safe/jmp/linux-2.6] / drivers / md / dm-snap-persistent.c
1 /*
2  * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
3  * Copyright (C) 2006-2008 Red Hat GmbH
4  *
5  * This file is released under the GPL.
6  */
7
8 #include "dm-exception-store.h"
9
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
16 #define DM_MSG_PREFIX "persistent snapshot"
17 #define DM_CHUNK_SIZE_DEFAULT_SECTORS 32        /* 16KB */
18
19 /*-----------------------------------------------------------------
20  * Persistent snapshots, by persistent we mean that the snapshot
21  * will survive a reboot.
22  *---------------------------------------------------------------*/
23
24 /*
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.
30  *
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
35  * constructor.
36  *
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
40  * metadata areas.
41  *
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
45  * header chunk.
46  */
47
48 /*
49  * Magic for persistent snapshots: "SnAp" - Feeble isn't it.
50  */
51 #define SNAP_MAGIC 0x70416e53
52
53 /*
54  * The on-disk version of the metadata.
55  */
56 #define SNAPSHOT_DISK_VERSION 1
57
58 struct disk_header {
59         uint32_t magic;
60
61         /*
62          * Is this snapshot valid.  There is no way of recovering
63          * an invalid snapshot.
64          */
65         uint32_t valid;
66
67         /*
68          * Simple, incrementing version. no backward
69          * compatibility.
70          */
71         uint32_t version;
72
73         /* In sectors */
74         uint32_t chunk_size;
75 };
76
77 struct disk_exception {
78         uint64_t old_chunk;
79         uint64_t new_chunk;
80 };
81
82 struct commit_callback {
83         void (*callback)(void *, int success);
84         void *context;
85 };
86
87 /*
88  * The top level structure for a persistent exception store.
89  */
90 struct pstore {
91         struct dm_exception_store *store;
92         int version;
93         int valid;
94         uint32_t exceptions_per_area;
95
96         /*
97          * Now that we have an asynchronous kcopyd there is no
98          * need for large chunk sizes, so it wont hurt to have a
99          * whole chunks worth of metadata in memory at once.
100          */
101         void *area;
102
103         /*
104          * An area of zeros used to clear the next area.
105          */
106         void *zero_area;
107
108         /*
109          * An area used for header. The header can be written
110          * concurrently with metadata (when invalidating the snapshot),
111          * so it needs a separate buffer.
112          */
113         void *header_area;
114
115         /*
116          * Used to keep track of which metadata area the data in
117          * 'chunk' refers to.
118          */
119         chunk_t current_area;
120
121         /*
122          * The next free chunk for an exception.
123          */
124         chunk_t next_free;
125
126         /*
127          * The index of next free exception in the current
128          * metadata area.
129          */
130         uint32_t current_committed;
131
132         atomic_t pending_count;
133         uint32_t callback_count;
134         struct commit_callback *callbacks;
135         struct dm_io_client *io_client;
136
137         struct workqueue_struct *metadata_wq;
138 };
139
140 static unsigned sectors_to_pages(unsigned sectors)
141 {
142         return DIV_ROUND_UP(sectors, PAGE_SIZE >> 9);
143 }
144
145 static int alloc_area(struct pstore *ps)
146 {
147         int r = -ENOMEM;
148         size_t len;
149
150         len = ps->store->chunk_size << SECTOR_SHIFT;
151
152         /*
153          * Allocate the chunk_size block of memory that will hold
154          * a single metadata area.
155          */
156         ps->area = vmalloc(len);
157         if (!ps->area)
158                 goto err_area;
159
160         ps->zero_area = vmalloc(len);
161         if (!ps->zero_area)
162                 goto err_zero_area;
163         memset(ps->zero_area, 0, len);
164
165         ps->header_area = vmalloc(len);
166         if (!ps->header_area)
167                 goto err_header_area;
168
169         return 0;
170
171 err_header_area:
172         vfree(ps->zero_area);
173
174 err_zero_area:
175         vfree(ps->area);
176
177 err_area:
178         return r;
179 }
180
181 static void free_area(struct pstore *ps)
182 {
183         if (ps->area)
184                 vfree(ps->area);
185         ps->area = NULL;
186
187         if (ps->zero_area)
188                 vfree(ps->zero_area);
189         ps->zero_area = NULL;
190
191         if (ps->header_area)
192                 vfree(ps->header_area);
193         ps->header_area = NULL;
194 }
195
196 struct mdata_req {
197         struct dm_io_region *where;
198         struct dm_io_request *io_req;
199         struct work_struct work;
200         int result;
201 };
202
203 static void do_metadata(struct work_struct *work)
204 {
205         struct mdata_req *req = container_of(work, struct mdata_req, work);
206
207         req->result = dm_io(req->io_req, 1, req->where, NULL);
208 }
209
210 /*
211  * Read or write a chunk aligned and sized block of data from a device.
212  */
213 static int chunk_io(struct pstore *ps, void *area, chunk_t chunk, int rw,
214                     int metadata)
215 {
216         struct dm_io_region where = {
217                 .bdev = dm_snap_cow(ps->store->snap)->bdev,
218                 .sector = ps->store->chunk_size * chunk,
219                 .count = ps->store->chunk_size,
220         };
221         struct dm_io_request io_req = {
222                 .bi_rw = rw,
223                 .mem.type = DM_IO_VMA,
224                 .mem.ptr.vma = area,
225                 .client = ps->io_client,
226                 .notify.fn = NULL,
227         };
228         struct mdata_req req;
229
230         if (!metadata)
231                 return dm_io(&io_req, 1, &where, NULL);
232
233         req.where = &where;
234         req.io_req = &io_req;
235
236         /*
237          * Issue the synchronous I/O from a different thread
238          * to avoid generic_make_request recursion.
239          */
240         INIT_WORK(&req.work, do_metadata);
241         queue_work(ps->metadata_wq, &req.work);
242         flush_workqueue(ps->metadata_wq);
243
244         return req.result;
245 }
246
247 /*
248  * Convert a metadata area index to a chunk index.
249  */
250 static chunk_t area_location(struct pstore *ps, chunk_t area)
251 {
252         return 1 + ((ps->exceptions_per_area + 1) * area);
253 }
254
255 /*
256  * Read or write a metadata area.  Remembering to skip the first
257  * chunk which holds the header.
258  */
259 static int area_io(struct pstore *ps, int rw)
260 {
261         int r;
262         chunk_t chunk;
263
264         chunk = area_location(ps, ps->current_area);
265
266         r = chunk_io(ps, ps->area, chunk, rw, 0);
267         if (r)
268                 return r;
269
270         return 0;
271 }
272
273 static void zero_memory_area(struct pstore *ps)
274 {
275         memset(ps->area, 0, ps->store->chunk_size << SECTOR_SHIFT);
276 }
277
278 static int zero_disk_area(struct pstore *ps, chunk_t area)
279 {
280         return chunk_io(ps, ps->zero_area, area_location(ps, area), WRITE, 0);
281 }
282
283 static int read_header(struct pstore *ps, int *new_snapshot)
284 {
285         int r;
286         struct disk_header *dh;
287         unsigned chunk_size;
288         int chunk_size_supplied = 1;
289         char *chunk_err;
290
291         /*
292          * Use default chunk size (or logical_block_size, if larger)
293          * if none supplied
294          */
295         if (!ps->store->chunk_size) {
296                 ps->store->chunk_size = max(DM_CHUNK_SIZE_DEFAULT_SECTORS,
297                     bdev_logical_block_size(dm_snap_cow(ps->store->snap)->
298                                             bdev) >> 9);
299                 ps->store->chunk_mask = ps->store->chunk_size - 1;
300                 ps->store->chunk_shift = ffs(ps->store->chunk_size) - 1;
301                 chunk_size_supplied = 0;
302         }
303
304         ps->io_client = dm_io_client_create(sectors_to_pages(ps->store->
305                                                              chunk_size));
306         if (IS_ERR(ps->io_client))
307                 return PTR_ERR(ps->io_client);
308
309         r = alloc_area(ps);
310         if (r)
311                 return r;
312
313         r = chunk_io(ps, ps->header_area, 0, READ, 1);
314         if (r)
315                 goto bad;
316
317         dh = ps->header_area;
318
319         if (le32_to_cpu(dh->magic) == 0) {
320                 *new_snapshot = 1;
321                 return 0;
322         }
323
324         if (le32_to_cpu(dh->magic) != SNAP_MAGIC) {
325                 DMWARN("Invalid or corrupt snapshot");
326                 r = -ENXIO;
327                 goto bad;
328         }
329
330         *new_snapshot = 0;
331         ps->valid = le32_to_cpu(dh->valid);
332         ps->version = le32_to_cpu(dh->version);
333         chunk_size = le32_to_cpu(dh->chunk_size);
334
335         if (ps->store->chunk_size == chunk_size)
336                 return 0;
337
338         if (chunk_size_supplied)
339                 DMWARN("chunk size %u in device metadata overrides "
340                        "table chunk size of %u.",
341                        chunk_size, ps->store->chunk_size);
342
343         /* We had a bogus chunk_size. Fix stuff up. */
344         free_area(ps);
345
346         r = dm_exception_store_set_chunk_size(ps->store, chunk_size,
347                                               &chunk_err);
348         if (r) {
349                 DMERR("invalid on-disk chunk size %u: %s.",
350                       chunk_size, chunk_err);
351                 return r;
352         }
353
354         r = dm_io_client_resize(sectors_to_pages(ps->store->chunk_size),
355                                 ps->io_client);
356         if (r)
357                 return r;
358
359         r = alloc_area(ps);
360         return r;
361
362 bad:
363         free_area(ps);
364         return r;
365 }
366
367 static int write_header(struct pstore *ps)
368 {
369         struct disk_header *dh;
370
371         memset(ps->header_area, 0, ps->store->chunk_size << SECTOR_SHIFT);
372
373         dh = ps->header_area;
374         dh->magic = cpu_to_le32(SNAP_MAGIC);
375         dh->valid = cpu_to_le32(ps->valid);
376         dh->version = cpu_to_le32(ps->version);
377         dh->chunk_size = cpu_to_le32(ps->store->chunk_size);
378
379         return chunk_io(ps, ps->header_area, 0, WRITE, 1);
380 }
381
382 /*
383  * Access functions for the disk exceptions, these do the endian conversions.
384  */
385 static struct disk_exception *get_exception(struct pstore *ps, uint32_t index)
386 {
387         BUG_ON(index >= ps->exceptions_per_area);
388
389         return ((struct disk_exception *) ps->area) + index;
390 }
391
392 static void read_exception(struct pstore *ps,
393                            uint32_t index, struct disk_exception *result)
394 {
395         struct disk_exception *e = get_exception(ps, index);
396
397         /* copy it */
398         result->old_chunk = le64_to_cpu(e->old_chunk);
399         result->new_chunk = le64_to_cpu(e->new_chunk);
400 }
401
402 static void write_exception(struct pstore *ps,
403                             uint32_t index, struct disk_exception *de)
404 {
405         struct disk_exception *e = get_exception(ps, index);
406
407         /* copy it */
408         e->old_chunk = cpu_to_le64(de->old_chunk);
409         e->new_chunk = cpu_to_le64(de->new_chunk);
410 }
411
412 /*
413  * Registers the exceptions that are present in the current area.
414  * 'full' is filled in to indicate if the area has been
415  * filled.
416  */
417 static int insert_exceptions(struct pstore *ps,
418                              int (*callback)(void *callback_context,
419                                              chunk_t old, chunk_t new),
420                              void *callback_context,
421                              int *full)
422 {
423         int r;
424         unsigned int i;
425         struct disk_exception de;
426
427         /* presume the area is full */
428         *full = 1;
429
430         for (i = 0; i < ps->exceptions_per_area; i++) {
431                 read_exception(ps, i, &de);
432
433                 /*
434                  * If the new_chunk is pointing at the start of
435                  * the COW device, where the first metadata area
436                  * is we know that we've hit the end of the
437                  * exceptions.  Therefore the area is not full.
438                  */
439                 if (de.new_chunk == 0LL) {
440                         ps->current_committed = i;
441                         *full = 0;
442                         break;
443                 }
444
445                 /*
446                  * Keep track of the start of the free chunks.
447                  */
448                 if (ps->next_free <= de.new_chunk)
449                         ps->next_free = de.new_chunk + 1;
450
451                 /*
452                  * Otherwise we add the exception to the snapshot.
453                  */
454                 r = callback(callback_context, de.old_chunk, de.new_chunk);
455                 if (r)
456                         return r;
457         }
458
459         return 0;
460 }
461
462 static int read_exceptions(struct pstore *ps,
463                            int (*callback)(void *callback_context, chunk_t old,
464                                            chunk_t new),
465                            void *callback_context)
466 {
467         int r, full = 1;
468
469         /*
470          * Keeping reading chunks and inserting exceptions until
471          * we find a partially full area.
472          */
473         for (ps->current_area = 0; full; ps->current_area++) {
474                 r = area_io(ps, READ);
475                 if (r)
476                         return r;
477
478                 r = insert_exceptions(ps, callback, callback_context, &full);
479                 if (r)
480                         return r;
481         }
482
483         ps->current_area--;
484
485         return 0;
486 }
487
488 static struct pstore *get_info(struct dm_exception_store *store)
489 {
490         return (struct pstore *) store->context;
491 }
492
493 static void persistent_usage(struct dm_exception_store *store,
494                              sector_t *total_sectors,
495                              sector_t *sectors_allocated,
496                              sector_t *metadata_sectors)
497 {
498         struct pstore *ps = get_info(store);
499
500         *sectors_allocated = ps->next_free * store->chunk_size;
501         *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
502
503         /*
504          * First chunk is the fixed header.
505          * Then there are (ps->current_area + 1) metadata chunks, each one
506          * separated from the next by ps->exceptions_per_area data chunks.
507          */
508         *metadata_sectors = (ps->current_area + 2) * store->chunk_size;
509 }
510
511 static void persistent_dtr(struct dm_exception_store *store)
512 {
513         struct pstore *ps = get_info(store);
514
515         destroy_workqueue(ps->metadata_wq);
516
517         /* Created in read_header */
518         if (ps->io_client)
519                 dm_io_client_destroy(ps->io_client);
520         free_area(ps);
521
522         /* Allocated in persistent_read_metadata */
523         if (ps->callbacks)
524                 vfree(ps->callbacks);
525
526         kfree(ps);
527 }
528
529 static int persistent_read_metadata(struct dm_exception_store *store,
530                                     int (*callback)(void *callback_context,
531                                                     chunk_t old, chunk_t new),
532                                     void *callback_context)
533 {
534         int r, uninitialized_var(new_snapshot);
535         struct pstore *ps = get_info(store);
536
537         /*
538          * Read the snapshot header.
539          */
540         r = read_header(ps, &new_snapshot);
541         if (r)
542                 return r;
543
544         /*
545          * Now we know correct chunk_size, complete the initialisation.
546          */
547         ps->exceptions_per_area = (ps->store->chunk_size << SECTOR_SHIFT) /
548                                   sizeof(struct disk_exception);
549         ps->callbacks = dm_vcalloc(ps->exceptions_per_area,
550                         sizeof(*ps->callbacks));
551         if (!ps->callbacks)
552                 return -ENOMEM;
553
554         /*
555          * Do we need to setup a new snapshot ?
556          */
557         if (new_snapshot) {
558                 r = write_header(ps);
559                 if (r) {
560                         DMWARN("write_header failed");
561                         return r;
562                 }
563
564                 ps->current_area = 0;
565                 zero_memory_area(ps);
566                 r = zero_disk_area(ps, 0);
567                 if (r)
568                         DMWARN("zero_disk_area(0) failed");
569                 return r;
570         }
571         /*
572          * Sanity checks.
573          */
574         if (ps->version != SNAPSHOT_DISK_VERSION) {
575                 DMWARN("unable to handle snapshot disk version %d",
576                        ps->version);
577                 return -EINVAL;
578         }
579
580         /*
581          * Metadata are valid, but snapshot is invalidated
582          */
583         if (!ps->valid)
584                 return 1;
585
586         /*
587          * Read the metadata.
588          */
589         r = read_exceptions(ps, callback, callback_context);
590
591         return r;
592 }
593
594 static int persistent_prepare_exception(struct dm_exception_store *store,
595                                         struct dm_exception *e)
596 {
597         struct pstore *ps = get_info(store);
598         uint32_t stride;
599         chunk_t next_free;
600         sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
601
602         /* Is there enough room ? */
603         if (size < ((ps->next_free + 1) * store->chunk_size))
604                 return -ENOSPC;
605
606         e->new_chunk = ps->next_free;
607
608         /*
609          * Move onto the next free pending, making sure to take
610          * into account the location of the metadata chunks.
611          */
612         stride = (ps->exceptions_per_area + 1);
613         next_free = ++ps->next_free;
614         if (sector_div(next_free, stride) == 1)
615                 ps->next_free++;
616
617         atomic_inc(&ps->pending_count);
618         return 0;
619 }
620
621 static void persistent_commit_exception(struct dm_exception_store *store,
622                                         struct dm_exception *e,
623                                         void (*callback) (void *, int success),
624                                         void *callback_context)
625 {
626         unsigned int i;
627         struct pstore *ps = get_info(store);
628         struct disk_exception de;
629         struct commit_callback *cb;
630
631         de.old_chunk = e->old_chunk;
632         de.new_chunk = e->new_chunk;
633         write_exception(ps, ps->current_committed++, &de);
634
635         /*
636          * Add the callback to the back of the array.  This code
637          * is the only place where the callback array is
638          * manipulated, and we know that it will never be called
639          * multiple times concurrently.
640          */
641         cb = ps->callbacks + ps->callback_count++;
642         cb->callback = callback;
643         cb->context = callback_context;
644
645         /*
646          * If there are exceptions in flight and we have not yet
647          * filled this metadata area there's nothing more to do.
648          */
649         if (!atomic_dec_and_test(&ps->pending_count) &&
650             (ps->current_committed != ps->exceptions_per_area))
651                 return;
652
653         /*
654          * If we completely filled the current area, then wipe the next one.
655          */
656         if ((ps->current_committed == ps->exceptions_per_area) &&
657              zero_disk_area(ps, ps->current_area + 1))
658                 ps->valid = 0;
659
660         /*
661          * Commit exceptions to disk.
662          */
663         if (ps->valid && area_io(ps, WRITE_BARRIER))
664                 ps->valid = 0;
665
666         /*
667          * Advance to the next area if this one is full.
668          */
669         if (ps->current_committed == ps->exceptions_per_area) {
670                 ps->current_committed = 0;
671                 ps->current_area++;
672                 zero_memory_area(ps);
673         }
674
675         for (i = 0; i < ps->callback_count; i++) {
676                 cb = ps->callbacks + i;
677                 cb->callback(cb->context, ps->valid);
678         }
679
680         ps->callback_count = 0;
681 }
682
683 static void persistent_drop_snapshot(struct dm_exception_store *store)
684 {
685         struct pstore *ps = get_info(store);
686
687         ps->valid = 0;
688         if (write_header(ps))
689                 DMWARN("write header failed");
690 }
691
692 static int persistent_ctr(struct dm_exception_store *store,
693                           unsigned argc, char **argv)
694 {
695         struct pstore *ps;
696
697         /* allocate the pstore */
698         ps = kzalloc(sizeof(*ps), GFP_KERNEL);
699         if (!ps)
700                 return -ENOMEM;
701
702         ps->store = store;
703         ps->valid = 1;
704         ps->version = SNAPSHOT_DISK_VERSION;
705         ps->area = NULL;
706         ps->zero_area = NULL;
707         ps->header_area = NULL;
708         ps->next_free = 2;      /* skipping the header and first area */
709         ps->current_committed = 0;
710
711         ps->callback_count = 0;
712         atomic_set(&ps->pending_count, 0);
713         ps->callbacks = NULL;
714
715         ps->metadata_wq = create_singlethread_workqueue("ksnaphd");
716         if (!ps->metadata_wq) {
717                 kfree(ps);
718                 DMERR("couldn't start header metadata update thread");
719                 return -ENOMEM;
720         }
721
722         store->context = ps;
723
724         return 0;
725 }
726
727 static unsigned persistent_status(struct dm_exception_store *store,
728                                   status_type_t status, char *result,
729                                   unsigned maxlen)
730 {
731         unsigned sz = 0;
732
733         switch (status) {
734         case STATUSTYPE_INFO:
735                 break;
736         case STATUSTYPE_TABLE:
737                 DMEMIT(" P %llu", (unsigned long long)store->chunk_size);
738         }
739
740         return sz;
741 }
742
743 static struct dm_exception_store_type _persistent_type = {
744         .name = "persistent",
745         .module = THIS_MODULE,
746         .ctr = persistent_ctr,
747         .dtr = persistent_dtr,
748         .read_metadata = persistent_read_metadata,
749         .prepare_exception = persistent_prepare_exception,
750         .commit_exception = persistent_commit_exception,
751         .drop_snapshot = persistent_drop_snapshot,
752         .usage = persistent_usage,
753         .status = persistent_status,
754 };
755
756 static struct dm_exception_store_type _persistent_compat_type = {
757         .name = "P",
758         .module = THIS_MODULE,
759         .ctr = persistent_ctr,
760         .dtr = persistent_dtr,
761         .read_metadata = persistent_read_metadata,
762         .prepare_exception = persistent_prepare_exception,
763         .commit_exception = persistent_commit_exception,
764         .drop_snapshot = persistent_drop_snapshot,
765         .usage = persistent_usage,
766         .status = persistent_status,
767 };
768
769 int dm_persistent_snapshot_init(void)
770 {
771         int r;
772
773         r = dm_exception_store_type_register(&_persistent_type);
774         if (r) {
775                 DMERR("Unable to register persistent exception store type");
776                 return r;
777         }
778
779         r = dm_exception_store_type_register(&_persistent_compat_type);
780         if (r) {
781                 DMERR("Unable to register old-style persistent exception "
782                       "store type");
783                 dm_exception_store_type_unregister(&_persistent_type);
784                 return r;
785         }
786
787         return r;
788 }
789
790 void dm_persistent_snapshot_exit(void)
791 {
792         dm_exception_store_type_unregister(&_persistent_type);
793         dm_exception_store_type_unregister(&_persistent_compat_type);
794 }