xref: /openbmc/u-boot/fs/ubifs/master.c (revision d66ff4ba)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * This file is part of UBIFS.
4  *
5  * Copyright (C) 2006-2008 Nokia Corporation.
6  *
7  * Authors: Artem Bityutskiy (Битюцкий Артём)
8  *          Adrian Hunter
9  */
10 
11 /* This file implements reading and writing the master node */
12 
13 #include "ubifs.h"
14 #ifdef __UBOOT__
15 #include <linux/compat.h>
16 #include <linux/err.h>
17 #include <ubi_uboot.h>
18 #endif
19 
20 /**
21  * scan_for_master - search the valid master node.
22  * @c: UBIFS file-system description object
23  *
24  * This function scans the master node LEBs and search for the latest master
25  * node. Returns zero in case of success, %-EUCLEAN if there master area is
26  * corrupted and requires recovery, and a negative error code in case of
27  * failure.
28  */
29 static int scan_for_master(struct ubifs_info *c)
30 {
31 	struct ubifs_scan_leb *sleb;
32 	struct ubifs_scan_node *snod;
33 	int lnum, offs = 0, nodes_cnt;
34 
35 	lnum = UBIFS_MST_LNUM;
36 
37 	sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
38 	if (IS_ERR(sleb))
39 		return PTR_ERR(sleb);
40 	nodes_cnt = sleb->nodes_cnt;
41 	if (nodes_cnt > 0) {
42 		snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
43 				  list);
44 		if (snod->type != UBIFS_MST_NODE)
45 			goto out_dump;
46 		memcpy(c->mst_node, snod->node, snod->len);
47 		offs = snod->offs;
48 	}
49 	ubifs_scan_destroy(sleb);
50 
51 	lnum += 1;
52 
53 	sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
54 	if (IS_ERR(sleb))
55 		return PTR_ERR(sleb);
56 	if (sleb->nodes_cnt != nodes_cnt)
57 		goto out;
58 	if (!sleb->nodes_cnt)
59 		goto out;
60 	snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
61 	if (snod->type != UBIFS_MST_NODE)
62 		goto out_dump;
63 	if (snod->offs != offs)
64 		goto out;
65 	if (memcmp((void *)c->mst_node + UBIFS_CH_SZ,
66 		   (void *)snod->node + UBIFS_CH_SZ,
67 		   UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
68 		goto out;
69 	c->mst_offs = offs;
70 	ubifs_scan_destroy(sleb);
71 	return 0;
72 
73 out:
74 	ubifs_scan_destroy(sleb);
75 	return -EUCLEAN;
76 
77 out_dump:
78 	ubifs_err(c, "unexpected node type %d master LEB %d:%d",
79 		  snod->type, lnum, snod->offs);
80 	ubifs_scan_destroy(sleb);
81 	return -EINVAL;
82 }
83 
84 /**
85  * validate_master - validate master node.
86  * @c: UBIFS file-system description object
87  *
88  * This function validates data which was read from master node. Returns zero
89  * if the data is all right and %-EINVAL if not.
90  */
91 static int validate_master(const struct ubifs_info *c)
92 {
93 	long long main_sz;
94 	int err;
95 
96 	if (c->max_sqnum >= SQNUM_WATERMARK) {
97 		err = 1;
98 		goto out;
99 	}
100 
101 	if (c->cmt_no >= c->max_sqnum) {
102 		err = 2;
103 		goto out;
104 	}
105 
106 	if (c->highest_inum >= INUM_WATERMARK) {
107 		err = 3;
108 		goto out;
109 	}
110 
111 	if (c->lhead_lnum < UBIFS_LOG_LNUM ||
112 	    c->lhead_lnum >= UBIFS_LOG_LNUM + c->log_lebs ||
113 	    c->lhead_offs < 0 || c->lhead_offs >= c->leb_size ||
114 	    c->lhead_offs & (c->min_io_size - 1)) {
115 		err = 4;
116 		goto out;
117 	}
118 
119 	if (c->zroot.lnum >= c->leb_cnt || c->zroot.lnum < c->main_first ||
120 	    c->zroot.offs >= c->leb_size || c->zroot.offs & 7) {
121 		err = 5;
122 		goto out;
123 	}
124 
125 	if (c->zroot.len < c->ranges[UBIFS_IDX_NODE].min_len ||
126 	    c->zroot.len > c->ranges[UBIFS_IDX_NODE].max_len) {
127 		err = 6;
128 		goto out;
129 	}
130 
131 	if (c->gc_lnum >= c->leb_cnt || c->gc_lnum < c->main_first) {
132 		err = 7;
133 		goto out;
134 	}
135 
136 	if (c->ihead_lnum >= c->leb_cnt || c->ihead_lnum < c->main_first ||
137 	    c->ihead_offs % c->min_io_size || c->ihead_offs < 0 ||
138 	    c->ihead_offs > c->leb_size || c->ihead_offs & 7) {
139 		err = 8;
140 		goto out;
141 	}
142 
143 	main_sz = (long long)c->main_lebs * c->leb_size;
144 	if (c->bi.old_idx_sz & 7 || c->bi.old_idx_sz >= main_sz) {
145 		err = 9;
146 		goto out;
147 	}
148 
149 	if (c->lpt_lnum < c->lpt_first || c->lpt_lnum > c->lpt_last ||
150 	    c->lpt_offs < 0 || c->lpt_offs + c->nnode_sz > c->leb_size) {
151 		err = 10;
152 		goto out;
153 	}
154 
155 	if (c->nhead_lnum < c->lpt_first || c->nhead_lnum > c->lpt_last ||
156 	    c->nhead_offs < 0 || c->nhead_offs % c->min_io_size ||
157 	    c->nhead_offs > c->leb_size) {
158 		err = 11;
159 		goto out;
160 	}
161 
162 	if (c->ltab_lnum < c->lpt_first || c->ltab_lnum > c->lpt_last ||
163 	    c->ltab_offs < 0 ||
164 	    c->ltab_offs + c->ltab_sz > c->leb_size) {
165 		err = 12;
166 		goto out;
167 	}
168 
169 	if (c->big_lpt && (c->lsave_lnum < c->lpt_first ||
170 	    c->lsave_lnum > c->lpt_last || c->lsave_offs < 0 ||
171 	    c->lsave_offs + c->lsave_sz > c->leb_size)) {
172 		err = 13;
173 		goto out;
174 	}
175 
176 	if (c->lscan_lnum < c->main_first || c->lscan_lnum >= c->leb_cnt) {
177 		err = 14;
178 		goto out;
179 	}
180 
181 	if (c->lst.empty_lebs < 0 || c->lst.empty_lebs > c->main_lebs - 2) {
182 		err = 15;
183 		goto out;
184 	}
185 
186 	if (c->lst.idx_lebs < 0 || c->lst.idx_lebs > c->main_lebs - 1) {
187 		err = 16;
188 		goto out;
189 	}
190 
191 	if (c->lst.total_free < 0 || c->lst.total_free > main_sz ||
192 	    c->lst.total_free & 7) {
193 		err = 17;
194 		goto out;
195 	}
196 
197 	if (c->lst.total_dirty < 0 || (c->lst.total_dirty & 7)) {
198 		err = 18;
199 		goto out;
200 	}
201 
202 	if (c->lst.total_used < 0 || (c->lst.total_used & 7)) {
203 		err = 19;
204 		goto out;
205 	}
206 
207 	if (c->lst.total_free + c->lst.total_dirty +
208 	    c->lst.total_used > main_sz) {
209 		err = 20;
210 		goto out;
211 	}
212 
213 	if (c->lst.total_dead + c->lst.total_dark +
214 	    c->lst.total_used + c->bi.old_idx_sz > main_sz) {
215 		err = 21;
216 		goto out;
217 	}
218 
219 	if (c->lst.total_dead < 0 ||
220 	    c->lst.total_dead > c->lst.total_free + c->lst.total_dirty ||
221 	    c->lst.total_dead & 7) {
222 		err = 22;
223 		goto out;
224 	}
225 
226 	if (c->lst.total_dark < 0 ||
227 	    c->lst.total_dark > c->lst.total_free + c->lst.total_dirty ||
228 	    c->lst.total_dark & 7) {
229 		err = 23;
230 		goto out;
231 	}
232 
233 	return 0;
234 
235 out:
236 	ubifs_err(c, "bad master node at offset %d error %d", c->mst_offs, err);
237 	ubifs_dump_node(c, c->mst_node);
238 	return -EINVAL;
239 }
240 
241 /**
242  * ubifs_read_master - read master node.
243  * @c: UBIFS file-system description object
244  *
245  * This function finds and reads the master node during file-system mount. If
246  * the flash is empty, it creates default master node as well. Returns zero in
247  * case of success and a negative error code in case of failure.
248  */
249 int ubifs_read_master(struct ubifs_info *c)
250 {
251 	int err, old_leb_cnt;
252 
253 	c->mst_node = kzalloc(c->mst_node_alsz, GFP_KERNEL);
254 	if (!c->mst_node)
255 		return -ENOMEM;
256 
257 	err = scan_for_master(c);
258 	if (err) {
259 		if (err == -EUCLEAN)
260 			err = ubifs_recover_master_node(c);
261 		if (err)
262 			/*
263 			 * Note, we do not free 'c->mst_node' here because the
264 			 * unmount routine will take care of this.
265 			 */
266 			return err;
267 	}
268 
269 	/* Make sure that the recovery flag is clear */
270 	c->mst_node->flags &= cpu_to_le32(~UBIFS_MST_RCVRY);
271 
272 	c->max_sqnum       = le64_to_cpu(c->mst_node->ch.sqnum);
273 	c->highest_inum    = le64_to_cpu(c->mst_node->highest_inum);
274 	c->cmt_no          = le64_to_cpu(c->mst_node->cmt_no);
275 	c->zroot.lnum      = le32_to_cpu(c->mst_node->root_lnum);
276 	c->zroot.offs      = le32_to_cpu(c->mst_node->root_offs);
277 	c->zroot.len       = le32_to_cpu(c->mst_node->root_len);
278 	c->lhead_lnum      = le32_to_cpu(c->mst_node->log_lnum);
279 	c->gc_lnum         = le32_to_cpu(c->mst_node->gc_lnum);
280 	c->ihead_lnum      = le32_to_cpu(c->mst_node->ihead_lnum);
281 	c->ihead_offs      = le32_to_cpu(c->mst_node->ihead_offs);
282 	c->bi.old_idx_sz   = le64_to_cpu(c->mst_node->index_size);
283 	c->lpt_lnum        = le32_to_cpu(c->mst_node->lpt_lnum);
284 	c->lpt_offs        = le32_to_cpu(c->mst_node->lpt_offs);
285 	c->nhead_lnum      = le32_to_cpu(c->mst_node->nhead_lnum);
286 	c->nhead_offs      = le32_to_cpu(c->mst_node->nhead_offs);
287 	c->ltab_lnum       = le32_to_cpu(c->mst_node->ltab_lnum);
288 	c->ltab_offs       = le32_to_cpu(c->mst_node->ltab_offs);
289 	c->lsave_lnum      = le32_to_cpu(c->mst_node->lsave_lnum);
290 	c->lsave_offs      = le32_to_cpu(c->mst_node->lsave_offs);
291 	c->lscan_lnum      = le32_to_cpu(c->mst_node->lscan_lnum);
292 	c->lst.empty_lebs  = le32_to_cpu(c->mst_node->empty_lebs);
293 	c->lst.idx_lebs    = le32_to_cpu(c->mst_node->idx_lebs);
294 	old_leb_cnt        = le32_to_cpu(c->mst_node->leb_cnt);
295 	c->lst.total_free  = le64_to_cpu(c->mst_node->total_free);
296 	c->lst.total_dirty = le64_to_cpu(c->mst_node->total_dirty);
297 	c->lst.total_used  = le64_to_cpu(c->mst_node->total_used);
298 	c->lst.total_dead  = le64_to_cpu(c->mst_node->total_dead);
299 	c->lst.total_dark  = le64_to_cpu(c->mst_node->total_dark);
300 
301 	c->calc_idx_sz = c->bi.old_idx_sz;
302 
303 	if (c->mst_node->flags & cpu_to_le32(UBIFS_MST_NO_ORPHS))
304 		c->no_orphs = 1;
305 
306 	if (old_leb_cnt != c->leb_cnt) {
307 		/* The file system has been resized */
308 		int growth = c->leb_cnt - old_leb_cnt;
309 
310 		if (c->leb_cnt < old_leb_cnt ||
311 		    c->leb_cnt < UBIFS_MIN_LEB_CNT) {
312 			ubifs_err(c, "bad leb_cnt on master node");
313 			ubifs_dump_node(c, c->mst_node);
314 			return -EINVAL;
315 		}
316 
317 		dbg_mnt("Auto resizing (master) from %d LEBs to %d LEBs",
318 			old_leb_cnt, c->leb_cnt);
319 		c->lst.empty_lebs += growth;
320 		c->lst.total_free += growth * (long long)c->leb_size;
321 		c->lst.total_dark += growth * (long long)c->dark_wm;
322 
323 		/*
324 		 * Reflect changes back onto the master node. N.B. the master
325 		 * node gets written immediately whenever mounting (or
326 		 * remounting) in read-write mode, so we do not need to write it
327 		 * here.
328 		 */
329 		c->mst_node->leb_cnt = cpu_to_le32(c->leb_cnt);
330 		c->mst_node->empty_lebs = cpu_to_le32(c->lst.empty_lebs);
331 		c->mst_node->total_free = cpu_to_le64(c->lst.total_free);
332 		c->mst_node->total_dark = cpu_to_le64(c->lst.total_dark);
333 	}
334 
335 	err = validate_master(c);
336 	if (err)
337 		return err;
338 
339 #ifndef __UBOOT__
340 	err = dbg_old_index_check_init(c, &c->zroot);
341 #endif
342 
343 	return err;
344 }
345 
346 #ifndef __UBOOT__
347 /**
348  * ubifs_write_master - write master node.
349  * @c: UBIFS file-system description object
350  *
351  * This function writes the master node. Returns zero in case of success and a
352  * negative error code in case of failure. The master node is written twice to
353  * enable recovery.
354  */
355 int ubifs_write_master(struct ubifs_info *c)
356 {
357 	int err, lnum, offs, len;
358 
359 	ubifs_assert(!c->ro_media && !c->ro_mount);
360 	if (c->ro_error)
361 		return -EROFS;
362 
363 	lnum = UBIFS_MST_LNUM;
364 	offs = c->mst_offs + c->mst_node_alsz;
365 	len = UBIFS_MST_NODE_SZ;
366 
367 	if (offs + UBIFS_MST_NODE_SZ > c->leb_size) {
368 		err = ubifs_leb_unmap(c, lnum);
369 		if (err)
370 			return err;
371 		offs = 0;
372 	}
373 
374 	c->mst_offs = offs;
375 	c->mst_node->highest_inum = cpu_to_le64(c->highest_inum);
376 
377 	err = ubifs_write_node(c, c->mst_node, len, lnum, offs);
378 	if (err)
379 		return err;
380 
381 	lnum += 1;
382 
383 	if (offs == 0) {
384 		err = ubifs_leb_unmap(c, lnum);
385 		if (err)
386 			return err;
387 	}
388 	err = ubifs_write_node(c, c->mst_node, len, lnum, offs);
389 
390 	return err;
391 }
392 #endif
393