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