Blame view

fs/ubifs/super.c 60.9 KB
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  /*
   * This file is part of UBIFS.
   *
   * Copyright (C) 2006-2008 Nokia Corporation.
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 as published by
   * the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   * more details.
   *
   * You should have received a copy of the GNU General Public License along with
   * this program; if not, write to the Free Software Foundation, Inc., 51
   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
   *
   * Authors: Artem Bityutskiy (Битюцкий Артём)
   *          Adrian Hunter
   */
  
  /*
   * This file implements UBIFS initialization and VFS superblock operations. Some
   * initialization stuff which is rather large and complex is placed at
   * corresponding subsystems, but most of it is here.
   */
  
  #include <linux/init.h>
  #include <linux/slab.h>
  #include <linux/module.h>
  #include <linux/ctype.h>
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
33
34
35
36
  #include <linux/kthread.h>
  #include <linux/parser.h>
  #include <linux/seq_file.h>
  #include <linux/mount.h>
4d61db4f8   Artem Bityutskiy   UBIFS: use nicer ...
37
  #include <linux/math64.h>
304d427cd   Artem Bityutskiy   UBIFS: fix file-s...
38
  #include <linux/writeback.h>
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
39
  #include "ubifs.h"
39ce81ce7   Artem Bityutskiy   UBIFS: do not pri...
40
41
42
43
44
  /*
   * Maximum amount of memory we may 'kmalloc()' without worrying that we are
   * allocating too much.
   */
  #define UBIFS_KMALLOC_OK (128*1024)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
45
  /* Slab cache for UBIFS inodes */
e996bfd42   Richard Weinberger   ubifs: Unexport u...
46
  static struct kmem_cache *ubifs_inode_slab;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
47
48
49
  
  /* UBIFS TNC shrinker description */
  static struct shrinker ubifs_shrinker_info = {
1ab6c4997   Dave Chinner   fs: convert fs sh...
50
51
  	.scan_objects = ubifs_shrink_scan,
  	.count_objects = ubifs_shrink_count,
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  	.seeks = DEFAULT_SEEKS,
  };
  
  /**
   * validate_inode - validate inode.
   * @c: UBIFS file-system description object
   * @inode: the inode to validate
   *
   * This is a helper function for 'ubifs_iget()' which validates various fields
   * of a newly built inode to make sure they contain sane values and prevent
   * possible vulnerabilities. Returns zero if the inode is all right and
   * a non-zero error code if not.
   */
  static int validate_inode(struct ubifs_info *c, const struct inode *inode)
  {
  	int err;
  	const struct ubifs_inode *ui = ubifs_inode(inode);
  
  	if (inode->i_size > c->max_inode_sz) {
235c362bd   Sheng Yong   UBIFS: extend deb...
71
  		ubifs_err(c, "inode is too large (%lld)",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
72
73
74
  			  (long long)inode->i_size);
  		return 1;
  	}
b793a8c88   hujianyang   UBIFS: remove use...
75
  	if (ui->compr_type >= UBIFS_COMPR_TYPES_CNT) {
235c362bd   Sheng Yong   UBIFS: extend deb...
76
  		ubifs_err(c, "unknown compression type %d", ui->compr_type);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
77
78
79
80
81
82
83
84
  		return 2;
  	}
  
  	if (ui->xattr_names + ui->xattr_cnt > XATTR_LIST_MAX)
  		return 3;
  
  	if (ui->data_len < 0 || ui->data_len > UBIFS_MAX_INO_DATA)
  		return 4;
a29fa9dfa   Artem Bityutskiy   UBIFS: minor clea...
85
  	if (ui->xattr && !S_ISREG(inode->i_mode))
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
86
87
88
  		return 5;
  
  	if (!ubifs_compr_present(ui->compr_type)) {
235c362bd   Sheng Yong   UBIFS: extend deb...
89
  		ubifs_warn(c, "inode %lu uses '%s' compression, but it was not compiled in",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
90
  			   inode->i_ino, ubifs_compr_name(ui->compr_type));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
91
  	}
1b51e9836   Artem Bityutskiy   UBIFS: rename dbg...
92
  	err = dbg_check_dir(c, inode);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
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
  	return err;
  }
  
  struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
  {
  	int err;
  	union ubifs_key key;
  	struct ubifs_ino_node *ino;
  	struct ubifs_info *c = sb->s_fs_info;
  	struct inode *inode;
  	struct ubifs_inode *ui;
  
  	dbg_gen("inode %lu", inum);
  
  	inode = iget_locked(sb, inum);
  	if (!inode)
  		return ERR_PTR(-ENOMEM);
  	if (!(inode->i_state & I_NEW))
  		return inode;
  	ui = ubifs_inode(inode);
  
  	ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
  	if (!ino) {
  		err = -ENOMEM;
  		goto out;
  	}
  
  	ino_key_init(c, &key, inode->i_ino);
  
  	err = ubifs_tnc_lookup(c, &key, ino);
  	if (err)
  		goto out_ino;
8c1c5f263   Dongsheng Yang   ubifs: introduce ...
125
126
127
128
  	inode->i_flags |= S_NOCMTIME;
  #ifndef CONFIG_UBIFS_ATIME_SUPPORT
  	inode->i_flags |= S_NOATIME;
  #endif
bfe868486   Miklos Szeredi   filesystems: add ...
129
  	set_nlink(inode, le32_to_cpu(ino->nlink));
39241beb7   Eric W. Biederman   userns: Convert u...
130
131
  	i_uid_write(inode, le32_to_cpu(ino->uid));
  	i_gid_write(inode, le32_to_cpu(ino->gid));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  	inode->i_atime.tv_sec  = (int64_t)le64_to_cpu(ino->atime_sec);
  	inode->i_atime.tv_nsec = le32_to_cpu(ino->atime_nsec);
  	inode->i_mtime.tv_sec  = (int64_t)le64_to_cpu(ino->mtime_sec);
  	inode->i_mtime.tv_nsec = le32_to_cpu(ino->mtime_nsec);
  	inode->i_ctime.tv_sec  = (int64_t)le64_to_cpu(ino->ctime_sec);
  	inode->i_ctime.tv_nsec = le32_to_cpu(ino->ctime_nsec);
  	inode->i_mode = le32_to_cpu(ino->mode);
  	inode->i_size = le64_to_cpu(ino->size);
  
  	ui->data_len    = le32_to_cpu(ino->data_len);
  	ui->flags       = le32_to_cpu(ino->flags);
  	ui->compr_type  = le16_to_cpu(ino->compr_type);
  	ui->creat_sqnum = le64_to_cpu(ino->creat_sqnum);
  	ui->xattr_cnt   = le32_to_cpu(ino->xattr_cnt);
  	ui->xattr_size  = le32_to_cpu(ino->xattr_size);
  	ui->xattr_names = le32_to_cpu(ino->xattr_names);
  	ui->synced_i_size = ui->ui_size = inode->i_size;
  
  	ui->xattr = (ui->flags & UBIFS_XATTR_FL) ? 1 : 0;
  
  	err = validate_inode(c, inode);
  	if (err)
  		goto out_invalid;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  	switch (inode->i_mode & S_IFMT) {
  	case S_IFREG:
  		inode->i_mapping->a_ops = &ubifs_file_address_operations;
  		inode->i_op = &ubifs_file_inode_operations;
  		inode->i_fop = &ubifs_file_operations;
  		if (ui->xattr) {
  			ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
  			if (!ui->data) {
  				err = -ENOMEM;
  				goto out_ino;
  			}
  			memcpy(ui->data, ino->data, ui->data_len);
  			((char *)ui->data)[ui->data_len] = '\0';
  		} else if (ui->data_len != 0) {
  			err = 10;
  			goto out_invalid;
  		}
  		break;
  	case S_IFDIR:
  		inode->i_op  = &ubifs_dir_inode_operations;
  		inode->i_fop = &ubifs_dir_operations;
  		if (ui->data_len != 0) {
  			err = 11;
  			goto out_invalid;
  		}
  		break;
  	case S_IFLNK:
  		inode->i_op = &ubifs_symlink_inode_operations;
  		if (ui->data_len <= 0 || ui->data_len > UBIFS_MAX_INO_DATA) {
  			err = 12;
  			goto out_invalid;
  		}
  		ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
  		if (!ui->data) {
  			err = -ENOMEM;
  			goto out_ino;
  		}
  		memcpy(ui->data, ino->data, ui->data_len);
  		((char *)ui->data)[ui->data_len] = '\0';
  		break;
  	case S_IFBLK:
  	case S_IFCHR:
  	{
  		dev_t rdev;
  		union ubifs_dev_desc *dev;
  
  		ui->data = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
  		if (!ui->data) {
  			err = -ENOMEM;
  			goto out_ino;
  		}
  
  		dev = (union ubifs_dev_desc *)ino->data;
  		if (ui->data_len == sizeof(dev->new))
  			rdev = new_decode_dev(le32_to_cpu(dev->new));
  		else if (ui->data_len == sizeof(dev->huge))
  			rdev = huge_decode_dev(le64_to_cpu(dev->huge));
  		else {
  			err = 13;
  			goto out_invalid;
  		}
  		memcpy(ui->data, ino->data, ui->data_len);
  		inode->i_op = &ubifs_file_inode_operations;
  		init_special_inode(inode, inode->i_mode, rdev);
  		break;
  	}
  	case S_IFSOCK:
  	case S_IFIFO:
  		inode->i_op = &ubifs_file_inode_operations;
  		init_special_inode(inode, inode->i_mode, 0);
  		if (ui->data_len != 0) {
  			err = 14;
  			goto out_invalid;
  		}
  		break;
  	default:
  		err = 15;
  		goto out_invalid;
  	}
  
  	kfree(ino);
  	ubifs_set_inode_flags(inode);
  	unlock_new_inode(inode);
  	return inode;
  
  out_invalid:
235c362bd   Sheng Yong   UBIFS: extend deb...
241
  	ubifs_err(c, "inode %lu validation failed, error %d", inode->i_ino, err);
edf6be245   Artem Bityutskiy   UBIFS: rename dum...
242
243
  	ubifs_dump_node(c, ino);
  	ubifs_dump_inode(c, inode);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
244
245
246
247
  	err = -EINVAL;
  out_ino:
  	kfree(ino);
  out:
235c362bd   Sheng Yong   UBIFS: extend deb...
248
  	ubifs_err(c, "failed to read inode %lu, error %d", inode->i_ino, err);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
  	iget_failed(inode);
  	return ERR_PTR(err);
  }
  
  static struct inode *ubifs_alloc_inode(struct super_block *sb)
  {
  	struct ubifs_inode *ui;
  
  	ui = kmem_cache_alloc(ubifs_inode_slab, GFP_NOFS);
  	if (!ui)
  		return NULL;
  
  	memset((void *)ui + sizeof(struct inode), 0,
  	       sizeof(struct ubifs_inode) - sizeof(struct inode));
  	mutex_init(&ui->ui_mutex);
  	spin_lock_init(&ui->ui_lock);
  	return &ui->vfs_inode;
  };
fa0d7e3de   Nick Piggin   fs: icache RCU fr...
267
268
269
270
  static void ubifs_i_callback(struct rcu_head *head)
  {
  	struct inode *inode = container_of(head, struct inode, i_rcu);
  	struct ubifs_inode *ui = ubifs_inode(inode);
fa0d7e3de   Nick Piggin   fs: icache RCU fr...
271
272
  	kmem_cache_free(ubifs_inode_slab, ui);
  }
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
273
274
275
276
277
  static void ubifs_destroy_inode(struct inode *inode)
  {
  	struct ubifs_inode *ui = ubifs_inode(inode);
  
  	kfree(ui->data);
fa0d7e3de   Nick Piggin   fs: icache RCU fr...
278
  	call_rcu(&inode->i_rcu, ubifs_i_callback);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
279
280
281
282
283
  }
  
  /*
   * Note, Linux write-back code calls this without 'i_mutex'.
   */
a9185b41a   Christoph Hellwig   pass writeback_co...
284
  static int ubifs_write_inode(struct inode *inode, struct writeback_control *wbc)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
