12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21e51764aSArtem Bityutskiy /*
31e51764aSArtem Bityutskiy * This file is part of UBIFS.
41e51764aSArtem Bityutskiy *
51e51764aSArtem Bityutskiy * Copyright (C) 2006-2008 Nokia Corporation.
61e51764aSArtem Bityutskiy *
71e51764aSArtem Bityutskiy * Authors: Adrian Hunter
81e51764aSArtem Bityutskiy * Artem Bityutskiy (Битюцкий Артём)
91e51764aSArtem Bityutskiy */
101e51764aSArtem Bityutskiy
111e51764aSArtem Bityutskiy /*
121e51764aSArtem Bityutskiy * This file implements functions that manage the running of the commit process.
131e51764aSArtem Bityutskiy * Each affected module has its own functions to accomplish their part in the
141e51764aSArtem Bityutskiy * commit and those functions are called here.
151e51764aSArtem Bityutskiy *
161e51764aSArtem Bityutskiy * The commit is the process whereby all updates to the index and LEB properties
171e51764aSArtem Bityutskiy * are written out together and the journal becomes empty. This keeps the
181e51764aSArtem Bityutskiy * file system consistent - at all times the state can be recreated by reading
191e51764aSArtem Bityutskiy * the index and LEB properties and then replaying the journal.
201e51764aSArtem Bityutskiy *
211e51764aSArtem Bityutskiy * The commit is split into two parts named "commit start" and "commit end".
221e51764aSArtem Bityutskiy * During commit start, the commit process has exclusive access to the journal
231e51764aSArtem Bityutskiy * by holding the commit semaphore down for writing. As few I/O operations as
241e51764aSArtem Bityutskiy * possible are performed during commit start, instead the nodes that are to be
251e51764aSArtem Bityutskiy * written are merely identified. During commit end, the commit semaphore is no
261e51764aSArtem Bityutskiy * longer held and the journal is again in operation, allowing users to continue
271e51764aSArtem Bityutskiy * to use the file system while the bulk of the commit I/O is performed. The
281e51764aSArtem Bityutskiy * purpose of this two-step approach is to prevent the commit from causing any
291e51764aSArtem Bityutskiy * latency blips. Note that in any case, the commit does not prevent lookups
301e51764aSArtem Bityutskiy * (as permitted by the TNC mutex), or access to VFS data structures e.g. page
311e51764aSArtem Bityutskiy * cache.
321e51764aSArtem Bityutskiy */
331e51764aSArtem Bityutskiy
341e51764aSArtem Bityutskiy #include <linux/freezer.h>
351e51764aSArtem Bityutskiy #include <linux/kthread.h>
365a0e3ad6STejun Heo #include <linux/slab.h>
371e51764aSArtem Bityutskiy #include "ubifs.h"
381e51764aSArtem Bityutskiy
39944fdef5SArtem Bityutskiy /*
40944fdef5SArtem Bityutskiy * nothing_to_commit - check if there is nothing to commit.
41944fdef5SArtem Bityutskiy * @c: UBIFS file-system description object
42944fdef5SArtem Bityutskiy *
43944fdef5SArtem Bityutskiy * This is a helper function which checks if there is anything to commit. It is
44944fdef5SArtem Bityutskiy * used as an optimization to avoid starting the commit if it is not really
45944fdef5SArtem Bityutskiy * necessary. Indeed, the commit operation always assumes flash I/O (e.g.,
46944fdef5SArtem Bityutskiy * writing the commit start node to the log), and it is better to avoid doing
47944fdef5SArtem Bityutskiy * this unnecessarily. E.g., 'ubifs_sync_fs()' runs the commit, but if there is
48944fdef5SArtem Bityutskiy * nothing to commit, it is more optimal to avoid any flash I/O.
49944fdef5SArtem Bityutskiy *
50944fdef5SArtem Bityutskiy * This function has to be called with @c->commit_sem locked for writing -
51944fdef5SArtem Bityutskiy * this function does not take LPT/TNC locks because the @c->commit_sem
52944fdef5SArtem Bityutskiy * guarantees that we have exclusive access to the TNC and LPT data structures.
53944fdef5SArtem Bityutskiy *
54944fdef5SArtem Bityutskiy * This function returns %1 if there is nothing to commit and %0 otherwise.
55944fdef5SArtem Bityutskiy */
nothing_to_commit(struct ubifs_info * c)56944fdef5SArtem Bityutskiy static int nothing_to_commit(struct ubifs_info *c)
57944fdef5SArtem Bityutskiy {
58944fdef5SArtem Bityutskiy /*
59944fdef5SArtem Bityutskiy * During mounting or remounting from R/O mode to R/W mode we may
60944fdef5SArtem Bityutskiy * commit for various recovery-related reasons.
61944fdef5SArtem Bityutskiy */
62944fdef5SArtem Bityutskiy if (c->mounting || c->remounting_rw)
63944fdef5SArtem Bityutskiy return 0;
64944fdef5SArtem Bityutskiy
65944fdef5SArtem Bityutskiy /*
66944fdef5SArtem Bityutskiy * If the root TNC node is dirty, we definitely have something to
67944fdef5SArtem Bityutskiy * commit.
68944fdef5SArtem Bityutskiy */
69f42eed7cSArtem Bityutskiy if (c->zroot.znode && ubifs_zn_dirty(c->zroot.znode))
70944fdef5SArtem Bityutskiy return 0;
71944fdef5SArtem Bityutskiy
72944fdef5SArtem Bityutskiy /*
73944fdef5SArtem Bityutskiy * Even though the TNC is clean, the LPT tree may have dirty nodes. For
74944fdef5SArtem Bityutskiy * example, this may happen if the budgeting subsystem invoked GC to
75944fdef5SArtem Bityutskiy * make some free space, and the GC found an LEB with only dirty and
76944fdef5SArtem Bityutskiy * free space. In this case GC would just change the lprops of this
77944fdef5SArtem Bityutskiy * LEB (by turning all space into free space) and unmap it.
78944fdef5SArtem Bityutskiy */
79944fdef5SArtem Bityutskiy if (c->nroot && test_bit(DIRTY_CNODE, &c->nroot->flags))
80944fdef5SArtem Bityutskiy return 0;
81944fdef5SArtem Bityutskiy
826eb61d58SRichard Weinberger ubifs_assert(c, atomic_long_read(&c->dirty_zn_cnt) == 0);
836eb61d58SRichard Weinberger ubifs_assert(c, c->dirty_pn_cnt == 0);
846eb61d58SRichard Weinberger ubifs_assert(c, c->dirty_nn_cnt == 0);
85944fdef5SArtem Bityutskiy
86944fdef5SArtem Bityutskiy return 1;
87944fdef5SArtem Bityutskiy }
88944fdef5SArtem Bityutskiy
891e51764aSArtem Bityutskiy /**
901e51764aSArtem Bityutskiy * do_commit - commit the journal.
911e51764aSArtem Bityutskiy * @c: UBIFS file-system description object
921e51764aSArtem Bityutskiy *
931e51764aSArtem Bityutskiy * This function implements UBIFS commit. It has to be called with commit lock
941e51764aSArtem Bityutskiy * locked. Returns zero in case of success and a negative error code in case of
951e51764aSArtem Bityutskiy * failure.
961e51764aSArtem Bityutskiy */
do_commit(struct ubifs_info * c)971e51764aSArtem Bityutskiy static int do_commit(struct ubifs_info *c)
981e51764aSArtem Bityutskiy {
991e51764aSArtem Bityutskiy int err, new_ltail_lnum, old_ltail_lnum, i;
1001e51764aSArtem Bityutskiy struct ubifs_zbranch zroot;
1011e51764aSArtem Bityutskiy struct ubifs_lp_stats lst;
1021e51764aSArtem Bityutskiy
1031e51764aSArtem Bityutskiy dbg_cmt("start");
1046eb61d58SRichard Weinberger ubifs_assert(c, !c->ro_media && !c->ro_mount);
1052680d722SArtem Bityutskiy
1062680d722SArtem Bityutskiy if (c->ro_error) {
1071e51764aSArtem Bityutskiy err = -EROFS;
1081e51764aSArtem Bityutskiy goto out_up;
1091e51764aSArtem Bityutskiy }
1101e51764aSArtem Bityutskiy
111944fdef5SArtem Bityutskiy if (nothing_to_commit(c)) {
112944fdef5SArtem Bityutskiy up_write(&c->commit_sem);
113944fdef5SArtem Bityutskiy err = 0;
114944fdef5SArtem Bityutskiy goto out_cancel;
115944fdef5SArtem Bityutskiy }
116944fdef5SArtem Bityutskiy
1171e51764aSArtem Bityutskiy /* Sync all write buffers (necessary for recovery) */
1181e51764aSArtem Bityutskiy for (i = 0; i < c->jhead_cnt; i++) {
1191e51764aSArtem Bityutskiy err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
1201e51764aSArtem Bityutskiy if (err)
1211e51764aSArtem Bityutskiy goto out_up;
1221e51764aSArtem Bityutskiy }
1231e51764aSArtem Bityutskiy
124014eb04bSArtem Bityutskiy c->cmt_no += 1;
1251e51764aSArtem Bityutskiy err = ubifs_gc_start_commit(c);
1261e51764aSArtem Bityutskiy if (err)
1271e51764aSArtem Bityutskiy goto out_up;
1281e51764aSArtem Bityutskiy err = dbg_check_lprops(c);
1291e51764aSArtem Bityutskiy if (err)
1301e51764aSArtem Bityutskiy goto out_up;
1311e51764aSArtem Bityutskiy err = ubifs_log_start_commit(c, &new_ltail_lnum);
1321e51764aSArtem Bityutskiy if (err)
1331e51764aSArtem Bityutskiy goto out_up;
1341e51764aSArtem Bityutskiy err = ubifs_tnc_start_commit(c, &zroot);
1351e51764aSArtem Bityutskiy if (err)
1361e51764aSArtem Bityutskiy goto out_up;
1371e51764aSArtem Bityutskiy err = ubifs_lpt_start_commit(c);
1381e51764aSArtem Bityutskiy if (err)
1391e51764aSArtem Bityutskiy goto out_up;
1401e51764aSArtem Bityutskiy err = ubifs_orphan_start_commit(c);
1411e51764aSArtem Bityutskiy if (err)
1421e51764aSArtem Bityutskiy goto out_up;
1431e51764aSArtem Bityutskiy
1441e51764aSArtem Bityutskiy ubifs_get_lp_stats(c, &lst);
1451e51764aSArtem Bityutskiy
1461e51764aSArtem Bityutskiy up_write(&c->commit_sem);
1471e51764aSArtem Bityutskiy
1481e51764aSArtem Bityutskiy err = ubifs_tnc_end_commit(c);
1491e51764aSArtem Bityutskiy if (err)
1501e51764aSArtem Bityutskiy goto out;
1511e51764aSArtem Bityutskiy err = ubifs_lpt_end_commit(c);
1521e51764aSArtem Bityutskiy if (err)
1531e51764aSArtem Bityutskiy goto out;
1541e51764aSArtem Bityutskiy err = ubifs_orphan_end_commit(c);
1551e51764aSArtem Bityutskiy if (err)
1561e51764aSArtem Bityutskiy goto out;
1571e51764aSArtem Bityutskiy err = dbg_check_old_index(c, &zroot);
1581e51764aSArtem Bityutskiy if (err)
1591e51764aSArtem Bityutskiy goto out;
1601e51764aSArtem Bityutskiy
161014eb04bSArtem Bityutskiy c->mst_node->cmt_no = cpu_to_le64(c->cmt_no);
1621e51764aSArtem Bityutskiy c->mst_node->log_lnum = cpu_to_le32(new_ltail_lnum);
1631e51764aSArtem Bityutskiy c->mst_node->root_lnum = cpu_to_le32(zroot.lnum);
1641e51764aSArtem Bityutskiy c->mst_node->root_offs = cpu_to_le32(zroot.offs);
1651e51764aSArtem Bityutskiy c->mst_node->root_len = cpu_to_le32(zroot.len);
1661e51764aSArtem Bityutskiy c->mst_node->ihead_lnum = cpu_to_le32(c->ihead_lnum);
1671e51764aSArtem Bityutskiy c->mst_node->ihead_offs = cpu_to_le32(c->ihead_offs);
168b137545cSArtem Bityutskiy c->mst_node->index_size = cpu_to_le64(c->bi.old_idx_sz);
1691e51764aSArtem Bityutskiy c->mst_node->lpt_lnum = cpu_to_le32(c->lpt_lnum);
1701e51764aSArtem Bityutskiy c->mst_node->lpt_offs = cpu_to_le32(c->lpt_offs);
1711e51764aSArtem Bityutskiy c->mst_node->nhead_lnum = cpu_to_le32(c->nhead_lnum);
1721e51764aSArtem Bityutskiy c->mst_node->nhead_offs = cpu_to_le32(c->nhead_offs);
1731e51764aSArtem Bityutskiy c->mst_node->ltab_lnum = cpu_to_le32(c->ltab_lnum);
1741e51764aSArtem Bityutskiy c->mst_node->ltab_offs = cpu_to_le32(c->ltab_offs);
1751e51764aSArtem Bityutskiy c->mst_node->lsave_lnum = cpu_to_le32(c->lsave_lnum);
1761e51764aSArtem Bityutskiy c->mst_node->lsave_offs = cpu_to_le32(c->lsave_offs);
1771e51764aSArtem Bityutskiy c->mst_node->lscan_lnum = cpu_to_le32(c->lscan_lnum);
1781e51764aSArtem Bityutskiy c->mst_node->empty_lebs = cpu_to_le32(lst.empty_lebs);
1791e51764aSArtem Bityutskiy c->mst_node->idx_lebs = cpu_to_le32(lst.idx_lebs);
1801e51764aSArtem Bityutskiy c->mst_node->total_free = cpu_to_le64(lst.total_free);
1811e51764aSArtem Bityutskiy c->mst_node->total_dirty = cpu_to_le64(lst.total_dirty);
1821e51764aSArtem Bityutskiy c->mst_node->total_used = cpu_to_le64(lst.total_used);
1831e51764aSArtem Bityutskiy c->mst_node->total_dead = cpu_to_le64(lst.total_dead);
1841e51764aSArtem Bityutskiy c->mst_node->total_dark = cpu_to_le64(lst.total_dark);
1851e51764aSArtem Bityutskiy if (c->no_orphs)
1861e51764aSArtem Bityutskiy c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
1871e51764aSArtem Bityutskiy else
1881e51764aSArtem Bityutskiy c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_NO_ORPHS);
189052c2807SArtem Bityutskiy
190052c2807SArtem Bityutskiy old_ltail_lnum = c->ltail_lnum;
191052c2807SArtem Bityutskiy err = ubifs_log_end_commit(c, new_ltail_lnum);
1921e51764aSArtem Bityutskiy if (err)
1931e51764aSArtem Bityutskiy goto out;
1941e51764aSArtem Bityutskiy
1951e51764aSArtem Bityutskiy err = ubifs_log_post_commit(c, old_ltail_lnum);
1961e51764aSArtem Bityutskiy if (err)
1971e51764aSArtem Bityutskiy goto out;
1981e51764aSArtem Bityutskiy err = ubifs_gc_end_commit(c);
1991e51764aSArtem Bityutskiy if (err)
2001e51764aSArtem Bityutskiy goto out;
2011e51764aSArtem Bityutskiy err = ubifs_lpt_post_commit(c);
2021e51764aSArtem Bityutskiy if (err)
2031e51764aSArtem Bityutskiy goto out;
2041e51764aSArtem Bityutskiy
205944fdef5SArtem Bityutskiy out_cancel:
2061e51764aSArtem Bityutskiy spin_lock(&c->cs_lock);
2071e51764aSArtem Bityutskiy c->cmt_state = COMMIT_RESTING;
2081e51764aSArtem Bityutskiy wake_up(&c->cmt_wq);
2091e51764aSArtem Bityutskiy dbg_cmt("commit end");
2101e51764aSArtem Bityutskiy spin_unlock(&c->cs_lock);
2111e51764aSArtem Bityutskiy return 0;
2121e51764aSArtem Bityutskiy
2131e51764aSArtem Bityutskiy out_up:
2141e51764aSArtem Bityutskiy up_write(&c->commit_sem);
2151e51764aSArtem Bityutskiy out:
216235c362bSSheng Yong ubifs_err(c, "commit failed, error %d", err);
2171e51764aSArtem Bityutskiy spin_lock(&c->cs_lock);
2181e51764aSArtem Bityutskiy c->cmt_state = COMMIT_BROKEN;
2191e51764aSArtem Bityutskiy wake_up(&c->cmt_wq);
2201e51764aSArtem Bityutskiy spin_unlock(&c->cs_lock);
2211e51764aSArtem Bityutskiy ubifs_ro_mode(c, err);
2221e51764aSArtem Bityutskiy return err;
2231e51764aSArtem Bityutskiy }
2241e51764aSArtem Bityutskiy
2251e51764aSArtem Bityutskiy /**
2261e51764aSArtem Bityutskiy * run_bg_commit - run background commit if it is needed.
2271e51764aSArtem Bityutskiy * @c: UBIFS file-system description object
2281e51764aSArtem Bityutskiy *
2291e51764aSArtem Bityutskiy * This function runs background commit if it is needed. Returns zero in case
2301e51764aSArtem Bityutskiy * of success and a negative error code in case of failure.
2311e51764aSArtem Bityutskiy */
run_bg_commit(struct ubifs_info * c)2321e51764aSArtem Bityutskiy static int run_bg_commit(struct ubifs_info *c)
2331e51764aSArtem Bityutskiy {
2341e51764aSArtem Bityutskiy spin_lock(&c->cs_lock);
2351e51764aSArtem Bityutskiy /*
2361e51764aSArtem Bityutskiy * Run background commit only if background commit was requested or if
2371e51764aSArtem Bityutskiy * commit is required.
2381e51764aSArtem Bityutskiy */
2391e51764aSArtem Bityutskiy if (c->cmt_state != COMMIT_BACKGROUND &&
2401e51764aSArtem Bityutskiy c->cmt_state != COMMIT_REQUIRED)
2411e51764aSArtem Bityutskiy goto out;
2421e51764aSArtem Bityutskiy spin_unlock(&c->cs_lock);
2431e51764aSArtem Bityutskiy
2441e51764aSArtem Bityutskiy down_write(&c->commit_sem);
2451e51764aSArtem Bityutskiy spin_lock(&c->cs_lock);
2461e51764aSArtem Bityutskiy if (c->cmt_state == COMMIT_REQUIRED)
2471e51764aSArtem Bityutskiy c->cmt_state = COMMIT_RUNNING_REQUIRED;
2481e51764aSArtem Bityutskiy else if (c->cmt_state == COMMIT_BACKGROUND)
2491e51764aSArtem Bityutskiy c->cmt_state = COMMIT_RUNNING_BACKGROUND;
2501e51764aSArtem Bityutskiy else
2511e51764aSArtem Bityutskiy goto out_cmt_unlock;
2521e51764aSArtem Bityutskiy spin_unlock(&c->cs_lock);
2531e51764aSArtem Bityutskiy
2541e51764aSArtem Bityutskiy return do_commit(c);
2551e51764aSArtem Bityutskiy
2561e51764aSArtem Bityutskiy out_cmt_unlock:
2571e51764aSArtem Bityutskiy up_write(&c->commit_sem);
2581e51764aSArtem Bityutskiy out:
2591e51764aSArtem Bityutskiy spin_unlock(&c->cs_lock);
2601e51764aSArtem Bityutskiy return 0;
2611e51764aSArtem Bityutskiy }
2621e51764aSArtem Bityutskiy
2631e51764aSArtem Bityutskiy /**
2641e51764aSArtem Bityutskiy * ubifs_bg_thread - UBIFS background thread function.
2651e51764aSArtem Bityutskiy * @info: points to the file-system description object
2661e51764aSArtem Bityutskiy *
2671e51764aSArtem Bityutskiy * This function implements various file-system background activities:
2681e51764aSArtem Bityutskiy * o when a write-buffer timer expires it synchronizes the appropriate
2691e51764aSArtem Bityutskiy * write-buffer;
2701e51764aSArtem Bityutskiy * o when the journal is about to be full, it starts in-advance commit.
2711e51764aSArtem Bityutskiy *
2721e51764aSArtem Bityutskiy * Note, other stuff like background garbage collection may be added here in
2731e51764aSArtem Bityutskiy * future.
2741e51764aSArtem Bityutskiy */
ubifs_bg_thread(void * info)2751e51764aSArtem Bityutskiy int ubifs_bg_thread(void *info)
2761e51764aSArtem Bityutskiy {
2771e51764aSArtem Bityutskiy int err;
2781e51764aSArtem Bityutskiy struct ubifs_info *c = info;
2791e51764aSArtem Bityutskiy
280235c362bSSheng Yong ubifs_msg(c, "background thread \"%s\" started, PID %d",
2811e51764aSArtem Bityutskiy c->bgt_name, current->pid);
2821e51764aSArtem Bityutskiy set_freezable();
2831e51764aSArtem Bityutskiy
2841e51764aSArtem Bityutskiy while (1) {
2851e51764aSArtem Bityutskiy if (kthread_should_stop())
2861e51764aSArtem Bityutskiy break;
2871e51764aSArtem Bityutskiy
2881e51764aSArtem Bityutskiy if (try_to_freeze())
2891e51764aSArtem Bityutskiy continue;
2901e51764aSArtem Bityutskiy
2911e51764aSArtem Bityutskiy set_current_state(TASK_INTERRUPTIBLE);
2921e51764aSArtem Bityutskiy /* Check if there is something to do */
2931e51764aSArtem Bityutskiy if (!c->need_bgt) {
2941e51764aSArtem Bityutskiy /*
2951e51764aSArtem Bityutskiy * Nothing prevents us from going sleep now and
2961e51764aSArtem Bityutskiy * be never woken up and block the task which
2971e51764aSArtem Bityutskiy * could wait in 'kthread_stop()' forever.
2981e51764aSArtem Bityutskiy */
2991e51764aSArtem Bityutskiy if (kthread_should_stop())
3001e51764aSArtem Bityutskiy break;
3011e51764aSArtem Bityutskiy schedule();
3021e51764aSArtem Bityutskiy continue;
3031e51764aSArtem Bityutskiy } else
3041e51764aSArtem Bityutskiy __set_current_state(TASK_RUNNING);
3051e51764aSArtem Bityutskiy
3061e51764aSArtem Bityutskiy c->need_bgt = 0;
3071e51764aSArtem Bityutskiy err = ubifs_bg_wbufs_sync(c);
3081e51764aSArtem Bityutskiy if (err)
3091e51764aSArtem Bityutskiy ubifs_ro_mode(c, err);
3101e51764aSArtem Bityutskiy
3111e51764aSArtem Bityutskiy run_bg_commit(c);
3121e51764aSArtem Bityutskiy cond_resched();
3131e51764aSArtem Bityutskiy }
3141e51764aSArtem Bityutskiy
315235c362bSSheng Yong ubifs_msg(c, "background thread \"%s\" stops", c->bgt_name);
3161e51764aSArtem Bityutskiy return 0;
3171e51764aSArtem Bityutskiy }
3181e51764aSArtem Bityutskiy
3191e51764aSArtem Bityutskiy /**
3201e51764aSArtem Bityutskiy * ubifs_commit_required - set commit state to "required".
3211e51764aSArtem Bityutskiy * @c: UBIFS file-system description object
3221e51764aSArtem Bityutskiy *
3231e51764aSArtem Bityutskiy * This function is called if a commit is required but cannot be done from the
3241e51764aSArtem Bityutskiy * calling function, so it is just flagged instead.
3251e51764aSArtem Bityutskiy */
ubifs_commit_required(struct ubifs_info * c)3261e51764aSArtem Bityutskiy void ubifs_commit_required(struct ubifs_info *c)
3271e51764aSArtem Bityutskiy {
3281e51764aSArtem Bityutskiy spin_lock(&c->cs_lock);
3291e51764aSArtem Bityutskiy switch (c->cmt_state) {
3301e51764aSArtem Bityutskiy case COMMIT_RESTING:
3311e51764aSArtem Bityutskiy case COMMIT_BACKGROUND:
3321e51764aSArtem Bityutskiy dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
3331e51764aSArtem Bityutskiy dbg_cstate(COMMIT_REQUIRED));
3341e51764aSArtem Bityutskiy c->cmt_state = COMMIT_REQUIRED;
3351e51764aSArtem Bityutskiy break;
3361e51764aSArtem Bityutskiy case COMMIT_RUNNING_BACKGROUND:
3371e51764aSArtem Bityutskiy dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
3381e51764aSArtem Bityutskiy dbg_cstate(COMMIT_RUNNING_REQUIRED));
3391e51764aSArtem Bityutskiy c->cmt_state = COMMIT_RUNNING_REQUIRED;
3401e51764aSArtem Bityutskiy break;
3411e51764aSArtem Bityutskiy case COMMIT_REQUIRED:
3421e51764aSArtem Bityutskiy case COMMIT_RUNNING_REQUIRED:
3431e51764aSArtem Bityutskiy case COMMIT_BROKEN:
3441e51764aSArtem Bityutskiy break;
3451e51764aSArtem Bityutskiy }
3461e51764aSArtem Bityutskiy spin_unlock(&c->cs_lock);
3471e51764aSArtem Bityutskiy }
3481e51764aSArtem Bityutskiy
3491e51764aSArtem Bityutskiy /**
3501e51764aSArtem Bityutskiy * ubifs_request_bg_commit - notify the background thread to do a commit.
3511e51764aSArtem Bityutskiy * @c: UBIFS file-system description object
3521e51764aSArtem Bityutskiy *
3531e51764aSArtem Bityutskiy * This function is called if the journal is full enough to make a commit
3541e51764aSArtem Bityutskiy * worthwhile, so background thread is kicked to start it.
3551e51764aSArtem Bityutskiy */
ubifs_request_bg_commit(struct ubifs_info * c)3561e51764aSArtem Bityutskiy void ubifs_request_bg_commit(struct ubifs_info *c)
3571e51764aSArtem Bityutskiy {
3581e51764aSArtem Bityutskiy spin_lock(&c->cs_lock);
3591e51764aSArtem Bityutskiy if (c->cmt_state == COMMIT_RESTING) {
3601e51764aSArtem Bityutskiy dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
3611e51764aSArtem Bityutskiy dbg_cstate(COMMIT_BACKGROUND));
3621e51764aSArtem Bityutskiy c->cmt_state = COMMIT_BACKGROUND;
3631e51764aSArtem Bityutskiy spin_unlock(&c->cs_lock);
3641e51764aSArtem Bityutskiy ubifs_wake_up_bgt(c);
3651e51764aSArtem Bityutskiy } else
3661e51764aSArtem Bityutskiy spin_unlock(&c->cs_lock);
3671e51764aSArtem Bityutskiy }
3681e51764aSArtem Bityutskiy
3691e51764aSArtem Bityutskiy /**
3701e51764aSArtem Bityutskiy * wait_for_commit - wait for commit.
3711e51764aSArtem Bityutskiy * @c: UBIFS file-system description object
3721e51764aSArtem Bityutskiy *
3731e51764aSArtem Bityutskiy * This function sleeps until the commit operation is no longer running.
3741e51764aSArtem Bityutskiy */
wait_for_commit(struct ubifs_info * c)3751e51764aSArtem Bityutskiy static int wait_for_commit(struct ubifs_info *c)
3761e51764aSArtem Bityutskiy {
3771e51764aSArtem Bityutskiy dbg_cmt("pid %d goes sleep", current->pid);
3781e51764aSArtem Bityutskiy
3791e51764aSArtem Bityutskiy /*
3801e51764aSArtem Bityutskiy * The following sleeps if the condition is false, and will be woken
3811e51764aSArtem Bityutskiy * when the commit ends. It is possible, although very unlikely, that we
3821e51764aSArtem Bityutskiy * will wake up and see the subsequent commit running, rather than the
3831e51764aSArtem Bityutskiy * one we were waiting for, and go back to sleep. However, we will be
3841e51764aSArtem Bityutskiy * woken again, so there is no danger of sleeping forever.
3851e51764aSArtem Bityutskiy */
3861e51764aSArtem Bityutskiy wait_event(c->cmt_wq, c->cmt_state != COMMIT_RUNNING_BACKGROUND &&
3871e51764aSArtem Bityutskiy c->cmt_state != COMMIT_RUNNING_REQUIRED);
3881e51764aSArtem Bityutskiy dbg_cmt("commit finished, pid %d woke up", current->pid);
3891e51764aSArtem Bityutskiy return 0;
3901e51764aSArtem Bityutskiy }
3911e51764aSArtem Bityutskiy
3921e51764aSArtem Bityutskiy /**
3931e51764aSArtem Bityutskiy * ubifs_run_commit - run or wait for commit.
3941e51764aSArtem Bityutskiy * @c: UBIFS file-system description object
3951e51764aSArtem Bityutskiy *
3961e51764aSArtem Bityutskiy * This function runs commit and returns zero in case of success and a negative
3971e51764aSArtem Bityutskiy * error code in case of failure.
3981e51764aSArtem Bityutskiy */
ubifs_run_commit(struct ubifs_info * c)3991e51764aSArtem Bityutskiy int ubifs_run_commit(struct ubifs_info *c)
4001e51764aSArtem Bityutskiy {
4011e51764aSArtem Bityutskiy int err = 0;
4021e51764aSArtem Bityutskiy
4031e51764aSArtem Bityutskiy spin_lock(&c->cs_lock);
4041e51764aSArtem Bityutskiy if (c->cmt_state == COMMIT_BROKEN) {
405549c999aSArtem Bityutskiy err = -EROFS;
4061e51764aSArtem Bityutskiy goto out;
4071e51764aSArtem Bityutskiy }
4081e51764aSArtem Bityutskiy
4091e51764aSArtem Bityutskiy if (c->cmt_state == COMMIT_RUNNING_BACKGROUND)
4101e51764aSArtem Bityutskiy /*
4111e51764aSArtem Bityutskiy * We set the commit state to 'running required' to indicate
4121e51764aSArtem Bityutskiy * that we want it to complete as quickly as possible.
4131e51764aSArtem Bityutskiy */
4141e51764aSArtem Bityutskiy c->cmt_state = COMMIT_RUNNING_REQUIRED;
4151e51764aSArtem Bityutskiy
4161e51764aSArtem Bityutskiy if (c->cmt_state == COMMIT_RUNNING_REQUIRED) {
4171e51764aSArtem Bityutskiy spin_unlock(&c->cs_lock);
4181e51764aSArtem Bityutskiy return wait_for_commit(c);
4191e51764aSArtem Bityutskiy }
4201e51764aSArtem Bityutskiy spin_unlock(&c->cs_lock);
4211e51764aSArtem Bityutskiy
4221e51764aSArtem Bityutskiy /* Ok, the commit is indeed needed */
4231e51764aSArtem Bityutskiy
4241e51764aSArtem Bityutskiy down_write(&c->commit_sem);
4251e51764aSArtem Bityutskiy spin_lock(&c->cs_lock);
4261e51764aSArtem Bityutskiy /*
4271e51764aSArtem Bityutskiy * Since we unlocked 'c->cs_lock', the state may have changed, so
4281e51764aSArtem Bityutskiy * re-check it.
4291e51764aSArtem Bityutskiy */
4301e51764aSArtem Bityutskiy if (c->cmt_state == COMMIT_BROKEN) {
431549c999aSArtem Bityutskiy err = -EROFS;
4321e51764aSArtem Bityutskiy goto out_cmt_unlock;
4331e51764aSArtem Bityutskiy }
4341e51764aSArtem Bityutskiy
4351e51764aSArtem Bityutskiy if (c->cmt_state == COMMIT_RUNNING_BACKGROUND)
4361e51764aSArtem Bityutskiy c->cmt_state = COMMIT_RUNNING_REQUIRED;
4371e51764aSArtem Bityutskiy
4381e51764aSArtem Bityutskiy if (c->cmt_state == COMMIT_RUNNING_REQUIRED) {
4391e51764aSArtem Bityutskiy up_write(&c->commit_sem);
4401e51764aSArtem Bityutskiy spin_unlock(&c->cs_lock);
4411e51764aSArtem Bityutskiy return wait_for_commit(c);
4421e51764aSArtem Bityutskiy }
4431e51764aSArtem Bityutskiy c->cmt_state = COMMIT_RUNNING_REQUIRED;
4441e51764aSArtem Bityutskiy spin_unlock(&c->cs_lock);
4451e51764aSArtem Bityutskiy
4461e51764aSArtem Bityutskiy err = do_commit(c);
4471e51764aSArtem Bityutskiy return err;
4481e51764aSArtem Bityutskiy
4491e51764aSArtem Bityutskiy out_cmt_unlock:
4501e51764aSArtem Bityutskiy up_write(&c->commit_sem);
4511e51764aSArtem Bityutskiy out:
4521e51764aSArtem Bityutskiy spin_unlock(&c->cs_lock);
4531e51764aSArtem Bityutskiy return err;
4541e51764aSArtem Bityutskiy }
4551e51764aSArtem Bityutskiy
4561e51764aSArtem Bityutskiy /**
4571e51764aSArtem Bityutskiy * ubifs_gc_should_commit - determine if it is time for GC to run commit.
4581e51764aSArtem Bityutskiy * @c: UBIFS file-system description object
4591e51764aSArtem Bityutskiy *
4601e51764aSArtem Bityutskiy * This function is called by garbage collection to determine if commit should
4611e51764aSArtem Bityutskiy * be run. If commit state is @COMMIT_BACKGROUND, which means that the journal
4621e51764aSArtem Bityutskiy * is full enough to start commit, this function returns true. It is not
4631e51764aSArtem Bityutskiy * absolutely necessary to commit yet, but it feels like this should be better
4641e51764aSArtem Bityutskiy * then to keep doing GC. This function returns %1 if GC has to initiate commit
4651e51764aSArtem Bityutskiy * and %0 if not.
4661e51764aSArtem Bityutskiy */
ubifs_gc_should_commit(struct ubifs_info * c)4671e51764aSArtem Bityutskiy int ubifs_gc_should_commit(struct ubifs_info *c)
4681e51764aSArtem Bityutskiy {
4691e51764aSArtem Bityutskiy int ret = 0;
4701e51764aSArtem Bityutskiy
4711e51764aSArtem Bityutskiy spin_lock(&c->cs_lock);
4721e51764aSArtem Bityutskiy if (c->cmt_state == COMMIT_BACKGROUND) {
4731e51764aSArtem Bityutskiy dbg_cmt("commit required now");
4741e51764aSArtem Bityutskiy c->cmt_state = COMMIT_REQUIRED;
4751e51764aSArtem Bityutskiy } else
4761e51764aSArtem Bityutskiy dbg_cmt("commit not requested");
4771e51764aSArtem Bityutskiy if (c->cmt_state == COMMIT_REQUIRED)
4781e51764aSArtem Bityutskiy ret = 1;
4791e51764aSArtem Bityutskiy spin_unlock(&c->cs_lock);
4801e51764aSArtem Bityutskiy return ret;
4811e51764aSArtem Bityutskiy }
4821e51764aSArtem Bityutskiy
483f70b7e52SArtem Bityutskiy /*
484f70b7e52SArtem Bityutskiy * Everything below is related to debugging.
485f70b7e52SArtem Bityutskiy */
4861e51764aSArtem Bityutskiy
4871e51764aSArtem Bityutskiy /**
4881e51764aSArtem Bityutskiy * struct idx_node - hold index nodes during index tree traversal.
4891e51764aSArtem Bityutskiy * @list: list
4901e51764aSArtem Bityutskiy * @iip: index in parent (slot number of this indexing node in the parent
4911e51764aSArtem Bityutskiy * indexing node)
4921e51764aSArtem Bityutskiy * @upper_key: all keys in this indexing node have to be less or equivalent to
4931e51764aSArtem Bityutskiy * this key
4941e51764aSArtem Bityutskiy * @idx: index node (8-byte aligned because all node structures must be 8-byte
4951e51764aSArtem Bityutskiy * aligned)
4961e51764aSArtem Bityutskiy */
4971e51764aSArtem Bityutskiy struct idx_node {
4981e51764aSArtem Bityutskiy struct list_head list;
4991e51764aSArtem Bityutskiy int iip;
5001e51764aSArtem Bityutskiy union ubifs_key upper_key;
50143457c60SArtem Bityutskiy struct ubifs_idx_node idx __aligned(8);
5021e51764aSArtem Bityutskiy };
5031e51764aSArtem Bityutskiy
5041e51764aSArtem Bityutskiy /**
5051e51764aSArtem Bityutskiy * dbg_old_index_check_init - get information for the next old index check.
5061e51764aSArtem Bityutskiy * @c: UBIFS file-system description object
5071e51764aSArtem Bityutskiy * @zroot: root of the index
5081e51764aSArtem Bityutskiy *
5091e51764aSArtem Bityutskiy * This function records information about the index that will be needed for the
5101e51764aSArtem Bityutskiy * next old index check i.e. 'dbg_check_old_index()'.
5111e51764aSArtem Bityutskiy *
5121e51764aSArtem Bityutskiy * This function returns %0 on success and a negative error code on failure.
5131e51764aSArtem Bityutskiy */
dbg_old_index_check_init(struct ubifs_info * c,struct ubifs_zbranch * zroot)5141e51764aSArtem Bityutskiy int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot)
5151e51764aSArtem Bityutskiy {
5161e51764aSArtem Bityutskiy struct ubifs_idx_node *idx;
5171e51764aSArtem Bityutskiy int lnum, offs, len, err = 0;
51817c2f9f8SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg;
5191e51764aSArtem Bityutskiy
52017c2f9f8SArtem Bityutskiy d->old_zroot = *zroot;
52117c2f9f8SArtem Bityutskiy lnum = d->old_zroot.lnum;
52217c2f9f8SArtem Bityutskiy offs = d->old_zroot.offs;
52317c2f9f8SArtem Bityutskiy len = d->old_zroot.len;
5241e51764aSArtem Bityutskiy
5251e51764aSArtem Bityutskiy idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
5261e51764aSArtem Bityutskiy if (!idx)
5271e51764aSArtem Bityutskiy return -ENOMEM;
5281e51764aSArtem Bityutskiy
5291e51764aSArtem Bityutskiy err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
5301e51764aSArtem Bityutskiy if (err)
5311e51764aSArtem Bityutskiy goto out;
5321e51764aSArtem Bityutskiy
53317c2f9f8SArtem Bityutskiy d->old_zroot_level = le16_to_cpu(idx->level);
53417c2f9f8SArtem Bityutskiy d->old_zroot_sqnum = le64_to_cpu(idx->ch.sqnum);
5351e51764aSArtem Bityutskiy out:
5361e51764aSArtem Bityutskiy kfree(idx);
5371e51764aSArtem Bityutskiy return err;
5381e51764aSArtem Bityutskiy }
5391e51764aSArtem Bityutskiy
5401e51764aSArtem Bityutskiy /**
5411e51764aSArtem Bityutskiy * dbg_check_old_index - check the old copy of the index.
5421e51764aSArtem Bityutskiy * @c: UBIFS file-system description object
5431e51764aSArtem Bityutskiy * @zroot: root of the new index
5441e51764aSArtem Bityutskiy *
5451e51764aSArtem Bityutskiy * In order to be able to recover from an unclean unmount, a complete copy of
5461e51764aSArtem Bityutskiy * the index must exist on flash. This is the "old" index. The commit process
5471e51764aSArtem Bityutskiy * must write the "new" index to flash without overwriting or destroying any
5481e51764aSArtem Bityutskiy * part of the old index. This function is run at commit end in order to check
5491e51764aSArtem Bityutskiy * that the old index does indeed exist completely intact.
5501e51764aSArtem Bityutskiy *
5511e51764aSArtem Bityutskiy * This function returns %0 on success and a negative error code on failure.
5521e51764aSArtem Bityutskiy */
dbg_check_old_index(struct ubifs_info * c,struct ubifs_zbranch * zroot)5531e51764aSArtem Bityutskiy int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot)
5541e51764aSArtem Bityutskiy {
5553f649ab7SKees Cook int lnum, offs, len, err = 0, last_level, child_cnt;
5561e51764aSArtem Bityutskiy int first = 1, iip;
55717c2f9f8SArtem Bityutskiy struct ubifs_debug_info *d = c->dbg;
5583f649ab7SKees Cook union ubifs_key lower_key, upper_key, l_key, u_key;
5593f649ab7SKees Cook unsigned long long last_sqnum;
5601e51764aSArtem Bityutskiy struct ubifs_idx_node *idx;
5611e51764aSArtem Bityutskiy struct list_head list;
5621e51764aSArtem Bityutskiy struct idx_node *i;
5631e51764aSArtem Bityutskiy size_t sz;
5641e51764aSArtem Bityutskiy
5658d7819b4SArtem Bityutskiy if (!dbg_is_chk_index(c))
5668b229c76SArtem Bityutskiy return 0;
5671e51764aSArtem Bityutskiy
5681e51764aSArtem Bityutskiy INIT_LIST_HEAD(&list);
5691e51764aSArtem Bityutskiy
5701e51764aSArtem Bityutskiy sz = sizeof(struct idx_node) + ubifs_idx_node_sz(c, c->fanout) -
5711e51764aSArtem Bityutskiy UBIFS_IDX_NODE_SZ;
5721e51764aSArtem Bityutskiy
5731e51764aSArtem Bityutskiy /* Start at the old zroot */
57417c2f9f8SArtem Bityutskiy lnum = d->old_zroot.lnum;
57517c2f9f8SArtem Bityutskiy offs = d->old_zroot.offs;
57617c2f9f8SArtem Bityutskiy len = d->old_zroot.len;
5771e51764aSArtem Bityutskiy iip = 0;
5781e51764aSArtem Bityutskiy
5791e51764aSArtem Bityutskiy /*
5801e51764aSArtem Bityutskiy * Traverse the index tree preorder depth-first i.e. do a node and then
5811e51764aSArtem Bityutskiy * its subtrees from left to right.
5821e51764aSArtem Bityutskiy */
5831e51764aSArtem Bityutskiy while (1) {
5841e51764aSArtem Bityutskiy struct ubifs_branch *br;
5851e51764aSArtem Bityutskiy
5861e51764aSArtem Bityutskiy /* Get the next index node */
5871e51764aSArtem Bityutskiy i = kmalloc(sz, GFP_NOFS);
5881e51764aSArtem Bityutskiy if (!i) {
5891e51764aSArtem Bityutskiy err = -ENOMEM;
5901e51764aSArtem Bityutskiy goto out_free;
5911e51764aSArtem Bityutskiy }
5921e51764aSArtem Bityutskiy i->iip = iip;
5931e51764aSArtem Bityutskiy /* Keep the index nodes on our path in a linked list */
5941e51764aSArtem Bityutskiy list_add_tail(&i->list, &list);
5951e51764aSArtem Bityutskiy /* Read the index node */
5961e51764aSArtem Bityutskiy idx = &i->idx;
5971e51764aSArtem Bityutskiy err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
5981e51764aSArtem Bityutskiy if (err)
5991e51764aSArtem Bityutskiy goto out_free;
6001e51764aSArtem Bityutskiy /* Validate index node */
6011e51764aSArtem Bityutskiy child_cnt = le16_to_cpu(idx->child_cnt);
6021e51764aSArtem Bityutskiy if (child_cnt < 1 || child_cnt > c->fanout) {
6031e51764aSArtem Bityutskiy err = 1;
6041e51764aSArtem Bityutskiy goto out_dump;
6051e51764aSArtem Bityutskiy }
6061e51764aSArtem Bityutskiy if (first) {
6071e51764aSArtem Bityutskiy first = 0;
6081e51764aSArtem Bityutskiy /* Check root level and sqnum */
60917c2f9f8SArtem Bityutskiy if (le16_to_cpu(idx->level) != d->old_zroot_level) {
6101e51764aSArtem Bityutskiy err = 2;
6111e51764aSArtem Bityutskiy goto out_dump;
6121e51764aSArtem Bityutskiy }
61317c2f9f8SArtem Bityutskiy if (le64_to_cpu(idx->ch.sqnum) != d->old_zroot_sqnum) {
6141e51764aSArtem Bityutskiy err = 3;
6151e51764aSArtem Bityutskiy goto out_dump;
6161e51764aSArtem Bityutskiy }
6171e51764aSArtem Bityutskiy /* Set last values as though root had a parent */
6181e51764aSArtem Bityutskiy last_level = le16_to_cpu(idx->level) + 1;
6191e51764aSArtem Bityutskiy last_sqnum = le64_to_cpu(idx->ch.sqnum) + 1;
6201e51764aSArtem Bityutskiy key_read(c, ubifs_idx_key(c, idx), &lower_key);
6211e51764aSArtem Bityutskiy highest_ino_key(c, &upper_key, INUM_WATERMARK);
6221e51764aSArtem Bityutskiy }
6231e51764aSArtem Bityutskiy key_copy(c, &upper_key, &i->upper_key);
6241e51764aSArtem Bityutskiy if (le16_to_cpu(idx->level) != last_level - 1) {
6251e51764aSArtem Bityutskiy err = 3;
6261e51764aSArtem Bityutskiy goto out_dump;
6271e51764aSArtem Bityutskiy }
6281e51764aSArtem Bityutskiy /*
6291e51764aSArtem Bityutskiy * The index is always written bottom up hence a child's sqnum
6301e51764aSArtem Bityutskiy * is always less than the parents.
6311e51764aSArtem Bityutskiy */
6321e51764aSArtem Bityutskiy if (le64_to_cpu(idx->ch.sqnum) >= last_sqnum) {
6331e51764aSArtem Bityutskiy err = 4;
6341e51764aSArtem Bityutskiy goto out_dump;
6351e51764aSArtem Bityutskiy }
6361e51764aSArtem Bityutskiy /* Check key range */
6371e51764aSArtem Bityutskiy key_read(c, ubifs_idx_key(c, idx), &l_key);
6381e51764aSArtem Bityutskiy br = ubifs_idx_branch(c, idx, child_cnt - 1);
6391e51764aSArtem Bityutskiy key_read(c, &br->key, &u_key);
6401e51764aSArtem Bityutskiy if (keys_cmp(c, &lower_key, &l_key) > 0) {
6411e51764aSArtem Bityutskiy err = 5;
6421e51764aSArtem Bityutskiy goto out_dump;
6431e51764aSArtem Bityutskiy }
6441e51764aSArtem Bityutskiy if (keys_cmp(c, &upper_key, &u_key) < 0) {
6451e51764aSArtem Bityutskiy err = 6;
6461e51764aSArtem Bityutskiy goto out_dump;
6471e51764aSArtem Bityutskiy }
6481e51764aSArtem Bityutskiy if (keys_cmp(c, &upper_key, &u_key) == 0)
6491e51764aSArtem Bityutskiy if (!is_hash_key(c, &u_key)) {
6501e51764aSArtem Bityutskiy err = 7;
6511e51764aSArtem Bityutskiy goto out_dump;
6521e51764aSArtem Bityutskiy }
6531e51764aSArtem Bityutskiy /* Go to next index node */
6541e51764aSArtem Bityutskiy if (le16_to_cpu(idx->level) == 0) {
6551e51764aSArtem Bityutskiy /* At the bottom, so go up until can go right */
6561e51764aSArtem Bityutskiy while (1) {
6571e51764aSArtem Bityutskiy /* Drop the bottom of the list */
6581e51764aSArtem Bityutskiy list_del(&i->list);
6591e51764aSArtem Bityutskiy kfree(i);
6601e51764aSArtem Bityutskiy /* No more list means we are done */
6611e51764aSArtem Bityutskiy if (list_empty(&list))
6621e51764aSArtem Bityutskiy goto out;
6631e51764aSArtem Bityutskiy /* Look at the new bottom */
6641e51764aSArtem Bityutskiy i = list_entry(list.prev, struct idx_node,
6651e51764aSArtem Bityutskiy list);
6661e51764aSArtem Bityutskiy idx = &i->idx;
6671e51764aSArtem Bityutskiy /* Can we go right */
6681e51764aSArtem Bityutskiy if (iip + 1 < le16_to_cpu(idx->child_cnt)) {
6691e51764aSArtem Bityutskiy iip = iip + 1;
6701e51764aSArtem Bityutskiy break;
6711e51764aSArtem Bityutskiy } else
6721e51764aSArtem Bityutskiy /* Nope, so go up again */
6731e51764aSArtem Bityutskiy iip = i->iip;
6741e51764aSArtem Bityutskiy }
6751e51764aSArtem Bityutskiy } else
6761e51764aSArtem Bityutskiy /* Go down left */
6771e51764aSArtem Bityutskiy iip = 0;
6781e51764aSArtem Bityutskiy /*
6791e51764aSArtem Bityutskiy * We have the parent in 'idx' and now we set up for reading the
6801e51764aSArtem Bityutskiy * child pointed to by slot 'iip'.
6811e51764aSArtem Bityutskiy */
6821e51764aSArtem Bityutskiy last_level = le16_to_cpu(idx->level);
6831e51764aSArtem Bityutskiy last_sqnum = le64_to_cpu(idx->ch.sqnum);
6841e51764aSArtem Bityutskiy br = ubifs_idx_branch(c, idx, iip);
6851e51764aSArtem Bityutskiy lnum = le32_to_cpu(br->lnum);
6861e51764aSArtem Bityutskiy offs = le32_to_cpu(br->offs);
6871e51764aSArtem Bityutskiy len = le32_to_cpu(br->len);
6881e51764aSArtem Bityutskiy key_read(c, &br->key, &lower_key);
6891e51764aSArtem Bityutskiy if (iip + 1 < le16_to_cpu(idx->child_cnt)) {
6901e51764aSArtem Bityutskiy br = ubifs_idx_branch(c, idx, iip + 1);
6911e51764aSArtem Bityutskiy key_read(c, &br->key, &upper_key);
6921e51764aSArtem Bityutskiy } else
6931e51764aSArtem Bityutskiy key_copy(c, &i->upper_key, &upper_key);
6941e51764aSArtem Bityutskiy }
6951e51764aSArtem Bityutskiy out:
6961e51764aSArtem Bityutskiy err = dbg_old_index_check_init(c, zroot);
6971e51764aSArtem Bityutskiy if (err)
6981e51764aSArtem Bityutskiy goto out_free;
6991e51764aSArtem Bityutskiy
7001e51764aSArtem Bityutskiy return 0;
7011e51764aSArtem Bityutskiy
7021e51764aSArtem Bityutskiy out_dump:
703235c362bSSheng Yong ubifs_err(c, "dumping index node (iip=%d)", i->iip);
704*a33e30a0SZhihao Cheng ubifs_dump_node(c, idx, ubifs_idx_node_sz(c, c->fanout));
7051e51764aSArtem Bityutskiy list_del(&i->list);
7061e51764aSArtem Bityutskiy kfree(i);
7071e51764aSArtem Bityutskiy if (!list_empty(&list)) {
7081e51764aSArtem Bityutskiy i = list_entry(list.prev, struct idx_node, list);
709235c362bSSheng Yong ubifs_err(c, "dumping parent index node");
710*a33e30a0SZhihao Cheng ubifs_dump_node(c, &i->idx, ubifs_idx_node_sz(c, c->fanout));
7111e51764aSArtem Bityutskiy }
7121e51764aSArtem Bityutskiy out_free:
7131e51764aSArtem Bityutskiy while (!list_empty(&list)) {
7141e51764aSArtem Bityutskiy i = list_entry(list.next, struct idx_node, list);
7151e51764aSArtem Bityutskiy list_del(&i->list);
7161e51764aSArtem Bityutskiy kfree(i);
7171e51764aSArtem Bityutskiy }
718235c362bSSheng Yong ubifs_err(c, "failed, error %d", err);
7191e51764aSArtem Bityutskiy if (err > 0)
7201e51764aSArtem Bityutskiy err = -EINVAL;
7211e51764aSArtem Bityutskiy return err;
7221e51764aSArtem Bityutskiy }
723