Merge branch 'bkl/ioctl' of git://git.kernel.org/pub/scm/linux/kernel/git/frederic...
[safe/jmp/linux-2.6] / fs / ubifs / lpt_commit.c
index 5f0b83e..13cb7a4 100644 (file)
@@ -26,6 +26,7 @@
  */
 
 #include <linux/crc16.h>
+#include <linux/slab.h>
 #include "ubifs.h"
 
 /**
@@ -177,8 +178,6 @@ static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
                        return 0;
                }
        }
-       dbg_err("last LEB %d", *lnum);
-       dump_stack();
        return -ENOSPC;
 }
 
@@ -193,6 +192,9 @@ static int layout_cnodes(struct ubifs_info *c)
        int lnum, offs, len, alen, done_lsave, done_ltab, err;
        struct ubifs_cnode *cnode;
 
+       err = dbg_chk_lpt_sz(c, 0, 0);
+       if (err)
+               return err;
        cnode = c->lpt_cnext;
        if (!cnode)
                return 0;
@@ -206,6 +208,7 @@ static int layout_cnodes(struct ubifs_info *c)
                c->lsave_lnum = lnum;
                c->lsave_offs = offs;
                offs += c->lsave_sz;
+               dbg_chk_lpt_sz(c, 1, c->lsave_sz);
        }
 
        if (offs + c->ltab_sz <= c->leb_size) {
@@ -213,6 +216,7 @@ static int layout_cnodes(struct ubifs_info *c)
                c->ltab_lnum = lnum;
                c->ltab_offs = offs;
                offs += c->ltab_sz;
+               dbg_chk_lpt_sz(c, 1, c->ltab_sz);
        }
 
        do {
@@ -226,9 +230,10 @@ static int layout_cnodes(struct ubifs_info *c)
                while (offs + len > c->leb_size) {
                        alen = ALIGN(offs, c->min_io_size);
                        upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
+                       dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
                        err = alloc_lpt_leb(c, &lnum);
                        if (err)
-                               return err;
+                               goto no_space;
                        offs = 0;
                        ubifs_assert(lnum >= c->lpt_first &&
                                     lnum <= c->lpt_last);
@@ -238,6 +243,7 @@ static int layout_cnodes(struct ubifs_info *c)
                                c->lsave_lnum = lnum;
                                c->lsave_offs = offs;
                                offs += c->lsave_sz;
+                               dbg_chk_lpt_sz(c, 1, c->lsave_sz);
                                continue;
                        }
                        if (!done_ltab) {
@@ -245,6 +251,7 @@ static int layout_cnodes(struct ubifs_info *c)
                                c->ltab_lnum = lnum;
                                c->ltab_offs = offs;
                                offs += c->ltab_sz;
+                               dbg_chk_lpt_sz(c, 1, c->ltab_sz);
                                continue;
                        }
                        break;
@@ -257,6 +264,7 @@ static int layout_cnodes(struct ubifs_info *c)
                        c->lpt_offs = offs;
                }
                offs += len;
+               dbg_chk_lpt_sz(c, 1, len);
                cnode = cnode->cnext;
        } while (cnode && cnode != c->lpt_cnext);
 
@@ -265,9 +273,10 @@ static int layout_cnodes(struct ubifs_info *c)
                if (offs + c->lsave_sz > c->leb_size) {
                        alen = ALIGN(offs, c->min_io_size);
                        upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
+                       dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
                        err = alloc_lpt_leb(c, &lnum);
                        if (err)
-                               return err;
+                               goto no_space;
                        offs = 0;
                        ubifs_assert(lnum >= c->lpt_first &&
                                     lnum <= c->lpt_last);
@@ -276,6 +285,7 @@ static int layout_cnodes(struct ubifs_info *c)
                c->lsave_lnum = lnum;
                c->lsave_offs = offs;
                offs += c->lsave_sz;
+               dbg_chk_lpt_sz(c, 1, c->lsave_sz);
        }
 
        /* Make sure to place LPT's own lprops table */
@@ -283,9 +293,10 @@ static int layout_cnodes(struct ubifs_info *c)
                if (offs + c->ltab_sz > c->leb_size) {
                        alen = ALIGN(offs, c->min_io_size);
                        upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
+                       dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
                        err = alloc_lpt_leb(c, &lnum);
                        if (err)
-                               return err;
+                               goto no_space;
                        offs = 0;
                        ubifs_assert(lnum >= c->lpt_first &&
                                     lnum <= c->lpt_last);
@@ -294,11 +305,25 @@ static int layout_cnodes(struct ubifs_info *c)
                c->ltab_lnum = lnum;
                c->ltab_offs = offs;
                offs += c->ltab_sz;
+               dbg_chk_lpt_sz(c, 1, c->ltab_sz);
        }
 
        alen = ALIGN(offs, c->min_io_size);
        upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
