Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * This file is part of UBIFS.
0004  *
0005  * Copyright (C) 2006-2008 Nokia Corporation.
0006  *
0007  * Authors: Adrian Hunter
0008  *          Artem Bityutskiy (Битюцкий Артём)
0009  */
0010 
0011 /*
0012  * This file contains miscelanious TNC-related functions shared betweend
0013  * different files. This file does not form any logically separate TNC
0014  * sub-system. The file was created because there is a lot of TNC code and
0015  * putting it all in one file would make that file too big and unreadable.
0016  */
0017 
0018 #include "ubifs.h"
0019 
0020 /**
0021  * ubifs_tnc_levelorder_next - next TNC tree element in levelorder traversal.
0022  * @c: UBIFS file-system description object
0023  * @zr: root of the subtree to traverse
0024  * @znode: previous znode
0025  *
0026  * This function implements levelorder TNC traversal. The LNC is ignored.
0027  * Returns the next element or %NULL if @znode is already the last one.
0028  */
0029 struct ubifs_znode *ubifs_tnc_levelorder_next(const struct ubifs_info *c,
0030                           struct ubifs_znode *zr,
0031                           struct ubifs_znode *znode)
0032 {
0033     int level, iip, level_search = 0;
0034     struct ubifs_znode *zn;
0035 
0036     ubifs_assert(c, zr);
0037 
0038     if (unlikely(!znode))
0039         return zr;
0040 
0041     if (unlikely(znode == zr)) {
0042         if (znode->level == 0)
0043             return NULL;
0044         return ubifs_tnc_find_child(zr, 0);
0045     }
0046 
0047     level = znode->level;
0048 
0049     iip = znode->iip;
0050     while (1) {
0051         ubifs_assert(c, znode->level <= zr->level);
0052 
0053         /*
0054          * First walk up until there is a znode with next branch to
0055          * look at.
0056          */
0057         while (znode->parent != zr && iip >= znode->parent->child_cnt) {
0058             znode = znode->parent;
0059             iip = znode->iip;
0060         }
0061 
0062         if (unlikely(znode->parent == zr &&
0063                  iip >= znode->parent->child_cnt)) {
0064             /* This level is done, switch to the lower one */
0065             level -= 1;
0066             if (level_search || level < 0)
0067                 /*
0068                  * We were already looking for znode at lower
0069                  * level ('level_search'). As we are here
0070                  * again, it just does not exist. Or all levels
0071                  * were finished ('level < 0').
0072                  */
0073                 return NULL;
0074 
0075             level_search = 1;
0076             iip = -1;
0077             znode = ubifs_tnc_find_child(zr, 0);
0078             ubifs_assert(c, znode);
0079         }
0080 
0081         /* Switch to the next index */
0082         zn = ubifs_tnc_find_child(znode->parent, iip + 1);
0083         if (!zn) {
0084             /* No more children to look at, we have walk up */
0085             iip = znode->parent->child_cnt;
0086             continue;
0087         }
0088 
0089         /* Walk back down to the level we came from ('level') */
0090         while (zn->level != level) {
0091             znode = zn;
0092             zn = ubifs_tnc_find_child(zn, 0);
0093             if (!zn) {
0094                 /*
0095                  * This path is not too deep so it does not
0096                  * reach 'level'. Try next path.
0097                  */
0098                 iip = znode->iip;
0099                 break;
0100             }
0101         }
0102 
0103         if (zn) {
0104             ubifs_assert(c, zn->level >= 0);
0105             return zn;
0106         }
0107     }
0108 }
0109 
0110 /**
0111  * ubifs_search_zbranch - search znode branch.
0112  * @c: UBIFS file-system description object
0113  * @znode: znode to search in
0114  * @key: key to search for
0115  * @n: znode branch slot number is returned here
0116  *
0117  * This is a helper function which search branch with key @key in @znode using
0118  * binary search. The result of the search may be:
0119  *   o exact match, then %1 is returned, and the slot number of the branch is
0120  *     stored in @n;
0121  *   o no exact match, then %0 is returned and the slot number of the left
0122  *     closest branch is returned in @n; the slot if all keys in this znode are
0123  *     greater than @key, then %-1 is returned in @n.
0124  */
0125 int ubifs_search_zbranch(const struct ubifs_info *c,
0126              const struct ubifs_znode *znode,
0127              const union ubifs_key *key, int *n)
0128 {
0129     int beg = 0, end = znode->child_cnt, mid;
0130     int cmp;
0131     const struct ubifs_zbranch *zbr = &znode->zbranch[0];
0132 
0133     ubifs_assert(c, end > beg);
0134 
0135     while (end > beg) {
0136         mid = (beg + end) >> 1;
0137         cmp = keys_cmp(c, key, &zbr[mid].key);
0138         if (cmp > 0)
0139             beg = mid + 1;
0140         else if (cmp < 0)
0141             end = mid;
0142         else {
0143             *n = mid;
0144             return 1;
0145         }
0146     }
0147 
0148     *n = end - 1;
0149 
0150     /* The insert point is after *n */
0151     ubifs_assert(c, *n >= -1 && *n < znode->child_cnt);
0152     if (*n == -1)
0153         ubifs_assert(c, keys_cmp(c, key, &zbr[0].key) < 0);
0154     else
0155         ubifs_assert(c, keys_cmp(c, key, &zbr[*n].key) > 0);
0156     if (*n + 1 < znode->child_cnt)
0157         ubifs_assert(c, keys_cmp(c, key, &zbr[*n + 1].key) < 0);
0158 
0159     return 0;
0160 }
0161 
0162 /**
0163  * ubifs_tnc_postorder_first - find first znode to do postorder tree traversal.
0164  * @znode: znode to start at (root of the sub-tree to traverse)
0165  *
0166  * Find the lowest leftmost znode in a subtree of the TNC tree. The LNC is
0167  * ignored.
0168  */
0169 struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode)
0170 {
0171     if (unlikely(!znode))
0172         return NULL;
0173 
0174     while (znode->level > 0) {
0175         struct ubifs_znode *child;
0176 
0177         child = ubifs_tnc_find_child(znode, 0);
0178         if (!child)
0179             return znode;
0180         znode = child;
0181     }
0182 
0183     return znode;
0184 }
0185 
0186 /**
0187  * ubifs_tnc_postorder_next - next TNC tree element in postorder traversal.
0188  * @c: UBIFS file-system description object
0189  * @znode: previous znode
0190  *
0191  * This function implements postorder TNC traversal. The LNC is ignored.
0192  * Returns the next element or %NULL if @znode is already the last one.
0193  */
0194 struct ubifs_znode *ubifs_tnc_postorder_next(const struct ubifs_info *c,
0195                          struct ubifs_znode *znode)
0196 {
0197     struct ubifs_znode *zn;
0198 
0199     ubifs_assert(c, znode);
0200     if (unlikely(!znode->parent))
0201         return NULL;
0202 
0203     /* Switch to the next index in the parent */
0204     zn = ubifs_tnc_find_child(znode->parent, znode->iip + 1);
0205     if (!zn)
0206         /* This is in fact the last child, return parent */
0207         return znode->parent;
0208 
0209     /* Go to the first znode in this new subtree */
0210     return ubifs_tnc_postorder_first(zn);
0211 }
0212 
0213 /**
0214  * ubifs_destroy_tnc_subtree - destroy all znodes connected to a subtree.
0215  * @c: UBIFS file-system description object
0216  * @znode: znode defining subtree to destroy
0217  *
0218  * This function destroys subtree of the TNC tree. Returns number of clean
0219  * znodes in the subtree.
0220  */
0221 long ubifs_destroy_tnc_subtree(const struct ubifs_info *c,
0222                    struct ubifs_znode *znode)
0223 {
0224     struct ubifs_znode *zn = ubifs_tnc_postorder_first(znode);
0225     long clean_freed = 0;
0226     int n;
0227 
0228     ubifs_assert(c, zn);
0229     while (1) {
0230         for (n = 0; n < zn->child_cnt; n++) {
0231             if (!zn->zbranch[n].znode)
0232                 continue;
0233 
0234             if (zn->level > 0 &&
0235                 !ubifs_zn_dirty(zn->zbranch[n].znode))
0236                 clean_freed += 1;
0237 
0238             cond_resched();
0239             kfree(zn->zbranch[n].znode);
0240         }
0241 
0242         if (zn == znode) {
0243             if (!ubifs_zn_dirty(zn))
0244                 clean_freed += 1;
0245             kfree(zn);
0246             return clean_freed;
0247         }
0248 
0249         zn = ubifs_tnc_postorder_next(c, zn);
0250     }
0251 }
0252 
0253 /**
0254  * read_znode - read an indexing node from flash and fill znode.
0255  * @c: UBIFS file-system description object
0256  * @zzbr: the zbranch describing the node to read
0257  * @znode: znode to read to
0258  *
0259  * This function reads an indexing node from the flash media and fills znode
0260  * with the read data. Returns zero in case of success and a negative error
0261  * code in case of failure. The read indexing node is validated and if anything
0262  * is wrong with it, this function prints complaint messages and returns
0263  * %-EINVAL.
0264  */
0265 static int read_znode(struct ubifs_info *c, struct ubifs_zbranch *zzbr,
0266               struct ubifs_znode *znode)
0267 {
0268     int lnum = zzbr->lnum;
0269     int offs = zzbr->offs;
0270     int len = zzbr->len;
0271     int i, err, type, cmp;
0272     struct ubifs_idx_node *idx;
0273 
0274     idx = kmalloc(c->max_idx_node_sz, GFP_NOFS);
0275     if (!idx)
0276         return -ENOMEM;
0277 
0278     err = ubifs_read_node(c, idx, UBIFS_IDX_NODE, len, lnum, offs);
0279     if (err < 0) {
0280         kfree(idx);
0281         return err;
0282     }
0283 
0284     err = ubifs_node_check_hash(c, idx, zzbr->hash);
0285     if (err) {
0286         ubifs_bad_hash(c, idx, zzbr->hash, lnum, offs);
0287         kfree(idx);
0288         return err;
0289     }
0290 
0291     znode->child_cnt = le16_to_cpu(idx->child_cnt);
0292     znode->level = le16_to_cpu(idx->level);
0293 
0294     dbg_tnc("LEB %d:%d, level %d, %d branch",
0295         lnum, offs, znode->level, znode->child_cnt);
0296 
0297     if (znode->child_cnt > c->fanout || znode->level > UBIFS_MAX_LEVELS) {
0298         ubifs_err(c, "current fanout %d, branch count %d",
0299               c->fanout, znode->child_cnt);
0300         ubifs_err(c, "max levels %d, znode level %d",
0301               UBIFS_MAX_LEVELS, znode->level);
0302         err = 1;
0303         goto out_dump;
0304     }
0305 
0306     for (i = 0; i < znode->child_cnt; i++) {
0307         struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
0308         struct ubifs_zbranch *zbr = &znode->zbranch[i];
0309 
0310         key_read(c, &br->key, &zbr->key);
0311         zbr->lnum = le32_to_cpu(br->lnum);
0312         zbr->offs = le32_to_cpu(br->offs);
0313         zbr->len  = le32_to_cpu(br->len);
0314         ubifs_copy_hash(c, ubifs_branch_hash(c, br), zbr->hash);
0315         zbr->znode = NULL;
0316 
0317         /* Validate branch */
0318 
0319         if (zbr->lnum < c->main_first ||
0320             zbr->lnum >= c->leb_cnt || zbr->offs < 0 ||
0321             zbr->offs + zbr->len > c->leb_size || zbr->offs & 7) {
0322             ubifs_err(c, "bad branch %d", i);
0323             err = 2;
0324             goto out_dump;
0325         }
0326 
0327         switch (key_type(c, &zbr->key)) {
0328         case UBIFS_INO_KEY:
0329         case UBIFS_DATA_KEY:
0330         case UBIFS_DENT_KEY:
0331         case UBIFS_XENT_KEY:
0332             break;
0333         default:
0334             ubifs_err(c, "bad key type at slot %d: %d",
0335                   i, key_type(c, &zbr->key));
0336             err = 3;
0337             goto out_dump;
0338         }
0339 
0340         if (znode->level)
0341             continue;
0342 
0343         type = key_type(c, &zbr->key);
0344         if (c->ranges[type].max_len == 0) {
0345             if (zbr->len != c->ranges[type].len) {
0346                 ubifs_err(c, "bad target node (type %d) length (%d)",
0347                       type, zbr->len);
0348                 ubifs_err(c, "have to be %d", c->ranges[type].len);
0349                 err = 4;
0350                 goto out_dump;
0351             }
0352         } else if (zbr->len < c->ranges[type].min_len ||
0353                zbr->len > c->ranges[type].max_len) {
0354             ubifs_err(c, "bad target node (type %d) length (%d)",
0355                   type, zbr->len);
0356             ubifs_err(c, "have to be in range of %d-%d",
0357                   c->ranges[type].min_len,
0358                   c->ranges[type].max_len);
0359             err = 5;
0360             goto out_dump;
0361         }
0362     }
0363 
0364     /*
0365      * Ensure that the next key is greater or equivalent to the
0366      * previous one.
0367      */
0368     for (i = 0; i < znode->child_cnt - 1; i++) {
0369         const union ubifs_key *key1, *key2;
0370 
0371         key1 = &znode->zbranch[i].key;
0372         key2 = &znode->zbranch[i + 1].key;
0373 
0374         cmp = keys_cmp(c, key1, key2);
0375         if (cmp > 0) {
0376             ubifs_err(c, "bad key order (keys %d and %d)", i, i + 1);
0377             err = 6;
0378             goto out_dump;
0379         } else if (cmp == 0 && !is_hash_key(c, key1)) {
0380             /* These can only be keys with colliding hash */
0381             ubifs_err(c, "keys %d and %d are not hashed but equivalent",
0382                   i, i + 1);
0383             err = 7;
0384             goto out_dump;
0385         }
0386     }
0387 
0388     kfree(idx);
0389     return 0;
0390 
0391 out_dump:
0392     ubifs_err(c, "bad indexing node at LEB %d:%d, error %d", lnum, offs, err);
0393     ubifs_dump_node(c, idx, c->max_idx_node_sz);
0394     kfree(idx);
0395     return -EINVAL;
0396 }
0397 
0398 /**
0399  * ubifs_load_znode - load znode to TNC cache.
0400  * @c: UBIFS file-system description object
0401  * @zbr: znode branch
0402  * @parent: znode's parent
0403  * @iip: index in parent
0404  *
0405  * This function loads znode pointed to by @zbr into the TNC cache and
0406  * returns pointer to it in case of success and a negative error code in case
0407  * of failure.
0408  */
0409 struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
0410                      struct ubifs_zbranch *zbr,
0411                      struct ubifs_znode *parent, int iip)
0412 {
0413     int err;
0414     struct ubifs_znode *znode;
0415 
0416     ubifs_assert(c, !zbr->znode);
0417     /*
0418      * A slab cache is not presently used for znodes because the znode size
0419      * depends on the fanout which is stored in the superblock.
0420      */
0421     znode = kzalloc(c->max_znode_sz, GFP_NOFS);
0422     if (!znode)
0423         return ERR_PTR(-ENOMEM);
0424 
0425     err = read_znode(c, zbr, znode);
0426     if (err)
0427         goto out;
0428 
0429     atomic_long_inc(&c->clean_zn_cnt);
0430 
0431     /*
0432      * Increment the global clean znode counter as well. It is OK that
0433      * global and per-FS clean znode counters may be inconsistent for some
0434      * short time (because we might be preempted at this point), the global
0435      * one is only used in shrinker.
0436      */
0437     atomic_long_inc(&ubifs_clean_zn_cnt);
0438 
0439     zbr->znode = znode;
0440     znode->parent = parent;
0441     znode->time = ktime_get_seconds();
0442     znode->iip = iip;
0443 
0444     return znode;
0445 
0446 out:
0447     kfree(znode);
0448     return ERR_PTR(err);
0449 }
0450 
0451 /**
0452  * ubifs_tnc_read_node - read a leaf node from the flash media.
0453  * @c: UBIFS file-system description object
0454  * @zbr: key and position of the node
0455  * @node: node is returned here
0456  *
0457  * This function reads a node defined by @zbr from the flash media. Returns
0458  * zero in case of success or a negative error code in case of failure.
0459  */
0460 int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
0461             void *node)
0462 {
0463     union ubifs_key key1, *key = &zbr->key;
0464     int err, type = key_type(c, key);
0465     struct ubifs_wbuf *wbuf;
0466 
0467     /*
0468      * 'zbr' has to point to on-flash node. The node may sit in a bud and
0469      * may even be in a write buffer, so we have to take care about this.
0470      */
0471     wbuf = ubifs_get_wbuf(c, zbr->lnum);
0472     if (wbuf)
0473         err = ubifs_read_node_wbuf(wbuf, node, type, zbr->len,
0474                        zbr->lnum, zbr->offs);
0475     else
0476         err = ubifs_read_node(c, node, type, zbr->len, zbr->lnum,
0477                       zbr->offs);
0478 
0479     if (err) {
0480         dbg_tnck(key, "key ");
0481         return err;
0482     }
0483 
0484     /* Make sure the key of the read node is correct */
0485     key_read(c, node + UBIFS_KEY_OFFSET, &key1);
0486     if (!keys_eq(c, key, &key1)) {
0487         ubifs_err(c, "bad key in node at LEB %d:%d",
0488               zbr->lnum, zbr->offs);
0489         dbg_tnck(key, "looked for key ");
0490         dbg_tnck(&key1, "but found node's key ");
0491         ubifs_dump_node(c, node, zbr->len);
0492         return -EINVAL;
0493     }
0494 
0495     err = ubifs_node_check_hash(c, node, zbr->hash);
0496     if (err) {
0497         ubifs_bad_hash(c, node, zbr->hash, zbr->lnum, zbr->offs);
0498         return err;
0499     }
0500 
0501     return 0;
0502 }