Blame view

fs/ubifs/sb.c 26.2 KB
2b27bdcc2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2
3
4
5
6
  /*
   * This file is part of UBIFS.
   *
   * Copyright (C) 2006-2008 Nokia Corporation.
   *
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
7
8
9
10
11
12
13
14
15
16
17
   * Authors: Artem Bityutskiy (Битюцкий Артём)
   *          Adrian Hunter
   */
  
  /*
   * This file implements UBIFS superblock. The superblock is stored at the first
   * LEB of the volume and is never changed by UBIFS. Only user-space tools may
   * change it. The superblock node mostly contains geometry information.
   */
  
  #include "ubifs.h"
5a0e3ad6a   Tejun Heo   include cleanup: ...
18
  #include <linux/slab.h>
4d61db4f8   Artem Bityutskiy   UBIFS: use nicer ...
19
  #include <linux/math64.h>
8da4b8c48   Andy Shevchenko   lib/uuid.c: move ...
20
  #include <linux/uuid.h>
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  
  /*
   * Default journal size in logical eraseblocks as a percent of total
   * flash size.
   */
  #define DEFAULT_JNL_PERCENT 5
  
  /* Default maximum journal size in bytes */
  #define DEFAULT_MAX_JNL (32*1024*1024)
  
  /* Default indexing tree fanout */
  #define DEFAULT_FANOUT 8
  
  /* Default number of data journal heads */
  #define DEFAULT_JHEADS_CNT 1
  
  /* Default positions of different LEBs in the main area */
  #define DEFAULT_IDX_LEB  0
  #define DEFAULT_DATA_LEB 1
  #define DEFAULT_GC_LEB   2
  
  /* Default number of LEB numbers in LPT's save table */
  #define DEFAULT_LSAVE_CNT 256
  
  /* Default reserved pool size as a percent of maximum free space */
  #define DEFAULT_RP_PERCENT 5
  
  /* The default maximum size of reserved pool in bytes */
  #define DEFAULT_MAX_RP_SIZE (5*1024*1024)
  
  /* Default time granularity in nanoseconds */
  #define DEFAULT_TIME_GRAN 1000000000
d62e98ed1   Gabor Juhos   ubifs: Fix defaul...
53
54
55
56
57
58
59
60
61
62
  static int get_default_compressor(struct ubifs_info *c)
  {
  	if (ubifs_compr_present(c, UBIFS_COMPR_LZO))
  		return UBIFS_COMPR_LZO;
  
  	if (ubifs_compr_present(c, UBIFS_COMPR_ZLIB))
  		return UBIFS_COMPR_ZLIB;
  
  	return UBIFS_COMPR_NONE;
  }
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  /**
   * create_default_filesystem - format empty UBI volume.
   * @c: UBIFS file-system description object
   *
   * This function creates default empty file-system. Returns zero in case of
   * success and a negative error code in case of failure.
   */
  static int create_default_filesystem(struct ubifs_info *c)
  {
  	struct ubifs_sb_node *sup;
  	struct ubifs_mst_node *mst;
  	struct ubifs_idx_node *idx;
  	struct ubifs_branch *br;
  	struct ubifs_ino_node *ino;
  	struct ubifs_cs_node *cs;
  	union ubifs_key key;
  	int err, tmp, jnl_lebs, log_lebs, max_buds, main_lebs, main_first;
  	int lpt_lebs, lpt_first, orph_lebs, big_lpt, ino_waste, sup_flags = 0;
  	int min_leb_cnt = UBIFS_MIN_LEB_CNT;
c4de6d7e4   Sascha Hauer   ubifs: Refactor c...
82
  	int idx_node_size;
4d61db4f8   Artem Bityutskiy   UBIFS: use nicer ...
83
  	long long tmp64, main_bytes;
0ecb9529a   Harvey Harrison   UBIFS: endian han...
84
  	__le64 tmp_le64;
0eca0b806   Arnd Bergmann   ubifs: use timesp...
85
  	struct timespec64 ts;
104115a3e   Sascha Hauer   ubifs: Add hashes...
86
  	u8 hash[UBIFS_HASH_ARR_SZ];
b5b1f0836   Sascha Hauer   ubifs: Create has...
87
  	u8 hash_lpt[UBIFS_HASH_ARR_SZ];
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  
  	/* Some functions called from here depend on the @c->key_len filed */
  	c->key_len = UBIFS_SK_LEN;
  
  	/*
  	 * First of all, we have to calculate default file-system geometry -
  	 * log size, journal size, etc.
  	 */
  	if (c->leb_cnt < 0x7FFFFFFF / DEFAULT_JNL_PERCENT)
  		/* We can first multiply then divide and have no overflow */
  		jnl_lebs = c->leb_cnt * DEFAULT_JNL_PERCENT / 100;
  	else
  		jnl_lebs = (c->leb_cnt / 100) * DEFAULT_JNL_PERCENT;
  
  	if (jnl_lebs < UBIFS_MIN_JNL_LEBS)
  		jnl_lebs = UBIFS_MIN_JNL_LEBS;
  	if (jnl_lebs * c->leb_size > DEFAULT_MAX_JNL)
  		jnl_lebs = DEFAULT_MAX_JNL / c->leb_size;
  
  	/*
  	 * The log should be large enough to fit reference nodes for all bud
  	 * LEBs. Because buds do not have to start from the beginning of LEBs
  	 * (half of the LEB may contain committed data), the log should
  	 * generally be larger, make it twice as large.
  	 */
  	tmp = 2 * (c->ref_node_alsz * jnl_lebs) + c->leb_size - 1;
  	log_lebs = tmp / c->leb_size;
  	/* Plus one LEB reserved for commit */
  	log_lebs += 1;
  	if (c->leb_cnt - min_leb_cnt > 8) {
  		/* And some extra space to allow writes while committing */
  		log_lebs += 1;
  		min_leb_cnt += 1;
  	}
  
  	max_buds = jnl_lebs - log_lebs;
  	if (max_buds < UBIFS_MIN_BUD_LEBS)
  		max_buds = UBIFS_MIN_BUD_LEBS;
  
  	/*
  	 * Orphan nodes are stored in a separate area. One node can store a lot
  	 * of orphan inode numbers, but when new orphan comes we just add a new
  	 * orphan node. At some point the nodes are consolidated into one
  	 * orphan node.
  	 */
  	orph_lebs = UBIFS_MIN_ORPH_LEBS;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
134
135
136
137
138
139
140
  	if (c->leb_cnt - min_leb_cnt > 1)
  		/*
  		 * For debugging purposes it is better to have at least 2
  		 * orphan LEBs, because the orphan subsystem would need to do
  		 * consolidations and would be stressed more.
  		 */
  		orph_lebs += 1;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
141
142
143
144
145
146
147
148
  
  	main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS - log_lebs;
  	main_lebs -= orph_lebs;
  
  	lpt_first = UBIFS_LOG_LNUM + log_lebs;
  	c->lsave_cnt = DEFAULT_LSAVE_CNT;
  	c->max_leb_cnt = c->leb_cnt;
  	err = ubifs_create_dflt_lpt(c, &main_lebs, lpt_first, &lpt_lebs,
b5b1f0836   Sascha Hauer   ubifs: Create has...
149
  				    &big_lpt, hash_lpt);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
150
151
152
153
154
155
156
  	if (err)
  		return err;
  
  	dbg_gen("LEB Properties Tree created (LEBs %d-%d)", lpt_first,
  		lpt_first + lpt_lebs - 1);
  
  	main_first = c->leb_cnt - main_lebs;
c4de6d7e4   Sascha Hauer   ubifs: Refactor c...
157
158
159
  	sup = kzalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_KERNEL);
  	mst = kzalloc(c->mst_node_alsz, GFP_KERNEL);
  	idx_node_size = ubifs_idx_node_sz(c, 1);
