^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 contains journal replay code. It runs when the file-system is being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * mounted and requires no locking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * The larger is the journal, the longer it takes to scan it, so the longer it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * takes to mount UBIFS. This is why the journal has limited size which may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * changed depending on the system requirements. But a larger journal gives
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * faster I/O speed because it writes the index less frequently. So this is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * larger is the journal, the more memory its index may consume.
^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) #include "ubifs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/list_sort.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <crypto/algapi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * struct replay_entry - replay list entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * @lnum: logical eraseblock number of the node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * @offs: node offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * @len: node length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * @deletion: non-zero if this entry corresponds to a node deletion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * @sqnum: node sequence number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * @list: links the replay list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @key: node key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * @nm: directory entry name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * @old_size: truncation old size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * @new_size: truncation new size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * The replay process first scans all buds and builds the replay list, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * sorts the replay list in nodes sequence number order, and then inserts all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * the replay entries to the TNC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct replay_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) u8 hash[UBIFS_HASH_ARR_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned int deletion:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned long long sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) union ubifs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct fscrypt_name nm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) loff_t old_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) loff_t new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * struct bud_entry - entry in the list of buds to replay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * @list: next bud in the list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * @bud: bud description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * @sqnum: reference node sequence number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * @free: free bytes in the bud
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * @dirty: dirty bytes in the bud
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct bud_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct ubifs_bud *bud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) unsigned long long sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * set_bud_lprops - set free and dirty space used by a bud.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * @b: bud entry which describes the bud
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * This function makes sure the LEB properties of bud @b are set correctly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * after the replay. Returns zero in case of success and a negative error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) const struct ubifs_lprops *lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int err = 0, dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ubifs_get_lprops(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (IS_ERR(lp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) err = PTR_ERR(lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) dirty = lp->dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * The LEB was added to the journal with a starting offset of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * zero which means the LEB must have been empty. The LEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * property values should be @lp->free == @c->leb_size and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * @lp->dirty == 0, but that is not the case. The reason is that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * the LEB had been garbage collected before it became the bud,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * and there was not commit inbetween. The garbage collector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * resets the free and dirty space without recording it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * anywhere except lprops, so if there was no commit then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * lprops does not have that information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * We do not need to adjust free space because the scan has told
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * us the exact value which is recorded in the replay entry as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * @b->free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * However we do need to subtract from the dirty space the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * amount of space that the garbage collector reclaimed, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * is the whole LEB minus the amount of space that was free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) lp->free, lp->dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) lp->free, lp->dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) dirty -= c->leb_size - lp->free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * If the replay order was perfect the dirty space would now be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * zero. The order is not perfect because the journal heads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * race with each other. This is not a problem but is does mean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * that the dirty space may temporarily exceed c->leb_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * during the replay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (dirty != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) b->bud->lnum, lp->free, lp->dirty, b->free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) b->dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) lp->flags | LPROPS_TAKEN, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (IS_ERR(lp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) err = PTR_ERR(lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* Make sure the journal head points to the latest bud */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) b->bud->lnum, c->leb_size - b->free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ubifs_release_lprops(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * set_buds_lprops - set free and dirty space for all replayed buds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * This function sets LEB properties for all replayed buds. Returns zero in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * case of success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static int set_buds_lprops(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct bud_entry *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) list_for_each_entry(b, &c->replay_buds, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) err = set_bud_lprops(c, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * trun_remove_range - apply a replay entry for a truncation to the TNC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * @r: replay entry of truncation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) unsigned min_blk, max_blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) union ubifs_key min_key, max_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) ino_t ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) min_blk = r->new_size / UBIFS_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) min_blk += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) max_blk = r->old_size / UBIFS_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) max_blk -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) ino = key_inum(c, &r->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) data_key_init(c, &min_key, ino, min_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) data_key_init(c, &max_key, ino, max_blk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return ubifs_tnc_remove_range(c, &min_key, &max_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * inode_still_linked - check whether inode in question will be re-linked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * @rino: replay entry to test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * O_TMPFILE files can be re-linked, this means link count goes from 0 to 1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * This case needs special care, otherwise all references to the inode will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * be removed upon the first replay entry of an inode with link count 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * is found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static bool inode_still_linked(struct ubifs_info *c, struct replay_entry *rino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct replay_entry *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) ubifs_assert(c, rino->deletion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ubifs_assert(c, key_type(c, &rino->key) == UBIFS_INO_KEY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * Find the most recent entry for the inode behind @rino and check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * whether it is a deletion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) list_for_each_entry_reverse(r, &c->replay_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) ubifs_assert(c, r->sqnum >= rino->sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (key_inum(c, &r->key) == key_inum(c, &rino->key) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) key_type(c, &r->key) == UBIFS_INO_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return r->deletion == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) ubifs_assert(c, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * apply_replay_entry - apply a replay entry to the TNC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * @r: replay entry to apply
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * Apply a replay entry to the TNC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) r->lnum, r->offs, r->len, r->deletion, r->sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (is_hash_key(c, &r->key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (r->deletion)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) r->len, r->hash, &r->nm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (r->deletion)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) switch (key_type(c, &r->key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) case UBIFS_INO_KEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) ino_t inum = key_inum(c, &r->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (inode_still_linked(c, r)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) err = ubifs_tnc_remove_ino(c, inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) case UBIFS_TRUN_KEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) err = trun_remove_range(c, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) err = ubifs_tnc_remove(c, &r->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) r->len, r->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (c->need_recovery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) err = ubifs_recover_size_accum(c, &r->key, r->deletion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) r->new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * replay_entries_cmp - compare 2 replay entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * @priv: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * @a: first replay entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * @b: second replay entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * This is a comparios function for 'list_sort()' which compares 2 replay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * entries @a and @b by comparing their sequence numer. Returns %1 if @a has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * greater sequence number and %-1 otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static int replay_entries_cmp(void *priv, struct list_head *a,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct list_head *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct ubifs_info *c = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct replay_entry *ra, *rb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (a == b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) ra = list_entry(a, struct replay_entry, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) rb = list_entry(b, struct replay_entry, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ubifs_assert(c, ra->sqnum != rb->sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (ra->sqnum > rb->sqnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return -1;
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * apply_replay_list - apply the replay list to the TNC.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * Apply all entries in the replay list to the TNC. Returns zero in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static int apply_replay_list(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) struct replay_entry *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) list_sort(c, &c->replay_list, &replay_entries_cmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) list_for_each_entry(r, &c->replay_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) err = apply_replay_entry(c, r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * destroy_replay_list - destroy the replay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * Destroy the replay list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static void destroy_replay_list(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct replay_entry *r, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (is_hash_key(c, &r->key))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) kfree(fname_name(&r->nm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) list_del(&r->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) kfree(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) * insert_node - insert a node to the replay list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * @lnum: node logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * @offs: node offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * @len: node length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * @key: node key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * @sqnum: sequence number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * @deletion: non-zero if this is a deletion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * @used: number of bytes in use in a LEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * @old_size: truncation old size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) * @new_size: truncation new size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * This function inserts a scanned non-direntry node to the replay list. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * replay list contains @struct replay_entry elements, and we sort this list in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) * sequence number order before applying it. The replay list is applied at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * very end of the replay process. Since the list is sorted in sequence number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * order, the older modifications are applied first. This function returns zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) * in case of success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) const u8 *hash, union ubifs_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) unsigned long long sqnum, int deletion, int *used,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) loff_t old_size, loff_t new_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) struct replay_entry *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) if (key_inum(c, key) >= c->highest_inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) c->highest_inum = key_inum(c, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) if (!r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (!deletion)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) *used += ALIGN(len, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) r->lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) r->offs = offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) r->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) ubifs_copy_hash(c, hash, r->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) r->deletion = !!deletion;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) r->sqnum = sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) key_copy(c, key, &r->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) r->old_size = old_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) r->new_size = new_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) list_add_tail(&r->list, &c->replay_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * insert_dent - insert a directory entry node into the replay list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * @lnum: node logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * @offs: node offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * @len: node length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * @key: node key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * @name: directory entry name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * @nlen: directory entry name length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * @sqnum: sequence number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * @deletion: non-zero if this is a deletion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * @used: number of bytes in use in a LEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * This function inserts a scanned directory entry node or an extended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) * attribute entry to the replay list. Returns zero in case of success and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) const u8 *hash, union ubifs_key *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) const char *name, int nlen, unsigned long long sqnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) int deletion, int *used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) struct replay_entry *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) char *nbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (key_inum(c, key) >= c->highest_inum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) c->highest_inum = key_inum(c, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) if (!r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) nbuf = kmalloc(nlen + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) if (!nbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) kfree(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (!deletion)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) *used += ALIGN(len, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) r->lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) r->offs = offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) r->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) ubifs_copy_hash(c, hash, r->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) r->deletion = !!deletion;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) r->sqnum = sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) key_copy(c, key, &r->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) fname_len(&r->nm) = nlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) memcpy(nbuf, name, nlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) nbuf[nlen] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) fname_name(&r->nm) = nbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) list_add_tail(&r->list, &c->replay_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * ubifs_validate_entry - validate directory or extended attribute entry node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) * @dent: the node to validate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) * This function validates directory or extended attribute entry node @dent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * Returns zero if the node is all right and a %-EINVAL if not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) int ubifs_validate_entry(struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) const struct ubifs_dent_node *dent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) int key_type = key_type_flash(c, dent->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) int nlen = le16_to_cpu(dent->nlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) dent->type >= UBIFS_ITYPES_CNT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) (key_type == UBIFS_XENT_KEY && strnlen(dent->name, nlen) != nlen) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) le64_to_cpu(dent->inum) > MAX_INUM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) "directory entry" : "extended attribute entry");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) ubifs_err(c, "bad key type %d", key_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) return 0;
^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) * is_last_bud - check if the bud is the last in the journal head.
^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) * @bud: bud description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * This function checks if bud @bud is the last bud in its journal head. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * information is then used by 'replay_bud()' to decide whether the bud can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * have corruptions or not. Indeed, only last buds can be corrupted by power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) * cuts. Returns %1 if this is the last bud, and %0 if not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) struct ubifs_jhead *jh = &c->jheads[bud->jhead];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct ubifs_bud *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) uint32_t data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (list_is_last(&bud->list, &jh->buds_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) * The following is a quirk to make sure we work correctly with UBIFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) * images used with older UBIFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * Normally, the last bud will be the last in the journal head's list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * of bud. However, there is one exception if the UBIFS image belongs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) * to older UBIFS. This is fairly unlikely: one would need to use old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * UBIFS, then have a power cut exactly at the right point, and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) * try to mount this image with new UBIFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * The exception is: it is possible to have 2 buds A and B, A goes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) * before B, and B is the last, bud B is contains no data, and bud A is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) * corrupted at the end. The reason is that in older versions when the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) * journal code switched the next bud (from A to B), it first added a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) * log reference node for the new bud (B), and only after this it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) * synchronized the write-buffer of current bud (A). But later this was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) * changed and UBIFS started to always synchronize the write-buffer of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * the bud (A) before writing the log reference for the new bud (B).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) * But because older UBIFS always synchronized A's write-buffer before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * writing to B, we can recognize this exceptional situation but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * checking the contents of bud B - if it is empty, then A can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * treated as the last and we can recover it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * TODO: remove this piece of code in a couple of years (today it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) * 16.05.2011).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) next = list_entry(bud->list.next, struct ubifs_bud, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (!list_is_last(&next->list, &jh->buds_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return data == 0xFFFFFFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) /* authenticate_sleb_hash is split out for stack usage */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) static int noinline_for_stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) authenticate_sleb_hash(struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) struct shash_desc *log_hash, u8 *hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) hash_desc->tfm = c->hash_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) ubifs_shash_copy_state(c, log_hash, hash_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) return crypto_shash_final(hash_desc, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) * authenticate_sleb - authenticate one scan LEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) * @sleb: the scan LEB to authenticate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) * @log_hash:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) * @is_last: if true, this is is the last LEB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) * This function iterates over the buds of a single LEB authenticating all buds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) * with the authentication nodes on this LEB. Authentication nodes are written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) * after some buds and contain a HMAC covering the authentication node itself
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) * and the buds between the last authentication node and the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) * authentication node. It can happen that the last buds cannot be authenticated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * because a powercut happened when some nodes were written but not the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) * corresponding authentication node. This function returns the number of nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * that could be authenticated or a negative error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) static int authenticate_sleb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) struct shash_desc *log_hash, int is_last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) int n_not_auth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) struct ubifs_scan_node *snod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) int n_nodes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) u8 hash[UBIFS_HASH_ARR_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) u8 hmac[UBIFS_HMAC_ARR_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (!ubifs_authenticated(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) return sleb->nodes_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) list_for_each_entry(snod, &sleb->nodes, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) n_nodes++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) if (snod->type == UBIFS_AUTH_NODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) struct ubifs_auth_node *auth = snod->node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) err = authenticate_sleb_hash(c, log_hash, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) err = crypto_shash_tfm_digest(c->hmac_tfm, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) c->hash_len, hmac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) err = ubifs_check_hmac(c, auth->hmac, hmac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) n_not_auth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) err = crypto_shash_update(log_hash, snod->node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) snod->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) n_not_auth++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * A powercut can happen when some nodes were written, but not yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) * the corresponding authentication node. This may only happen on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) * the last bud though.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (n_not_auth) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) if (is_last) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) dbg_mnt("%d unauthenticated nodes found on LEB %d, Ignoring them",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) n_not_auth, sleb->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) dbg_mnt("%d unauthenticated nodes found on non-last LEB %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) n_not_auth, sleb->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) return err ? err : n_nodes - n_not_auth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) * replay_bud - replay a bud logical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) * @b: bud entry which describes the bud
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * This function replays bud @bud, recovers it if needed, and adds all nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) * from this bud to the replay list. Returns zero in case of success and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) * negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) int is_last = is_last_bud(c, b->bud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) int n_nodes, n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) struct ubifs_scan_leb *sleb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) struct ubifs_scan_node *snod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) lnum, b->bud->jhead, offs, is_last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (c->need_recovery && is_last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) * Recover only last LEBs in the journal heads, because power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) * cuts may cause corruptions only in these LEBs, because only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) * these LEBs could possibly be written to at the power cut
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) * time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) if (IS_ERR(sleb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) return PTR_ERR(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) n_nodes = authenticate_sleb(c, sleb, b->bud->log_hash, is_last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) if (n_nodes < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) err = n_nodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) ubifs_shash_copy_state(c, b->bud->log_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) c->jheads[b->bud->jhead].log_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) * The bud does not have to start from offset zero - the beginning of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) * the 'lnum' LEB may contain previously committed data. One of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) * things we have to do in replay is to correctly update lprops with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) * newer information about this LEB.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) * At this point lprops thinks that this LEB has 'c->leb_size - offs'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) * bytes of free space because it only contain information about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) * committed data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) * But we know that real amount of free space is 'c->leb_size -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) * 'sleb->endpt' is used by bud data. We have to correctly calculate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) * how much of these data are dirty and update lprops with this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) * information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) * The dirt in that LEB region is comprised of padding nodes, deletion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) * nodes, truncation nodes and nodes which are obsoleted by subsequent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) * nodes in this LEB. So instead of calculating clean space, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) * calculate used space ('used' variable).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) list_for_each_entry(snod, &sleb->nodes, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) u8 hash[UBIFS_HASH_ARR_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) int deletion = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) if (snod->sqnum >= SQNUM_WATERMARK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) ubifs_err(c, "file system's life ended");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) goto out_dump;
^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) ubifs_node_calc_hash(c, snod->node, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) if (snod->sqnum > c->max_sqnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) c->max_sqnum = snod->sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) switch (snod->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) case UBIFS_INO_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) struct ubifs_ino_node *ino = snod->node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) loff_t new_size = le64_to_cpu(ino->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) if (le32_to_cpu(ino->nlink) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) deletion = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) err = insert_node(c, lnum, snod->offs, snod->len, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) &snod->key, snod->sqnum, deletion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) &used, 0, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) case UBIFS_DATA_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) struct ubifs_data_node *dn = snod->node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) loff_t new_size = le32_to_cpu(dn->size) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) key_block(c, &snod->key) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) UBIFS_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) err = insert_node(c, lnum, snod->offs, snod->len, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) &snod->key, snod->sqnum, deletion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) &used, 0, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) case UBIFS_DENT_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) case UBIFS_XENT_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) struct ubifs_dent_node *dent = snod->node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) err = ubifs_validate_entry(c, dent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) err = insert_dent(c, lnum, snod->offs, snod->len, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) &snod->key, dent->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) le16_to_cpu(dent->nlen), snod->sqnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) !le64_to_cpu(dent->inum), &used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) case UBIFS_TRUN_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) struct ubifs_trun_node *trun = snod->node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) loff_t old_size = le64_to_cpu(trun->old_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) loff_t new_size = le64_to_cpu(trun->new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) union ubifs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) /* Validate truncation node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) if (old_size < 0 || old_size > c->max_inode_sz ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) new_size < 0 || new_size > c->max_inode_sz ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) old_size <= new_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) ubifs_err(c, "bad truncation node");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) * Create a fake truncation key just to use the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) * functions which expect nodes to have keys.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) trun_key_init(c, &key, le32_to_cpu(trun->inum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) err = insert_node(c, lnum, snod->offs, snod->len, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) &key, snod->sqnum, 1, &used,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) old_size, new_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) case UBIFS_AUTH_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) snod->type, lnum, snod->offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) if (n == n_nodes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) ubifs_assert(c, ubifs_search_bud(c, lnum));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) ubifs_assert(c, sleb->endpt - offs >= used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) ubifs_assert(c, sleb->endpt % c->min_io_size == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) b->dirty = sleb->endpt - offs - used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) b->free = c->leb_size - sleb->endpt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) dbg_mnt("bud LEB %d replied: dirty %d, free %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) lnum, b->dirty, b->free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) ubifs_scan_destroy(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) out_dump:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) ubifs_dump_node(c, snod->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) ubifs_scan_destroy(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) * replay_buds - replay all buds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) * This function returns zero in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) static int replay_buds(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) struct bud_entry *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) unsigned long long prev_sqnum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) list_for_each_entry(b, &c->replay_buds, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) err = replay_bud(c, b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) ubifs_assert(c, b->sqnum > prev_sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) prev_sqnum = b->sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) * destroy_bud_list - destroy the list of buds to replay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) static void destroy_bud_list(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) struct bud_entry *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) while (!list_empty(&c->replay_buds)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) b = list_entry(c->replay_buds.next, struct bud_entry, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) list_del(&b->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) kfree(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) * add_replay_bud - add a bud to the list of buds to replay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) * @lnum: bud logical eraseblock number to replay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) * @offs: bud start offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) * @jhead: journal head to which this bud belongs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) * @sqnum: reference node sequence number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * This function returns zero in case of success and a negative error code in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) unsigned long long sqnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) struct ubifs_bud *bud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) struct bud_entry *b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) if (!bud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) if (!b) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) bud->lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) bud->start = offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) bud->jhead = jhead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) bud->log_hash = ubifs_hash_get_desc(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) if (IS_ERR(bud->log_hash)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) err = PTR_ERR(bud->log_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) ubifs_shash_copy_state(c, c->log_hash, bud->log_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) ubifs_add_bud(c, bud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) b->bud = bud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) b->sqnum = sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) list_add_tail(&b->list, &c->replay_buds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) kfree(bud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) kfree(b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) * validate_ref - validate a reference node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) * @ref: the reference node to validate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) * This function returns %1 if a bud reference already exists for the LEB. %0 is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) * returned if the reference node is new, otherwise %-EINVAL is returned if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) * validation failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) struct ubifs_bud *bud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) int lnum = le32_to_cpu(ref->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) unsigned int offs = le32_to_cpu(ref->offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) unsigned int jhead = le32_to_cpu(ref->jhead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) * ref->offs may point to the end of LEB when the journal head points
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) * to the end of LEB and we write reference node for it during commit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) * So this is why we require 'offs > c->leb_size'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) lnum < c->main_first || offs > c->leb_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) offs & (c->min_io_size - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) /* Make sure we have not already looked at this bud */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) bud = ubifs_search_bud(c, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) if (bud) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) if (bud->jhead == jhead && bud->start <= offs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) * replay_log_leb - replay a log logical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) * @lnum: log logical eraseblock to replay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) * @offs: offset to start replaying from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) * @sbuf: scan buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) * This function replays a log LEB and returns zero in case of success, %1 if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) * this is the last LEB in the log, and a negative error code in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) struct ubifs_scan_leb *sleb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) struct ubifs_scan_node *snod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) const struct ubifs_cs_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) dbg_mnt("replay log LEB %d:%d", lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) if (IS_ERR(sleb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) return PTR_ERR(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) * Note, the below function will recover this log LEB only if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) * it is the last, because unclean reboots can possibly corrupt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) * only the tail of the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) if (IS_ERR(sleb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) return PTR_ERR(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) if (sleb->nodes_cnt == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) err = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) node = sleb->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) if (c->cs_sqnum == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) * This is the first log LEB we are looking at, make sure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) * the first node is a commit start node. Also record its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) * sequence number so that UBIFS can determine where the log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) * ends, because all nodes which were have higher sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) * numbers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) if (snod->type != UBIFS_CS_NODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) ubifs_err(c, "first log node at LEB %d:%d is not CS node",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) lnum, offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) (unsigned long long)le64_to_cpu(node->cmt_no),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) c->cmt_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) err = ubifs_shash_init(c, c->log_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) err = ubifs_shash_update(c, c->log_hash, node, UBIFS_CS_NODE_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) if (snod->sqnum < c->cs_sqnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) * This means that we reached end of log and now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) * look to the older log data, which was already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) * committed but the eraseblock was not erased (UBIFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) * only un-maps it). So this basically means we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) * exit with "end of log" code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) err = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) /* Make sure the first node sits at offset zero of the LEB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) if (snod->offs != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) ubifs_err(c, "first node is not at zero offset");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) list_for_each_entry(snod, &sleb->nodes, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) if (snod->sqnum >= SQNUM_WATERMARK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) ubifs_err(c, "file system's life ended");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) if (snod->sqnum < c->cs_sqnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) snod->sqnum, c->cs_sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) if (snod->sqnum > c->max_sqnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) c->max_sqnum = snod->sqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) switch (snod->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) case UBIFS_REF_NODE: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) const struct ubifs_ref_node *ref = snod->node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) err = validate_ref(c, ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) if (err == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) break; /* Already have this bud */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) err = ubifs_shash_update(c, c->log_hash, ref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) UBIFS_REF_NODE_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) err = add_replay_bud(c, le32_to_cpu(ref->lnum),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) le32_to_cpu(ref->offs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) le32_to_cpu(ref->jhead),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) snod->sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) case UBIFS_CS_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) /* Make sure it sits at the beginning of LEB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) if (snod->offs != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) ubifs_err(c, "unexpected node in log");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) ubifs_err(c, "unexpected node in log");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) if (sleb->endpt || c->lhead_offs >= c->leb_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) c->lhead_lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) c->lhead_offs = sleb->endpt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) err = !sleb->endpt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) ubifs_scan_destroy(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) out_dump:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) lnum, offs + snod->offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) ubifs_dump_node(c, snod->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) ubifs_scan_destroy(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) * take_ihead - update the status of the index head in lprops to 'taken'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) * This function returns the amount of free space in the index head LEB or a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) * negative error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) static int take_ihead(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) const struct ubifs_lprops *lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) int err, free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) ubifs_get_lprops(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) if (IS_ERR(lp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) err = PTR_ERR(lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) free = lp->free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) lp->flags | LPROPS_TAKEN, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) if (IS_ERR(lp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) err = PTR_ERR(lp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) err = free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) ubifs_release_lprops(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) * ubifs_replay_journal - replay journal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) * This function scans the journal, replays and cleans it up. It makes sure all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) * memory data structures related to uncommitted journal are built (dirty TNC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) * tree, tree of buds, modified lprops, etc).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) int ubifs_replay_journal(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) int err, lnum, free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) /* Update the status of the index head in lprops to 'taken' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) free = take_ihead(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) if (free < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) return free; /* Error code */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) if (c->ihead_offs != c->leb_size - free) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) c->ihead_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) dbg_mnt("start replaying the journal");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) c->replaying = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) lnum = c->ltail_lnum = c->lhead_lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) err = replay_log_leb(c, lnum, 0, c->sbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) if (err == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) if (lnum != c->lhead_lnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) /* We hit the end of the log */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) * The head of the log must always start with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) * "commit start" node on a properly formatted UBIFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) * But we found no nodes at all, which means that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) * something went wrong and we cannot proceed mounting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) * the file-system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) lnum, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) lnum = ubifs_next_log_lnum(c, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) } while (lnum != c->ltail_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) err = replay_buds(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) err = apply_replay_list(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) err = set_buds_lprops(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) * depend on it. This means we have to initialize it to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) * budgeting works properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) c->bi.uncommitted_idx *= c->max_idx_node_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) ubifs_assert(c, c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) c->lhead_lnum, c->lhead_offs, c->max_sqnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) (unsigned long)c->highest_inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) destroy_replay_list(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) destroy_bud_list(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) c->replaying = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) }