summaryrefslogtreecommitdiffstats
path: root/src/barebox-state/backend_bucket_circular.c
blob: 135a43ab278600730f2636cafe66baada62cd366 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
 * Copyright (C) 2016 Pengutronix, Markus Pargmann <mpa@pengutronix.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <asm-generic/ioctl.h>
#include <common.h>
#include <fcntl.h>
#include <fs.h>
#include <libfile.h>
#include <linux/kernel.h>
#include <linux/mtd/mtd-abi.h>
#include <malloc.h>
#include <mtd/mtd-peb.h>
#include <string.h>

#include "state.h"

/*
 * The circular backend bucket code. The circular backend bucket is intended
 * for mtd devices which need an erase operation.
 *
 * Erasing blocks is an operation that should be avoided. On NOR flashes erasing
 * blocks is very time consuming and on NAND flashes each block only has a limited
 * number of erase cycles allowed. For this reason we continuously write more data
 * into each eraseblock and only erase it when no more free space is available.
 * Don't confuse these multiple writes into a single eraseblock with buckets. A bucket
 * is the whole eraseblock, we just happen to reuse the same bucket for storing
 * new data.
 *
 * If your device is a mtd device, but does not have eraseblocks, like MRAMs, then
 * the direct bucket is used instead.
 */
struct state_backend_storage_bucket_circular {
	struct state_backend_storage_bucket bucket;

	unsigned int eraseblock; /* Which eraseblock is used */
	ssize_t writesize; /* Alignment of writes */
	ssize_t max_size; /* Maximum size of this bucket */

	off_t write_area; /* Start of the write area (relative offset) */
	uint32_t last_written_length; /* Size of the data written in the storage */

#ifdef __BAREBOX__
	struct mtd_info *mtd; /* mtd info (used for io in Barebox)*/
#else
	struct mtd_info_user *mtd;
	int fd;
#endif

	/* For outputs */
	struct device_d *dev;
};

/*
 * The metadata will be written directly before writesize aligned offsets.
 * When searching backwards through the pages it allows us to find the
 * beginning of the data.
 */
struct __attribute__((__packed__)) state_backend_storage_bucket_circular_meta {
	uint32_t magic;
	uint32_t written_length;
};

static const uint32_t circular_magic = 0x14fa2d02;
static const uint8_t free_pattern = 0xff;

static inline struct state_backend_storage_bucket_circular
    *get_bucket_circular(struct state_backend_storage_bucket *bucket)
{
	return container_of(bucket,
			    struct state_backend_storage_bucket_circular,
			    bucket);
}

#ifdef __BAREBOX__
static int state_mtd_peb_read(struct state_backend_storage_bucket_circular *circ,
			      void *buf, int offset, int len)
{
	int ret;

	ret = mtd_peb_read(circ->mtd, buf, circ->eraseblock, offset, len);
	if (ret == -EBADMSG) {
		ret = mtd_peb_torture(circ->mtd, circ->eraseblock);
		if (ret == -EIO) {
			dev_err(circ->dev, "Tortured eraseblock failed and is marked bad now, PEB %u\n",
				circ->eraseblock);
			return -EIO;
		} else if (ret < 0) {
			dev_err(circ->dev, "Failed to torture eraseblock, %d\n",
				ret);
			return ret;
		}
		/*
		 * Fill with invalid data so that the next write is done
		 * behind this area
		 */
		memset(buf, 0, len);
		ret = -EUCLEAN;
		circ->write_area = 0;
		dev_dbg(circ->dev, "PEB %u has ECC error, forcing rewrite\n",
			circ->eraseblock);
	} else if (ret == -EUCLEAN) {
		dev_dbg(circ->dev, "PEB %u is unclean, forcing rewrite\n",
			circ->eraseblock);
	} else if (ret < 0) {
		dev_err(circ->dev, "Failed to read PEB %u, %d\n",
			circ->eraseblock, ret);
	}

	return ret;
}

