cbfs: Record the start address in cbfs_priv
[oweals/u-boot.git] / fs / ubifs / lpt.c
index c49d3b0687f5e11d395c476a089e1621e95b33a5..62748b0210b2040b6b9811fce27eca7606e6b051 100644 (file)
@@ -1,10 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * This file is part of UBIFS.
  *
  * Copyright (C) 2006-2008 Nokia Corporation.
  *
- * SPDX-License-Identifier:    GPL-2.0+
- *
  * Authors: Adrian Hunter
  *          Artem Bityutskiy (Битюцкий Артём)
  */
@@ -33,8 +32,9 @@
  */
 
 #include "ubifs.h"
-#define __UBOOT__
 #ifndef __UBOOT__
+#include <log.h>
+#include <dm/devres.h>
 #include <linux/crc16.h>
 #include <linux/math64.h>
 #include <linux/slab.h>
@@ -142,13 +142,13 @@ int ubifs_calc_lpt_geom(struct ubifs_info *c)
        sz = c->lpt_sz * 2; /* Must have at least 2 times the size */
        lebs_needed = div_u64(sz + c->leb_size - 1, c->leb_size);
        if (lebs_needed > c->lpt_lebs) {
-               ubifs_err("too few LPT LEBs");
+               ubifs_err(c, "too few LPT LEBs");
                return -EINVAL;
        }
 
        /* Verify that ltab fits in a single LEB (since ltab is a single node */
        if (c->ltab_sz > c->leb_size) {
-               ubifs_err("LPT ltab too big");
+               ubifs_err(c, "LPT ltab too big");
                return -EINVAL;
        }
 
@@ -210,7 +210,7 @@ static int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs,
                        continue;
                }
                if (c->ltab_sz > c->leb_size) {
-                       ubifs_err("LPT ltab too big");
+                       ubifs_err(c, "LPT ltab too big");
                        return -EINVAL;
                }
                *main_lebs = c->main_lebs;
@@ -908,7 +908,7 @@ static void replace_cats(struct ubifs_info *c, struct ubifs_pnode *old_pnode,
  *
  * This function returns %0 on success and a negative error code on failure.
  */
-static int check_lpt_crc(void *buf, int len)
+static int check_lpt_crc(const struct ubifs_info *c, void *buf, int len)
 {
        int pos = 0;
        uint8_t *addr = buf;
@@ -918,8 +918,8 @@ static int check_lpt_crc(void *buf, int len)
        calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
                         len - UBIFS_LPT_CRC_BYTES);
        if (crc != calc_crc) {
-               ubifs_err("invalid crc in LPT node: crc %hx calc %hx", crc,
-                         calc_crc);
+               ubifs_err(c, "invalid crc in LPT node: crc %hx calc %hx",
+                         crc, calc_crc);
                dump_stack();
                return -EINVAL;
        }
@@ -935,14 +935,15 @@ static int check_lpt_crc(void *buf, int len)
  *
  * This function returns %0 on success and a negative error code on failure.
  */
-static int check_lpt_type(uint8_t **addr, int *pos, int type)
+static int check_lpt_type(const struct ubifs_info *c, uint8_t **addr,
+                         int *pos, int type)
 {
        int node_type;
 
        node_type = ubifs_unpack_bits(addr, pos, UBIFS_LPT_TYPE_BITS);
        if (node_type != type) {
-               ubifs_err("invalid type (%d) in LPT node type %d", node_type,
-                         type);
+               ubifs_err(c, "invalid type (%d) in LPT node type %d",
+                         node_type, type);
                dump_stack();
                return -EINVAL;
        }
@@ -963,7 +964,7 @@ static int unpack_pnode(const struct ubifs_info *c, void *buf,
        uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
        int i, pos = 0, err;
 
-       err = check_lpt_type(&addr, &pos, UBIFS_LPT_PNODE);
+       err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_PNODE);
        if (err)
                return err;
        if (c->big_lpt)
@@ -982,7 +983,7 @@ static int unpack_pnode(const struct ubifs_info *c, void *buf,
                        lprops->flags = 0;
                lprops->flags |= ubifs_categorize_lprops(c, lprops);
        }
-       err = check_lpt_crc(buf, c->pnode_sz);
+       err = check_lpt_crc(c, buf, c->pnode_sz);
        return err;
 }
 
@@ -1000,7 +1001,7 @@ int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
        uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
        int i, pos = 0, err;
 
-       err = check_lpt_type(&addr, &pos, UBIFS_LPT_NNODE);
+       err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_NNODE);
        if (err)
                return err;
        if (c->big_lpt)