edec51374   Sascha Hauer   ubifs: Fix wrong ...
160
  	idx = kzalloc(ALIGN(idx_node_size, c->min_io_size), GFP_KERNEL);
c4de6d7e4   Sascha Hauer   ubifs: Refactor c...
161
162
163
164
165
166
167
  	ino = kzalloc(ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size), GFP_KERNEL);
  	cs = kzalloc(ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size), GFP_KERNEL);
  
  	if (!sup || !mst || !idx || !ino || !cs) {
  		err = -ENOMEM;
  		goto out;
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
168
  	/* Create default superblock */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
169

4d61db4f8   Artem Bityutskiy   UBIFS: use nicer ...
170
  	tmp64 = (long long)max_buds * c->leb_size;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
171
172
  	if (big_lpt)
  		sup_flags |= UBIFS_FLG_BIGLPT;
a7a8f4a1e   Martin Kaistra   ubifs: add option...
173
174
  	if (ubifs_default_version > 4)
  		sup_flags |= UBIFS_FLG_DOUBLE_HASH;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
175

104115a3e   Sascha Hauer   ubifs: Add hashes...
176
177
178
179
180
181
182
  	if (ubifs_authenticated(c)) {
  		sup_flags |= UBIFS_FLG_AUTHENTICATION;
  		sup->hash_algo = cpu_to_le16(c->auth_hash_algo);
  		err = ubifs_hmac_wkm(c, sup->hmac_wkm);
  		if (err)
  			goto out;
  	} else {
7cc7720f0   Ben Dooks (Codethink)   ubifs: Fix type o...
183
  		sup->hash_algo = cpu_to_le16(0xffff);
104115a3e   Sascha Hauer   ubifs: Add hashes...
184
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  	sup->ch.node_type  = UBIFS_SB_NODE;
  	sup->key_hash      = UBIFS_KEY_HASH_R5;
  	sup->flags         = cpu_to_le32(sup_flags);
  	sup->min_io_size   = cpu_to_le32(c->min_io_size);
  	sup->leb_size      = cpu_to_le32(c->leb_size);
  	sup->leb_cnt       = cpu_to_le32(c->leb_cnt);
  	sup->max_leb_cnt   = cpu_to_le32(c->max_leb_cnt);
  	sup->max_bud_bytes = cpu_to_le64(tmp64);
  	sup->log_lebs      = cpu_to_le32(log_lebs);
  	sup->lpt_lebs      = cpu_to_le32(lpt_lebs);
  	sup->orph_lebs     = cpu_to_le32(orph_lebs);
  	sup->jhead_cnt     = cpu_to_le32(DEFAULT_JHEADS_CNT);
  	sup->fanout        = cpu_to_le32(DEFAULT_FANOUT);
  	sup->lsave_cnt     = cpu_to_le32(c->lsave_cnt);
a7a8f4a1e   Martin Kaistra   ubifs: add option...
199
  	sup->fmt_version   = cpu_to_le32(ubifs_default_version);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
200
  	sup->time_gran     = cpu_to_le32(DEFAULT_TIME_GRAN);
553dea4dd   Artem Bityutskiy   UBIFS: introduce ...
201
202
203
  	if (c->mount_opts.override_compr)
  		sup->default_compr = cpu_to_le16(c->mount_opts.compr_type);
  	else
d62e98ed1   Gabor Juhos   ubifs: Fix defaul...
204
  		sup->default_compr = cpu_to_le16(get_default_compressor(c));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
205
206
  
  	generate_random_uuid(sup->uuid);
4d61db4f8   Artem Bityutskiy   UBIFS: use nicer ...
207
208
  	main_bytes = (long long)main_lebs * c->leb_size;
  	tmp64 = div_u64(main_bytes * DEFAULT_RP_PERCENT, 100);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
209
210
211
  	if (tmp64 > DEFAULT_MAX_RP_SIZE)
  		tmp64 = DEFAULT_MAX_RP_SIZE;
  	sup->rp_size = cpu_to_le64(tmp64);
963f0cf6d   Artem Bityutskiy   UBIFS: add R/O co...
212
  	sup->ro_compat_version = cpu_to_le32(UBIFS_RO_COMPAT_VERSION);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
213

1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
214
215
216
  	dbg_gen("default superblock created at LEB 0:0");
  
  	/* Create default master node */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
  
  	mst->ch.node_type = UBIFS_MST_NODE;
  	mst->log_lnum     = cpu_to_le32(UBIFS_LOG_LNUM);
  	mst->highest_inum = cpu_to_le64(UBIFS_FIRST_INO);
  	mst->cmt_no       = 0;
  	mst->root_lnum    = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
  	mst->root_offs    = 0;
  	tmp = ubifs_idx_node_sz(c, 1);
  	mst->root_len     = cpu_to_le32(tmp);
  	mst->gc_lnum      = cpu_to_le32(main_first + DEFAULT_GC_LEB);
  	mst->ihead_lnum   = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
  	mst->ihead_offs   = cpu_to_le32(ALIGN(tmp, c->min_io_size));
  	mst->index_size   = cpu_to_le64(ALIGN(tmp, 8));
  	mst->lpt_lnum     = cpu_to_le32(c->lpt_lnum);
  	mst->lpt_offs     = cpu_to_le32(c->lpt_offs);
  	mst->nhead_lnum   = cpu_to_le32(c->nhead_lnum);
  	mst->nhead_offs   = cpu_to_le32(c->nhead_offs);
  	mst->ltab_lnum    = cpu_to_le32(c->ltab_lnum);
  	mst->ltab_offs    = cpu_to_le32(c->ltab_offs);
  	mst->lsave_lnum   = cpu_to_le32(c->lsave_lnum);
  	mst->lsave_offs   = cpu_to_le32(c->lsave_offs);
  	mst->lscan_lnum   = cpu_to_le32(main_first);
  	mst->empty_lebs   = cpu_to_le32(main_lebs - 2);
  	mst->idx_lebs     = cpu_to_le32(1);
  	mst->leb_cnt      = cpu_to_le32(c->leb_cnt);
104115a3e   Sascha Hauer   ubifs: Add hashes...
242
  	ubifs_copy_hash(c, hash_lpt, mst->hash_lpt);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  
  	/* Calculate lprops statistics */
  	tmp64 = main_bytes;
  	tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
  	tmp64 -= ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
  	mst->total_free = cpu_to_le64(tmp64);
  
  	tmp64 = ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
  	ino_waste = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size) -
  			  UBIFS_INO_NODE_SZ;
  	tmp64 += ino_waste;
  	tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), 8);
  	mst->total_dirty = cpu_to_le64(tmp64);
  
  	/*  The indexing LEB does not contribute to dark space */
