blob: 7e08c81433165b49192bc4eb60cdda0551de409c [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;
Artem Bityutskiy1e517642008-07-14 19:08:37 +030090
91 /* Some functions called from here depend on the @c->key_len filed */
92 c->key_len = UBIFS_SK_LEN;
93
94 /*
95 * First of all, we have to calculate default file-system geometry -
96 * log size, journal size, etc.
97 */
98 if (c->leb_cnt < 0x7FFFFFFF / DEFAULT_JNL_PERCENT)
99 /* We can first multiply then divide and have no overflow */
100 jnl_lebs = c->leb_cnt * DEFAULT_JNL_PERCENT / 100;
101 else
102 jnl_lebs = (c->leb_cnt / 100) * DEFAULT_JNL_PERCENT;
103
104 if (jnl_lebs < UBIFS_MIN_JNL_LEBS)
105 jnl_lebs = UBIFS_MIN_JNL_LEBS;
106 if (jnl_lebs * c->leb_size > DEFAULT_MAX_JNL)
107 jnl_lebs = DEFAULT_MAX_JNL / c->leb_size;
108
109 /*
110 * The log should be large enough to fit reference nodes for all bud
111 * LEBs. Because buds do not have to start from the beginning of LEBs
112 * (half of the LEB may contain committed data), the log should
113 * generally be larger, make it twice as large.
114 */
115 tmp = 2 * (c->ref_node_alsz * jnl_lebs) + c->leb_size - 1;
116 log_lebs = tmp / c->leb_size;
117 /* Plus one LEB reserved for commit */
118 log_lebs += 1;
119 if (c->leb_cnt - min_leb_cnt > 8) {
120 /* And some extra space to allow writes while committing */
121 log_lebs += 1;
122 min_leb_cnt += 1;
123 }
124
125 max_buds = jnl_lebs - log_lebs;
126 if (max_buds < UBIFS_MIN_BUD_LEBS)
127 max_buds = UBIFS_MIN_BUD_LEBS;
128
129 /*
130 * Orphan nodes are stored in a separate area. One node can store a lot
131 * of orphan inode numbers, but when new orphan comes we just add a new
132 * orphan node. At some point the nodes are consolidated into one
133 * orphan node.
134 */
135 orph_lebs = UBIFS_MIN_ORPH_LEBS;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300136 if (c->leb_cnt - min_leb_cnt > 1)
137 /*
138 * For debugging purposes it is better to have at least 2
139 * orphan LEBs, because the orphan subsystem would need to do
140 * consolidations and would be stressed more.
141 */
142 orph_lebs += 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300143
144 main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS - log_lebs;
145 main_lebs -= orph_lebs;
146
147 lpt_first = UBIFS_LOG_LNUM + log_lebs;
148 c->lsave_cnt = DEFAULT_LSAVE_CNT;
149 c->max_leb_cnt = c->leb_cnt;
150 err = ubifs_create_dflt_lpt(c, &main_lebs, lpt_first, &lpt_lebs,
151 &big_lpt);
152 if (err)
153 return err;
154
155 dbg_gen("LEB Properties Tree created (LEBs %d-%d)", lpt_first,
156 lpt_first + lpt_lebs - 1);
157
158 main_first = c->leb_cnt - main_lebs;
159
Sascha Hauerc4de6d72018-09-07 14:36:23 +0200160 sup = kzalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_KERNEL);
161 mst = kzalloc(c->mst_node_alsz, GFP_KERNEL);
162 idx_node_size = ubifs_idx_node_sz(c, 1);
163 idx = kzalloc(ALIGN(tmp, c->min_io_size), GFP_KERNEL);
164 ino = kzalloc(ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size), GFP_KERNEL);
165 cs = kzalloc(ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size), GFP_KERNEL);
166
167 if (!sup || !mst || !idx || !ino || !cs) {
168 err = -ENOMEM;
169 goto out;
170 }
171
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300172 /* Create default superblock */
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300173
Artem Bityutskiy4d61db42008-12-18 14:06:51 +0200174 tmp64 = (long long)max_buds * c->leb_size;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300175 if (big_lpt)
176 sup_flags |= UBIFS_FLG_BIGLPT;
Richard Weinbergerd63d61c2016-10-19 15:59:12 +0200177 sup_flags |= UBIFS_FLG_DOUBLE_HASH;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300178
179 sup->ch.node_type = UBIFS_SB_NODE;
180 sup->key_hash = UBIFS_KEY_HASH_R5;
181 sup->flags = cpu_to_le32(sup_flags);
182 sup->min_io_size = cpu_to_le32(c->min_io_size);
183 sup->leb_size = cpu_to_le32(c->leb_size);
184 sup->leb_cnt = cpu_to_le32(c->leb_cnt);
185 sup->max_leb_cnt = cpu_to_le32(c->max_leb_cnt);
186 sup->max_bud_bytes = cpu_to_le64(tmp64);
187 sup->log_lebs = cpu_to_le32(log_lebs);
188 sup->lpt_lebs = cpu_to_le32(lpt_lebs);
189 sup->orph_lebs = cpu_to_le32(orph_lebs);
190 sup->jhead_cnt = cpu_to_le32(DEFAULT_JHEADS_CNT);
191 sup->fanout = cpu_to_le32(DEFAULT_FANOUT);
192 sup->lsave_cnt = cpu_to_le32(c->lsave_cnt);
193 sup->fmt_version = cpu_to_le32(UBIFS_FORMAT_VERSION);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300194 sup->time_gran = cpu_to_le32(DEFAULT_TIME_GRAN);
Artem Bityutskiy553dea42008-11-01 14:57:49 +0200195 if (c->mount_opts.override_compr)
196 sup->default_compr = cpu_to_le16(c->mount_opts.compr_type);
197 else
198 sup->default_compr = cpu_to_le16(UBIFS_COMPR_LZO);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300199
200 generate_random_uuid(sup->uuid);
201
Artem Bityutskiy4d61db42008-12-18 14:06:51 +0200202 main_bytes = (long long)main_lebs * c->leb_size;
203 tmp64 = div_u64(main_bytes * DEFAULT_RP_PERCENT, 100);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300204 if (tmp64 > DEFAULT_MAX_RP_SIZE)
205 tmp64 = DEFAULT_MAX_RP_SIZE;
206 sup->rp_size = cpu_to_le64(tmp64);
Artem Bityutskiy963f0cf2009-03-26 12:51:21 +0200207 sup->ro_compat_version = cpu_to_le32(UBIFS_RO_COMPAT_VERSION);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300208
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300209 dbg_gen("default superblock created at LEB 0:0");
210
211 /* Create default master node */
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300212
213 mst->ch.node_type = UBIFS_MST_NODE;
214 mst->log_lnum = cpu_to_le32(UBIFS_LOG_LNUM);
215 mst->highest_inum = cpu_to_le64(UBIFS_FIRST_INO);
216 mst->cmt_no = 0;
217 mst->root_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
218 mst->root_offs = 0;
219 tmp = ubifs_idx_node_sz(c, 1);
220 mst->root_len = cpu_to_le32(tmp);
221 mst->gc_lnum = cpu_to_le32(main_first + DEFAULT_GC_LEB);
222 mst->ihead_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
223 mst->ihead_offs = cpu_to_le32(ALIGN(tmp, c->min_io_size));
224 mst->index_size = cpu_to_le64(ALIGN(tmp, 8));
225 mst->lpt_lnum = cpu_to_le32(c->lpt_lnum);
226 mst->lpt_offs = cpu_to_le32(c->lpt_offs);
227 mst->nhead_lnum = cpu_to_le32(c->nhead_lnum);
228 mst->nhead_offs = cpu_to_le32(c->nhead_offs);
229 mst->ltab_lnum = cpu_to_le32(c->ltab_lnum);
230 mst->ltab_offs = cpu_to_le32(c->ltab_offs);
231 mst->lsave_lnum = cpu_to_le32(c->lsave_lnum);
232 mst->lsave_offs = cpu_to_le32(c->lsave_offs);
233 mst->lscan_lnum = cpu_to_le32(main_first);
234 mst->empty_lebs = cpu_to_le32(main_lebs - 2);
235 mst->idx_lebs = cpu_to_le32(1);
236 mst->leb_cnt = cpu_to_le32(c->leb_cnt);
237
238 /* Calculate lprops statistics */
239 tmp64 = main_bytes;
240 tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
241 tmp64 -= ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
242 mst->total_free = cpu_to_le64(tmp64);
243
244 tmp64 = ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
245 ino_waste = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size) -
246 UBIFS_INO_NODE_SZ;
247 tmp64 += ino_waste;
248 tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), 8);
249 mst->total_dirty = cpu_to_le64(tmp64);
250
251 /* The indexing LEB does not contribute to dark space */
srimugunthan dhandapani7606f852011-08-26 16:08:39 +0530252 tmp64 = ((long long)(c->main_lebs - 1) * c->dark_wm);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300253 mst->total_dark = cpu_to_le64(tmp64);
254
255 mst->total_used = cpu_to_le64(UBIFS_INO_NODE_SZ);
256
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300257 dbg_gen("default master node created at LEB %d:0", UBIFS_MST_LNUM);
258
259 /* Create the root indexing node */
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300260
261 c->key_fmt = UBIFS_SIMPLE_KEY_FMT;
262 c->key_hash = key_r5_hash;
263
264 idx->ch.node_type = UBIFS_IDX_NODE;
265 idx->child_cnt = cpu_to_le16(1);
266 ino_key_init(c, &key, UBIFS_ROOT_INO);
267 br = ubifs_idx_branch(c, idx, 0);
268 key_write_idx(c, &key, &br->key);
269 br->lnum = cpu_to_le32(main_first + DEFAULT_DATA_LEB);
270 br->len = cpu_to_le32(UBIFS_INO_NODE_SZ);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300271
272 dbg_gen("default root indexing node created LEB %d:0",
273 main_first + DEFAULT_IDX_LEB);
274
275 /* Create default root inode */
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300276
277 ino_key_init_flash(c, &ino->key, UBIFS_ROOT_INO);
278 ino->ch.node_type = UBIFS_INO_NODE;
279 ino->creat_sqnum = cpu_to_le64(++c->max_sqnum);
280 ino->nlink = cpu_to_le32(2);
Deepa Dinamani607a11a2017-05-08 15:59:25 -0700281
Arnd Bergmann0eca0b82018-07-13 16:31:55 +0200282 ktime_get_real_ts64(&ts);
283 ts = timespec64_trunc(ts, DEFAULT_TIME_GRAN);
Deepa Dinamani607a11a2017-05-08 15:59:25 -0700284 tmp_le64 = cpu_to_le64(ts.tv_sec);
Harvey Harrison0ecb9522008-10-24 10:52:57 -0700285 ino->atime_sec = tmp_le64;
286 ino->ctime_sec = tmp_le64;
287 ino->mtime_sec = tmp_le64;
Deepa Dinamani607a11a2017-05-08 15:59:25 -0700288 tmp_le32 = cpu_to_le32(ts.tv_nsec);
289 ino->atime_nsec = tmp_le32;
290 ino->ctime_nsec = tmp_le32;
291 ino->mtime_nsec = tmp_le32;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300292 ino->mode = cpu_to_le32(S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
293 ino->size = cpu_to_le64(UBIFS_INO_NODE_SZ);
294
295 /* Set compression enabled by default */
296 ino->flags = cpu_to_le32(UBIFS_COMPR_FL);
297
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300298 dbg_gen("root inode created at LEB %d:0",
299 main_first + DEFAULT_DATA_LEB);
300
301 /*
302 * The first node in the log has to be the commit start node. This is
303 * always the case during normal file-system operation. Write a fake
304 * commit start node to the log.
305 */
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300306
307 cs->ch.node_type = UBIFS_CS_NODE;
Sascha Hauerc4de6d72018-09-07 14:36:23 +0200308
309 err = ubifs_write_node(c, sup, UBIFS_SB_NODE_SZ, 0, 0);
hujianyang6dcfb802014-06-11 10:38:45 +0800310 if (err)
Sascha Hauerc4de6d72018-09-07 14:36:23 +0200311 goto out;
312
313 err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0);
314 if (err)
315 goto out;
316
317 err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1,
318 0);
319 if (err)
320 goto out;
321
322 err = ubifs_write_node(c, idx, idx_node_size, main_first + DEFAULT_IDX_LEB, 0);
323 if (err)
324 goto out;
325
326 err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
327 main_first + DEFAULT_DATA_LEB, 0);
328 if (err)
329 goto out;
330
331 err = ubifs_write_node(c, cs, UBIFS_CS_NODE_SZ, UBIFS_LOG_LNUM, 0);
332 if (err)
333 goto out;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300334
Sheng Yong235c3622015-03-20 10:39:42 +0000335 ubifs_msg(c, "default file-system created");
Sascha Hauerc4de6d72018-09-07 14:36:23 +0200336
337 err = 0;
338out:
339 kfree(sup);
340 kfree(mst);
341 kfree(idx);
342 kfree(ino);
343 kfree(cs);
344
345 return err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300346}
347
348/**
349 * validate_sb - validate superblock node.
350 * @c: UBIFS file-system description object
351 * @sup: superblock node
352 *
353 * This function validates superblock node @sup. Since most of data was read
354 * from the superblock and stored in @c, the function validates fields in @c
355 * instead. Returns zero in case of success and %-EINVAL in case of validation
356 * failure.
357 */
358static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
359{
360 long long max_bytes;
361 int err = 1, min_leb_cnt;
362
363 if (!c->key_hash) {
364 err = 2;
365 goto failed;
366 }
367
368 if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
369 err = 3;
370 goto failed;
371 }
372
373 if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
Sheng Yong235c3622015-03-20 10:39:42 +0000374 ubifs_err(c, "min. I/O unit mismatch: %d in superblock, %d real",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300375 le32_to_cpu(sup->min_io_size), c->min_io_size);
376 goto failed;
377 }
378
379 if (le32_to_cpu(sup->leb_size) != c->leb_size) {
Sheng Yong235c3622015-03-20 10:39:42 +0000380 ubifs_err(c, "LEB size mismatch: %d in superblock, %d real",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300381 le32_to_cpu(sup->leb_size), c->leb_size);
382 goto failed;
383 }
384
385 if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
386 c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
387 c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
388 c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
389 err = 4;
390 goto failed;
391 }
392
393 /*
394 * Calculate minimum allowed amount of main area LEBs. This is very
395 * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
396 * have just read from the superblock.
397 */
398 min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
399 min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
400
401 if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
Sheng Yong235c3622015-03-20 10:39:42 +0000402 ubifs_err(c, "bad LEB count: %d in superblock, %d on UBI volume, %d minimum required",
Artem Bityutskiy79fda512012-08-27 13:34:09 +0300403 c->leb_cnt, c->vi.size, min_leb_cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300404 goto failed;
405 }
406
407 if (c->max_leb_cnt < c->leb_cnt) {
Sheng Yong235c3622015-03-20 10:39:42 +0000408 ubifs_err(c, "max. LEB count %d less than LEB count %d",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300409 c->max_leb_cnt, c->leb_cnt);
410 goto failed;
411 }
412
413 if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
Sheng Yong235c3622015-03-20 10:39:42 +0000414 ubifs_err(c, "too few main LEBs count %d, must be at least %d",
Artem Bityutskiy5a1f36c2012-03-07 16:29:45 +0200415 c->main_lebs, UBIFS_MIN_MAIN_LEBS);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300416 goto failed;
417 }
418
Artem Bityutskiy5a1f36c2012-03-07 16:29:45 +0200419 max_bytes = (long long)c->leb_size * UBIFS_MIN_BUD_LEBS;
420 if (c->max_bud_bytes < max_bytes) {
Sheng Yong235c3622015-03-20 10:39:42 +0000421 ubifs_err(c, "too small journal (%lld bytes), must be at least %lld bytes",
Artem Bityutskiy79fda512012-08-27 13:34:09 +0300422 c->max_bud_bytes, max_bytes);
Artem Bityutskiy5a1f36c2012-03-07 16:29:45 +0200423 goto failed;
424 }
425
426 max_bytes = (long long)c->leb_size * c->main_lebs;
427 if (c->max_bud_bytes > max_bytes) {
Sheng Yong235c3622015-03-20 10:39:42 +0000428 ubifs_err(c, "too large journal size (%lld bytes), only %lld bytes available in the main area",
Artem Bityutskiy5a1f36c2012-03-07 16:29:45 +0200429 c->max_bud_bytes, max_bytes);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300430 goto failed;
431 }
432
433 if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
434 c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
435 err = 9;
436 goto failed;
437 }
438
439 if (c->fanout < UBIFS_MIN_FANOUT ||
440 ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
441 err = 10;
442 goto failed;
443 }
444
445 if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
446 c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
447 c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
448 err = 11;
449 goto failed;
450 }
451
452 if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
453 c->orph_lebs + c->main_lebs != c->leb_cnt) {
454 err = 12;
455 goto failed;
456 }
457
hujianyangb793a8c2014-06-11 10:42:52 +0800458 if (c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300459 err = 13;
460 goto failed;
461 }
462
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300463 if (c->rp_size < 0 || max_bytes < c->rp_size) {
464 err = 14;
465 goto failed;
466 }
467
468 if (le32_to_cpu(sup->time_gran) > 1000000000 ||
469 le32_to_cpu(sup->time_gran) < 1) {
470 err = 15;
471 goto failed;
472 }
473
Richard Weinbergerfc4b8912016-10-19 23:46:39 +0200474 if (!c->double_hash && c->fmt_version >= 5) {
475 err = 16;
476 goto failed;
477 }
478
479 if (c->encrypted && c->fmt_version < 5) {
480 err = 17;
481 goto failed;
482 }
483
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300484 return 0;
485
486failed:
Sheng Yong235c3622015-03-20 10:39:42 +0000487 ubifs_err(c, "bad superblock, error %d", err);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300488 ubifs_dump_node(c, sup);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300489 return -EINVAL;
490}
491
492/**
493 * ubifs_read_sb_node - read superblock node.
494 * @c: UBIFS file-system description object
495 *
496 * This function returns a pointer to the superblock node or a negative error
Artem Bityutskiyeaeee242011-05-06 17:08:56 +0300497 * code. Note, the user of this function is responsible of kfree()'ing the
498 * returned superblock buffer.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300499 */
Sascha Hauerfd615002018-09-07 14:36:29 +0200500static struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300501{
502 struct ubifs_sb_node *sup;
503 int err;
504
505 sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
506 if (!sup)
507 return ERR_PTR(-ENOMEM);
508
509 err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
510 UBIFS_SB_LNUM, 0);
511 if (err) {
512 kfree(sup);
513 return ERR_PTR(err);
514 }
515
516 return sup;
517}
518
519/**
520 * ubifs_write_sb_node - write superblock node.
521 * @c: UBIFS file-system description object
522 * @sup: superblock node read with 'ubifs_read_sb_node()'
523 *
524 * This function returns %0 on success and a negative error code on failure.
525 */
526int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
527{
528 int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
529
530 ubifs_prepare_node(c, sup, UBIFS_SB_NODE_SZ, 1);
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200531 return ubifs_leb_change(c, UBIFS_SB_LNUM, sup, len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300532}
533
534/**
535 * ubifs_read_superblock - read superblock.
536 * @c: UBIFS file-system description object
537 *
538 * This function finds, reads and checks the superblock. If an empty UBI volume
539 * is being mounted, this function creates default superblock. Returns zero in
540 * case of success, and a negative error code in case of failure.
541 */
542int ubifs_read_superblock(struct ubifs_info *c)
543{
544 int err, sup_flags;
545 struct ubifs_sb_node *sup;
546
547 if (c->empty) {
548 err = create_default_filesystem(c);
549 if (err)
550 return err;
551 }
552
553 sup = ubifs_read_sb_node(c);
554 if (IS_ERR(sup))
555 return PTR_ERR(sup);
556
Sascha Hauerfd615002018-09-07 14:36:29 +0200557 c->sup_node = sup;
558
Artem Bityutskiy963f0cf2009-03-26 12:51:21 +0200559 c->fmt_version = le32_to_cpu(sup->fmt_version);
560 c->ro_compat_version = le32_to_cpu(sup->ro_compat_version);
561
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300562 /*
563 * The software supports all previous versions but not future versions,
564 * due to the unavailability of time-travelling equipment.
565 */
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300566 if (c->fmt_version > UBIFS_FORMAT_VERSION) {
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200567 ubifs_assert(c, !c->ro_media || c->ro_mount);
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300568 if (!c->ro_mount ||
Artem Bityutskiy963f0cf2009-03-26 12:51:21 +0200569 c->ro_compat_version > UBIFS_RO_COMPAT_VERSION) {
Sheng Yong235c3622015-03-20 10:39:42 +0000570 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 +0300571 c->fmt_version, c->ro_compat_version,
572 UBIFS_FORMAT_VERSION,
Artem Bityutskiy963f0cf2009-03-26 12:51:21 +0200573 UBIFS_RO_COMPAT_VERSION);
574 if (c->ro_compat_version <= UBIFS_RO_COMPAT_VERSION) {
Sheng Yong235c3622015-03-20 10:39:42 +0000575 ubifs_msg(c, "only R/O mounting is possible");
Artem Bityutskiy963f0cf2009-03-26 12:51:21 +0200576 err = -EROFS;
577 } else
578 err = -EINVAL;
579 goto out;
580 }
581
582 /*
583 * The FS is mounted R/O, and the media format is
584 * R/O-compatible with the UBIFS implementation, so we can
585 * mount.
586 */
587 c->rw_incompat = 1;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300588 }
589
590 if (c->fmt_version < 3) {
Sheng Yong235c3622015-03-20 10:39:42 +0000591 ubifs_err(c, "on-flash format version %d is not supported",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300592 c->fmt_version);
593 err = -EINVAL;
594 goto out;
595 }
596
597 switch (sup->key_hash) {
598 case UBIFS_KEY_HASH_R5:
599 c->key_hash = key_r5_hash;
600 c->key_hash_type = UBIFS_KEY_HASH_R5;
601 break;
602
603 case UBIFS_KEY_HASH_TEST:
604 c->key_hash = key_test_hash;
605 c->key_hash_type = UBIFS_KEY_HASH_TEST;
606 break;
607 };
608
609 c->key_fmt = sup->key_fmt;
610
611 switch (c->key_fmt) {
612 case UBIFS_SIMPLE_KEY_FMT:
613 c->key_len = UBIFS_SK_LEN;
614 break;
615 default:
Sheng Yong235c3622015-03-20 10:39:42 +0000616 ubifs_err(c, "unsupported key format");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300617 err = -EINVAL;
618 goto out;
619 }
620
621 c->leb_cnt = le32_to_cpu(sup->leb_cnt);
622 c->max_leb_cnt = le32_to_cpu(sup->max_leb_cnt);
623 c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
624 c->log_lebs = le32_to_cpu(sup->log_lebs);
625 c->lpt_lebs = le32_to_cpu(sup->lpt_lebs);
626 c->orph_lebs = le32_to_cpu(sup->orph_lebs);
627 c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
628 c->fanout = le32_to_cpu(sup->fanout);
629 c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300630 c->rp_size = le64_to_cpu(sup->rp_size);
Eric W. Biederman39241be2012-02-07 15:50:56 -0800631 c->rp_uid = make_kuid(&init_user_ns, le32_to_cpu(sup->rp_uid));
632 c->rp_gid = make_kgid(&init_user_ns, le32_to_cpu(sup->rp_gid));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300633 sup_flags = le32_to_cpu(sup->flags);
Artem Bityutskiy553dea42008-11-01 14:57:49 +0200634 if (!c->mount_opts.override_compr)
635 c->default_compr = le16_to_cpu(sup->default_compr);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300636
637 c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300638 memcpy(&c->uuid, &sup->uuid, 16);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300639 c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
Matthew L. Creech9f58d352011-05-05 16:33:20 -0400640 c->space_fixup = !!(sup_flags & UBIFS_FLG_SPACE_FIXUP);
Richard Weinbergerd63d61c2016-10-19 15:59:12 +0200641 c->double_hash = !!(sup_flags & UBIFS_FLG_DOUBLE_HASH);
Richard Weinbergere0219862016-10-19 23:24:47 +0200642 c->encrypted = !!(sup_flags & UBIFS_FLG_ENCRYPTION);
643
Richard Weinbergerfc4b8912016-10-19 23:46:39 +0200644 if ((sup_flags & ~UBIFS_FLG_MASK) != 0) {
645 ubifs_err(c, "Unknown feature flags found: %#x",
646 sup_flags & ~UBIFS_FLG_MASK);
647 err = -EINVAL;
648 goto out;
649 }
650
Richard Weinbergere0219862016-10-19 23:24:47 +0200651#ifndef CONFIG_UBIFS_FS_ENCRYPTION
652 if (c->encrypted) {
653 ubifs_err(c, "file system contains encrypted files but UBIFS"
654 " was built without crypto support.");
655 err = -EINVAL;
656 goto out;
657 }
658#endif
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300659
660 /* Automatically increase file system size to the maximum size */
661 c->old_leb_cnt = c->leb_cnt;
662 if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
663 c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300664 if (c->ro_mount)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300665 dbg_mnt("Auto resizing (ro) from %d LEBs to %d LEBs",
666 c->old_leb_cnt, c->leb_cnt);
667 else {
668 dbg_mnt("Auto resizing (sb) from %d LEBs to %d LEBs",
669 c->old_leb_cnt, c->leb_cnt);
670 sup->leb_cnt = cpu_to_le32(c->leb_cnt);
671 err = ubifs_write_sb_node(c, sup);
672 if (err)
673 goto out;
674 c->old_leb_cnt = c->leb_cnt;
675 }
676 }
677
678 c->log_bytes = (long long)c->log_lebs * c->leb_size;
679 c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
680 c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
681 c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
682 c->orph_first = c->lpt_last + 1;
683 c->orph_last = c->orph_first + c->orph_lebs - 1;
684 c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
685 c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
686 c->main_first = c->leb_cnt - c->main_lebs;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300687
688 err = validate_sb(c, sup);
689out:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300690 return err;
691}
Matthew L. Creech6554a652011-05-06 18:58:22 -0400692
693/**
694 * fixup_leb - fixup/unmap an LEB containing free space.
695 * @c: UBIFS file-system description object
696 * @lnum: the LEB number to fix up
697 * @len: number of used bytes in LEB (starting at offset 0)
698 *
699 * This function reads the contents of the given LEB number @lnum, then fixes
700 * it up, so that empty min. I/O units in the end of LEB are actually erased on
701 * flash (rather than being just all-0xff real data). If the LEB is completely
702 * empty, it is simply unmapped.
703 */
704static int fixup_leb(struct ubifs_info *c, int lnum, int len)
705{
706 int err;
707
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200708 ubifs_assert(c, len >= 0);
709 ubifs_assert(c, len % c->min_io_size == 0);
710 ubifs_assert(c, len < c->leb_size);
Matthew L. Creech6554a652011-05-06 18:58:22 -0400711
712 if (len == 0) {
713 dbg_mnt("unmap empty LEB %d", lnum);
Artem Bityutskiyd3b25782011-06-03 14:22:05 +0300714 return ubifs_leb_unmap(c, lnum);
Matthew L. Creech6554a652011-05-06 18:58:22 -0400715 }
716
717 dbg_mnt("fixup LEB %d, data len %d", lnum, len);
Artem Bityutskiyd3048202011-06-03 14:03:25 +0300718 err = ubifs_leb_read(c, lnum, c->sbuf, 0, len, 1);
Matthew L. Creech6554a652011-05-06 18:58:22 -0400719 if (err)
720 return err;
721
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200722 return ubifs_leb_change(c, lnum, c->sbuf, len);
Matthew L. Creech6554a652011-05-06 18:58:22 -0400723}
724
725/**
726 * fixup_free_space - find & remap all LEBs containing free space.
727 * @c: UBIFS file-system description object
728 *
729 * This function walks through all LEBs in the filesystem and fiexes up those
730 * containing free/empty space.
731 */
732static int fixup_free_space(struct ubifs_info *c)
733{
734 int lnum, err = 0;
735 struct ubifs_lprops *lprops;
736
737 ubifs_get_lprops(c);
738
739 /* Fixup LEBs in the master area */
740 for (lnum = UBIFS_MST_LNUM; lnum < UBIFS_LOG_LNUM; lnum++) {
741 err = fixup_leb(c, lnum, c->mst_offs + c->mst_node_alsz);
742 if (err)
743 goto out;
744 }
745
746 /* Unmap unused log LEBs */
747 lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
748 while (lnum != c->ltail_lnum) {
749 err = fixup_leb(c, lnum, 0);
750 if (err)
751 goto out;
752 lnum = ubifs_next_log_lnum(c, lnum);
753 }
754
Artem Bityutskiyc6727932012-07-14 14:33:09 +0300755 /*
756 * Fixup the log head which contains the only a CS node at the
757 * beginning.
758 */
759 err = fixup_leb(c, c->lhead_lnum,
760 ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size));
Matthew L. Creech6554a652011-05-06 18:58:22 -0400761 if (err)
762 goto out;
763
764 /* Fixup LEBs in the LPT area */
765 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
766 int free = c->ltab[lnum - c->lpt_first].free;
767
768 if (free > 0) {
769 err = fixup_leb(c, lnum, c->leb_size - free);
770 if (err)
771 goto out;
772 }
773 }
774
775 /* Unmap LEBs in the orphans area */
776 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
777 err = fixup_leb(c, lnum, 0);
778 if (err)
779 goto out;
780 }
781
782 /* Fixup LEBs in the main area */
783 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
784 lprops = ubifs_lpt_lookup(c, lnum);
785 if (IS_ERR(lprops)) {
786 err = PTR_ERR(lprops);
787 goto out;
788 }
789
790 if (lprops->free > 0) {
791 err = fixup_leb(c, lnum, c->leb_size - lprops->free);
792 if (err)
793 goto out;
794 }
795 }
796
797out:
798 ubifs_release_lprops(c);
799 return err;
800}
801
802/**
803 * ubifs_fixup_free_space - find & fix all LEBs with free space.
804 * @c: UBIFS file-system description object
805 *
806 * This function fixes up LEBs containing free space on first mount, if the
807 * appropriate flag was set when the FS was created. Each LEB with one or more
808 * empty min. I/O unit (i.e. free-space-count > 0) is re-written, to make sure
809 * the free space is actually erased. E.g., this is necessary for some NAND
810 * chips, since the free space may have been programmed like real "0xff" data
811 * (generating a non-0xff ECC), causing future writes to the not-really-erased
812 * NAND pages to behave badly. After the space is fixed up, the superblock flag
813 * is cleared, so that this is skipped for all future mounts.
814 */
815int ubifs_fixup_free_space(struct ubifs_info *c)
816{
817 int err;
Sascha Hauerfd615002018-09-07 14:36:29 +0200818 struct ubifs_sb_node *sup = c->sup_node;
Matthew L. Creech6554a652011-05-06 18:58:22 -0400819
Richard Weinberger6eb61d52018-07-12 13:01:57 +0200820 ubifs_assert(c, c->space_fixup);
821 ubifs_assert(c, !c->ro_mount);
Matthew L. Creech6554a652011-05-06 18:58:22 -0400822
Sheng Yong235c3622015-03-20 10:39:42 +0000823 ubifs_msg(c, "start fixing up free space");
Matthew L. Creech6554a652011-05-06 18:58:22 -0400824
825 err = fixup_free_space(c);
826 if (err)
827 return err;
828
Matthew L. Creech6554a652011-05-06 18:58:22 -0400829 /* Free-space fixup is no longer required */
830 c->space_fixup = 0;
831 sup->flags &= cpu_to_le32(~UBIFS_FLG_SPACE_FIXUP);
832
833 err = ubifs_write_sb_node(c, sup);
Matthew L. Creech6554a652011-05-06 18:58:22 -0400834 if (err)
835 return err;
836
Sheng Yong235c3622015-03-20 10:39:42 +0000837 ubifs_msg(c, "free space fixup complete");
Matthew L. Creech6554a652011-05-06 18:58:22 -0400838 return err;
839}
Richard Weinbergere0219862016-10-19 23:24:47 +0200840
841int ubifs_enable_encryption(struct ubifs_info *c)
842{
843 int err;
Sascha Hauerfd615002018-09-07 14:36:29 +0200844 struct ubifs_sb_node *sup = c->sup_node;
Richard Weinbergere0219862016-10-19 23:24:47 +0200845
846 if (c->encrypted)
847 return 0;
848
849 if (c->ro_mount || c->ro_media)
850 return -EROFS;
851
852 if (c->fmt_version < 5) {
853 ubifs_err(c, "on-flash format version 5 is needed for encryption");
854 return -EINVAL;
855 }
856
Richard Weinbergere0219862016-10-19 23:24:47 +0200857 sup->flags |= cpu_to_le32(UBIFS_FLG_ENCRYPTION);
858
859 err = ubifs_write_sb_node(c, sup);
860 if (!err)
861 c->encrypted = 1;
Richard Weinbergere0219862016-10-19 23:24:47 +0200862
863 return err;
864}