aboutsummaryrefslogtreecommitdiff
path: root/ubifs-utils/fsck.ubifs/load_fs.c
blob: 4a06b4c242922c5f566a959d57c46e4638afce53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// 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 "bitops.h"
#include "kmem.h"
#include "ubifs.h"
#include "defs.h"
#include "debug.h"
#include "key.h"
#include "misc.h"
#include "fsck.ubifs.h"

int ubifs_load_filesystem(struct ubifs_info *c)
{
	int err;
	size_t sz;

	err = init_constants_early(c);
	if (err) {
		exit_code |= FSCK_ERROR;
		return err;
	}

	err = check_volume_empty(c);
	if (err <= 0) {
		exit_code |= FSCK_ERROR;
		log_err(c, 0, "%s UBI volume!", err < 0 ? "bad" : "empty");
		return -EINVAL;
	}

	if (c->ro_media && !c->ro_mount) {
		exit_code |= FSCK_ERROR;
		log_err(c, 0, "cannot read-write on read-only media");
		return -EROFS;
	}

	err = -ENOMEM;
	c->bottom_up_buf = kmalloc_array(BOTTOM_UP_HEIGHT, sizeof(int),
					 GFP_KERNEL);
	if (!c->bottom_up_buf) {
		exit_code |= FSCK_ERROR;
		log_err(c, errno, "cannot allocate bottom_up_buf");
		goto out_free;
	}

	c->sbuf = vmalloc(c->leb_size);
	if (!c->sbuf) {
		exit_code |= FSCK_ERROR;
		log_err(c, errno, "cannot allocate sbuf");
		goto out_free;
	}

	if (!c->ro_mount) {
		c->ileb_buf = vmalloc(c->leb_size);
		if (!c->ileb_buf) {
			exit_code |= FSCK_ERROR;
			log_err(c, errno, "cannot allocate ileb_buf");
			goto out_free;
		}
	}

	c->mounting = 1;

	log_out(c, "Read superblock");
	err = ubifs_read_superblock(c);
	if (err) {
		if (test_and_clear_failure_reason_callback(c, FR_DATA_CORRUPTED))
			fix_problem(c, SB_CORRUPTED);
		exit_code |= FSCK_ERROR;
		goto out_mounting;
	}

	err = init_constants_sb(c);
	if (err) {
		exit_code |= FSCK_ERROR;
		goto out_mounting;
	}

	sz = ALIGN(c->max_idx_node_sz, c->min_io_size) * 2;
	c->cbuf = kmalloc(sz, GFP_NOFS);
	if (!c->cbuf) {
		err = -ENOMEM;
		exit_code |= FSCK_ERROR;
		log_err(c, errno, "cannot allocate cbuf");
		goto out_mounting;
	}

	err = alloc_wbufs(c);
	if (err) {
		exit_code |= FSCK_ERROR;
		log_err(c, 0, "cannot allocate wbuf");
		goto out_mounting;
	}

	c->mounting = 0;

	return 0;

out_mounting:
	c->mounting = 0;
out_free:
	kfree(c->cbuf);
	kfree(c->ileb_buf);
	kfree(c->sbuf);
	kfree(c->bottom_up_buf);
	kfree(c->sup_node);

	return err;
}

void ubifs_destroy_filesystem(struct ubifs_info *c)
{
	free_wbufs(c);

	kfree(c->cbuf);
	kfree(c->ileb_buf);
	kfree(c->sbuf);
	kfree(c->bottom_up_buf);
	kfree(c->sup_node);
}