285
  {
fbfa6c884   Artem Bityutskiy   UBIFS: do not wri...
286
  	int err = 0;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
287
288
289
290
291
292
293
294
295
296
  	struct ubifs_info *c = inode->i_sb->s_fs_info;
  	struct ubifs_inode *ui = ubifs_inode(inode);
  
  	ubifs_assert(!ui->xattr);
  	if (is_bad_inode(inode))
  		return 0;
  
  	mutex_lock(&ui->ui_mutex);
  	/*
  	 * Due to races between write-back forced by budgeting
5c57f20b8   Artem Bityutskiy   UBIFS: nuke pdflu...
297
  	 * (see 'sync_some_inodes()') and background write-back, the inode may
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
298
299
300
301
302
303
304
305
  	 * have already been synchronized, do not do this again. This might
  	 * also happen if it was synchronized in an VFS operation, e.g.
  	 * 'ubifs_link()'.
  	 */
  	if (!ui->dirty) {
  		mutex_unlock(&ui->ui_mutex);
  		return 0;
  	}
fbfa6c884   Artem Bityutskiy   UBIFS: do not wri...
306
307
308
309
310
311
312
  	/*
  	 * As an optimization, do not write orphan inodes to the media just
  	 * because this is not needed.
  	 */
  	dbg_gen("inode %lu, mode %#x, nlink %u",
  		inode->i_ino, (int)inode->i_mode, inode->i_nlink);
  	if (inode->i_nlink) {
1f28681ad   Artem Bityutskiy   UBIFS: remove unn...
313
  		err = ubifs_jnl_write_inode(c, inode);
fbfa6c884   Artem Bityutskiy   UBIFS: do not wri...
314
  		if (err)
235c362bd   Sheng Yong   UBIFS: extend deb...
315
  			ubifs_err(c, "can't write inode %lu, error %d",
fbfa6c884   Artem Bityutskiy   UBIFS: do not wri...
316
  				  inode->i_ino, err);
e3c3efc24   Artem Bityutskiy   UBIFS: add inode ...
317
318
  		else
  			err = dbg_check_inode_size(c, inode, ui->ui_size);
fbfa6c884   Artem Bityutskiy   UBIFS: do not wri...
319
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
320
321
322
323
324
325
  
  	ui->dirty = 0;
  	mutex_unlock(&ui->ui_mutex);
  	ubifs_release_dirty_inode_budget(c, ui);
  	return err;
  }
d640e1b50   Al Viro   switch ubifs to -...
326
  static void ubifs_evict_inode(struct inode *inode)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
327
328
329
  {
  	int err;
  	struct ubifs_info *c = inode->i_sb->s_fs_info;
1e0f358e2   Artem Bityutskiy   UBIFS: free budge...
330
  	struct ubifs_inode *ui = ubifs_inode(inode);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
331

1e0f358e2   Artem Bityutskiy   UBIFS: free budge...
332
  	if (ui->xattr)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
333
334
335
336
337
338
  		/*
  		 * Extended attribute inode deletions are fully handled in
  		 * 'ubifs_removexattr()'. These inodes are special and have
  		 * limited usage, so there is nothing to do here.
  		 */
  		goto out;
7d32c2bb1   Artem Bityutskiy   UBIFS: improve de...
339
  	dbg_gen("inode %lu, mode %#x", inode->i_ino, (int)inode->i_mode);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
340
  	ubifs_assert(!atomic_read(&inode->i_count));
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
341

91b0abe36   Johannes Weiner   mm + fs: store sh...
342
  	truncate_inode_pages_final(&inode->i_data);
d640e1b50   Al Viro   switch ubifs to -...
343
344
345
  
  	if (inode->i_nlink)
  		goto done;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
346
347
  	if (is_bad_inode(inode))
  		goto out;
1e0f358e2   Artem Bityutskiy   UBIFS: free budge...
348
  	ui->ui_size = inode->i_size = 0;
de94eb558   Artem Bityutskiy   UBIFS: optimize d...
349
  	err = ubifs_jnl_delete_inode(c, inode);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
350
351
352
  	if (err)
  		/*
  		 * Worst case we have a lost orphan inode wasting space, so a
0a883a05c   Artem Bityutskiy   UBIFS: few commen...
353
  		 * simple error message is OK here.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
354
  		 */
235c362bd   Sheng Yong   UBIFS: extend deb...
355
  		ubifs_err(c, "can't delete inode %lu, error %d",
de94eb558   Artem Bityutskiy   UBIFS: optimize d...
356
  			  inode->i_ino, err);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
357
  out:
1e0f358e2   Artem Bityutskiy   UBIFS: free budge...
358
359
  	if (ui->dirty)
  		ubifs_release_dirty_inode_budget(c, ui);
6d6cb0d68   Adrian Hunter   UBIFS: reset no_s...
360
361
  	else {
  		/* We've deleted something - clean the "no space" flags */
b137545c4   Artem Bityutskiy   UBIFS: introduce ...
362
  		c->bi.nospace = c->bi.nospace_rp = 0;
6d6cb0d68   Adrian Hunter   UBIFS: reset no_s...
363
364
  		smp_wmb();
  	}
d640e1b50   Al Viro   switch ubifs to -...
365
  done:
dbd5768f8   Jan Kara   vfs: Rename end_w...
366
  	clear_inode(inode);
d475a5074   Richard Weinberger   ubifs: Add skelet...
367
368
369
  #ifdef CONFIG_UBIFS_FS_ENCRYPTION
  	fscrypt_put_encryption_info(inode, NULL);
  #endif
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
370
  }
aa3857295   Christoph Hellwig   fs: pass exact ty...
371
  static void ubifs_dirty_inode(struct inode *inode, int flags)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
372
373
374
375
376
377
378
379
380
381
382
383
384
385
  {
  	struct ubifs_inode *ui = ubifs_inode(inode);
  
  	ubifs_assert(mutex_is_locked(&ui->ui_mutex));
  	if (!ui->dirty) {
  		ui->dirty = 1;
  		dbg_gen("inode %lu",  inode->i_ino);
  	}
  }
  
  static int ubifs_statfs(struct dentry *dentry, struct kstatfs *buf)
  {
  	struct ubifs_info *c = dentry->d_sb->s_fs_info;
  	unsigned long long free;
7c7cbadf7   Artem Bityutskiy   UBIFS: amend f_fsid
386
  	__le32 *uuid = (__le32 *)c->uuid;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
387

7dad181bb   Artem Bityutskiy   UBIFS: improve st...
388
  	free = ubifs_get_free_space(c);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
389
390
391
392
393
394
395
396
397
398
399
400
401
402
  	dbg_gen("free space %lld bytes (%lld blocks)",
  		free, free >> UBIFS_BLOCK_SHIFT);
  
  	buf->f_type = UBIFS_SUPER_MAGIC;
  	buf->f_bsize = UBIFS_BLOCK_SIZE;
  	buf->f_blocks = c->block_cnt;
  	buf->f_bfree = free >> UBIFS_BLOCK_SHIFT;
  	if (free > c->report_rp_size)
  		buf->f_bavail = (free - c->report_rp_size) >> UBIFS_BLOCK_SHIFT;
  	else
  		buf->f_bavail = 0;
  	buf->f_files = 0;
  	buf->f_ffree = 0;
  	buf->f_namelen = UBIFS_MAX_NLEN;
7c7cbadf7   Artem Bityutskiy   UBIFS: amend f_fsid
403
404
  	buf->f_fsid.val[0] = le32_to_cpu(uuid[0]) ^ le32_to_cpu(uuid[2]);
  	buf->f_fsid.val[1] = le32_to_cpu(uuid[1]) ^ le32_to_cpu(uuid[3]);
b4978e949   Artem Bityutskiy   UBIFS: always cle...
405
  	ubifs_assert(buf->f_bfree <= c->block_cnt);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
406
407
  	return 0;
  }
34c80b1d9   Al Viro   vfs: switch ->sho...
408
  static int ubifs_show_options(struct seq_file *s, struct dentry *root)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
409
  {
34c80b1d9   Al Viro   vfs: switch ->sho...
410
  	struct ubifs_info *c = root->d_sb->s_fs_info;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
411
412
  
  	if (c->mount_opts.unmount_mode == 2)
d4eb08ff0   Fabian Frederick   UBIFS: replace se...
413
  		seq_puts(s, ",fast_unmount");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
414
  	else if (c->mount_opts.unmount_mode == 1)
d4eb08ff0   Fabian Frederick   UBIFS: replace se...
415
  		seq_puts(s, ",norm_unmount");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
416

4793e7c5e   Adrian Hunter   UBIFS: add bulk-r...
417
  	if (c->mount_opts.bulk_read == 2)
d4eb08ff0   Fabian Frederick   UBIFS: replace se...
418
  		seq_puts(s, ",bulk_read");
4793e7c5e   Adrian Hunter   UBIFS: add bulk-r...
419
  	else if (c->mount_opts.bulk_read == 1)
d4eb08ff0   Fabian Frederick   UBIFS: replace se...
420
  		seq_puts(s, ",no_bulk_read");
4793e7c5e   Adrian Hunter   UBIFS: add bulk-r...
421

2953e73f1   Adrian Hunter   UBIFS: add no_chk...
422
  	if (c->mount_opts.chk_data_crc == 2)
d4eb08ff0   Fabian Frederick   UBIFS: replace se...
423
  		seq_puts(s, ",chk_data_crc");
2953e73f1   Adrian Hunter   UBIFS: add no_chk...
424
  	else if (c->mount_opts.chk_data_crc == 1)
d4eb08ff0   Fabian Frederick   UBIFS: replace se...
425
  		seq_puts(s, ",no_chk_data_crc");
2953e73f1   Adrian Hunter   UBIFS: add no_chk...
426

553dea4dd   Artem Bityutskiy   UBIFS: introduce ...
427
  	if (c->mount_opts.override_compr) {
fcabb3479   Hunter Adrian   UBIFS: fix compil...
428
429
  		seq_printf(s, ",compr=%s",
  			   ubifs_compr_name(c->mount_opts.compr_type));
553dea4dd   Artem Bityutskiy   UBIFS: introduce ...
430
  	}
319c10427   Rabin Vincent   ubifs: allow user...
431
  	seq_printf(s, ",ubi=%d,vol=%d", c->vi.ubi_num, c->vi.vol_id);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
432
433
434
435
436
  	return 0;
  }
  
  static int ubifs_sync_fs(struct super_block *sb, int wait)
  {
f10383006   Artem Bityutskiy   UBIFS: always com...
437
  	int i, err;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
438
  	struct ubifs_info *c = sb->s_fs_info;
304d427cd   Artem Bityutskiy   UBIFS: fix file-s...
439

e8ea17591   Artem Bityutskiy   UBIFS: do not use...
440
  	/*
dedb0d48a   Artem Bityutskiy   UBIFS: do not com...
441
442
  	 * Zero @wait is just an advisory thing to help the file system shove
  	 * lots of data into the queues, and there will be the second
e8ea17591   Artem Bityutskiy   UBIFS: do not use...
443
444
  	 * '->sync_fs()' call, with non-zero @wait.
  	 */
dedb0d48a   Artem Bityutskiy   UBIFS: do not com...
445
446
  	if (!wait)
  		return 0;
e8ea17591   Artem Bityutskiy   UBIFS: do not use...
447

f10383006   Artem Bityutskiy   UBIFS: always com...
448
  	/*
3eb14297c   Adrian Hunter   UBIFS: sync wbufs...
449
450
451
452
453
454
455
456
  	 * Synchronize write buffers, because 'ubifs_run_commit()' does not
  	 * do this if it waits for an already running commit.
  	 */
  	for (i = 0; i < c->jhead_cnt; i++) {
  		err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
  		if (err)
  			return err;
  	}
887ee1711   Artem Bityutskiy   UBIFS: remove unn...
457
458
459
460
461
462
463
  	/*
  	 * Strictly speaking, it is not necessary to commit the journal here,
  	 * synchronizing write-buffers would be enough. But committing makes
  	 * UBIFS free space predictions much more accurate, so we want to let
  	 * the user be able to get more accurate results of 'statfs()' after
  	 * they synchronize the file system.
  	 */
f10383006   Artem Bityutskiy   UBIFS: always com...
464
465
466
  	err = ubifs_run_commit(c);
  	if (err)
  		return err;
403e12ab3   Artem Bityutskiy   UBIFS: commit on ...
467

cb5c6a2b2   Artem Bityutskiy   UBIFS: use ubi_sync
468
  	return ubi_sync(c->vi.ubi_num);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
469
470
471
472
473
474
475
476
477
478
479
480
481
482
  }
  
  /**
   * init_constants_early - initialize UBIFS constants.
   * @c: UBIFS file-system description object
   *
   * This function initialize UBIFS constants which do not need the superblock to
   * be read. It also checks that the UBI volume satisfies basic UBIFS
   * requirements. Returns zero in case of success and a negative error code in
   * case of failure.
   */
  static int init_constants_early(struct ubifs_info *c)
  {
  	if (c->vi.corrupted) {
235c362bd   Sheng Yong   UBIFS: extend deb...
483
  		ubifs_warn(c, "UBI volume is corrupted - read-only mode");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
484
485
486
487
  		c->ro_media = 1;
  	}
  
  	if (c->di.ro_mode) {
235c362bd   Sheng Yong   UBIFS: extend deb...
488
  		ubifs_msg(c, "read-only UBI device");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
489
490
491
492
  		c->ro_media = 1;
  	}
  
  	if (c->vi.vol_type == UBI_STATIC_VOLUME) {
235c362bd   Sheng Yong   UBIFS: extend deb...
493
  		ubifs_msg(c, "static UBI volume - read-only mode");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
494
495
496
497
498
  		c->ro_media = 1;
  	}
  
  	c->leb_cnt = c->vi.size;
  	c->leb_size = c->vi.usable_leb_size;
ca2ec61d1   Artem Bityutskiy   UBI: incorporate ...
499
  	c->leb_start = c->di.leb_start;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
500
501
502
  	c->half_leb_size = c->leb_size / 2;
  	c->min_io_size = c->di.min_io_size;
  	c->min_io_shift = fls(c->min_io_size) - 1;
3e8e2e0c8   Artem Bityutskiy   UBIFS: incorporat...
503
504
  	c->max_write_size = c->di.max_write_size;
  	c->max_write_shift = fls(c->max_write_size) - 1;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
505
506
  
  	if (c->leb_size < UBIFS_MIN_LEB_SZ) {
dccbc9197   Daniel Golle   ubifs: Silence ea...
507
508
  		ubifs_errc(c, "too small LEBs (%d bytes), min. is %d bytes",
  			   c->leb_size, UBIFS_MIN_LEB_SZ);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
509
510
511
512
  		return -EINVAL;
  	}
  
  	if (c->leb_cnt < UBIFS_MIN_LEB_CNT) {
dccbc9197   Daniel Golle   ubifs: Silence ea...
513
514
  		ubifs_errc(c, "too few LEBs (%d), min. is %d",
  			   c->leb_cnt, UBIFS_MIN_LEB_CNT);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
515
516
517
518
  		return -EINVAL;
  	}
  
  	if (!is_power_of_2(c->min_io_size)) {
dccbc9197   Daniel Golle   ubifs: Silence ea...
519
  		ubifs_errc(c, "bad min. I/O size %d", c->min_io_size);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
520
521
522
523
  		return -EINVAL;
  	}
  
  	/*
3e8e2e0c8   Artem Bityutskiy   UBIFS: incorporat...
524
525
526
527
528
529
  	 * Maximum write size has to be greater or equivalent to min. I/O
  	 * size, and be multiple of min. I/O size.
  	 */
  	if (c->max_write_size < c->min_io_size ||
  	    c->max_write_size % c->min_io_size ||
  	    !is_power_of_2(c->max_write_size)) {
dccbc9197   Daniel Golle   ubifs: Silence ea...
530
531
  		ubifs_errc(c, "bad write buffer size %d for %d min. I/O unit",
  			   c->max_write_size, c->min_io_size);
3e8e2e0c8   Artem Bityutskiy   UBIFS: incorporat...
532
533
534
535
  		return -EINVAL;
  	}
  
  	/*
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
536
537
538
539
540
541
542
  	 * UBIFS aligns all node to 8-byte boundary, so to make function in
  	 * io.c simpler, assume minimum I/O unit size to be 8 bytes if it is
  	 * less than 8.
  	 */
  	if (c->min_io_size < 8) {
  		c->min_io_size = 8;
  		c->min_io_shift = 3;
3e8e2e0c8   Artem Bityutskiy   UBIFS: incorporat...
543
544
545
546
  		if (c->max_write_size < c->min_io_size) {
  			c->max_write_size = c->min_io_size;
  			c->max_write_shift = c->min_io_shift;
  		}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
  	}
  
  	c->ref_node_alsz = ALIGN(UBIFS_REF_NODE_SZ, c->min_io_size);
  	c->mst_node_alsz = ALIGN(UBIFS_MST_NODE_SZ, c->min_io_size);
  
  	/*
  	 * Initialize node length ranges which are mostly needed for node
  	 * length validation.
  	 */
  	c->ranges[UBIFS_PAD_NODE].len  = UBIFS_PAD_NODE_SZ;
  	c->ranges[UBIFS_SB_NODE].len   = UBIFS_SB_NODE_SZ;
  	c->ranges[UBIFS_MST_NODE].len  = UBIFS_MST_NODE_SZ;
  	c->ranges[UBIFS_REF_NODE].len  = UBIFS_REF_NODE_SZ;
  	c->ranges[UBIFS_TRUN_NODE].len = UBIFS_TRUN_NODE_SZ;
  	c->ranges[UBIFS_CS_NODE].len   = UBIFS_CS_NODE_SZ;
  
  	c->ranges[UBIFS_INO_NODE].min_len  = UBIFS_INO_NODE_SZ;
  	c->ranges[UBIFS_INO_NODE].max_len  = UBIFS_MAX_INO_NODE_SZ;
  	c->ranges[UBIFS_ORPH_NODE].min_len =
  				UBIFS_ORPH_NODE_SZ + sizeof(__le64);
  	c->ranges[UBIFS_ORPH_NODE].max_len = c->leb_size;
  	c->ranges[UBIFS_DENT_NODE].min_len = UBIFS_DENT_NODE_SZ;
  	c->ranges[UBIFS_DENT_NODE].max_len = UBIFS_MAX_DENT_NODE_SZ;
  	c->ranges[UBIFS_XENT_NODE].min_len = UBIFS_XENT_NODE_SZ;
  	c->ranges[UBIFS_XENT_NODE].max_len = UBIFS_MAX_XENT_NODE_SZ;
  	c->ranges[UBIFS_DATA_NODE].min_len = UBIFS_DATA_NODE_SZ;
  	c->ranges[UBIFS_DATA_NODE].max_len = UBIFS_MAX_DATA_NODE_SZ;
  	/*
  	 * Minimum indexing node size is amended later when superblock is
  	 * read and the key length is known.
  	 */
  	c->ranges[UBIFS_IDX_NODE].min_len = UBIFS_IDX_NODE_SZ + UBIFS_BRANCH_SZ;
  	/*
  	 * Maximum indexing node size is amended later when superblock is
  	 * read and the fanout is known.
  	 */
  	c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX;
  
  	/*
7078202e5   Artem Bityutskiy   UBIFS: document d...
586
587
  	 * Initialize dead and dark LEB space watermarks. See gc.c for comments
  	 * about these values.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
588
589
590
  	 */
  	c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size);
  	c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
9bbb5726e   Artem Bityutskiy   UBIFS: introduce ...
591
592
593
594
595
596
  	/*
  	 * Calculate how many bytes would be wasted at the end of LEB if it was
  	 * fully filled with data nodes of maximum size. This is used in
  	 * calculations when reporting free space.
  	 */
  	c->leb_overhead = c->leb_size % UBIFS_MAX_DATA_NODE_SZ;
39ce81ce7   Artem Bityutskiy   UBIFS: do not pri...
597

4793e7c5e   Adrian Hunter   UBIFS: add bulk-r...
598
  	/* Buffer size for bulk-reads */
6c0c42cdf   Artem Bityutskiy   UBIFS: do not all...
599
600
601
  	c->max_bu_buf_len = UBIFS_MAX_BULK_READ * UBIFS_MAX_DATA_NODE_SZ;
  	if (c->max_bu_buf_len > c->leb_size)
  		c->max_bu_buf_len = c->leb_size;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
  	return 0;
  }
  
  /**
   * bud_wbuf_callback - bud LEB write-buffer synchronization call-back.
   * @c: UBIFS file-system description object
   * @lnum: LEB the write-buffer was synchronized to
   * @free: how many free bytes left in this LEB
   * @pad: how many bytes were padded
   *
   * This is a callback function which is called by the I/O unit when the
   * write-buffer is synchronized. We need this to correctly maintain space
   * accounting in bud logical eraseblocks. This function returns zero in case of
   * success and a negative error code in case of failure.
   *
   * This function actually belongs to the journal, but we keep it here because
   * we want to keep it static.
   */
  static int bud_wbuf_callback(struct ubifs_info *c, int lnum, int free, int pad)
  {
  	return ubifs_update_one_lp(c, lnum, free, pad, 0, 0);
  }
  
  /*
79807d075   Artem Bityutskiy   UBIFS: fix consta...
626
   * init_constants_sb - initialize UBIFS constants.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
627
628
629
630
631
632
633
   * @c: UBIFS file-system description object
   *
   * This is a helper function which initializes various UBIFS constants after
   * the superblock has been read. It also checks various UBIFS parameters and
   * makes sure they are all right. Returns zero in case of success and a
   * negative error code in case of failure.
   */
