Commit | Line | Data |
---|---|---|
1e51764a AB |
1 | /* |
2 | * This file is part of UBIFS. | |
3 | * | |
4 | * Copyright (C) 2006-2008 Nokia Corporation | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License version 2 as published by | |
8 | * the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along with | |
16 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
17 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | * | |
19 | * Authors: Adrian Hunter | |
20 | * Artem Bityutskiy (Битюцкий Артём) | |
21 | */ | |
22 | ||
23 | /* | |
24 | * This file implements functions needed to recover from unclean un-mounts. | |
25 | * When UBIFS is mounted, it checks a flag on the master node to determine if | |
af901ca1 | 26 | * an un-mount was completed successfully. If not, the process of mounting |
6fb4374f | 27 | * incorporates additional checking and fixing of on-flash data structures. |
1e51764a AB |
28 | * UBIFS always cleans away all remnants of an unclean un-mount, so that |
29 | * errors do not accumulate. However UBIFS defers recovery if it is mounted | |
30 | * read-only, and the flash is not modified in that case. | |
be7b42a5 AB |
31 | * |
32 | * The general UBIFS approach to the recovery is that it recovers from | |
33 | * corruptions which could be caused by power cuts, but it refuses to recover | |
34 | * from corruption caused by other reasons. And UBIFS tries to distinguish | |
35 | * between these 2 reasons of corruptions and silently recover in the former | |
36 | * case and loudly complain in the latter case. | |
37 | * | |
38 | * UBIFS writes only to erased LEBs, so it writes only to the flash space | |
39 | * containing only 0xFFs. UBIFS also always writes strictly from the beginning | |
40 | * of the LEB to the end. And UBIFS assumes that the underlying flash media | |
2765df7d | 41 | * writes in @c->max_write_size bytes at a time. |
be7b42a5 AB |
42 | * |
43 | * Hence, if UBIFS finds a corrupted node at offset X, it expects only the min. | |
44 | * I/O unit corresponding to offset X to contain corrupted data, all the | |
45 | * following min. I/O units have to contain empty space (all 0xFFs). If this is | |
46 | * not true, the corruption cannot be the result of a power cut, and UBIFS | |
47 | * refuses to mount. | |
1e51764a AB |
48 | */ |
49 | ||
50 | #include <linux/crc32.h> | |
5a0e3ad6 | 51 | #include <linux/slab.h> |
1e51764a AB |
52 | #include "ubifs.h" |
53 | ||
54 | /** | |
55 | * is_empty - determine whether a buffer is empty (contains all 0xff). | |
56 | * @buf: buffer to clean | |
57 | * @len: length of buffer | |
58 | * | |
59 | * This function returns %1 if the buffer is empty (contains all 0xff) otherwise | |
60 | * %0 is returned. | |
61 | */ | |
62 | static int is_empty(void *buf, int len) | |
63 | { | |
64 | uint8_t *p = buf; | |
65 | int i; | |
66 | ||
67 | for (i = 0; i < len; i++) | |
68 | if (*p++ != 0xff) | |
69 | return 0; | |
70 | return 1; | |
71 | } | |
72 | ||
06112547 AB |
73 | /** |
74 | * first_non_ff - find offset of the first non-0xff byte. | |
75 | * @buf: buffer to search in | |
76 | * @len: length of buffer | |
77 | * | |
78 | * This function returns offset of the first non-0xff byte in @buf or %-1 if | |
79 | * the buffer contains only 0xff bytes. | |
80 | */ | |
81 | static int first_non_ff(void *buf, int len) | |
82 | { | |
83 | uint8_t *p = buf; | |
84 | int i; | |
85 | ||
86 | for (i = 0; i < len; i++) | |
87 | if (*p++ != 0xff) | |
88 | return i; | |
89 | return -1; | |
90 | } | |
91 | ||
1e51764a AB |
92 | /** |
93 | * get_master_node - get the last valid master node allowing for corruption. | |
94 | * @c: UBIFS file-system description object | |
95 | * @lnum: LEB number | |
96 | * @pbuf: buffer containing the LEB read, is returned here | |
97 | * @mst: master node, if found, is returned here | |
98 | * @cor: corruption, if found, is returned here | |
99 | * | |
100 | * This function allocates a buffer, reads the LEB into it, and finds and | |
101 | * returns the last valid master node allowing for one area of corruption. | |
102 | * The corrupt area, if there is one, must be consistent with the assumption | |
103 | * that it is the result of an unclean unmount while the master node was being | |
104 | * written. Under those circumstances, it is valid to use the previously written | |
105 | * master node. | |
106 | * | |
107 | * This function returns %0 on success and a negative error code on failure. | |
108 | */ | |
109 | static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf, | |
110 | struct ubifs_mst_node **mst, void **cor) | |
111 | { | |
112 | const int sz = c->mst_node_alsz; | |
113 | int err, offs, len; | |
114 | void *sbuf, *buf; | |
115 | ||
6fa3eb70 | 116 | sbuf = kmalloc(c->leb_size, GFP_KERNEL); |
1e51764a AB |
117 | if (!sbuf) |
118 | return -ENOMEM; | |
119 | ||
d304820a | 120 | err = ubifs_leb_read(c, lnum, sbuf, 0, c->leb_size, 0); |
1e51764a AB |
121 | if (err && err != -EBADMSG) |
122 | goto out_free; | |
123 | ||
124 | /* Find the first position that is definitely not a node */ | |
125 | offs = 0; | |
126 | buf = sbuf; | |
127 | len = c->leb_size; | |
128 | while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) { | |
129 | struct ubifs_ch *ch = buf; | |
130 | ||
131 | if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) | |
132 | break; | |
133 | offs += sz; | |
134 | buf += sz; | |
135 | len -= sz; | |
136 | } | |
137 | /* See if there was a valid master node before that */ | |
138 | if (offs) { | |
139 | int ret; | |
140 | ||
141 | offs -= sz; | |
142 | buf -= sz; | |
143 | len += sz; | |
144 | ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1); | |
145 | if (ret != SCANNED_A_NODE && offs) { | |
146 | /* Could have been corruption so check one place back */ | |
147 | offs -= sz; | |
148 | buf -= sz; | |
149 | len += sz; | |
150 | ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1); | |
151 | if (ret != SCANNED_A_NODE) | |
152 | /* | |
153 | * We accept only one area of corruption because | |
154 | * we are assuming that it was caused while | |
155 | * trying to write a master node. | |
156 | */ | |
157 | goto out_err; | |
158 | } | |
159 | if (ret == SCANNED_A_NODE) { | |
160 | struct ubifs_ch *ch = buf; | |
161 | ||
162 | if (ch->node_type != UBIFS_MST_NODE) | |
163 | goto out_err; | |
164 | dbg_rcvry("found a master node at %d:%d", lnum, offs); | |
165 | *mst = buf; | |
166 | offs += sz; | |
167 | buf += sz; | |
168 | len -= sz; | |
169 | } | |
170 | } | |
171 | /* Check for corruption */ | |
172 | if (offs < c->leb_size) { | |
173 | if (!is_empty(buf, min_t(int, len, sz))) { | |
174 | *cor = buf; | |
175 | dbg_rcvry("found corruption at %d:%d", lnum, offs); | |
176 | } | |
177 | offs += sz; | |
178 | buf += sz; | |
179 | len -= sz; | |
180 | } | |
181 | /* Check remaining empty space */ | |
182 | if (offs < c->leb_size) | |
183 | if (!is_empty(buf, len)) | |
184 | goto out_err; | |
185 | *pbuf = sbuf; | |
186 | return 0; | |
187 | ||
188 | out_err: | |
189 | err = -EINVAL; | |
190 | out_free: | |
6fa3eb70 | 191 | kfree(sbuf); |
1e51764a AB |
192 | *mst = NULL; |
193 | *cor = NULL; | |
194 | return err; | |
195 | } | |
196 | ||
197 | /** | |
198 | * write_rcvrd_mst_node - write recovered master node. | |
199 | * @c: UBIFS file-system description object | |
200 | * @mst: master node | |
201 | * | |
202 | * This function returns %0 on success and a negative error code on failure. | |
203 | */ | |
204 | static int write_rcvrd_mst_node(struct ubifs_info *c, | |
205 | struct ubifs_mst_node *mst) | |
206 | { | |
207 | int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz; | |
0ecb9529 | 208 | __le32 save_flags; |
1e51764a AB |
209 | |
210 | dbg_rcvry("recovery"); | |
211 | ||
212 | save_flags = mst->flags; | |
0ecb9529 | 213 | mst->flags |= cpu_to_le32(UBIFS_MST_RCVRY); |
1e51764a AB |
214 | |
215 | ubifs_prepare_node(c, mst, UBIFS_MST_NODE_SZ, 1); | |
b36a261e | 216 | err = ubifs_leb_change(c, lnum, mst, sz); |
1e51764a AB |
217 | if (err) |
218 | goto out; | |
b36a261e | 219 | err = ubifs_leb_change(c, lnum + 1, mst, sz); |
1e51764a AB |
220 | if (err) |
221 | goto out; | |
6fa3eb70 | 222 | c->mst_offs = 0; //MTK |
1e51764a AB |
223 | out: |
224 | mst->flags = save_flags; | |
225 | return err; | |
226 | } | |
227 | ||
228 | /** | |
229 | * ubifs_recover_master_node - recover the master node. | |
230 | * @c: UBIFS file-system description object | |
231 | * | |
232 | * This function recovers the master node from corruption that may occur due to | |
233 | * an unclean unmount. | |
234 | * | |
235 | * This function returns %0 on success and a negative error code on failure. | |
236 | */ | |
237 | int ubifs_recover_master_node(struct ubifs_info *c) | |
238 | { | |
239 | void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL; | |
240 | struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst; | |
241 | const int sz = c->mst_node_alsz; | |
242 | int err, offs1, offs2; | |
243 | ||
244 | dbg_rcvry("recovery"); | |
245 | ||
246 | err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1); | |
247 | if (err) | |
248 | goto out_free; | |
249 | ||
250 | err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2); | |
251 | if (err) | |
252 | goto out_free; | |
253 | ||
254 | if (mst1) { | |
255 | offs1 = (void *)mst1 - buf1; | |
256 | if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) && | |
257 | (offs1 == 0 && !cor1)) { | |
258 | /* | |
259 | * mst1 was written by recovery at offset 0 with no | |
260 | * corruption. | |
261 | */ | |
262 | dbg_rcvry("recovery recovery"); | |
263 | mst = mst1; | |
264 | } else if (mst2) { | |
265 | offs2 = (void *)mst2 - buf2; | |
266 | if (offs1 == offs2) { | |
267 | /* Same offset, so must be the same */ | |
268 | if (memcmp((void *)mst1 + UBIFS_CH_SZ, | |
269 | (void *)mst2 + UBIFS_CH_SZ, | |
270 | UBIFS_MST_NODE_SZ - UBIFS_CH_SZ)) | |
271 | goto out_err; | |
272 | mst = mst1; | |
273 | } else if (offs2 + sz == offs1) { | |
274 | /* 1st LEB was written, 2nd was not */ | |
275 | if (cor1) | |
276 | goto out_err; | |
277 | mst = mst1; | |
19495f70 AG |
278 | } else if (offs1 == 0 && |
279 | c->leb_size - offs2 - sz < sz) { | |
1e51764a AB |
280 | /* 1st LEB was unmapped and written, 2nd not */ |
281 | if (cor1) | |
282 | goto out_err; | |
283 | mst = mst1; | |
284 | } else | |
285 | goto out_err; | |
286 | } else { | |
287 | /* | |
288 | * 2nd LEB was unmapped and about to be written, so | |
289 | * there must be only one master node in the first LEB | |
290 | * and no corruption. | |
291 | */ | |
292 | if (offs1 != 0 || cor1) | |
293 | goto out_err; | |
294 | mst = mst1; | |
295 | } | |
296 | } else { | |
297 | if (!mst2) | |
298 | goto out_err; | |
299 | /* | |
300 | * 1st LEB was unmapped and about to be written, so there must | |
301 | * be no room left in 2nd LEB. | |
302 | */ | |
303 | offs2 = (void *)mst2 - buf2; | |
304 | if (offs2 + sz + sz <= c->leb_size) | |
305 | goto out_err; | |
306 | mst = mst2; | |
307 | } | |
308 | ||
348709ba | 309 | ubifs_msg("recovered master node from LEB %d", |
1e51764a AB |
310 | (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1)); |
311 | ||
312 | memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ); | |
313 | ||
2ef13294 | 314 | if (c->ro_mount) { |
1e51764a AB |
315 | /* Read-only mode. Keep a copy for switching to rw mode */ |
316 | c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL); | |
317 | if (!c->rcvrd_mst_node) { | |
318 | err = -ENOMEM; | |
319 | goto out_free; | |
320 | } | |
321 | memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ); | |
6e0d9fd3 AB |
322 | |
323 | /* | |
324 | * We had to recover the master node, which means there was an | |
325 | * unclean reboot. However, it is possible that the master node | |
326 | * is clean at this point, i.e., %UBIFS_MST_DIRTY is not set. | |
327 | * E.g., consider the following chain of events: | |
328 | * | |
329 | * 1. UBIFS was cleanly unmounted, so the master node is clean | |
330 | * 2. UBIFS is being mounted R/W and starts changing the master | |
331 | * node in the first (%UBIFS_MST_LNUM). A power cut happens, | |
332 | * so this LEB ends up with some amount of garbage at the | |
333 | * end. | |
334 | * 3. UBIFS is being mounted R/O. We reach this place and | |
335 | * recover the master node from the second LEB | |
336 | * (%UBIFS_MST_LNUM + 1). But we cannot update the media | |
337 | * because we are being mounted R/O. We have to defer the | |
338 | * operation. | |
339 | * 4. However, this master node (@c->mst_node) is marked as | |
340 | * clean (since the step 1). And if we just return, the | |
341 | * mount code will be confused and won't recover the master | |
342 | * node when it is re-mounter R/W later. | |
343 | * | |
344 | * Thus, to force the recovery by marking the master node as | |
345 | * dirty. | |
346 | */ | |
347 | c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY); | |
1e51764a AB |
348 | } else { |
349 | /* Write the recovered master node */ | |
350 | c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1; | |
351 | err = write_rcvrd_mst_node(c, c->mst_node); | |
352 | if (err) | |
353 | goto out_free; | |
354 | } | |
355 | ||
6fa3eb70 S |
356 | kfree(buf2); |
357 | kfree(buf1); | |
1e51764a AB |
358 | |
359 | return 0; | |
360 | ||
361 | out_err: | |
362 | err = -EINVAL; | |
363 | out_free: | |
364 | ubifs_err("failed to recover master node"); | |
365 | if (mst1) { | |
a6aae4dd | 366 | ubifs_err("dumping first master node"); |
edf6be24 | 367 | ubifs_dump_node(c, mst1); |
1e51764a AB |
368 | } |
369 | if (mst2) { | |
a6aae4dd | 370 | ubifs_err("dumping second master node"); |
edf6be24 | 371 | ubifs_dump_node(c, mst2); |
1e51764a | 372 | } |
6fa3eb70 S |
373 | kfree(buf2); |
374 | kfree(buf1); | |
1e51764a AB |
375 | return err; |
376 | } | |
377 | ||
378 | /** | |
379 | * ubifs_write_rcvrd_mst_node - write the recovered master node. | |
380 | * @c: UBIFS file-system description object | |
381 | * | |
382 | * This function writes the master node that was recovered during mounting in | |
383 | * read-only mode and must now be written because we are remounting rw. | |
384 | * | |
385 | * This function returns %0 on success and a negative error code on failure. | |
386 | */ | |
387 | int ubifs_write_rcvrd_mst_node(struct ubifs_info *c) | |
388 | { | |
389 | int err; | |
390 | ||
391 | if (!c->rcvrd_mst_node) | |
392 | return 0; | |
393 | c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY); | |
394 | c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY); | |
395 | err = write_rcvrd_mst_node(c, c->rcvrd_mst_node); | |
396 | if (err) | |
397 | return err; | |
398 | kfree(c->rcvrd_mst_node); | |
399 | c->rcvrd_mst_node = NULL; | |
400 | return 0; | |
401 | } | |
402 | ||
403 | /** | |
404 | * is_last_write - determine if an offset was in the last write to a LEB. | |
405 | * @c: UBIFS file-system description object | |
406 | * @buf: buffer to check | |
407 | * @offs: offset to check | |
408 | * | |
409 | * This function returns %1 if @offs was in the last write to the LEB whose data | |
2765df7d AB |
410 | * is in @buf, otherwise %0 is returned. The determination is made by checking |
411 | * for subsequent empty space starting from the next @c->max_write_size | |
412 | * boundary. | |
1e51764a AB |
413 | */ |
414 | static int is_last_write(const struct ubifs_info *c, void *buf, int offs) | |
415 | { | |
428ff9d2 | 416 | int empty_offs, check_len; |
1e51764a AB |
417 | uint8_t *p; |
418 | ||
1e51764a | 419 | /* |
2765df7d AB |
420 | * Round up to the next @c->max_write_size boundary i.e. @offs is in |
421 | * the last wbuf written. After that should be empty space. | |
1e51764a | 422 | */ |
2765df7d | 423 | empty_offs = ALIGN(offs + 1, c->max_write_size); |
1e51764a AB |
424 | check_len = c->leb_size - empty_offs; |
425 | p = buf + empty_offs - offs; | |
431102fe | 426 | return is_empty(p, check_len); |
1e51764a AB |
427 | } |
428 | ||
429 | /** | |
430 | * clean_buf - clean the data from an LEB sitting in a buffer. | |
431 | * @c: UBIFS file-system description object | |
432 | * @buf: buffer to clean | |
433 | * @lnum: LEB number to clean | |
434 | * @offs: offset from which to clean | |
435 | * @len: length of buffer | |
436 | * | |
437 | * This function pads up to the next min_io_size boundary (if there is one) and | |
438 | * sets empty space to all 0xff. @buf, @offs and @len are updated to the next | |
428ff9d2 | 439 | * @c->min_io_size boundary. |
1e51764a AB |
440 | */ |
441 | static void clean_buf(const struct ubifs_info *c, void **buf, int lnum, | |
442 | int *offs, int *len) | |
443 | { | |
444 | int empty_offs, pad_len; | |
445 | ||
446 | lnum = lnum; | |
447 | dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs); | |
448 | ||
1e51764a AB |
449 | ubifs_assert(!(*offs & 7)); |
450 | empty_offs = ALIGN(*offs, c->min_io_size); | |
451 | pad_len = empty_offs - *offs; | |
452 | ubifs_pad(c, *buf, pad_len); | |
453 | *offs += pad_len; | |
454 | *buf += pad_len; | |
455 | *len -= pad_len; | |
456 | memset(*buf, 0xff, c->leb_size - empty_offs); | |
457 | } | |
458 | ||
459 | /** | |
460 | * no_more_nodes - determine if there are no more nodes in a buffer. | |
461 | * @c: UBIFS file-system description object | |
462 | * @buf: buffer to check | |
463 | * @len: length of buffer | |
464 | * @lnum: LEB number of the LEB from which @buf was read | |
465 | * @offs: offset from which @buf was read | |
466 | * | |
de097578 AH |
467 | * This function ensures that the corrupted node at @offs is the last thing |
468 | * written to a LEB. This function returns %1 if more data is not found and | |
469 | * %0 if more data is found. | |
1e51764a AB |
470 | */ |
471 | static int no_more_nodes(const struct ubifs_info *c, void *buf, int len, | |
472 | int lnum, int offs) | |
473 | { | |
de097578 AH |
474 | struct ubifs_ch *ch = buf; |
475 | int skip, dlen = le32_to_cpu(ch->len); | |
1e51764a | 476 | |
de097578 | 477 | /* Check for empty space after the corrupt node's common header */ |
2765df7d | 478 | skip = ALIGN(offs + UBIFS_CH_SZ, c->max_write_size) - offs; |
de097578 AH |
479 | if (is_empty(buf + skip, len - skip)) |
480 | return 1; | |
481 | /* | |
482 | * The area after the common header size is not empty, so the common | |
483 | * header must be intact. Check it. | |
484 | */ | |
485 | if (ubifs_check_node(c, buf, lnum, offs, 1, 0) != -EUCLEAN) { | |
486 | dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs); | |
487 | return 0; | |
1e51764a | 488 | } |
de097578 | 489 | /* Now we know the corrupt node's length we can skip over it */ |
2765df7d | 490 | skip = ALIGN(offs + dlen, c->max_write_size) - offs; |
de097578 AH |
491 | /* After which there should be empty space */ |
492 | if (is_empty(buf + skip, len - skip)) | |
493 | return 1; | |
494 | dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip); | |
495 | return 0; | |
1e51764a AB |
496 | } |
497 | ||
498 | /** | |
499 | * fix_unclean_leb - fix an unclean LEB. | |
500 | * @c: UBIFS file-system description object | |
501 | * @sleb: scanned LEB information | |
502 | * @start: offset where scan started | |
503 | */ | |
504 | static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb, | |
505 | int start) | |
506 | { | |
507 | int lnum = sleb->lnum, endpt = start; | |
508 | ||
509 | /* Get the end offset of the last node we are keeping */ | |
510 | if (!list_empty(&sleb->nodes)) { | |
511 | struct ubifs_scan_node *snod; | |
512 | ||
513 | snod = list_entry(sleb->nodes.prev, | |
514 | struct ubifs_scan_node, list); | |
515 | endpt = snod->offs + snod->len; | |
516 | } | |
517 | ||
2ef13294 | 518 | if (c->ro_mount && !c->remounting_rw) { |
1e51764a AB |
519 | /* Add to recovery list */ |
520 | struct ubifs_unclean_leb *ucleb; | |
521 | ||
522 | dbg_rcvry("need to fix LEB %d start %d endpt %d", | |
523 | lnum, start, sleb->endpt); | |
524 | ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS); | |
525 | if (!ucleb) | |
526 | return -ENOMEM; | |
527 | ucleb->lnum = lnum; | |
528 | ucleb->endpt = endpt; | |
529 | list_add_tail(&ucleb->list, &c->unclean_leb_list); | |
530 | } else { | |
531 | /* Write the fixed LEB back to flash */ | |
532 | int err; | |
533 | ||
534 | dbg_rcvry("fixing LEB %d start %d endpt %d", | |
535 | lnum, start, sleb->endpt); | |
536 | if (endpt == 0) { | |
537 | err = ubifs_leb_unmap(c, lnum); | |
538 | if (err) | |
539 | return err; | |
540 | } else { | |
541 | int len = ALIGN(endpt, c->min_io_size); | |
542 | ||
543 | if (start) { | |
d304820a AB |
544 | err = ubifs_leb_read(c, lnum, sleb->buf, 0, |
545 | start, 1); | |
1e51764a AB |
546 | if (err) |
547 | return err; | |
548 | } | |
549 | /* Pad to min_io_size */ | |
550 | if (len > endpt) { | |
551 | int pad_len = len - ALIGN(endpt, 8); | |
552 | ||
553 | if (pad_len > 0) { | |
554 | void *buf = sleb->buf + len - pad_len; | |
555 | ||
556 | ubifs_pad(c, buf, pad_len); | |
557 | } | |
558 | } | |
b36a261e | 559 | err = ubifs_leb_change(c, lnum, sleb->buf, len); |
1e51764a AB |
560 | if (err) |
561 | return err; | |
562 | } | |
563 | } | |
564 | return 0; | |
565 | } | |
566 | ||
567 | /** | |
da8b94ea | 568 | * drop_last_group - drop the last group of nodes. |
1e51764a AB |
569 | * @sleb: scanned LEB information |
570 | * @offs: offset of dropped nodes is returned here | |
571 | * | |
bbf2b37a | 572 | * This is a helper function for 'ubifs_recover_leb()' which drops the last |
da8b94ea | 573 | * group of nodes of the scanned LEB. |
1e51764a | 574 | */ |
da8b94ea | 575 | static void drop_last_group(struct ubifs_scan_leb *sleb, int *offs) |
1e51764a | 576 | { |
1e51764a AB |
577 | while (!list_empty(&sleb->nodes)) { |
578 | struct ubifs_scan_node *snod; | |
579 | struct ubifs_ch *ch; | |
580 | ||
581 | snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, | |
582 | list); | |
583 | ch = snod->node; | |
584 | if (ch->group_type != UBIFS_IN_NODE_GROUP) | |
da8b94ea AB |
585 | break; |
586 | ||
587 | dbg_rcvry("dropping grouped node at %d:%d", | |
588 | sleb->lnum, snod->offs); | |
589 | *offs = snod->offs; | |
590 | list_del(&snod->list); | |
591 | kfree(snod); | |
592 | sleb->nodes_cnt -= 1; | |
593 | } | |
594 | } | |
595 | ||
596 | /** | |
597 | * drop_last_node - drop the last node. | |
598 | * @sleb: scanned LEB information | |
599 | * @offs: offset of dropped nodes is returned here | |
600 | * @grouped: non-zero if whole group of nodes have to be dropped | |
601 | * | |
602 | * This is a helper function for 'ubifs_recover_leb()' which drops the last | |
603 | * node of the scanned LEB. | |
604 | */ | |
605 | static void drop_last_node(struct ubifs_scan_leb *sleb, int *offs) | |
606 | { | |
607 | struct ubifs_scan_node *snod; | |
608 | ||
609 | if (!list_empty(&sleb->nodes)) { | |
610 | snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, | |
611 | list); | |
612 | ||
79fda517 AB |
613 | dbg_rcvry("dropping last node at %d:%d", |
614 | sleb->lnum, snod->offs); | |
1e51764a AB |
615 | *offs = snod->offs; |
616 | list_del(&snod->list); | |
617 | kfree(snod); | |
618 | sleb->nodes_cnt -= 1; | |
1e51764a | 619 | } |
1e51764a AB |
620 | } |
621 | ||
622 | /** | |
623 | * ubifs_recover_leb - scan and recover a LEB. | |
624 | * @c: UBIFS file-system description object | |
625 | * @lnum: LEB number | |
626 | * @offs: offset | |
627 | * @sbuf: LEB-sized buffer to use | |
efcfde54 AB |
628 | * @jhead: journal head number this LEB belongs to (%-1 if the LEB does not |
629 | * belong to any journal head) | |
1e51764a AB |
630 | * |
631 | * This function does a scan of a LEB, but caters for errors that might have | |
632 | * been caused by the unclean unmount from which we are attempting to recover. | |
ed43f2f0 AB |
633 | * Returns %0 in case of success, %-EUCLEAN if an unrecoverable corruption is |
634 | * found, and a negative error code in case of failure. | |
1e51764a AB |
635 | */ |
636 | struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum, | |
efcfde54 | 637 | int offs, void *sbuf, int jhead) |
1e51764a | 638 | { |
bbf2b37a | 639 | int ret = 0, err, len = c->leb_size - offs, start = offs, min_io_unit; |
efcfde54 | 640 | int grouped = jhead == -1 ? 0 : c->jheads[jhead].grouped; |
1e51764a AB |
641 | struct ubifs_scan_leb *sleb; |
642 | void *buf = sbuf + offs; | |
643 | ||
efcfde54 | 644 | dbg_rcvry("%d:%d, jhead %d, grouped %d", lnum, offs, jhead, grouped); |
1e51764a AB |
645 | |
646 | sleb = ubifs_start_scan(c, lnum, offs, sbuf); | |
647 | if (IS_ERR(sleb)) | |
648 | return sleb; | |
649 | ||
bbf2b37a | 650 | ubifs_assert(len >= 8); |
1e51764a | 651 | while (len >= 8) { |
1e51764a AB |
652 | dbg_scan("look at LEB %d:%d (%d bytes left)", |
653 | lnum, offs, len); | |
654 | ||
655 | cond_resched(); | |
656 | ||
657 | /* | |
658 | * Scan quietly until there is an error from which we cannot | |
659 | * recover | |
660 | */ | |
ab75950b | 661 | ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1); |
1e51764a AB |
662 | if (ret == SCANNED_A_NODE) { |
663 | /* A valid node, and not a padding node */ | |
664 | struct ubifs_ch *ch = buf; | |
665 | int node_len; | |
666 | ||
667 | err = ubifs_add_snod(c, sleb, buf, offs); | |
668 | if (err) | |
669 | goto error; | |
670 | node_len = ALIGN(le32_to_cpu(ch->len), 8); | |
671 | offs += node_len; | |
672 | buf += node_len; | |
673 | len -= node_len; | |
61799206 | 674 | } else if (ret > 0) { |
1e51764a AB |
675 | /* Padding bytes or a valid padding node */ |
676 | offs += ret; | |
677 | buf += ret; | |
678 | len -= ret; | |
61799206 AB |
679 | } else if (ret == SCANNED_EMPTY_SPACE || |
680 | ret == SCANNED_GARBAGE || | |
681 | ret == SCANNED_A_BAD_PAD_NODE || | |
682 | ret == SCANNED_A_CORRUPT_NODE) { | |
78437368 AB |
683 | dbg_rcvry("found corruption (%d) at %d:%d", |
684 | ret, lnum, offs); | |
1e51764a | 685 | break; |
61799206 | 686 | } else { |
a6aae4dd | 687 | ubifs_err("unexpected return value %d", ret); |
ed43f2f0 AB |
688 | err = -EINVAL; |
689 | goto error; | |
1e51764a AB |
690 | } |
691 | } | |
692 | ||
61799206 | 693 | if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE) { |
43e07073 | 694 | if (!is_last_write(c, buf, offs)) |
61799206 AB |
695 | goto corrupted_rescan; |
696 | } else if (ret == SCANNED_A_CORRUPT_NODE) { | |
43e07073 | 697 | if (!no_more_nodes(c, buf, len, lnum, offs)) |
61799206 AB |
698 | goto corrupted_rescan; |
699 | } else if (!is_empty(buf, len)) { | |
43e07073 | 700 | if (!is_last_write(c, buf, offs)) { |
06112547 AB |
701 | int corruption = first_non_ff(buf, len); |
702 | ||
be7b42a5 AB |
703 | /* |
704 | * See header comment for this file for more | |
705 | * explanations about the reasons we have this check. | |
706 | */ | |
79fda517 AB |
707 | ubifs_err("corrupt empty space LEB %d:%d, corruption starts at %d", |
708 | lnum, offs, corruption); | |
06112547 | 709 | /* Make sure we dump interesting non-0xFF data */ |
10ac2797 | 710 | offs += corruption; |
06112547 | 711 | buf += corruption; |
1e51764a AB |
712 | goto corrupted; |
713 | } | |
714 | } | |
715 | ||
bbf2b37a AB |
716 | min_io_unit = round_down(offs, c->min_io_size); |
717 | if (grouped) | |
718 | /* | |
719 | * If nodes are grouped, always drop the incomplete group at | |
720 | * the end. | |
721 | */ | |
da8b94ea | 722 | drop_last_group(sleb, &offs); |
bbf2b37a | 723 | |
da8b94ea AB |
724 | if (jhead == GCHD) { |
725 | /* | |
726 | * If this LEB belongs to the GC head then while we are in the | |
727 | * middle of the same min. I/O unit keep dropping nodes. So | |
728 | * basically, what we want is to make sure that the last min. | |
729 | * I/O unit where we saw the corruption is dropped completely | |
730 | * with all the uncorrupted nodes which may possibly sit there. | |
731 | * | |
732 | * In other words, let's name the min. I/O unit where the | |
733 | * corruption starts B, and the previous min. I/O unit A. The | |
734 | * below code tries to deal with a situation when half of B | |
735 | * contains valid nodes or the end of a valid node, and the | |
736 | * second half of B contains corrupted data or garbage. This | |
737 | * means that UBIFS had been writing to B just before the power | |
738 | * cut happened. I do not know how realistic is this scenario | |
739 | * that half of the min. I/O unit had been written successfully | |
740 | * and the other half not, but this is possible in our 'failure | |
741 | * mode emulation' infrastructure at least. | |
742 | * | |
743 | * So what is the problem, why we need to drop those nodes? Why | |
744 | * can't we just clean-up the second half of B by putting a | |
745 | * padding node there? We can, and this works fine with one | |
746 | * exception which was reproduced with power cut emulation | |
747 | * testing and happens extremely rarely. | |
748 | * | |
749 | * Imagine the file-system is full, we run GC which starts | |
750 | * moving valid nodes from LEB X to LEB Y (obviously, LEB Y is | |
751 | * the current GC head LEB). The @c->gc_lnum is -1, which means | |
752 | * that GC will retain LEB X and will try to continue. Imagine | |
753 | * that LEB X is currently the dirtiest LEB, and the amount of | |
754 | * used space in LEB Y is exactly the same as amount of free | |
755 | * space in LEB X. | |
756 | * | |
757 | * And a power cut happens when nodes are moved from LEB X to | |
758 | * LEB Y. We are here trying to recover LEB Y which is the GC | |
759 | * head LEB. We find the min. I/O unit B as described above. | |
760 | * Then we clean-up LEB Y by padding min. I/O unit. And later | |
761 | * 'ubifs_rcvry_gc_commit()' function fails, because it cannot | |
762 | * find a dirty LEB which could be GC'd into LEB Y! Even LEB X | |
763 | * does not match because the amount of valid nodes there does | |
764 | * not fit the free space in LEB Y any more! And this is | |
765 | * because of the padding node which we added to LEB Y. The | |
766 | * user-visible effect of this which I once observed and | |
767 | * analysed is that we cannot mount the file-system with | |
768 | * -ENOSPC error. | |
769 | * | |
770 | * So obviously, to make sure that situation does not happen we | |
771 | * should free min. I/O unit B in LEB Y completely and the last | |
772 | * used min. I/O unit in LEB Y should be A. This is basically | |
773 | * what the below code tries to do. | |
774 | */ | |
775 | while (offs > min_io_unit) | |
776 | drop_last_node(sleb, &offs); | |
777 | } | |
bbf2b37a AB |
778 | |
779 | buf = sbuf + offs; | |
780 | len = c->leb_size - offs; | |
1e51764a | 781 | |
43e07073 | 782 | clean_buf(c, &buf, lnum, &offs, &len); |
1e51764a AB |
783 | ubifs_end_scan(c, sleb, lnum, offs); |
784 | ||
7c47bfd0 AB |
785 | err = fix_unclean_leb(c, sleb, start); |
786 | if (err) | |
787 | goto error; | |
1e51764a AB |
788 | |
789 | return sleb; | |
790 | ||
61799206 AB |
791 | corrupted_rescan: |
792 | /* Re-scan the corrupted data with verbose messages */ | |
69f90258 | 793 | ubifs_err("corruption %d", ret); |
61799206 | 794 | ubifs_scan_a_node(c, buf, len, lnum, offs, 1); |
1e51764a AB |
795 | corrupted: |
796 | ubifs_scanned_corruption(c, lnum, offs, buf); | |
797 | err = -EUCLEAN; | |
798 | error: | |
799 | ubifs_err("LEB %d scanning failed", lnum); | |
800 | ubifs_scan_destroy(sleb); | |
801 | return ERR_PTR(err); | |
802 | } | |
803 | ||
804 | /** | |
805 | * get_cs_sqnum - get commit start sequence number. | |
806 | * @c: UBIFS file-system description object | |
807 | * @lnum: LEB number of commit start node | |
808 | * @offs: offset of commit start node | |
809 | * @cs_sqnum: commit start sequence number is returned here | |
810 | * | |
811 | * This function returns %0 on success and a negative error code on failure. | |
812 | */ | |
813 | static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs, | |
814 | unsigned long long *cs_sqnum) | |
815 | { | |
816 | struct ubifs_cs_node *cs_node = NULL; | |
817 | int err, ret; | |
818 | ||
819 | dbg_rcvry("at %d:%d", lnum, offs); | |
820 | cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL); | |
821 | if (!cs_node) | |
822 | return -ENOMEM; | |
823 | if (c->leb_size - offs < UBIFS_CS_NODE_SZ) | |
824 | goto out_err; | |
d304820a AB |
825 | err = ubifs_leb_read(c, lnum, (void *)cs_node, offs, |
826 | UBIFS_CS_NODE_SZ, 0); | |
1e51764a AB |
827 | if (err && err != -EBADMSG) |
828 | goto out_free; | |
829 | ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0); | |
830 | if (ret != SCANNED_A_NODE) { | |
a6aae4dd | 831 | ubifs_err("Not a valid node"); |
1e51764a AB |
832 | goto out_err; |
833 | } | |
834 | if (cs_node->ch.node_type != UBIFS_CS_NODE) { | |
a6aae4dd | 835 | ubifs_err("Node a CS node, type is %d", cs_node->ch.node_type); |
1e51764a AB |
836 | goto out_err; |
837 | } | |
838 | if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) { | |
a6aae4dd AB |
839 | ubifs_err("CS node cmt_no %llu != current cmt_no %llu", |
840 | (unsigned long long)le64_to_cpu(cs_node->cmt_no), | |
841 | c->cmt_no); | |
1e51764a AB |
842 | goto out_err; |
843 | } | |
844 | *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum); | |
845 | dbg_rcvry("commit start sqnum %llu", *cs_sqnum); | |
846 | kfree(cs_node); | |
847 | return 0; | |
848 | ||
849 | out_err: | |
850 | err = -EINVAL; | |
851 | out_free: | |
852 | ubifs_err("failed to get CS sqnum"); | |
853 | kfree(cs_node); | |
854 | return err; | |
855 | } | |
856 | ||
857 | /** | |
858 | * ubifs_recover_log_leb - scan and recover a log LEB. | |
859 | * @c: UBIFS file-system description object | |
860 | * @lnum: LEB number | |
861 | * @offs: offset | |
862 | * @sbuf: LEB-sized buffer to use | |
863 | * | |
864 | * This function does a scan of a LEB, but caters for errors that might have | |
7d08ae3c AB |
865 | * been caused by unclean reboots from which we are attempting to recover |
866 | * (assume that only the last log LEB can be corrupted by an unclean reboot). | |
1e51764a AB |
867 | * |
868 | * This function returns %0 on success and a negative error code on failure. | |
869 | */ | |
870 | struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum, | |
871 | int offs, void *sbuf) | |
872 | { | |
873 | struct ubifs_scan_leb *sleb; | |
874 | int next_lnum; | |
875 | ||
876 | dbg_rcvry("LEB %d", lnum); | |
877 | next_lnum = lnum + 1; | |
878 | if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs) | |
879 | next_lnum = UBIFS_LOG_LNUM; | |
880 | if (next_lnum != c->ltail_lnum) { | |
881 | /* | |
882 | * We can only recover at the end of the log, so check that the | |
883 | * next log LEB is empty or out of date. | |
884 | */ | |
348709ba | 885 | sleb = ubifs_scan(c, next_lnum, 0, sbuf, 0); |
1e51764a AB |
886 | if (IS_ERR(sleb)) |
887 | return sleb; | |
888 | if (sleb->nodes_cnt) { | |
889 | struct ubifs_scan_node *snod; | |
890 | unsigned long long cs_sqnum = c->cs_sqnum; | |
891 | ||
892 | snod = list_entry(sleb->nodes.next, | |
893 | struct ubifs_scan_node, list); | |
894 | if (cs_sqnum == 0) { | |
895 | int err; | |
896 | ||
897 | err = get_cs_sqnum(c, lnum, offs, &cs_sqnum); | |
898 | if (err) { | |
899 | ubifs_scan_destroy(sleb); | |
900 | return ERR_PTR(err); | |
901 | } | |
902 | } | |
903 | if (snod->sqnum > cs_sqnum) { | |
79fda517 AB |
904 | ubifs_err("unrecoverable log corruption in LEB %d", |
905 | lnum); | |
1e51764a AB |
906 | ubifs_scan_destroy(sleb); |
907 | return ERR_PTR(-EUCLEAN); | |
908 | } | |
909 | } | |
910 | ubifs_scan_destroy(sleb); | |
911 | } | |
efcfde54 | 912 | return ubifs_recover_leb(c, lnum, offs, sbuf, -1); |
1e51764a AB |
913 | } |
914 | ||
915 | /** | |
916 | * recover_head - recover a head. | |
917 | * @c: UBIFS file-system description object | |
918 | * @lnum: LEB number of head to recover | |
919 | * @offs: offset of head to recover | |
920 | * @sbuf: LEB-sized buffer to use | |
921 | * | |
922 | * This function ensures that there is no data on the flash at a head location. | |
923 | * | |
924 | * This function returns %0 on success and a negative error code on failure. | |
925 | */ | |
83cef708 | 926 | static int recover_head(struct ubifs_info *c, int lnum, int offs, void *sbuf) |
1e51764a | 927 | { |
2765df7d | 928 | int len = c->max_write_size, err; |
1e51764a | 929 | |
1e51764a AB |
930 | if (offs + len > c->leb_size) |
931 | len = c->leb_size - offs; | |
932 | ||
933 | if (!len) | |
934 | return 0; | |
935 | ||
936 | /* Read at the head location and check it is empty flash */ | |
d304820a | 937 | err = ubifs_leb_read(c, lnum, sbuf, offs, len, 1); |
431102fe | 938 | if (err || !is_empty(sbuf, len)) { |
1e51764a AB |
939 | dbg_rcvry("cleaning head at %d:%d", lnum, offs); |
940 | if (offs == 0) | |
941 | return ubifs_leb_unmap(c, lnum); | |
d304820a | 942 | err = ubifs_leb_read(c, lnum, sbuf, 0, offs, 1); |
1e51764a AB |
943 | if (err) |
944 | return err; | |
b36a261e | 945 | return ubifs_leb_change(c, lnum, sbuf, offs); |
1e51764a AB |
946 | } |
947 | ||
948 | return 0; | |
949 | } | |
950 | ||
951 | /** | |
952 | * ubifs_recover_inl_heads - recover index and LPT heads. | |
953 | * @c: UBIFS file-system description object | |
954 | * @sbuf: LEB-sized buffer to use | |
955 | * | |
956 | * This function ensures that there is no data on the flash at the index and | |
957 | * LPT head locations. | |
958 | * | |
959 | * This deals with the recovery of a half-completed journal commit. UBIFS is | |
960 | * careful never to overwrite the last version of the index or the LPT. Because | |
961 | * the index and LPT are wandering trees, data from a half-completed commit will | |
962 | * not be referenced anywhere in UBIFS. The data will be either in LEBs that are | |
963 | * assumed to be empty and will be unmapped anyway before use, or in the index | |
964 | * and LPT heads. | |
965 | * | |
966 | * This function returns %0 on success and a negative error code on failure. | |
967 | */ | |
83cef708 | 968 | int ubifs_recover_inl_heads(struct ubifs_info *c, void *sbuf) |
1e51764a AB |
969 | { |
970 | int err; | |
971 | ||
2ef13294 | 972 | ubifs_assert(!c->ro_mount || c->remounting_rw); |
1e51764a AB |
973 | |
974 | dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs); | |
975 | err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf); | |
976 | if (err) | |
977 | return err; | |
978 | ||
979 | dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs); | |
980 | err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf); | |
981 | if (err) | |
982 | return err; | |
983 | ||
984 | return 0; | |
985 | } | |
986 | ||
987 | /** | |
7606f85a | 988 | * clean_an_unclean_leb - read and write a LEB to remove corruption. |
1e51764a AB |
989 | * @c: UBIFS file-system description object |
990 | * @ucleb: unclean LEB information | |
991 | * @sbuf: LEB-sized buffer to use | |
992 | * | |
993 | * This function reads a LEB up to a point pre-determined by the mount recovery, | |
994 | * checks the nodes, and writes the result back to the flash, thereby cleaning | |
995 | * off any following corruption, or non-fatal ECC errors. | |
996 | * | |
997 | * This function returns %0 on success and a negative error code on failure. | |
998 | */ | |
83cef708 | 999 | static int clean_an_unclean_leb(struct ubifs_info *c, |
1e51764a AB |
1000 | struct ubifs_unclean_leb *ucleb, void *sbuf) |
1001 | { | |
1002 | int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1; | |
1003 | void *buf = sbuf; | |
1004 | ||
1005 | dbg_rcvry("LEB %d len %d", lnum, len); | |
1006 | ||
1007 | if (len == 0) { | |
1008 | /* Nothing to read, just unmap it */ | |
1009 | err = ubifs_leb_unmap(c, lnum); | |
1010 | if (err) | |
1011 | return err; | |
1012 | return 0; | |
1013 | } | |
1014 | ||
d304820a | 1015 | err = ubifs_leb_read(c, lnum, buf, offs, len, 0); |
1e51764a AB |
1016 | if (err && err != -EBADMSG) |
1017 | return err; | |
1018 | ||
1019 | while (len >= 8) { | |
1020 | int ret; | |
1021 | ||
1022 | cond_resched(); | |
1023 | ||
1024 | /* Scan quietly until there is an error */ | |
1025 | ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet); | |
1026 | ||
1027 | if (ret == SCANNED_A_NODE) { | |
1028 | /* A valid node, and not a padding node */ | |
1029 | struct ubifs_ch *ch = buf; | |
1030 | int node_len; | |
1031 | ||
1032 | node_len = ALIGN(le32_to_cpu(ch->len), 8); | |
1033 | offs += node_len; | |
1034 | buf += node_len; | |
1035 | len -= node_len; | |
1036 | continue; | |
1037 | } | |
1038 | ||
1039 | if (ret > 0) { | |
1040 | /* Padding bytes or a valid padding node */ | |
1041 | offs += ret; | |
1042 | buf += ret; | |
1043 | len -= ret; | |
1044 | continue; | |
1045 | } | |
1046 | ||
1047 | if (ret == SCANNED_EMPTY_SPACE) { | |
1048 | ubifs_err("unexpected empty space at %d:%d", | |
1049 | lnum, offs); | |
1050 | return -EUCLEAN; | |
1051 | } | |
1052 | ||
1053 | if (quiet) { | |
1054 | /* Redo the last scan but noisily */ | |
1055 | quiet = 0; | |
1056 | continue; | |
1057 | } | |
1058 | ||
1059 | ubifs_scanned_corruption(c, lnum, offs, buf); | |
1060 | return -EUCLEAN; | |
1061 | } | |
1062 | ||
1063 | /* Pad to min_io_size */ | |
1064 | len = ALIGN(ucleb->endpt, c->min_io_size); | |
1065 | if (len > ucleb->endpt) { | |
1066 | int pad_len = len - ALIGN(ucleb->endpt, 8); | |
1067 | ||
1068 | if (pad_len > 0) { | |
1069 | buf = c->sbuf + len - pad_len; | |
1070 | ubifs_pad(c, buf, pad_len); | |
1071 | } | |
1072 | } | |
1073 | ||
1074 | /* Write back the LEB atomically */ | |
b36a261e | 1075 | err = ubifs_leb_change(c, lnum, sbuf, len); |
1e51764a AB |
1076 | if (err) |
1077 | return err; | |
1078 | ||
1079 | dbg_rcvry("cleaned LEB %d", lnum); | |
1080 | ||
1081 | return 0; | |
1082 | } | |
1083 | ||
1084 | /** | |
1085 | * ubifs_clean_lebs - clean LEBs recovered during read-only mount. | |
1086 | * @c: UBIFS file-system description object | |
1087 | * @sbuf: LEB-sized buffer to use | |
1088 | * | |
1089 | * This function cleans a LEB identified during recovery that needs to be | |
1090 | * written but was not because UBIFS was mounted read-only. This happens when | |
1091 | * remounting to read-write mode. | |
1092 | * | |
1093 | * This function returns %0 on success and a negative error code on failure. | |
1094 | */ | |
83cef708 | 1095 | int ubifs_clean_lebs(struct ubifs_info *c, void *sbuf) |
1e51764a AB |
1096 | { |
1097 | dbg_rcvry("recovery"); | |
1098 | while (!list_empty(&c->unclean_leb_list)) { | |
1099 | struct ubifs_unclean_leb *ucleb; | |
1100 | int err; | |
1101 | ||
1102 | ucleb = list_entry(c->unclean_leb_list.next, | |
1103 | struct ubifs_unclean_leb, list); | |
1104 | err = clean_an_unclean_leb(c, ucleb, sbuf); | |
1105 | if (err) | |
1106 | return err; | |
1107 | list_del(&ucleb->list); | |
1108 | kfree(ucleb); | |
1109 | } | |
1110 | return 0; | |
1111 | } | |
1112 | ||
44744213 AB |
1113 | /** |
1114 | * grab_empty_leb - grab an empty LEB to use as GC LEB and run commit. | |
1115 | * @c: UBIFS file-system description object | |
1116 | * | |
1117 | * This is a helper function for 'ubifs_rcvry_gc_commit()' which grabs an empty | |
1118 | * LEB to be used as GC LEB (@c->gc_lnum), and then runs the commit. Returns | |
1119 | * zero in case of success and a negative error code in case of failure. | |
1120 | */ | |
1121 | static int grab_empty_leb(struct ubifs_info *c) | |
1122 | { | |
1123 | int lnum, err; | |
1124 | ||
1125 | /* | |
1126 | * Note, it is very important to first search for an empty LEB and then | |
1127 | * run the commit, not vice-versa. The reason is that there might be | |
1128 | * only one empty LEB at the moment, the one which has been the | |
1129 | * @c->gc_lnum just before the power cut happened. During the regular | |
1130 | * UBIFS operation (not now) @c->gc_lnum is marked as "taken", so no | |
1131 | * one but GC can grab it. But at this moment this single empty LEB is | |
1132 | * not marked as taken, so if we run commit - what happens? Right, the | |
1133 | * commit will grab it and write the index there. Remember that the | |
1134 | * index always expands as long as there is free space, and it only | |
1135 | * starts consolidating when we run out of space. | |
1136 | * | |
1137 | * IOW, if we run commit now, we might not be able to find a free LEB | |
1138 | * after this. | |
1139 | */ | |
1140 | lnum = ubifs_find_free_leb_for_idx(c); | |
1141 | if (lnum < 0) { | |
a6aae4dd | 1142 | ubifs_err("could not find an empty LEB"); |
edf6be24 AB |
1143 | ubifs_dump_lprops(c); |
1144 | ubifs_dump_budg(c, &c->bi); | |
44744213 AB |
1145 | return lnum; |
1146 | } | |
1147 | ||
1148 | /* Reset the index flag */ | |
1149 | err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0, | |
1150 | LPROPS_INDEX, 0); | |
1151 | if (err) | |
1152 | return err; | |
1153 | ||
1154 | c->gc_lnum = lnum; | |
1155 | dbg_rcvry("found empty LEB %d, run commit", lnum); | |
1156 | ||
1157 | return ubifs_run_commit(c); | |
1158 | } | |
1159 | ||
1e51764a AB |
1160 | /** |
1161 | * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit. | |
1162 | * @c: UBIFS file-system description object | |
1163 | * | |
1164 | * Out-of-place garbage collection requires always one empty LEB with which to | |
1165 | * start garbage collection. The LEB number is recorded in c->gc_lnum and is | |
1166 | * written to the master node on unmounting. In the case of an unclean unmount | |
1167 | * the value of gc_lnum recorded in the master node is out of date and cannot | |
1168 | * be used. Instead, recovery must allocate an empty LEB for this purpose. | |
1169 | * However, there may not be enough empty space, in which case it must be | |
1170 | * possible to GC the dirtiest LEB into the GC head LEB. | |
1171 | * | |
1172 | * This function also runs the commit which causes the TNC updates from | |
1173 | * size-recovery and orphans to be written to the flash. That is important to | |
1174 | * ensure correct replay order for subsequent mounts. | |
1175 | * | |
1176 | * This function returns %0 on success and a negative error code on failure. | |
1177 | */ | |
1178 | int ubifs_rcvry_gc_commit(struct ubifs_info *c) | |
1179 | { | |
1180 | struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf; | |
1181 | struct ubifs_lprops lp; | |
fe79c05f | 1182 | int err; |
1e51764a | 1183 | |
c839e297 AB |
1184 | dbg_rcvry("GC head LEB %d, offs %d", wbuf->lnum, wbuf->offs); |
1185 | ||
1e51764a | 1186 | c->gc_lnum = -1; |
c839e297 | 1187 | if (wbuf->lnum == -1 || wbuf->offs == c->leb_size) |
44744213 | 1188 | return grab_empty_leb(c); |
fe79c05f | 1189 | |
1e51764a AB |
1190 | err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2); |
1191 | if (err) { | |
fe79c05f AB |
1192 | if (err != -ENOSPC) |
1193 | return err; | |
1194 | ||
1195 | dbg_rcvry("could not find a dirty LEB"); | |
1196 | return grab_empty_leb(c); | |
1e51764a | 1197 | } |
2405f594 | 1198 | |
1e51764a | 1199 | ubifs_assert(!(lp.flags & LPROPS_INDEX)); |
bcdca3e1 | 1200 | ubifs_assert(lp.free + lp.dirty >= wbuf->offs); |
2405f594 | 1201 | |
1e51764a AB |
1202 | /* |
1203 | * We run the commit before garbage collection otherwise subsequent | |
1204 | * mounts will see the GC and orphan deletion in a different order. | |
1205 | */ | |
1206 | dbg_rcvry("committing"); | |
1207 | err = ubifs_run_commit(c); | |
1208 | if (err) | |
1209 | return err; | |
fe79c05f AB |
1210 | |
1211 | dbg_rcvry("GC'ing LEB %d", lp.lnum); | |
1e51764a AB |
1212 | mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); |
1213 | err = ubifs_garbage_collect_leb(c, &lp); | |
1214 | if (err >= 0) { | |
1215 | int err2 = ubifs_wbuf_sync_nolock(wbuf); | |
1216 | ||
1217 | if (err2) | |
1218 | err = err2; | |
1219 | } | |
1220 | mutex_unlock(&wbuf->io_mutex); | |
1221 | if (err < 0) { | |
a6aae4dd | 1222 | ubifs_err("GC failed, error %d", err); |
1e51764a AB |
1223 | if (err == -EAGAIN) |
1224 | err = -EINVAL; | |
1225 | return err; | |
1226 | } | |
fe79c05f AB |
1227 | |
1228 | ubifs_assert(err == LEB_RETAINED); | |
1229 | if (err != LEB_RETAINED) | |
1e51764a | 1230 | return -EINVAL; |
fe79c05f | 1231 | |
1e51764a AB |
1232 | err = ubifs_leb_unmap(c, c->gc_lnum); |
1233 | if (err) | |
1234 | return err; | |
fe79c05f AB |
1235 | |
1236 | dbg_rcvry("allocated LEB %d for GC", lp.lnum); | |
1e51764a | 1237 | return 0; |
1e51764a AB |
1238 | } |
1239 | ||
1240 | /** | |
1241 | * struct size_entry - inode size information for recovery. | |
1242 | * @rb: link in the RB-tree of sizes | |
1243 | * @inum: inode number | |
1244 | * @i_size: size on inode | |
1245 | * @d_size: maximum size based on data nodes | |
1246 | * @exists: indicates whether the inode exists | |
1247 | * @inode: inode if pinned in memory awaiting rw mode to fix it | |
1248 | */ | |
1249 | struct size_entry { | |
1250 | struct rb_node rb; | |
1251 | ino_t inum; | |
1252 | loff_t i_size; | |
1253 | loff_t d_size; | |
1254 | int exists; | |
1255 | struct inode *inode; | |
1256 | }; | |
1257 | ||
1258 | /** | |
1259 | * add_ino - add an entry to the size tree. | |
1260 | * @c: UBIFS file-system description object | |
1261 | * @inum: inode number | |
1262 | * @i_size: size on inode | |
1263 | * @d_size: maximum size based on data nodes | |
1264 | * @exists: indicates whether the inode exists | |
1265 | */ | |
1266 | static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size, | |
1267 | loff_t d_size, int exists) | |
1268 | { | |
1269 | struct rb_node **p = &c->size_tree.rb_node, *parent = NULL; | |
1270 | struct size_entry *e; | |
1271 | ||
1272 | while (*p) { | |
1273 | parent = *p; | |
1274 | e = rb_entry(parent, struct size_entry, rb); | |
1275 | if (inum < e->inum) | |
1276 | p = &(*p)->rb_left; | |
1277 | else | |
1278 | p = &(*p)->rb_right; | |
1279 | } | |
1280 | ||
1281 | e = kzalloc(sizeof(struct size_entry), GFP_KERNEL); | |
1282 | if (!e) | |
1283 | return -ENOMEM; | |
1284 | ||
1285 | e->inum = inum; | |
1286 | e->i_size = i_size; | |
1287 | e->d_size = d_size; | |
1288 | e->exists = exists; | |
1289 | ||
1290 | rb_link_node(&e->rb, parent, p); | |
1291 | rb_insert_color(&e->rb, &c->size_tree); | |
1292 | ||
1293 | return 0; | |
1294 | } | |
1295 | ||
1296 | /** | |
1297 | * find_ino - find an entry on the size tree. | |
1298 | * @c: UBIFS file-system description object | |
1299 | * @inum: inode number | |
1300 | */ | |
1301 | static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum) | |
1302 | { | |
1303 | struct rb_node *p = c->size_tree.rb_node; | |
1304 | struct size_entry *e; | |
1305 | ||
1306 | while (p) { | |
1307 | e = rb_entry(p, struct size_entry, rb); | |
1308 | if (inum < e->inum) | |
1309 | p = p->rb_left; | |
1310 | else if (inum > e->inum) | |
1311 | p = p->rb_right; | |
1312 | else | |
1313 | return e; | |
1314 | } | |
1315 | return NULL; | |
1316 | } | |
1317 | ||
1318 | /** | |
1319 | * remove_ino - remove an entry from the size tree. | |
1320 | * @c: UBIFS file-system description object | |
1321 | * @inum: inode number | |
1322 | */ | |
1323 | static void remove_ino(struct ubifs_info *c, ino_t inum) | |
1324 | { | |
1325 | struct size_entry *e = find_ino(c, inum); | |
1326 | ||
1327 | if (!e) | |
1328 | return; | |
1329 | rb_erase(&e->rb, &c->size_tree); | |
1330 | kfree(e); | |
1331 | } | |
1332 | ||
1333 | /** | |
1334 | * ubifs_destroy_size_tree - free resources related to the size tree. | |
1335 | * @c: UBIFS file-system description object | |
1336 | */ | |
1337 | void ubifs_destroy_size_tree(struct ubifs_info *c) | |
1338 | { | |
1339 | struct rb_node *this = c->size_tree.rb_node; | |
1340 | struct size_entry *e; | |
1341 | ||
1342 | while (this) { | |
1343 | if (this->rb_left) { | |
1344 | this = this->rb_left; | |
1345 | continue; | |
1346 | } else if (this->rb_right) { | |
1347 | this = this->rb_right; | |
1348 | continue; | |
1349 | } | |
1350 | e = rb_entry(this, struct size_entry, rb); | |
1351 | if (e->inode) | |
1352 | iput(e->inode); | |
1353 | this = rb_parent(this); | |
1354 | if (this) { | |
1355 | if (this->rb_left == &e->rb) | |
1356 | this->rb_left = NULL; | |
1357 | else | |
1358 | this->rb_right = NULL; | |
1359 | } | |
1360 | kfree(e); | |
1361 | } | |
1362 | c->size_tree = RB_ROOT; | |
1363 | } | |
1364 | ||
1365 | /** | |
1366 | * ubifs_recover_size_accum - accumulate inode sizes for recovery. | |
1367 | * @c: UBIFS file-system description object | |
1368 | * @key: node key | |
1369 | * @deletion: node is for a deletion | |
1370 | * @new_size: inode size | |
1371 | * | |
1372 | * This function has two purposes: | |
1373 | * 1) to ensure there are no data nodes that fall outside the inode size | |
1374 | * 2) to ensure there are no data nodes for inodes that do not exist | |
1375 | * To accomplish those purposes, a rb-tree is constructed containing an entry | |
1376 | * for each inode number in the journal that has not been deleted, and recording | |
1377 | * the size from the inode node, the maximum size of any data node (also altered | |
1378 | * by truncations) and a flag indicating a inode number for which no inode node | |
1379 | * was present in the journal. | |
1380 | * | |
1381 | * Note that there is still the possibility that there are data nodes that have | |
1382 | * been committed that are beyond the inode size, however the only way to find | |
1383 | * them would be to scan the entire index. Alternatively, some provision could | |
1384 | * be made to record the size of inodes at the start of commit, which would seem | |
1385 | * very cumbersome for a scenario that is quite unlikely and the only negative | |
1386 | * consequence of which is wasted space. | |
1387 | * | |
1388 | * This functions returns %0 on success and a negative error code on failure. | |
1389 | */ | |
1390 | int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key, | |
1391 | int deletion, loff_t new_size) | |
1392 | { | |
1393 | ino_t inum = key_inum(c, key); | |
1394 | struct size_entry *e; | |
1395 | int err; | |
1396 | ||
1397 | switch (key_type(c, key)) { | |
1398 | case UBIFS_INO_KEY: | |
1399 | if (deletion) | |
1400 | remove_ino(c, inum); | |
1401 | else { | |
1402 | e = find_ino(c, inum); | |
1403 | if (e) { | |
1404 | e->i_size = new_size; | |
1405 | e->exists = 1; | |
1406 | } else { | |
1407 | err = add_ino(c, inum, new_size, 0, 1); | |
1408 | if (err) | |
1409 | return err; | |
1410 | } | |
1411 | } | |
1412 | break; | |
1413 | case UBIFS_DATA_KEY: | |
1414 | e = find_ino(c, inum); | |
1415 | if (e) { | |
1416 | if (new_size > e->d_size) | |
1417 | e->d_size = new_size; | |
1418 | } else { | |
1419 | err = add_ino(c, inum, 0, new_size, 0); | |
1420 | if (err) | |
1421 | return err; | |
1422 | } | |
1423 | break; | |
1424 | case UBIFS_TRUN_KEY: | |
1425 | e = find_ino(c, inum); | |
1426 | if (e) | |
1427 | e->d_size = new_size; | |
1428 | break; | |
1429 | } | |
1430 | return 0; | |
1431 | } | |
1432 | ||
1433 | /** | |
1434 | * fix_size_in_place - fix inode size in place on flash. | |
1435 | * @c: UBIFS file-system description object | |
1436 | * @e: inode size information for recovery | |
1437 | */ | |
1438 | static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e) | |
1439 | { | |
1440 | struct ubifs_ino_node *ino = c->sbuf; | |
1441 | unsigned char *p; | |
1442 | union ubifs_key key; | |
1443 | int err, lnum, offs, len; | |
1444 | loff_t i_size; | |
1445 | uint32_t crc; | |
1446 | ||
1447 | /* Locate the inode node LEB number and offset */ | |
1448 | ino_key_init(c, &key, e->inum); | |
1449 | err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs); | |
1450 | if (err) | |
1451 | goto out; | |
1452 | /* | |
1453 | * If the size recorded on the inode node is greater than the size that | |
1454 | * was calculated from nodes in the journal then don't change the inode. | |
1455 | */ | |
1456 | i_size = le64_to_cpu(ino->size); | |
1457 | if (i_size >= e->d_size) | |
1458 | return 0; | |
1459 | /* Read the LEB */ | |
d304820a | 1460 | err = ubifs_leb_read(c, lnum, c->sbuf, 0, c->leb_size, 1); |
1e51764a AB |
1461 | if (err) |
1462 | goto out; | |
1463 | /* Change the size field and recalculate the CRC */ | |
1464 | ino = c->sbuf + offs; | |
1465 | ino->size = cpu_to_le64(e->d_size); | |
1466 | len = le32_to_cpu(ino->ch.len); | |
1467 | crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8); | |
1468 | ino->ch.crc = cpu_to_le32(crc); | |
1469 | /* Work out where data in the LEB ends and free space begins */ | |
1470 | p = c->sbuf; | |
1471 | len = c->leb_size - 1; | |
1472 | while (p[len] == 0xff) | |
1473 | len -= 1; | |
1474 | len = ALIGN(len + 1, c->min_io_size); | |
1475 | /* Atomically write the fixed LEB back again */ | |
b36a261e | 1476 | err = ubifs_leb_change(c, lnum, c->sbuf, len); |
1e51764a AB |
1477 | if (err) |
1478 | goto out; | |
69f8a75a | 1479 | dbg_rcvry("inode %lu at %d:%d size %lld -> %lld", |
e84461ad | 1480 | (unsigned long)e->inum, lnum, offs, i_size, e->d_size); |
1e51764a AB |
1481 | return 0; |
1482 | ||
1483 | out: | |
1484 | ubifs_warn("inode %lu failed to fix size %lld -> %lld error %d", | |
e84461ad | 1485 | (unsigned long)e->inum, e->i_size, e->d_size, err); |
1e51764a AB |
1486 | return err; |
1487 | } | |
1488 | ||
1489 | /** | |
1490 | * ubifs_recover_size - recover inode size. | |
1491 | * @c: UBIFS file-system description object | |
1492 | * | |
1493 | * This function attempts to fix inode size discrepancies identified by the | |
1494 | * 'ubifs_recover_size_accum()' function. | |
1495 | * | |
1496 | * This functions returns %0 on success and a negative error code on failure. | |
1497 | */ | |
1498 | int ubifs_recover_size(struct ubifs_info *c) | |
1499 | { | |
1500 | struct rb_node *this = rb_first(&c->size_tree); | |
1501 | ||
1502 | while (this) { | |
1503 | struct size_entry *e; | |
1504 | int err; | |
1505 | ||
1506 | e = rb_entry(this, struct size_entry, rb); | |
1507 | if (!e->exists) { | |
1508 | union ubifs_key key; | |
1509 | ||
1510 | ino_key_init(c, &key, e->inum); | |
1511 | err = ubifs_tnc_lookup(c, &key, c->sbuf); | |
1512 | if (err && err != -ENOENT) | |
1513 | return err; | |
1514 | if (err == -ENOENT) { | |
1515 | /* Remove data nodes that have no inode */ | |
e84461ad AB |
1516 | dbg_rcvry("removing ino %lu", |
1517 | (unsigned long)e->inum); | |
1e51764a AB |
1518 | err = ubifs_tnc_remove_ino(c, e->inum); |
1519 | if (err) | |
1520 | return err; | |
1521 | } else { | |
1522 | struct ubifs_ino_node *ino = c->sbuf; | |
1523 | ||
1524 | e->exists = 1; | |
1525 | e->i_size = le64_to_cpu(ino->size); | |
1526 | } | |
1527 | } | |
69f8a75a | 1528 | |
1e51764a | 1529 | if (e->exists && e->i_size < e->d_size) { |
69f8a75a | 1530 | if (c->ro_mount) { |
1e51764a AB |
1531 | /* Fix the inode size and pin it in memory */ |
1532 | struct inode *inode; | |
c1f1f91d | 1533 | struct ubifs_inode *ui; |
1e51764a | 1534 | |
69f8a75a AB |
1535 | ubifs_assert(!e->inode); |
1536 | ||
1e51764a AB |
1537 | inode = ubifs_iget(c->vfs_sb, e->inum); |
1538 | if (IS_ERR(inode)) | |
1539 | return PTR_ERR(inode); | |
c1f1f91d AB |
1540 | |
1541 | ui = ubifs_inode(inode); | |
1e51764a AB |
1542 | if (inode->i_size < e->d_size) { |
1543 | dbg_rcvry("ino %lu size %lld -> %lld", | |
e84461ad | 1544 | (unsigned long)e->inum, |
4c954520 | 1545 | inode->i_size, e->d_size); |
1e51764a | 1546 | inode->i_size = e->d_size; |
c1f1f91d AB |
1547 | ui->ui_size = e->d_size; |
1548 | ui->synced_i_size = e->d_size; | |
1e51764a AB |
1549 | e->inode = inode; |
1550 | this = rb_next(this); | |
1551 | continue; | |
1552 | } | |
1553 | iput(inode); | |
1554 | } else { | |
1555 | /* Fix the size in place */ | |
1556 | err = fix_size_in_place(c, e); | |
1557 | if (err) | |
1558 | return err; | |
1559 | if (e->inode) | |
1560 | iput(e->inode); | |
1561 | } | |
1562 | } | |
69f8a75a | 1563 | |
1e51764a AB |
1564 | this = rb_next(this); |
1565 | rb_erase(&e->rb, &c->size_tree); | |
1566 | kfree(e); | |
1567 | } | |
69f8a75a | 1568 | |
1e51764a AB |
1569 | return 0; |
1570 | } |