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