7606f85a7   srimugunthan dhandapani   UBIFS: fix the da...
258
  	tmp64 = ((long long)(c->main_lebs - 1) * c->dark_wm);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
259
260
261
  	mst->total_dark = cpu_to_le64(tmp64);
  
  	mst->total_used = cpu_to_le64(UBIFS_INO_NODE_SZ);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
262
263
264
  	dbg_gen("default master node created at LEB %d:0", UBIFS_MST_LNUM);
  
  	/* Create the root indexing node */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
265
266
267
268
269
270
271
272
273
274
275
  
  	c->key_fmt = UBIFS_SIMPLE_KEY_FMT;
  	c->key_hash = key_r5_hash;
  
  	idx->ch.node_type = UBIFS_IDX_NODE;
  	idx->child_cnt = cpu_to_le16(1);
  	ino_key_init(c, &key, UBIFS_ROOT_INO);
  	br = ubifs_idx_branch(c, idx, 0);
  	key_write_idx(c, &key, &br->key);
  	br->lnum = cpu_to_le32(main_first + DEFAULT_DATA_LEB);
  	br->len  = cpu_to_le32(UBIFS_INO_NODE_SZ);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
276
277
278
279
280
  
  	dbg_gen("default root indexing node created LEB %d:0",
  		main_first + DEFAULT_IDX_LEB);
  
  	/* Create default root inode */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
281
282
283
284
285
  
  	ino_key_init_flash(c, &ino->key, UBIFS_ROOT_INO);
  	ino->ch.node_type = UBIFS_INO_NODE;
  	ino->creat_sqnum = cpu_to_le64(++c->max_sqnum);
  	ino->nlink = cpu_to_le32(2);
607a11ad9   Deepa Dinamani   fs: ubifs: replac...
286

1bfad0c07   Deepa Dinamani   fs: ubifs: Elimin...
287
  	ktime_get_coarse_real_ts64(&ts);
607a11ad9   Deepa Dinamani   fs: ubifs: replac...
288
  	tmp_le64 = cpu_to_le64(ts.tv_sec);
0ecb9529a   Harvey Harrison   UBIFS: endian han...
289
290
291
  	ino->atime_sec   = tmp_le64;
  	ino->ctime_sec   = tmp_le64;
  	ino->mtime_sec   = tmp_le64;
1bfad0c07   Deepa Dinamani   fs: ubifs: Elimin...
292
293
294
  	ino->atime_nsec  = 0;
  	ino->ctime_nsec  = 0;
  	ino->mtime_nsec  = 0;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
