dm: change "unsigned" to "unsigned int"
[linux-block.git] / drivers / md / dm-snap-transient.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2001-2002 Sistina Software (UK) Limited.
4  * Copyright (C) 2006-2008 Red Hat GmbH
5  *
6  * This file is released under the GPL.
7  */
8
9 #include "dm-exception-store.h"
10
11 #include <linux/mm.h>
12 #include <linux/pagemap.h>
13 #include <linux/vmalloc.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/dm-io.h>
17
18 #define DM_MSG_PREFIX "transient snapshot"
19
20 /*-----------------------------------------------------------------
21  * Implementation of the store for non-persistent snapshots.
22  *---------------------------------------------------------------*/
23 struct transient_c {
24         sector_t next_free;
25 };
26
27 static void transient_dtr(struct dm_exception_store *store)
28 {
29         kfree(store->context);
30 }
31
32 static int transient_read_metadata(struct dm_exception_store *store,
33                                    int (*callback)(void *callback_context,
34                                                    chunk_t old, chunk_t new),
35                                    void *callback_context)
36 {
37         return 0;
38 }
39
40 static int transient_prepare_exception(struct dm_exception_store *store,
41                                        struct dm_exception *e)
42 {
43         struct transient_c *tc = store->context;
44         sector_t size = get_dev_size(dm_snap_cow(store->snap)->bdev);
45
46         if (size < (tc->next_free + store->chunk_size))
47                 return -1;
48
49         e->new_chunk = sector_to_chunk(store, tc->next_free);
50         tc->next_free += store->chunk_size;
51
52         return 0;
53 }
54
55 static void transient_commit_exception(struct dm_exception_store *store,
56                                        struct dm_exception *e, int valid,
57                                        void (*callback) (void *, int success),
58                                        void *callback_context)
59 {
60         /* Just succeed */
61         callback(callback_context, valid);
62 }
63
64 static void transient_usage(struct dm_exception_store *store,
65                             sector_t *total_sectors,
66                             sector_t *sectors_allocated,
67                             sector_t *metadata_sectors)
68 {
69         *sectors_allocated = ((struct transient_c *) store->context)->next_free;
70         *total_sectors = get_dev_size(dm_snap_cow(store->snap)->bdev);
71         *metadata_sectors = 0;
72 }
73
74 static int transient_ctr(struct dm_exception_store *store, char *options)
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 int transient_status(struct dm_exception_store *store,
89                                  status_type_t status, char *result,
90                                  unsigned int maxlen)
91 {
92         unsigned int 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                 break;
100         case STATUSTYPE_IMA:
101                 *result = '\0';
102                 break;
103         }
104
105         return sz;
106 }
107
108 static struct dm_exception_store_type _transient_type = {
109         .name = "transient",
110         .module = THIS_MODULE,
111         .ctr = transient_ctr,
112         .dtr = transient_dtr,
113         .read_metadata = transient_read_metadata,
114         .prepare_exception = transient_prepare_exception,
115         .commit_exception = transient_commit_exception,
116         .usage = transient_usage,
117         .status = transient_status,
118 };
119
120 static struct dm_exception_store_type _transient_compat_type = {
121         .name = "N",
122         .module = THIS_MODULE,
123         .ctr = transient_ctr,
124         .dtr = transient_dtr,
125         .read_metadata = transient_read_metadata,
126         .prepare_exception = transient_prepare_exception,
127         .commit_exception = transient_commit_exception,
128         .usage = transient_usage,
129         .status = transient_status,
130 };
131
132 int dm_transient_snapshot_init(void)
133 {
134         int r;
135
136         r = dm_exception_store_type_register(&_transient_type);
137         if (r) {
138                 DMWARN("Unable to register transient exception store type");
139                 return r;
140         }
141
142         r = dm_exception_store_type_register(&_transient_compat_type);
143         if (r) {
144                 DMWARN("Unable to register old-style transient "
145                        "exception store type");
146                 dm_exception_store_type_unregister(&_transient_type);
147                 return r;
148         }
149
150         return r;
151 }
152
153 void dm_transient_snapshot_exit(void)
154 {
155         dm_exception_store_type_unregister(&_transient_type);
156         dm_exception_store_type_unregister(&_transient_compat_type);
157 }