^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: Artem Bityutskiy (Битюцкий Артём)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Adrian Hunter
^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 is a part of UBIFS journal implementation and contains various
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * functions which manipulate the log. The log is a fixed area on the flash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * which does not contain any data but refers to buds. The log is a part of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * journal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "ubifs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int dbg_check_bud_bytes(struct ubifs_info *c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * ubifs_search_bud - search bud LEB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * @lnum: logical eraseblock number to search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * This function searches bud LEB @lnum. Returns bud description object in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * of success and %NULL if there is no bud with this LEB number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct rb_node *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct ubifs_bud *bud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) spin_lock(&c->buds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) p = c->buds.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) while (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) bud = rb_entry(p, struct ubifs_bud, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (lnum < bud->lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) p = p->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) else if (lnum > bud->lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) p = p->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) spin_unlock(&c->buds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return bud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) spin_unlock(&c->buds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * ubifs_get_wbuf - get the wbuf associated with a LEB, if there is one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * @lnum: logical eraseblock number to search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * This functions returns the wbuf for @lnum or %NULL if there is not one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct rb_node *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct ubifs_bud *bud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int jhead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (!c->jheads)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) spin_lock(&c->buds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) p = c->buds.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) while (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) bud = rb_entry(p, struct ubifs_bud, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (lnum < bud->lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) p = p->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) else if (lnum > bud->lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) p = p->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) jhead = bud->jhead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) spin_unlock(&c->buds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return &c->jheads[jhead].wbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) spin_unlock(&c->buds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * empty_log_bytes - calculate amount of empty space in the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) static inline long long empty_log_bytes(const struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) long long h, t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) h = (long long)c->lhead_lnum * c->leb_size + c->lhead_offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) t = (long long)c->ltail_lnum * c->leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (h > t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return c->log_bytes - h + t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) else if (h != t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return t - h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) else if (c->lhead_lnum != c->ltail_lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return c->log_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * ubifs_add_bud - add bud LEB to the tree of buds and its journal head list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * @bud: the bud to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct rb_node **p, *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct ubifs_bud *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) struct ubifs_jhead *jhead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) spin_lock(&c->buds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) p = &c->buds.rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) b = rb_entry(parent, struct ubifs_bud, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) ubifs_assert(c, bud->lnum != b->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) if (bud->lnum < b->lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) rb_link_node(&bud->rb, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) rb_insert_color(&bud->rb, &c->buds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (c->jheads) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) jhead = &c->jheads[bud->jhead];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) list_add_tail(&bud->list, &jhead->buds_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) ubifs_assert(c, c->replaying && c->ro_mount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * Note, although this is a new bud, we anyway account this space now,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * before any data has been written to it, because this is about to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * guarantee fixed mount time, and this bud will anyway be read and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * scanned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) c->bud_bytes += c->leb_size - bud->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) dbg_log("LEB %d:%d, jhead %s, bud_bytes %lld", bud->lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) bud->start, dbg_jhead(bud->jhead), c->bud_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) spin_unlock(&c->buds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * ubifs_add_bud_to_log - add a new bud to the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * @jhead: journal head the bud belongs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * @lnum: LEB number of the bud
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * @offs: starting offset of the bud
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * This function writes a reference node for the new bud LEB @lnum to the log,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * and adds it to the buds trees. It also makes sure that log size does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * exceed the 'c->max_bud_bytes' limit. Returns zero in case of success,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * %-EAGAIN if commit is required, and a negative error code in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct ubifs_bud *bud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct ubifs_ref_node *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) bud = kmalloc(sizeof(struct ubifs_bud), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!bud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) ref = kzalloc(c->ref_node_alsz, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (!ref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) kfree(bud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) mutex_lock(&c->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ubifs_assert(c, !c->ro_media && !c->ro_mount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (c->ro_error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) err = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /* Make sure we have enough space in the log */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (empty_log_bytes(c) - c->ref_node_alsz < c->min_log_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) dbg_log("not enough log space - %lld, required %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) empty_log_bytes(c), c->min_log_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) ubifs_commit_required(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) * Make sure the amount of space in buds will not exceed the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * 'c->max_bud_bytes' limit, because we want to guarantee mount time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * limits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * It is not necessary to hold @c->buds_lock when reading @c->bud_bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * because we are holding @c->log_mutex. All @c->bud_bytes take place
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * when both @c->log_mutex and @c->bud_bytes are locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (c->bud_bytes + c->leb_size - offs > c->max_bud_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) dbg_log("bud bytes %lld (%lld max), require commit",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) c->bud_bytes, c->max_bud_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) ubifs_commit_required(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * If the journal is full enough - start background commit. Note, it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * OK to read 'c->cmt_state' without spinlock because integer reads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * are atomic in the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (c->bud_bytes >= c->bg_bud_bytes &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) c->cmt_state == COMMIT_RESTING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) dbg_log("bud bytes %lld (%lld max), initiate BG commit",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) c->bud_bytes, c->max_bud_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) ubifs_request_bg_commit(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) bud->lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) bud->start = offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) bud->jhead = jhead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) bud->log_hash = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ref->ch.node_type = UBIFS_REF_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) ref->lnum = cpu_to_le32(bud->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) ref->offs = cpu_to_le32(bud->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) ref->jhead = cpu_to_le32(jhead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (c->lhead_offs > c->leb_size - c->ref_node_alsz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) ubifs_assert(c, c->lhead_lnum != c->ltail_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) c->lhead_offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (c->lhead_offs == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /* Must ensure next log LEB has been unmapped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) err = ubifs_leb_unmap(c, c->lhead_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (bud->start == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * Before writing the LEB reference which refers an empty LEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * to the log, we have to make sure it is mapped, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * otherwise we'd risk to refer an LEB with garbage in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * an unclean reboot, because the target LEB might have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * unmapped, but not yet physically erased.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) err = ubifs_leb_map(c, bud->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) dbg_log("write ref LEB %d:%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) c->lhead_lnum, c->lhead_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) c->lhead_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) err = ubifs_shash_update(c, c->log_hash, ref, UBIFS_REF_NODE_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) err = ubifs_shash_copy_state(c, c->log_hash, c->jheads[jhead].log_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) c->lhead_offs += c->ref_node_alsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) ubifs_add_bud(c, bud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) mutex_unlock(&c->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) kfree(ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) mutex_unlock(&c->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) kfree(ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) kfree(bud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * remove_buds - remove used buds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * This function removes use buds from the buds tree. It does not remove the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * buds which are pointed to by journal heads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static void remove_buds(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct rb_node *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) ubifs_assert(c, list_empty(&c->old_buds));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) c->cmt_bud_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) spin_lock(&c->buds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) p = rb_first(&c->buds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) while (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct rb_node *p1 = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct ubifs_bud *bud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct ubifs_wbuf *wbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) p = rb_next(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) bud = rb_entry(p1, struct ubifs_bud, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) wbuf = &c->jheads[bud->jhead].wbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (wbuf->lnum == bud->lnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * Do not remove buds which are pointed to by journal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * heads (non-closed buds).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) c->cmt_bud_bytes += wbuf->offs - bud->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) dbg_log("preserve %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) bud->lnum, bud->start, dbg_jhead(bud->jhead),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) wbuf->offs - bud->start, c->cmt_bud_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) bud->start = wbuf->offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) c->cmt_bud_bytes += c->leb_size - bud->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) dbg_log("remove %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) bud->lnum, bud->start, dbg_jhead(bud->jhead),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) c->leb_size - bud->start, c->cmt_bud_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) rb_erase(p1, &c->buds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) * If the commit does not finish, the recovery will need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) * to replay the journal, in which case the old buds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) * must be unchanged. Do not release them until post
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) * commit i.e. do not allow them to be garbage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) * collected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) list_move(&bud->list, &c->old_buds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) spin_unlock(&c->buds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * ubifs_log_start_commit - start commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * @ltail_lnum: return new log tail LEB number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * The commit operation starts with writing "commit start" node to the log and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * reference nodes for all journal heads which will define new journal after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * the commit has been finished. The commit start and reference nodes are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * written in one go to the nearest empty log LEB (hence, when commit is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * finished UBIFS may safely unmap all the previous log LEBs). This function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * returns zero in case of success and a negative error code in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) struct ubifs_cs_node *cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) struct ubifs_ref_node *ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) int err, i, max_len, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) err = dbg_check_bud_bytes(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) max_len = ALIGN(max_len, c->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) buf = cs = kmalloc(max_len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) cs->ch.node_type = UBIFS_CS_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) cs->cmt_no = cpu_to_le64(c->cmt_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) err = ubifs_shash_init(c, c->log_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) err = ubifs_shash_update(c, c->log_hash, cs, UBIFS_CS_NODE_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * Note, we do not lock 'c->log_mutex' because this is the commit start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * phase and we are exclusively using the log. And we do not lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * write-buffer because nobody can write to the file-system at this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * phase.
^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) len = UBIFS_CS_NODE_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) for (i = 0; i < c->jhead_cnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) int lnum = c->jheads[i].wbuf.lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) int offs = c->jheads[i].wbuf.offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (lnum == -1 || offs == c->leb_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) dbg_log("add ref to LEB %d:%d for jhead %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) lnum, offs, dbg_jhead(i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) ref = buf + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) ref->ch.node_type = UBIFS_REF_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) ref->lnum = cpu_to_le32(lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) ref->offs = cpu_to_le32(offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) ref->jhead = cpu_to_le32(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) len += UBIFS_REF_NODE_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) err = ubifs_shash_update(c, c->log_hash, ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) UBIFS_REF_NODE_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) ubifs_shash_copy_state(c, c->log_hash, c->jheads[i].log_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) /* Switch to the next log LEB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (c->lhead_offs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) ubifs_assert(c, c->lhead_lnum != c->ltail_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) c->lhead_offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) /* Must ensure next LEB has been unmapped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) err = ubifs_leb_unmap(c, c->lhead_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) len = ALIGN(len, c->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) *ltail_lnum = c->lhead_lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) c->lhead_offs += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) ubifs_assert(c, c->lhead_offs < c->leb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) remove_buds(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * We have started the commit and now users may use the rest of the log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * for new writes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) c->min_log_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) kfree(buf);
^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_log_end_commit - end 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) * @ltail_lnum: new log tail LEB number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * This function is called on when the commit operation was finished. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * moves log tail to new position and updates the master node so that it stores
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * the new log tail LEB number. Returns zero in case of success and a negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * writes during commit. Its only short "commit" start phase when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * writers are blocked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) mutex_lock(&c->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) c->ltail_lnum, ltail_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) c->ltail_lnum = ltail_lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * The commit is finished and from now on it must be guaranteed that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * there is always enough space for the next commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) c->min_log_bytes = c->leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) spin_lock(&c->buds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) c->bud_bytes -= c->cmt_bud_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) spin_unlock(&c->buds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) err = dbg_check_bud_bytes(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) err = ubifs_write_master(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) mutex_unlock(&c->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * ubifs_log_post_commit - things to do after commit is completed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * @old_ltail_lnum: old log tail LEB number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * Release buds only after commit is completed, because they must be unchanged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * if recovery is needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * Unmap log LEBs only after commit is completed, because they may be needed for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * recovery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) * This function returns %0 on success and a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) int lnum, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) while (!list_empty(&c->old_buds)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) struct ubifs_bud *bud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) err = ubifs_return_leb(c, bud->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) list_del(&bud->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) kfree(bud->log_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) kfree(bud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) mutex_lock(&c->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) lnum = ubifs_next_log_lnum(c, lnum)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) dbg_log("unmap log LEB %d", lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) err = ubifs_leb_unmap(c, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) mutex_unlock(&c->log_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * struct done_ref - references that have been done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * @rb: rb-tree node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * @lnum: LEB number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) struct done_ref {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) struct rb_node rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) int lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) * done_already - determine if a reference has been done already.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) * @done_tree: rb-tree to store references that have been done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) * @lnum: LEB number of reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * This function returns %1 if the reference has been done, %0 if not, otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * a negative error code is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) static int done_already(struct rb_root *done_tree, int lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) struct rb_node **p = &done_tree->rb_node, *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) struct done_ref *dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) parent = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) dr = rb_entry(parent, struct done_ref, rb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (lnum < dr->lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) p = &(*p)->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) else if (lnum > dr->lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) p = &(*p)->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (!dr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) dr->lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) rb_link_node(&dr->rb, parent, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) rb_insert_color(&dr->rb, done_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) * destroy_done_tree - destroy the done tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * @done_tree: done tree to destroy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) static void destroy_done_tree(struct rb_root *done_tree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) struct done_ref *dr, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) rbtree_postorder_for_each_entry_safe(dr, n, done_tree, rb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) kfree(dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) * add_node - add a node to the consolidated log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) * @buf: buffer to which to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) * @lnum: LEB number to which to write is passed and returned here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) * @offs: offset to where to write is passed and returned here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) * @node: node to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) * This function returns %0 on success and a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) void *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) struct ubifs_ch *ch = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (len > remains) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) int sz = ALIGN(*offs, c->min_io_size), err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) ubifs_pad(c, buf + *offs, sz - *offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) err = ubifs_leb_change(c, *lnum, buf, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) *lnum = ubifs_next_log_lnum(c, *lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) *offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) memcpy(buf + *offs, node, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) *offs += ALIGN(len, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) * ubifs_consolidate_log - consolidate the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * Repeated failed commits could cause the log to be full, but at least 1 LEB is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) * needed for commit. This function rewrites the reference nodes in the log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * omitting duplicates, and failed CS nodes, and leaving no gaps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) * This function returns %0 on success and a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) int ubifs_consolidate_log(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) struct ubifs_scan_leb *sleb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) struct ubifs_scan_node *snod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) struct rb_root done_tree = RB_ROOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) int lnum, err, first = 1, write_lnum, offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) void *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) c->lhead_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) buf = vmalloc(c->leb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) lnum = c->ltail_lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) write_lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (IS_ERR(sleb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) err = PTR_ERR(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) list_for_each_entry(snod, &sleb->nodes, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) switch (snod->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) case UBIFS_REF_NODE: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) struct ubifs_ref_node *ref = snod->node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) int ref_lnum = le32_to_cpu(ref->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) err = done_already(&done_tree, ref_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) goto out_scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) if (err != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) err = add_node(c, buf, &write_lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) &offs, snod->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) goto out_scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) case UBIFS_CS_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) if (!first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) err = add_node(c, buf, &write_lnum, &offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) snod->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) goto out_scan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) first = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) ubifs_scan_destroy(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) if (lnum == c->lhead_lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) lnum = ubifs_next_log_lnum(c, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) if (offs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) int sz = ALIGN(offs, c->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) ubifs_pad(c, buf + offs, sz - offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) err = ubifs_leb_change(c, write_lnum, buf, sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) offs = ALIGN(offs, c->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) destroy_done_tree(&done_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) vfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (write_lnum == c->lhead_lnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) ubifs_err(c, "log is too full");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) /* Unmap remaining LEBs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) lnum = write_lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) lnum = ubifs_next_log_lnum(c, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) err = ubifs_leb_unmap(c, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) } while (lnum != c->lhead_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) c->lhead_lnum = write_lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) c->lhead_offs = offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) out_scan:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) ubifs_scan_destroy(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) destroy_done_tree(&done_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) vfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) * This function makes sure the amount of flash space used by closed buds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) static int dbg_check_bud_bytes(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) int i, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) struct ubifs_bud *bud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) long long bud_bytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) if (!dbg_is_chk_gen(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) spin_lock(&c->buds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) for (i = 0; i < c->jhead_cnt; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) list_for_each_entry(bud, &c->jheads[i].buds_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) bud_bytes += c->leb_size - bud->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) if (c->bud_bytes != bud_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) ubifs_err(c, "bad bud_bytes %lld, calculated %lld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) c->bud_bytes, bud_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) spin_unlock(&c->buds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) }