79807d075   Artem Bityutskiy   UBIFS: fix consta...
634
  static int init_constants_sb(struct ubifs_info *c)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
635
636
  {
  	int tmp, err;
4d61db4f8   Artem Bityutskiy   UBIFS: use nicer ...
637
  	long long tmp64;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
  
  	c->main_bytes = (long long)c->main_lebs * c->leb_size;
  	c->max_znode_sz = sizeof(struct ubifs_znode) +
  				c->fanout * sizeof(struct ubifs_zbranch);
  
  	tmp = ubifs_idx_node_sz(c, 1);
  	c->ranges[UBIFS_IDX_NODE].min_len = tmp;
  	c->min_idx_node_sz = ALIGN(tmp, 8);
  
  	tmp = ubifs_idx_node_sz(c, c->fanout);
  	c->ranges[UBIFS_IDX_NODE].max_len = tmp;
  	c->max_idx_node_sz = ALIGN(tmp, 8);
  
  	/* Make sure LEB size is large enough to fit full commit */
  	tmp = UBIFS_CS_NODE_SZ + UBIFS_REF_NODE_SZ * c->jhead_cnt;
  	tmp = ALIGN(tmp, c->min_io_size);
  	if (tmp > c->leb_size) {
235c362bd   Sheng Yong   UBIFS: extend deb...
655
  		ubifs_err(c, "too small LEB size %d, at least %d needed",
a6aae4dd0   Artem Bityutskiy   UBIFS: get rid of...
656
  			  c->leb_size, tmp);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
657
658
659
660
661
662
663
  		return -EINVAL;
  	}
  
  	/*
  	 * Make sure that the log is large enough to fit reference nodes for
  	 * all buds plus one reserved LEB.
  	 */
4d61db4f8   Artem Bityutskiy   UBIFS: use nicer ...
664
665
  	tmp64 = c->max_bud_bytes + c->leb_size - 1;
  	c->max_bud_cnt = div_u64(tmp64, c->leb_size);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
666
667
668
669
  	tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1);
  	tmp /= c->leb_size;
  	tmp += 1;
  	if (c->log_lebs < tmp) {
235c362bd   Sheng Yong   UBIFS: extend deb...
670
  		ubifs_err(c, "too small log %d LEBs, required min. %d LEBs",
a6aae4dd0   Artem Bityutskiy   UBIFS: get rid of...
671
  			  c->log_lebs, tmp);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
672
673
674
675
676
677
678
679
  		return -EINVAL;
  	}
  
  	/*
  	 * When budgeting we assume worst-case scenarios when the pages are not
  	 * be compressed and direntries are of the maximum size.
  	 *
  	 * Note, data, which may be stored in inodes is budgeted separately, so
b137545c4   Artem Bityutskiy   UBIFS: introduce ...
680
  	 * it is not included into 'c->bi.inode_budget'.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
681
  	 */
b137545c4   Artem Bityutskiy   UBIFS: introduce ...
682
683
684
  	c->bi.page_budget = UBIFS_MAX_DATA_NODE_SZ * UBIFS_BLOCKS_PER_PAGE;
  	c->bi.inode_budget = UBIFS_INO_NODE_SZ;
  	c->bi.dent_budget = UBIFS_MAX_DENT_NODE_SZ;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
  
  	/*
  	 * When the amount of flash space used by buds becomes
  	 * 'c->max_bud_bytes', UBIFS just blocks all writers and starts commit.
  	 * The writers are unblocked when the commit is finished. To avoid
  	 * writers to be blocked UBIFS initiates background commit in advance,
  	 * when number of bud bytes becomes above the limit defined below.
  	 */
  	c->bg_bud_bytes = (c->max_bud_bytes * 13) >> 4;
  
  	/*
  	 * Ensure minimum journal size. All the bytes in the journal heads are
  	 * considered to be used, when calculating the current journal usage.
  	 * Consequently, if the journal is too small, UBIFS will treat it as
  	 * always full.
  	 */
4d61db4f8   Artem Bityutskiy   UBIFS: use nicer ...
701
  	tmp64 = (long long)(c->jhead_cnt + 1) * c->leb_size + 1;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
702
703
704
705
706
707
708
709
  	if (c->bg_bud_bytes < tmp64)
  		c->bg_bud_bytes = tmp64;
  	if (c->max_bud_bytes < tmp64 + c->leb_size)
  		c->max_bud_bytes = tmp64 + c->leb_size;
  
  	err = ubifs_calc_lpt_geom(c);
  	if (err)
  		return err;
fb1cd01a3   Artem Bityutskiy   UBIFS: introduce ...
710
711
  	/* Initialize effective LEB size used in budgeting calculations */
  	c->idx_leb_size = c->leb_size - c->max_idx_node_sz;
