xref: /linux-6.15/fs/ubifs/commit.c (revision c07a4dab)
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 	/*
73*c07a4dabSZhihao Cheng 	 * Increasing @c->dirty_pn_cnt/@c->dirty_nn_cnt and marking
74*c07a4dabSZhihao Cheng 	 * nnodes/pnodes as dirty in run_gc() could race with following
75*c07a4dabSZhihao Cheng 	 * checking, which leads inconsistent states between @c->nroot
76*c07a4dabSZhihao Cheng 	 * and @c->dirty_pn_cnt/@c->dirty_nn_cnt, holding @c->lp_mutex
77*c07a4dabSZhihao Cheng 	 * to avoid that.
78*c07a4dabSZhihao Cheng 	 */
79*c07a4dabSZhihao Cheng 	mutex_lock(&c->lp_mutex);
80*c07a4dabSZhihao Cheng 	/*
81944fdef5SArtem Bityutskiy 	 * Even though the TNC is clean, the LPT tree may have dirty nodes. For
82944fdef5SArtem Bityutskiy 	 * example, this may happen if the budgeting subsystem invoked GC to
83944fdef5SArtem Bityutskiy 	 * make some free space, and the GC found an LEB with only dirty and
84944fdef5SArtem Bityutskiy 	 * free space. In this case GC would just change the lprops of this
85944fdef5SArtem Bityutskiy 	 * LEB (by turning all space into free space) and unmap it.
86944fdef5SArtem Bityutskiy 	 */
87*c07a4dabSZhihao Cheng 	if (c->nroot && test_bit(DIRTY_CNODE, &c->nroot->flags)) {
88*c07a4dabSZhihao Cheng 		mutex_unlock(&c->lp_mutex);
89944fdef5SArtem Bityutskiy 		return 0;
90*c07a4dabSZhihao Cheng 	}
91944fdef5SArtem Bityutskiy 
926eb61d58SRichard Weinberger 	ubifs_assert(c, atomic_long_read(&c->dirty_zn_cnt) == 0);
936eb61d58SRichard Weinberger 	ubifs_assert(c, c->dirty_pn_cnt == 0);
946eb61d58SRichard Weinberger 	ubifs_assert(c, c->dirty_nn_cnt == 0);
95*c07a4dabSZhihao Cheng 	mutex_unlock(&c->lp_mutex);
96944fdef5SArtem Bityutskiy 
97944fdef5SArtem Bityutskiy 	return 1;
98944fdef5SArtem Bityutskiy }
99944fdef5SArtem Bityutskiy 
1001e51764aSArtem Bityutskiy /**
1011e51764aSArtem Bityutskiy  * do_commit - commit the journal.
1021e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1031e51764aSArtem Bityutskiy  *
1041e51764aSArtem Bityutskiy  * This function implements UBIFS commit. It has to be called with commit lock
1051e51764aSArtem Bityutskiy  * locked. Returns zero in case of success and a negative error code in case of
1061e51764aSArtem Bityutskiy  * failure.
1071e51764aSArtem Bityutskiy  */
do_commit(struct ubifs_info * c)1081e51764aSArtem Bityutskiy static int do_commit(struct ubifs_info *c)
1091e51764aSArtem Bityutskiy {
1101e51764aSArtem Bityutskiy 	int err, new_ltail_lnum, old_ltail_lnum, i;
1111e51764aSArtem Bityutskiy 	struct ubifs_zbranch zroot;
1121e51764aSArtem Bityutskiy 	struct ubifs_lp_stats lst;
1131e51764aSArtem Bityutskiy 
1141e51764aSArtem Bityutskiy 	dbg_cmt("start");
1156eb61d58SRichard Weinberger 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
1162680d722SArtem Bityutskiy 
1172680d722SArtem Bityutskiy 	if (c->ro_error) {
1181e51764aSArtem Bityutskiy 		err = -EROFS;
1191e51764aSArtem Bityutskiy 		goto out_up;
1201e51764aSArtem Bityutskiy 	}
1211e51764aSArtem Bityutskiy 
122944fdef5SArtem Bityutskiy 	if (nothing_to_commit(c)) {
123944fdef5SArtem Bityutskiy 		up_write(&c->commit_sem);
124944fdef5SArtem Bityutskiy 		err = 0;
125944fdef5SArtem Bityutskiy 		goto out_cancel;
126944fdef5SArtem Bityutskiy 	}
127944fdef5SArtem Bityutskiy 
1281e51764aSArtem Bityutskiy 	/* Sync all write buffers (necessary for recovery) */
1291e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
1301e51764aSArtem Bityutskiy 		err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
1311e51764aSArtem Bityutskiy 		if (err)
1321e51764aSArtem Bityutskiy 			goto out_up;
1331e51764aSArtem Bityutskiy 	}
1341e51764aSArtem Bityutskiy 
135014eb04bSArtem Bityutskiy 	c->cmt_no += 1;
1361e51764aSArtem Bityutskiy 	err = ubifs_gc_start_commit(c);
1371e51764aSArtem Bityutskiy 	if (err)
1381e51764aSArtem Bityutskiy 		goto out_up;
1391e51764aSArtem Bityutskiy 	err = dbg_check_lprops(c);
1401e51764aSArtem Bityutskiy 	if (err)
1411e51764aSArtem Bityutskiy 		goto out_up;
1421e51764aSArtem Bityutskiy 	err = ubifs_log_start_commit(c, &new_ltail_lnum);
1431e51764aSArtem Bityutskiy 	if (err)
1441e51764aSArtem Bityutskiy 		goto out_up;
1451e51764aSArtem Bityutskiy 	err = ubifs_tnc_start_commit(c, &zroot);
1461e51764aSArtem Bityutskiy 	if (err)
1471e51764aSArtem Bityutskiy 		goto out_up;
1481e51764aSArtem Bityutskiy 	err = ubifs_lpt_start_commit(c);
1491e51764aSArtem Bityutskiy 	if (err)
1501e51764aSArtem Bityutskiy 		goto out_up;
1511e51764aSArtem Bityutskiy 	err = ubifs_orphan_start_commit(c);
1521e51764aSArtem Bityutskiy 	if (err)
1531e51764aSArtem Bityutskiy 		goto out_up;
1541e51764aSArtem Bityutskiy 
1551e51764aSArtem Bityutskiy 	ubifs_get_lp_stats(c, &lst);
1561e51764aSArtem Bityutskiy 
1571e51764aSArtem Bityutskiy 	up_write(&c->commit_sem);
1581e51764aSArtem Bityutskiy 
1591e51764aSArtem Bityutskiy 	err = ubifs_tnc_end_commit(c);
1601e51764aSArtem Bityutskiy 	if (err)
1611e51764aSArtem Bityutskiy 		goto out;
1621e51764aSArtem Bityutskiy 	err = ubifs_lpt_end_commit(c);
1631e51764aSArtem Bityutskiy 	if (err)
1641e51764aSArtem Bityutskiy 		goto out;
1651e51764aSArtem Bityutskiy 	err = ubifs_orphan_end_commit(c);
1661e51764aSArtem Bityutskiy 	if (err)
1671e51764aSArtem Bityutskiy 		goto out;
1681e51764aSArtem Bityutskiy 	err = dbg_check_old_index(c, &zroot);
1691e51764aSArtem Bityutskiy 	if (err)
1701e51764aSArtem Bityutskiy 		goto out;
1711e51764aSArtem Bityutskiy 
172014eb04bSArtem Bityutskiy 	c->mst_node->cmt_no      = cpu_to_le64(c->cmt_no);
1731e51764aSArtem Bityutskiy 	c->mst_node->log_lnum    = cpu_to_le32(new_ltail_lnum);
1741e51764aSArtem Bityutskiy 	c->mst_node->root_lnum   = cpu_to_le32(zroot.lnum);
1751e51764aSArtem Bityutskiy 	c->mst_node->root_offs   = cpu_to_le32(zroot.offs);
1761e51764aSArtem Bityutskiy 	c->mst_node->root_len    = cpu_to_le32(zroot.len);
1771e51764aSArtem Bityutskiy 	c->mst_node->ihead_lnum  = cpu_to_le32(c->ihead_lnum);
1781e51764aSArtem Bityutskiy 	c->mst_node->ihead_offs  = cpu_to_le32(c->ihead_offs);
179b137545cSArtem Bityutskiy 	c->mst_node->index_size  = cpu_to_le64(c->bi.old_idx_sz);
1801e51764aSArtem Bityutskiy 	c->mst_node->lpt_lnum    = cpu_to_le32(c->lpt_lnum);
1811e51764aSArtem Bityutskiy 	c->mst_node->lpt_offs    = cpu_to_le32(c->lpt_offs);
1821e51764aSArtem Bityutskiy 	c->mst_node->nhead_lnum  = cpu_to_le32(c->nhead_lnum);
1831e51764aSArtem Bityutskiy 	c->mst_node->nhead_offs  = cpu_to_le32(c->nhead_offs);
1841e51764aSArtem Bityutskiy 	c->mst_node->ltab_lnum   = cpu_to_le32(c->ltab_lnum);
1851e51764aSArtem Bityutskiy 	c->mst_node->ltab_offs   = cpu_to_le32(c->ltab_offs);
1861e51764aSArtem Bityutskiy 	c->mst_node->lsave_lnum  = cpu_to_le32(c->lsave_lnum);
1871e51764aSArtem Bityutskiy 	c->mst_node->lsave_offs  = cpu_to_le32(c->lsave_offs);
1881e51764aSArtem Bityutskiy 	c->mst_node->lscan_lnum  = cpu_to_le32(c->lscan_lnum);
1891e51764aSArtem Bityutskiy 	c->mst_node->empty_lebs  = cpu_to_le32(lst.empty_lebs);
1901e51764aSArtem Bityutskiy 	c->mst_node->idx_lebs    = cpu_to_le32(lst.idx_lebs);
1911e51764aSArtem Bityutskiy 	c->mst_node->total_free  = cpu_to_le64(lst.total_free);
1921e51764aSArtem Bityutskiy 	c->mst_node->total_dirty = cpu_to_le64(lst.total_dirty);
1931e51764aSArtem Bityutskiy 	c->mst_node->total_used  = cpu_to_le64(lst.total_used);
1941e51764aSArtem Bityutskiy 	c->mst_node->total_dead  = cpu_to_le64(lst.total_dead);
1951e51764aSArtem Bityutskiy 	c->mst_node->total_dark  = cpu_to_le64(lst.total_dark);
1961e51764aSArtem Bityutskiy 	if (c->no_orphs)
1971e51764aSArtem Bityutskiy 		c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
1981e51764aSArtem Bityutskiy 	else
1991e51764aSArtem Bityutskiy 		c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_NO_ORPHS);
200052c2807SArtem Bityutskiy 
201052c2807SArtem Bityutskiy 	old_ltail_lnum = c->ltail_lnum;
202052c2807SArtem Bityutskiy 	err = ubifs_log_end_commit(c, new_ltail_lnum);
2031e51764aSArtem Bityutskiy 	if (err)
2041e51764aSArtem Bityutskiy 		goto out;
2051e51764aSArtem Bityutskiy 
2061e51764aSArtem Bityutskiy 	err = ubifs_log_post_commit(c, old_ltail_lnum);
2071e51764aSArtem Bityutskiy 	if (err)
2081e51764aSArtem Bityutskiy 		goto out;
2091e51764aSArtem Bityutskiy 	err = ubifs_gc_end_commit(c);
2101e51764aSArtem Bityutskiy 	if (err)
2111e51764aSArtem Bityutskiy 		goto out;
2121e51764aSArtem Bityutskiy 	err = ubifs_lpt_post_commit(c);
2131e51764aSArtem Bityutskiy 	if (err)
2141e51764aSArtem Bityutskiy 		goto out;
2151e51764aSArtem Bityutskiy 
216944fdef5SArtem Bityutskiy out_cancel:
2171e51764aSArtem Bityutskiy 	spin_lock(&c->cs_lock);
2181e51764aSArtem Bityutskiy 	c->cmt_state = COMMIT_RESTING;
2191e51764aSArtem Bityutskiy 	wake_up(&c->cmt_wq);
2201e51764aSArtem Bityutskiy 	dbg_cmt("commit end");
2211e51764aSArtem Bityutskiy 	spin_unlock(&c->cs_lock);
2221e51764aSArtem Bityutskiy 	return 0;
2231e51764aSArtem Bityutskiy 
2241e51764aSArtem Bityutskiy out_up:
2251e51764aSArtem Bityutskiy 	up_write(&c->commit_sem);
2261e51764aSArtem Bityutskiy out:
227235c362bSSheng Yong 	ubifs_err(c, "commit failed, error %d", err);
2281e51764aSArtem Bityutskiy 	spin_lock(&c->cs_lock);
2291e51764aSArtem Bityutskiy 	c->cmt_state = COMMIT_BROKEN;
2301e51764aSArtem Bityutskiy 	wake_up(&c->cmt_wq);
2311e51764aSArtem Bityutskiy 	spin_unlock(&c->cs_lock);
2321e51764aSArtem Bityutskiy 	ubifs_ro_mode(c, err);
2331e51764aSArtem Bityutskiy 	return err;
2341e51764aSArtem Bityutskiy }
2351e51764aSArtem Bityutskiy 
2361e51764aSArtem Bityutskiy /**
2371e51764aSArtem Bityutskiy  * run_bg_commit - run background commit if it is needed.
2381e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2391e51764aSArtem Bityutskiy  *
2401e51764aSArtem Bityutskiy  * This function runs background commit if it is needed. Returns zero in case
2411e51764aSArtem Bityutskiy  * of success and a negative error code in case of failure.
2421e51764aSArtem Bityutskiy  */
run_bg_commit(struct ubifs_info * c)2431e51764aSArtem Bityutskiy static int run_bg_commit(struct ubifs_info *c)
2441e51764aSArtem Bityutskiy {
2451e51764aSArtem Bityutskiy 	spin_lock(&c->cs_lock);
2461e51764aSArtem Bityutskiy 	/*
2471e51764aSArtem Bityutskiy 	 * Run background commit only if background commit was requested or if
2481e51764aSArtem Bityutskiy 	 * commit is required.
2491e51764aSArtem Bityutskiy 	 */
2501e51764aSArtem Bityutskiy 	if (c->cmt_state != COMMIT_BACKGROUND &&
2511e51764aSArtem Bityutskiy 	    c->cmt_state != COMMIT_REQUIRED)
2521e51764aSArtem Bityutskiy 		goto out;
2531e51764aSArtem Bityutskiy 	spin_unlock(&c->cs_lock);
2541e51764aSArtem Bityutskiy 
2551e51764aSArtem Bityutskiy 	down_write(&c->commit_sem);
2561e51764aSArtem Bityutskiy 	spin_lock(&c->cs_lock);
2571e51764aSArtem Bityutskiy 	if (c->cmt_state == COMMIT_REQUIRED)
2581e51764aSArtem Bityutskiy 		c->cmt_state = COMMIT_RUNNING_REQUIRED;
2591e51764aSArtem Bityutskiy 	else if (c->cmt_state == COMMIT_BACKGROUND)
2601e51764aSArtem Bityutskiy 		c->cmt_state = COMMIT_RUNNING_BACKGROUND;
2611e51764aSArtem Bityutskiy 	else
2621e51764aSArtem Bityutskiy 		goto out_cmt_unlock;
2631e51764aSArtem Bityutskiy 	spin_unlock(&c->cs_lock);
2641e51764aSArtem Bityutskiy 
2651e51764aSArtem Bityutskiy 	return do_commit(c);
2661e51764aSArtem Bityutskiy 
2671e51764aSArtem Bityutskiy out_cmt_unlock:
2681e51764aSArtem Bityutskiy 	up_write(&c->commit_sem);
2691e51764aSArtem Bityutskiy out:
2701e51764aSArtem Bityutskiy 	spin_unlock(&c->cs_lock);
2711e51764aSArtem Bityutskiy 	return 0;
2721e51764aSArtem Bityutskiy }
2731e51764aSArtem Bityutskiy 
2741e51764aSArtem Bityutskiy /**
2751e51764aSArtem Bityutskiy  * ubifs_bg_thread - UBIFS background thread function.
2761e51764aSArtem Bityutskiy  * @info: points to the file-system description object
2771e51764aSArtem Bityutskiy  *
2781e51764aSArtem Bityutskiy  * This function implements various file-system background activities:
2791e51764aSArtem Bityutskiy  * o when a write-buffer timer expires it synchronizes the appropriate
2801e51764aSArtem Bityutskiy  *   write-buffer;
2811e51764aSArtem Bityutskiy  * o when the journal is about to be full, it starts in-advance commit.
2821e51764aSArtem Bityutskiy  *
2831e51764aSArtem Bityutskiy  * Note, other stuff like background garbage collection may be added here in
2841e51764aSArtem Bityutskiy  * future.
2851e51764aSArtem Bityutskiy  */
ubifs_bg_thread(void * info)2861e51764aSArtem Bityutskiy int ubifs_bg_thread(void *info)
2871e51764aSArtem Bityutskiy {
2881e51764aSArtem Bityutskiy 	int err;
2891e51764aSArtem Bityutskiy 	struct ubifs_info *c = info;
2901e51764aSArtem Bityutskiy 
291235c362bSSheng Yong 	ubifs_msg(c, "background thread \"%s\" started, PID %d",
2921e51764aSArtem Bityutskiy 		  c->bgt_name, current->pid);
2931e51764aSArtem Bityutskiy 	set_freezable();
2941e51764aSArtem Bityutskiy 
2951e51764aSArtem Bityutskiy 	while (1) {
2961e51764aSArtem Bityutskiy 		if (kthread_should_stop())
2971e51764aSArtem Bityutskiy 			break;
2981e51764aSArtem Bityutskiy 
2991e51764aSArtem Bityutskiy 		if (try_to_freeze())
3001e51764aSArtem Bityutskiy 			continue;
3011e51764aSArtem Bityutskiy 
3021e51764aSArtem Bityutskiy 		set_current_state(TASK_INTERRUPTIBLE);
3031e51764aSArtem Bityutskiy 		/* Check if there is something to do */
3041e51764aSArtem Bityutskiy 		if (!c->need_bgt) {
3051e51764aSArtem Bityutskiy 			/*
3061e51764aSArtem Bityutskiy 			 * Nothing prevents us from going sleep now and
3071e51764aSArtem Bityutskiy 			 * be never woken up and block the task which
3081e51764aSArtem Bityutskiy 			 * could wait in 'kthread_stop()' forever.
3091e51764aSArtem Bityutskiy 			 */
3101e51764aSArtem Bityutskiy 			if (kthread_should_stop())
3111e51764aSArtem Bityutskiy 				break;
3121e51764aSArtem Bityutskiy 			schedule();
3131e51764aSArtem Bityutskiy 			continue;
3141e51764aSArtem Bityutskiy 		} else
3151e51764aSArtem Bityutskiy 			__set_current_state(TASK_RUNNING);
3161e51764aSArtem Bityutskiy 
3171e51764aSArtem Bityutskiy 		c->need_bgt = 0;
3181e51764aSArtem Bityutskiy 		err = ubifs_bg_wbufs_sync(c);
3191e51764aSArtem Bityutskiy 		if (err)
3201e51764aSArtem Bityutskiy 			ubifs_ro_mode(c, err);
3211e51764aSArtem Bityutskiy 
3221e51764aSArtem Bityutskiy 		run_bg_commit(c);
3231e51764aSArtem Bityutskiy 		cond_resched();
3241e51764aSArtem Bityutskiy 	}
3251e51764aSArtem Bityutskiy 
326235c362bSSheng Yong 	ubifs_msg(c, "background thread \"%s\" stops", c->bgt_name);
3271e51764aSArtem Bityutskiy 	return 0;
3281e51764aSArtem Bityutskiy }
3291e51764aSArtem Bityutskiy 
3301e51764aSArtem Bityutskiy /**
3311e51764aSArtem Bityutskiy  * ubifs_commit_required - set commit state to "required".
3321e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3331e51764aSArtem Bityutskiy  *
3341e51764aSArtem Bityutskiy  * This function is called if a commit is required but cannot be done from the
3351e51764aSArtem Bityutskiy  * calling function, so it is just flagged instead.
3361e51764aSArtem Bityutskiy  */
ubifs_commit_required(struct ubifs_info * c)3371e51764aSArtem Bityutskiy void ubifs_commit_required(struct ubifs_info *c)
3381e51764aSArtem Bityutskiy {
3391e51764aSArtem Bityutskiy 	spin_lock(&c->cs_lock);
3401e51764aSArtem Bityutskiy 	switch (c->cmt_state) {
3411e51764aSArtem Bityutskiy 	case COMMIT_RESTING:
3421e51764aSArtem Bityutskiy 	case COMMIT_BACKGROUND:
3431e51764aSArtem Bityutskiy 		dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
3441e51764aSArtem Bityutskiy 			dbg_cstate(COMMIT_REQUIRED));
3451e51764aSArtem Bityutskiy 		c->cmt_state = COMMIT_REQUIRED;
3461e51764aSArtem Bityutskiy 		break;
3471e51764aSArtem Bityutskiy 	case COMMIT_RUNNING_BACKGROUND:
3481e51764aSArtem Bityutskiy 		dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
3491e51764aSArtem Bityutskiy 			dbg_cstate(COMMIT_RUNNING_REQUIRED));
3501e51764aSArtem Bityutskiy 		c->cmt_state = COMMIT_RUNNING_REQUIRED;
3511e51764aSArtem Bityutskiy 		break;
3521e51764aSArtem Bityutskiy 	case COMMIT_REQUIRED:
3531e51764aSArtem Bityutskiy 	case COMMIT_RUNNING_REQUIRED:
3541e51764aSArtem Bityutskiy 	case COMMIT_BROKEN:
3551e51764aSArtem Bityutskiy 		break;
3561e51764aSArtem Bityutskiy 	}
3571e51764aSArtem Bityutskiy 	spin_unlock(&c->cs_lock);
3581e51764aSArtem Bityutskiy }
3591e51764aSArtem Bityutskiy 
3601e51764aSArtem Bityutskiy /**
3611e51764aSArtem Bityutskiy  * ubifs_request_bg_commit - notify the background thread to do a commit.
3621e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3631e51764aSArtem Bityutskiy  *
3641e51764aSArtem Bityutskiy  * This function is called if the journal is full enough to make a commit
3651e51764aSArtem Bityutskiy  * worthwhile, so background thread is kicked to start it.
3661e51764aSArtem Bityutskiy  */
ubifs_request_bg_commit(struct ubifs_info * c)3671e51764aSArtem Bityutskiy void ubifs_request_bg_commit(struct ubifs_info *c)
3681e51764aSArtem Bityutskiy {
3691e51764aSArtem Bityutskiy 	spin_lock(&c->cs_lock);
3701e51764aSArtem Bityutskiy 	if (c->cmt_state == COMMIT_RESTING) {
3711e51764aSArtem Bityutskiy 		dbg_cmt("old: %s, new: %s", dbg_cstate(c->cmt_state),
3721e51764aSArtem Bityutskiy 			dbg_cstate(COMMIT_BACKGROUND));
3731e51764aSArtem Bityutskiy 		c->cmt_state = COMMIT_BACKGROUND;
3741e51764aSArtem Bityutskiy 		spin_unlock(&c->cs_lock);
3751e51764aSArtem Bityutskiy 		ubifs_wake_up_bgt(c);
3761e51764aSArtem Bityutskiy 	} else
3771e51764aSArtem Bityutskiy 		spin_unlock(&c->cs_lock);
3781e51764aSArtem Bityutskiy }
3791e51764aSArtem Bityutskiy 
3801e51764aSArtem Bityutskiy /**
3811e51764aSArtem Bityutskiy  * wait_for_commit - wait for commit.
3821e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3831e51764aSArtem Bityutskiy  *
3841e51764aSArtem Bityutskiy  * This function sleeps until the commit operation is no longer running.
3851e51764aSArtem Bityutskiy  */
wait_for_commit(struct ubifs_info * c)3861e51764aSArtem Bityutskiy static int wait_for_commit(struct ubifs_info *c)
3871e51764aSArtem Bityutskiy {
3881e51764aSArtem Bityutskiy 	dbg_cmt("pid %d goes sleep", current->pid);
3891e51764aSArtem Bityutskiy 
3901e51764aSArtem Bityutskiy 	/*
3911e51764aSArtem Bityutskiy 	 * The following sleeps if the condition is false, and will be woken
3921e51764aSArtem Bityutskiy 	 * when the commit ends. It is possible, although very unlikely, that we
3931e51764aSArtem Bityutskiy 	 * will wake up and see the subsequent commit running, rather than the
3941e51764aSArtem Bityutskiy 	 * one we were waiting for, and go back to sleep.  However, we will be
3951e51764aSArtem Bityutskiy 	 * woken again, so there is no danger of sleeping forever.
3961e51764aSArtem Bityutskiy 	 */
3971e51764aSArtem Bityutskiy 	wait_event(c->cmt_wq, c->cmt_state != COMMIT_RUNNING_BACKGROUND &&
3981e51764aSArtem Bityutskiy 			      c->cmt_state != COMMIT_RUNNING_REQUIRED);
3991e51764aSArtem Bityutskiy 	dbg_cmt("commit finished, pid %d woke up", current->pid);
4001e51764aSArtem Bityutskiy 	return 0;
4011e51764aSArtem Bityutskiy }
4021e51764aSArtem Bityutskiy 
4031e51764aSArtem Bityutskiy /**
4041e51764aSArtem Bityutskiy  * ubifs_run_commit - run or wait for commit.
4051e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4061e51764aSArtem Bityutskiy  *
4071e51764aSArtem Bityutskiy  * This function runs commit and returns zero in case of success and a negative
4081e51764aSArtem Bityutskiy  * error code in case of failure.
4091e51764aSArtem Bityutskiy  */
ubifs_run_commit(struct ubifs_info * c)4101e51764aSArtem Bityutskiy int ubifs_run_commit(struct ubifs_info *c)
4111e51764aSArtem Bityutskiy {
4121e51764aSArtem Bityutskiy 	int err = 0;
4131e51764aSArtem Bityutskiy 
4141e51764aSArtem Bityutskiy 	spin_lock(&c->cs_lock);
4151e51764aSArtem Bityutskiy 	if (c->cmt_state == COMMIT_BROKEN) {
416549c999aSArtem Bityutskiy 		err = -EROFS;
4171e51764aSArtem Bityutskiy 		goto out;
4181e51764aSArtem Bityutskiy 	}
4191e51764aSArtem Bityutskiy 
4201e51764aSArtem Bityutskiy 	if (c->cmt_state == COMMIT_RUNNING_BACKGROUND)
4211e51764aSArtem Bityutskiy 		/*
4221e51764aSArtem Bityutskiy 		 * We set the commit state to 'running required' to indicate
4231e51764aSArtem Bityutskiy 		 * that we want it to complete as quickly as possible.
4241e51764aSArtem Bityutskiy 		 */
4251e51764aSArtem Bityutskiy 		c->cmt_state = COMMIT_RUNNING_REQUIRED;
4261e51764aSArtem Bityutskiy 
4271e51764aSArtem Bityutskiy 	if (c->cmt_state == COMMIT_RUNNING_REQUIRED) {
4281e51764aSArtem Bityutskiy 		spin_unlock(&c->cs_lock);
4291e51764aSArtem Bityutskiy 		return wait_for_commit(c);
4301e51764aSArtem Bityutskiy 	}
4311e51764aSArtem Bityutskiy 	spin_unlock(&c->cs_lock);
4321e51764aSArtem Bityutskiy 
4331e51764aSArtem Bityutskiy 	/* Ok, the commit is indeed needed */
4341e51764aSArtem Bityutskiy 
4351e51764aSArtem Bityutskiy 	down_write(&c->commit_sem);
4361e51764aSArtem Bityutskiy 	spin_lock(&c->cs_lock);
4371e51764aSArtem Bityutskiy 	/*
4381e51764aSArtem Bityutskiy 	 * Since we unlocked 'c->cs_lock', the state may have changed, so
4391e51764aSArtem Bityutskiy 	 * re-check it.
4401e51764aSArtem Bityutskiy 	 */
4411e51764aSArtem Bityutskiy 	if (c->cmt_state == COMMIT_BROKEN) {
442549c999aSArtem Bityutskiy 		err = -EROFS;
4431e51764aSArtem Bityutskiy 		goto out_cmt_unlock;
4441e51764aSArtem Bityutskiy 	}
4451e51764aSArtem Bityutskiy 
4461e51764aSArtem Bityutskiy 	if (c->cmt_state == COMMIT_RUNNING_BACKGROUND)
4471e51764aSArtem Bityutskiy 		c->cmt_state = COMMIT_RUNNING_REQUIRED;
4481e51764aSArtem Bityutskiy 
4491e51764aSArtem Bityutskiy 	if (c->cmt_state == COMMIT_RUNNING_REQUIRED) {
4501e51764aSArtem Bityutskiy 		up_write(&c->commit_sem);
4511e51764aSArtem Bityutskiy 		spin_unlock(&c->cs_lock);
4521e51764aSArtem Bityutskiy 		return wait_for_commit(c);
4531e51764aSArtem Bityutskiy 	}
4541e51764aSArtem Bityutskiy 	c->cmt_state = COMMIT_RUNNING_REQUIRED;
4551e51764aSArtem Bityutskiy 	spin_unlock(&c->cs_lock);
4561e51764aSArtem Bityutskiy 
4571e51764aSArtem Bityutskiy 	err = do_commit(c);
4581e51764aSArtem Bityutskiy 	return err;
4591e51764aSArtem Bityutskiy 
4601e51764aSArtem Bityutskiy out_cmt_unlock:
4611e51764aSArtem Bityutskiy 	up_write(&c->commit_sem);
4621e51764aSArtem Bityutskiy out:
4631e51764aSArtem Bityutskiy 	spin_unlock(&c->cs_lock);
4641e51764aSArtem Bityutskiy 	return err;
4651e51764aSArtem Bityutskiy }
4661e51764aSArtem Bityutskiy 
4671e51764aSArtem Bityutskiy /**
4681e51764aSArtem Bityutskiy  * ubifs_gc_should_commit - determine if it is time for GC to run commit.
4691e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4701e51764aSArtem Bityutskiy  *
4711e51764aSArtem Bityutskiy  * This function is called by garbage collection to determine if commit should
4721e51764aSArtem Bityutskiy  * be run. If commit state is @COMMIT_BACKGROUND, which means that the journal
4731e51764aSArtem Bityutskiy  * is full enough to start commit, this function returns true. It is not
4741e51764aSArtem Bityutskiy  * absolutely necessary to commit yet, but it feels like this should be better
4751e51764aSArtem Bityutskiy  * then to keep doing GC. This function returns %1 if GC has to initiate commit
4761e51764aSArtem Bityutskiy  * and %0 if not.
4771e51764aSArtem Bityutskiy  */
ubifs_gc_should_commit(struct ubifs_info * c)4781e51764aSArtem Bityutskiy int ubifs_gc_should_commit(struct ubifs_info *c)
4791e51764aSArtem Bityutskiy {
4801e51764aSArtem Bityutskiy 	int ret = 0;
4811e51764aSArtem Bityutskiy 
4821e51764aSArtem Bityutskiy 	spin_lock(&c->cs_lock);
4831e51764aSArtem Bityutskiy 	if (c->cmt_state == COMMIT_BACKGROUND) {
4841e51764aSArtem Bityutskiy 		dbg_cmt("commit required now");
4851e51764aSArtem Bityutskiy 		c->cmt_state = COMMIT_REQUIRED;
4861e51764aSArtem Bityutskiy 	} else
4871e51764aSArtem Bityutskiy 		dbg_cmt("commit not requested");
4881e51764aSArtem Bityutskiy 	if (c->cmt_state == COMMIT_REQUIRED)
4891e51764aSArtem Bityutskiy 		ret = 1;
4901e51764aSArtem Bityutskiy 	spin_unlock(&c->cs_lock);
4911e51764aSArtem Bityutskiy 	return ret;
4921e51764aSArtem Bityutskiy }
4931e51764aSArtem Bityutskiy 
494f70b7e52SArtem Bityutskiy /*
495f70b7e52SArtem Bityutskiy  * Everything below is related to debugging.
496f70b7e52SArtem Bityutskiy  */
4971e51764aSArtem Bityutskiy 
4981e51764aSArtem Bityutskiy /**
4991e51764aSArtem Bityutskiy  * struct idx_node - hold index nodes during index tree traversal.
5001e51764aSArtem Bityutskiy  * @list: list
5011e51764aSArtem Bityutskiy  * @iip: index in parent (slot number of this indexing node in the parent
5021e51764aSArtem Bityutskiy  *       indexing node)
5031e51764aSArtem Bityutskiy  * @upper_key: all keys in this indexing node have to be less or equivalent to
5041e51764aSArtem Bityutskiy  *             this key
5051e51764aSArtem Bityutskiy  * @idx: index node (8-byte aligned because all node structures must be 8-byte
5061e51764aSArtem Bityutskiy  *       aligned)
5071e51764aSArtem Bityutskiy  */
5081e51764aSArtem Bityutskiy struct idx_node {
5091e51764aSArtem Bityutskiy 	struct list_head list;
5101e51764aSArtem Bityutskiy 	int iip;
5111e51764aSArtem Bityutskiy 	union ubifs_key upper_key;
51243457c60SArtem Bityutskiy 	struct ubifs_idx_node idx __aligned(8);
5131e51764aSArtem Bityutskiy };
5141e51764aSArtem Bityutskiy 
5151e51764aSArtem Bityutskiy /**
5161e51764aSArtem Bityutskiy  * dbg_old_index_check_init - get information for the next old index check.
5171e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
5181e51764aSArtem Bityutskiy  * @zroot: root of the index
5191e51764aSArtem Bityutskiy  *
5201e51764aSArtem Bityutskiy  * This function records information about the index that will be needed for the
5211e51764aSArtem Bityutskiy  * next old index check i.e. 'dbg_check_old_index()'.
5221e51764aSArtem Bityutskiy  *
5231e51764aSArtem Bityutskiy  * This function returns %0 on success and a negative error code on failure.
5241e51764aSArtem Bityutskiy  */
dbg_old_index_check_init(struct ubifs_info * c,struct ubifs_zbranch * zroot)5251e51764aSArtem Bityutskiy int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot)
5261e51764aSArtem Bityutskiy {
5271e51764aSArtem Bityutskiy 	struct ubifs_idx_node *idx;
5281e51764aSArtem Bityutskiy 	int lnum, offs, len, err = 0;
52917c2f9f8SArtem Bityutskiy 	struct ubifs_debug_info *d = c->dbg;
5301e51764aSArtem Bityutskiy 
53117c2f9f8SArtem Bityutskiy 	d->old_zroot = *zroot;
53217c2f9f8SArtem Bityutskiy 	lnum = d->old_zroot.lnum;
53317c2f9f8SArtem Bityutskiy 	offs = d->old_zroot.offs;
53417c2f9f8SArtem Bityutskiy 	len = d->old_zroot.len;
5351e51764aSArtem Bityutskiy 
5361e51764aSArtem Bityutskiy 	idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
5371e51764aSArtem Bityutskiy 	if (!idx)
5381e51764aSArtem Bityutskiy 		return -ENOMEM;
5391e51764aSArtem Bityutskiy 
5401e51764aSArtem Bityutskiy 	err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
5411e51764aSArtem Bityutskiy 	if (err)
5421e51764aSArtem Bityutskiy 		goto out;
5431e51764aSArtem Bityutskiy 
54417c2f9f8SArtem Bityutskiy 	d->old_zroot_level = le16_to_cpu(idx->level);
54517c2f9f8SArtem Bityutskiy 	d->old_zroot_sqnum = le64_to_cpu(idx->ch.sqnum);
5461e51764aSArtem Bityutskiy out:
5471e51764aSArtem Bityutskiy 	kfree(idx);
5481e51764aSArtem Bityutskiy 	return err;
5491e51764aSArtem Bityutskiy }
5501e51764aSArtem Bityutskiy 
5511e51764aSArtem Bityutskiy /**
5521e51764aSArtem Bityutskiy  * dbg_check_old_index - check the old copy of the index.
5531e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
5541e51764aSArtem Bityutskiy  * @zroot: root of the new index
5551e51764aSArtem Bityutskiy  *
5561e51764aSArtem Bityutskiy  * In order to be able to recover from an unclean unmount, a complete copy of
5571e51764aSArtem Bityutskiy  * the index must exist on flash. This is the "old" index. The commit process
5581e51764aSArtem Bityutskiy  * must write the "new" index to flash without overwriting or destroying any
5591e51764aSArtem Bityutskiy  * part of the old index. This function is run at commit end in order to check
5601e51764aSArtem Bityutskiy  * that the old index does indeed exist completely intact.
5611e51764aSArtem Bityutskiy  *
5621e51764aSArtem Bityutskiy  * This function returns %0 on success and a negative error code on failure.
5631e51764aSArtem Bityutskiy  */
dbg_check_old_index(struct ubifs_info * c,struct ubifs_zbranch * zroot)5641e51764aSArtem Bityutskiy int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot)
5651e51764aSArtem Bityutskiy {
5663f649ab7SKees Cook 	int lnum, offs, len, err = 0, last_level, child_cnt;
5671e51764aSArtem Bityutskiy 	int first = 1, iip;
56817c2f9f8SArtem Bityutskiy 	struct ubifs_debug_info *d = c->dbg;
5693f649ab7SKees Cook 	union ubifs_key lower_key, upper_key, l_key, u_key;
5703f649ab7SKees Cook 	unsigned long long last_sqnum;
5711e51764aSArtem Bityutskiy 	struct ubifs_idx_node *idx;
5721e51764aSArtem Bityutskiy 	struct list_head list;
5731e51764aSArtem Bityutskiy 	struct idx_node *i;
5741e51764aSArtem Bityutskiy 	size_t sz;
5751e51764aSArtem Bityutskiy 
5768d7819b4SArtem Bityutskiy 	if (!dbg_is_chk_index(c))
5778b229c76SArtem Bityutskiy 		return 0;
5781e51764aSArtem Bityutskiy 
5791e51764aSArtem Bityutskiy 	INIT_LIST_HEAD(&list);
5801e51764aSArtem Bityutskiy 
5811e51764aSArtem Bityutskiy 	sz = sizeof(struct idx_node) + ubifs_idx_node_sz(c, c->fanout) -
5821e51764aSArtem Bityutskiy 	     UBIFS_IDX_NODE_SZ;
5831e51764aSArtem Bityutskiy 
5841e51764aSArtem Bityutskiy 	/* Start at the old zroot */
58517c2f9f8SArtem Bityutskiy 	lnum = d->old_zroot.lnum;
58617c2f9f8SArtem Bityutskiy 	offs = d->old_zroot.offs;
58717c2f9f8SArtem Bityutskiy 	len = d->old_zroot.len;
5881e51764aSArtem Bityutskiy 	iip = 0;
5891e51764aSArtem Bityutskiy 
5901e51764aSArtem Bityutskiy 	/*
5911e51764aSArtem Bityutskiy 	 * Traverse the index tree preorder depth-first i.e. do a node and then
5921e51764aSArtem Bityutskiy 	 * its subtrees from left to right.
5931e51764aSArtem Bityutskiy 	 */
5941e51764aSArtem Bityutskiy 	while (1) {
5951e51764aSArtem Bityutskiy 		struct ubifs_branch *br;
5961e51764aSArtem Bityutskiy 
5971e51764aSArtem Bityutskiy 		/* Get the next index node */
5981e51764aSArtem Bityutskiy 		i = kmalloc(sz, GFP_NOFS);
5991e51764aSArtem Bityutskiy 		if (!i) {
6001e51764aSArtem Bityutskiy 			err = -ENOMEM;
6011e51764aSArtem Bityutskiy 			goto out_free;
6021e51764aSArtem Bityutskiy 		}
6031e51764aSArtem Bityutskiy 		i->iip = iip;
6041e51764aSArtem Bityutskiy 		/* Keep the index nodes on our path in a linked list */
6051e51764aSArtem Bityutskiy 		list_add_tail(&i->list, &list);
6061e51764aSArtem Bityutskiy 		/* Read the index node */
6071e51764aSArtem Bityutskiy 		idx = &i->idx;
6081e51764aSArtem Bityutskiy 		err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
6091e51764aSArtem Bityutskiy 		if (err)
6101e51764aSArtem Bityutskiy 			goto out_free;
6111e51764aSArtem Bityutskiy 		/* Validate index node */
6121e51764aSArtem Bityutskiy 		child_cnt = le16_to_cpu(idx->child_cnt);
6131e51764aSArtem Bityutskiy 		if (child_cnt < 1 || child_cnt > c->fanout) {
6141e51764aSArtem Bityutskiy 			err = 1;
6151e51764aSArtem Bityutskiy 			goto out_dump;
6161e51764aSArtem Bityutskiy 		}
6171e51764aSArtem Bityutskiy 		if (first) {
6181e51764aSArtem Bityutskiy 			first = 0;
6191e51764aSArtem Bityutskiy 			/* Check root level and sqnum */
62017c2f9f8SArtem Bityutskiy 			if (le16_to_cpu(idx->level) != d->old_zroot_level) {
6211e51764aSArtem Bityutskiy 				err = 2;
6221e51764aSArtem Bityutskiy 				goto out_dump;
6231e51764aSArtem Bityutskiy 			}
62417c2f9f8SArtem Bityutskiy 			if (le64_to_cpu(idx->ch.sqnum) != d->old_zroot_sqnum) {
6251e51764aSArtem Bityutskiy 				err = 3;
6261e51764aSArtem Bityutskiy 				goto out_dump;
6271e51764aSArtem Bityutskiy 			}
6281e51764aSArtem Bityutskiy 			/* Set last values as though root had a parent */
6291e51764aSArtem Bityutskiy 			last_level = le16_to_cpu(idx->level) + 1;
6301e51764aSArtem Bityutskiy 			last_sqnum = le64_to_cpu(idx->ch.sqnum) + 1;
6311e51764aSArtem Bityutskiy 			key_read(c, ubifs_idx_key(c, idx), &lower_key);
6321e51764aSArtem Bityutskiy 			highest_ino_key(c, &upper_key, INUM_WATERMARK);
6331e51764aSArtem Bityutskiy 		}
6341e51764aSArtem Bityutskiy 		key_copy(c, &upper_key, &i->upper_key);
6351e51764aSArtem Bityutskiy 		if (le16_to_cpu(idx->level) != last_level - 1) {
6361e51764aSArtem Bityutskiy 			err = 3;
6371e51764aSArtem Bityutskiy 			goto out_dump;
6381e51764aSArtem Bityutskiy 		}
6391e51764aSArtem Bityutskiy 		/*
6401e51764aSArtem Bityutskiy 		 * The index is always written bottom up hence a child's sqnum
6411e51764aSArtem Bityutskiy 		 * is always less than the parents.
6421e51764aSArtem Bityutskiy 		 */
6431e51764aSArtem Bityutskiy 		if (le64_to_cpu(idx->ch.sqnum) >= last_sqnum) {
6441e51764aSArtem Bityutskiy 			err = 4;
6451e51764aSArtem Bityutskiy 			goto out_dump;
6461e51764aSArtem Bityutskiy 		}
6471e51764aSArtem Bityutskiy 		/* Check key range */
6481e51764aSArtem Bityutskiy 		key_read(c, ubifs_idx_key(c, idx), &l_key);
6491e51764aSArtem Bityutskiy 		br = ubifs_idx_branch(c, idx, child_cnt - 1);
6501e51764aSArtem Bityutskiy 		key_read(c, &br->key, &u_key);
6511e51764aSArtem Bityutskiy 		if (keys_cmp(c, &lower_key, &l_key) > 0) {
6521e51764aSArtem Bityutskiy 			err = 5;
6531e51764aSArtem Bityutskiy 			goto out_dump;
6541e51764aSArtem Bityutskiy 		}
6551e51764aSArtem Bityutskiy 		if (keys_cmp(c, &upper_key, &u_key) < 0) {
6561e51764aSArtem Bityutskiy 			err = 6;
6571e51764aSArtem Bityutskiy 			goto out_dump;
6581e51764aSArtem Bityutskiy 		}
6591e51764aSArtem Bityutskiy 		if (keys_cmp(c, &upper_key, &u_key) == 0)
6601e51764aSArtem Bityutskiy 			if (!is_hash_key(c, &u_key)) {
6611e51764aSArtem Bityutskiy 				err = 7;
6621e51764aSArtem Bityutskiy 				goto out_dump;
6631e51764aSArtem Bityutskiy 			}
6641e51764aSArtem Bityutskiy 		/* Go to next index node */
6651e51764aSArtem Bityutskiy 		if (le16_to_cpu(idx->level) == 0) {
6661e51764aSArtem Bityutskiy 			/* At the bottom, so go up until can go right */
6671e51764aSArtem Bityutskiy 			while (1) {
6681e51764aSArtem Bityutskiy 				/* Drop the bottom of the list */
6691e51764aSArtem Bityutskiy 				list_del(&i->list);
6701e51764aSArtem Bityutskiy 				kfree(i);
6711e51764aSArtem Bityutskiy 				/* No more list means we are done */
6721e51764aSArtem Bityutskiy 				if (list_empty(&list))
6731e51764aSArtem Bityutskiy 					goto out;
6741e51764aSArtem Bityutskiy 				/* Look at the new bottom */
6751e51764aSArtem Bityutskiy 				i = list_entry(list.prev, struct idx_node,
6761e51764aSArtem Bityutskiy 					       list);
6771e51764aSArtem Bityutskiy 				idx = &i->idx;
6781e51764aSArtem Bityutskiy 				/* Can we go right */
6791e51764aSArtem Bityutskiy 				if (iip + 1 < le16_to_cpu(idx->child_cnt)) {
6801e51764aSArtem Bityutskiy 					iip = iip + 1;
6811e51764aSArtem Bityutskiy 					break;
6821e51764aSArtem Bityutskiy 				} else
6831e51764aSArtem Bityutskiy 					/* Nope, so go up again */
6841e51764aSArtem Bityutskiy 					iip = i->iip;
6851e51764aSArtem Bityutskiy 			}
6861e51764aSArtem Bityutskiy 		} else
6871e51764aSArtem Bityutskiy 			/* Go down left */
6881e51764aSArtem Bityutskiy 			iip = 0;
6891e51764aSArtem Bityutskiy 		/*
6901e51764aSArtem Bityutskiy 		 * We have the parent in 'idx' and now we set up for reading the
6911e51764aSArtem Bityutskiy 		 * child pointed to by slot 'iip'.
6921e51764aSArtem Bityutskiy 		 */
6931e51764aSArtem Bityutskiy 		last_level = le16_to_cpu(idx->level);
6941e51764aSArtem Bityutskiy 		last_sqnum = le64_to_cpu(idx->ch.sqnum);
6951e51764aSArtem Bityutskiy 		br = ubifs_idx_branch(c, idx, iip);
6961e51764aSArtem Bityutskiy 		lnum = le32_to_cpu(br->lnum);
6971e51764aSArtem Bityutskiy 		offs = le32_to_cpu(br->offs);
6981e51764aSArtem Bityutskiy 		len = le32_to_cpu(br->len);
6991e51764aSArtem Bityutskiy 		key_read(c, &br->key, &lower_key);
7001e51764aSArtem Bityutskiy 		if (iip + 1 < le16_to_cpu(idx->child_cnt)) {
7011e51764aSArtem Bityutskiy 			br = ubifs_idx_branch(c, idx, iip + 1);
7021e51764aSArtem Bityutskiy 			key_read(c, &br->key, &upper_key);
7031e51764aSArtem Bityutskiy 		} else
7041e51764aSArtem Bityutskiy 			key_copy(c, &i->upper_key, &upper_key);
7051e51764aSArtem Bityutskiy 	}
7061e51764aSArtem Bityutskiy out:
7071e51764aSArtem Bityutskiy 	err = dbg_old_index_check_init(c, zroot);
7081e51764aSArtem Bityutskiy 	if (err)
7091e51764aSArtem Bityutskiy 		goto out_free;
7101e51764aSArtem Bityutskiy 
7111e51764aSArtem Bityutskiy 	return 0;
7121e51764aSArtem Bityutskiy 
7131e51764aSArtem Bityutskiy out_dump:
714235c362bSSheng Yong 	ubifs_err(c, "dumping index node (iip=%d)", i->iip);
715a33e30a0SZhihao Cheng 	ubifs_dump_node(c, idx, ubifs_idx_node_sz(c, c->fanout));
7161e51764aSArtem Bityutskiy 	list_del(&i->list);
7171e51764aSArtem Bityutskiy 	kfree(i);
7181e51764aSArtem Bityutskiy 	if (!list_empty(&list)) {
7191e51764aSArtem Bityutskiy 		i = list_entry(list.prev, struct idx_node, list);
720235c362bSSheng Yong 		ubifs_err(c, "dumping parent index node");
721a33e30a0SZhihao Cheng 		ubifs_dump_node(c, &i->idx, ubifs_idx_node_sz(c, c->fanout));
7221e51764aSArtem Bityutskiy 	}
7231e51764aSArtem Bityutskiy out_free:
7241e51764aSArtem Bityutskiy 	while (!list_empty(&list)) {
7251e51764aSArtem Bityutskiy 		i = list_entry(list.next, struct idx_node, list);
7261e51764aSArtem Bityutskiy 		list_del(&i->list);
7271e51764aSArtem Bityutskiy 		kfree(i);
7281e51764aSArtem Bityutskiy 	}
729235c362bSSheng Yong 	ubifs_err(c, "failed, error %d", err);
7301e51764aSArtem Bityutskiy 	if (err > 0)
7311e51764aSArtem Bityutskiy 		err = -EINVAL;
7321e51764aSArtem Bityutskiy 	return err;
7331e51764aSArtem Bityutskiy }
734