+       dbg_chk_lpt_sz(c, 4, alen - offs);
+       err = dbg_chk_lpt_sz(c, 3, alen);
+       if (err)
+               return err;
        return 0;
+
+no_space:
+       ubifs_err("LPT out of space");
+       dbg_err("LPT out of space at LEB %d:%d needing %d, done_ltab %d, "
+               "done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
+       dbg_dump_lpt_info(c);
+       dbg_dump_lpt_lebs(c);
+       dump_stack();
+       return err;
 }
 
 /**
@@ -333,8 +358,6 @@ static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
                        *lnum = i + c->lpt_first;
                        return 0;
                }
-       dbg_err("last LEB %d", *lnum);
-       dump_stack();
        return -ENOSPC;
 }
 
@@ -369,12 +392,14 @@ static int write_cnodes(struct ubifs_info *c)
                done_lsave = 1;
                ubifs_pack_lsave(c, buf + offs, c->lsave);
                offs += c->lsave_sz;
+               dbg_chk_lpt_sz(c, 1, c->lsave_sz);
        }
 
        if (offs + c->ltab_sz <= c->leb_size) {
                done_ltab = 1;
                ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
                offs += c->ltab_sz;
+               dbg_chk_lpt_sz(c, 1, c->ltab_sz);
        }
 
        /* Loop for each cnode */
@@ -393,11 +418,11 @@ static int write_cnodes(struct ubifs_info *c)
                                if (err)
                                        return err;
                        }
+                       dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
                        err = realloc_lpt_leb(c, &lnum);
                        if (err)
-                               return err;
-                       offs = 0;
-                       from = 0;
+                               goto no_space;
+                       offs = from = 0;
                        ubifs_assert(lnum >= c->lpt_first &&
                                     lnum <= c->lpt_last);
                        err = ubifs_leb_unmap(c, lnum);
@@ -408,12 +433,14 @@ static int write_cnodes(struct ubifs_info *c)
                                done_lsave = 1;
                                ubifs_pack_lsave(c, buf + offs, c->lsave);
                                offs += c->lsave_sz;
+                               dbg_chk_lpt_sz(c, 1, c->lsave_sz);
                                continue;
                        }
                        if (!done_ltab) {
                                done_ltab = 1;
                                ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
                                offs += c->ltab_sz;
+                               dbg_chk_lpt_sz(c, 1, c->ltab_sz);
                                continue;
                        }
                        break;
@@ -435,6 +462,7 @@ static int write_cnodes(struct ubifs_info *c)
                clear_bit(COW_ZNODE, &cnode->flags);
                smp_mb__after_clear_bit();
                offs += len;
+               dbg_chk_lpt_sz(c, 1, len);
                cnode = cnode->cnext;
        } while (cnode && cnode != c->lpt_cnext);
 
@@ -448,10 +476,11 @@ static int write_cnodes(struct ubifs_info *c)
                                              UBI_SHORTTERM);
                        if (err)
                                return err;
+                       dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
                        err = realloc_lpt_leb(c, &lnum);
                        if (err)
-                               return err;
-                       offs = 0;
+                               goto no_space;
+                       offs = from = 0;
                        ubifs_assert(lnum >= c->lpt_first &&
                                     lnum <= c->lpt_last);
                        err = ubifs_leb_unmap(c, lnum);
@@ -461,6 +490,7 @@ static int write_cnodes(struct ubifs_info *c)
                done_lsave = 1;
                ubifs_pack_lsave(c, buf + offs, c->lsave);
                offs += c->lsave_sz;
+               dbg_chk_lpt_sz(c, 1, c->lsave_sz);
        }
 
        /* Make sure to place LPT's own lprops table */
@@ -473,10 +503,11 @@ static int write_cnodes(struct ubifs_info *c)
                                              UBI_SHORTTERM);
                        if (err)
                                return err;
+                       dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
                        err = realloc_lpt_leb(c, &lnum);
                        if (err)