79807d075   Artem Bityutskiy   UBIFS: fix consta...
712
713
714
715
716
717
718
719
720
721
722
723
724
725
  	return 0;
  }
  
  /*
   * init_constants_master - initialize UBIFS constants.
   * @c: UBIFS file-system description object
   *
   * This is a helper function which initializes various UBIFS constants after
   * the master node has been read. It also checks various UBIFS parameters and
   * makes sure they are all right.
   */
  static void init_constants_master(struct ubifs_info *c)
  {
  	long long tmp64;
b137545c4   Artem Bityutskiy   UBIFS: introduce ...
726
  	c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
fb1cd01a3   Artem Bityutskiy   UBIFS: introduce ...
727
  	c->report_rp_size = ubifs_reported_space(c, c->rp_size);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
728
729
730
731
732
733
  
  	/*
  	 * Calculate total amount of FS blocks. This number is not used
  	 * internally because it does not make much sense for UBIFS, but it is
  	 * necessary to report something for the 'statfs()' call.
  	 *
7dad181bb   Artem Bityutskiy   UBIFS: improve st...
734
  	 * Subtract the LEB reserved for GC, the LEB which is reserved for
af14a1ad7   Artem Bityutskiy   UBIFS: fix availa...
735
736
  	 * deletions, minimum LEBs for the index, and assume only one journal
  	 * head is available.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
737
  	 */
af14a1ad7   Artem Bityutskiy   UBIFS: fix availa...
738
  	tmp64 = c->main_lebs - 1 - 1 - MIN_INDEX_LEBS - c->jhead_cnt + 1;
4d61db4f8   Artem Bityutskiy   UBIFS: use nicer ...
739
  	tmp64 *= (long long)c->leb_size - c->leb_overhead;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
740
741
  	tmp64 = ubifs_reported_space(c, tmp64);
  	c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
742
743
744
745
746
747
  }
  
  /**
   * take_gc_lnum - reserve GC LEB.
   * @c: UBIFS file-system description object
   *
b4978e949   Artem Bityutskiy   UBIFS: always cle...
748
749
750
751
752
753
   * This function ensures that the LEB reserved for garbage collection is marked
   * as "taken" in lprops. We also have to set free space to LEB size and dirty
   * space to zero, because lprops may contain out-of-date information if the
   * file-system was un-mounted before it has been committed. This function
   * returns zero in case of success and a negative error code in case of
   * failure.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
754
755
756
757
758
759
   */
  static int take_gc_lnum(struct ubifs_info *c)
  {
  	int err;
  
  	if (c->gc_lnum == -1) {
235c362bd   Sheng Yong   UBIFS: extend deb...
760
  		ubifs_err(c, "no LEB for GC");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
761
762
  		return -EINVAL;
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
  	/* And we have to tell lprops that this LEB is taken */
  	err = ubifs_change_one_lp(c, c->gc_lnum, c->leb_size, 0,
  				  LPROPS_TAKEN, 0, 0);
  	return err;
  }
  
  /**
   * alloc_wbufs - allocate write-buffers.
   * @c: UBIFS file-system description object
   *
   * This helper function allocates and initializes UBIFS write-buffers. Returns
   * zero in case of success and %-ENOMEM in case of failure.
   */
  static int alloc_wbufs(struct ubifs_info *c)
  {
  	int i, err;
86b4c14de   Fabian Frederick   UBIFS: replace co...
779
780
  	c->jheads = kcalloc(c->jhead_cnt, sizeof(struct ubifs_jhead),
  			    GFP_KERNEL);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
781
782
783
784
785
786
787
788
789
790
791
792
  	if (!c->jheads)
  		return -ENOMEM;
  
  	/* Initialize journal heads */
  	for (i = 0; i < c->jhead_cnt; i++) {
  		INIT_LIST_HEAD(&c->jheads[i].buds_list);
  		err = ubifs_wbuf_init(c, &c->jheads[i].wbuf);
  		if (err)
  			return err;
  
  		c->jheads[i].wbuf.sync_callback = &bud_wbuf_callback;
  		c->jheads[i].wbuf.jhead = i;
1a0b06997   Artem Bityutskiy   UBIFS: introduce ...
793
  		c->jheads[i].grouped = 1;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
794
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
795
  	/*
441562673   Artem Bityutskiy   UBI: amend commen...
796
797
  	 * Garbage Collector head does not need to be synchronized by timer.
  	 * Also GC head nodes are not grouped.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
798
  	 */
0b335b9d7   Artem Bityutskiy   UBIFS: slightly o...
799
  	c->jheads[GCHD].wbuf.no_timer = 1;
1a0b06997   Artem Bityutskiy   UBIFS: introduce ...
800
  	c->jheads[GCHD].grouped = 0;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
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
  
  	return 0;
  }
  
  /**
   * free_wbufs - free write-buffers.
   * @c: UBIFS file-system description object
   */
  static void free_wbufs(struct ubifs_info *c)
  {
  	int i;
  
  	if (c->jheads) {
  		for (i = 0; i < c->jhead_cnt; i++) {
  			kfree(c->jheads[i].wbuf.buf);
  			kfree(c->jheads[i].wbuf.inodes);
  		}
  		kfree(c->jheads);
  		c->jheads = NULL;
  	}
  }
  
  /**
   * free_orphans - free orphans.
   * @c: UBIFS file-system description object
   */
  static void free_orphans(struct ubifs_info *c)
  {
  	struct ubifs_orphan *orph;
  
  	while (c->orph_dnext) {
  		orph = c->orph_dnext;
  		c->orph_dnext = orph->dnext;
  		list_del(&orph->list);
  		kfree(orph);
  	}
  
  	while (!list_empty(&c->orph_list)) {
  		orph = list_entry(c->orph_list.next, struct ubifs_orphan, list);
  		list_del(&orph->list);
  		kfree(orph);
235c362bd   Sheng Yong   UBIFS: extend deb...
842
  		ubifs_err(c, "orphan list not empty at unmount");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
843
844
845
846
847
848
849
850
851
852
853
854
  	}
  
  	vfree(c->orph_buf);
  	c->orph_buf = NULL;
  }
  
  /**
   * free_buds - free per-bud objects.
   * @c: UBIFS file-system description object
   */
  static void free_buds(struct ubifs_info *c)
  {
bb25e49ff   Cody P Schafer   fs/ubifs: use rbt...
855
856
857
858
  	struct ubifs_bud *bud, *n;
  
  	rbtree_postorder_for_each_entry_safe(bud, n, &c->buds, rb)
  		kfree(bud);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
  }
  
  /**
   * check_volume_empty - check if the UBI volume is empty.
   * @c: UBIFS file-system description object
   *
   * This function checks if the UBIFS volume is empty by looking if its LEBs are
   * mapped or not. The result of checking is stored in the @c->empty variable.
   * Returns zero in case of success and a negative error code in case of
   * failure.
   */
  static int check_volume_empty(struct ubifs_info *c)
  {
  	int lnum, err;
  
  	c->empty = 1;
  	for (lnum = 0; lnum < c->leb_cnt; lnum++) {
d3b2578f5   Artem Bityutskiy   UBIFS: switch to ...
876
  		err = ubifs_is_mapped(c, lnum);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
  		if (unlikely(err < 0))
  			return err;
  		if (err == 1) {
  			c->empty = 0;
  			break;
  		}
  
  		cond_resched();
  	}
  
  	return 0;
  }
  
  /*
   * UBIFS mount options.
   *
   * Opt_fast_unmount: do not run a journal commit before un-mounting
   * Opt_norm_unmount: run a journal commit before un-mounting
4793e7c5e   Adrian Hunter   UBIFS: add bulk-r...
895
896
   * Opt_bulk_read: enable bulk-reads
   * Opt_no_bulk_read: disable bulk-reads
2953e73f1   Adrian Hunter   UBIFS: add no_chk...
897
898
   * Opt_chk_data_crc: check CRCs when reading data nodes
   * Opt_no_chk_data_crc: do not check CRCs when reading data nodes
553dea4dd   Artem Bityutskiy   UBIFS: introduce ...
899
   * Opt_override_compr: override default compressor
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
900
901
902
903
904
   * Opt_err: just end of array marker
   */
  enum {
  	Opt_fast_unmount,
  	Opt_norm_unmount,
4793e7c5e   Adrian Hunter   UBIFS: add bulk-r...
905
906
  	Opt_bulk_read,
  	Opt_no_bulk_read,
2953e73f1   Adrian Hunter   UBIFS: add no_chk...
907
908
  	Opt_chk_data_crc,
  	Opt_no_chk_data_crc,
553dea4dd   Artem Bityutskiy   UBIFS: introduce ...
909
  	Opt_override_compr,
319c10427   Rabin Vincent   ubifs: allow user...
910
  	Opt_ignore,
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
911
912
  	Opt_err,
  };
a447c0932   Steven Whitehouse   vfs: Use const fo...
913
  static const match_table_t tokens = {
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
914
915
  	{Opt_fast_unmount, "fast_unmount"},
  	{Opt_norm_unmount, "norm_unmount"},
4793e7c5e   Adrian Hunter   UBIFS: add bulk-r...
916
917
  	{Opt_bulk_read, "bulk_read"},
  	{Opt_no_bulk_read, "no_bulk_read"},
2953e73f1   Adrian Hunter   UBIFS: add no_chk...
918
919
  	{Opt_chk_data_crc, "chk_data_crc"},
  	{Opt_no_chk_data_crc, "no_chk_data_crc"},
553dea4dd   Artem Bityutskiy   UBIFS: introduce ...
920
  	{Opt_override_compr, "compr=%s"},
319c10427   Rabin Vincent   ubifs: allow user...
921
922
  	{Opt_ignore, "ubi=%s"},
  	{Opt_ignore, "vol=%s"},
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
923
924
925
926
  	{Opt_err, NULL},
  };
  
  /**
8379ea31e   Artem Bityutskiy   UBIFS: allow sync...
927
928
929
930
931
932
933
934
935
936
937
938
939
940
   * parse_standard_option - parse a standard mount option.
   * @option: the option to parse
   *
   * Normally, standard mount options like "sync" are passed to file-systems as
   * flags. However, when a "rootflags=" kernel boot parameter is used, they may
   * be present in the options string. This function tries to deal with this
   * situation and parse standard options. Returns 0 if the option was not
   * recognized, and the corresponding integer flag if it was.
   *
   * UBIFS is only interested in the "sync" option, so do not check for anything
   * else.
   */
  static int parse_standard_option(const char *option)
  {
235c362bd   Sheng Yong   UBIFS: extend deb...
941
942
943
  
  	pr_notice("UBIFS: parse %s
  ", option);
8379ea31e   Artem Bityutskiy   UBIFS: allow sync...
944
945
946
947
948
949
  	if (!strcmp(option, "sync"))
  		return MS_SYNCHRONOUS;
  	return 0;
  }
  
  /**
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
   * ubifs_parse_options - parse mount parameters.
   * @c: UBIFS file-system description object
   * @options: parameters to parse
   * @is_remount: non-zero if this is FS re-mount
   *
   * This function parses UBIFS mount options and returns zero in case success
   * and a negative error code in case of failure.
   */
  static int ubifs_parse_options(struct ubifs_info *c, char *options,
  			       int is_remount)
  {
  	char *p;
  	substring_t args[MAX_OPT_ARGS];
  
  	if (!options)
  		return 0;
  
  	while ((p = strsep(&options, ","))) {
  		int token;
  
  		if (!*p)
  			continue;
  
  		token = match_token(p, tokens, args);
  		switch (token) {
27ad27993   Artem Bityutskiy   UBIFS: remove fas...
975
976
  		/*
  		 * %Opt_fast_unmount and %Opt_norm_unmount options are ignored.
cb54ef8b1   Artem Bityutskiy   UBIFS: few spelli...
977
  		 * We accept them in order to be backward-compatible. But this
27ad27993   Artem Bityutskiy   UBIFS: remove fas...
978
979
  		 * should be removed at some point.
  		 */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
980
981
  		case Opt_fast_unmount:
  			c->mount_opts.unmount_mode = 2;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
982
983
984
  			break;
  		case Opt_norm_unmount:
  			c->mount_opts.unmount_mode = 1;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
985
  			break;
4793e7c5e   Adrian Hunter   UBIFS: add bulk-r...
986
987
988
989
990
991
992
993
  		case Opt_bulk_read:
  			c->mount_opts.bulk_read = 2;
  			c->bulk_read = 1;
  			break;
  		case Opt_no_bulk_read:
  			c->mount_opts.bulk_read = 1;
  			c->bulk_read = 0;
  			break;
2953e73f1   Adrian Hunter   UBIFS: add no_chk...
994
995
996
997
998
999
1000
1001
  		case Opt_chk_data_crc:
  			c->mount_opts.chk_data_crc = 2;
  			c->no_chk_data_crc = 0;
  			break;
  		case Opt_no_chk_data_crc:
  			c->mount_opts.chk_data_crc = 1;
  			c->no_chk_data_crc = 1;
  			break;
553dea4dd   Artem Bityutskiy   UBIFS: introduce ...
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
  		case Opt_override_compr:
  		{
  			char *name = match_strdup(&args[0]);
  
  			if (!name)
  				return -ENOMEM;
  			if (!strcmp(name, "none"))
  				c->mount_opts.compr_type = UBIFS_COMPR_NONE;
  			else if (!strcmp(name, "lzo"))
  				c->mount_opts.compr_type = UBIFS_COMPR_LZO;
  			else if (!strcmp(name, "zlib"))
  				c->mount_opts.compr_type = UBIFS_COMPR_ZLIB;
  			else {
235c362bd   Sheng Yong   UBIFS: extend deb...
1015
  				ubifs_err(c, "unknown compressor \"%s\"", name); //FIXME: is c ready?
553dea4dd   Artem Bityutskiy   UBIFS: introduce ...
1016
1017
1018
1019
1020
1021
1022
1023
  				kfree(name);
  				return -EINVAL;
  			}
  			kfree(name);
  			c->mount_opts.override_compr = 1;
  			c->default_compr = c->mount_opts.compr_type;
  			break;
  		}
319c10427   Rabin Vincent   ubifs: allow user...
1024
1025
  		case Opt_ignore:
  			break;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1026
  		default:
8379ea31e   Artem Bityutskiy   UBIFS: allow sync...
1027
1028
1029
1030
1031
1032
  		{
  			unsigned long flag;
  			struct super_block *sb = c->vfs_sb;
  
  			flag = parse_standard_option(p);
  			if (!flag) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1033
  				ubifs_err(c, "unrecognized mount option \"%s\" or missing value",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
1034
  					  p);
8379ea31e   Artem Bityutskiy   UBIFS: allow sync...
1035
1036
1037
1038
1039
  				return -EINVAL;
  			}
  			sb->s_flags |= flag;
  			break;
  		}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
  		}
  	}
  
  	return 0;
  }
  
  /**
   * destroy_journal - destroy journal data structures.
   * @c: UBIFS file-system description object
   *
   * This function destroys journal data structures including those that may have
   * been created by recovery functions.
   */
  static void destroy_journal(struct ubifs_info *c)
  {
  	while (!list_empty(&c->unclean_leb_list)) {
  		struct ubifs_unclean_leb *ucleb;
  
  		ucleb = list_entry(c->unclean_leb_list.next,
  				   struct ubifs_unclean_leb, list);
  		list_del(&ucleb->list);
  		kfree(ucleb);
  	}
  	while (!list_empty(&c->old_buds)) {
  		struct ubifs_bud *bud;
  
  		bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
  		list_del(&bud->list);
  		kfree(bud);
  	}
  	ubifs_destroy_idx_gc(c);
  	ubifs_destroy_size_tree(c);
  	ubifs_tnc_close(c);
  	free_buds(c);
  }
  
  /**
3477d2046   Artem Bityutskiy   UBIFS: pre-alloca...
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
   * bu_init - initialize bulk-read information.
   * @c: UBIFS file-system description object
   */
  static void bu_init(struct ubifs_info *c)
  {
  	ubifs_assert(c->bulk_read == 1);
  
  	if (c->bu.buf)
  		return; /* Already initialized */
  
  again:
  	c->bu.buf = kmalloc(c->max_bu_buf_len, GFP_KERNEL | __GFP_NOWARN);
  	if (!c->bu.buf) {
  		if (c->max_bu_buf_len > UBIFS_KMALLOC_OK) {
  			c->max_bu_buf_len = UBIFS_KMALLOC_OK;
  			goto again;
  		}
  
  		/* Just disable bulk-read */
235c362bd   Sheng Yong   UBIFS: extend deb...
1096
  		ubifs_warn(c, "cannot allocate %d bytes of memory for bulk-read, disabling it",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
1097
  			   c->max_bu_buf_len);
3477d2046   Artem Bityutskiy   UBIFS: pre-alloca...
1098
1099
1100
1101
1102
1103
1104
  		c->mount_opts.bulk_read = 1;
  		c->bulk_read = 0;
  		return;
  	}
  }
  
  /**
57a450e95   Artem Bityutskiy   UBIFS: allow moun...
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
   * check_free_space - check if there is enough free space to mount.
   * @c: UBIFS file-system description object
   *
   * This function makes sure UBIFS has enough free space to be mounted in
   * read/write mode. UBIFS must always have some free space to allow deletions.
   */
  static int check_free_space(struct ubifs_info *c)
  {
  	ubifs_assert(c->dark_wm > 0);
  	if (c->lst.total_free + c->lst.total_dirty < c->dark_wm) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1115
  		ubifs_err(c, "insufficient free space to mount in R/W mode");
edf6be245   Artem Bityutskiy   UBIFS: rename dum...
1116
1117
  		ubifs_dump_budg(c, &c->bi);
  		ubifs_dump_lprops(c);
a2b9df3ff   Artem Bityutskiy   UBIFS: return sen...
1118
  		return -ENOSPC;
57a450e95   Artem Bityutskiy   UBIFS: allow moun...
1119
1120
1121
1122
1123
  	}
  	return 0;
  }
  
  /**
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1124
1125
1126
1127
1128
   * mount_ubifs - mount UBIFS file-system.
   * @c: UBIFS file-system description object
   *
   * This function mounts UBIFS file system. Returns zero in case of success and
   * a negative error code in case of failure.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1129
1130
1131
   */
  static int mount_ubifs(struct ubifs_info *c)
  {
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1132
  	int err;
3668b70fc   Artem Bityutskiy   UBIFS: print less
1133
  	long long x, y;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1134
  	size_t sz;
bc98a42c1   David Howells   VFS: Convert sb->...
1135
  	c->ro_mount = !!sb_rdonly(c->vfs_sb);
90bea5a3f   Daniel Golle   UBIFS: respect MS...
1136
1137
  	/* Suppress error messages while probing if MS_SILENT is set */
  	c->probing = !!(c->vfs_sb->s_flags & MS_SILENT);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1138
1139
1140
  	err = init_constants_early(c);
  	if (err)
  		return err;
17c2f9f85   Artem Bityutskiy   UBIFS: separate d...
1141
1142
1143
  	err = ubifs_debugging_init(c);
  	if (err)
  		return err;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1144
1145
1146
1147
  
  	err = check_volume_empty(c);
  	if (err)
  		goto out_free;
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1148
  	if (c->empty && (c->ro_mount || c->ro_media)) {
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1149
1150
1151
1152
  		/*
  		 * This UBI volume is empty, and read-only, or the file system
  		 * is mounted read-only - we cannot format it.
  		 */
235c362bd   Sheng Yong   UBIFS: extend deb...
1153
  		ubifs_err(c, "can't format empty UBI volume: read-only %s",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1154
1155
1156
1157
  			  c->ro_media ? "UBI volume" : "mount");
  		err = -EROFS;
  		goto out_free;
  	}
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1158
  	if (c->ro_media && !c->ro_mount) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1159
  		ubifs_err(c, "cannot mount read-write - read-only media");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
  		err = -EROFS;
  		goto out_free;
  	}
  
  	/*
  	 * The requirement for the buffer is that it should fit indexing B-tree
  	 * height amount of integers. We assume the height if the TNC tree will
  	 * never exceed 64.
  	 */
  	err = -ENOMEM;
  	c->bottom_up_buf = kmalloc(BOTTOM_UP_HEIGHT * sizeof(int), GFP_KERNEL);
  	if (!c->bottom_up_buf)
  		goto out_free;
  
  	c->sbuf = vmalloc(c->leb_size);
  	if (!c->sbuf)
  		goto out_free;
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1177
  	if (!c->ro_mount) {
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1178
1179
1180
1181
  		c->ileb_buf = vmalloc(c->leb_size);
  		if (!c->ileb_buf)
  			goto out_free;
  	}
3477d2046   Artem Bityutskiy   UBIFS: pre-alloca...
1182
1183
  	if (c->bulk_read == 1)
  		bu_init(c);
d882962f6   Matthew L. Creech   UBIFS: handle all...
1184
  	if (!c->ro_mount) {
7799953b3   Richard Weinberger   ubifs: Implement ...
1185
1186
  		c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ + \
  					       UBIFS_CIPHER_BLOCK_SIZE,
d882962f6   Matthew L. Creech   UBIFS: handle all...
1187
1188
1189
1190
  					       GFP_KERNEL);
  		if (!c->write_reserve_buf)
  			goto out_free;
  	}
