UBIFS: fix oops on error path in read_pnode
[firefly-linux-kernel-4.4.55.git] / fs / ubifs / lpt.c
index 6d914160ec55fcb81aa7f75bddc41e6d815fc6a1..d0dfe7a0dedfdb5c90660f2c7b0c18fd601e5c27 100644 (file)
@@ -43,8 +43,9 @@
  * mounted.
  */
 
-#include <linux/crc16.h>
 #include "ubifs.h"
+#include <linux/crc16.h>
+#include <linux/math64.h>
 
 /**
  * do_calc_lpt_geom - calculate sizes for the LPT area.
@@ -135,15 +136,13 @@ static void do_calc_lpt_geom(struct ubifs_info *c)
 int ubifs_calc_lpt_geom(struct ubifs_info *c)
 {
        int lebs_needed;
-       uint64_t sz;
+       long long sz;
 
        do_calc_lpt_geom(c);
 
        /* Verify that lpt_lebs is big enough */
        sz = c->lpt_sz * 2; /* Must have at least 2 times the size */
-       sz += c->leb_size - 1;
-       do_div(sz, c->leb_size);
-       lebs_needed = sz;
+       lebs_needed = div_u64(sz + c->leb_size - 1, c->leb_size);
        if (lebs_needed > c->lpt_lebs) {
                ubifs_err("too few LPT LEBs");
                return -EINVAL;
@@ -175,7 +174,7 @@ static int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs,
                              int *big_lpt)
 {
        int i, lebs_needed;
-       uint64_t sz;
+       long long sz;
 
        /* Start by assuming the minimum number of LPT LEBs */
        c->lpt_lebs = UBIFS_MIN_LPT_LEBS;
@@ -202,9 +201,7 @@ static int calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs,
        /* Now check there are enough LPT LEBs */
        for (i = 0; i < 64 ; i++) {
                sz = c->lpt_sz * 4; /* Allow 4 times the size */
-               sz += c->leb_size - 1;
-               do_div(sz, c->leb_size);
-               lebs_needed = sz;
+               lebs_needed = div_u64(sz + c->leb_size - 1, c->leb_size);
                if (lebs_needed > c->lpt_lebs) {
                        /* Not enough LPT LEBs so try again with more */
                        c->lpt_lebs = lebs_needed;
@@ -1272,10 +1269,9 @@ static int read_pnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip)
        lnum = branch->lnum;
        offs = branch->offs;
        pnode = kzalloc(sizeof(struct ubifs_pnode), GFP_NOFS);
-       if (!pnode) {
-               err = -ENOMEM;
-               goto out;
-       }
+       if (!pnode)
+               return -ENOMEM;
+
        if (lnum == 0) {
                /*
                 * This pnode was not written which just means that the LEB