static int state_mtd_peb_write(struct state_backend_storage_bucket_circular *circ,
			       const void *buf, int offset, int len)
{
	int ret;

	ret = mtd_peb_write(circ->mtd, buf, circ->eraseblock, offset, len);
	if (ret == -EBADMSG) {
		ret = mtd_peb_torture(circ->mtd, circ->eraseblock);
		if (ret == -EIO) {
			dev_err(circ->dev, "Tortured eraseblock failed and is marked bad now, PEB %u\n",
				circ->eraseblock);
			return -EIO;
		} else if (ret < 0) {
			dev_err(circ->dev, "Failed to torture eraseblock, %d\n",
				ret);
			return ret;
		}
		ret = -EUCLEAN;
	} else if (ret < 0 && ret != -EUCLEAN) {
		dev_err(circ->dev, "Failed to write PEB %u, %d\n",
			circ->eraseblock, ret);
	}

	return ret;
}

static int state_mtd_peb_erase(struct state_backend_storage_bucket_circular *circ)
{
	return mtd_peb_erase(circ->mtd, circ->eraseblock);
}
#else
static int state_mtd_peb_read(struct state_backend_storage_bucket_circular *circ,
			      void *buf, int suboffset, int len)
{
	int ret;
	off_t offset = suboffset;
	struct mtd_ecc_stats stat1, stat2;
	bool nostats = false;

	offset += (off_t)circ->eraseblock * circ->mtd->erasesize;

	ret = lseek(circ->fd, offset, SEEK_SET);
	if (ret < 0) {
		dev_err(circ->dev, "Failed to set circular read position to %lld, %d\n",
			offset, ret);
		return ret;
	}

	dev_dbg(circ->dev, "Read state from %ld length %zd\n", offset,
		len);


	ret = read_full(circ->fd, buf, len);
	if (ret < 0) {
		dev_err(circ->dev, "Failed to read circular storage len %zd, %d\n",
			len, ret);
		free(buf);
		return ret;
	}

	return 0;
}

static int state_mtd_peb_write(struct state_backend_storage_bucket_circular *circ,
			       const void *buf, int suboffset, int len)
{
	int ret;
	off_t offset = suboffset;

	offset += circ->eraseblock * circ->mtd->erasesize;

	ret = lseek(circ->fd, offset, SEEK_SET);
	if (ret < 0) {
		dev_err(circ->dev, "Failed to set position for circular write %ld, %d\n",
			offset, ret);
		return ret;
	}

	ret = write_full(circ->fd, buf, len);
	if (ret < 0) {
		dev_err(circ->dev, "Failed to write circular to %ld length %zd, %d\n",
			offset, len, ret);
		return ret;
	}

	/*
	 * We keep the fd open, so flush is necessary. We ignore the return
	 * value as flush is currently not supported for mtd under linux.
	 */
	flush(circ->fd);

	dev_dbg(circ->dev, "Written state to offset %ld length %zd data length %zd\n",
		offset, len, len);

	return 0;
}

static int state_mtd_peb_erase(struct state_backend_storage_bucket_circular *circ)
{
	return erase(circ->fd, circ->mtd->erasesize,
		     (off_t)circ->eraseblock * circ->mtd->erasesize);
}
#endif