18d1d7fbc   Artem Bityutskiy   UBIFS: introduce ...
1191
  	c->mounting = 1;
2953e73f1   Adrian Hunter   UBIFS: add no_chk...
1192

1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1193
1194
1195
  	err = ubifs_read_superblock(c);
  	if (err)
  		goto out_free;
90bea5a3f   Daniel Golle   UBIFS: respect MS...
1196
  	c->probing = 0;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1197
  	/*
553dea4dd   Artem Bityutskiy   UBIFS: introduce ...
1198
  	 * Make sure the compressor which is set as default in the superblock
57a450e95   Artem Bityutskiy   UBIFS: allow moun...
1199
  	 * or overridden by mount options is actually compiled in.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1200
1201
  	 */
  	if (!ubifs_compr_present(c->default_compr)) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1202
  		ubifs_err(c, "'compressor \"%s\" is not compiled in",
553dea4dd   Artem Bityutskiy   UBIFS: introduce ...
1203
  			  ubifs_compr_name(c->default_compr));
8eec2f36f   Corentin Chary   UBIFS: return pro...
1204
  		err = -ENOTSUPP;
553dea4dd   Artem Bityutskiy   UBIFS: introduce ...
1205
  		goto out_free;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1206
  	}
79807d075   Artem Bityutskiy   UBIFS: fix consta...
1207
  	err = init_constants_sb(c);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1208
  	if (err)
17c2f9f85   Artem Bityutskiy   UBIFS: separate d...
1209
  		goto out_free;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1210
1211
1212
1213
1214
1215
  
  	sz = ALIGN(c->max_idx_node_sz, c->min_io_size);
  	sz = ALIGN(sz + c->max_idx_node_sz, c->min_io_size);
  	c->cbuf = kmalloc(sz, GFP_NOFS);
  	if (!c->cbuf) {
  		err = -ENOMEM;
17c2f9f85   Artem Bityutskiy   UBIFS: separate d...
1216
  		goto out_free;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1217
  	}
b50b9f408   Artem Bityutskiy   UBIFS: do not fre...
1218
1219
1220
  	err = alloc_wbufs(c);
  	if (err)
  		goto out_cbuf;
0855f310d   Sebastian Siewior   UBIFS: create the...
1221
  	sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num, c->vi.vol_id);
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1222
  	if (!c->ro_mount) {
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1223
  		/* Create background thread */
fcabb3479   Hunter Adrian   UBIFS: fix compil...
1224
  		c->bgt = kthread_create(ubifs_bg_thread, c, "%s", c->bgt_name);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1225
1226
1227
  		if (IS_ERR(c->bgt)) {
  			err = PTR_ERR(c->bgt);
  			c->bgt = NULL;
235c362bd   Sheng Yong   UBIFS: extend deb...
1228
  			ubifs_err(c, "cannot spawn \"%s\", error %d",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1229
1230
1231
1232
1233
1234
1235
1236
1237
  				  c->bgt_name, err);
  			goto out_wbufs;
  		}
  		wake_up_process(c->bgt);
  	}
  
  	err = ubifs_read_master(c);
  	if (err)
  		goto out_master;
098011940   Ben Gardiner   UBIFS: fix-up fre...
1238
  	init_constants_master(c);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1239
  	if ((c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1240
  		ubifs_msg(c, "recovery needed");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1241
  		c->need_recovery = 1;
781c5717a   Ben Gardiner   UBIFS: intialize ...
1242
  	}
781c5717a   Ben Gardiner   UBIFS: intialize ...
1243
1244
1245
1246
1247
1248
1249
1250
1251
  	if (c->need_recovery && !c->ro_mount) {
  		err = ubifs_recover_inl_heads(c, c->sbuf);
  		if (err)
  			goto out_master;
  	}
  
  	err = ubifs_lpt_init(c, 1, !c->ro_mount);
  	if (err)
  		goto out_master;
098011940   Ben Gardiner   UBIFS: fix-up fre...
1252
1253
1254
  	if (!c->ro_mount && c->space_fixup) {
  		err = ubifs_fixup_free_space(c);
  		if (err)
56b04e3e8   Sidney Amani   UBIFS: fix memory...
1255
  			goto out_lpt;
098011940   Ben Gardiner   UBIFS: fix-up fre...
1256
  	}
2c84599ca   Sheng Yong   UBIFS: do not wri...
1257
  	if (!c->ro_mount && !c->need_recovery) {
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1258
1259
1260
1261
1262
1263
1264
  		/*
  		 * Set the "dirty" flag so that if we reboot uncleanly we
  		 * will notice this immediately on the next mount.
  		 */
  		c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  		err = ubifs_write_master(c);
  		if (err)
781c5717a   Ben Gardiner   UBIFS: intialize ...
1265
  			goto out_lpt;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1266
  	}
b137545c4   Artem Bityutskiy   UBIFS: introduce ...
1267
  	err = dbg_check_idx_size(c, c->bi.old_idx_sz);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1268
1269
1270
1271
1272
1273
  	if (err)
  		goto out_lpt;
  
  	err = ubifs_replay_journal(c);
  	if (err)
  		goto out_journal;
1fb8bd01e   Artem Bityutskiy   UBIFS: fix assert...
1274
  	/* Calculate 'min_idx_lebs' after journal replay */
b137545c4   Artem Bityutskiy   UBIFS: introduce ...
1275
  	c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
1fb8bd01e   Artem Bityutskiy   UBIFS: fix assert...
1276

2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1277
  	err = ubifs_mount_orphans(c, c->need_recovery, c->ro_mount);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1278
1279
  	if (err)
  		goto out_orphans;
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1280
  	if (!c->ro_mount) {
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1281
  		int lnum;
57a450e95   Artem Bityutskiy   UBIFS: allow moun...
1282
1283
  		err = check_free_space(c);
  		if (err)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1284
  			goto out_orphans;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
  
  		/* Check for enough log space */
  		lnum = c->lhead_lnum + 1;
  		if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
  			lnum = UBIFS_LOG_LNUM;
  		if (lnum == c->ltail_lnum) {
  			err = ubifs_consolidate_log(c);
  			if (err)
  				goto out_orphans;
  		}
  
  		if (c->need_recovery) {
  			err = ubifs_recover_size(c);
  			if (err)
  				goto out_orphans;
  			err = ubifs_rcvry_gc_commit(c);
276de5d2a   Artem Bityutskiy   UBIFS: check retu...
1301
1302
  			if (err)
  				goto out_orphans;
b4978e949   Artem Bityutskiy   UBIFS: always cle...
1303
  		} else {
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1304
  			err = take_gc_lnum(c);
b4978e949   Artem Bityutskiy   UBIFS: always cle...
1305
1306
1307
1308
1309
1310
1311
1312
1313
  			if (err)
  				goto out_orphans;
  
  			/*
  			 * GC LEB may contain garbage if there was an unclean
  			 * reboot, and it should be un-mapped.
  			 */
  			err = ubifs_leb_unmap(c, c->gc_lnum);
  			if (err)
c18de72fb   Matthieu CASTET   UBIFS: fix a memo...
1314
  				goto out_orphans;
b4978e949   Artem Bityutskiy   UBIFS: always cle...
1315
  		}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1316
1317
1318
1319
1320
1321
1322
1323
  
  		err = dbg_check_lprops(c);
  		if (err)
  			goto out_orphans;
  	} else if (c->need_recovery) {
  		err = ubifs_recover_size(c);
  		if (err)
  			goto out_orphans;
b4978e949   Artem Bityutskiy   UBIFS: always cle...
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
  	} else {
  		/*
  		 * Even if we mount read-only, we have to set space in GC LEB
  		 * to proper value because this affects UBIFS free space
  		 * reporting. We do not want to have a situation when
  		 * re-mounting from R/O to R/W changes amount of free space.
  		 */
  		err = take_gc_lnum(c);
  		if (err)
  			goto out_orphans;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1334
1335
1336
1337
1338
1339
1340
  	}
  
  	spin_lock(&ubifs_infos_lock);
  	list_add_tail(&c->infos_list, &ubifs_infos);
  	spin_unlock(&ubifs_infos_lock);
  
  	if (c->need_recovery) {
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1341
  		if (c->ro_mount)
235c362bd   Sheng Yong   UBIFS: extend deb...
1342
  			ubifs_msg(c, "recovery deferred");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1343
1344
  		else {
  			c->need_recovery = 0;
235c362bd   Sheng Yong   UBIFS: extend deb...
1345
  			ubifs_msg(c, "recovery completed");
b221337ae   Artem Bityutskiy   UBIFS: fix bogus ...
1346
1347
1348
1349
1350
1351
  			/*
  			 * GC LEB has to be empty and taken at this point. But
  			 * the journal head LEBs may also be accounted as
  			 * "empty taken" if they are empty.
  			 */
  			ubifs_assert(c->lst.taken_empty_lebs > 0);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1352
  		}
6ba87c9b9   Artem Bityutskiy   UBIFS: fix assert...
1353
  	} else
b221337ae   Artem Bityutskiy   UBIFS: fix bogus ...
1354
  		ubifs_assert(c->lst.taken_empty_lebs > 0);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1355

6ba87c9b9   Artem Bityutskiy   UBIFS: fix assert...
1356
  	err = dbg_check_filesystem(c);
552ff3179   Artem Bityutskiy   UBIFS: add debugf...
1357
1358
  	if (err)
  		goto out_infos;
6ba87c9b9   Artem Bityutskiy   UBIFS: fix assert...
1359
  	err = dbg_debugfs_init_fs(c);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1360
1361
  	if (err)
  		goto out_infos;
18d1d7fbc   Artem Bityutskiy   UBIFS: introduce ...
1362
  	c->mounting = 0;
2953e73f1   Adrian Hunter   UBIFS: add no_chk...
1363

235c362bd   Sheng Yong   UBIFS: extend deb...
1364
  	ubifs_msg(c, "UBIFS: mounted UBI device %d, volume %d, name \"%s\"%s",
3668b70fc   Artem Bityutskiy   UBIFS: print less
1365
  		  c->vi.ubi_num, c->vi.vol_id, c->vi.name,
beadadfa5   Richard Genoud   UBIFS: correct mo...
1366
  		  c->ro_mount ? ", R/O mode" : "");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1367
  	x = (long long)c->main_lebs * c->leb_size;
3668b70fc   Artem Bityutskiy   UBIFS: print less
1368
  	y = (long long)c->log_lebs * c->leb_size + c->max_bud_bytes;
235c362bd   Sheng Yong   UBIFS: extend deb...
1369
  	ubifs_msg(c, "LEB size: %d bytes (%d KiB), min./max. I/O unit sizes: %d bytes/%d bytes",
3668b70fc   Artem Bityutskiy   UBIFS: print less
1370
1371
  		  c->leb_size, c->leb_size >> 10, c->min_io_size,
  		  c->max_write_size);
235c362bd   Sheng Yong   UBIFS: extend deb...
1372
  	ubifs_msg(c, "FS size: %lld bytes (%lld MiB, %d LEBs), journal size %lld bytes (%lld MiB, %d LEBs)",
3668b70fc   Artem Bityutskiy   UBIFS: print less
1373
1374
  		  x, x >> 20, c->main_lebs,
  		  y, y >> 20, c->log_lebs + c->max_bud_cnt);
235c362bd   Sheng Yong   UBIFS: extend deb...
1375
  	ubifs_msg(c, "reserved for root: %llu bytes (%llu KiB)",
3668b70fc   Artem Bityutskiy   UBIFS: print less
1376
  		  c->report_rp_size, c->report_rp_size >> 10);
235c362bd   Sheng Yong   UBIFS: extend deb...
1377
  	ubifs_msg(c, "media format: w%d/r%d (latest is w%d/r%d), UUID %pUB%s",
963f0cf6d   Artem Bityutskiy   UBIFS: add R/O co...
1378
  		  c->fmt_version, c->ro_compat_version,
3668b70fc   Artem Bityutskiy   UBIFS: print less
1379
1380
1381
1382
1383
  		  UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION, c->uuid,
  		  c->big_lpt ? ", big LPT model" : ", small LPT model");
  
  	dbg_gen("default compressor:  %s", ubifs_compr_name(c->default_compr));
  	dbg_gen("data journal heads:  %d",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1384
  		c->jhead_cnt - NONDATA_JHEADS_CNT);
3668b70fc   Artem Bityutskiy   UBIFS: print less
1385
  	dbg_gen("log LEBs:            %d (%d - %d)",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1386
  		c->log_lebs, UBIFS_LOG_LNUM, c->log_last);
3668b70fc   Artem Bityutskiy   UBIFS: print less
1387
  	dbg_gen("LPT area LEBs:       %d (%d - %d)",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1388
  		c->lpt_lebs, c->lpt_first, c->lpt_last);
3668b70fc   Artem Bityutskiy   UBIFS: print less
1389
  	dbg_gen("orphan area LEBs:    %d (%d - %d)",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1390
  		c->orph_lebs, c->orph_first, c->orph_last);
3668b70fc   Artem Bityutskiy   UBIFS: print less
1391
  	dbg_gen("main area LEBs:      %d (%d - %d)",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1392
  		c->main_lebs, c->main_first, c->leb_cnt - 1);
3668b70fc   Artem Bityutskiy   UBIFS: print less
1393
1394
  	dbg_gen("index LEBs:          %d", c->lst.idx_lebs);
  	dbg_gen("total index bytes:   %lld (%lld KiB, %lld MiB)",
b137545c4   Artem Bityutskiy   UBIFS: introduce ...
1395
1396
  		c->bi.old_idx_sz, c->bi.old_idx_sz >> 10,
  		c->bi.old_idx_sz >> 20);
3668b70fc   Artem Bityutskiy   UBIFS: print less
1397
1398
1399
1400
1401
1402
  	dbg_gen("key hash type:       %d", c->key_hash_type);
  	dbg_gen("tree fanout:         %d", c->fanout);
  	dbg_gen("reserved GC LEB:     %d", c->gc_lnum);
  	dbg_gen("max. znode size      %d", c->max_znode_sz);
  	dbg_gen("max. index node size %d", c->max_idx_node_sz);
  	dbg_gen("node sizes:          data %zu, inode %zu, dentry %zu",
8e5033adc   Artem Bityutskiy   UBIFS: add more u...
1403
  		UBIFS_DATA_NODE_SZ, UBIFS_INO_NODE_SZ, UBIFS_DENT_NODE_SZ);
3668b70fc   Artem Bityutskiy   UBIFS: print less
1404
  	dbg_gen("node sizes:          trun %zu, sb %zu, master %zu",
8e5033adc   Artem Bityutskiy   UBIFS: add more u...
1405
  		UBIFS_TRUN_NODE_SZ, UBIFS_SB_NODE_SZ, UBIFS_MST_NODE_SZ);
3668b70fc   Artem Bityutskiy   UBIFS: print less
1406
  	dbg_gen("node sizes:          ref %zu, cmt. start %zu, orph %zu",
8e5033adc   Artem Bityutskiy   UBIFS: add more u...
1407
  		UBIFS_REF_NODE_SZ, UBIFS_CS_NODE_SZ, UBIFS_ORPH_NODE_SZ);
3668b70fc   Artem Bityutskiy   UBIFS: print less
1408
  	dbg_gen("max. node sizes:     data %zu, inode %zu dentry %zu, idx %d",
c43615702   Artem Bityutskiy   UBIFS: fix minor ...
1409
  		UBIFS_MAX_DATA_NODE_SZ, UBIFS_MAX_INO_NODE_SZ,
6342aaebd   Artem Bityutskiy   UBIFS: print max....
1410
  		UBIFS_MAX_DENT_NODE_SZ, ubifs_idx_node_sz(c, c->fanout));
3668b70fc   Artem Bityutskiy   UBIFS: print less
1411
1412
1413
  	dbg_gen("dead watermark:      %d", c->dead_wm);
  	dbg_gen("dark watermark:      %d", c->dark_wm);
  	dbg_gen("LEB overhead:        %d", c->leb_overhead);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1414
  	x = (long long)c->main_lebs * c->dark_wm;
3668b70fc   Artem Bityutskiy   UBIFS: print less
1415
  	dbg_gen("max. dark space:     %lld (%lld KiB, %lld MiB)",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1416
  		x, x >> 10, x >> 20);
3668b70fc   Artem Bityutskiy   UBIFS: print less
1417
  	dbg_gen("maximum bud bytes:   %lld (%lld KiB, %lld MiB)",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1418
1419
  		c->max_bud_bytes, c->max_bud_bytes >> 10,
  		c->max_bud_bytes >> 20);
3668b70fc   Artem Bityutskiy   UBIFS: print less
1420
  	dbg_gen("BG commit bud bytes: %lld (%lld KiB, %lld MiB)",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1421
1422
  		c->bg_bud_bytes, c->bg_bud_bytes >> 10,
  		c->bg_bud_bytes >> 20);
3668b70fc   Artem Bityutskiy   UBIFS: print less
1423
  	dbg_gen("current bud bytes    %lld (%lld KiB, %lld MiB)",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1424
  		c->bud_bytes, c->bud_bytes >> 10, c->bud_bytes >> 20);
3668b70fc   Artem Bityutskiy   UBIFS: print less
1425
1426
  	dbg_gen("max. seq. number:    %llu", c->max_sqnum);
  	dbg_gen("commit number:       %llu", c->cmt_no);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
  
  	return 0;
  
  out_infos:
  	spin_lock(&ubifs_infos_lock);
  	list_del(&c->infos_list);
  	spin_unlock(&ubifs_infos_lock);
  out_orphans:
  	free_orphans(c);
  out_journal:
  	destroy_journal(c);
  out_lpt:
  	ubifs_lpt_free(c, 0);
  out_master:
  	kfree(c->mst_node);
  	kfree(c->rcvrd_mst_node);
  	if (c->bgt)
  		kthread_stop(c->bgt);
  out_wbufs:
  	free_wbufs(c);
  out_cbuf:
  	kfree(c->cbuf);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1449
  out_free:
d882962f6   Matthew L. Creech   UBIFS: handle all...
1450
  	kfree(c->write_reserve_buf);
3477d2046   Artem Bityutskiy   UBIFS: pre-alloca...
1451
  	kfree(c->bu.buf);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1452
1453
1454
  	vfree(c->ileb_buf);
  	vfree(c->sbuf);
  	kfree(c->bottom_up_buf);
17c2f9f85   Artem Bityutskiy   UBIFS: separate d...
1455
  	ubifs_debugging_exit(c);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
  	return err;
  }
  
  /**
   * ubifs_umount - un-mount UBIFS file-system.
   * @c: UBIFS file-system description object
   *
   * Note, this function is called to free allocated resourced when un-mounting,
   * as well as free resources when an error occurred while we were half way
   * through mounting (error path cleanup function). So it has to make sure the
   * resource was actually allocated before freeing it.
   */
  static void ubifs_umount(struct ubifs_info *c)
  {
  	dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num,
  		c->vi.vol_id);