295
296
297
298
299
  	ino->mode = cpu_to_le32(S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
  	ino->size = cpu_to_le64(UBIFS_INO_NODE_SZ);
  
  	/* Set compression enabled by default */
  	ino->flags = cpu_to_le32(UBIFS_COMPR_FL);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
300
301
302
303
304
305
306
307
  	dbg_gen("root inode created at LEB %d:0",
  		main_first + DEFAULT_DATA_LEB);
  
  	/*
  	 * The first node in the log has to be the commit start node. This is
  	 * always the case during normal file-system operation. Write a fake
  	 * commit start node to the log.
  	 */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
308
309
  
  	cs->ch.node_type = UBIFS_CS_NODE;
c4de6d7e4   Sascha Hauer   ubifs: Refactor c...
310

104115a3e   Sascha Hauer   ubifs: Add hashes...
311
312
  	err = ubifs_write_node_hmac(c, sup, UBIFS_SB_NODE_SZ, 0, 0,
  				    offsetof(struct ubifs_sb_node, hmac));
c4de6d7e4   Sascha Hauer   ubifs: Refactor c...
313
314
  	if (err)
  		goto out;
104115a3e   Sascha Hauer   ubifs: Add hashes...
315
316
  	err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
  			       main_first + DEFAULT_DATA_LEB, 0);
c4de6d7e4   Sascha Hauer   ubifs: Refactor c...
317
318
  	if (err)
  		goto out;
104115a3e   Sascha Hauer   ubifs: Add hashes...
319
320
321
322
  	ubifs_node_calc_hash(c, ino, hash);
  	ubifs_copy_hash(c, hash, ubifs_branch_hash(c, br));
  
  	err = ubifs_write_node(c, idx, idx_node_size, main_first + DEFAULT_IDX_LEB, 0);
c4de6d7e4   Sascha Hauer   ubifs: Refactor c...
323
324
  	if (err)
  		goto out;
104115a3e   Sascha Hauer   ubifs: Add hashes...
325
326
327
328
329
  	ubifs_node_calc_hash(c, idx, hash);
  	ubifs_copy_hash(c, hash, mst->hash_root_idx);
  
  	err = ubifs_write_node_hmac(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0,
  		offsetof(struct ubifs_mst_node, hmac));
c4de6d7e4   Sascha Hauer   ubifs: Refactor c...
330
331
  	if (err)
  		goto out;
104115a3e   Sascha Hauer   ubifs: Add hashes...
332
333
  	err = ubifs_write_node_hmac(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1,
  			       0, offsetof(struct ubifs_mst_node, hmac));
c4de6d7e4   Sascha Hauer   ubifs: Refactor c...
334
335
  	if (err)
  		goto out;
b36a261e8   Richard Weinberger   UBI: Kill data ty...
336
  	err = ubifs_write_node(c, cs, UBIFS_CS_NODE_SZ, UBIFS_LOG_LNUM, 0);
6dcfb8026   hujianyang   UBIFS: fix error ...
337
  	if (err)
c4de6d7e4   Sascha Hauer   ubifs: Refactor c...
338
  		goto out;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
339

235c362bd   Sheng Yong   UBIFS: extend deb...
340
  	ubifs_msg(c, "default file-system created");
c4de6d7e4   Sascha Hauer   ubifs: Refactor c...
341
342
343
344
345
346
347
348
349
350
  
  	err = 0;
  out:
  	kfree(sup);
  	kfree(mst);
  	kfree(idx);
  	kfree(ino);
  	kfree(cs);
  
  	return err;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
  }
  
  /**
   * validate_sb - validate superblock node.
   * @c: UBIFS file-system description object
   * @sup: superblock node
   *
   * This function validates superblock node @sup. Since most of data was read
   * from the superblock and stored in @c, the function validates fields in @c
   * instead. Returns zero in case of success and %-EINVAL in case of validation
   * failure.
   */
  static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
  {
  	long long max_bytes;
  	int err = 1, min_leb_cnt;
  
  	if (!c->key_hash) {
  		err = 2;
  		goto failed;
  	}
  
  	if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
  		err = 3;
  		goto failed;
  	}
  
  	if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
235c362bd   Sheng Yong   UBIFS: extend deb...
379
  		ubifs_err(c, "min. I/O unit mismatch: %d in superblock, %d real",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
380
381
382
383
384
  			  le32_to_cpu(sup->min_io_size), c->min_io_size);
  		goto failed;
  	}
  
  	if (le32_to_cpu(sup->leb_size) != c->leb_size) {
235c362bd   Sheng Yong   UBIFS: extend deb...
385
  		ubifs_err(c, "LEB size mismatch: %d in superblock, %d real",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
  			  le32_to_cpu(sup->leb_size), c->leb_size);
  		goto failed;
  	}
  
  	if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
  	    c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
  	    c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
  	    c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
  		err = 4;
  		goto failed;
  	}
  
  	/*
  	 * Calculate minimum allowed amount of main area LEBs. This is very
  	 * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
  	 * have just read from the superblock.
  	 */
  	min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
  	min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
  
  	if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
235c362bd   Sheng Yong   UBIFS: extend deb...
407
  		ubifs_err(c, "bad LEB count: %d in superblock, %d on UBI volume, %d minimum required",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
408
  			  c->leb_cnt, c->vi.size, min_leb_cnt);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
409
410
411
412
  		goto failed;
  	}
  
  	if (c->max_leb_cnt < c->leb_cnt) {
235c362bd   Sheng Yong   UBIFS: extend deb...
413
  		ubifs_err(c, "max. LEB count %d less than LEB count %d",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
414
415
416
417
418
  			  c->max_leb_cnt, c->leb_cnt);
  		goto failed;
  	}
  
  	if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
235c362bd   Sheng Yong   UBIFS: extend deb...
419
  		ubifs_err(c, "too few main LEBs count %d, must be at least %d",
5a1f36c90   Artem Bityutskiy   UBIFS: improve er...
420
  			  c->main_lebs, UBIFS_MIN_MAIN_LEBS);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
421
422
  		goto failed;
  	}