static int state_backend_bucket_circular_read(struct state_backend_storage_bucket *bucket,
					      void ** buf_out,
					      ssize_t * len_out)
{
	struct state_backend_storage_bucket_circular *circ =
	    get_bucket_circular(bucket);
	ssize_t read_len;
	off_t offset;
	void *buf;
	int ret;

	/* Storage is empty */
	if (circ->write_area == 0)
		return -ENODATA;

	if (!circ->last_written_length) {
		/*
		 * Last write did not contain length information, assuming old
		 * state and reading from the beginning.
		 */
		offset = 0;
		read_len = min(circ->write_area, (off_t)(circ->max_size -
			       sizeof(struct state_backend_storage_bucket_circular_meta)));
		circ->write_area = 0;
		dev_dbg(circ->dev, "Detected old on-storage format\n");
	} else if (circ->last_written_length > circ->write_area
		   || !IS_ALIGNED(circ->last_written_length, circ->writesize)) {
		circ->write_area = 0;
		dev_err(circ->dev, "Error, invalid number of bytes written last time %d\n",
			circ->last_written_length);
		return -EINVAL;
	} else {
		/*
		 * Normally we read at the end of the non-free area. The length
		 * of the read is then what we read from the meta data
		 * (last_written_length)
		 */
		read_len = circ->last_written_length;
		offset = circ->write_area - read_len;
	}

	buf = xmalloc(read_len);
	if (!buf)
		return -ENOMEM;

	dev_dbg(circ->dev, "Read state from PEB %u global offset %ld length %zd\n",
		circ->eraseblock, offset, read_len);

	ret = state_mtd_peb_read(circ, buf, offset, read_len);
	if (ret < 0 && ret != -EUCLEAN) {
		dev_err(circ->dev, "Failed to read circular storage len %zd, %d\n",
			read_len, ret);
		free(buf);
		return ret;
	}

	*buf_out = buf;
	*len_out = read_len - sizeof(struct state_backend_storage_bucket_circular_meta);

	return ret;
}

static int state_backend_bucket_circular_write(struct state_backend_storage_bucket *bucket,
					       const void * buf,
					       ssize_t len)
{
	struct state_backend_storage_bucket_circular *circ =
	    get_bucket_circular(bucket);
	off_t offset;
	struct state_backend_storage_bucket_circular_meta *meta;
	uint32_t written_length = ALIGN(len + sizeof(*meta), circ->writesize);
	int ret;
	void *write_buf;

	if (written_length > circ->max_size) {
		dev_err(circ->dev, "Error, state data too big to be written, to write: %zd, writesize: %zd, length: %zd, available: %zd\n",
			written_length, circ->writesize, len, circ->max_size);
		return -E2BIG;
	}

	/*
	 * We need zero initialization so that our data comparisons don't show
	 * random changes
	 */
	write_buf = xzalloc(written_length);
	if (!write_buf)
		return -ENOMEM;

	memcpy(write_buf, buf, len);
	meta = (struct state_backend_storage_bucket_circular_meta *)
			(write_buf + written_length - sizeof(*meta));
	meta->magic = circular_magic;
	meta->written_length = written_length;

	if (circ->write_area + written_length >= circ->max_size) {
		circ->write_area = 0;
	}
	/*
	 * If the write area is at the beginning of the eraseblock, erase it and write
	 * at offset 0. As we only erase right before writing there are no
	 * conditions where we regularly erase a block multiple times without
	 * writing.
	 */
	if (circ->write_area == 0) {
		dev_dbg(circ->dev, "Erasing PEB %u\n", circ->eraseblock);
		ret = state_mtd_peb_erase(circ);
		if (ret) {
			dev_err(circ->dev, "Failed to erase PEB %u\n",
				circ->eraseblock);
			goto out_free;
		}
	}

	offset = circ->write_area;

	/*
	 * Update write_area before writing. The write operation may put
	 * arbitrary amount of the data into the storage before failing. In this
	 * case we want to start after that area.
	 */
	circ->write_area += written_length;

	ret = state_mtd_peb_write(circ, write_buf, offset, written_length);
	if (ret < 0 && ret != -EUCLEAN) {
		dev_err(circ->dev, "Failed to write circular to %ld length %zd, %d\n",
			offset, written_length, ret);
		goto out_free;
	}

	dev_dbg(circ->dev, "Written state to PEB %u offset %ld length %zd data length %zd\n",
		circ->eraseblock, offset, written_length, len);

out_free:
	free(write_buf);
	return ret;
}

/**
 * state_backend_bucket_circular_init - Initialize circular bucket
 * @param bucket
 * @return 0 on success, -errno otherwise
 *
 * This function searches for the beginning of the written area from the end of
 * the MTD device. This way it knows where the data ends and where the free area
 * starts.
 */
