Merge branch 'core' of git://amd64.org/linux/rric into perf/core
[firefly-linux-kernel-4.4.55.git] / drivers / md / dm-snap-transient.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/export.h>
14 #include <linux/slab.h>
15 #include <linux/dm-io.h>
16
17 #define DM_MSG_PREFIX "transient snapshot"
18
19 /*-----------------------------------------------------------------
20  * Implementation of the store for non-persistent snapshots.
21  *---------------------------------------------------------------*/
22 struct transient_c {
23         sector_t next_free;
24 };
25
26 static void transient_dtr(struct dm_exception_store *store)
27 {
28         kfree(store->context);
29 }
30
31 static int transient_read_metadata(struct dm_exception_store *store,
32                                    int (*callback)(void *callback_context,
33                                                    chunk_t old, chunk_t new),
34                                    void *callback_context)
35 {
36         return 0;
37 }
38
39 static int transient_prepare_exception(struct dm_exception_store *store,
40                                        struct dm_exception *e)
41 {
42         struct transient_c *tc = store->context;
43         sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
44
45         if (size < (tc->next_free + store->chunk_size))
46                 return -1;
47
48         e->new_chunk = sector_to_chunk(store, tc->next_free);
49         tc->next_free += store->chunk_size;
50
51         return 0;
52 }
53
54 static void transient_commit_exception(struct dm_exception_store *store,
55                                        struct dm_exception *e,
56                                        void (*callback) (void *, int success),
57                                        void *callback_context)
58 {
59         /* Just succeed */
60         callback(callback_context, 1);
61 }
62
63 static void transient_usage(struct dm_exception_store *store,
64                             sector_t *total_sectors,
65                             sector_t *sectors_allocated,
66                             sector_t *metadata_sectors)
67 {
68         *sectors_allocated = ((struct transient_c *) store->context)->next_free;
69         *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
70         *metadata_sectors = 0;
71 }
72
73 static int transient_ctr(struct dm_exception_store *store,
74                          unsigned argc, char **argv)
75 {
76         struct transient_c *tc;
77
78         tc = kmalloc(sizeof(struct transient_c), GFP_KERNEL);
79         if (!tc)
80                 return -ENOMEM;
81
82         tc->next_free = 0;
83         store->context = tc;
84
85         return 0;
86 }
87
88 static unsigned transient_status(struct dm_exception_store *store,
89                                  status_type_t status, char *result,
90                                  unsigned maxlen)
91 {
92         unsigned sz = 0;
93
94         switch (status) {
95         case STATUSTYPE_INFO:
96                 break;
97         case STATUSTYPE_TABLE:
98                 DMEMIT(" N %llu", (unsigned long long)store->chunk_size);
99         }
100
101         return sz;
102 }
103
104 static struct dm_exception_store_type _transient_type = {
105         .name = "transient",
106         .module = THIS_MODULE,
107         .ctr = transient_ctr,
108         .dtr = transient_dtr,
109         .read_metadata = transient_read_metadata,
110         .prepare_exception = transient_prepare_exception,
111         .commit_exception = transient_commit_exception,
112         .usage = transient_usage,
113         .status = transient_status,
114 };
115
116 static struct dm_exception_store_type _transient_compat_type = {
117         .name = "N",
118         .module = THIS_MODULE,
119         .ctr = transient_ctr,
120         .dtr = transient_dtr,
121         .read_metadata = transient_read_metadata,
122         .prepare_exception = transient_prepare_exception,
123         .commit_exception = transient_commit_exception,
124         .usage = transient_usage,
125         .status = transient_status,
126 };
127
128 int dm_transient_snapshot_init(void)
129 {
130         int r;
131
132         r = dm_exception_store_type_register(&_transient_type);
133         if (r) {
134                 DMWARN("Unable to register transient exception store type");
135                 return r;
136         }
137
138         r = dm_exception_store_type_register(&_transient_compat_type);
139         if (r) {
140                 DMWARN("Unable to register old-style transient "
141                        "exception store type");
142                 dm_exception_store_type_unregister(&_transient_type);
143                 return r;
144         }
145
146         return r;
147 }
148
149 void dm_transient_snapshot_exit(void)
150 {
151         dm_exception_store_type_unregister(&_transient_type);
152         dm_exception_store_type_unregister(&_transient_compat_type);
153 }