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 functions that manage the running of the commit process. |
| 25 | * Each affected module has its own functions to accomplish their part in the |
| 26 | * commit and those functions are called here. |
| 27 | * |
| 28 | * The commit is the process whereby all updates to the index and LEB properties |
| 29 | * are written out together and the journal becomes empty. This keeps the |
| 30 | * file system consistent - at all times the state can be recreated by reading |
| 31 | * the index and LEB properties and then replaying the journal. |
| 32 | * |
| 33 | * The commit is split into two parts named "commit start" and "commit end". |
| 34 | * During commit start, the commit process has exclusive access to the journal |
| 35 | * by holding the commit semaphore down for writing. As few I/O operations as |
| 36 | * possible are performed during commit start, instead the nodes that are to be |
| 37 | * written are merely identified. During commit end, the commit semaphore is no |
| 38 | * longer held and the journal is again in operation, allowing users to continue |
| 39 | * to use the file system while the bulk of the commit I/O is performed. The |
| 40 | * purpose of this two-step approach is to prevent the commit from causing any |
| 41 | * latency blips. Note that in any case, the commit does not prevent lookups |
| 42 | * (as permitted by the TNC mutex), or access to VFS data structures e.g. page |
| 43 | * cache. |
| 44 | */ |
| 45 | |
| 46 | #include <linux/freezer.h> |
| 47 | #include <linux/kthread.h> |
| 48 | #include "ubifs.h" |
| 49 | |
| 50 | /** |
| 51 | * do_commit - commit the journal. |
| 52 | * @c: UBIFS file-system description object |
| 53 | * |
| 54 | * This function implements UBIFS commit. It has to be called with commit lock |
| 55 | * locked. Returns zero in case of success and a negative error code in case of |
| 56 | * failure. |
| 57 | */ |
| 58 | static int do_commit(struct ubifs_info *c) |
| 59 | { |
| 60 | int err, new_ltail_lnum, old_ltail_lnum, i; |
| 61 | struct ubifs_zbranch zroot; |
| 62 | struct ubifs_lp_stats lst; |
| 63 | |
| 64 | dbg_cmt("start"); |
| 65 | if (c->ro_media) { |
| 66 | err = -EROFS; |
| 67 | goto out_up; |
| 68 | } |
| 69 | |
| 70 | /* Sync all write buffers (necessary for recovery) */ |
| 71 | for (i = 0; i < c->jhead_cnt; i++) { |
| 72 | err = ubifs_wbuf_sync(&c->jheads[i].wbuf); |
| 73 | if (err) |
| 74 | goto out_up; |
| 75 | } |
| 76 | |
Artem Bityutskiy | 014eb04 | 2008-07-21 17:14:29 +0300 | [diff] [blame] | 77 | c->cmt_no += 1; |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 78 | err = ubifs_gc_start_commit(c); |
| 79 | if (err) |
| 80 | goto out_up; |
| 81 | err = dbg_check_lprops(c); |
| 82 | if (err) |
| 83 | goto out_up; |
| 84 | err = ubifs_log_start_commit(c, &new_ltail_lnum); |
| 85 | if (err) |
| 86 | goto out_up; |
| 87 | err = ubifs_tnc_start_commit(c, &zroot); |
| 88 | if (err) |
| 89 | goto out_up; |
| 90 | err = ubifs_lpt_start_commit(c); |
| 91 | if (err) |
| 92 | goto out_up; |
| 93 | err = ubifs_orphan_start_commit(c); |
| 94 | if (err) |
| 95 | goto out_up; |
| 96 | |
| 97 | ubifs_get_lp_stats(c, &lst); |
| 98 | |
| 99 | up_write(&c->commit_sem); |
| 100 | |
| 101 | err = ubifs_tnc_end_commit(c); |
| 102 | if (err) |
| 103 | goto out; |
| 104 | err = ubifs_lpt_end_commit(c); |
| 105 | if (err) |
| 106 | goto out; |
| 107 | err = ubifs_orphan_end_commit(c); |
| 108 | if (err) |
| 109 | goto out; |
| 110 | old_ltail_lnum = c->ltail_lnum; |
| 111 | err = ubifs_log_end_commit(c, new_ltail_lnum); |
| 112 | if (err) |
| 113 | goto out; |
| 114 | err = dbg_check_old_index(c, &zroot); |
| 115 | if (err) |
| 116 | goto out; |
| 117 | |
| 118 | mutex_lock(&c->mst_mutex); |
Artem Bityutskiy | 014eb04 | 2008-07-21 17:14:29 +0300 | [diff] [blame] | 119 | c->mst_node->cmt_no = cpu_to_le64(c->cmt_no); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 120 | c->mst_node->log_lnum = cpu_to_le32(new_ltail_lnum); |
| 121 | c->mst_node->root_lnum = cpu_to_le32(zroot.lnum); |
| 122 | c->mst_node->root_offs = cpu_to_le32(zroot.offs); |
| 123 | c->mst_node->root_len = cpu_to_le32(zroot.len); |
| 124 | c->mst_node->ihead_lnum = cpu_to_le32(c->ihead_lnum); |
| 125 | c->mst_node->ihead_offs = cpu_to_le32(c->ihead_offs); |
| 126 | c->mst_node->index_size = cpu_to_le64(c->old_idx_sz); |
| 127 | c->mst_node->lpt_lnum = cpu_to_le32(c->lpt_lnum); |
| 128 | c->mst_node->lpt_offs = cpu_to_le32(c->lpt_offs); |
| 129 | c->mst_node->nhead_lnum = cpu_to_le32(c->nhead_lnum); |
| 130 | c->mst_node->nhead_offs = cpu_to_le32(c->nhead_offs); |
| 131 | c->mst_node->ltab_lnum = cpu_to_le32(c->ltab_lnum); |
| 132 | c->mst_node->ltab_offs = cpu_to_le32(c->ltab_offs); |
| 133 | c->mst_node->lsave_lnum = cpu_to_le32(c->lsave_lnum); |
| 134 | c->mst_node->lsave_offs = cpu_to_le32(c->lsave_offs); |
| 135 | c->mst_node->lscan_lnum = cpu_to_le32(c->lscan_lnum); |
| 136 | c->mst_node->empty_lebs = cpu_to_le32(lst.empty_lebs); |
| 137 | c->mst_node->idx_lebs = cpu_to_le32(lst.idx_lebs); |
| 138 | c->mst_node->total_free = cpu_to_le64(lst.total_free); |
| 139 | c->mst_node->total_dirty = cpu_to_le64(lst.total_dirty); |
| 140 | c->mst_node->total_used = cpu_to_le64(lst.total_used); |
| 141 | c->mst_node->total_dead = cpu_to_le64(lst.total_dead); |
| 142 | c->mst_node->total_dark = cpu_to_le64(lst.total_dark); |
| 143 | if (c->no_orphs) |
| 144 | c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS); |
| 145 | else |
| 146 | c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_NO_ORPHS); |
| 147 | err = ubifs_write_master(c); |
| 148 | mutex_unlock(&c->mst_mutex); |
| 149 | if (err) |
| 150 | goto out; |
| 151 | |
| 152 | err = ubifs_log_post_commit(c, old_ltail_lnum); |
| 153 | if (err) |
| 154 | goto out; |
| 155 | err = ubifs_gc_end_commit(c); |
| 156 | if (err) |
| 157 | goto out; |
| 158 | err = ubifs_lpt_post_commit(c); |
| 159 | if (err) |
| 160 | goto out; |
| 161 | |
| 162 | spin_lock(&c->cs_lock); |
| 163 | c->cmt_state = COMMIT_RESTING; |
| 164 | wake_up(&c->cmt_wq); |
| 165 | dbg_cmt("commit end"); |
| 166 | spin_unlock(&c->cs_lock); |
| 167 | |
| 168 | return 0; |
| 169 | |
| 170 | out_up: |
| 171 | up_write(&c->commit_sem); |
| 172 | out: |
| 173 | ubifs_err("commit failed, error %d", err); |
| 174 | spin_lock(&c->cs_lock); |
| 175 | c->cmt_state = COMMIT_BROKEN; |
| 176 | wake_up(&c->cmt_wq); |
| 177 | spin_unlock(&c->cs_lock); |
| 178 | ubifs_ro_mode(c, err); |
| 179 | return err; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * run_bg_commit - run background commit if it is needed. |
| 184 | * @c: UBIFS file-system description object |
| 185 | * |
| 186 | * This function runs background commit if it is needed. Returns zero in case |
| 187 | * of success and a negative error code in case of failure. |
| 188 | */ |
| 189 | static int run_bg_commit(struct ubifs_info *c) |
| 190 | { |
| 191 | spin_lock(&c->cs_lock); |
| 192 | /* |
| 193 | * Run background commit only if background commit was requested or if |
| 194 | * commit is required. |
| 195 | */ |
| 196 | if (c->cmt_state != COMMIT_BACKGROUND && |
| 197 | c->cmt_state != COMMIT_REQUIRED) |
| 198 | goto out; |
| 199 | spin_unlock(&c->cs_lock); |
| 200 | |
| 201 | down_write(&c->commit_sem); |
| 202 | spin_lock(&c->cs_lock); |
| 203 | if (c->cmt_state == COMMIT_REQUIRED) |
| 204 | c->cmt_state = COMMIT_RUNNING_REQUIRED; |
| 205 | else if (c->cmt_state == COMMIT_BACKGROUND) |
| 206 | c->cmt_state = COMMIT_RUNNING_BACKGROUND; |
| 207 | else |
| 208 | goto out_cmt_unlock; |
| 209 | spin_unlock(&c->cs_lock); |
| 210 | |
| 211 | return do_commit(c); |
| 212 | |
| 213 | out_cmt_unlock: |
| 214 | up_write(&c->commit_sem); |
| 215 | out: |
| 216 | spin_unlock(&c->cs_lock); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * ubifs_bg_thread - UBIFS background thread function. |
| 222 | * @info: points to the file-system description object |
| 223 | * |
| 224 | * This function implements various file-system background activities: |
| 225 | * o when a write-buffer timer expires it synchronizes the appropriate |
| 226 | * write-buffer; |
| 227 | * o when the journal is about to be full, it starts in-advance commit. |
| 228 | * |
| 229 | * Note, other stuff like background garbage collection may be added here in |
| 230 | * future. |
| 231 | */ |
| 232 | int ubifs_bg_thread(void *info) |
| 233 | { |
| 234 | int err; |
| 235 | struct ubifs_info *c = info; |
| 236 | |
Artem Bityutskiy | 069782a | 2008-10-21 12:56:31 +0300 | [diff] [blame] | 237 | dbg_msg("background thread \"%s\" started, PID %d", |
| 238 | c->bgt_name, current->pid); |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 239 | set_freezable(); |
| 240 | |
| 241 | while (1) { |
| 242 | if (kthread_should_stop()) |
| 243 | break; |
| 244 | |
| 245 | if (try_to_freeze()) |
| 246 | continue; |
| 247 | |
| 248 | set_current_state(TASK_INTERRUPTIBLE); |
| 249 | /* Check if there is something to do */ |
| 250 | if (!c->need_bgt) { |
| 251 | /* |
| 252 | * Nothing prevents us from going sleep now and |
| 253 | * be never woken up and block the task which |
| 254 | * could wait in 'kthread_stop()' forever. |
| 255 | */ |
| 256 | if (kthread_should_stop()) |
| 257 | break; |
| 258 | schedule(); |
| 259 | continue; |
| 260 | } else |
| 261 | __set_current_state(TASK_RUNNING); |
| 262 | |
| 263 | c->need_bgt = 0; |
| 264 | err = ubifs_bg_wbufs_sync(c); |
| 265 | if (err) |
| 266 | ubifs_ro_mode(c, err); |
| 267 | |
| 268 | run_bg_commit(c); |
| 269 | cond_resched(); |
| 270 | } |
| 271 | |
| 272 | dbg_msg("background thread \"%s\" stops", c->bgt_name); |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * ubifs_commit_required - set commit state to "required". |
| 278 | * @c: UBIFS file-system description object |
| 279 | * |
| 280 | * This function is called if a commit is required but cannot be done from the |
| 281 | * calling function, so it is just flagged instead. |
| 282 | */ |
| 283 | void ubifs_commit_required(struct ubifs_info *c) |
| 284 | { |
| 285 | spin_lock(&c->cs_lock); |
| 286 | switch (c->cmt_state) { |
| 287 | case COMMIT_RESTING: |
| 288 | case COMMIT_BACKGROUND: |
| 289 | dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state), |
| 290 | dbg_cstate(COMMIT_REQUIRED)); |
| 291 | c->cmt_state = COMMIT_REQUIRED; |
| 292 | break; |
| 293 | case COMMIT_RUNNING_BACKGROUND: |
| 294 | dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state), |
| 295 | dbg_cstate(COMMIT_RUNNING_REQUIRED)); |
| 296 | c->cmt_state = COMMIT_RUNNING_REQUIRED; |
| 297 | break; |
| 298 | case COMMIT_REQUIRED: |
| 299 | case COMMIT_RUNNING_REQUIRED: |
| 300 | case COMMIT_BROKEN: |
| 301 | break; |
| 302 | } |
| 303 | spin_unlock(&c->cs_lock); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * ubifs_request_bg_commit - notify the background thread to do a commit. |
| 308 | * @c: UBIFS file-system description object |
| 309 | * |
| 310 | * This function is called if the journal is full enough to make a commit |
| 311 | * worthwhile, so background thread is kicked to start it. |
| 312 | */ |
| 313 | void ubifs_request_bg_commit(struct ubifs_info *c) |
| 314 | { |
| 315 | spin_lock(&c->cs_lock); |
| 316 | if (c->cmt_state == COMMIT_RESTING) { |
| 317 | dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state), |
| 318 | dbg_cstate(COMMIT_BACKGROUND)); |
| 319 | c->cmt_state = COMMIT_BACKGROUND; |
| 320 | spin_unlock(&c->cs_lock); |
| 321 | ubifs_wake_up_bgt(c); |
| 322 | } else |
| 323 | spin_unlock(&c->cs_lock); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * wait_for_commit - wait for commit. |
| 328 | * @c: UBIFS file-system description object |
| 329 | * |
| 330 | * This function sleeps until the commit operation is no longer running. |
| 331 | */ |
| 332 | static int wait_for_commit(struct ubifs_info *c) |
| 333 | { |
| 334 | dbg_cmt("pid %d goes sleep", current->pid); |
| 335 | |
| 336 | /* |
| 337 | * The following sleeps if the condition is false, and will be woken |
| 338 | * when the commit ends. It is possible, although very unlikely, that we |
| 339 | * will wake up and see the subsequent commit running, rather than the |
| 340 | * one we were waiting for, and go back to sleep. However, we will be |
| 341 | * woken again, so there is no danger of sleeping forever. |
| 342 | */ |
| 343 | wait_event(c->cmt_wq, c->cmt_state != COMMIT_RUNNING_BACKGROUND && |
| 344 | c->cmt_state != COMMIT_RUNNING_REQUIRED); |
| 345 | dbg_cmt("commit finished, pid %d woke up", current->pid); |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * ubifs_run_commit - run or wait for commit. |
| 351 | * @c: UBIFS file-system description object |
| 352 | * |
| 353 | * This function runs commit and returns zero in case of success and a negative |
| 354 | * error code in case of failure. |
| 355 | */ |
| 356 | int ubifs_run_commit(struct ubifs_info *c) |
| 357 | { |
| 358 | int err = 0; |
| 359 | |
| 360 | spin_lock(&c->cs_lock); |
| 361 | if (c->cmt_state == COMMIT_BROKEN) { |
| 362 | err = -EINVAL; |
| 363 | goto out; |
| 364 | } |
| 365 | |
| 366 | if (c->cmt_state == COMMIT_RUNNING_BACKGROUND) |
| 367 | /* |
| 368 | * We set the commit state to 'running required' to indicate |
| 369 | * that we want it to complete as quickly as possible. |
| 370 | */ |
| 371 | c->cmt_state = COMMIT_RUNNING_REQUIRED; |
| 372 | |
| 373 | if (c->cmt_state == COMMIT_RUNNING_REQUIRED) { |
| 374 | spin_unlock(&c->cs_lock); |
| 375 | return wait_for_commit(c); |
| 376 | } |
| 377 | spin_unlock(&c->cs_lock); |
| 378 | |
| 379 | /* Ok, the commit is indeed needed */ |
| 380 | |
| 381 | down_write(&c->commit_sem); |
| 382 | spin_lock(&c->cs_lock); |
| 383 | /* |
| 384 | * Since we unlocked 'c->cs_lock', the state may have changed, so |
| 385 | * re-check it. |
| 386 | */ |
| 387 | if (c->cmt_state == COMMIT_BROKEN) { |
| 388 | err = -EINVAL; |
| 389 | goto out_cmt_unlock; |
| 390 | } |
| 391 | |
| 392 | if (c->cmt_state == COMMIT_RUNNING_BACKGROUND) |
| 393 | c->cmt_state = COMMIT_RUNNING_REQUIRED; |
| 394 | |
| 395 | if (c->cmt_state == COMMIT_RUNNING_REQUIRED) { |
| 396 | up_write(&c->commit_sem); |
| 397 | spin_unlock(&c->cs_lock); |
| 398 | return wait_for_commit(c); |
| 399 | } |
| 400 | c->cmt_state = COMMIT_RUNNING_REQUIRED; |
| 401 | spin_unlock(&c->cs_lock); |
| 402 | |
| 403 | err = do_commit(c); |
| 404 | return err; |
| 405 | |
| 406 | out_cmt_unlock: |
| 407 | up_write(&c->commit_sem); |
| 408 | out: |
| 409 | spin_unlock(&c->cs_lock); |
| 410 | return err; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * ubifs_gc_should_commit - determine if it is time for GC to run commit. |
| 415 | * @c: UBIFS file-system description object |
| 416 | * |
| 417 | * This function is called by garbage collection to determine if commit should |
| 418 | * be run. If commit state is @COMMIT_BACKGROUND, which means that the journal |
| 419 | * is full enough to start commit, this function returns true. It is not |
| 420 | * absolutely necessary to commit yet, but it feels like this should be better |
| 421 | * then to keep doing GC. This function returns %1 if GC has to initiate commit |
| 422 | * and %0 if not. |
| 423 | */ |
| 424 | int ubifs_gc_should_commit(struct ubifs_info *c) |
| 425 | { |
| 426 | int ret = 0; |
| 427 | |
| 428 | spin_lock(&c->cs_lock); |
| 429 | if (c->cmt_state == COMMIT_BACKGROUND) { |
| 430 | dbg_cmt("commit required now"); |
| 431 | c->cmt_state = COMMIT_REQUIRED; |
| 432 | } else |
| 433 | dbg_cmt("commit not requested"); |
| 434 | if (c->cmt_state == COMMIT_REQUIRED) |
| 435 | ret = 1; |
| 436 | spin_unlock(&c->cs_lock); |
| 437 | return ret; |
| 438 | } |
| 439 | |
| 440 | #ifdef CONFIG_UBIFS_FS_DEBUG |
| 441 | |
| 442 | /** |
| 443 | * struct idx_node - hold index nodes during index tree traversal. |
| 444 | * @list: list |
| 445 | * @iip: index in parent (slot number of this indexing node in the parent |
| 446 | * indexing node) |
| 447 | * @upper_key: all keys in this indexing node have to be less or equivalent to |
| 448 | * this key |
| 449 | * @idx: index node (8-byte aligned because all node structures must be 8-byte |
| 450 | * aligned) |
| 451 | */ |
| 452 | struct idx_node { |
| 453 | struct list_head list; |
| 454 | int iip; |
| 455 | union ubifs_key upper_key; |
| 456 | struct ubifs_idx_node idx __attribute__((aligned(8))); |
| 457 | }; |
| 458 | |
| 459 | /** |
| 460 | * dbg_old_index_check_init - get information for the next old index check. |
| 461 | * @c: UBIFS file-system description object |
| 462 | * @zroot: root of the index |
| 463 | * |
| 464 | * This function records information about the index that will be needed for the |
| 465 | * next old index check i.e. 'dbg_check_old_index()'. |
| 466 | * |
| 467 | * This function returns %0 on success and a negative error code on failure. |
| 468 | */ |
| 469 | int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot) |
| 470 | { |
| 471 | struct ubifs_idx_node *idx; |
| 472 | int lnum, offs, len, err = 0; |
| 473 | |
| 474 | c->old_zroot = *zroot; |
| 475 | |
| 476 | lnum = c->old_zroot.lnum; |
| 477 | offs = c->old_zroot.offs; |
| 478 | len = c->old_zroot.len; |
| 479 | |
| 480 | idx = kmalloc(c->max_idx_node_sz, GFP_NOFS); |
| 481 | if (!idx) |
| 482 | return -ENOMEM; |
| 483 | |
| 484 | err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs); |
| 485 | if (err) |
| 486 | goto out; |
| 487 | |
| 488 | c->old_zroot_level = le16_to_cpu(idx->level); |
| 489 | c->old_zroot_sqnum = le64_to_cpu(idx->ch.sqnum); |
| 490 | out: |
| 491 | kfree(idx); |
| 492 | return err; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * dbg_check_old_index - check the old copy of the index. |
| 497 | * @c: UBIFS file-system description object |
| 498 | * @zroot: root of the new index |
| 499 | * |
| 500 | * In order to be able to recover from an unclean unmount, a complete copy of |
| 501 | * the index must exist on flash. This is the "old" index. The commit process |
| 502 | * must write the "new" index to flash without overwriting or destroying any |
| 503 | * part of the old index. This function is run at commit end in order to check |
| 504 | * that the old index does indeed exist completely intact. |
| 505 | * |
| 506 | * This function returns %0 on success and a negative error code on failure. |
| 507 | */ |
| 508 | int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot) |
| 509 | { |
| 510 | int lnum, offs, len, err = 0, uninitialized_var(last_level), child_cnt; |
| 511 | int first = 1, iip; |
| 512 | union ubifs_key lower_key, upper_key, l_key, u_key; |
| 513 | unsigned long long uninitialized_var(last_sqnum); |
| 514 | struct ubifs_idx_node *idx; |
| 515 | struct list_head list; |
| 516 | struct idx_node *i; |
| 517 | size_t sz; |
| 518 | |
| 519 | if (!(ubifs_chk_flags & UBIFS_CHK_OLD_IDX)) |
| 520 | goto out; |
| 521 | |
| 522 | INIT_LIST_HEAD(&list); |
| 523 | |
| 524 | sz = sizeof(struct idx_node) + ubifs_idx_node_sz(c, c->fanout) - |
| 525 | UBIFS_IDX_NODE_SZ; |
| 526 | |
| 527 | /* Start at the old zroot */ |
| 528 | lnum = c->old_zroot.lnum; |
| 529 | offs = c->old_zroot.offs; |
| 530 | len = c->old_zroot.len; |
| 531 | iip = 0; |
| 532 | |
| 533 | /* |
| 534 | * Traverse the index tree preorder depth-first i.e. do a node and then |
| 535 | * its subtrees from left to right. |
| 536 | */ |
| 537 | while (1) { |
| 538 | struct ubifs_branch *br; |
| 539 | |
| 540 | /* Get the next index node */ |
| 541 | i = kmalloc(sz, GFP_NOFS); |
| 542 | if (!i) { |
| 543 | err = -ENOMEM; |
| 544 | goto out_free; |
| 545 | } |
| 546 | i->iip = iip; |
| 547 | /* Keep the index nodes on our path in a linked list */ |
| 548 | list_add_tail(&i->list, &list); |
| 549 | /* Read the index node */ |
| 550 | idx = &i->idx; |
| 551 | err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs); |
| 552 | if (err) |
| 553 | goto out_free; |
| 554 | /* Validate index node */ |
| 555 | child_cnt = le16_to_cpu(idx->child_cnt); |
| 556 | if (child_cnt < 1 || child_cnt > c->fanout) { |
| 557 | err = 1; |
| 558 | goto out_dump; |
| 559 | } |
| 560 | if (first) { |
| 561 | first = 0; |
| 562 | /* Check root level and sqnum */ |
| 563 | if (le16_to_cpu(idx->level) != c->old_zroot_level) { |
| 564 | err = 2; |
| 565 | goto out_dump; |
| 566 | } |
| 567 | if (le64_to_cpu(idx->ch.sqnum) != c->old_zroot_sqnum) { |
| 568 | err = 3; |
| 569 | goto out_dump; |
| 570 | } |
| 571 | /* Set last values as though root had a parent */ |
| 572 | last_level = le16_to_cpu(idx->level) + 1; |
| 573 | last_sqnum = le64_to_cpu(idx->ch.sqnum) + 1; |
| 574 | key_read(c, ubifs_idx_key(c, idx), &lower_key); |
| 575 | highest_ino_key(c, &upper_key, INUM_WATERMARK); |
| 576 | } |
| 577 | key_copy(c, &upper_key, &i->upper_key); |
| 578 | if (le16_to_cpu(idx->level) != last_level - 1) { |
| 579 | err = 3; |
| 580 | goto out_dump; |
| 581 | } |
| 582 | /* |
| 583 | * The index is always written bottom up hence a child's sqnum |
| 584 | * is always less than the parents. |
| 585 | */ |
| 586 | if (le64_to_cpu(idx->ch.sqnum) >= last_sqnum) { |
| 587 | err = 4; |
| 588 | goto out_dump; |
| 589 | } |
| 590 | /* Check key range */ |
| 591 | key_read(c, ubifs_idx_key(c, idx), &l_key); |
| 592 | br = ubifs_idx_branch(c, idx, child_cnt - 1); |
| 593 | key_read(c, &br->key, &u_key); |
| 594 | if (keys_cmp(c, &lower_key, &l_key) > 0) { |
| 595 | err = 5; |
| 596 | goto out_dump; |
| 597 | } |
| 598 | if (keys_cmp(c, &upper_key, &u_key) < 0) { |
| 599 | err = 6; |
| 600 | goto out_dump; |
| 601 | } |
| 602 | if (keys_cmp(c, &upper_key, &u_key) == 0) |
| 603 | if (!is_hash_key(c, &u_key)) { |
| 604 | err = 7; |
| 605 | goto out_dump; |
| 606 | } |
| 607 | /* Go to next index node */ |
| 608 | if (le16_to_cpu(idx->level) == 0) { |
| 609 | /* At the bottom, so go up until can go right */ |
| 610 | while (1) { |
| 611 | /* Drop the bottom of the list */ |
| 612 | list_del(&i->list); |
| 613 | kfree(i); |
| 614 | /* No more list means we are done */ |
| 615 | if (list_empty(&list)) |
| 616 | goto out; |
| 617 | /* Look at the new bottom */ |
| 618 | i = list_entry(list.prev, struct idx_node, |
| 619 | list); |
| 620 | idx = &i->idx; |
| 621 | /* Can we go right */ |
| 622 | if (iip + 1 < le16_to_cpu(idx->child_cnt)) { |
| 623 | iip = iip + 1; |
| 624 | break; |
| 625 | } else |
| 626 | /* Nope, so go up again */ |
| 627 | iip = i->iip; |
| 628 | } |
| 629 | } else |
| 630 | /* Go down left */ |
| 631 | iip = 0; |
| 632 | /* |
| 633 | * We have the parent in 'idx' and now we set up for reading the |
| 634 | * child pointed to by slot 'iip'. |
| 635 | */ |
| 636 | last_level = le16_to_cpu(idx->level); |
| 637 | last_sqnum = le64_to_cpu(idx->ch.sqnum); |
| 638 | br = ubifs_idx_branch(c, idx, iip); |
| 639 | lnum = le32_to_cpu(br->lnum); |
| 640 | offs = le32_to_cpu(br->offs); |
| 641 | len = le32_to_cpu(br->len); |
| 642 | key_read(c, &br->key, &lower_key); |
| 643 | if (iip + 1 < le16_to_cpu(idx->child_cnt)) { |
| 644 | br = ubifs_idx_branch(c, idx, iip + 1); |
| 645 | key_read(c, &br->key, &upper_key); |
| 646 | } else |
| 647 | key_copy(c, &i->upper_key, &upper_key); |
| 648 | } |
| 649 | out: |
| 650 | err = dbg_old_index_check_init(c, zroot); |
| 651 | if (err) |
| 652 | goto out_free; |
| 653 | |
| 654 | return 0; |
| 655 | |
| 656 | out_dump: |
| 657 | dbg_err("dumping index node (iip=%d)", i->iip); |
| 658 | dbg_dump_node(c, idx); |
| 659 | list_del(&i->list); |
| 660 | kfree(i); |
| 661 | if (!list_empty(&list)) { |
| 662 | i = list_entry(list.prev, struct idx_node, list); |
| 663 | dbg_err("dumping parent index node"); |
| 664 | dbg_dump_node(c, &i->idx); |
| 665 | } |
| 666 | out_free: |
| 667 | while (!list_empty(&list)) { |
| 668 | i = list_entry(list.next, struct idx_node, list); |
| 669 | list_del(&i->list); |
| 670 | kfree(i); |
| 671 | } |
| 672 | ubifs_err("failed, error %d", err); |
| 673 | if (err > 0) |
| 674 | err = -EINVAL; |
| 675 | return err; |
| 676 | } |
| 677 | |
| 678 | #endif /* CONFIG_UBIFS_FS_DEBUG */ |