1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) International Business Machines Corp., 2006
4  *
5  * Author: Artem Bityutskiy (Битюцкий Артём)
6  */
7 
8 /*
9  * The UBI Eraseblock Association (EBA) sub-system.
10  *
11  * This sub-system is responsible for I/O to/from logical eraseblock.
12  *
13  * Although in this implementation the EBA table is fully kept and managed in
14  * RAM, which assumes poor scalability, it might be (partially) maintained on
15  * flash in future implementations.
16  *
17  * The EBA sub-system implements per-logical eraseblock locking. Before
18  * accessing a logical eraseblock it is locked for reading or writing. The
19  * per-logical eraseblock locking is implemented by means of the lock tree. The
20  * lock tree is an RB-tree which refers all the currently locked logical
21  * eraseblocks. The lock tree elements are &struct ubi_ltree_entry objects.
22  * They are indexed by (@vol_id, @lnum) pairs.
23  *
24  * EBA also maintains the global sequence counter which is incremented each
25  * time a logical eraseblock is mapped to a physical eraseblock and it is
26  * stored in the volume identifier header. This means that each VID header has
27  * a unique sequence number. The sequence number is only increased an we assume
28  * 64 bits is enough to never overflow.
29  */
30 
31 #ifndef __UBOOT__
32 #include <log.h>
33 #include <dm/devres.h>
34 #include <linux/slab.h>
35 #include <linux/crc32.h>
36 #include <u-boot/crc.h>
37 #else
38 #include <ubi_uboot.h>
39 #endif
40 
41 #include <linux/err.h>
42 #include "ubi.h"
43 
44 /* Number of physical eraseblocks reserved for atomic LEB change operation */
45 #define EBA_RESERVED_PEBS 1
46 
47 /**
48  * next_sqnum - get next sequence number.
49  * @ubi: UBI device description object
50  *
51  * This function returns next sequence number to use, which is just the current
52  * global sequence counter value. It also increases the global sequence
53  * counter.
54  */
ubi_next_sqnum(struct ubi_device * ubi)55 unsigned long long ubi_next_sqnum(struct ubi_device *ubi)
56 {
57 	unsigned long long sqnum;
58 
59 	spin_lock(&ubi->ltree_lock);
60 	sqnum = ubi->global_sqnum++;
61 	spin_unlock(&ubi->ltree_lock);
62 
63 	return sqnum;
64 }
65 
66 /**
67  * ubi_get_compat - get compatibility flags of a volume.
68  * @ubi: UBI device description object
69  * @vol_id: volume ID
70  *
71  * This function returns compatibility flags for an internal volume. User
72  * volumes have no compatibility flags, so %0 is returned.
73  */
ubi_get_compat(const struct ubi_device * ubi,int vol_id)74 static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)
75 {
76 	if (vol_id == UBI_LAYOUT_VOLUME_ID)
77 		return UBI_LAYOUT_VOLUME_COMPAT;
78 	return 0;
79 }
80 
81 /**
82  * ltree_lookup - look up the lock tree.
83  * @ubi: UBI device description object
84  * @vol_id: volume ID
85  * @lnum: logical eraseblock number
86  *
87  * This function returns a pointer to the corresponding &struct ubi_ltree_entry
88  * object if the logical eraseblock is locked and %NULL if it is not.
89  * @ubi->ltree_lock has to be locked.
90  */
ltree_lookup(struct ubi_device * ubi,int vol_id,int lnum)91 static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
92 					    int lnum)
93 {
94 	struct rb_node *p;
95 
96 	p = ubi->ltree.rb_node;
97 	while (p) {
98 		struct ubi_ltree_entry *le;
99 
100 		le = rb_entry(p, struct ubi_ltree_entry, rb);
101 
102 		if (vol_id < le->vol_id)
103 			p = p->rb_left;
104 		else if (vol_id > le->vol_id)
105 			p = p->rb_right;
106 		else {
107 			if (lnum < le->lnum)
108 				p = p->rb_left;
109 			else if (lnum > le->lnum)
110 				p = p->rb_right;
111 			else
112 				return le;
113 		}
114 	}
115 
116 	return NULL;
117 }
118 
119 /**
120  * ltree_add_entry - add new entry to the lock tree.
121  * @ubi: UBI device description object
122  * @vol_id: volume ID
123  * @lnum: logical eraseblock number
124  *
125  * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the
126  * lock tree. If such entry is already there, its usage counter is increased.
127  * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation
128  * failed.
129  */
ltree_add_entry(struct ubi_device * ubi,int vol_id,int lnum)130 static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,
131 					       int vol_id, int lnum)
132 {
133 	struct ubi_ltree_entry *le, *le1, *le_free;
134 
135 	le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);
136 	if (!le)
137 		return ERR_PTR(-ENOMEM);
138 
139 	le->users = 0;
140 	init_rwsem(&le->mutex);
141 	le->vol_id = vol_id;
142 	le->lnum = lnum;
143 
144 	spin_lock(&ubi->ltree_lock);
145 	le1 = ltree_lookup(ubi, vol_id, lnum);
146 
147 	if (le1) {
148 		/*
149 		 * This logical eraseblock is already locked. The newly
150 		 * allocated lock entry is not needed.
151 		 */
152 		le_free = le;
153 		le = le1;
154 	} else {
155 		struct rb_node **p, *parent = NULL;
156 
157 		/*
158 		 * No lock entry, add the newly allocated one to the
159 		 * @ubi->ltree RB-tree.
160 		 */
161 		le_free = NULL;
162 
163 		p = &ubi->ltree.rb_node;
164 		while (*p) {
165 			parent = *p;
166 			le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
167 
168 			if (vol_id < le1->vol_id)
169 				p = &(*p)->rb_left;
170 			else if (vol_id > le1->vol_id)
171 				p = &(*p)->rb_right;
172 			else {
173 				ubi_assert(lnum != le1->lnum);
174 				if (lnum < le1->lnum)
175 					p = &(*p)->rb_left;
176 				else
177 					p = &(*p)->rb_right;
178 			}
179 		}
180 
181 		rb_link_node(&le->rb, parent, p);
182 		rb_insert_color(&le->rb, &ubi->ltree);
183 	}
184 	le->users += 1;
185 	spin_unlock(&ubi->ltree_lock);
186 
187 	kfree(le_free);
188 	return le;
189 }
190 
191 /**
192  * leb_read_lock - lock logical eraseblock for reading.
193  * @ubi: UBI device description object
194  * @vol_id: volume ID
195  * @lnum: logical eraseblock number
196  *
197  * This function locks a logical eraseblock for reading. Returns zero in case
198  * of success and a negative error code in case of failure.
199  */
leb_read_lock(struct ubi_device * ubi,int vol_id,int lnum)200 static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
201 {
202 	struct ubi_ltree_entry *le;
203 
204 	le = ltree_add_entry(ubi, vol_id, lnum);
205 	if (IS_ERR(le))
206 		return PTR_ERR(le);
207 	down_read(&le->mutex);
208 	return 0;
209 }
210 
211 /**
212  * leb_read_unlock - unlock logical eraseblock.
213  * @ubi: UBI device description object
214  * @vol_id: volume ID
215  * @lnum: logical eraseblock number
216  */
leb_read_unlock(struct ubi_device * ubi,int vol_id,int lnum)217 static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
218 {
219 	struct ubi_ltree_entry *le;
220 
221 	spin_lock(&ubi->ltree_lock);
222 	le = ltree_lookup(ubi, vol_id, lnum);
223 	le->users -= 1;
224 	ubi_assert(le->users >= 0);
225 	up_read(&le->mutex);
226 	if (le->users == 0) {
227 		rb_erase(&le->rb, &ubi->ltree);
228 		kfree(le);
229 	}
230 	spin_unlock(&ubi->ltree_lock);
231 }
232 
233 /**
234  * leb_write_lock - lock logical eraseblock for writing.
235  * @ubi: UBI device description object
236  * @vol_id: volume ID
237  * @lnum: logical eraseblock number
238  *
239  * This function locks a logical eraseblock for writing. Returns zero in case
240  * of success and a negative error code in case of failure.
241  */
leb_write_lock(struct ubi_device * ubi,int vol_id,int lnum)242 static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
243 {
244 	struct ubi_ltree_entry *le;
245 
246 	le = ltree_add_entry(ubi, vol_id, lnum);
247 	if (IS_ERR(le))
248 		return PTR_ERR(le);
249 	down_write(&le->mutex);
250 	return 0;
251 }
252 
253 /**
254  * leb_write_lock - lock logical eraseblock for writing.
255  * @ubi: UBI device description object
256  * @vol_id: volume ID
257  * @lnum: logical eraseblock number
258  *
259  * This function locks a logical eraseblock for writing if there is no
260  * contention and does nothing if there is contention. Returns %0 in case of
261  * success, %1 in case of contention, and and a negative error code in case of
262  * failure.
263  */
leb_write_trylock(struct ubi_device * ubi,int vol_id,int lnum)264 static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
265 {
266 	struct ubi_ltree_entry *le;
267 
268 	le = ltree_add_entry(ubi, vol_id, lnum);
269 	if (IS_ERR(le))
270 		return PTR_ERR(le);
271 	if (down_write_trylock(&le->mutex))
272 		return 0;
273 
274 	/* Contention, cancel */
275 	spin_lock(&ubi->ltree_lock);
276 	le->users -= 1;
277 	ubi_assert(le->users >= 0);
278 	if (le->users == 0) {
279 		rb_erase(&le->rb, &ubi->ltree);
280 		kfree(le);
281 	}
282 	spin_unlock(&ubi->ltree_lock);
283 
284 	return 1;
285 }
286 
287 /**
288  * leb_write_unlock - unlock logical eraseblock.
289  * @ubi: UBI device description object
290  * @vol_id: volume ID
291  * @lnum: logical eraseblock number
292  */
leb_write_unlock(struct ubi_device * ubi,int vol_id,int lnum)293 static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
294 {
295 	struct ubi_ltree_entry *le;
296 
297 	spin_lock(&ubi->ltree_lock);
298 	le = ltree_lookup(ubi, vol_id, lnum);
299 	le->users -= 1;
300 	ubi_assert(le->users >= 0);
301 	up_write(&le->mutex);
302 	if (le->users == 0) {
303 		rb_erase(&le->rb, &ubi->ltree);
304 		kfree(le);
305 	}
306 	spin_unlock(&ubi->ltree_lock);
307 }
308 
309 /**
310  * ubi_eba_unmap_leb - un-map logical eraseblock.
311  * @ubi: UBI device description object
312  * @vol: volume description object
313  * @lnum: logical eraseblock number
314  *
315  * This function un-maps logical eraseblock @lnum and schedules corresponding
316  * physical eraseblock for erasure. Returns zero in case of success and a
317  * negative error code in case of failure.
318  */
ubi_eba_unmap_leb(struct ubi_device * ubi,struct ubi_volume * vol,int lnum)319 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
320 		      int lnum)
321 {
322 	int err, pnum, vol_id = vol->vol_id;
323 
324 	if (ubi->ro_mode)
325 		return -EROFS;
326 
327 	err = leb_write_lock(ubi, vol_id, lnum);
328 	if (err)
329 		return err;
330 
331 	pnum = vol->eba_tbl[lnum];
332 	if (pnum < 0)
333 		/* This logical eraseblock is already unmapped */
334 		goto out_unlock;
335 
336 	dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
337 
338 	down_read(&ubi->fm_eba_sem);
339 	vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
340 	up_read(&ubi->fm_eba_sem);
341 	err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0);
342 
343 out_unlock:
344 	leb_write_unlock(ubi, vol_id, lnum);
345 	return err;
346 }
347 
348 /**
349  * ubi_eba_read_leb - read data.
350  * @ubi: UBI device description object
351  * @vol: volume description object
352  * @lnum: logical eraseblock number
353  * @buf: buffer to store the read data
354  * @offset: offset from where to read
355  * @len: how many bytes to read
356  * @check: data CRC check flag
357  *
358  * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
359  * bytes. The @check flag only makes sense for static volumes and forces
360  * eraseblock data CRC checking.
361  *
362  * In case of success this function returns zero. In case of a static volume,
363  * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
364  * returned for any volume type if an ECC error was detected by the MTD device
365  * driver. Other negative error cored may be returned in case of other errors.
366  */
ubi_eba_read_leb(struct ubi_device * ubi,struct ubi_volume * vol,int lnum,void * buf,int offset,int len,int check)367 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
368 		     void *buf, int offset, int len, int check)
369 {
370 	int err, pnum, scrub = 0, vol_id = vol->vol_id;
371 	struct ubi_vid_hdr *vid_hdr;
372 	uint32_t uninitialized_var(crc);
373 
374 	err = leb_read_lock(ubi, vol_id, lnum);
375 	if (err)
376 		return err;
377 
378 	pnum = vol->eba_tbl[lnum];
379 	if (pnum < 0) {
380 		/*
381 		 * The logical eraseblock is not mapped, fill the whole buffer
382 		 * with 0xFF bytes. The exception is static volumes for which
383 		 * it is an error to read unmapped logical eraseblocks.
384 		 */
385 		dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
386 			len, offset, vol_id, lnum);
387 		leb_read_unlock(ubi, vol_id, lnum);
388 		ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
389 		memset(buf, 0xFF, len);
390 		return 0;
391 	}
392 
393 	dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
394 		len, offset, vol_id, lnum, pnum);
395 
396 	if (vol->vol_type == UBI_DYNAMIC_VOLUME)
397 		check = 0;
398 
399 retry:
400 	if (check) {
401 		vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
402 		if (!vid_hdr) {
403 			err = -ENOMEM;
404 			goto out_unlock;
405 		}
406 
407 		err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
408 		if (err && err != UBI_IO_BITFLIPS) {
409 			if (err > 0) {
410 				/*
411 				 * The header is either absent or corrupted.
412 				 * The former case means there is a bug -
413 				 * switch to read-only mode just in case.
414 				 * The latter case means a real corruption - we
415 				 * may try to recover data. FIXME: but this is
416 				 * not implemented.
417 				 */
418 				if (err == UBI_IO_BAD_HDR_EBADMSG ||
419 				    err == UBI_IO_BAD_HDR) {
420 					ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d",
421 						 pnum, vol_id, lnum);
422 					err = -EBADMSG;
423 				} else {
424 					err = -EINVAL;
425 					ubi_ro_mode(ubi);
426 				}
427 			}
428 			goto out_free;
429 		} else if (err == UBI_IO_BITFLIPS)
430 			scrub = 1;
431 
432 		ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
433 		ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
434 
435 		crc = be32_to_cpu(vid_hdr->data_crc);
436 		ubi_free_vid_hdr(ubi, vid_hdr);
437 	}
438 
439 	err = ubi_io_read_data(ubi, buf, pnum, offset, len);
440 	if (err) {
441 		if (err == UBI_IO_BITFLIPS)
442 			scrub = 1;
443 		else if (mtd_is_eccerr(err)) {
444 			if (vol->vol_type == UBI_DYNAMIC_VOLUME)
445 				goto out_unlock;
446 			scrub = 1;
447 			if (!check) {
448 				ubi_msg(ubi, "force data checking");
449 				check = 1;
450 				goto retry;
451 			}
452 		} else
453 			goto out_unlock;
454 	}
455 
456 	if (check) {
457 		uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
458 		if (crc1 != crc) {
459 			ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x",
460 				 crc1, crc);
461 			err = -EBADMSG;
462 			goto out_unlock;
463 		}
464 	}
465 
466 	if (scrub)
467 		err = ubi_wl_scrub_peb(ubi, pnum);
468 
469 	leb_read_unlock(ubi, vol_id, lnum);
470 	return err;
471 
472 out_free:
473 	ubi_free_vid_hdr(ubi, vid_hdr);
474 out_unlock:
475 	leb_read_unlock(ubi, vol_id, lnum);
476 	return err;
477 }
478 
479 #ifndef __UBOOT__
480 /**
481  * ubi_eba_read_leb_sg - read data into a scatter gather list.
482  * @ubi: UBI device description object
483  * @vol: volume description object
484  * @lnum: logical eraseblock number
485  * @sgl: UBI scatter gather list to store the read data
486  * @offset: offset from where to read
487  * @len: how many bytes to read
488  * @check: data CRC check flag
489  *
490  * This function works exactly like ubi_eba_read_leb(). But instead of
491  * storing the read data into a buffer it writes to an UBI scatter gather
492  * list.
493  */
ubi_eba_read_leb_sg(struct ubi_device * ubi,struct ubi_volume * vol,struct ubi_sgl * sgl,int lnum,int offset,int len,int check)494 int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
495 			struct ubi_sgl *sgl, int lnum, int offset, int len,
496 			int check)
497 {
498 	int to_read;
499 	int ret;
500 	struct scatterlist *sg;
501 
502 	for (;;) {
503 		ubi_assert(sgl->list_pos < UBI_MAX_SG_COUNT);
504 		sg = &sgl->sg[sgl->list_pos];
505 		if (len < sg->length - sgl->page_pos)
506 			to_read = len;
507 		else
508 			to_read = sg->length - sgl->page_pos;
509 
510 		ret = ubi_eba_read_leb(ubi, vol, lnum,
511 				       sg_virt(sg) + sgl->page_pos, offset,
512 				       to_read, check);
513 		if (ret < 0)
514 			return ret;
515 
516 		offset += to_read;
517 		len -= to_read;
518 		if (!len) {
519 			sgl->page_pos += to_read;
520 			if (sgl->page_pos == sg->length) {
521 				sgl->list_pos++;
522 				sgl->page_pos = 0;
523 			}
524 
525 			break;
526 		}
527 
528 		sgl->list_pos++;
529 		sgl->page_pos = 0;
530 	}
531 
532 	return ret;
533 }
534 #endif
535 
536 /**
537  * recover_peb - recover from write failure.
538  * @ubi: UBI device description object
539  * @pnum: the physical eraseblock to recover
540  * @vol_id: volume ID
541  * @lnum: logical eraseblock number
542  * @buf: data which was not written because of the write failure
543  * @offset: offset of the failed write
544  * @len: how many bytes should have been written
545  *
546  * This function is called in case of a write failure and moves all good data
547  * from the potentially bad physical eraseblock to a good physical eraseblock.
548  * This function also writes the data which was not written due to the failure.
549  * Returns new physical eraseblock number in case of success, and a negative
550  * error code in case of failure.
551  */
recover_peb(struct ubi_device * ubi,int pnum,int vol_id,int lnum,const void * buf,int offset,int len)552 static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
553 		       const void *buf, int offset, int len)
554 {
555 	int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
556 	struct ubi_volume *vol = ubi->volumes[idx];
557 	struct ubi_vid_hdr *vid_hdr;
558 
559 	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
560 	if (!vid_hdr)
561 		return -ENOMEM;
562 
563 retry:
564 	new_pnum = ubi_wl_get_peb(ubi);
565 	if (new_pnum < 0) {
566 		ubi_free_vid_hdr(ubi, vid_hdr);
567 		up_read(&ubi->fm_eba_sem);
568 		return new_pnum;
569 	}
570 
571 	ubi_msg(ubi, "recover PEB %d, move data to PEB %d",
572 		pnum, new_pnum);
573 
574 	err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
575 	if (err && err != UBI_IO_BITFLIPS) {
576 		if (err > 0)
577 			err = -EIO;
578 		up_read(&ubi->fm_eba_sem);
579 		goto out_put;
580 	}
581 
582 	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
583 	err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
584 	if (err) {
585 		up_read(&ubi->fm_eba_sem);
586 		goto write_error;
587 	}
588 
589 	data_size = offset + len;
590 	mutex_lock(&ubi->buf_mutex);
591 	memset(ubi->peb_buf + offset, 0xFF, len);
592 
593 	/* Read everything before the area where the write failure happened */
594 	if (offset > 0) {
595 		err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset);
596 		if (err && err != UBI_IO_BITFLIPS) {
597 			up_read(&ubi->fm_eba_sem);
598 			goto out_unlock;
599 		}
600 	}
601 
602 	memcpy(ubi->peb_buf + offset, buf, len);
603 
604 	err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size);
605 	if (err) {
606 		mutex_unlock(&ubi->buf_mutex);
607 		up_read(&ubi->fm_eba_sem);
608 		goto write_error;
609 	}
610 
611 	mutex_unlock(&ubi->buf_mutex);
612 	ubi_free_vid_hdr(ubi, vid_hdr);
613 
614 	vol->eba_tbl[lnum] = new_pnum;
615 	up_read(&ubi->fm_eba_sem);
616 	ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
617 
618 	ubi_msg(ubi, "data was successfully recovered");
619 	return 0;
620 
621 out_unlock:
622 	mutex_unlock(&ubi->buf_mutex);
623 out_put:
624 	ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
625 	ubi_free_vid_hdr(ubi, vid_hdr);
626 	return err;
627 
628 write_error:
629 	/*
630 	 * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
631 	 * get another one.
632 	 */
633 	ubi_warn(ubi, "failed to write to PEB %d", new_pnum);
634 	ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1);
635 	if (++tries > UBI_IO_RETRIES) {
636 		ubi_free_vid_hdr(ubi, vid_hdr);
637 		return err;
638 	}
639 	ubi_msg(ubi, "try again");
640 	goto retry;
641 }
642 
643 /**
644  * ubi_eba_write_leb - write data to dynamic volume.
645  * @ubi: UBI device description object
646  * @vol: volume description object
647  * @lnum: logical eraseblock number
648  * @buf: the data to write
649  * @offset: offset within the logical eraseblock where to write
650  * @len: how many bytes to write
651  *
652  * This function writes data to logical eraseblock @lnum of a dynamic volume
653  * @vol. Returns zero in case of success and a negative error code in case
654  * of failure. In case of error, it is possible that something was still
655  * written to the flash media, but may be some garbage.
656  */
ubi_eba_write_leb(struct ubi_device * ubi,struct ubi_volume * vol,int lnum,const void * buf,int offset,int len)657 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
658 		      const void *buf, int offset, int len)
659 {
660 	int err, pnum, tries = 0, vol_id = vol->vol_id;
661 	struct ubi_vid_hdr *vid_hdr;
662 
663 	if (ubi->ro_mode)
664 		return -EROFS;
665 
666 	err = leb_write_lock(ubi, vol_id, lnum);
667 	if (err)
668 		return err;
669 
670 	pnum = vol->eba_tbl[lnum];
671 	if (pnum >= 0) {
672 		dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
673 			len, offset, vol_id, lnum, pnum);
674 
675 		err = ubi_io_write_data(ubi, buf, pnum, offset, len);
676 		if (err) {
677 			ubi_warn(ubi, "failed to write data to PEB %d", pnum);
678 			if (err == -EIO && ubi->bad_allowed)
679 				err = recover_peb(ubi, pnum, vol_id, lnum, buf,
680 						  offset, len);
681 			if (err)
682 				ubi_ro_mode(ubi);
683 		}
684 		leb_write_unlock(ubi, vol_id, lnum);
685 		return err;
686 	}
687 
688 	/*
689 	 * The logical eraseblock is not mapped. We have to get a free physical
690 	 * eraseblock and write the volume identifier header there first.
691 	 */
692 	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
693 	if (!vid_hdr) {
694 		leb_write_unlock(ubi, vol_id, lnum);
695 		return -ENOMEM;
696 	}
697 
698 	vid_hdr->vol_type = UBI_VID_DYNAMIC;
699 	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
700 	vid_hdr->vol_id = cpu_to_be32(vol_id);
701 	vid_hdr->lnum = cpu_to_be32(lnum);
702 	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
703 	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
704 
705 retry:
706 	pnum = ubi_wl_get_peb(ubi);
707 	if (pnum < 0) {
708 		ubi_free_vid_hdr(ubi, vid_hdr);
709 		leb_write_unlock(ubi, vol_id, lnum);
710 		up_read(&ubi->fm_eba_sem);
711 		return pnum;
712 	}
713 
714 	dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
715 		len, offset, vol_id, lnum, pnum);
716 
717 	err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
718 	if (err) {
719 		ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
720 			 vol_id, lnum, pnum);
721 		up_read(&ubi->fm_eba_sem);
722 		goto write_error;
723 	}
724 
725 	if (len) {
726 		err = ubi_io_write_data(ubi, buf, pnum, offset, len);
727 		if (err) {
728 			ubi_warn(ubi, "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d",
729 				 len, offset, vol_id, lnum, pnum);
730 			up_read(&ubi->fm_eba_sem);
731 			goto write_error;
732 		}
733 	}
734 
735 	vol->eba_tbl[lnum] = pnum;
736 	up_read(&ubi->fm_eba_sem);
737 
738 	leb_write_unlock(ubi, vol_id, lnum);
739 	ubi_free_vid_hdr(ubi, vid_hdr);
740 	return 0;
741 
742 write_error:
743 	if (err != -EIO || !ubi->bad_allowed) {
744 		ubi_ro_mode(ubi);
745 		leb_write_unlock(ubi, vol_id, lnum);
746 		ubi_free_vid_hdr(ubi, vid_hdr);
747 		return err;
748 	}
749 
750 	/*
751 	 * Fortunately, this is the first write operation to this physical
752 	 * eraseblock, so just put it and request a new one. We assume that if
753 	 * this physical eraseblock went bad, the erase code will handle that.
754 	 */
755 	err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
756 	if (err || ++tries > UBI_IO_RETRIES) {
757 		ubi_ro_mode(ubi);
758 		leb_write_unlock(ubi, vol_id, lnum);
759 		ubi_free_vid_hdr(ubi, vid_hdr);
760 		return err;
761 	}
762 
763 	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
764 	ubi_msg(ubi, "try another PEB");
765 	goto retry;
766 }
767 
768 /**
769  * ubi_eba_write_leb_st - write data to static volume.
770  * @ubi: UBI device description object
771  * @vol: volume description object
772  * @lnum: logical eraseblock number
773  * @buf: data to write
774  * @len: how many bytes to write
775  * @used_ebs: how many logical eraseblocks will this volume contain
776  *
777  * This function writes data to logical eraseblock @lnum of static volume
778  * @vol. The @used_ebs argument should contain total number of logical
779  * eraseblock in this static volume.
780  *
781  * When writing to the last logical eraseblock, the @len argument doesn't have
782  * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
783  * to the real data size, although the @buf buffer has to contain the
784  * alignment. In all other cases, @len has to be aligned.
785  *
786  * It is prohibited to write more than once to logical eraseblocks of static
787  * volumes. This function returns zero in case of success and a negative error
788  * code in case of failure.
789  */
ubi_eba_write_leb_st(struct ubi_device * ubi,struct ubi_volume * vol,int lnum,const void * buf,int len,int used_ebs)790 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
791 			 int lnum, const void *buf, int len, int used_ebs)
792 {
793 	int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
794 	struct ubi_vid_hdr *vid_hdr;
795 	uint32_t crc;
796 
797 	if (ubi->ro_mode)
798 		return -EROFS;
799 
800 	if (lnum == used_ebs - 1)
801 		/* If this is the last LEB @len may be unaligned */
802 		len = ALIGN(data_size, ubi->min_io_size);
803 	else
804 		ubi_assert(!(len & (ubi->min_io_size - 1)));
805 
806 	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
807 	if (!vid_hdr)
808 		return -ENOMEM;
809 
810 	err = leb_write_lock(ubi, vol_id, lnum);
811 	if (err) {
812 		ubi_free_vid_hdr(ubi, vid_hdr);
813 		return err;
814 	}
815 
816 	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
817 	vid_hdr->vol_id = cpu_to_be32(vol_id);
818 	vid_hdr->lnum = cpu_to_be32(lnum);
819 	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
820 	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
821 
822 	crc = crc32(UBI_CRC32_INIT, buf, data_size);
823 	vid_hdr->vol_type = UBI_VID_STATIC;
824 	vid_hdr->data_size = cpu_to_be32(data_size);
825 	vid_hdr->used_ebs = cpu_to_be32(used_ebs);
826 	vid_hdr->data_crc = cpu_to_be32(crc);
827 
828 retry:
829 	pnum = ubi_wl_get_peb(ubi);
830 	if (pnum < 0) {
831 		ubi_free_vid_hdr(ubi, vid_hdr);
832 		leb_write_unlock(ubi, vol_id, lnum);
833 		up_read(&ubi->fm_eba_sem);
834 		return pnum;
835 	}
836 
837 	dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
838 		len, vol_id, lnum, pnum, used_ebs);
839 
840 	err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
841 	if (err) {
842 		ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
843 			 vol_id, lnum, pnum);
844 		up_read(&ubi->fm_eba_sem);
845 		goto write_error;
846 	}
847 
848 	err = ubi_io_write_data(ubi, buf, pnum, 0, len);
849 	if (err) {
850 		ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
851 			 len, pnum);
852 		up_read(&ubi->fm_eba_sem);
853 		goto write_error;
854 	}
855 
856 	ubi_assert(vol->eba_tbl[lnum] < 0);
857 	vol->eba_tbl[lnum] = pnum;
858 	up_read(&ubi->fm_eba_sem);
859 
860 	leb_write_unlock(ubi, vol_id, lnum);
861 	ubi_free_vid_hdr(ubi, vid_hdr);
862 	return 0;
863 
864 write_error:
865 	if (err != -EIO || !ubi->bad_allowed) {
866 		/*
867 		 * This flash device does not admit of bad eraseblocks or
868 		 * something nasty and unexpected happened. Switch to read-only
869 		 * mode just in case.
870 		 */
871 		ubi_ro_mode(ubi);
872 		leb_write_unlock(ubi, vol_id, lnum);
873 		ubi_free_vid_hdr(ubi, vid_hdr);
874 		return err;
875 	}
876 
877 	err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
878 	if (err || ++tries > UBI_IO_RETRIES) {
879 		ubi_ro_mode(ubi);
880 		leb_write_unlock(ubi, vol_id, lnum);
881 		ubi_free_vid_hdr(ubi, vid_hdr);
882 		return err;
883 	}
884 
885 	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
886 	ubi_msg(ubi, "try another PEB");
887 	goto retry;
888 }
889 
890 /*
891  * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
892  * @ubi: UBI device description object
893  * @vol: volume description object
894  * @lnum: logical eraseblock number
895  * @buf: data to write
896  * @len: how many bytes to write
897  *
898  * This function changes the contents of a logical eraseblock atomically. @buf
899  * has to contain new logical eraseblock data, and @len - the length of the
900  * data, which has to be aligned. This function guarantees that in case of an
901  * unclean reboot the old contents is preserved. Returns zero in case of
902  * success and a negative error code in case of failure.
903  *
904  * UBI reserves one LEB for the "atomic LEB change" operation, so only one
905  * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
906  */
ubi_eba_atomic_leb_change(struct ubi_device * ubi,struct ubi_volume * vol,int lnum,const void * buf,int len)907 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
908 			      int lnum, const void *buf, int len)
909 {
910 	int err, pnum, old_pnum, tries = 0, vol_id = vol->vol_id;
911 	struct ubi_vid_hdr *vid_hdr;
912 	uint32_t crc;
913 
914 	if (ubi->ro_mode)
915 		return -EROFS;
916 
917 	if (len == 0) {
918 		/*
919 		 * Special case when data length is zero. In this case the LEB
920 		 * has to be unmapped and mapped somewhere else.
921 		 */
922 		err = ubi_eba_unmap_leb(ubi, vol, lnum);
923 		if (err)
924 			return err;
925 		return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
926 	}
927 
928 	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
929 	if (!vid_hdr)
930 		return -ENOMEM;
931 
932 	mutex_lock(&ubi->alc_mutex);
933 	err = leb_write_lock(ubi, vol_id, lnum);
934 	if (err)
935 		goto out_mutex;
936 
937 	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
938 	vid_hdr->vol_id = cpu_to_be32(vol_id);
939 	vid_hdr->lnum = cpu_to_be32(lnum);
940 	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
941 	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
942 
943 	crc = crc32(UBI_CRC32_INIT, buf, len);
944 	vid_hdr->vol_type = UBI_VID_DYNAMIC;
945 	vid_hdr->data_size = cpu_to_be32(len);
946 	vid_hdr->copy_flag = 1;
947 	vid_hdr->data_crc = cpu_to_be32(crc);
948 
949 retry:
950 	pnum = ubi_wl_get_peb(ubi);
951 	if (pnum < 0) {
952 		err = pnum;
953 		up_read(&ubi->fm_eba_sem);
954 		goto out_leb_unlock;
955 	}
956 
957 	dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
958 		vol_id, lnum, vol->eba_tbl[lnum], pnum);
959 
960 	err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
961 	if (err) {
962 		ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
963 			 vol_id, lnum, pnum);
964 		up_read(&ubi->fm_eba_sem);
965 		goto write_error;
966 	}
967 
968 	err = ubi_io_write_data(ubi, buf, pnum, 0, len);
969 	if (err) {
970 		ubi_warn(ubi, "failed to write %d bytes of data to PEB %d",
971 			 len, pnum);
972 		up_read(&ubi->fm_eba_sem);
973 		goto write_error;
974 	}
975 
976 	old_pnum = vol->eba_tbl[lnum];
977 	vol->eba_tbl[lnum] = pnum;
978 	up_read(&ubi->fm_eba_sem);
979 
980 	if (old_pnum >= 0) {
981 		err = ubi_wl_put_peb(ubi, vol_id, lnum, old_pnum, 0);
982 		if (err)
983 			goto out_leb_unlock;
984 	}
985 
986 out_leb_unlock:
987 	leb_write_unlock(ubi, vol_id, lnum);
988 out_mutex:
989 	mutex_unlock(&ubi->alc_mutex);
990 	ubi_free_vid_hdr(ubi, vid_hdr);
991 	return err;
992 
993 write_error:
994 	if (err != -EIO || !ubi->bad_allowed) {
995 		/*
996 		 * This flash device does not admit of bad eraseblocks or
997 		 * something nasty and unexpected happened. Switch to read-only
998 		 * mode just in case.
999 		 */
1000 		ubi_ro_mode(ubi);
1001 		goto out_leb_unlock;
1002 	}
1003 
1004 	err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1);
1005 	if (err || ++tries > UBI_IO_RETRIES) {
1006 		ubi_ro_mode(ubi);
1007 		goto out_leb_unlock;
1008 	}
1009 
1010 	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1011 	ubi_msg(ubi, "try another PEB");
1012 	goto retry;
1013 }
1014 
1015 /**
1016  * is_error_sane - check whether a read error is sane.
1017  * @err: code of the error happened during reading
1018  *
1019  * This is a helper function for 'ubi_eba_copy_leb()' which is called when we
1020  * cannot read data from the target PEB (an error @err happened). If the error
1021  * code is sane, then we treat this error as non-fatal. Otherwise the error is
1022  * fatal and UBI will be switched to R/O mode later.
1023  *
1024  * The idea is that we try not to switch to R/O mode if the read error is
1025  * something which suggests there was a real read problem. E.g., %-EIO. Or a
1026  * memory allocation failed (-%ENOMEM). Otherwise, it is safer to switch to R/O
1027  * mode, simply because we do not know what happened at the MTD level, and we
1028  * cannot handle this. E.g., the underlying driver may have become crazy, and
1029  * it is safer to switch to R/O mode to preserve the data.
1030  *
1031  * And bear in mind, this is about reading from the target PEB, i.e. the PEB
1032  * which we have just written.
1033  */
is_error_sane(int err)1034 static int is_error_sane(int err)
1035 {
1036 	if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
1037 	    err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT)
1038 		return 0;
1039 	return 1;
1040 }
1041 
1042 /**
1043  * ubi_eba_copy_leb - copy logical eraseblock.
1044  * @ubi: UBI device description object
1045  * @from: physical eraseblock number from where to copy
1046  * @to: physical eraseblock number where to copy
1047  * @vid_hdr: VID header of the @from physical eraseblock
1048  *
1049  * This function copies logical eraseblock from physical eraseblock @from to
1050  * physical eraseblock @to. The @vid_hdr buffer may be changed by this
1051  * function. Returns:
1052  *   o %0 in case of success;
1053  *   o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc;
1054  *   o a negative error code in case of failure.
1055  */
ubi_eba_copy_leb(struct ubi_device * ubi,int from,int to,struct ubi_vid_hdr * vid_hdr)1056 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
1057 		     struct ubi_vid_hdr *vid_hdr)
1058 {
1059 	int err, vol_id, lnum, data_size, aldata_size, idx;
1060 	struct ubi_volume *vol;
1061 	uint32_t crc;
1062 
1063 	vol_id = be32_to_cpu(vid_hdr->vol_id);
1064 	lnum = be32_to_cpu(vid_hdr->lnum);
1065 
1066 	dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
1067 
1068 	if (vid_hdr->vol_type == UBI_VID_STATIC) {
1069 		data_size = be32_to_cpu(vid_hdr->data_size);
1070 		aldata_size = ALIGN(data_size, ubi->min_io_size);
1071 	} else
1072 		data_size = aldata_size =
1073 			    ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
1074 
1075 	idx = vol_id2idx(ubi, vol_id);
1076 	spin_lock(&ubi->volumes_lock);
1077 	/*
1078 	 * Note, we may race with volume deletion, which means that the volume
1079 	 * this logical eraseblock belongs to might be being deleted. Since the
1080 	 * volume deletion un-maps all the volume's logical eraseblocks, it will
1081 	 * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
1082 	 */
1083 	vol = ubi->volumes[idx];
1084 	spin_unlock(&ubi->volumes_lock);
1085 	if (!vol) {
1086 		/* No need to do further work, cancel */
1087 		dbg_wl("volume %d is being removed, cancel", vol_id);
1088 		return MOVE_CANCEL_RACE;
1089 	}
1090 
1091 	/*
1092 	 * We do not want anybody to write to this logical eraseblock while we
1093 	 * are moving it, so lock it.
1094 	 *
1095 	 * Note, we are using non-waiting locking here, because we cannot sleep
1096 	 * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
1097 	 * unmapping the LEB which is mapped to the PEB we are going to move
1098 	 * (@from). This task locks the LEB and goes sleep in the
1099 	 * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
1100 	 * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
1101 	 * LEB is already locked, we just do not move it and return
1102 	 * %MOVE_RETRY. Note, we do not return %MOVE_CANCEL_RACE here because
1103 	 * we do not know the reasons of the contention - it may be just a
1104 	 * normal I/O on this LEB, so we want to re-try.
1105 	 */
1106 	err = leb_write_trylock(ubi, vol_id, lnum);
1107 	if (err) {
1108 		dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum);
1109 		return MOVE_RETRY;
1110 	}
1111 
1112 	/*
1113 	 * The LEB might have been put meanwhile, and the task which put it is
1114 	 * probably waiting on @ubi->move_mutex. No need to continue the work,
1115 	 * cancel it.
1116 	 */
1117 	if (vol->eba_tbl[lnum] != from) {
1118 		dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel",
1119 		       vol_id, lnum, from, vol->eba_tbl[lnum]);
1120 		err = MOVE_CANCEL_RACE;
1121 		goto out_unlock_leb;
1122 	}
1123 
1124 	/*
1125 	 * OK, now the LEB is locked and we can safely start moving it. Since
1126 	 * this function utilizes the @ubi->peb_buf buffer which is shared
1127 	 * with some other functions - we lock the buffer by taking the
1128 	 * @ubi->buf_mutex.
1129 	 */
1130 	mutex_lock(&ubi->buf_mutex);
1131 	dbg_wl("read %d bytes of data", aldata_size);
1132 	err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size);
1133 	if (err && err != UBI_IO_BITFLIPS) {
1134 		ubi_warn(ubi, "error %d while reading data from PEB %d",
1135 			 err, from);
1136 		err = MOVE_SOURCE_RD_ERR;
1137 		goto out_unlock_buf;
1138 	}
1139 
1140 	/*
1141 	 * Now we have got to calculate how much data we have to copy. In
1142 	 * case of a static volume it is fairly easy - the VID header contains
1143 	 * the data size. In case of a dynamic volume it is more difficult - we
1144 	 * have to read the contents, cut 0xFF bytes from the end and copy only
1145 	 * the first part. We must do this to avoid writing 0xFF bytes as it
1146 	 * may have some side-effects. And not only this. It is important not
1147 	 * to include those 0xFFs to CRC because later the they may be filled
1148 	 * by data.
1149 	 */
1150 	if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
1151 		aldata_size = data_size =
1152 			ubi_calc_data_len(ubi, ubi->peb_buf, data_size);
1153 
1154 	cond_resched();
1155 	crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size);
1156 	cond_resched();
1157 
1158 	/*
1159 	 * It may turn out to be that the whole @from physical eraseblock
1160 	 * contains only 0xFF bytes. Then we have to only write the VID header
1161 	 * and do not write any data. This also means we should not set
1162 	 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
1163 	 */
1164 	if (data_size > 0) {
1165 		vid_hdr->copy_flag = 1;
1166 		vid_hdr->data_size = cpu_to_be32(data_size);
1167 		vid_hdr->data_crc = cpu_to_be32(crc);
1168 	}
1169 	vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1170 
1171 	err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1172 	if (err) {
1173 		if (err == -EIO)
1174 			err = MOVE_TARGET_WR_ERR;
1175 		goto out_unlock_buf;
1176 	}
1177 
1178 	cond_resched();
1179 
1180 	/* Read the VID header back and check if it was written correctly */
1181 	err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
1182 	if (err) {
1183 		if (err != UBI_IO_BITFLIPS) {
1184 			ubi_warn(ubi, "error %d while reading VID header back from PEB %d",
1185 				 err, to);
1186 			if (is_error_sane(err))
1187 				err = MOVE_TARGET_RD_ERR;
1188 		} else
1189 			err = MOVE_TARGET_BITFLIPS;
1190 		goto out_unlock_buf;
1191 	}
1192 
1193 	if (data_size > 0) {
1194 		err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size);
1195 		if (err) {
1196 			if (err == -EIO)
1197 				err = MOVE_TARGET_WR_ERR;
1198 			goto out_unlock_buf;
1199 		}
1200 
1201 		cond_resched();
1202 
1203 		/*
1204 		 * We've written the data and are going to read it back to make
1205 		 * sure it was written correctly.
1206 		 */
1207 		memset(ubi->peb_buf, 0xFF, aldata_size);
1208 		err = ubi_io_read_data(ubi, ubi->peb_buf, to, 0, aldata_size);
1209 		if (err) {
1210 			if (err != UBI_IO_BITFLIPS) {
1211 				ubi_warn(ubi, "error %d while reading data back from PEB %d",
1212 					 err, to);
1213 				if (is_error_sane(err))
1214 					err = MOVE_TARGET_RD_ERR;
1215 			} else
1216 				err = MOVE_TARGET_BITFLIPS;
1217 			goto out_unlock_buf;
1218 		}
1219 
1220 		cond_resched();
1221 
1222 		if (crc != crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size)) {
1223 			ubi_warn(ubi, "read data back from PEB %d and it is different",
1224 				 to);
1225 			err = -EINVAL;
1226 			goto out_unlock_buf;
1227 		}
1228 	}
1229 
1230 	ubi_assert(vol->eba_tbl[lnum] == from);
1231 	down_read(&ubi->fm_eba_sem);
1232 	vol->eba_tbl[lnum] = to;
1233 	up_read(&ubi->fm_eba_sem);
1234 
1235 out_unlock_buf:
1236 	mutex_unlock(&ubi->buf_mutex);
1237 out_unlock_leb:
1238 	leb_write_unlock(ubi, vol_id, lnum);
1239 	return err;
1240 }
1241 
1242 /**
1243  * print_rsvd_warning - warn about not having enough reserved PEBs.
1244  * @ubi: UBI device description object
1245  *
1246  * This is a helper function for 'ubi_eba_init()' which is called when UBI
1247  * cannot reserve enough PEBs for bad block handling. This function makes a
1248  * decision whether we have to print a warning or not. The algorithm is as
1249  * follows:
1250  *   o if this is a new UBI image, then just print the warning
1251  *   o if this is an UBI image which has already been used for some time, print
1252  *     a warning only if we can reserve less than 10% of the expected amount of
1253  *     the reserved PEB.
1254  *
1255  * The idea is that when UBI is used, PEBs become bad, and the reserved pool
1256  * of PEBs becomes smaller, which is normal and we do not want to scare users
1257  * with a warning every time they attach the MTD device. This was an issue
1258  * reported by real users.
1259  */
print_rsvd_warning(struct ubi_device * ubi,struct ubi_attach_info * ai)1260 static void print_rsvd_warning(struct ubi_device *ubi,
1261 			       struct ubi_attach_info *ai)
1262 {
1263 	/*
1264 	 * The 1 << 18 (256KiB) number is picked randomly, just a reasonably
1265 	 * large number to distinguish between newly flashed and used images.
1266 	 */
1267 	if (ai->max_sqnum > (1 << 18)) {
1268 		int min = ubi->beb_rsvd_level / 10;
1269 
1270 		if (!min)
1271 			min = 1;
1272 		if (ubi->beb_rsvd_pebs > min)
1273 			return;
1274 	}
1275 
1276 	ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d",
1277 		 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1278 	if (ubi->corr_peb_count)
1279 		ubi_warn(ubi, "%d PEBs are corrupted and not used",
1280 			 ubi->corr_peb_count);
1281 }
1282 
1283 /**
1284  * self_check_eba - run a self check on the EBA table constructed by fastmap.
1285  * @ubi: UBI device description object
1286  * @ai_fastmap: UBI attach info object created by fastmap
1287  * @ai_scan: UBI attach info object created by scanning
1288  *
1289  * Returns < 0 in case of an internal error, 0 otherwise.
1290  * If a bad EBA table entry was found it will be printed out and
1291  * ubi_assert() triggers.
1292  */
self_check_eba(struct ubi_device * ubi,struct ubi_attach_info * ai_fastmap,struct ubi_attach_info * ai_scan)1293 int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
1294 		   struct ubi_attach_info *ai_scan)
1295 {
1296 	int i, j, num_volumes, ret = 0;
1297 	int **scan_eba, **fm_eba;
1298 	struct ubi_ainf_volume *av;
1299 	struct ubi_volume *vol;
1300 	struct ubi_ainf_peb *aeb;
1301 	struct rb_node *rb;
1302 
1303 	num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1304 
1305 	scan_eba = kmalloc(sizeof(*scan_eba) * num_volumes, GFP_KERNEL);
1306 	if (!scan_eba)
1307 		return -ENOMEM;
1308 
1309 	fm_eba = kmalloc(sizeof(*fm_eba) * num_volumes, GFP_KERNEL);
1310 	if (!fm_eba) {
1311 		kfree(scan_eba);
1312 		return -ENOMEM;
1313 	}
1314 
1315 	for (i = 0; i < num_volumes; i++) {
1316 		vol = ubi->volumes[i];
1317 		if (!vol)
1318 			continue;
1319 
1320 		scan_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**scan_eba),
1321 				      GFP_KERNEL);
1322 		if (!scan_eba[i]) {
1323 			ret = -ENOMEM;
1324 			goto out_free;
1325 		}
1326 
1327 		fm_eba[i] = kmalloc(vol->reserved_pebs * sizeof(**fm_eba),
1328 				    GFP_KERNEL);
1329 		if (!fm_eba[i]) {
1330 			ret = -ENOMEM;
1331 			goto out_free;
1332 		}
1333 
1334 		for (j = 0; j < vol->reserved_pebs; j++)
1335 			scan_eba[i][j] = fm_eba[i][j] = UBI_LEB_UNMAPPED;
1336 
1337 		av = ubi_find_av(ai_scan, idx2vol_id(ubi, i));
1338 		if (!av)
1339 			continue;
1340 
1341 		ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
1342 			scan_eba[i][aeb->lnum] = aeb->pnum;
1343 
1344 		av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i));
1345 		if (!av)
1346 			continue;
1347 
1348 		ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb)
1349 			fm_eba[i][aeb->lnum] = aeb->pnum;
1350 
1351 		for (j = 0; j < vol->reserved_pebs; j++) {
1352 			if (scan_eba[i][j] != fm_eba[i][j]) {
1353 				if (scan_eba[i][j] == UBI_LEB_UNMAPPED ||
1354 					fm_eba[i][j] == UBI_LEB_UNMAPPED)
1355 					continue;
1356 
1357 				ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!",
1358 					vol->vol_id, i, fm_eba[i][j],
1359 					scan_eba[i][j]);
1360 				ubi_assert(0);
1361 			}
1362 		}
1363 	}
1364 
1365 out_free:
1366 	for (i = 0; i < num_volumes; i++) {
1367 		if (!ubi->volumes[i])
1368 			continue;
1369 
1370 		kfree(scan_eba[i]);
1371 		kfree(fm_eba[i]);
1372 	}
1373 
1374 	kfree(scan_eba);
1375 	kfree(fm_eba);
1376 	return ret;
1377 }
1378 
1379 /**
1380  * ubi_eba_init - initialize the EBA sub-system using attaching information.
1381  * @ubi: UBI device description object
1382  * @ai: attaching information
1383  *
1384  * This function returns zero in case of success and a negative error code in
1385  * case of failure.
1386  */
ubi_eba_init(struct ubi_device * ubi,struct ubi_attach_info * ai)1387 int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
1388 {
1389 	int i, j, err, num_volumes;
1390 	struct ubi_ainf_volume *av;
1391 	struct ubi_volume *vol;
1392 	struct ubi_ainf_peb *aeb;
1393 	struct rb_node *rb;
1394 
1395 	dbg_eba("initialize EBA sub-system");
1396 
1397 	spin_lock_init(&ubi->ltree_lock);
1398 	mutex_init(&ubi->alc_mutex);
1399 	ubi->ltree = RB_ROOT;
1400 
1401 	ubi->global_sqnum = ai->max_sqnum + 1;
1402 	num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1403 
1404 	for (i = 0; i < num_volumes; i++) {
1405 		vol = ubi->volumes[i];
1406 		if (!vol)
1407 			continue;
1408 
1409 		cond_resched();
1410 
1411 		vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
1412 				       GFP_KERNEL);
1413 		if (!vol->eba_tbl) {
1414 			err = -ENOMEM;
1415 			goto out_free;
1416 		}
1417 
1418 		for (j = 0; j < vol->reserved_pebs; j++)
1419 			vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
1420 
1421 		av = ubi_find_av(ai, idx2vol_id(ubi, i));
1422 		if (!av)
1423 			continue;
1424 
1425 		ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) {
1426 			if (aeb->lnum >= vol->reserved_pebs)
1427 				/*
1428 				 * This may happen in case of an unclean reboot
1429 				 * during re-size.
1430 				 */
1431 				ubi_move_aeb_to_list(av, aeb, &ai->erase);
1432 			else
1433 				vol->eba_tbl[aeb->lnum] = aeb->pnum;
1434 		}
1435 	}
1436 
1437 	if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
1438 		ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
1439 			ubi->avail_pebs, EBA_RESERVED_PEBS);
1440 		if (ubi->corr_peb_count)
1441 			ubi_err(ubi, "%d PEBs are corrupted and not used",
1442 				ubi->corr_peb_count);
1443 		err = -ENOSPC;
1444 		goto out_free;
1445 	}
1446 	ubi->avail_pebs -= EBA_RESERVED_PEBS;
1447 	ubi->rsvd_pebs += EBA_RESERVED_PEBS;
1448 
1449 	if (ubi->bad_allowed) {
1450 		ubi_calculate_reserved(ubi);
1451 
1452 		if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1453 			/* No enough free physical eraseblocks */
1454 			ubi->beb_rsvd_pebs = ubi->avail_pebs;
1455 			print_rsvd_warning(ubi, ai);
1456 		} else
1457 			ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1458 
1459 		ubi->avail_pebs -= ubi->beb_rsvd_pebs;
1460 		ubi->rsvd_pebs  += ubi->beb_rsvd_pebs;
1461 	}
1462 
1463 	dbg_eba("EBA sub-system is initialized");
1464 	return 0;
1465 
1466 out_free:
1467 	for (i = 0; i < num_volumes; i++) {
1468 		if (!ubi->volumes[i])
1469 			continue;
1470 		kfree(ubi->volumes[i]->eba_tbl);
1471 		ubi->volumes[i]->eba_tbl = NULL;
1472 	}
1473 	return err;
1474 }
1475