Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 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 the scan which is a general-purpose function for |
| 25 | * determining what nodes are in an eraseblock. The scan is used to replay the |
| 26 | * journal, to do garbage collection. for the TNC in-the-gaps method, and by |
| 27 | * debugging functions. |
| 28 | */ |
| 29 | |
| 30 | #include "ubifs.h" |
| 31 | |
| 32 | /** |
| 33 | * scan_padding_bytes - scan for padding bytes. |
| 34 | * @buf: buffer to scan |
| 35 | * @len: length of buffer |
| 36 | * |
| 37 | * This function returns the number of padding bytes on success and |
| 38 | * %SCANNED_GARBAGE on failure. |
| 39 | */ |
| 40 | static int scan_padding_bytes(void *buf, int len) |
| 41 | { |
| 42 | int pad_len = 0, max_pad_len = min_t(int, UBIFS_PAD_NODE_SZ, len); |
| 43 | uint8_t *p = buf; |
| 44 | |
| 45 | dbg_scan("not a node"); |
| 46 | |
| 47 | while (pad_len < max_pad_len && *p++ == UBIFS_PADDING_BYTE) |
| 48 | pad_len += 1; |
| 49 | |
| 50 | if (!pad_len || (pad_len & 7)) |
| 51 | return SCANNED_GARBAGE; |
| 52 | |
| 53 | dbg_scan("%d padding bytes", pad_len); |
| 54 | |
| 55 | return pad_len; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * ubifs_scan_a_node - scan for a node or padding. |
| 60 | * @c: UBIFS file-system description object |
| 61 | * @buf: buffer to scan |
| 62 | * @len: length of buffer |
| 63 | * @lnum: logical eraseblock number |
| 64 | * @offs: offset within the logical eraseblock |
| 65 | * @quiet: print no messages |
| 66 | * |
| 67 | * This function returns a scanning code to indicate what was scanned. |
| 68 | */ |
| 69 | int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum, |
| 70 | int offs, int quiet) |
| 71 | { |
| 72 | struct ubifs_ch *ch = buf; |
| 73 | uint32_t magic; |
| 74 | |
| 75 | magic = le32_to_cpu(ch->magic); |
| 76 | |
| 77 | if (magic == 0xFFFFFFFF) { |
Artem Bityutskiy | 4b3e58a | 2012-08-23 17:28:58 +0300 | [diff] [blame] | 78 | dbg_scan("hit empty space at LEB %d:%d", lnum, offs); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 79 | return SCANNED_EMPTY_SPACE; |
| 80 | } |
| 81 | |
| 82 | if (magic != UBIFS_NODE_MAGIC) |
| 83 | return scan_padding_bytes(buf, len); |
| 84 | |
| 85 | if (len < UBIFS_CH_SZ) |
| 86 | return SCANNED_GARBAGE; |
| 87 | |
Artem Bityutskiy | 79fda51 | 2012-08-27 13:34:09 +0300 | [diff] [blame] | 88 | dbg_scan("scanning %s at LEB %d:%d", |
| 89 | dbg_ntype(ch->node_type), lnum, offs); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 90 | |
Adrian Hunter | 2953e73 | 2008-09-04 16:26:00 +0300 | [diff] [blame] | 91 | if (ubifs_check_node(c, buf, lnum, offs, quiet, 1)) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 92 | return SCANNED_A_CORRUPT_NODE; |
| 93 | |
| 94 | if (ch->node_type == UBIFS_PAD_NODE) { |
| 95 | struct ubifs_pad_node *pad = buf; |
| 96 | int pad_len = le32_to_cpu(pad->pad_len); |
| 97 | int node_len = le32_to_cpu(ch->len); |
| 98 | |
| 99 | /* Validate the padding node */ |
| 100 | if (pad_len < 0 || |
| 101 | offs + node_len + pad_len > c->leb_size) { |
| 102 | if (!quiet) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 103 | ubifs_err(c, "bad pad node at LEB %d:%d", |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 104 | lnum, offs); |
Artem Bityutskiy | edf6be2 | 2012-05-16 19:15:56 +0300 | [diff] [blame] | 105 | ubifs_dump_node(c, pad); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 106 | } |
| 107 | return SCANNED_A_BAD_PAD_NODE; |
| 108 | } |
| 109 | |
| 110 | /* Make the node pads to 8-byte boundary */ |
| 111 | if ((node_len + pad_len) & 7) { |
Artem Bityutskiy | 348709b | 2009-08-25 15:00:55 +0300 | [diff] [blame] | 112 | if (!quiet) |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 113 | ubifs_err(c, "bad padding length %d - %d", |
Artem Bityutskiy | a6aae4d | 2012-05-16 20:11:23 +0300 | [diff] [blame] | 114 | offs, offs + node_len + pad_len); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 115 | return SCANNED_A_BAD_PAD_NODE; |
| 116 | } |
| 117 | |
Artem Bityutskiy | 4b3e58a | 2012-08-23 17:28:58 +0300 | [diff] [blame] | 118 | dbg_scan("%d bytes padded at LEB %d:%d, offset now %d", pad_len, |
| 119 | lnum, offs, ALIGN(offs + node_len + pad_len, 8)); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 120 | |
| 121 | return node_len + pad_len; |
| 122 | } |
| 123 | |
| 124 | return SCANNED_A_NODE; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * ubifs_start_scan - create LEB scanning information at start of scan. |
| 129 | * @c: UBIFS file-system description object |
| 130 | * @lnum: logical eraseblock number |
| 131 | * @offs: offset to start at (usually zero) |
| 132 | * @sbuf: scan buffer (must be c->leb_size) |
| 133 | * |
Artem Bityutskiy | f2b6521 | 2014-07-01 18:32:15 +0300 | [diff] [blame] | 134 | * This function returns the scanned information on success and a negative error |
Seunghun Lee | d685c41 | 2014-07-01 23:45:25 +0900 | [diff] [blame] | 135 | * code on failure. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 136 | */ |
| 137 | struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum, |
| 138 | int offs, void *sbuf) |
| 139 | { |
| 140 | struct ubifs_scan_leb *sleb; |
| 141 | int err; |
| 142 | |
| 143 | dbg_scan("scan LEB %d:%d", lnum, offs); |
| 144 | |
| 145 | sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS); |
| 146 | if (!sleb) |
| 147 | return ERR_PTR(-ENOMEM); |
| 148 | |
| 149 | sleb->lnum = lnum; |
| 150 | INIT_LIST_HEAD(&sleb->nodes); |
| 151 | sleb->buf = sbuf; |
| 152 | |
Artem Bityutskiy | d304820 | 2011-06-03 14:03:25 +0300 | [diff] [blame] | 153 | err = ubifs_leb_read(c, lnum, sbuf + offs, offs, c->leb_size - offs, 0); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 154 | if (err && err != -EBADMSG) { |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 155 | ubifs_err(c, "cannot read %d bytes from LEB %d:%d, error %d", |
Artem Bityutskiy | 79fda51 | 2012-08-27 13:34:09 +0300 | [diff] [blame] | 156 | c->leb_size - offs, lnum, offs, err); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 157 | kfree(sleb); |
| 158 | return ERR_PTR(err); |
| 159 | } |
| 160 | |
hujianyang | c7b5bb0 | 2014-06-24 11:46:36 +0800 | [diff] [blame] | 161 | /* |
| 162 | * Note, we ignore integrity errors (EBASMSG) because all the nodes are |
| 163 | * protected by CRC checksums. |
| 164 | */ |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 165 | return sleb; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * ubifs_end_scan - update LEB scanning information at end of scan. |
| 170 | * @c: UBIFS file-system description object |
| 171 | * @sleb: scanning information |
| 172 | * @lnum: logical eraseblock number |
| 173 | * @offs: offset to start at (usually zero) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 174 | */ |
| 175 | void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb, |
| 176 | int lnum, int offs) |
| 177 | { |
| 178 | lnum = lnum; |
| 179 | dbg_scan("stop scanning LEB %d at offset %d", lnum, offs); |
| 180 | ubifs_assert(offs % c->min_io_size == 0); |
| 181 | |
| 182 | sleb->endpt = ALIGN(offs, c->min_io_size); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * ubifs_add_snod - add a scanned node to LEB scanning information. |
| 187 | * @c: UBIFS file-system description object |
| 188 | * @sleb: scanning information |
| 189 | * @buf: buffer containing node |
| 190 | * @offs: offset of node on flash |
| 191 | * |
| 192 | * This function returns %0 on success and a negative error code on failure. |
| 193 | */ |
| 194 | int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb, |
| 195 | void *buf, int offs) |
| 196 | { |
| 197 | struct ubifs_ch *ch = buf; |
| 198 | struct ubifs_ino_node *ino = buf; |
| 199 | struct ubifs_scan_node *snod; |
| 200 | |
Artem Bityutskiy | ba2f48f | 2010-08-22 07:10:12 +0300 | [diff] [blame] | 201 | snod = kmalloc(sizeof(struct ubifs_scan_node), GFP_NOFS); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 202 | if (!snod) |
| 203 | return -ENOMEM; |
| 204 | |
| 205 | snod->sqnum = le64_to_cpu(ch->sqnum); |
| 206 | snod->type = ch->node_type; |
| 207 | snod->offs = offs; |
| 208 | snod->len = le32_to_cpu(ch->len); |
| 209 | snod->node = buf; |
| 210 | |
| 211 | switch (ch->node_type) { |
| 212 | case UBIFS_INO_NODE: |
| 213 | case UBIFS_DENT_NODE: |
| 214 | case UBIFS_XENT_NODE: |
| 215 | case UBIFS_DATA_NODE: |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 216 | /* |
| 217 | * The key is in the same place in all keyed |
| 218 | * nodes. |
| 219 | */ |
| 220 | key_read(c, &ino->key, &snod->key); |
| 221 | break; |
Artem Bityutskiy | ba2f48f | 2010-08-22 07:10:12 +0300 | [diff] [blame] | 222 | default: |
| 223 | invalid_key_init(c, &snod->key); |
| 224 | break; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 225 | } |
| 226 | list_add_tail(&snod->list, &sleb->nodes); |
| 227 | sleb->nodes_cnt += 1; |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * ubifs_scanned_corruption - print information after UBIFS scanned corruption. |
| 233 | * @c: UBIFS file-system description object |
| 234 | * @lnum: LEB number of corruption |
| 235 | * @offs: offset of corruption |
| 236 | * @buf: buffer containing corruption |
| 237 | */ |
| 238 | void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs, |
| 239 | void *buf) |
| 240 | { |
| 241 | int len; |
| 242 | |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 243 | ubifs_err(c, "corruption at LEB %d:%d", lnum, offs); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 244 | len = c->leb_size - offs; |
Artem Bityutskiy | 086b364 | 2009-06-29 16:25:33 +0300 | [diff] [blame] | 245 | if (len > 8192) |
| 246 | len = 8192; |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 247 | ubifs_err(c, "first %d bytes from LEB %d:%d", len, lnum, offs); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 248 | print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * ubifs_scan - scan a logical eraseblock. |
| 253 | * @c: UBIFS file-system description object |
| 254 | * @lnum: logical eraseblock number |
| 255 | * @offs: offset to start at (usually zero) |
Artem Bityutskiy | 348709b | 2009-08-25 15:00:55 +0300 | [diff] [blame] | 256 | * @sbuf: scan buffer (must be of @c->leb_size bytes in size) |
| 257 | * @quiet: print no messages |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 258 | * |
| 259 | * This function scans LEB number @lnum and returns complete information about |
Artem Bityutskiy | f2b6521 | 2014-07-01 18:32:15 +0300 | [diff] [blame] | 260 | * its contents. Returns the scanned information in case of success and, |
Artem Bityutskiy | ed43f2f | 2009-06-29 17:59:23 +0300 | [diff] [blame] | 261 | * %-EUCLEAN if the LEB neads recovery, and other negative error codes in case |
| 262 | * of failure. |
Artem Bityutskiy | 348709b | 2009-08-25 15:00:55 +0300 | [diff] [blame] | 263 | * |
| 264 | * If @quiet is non-zero, this function does not print large and scary |
| 265 | * error messages and flash dumps in case of errors. |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 266 | */ |
| 267 | struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum, |
Artem Bityutskiy | 348709b | 2009-08-25 15:00:55 +0300 | [diff] [blame] | 268 | int offs, void *sbuf, int quiet) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 269 | { |
| 270 | void *buf = sbuf + offs; |
| 271 | int err, len = c->leb_size - offs; |
| 272 | struct ubifs_scan_leb *sleb; |
| 273 | |
| 274 | sleb = ubifs_start_scan(c, lnum, offs, sbuf); |
| 275 | if (IS_ERR(sleb)) |
| 276 | return sleb; |
| 277 | |
| 278 | while (len >= 8) { |
| 279 | struct ubifs_ch *ch = buf; |
| 280 | int node_len, ret; |
| 281 | |
| 282 | dbg_scan("look at LEB %d:%d (%d bytes left)", |
| 283 | lnum, offs, len); |
| 284 | |
| 285 | cond_resched(); |
| 286 | |
Artem Bityutskiy | 348709b | 2009-08-25 15:00:55 +0300 | [diff] [blame] | 287 | ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 288 | if (ret > 0) { |
| 289 | /* Padding bytes or a valid padding node */ |
| 290 | offs += ret; |
| 291 | buf += ret; |
| 292 | len -= ret; |
| 293 | continue; |
| 294 | } |
| 295 | |
| 296 | if (ret == SCANNED_EMPTY_SPACE) |
| 297 | /* Empty space is checked later */ |
| 298 | break; |
| 299 | |
| 300 | switch (ret) { |
| 301 | case SCANNED_GARBAGE: |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 302 | ubifs_err(c, "garbage"); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 303 | goto corrupted; |
| 304 | case SCANNED_A_NODE: |
| 305 | break; |
| 306 | case SCANNED_A_CORRUPT_NODE: |
| 307 | case SCANNED_A_BAD_PAD_NODE: |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 308 | ubifs_err(c, "bad node"); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 309 | goto corrupted; |
| 310 | default: |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 311 | ubifs_err(c, "unknown"); |
Artem Bityutskiy | ed43f2f | 2009-06-29 17:59:23 +0300 | [diff] [blame] | 312 | err = -EINVAL; |
| 313 | goto error; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | err = ubifs_add_snod(c, sleb, buf, offs); |
| 317 | if (err) |
| 318 | goto error; |
| 319 | |
| 320 | node_len = ALIGN(le32_to_cpu(ch->len), 8); |
| 321 | offs += node_len; |
| 322 | buf += node_len; |
| 323 | len -= node_len; |
| 324 | } |
| 325 | |
Artem Bityutskiy | ed43f2f | 2009-06-29 17:59:23 +0300 | [diff] [blame] | 326 | if (offs % c->min_io_size) { |
Artem Bityutskiy | 348709b | 2009-08-25 15:00:55 +0300 | [diff] [blame] | 327 | if (!quiet) |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 328 | ubifs_err(c, "empty space starts at non-aligned offset %d", |
Artem Bityutskiy | 348709b | 2009-08-25 15:00:55 +0300 | [diff] [blame] | 329 | offs); |
Artem Bityutskiy | 822ed64 | 2011-02-06 14:49:26 +0200 | [diff] [blame] | 330 | goto corrupted; |
Artem Bityutskiy | ed43f2f | 2009-06-29 17:59:23 +0300 | [diff] [blame] | 331 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 332 | |
| 333 | ubifs_end_scan(c, sleb, lnum, offs); |
| 334 | |
| 335 | for (; len > 4; offs += 4, buf = buf + 4, len -= 4) |
| 336 | if (*(uint32_t *)buf != 0xffffffff) |
| 337 | break; |
| 338 | for (; len; offs++, buf++, len--) |
| 339 | if (*(uint8_t *)buf != 0xff) { |
Artem Bityutskiy | 348709b | 2009-08-25 15:00:55 +0300 | [diff] [blame] | 340 | if (!quiet) |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 341 | ubifs_err(c, "corrupt empty space at LEB %d:%d", |
Artem Bityutskiy | 348709b | 2009-08-25 15:00:55 +0300 | [diff] [blame] | 342 | lnum, offs); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 343 | goto corrupted; |
| 344 | } |
| 345 | |
| 346 | return sleb; |
| 347 | |
| 348 | corrupted: |
Artem Bityutskiy | 348709b | 2009-08-25 15:00:55 +0300 | [diff] [blame] | 349 | if (!quiet) { |
| 350 | ubifs_scanned_corruption(c, lnum, offs, buf); |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 351 | ubifs_err(c, "LEB %d scanning failed", lnum); |
Artem Bityutskiy | 348709b | 2009-08-25 15:00:55 +0300 | [diff] [blame] | 352 | } |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 353 | err = -EUCLEAN; |
Artem Bityutskiy | 348709b | 2009-08-25 15:00:55 +0300 | [diff] [blame] | 354 | ubifs_scan_destroy(sleb); |
| 355 | return ERR_PTR(err); |
| 356 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 357 | error: |
Sheng Yong | 235c362 | 2015-03-20 10:39:42 +0000 | [diff] [blame] | 358 | ubifs_err(c, "LEB %d scanning failed, error %d", lnum, err); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 359 | ubifs_scan_destroy(sleb); |
| 360 | return ERR_PTR(err); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * ubifs_scan_destroy - destroy LEB scanning information. |
| 365 | * @sleb: scanning information to free |
| 366 | */ |
| 367 | void ubifs_scan_destroy(struct ubifs_scan_leb *sleb) |
| 368 | { |
| 369 | struct ubifs_scan_node *node; |
| 370 | struct list_head *head; |
| 371 | |
| 372 | head = &sleb->nodes; |
| 373 | while (!list_empty(head)) { |
| 374 | node = list_entry(head->next, struct ubifs_scan_node, list); |
| 375 | list_del(&node->list); |
| 376 | kfree(node); |
| 377 | } |
| 378 | kfree(sleb); |
| 379 | } |