-                               return err;
-                       offs = 0;
+                               goto no_space;
+                       offs = from = 0;
                        ubifs_assert(lnum >= c->lpt_first &&
                                     lnum <= c->lpt_last);
                        err = ubifs_leb_unmap(c, lnum);
@@ -486,6 +517,7 @@ static int write_cnodes(struct ubifs_info *c)
                done_ltab = 1;
                ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
                offs += c->ltab_sz;
+               dbg_chk_lpt_sz(c, 1, c->ltab_sz);
        }
 
        /* Write remaining data in buffer */
@@ -495,6 +527,12 @@ static int write_cnodes(struct ubifs_info *c)
        err = ubifs_leb_write(c, lnum, buf + from, from, alen, UBI_SHORTTERM);
        if (err)
                return err;
+
+       dbg_chk_lpt_sz(c, 4, alen - wlen);
+       err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
+       if (err)
+               return err;
+
        c->nhead_lnum = lnum;
        c->nhead_offs = ALIGN(offs, c->min_io_size);
 
@@ -503,31 +541,39 @@ static int write_cnodes(struct ubifs_info *c)
        dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
        if (c->big_lpt)
                dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
+
        return 0;
+
+no_space:
+       ubifs_err("LPT out of space mismatch");
+       dbg_err("LPT out of space mismatch at LEB %d:%d needing %d, done_ltab "
+               "%d, done_lsave %d", lnum, offs, len, done_ltab, done_lsave);
+       dbg_dump_lpt_info(c);
+       dbg_dump_lpt_lebs(c);
+       dump_stack();
+       return err;
 }
 
 /**
- * next_pnode - find next pnode.
+ * next_pnode_to_dirty - find next pnode to dirty.
  * @c: UBIFS file-system description object
  * @pnode: pnode
  *
- * This function returns the next pnode or %NULL if there are no more pnodes.
+ * This function returns the next pnode to dirty or %NULL if there are no more
+ * pnodes.  Note that pnodes that have never been written (lnum == 0) are
+ * skipped.
  */
-static struct ubifs_pnode *next_pnode(struct ubifs_info *c,
-                                     struct ubifs_pnode *pnode)
+static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
+                                              struct ubifs_pnode *pnode)
 {
        struct ubifs_nnode *nnode;
        int iip;
 
        /* Try to go right */
        nnode = pnode->parent;
-       iip = pnode->iip + 1;
-       if (iip < UBIFS_LPT_FANOUT) {
-               /* We assume here that LEB zero is never an LPT LEB */
+       for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
                if (nnode->nbranch[iip].lnum)
                        return ubifs_get_pnode(c, nnode, iip);
-               else
-                       return NULL;
        }
 
        /* Go up while can't go right */
@@ -536,8 +582,11 @@ static struct ubifs_pnode *next_pnode(struct ubifs_info *c,
                nnode = nnode->parent;
                if (!nnode)
                        return NULL;
-               /* We assume here that LEB zero is never an LPT LEB */
-       } while (iip >= UBIFS_LPT_FANOUT || !nnode->nbranch[iip].lnum);
+               for (; iip < UBIFS_LPT_FANOUT; iip++) {
+                       if (nnode->nbranch[iip].lnum)
+                               break;
+               }
+       } while (iip >= UBIFS_LPT_FANOUT);
 
        /* Go right */
        nnode = ubifs_get_nnode(c, nnode, iip);
@@ -546,12 +595,29 @@ static struct ubifs_pnode *next_pnode(struct ubifs_info *c,
 
        /* Go down to level 1 */
        while (nnode->level > 1) {
-               nnode = ubifs_get_nnode(c, nnode, 0);
+               for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
+                       if (nnode->nbranch[iip].lnum)
+                               break;
+               }
+               if (iip >= UBIFS_LPT_FANOUT) {
+                       /*
+                        * Should not happen, but we need to keep going
+                        * if it does.
+                        */
+                       iip = 0;
+               }
+               nnode = ubifs_get_nnode(c, nnode, iip);
                if (IS_ERR(nnode))
                        return (void *)nnode;
        }
 
