xref: /openbmc/linux/fs/ubifs/ubifs.h (revision 481a9b80)
11e51764aSArtem Bityutskiy /*
21e51764aSArtem Bityutskiy  * This file is part of UBIFS.
31e51764aSArtem Bityutskiy  *
41e51764aSArtem Bityutskiy  * Copyright (C) 2006-2008 Nokia Corporation
51e51764aSArtem Bityutskiy  *
61e51764aSArtem Bityutskiy  * This program is free software; you can redistribute it and/or modify it
71e51764aSArtem Bityutskiy  * under the terms of the GNU General Public License version 2 as published by
81e51764aSArtem Bityutskiy  * the Free Software Foundation.
91e51764aSArtem Bityutskiy  *
101e51764aSArtem Bityutskiy  * This program is distributed in the hope that it will be useful, but WITHOUT
111e51764aSArtem Bityutskiy  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
121e51764aSArtem Bityutskiy  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
131e51764aSArtem Bityutskiy  * more details.
141e51764aSArtem Bityutskiy  *
151e51764aSArtem Bityutskiy  * You should have received a copy of the GNU General Public License along with
161e51764aSArtem Bityutskiy  * this program; if not, write to the Free Software Foundation, Inc., 51
171e51764aSArtem Bityutskiy  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
181e51764aSArtem Bityutskiy  *
191e51764aSArtem Bityutskiy  * Authors: Artem Bityutskiy (Битюцкий Артём)
201e51764aSArtem Bityutskiy  *          Adrian Hunter
211e51764aSArtem Bityutskiy  */
221e51764aSArtem Bityutskiy 
231e51764aSArtem Bityutskiy #ifndef __UBIFS_H__
241e51764aSArtem Bityutskiy #define __UBIFS_H__
251e51764aSArtem Bityutskiy 
261e51764aSArtem Bityutskiy #include <asm/div64.h>
271e51764aSArtem Bityutskiy #include <linux/statfs.h>
281e51764aSArtem Bityutskiy #include <linux/fs.h>
291e51764aSArtem Bityutskiy #include <linux/err.h>
301e51764aSArtem Bityutskiy #include <linux/sched.h>
315a0e3ad6STejun Heo #include <linux/slab.h>
321e51764aSArtem Bityutskiy #include <linux/vmalloc.h>
331e51764aSArtem Bityutskiy #include <linux/spinlock.h>
341e51764aSArtem Bityutskiy #include <linux/mutex.h>
351e51764aSArtem Bityutskiy #include <linux/rwsem.h>
361e51764aSArtem Bityutskiy #include <linux/mtd/ubi.h>
371e51764aSArtem Bityutskiy #include <linux/pagemap.h>
381e51764aSArtem Bityutskiy #include <linux/backing-dev.h>
39d7f0b70dSSubodh Nijsure #include <linux/security.h>
402b88fc21SAndreas Gruenbacher #include <linux/xattr.h>
41cc41a536SRichard Weinberger #include <linux/random.h>
4249525e5eSSascha Hauer #include <crypto/hash_info.h>
4349525e5eSSascha Hauer #include <crypto/hash.h>
4449525e5eSSascha Hauer #include <crypto/algapi.h>
45734f0d24SDave Chinner 
46734f0d24SDave Chinner #include <linux/fscrypt.h>
47734f0d24SDave Chinner 
481e51764aSArtem Bityutskiy #include "ubifs-media.h"
491e51764aSArtem Bityutskiy 
501e51764aSArtem Bityutskiy /* Version of this UBIFS implementation */
511e51764aSArtem Bityutskiy #define UBIFS_VERSION 1
521e51764aSArtem Bityutskiy 
531e51764aSArtem Bityutskiy /* UBIFS file system VFS magic number */
541e51764aSArtem Bityutskiy #define UBIFS_SUPER_MAGIC 0x24051905
551e51764aSArtem Bityutskiy 
561e51764aSArtem Bityutskiy /* Number of UBIFS blocks per VFS page */
5709cbfeafSKirill A. Shutemov #define UBIFS_BLOCKS_PER_PAGE (PAGE_SIZE / UBIFS_BLOCK_SIZE)
5809cbfeafSKirill A. Shutemov #define UBIFS_BLOCKS_PER_PAGE_SHIFT (PAGE_SHIFT - UBIFS_BLOCK_SHIFT)
591e51764aSArtem Bityutskiy 
601e51764aSArtem Bityutskiy /* "File system end of life" sequence number watermark */
611e51764aSArtem Bityutskiy #define SQNUM_WARN_WATERMARK 0xFFFFFFFF00000000ULL
621e51764aSArtem Bityutskiy #define SQNUM_WATERMARK      0xFFFFFFFFFF000000ULL
631e51764aSArtem Bityutskiy 
64af14a1adSArtem Bityutskiy /*
65af14a1adSArtem Bityutskiy  * Minimum amount of LEBs reserved for the index. At present the index needs at
66af14a1adSArtem Bityutskiy  * least 2 LEBs: one for the index head and one for in-the-gaps method (which
67af14a1adSArtem Bityutskiy  * currently does not cater for the index head and so excludes it from
68af14a1adSArtem Bityutskiy  * consideration).
69af14a1adSArtem Bityutskiy  */
70af14a1adSArtem Bityutskiy #define MIN_INDEX_LEBS 2
71af14a1adSArtem Bityutskiy 
721e51764aSArtem Bityutskiy /* Minimum amount of data UBIFS writes to the flash */
731e51764aSArtem Bityutskiy #define MIN_WRITE_SZ (UBIFS_DATA_NODE_SZ + 8)
741e51764aSArtem Bityutskiy 
751e51764aSArtem Bityutskiy /*
761e51764aSArtem Bityutskiy  * Currently we do not support inode number overlapping and re-using, so this
771e51764aSArtem Bityutskiy  * watermark defines dangerous inode number level. This should be fixed later,
781e51764aSArtem Bityutskiy  * although it is difficult to exceed current limit. Another option is to use
791e51764aSArtem Bityutskiy  * 64-bit inode numbers, but this means more overhead.
801e51764aSArtem Bityutskiy  */
811e51764aSArtem Bityutskiy #define INUM_WARN_WATERMARK 0xFFF00000
821e51764aSArtem Bityutskiy #define INUM_WATERMARK      0xFFFFFF00
831e51764aSArtem Bityutskiy 
841e51764aSArtem Bityutskiy /* Maximum number of entries in each LPT (LEB category) heap */
851e51764aSArtem Bityutskiy #define LPT_HEAP_SZ 256
861e51764aSArtem Bityutskiy 
871e51764aSArtem Bityutskiy /*
881e51764aSArtem Bityutskiy  * Background thread name pattern. The numbers are UBI device and volume
891e51764aSArtem Bityutskiy  * numbers.
901e51764aSArtem Bityutskiy  */
911e51764aSArtem Bityutskiy #define BGT_NAME_PATTERN "ubifs_bgt%d_%d"
921e51764aSArtem Bityutskiy 
931e51764aSArtem Bityutskiy /* Maximum possible inode number (only 32-bit inodes are supported now) */
941e51764aSArtem Bityutskiy #define MAX_INUM 0xFFFFFFFF
951e51764aSArtem Bityutskiy 
961e51764aSArtem Bityutskiy /* Number of non-data journal heads */
971e51764aSArtem Bityutskiy #define NONDATA_JHEADS_CNT 2
981e51764aSArtem Bityutskiy 
99d6d14009SArtem Bityutskiy /* Shorter names for journal head numbers for internal usage */
100d6d14009SArtem Bityutskiy #define GCHD   UBIFS_GC_HEAD
101d6d14009SArtem Bityutskiy #define BASEHD UBIFS_BASE_HEAD
102d6d14009SArtem Bityutskiy #define DATAHD UBIFS_DATA_HEAD
1031e51764aSArtem Bityutskiy 
1041e51764aSArtem Bityutskiy /* 'No change' value for 'ubifs_change_lp()' */
1051e51764aSArtem Bityutskiy #define LPROPS_NC 0x80000001
1061e51764aSArtem Bityutskiy 
1071e51764aSArtem Bityutskiy /*
1081e51764aSArtem Bityutskiy  * There is no notion of truncation key because truncation nodes do not exist
1091e51764aSArtem Bityutskiy  * in TNC. However, when replaying, it is handy to introduce fake "truncation"
1101e51764aSArtem Bityutskiy  * keys for truncation nodes because the code becomes simpler. So we define
1111e51764aSArtem Bityutskiy  * %UBIFS_TRUN_KEY type.
112ba2f48f7SArtem Bityutskiy  *
113ba2f48f7SArtem Bityutskiy  * But otherwise, out of the journal reply scope, the truncation keys are
114ba2f48f7SArtem Bityutskiy  * invalid.
1151e51764aSArtem Bityutskiy  */
1161e51764aSArtem Bityutskiy #define UBIFS_TRUN_KEY    UBIFS_KEY_TYPES_CNT
117ba2f48f7SArtem Bityutskiy #define UBIFS_INVALID_KEY UBIFS_KEY_TYPES_CNT
1181e51764aSArtem Bityutskiy 
1191e51764aSArtem Bityutskiy /*
1201e51764aSArtem Bityutskiy  * How much a directory entry/extended attribute entry adds to the parent/host
1211e51764aSArtem Bityutskiy  * inode.
1221e51764aSArtem Bityutskiy  */
1231e51764aSArtem Bityutskiy #define CALC_DENT_SIZE(name_len) ALIGN(UBIFS_DENT_NODE_SZ + (name_len) + 1, 8)
1241e51764aSArtem Bityutskiy 
1251e51764aSArtem Bityutskiy /* How much an extended attribute adds to the host inode */
1261e51764aSArtem Bityutskiy #define CALC_XATTR_BYTES(data_len) ALIGN(UBIFS_INO_NODE_SZ + (data_len) + 1, 8)
1271e51764aSArtem Bityutskiy 
1281e51764aSArtem Bityutskiy /*
1291e51764aSArtem Bityutskiy  * Znodes which were not touched for 'OLD_ZNODE_AGE' seconds are considered
1301e51764aSArtem Bityutskiy  * "old", and znode which were touched last 'YOUNG_ZNODE_AGE' seconds ago are
1311e51764aSArtem Bityutskiy  * considered "young". This is used by shrinker when selecting znode to trim
1321e51764aSArtem Bityutskiy  * off.
1331e51764aSArtem Bityutskiy  */
1341e51764aSArtem Bityutskiy #define OLD_ZNODE_AGE 20
1351e51764aSArtem Bityutskiy #define YOUNG_ZNODE_AGE 5
1361e51764aSArtem Bityutskiy 
1371e51764aSArtem Bityutskiy /*
1381e51764aSArtem Bityutskiy  * Some compressors, like LZO, may end up with more data then the input buffer.
1391e51764aSArtem Bityutskiy  * So UBIFS always allocates larger output buffer, to be sure the compressor
1401e51764aSArtem Bityutskiy  * will not corrupt memory in case of worst case compression.
1411e51764aSArtem Bityutskiy  */
1421e51764aSArtem Bityutskiy #define WORST_COMPR_FACTOR 2
1431e51764aSArtem Bityutskiy 
144643fa961SChandan Rajendra #ifdef CONFIG_FS_ENCRYPTION
1457799953bSRichard Weinberger #define UBIFS_CIPHER_BLOCK_SIZE FS_CRYPTO_BLOCK_SIZE
1467799953bSRichard Weinberger #else
1477799953bSRichard Weinberger #define UBIFS_CIPHER_BLOCK_SIZE 0
1487799953bSRichard Weinberger #endif
1497799953bSRichard Weinberger 
150d882962fSMatthew L. Creech /*
151d3f9db00SYannick Guerrini  * How much memory is needed for a buffer where we compress a data node.
152d882962fSMatthew L. Creech  */
153d882962fSMatthew L. Creech #define COMPRESSED_DATA_NODE_BUF_SZ \
154d882962fSMatthew L. Creech 	(UBIFS_DATA_NODE_SZ + UBIFS_BLOCK_SIZE * WORST_COMPR_FACTOR)
155d882962fSMatthew L. Creech 
1561e51764aSArtem Bityutskiy /* Maximum expected tree height for use by bottom_up_buf */
1571e51764aSArtem Bityutskiy #define BOTTOM_UP_HEIGHT 64
1581e51764aSArtem Bityutskiy 
1594793e7c5SAdrian Hunter /* Maximum number of data nodes to bulk-read */
1604793e7c5SAdrian Hunter #define UBIFS_MAX_BULK_READ 32
1614793e7c5SAdrian Hunter 
16249525e5eSSascha Hauer #ifdef CONFIG_UBIFS_FS_AUTHENTICATION
16349525e5eSSascha Hauer #define UBIFS_HASH_ARR_SZ UBIFS_MAX_HASH_LEN
16449525e5eSSascha Hauer #define UBIFS_HMAC_ARR_SZ UBIFS_MAX_HMAC_LEN
16549525e5eSSascha Hauer #else
16649525e5eSSascha Hauer #define UBIFS_HASH_ARR_SZ 0
16749525e5eSSascha Hauer #define UBIFS_HMAC_ARR_SZ 0
16849525e5eSSascha Hauer #endif
16949525e5eSSascha Hauer 
1701e51764aSArtem Bityutskiy /*
1711e51764aSArtem Bityutskiy  * Lockdep classes for UBIFS inode @ui_mutex.
1721e51764aSArtem Bityutskiy  */
1731e51764aSArtem Bityutskiy enum {
1741e51764aSArtem Bityutskiy 	WB_MUTEX_1 = 0,
1751e51764aSArtem Bityutskiy 	WB_MUTEX_2 = 1,
1761e51764aSArtem Bityutskiy 	WB_MUTEX_3 = 2,
1779e0a1fffSRichard Weinberger 	WB_MUTEX_4 = 3,
1781e51764aSArtem Bityutskiy };
1791e51764aSArtem Bityutskiy 
1801e51764aSArtem Bityutskiy /*
1811e51764aSArtem Bityutskiy  * Znode flags (actually, bit numbers which store the flags).
1821e51764aSArtem Bityutskiy  *
1831e51764aSArtem Bityutskiy  * DIRTY_ZNODE: znode is dirty
1841e51764aSArtem Bityutskiy  * COW_ZNODE: znode is being committed and a new instance of this znode has to
1851e51764aSArtem Bityutskiy  *            be created before changing this znode
1861e51764aSArtem Bityutskiy  * OBSOLETE_ZNODE: znode is obsolete, which means it was deleted, but it is
1871e51764aSArtem Bityutskiy  *                 still in the commit list and the ongoing commit operation
1881e51764aSArtem Bityutskiy  *                 will commit it, and delete this znode after it is done
1891e51764aSArtem Bityutskiy  */
1901e51764aSArtem Bityutskiy enum {
1911e51764aSArtem Bityutskiy 	DIRTY_ZNODE    = 0,
1921e51764aSArtem Bityutskiy 	COW_ZNODE      = 1,
1931e51764aSArtem Bityutskiy 	OBSOLETE_ZNODE = 2,
1941e51764aSArtem Bityutskiy };
1951e51764aSArtem Bityutskiy 
1961e51764aSArtem Bityutskiy /*
1971e51764aSArtem Bityutskiy  * Commit states.
1981e51764aSArtem Bityutskiy  *
1991e51764aSArtem Bityutskiy  * COMMIT_RESTING: commit is not wanted
2001e51764aSArtem Bityutskiy  * COMMIT_BACKGROUND: background commit has been requested
2011e51764aSArtem Bityutskiy  * COMMIT_REQUIRED: commit is required
2021e51764aSArtem Bityutskiy  * COMMIT_RUNNING_BACKGROUND: background commit is running
2031e51764aSArtem Bityutskiy  * COMMIT_RUNNING_REQUIRED: commit is running and it is required
2041e51764aSArtem Bityutskiy  * COMMIT_BROKEN: commit failed
2051e51764aSArtem Bityutskiy  */
2061e51764aSArtem Bityutskiy enum {
2071e51764aSArtem Bityutskiy 	COMMIT_RESTING = 0,
2081e51764aSArtem Bityutskiy 	COMMIT_BACKGROUND,
2091e51764aSArtem Bityutskiy 	COMMIT_REQUIRED,
2101e51764aSArtem Bityutskiy 	COMMIT_RUNNING_BACKGROUND,
2111e51764aSArtem Bityutskiy 	COMMIT_RUNNING_REQUIRED,
2121e51764aSArtem Bityutskiy 	COMMIT_BROKEN,
2131e51764aSArtem Bityutskiy };
2141e51764aSArtem Bityutskiy 
2151e51764aSArtem Bityutskiy /*
2161e51764aSArtem Bityutskiy  * 'ubifs_scan_a_node()' return values.
2171e51764aSArtem Bityutskiy  *
2181e51764aSArtem Bityutskiy  * SCANNED_GARBAGE:  scanned garbage
2191e51764aSArtem Bityutskiy  * SCANNED_EMPTY_SPACE: scanned empty space
2201e51764aSArtem Bityutskiy  * SCANNED_A_NODE: scanned a valid node
2211e51764aSArtem Bityutskiy  * SCANNED_A_CORRUPT_NODE: scanned a corrupted node
2221e51764aSArtem Bityutskiy  * SCANNED_A_BAD_PAD_NODE: scanned a padding node with invalid pad length
2231e51764aSArtem Bityutskiy  *
2241e51764aSArtem Bityutskiy  * Greater than zero means: 'scanned that number of padding bytes'
2251e51764aSArtem Bityutskiy  */
2261e51764aSArtem Bityutskiy enum {
2271e51764aSArtem Bityutskiy 	SCANNED_GARBAGE        = 0,
2281e51764aSArtem Bityutskiy 	SCANNED_EMPTY_SPACE    = -1,
2291e51764aSArtem Bityutskiy 	SCANNED_A_NODE         = -2,
2301e51764aSArtem Bityutskiy 	SCANNED_A_CORRUPT_NODE = -3,
2311e51764aSArtem Bityutskiy 	SCANNED_A_BAD_PAD_NODE = -4,
2321e51764aSArtem Bityutskiy };
2331e51764aSArtem Bityutskiy 
2341e51764aSArtem Bityutskiy /*
2351e51764aSArtem Bityutskiy  * LPT cnode flag bits.
2361e51764aSArtem Bityutskiy  *
2371e51764aSArtem Bityutskiy  * DIRTY_CNODE: cnode is dirty
2381e51764aSArtem Bityutskiy  * OBSOLETE_CNODE: cnode is being committed and has been copied (or deleted),
2391e51764aSArtem Bityutskiy  *                 so it can (and must) be freed when the commit is finished
24037662447SArtem Bityutskiy  * COW_CNODE: cnode is being committed and must be copied before writing
2411e51764aSArtem Bityutskiy  */
2421e51764aSArtem Bityutskiy enum {
2431e51764aSArtem Bityutskiy 	DIRTY_CNODE    = 0,
24437662447SArtem Bityutskiy 	OBSOLETE_CNODE = 1,
24537662447SArtem Bityutskiy 	COW_CNODE      = 2,
2461e51764aSArtem Bityutskiy };
2471e51764aSArtem Bityutskiy 
2481e51764aSArtem Bityutskiy /*
2491e51764aSArtem Bityutskiy  * Dirty flag bits (lpt_drty_flgs) for LPT special nodes.
2501e51764aSArtem Bityutskiy  *
2511e51764aSArtem Bityutskiy  * LTAB_DIRTY: ltab node is dirty
2521e51764aSArtem Bityutskiy  * LSAVE_DIRTY: lsave node is dirty
2531e51764aSArtem Bityutskiy  */
2541e51764aSArtem Bityutskiy enum {
2551e51764aSArtem Bityutskiy 	LTAB_DIRTY  = 1,
2561e51764aSArtem Bityutskiy 	LSAVE_DIRTY = 2,
2571e51764aSArtem Bityutskiy };
2581e51764aSArtem Bityutskiy 
2591e51764aSArtem Bityutskiy /*
2601e51764aSArtem Bityutskiy  * Return codes used by the garbage collector.
2611e51764aSArtem Bityutskiy  * @LEB_FREED: the logical eraseblock was freed and is ready to use
2621e51764aSArtem Bityutskiy  * @LEB_FREED_IDX: indexing LEB was freed and can be used only after the commit
2631e51764aSArtem Bityutskiy  * @LEB_RETAINED: the logical eraseblock was freed and retained for GC purposes
2641e51764aSArtem Bityutskiy  */
2651e51764aSArtem Bityutskiy enum {
2661e51764aSArtem Bityutskiy 	LEB_FREED,
2671e51764aSArtem Bityutskiy 	LEB_FREED_IDX,
2681e51764aSArtem Bityutskiy 	LEB_RETAINED,
2691e51764aSArtem Bityutskiy };
2701e51764aSArtem Bityutskiy 
2712e52eb74SRichard Weinberger /*
2722e52eb74SRichard Weinberger  * Action taken upon a failed ubifs_assert().
2732e52eb74SRichard Weinberger  * @ASSACT_REPORT: just report the failed assertion
2742e52eb74SRichard Weinberger  * @ASSACT_RO: switch to read-only mode
2752e52eb74SRichard Weinberger  * @ASSACT_PANIC: call BUG() and possible panic the kernel
2762e52eb74SRichard Weinberger  */
2772e52eb74SRichard Weinberger enum {
2782e52eb74SRichard Weinberger 	ASSACT_REPORT = 0,
2792e52eb74SRichard Weinberger 	ASSACT_RO,
2802e52eb74SRichard Weinberger 	ASSACT_PANIC,
2812e52eb74SRichard Weinberger };
2822e52eb74SRichard Weinberger 
2831e51764aSArtem Bityutskiy /**
2841e51764aSArtem Bityutskiy  * struct ubifs_old_idx - index node obsoleted since last commit start.
2851e51764aSArtem Bityutskiy  * @rb: rb-tree node
2861e51764aSArtem Bityutskiy  * @lnum: LEB number of obsoleted index node
2871e51764aSArtem Bityutskiy  * @offs: offset of obsoleted index node
2881e51764aSArtem Bityutskiy  */
2891e51764aSArtem Bityutskiy struct ubifs_old_idx {
2901e51764aSArtem Bityutskiy 	struct rb_node rb;
2911e51764aSArtem Bityutskiy 	int lnum;
2921e51764aSArtem Bityutskiy 	int offs;
2931e51764aSArtem Bityutskiy };
2941e51764aSArtem Bityutskiy 
2951e51764aSArtem Bityutskiy /* The below union makes it easier to deal with keys */
2961e51764aSArtem Bityutskiy union ubifs_key {
2977ca58badSArtem Bityutskiy 	uint8_t u8[UBIFS_SK_LEN];
2987ca58badSArtem Bityutskiy 	uint32_t u32[UBIFS_SK_LEN/4];
2997ca58badSArtem Bityutskiy 	uint64_t u64[UBIFS_SK_LEN/8];
3007ca58badSArtem Bityutskiy 	__le32 j32[UBIFS_SK_LEN/4];
3011e51764aSArtem Bityutskiy };
3021e51764aSArtem Bityutskiy 
3031e51764aSArtem Bityutskiy /**
3041e51764aSArtem Bityutskiy  * struct ubifs_scan_node - UBIFS scanned node information.
3051e51764aSArtem Bityutskiy  * @list: list of scanned nodes
3061e51764aSArtem Bityutskiy  * @key: key of node scanned (if it has one)
3071e51764aSArtem Bityutskiy  * @sqnum: sequence number
3081e51764aSArtem Bityutskiy  * @type: type of node scanned
3091e51764aSArtem Bityutskiy  * @offs: offset with LEB of node scanned
3101e51764aSArtem Bityutskiy  * @len: length of node scanned
3111e51764aSArtem Bityutskiy  * @node: raw node
3121e51764aSArtem Bityutskiy  */
3131e51764aSArtem Bityutskiy struct ubifs_scan_node {
3141e51764aSArtem Bityutskiy 	struct list_head list;
3151e51764aSArtem Bityutskiy 	union ubifs_key key;
3161e51764aSArtem Bityutskiy 	unsigned long long sqnum;
3171e51764aSArtem Bityutskiy 	int type;
3181e51764aSArtem Bityutskiy 	int offs;
3191e51764aSArtem Bityutskiy 	int len;
3201e51764aSArtem Bityutskiy 	void *node;
3211e51764aSArtem Bityutskiy };
3221e51764aSArtem Bityutskiy 
3231e51764aSArtem Bityutskiy /**
3241e51764aSArtem Bityutskiy  * struct ubifs_scan_leb - UBIFS scanned LEB information.
3251e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
3261e51764aSArtem Bityutskiy  * @nodes_cnt: number of nodes scanned
3271e51764aSArtem Bityutskiy  * @nodes: list of struct ubifs_scan_node
3281e51764aSArtem Bityutskiy  * @endpt: end point (and therefore the start of empty space)
3291e51764aSArtem Bityutskiy  * @buf: buffer containing entire LEB scanned
3301e51764aSArtem Bityutskiy  */
3311e51764aSArtem Bityutskiy struct ubifs_scan_leb {
3321e51764aSArtem Bityutskiy 	int lnum;
3331e51764aSArtem Bityutskiy 	int nodes_cnt;
3341e51764aSArtem Bityutskiy 	struct list_head nodes;
3351e51764aSArtem Bityutskiy 	int endpt;
3361e51764aSArtem Bityutskiy 	void *buf;
3371e51764aSArtem Bityutskiy };
3381e51764aSArtem Bityutskiy 
3391e51764aSArtem Bityutskiy /**
3401e51764aSArtem Bityutskiy  * struct ubifs_gced_idx_leb - garbage-collected indexing LEB.
3411e51764aSArtem Bityutskiy  * @list: list
3421e51764aSArtem Bityutskiy  * @lnum: LEB number
3431e51764aSArtem Bityutskiy  * @unmap: OK to unmap this LEB
3441e51764aSArtem Bityutskiy  *
3451e51764aSArtem Bityutskiy  * This data structure is used to temporary store garbage-collected indexing
3461e51764aSArtem Bityutskiy  * LEBs - they are not released immediately, but only after the next commit.
3471e51764aSArtem Bityutskiy  * This is needed to guarantee recoverability.
3481e51764aSArtem Bityutskiy  */
3491e51764aSArtem Bityutskiy struct ubifs_gced_idx_leb {
3501e51764aSArtem Bityutskiy 	struct list_head list;
3511e51764aSArtem Bityutskiy 	int lnum;
3521e51764aSArtem Bityutskiy 	int unmap;
3531e51764aSArtem Bityutskiy };
3541e51764aSArtem Bityutskiy 
3551e51764aSArtem Bityutskiy /**
3561e51764aSArtem Bityutskiy  * struct ubifs_inode - UBIFS in-memory inode description.
3571e51764aSArtem Bityutskiy  * @vfs_inode: VFS inode description object
3581e51764aSArtem Bityutskiy  * @creat_sqnum: sequence number at time of creation
359de94eb55SArtem Bityutskiy  * @del_cmtno: commit number corresponding to the time the inode was deleted,
360de94eb55SArtem Bityutskiy  *             protected by @c->commit_sem;
3611e51764aSArtem Bityutskiy  * @xattr_size: summarized size of all extended attributes in bytes
3621e51764aSArtem Bityutskiy  * @xattr_cnt: count of extended attributes this inode has
3631e51764aSArtem Bityutskiy  * @xattr_names: sum of lengths of all extended attribute names belonging to
3641e51764aSArtem Bityutskiy  *               this inode
3651e51764aSArtem Bityutskiy  * @dirty: non-zero if the inode is dirty
3661e51764aSArtem Bityutskiy  * @xattr: non-zero if this is an extended attribute inode
367625bf371SArtem Bityutskiy  * @bulk_read: non-zero if bulk-read should be used
3681e51764aSArtem Bityutskiy  * @ui_mutex: serializes inode write-back with the rest of VFS operations,
3694793e7c5SAdrian Hunter  *            serializes "clean <-> dirty" state changes, serializes bulk-read,
370ba60ecabSArtem Bityutskiy  *            protects @dirty, @bulk_read, @ui_size, and @xattr_size
3711e51764aSArtem Bityutskiy  * @ui_lock: protects @synced_i_size
3721e51764aSArtem Bityutskiy  * @synced_i_size: synchronized size of inode, i.e. the value of inode size
3731e51764aSArtem Bityutskiy  *                 currently stored on the flash; used only for regular file
3741e51764aSArtem Bityutskiy  *                 inodes
3751e51764aSArtem Bityutskiy  * @ui_size: inode size used by UBIFS when writing to flash
3761e51764aSArtem Bityutskiy  * @flags: inode flags (@UBIFS_COMPR_FL, etc)
3771e51764aSArtem Bityutskiy  * @compr_type: default compression type used for this inode
3784793e7c5SAdrian Hunter  * @last_page_read: page number of last page read (for bulk read)
3794793e7c5SAdrian Hunter  * @read_in_a_row: number of consecutive pages read in a row (for bulk read)
3801e51764aSArtem Bityutskiy  * @data_len: length of the data attached to the inode
3811e51764aSArtem Bityutskiy  * @data: inode's data
3821e51764aSArtem Bityutskiy  *
3831e51764aSArtem Bityutskiy  * @ui_mutex exists for two main reasons. At first it prevents inodes from
3841e51764aSArtem Bityutskiy  * being written back while UBIFS changing them, being in the middle of an VFS
3851e51764aSArtem Bityutskiy  * operation. This way UBIFS makes sure the inode fields are consistent. For
3861e51764aSArtem Bityutskiy  * example, in 'ubifs_rename()' we change 3 inodes simultaneously, and
3871e51764aSArtem Bityutskiy  * write-back must not write any of them before we have finished.
3881e51764aSArtem Bityutskiy  *
3891e51764aSArtem Bityutskiy  * The second reason is budgeting - UBIFS has to budget all operations. If an
3901e51764aSArtem Bityutskiy  * operation is going to mark an inode dirty, it has to allocate budget for
3911e51764aSArtem Bityutskiy  * this. It cannot just mark it dirty because there is no guarantee there will
3921e51764aSArtem Bityutskiy  * be enough flash space to write the inode back later. This means UBIFS has
3931e51764aSArtem Bityutskiy  * to have full control over inode "clean <-> dirty" transitions (and pages
3941e51764aSArtem Bityutskiy  * actually). But unfortunately, VFS marks inodes dirty in many places, and it
3951e51764aSArtem Bityutskiy  * does not ask the file-system if it is allowed to do so (there is a notifier,
3961e51764aSArtem Bityutskiy  * but it is not enough), i.e., there is no mechanism to synchronize with this.
3971e51764aSArtem Bityutskiy  * So UBIFS has its own inode dirty flag and its own mutex to serialize
3981e51764aSArtem Bityutskiy  * "clean <-> dirty" transitions.
3991e51764aSArtem Bityutskiy  *
4001e51764aSArtem Bityutskiy  * The @synced_i_size field is used to make sure we never write pages which are
4011e51764aSArtem Bityutskiy  * beyond last synchronized inode size. See 'ubifs_writepage()' for more
4021e51764aSArtem Bityutskiy  * information.
4031e51764aSArtem Bityutskiy  *
4041e51764aSArtem Bityutskiy  * The @ui_size is a "shadow" variable for @inode->i_size and UBIFS uses
4051e51764aSArtem Bityutskiy  * @ui_size instead of @inode->i_size. The reason for this is that UBIFS cannot
4061e51764aSArtem Bityutskiy  * make sure @inode->i_size is always changed under @ui_mutex, because it
407c4361570SArtem Bityutskiy  * cannot call 'truncate_setsize()' with @ui_mutex locked, because it would
408c4361570SArtem Bityutskiy  * deadlock with 'ubifs_writepage()' (see file.c). All the other inode fields
409c4361570SArtem Bityutskiy  * are changed under @ui_mutex, so they do not need "shadow" fields. Note, one
4101e51764aSArtem Bityutskiy  * could consider to rework locking and base it on "shadow" fields.
4111e51764aSArtem Bityutskiy  */
4121e51764aSArtem Bityutskiy struct ubifs_inode {
4131e51764aSArtem Bityutskiy 	struct inode vfs_inode;
4141e51764aSArtem Bityutskiy 	unsigned long long creat_sqnum;
415de94eb55SArtem Bityutskiy 	unsigned long long del_cmtno;
4161e51764aSArtem Bityutskiy 	unsigned int xattr_size;
4171e51764aSArtem Bityutskiy 	unsigned int xattr_cnt;
4181e51764aSArtem Bityutskiy 	unsigned int xattr_names;
4191e51764aSArtem Bityutskiy 	unsigned int dirty:1;
4201e51764aSArtem Bityutskiy 	unsigned int xattr:1;
421625bf371SArtem Bityutskiy 	unsigned int bulk_read:1;
422a1dc080cSArtem Bityutskiy 	unsigned int compr_type:2;
4231e51764aSArtem Bityutskiy 	struct mutex ui_mutex;
4241e51764aSArtem Bityutskiy 	spinlock_t ui_lock;
4251e51764aSArtem Bityutskiy 	loff_t synced_i_size;
4261e51764aSArtem Bityutskiy 	loff_t ui_size;
4271e51764aSArtem Bityutskiy 	int flags;
4284793e7c5SAdrian Hunter 	pgoff_t last_page_read;
4294793e7c5SAdrian Hunter 	pgoff_t read_in_a_row;
4301e51764aSArtem Bityutskiy 	int data_len;
4311e51764aSArtem Bityutskiy 	void *data;
4321e51764aSArtem Bityutskiy };
4331e51764aSArtem Bityutskiy 
4341e51764aSArtem Bityutskiy /**
4351e51764aSArtem Bityutskiy  * struct ubifs_unclean_leb - records a LEB recovered under read-only mode.
4361e51764aSArtem Bityutskiy  * @list: list
4371e51764aSArtem Bityutskiy  * @lnum: LEB number of recovered LEB
4381e51764aSArtem Bityutskiy  * @endpt: offset where recovery ended
4391e51764aSArtem Bityutskiy  *
4401e51764aSArtem Bityutskiy  * This structure records a LEB identified during recovery that needs to be
4411e51764aSArtem Bityutskiy  * cleaned but was not because UBIFS was mounted read-only. The information
4421e51764aSArtem Bityutskiy  * is used to clean the LEB when remounting to read-write mode.
4431e51764aSArtem Bityutskiy  */
4441e51764aSArtem Bityutskiy struct ubifs_unclean_leb {
4451e51764aSArtem Bityutskiy 	struct list_head list;
4461e51764aSArtem Bityutskiy 	int lnum;
4471e51764aSArtem Bityutskiy 	int endpt;
4481e51764aSArtem Bityutskiy };
4491e51764aSArtem Bityutskiy 
4501e51764aSArtem Bityutskiy /*
4511e51764aSArtem Bityutskiy  * LEB properties flags.
4521e51764aSArtem Bityutskiy  *
4531e51764aSArtem Bityutskiy  * LPROPS_UNCAT: not categorized
4547078202eSArtem Bityutskiy  * LPROPS_DIRTY: dirty > free, dirty >= @c->dead_wm, not index
45521a60258SArtem Bityutskiy  * LPROPS_DIRTY_IDX: dirty + free > @c->min_idx_node_sze and index
4567078202eSArtem Bityutskiy  * LPROPS_FREE: free > 0, dirty < @c->dead_wm, not empty, not index
4571e51764aSArtem Bityutskiy  * LPROPS_HEAP_CNT: number of heaps used for storing categorized LEBs
4581e51764aSArtem Bityutskiy  * LPROPS_EMPTY: LEB is empty, not taken
4591e51764aSArtem Bityutskiy  * LPROPS_FREEABLE: free + dirty == leb_size, not index, not taken
4601e51764aSArtem Bityutskiy  * LPROPS_FRDI_IDX: free + dirty == leb_size and index, may be taken
4611e51764aSArtem Bityutskiy  * LPROPS_CAT_MASK: mask for the LEB categories above
4621e51764aSArtem Bityutskiy  * LPROPS_TAKEN: LEB was taken (this flag is not saved on the media)
4631e51764aSArtem Bityutskiy  * LPROPS_INDEX: LEB contains indexing nodes (this flag also exists on flash)
4641e51764aSArtem Bityutskiy  */
4651e51764aSArtem Bityutskiy enum {
4661e51764aSArtem Bityutskiy 	LPROPS_UNCAT     =  0,
4671e51764aSArtem Bityutskiy 	LPROPS_DIRTY     =  1,
4681e51764aSArtem Bityutskiy 	LPROPS_DIRTY_IDX =  2,
4691e51764aSArtem Bityutskiy 	LPROPS_FREE      =  3,
4701e51764aSArtem Bityutskiy 	LPROPS_HEAP_CNT  =  3,
4711e51764aSArtem Bityutskiy 	LPROPS_EMPTY     =  4,
4721e51764aSArtem Bityutskiy 	LPROPS_FREEABLE  =  5,
4731e51764aSArtem Bityutskiy 	LPROPS_FRDI_IDX  =  6,
4741e51764aSArtem Bityutskiy 	LPROPS_CAT_MASK  = 15,
4751e51764aSArtem Bityutskiy 	LPROPS_TAKEN     = 16,
4761e51764aSArtem Bityutskiy 	LPROPS_INDEX     = 32,
4771e51764aSArtem Bityutskiy };
4781e51764aSArtem Bityutskiy 
4791e51764aSArtem Bityutskiy /**
4801e51764aSArtem Bityutskiy  * struct ubifs_lprops - logical eraseblock properties.
4811e51764aSArtem Bityutskiy  * @free: amount of free space in bytes
4821e51764aSArtem Bityutskiy  * @dirty: amount of dirty space in bytes
4831e51764aSArtem Bityutskiy  * @flags: LEB properties flags (see above)
4841e51764aSArtem Bityutskiy  * @lnum: LEB number
4851e51764aSArtem Bityutskiy  * @list: list of same-category lprops (for LPROPS_EMPTY and LPROPS_FREEABLE)
4861e51764aSArtem Bityutskiy  * @hpos: heap position in heap of same-category lprops (other categories)
4871e51764aSArtem Bityutskiy  */
4881e51764aSArtem Bityutskiy struct ubifs_lprops {
4891e51764aSArtem Bityutskiy 	int free;
4901e51764aSArtem Bityutskiy 	int dirty;
4911e51764aSArtem Bityutskiy 	int flags;
4921e51764aSArtem Bityutskiy 	int lnum;
4931e51764aSArtem Bityutskiy 	union {
4941e51764aSArtem Bityutskiy 		struct list_head list;
4951e51764aSArtem Bityutskiy 		int hpos;
4961e51764aSArtem Bityutskiy 	};
4971e51764aSArtem Bityutskiy };
4981e51764aSArtem Bityutskiy 
4991e51764aSArtem Bityutskiy /**
5001e51764aSArtem Bityutskiy  * struct ubifs_lpt_lprops - LPT logical eraseblock properties.
5011e51764aSArtem Bityutskiy  * @free: amount of free space in bytes
5021e51764aSArtem Bityutskiy  * @dirty: amount of dirty space in bytes
5031e51764aSArtem Bityutskiy  * @tgc: trivial GC flag (1 => unmap after commit end)
5041e51764aSArtem Bityutskiy  * @cmt: commit flag (1 => reserved for commit)
5051e51764aSArtem Bityutskiy  */
5061e51764aSArtem Bityutskiy struct ubifs_lpt_lprops {
5071e51764aSArtem Bityutskiy 	int free;
5081e51764aSArtem Bityutskiy 	int dirty;
5091e51764aSArtem Bityutskiy 	unsigned tgc:1;
5101e51764aSArtem Bityutskiy 	unsigned cmt:1;
5111e51764aSArtem Bityutskiy };
5121e51764aSArtem Bityutskiy 
5131e51764aSArtem Bityutskiy /**
5141e51764aSArtem Bityutskiy  * struct ubifs_lp_stats - statistics of eraseblocks in the main area.
5151e51764aSArtem Bityutskiy  * @empty_lebs: number of empty LEBs
5161e51764aSArtem Bityutskiy  * @taken_empty_lebs: number of taken LEBs
5171e51764aSArtem Bityutskiy  * @idx_lebs: number of indexing LEBs
518d3cf502bSArtem Bityutskiy  * @total_free: total free space in bytes (includes all LEBs)
519d3cf502bSArtem Bityutskiy  * @total_dirty: total dirty space in bytes (includes all LEBs)
520d3cf502bSArtem Bityutskiy  * @total_used: total used space in bytes (does not include index LEBs)
521d3cf502bSArtem Bityutskiy  * @total_dead: total dead space in bytes (does not include index LEBs)
522d3cf502bSArtem Bityutskiy  * @total_dark: total dark space in bytes (does not include index LEBs)
5231e51764aSArtem Bityutskiy  *
524d3cf502bSArtem Bityutskiy  * The @taken_empty_lebs field counts the LEBs that are in the transient state
525d3cf502bSArtem Bityutskiy  * of having been "taken" for use but not yet written to. @taken_empty_lebs is
526d3cf502bSArtem Bityutskiy  * needed to account correctly for @gc_lnum, otherwise @empty_lebs could be
527d3cf502bSArtem Bityutskiy  * used by itself (in which case 'unused_lebs' would be a better name). In the
528d3cf502bSArtem Bityutskiy  * case of @gc_lnum, it is "taken" at mount time or whenever a LEB is retained
529d3cf502bSArtem Bityutskiy  * by GC, but unlike other empty LEBs that are "taken", it may not be written
530d3cf502bSArtem Bityutskiy  * straight away (i.e. before the next commit start or unmount), so either
531d3cf502bSArtem Bityutskiy  * @gc_lnum must be specially accounted for, or the current approach followed
532d3cf502bSArtem Bityutskiy  * i.e. count it under @taken_empty_lebs.
5331e51764aSArtem Bityutskiy  *
534d3cf502bSArtem Bityutskiy  * @empty_lebs includes @taken_empty_lebs.
535d3cf502bSArtem Bityutskiy  *
536d3cf502bSArtem Bityutskiy  * @total_used, @total_dead and @total_dark fields do not account indexing
537d3cf502bSArtem Bityutskiy  * LEBs.
5381e51764aSArtem Bityutskiy  */
5391e51764aSArtem Bityutskiy struct ubifs_lp_stats {
5401e51764aSArtem Bityutskiy 	int empty_lebs;
5411e51764aSArtem Bityutskiy 	int taken_empty_lebs;
5421e51764aSArtem Bityutskiy 	int idx_lebs;
5431e51764aSArtem Bityutskiy 	long long total_free;
5441e51764aSArtem Bityutskiy 	long long total_dirty;
5451e51764aSArtem Bityutskiy 	long long total_used;
5461e51764aSArtem Bityutskiy 	long long total_dead;
5471e51764aSArtem Bityutskiy 	long long total_dark;
5481e51764aSArtem Bityutskiy };
5491e51764aSArtem Bityutskiy 
5501e51764aSArtem Bityutskiy struct ubifs_nnode;
5511e51764aSArtem Bityutskiy 
5521e51764aSArtem Bityutskiy /**
5531e51764aSArtem Bityutskiy  * struct ubifs_cnode - LEB Properties Tree common node.
5541e51764aSArtem Bityutskiy  * @parent: parent nnode
5551e51764aSArtem Bityutskiy  * @cnext: next cnode to commit
5561e51764aSArtem Bityutskiy  * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
5571e51764aSArtem Bityutskiy  * @iip: index in parent
5581e51764aSArtem Bityutskiy  * @level: level in the tree (zero for pnodes, greater than zero for nnodes)
5591e51764aSArtem Bityutskiy  * @num: node number
5601e51764aSArtem Bityutskiy  */
5611e51764aSArtem Bityutskiy struct ubifs_cnode {
5621e51764aSArtem Bityutskiy 	struct ubifs_nnode *parent;
5631e51764aSArtem Bityutskiy 	struct ubifs_cnode *cnext;
5641e51764aSArtem Bityutskiy 	unsigned long flags;
5651e51764aSArtem Bityutskiy 	int iip;
5661e51764aSArtem Bityutskiy 	int level;
5671e51764aSArtem Bityutskiy 	int num;
5681e51764aSArtem Bityutskiy };
5691e51764aSArtem Bityutskiy 
5701e51764aSArtem Bityutskiy /**
5711e51764aSArtem Bityutskiy  * struct ubifs_pnode - LEB Properties Tree leaf node.
5721e51764aSArtem Bityutskiy  * @parent: parent nnode
5731e51764aSArtem Bityutskiy  * @cnext: next cnode to commit
5741e51764aSArtem Bityutskiy  * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
5751e51764aSArtem Bityutskiy  * @iip: index in parent
5761e51764aSArtem Bityutskiy  * @level: level in the tree (always zero for pnodes)
5771e51764aSArtem Bityutskiy  * @num: node number
5781e51764aSArtem Bityutskiy  * @lprops: LEB properties array
5791e51764aSArtem Bityutskiy  */
5801e51764aSArtem Bityutskiy struct ubifs_pnode {
5811e51764aSArtem Bityutskiy 	struct ubifs_nnode *parent;
5821e51764aSArtem Bityutskiy 	struct ubifs_cnode *cnext;
5831e51764aSArtem Bityutskiy 	unsigned long flags;
5841e51764aSArtem Bityutskiy 	int iip;
5851e51764aSArtem Bityutskiy 	int level;
5861e51764aSArtem Bityutskiy 	int num;
5871e51764aSArtem Bityutskiy 	struct ubifs_lprops lprops[UBIFS_LPT_FANOUT];
5881e51764aSArtem Bityutskiy };
5891e51764aSArtem Bityutskiy 
5901e51764aSArtem Bityutskiy /**
5911e51764aSArtem Bityutskiy  * struct ubifs_nbranch - LEB Properties Tree internal node branch.
5921e51764aSArtem Bityutskiy  * @lnum: LEB number of child
5931e51764aSArtem Bityutskiy  * @offs: offset of child
5941e51764aSArtem Bityutskiy  * @nnode: nnode child
5951e51764aSArtem Bityutskiy  * @pnode: pnode child
5961e51764aSArtem Bityutskiy  * @cnode: cnode child
5971e51764aSArtem Bityutskiy  */
5981e51764aSArtem Bityutskiy struct ubifs_nbranch {
5991e51764aSArtem Bityutskiy 	int lnum;
6001e51764aSArtem Bityutskiy 	int offs;
6011e51764aSArtem Bityutskiy 	union {
6021e51764aSArtem Bityutskiy 		struct ubifs_nnode *nnode;
6031e51764aSArtem Bityutskiy 		struct ubifs_pnode *pnode;
6041e51764aSArtem Bityutskiy 		struct ubifs_cnode *cnode;
6051e51764aSArtem Bityutskiy 	};
6061e51764aSArtem Bityutskiy };
6071e51764aSArtem Bityutskiy 
6081e51764aSArtem Bityutskiy /**
6091e51764aSArtem Bityutskiy  * struct ubifs_nnode - LEB Properties Tree internal node.
6101e51764aSArtem Bityutskiy  * @parent: parent nnode
6111e51764aSArtem Bityutskiy  * @cnext: next cnode to commit
6121e51764aSArtem Bityutskiy  * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
6131e51764aSArtem Bityutskiy  * @iip: index in parent
6141e51764aSArtem Bityutskiy  * @level: level in the tree (always greater than zero for nnodes)
6151e51764aSArtem Bityutskiy  * @num: node number
6161e51764aSArtem Bityutskiy  * @nbranch: branches to child nodes
6171e51764aSArtem Bityutskiy  */
6181e51764aSArtem Bityutskiy struct ubifs_nnode {
6191e51764aSArtem Bityutskiy 	struct ubifs_nnode *parent;
6201e51764aSArtem Bityutskiy 	struct ubifs_cnode *cnext;
6211e51764aSArtem Bityutskiy 	unsigned long flags;
6221e51764aSArtem Bityutskiy 	int iip;
6231e51764aSArtem Bityutskiy 	int level;
6241e51764aSArtem Bityutskiy 	int num;
6251e51764aSArtem Bityutskiy 	struct ubifs_nbranch nbranch[UBIFS_LPT_FANOUT];
6261e51764aSArtem Bityutskiy };
6271e51764aSArtem Bityutskiy 
6281e51764aSArtem Bityutskiy /**
6291e51764aSArtem Bityutskiy  * struct ubifs_lpt_heap - heap of categorized lprops.
6301e51764aSArtem Bityutskiy  * @arr: heap array
6311e51764aSArtem Bityutskiy  * @cnt: number in heap
6321e51764aSArtem Bityutskiy  * @max_cnt: maximum number allowed in heap
6331e51764aSArtem Bityutskiy  *
6341e51764aSArtem Bityutskiy  * There are %LPROPS_HEAP_CNT heaps.
6351e51764aSArtem Bityutskiy  */
6361e51764aSArtem Bityutskiy struct ubifs_lpt_heap {
6371e51764aSArtem Bityutskiy 	struct ubifs_lprops **arr;
6381e51764aSArtem Bityutskiy 	int cnt;
6391e51764aSArtem Bityutskiy 	int max_cnt;
6401e51764aSArtem Bityutskiy };
6411e51764aSArtem Bityutskiy 
6421e51764aSArtem Bityutskiy /*
6431e51764aSArtem Bityutskiy  * Return codes for LPT scan callback function.
6441e51764aSArtem Bityutskiy  *
6451e51764aSArtem Bityutskiy  * LPT_SCAN_CONTINUE: continue scanning
6461e51764aSArtem Bityutskiy  * LPT_SCAN_ADD: add the LEB properties scanned to the tree in memory
6471e51764aSArtem Bityutskiy  * LPT_SCAN_STOP: stop scanning
6481e51764aSArtem Bityutskiy  */
6491e51764aSArtem Bityutskiy enum {
6501e51764aSArtem Bityutskiy 	LPT_SCAN_CONTINUE = 0,
6511e51764aSArtem Bityutskiy 	LPT_SCAN_ADD = 1,
6521e51764aSArtem Bityutskiy 	LPT_SCAN_STOP = 2,
6531e51764aSArtem Bityutskiy };
6541e51764aSArtem Bityutskiy 
6551e51764aSArtem Bityutskiy struct ubifs_info;
6561e51764aSArtem Bityutskiy 
6571e51764aSArtem Bityutskiy /* Callback used by the 'ubifs_lpt_scan_nolock()' function */
6581e51764aSArtem Bityutskiy typedef int (*ubifs_lpt_scan_callback)(struct ubifs_info *c,
6591e51764aSArtem Bityutskiy 				       const struct ubifs_lprops *lprops,
6601e51764aSArtem Bityutskiy 				       int in_tree, void *data);
6611e51764aSArtem Bityutskiy 
6621e51764aSArtem Bityutskiy /**
6631e51764aSArtem Bityutskiy  * struct ubifs_wbuf - UBIFS write-buffer.
6641e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
6651e51764aSArtem Bityutskiy  * @buf: write-buffer (of min. flash I/O unit size)
6661e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number the write-buffer points to
6671e51764aSArtem Bityutskiy  * @offs: write-buffer offset in this logical eraseblock
6681e51764aSArtem Bityutskiy  * @avail: number of bytes available in the write-buffer
6691e51764aSArtem Bityutskiy  * @used:  number of used bytes in the write-buffer
6703c89f396SArtem Bityutskiy  * @size: write-buffer size (in [@c->min_io_size, @c->max_write_size] range)
6711e51764aSArtem Bityutskiy  * @jhead: journal head the mutex belongs to (note, needed only to shut lockdep
6721e51764aSArtem Bityutskiy  *         up by 'mutex_lock_nested()).
6731e51764aSArtem Bityutskiy  * @sync_callback: write-buffer synchronization callback
6741e51764aSArtem Bityutskiy  * @io_mutex: serializes write-buffer I/O
6751e51764aSArtem Bityutskiy  * @lock: serializes @buf, @lnum, @offs, @avail, @used, @next_ino and @inodes
6761e51764aSArtem Bityutskiy  *        fields
6771e51764aSArtem Bityutskiy  * @timer: write-buffer timer
678681947d2SAdrian Hunter  * @no_timer: non-zero if this write-buffer does not have a timer
679681947d2SAdrian Hunter  * @need_sync: non-zero if the timer expired and the wbuf needs sync'ing
6801e51764aSArtem Bityutskiy  * @next_ino: points to the next position of the following inode number
6811e51764aSArtem Bityutskiy  * @inodes: stores the inode numbers of the nodes which are in wbuf
6821e51764aSArtem Bityutskiy  *
6831e51764aSArtem Bityutskiy  * The write-buffer synchronization callback is called when the write-buffer is
6841e51764aSArtem Bityutskiy  * synchronized in order to notify how much space was wasted due to
6851e51764aSArtem Bityutskiy  * write-buffer padding and how much free space is left in the LEB.
6861e51764aSArtem Bityutskiy  *
6871e51764aSArtem Bityutskiy  * Note: the fields @buf, @lnum, @offs, @avail and @used can be read under
6881e51764aSArtem Bityutskiy  * spin-lock or mutex because they are written under both mutex and spin-lock.
6891e51764aSArtem Bityutskiy  * @buf is appended to under mutex but overwritten under both mutex and
6901e51764aSArtem Bityutskiy  * spin-lock. Thus the data between @buf and @buf + @used can be read under
6911e51764aSArtem Bityutskiy  * spinlock.
6921e51764aSArtem Bityutskiy  */
6931e51764aSArtem Bityutskiy struct ubifs_wbuf {
6941e51764aSArtem Bityutskiy 	struct ubifs_info *c;
6951e51764aSArtem Bityutskiy 	void *buf;
6961e51764aSArtem Bityutskiy 	int lnum;
6971e51764aSArtem Bityutskiy 	int offs;
6981e51764aSArtem Bityutskiy 	int avail;
6991e51764aSArtem Bityutskiy 	int used;
7003c89f396SArtem Bityutskiy 	int size;
7011e51764aSArtem Bityutskiy 	int jhead;
7021e51764aSArtem Bityutskiy 	int (*sync_callback)(struct ubifs_info *c, int lnum, int free, int pad);
7031e51764aSArtem Bityutskiy 	struct mutex io_mutex;
7041e51764aSArtem Bityutskiy 	spinlock_t lock;
705f2c5dbd7SArtem Bityutskiy 	struct hrtimer timer;
7060b335b9dSArtem Bityutskiy 	unsigned int no_timer:1;
7070b335b9dSArtem Bityutskiy 	unsigned int need_sync:1;
7081e51764aSArtem Bityutskiy 	int next_ino;
7091e51764aSArtem Bityutskiy 	ino_t *inodes;
7101e51764aSArtem Bityutskiy };
7111e51764aSArtem Bityutskiy 
7121e51764aSArtem Bityutskiy /**
7131e51764aSArtem Bityutskiy  * struct ubifs_bud - bud logical eraseblock.
7141e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number
7151e51764aSArtem Bityutskiy  * @start: where the (uncommitted) bud data starts
7161e51764aSArtem Bityutskiy  * @jhead: journal head number this bud belongs to
7171e51764aSArtem Bityutskiy  * @list: link in the list buds belonging to the same journal head
7181e51764aSArtem Bityutskiy  * @rb: link in the tree of all buds
7196a98bc46SSascha Hauer  * @log_hash: the log hash from the commit start node up to this bud
7201e51764aSArtem Bityutskiy  */
7211e51764aSArtem Bityutskiy struct ubifs_bud {
7221e51764aSArtem Bityutskiy 	int lnum;
7231e51764aSArtem Bityutskiy 	int start;
7241e51764aSArtem Bityutskiy 	int jhead;
7251e51764aSArtem Bityutskiy 	struct list_head list;
7261e51764aSArtem Bityutskiy 	struct rb_node rb;
7276a98bc46SSascha Hauer 	struct shash_desc *log_hash;
7281e51764aSArtem Bityutskiy };
7291e51764aSArtem Bityutskiy 
7301e51764aSArtem Bityutskiy /**
7311e51764aSArtem Bityutskiy  * struct ubifs_jhead - journal head.
7321e51764aSArtem Bityutskiy  * @wbuf: head's write-buffer
7331e51764aSArtem Bityutskiy  * @buds_list: list of bud LEBs belonging to this journal head
7341a0b0699SArtem Bityutskiy  * @grouped: non-zero if UBIFS groups nodes when writing to this journal head
7356a98bc46SSascha Hauer  * @log_hash: the log hash from the commit start node up to this journal head
7361e51764aSArtem Bityutskiy  *
7371e51764aSArtem Bityutskiy  * Note, the @buds list is protected by the @c->buds_lock.
7381e51764aSArtem Bityutskiy  */
7391e51764aSArtem Bityutskiy struct ubifs_jhead {
7401e51764aSArtem Bityutskiy 	struct ubifs_wbuf wbuf;
7411e51764aSArtem Bityutskiy 	struct list_head buds_list;
7421a0b0699SArtem Bityutskiy 	unsigned int grouped:1;
7436a98bc46SSascha Hauer 	struct shash_desc *log_hash;
7441e51764aSArtem Bityutskiy };
7451e51764aSArtem Bityutskiy 
7461e51764aSArtem Bityutskiy /**
7471e51764aSArtem Bityutskiy  * struct ubifs_zbranch - key/coordinate/length branch stored in znodes.
7481e51764aSArtem Bityutskiy  * @key: key
7491e51764aSArtem Bityutskiy  * @znode: znode address in memory
750be61678bSArtem Bityutskiy  * @lnum: LEB number of the target node (indexing node or data node)
751be61678bSArtem Bityutskiy  * @offs: target node offset within @lnum
7521e51764aSArtem Bityutskiy  * @len: target node length
753823838a4SSascha Hauer  * @hash: the hash of the target node
7541e51764aSArtem Bityutskiy  */
7551e51764aSArtem Bityutskiy struct ubifs_zbranch {
7561e51764aSArtem Bityutskiy 	union ubifs_key key;
7571e51764aSArtem Bityutskiy 	union {
7581e51764aSArtem Bityutskiy 		struct ubifs_znode *znode;
7591e51764aSArtem Bityutskiy 		void *leaf;
7601e51764aSArtem Bityutskiy 	};
7611e51764aSArtem Bityutskiy 	int lnum;
7621e51764aSArtem Bityutskiy 	int offs;
7631e51764aSArtem Bityutskiy 	int len;
764823838a4SSascha Hauer 	u8 hash[UBIFS_HASH_ARR_SZ];
7651e51764aSArtem Bityutskiy };
7661e51764aSArtem Bityutskiy 
7671e51764aSArtem Bityutskiy /**
7681e51764aSArtem Bityutskiy  * struct ubifs_znode - in-memory representation of an indexing node.
7691e51764aSArtem Bityutskiy  * @parent: parent znode or NULL if it is the root
7701e51764aSArtem Bityutskiy  * @cnext: next znode to commit
77116a26b20SSascha Hauer  * @cparent: parent node for this commit
77216a26b20SSascha Hauer  * @ciip: index in cparent's zbranch array
7731e51764aSArtem Bityutskiy  * @flags: znode flags (%DIRTY_ZNODE, %COW_ZNODE or %OBSOLETE_ZNODE)
7741e51764aSArtem Bityutskiy  * @time: last access time (seconds)
7751e51764aSArtem Bityutskiy  * @level: level of the entry in the TNC tree
7761e51764aSArtem Bityutskiy  * @child_cnt: count of child znodes
7771e51764aSArtem Bityutskiy  * @iip: index in parent's zbranch array
7781e51764aSArtem Bityutskiy  * @alt: lower bound of key range has altered i.e. child inserted at slot 0
7791e51764aSArtem Bityutskiy  * @lnum: LEB number of the corresponding indexing node
7801e51764aSArtem Bityutskiy  * @offs: offset of the corresponding indexing node
7811e51764aSArtem Bityutskiy  * @len: length  of the corresponding indexing node
7821e51764aSArtem Bityutskiy  * @zbranch: array of znode branches (@c->fanout elements)
783f70b7e52SArtem Bityutskiy  *
784f70b7e52SArtem Bityutskiy  * Note! The @lnum, @offs, and @len fields are not really needed - we have them
785f70b7e52SArtem Bityutskiy  * only for internal consistency check. They could be removed to save some RAM.
7861e51764aSArtem Bityutskiy  */
7871e51764aSArtem Bityutskiy struct ubifs_znode {
7881e51764aSArtem Bityutskiy 	struct ubifs_znode *parent;
7891e51764aSArtem Bityutskiy 	struct ubifs_znode *cnext;
79016a26b20SSascha Hauer 	struct ubifs_znode *cparent;
79116a26b20SSascha Hauer 	int ciip;
7921e51764aSArtem Bityutskiy 	unsigned long flags;
7936cff5732SArnd Bergmann 	time64_t time;
7941e51764aSArtem Bityutskiy 	int level;
7951e51764aSArtem Bityutskiy 	int child_cnt;
7961e51764aSArtem Bityutskiy 	int iip;
7971e51764aSArtem Bityutskiy 	int alt;
798f70b7e52SArtem Bityutskiy 	int lnum;
799f70b7e52SArtem Bityutskiy 	int offs;
800f70b7e52SArtem Bityutskiy 	int len;
8011e51764aSArtem Bityutskiy 	struct ubifs_zbranch zbranch[];
8021e51764aSArtem Bityutskiy };
8031e51764aSArtem Bityutskiy 
8041e51764aSArtem Bityutskiy /**
80539ce81ceSArtem Bityutskiy  * struct bu_info - bulk-read information.
8064793e7c5SAdrian Hunter  * @key: first data node key
8074793e7c5SAdrian Hunter  * @zbranch: zbranches of data nodes to bulk read
8084793e7c5SAdrian Hunter  * @buf: buffer to read into
8094793e7c5SAdrian Hunter  * @buf_len: buffer length
8104793e7c5SAdrian Hunter  * @gc_seq: GC sequence number to detect races with GC
8114793e7c5SAdrian Hunter  * @cnt: number of data nodes for bulk read
8124793e7c5SAdrian Hunter  * @blk_cnt: number of data blocks including holes
8134793e7c5SAdrian Hunter  * @oef: end of file reached
8144793e7c5SAdrian Hunter  */
8154793e7c5SAdrian Hunter struct bu_info {
8164793e7c5SAdrian Hunter 	union ubifs_key key;
8174793e7c5SAdrian Hunter 	struct ubifs_zbranch zbranch[UBIFS_MAX_BULK_READ];
8184793e7c5SAdrian Hunter 	void *buf;
8194793e7c5SAdrian Hunter 	int buf_len;
8204793e7c5SAdrian Hunter 	int gc_seq;
8214793e7c5SAdrian Hunter 	int cnt;
8224793e7c5SAdrian Hunter 	int blk_cnt;
8234793e7c5SAdrian Hunter 	int eof;
8244793e7c5SAdrian Hunter };
8254793e7c5SAdrian Hunter 
8264793e7c5SAdrian Hunter /**
8271e51764aSArtem Bityutskiy  * struct ubifs_node_range - node length range description data structure.
8281e51764aSArtem Bityutskiy  * @len: fixed node length
8291e51764aSArtem Bityutskiy  * @min_len: minimum possible node length
8301e51764aSArtem Bityutskiy  * @max_len: maximum possible node length
8311e51764aSArtem Bityutskiy  *
8321e51764aSArtem Bityutskiy  * If @max_len is %0, the node has fixed length @len.
8331e51764aSArtem Bityutskiy  */
8341e51764aSArtem Bityutskiy struct ubifs_node_range {
8351e51764aSArtem Bityutskiy 	union {
8361e51764aSArtem Bityutskiy 		int len;
8371e51764aSArtem Bityutskiy 		int min_len;
8381e51764aSArtem Bityutskiy 	};
8391e51764aSArtem Bityutskiy 	int max_len;
8401e51764aSArtem Bityutskiy };
8411e51764aSArtem Bityutskiy 
8421e51764aSArtem Bityutskiy /**
8431e51764aSArtem Bityutskiy  * struct ubifs_compressor - UBIFS compressor description structure.
8441e51764aSArtem Bityutskiy  * @compr_type: compressor type (%UBIFS_COMPR_LZO, etc)
8451e51764aSArtem Bityutskiy  * @cc: cryptoapi compressor handle
8461e51764aSArtem Bityutskiy  * @comp_mutex: mutex used during compression
8471e51764aSArtem Bityutskiy  * @decomp_mutex: mutex used during decompression
8481e51764aSArtem Bityutskiy  * @name: compressor name
8491e51764aSArtem Bityutskiy  * @capi_name: cryptoapi compressor name
8501e51764aSArtem Bityutskiy  */
8511e51764aSArtem Bityutskiy struct ubifs_compressor {
8521e51764aSArtem Bityutskiy 	int compr_type;
8531e51764aSArtem Bityutskiy 	struct crypto_comp *cc;
8541e51764aSArtem Bityutskiy 	struct mutex *comp_mutex;
8551e51764aSArtem Bityutskiy 	struct mutex *decomp_mutex;
8561e51764aSArtem Bityutskiy 	const char *name;
8571e51764aSArtem Bityutskiy 	const char *capi_name;
8581e51764aSArtem Bityutskiy };
8591e51764aSArtem Bityutskiy 
8601e51764aSArtem Bityutskiy /**
8611e51764aSArtem Bityutskiy  * struct ubifs_budget_req - budget requirements of an operation.
8621e51764aSArtem Bityutskiy  *
863de94eb55SArtem Bityutskiy  * @fast: non-zero if the budgeting should try to acquire budget quickly and
8641e51764aSArtem Bityutskiy  *        should not try to call write-back
8651e51764aSArtem Bityutskiy  * @recalculate: non-zero if @idx_growth, @data_growth, and @dd_growth fields
8661e51764aSArtem Bityutskiy  *               have to be re-calculated
8671e51764aSArtem Bityutskiy  * @new_page: non-zero if the operation adds a new page
8681e51764aSArtem Bityutskiy  * @dirtied_page: non-zero if the operation makes a page dirty
8691e51764aSArtem Bityutskiy  * @new_dent: non-zero if the operation adds a new directory entry
8701e51764aSArtem Bityutskiy  * @mod_dent: non-zero if the operation removes or modifies an existing
8711e51764aSArtem Bityutskiy  *            directory entry
8721e51764aSArtem Bityutskiy  * @new_ino: non-zero if the operation adds a new inode
8737d25b361SDongsheng Yang  * @new_ino_d: how much data newly created inode contains
8741e51764aSArtem Bityutskiy  * @dirtied_ino: how many inodes the operation makes dirty
8757d25b361SDongsheng Yang  * @dirtied_ino_d: how much data dirtied inode contains
8761e51764aSArtem Bityutskiy  * @idx_growth: how much the index will supposedly grow
8771e51764aSArtem Bityutskiy  * @data_growth: how much new data the operation will supposedly add
8781e51764aSArtem Bityutskiy  * @dd_growth: how much data that makes other data dirty the operation will
8791e51764aSArtem Bityutskiy  *             supposedly add
8801e51764aSArtem Bityutskiy  *
8811e51764aSArtem Bityutskiy  * @idx_growth, @data_growth and @dd_growth are not used in budget request. The
8821e51764aSArtem Bityutskiy  * budgeting subsystem caches index and data growth values there to avoid
8831e51764aSArtem Bityutskiy  * re-calculating them when the budget is released. However, if @idx_growth is
8841e51764aSArtem Bityutskiy  * %-1, it is calculated by the release function using other fields.
8851e51764aSArtem Bityutskiy  *
8861e51764aSArtem Bityutskiy  * An inode may contain 4KiB of data at max., thus the widths of @new_ino_d
8871e51764aSArtem Bityutskiy  * is 13 bits, and @dirtied_ino_d - 15, because up to 4 inodes may be made
8881e51764aSArtem Bityutskiy  * dirty by the re-name operation.
889dab4b4d2SArtem Bityutskiy  *
890dab4b4d2SArtem Bityutskiy  * Note, UBIFS aligns node lengths to 8-bytes boundary, so the requester has to
891dab4b4d2SArtem Bityutskiy  * make sure the amount of inode data which contribute to @new_ino_d and
892dab4b4d2SArtem Bityutskiy  * @dirtied_ino_d fields are aligned.
8931e51764aSArtem Bityutskiy  */
8941e51764aSArtem Bityutskiy struct ubifs_budget_req {
8951e51764aSArtem Bityutskiy 	unsigned int fast:1;
8961e51764aSArtem Bityutskiy 	unsigned int recalculate:1;
897547000daSArtem Bityutskiy #ifndef UBIFS_DEBUG
8981e51764aSArtem Bityutskiy 	unsigned int new_page:1;
8991e51764aSArtem Bityutskiy 	unsigned int dirtied_page:1;
9001e51764aSArtem Bityutskiy 	unsigned int new_dent:1;
9011e51764aSArtem Bityutskiy 	unsigned int mod_dent:1;
9021e51764aSArtem Bityutskiy 	unsigned int new_ino:1;
9031e51764aSArtem Bityutskiy 	unsigned int new_ino_d:13;
9041e51764aSArtem Bityutskiy 	unsigned int dirtied_ino:4;
9051e51764aSArtem Bityutskiy 	unsigned int dirtied_ino_d:15;
9061e51764aSArtem Bityutskiy #else
9071e51764aSArtem Bityutskiy 	/* Not bit-fields to check for overflows */
908547000daSArtem Bityutskiy 	unsigned int new_page;
909547000daSArtem Bityutskiy 	unsigned int dirtied_page;
910547000daSArtem Bityutskiy 	unsigned int new_dent;
911547000daSArtem Bityutskiy 	unsigned int mod_dent;
912547000daSArtem Bityutskiy 	unsigned int new_ino;
913547000daSArtem Bityutskiy 	unsigned int new_ino_d;
9141e51764aSArtem Bityutskiy 	unsigned int dirtied_ino;
9151e51764aSArtem Bityutskiy 	unsigned int dirtied_ino_d;
9161e51764aSArtem Bityutskiy #endif
9171e51764aSArtem Bityutskiy 	int idx_growth;
9181e51764aSArtem Bityutskiy 	int data_growth;
9191e51764aSArtem Bityutskiy 	int dd_growth;
9201e51764aSArtem Bityutskiy };
9211e51764aSArtem Bityutskiy 
9221e51764aSArtem Bityutskiy /**
9231e51764aSArtem Bityutskiy  * struct ubifs_orphan - stores the inode number of an orphan.
9241e51764aSArtem Bityutskiy  * @rb: rb-tree node of rb-tree of orphans sorted by inode number
9251e51764aSArtem Bityutskiy  * @list: list head of list of orphans in order added
9261e51764aSArtem Bityutskiy  * @new_list: list head of list of orphans added since the last commit
927988bec41SRichard Weinberger  * @child_list: list of xattr childs if this orphan hosts xattrs, list head
928988bec41SRichard Weinberger  * if this orphan is a xattr, not used otherwise.
9291e51764aSArtem Bityutskiy  * @cnext: next orphan to commit
9301e51764aSArtem Bityutskiy  * @dnext: next orphan to delete
9311e51764aSArtem Bityutskiy  * @inum: inode number
9321e51764aSArtem Bityutskiy  * @new: %1 => added since the last commit, otherwise %0
9332928f0d0SAdam Thomas  * @cmt: %1 => commit pending, otherwise %0
9348afd500cSAdam Thomas  * @del: %1 => delete pending, otherwise %0
9351e51764aSArtem Bityutskiy  */
9361e51764aSArtem Bityutskiy struct ubifs_orphan {
9371e51764aSArtem Bityutskiy 	struct rb_node rb;
9381e51764aSArtem Bityutskiy 	struct list_head list;
9391e51764aSArtem Bityutskiy 	struct list_head new_list;
940988bec41SRichard Weinberger 	struct list_head child_list;
9411e51764aSArtem Bityutskiy 	struct ubifs_orphan *cnext;
9421e51764aSArtem Bityutskiy 	struct ubifs_orphan *dnext;
9431e51764aSArtem Bityutskiy 	ino_t inum;
9442928f0d0SAdam Thomas 	unsigned new:1;
9452928f0d0SAdam Thomas 	unsigned cmt:1;
9468afd500cSAdam Thomas 	unsigned del:1;
9471e51764aSArtem Bityutskiy };
9481e51764aSArtem Bityutskiy 
9491e51764aSArtem Bityutskiy /**
9501e51764aSArtem Bityutskiy  * struct ubifs_mount_opts - UBIFS-specific mount options information.
9511e51764aSArtem Bityutskiy  * @unmount_mode: selected unmount mode (%0 default, %1 normal, %2 fast)
952d3f9db00SYannick Guerrini  * @bulk_read: enable/disable bulk-reads (%0 default, %1 disable, %2 enable)
953553dea4dSArtem Bityutskiy  * @chk_data_crc: enable/disable CRC data checking when reading data nodes
954d3f9db00SYannick Guerrini  *                (%0 default, %1 disable, %2 enable)
955553dea4dSArtem Bityutskiy  * @override_compr: override default compressor (%0 - do not override and use
956553dea4dSArtem Bityutskiy  *                  superblock compressor, %1 - override and use compressor
957553dea4dSArtem Bityutskiy  *                  specified in @compr_type)
958553dea4dSArtem Bityutskiy  * @compr_type: compressor type to override the superblock compressor with
959553dea4dSArtem Bityutskiy  *              (%UBIFS_COMPR_NONE, etc)
9601e51764aSArtem Bityutskiy  */
9611e51764aSArtem Bityutskiy struct ubifs_mount_opts {
9621e51764aSArtem Bityutskiy 	unsigned int unmount_mode:2;
9634793e7c5SAdrian Hunter 	unsigned int bulk_read:2;
9642953e73fSAdrian Hunter 	unsigned int chk_data_crc:2;
965553dea4dSArtem Bityutskiy 	unsigned int override_compr:1;
966553dea4dSArtem Bityutskiy 	unsigned int compr_type:2;
9671e51764aSArtem Bityutskiy };
9681e51764aSArtem Bityutskiy 
969b137545cSArtem Bityutskiy /**
970b137545cSArtem Bityutskiy  * struct ubifs_budg_info - UBIFS budgeting information.
971b137545cSArtem Bityutskiy  * @idx_growth: amount of bytes budgeted for index growth
972b137545cSArtem Bityutskiy  * @data_growth: amount of bytes budgeted for cached data
973b137545cSArtem Bityutskiy  * @dd_growth: amount of bytes budgeted for cached data that will make
974b137545cSArtem Bityutskiy  *             other data dirty
975b137545cSArtem Bityutskiy  * @uncommitted_idx: amount of bytes were budgeted for growth of the index, but
976b137545cSArtem Bityutskiy  *                   which still have to be taken into account because the index
977b137545cSArtem Bityutskiy  *                   has not been committed so far
978b137545cSArtem Bityutskiy  * @old_idx_sz: size of index on flash
979b137545cSArtem Bityutskiy  * @min_idx_lebs: minimum number of LEBs required for the index
980b137545cSArtem Bityutskiy  * @nospace: non-zero if the file-system does not have flash space (used as
981b137545cSArtem Bityutskiy  *           optimization)
982b137545cSArtem Bityutskiy  * @nospace_rp: the same as @nospace, but additionally means that even reserved
983b137545cSArtem Bityutskiy  *              pool is full
984d3f9db00SYannick Guerrini  * @page_budget: budget for a page (constant, never changed after mount)
985d3f9db00SYannick Guerrini  * @inode_budget: budget for an inode (constant, never changed after mount)
986d3f9db00SYannick Guerrini  * @dent_budget: budget for a directory entry (constant, never changed after
987b137545cSArtem Bityutskiy  *               mount)
988b137545cSArtem Bityutskiy  */
989b137545cSArtem Bityutskiy struct ubifs_budg_info {
990b137545cSArtem Bityutskiy 	long long idx_growth;
991b137545cSArtem Bityutskiy 	long long data_growth;
992b137545cSArtem Bityutskiy 	long long dd_growth;
993b137545cSArtem Bityutskiy 	long long uncommitted_idx;
994b137545cSArtem Bityutskiy 	unsigned long long old_idx_sz;
995b137545cSArtem Bityutskiy 	int min_idx_lebs;
996b137545cSArtem Bityutskiy 	unsigned int nospace:1;
997b137545cSArtem Bityutskiy 	unsigned int nospace_rp:1;
998b137545cSArtem Bityutskiy 	int page_budget;
999b137545cSArtem Bityutskiy 	int inode_budget;
1000b137545cSArtem Bityutskiy 	int dent_budget;
1001b137545cSArtem Bityutskiy };
1002b137545cSArtem Bityutskiy 
100317c2f9f8SArtem Bityutskiy struct ubifs_debug_info;
100417c2f9f8SArtem Bityutskiy 
10051e51764aSArtem Bityutskiy /**
10061e51764aSArtem Bityutskiy  * struct ubifs_info - UBIFS file-system description data structure
10071e51764aSArtem Bityutskiy  * (per-superblock).
10081e51764aSArtem Bityutskiy  * @vfs_sb: VFS @struct super_block object
1009fd615005SSascha Hauer  * @sup_node: The super block node as read from the device
10101e51764aSArtem Bityutskiy  *
10111e51764aSArtem Bityutskiy  * @highest_inum: highest used inode number
10121e51764aSArtem Bityutskiy  * @max_sqnum: current global sequence number
1013014eb04bSArtem Bityutskiy  * @cmt_no: commit number of the last successfully completed commit, protected
1014014eb04bSArtem Bityutskiy  *          by @commit_sem
101581ffa38eSAdrian Hunter  * @cnt_lock: protects @highest_inum and @max_sqnum counters
10161e51764aSArtem Bityutskiy  * @fmt_version: UBIFS on-flash format version
1017963f0cf6SArtem Bityutskiy  * @ro_compat_version: R/O compatibility version
10181e51764aSArtem Bityutskiy  * @uuid: UUID from super block
10191e51764aSArtem Bityutskiy  *
10201e51764aSArtem Bityutskiy  * @lhead_lnum: log head logical eraseblock number
10211e51764aSArtem Bityutskiy  * @lhead_offs: log head offset
10221e51764aSArtem Bityutskiy  * @ltail_lnum: log tail logical eraseblock number (offset is always 0)
10231e51764aSArtem Bityutskiy  * @log_mutex: protects the log, @lhead_lnum, @lhead_offs, @ltail_lnum, and
10241e51764aSArtem Bityutskiy  *             @bud_bytes
10251e51764aSArtem Bityutskiy  * @min_log_bytes: minimum required number of bytes in the log
10261e51764aSArtem Bityutskiy  * @cmt_bud_bytes: used during commit to temporarily amount of bytes in
10271e51764aSArtem Bityutskiy  *                 committed buds
10281e51764aSArtem Bityutskiy  *
10291e51764aSArtem Bityutskiy  * @buds: tree of all buds indexed by bud LEB number
10301e51764aSArtem Bityutskiy  * @bud_bytes: how many bytes of flash is used by buds
10311e51764aSArtem Bityutskiy  * @buds_lock: protects the @buds tree, @bud_bytes, and per-journal head bud
10321e51764aSArtem Bityutskiy  *             lists
10331e51764aSArtem Bityutskiy  * @jhead_cnt: count of journal heads
10341e51764aSArtem Bityutskiy  * @jheads: journal heads (head zero is base head)
10351e51764aSArtem Bityutskiy  * @max_bud_bytes: maximum number of bytes allowed in buds
10361e51764aSArtem Bityutskiy  * @bg_bud_bytes: number of bud bytes when background commit is initiated
10371e51764aSArtem Bityutskiy  * @old_buds: buds to be released after commit ends
10381e51764aSArtem Bityutskiy  * @max_bud_cnt: maximum number of buds
10391e51764aSArtem Bityutskiy  *
10401e51764aSArtem Bityutskiy  * @commit_sem: synchronizes committer with other processes
10411e51764aSArtem Bityutskiy  * @cmt_state: commit state
10421e51764aSArtem Bityutskiy  * @cs_lock: commit state lock
10431e51764aSArtem Bityutskiy  * @cmt_wq: wait queue to sleep on if the log is full and a commit is running
1044625bf371SArtem Bityutskiy  *
10451e51764aSArtem Bityutskiy  * @big_lpt: flag that LPT is too big to write whole during commit
10469f58d350SMatthew L. Creech  * @space_fixup: flag indicating that free space in LEBs needs to be cleaned up
1047d63d61c1SRichard Weinberger  * @double_hash: flag indicating that we can do lookups by hash
1048e021986eSRichard Weinberger  * @encrypted: flag indicating that this file system contains encrypted files
1049625bf371SArtem Bityutskiy  * @no_chk_data_crc: do not check CRCs when reading data nodes (except during
1050625bf371SArtem Bityutskiy  *                   recovery)
1051625bf371SArtem Bityutskiy  * @bulk_read: enable bulk-reads
1052a1dc080cSArtem Bityutskiy  * @default_compr: default compression algorithm (%UBIFS_COMPR_LZO, etc)
1053963f0cf6SArtem Bityutskiy  * @rw_incompat: the media is not R/W compatible
10542e52eb74SRichard Weinberger  * @assert_action: action to take when a ubifs_assert() fails
105549525e5eSSascha Hauer  * @authenticated: flag indigating the FS is mounted in authenticated mode
10561e51764aSArtem Bityutskiy  *
10571e51764aSArtem Bityutskiy  * @tnc_mutex: protects the Tree Node Cache (TNC), @zroot, @cnext, @enext, and
10581e51764aSArtem Bityutskiy  *             @calc_idx_sz
10591e51764aSArtem Bityutskiy  * @zroot: zbranch which points to the root index node and znode
10601e51764aSArtem Bityutskiy  * @cnext: next znode to commit
10611e51764aSArtem Bityutskiy  * @enext: next znode to commit to empty space
10621e51764aSArtem Bityutskiy  * @gap_lebs: array of LEBs used by the in-gaps commit method
10631e51764aSArtem Bityutskiy  * @cbuf: commit buffer
10641e51764aSArtem Bityutskiy  * @ileb_buf: buffer for commit in-the-gaps method
10651e51764aSArtem Bityutskiy  * @ileb_len: length of data in ileb_buf
10661e51764aSArtem Bityutskiy  * @ihead_lnum: LEB number of index head
10671e51764aSArtem Bityutskiy  * @ihead_offs: offset of index head
10681e51764aSArtem Bityutskiy  * @ilebs: pre-allocated index LEBs
10691e51764aSArtem Bityutskiy  * @ileb_cnt: number of pre-allocated index LEBs
10701e51764aSArtem Bityutskiy  * @ileb_nxt: next pre-allocated index LEBs
10711e51764aSArtem Bityutskiy  * @old_idx: tree of index nodes obsoleted since the last commit start
10721e51764aSArtem Bityutskiy  * @bottom_up_buf: a buffer which is used by 'dirty_cow_bottom_up()' in tnc.c
10731e51764aSArtem Bityutskiy  *
10741e51764aSArtem Bityutskiy  * @mst_node: master node
10751e51764aSArtem Bityutskiy  * @mst_offs: offset of valid master node
10763477d204SArtem Bityutskiy  *
10776c0c42cdSArtem Bityutskiy  * @max_bu_buf_len: maximum bulk-read buffer length
10783477d204SArtem Bityutskiy  * @bu_mutex: protects the pre-allocated bulk-read buffer and @c->bu
10793477d204SArtem Bityutskiy  * @bu: pre-allocated bulk-read information
10801e51764aSArtem Bityutskiy  *
1081d882962fSMatthew L. Creech  * @write_reserve_mutex: protects @write_reserve_buf
1082d882962fSMatthew L. Creech  * @write_reserve_buf: on the write path we allocate memory, which might
1083d882962fSMatthew L. Creech  *                     sometimes be unavailable, in which case we use this
1084d882962fSMatthew L. Creech  *                     write reserve buffer
1085d882962fSMatthew L. Creech  *
10861e51764aSArtem Bityutskiy  * @log_lebs: number of logical eraseblocks in the log
10871e51764aSArtem Bityutskiy  * @log_bytes: log size in bytes
10881e51764aSArtem Bityutskiy  * @log_last: last LEB of the log
10891e51764aSArtem Bityutskiy  * @lpt_lebs: number of LEBs used for lprops table
10901e51764aSArtem Bityutskiy  * @lpt_first: first LEB of the lprops table area
10911e51764aSArtem Bityutskiy  * @lpt_last: last LEB of the lprops table area
10921e51764aSArtem Bityutskiy  * @orph_lebs: number of LEBs used for the orphan area
10931e51764aSArtem Bityutskiy  * @orph_first: first LEB of the orphan area
10941e51764aSArtem Bityutskiy  * @orph_last: last LEB of the orphan area
10951e51764aSArtem Bityutskiy  * @main_lebs: count of LEBs in the main area
10961e51764aSArtem Bityutskiy  * @main_first: first LEB of the main area
10971e51764aSArtem Bityutskiy  * @main_bytes: main area size in bytes
10981e51764aSArtem Bityutskiy  *
10991e51764aSArtem Bityutskiy  * @key_hash_type: type of the key hash
11001e51764aSArtem Bityutskiy  * @key_hash: direntry key hash function
11011e51764aSArtem Bityutskiy  * @key_fmt: key format
11021e51764aSArtem Bityutskiy  * @key_len: key length
110349525e5eSSascha Hauer  * @hash_len: The length of the index node hashes
11041e51764aSArtem Bityutskiy  * @fanout: fanout of the index tree (number of links per indexing node)
11051e51764aSArtem Bityutskiy  *
11061e51764aSArtem Bityutskiy  * @min_io_size: minimal input/output unit size
11071e51764aSArtem Bityutskiy  * @min_io_shift: number of bits in @min_io_size minus one
11083e8e2e0cSArtem Bityutskiy  * @max_write_size: maximum amount of bytes the underlying flash can write at a
11093e8e2e0cSArtem Bityutskiy  *                  time (MTD write buffer size)
11103e8e2e0cSArtem Bityutskiy  * @max_write_shift: number of bits in @max_write_size minus one
11111e51764aSArtem Bityutskiy  * @leb_size: logical eraseblock size in bytes
1112ca2ec61dSArtem Bityutskiy  * @leb_start: starting offset of logical eraseblocks within physical
1113ca2ec61dSArtem Bityutskiy  *             eraseblocks
11141e51764aSArtem Bityutskiy  * @half_leb_size: half LEB size
1115fb1cd01aSArtem Bityutskiy  * @idx_leb_size: how many bytes of an LEB are effectively available when it is
1116fb1cd01aSArtem Bityutskiy  *                used to store indexing nodes (@leb_size - @max_idx_node_sz)
11171e51764aSArtem Bityutskiy  * @leb_cnt: count of logical eraseblocks
11181e51764aSArtem Bityutskiy  * @max_leb_cnt: maximum count of logical eraseblocks
11191e51764aSArtem Bityutskiy  * @old_leb_cnt: count of logical eraseblocks before re-size
11201e51764aSArtem Bityutskiy  * @ro_media: the underlying UBI volume is read-only
11212ef13294SArtem Bityutskiy  * @ro_mount: the file-system was mounted as read-only
11222680d722SArtem Bityutskiy  * @ro_error: UBIFS switched to R/O mode because an error happened
11231e51764aSArtem Bityutskiy  *
11241e51764aSArtem Bityutskiy  * @dirty_pg_cnt: number of dirty pages (not used)
11251e51764aSArtem Bityutskiy  * @dirty_zn_cnt: number of dirty znodes
11261e51764aSArtem Bityutskiy  * @clean_zn_cnt: number of clean znodes
11271e51764aSArtem Bityutskiy  *
1128b137545cSArtem Bityutskiy  * @space_lock: protects @bi and @lst
1129b137545cSArtem Bityutskiy  * @lst: lprops statistics
1130b137545cSArtem Bityutskiy  * @bi: budgeting information
11311e51764aSArtem Bityutskiy  * @calc_idx_sz: temporary variable which is used to calculate new index size
11321e51764aSArtem Bityutskiy  *               (contains accurate new index size at end of TNC commit start)
11331e51764aSArtem Bityutskiy  *
11341e51764aSArtem Bityutskiy  * @ref_node_alsz: size of the LEB reference node aligned to the min. flash
11351e51764aSArtem Bityutskiy  *                 I/O unit
11361e51764aSArtem Bityutskiy  * @mst_node_alsz: master node aligned size
11371e51764aSArtem Bityutskiy  * @min_idx_node_sz: minimum indexing node aligned on 8-bytes boundary
11381e51764aSArtem Bityutskiy  * @max_idx_node_sz: maximum indexing node aligned on 8-bytes boundary
11391e51764aSArtem Bityutskiy  * @max_inode_sz: maximum possible inode size in bytes
11401e51764aSArtem Bityutskiy  * @max_znode_sz: size of znode in bytes
11419bbb5726SArtem Bityutskiy  *
11429bbb5726SArtem Bityutskiy  * @leb_overhead: how many bytes are wasted in an LEB when it is filled with
11439bbb5726SArtem Bityutskiy  *                data nodes of maximum size - used in free space reporting
11441e51764aSArtem Bityutskiy  * @dead_wm: LEB dead space watermark
11451e51764aSArtem Bityutskiy  * @dark_wm: LEB dark space watermark
11461e51764aSArtem Bityutskiy  * @block_cnt: count of 4KiB blocks on the FS
11471e51764aSArtem Bityutskiy  *
11481e51764aSArtem Bityutskiy  * @ranges: UBIFS node length ranges
11491e51764aSArtem Bityutskiy  * @ubi: UBI volume descriptor
11501e51764aSArtem Bityutskiy  * @di: UBI device information
11511e51764aSArtem Bityutskiy  * @vi: UBI volume information
11521e51764aSArtem Bityutskiy  *
11531e51764aSArtem Bityutskiy  * @orph_tree: rb-tree of orphan inode numbers
11541e51764aSArtem Bityutskiy  * @orph_list: list of orphan inode numbers in order added
11551e51764aSArtem Bityutskiy  * @orph_new: list of orphan inode numbers added since last commit
11561e51764aSArtem Bityutskiy  * @orph_cnext: next orphan to commit
11571e51764aSArtem Bityutskiy  * @orph_dnext: next orphan to delete
11581e51764aSArtem Bityutskiy  * @orphan_lock: lock for orph_tree and orph_new
11591e51764aSArtem Bityutskiy  * @orph_buf: buffer for orphan nodes
11601e51764aSArtem Bityutskiy  * @new_orphans: number of orphans since last commit
11611e51764aSArtem Bityutskiy  * @cmt_orphans: number of orphans being committed
11621e51764aSArtem Bityutskiy  * @tot_orphans: number of orphans in the rb_tree
11631e51764aSArtem Bityutskiy  * @max_orphans: maximum number of orphans allowed
11641e51764aSArtem Bityutskiy  * @ohead_lnum: orphan head LEB number
11651e51764aSArtem Bityutskiy  * @ohead_offs: orphan head offset
11661e51764aSArtem Bityutskiy  * @no_orphs: non-zero if there are no orphans
11671e51764aSArtem Bityutskiy  *
11681e51764aSArtem Bityutskiy  * @bgt: UBIFS background thread
11691e51764aSArtem Bityutskiy  * @bgt_name: background thread name
11701e51764aSArtem Bityutskiy  * @need_bgt: if background thread should run
11711e51764aSArtem Bityutskiy  * @need_wbuf_sync: if write-buffers have to be synchronized
11721e51764aSArtem Bityutskiy  *
11731e51764aSArtem Bityutskiy  * @gc_lnum: LEB number used for garbage collection
11741e51764aSArtem Bityutskiy  * @sbuf: a buffer of LEB size used by GC and replay for scanning
11751e51764aSArtem Bityutskiy  * @idx_gc: list of index LEBs that have been garbage collected
11761e51764aSArtem Bityutskiy  * @idx_gc_cnt: number of elements on the idx_gc list
1177601c0bc4SAdrian Hunter  * @gc_seq: incremented for every non-index LEB garbage collected
1178601c0bc4SAdrian Hunter  * @gced_lnum: last non-index LEB that was garbage collected
11791e51764aSArtem Bityutskiy  *
11801e51764aSArtem Bityutskiy  * @infos_list: links all 'ubifs_info' objects
11811e51764aSArtem Bityutskiy  * @umount_mutex: serializes shrinker and un-mount
11821e51764aSArtem Bityutskiy  * @shrinker_run_no: shrinker run number
11831e51764aSArtem Bityutskiy  *
11841e51764aSArtem Bityutskiy  * @space_bits: number of bits needed to record free or dirty space
11851e51764aSArtem Bityutskiy  * @lpt_lnum_bits: number of bits needed to record a LEB number in the LPT
11861e51764aSArtem Bityutskiy  * @lpt_offs_bits: number of bits needed to record an offset in the LPT
11871e51764aSArtem Bityutskiy  * @lpt_spc_bits: number of bits needed to space in the LPT
11881e51764aSArtem Bityutskiy  * @pcnt_bits: number of bits needed to record pnode or nnode number
11891e51764aSArtem Bityutskiy  * @lnum_bits: number of bits needed to record LEB number
11901e51764aSArtem Bityutskiy  * @nnode_sz: size of on-flash nnode
11911e51764aSArtem Bityutskiy  * @pnode_sz: size of on-flash pnode
11921e51764aSArtem Bityutskiy  * @ltab_sz: size of on-flash LPT lprops table
11931e51764aSArtem Bityutskiy  * @lsave_sz: size of on-flash LPT save table
11941e51764aSArtem Bityutskiy  * @pnode_cnt: number of pnodes
11951e51764aSArtem Bityutskiy  * @nnode_cnt: number of nnodes
11961e51764aSArtem Bityutskiy  * @lpt_hght: height of the LPT
11971e51764aSArtem Bityutskiy  * @pnodes_have: number of pnodes in memory
11981e51764aSArtem Bityutskiy  *
11991e51764aSArtem Bityutskiy  * @lp_mutex: protects lprops table and all the other lprops-related fields
12001e51764aSArtem Bityutskiy  * @lpt_lnum: LEB number of the root nnode of the LPT
12011e51764aSArtem Bityutskiy  * @lpt_offs: offset of the root nnode of the LPT
12021e51764aSArtem Bityutskiy  * @nhead_lnum: LEB number of LPT head
12031e51764aSArtem Bityutskiy  * @nhead_offs: offset of LPT head
12041e51764aSArtem Bityutskiy  * @lpt_drty_flgs: dirty flags for LPT special nodes e.g. ltab
12051e51764aSArtem Bityutskiy  * @dirty_nn_cnt: number of dirty nnodes
12061e51764aSArtem Bityutskiy  * @dirty_pn_cnt: number of dirty pnodes
120773944a6dSAdrian Hunter  * @check_lpt_free: flag that indicates LPT GC may be needed
12081e51764aSArtem Bityutskiy  * @lpt_sz: LPT size
12091e51764aSArtem Bityutskiy  * @lpt_nod_buf: buffer for an on-flash nnode or pnode
12101e51764aSArtem Bityutskiy  * @lpt_buf: buffer of LEB size used by LPT
12111e51764aSArtem Bityutskiy  * @nroot: address in memory of the root nnode of the LPT
12121e51764aSArtem Bityutskiy  * @lpt_cnext: next LPT node to commit
12131e51764aSArtem Bityutskiy  * @lpt_heap: array of heaps of categorized lprops
12141e51764aSArtem Bityutskiy  * @dirty_idx: a (reverse sorted) copy of the LPROPS_DIRTY_IDX heap as at
12151e51764aSArtem Bityutskiy  *             previous commit start
12161e51764aSArtem Bityutskiy  * @uncat_list: list of un-categorized LEBs
12171e51764aSArtem Bityutskiy  * @empty_list: list of empty LEBs
1218fb1cd01aSArtem Bityutskiy  * @freeable_list: list of freeable non-index LEBs (free + dirty == @leb_size)
1219fb1cd01aSArtem Bityutskiy  * @frdi_idx_list: list of freeable index LEBs (free + dirty == @leb_size)
12201e51764aSArtem Bityutskiy  * @freeable_cnt: number of freeable LEBs in @freeable_list
122198a1eebdSArtem Bityutskiy  * @in_a_category_cnt: count of lprops which are in a certain category, which
122298a1eebdSArtem Bityutskiy  *                     basically meants that they were loaded from the flash
12231e51764aSArtem Bityutskiy  *
12241e51764aSArtem Bityutskiy  * @ltab_lnum: LEB number of LPT's own lprops table
12251e51764aSArtem Bityutskiy  * @ltab_offs: offset of LPT's own lprops table
12261e51764aSArtem Bityutskiy  * @ltab: LPT's own lprops table
12271e51764aSArtem Bityutskiy  * @ltab_cmt: LPT's own lprops table (commit copy)
12281e51764aSArtem Bityutskiy  * @lsave_cnt: number of LEB numbers in LPT's save table
12291e51764aSArtem Bityutskiy  * @lsave_lnum: LEB number of LPT's save table
12301e51764aSArtem Bityutskiy  * @lsave_offs: offset of LPT's save table
12311e51764aSArtem Bityutskiy  * @lsave: LPT's save table
12321e51764aSArtem Bityutskiy  * @lscan_lnum: LEB number of last LPT scan
12331e51764aSArtem Bityutskiy  *
12341e51764aSArtem Bityutskiy  * @rp_size: size of the reserved pool in bytes
12351e51764aSArtem Bityutskiy  * @report_rp_size: size of the reserved pool reported to user-space
12361e51764aSArtem Bityutskiy  * @rp_uid: reserved pool user ID
12371e51764aSArtem Bityutskiy  * @rp_gid: reserved pool group ID
12381e51764aSArtem Bityutskiy  *
123949525e5eSSascha Hauer  * @hash_tfm: the hash transformation used for hashing nodes
124049525e5eSSascha Hauer  * @hmac_tfm: the HMAC transformation for this filesystem
124149525e5eSSascha Hauer  * @hmac_desc_len: length of the HMAC used for authentication
124249525e5eSSascha Hauer  * @auth_key_name: the authentication key name
124349525e5eSSascha Hauer  * @auth_hash_name: the name of the hash algorithm used for authentication
124449525e5eSSascha Hauer  * @auth_hash_algo: the authentication hash used for this fs
12456a98bc46SSascha Hauer  * @log_hash: the log hash from the commit start node up to the latest reference
12466a98bc46SSascha Hauer  *            node.
124749525e5eSSascha Hauer  *
1248d8cdda3eSArtem Bityutskiy  * @empty: %1 if the UBI device is empty
1249d8cdda3eSArtem Bityutskiy  * @need_recovery: %1 if the file-system needs recovery
1250d8cdda3eSArtem Bityutskiy  * @replaying: %1 during journal replay
125118d1d7fbSArtem Bityutskiy  * @mounting: %1 while mounting
12521751e8a6SLinus Torvalds  * @probing: %1 while attempting to mount if SB_SILENT mount flag is set
1253d8cdda3eSArtem Bityutskiy  * @remounting_rw: %1 while re-mounting from R/O mode to R/W mode
12541e51764aSArtem Bityutskiy  * @replay_list: temporary list used during journal replay
12551e51764aSArtem Bityutskiy  * @replay_buds: list of buds to replay
12561e51764aSArtem Bityutskiy  * @cs_sqnum: sequence number of first node in the log (commit start node)
12572ef13294SArtem Bityutskiy  * @unclean_leb_list: LEBs to recover when re-mounting R/O mounted FS to R/W
12582ef13294SArtem Bityutskiy  *                    mode
12592ef13294SArtem Bityutskiy  * @rcvrd_mst_node: recovered master node to write when re-mounting R/O mounted
12602ef13294SArtem Bityutskiy  *                  FS to R/W mode
12611e51764aSArtem Bityutskiy  * @size_tree: inode size information for recovery
12621e51764aSArtem Bityutskiy  * @mount_opts: UBIFS-specific mount options
12631e51764aSArtem Bityutskiy  *
126417c2f9f8SArtem Bityutskiy  * @dbg: debugging-related information
12651e51764aSArtem Bityutskiy  */
12661e51764aSArtem Bityutskiy struct ubifs_info {
12671e51764aSArtem Bityutskiy 	struct super_block *vfs_sb;
1268fd615005SSascha Hauer 	struct ubifs_sb_node *sup_node;
12691e51764aSArtem Bityutskiy 
12701e51764aSArtem Bityutskiy 	ino_t highest_inum;
12711e51764aSArtem Bityutskiy 	unsigned long long max_sqnum;
12721e51764aSArtem Bityutskiy 	unsigned long long cmt_no;
12731e51764aSArtem Bityutskiy 	spinlock_t cnt_lock;
12741e51764aSArtem Bityutskiy 	int fmt_version;
1275963f0cf6SArtem Bityutskiy 	int ro_compat_version;
12761e51764aSArtem Bityutskiy 	unsigned char uuid[16];
12771e51764aSArtem Bityutskiy 
12781e51764aSArtem Bityutskiy 	int lhead_lnum;
12791e51764aSArtem Bityutskiy 	int lhead_offs;
12801e51764aSArtem Bityutskiy 	int ltail_lnum;
12811e51764aSArtem Bityutskiy 	struct mutex log_mutex;
12821e51764aSArtem Bityutskiy 	int min_log_bytes;
12831e51764aSArtem Bityutskiy 	long long cmt_bud_bytes;
12841e51764aSArtem Bityutskiy 
12851e51764aSArtem Bityutskiy 	struct rb_root buds;
12861e51764aSArtem Bityutskiy 	long long bud_bytes;
12871e51764aSArtem Bityutskiy 	spinlock_t buds_lock;
12881e51764aSArtem Bityutskiy 	int jhead_cnt;
12891e51764aSArtem Bityutskiy 	struct ubifs_jhead *jheads;
12901e51764aSArtem Bityutskiy 	long long max_bud_bytes;
12911e51764aSArtem Bityutskiy 	long long bg_bud_bytes;
12921e51764aSArtem Bityutskiy 	struct list_head old_buds;
12931e51764aSArtem Bityutskiy 	int max_bud_cnt;
12941e51764aSArtem Bityutskiy 
12951e51764aSArtem Bityutskiy 	struct rw_semaphore commit_sem;
12961e51764aSArtem Bityutskiy 	int cmt_state;
12971e51764aSArtem Bityutskiy 	spinlock_t cs_lock;
12981e51764aSArtem Bityutskiy 	wait_queue_head_t cmt_wq;
1299625bf371SArtem Bityutskiy 
13001e51764aSArtem Bityutskiy 	unsigned int big_lpt:1;
13019f58d350SMatthew L. Creech 	unsigned int space_fixup:1;
1302d63d61c1SRichard Weinberger 	unsigned int double_hash:1;
1303e021986eSRichard Weinberger 	unsigned int encrypted:1;
1304625bf371SArtem Bityutskiy 	unsigned int no_chk_data_crc:1;
1305625bf371SArtem Bityutskiy 	unsigned int bulk_read:1;
1306a1dc080cSArtem Bityutskiy 	unsigned int default_compr:2;
1307963f0cf6SArtem Bityutskiy 	unsigned int rw_incompat:1;
13082e52eb74SRichard Weinberger 	unsigned int assert_action:2;
130949525e5eSSascha Hauer 	unsigned int authenticated:1;
13101e51764aSArtem Bityutskiy 
13111e51764aSArtem Bityutskiy 	struct mutex tnc_mutex;
13121e51764aSArtem Bityutskiy 	struct ubifs_zbranch zroot;
13131e51764aSArtem Bityutskiy 	struct ubifs_znode *cnext;
13141e51764aSArtem Bityutskiy 	struct ubifs_znode *enext;
13151e51764aSArtem Bityutskiy 	int *gap_lebs;
13161e51764aSArtem Bityutskiy 	void *cbuf;
13171e51764aSArtem Bityutskiy 	void *ileb_buf;
13181e51764aSArtem Bityutskiy 	int ileb_len;
13191e51764aSArtem Bityutskiy 	int ihead_lnum;
13201e51764aSArtem Bityutskiy 	int ihead_offs;
13211e51764aSArtem Bityutskiy 	int *ilebs;
13221e51764aSArtem Bityutskiy 	int ileb_cnt;
13231e51764aSArtem Bityutskiy 	int ileb_nxt;
13241e51764aSArtem Bityutskiy 	struct rb_root old_idx;
13251e51764aSArtem Bityutskiy 	int *bottom_up_buf;
13261e51764aSArtem Bityutskiy 
13271e51764aSArtem Bityutskiy 	struct ubifs_mst_node *mst_node;
13281e51764aSArtem Bityutskiy 	int mst_offs;
13293477d204SArtem Bityutskiy 
13306c0c42cdSArtem Bityutskiy 	int max_bu_buf_len;
13313477d204SArtem Bityutskiy 	struct mutex bu_mutex;
13323477d204SArtem Bityutskiy 	struct bu_info bu;
13331e51764aSArtem Bityutskiy 
1334d882962fSMatthew L. Creech 	struct mutex write_reserve_mutex;
1335d882962fSMatthew L. Creech 	void *write_reserve_buf;
1336d882962fSMatthew L. Creech 
13371e51764aSArtem Bityutskiy 	int log_lebs;
13381e51764aSArtem Bityutskiy 	long long log_bytes;
13391e51764aSArtem Bityutskiy 	int log_last;
13401e51764aSArtem Bityutskiy 	int lpt_lebs;
13411e51764aSArtem Bityutskiy 	int lpt_first;
13421e51764aSArtem Bityutskiy 	int lpt_last;
13431e51764aSArtem Bityutskiy 	int orph_lebs;
13441e51764aSArtem Bityutskiy 	int orph_first;
13451e51764aSArtem Bityutskiy 	int orph_last;
13461e51764aSArtem Bityutskiy 	int main_lebs;
13471e51764aSArtem Bityutskiy 	int main_first;
13481e51764aSArtem Bityutskiy 	long long main_bytes;
13491e51764aSArtem Bityutskiy 
13501e51764aSArtem Bityutskiy 	uint8_t key_hash_type;
13511e51764aSArtem Bityutskiy 	uint32_t (*key_hash)(const char *str, int len);
13521e51764aSArtem Bityutskiy 	int key_fmt;
13531e51764aSArtem Bityutskiy 	int key_len;
135449525e5eSSascha Hauer 	int hash_len;
13551e51764aSArtem Bityutskiy 	int fanout;
13561e51764aSArtem Bityutskiy 
13571e51764aSArtem Bityutskiy 	int min_io_size;
13581e51764aSArtem Bityutskiy 	int min_io_shift;
13593e8e2e0cSArtem Bityutskiy 	int max_write_size;
13603e8e2e0cSArtem Bityutskiy 	int max_write_shift;
13611e51764aSArtem Bityutskiy 	int leb_size;
1362ca2ec61dSArtem Bityutskiy 	int leb_start;
13631e51764aSArtem Bityutskiy 	int half_leb_size;
1364fb1cd01aSArtem Bityutskiy 	int idx_leb_size;
13651e51764aSArtem Bityutskiy 	int leb_cnt;
13661e51764aSArtem Bityutskiy 	int max_leb_cnt;
13671e51764aSArtem Bityutskiy 	int old_leb_cnt;
13682680d722SArtem Bityutskiy 	unsigned int ro_media:1;
13692ef13294SArtem Bityutskiy 	unsigned int ro_mount:1;
13702680d722SArtem Bityutskiy 	unsigned int ro_error:1;
13711e51764aSArtem Bityutskiy 
13721e51764aSArtem Bityutskiy 	atomic_long_t dirty_pg_cnt;
13731e51764aSArtem Bityutskiy 	atomic_long_t dirty_zn_cnt;
13741e51764aSArtem Bityutskiy 	atomic_long_t clean_zn_cnt;
13751e51764aSArtem Bityutskiy 
13761e51764aSArtem Bityutskiy 	spinlock_t space_lock;
13771e51764aSArtem Bityutskiy 	struct ubifs_lp_stats lst;
1378b137545cSArtem Bityutskiy 	struct ubifs_budg_info bi;
1379b137545cSArtem Bityutskiy 	unsigned long long calc_idx_sz;
13801e51764aSArtem Bityutskiy 
13811e51764aSArtem Bityutskiy 	int ref_node_alsz;
13821e51764aSArtem Bityutskiy 	int mst_node_alsz;
13831e51764aSArtem Bityutskiy 	int min_idx_node_sz;
13841e51764aSArtem Bityutskiy 	int max_idx_node_sz;
13851e51764aSArtem Bityutskiy 	long long max_inode_sz;
13861e51764aSArtem Bityutskiy 	int max_znode_sz;
13879bbb5726SArtem Bityutskiy 
13889bbb5726SArtem Bityutskiy 	int leb_overhead;
13891e51764aSArtem Bityutskiy 	int dead_wm;
13901e51764aSArtem Bityutskiy 	int dark_wm;
13911e51764aSArtem Bityutskiy 	int block_cnt;
13921e51764aSArtem Bityutskiy 
13931e51764aSArtem Bityutskiy 	struct ubifs_node_range ranges[UBIFS_NODE_TYPES_CNT];
13941e51764aSArtem Bityutskiy 	struct ubi_volume_desc *ubi;
13951e51764aSArtem Bityutskiy 	struct ubi_device_info di;
13961e51764aSArtem Bityutskiy 	struct ubi_volume_info vi;
13971e51764aSArtem Bityutskiy 
13981e51764aSArtem Bityutskiy 	struct rb_root orph_tree;
13991e51764aSArtem Bityutskiy 	struct list_head orph_list;
14001e51764aSArtem Bityutskiy 	struct list_head orph_new;
14011e51764aSArtem Bityutskiy 	struct ubifs_orphan *orph_cnext;
14021e51764aSArtem Bityutskiy 	struct ubifs_orphan *orph_dnext;
14031e51764aSArtem Bityutskiy 	spinlock_t orphan_lock;
14041e51764aSArtem Bityutskiy 	void *orph_buf;
14051e51764aSArtem Bityutskiy 	int new_orphans;
14061e51764aSArtem Bityutskiy 	int cmt_orphans;
14071e51764aSArtem Bityutskiy 	int tot_orphans;
14081e51764aSArtem Bityutskiy 	int max_orphans;
14091e51764aSArtem Bityutskiy 	int ohead_lnum;
14101e51764aSArtem Bityutskiy 	int ohead_offs;
14111e51764aSArtem Bityutskiy 	int no_orphs;
14121e51764aSArtem Bityutskiy 
14131e51764aSArtem Bityutskiy 	struct task_struct *bgt;
14141e51764aSArtem Bityutskiy 	char bgt_name[sizeof(BGT_NAME_PATTERN) + 9];
14151e51764aSArtem Bityutskiy 	int need_bgt;
14161e51764aSArtem Bityutskiy 	int need_wbuf_sync;
14171e51764aSArtem Bityutskiy 
14181e51764aSArtem Bityutskiy 	int gc_lnum;
14191e51764aSArtem Bityutskiy 	void *sbuf;
14201e51764aSArtem Bityutskiy 	struct list_head idx_gc;
14211e51764aSArtem Bityutskiy 	int idx_gc_cnt;
1422f92b9826SArtem Bityutskiy 	int gc_seq;
1423f92b9826SArtem Bityutskiy 	int gced_lnum;
14241e51764aSArtem Bityutskiy 
14251e51764aSArtem Bityutskiy 	struct list_head infos_list;
14261e51764aSArtem Bityutskiy 	struct mutex umount_mutex;
14271e51764aSArtem Bityutskiy 	unsigned int shrinker_run_no;
14281e51764aSArtem Bityutskiy 
14291e51764aSArtem Bityutskiy 	int space_bits;
14301e51764aSArtem Bityutskiy 	int lpt_lnum_bits;
14311e51764aSArtem Bityutskiy 	int lpt_offs_bits;
14321e51764aSArtem Bityutskiy 	int lpt_spc_bits;
14331e51764aSArtem Bityutskiy 	int pcnt_bits;
14341e51764aSArtem Bityutskiy 	int lnum_bits;
14351e51764aSArtem Bityutskiy 	int nnode_sz;
14361e51764aSArtem Bityutskiy 	int pnode_sz;
14371e51764aSArtem Bityutskiy 	int ltab_sz;
14381e51764aSArtem Bityutskiy 	int lsave_sz;
14391e51764aSArtem Bityutskiy 	int pnode_cnt;
14401e51764aSArtem Bityutskiy 	int nnode_cnt;
14411e51764aSArtem Bityutskiy 	int lpt_hght;
14421e51764aSArtem Bityutskiy 	int pnodes_have;
14431e51764aSArtem Bityutskiy 
14441e51764aSArtem Bityutskiy 	struct mutex lp_mutex;
14451e51764aSArtem Bityutskiy 	int lpt_lnum;
14461e51764aSArtem Bityutskiy 	int lpt_offs;
14471e51764aSArtem Bityutskiy 	int nhead_lnum;
14481e51764aSArtem Bityutskiy 	int nhead_offs;
14491e51764aSArtem Bityutskiy 	int lpt_drty_flgs;
14501e51764aSArtem Bityutskiy 	int dirty_nn_cnt;
14511e51764aSArtem Bityutskiy 	int dirty_pn_cnt;
145273944a6dSAdrian Hunter 	int check_lpt_free;
14531e51764aSArtem Bityutskiy 	long long lpt_sz;
14541e51764aSArtem Bityutskiy 	void *lpt_nod_buf;
14551e51764aSArtem Bityutskiy 	void *lpt_buf;
14561e51764aSArtem Bityutskiy 	struct ubifs_nnode *nroot;
14571e51764aSArtem Bityutskiy 	struct ubifs_cnode *lpt_cnext;
14581e51764aSArtem Bityutskiy 	struct ubifs_lpt_heap lpt_heap[LPROPS_HEAP_CNT];
14591e51764aSArtem Bityutskiy 	struct ubifs_lpt_heap dirty_idx;
14601e51764aSArtem Bityutskiy 	struct list_head uncat_list;
14611e51764aSArtem Bityutskiy 	struct list_head empty_list;
14621e51764aSArtem Bityutskiy 	struct list_head freeable_list;
14631e51764aSArtem Bityutskiy 	struct list_head frdi_idx_list;
14641e51764aSArtem Bityutskiy 	int freeable_cnt;
146598a1eebdSArtem Bityutskiy 	int in_a_category_cnt;
14661e51764aSArtem Bityutskiy 
14671e51764aSArtem Bityutskiy 	int ltab_lnum;
14681e51764aSArtem Bityutskiy 	int ltab_offs;
14691e51764aSArtem Bityutskiy 	struct ubifs_lpt_lprops *ltab;
14701e51764aSArtem Bityutskiy 	struct ubifs_lpt_lprops *ltab_cmt;
14711e51764aSArtem Bityutskiy 	int lsave_cnt;
14721e51764aSArtem Bityutskiy 	int lsave_lnum;
14731e51764aSArtem Bityutskiy 	int lsave_offs;
14741e51764aSArtem Bityutskiy 	int *lsave;
14751e51764aSArtem Bityutskiy 	int lscan_lnum;
14761e51764aSArtem Bityutskiy 
14771e51764aSArtem Bityutskiy 	long long rp_size;
14781e51764aSArtem Bityutskiy 	long long report_rp_size;
147939241bebSEric W. Biederman 	kuid_t rp_uid;
148039241bebSEric W. Biederman 	kgid_t rp_gid;
14811e51764aSArtem Bityutskiy 
148249525e5eSSascha Hauer 	struct crypto_shash *hash_tfm;
148349525e5eSSascha Hauer 	struct crypto_shash *hmac_tfm;
148449525e5eSSascha Hauer 	int hmac_desc_len;
148549525e5eSSascha Hauer 	char *auth_key_name;
148649525e5eSSascha Hauer 	char *auth_hash_name;
148749525e5eSSascha Hauer 	enum hash_algo auth_hash_algo;
148849525e5eSSascha Hauer 
14896a98bc46SSascha Hauer 	struct shash_desc *log_hash;
14906a98bc46SSascha Hauer 
14911e51764aSArtem Bityutskiy 	/* The below fields are used only during mounting and re-mounting */
1492d8cdda3eSArtem Bityutskiy 	unsigned int empty:1;
1493d8cdda3eSArtem Bityutskiy 	unsigned int need_recovery:1;
1494d8cdda3eSArtem Bityutskiy 	unsigned int replaying:1;
149518d1d7fbSArtem Bityutskiy 	unsigned int mounting:1;
1496d8cdda3eSArtem Bityutskiy 	unsigned int remounting_rw:1;
149790bea5a3SDaniel Golle 	unsigned int probing:1;
14981e51764aSArtem Bityutskiy 	struct list_head replay_list;
14991e51764aSArtem Bityutskiy 	struct list_head replay_buds;
15001e51764aSArtem Bityutskiy 	unsigned long long cs_sqnum;
15011e51764aSArtem Bityutskiy 	struct list_head unclean_leb_list;
15021e51764aSArtem Bityutskiy 	struct ubifs_mst_node *rcvrd_mst_node;
15031e51764aSArtem Bityutskiy 	struct rb_root size_tree;
15041e51764aSArtem Bityutskiy 	struct ubifs_mount_opts mount_opts;
15051e51764aSArtem Bityutskiy 
150617c2f9f8SArtem Bityutskiy 	struct ubifs_debug_info *dbg;
15071e51764aSArtem Bityutskiy };
15081e51764aSArtem Bityutskiy 
15091e51764aSArtem Bityutskiy extern struct list_head ubifs_infos;
15101e51764aSArtem Bityutskiy extern spinlock_t ubifs_infos_lock;
15111e51764aSArtem Bityutskiy extern atomic_long_t ubifs_clean_zn_cnt;
1512e8b81566SArtem Bityutskiy extern const struct super_operations ubifs_super_operations;
1513e8b81566SArtem Bityutskiy extern const struct address_space_operations ubifs_file_address_operations;
1514e8b81566SArtem Bityutskiy extern const struct file_operations ubifs_file_operations;
1515e8b81566SArtem Bityutskiy extern const struct inode_operations ubifs_file_inode_operations;
1516e8b81566SArtem Bityutskiy extern const struct file_operations ubifs_dir_operations;
1517e8b81566SArtem Bityutskiy extern const struct inode_operations ubifs_dir_inode_operations;
1518e8b81566SArtem Bityutskiy extern const struct inode_operations ubifs_symlink_inode_operations;
15191e51764aSArtem Bityutskiy extern struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
15201e51764aSArtem Bityutskiy 
152149525e5eSSascha Hauer /* auth.c */
152249525e5eSSascha Hauer static inline int ubifs_authenticated(const struct ubifs_info *c)
152349525e5eSSascha Hauer {
152449525e5eSSascha Hauer 	return (IS_ENABLED(CONFIG_UBIFS_FS_AUTHENTICATION)) && c->authenticated;
152549525e5eSSascha Hauer }
152649525e5eSSascha Hauer 
152749525e5eSSascha Hauer struct shash_desc *__ubifs_hash_get_desc(const struct ubifs_info *c);
152849525e5eSSascha Hauer static inline struct shash_desc *ubifs_hash_get_desc(const struct ubifs_info *c)
152949525e5eSSascha Hauer {
153049525e5eSSascha Hauer 	return ubifs_authenticated(c) ? __ubifs_hash_get_desc(c) : NULL;
153149525e5eSSascha Hauer }
153249525e5eSSascha Hauer 
153349525e5eSSascha Hauer static inline int ubifs_shash_init(const struct ubifs_info *c,
153449525e5eSSascha Hauer 				   struct shash_desc *desc)
153549525e5eSSascha Hauer {
153649525e5eSSascha Hauer 	if (ubifs_authenticated(c))
153749525e5eSSascha Hauer 		return crypto_shash_init(desc);
153849525e5eSSascha Hauer 	else
153949525e5eSSascha Hauer 		return 0;
154049525e5eSSascha Hauer }
154149525e5eSSascha Hauer 
154249525e5eSSascha Hauer static inline int ubifs_shash_update(const struct ubifs_info *c,
154349525e5eSSascha Hauer 				      struct shash_desc *desc, const void *buf,
154449525e5eSSascha Hauer 				      unsigned int len)
154549525e5eSSascha Hauer {
154649525e5eSSascha Hauer 	int err = 0;
154749525e5eSSascha Hauer 
154849525e5eSSascha Hauer 	if (ubifs_authenticated(c)) {
154949525e5eSSascha Hauer 		err = crypto_shash_update(desc, buf, len);
155049525e5eSSascha Hauer 		if (err < 0)
155149525e5eSSascha Hauer 			return err;
155249525e5eSSascha Hauer 	}
155349525e5eSSascha Hauer 
155449525e5eSSascha Hauer 	return 0;
155549525e5eSSascha Hauer }
155649525e5eSSascha Hauer 
155749525e5eSSascha Hauer static inline int ubifs_shash_final(const struct ubifs_info *c,
155849525e5eSSascha Hauer 				    struct shash_desc *desc, u8 *out)
155949525e5eSSascha Hauer {
156049525e5eSSascha Hauer 	return ubifs_authenticated(c) ? crypto_shash_final(desc, out) : 0;
156149525e5eSSascha Hauer }
156249525e5eSSascha Hauer 
156349525e5eSSascha Hauer int __ubifs_node_calc_hash(const struct ubifs_info *c, const void *buf,
156449525e5eSSascha Hauer 			  u8 *hash);
156549525e5eSSascha Hauer static inline int ubifs_node_calc_hash(const struct ubifs_info *c,
156649525e5eSSascha Hauer 					const void *buf, u8 *hash)
156749525e5eSSascha Hauer {
156849525e5eSSascha Hauer 	if (ubifs_authenticated(c))
156949525e5eSSascha Hauer 		return __ubifs_node_calc_hash(c, buf, hash);
157049525e5eSSascha Hauer 	else
157149525e5eSSascha Hauer 		return 0;
157249525e5eSSascha Hauer }
157349525e5eSSascha Hauer 
157449525e5eSSascha Hauer int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
157549525e5eSSascha Hauer 			     struct shash_desc *inhash);
157649525e5eSSascha Hauer 
157749525e5eSSascha Hauer /**
157849525e5eSSascha Hauer  * ubifs_check_hash - compare two hashes
157949525e5eSSascha Hauer  * @c: UBIFS file-system description object
158049525e5eSSascha Hauer  * @expected: first hash
158149525e5eSSascha Hauer  * @got: second hash
158249525e5eSSascha Hauer  *
158349525e5eSSascha Hauer  * Compare two hashes @expected and @got. Returns 0 when they are equal, a
158449525e5eSSascha Hauer  * negative error code otherwise.
158549525e5eSSascha Hauer  */
158649525e5eSSascha Hauer static inline int ubifs_check_hash(const struct ubifs_info *c,
158749525e5eSSascha Hauer 				   const u8 *expected, const u8 *got)
158849525e5eSSascha Hauer {
158949525e5eSSascha Hauer 	return crypto_memneq(expected, got, c->hash_len);
159049525e5eSSascha Hauer }
159149525e5eSSascha Hauer 
159249525e5eSSascha Hauer /**
159349525e5eSSascha Hauer  * ubifs_check_hmac - compare two HMACs
159449525e5eSSascha Hauer  * @c: UBIFS file-system description object
159549525e5eSSascha Hauer  * @expected: first HMAC
159649525e5eSSascha Hauer  * @got: second HMAC
159749525e5eSSascha Hauer  *
159849525e5eSSascha Hauer  * Compare two hashes @expected and @got. Returns 0 when they are equal, a
159949525e5eSSascha Hauer  * negative error code otherwise.
160049525e5eSSascha Hauer  */
160149525e5eSSascha Hauer static inline int ubifs_check_hmac(const struct ubifs_info *c,
160249525e5eSSascha Hauer 				   const u8 *expected, const u8 *got)
160349525e5eSSascha Hauer {
160449525e5eSSascha Hauer 	return crypto_memneq(expected, got, c->hmac_desc_len);
160549525e5eSSascha Hauer }
160649525e5eSSascha Hauer 
160749525e5eSSascha Hauer void ubifs_bad_hash(const struct ubifs_info *c, const void *node,
160849525e5eSSascha Hauer 		    const u8 *hash, int lnum, int offs);
160949525e5eSSascha Hauer 
161049525e5eSSascha Hauer int __ubifs_node_check_hash(const struct ubifs_info *c, const void *buf,
161149525e5eSSascha Hauer 			  const u8 *expected);
161249525e5eSSascha Hauer static inline int ubifs_node_check_hash(const struct ubifs_info *c,
161349525e5eSSascha Hauer 					const void *buf, const u8 *expected)
161449525e5eSSascha Hauer {
161549525e5eSSascha Hauer 	if (ubifs_authenticated(c))
161649525e5eSSascha Hauer 		return __ubifs_node_check_hash(c, buf, expected);
161749525e5eSSascha Hauer 	else
161849525e5eSSascha Hauer 		return 0;
161949525e5eSSascha Hauer }
162049525e5eSSascha Hauer 
162149525e5eSSascha Hauer int ubifs_init_authentication(struct ubifs_info *c);
162249525e5eSSascha Hauer void __ubifs_exit_authentication(struct ubifs_info *c);
162349525e5eSSascha Hauer static inline void ubifs_exit_authentication(struct ubifs_info *c)
162449525e5eSSascha Hauer {
162549525e5eSSascha Hauer 	if (ubifs_authenticated(c))
162649525e5eSSascha Hauer 		__ubifs_exit_authentication(c);
162749525e5eSSascha Hauer }
162849525e5eSSascha Hauer 
162949525e5eSSascha Hauer /**
163049525e5eSSascha Hauer  * ubifs_branch_hash - returns a pointer to the hash of a branch
163149525e5eSSascha Hauer  * @c: UBIFS file-system description object
163249525e5eSSascha Hauer  * @br: branch to get the hash from
163349525e5eSSascha Hauer  *
163449525e5eSSascha Hauer  * This returns a pointer to the hash of a branch. Since the key already is a
163549525e5eSSascha Hauer  * dynamically sized object we cannot use a struct member here.
163649525e5eSSascha Hauer  */
163749525e5eSSascha Hauer static inline u8 *ubifs_branch_hash(struct ubifs_info *c,
163849525e5eSSascha Hauer 				    struct ubifs_branch *br)
163949525e5eSSascha Hauer {
164049525e5eSSascha Hauer 	return (void *)br + sizeof(*br) + c->key_len;
164149525e5eSSascha Hauer }
164249525e5eSSascha Hauer 
164349525e5eSSascha Hauer /**
164449525e5eSSascha Hauer  * ubifs_copy_hash - copy a hash
164549525e5eSSascha Hauer  * @c: UBIFS file-system description object
164649525e5eSSascha Hauer  * @from: source hash
164749525e5eSSascha Hauer  * @to: destination hash
164849525e5eSSascha Hauer  *
164949525e5eSSascha Hauer  * With authentication this copies a hash, otherwise does nothing.
165049525e5eSSascha Hauer  */
165149525e5eSSascha Hauer static inline void ubifs_copy_hash(const struct ubifs_info *c, const u8 *from,
165249525e5eSSascha Hauer 				   u8 *to)
165349525e5eSSascha Hauer {
165449525e5eSSascha Hauer 	if (ubifs_authenticated(c))
165549525e5eSSascha Hauer 		memcpy(to, from, c->hash_len);
165649525e5eSSascha Hauer }
165749525e5eSSascha Hauer 
165849525e5eSSascha Hauer int __ubifs_node_insert_hmac(const struct ubifs_info *c, void *buf,
165949525e5eSSascha Hauer 			      int len, int ofs_hmac);
166049525e5eSSascha Hauer static inline int ubifs_node_insert_hmac(const struct ubifs_info *c, void *buf,
166149525e5eSSascha Hauer 					  int len, int ofs_hmac)
166249525e5eSSascha Hauer {
166349525e5eSSascha Hauer 	if (ubifs_authenticated(c))
166449525e5eSSascha Hauer 		return __ubifs_node_insert_hmac(c, buf, len, ofs_hmac);
166549525e5eSSascha Hauer 	else
166649525e5eSSascha Hauer 		return 0;
166749525e5eSSascha Hauer }
166849525e5eSSascha Hauer 
166949525e5eSSascha Hauer int __ubifs_node_verify_hmac(const struct ubifs_info *c, const void *buf,
167049525e5eSSascha Hauer 			     int len, int ofs_hmac);
167149525e5eSSascha Hauer static inline int ubifs_node_verify_hmac(const struct ubifs_info *c,
167249525e5eSSascha Hauer 					 const void *buf, int len, int ofs_hmac)
167349525e5eSSascha Hauer {
167449525e5eSSascha Hauer 	if (ubifs_authenticated(c))
167549525e5eSSascha Hauer 		return __ubifs_node_verify_hmac(c, buf, len, ofs_hmac);
167649525e5eSSascha Hauer 	else
167749525e5eSSascha Hauer 		return 0;
167849525e5eSSascha Hauer }
167949525e5eSSascha Hauer 
168049525e5eSSascha Hauer /**
168149525e5eSSascha Hauer  * ubifs_auth_node_sz - returns the size of an authentication node
168249525e5eSSascha Hauer  * @c: UBIFS file-system description object
168349525e5eSSascha Hauer  *
168449525e5eSSascha Hauer  * This function returns the size of an authentication node which can
168549525e5eSSascha Hauer  * be 0 for unauthenticated filesystems or the real size of an auth node
168649525e5eSSascha Hauer  * authentication is enabled.
168749525e5eSSascha Hauer  */
168849525e5eSSascha Hauer static inline int ubifs_auth_node_sz(const struct ubifs_info *c)
168949525e5eSSascha Hauer {
169049525e5eSSascha Hauer 	if (ubifs_authenticated(c))
169149525e5eSSascha Hauer 		return sizeof(struct ubifs_auth_node) + c->hmac_desc_len;
169249525e5eSSascha Hauer 	else
169349525e5eSSascha Hauer 		return 0;
169449525e5eSSascha Hauer }
169549525e5eSSascha Hauer 
169649525e5eSSascha Hauer int ubifs_hmac_wkm(struct ubifs_info *c, u8 *hmac);
169749525e5eSSascha Hauer 
169849525e5eSSascha Hauer int __ubifs_shash_copy_state(const struct ubifs_info *c, struct shash_desc *src,
169949525e5eSSascha Hauer 			     struct shash_desc *target);
170049525e5eSSascha Hauer static inline int ubifs_shash_copy_state(const struct ubifs_info *c,
170149525e5eSSascha Hauer 					   struct shash_desc *src,
170249525e5eSSascha Hauer 					   struct shash_desc *target)
170349525e5eSSascha Hauer {
170449525e5eSSascha Hauer 	if (ubifs_authenticated(c))
170549525e5eSSascha Hauer 		return __ubifs_shash_copy_state(c, src, target);
170649525e5eSSascha Hauer 	else
170749525e5eSSascha Hauer 		return 0;
170849525e5eSSascha Hauer }
170949525e5eSSascha Hauer 
17101e51764aSArtem Bityutskiy /* io.c */
1711ff46d7b3SAdrian Hunter void ubifs_ro_mode(struct ubifs_info *c, int err);
171283cef708SArtem Bityutskiy int ubifs_leb_read(const struct ubifs_info *c, int lnum, void *buf, int offs,
171383cef708SArtem Bityutskiy 		   int len, int even_ebadmsg);
171483cef708SArtem Bityutskiy int ubifs_leb_write(struct ubifs_info *c, int lnum, const void *buf, int offs,
1715b36a261eSRichard Weinberger 		    int len);
1716b36a261eSRichard Weinberger int ubifs_leb_change(struct ubifs_info *c, int lnum, const void *buf, int len);
171783cef708SArtem Bityutskiy int ubifs_leb_unmap(struct ubifs_info *c, int lnum);
1718b36a261eSRichard Weinberger int ubifs_leb_map(struct ubifs_info *c, int lnum);
171983cef708SArtem Bityutskiy int ubifs_is_mapped(const struct ubifs_info *c, int lnum);
17201e51764aSArtem Bityutskiy int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len);
1721b36a261eSRichard Weinberger int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs);
17221e51764aSArtem Bityutskiy int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf);
17231e51764aSArtem Bityutskiy int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
17241e51764aSArtem Bityutskiy 		    int lnum, int offs);
17251e51764aSArtem Bityutskiy int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
17261e51764aSArtem Bityutskiy 			 int lnum, int offs);
17271e51764aSArtem Bityutskiy int ubifs_write_node(struct ubifs_info *c, void *node, int len, int lnum,
1728b36a261eSRichard Weinberger 		     int offs);
1729a384b47eSSascha Hauer int ubifs_write_node_hmac(struct ubifs_info *c, void *buf, int len, int lnum,
1730a384b47eSSascha Hauer 			  int offs, int hmac_offs);
17311e51764aSArtem Bityutskiy int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
17326f7ab6d4SArtem Bityutskiy 		     int offs, int quiet, int must_chk_crc);
1733dead9726SSascha Hauer void ubifs_init_node(struct ubifs_info *c, void *buf, int len, int pad);
1734dead9726SSascha Hauer void ubifs_crc_node(struct ubifs_info *c, void *buf, int len);
17351e51764aSArtem Bityutskiy void ubifs_prepare_node(struct ubifs_info *c, void *buf, int len, int pad);
1736a384b47eSSascha Hauer int ubifs_prepare_node_hmac(struct ubifs_info *c, void *node, int len,
1737a384b47eSSascha Hauer 			    int hmac_offs, int pad);
17381e51764aSArtem Bityutskiy void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last);
17391e51764aSArtem Bityutskiy int ubifs_io_init(struct ubifs_info *c);
17401e51764aSArtem Bityutskiy void ubifs_pad(const struct ubifs_info *c, void *buf, int pad);
17411e51764aSArtem Bityutskiy int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf);
17421e51764aSArtem Bityutskiy int ubifs_bg_wbufs_sync(struct ubifs_info *c);
17431e51764aSArtem Bityutskiy void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum);
17441e51764aSArtem Bityutskiy int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode);
17451e51764aSArtem Bityutskiy 
17461e51764aSArtem Bityutskiy /* scan.c */
17471e51764aSArtem Bityutskiy struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
1748348709baSArtem Bityutskiy 				  int offs, void *sbuf, int quiet);
17491e51764aSArtem Bityutskiy void ubifs_scan_destroy(struct ubifs_scan_leb *sleb);
17501e51764aSArtem Bityutskiy int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum,
17511e51764aSArtem Bityutskiy 		      int offs, int quiet);
17521e51764aSArtem Bityutskiy struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
17531e51764aSArtem Bityutskiy 					int offs, void *sbuf);
17541e51764aSArtem Bityutskiy void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
17551e51764aSArtem Bityutskiy 		    int lnum, int offs);
17561e51764aSArtem Bityutskiy int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
17571e51764aSArtem Bityutskiy 		   void *buf, int offs);
17581e51764aSArtem Bityutskiy void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
17591e51764aSArtem Bityutskiy 			      void *buf);
17601e51764aSArtem Bityutskiy 
17611e51764aSArtem Bityutskiy /* log.c */
17621e51764aSArtem Bityutskiy void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud);
17631e51764aSArtem Bityutskiy void ubifs_create_buds_lists(struct ubifs_info *c);
17641e51764aSArtem Bityutskiy int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs);
17651e51764aSArtem Bityutskiy struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum);
17661e51764aSArtem Bityutskiy struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum);
17671e51764aSArtem Bityutskiy int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum);
17681e51764aSArtem Bityutskiy int ubifs_log_end_commit(struct ubifs_info *c, int new_ltail_lnum);
17691e51764aSArtem Bityutskiy int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum);
17701e51764aSArtem Bityutskiy int ubifs_consolidate_log(struct ubifs_info *c);
17711e51764aSArtem Bityutskiy 
17721e51764aSArtem Bityutskiy /* journal.c */
17731e51764aSArtem Bityutskiy int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
1774f4f61d2cSRichard Weinberger 		     const struct fscrypt_name *nm, const struct inode *inode,
17751e51764aSArtem Bityutskiy 		     int deletion, int xent);
17761e51764aSArtem Bityutskiy int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
17771e51764aSArtem Bityutskiy 			 const union ubifs_key *key, const void *buf, int len);
17781f28681aSArtem Bityutskiy int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode);
1779de94eb55SArtem Bityutskiy int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode);
17809ec64962SRichard Weinberger int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
1781f4f61d2cSRichard Weinberger 		      const struct inode *fst_inode,
1782f4f61d2cSRichard Weinberger 		      const struct fscrypt_name *fst_nm,
17839ec64962SRichard Weinberger 		      const struct inode *snd_dir,
1784f4f61d2cSRichard Weinberger 		      const struct inode *snd_inode,
1785f4f61d2cSRichard Weinberger 		      const struct fscrypt_name *snd_nm, int sync);
17861e51764aSArtem Bityutskiy int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
1787f4f61d2cSRichard Weinberger 		     const struct inode *old_inode,
1788f4f61d2cSRichard Weinberger 		     const struct fscrypt_name *old_nm,
17891e51764aSArtem Bityutskiy 		     const struct inode *new_dir,
1790f4f61d2cSRichard Weinberger 		     const struct inode *new_inode,
1791f4f61d2cSRichard Weinberger 		     const struct fscrypt_name *new_nm,
17929e0a1fffSRichard Weinberger 		     const struct inode *whiteout, int sync);
17931e51764aSArtem Bityutskiy int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
17941e51764aSArtem Bityutskiy 		       loff_t old_size, loff_t new_size);
17951e51764aSArtem Bityutskiy int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
1796f4f61d2cSRichard Weinberger 			   const struct inode *inode, const struct fscrypt_name *nm);
17971e51764aSArtem Bityutskiy int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode1,
17981e51764aSArtem Bityutskiy 			   const struct inode *inode2);
17991e51764aSArtem Bityutskiy 
18001e51764aSArtem Bityutskiy /* budget.c */
18011e51764aSArtem Bityutskiy int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req);
18021e51764aSArtem Bityutskiy void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req);
18031e51764aSArtem Bityutskiy void ubifs_release_dirty_inode_budget(struct ubifs_info *c,
18041e51764aSArtem Bityutskiy 				      struct ubifs_inode *ui);
18051e51764aSArtem Bityutskiy int ubifs_budget_inode_op(struct ubifs_info *c, struct inode *inode,
18061e51764aSArtem Bityutskiy 			  struct ubifs_budget_req *req);
18071e51764aSArtem Bityutskiy void ubifs_release_ino_dirty(struct ubifs_info *c, struct inode *inode,
18081e51764aSArtem Bityutskiy 				struct ubifs_budget_req *req);
18091e51764aSArtem Bityutskiy void ubifs_cancel_ino_op(struct ubifs_info *c, struct inode *inode,
18101e51764aSArtem Bityutskiy 			 struct ubifs_budget_req *req);
18117dad181bSArtem Bityutskiy long long ubifs_get_free_space(struct ubifs_info *c);
181284abf972SArtem Bityutskiy long long ubifs_get_free_space_nolock(struct ubifs_info *c);
18131e51764aSArtem Bityutskiy int ubifs_calc_min_idx_lebs(struct ubifs_info *c);
18141e51764aSArtem Bityutskiy void ubifs_convert_page_budget(struct ubifs_info *c);
18154d61db4fSArtem Bityutskiy long long ubifs_reported_space(const struct ubifs_info *c, long long free);
18161e51764aSArtem Bityutskiy long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs);
18171e51764aSArtem Bityutskiy 
18181e51764aSArtem Bityutskiy /* find.c */
18193edaae7cSArtem Bityutskiy int ubifs_find_free_space(struct ubifs_info *c, int min_space, int *offs,
18201e51764aSArtem Bityutskiy 			  int squeeze);
18211e51764aSArtem Bityutskiy int ubifs_find_free_leb_for_idx(struct ubifs_info *c);
18221e51764aSArtem Bityutskiy int ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp,
18231e51764aSArtem Bityutskiy 			 int min_space, int pick_free);
18241e51764aSArtem Bityutskiy int ubifs_find_dirty_idx_leb(struct ubifs_info *c);
18251e51764aSArtem Bityutskiy int ubifs_save_dirty_idx_lnums(struct ubifs_info *c);
18261e51764aSArtem Bityutskiy 
18271e51764aSArtem Bityutskiy /* tnc.c */
18281e51764aSArtem Bityutskiy int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
18291e51764aSArtem Bityutskiy 			struct ubifs_znode **zn, int *n);
18301e51764aSArtem Bityutskiy int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1831f4f61d2cSRichard Weinberger 			void *node, const struct fscrypt_name *nm);
1832528e3d17SRichard Weinberger int ubifs_tnc_lookup_dh(struct ubifs_info *c, const union ubifs_key *key,
1833528e3d17SRichard Weinberger 			void *node, uint32_t secondary_hash);
18341e51764aSArtem Bityutskiy int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
18351e51764aSArtem Bityutskiy 		     void *node, int *lnum, int *offs);
18361e51764aSArtem Bityutskiy int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
1837823838a4SSascha Hauer 		  int offs, int len, const u8 *hash);
18381e51764aSArtem Bityutskiy int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key,
18391e51764aSArtem Bityutskiy 		      int old_lnum, int old_offs, int lnum, int offs, int len);
18401e51764aSArtem Bityutskiy int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
1841823838a4SSascha Hauer 		     int lnum, int offs, int len, const u8 *hash,
1842823838a4SSascha Hauer 		     const struct fscrypt_name *nm);
18431e51764aSArtem Bityutskiy int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key);
18441e51764aSArtem Bityutskiy int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key,
1845f4f61d2cSRichard Weinberger 			const struct fscrypt_name *nm);
1846781f675eSRichard Weinberger int ubifs_tnc_remove_dh(struct ubifs_info *c, const union ubifs_key *key,
1847781f675eSRichard Weinberger 			uint32_t cookie);
18481e51764aSArtem Bityutskiy int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
18491e51764aSArtem Bityutskiy 			   union ubifs_key *to_key);
18501e51764aSArtem Bityutskiy int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum);
18511e51764aSArtem Bityutskiy struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c,
18521e51764aSArtem Bityutskiy 					   union ubifs_key *key,
1853f4f61d2cSRichard Weinberger 					   const struct fscrypt_name *nm);
18541e51764aSArtem Bityutskiy void ubifs_tnc_close(struct ubifs_info *c);
18551e51764aSArtem Bityutskiy int ubifs_tnc_has_node(struct ubifs_info *c, union ubifs_key *key, int level,
18561e51764aSArtem Bityutskiy 		       int lnum, int offs, int is_idx);
18571e51764aSArtem Bityutskiy int ubifs_dirty_idx_node(struct ubifs_info *c, union ubifs_key *key, int level,
18581e51764aSArtem Bityutskiy 			 int lnum, int offs);
18591e51764aSArtem Bityutskiy /* Shared by tnc.c for tnc_commit.c */
18601e51764aSArtem Bityutskiy void destroy_old_idx(struct ubifs_info *c);
18611e51764aSArtem Bityutskiy int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level,
18621e51764aSArtem Bityutskiy 		       int lnum, int offs);
18631e51764aSArtem Bityutskiy int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode);
18644793e7c5SAdrian Hunter int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu);
18654793e7c5SAdrian Hunter int ubifs_tnc_bulk_read(struct ubifs_info *c, struct bu_info *bu);
18661e51764aSArtem Bityutskiy 
18671e51764aSArtem Bityutskiy /* tnc_misc.c */
18686eb61d58SRichard Weinberger struct ubifs_znode *ubifs_tnc_levelorder_next(const struct ubifs_info *c,
18696eb61d58SRichard Weinberger 					      struct ubifs_znode *zr,
18701e51764aSArtem Bityutskiy 					      struct ubifs_znode *znode);
18711e51764aSArtem Bityutskiy int ubifs_search_zbranch(const struct ubifs_info *c,
18721e51764aSArtem Bityutskiy 			 const struct ubifs_znode *znode,
18731e51764aSArtem Bityutskiy 			 const union ubifs_key *key, int *n);
18741e51764aSArtem Bityutskiy struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode);
18756eb61d58SRichard Weinberger struct ubifs_znode *ubifs_tnc_postorder_next(const struct ubifs_info *c,
18766eb61d58SRichard Weinberger 					     struct ubifs_znode *znode);
18776eb61d58SRichard Weinberger long ubifs_destroy_tnc_subtree(const struct ubifs_info *c,
18786eb61d58SRichard Weinberger 			       struct ubifs_znode *zr);
18791e51764aSArtem Bityutskiy struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
18801e51764aSArtem Bityutskiy 				     struct ubifs_zbranch *zbr,
18811e51764aSArtem Bityutskiy 				     struct ubifs_znode *parent, int iip);
18821e51764aSArtem Bityutskiy int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
18831e51764aSArtem Bityutskiy 			void *node);
18841e51764aSArtem Bityutskiy 
18851e51764aSArtem Bityutskiy /* tnc_commit.c */
18861e51764aSArtem Bityutskiy int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot);
18871e51764aSArtem Bityutskiy int ubifs_tnc_end_commit(struct ubifs_info *c);
18881e51764aSArtem Bityutskiy 
18891e51764aSArtem Bityutskiy /* shrinker.c */
18901ab6c499SDave Chinner unsigned long ubifs_shrink_scan(struct shrinker *shrink,
18911ab6c499SDave Chinner 				struct shrink_control *sc);
18921ab6c499SDave Chinner unsigned long ubifs_shrink_count(struct shrinker *shrink,
18931ab6c499SDave Chinner 				 struct shrink_control *sc);
18941e51764aSArtem Bityutskiy 
18951e51764aSArtem Bityutskiy /* commit.c */
18961e51764aSArtem Bityutskiy int ubifs_bg_thread(void *info);
18971e51764aSArtem Bityutskiy void ubifs_commit_required(struct ubifs_info *c);
18981e51764aSArtem Bityutskiy void ubifs_request_bg_commit(struct ubifs_info *c);
18991e51764aSArtem Bityutskiy int ubifs_run_commit(struct ubifs_info *c);
19001e51764aSArtem Bityutskiy void ubifs_recovery_commit(struct ubifs_info *c);
19011e51764aSArtem Bityutskiy int ubifs_gc_should_commit(struct ubifs_info *c);
19021e51764aSArtem Bityutskiy void ubifs_wait_for_commit(struct ubifs_info *c);
19031e51764aSArtem Bityutskiy 
19041e51764aSArtem Bityutskiy /* master.c */
1905625700ccSSascha Hauer int ubifs_compare_master_node(struct ubifs_info *c, void *m1, void *m2);
19061e51764aSArtem Bityutskiy int ubifs_read_master(struct ubifs_info *c);
19071e51764aSArtem Bityutskiy int ubifs_write_master(struct ubifs_info *c);
19081e51764aSArtem Bityutskiy 
19091e51764aSArtem Bityutskiy /* sb.c */
19101e51764aSArtem Bityutskiy int ubifs_read_superblock(struct ubifs_info *c);
19111e51764aSArtem Bityutskiy int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup);
19126554a657SMatthew L. Creech int ubifs_fixup_free_space(struct ubifs_info *c);
1913e021986eSRichard Weinberger int ubifs_enable_encryption(struct ubifs_info *c);
19141e51764aSArtem Bityutskiy 
19151e51764aSArtem Bityutskiy /* replay.c */
19161e51764aSArtem Bityutskiy int ubifs_validate_entry(struct ubifs_info *c,
19171e51764aSArtem Bityutskiy 			 const struct ubifs_dent_node *dent);
19181e51764aSArtem Bityutskiy int ubifs_replay_journal(struct ubifs_info *c);
19191e51764aSArtem Bityutskiy 
19201e51764aSArtem Bityutskiy /* gc.c */
19211e51764aSArtem Bityutskiy int ubifs_garbage_collect(struct ubifs_info *c, int anyway);
19221e51764aSArtem Bityutskiy int ubifs_gc_start_commit(struct ubifs_info *c);
19231e51764aSArtem Bityutskiy int ubifs_gc_end_commit(struct ubifs_info *c);
1924b466f17dSAdrian Hunter void ubifs_destroy_idx_gc(struct ubifs_info *c);
19251e51764aSArtem Bityutskiy int ubifs_get_idx_gc_leb(struct ubifs_info *c);
19261e51764aSArtem Bityutskiy int ubifs_garbage_collect_leb(struct ubifs_info *c, struct ubifs_lprops *lp);
19271e51764aSArtem Bityutskiy 
19281e51764aSArtem Bityutskiy /* orphan.c */
19291e51764aSArtem Bityutskiy int ubifs_add_orphan(struct ubifs_info *c, ino_t inum);
19301e51764aSArtem Bityutskiy void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum);
19311e51764aSArtem Bityutskiy int ubifs_orphan_start_commit(struct ubifs_info *c);
19321e51764aSArtem Bityutskiy int ubifs_orphan_end_commit(struct ubifs_info *c);
19331e51764aSArtem Bityutskiy int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only);
193449d128aaSAdrian Hunter int ubifs_clear_orphans(struct ubifs_info *c);
19351e51764aSArtem Bityutskiy 
19361e51764aSArtem Bityutskiy /* lpt.c */
19371e51764aSArtem Bityutskiy int ubifs_calc_lpt_geom(struct ubifs_info *c);
19381e51764aSArtem Bityutskiy int ubifs_create_dflt_lpt(struct ubifs_info *c, int *main_lebs, int lpt_first,
1939b5b1f083SSascha Hauer 			  int *lpt_lebs, int *big_lpt, u8 *hash);
19401e51764aSArtem Bityutskiy int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr);
19411e51764aSArtem Bityutskiy struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum);
19421e51764aSArtem Bityutskiy struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum);
19431e51764aSArtem Bityutskiy int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
19441e51764aSArtem Bityutskiy 			  ubifs_lpt_scan_callback scan_cb, void *data);
19451e51764aSArtem Bityutskiy 
19461e51764aSArtem Bityutskiy /* Shared by lpt.c for lpt_commit.c */
19471e51764aSArtem Bityutskiy void ubifs_pack_lsave(struct ubifs_info *c, void *buf, int *lsave);
19481e51764aSArtem Bityutskiy void ubifs_pack_ltab(struct ubifs_info *c, void *buf,
19491e51764aSArtem Bityutskiy 		     struct ubifs_lpt_lprops *ltab);
19501e51764aSArtem Bityutskiy void ubifs_pack_pnode(struct ubifs_info *c, void *buf,
19511e51764aSArtem Bityutskiy 		      struct ubifs_pnode *pnode);
19521e51764aSArtem Bityutskiy void ubifs_pack_nnode(struct ubifs_info *c, void *buf,
19531e51764aSArtem Bityutskiy 		      struct ubifs_nnode *nnode);
19541e51764aSArtem Bityutskiy struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
19551e51764aSArtem Bityutskiy 				    struct ubifs_nnode *parent, int iip);
19561e51764aSArtem Bityutskiy struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
19571e51764aSArtem Bityutskiy 				    struct ubifs_nnode *parent, int iip);
19580e26b6e2SSascha Hauer struct ubifs_pnode *ubifs_pnode_lookup(struct ubifs_info *c, int i);
19591e51764aSArtem Bityutskiy int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip);
19601e51764aSArtem Bityutskiy void ubifs_add_lpt_dirt(struct ubifs_info *c, int lnum, int dirty);
19611e51764aSArtem Bityutskiy void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode);
19626eb61d58SRichard Weinberger uint32_t ubifs_unpack_bits(const struct ubifs_info *c, uint8_t **addr, int *pos, int nrbits);
19631e51764aSArtem Bityutskiy struct ubifs_nnode *ubifs_first_nnode(struct ubifs_info *c, int *hght);
19642ba5f7aeSArtem Bityutskiy /* Needed only in debugging code in lpt_commit.c */
19652ba5f7aeSArtem Bityutskiy int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
19662ba5f7aeSArtem Bityutskiy 		       struct ubifs_nnode *nnode);
1967a1dc5814SSascha Hauer int ubifs_lpt_calc_hash(struct ubifs_info *c, u8 *hash);
19681e51764aSArtem Bityutskiy 
19691e51764aSArtem Bityutskiy /* lpt_commit.c */
19701e51764aSArtem Bityutskiy int ubifs_lpt_start_commit(struct ubifs_info *c);
19711e51764aSArtem Bityutskiy int ubifs_lpt_end_commit(struct ubifs_info *c);
19721e51764aSArtem Bityutskiy int ubifs_lpt_post_commit(struct ubifs_info *c);
19731e51764aSArtem Bityutskiy void ubifs_lpt_free(struct ubifs_info *c, int wr_only);
19741e51764aSArtem Bityutskiy 
19751e51764aSArtem Bityutskiy /* lprops.c */
19761e51764aSArtem Bityutskiy const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
19771e51764aSArtem Bityutskiy 					   const struct ubifs_lprops *lp,
19781e51764aSArtem Bityutskiy 					   int free, int dirty, int flags,
19791e51764aSArtem Bityutskiy 					   int idx_gc_cnt);
198084abf972SArtem Bityutskiy void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *lst);
19811e51764aSArtem Bityutskiy void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
19821e51764aSArtem Bityutskiy 		      int cat);
19831e51764aSArtem Bityutskiy void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
19841e51764aSArtem Bityutskiy 		       struct ubifs_lprops *new_lprops);
19851e51764aSArtem Bityutskiy void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops);
19861e51764aSArtem Bityutskiy int ubifs_categorize_lprops(const struct ubifs_info *c,
19871e51764aSArtem Bityutskiy 			    const struct ubifs_lprops *lprops);
19881e51764aSArtem Bityutskiy int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
19891e51764aSArtem Bityutskiy 			int flags_set, int flags_clean, int idx_gc_cnt);
19901e51764aSArtem Bityutskiy int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
19911e51764aSArtem Bityutskiy 			int flags_set, int flags_clean);
19921e51764aSArtem Bityutskiy int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp);
19931e51764aSArtem Bityutskiy const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c);
19941e51764aSArtem Bityutskiy const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c);
19951e51764aSArtem Bityutskiy const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c);
19961e51764aSArtem Bityutskiy const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c);
1997be9e62a7SArtem Bityutskiy int ubifs_calc_dark(const struct ubifs_info *c, int spc);
19981e51764aSArtem Bityutskiy 
19991e51764aSArtem Bityutskiy /* file.c */
200002c24a82SJosef Bacik int ubifs_fsync(struct file *file, loff_t start, loff_t end, int datasync);
20011e51764aSArtem Bityutskiy int ubifs_setattr(struct dentry *dentry, struct iattr *attr);
200295582b00SDeepa Dinamani int ubifs_update_time(struct inode *inode, struct timespec64 *time, int flags);
20031e51764aSArtem Bityutskiy 
20041e51764aSArtem Bityutskiy /* dir.c */
2005d475a507SRichard Weinberger struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
2006ad44be5cSAl Viro 			      umode_t mode);
2007a528d35eSDavid Howells int ubifs_getattr(const struct path *path, struct kstat *stat,
2008a528d35eSDavid Howells 		  u32 request_mask, unsigned int flags);
2009f6337d84SRichard Weinberger int ubifs_check_dir_empty(struct inode *dir);
20101e51764aSArtem Bityutskiy 
20111e51764aSArtem Bityutskiy /* xattr.c */
20122b88fc21SAndreas Gruenbacher extern const struct xattr_handler *ubifs_xattr_handlers[];
20131e51764aSArtem Bityutskiy ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size);
2014ade46c3aSRichard Weinberger int ubifs_xattr_set(struct inode *host, const char *name, const void *value,
2015d8db5b1cSXiaolei Li 		    size_t size, int flags, bool check_lock);
2016ade46c3aSRichard Weinberger ssize_t ubifs_xattr_get(struct inode *host, const char *name, void *buf,
2017ade46c3aSRichard Weinberger 			size_t size);
20187e5471ceSStefan Agner 
20197e5471ceSStefan Agner #ifdef CONFIG_UBIFS_FS_XATTR
2020272eda82SRichard Weinberger void ubifs_evict_xattr_inode(struct ubifs_info *c, ino_t xattr_inum);
2021481a9b80SYueHaibing int ubifs_purge_xattrs(struct inode *host);
20227e5471ceSStefan Agner #else
20237e5471ceSStefan Agner static inline void ubifs_evict_xattr_inode(struct ubifs_info *c,
20247e5471ceSStefan Agner 					   ino_t xattr_inum) { }
2025481a9b80SYueHaibing static inline int ubifs_purge_xattrs(struct inode *host)
2026481a9b80SYueHaibing {
2027481a9b80SYueHaibing 	return 0;
2028481a9b80SYueHaibing }
20297e5471ceSStefan Agner #endif
20301e51764aSArtem Bityutskiy 
20318326c1eeSHyunchul Lee #ifdef CONFIG_UBIFS_FS_SECURITY
20328326c1eeSHyunchul Lee extern int ubifs_init_security(struct inode *dentry, struct inode *inode,
20338326c1eeSHyunchul Lee 			const struct qstr *qstr);
20348326c1eeSHyunchul Lee #else
20358326c1eeSHyunchul Lee static inline int ubifs_init_security(struct inode *dentry,
20368326c1eeSHyunchul Lee 			struct inode *inode, const struct qstr *qstr)
20378326c1eeSHyunchul Lee {
20388326c1eeSHyunchul Lee 	return 0;
20398326c1eeSHyunchul Lee }
20408326c1eeSHyunchul Lee #endif
20418326c1eeSHyunchul Lee 
20428326c1eeSHyunchul Lee 
20431e51764aSArtem Bityutskiy /* super.c */
20441e51764aSArtem Bityutskiy struct inode *ubifs_iget(struct super_block *sb, unsigned long inum);
20451e51764aSArtem Bityutskiy 
20461e51764aSArtem Bityutskiy /* recovery.c */
20471e51764aSArtem Bityutskiy int ubifs_recover_master_node(struct ubifs_info *c);
20481e51764aSArtem Bityutskiy int ubifs_write_rcvrd_mst_node(struct ubifs_info *c);
20491e51764aSArtem Bityutskiy struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
2050efcfde54SArtem Bityutskiy 					 int offs, void *sbuf, int jhead);
20511e51764aSArtem Bityutskiy struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
20521e51764aSArtem Bityutskiy 					     int offs, void *sbuf);
205383cef708SArtem Bityutskiy int ubifs_recover_inl_heads(struct ubifs_info *c, void *sbuf);
205483cef708SArtem Bityutskiy int ubifs_clean_lebs(struct ubifs_info *c, void *sbuf);
20551e51764aSArtem Bityutskiy int ubifs_rcvry_gc_commit(struct ubifs_info *c);
20561e51764aSArtem Bityutskiy int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
20571e51764aSArtem Bityutskiy 			     int deletion, loff_t new_size);
20581e76592fSSascha Hauer int ubifs_recover_size(struct ubifs_info *c, bool in_place);
20591e51764aSArtem Bityutskiy void ubifs_destroy_size_tree(struct ubifs_info *c);
20601e51764aSArtem Bityutskiy 
20611e51764aSArtem Bityutskiy /* ioctl.c */
20621e51764aSArtem Bityutskiy long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
20631e51764aSArtem Bityutskiy void ubifs_set_inode_flags(struct inode *inode);
20641e51764aSArtem Bityutskiy #ifdef CONFIG_COMPAT
20651e51764aSArtem Bityutskiy long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
20661e51764aSArtem Bityutskiy #endif
20671e51764aSArtem Bityutskiy 
20681e51764aSArtem Bityutskiy /* compressor.c */
20691e51764aSArtem Bityutskiy int __init ubifs_compressors_init(void);
2070995be045SAlexey Dobriyan void ubifs_compressors_exit(void);
2071235c362bSSheng Yong void ubifs_compress(const struct ubifs_info *c, const void *in_buf, int in_len,
2072235c362bSSheng Yong 		    void *out_buf, int *out_len, int *compr_type);
2073235c362bSSheng Yong int ubifs_decompress(const struct ubifs_info *c, const void *buf, int len,
2074235c362bSSheng Yong 		     void *out, int *out_len, int compr_type);
20751e51764aSArtem Bityutskiy 
20761e51764aSArtem Bityutskiy #include "debug.h"
20771e51764aSArtem Bityutskiy #include "misc.h"
20781e51764aSArtem Bityutskiy #include "key.h"
20791e51764aSArtem Bityutskiy 
2080643fa961SChandan Rajendra #ifndef CONFIG_FS_ENCRYPTION
20817799953bSRichard Weinberger static inline int ubifs_encrypt(const struct inode *inode,
20827799953bSRichard Weinberger 				struct ubifs_data_node *dn,
20837799953bSRichard Weinberger 				unsigned int in_len, unsigned int *out_len,
20847799953bSRichard Weinberger 				int block)
20857799953bSRichard Weinberger {
20866eb61d58SRichard Weinberger 	struct ubifs_info *c = inode->i_sb->s_fs_info;
20876eb61d58SRichard Weinberger 	ubifs_assert(c, 0);
20887799953bSRichard Weinberger 	return -EOPNOTSUPP;
20897799953bSRichard Weinberger }
20907799953bSRichard Weinberger static inline int ubifs_decrypt(const struct inode *inode,
20917799953bSRichard Weinberger 				struct ubifs_data_node *dn,
20927799953bSRichard Weinberger 				unsigned int *out_len, int block)
20937799953bSRichard Weinberger {
20946eb61d58SRichard Weinberger 	struct ubifs_info *c = inode->i_sb->s_fs_info;
20956eb61d58SRichard Weinberger 	ubifs_assert(c, 0);
20967799953bSRichard Weinberger 	return -EOPNOTSUPP;
20977799953bSRichard Weinberger }
20987799953bSRichard Weinberger #else
20997799953bSRichard Weinberger /* crypto.c */
21007799953bSRichard Weinberger int ubifs_encrypt(const struct inode *inode, struct ubifs_data_node *dn,
21017799953bSRichard Weinberger 		  unsigned int in_len, unsigned int *out_len, int block);
21027799953bSRichard Weinberger int ubifs_decrypt(const struct inode *inode, struct ubifs_data_node *dn,
21037799953bSRichard Weinberger 		  unsigned int *out_len, int block);
2104d475a507SRichard Weinberger #endif
2105d475a507SRichard Weinberger 
21066f69f0edSEric Biggers extern const struct fscrypt_operations ubifs_crypt_operations;
21077799953bSRichard Weinberger 
21081ee77870SRichard Weinberger static inline bool ubifs_crypt_is_encrypted(const struct inode *inode)
21091ee77870SRichard Weinberger {
2110f7293e48SEric Biggers 	const struct ubifs_inode *ui = ubifs_inode(inode);
2111f7293e48SEric Biggers 
2112f7293e48SEric Biggers 	return ui->flags & UBIFS_CRYPT_FL;
21131ee77870SRichard Weinberger }
21141ee77870SRichard Weinberger 
21153e7f2c51SJoe Perches /* Normal UBIFS messages */
21163e7f2c51SJoe Perches __printf(2, 3)
21173e7f2c51SJoe Perches void ubifs_msg(const struct ubifs_info *c, const char *fmt, ...);
21183e7f2c51SJoe Perches __printf(2, 3)
21193e7f2c51SJoe Perches void ubifs_err(const struct ubifs_info *c, const char *fmt, ...);
21203e7f2c51SJoe Perches __printf(2, 3)
21213e7f2c51SJoe Perches void ubifs_warn(const struct ubifs_info *c, const char *fmt, ...);
21223e7f2c51SJoe Perches /*
2123380bc8b7SDaniel Golle  * A conditional variant of 'ubifs_err()' which doesn't output anything
21241751e8a6SLinus Torvalds  * if probing (ie. SB_SILENT set).
21253e7f2c51SJoe Perches  */
21263e7f2c51SJoe Perches #define ubifs_errc(c, fmt, ...)						\
21273e7f2c51SJoe Perches do {									\
21283e7f2c51SJoe Perches 	if (!(c)->probing)						\
21293e7f2c51SJoe Perches 		ubifs_err(c, fmt, ##__VA_ARGS__);			\
21303e7f2c51SJoe Perches } while (0)
21313e7f2c51SJoe Perches 
21321e51764aSArtem Bityutskiy #endif /* !__UBIFS_H__ */
2133