summaryrefslogtreecommitdiff
path: root/ubifs-utils/fsck.ubifs/check_space.c
diff options
context:
space:
mode:
authorZhihao Cheng <chengzhihao1@huawei.com>2024-11-11 17:08:08 +0800
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2024-11-11 10:32:46 +0100
commit47c1cfd5e8ec289597f7342f88e103811511f0a8 (patch)
tree364fe0e96fbcd6bcbd964d0349f3d4883c5f539b /ubifs-utils/fsck.ubifs/check_space.c
parent59dd2cf5ad8e32c7b38b3287b9d05a2d809e93e4 (diff)
fsck.ubifs: Move common functions and data structures into check_space.c
This is a preparation for adding LPT checking support. Move some data structures and functions into check_space.c, also factor out some common functions in libubifs: 1. Move 'lpts' from rebuild module, make it resuable for non-rebuild_fs modes. 2. Move function 'get_free_leb' from rebuild_fs.c, it could be reused in building LPT. 3. Move function 'build_lpt' from rebuild_fs.c, it could be reused in building LPT. 4. Factor out lpt nodes freeing into a new function ubifs_free_lpt_nodes. 5. Factor out nnode dirty marking implementations into a new function ubifs_make_nnode_dirty. 5. Export the function of nnode number calculation, calc_nnode_num is renamed as ubifs_calc_nnode_num. 6. Export the function of making pnode dirty, do_make_pnode_dirty is renamed as ubifs_make_pnode_dirty. 7. Rename next_pnode_to_dirty to ubifs_find_next_pnode and export it. 8. Export free_buds and expend its parameters. Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com> Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'ubifs-utils/fsck.ubifs/check_space.c')
-rw-r--r--ubifs-utils/fsck.ubifs/check_space.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/ubifs-utils/fsck.ubifs/check_space.c b/ubifs-utils/fsck.ubifs/check_space.c
new file mode 100644
index 0000000..f758bf1
--- /dev/null
+++ b/ubifs-utils/fsck.ubifs/check_space.c
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2024, Huawei Technologies Co, Ltd.
+ *
+ * Authors: Zhihao Cheng <chengzhihao1@huawei.com>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "linux_err.h"
+#include "bitops.h"
+#include "kmem.h"
+#include "ubifs.h"
+#include "defs.h"
+#include "debug.h"
+#include "key.h"
+#include "misc.h"
+#include "fsck.ubifs.h"
+
+/**
+ * get_free_leb - get a free LEB according to @FSCK(c)->used_lebs.
+ * @c: UBIFS file-system description object
+ *
+ * This function tries to find a free LEB, lnum is returned if found, otherwise
+ * %-ENOSPC is returned.
+ */
+int get_free_leb(struct ubifs_info *c)
+{
+ int lnum;
+
+ lnum = find_next_zero_bit(FSCK(c)->used_lebs, c->main_lebs, 0);
+ if (lnum >= c->main_lebs) {
+ ubifs_err(c, "No space left.");
+ return -ENOSPC;
+ }
+ set_bit(lnum, FSCK(c)->used_lebs);
+ lnum += c->main_first;
+
+ return lnum;
+}
+
+/**
+ * build_lpt - construct LPT and write it into flash.
+ * @c: UBIFS file-system description object
+ * @calculate_lp_cb: callback function to calculate the properties for given LEB
+ *
+ * This function builds LPT according to the calculated results by
+ * @calculate_lp_cb and writes LPT into flash. Returns zero in case of success,
+ * a negative error code in case of failure.
+ */
+int build_lpt(struct ubifs_info *c, calculate_lp_callback calculate_lp_cb)
+{
+ int i, err, lnum, free, dirty;
+ u8 hash_lpt[UBIFS_HASH_ARR_SZ];
+
+ memset(&c->lst, 0, sizeof(struct ubifs_lp_stats));
+ /* Set gc lnum. */
+ lnum = get_free_leb(c);
+ if (lnum < 0)
+ return lnum;
+ c->gc_lnum = lnum;
+
+ /* Update LPT. */
+ for (i = 0; i < c->main_lebs; i++) {
+ err = calculate_lp_cb(c, i, &free, &dirty);
+ if (err)
+ return err;
+
+ FSCK(c)->lpts[i].free = free;
+ FSCK(c)->lpts[i].dirty = dirty;
+ c->lst.total_free += free;
+ c->lst.total_dirty += dirty;
+
+ if (free == c->leb_size)
+ c->lst.empty_lebs++;
+
+ if (FSCK(c)->lpts[i].flags & LPROPS_INDEX) {
+ c->lst.idx_lebs += 1;
+ } else {
+ int spc;
+
+ spc = free + dirty;
+ if (spc < c->dead_wm)
+ c->lst.total_dead += spc;
+ else
+ c->lst.total_dark += ubifs_calc_dark(c, spc);
+ c->lst.total_used += c->leb_size - spc;
+ }
+ }
+
+ /* Write LPT. */
+ return ubifs_create_lpt(c, FSCK(c)->lpts, c->main_lebs, hash_lpt);
+}