18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * This file is part of UBIFS. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2006-2008 Nokia Corporation. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Authors: Adrian Hunter 88c2ecf20Sopenharmony_ci * Artem Bityutskiy (Битюцкий Артём) 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci/* 128c2ecf20Sopenharmony_ci * This file contains miscelanious TNC-related functions shared betweend 138c2ecf20Sopenharmony_ci * different files. This file does not form any logically separate TNC 148c2ecf20Sopenharmony_ci * sub-system. The file was created because there is a lot of TNC code and 158c2ecf20Sopenharmony_ci * putting it all in one file would make that file too big and unreadable. 168c2ecf20Sopenharmony_ci */ 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include "ubifs.h" 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci/** 218c2ecf20Sopenharmony_ci * ubifs_tnc_levelorder_next - next TNC tree element in levelorder traversal. 228c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object 238c2ecf20Sopenharmony_ci * @zr: root of the subtree to traverse 248c2ecf20Sopenharmony_ci * @znode: previous znode 258c2ecf20Sopenharmony_ci * 268c2ecf20Sopenharmony_ci * This function implements levelorder TNC traversal. The LNC is ignored. 278c2ecf20Sopenharmony_ci * Returns the next element or %NULL if @znode is already the last one. 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_cistruct ubifs_znode *ubifs_tnc_levelorder_next(const struct ubifs_info *c, 308c2ecf20Sopenharmony_ci struct ubifs_znode *zr, 318c2ecf20Sopenharmony_ci struct ubifs_znode *znode) 328c2ecf20Sopenharmony_ci{ 338c2ecf20Sopenharmony_ci int level, iip, level_search = 0; 348c2ecf20Sopenharmony_ci struct ubifs_znode *zn; 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci ubifs_assert(c, zr); 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci if (unlikely(!znode)) 398c2ecf20Sopenharmony_ci return zr; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci if (unlikely(znode == zr)) { 428c2ecf20Sopenharmony_ci if (znode->level == 0) 438c2ecf20Sopenharmony_ci return NULL; 448c2ecf20Sopenharmony_ci return ubifs_tnc_find_child(zr, 0); 458c2ecf20Sopenharmony_ci } 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci level = znode->level; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci iip = znode->iip; 508c2ecf20Sopenharmony_ci while (1) { 518c2ecf20Sopenharmony_ci ubifs_assert(c, znode->level <= zr->level); 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci /* 548c2ecf20Sopenharmony_ci * First walk up until there is a znode with next branch to 558c2ecf20Sopenharmony_ci * look at. 568c2ecf20Sopenharmony_ci */ 578c2ecf20Sopenharmony_ci while (znode->parent != zr && iip >= znode->parent->child_cnt) { 588c2ecf20Sopenharmony_ci znode = znode->parent; 598c2ecf20Sopenharmony_ci iip = znode->iip; 608c2ecf20Sopenharmony_ci } 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci if (unlikely(znode->parent == zr && 638c2ecf20Sopenharmony_ci iip >= znode->parent->child_cnt)) { 648c2ecf20Sopenharmony_ci /* This level is done, switch to the lower one */ 658c2ecf20Sopenharmony_ci level -= 1; 668c2ecf20Sopenharmony_ci if (level_search || level < 0) 678c2ecf20Sopenharmony_ci /* 688c2ecf20Sopenharmony_ci * We were already looking for znode at lower 698c2ecf20Sopenharmony_ci * level ('level_search'). As we are here 708c2ecf20Sopenharmony_ci * again, it just does not exist. Or all levels 718c2ecf20Sopenharmony_ci * were finished ('level < 0'). 728c2ecf20Sopenharmony_ci */ 738c2ecf20Sopenharmony_ci return NULL; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci level_search = 1; 768c2ecf20Sopenharmony_ci iip = -1; 778c2ecf20Sopenharmony_ci znode = ubifs_tnc_find_child(zr, 0); 788c2ecf20Sopenharmony_ci ubifs_assert(c, znode); 798c2ecf20Sopenharmony_ci } 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci /* Switch to the next index */ 828c2ecf20Sopenharmony_ci zn = ubifs_tnc_find_child(znode->parent, iip + 1); 838c2ecf20Sopenharmony_ci if (!zn) { 848c2ecf20Sopenharmony_ci /* No more children to look at, we have walk up */ 858c2ecf20Sopenharmony_ci iip = znode->parent->child_cnt; 868c2ecf20Sopenharmony_ci continue; 878c2ecf20Sopenharmony_ci } 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci /* Walk back down to the level we came from ('level') */ 908c2ecf20Sopenharmony_ci while (zn->level != level) { 918c2ecf20Sopenharmony_ci znode = zn; 928c2ecf20Sopenharmony_ci zn = ubifs_tnc_find_child(zn, 0); 938c2ecf20Sopenharmony_ci if (!zn) { 948c2ecf20Sopenharmony_ci /* 958c2ecf20Sopenharmony_ci * This path is not too deep so it does not 968c2ecf20Sopenharmony_ci * reach 'level'. Try next path. 978c2ecf20Sopenharmony_ci */ 988c2ecf20Sopenharmony_ci iip = znode->iip; 998c2ecf20Sopenharmony_ci break; 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci } 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci if (zn) { 1048c2ecf20Sopenharmony_ci ubifs_assert(c, zn->level >= 0); 1058c2ecf20Sopenharmony_ci return zn; 1068c2ecf20Sopenharmony_ci } 1078c2ecf20Sopenharmony_ci } 1088c2ecf20Sopenharmony_ci} 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci/** 1118c2ecf20Sopenharmony_ci * ubifs_search_zbranch - search znode branch. 1128c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object 1138c2ecf20Sopenharmony_ci * @znode: znode to search in 1148c2ecf20Sopenharmony_ci * @key: key to search for 1158c2ecf20Sopenharmony_ci * @n: znode branch slot number is returned here 1168c2ecf20Sopenharmony_ci * 1178c2ecf20Sopenharmony_ci * This is a helper function which search branch with key @key in @znode using 1188c2ecf20Sopenharmony_ci * binary search. The result of the search may be: 1198c2ecf20Sopenharmony_ci * o exact match, then %1 is returned, and the slot number of the branch is 1208c2ecf20Sopenharmony_ci * stored in @n; 1218c2ecf20Sopenharmony_ci * o no exact match, then %0 is returned and the slot number of the left 1228c2ecf20Sopenharmony_ci * closest branch is returned in @n; the slot if all keys in this znode are 1238c2ecf20Sopenharmony_ci * greater than @key, then %-1 is returned in @n. 1248c2ecf20Sopenharmony_ci */ 1258c2ecf20Sopenharmony_ciint ubifs_search_zbranch(const struct ubifs_info *c, 1268c2ecf20Sopenharmony_ci const struct ubifs_znode *znode, 1278c2ecf20Sopenharmony_ci const union ubifs_key *key, int *n) 1288c2ecf20Sopenharmony_ci{ 1298c2ecf20Sopenharmony_ci int beg = 0, end = znode->child_cnt, mid; 1308c2ecf20Sopenharmony_ci int cmp; 1318c2ecf20Sopenharmony_ci const struct ubifs_zbranch *zbr = &znode->zbranch[0]; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci ubifs_assert(c, end > beg); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci while (end > beg) { 1368c2ecf20Sopenharmony_ci mid = (beg + end) >> 1; 1378c2ecf20Sopenharmony_ci cmp = keys_cmp(c, key, &zbr[mid].key); 1388c2ecf20Sopenharmony_ci if (cmp > 0) 1398c2ecf20Sopenharmony_ci beg = mid + 1; 1408c2ecf20Sopenharmony_ci else if (cmp < 0) 1418c2ecf20Sopenharmony_ci end = mid; 1428c2ecf20Sopenharmony_ci else { 1438c2ecf20Sopenharmony_ci *n = mid; 1448c2ecf20Sopenharmony_ci return 1; 1458c2ecf20Sopenharmony_ci } 1468c2ecf20Sopenharmony_ci } 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci *n = end - 1; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci /* The insert point is after *n */ 1518c2ecf20Sopenharmony_ci ubifs_assert(c, *n >= -1 && *n < znode->child_cnt); 1528c2ecf20Sopenharmony_ci if (*n == -1) 1538c2ecf20Sopenharmony_ci ubifs_assert(c, keys_cmp(c, key, &zbr[0].key) < 0); 1548c2ecf20Sopenharmony_ci else 1558c2ecf20Sopenharmony_ci ubifs_assert(c, keys_cmp(c, key, &zbr[*n].key) > 0); 1568c2ecf20Sopenharmony_ci if (*n + 1 < znode->child_cnt) 1578c2ecf20Sopenharmony_ci ubifs_assert(c, keys_cmp(c, key, &zbr[*n + 1].key) < 0); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci return 0; 1608c2ecf20Sopenharmony_ci} 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci/** 1638c2ecf20Sopenharmony_ci * ubifs_tnc_postorder_first - find first znode to do postorder tree traversal. 1648c2ecf20Sopenharmony_ci * @znode: znode to start at (root of the sub-tree to traverse) 1658c2ecf20Sopenharmony_ci * 1668c2ecf20Sopenharmony_ci * Find the lowest leftmost znode in a subtree of the TNC tree. The LNC is 1678c2ecf20Sopenharmony_ci * ignored. 1688c2ecf20Sopenharmony_ci */ 1698c2ecf20Sopenharmony_cistruct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode) 1708c2ecf20Sopenharmony_ci{ 1718c2ecf20Sopenharmony_ci if (unlikely(!znode)) 1728c2ecf20Sopenharmony_ci return NULL; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci while (znode->level > 0) { 1758c2ecf20Sopenharmony_ci struct ubifs_znode *child; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci child = ubifs_tnc_find_child(znode, 0); 1788c2ecf20Sopenharmony_ci if (!child) 1798c2ecf20Sopenharmony_ci return znode; 1808c2ecf20Sopenharmony_ci znode = child; 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci return znode; 1848c2ecf20Sopenharmony_ci} 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci/** 1878c2ecf20Sopenharmony_ci * ubifs_tnc_postorder_next - next TNC tree element in postorder traversal. 1888c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object 1898c2ecf20Sopenharmony_ci * @znode: previous znode 1908c2ecf20Sopenharmony_ci * 1918c2ecf20Sopenharmony_ci * This function implements postorder TNC traversal. The LNC is ignored. 1928c2ecf20Sopenharmony_ci * Returns the next element or %NULL if @znode is already the last one. 1938c2ecf20Sopenharmony_ci */ 1948c2ecf20Sopenharmony_cistruct ubifs_znode *ubifs_tnc_postorder_next(const struct ubifs_info *c, 1958c2ecf20Sopenharmony_ci struct ubifs_znode *znode) 1968c2ecf20Sopenharmony_ci{ 1978c2ecf20Sopenharmony_ci struct ubifs_znode *zn; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci ubifs_assert(c, znode); 2008c2ecf20Sopenharmony_ci if (unlikely(!znode->parent)) 2018c2ecf20Sopenharmony_ci return NULL; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci /* Switch to the next index in the parent */ 2048c2ecf20Sopenharmony_ci zn = ubifs_tnc_find_child(znode->parent, znode->iip + 1); 2058c2ecf20Sopenharmony_ci if (!zn) 2068c2ecf20Sopenharmony_ci /* This is in fact the last child, return parent */ 2078c2ecf20Sopenharmony_ci return znode->parent; 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci /* Go to the first znode in this new subtree */ 2108c2ecf20Sopenharmony_ci return ubifs_tnc_postorder_first(zn); 2118c2ecf20Sopenharmony_ci} 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci/** 2148c2ecf20Sopenharmony_ci * ubifs_destroy_tnc_subtree - destroy all znodes connected to a subtree. 2158c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object 2168c2ecf20Sopenharmony_ci * @znode: znode defining subtree to destroy 2178c2ecf20Sopenharmony_ci * 2188c2ecf20Sopenharmony_ci * This function destroys subtree of the TNC tree. Returns number of clean 2198c2ecf20Sopenharmony_ci * znodes in the subtree. 2208c2ecf20Sopenharmony_ci */ 2218c2ecf20Sopenharmony_cilong ubifs_destroy_tnc_subtree(const struct ubifs_info *c, 2228c2ecf20Sopenharmony_ci struct ubifs_znode *znode) 2238c2ecf20Sopenharmony_ci{ 2248c2ecf20Sopenharmony_ci struct ubifs_znode *zn = ubifs_tnc_postorder_first(znode); 2258c2ecf20Sopenharmony_ci long clean_freed = 0; 2268c2ecf20Sopenharmony_ci int n; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci ubifs_assert(c, zn); 2298c2ecf20Sopenharmony_ci while (1) { 2308c2ecf20Sopenharmony_ci for (n = 0; n < zn->child_cnt; n++) { 2318c2ecf20Sopenharmony_ci if (!zn->zbranch[n].znode) 2328c2ecf20Sopenharmony_ci continue; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci if (zn->level > 0 && 2358c2ecf20Sopenharmony_ci !ubifs_zn_dirty(zn->zbranch[n].znode)) 2368c2ecf20Sopenharmony_ci clean_freed += 1; 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci cond_resched(); 2398c2ecf20Sopenharmony_ci kfree(zn->zbranch[n].znode); 2408c2ecf20Sopenharmony_ci } 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci if (zn == znode) { 2438c2ecf20Sopenharmony_ci if (!ubifs_zn_dirty(zn)) 2448c2ecf20Sopenharmony_ci clean_freed += 1; 2458c2ecf20Sopenharmony_ci kfree(zn); 2468c2ecf20Sopenharmony_ci return clean_freed; 2478c2ecf20Sopenharmony_ci } 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci zn = ubifs_tnc_postorder_next(c, zn); 2508c2ecf20Sopenharmony_ci } 2518c2ecf20Sopenharmony_ci} 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci/** 2548c2ecf20Sopenharmony_ci * read_znode - read an indexing node from flash and fill znode. 2558c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object 2568c2ecf20Sopenharmony_ci * @zzbr: the zbranch describing the node to read 2578c2ecf20Sopenharmony_ci * @znode: znode to read to 2588c2ecf20Sopenharmony_ci * 2598c2ecf20Sopenharmony_ci * This function reads an indexing node from the flash media and fills znode 2608c2ecf20Sopenharmony_ci * with the read data. Returns zero in case of success and a negative error 2618c2ecf20Sopenharmony_ci * code in case of failure. The read indexing node is validated and if anything 2628c2ecf20Sopenharmony_ci * is wrong with it, this function prints complaint messages and returns 2638c2ecf20Sopenharmony_ci * %-EINVAL. 2648c2ecf20Sopenharmony_ci */ 2658c2ecf20Sopenharmony_cistatic int read_znode(struct ubifs_info *c, struct ubifs_zbranch *zzbr, 2668c2ecf20Sopenharmony_ci struct ubifs_znode *znode) 2678c2ecf20Sopenharmony_ci{ 2688c2ecf20Sopenharmony_ci int lnum = zzbr->lnum; 2698c2ecf20Sopenharmony_ci int offs = zzbr->offs; 2708c2ecf20Sopenharmony_ci int len = zzbr->len; 2718c2ecf20Sopenharmony_ci int i, err, type, cmp; 2728c2ecf20Sopenharmony_ci struct ubifs_idx_node *idx; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci idx = kmalloc(c->max_idx_node_sz, GFP_NOFS); 2758c2ecf20Sopenharmony_ci if (!idx) 2768c2ecf20Sopenharmony_ci return -ENOMEM; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs); 2798c2ecf20Sopenharmony_ci if (err < 0) { 2808c2ecf20Sopenharmony_ci kfree(idx); 2818c2ecf20Sopenharmony_ci return err; 2828c2ecf20Sopenharmony_ci } 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci err = ubifs_node_check_hash(c, idx, zzbr->hash); 2858c2ecf20Sopenharmony_ci if (err) { 2868c2ecf20Sopenharmony_ci ubifs_bad_hash(c, idx, zzbr->hash, lnum, offs); 2878c2ecf20Sopenharmony_ci kfree(idx); 2888c2ecf20Sopenharmony_ci return err; 2898c2ecf20Sopenharmony_ci } 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci znode->child_cnt = le16_to_cpu(idx->child_cnt); 2928c2ecf20Sopenharmony_ci znode->level = le16_to_cpu(idx->level); 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci dbg_tnc("LEB %d:%d, level %d, %d branch", 2958c2ecf20Sopenharmony_ci lnum, offs, znode->level, znode->child_cnt); 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci if (znode->child_cnt > c->fanout || znode->level > UBIFS_MAX_LEVELS) { 2988c2ecf20Sopenharmony_ci ubifs_err(c, "current fanout %d, branch count %d", 2998c2ecf20Sopenharmony_ci c->fanout, znode->child_cnt); 3008c2ecf20Sopenharmony_ci ubifs_err(c, "max levels %d, znode level %d", 3018c2ecf20Sopenharmony_ci UBIFS_MAX_LEVELS, znode->level); 3028c2ecf20Sopenharmony_ci err = 1; 3038c2ecf20Sopenharmony_ci goto out_dump; 3048c2ecf20Sopenharmony_ci } 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci for (i = 0; i < znode->child_cnt; i++) { 3078c2ecf20Sopenharmony_ci struct ubifs_branch *br = ubifs_idx_branch(c, idx, i); 3088c2ecf20Sopenharmony_ci struct ubifs_zbranch *zbr = &znode->zbranch[i]; 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci key_read(c, &br->key, &zbr->key); 3118c2ecf20Sopenharmony_ci zbr->lnum = le32_to_cpu(br->lnum); 3128c2ecf20Sopenharmony_ci zbr->offs = le32_to_cpu(br->offs); 3138c2ecf20Sopenharmony_ci zbr->len = le32_to_cpu(br->len); 3148c2ecf20Sopenharmony_ci ubifs_copy_hash(c, ubifs_branch_hash(c, br), zbr->hash); 3158c2ecf20Sopenharmony_ci zbr->znode = NULL; 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci /* Validate branch */ 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci if (zbr->lnum < c->main_first || 3208c2ecf20Sopenharmony_ci zbr->lnum >= c->leb_cnt || zbr->offs < 0 || 3218c2ecf20Sopenharmony_ci zbr->offs + zbr->len > c->leb_size || zbr->offs & 7) { 3228c2ecf20Sopenharmony_ci ubifs_err(c, "bad branch %d", i); 3238c2ecf20Sopenharmony_ci err = 2; 3248c2ecf20Sopenharmony_ci goto out_dump; 3258c2ecf20Sopenharmony_ci } 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci switch (key_type(c, &zbr->key)) { 3288c2ecf20Sopenharmony_ci case UBIFS_INO_KEY: 3298c2ecf20Sopenharmony_ci case UBIFS_DATA_KEY: 3308c2ecf20Sopenharmony_ci case UBIFS_DENT_KEY: 3318c2ecf20Sopenharmony_ci case UBIFS_XENT_KEY: 3328c2ecf20Sopenharmony_ci break; 3338c2ecf20Sopenharmony_ci default: 3348c2ecf20Sopenharmony_ci ubifs_err(c, "bad key type at slot %d: %d", 3358c2ecf20Sopenharmony_ci i, key_type(c, &zbr->key)); 3368c2ecf20Sopenharmony_ci err = 3; 3378c2ecf20Sopenharmony_ci goto out_dump; 3388c2ecf20Sopenharmony_ci } 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci if (znode->level) 3418c2ecf20Sopenharmony_ci continue; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci type = key_type(c, &zbr->key); 3448c2ecf20Sopenharmony_ci if (c->ranges[type].max_len == 0) { 3458c2ecf20Sopenharmony_ci if (zbr->len != c->ranges[type].len) { 3468c2ecf20Sopenharmony_ci ubifs_err(c, "bad target node (type %d) length (%d)", 3478c2ecf20Sopenharmony_ci type, zbr->len); 3488c2ecf20Sopenharmony_ci ubifs_err(c, "have to be %d", c->ranges[type].len); 3498c2ecf20Sopenharmony_ci err = 4; 3508c2ecf20Sopenharmony_ci goto out_dump; 3518c2ecf20Sopenharmony_ci } 3528c2ecf20Sopenharmony_ci } else if (zbr->len < c->ranges[type].min_len || 3538c2ecf20Sopenharmony_ci zbr->len > c->ranges[type].max_len) { 3548c2ecf20Sopenharmony_ci ubifs_err(c, "bad target node (type %d) length (%d)", 3558c2ecf20Sopenharmony_ci type, zbr->len); 3568c2ecf20Sopenharmony_ci ubifs_err(c, "have to be in range of %d-%d", 3578c2ecf20Sopenharmony_ci c->ranges[type].min_len, 3588c2ecf20Sopenharmony_ci c->ranges[type].max_len); 3598c2ecf20Sopenharmony_ci err = 5; 3608c2ecf20Sopenharmony_ci goto out_dump; 3618c2ecf20Sopenharmony_ci } 3628c2ecf20Sopenharmony_ci } 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci /* 3658c2ecf20Sopenharmony_ci * Ensure that the next key is greater or equivalent to the 3668c2ecf20Sopenharmony_ci * previous one. 3678c2ecf20Sopenharmony_ci */ 3688c2ecf20Sopenharmony_ci for (i = 0; i < znode->child_cnt - 1; i++) { 3698c2ecf20Sopenharmony_ci const union ubifs_key *key1, *key2; 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci key1 = &znode->zbranch[i].key; 3728c2ecf20Sopenharmony_ci key2 = &znode->zbranch[i + 1].key; 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci cmp = keys_cmp(c, key1, key2); 3758c2ecf20Sopenharmony_ci if (cmp > 0) { 3768c2ecf20Sopenharmony_ci ubifs_err(c, "bad key order (keys %d and %d)", i, i + 1); 3778c2ecf20Sopenharmony_ci err = 6; 3788c2ecf20Sopenharmony_ci goto out_dump; 3798c2ecf20Sopenharmony_ci } else if (cmp == 0 && !is_hash_key(c, key1)) { 3808c2ecf20Sopenharmony_ci /* These can only be keys with colliding hash */ 3818c2ecf20Sopenharmony_ci ubifs_err(c, "keys %d and %d are not hashed but equivalent", 3828c2ecf20Sopenharmony_ci i, i + 1); 3838c2ecf20Sopenharmony_ci err = 7; 3848c2ecf20Sopenharmony_ci goto out_dump; 3858c2ecf20Sopenharmony_ci } 3868c2ecf20Sopenharmony_ci } 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci kfree(idx); 3898c2ecf20Sopenharmony_ci return 0; 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ciout_dump: 3928c2ecf20Sopenharmony_ci ubifs_err(c, "bad indexing node at LEB %d:%d, error %d", lnum, offs, err); 3938c2ecf20Sopenharmony_ci ubifs_dump_node(c, idx, c->max_idx_node_sz); 3948c2ecf20Sopenharmony_ci kfree(idx); 3958c2ecf20Sopenharmony_ci return -EINVAL; 3968c2ecf20Sopenharmony_ci} 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci/** 3998c2ecf20Sopenharmony_ci * ubifs_load_znode - load znode to TNC cache. 4008c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object 4018c2ecf20Sopenharmony_ci * @zbr: znode branch 4028c2ecf20Sopenharmony_ci * @parent: znode's parent 4038c2ecf20Sopenharmony_ci * @iip: index in parent 4048c2ecf20Sopenharmony_ci * 4058c2ecf20Sopenharmony_ci * This function loads znode pointed to by @zbr into the TNC cache and 4068c2ecf20Sopenharmony_ci * returns pointer to it in case of success and a negative error code in case 4078c2ecf20Sopenharmony_ci * of failure. 4088c2ecf20Sopenharmony_ci */ 4098c2ecf20Sopenharmony_cistruct ubifs_znode *ubifs_load_znode(struct ubifs_info *c, 4108c2ecf20Sopenharmony_ci struct ubifs_zbranch *zbr, 4118c2ecf20Sopenharmony_ci struct ubifs_znode *parent, int iip) 4128c2ecf20Sopenharmony_ci{ 4138c2ecf20Sopenharmony_ci int err; 4148c2ecf20Sopenharmony_ci struct ubifs_znode *znode; 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci ubifs_assert(c, !zbr->znode); 4178c2ecf20Sopenharmony_ci /* 4188c2ecf20Sopenharmony_ci * A slab cache is not presently used for znodes because the znode size 4198c2ecf20Sopenharmony_ci * depends on the fanout which is stored in the superblock. 4208c2ecf20Sopenharmony_ci */ 4218c2ecf20Sopenharmony_ci znode = kzalloc(c->max_znode_sz, GFP_NOFS); 4228c2ecf20Sopenharmony_ci if (!znode) 4238c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci err = read_znode(c, zbr, znode); 4268c2ecf20Sopenharmony_ci if (err) 4278c2ecf20Sopenharmony_ci goto out; 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci atomic_long_inc(&c->clean_zn_cnt); 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci /* 4328c2ecf20Sopenharmony_ci * Increment the global clean znode counter as well. It is OK that 4338c2ecf20Sopenharmony_ci * global and per-FS clean znode counters may be inconsistent for some 4348c2ecf20Sopenharmony_ci * short time (because we might be preempted at this point), the global 4358c2ecf20Sopenharmony_ci * one is only used in shrinker. 4368c2ecf20Sopenharmony_ci */ 4378c2ecf20Sopenharmony_ci atomic_long_inc(&ubifs_clean_zn_cnt); 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci zbr->znode = znode; 4408c2ecf20Sopenharmony_ci znode->parent = parent; 4418c2ecf20Sopenharmony_ci znode->time = ktime_get_seconds(); 4428c2ecf20Sopenharmony_ci znode->iip = iip; 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci return znode; 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_ciout: 4478c2ecf20Sopenharmony_ci kfree(znode); 4488c2ecf20Sopenharmony_ci return ERR_PTR(err); 4498c2ecf20Sopenharmony_ci} 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci/** 4528c2ecf20Sopenharmony_ci * ubifs_tnc_read_node - read a leaf node from the flash media. 4538c2ecf20Sopenharmony_ci * @c: UBIFS file-system description object 4548c2ecf20Sopenharmony_ci * @zbr: key and position of the node 4558c2ecf20Sopenharmony_ci * @node: node is returned here 4568c2ecf20Sopenharmony_ci * 4578c2ecf20Sopenharmony_ci * This function reads a node defined by @zbr from the flash media. Returns 4588c2ecf20Sopenharmony_ci * zero in case of success or a negative negative error code in case of 4598c2ecf20Sopenharmony_ci * failure. 4608c2ecf20Sopenharmony_ci */ 4618c2ecf20Sopenharmony_ciint ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr, 4628c2ecf20Sopenharmony_ci void *node) 4638c2ecf20Sopenharmony_ci{ 4648c2ecf20Sopenharmony_ci union ubifs_key key1, *key = &zbr->key; 4658c2ecf20Sopenharmony_ci int err, type = key_type(c, key); 4668c2ecf20Sopenharmony_ci struct ubifs_wbuf *wbuf; 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci /* 4698c2ecf20Sopenharmony_ci * 'zbr' has to point to on-flash node. The node may sit in a bud and 4708c2ecf20Sopenharmony_ci * may even be in a write buffer, so we have to take care about this. 4718c2ecf20Sopenharmony_ci */ 4728c2ecf20Sopenharmony_ci wbuf = ubifs_get_wbuf(c, zbr->lnum); 4738c2ecf20Sopenharmony_ci if (wbuf) 4748c2ecf20Sopenharmony_ci err = ubifs_read_node_wbuf(wbuf, node, type, zbr->len, 4758c2ecf20Sopenharmony_ci zbr->lnum, zbr->offs); 4768c2ecf20Sopenharmony_ci else 4778c2ecf20Sopenharmony_ci err = ubifs_read_node(c, node, type, zbr->len, zbr->lnum, 4788c2ecf20Sopenharmony_ci zbr->offs); 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci if (err) { 4818c2ecf20Sopenharmony_ci dbg_tnck(key, "key "); 4828c2ecf20Sopenharmony_ci return err; 4838c2ecf20Sopenharmony_ci } 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci /* Make sure the key of the read node is correct */ 4868c2ecf20Sopenharmony_ci key_read(c, node + UBIFS_KEY_OFFSET, &key1); 4878c2ecf20Sopenharmony_ci if (!keys_eq(c, key, &key1)) { 4888c2ecf20Sopenharmony_ci ubifs_err(c, "bad key in node at LEB %d:%d", 4898c2ecf20Sopenharmony_ci zbr->lnum, zbr->offs); 4908c2ecf20Sopenharmony_ci dbg_tnck(key, "looked for key "); 4918c2ecf20Sopenharmony_ci dbg_tnck(&key1, "but found node's key "); 4928c2ecf20Sopenharmony_ci ubifs_dump_node(c, node, zbr->len); 4938c2ecf20Sopenharmony_ci return -EINVAL; 4948c2ecf20Sopenharmony_ci } 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_ci err = ubifs_node_check_hash(c, node, zbr->hash); 4978c2ecf20Sopenharmony_ci if (err) { 4988c2ecf20Sopenharmony_ci ubifs_bad_hash(c, node, zbr->hash, zbr->lnum, zbr->offs); 4998c2ecf20Sopenharmony_ci return err; 5008c2ecf20Sopenharmony_ci } 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci return 0; 5038c2ecf20Sopenharmony_ci} 504