xref: /linux/drivers/mtd/ubi/eba.c (revision 367b8112fe2ea5c39a7bb4d263dcdd9b612fae18)
1 /*
2  * Copyright (c) International Business Machines Corp., 2006
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12  * the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Artem Bityutskiy (Битюцкий Артём)
19  */
20 
21 /*
22  * The UBI Eraseblock Association (EBA) sub-system.
23  *
24  * This sub-system is responsible for I/O to/from logical eraseblock.
25  *
26  * Although in this implementation the EBA table is fully kept and managed in
27  * RAM, which assumes poor scalability, it might be (partially) maintained on
28  * flash in future implementations.
29  *
30  * The EBA sub-system implements per-logical eraseblock locking. Before
31  * accessing a logical eraseblock it is locked for reading or writing. The
32  * per-logical eraseblock locking is implemented by means of the lock tree. The
33  * lock tree is an RB-tree which refers all the currently locked logical
34  * eraseblocks. The lock tree elements are &struct ubi_ltree_entry objects.
35  * They are indexed by (@vol_id, @lnum) pairs.
36  *
37  * EBA also maintains the global sequence counter which is incremented each
38  * time a logical eraseblock is mapped to a physical eraseblock and it is
39  * stored in the volume identifier header. This means that each VID header has
40  * a unique sequence number. The sequence number is only increased an we assume
41  * 64 bits is enough to never overflow.
42  */
43 
44 #include <linux/slab.h>
45 #include <linux/crc32.h>
46 #include <linux/err.h>
47 #include "ubi.h"
48 
49 /* Number of physical eraseblocks reserved for atomic LEB change operation */
50 #define EBA_RESERVED_PEBS 1
51 
52 /**
53  * next_sqnum - get next sequence number.
54  * @ubi: UBI device description object
55  *
56  * This function returns next sequence number to use, which is just the current
57  * global sequence counter value. It also increases the global sequence
58  * counter.
59  */
60 static unsigned long long next_sqnum(struct ubi_device *ubi)
61 {
62 	unsigned long long sqnum;
63 
64 	spin_lock(&ubi->ltree_lock);
65 	sqnum = ubi->global_sqnum++;
66 	spin_unlock(&ubi->ltree_lock);
67 
68 	return sqnum;
69 }
70 
71 /**
72  * ubi_get_compat - get compatibility flags of a volume.
73  * @ubi: UBI device description object
74  * @vol_id: volume ID
75  *
76  * This function returns compatibility flags for an internal volume. User
77  * volumes have no compatibility flags, so %0 is returned.
78  */
79 static int ubi_get_compat(const struct ubi_device *ubi, int vol_id)
80 {
81 	if (vol_id == UBI_LAYOUT_VOLUME_ID)
82 		return UBI_LAYOUT_VOLUME_COMPAT;
83 	return 0;
84 }
85 
86 /**
87  * ltree_lookup - look up the lock tree.
88  * @ubi: UBI device description object
89  * @vol_id: volume ID
90  * @lnum: logical eraseblock number
91  *
92  * This function returns a pointer to the corresponding &struct ubi_ltree_entry
93  * object if the logical eraseblock is locked and %NULL if it is not.
94  * @ubi->ltree_lock has to be locked.
95  */
96 static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id,
97 					    int lnum)
98 {
99 	struct rb_node *p;
100 
101 	p = ubi->ltree.rb_node;
102 	while (p) {
103 		struct ubi_ltree_entry *le;
104 
105 		le = rb_entry(p, struct ubi_ltree_entry, rb);
106 
107 		if (vol_id < le->vol_id)
108 			p = p->rb_left;
109 		else if (vol_id > le->vol_id)
110 			p = p->rb_right;
111 		else {
112 			if (lnum < le->lnum)
113 				p = p->rb_left;
114 			else if (lnum > le->lnum)
115 				p = p->rb_right;
116 			else
117 				return le;
118 		}
119 	}
120 
121 	return NULL;
122 }
123 
124 /**
125  * ltree_add_entry - add new entry to the lock tree.
126  * @ubi: UBI device description object
127  * @vol_id: volume ID
128  * @lnum: logical eraseblock number
129  *
130  * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the
131  * lock tree. If such entry is already there, its usage counter is increased.
132  * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation
133  * failed.
134  */
135 static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi,
136 					       int vol_id, int lnum)
137 {
138 	struct ubi_ltree_entry *le, *le1, *le_free;
139 
140 	le = kmalloc(sizeof(struct ubi_ltree_entry), GFP_NOFS);
141 	if (!le)
142 		return ERR_PTR(-ENOMEM);
143 
144 	le->users = 0;
145 	init_rwsem(&le->mutex);
146 	le->vol_id = vol_id;
147 	le->lnum = lnum;
148 
149 	spin_lock(&ubi->ltree_lock);
150 	le1 = ltree_lookup(ubi, vol_id, lnum);
151 
152 	if (le1) {
153 		/*
154 		 * This logical eraseblock is already locked. The newly
155 		 * allocated lock entry is not needed.
156 		 */
157 		le_free = le;
158 		le = le1;
159 	} else {
160 		struct rb_node **p, *parent = NULL;
161 
162 		/*
163 		 * No lock entry, add the newly allocated one to the
164 		 * @ubi->ltree RB-tree.
165 		 */
166 		le_free = NULL;
167 
168 		p = &ubi->ltree.rb_node;
169 		while (*p) {
170 			parent = *p;
171 			le1 = rb_entry(parent, struct ubi_ltree_entry, rb);
172 
173 			if (vol_id < le1->vol_id)
174 				p = &(*p)->rb_left;
175 			else if (vol_id > le1->vol_id)
176 				p = &(*p)->rb_right;
177 			else {
178 				ubi_assert(lnum != le1->lnum);
179 				if (lnum < le1->lnum)
180 					p = &(*p)->rb_left;
181 				else
182 					p = &(*p)->rb_right;
183 			}
184 		}
185 
186 		rb_link_node(&le->rb, parent, p);
187 		rb_insert_color(&le->rb, &ubi->ltree);
188 	}
189 	le->users += 1;
190 	spin_unlock(&ubi->ltree_lock);
191 
192 	kfree(le_free);
193 	return le;
194 }
195 
196 /**
197  * leb_read_lock - lock logical eraseblock for reading.
198  * @ubi: UBI device description object
199  * @vol_id: volume ID
200  * @lnum: logical eraseblock number
201  *
202  * This function locks a logical eraseblock for reading. Returns zero in case
203  * of success and a negative error code in case of failure.
204  */
205 static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum)
206 {
207 	struct ubi_ltree_entry *le;
208 
209 	le = ltree_add_entry(ubi, vol_id, lnum);
210 	if (IS_ERR(le))
211 		return PTR_ERR(le);
212 	down_read(&le->mutex);
213 	return 0;
214 }
215 
216 /**
217  * leb_read_unlock - unlock logical eraseblock.
218  * @ubi: UBI device description object
219  * @vol_id: volume ID
220  * @lnum: logical eraseblock number
221  */
222 static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum)
223 {
224 	struct ubi_ltree_entry *le;
225 
226 	spin_lock(&ubi->ltree_lock);
227 	le = ltree_lookup(ubi, vol_id, lnum);
228 	le->users -= 1;
229 	ubi_assert(le->users >= 0);
230 	up_read(&le->mutex);
231 	if (le->users == 0) {
232 		rb_erase(&le->rb, &ubi->ltree);
233 		kfree(le);
234 	}
235 	spin_unlock(&ubi->ltree_lock);
236 }
237 
238 /**
239  * leb_write_lock - lock logical eraseblock for writing.
240  * @ubi: UBI device description object
241  * @vol_id: volume ID
242  * @lnum: logical eraseblock number
243  *
244  * This function locks a logical eraseblock for writing. Returns zero in case
245  * of success and a negative error code in case of failure.
246  */
247 static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum)
248 {
249 	struct ubi_ltree_entry *le;
250 
251 	le = ltree_add_entry(ubi, vol_id, lnum);
252 	if (IS_ERR(le))
253 		return PTR_ERR(le);
254 	down_write(&le->mutex);
255 	return 0;
256 }
257 
258 /**
259  * leb_write_lock - lock logical eraseblock for writing.
260  * @ubi: UBI device description object
261  * @vol_id: volume ID
262  * @lnum: logical eraseblock number
263  *
264  * This function locks a logical eraseblock for writing if there is no
265  * contention and does nothing if there is contention. Returns %0 in case of
266  * success, %1 in case of contention, and and a negative error code in case of
267  * failure.
268  */
269 static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum)
270 {
271 	struct ubi_ltree_entry *le;
272 
273 	le = ltree_add_entry(ubi, vol_id, lnum);
274 	if (IS_ERR(le))
275 		return PTR_ERR(le);
276 	if (down_write_trylock(&le->mutex))
277 		return 0;
278 
279 	/* Contention, cancel */
280 	spin_lock(&ubi->ltree_lock);
281 	le->users -= 1;
282 	ubi_assert(le->users >= 0);
283 	if (le->users == 0) {
284 		rb_erase(&le->rb, &ubi->ltree);
285 		kfree(le);
286 	}
287 	spin_unlock(&ubi->ltree_lock);
288 
289 	return 1;
290 }
291 
292 /**
293  * leb_write_unlock - unlock logical eraseblock.
294  * @ubi: UBI device description object
295  * @vol_id: volume ID
296  * @lnum: logical eraseblock number
297  */
298 static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum)
299 {
300 	struct ubi_ltree_entry *le;
301 
302 	spin_lock(&ubi->ltree_lock);
303 	le = ltree_lookup(ubi, vol_id, lnum);
304 	le->users -= 1;
305 	ubi_assert(le->users >= 0);
306 	up_write(&le->mutex);
307 	if (le->users == 0) {
308 		rb_erase(&le->rb, &ubi->ltree);
309 		kfree(le);
310 	}
311 	spin_unlock(&ubi->ltree_lock);
312 }
313 
314 /**
315  * ubi_eba_unmap_leb - un-map logical eraseblock.
316  * @ubi: UBI device description object
317  * @vol: volume description object
318  * @lnum: logical eraseblock number
319  *
320  * This function un-maps logical eraseblock @lnum and schedules corresponding
321  * physical eraseblock for erasure. Returns zero in case of success and a
322  * negative error code in case of failure.
323  */
324 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
325 		      int lnum)
326 {
327 	int err, pnum, vol_id = vol->vol_id;
328 
329 	if (ubi->ro_mode)
330 		return -EROFS;
331 
332 	err = leb_write_lock(ubi, vol_id, lnum);
333 	if (err)
334 		return err;
335 
336 	pnum = vol->eba_tbl[lnum];
337 	if (pnum < 0)
338 		/* This logical eraseblock is already unmapped */
339 		goto out_unlock;
340 
341 	dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum);
342 
343 	vol->eba_tbl[lnum] = UBI_LEB_UNMAPPED;
344 	err = ubi_wl_put_peb(ubi, pnum, 0);
345 
346 out_unlock:
347 	leb_write_unlock(ubi, vol_id, lnum);
348 	return err;
349 }
350 
351 /**
352  * ubi_eba_read_leb - read data.
353  * @ubi: UBI device description object
354  * @vol: volume description object
355  * @lnum: logical eraseblock number
356  * @buf: buffer to store the read data
357  * @offset: offset from where to read
358  * @len: how many bytes to read
359  * @check: data CRC check flag
360  *
361  * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF
362  * bytes. The @check flag only makes sense for static volumes and forces
363  * eraseblock data CRC checking.
364  *
365  * In case of success this function returns zero. In case of a static volume,
366  * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be
367  * returned for any volume type if an ECC error was detected by the MTD device
368  * driver. Other negative error cored may be returned in case of other errors.
369  */
370 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
371 		     void *buf, int offset, int len, int check)
372 {
373 	int err, pnum, scrub = 0, vol_id = vol->vol_id;
374 	struct ubi_vid_hdr *vid_hdr;
375 	uint32_t uninitialized_var(crc);
376 
377 	err = leb_read_lock(ubi, vol_id, lnum);
378 	if (err)
379 		return err;
380 
381 	pnum = vol->eba_tbl[lnum];
382 	if (pnum < 0) {
383 		/*
384 		 * The logical eraseblock is not mapped, fill the whole buffer
385 		 * with 0xFF bytes. The exception is static volumes for which
386 		 * it is an error to read unmapped logical eraseblocks.
387 		 */
388 		dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)",
389 			len, offset, vol_id, lnum);
390 		leb_read_unlock(ubi, vol_id, lnum);
391 		ubi_assert(vol->vol_type != UBI_STATIC_VOLUME);
392 		memset(buf, 0xFF, len);
393 		return 0;
394 	}
395 
396 	dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d",
397 		len, offset, vol_id, lnum, pnum);
398 
399 	if (vol->vol_type == UBI_DYNAMIC_VOLUME)
400 		check = 0;
401 
402 retry:
403 	if (check) {
404 		vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
405 		if (!vid_hdr) {
406 			err = -ENOMEM;
407 			goto out_unlock;
408 		}
409 
410 		err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
411 		if (err && err != UBI_IO_BITFLIPS) {
412 			if (err > 0) {
413 				/*
414 				 * The header is either absent or corrupted.
415 				 * The former case means there is a bug -
416 				 * switch to read-only mode just in case.
417 				 * The latter case means a real corruption - we
418 				 * may try to recover data. FIXME: but this is
419 				 * not implemented.
420 				 */
421 				if (err == UBI_IO_BAD_VID_HDR) {
422 					ubi_warn("bad VID header at PEB %d, LEB"
423 						 "%d:%d", pnum, vol_id, lnum);
424 					err = -EBADMSG;
425 				} else
426 					ubi_ro_mode(ubi);
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 			err = 0;
444 		} else if (err == -EBADMSG) {
445 			if (vol->vol_type == UBI_DYNAMIC_VOLUME)
446 				goto out_unlock;
447 			scrub = 1;
448 			if (!check) {
449 				ubi_msg("force data checking");
450 				check = 1;
451 				goto retry;
452 			}
453 		} else
454 			goto out_unlock;
455 	}
456 
457 	if (check) {
458 		uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len);
459 		if (crc1 != crc) {
460 			ubi_warn("CRC error: calculated %#08x, must be %#08x",
461 				 crc1, crc);
462 			err = -EBADMSG;
463 			goto out_unlock;
464 		}
465 	}
466 
467 	if (scrub)
468 		err = ubi_wl_scrub_peb(ubi, pnum);
469 
470 	leb_read_unlock(ubi, vol_id, lnum);
471 	return err;
472 
473 out_free:
474 	ubi_free_vid_hdr(ubi, vid_hdr);
475 out_unlock:
476 	leb_read_unlock(ubi, vol_id, lnum);
477 	return err;
478 }
479 
480 /**
481  * recover_peb - recover from write failure.
482  * @ubi: UBI device description object
483  * @pnum: the physical eraseblock to recover
484  * @vol_id: volume ID
485  * @lnum: logical eraseblock number
486  * @buf: data which was not written because of the write failure
487  * @offset: offset of the failed write
488  * @len: how many bytes should have been written
489  *
490  * This function is called in case of a write failure and moves all good data
491  * from the potentially bad physical eraseblock to a good physical eraseblock.
492  * This function also writes the data which was not written due to the failure.
493  * Returns new physical eraseblock number in case of success, and a negative
494  * error code in case of failure.
495  */
496 static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
497 		       const void *buf, int offset, int len)
498 {
499 	int err, idx = vol_id2idx(ubi, vol_id), new_pnum, data_size, tries = 0;
500 	struct ubi_volume *vol = ubi->volumes[idx];
501 	struct ubi_vid_hdr *vid_hdr;
502 
503 	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
504 	if (!vid_hdr)
505 		return -ENOMEM;
506 
507 	mutex_lock(&ubi->buf_mutex);
508 
509 retry:
510 	new_pnum = ubi_wl_get_peb(ubi, UBI_UNKNOWN);
511 	if (new_pnum < 0) {
512 		mutex_unlock(&ubi->buf_mutex);
513 		ubi_free_vid_hdr(ubi, vid_hdr);
514 		return new_pnum;
515 	}
516 
517 	ubi_msg("recover PEB %d, move data to PEB %d", pnum, new_pnum);
518 
519 	err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1);
520 	if (err && err != UBI_IO_BITFLIPS) {
521 		if (err > 0)
522 			err = -EIO;
523 		goto out_put;
524 	}
525 
526 	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
527 	err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
528 	if (err)
529 		goto write_error;
530 
531 	data_size = offset + len;
532 	memset(ubi->peb_buf1 + offset, 0xFF, len);
533 
534 	/* Read everything before the area where the write failure happened */
535 	if (offset > 0) {
536 		err = ubi_io_read_data(ubi, ubi->peb_buf1, pnum, 0, offset);
537 		if (err && err != UBI_IO_BITFLIPS)
538 			goto out_put;
539 	}
540 
541 	memcpy(ubi->peb_buf1 + offset, buf, len);
542 
543 	err = ubi_io_write_data(ubi, ubi->peb_buf1, new_pnum, 0, data_size);
544 	if (err)
545 		goto write_error;
546 
547 	mutex_unlock(&ubi->buf_mutex);
548 	ubi_free_vid_hdr(ubi, vid_hdr);
549 
550 	vol->eba_tbl[lnum] = new_pnum;
551 	ubi_wl_put_peb(ubi, pnum, 1);
552 
553 	ubi_msg("data was successfully recovered");
554 	return 0;
555 
556 out_put:
557 	mutex_unlock(&ubi->buf_mutex);
558 	ubi_wl_put_peb(ubi, new_pnum, 1);
559 	ubi_free_vid_hdr(ubi, vid_hdr);
560 	return err;
561 
562 write_error:
563 	/*
564 	 * Bad luck? This physical eraseblock is bad too? Crud. Let's try to
565 	 * get another one.
566 	 */
567 	ubi_warn("failed to write to PEB %d", new_pnum);
568 	ubi_wl_put_peb(ubi, new_pnum, 1);
569 	if (++tries > UBI_IO_RETRIES) {
570 		mutex_unlock(&ubi->buf_mutex);
571 		ubi_free_vid_hdr(ubi, vid_hdr);
572 		return err;
573 	}
574 	ubi_msg("try again");
575 	goto retry;
576 }
577 
578 /**
579  * ubi_eba_write_leb - write data to dynamic volume.
580  * @ubi: UBI device description object
581  * @vol: volume description object
582  * @lnum: logical eraseblock number
583  * @buf: the data to write
584  * @offset: offset within the logical eraseblock where to write
585  * @len: how many bytes to write
586  * @dtype: data type
587  *
588  * This function writes data to logical eraseblock @lnum of a dynamic volume
589  * @vol. Returns zero in case of success and a negative error code in case
590  * of failure. In case of error, it is possible that something was still
591  * written to the flash media, but may be some garbage.
592  */
593 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
594 		      const void *buf, int offset, int len, int dtype)
595 {
596 	int err, pnum, tries = 0, vol_id = vol->vol_id;
597 	struct ubi_vid_hdr *vid_hdr;
598 
599 	if (ubi->ro_mode)
600 		return -EROFS;
601 
602 	err = leb_write_lock(ubi, vol_id, lnum);
603 	if (err)
604 		return err;
605 
606 	pnum = vol->eba_tbl[lnum];
607 	if (pnum >= 0) {
608 		dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d",
609 			len, offset, vol_id, lnum, pnum);
610 
611 		err = ubi_io_write_data(ubi, buf, pnum, offset, len);
612 		if (err) {
613 			ubi_warn("failed to write data to PEB %d", pnum);
614 			if (err == -EIO && ubi->bad_allowed)
615 				err = recover_peb(ubi, pnum, vol_id, lnum, buf,
616 						  offset, len);
617 			if (err)
618 				ubi_ro_mode(ubi);
619 		}
620 		leb_write_unlock(ubi, vol_id, lnum);
621 		return err;
622 	}
623 
624 	/*
625 	 * The logical eraseblock is not mapped. We have to get a free physical
626 	 * eraseblock and write the volume identifier header there first.
627 	 */
628 	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
629 	if (!vid_hdr) {
630 		leb_write_unlock(ubi, vol_id, lnum);
631 		return -ENOMEM;
632 	}
633 
634 	vid_hdr->vol_type = UBI_VID_DYNAMIC;
635 	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
636 	vid_hdr->vol_id = cpu_to_be32(vol_id);
637 	vid_hdr->lnum = cpu_to_be32(lnum);
638 	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
639 	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
640 
641 retry:
642 	pnum = ubi_wl_get_peb(ubi, dtype);
643 	if (pnum < 0) {
644 		ubi_free_vid_hdr(ubi, vid_hdr);
645 		leb_write_unlock(ubi, vol_id, lnum);
646 		return pnum;
647 	}
648 
649 	dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
650 		len, offset, vol_id, lnum, pnum);
651 
652 	err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
653 	if (err) {
654 		ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
655 			 vol_id, lnum, pnum);
656 		goto write_error;
657 	}
658 
659 	if (len) {
660 		err = ubi_io_write_data(ubi, buf, pnum, offset, len);
661 		if (err) {
662 			ubi_warn("failed to write %d bytes at offset %d of "
663 				 "LEB %d:%d, PEB %d", len, offset, vol_id,
664 				 lnum, pnum);
665 			goto write_error;
666 		}
667 	}
668 
669 	vol->eba_tbl[lnum] = pnum;
670 
671 	leb_write_unlock(ubi, vol_id, lnum);
672 	ubi_free_vid_hdr(ubi, vid_hdr);
673 	return 0;
674 
675 write_error:
676 	if (err != -EIO || !ubi->bad_allowed) {
677 		ubi_ro_mode(ubi);
678 		leb_write_unlock(ubi, vol_id, lnum);
679 		ubi_free_vid_hdr(ubi, vid_hdr);
680 		return err;
681 	}
682 
683 	/*
684 	 * Fortunately, this is the first write operation to this physical
685 	 * eraseblock, so just put it and request a new one. We assume that if
686 	 * this physical eraseblock went bad, the erase code will handle that.
687 	 */
688 	err = ubi_wl_put_peb(ubi, pnum, 1);
689 	if (err || ++tries > UBI_IO_RETRIES) {
690 		ubi_ro_mode(ubi);
691 		leb_write_unlock(ubi, vol_id, lnum);
692 		ubi_free_vid_hdr(ubi, vid_hdr);
693 		return err;
694 	}
695 
696 	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
697 	ubi_msg("try another PEB");
698 	goto retry;
699 }
700 
701 /**
702  * ubi_eba_write_leb_st - write data to static volume.
703  * @ubi: UBI device description object
704  * @vol: volume description object
705  * @lnum: logical eraseblock number
706  * @buf: data to write
707  * @len: how many bytes to write
708  * @dtype: data type
709  * @used_ebs: how many logical eraseblocks will this volume contain
710  *
711  * This function writes data to logical eraseblock @lnum of static volume
712  * @vol. The @used_ebs argument should contain total number of logical
713  * eraseblock in this static volume.
714  *
715  * When writing to the last logical eraseblock, the @len argument doesn't have
716  * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent
717  * to the real data size, although the @buf buffer has to contain the
718  * alignment. In all other cases, @len has to be aligned.
719  *
720  * It is prohibited to write more then once to logical eraseblocks of static
721  * volumes. This function returns zero in case of success and a negative error
722  * code in case of failure.
723  */
724 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
725 			 int lnum, const void *buf, int len, int dtype,
726 			 int used_ebs)
727 {
728 	int err, pnum, tries = 0, data_size = len, vol_id = vol->vol_id;
729 	struct ubi_vid_hdr *vid_hdr;
730 	uint32_t crc;
731 
732 	if (ubi->ro_mode)
733 		return -EROFS;
734 
735 	if (lnum == used_ebs - 1)
736 		/* If this is the last LEB @len may be unaligned */
737 		len = ALIGN(data_size, ubi->min_io_size);
738 	else
739 		ubi_assert(!(len & (ubi->min_io_size - 1)));
740 
741 	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
742 	if (!vid_hdr)
743 		return -ENOMEM;
744 
745 	err = leb_write_lock(ubi, vol_id, lnum);
746 	if (err) {
747 		ubi_free_vid_hdr(ubi, vid_hdr);
748 		return err;
749 	}
750 
751 	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
752 	vid_hdr->vol_id = cpu_to_be32(vol_id);
753 	vid_hdr->lnum = cpu_to_be32(lnum);
754 	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
755 	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
756 
757 	crc = crc32(UBI_CRC32_INIT, buf, data_size);
758 	vid_hdr->vol_type = UBI_VID_STATIC;
759 	vid_hdr->data_size = cpu_to_be32(data_size);
760 	vid_hdr->used_ebs = cpu_to_be32(used_ebs);
761 	vid_hdr->data_crc = cpu_to_be32(crc);
762 
763 retry:
764 	pnum = ubi_wl_get_peb(ubi, dtype);
765 	if (pnum < 0) {
766 		ubi_free_vid_hdr(ubi, vid_hdr);
767 		leb_write_unlock(ubi, vol_id, lnum);
768 		return pnum;
769 	}
770 
771 	dbg_eba("write VID hdr and %d bytes at LEB %d:%d, PEB %d, used_ebs %d",
772 		len, vol_id, lnum, pnum, used_ebs);
773 
774 	err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
775 	if (err) {
776 		ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
777 			 vol_id, lnum, pnum);
778 		goto write_error;
779 	}
780 
781 	err = ubi_io_write_data(ubi, buf, pnum, 0, len);
782 	if (err) {
783 		ubi_warn("failed to write %d bytes of data to PEB %d",
784 			 len, pnum);
785 		goto write_error;
786 	}
787 
788 	ubi_assert(vol->eba_tbl[lnum] < 0);
789 	vol->eba_tbl[lnum] = pnum;
790 
791 	leb_write_unlock(ubi, vol_id, lnum);
792 	ubi_free_vid_hdr(ubi, vid_hdr);
793 	return 0;
794 
795 write_error:
796 	if (err != -EIO || !ubi->bad_allowed) {
797 		/*
798 		 * This flash device does not admit of bad eraseblocks or
799 		 * something nasty and unexpected happened. Switch to read-only
800 		 * mode just in case.
801 		 */
802 		ubi_ro_mode(ubi);
803 		leb_write_unlock(ubi, vol_id, lnum);
804 		ubi_free_vid_hdr(ubi, vid_hdr);
805 		return err;
806 	}
807 
808 	err = ubi_wl_put_peb(ubi, pnum, 1);
809 	if (err || ++tries > UBI_IO_RETRIES) {
810 		ubi_ro_mode(ubi);
811 		leb_write_unlock(ubi, vol_id, lnum);
812 		ubi_free_vid_hdr(ubi, vid_hdr);
813 		return err;
814 	}
815 
816 	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
817 	ubi_msg("try another PEB");
818 	goto retry;
819 }
820 
821 /*
822  * ubi_eba_atomic_leb_change - change logical eraseblock atomically.
823  * @ubi: UBI device description object
824  * @vol: volume description object
825  * @lnum: logical eraseblock number
826  * @buf: data to write
827  * @len: how many bytes to write
828  * @dtype: data type
829  *
830  * This function changes the contents of a logical eraseblock atomically. @buf
831  * has to contain new logical eraseblock data, and @len - the length of the
832  * data, which has to be aligned. This function guarantees that in case of an
833  * unclean reboot the old contents is preserved. Returns zero in case of
834  * success and a negative error code in case of failure.
835  *
836  * UBI reserves one LEB for the "atomic LEB change" operation, so only one
837  * LEB change may be done at a time. This is ensured by @ubi->alc_mutex.
838  */
839 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
840 			      int lnum, const void *buf, int len, int dtype)
841 {
842 	int err, pnum, tries = 0, vol_id = vol->vol_id;
843 	struct ubi_vid_hdr *vid_hdr;
844 	uint32_t crc;
845 
846 	if (ubi->ro_mode)
847 		return -EROFS;
848 
849 	if (len == 0) {
850 		/*
851 		 * Special case when data length is zero. In this case the LEB
852 		 * has to be unmapped and mapped somewhere else.
853 		 */
854 		err = ubi_eba_unmap_leb(ubi, vol, lnum);
855 		if (err)
856 			return err;
857 		return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0, dtype);
858 	}
859 
860 	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
861 	if (!vid_hdr)
862 		return -ENOMEM;
863 
864 	mutex_lock(&ubi->alc_mutex);
865 	err = leb_write_lock(ubi, vol_id, lnum);
866 	if (err)
867 		goto out_mutex;
868 
869 	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
870 	vid_hdr->vol_id = cpu_to_be32(vol_id);
871 	vid_hdr->lnum = cpu_to_be32(lnum);
872 	vid_hdr->compat = ubi_get_compat(ubi, vol_id);
873 	vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
874 
875 	crc = crc32(UBI_CRC32_INIT, buf, len);
876 	vid_hdr->vol_type = UBI_VID_DYNAMIC;
877 	vid_hdr->data_size = cpu_to_be32(len);
878 	vid_hdr->copy_flag = 1;
879 	vid_hdr->data_crc = cpu_to_be32(crc);
880 
881 retry:
882 	pnum = ubi_wl_get_peb(ubi, dtype);
883 	if (pnum < 0) {
884 		err = pnum;
885 		goto out_leb_unlock;
886 	}
887 
888 	dbg_eba("change LEB %d:%d, PEB %d, write VID hdr to PEB %d",
889 		vol_id, lnum, vol->eba_tbl[lnum], pnum);
890 
891 	err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr);
892 	if (err) {
893 		ubi_warn("failed to write VID header to LEB %d:%d, PEB %d",
894 			 vol_id, lnum, pnum);
895 		goto write_error;
896 	}
897 
898 	err = ubi_io_write_data(ubi, buf, pnum, 0, len);
899 	if (err) {
900 		ubi_warn("failed to write %d bytes of data to PEB %d",
901 			 len, pnum);
902 		goto write_error;
903 	}
904 
905 	if (vol->eba_tbl[lnum] >= 0) {
906 		err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 0);
907 		if (err)
908 			goto out_leb_unlock;
909 	}
910 
911 	vol->eba_tbl[lnum] = pnum;
912 
913 out_leb_unlock:
914 	leb_write_unlock(ubi, vol_id, lnum);
915 out_mutex:
916 	mutex_unlock(&ubi->alc_mutex);
917 	ubi_free_vid_hdr(ubi, vid_hdr);
918 	return err;
919 
920 write_error:
921 	if (err != -EIO || !ubi->bad_allowed) {
922 		/*
923 		 * This flash device does not admit of bad eraseblocks or
924 		 * something nasty and unexpected happened. Switch to read-only
925 		 * mode just in case.
926 		 */
927 		ubi_ro_mode(ubi);
928 		goto out_leb_unlock;
929 	}
930 
931 	err = ubi_wl_put_peb(ubi, pnum, 1);
932 	if (err || ++tries > UBI_IO_RETRIES) {
933 		ubi_ro_mode(ubi);
934 		goto out_leb_unlock;
935 	}
936 
937 	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
938 	ubi_msg("try another PEB");
939 	goto retry;
940 }
941 
942 /**
943  * ubi_eba_copy_leb - copy logical eraseblock.
944  * @ubi: UBI device description object
945  * @from: physical eraseblock number from where to copy
946  * @to: physical eraseblock number where to copy
947  * @vid_hdr: VID header of the @from physical eraseblock
948  *
949  * This function copies logical eraseblock from physical eraseblock @from to
950  * physical eraseblock @to. The @vid_hdr buffer may be changed by this
951  * function. Returns:
952  *   o %0  in case of success;
953  *   o %1 if the operation was canceled and should be tried later (e.g.,
954  *     because a bit-flip was detected at the target PEB);
955  *   o %2 if the volume is being deleted and this LEB should not be moved.
956  */
957 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
958 		     struct ubi_vid_hdr *vid_hdr)
959 {
960 	int err, vol_id, lnum, data_size, aldata_size, idx;
961 	struct ubi_volume *vol;
962 	uint32_t crc;
963 
964 	vol_id = be32_to_cpu(vid_hdr->vol_id);
965 	lnum = be32_to_cpu(vid_hdr->lnum);
966 
967 	dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
968 
969 	if (vid_hdr->vol_type == UBI_VID_STATIC) {
970 		data_size = be32_to_cpu(vid_hdr->data_size);
971 		aldata_size = ALIGN(data_size, ubi->min_io_size);
972 	} else
973 		data_size = aldata_size =
974 			    ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
975 
976 	idx = vol_id2idx(ubi, vol_id);
977 	spin_lock(&ubi->volumes_lock);
978 	/*
979 	 * Note, we may race with volume deletion, which means that the volume
980 	 * this logical eraseblock belongs to might be being deleted. Since the
981 	 * volume deletion unmaps all the volume's logical eraseblocks, it will
982 	 * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish.
983 	 */
984 	vol = ubi->volumes[idx];
985 	if (!vol) {
986 		/* No need to do further work, cancel */
987 		dbg_eba("volume %d is being removed, cancel", vol_id);
988 		spin_unlock(&ubi->volumes_lock);
989 		return 2;
990 	}
991 	spin_unlock(&ubi->volumes_lock);
992 
993 	/*
994 	 * We do not want anybody to write to this logical eraseblock while we
995 	 * are moving it, so lock it.
996 	 *
997 	 * Note, we are using non-waiting locking here, because we cannot sleep
998 	 * on the LEB, since it may cause deadlocks. Indeed, imagine a task is
999 	 * unmapping the LEB which is mapped to the PEB we are going to move
1000 	 * (@from). This task locks the LEB and goes sleep in the
1001 	 * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are
1002 	 * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the
1003 	 * LEB is already locked, we just do not move it and return %1.
1004 	 */
1005 	err = leb_write_trylock(ubi, vol_id, lnum);
1006 	if (err) {
1007 		dbg_eba("contention on LEB %d:%d, cancel", vol_id, lnum);
1008 		return err;
1009 	}
1010 
1011 	/*
1012 	 * The LEB might have been put meanwhile, and the task which put it is
1013 	 * probably waiting on @ubi->move_mutex. No need to continue the work,
1014 	 * cancel it.
1015 	 */
1016 	if (vol->eba_tbl[lnum] != from) {
1017 		dbg_eba("LEB %d:%d is no longer mapped to PEB %d, mapped to "
1018 			"PEB %d, cancel", vol_id, lnum, from,
1019 			vol->eba_tbl[lnum]);
1020 		err = 1;
1021 		goto out_unlock_leb;
1022 	}
1023 
1024 	/*
1025 	 * OK, now the LEB is locked and we can safely start moving iy. Since
1026 	 * this function utilizes thie @ubi->peb1_buf buffer which is shared
1027 	 * with some other functions, so lock the buffer by taking the
1028 	 * @ubi->buf_mutex.
1029 	 */
1030 	mutex_lock(&ubi->buf_mutex);
1031 	dbg_eba("read %d bytes of data", aldata_size);
1032 	err = ubi_io_read_data(ubi, ubi->peb_buf1, from, 0, aldata_size);
1033 	if (err && err != UBI_IO_BITFLIPS) {
1034 		ubi_warn("error %d while reading data from PEB %d",
1035 			 err, from);
1036 		goto out_unlock_buf;
1037 	}
1038 
1039 	/*
1040 	 * Now we have got to calculate how much data we have to to copy. In
1041 	 * case of a static volume it is fairly easy - the VID header contains
1042 	 * the data size. In case of a dynamic volume it is more difficult - we
1043 	 * have to read the contents, cut 0xFF bytes from the end and copy only
1044 	 * the first part. We must do this to avoid writing 0xFF bytes as it
1045 	 * may have some side-effects. And not only this. It is important not
1046 	 * to include those 0xFFs to CRC because later the they may be filled
1047 	 * by data.
1048 	 */
1049 	if (vid_hdr->vol_type == UBI_VID_DYNAMIC)
1050 		aldata_size = data_size =
1051 			ubi_calc_data_len(ubi, ubi->peb_buf1, data_size);
1052 
1053 	cond_resched();
1054 	crc = crc32(UBI_CRC32_INIT, ubi->peb_buf1, data_size);
1055 	cond_resched();
1056 
1057 	/*
1058 	 * It may turn out to me that the whole @from physical eraseblock
1059 	 * contains only 0xFF bytes. Then we have to only write the VID header
1060 	 * and do not write any data. This also means we should not set
1061 	 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc.
1062 	 */
1063 	if (data_size > 0) {
1064 		vid_hdr->copy_flag = 1;
1065 		vid_hdr->data_size = cpu_to_be32(data_size);
1066 		vid_hdr->data_crc = cpu_to_be32(crc);
1067 	}
1068 	vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
1069 
1070 	err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1071 	if (err)
1072 		goto out_unlock_buf;
1073 
1074 	cond_resched();
1075 
1076 	/* Read the VID header back and check if it was written correctly */
1077 	err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1);
1078 	if (err) {
1079 		if (err != UBI_IO_BITFLIPS)
1080 			ubi_warn("cannot read VID header back from PEB %d", to);
1081 		else
1082 			err = 1;
1083 		goto out_unlock_buf;
1084 	}
1085 
1086 	if (data_size > 0) {
1087 		err = ubi_io_write_data(ubi, ubi->peb_buf1, to, 0, aldata_size);
1088 		if (err)
1089 			goto out_unlock_buf;
1090 
1091 		cond_resched();
1092 
1093 		/*
1094 		 * We've written the data and are going to read it back to make
1095 		 * sure it was written correctly.
1096 		 */
1097 
1098 		err = ubi_io_read_data(ubi, ubi->peb_buf2, to, 0, aldata_size);
1099 		if (err) {
1100 			if (err != UBI_IO_BITFLIPS)
1101 				ubi_warn("cannot read data back from PEB %d",
1102 					 to);
1103 			else
1104 				err = 1;
1105 			goto out_unlock_buf;
1106 		}
1107 
1108 		cond_resched();
1109 
1110 		if (memcmp(ubi->peb_buf1, ubi->peb_buf2, aldata_size)) {
1111 			ubi_warn("read data back from PEB %d - it is different",
1112 				 to);
1113 			goto out_unlock_buf;
1114 		}
1115 	}
1116 
1117 	ubi_assert(vol->eba_tbl[lnum] == from);
1118 	vol->eba_tbl[lnum] = to;
1119 
1120 out_unlock_buf:
1121 	mutex_unlock(&ubi->buf_mutex);
1122 out_unlock_leb:
1123 	leb_write_unlock(ubi, vol_id, lnum);
1124 	return err;
1125 }
1126 
1127 /**
1128  * ubi_eba_init_scan - initialize the EBA sub-system using scanning information.
1129  * @ubi: UBI device description object
1130  * @si: scanning information
1131  *
1132  * This function returns zero in case of success and a negative error code in
1133  * case of failure.
1134  */
1135 int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1136 {
1137 	int i, j, err, num_volumes;
1138 	struct ubi_scan_volume *sv;
1139 	struct ubi_volume *vol;
1140 	struct ubi_scan_leb *seb;
1141 	struct rb_node *rb;
1142 
1143 	dbg_eba("initialize EBA sub-system");
1144 
1145 	spin_lock_init(&ubi->ltree_lock);
1146 	mutex_init(&ubi->alc_mutex);
1147 	ubi->ltree = RB_ROOT;
1148 
1149 	ubi->global_sqnum = si->max_sqnum + 1;
1150 	num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT;
1151 
1152 	for (i = 0; i < num_volumes; i++) {
1153 		vol = ubi->volumes[i];
1154 		if (!vol)
1155 			continue;
1156 
1157 		cond_resched();
1158 
1159 		vol->eba_tbl = kmalloc(vol->reserved_pebs * sizeof(int),
1160 				       GFP_KERNEL);
1161 		if (!vol->eba_tbl) {
1162 			err = -ENOMEM;
1163 			goto out_free;
1164 		}
1165 
1166 		for (j = 0; j < vol->reserved_pebs; j++)
1167 			vol->eba_tbl[j] = UBI_LEB_UNMAPPED;
1168 
1169 		sv = ubi_scan_find_sv(si, idx2vol_id(ubi, i));
1170 		if (!sv)
1171 			continue;
1172 
1173 		ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
1174 			if (seb->lnum >= vol->reserved_pebs)
1175 				/*
1176 				 * This may happen in case of an unclean reboot
1177 				 * during re-size.
1178 				 */
1179 				ubi_scan_move_to_list(sv, seb, &si->erase);
1180 			vol->eba_tbl[seb->lnum] = seb->pnum;
1181 		}
1182 	}
1183 
1184 	if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
1185 		ubi_err("no enough physical eraseblocks (%d, need %d)",
1186 			ubi->avail_pebs, EBA_RESERVED_PEBS);
1187 		err = -ENOSPC;
1188 		goto out_free;
1189 	}
1190 	ubi->avail_pebs -= EBA_RESERVED_PEBS;
1191 	ubi->rsvd_pebs += EBA_RESERVED_PEBS;
1192 
1193 	if (ubi->bad_allowed) {
1194 		ubi_calculate_reserved(ubi);
1195 
1196 		if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1197 			/* No enough free physical eraseblocks */
1198 			ubi->beb_rsvd_pebs = ubi->avail_pebs;
1199 			ubi_warn("cannot reserve enough PEBs for bad PEB "
1200 				 "handling, reserved %d, need %d",
1201 				 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1202 		} else
1203 			ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1204 
1205 		ubi->avail_pebs -= ubi->beb_rsvd_pebs;
1206 		ubi->rsvd_pebs  += ubi->beb_rsvd_pebs;
1207 	}
1208 
1209 	dbg_eba("EBA sub-system is initialized");
1210 	return 0;
1211 
1212 out_free:
1213 	for (i = 0; i < num_volumes; i++) {
1214 		if (!ubi->volumes[i])
1215 			continue;
1216 		kfree(ubi->volumes[i]->eba_tbl);
1217 	}
1218 	return err;
1219 }
1220