Linux 4.9.89
[linux/fpc-iii.git] / drivers / mtd / ubi / misc.c
blob989036c681b8b1993fe0e7e8b57838307cca8a26
1 /*
2 * Copyright (c) International Business Machines Corp., 2006
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.
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.
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
18 * Author: Artem Bityutskiy (Битюцкий Артём)
21 /* Here we keep miscellaneous functions which are used all over the UBI code */
23 #include "ubi.h"
25 /**
26 * calc_data_len - calculate how much real data is stored in a buffer.
27 * @ubi: UBI device description object
28 * @buf: a buffer with the contents of the physical eraseblock
29 * @length: the buffer length
31 * This function calculates how much "real data" is stored in @buf and returnes
32 * the length. Continuous 0xFF bytes at the end of the buffer are not
33 * considered as "real data".
35 int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
36 int length)
38 int i;
40 ubi_assert(!(length & (ubi->min_io_size - 1)));
42 for (i = length - 1; i >= 0; i--)
43 if (((const uint8_t *)buf)[i] != 0xFF)
44 break;
46 /* The resulting length must be aligned to the minimum flash I/O size */
47 length = ALIGN(i + 1, ubi->min_io_size);
48 return length;
51 /**
52 * ubi_check_volume - check the contents of a static volume.
53 * @ubi: UBI device description object
54 * @vol_id: ID of the volume to check
56 * This function checks if static volume @vol_id is corrupted by fully reading
57 * it and checking data CRC. This function returns %0 if the volume is not
58 * corrupted, %1 if it is corrupted and a negative error code in case of
59 * failure. Dynamic volumes are not checked and zero is returned immediately.
61 int ubi_check_volume(struct ubi_device *ubi, int vol_id)
63 void *buf;
64 int err = 0, i;
65 struct ubi_volume *vol = ubi->volumes[vol_id];
67 if (vol->vol_type != UBI_STATIC_VOLUME)
68 return 0;
70 buf = vmalloc(vol->usable_leb_size);
71 if (!buf)
72 return -ENOMEM;
74 for (i = 0; i < vol->used_ebs; i++) {
75 int size;
77 cond_resched();
79 if (i == vol->used_ebs - 1)
80 size = vol->last_eb_bytes;
81 else
82 size = vol->usable_leb_size;
84 err = ubi_eba_read_leb(ubi, vol, i, buf, 0, size, 1);
85 if (err) {
86 if (mtd_is_eccerr(err))
87 err = 1;
88 break;
92 vfree(buf);
93 return err;
96 /**
97 * ubi_update_reserved - update bad eraseblock handling accounting data.
98 * @ubi: UBI device description object
100 * This function calculates the gap between current number of PEBs reserved for
101 * bad eraseblock handling and the required level of PEBs that must be
102 * reserved, and if necessary, reserves more PEBs to fill that gap, according
103 * to availability. Should be called with ubi->volumes_lock held.
105 void ubi_update_reserved(struct ubi_device *ubi)
107 int need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs;
109 if (need <= 0 || ubi->avail_pebs == 0)
110 return;
112 need = min_t(int, need, ubi->avail_pebs);
113 ubi->avail_pebs -= need;
114 ubi->rsvd_pebs += need;
115 ubi->beb_rsvd_pebs += need;
116 ubi_msg(ubi, "reserved more %d PEBs for bad PEB handling", need);
120 * ubi_calculate_reserved - calculate how many PEBs must be reserved for bad
121 * eraseblock handling.
122 * @ubi: UBI device description object
124 void ubi_calculate_reserved(struct ubi_device *ubi)
127 * Calculate the actual number of PEBs currently needed to be reserved
128 * for future bad eraseblock handling.
130 ubi->beb_rsvd_level = ubi->bad_peb_limit - ubi->bad_peb_count;
131 if (ubi->beb_rsvd_level < 0) {
132 ubi->beb_rsvd_level = 0;
133 ubi_warn(ubi, "number of bad PEBs (%d) is above the expected limit (%d), not reserving any PEBs for bad PEB handling, will use available PEBs (if any)",
134 ubi->bad_peb_count, ubi->bad_peb_limit);
139 * ubi_check_pattern - check if buffer contains only a certain byte pattern.
140 * @buf: buffer to check
141 * @patt: the pattern to check
142 * @size: buffer size in bytes
144 * This function returns %1 in there are only @patt bytes in @buf, and %0 if
145 * something else was also found.
147 int ubi_check_pattern(const void *buf, uint8_t patt, int size)
149 int i;
151 for (i = 0; i < size; i++)
152 if (((const uint8_t *)buf)[i] != patt)
153 return 0;
154 return 1;
157 /* Normal UBI messages */
158 void ubi_msg(const struct ubi_device *ubi, const char *fmt, ...)
160 struct va_format vaf;
161 va_list args;
163 va_start(args, fmt);
165 vaf.fmt = fmt;
166 vaf.va = &args;
168 pr_notice(UBI_NAME_STR "%d: %pV\n", ubi->ubi_num, &vaf);
170 va_end(args);
173 /* UBI warning messages */
174 void ubi_warn(const struct ubi_device *ubi, const char *fmt, ...)
176 struct va_format vaf;
177 va_list args;
179 va_start(args, fmt);
181 vaf.fmt = fmt;
182 vaf.va = &args;
184 pr_warn(UBI_NAME_STR "%d warning: %ps: %pV\n",
185 ubi->ubi_num, __builtin_return_address(0), &vaf);
187 va_end(args);
190 /* UBI error messages */
191 void ubi_err(const struct ubi_device *ubi, const char *fmt, ...)
193 struct va_format vaf;
194 va_list args;
196 va_start(args, fmt);
198 vaf.fmt = fmt;
199 vaf.va = &args;
201 pr_err(UBI_NAME_STR "%d error: %ps: %pV\n",
202 ubi->ubi_num, __builtin_return_address(0), &vaf);
203 va_end(args);