-       return ubifs_get_pnode(c, nnode, 0);
+       for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
+               if (nnode->nbranch[iip].lnum)
+                       break;
+       if (iip >= UBIFS_LPT_FANOUT)
+               /* Should not happen, but we need to keep going if it does */
+               iip = 0;
+       return ubifs_get_pnode(c, nnode, iip);
 }
 
 /**
@@ -641,7 +707,7 @@ static int make_tree_dirty(struct ubifs_info *c)
        pnode = pnode_lookup(c, 0);
        while (pnode) {
                do_make_pnode_dirty(c, pnode);
-               pnode = next_pnode(c, pnode);
+               pnode = next_pnode_to_dirty(c, pnode);
                if (IS_ERR(pnode))
                        return PTR_ERR(pnode);
        }
@@ -706,7 +772,7 @@ static void lpt_tgc_start(struct ubifs_info *c)
  * LPT trivial garbage collection is where a LPT LEB contains only dirty and
  * free space and so may be reused as soon as the next commit is completed.
  * This function is called after the commit is completed (master node has been
- * written) and unmaps LPT LEBs that were marked for trivial GC.
+ * written) and un-maps LPT LEBs that were marked for trivial GC.
  */
 static int lpt_tgc_end(struct ubifs_info *c)
 {
@@ -982,7 +1048,7 @@ static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
  * @c: UBIFS file-system description object
  * @node_type: LPT node type
  */
-static int get_lpt_node_len(struct ubifs_info *c, int node_type)
+static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
 {
        switch (node_type) {
        case UBIFS_LPT_NNODE:
@@ -1003,7 +1069,7 @@ static int get_lpt_node_len(struct ubifs_info *c, int node_type)
  * @buf: buffer
  * @len: length of buffer
  */
-static int get_pad_len(struct ubifs_info *c, uint8_t *buf, int len)
+static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
 {
        int offs, pad_len;
 
@@ -1020,7 +1086,8 @@ static int get_pad_len(struct ubifs_info *c, uint8_t *buf, int len)
  * @buf: buffer
  * @node_num: node number is returned here
  */
-static int get_lpt_node_type(struct ubifs_info *c, uint8_t *buf, int *node_num)
+static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
+                            int *node_num)
 {
        uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
        int pos = 0, node_type;
@@ -1038,12 +1105,14 @@ static int get_lpt_node_type(struct ubifs_info *c, uint8_t *buf, int *node_num)
  *
  * This function returns %1 if the buffer contains a node or %0 if it does not.
  */
-static int is_a_node(struct ubifs_info *c, uint8_t *buf, int len)
+static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
 {
        uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
        int pos = 0, node_type, node_len;
        uint16_t crc, calc_crc;
 
+       if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
+               return 0;
        node_type = ubifs_unpack_bits(&addr, &pos, UBIFS_LPT_TYPE_BITS);
        if (node_type == UBIFS_LPT_NOT_A_NODE)
                return 0;
@@ -1060,7 +1129,6 @@ static int is_a_node(struct ubifs_info *c, uint8_t *buf, int len)
        return 1;
 }
 
-
 /**
  * lpt_gc_lnum - garbage collect a LPT LEB.
  * @c: UBIFS file-system description object
@@ -1156,6 +1224,9 @@ int ubifs_lpt_start_commit(struct ubifs_info *c)
        dbg_lp("");
 
        mutex_lock(&c->lp_mutex);
+       err = dbg_chk_lpt_free_spc(c);
+       if (err)
+               goto out;
        err = dbg_check_ltab(c);
        if (err)
                goto out;
@@ -1415,7 +1486,7 @@ void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
 #ifdef CONFIG_UBIFS_FS_DEBUG
 
 /**
- * dbg_is_all_ff - determine if a buffer contains only 0xff bytes.
+ * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
  * @buf: buffer
  * @len: buffer length
  */
@@ -1440,7 +1511,7 @@ static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
        struct ubifs_nnode *nnode;
        int hght;
 
-       /* Entire tree is in memory so first_nnode / next_nnode are ok */
+       /* Entire tree is in memory so first_nnode / next_nnode are OK */
        nnode = first_nnode(c, &hght);
        for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
                struct ubifs_nbranch *branch;
@@ -1554,7 +1625,10 @@ static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
 {
        int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
        int ret;
-       void *buf = c->dbg_buf;
+       void *buf = c->dbg->buf;
+
+       if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
+               return 0;
 
        dbg_lp("LEB %d", lnum);
        err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
@@ -1645,4 +1719,256 @@ int dbg_check_ltab(struct ubifs_info *c)
        return 0;
 }
 
+/**
+ * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
+ * @c: the UBIFS file-system description object
+ *
+ * This function returns %0 on success and a negative error code on failure.
+ */
+int dbg_chk_lpt_free_spc(struct ubifs_info *c)
+{
+       long long free = 0;
+       int i;
+
+       if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
+               return 0;
+
+       for (i = 0; i < c->lpt_lebs; i++) {
+               if (c->ltab[i].tgc || c->ltab[i].cmt)
+                       continue;
+               if (i + c->lpt_first == c->nhead_lnum)
+                       free += c->leb_size - c->nhead_offs;
+               else if (c->ltab[i].free == c->leb_size)
+                       free += c->leb_size;
+       }
+       if (free < c->lpt_sz) {
+               dbg_err("LPT space error: free %lld lpt_sz %lld",
+                       free, c->lpt_sz);
+               dbg_dump_lpt_info(c);
+               dbg_dump_lpt_lebs(c);
+               dump_stack();
+               return -EINVAL;
+       }
+       return 0;
+}
+
+/**
+ * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
+ * @c: the UBIFS file-system description object
+ * @action: what to do
+ * @len: length written
+ *
+ * This function returns %0 on success and a negative error code on failure.
+ * The @action argument may be one of:
+ *   o %0 - LPT debugging checking starts, initialize debugging variables;
+ *   o %1 - wrote an LPT node, increase LPT size by @len bytes;
+ *   o %2 - switched to a different LEB and wasted @len bytes;
+ *   o %3 - check that we've written the right number of bytes.
+ *   o %4 - wasted @len bytes;
+ */
+int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
+{
+       struct ubifs_debug_info *d = c->dbg;
+       long long chk_lpt_sz, lpt_sz;
+       int err = 0;
+
+       if (!(ubifs_chk_flags & UBIFS_CHK_LPROPS))
+               return 0;
+
+       switch (action) {
+       case 0:
+               d->chk_lpt_sz = 0;
+               d->chk_lpt_sz2 = 0;
+               d->chk_lpt_lebs = 0;
+               d->chk_lpt_wastage = 0;
+               if (c->dirty_pn_cnt > c->pnode_cnt) {
+                       dbg_err("dirty pnodes %d exceed max %d",
+                               c->dirty_pn_cnt, c->pnode_cnt);
+                       err = -EINVAL;
+               }
+               if (c->dirty_nn_cnt > c->nnode_cnt) {
+                       dbg_err("dirty nnodes %d exceed max %d",
+                               c->dirty_nn_cnt, c->nnode_cnt);
+                       err = -EINVAL;
+               }
+               return err;
+       case 1:
+               d->chk_lpt_sz += len;
+               return 0;
+       case 2:
+               d->chk_lpt_sz += len;
+               d->chk_lpt_wastage += len;
+               d->chk_lpt_lebs += 1;
+               return 0;
+       case 3:
+               chk_lpt_sz = c->leb_size;
+               chk_lpt_sz *= d->chk_lpt_lebs;
+               chk_lpt_sz += len - c->nhead_offs;
+               if (d->chk_lpt_sz != chk_lpt_sz) {
+                       dbg_err("LPT wrote %lld but space used was %lld",
+                               d->chk_lpt_sz, chk_lpt_sz);
+                       err = -EINVAL;
+               }
+               if (d->chk_lpt_sz > c->lpt_sz) {
+                       dbg_err("LPT wrote %lld but lpt_sz is %lld",
+                               d->chk_lpt_sz, c->lpt_sz);
+                       err = -EINVAL;
+               }
+               if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
+                       dbg_err("LPT layout size %lld but wrote %lld",
+                               d->chk_lpt_sz, d->chk_lpt_sz2);
+                       err = -EINVAL;
+               }
+               if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
+                       dbg_err("LPT new nhead offs: expected %d was %d",
+                               d->new_nhead_offs, len);
+                       err = -EINVAL;
+               }
+               lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
+               lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
+               lpt_sz += c->ltab_sz;
+               if (c->big_lpt)
+                       lpt_sz += c->lsave_sz;
+               if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
+                       dbg_err("LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
+                               d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
+                       err = -EINVAL;
+               }
+               if (err) {
+                       dbg_dump_lpt_info(c);
+                       dbg_dump_lpt_lebs(c);
+                       dump_stack();
+               }
+               d->chk_lpt_sz2 = d->chk_lpt_sz;
+               d->chk_lpt_sz = 0;
+               d->chk_lpt_wastage = 0;
+               d->chk_lpt_lebs = 0;
+               d->new_nhead_offs = len;
+               return err;
+       case 4:
+               d->chk_lpt_sz += len;
+               d->chk_lpt_wastage += len;
+               return 0;
+       default:
+               return -EINVAL;
+       }
+}
+
+/**
+ * dbg_dump_lpt_leb - dump an LPT LEB.
+ * @c: UBIFS file-system description object
+ * @lnum: LEB number to dump
+ *
+ * This function dumps an LEB from LPT area. Nodes in this area are very
+ * different to nodes in the main area (e.g., they do not have common headers,
+ * they do not have 8-byte alignments, etc), so we have a separate function to
+ * dump LPT area LEBs. Note, LPT has to be locked by the caller.
+ */
+static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
+{
+       int err, len = c->leb_size, node_type, node_num, node_len, offs;
+       void *buf = c->dbg->buf;
+
+       printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n",
+              current->pid, lnum);
+       err = ubi_read(c->ubi, lnum, buf, 0, c->leb_size);
+       if (err) {
+               ubifs_err("cannot read LEB %d, error %d", lnum, err);
+               return;
+       }
+       while (1) {
+               offs = c->leb_size - len;
+               if (!is_a_node(c, buf, len)) {
+                       int pad_len;
+
+                       pad_len = get_pad_len(c, buf, len);
+                       if (pad_len) {
+                               printk(KERN_DEBUG "LEB %d:%d, pad %d bytes\n",
+                                      lnum, offs, pad_len);
+                               buf += pad_len;
+                               len -= pad_len;
+                               continue;
+                       }
+                       if (len)
+                               printk(KERN_DEBUG "LEB %d:%d, free %d bytes\n",
+                                      lnum, offs, len);
+                       break;
+               }
+
+               node_type = get_lpt_node_type(c, buf, &node_num);
+               switch (node_type) {
+               case UBIFS_LPT_PNODE:
+               {
+                       node_len = c->pnode_sz;
+                       if (c->big_lpt)
+                               printk(KERN_DEBUG "LEB %d:%d, pnode num %d\n",
+                                      lnum, offs, node_num);
+                       else
+                               printk(KERN_DEBUG "LEB %d:%d, pnode\n",
+                                      lnum, offs);
+                       break;
+               }
+               case UBIFS_LPT_NNODE:
+               {
+                       int i;
+                       struct ubifs_nnode nnode;
+
+                       node_len = c->nnode_sz;
+                       if (c->big_lpt)
+                               printk(KERN_DEBUG "LEB %d:%d, nnode num %d, ",
+                                      lnum, offs, node_num);
+                       else
+                               printk(KERN_DEBUG "LEB %d:%d, nnode, ",
+                                      lnum, offs);
+                       err = ubifs_unpack_nnode(c, buf, &nnode);
+                       for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
+                               printk(KERN_CONT "%d:%d", nnode.nbranch[i].lnum,
+                                      nnode.nbranch[i].offs);
+                               if (i != UBIFS_LPT_FANOUT - 1)
+                                       printk(KERN_CONT ", ");
+                       }
+                       printk(KERN_CONT "\n");
+                       break;
+               }
+               case UBIFS_LPT_LTAB:
+                       node_len = c->ltab_sz;
+                       printk(KERN_DEBUG "LEB %d:%d, ltab\n",
+                              lnum, offs);
+                       break;
+               case UBIFS_LPT_LSAVE:
+                       node_len = c->lsave_sz;
+                       printk(KERN_DEBUG "LEB %d:%d, lsave len\n", lnum, offs);
+                       break;
+               default:
+                       ubifs_err("LPT node type %d not recognized", node_type);
+                       return;
+               }
+
+               buf += node_len;
+               len -= node_len;
+       }
+
+       printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n",
+              current->pid, lnum);
+}
+
+/**
+ * dbg_dump_lpt_lebs - dump LPT lebs.
+ * @c: UBIFS file-system description object
+ *
+ * This function dumps all LPT LEBs. The caller has to make sure the LPT is
+ * locked.
+ */
+void dbg_dump_lpt_lebs(const struct ubifs_info *c)
+{
+       int i;
+
+       printk(KERN_DEBUG "(pid %d) start dumping all LPT LEBs\n",
+              current->pid);
+       for (i = 0; i < c->lpt_lebs; i++)
+               dump_lpt_leb(c, i + c->lpt_first);
+       printk(KERN_DEBUG "(pid %d) finish dumping all LPT LEBs\n",
+              current->pid);
+}
+
 #endif /* CONFIG_UBIFS_FS_DEBUG */