5a1f36c90   Artem Bityutskiy   UBIFS: improve er...
423
424
  	max_bytes = (long long)c->leb_size * UBIFS_MIN_BUD_LEBS;
  	if (c->max_bud_bytes < max_bytes) {
235c362bd   Sheng Yong   UBIFS: extend deb...
425
  		ubifs_err(c, "too small journal (%lld bytes), must be at least %lld bytes",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
426
  			  c->max_bud_bytes, max_bytes);
5a1f36c90   Artem Bityutskiy   UBIFS: improve er...
427
428
429
430
431
  		goto failed;
  	}
  
  	max_bytes = (long long)c->leb_size * c->main_lebs;
  	if (c->max_bud_bytes > max_bytes) {
235c362bd   Sheng Yong   UBIFS: extend deb...
432
  		ubifs_err(c, "too large journal size (%lld bytes), only %lld bytes available in the main area",
5a1f36c90   Artem Bityutskiy   UBIFS: improve er...
433
  			  c->max_bud_bytes, max_bytes);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
  		goto failed;
  	}
  
  	if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
  	    c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
  		err = 9;
  		goto failed;
  	}
  
  	if (c->fanout < UBIFS_MIN_FANOUT ||
  	    ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
  		err = 10;
  		goto failed;
  	}
  
  	if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
  	    c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
  	    c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
  		err = 11;
  		goto failed;
  	}
  
  	if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
  	    c->orph_lebs + c->main_lebs != c->leb_cnt) {
  		err = 12;
  		goto failed;
  	}
b793a8c88   hujianyang   UBIFS: remove use...
461
  	if (c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
462
463
464
  		err = 13;
  		goto failed;
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
465
466
467
468
469
470
471
472
473
474
  	if (c->rp_size < 0 || max_bytes < c->rp_size) {
  		err = 14;
  		goto failed;
  	}
  
  	if (le32_to_cpu(sup->time_gran) > 1000000000 ||
  	    le32_to_cpu(sup->time_gran) < 1) {
  		err = 15;
  		goto failed;
  	}
fc4b891bb   Richard Weinberger   ubifs: Raise writ...
475
476
477
478
479
480
481
482
483
  	if (!c->double_hash && c->fmt_version >= 5) {
  		err = 16;
  		goto failed;
  	}
  
  	if (c->encrypted && c->fmt_version < 5) {
  		err = 17;
  		goto failed;
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
484
485
486
  	return 0;
  
  failed:
235c362bd   Sheng Yong   UBIFS: extend deb...
487
  	ubifs_err(c, "bad superblock, error %d", err);
edf6be245   Artem Bityutskiy   UBIFS: rename dum...
488
  	ubifs_dump_node(c, sup);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
489
490
491
492
493
494
495
496
  	return -EINVAL;
  }
  
  /**
   * ubifs_read_sb_node - read superblock node.
   * @c: UBIFS file-system description object
   *
   * This function returns a pointer to the superblock node or a negative error
eaeee242c   Artem Bityutskiy   UBIFS: fix a rare...
497
498
   * code. Note, the user of this function is responsible of kfree()'ing the
   * returned superblock buffer.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
499
   */
fd6150051   Sascha Hauer   ubifs: Store read...
500
  static struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
  {
  	struct ubifs_sb_node *sup;
  	int err;
  
  	sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
  	if (!sup)
  		return ERR_PTR(-ENOMEM);
  
  	err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
  			      UBIFS_SB_LNUM, 0);
  	if (err) {
  		kfree(sup);
  		return ERR_PTR(err);
  	}
  
  	return sup;
  }
