xref: /openbmc/u-boot/fs/ubifs/sb.c (revision 301e8038)
1 /*
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 /*
24  * This file implements UBIFS superblock. The superblock is stored at the first
25  * LEB of the volume and is never changed by UBIFS. Only user-space tools may
26  * change it. The superblock node mostly contains geometry information.
27  */
28 
29 #include "ubifs.h"
30 
31 /*
32  * Default journal size in logical eraseblocks as a percent of total
33  * flash size.
34  */
35 #define DEFAULT_JNL_PERCENT 5
36 
37 /* Default maximum journal size in bytes */
38 #define DEFAULT_MAX_JNL (32*1024*1024)
39 
40 /* Default indexing tree fanout */
41 #define DEFAULT_FANOUT 8
42 
43 /* Default number of data journal heads */
44 #define DEFAULT_JHEADS_CNT 1
45 
46 /* Default positions of different LEBs in the main area */
47 #define DEFAULT_IDX_LEB  0
48 #define DEFAULT_DATA_LEB 1
49 #define DEFAULT_GC_LEB   2
50 
51 /* Default number of LEB numbers in LPT's save table */
52 #define DEFAULT_LSAVE_CNT 256
53 
54 /* Default reserved pool size as a percent of maximum free space */
55 #define DEFAULT_RP_PERCENT 5
56 
57 /* The default maximum size of reserved pool in bytes */
58 #define DEFAULT_MAX_RP_SIZE (5*1024*1024)
59 
60 /* Default time granularity in nanoseconds */
61 #define DEFAULT_TIME_GRAN 1000000000
62 
63 /**
64  * validate_sb - validate superblock node.
65  * @c: UBIFS file-system description object
66  * @sup: superblock node
67  *
68  * This function validates superblock node @sup. Since most of data was read
69  * from the superblock and stored in @c, the function validates fields in @c
70  * instead. Returns zero in case of success and %-EINVAL in case of validation
71  * failure.
72  */
73 static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
74 {
75 	long long max_bytes;
76 	int err = 1, min_leb_cnt;
77 
78 	if (!c->key_hash) {
79 		err = 2;
80 		goto failed;
81 	}
82 
83 	if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
84 		err = 3;
85 		goto failed;
86 	}
87 
88 	if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
89 		ubifs_err("min. I/O unit mismatch: %d in superblock, %d real",
90 			  le32_to_cpu(sup->min_io_size), c->min_io_size);
91 		goto failed;
92 	}
93 
94 	if (le32_to_cpu(sup->leb_size) != c->leb_size) {
95 		ubifs_err("LEB size mismatch: %d in superblock, %d real",
96 			  le32_to_cpu(sup->leb_size), c->leb_size);
97 		goto failed;
98 	}
99 
100 	if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
101 	    c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
102 	    c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
103 	    c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
104 		err = 4;
105 		goto failed;
106 	}
107 
108 	/*
109 	 * Calculate minimum allowed amount of main area LEBs. This is very
110 	 * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
111 	 * have just read from the superblock.
112 	 */
113 	min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
114 	min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
115 
116 	if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
117 		ubifs_err("bad LEB count: %d in superblock, %d on UBI volume, "
118 			  "%d minimum required", c->leb_cnt, c->vi.size,
119 			  min_leb_cnt);
120 		goto failed;
121 	}
122 
123 	if (c->max_leb_cnt < c->leb_cnt) {
124 		ubifs_err("max. LEB count %d less than LEB count %d",
125 			  c->max_leb_cnt, c->leb_cnt);
126 		goto failed;
127 	}
128 
129 	if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
130 		err = 7;
131 		goto failed;
132 	}
133 
134 	if (c->max_bud_bytes < (long long)c->leb_size * UBIFS_MIN_BUD_LEBS ||
135 	    c->max_bud_bytes > (long long)c->leb_size * c->main_lebs) {
136 		err = 8;
137 		goto failed;
138 	}
139 
140 	if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
141 	    c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
142 		err = 9;
143 		goto failed;
144 	}
145 
146 	if (c->fanout < UBIFS_MIN_FANOUT ||
147 	    ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
148 		err = 10;
149 		goto failed;
150 	}
151 
152 	if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
153 	    c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
154 	    c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
155 		err = 11;
156 		goto failed;
157 	}
158 
159 	if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
160 	    c->orph_lebs + c->main_lebs != c->leb_cnt) {
161 		err = 12;
162 		goto failed;
163 	}
164 
165 	if (c->default_compr < 0 || c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
166 		err = 13;
167 		goto failed;
168 	}
169 
170 	max_bytes = c->main_lebs * (long long)c->leb_size;
171 	if (c->rp_size < 0 || max_bytes < c->rp_size) {
172 		err = 14;
173 		goto failed;
174 	}
175 
176 	if (le32_to_cpu(sup->time_gran) > 1000000000 ||
177 	    le32_to_cpu(sup->time_gran) < 1) {
178 		err = 15;
179 		goto failed;
180 	}
181 
182 	return 0;
183 
184 failed:
185 	ubifs_err("bad superblock, error %d", err);
186 	dbg_dump_node(c, sup);
187 	return -EINVAL;
188 }
189 
190 /**
191  * ubifs_read_sb_node - read superblock node.
192  * @c: UBIFS file-system description object
193  *
194  * This function returns a pointer to the superblock node or a negative error
195  * code.
196  */
197 struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
198 {
199 	struct ubifs_sb_node *sup;
200 	int err;
201 
202 	sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
203 	if (!sup)
204 		return ERR_PTR(-ENOMEM);
205 
206 	err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
207 			      UBIFS_SB_LNUM, 0);
208 	if (err) {
209 		kfree(sup);
210 		return ERR_PTR(err);
211 	}
212 
213 	return sup;
214 }
215 
216 /**
217  * ubifs_read_superblock - read superblock.
218  * @c: UBIFS file-system description object
219  *
220  * This function finds, reads and checks the superblock. If an empty UBI volume
221  * is being mounted, this function creates default superblock. Returns zero in
222  * case of success, and a negative error code in case of failure.
223  */
224 int ubifs_read_superblock(struct ubifs_info *c)
225 {
226 	int err, sup_flags;
227 	struct ubifs_sb_node *sup;
228 
229 	if (c->empty) {
230 		printf("No UBIFS filesystem found!\n");
231 		return -1;
232 	}
233 
234 	sup = ubifs_read_sb_node(c);
235 	if (IS_ERR(sup))
236 		return PTR_ERR(sup);
237 
238 	c->fmt_version = le32_to_cpu(sup->fmt_version);
239 	c->ro_compat_version = le32_to_cpu(sup->ro_compat_version);
240 
241 	/*
242 	 * The software supports all previous versions but not future versions,
243 	 * due to the unavailability of time-travelling equipment.
244 	 */
245 	if (c->fmt_version > UBIFS_FORMAT_VERSION) {
246 		struct super_block *sb = c->vfs_sb;
247 		int mounting_ro = sb->s_flags & MS_RDONLY;
248 
249 		ubifs_assert(!c->ro_media || mounting_ro);
250 		if (!mounting_ro ||
251 		    c->ro_compat_version > UBIFS_RO_COMPAT_VERSION) {
252 			ubifs_err("on-flash format version is w%d/r%d, but "
253 				  "software only supports up to version "
254 				  "w%d/r%d", c->fmt_version,
255 				  c->ro_compat_version, UBIFS_FORMAT_VERSION,
256 				  UBIFS_RO_COMPAT_VERSION);
257 			if (c->ro_compat_version <= UBIFS_RO_COMPAT_VERSION) {
258 				ubifs_msg("only R/O mounting is possible");
259 				err = -EROFS;
260 			} else
261 				err = -EINVAL;
262 			goto out;
263 		}
264 
265 		/*
266 		 * The FS is mounted R/O, and the media format is
267 		 * R/O-compatible with the UBIFS implementation, so we can
268 		 * mount.
269 		 */
270 		c->rw_incompat = 1;
271 	}
272 
273 	if (c->fmt_version < 3) {
274 		ubifs_err("on-flash format version %d is not supported",
275 			  c->fmt_version);
276 		err = -EINVAL;
277 		goto out;
278 	}
279 
280 	switch (sup->key_hash) {
281 	case UBIFS_KEY_HASH_R5:
282 		c->key_hash = key_r5_hash;
283 		c->key_hash_type = UBIFS_KEY_HASH_R5;
284 		break;
285 
286 	case UBIFS_KEY_HASH_TEST:
287 		c->key_hash = key_test_hash;
288 		c->key_hash_type = UBIFS_KEY_HASH_TEST;
289 		break;
290 	};
291 
292 	c->key_fmt = sup->key_fmt;
293 
294 	switch (c->key_fmt) {
295 	case UBIFS_SIMPLE_KEY_FMT:
296 		c->key_len = UBIFS_SK_LEN;
297 		break;
298 	default:
299 		ubifs_err("unsupported key format");
300 		err = -EINVAL;
301 		goto out;
302 	}
303 
304 	c->leb_cnt       = le32_to_cpu(sup->leb_cnt);
305 	c->max_leb_cnt   = le32_to_cpu(sup->max_leb_cnt);
306 	c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
307 	c->log_lebs      = le32_to_cpu(sup->log_lebs);
308 	c->lpt_lebs      = le32_to_cpu(sup->lpt_lebs);
309 	c->orph_lebs     = le32_to_cpu(sup->orph_lebs);
310 	c->jhead_cnt     = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
311 	c->fanout        = le32_to_cpu(sup->fanout);
312 	c->lsave_cnt     = le32_to_cpu(sup->lsave_cnt);
313 	c->default_compr = le16_to_cpu(sup->default_compr);
314 	c->rp_size       = le64_to_cpu(sup->rp_size);
315 	c->rp_uid        = le32_to_cpu(sup->rp_uid);
316 	c->rp_gid        = le32_to_cpu(sup->rp_gid);
317 	sup_flags        = le32_to_cpu(sup->flags);
318 
319 	c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
320 	memcpy(&c->uuid, &sup->uuid, 16);
321 	c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
322 
323 	/* Automatically increase file system size to the maximum size */
324 	c->old_leb_cnt = c->leb_cnt;
325 	if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
326 		c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
327 		dbg_mnt("Auto resizing (ro) from %d LEBs to %d LEBs",
328 			c->old_leb_cnt,	c->leb_cnt);
329 	}
330 
331 	c->log_bytes = (long long)c->log_lebs * c->leb_size;
332 	c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
333 	c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
334 	c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
335 	c->orph_first = c->lpt_last + 1;
336 	c->orph_last = c->orph_first + c->orph_lebs - 1;
337 	c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
338 	c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
339 	c->main_first = c->leb_cnt - c->main_lebs;
340 	c->report_rp_size = ubifs_reported_space(c, c->rp_size);
341 
342 	err = validate_sb(c, sup);
343 out:
344 	kfree(sup);
345 	return err;
346 }
347