552ff3179   Artem Bityutskiy   UBIFS: add debugf...
1472
  	dbg_debugfs_exit_fs(c);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
  	spin_lock(&ubifs_infos_lock);
  	list_del(&c->infos_list);
  	spin_unlock(&ubifs_infos_lock);
  
  	if (c->bgt)
  		kthread_stop(c->bgt);
  
  	destroy_journal(c);
  	free_wbufs(c);
  	free_orphans(c);
  	ubifs_lpt_free(c, 0);
  
  	kfree(c->cbuf);
  	kfree(c->rcvrd_mst_node);
  	kfree(c->mst_node);
d882962f6   Matthew L. Creech   UBIFS: handle all...
1488
  	kfree(c->write_reserve_buf);
3477d2046   Artem Bityutskiy   UBIFS: pre-alloca...
1489
1490
  	kfree(c->bu.buf);
  	vfree(c->ileb_buf);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1491
1492
  	vfree(c->sbuf);
  	kfree(c->bottom_up_buf);
17c2f9f85   Artem Bityutskiy   UBIFS: separate d...
1493
  	ubifs_debugging_exit(c);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
  }
  
  /**
   * ubifs_remount_rw - re-mount in read-write mode.
   * @c: UBIFS file-system description object
   *
   * UBIFS avoids allocating many unnecessary resources when mounted in read-only
   * mode. This function allocates the needed resources and re-mounts UBIFS in
   * read-write mode.
   */
  static int ubifs_remount_rw(struct ubifs_info *c)
  {
  	int err, lnum;
963f0cf6d   Artem Bityutskiy   UBIFS: add R/O co...
1507
  	if (c->rw_incompat) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1508
1509
  		ubifs_err(c, "the file-system is not R/W-compatible");
  		ubifs_msg(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...
1510
1511
  			  c->fmt_version, c->ro_compat_version,
  			  UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION);
963f0cf6d   Artem Bityutskiy   UBIFS: add R/O co...
1512
1513
  		return -EROFS;
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1514
  	mutex_lock(&c->umount_mutex);
84abf972c   Artem Bityutskiy   UBIFS: add re-mou...
1515
  	dbg_save_space_info(c);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1516
  	c->remounting_rw = 1;
c88ac00c5   Artem Bityutskiy   UBIFS: fix assert...
1517
  	c->ro_mount = 0;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1518

67e753ca4   Artem Bityutskiy   UBIFS: make space...
1519
1520
1521
  	if (c->space_fixup) {
  		err = ubifs_fixup_free_space(c);
  		if (err)
fcdd57c89   Artem Bityutskiy   UBIFS: fix remoun...
1522
  			goto out;
67e753ca4   Artem Bityutskiy   UBIFS: make space...
1523
  	}
57a450e95   Artem Bityutskiy   UBIFS: allow moun...
1524
1525
  	err = check_free_space(c);
  	if (err)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1526
  		goto out;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
  
  	if (c->old_leb_cnt != c->leb_cnt) {
  		struct ubifs_sb_node *sup;
  
  		sup = ubifs_read_sb_node(c);
  		if (IS_ERR(sup)) {
  			err = PTR_ERR(sup);
  			goto out;
  		}
  		sup->leb_cnt = cpu_to_le32(c->leb_cnt);
  		err = ubifs_write_sb_node(c, sup);
eaeee242c   Artem Bityutskiy   UBIFS: fix a rare...
1538
  		kfree(sup);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1539
1540
1541
1542
1543
  		if (err)
  			goto out;
  	}
  
  	if (c->need_recovery) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1544
  		ubifs_msg(c, "completing deferred recovery");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
  		err = ubifs_write_rcvrd_mst_node(c);
  		if (err)
  			goto out;
  		err = ubifs_recover_size(c);
  		if (err)
  			goto out;
  		err = ubifs_clean_lebs(c, c->sbuf);
  		if (err)
  			goto out;
  		err = ubifs_recover_inl_heads(c, c->sbuf);
  		if (err)
  			goto out;
49d128aa6   Adrian Hunter   UBIFS: ensure orp...
1557
1558
1559
1560
1561
1562
  	} else {
  		/* A readonly mount is not allowed to have orphans */
  		ubifs_assert(c->tot_orphans == 0);
  		err = ubifs_clear_orphans(c);
  		if (err)
  			goto out;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
  	}
  
  	if (!(c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY))) {
  		c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  		err = ubifs_write_master(c);
  		if (err)
  			goto out;
  	}
  
  	c->ileb_buf = vmalloc(c->leb_size);
  	if (!c->ileb_buf) {
  		err = -ENOMEM;
  		goto out;
  	}
7799953b3   Richard Weinberger   ubifs: Implement ...
1577
1578
  	c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ + \
  				       UBIFS_CIPHER_BLOCK_SIZE, GFP_KERNEL);
7203db97b   Wei Yongjun   UBIFS: fix return...
1579
1580
  	if (!c->write_reserve_buf) {
  		err = -ENOMEM;
d882962f6   Matthew L. Creech   UBIFS: handle all...
1581
  		goto out;
7203db97b   Wei Yongjun   UBIFS: fix return...
1582
  	}
d882962f6   Matthew L. Creech   UBIFS: handle all...
1583

1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1584
1585
1586
  	err = ubifs_lpt_init(c, 0, 1);
  	if (err)
  		goto out;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1587
  	/* Create background thread */
fcabb3479   Hunter Adrian   UBIFS: fix compil...
1588
  	c->bgt = kthread_create(ubifs_bg_thread, c, "%s", c->bgt_name);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1589
1590
1591
  	if (IS_ERR(c->bgt)) {
  		err = PTR_ERR(c->bgt);
  		c->bgt = NULL;
235c362bd   Sheng Yong   UBIFS: extend deb...
1592
  		ubifs_err(c, "cannot spawn \"%s\", error %d",
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1593
  			  c->bgt_name, err);
2953e73f1   Adrian Hunter   UBIFS: add no_chk...
1594
  		goto out;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1595
1596
1597
1598
  	}
  	wake_up_process(c->bgt);
  
  	c->orph_buf = vmalloc(c->leb_size);