e158e02ff   Sascha Hauer   ubifs: authentica...
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
  static int authenticate_sb_node(struct ubifs_info *c,
  				const struct ubifs_sb_node *sup)
  {
  	unsigned int sup_flags = le32_to_cpu(sup->flags);
  	u8 hmac_wkm[UBIFS_HMAC_ARR_SZ];
  	int authenticated = !!(sup_flags & UBIFS_FLG_AUTHENTICATION);
  	int hash_algo;
  	int err;
  
  	if (c->authenticated && !authenticated) {
  		ubifs_err(c, "authenticated FS forced, but found FS without authentication");
  		return -EINVAL;
  	}
  
  	if (!c->authenticated && authenticated) {
  		ubifs_err(c, "authenticated FS found, but no key given");
  		return -EINVAL;
  	}
  
  	ubifs_msg(c, "Mounting in %sauthenticated mode",
  		  c->authenticated ? "" : "un");
  
  	if (!c->authenticated)
  		return 0;
  
  	if (!IS_ENABLED(CONFIG_UBIFS_FS_AUTHENTICATION))
  		return -EOPNOTSUPP;
  
  	hash_algo = le16_to_cpu(sup->hash_algo);
  	if (hash_algo >= HASH_ALGO__LAST) {
  		ubifs_err(c, "superblock uses unknown hash algo %d",
  			  hash_algo);
  		return -EINVAL;
  	}
  
  	if (strcmp(hash_algo_name[hash_algo], c->auth_hash_name)) {
  		ubifs_err(c, "This filesystem uses %s for hashing,"
  			     " but %s is specified", hash_algo_name[hash_algo],
  			     c->auth_hash_name);
  		return -EINVAL;
  	}
817aa0948   Sascha Hauer   ubifs: support of...
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
  	/*
  	 * The super block node can either be authenticated by a HMAC or
  	 * by a signature in a ubifs_sig_node directly following the
  	 * super block node to support offline image creation.
  	 */
  	if (ubifs_hmac_zero(c, sup->hmac)) {
  		err = ubifs_sb_verify_signature(c, sup);
  	} else {
  		err = ubifs_hmac_wkm(c, hmac_wkm);
  		if (err)
  			return err;
  		if (ubifs_check_hmac(c, hmac_wkm, sup->hmac_wkm)) {
  			ubifs_err(c, "provided key does not fit");
  			return -ENOKEY;
  		}
  		err = ubifs_node_verify_hmac(c, sup, sizeof(*sup),
  					     offsetof(struct ubifs_sb_node,
  						      hmac));
e158e02ff   Sascha Hauer   ubifs: authentica...
577
  	}
e158e02ff   Sascha Hauer   ubifs: authentica...
578
579
580
581
582
  	if (err)
  		ubifs_err(c, "Failed to authenticate superblock: %d", err);
  
  	return err;
  }
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
583
584
585
586
587
588
589
590
591
592
  /**
   * ubifs_write_sb_node - write superblock node.
   * @c: UBIFS file-system description object
   * @sup: superblock node read with 'ubifs_read_sb_node()'
   *
   * This function returns %0 on success and a negative error code on failure.
   */
  int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
  {
  	int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
e158e02ff   Sascha Hauer   ubifs: authentica...
593
594
595
596
597
598
  	int err;
  
  	err = ubifs_prepare_node_hmac(c, sup, UBIFS_SB_NODE_SZ,
  				      offsetof(struct ubifs_sb_node, hmac), 1);
  	if (err)
  		return err;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
599

b36a261e8   Richard Weinberger   UBI: Kill data ty...
600
  	return ubifs_leb_change(c, UBIFS_SB_LNUM, sup, len);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
  }
  
  /**
   * ubifs_read_superblock - read superblock.
   * @c: UBIFS file-system description object
   *
   * This function finds, reads and checks the superblock. If an empty UBI volume
   * is being mounted, this function creates default superblock. Returns zero in
   * case of success, and a negative error code in case of failure.
   */
  int ubifs_read_superblock(struct ubifs_info *c)
  {
  	int err, sup_flags;
  	struct ubifs_sb_node *sup;
  
  	if (c->empty) {
  		err = create_default_filesystem(c);
  		if (err)
  			return err;
  	}
  
  	sup = ubifs_read_sb_node(c);
  	if (IS_ERR(sup))
  		return PTR_ERR(sup);
fd6150051   Sascha Hauer   ubifs: Store read...
625
  	c->sup_node = sup;
963f0cf6d   Artem Bityutskiy   UBIFS: add R/O co...
626
627
  	c->fmt_version = le32_to_cpu(sup->fmt_version);
  	c->ro_compat_version = le32_to_cpu(sup->ro_compat_version);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
628
629
630
631
  	/*
  	 * The software supports all previous versions but not future versions,
  	 * due to the unavailability of time-travelling equipment.
  	 */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
632
  	if (c->fmt_version > UBIFS_FORMAT_VERSION) {
6eb61d587   Richard Weinberger   ubifs: Pass struc...
633
  		ubifs_assert(c, !c->ro_media || c->ro_mount);
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
634
  		if (!c->ro_mount ||
963f0cf6d   Artem Bityutskiy   UBIFS: add R/O co...
635
  		    c->ro_compat_version > UBIFS_RO_COMPAT_VERSION) {
235c362bd   Sheng Yong   UBIFS: extend deb...
636
  			ubifs_err(c, "on-flash format version is w%d/r%d, but software only supports up to version w%d/r%d",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
637
638
  				  c->fmt_version, c->ro_compat_version,
  				  UBIFS_FORMAT_VERSION,
963f0cf6d   Artem Bityutskiy   UBIFS: add R/O co...
639
640
  				  UBIFS_RO_COMPAT_VERSION);
  			if (c->ro_compat_version <= UBIFS_RO_COMPAT_VERSION) {
235c362bd   Sheng Yong   UBIFS: extend deb...
641
  				ubifs_msg(c, "only R/O mounting is possible");
963f0cf6d   Artem Bityutskiy   UBIFS: add R/O co...
642
643
644
645
646
647
648
649
650
651
652
653
  				err = -EROFS;
  			} else
  				err = -EINVAL;
  			goto out;
  		}
  
  		/*
  		 * The FS is mounted R/O, and the media format is
  		 * R/O-compatible with the UBIFS implementation, so we can
  		 * mount.
  		 */
  		c->rw_incompat = 1;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
654
655
656
  	}
  
  	if (c->fmt_version < 3) {
235c362bd   Sheng Yong   UBIFS: extend deb...
657
  		ubifs_err(c, "on-flash format version %d is not supported",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
  			  c->fmt_version);
  		err = -EINVAL;
  		goto out;
  	}
  
  	switch (sup->key_hash) {
  	case UBIFS_KEY_HASH_R5:
  		c->key_hash = key_r5_hash;
  		c->key_hash_type = UBIFS_KEY_HASH_R5;
  		break;
  
  	case UBIFS_KEY_HASH_TEST:
  		c->key_hash = key_test_hash;
  		c->key_hash_type = UBIFS_KEY_HASH_TEST;
  		break;
84db119f5   Ding Xiang   ubifs: Remove unn...
673
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
674
675
676
677
678
679
680
681
  
  	c->key_fmt = sup->key_fmt;
  
  	switch (c->key_fmt) {
  	case UBIFS_SIMPLE_KEY_FMT:
  		c->key_len = UBIFS_SK_LEN;
  		break;
  	default:
235c362bd   Sheng Yong   UBIFS: extend deb...
682
  		ubifs_err(c, "unsupported key format");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
683
684
685
686
687
688
689
690
691
692
693
694
695
  		err = -EINVAL;
  		goto out;
  	}
  
  	c->leb_cnt       = le32_to_cpu(sup->leb_cnt);
  	c->max_leb_cnt   = le32_to_cpu(sup->max_leb_cnt);
  	c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
  	c->log_lebs      = le32_to_cpu(sup->log_lebs);
  	c->lpt_lebs      = le32_to_cpu(sup->lpt_lebs);
  	c->orph_lebs     = le32_to_cpu(sup->orph_lebs);
  	c->jhead_cnt     = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
  	c->fanout        = le32_to_cpu(sup->fanout);
  	c->lsave_cnt     = le32_to_cpu(sup->lsave_cnt);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
696
  	c->rp_size       = le64_to_cpu(sup->rp_size);
39241beb7   Eric W. Biederman   userns: Convert u...
697
698
  	c->rp_uid        = make_kuid(&init_user_ns, le32_to_cpu(sup->rp_uid));
  	c->rp_gid        = make_kgid(&init_user_ns, le32_to_cpu(sup->rp_gid));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
699
  	sup_flags        = le32_to_cpu(sup->flags);
553dea4dd   Artem Bityutskiy   UBIFS: introduce ...
700
701
  	if (!c->mount_opts.override_compr)
  		c->default_compr = le16_to_cpu(sup->default_compr);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
702
703
  
  	c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
704
  	memcpy(&c->uuid, &sup->uuid, 16);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
705
  	c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
9f58d3503   Matthew L. Creech   UBIFS: add a supe...
706
  	c->space_fixup = !!(sup_flags & UBIFS_FLG_SPACE_FIXUP);
d63d61c16   Richard Weinberger   ubifs: Implement ...
707
  	c->double_hash = !!(sup_flags & UBIFS_FLG_DOUBLE_HASH);
e021986ee   Richard Weinberger   ubifs: Implement ...
708
  	c->encrypted = !!(sup_flags & UBIFS_FLG_ENCRYPTION);
e158e02ff   Sascha Hauer   ubifs: authentica...
709
710
711
  	err = authenticate_sb_node(c, sup);
  	if (err)
  		goto out;
fc4b891bb   Richard Weinberger   ubifs: Raise writ...
712
713
714
715
716
717
  	if ((sup_flags & ~UBIFS_FLG_MASK) != 0) {
  		ubifs_err(c, "Unknown feature flags found: %#x",
  			  sup_flags & ~UBIFS_FLG_MASK);
  		err = -EINVAL;
  		goto out;
  	}
76aa34944   Richard Weinberger   ubifs: Use correc...
718
  	if (!IS_ENABLED(CONFIG_FS_ENCRYPTION) && c->encrypted) {
e021986ee   Richard Weinberger   ubifs: Implement ...
719
720
721
722
723
  		ubifs_err(c, "file system contains encrypted files but UBIFS"
  			     " was built without crypto support.");
  		err = -EINVAL;
  		goto out;
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
724
725
  
  	/* Automatically increase file system size to the maximum size */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
726
  	if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
817aa0948   Sascha Hauer   ubifs: support of...
727
  		int old_leb_cnt = c->leb_cnt;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
728
  		c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
817aa0948   Sascha Hauer   ubifs: support of...
729
730
731
732
733
734
  		sup->leb_cnt = cpu_to_le32(c->leb_cnt);
  
  		c->superblock_need_write = 1;
  
  		dbg_mnt("Auto resizing from %d LEBs to %d LEBs",
  			old_leb_cnt, c->leb_cnt);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
735
736
737
738
739
740
741
742
743
744
745
  	}
  
  	c->log_bytes = (long long)c->log_lebs * c->leb_size;
  	c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
  	c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
  	c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
  	c->orph_first = c->lpt_last + 1;
  	c->orph_last = c->orph_first + c->orph_lebs - 1;
  	c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
  	c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
  	c->main_first = c->leb_cnt - c->main_lebs;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
746
747
748
  
  	err = validate_sb(c, sup);
  out:
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
749
750
  	return err;
  }
6554a6578   Matthew L. Creech   UBIFS: add the fi...
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
  
  /**
   * fixup_leb - fixup/unmap an LEB containing free space.
   * @c: UBIFS file-system description object
   * @lnum: the LEB number to fix up
   * @len: number of used bytes in LEB (starting at offset 0)
   *
   * This function reads the contents of the given LEB number @lnum, then fixes
   * it up, so that empty min. I/O units in the end of LEB are actually erased on
   * flash (rather than being just all-0xff real data). If the LEB is completely
   * empty, it is simply unmapped.
   */
  static int fixup_leb(struct ubifs_info *c, int lnum, int len)
  {
  	int err;
6eb61d587   Richard Weinberger   ubifs: Pass struc...
766
767
768
  	ubifs_assert(c, len >= 0);
  	ubifs_assert(c, len % c->min_io_size == 0);
  	ubifs_assert(c, len < c->leb_size);
6554a6578   Matthew L. Creech   UBIFS: add the fi...
769
770
771
  
  	if (len == 0) {
  		dbg_mnt("unmap empty LEB %d", lnum);
d3b2578f5   Artem Bityutskiy   UBIFS: switch to ...
772
  		return ubifs_leb_unmap(c, lnum);
6554a6578   Matthew L. Creech   UBIFS: add the fi...
773
774
775
  	}
  
  	dbg_mnt("fixup LEB %d, data len %d", lnum, len);
d304820a1   Artem Bityutskiy   UBIFS: switch to ...
776
  	err = ubifs_leb_read(c, lnum, c->sbuf, 0, len, 1);
6554a6578   Matthew L. Creech   UBIFS: add the fi...
777
778
  	if (err)
  		return err;
b36a261e8   Richard Weinberger   UBI: Kill data ty...
779
  	return ubifs_leb_change(c, lnum, c->sbuf, len);
6554a6578   Matthew L. Creech   UBIFS: add the fi...
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
  }
  
  /**
   * fixup_free_space - find & remap all LEBs containing free space.
   * @c: UBIFS file-system description object
   *
   * This function walks through all LEBs in the filesystem and fiexes up those
   * containing free/empty space.
   */
  static int fixup_free_space(struct ubifs_info *c)
  {
  	int lnum, err = 0;
  	struct ubifs_lprops *lprops;
  
  	ubifs_get_lprops(c);
  
  	/* Fixup LEBs in the master area */
  	for (lnum = UBIFS_MST_LNUM; lnum < UBIFS_LOG_LNUM; lnum++) {
  		err = fixup_leb(c, lnum, c->mst_offs + c->mst_node_alsz);
  		if (err)
  			goto out;
  	}
  
  	/* Unmap unused log LEBs */
  	lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
  	while (lnum != c->ltail_lnum) {
  		err = fixup_leb(c, lnum, 0);
  		if (err)
  			goto out;
  		lnum = ubifs_next_log_lnum(c, lnum);
  	}
c6727932c   Artem Bityutskiy   UBIFS: fix a bug ...
811
812
813
814
815
816
  	/*
  	 * Fixup the log head which contains the only a CS node at the
  	 * beginning.
  	 */
  	err = fixup_leb(c, c->lhead_lnum,
  			ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size));
6554a6578   Matthew L. Creech   UBIFS: add the fi...
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
  	if (err)
  		goto out;
  
  	/* Fixup LEBs in the LPT area */
  	for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
  		int free = c->ltab[lnum - c->lpt_first].free;
  
  		if (free > 0) {
  			err = fixup_leb(c, lnum, c->leb_size - free);
  			if (err)
  				goto out;
  		}
  	}
  
  	/* Unmap LEBs in the orphans area */
  	for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
  		err = fixup_leb(c, lnum, 0);
  		if (err)
  			goto out;
  	}
  
  	/* Fixup LEBs in the main area */
  	for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
  		lprops = ubifs_lpt_lookup(c, lnum);
  		if (IS_ERR(lprops)) {
  			err = PTR_ERR(lprops);
  			goto out;
  		}
  
  		if (lprops->free > 0) {
  			err = fixup_leb(c, lnum, c->leb_size - lprops->free);
  			if (err)
  				goto out;
  		}
  	}
  
  out:
  	ubifs_release_lprops(c);
  	return err;
  }
  
  /**
   * ubifs_fixup_free_space - find & fix all LEBs with free space.
   * @c: UBIFS file-system description object
   *
   * This function fixes up LEBs containing free space on first mount, if the
   * appropriate flag was set when the FS was created. Each LEB with one or more
   * empty min. I/O unit (i.e. free-space-count > 0) is re-written, to make sure
   * the free space is actually erased. E.g., this is necessary for some NAND
   * chips, since the free space may have been programmed like real "0xff" data
   * (generating a non-0xff ECC), causing future writes to the not-really-erased
   * NAND pages to behave badly. After the space is fixed up, the superblock flag
   * is cleared, so that this is skipped for all future mounts.
   */
  int ubifs_fixup_free_space(struct ubifs_info *c)
  {
  	int err;
fd6150051   Sascha Hauer   ubifs: Store read...
874
  	struct ubifs_sb_node *sup = c->sup_node;
6554a6578   Matthew L. Creech   UBIFS: add the fi...
875

6eb61d587   Richard Weinberger   ubifs: Pass struc...
876
877
  	ubifs_assert(c, c->space_fixup);
  	ubifs_assert(c, !c->ro_mount);
6554a6578   Matthew L. Creech   UBIFS: add the fi...
878

235c362bd   Sheng Yong   UBIFS: extend deb...
879
  	ubifs_msg(c, "start fixing up free space");
6554a6578   Matthew L. Creech   UBIFS: add the fi...
880
881
882
883
  
  	err = fixup_free_space(c);
  	if (err)
  		return err;
6554a6578   Matthew L. Creech   UBIFS: add the fi...
884
885
886
  	/* Free-space fixup is no longer required */
  	c->space_fixup = 0;
  	sup->flags &= cpu_to_le32(~UBIFS_FLG_SPACE_FIXUP);
817aa0948   Sascha Hauer   ubifs: support of...
887
  	c->superblock_need_write = 1;
6554a6578   Matthew L. Creech   UBIFS: add the fi...
888

235c362bd   Sheng Yong   UBIFS: extend deb...
889
  	ubifs_msg(c, "free space fixup complete");
6554a6578   Matthew L. Creech   UBIFS: add the fi...
890
891
  	return err;
  }
