^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 miscelanious TNC-related functions shared betweend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * different files. This file does not form any logically separate TNC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * sub-system. The file was created because there is a lot of TNC code and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * putting it all in one file would make that file too big and unreadable.
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * ubifs_tnc_levelorder_next - next TNC tree element in levelorder traversal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * @zr: root of the subtree to traverse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @znode: previous znode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * This function implements levelorder TNC traversal. The LNC is ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * Returns the next element or %NULL if @znode is already the last one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct ubifs_znode *ubifs_tnc_levelorder_next(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct ubifs_znode *zr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct ubifs_znode *znode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int level, iip, level_search = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct ubifs_znode *zn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ubifs_assert(c, zr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (unlikely(!znode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return zr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) if (unlikely(znode == zr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (znode->level == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return ubifs_tnc_find_child(zr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) level = znode->level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) iip = znode->iip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ubifs_assert(c, znode->level <= zr->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * First walk up until there is a znode with next branch to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * look at.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) while (znode->parent != zr && iip >= znode->parent->child_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) znode = znode->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) iip = znode->iip;
^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) if (unlikely(znode->parent == zr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) iip >= znode->parent->child_cnt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* This level is done, switch to the lower one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) level -= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) if (level_search || level < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * We were already looking for znode at lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * level ('level_search'). As we are here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * again, it just does not exist. Or all levels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * were finished ('level < 0').
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) level_search = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) iip = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) znode = ubifs_tnc_find_child(zr, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) ubifs_assert(c, znode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* Switch to the next index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) zn = ubifs_tnc_find_child(znode->parent, iip + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!zn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) /* No more children to look at, we have walk up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) iip = znode->parent->child_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* Walk back down to the level we came from ('level') */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) while (zn->level != level) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) znode = zn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) zn = ubifs_tnc_find_child(zn, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (!zn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * This path is not too deep so it does not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * reach 'level'. Try next path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) iip = znode->iip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (zn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ubifs_assert(c, zn->level >= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return zn;
^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) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * ubifs_search_zbranch - search znode branch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * @znode: znode to search in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * @key: key to search for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * @n: znode branch slot number is returned here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * This is a helper function which search branch with key @key in @znode using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * binary search. The result of the search may be:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * o exact match, then %1 is returned, and the slot number of the branch is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * stored in @n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * o no exact match, then %0 is returned and the slot number of the left
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * closest branch is returned in @n; the slot if all keys in this znode are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * greater than @key, then %-1 is returned in @n.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int ubifs_search_zbranch(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) const struct ubifs_znode *znode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) const union ubifs_key *key, int *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int beg = 0, end = znode->child_cnt, mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) int cmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) const struct ubifs_zbranch *zbr = &znode->zbranch[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ubifs_assert(c, end > beg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) while (end > beg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) mid = (beg + end) >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) cmp = keys_cmp(c, key, &zbr[mid].key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (cmp > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) beg = mid + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) else if (cmp < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) end = mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) *n = mid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) *n = end - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* The insert point is after *n */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ubifs_assert(c, *n >= -1 && *n < znode->child_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (*n == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) ubifs_assert(c, keys_cmp(c, key, &zbr[0].key) < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) ubifs_assert(c, keys_cmp(c, key, &zbr[*n].key) > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (*n + 1 < znode->child_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) ubifs_assert(c, keys_cmp(c, key, &zbr[*n + 1].key) < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * ubifs_tnc_postorder_first - find first znode to do postorder tree traversal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * @znode: znode to start at (root of the sub-tree to traverse)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * Find the lowest leftmost znode in a subtree of the TNC tree. The LNC is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (unlikely(!znode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) while (znode->level > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct ubifs_znode *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) child = ubifs_tnc_find_child(znode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (!child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return znode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) znode = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return znode;
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * ubifs_tnc_postorder_next - next TNC tree element in postorder traversal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * @znode: previous znode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * This function implements postorder TNC traversal. The LNC is ignored.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * Returns the next element or %NULL if @znode is already the last one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) struct ubifs_znode *ubifs_tnc_postorder_next(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct ubifs_znode *znode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct ubifs_znode *zn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) ubifs_assert(c, znode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (unlikely(!znode->parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /* Switch to the next index in the parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) zn = ubifs_tnc_find_child(znode->parent, znode->iip + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (!zn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /* This is in fact the last child, return parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return znode->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /* Go to the first znode in this new subtree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return ubifs_tnc_postorder_first(zn);
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * ubifs_destroy_tnc_subtree - destroy all znodes connected to a subtree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * @znode: znode defining subtree to destroy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * This function destroys subtree of the TNC tree. Returns number of clean
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * znodes in the subtree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) long ubifs_destroy_tnc_subtree(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct ubifs_znode *znode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct ubifs_znode *zn = ubifs_tnc_postorder_first(znode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) long clean_freed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) ubifs_assert(c, zn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) for (n = 0; n < zn->child_cnt; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (!zn->zbranch[n].znode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (zn->level > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) !ubifs_zn_dirty(zn->zbranch[n].znode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) clean_freed += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) kfree(zn->zbranch[n].znode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (zn == znode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (!ubifs_zn_dirty(zn))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) clean_freed += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) kfree(zn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return clean_freed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) zn = ubifs_tnc_postorder_next(c, zn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * read_znode - read an indexing node from flash and fill znode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * @zzbr: the zbranch describing the node to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * @znode: znode to read to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * This function reads an indexing node from the flash media and fills znode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) * with the read data. Returns zero in case of success and a negative error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * code in case of failure. The read indexing node is validated and if anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * is wrong with it, this function prints complaint messages and returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * %-EINVAL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) static int read_znode(struct ubifs_info *c, struct ubifs_zbranch *zzbr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct ubifs_znode *znode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int lnum = zzbr->lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) int offs = zzbr->offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int len = zzbr->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) int i, err, type, cmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct ubifs_idx_node *idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (!idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) kfree(idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) err = ubifs_node_check_hash(c, idx, zzbr->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) ubifs_bad_hash(c, idx, zzbr->hash, lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) kfree(idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return err;
^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) znode->child_cnt = le16_to_cpu(idx->child_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) znode->level = le16_to_cpu(idx->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) dbg_tnc("LEB %d:%d, level %d, %d branch",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) lnum, offs, znode->level, znode->child_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (znode->child_cnt > c->fanout || znode->level > UBIFS_MAX_LEVELS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) ubifs_err(c, "current fanout %d, branch count %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) c->fanout, znode->child_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) ubifs_err(c, "max levels %d, znode level %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) UBIFS_MAX_LEVELS, znode->level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) err = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) for (i = 0; i < znode->child_cnt; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct ubifs_zbranch *zbr = &znode->zbranch[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) key_read(c, &br->key, &zbr->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) zbr->lnum = le32_to_cpu(br->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) zbr->offs = le32_to_cpu(br->offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) zbr->len = le32_to_cpu(br->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) ubifs_copy_hash(c, ubifs_branch_hash(c, br), zbr->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) zbr->znode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) /* Validate branch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (zbr->lnum < c->main_first ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) zbr->lnum >= c->leb_cnt || zbr->offs < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) zbr->offs + zbr->len > c->leb_size || zbr->offs & 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) ubifs_err(c, "bad branch %d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) err = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) switch (key_type(c, &zbr->key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) case UBIFS_INO_KEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) case UBIFS_DATA_KEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) case UBIFS_DENT_KEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) case UBIFS_XENT_KEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ubifs_err(c, "bad key type at slot %d: %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) i, key_type(c, &zbr->key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) err = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) goto out_dump;
^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) if (znode->level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) type = key_type(c, &zbr->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (c->ranges[type].max_len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (zbr->len != c->ranges[type].len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) ubifs_err(c, "bad target node (type %d) length (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) type, zbr->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) ubifs_err(c, "have to be %d", c->ranges[type].len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) err = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) } else if (zbr->len < c->ranges[type].min_len ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) zbr->len > c->ranges[type].max_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) ubifs_err(c, "bad target node (type %d) length (%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) type, zbr->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) ubifs_err(c, "have to be in range of %d-%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) c->ranges[type].min_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) c->ranges[type].max_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) err = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) goto out_dump;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * Ensure that the next key is greater or equivalent to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * previous one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) for (i = 0; i < znode->child_cnt - 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) const union ubifs_key *key1, *key2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) key1 = &znode->zbranch[i].key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) key2 = &znode->zbranch[i + 1].key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) cmp = keys_cmp(c, key1, key2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (cmp > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) ubifs_err(c, "bad key order (keys %d and %d)", i, i + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) err = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) } else if (cmp == 0 && !is_hash_key(c, key1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /* These can only be keys with colliding hash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) ubifs_err(c, "keys %d and %d are not hashed but equivalent",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) i, i + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) err = 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) kfree(idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) out_dump:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) ubifs_err(c, "bad indexing node at LEB %d:%d, error %d", lnum, offs, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) ubifs_dump_node(c, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) kfree(idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * ubifs_load_znode - load znode to TNC cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * @zbr: znode branch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * @parent: znode's parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * @iip: index in parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * This function loads znode pointed to by @zbr into the TNC cache and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * returns pointer to it in case of success and a negative error code in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) struct ubifs_zbranch *zbr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) struct ubifs_znode *parent, int iip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) struct ubifs_znode *znode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) ubifs_assert(c, !zbr->znode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * A slab cache is not presently used for znodes because the znode size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * depends on the fanout which is stored in the superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) znode = kzalloc(c->max_znode_sz, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) if (!znode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) err = read_znode(c, zbr, znode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) atomic_long_inc(&c->clean_zn_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) * Increment the global clean znode counter as well. It is OK that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) * global and per-FS clean znode counters may be inconsistent for some
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * short time (because we might be preempted at this point), the global
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * one is only used in shrinker.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) atomic_long_inc(&ubifs_clean_zn_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) zbr->znode = znode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) znode->parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) znode->time = ktime_get_seconds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) znode->iip = iip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) return znode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) kfree(znode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * ubifs_tnc_read_node - read a leaf node from the flash media.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * @zbr: key and position of the node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * @node: node is returned here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * This function reads a node defined by @zbr from the flash media. Returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * zero in case of success or a negative negative error code in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) void *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) union ubifs_key key1, *key = &zbr->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) int err, type = key_type(c, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) struct ubifs_wbuf *wbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * 'zbr' has to point to on-flash node. The node may sit in a bud and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * may even be in a write buffer, so we have to take care about this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) wbuf = ubifs_get_wbuf(c, zbr->lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (wbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) err = ubifs_read_node_wbuf(wbuf, node, type, zbr->len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) zbr->lnum, zbr->offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) err = ubifs_read_node(c, node, type, zbr->len, zbr->lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) zbr->offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) dbg_tnck(key, "key ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) /* Make sure the key of the read node is correct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) key_read(c, node + UBIFS_KEY_OFFSET, &key1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (!keys_eq(c, key, &key1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) ubifs_err(c, "bad key in node at LEB %d:%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) zbr->lnum, zbr->offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) dbg_tnck(key, "looked for key ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) dbg_tnck(&key1, "but found node's key ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) ubifs_dump_node(c, node);
^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) err = ubifs_node_check_hash(c, node, zbr->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) ubifs_bad_hash(c, node, zbr->hash, zbr->lnum, zbr->offs);
^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) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) }