xref: /openbmc/linux/fs/ubifs/super.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21e51764aSArtem Bityutskiy /*
31e51764aSArtem Bityutskiy  * This file is part of UBIFS.
41e51764aSArtem Bityutskiy  *
51e51764aSArtem Bityutskiy  * Copyright (C) 2006-2008 Nokia Corporation.
61e51764aSArtem Bityutskiy  *
71e51764aSArtem Bityutskiy  * Authors: Artem Bityutskiy (Битюцкий Артём)
81e51764aSArtem Bityutskiy  *          Adrian Hunter
91e51764aSArtem Bityutskiy  */
101e51764aSArtem Bityutskiy 
111e51764aSArtem Bityutskiy /*
121e51764aSArtem Bityutskiy  * This file implements UBIFS initialization and VFS superblock operations. Some
131e51764aSArtem Bityutskiy  * initialization stuff which is rather large and complex is placed at
141e51764aSArtem Bityutskiy  * corresponding subsystems, but most of it is here.
151e51764aSArtem Bityutskiy  */
161e51764aSArtem Bityutskiy 
171e51764aSArtem Bityutskiy #include <linux/init.h>
181e51764aSArtem Bityutskiy #include <linux/slab.h>
191e51764aSArtem Bityutskiy #include <linux/module.h>
201e51764aSArtem Bityutskiy #include <linux/ctype.h>
211e51764aSArtem Bityutskiy #include <linux/kthread.h>
221e51764aSArtem Bityutskiy #include <linux/parser.h>
231e51764aSArtem Bityutskiy #include <linux/seq_file.h>
241e51764aSArtem Bityutskiy #include <linux/mount.h>
254d61db4fSArtem Bityutskiy #include <linux/math64.h>
26304d427cSArtem Bityutskiy #include <linux/writeback.h>
271e51764aSArtem Bityutskiy #include "ubifs.h"
281e51764aSArtem Bityutskiy 
ubifs_default_version_set(const char * val,const struct kernel_param * kp)29a7a8f4a1SMartin Kaistra static int ubifs_default_version_set(const char *val, const struct kernel_param *kp)
30a7a8f4a1SMartin Kaistra {
31a7a8f4a1SMartin Kaistra 	int n = 0, ret;
32a7a8f4a1SMartin Kaistra 
33a7a8f4a1SMartin Kaistra 	ret = kstrtoint(val, 10, &n);
34a7a8f4a1SMartin Kaistra 	if (ret != 0 || n < 4 || n > UBIFS_FORMAT_VERSION)
35a7a8f4a1SMartin Kaistra 		return -EINVAL;
36a7a8f4a1SMartin Kaistra 	return param_set_int(val, kp);
37a7a8f4a1SMartin Kaistra }
38a7a8f4a1SMartin Kaistra 
39a7a8f4a1SMartin Kaistra static const struct kernel_param_ops ubifs_default_version_ops = {
40a7a8f4a1SMartin Kaistra 	.set = ubifs_default_version_set,
41a7a8f4a1SMartin Kaistra 	.get = param_get_int,
42a7a8f4a1SMartin Kaistra };
43a7a8f4a1SMartin Kaistra 
44a7a8f4a1SMartin Kaistra int ubifs_default_version = UBIFS_FORMAT_VERSION;
45a7a8f4a1SMartin Kaistra module_param_cb(default_version, &ubifs_default_version_ops, &ubifs_default_version, 0600);
46a7a8f4a1SMartin Kaistra 
4739ce81ceSArtem Bityutskiy /*
4839ce81ceSArtem Bityutskiy  * Maximum amount of memory we may 'kmalloc()' without worrying that we are
4939ce81ceSArtem Bityutskiy  * allocating too much.
5039ce81ceSArtem Bityutskiy  */
5139ce81ceSArtem Bityutskiy #define UBIFS_KMALLOC_OK (128*1024)
5239ce81ceSArtem Bityutskiy 
531e51764aSArtem Bityutskiy /* Slab cache for UBIFS inodes */
54e996bfd4SRichard Weinberger static struct kmem_cache *ubifs_inode_slab;
551e51764aSArtem Bityutskiy 
561e51764aSArtem Bityutskiy /* UBIFS TNC shrinker description */
571e51764aSArtem Bityutskiy static struct shrinker ubifs_shrinker_info = {
581ab6c499SDave Chinner 	.scan_objects = ubifs_shrink_scan,
591ab6c499SDave Chinner 	.count_objects = ubifs_shrink_count,
601e51764aSArtem Bityutskiy 	.seeks = DEFAULT_SEEKS,
611e51764aSArtem Bityutskiy };
621e51764aSArtem Bityutskiy 
631e51764aSArtem Bityutskiy /**
641e51764aSArtem Bityutskiy  * validate_inode - validate inode.
651e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
661e51764aSArtem Bityutskiy  * @inode: the inode to validate
671e51764aSArtem Bityutskiy  *
681e51764aSArtem Bityutskiy  * This is a helper function for 'ubifs_iget()' which validates various fields
691e51764aSArtem Bityutskiy  * of a newly built inode to make sure they contain sane values and prevent
701e51764aSArtem Bityutskiy  * possible vulnerabilities. Returns zero if the inode is all right and
711e51764aSArtem Bityutskiy  * a non-zero error code if not.
721e51764aSArtem Bityutskiy  */
validate_inode(struct ubifs_info * c,const struct inode * inode)731e51764aSArtem Bityutskiy static int validate_inode(struct ubifs_info *c, const struct inode *inode)
741e51764aSArtem Bityutskiy {
751e51764aSArtem Bityutskiy 	int err;
761e51764aSArtem Bityutskiy 	const struct ubifs_inode *ui = ubifs_inode(inode);
771e51764aSArtem Bityutskiy 
781e51764aSArtem Bityutskiy 	if (inode->i_size > c->max_inode_sz) {
79235c362bSSheng Yong 		ubifs_err(c, "inode is too large (%lld)",
801e51764aSArtem Bityutskiy 			  (long long)inode->i_size);
811e51764aSArtem Bityutskiy 		return 1;
821e51764aSArtem Bityutskiy 	}
831e51764aSArtem Bityutskiy 
84b793a8c8Shujianyang 	if (ui->compr_type >= UBIFS_COMPR_TYPES_CNT) {
85235c362bSSheng Yong 		ubifs_err(c, "unknown compression type %d", ui->compr_type);
861e51764aSArtem Bityutskiy 		return 2;
871e51764aSArtem Bityutskiy 	}
881e51764aSArtem Bityutskiy 
891e51764aSArtem Bityutskiy 	if (ui->xattr_names + ui->xattr_cnt > XATTR_LIST_MAX)
901e51764aSArtem Bityutskiy 		return 3;
911e51764aSArtem Bityutskiy 
921e51764aSArtem Bityutskiy 	if (ui->data_len < 0 || ui->data_len > UBIFS_MAX_INO_DATA)
931e51764aSArtem Bityutskiy 		return 4;
941e51764aSArtem Bityutskiy 
95a29fa9dfSArtem Bityutskiy 	if (ui->xattr && !S_ISREG(inode->i_mode))
961e51764aSArtem Bityutskiy 		return 5;
971e51764aSArtem Bityutskiy 
986eb61d58SRichard Weinberger 	if (!ubifs_compr_present(c, ui->compr_type)) {
99235c362bSSheng Yong 		ubifs_warn(c, "inode %lu uses '%s' compression, but it was not compiled in",
1006eb61d58SRichard Weinberger 			   inode->i_ino, ubifs_compr_name(c, ui->compr_type));
1011e51764aSArtem Bityutskiy 	}
1021e51764aSArtem Bityutskiy 
1031b51e983SArtem Bityutskiy 	err = dbg_check_dir(c, inode);
1041e51764aSArtem Bityutskiy 	return err;
1051e51764aSArtem Bityutskiy }
1061e51764aSArtem Bityutskiy 
ubifs_iget(struct super_block * sb,unsigned long inum)1071e51764aSArtem Bityutskiy struct inode *ubifs_iget(struct super_block *sb, unsigned long inum)
1081e51764aSArtem Bityutskiy {
1091e51764aSArtem Bityutskiy 	int err;
1101e51764aSArtem Bityutskiy 	union ubifs_key key;
1111e51764aSArtem Bityutskiy 	struct ubifs_ino_node *ino;
1121e51764aSArtem Bityutskiy 	struct ubifs_info *c = sb->s_fs_info;
1131e51764aSArtem Bityutskiy 	struct inode *inode;
1141e51764aSArtem Bityutskiy 	struct ubifs_inode *ui;
1151e51764aSArtem Bityutskiy 
1161e51764aSArtem Bityutskiy 	dbg_gen("inode %lu", inum);
1171e51764aSArtem Bityutskiy 
1181e51764aSArtem Bityutskiy 	inode = iget_locked(sb, inum);
1191e51764aSArtem Bityutskiy 	if (!inode)
1201e51764aSArtem Bityutskiy 		return ERR_PTR(-ENOMEM);
1211e51764aSArtem Bityutskiy 	if (!(inode->i_state & I_NEW))
1221e51764aSArtem Bityutskiy 		return inode;
1231e51764aSArtem Bityutskiy 	ui = ubifs_inode(inode);
1241e51764aSArtem Bityutskiy 
1251e51764aSArtem Bityutskiy 	ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
1261e51764aSArtem Bityutskiy 	if (!ino) {
1271e51764aSArtem Bityutskiy 		err = -ENOMEM;
1281e51764aSArtem Bityutskiy 		goto out;
1291e51764aSArtem Bityutskiy 	}
1301e51764aSArtem Bityutskiy 
1311e51764aSArtem Bityutskiy 	ino_key_init(c, &key, inode->i_ino);
1321e51764aSArtem Bityutskiy 
1331e51764aSArtem Bityutskiy 	err = ubifs_tnc_lookup(c, &key, ino);
1341e51764aSArtem Bityutskiy 	if (err)
1351e51764aSArtem Bityutskiy 		goto out_ino;
1361e51764aSArtem Bityutskiy 
1378c1c5f26SDongsheng Yang 	inode->i_flags |= S_NOCMTIME;
138e3d73deaSSascha Hauer 
139e3d73deaSSascha Hauer 	if (!IS_ENABLED(CONFIG_UBIFS_ATIME_SUPPORT))
1408c1c5f26SDongsheng Yang 		inode->i_flags |= S_NOATIME;
141e3d73deaSSascha Hauer 
142bfe86848SMiklos Szeredi 	set_nlink(inode, le32_to_cpu(ino->nlink));
14339241bebSEric W. Biederman 	i_uid_write(inode, le32_to_cpu(ino->uid));
14439241bebSEric W. Biederman 	i_gid_write(inode, le32_to_cpu(ino->gid));
1451e51764aSArtem Bityutskiy 	inode->i_atime.tv_sec  = (int64_t)le64_to_cpu(ino->atime_sec);
1461e51764aSArtem Bityutskiy 	inode->i_atime.tv_nsec = le32_to_cpu(ino->atime_nsec);
1471e51764aSArtem Bityutskiy 	inode->i_mtime.tv_sec  = (int64_t)le64_to_cpu(ino->mtime_sec);
1481e51764aSArtem Bityutskiy 	inode->i_mtime.tv_nsec = le32_to_cpu(ino->mtime_nsec);
149d07d3a7eSJeff Layton 	inode_set_ctime(inode, (int64_t)le64_to_cpu(ino->ctime_sec),
150d07d3a7eSJeff Layton 			le32_to_cpu(ino->ctime_nsec));
1511e51764aSArtem Bityutskiy 	inode->i_mode = le32_to_cpu(ino->mode);
1521e51764aSArtem Bityutskiy 	inode->i_size = le64_to_cpu(ino->size);
1531e51764aSArtem Bityutskiy 
1541e51764aSArtem Bityutskiy 	ui->data_len    = le32_to_cpu(ino->data_len);
1551e51764aSArtem Bityutskiy 	ui->flags       = le32_to_cpu(ino->flags);
1561e51764aSArtem Bityutskiy 	ui->compr_type  = le16_to_cpu(ino->compr_type);
1571e51764aSArtem Bityutskiy 	ui->creat_sqnum = le64_to_cpu(ino->creat_sqnum);
1581e51764aSArtem Bityutskiy 	ui->xattr_cnt   = le32_to_cpu(ino->xattr_cnt);
1591e51764aSArtem Bityutskiy 	ui->xattr_size  = le32_to_cpu(ino->xattr_size);
1601e51764aSArtem Bityutskiy 	ui->xattr_names = le32_to_cpu(ino->xattr_names);
1611e51764aSArtem Bityutskiy 	ui->synced_i_size = ui->ui_size = inode->i_size;
1621e51764aSArtem Bityutskiy 
1631e51764aSArtem Bityutskiy 	ui->xattr = (ui->flags & UBIFS_XATTR_FL) ? 1 : 0;
1641e51764aSArtem Bityutskiy 
1651e51764aSArtem Bityutskiy 	err = validate_inode(c, inode);
1661e51764aSArtem Bityutskiy 	if (err)
1671e51764aSArtem Bityutskiy 		goto out_invalid;
1681e51764aSArtem Bityutskiy 
1691e51764aSArtem Bityutskiy 	switch (inode->i_mode & S_IFMT) {
1701e51764aSArtem Bityutskiy 	case S_IFREG:
1711e51764aSArtem Bityutskiy 		inode->i_mapping->a_ops = &ubifs_file_address_operations;
1721e51764aSArtem Bityutskiy 		inode->i_op = &ubifs_file_inode_operations;
1731e51764aSArtem Bityutskiy 		inode->i_fop = &ubifs_file_operations;
1741e51764aSArtem Bityutskiy 		if (ui->xattr) {
1751e51764aSArtem Bityutskiy 			ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
1761e51764aSArtem Bityutskiy 			if (!ui->data) {
1771e51764aSArtem Bityutskiy 				err = -ENOMEM;
1781e51764aSArtem Bityutskiy 				goto out_ino;
1791e51764aSArtem Bityutskiy 			}
1801e51764aSArtem Bityutskiy 			memcpy(ui->data, ino->data, ui->data_len);
1811e51764aSArtem Bityutskiy 			((char *)ui->data)[ui->data_len] = '\0';
1821e51764aSArtem Bityutskiy 		} else if (ui->data_len != 0) {
1831e51764aSArtem Bityutskiy 			err = 10;
1841e51764aSArtem Bityutskiy 			goto out_invalid;
1851e51764aSArtem Bityutskiy 		}
1861e51764aSArtem Bityutskiy 		break;
1871e51764aSArtem Bityutskiy 	case S_IFDIR:
1881e51764aSArtem Bityutskiy 		inode->i_op  = &ubifs_dir_inode_operations;
1891e51764aSArtem Bityutskiy 		inode->i_fop = &ubifs_dir_operations;
1901e51764aSArtem Bityutskiy 		if (ui->data_len != 0) {
1911e51764aSArtem Bityutskiy 			err = 11;
1921e51764aSArtem Bityutskiy 			goto out_invalid;
1931e51764aSArtem Bityutskiy 		}
1941e51764aSArtem Bityutskiy 		break;
1951e51764aSArtem Bityutskiy 	case S_IFLNK:
1961e51764aSArtem Bityutskiy 		inode->i_op = &ubifs_symlink_inode_operations;
1971e51764aSArtem Bityutskiy 		if (ui->data_len <= 0 || ui->data_len > UBIFS_MAX_INO_DATA) {
1981e51764aSArtem Bityutskiy 			err = 12;
1991e51764aSArtem Bityutskiy 			goto out_invalid;
2001e51764aSArtem Bityutskiy 		}
2011e51764aSArtem Bityutskiy 		ui->data = kmalloc(ui->data_len + 1, GFP_NOFS);
2021e51764aSArtem Bityutskiy 		if (!ui->data) {
2031e51764aSArtem Bityutskiy 			err = -ENOMEM;
2041e51764aSArtem Bityutskiy 			goto out_ino;
2051e51764aSArtem Bityutskiy 		}
2061e51764aSArtem Bityutskiy 		memcpy(ui->data, ino->data, ui->data_len);
2071e51764aSArtem Bityutskiy 		((char *)ui->data)[ui->data_len] = '\0';
2081e51764aSArtem Bityutskiy 		break;
2091e51764aSArtem Bityutskiy 	case S_IFBLK:
2101e51764aSArtem Bityutskiy 	case S_IFCHR:
2111e51764aSArtem Bityutskiy 	{
2121e51764aSArtem Bityutskiy 		dev_t rdev;
2131e51764aSArtem Bityutskiy 		union ubifs_dev_desc *dev;
2141e51764aSArtem Bityutskiy 
2151e51764aSArtem Bityutskiy 		ui->data = kmalloc(sizeof(union ubifs_dev_desc), GFP_NOFS);
2161e51764aSArtem Bityutskiy 		if (!ui->data) {
2171e51764aSArtem Bityutskiy 			err = -ENOMEM;
2181e51764aSArtem Bityutskiy 			goto out_ino;
2191e51764aSArtem Bityutskiy 		}
2201e51764aSArtem Bityutskiy 
2211e51764aSArtem Bityutskiy 		dev = (union ubifs_dev_desc *)ino->data;
2221e51764aSArtem Bityutskiy 		if (ui->data_len == sizeof(dev->new))
2231e51764aSArtem Bityutskiy 			rdev = new_decode_dev(le32_to_cpu(dev->new));
2241e51764aSArtem Bityutskiy 		else if (ui->data_len == sizeof(dev->huge))
2251e51764aSArtem Bityutskiy 			rdev = huge_decode_dev(le64_to_cpu(dev->huge));
2261e51764aSArtem Bityutskiy 		else {
2271e51764aSArtem Bityutskiy 			err = 13;
2281e51764aSArtem Bityutskiy 			goto out_invalid;
2291e51764aSArtem Bityutskiy 		}
2301e51764aSArtem Bityutskiy 		memcpy(ui->data, ino->data, ui->data_len);
2311e51764aSArtem Bityutskiy 		inode->i_op = &ubifs_file_inode_operations;
2321e51764aSArtem Bityutskiy 		init_special_inode(inode, inode->i_mode, rdev);
2331e51764aSArtem Bityutskiy 		break;
2341e51764aSArtem Bityutskiy 	}
2351e51764aSArtem Bityutskiy 	case S_IFSOCK:
2361e51764aSArtem Bityutskiy 	case S_IFIFO:
2371e51764aSArtem Bityutskiy 		inode->i_op = &ubifs_file_inode_operations;
2381e51764aSArtem Bityutskiy 		init_special_inode(inode, inode->i_mode, 0);
2391e51764aSArtem Bityutskiy 		if (ui->data_len != 0) {
2401e51764aSArtem Bityutskiy 			err = 14;
2411e51764aSArtem Bityutskiy 			goto out_invalid;
2421e51764aSArtem Bityutskiy 		}
2431e51764aSArtem Bityutskiy 		break;
2441e51764aSArtem Bityutskiy 	default:
2451e51764aSArtem Bityutskiy 		err = 15;
2461e51764aSArtem Bityutskiy 		goto out_invalid;
2471e51764aSArtem Bityutskiy 	}
2481e51764aSArtem Bityutskiy 
2491e51764aSArtem Bityutskiy 	kfree(ino);
2501e51764aSArtem Bityutskiy 	ubifs_set_inode_flags(inode);
2511e51764aSArtem Bityutskiy 	unlock_new_inode(inode);
2521e51764aSArtem Bityutskiy 	return inode;
2531e51764aSArtem Bityutskiy 
2541e51764aSArtem Bityutskiy out_invalid:
255235c362bSSheng Yong 	ubifs_err(c, "inode %lu validation failed, error %d", inode->i_ino, err);
256a33e30a0SZhihao Cheng 	ubifs_dump_node(c, ino, UBIFS_MAX_INO_NODE_SZ);
257edf6be24SArtem Bityutskiy 	ubifs_dump_inode(c, inode);
2581e51764aSArtem Bityutskiy 	err = -EINVAL;
2591e51764aSArtem Bityutskiy out_ino:
2601e51764aSArtem Bityutskiy 	kfree(ino);
2611e51764aSArtem Bityutskiy out:
262235c362bSSheng Yong 	ubifs_err(c, "failed to read inode %lu, error %d", inode->i_ino, err);
2631e51764aSArtem Bityutskiy 	iget_failed(inode);
2641e51764aSArtem Bityutskiy 	return ERR_PTR(err);
2651e51764aSArtem Bityutskiy }
2661e51764aSArtem Bityutskiy 
ubifs_alloc_inode(struct super_block * sb)2671e51764aSArtem Bityutskiy static struct inode *ubifs_alloc_inode(struct super_block *sb)
2681e51764aSArtem Bityutskiy {
2691e51764aSArtem Bityutskiy 	struct ubifs_inode *ui;
2701e51764aSArtem Bityutskiy 
271fd60b288SMuchun Song 	ui = alloc_inode_sb(sb, ubifs_inode_slab, GFP_NOFS);
2721e51764aSArtem Bityutskiy 	if (!ui)
2731e51764aSArtem Bityutskiy 		return NULL;
2741e51764aSArtem Bityutskiy 
2751e51764aSArtem Bityutskiy 	memset((void *)ui + sizeof(struct inode), 0,
2761e51764aSArtem Bityutskiy 	       sizeof(struct ubifs_inode) - sizeof(struct inode));
2771e51764aSArtem Bityutskiy 	mutex_init(&ui->ui_mutex);
278f4e3634aSZhihao Cheng 	init_rwsem(&ui->xattr_sem);
2791e51764aSArtem Bityutskiy 	spin_lock_init(&ui->ui_lock);
2801e51764aSArtem Bityutskiy 	return &ui->vfs_inode;
2811e51764aSArtem Bityutskiy };
2821e51764aSArtem Bityutskiy 
ubifs_free_inode(struct inode * inode)283dc431759SAl Viro static void ubifs_free_inode(struct inode *inode)
284fa0d7e3dSNick Piggin {
285fa0d7e3dSNick Piggin 	struct ubifs_inode *ui = ubifs_inode(inode);
2862c58d548SEric Biggers 
2870cdc17ebSAl Viro 	kfree(ui->data);
2882c58d548SEric Biggers 	fscrypt_free_inode(inode);
2892c58d548SEric Biggers 
290fa0d7e3dSNick Piggin 	kmem_cache_free(ubifs_inode_slab, ui);
291fa0d7e3dSNick Piggin }
292fa0d7e3dSNick Piggin 
2931e51764aSArtem Bityutskiy /*
2941e51764aSArtem Bityutskiy  * Note, Linux write-back code calls this without 'i_mutex'.
2951e51764aSArtem Bityutskiy  */
ubifs_write_inode(struct inode * inode,struct writeback_control * wbc)296a9185b41SChristoph Hellwig static int ubifs_write_inode(struct inode *inode, struct writeback_control *wbc)
2971e51764aSArtem Bityutskiy {
298fbfa6c88SArtem Bityutskiy 	int err = 0;
2991e51764aSArtem Bityutskiy 	struct ubifs_info *c = inode->i_sb->s_fs_info;
3001e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
3011e51764aSArtem Bityutskiy 
3026eb61d58SRichard Weinberger 	ubifs_assert(c, !ui->xattr);
3031e51764aSArtem Bityutskiy 	if (is_bad_inode(inode))
3041e51764aSArtem Bityutskiy 		return 0;
3051e51764aSArtem Bityutskiy 
3061e51764aSArtem Bityutskiy 	mutex_lock(&ui->ui_mutex);
3071e51764aSArtem Bityutskiy 	/*
3081e51764aSArtem Bityutskiy 	 * Due to races between write-back forced by budgeting
3095c57f20bSArtem Bityutskiy 	 * (see 'sync_some_inodes()') and background write-back, the inode may
3101e51764aSArtem Bityutskiy 	 * have already been synchronized, do not do this again. This might
3111e51764aSArtem Bityutskiy 	 * also happen if it was synchronized in an VFS operation, e.g.
3121e51764aSArtem Bityutskiy 	 * 'ubifs_link()'.
3131e51764aSArtem Bityutskiy 	 */
3141e51764aSArtem Bityutskiy 	if (!ui->dirty) {
3151e51764aSArtem Bityutskiy 		mutex_unlock(&ui->ui_mutex);
3161e51764aSArtem Bityutskiy 		return 0;
3171e51764aSArtem Bityutskiy 	}
3181e51764aSArtem Bityutskiy 
319fbfa6c88SArtem Bityutskiy 	/*
320fbfa6c88SArtem Bityutskiy 	 * As an optimization, do not write orphan inodes to the media just
321fbfa6c88SArtem Bityutskiy 	 * because this is not needed.
322fbfa6c88SArtem Bityutskiy 	 */
323fbfa6c88SArtem Bityutskiy 	dbg_gen("inode %lu, mode %#x, nlink %u",
324fbfa6c88SArtem Bityutskiy 		inode->i_ino, (int)inode->i_mode, inode->i_nlink);
325fbfa6c88SArtem Bityutskiy 	if (inode->i_nlink) {
3261f28681aSArtem Bityutskiy 		err = ubifs_jnl_write_inode(c, inode);
3271e51764aSArtem Bityutskiy 		if (err)
328235c362bSSheng Yong 			ubifs_err(c, "can't write inode %lu, error %d",
329fbfa6c88SArtem Bityutskiy 				  inode->i_ino, err);
330e3c3efc2SArtem Bityutskiy 		else
331e3c3efc2SArtem Bityutskiy 			err = dbg_check_inode_size(c, inode, ui->ui_size);
332fbfa6c88SArtem Bityutskiy 	}
3331e51764aSArtem Bityutskiy 
3341e51764aSArtem Bityutskiy 	ui->dirty = 0;
3351e51764aSArtem Bityutskiy 	mutex_unlock(&ui->ui_mutex);
3361e51764aSArtem Bityutskiy 	ubifs_release_dirty_inode_budget(c, ui);
3371e51764aSArtem Bityutskiy 	return err;
3381e51764aSArtem Bityutskiy }
3391e51764aSArtem Bityutskiy 
ubifs_drop_inode(struct inode * inode)34062de2592SEric Biggers static int ubifs_drop_inode(struct inode *inode)
34162de2592SEric Biggers {
34262de2592SEric Biggers 	int drop = generic_drop_inode(inode);
34362de2592SEric Biggers 
34462de2592SEric Biggers 	if (!drop)
34562de2592SEric Biggers 		drop = fscrypt_drop_inode(inode);
34662de2592SEric Biggers 
34762de2592SEric Biggers 	return drop;
34862de2592SEric Biggers }
34962de2592SEric Biggers 
ubifs_evict_inode(struct inode * inode)350d640e1b5SAl Viro static void ubifs_evict_inode(struct inode *inode)
3511e51764aSArtem Bityutskiy {
3521e51764aSArtem Bityutskiy 	int err;
3531e51764aSArtem Bityutskiy 	struct ubifs_info *c = inode->i_sb->s_fs_info;
3541e0f358eSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
3551e51764aSArtem Bityutskiy 
3561e0f358eSArtem Bityutskiy 	if (ui->xattr)
3571e51764aSArtem Bityutskiy 		/*
3581e51764aSArtem Bityutskiy 		 * Extended attribute inode deletions are fully handled in
3591e51764aSArtem Bityutskiy 		 * 'ubifs_removexattr()'. These inodes are special and have
3601e51764aSArtem Bityutskiy 		 * limited usage, so there is nothing to do here.
3611e51764aSArtem Bityutskiy 		 */
3621e51764aSArtem Bityutskiy 		goto out;
3631e51764aSArtem Bityutskiy 
3647d32c2bbSArtem Bityutskiy 	dbg_gen("inode %lu, mode %#x", inode->i_ino, (int)inode->i_mode);
3656eb61d58SRichard Weinberger 	ubifs_assert(c, !atomic_read(&inode->i_count));
3661e51764aSArtem Bityutskiy 
36791b0abe3SJohannes Weiner 	truncate_inode_pages_final(&inode->i_data);
368d640e1b5SAl Viro 
369d640e1b5SAl Viro 	if (inode->i_nlink)
370d640e1b5SAl Viro 		goto done;
371d640e1b5SAl Viro 
3721e51764aSArtem Bityutskiy 	if (is_bad_inode(inode))
3731e51764aSArtem Bityutskiy 		goto out;
3741e51764aSArtem Bityutskiy 
3751e0f358eSArtem Bityutskiy 	ui->ui_size = inode->i_size = 0;
376de94eb55SArtem Bityutskiy 	err = ubifs_jnl_delete_inode(c, inode);
3771e51764aSArtem Bityutskiy 	if (err)
3781e51764aSArtem Bityutskiy 		/*
3791e51764aSArtem Bityutskiy 		 * Worst case we have a lost orphan inode wasting space, so a
3800a883a05SArtem Bityutskiy 		 * simple error message is OK here.
3811e51764aSArtem Bityutskiy 		 */
382235c362bSSheng Yong 		ubifs_err(c, "can't delete inode %lu, error %d",
383de94eb55SArtem Bityutskiy 			  inode->i_ino, err);
384de94eb55SArtem Bityutskiy 
3851e51764aSArtem Bityutskiy out:
3861e0f358eSArtem Bityutskiy 	if (ui->dirty)
3871e0f358eSArtem Bityutskiy 		ubifs_release_dirty_inode_budget(c, ui);
3886d6cb0d6SAdrian Hunter 	else {
3896d6cb0d6SAdrian Hunter 		/* We've deleted something - clean the "no space" flags */
390b137545cSArtem Bityutskiy 		c->bi.nospace = c->bi.nospace_rp = 0;
3916d6cb0d6SAdrian Hunter 		smp_wmb();
3926d6cb0d6SAdrian Hunter 	}
393d640e1b5SAl Viro done:
394dbd5768fSJan Kara 	clear_inode(inode);
3953d204e24SEric Biggers 	fscrypt_put_encryption_info(inode);
3961e51764aSArtem Bityutskiy }
3971e51764aSArtem Bityutskiy 
ubifs_dirty_inode(struct inode * inode,int flags)398aa385729SChristoph Hellwig static void ubifs_dirty_inode(struct inode *inode, int flags)
3991e51764aSArtem Bityutskiy {
4006eb61d58SRichard Weinberger 	struct ubifs_info *c = inode->i_sb->s_fs_info;
4011e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = ubifs_inode(inode);
4021e51764aSArtem Bityutskiy 
4036eb61d58SRichard Weinberger 	ubifs_assert(c, mutex_is_locked(&ui->ui_mutex));
4041e51764aSArtem Bityutskiy 	if (!ui->dirty) {
4051e51764aSArtem Bityutskiy 		ui->dirty = 1;
4061e51764aSArtem Bityutskiy 		dbg_gen("inode %lu",  inode->i_ino);
4071e51764aSArtem Bityutskiy 	}
4081e51764aSArtem Bityutskiy }
4091e51764aSArtem Bityutskiy 
ubifs_statfs(struct dentry * dentry,struct kstatfs * buf)4101e51764aSArtem Bityutskiy static int ubifs_statfs(struct dentry *dentry, struct kstatfs *buf)
4111e51764aSArtem Bityutskiy {
4121e51764aSArtem Bityutskiy 	struct ubifs_info *c = dentry->d_sb->s_fs_info;
4131e51764aSArtem Bityutskiy 	unsigned long long free;
4147c7cbadfSArtem Bityutskiy 	__le32 *uuid = (__le32 *)c->uuid;
4151e51764aSArtem Bityutskiy 
4167dad181bSArtem Bityutskiy 	free = ubifs_get_free_space(c);
4171e51764aSArtem Bityutskiy 	dbg_gen("free space %lld bytes (%lld blocks)",
4181e51764aSArtem Bityutskiy 		free, free >> UBIFS_BLOCK_SHIFT);
4191e51764aSArtem Bityutskiy 
4201e51764aSArtem Bityutskiy 	buf->f_type = UBIFS_SUPER_MAGIC;
4211e51764aSArtem Bityutskiy 	buf->f_bsize = UBIFS_BLOCK_SIZE;
4221e51764aSArtem Bityutskiy 	buf->f_blocks = c->block_cnt;
4231e51764aSArtem Bityutskiy 	buf->f_bfree = free >> UBIFS_BLOCK_SHIFT;
4241e51764aSArtem Bityutskiy 	if (free > c->report_rp_size)
4251e51764aSArtem Bityutskiy 		buf->f_bavail = (free - c->report_rp_size) >> UBIFS_BLOCK_SHIFT;
4261e51764aSArtem Bityutskiy 	else
4271e51764aSArtem Bityutskiy 		buf->f_bavail = 0;
4281e51764aSArtem Bityutskiy 	buf->f_files = 0;
4291e51764aSArtem Bityutskiy 	buf->f_ffree = 0;
4301e51764aSArtem Bityutskiy 	buf->f_namelen = UBIFS_MAX_NLEN;
4317c7cbadfSArtem Bityutskiy 	buf->f_fsid.val[0] = le32_to_cpu(uuid[0]) ^ le32_to_cpu(uuid[2]);
4327c7cbadfSArtem Bityutskiy 	buf->f_fsid.val[1] = le32_to_cpu(uuid[1]) ^ le32_to_cpu(uuid[3]);
4336eb61d58SRichard Weinberger 	ubifs_assert(c, buf->f_bfree <= c->block_cnt);
4341e51764aSArtem Bityutskiy 	return 0;
4351e51764aSArtem Bityutskiy }
4361e51764aSArtem Bityutskiy 
ubifs_show_options(struct seq_file * s,struct dentry * root)43734c80b1dSAl Viro static int ubifs_show_options(struct seq_file *s, struct dentry *root)
4381e51764aSArtem Bityutskiy {
43934c80b1dSAl Viro 	struct ubifs_info *c = root->d_sb->s_fs_info;
4401e51764aSArtem Bityutskiy 
4411e51764aSArtem Bityutskiy 	if (c->mount_opts.unmount_mode == 2)
442d4eb08ffSFabian Frederick 		seq_puts(s, ",fast_unmount");
4431e51764aSArtem Bityutskiy 	else if (c->mount_opts.unmount_mode == 1)
444d4eb08ffSFabian Frederick 		seq_puts(s, ",norm_unmount");
4451e51764aSArtem Bityutskiy 
4464793e7c5SAdrian Hunter 	if (c->mount_opts.bulk_read == 2)
447d4eb08ffSFabian Frederick 		seq_puts(s, ",bulk_read");
4484793e7c5SAdrian Hunter 	else if (c->mount_opts.bulk_read == 1)
449d4eb08ffSFabian Frederick 		seq_puts(s, ",no_bulk_read");
4504793e7c5SAdrian Hunter 
4512953e73fSAdrian Hunter 	if (c->mount_opts.chk_data_crc == 2)
452d4eb08ffSFabian Frederick 		seq_puts(s, ",chk_data_crc");
4532953e73fSAdrian Hunter 	else if (c->mount_opts.chk_data_crc == 1)
454d4eb08ffSFabian Frederick 		seq_puts(s, ",no_chk_data_crc");
4552953e73fSAdrian Hunter 
456553dea4dSArtem Bityutskiy 	if (c->mount_opts.override_compr) {
457fcabb347SHunter Adrian 		seq_printf(s, ",compr=%s",
4586eb61d58SRichard Weinberger 			   ubifs_compr_name(c, c->mount_opts.compr_type));
459553dea4dSArtem Bityutskiy 	}
460553dea4dSArtem Bityutskiy 
461c38c5a7fSRichard Weinberger 	seq_printf(s, ",assert=%s", ubifs_assert_action_name(c));
462319c1042SRabin Vincent 	seq_printf(s, ",ubi=%d,vol=%d", c->vi.ubi_num, c->vi.vol_id);
463319c1042SRabin Vincent 
4641e51764aSArtem Bityutskiy 	return 0;
4651e51764aSArtem Bityutskiy }
4661e51764aSArtem Bityutskiy 
ubifs_sync_fs(struct super_block * sb,int wait)4671e51764aSArtem Bityutskiy static int ubifs_sync_fs(struct super_block *sb, int wait)
4681e51764aSArtem Bityutskiy {
469f1038300SArtem Bityutskiy 	int i, err;
4701e51764aSArtem Bityutskiy 	struct ubifs_info *c = sb->s_fs_info;
471304d427cSArtem Bityutskiy 
472e8ea1759SArtem Bityutskiy 	/*
473dedb0d48SArtem Bityutskiy 	 * Zero @wait is just an advisory thing to help the file system shove
474dedb0d48SArtem Bityutskiy 	 * lots of data into the queues, and there will be the second
475e8ea1759SArtem Bityutskiy 	 * '->sync_fs()' call, with non-zero @wait.
476e8ea1759SArtem Bityutskiy 	 */
477dedb0d48SArtem Bityutskiy 	if (!wait)
478dedb0d48SArtem Bityutskiy 		return 0;
479e8ea1759SArtem Bityutskiy 
480f1038300SArtem Bityutskiy 	/*
4813eb14297SAdrian Hunter 	 * Synchronize write buffers, because 'ubifs_run_commit()' does not
4823eb14297SAdrian Hunter 	 * do this if it waits for an already running commit.
4833eb14297SAdrian Hunter 	 */
4843eb14297SAdrian Hunter 	for (i = 0; i < c->jhead_cnt; i++) {
4853eb14297SAdrian Hunter 		err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
4863eb14297SAdrian Hunter 		if (err)
4873eb14297SAdrian Hunter 			return err;
4883eb14297SAdrian Hunter 	}
4893eb14297SAdrian Hunter 
490887ee171SArtem Bityutskiy 	/*
491887ee171SArtem Bityutskiy 	 * Strictly speaking, it is not necessary to commit the journal here,
492887ee171SArtem Bityutskiy 	 * synchronizing write-buffers would be enough. But committing makes
493887ee171SArtem Bityutskiy 	 * UBIFS free space predictions much more accurate, so we want to let
494887ee171SArtem Bityutskiy 	 * the user be able to get more accurate results of 'statfs()' after
495887ee171SArtem Bityutskiy 	 * they synchronize the file system.
496887ee171SArtem Bityutskiy 	 */
497403e12abSArtem Bityutskiy 	err = ubifs_run_commit(c);
498403e12abSArtem Bityutskiy 	if (err)
499403e12abSArtem Bityutskiy 		return err;
500403e12abSArtem Bityutskiy 
501cb5c6a2bSArtem Bityutskiy 	return ubi_sync(c->vi.ubi_num);
5021e51764aSArtem Bityutskiy }
5031e51764aSArtem Bityutskiy 
5041e51764aSArtem Bityutskiy /**
5051e51764aSArtem Bityutskiy  * init_constants_early - initialize UBIFS constants.
5061e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
5071e51764aSArtem Bityutskiy  *
5081e51764aSArtem Bityutskiy  * This function initialize UBIFS constants which do not need the superblock to
5091e51764aSArtem Bityutskiy  * be read. It also checks that the UBI volume satisfies basic UBIFS
5101e51764aSArtem Bityutskiy  * requirements. Returns zero in case of success and a negative error code in
5111e51764aSArtem Bityutskiy  * case of failure.
5121e51764aSArtem Bityutskiy  */
init_constants_early(struct ubifs_info * c)5131e51764aSArtem Bityutskiy static int init_constants_early(struct ubifs_info *c)
5141e51764aSArtem Bityutskiy {
5151e51764aSArtem Bityutskiy 	if (c->vi.corrupted) {
516235c362bSSheng Yong 		ubifs_warn(c, "UBI volume is corrupted - read-only mode");
5171e51764aSArtem Bityutskiy 		c->ro_media = 1;
5181e51764aSArtem Bityutskiy 	}
5191e51764aSArtem Bityutskiy 
5201e51764aSArtem Bityutskiy 	if (c->di.ro_mode) {
521235c362bSSheng Yong 		ubifs_msg(c, "read-only UBI device");
5221e51764aSArtem Bityutskiy 		c->ro_media = 1;
5231e51764aSArtem Bityutskiy 	}
5241e51764aSArtem Bityutskiy 
5251e51764aSArtem Bityutskiy 	if (c->vi.vol_type == UBI_STATIC_VOLUME) {
526235c362bSSheng Yong 		ubifs_msg(c, "static UBI volume - read-only mode");
5271e51764aSArtem Bityutskiy 		c->ro_media = 1;
5281e51764aSArtem Bityutskiy 	}
5291e51764aSArtem Bityutskiy 
5301e51764aSArtem Bityutskiy 	c->leb_cnt = c->vi.size;
5311e51764aSArtem Bityutskiy 	c->leb_size = c->vi.usable_leb_size;
532ca2ec61dSArtem Bityutskiy 	c->leb_start = c->di.leb_start;
5331e51764aSArtem Bityutskiy 	c->half_leb_size = c->leb_size / 2;
5341e51764aSArtem Bityutskiy 	c->min_io_size = c->di.min_io_size;
5351e51764aSArtem Bityutskiy 	c->min_io_shift = fls(c->min_io_size) - 1;
5363e8e2e0cSArtem Bityutskiy 	c->max_write_size = c->di.max_write_size;
5373e8e2e0cSArtem Bityutskiy 	c->max_write_shift = fls(c->max_write_size) - 1;
5381e51764aSArtem Bityutskiy 
5391e51764aSArtem Bityutskiy 	if (c->leb_size < UBIFS_MIN_LEB_SZ) {
540dccbc919SDaniel Golle 		ubifs_errc(c, "too small LEBs (%d bytes), min. is %d bytes",
5411e51764aSArtem Bityutskiy 			   c->leb_size, UBIFS_MIN_LEB_SZ);
5421e51764aSArtem Bityutskiy 		return -EINVAL;
5431e51764aSArtem Bityutskiy 	}
5441e51764aSArtem Bityutskiy 
5451e51764aSArtem Bityutskiy 	if (c->leb_cnt < UBIFS_MIN_LEB_CNT) {
546dccbc919SDaniel Golle 		ubifs_errc(c, "too few LEBs (%d), min. is %d",
5471e51764aSArtem Bityutskiy 			   c->leb_cnt, UBIFS_MIN_LEB_CNT);
5481e51764aSArtem Bityutskiy 		return -EINVAL;
5491e51764aSArtem Bityutskiy 	}
5501e51764aSArtem Bityutskiy 
5511e51764aSArtem Bityutskiy 	if (!is_power_of_2(c->min_io_size)) {
552dccbc919SDaniel Golle 		ubifs_errc(c, "bad min. I/O size %d", c->min_io_size);
5531e51764aSArtem Bityutskiy 		return -EINVAL;
5541e51764aSArtem Bityutskiy 	}
5551e51764aSArtem Bityutskiy 
5561e51764aSArtem Bityutskiy 	/*
5573e8e2e0cSArtem Bityutskiy 	 * Maximum write size has to be greater or equivalent to min. I/O
5583e8e2e0cSArtem Bityutskiy 	 * size, and be multiple of min. I/O size.
5593e8e2e0cSArtem Bityutskiy 	 */
5603e8e2e0cSArtem Bityutskiy 	if (c->max_write_size < c->min_io_size ||
5613e8e2e0cSArtem Bityutskiy 	    c->max_write_size % c->min_io_size ||
5623e8e2e0cSArtem Bityutskiy 	    !is_power_of_2(c->max_write_size)) {
563dccbc919SDaniel Golle 		ubifs_errc(c, "bad write buffer size %d for %d min. I/O unit",
5643e8e2e0cSArtem Bityutskiy 			   c->max_write_size, c->min_io_size);
5653e8e2e0cSArtem Bityutskiy 		return -EINVAL;
5663e8e2e0cSArtem Bityutskiy 	}
5673e8e2e0cSArtem Bityutskiy 
5683e8e2e0cSArtem Bityutskiy 	/*
5691e51764aSArtem Bityutskiy 	 * UBIFS aligns all node to 8-byte boundary, so to make function in
5701e51764aSArtem Bityutskiy 	 * io.c simpler, assume minimum I/O unit size to be 8 bytes if it is
5711e51764aSArtem Bityutskiy 	 * less than 8.
5721e51764aSArtem Bityutskiy 	 */
5731e51764aSArtem Bityutskiy 	if (c->min_io_size < 8) {
5741e51764aSArtem Bityutskiy 		c->min_io_size = 8;
5751e51764aSArtem Bityutskiy 		c->min_io_shift = 3;
5763e8e2e0cSArtem Bityutskiy 		if (c->max_write_size < c->min_io_size) {
5773e8e2e0cSArtem Bityutskiy 			c->max_write_size = c->min_io_size;
5783e8e2e0cSArtem Bityutskiy 			c->max_write_shift = c->min_io_shift;
5793e8e2e0cSArtem Bityutskiy 		}
5801e51764aSArtem Bityutskiy 	}
5811e51764aSArtem Bityutskiy 
5821e51764aSArtem Bityutskiy 	c->ref_node_alsz = ALIGN(UBIFS_REF_NODE_SZ, c->min_io_size);
5831e51764aSArtem Bityutskiy 	c->mst_node_alsz = ALIGN(UBIFS_MST_NODE_SZ, c->min_io_size);
5841e51764aSArtem Bityutskiy 
5851e51764aSArtem Bityutskiy 	/*
5861e51764aSArtem Bityutskiy 	 * Initialize node length ranges which are mostly needed for node
5871e51764aSArtem Bityutskiy 	 * length validation.
5881e51764aSArtem Bityutskiy 	 */
5891e51764aSArtem Bityutskiy 	c->ranges[UBIFS_PAD_NODE].len  = UBIFS_PAD_NODE_SZ;
5901e51764aSArtem Bityutskiy 	c->ranges[UBIFS_SB_NODE].len   = UBIFS_SB_NODE_SZ;
5911e51764aSArtem Bityutskiy 	c->ranges[UBIFS_MST_NODE].len  = UBIFS_MST_NODE_SZ;
5921e51764aSArtem Bityutskiy 	c->ranges[UBIFS_REF_NODE].len  = UBIFS_REF_NODE_SZ;
5931e51764aSArtem Bityutskiy 	c->ranges[UBIFS_TRUN_NODE].len = UBIFS_TRUN_NODE_SZ;
5941e51764aSArtem Bityutskiy 	c->ranges[UBIFS_CS_NODE].len   = UBIFS_CS_NODE_SZ;
595d8a22773SSascha Hauer 	c->ranges[UBIFS_AUTH_NODE].min_len = UBIFS_AUTH_NODE_SZ;
596d8a22773SSascha Hauer 	c->ranges[UBIFS_AUTH_NODE].max_len = UBIFS_AUTH_NODE_SZ +
597d8a22773SSascha Hauer 				UBIFS_MAX_HMAC_LEN;
598817aa094SSascha Hauer 	c->ranges[UBIFS_SIG_NODE].min_len = UBIFS_SIG_NODE_SZ;
599817aa094SSascha Hauer 	c->ranges[UBIFS_SIG_NODE].max_len = c->leb_size - UBIFS_SB_NODE_SZ;
6001e51764aSArtem Bityutskiy 
6011e51764aSArtem Bityutskiy 	c->ranges[UBIFS_INO_NODE].min_len  = UBIFS_INO_NODE_SZ;
6021e51764aSArtem Bityutskiy 	c->ranges[UBIFS_INO_NODE].max_len  = UBIFS_MAX_INO_NODE_SZ;
6031e51764aSArtem Bityutskiy 	c->ranges[UBIFS_ORPH_NODE].min_len =
6041e51764aSArtem Bityutskiy 				UBIFS_ORPH_NODE_SZ + sizeof(__le64);
6051e51764aSArtem Bityutskiy 	c->ranges[UBIFS_ORPH_NODE].max_len = c->leb_size;
6061e51764aSArtem Bityutskiy 	c->ranges[UBIFS_DENT_NODE].min_len = UBIFS_DENT_NODE_SZ;
6071e51764aSArtem Bityutskiy 	c->ranges[UBIFS_DENT_NODE].max_len = UBIFS_MAX_DENT_NODE_SZ;
6081e51764aSArtem Bityutskiy 	c->ranges[UBIFS_XENT_NODE].min_len = UBIFS_XENT_NODE_SZ;
6091e51764aSArtem Bityutskiy 	c->ranges[UBIFS_XENT_NODE].max_len = UBIFS_MAX_XENT_NODE_SZ;
6101e51764aSArtem Bityutskiy 	c->ranges[UBIFS_DATA_NODE].min_len = UBIFS_DATA_NODE_SZ;
6111e51764aSArtem Bityutskiy 	c->ranges[UBIFS_DATA_NODE].max_len = UBIFS_MAX_DATA_NODE_SZ;
6121e51764aSArtem Bityutskiy 	/*
6131e51764aSArtem Bityutskiy 	 * Minimum indexing node size is amended later when superblock is
6141e51764aSArtem Bityutskiy 	 * read and the key length is known.
6151e51764aSArtem Bityutskiy 	 */
6161e51764aSArtem Bityutskiy 	c->ranges[UBIFS_IDX_NODE].min_len = UBIFS_IDX_NODE_SZ + UBIFS_BRANCH_SZ;
6171e51764aSArtem Bityutskiy 	/*
6181e51764aSArtem Bityutskiy 	 * Maximum indexing node size is amended later when superblock is
6191e51764aSArtem Bityutskiy 	 * read and the fanout is known.
6201e51764aSArtem Bityutskiy 	 */
6211e51764aSArtem Bityutskiy 	c->ranges[UBIFS_IDX_NODE].max_len = INT_MAX;
6221e51764aSArtem Bityutskiy 
6231e51764aSArtem Bityutskiy 	/*
6247078202eSArtem Bityutskiy 	 * Initialize dead and dark LEB space watermarks. See gc.c for comments
6257078202eSArtem Bityutskiy 	 * about these values.
6261e51764aSArtem Bityutskiy 	 */
6271e51764aSArtem Bityutskiy 	c->dead_wm = ALIGN(MIN_WRITE_SZ, c->min_io_size);
6281e51764aSArtem Bityutskiy 	c->dark_wm = ALIGN(UBIFS_MAX_NODE_SZ, c->min_io_size);
6291e51764aSArtem Bityutskiy 
6309bbb5726SArtem Bityutskiy 	/*
6319bbb5726SArtem Bityutskiy 	 * Calculate how many bytes would be wasted at the end of LEB if it was
6329bbb5726SArtem Bityutskiy 	 * fully filled with data nodes of maximum size. This is used in
6339bbb5726SArtem Bityutskiy 	 * calculations when reporting free space.
6349bbb5726SArtem Bityutskiy 	 */
6359bbb5726SArtem Bityutskiy 	c->leb_overhead = c->leb_size % UBIFS_MAX_DATA_NODE_SZ;
63639ce81ceSArtem Bityutskiy 
6374793e7c5SAdrian Hunter 	/* Buffer size for bulk-reads */
6386c0c42cdSArtem Bityutskiy 	c->max_bu_buf_len = UBIFS_MAX_BULK_READ * UBIFS_MAX_DATA_NODE_SZ;
6396c0c42cdSArtem Bityutskiy 	if (c->max_bu_buf_len > c->leb_size)
6406c0c42cdSArtem Bityutskiy 		c->max_bu_buf_len = c->leb_size;
641377e208fSRichard Weinberger 
642377e208fSRichard Weinberger 	/* Log is ready, preserve one LEB for commits. */
643377e208fSRichard Weinberger 	c->min_log_bytes = c->leb_size;
644377e208fSRichard Weinberger 
6451e51764aSArtem Bityutskiy 	return 0;
6461e51764aSArtem Bityutskiy }
6471e51764aSArtem Bityutskiy 
6481e51764aSArtem Bityutskiy /**
6491e51764aSArtem Bityutskiy  * bud_wbuf_callback - bud LEB write-buffer synchronization call-back.
6501e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
6511e51764aSArtem Bityutskiy  * @lnum: LEB the write-buffer was synchronized to
6521e51764aSArtem Bityutskiy  * @free: how many free bytes left in this LEB
6531e51764aSArtem Bityutskiy  * @pad: how many bytes were padded
6541e51764aSArtem Bityutskiy  *
6551e51764aSArtem Bityutskiy  * This is a callback function which is called by the I/O unit when the
6561e51764aSArtem Bityutskiy  * write-buffer is synchronized. We need this to correctly maintain space
6571e51764aSArtem Bityutskiy  * accounting in bud logical eraseblocks. This function returns zero in case of
6581e51764aSArtem Bityutskiy  * success and a negative error code in case of failure.
6591e51764aSArtem Bityutskiy  *
6601e51764aSArtem Bityutskiy  * This function actually belongs to the journal, but we keep it here because
6611e51764aSArtem Bityutskiy  * we want to keep it static.
6621e51764aSArtem Bityutskiy  */
bud_wbuf_callback(struct ubifs_info * c,int lnum,int free,int pad)6631e51764aSArtem Bityutskiy static int bud_wbuf_callback(struct ubifs_info *c, int lnum, int free, int pad)
6641e51764aSArtem Bityutskiy {
6651e51764aSArtem Bityutskiy 	return ubifs_update_one_lp(c, lnum, free, pad, 0, 0);
6661e51764aSArtem Bityutskiy }
6671e51764aSArtem Bityutskiy 
6681e51764aSArtem Bityutskiy /*
66979807d07SArtem Bityutskiy  * init_constants_sb - initialize UBIFS constants.
6701e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
6711e51764aSArtem Bityutskiy  *
6721e51764aSArtem Bityutskiy  * This is a helper function which initializes various UBIFS constants after
6731e51764aSArtem Bityutskiy  * the superblock has been read. It also checks various UBIFS parameters and
6741e51764aSArtem Bityutskiy  * makes sure they are all right. Returns zero in case of success and a
6751e51764aSArtem Bityutskiy  * negative error code in case of failure.
6761e51764aSArtem Bityutskiy  */
init_constants_sb(struct ubifs_info * c)67779807d07SArtem Bityutskiy static int init_constants_sb(struct ubifs_info *c)
6781e51764aSArtem Bityutskiy {
6791e51764aSArtem Bityutskiy 	int tmp, err;
6804d61db4fSArtem Bityutskiy 	long long tmp64;
6811e51764aSArtem Bityutskiy 
6821e51764aSArtem Bityutskiy 	c->main_bytes = (long long)c->main_lebs * c->leb_size;
6831e51764aSArtem Bityutskiy 	c->max_znode_sz = sizeof(struct ubifs_znode) +
6841e51764aSArtem Bityutskiy 				c->fanout * sizeof(struct ubifs_zbranch);
6851e51764aSArtem Bityutskiy 
6861e51764aSArtem Bityutskiy 	tmp = ubifs_idx_node_sz(c, 1);
6871e51764aSArtem Bityutskiy 	c->ranges[UBIFS_IDX_NODE].min_len = tmp;
6881e51764aSArtem Bityutskiy 	c->min_idx_node_sz = ALIGN(tmp, 8);
6891e51764aSArtem Bityutskiy 
6901e51764aSArtem Bityutskiy 	tmp = ubifs_idx_node_sz(c, c->fanout);
6911e51764aSArtem Bityutskiy 	c->ranges[UBIFS_IDX_NODE].max_len = tmp;
6921e51764aSArtem Bityutskiy 	c->max_idx_node_sz = ALIGN(tmp, 8);
6931e51764aSArtem Bityutskiy 
6941e51764aSArtem Bityutskiy 	/* Make sure LEB size is large enough to fit full commit */
6951e51764aSArtem Bityutskiy 	tmp = UBIFS_CS_NODE_SZ + UBIFS_REF_NODE_SZ * c->jhead_cnt;
6961e51764aSArtem Bityutskiy 	tmp = ALIGN(tmp, c->min_io_size);
6971e51764aSArtem Bityutskiy 	if (tmp > c->leb_size) {
698235c362bSSheng Yong 		ubifs_err(c, "too small LEB size %d, at least %d needed",
6991e51764aSArtem Bityutskiy 			  c->leb_size, tmp);
7001e51764aSArtem Bityutskiy 		return -EINVAL;
7011e51764aSArtem Bityutskiy 	}
7021e51764aSArtem Bityutskiy 
7031e51764aSArtem Bityutskiy 	/*
7041e51764aSArtem Bityutskiy 	 * Make sure that the log is large enough to fit reference nodes for
7051e51764aSArtem Bityutskiy 	 * all buds plus one reserved LEB.
7061e51764aSArtem Bityutskiy 	 */
7074d61db4fSArtem Bityutskiy 	tmp64 = c->max_bud_bytes + c->leb_size - 1;
7084d61db4fSArtem Bityutskiy 	c->max_bud_cnt = div_u64(tmp64, c->leb_size);
7091e51764aSArtem Bityutskiy 	tmp = (c->ref_node_alsz * c->max_bud_cnt + c->leb_size - 1);
7101e51764aSArtem Bityutskiy 	tmp /= c->leb_size;
7111e51764aSArtem Bityutskiy 	tmp += 1;
7121e51764aSArtem Bityutskiy 	if (c->log_lebs < tmp) {
713235c362bSSheng Yong 		ubifs_err(c, "too small log %d LEBs, required min. %d LEBs",
7141e51764aSArtem Bityutskiy 			  c->log_lebs, tmp);
7151e51764aSArtem Bityutskiy 		return -EINVAL;
7161e51764aSArtem Bityutskiy 	}
7171e51764aSArtem Bityutskiy 
7181e51764aSArtem Bityutskiy 	/*
7191e51764aSArtem Bityutskiy 	 * When budgeting we assume worst-case scenarios when the pages are not
7201e51764aSArtem Bityutskiy 	 * be compressed and direntries are of the maximum size.
7211e51764aSArtem Bityutskiy 	 *
7221e51764aSArtem Bityutskiy 	 * Note, data, which may be stored in inodes is budgeted separately, so
723b137545cSArtem Bityutskiy 	 * it is not included into 'c->bi.inode_budget'.
7241e51764aSArtem Bityutskiy 	 */
725b137545cSArtem Bityutskiy 	c->bi.page_budget = UBIFS_MAX_DATA_NODE_SZ * UBIFS_BLOCKS_PER_PAGE;
726b137545cSArtem Bityutskiy 	c->bi.inode_budget = UBIFS_INO_NODE_SZ;
727b137545cSArtem Bityutskiy 	c->bi.dent_budget = UBIFS_MAX_DENT_NODE_SZ;
7281e51764aSArtem Bityutskiy 
7291e51764aSArtem Bityutskiy 	/*
7301e51764aSArtem Bityutskiy 	 * When the amount of flash space used by buds becomes
7311e51764aSArtem Bityutskiy 	 * 'c->max_bud_bytes', UBIFS just blocks all writers and starts commit.
7321e51764aSArtem Bityutskiy 	 * The writers are unblocked when the commit is finished. To avoid
7331e51764aSArtem Bityutskiy 	 * writers to be blocked UBIFS initiates background commit in advance,
7341e51764aSArtem Bityutskiy 	 * when number of bud bytes becomes above the limit defined below.
7351e51764aSArtem Bityutskiy 	 */
7361e51764aSArtem Bityutskiy 	c->bg_bud_bytes = (c->max_bud_bytes * 13) >> 4;
7371e51764aSArtem Bityutskiy 
7381e51764aSArtem Bityutskiy 	/*
7391e51764aSArtem Bityutskiy 	 * Ensure minimum journal size. All the bytes in the journal heads are
7401e51764aSArtem Bityutskiy 	 * considered to be used, when calculating the current journal usage.
7411e51764aSArtem Bityutskiy 	 * Consequently, if the journal is too small, UBIFS will treat it as
7421e51764aSArtem Bityutskiy 	 * always full.
7431e51764aSArtem Bityutskiy 	 */
7444d61db4fSArtem Bityutskiy 	tmp64 = (long long)(c->jhead_cnt + 1) * c->leb_size + 1;
7451e51764aSArtem Bityutskiy 	if (c->bg_bud_bytes < tmp64)
7461e51764aSArtem Bityutskiy 		c->bg_bud_bytes = tmp64;
7471e51764aSArtem Bityutskiy 	if (c->max_bud_bytes < tmp64 + c->leb_size)
7481e51764aSArtem Bityutskiy 		c->max_bud_bytes = tmp64 + c->leb_size;
7491e51764aSArtem Bityutskiy 
7501e51764aSArtem Bityutskiy 	err = ubifs_calc_lpt_geom(c);
7511e51764aSArtem Bityutskiy 	if (err)
7521e51764aSArtem Bityutskiy 		return err;
7531e51764aSArtem Bityutskiy 
754fb1cd01aSArtem Bityutskiy 	/* Initialize effective LEB size used in budgeting calculations */
755fb1cd01aSArtem Bityutskiy 	c->idx_leb_size = c->leb_size - c->max_idx_node_sz;
75679807d07SArtem Bityutskiy 	return 0;
75779807d07SArtem Bityutskiy }
75879807d07SArtem Bityutskiy 
75979807d07SArtem Bityutskiy /*
76079807d07SArtem Bityutskiy  * init_constants_master - initialize UBIFS constants.
76179807d07SArtem Bityutskiy  * @c: UBIFS file-system description object
76279807d07SArtem Bityutskiy  *
76379807d07SArtem Bityutskiy  * This is a helper function which initializes various UBIFS constants after
76479807d07SArtem Bityutskiy  * the master node has been read. It also checks various UBIFS parameters and
76579807d07SArtem Bityutskiy  * makes sure they are all right.
76679807d07SArtem Bityutskiy  */
init_constants_master(struct ubifs_info * c)76779807d07SArtem Bityutskiy static void init_constants_master(struct ubifs_info *c)
76879807d07SArtem Bityutskiy {
76979807d07SArtem Bityutskiy 	long long tmp64;
77079807d07SArtem Bityutskiy 
771b137545cSArtem Bityutskiy 	c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
772fb1cd01aSArtem Bityutskiy 	c->report_rp_size = ubifs_reported_space(c, c->rp_size);
7731e51764aSArtem Bityutskiy 
7741e51764aSArtem Bityutskiy 	/*
7751e51764aSArtem Bityutskiy 	 * Calculate total amount of FS blocks. This number is not used
7761e51764aSArtem Bityutskiy 	 * internally because it does not make much sense for UBIFS, but it is
7771e51764aSArtem Bityutskiy 	 * necessary to report something for the 'statfs()' call.
7781e51764aSArtem Bityutskiy 	 *
7797dad181bSArtem Bityutskiy 	 * Subtract the LEB reserved for GC, the LEB which is reserved for
780*074b310fSZhihao Cheng 	 * deletions, minimum LEBs for the index, the LEBs which are reserved
781*074b310fSZhihao Cheng 	 * for each journal head.
7821e51764aSArtem Bityutskiy 	 */
783*074b310fSZhihao Cheng 	tmp64 = c->main_lebs - 1 - 1 - MIN_INDEX_LEBS - c->jhead_cnt;
7844d61db4fSArtem Bityutskiy 	tmp64 *= (long long)c->leb_size - c->leb_overhead;
7851e51764aSArtem Bityutskiy 	tmp64 = ubifs_reported_space(c, tmp64);
7861e51764aSArtem Bityutskiy 	c->block_cnt = tmp64 >> UBIFS_BLOCK_SHIFT;
7871e51764aSArtem Bityutskiy }
7881e51764aSArtem Bityutskiy 
7891e51764aSArtem Bityutskiy /**
7901e51764aSArtem Bityutskiy  * take_gc_lnum - reserve GC LEB.
7911e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
7921e51764aSArtem Bityutskiy  *
793b4978e94SArtem Bityutskiy  * This function ensures that the LEB reserved for garbage collection is marked
794b4978e94SArtem Bityutskiy  * as "taken" in lprops. We also have to set free space to LEB size and dirty
795b4978e94SArtem Bityutskiy  * space to zero, because lprops may contain out-of-date information if the
796b4978e94SArtem Bityutskiy  * file-system was un-mounted before it has been committed. This function
797b4978e94SArtem Bityutskiy  * returns zero in case of success and a negative error code in case of
798b4978e94SArtem Bityutskiy  * failure.
7991e51764aSArtem Bityutskiy  */
take_gc_lnum(struct ubifs_info * c)8001e51764aSArtem Bityutskiy static int take_gc_lnum(struct ubifs_info *c)
8011e51764aSArtem Bityutskiy {
8021e51764aSArtem Bityutskiy 	int err;
8031e51764aSArtem Bityutskiy 
8041e51764aSArtem Bityutskiy 	if (c->gc_lnum == -1) {
805235c362bSSheng Yong 		ubifs_err(c, "no LEB for GC");
8061e51764aSArtem Bityutskiy 		return -EINVAL;
8071e51764aSArtem Bityutskiy 	}
8081e51764aSArtem Bityutskiy 
8091e51764aSArtem Bityutskiy 	/* And we have to tell lprops that this LEB is taken */
8101e51764aSArtem Bityutskiy 	err = ubifs_change_one_lp(c, c->gc_lnum, c->leb_size, 0,
8111e51764aSArtem Bityutskiy 				  LPROPS_TAKEN, 0, 0);
8121e51764aSArtem Bityutskiy 	return err;
8131e51764aSArtem Bityutskiy }
8141e51764aSArtem Bityutskiy 
8151e51764aSArtem Bityutskiy /**
8161e51764aSArtem Bityutskiy  * alloc_wbufs - allocate write-buffers.
8171e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8181e51764aSArtem Bityutskiy  *
8191e51764aSArtem Bityutskiy  * This helper function allocates and initializes UBIFS write-buffers. Returns
8201e51764aSArtem Bityutskiy  * zero in case of success and %-ENOMEM in case of failure.
8211e51764aSArtem Bityutskiy  */
alloc_wbufs(struct ubifs_info * c)8221e51764aSArtem Bityutskiy static int alloc_wbufs(struct ubifs_info *c)
8231e51764aSArtem Bityutskiy {
8241e51764aSArtem Bityutskiy 	int i, err;
8251e51764aSArtem Bityutskiy 
82686b4c14dSFabian Frederick 	c->jheads = kcalloc(c->jhead_cnt, sizeof(struct ubifs_jhead),
8271e51764aSArtem Bityutskiy 			    GFP_KERNEL);
8281e51764aSArtem Bityutskiy 	if (!c->jheads)
8291e51764aSArtem Bityutskiy 		return -ENOMEM;
8301e51764aSArtem Bityutskiy 
8311e51764aSArtem Bityutskiy 	/* Initialize journal heads */
8321e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
8331e51764aSArtem Bityutskiy 		INIT_LIST_HEAD(&c->jheads[i].buds_list);
8341e51764aSArtem Bityutskiy 		err = ubifs_wbuf_init(c, &c->jheads[i].wbuf);
8351e51764aSArtem Bityutskiy 		if (err)
8364a1ff3c5SLi Zetao 			goto out_wbuf;
8371e51764aSArtem Bityutskiy 
8381e51764aSArtem Bityutskiy 		c->jheads[i].wbuf.sync_callback = &bud_wbuf_callback;
8391e51764aSArtem Bityutskiy 		c->jheads[i].wbuf.jhead = i;
8401a0b0699SArtem Bityutskiy 		c->jheads[i].grouped = 1;
8416a98bc46SSascha Hauer 		c->jheads[i].log_hash = ubifs_hash_get_desc(c);
84242119dbeSWang ShaoBo 		if (IS_ERR(c->jheads[i].log_hash)) {
84342119dbeSWang ShaoBo 			err = PTR_ERR(c->jheads[i].log_hash);
8444a1ff3c5SLi Zetao 			goto out_log_hash;
8451e51764aSArtem Bityutskiy 		}
84642119dbeSWang ShaoBo 	}
8471e51764aSArtem Bityutskiy 
8481e51764aSArtem Bityutskiy 	/*
84944156267SArtem Bityutskiy 	 * Garbage Collector head does not need to be synchronized by timer.
85044156267SArtem Bityutskiy 	 * Also GC head nodes are not grouped.
8511e51764aSArtem Bityutskiy 	 */
8520b335b9dSArtem Bityutskiy 	c->jheads[GCHD].wbuf.no_timer = 1;
8531a0b0699SArtem Bityutskiy 	c->jheads[GCHD].grouped = 0;
8541e51764aSArtem Bityutskiy 
8551e51764aSArtem Bityutskiy 	return 0;
8566a98bc46SSascha Hauer 
8574a1ff3c5SLi Zetao out_log_hash:
8584a1ff3c5SLi Zetao 	kfree(c->jheads[i].wbuf.buf);
8594a1ff3c5SLi Zetao 	kfree(c->jheads[i].wbuf.inodes);
8604a1ff3c5SLi Zetao 
8614a1ff3c5SLi Zetao out_wbuf:
8624a1ff3c5SLi Zetao 	while (i--) {
8634a1ff3c5SLi Zetao 		kfree(c->jheads[i].wbuf.buf);
8644a1ff3c5SLi Zetao 		kfree(c->jheads[i].wbuf.inodes);
8656a98bc46SSascha Hauer 		kfree(c->jheads[i].log_hash);
8664a1ff3c5SLi Zetao 	}
8674a1ff3c5SLi Zetao 	kfree(c->jheads);
8684a1ff3c5SLi Zetao 	c->jheads = NULL;
8696a98bc46SSascha Hauer 
8706a98bc46SSascha Hauer 	return err;
8711e51764aSArtem Bityutskiy }
8721e51764aSArtem Bityutskiy 
8731e51764aSArtem Bityutskiy /**
8741e51764aSArtem Bityutskiy  * free_wbufs - free write-buffers.
8751e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8761e51764aSArtem Bityutskiy  */
free_wbufs(struct ubifs_info * c)8771e51764aSArtem Bityutskiy static void free_wbufs(struct ubifs_info *c)
8781e51764aSArtem Bityutskiy {
8791e51764aSArtem Bityutskiy 	int i;
8801e51764aSArtem Bityutskiy 
8811e51764aSArtem Bityutskiy 	if (c->jheads) {
8821e51764aSArtem Bityutskiy 		for (i = 0; i < c->jhead_cnt; i++) {
8831e51764aSArtem Bityutskiy 			kfree(c->jheads[i].wbuf.buf);
8841e51764aSArtem Bityutskiy 			kfree(c->jheads[i].wbuf.inodes);
8856a98bc46SSascha Hauer 			kfree(c->jheads[i].log_hash);
8861e51764aSArtem Bityutskiy 		}
8871e51764aSArtem Bityutskiy 		kfree(c->jheads);
8881e51764aSArtem Bityutskiy 		c->jheads = NULL;
8891e51764aSArtem Bityutskiy 	}
8901e51764aSArtem Bityutskiy }
8911e51764aSArtem Bityutskiy 
8921e51764aSArtem Bityutskiy /**
8931e51764aSArtem Bityutskiy  * free_orphans - free orphans.
8941e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8951e51764aSArtem Bityutskiy  */
free_orphans(struct ubifs_info * c)8961e51764aSArtem Bityutskiy static void free_orphans(struct ubifs_info *c)
8971e51764aSArtem Bityutskiy {
8981e51764aSArtem Bityutskiy 	struct ubifs_orphan *orph;
8991e51764aSArtem Bityutskiy 
9001e51764aSArtem Bityutskiy 	while (c->orph_dnext) {
9011e51764aSArtem Bityutskiy 		orph = c->orph_dnext;
9021e51764aSArtem Bityutskiy 		c->orph_dnext = orph->dnext;
9031e51764aSArtem Bityutskiy 		list_del(&orph->list);
9041e51764aSArtem Bityutskiy 		kfree(orph);
9051e51764aSArtem Bityutskiy 	}
9061e51764aSArtem Bityutskiy 
9071e51764aSArtem Bityutskiy 	while (!list_empty(&c->orph_list)) {
9081e51764aSArtem Bityutskiy 		orph = list_entry(c->orph_list.next, struct ubifs_orphan, list);
9091e51764aSArtem Bityutskiy 		list_del(&orph->list);
9101e51764aSArtem Bityutskiy 		kfree(orph);
911235c362bSSheng Yong 		ubifs_err(c, "orphan list not empty at unmount");
9121e51764aSArtem Bityutskiy 	}
9131e51764aSArtem Bityutskiy 
9141e51764aSArtem Bityutskiy 	vfree(c->orph_buf);
9151e51764aSArtem Bityutskiy 	c->orph_buf = NULL;
9161e51764aSArtem Bityutskiy }
9171e51764aSArtem Bityutskiy 
9181e51764aSArtem Bityutskiy /**
9191e51764aSArtem Bityutskiy  * free_buds - free per-bud objects.
9201e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9211e51764aSArtem Bityutskiy  */
free_buds(struct ubifs_info * c)9221e51764aSArtem Bityutskiy static void free_buds(struct ubifs_info *c)
9231e51764aSArtem Bityutskiy {
924bb25e49fSCody P Schafer 	struct ubifs_bud *bud, *n;
9251e51764aSArtem Bityutskiy 
926bb25e49fSCody P Schafer 	rbtree_postorder_for_each_entry_safe(bud, n, &c->buds, rb)
9271e51764aSArtem Bityutskiy 		kfree(bud);
9281e51764aSArtem Bityutskiy }
9291e51764aSArtem Bityutskiy 
9301e51764aSArtem Bityutskiy /**
9311e51764aSArtem Bityutskiy  * check_volume_empty - check if the UBI volume is empty.
9321e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9331e51764aSArtem Bityutskiy  *
9341e51764aSArtem Bityutskiy  * This function checks if the UBIFS volume is empty by looking if its LEBs are
9351e51764aSArtem Bityutskiy  * mapped or not. The result of checking is stored in the @c->empty variable.
9361e51764aSArtem Bityutskiy  * Returns zero in case of success and a negative error code in case of
9371e51764aSArtem Bityutskiy  * failure.
9381e51764aSArtem Bityutskiy  */
check_volume_empty(struct ubifs_info * c)9391e51764aSArtem Bityutskiy static int check_volume_empty(struct ubifs_info *c)
9401e51764aSArtem Bityutskiy {
9411e51764aSArtem Bityutskiy 	int lnum, err;
9421e51764aSArtem Bityutskiy 
9431e51764aSArtem Bityutskiy 	c->empty = 1;
9441e51764aSArtem Bityutskiy 	for (lnum = 0; lnum < c->leb_cnt; lnum++) {
945d3b2578fSArtem Bityutskiy 		err = ubifs_is_mapped(c, lnum);
9461e51764aSArtem Bityutskiy 		if (unlikely(err < 0))
9471e51764aSArtem Bityutskiy 			return err;
9481e51764aSArtem Bityutskiy 		if (err == 1) {
9491e51764aSArtem Bityutskiy 			c->empty = 0;
9501e51764aSArtem Bityutskiy 			break;
9511e51764aSArtem Bityutskiy 		}
9521e51764aSArtem Bityutskiy 
9531e51764aSArtem Bityutskiy 		cond_resched();
9541e51764aSArtem Bityutskiy 	}
9551e51764aSArtem Bityutskiy 
9561e51764aSArtem Bityutskiy 	return 0;
9571e51764aSArtem Bityutskiy }
9581e51764aSArtem Bityutskiy 
9591e51764aSArtem Bityutskiy /*
9601e51764aSArtem Bityutskiy  * UBIFS mount options.
9611e51764aSArtem Bityutskiy  *
9621e51764aSArtem Bityutskiy  * Opt_fast_unmount: do not run a journal commit before un-mounting
9631e51764aSArtem Bityutskiy  * Opt_norm_unmount: run a journal commit before un-mounting
9644793e7c5SAdrian Hunter  * Opt_bulk_read: enable bulk-reads
9654793e7c5SAdrian Hunter  * Opt_no_bulk_read: disable bulk-reads
9662953e73fSAdrian Hunter  * Opt_chk_data_crc: check CRCs when reading data nodes
9672953e73fSAdrian Hunter  * Opt_no_chk_data_crc: do not check CRCs when reading data nodes
968553dea4dSArtem Bityutskiy  * Opt_override_compr: override default compressor
969c38c5a7fSRichard Weinberger  * Opt_assert: set ubifs_assert() action
970d8a22773SSascha Hauer  * Opt_auth_key: The key name used for authentication
971d8a22773SSascha Hauer  * Opt_auth_hash_name: The hash type used for authentication
9721e51764aSArtem Bityutskiy  * Opt_err: just end of array marker
9731e51764aSArtem Bityutskiy  */
9741e51764aSArtem Bityutskiy enum {
9751e51764aSArtem Bityutskiy 	Opt_fast_unmount,
9761e51764aSArtem Bityutskiy 	Opt_norm_unmount,
9774793e7c5SAdrian Hunter 	Opt_bulk_read,
9784793e7c5SAdrian Hunter 	Opt_no_bulk_read,
9792953e73fSAdrian Hunter 	Opt_chk_data_crc,
9802953e73fSAdrian Hunter 	Opt_no_chk_data_crc,
981553dea4dSArtem Bityutskiy 	Opt_override_compr,
982c38c5a7fSRichard Weinberger 	Opt_assert,
983d8a22773SSascha Hauer 	Opt_auth_key,
984d8a22773SSascha Hauer 	Opt_auth_hash_name,
985319c1042SRabin Vincent 	Opt_ignore,
9861e51764aSArtem Bityutskiy 	Opt_err,
9871e51764aSArtem Bityutskiy };
9881e51764aSArtem Bityutskiy 
989a447c093SSteven Whitehouse static const match_table_t tokens = {
9901e51764aSArtem Bityutskiy 	{Opt_fast_unmount, "fast_unmount"},
9911e51764aSArtem Bityutskiy 	{Opt_norm_unmount, "norm_unmount"},
9924793e7c5SAdrian Hunter 	{Opt_bulk_read, "bulk_read"},
9934793e7c5SAdrian Hunter 	{Opt_no_bulk_read, "no_bulk_read"},
9942953e73fSAdrian Hunter 	{Opt_chk_data_crc, "chk_data_crc"},
9952953e73fSAdrian Hunter 	{Opt_no_chk_data_crc, "no_chk_data_crc"},
996553dea4dSArtem Bityutskiy 	{Opt_override_compr, "compr=%s"},
997d8a22773SSascha Hauer 	{Opt_auth_key, "auth_key=%s"},
998d8a22773SSascha Hauer 	{Opt_auth_hash_name, "auth_hash_name=%s"},
999319c1042SRabin Vincent 	{Opt_ignore, "ubi=%s"},
1000319c1042SRabin Vincent 	{Opt_ignore, "vol=%s"},
1001c38c5a7fSRichard Weinberger 	{Opt_assert, "assert=%s"},
10021e51764aSArtem Bityutskiy 	{Opt_err, NULL},
10031e51764aSArtem Bityutskiy };
10041e51764aSArtem Bityutskiy 
10051e51764aSArtem Bityutskiy /**
10068379ea31SArtem Bityutskiy  * parse_standard_option - parse a standard mount option.
10078379ea31SArtem Bityutskiy  * @option: the option to parse
10088379ea31SArtem Bityutskiy  *
10098379ea31SArtem Bityutskiy  * Normally, standard mount options like "sync" are passed to file-systems as
10108379ea31SArtem Bityutskiy  * flags. However, when a "rootflags=" kernel boot parameter is used, they may
10118379ea31SArtem Bityutskiy  * be present in the options string. This function tries to deal with this
10128379ea31SArtem Bityutskiy  * situation and parse standard options. Returns 0 if the option was not
10138379ea31SArtem Bityutskiy  * recognized, and the corresponding integer flag if it was.
10148379ea31SArtem Bityutskiy  *
10158379ea31SArtem Bityutskiy  * UBIFS is only interested in the "sync" option, so do not check for anything
10168379ea31SArtem Bityutskiy  * else.
10178379ea31SArtem Bityutskiy  */
parse_standard_option(const char * option)10188379ea31SArtem Bityutskiy static int parse_standard_option(const char *option)
10198379ea31SArtem Bityutskiy {
1020235c362bSSheng Yong 
1021235c362bSSheng Yong 	pr_notice("UBIFS: parse %s\n", option);
10228379ea31SArtem Bityutskiy 	if (!strcmp(option, "sync"))
10231751e8a6SLinus Torvalds 		return SB_SYNCHRONOUS;
10248379ea31SArtem Bityutskiy 	return 0;
10258379ea31SArtem Bityutskiy }
10268379ea31SArtem Bityutskiy 
10278379ea31SArtem Bityutskiy /**
10281e51764aSArtem Bityutskiy  * ubifs_parse_options - parse mount parameters.
10291e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
10301e51764aSArtem Bityutskiy  * @options: parameters to parse
10311e51764aSArtem Bityutskiy  * @is_remount: non-zero if this is FS re-mount
10321e51764aSArtem Bityutskiy  *
10331e51764aSArtem Bityutskiy  * This function parses UBIFS mount options and returns zero in case success
10341e51764aSArtem Bityutskiy  * and a negative error code in case of failure.
10351e51764aSArtem Bityutskiy  */
ubifs_parse_options(struct ubifs_info * c,char * options,int is_remount)10361e51764aSArtem Bityutskiy static int ubifs_parse_options(struct ubifs_info *c, char *options,
10371e51764aSArtem Bityutskiy 			       int is_remount)
10381e51764aSArtem Bityutskiy {
10391e51764aSArtem Bityutskiy 	char *p;
10401e51764aSArtem Bityutskiy 	substring_t args[MAX_OPT_ARGS];
10411e51764aSArtem Bityutskiy 
10421e51764aSArtem Bityutskiy 	if (!options)
10431e51764aSArtem Bityutskiy 		return 0;
10441e51764aSArtem Bityutskiy 
10451e51764aSArtem Bityutskiy 	while ((p = strsep(&options, ","))) {
10461e51764aSArtem Bityutskiy 		int token;
10471e51764aSArtem Bityutskiy 
10481e51764aSArtem Bityutskiy 		if (!*p)
10491e51764aSArtem Bityutskiy 			continue;
10501e51764aSArtem Bityutskiy 
10511e51764aSArtem Bityutskiy 		token = match_token(p, tokens, args);
10521e51764aSArtem Bityutskiy 		switch (token) {
105327ad2799SArtem Bityutskiy 		/*
105427ad2799SArtem Bityutskiy 		 * %Opt_fast_unmount and %Opt_norm_unmount options are ignored.
1055cb54ef8bSArtem Bityutskiy 		 * We accept them in order to be backward-compatible. But this
105627ad2799SArtem Bityutskiy 		 * should be removed at some point.
105727ad2799SArtem Bityutskiy 		 */
10581e51764aSArtem Bityutskiy 		case Opt_fast_unmount:
10591e51764aSArtem Bityutskiy 			c->mount_opts.unmount_mode = 2;
10601e51764aSArtem Bityutskiy 			break;
10611e51764aSArtem Bityutskiy 		case Opt_norm_unmount:
10621e51764aSArtem Bityutskiy 			c->mount_opts.unmount_mode = 1;
10631e51764aSArtem Bityutskiy 			break;
10644793e7c5SAdrian Hunter 		case Opt_bulk_read:
10654793e7c5SAdrian Hunter 			c->mount_opts.bulk_read = 2;
10664793e7c5SAdrian Hunter 			c->bulk_read = 1;
10674793e7c5SAdrian Hunter 			break;
10684793e7c5SAdrian Hunter 		case Opt_no_bulk_read:
10694793e7c5SAdrian Hunter 			c->mount_opts.bulk_read = 1;
10704793e7c5SAdrian Hunter 			c->bulk_read = 0;
10714793e7c5SAdrian Hunter 			break;
10722953e73fSAdrian Hunter 		case Opt_chk_data_crc:
10732953e73fSAdrian Hunter 			c->mount_opts.chk_data_crc = 2;
10742953e73fSAdrian Hunter 			c->no_chk_data_crc = 0;
10752953e73fSAdrian Hunter 			break;
10762953e73fSAdrian Hunter 		case Opt_no_chk_data_crc:
10772953e73fSAdrian Hunter 			c->mount_opts.chk_data_crc = 1;
10782953e73fSAdrian Hunter 			c->no_chk_data_crc = 1;
10792953e73fSAdrian Hunter 			break;
1080553dea4dSArtem Bityutskiy 		case Opt_override_compr:
1081553dea4dSArtem Bityutskiy 		{
1082553dea4dSArtem Bityutskiy 			char *name = match_strdup(&args[0]);
1083553dea4dSArtem Bityutskiy 
1084553dea4dSArtem Bityutskiy 			if (!name)
1085553dea4dSArtem Bityutskiy 				return -ENOMEM;
1086553dea4dSArtem Bityutskiy 			if (!strcmp(name, "none"))
1087553dea4dSArtem Bityutskiy 				c->mount_opts.compr_type = UBIFS_COMPR_NONE;
1088553dea4dSArtem Bityutskiy 			else if (!strcmp(name, "lzo"))
1089553dea4dSArtem Bityutskiy 				c->mount_opts.compr_type = UBIFS_COMPR_LZO;
1090553dea4dSArtem Bityutskiy 			else if (!strcmp(name, "zlib"))
1091553dea4dSArtem Bityutskiy 				c->mount_opts.compr_type = UBIFS_COMPR_ZLIB;
1092eeabb986SMichele Dionisio 			else if (!strcmp(name, "zstd"))
1093eeabb986SMichele Dionisio 				c->mount_opts.compr_type = UBIFS_COMPR_ZSTD;
1094553dea4dSArtem Bityutskiy 			else {
1095235c362bSSheng Yong 				ubifs_err(c, "unknown compressor \"%s\"", name); //FIXME: is c ready?
1096553dea4dSArtem Bityutskiy 				kfree(name);
1097553dea4dSArtem Bityutskiy 				return -EINVAL;
1098553dea4dSArtem Bityutskiy 			}
1099553dea4dSArtem Bityutskiy 			kfree(name);
1100553dea4dSArtem Bityutskiy 			c->mount_opts.override_compr = 1;
1101553dea4dSArtem Bityutskiy 			c->default_compr = c->mount_opts.compr_type;
1102553dea4dSArtem Bityutskiy 			break;
1103553dea4dSArtem Bityutskiy 		}
1104c38c5a7fSRichard Weinberger 		case Opt_assert:
1105c38c5a7fSRichard Weinberger 		{
1106c38c5a7fSRichard Weinberger 			char *act = match_strdup(&args[0]);
1107c38c5a7fSRichard Weinberger 
1108c38c5a7fSRichard Weinberger 			if (!act)
1109c38c5a7fSRichard Weinberger 				return -ENOMEM;
1110c38c5a7fSRichard Weinberger 			if (!strcmp(act, "report"))
1111c38c5a7fSRichard Weinberger 				c->assert_action = ASSACT_REPORT;
1112c38c5a7fSRichard Weinberger 			else if (!strcmp(act, "read-only"))
1113c38c5a7fSRichard Weinberger 				c->assert_action = ASSACT_RO;
1114c38c5a7fSRichard Weinberger 			else if (!strcmp(act, "panic"))
1115c38c5a7fSRichard Weinberger 				c->assert_action = ASSACT_PANIC;
1116c38c5a7fSRichard Weinberger 			else {
1117c38c5a7fSRichard Weinberger 				ubifs_err(c, "unknown assert action \"%s\"", act);
1118c38c5a7fSRichard Weinberger 				kfree(act);
1119c38c5a7fSRichard Weinberger 				return -EINVAL;
1120c38c5a7fSRichard Weinberger 			}
1121c38c5a7fSRichard Weinberger 			kfree(act);
1122c38c5a7fSRichard Weinberger 			break;
1123c38c5a7fSRichard Weinberger 		}
1124d8a22773SSascha Hauer 		case Opt_auth_key:
1125bb674a4dSZhihao Cheng 			if (!is_remount) {
1126bb674a4dSZhihao Cheng 				c->auth_key_name = kstrdup(args[0].from,
1127bb674a4dSZhihao Cheng 								GFP_KERNEL);
1128d8a22773SSascha Hauer 				if (!c->auth_key_name)
1129d8a22773SSascha Hauer 					return -ENOMEM;
1130bb674a4dSZhihao Cheng 			}
1131d8a22773SSascha Hauer 			break;
1132d8a22773SSascha Hauer 		case Opt_auth_hash_name:
1133bb674a4dSZhihao Cheng 			if (!is_remount) {
1134bb674a4dSZhihao Cheng 				c->auth_hash_name = kstrdup(args[0].from,
1135bb674a4dSZhihao Cheng 								GFP_KERNEL);
1136d8a22773SSascha Hauer 				if (!c->auth_hash_name)
1137d8a22773SSascha Hauer 					return -ENOMEM;
1138bb674a4dSZhihao Cheng 			}
1139d8a22773SSascha Hauer 			break;
1140319c1042SRabin Vincent 		case Opt_ignore:
1141319c1042SRabin Vincent 			break;
11421e51764aSArtem Bityutskiy 		default:
11438379ea31SArtem Bityutskiy 		{
11448379ea31SArtem Bityutskiy 			unsigned long flag;
11458379ea31SArtem Bityutskiy 			struct super_block *sb = c->vfs_sb;
11468379ea31SArtem Bityutskiy 
11478379ea31SArtem Bityutskiy 			flag = parse_standard_option(p);
11488379ea31SArtem Bityutskiy 			if (!flag) {
1149235c362bSSheng Yong 				ubifs_err(c, "unrecognized mount option \"%s\" or missing value",
115079fda517SArtem Bityutskiy 					  p);
11511e51764aSArtem Bityutskiy 				return -EINVAL;
11521e51764aSArtem Bityutskiy 			}
11538379ea31SArtem Bityutskiy 			sb->s_flags |= flag;
11548379ea31SArtem Bityutskiy 			break;
11558379ea31SArtem Bityutskiy 		}
11568379ea31SArtem Bityutskiy 		}
11571e51764aSArtem Bityutskiy 	}
11581e51764aSArtem Bityutskiy 
11591e51764aSArtem Bityutskiy 	return 0;
11601e51764aSArtem Bityutskiy }
11611e51764aSArtem Bityutskiy 
116247f6d9ceSZhihao Cheng /*
116347f6d9ceSZhihao Cheng  * ubifs_release_options - release mount parameters which have been dumped.
116447f6d9ceSZhihao Cheng  * @c: UBIFS file-system description object
116547f6d9ceSZhihao Cheng  */
ubifs_release_options(struct ubifs_info * c)116647f6d9ceSZhihao Cheng static void ubifs_release_options(struct ubifs_info *c)
116747f6d9ceSZhihao Cheng {
116847f6d9ceSZhihao Cheng 	kfree(c->auth_key_name);
116947f6d9ceSZhihao Cheng 	c->auth_key_name = NULL;
117047f6d9ceSZhihao Cheng 	kfree(c->auth_hash_name);
117147f6d9ceSZhihao Cheng 	c->auth_hash_name = NULL;
117247f6d9ceSZhihao Cheng }
117347f6d9ceSZhihao Cheng 
11741e51764aSArtem Bityutskiy /**
11751e51764aSArtem Bityutskiy  * destroy_journal - destroy journal data structures.
11761e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
11771e51764aSArtem Bityutskiy  *
11781e51764aSArtem Bityutskiy  * This function destroys journal data structures including those that may have
11791e51764aSArtem Bityutskiy  * been created by recovery functions.
11801e51764aSArtem Bityutskiy  */
destroy_journal(struct ubifs_info * c)11811e51764aSArtem Bityutskiy static void destroy_journal(struct ubifs_info *c)
11821e51764aSArtem Bityutskiy {
11831e51764aSArtem Bityutskiy 	while (!list_empty(&c->unclean_leb_list)) {
11841e51764aSArtem Bityutskiy 		struct ubifs_unclean_leb *ucleb;
11851e51764aSArtem Bityutskiy 
11861e51764aSArtem Bityutskiy 		ucleb = list_entry(c->unclean_leb_list.next,
11871e51764aSArtem Bityutskiy 				   struct ubifs_unclean_leb, list);
11881e51764aSArtem Bityutskiy 		list_del(&ucleb->list);
11891e51764aSArtem Bityutskiy 		kfree(ucleb);
11901e51764aSArtem Bityutskiy 	}
11911e51764aSArtem Bityutskiy 	while (!list_empty(&c->old_buds)) {
11921e51764aSArtem Bityutskiy 		struct ubifs_bud *bud;
11931e51764aSArtem Bityutskiy 
11941e51764aSArtem Bityutskiy 		bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
11951e51764aSArtem Bityutskiy 		list_del(&bud->list);
11961e51764aSArtem Bityutskiy 		kfree(bud);
11971e51764aSArtem Bityutskiy 	}
11981e51764aSArtem Bityutskiy 	ubifs_destroy_idx_gc(c);
11991e51764aSArtem Bityutskiy 	ubifs_destroy_size_tree(c);
12001e51764aSArtem Bityutskiy 	ubifs_tnc_close(c);
12011e51764aSArtem Bityutskiy 	free_buds(c);
12021e51764aSArtem Bityutskiy }
12031e51764aSArtem Bityutskiy 
12041e51764aSArtem Bityutskiy /**
12053477d204SArtem Bityutskiy  * bu_init - initialize bulk-read information.
12063477d204SArtem Bityutskiy  * @c: UBIFS file-system description object
12073477d204SArtem Bityutskiy  */
bu_init(struct ubifs_info * c)12083477d204SArtem Bityutskiy static void bu_init(struct ubifs_info *c)
12093477d204SArtem Bityutskiy {
12106eb61d58SRichard Weinberger 	ubifs_assert(c, c->bulk_read == 1);
12113477d204SArtem Bityutskiy 
12123477d204SArtem Bityutskiy 	if (c->bu.buf)
12133477d204SArtem Bityutskiy 		return; /* Already initialized */
12143477d204SArtem Bityutskiy 
12153477d204SArtem Bityutskiy again:
12163477d204SArtem Bityutskiy 	c->bu.buf = kmalloc(c->max_bu_buf_len, GFP_KERNEL | __GFP_NOWARN);
12173477d204SArtem Bityutskiy 	if (!c->bu.buf) {
12183477d204SArtem Bityutskiy 		if (c->max_bu_buf_len > UBIFS_KMALLOC_OK) {
12193477d204SArtem Bityutskiy 			c->max_bu_buf_len = UBIFS_KMALLOC_OK;
12203477d204SArtem Bityutskiy 			goto again;
12213477d204SArtem Bityutskiy 		}
12223477d204SArtem Bityutskiy 
12233477d204SArtem Bityutskiy 		/* Just disable bulk-read */
1224235c362bSSheng Yong 		ubifs_warn(c, "cannot allocate %d bytes of memory for bulk-read, disabling it",
122579fda517SArtem Bityutskiy 			   c->max_bu_buf_len);
12263477d204SArtem Bityutskiy 		c->mount_opts.bulk_read = 1;
12273477d204SArtem Bityutskiy 		c->bulk_read = 0;
12283477d204SArtem Bityutskiy 		return;
12293477d204SArtem Bityutskiy 	}
12303477d204SArtem Bityutskiy }
12313477d204SArtem Bityutskiy 
12323477d204SArtem Bityutskiy /**
123357a450e9SArtem Bityutskiy  * check_free_space - check if there is enough free space to mount.
123457a450e9SArtem Bityutskiy  * @c: UBIFS file-system description object
123557a450e9SArtem Bityutskiy  *
123657a450e9SArtem Bityutskiy  * This function makes sure UBIFS has enough free space to be mounted in
123757a450e9SArtem Bityutskiy  * read/write mode. UBIFS must always have some free space to allow deletions.
123857a450e9SArtem Bityutskiy  */
check_free_space(struct ubifs_info * c)123957a450e9SArtem Bityutskiy static int check_free_space(struct ubifs_info *c)
124057a450e9SArtem Bityutskiy {
12416eb61d58SRichard Weinberger 	ubifs_assert(c, c->dark_wm > 0);
124257a450e9SArtem Bityutskiy 	if (c->lst.total_free + c->lst.total_dirty < c->dark_wm) {
1243235c362bSSheng Yong 		ubifs_err(c, "insufficient free space to mount in R/W mode");
1244edf6be24SArtem Bityutskiy 		ubifs_dump_budg(c, &c->bi);
1245edf6be24SArtem Bityutskiy 		ubifs_dump_lprops(c);
1246a2b9df3fSArtem Bityutskiy 		return -ENOSPC;
124757a450e9SArtem Bityutskiy 	}
124857a450e9SArtem Bityutskiy 	return 0;
124957a450e9SArtem Bityutskiy }
125057a450e9SArtem Bityutskiy 
125157a450e9SArtem Bityutskiy /**
12521e51764aSArtem Bityutskiy  * mount_ubifs - mount UBIFS file-system.
12531e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
12541e51764aSArtem Bityutskiy  *
12551e51764aSArtem Bityutskiy  * This function mounts UBIFS file system. Returns zero in case of success and
12561e51764aSArtem Bityutskiy  * a negative error code in case of failure.
12571e51764aSArtem Bityutskiy  */
mount_ubifs(struct ubifs_info * c)12581e51764aSArtem Bityutskiy static int mount_ubifs(struct ubifs_info *c)
12591e51764aSArtem Bityutskiy {
12602ef13294SArtem Bityutskiy 	int err;
12613668b70fSArtem Bityutskiy 	long long x, y;
12621e51764aSArtem Bityutskiy 	size_t sz;
12631e51764aSArtem Bityutskiy 
1264bc98a42cSDavid Howells 	c->ro_mount = !!sb_rdonly(c->vfs_sb);
12651751e8a6SLinus Torvalds 	/* Suppress error messages while probing if SB_SILENT is set */
12661751e8a6SLinus Torvalds 	c->probing = !!(c->vfs_sb->s_flags & SB_SILENT);
126790bea5a3SDaniel Golle 
12681e51764aSArtem Bityutskiy 	err = init_constants_early(c);
12691e51764aSArtem Bityutskiy 	if (err)
12701e51764aSArtem Bityutskiy 		return err;
12711e51764aSArtem Bityutskiy 
127217c2f9f8SArtem Bityutskiy 	err = ubifs_debugging_init(c);
127317c2f9f8SArtem Bityutskiy 	if (err)
127417c2f9f8SArtem Bityutskiy 		return err;
12751e51764aSArtem Bityutskiy 
12762e3cbf42SStefan Schaeckeler 	err = ubifs_sysfs_register(c);
12772e3cbf42SStefan Schaeckeler 	if (err)
12782e3cbf42SStefan Schaeckeler 		goto out_debugging;
12792e3cbf42SStefan Schaeckeler 
12801e51764aSArtem Bityutskiy 	err = check_volume_empty(c);
12811e51764aSArtem Bityutskiy 	if (err)
12821e51764aSArtem Bityutskiy 		goto out_free;
12831e51764aSArtem Bityutskiy 
12842ef13294SArtem Bityutskiy 	if (c->empty && (c->ro_mount || c->ro_media)) {
12851e51764aSArtem Bityutskiy 		/*
12861e51764aSArtem Bityutskiy 		 * This UBI volume is empty, and read-only, or the file system
12871e51764aSArtem Bityutskiy 		 * is mounted read-only - we cannot format it.
12881e51764aSArtem Bityutskiy 		 */
1289235c362bSSheng Yong 		ubifs_err(c, "can't format empty UBI volume: read-only %s",
12901e51764aSArtem Bityutskiy 			  c->ro_media ? "UBI volume" : "mount");
12911e51764aSArtem Bityutskiy 		err = -EROFS;
12921e51764aSArtem Bityutskiy 		goto out_free;
12931e51764aSArtem Bityutskiy 	}
12941e51764aSArtem Bityutskiy 
12952ef13294SArtem Bityutskiy 	if (c->ro_media && !c->ro_mount) {
1296235c362bSSheng Yong 		ubifs_err(c, "cannot mount read-write - read-only media");
12971e51764aSArtem Bityutskiy 		err = -EROFS;
12981e51764aSArtem Bityutskiy 		goto out_free;
12991e51764aSArtem Bityutskiy 	}
13001e51764aSArtem Bityutskiy 
13011e51764aSArtem Bityutskiy 	/*
13021e51764aSArtem Bityutskiy 	 * The requirement for the buffer is that it should fit indexing B-tree
13031e51764aSArtem Bityutskiy 	 * height amount of integers. We assume the height if the TNC tree will
13041e51764aSArtem Bityutskiy 	 * never exceed 64.
13051e51764aSArtem Bityutskiy 	 */
13061e51764aSArtem Bityutskiy 	err = -ENOMEM;
13076da2ec56SKees Cook 	c->bottom_up_buf = kmalloc_array(BOTTOM_UP_HEIGHT, sizeof(int),
13086da2ec56SKees Cook 					 GFP_KERNEL);
13091e51764aSArtem Bityutskiy 	if (!c->bottom_up_buf)
13101e51764aSArtem Bityutskiy 		goto out_free;
13111e51764aSArtem Bityutskiy 
13121e51764aSArtem Bityutskiy 	c->sbuf = vmalloc(c->leb_size);
13131e51764aSArtem Bityutskiy 	if (!c->sbuf)
13141e51764aSArtem Bityutskiy 		goto out_free;
13151e51764aSArtem Bityutskiy 
13162ef13294SArtem Bityutskiy 	if (!c->ro_mount) {
13171e51764aSArtem Bityutskiy 		c->ileb_buf = vmalloc(c->leb_size);
13181e51764aSArtem Bityutskiy 		if (!c->ileb_buf)
13191e51764aSArtem Bityutskiy 			goto out_free;
13201e51764aSArtem Bityutskiy 	}
13211e51764aSArtem Bityutskiy 
13223477d204SArtem Bityutskiy 	if (c->bulk_read == 1)
13233477d204SArtem Bityutskiy 		bu_init(c);
13243477d204SArtem Bityutskiy 
1325d882962fSMatthew L. Creech 	if (!c->ro_mount) {
13267799953bSRichard Weinberger 		c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ + \
13277799953bSRichard Weinberger 					       UBIFS_CIPHER_BLOCK_SIZE,
1328d882962fSMatthew L. Creech 					       GFP_KERNEL);
1329d882962fSMatthew L. Creech 		if (!c->write_reserve_buf)
1330d882962fSMatthew L. Creech 			goto out_free;
1331d882962fSMatthew L. Creech 	}
1332d882962fSMatthew L. Creech 
133318d1d7fbSArtem Bityutskiy 	c->mounting = 1;
13342953e73fSAdrian Hunter 
1335d8a22773SSascha Hauer 	if (c->auth_key_name) {
1336d8a22773SSascha Hauer 		if (IS_ENABLED(CONFIG_UBIFS_FS_AUTHENTICATION)) {
1337d8a22773SSascha Hauer 			err = ubifs_init_authentication(c);
1338d8a22773SSascha Hauer 			if (err)
1339d8a22773SSascha Hauer 				goto out_free;
1340d8a22773SSascha Hauer 		} else {
1341d8a22773SSascha Hauer 			ubifs_err(c, "auth_key_name, but UBIFS is built without"
1342d8a22773SSascha Hauer 				  " authentication support");
1343d8a22773SSascha Hauer 			err = -EINVAL;
1344d8a22773SSascha Hauer 			goto out_free;
1345d8a22773SSascha Hauer 		}
1346d8a22773SSascha Hauer 	}
1347d8a22773SSascha Hauer 
13481e51764aSArtem Bityutskiy 	err = ubifs_read_superblock(c);
13491e51764aSArtem Bityutskiy 	if (err)
1350e2a05cc7SZhihao Cheng 		goto out_auth;
13511e51764aSArtem Bityutskiy 
135290bea5a3SDaniel Golle 	c->probing = 0;
135390bea5a3SDaniel Golle 
13541e51764aSArtem Bityutskiy 	/*
1355553dea4dSArtem Bityutskiy 	 * Make sure the compressor which is set as default in the superblock
135657a450e9SArtem Bityutskiy 	 * or overridden by mount options is actually compiled in.
13571e51764aSArtem Bityutskiy 	 */
13586eb61d58SRichard Weinberger 	if (!ubifs_compr_present(c, c->default_compr)) {
1359235c362bSSheng Yong 		ubifs_err(c, "'compressor \"%s\" is not compiled in",
13606eb61d58SRichard Weinberger 			  ubifs_compr_name(c, c->default_compr));
13618eec2f36SCorentin Chary 		err = -ENOTSUPP;
1362e2a05cc7SZhihao Cheng 		goto out_auth;
13631e51764aSArtem Bityutskiy 	}
13641e51764aSArtem Bityutskiy 
136579807d07SArtem Bityutskiy 	err = init_constants_sb(c);
13661e51764aSArtem Bityutskiy 	if (err)
1367e2a05cc7SZhihao Cheng 		goto out_auth;
13681e51764aSArtem Bityutskiy 
1369d5cf9473SLiu Song 	sz = ALIGN(c->max_idx_node_sz, c->min_io_size) * 2;
13701e51764aSArtem Bityutskiy 	c->cbuf = kmalloc(sz, GFP_NOFS);
13711e51764aSArtem Bityutskiy 	if (!c->cbuf) {
13721e51764aSArtem Bityutskiy 		err = -ENOMEM;
1373e2a05cc7SZhihao Cheng 		goto out_auth;
13741e51764aSArtem Bityutskiy 	}
13751e51764aSArtem Bityutskiy 
13761e51764aSArtem Bityutskiy 	err = alloc_wbufs(c);
13771e51764aSArtem Bityutskiy 	if (err)
13781e51764aSArtem Bityutskiy 		goto out_cbuf;
13791e51764aSArtem Bityutskiy 
1380b50b9f40SArtem Bityutskiy 	sprintf(c->bgt_name, BGT_NAME_PATTERN, c->vi.ubi_num, c->vi.vol_id);
1381b50b9f40SArtem Bityutskiy 	if (!c->ro_mount) {
13821e51764aSArtem Bityutskiy 		/* Create background thread */
1383d98c6c35SCai Huoqing 		c->bgt = kthread_run(ubifs_bg_thread, c, "%s", c->bgt_name);
13841e51764aSArtem Bityutskiy 		if (IS_ERR(c->bgt)) {
13851e51764aSArtem Bityutskiy 			err = PTR_ERR(c->bgt);
13861e51764aSArtem Bityutskiy 			c->bgt = NULL;
1387235c362bSSheng Yong 			ubifs_err(c, "cannot spawn \"%s\", error %d",
13881e51764aSArtem Bityutskiy 				  c->bgt_name, err);
13891e51764aSArtem Bityutskiy 			goto out_wbufs;
13901e51764aSArtem Bityutskiy 		}
13911e51764aSArtem Bityutskiy 	}
13921e51764aSArtem Bityutskiy 
13931e51764aSArtem Bityutskiy 	err = ubifs_read_master(c);
13941e51764aSArtem Bityutskiy 	if (err)
13951e51764aSArtem Bityutskiy 		goto out_master;
13961e51764aSArtem Bityutskiy 
139709801194SBen Gardiner 	init_constants_master(c);
139809801194SBen Gardiner 
13991e51764aSArtem Bityutskiy 	if ((c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY)) != 0) {
1400235c362bSSheng Yong 		ubifs_msg(c, "recovery needed");
14011e51764aSArtem Bityutskiy 		c->need_recovery = 1;
1402781c5717SBen Gardiner 	}
1403781c5717SBen Gardiner 
1404781c5717SBen Gardiner 	if (c->need_recovery && !c->ro_mount) {
14051e51764aSArtem Bityutskiy 		err = ubifs_recover_inl_heads(c, c->sbuf);
14061e51764aSArtem Bityutskiy 		if (err)
14071e51764aSArtem Bityutskiy 			goto out_master;
14081e51764aSArtem Bityutskiy 	}
1409781c5717SBen Gardiner 
1410781c5717SBen Gardiner 	err = ubifs_lpt_init(c, 1, !c->ro_mount);
1411781c5717SBen Gardiner 	if (err)
1412781c5717SBen Gardiner 		goto out_master;
1413781c5717SBen Gardiner 
141409801194SBen Gardiner 	if (!c->ro_mount && c->space_fixup) {
141509801194SBen Gardiner 		err = ubifs_fixup_free_space(c);
141609801194SBen Gardiner 		if (err)
141756b04e3eSSidney Amani 			goto out_lpt;
141809801194SBen Gardiner 	}
141909801194SBen Gardiner 
14202c84599cSSheng Yong 	if (!c->ro_mount && !c->need_recovery) {
14211e51764aSArtem Bityutskiy 		/*
14221e51764aSArtem Bityutskiy 		 * Set the "dirty" flag so that if we reboot uncleanly we
14231e51764aSArtem Bityutskiy 		 * will notice this immediately on the next mount.
14241e51764aSArtem Bityutskiy 		 */
14251e51764aSArtem Bityutskiy 		c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
14261e51764aSArtem Bityutskiy 		err = ubifs_write_master(c);
14271e51764aSArtem Bityutskiy 		if (err)
14281e51764aSArtem Bityutskiy 			goto out_lpt;
1429781c5717SBen Gardiner 	}
14301e51764aSArtem Bityutskiy 
1431817aa094SSascha Hauer 	/*
1432817aa094SSascha Hauer 	 * Handle offline signed images: Now that the master node is
1433817aa094SSascha Hauer 	 * written and its validation no longer depends on the hash
1434817aa094SSascha Hauer 	 * in the superblock, we can update the offline signed
1435817aa094SSascha Hauer 	 * superblock with a HMAC version,
1436817aa094SSascha Hauer 	 */
1437817aa094SSascha Hauer 	if (ubifs_authenticated(c) && ubifs_hmac_zero(c, c->sup_node->hmac)) {
1438817aa094SSascha Hauer 		err = ubifs_hmac_wkm(c, c->sup_node->hmac_wkm);
1439817aa094SSascha Hauer 		if (err)
1440817aa094SSascha Hauer 			goto out_lpt;
1441817aa094SSascha Hauer 		c->superblock_need_write = 1;
1442817aa094SSascha Hauer 	}
1443817aa094SSascha Hauer 
1444817aa094SSascha Hauer 	if (!c->ro_mount && c->superblock_need_write) {
1445817aa094SSascha Hauer 		err = ubifs_write_sb_node(c, c->sup_node);
1446817aa094SSascha Hauer 		if (err)
1447817aa094SSascha Hauer 			goto out_lpt;
1448817aa094SSascha Hauer 		c->superblock_need_write = 0;
1449817aa094SSascha Hauer 	}
1450817aa094SSascha Hauer 
1451b137545cSArtem Bityutskiy 	err = dbg_check_idx_size(c, c->bi.old_idx_sz);
14521e51764aSArtem Bityutskiy 	if (err)
14531e51764aSArtem Bityutskiy 		goto out_lpt;
14541e51764aSArtem Bityutskiy 
14551e51764aSArtem Bityutskiy 	err = ubifs_replay_journal(c);
14561e51764aSArtem Bityutskiy 	if (err)
14571e51764aSArtem Bityutskiy 		goto out_journal;
14581e51764aSArtem Bityutskiy 
14591fb8bd01SArtem Bityutskiy 	/* Calculate 'min_idx_lebs' after journal replay */
1460b137545cSArtem Bityutskiy 	c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
14611fb8bd01SArtem Bityutskiy 
14622ef13294SArtem Bityutskiy 	err = ubifs_mount_orphans(c, c->need_recovery, c->ro_mount);
14631e51764aSArtem Bityutskiy 	if (err)
14641e51764aSArtem Bityutskiy 		goto out_orphans;
14651e51764aSArtem Bityutskiy 
14662ef13294SArtem Bityutskiy 	if (!c->ro_mount) {
14671e51764aSArtem Bityutskiy 		int lnum;
14681e51764aSArtem Bityutskiy 
146957a450e9SArtem Bityutskiy 		err = check_free_space(c);
147057a450e9SArtem Bityutskiy 		if (err)
14711e51764aSArtem Bityutskiy 			goto out_orphans;
14721e51764aSArtem Bityutskiy 
14731e51764aSArtem Bityutskiy 		/* Check for enough log space */
14741e51764aSArtem Bityutskiy 		lnum = c->lhead_lnum + 1;
14751e51764aSArtem Bityutskiy 		if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
14761e51764aSArtem Bityutskiy 			lnum = UBIFS_LOG_LNUM;
14771e51764aSArtem Bityutskiy 		if (lnum == c->ltail_lnum) {
14781e51764aSArtem Bityutskiy 			err = ubifs_consolidate_log(c);
14791e51764aSArtem Bityutskiy 			if (err)
14801e51764aSArtem Bityutskiy 				goto out_orphans;
14811e51764aSArtem Bityutskiy 		}
14821e51764aSArtem Bityutskiy 
14831e51764aSArtem Bityutskiy 		if (c->need_recovery) {
14841e76592fSSascha Hauer 			if (!ubifs_authenticated(c)) {
14851e76592fSSascha Hauer 				err = ubifs_recover_size(c, true);
14861e51764aSArtem Bityutskiy 				if (err)
14871e51764aSArtem Bityutskiy 					goto out_orphans;
14881e76592fSSascha Hauer 			}
14891e76592fSSascha Hauer 
14901e51764aSArtem Bityutskiy 			err = ubifs_rcvry_gc_commit(c);
1491276de5d2SArtem Bityutskiy 			if (err)
1492276de5d2SArtem Bityutskiy 				goto out_orphans;
14931e76592fSSascha Hauer 
14941e76592fSSascha Hauer 			if (ubifs_authenticated(c)) {
14951e76592fSSascha Hauer 				err = ubifs_recover_size(c, false);
14961e76592fSSascha Hauer 				if (err)
14971e76592fSSascha Hauer 					goto out_orphans;
14981e76592fSSascha Hauer 			}
1499b4978e94SArtem Bityutskiy 		} else {
15001e51764aSArtem Bityutskiy 			err = take_gc_lnum(c);
15011e51764aSArtem Bityutskiy 			if (err)
15021e51764aSArtem Bityutskiy 				goto out_orphans;
15031e51764aSArtem Bityutskiy 
1504b4978e94SArtem Bityutskiy 			/*
1505b4978e94SArtem Bityutskiy 			 * GC LEB may contain garbage if there was an unclean
1506b4978e94SArtem Bityutskiy 			 * reboot, and it should be un-mapped.
1507b4978e94SArtem Bityutskiy 			 */
1508b4978e94SArtem Bityutskiy 			err = ubifs_leb_unmap(c, c->gc_lnum);
1509b4978e94SArtem Bityutskiy 			if (err)
1510c18de72fSMatthieu CASTET 				goto out_orphans;
1511b4978e94SArtem Bityutskiy 		}
1512b4978e94SArtem Bityutskiy 
15131e51764aSArtem Bityutskiy 		err = dbg_check_lprops(c);
15141e51764aSArtem Bityutskiy 		if (err)
15151e51764aSArtem Bityutskiy 			goto out_orphans;
15161e51764aSArtem Bityutskiy 	} else if (c->need_recovery) {
15171e76592fSSascha Hauer 		err = ubifs_recover_size(c, false);
15181e51764aSArtem Bityutskiy 		if (err)
15191e51764aSArtem Bityutskiy 			goto out_orphans;
1520b4978e94SArtem Bityutskiy 	} else {
1521b4978e94SArtem Bityutskiy 		/*
1522b4978e94SArtem Bityutskiy 		 * Even if we mount read-only, we have to set space in GC LEB
1523b4978e94SArtem Bityutskiy 		 * to proper value because this affects UBIFS free space
1524b4978e94SArtem Bityutskiy 		 * reporting. We do not want to have a situation when
1525b4978e94SArtem Bityutskiy 		 * re-mounting from R/O to R/W changes amount of free space.
1526b4978e94SArtem Bityutskiy 		 */
1527b4978e94SArtem Bityutskiy 		err = take_gc_lnum(c);
1528b4978e94SArtem Bityutskiy 		if (err)
1529b4978e94SArtem Bityutskiy 			goto out_orphans;
15301e51764aSArtem Bityutskiy 	}
15311e51764aSArtem Bityutskiy 
15321e51764aSArtem Bityutskiy 	spin_lock(&ubifs_infos_lock);
15331e51764aSArtem Bityutskiy 	list_add_tail(&c->infos_list, &ubifs_infos);
15341e51764aSArtem Bityutskiy 	spin_unlock(&ubifs_infos_lock);
15351e51764aSArtem Bityutskiy 
15361e51764aSArtem Bityutskiy 	if (c->need_recovery) {
15372ef13294SArtem Bityutskiy 		if (c->ro_mount)
1538235c362bSSheng Yong 			ubifs_msg(c, "recovery deferred");
15391e51764aSArtem Bityutskiy 		else {
15401e51764aSArtem Bityutskiy 			c->need_recovery = 0;
1541235c362bSSheng Yong 			ubifs_msg(c, "recovery completed");
1542b221337aSArtem Bityutskiy 			/*
1543b221337aSArtem Bityutskiy 			 * GC LEB has to be empty and taken at this point. But
1544b221337aSArtem Bityutskiy 			 * the journal head LEBs may also be accounted as
1545b221337aSArtem Bityutskiy 			 * "empty taken" if they are empty.
1546b221337aSArtem Bityutskiy 			 */
15476eb61d58SRichard Weinberger 			ubifs_assert(c, c->lst.taken_empty_lebs > 0);
15481e51764aSArtem Bityutskiy 		}
15496ba87c9bSArtem Bityutskiy 	} else
15506eb61d58SRichard Weinberger 		ubifs_assert(c, c->lst.taken_empty_lebs > 0);
1551552ff317SArtem Bityutskiy 
15521e51764aSArtem Bityutskiy 	err = dbg_check_filesystem(c);
15531e51764aSArtem Bityutskiy 	if (err)
15541e51764aSArtem Bityutskiy 		goto out_infos;
15551e51764aSArtem Bityutskiy 
1556702d6a83SGreg Kroah-Hartman 	dbg_debugfs_init_fs(c);
15576ba87c9bSArtem Bityutskiy 
155818d1d7fbSArtem Bityutskiy 	c->mounting = 0;
15592953e73fSAdrian Hunter 
1560235c362bSSheng Yong 	ubifs_msg(c, "UBIFS: mounted UBI device %d, volume %d, name \"%s\"%s",
15613668b70fSArtem Bityutskiy 		  c->vi.ubi_num, c->vi.vol_id, c->vi.name,
1562beadadfaSRichard Genoud 		  c->ro_mount ? ", R/O mode" : "");
15631e51764aSArtem Bityutskiy 	x = (long long)c->main_lebs * c->leb_size;
15643668b70fSArtem Bityutskiy 	y = (long long)c->log_lebs * c->leb_size + c->max_bud_bytes;
1565235c362bSSheng Yong 	ubifs_msg(c, "LEB size: %d bytes (%d KiB), min./max. I/O unit sizes: %d bytes/%d bytes",
15663668b70fSArtem Bityutskiy 		  c->leb_size, c->leb_size >> 10, c->min_io_size,
15673668b70fSArtem Bityutskiy 		  c->max_write_size);
1568829ad58aSMartin Devera 	ubifs_msg(c, "FS size: %lld bytes (%lld MiB, %d LEBs), max %d LEBs, journal size %lld bytes (%lld MiB, %d LEBs)",
1569829ad58aSMartin Devera 		  x, x >> 20, c->main_lebs, c->max_leb_cnt,
15703668b70fSArtem Bityutskiy 		  y, y >> 20, c->log_lebs + c->max_bud_cnt);
1571235c362bSSheng Yong 	ubifs_msg(c, "reserved for root: %llu bytes (%llu KiB)",
1572948cfb21SArtem Bityutskiy 		  c->report_rp_size, c->report_rp_size >> 10);
1573235c362bSSheng Yong 	ubifs_msg(c, "media format: w%d/r%d (latest is w%d/r%d), UUID %pUB%s",
15743668b70fSArtem Bityutskiy 		  c->fmt_version, c->ro_compat_version,
15753668b70fSArtem Bityutskiy 		  UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION, c->uuid,
15763668b70fSArtem Bityutskiy 		  c->big_lpt ? ", big LPT model" : ", small LPT model");
15771e51764aSArtem Bityutskiy 
15786eb61d58SRichard Weinberger 	dbg_gen("default compressor:  %s", ubifs_compr_name(c, c->default_compr));
15793668b70fSArtem Bityutskiy 	dbg_gen("data journal heads:  %d",
15801e51764aSArtem Bityutskiy 		c->jhead_cnt - NONDATA_JHEADS_CNT);
15813668b70fSArtem Bityutskiy 	dbg_gen("log LEBs:            %d (%d - %d)",
15821e51764aSArtem Bityutskiy 		c->log_lebs, UBIFS_LOG_LNUM, c->log_last);
15833668b70fSArtem Bityutskiy 	dbg_gen("LPT area LEBs:       %d (%d - %d)",
15841e51764aSArtem Bityutskiy 		c->lpt_lebs, c->lpt_first, c->lpt_last);
15853668b70fSArtem Bityutskiy 	dbg_gen("orphan area LEBs:    %d (%d - %d)",
15861e51764aSArtem Bityutskiy 		c->orph_lebs, c->orph_first, c->orph_last);
15873668b70fSArtem Bityutskiy 	dbg_gen("main area LEBs:      %d (%d - %d)",
15881e51764aSArtem Bityutskiy 		c->main_lebs, c->main_first, c->leb_cnt - 1);
15893668b70fSArtem Bityutskiy 	dbg_gen("index LEBs:          %d", c->lst.idx_lebs);
159089f40d0aSFangping Liang 	dbg_gen("total index bytes:   %llu (%llu KiB, %llu MiB)",
1591b137545cSArtem Bityutskiy 		c->bi.old_idx_sz, c->bi.old_idx_sz >> 10,
1592b137545cSArtem Bityutskiy 		c->bi.old_idx_sz >> 20);
15933668b70fSArtem Bityutskiy 	dbg_gen("key hash type:       %d", c->key_hash_type);
15943668b70fSArtem Bityutskiy 	dbg_gen("tree fanout:         %d", c->fanout);
15953668b70fSArtem Bityutskiy 	dbg_gen("reserved GC LEB:     %d", c->gc_lnum);
15963668b70fSArtem Bityutskiy 	dbg_gen("max. znode size      %d", c->max_znode_sz);
15973668b70fSArtem Bityutskiy 	dbg_gen("max. index node size %d", c->max_idx_node_sz);
15983668b70fSArtem Bityutskiy 	dbg_gen("node sizes:          data %zu, inode %zu, dentry %zu",
15998e5033adSArtem Bityutskiy 		UBIFS_DATA_NODE_SZ, UBIFS_INO_NODE_SZ, UBIFS_DENT_NODE_SZ);
16003668b70fSArtem Bityutskiy 	dbg_gen("node sizes:          trun %zu, sb %zu, master %zu",
16018e5033adSArtem Bityutskiy 		UBIFS_TRUN_NODE_SZ, UBIFS_SB_NODE_SZ, UBIFS_MST_NODE_SZ);
16023668b70fSArtem Bityutskiy 	dbg_gen("node sizes:          ref %zu, cmt. start %zu, orph %zu",
16038e5033adSArtem Bityutskiy 		UBIFS_REF_NODE_SZ, UBIFS_CS_NODE_SZ, UBIFS_ORPH_NODE_SZ);
16043668b70fSArtem Bityutskiy 	dbg_gen("max. node sizes:     data %zu, inode %zu dentry %zu, idx %d",
16058e5033adSArtem Bityutskiy 		UBIFS_MAX_DATA_NODE_SZ, UBIFS_MAX_INO_NODE_SZ,
16066342aaebSArtem Bityutskiy 		UBIFS_MAX_DENT_NODE_SZ, ubifs_idx_node_sz(c, c->fanout));
16073668b70fSArtem Bityutskiy 	dbg_gen("dead watermark:      %d", c->dead_wm);
16083668b70fSArtem Bityutskiy 	dbg_gen("dark watermark:      %d", c->dark_wm);
16093668b70fSArtem Bityutskiy 	dbg_gen("LEB overhead:        %d", c->leb_overhead);
16101e51764aSArtem Bityutskiy 	x = (long long)c->main_lebs * c->dark_wm;
16113668b70fSArtem Bityutskiy 	dbg_gen("max. dark space:     %lld (%lld KiB, %lld MiB)",
16121e51764aSArtem Bityutskiy 		x, x >> 10, x >> 20);
16133668b70fSArtem Bityutskiy 	dbg_gen("maximum bud bytes:   %lld (%lld KiB, %lld MiB)",
16141e51764aSArtem Bityutskiy 		c->max_bud_bytes, c->max_bud_bytes >> 10,
16151e51764aSArtem Bityutskiy 		c->max_bud_bytes >> 20);
16163668b70fSArtem Bityutskiy 	dbg_gen("BG commit bud bytes: %lld (%lld KiB, %lld MiB)",
16171e51764aSArtem Bityutskiy 		c->bg_bud_bytes, c->bg_bud_bytes >> 10,
16181e51764aSArtem Bityutskiy 		c->bg_bud_bytes >> 20);
16193668b70fSArtem Bityutskiy 	dbg_gen("current bud bytes    %lld (%lld KiB, %lld MiB)",
16201e51764aSArtem Bityutskiy 		c->bud_bytes, c->bud_bytes >> 10, c->bud_bytes >> 20);
16213668b70fSArtem Bityutskiy 	dbg_gen("max. seq. number:    %llu", c->max_sqnum);
16223668b70fSArtem Bityutskiy 	dbg_gen("commit number:       %llu", c->cmt_no);
16239ca2d732SRichard Weinberger 	dbg_gen("max. xattrs per inode: %d", ubifs_xattr_max_cnt(c));
16249ca2d732SRichard Weinberger 	dbg_gen("max orphans:           %d", c->max_orphans);
16251e51764aSArtem Bityutskiy 
16261e51764aSArtem Bityutskiy 	return 0;
16271e51764aSArtem Bityutskiy 
16281e51764aSArtem Bityutskiy out_infos:
16291e51764aSArtem Bityutskiy 	spin_lock(&ubifs_infos_lock);
16301e51764aSArtem Bityutskiy 	list_del(&c->infos_list);
16311e51764aSArtem Bityutskiy 	spin_unlock(&ubifs_infos_lock);
16321e51764aSArtem Bityutskiy out_orphans:
16331e51764aSArtem Bityutskiy 	free_orphans(c);
16341e51764aSArtem Bityutskiy out_journal:
16351e51764aSArtem Bityutskiy 	destroy_journal(c);
16361e51764aSArtem Bityutskiy out_lpt:
16371e51764aSArtem Bityutskiy 	ubifs_lpt_free(c, 0);
16381e51764aSArtem Bityutskiy out_master:
16391e51764aSArtem Bityutskiy 	kfree(c->mst_node);
16401e51764aSArtem Bityutskiy 	kfree(c->rcvrd_mst_node);
16411e51764aSArtem Bityutskiy 	if (c->bgt)
16421e51764aSArtem Bityutskiy 		kthread_stop(c->bgt);
16431e51764aSArtem Bityutskiy out_wbufs:
16441e51764aSArtem Bityutskiy 	free_wbufs(c);
16451e51764aSArtem Bityutskiy out_cbuf:
16461e51764aSArtem Bityutskiy 	kfree(c->cbuf);
1647e2a05cc7SZhihao Cheng out_auth:
1648e2a05cc7SZhihao Cheng 	ubifs_exit_authentication(c);
16491e51764aSArtem Bityutskiy out_free:
1650d882962fSMatthew L. Creech 	kfree(c->write_reserve_buf);
16513477d204SArtem Bityutskiy 	kfree(c->bu.buf);
16521e51764aSArtem Bityutskiy 	vfree(c->ileb_buf);
16531e51764aSArtem Bityutskiy 	vfree(c->sbuf);
16541e51764aSArtem Bityutskiy 	kfree(c->bottom_up_buf);
1655ff90bdfbSQuanyang Wang 	kfree(c->sup_node);
16562e3cbf42SStefan Schaeckeler 	ubifs_sysfs_unregister(c);
16572e3cbf42SStefan Schaeckeler out_debugging:
165817c2f9f8SArtem Bityutskiy 	ubifs_debugging_exit(c);
16591e51764aSArtem Bityutskiy 	return err;
16601e51764aSArtem Bityutskiy }
16611e51764aSArtem Bityutskiy 
16621e51764aSArtem Bityutskiy /**
16631e51764aSArtem Bityutskiy  * ubifs_umount - un-mount UBIFS file-system.
16641e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
16651e51764aSArtem Bityutskiy  *
16661e51764aSArtem Bityutskiy  * Note, this function is called to free allocated resourced when un-mounting,
16671e51764aSArtem Bityutskiy  * as well as free resources when an error occurred while we were half way
16681e51764aSArtem Bityutskiy  * through mounting (error path cleanup function). So it has to make sure the
16691e51764aSArtem Bityutskiy  * resource was actually allocated before freeing it.
16701e51764aSArtem Bityutskiy  */
ubifs_umount(struct ubifs_info * c)16711e51764aSArtem Bityutskiy static void ubifs_umount(struct ubifs_info *c)
16721e51764aSArtem Bityutskiy {
16731e51764aSArtem Bityutskiy 	dbg_gen("un-mounting UBI device %d, volume %d", c->vi.ubi_num,
16741e51764aSArtem Bityutskiy 		c->vi.vol_id);
16751e51764aSArtem Bityutskiy 
1676552ff317SArtem Bityutskiy 	dbg_debugfs_exit_fs(c);
16771e51764aSArtem Bityutskiy 	spin_lock(&ubifs_infos_lock);
16781e51764aSArtem Bityutskiy 	list_del(&c->infos_list);
16791e51764aSArtem Bityutskiy 	spin_unlock(&ubifs_infos_lock);
16801e51764aSArtem Bityutskiy 
16811e51764aSArtem Bityutskiy 	if (c->bgt)
16821e51764aSArtem Bityutskiy 		kthread_stop(c->bgt);
16831e51764aSArtem Bityutskiy 
16841e51764aSArtem Bityutskiy 	destroy_journal(c);
16851e51764aSArtem Bityutskiy 	free_wbufs(c);
16861e51764aSArtem Bityutskiy 	free_orphans(c);
16871e51764aSArtem Bityutskiy 	ubifs_lpt_free(c, 0);
1688d8a22773SSascha Hauer 	ubifs_exit_authentication(c);
16891e51764aSArtem Bityutskiy 
169047f6d9ceSZhihao Cheng 	ubifs_release_options(c);
16911e51764aSArtem Bityutskiy 	kfree(c->cbuf);
16921e51764aSArtem Bityutskiy 	kfree(c->rcvrd_mst_node);
16931e51764aSArtem Bityutskiy 	kfree(c->mst_node);
1694d882962fSMatthew L. Creech 	kfree(c->write_reserve_buf);
16953477d204SArtem Bityutskiy 	kfree(c->bu.buf);
16963477d204SArtem Bityutskiy 	vfree(c->ileb_buf);
16971e51764aSArtem Bityutskiy 	vfree(c->sbuf);
16981e51764aSArtem Bityutskiy 	kfree(c->bottom_up_buf);
1699ff90bdfbSQuanyang Wang 	kfree(c->sup_node);
170017c2f9f8SArtem Bityutskiy 	ubifs_debugging_exit(c);
17012e3cbf42SStefan Schaeckeler 	ubifs_sysfs_unregister(c);
17021e51764aSArtem Bityutskiy }
17031e51764aSArtem Bityutskiy 
17041e51764aSArtem Bityutskiy /**
17051e51764aSArtem Bityutskiy  * ubifs_remount_rw - re-mount in read-write mode.
17061e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
17071e51764aSArtem Bityutskiy  *
17081e51764aSArtem Bityutskiy  * UBIFS avoids allocating many unnecessary resources when mounted in read-only
17091e51764aSArtem Bityutskiy  * mode. This function allocates the needed resources and re-mounts UBIFS in
17101e51764aSArtem Bityutskiy  * read-write mode.
17111e51764aSArtem Bityutskiy  */
ubifs_remount_rw(struct ubifs_info * c)17121e51764aSArtem Bityutskiy static int ubifs_remount_rw(struct ubifs_info *c)
17131e51764aSArtem Bityutskiy {
17141e51764aSArtem Bityutskiy 	int err, lnum;
17151e51764aSArtem Bityutskiy 
1716963f0cf6SArtem Bityutskiy 	if (c->rw_incompat) {
1717235c362bSSheng Yong 		ubifs_err(c, "the file-system is not R/W-compatible");
1718235c362bSSheng Yong 		ubifs_msg(c, "on-flash format version is w%d/r%d, but software only supports up to version w%d/r%d",
171979fda517SArtem Bityutskiy 			  c->fmt_version, c->ro_compat_version,
172079fda517SArtem Bityutskiy 			  UBIFS_FORMAT_VERSION, UBIFS_RO_COMPAT_VERSION);
1721963f0cf6SArtem Bityutskiy 		return -EROFS;
1722963f0cf6SArtem Bityutskiy 	}
1723963f0cf6SArtem Bityutskiy 
17241e51764aSArtem Bityutskiy 	mutex_lock(&c->umount_mutex);
172584abf972SArtem Bityutskiy 	dbg_save_space_info(c);
17261e51764aSArtem Bityutskiy 	c->remounting_rw = 1;
1727c88ac00cSArtem Bityutskiy 	c->ro_mount = 0;
17281e51764aSArtem Bityutskiy 
172967e753caSArtem Bityutskiy 	if (c->space_fixup) {
173067e753caSArtem Bityutskiy 		err = ubifs_fixup_free_space(c);
173167e753caSArtem Bityutskiy 		if (err)
1732fcdd57c8SArtem Bityutskiy 			goto out;
173367e753caSArtem Bityutskiy 	}
173467e753caSArtem Bityutskiy 
173557a450e9SArtem Bityutskiy 	err = check_free_space(c);
173657a450e9SArtem Bityutskiy 	if (err)
17371e51764aSArtem Bityutskiy 		goto out;
17381e51764aSArtem Bityutskiy 
17391e51764aSArtem Bityutskiy 	if (c->need_recovery) {
1740235c362bSSheng Yong 		ubifs_msg(c, "completing deferred recovery");
17411e51764aSArtem Bityutskiy 		err = ubifs_write_rcvrd_mst_node(c);
17421e51764aSArtem Bityutskiy 		if (err)
17431e51764aSArtem Bityutskiy 			goto out;
17441e76592fSSascha Hauer 		if (!ubifs_authenticated(c)) {
17451e76592fSSascha Hauer 			err = ubifs_recover_size(c, true);
17461e51764aSArtem Bityutskiy 			if (err)
17471e51764aSArtem Bityutskiy 				goto out;
17481e76592fSSascha Hauer 		}
17491e51764aSArtem Bityutskiy 		err = ubifs_clean_lebs(c, c->sbuf);
17501e51764aSArtem Bityutskiy 		if (err)
17511e51764aSArtem Bityutskiy 			goto out;
17521e51764aSArtem Bityutskiy 		err = ubifs_recover_inl_heads(c, c->sbuf);
17531e51764aSArtem Bityutskiy 		if (err)
17541e51764aSArtem Bityutskiy 			goto out;
175549d128aaSAdrian Hunter 	} else {
175649d128aaSAdrian Hunter 		/* A readonly mount is not allowed to have orphans */
17576eb61d58SRichard Weinberger 		ubifs_assert(c, c->tot_orphans == 0);
175849d128aaSAdrian Hunter 		err = ubifs_clear_orphans(c);
175949d128aaSAdrian Hunter 		if (err)
176049d128aaSAdrian Hunter 			goto out;
17611e51764aSArtem Bityutskiy 	}
17621e51764aSArtem Bityutskiy 
17631e51764aSArtem Bityutskiy 	if (!(c->mst_node->flags & cpu_to_le32(UBIFS_MST_DIRTY))) {
17641e51764aSArtem Bityutskiy 		c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
17651e51764aSArtem Bityutskiy 		err = ubifs_write_master(c);
17661e51764aSArtem Bityutskiy 		if (err)
17671e51764aSArtem Bityutskiy 			goto out;
17681e51764aSArtem Bityutskiy 	}
17691e51764aSArtem Bityutskiy 
1770817aa094SSascha Hauer 	if (c->superblock_need_write) {
1771817aa094SSascha Hauer 		struct ubifs_sb_node *sup = c->sup_node;
1772817aa094SSascha Hauer 
1773817aa094SSascha Hauer 		err = ubifs_write_sb_node(c, sup);
1774817aa094SSascha Hauer 		if (err)
1775817aa094SSascha Hauer 			goto out;
1776817aa094SSascha Hauer 
1777817aa094SSascha Hauer 		c->superblock_need_write = 0;
1778817aa094SSascha Hauer 	}
1779817aa094SSascha Hauer 
17801e51764aSArtem Bityutskiy 	c->ileb_buf = vmalloc(c->leb_size);
17811e51764aSArtem Bityutskiy 	if (!c->ileb_buf) {
17821e51764aSArtem Bityutskiy 		err = -ENOMEM;
17831e51764aSArtem Bityutskiy 		goto out;
17841e51764aSArtem Bityutskiy 	}
17851e51764aSArtem Bityutskiy 
17867799953bSRichard Weinberger 	c->write_reserve_buf = kmalloc(COMPRESSED_DATA_NODE_BUF_SZ + \
17877799953bSRichard Weinberger 				       UBIFS_CIPHER_BLOCK_SIZE, GFP_KERNEL);
17887203db97SWei Yongjun 	if (!c->write_reserve_buf) {
17897203db97SWei Yongjun 		err = -ENOMEM;
1790d882962fSMatthew L. Creech 		goto out;
17917203db97SWei Yongjun 	}
1792d882962fSMatthew L. Creech 
17931e51764aSArtem Bityutskiy 	err = ubifs_lpt_init(c, 0, 1);
17941e51764aSArtem Bityutskiy 	if (err)
17951e51764aSArtem Bityutskiy 		goto out;
17961e51764aSArtem Bityutskiy 
17971e51764aSArtem Bityutskiy 	/* Create background thread */
1798d98c6c35SCai Huoqing 	c->bgt = kthread_run(ubifs_bg_thread, c, "%s", c->bgt_name);
17991e51764aSArtem Bityutskiy 	if (IS_ERR(c->bgt)) {
18001e51764aSArtem Bityutskiy 		err = PTR_ERR(c->bgt);
18011e51764aSArtem Bityutskiy 		c->bgt = NULL;
1802235c362bSSheng Yong 		ubifs_err(c, "cannot spawn \"%s\", error %d",
18031e51764aSArtem Bityutskiy 			  c->bgt_name, err);
18042953e73fSAdrian Hunter 		goto out;
18051e51764aSArtem Bityutskiy 	}
18061e51764aSArtem Bityutskiy 
18071e51764aSArtem Bityutskiy 	c->orph_buf = vmalloc(c->leb_size);
18082953e73fSAdrian Hunter 	if (!c->orph_buf) {
18092953e73fSAdrian Hunter 		err = -ENOMEM;
18102953e73fSAdrian Hunter 		goto out;
18112953e73fSAdrian Hunter 	}
18121e51764aSArtem Bityutskiy 
18131e51764aSArtem Bityutskiy 	/* Check for enough log space */
18141e51764aSArtem Bityutskiy 	lnum = c->lhead_lnum + 1;
18151e51764aSArtem Bityutskiy 	if (lnum >= UBIFS_LOG_LNUM + c->log_lebs)
18161e51764aSArtem Bityutskiy 		lnum = UBIFS_LOG_LNUM;
18171e51764aSArtem Bityutskiy 	if (lnum == c->ltail_lnum) {
18181e51764aSArtem Bityutskiy 		err = ubifs_consolidate_log(c);
18191e51764aSArtem Bityutskiy 		if (err)
18201e51764aSArtem Bityutskiy 			goto out;
18211e51764aSArtem Bityutskiy 	}
18221e51764aSArtem Bityutskiy 
18231e76592fSSascha Hauer 	if (c->need_recovery) {
18241e51764aSArtem Bityutskiy 		err = ubifs_rcvry_gc_commit(c);
18251e76592fSSascha Hauer 		if (err)
18261e76592fSSascha Hauer 			goto out;
18271e76592fSSascha Hauer 
18281e76592fSSascha Hauer 		if (ubifs_authenticated(c)) {
18291e76592fSSascha Hauer 			err = ubifs_recover_size(c, false);
18301e76592fSSascha Hauer 			if (err)
18311e76592fSSascha Hauer 				goto out;
18321e76592fSSascha Hauer 		}
18331e76592fSSascha Hauer 	} else {
1834b4978e94SArtem Bityutskiy 		err = ubifs_leb_unmap(c, c->gc_lnum);
18351e76592fSSascha Hauer 	}
18361e51764aSArtem Bityutskiy 	if (err)
18371e51764aSArtem Bityutskiy 		goto out;
18381e51764aSArtem Bityutskiy 
18398c230d9aSArtem Bityutskiy 	dbg_gen("re-mounted read-write");
18408c230d9aSArtem Bityutskiy 	c->remounting_rw = 0;
18418c230d9aSArtem Bityutskiy 
18421e51764aSArtem Bityutskiy 	if (c->need_recovery) {
18431e51764aSArtem Bityutskiy 		c->need_recovery = 0;
1844235c362bSSheng Yong 		ubifs_msg(c, "deferred recovery completed");
18458c230d9aSArtem Bityutskiy 	} else {
18468c230d9aSArtem Bityutskiy 		/*
18478c230d9aSArtem Bityutskiy 		 * Do not run the debugging space check if the were doing
18488c230d9aSArtem Bityutskiy 		 * recovery, because when we saved the information we had the
18498c230d9aSArtem Bityutskiy 		 * file-system in a state where the TNC and lprops has been
18508c230d9aSArtem Bityutskiy 		 * modified in memory, but all the I/O operations (including a
18518c230d9aSArtem Bityutskiy 		 * commit) were deferred. So the file-system was in
18528c230d9aSArtem Bityutskiy 		 * "non-committed" state. Now the file-system is in committed
18538c230d9aSArtem Bityutskiy 		 * state, and of course the amount of free space will change
18548c230d9aSArtem Bityutskiy 		 * because, for example, the old index size was imprecise.
18558c230d9aSArtem Bityutskiy 		 */
185684abf972SArtem Bityutskiy 		err = dbg_check_space_info(c);
18578c230d9aSArtem Bityutskiy 	}
18589d510db4SMatthew L. Creech 
18591e51764aSArtem Bityutskiy 	mutex_unlock(&c->umount_mutex);
186084abf972SArtem Bityutskiy 	return err;
18611e51764aSArtem Bityutskiy 
18621e51764aSArtem Bityutskiy out:
1863c88ac00cSArtem Bityutskiy 	c->ro_mount = 1;
18641e51764aSArtem Bityutskiy 	vfree(c->orph_buf);
18651e51764aSArtem Bityutskiy 	c->orph_buf = NULL;
18661e51764aSArtem Bityutskiy 	if (c->bgt) {
18671e51764aSArtem Bityutskiy 		kthread_stop(c->bgt);
18681e51764aSArtem Bityutskiy 		c->bgt = NULL;
18691e51764aSArtem Bityutskiy 	}
1870d882962fSMatthew L. Creech 	kfree(c->write_reserve_buf);
1871d882962fSMatthew L. Creech 	c->write_reserve_buf = NULL;
18721e51764aSArtem Bityutskiy 	vfree(c->ileb_buf);
18731e51764aSArtem Bityutskiy 	c->ileb_buf = NULL;
18741e51764aSArtem Bityutskiy 	ubifs_lpt_free(c, 1);
18751e51764aSArtem Bityutskiy 	c->remounting_rw = 0;
18761e51764aSArtem Bityutskiy 	mutex_unlock(&c->umount_mutex);
18771e51764aSArtem Bityutskiy 	return err;
18781e51764aSArtem Bityutskiy }
18791e51764aSArtem Bityutskiy 
18801e51764aSArtem Bityutskiy /**
18811e51764aSArtem Bityutskiy  * ubifs_remount_ro - re-mount in read-only mode.
18821e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
18831e51764aSArtem Bityutskiy  *
188484abf972SArtem Bityutskiy  * We assume VFS has stopped writing. Possibly the background thread could be
188584abf972SArtem Bityutskiy  * running a commit, however kthread_stop will wait in that case.
18861e51764aSArtem Bityutskiy  */
ubifs_remount_ro(struct ubifs_info * c)18871e51764aSArtem Bityutskiy static void ubifs_remount_ro(struct ubifs_info *c)
18881e51764aSArtem Bityutskiy {
18891e51764aSArtem Bityutskiy 	int i, err;
18901e51764aSArtem Bityutskiy 
18916eb61d58SRichard Weinberger 	ubifs_assert(c, !c->need_recovery);
18926eb61d58SRichard Weinberger 	ubifs_assert(c, !c->ro_mount);
1893e4d9b6cbSArtem Bityutskiy 
18941e51764aSArtem Bityutskiy 	mutex_lock(&c->umount_mutex);
18951e51764aSArtem Bityutskiy 	if (c->bgt) {
18961e51764aSArtem Bityutskiy 		kthread_stop(c->bgt);
18971e51764aSArtem Bityutskiy 		c->bgt = NULL;
18981e51764aSArtem Bityutskiy 	}
18991e51764aSArtem Bityutskiy 
190084abf972SArtem Bityutskiy 	dbg_save_space_info(c);
190184abf972SArtem Bityutskiy 
1902aac17948SRichard Weinberger 	for (i = 0; i < c->jhead_cnt; i++) {
1903aac17948SRichard Weinberger 		err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
1904aac17948SRichard Weinberger 		if (err)
1905aac17948SRichard Weinberger 			ubifs_ro_mode(c, err);
1906aac17948SRichard Weinberger 	}
19071e51764aSArtem Bityutskiy 
19081e51764aSArtem Bityutskiy 	c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
19091e51764aSArtem Bityutskiy 	c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
19101e51764aSArtem Bityutskiy 	c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
19111e51764aSArtem Bityutskiy 	err = ubifs_write_master(c);
19121e51764aSArtem Bityutskiy 	if (err)
19131e51764aSArtem Bityutskiy 		ubifs_ro_mode(c, err);
19141e51764aSArtem Bityutskiy 
19151e51764aSArtem Bityutskiy 	vfree(c->orph_buf);
19161e51764aSArtem Bityutskiy 	c->orph_buf = NULL;
1917d882962fSMatthew L. Creech 	kfree(c->write_reserve_buf);
1918d882962fSMatthew L. Creech 	c->write_reserve_buf = NULL;
19191e51764aSArtem Bityutskiy 	vfree(c->ileb_buf);
19201e51764aSArtem Bityutskiy 	c->ileb_buf = NULL;
19211e51764aSArtem Bityutskiy 	ubifs_lpt_free(c, 1);
19222ef13294SArtem Bityutskiy 	c->ro_mount = 1;
192384abf972SArtem Bityutskiy 	err = dbg_check_space_info(c);
192484abf972SArtem Bityutskiy 	if (err)
192584abf972SArtem Bityutskiy 		ubifs_ro_mode(c, err);
19261e51764aSArtem Bityutskiy 	mutex_unlock(&c->umount_mutex);
19271e51764aSArtem Bityutskiy }
19281e51764aSArtem Bityutskiy 
ubifs_put_super(struct super_block * sb)19291e51764aSArtem Bityutskiy static void ubifs_put_super(struct super_block *sb)
19301e51764aSArtem Bityutskiy {
19311e51764aSArtem Bityutskiy 	int i;
19321e51764aSArtem Bityutskiy 	struct ubifs_info *c = sb->s_fs_info;
19331e51764aSArtem Bityutskiy 
1934235c362bSSheng Yong 	ubifs_msg(c, "un-mount UBI device %d", c->vi.ubi_num);
19356cfd0148SChristoph Hellwig 
19361e51764aSArtem Bityutskiy 	/*
19371e51764aSArtem Bityutskiy 	 * The following asserts are only valid if there has not been a failure
19381e51764aSArtem Bityutskiy 	 * of the media. For example, there will be dirty inodes if we failed
19391e51764aSArtem Bityutskiy 	 * to write them back because of I/O errors.
19401e51764aSArtem Bityutskiy 	 */
19411a067a22SArtem Bityutskiy 	if (!c->ro_error) {
19426eb61d58SRichard Weinberger 		ubifs_assert(c, c->bi.idx_growth == 0);
19436eb61d58SRichard Weinberger 		ubifs_assert(c, c->bi.dd_growth == 0);
19446eb61d58SRichard Weinberger 		ubifs_assert(c, c->bi.data_growth == 0);
19451a067a22SArtem Bityutskiy 	}
19461e51764aSArtem Bityutskiy 
19471e51764aSArtem Bityutskiy 	/*
19481e51764aSArtem Bityutskiy 	 * The 'c->umount_lock' prevents races between UBIFS memory shrinker
19491e51764aSArtem Bityutskiy 	 * and file system un-mount. Namely, it prevents the shrinker from
19501e51764aSArtem Bityutskiy 	 * picking this superblock for shrinking - it will be just skipped if
19511e51764aSArtem Bityutskiy 	 * the mutex is locked.
19521e51764aSArtem Bityutskiy 	 */
19531e51764aSArtem Bityutskiy 	mutex_lock(&c->umount_mutex);
19542ef13294SArtem Bityutskiy 	if (!c->ro_mount) {
19551e51764aSArtem Bityutskiy 		/*
19561e51764aSArtem Bityutskiy 		 * First of all kill the background thread to make sure it does
19571e51764aSArtem Bityutskiy 		 * not interfere with un-mounting and freeing resources.
19581e51764aSArtem Bityutskiy 		 */
19591e51764aSArtem Bityutskiy 		if (c->bgt) {
19601e51764aSArtem Bityutskiy 			kthread_stop(c->bgt);
19611e51764aSArtem Bityutskiy 			c->bgt = NULL;
19621e51764aSArtem Bityutskiy 		}
19631e51764aSArtem Bityutskiy 
19642ef13294SArtem Bityutskiy 		/*
19652ef13294SArtem Bityutskiy 		 * On fatal errors c->ro_error is set to 1, in which case we do
19662ef13294SArtem Bityutskiy 		 * not write the master node.
19672ef13294SArtem Bityutskiy 		 */
19682ef13294SArtem Bityutskiy 		if (!c->ro_error) {
19692ef13294SArtem Bityutskiy 			int err;
19702ef13294SArtem Bityutskiy 
19711e51764aSArtem Bityutskiy 			/* Synchronize write-buffers */
1972aac17948SRichard Weinberger 			for (i = 0; i < c->jhead_cnt; i++) {
1973aac17948SRichard Weinberger 				err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
1974aac17948SRichard Weinberger 				if (err)
1975aac17948SRichard Weinberger 					ubifs_ro_mode(c, err);
1976aac17948SRichard Weinberger 			}
19771e51764aSArtem Bityutskiy 
19781e51764aSArtem Bityutskiy 			/*
19791e51764aSArtem Bityutskiy 			 * We are being cleanly unmounted which means the
19801e51764aSArtem Bityutskiy 			 * orphans were killed - indicate this in the master
19811e51764aSArtem Bityutskiy 			 * node. Also save the reserved GC LEB number.
19821e51764aSArtem Bityutskiy 			 */
19831e51764aSArtem Bityutskiy 			c->mst_node->flags &= ~cpu_to_le32(UBIFS_MST_DIRTY);
19841e51764aSArtem Bityutskiy 			c->mst_node->flags |= cpu_to_le32(UBIFS_MST_NO_ORPHS);
19851e51764aSArtem Bityutskiy 			c->mst_node->gc_lnum = cpu_to_le32(c->gc_lnum);
19861e51764aSArtem Bityutskiy 			err = ubifs_write_master(c);
19871e51764aSArtem Bityutskiy 			if (err)
19881e51764aSArtem Bityutskiy 				/*
19891e51764aSArtem Bityutskiy 				 * Recovery will attempt to fix the master area
19901e51764aSArtem Bityutskiy 				 * next mount, so we just print a message and
19911e51764aSArtem Bityutskiy 				 * continue to unmount normally.
19921e51764aSArtem Bityutskiy 				 */
1993235c362bSSheng Yong 				ubifs_err(c, "failed to write master node, error %d",
199479fda517SArtem Bityutskiy 					  err);
19953601ba27SArtem Bityutskiy 		} else {
19963601ba27SArtem Bityutskiy 			for (i = 0; i < c->jhead_cnt; i++)
19973601ba27SArtem Bityutskiy 				/* Make sure write-buffer timers are canceled */
19983601ba27SArtem Bityutskiy 				hrtimer_cancel(&c->jheads[i].wbuf.timer);
19991e51764aSArtem Bityutskiy 		}
20001e51764aSArtem Bityutskiy 	}
20011e51764aSArtem Bityutskiy 
20021e51764aSArtem Bityutskiy 	ubifs_umount(c);
20031e51764aSArtem Bityutskiy 	ubi_close_volume(c->ubi);
20041e51764aSArtem Bityutskiy 	mutex_unlock(&c->umount_mutex);
20051e51764aSArtem Bityutskiy }
20061e51764aSArtem Bityutskiy 
ubifs_remount_fs(struct super_block * sb,int * flags,char * data)20071e51764aSArtem Bityutskiy static int ubifs_remount_fs(struct super_block *sb, int *flags, char *data)
20081e51764aSArtem Bityutskiy {
20091e51764aSArtem Bityutskiy 	int err;
20101e51764aSArtem Bityutskiy 	struct ubifs_info *c = sb->s_fs_info;
20111e51764aSArtem Bityutskiy 
201202b9984dSTheodore Ts'o 	sync_filesystem(sb);
20131e51764aSArtem Bityutskiy 	dbg_gen("old flags %#lx, new flags %#x", sb->s_flags, *flags);
20141e51764aSArtem Bityutskiy 
20151e51764aSArtem Bityutskiy 	err = ubifs_parse_options(c, data, 1);
20161e51764aSArtem Bityutskiy 	if (err) {
2017235c362bSSheng Yong 		ubifs_err(c, "invalid or unknown remount parameter");
20181e51764aSArtem Bityutskiy 		return err;
20191e51764aSArtem Bityutskiy 	}
20203477d204SArtem Bityutskiy 
20211751e8a6SLinus Torvalds 	if (c->ro_mount && !(*flags & SB_RDONLY)) {
20222680d722SArtem Bityutskiy 		if (c->ro_error) {
2023235c362bSSheng Yong 			ubifs_msg(c, "cannot re-mount R/W due to prior errors");
20242680d722SArtem Bityutskiy 			return -EROFS;
20252680d722SArtem Bityutskiy 		}
2026e4d9b6cbSArtem Bityutskiy 		if (c->ro_media) {
2027235c362bSSheng Yong 			ubifs_msg(c, "cannot re-mount R/W - UBI volume is R/O");
2028a2b9df3fSArtem Bityutskiy 			return -EROFS;
2029e4d9b6cbSArtem Bityutskiy 		}
20301e51764aSArtem Bityutskiy 		err = ubifs_remount_rw(c);
2031e9d6bbc4SArtem Bityutskiy 		if (err)
20321e51764aSArtem Bityutskiy 			return err;
20331751e8a6SLinus Torvalds 	} else if (!c->ro_mount && (*flags & SB_RDONLY)) {
20342680d722SArtem Bityutskiy 		if (c->ro_error) {
2035235c362bSSheng Yong 			ubifs_msg(c, "cannot re-mount R/O due to prior errors");
2036a2b9df3fSArtem Bityutskiy 			return -EROFS;
2037b466f17dSAdrian Hunter 		}
20381e51764aSArtem Bityutskiy 		ubifs_remount_ro(c);
2039b466f17dSAdrian Hunter 	}
20401e51764aSArtem Bityutskiy 
20413477d204SArtem Bityutskiy 	if (c->bulk_read == 1)
20423477d204SArtem Bityutskiy 		bu_init(c);
20433477d204SArtem Bityutskiy 	else {
20443477d204SArtem Bityutskiy 		dbg_gen("disable bulk-read");
204507d41c3cSkaram.lee 		mutex_lock(&c->bu_mutex);
20463477d204SArtem Bityutskiy 		kfree(c->bu.buf);
20473477d204SArtem Bityutskiy 		c->bu.buf = NULL;
204807d41c3cSkaram.lee 		mutex_unlock(&c->bu_mutex);
20493477d204SArtem Bityutskiy 	}
20503477d204SArtem Bityutskiy 
2051d3bdc016SSascha Hauer 	if (!c->need_recovery)
20526eb61d58SRichard Weinberger 		ubifs_assert(c, c->lst.taken_empty_lebs > 0);
2053d3bdc016SSascha Hauer 
20541e51764aSArtem Bityutskiy 	return 0;
20551e51764aSArtem Bityutskiy }
20561e51764aSArtem Bityutskiy 
2057e8b81566SArtem Bityutskiy const struct super_operations ubifs_super_operations = {
20581e51764aSArtem Bityutskiy 	.alloc_inode   = ubifs_alloc_inode,
2059dc431759SAl Viro 	.free_inode    = ubifs_free_inode,
20601e51764aSArtem Bityutskiy 	.put_super     = ubifs_put_super,
20611e51764aSArtem Bityutskiy 	.write_inode   = ubifs_write_inode,
206262de2592SEric Biggers 	.drop_inode    = ubifs_drop_inode,
2063d640e1b5SAl Viro 	.evict_inode   = ubifs_evict_inode,
20641e51764aSArtem Bityutskiy 	.statfs        = ubifs_statfs,
20651e51764aSArtem Bityutskiy 	.dirty_inode   = ubifs_dirty_inode,
20661e51764aSArtem Bityutskiy 	.remount_fs    = ubifs_remount_fs,
20671e51764aSArtem Bityutskiy 	.show_options  = ubifs_show_options,
20681e51764aSArtem Bityutskiy 	.sync_fs       = ubifs_sync_fs,
20691e51764aSArtem Bityutskiy };
20701e51764aSArtem Bityutskiy 
20711e51764aSArtem Bityutskiy /**
20721e51764aSArtem Bityutskiy  * open_ubi - parse UBI device name string and open the UBI device.
20731e51764aSArtem Bityutskiy  * @name: UBI volume name
20741e51764aSArtem Bityutskiy  * @mode: UBI volume open mode
20751e51764aSArtem Bityutskiy  *
20769722324eSCorentin Chary  * The primary method of mounting UBIFS is by specifying the UBI volume
207707c32de4SZheng Yongjun  * character device node path. However, UBIFS may also be mounted without any
20789722324eSCorentin Chary  * character device node using one of the following methods:
20799722324eSCorentin Chary  *
20809722324eSCorentin Chary  * o ubiX_Y    - mount UBI device number X, volume Y;
20819722324eSCorentin Chary  * o ubiY      - mount UBI device number 0, volume Y;
20821e51764aSArtem Bityutskiy  * o ubiX:NAME - mount UBI device X, volume with name NAME;
20831e51764aSArtem Bityutskiy  * o ubi:NAME  - mount UBI device 0, volume with name NAME.
20841e51764aSArtem Bityutskiy  *
20851e51764aSArtem Bityutskiy  * Alternative '!' separator may be used instead of ':' (because some shells
20861e51764aSArtem Bityutskiy  * like busybox may interpret ':' as an NFS host name separator). This function
20879722324eSCorentin Chary  * returns UBI volume description object in case of success and a negative
20889722324eSCorentin Chary  * error code in case of failure.
20891e51764aSArtem Bityutskiy  */
open_ubi(const char * name,int mode)20901e51764aSArtem Bityutskiy static struct ubi_volume_desc *open_ubi(const char *name, int mode)
20911e51764aSArtem Bityutskiy {
20929722324eSCorentin Chary 	struct ubi_volume_desc *ubi;
20931e51764aSArtem Bityutskiy 	int dev, vol;
20941e51764aSArtem Bityutskiy 	char *endptr;
20951e51764aSArtem Bityutskiy 
209637f31b6cSRichard Weinberger 	if (!name || !*name)
209737f31b6cSRichard Weinberger 		return ERR_PTR(-EINVAL);
209837f31b6cSRichard Weinberger 
20999722324eSCorentin Chary 	/* First, try to open using the device node path method */
21009722324eSCorentin Chary 	ubi = ubi_open_volume_path(name, mode);
21019722324eSCorentin Chary 	if (!IS_ERR(ubi))
21029722324eSCorentin Chary 		return ubi;
21039722324eSCorentin Chary 
21049722324eSCorentin Chary 	/* Try the "nodev" method */
21051e51764aSArtem Bityutskiy 	if (name[0] != 'u' || name[1] != 'b' || name[2] != 'i')
21061e51764aSArtem Bityutskiy 		return ERR_PTR(-EINVAL);
21071e51764aSArtem Bityutskiy 
21081e51764aSArtem Bityutskiy 	/* ubi:NAME method */
21091e51764aSArtem Bityutskiy 	if ((name[3] == ':' || name[3] == '!') && name[4] != '\0')
21101e51764aSArtem Bityutskiy 		return ubi_open_volume_nm(0, name + 4, mode);
21111e51764aSArtem Bityutskiy 
21121e51764aSArtem Bityutskiy 	if (!isdigit(name[3]))
21131e51764aSArtem Bityutskiy 		return ERR_PTR(-EINVAL);
21141e51764aSArtem Bityutskiy 
21151e51764aSArtem Bityutskiy 	dev = simple_strtoul(name + 3, &endptr, 0);
21161e51764aSArtem Bityutskiy 
21171e51764aSArtem Bityutskiy 	/* ubiY method */
21181e51764aSArtem Bityutskiy 	if (*endptr == '\0')
21191e51764aSArtem Bityutskiy 		return ubi_open_volume(0, dev, mode);
21201e51764aSArtem Bityutskiy 
21211e51764aSArtem Bityutskiy 	/* ubiX_Y method */
21221e51764aSArtem Bityutskiy 	if (*endptr == '_' && isdigit(endptr[1])) {
21231e51764aSArtem Bityutskiy 		vol = simple_strtoul(endptr + 1, &endptr, 0);
21241e51764aSArtem Bityutskiy 		if (*endptr != '\0')
21251e51764aSArtem Bityutskiy 			return ERR_PTR(-EINVAL);
21261e51764aSArtem Bityutskiy 		return ubi_open_volume(dev, vol, mode);
21271e51764aSArtem Bityutskiy 	}
21281e51764aSArtem Bityutskiy 
21291e51764aSArtem Bityutskiy 	/* ubiX:NAME method */
21301e51764aSArtem Bityutskiy 	if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
21311e51764aSArtem Bityutskiy 		return ubi_open_volume_nm(dev, ++endptr, mode);
21321e51764aSArtem Bityutskiy 
21331e51764aSArtem Bityutskiy 	return ERR_PTR(-EINVAL);
21341e51764aSArtem Bityutskiy }
21351e51764aSArtem Bityutskiy 
alloc_ubifs_info(struct ubi_volume_desc * ubi)2136b1c27ab3SAl Viro static struct ubifs_info *alloc_ubifs_info(struct ubi_volume_desc *ubi)
21371e51764aSArtem Bityutskiy {
21381e51764aSArtem Bityutskiy 	struct ubifs_info *c;
21391e51764aSArtem Bityutskiy 
21401e51764aSArtem Bityutskiy 	c = kzalloc(sizeof(struct ubifs_info), GFP_KERNEL);
2141b1c27ab3SAl Viro 	if (c) {
21421e51764aSArtem Bityutskiy 		spin_lock_init(&c->cnt_lock);
21431e51764aSArtem Bityutskiy 		spin_lock_init(&c->cs_lock);
21441e51764aSArtem Bityutskiy 		spin_lock_init(&c->buds_lock);
21451e51764aSArtem Bityutskiy 		spin_lock_init(&c->space_lock);
21461e51764aSArtem Bityutskiy 		spin_lock_init(&c->orphan_lock);
21471e51764aSArtem Bityutskiy 		init_rwsem(&c->commit_sem);
21481e51764aSArtem Bityutskiy 		mutex_init(&c->lp_mutex);
21491e51764aSArtem Bityutskiy 		mutex_init(&c->tnc_mutex);
21501e51764aSArtem Bityutskiy 		mutex_init(&c->log_mutex);
21511e51764aSArtem Bityutskiy 		mutex_init(&c->umount_mutex);
21523477d204SArtem Bityutskiy 		mutex_init(&c->bu_mutex);
2153d882962fSMatthew L. Creech 		mutex_init(&c->write_reserve_mutex);
21541e51764aSArtem Bityutskiy 		init_waitqueue_head(&c->cmt_wq);
21551e51764aSArtem Bityutskiy 		c->buds = RB_ROOT;
21561e51764aSArtem Bityutskiy 		c->old_idx = RB_ROOT;
21571e51764aSArtem Bityutskiy 		c->size_tree = RB_ROOT;
21581e51764aSArtem Bityutskiy 		c->orph_tree = RB_ROOT;
21591e51764aSArtem Bityutskiy 		INIT_LIST_HEAD(&c->infos_list);
21601e51764aSArtem Bityutskiy 		INIT_LIST_HEAD(&c->idx_gc);
21611e51764aSArtem Bityutskiy 		INIT_LIST_HEAD(&c->replay_list);
21621e51764aSArtem Bityutskiy 		INIT_LIST_HEAD(&c->replay_buds);
21631e51764aSArtem Bityutskiy 		INIT_LIST_HEAD(&c->uncat_list);
21641e51764aSArtem Bityutskiy 		INIT_LIST_HEAD(&c->empty_list);
21651e51764aSArtem Bityutskiy 		INIT_LIST_HEAD(&c->freeable_list);
21661e51764aSArtem Bityutskiy 		INIT_LIST_HEAD(&c->frdi_idx_list);
21671e51764aSArtem Bityutskiy 		INIT_LIST_HEAD(&c->unclean_leb_list);
21681e51764aSArtem Bityutskiy 		INIT_LIST_HEAD(&c->old_buds);
21691e51764aSArtem Bityutskiy 		INIT_LIST_HEAD(&c->orph_list);
21701e51764aSArtem Bityutskiy 		INIT_LIST_HEAD(&c->orph_new);
21712bcf0021SArtem Bityutskiy 		c->no_chk_data_crc = 1;
217299a24e02SRichard Weinberger 		c->assert_action = ASSACT_RO;
21731e51764aSArtem Bityutskiy 
21741e51764aSArtem Bityutskiy 		c->highest_inum = UBIFS_FIRST_INO;
21751e51764aSArtem Bityutskiy 		c->lhead_lnum = c->ltail_lnum = UBIFS_LOG_LNUM;
21761e51764aSArtem Bityutskiy 
21771e51764aSArtem Bityutskiy 		ubi_get_volume_info(ubi, &c->vi);
21781e51764aSArtem Bityutskiy 		ubi_get_device_info(c->vi.ubi_num, &c->di);
2179b1c27ab3SAl Viro 	}
2180b1c27ab3SAl Viro 	return c;
2181b1c27ab3SAl Viro }
21821e51764aSArtem Bityutskiy 
ubifs_fill_super(struct super_block * sb,void * data,int silent)2183b1c27ab3SAl Viro static int ubifs_fill_super(struct super_block *sb, void *data, int silent)
2184b1c27ab3SAl Viro {
2185d251ed27SAl Viro 	struct ubifs_info *c = sb->s_fs_info;
2186b1c27ab3SAl Viro 	struct inode *root;
2187b1c27ab3SAl Viro 	int err;
2188b1c27ab3SAl Viro 
2189b1c27ab3SAl Viro 	c->vfs_sb = sb;
21901e51764aSArtem Bityutskiy 	/* Re-open the UBI device in read-write mode */
21911e51764aSArtem Bityutskiy 	c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READWRITE);
21921e51764aSArtem Bityutskiy 	if (IS_ERR(c->ubi)) {
21931e51764aSArtem Bityutskiy 		err = PTR_ERR(c->ubi);
2194d251ed27SAl Viro 		goto out;
21951e51764aSArtem Bityutskiy 	}
21961e51764aSArtem Bityutskiy 
219799edd458SJan Kara 	err = ubifs_parse_options(c, data, 0);
219899edd458SJan Kara 	if (err)
219999edd458SJan Kara 		goto out_close;
220099edd458SJan Kara 
22011e51764aSArtem Bityutskiy 	/*
22020a883a05SArtem Bityutskiy 	 * UBIFS provides 'backing_dev_info' in order to disable read-ahead. For
22030b7bf483SMatthew Wilcox (Oracle) 	 * UBIFS, I/O is not deferred, it is done immediately in read_folio,
22041e51764aSArtem Bityutskiy 	 * which means the user would have to wait not just for their own I/O
22050a883a05SArtem Bityutskiy 	 * but the read-ahead I/O as well i.e. completely pointless.
22061e51764aSArtem Bityutskiy 	 *
220799edd458SJan Kara 	 * Read-ahead will be disabled because @sb->s_bdi->ra_pages is 0. Also
220899edd458SJan Kara 	 * @sb->s_bdi->capabilities are initialized to 0 so there won't be any
220999edd458SJan Kara 	 * writeback happening.
22101e51764aSArtem Bityutskiy 	 */
221199edd458SJan Kara 	err = super_setup_bdi_name(sb, "ubifs_%d_%d", c->vi.ubi_num,
221299edd458SJan Kara 				   c->vi.vol_id);
22131e51764aSArtem Bityutskiy 	if (err)
22141e51764aSArtem Bityutskiy 		goto out_close;
221555b2598eSChristoph Hellwig 	sb->s_bdi->ra_pages = 0;
221655b2598eSChristoph Hellwig 	sb->s_bdi->io_pages = 0;
22171e51764aSArtem Bityutskiy 
22181e51764aSArtem Bityutskiy 	sb->s_fs_info = c;
22191e51764aSArtem Bityutskiy 	sb->s_magic = UBIFS_SUPER_MAGIC;
22201e51764aSArtem Bityutskiy 	sb->s_blocksize = UBIFS_BLOCK_SIZE;
22211e51764aSArtem Bityutskiy 	sb->s_blocksize_bits = UBIFS_BLOCK_SHIFT;
22221e51764aSArtem Bityutskiy 	sb->s_maxbytes = c->max_inode_sz = key_max_inode_size(c);
22231e51764aSArtem Bityutskiy 	if (c->max_inode_sz > MAX_LFS_FILESIZE)
22241e51764aSArtem Bityutskiy 		sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE;
22251e51764aSArtem Bityutskiy 	sb->s_op = &ubifs_super_operations;
22262b88fc21SAndreas Gruenbacher 	sb->s_xattr = ubifs_xattr_handlers;
2227eea2c05dSSascha Hauer 	fscrypt_set_ops(sb, &ubifs_crypt_operations);
22281e51764aSArtem Bityutskiy 
22291e51764aSArtem Bityutskiy 	mutex_lock(&c->umount_mutex);
22301e51764aSArtem Bityutskiy 	err = mount_ubifs(c);
22311e51764aSArtem Bityutskiy 	if (err) {
22326eb61d58SRichard Weinberger 		ubifs_assert(c, err < 0);
22331e51764aSArtem Bityutskiy 		goto out_unlock;
22341e51764aSArtem Bityutskiy 	}
22351e51764aSArtem Bityutskiy 
22361e51764aSArtem Bityutskiy 	/* Read the root inode */
22371e51764aSArtem Bityutskiy 	root = ubifs_iget(sb, UBIFS_ROOT_INO);
22381e51764aSArtem Bityutskiy 	if (IS_ERR(root)) {
22391e51764aSArtem Bityutskiy 		err = PTR_ERR(root);
22401e51764aSArtem Bityutskiy 		goto out_umount;
22411e51764aSArtem Bityutskiy 	}
22421e51764aSArtem Bityutskiy 
224348fde701SAl Viro 	sb->s_root = d_make_root(root);
22447203db97SWei Yongjun 	if (!sb->s_root) {
22457203db97SWei Yongjun 		err = -ENOMEM;
224648fde701SAl Viro 		goto out_umount;
22477203db97SWei Yongjun 	}
22481e51764aSArtem Bityutskiy 
2249af61e7bfSSteffen Trumtrar 	import_uuid(&sb->s_uuid, c->uuid);
2250af61e7bfSSteffen Trumtrar 
22511e51764aSArtem Bityutskiy 	mutex_unlock(&c->umount_mutex);
22521e51764aSArtem Bityutskiy 	return 0;
22531e51764aSArtem Bityutskiy 
22541e51764aSArtem Bityutskiy out_umount:
22551e51764aSArtem Bityutskiy 	ubifs_umount(c);
22561e51764aSArtem Bityutskiy out_unlock:
22571e51764aSArtem Bityutskiy 	mutex_unlock(&c->umount_mutex);
22581e51764aSArtem Bityutskiy out_close:
225947f6d9ceSZhihao Cheng 	ubifs_release_options(c);
22601e51764aSArtem Bityutskiy 	ubi_close_volume(c->ubi);
2261d251ed27SAl Viro out:
22621e51764aSArtem Bityutskiy 	return err;
22631e51764aSArtem Bityutskiy }
22641e51764aSArtem Bityutskiy 
sb_test(struct super_block * sb,void * data)22651e51764aSArtem Bityutskiy static int sb_test(struct super_block *sb, void *data)
22661e51764aSArtem Bityutskiy {
2267d251ed27SAl Viro 	struct ubifs_info *c1 = data;
22687c83f5cbSArtem Bityutskiy 	struct ubifs_info *c = sb->s_fs_info;
22691e51764aSArtem Bityutskiy 
2270d251ed27SAl Viro 	return c->vi.cdev == c1->vi.cdev;
2271d251ed27SAl Viro }
2272d251ed27SAl Viro 
sb_set(struct super_block * sb,void * data)2273d251ed27SAl Viro static int sb_set(struct super_block *sb, void *data)
2274d251ed27SAl Viro {
2275d251ed27SAl Viro 	sb->s_fs_info = data;
2276d251ed27SAl Viro 	return set_anon_super(sb, NULL);
22771e51764aSArtem Bityutskiy }
22781e51764aSArtem Bityutskiy 
ubifs_mount(struct file_system_type * fs_type,int flags,const char * name,void * data)2279157d81e7SAl Viro static struct dentry *ubifs_mount(struct file_system_type *fs_type, int flags,
2280157d81e7SAl Viro 			const char *name, void *data)
22811e51764aSArtem Bityutskiy {
22821e51764aSArtem Bityutskiy 	struct ubi_volume_desc *ubi;
2283d251ed27SAl Viro 	struct ubifs_info *c;
22841e51764aSArtem Bityutskiy 	struct super_block *sb;
22851e51764aSArtem Bityutskiy 	int err;
22861e51764aSArtem Bityutskiy 
22871e51764aSArtem Bityutskiy 	dbg_gen("name %s, flags %#x", name, flags);
22881e51764aSArtem Bityutskiy 
22891e51764aSArtem Bityutskiy 	/*
22901e51764aSArtem Bityutskiy 	 * Get UBI device number and volume ID. Mount it read-only so far
22911e51764aSArtem Bityutskiy 	 * because this might be a new mount point, and UBI allows only one
22921e51764aSArtem Bityutskiy 	 * read-write user at a time.
22931e51764aSArtem Bityutskiy 	 */
22941e51764aSArtem Bityutskiy 	ubi = open_ubi(name, UBI_READONLY);
22951e51764aSArtem Bityutskiy 	if (IS_ERR(ubi)) {
22961751e8a6SLinus Torvalds 		if (!(flags & SB_SILENT))
2297235c362bSSheng Yong 			pr_err("UBIFS error (pid: %d): cannot open \"%s\", error %d",
2298235c362bSSheng Yong 			       current->pid, name, (int)PTR_ERR(ubi));
2299157d81e7SAl Viro 		return ERR_CAST(ubi);
23001e51764aSArtem Bityutskiy 	}
23011e51764aSArtem Bityutskiy 
2302d251ed27SAl Viro 	c = alloc_ubifs_info(ubi);
2303d251ed27SAl Viro 	if (!c) {
2304d251ed27SAl Viro 		err = -ENOMEM;
2305d251ed27SAl Viro 		goto out_close;
2306d251ed27SAl Viro 	}
23071e51764aSArtem Bityutskiy 
2308d251ed27SAl Viro 	dbg_gen("opened ubi%d_%d", c->vi.ubi_num, c->vi.vol_id);
2309d251ed27SAl Viro 
23109249e17fSDavid Howells 	sb = sget(fs_type, sb_test, sb_set, flags, c);
23111e51764aSArtem Bityutskiy 	if (IS_ERR(sb)) {
23121e51764aSArtem Bityutskiy 		err = PTR_ERR(sb);
2313d251ed27SAl Viro 		kfree(c);
2314185bf873SDan Carpenter 		goto out_close;
23151e51764aSArtem Bityutskiy 	}
23161e51764aSArtem Bityutskiy 
23171e51764aSArtem Bityutskiy 	if (sb->s_root) {
23182ef13294SArtem Bityutskiy 		struct ubifs_info *c1 = sb->s_fs_info;
2319d251ed27SAl Viro 		kfree(c);
23201e51764aSArtem Bityutskiy 		/* A new mount point for already mounted UBIFS */
23211e51764aSArtem Bityutskiy 		dbg_gen("this ubi volume is already mounted");
23221751e8a6SLinus Torvalds 		if (!!(flags & SB_RDONLY) != c1->ro_mount) {
23231e51764aSArtem Bityutskiy 			err = -EBUSY;
23241e51764aSArtem Bityutskiy 			goto out_deact;
23251e51764aSArtem Bityutskiy 		}
23261e51764aSArtem Bityutskiy 	} else {
23271751e8a6SLinus Torvalds 		err = ubifs_fill_super(sb, data, flags & SB_SILENT ? 1 : 0);
232891cbf011SRichard Weinberger 		if (err)
23291e51764aSArtem Bityutskiy 			goto out_deact;
23301e51764aSArtem Bityutskiy 		/* We do not support atime */
23311751e8a6SLinus Torvalds 		sb->s_flags |= SB_ACTIVE;
2332e3d73deaSSascha Hauer 		if (IS_ENABLED(CONFIG_UBIFS_ATIME_SUPPORT))
23338c1c5f26SDongsheng Yang 			ubifs_msg(c, "full atime support is enabled.");
2334e3d73deaSSascha Hauer 		else
2335e3d73deaSSascha Hauer 			sb->s_flags |= SB_NOATIME;
23361e51764aSArtem Bityutskiy 	}
23371e51764aSArtem Bityutskiy 
23381e51764aSArtem Bityutskiy 	/* 'fill_super()' opens ubi again so we must close it here */
23391e51764aSArtem Bityutskiy 	ubi_close_volume(ubi);
23401e51764aSArtem Bityutskiy 
2341157d81e7SAl Viro 	return dget(sb->s_root);
23421e51764aSArtem Bityutskiy 
23431e51764aSArtem Bityutskiy out_deact:
23446f5bbff9SAl Viro 	deactivate_locked_super(sb);
23451e51764aSArtem Bityutskiy out_close:
23461e51764aSArtem Bityutskiy 	ubi_close_volume(ubi);
2347157d81e7SAl Viro 	return ERR_PTR(err);
23481e51764aSArtem Bityutskiy }
23491e51764aSArtem Bityutskiy 
kill_ubifs_super(struct super_block * s)2350d251ed27SAl Viro static void kill_ubifs_super(struct super_block *s)
2351d251ed27SAl Viro {
2352d251ed27SAl Viro 	struct ubifs_info *c = s->s_fs_info;
2353d251ed27SAl Viro 	kill_anon_super(s);
2354d251ed27SAl Viro 	kfree(c);
2355d251ed27SAl Viro }
2356d251ed27SAl Viro 
23571e51764aSArtem Bityutskiy static struct file_system_type ubifs_fs_type = {
23581e51764aSArtem Bityutskiy 	.name    = "ubifs",
23591e51764aSArtem Bityutskiy 	.owner   = THIS_MODULE,
2360157d81e7SAl Viro 	.mount   = ubifs_mount,
2361d251ed27SAl Viro 	.kill_sb = kill_ubifs_super,
23621e51764aSArtem Bityutskiy };
23637f78e035SEric W. Biederman MODULE_ALIAS_FS("ubifs");
23641e51764aSArtem Bityutskiy 
23651e51764aSArtem Bityutskiy /*
23661e51764aSArtem Bityutskiy  * Inode slab cache constructor.
23671e51764aSArtem Bityutskiy  */
inode_slab_ctor(void * obj)236851cc5068SAlexey Dobriyan static void inode_slab_ctor(void *obj)
23691e51764aSArtem Bityutskiy {
23701e51764aSArtem Bityutskiy 	struct ubifs_inode *ui = obj;
23711e51764aSArtem Bityutskiy 	inode_init_once(&ui->vfs_inode);
23721e51764aSArtem Bityutskiy }
23731e51764aSArtem Bityutskiy 
ubifs_init(void)23741e51764aSArtem Bityutskiy static int __init ubifs_init(void)
23751e51764aSArtem Bityutskiy {
23761e51764aSArtem Bityutskiy 	int err;
23771e51764aSArtem Bityutskiy 
23781e51764aSArtem Bityutskiy 	BUILD_BUG_ON(sizeof(struct ubifs_ch) != 24);
23791e51764aSArtem Bityutskiy 
23801e51764aSArtem Bityutskiy 	/* Make sure node sizes are 8-byte aligned */
23811e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_CH_SZ        & 7);
23821e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_INO_NODE_SZ  & 7);
23831e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_DENT_NODE_SZ & 7);
23841e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_XENT_NODE_SZ & 7);
23851e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_DATA_NODE_SZ & 7);
23861e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ & 7);
23871e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_SB_NODE_SZ   & 7);
23881e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_MST_NODE_SZ  & 7);
23891e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_REF_NODE_SZ  & 7);
23901e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_CS_NODE_SZ   & 7);
23911e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_ORPH_NODE_SZ & 7);
23921e51764aSArtem Bityutskiy 
23931e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ & 7);
23941e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ & 7);
23951e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ & 7);
23961e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ  & 7);
23971e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_MAX_NODE_SZ      & 7);
23981e51764aSArtem Bityutskiy 	BUILD_BUG_ON(MIN_WRITE_SZ           & 7);
23991e51764aSArtem Bityutskiy 
24001e51764aSArtem Bityutskiy 	/* Check min. node size */
24011e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_INO_NODE_SZ  < MIN_WRITE_SZ);
24021e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_DENT_NODE_SZ < MIN_WRITE_SZ);
24031e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_XENT_NODE_SZ < MIN_WRITE_SZ);
24041e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_TRUN_NODE_SZ < MIN_WRITE_SZ);
24051e51764aSArtem Bityutskiy 
24061e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_MAX_DENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
24071e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_MAX_XENT_NODE_SZ > UBIFS_MAX_NODE_SZ);
24081e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_MAX_DATA_NODE_SZ > UBIFS_MAX_NODE_SZ);
24091e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_MAX_INO_NODE_SZ  > UBIFS_MAX_NODE_SZ);
24101e51764aSArtem Bityutskiy 
24111e51764aSArtem Bityutskiy 	/* Defined node sizes */
24121e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_SB_NODE_SZ  != 4096);
24131e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_MST_NODE_SZ != 512);
24141e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_INO_NODE_SZ != 160);
24151e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_REF_NODE_SZ != 64);
24161e51764aSArtem Bityutskiy 
24171e51764aSArtem Bityutskiy 	/*
2418a1dc080cSArtem Bityutskiy 	 * We use 2 bit wide bit-fields to store compression type, which should
2419a1dc080cSArtem Bityutskiy 	 * be amended if more compressors are added. The bit-fields are:
2420553dea4dSArtem Bityutskiy 	 * @compr_type in 'struct ubifs_inode', @default_compr in
2421553dea4dSArtem Bityutskiy 	 * 'struct ubifs_info' and @compr_type in 'struct ubifs_mount_opts'.
2422a1dc080cSArtem Bityutskiy 	 */
2423a1dc080cSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_COMPR_TYPES_CNT > 4);
2424a1dc080cSArtem Bityutskiy 
2425a1dc080cSArtem Bityutskiy 	/*
2426ea1754a0SKirill A. Shutemov 	 * We require that PAGE_SIZE is greater-than-or-equal-to
24271e51764aSArtem Bityutskiy 	 * UBIFS_BLOCK_SIZE. It is assumed that both are powers of 2.
24281e51764aSArtem Bityutskiy 	 */
242909cbfeafSKirill A. Shutemov 	if (PAGE_SIZE < UBIFS_BLOCK_SIZE) {
2430235c362bSSheng Yong 		pr_err("UBIFS error (pid %d): VFS page cache size is %u bytes, but UBIFS requires at least 4096 bytes",
243109cbfeafSKirill A. Shutemov 		       current->pid, (unsigned int)PAGE_SIZE);
24321e51764aSArtem Bityutskiy 		return -EINVAL;
24331e51764aSArtem Bityutskiy 	}
24341e51764aSArtem Bityutskiy 
24351e51764aSArtem Bityutskiy 	ubifs_inode_slab = kmem_cache_create("ubifs_inode_slab",
24361e51764aSArtem Bityutskiy 				sizeof(struct ubifs_inode), 0,
24375d097056SVladimir Davydov 				SLAB_MEM_SPREAD | SLAB_RECLAIM_ACCOUNT |
24385d097056SVladimir Davydov 				SLAB_ACCOUNT, &inode_slab_ctor);
24391e51764aSArtem Bityutskiy 	if (!ubifs_inode_slab)
24405cc361e3SAl Viro 		return -ENOMEM;
24411e51764aSArtem Bityutskiy 
2442e33c267aSRoman Gushchin 	err = register_shrinker(&ubifs_shrinker_info, "ubifs-slab");
2443a1fe33afSChao Yu 	if (err)
2444a1fe33afSChao Yu 		goto out_slab;
24451e51764aSArtem Bityutskiy 
24461e51764aSArtem Bityutskiy 	err = ubifs_compressors_init();
24471e51764aSArtem Bityutskiy 	if (err)
2448552ff317SArtem Bityutskiy 		goto out_shrinker;
2449552ff317SArtem Bityutskiy 
2450702d6a83SGreg Kroah-Hartman 	dbg_debugfs_init();
24511e51764aSArtem Bityutskiy 
24522e3cbf42SStefan Schaeckeler 	err = ubifs_sysfs_init();
24532e3cbf42SStefan Schaeckeler 	if (err)
24542e3cbf42SStefan Schaeckeler 		goto out_dbg;
24552e3cbf42SStefan Schaeckeler 
24565cc361e3SAl Viro 	err = register_filesystem(&ubifs_fs_type);
24575cc361e3SAl Viro 	if (err) {
2458235c362bSSheng Yong 		pr_err("UBIFS error (pid %d): cannot register file system, error %d",
2459235c362bSSheng Yong 		       current->pid, err);
24602e3cbf42SStefan Schaeckeler 		goto out_sysfs;
24615cc361e3SAl Viro 	}
24621e51764aSArtem Bityutskiy 	return 0;
24631e51764aSArtem Bityutskiy 
24642e3cbf42SStefan Schaeckeler out_sysfs:
24652e3cbf42SStefan Schaeckeler 	ubifs_sysfs_exit();
24665cc361e3SAl Viro out_dbg:
24675cc361e3SAl Viro 	dbg_debugfs_exit();
2468552ff317SArtem Bityutskiy 	ubifs_compressors_exit();
2469552ff317SArtem Bityutskiy out_shrinker:
24701e51764aSArtem Bityutskiy 	unregister_shrinker(&ubifs_shrinker_info);
2471a1fe33afSChao Yu out_slab:
24721e51764aSArtem Bityutskiy 	kmem_cache_destroy(ubifs_inode_slab);
24731e51764aSArtem Bityutskiy 	return err;
24741e51764aSArtem Bityutskiy }
24751e51764aSArtem Bityutskiy /* late_initcall to let compressors initialize first */
24761e51764aSArtem Bityutskiy late_initcall(ubifs_init);
24771e51764aSArtem Bityutskiy 
ubifs_exit(void)24781e51764aSArtem Bityutskiy static void __exit ubifs_exit(void)
24791e51764aSArtem Bityutskiy {
2480f8ccb14fSRichard Weinberger 	WARN_ON(!list_empty(&ubifs_infos));
2481f8ccb14fSRichard Weinberger 	WARN_ON(atomic_long_read(&ubifs_clean_zn_cnt) != 0);
24821e51764aSArtem Bityutskiy 
2483552ff317SArtem Bityutskiy 	dbg_debugfs_exit();
24842e3cbf42SStefan Schaeckeler 	ubifs_sysfs_exit();
24851e51764aSArtem Bityutskiy 	ubifs_compressors_exit();
24861e51764aSArtem Bityutskiy 	unregister_shrinker(&ubifs_shrinker_info);
24878c0a8537SKirill A. Shutemov 
24888c0a8537SKirill A. Shutemov 	/*
24898c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
24908c0a8537SKirill A. Shutemov 	 * destroy cache.
24918c0a8537SKirill A. Shutemov 	 */
24928c0a8537SKirill A. Shutemov 	rcu_barrier();
24931e51764aSArtem Bityutskiy 	kmem_cache_destroy(ubifs_inode_slab);
24941e51764aSArtem Bityutskiy 	unregister_filesystem(&ubifs_fs_type);
24951e51764aSArtem Bityutskiy }
24961e51764aSArtem Bityutskiy module_exit(ubifs_exit);
24971e51764aSArtem Bityutskiy 
24981e51764aSArtem Bityutskiy MODULE_LICENSE("GPL");
24991e51764aSArtem Bityutskiy MODULE_VERSION(__stringify(UBIFS_VERSION));
25001e51764aSArtem Bityutskiy MODULE_AUTHOR("Artem Bityutskiy, Adrian Hunter");
25011e51764aSArtem Bityutskiy MODULE_DESCRIPTION("UBIFS - UBI File System");
2502