/src/barebox/common/state/backend_storage.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2016 Pengutronix, Markus Pargmann <mpa@pengutronix.de> |
3 | | * |
4 | | * This program is free software; you can redistribute it and/or |
5 | | * modify it under the terms of the GNU General Public License |
6 | | * version 2, as published by the Free Software Foundation. |
7 | | * |
8 | | * This program is distributed in the hope that it will be useful, |
9 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | | * GNU General Public License for more details. |
12 | | * |
13 | | */ |
14 | | |
15 | | #include <asm-generic/ioctl.h> |
16 | | #include <fcntl.h> |
17 | | #include <fs.h> |
18 | | #include <libfile.h> |
19 | | #include <linux/kernel.h> |
20 | | #include <linux/list.h> |
21 | | #include <linux/mtd/mtd-abi.h> |
22 | | #include <sys/stat.h> |
23 | | #include <malloc.h> |
24 | | #include <linux/printk.h> |
25 | | |
26 | | #include "state.h" |
27 | | |
28 | | /* |
29 | | * The state framework stores data in so called buckets. A bucket is |
30 | | * exactly one copy of the state we want to store. On flash type media |
31 | | * a bucket corresponds to a single eraseblock. On media which do not |
32 | | * need an erase operation a bucket corresponds to a storage area of |
33 | | * @stridesize bytes. |
34 | | * |
35 | | * For redundancy and to make sure that we have valid data on the storage |
36 | | * device at any time the state framework stores multiple buckets. The strategy |
37 | | * is as follows: |
38 | | * |
39 | | * When loading the state from the storage we iterate over the buckets. We |
40 | | * take the first one we find which has valid crcs. The next step is to |
41 | | * restore consistency between the different buckets. This means rewriting |
42 | | * a bucket when it signalled it needs refresh (i.e. returned -EUCLEAN) |
43 | | * or when contains data different from the bucket we use. |
44 | | * |
45 | | * When the state backend initialized successfully we already restored |
46 | | * consistency which means all buckets contain the same data. This means |
47 | | * when storing a new state we can just write all buckets in order. |
48 | | */ |
49 | | |
50 | | static const unsigned int min_buckets_written = 1; |
51 | | |
52 | | /** |
53 | | * state_storage_write - Writes the given data to the storage |
54 | | * @param storage Storage object |
55 | | * @param buf Buffer with the data |
56 | | * @param len Length of the buffer |
57 | | * @return 0 on success, -errno otherwise |
58 | | * |
59 | | * This function iterates over all registered buckets and executes a write |
60 | | * operation on all of them. Writes are always in the same sequence. This |
61 | | * ensures, that reading in the same sequence will always return the latest |
62 | | * written valid data first. |
63 | | * We try to at least write min_buckets_written. If this fails we return with an |
64 | | * error. |
65 | | */ |
66 | | int state_storage_write(struct state_backend_storage *storage, |
67 | | const void * buf, ssize_t len) |
68 | 0 | { |
69 | 0 | struct state_backend_storage_bucket *bucket; |
70 | 0 | int ret; |
71 | 0 | int buckets_written = 0; |
72 | |
|
73 | 0 | if (storage->readonly) |
74 | 0 | return 0; |
75 | | |
76 | 0 | list_for_each_entry(bucket, &storage->buckets, bucket_list) { |
77 | 0 | ret = bucket->write(bucket, buf, len); |
78 | 0 | if (ret) { |
79 | 0 | dev_warn(storage->dev, "Failed to write state backend bucket, %d\n", |
80 | 0 | ret); |
81 | 0 | } else { |
82 | 0 | ++buckets_written; |
83 | 0 | } |
84 | 0 | } |
85 | |
|
86 | 0 | if (buckets_written >= min_buckets_written) |
87 | 0 | return 0; |
88 | | |
89 | 0 | dev_err(storage->dev, "Failed to write state to at least %d buckets. Successfully written to %d buckets\n", |
90 | 0 | min_buckets_written, buckets_written); |
91 | 0 | return -EIO; |
92 | 0 | } |
93 | | |
94 | | static int bucket_refresh(struct state_backend_storage *storage, |
95 | | struct state_backend_storage_bucket *bucket, void *buf, ssize_t len) |
96 | 96 | { |
97 | 96 | int ret; |
98 | | |
99 | 96 | if (bucket->needs_refresh) |
100 | 0 | goto refresh; |
101 | | |
102 | 96 | if (bucket->len != len) |
103 | 0 | goto refresh; |
104 | | |
105 | 96 | if (memcmp(bucket->buf, buf, len)) |
106 | 0 | goto refresh; |
107 | | |
108 | 96 | return 0; |
109 | | |
110 | 0 | refresh: |
111 | 0 | ret = bucket->write(bucket, buf, len); |
112 | |
|
113 | 0 | if (ret) { |
114 | 0 | dev_warn(storage->dev, "Failed to restore bucket %d@0x%08llx\n", |
115 | 0 | bucket->num, (long long) bucket->offset); |
116 | 0 | } else { |
117 | 0 | dev_info(storage->dev, "restored bucket %d@0x%08llx\n", |
118 | 0 | bucket->num, (long long) bucket->offset); |
119 | 0 | bucket->needs_refresh = 0; |
120 | 0 | } |
121 | |
|
122 | 0 | return ret; |
123 | 96 | } |
124 | | |
125 | | /** |
126 | | * state_storage_read - Reads valid data from the backend storage |
127 | | * @param storage Storage object |
128 | | * @param format Format of the data that is stored |
129 | | * @param magic state magic value |
130 | | * @param buf The newly allocated data area will be stored in this pointer |
131 | | * @param len The resulting length of the buffer |
132 | | * @param flags flags controlling how to load state |
133 | | * @return 0 on success, -errno otherwise. buf and len will be set to valid |
134 | | * values on success. |
135 | | * |
136 | | * This function goes through all buckets and tries to read valid data from |
137 | | * them. The first bucket which returns data that is successfully verified |
138 | | * against the data format is used. To ensure the validity of all bucket copies, |
139 | | * we restore the consistency at the end. |
140 | | */ |
141 | | int state_storage_read(struct state_backend_storage *storage, |
142 | | struct state_backend_format *format, |
143 | | uint32_t magic, void **buf, ssize_t *len, |
144 | | enum state_flags flags) |
145 | 32 | { |
146 | 32 | struct state_backend_storage_bucket *bucket, *bucket_used = NULL; |
147 | 32 | int zerobuckets = 0, totalbuckets = 0; |
148 | 32 | int ret; |
149 | | |
150 | 32 | dev_dbg(storage->dev, "Checking redundant buckets...\n"); |
151 | | /* |
152 | | * Iterate over all buckets. The first valid one we find is the |
153 | | * one we want to use. |
154 | | */ |
155 | 96 | list_for_each_entry(bucket, &storage->buckets, bucket_list) { |
156 | 96 | totalbuckets++; |
157 | | |
158 | 96 | ret = bucket->read(bucket, &bucket->buf, &bucket->len); |
159 | 96 | if (ret == -EUCLEAN) { |
160 | 0 | bucket->needs_refresh = 1; |
161 | 96 | } else if (ret) { |
162 | 0 | if (ret == -ENOMEDIUM) |
163 | 0 | zerobuckets++; |
164 | 0 | continue; |
165 | 0 | } |
166 | | |
167 | | /* |
168 | | * Verify the buffer crcs. The buffer length is passed in the len argument, |
169 | | * .verify overwrites it with the length actually used. |
170 | | */ |
171 | 96 | ret = format->verify(format, magic, bucket->buf, &bucket->len, flags); |
172 | 96 | if (ret == -ENOMEDIUM) |
173 | 0 | zerobuckets++; |
174 | 96 | else if (!ret && !bucket_used) |
175 | 32 | bucket_used = bucket; |
176 | 64 | else if (ret) |
177 | 0 | dev_info(storage->dev, "Ignoring broken bucket %d@0x%08llx...\n", bucket->num, (long long) bucket->offset); |
178 | 96 | } |
179 | | |
180 | 32 | dev_dbg(storage->dev, "Checking redundant buckets finished.\n"); |
181 | | |
182 | 32 | if (!bucket_used) |
183 | 0 | return dev_err_state_init(storage->dev, zerobuckets == totalbuckets ? -ENOMEDIUM : -ENOENT, |
184 | 32 | "no valid state copy in any bucket\n"); |
185 | | |
186 | 32 | dev_info(storage->dev, "Using bucket %d@0x%08llx\n", bucket_used->num, (long long) bucket_used->offset); |
187 | | |
188 | | /* |
189 | | * Restore/refresh all buckets except the one we currently use (in case |
190 | | * it's the only usable bucket at the moment) |
191 | | */ |
192 | 96 | list_for_each_entry(bucket, &storage->buckets, bucket_list) { |
193 | 96 | if (bucket == bucket_used) |
194 | 32 | continue; |
195 | | |
196 | 64 | ret = bucket_refresh(storage, bucket, bucket_used->buf, bucket_used->len); |
197 | | |
198 | | /* Free buffer from the unused buckets */ |
199 | 64 | free(bucket->buf); |
200 | 64 | bucket->buf = NULL; |
201 | 64 | bucket->len = 0; |
202 | 64 | } |
203 | | |
204 | | /* |
205 | | * Restore/refresh the bucket we currently use |
206 | | */ |
207 | 32 | ret = bucket_refresh(storage, bucket_used, bucket_used->buf, bucket_used->len); |
208 | | |
209 | 32 | *buf = bucket_used->buf; |
210 | 32 | *len = bucket_used->len; |
211 | | |
212 | | /* buffer from the used bucket is passed to the caller, do not free */ |
213 | 32 | bucket_used->buf = NULL; |
214 | 32 | bucket_used->len = 0; |
215 | | |
216 | 32 | return 0; |
217 | 32 | } |
218 | | |
219 | | static int mtd_get_meminfo(const char *path, struct mtd_info_user *meminfo) |
220 | 32 | { |
221 | 32 | int fd, ret; |
222 | | |
223 | 32 | fd = open(path, O_RDONLY); |
224 | 32 | if (fd < 0) { |
225 | 0 | pr_err("Failed to open '%s', %d\n", path, fd); |
226 | 0 | return fd; |
227 | 0 | } |
228 | | |
229 | 32 | ret = ioctl(fd, MEMGETINFO, meminfo); |
230 | | |
231 | 32 | close(fd); |
232 | | |
233 | 32 | return ret; |
234 | 32 | } |
235 | | |
236 | | /* Number of buckets that should be used */ |
237 | | static const int desired_buckets = 3; |
238 | | |
239 | | /** |
240 | | * state_storage_mtd_buckets_init - Creates storage buckets for mtd devices |
241 | | * @param storage Storage object |
242 | | * @param meminfo Info about the mtd device |
243 | | * @param circular If false, use non-circular mode to write data that is compatible with the old on-flash format |
244 | | * @return 0 on success, -errno otherwise |
245 | | * |
246 | | * This function iterates over the eraseblocks and creates one bucket on |
247 | | * each eraseblock until we have the number of desired buckets. Bad blocks |
248 | | * will be skipped and the next block will be used. |
249 | | */ |
250 | | static int state_storage_mtd_buckets_init(struct state_backend_storage *storage, |
251 | | struct mtd_info_user *meminfo, bool circular) |
252 | 0 | { |
253 | 0 | struct state_backend_storage_bucket *bucket; |
254 | 0 | ssize_t end = storage->offset + storage->max_size; |
255 | 0 | int n_buckets = 0; |
256 | 0 | off_t offset; |
257 | 0 | ssize_t writesize; |
258 | |
|
259 | 0 | if (!end || end > meminfo->size) |
260 | 0 | end = meminfo->size; |
261 | |
|
262 | 0 | if (!IS_ALIGNED(storage->offset, meminfo->erasesize)) { |
263 | 0 | dev_err(storage->dev, "Offset within the device is not aligned to eraseblocks. Offset is %lld, erasesize %u\n", |
264 | 0 | (long long) storage->offset, meminfo->erasesize); |
265 | 0 | return -EINVAL; |
266 | 0 | } |
267 | | |
268 | 0 | if (circular) |
269 | 0 | writesize = meminfo->writesize; |
270 | 0 | else |
271 | 0 | writesize = meminfo->erasesize; |
272 | |
|
273 | 0 | for (offset = storage->offset; offset < end; offset += meminfo->erasesize) { |
274 | 0 | int ret; |
275 | 0 | unsigned int eraseblock = offset / meminfo->erasesize; |
276 | |
|
277 | 0 | ret = state_backend_bucket_circular_create(storage->dev, storage->path, |
278 | 0 | &bucket, |
279 | 0 | eraseblock, |
280 | 0 | writesize, |
281 | 0 | meminfo); |
282 | 0 | if (ret) |
283 | 0 | continue; |
284 | | |
285 | 0 | bucket->offset = offset; |
286 | 0 | bucket->num = n_buckets; |
287 | |
|
288 | 0 | list_add_tail(&bucket->bucket_list, &storage->buckets); |
289 | 0 | ++n_buckets; |
290 | 0 | if (n_buckets >= desired_buckets) |
291 | 0 | return 0; |
292 | 0 | } |
293 | | |
294 | 0 | if (!n_buckets) { |
295 | 0 | dev_err(storage->dev, "Failed to initialize any state storage bucket\n"); |
296 | 0 | return -EIO; |
297 | 0 | } |
298 | | |
299 | 0 | dev_warn(storage->dev, "Failed to initialize desired amount of buckets, only %d of %d succeeded\n", |
300 | 0 | n_buckets, desired_buckets); |
301 | 0 | return 0; |
302 | 0 | } |
303 | | |
304 | | /** |
305 | | * state_storage_file_buckets_init - Create buckets for a conventional file descriptor |
306 | | * @param storage Storage object |
307 | | * @return 0 on success, -errno otherwise |
308 | | * |
309 | | * direct buckets are simpler than circular buckets and can be used on blockdevices |
310 | | * and mtd devices that don't need erase (MRAM). Also used for EEPROMs. |
311 | | */ |
312 | | static int state_storage_file_buckets_init(struct state_backend_storage *storage) |
313 | 32 | { |
314 | 32 | struct state_backend_storage_bucket *bucket; |
315 | 32 | int ret, n; |
316 | 32 | off_t offset; |
317 | 32 | int n_buckets = 0; |
318 | 32 | uint32_t stridesize = storage->stridesize; |
319 | 32 | size_t max_size = storage->max_size; |
320 | | |
321 | 32 | if (!stridesize) { |
322 | 0 | dev_err(storage->dev, "stridesize unspecified\n"); |
323 | 0 | return -EINVAL; |
324 | 0 | } |
325 | | |
326 | 32 | if (max_size && max_size < desired_buckets * stridesize) { |
327 | 0 | dev_err(storage->dev, "device is too small to hold %d copies\n", desired_buckets); |
328 | 0 | return -EINVAL; |
329 | 0 | } |
330 | | |
331 | 128 | for (n = 0; n < desired_buckets; n++) { |
332 | 96 | offset = storage->offset + n * stridesize; |
333 | 96 | ret = state_backend_bucket_direct_create(storage->dev, storage->path, |
334 | 96 | &bucket, offset, |
335 | 96 | stridesize, storage->readonly); |
336 | 96 | if (ret) { |
337 | 0 | dev_warn(storage->dev, "Failed to create direct bucket at '%s' offset %lld\n", |
338 | 0 | storage->path, (long long) offset); |
339 | 0 | continue; |
340 | 0 | } |
341 | | |
342 | 96 | bucket->offset = offset; |
343 | 96 | bucket->num = n_buckets; |
344 | | |
345 | 96 | list_add_tail(&bucket->bucket_list, &storage->buckets); |
346 | 96 | ++n_buckets; |
347 | 96 | } |
348 | | |
349 | 32 | if (!n_buckets) { |
350 | 0 | dev_err(storage->dev, "Failed to initialize any state direct storage bucket\n"); |
351 | 0 | return -EIO; |
352 | 0 | } |
353 | | |
354 | 32 | if (n_buckets < desired_buckets) |
355 | 32 | dev_warn(storage->dev, "Failed to initialize desired amount of direct buckets, only %d of %d succeeded\n", |
356 | 32 | n_buckets, desired_buckets); |
357 | | |
358 | 32 | return 0; |
359 | 32 | } |
360 | | |
361 | | |
362 | | /** |
363 | | * state_storage_init - Init backend storage |
364 | | * @param path Path to the backend storage file |
365 | | * @param dev_offset Offset in the device to start writing at. |
366 | | * @param max_size Maximum size of the data. May be 0 for infinite. |
367 | | * @param stridesize Distance between two copies of the data. Not relevant for MTD |
368 | | * @param storagetype Type of the storage backend. May be NULL for autoselection. |
369 | | * @return 0 on success, -errno otherwise |
370 | | * |
371 | | * If the backend memory needs to be erased prior a write, the @b storagetype |
372 | | * defaults to 'circular' storage backend type, for backend memories like RAMs |
373 | | * or EEPROMs @b storagetype defaults to the 'direct' storage backend type. |
374 | | */ |
375 | | int state_storage_init(struct state *state, const char *path, |
376 | | off_t offset, size_t max_size, uint32_t stridesize, |
377 | | const char *storagetype) |
378 | 32 | { |
379 | 32 | struct state_backend_storage *storage = &state->storage; |
380 | 32 | int ret = -ENODEV; |
381 | 32 | struct mtd_info_user meminfo; |
382 | | |
383 | 32 | INIT_LIST_HEAD(&storage->buckets); |
384 | 32 | storage->dev = &state->dev; |
385 | 32 | storage->name = storagetype; |
386 | 32 | storage->stridesize = stridesize; |
387 | 32 | storage->offset = offset; |
388 | 32 | storage->max_size = max_size; |
389 | 32 | storage->path = xstrdup_const(path); |
390 | | |
391 | 32 | if (IS_ENABLED(CONFIG_MTD)) |
392 | 32 | ret = mtd_get_meminfo(path, &meminfo); |
393 | | |
394 | 32 | if (!ret && !(meminfo.flags & MTD_NO_ERASE)) { |
395 | 0 | bool circular; |
396 | 0 | if (!storagetype || !strcmp(storagetype, "circular")) { |
397 | 0 | storage->name = "circular"; |
398 | 0 | circular = true; |
399 | 0 | } else if (!strcmp(storagetype, "noncircular")) { |
400 | 0 | dev_warn(storage->dev, "using old format circular storage type.\n"); |
401 | 0 | circular = false; |
402 | 0 | } else { |
403 | 0 | dev_dbg(storage->dev, "unknown storage type '%s'\n", storagetype); |
404 | 0 | return -EINVAL; |
405 | 0 | } |
406 | 0 | return state_storage_mtd_buckets_init(storage, &meminfo, circular); |
407 | 32 | } else { |
408 | 32 | return state_storage_file_buckets_init(storage); |
409 | 32 | } |
410 | 32 | } |
411 | | |
412 | | void state_storage_set_readonly(struct state_backend_storage *storage) |
413 | 0 | { |
414 | 0 | storage->readonly = true; |
415 | 0 | } |
416 | | |
417 | | /** |
418 | | * state_storage_free - Free backend storage |
419 | | * @param storage Storage object |
420 | | */ |
421 | | void state_storage_free(struct state_backend_storage *storage) |
422 | 0 | { |
423 | 0 | struct state_backend_storage_bucket *bucket; |
424 | 0 | struct state_backend_storage_bucket *bucket_tmp; |
425 | |
|
426 | 0 | if (!storage->buckets.next) |
427 | 0 | return; |
428 | | |
429 | 0 | list_for_each_entry_safe(bucket, bucket_tmp, &storage->buckets, |
430 | 0 | bucket_list) { |
431 | 0 | list_del(&bucket->bucket_list); |
432 | 0 | bucket->free(bucket); |
433 | 0 | } |
434 | |
|
435 | 0 | free_const(storage->path); |
436 | 0 | } |