@@ -1016,7 +1017,7 @@ int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
                nnode->nbranch[i].offs = ubifs_unpack_bits(&addr, &pos,
                                                     c->lpt_offs_bits);
        }
-       err = check_lpt_crc(buf, c->nnode_sz);
+       err = check_lpt_crc(c, buf, c->nnode_sz);
        return err;
 }
 
@@ -1032,7 +1033,7 @@ static int unpack_ltab(const struct ubifs_info *c, void *buf)
        uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
        int i, pos = 0, err;
 
-       err = check_lpt_type(&addr, &pos, UBIFS_LPT_LTAB);
+       err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_LTAB);
        if (err)
                return err;
        for (i = 0; i < c->lpt_lebs; i++) {
@@ -1048,7 +1049,7 @@ static int unpack_ltab(const struct ubifs_info *c, void *buf)
                c->ltab[i].tgc = 0;
                c->ltab[i].cmt = 0;
        }
-       err = check_lpt_crc(buf, c->ltab_sz);
+       err = check_lpt_crc(c, buf, c->ltab_sz);
        return err;
 }
 
@@ -1065,7 +1066,7 @@ static int unpack_lsave(const struct ubifs_info *c, void *buf)
        uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
        int i, pos = 0, err;
 
-       err = check_lpt_type(&addr, &pos, UBIFS_LPT_LSAVE);
+       err = check_lpt_type(c, &addr, &pos, UBIFS_LPT_LSAVE);
        if (err)
                return err;
        for (i = 0; i < c->lsave_cnt; i++) {
@@ -1075,7 +1076,7 @@ static int unpack_lsave(const struct ubifs_info *c, void *buf)
                        return -EINVAL;
                c->lsave[i] = lnum;
        }
-       err = check_lpt_crc(buf, c->lsave_sz);
+       err = check_lpt_crc(c, buf, c->lsave_sz);
        return err;
 }
 #endif
@@ -1242,7 +1243,7 @@ int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
        return 0;
 
 out:
-       ubifs_err("error %d reading nnode at %d:%d", err, lnum, offs);
+       ubifs_err(c, "error %d reading nnode at %d:%d", err, lnum, offs);
        dump_stack();
        kfree(nnode);
        return err;
@@ -1307,10 +1308,10 @@ static int read_pnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
        return 0;
 
 out:
-       ubifs_err("error %d reading pnode at %d:%d", err, lnum, offs);
+       ubifs_err(c, "error %d reading pnode at %d:%d", err, lnum, offs);
        ubifs_dump_pnode(c, pnode, parent, iip);
        dump_stack();
-       ubifs_err("calc num: %d", calc_pnode_num_from_parent(c, parent, iip));
+       ubifs_err(c, "calc num: %d", calc_pnode_num_from_parent(c, parent, iip));
        kfree(pnode);
        return err;
 }
@@ -1465,7 +1466,6 @@ struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum)
                        return ERR_CAST(nnode);
        }
        iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
-       shft -= UBIFS_LPT_FANOUT_SHIFT;
        pnode = ubifs_get_pnode(c, nnode, iip);
        if (IS_ERR(pnode))
                return ERR_CAST(pnode);
@@ -1605,7 +1605,6 @@ struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum)
                        return ERR_CAST(nnode);
        }
        iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
