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