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