2953e73f1   Adrian Hunter   UBIFS: add no_chk...
1599
1600
1601
1602
  	if (!c->orph_buf) {
  		err = -ENOMEM;
  		goto out;
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
  
  	/* Check for enough log space */
  	lnum = c->lhead_lnum + 1;
  	if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
  		lnum = UBIFS_LOG_LNUM;
  	if (lnum == c->ltail_lnum) {
  		err = ubifs_consolidate_log(c);
  		if (err)
  			goto out;
  	}
  
  	if (c->need_recovery)
  		err = ubifs_rcvry_gc_commit(c);
  	else
b4978e949   Artem Bityutskiy   UBIFS: always cle...
1617
  		err = ubifs_leb_unmap(c, c->gc_lnum);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1618
1619
  	if (err)
  		goto out;
8c230d9a5   Artem Bityutskiy   UBIFS: fix false ...
1620
1621
  	dbg_gen("re-mounted read-write");
  	c->remounting_rw = 0;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1622
1623
  	if (c->need_recovery) {
  		c->need_recovery = 0;
235c362bd   Sheng Yong   UBIFS: extend deb...
1624
  		ubifs_msg(c, "deferred recovery completed");
8c230d9a5   Artem Bityutskiy   UBIFS: fix false ...
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
  	} else {
  		/*
  		 * Do not run the debugging space check if the were doing
  		 * recovery, because when we saved the information we had the
  		 * file-system in a state where the TNC and lprops has been
  		 * modified in memory, but all the I/O operations (including a
  		 * commit) were deferred. So the file-system was in
  		 * "non-committed" state. Now the file-system is in committed
  		 * state, and of course the amount of free space will change
  		 * because, for example, the old index size was imprecise.
  		 */
  		err = dbg_check_space_info(c);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1637
  	}
9d510db42   Matthew L. Creech   UBIFS: fix-up fre...
1638

1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1639
  	mutex_unlock(&c->umount_mutex);
84abf972c   Artem Bityutskiy   UBIFS: add re-mou...
1640
  	return err;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1641
1642
  
  out:
c88ac00c5   Artem Bityutskiy   UBIFS: fix assert...
1643
  	c->ro_mount = 1;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1644
1645
1646
1647
1648
1649
1650
  	vfree(c->orph_buf);
  	c->orph_buf = NULL;
  	if (c->bgt) {
  		kthread_stop(c->bgt);
  		c->bgt = NULL;
  	}
  	free_wbufs(c);
d882962f6   Matthew L. Creech   UBIFS: handle all...
1651
1652
  	kfree(c->write_reserve_buf);
  	c->write_reserve_buf = NULL;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1653
1654
1655
1656
1657
1658
1659
1660
1661
  	vfree(c->ileb_buf);
  	c->ileb_buf = NULL;
  	ubifs_lpt_free(c, 1);
  	c->remounting_rw = 0;
  	mutex_unlock(&c->umount_mutex);
  	return err;
  }
  
  /**
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1662
1663
1664
   * ubifs_remount_ro - re-mount in read-only mode.
   * @c: UBIFS file-system description object
   *
84abf972c   Artem Bityutskiy   UBIFS: add re-mou...
1665
1666
   * We assume VFS has stopped writing. Possibly the background thread could be
   * running a commit, however kthread_stop will wait in that case.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1667
1668
1669
1670
1671
1672
   */
  static void ubifs_remount_ro(struct ubifs_info *c)
  {
  	int i, err;
  
  	ubifs_assert(!c->need_recovery);
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1673
  	ubifs_assert(!c->ro_mount);
e4d9b6cbf   Artem Bityutskiy   UBIFS: fix LEB li...
1674

1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1675
1676
1677
1678
1679
  	mutex_lock(&c->umount_mutex);
  	if (c->bgt) {
  		kthread_stop(c->bgt);
  		c->bgt = NULL;
  	}
84abf972c   Artem Bityutskiy   UBIFS: add re-mou...
1680
  	dbg_save_space_info(c);
09844df06   Richard Weinberger   ubifs: Check ubif...
1681
1682
1683
1684
1685
  	for (i = 0; i < c->jhead_cnt; i++) {
  		err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
  		if (err)
  			ubifs_ro_mode(c, err);
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1686

e4d9b6cbf   Artem Bityutskiy   UBIFS: fix LEB li...
1687
1688
1689
1690
1691
1692
  	c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
  	c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
  	c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
  	err = ubifs_write_master(c);
  	if (err)
  		ubifs_ro_mode(c, err);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1693
1694
  	vfree(c->orph_buf);
  	c->orph_buf = NULL;
d882962f6   Matthew L. Creech   UBIFS: handle all...
1695
1696
  	kfree(c->write_reserve_buf);
  	c->write_reserve_buf = NULL;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1697
1698
1699
  	vfree(c->ileb_buf);
  	c->ileb_buf = NULL;
  	ubifs_lpt_free(c, 1);
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1700
  	c->ro_mount = 1;
84abf972c   Artem Bityutskiy   UBIFS: add re-mou...
1701
1702
1703
  	err = dbg_check_space_info(c);
  	if (err)
  		ubifs_ro_mode(c, err);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1704
1705
1706
1707
1708
1709
1710
  	mutex_unlock(&c->umount_mutex);
  }
  
  static void ubifs_put_super(struct super_block *sb)
  {
  	int i;
  	struct ubifs_info *c = sb->s_fs_info;
235c362bd   Sheng Yong   UBIFS: extend deb...
1711
  	ubifs_msg(c, "un-mount UBI device %d", c->vi.ubi_num);
6cfd01484   Christoph Hellwig   push BKL down int...
1712

1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1713
1714
1715
1716
1717
  	/*
  	 * The following asserts are only valid if there has not been a failure
  	 * of the media. For example, there will be dirty inodes if we failed
  	 * to write them back because of I/O errors.
  	 */
1a067a22e   Artem Bityutskiy   UBIFS: fix false ...
1718
  	if (!c->ro_error) {
b137545c4   Artem Bityutskiy   UBIFS: introduce ...
1719
1720
1721
  		ubifs_assert(c->bi.idx_growth == 0);
  		ubifs_assert(c->bi.dd_growth == 0);
  		ubifs_assert(c->bi.data_growth == 0);
1a067a22e   Artem Bityutskiy   UBIFS: fix false ...
1722
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1723
1724
1725
1726
1727
1728
1729
1730
  
  	/*
  	 * The 'c->umount_lock' prevents races between UBIFS memory shrinker
  	 * and file system un-mount. Namely, it prevents the shrinker from
  	 * picking this superblock for shrinking - it will be just skipped if
  	 * the mutex is locked.
  	 */
  	mutex_lock(&c->umount_mutex);
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1731
  	if (!c->ro_mount) {
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1732
1733
1734
1735
1736
1737
1738
1739
  		/*
  		 * First of all kill the background thread to make sure it does
  		 * not interfere with un-mounting and freeing resources.
  		 */
  		if (c->bgt) {
  			kthread_stop(c->bgt);
  			c->bgt = NULL;
  		}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1740
  		/*
2680d722b   Artem Bityutskiy   UBIFS: introduce ...
1741
  		 * On fatal errors c->ro_error is set to 1, in which case we do
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1742
1743
  		 * not write the master node.
  		 */
2680d722b   Artem Bityutskiy   UBIFS: introduce ...
1744
  		if (!c->ro_error) {
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1745
1746
1747
  			int err;
  
  			/* Synchronize write-buffers */
09844df06   Richard Weinberger   ubifs: Check ubif...
1748
1749
1750
1751
1752
  			for (i = 0; i < c->jhead_cnt; i++) {
  				err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
  				if (err)
  					ubifs_ro_mode(c, err);
  			}
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1753

1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1754
1755
1756
1757
1758
  			/*
  			 * We are being cleanly unmounted which means the
  			 * orphans were killed - indicate this in the master
  			 * node. Also save the reserved GC LEB number.
  			 */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
  			c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
  			c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
  			c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
  			err = ubifs_write_master(c);
  			if (err)
  				/*
  				 * Recovery will attempt to fix the master area
  				 * next mount, so we just print a message and
  				 * continue to unmount normally.
  				 */
235c362bd   Sheng Yong   UBIFS: extend deb...
1769
  				ubifs_err(c, "failed to write master node, error %d",
79fda5179   Artem Bityutskiy   UBIFS: comply wit...
1770
  					  err);
3601ba273   Artem Bityutskiy   UBIFS: do not for...
1771
1772
1773
1774
  		} else {
  			for (i = 0; i < c->jhead_cnt; i++)
  				/* Make sure write-buffer timers are canceled */
  				hrtimer_cancel(&c->jheads[i].wbuf.timer);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1775
1776
1777
1778
  		}
  	}
  
  	ubifs_umount(c);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1779
1780
  	ubi_close_volume(c->ubi);
  	mutex_unlock(&c->umount_mutex);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1781
1782
1783
1784
1785
1786
  }
  
  static int ubifs_remount_fs(struct super_block *sb, int *flags, char *data)
  {
  	int err;
  	struct ubifs_info *c = sb->s_fs_info;
02b9984d6   Theodore Ts'o   fs: push sync_fil...
1787
  	sync_filesystem(sb);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1788
1789
1790
1791
  	dbg_gen("old flags %#lx, new flags %#x", sb->s_flags, *flags);
  
  	err = ubifs_parse_options(c, data, 1);
  	if (err) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1792
  		ubifs_err(c, "invalid or unknown remount parameter");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1793
1794
  		return err;
  	}
3477d2046   Artem Bityutskiy   UBIFS: pre-alloca...
1795

2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1796
  	if (c->ro_mount && !(*flags & MS_RDONLY)) {
2680d722b   Artem Bityutskiy   UBIFS: introduce ...
1797
  		if (c->ro_error) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1798
  			ubifs_msg(c, "cannot re-mount R/W due to prior errors");
2680d722b   Artem Bityutskiy   UBIFS: introduce ...
1799
1800
  			return -EROFS;
  		}
e4d9b6cbf   Artem Bityutskiy   UBIFS: fix LEB li...
1801
  		if (c->ro_media) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1802
  			ubifs_msg(c, "cannot re-mount R/W - UBI volume is R/O");
a2b9df3ff   Artem Bityutskiy   UBIFS: return sen...
1803
  			return -EROFS;
e4d9b6cbf   Artem Bityutskiy   UBIFS: fix LEB li...
1804
  		}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1805
  		err = ubifs_remount_rw(c);
e9d6bbc42   Artem Bityutskiy   UBIFS: kill BKL
1806
  		if (err)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1807
  			return err;
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
1808
  	} else if (!c->ro_mount && (*flags & MS_RDONLY)) {
2680d722b   Artem Bityutskiy   UBIFS: introduce ...
1809
  		if (c->ro_error) {
235c362bd   Sheng Yong   UBIFS: extend deb...
1810
  			ubifs_msg(c, "cannot re-mount R/O due to prior errors");
a2b9df3ff   Artem Bityutskiy   UBIFS: return sen...
1811
  			return -EROFS;
b466f17d7   Adrian Hunter   UBIFS: remount ro...
1812
  		}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1813
  		ubifs_remount_ro(c);
b466f17d7   Adrian Hunter   UBIFS: remount ro...
1814
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1815

3477d2046   Artem Bityutskiy   UBIFS: pre-alloca...
1816
1817
1818
1819
  	if (c->bulk_read == 1)
  		bu_init(c);
  	else {
  		dbg_gen("disable bulk-read");
07d41c3cf   karam.lee   ubifs: Fix oops w...
1820
  		mutex_lock(&c->bu_mutex);
3477d2046   Artem Bityutskiy   UBIFS: pre-alloca...
1821
1822
  		kfree(c->bu.buf);
  		c->bu.buf = NULL;
07d41c3cf   karam.lee   ubifs: Fix oops w...
1823
  		mutex_unlock(&c->bu_mutex);
3477d2046   Artem Bityutskiy   UBIFS: pre-alloca...
1824
  	}
b221337ae   Artem Bityutskiy   UBIFS: fix bogus ...
1825
  	ubifs_assert(c->lst.taken_empty_lebs > 0);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1826
1827
  	return 0;
  }
e8b815663   Artem Bityutskiy   UBIFS: constify o...
1828
  const struct super_operations ubifs_super_operations = {
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1829
1830
1831
1832
  	.alloc_inode   = ubifs_alloc_inode,
  	.destroy_inode = ubifs_destroy_inode,
  	.put_super     = ubifs_put_super,
  	.write_inode   = ubifs_write_inode,
d640e1b50   Al Viro   switch ubifs to -...
1833
  	.evict_inode   = ubifs_evict_inode,
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
  	.statfs        = ubifs_statfs,
  	.dirty_inode   = ubifs_dirty_inode,
  	.remount_fs    = ubifs_remount_fs,
  	.show_options  = ubifs_show_options,
  	.sync_fs       = ubifs_sync_fs,
  };
  
  /**
   * open_ubi - parse UBI device name string and open the UBI device.
   * @name: UBI volume name
   * @mode: UBI volume open mode
   *
9722324e6   Corentin Chary   UBIFS: support mo...
1846
1847
1848
1849
1850
1851
   * The primary method of mounting UBIFS is by specifying the UBI volume
   * character device node path. However, UBIFS may also be mounted withoug any
   * character device node using one of the following methods:
   *
   * o ubiX_Y    - mount UBI device number X, volume Y;
   * o ubiY      - mount UBI device number 0, volume Y;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1852
1853
1854
1855
1856
   * o ubiX:NAME - mount UBI device X, volume with name NAME;
   * o ubi:NAME  - mount UBI device 0, volume with name NAME.
   *
   * Alternative '!' separator may be used instead of ':' (because some shells
   * like busybox may interpret ':' as an NFS host name separator). This function
9722324e6   Corentin Chary   UBIFS: support mo...
1857
1858
   * returns UBI volume description object in case of success and a negative
   * error code in case of failure.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1859
1860
1861
   */
  static struct ubi_volume_desc *open_ubi(const char *name, int mode)
  {
9722324e6   Corentin Chary   UBIFS: support mo...
1862
  	struct ubi_volume_desc *ubi;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1863
1864
  	int dev, vol;
  	char *endptr;
4c925efc2   Richard Weinberger   ubifs: Check for ...
1865
1866
  	if (!name || !*name)
  		return ERR_PTR(-EINVAL);
9722324e6   Corentin Chary   UBIFS: support mo...
1867
1868
1869
1870
1871
1872
  	/* First, try to open using the device node path method */
  	ubi = ubi_open_volume_path(name, mode);
  	if (!IS_ERR(ubi))
  		return ubi;
  
  	/* Try the "nodev" method */
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
  	if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i')
  		return ERR_PTR(-EINVAL);
  
  	/* ubi:NAME method */
  	if ((name[3] == ':' || name[3] == '!') && name[4] != '\0')
  		return ubi_open_volume_nm(0, name + 4, mode);
  
  	if (!isdigit(name[3]))
  		return ERR_PTR(-EINVAL);
  
  	dev = simple_strtoul(name + 3, &endptr, 0);
  
  	/* ubiY method */
  	if (*endptr == '\0')
  		return ubi_open_volume(0, dev, mode);
  
  	/* ubiX_Y method */
  	if (*endptr == '_' && isdigit(endptr[1])) {
  		vol = simple_strtoul(endptr + 1, &endptr, 0);
  		if (*endptr != '\0')
  			return ERR_PTR(-EINVAL);
  		return ubi_open_volume(dev, vol, mode);
  	}
  
  	/* ubiX:NAME method */
  	if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
  		return ubi_open_volume_nm(dev, ++endptr, mode);
  
  	return ERR_PTR(-EINVAL);
  }
b1c27ab3f   Al Viro   ubifs: split allo...
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
  static struct ubifs_info *alloc_ubifs_info(struct ubi_volume_desc *ubi)
  {
  	struct ubifs_info *c;
  
  	c = kzalloc(sizeof(struct ubifs_info), GFP_KERNEL);
  	if (c) {
  		spin_lock_init(&c->cnt_lock);
  		spin_lock_init(&c->cs_lock);
  		spin_lock_init(&c->buds_lock);
  		spin_lock_init(&c->space_lock);
  		spin_lock_init(&c->orphan_lock);
  		init_rwsem(&c->commit_sem);
  		mutex_init(&c->lp_mutex);
  		mutex_init(&c->tnc_mutex);
  		mutex_init(&c->log_mutex);
b1c27ab3f   Al Viro   ubifs: split allo...
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
  		mutex_init(&c->umount_mutex);
  		mutex_init(&c->bu_mutex);
  		mutex_init(&c->write_reserve_mutex);
  		init_waitqueue_head(&c->cmt_wq);
  		c->buds = RB_ROOT;
  		c->old_idx = RB_ROOT;
  		c->size_tree = RB_ROOT;
  		c->orph_tree = RB_ROOT;
  		INIT_LIST_HEAD(&c->infos_list);
  		INIT_LIST_HEAD(&c->idx_gc);
  		INIT_LIST_HEAD(&c->replay_list);
  		INIT_LIST_HEAD(&c->replay_buds);
  		INIT_LIST_HEAD(&c->uncat_list);
  		INIT_LIST_HEAD(&c->empty_list);
  		INIT_LIST_HEAD(&c->freeable_list);
  		INIT_LIST_HEAD(&c->frdi_idx_list);
  		INIT_LIST_HEAD(&c->unclean_leb_list);
  		INIT_LIST_HEAD(&c->old_buds);
  		INIT_LIST_HEAD(&c->orph_list);
  		INIT_LIST_HEAD(&c->orph_new);
  		c->no_chk_data_crc = 1;
  
  		c->highest_inum = UBIFS_FIRST_INO;
  		c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
  
  		ubi_get_volume_info(ubi, &c->vi);
  		ubi_get_device_info(c->vi.ubi_num, &c->di);
  	}
  	return c;
  }
d475a5074   Richard Weinberger   ubifs: Add skelet...
1948
  #ifndef CONFIG_UBIFS_FS_ENCRYPTION
6f69f0ed6   Eric Biggers   fscrypt: constify...
1949
  const struct fscrypt_operations ubifs_crypt_operations = {
1ee77870c   Richard Weinberger   ubifs: Constify s...
1950
  	.is_encrypted		= __ubifs_crypt_is_encrypted,
d475a5074   Richard Weinberger   ubifs: Add skelet...
1951
1952
  };
  #endif
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1953
1954
  static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
  {
d251ed271   Al Viro   ubifs: fix sget r...
1955
  	struct ubifs_info *c = sb->s_fs_info;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1956
1957
  	struct inode *root;
  	int err;
8379ea31e   Artem Bityutskiy   UBIFS: allow sync...
1958
  	c->vfs_sb = sb;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1959
1960
1961
1962
  	/* Re-open the UBI device in read-write mode */
  	c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READWRITE);
  	if (IS_ERR(c->ubi)) {
  		err = PTR_ERR(c->ubi);
d251ed271   Al Viro   ubifs: fix sget r...
1963
  		goto out;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1964
  	}
99edd4580   Jan Kara   ubifs: Convert to...
1965
1966
1967
  	err = ubifs_parse_options(c, data, 0);
  	if (err)
  		goto out_close;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1968
  	/*
0a883a05c   Artem Bityutskiy   UBIFS: few commen...
1969
  	 * UBIFS provides 'backing_dev_info' in order to disable read-ahead. For
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1970
1971
  	 * UBIFS, I/O is not deferred, it is done immediately in readpage,
  	 * which means the user would have to wait not just for their own I/O
0a883a05c   Artem Bityutskiy   UBIFS: few commen...
1972
  	 * but the read-ahead I/O as well i.e. completely pointless.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1973
  	 *
99edd4580   Jan Kara   ubifs: Convert to...
1974
1975
1976
  	 * Read-ahead will be disabled because @sb->s_bdi->ra_pages is 0. Also
  	 * @sb->s_bdi->capabilities are initialized to 0 so there won't be any
  	 * writeback happening.
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1977
  	 */
