]>
git.ipfire.org Git - people/ms/u-boot.git/blob - fs/ubifs/master.c
2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * SPDX-License-Identifier: GPL-2.0+
8 * Authors: Artem Bityutskiy (Битюцкий Артём)
12 /* This file implements reading and writing the master node */
16 #include <linux/compat.h>
17 #include <linux/err.h>
18 #include <ubi_uboot.h>
22 * scan_for_master - search the valid master node.
23 * @c: UBIFS file-system description object
25 * This function scans the master node LEBs and search for the latest master
26 * node. Returns zero in case of success, %-EUCLEAN if there master area is
27 * corrupted and requires recovery, and a negative error code in case of
30 static int scan_for_master(struct ubifs_info
*c
)
32 struct ubifs_scan_leb
*sleb
;
33 struct ubifs_scan_node
*snod
;
34 int lnum
, offs
= 0, nodes_cnt
;
36 lnum
= UBIFS_MST_LNUM
;
38 sleb
= ubifs_scan(c
, lnum
, 0, c
->sbuf
, 1);
41 nodes_cnt
= sleb
->nodes_cnt
;
43 snod
= list_entry(sleb
->nodes
.prev
, struct ubifs_scan_node
,
45 if (snod
->type
!= UBIFS_MST_NODE
)
47 memcpy(c
->mst_node
, snod
->node
, snod
->len
);
50 ubifs_scan_destroy(sleb
);
54 sleb
= ubifs_scan(c
, lnum
, 0, c
->sbuf
, 1);
57 if (sleb
->nodes_cnt
!= nodes_cnt
)
61 snod
= list_entry(sleb
->nodes
.prev
, struct ubifs_scan_node
, list
);
62 if (snod
->type
!= UBIFS_MST_NODE
)
64 if (snod
->offs
!= offs
)
66 if (memcmp((void *)c
->mst_node
+ UBIFS_CH_SZ
,
67 (void *)snod
->node
+ UBIFS_CH_SZ
,
68 UBIFS_MST_NODE_SZ
- UBIFS_CH_SZ
))
71 ubifs_scan_destroy(sleb
);
75 ubifs_scan_destroy(sleb
);
79 ubifs_err(c
, "unexpected node type %d master LEB %d:%d",
80 snod
->type
, lnum
, snod
->offs
);
81 ubifs_scan_destroy(sleb
);
86 * validate_master - validate master node.
87 * @c: UBIFS file-system description object
89 * This function validates data which was read from master node. Returns zero
90 * if the data is all right and %-EINVAL if not.
92 static int validate_master(const struct ubifs_info
*c
)
97 if (c
->max_sqnum
>= SQNUM_WATERMARK
) {
102 if (c
->cmt_no
>= c
->max_sqnum
) {
107 if (c
->highest_inum
>= INUM_WATERMARK
) {
112 if (c
->lhead_lnum
< UBIFS_LOG_LNUM
||
113 c
->lhead_lnum
>= UBIFS_LOG_LNUM
+ c
->log_lebs
||
114 c
->lhead_offs
< 0 || c
->lhead_offs
>= c
->leb_size
||
115 c
->lhead_offs
& (c
->min_io_size
- 1)) {
120 if (c
->zroot
.lnum
>= c
->leb_cnt
|| c
->zroot
.lnum
< c
->main_first
||
121 c
->zroot
.offs
>= c
->leb_size
|| c
->zroot
.offs
& 7) {
126 if (c
->zroot
.len
< c
->ranges
[UBIFS_IDX_NODE
].min_len
||
127 c
->zroot
.len
> c
->ranges
[UBIFS_IDX_NODE
].max_len
) {
132 if (c
->gc_lnum
>= c
->leb_cnt
|| c
->gc_lnum
< c
->main_first
) {
137 if (c
->ihead_lnum
>= c
->leb_cnt
|| c
->ihead_lnum
< c
->main_first
||
138 c
->ihead_offs
% c
->min_io_size
|| c
->ihead_offs
< 0 ||
139 c
->ihead_offs
> c
->leb_size
|| c
->ihead_offs
& 7) {
144 main_sz
= (long long)c
->main_lebs
* c
->leb_size
;
145 if (c
->bi
.old_idx_sz
& 7 || c
->bi
.old_idx_sz
>= main_sz
) {
150 if (c
->lpt_lnum
< c
->lpt_first
|| c
->lpt_lnum
> c
->lpt_last
||
151 c
->lpt_offs
< 0 || c
->lpt_offs
+ c
->nnode_sz
> c
->leb_size
) {
156 if (c
->nhead_lnum
< c
->lpt_first
|| c
->nhead_lnum
> c
->lpt_last
||
157 c
->nhead_offs
< 0 || c
->nhead_offs
% c
->min_io_size
||
158 c
->nhead_offs
> c
->leb_size
) {
163 if (c
->ltab_lnum
< c
->lpt_first
|| c
->ltab_lnum
> c
->lpt_last
||
165 c
->ltab_offs
+ c
->ltab_sz
> c
->leb_size
) {
170 if (c
->big_lpt
&& (c
->lsave_lnum
< c
->lpt_first
||
171 c
->lsave_lnum
> c
->lpt_last
|| c
->lsave_offs
< 0 ||
172 c
->lsave_offs
+ c
->lsave_sz
> c
->leb_size
)) {
177 if (c
->lscan_lnum
< c
->main_first
|| c
->lscan_lnum
>= c
->leb_cnt
) {
182 if (c
->lst
.empty_lebs
< 0 || c
->lst
.empty_lebs
> c
->main_lebs
- 2) {
187 if (c
->lst
.idx_lebs
< 0 || c
->lst
.idx_lebs
> c
->main_lebs
- 1) {
192 if (c
->lst
.total_free
< 0 || c
->lst
.total_free
> main_sz
||
193 c
->lst
.total_free
& 7) {
198 if (c
->lst
.total_dirty
< 0 || (c
->lst
.total_dirty
& 7)) {
203 if (c
->lst
.total_used
< 0 || (c
->lst
.total_used
& 7)) {
208 if (c
->lst
.total_free
+ c
->lst
.total_dirty
+
209 c
->lst
.total_used
> main_sz
) {
214 if (c
->lst
.total_dead
+ c
->lst
.total_dark
+
215 c
->lst
.total_used
+ c
->bi
.old_idx_sz
> main_sz
) {
220 if (c
->lst
.total_dead
< 0 ||
221 c
->lst
.total_dead
> c
->lst
.total_free
+ c
->lst
.total_dirty
||
222 c
->lst
.total_dead
& 7) {
227 if (c
->lst
.total_dark
< 0 ||
228 c
->lst
.total_dark
> c
->lst
.total_free
+ c
->lst
.total_dirty
||
229 c
->lst
.total_dark
& 7) {
237 ubifs_err(c
, "bad master node at offset %d error %d", c
->mst_offs
, err
);
238 ubifs_dump_node(c
, c
->mst_node
);
243 * ubifs_read_master - read master node.
244 * @c: UBIFS file-system description object
246 * This function finds and reads the master node during file-system mount. If
247 * the flash is empty, it creates default master node as well. Returns zero in
248 * case of success and a negative error code in case of failure.
250 int ubifs_read_master(struct ubifs_info
*c
)
252 int err
, old_leb_cnt
;
254 c
->mst_node
= kzalloc(c
->mst_node_alsz
, GFP_KERNEL
);
258 err
= scan_for_master(c
);
261 err
= ubifs_recover_master_node(c
);
264 * Note, we do not free 'c->mst_node' here because the
265 * unmount routine will take care of this.
270 /* Make sure that the recovery flag is clear */
271 c
->mst_node
->flags
&= cpu_to_le32(~UBIFS_MST_RCVRY
);
273 c
->max_sqnum
= le64_to_cpu(c
->mst_node
->ch
.sqnum
);
274 c
->highest_inum
= le64_to_cpu(c
->mst_node
->highest_inum
);
275 c
->cmt_no
= le64_to_cpu(c
->mst_node
->cmt_no
);
276 c
->zroot
.lnum
= le32_to_cpu(c
->mst_node
->root_lnum
);
277 c
->zroot
.offs
= le32_to_cpu(c
->mst_node
->root_offs
);
278 c
->zroot
.len
= le32_to_cpu(c
->mst_node
->root_len
);
279 c
->lhead_lnum
= le32_to_cpu(c
->mst_node
->log_lnum
);
280 c
->gc_lnum
= le32_to_cpu(c
->mst_node
->gc_lnum
);
281 c
->ihead_lnum
= le32_to_cpu(c
->mst_node
->ihead_lnum
);
282 c
->ihead_offs
= le32_to_cpu(c
->mst_node
->ihead_offs
);
283 c
->bi
.old_idx_sz
= le64_to_cpu(c
->mst_node
->index_size
);
284 c
->lpt_lnum
= le32_to_cpu(c
->mst_node
->lpt_lnum
);
285 c
->lpt_offs
= le32_to_cpu(c
->mst_node
->lpt_offs
);
286 c
->nhead_lnum
= le32_to_cpu(c
->mst_node
->nhead_lnum
);
287 c
->nhead_offs
= le32_to_cpu(c
->mst_node
->nhead_offs
);
288 c
->ltab_lnum
= le32_to_cpu(c
->mst_node
->ltab_lnum
);
289 c
->ltab_offs
= le32_to_cpu(c
->mst_node
->ltab_offs
);
290 c
->lsave_lnum
= le32_to_cpu(c
->mst_node
->lsave_lnum
);
291 c
->lsave_offs
= le32_to_cpu(c
->mst_node
->lsave_offs
);
292 c
->lscan_lnum
= le32_to_cpu(c
->mst_node
->lscan_lnum
);
293 c
->lst
.empty_lebs
= le32_to_cpu(c
->mst_node
->empty_lebs
);
294 c
->lst
.idx_lebs
= le32_to_cpu(c
->mst_node
->idx_lebs
);
295 old_leb_cnt
= le32_to_cpu(c
->mst_node
->leb_cnt
);
296 c
->lst
.total_free
= le64_to_cpu(c
->mst_node
->total_free
);
297 c
->lst
.total_dirty
= le64_to_cpu(c
->mst_node
->total_dirty
);
298 c
->lst
.total_used
= le64_to_cpu(c
->mst_node
->total_used
);
299 c
->lst
.total_dead
= le64_to_cpu(c
->mst_node
->total_dead
);
300 c
->lst
.total_dark
= le64_to_cpu(c
->mst_node
->total_dark
);
302 c
->calc_idx_sz
= c
->bi
.old_idx_sz
;
304 if (c
->mst_node
->flags
& cpu_to_le32(UBIFS_MST_NO_ORPHS
))
307 if (old_leb_cnt
!= c
->leb_cnt
) {
308 /* The file system has been resized */
309 int growth
= c
->leb_cnt
- old_leb_cnt
;
311 if (c
->leb_cnt
< old_leb_cnt
||
312 c
->leb_cnt
< UBIFS_MIN_LEB_CNT
) {
313 ubifs_err(c
, "bad leb_cnt on master node");
314 ubifs_dump_node(c
, c
->mst_node
);
318 dbg_mnt("Auto resizing (master) from %d LEBs to %d LEBs",
319 old_leb_cnt
, c
->leb_cnt
);
320 c
->lst
.empty_lebs
+= growth
;
321 c
->lst
.total_free
+= growth
* (long long)c
->leb_size
;
322 c
->lst
.total_dark
+= growth
* (long long)c
->dark_wm
;
325 * Reflect changes back onto the master node. N.B. the master
326 * node gets written immediately whenever mounting (or
327 * remounting) in read-write mode, so we do not need to write it
330 c
->mst_node
->leb_cnt
= cpu_to_le32(c
->leb_cnt
);
331 c
->mst_node
->empty_lebs
= cpu_to_le32(c
->lst
.empty_lebs
);
332 c
->mst_node
->total_free
= cpu_to_le64(c
->lst
.total_free
);
333 c
->mst_node
->total_dark
= cpu_to_le64(c
->lst
.total_dark
);
336 err
= validate_master(c
);
341 err
= dbg_old_index_check_init(c
, &c
->zroot
);
349 * ubifs_write_master - write master node.
350 * @c: UBIFS file-system description object
352 * This function writes the master node. Returns zero in case of success and a
353 * negative error code in case of failure. The master node is written twice to
356 int ubifs_write_master(struct ubifs_info
*c
)
358 int err
, lnum
, offs
, len
;
360 ubifs_assert(!c
->ro_media
&& !c
->ro_mount
);
364 lnum
= UBIFS_MST_LNUM
;
365 offs
= c
->mst_offs
+ c
->mst_node_alsz
;
366 len
= UBIFS_MST_NODE_SZ
;
368 if (offs
+ UBIFS_MST_NODE_SZ
> c
->leb_size
) {
369 err
= ubifs_leb_unmap(c
, lnum
);
376 c
->mst_node
->highest_inum
= cpu_to_le64(c
->highest_inum
);
378 err
= ubifs_write_node(c
, c
->mst_node
, len
, lnum
, offs
);
385 err
= ubifs_leb_unmap(c
, lnum
);
389 err
= ubifs_write_node(c
, c
->mst_node
, len
, lnum
, offs
);