Lines Matching +full:- +full:c
1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (C) 2006-2008 Nokia Corporation.
21 * scan_for_master - search the valid master node.
22 * @c: UBIFS file-system description object
25 * node. Returns zero in case of success, %-EUCLEAN if there master area is
29 static int scan_for_master(struct ubifs_info *c) in scan_for_master() argument
37 sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1); in scan_for_master()
40 nodes_cnt = sleb->nodes_cnt; in scan_for_master()
42 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, in scan_for_master()
44 if (snod->type != UBIFS_MST_NODE) in scan_for_master()
46 memcpy(c->mst_node, snod->node, snod->len); in scan_for_master()
47 offs = snod->offs; in scan_for_master()
53 sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1); in scan_for_master()
56 if (sleb->nodes_cnt != nodes_cnt) in scan_for_master()
58 if (!sleb->nodes_cnt) in scan_for_master()
60 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list); in scan_for_master()
61 if (snod->type != UBIFS_MST_NODE) in scan_for_master()
63 if (snod->offs != offs) in scan_for_master()
65 if (memcmp((void *)c->mst_node + UBIFS_CH_SZ, in scan_for_master()
66 (void *)snod->node + UBIFS_CH_SZ, in scan_for_master()
67 UBIFS_MST_NODE_SZ - UBIFS_CH_SZ)) in scan_for_master()
69 c->mst_offs = offs; in scan_for_master()
75 return -EUCLEAN; in scan_for_master()
78 ubifs_err(c, "unexpected node type %d master LEB %d:%d", in scan_for_master()
79 snod->type, lnum, snod->offs); in scan_for_master()
81 return -EINVAL; in scan_for_master()
85 * validate_master - validate master node.
86 * @c: UBIFS file-system description object
89 * if the data is all right and %-EINVAL if not.
91 static int validate_master(const struct ubifs_info *c) in validate_master() argument
96 if (c->max_sqnum >= SQNUM_WATERMARK) { in validate_master()
101 if (c->cmt_no >= c->max_sqnum) { in validate_master()
106 if (c->highest_inum >= INUM_WATERMARK) { in validate_master()
111 if (c->lhead_lnum < UBIFS_LOG_LNUM || in validate_master()
112 c->lhead_lnum >= UBIFS_LOG_LNUM + c->log_lebs || in validate_master()
113 c->lhead_offs < 0 || c->lhead_offs >= c->leb_size || in validate_master()
114 c->lhead_offs & (c->min_io_size - 1)) { in validate_master()
119 if (c->zroot.lnum >= c->leb_cnt || c->zroot.lnum < c->main_first || in validate_master()
120 c->zroot.offs >= c->leb_size || c->zroot.offs & 7) { in validate_master()
125 if (c->zroot.len < c->ranges[UBIFS_IDX_NODE].min_len || in validate_master()
126 c->zroot.len > c->ranges[UBIFS_IDX_NODE].max_len) { in validate_master()
131 if (c->gc_lnum >= c->leb_cnt || c->gc_lnum < c->main_first) { in validate_master()
136 if (c->ihead_lnum >= c->leb_cnt || c->ihead_lnum < c->main_first || in validate_master()
137 c->ihead_offs % c->min_io_size || c->ihead_offs < 0 || in validate_master()
138 c->ihead_offs > c->leb_size || c->ihead_offs & 7) { in validate_master()
143 main_sz = (long long)c->main_lebs * c->leb_size; in validate_master()
144 if (c->bi.old_idx_sz & 7 || c->bi.old_idx_sz >= main_sz) { in validate_master()
149 if (c->lpt_lnum < c->lpt_first || c->lpt_lnum > c->lpt_last || in validate_master()
150 c->lpt_offs < 0 || c->lpt_offs + c->nnode_sz > c->leb_size) { in validate_master()
155 if (c->nhead_lnum < c->lpt_first || c->nhead_lnum > c->lpt_last || in validate_master()
156 c->nhead_offs < 0 || c->nhead_offs % c->min_io_size || in validate_master()
157 c->nhead_offs > c->leb_size) { in validate_master()
162 if (c->ltab_lnum < c->lpt_first || c->ltab_lnum > c->lpt_last || in validate_master()
163 c->ltab_offs < 0 || in validate_master()
164 c->ltab_offs + c->ltab_sz > c->leb_size) { in validate_master()
169 if (c->big_lpt && (c->lsave_lnum < c->lpt_first || in validate_master()
170 c->lsave_lnum > c->lpt_last || c->lsave_offs < 0 || in validate_master()
171 c->lsave_offs + c->lsave_sz > c->leb_size)) { in validate_master()
176 if (c->lscan_lnum < c->main_first || c->lscan_lnum >= c->leb_cnt) { in validate_master()
181 if (c->lst.empty_lebs < 0 || c->lst.empty_lebs > c->main_lebs - 2) { in validate_master()
186 if (c->lst.idx_lebs < 0 || c->lst.idx_lebs > c->main_lebs - 1) { in validate_master()
191 if (c->lst.total_free < 0 || c->lst.total_free > main_sz || in validate_master()
192 c->lst.total_free & 7) { in validate_master()
197 if (c->lst.total_dirty < 0 || (c->lst.total_dirty & 7)) { in validate_master()
202 if (c->lst.total_used < 0 || (c->lst.total_used & 7)) { in validate_master()
207 if (c->lst.total_free + c->lst.total_dirty + in validate_master()
208 c->lst.total_used > main_sz) { in validate_master()
213 if (c->lst.total_dead + c->lst.total_dark + in validate_master()
214 c->lst.total_used + c->bi.old_idx_sz > main_sz) { in validate_master()
219 if (c->lst.total_dead < 0 || in validate_master()
220 c->lst.total_dead > c->lst.total_free + c->lst.total_dirty || in validate_master()
221 c->lst.total_dead & 7) { in validate_master()
226 if (c->lst.total_dark < 0 || in validate_master()
227 c->lst.total_dark > c->lst.total_free + c->lst.total_dirty || in validate_master()
228 c->lst.total_dark & 7) { in validate_master()
236 ubifs_err(c, "bad master node at offset %d error %d", c->mst_offs, err); in validate_master()
237 ubifs_dump_node(c, c->mst_node); in validate_master()
238 return -EINVAL; in validate_master()
242 * ubifs_read_master - read master node.
243 * @c: UBIFS file-system description object
245 * This function finds and reads the master node during file-system mount. If
249 int ubifs_read_master(struct ubifs_info *c) in ubifs_read_master() argument
253 c->mst_node = kzalloc(c->mst_node_alsz, GFP_KERNEL); in ubifs_read_master()
254 if (!c->mst_node) in ubifs_read_master()
255 return -ENOMEM; in ubifs_read_master()
257 err = scan_for_master(c); in ubifs_read_master()
259 if (err == -EUCLEAN) in ubifs_read_master()
260 err = ubifs_recover_master_node(c); in ubifs_read_master()
263 * Note, we do not free 'c->mst_node' here because the in ubifs_read_master()
270 c->mst_node->flags &= cpu_to_le32(~UBIFS_MST_RCVRY); in ubifs_read_master()
272 c->max_sqnum = le64_to_cpu(c->mst_node->ch.sqnum); in ubifs_read_master()
273 c->highest_inum = le64_to_cpu(c->mst_node->highest_inum); in ubifs_read_master()
274 c->cmt_no = le64_to_cpu(c->mst_node->cmt_no); in ubifs_read_master()
275 c->zroot.lnum = le32_to_cpu(c->mst_node->root_lnum); in ubifs_read_master()
276 c->zroot.offs = le32_to_cpu(c->mst_node->root_offs); in ubifs_read_master()
277 c->zroot.len = le32_to_cpu(c->mst_node->root_len); in ubifs_read_master()
278 c->lhead_lnum = le32_to_cpu(c->mst_node->log_lnum); in ubifs_read_master()
279 c->gc_lnum = le32_to_cpu(c->mst_node->gc_lnum); in ubifs_read_master()
280 c->ihead_lnum = le32_to_cpu(c->mst_node->ihead_lnum); in ubifs_read_master()
281 c->ihead_offs = le32_to_cpu(c->mst_node->ihead_offs); in ubifs_read_master()
282 c->bi.old_idx_sz = le64_to_cpu(c->mst_node->index_size); in ubifs_read_master()
283 c->lpt_lnum = le32_to_cpu(c->mst_node->lpt_lnum); in ubifs_read_master()
284 c->lpt_offs = le32_to_cpu(c->mst_node->lpt_offs); in ubifs_read_master()
285 c->nhead_lnum = le32_to_cpu(c->mst_node->nhead_lnum); in ubifs_read_master()
286 c->nhead_offs = le32_to_cpu(c->mst_node->nhead_offs); in ubifs_read_master()
287 c->ltab_lnum = le32_to_cpu(c->mst_node->ltab_lnum); in ubifs_read_master()
288 c->ltab_offs = le32_to_cpu(c->mst_node->ltab_offs); in ubifs_read_master()
289 c->lsave_lnum = le32_to_cpu(c->mst_node->lsave_lnum); in ubifs_read_master()
290 c->lsave_offs = le32_to_cpu(c->mst_node->lsave_offs); in ubifs_read_master()
291 c->lscan_lnum = le32_to_cpu(c->mst_node->lscan_lnum); in ubifs_read_master()
292 c->lst.empty_lebs = le32_to_cpu(c->mst_node->empty_lebs); in ubifs_read_master()
293 c->lst.idx_lebs = le32_to_cpu(c->mst_node->idx_lebs); in ubifs_read_master()
294 old_leb_cnt = le32_to_cpu(c->mst_node->leb_cnt); in ubifs_read_master()
295 c->lst.total_free = le64_to_cpu(c->mst_node->total_free); in ubifs_read_master()
296 c->lst.total_dirty = le64_to_cpu(c->mst_node->total_dirty); in ubifs_read_master()
297 c->lst.total_used = le64_to_cpu(c->mst_node->total_used); in ubifs_read_master()
298 c->lst.total_dead = le64_to_cpu(c->mst_node->total_dead); in ubifs_read_master()
299 c->lst.total_dark = le64_to_cpu(c->mst_node->total_dark); in ubifs_read_master()
301 c->calc_idx_sz = c->bi.old_idx_sz; in ubifs_read_master()
303 if (c->mst_node->flags & cpu_to_le32(UBIFS_MST_NO_ORPHS)) in ubifs_read_master()
304 c->no_orphs = 1; in ubifs_read_master()
306 if (old_leb_cnt != c->leb_cnt) { in ubifs_read_master()
308 int growth = c->leb_cnt - old_leb_cnt; in ubifs_read_master()
310 if (c->leb_cnt < old_leb_cnt || in ubifs_read_master()
311 c->leb_cnt < UBIFS_MIN_LEB_CNT) { in ubifs_read_master()
312 ubifs_err(c, "bad leb_cnt on master node"); in ubifs_read_master()
313 ubifs_dump_node(c, c->mst_node); in ubifs_read_master()
314 return -EINVAL; in ubifs_read_master()
318 old_leb_cnt, c->leb_cnt); in ubifs_read_master()
319 c->lst.empty_lebs += growth; in ubifs_read_master()
320 c->lst.total_free += growth * (long long)c->leb_size; in ubifs_read_master()
321 c->lst.total_dark += growth * (long long)c->dark_wm; in ubifs_read_master()
326 * remounting) in read-write mode, so we do not need to write it in ubifs_read_master()
329 c->mst_node->leb_cnt = cpu_to_le32(c->leb_cnt); in ubifs_read_master()
330 c->mst_node->empty_lebs = cpu_to_le32(c->lst.empty_lebs); in ubifs_read_master()
331 c->mst_node->total_free = cpu_to_le64(c->lst.total_free); in ubifs_read_master()
332 c->mst_node->total_dark = cpu_to_le64(c->lst.total_dark); in ubifs_read_master()
335 err = validate_master(c); in ubifs_read_master()
340 err = dbg_old_index_check_init(c, &c->zroot); in ubifs_read_master()
348 * ubifs_write_master - write master node.
349 * @c: UBIFS file-system description object
355 int ubifs_write_master(struct ubifs_info *c) in ubifs_write_master() argument
359 ubifs_assert(!c->ro_media && !c->ro_mount); in ubifs_write_master()
360 if (c->ro_error) in ubifs_write_master()
361 return -EROFS; in ubifs_write_master()
364 offs = c->mst_offs + c->mst_node_alsz; in ubifs_write_master()
367 if (offs + UBIFS_MST_NODE_SZ > c->leb_size) { in ubifs_write_master()
368 err = ubifs_leb_unmap(c, lnum); in ubifs_write_master()
374 c->mst_offs = offs; in ubifs_write_master()
375 c->mst_node->highest_inum = cpu_to_le64(c->highest_inum); in ubifs_write_master()
377 err = ubifs_write_node(c, c->mst_node, len, lnum, offs); in ubifs_write_master()
384 err = ubifs_leb_unmap(c, lnum); in ubifs_write_master()
388 err = ubifs_write_node(c, c->mst_node, len, lnum, offs); in ubifs_write_master()