Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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