LCOV - code coverage report
Current view: top level - libubifs - ubifs.h (source / functions) Hit Total Coverage
Test: a simple test Lines: 21 24 87.5 %
Date: 2024-06-05 20:10:43 Functions: 0 0 -
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* SPDX-License-Identifier: GPL-2.0-only */
       2             : /*
       3             :  * This file is part of UBIFS.
       4             :  *
       5             :  * Copyright (C) 2006-2008 Nokia Corporation
       6             :  *
       7             :  * Authors: Artem Bityutskiy (Битюцкий Артём)
       8             :  *          Adrian Hunter
       9             :  */
      10             : 
      11             : #ifndef __UBIFS_H__
      12             : #define __UBIFS_H__
      13             : 
      14             : #include <string.h>
      15             : #include <unistd.h>
      16             : #include <sys/user.h>
      17             : 
      18             : #include "linux_types.h"
      19             : #include "list.h"
      20             : #include "rbtree.h"
      21             : #include "spinlock.h"
      22             : #include "mutex.h"
      23             : #include "rwsem.h"
      24             : #include "atomic.h"
      25             : #include "libubi.h"
      26             : #include "ubifs-media.h"
      27             : 
      28             : /* Number of UBIFS blocks per VFS page */
      29             : #define UBIFS_BLOCKS_PER_PAGE (PAGE_SIZE / UBIFS_BLOCK_SIZE)
      30             : #define UBIFS_BLOCKS_PER_PAGE_SHIFT (PAGE_SHIFT - UBIFS_BLOCK_SHIFT)
      31             : 
      32             : /* "File system end of life" sequence number watermark */
      33             : #define SQNUM_WARN_WATERMARK 0xFFFFFFFF00000000ULL
      34             : #define SQNUM_WATERMARK      0xFFFFFFFFFF000000ULL
      35             : 
      36             : /*
      37             :  * Minimum amount of LEBs reserved for the index. At present the index needs at
      38             :  * least 2 LEBs: one for the index head and one for in-the-gaps method (which
      39             :  * currently does not cater for the index head and so excludes it from
      40             :  * consideration).
      41             :  */
      42             : #define MIN_INDEX_LEBS 2
      43             : 
      44             : /* Maximum logical eraseblock size in bytes */
      45             : #define UBIFS_MAX_LEB_SZ (2*1024*1024)
      46             : 
      47             : /* Minimum amount of data UBIFS writes to the flash */
      48             : #define MIN_WRITE_SZ (UBIFS_DATA_NODE_SZ + 8)
      49             : 
      50             : /*
      51             :  * Currently we do not support inode number overlapping and re-using, so this
      52             :  * watermark defines dangerous inode number level. This should be fixed later,
      53             :  * although it is difficult to exceed current limit. Another option is to use
      54             :  * 64-bit inode numbers, but this means more overhead.
      55             :  */
      56             : #define INUM_WARN_WATERMARK 0xFFF00000
      57             : #define INUM_WATERMARK      0xFFFFFF00
      58             : 
      59             : /* Maximum number of entries in each LPT (LEB category) heap */
      60             : #define LPT_HEAP_SZ 256
      61             : 
      62             : /* Maximum possible inode number (only 32-bit inodes are supported now) */
      63             : #define MAX_INUM 0xFFFFFFFF
      64             : 
      65             : /* Number of non-data journal heads */
      66             : #define NONDATA_JHEADS_CNT 2
      67             : 
      68             : /* Shorter names for journal head numbers for internal usage */
      69             : #define GCHD   UBIFS_GC_HEAD
      70             : #define BASEHD UBIFS_BASE_HEAD
      71             : #define DATAHD UBIFS_DATA_HEAD
      72             : 
      73             : /* 'No change' value for 'ubifs_change_lp()' */
      74             : #define LPROPS_NC 0x80000001
      75             : 
      76             : /*
      77             :  * There is no notion of truncation key because truncation nodes do not exist
      78             :  * in TNC. However, when replaying, it is handy to introduce fake "truncation"
      79             :  * keys for truncation nodes because the code becomes simpler. So we define
      80             :  * %UBIFS_TRUN_KEY type.
      81             :  *
      82             :  * But otherwise, out of the journal reply scope, the truncation keys are
      83             :  * invalid.
      84             :  */
      85             : #define UBIFS_TRUN_KEY    UBIFS_KEY_TYPES_CNT
      86             : #define UBIFS_INVALID_KEY UBIFS_KEY_TYPES_CNT
      87             : 
      88             : /*
      89             :  * How much a directory entry/extended attribute entry adds to the parent/host
      90             :  * inode.
      91             :  */
      92             : #define CALC_DENT_SIZE(name_len) ALIGN(UBIFS_DENT_NODE_SZ + (name_len) + 1, 8)
      93             : 
      94             : /* How much an extended attribute adds to the host inode */
      95             : #define CALC_XATTR_BYTES(data_len) ALIGN(UBIFS_INO_NODE_SZ + (data_len) + 1, 8)
      96             : 
      97             : /* Maximum expected tree height for use by bottom_up_buf */
      98             : #define BOTTOM_UP_HEIGHT 64
      99             : 
     100             : #define UBIFS_HASH_ARR_SZ UBIFS_MAX_HASH_LEN
     101             : #define UBIFS_HMAC_ARR_SZ UBIFS_MAX_HMAC_LEN
     102             : 
     103             : /*
     104             :  * Znode flags (actually, bit numbers which store the flags).
     105             :  *
     106             :  * DIRTY_ZNODE: znode is dirty
     107             :  * COW_ZNODE: znode is being committed and a new instance of this znode has to
     108             :  *            be created before changing this znode
     109             :  * OBSOLETE_ZNODE: znode is obsolete, which means it was deleted, but it is
     110             :  *                 still in the commit list and the ongoing commit operation
     111             :  *                 will commit it, and delete this znode after it is done
     112             :  */
     113             : enum {
     114             :         DIRTY_ZNODE    = 0,
     115             :         COW_ZNODE      = 1,
     116             :         OBSOLETE_ZNODE = 2,
     117             : };
     118             : 
     119             : /*
     120             :  * Commit states.
     121             :  *
     122             :  * COMMIT_RESTING: commit is not wanted
     123             :  * COMMIT_BACKGROUND: background commit has been requested
     124             :  * COMMIT_REQUIRED: commit is required
     125             :  * COMMIT_RUNNING_BACKGROUND: background commit is running
     126             :  * COMMIT_RUNNING_REQUIRED: commit is running and it is required
     127             :  * COMMIT_BROKEN: commit failed
     128             :  */
     129             : enum {
     130             :         COMMIT_RESTING = 0,
     131             :         COMMIT_BACKGROUND,
     132             :         COMMIT_REQUIRED,
     133             :         COMMIT_RUNNING_BACKGROUND,
     134             :         COMMIT_RUNNING_REQUIRED,
     135             :         COMMIT_BROKEN,
     136             : };
     137             : 
     138             : /*
     139             :  * 'ubifs_scan_a_node()' return values.
     140             :  *
     141             :  * SCANNED_GARBAGE:  scanned garbage
     142             :  * SCANNED_EMPTY_SPACE: scanned empty space
     143             :  * SCANNED_A_NODE: scanned a valid node
     144             :  * SCANNED_A_CORRUPT_NODE: scanned a corrupted node
     145             :  * SCANNED_A_BAD_PAD_NODE: scanned a padding node with invalid pad length
     146             :  *
     147             :  * Greater than zero means: 'scanned that number of padding bytes'
     148             :  */
     149             : enum {
     150             :         SCANNED_GARBAGE        = 0,
     151             :         SCANNED_EMPTY_SPACE    = -1,
     152             :         SCANNED_A_NODE         = -2,
     153             :         SCANNED_A_CORRUPT_NODE = -3,
     154             :         SCANNED_A_BAD_PAD_NODE = -4,
     155             : };
     156             : 
     157             : /*
     158             :  * LPT cnode flag bits.
     159             :  *
     160             :  * DIRTY_CNODE: cnode is dirty
     161             :  * OBSOLETE_CNODE: cnode is being committed and has been copied (or deleted),
     162             :  *                 so it can (and must) be freed when the commit is finished
     163             :  * COW_CNODE: cnode is being committed and must be copied before writing
     164             :  */
     165             : enum {
     166             :         DIRTY_CNODE    = 0,
     167             :         OBSOLETE_CNODE = 1,
     168             :         COW_CNODE      = 2,
     169             : };
     170             : 
     171             : /*
     172             :  * Dirty flag bits (lpt_drty_flgs) for LPT special nodes.
     173             :  *
     174             :  * LTAB_DIRTY: ltab node is dirty
     175             :  * LSAVE_DIRTY: lsave node is dirty
     176             :  */
     177             : enum {
     178             :         LTAB_DIRTY  = 1,
     179             :         LSAVE_DIRTY = 2,
     180             : };
     181             : 
     182             : /*
     183             :  * Return codes used by the garbage collector.
     184             :  * @LEB_FREED: the logical eraseblock was freed and is ready to use
     185             :  * @LEB_FREED_IDX: indexing LEB was freed and can be used only after the commit
     186             :  * @LEB_RETAINED: the logical eraseblock was freed and retained for GC purposes
     187             :  */
     188             : enum {
     189             :         LEB_FREED,
     190             :         LEB_FREED_IDX,
     191             :         LEB_RETAINED,
     192             : };
     193             : 
     194             : /**
     195             :  * struct ubifs_old_idx - index node obsoleted since last commit start.
     196             :  * @rb: rb-tree node
     197             :  * @lnum: LEB number of obsoleted index node
     198             :  * @offs: offset of obsoleted index node
     199             :  */
     200             : struct ubifs_old_idx {
     201             :         struct rb_node rb;
     202             :         int lnum;
     203             :         int offs;
     204             : };
     205             : 
     206             : /* The below union makes it easier to deal with keys */
     207             : union ubifs_key {
     208             :         uint8_t u8[UBIFS_SK_LEN];
     209             :         uint32_t u32[UBIFS_SK_LEN/4];
     210             :         uint64_t u64[UBIFS_SK_LEN/8];
     211             :         __le32 j32[UBIFS_SK_LEN/4];
     212             : };
     213             : 
     214             : /**
     215             :  * struct ubifs_scan_node - UBIFS scanned node information.
     216             :  * @list: list of scanned nodes
     217             :  * @key: key of node scanned (if it has one)
     218             :  * @sqnum: sequence number
     219             :  * @type: type of node scanned
     220             :  * @offs: offset with LEB of node scanned
     221             :  * @len: length of node scanned
     222             :  * @node: raw node
     223             :  */
     224             : struct ubifs_scan_node {
     225             :         struct list_head list;
     226             :         union ubifs_key key;
     227             :         unsigned long long sqnum;
     228             :         int type;
     229             :         int offs;
     230             :         int len;
     231             :         void *node;
     232             : };
     233             : 
     234             : /**
     235             :  * struct ubifs_scan_leb - UBIFS scanned LEB information.
     236             :  * @lnum: logical eraseblock number
     237             :  * @nodes_cnt: number of nodes scanned
     238             :  * @nodes: list of struct ubifs_scan_node
     239             :  * @endpt: end point (and therefore the start of empty space)
     240             :  * @buf: buffer containing entire LEB scanned
     241             :  */
     242             : struct ubifs_scan_leb {
     243             :         int lnum;
     244             :         int nodes_cnt;
     245             :         struct list_head nodes;
     246             :         int endpt;
     247             :         void *buf;
     248             : };
     249             : 
     250             : /**
     251             :  * struct ubifs_gced_idx_leb - garbage-collected indexing LEB.
     252             :  * @list: list
     253             :  * @lnum: LEB number
     254             :  * @unmap: OK to unmap this LEB
     255             :  *
     256             :  * This data structure is used to temporary store garbage-collected indexing
     257             :  * LEBs - they are not released immediately, but only after the next commit.
     258             :  * This is needed to guarantee recoverability.
     259             :  */
     260             : struct ubifs_gced_idx_leb {
     261             :         struct list_head list;
     262             :         int lnum;
     263             :         int unmap;
     264             : };
     265             : 
     266             : /**
     267             :  * struct inode - inode description.
     268             :  * @uid: owner ID
     269             :  * @gid: group ID
     270             :  * @mode: access flags
     271             :  * @nlink: number of hard links
     272             :  * @inum: inode number
     273             :  * @atime_sec: access time seconds
     274             :  * @ctime_sec: creation time seconds
     275             :  * @mtime_sec: modification time seconds
     276             :  * @atime_nsec: access time nanoseconds
     277             :  * @ctime_nsec: creation time nanoseconds
     278             :  * @mtime_nsec: modification time nanoseconds
     279             :  */
     280             : struct inode {
     281             :         unsigned int uid;
     282             :         unsigned int gid;
     283             :         unsigned int mode;
     284             :         unsigned int nlink;
     285             :         ino_t inum;
     286             :         unsigned long long atime_sec;
     287             :         unsigned long long ctime_sec;
     288             :         unsigned long long mtime_sec;
     289             :         unsigned int atime_nsec;
     290             :         unsigned int ctime_nsec;
     291             :         unsigned int mtime_nsec;
     292             : };
     293             : 
     294             : /**
     295             :  * struct ubifs_inode - UBIFS in-memory inode description.
     296             :  * @vfs_inode: VFS inode description object
     297             :  * @creat_sqnum: sequence number at time of creation
     298             :  * @xattr_size: summarized size of all extended attributes in bytes
     299             :  * @xattr_cnt: count of extended attributes this inode has
     300             :  * @xattr_names: sum of lengths of all extended attribute names belonging to
     301             :  *               this inode
     302             :  * @ui_size: inode size used by UBIFS when writing to flash
     303             :  * @flags: inode flags (@UBIFS_COMPR_FL, etc)
     304             :  * @compr_type: default compression type used for this inode
     305             :  * @data_len: length of the data attached to the inode
     306             :  * @data: inode's data
     307             :  */
     308             : struct ubifs_inode {
     309             :         struct inode vfs_inode;
     310             :         unsigned long long creat_sqnum;
     311             :         unsigned int xattr_size;
     312             :         unsigned int xattr_cnt;
     313             :         unsigned int xattr_names;
     314             :         unsigned int compr_type:2;
     315             :         loff_t ui_size;
     316             :         int flags;
     317             :         int data_len;
     318             :         void *data;
     319             : };
     320             : 
     321             : /**
     322             :  * struct ubifs_unclean_leb - records a LEB recovered under read-only mode.
     323             :  * @list: list
     324             :  * @lnum: LEB number of recovered LEB
     325             :  * @endpt: offset where recovery ended
     326             :  *
     327             :  * This structure records a LEB identified during recovery that needs to be
     328             :  * cleaned but was not because UBIFS was mounted read-only. The information
     329             :  * is used to clean the LEB when remounting to read-write mode.
     330             :  */
     331             : struct ubifs_unclean_leb {
     332             :         struct list_head list;
     333             :         int lnum;
     334             :         int endpt;
     335             : };
     336             : 
     337             : /*
     338             :  * LEB properties flags.
     339             :  *
     340             :  * LPROPS_UNCAT: not categorized
     341             :  * LPROPS_DIRTY: dirty > free, dirty >= @c->dead_wm, not index
     342             :  * LPROPS_DIRTY_IDX: dirty + free > @c->min_idx_node_sze and index
     343             :  * LPROPS_FREE: free > 0, dirty < @c->dead_wm, not empty, not index
     344             :  * LPROPS_HEAP_CNT: number of heaps used for storing categorized LEBs
     345             :  * LPROPS_EMPTY: LEB is empty, not taken
     346             :  * LPROPS_FREEABLE: free + dirty == leb_size, not index, not taken
     347             :  * LPROPS_FRDI_IDX: free + dirty == leb_size and index, may be taken
     348             :  * LPROPS_CAT_MASK: mask for the LEB categories above
     349             :  * LPROPS_TAKEN: LEB was taken (this flag is not saved on the media)
     350             :  * LPROPS_INDEX: LEB contains indexing nodes (this flag also exists on flash)
     351             :  */
     352             : enum {
     353             :         LPROPS_UNCAT     =  0,
     354             :         LPROPS_DIRTY     =  1,
     355             :         LPROPS_DIRTY_IDX =  2,
     356             :         LPROPS_FREE      =  3,
     357             :         LPROPS_HEAP_CNT  =  3,
     358             :         LPROPS_EMPTY     =  4,
     359             :         LPROPS_FREEABLE  =  5,
     360             :         LPROPS_FRDI_IDX  =  6,
     361             :         LPROPS_CAT_MASK  = 15,
     362             :         LPROPS_TAKEN     = 16,
     363             :         LPROPS_INDEX     = 32,
     364             : };
     365             : 
     366             : /**
     367             :  * struct ubifs_lprops - logical eraseblock properties.
     368             :  * @free: amount of free space in bytes
     369             :  * @dirty: amount of dirty space in bytes
     370             :  * @flags: LEB properties flags (see above)
     371             :  * @lnum: LEB number
     372             :  * @end: the end postition of LEB calculated by the last node
     373             :  * @used: amount of used space in bytes
     374             :  * @list: list of same-category lprops (for LPROPS_EMPTY and LPROPS_FREEABLE)
     375             :  * @hpos: heap position in heap of same-category lprops (other categories)
     376             :  */
     377             : struct ubifs_lprops {
     378             :         int free;
     379             :         int dirty;
     380             :         int flags;
     381             :         int lnum;
     382             :         int end;
     383             :         int used;
     384             :         union {
     385             :                 struct list_head list;
     386             :                 int hpos;
     387             :         };
     388             : };
     389             : 
     390             : /**
     391             :  * struct ubifs_lpt_lprops - LPT logical eraseblock properties.
     392             :  * @free: amount of free space in bytes
     393             :  * @dirty: amount of dirty space in bytes
     394             :  * @tgc: trivial GC flag (1 => unmap after commit end)
     395             :  * @cmt: commit flag (1 => reserved for commit)
     396             :  */
     397             : struct ubifs_lpt_lprops {
     398             :         int free;
     399             :         int dirty;
     400             :         unsigned tgc:1;
     401             :         unsigned cmt:1;
     402             : };
     403             : 
     404             : /**
     405             :  * struct ubifs_lp_stats - statistics of eraseblocks in the main area.
     406             :  * @empty_lebs: number of empty LEBs
     407             :  * @taken_empty_lebs: number of taken LEBs
     408             :  * @idx_lebs: number of indexing LEBs
     409             :  * @total_free: total free space in bytes (includes all LEBs)
     410             :  * @total_dirty: total dirty space in bytes (includes all LEBs)
     411             :  * @total_used: total used space in bytes (does not include index LEBs)
     412             :  * @total_dead: total dead space in bytes (does not include index LEBs)
     413             :  * @total_dark: total dark space in bytes (does not include index LEBs)
     414             :  *
     415             :  * The @taken_empty_lebs field counts the LEBs that are in the transient state
     416             :  * of having been "taken" for use but not yet written to. @taken_empty_lebs is
     417             :  * needed to account correctly for @gc_lnum, otherwise @empty_lebs could be
     418             :  * used by itself (in which case 'unused_lebs' would be a better name). In the
     419             :  * case of @gc_lnum, it is "taken" at mount time or whenever a LEB is retained
     420             :  * by GC, but unlike other empty LEBs that are "taken", it may not be written
     421             :  * straight away (i.e. before the next commit start or unmount), so either
     422             :  * @gc_lnum must be specially accounted for, or the current approach followed
     423             :  * i.e. count it under @taken_empty_lebs.
     424             :  *
     425             :  * @empty_lebs includes @taken_empty_lebs.
     426             :  *
     427             :  * @total_used, @total_dead and @total_dark fields do not account indexing
     428             :  * LEBs.
     429             :  */
     430             : struct ubifs_lp_stats {
     431             :         int empty_lebs;
     432             :         int taken_empty_lebs;
     433             :         int idx_lebs;
     434             :         long long total_free;
     435             :         long long total_dirty;
     436             :         long long total_used;
     437             :         long long total_dead;
     438             :         long long total_dark;
     439             : };
     440             : 
     441             : struct ubifs_nnode;
     442             : 
     443             : /**
     444             :  * struct ubifs_cnode - LEB Properties Tree common node.
     445             :  * @parent: parent nnode
     446             :  * @cnext: next cnode to commit
     447             :  * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
     448             :  * @iip: index in parent
     449             :  * @level: level in the tree (zero for pnodes, greater than zero for nnodes)
     450             :  * @num: node number
     451             :  */
     452             : struct ubifs_cnode {
     453             :         struct ubifs_nnode *parent;
     454             :         struct ubifs_cnode *cnext;
     455             :         unsigned long flags;
     456             :         int iip;
     457             :         int level;
     458             :         int num;
     459             : };
     460             : 
     461             : /**
     462             :  * struct ubifs_pnode - LEB Properties Tree leaf node.
     463             :  * @parent: parent nnode
     464             :  * @cnext: next cnode to commit
     465             :  * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
     466             :  * @iip: index in parent
     467             :  * @level: level in the tree (always zero for pnodes)
     468             :  * @num: node number
     469             :  * @lprops: LEB properties array
     470             :  */
     471             : struct ubifs_pnode {
     472             :         struct ubifs_nnode *parent;
     473             :         struct ubifs_cnode *cnext;
     474             :         unsigned long flags;
     475             :         int iip;
     476             :         int level;
     477             :         int num;
     478             :         struct ubifs_lprops lprops[UBIFS_LPT_FANOUT];
     479             : };
     480             : 
     481             : /**
     482             :  * struct ubifs_nbranch - LEB Properties Tree internal node branch.
     483             :  * @lnum: LEB number of child
     484             :  * @offs: offset of child
     485             :  * @nnode: nnode child
     486             :  * @pnode: pnode child
     487             :  * @cnode: cnode child
     488             :  */
     489             : struct ubifs_nbranch {
     490             :         int lnum;
     491             :         int offs;
     492             :         union {
     493             :                 struct ubifs_nnode *nnode;
     494             :                 struct ubifs_pnode *pnode;
     495             :                 struct ubifs_cnode *cnode;
     496             :         };
     497             : };
     498             : 
     499             : /**
     500             :  * struct ubifs_nnode - LEB Properties Tree internal node.
     501             :  * @parent: parent nnode
     502             :  * @cnext: next cnode to commit
     503             :  * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
     504             :  * @iip: index in parent
     505             :  * @level: level in the tree (always greater than zero for nnodes)
     506             :  * @num: node number
     507             :  * @nbranch: branches to child nodes
     508             :  */
     509             : struct ubifs_nnode {
     510             :         struct ubifs_nnode *parent;
     511             :         struct ubifs_cnode *cnext;
     512             :         unsigned long flags;
     513             :         int iip;
     514             :         int level;
     515             :         int num;
     516             :         struct ubifs_nbranch nbranch[UBIFS_LPT_FANOUT];
     517             : };
     518             : 
     519             : /**
     520             :  * struct ubifs_lpt_heap - heap of categorized lprops.
     521             :  * @arr: heap array
     522             :  * @cnt: number in heap
     523             :  * @max_cnt: maximum number allowed in heap
     524             :  *
     525             :  * There are %LPROPS_HEAP_CNT heaps.
     526             :  */
     527             : struct ubifs_lpt_heap {
     528             :         struct ubifs_lprops **arr;
     529             :         int cnt;
     530             :         int max_cnt;
     531             : };
     532             : 
     533             : /*
     534             :  * Return codes for LPT scan callback function.
     535             :  *
     536             :  * LPT_SCAN_CONTINUE: continue scanning
     537             :  * LPT_SCAN_ADD: add the LEB properties scanned to the tree in memory
     538             :  * LPT_SCAN_STOP: stop scanning
     539             :  */
     540             : enum {
     541             :         LPT_SCAN_CONTINUE = 0,
     542             :         LPT_SCAN_ADD = 1,
     543             :         LPT_SCAN_STOP = 2,
     544             : };
     545             : 
     546             : struct ubifs_info;
     547             : 
     548             : /* Callback used by the 'ubifs_lpt_scan_nolock()' function */
     549             : typedef int (*ubifs_lpt_scan_callback)(struct ubifs_info *c,
     550             :                                        const struct ubifs_lprops *lprops,
     551             :                                        int in_tree, void *data);
     552             : 
     553             : /**
     554             :  * struct ubifs_wbuf - UBIFS write-buffer.
     555             :  * @c: UBIFS file-system description object
     556             :  * @buf: write-buffer (of min. flash I/O unit size)
     557             :  * @lnum: logical eraseblock number the write-buffer points to
     558             :  * @offs: write-buffer offset in this logical eraseblock
     559             :  * @avail: number of bytes available in the write-buffer
     560             :  * @used:  number of used bytes in the write-buffer
     561             :  * @size: write-buffer size (in [@c->min_io_size, @c->max_write_size] range)
     562             :  * @jhead: journal head the mutex belongs to (note, needed only to shut lockdep
     563             :  *         up by 'mutex_lock_nested()).
     564             :  * @sync_callback: write-buffer synchronization callback
     565             :  * @io_mutex: serializes write-buffer I/O
     566             :  * @lock: serializes @buf, @lnum, @offs, @avail, @used, @next_ino and @inodes
     567             :  *        fields
     568             :  * @next_ino: points to the next position of the following inode number
     569             :  * @inodes: stores the inode numbers of the nodes which are in wbuf
     570             :  *
     571             :  * The write-buffer synchronization callback is called when the write-buffer is
     572             :  * synchronized in order to notify how much space was wasted due to
     573             :  * write-buffer padding and how much free space is left in the LEB.
     574             :  *
     575             :  * Note: the fields @buf, @lnum, @offs, @avail and @used can be read under
     576             :  * spin-lock or mutex because they are written under both mutex and spin-lock.
     577             :  * @buf is appended to under mutex but overwritten under both mutex and
     578             :  * spin-lock. Thus the data between @buf and @buf + @used can be read under
     579             :  * spinlock.
     580             :  */
     581             : struct ubifs_wbuf {
     582             :         struct ubifs_info *c;
     583             :         void *buf;
     584             :         int lnum;
     585             :         int offs;
     586             :         int avail;
     587             :         int used;
     588             :         int size;
     589             :         int jhead;
     590             :         int (*sync_callback)(struct ubifs_info *c, int lnum, int free, int pad);
     591             :         struct mutex io_mutex;
     592             :         spinlock_t lock;
     593             :         int next_ino;
     594             :         ino_t *inodes;
     595             : };
     596             : 
     597             : /**
     598             :  * struct ubifs_bud - bud logical eraseblock.
     599             :  * @lnum: logical eraseblock number
     600             :  * @start: where the (uncommitted) bud data starts
     601             :  * @jhead: journal head number this bud belongs to
     602             :  * @list: link in the list buds belonging to the same journal head
     603             :  * @rb: link in the tree of all buds
     604             :  * @log_hash: the log hash from the commit start node up to this bud
     605             :  */
     606             : struct ubifs_bud {
     607             :         int lnum;
     608             :         int start;
     609             :         int jhead;
     610             :         struct list_head list;
     611             :         struct rb_node rb;
     612             :         struct shash_desc *log_hash;
     613             : };
     614             : 
     615             : /**
     616             :  * struct ubifs_jhead - journal head.
     617             :  * @wbuf: head's write-buffer
     618             :  * @buds_list: list of bud LEBs belonging to this journal head
     619             :  * @grouped: non-zero if UBIFS groups nodes when writing to this journal head
     620             :  * @log_hash: the log hash from the commit start node up to this journal head
     621             :  *
     622             :  * Note, the @buds list is protected by the @c->buds_lock.
     623             :  */
     624             : struct ubifs_jhead {
     625             :         struct ubifs_wbuf wbuf;
     626             :         struct list_head buds_list;
     627             :         unsigned int grouped:1;
     628             :         struct shash_desc *log_hash;
     629             : };
     630             : 
     631             : /**
     632             :  * struct ubifs_zbranch - key/coordinate/length branch stored in znodes.
     633             :  * @key: key
     634             :  * @znode: znode address in memory
     635             :  * @lnum: LEB number of the target node (indexing node or data node)
     636             :  * @offs: target node offset within @lnum
     637             :  * @len: target node length
     638             :  * @hash: the hash of the target node
     639             :  */
     640             : struct ubifs_zbranch {
     641             :         union ubifs_key key;
     642             :         union {
     643             :                 struct ubifs_znode *znode;
     644             :                 void *leaf;
     645             :         };
     646             :         int lnum;
     647             :         int offs;
     648             :         int len;
     649             :         u8 hash[UBIFS_HASH_ARR_SZ];
     650             : };
     651             : 
     652             : /**
     653             :  * struct ubifs_znode - in-memory representation of an indexing node.
     654             :  * @parent: parent znode or NULL if it is the root
     655             :  * @cnext: next znode to commit
     656             :  * @cparent: parent node for this commit
     657             :  * @ciip: index in cparent's zbranch array
     658             :  * @flags: znode flags (%DIRTY_ZNODE, %COW_ZNODE or %OBSOLETE_ZNODE)
     659             :  * @time: last access time (seconds)
     660             :  * @level: level of the entry in the TNC tree
     661             :  * @child_cnt: count of child znodes
     662             :  * @iip: index in parent's zbranch array
     663             :  * @alt: lower bound of key range has altered i.e. child inserted at slot 0
     664             :  * @lnum: LEB number of the corresponding indexing node
     665             :  * @offs: offset of the corresponding indexing node
     666             :  * @len: length  of the corresponding indexing node
     667             :  * @zbranch: array of znode branches (@c->fanout elements)
     668             :  *
     669             :  * Note! The @lnum, @offs, and @len fields are not really needed - we have them
     670             :  * only for internal consistency check. They could be removed to save some RAM.
     671             :  */
     672             : struct ubifs_znode {
     673             :         struct ubifs_znode *parent;
     674             :         struct ubifs_znode *cnext;
     675             :         struct ubifs_znode *cparent;
     676             :         int ciip;
     677             :         unsigned long flags;
     678             :         time64_t time;
     679             :         int level;
     680             :         int child_cnt;
     681             :         int iip;
     682             :         int alt;
     683             :         int lnum;
     684             :         int offs;
     685             :         int len;
     686             :         struct ubifs_zbranch zbranch[];
     687             : };
     688             : 
     689             : /**
     690             :  * struct ubifs_node_range - node length range description data structure.
     691             :  * @len: fixed node length
     692             :  * @min_len: minimum possible node length
     693             :  * @max_len: maximum possible node length
     694             :  *
     695             :  * If @max_len is %0, the node has fixed length @len.
     696             :  */
     697             : struct ubifs_node_range {
     698             :         union {
     699             :                 int len;
     700             :                 int min_len;
     701             :         };
     702             :         int max_len;
     703             : };
     704             : 
     705             : /**
     706             :  * struct ubifs_budget_req - budget requirements of an operation.
     707             :  *
     708             :  * @fast: non-zero if the budgeting should try to acquire budget quickly and
     709             :  *        should not try to call write-back
     710             :  * @recalculate: non-zero if @idx_growth, @data_growth, and @dd_growth fields
     711             :  *               have to be re-calculated
     712             :  * @new_page: non-zero if the operation adds a new page
     713             :  * @dirtied_page: non-zero if the operation makes a page dirty
     714             :  * @new_dent: non-zero if the operation adds a new directory entry
     715             :  * @mod_dent: non-zero if the operation removes or modifies an existing
     716             :  *            directory entry
     717             :  * @new_ino: non-zero if the operation adds a new inode
     718             :  * @new_ino_d: how much data newly created inode contains
     719             :  * @dirtied_ino: how many inodes the operation makes dirty
     720             :  * @dirtied_ino_d: how much data dirtied inode contains
     721             :  * @idx_growth: how much the index will supposedly grow
     722             :  * @data_growth: how much new data the operation will supposedly add
     723             :  * @dd_growth: how much data that makes other data dirty the operation will
     724             :  *             supposedly add
     725             :  *
     726             :  * @idx_growth, @data_growth and @dd_growth are not used in budget request. The
     727             :  * budgeting subsystem caches index and data growth values there to avoid
     728             :  * re-calculating them when the budget is released. However, if @idx_growth is
     729             :  * %-1, it is calculated by the release function using other fields.
     730             :  *
     731             :  * An inode may contain 4KiB of data at max., thus the widths of @new_ino_d
     732             :  * is 13 bits, and @dirtied_ino_d - 15, because up to 4 inodes may be made
     733             :  * dirty by the re-name operation.
     734             :  *
     735             :  * Note, UBIFS aligns node lengths to 8-bytes boundary, so the requester has to
     736             :  * make sure the amount of inode data which contribute to @new_ino_d and
     737             :  * @dirtied_ino_d fields are aligned.
     738             :  */
     739             : struct ubifs_budget_req {
     740             :         unsigned int fast:1;
     741             :         unsigned int recalculate:1;
     742             : #ifndef UBIFS_DEBUG
     743             :         unsigned int new_page:1;
     744             :         unsigned int dirtied_page:1;
     745             :         unsigned int new_dent:1;
     746             :         unsigned int mod_dent:1;
     747             :         unsigned int new_ino:1;
     748             :         unsigned int new_ino_d:13;
     749             :         unsigned int dirtied_ino:4;
     750             :         unsigned int dirtied_ino_d:15;
     751             : #else
     752             :         /* Not bit-fields to check for overflows */
     753             :         unsigned int new_page;
     754             :         unsigned int dirtied_page;
     755             :         unsigned int new_dent;
     756             :         unsigned int mod_dent;
     757             :         unsigned int new_ino;
     758             :         unsigned int new_ino_d;
     759             :         unsigned int dirtied_ino;
     760             :         unsigned int dirtied_ino_d;
     761             : #endif
     762             :         int idx_growth;
     763             :         int data_growth;
     764             :         int dd_growth;
     765             : };
     766             : 
     767             : /**
     768             :  * struct ubifs_orphan - stores the inode number of an orphan.
     769             :  * @rb: rb-tree node of rb-tree of orphans sorted by inode number
     770             :  * @list: list head of list of orphans in order added
     771             :  * @new_list: list head of list of orphans added since the last commit
     772             :  * @cnext: next orphan to commit
     773             :  * @dnext: next orphan to delete
     774             :  * @inum: inode number
     775             :  * @new: %1 => added since the last commit, otherwise %0
     776             :  * @cmt: %1 => commit pending, otherwise %0
     777             :  * @del: %1 => delete pending, otherwise %0
     778             :  */
     779             : struct ubifs_orphan {
     780             :         struct rb_node rb;
     781             :         struct list_head list;
     782             :         struct list_head new_list;
     783             :         struct ubifs_orphan *cnext;
     784             :         struct ubifs_orphan *dnext;
     785             :         ino_t inum;
     786             :         unsigned new:1;
     787             :         unsigned cmt:1;
     788             :         unsigned del:1;
     789             : };
     790             : 
     791             : /**
     792             :  * struct ubifs_budg_info - UBIFS budgeting information.
     793             :  * @idx_growth: amount of bytes budgeted for index growth
     794             :  * @data_growth: amount of bytes budgeted for cached data
     795             :  * @dd_growth: amount of bytes budgeted for cached data that will make
     796             :  *             other data dirty
     797             :  * @uncommitted_idx: amount of bytes were budgeted for growth of the index, but
     798             :  *                   which still have to be taken into account because the index
     799             :  *                   has not been committed so far
     800             :  * @old_idx_sz: size of index on flash
     801             :  * @min_idx_lebs: minimum number of LEBs required for the index
     802             :  * @nospace: non-zero if the file-system does not have flash space (used as
     803             :  *           optimization)
     804             :  * @nospace_rp: the same as @nospace, but additionally means that even reserved
     805             :  *              pool is full
     806             :  * @page_budget: budget for a page (constant, never changed after mount)
     807             :  * @inode_budget: budget for an inode (constant, never changed after mount)
     808             :  * @dent_budget: budget for a directory entry (constant, never changed after
     809             :  *               mount)
     810             :  */
     811             : struct ubifs_budg_info {
     812             :         long long idx_growth;
     813             :         long long data_growth;
     814             :         long long dd_growth;
     815             :         long long uncommitted_idx;
     816             :         unsigned long long old_idx_sz;
     817             :         int min_idx_lebs;
     818             :         unsigned int nospace:1;
     819             :         unsigned int nospace_rp:1;
     820             :         int page_budget;
     821             :         int inode_budget;
     822             :         int dent_budget;
     823             : };
     824             : 
     825             : /**
     826             :  * struct ubifs_info - UBIFS file-system description data structure
     827             :  * (per-superblock).
     828             :  *
     829             :  * @sup_node: The super block node as read from the device
     830             :  *
     831             :  * @highest_inum: highest used inode number
     832             :  * @max_sqnum: current global sequence number
     833             :  * @cmt_no: commit number of the last successfully completed commit, protected
     834             :  *          by @commit_sem
     835             :  * @cnt_lock: protects @highest_inum and @max_sqnum counters
     836             :  * @fmt_version: UBIFS on-flash format version
     837             :  * @ro_compat_version: R/O compatibility version
     838             :  *
     839             :  * @debug_level: level of debug messages, 0 - none, 1 - error message,
     840             :  *               2 - warning message, 3 - notice message, 4 - debug message
     841             :  * @program_type: used to identify the type of current program
     842             :  * @program_name: program name
     843             :  * @dev_name: device name
     844             :  * @dev_fd: opening handler for an UBI volume or an image file
     845             :  * @libubi: opening handler for libubi
     846             :  *
     847             :  * @lhead_lnum: log head logical eraseblock number
     848             :  * @lhead_offs: log head offset
     849             :  * @ltail_lnum: log tail logical eraseblock number (offset is always 0)
     850             :  * @log_mutex: protects the log, @lhead_lnum, @lhead_offs, @ltail_lnum, and
     851             :  *             @bud_bytes
     852             :  * @min_log_bytes: minimum required number of bytes in the log
     853             :  * @cmt_bud_bytes: used during commit to temporarily amount of bytes in
     854             :  *                 committed buds
     855             :  *
     856             :  * @buds: tree of all buds indexed by bud LEB number
     857             :  * @bud_bytes: how many bytes of flash is used by buds
     858             :  * @buds_lock: protects the @buds tree, @bud_bytes, and per-journal head bud
     859             :  *             lists
     860             :  * @jhead_cnt: count of journal heads
     861             :  * @jheads: journal heads (head zero is base head)
     862             :  * @max_bud_bytes: maximum number of bytes allowed in buds
     863             :  * @bg_bud_bytes: number of bud bytes when background commit is initiated
     864             :  * @old_buds: buds to be released after commit ends
     865             :  * @max_bud_cnt: maximum number of buds
     866             :  *
     867             :  * @commit_sem: synchronizes committer with other processes
     868             :  * @cmt_state: commit state
     869             :  * @cs_lock: commit state lock
     870             :  *
     871             :  * @big_lpt: flag that LPT is too big to write whole during commit
     872             :  * @space_fixup: flag indicating that free space in LEBs needs to be cleaned up
     873             :  * @double_hash: flag indicating that we can do lookups by hash
     874             :  * @encrypted: flag indicating that this file system contains encrypted files
     875             :  * @no_chk_data_crc: do not check CRCs when reading data nodes (except during
     876             :  *                   recovery)
     877             :  * @authenticated: flag indigating the FS is mounted in authenticated mode
     878             :  * @superblock_need_write: flag indicating that we need to write superblock node
     879             :  *
     880             :  * @tnc_mutex: protects the Tree Node Cache (TNC), @zroot, @cnext, @enext, and
     881             :  *             @calc_idx_sz
     882             :  * @zroot: zbranch which points to the root index node and znode
     883             :  * @cnext: next znode to commit
     884             :  * @enext: next znode to commit to empty space
     885             :  * @gap_lebs: array of LEBs used by the in-gaps commit method
     886             :  * @cbuf: commit buffer
     887             :  * @ileb_buf: buffer for commit in-the-gaps method
     888             :  * @ileb_len: length of data in ileb_buf
     889             :  * @ihead_lnum: LEB number of index head
     890             :  * @ihead_offs: offset of index head
     891             :  * @ilebs: pre-allocated index LEBs
     892             :  * @ileb_cnt: number of pre-allocated index LEBs
     893             :  * @ileb_nxt: next pre-allocated index LEBs
     894             :  * @old_idx: tree of index nodes obsoleted since the last commit start
     895             :  * @bottom_up_buf: a buffer which is used by 'dirty_cow_bottom_up()' in tnc.c
     896             :  *
     897             :  * @mst_node: master node
     898             :  * @mst_offs: offset of valid master node
     899             :  *
     900             :  * @log_lebs: number of logical eraseblocks in the log
     901             :  * @log_bytes: log size in bytes
     902             :  * @log_last: last LEB of the log
     903             :  * @lpt_lebs: number of LEBs used for lprops table
     904             :  * @lpt_first: first LEB of the lprops table area
     905             :  * @lpt_last: last LEB of the lprops table area
     906             :  * @orph_lebs: number of LEBs used for the orphan area
     907             :  * @orph_first: first LEB of the orphan area
     908             :  * @orph_last: last LEB of the orphan area
     909             :  * @main_lebs: count of LEBs in the main area
     910             :  * @main_first: first LEB of the main area
     911             :  * @main_bytes: main area size in bytes
     912             :  * @default_compr: default compression type
     913             :  * @favor_lzo: favor LZO compression method
     914             :  * @favor_percent: lzo vs. zlib threshold used in case favor LZO
     915             :  *
     916             :  * @key_hash_type: type of the key hash
     917             :  * @key_hash: direntry key hash function
     918             :  * @key_fmt: key format
     919             :  * @key_len: key length
     920             :  * @fanout: fanout of the index tree (number of links per indexing node)
     921             :  *
     922             :  * @min_io_size: minimal input/output unit size
     923             :  * @min_io_shift: number of bits in @min_io_size minus one
     924             :  * @max_write_size: maximum amount of bytes the underlying flash can write at a
     925             :  *                  time (MTD write buffer size)
     926             :  * @max_write_shift: number of bits in @max_write_size minus one
     927             :  * @leb_size: logical eraseblock size in bytes
     928             :  * @half_leb_size: half LEB size
     929             :  * @idx_leb_size: how many bytes of an LEB are effectively available when it is
     930             :  *                used to store indexing nodes (@leb_size - @max_idx_node_sz)
     931             :  * @leb_cnt: count of logical eraseblocks
     932             :  * @max_leb_cnt: maximum count of logical eraseblocks
     933             :  * @ro_media: the underlying UBI volume is read-only
     934             :  * @ro_mount: the file-system was mounted as read-only
     935             :  * @ro_error: UBIFS switched to R/O mode because an error happened
     936             :  *
     937             :  * @dirty_pg_cnt: number of dirty pages (not used)
     938             :  * @dirty_zn_cnt: number of dirty znodes
     939             :  * @clean_zn_cnt: number of clean znodes
     940             :  *
     941             :  * @space_lock: protects @bi and @lst
     942             :  * @lst: lprops statistics
     943             :  * @bi: budgeting information
     944             :  * @calc_idx_sz: temporary variable which is used to calculate new index size
     945             :  *               (contains accurate new index size at end of TNC commit start)
     946             :  *
     947             :  * @ref_node_alsz: size of the LEB reference node aligned to the min. flash
     948             :  *                 I/O unit
     949             :  * @mst_node_alsz: master node aligned size
     950             :  * @min_idx_node_sz: minimum indexing node aligned on 8-bytes boundary
     951             :  * @max_idx_node_sz: maximum indexing node aligned on 8-bytes boundary
     952             :  * @max_inode_sz: maximum possible inode size in bytes
     953             :  * @max_znode_sz: size of znode in bytes
     954             :  *
     955             :  * @leb_overhead: how many bytes are wasted in an LEB when it is filled with
     956             :  *                data nodes of maximum size - used in free space reporting
     957             :  * @dead_wm: LEB dead space watermark
     958             :  * @dark_wm: LEB dark space watermark
     959             :  *
     960             :  * @ranges: UBIFS node length ranges
     961             :  * @di: UBI device information
     962             :  * @vi: UBI volume information
     963             :  *
     964             :  * @orph_tree: rb-tree of orphan inode numbers
     965             :  * @orph_list: list of orphan inode numbers in order added
     966             :  * @orph_new: list of orphan inode numbers added since last commit
     967             :  * @orph_cnext: next orphan to commit
     968             :  * @orph_dnext: next orphan to delete
     969             :  * @orphan_lock: lock for orph_tree and orph_new
     970             :  * @orph_buf: buffer for orphan nodes
     971             :  * @new_orphans: number of orphans since last commit
     972             :  * @cmt_orphans: number of orphans being committed
     973             :  * @tot_orphans: number of orphans in the rb_tree
     974             :  * @max_orphans: maximum number of orphans allowed
     975             :  * @ohead_lnum: orphan head LEB number
     976             :  * @ohead_offs: orphan head offset
     977             :  * @no_orphs: non-zero if there are no orphans
     978             :  *
     979             :  * @gc_lnum: LEB number used for garbage collection
     980             :  * @sbuf: a buffer of LEB size used by GC and replay for scanning
     981             :  * @idx_gc: list of index LEBs that have been garbage collected
     982             :  * @idx_gc_cnt: number of elements on the idx_gc list
     983             :  * @gc_seq: incremented for every non-index LEB garbage collected
     984             :  * @gced_lnum: last non-index LEB that was garbage collected
     985             :  *
     986             :  * @space_bits: number of bits needed to record free or dirty space
     987             :  * @lpt_lnum_bits: number of bits needed to record a LEB number in the LPT
     988             :  * @lpt_offs_bits: number of bits needed to record an offset in the LPT
     989             :  * @lpt_spc_bits: number of bits needed to space in the LPT
     990             :  * @pcnt_bits: number of bits needed to record pnode or nnode number
     991             :  * @lnum_bits: number of bits needed to record LEB number
     992             :  * @nnode_sz: size of on-flash nnode
     993             :  * @pnode_sz: size of on-flash pnode
     994             :  * @ltab_sz: size of on-flash LPT lprops table
     995             :  * @lsave_sz: size of on-flash LPT save table
     996             :  * @pnode_cnt: number of pnodes
     997             :  * @nnode_cnt: number of nnodes
     998             :  * @lpt_hght: height of the LPT
     999             :  * @pnodes_have: number of pnodes in memory
    1000             :  *
    1001             :  * @lp_mutex: protects lprops table and all the other lprops-related fields
    1002             :  * @lpt_lnum: LEB number of the root nnode of the LPT
    1003             :  * @lpt_offs: offset of the root nnode of the LPT
    1004             :  * @nhead_lnum: LEB number of LPT head
    1005             :  * @nhead_offs: offset of LPT head
    1006             :  * @lpt_drty_flgs: dirty flags for LPT special nodes e.g. ltab
    1007             :  * @dirty_nn_cnt: number of dirty nnodes
    1008             :  * @dirty_pn_cnt: number of dirty pnodes
    1009             :  * @check_lpt_free: flag that indicates LPT GC may be needed
    1010             :  * @lpt_sz: LPT size
    1011             :  * @lpt_nod_buf: buffer for an on-flash nnode or pnode
    1012             :  * @lpt_buf: buffer of LEB size used by LPT
    1013             :  * @nroot: address in memory of the root nnode of the LPT
    1014             :  * @lpt_cnext: next LPT node to commit
    1015             :  * @lpt_heap: array of heaps of categorized lprops
    1016             :  * @dirty_idx: a (reverse sorted) copy of the LPROPS_DIRTY_IDX heap as at
    1017             :  *             previous commit start
    1018             :  * @uncat_list: list of un-categorized LEBs
    1019             :  * @empty_list: list of empty LEBs
    1020             :  * @freeable_list: list of freeable non-index LEBs (free + dirty == @leb_size)
    1021             :  * @frdi_idx_list: list of freeable index LEBs (free + dirty == @leb_size)
    1022             :  * @freeable_cnt: number of freeable LEBs in @freeable_list
    1023             :  * @in_a_category_cnt: count of lprops which are in a certain category, which
    1024             :  *                     basically meants that they were loaded from the flash
    1025             :  *
    1026             :  * @ltab_lnum: LEB number of LPT's own lprops table
    1027             :  * @ltab_offs: offset of LPT's own lprops table
    1028             :  * @lpt: lprops table
    1029             :  * @ltab: LPT's own lprops table
    1030             :  * @ltab_cmt: LPT's own lprops table (commit copy)
    1031             :  * @lsave_cnt: number of LEB numbers in LPT's save table
    1032             :  * @lsave_lnum: LEB number of LPT's save table
    1033             :  * @lsave_offs: offset of LPT's save table
    1034             :  * @lsave: LPT's save table
    1035             :  * @lscan_lnum: LEB number of last LPT scan
    1036             :  *
    1037             :  * @rp_size: reserved pool size
    1038             :  *
    1039             :  * @hash_algo_name: the name of the hashing algorithm to use
    1040             :  * @hash_algo: The hash algo number (from include/linux/hash_info.h)
    1041             :  * @auth_key_filename: authentication key file name
    1042             :  * @x509_filename: x509 certificate file name for authentication
    1043             :  * @hash_len: the length of the hash
    1044             :  * @root_idx_hash: The hash of the root index node
    1045             :  * @lpt_hash: The hash of the LPT
    1046             :  * @mst_hash: The hash of the master node
    1047             :  * @log_hash: the log hash from the commit start node up to the latest reference
    1048             :  *            node.
    1049             :  *
    1050             :  * @need_recovery: %1 if the file-system needs recovery
    1051             :  * @replaying: %1 during journal replay
    1052             :  * @mounting: %1 while mounting
    1053             :  * @remounting_rw: %1 while re-mounting from R/O mode to R/W mode
    1054             :  * @replay_list: temporary list used during journal replay
    1055             :  * @replay_buds: list of buds to replay
    1056             :  * @cs_sqnum: sequence number of first node in the log (commit start node)
    1057             :  * @unclean_leb_list: LEBs to recover when re-mounting R/O mounted FS to R/W
    1058             :  *                    mode
    1059             :  * @rcvrd_mst_node: recovered master node to write when re-mounting R/O mounted
    1060             :  *                  FS to R/W mode
    1061             :  * @size_tree: inode size information for recovery
    1062             :  *
    1063             :  * @new_ihead_lnum: used by debugging to check @c->ihead_lnum
    1064             :  * @new_ihead_offs: used by debugging to check @c->ihead_offs
    1065             :  *
    1066             :  * @private: private information related to specific situation, eg. fsck.
    1067             :  * @assert_failed_cb: callback function to handle assertion failure
    1068             :  * @set_failure_reason_cb: record reasons while certain failure happens
    1069             :  * @get_failure_reason_cb: get failure reasons
    1070             :  * @clear_failure_reason_cb: callback function to clear the error which is
    1071             :  *                           caused by reading corrupted data or invalid lpt
    1072             :  * @test_and_clear_failure_reason_cb: callback function to check and clear the
    1073             :  *                                    error which is caused by reading corrupted
    1074             :  *                                    data or invalid lpt
    1075             :  * @set_lpt_invalid_cb: callback function to set the invalid lpt status
    1076             :  * @test_lpt_valid_cb: callback function to check whether lpt is corrupted or
    1077             :  *                     incorrect, should be called before updating lpt
    1078             :  * @can_ignore_failure_cb: callback function to decide whether the failure
    1079             :  *                         can be ignored
    1080             :  * @handle_failure_cb: callback function to decide whether the failure can be
    1081             :  *                     handled
    1082             :  */
    1083             : struct ubifs_info {
    1084             :         struct ubifs_sb_node *sup_node;
    1085             : 
    1086             :         ino_t highest_inum;
    1087             :         unsigned long long max_sqnum;
    1088             :         unsigned long long cmt_no;
    1089             :         spinlock_t cnt_lock;
    1090             :         int fmt_version;
    1091             :         int ro_compat_version;
    1092             : 
    1093             :         int debug_level;
    1094             :         int program_type;
    1095             :         const char *program_name;
    1096             :         char *dev_name;
    1097             :         int dev_fd;
    1098             :         libubi_t libubi;
    1099             : 
    1100             :         int lhead_lnum;
    1101             :         int lhead_offs;
    1102             :         int ltail_lnum;
    1103             :         struct mutex log_mutex;
    1104             :         int min_log_bytes;
    1105             :         long long cmt_bud_bytes;
    1106             : 
    1107             :         struct rb_root buds;
    1108             :         long long bud_bytes;
    1109             :         spinlock_t buds_lock;
    1110             :         int jhead_cnt;
    1111             :         struct ubifs_jhead *jheads;
    1112             :         long long max_bud_bytes;
    1113             :         long long bg_bud_bytes;
    1114             :         struct list_head old_buds;
    1115             :         int max_bud_cnt;
    1116             : 
    1117             :         struct rw_semaphore commit_sem;
    1118             :         int cmt_state;
    1119             :         spinlock_t cs_lock;
    1120             : 
    1121             :         unsigned int big_lpt:1;
    1122             :         unsigned int space_fixup:1;
    1123             :         unsigned int double_hash:1;
    1124             :         unsigned int encrypted:1;
    1125             :         unsigned int no_chk_data_crc:1;
    1126             :         unsigned int authenticated:1;
    1127             :         unsigned int superblock_need_write:1;
    1128             : 
    1129             :         struct mutex tnc_mutex;
    1130             :         struct ubifs_zbranch zroot;
    1131             :         struct ubifs_znode *cnext;
    1132             :         struct ubifs_znode *enext;
    1133             :         int *gap_lebs;
    1134             :         void *cbuf;
    1135             :         void *ileb_buf;
    1136             :         int ileb_len;
    1137             :         int ihead_lnum;
    1138             :         int ihead_offs;
    1139             :         int *ilebs;
    1140             :         int ileb_cnt;
    1141             :         int ileb_nxt;
    1142             :         struct rb_root old_idx;
    1143             :         int *bottom_up_buf;
    1144             : 
    1145             :         struct ubifs_mst_node *mst_node;
    1146             :         int mst_offs;
    1147             : 
    1148             :         int log_lebs;
    1149             :         long long log_bytes;
    1150             :         int log_last;
    1151             :         int lpt_lebs;
    1152             :         int lpt_first;
    1153             :         int lpt_last;
    1154             :         int orph_lebs;
    1155             :         int orph_first;
    1156             :         int orph_last;
    1157             :         int main_lebs;
    1158             :         int main_first;
    1159             :         long long main_bytes;
    1160             :         int default_compr;
    1161             :         int favor_lzo;
    1162             :         int favor_percent;
    1163             : 
    1164             :         uint8_t key_hash_type;
    1165             :         uint32_t (*key_hash)(const char *str, int len);
    1166             :         int key_fmt;
    1167             :         int key_len;
    1168             :         int fanout;
    1169             : 
    1170             :         int min_io_size;
    1171             :         int min_io_shift;
    1172             :         int max_write_size;
    1173             :         int max_write_shift;
    1174             :         int leb_size;
    1175             :         int half_leb_size;
    1176             :         int idx_leb_size;
    1177             :         int leb_cnt;
    1178             :         int max_leb_cnt;
    1179             :         unsigned int ro_media:1;
    1180             :         unsigned int ro_mount:1;
    1181             :         unsigned int ro_error:1;
    1182             : 
    1183             :         atomic_long_t dirty_pg_cnt;
    1184             :         atomic_long_t dirty_zn_cnt;
    1185             :         atomic_long_t clean_zn_cnt;
    1186             : 
    1187             :         spinlock_t space_lock;
    1188             :         struct ubifs_lp_stats lst;
    1189             :         struct ubifs_budg_info bi;
    1190             :         unsigned long long calc_idx_sz;
    1191             : 
    1192             :         int ref_node_alsz;
    1193             :         int mst_node_alsz;
    1194             :         int min_idx_node_sz;
    1195             :         int max_idx_node_sz;
    1196             :         long long max_inode_sz;
    1197             :         int max_znode_sz;
    1198             : 
    1199             :         int leb_overhead;
    1200             :         int dead_wm;
    1201             :         int dark_wm;
    1202             : 
    1203             :         struct ubifs_node_range ranges[UBIFS_NODE_TYPES_CNT];
    1204             :         struct ubi_dev_info di;
    1205             :         struct ubi_vol_info vi;
    1206             : 
    1207             :         struct rb_root orph_tree;
    1208             :         struct list_head orph_list;
    1209             :         struct list_head orph_new;
    1210             :         struct ubifs_orphan *orph_cnext;
    1211             :         struct ubifs_orphan *orph_dnext;
    1212             :         spinlock_t orphan_lock;
    1213             :         void *orph_buf;
    1214             :         int new_orphans;
    1215             :         int cmt_orphans;
    1216             :         int tot_orphans;
    1217             :         int max_orphans;
    1218             :         int ohead_lnum;
    1219             :         int ohead_offs;
    1220             :         int no_orphs;
    1221             : 
    1222             :         int gc_lnum;
    1223             :         void *sbuf;
    1224             :         struct list_head idx_gc;
    1225             :         int idx_gc_cnt;
    1226             :         int gc_seq;
    1227             :         int gced_lnum;
    1228             : 
    1229             :         int space_bits;
    1230             :         int lpt_lnum_bits;
    1231             :         int lpt_offs_bits;
    1232             :         int lpt_spc_bits;
    1233             :         int pcnt_bits;
    1234             :         int lnum_bits;
    1235             :         int nnode_sz;
    1236             :         int pnode_sz;
    1237             :         int ltab_sz;
    1238             :         int lsave_sz;
    1239             :         int pnode_cnt;
    1240             :         int nnode_cnt;
    1241             :         int lpt_hght;
    1242             :         int pnodes_have;
    1243             : 
    1244             :         struct mutex lp_mutex;
    1245             :         int lpt_lnum;
    1246             :         int lpt_offs;
    1247             :         int nhead_lnum;
    1248             :         int nhead_offs;
    1249             :         int lpt_drty_flgs;
    1250             :         int dirty_nn_cnt;
    1251             :         int dirty_pn_cnt;
    1252             :         int check_lpt_free;
    1253             :         long long lpt_sz;
    1254             :         void *lpt_nod_buf;
    1255             :         void *lpt_buf;
    1256             :         struct ubifs_nnode *nroot;
    1257             :         struct ubifs_cnode *lpt_cnext;
    1258             :         struct ubifs_lpt_heap lpt_heap[LPROPS_HEAP_CNT];
    1259             :         struct ubifs_lpt_heap dirty_idx;
    1260             :         struct list_head uncat_list;
    1261             :         struct list_head empty_list;
    1262             :         struct list_head freeable_list;
    1263             :         struct list_head frdi_idx_list;
    1264             :         int freeable_cnt;
    1265             :         int in_a_category_cnt;
    1266             : 
    1267             :         int ltab_lnum;
    1268             :         int ltab_offs;
    1269             :         struct ubifs_lprops *lpt;
    1270             :         struct ubifs_lpt_lprops *ltab;
    1271             :         struct ubifs_lpt_lprops *ltab_cmt;
    1272             :         int lsave_cnt;
    1273             :         int lsave_lnum;
    1274             :         int lsave_offs;
    1275             :         int *lsave;
    1276             :         int lscan_lnum;
    1277             : 
    1278             :         long long rp_size;
    1279             : 
    1280             :         char *hash_algo_name;
    1281             :         int hash_algo;
    1282             :         char *auth_key_filename;
    1283             :         char *auth_cert_filename;
    1284             :         int hash_len;
    1285             :         uint8_t root_idx_hash[UBIFS_MAX_HASH_LEN];
    1286             :         uint8_t lpt_hash[UBIFS_MAX_HASH_LEN];
    1287             :         uint8_t mst_hash[UBIFS_MAX_HASH_LEN];
    1288             : 
    1289             :         struct shash_desc *log_hash;
    1290             : 
    1291             :         unsigned int need_recovery:1;
    1292             :         unsigned int replaying:1;
    1293             :         unsigned int mounting:1;
    1294             :         unsigned int remounting_rw:1;
    1295             :         struct list_head replay_list;
    1296             :         struct list_head replay_buds;
    1297             :         unsigned long long cs_sqnum;
    1298             :         struct list_head unclean_leb_list;
    1299             :         struct ubifs_mst_node *rcvrd_mst_node;
    1300             :         struct rb_root size_tree;
    1301             : 
    1302             :         int new_ihead_lnum;
    1303             :         int new_ihead_offs;
    1304             : 
    1305             :         void *private;
    1306             :         void (*assert_failed_cb)(const struct ubifs_info *c);
    1307             :         void (*set_failure_reason_cb)(const struct ubifs_info *c,
    1308             :                                       unsigned int reason);
    1309             :         unsigned int (*get_failure_reason_cb)(const struct ubifs_info *c);
    1310             :         void (*clear_failure_reason_cb)(const struct ubifs_info *c);
    1311             :         bool (*test_and_clear_failure_reason_cb)(const struct ubifs_info *c,
    1312             :                                                  unsigned int reason);
    1313             :         void (*set_lpt_invalid_cb)(const struct ubifs_info *c,
    1314             :                                    unsigned int reason);
    1315             :         bool (*test_lpt_valid_cb)(const struct ubifs_info *c, int lnum,
    1316             :                                   int old_free, int old_dirty,
    1317             :                                   int free, int dirty);
    1318             :         bool (*can_ignore_failure_cb)(const struct ubifs_info *c,
    1319             :                                       unsigned int reason);
    1320             :         bool (*handle_failure_cb)(const struct ubifs_info *c,
    1321             :                                   unsigned int reason, void *priv);
    1322             : };
    1323             : 
    1324             : extern atomic_long_t ubifs_clean_zn_cnt;
    1325             : 
    1326             : /* auth.c */
    1327             : static inline int ubifs_authenticated(const struct ubifs_info *c)
    1328             : {
    1329  3086154188 :         return c->authenticated;
    1330             : }
    1331             : 
    1332             : /**
    1333             :  * struct size_entry - inode size information for recovery.
    1334             :  * @rb: link in the RB-tree of sizes
    1335             :  * @inum: inode number
    1336             :  * @i_size: size on inode
    1337             :  * @d_size: maximum size based on data nodes
    1338             :  * @exists: indicates whether the inode exists
    1339             :  */
    1340             : struct size_entry {
    1341             :         struct rb_node rb;
    1342             :         ino_t inum;
    1343             :         loff_t i_size;
    1344             :         loff_t d_size;
    1345             :         int exists;
    1346             : };
    1347             : 
    1348             : #ifdef WITH_CRYPTO
    1349             : int ubifs_init_authentication(struct ubifs_info *c);
    1350             : int ubifs_shash_init(const struct ubifs_info *c, struct shash_desc *desc);
    1351             : int ubifs_shash_update(const struct ubifs_info *c, struct shash_desc *desc,
    1352             :                        const void *buf, unsigned int len);
    1353             : int ubifs_shash_final(const struct ubifs_info *c, struct shash_desc *desc,
    1354             :                       u8 *out);
    1355             : struct shash_desc *ubifs_hash_get_desc(const struct ubifs_info *c);
    1356             : int __ubifs_node_calc_hash(const struct ubifs_info *c, const void *buf,
    1357             :                            u8 *hash);
    1358             : int ubifs_master_node_calc_hash(const struct ubifs_info *c, const void *node,
    1359             :                                 uint8_t *hash);
    1360             : int ubifs_sign_superblock_node(struct ubifs_info *c, void *node);
    1361             : void ubifs_bad_hash(const struct ubifs_info *c, const void *node,
    1362             :                     const u8 *hash, int lnum, int offs);
    1363             : void __ubifs_exit_authentication(struct ubifs_info *c);
    1364             : #else
    1365             : static inline int ubifs_init_authentication(__unused struct ubifs_info *c)
    1366             : { return 0; }
    1367             : static inline int ubifs_shash_init(__unused const struct ubifs_info *c,
    1368             :                                    __unused struct shash_desc *desc)
    1369             : { return 0; }
    1370             : static inline int ubifs_shash_update(__unused const struct ubifs_info *c,
    1371             :                                      __unused struct shash_desc *desc,
    1372             :                                      __unused const void *buf,
    1373             :                                      __unused unsigned int len) { return 0; }
    1374             : static inline int ubifs_shash_final(__unused const struct ubifs_info *c,
    1375             :                                     __unused struct shash_desc *desc,
    1376             :                                     __unused u8 *out) { return 0; }
    1377             : static inline struct shash_desc *
    1378             : ubifs_hash_get_desc(__unused const struct ubifs_info *c) { return NULL; }
    1379             : static inline int __ubifs_node_calc_hash(__unused const struct ubifs_info *c,
    1380             :                                          __unused const void *buf,
    1381             :                                          __unused u8 *hash) { return 0; }
    1382             : static inline int
    1383             : ubifs_master_node_calc_hash(__unused const struct ubifs_info *c,
    1384             :                             __unused const void *node, __unused uint8_t *hash)
    1385             : { return 0; }
    1386             : static inline int ubifs_sign_superblock_node(__unused struct ubifs_info *c,
    1387             :                                              __unused void *node)
    1388             : { return 0; }
    1389             : static inline void ubifs_bad_hash(__unused const struct ubifs_info *c,
    1390             :                                   __unused const void *node,
    1391             :                                   __unused const u8 *hash, __unused int lnum,
    1392             :                                   __unused int offs) {}
    1393             : static inline void __ubifs_exit_authentication(__unused struct ubifs_info *c) {}
    1394             : #endif
    1395             : 
    1396             : static inline int ubifs_prepare_auth_node(__unused struct ubifs_info *c,
    1397             :                                           __unused void *node,
    1398             :                                           __unused struct shash_desc *inhash)
    1399             : {
    1400             :         // To be implemented
    1401             :         return 0;
    1402             : }
    1403             : 
    1404             : static inline int
    1405             : ubifs_node_calc_hash(const struct ubifs_info *c, const void *buf, u8 *hash)
    1406             : {
    1407     7152326 :         if (ubifs_authenticated(c))
    1408           0 :                 return __ubifs_node_calc_hash(c, buf, hash);
    1409             :         else
    1410             :                 return 0;
    1411             : }
    1412             : 
    1413             : static inline int
    1414             : ubifs_node_check_hash(__unused const struct ubifs_info *c,
    1415             :                       __unused const void *buf, __unused const u8 *expected)
    1416             : {
    1417             :         // To be implemented
    1418             :         return 0;
    1419             : }
    1420             : 
    1421             : /**
    1422             :  * ubifs_check_hash - compare two hashes
    1423             :  * @c: UBIFS file-system description object
    1424             :  * @expected: first hash
    1425             :  * @got: second hash
    1426             :  *
    1427             :  * Compare two hashes @expected and @got. Returns 0 when they are equal, a
    1428             :  * negative error code otherwise.
    1429             :  */
    1430             : static inline int
    1431             : ubifs_check_hash(__unused const struct ubifs_info *c,
    1432             :                  __unused const u8 *expected, __unused const u8 *got)
    1433             : {
    1434             :         // To be implemented
    1435             :         return 0;
    1436             : }
    1437             : 
    1438             : /**
    1439             :  * ubifs_check_hmac - compare two HMACs
    1440             :  * @c: UBIFS file-system description object
    1441             :  * @expected: first HMAC
    1442             :  * @got: second HMAC
    1443             :  *
    1444             :  * Compare two hashes @expected and @got. Returns 0 when they are equal, a
    1445             :  * negative error code otherwise.
    1446             :  */
    1447             : static inline int
    1448             : ubifs_check_hmac(__unused const struct ubifs_info *c,
    1449             :                  __unused const u8 *expected, __unused const u8 *got)
    1450             : {
    1451             :         // To be implemented
    1452             :         return 0;
    1453             : }
    1454             : 
    1455             : /**
    1456             :  * ubifs_branch_hash - returns a pointer to the hash of a branch
    1457             :  * @c: UBIFS file-system description object
    1458             :  * @br: branch to get the hash from
    1459             :  *
    1460             :  * This returns a pointer to the hash of a branch. Since the key already is a
    1461             :  * dynamically sized object we cannot use a struct member here.
    1462             :  */
    1463             : static inline u8 *
    1464             : ubifs_branch_hash(struct ubifs_info *c, struct ubifs_branch *br)
    1465             : {
    1466  3071963100 :         return (void *)br + sizeof(*br) + c->key_len;
    1467             : }
    1468             : 
    1469             : /**
    1470             :  * ubifs_copy_hash - copy a hash
    1471             :  * @c: UBIFS file-system description object
    1472             :  * @from: source hash
    1473             :  * @to: destination hash
    1474             :  *
    1475             :  * With authentication this copies a hash, otherwise does nothing.
    1476             :  */
    1477             : static inline void
    1478             : ubifs_copy_hash(const struct ubifs_info *c, const u8 *from, u8 *to)
    1479             : {
    1480  3078862922 :         if (ubifs_authenticated(c))
    1481           0 :                 memcpy(to, from, c->hash_len);
    1482             : }
    1483             : 
    1484             : static inline int
    1485             : ubifs_node_insert_hmac(__unused const struct ubifs_info *c, __unused void *buf,
    1486             :                        __unused int len, __unused int ofs_hmac)
    1487             : {
    1488             :         // To be implemented
    1489             :         return 0;
    1490             : }
    1491             : 
    1492             : static inline int
    1493             : ubifs_node_verify_hmac(__unused const struct ubifs_info *c,
    1494             :                        __unused const void *buf, __unused int len,
    1495             :                        __unused int ofs_hmac)
    1496             : {
    1497             :         // To be implemented
    1498             :         return 0;
    1499             : }
    1500             : 
    1501             : /**
    1502             :  * ubifs_auth_node_sz - returns the size of an authentication node
    1503             :  * @c: UBIFS file-system description object
    1504             :  *
    1505             :  * This function returns the size of an authentication node which can
    1506             :  * be 0 for unauthenticated filesystems or the real size of an auth node
    1507             :  * authentication is enabled.
    1508             :  */
    1509             : static inline int
    1510             : ubifs_auth_node_sz(__unused const struct ubifs_info *c)
    1511             : {
    1512             :         // To be implemented
    1513             :         return 0;
    1514             : }
    1515             : 
    1516             : static inline bool
    1517             : ubifs_hmac_zero(__unused struct ubifs_info *c, __unused const u8 *hmac)
    1518             : {
    1519             :         // To be implemented
    1520             :         return true;
    1521             : }
    1522             : 
    1523             : static inline int
    1524             : ubifs_shash_copy_state(__unused const struct ubifs_info *c,
    1525             :                        __unused struct shash_desc *src,
    1526             :                        __unused struct shash_desc *target)
    1527             : {
    1528             :         // To be implemented
    1529             :         return 0;
    1530             : }
    1531             : 
    1532             : static inline void ubifs_exit_authentication(struct ubifs_info *c)
    1533             : {
    1534         640 :         if (ubifs_authenticated(c))
    1535           0 :                 __ubifs_exit_authentication(c);
    1536             : }
    1537             : 
    1538             : /* io.c */
    1539             : void ubifs_ro_mode(struct ubifs_info *c, int err);
    1540             : int ubifs_leb_read(const struct ubifs_info *c, int lnum, void *buf, int offs,
    1541             :                    int len, int even_ebadmsg);
    1542             : int ubifs_leb_write(struct ubifs_info *c, int lnum, const void *buf, int offs,
    1543             :                     int len);
    1544             : int ubifs_leb_change(struct ubifs_info *c, int lnum, const void *buf, int len);
    1545             : int ubifs_leb_unmap(struct ubifs_info *c, int lnum);
    1546             : int ubifs_leb_map(struct ubifs_info *c, int lnum);
    1547             : int ubifs_is_mapped(const struct ubifs_info *c, int lnum);
    1548             : int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len);
    1549             : int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs);
    1550             : int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf);
    1551             : int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
    1552             :                     int lnum, int offs);
    1553             : int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
    1554             :                          int lnum, int offs);
    1555             : int ubifs_write_node(struct ubifs_info *c, void *node, int len, int lnum,
    1556             :                      int offs);
    1557             : int ubifs_write_node_hmac(struct ubifs_info *c, void *buf, int len, int lnum,
    1558             :                           int offs, int hmac_offs);
    1559             : int ubifs_check_node(const struct ubifs_info *c, const void *buf, int len,
    1560             :                      int lnum, int offs, int quiet, int must_chk_crc);
    1561             : void ubifs_init_node(struct ubifs_info *c, void *buf, int len, int pad);
    1562             : void ubifs_crc_node(struct ubifs_info *c, void *buf, int len);
    1563             : void ubifs_prepare_node(struct ubifs_info *c, void *buf, int len, int pad);
    1564             : int ubifs_prepare_node_hmac(struct ubifs_info *c, void *node, int len,
    1565             :                             int hmac_offs, int pad);
    1566             : void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last);
    1567             : int ubifs_io_init(struct ubifs_info *c);
    1568             : void ubifs_pad(const struct ubifs_info *c, void *buf, int pad);
    1569             : int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf);
    1570             : 
    1571             : /* scan.c */
    1572             : struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
    1573             :                                   int offs, void *sbuf, int quiet);
    1574             : void ubifs_scan_destroy(struct ubifs_scan_leb *sleb);
    1575             : int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum,
    1576             :                       int offs, int quiet);
    1577             : struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
    1578             :                                         int offs, void *sbuf);
    1579             : void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
    1580             :                     int lnum, int offs);
    1581             : int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
    1582             :                    void *buf, int offs);
    1583             : void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
    1584             :                               void *buf);
    1585             : 
    1586             : /* Failure reasons which are checked by fsck. */
    1587             : enum {
    1588             :         FR_DATA_CORRUPTED = 1,  /* Data is corrupted(master/log/orphan/main) */
    1589             :         FR_TNC_CORRUPTED = 2,   /* TNC is corrupted */
    1590             :         FR_LPT_CORRUPTED = 4,   /* LPT is corrupted */
    1591             :         FR_LPT_INCORRECT = 8    /* Space statistics are wrong */
    1592             : };
    1593             : /* Partial failure reasons in common libs, which are handled by fsck. */
    1594             : enum {
    1595             :         FR_H_BUD_CORRUPTED = 0,         /* Bud LEB is corrupted */
    1596             :         FR_H_TNC_DATA_CORRUPTED,        /* Data searched from TNC is corrupted */
    1597             :         FR_H_ORPHAN_CORRUPTED,          /* Orphan LEB is corrupted */
    1598             :         FR_H_LTAB_INCORRECT,            /* Lprops table is incorrect */
    1599             : };
    1600             : /* Callback functions for failure(which can be handled by fsck) happens. */
    1601             : static inline void set_failure_reason_callback(const struct ubifs_info *c,
    1602             :                                                unsigned int reason)
    1603             : {
    1604      190794 :         if (c->set_failure_reason_cb)
    1605      190794 :                 c->set_failure_reason_cb(c, reason);
    1606             : }
    1607             : static inline unsigned int get_failure_reason_callback(
    1608             :                                                 const struct ubifs_info *c)
    1609             : {
    1610         794 :         if (c->get_failure_reason_cb)
    1611         794 :                 return c->get_failure_reason_cb(c);
    1612             : 
    1613             :         return 0;
    1614             : }
    1615             : static inline void clear_failure_reason_callback(const struct ubifs_info *c)
    1616             : {
    1617         570 :         if (c->clear_failure_reason_cb)
    1618         570 :                 c->clear_failure_reason_cb(c);
    1619             : }
    1620             : static inline bool test_and_clear_failure_reason_callback(
    1621             :                                                 const struct ubifs_info *c,
    1622             :                                                 unsigned int reason)
    1623             : {
    1624      186036 :         if (c->test_and_clear_failure_reason_cb)
    1625      186036 :                 return c->test_and_clear_failure_reason_cb(c, reason);
    1626             : 
    1627             :         return false;
    1628             : }
    1629             : static inline void set_lpt_invalid_callback(const struct ubifs_info *c,
    1630             :                                             unsigned int reason)
    1631             : {
    1632           7 :         if (c->set_lpt_invalid_cb)
    1633           7 :                 c->set_lpt_invalid_cb(c, reason);
    1634             : }
    1635             : static inline bool test_lpt_valid_callback(const struct ubifs_info *c, int lnum,
    1636             :                                            int old_free, int old_dirty,
    1637             :                                            int free, int dirty)
    1638             : {
    1639    11625442 :         if (c->test_lpt_valid_cb)
    1640    11625442 :                 return c->test_lpt_valid_cb(c, lnum,
    1641             :                                             old_free, old_dirty, free, dirty);
    1642             : 
    1643             :         return false;
    1644             : }
    1645             : static inline bool can_ignore_failure_callback(const struct ubifs_info *c,
    1646             :                                                unsigned int reason)
    1647             : {
    1648          89 :         if (c->can_ignore_failure_cb)
    1649          89 :                 return c->can_ignore_failure_cb(c, reason);
    1650             : 
    1651             :         return false;
    1652             : }
    1653             : static inline bool handle_failure_callback(const struct ubifs_info *c,
    1654             :                                            unsigned int reason, void *priv)
    1655             : {
    1656          34 :         if (c->handle_failure_cb)
    1657          34 :                 return c->handle_failure_cb(c, reason, priv);
    1658             : 
    1659             :         return false;
    1660             : }
    1661             : 
    1662             : /* log.c */
    1663             : void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud);
    1664             : void ubifs_create_buds_lists(struct ubifs_info *c);
    1665             : int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs);
    1666             : struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum);
    1667             : struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum);
    1668             : int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum);
    1669             : int ubifs_log_end_commit(struct ubifs_info *c, int new_ltail_lnum);
    1670             : int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum);
    1671             : int ubifs_consolidate_log(struct ubifs_info *c);
    1672             : 
    1673             : /* journal.c */
    1674             : int ubifs_get_dent_type(int mode);
    1675             : int ubifs_jnl_update_file(struct ubifs_info *c,
    1676             :                           const struct ubifs_inode *dir_ui,
    1677             :                           const struct fscrypt_name *nm,
    1678             :                           const struct ubifs_inode *ui);
    1679             : 
    1680             : /* budget.c */
    1681             : int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req);
    1682             : void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req);
    1683             : long long ubifs_get_free_space_nolock(struct ubifs_info *c);
    1684             : int ubifs_calc_min_idx_lebs(struct ubifs_info *c);
    1685             : long long ubifs_reported_space(const struct ubifs_info *c, long long free);
    1686             : long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs);
    1687             : 
    1688             : /* find.c */
    1689             : int ubifs_find_free_space(struct ubifs_info *c, int min_space, int *offs,
    1690             :                           int squeeze);
    1691             : int ubifs_find_free_leb_for_idx(struct ubifs_info *c);
    1692             : int ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp,
    1693             :                          int min_space, int pick_free);
    1694             : int ubifs_find_dirty_idx_leb(struct ubifs_info *c);
    1695             : int ubifs_save_dirty_idx_lnums(struct ubifs_info *c);
    1696             : 
    1697             : /* tnc.c */
    1698             : int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
    1699             :                         struct ubifs_znode **zn, int *n);
    1700             : int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
    1701             :                         void *node, const struct fscrypt_name *nm);
    1702             : int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
    1703             :                      void *node, int *lnum, int *offs);
    1704             : int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
    1705             :                   int offs, int len, const u8 *hash);
    1706             : int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key,
    1707             :                       int old_lnum, int old_offs, int lnum, int offs, int len);
    1708             : int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
    1709             :                      int lnum, int offs, int len, const u8 *hash,
    1710             :                      const struct fscrypt_name *nm);
    1711             : int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key);
    1712             : int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key,
    1713             :                         const struct fscrypt_name *nm);
    1714             : int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
    1715             :                            union ubifs_key *to_key);
    1716             : int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum);
    1717             : int ubifs_tnc_remove_node(struct ubifs_info *c, const union ubifs_key *key,
    1718             :                           int lnum, int offs);
    1719             : struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c,
    1720             :                                            union ubifs_key *key,
    1721             :                                            const struct fscrypt_name *nm);
    1722             : void ubifs_tnc_close(struct ubifs_info *c);
    1723             : int ubifs_tnc_has_node(struct ubifs_info *c, union ubifs_key *key, int level,
    1724             :                        int lnum, int offs, int is_idx);
    1725             : int ubifs_dirty_idx_node(struct ubifs_info *c, union ubifs_key *key, int level,
    1726             :                          int lnum, int offs);
    1727             : /* Shared by tnc.c for tnc_commit.c */
    1728             : void destroy_old_idx(struct ubifs_info *c);
    1729             : int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level,
    1730             :                        int lnum, int offs);
    1731             : int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode);
    1732             : 
    1733             : /* tnc_misc.c */
    1734             : int ubifs_search_zbranch(const struct ubifs_info *c,
    1735             :                          const struct ubifs_znode *znode,
    1736             :                          const union ubifs_key *key, int *n);
    1737             : struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode);
    1738             : struct ubifs_znode *ubifs_tnc_postorder_next(const struct ubifs_info *c,
    1739             :                                              struct ubifs_znode *znode);
    1740             : long ubifs_destroy_tnc_subtree(const struct ubifs_info *c,
    1741             :                                struct ubifs_znode *zr);
    1742             : void ubifs_destroy_tnc_tree(struct ubifs_info *c);
    1743             : struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
    1744             :                                      struct ubifs_zbranch *zbr,
    1745             :                                      struct ubifs_znode *parent, int iip);
    1746             : int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
    1747             :                         void *node);
    1748             : 
    1749             : /* tnc_commit.c */
    1750             : int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot);
    1751             : int ubifs_tnc_end_commit(struct ubifs_info *c);
    1752             : 
    1753             : /* commit.c */
    1754             : void ubifs_commit_required(struct ubifs_info *c);
    1755             : void ubifs_request_bg_commit(struct ubifs_info *c);
    1756             : int ubifs_run_commit(struct ubifs_info *c);
    1757             : void ubifs_recovery_commit(struct ubifs_info *c);
    1758             : int ubifs_gc_should_commit(struct ubifs_info *c);
    1759             : void ubifs_wait_for_commit(struct ubifs_info *c);
    1760             : 
    1761             : /* master.c */
    1762             : int ubifs_compare_master_node(struct ubifs_info *c, void *m1, void *m2);
    1763             : int ubifs_read_master(struct ubifs_info *c);
    1764             : int ubifs_write_master(struct ubifs_info *c);
    1765             : 
    1766             : /* sb.c */
    1767             : int ubifs_read_superblock(struct ubifs_info *c);
    1768             : int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup);
    1769             : int ubifs_fixup_free_space(struct ubifs_info *c);
    1770             : 
    1771             : /* replay.c */
    1772             : int ubifs_validate_entry(struct ubifs_info *c,
    1773             :                          const struct ubifs_dent_node *dent);
    1774             : int take_ihead(struct ubifs_info *c);
    1775             : int ubifs_replay_journal(struct ubifs_info *c);
    1776             : 
    1777             : /* gc.c */
    1778             : int ubifs_garbage_collect(struct ubifs_info *c, int anyway);
    1779             : int ubifs_gc_start_commit(struct ubifs_info *c);
    1780             : int ubifs_gc_end_commit(struct ubifs_info *c);
    1781             : void ubifs_destroy_idx_gc(struct ubifs_info *c);
    1782             : int ubifs_get_idx_gc_leb(struct ubifs_info *c);
    1783             : int ubifs_garbage_collect_leb(struct ubifs_info *c, struct ubifs_lprops *lp);
    1784             : 
    1785             : /* orphan.c */
    1786             : int ubifs_orphan_start_commit(struct ubifs_info *c);
    1787             : int ubifs_orphan_end_commit(struct ubifs_info *c);
    1788             : int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only);
    1789             : int ubifs_clear_orphans(struct ubifs_info *c);
    1790             : 
    1791             : /* lpt.c */
    1792             : int ubifs_calc_dflt_lpt_geom(struct ubifs_info *c, int *main_lebs, int *big_lpt);
    1793             : int ubifs_calc_lpt_geom(struct ubifs_info *c);
    1794             : int ubifs_create_lpt(struct ubifs_info *c, struct ubifs_lprops *lps, int lp_cnt,
    1795             :                      u8 *hash, bool free_ltab);
    1796             : int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr);
    1797             : struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum);
    1798             : struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum);
    1799             : int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
    1800             :                           ubifs_lpt_scan_callback scan_cb, void *data);
    1801             : 
    1802             : /* Shared by lpt.c for lpt_commit.c */
    1803             : void ubifs_pack_lsave(struct ubifs_info *c, void *buf, int *lsave);
    1804             : void ubifs_pack_ltab(struct ubifs_info *c, void *buf,
    1805             :                      struct ubifs_lpt_lprops *ltab);
    1806             : void ubifs_pack_pnode(struct ubifs_info *c, void *buf,
    1807             :                       struct ubifs_pnode *pnode);
    1808             : void ubifs_pack_nnode(struct ubifs_info *c, void *buf,
    1809             :                       struct ubifs_nnode *nnode);
    1810             : struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
    1811             :                                     struct ubifs_nnode *parent, int iip);
    1812             : struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
    1813             :                                     struct ubifs_nnode *parent, int iip);
    1814             : struct ubifs_pnode *ubifs_pnode_lookup(struct ubifs_info *c, int i);
    1815             : int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip);
    1816             : void ubifs_add_lpt_dirt(struct ubifs_info *c, int lnum, int dirty);
    1817             : void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode);
    1818             : uint32_t ubifs_unpack_bits(const struct ubifs_info *c, uint8_t **addr, int *pos, int nrbits);
    1819             : int ubifs_calc_nnode_num(int row, int col);
    1820             : struct ubifs_nnode *ubifs_first_nnode(struct ubifs_info *c, int *hght);
    1821             : /* Needed only in debugging code in lpt_commit.c */
    1822             : int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
    1823             :                        struct ubifs_nnode *nnode);
    1824             : int ubifs_lpt_calc_hash(struct ubifs_info *c, u8 *hash);
    1825             : 
    1826             : /* lpt_commit.c */
    1827             : struct ubifs_pnode *ubifs_find_next_pnode(struct ubifs_info *c,
    1828             :                                           struct ubifs_pnode *pnode);
    1829             : void ubifs_make_nnode_dirty(struct ubifs_info *c, struct ubifs_nnode *nnode);
    1830             : void ubifs_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode);
    1831             : int ubifs_lpt_start_commit(struct ubifs_info *c);
    1832             : int ubifs_lpt_end_commit(struct ubifs_info *c);
    1833             : int ubifs_lpt_post_commit(struct ubifs_info *c);
    1834             : void ubifs_free_lpt_nodes(struct ubifs_info *c);
    1835             : void ubifs_lpt_free(struct ubifs_info *c, int wr_only);
    1836             : int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum);
    1837             : 
    1838             : /* lprops.c */
    1839             : const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
    1840             :                                            const struct ubifs_lprops *lp,
    1841             :                                            int free, int dirty, int flags,
    1842             :                                            int idx_gc_cnt);
    1843             : void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *lst);
    1844             : void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
    1845             :                       int cat);
    1846             : void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
    1847             :                        struct ubifs_lprops *new_lprops);
    1848             : void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops);
    1849             : int ubifs_categorize_lprops(const struct ubifs_info *c,
    1850             :                             const struct ubifs_lprops *lprops);
    1851             : int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
    1852             :                         int flags_set, int flags_clean, int idx_gc_cnt);
    1853             : int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
    1854             :                         int flags_set, int flags_clean);
    1855             : int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp);
    1856             : const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c);
    1857             : const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c);
    1858             : const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c);
    1859             : const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c);
    1860             : int ubifs_calc_dark(const struct ubifs_info *c, int spc);
    1861             : 
    1862             : /* dir.c */
    1863             : struct ubifs_inode *ubifs_lookup_by_inum(struct ubifs_info *c, ino_t inum);
    1864             : struct ubifs_inode *ubifs_lookup(struct ubifs_info *c,
    1865             :                                  struct ubifs_inode *dir_ui,
    1866             :                                  const struct fscrypt_name *nm);
    1867             : int ubifs_mkdir(struct ubifs_info *c, struct ubifs_inode *dir_ui,
    1868             :                 const struct fscrypt_name *nm, unsigned int mode);
    1869             : int ubifs_link_recovery(struct ubifs_info *c, struct ubifs_inode *dir_ui,
    1870             :                         struct ubifs_inode *ui, const struct fscrypt_name *nm);
    1871             : int ubifs_create_root(struct ubifs_info *c);
    1872             : 
    1873             : /* super.c */
    1874             : int open_ubi(struct ubifs_info *c, const char *node);
    1875             : void close_ubi(struct ubifs_info *c);
    1876             : int open_target(struct ubifs_info *c);
    1877             : int close_target(struct ubifs_info *c);
    1878             : int ubifs_open_volume(struct ubifs_info *c, const char *volume_name);
    1879             : int ubifs_close_volume(struct ubifs_info *c);
    1880             : int check_volume_empty(struct ubifs_info *c);
    1881             : void init_ubifs_info(struct ubifs_info *c, int program_type);
    1882             : int init_constants_early(struct ubifs_info *c);
    1883             : int init_constants_sb(struct ubifs_info *c);
    1884             : void init_constants_master(struct ubifs_info *c);
    1885             : int take_gc_lnum(struct ubifs_info *c);
    1886             : int alloc_wbufs(struct ubifs_info *c);
    1887             : void free_wbufs(struct ubifs_info *c);
    1888             : void free_orphans(struct ubifs_info *c);
    1889             : void free_buds(struct ubifs_info *c, bool delete_from_list);
    1890             : void destroy_journal(struct ubifs_info *c);
    1891             : 
    1892             : /* recovery.c */
    1893             : int ubifs_recover_master_node(struct ubifs_info *c);
    1894             : struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
    1895             :                                          int offs, void *sbuf, int jhead);
    1896             : struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
    1897             :                                              int offs, void *sbuf);
    1898             : int ubifs_recover_inl_heads(struct ubifs_info *c, void *sbuf);
    1899             : int ubifs_rcvry_gc_commit(struct ubifs_info *c);
    1900             : int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
    1901             :                              int deletion, loff_t new_size);
    1902             : int ubifs_recover_size(struct ubifs_info *c, bool in_place);
    1903             : void ubifs_destroy_size_tree(struct ubifs_info *c);
    1904             : 
    1905             : /* Normal UBIFS messages */
    1906             : enum { ERR_LEVEL = 1, WARN_LEVEL, INFO_LEVEL, DEBUG_LEVEL };
    1907             : #define ubifs_msg(c, fmt, ...) do {                                     \
    1908             :         if (c->debug_level >= INFO_LEVEL)                         \
    1909             :                 printf("<INFO> %s[%d] (%s): %s: " fmt "\n",           \
    1910             :                        c->program_name, getpid(),                    \
    1911             :                        c->dev_name, __FUNCTION__, ##__VA_ARGS__);    \
    1912             : } while (0)
    1913             : #define ubifs_warn(c, fmt, ...) do {                                    \
    1914             :         if (c->debug_level >= WARN_LEVEL)                         \
    1915             :                 printf("<WARN> %s[%d] (%s): %s: " fmt "\n",           \
    1916             :                        c->program_name, getpid(),                    \
    1917             :                        c->dev_name, __FUNCTION__, ##__VA_ARGS__);    \
    1918             : } while (0)
    1919             : #define ubifs_err(c, fmt, ...) do {                                     \
    1920             :         if (c->debug_level >= ERR_LEVEL)                          \
    1921             :                 printf("<ERROR> %s[%d] (%s): %s: " fmt "\n",          \
    1922             :                        c->program_name, getpid(),                    \
    1923             :                        c->dev_name, __FUNCTION__, ##__VA_ARGS__);    \
    1924             : } while (0)
    1925             : 
    1926             : #endif /* !__UBIFS_H__ */

Generated by: LCOV version 1.13