blob: 7f0967771c9f7e4711353a24f7c7ac64d34fe292 [file] [log] [blame]
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001/*
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"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Artem Bityutskiy4d61db42008-12-18 14:06:51 +020031#include <linux/math64.h>
Andy Shevchenko8da4b8c2016-05-20 17:01:00 -070032#include <linux/uuid.h>
Artem Bityutskiy1e517642008-07-14 19:08:37 +030033
34/*
35 * Default journal size in logical eraseblocks as a percent of total
36 * flash size.
37 */
38#define DEFAULT_JNL_PERCENT 5
39
40/* Default maximum journal size in bytes */
41#define DEFAULT_MAX_JNL (32*1024*1024)
42
43/* Default indexing tree fanout */
44#define DEFAULT_FANOUT 8
45
46/* Default number of data journal heads */
47#define DEFAULT_JHEADS_CNT 1
48
49/* Default positions of different LEBs in the main area */
50#define DEFAULT_IDX_LEB 0
51#define DEFAULT_DATA_LEB 1
52#define DEFAULT_GC_LEB 2
53
54/* Default number of LEB numbers in LPT's save table */
55#define DEFAULT_LSAVE_CNT 256
56
57/* Default reserved pool size as a percent of maximum free space */
58#define DEFAULT_RP_PERCENT 5
59
60/* The default maximum size of reserved pool in bytes */
61#define DEFAULT_MAX_RP_SIZE (5*1024*1024)
62
63/* Default time granularity in nanoseconds */
64#define DEFAULT_TIME_GRAN 1000000000
65
66/**
67 * create_default_filesystem - format empty UBI volume.
68 * @c: UBIFS file-system description object
69 *
70 * This function creates default empty file-system. Returns zero in case of
71 * success and a negative error code in case of failure.
72 */
73static int create_default_filesystem(struct ubifs_info *c)
74{
75 struct ubifs_sb_node *sup;
76 struct ubifs_mst_node *mst;
77 struct ubifs_idx_node *idx;
78 struct ubifs_branch *br;
79 struct ubifs_ino_node *ino;
80 struct ubifs_cs_node *cs;
81 union ubifs_key key;
82 int err, tmp, jnl_lebs, log_lebs, max_buds, main_lebs, main_first;
83 int lpt_lebs, lpt_first, orph_lebs, big_lpt, ino_waste, sup_flags = 0;
84 int min_leb_cnt = UBIFS_MIN_LEB_CNT;
Sascha Hauerc4de6d72018-09-07 14:36:23 +020085 int idx_node_size;
Artem Bityutskiy4d61db42008-12-18 14:06:51 +020086 long long tmp64, main_bytes;
Harvey Harrison0ecb9522008-10-24 10:52:57 -070087 __le64 tmp_le64;
Deepa Dinamani607a11a2017-05-08 15:59:25 -070088 __le32 tmp_le32;
Arnd Bergmann0eca0b82018-07-13 16:31:55 +020089 struct timespec64 ts;
Sascha Hauerb5b1f0832018-09-07 14:36:41 +020090 u8 hash_lpt[UBIFS_HASH_ARR_SZ];
Artem Bityutskiy1e517642008-07-14 19:08:37 +030091
92 /* Some functions called from here depend on the @c->key_len filed */
93 c->key_len = UBIFS_SK_LEN;
94
95 /*
96 * First of all, we have to calculate default file-system geometry -
97 * log size, journal size, etc.
98 */
99 if (c->leb_cnt < 0x7FFFFFFF / DEFAULT_JNL_PERCENT)
100 /* We can first multiply then divide and have no overflow */
101 jnl_lebs = c->leb_cnt * DEFAULT_JNL_PERCENT / 100;
102 else
103 jnl_lebs = (c->leb_cnt / 100) * DEFAULT_JNL_PERCENT;
104
105 if (jnl_lebs < UBIFS_MIN_JNL_LEBS)
106 jnl_lebs = UBIFS_MIN_JNL_LEBS;
107 if (jnl_lebs * c->leb_size > DEFAULT_MAX_JNL)
108 jnl_lebs = DEFAULT_MAX_JNL / c->leb_size;
109
110 /*
111 * The log should be large enough to fit reference nodes for all bud
112 * LEBs. Because buds do not have to start from the beginning of LEBs
113 * (half of the LEB may contain committed data), the log should
114 * generally be larger, make it twice as large.
115 */
116 tmp = 2 * (c->ref_node_alsz * jnl_lebs) + c->leb_size - 1;
117 log_lebs = tmp / c->leb_size;
118 /* Plus one LEB reserved for commit */
119 log_lebs += 1;
120 if (c->leb_cnt - min_leb_cnt > 8) {
121 /* And some extra space to allow writes while committing */
122 log_lebs += 1;
123 min_leb_cnt += 1;
124 }
125
126 max_buds = jnl_lebs - log_lebs;
127 if (max_buds < UBIFS_MIN_BUD_LEBS)
128 max_buds = UBIFS_MIN_BUD_LEBS;
129
130 /*
131 * Orphan nodes are stored in a separate area. One node can store a lot
132 * of orphan inode numbers, but when new orphan comes we just add a new
133 * orphan node. At some point the nodes are consolidated into one
134 * orphan node.
135 */
136 orph_lebs = UBIFS_MIN_ORPH_LEBS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300137 if (c->leb_cnt - min_leb_cnt > 1)
138 /*
139 * For debugging purposes it is better to have at least 2
140 * orphan LEBs, because the orphan subsystem would need to do
141 * consolidations and would be stressed more.
142 */
143 orph_lebs += 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300144
145 main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS - log_lebs;
146 main_lebs -= orph_lebs;
147
148 lpt_first = UBIFS_LOG_LNUM + log_lebs;
149 c->lsave_cnt = DEFAULT_LSAVE_CNT;
150 c->max_leb_cnt = c->leb_cnt;
151 err = ubifs_create_dflt_lpt(c, &main_lebs, lpt_first, &lpt_lebs,
Sascha Hauerb5b1f0832018-09-07 14:36:41 +0200152 &big_lpt, hash_lpt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300153 if (err)
154 return err;
155
156 dbg_gen("LEB Properties Tree created (LEBs %d-%d)", lpt_first,
157 lpt_first + lpt_lebs - 1);
158
159 main_first = c->leb_cnt - main_lebs;
160
Sascha Hauerc4de6d72018-09-07 14:36:23 +0200161 sup = kzalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_KERNEL);
162 mst = kzalloc(c->mst_node_alsz, GFP_KERNEL);
163 idx_node_size = ubifs_idx_node_sz(c, 1);
164 idx = kzalloc(ALIGN(tmp, c->min_io_size), GFP_KERNEL);
165 ino = kzalloc(ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size), GFP_KERNEL);
166 cs = kzalloc(ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size), GFP_KERNEL);
167
168 if (!sup || !mst || !idx || !ino || !cs) {
169 err = -ENOMEM;
170 goto out;
171 }
172
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300173 /* Create default superblock */
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300174
Artem Bityutskiy4d61db42008-12-18 14:06:51 +0200175 tmp64 = (long long)max_buds * c->leb_size;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300176 if (big_lpt)
177 sup_flags |= UBIFS_FLG_BIGLPT;
Richard Weinbergerd63d61c2016-10-19 15:59:12 +0200178 sup_flags |= UBIFS_FLG_DOUBLE_HASH;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300179
180 sup->ch.node_type = UBIFS_SB_NODE;
181 sup->key_hash = UBIFS_KEY_HASH_R5;
182 sup->flags = cpu_to_le32(sup_flags);
183 sup->min_io_size = cpu_to_le32(c->min_io_size);
184 sup->leb_size = cpu_to_le32(c->leb_size);
185 sup->leb_cnt = cpu_to_le32(c->leb_cnt);
186 sup->max_leb_cnt = cpu_to_le32(c->max_leb_cnt);
187 sup->max_bud_bytes = cpu_to_le64(tmp64);
188 sup->log_lebs = cpu_to_le32(log_lebs);
189 sup->lpt_lebs = cpu_to_le32(lpt_lebs);
190 sup->orph_lebs = cpu_to_le32(orph_lebs);
191 sup->jhead_cnt = cpu_to_le32(DEFAULT_JHEADS_CNT);
192 sup->fanout = cpu_to_le32(DEFAULT_FANOUT);
193 sup->lsave_cnt = cpu_to_le32(c->lsave_cnt);
194 sup->fmt_version = cpu_to_le32(UBIFS_FORMAT_VERSION);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300195 sup->time_gran = cpu_to_le32(DEFAULT_TIME_GRAN);
Artem Bityutskiy553dea42008-11-01 14:57:49 +0200196 if (c->mount_opts.override_compr)
197 sup->default_compr = cpu_to_le16(c->mount_opts.compr_type);
198 else
199 sup->default_compr = cpu_to_le16(UBIFS_COMPR_LZO);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300200
201 generate_random_uuid(sup->uuid);
202
Artem Bityutskiy4d61db42008-12-18 14:06:51 +0200203 main_bytes = (long long)main_lebs * c->leb_size;
204 tmp64 = div_u64(main_bytes * DEFAULT_RP_PERCENT, 100);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300205 if (tmp64 > DEFAULT_MAX_RP_SIZE)
206 tmp64 = DEFAULT_MAX_RP_SIZE;
207 sup->rp_size = cpu_to_le64(tmp64);
Artem Bityutskiy963f0cf2009-03-26 12:51:21 +0200208 sup->ro_compat_version = cpu_to_le32(UBIFS_RO_COMPAT_VERSION);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300209
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300210 dbg_gen("default superblock created at LEB 0:0");
211
212 /* Create default master node */
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300213
214 mst->ch.node_type = UBIFS_MST_NODE;
215 mst->log_lnum = cpu_to_le32(UBIFS_LOG_LNUM);
216 mst->highest_inum = cpu_to_le64(UBIFS_FIRST_INO);
217 mst->cmt_no = 0;
218 mst->root_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
219 mst->root_offs = 0;
220 tmp = ubifs_idx_node_sz(c, 1);
221 mst->root_len = cpu_to_le32(tmp);
222 mst->gc_lnum = cpu_to_le32(main_first + DEFAULT_GC_LEB);
223 mst->ihead_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
224 mst->ihead_offs = cpu_to_le32(ALIGN(tmp, c->min_io_size));
225 mst->index_size = cpu_to_le64(ALIGN(tmp, 8));
226 mst->lpt_lnum = cpu_to_le32(c->lpt_lnum);
227 mst->lpt_offs = cpu_to_le32(c->lpt_offs);
228 mst->nhead_lnum = cpu_to_le32(c->nhead_lnum);
229 mst->nhead_offs = cpu_to_le32(c->nhead_offs);
230 mst->ltab_lnum = cpu_to_le32(c->ltab_lnum);
231 mst->ltab_offs = cpu_to_le32(c->ltab_offs);
232 mst->lsave_lnum = cpu_to_le32(c->lsave_lnum);
233 mst->lsave_offs = cpu_to_le32(c->lsave_offs);
234 mst->lscan_lnum = cpu_to_le32(main_first);
235 mst->empty_lebs = cpu_to_le32(main_lebs - 2);
236 mst->idx_lebs = cpu_to_le32(1);
237 mst->leb_cnt = cpu_to_le32(c->leb_cnt);
238
239 /* Calculate lprops statistics */
240 tmp64 = main_bytes;
241 tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
242 tmp64 -= ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
243 mst->total_free = cpu_to_le64(tmp64);
244
245 tmp64 = ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
246 ino_waste = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size) -
247 UBIFS_INO_NODE_SZ;
248 tmp64 += ino_waste;
249 tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), 8);
250 mst->total_dirty = cpu_to_le64(tmp64);
251
252 /* The indexing LEB does not contribute to dark space */
srimugunthan dhandapani7606f852011-08-26 16:08:39 +0530253 tmp64 = ((long long)(c->main_lebs - 1) * c->dark_wm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300254 mst->total_dark = cpu_to_le64(tmp64);
255
256 mst->total_used = cpu_to_le64(UBIFS_INO_NODE_SZ);
257
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300258 dbg_gen("default master node created at LEB %d:0", UBIFS_MST_LNUM);
259
260 /* Create the root indexing node */
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300261
262 c->key_fmt = UBIFS_SIMPLE_KEY_FMT;
263 c->key_hash = key_r5_hash;
264
265 idx->ch.node_type = UBIFS_IDX_NODE;
266 idx->child_cnt = cpu_to_le16(1);
267 ino_key_init(c, &key, UBIFS_ROOT_INO);
268 br = ubifs_idx_branch(c, idx, 0);
269 key_write_idx(c, &key, &br->key);
270 br->lnum = cpu_to_le32(main_first + DEFAULT_DATA_LEB);
271 br->len = cpu_to_le32(UBIFS_INO_NODE_SZ);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300272
273 dbg_gen("default root indexing node created LEB %d:0",
274 main_first + DEFAULT_IDX_LEB);
275
276 /* Create default root inode */
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300277
278 ino_key_init_flash(c, &ino->key, UBIFS_ROOT_INO);
279 ino->ch.node_type = UBIFS_INO_NODE;
280 ino->creat_sqnum = cpu_to_le64(++c->max_sqnum);
281 ino->nlink = cpu_to_le32(2);
Deepa Dinamani607a11a2017-05-08 15:59:25 -0700282
Arnd Bergmann0eca0b82018-07-13 16:31:55 +0200283 ktime_get_real_ts64(&ts);
284 ts = timespec64_trunc(ts, DEFAULT_TIME_GRAN);
Deepa Dinamani607a11a2017-05-08 15:59:25 -0700285 tmp_le64 = cpu_to_le64(ts.tv_sec);
Harvey Harrison0ecb9522008-10-24 10:52:57 -0700286 ino->atime_sec = tmp_le64;
287 ino->ctime_sec = tmp_le64;
288 ino->mtime_sec = tmp_le64;
Deepa Dinamani607a11a2017-05-08 15:59:25 -0700289 tmp_le32 = cpu_to_le32(ts.tv_nsec);
290 ino->atime_nsec = tmp_le32;
291 ino->ctime_nsec = tmp_le32;
292 ino->mtime_nsec = tmp_le32;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300293 ino->mode = cpu_to_le32(S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
294 ino->size = cpu_to_le64(UBIFS_INO_NODE_SZ);
295
296 /* Set compression enabled by default */
297 ino->flags = cpu_to_le32(UBIFS_COMPR_FL);
298
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300299 dbg_gen("root inode created at LEB %d:0",
300 main_first + DEFAULT_DATA_LEB);
301
302 /*
303 * The first node in the log has to be the commit start node. This is
304 * always the case during normal file-system operation. Write a fake
305 * commit start node to the log.
306 */
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300307
308 cs->ch.node_type = UBIFS_CS_NODE;
Sascha Hauerc4de6d72018-09-07 14:36:23 +0200309
310 err = ubifs_write_node(c, sup, UBIFS_SB_NODE_SZ, 0, 0);
hujianyang6dcfb802014-06-11 10:38:45 +0800311 if (err)
Sascha Hauerc4de6d72018-09-07 14:36:23 +0200312 goto out;
313
314 err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0);
315 if (err)
316 goto out;
317
318 err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1,
319 0);
320 if (err)
321 goto out;
322
323 err = ubifs_write_node(c, idx, idx_node_size, main_first + DEFAULT_IDX_LEB, 0);
324 if (err)
325 goto out;
326
327 err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
328 main_first + DEFAULT_DATA_LEB, 0);
329 if (err)
330 goto out;
331
332 err = ubifs_write_node(c, cs, UBIFS_CS_NODE_SZ, UBIFS_LOG_LNUM, 0);
333 if (err)
334 goto out;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300335
Sheng Yong235c3622015-03-20 10:39:42 +0000336 ubifs_msg(c, "default file-system created");
Sascha Hauerc4de6d72018-09-07 14:36:23 +0200337
338 err = 0;
339out:
340 kfree(sup);
341 kfree(mst);
342 kfree(idx);
343 kfree(ino);
344 kfree(cs);
345
346 return err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300347}
348
349/**
350 * validate_sb - validate superblock node.
351 * @c: UBIFS file-system description object
352 * @sup: superblock node
353 *
354 * This function validates superblock node @sup. Since most of data was read
355 * from the superblock and stored in @c, the function validates fields in @c
356 * instead. Returns zero in case of success and %-EINVAL in case of validation
357 * failure.
358 */
359static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
360{
361 long long max_bytes;
362 int err = 1, min_leb_cnt;
363
364 if (!c->key_hash) {
365 err = 2;
366 goto failed;
367 }
368
369 if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
370 err = 3;
371 goto failed;
372 }
373
374 if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
Sheng Yong235c3622015-03-20 10:39:42 +0000375 ubifs_err(c, "min. I/O unit mismatch: %d in superblock, %d real",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300376 le32_to_cpu(sup->min_io_size), c->min_io_size);
377 goto failed;
378 }
379
380 if (le32_to_cpu(sup->leb_size) != c->leb_size) {
Sheng Yong235c3622015-03-20 10:39:42 +0000381 ubifs_err(c, "LEB size mismatch: %d in superblock, %d real",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300382 le32_to_cpu(sup->leb_size), c->leb_size);
383 goto failed;
384 }
385
386 if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
387 c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
388 c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
389 c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
390 err = 4;
391 goto failed;
392 }
393
394 /*
395 * Calculate minimum allowed amount of main area LEBs. This is very
396 * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
397 * have just read from the superblock.
398 */
399 min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
400 min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
401
402 if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
Sheng Yong235c3622015-03-20 10:39:42 +0000403 ubifs_err(c, "bad LEB count: %d in superblock, %d on UBI volume, %d minimum required",
Artem Bityutskiy79fda512012-08-27 13:34:09 +0300404 c->leb_cnt, c->vi.size, min_leb_cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300405 goto failed;
406 }
407
408 if (c->max_leb_cnt < c->leb_cnt) {
Sheng Yong235c3622015-03-20 10:39:42 +0000409 ubifs_err(c, "max. LEB count %d less than LEB count %d",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300410 c->max_leb_cnt, c->leb_cnt);
411 goto failed;
412 }
413
414 if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
Sheng Yong235c3622015-03-20 10:39:42 +0000415 ubifs_err(c, "too few main LEBs count %d, must be at least %d",
Artem Bityutskiy5a1f36c2012-03-07 16:29:45 +0200416 c->main_lebs, UBIFS_MIN_MAIN_LEBS);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300417 goto failed;
418 }
419
Artem Bityutskiy5a1f36c2012-03-07 16:29:45 +0200420 max_bytes = (long long)c->leb_size * UBIFS_MIN_BUD_LEBS;
421 if (c->max_bud_bytes < max_bytes) {
Sheng Yong235c3622015-03-20 10:39:42 +0000422 ubifs_err(c, "too small journal (%lld bytes), must be at least %lld bytes",
Artem Bityutskiy79fda512012-08-27 13:34:09 +0300423 c->max_bud_bytes, max_bytes);
Artem Bityutskiy5a1f36c2012-03-07 16:29:45 +0200424 goto failed;
425 }
426
427 max_bytes = (long long)c->leb_size * c->main_lebs;
428 if (c->max_bud_bytes > max_bytes) {
Sheng Yong235c3622015-03-20 10:39:42 +0000429 ubifs_err(c, "too large journal size (%lld bytes), only %lld bytes available in the main area",
Artem Bityutskiy5a1f36c2012-03-07 16:29:45 +0200430 c->max_bud_bytes, max_bytes);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300431 goto failed;
432 }
433
434 if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
435 c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
436 err = 9;
437 goto failed;
438 }
439
440 if (c->fanout < UBIFS_MIN_FANOUT ||
441 ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
442 err = 10;
443 goto failed;
444 }
445
446 if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
447 c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
448 c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
449 err = 11;
450 goto failed;
451 }
452
453 if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
454 c->orph_lebs + c->main_lebs != c->leb_cnt) {
455 err = 12;
456 goto failed;
457 }
458
hujianyangb793a8c2014-06-11 10:42:52 +0800459 if (c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300460 err = 13;
461 goto failed;
462 }
463
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300464 if (c->rp_size < 0 || max_bytes < c->rp_size) {
465 err = 14;
466 goto failed;
467 }
468
469 if (le32_to_cpu(sup->time_gran) > 1000000000 ||
470 le32_to_cpu(sup->time_gran) < 1) {
471 err = 15;
472 goto failed;
473 }
474
Richard Weinbergerfc4b8912016-10-19 23:46:39 +0200475 if (!c->double_hash && c->fmt_version >= 5) {
476 err = 16;
477 goto failed;
478 }
479
480 if (c->encrypted && c->fmt_version < 5) {
481 err = 17;
482 goto failed;
483 }
484
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300485 return 0;
486
487failed:
Sheng Yong235c3622015-03-20 10:39:42 +0000488 ubifs_err(c, "bad superblock, error %d", err);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300489 ubifs_dump_node(c, sup);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300490 return -EINVAL;
491}
492
493/**
494 * ubifs_read_sb_node - read superblock node.
495 * @c: UBIFS file-system description object
496 *
497 * This function returns a pointer to the superblock node or a negative error
Artem Bityutskiyeaeee242011-05-06 17:08:56 +0300498 * code. Note, the user of this function is responsible of kfree()'ing the
499 * returned superblock buffer.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300500 */
Sascha Hauerfd615002018-09-07 14:36:29 +0200501static struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300502{
503 struct ubifs_sb_node *sup;
504 int err;
505
506 sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
507 if (!sup)
508 return ERR_PTR(-ENOMEM);
509
510 err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
511 UBIFS_SB_LNUM, 0);
512 if (err) {
513 kfree(sup);
514 return ERR_PTR(err);
515 }
516
517 return sup;
518}
519
Sascha Hauere158e022018-09-07 14:36:42 +0200520static int authenticate_sb_node(struct ubifs_info *c,
521 const struct ubifs_sb_node *sup)
522{
523 unsigned int sup_flags = le32_to_cpu(sup->flags);
524 u8 hmac_wkm[UBIFS_HMAC_ARR_SZ];
525 int authenticated = !!(sup_flags & UBIFS_FLG_AUTHENTICATION);
526 int hash_algo;
527 int err;
528
529 if (c->authenticated && !authenticated) {
530 ubifs_err(c, "authenticated FS forced, but found FS without authentication");
531 return -EINVAL;
532 }
533
534 if (!c->authenticated && authenticated) {
535 ubifs_err(c, "authenticated FS found, but no key given");
536 return -EINVAL;
537 }
538
539 ubifs_msg(c, "Mounting in %sauthenticated mode",
540 c->authenticated ? "" : "un");
541
542 if (!c->authenticated)
543 return 0;
544
545 if (!IS_ENABLED(CONFIG_UBIFS_FS_AUTHENTICATION))
546 return -EOPNOTSUPP;
547
548 hash_algo = le16_to_cpu(sup->hash_algo);
549 if (hash_algo >= HASH_ALGO__LAST) {
550 ubifs_err(c, "superblock uses unknown hash algo %d",
551 hash_algo);
552 return -EINVAL;
553 }
554
555 if (strcmp(hash_algo_name[hash_algo], c->auth_hash_name)) {
556 ubifs_err(c, "This filesystem uses %s for hashing,"
557 " but %s is specified", hash_algo_name[hash_algo],
558 c->auth_hash_name);
559 return -EINVAL;
560 }
561
562 err = ubifs_hmac_wkm(c, hmac_wkm);
563 if (err)
564 return err;
565
566 if (ubifs_check_hmac(c, hmac_wkm, sup->hmac_wkm)) {
567 ubifs_err(c, "provided key does not fit");
568 return -ENOKEY;
569 }
570
571 err = ubifs_node_verify_hmac(c, sup, sizeof(*sup),
572 offsetof(struct ubifs_sb_node, hmac));
573 if (err)
574 ubifs_err(c, "Failed to authenticate superblock: %d", err);
575
576 return err;
577}
578
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300579/**
580 * ubifs_write_sb_node - write superblock node.
581 * @c: UBIFS file-system description object
582 * @sup: superblock node read with 'ubifs_read_sb_node()'
583 *
584 * This function returns %0 on success and a negative error code on failure.
585 */
586int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
587{
588 int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
Sascha Hauere158e022018-09-07 14:36:42 +0200589 int err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300590
Sascha Hauere158e022018-09-07 14:36:42 +0200591 err = ubifs_prepare_node_hmac(c, sup, UBIFS_SB_NODE_SZ,
592 offsetof(struct ubifs_sb_node, hmac), 1);
593 if (err)
594 return err;
595
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200596 return ubifs_leb_change(c, UBIFS_SB_LNUM, sup, len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300597}
598
599/**
600 * ubifs_read_superblock - read superblock.
601 * @c: UBIFS file-system description object
602 *
603 * This function finds, reads and checks the superblock. If an empty UBI volume
604 * is being mounted, this function creates default superblock. Returns zero in
605 * case of success, and a negative error code in case of failure.
606 */
607int ubifs_read_superblock(struct ubifs_info *c)
608{
609 int err, sup_flags;
610 struct ubifs_sb_node *sup;
611
612 if (c->empty) {
613 err = create_default_filesystem(c);
614 if (err)
615 return err;
616 }
617
618 sup = ubifs_read_sb_node(c);
619 if (IS_ERR(sup))
620 return PTR_ERR(sup);
621
Sascha Hauerfd615002018-09-07 14:36:29 +0200622 c->sup_node = sup;
623
Artem Bityutskiy963f0cf2009-03-26 12:51:21 +0200624 c->fmt_version = le32_to_cpu(sup->fmt_version);
625 c->ro_compat_version = le32_to_cpu(sup->ro_compat_version);
626
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300627 /*
628 * The software supports all previous versions but not future versions,
629 * due to the unavailability of time-travelling equipment.
630 */
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300631 if (c->fmt_version > UBIFS_FORMAT_VERSION) {
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200632 ubifs_assert(c, !c->ro_media || c->ro_mount);
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300633 if (!c->ro_mount ||
Artem Bityutskiy963f0cf2009-03-26 12:51:21 +0200634 c->ro_compat_version > UBIFS_RO_COMPAT_VERSION) {
Sheng Yong235c3622015-03-20 10:39:42 +0000635 ubifs_err(c, "on-flash format version is w%d/r%d, but software only supports up to version w%d/r%d",
Artem Bityutskiy79fda512012-08-27 13:34:09 +0300636 c->fmt_version, c->ro_compat_version,
637 UBIFS_FORMAT_VERSION,
Artem Bityutskiy963f0cf2009-03-26 12:51:21 +0200638 UBIFS_RO_COMPAT_VERSION);
639 if (c->ro_compat_version <= UBIFS_RO_COMPAT_VERSION) {
Sheng Yong235c3622015-03-20 10:39:42 +0000640 ubifs_msg(c, "only R/O mounting is possible");
Artem Bityutskiy963f0cf2009-03-26 12:51:21 +0200641 err = -EROFS;
642 } else
643 err = -EINVAL;
644 goto out;
645 }
646
647 /*
648 * The FS is mounted R/O, and the media format is
649 * R/O-compatible with the UBIFS implementation, so we can
650 * mount.
651 */
652 c->rw_incompat = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300653 }
654
655 if (c->fmt_version < 3) {
Sheng Yong235c3622015-03-20 10:39:42 +0000656 ubifs_err(c, "on-flash format version %d is not supported",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300657 c->fmt_version);
658 err = -EINVAL;
659 goto out;
660 }
661
662 switch (sup->key_hash) {
663 case UBIFS_KEY_HASH_R5:
664 c->key_hash = key_r5_hash;
665 c->key_hash_type = UBIFS_KEY_HASH_R5;
666 break;
667
668 case UBIFS_KEY_HASH_TEST:
669 c->key_hash = key_test_hash;
670 c->key_hash_type = UBIFS_KEY_HASH_TEST;
671 break;
672 };
673
674 c->key_fmt = sup->key_fmt;
675
676 switch (c->key_fmt) {
677 case UBIFS_SIMPLE_KEY_FMT:
678 c->key_len = UBIFS_SK_LEN;
679 break;
680 default:
Sheng Yong235c3622015-03-20 10:39:42 +0000681 ubifs_err(c, "unsupported key format");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300682 err = -EINVAL;
683 goto out;
684 }
685
686 c->leb_cnt = le32_to_cpu(sup->leb_cnt);
687 c->max_leb_cnt = le32_to_cpu(sup->max_leb_cnt);
688 c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
689 c->log_lebs = le32_to_cpu(sup->log_lebs);
690 c->lpt_lebs = le32_to_cpu(sup->lpt_lebs);
691 c->orph_lebs = le32_to_cpu(sup->orph_lebs);
692 c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
693 c->fanout = le32_to_cpu(sup->fanout);
694 c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300695 c->rp_size = le64_to_cpu(sup->rp_size);
Eric W. Biederman39241be2012-02-07 15:50:56 -0800696 c->rp_uid = make_kuid(&init_user_ns, le32_to_cpu(sup->rp_uid));
697 c->rp_gid = make_kgid(&init_user_ns, le32_to_cpu(sup->rp_gid));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300698 sup_flags = le32_to_cpu(sup->flags);
Artem Bityutskiy553dea42008-11-01 14:57:49 +0200699 if (!c->mount_opts.override_compr)
700 c->default_compr = le16_to_cpu(sup->default_compr);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300701
702 c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300703 memcpy(&c->uuid, &sup->uuid, 16);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300704 c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
Matthew L. Creech9f58d352011-05-05 16:33:20 -0400705 c->space_fixup = !!(sup_flags & UBIFS_FLG_SPACE_FIXUP);
Richard Weinbergerd63d61c2016-10-19 15:59:12 +0200706 c->double_hash = !!(sup_flags & UBIFS_FLG_DOUBLE_HASH);
Richard Weinbergere0219862016-10-19 23:24:47 +0200707 c->encrypted = !!(sup_flags & UBIFS_FLG_ENCRYPTION);
708
Sascha Hauere158e022018-09-07 14:36:42 +0200709 err = authenticate_sb_node(c, sup);
710 if (err)
711 goto out;
712
Richard Weinbergerfc4b8912016-10-19 23:46:39 +0200713 if ((sup_flags & ~UBIFS_FLG_MASK) != 0) {
714 ubifs_err(c, "Unknown feature flags found: %#x",
715 sup_flags & ~UBIFS_FLG_MASK);
716 err = -EINVAL;
717 goto out;
718 }
719
Richard Weinbergere0219862016-10-19 23:24:47 +0200720#ifndef CONFIG_UBIFS_FS_ENCRYPTION
721 if (c->encrypted) {
722 ubifs_err(c, "file system contains encrypted files but UBIFS"
723 " was built without crypto support.");
724 err = -EINVAL;
725 goto out;
726 }
727#endif
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300728
729 /* Automatically increase file system size to the maximum size */
730 c->old_leb_cnt = c->leb_cnt;
731 if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
732 c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300733 if (c->ro_mount)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300734 dbg_mnt("Auto resizing (ro) from %d LEBs to %d LEBs",
735 c->old_leb_cnt, c->leb_cnt);
736 else {
737 dbg_mnt("Auto resizing (sb) from %d LEBs to %d LEBs",
738 c->old_leb_cnt, c->leb_cnt);
739 sup->leb_cnt = cpu_to_le32(c->leb_cnt);
740 err = ubifs_write_sb_node(c, sup);
741 if (err)
742 goto out;
743 c->old_leb_cnt = c->leb_cnt;
744 }
745 }
746
747 c->log_bytes = (long long)c->log_lebs * c->leb_size;
748 c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
749 c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
750 c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
751 c->orph_first = c->lpt_last + 1;
752 c->orph_last = c->orph_first + c->orph_lebs - 1;
753 c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
754 c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
755 c->main_first = c->leb_cnt - c->main_lebs;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300756
757 err = validate_sb(c, sup);
758out:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300759 return err;
760}
Matthew L. Creech6554a652011-05-06 18:58:22 -0400761
762/**
763 * fixup_leb - fixup/unmap an LEB containing free space.
764 * @c: UBIFS file-system description object
765 * @lnum: the LEB number to fix up
766 * @len: number of used bytes in LEB (starting at offset 0)
767 *
768 * This function reads the contents of the given LEB number @lnum, then fixes
769 * it up, so that empty min. I/O units in the end of LEB are actually erased on
770 * flash (rather than being just all-0xff real data). If the LEB is completely
771 * empty, it is simply unmapped.
772 */
773static int fixup_leb(struct ubifs_info *c, int lnum, int len)
774{
775 int err;
776
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200777 ubifs_assert(c, len >= 0);
778 ubifs_assert(c, len % c->min_io_size == 0);
779 ubifs_assert(c, len < c->leb_size);
Matthew L. Creech6554a652011-05-06 18:58:22 -0400780
781 if (len == 0) {
782 dbg_mnt("unmap empty LEB %d", lnum);
Artem Bityutskiyd3b25782011-06-03 14:22:05 +0300783 return ubifs_leb_unmap(c, lnum);
Matthew L. Creech6554a652011-05-06 18:58:22 -0400784 }
785
786 dbg_mnt("fixup LEB %d, data len %d", lnum, len);
Artem Bityutskiyd3048202011-06-03 14:03:25 +0300787 err = ubifs_leb_read(c, lnum, c->sbuf, 0, len, 1);
Matthew L. Creech6554a652011-05-06 18:58:22 -0400788 if (err)
789 return err;
790
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200791 return ubifs_leb_change(c, lnum, c->sbuf, len);
Matthew L. Creech6554a652011-05-06 18:58:22 -0400792}
793
794/**
795 * fixup_free_space - find & remap all LEBs containing free space.
796 * @c: UBIFS file-system description object
797 *
798 * This function walks through all LEBs in the filesystem and fiexes up those
799 * containing free/empty space.
800 */
801static int fixup_free_space(struct ubifs_info *c)
802{
803 int lnum, err = 0;
804 struct ubifs_lprops *lprops;
805
806 ubifs_get_lprops(c);
807
808 /* Fixup LEBs in the master area */
809 for (lnum = UBIFS_MST_LNUM; lnum < UBIFS_LOG_LNUM; lnum++) {
810 err = fixup_leb(c, lnum, c->mst_offs + c->mst_node_alsz);
811 if (err)
812 goto out;
813 }
814
815 /* Unmap unused log LEBs */
816 lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
817 while (lnum != c->ltail_lnum) {
818 err = fixup_leb(c, lnum, 0);
819 if (err)
820 goto out;
821 lnum = ubifs_next_log_lnum(c, lnum);
822 }
823
Artem Bityutskiyc6727932012-07-14 14:33:09 +0300824 /*
825 * Fixup the log head which contains the only a CS node at the
826 * beginning.
827 */
828 err = fixup_leb(c, c->lhead_lnum,
829 ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size));
Matthew L. Creech6554a652011-05-06 18:58:22 -0400830 if (err)
831 goto out;
832
833 /* Fixup LEBs in the LPT area */
834 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
835 int free = c->ltab[lnum - c->lpt_first].free;
836
837 if (free > 0) {
838 err = fixup_leb(c, lnum, c->leb_size - free);
839 if (err)
840 goto out;
841 }
842 }
843
844 /* Unmap LEBs in the orphans area */
845 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
846 err = fixup_leb(c, lnum, 0);
847 if (err)
848 goto out;
849 }
850
851 /* Fixup LEBs in the main area */
852 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
853 lprops = ubifs_lpt_lookup(c, lnum);
854 if (IS_ERR(lprops)) {
855 err = PTR_ERR(lprops);
856 goto out;
857 }
858
859 if (lprops->free > 0) {
860 err = fixup_leb(c, lnum, c->leb_size - lprops->free);
861 if (err)
862 goto out;
863 }
864 }
865
866out:
867 ubifs_release_lprops(c);
868 return err;
869}
870
871/**
872 * ubifs_fixup_free_space - find & fix all LEBs with free space.
873 * @c: UBIFS file-system description object
874 *
875 * This function fixes up LEBs containing free space on first mount, if the
876 * appropriate flag was set when the FS was created. Each LEB with one or more
877 * empty min. I/O unit (i.e. free-space-count > 0) is re-written, to make sure
878 * the free space is actually erased. E.g., this is necessary for some NAND
879 * chips, since the free space may have been programmed like real "0xff" data
880 * (generating a non-0xff ECC), causing future writes to the not-really-erased
881 * NAND pages to behave badly. After the space is fixed up, the superblock flag
882 * is cleared, so that this is skipped for all future mounts.
883 */
884int ubifs_fixup_free_space(struct ubifs_info *c)
885{
886 int err;
Sascha Hauerfd615002018-09-07 14:36:29 +0200887 struct ubifs_sb_node *sup = c->sup_node;
Matthew L. Creech6554a652011-05-06 18:58:22 -0400888
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200889 ubifs_assert(c, c->space_fixup);
890 ubifs_assert(c, !c->ro_mount);
Matthew L. Creech6554a652011-05-06 18:58:22 -0400891
Sheng Yong235c3622015-03-20 10:39:42 +0000892 ubifs_msg(c, "start fixing up free space");
Matthew L. Creech6554a652011-05-06 18:58:22 -0400893
894 err = fixup_free_space(c);
895 if (err)
896 return err;
897
Matthew L. Creech6554a652011-05-06 18:58:22 -0400898 /* Free-space fixup is no longer required */
899 c->space_fixup = 0;
900 sup->flags &= cpu_to_le32(~UBIFS_FLG_SPACE_FIXUP);
901
902 err = ubifs_write_sb_node(c, sup);
Matthew L. Creech6554a652011-05-06 18:58:22 -0400903 if (err)
904 return err;
905
Sheng Yong235c3622015-03-20 10:39:42 +0000906 ubifs_msg(c, "free space fixup complete");
Matthew L. Creech6554a652011-05-06 18:58:22 -0400907 return err;
908}
Richard Weinbergere0219862016-10-19 23:24:47 +0200909
910int ubifs_enable_encryption(struct ubifs_info *c)
911{
912 int err;
Sascha Hauerfd615002018-09-07 14:36:29 +0200913 struct ubifs_sb_node *sup = c->sup_node;
Richard Weinbergere0219862016-10-19 23:24:47 +0200914
915 if (c->encrypted)
916 return 0;
917
918 if (c->ro_mount || c->ro_media)
919 return -EROFS;
920
921 if (c->fmt_version < 5) {
922 ubifs_err(c, "on-flash format version 5 is needed for encryption");
923 return -EINVAL;
924 }
925
Richard Weinbergere0219862016-10-19 23:24:47 +0200926 sup->flags |= cpu_to_le32(UBIFS_FLG_ENCRYPTION);
927
928 err = ubifs_write_sb_node(c, sup);
929 if (!err)
930 c->encrypted = 1;
Richard Weinbergere0219862016-10-19 23:24:47 +0200931
932 return err;
933}