static int state_backend_bucket_circular_init(
		struct state_backend_storage_bucket *bucket)
{
	struct state_backend_storage_bucket_circular *circ =
	    get_bucket_circular(bucket);
	int sub_offset;
	uint32_t written_length = 0;
	uint8_t *buf;
	int ret;

	buf = xmalloc(circ->max_size);
	if (!buf)
		return -ENOMEM;

	ret = state_mtd_peb_read(circ, buf, 0, circ->max_size);
	if (ret && ret != -EUCLEAN)
		return ret;

	for (sub_offset = circ->max_size - circ->writesize; sub_offset >= 0;
	     sub_offset -= circ->writesize) {
		ret = mtd_buf_all_ff(buf + sub_offset, circ->writesize);
		if (!ret) {
			struct state_backend_storage_bucket_circular_meta *meta;

			meta = (struct state_backend_storage_bucket_circular_meta *)
					(buf + sub_offset + circ->writesize - sizeof(*meta));

			if (meta->magic != circular_magic)
				written_length = 0;
			else
				written_length = meta->written_length;

			break;
		}
	}

	circ->write_area = sub_offset + circ->writesize;
	circ->last_written_length = written_length;

	free(buf);

	return 0;
}

static void state_backend_bucket_circular_free(struct
					       state_backend_storage_bucket
					       *bucket)
{
	struct state_backend_storage_bucket_circular *circ =
	    get_bucket_circular(bucket);

	free(circ);
}

#ifdef __BAREBOX__
static int bucket_circular_is_block_bad(struct state_backend_storage_bucket_circular *circ)
{
	int ret;

	ret = mtd_peb_is_bad(circ->mtd, circ->eraseblock);
	if (ret < 0)
		dev_err(circ->dev, "Failed to determine whether eraseblock %u is bad, %d\n",
			circ->eraseblock, ret);

	return ret;
}
#else
static int bucket_circular_is_block_bad(struct state_backend_storage_bucket_circular *circ)
{
	int ret;
	loff_t offs = circ->eraseblock * circ->mtd->erasesize;

	ret = ioctl(circ->fd, MEMGETBADBLOCK, &offs);
	if (ret < 0)
		dev_err(circ->dev, "Failed to use ioctl to check for bad block at offset %ld, %d\n",
			offs, ret);

	return ret;
}
#endif

int state_backend_bucket_circular_create(struct device_d *dev, const char *path,
					 struct state_backend_storage_bucket **bucket,
					 unsigned int eraseblock,
					 ssize_t writesize,
					 struct mtd_info_user *mtd_uinfo)
{
	struct state_backend_storage_bucket_circular *circ;
	int ret;

	if (writesize < 8)
		writesize = 8;

	circ = xzalloc(sizeof(*circ));
	circ->eraseblock = eraseblock;
	circ->writesize = writesize;
	circ->max_size = mtd_uinfo->erasesize;
	circ->dev = dev;

#ifdef __BAREBOX__
	circ->mtd = mtd_uinfo->mtd;
#else
	circ->mtd = xzalloc(sizeof(*mtd_uinfo));
	memcpy(circ->mtd, mtd_uinfo, sizeof(*mtd_uinfo));
	circ->fd = open(path, O_RDWR);
	if (circ->fd < 0) {
		pr_err("Failed to open circular bucket '%s'\n", path);
		return -errno;
	}
#endif

	ret = bucket_circular_is_block_bad(circ);
	if (ret) {
		dev_info(dev, "Not using eraseblock %u, it is marked as bad (%d)\n",
			 circ->eraseblock, ret);
		ret = -EIO;
		goto out_free;
	}

	circ->bucket.read = state_backend_bucket_circular_read;
	circ->bucket.write = state_backend_bucket_circular_write;
	circ->bucket.free = state_backend_bucket_circular_free;
	*bucket = &circ->bucket;

	ret = state_backend_bucket_circular_init(*bucket);
	if (ret)
		goto out_free;

	return 0;

out_free:
#ifndef __BAREBOX__
	close(circ->fd);
#endif
	free(circ);

	return ret;
}