-       shft -= UBIFS_LPT_FANOUT_SHIFT;
        pnode = ubifs_get_pnode(c, nnode, iip);
        if (IS_ERR(pnode))
                return ERR_CAST(pnode);
@@ -1971,7 +1970,6 @@ again:
                }
        }
        iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1));
-       shft -= UBIFS_LPT_FANOUT_SHIFT;
        pnode = scan_get_pnode(c, path + h, nnode, iip);
        if (IS_ERR(pnode)) {
                err = PTR_ERR(pnode);
@@ -2105,7 +2103,7 @@ static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
        int i;
 
        if (pnode->num != col) {
-               ubifs_err("pnode num %d expected %d parent num %d iip %d",
+               ubifs_err(c, "pnode num %d expected %d parent num %d iip %d",
                          pnode->num, col, pnode->parent->num, pnode->iip);
                return -EINVAL;
        }
@@ -2120,13 +2118,13 @@ static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
                if (lnum >= c->leb_cnt)
                        continue;
                if (lprops->lnum != lnum) {
-                       ubifs_err("bad LEB number %d expected %d",
+                       ubifs_err(c, "bad LEB number %d expected %d",
                                  lprops->lnum, lnum);
                        return -EINVAL;
                }
                if (lprops->flags & LPROPS_TAKEN) {
                        if (cat != LPROPS_UNCAT) {
-                               ubifs_err("LEB %d taken but not uncat %d",
+                               ubifs_err(c, "LEB %d taken but not uncat %d",
                                          lprops->lnum, cat);
                                return -EINVAL;
                        }
@@ -2139,7 +2137,7 @@ static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
                        case LPROPS_FRDI_IDX:
                                break;
                        default:
-                               ubifs_err("LEB %d index but cat %d",
+                               ubifs_err(c, "LEB %d index but cat %d",
                                          lprops->lnum, cat);
                                return -EINVAL;
                        }
@@ -2152,7 +2150,7 @@ static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
                        case LPROPS_FREEABLE:
                                break;
                        default:
-                               ubifs_err("LEB %d not index but cat %d",
+                               ubifs_err(c, "LEB %d not index but cat %d",
                                          lprops->lnum, cat);
                                return -EINVAL;
                        }
@@ -2193,26 +2191,28 @@ static int dbg_chk_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode,
                        break;
                }
                if (!found) {
-                       ubifs_err("LEB %d cat %d not found in cat heap/list",
+                       ubifs_err(c, "LEB %d cat %d not found in cat heap/list",
                                  lprops->lnum, cat);
                        return -EINVAL;
                }
                switch (cat) {
                case LPROPS_EMPTY:
                        if (lprops->free != c->leb_size) {
-                               ubifs_err("LEB %d cat %d free %d dirty %d",
+                               ubifs_err(c, "LEB %d cat %d free %d dirty %d",
                                          lprops->lnum, cat, lprops->free,
                                          lprops->dirty);
                                return -EINVAL;
                        }
+                       break;
                case LPROPS_FREEABLE:
                case LPROPS_FRDI_IDX:
                        if (lprops->free + lprops->dirty != c->leb_size) {
-                               ubifs_err("LEB %d cat %d free %d dirty %d",
+                               ubifs_err(c, "LEB %d cat %d free %d dirty %d",
                                          lprops->lnum, cat, lprops->free,
                                          lprops->dirty);
                                return -EINVAL;
                        }
+                       break;
                }
        }
        return 0;
@@ -2244,7 +2244,7 @@ int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode,
                        /* cnode is a nnode */
                        num = calc_nnode_num(row, col);
                        if (cnode->num != num) {
-                               ubifs_err("nnode num %d expected %d parent num %d iip %d",
+                               ubifs_err(c, "nnode num %d expected %d parent num %d iip %d",
                                          cnode->num, num,
                                          (nnode ? nnode->num : 0), cnode->iip);
                                return -EINVAL;