99edd4580   Jan Kara   ubifs: Convert to...
1978
1979
  	err = super_setup_bdi_name(sb, "ubifs_%d_%d", c->vi.ubi_num,
  				   c->vi.vol_id);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1980
1981
  	if (err)
  		goto out_close;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1982

1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1983
1984
1985
1986
  	sb->s_fs_info = c;
  	sb->s_magic = UBIFS_SUPER_MAGIC;
  	sb->s_blocksize = UBIFS_BLOCK_SIZE;
  	sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1987
1988
1989
1990
  	sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c);
  	if (c->max_inode_sz > MAX_LFS_FILESIZE)
  		sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
  	sb->s_op = &ubifs_super_operations;
2b88fc21c   Andreas Gruenbacher   ubifs: Switch to ...
1991
  	sb->s_xattr = ubifs_xattr_handlers;
d475a5074   Richard Weinberger   ubifs: Add skelet...
1992
  	sb->s_cop = &ubifs_crypt_operations;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
  
  	mutex_lock(&c->umount_mutex);
  	err = mount_ubifs(c);
  	if (err) {
  		ubifs_assert(err < 0);
  		goto out_unlock;
  	}
  
  	/* Read the root inode */
  	root = ubifs_iget(sb, UBIFS_ROOT_INO);
  	if (IS_ERR(root)) {
  		err = PTR_ERR(root);
  		goto out_umount;
  	}
48fde701a   Al Viro   switch open-coded...
2007
  	sb->s_root = d_make_root(root);
7203db97b   Wei Yongjun   UBIFS: fix return...
2008
2009
  	if (!sb->s_root) {
  		err = -ENOMEM;
48fde701a   Al Viro   switch open-coded...
2010
  		goto out_umount;
7203db97b   Wei Yongjun   UBIFS: fix return...
2011
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2012
2013
  
  	mutex_unlock(&c->umount_mutex);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2014
  	return 0;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2015
2016
2017
2018
  out_umount:
  	ubifs_umount(c);
  out_unlock:
  	mutex_unlock(&c->umount_mutex);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2019
2020
  out_close:
  	ubi_close_volume(c->ubi);
d251ed271   Al Viro   ubifs: fix sget r...
2021
  out:
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2022
2023
2024
2025
2026
  	return err;
  }
  
  static int sb_test(struct super_block *sb, void *data)
  {
d251ed271   Al Viro   ubifs: fix sget r...
2027
  	struct ubifs_info *c1 = data;
7c83f5cb5   Artem Bityutskiy   UBIFS: use anonym...
2028
  	struct ubifs_info *c = sb->s_fs_info;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2029

d251ed271   Al Viro   ubifs: fix sget r...
2030
2031
2032
2033
2034
2035
2036
  	return c->vi.cdev == c1->vi.cdev;
  }
  
  static int sb_set(struct super_block *sb, void *data)
  {
  	sb->s_fs_info = data;
  	return set_anon_super(sb, NULL);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2037
  }
157d81e7f   Al Viro   convert ubifs
2038
2039
  static struct dentry *ubifs_mount(struct file_system_type *fs_type, int flags,
  			const char *name, void *data)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2040
2041
  {
  	struct ubi_volume_desc *ubi;
d251ed271   Al Viro   ubifs: fix sget r...
2042
  	struct ubifs_info *c;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
  	struct super_block *sb;
  	int err;
  
  	dbg_gen("name %s, flags %#x", name, flags);
  
  	/*
  	 * Get UBI device number and volume ID. Mount it read-only so far
  	 * because this might be a new mount point, and UBI allows only one
  	 * read-write user at a time.
  	 */
  	ubi = open_ubi(name, UBI_READONLY);
  	if (IS_ERR(ubi)) {
1ae92642e   Daniel Golle   ubifs: Silence er...
2055
2056
2057
  		if (!(flags & MS_SILENT))
  			pr_err("UBIFS error (pid: %d): cannot open \"%s\", error %d",
  			       current->pid, name, (int)PTR_ERR(ubi));
157d81e7f   Al Viro   convert ubifs
2058
  		return ERR_CAST(ubi);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2059
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2060

d251ed271   Al Viro   ubifs: fix sget r...
2061
2062
2063
2064
2065
2066
2067
  	c = alloc_ubifs_info(ubi);
  	if (!c) {
  		err = -ENOMEM;
  		goto out_close;
  	}
  
  	dbg_gen("opened ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2068

9249e17fe   David Howells   VFS: Pass mount f...
2069
  	sb = sget(fs_type, sb_test, sb_set, flags, c);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2070
2071
  	if (IS_ERR(sb)) {
  		err = PTR_ERR(sb);
d251ed271   Al Viro   ubifs: fix sget r...
2072
  		kfree(c);
185bf8739   Dan Carpenter   ubifs: dereferenc...
2073
  		goto out_close;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2074
2075
2076
  	}
  
  	if (sb->s_root) {
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
2077
  		struct ubifs_info *c1 = sb->s_fs_info;
d251ed271   Al Viro   ubifs: fix sget r...
2078
  		kfree(c);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2079
2080
  		/* A new mount point for already mounted UBIFS */
  		dbg_gen("this ubi volume is already mounted");
2ef13294d   Artem Bityutskiy   UBIFS: introduce ...
2081
  		if (!!(flags & MS_RDONLY) != c1->ro_mount) {
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2082
2083
2084
2085
  			err = -EBUSY;
  			goto out_deact;
  		}
  	} else {
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2086
2087
2088
2089
  		err = ubifs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  		if (err)
  			goto out_deact;
  		/* We do not support atime */
8c1c5f263   Dongsheng Yang   ubifs: introduce ...
2090
2091
2092
2093
2094
2095
  		sb->s_flags |= MS_ACTIVE;
  #ifndef CONFIG_UBIFS_ATIME_SUPPORT
  		sb->s_flags |= MS_NOATIME;
  #else
  		ubifs_msg(c, "full atime support is enabled.");
  #endif
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2096
2097
2098
2099
  	}
  
  	/* 'fill_super()' opens ubi again so we must close it here */
  	ubi_close_volume(ubi);
157d81e7f   Al Viro   convert ubifs
2100
  	return dget(sb->s_root);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2101
2102
  
  out_deact:
6f5bbff9a   Al Viro   Convert obvious p...
2103
  	deactivate_locked_super(sb);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2104
2105
  out_close:
  	ubi_close_volume(ubi);
157d81e7f   Al Viro   convert ubifs
2106
  	return ERR_PTR(err);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2107
  }
d251ed271   Al Viro   ubifs: fix sget r...
2108
2109
2110
2111
2112
2113
  static void kill_ubifs_super(struct super_block *s)
  {
  	struct ubifs_info *c = s->s_fs_info;
  	kill_anon_super(s);
  	kfree(c);
  }
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2114
2115
2116
  static struct file_system_type ubifs_fs_type = {
  	.name    = "ubifs",
  	.owner   = THIS_MODULE,
157d81e7f   Al Viro   convert ubifs
2117
  	.mount   = ubifs_mount,
d251ed271   Al Viro   ubifs: fix sget r...
2118
  	.kill_sb = kill_ubifs_super,
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2119
  };
7f78e0351   Eric W. Biederman   fs: Limit sys_mou...
2120
  MODULE_ALIAS_FS("ubifs");
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2121
2122
2123
2124
  
  /*
   * Inode slab cache constructor.
   */
51cc50685   Alexey Dobriyan   SL*B: drop kmem c...
2125
  static void inode_slab_ctor(void *obj)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
  {
  	struct ubifs_inode *ui = obj;
  	inode_init_once(&ui->vfs_inode);
  }
  
  static int __init ubifs_init(void)
  {
  	int err;
  
  	BUILD_BUG_ON(sizeof(struct ubifs_ch) != 24);
  
  	/* Make sure node sizes are 8-byte aligned */
  	BUILD_BUG_ON(UBIFS_CH_SZ        & 7);
  	BUILD_BUG_ON(UBIFS_INO_NODE_SZ  & 7);
  	BUILD_BUG_ON(UBIFS_DENT_NODE_SZ & 7);
  	BUILD_BUG_ON(UBIFS_XENT_NODE_SZ & 7);
  	BUILD_BUG_ON(UBIFS_DATA_NODE_SZ & 7);
  	BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ & 7);
  	BUILD_BUG_ON(UBIFS_SB_NODE_SZ   & 7);
  	BUILD_BUG_ON(UBIFS_MST_NODE_SZ  & 7);
  	BUILD_BUG_ON(UBIFS_REF_NODE_SZ  & 7);
  	BUILD_BUG_ON(UBIFS_CS_NODE_SZ   & 7);
  	BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ & 7);
  
  	BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ & 7);
  	BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ & 7);
  	BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ & 7);
  	BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ  & 7);
  	BUILD_BUG_ON(UBIFS_MAX_NODE_SZ      & 7);
  	BUILD_BUG_ON(MIN_WRITE_SZ           & 7);
  
  	/* Check min. node size */
  	BUILD_BUG_ON(UBIFS_INO_NODE_SZ  < MIN_WRITE_SZ);
  	BUILD_BUG_ON(UBIFS_DENT_NODE_SZ < MIN_WRITE_SZ);
  	BUILD_BUG_ON(UBIFS_XENT_NODE_SZ < MIN_WRITE_SZ);
  	BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ < MIN_WRITE_SZ);
  
  	BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
  	BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
  	BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ > UBIFS_MAX_NODE_SZ);
  	BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ  > UBIFS_MAX_NODE_SZ);
  
  	/* Defined node sizes */
  	BUILD_BUG_ON(UBIFS_SB_NODE_SZ  != 4096);
  	BUILD_BUG_ON(UBIFS_MST_NODE_SZ != 512);
  	BUILD_BUG_ON(UBIFS_INO_NODE_SZ != 160);
  	BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
  
  	/*
a1dc080c2   Artem Bityutskiy   UBIFS: use bit-fi...
2175
2176
  	 * We use 2 bit wide bit-fields to store compression type, which should
  	 * be amended if more compressors are added. The bit-fields are:
553dea4dd   Artem Bityutskiy   UBIFS: introduce ...
2177
2178
  	 * @compr_type in 'struct ubifs_inode', @default_compr in
  	 * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'.
a1dc080c2   Artem Bityutskiy   UBIFS: use bit-fi...
2179
2180
2181
2182
  	 */
  	BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > 4);
  
  	/*
ea1754a08   Kirill A. Shutemov   mm, fs: remove re...
2183
  	 * We require that PAGE_SIZE is greater-than-or-equal-to
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2184
2185
  	 * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
  	 */
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
2186
  	if (PAGE_SIZE < UBIFS_BLOCK_SIZE) {
235c362bd   Sheng Yong   UBIFS: extend deb...
2187
  		pr_err("UBIFS error (pid %d): VFS page cache size is %u bytes, but UBIFS requires at least 4096 bytes",
09cbfeaf1   Kirill A. Shutemov   mm, fs: get rid o...
2188
  		       current->pid, (unsigned int)PAGE_SIZE);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2189
2190
  		return -EINVAL;
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2191
2192
  	ubifs_inode_slab = kmem_cache_create("ubifs_inode_slab",
  				sizeof(struct ubifs_inode), 0,
5d097056c   Vladimir Davydov   kmemcg: account c...
2193
2194
  				SLAB_MEM_SPREAD | SLAB_RECLAIM_ACCOUNT |
  				SLAB_ACCOUNT, &inode_slab_ctor);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2195
  	if (!ubifs_inode_slab)
5cc361e3b   Al Viro   ubifs: too early ...
2196
  		return -ENOMEM;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2197

a1fe33af5   Chao Yu   ubifs: fix to che...
2198
2199
2200
  	err = register_shrinker(&ubifs_shrinker_info);
  	if (err)
  		goto out_slab;
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2201
2202
2203
  
  	err = ubifs_compressors_init();
  	if (err)
552ff3179   Artem Bityutskiy   UBIFS: add debugf...
2204
2205
2206
2207
  		goto out_shrinker;
  
  	err = dbg_debugfs_init();
  	if (err)
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2208
  		goto out_compr;
5cc361e3b   Al Viro   ubifs: too early ...
2209
2210
  	err = register_filesystem(&ubifs_fs_type);
  	if (err) {
235c362bd   Sheng Yong   UBIFS: extend deb...
2211
2212
  		pr_err("UBIFS error (pid %d): cannot register file system, error %d",
  		       current->pid, err);
5cc361e3b   Al Viro   ubifs: too early ...
2213
2214
  		goto out_dbg;
  	}
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2215
  	return 0;
5cc361e3b   Al Viro   ubifs: too early ...
2216
2217
  out_dbg:
  	dbg_debugfs_exit();
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2218
  out_compr:
552ff3179   Artem Bityutskiy   UBIFS: add debugf...
2219
2220
  	ubifs_compressors_exit();
  out_shrinker:
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2221
  	unregister_shrinker(&ubifs_shrinker_info);
a1fe33af5   Chao Yu   ubifs: fix to che...
2222
  out_slab:
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2223
  	kmem_cache_destroy(ubifs_inode_slab);
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2224
2225
2226
2227
2228
2229
2230
2231
2232
  	return err;
  }
  /* late_initcall to let compressors initialize first */
  late_initcall(ubifs_init);
  
  static void __exit ubifs_exit(void)
  {
  	ubifs_assert(list_empty(&ubifs_infos));
  	ubifs_assert(atomic_long_read(&ubifs_clean_zn_cnt) == 0);
552ff3179   Artem Bityutskiy   UBIFS: add debugf...
2233
  	dbg_debugfs_exit();
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2234
2235
  	ubifs_compressors_exit();
  	unregister_shrinker(&ubifs_shrinker_info);
8c0a85377   Kirill A. Shutemov   fs: push rcu_barr...
2236
2237
2238
2239
2240
2241
  
  	/*
  	 * Make sure all delayed rcu free inodes are flushed before we
  	 * destroy cache.
  	 */
  	rcu_barrier();
1e51764a3   Artem Bityutskiy   UBIFS: add new fl...
2242
2243
2244
2245
2246
2247
2248
2249
2250
  	kmem_cache_destroy(ubifs_inode_slab);
  	unregister_filesystem(&ubifs_fs_type);
  }
  module_exit(ubifs_exit);
  
  MODULE_LICENSE("GPL");
  MODULE_VERSION(__stringify(UBIFS_VERSION));
  MODULE_AUTHOR("Artem Bityutskiy, Adrian Hunter");
  MODULE_DESCRIPTION("UBIFS - UBI File System");