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