xref: /openbmc/u-boot/fs/ubifs/super.c (revision 4e3349b6)
1 /*
2  * This file is part of UBIFS.
3  *
4  * Copyright (C) 2006-2008 Nokia Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Authors: Artem Bityutskiy (Битюцкий Артём)
20  *          Adrian Hunter
21  */
22 
23 /*
24  * This file implements UBIFS initialization and VFS superblock operations. Some
25  * initialization stuff which is rather large and complex is placed at
26  * corresponding subsystems, but most of it is here.
27  */
28 
29 #include "ubifs.h"
30 #include <linux/math64.h>
31 
32 #define INODE_LOCKED_MAX	64
33 
34 struct super_block *ubifs_sb;
35 static struct inode *inodes_locked_down[INODE_LOCKED_MAX];
36 
37 /* shrinker.c */
38 
39 /* List of all UBIFS file-system instances */
40 struct list_head ubifs_infos;
41 
42 /* linux/fs/super.c */
43 
44 static int sb_set(struct super_block *sb, void *data)
45 {
46 	dev_t *dev = data;
47 
48 	sb->s_dev = *dev;
49 	return 0;
50 }
51 
52 /**
53  *	sget	-	find or create a superblock
54  *	@type:	filesystem type superblock should belong to
55  *	@test:	comparison callback
56  *	@set:	setup callback
57  *	@data:	argument to each of them
58  */
59 struct super_block *sget(struct file_system_type *type,
60 			int (*test)(struct super_block *,void *),
61 			int (*set)(struct super_block *,void *),
62 			void *data)
63 {
64 	struct super_block *s = NULL;
65 	int err;
66 
67 	s = kzalloc(sizeof(struct super_block),  GFP_USER);
68 	if (!s) {
69 		err = -ENOMEM;
70 		return ERR_PTR(err);
71 	}
72 
73 	INIT_LIST_HEAD(&s->s_instances);
74 	INIT_LIST_HEAD(&s->s_inodes);
75 	s->s_time_gran = 1000000000;
76 
77 	err = set(s, data);
78 	if (err) {
79 		return ERR_PTR(err);
80 	}
81 	s->s_type = type;
82 	strncpy(s->s_id, type->name, sizeof(s->s_id));
83 	list_add(&s->s_instances, &type->fs_supers);
84 	return s;
85 }
86 
87 /**
88  * validate_inode - validate inode.
89  * @c: UBIFS file-system description object
90  * @inode: the inode to validate
91  *
92  * This is a helper function for 'ubifs_iget()' which validates various fields
93  * of a newly built inode to make sure they contain sane values and prevent
94  * possible vulnerabilities. Returns zero if the inode is all right and
95  * a non-zero error code if not.
96  */
97 static int validate_inode(struct ubifs_info *c, const struct inode *inode)
98 {
99 	int err;
100 	const struct ubifs_inode *ui = ubifs_inode(inode);
101 
102 	if (inode->i_size > c->max_inode_sz) {
103 		ubifs_err("inode is too large (%lld)",
104 			  (long long)inode->i_size);
105 		return 1;
106 	}
107 
108 	if (ui->compr_type < 0 || ui->compr_type >= UBIFS_COMPR_TYPES_CNT) {
109 		ubifs_err("unknown compression type %d", ui->compr_type);
110 		return 2;
111 	}
112 
113 	if (ui->data_len < 0 || ui->data_len > UBIFS_MAX_INO_DATA)
114 		return 4;
115 
116 	if (!ubifs_compr_present(ui->compr_type)) {
117 		ubifs_warn("inode %lu uses '%s' compression, but it was not "
118 			   "compiled in", inode->i_ino,
119 			   ubifs_compr_name(ui->compr_type));
120 	}
121 
122 	err = dbg_check_dir_size(c, inode);
123 	return err;
124 }
125 
126 struct inode *iget_locked(struct super_block *sb, unsigned long ino)
127 {
128 	struct inode *inode;
129 
130 	inode = (struct inode *)malloc(sizeof(struct ubifs_inode));
131 	if (inode) {
132 		inode->i_ino = ino;
133 		inode->i_sb = sb;
134 		list_add(&inode->i_sb_list, &sb->s_inodes);
135 		inode->i_state = I_LOCK | I_NEW;
136 	}
137 
138 	return inode;
139 }
140 
141 int ubifs_iput(struct inode *inode)
142 {
143 	list_del_init(&inode->i_sb_list);
144 
145 	free(inode);
146 	return 0;
147 }
148 
149 /*
150  * Lock (save) inode in inode array for readback after recovery
151  */
152 void iput(struct inode *inode)
153 {
154 	int i;
155 	struct inode *ino;
156 
157 	/*
158 	 * Search end of list
159 	 */
160 	for (i = 0; i < INODE_LOCKED_MAX; i++) {
161 		if (inodes_locked_down[i] == NULL)
162 			break;
163 	}
164 
165 	if (i >= INODE_LOCKED_MAX) {
166 		ubifs_err("Error, can't lock (save) more inodes while recovery!!!");
167 		return;
168 	}
169 
170 	/*
171 	 * Allocate and use new inode
172 	 */
173 	ino = (struct inode *)malloc(sizeof(struct ubifs_inode));
174 	memcpy(ino, inode, sizeof(struct ubifs_inode));
175 
176 	/*
177 	 * Finally save inode in array
178 	 */
179 	inodes_locked_down[i] = ino;
180 }
181 
182 struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
183 {
184 	int err;
185 	union ubifs_key key;
186 	struct ubifs_ino_node *ino;
187 	struct ubifs_info *c = sb->s_fs_info;
188 	struct inode *inode;
189 	struct ubifs_inode *ui;
190 	int i;
191 
192 	dbg_gen("inode %lu", inum);
193 
194 	/*
195 	 * U-Boot special handling of locked down inodes via recovery
196 	 * e.g. ubifs_recover_size()
197 	 */
198 	for (i = 0; i < INODE_LOCKED_MAX; i++) {
199 		/*
200 		 * Exit on last entry (NULL), inode not found in list
201 		 */
202 		if (inodes_locked_down[i] == NULL)
203 			break;
204 
205 		if (inodes_locked_down[i]->i_ino == inum) {
206 			/*
207 			 * We found the locked down inode in our array,
208 			 * so just return this pointer instead of creating
209 			 * a new one.
210 			 */
211 			return inodes_locked_down[i];
212 		}
213 	}
214 
215 	inode = iget_locked(sb, inum);
216 	if (!inode)
217 		return ERR_PTR(-ENOMEM);
218 	if (!(inode->i_state & I_NEW))
219 		return inode;
220 	ui = ubifs_inode(inode);
221 
222 	ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
223 	if (!ino) {
224 		err = -ENOMEM;
225 		goto out;
226 	}
227 
228 	ino_key_init(c, &key, inode->i_ino);
229 
230 	err = ubifs_tnc_lookup(c, &key, ino);
231 	if (err)
232 		goto out_ino;
233 
234 	inode->i_flags |= (S_NOCMTIME | S_NOATIME);
235 	inode->i_nlink = le32_to_cpu(ino->nlink);
236 	inode->i_uid   = le32_to_cpu(ino->uid);
237 	inode->i_gid   = le32_to_cpu(ino->gid);
238 	inode->i_atime.tv_sec  = (int64_t)le64_to_cpu(ino->atime_sec);
239 	inode->i_atime.tv_nsec = le32_to_cpu(ino->atime_nsec);
240 	inode->i_mtime.tv_sec  = (int64_t)le64_to_cpu(ino->mtime_sec);
241 	inode->i_mtime.tv_nsec = le32_to_cpu(ino->mtime_nsec);
242 	inode->i_ctime.tv_sec  = (int64_t)le64_to_cpu(ino->ctime_sec);
243 	inode->i_ctime.tv_nsec = le32_to_cpu(ino->ctime_nsec);
244 	inode->i_mode = le32_to_cpu(ino->mode);
245 	inode->i_size = le64_to_cpu(ino->size);
246 
247 	ui->data_len    = le32_to_cpu(ino->data_len);
248 	ui->flags       = le32_to_cpu(ino->flags);
249 	ui->compr_type  = le16_to_cpu(ino->compr_type);
250 	ui->creat_sqnum = le64_to_cpu(ino->creat_sqnum);
251 	ui->synced_i_size = ui->ui_size = inode->i_size;
252 
253 	err = validate_inode(c, inode);
254 	if (err)
255 		goto out_invalid;
256 
257 	if ((inode->i_mode & S_IFMT) == S_IFLNK) {
258 		if (ui->data_len <= 0 || ui->data_len > UBIFS_MAX_INO_DATA) {
259 			err = 12;
260 			goto out_invalid;
261 		}
262 		ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
263 		if (!ui->data) {
264 			err = -ENOMEM;
265 			goto out_ino;
266 		}
267 		memcpy(ui->data, ino->data, ui->data_len);
268 		((char *)ui->data)[ui->data_len] = '\0';
269 	}
270 
271 	kfree(ino);
272 	inode->i_state &= ~(I_LOCK | I_NEW);
273 	return inode;
274 
275 out_invalid:
276 	ubifs_err("inode %lu validation failed, error %d", inode->i_ino, err);
277 	dbg_dump_node(c, ino);
278 	dbg_dump_inode(c, inode);
279 	err = -EINVAL;
280 out_ino:
281 	kfree(ino);
282 out:
283 	ubifs_err("failed to read inode %lu, error %d", inode->i_ino, err);
284 	return ERR_PTR(err);
285 }
286 
287 /**
288  * init_constants_early - initialize UBIFS constants.
289  * @c: UBIFS file-system description object
290  *
291  * This function initialize UBIFS constants which do not need the superblock to
292  * be read. It also checks that the UBI volume satisfies basic UBIFS
293  * requirements. Returns zero in case of success and a negative error code in
294  * case of failure.
295  */
296 static int init_constants_early(struct ubifs_info *c)
297 {
298 	if (c->vi.corrupted) {
299 		ubifs_warn("UBI volume is corrupted - read-only mode");
300 		c->ro_media = 1;
301 	}
302 
303 	if (c->di.ro_mode) {
304 		ubifs_msg("read-only UBI device");
305 		c->ro_media = 1;
306 	}
307 
308 	if (c->vi.vol_type == UBI_STATIC_VOLUME) {
309 		ubifs_msg("static UBI volume - read-only mode");
310 		c->ro_media = 1;
311 	}
312 
313 	c->leb_cnt = c->vi.size;
314 	c->leb_size = c->vi.usable_leb_size;
315 	c->half_leb_size = c->leb_size / 2;
316 	c->min_io_size = c->di.min_io_size;
317 	c->min_io_shift = fls(c->min_io_size) - 1;
318 
319 	if (c->leb_size < UBIFS_MIN_LEB_SZ) {
320 		ubifs_err("too small LEBs (%d bytes), min. is %d bytes",
321 			  c->leb_size, UBIFS_MIN_LEB_SZ);
322 		return -EINVAL;
323 	}
324 
325 	if (c->leb_cnt < UBIFS_MIN_LEB_CNT) {
326 		ubifs_err("too few LEBs (%d), min. is %d",
327 			  c->leb_cnt, UBIFS_MIN_LEB_CNT);
328 		return -EINVAL;
329 	}
330 
331 	if (!is_power_of_2(c->min_io_size)) {
332 		ubifs_err("bad min. I/O size %d", c->min_io_size);
333 		return -EINVAL;
334 	}
335 
336 	/*
337 	 * UBIFS aligns all node to 8-byte boundary, so to make function in
338 	 * io.c simpler, assume minimum I/O unit size to be 8 bytes if it is
339 	 * less than 8.
340 	 */
341 	if (c->min_io_size < 8) {
342 		c->min_io_size = 8;
343 		c->min_io_shift = 3;
344 	}
345 
346 	c->ref_node_alsz = ALIGN(UBIFS_REF_NODE_SZ, c->min_io_size);
347 	c->mst_node_alsz = ALIGN(UBIFS_MST_NODE_SZ, c->min_io_size);
348 
349 	/*
350 	 * Initialize node length ranges which are mostly needed for node
351 	 * length validation.
352 	 */
353 	c->ranges[UBIFS_PAD_NODE].len  = UBIFS_PAD_NODE_SZ;
354 	c->ranges[UBIFS_SB_NODE].len   = UBIFS_SB_NODE_SZ;
355 	c->ranges[UBIFS_MST_NODE].len  = UBIFS_MST_NODE_SZ;
356 	c->ranges[UBIFS_REF_NODE].len  = UBIFS_REF_NODE_SZ;
357 	c->ranges[UBIFS_TRUN_NODE].len = UBIFS_TRUN_NODE_SZ;
358 	c->ranges[UBIFS_CS_NODE].len   = UBIFS_CS_NODE_SZ;
359 
360 	c->ranges[UBIFS_INO_NODE].min_len  = UBIFS_INO_NODE_SZ;
361 	c->ranges[UBIFS_INO_NODE].max_len  = UBIFS_MAX_INO_NODE_SZ;
362 	c->ranges[UBIFS_ORPH_NODE].min_len =
363 				UBIFS_ORPH_NODE_SZ + sizeof(__le64);
364 	c->ranges[UBIFS_ORPH_NODE].max_len = c->leb_size;
365 	c->ranges[UBIFS_DENT_NODE].min_len = UBIFS_DENT_NODE_SZ;
366 	c->ranges[UBIFS_DENT_NODE].max_len = UBIFS_MAX_DENT_NODE_SZ;
367 	c->ranges[UBIFS_XENT_NODE].min_len = UBIFS_XENT_NODE_SZ;
368 	c->ranges[UBIFS_XENT_NODE].max_len = UBIFS_MAX_XENT_NODE_SZ;
369 	c->ranges[UBIFS_DATA_NODE].min_len = UBIFS_DATA_NODE_SZ;
370 	c->ranges[UBIFS_DATA_NODE].max_len = UBIFS_MAX_DATA_NODE_SZ;
371 	/*
372 	 * Minimum indexing node size is amended later when superblock is
373 	 * read and the key length is known.
374 	 */
375 	c->ranges[UBIFS_IDX_NODE].min_len = UBIFS_IDX_NODE_SZ + UBIFS_BRANCH_SZ;
376 	/*
377 	 * Maximum indexing node size is amended later when superblock is
378 	 * read and the fanout is known.
379 	 */
380 	c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX;
381 
382 	/*
383 	 * Initialize dead and dark LEB space watermarks. See gc.c for comments
384 	 * about these values.
385 	 */
386 	c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size);
387 	c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
388 
389 	/*
390 	 * Calculate how many bytes would be wasted at the end of LEB if it was
391 	 * fully filled with data nodes of maximum size. This is used in
392 	 * calculations when reporting free space.
393 	 */
394 	c->leb_overhead = c->leb_size % UBIFS_MAX_DATA_NODE_SZ;
395 
396 	return 0;
397 }
398 
399 /*
400  * init_constants_sb - initialize UBIFS constants.
401  * @c: UBIFS file-system description object
402  *
403  * This is a helper function which initializes various UBIFS constants after
404  * the superblock has been read. It also checks various UBIFS parameters and
405  * makes sure they are all right. Returns zero in case of success and a
406  * negative error code in case of failure.
407  */
408 static int init_constants_sb(struct ubifs_info *c)
409 {
410 	int tmp, err;
411 	long long tmp64;
412 
413 	c->main_bytes = (long long)c->main_lebs * c->leb_size;
414 	c->max_znode_sz = sizeof(struct ubifs_znode) +
415 				c->fanout * sizeof(struct ubifs_zbranch);
416 
417 	tmp = ubifs_idx_node_sz(c, 1);
418 	c->ranges[UBIFS_IDX_NODE].min_len = tmp;
419 	c->min_idx_node_sz = ALIGN(tmp, 8);
420 
421 	tmp = ubifs_idx_node_sz(c, c->fanout);
422 	c->ranges[UBIFS_IDX_NODE].max_len = tmp;
423 	c->max_idx_node_sz = ALIGN(tmp, 8);
424 
425 	/* Make sure LEB size is large enough to fit full commit */
426 	tmp = UBIFS_CS_NODE_SZ + UBIFS_REF_NODE_SZ * c->jhead_cnt;
427 	tmp = ALIGN(tmp, c->min_io_size);
428 	if (tmp > c->leb_size) {
429 		dbg_err("too small LEB size %d, at least %d needed",
430 			c->leb_size, tmp);
431 		return -EINVAL;
432 	}
433 
434 	/*
435 	 * Make sure that the log is large enough to fit reference nodes for
436 	 * all buds plus one reserved LEB.
437 	 */
438 	tmp64 = c->max_bud_bytes + c->leb_size - 1;
439 	c->max_bud_cnt = div_u64(tmp64, c->leb_size);
440 	tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1);
441 	tmp /= c->leb_size;
442 	tmp += 1;
443 	if (c->log_lebs < tmp) {
444 		dbg_err("too small log %d LEBs, required min. %d LEBs",
445 			c->log_lebs, tmp);
446 		return -EINVAL;
447 	}
448 
449 	/*
450 	 * When budgeting we assume worst-case scenarios when the pages are not
451 	 * be compressed and direntries are of the maximum size.
452 	 *
453 	 * Note, data, which may be stored in inodes is budgeted separately, so
454 	 * it is not included into 'c->inode_budget'.
455 	 */
456 	c->page_budget = UBIFS_MAX_DATA_NODE_SZ * UBIFS_BLOCKS_PER_PAGE;
457 	c->inode_budget = UBIFS_INO_NODE_SZ;
458 	c->dent_budget = UBIFS_MAX_DENT_NODE_SZ;
459 
460 	/*
461 	 * When the amount of flash space used by buds becomes
462 	 * 'c->max_bud_bytes', UBIFS just blocks all writers and starts commit.
463 	 * The writers are unblocked when the commit is finished. To avoid
464 	 * writers to be blocked UBIFS initiates background commit in advance,
465 	 * when number of bud bytes becomes above the limit defined below.
466 	 */
467 	c->bg_bud_bytes = (c->max_bud_bytes * 13) >> 4;
468 
469 	/*
470 	 * Ensure minimum journal size. All the bytes in the journal heads are
471 	 * considered to be used, when calculating the current journal usage.
472 	 * Consequently, if the journal is too small, UBIFS will treat it as
473 	 * always full.
474 	 */
475 	tmp64 = (long long)(c->jhead_cnt + 1) * c->leb_size + 1;
476 	if (c->bg_bud_bytes < tmp64)
477 		c->bg_bud_bytes = tmp64;
478 	if (c->max_bud_bytes < tmp64 + c->leb_size)
479 		c->max_bud_bytes = tmp64 + c->leb_size;
480 
481 	err = ubifs_calc_lpt_geom(c);
482 	if (err)
483 		return err;
484 
485 	return 0;
486 }
487 
488 /*
489  * init_constants_master - initialize UBIFS constants.
490  * @c: UBIFS file-system description object
491  *
492  * This is a helper function which initializes various UBIFS constants after
493  * the master node has been read. It also checks various UBIFS parameters and
494  * makes sure they are all right.
495  */
496 static void init_constants_master(struct ubifs_info *c)
497 {
498 	long long tmp64;
499 
500 	c->min_idx_lebs = ubifs_calc_min_idx_lebs(c);
501 
502 	/*
503 	 * Calculate total amount of FS blocks. This number is not used
504 	 * internally because it does not make much sense for UBIFS, but it is
505 	 * necessary to report something for the 'statfs()' call.
506 	 *
507 	 * Subtract the LEB reserved for GC, the LEB which is reserved for
508 	 * deletions, minimum LEBs for the index, and assume only one journal
509 	 * head is available.
510 	 */
511 	tmp64 = c->main_lebs - 1 - 1 - MIN_INDEX_LEBS - c->jhead_cnt + 1;
512 	tmp64 *= (long long)c->leb_size - c->leb_overhead;
513 	tmp64 = ubifs_reported_space(c, tmp64);
514 	c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT;
515 }
516 
517 /**
518  * free_orphans - free orphans.
519  * @c: UBIFS file-system description object
520  */
521 static void free_orphans(struct ubifs_info *c)
522 {
523 	struct ubifs_orphan *orph;
524 
525 	while (c->orph_dnext) {
526 		orph = c->orph_dnext;
527 		c->orph_dnext = orph->dnext;
528 		list_del(&orph->list);
529 		kfree(orph);
530 	}
531 
532 	while (!list_empty(&c->orph_list)) {
533 		orph = list_entry(c->orph_list.next, struct ubifs_orphan, list);
534 		list_del(&orph->list);
535 		kfree(orph);
536 		dbg_err("orphan list not empty at unmount");
537 	}
538 
539 	vfree(c->orph_buf);
540 	c->orph_buf = NULL;
541 }
542 
543 /**
544  * check_volume_empty - check if the UBI volume is empty.
545  * @c: UBIFS file-system description object
546  *
547  * This function checks if the UBIFS volume is empty by looking if its LEBs are
548  * mapped or not. The result of checking is stored in the @c->empty variable.
549  * Returns zero in case of success and a negative error code in case of
550  * failure.
551  */
552 static int check_volume_empty(struct ubifs_info *c)
553 {
554 	int lnum, err;
555 
556 	c->empty = 1;
557 	for (lnum = 0; lnum < c->leb_cnt; lnum++) {
558 		err = ubi_is_mapped(c->ubi, lnum);
559 		if (unlikely(err < 0))
560 			return err;
561 		if (err == 1) {
562 			c->empty = 0;
563 			break;
564 		}
565 
566 		cond_resched();
567 	}
568 
569 	return 0;
570 }
571 
572 /**
573  * mount_ubifs - mount UBIFS file-system.
574  * @c: UBIFS file-system description object
575  *
576  * This function mounts UBIFS file system. Returns zero in case of success and
577  * a negative error code in case of failure.
578  *
579  * Note, the function does not de-allocate resources it it fails half way
580  * through, and the caller has to do this instead.
581  */
582 static int mount_ubifs(struct ubifs_info *c)
583 {
584 	struct super_block *sb = c->vfs_sb;
585 	int err, mounted_read_only = (sb->s_flags & MS_RDONLY);
586 	long long x;
587 	size_t sz;
588 
589 	err = init_constants_early(c);
590 	if (err)
591 		return err;
592 
593 	err = ubifs_debugging_init(c);
594 	if (err)
595 		return err;
596 
597 	err = check_volume_empty(c);
598 	if (err)
599 		goto out_free;
600 
601 	if (c->empty && (mounted_read_only || c->ro_media)) {
602 		/*
603 		 * This UBI volume is empty, and read-only, or the file system
604 		 * is mounted read-only - we cannot format it.
605 		 */
606 		ubifs_err("can't format empty UBI volume: read-only %s",
607 			  c->ro_media ? "UBI volume" : "mount");
608 		err = -EROFS;
609 		goto out_free;
610 	}
611 
612 	if (c->ro_media && !mounted_read_only) {
613 		ubifs_err("cannot mount read-write - read-only media");
614 		err = -EROFS;
615 		goto out_free;
616 	}
617 
618 	/*
619 	 * The requirement for the buffer is that it should fit indexing B-tree
620 	 * height amount of integers. We assume the height if the TNC tree will
621 	 * never exceed 64.
622 	 */
623 	err = -ENOMEM;
624 	c->bottom_up_buf = kmalloc(BOTTOM_UP_HEIGHT * sizeof(int), GFP_KERNEL);
625 	if (!c->bottom_up_buf)
626 		goto out_free;
627 
628 	c->sbuf = vmalloc(c->leb_size);
629 	if (!c->sbuf)
630 		goto out_free;
631 
632 	/*
633 	 * We have to check all CRCs, even for data nodes, when we mount the FS
634 	 * (specifically, when we are replaying).
635 	 */
636 	c->always_chk_crc = 1;
637 
638 	err = ubifs_read_superblock(c);
639 	if (err)
640 		goto out_free;
641 
642 	/*
643 	 * Make sure the compressor which is set as default in the superblock
644 	 * or overridden by mount options is actually compiled in.
645 	 */
646 	if (!ubifs_compr_present(c->default_compr)) {
647 		ubifs_err("'compressor \"%s\" is not compiled in",
648 			  ubifs_compr_name(c->default_compr));
649 		goto out_free;
650 	}
651 
652 	dbg_failure_mode_registration(c);
653 
654 	err = init_constants_sb(c);
655 	if (err)
656 		goto out_free;
657 
658 	sz = ALIGN(c->max_idx_node_sz, c->min_io_size);
659 	sz = ALIGN(sz + c->max_idx_node_sz, c->min_io_size);
660 	c->cbuf = kmalloc(sz, GFP_NOFS);
661 	if (!c->cbuf) {
662 		err = -ENOMEM;
663 		goto out_free;
664 	}
665 
666 	sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num, c->vi.vol_id);
667 
668 	err = ubifs_read_master(c);
669 	if (err)
670 		goto out_master;
671 
672 	init_constants_master(c);
673 
674 	if ((c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) {
675 		ubifs_msg("recovery needed");
676 		c->need_recovery = 1;
677 	}
678 
679 	err = ubifs_lpt_init(c, 1, !mounted_read_only);
680 	if (err)
681 		goto out_lpt;
682 
683 	err = dbg_check_idx_size(c, c->old_idx_sz);
684 	if (err)
685 		goto out_lpt;
686 
687 	err = ubifs_replay_journal(c);
688 	if (err)
689 		goto out_journal;
690 
691 	err = ubifs_mount_orphans(c, c->need_recovery, mounted_read_only);
692 	if (err)
693 		goto out_orphans;
694 
695 	if (c->need_recovery) {
696 		err = ubifs_recover_size(c);
697 		if (err)
698 			goto out_orphans;
699 	}
700 
701 	spin_lock(&ubifs_infos_lock);
702 	list_add_tail(&c->infos_list, &ubifs_infos);
703 	spin_unlock(&ubifs_infos_lock);
704 
705 	if (c->need_recovery) {
706 		if (mounted_read_only)
707 			ubifs_msg("recovery deferred");
708 		else {
709 			c->need_recovery = 0;
710 			ubifs_msg("recovery completed");
711 		}
712 	}
713 
714 	err = dbg_check_filesystem(c);
715 	if (err)
716 		goto out_infos;
717 
718 	c->always_chk_crc = 0;
719 
720 	ubifs_msg("mounted UBI device %d, volume %d, name \"%s\"",
721 		  c->vi.ubi_num, c->vi.vol_id, c->vi.name);
722 	if (mounted_read_only)
723 		ubifs_msg("mounted read-only");
724 	x = (long long)c->main_lebs * c->leb_size;
725 	ubifs_msg("file system size:   %lld bytes (%lld KiB, %lld MiB, %d "
726 		  "LEBs)", x, x >> 10, x >> 20, c->main_lebs);
727 	x = (long long)c->log_lebs * c->leb_size + c->max_bud_bytes;
728 	ubifs_msg("journal size:       %lld bytes (%lld KiB, %lld MiB, %d "
729 		  "LEBs)", x, x >> 10, x >> 20, c->log_lebs + c->max_bud_cnt);
730 	ubifs_msg("media format:       w%d/r%d (latest is w%d/r%d)",
731 		  c->fmt_version, c->ro_compat_version,
732 		  UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION);
733 	ubifs_msg("default compressor: %s", ubifs_compr_name(c->default_compr));
734 	ubifs_msg("reserved for root:  %llu bytes (%llu KiB)",
735 		c->report_rp_size, c->report_rp_size >> 10);
736 
737 	dbg_msg("compiled on:         " __DATE__ " at " __TIME__);
738 	dbg_msg("min. I/O unit size:  %d bytes", c->min_io_size);
739 	dbg_msg("LEB size:            %d bytes (%d KiB)",
740 		c->leb_size, c->leb_size >> 10);
741 	dbg_msg("data journal heads:  %d",
742 		c->jhead_cnt - NONDATA_JHEADS_CNT);
743 	dbg_msg("UUID:                %02X%02X%02X%02X-%02X%02X"
744 	       "-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
745 	       c->uuid[0], c->uuid[1], c->uuid[2], c->uuid[3],
746 	       c->uuid[4], c->uuid[5], c->uuid[6], c->uuid[7],
747 	       c->uuid[8], c->uuid[9], c->uuid[10], c->uuid[11],
748 	       c->uuid[12], c->uuid[13], c->uuid[14], c->uuid[15]);
749 	dbg_msg("big_lpt              %d", c->big_lpt);
750 	dbg_msg("log LEBs:            %d (%d - %d)",
751 		c->log_lebs, UBIFS_LOG_LNUM, c->log_last);
752 	dbg_msg("LPT area LEBs:       %d (%d - %d)",
753 		c->lpt_lebs, c->lpt_first, c->lpt_last);
754 	dbg_msg("orphan area LEBs:    %d (%d - %d)",
755 		c->orph_lebs, c->orph_first, c->orph_last);
756 	dbg_msg("main area LEBs:      %d (%d - %d)",
757 		c->main_lebs, c->main_first, c->leb_cnt - 1);
758 	dbg_msg("index LEBs:          %d", c->lst.idx_lebs);
759 	dbg_msg("total index bytes:   %lld (%lld KiB, %lld MiB)",
760 		c->old_idx_sz, c->old_idx_sz >> 10, c->old_idx_sz >> 20);
761 	dbg_msg("key hash type:       %d", c->key_hash_type);
762 	dbg_msg("tree fanout:         %d", c->fanout);
763 	dbg_msg("reserved GC LEB:     %d", c->gc_lnum);
764 	dbg_msg("first main LEB:      %d", c->main_first);
765 	dbg_msg("max. znode size      %d", c->max_znode_sz);
766 	dbg_msg("max. index node size %d", c->max_idx_node_sz);
767 	dbg_msg("node sizes:          data %zu, inode %zu, dentry %zu",
768 		UBIFS_DATA_NODE_SZ, UBIFS_INO_NODE_SZ, UBIFS_DENT_NODE_SZ);
769 	dbg_msg("node sizes:          trun %zu, sb %zu, master %zu",
770 		UBIFS_TRUN_NODE_SZ, UBIFS_SB_NODE_SZ, UBIFS_MST_NODE_SZ);
771 	dbg_msg("node sizes:          ref %zu, cmt. start %zu, orph %zu",
772 		UBIFS_REF_NODE_SZ, UBIFS_CS_NODE_SZ, UBIFS_ORPH_NODE_SZ);
773 	dbg_msg("max. node sizes:     data %zu, inode %zu dentry %zu",
774 	        UBIFS_MAX_DATA_NODE_SZ, UBIFS_MAX_INO_NODE_SZ,
775 		UBIFS_MAX_DENT_NODE_SZ);
776 	dbg_msg("dead watermark:      %d", c->dead_wm);
777 	dbg_msg("dark watermark:      %d", c->dark_wm);
778 	dbg_msg("LEB overhead:        %d", c->leb_overhead);
779 	x = (long long)c->main_lebs * c->dark_wm;
780 	dbg_msg("max. dark space:     %lld (%lld KiB, %lld MiB)",
781 		x, x >> 10, x >> 20);
782 	dbg_msg("maximum bud bytes:   %lld (%lld KiB, %lld MiB)",
783 		c->max_bud_bytes, c->max_bud_bytes >> 10,
784 		c->max_bud_bytes >> 20);
785 	dbg_msg("BG commit bud bytes: %lld (%lld KiB, %lld MiB)",
786 		c->bg_bud_bytes, c->bg_bud_bytes >> 10,
787 		c->bg_bud_bytes >> 20);
788 	dbg_msg("current bud bytes    %lld (%lld KiB, %lld MiB)",
789 		c->bud_bytes, c->bud_bytes >> 10, c->bud_bytes >> 20);
790 	dbg_msg("max. seq. number:    %llu", c->max_sqnum);
791 	dbg_msg("commit number:       %llu", c->cmt_no);
792 
793 	return 0;
794 
795 out_infos:
796 	spin_lock(&ubifs_infos_lock);
797 	list_del(&c->infos_list);
798 	spin_unlock(&ubifs_infos_lock);
799 out_orphans:
800 	free_orphans(c);
801 out_journal:
802 out_lpt:
803 	ubifs_lpt_free(c, 0);
804 out_master:
805 	kfree(c->mst_node);
806 	kfree(c->rcvrd_mst_node);
807 	if (c->bgt)
808 		kthread_stop(c->bgt);
809 	kfree(c->cbuf);
810 out_free:
811 	vfree(c->ileb_buf);
812 	vfree(c->sbuf);
813 	kfree(c->bottom_up_buf);
814 	ubifs_debugging_exit(c);
815 	return err;
816 }
817 
818 /**
819  * ubifs_umount - un-mount UBIFS file-system.
820  * @c: UBIFS file-system description object
821  *
822  * Note, this function is called to free allocated resourced when un-mounting,
823  * as well as free resources when an error occurred while we were half way
824  * through mounting (error path cleanup function). So it has to make sure the
825  * resource was actually allocated before freeing it.
826  */
827 void ubifs_umount(struct ubifs_info *c)
828 {
829 	dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num,
830 		c->vi.vol_id);
831 
832 	spin_lock(&ubifs_infos_lock);
833 	list_del(&c->infos_list);
834 	spin_unlock(&ubifs_infos_lock);
835 
836 	if (c->bgt)
837 		kthread_stop(c->bgt);
838 
839 	free_orphans(c);
840 	ubifs_lpt_free(c, 0);
841 
842 	kfree(c->cbuf);
843 	kfree(c->rcvrd_mst_node);
844 	kfree(c->mst_node);
845 	vfree(c->ileb_buf);
846 	vfree(c->sbuf);
847 	kfree(c->bottom_up_buf);
848 	ubifs_debugging_exit(c);
849 
850 	/* Finally free U-Boot's global copy of superblock */
851 	if (ubifs_sb != NULL) {
852 		free(ubifs_sb->s_fs_info);
853 		free(ubifs_sb);
854 	}
855 }
856 
857 /**
858  * open_ubi - parse UBI device name string and open the UBI device.
859  * @name: UBI volume name
860  * @mode: UBI volume open mode
861  *
862  * There are several ways to specify UBI volumes when mounting UBIFS:
863  * o ubiX_Y    - UBI device number X, volume Y;
864  * o ubiY      - UBI device number 0, volume Y;
865  * o ubiX:NAME - mount UBI device X, volume with name NAME;
866  * o ubi:NAME  - mount UBI device 0, volume with name NAME.
867  *
868  * Alternative '!' separator may be used instead of ':' (because some shells
869  * like busybox may interpret ':' as an NFS host name separator). This function
870  * returns ubi volume object in case of success and a negative error code in
871  * case of failure.
872  */
873 static struct ubi_volume_desc *open_ubi(const char *name, int mode)
874 {
875 	int dev, vol;
876 	char *endptr;
877 
878 	if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i')
879 		return ERR_PTR(-EINVAL);
880 
881 	/* ubi:NAME method */
882 	if ((name[3] == ':' || name[3] == '!') && name[4] != '\0')
883 		return ubi_open_volume_nm(0, name + 4, mode);
884 
885 	if (!isdigit(name[3]))
886 		return ERR_PTR(-EINVAL);
887 
888 	dev = simple_strtoul(name + 3, &endptr, 0);
889 
890 	/* ubiY method */
891 	if (*endptr == '\0')
892 		return ubi_open_volume(0, dev, mode);
893 
894 	/* ubiX_Y method */
895 	if (*endptr == '_' && isdigit(endptr[1])) {
896 		vol = simple_strtoul(endptr + 1, &endptr, 0);
897 		if (*endptr != '\0')
898 			return ERR_PTR(-EINVAL);
899 		return ubi_open_volume(dev, vol, mode);
900 	}
901 
902 	/* ubiX:NAME method */
903 	if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
904 		return ubi_open_volume_nm(dev, ++endptr, mode);
905 
906 	return ERR_PTR(-EINVAL);
907 }
908 
909 static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
910 {
911 	struct ubi_volume_desc *ubi = sb->s_fs_info;
912 	struct ubifs_info *c;
913 	struct inode *root;
914 	int err;
915 
916 	c = kzalloc(sizeof(struct ubifs_info), GFP_KERNEL);
917 	if (!c)
918 		return -ENOMEM;
919 
920 	spin_lock_init(&c->cnt_lock);
921 	spin_lock_init(&c->cs_lock);
922 	spin_lock_init(&c->buds_lock);
923 	spin_lock_init(&c->space_lock);
924 	spin_lock_init(&c->orphan_lock);
925 	init_rwsem(&c->commit_sem);
926 	mutex_init(&c->lp_mutex);
927 	mutex_init(&c->tnc_mutex);
928 	mutex_init(&c->log_mutex);
929 	mutex_init(&c->mst_mutex);
930 	mutex_init(&c->umount_mutex);
931 	init_waitqueue_head(&c->cmt_wq);
932 	c->buds = RB_ROOT;
933 	c->old_idx = RB_ROOT;
934 	c->size_tree = RB_ROOT;
935 	c->orph_tree = RB_ROOT;
936 	INIT_LIST_HEAD(&c->infos_list);
937 	INIT_LIST_HEAD(&c->idx_gc);
938 	INIT_LIST_HEAD(&c->replay_list);
939 	INIT_LIST_HEAD(&c->replay_buds);
940 	INIT_LIST_HEAD(&c->uncat_list);
941 	INIT_LIST_HEAD(&c->empty_list);
942 	INIT_LIST_HEAD(&c->freeable_list);
943 	INIT_LIST_HEAD(&c->frdi_idx_list);
944 	INIT_LIST_HEAD(&c->unclean_leb_list);
945 	INIT_LIST_HEAD(&c->old_buds);
946 	INIT_LIST_HEAD(&c->orph_list);
947 	INIT_LIST_HEAD(&c->orph_new);
948 
949 	c->highest_inum = UBIFS_FIRST_INO;
950 	c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
951 
952 	ubi_get_volume_info(ubi, &c->vi);
953 	ubi_get_device_info(c->vi.ubi_num, &c->di);
954 
955 	/* Re-open the UBI device in read-write mode */
956 	c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
957 	if (IS_ERR(c->ubi)) {
958 		err = PTR_ERR(c->ubi);
959 		goto out_free;
960 	}
961 
962 	c->vfs_sb = sb;
963 
964 	sb->s_fs_info = c;
965 	sb->s_magic = UBIFS_SUPER_MAGIC;
966 	sb->s_blocksize = UBIFS_BLOCK_SIZE;
967 	sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT;
968 	sb->s_dev = c->vi.cdev;
969 	sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c);
970 	if (c->max_inode_sz > MAX_LFS_FILESIZE)
971 		sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
972 
973 	if (c->rw_incompat) {
974 		ubifs_err("the file-system is not R/W-compatible");
975 		ubifs_msg("on-flash format version is w%d/r%d, but software "
976 			  "only supports up to version w%d/r%d", c->fmt_version,
977 			  c->ro_compat_version, UBIFS_FORMAT_VERSION,
978 			  UBIFS_RO_COMPAT_VERSION);
979 		return -EROFS;
980 	}
981 
982 	mutex_lock(&c->umount_mutex);
983 	err = mount_ubifs(c);
984 	if (err) {
985 		ubifs_assert(err < 0);
986 		goto out_unlock;
987 	}
988 
989 	/* Read the root inode */
990 	root = ubifs_iget(sb, UBIFS_ROOT_INO);
991 	if (IS_ERR(root)) {
992 		err = PTR_ERR(root);
993 		goto out_umount;
994 	}
995 
996 	sb->s_root = NULL;
997 
998 	mutex_unlock(&c->umount_mutex);
999 	return 0;
1000 
1001 out_umount:
1002 	ubifs_umount(c);
1003 out_unlock:
1004 	mutex_unlock(&c->umount_mutex);
1005 	ubi_close_volume(c->ubi);
1006 out_free:
1007 	kfree(c);
1008 	return err;
1009 }
1010 
1011 static int sb_test(struct super_block *sb, void *data)
1012 {
1013 	dev_t *dev = data;
1014 
1015 	return sb->s_dev == *dev;
1016 }
1017 
1018 static int ubifs_get_sb(struct file_system_type *fs_type, int flags,
1019 			const char *name, void *data, struct vfsmount *mnt)
1020 {
1021 	struct ubi_volume_desc *ubi;
1022 	struct ubi_volume_info vi;
1023 	struct super_block *sb;
1024 	int err;
1025 
1026 	dbg_gen("name %s, flags %#x", name, flags);
1027 
1028 	/*
1029 	 * Get UBI device number and volume ID. Mount it read-only so far
1030 	 * because this might be a new mount point, and UBI allows only one
1031 	 * read-write user at a time.
1032 	 */
1033 	ubi = open_ubi(name, UBI_READONLY);
1034 	if (IS_ERR(ubi)) {
1035 		ubifs_err("cannot open \"%s\", error %d",
1036 			  name, (int)PTR_ERR(ubi));
1037 		return PTR_ERR(ubi);
1038 	}
1039 	ubi_get_volume_info(ubi, &vi);
1040 
1041 	dbg_gen("opened ubi%d_%d", vi.ubi_num, vi.vol_id);
1042 
1043 	sb = sget(fs_type, &sb_test, &sb_set, &vi.cdev);
1044 	if (IS_ERR(sb)) {
1045 		err = PTR_ERR(sb);
1046 		goto out_close;
1047 	}
1048 
1049 	if (sb->s_root) {
1050 		/* A new mount point for already mounted UBIFS */
1051 		dbg_gen("this ubi volume is already mounted");
1052 		if ((flags ^ sb->s_flags) & MS_RDONLY) {
1053 			err = -EBUSY;
1054 			goto out_deact;
1055 		}
1056 	} else {
1057 		sb->s_flags = flags;
1058 		/*
1059 		 * Pass 'ubi' to 'fill_super()' in sb->s_fs_info where it is
1060 		 * replaced by 'c'.
1061 		 */
1062 		sb->s_fs_info = ubi;
1063 		err = ubifs_fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
1064 		if (err)
1065 			goto out_deact;
1066 		/* We do not support atime */
1067 		sb->s_flags |= MS_ACTIVE | MS_NOATIME;
1068 	}
1069 
1070 	/* 'fill_super()' opens ubi again so we must close it here */
1071 	ubi_close_volume(ubi);
1072 
1073 	ubifs_sb = sb;
1074 	return 0;
1075 
1076 out_deact:
1077 	up_write(&sb->s_umount);
1078 out_close:
1079 	ubi_close_volume(ubi);
1080 	return err;
1081 }
1082 
1083 int __init ubifs_init(void)
1084 {
1085 	int err;
1086 
1087 	BUILD_BUG_ON(sizeof(struct ubifs_ch) != 24);
1088 
1089 	/* Make sure node sizes are 8-byte aligned */
1090 	BUILD_BUG_ON(UBIFS_CH_SZ        & 7);
1091 	BUILD_BUG_ON(UBIFS_INO_NODE_SZ  & 7);
1092 	BUILD_BUG_ON(UBIFS_DENT_NODE_SZ & 7);
1093 	BUILD_BUG_ON(UBIFS_XENT_NODE_SZ & 7);
1094 	BUILD_BUG_ON(UBIFS_DATA_NODE_SZ & 7);
1095 	BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ & 7);
1096 	BUILD_BUG_ON(UBIFS_SB_NODE_SZ   & 7);
1097 	BUILD_BUG_ON(UBIFS_MST_NODE_SZ  & 7);
1098 	BUILD_BUG_ON(UBIFS_REF_NODE_SZ  & 7);
1099 	BUILD_BUG_ON(UBIFS_CS_NODE_SZ   & 7);
1100 	BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ & 7);
1101 
1102 	BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ & 7);
1103 	BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ & 7);
1104 	BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ & 7);
1105 	BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ  & 7);
1106 	BUILD_BUG_ON(UBIFS_MAX_NODE_SZ      & 7);
1107 	BUILD_BUG_ON(MIN_WRITE_SZ           & 7);
1108 
1109 	/* Check min. node size */
1110 	BUILD_BUG_ON(UBIFS_INO_NODE_SZ  < MIN_WRITE_SZ);
1111 	BUILD_BUG_ON(UBIFS_DENT_NODE_SZ < MIN_WRITE_SZ);
1112 	BUILD_BUG_ON(UBIFS_XENT_NODE_SZ < MIN_WRITE_SZ);
1113 	BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ < MIN_WRITE_SZ);
1114 
1115 	BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
1116 	BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
1117 	BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ > UBIFS_MAX_NODE_SZ);
1118 	BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ  > UBIFS_MAX_NODE_SZ);
1119 
1120 	/* Defined node sizes */
1121 	BUILD_BUG_ON(UBIFS_SB_NODE_SZ  != 4096);
1122 	BUILD_BUG_ON(UBIFS_MST_NODE_SZ != 512);
1123 	BUILD_BUG_ON(UBIFS_INO_NODE_SZ != 160);
1124 	BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
1125 
1126 	/*
1127 	 * We use 2 bit wide bit-fields to store compression type, which should
1128 	 * be amended if more compressors are added. The bit-fields are:
1129 	 * @compr_type in 'struct ubifs_inode', @default_compr in
1130 	 * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'.
1131 	 */
1132 	BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > 4);
1133 
1134 	/*
1135 	 * We require that PAGE_CACHE_SIZE is greater-than-or-equal-to
1136 	 * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
1137 	 */
1138 	if (PAGE_CACHE_SIZE < UBIFS_BLOCK_SIZE) {
1139 		ubifs_err("VFS page cache size is %u bytes, but UBIFS requires"
1140 			  " at least 4096 bytes",
1141 			  (unsigned int)PAGE_CACHE_SIZE);
1142 		return -EINVAL;
1143 	}
1144 
1145 	err = -ENOMEM;
1146 
1147 	err = ubifs_compressors_init();
1148 	if (err)
1149 		goto out_shrinker;
1150 
1151 	return 0;
1152 
1153 out_shrinker:
1154 	return err;
1155 }
1156 
1157 /*
1158  * ubifsmount...
1159  */
1160 
1161 static struct file_system_type ubifs_fs_type = {
1162 	.name    = "ubifs",
1163 	.owner   = THIS_MODULE,
1164 	.get_sb  = ubifs_get_sb,
1165 };
1166 
1167 int ubifs_mount(char *vol_name)
1168 {
1169 	int flags;
1170 	char name[80] = "ubi:";
1171 	void *data;
1172 	struct vfsmount *mnt;
1173 	int ret;
1174 	struct ubifs_info *c;
1175 
1176 	/*
1177 	 * First unmount if allready mounted
1178 	 */
1179 	if (ubifs_sb)
1180 		ubifs_umount(ubifs_sb->s_fs_info);
1181 
1182 	INIT_LIST_HEAD(&ubifs_infos);
1183 	INIT_LIST_HEAD(&ubifs_fs_type.fs_supers);
1184 
1185 	/*
1186 	 * Mount in read-only mode
1187 	 */
1188 	flags = MS_RDONLY;
1189 	strcat(name, vol_name);
1190 	data = NULL;
1191 	mnt = NULL;
1192 	ret = ubifs_get_sb(&ubifs_fs_type, flags, name, data, mnt);
1193 	if (ret) {
1194 		ubifs_err("Error reading superblock on volume '%s' errno=%d!\n", name, ret);
1195 		return -1;
1196 	}
1197 
1198 	c = ubifs_sb->s_fs_info;
1199 	ubi_close_volume(c->ubi);
1200 
1201 	return 0;
1202 }
1203