e021986ee   Richard Weinberger   ubifs: Implement ...
892
893
894
895
  
  int ubifs_enable_encryption(struct ubifs_info *c)
  {
  	int err;
fd6150051   Sascha Hauer   ubifs: Store read...
896
  	struct ubifs_sb_node *sup = c->sup_node;
e021986ee   Richard Weinberger   ubifs: Implement ...
897

76aa34944   Richard Weinberger   ubifs: Use correc...
898
  	if (!IS_ENABLED(CONFIG_FS_ENCRYPTION))
eea2c05d9   Sascha Hauer   ubifs: Remove #if...
899
  		return -EOPNOTSUPP;
e021986ee   Richard Weinberger   ubifs: Implement ...
900
901
902
903
904
905
906
907
908
909
  	if (c->encrypted)
  		return 0;
  
  	if (c->ro_mount || c->ro_media)
  		return -EROFS;
  
  	if (c->fmt_version < 5) {
  		ubifs_err(c, "on-flash format version 5 is needed for encryption");
  		return -EINVAL;
  	}
e021986ee   Richard Weinberger   ubifs: Implement ...
910
911
912
913
914
  	sup->flags |= cpu_to_le32(UBIFS_FLG_ENCRYPTION);
  
  	err = ubifs_write_sb_node(c, sup);
  	if (!err)
  		c->encrypted = 1;
e021986ee   Richard Weinberger   ubifs: Implement ...
915
916
917
  
  	return err;
  }