xref: /linux/fs/ubifs/ubifs.h (revision 1f2367a39f17bd553a75e179a747f9b257bc9478)
1 /*
2  * This file is part of UBIFS.
3  *
4  * Copyright (C) 2006-2008 Nokia Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Authors: Artem Bityutskiy (Битюцкий Артём)
20  *          Adrian Hunter
21  */
22 
23 #ifndef __UBIFS_H__
24 #define __UBIFS_H__
25 
26 #include <asm/div64.h>
27 #include <linux/statfs.h>
28 #include <linux/fs.h>
29 #include <linux/err.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
32 #include <linux/vmalloc.h>
33 #include <linux/spinlock.h>
34 #include <linux/mutex.h>
35 #include <linux/rwsem.h>
36 #include <linux/mtd/ubi.h>
37 #include <linux/pagemap.h>
38 #include <linux/backing-dev.h>
39 #include <linux/security.h>
40 #include <linux/xattr.h>
41 #include <linux/random.h>
42 #include <crypto/hash_info.h>
43 #include <crypto/hash.h>
44 #include <crypto/algapi.h>
45 
46 #include <linux/fscrypt.h>
47 
48 #include "ubifs-media.h"
49 
50 /* Version of this UBIFS implementation */
51 #define UBIFS_VERSION 1
52 
53 /* UBIFS file system VFS magic number */
54 #define UBIFS_SUPER_MAGIC 0x24051905
55 
56 /* Number of UBIFS blocks per VFS page */
57 #define UBIFS_BLOCKS_PER_PAGE (PAGE_SIZE / UBIFS_BLOCK_SIZE)
58 #define UBIFS_BLOCKS_PER_PAGE_SHIFT (PAGE_SHIFT - UBIFS_BLOCK_SHIFT)
59 
60 /* "File system end of life" sequence number watermark */
61 #define SQNUM_WARN_WATERMARK 0xFFFFFFFF00000000ULL
62 #define SQNUM_WATERMARK      0xFFFFFFFFFF000000ULL
63 
64 /*
65  * Minimum amount of LEBs reserved for the index. At present the index needs at
66  * least 2 LEBs: one for the index head and one for in-the-gaps method (which
67  * currently does not cater for the index head and so excludes it from
68  * consideration).
69  */
70 #define MIN_INDEX_LEBS 2
71 
72 /* Minimum amount of data UBIFS writes to the flash */
73 #define MIN_WRITE_SZ (UBIFS_DATA_NODE_SZ + 8)
74 
75 /*
76  * Currently we do not support inode number overlapping and re-using, so this
77  * watermark defines dangerous inode number level. This should be fixed later,
78  * although it is difficult to exceed current limit. Another option is to use
79  * 64-bit inode numbers, but this means more overhead.
80  */
81 #define INUM_WARN_WATERMARK 0xFFF00000
82 #define INUM_WATERMARK      0xFFFFFF00
83 
84 /* Maximum number of entries in each LPT (LEB category) heap */
85 #define LPT_HEAP_SZ 256
86 
87 /*
88  * Background thread name pattern. The numbers are UBI device and volume
89  * numbers.
90  */
91 #define BGT_NAME_PATTERN "ubifs_bgt%d_%d"
92 
93 /* Maximum possible inode number (only 32-bit inodes are supported now) */
94 #define MAX_INUM 0xFFFFFFFF
95 
96 /* Number of non-data journal heads */
97 #define NONDATA_JHEADS_CNT 2
98 
99 /* Shorter names for journal head numbers for internal usage */
100 #define GCHD   UBIFS_GC_HEAD
101 #define BASEHD UBIFS_BASE_HEAD
102 #define DATAHD UBIFS_DATA_HEAD
103 
104 /* 'No change' value for 'ubifs_change_lp()' */
105 #define LPROPS_NC 0x80000001
106 
107 /*
108  * There is no notion of truncation key because truncation nodes do not exist
109  * in TNC. However, when replaying, it is handy to introduce fake "truncation"
110  * keys for truncation nodes because the code becomes simpler. So we define
111  * %UBIFS_TRUN_KEY type.
112  *
113  * But otherwise, out of the journal reply scope, the truncation keys are
114  * invalid.
115  */
116 #define UBIFS_TRUN_KEY    UBIFS_KEY_TYPES_CNT
117 #define UBIFS_INVALID_KEY UBIFS_KEY_TYPES_CNT
118 
119 /*
120  * How much a directory entry/extended attribute entry adds to the parent/host
121  * inode.
122  */
123 #define CALC_DENT_SIZE(name_len) ALIGN(UBIFS_DENT_NODE_SZ + (name_len) + 1, 8)
124 
125 /* How much an extended attribute adds to the host inode */
126 #define CALC_XATTR_BYTES(data_len) ALIGN(UBIFS_INO_NODE_SZ + (data_len) + 1, 8)
127 
128 /*
129  * Znodes which were not touched for 'OLD_ZNODE_AGE' seconds are considered
130  * "old", and znode which were touched last 'YOUNG_ZNODE_AGE' seconds ago are
131  * considered "young". This is used by shrinker when selecting znode to trim
132  * off.
133  */
134 #define OLD_ZNODE_AGE 20
135 #define YOUNG_ZNODE_AGE 5
136 
137 /*
138  * Some compressors, like LZO, may end up with more data then the input buffer.
139  * So UBIFS always allocates larger output buffer, to be sure the compressor
140  * will not corrupt memory in case of worst case compression.
141  */
142 #define WORST_COMPR_FACTOR 2
143 
144 #ifdef CONFIG_FS_ENCRYPTION
145 #define UBIFS_CIPHER_BLOCK_SIZE FS_CRYPTO_BLOCK_SIZE
146 #else
147 #define UBIFS_CIPHER_BLOCK_SIZE 0
148 #endif
149 
150 /*
151  * How much memory is needed for a buffer where we compress a data node.
152  */
153 #define COMPRESSED_DATA_NODE_BUF_SZ \
154 	(UBIFS_DATA_NODE_SZ + UBIFS_BLOCK_SIZE * WORST_COMPR_FACTOR)
155 
156 /* Maximum expected tree height for use by bottom_up_buf */
157 #define BOTTOM_UP_HEIGHT 64
158 
159 /* Maximum number of data nodes to bulk-read */
160 #define UBIFS_MAX_BULK_READ 32
161 
162 #ifdef CONFIG_UBIFS_FS_AUTHENTICATION
163 #define UBIFS_HASH_ARR_SZ UBIFS_MAX_HASH_LEN
164 #define UBIFS_HMAC_ARR_SZ UBIFS_MAX_HMAC_LEN
165 #else
166 #define UBIFS_HASH_ARR_SZ 0
167 #define UBIFS_HMAC_ARR_SZ 0
168 #endif
169 
170 /*
171  * Lockdep classes for UBIFS inode @ui_mutex.
172  */
173 enum {
174 	WB_MUTEX_1 = 0,
175 	WB_MUTEX_2 = 1,
176 	WB_MUTEX_3 = 2,
177 	WB_MUTEX_4 = 3,
178 };
179 
180 /*
181  * Znode flags (actually, bit numbers which store the flags).
182  *
183  * DIRTY_ZNODE: znode is dirty
184  * COW_ZNODE: znode is being committed and a new instance of this znode has to
185  *            be created before changing this znode
186  * OBSOLETE_ZNODE: znode is obsolete, which means it was deleted, but it is
187  *                 still in the commit list and the ongoing commit operation
188  *                 will commit it, and delete this znode after it is done
189  */
190 enum {
191 	DIRTY_ZNODE    = 0,
192 	COW_ZNODE      = 1,
193 	OBSOLETE_ZNODE = 2,
194 };
195 
196 /*
197  * Commit states.
198  *
199  * COMMIT_RESTING: commit is not wanted
200  * COMMIT_BACKGROUND: background commit has been requested
201  * COMMIT_REQUIRED: commit is required
202  * COMMIT_RUNNING_BACKGROUND: background commit is running
203  * COMMIT_RUNNING_REQUIRED: commit is running and it is required
204  * COMMIT_BROKEN: commit failed
205  */
206 enum {
207 	COMMIT_RESTING = 0,
208 	COMMIT_BACKGROUND,
209 	COMMIT_REQUIRED,
210 	COMMIT_RUNNING_BACKGROUND,
211 	COMMIT_RUNNING_REQUIRED,
212 	COMMIT_BROKEN,
213 };
214 
215 /*
216  * 'ubifs_scan_a_node()' return values.
217  *
218  * SCANNED_GARBAGE:  scanned garbage
219  * SCANNED_EMPTY_SPACE: scanned empty space
220  * SCANNED_A_NODE: scanned a valid node
221  * SCANNED_A_CORRUPT_NODE: scanned a corrupted node
222  * SCANNED_A_BAD_PAD_NODE: scanned a padding node with invalid pad length
223  *
224  * Greater than zero means: 'scanned that number of padding bytes'
225  */
226 enum {
227 	SCANNED_GARBAGE        = 0,
228 	SCANNED_EMPTY_SPACE    = -1,
229 	SCANNED_A_NODE         = -2,
230 	SCANNED_A_CORRUPT_NODE = -3,
231 	SCANNED_A_BAD_PAD_NODE = -4,
232 };
233 
234 /*
235  * LPT cnode flag bits.
236  *
237  * DIRTY_CNODE: cnode is dirty
238  * OBSOLETE_CNODE: cnode is being committed and has been copied (or deleted),
239  *                 so it can (and must) be freed when the commit is finished
240  * COW_CNODE: cnode is being committed and must be copied before writing
241  */
242 enum {
243 	DIRTY_CNODE    = 0,
244 	OBSOLETE_CNODE = 1,
245 	COW_CNODE      = 2,
246 };
247 
248 /*
249  * Dirty flag bits (lpt_drty_flgs) for LPT special nodes.
250  *
251  * LTAB_DIRTY: ltab node is dirty
252  * LSAVE_DIRTY: lsave node is dirty
253  */
254 enum {
255 	LTAB_DIRTY  = 1,
256 	LSAVE_DIRTY = 2,
257 };
258 
259 /*
260  * Return codes used by the garbage collector.
261  * @LEB_FREED: the logical eraseblock was freed and is ready to use
262  * @LEB_FREED_IDX: indexing LEB was freed and can be used only after the commit
263  * @LEB_RETAINED: the logical eraseblock was freed and retained for GC purposes
264  */
265 enum {
266 	LEB_FREED,
267 	LEB_FREED_IDX,
268 	LEB_RETAINED,
269 };
270 
271 /*
272  * Action taken upon a failed ubifs_assert().
273  * @ASSACT_REPORT: just report the failed assertion
274  * @ASSACT_RO: switch to read-only mode
275  * @ASSACT_PANIC: call BUG() and possible panic the kernel
276  */
277 enum {
278 	ASSACT_REPORT = 0,
279 	ASSACT_RO,
280 	ASSACT_PANIC,
281 };
282 
283 /**
284  * struct ubifs_old_idx - index node obsoleted since last commit start.
285  * @rb: rb-tree node
286  * @lnum: LEB number of obsoleted index node
287  * @offs: offset of obsoleted index node
288  */
289 struct ubifs_old_idx {
290 	struct rb_node rb;
291 	int lnum;
292 	int offs;
293 };
294 
295 /* The below union makes it easier to deal with keys */
296 union ubifs_key {
297 	uint8_t u8[UBIFS_SK_LEN];
298 	uint32_t u32[UBIFS_SK_LEN/4];
299 	uint64_t u64[UBIFS_SK_LEN/8];
300 	__le32 j32[UBIFS_SK_LEN/4];
301 };
302 
303 /**
304  * struct ubifs_scan_node - UBIFS scanned node information.
305  * @list: list of scanned nodes
306  * @key: key of node scanned (if it has one)
307  * @sqnum: sequence number
308  * @type: type of node scanned
309  * @offs: offset with LEB of node scanned
310  * @len: length of node scanned
311  * @node: raw node
312  */
313 struct ubifs_scan_node {
314 	struct list_head list;
315 	union ubifs_key key;
316 	unsigned long long sqnum;
317 	int type;
318 	int offs;
319 	int len;
320 	void *node;
321 };
322 
323 /**
324  * struct ubifs_scan_leb - UBIFS scanned LEB information.
325  * @lnum: logical eraseblock number
326  * @nodes_cnt: number of nodes scanned
327  * @nodes: list of struct ubifs_scan_node
328  * @endpt: end point (and therefore the start of empty space)
329  * @buf: buffer containing entire LEB scanned
330  */
331 struct ubifs_scan_leb {
332 	int lnum;
333 	int nodes_cnt;
334 	struct list_head nodes;
335 	int endpt;
336 	void *buf;
337 };
338 
339 /**
340  * struct ubifs_gced_idx_leb - garbage-collected indexing LEB.
341  * @list: list
342  * @lnum: LEB number
343  * @unmap: OK to unmap this LEB
344  *
345  * This data structure is used to temporary store garbage-collected indexing
346  * LEBs - they are not released immediately, but only after the next commit.
347  * This is needed to guarantee recoverability.
348  */
349 struct ubifs_gced_idx_leb {
350 	struct list_head list;
351 	int lnum;
352 	int unmap;
353 };
354 
355 /**
356  * struct ubifs_inode - UBIFS in-memory inode description.
357  * @vfs_inode: VFS inode description object
358  * @creat_sqnum: sequence number at time of creation
359  * @del_cmtno: commit number corresponding to the time the inode was deleted,
360  *             protected by @c->commit_sem;
361  * @xattr_size: summarized size of all extended attributes in bytes
362  * @xattr_cnt: count of extended attributes this inode has
363  * @xattr_names: sum of lengths of all extended attribute names belonging to
364  *               this inode
365  * @dirty: non-zero if the inode is dirty
366  * @xattr: non-zero if this is an extended attribute inode
367  * @bulk_read: non-zero if bulk-read should be used
368  * @ui_mutex: serializes inode write-back with the rest of VFS operations,
369  *            serializes "clean <-> dirty" state changes, serializes bulk-read,
370  *            protects @dirty, @bulk_read, @ui_size, and @xattr_size
371  * @ui_lock: protects @synced_i_size
372  * @synced_i_size: synchronized size of inode, i.e. the value of inode size
373  *                 currently stored on the flash; used only for regular file
374  *                 inodes
375  * @ui_size: inode size used by UBIFS when writing to flash
376  * @flags: inode flags (@UBIFS_COMPR_FL, etc)
377  * @compr_type: default compression type used for this inode
378  * @last_page_read: page number of last page read (for bulk read)
379  * @read_in_a_row: number of consecutive pages read in a row (for bulk read)
380  * @data_len: length of the data attached to the inode
381  * @data: inode's data
382  *
383  * @ui_mutex exists for two main reasons. At first it prevents inodes from
384  * being written back while UBIFS changing them, being in the middle of an VFS
385  * operation. This way UBIFS makes sure the inode fields are consistent. For
386  * example, in 'ubifs_rename()' we change 3 inodes simultaneously, and
387  * write-back must not write any of them before we have finished.
388  *
389  * The second reason is budgeting - UBIFS has to budget all operations. If an
390  * operation is going to mark an inode dirty, it has to allocate budget for
391  * this. It cannot just mark it dirty because there is no guarantee there will
392  * be enough flash space to write the inode back later. This means UBIFS has
393  * to have full control over inode "clean <-> dirty" transitions (and pages
394  * actually). But unfortunately, VFS marks inodes dirty in many places, and it
395  * does not ask the file-system if it is allowed to do so (there is a notifier,
396  * but it is not enough), i.e., there is no mechanism to synchronize with this.
397  * So UBIFS has its own inode dirty flag and its own mutex to serialize
398  * "clean <-> dirty" transitions.
399  *
400  * The @synced_i_size field is used to make sure we never write pages which are
401  * beyond last synchronized inode size. See 'ubifs_writepage()' for more
402  * information.
403  *
404  * The @ui_size is a "shadow" variable for @inode->i_size and UBIFS uses
405  * @ui_size instead of @inode->i_size. The reason for this is that UBIFS cannot
406  * make sure @inode->i_size is always changed under @ui_mutex, because it
407  * cannot call 'truncate_setsize()' with @ui_mutex locked, because it would
408  * deadlock with 'ubifs_writepage()' (see file.c). All the other inode fields
409  * are changed under @ui_mutex, so they do not need "shadow" fields. Note, one
410  * could consider to rework locking and base it on "shadow" fields.
411  */
412 struct ubifs_inode {
413 	struct inode vfs_inode;
414 	unsigned long long creat_sqnum;
415 	unsigned long long del_cmtno;
416 	unsigned int xattr_size;
417 	unsigned int xattr_cnt;
418 	unsigned int xattr_names;
419 	unsigned int dirty:1;
420 	unsigned int xattr:1;
421 	unsigned int bulk_read:1;
422 	unsigned int compr_type:2;
423 	struct mutex ui_mutex;
424 	spinlock_t ui_lock;
425 	loff_t synced_i_size;
426 	loff_t ui_size;
427 	int flags;
428 	pgoff_t last_page_read;
429 	pgoff_t read_in_a_row;
430 	int data_len;
431 	void *data;
432 };
433 
434 /**
435  * struct ubifs_unclean_leb - records a LEB recovered under read-only mode.
436  * @list: list
437  * @lnum: LEB number of recovered LEB
438  * @endpt: offset where recovery ended
439  *
440  * This structure records a LEB identified during recovery that needs to be
441  * cleaned but was not because UBIFS was mounted read-only. The information
442  * is used to clean the LEB when remounting to read-write mode.
443  */
444 struct ubifs_unclean_leb {
445 	struct list_head list;
446 	int lnum;
447 	int endpt;
448 };
449 
450 /*
451  * LEB properties flags.
452  *
453  * LPROPS_UNCAT: not categorized
454  * LPROPS_DIRTY: dirty > free, dirty >= @c->dead_wm, not index
455  * LPROPS_DIRTY_IDX: dirty + free > @c->min_idx_node_sze and index
456  * LPROPS_FREE: free > 0, dirty < @c->dead_wm, not empty, not index
457  * LPROPS_HEAP_CNT: number of heaps used for storing categorized LEBs
458  * LPROPS_EMPTY: LEB is empty, not taken
459  * LPROPS_FREEABLE: free + dirty == leb_size, not index, not taken
460  * LPROPS_FRDI_IDX: free + dirty == leb_size and index, may be taken
461  * LPROPS_CAT_MASK: mask for the LEB categories above
462  * LPROPS_TAKEN: LEB was taken (this flag is not saved on the media)
463  * LPROPS_INDEX: LEB contains indexing nodes (this flag also exists on flash)
464  */
465 enum {
466 	LPROPS_UNCAT     =  0,
467 	LPROPS_DIRTY     =  1,
468 	LPROPS_DIRTY_IDX =  2,
469 	LPROPS_FREE      =  3,
470 	LPROPS_HEAP_CNT  =  3,
471 	LPROPS_EMPTY     =  4,
472 	LPROPS_FREEABLE  =  5,
473 	LPROPS_FRDI_IDX  =  6,
474 	LPROPS_CAT_MASK  = 15,
475 	LPROPS_TAKEN     = 16,
476 	LPROPS_INDEX     = 32,
477 };
478 
479 /**
480  * struct ubifs_lprops - logical eraseblock properties.
481  * @free: amount of free space in bytes
482  * @dirty: amount of dirty space in bytes
483  * @flags: LEB properties flags (see above)
484  * @lnum: LEB number
485  * @list: list of same-category lprops (for LPROPS_EMPTY and LPROPS_FREEABLE)
486  * @hpos: heap position in heap of same-category lprops (other categories)
487  */
488 struct ubifs_lprops {
489 	int free;
490 	int dirty;
491 	int flags;
492 	int lnum;
493 	union {
494 		struct list_head list;
495 		int hpos;
496 	};
497 };
498 
499 /**
500  * struct ubifs_lpt_lprops - LPT logical eraseblock properties.
501  * @free: amount of free space in bytes
502  * @dirty: amount of dirty space in bytes
503  * @tgc: trivial GC flag (1 => unmap after commit end)
504  * @cmt: commit flag (1 => reserved for commit)
505  */
506 struct ubifs_lpt_lprops {
507 	int free;
508 	int dirty;
509 	unsigned tgc:1;
510 	unsigned cmt:1;
511 };
512 
513 /**
514  * struct ubifs_lp_stats - statistics of eraseblocks in the main area.
515  * @empty_lebs: number of empty LEBs
516  * @taken_empty_lebs: number of taken LEBs
517  * @idx_lebs: number of indexing LEBs
518  * @total_free: total free space in bytes (includes all LEBs)
519  * @total_dirty: total dirty space in bytes (includes all LEBs)
520  * @total_used: total used space in bytes (does not include index LEBs)
521  * @total_dead: total dead space in bytes (does not include index LEBs)
522  * @total_dark: total dark space in bytes (does not include index LEBs)
523  *
524  * The @taken_empty_lebs field counts the LEBs that are in the transient state
525  * of having been "taken" for use but not yet written to. @taken_empty_lebs is
526  * needed to account correctly for @gc_lnum, otherwise @empty_lebs could be
527  * used by itself (in which case 'unused_lebs' would be a better name). In the
528  * case of @gc_lnum, it is "taken" at mount time or whenever a LEB is retained
529  * by GC, but unlike other empty LEBs that are "taken", it may not be written
530  * straight away (i.e. before the next commit start or unmount), so either
531  * @gc_lnum must be specially accounted for, or the current approach followed
532  * i.e. count it under @taken_empty_lebs.
533  *
534  * @empty_lebs includes @taken_empty_lebs.
535  *
536  * @total_used, @total_dead and @total_dark fields do not account indexing
537  * LEBs.
538  */
539 struct ubifs_lp_stats {
540 	int empty_lebs;
541 	int taken_empty_lebs;
542 	int idx_lebs;
543 	long long total_free;
544 	long long total_dirty;
545 	long long total_used;
546 	long long total_dead;
547 	long long total_dark;
548 };
549 
550 struct ubifs_nnode;
551 
552 /**
553  * struct ubifs_cnode - LEB Properties Tree common node.
554  * @parent: parent nnode
555  * @cnext: next cnode to commit
556  * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
557  * @iip: index in parent
558  * @level: level in the tree (zero for pnodes, greater than zero for nnodes)
559  * @num: node number
560  */
561 struct ubifs_cnode {
562 	struct ubifs_nnode *parent;
563 	struct ubifs_cnode *cnext;
564 	unsigned long flags;
565 	int iip;
566 	int level;
567 	int num;
568 };
569 
570 /**
571  * struct ubifs_pnode - LEB Properties Tree leaf node.
572  * @parent: parent nnode
573  * @cnext: next cnode to commit
574  * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
575  * @iip: index in parent
576  * @level: level in the tree (always zero for pnodes)
577  * @num: node number
578  * @lprops: LEB properties array
579  */
580 struct ubifs_pnode {
581 	struct ubifs_nnode *parent;
582 	struct ubifs_cnode *cnext;
583 	unsigned long flags;
584 	int iip;
585 	int level;
586 	int num;
587 	struct ubifs_lprops lprops[UBIFS_LPT_FANOUT];
588 };
589 
590 /**
591  * struct ubifs_nbranch - LEB Properties Tree internal node branch.
592  * @lnum: LEB number of child
593  * @offs: offset of child
594  * @nnode: nnode child
595  * @pnode: pnode child
596  * @cnode: cnode child
597  */
598 struct ubifs_nbranch {
599 	int lnum;
600 	int offs;
601 	union {
602 		struct ubifs_nnode *nnode;
603 		struct ubifs_pnode *pnode;
604 		struct ubifs_cnode *cnode;
605 	};
606 };
607 
608 /**
609  * struct ubifs_nnode - LEB Properties Tree internal node.
610  * @parent: parent nnode
611  * @cnext: next cnode to commit
612  * @flags: flags (%DIRTY_LPT_NODE or %OBSOLETE_LPT_NODE)
613  * @iip: index in parent
614  * @level: level in the tree (always greater than zero for nnodes)
615  * @num: node number
616  * @nbranch: branches to child nodes
617  */
618 struct ubifs_nnode {
619 	struct ubifs_nnode *parent;
620 	struct ubifs_cnode *cnext;
621 	unsigned long flags;
622 	int iip;
623 	int level;
624 	int num;
625 	struct ubifs_nbranch nbranch[UBIFS_LPT_FANOUT];
626 };
627 
628 /**
629  * struct ubifs_lpt_heap - heap of categorized lprops.
630  * @arr: heap array
631  * @cnt: number in heap
632  * @max_cnt: maximum number allowed in heap
633  *
634  * There are %LPROPS_HEAP_CNT heaps.
635  */
636 struct ubifs_lpt_heap {
637 	struct ubifs_lprops **arr;
638 	int cnt;
639 	int max_cnt;
640 };
641 
642 /*
643  * Return codes for LPT scan callback function.
644  *
645  * LPT_SCAN_CONTINUE: continue scanning
646  * LPT_SCAN_ADD: add the LEB properties scanned to the tree in memory
647  * LPT_SCAN_STOP: stop scanning
648  */
649 enum {
650 	LPT_SCAN_CONTINUE = 0,
651 	LPT_SCAN_ADD = 1,
652 	LPT_SCAN_STOP = 2,
653 };
654 
655 struct ubifs_info;
656 
657 /* Callback used by the 'ubifs_lpt_scan_nolock()' function */
658 typedef int (*ubifs_lpt_scan_callback)(struct ubifs_info *c,
659 				       const struct ubifs_lprops *lprops,
660 				       int in_tree, void *data);
661 
662 /**
663  * struct ubifs_wbuf - UBIFS write-buffer.
664  * @c: UBIFS file-system description object
665  * @buf: write-buffer (of min. flash I/O unit size)
666  * @lnum: logical eraseblock number the write-buffer points to
667  * @offs: write-buffer offset in this logical eraseblock
668  * @avail: number of bytes available in the write-buffer
669  * @used:  number of used bytes in the write-buffer
670  * @size: write-buffer size (in [@c->min_io_size, @c->max_write_size] range)
671  * @jhead: journal head the mutex belongs to (note, needed only to shut lockdep
672  *         up by 'mutex_lock_nested()).
673  * @sync_callback: write-buffer synchronization callback
674  * @io_mutex: serializes write-buffer I/O
675  * @lock: serializes @buf, @lnum, @offs, @avail, @used, @next_ino and @inodes
676  *        fields
677  * @timer: write-buffer timer
678  * @no_timer: non-zero if this write-buffer does not have a timer
679  * @need_sync: non-zero if the timer expired and the wbuf needs sync'ing
680  * @next_ino: points to the next position of the following inode number
681  * @inodes: stores the inode numbers of the nodes which are in wbuf
682  *
683  * The write-buffer synchronization callback is called when the write-buffer is
684  * synchronized in order to notify how much space was wasted due to
685  * write-buffer padding and how much free space is left in the LEB.
686  *
687  * Note: the fields @buf, @lnum, @offs, @avail and @used can be read under
688  * spin-lock or mutex because they are written under both mutex and spin-lock.
689  * @buf is appended to under mutex but overwritten under both mutex and
690  * spin-lock. Thus the data between @buf and @buf + @used can be read under
691  * spinlock.
692  */
693 struct ubifs_wbuf {
694 	struct ubifs_info *c;
695 	void *buf;
696 	int lnum;
697 	int offs;
698 	int avail;
699 	int used;
700 	int size;
701 	int jhead;
702 	int (*sync_callback)(struct ubifs_info *c, int lnum, int free, int pad);
703 	struct mutex io_mutex;
704 	spinlock_t lock;
705 	struct hrtimer timer;
706 	unsigned int no_timer:1;
707 	unsigned int need_sync:1;
708 	int next_ino;
709 	ino_t *inodes;
710 };
711 
712 /**
713  * struct ubifs_bud - bud logical eraseblock.
714  * @lnum: logical eraseblock number
715  * @start: where the (uncommitted) bud data starts
716  * @jhead: journal head number this bud belongs to
717  * @list: link in the list buds belonging to the same journal head
718  * @rb: link in the tree of all buds
719  * @log_hash: the log hash from the commit start node up to this bud
720  */
721 struct ubifs_bud {
722 	int lnum;
723 	int start;
724 	int jhead;
725 	struct list_head list;
726 	struct rb_node rb;
727 	struct shash_desc *log_hash;
728 };
729 
730 /**
731  * struct ubifs_jhead - journal head.
732  * @wbuf: head's write-buffer
733  * @buds_list: list of bud LEBs belonging to this journal head
734  * @grouped: non-zero if UBIFS groups nodes when writing to this journal head
735  * @log_hash: the log hash from the commit start node up to this journal head
736  *
737  * Note, the @buds list is protected by the @c->buds_lock.
738  */
739 struct ubifs_jhead {
740 	struct ubifs_wbuf wbuf;
741 	struct list_head buds_list;
742 	unsigned int grouped:1;
743 	struct shash_desc *log_hash;
744 };
745 
746 /**
747  * struct ubifs_zbranch - key/coordinate/length branch stored in znodes.
748  * @key: key
749  * @znode: znode address in memory
750  * @lnum: LEB number of the target node (indexing node or data node)
751  * @offs: target node offset within @lnum
752  * @len: target node length
753  * @hash: the hash of the target node
754  */
755 struct ubifs_zbranch {
756 	union ubifs_key key;
757 	union {
758 		struct ubifs_znode *znode;
759 		void *leaf;
760 	};
761 	int lnum;
762 	int offs;
763 	int len;
764 	u8 hash[UBIFS_HASH_ARR_SZ];
765 };
766 
767 /**
768  * struct ubifs_znode - in-memory representation of an indexing node.
769  * @parent: parent znode or NULL if it is the root
770  * @cnext: next znode to commit
771  * @cparent: parent node for this commit
772  * @ciip: index in cparent's zbranch array
773  * @flags: znode flags (%DIRTY_ZNODE, %COW_ZNODE or %OBSOLETE_ZNODE)
774  * @time: last access time (seconds)
775  * @level: level of the entry in the TNC tree
776  * @child_cnt: count of child znodes
777  * @iip: index in parent's zbranch array
778  * @alt: lower bound of key range has altered i.e. child inserted at slot 0
779  * @lnum: LEB number of the corresponding indexing node
780  * @offs: offset of the corresponding indexing node
781  * @len: length  of the corresponding indexing node
782  * @zbranch: array of znode branches (@c->fanout elements)
783  *
784  * Note! The @lnum, @offs, and @len fields are not really needed - we have them
785  * only for internal consistency check. They could be removed to save some RAM.
786  */
787 struct ubifs_znode {
788 	struct ubifs_znode *parent;
789 	struct ubifs_znode *cnext;
790 	struct ubifs_znode *cparent;
791 	int ciip;
792 	unsigned long flags;
793 	time64_t time;
794 	int level;
795 	int child_cnt;
796 	int iip;
797 	int alt;
798 	int lnum;
799 	int offs;
800 	int len;
801 	struct ubifs_zbranch zbranch[];
802 };
803 
804 /**
805  * struct bu_info - bulk-read information.
806  * @key: first data node key
807  * @zbranch: zbranches of data nodes to bulk read
808  * @buf: buffer to read into
809  * @buf_len: buffer length
810  * @gc_seq: GC sequence number to detect races with GC
811  * @cnt: number of data nodes for bulk read
812  * @blk_cnt: number of data blocks including holes
813  * @oef: end of file reached
814  */
815 struct bu_info {
816 	union ubifs_key key;
817 	struct ubifs_zbranch zbranch[UBIFS_MAX_BULK_READ];
818 	void *buf;
819 	int buf_len;
820 	int gc_seq;
821 	int cnt;
822 	int blk_cnt;
823 	int eof;
824 };
825 
826 /**
827  * struct ubifs_node_range - node length range description data structure.
828  * @len: fixed node length
829  * @min_len: minimum possible node length
830  * @max_len: maximum possible node length
831  *
832  * If @max_len is %0, the node has fixed length @len.
833  */
834 struct ubifs_node_range {
835 	union {
836 		int len;
837 		int min_len;
838 	};
839 	int max_len;
840 };
841 
842 /**
843  * struct ubifs_compressor - UBIFS compressor description structure.
844  * @compr_type: compressor type (%UBIFS_COMPR_LZO, etc)
845  * @cc: cryptoapi compressor handle
846  * @comp_mutex: mutex used during compression
847  * @decomp_mutex: mutex used during decompression
848  * @name: compressor name
849  * @capi_name: cryptoapi compressor name
850  */
851 struct ubifs_compressor {
852 	int compr_type;
853 	struct crypto_comp *cc;
854 	struct mutex *comp_mutex;
855 	struct mutex *decomp_mutex;
856 	const char *name;
857 	const char *capi_name;
858 };
859 
860 /**
861  * struct ubifs_budget_req - budget requirements of an operation.
862  *
863  * @fast: non-zero if the budgeting should try to acquire budget quickly and
864  *        should not try to call write-back
865  * @recalculate: non-zero if @idx_growth, @data_growth, and @dd_growth fields
866  *               have to be re-calculated
867  * @new_page: non-zero if the operation adds a new page
868  * @dirtied_page: non-zero if the operation makes a page dirty
869  * @new_dent: non-zero if the operation adds a new directory entry
870  * @mod_dent: non-zero if the operation removes or modifies an existing
871  *            directory entry
872  * @new_ino: non-zero if the operation adds a new inode
873  * @new_ino_d: how much data newly created inode contains
874  * @dirtied_ino: how many inodes the operation makes dirty
875  * @dirtied_ino_d: how much data dirtied inode contains
876  * @idx_growth: how much the index will supposedly grow
877  * @data_growth: how much new data the operation will supposedly add
878  * @dd_growth: how much data that makes other data dirty the operation will
879  *             supposedly add
880  *
881  * @idx_growth, @data_growth and @dd_growth are not used in budget request. The
882  * budgeting subsystem caches index and data growth values there to avoid
883  * re-calculating them when the budget is released. However, if @idx_growth is
884  * %-1, it is calculated by the release function using other fields.
885  *
886  * An inode may contain 4KiB of data at max., thus the widths of @new_ino_d
887  * is 13 bits, and @dirtied_ino_d - 15, because up to 4 inodes may be made
888  * dirty by the re-name operation.
889  *
890  * Note, UBIFS aligns node lengths to 8-bytes boundary, so the requester has to
891  * make sure the amount of inode data which contribute to @new_ino_d and
892  * @dirtied_ino_d fields are aligned.
893  */
894 struct ubifs_budget_req {
895 	unsigned int fast:1;
896 	unsigned int recalculate:1;
897 #ifndef UBIFS_DEBUG
898 	unsigned int new_page:1;
899 	unsigned int dirtied_page:1;
900 	unsigned int new_dent:1;
901 	unsigned int mod_dent:1;
902 	unsigned int new_ino:1;
903 	unsigned int new_ino_d:13;
904 	unsigned int dirtied_ino:4;
905 	unsigned int dirtied_ino_d:15;
906 #else
907 	/* Not bit-fields to check for overflows */
908 	unsigned int new_page;
909 	unsigned int dirtied_page;
910 	unsigned int new_dent;
911 	unsigned int mod_dent;
912 	unsigned int new_ino;
913 	unsigned int new_ino_d;
914 	unsigned int dirtied_ino;
915 	unsigned int dirtied_ino_d;
916 #endif
917 	int idx_growth;
918 	int data_growth;
919 	int dd_growth;
920 };
921 
922 /**
923  * struct ubifs_orphan - stores the inode number of an orphan.
924  * @rb: rb-tree node of rb-tree of orphans sorted by inode number
925  * @list: list head of list of orphans in order added
926  * @new_list: list head of list of orphans added since the last commit
927  * @cnext: next orphan to commit
928  * @dnext: next orphan to delete
929  * @inum: inode number
930  * @new: %1 => added since the last commit, otherwise %0
931  * @cmt: %1 => commit pending, otherwise %0
932  * @del: %1 => delete pending, otherwise %0
933  */
934 struct ubifs_orphan {
935 	struct rb_node rb;
936 	struct list_head list;
937 	struct list_head new_list;
938 	struct ubifs_orphan *cnext;
939 	struct ubifs_orphan *dnext;
940 	ino_t inum;
941 	unsigned new:1;
942 	unsigned cmt:1;
943 	unsigned del:1;
944 };
945 
946 /**
947  * struct ubifs_mount_opts - UBIFS-specific mount options information.
948  * @unmount_mode: selected unmount mode (%0 default, %1 normal, %2 fast)
949  * @bulk_read: enable/disable bulk-reads (%0 default, %1 disable, %2 enable)
950  * @chk_data_crc: enable/disable CRC data checking when reading data nodes
951  *                (%0 default, %1 disable, %2 enable)
952  * @override_compr: override default compressor (%0 - do not override and use
953  *                  superblock compressor, %1 - override and use compressor
954  *                  specified in @compr_type)
955  * @compr_type: compressor type to override the superblock compressor with
956  *              (%UBIFS_COMPR_NONE, etc)
957  */
958 struct ubifs_mount_opts {
959 	unsigned int unmount_mode:2;
960 	unsigned int bulk_read:2;
961 	unsigned int chk_data_crc:2;
962 	unsigned int override_compr:1;
963 	unsigned int compr_type:2;
964 };
965 
966 /**
967  * struct ubifs_budg_info - UBIFS budgeting information.
968  * @idx_growth: amount of bytes budgeted for index growth
969  * @data_growth: amount of bytes budgeted for cached data
970  * @dd_growth: amount of bytes budgeted for cached data that will make
971  *             other data dirty
972  * @uncommitted_idx: amount of bytes were budgeted for growth of the index, but
973  *                   which still have to be taken into account because the index
974  *                   has not been committed so far
975  * @old_idx_sz: size of index on flash
976  * @min_idx_lebs: minimum number of LEBs required for the index
977  * @nospace: non-zero if the file-system does not have flash space (used as
978  *           optimization)
979  * @nospace_rp: the same as @nospace, but additionally means that even reserved
980  *              pool is full
981  * @page_budget: budget for a page (constant, never changed after mount)
982  * @inode_budget: budget for an inode (constant, never changed after mount)
983  * @dent_budget: budget for a directory entry (constant, never changed after
984  *               mount)
985  */
986 struct ubifs_budg_info {
987 	long long idx_growth;
988 	long long data_growth;
989 	long long dd_growth;
990 	long long uncommitted_idx;
991 	unsigned long long old_idx_sz;
992 	int min_idx_lebs;
993 	unsigned int nospace:1;
994 	unsigned int nospace_rp:1;
995 	int page_budget;
996 	int inode_budget;
997 	int dent_budget;
998 };
999 
1000 struct ubifs_debug_info;
1001 
1002 /**
1003  * struct ubifs_info - UBIFS file-system description data structure
1004  * (per-superblock).
1005  * @vfs_sb: VFS @struct super_block object
1006  * @sup_node: The super block node as read from the device
1007  *
1008  * @highest_inum: highest used inode number
1009  * @max_sqnum: current global sequence number
1010  * @cmt_no: commit number of the last successfully completed commit, protected
1011  *          by @commit_sem
1012  * @cnt_lock: protects @highest_inum and @max_sqnum counters
1013  * @fmt_version: UBIFS on-flash format version
1014  * @ro_compat_version: R/O compatibility version
1015  * @uuid: UUID from super block
1016  *
1017  * @lhead_lnum: log head logical eraseblock number
1018  * @lhead_offs: log head offset
1019  * @ltail_lnum: log tail logical eraseblock number (offset is always 0)
1020  * @log_mutex: protects the log, @lhead_lnum, @lhead_offs, @ltail_lnum, and
1021  *             @bud_bytes
1022  * @min_log_bytes: minimum required number of bytes in the log
1023  * @cmt_bud_bytes: used during commit to temporarily amount of bytes in
1024  *                 committed buds
1025  *
1026  * @buds: tree of all buds indexed by bud LEB number
1027  * @bud_bytes: how many bytes of flash is used by buds
1028  * @buds_lock: protects the @buds tree, @bud_bytes, and per-journal head bud
1029  *             lists
1030  * @jhead_cnt: count of journal heads
1031  * @jheads: journal heads (head zero is base head)
1032  * @max_bud_bytes: maximum number of bytes allowed in buds
1033  * @bg_bud_bytes: number of bud bytes when background commit is initiated
1034  * @old_buds: buds to be released after commit ends
1035  * @max_bud_cnt: maximum number of buds
1036  *
1037  * @commit_sem: synchronizes committer with other processes
1038  * @cmt_state: commit state
1039  * @cs_lock: commit state lock
1040  * @cmt_wq: wait queue to sleep on if the log is full and a commit is running
1041  *
1042  * @big_lpt: flag that LPT is too big to write whole during commit
1043  * @space_fixup: flag indicating that free space in LEBs needs to be cleaned up
1044  * @double_hash: flag indicating that we can do lookups by hash
1045  * @encrypted: flag indicating that this file system contains encrypted files
1046  * @no_chk_data_crc: do not check CRCs when reading data nodes (except during
1047  *                   recovery)
1048  * @bulk_read: enable bulk-reads
1049  * @default_compr: default compression algorithm (%UBIFS_COMPR_LZO, etc)
1050  * @rw_incompat: the media is not R/W compatible
1051  * @assert_action: action to take when a ubifs_assert() fails
1052  * @authenticated: flag indigating the FS is mounted in authenticated mode
1053  *
1054  * @tnc_mutex: protects the Tree Node Cache (TNC), @zroot, @cnext, @enext, and
1055  *             @calc_idx_sz
1056  * @zroot: zbranch which points to the root index node and znode
1057  * @cnext: next znode to commit
1058  * @enext: next znode to commit to empty space
1059  * @gap_lebs: array of LEBs used by the in-gaps commit method
1060  * @cbuf: commit buffer
1061  * @ileb_buf: buffer for commit in-the-gaps method
1062  * @ileb_len: length of data in ileb_buf
1063  * @ihead_lnum: LEB number of index head
1064  * @ihead_offs: offset of index head
1065  * @ilebs: pre-allocated index LEBs
1066  * @ileb_cnt: number of pre-allocated index LEBs
1067  * @ileb_nxt: next pre-allocated index LEBs
1068  * @old_idx: tree of index nodes obsoleted since the last commit start
1069  * @bottom_up_buf: a buffer which is used by 'dirty_cow_bottom_up()' in tnc.c
1070  *
1071  * @mst_node: master node
1072  * @mst_offs: offset of valid master node
1073  *
1074  * @max_bu_buf_len: maximum bulk-read buffer length
1075  * @bu_mutex: protects the pre-allocated bulk-read buffer and @c->bu
1076  * @bu: pre-allocated bulk-read information
1077  *
1078  * @write_reserve_mutex: protects @write_reserve_buf
1079  * @write_reserve_buf: on the write path we allocate memory, which might
1080  *                     sometimes be unavailable, in which case we use this
1081  *                     write reserve buffer
1082  *
1083  * @log_lebs: number of logical eraseblocks in the log
1084  * @log_bytes: log size in bytes
1085  * @log_last: last LEB of the log
1086  * @lpt_lebs: number of LEBs used for lprops table
1087  * @lpt_first: first LEB of the lprops table area
1088  * @lpt_last: last LEB of the lprops table area
1089  * @orph_lebs: number of LEBs used for the orphan area
1090  * @orph_first: first LEB of the orphan area
1091  * @orph_last: last LEB of the orphan area
1092  * @main_lebs: count of LEBs in the main area
1093  * @main_first: first LEB of the main area
1094  * @main_bytes: main area size in bytes
1095  *
1096  * @key_hash_type: type of the key hash
1097  * @key_hash: direntry key hash function
1098  * @key_fmt: key format
1099  * @key_len: key length
1100  * @hash_len: The length of the index node hashes
1101  * @fanout: fanout of the index tree (number of links per indexing node)
1102  *
1103  * @min_io_size: minimal input/output unit size
1104  * @min_io_shift: number of bits in @min_io_size minus one
1105  * @max_write_size: maximum amount of bytes the underlying flash can write at a
1106  *                  time (MTD write buffer size)
1107  * @max_write_shift: number of bits in @max_write_size minus one
1108  * @leb_size: logical eraseblock size in bytes
1109  * @leb_start: starting offset of logical eraseblocks within physical
1110  *             eraseblocks
1111  * @half_leb_size: half LEB size
1112  * @idx_leb_size: how many bytes of an LEB are effectively available when it is
1113  *                used to store indexing nodes (@leb_size - @max_idx_node_sz)
1114  * @leb_cnt: count of logical eraseblocks
1115  * @max_leb_cnt: maximum count of logical eraseblocks
1116  * @old_leb_cnt: count of logical eraseblocks before re-size
1117  * @ro_media: the underlying UBI volume is read-only
1118  * @ro_mount: the file-system was mounted as read-only
1119  * @ro_error: UBIFS switched to R/O mode because an error happened
1120  *
1121  * @dirty_pg_cnt: number of dirty pages (not used)
1122  * @dirty_zn_cnt: number of dirty znodes
1123  * @clean_zn_cnt: number of clean znodes
1124  *
1125  * @space_lock: protects @bi and @lst
1126  * @lst: lprops statistics
1127  * @bi: budgeting information
1128  * @calc_idx_sz: temporary variable which is used to calculate new index size
1129  *               (contains accurate new index size at end of TNC commit start)
1130  *
1131  * @ref_node_alsz: size of the LEB reference node aligned to the min. flash
1132  *                 I/O unit
1133  * @mst_node_alsz: master node aligned size
1134  * @min_idx_node_sz: minimum indexing node aligned on 8-bytes boundary
1135  * @max_idx_node_sz: maximum indexing node aligned on 8-bytes boundary
1136  * @max_inode_sz: maximum possible inode size in bytes
1137  * @max_znode_sz: size of znode in bytes
1138  *
1139  * @leb_overhead: how many bytes are wasted in an LEB when it is filled with
1140  *                data nodes of maximum size - used in free space reporting
1141  * @dead_wm: LEB dead space watermark
1142  * @dark_wm: LEB dark space watermark
1143  * @block_cnt: count of 4KiB blocks on the FS
1144  *
1145  * @ranges: UBIFS node length ranges
1146  * @ubi: UBI volume descriptor
1147  * @di: UBI device information
1148  * @vi: UBI volume information
1149  *
1150  * @orph_tree: rb-tree of orphan inode numbers
1151  * @orph_list: list of orphan inode numbers in order added
1152  * @orph_new: list of orphan inode numbers added since last commit
1153  * @orph_cnext: next orphan to commit
1154  * @orph_dnext: next orphan to delete
1155  * @orphan_lock: lock for orph_tree and orph_new
1156  * @orph_buf: buffer for orphan nodes
1157  * @new_orphans: number of orphans since last commit
1158  * @cmt_orphans: number of orphans being committed
1159  * @tot_orphans: number of orphans in the rb_tree
1160  * @max_orphans: maximum number of orphans allowed
1161  * @ohead_lnum: orphan head LEB number
1162  * @ohead_offs: orphan head offset
1163  * @no_orphs: non-zero if there are no orphans
1164  *
1165  * @bgt: UBIFS background thread
1166  * @bgt_name: background thread name
1167  * @need_bgt: if background thread should run
1168  * @need_wbuf_sync: if write-buffers have to be synchronized
1169  *
1170  * @gc_lnum: LEB number used for garbage collection
1171  * @sbuf: a buffer of LEB size used by GC and replay for scanning
1172  * @idx_gc: list of index LEBs that have been garbage collected
1173  * @idx_gc_cnt: number of elements on the idx_gc list
1174  * @gc_seq: incremented for every non-index LEB garbage collected
1175  * @gced_lnum: last non-index LEB that was garbage collected
1176  *
1177  * @infos_list: links all 'ubifs_info' objects
1178  * @umount_mutex: serializes shrinker and un-mount
1179  * @shrinker_run_no: shrinker run number
1180  *
1181  * @space_bits: number of bits needed to record free or dirty space
1182  * @lpt_lnum_bits: number of bits needed to record a LEB number in the LPT
1183  * @lpt_offs_bits: number of bits needed to record an offset in the LPT
1184  * @lpt_spc_bits: number of bits needed to space in the LPT
1185  * @pcnt_bits: number of bits needed to record pnode or nnode number
1186  * @lnum_bits: number of bits needed to record LEB number
1187  * @nnode_sz: size of on-flash nnode
1188  * @pnode_sz: size of on-flash pnode
1189  * @ltab_sz: size of on-flash LPT lprops table
1190  * @lsave_sz: size of on-flash LPT save table
1191  * @pnode_cnt: number of pnodes
1192  * @nnode_cnt: number of nnodes
1193  * @lpt_hght: height of the LPT
1194  * @pnodes_have: number of pnodes in memory
1195  *
1196  * @lp_mutex: protects lprops table and all the other lprops-related fields
1197  * @lpt_lnum: LEB number of the root nnode of the LPT
1198  * @lpt_offs: offset of the root nnode of the LPT
1199  * @nhead_lnum: LEB number of LPT head
1200  * @nhead_offs: offset of LPT head
1201  * @lpt_drty_flgs: dirty flags for LPT special nodes e.g. ltab
1202  * @dirty_nn_cnt: number of dirty nnodes
1203  * @dirty_pn_cnt: number of dirty pnodes
1204  * @check_lpt_free: flag that indicates LPT GC may be needed
1205  * @lpt_sz: LPT size
1206  * @lpt_nod_buf: buffer for an on-flash nnode or pnode
1207  * @lpt_buf: buffer of LEB size used by LPT
1208  * @nroot: address in memory of the root nnode of the LPT
1209  * @lpt_cnext: next LPT node to commit
1210  * @lpt_heap: array of heaps of categorized lprops
1211  * @dirty_idx: a (reverse sorted) copy of the LPROPS_DIRTY_IDX heap as at
1212  *             previous commit start
1213  * @uncat_list: list of un-categorized LEBs
1214  * @empty_list: list of empty LEBs
1215  * @freeable_list: list of freeable non-index LEBs (free + dirty == @leb_size)
1216  * @frdi_idx_list: list of freeable index LEBs (free + dirty == @leb_size)
1217  * @freeable_cnt: number of freeable LEBs in @freeable_list
1218  * @in_a_category_cnt: count of lprops which are in a certain category, which
1219  *                     basically meants that they were loaded from the flash
1220  *
1221  * @ltab_lnum: LEB number of LPT's own lprops table
1222  * @ltab_offs: offset of LPT's own lprops table
1223  * @ltab: LPT's own lprops table
1224  * @ltab_cmt: LPT's own lprops table (commit copy)
1225  * @lsave_cnt: number of LEB numbers in LPT's save table
1226  * @lsave_lnum: LEB number of LPT's save table
1227  * @lsave_offs: offset of LPT's save table
1228  * @lsave: LPT's save table
1229  * @lscan_lnum: LEB number of last LPT scan
1230  *
1231  * @rp_size: size of the reserved pool in bytes
1232  * @report_rp_size: size of the reserved pool reported to user-space
1233  * @rp_uid: reserved pool user ID
1234  * @rp_gid: reserved pool group ID
1235  *
1236  * @hash_tfm: the hash transformation used for hashing nodes
1237  * @hmac_tfm: the HMAC transformation for this filesystem
1238  * @hmac_desc_len: length of the HMAC used for authentication
1239  * @auth_key_name: the authentication key name
1240  * @auth_hash_name: the name of the hash algorithm used for authentication
1241  * @auth_hash_algo: the authentication hash used for this fs
1242  * @log_hash: the log hash from the commit start node up to the latest reference
1243  *            node.
1244  *
1245  * @empty: %1 if the UBI device is empty
1246  * @need_recovery: %1 if the file-system needs recovery
1247  * @replaying: %1 during journal replay
1248  * @mounting: %1 while mounting
1249  * @probing: %1 while attempting to mount if SB_SILENT mount flag is set
1250  * @remounting_rw: %1 while re-mounting from R/O mode to R/W mode
1251  * @replay_list: temporary list used during journal replay
1252  * @replay_buds: list of buds to replay
1253  * @cs_sqnum: sequence number of first node in the log (commit start node)
1254  * @unclean_leb_list: LEBs to recover when re-mounting R/O mounted FS to R/W
1255  *                    mode
1256  * @rcvrd_mst_node: recovered master node to write when re-mounting R/O mounted
1257  *                  FS to R/W mode
1258  * @size_tree: inode size information for recovery
1259  * @mount_opts: UBIFS-specific mount options
1260  *
1261  * @dbg: debugging-related information
1262  */
1263 struct ubifs_info {
1264 	struct super_block *vfs_sb;
1265 	struct ubifs_sb_node *sup_node;
1266 
1267 	ino_t highest_inum;
1268 	unsigned long long max_sqnum;
1269 	unsigned long long cmt_no;
1270 	spinlock_t cnt_lock;
1271 	int fmt_version;
1272 	int ro_compat_version;
1273 	unsigned char uuid[16];
1274 
1275 	int lhead_lnum;
1276 	int lhead_offs;
1277 	int ltail_lnum;
1278 	struct mutex log_mutex;
1279 	int min_log_bytes;
1280 	long long cmt_bud_bytes;
1281 
1282 	struct rb_root buds;
1283 	long long bud_bytes;
1284 	spinlock_t buds_lock;
1285 	int jhead_cnt;
1286 	struct ubifs_jhead *jheads;
1287 	long long max_bud_bytes;
1288 	long long bg_bud_bytes;
1289 	struct list_head old_buds;
1290 	int max_bud_cnt;
1291 
1292 	struct rw_semaphore commit_sem;
1293 	int cmt_state;
1294 	spinlock_t cs_lock;
1295 	wait_queue_head_t cmt_wq;
1296 
1297 	unsigned int big_lpt:1;
1298 	unsigned int space_fixup:1;
1299 	unsigned int double_hash:1;
1300 	unsigned int encrypted:1;
1301 	unsigned int no_chk_data_crc:1;
1302 	unsigned int bulk_read:1;
1303 	unsigned int default_compr:2;
1304 	unsigned int rw_incompat:1;
1305 	unsigned int assert_action:2;
1306 	unsigned int authenticated:1;
1307 
1308 	struct mutex tnc_mutex;
1309 	struct ubifs_zbranch zroot;
1310 	struct ubifs_znode *cnext;
1311 	struct ubifs_znode *enext;
1312 	int *gap_lebs;
1313 	void *cbuf;
1314 	void *ileb_buf;
1315 	int ileb_len;
1316 	int ihead_lnum;
1317 	int ihead_offs;
1318 	int *ilebs;
1319 	int ileb_cnt;
1320 	int ileb_nxt;
1321 	struct rb_root old_idx;
1322 	int *bottom_up_buf;
1323 
1324 	struct ubifs_mst_node *mst_node;
1325 	int mst_offs;
1326 
1327 	int max_bu_buf_len;
1328 	struct mutex bu_mutex;
1329 	struct bu_info bu;
1330 
1331 	struct mutex write_reserve_mutex;
1332 	void *write_reserve_buf;
1333 
1334 	int log_lebs;
1335 	long long log_bytes;
1336 	int log_last;
1337 	int lpt_lebs;
1338 	int lpt_first;
1339 	int lpt_last;
1340 	int orph_lebs;
1341 	int orph_first;
1342 	int orph_last;
1343 	int main_lebs;
1344 	int main_first;
1345 	long long main_bytes;
1346 
1347 	uint8_t key_hash_type;
1348 	uint32_t (*key_hash)(const char *str, int len);
1349 	int key_fmt;
1350 	int key_len;
1351 	int hash_len;
1352 	int fanout;
1353 
1354 	int min_io_size;
1355 	int min_io_shift;
1356 	int max_write_size;
1357 	int max_write_shift;
1358 	int leb_size;
1359 	int leb_start;
1360 	int half_leb_size;
1361 	int idx_leb_size;
1362 	int leb_cnt;
1363 	int max_leb_cnt;
1364 	int old_leb_cnt;
1365 	unsigned int ro_media:1;
1366 	unsigned int ro_mount:1;
1367 	unsigned int ro_error:1;
1368 
1369 	atomic_long_t dirty_pg_cnt;
1370 	atomic_long_t dirty_zn_cnt;
1371 	atomic_long_t clean_zn_cnt;
1372 
1373 	spinlock_t space_lock;
1374 	struct ubifs_lp_stats lst;
1375 	struct ubifs_budg_info bi;
1376 	unsigned long long calc_idx_sz;
1377 
1378 	int ref_node_alsz;
1379 	int mst_node_alsz;
1380 	int min_idx_node_sz;
1381 	int max_idx_node_sz;
1382 	long long max_inode_sz;
1383 	int max_znode_sz;
1384 
1385 	int leb_overhead;
1386 	int dead_wm;
1387 	int dark_wm;
1388 	int block_cnt;
1389 
1390 	struct ubifs_node_range ranges[UBIFS_NODE_TYPES_CNT];
1391 	struct ubi_volume_desc *ubi;
1392 	struct ubi_device_info di;
1393 	struct ubi_volume_info vi;
1394 
1395 	struct rb_root orph_tree;
1396 	struct list_head orph_list;
1397 	struct list_head orph_new;
1398 	struct ubifs_orphan *orph_cnext;
1399 	struct ubifs_orphan *orph_dnext;
1400 	spinlock_t orphan_lock;
1401 	void *orph_buf;
1402 	int new_orphans;
1403 	int cmt_orphans;
1404 	int tot_orphans;
1405 	int max_orphans;
1406 	int ohead_lnum;
1407 	int ohead_offs;
1408 	int no_orphs;
1409 
1410 	struct task_struct *bgt;
1411 	char bgt_name[sizeof(BGT_NAME_PATTERN) + 9];
1412 	int need_bgt;
1413 	int need_wbuf_sync;
1414 
1415 	int gc_lnum;
1416 	void *sbuf;
1417 	struct list_head idx_gc;
1418 	int idx_gc_cnt;
1419 	int gc_seq;
1420 	int gced_lnum;
1421 
1422 	struct list_head infos_list;
1423 	struct mutex umount_mutex;
1424 	unsigned int shrinker_run_no;
1425 
1426 	int space_bits;
1427 	int lpt_lnum_bits;
1428 	int lpt_offs_bits;
1429 	int lpt_spc_bits;
1430 	int pcnt_bits;
1431 	int lnum_bits;
1432 	int nnode_sz;
1433 	int pnode_sz;
1434 	int ltab_sz;
1435 	int lsave_sz;
1436 	int pnode_cnt;
1437 	int nnode_cnt;
1438 	int lpt_hght;
1439 	int pnodes_have;
1440 
1441 	struct mutex lp_mutex;
1442 	int lpt_lnum;
1443 	int lpt_offs;
1444 	int nhead_lnum;
1445 	int nhead_offs;
1446 	int lpt_drty_flgs;
1447 	int dirty_nn_cnt;
1448 	int dirty_pn_cnt;
1449 	int check_lpt_free;
1450 	long long lpt_sz;
1451 	void *lpt_nod_buf;
1452 	void *lpt_buf;
1453 	struct ubifs_nnode *nroot;
1454 	struct ubifs_cnode *lpt_cnext;
1455 	struct ubifs_lpt_heap lpt_heap[LPROPS_HEAP_CNT];
1456 	struct ubifs_lpt_heap dirty_idx;
1457 	struct list_head uncat_list;
1458 	struct list_head empty_list;
1459 	struct list_head freeable_list;
1460 	struct list_head frdi_idx_list;
1461 	int freeable_cnt;
1462 	int in_a_category_cnt;
1463 
1464 	int ltab_lnum;
1465 	int ltab_offs;
1466 	struct ubifs_lpt_lprops *ltab;
1467 	struct ubifs_lpt_lprops *ltab_cmt;
1468 	int lsave_cnt;
1469 	int lsave_lnum;
1470 	int lsave_offs;
1471 	int *lsave;
1472 	int lscan_lnum;
1473 
1474 	long long rp_size;
1475 	long long report_rp_size;
1476 	kuid_t rp_uid;
1477 	kgid_t rp_gid;
1478 
1479 	struct crypto_shash *hash_tfm;
1480 	struct crypto_shash *hmac_tfm;
1481 	int hmac_desc_len;
1482 	char *auth_key_name;
1483 	char *auth_hash_name;
1484 	enum hash_algo auth_hash_algo;
1485 
1486 	struct shash_desc *log_hash;
1487 
1488 	/* The below fields are used only during mounting and re-mounting */
1489 	unsigned int empty:1;
1490 	unsigned int need_recovery:1;
1491 	unsigned int replaying:1;
1492 	unsigned int mounting:1;
1493 	unsigned int remounting_rw:1;
1494 	unsigned int probing:1;
1495 	struct list_head replay_list;
1496 	struct list_head replay_buds;
1497 	unsigned long long cs_sqnum;
1498 	struct list_head unclean_leb_list;
1499 	struct ubifs_mst_node *rcvrd_mst_node;
1500 	struct rb_root size_tree;
1501 	struct ubifs_mount_opts mount_opts;
1502 
1503 	struct ubifs_debug_info *dbg;
1504 };
1505 
1506 extern struct list_head ubifs_infos;
1507 extern spinlock_t ubifs_infos_lock;
1508 extern atomic_long_t ubifs_clean_zn_cnt;
1509 extern const struct super_operations ubifs_super_operations;
1510 extern const struct address_space_operations ubifs_file_address_operations;
1511 extern const struct file_operations ubifs_file_operations;
1512 extern const struct inode_operations ubifs_file_inode_operations;
1513 extern const struct file_operations ubifs_dir_operations;
1514 extern const struct inode_operations ubifs_dir_inode_operations;
1515 extern const struct inode_operations ubifs_symlink_inode_operations;
1516 extern struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
1517 
1518 /* auth.c */
1519 static inline int ubifs_authenticated(const struct ubifs_info *c)
1520 {
1521 	return (IS_ENABLED(CONFIG_UBIFS_FS_AUTHENTICATION)) && c->authenticated;
1522 }
1523 
1524 struct shash_desc *__ubifs_hash_get_desc(const struct ubifs_info *c);
1525 static inline struct shash_desc *ubifs_hash_get_desc(const struct ubifs_info *c)
1526 {
1527 	return ubifs_authenticated(c) ? __ubifs_hash_get_desc(c) : NULL;
1528 }
1529 
1530 static inline int ubifs_shash_init(const struct ubifs_info *c,
1531 				   struct shash_desc *desc)
1532 {
1533 	if (ubifs_authenticated(c))
1534 		return crypto_shash_init(desc);
1535 	else
1536 		return 0;
1537 }
1538 
1539 static inline int ubifs_shash_update(const struct ubifs_info *c,
1540 				      struct shash_desc *desc, const void *buf,
1541 				      unsigned int len)
1542 {
1543 	int err = 0;
1544 
1545 	if (ubifs_authenticated(c)) {
1546 		err = crypto_shash_update(desc, buf, len);
1547 		if (err < 0)
1548 			return err;
1549 	}
1550 
1551 	return 0;
1552 }
1553 
1554 static inline int ubifs_shash_final(const struct ubifs_info *c,
1555 				    struct shash_desc *desc, u8 *out)
1556 {
1557 	return ubifs_authenticated(c) ? crypto_shash_final(desc, out) : 0;
1558 }
1559 
1560 int __ubifs_node_calc_hash(const struct ubifs_info *c, const void *buf,
1561 			  u8 *hash);
1562 static inline int ubifs_node_calc_hash(const struct ubifs_info *c,
1563 					const void *buf, u8 *hash)
1564 {
1565 	if (ubifs_authenticated(c))
1566 		return __ubifs_node_calc_hash(c, buf, hash);
1567 	else
1568 		return 0;
1569 }
1570 
1571 int ubifs_prepare_auth_node(struct ubifs_info *c, void *node,
1572 			     struct shash_desc *inhash);
1573 
1574 /**
1575  * ubifs_check_hash - compare two hashes
1576  * @c: UBIFS file-system description object
1577  * @expected: first hash
1578  * @got: second hash
1579  *
1580  * Compare two hashes @expected and @got. Returns 0 when they are equal, a
1581  * negative error code otherwise.
1582  */
1583 static inline int ubifs_check_hash(const struct ubifs_info *c,
1584 				   const u8 *expected, const u8 *got)
1585 {
1586 	return crypto_memneq(expected, got, c->hash_len);
1587 }
1588 
1589 /**
1590  * ubifs_check_hmac - compare two HMACs
1591  * @c: UBIFS file-system description object
1592  * @expected: first HMAC
1593  * @got: second HMAC
1594  *
1595  * Compare two hashes @expected and @got. Returns 0 when they are equal, a
1596  * negative error code otherwise.
1597  */
1598 static inline int ubifs_check_hmac(const struct ubifs_info *c,
1599 				   const u8 *expected, const u8 *got)
1600 {
1601 	return crypto_memneq(expected, got, c->hmac_desc_len);
1602 }
1603 
1604 void ubifs_bad_hash(const struct ubifs_info *c, const void *node,
1605 		    const u8 *hash, int lnum, int offs);
1606 
1607 int __ubifs_node_check_hash(const struct ubifs_info *c, const void *buf,
1608 			  const u8 *expected);
1609 static inline int ubifs_node_check_hash(const struct ubifs_info *c,
1610 					const void *buf, const u8 *expected)
1611 {
1612 	if (ubifs_authenticated(c))
1613 		return __ubifs_node_check_hash(c, buf, expected);
1614 	else
1615 		return 0;
1616 }
1617 
1618 int ubifs_init_authentication(struct ubifs_info *c);
1619 void __ubifs_exit_authentication(struct ubifs_info *c);
1620 static inline void ubifs_exit_authentication(struct ubifs_info *c)
1621 {
1622 	if (ubifs_authenticated(c))
1623 		__ubifs_exit_authentication(c);
1624 }
1625 
1626 /**
1627  * ubifs_branch_hash - returns a pointer to the hash of a branch
1628  * @c: UBIFS file-system description object
1629  * @br: branch to get the hash from
1630  *
1631  * This returns a pointer to the hash of a branch. Since the key already is a
1632  * dynamically sized object we cannot use a struct member here.
1633  */
1634 static inline u8 *ubifs_branch_hash(struct ubifs_info *c,
1635 				    struct ubifs_branch *br)
1636 {
1637 	return (void *)br + sizeof(*br) + c->key_len;
1638 }
1639 
1640 /**
1641  * ubifs_copy_hash - copy a hash
1642  * @c: UBIFS file-system description object
1643  * @from: source hash
1644  * @to: destination hash
1645  *
1646  * With authentication this copies a hash, otherwise does nothing.
1647  */
1648 static inline void ubifs_copy_hash(const struct ubifs_info *c, const u8 *from,
1649 				   u8 *to)
1650 {
1651 	if (ubifs_authenticated(c))
1652 		memcpy(to, from, c->hash_len);
1653 }
1654 
1655 int __ubifs_node_insert_hmac(const struct ubifs_info *c, void *buf,
1656 			      int len, int ofs_hmac);
1657 static inline int ubifs_node_insert_hmac(const struct ubifs_info *c, void *buf,
1658 					  int len, int ofs_hmac)
1659 {
1660 	if (ubifs_authenticated(c))
1661 		return __ubifs_node_insert_hmac(c, buf, len, ofs_hmac);
1662 	else
1663 		return 0;
1664 }
1665 
1666 int __ubifs_node_verify_hmac(const struct ubifs_info *c, const void *buf,
1667 			     int len, int ofs_hmac);
1668 static inline int ubifs_node_verify_hmac(const struct ubifs_info *c,
1669 					 const void *buf, int len, int ofs_hmac)
1670 {
1671 	if (ubifs_authenticated(c))
1672 		return __ubifs_node_verify_hmac(c, buf, len, ofs_hmac);
1673 	else
1674 		return 0;
1675 }
1676 
1677 /**
1678  * ubifs_auth_node_sz - returns the size of an authentication node
1679  * @c: UBIFS file-system description object
1680  *
1681  * This function returns the size of an authentication node which can
1682  * be 0 for unauthenticated filesystems or the real size of an auth node
1683  * authentication is enabled.
1684  */
1685 static inline int ubifs_auth_node_sz(const struct ubifs_info *c)
1686 {
1687 	if (ubifs_authenticated(c))
1688 		return sizeof(struct ubifs_auth_node) + c->hmac_desc_len;
1689 	else
1690 		return 0;
1691 }
1692 
1693 int ubifs_hmac_wkm(struct ubifs_info *c, u8 *hmac);
1694 
1695 int __ubifs_shash_copy_state(const struct ubifs_info *c, struct shash_desc *src,
1696 			     struct shash_desc *target);
1697 static inline int ubifs_shash_copy_state(const struct ubifs_info *c,
1698 					   struct shash_desc *src,
1699 					   struct shash_desc *target)
1700 {
1701 	if (ubifs_authenticated(c))
1702 		return __ubifs_shash_copy_state(c, src, target);
1703 	else
1704 		return 0;
1705 }
1706 
1707 /* io.c */
1708 void ubifs_ro_mode(struct ubifs_info *c, int err);
1709 int ubifs_leb_read(const struct ubifs_info *c, int lnum, void *buf, int offs,
1710 		   int len, int even_ebadmsg);
1711 int ubifs_leb_write(struct ubifs_info *c, int lnum, const void *buf, int offs,
1712 		    int len);
1713 int ubifs_leb_change(struct ubifs_info *c, int lnum, const void *buf, int len);
1714 int ubifs_leb_unmap(struct ubifs_info *c, int lnum);
1715 int ubifs_leb_map(struct ubifs_info *c, int lnum);
1716 int ubifs_is_mapped(const struct ubifs_info *c, int lnum);
1717 int ubifs_wbuf_write_nolock(struct ubifs_wbuf *wbuf, void *buf, int len);
1718 int ubifs_wbuf_seek_nolock(struct ubifs_wbuf *wbuf, int lnum, int offs);
1719 int ubifs_wbuf_init(struct ubifs_info *c, struct ubifs_wbuf *wbuf);
1720 int ubifs_read_node(const struct ubifs_info *c, void *buf, int type, int len,
1721 		    int lnum, int offs);
1722 int ubifs_read_node_wbuf(struct ubifs_wbuf *wbuf, void *buf, int type, int len,
1723 			 int lnum, int offs);
1724 int ubifs_write_node(struct ubifs_info *c, void *node, int len, int lnum,
1725 		     int offs);
1726 int ubifs_write_node_hmac(struct ubifs_info *c, void *buf, int len, int lnum,
1727 			  int offs, int hmac_offs);
1728 int ubifs_check_node(const struct ubifs_info *c, const void *buf, int lnum,
1729 		     int offs, int quiet, int must_chk_crc);
1730 void ubifs_init_node(struct ubifs_info *c, void *buf, int len, int pad);
1731 void ubifs_crc_node(struct ubifs_info *c, void *buf, int len);
1732 void ubifs_prepare_node(struct ubifs_info *c, void *buf, int len, int pad);
1733 int ubifs_prepare_node_hmac(struct ubifs_info *c, void *node, int len,
1734 			    int hmac_offs, int pad);
1735 void ubifs_prep_grp_node(struct ubifs_info *c, void *node, int len, int last);
1736 int ubifs_io_init(struct ubifs_info *c);
1737 void ubifs_pad(const struct ubifs_info *c, void *buf, int pad);
1738 int ubifs_wbuf_sync_nolock(struct ubifs_wbuf *wbuf);
1739 int ubifs_bg_wbufs_sync(struct ubifs_info *c);
1740 void ubifs_wbuf_add_ino_nolock(struct ubifs_wbuf *wbuf, ino_t inum);
1741 int ubifs_sync_wbufs_by_inode(struct ubifs_info *c, struct inode *inode);
1742 
1743 /* scan.c */
1744 struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
1745 				  int offs, void *sbuf, int quiet);
1746 void ubifs_scan_destroy(struct ubifs_scan_leb *sleb);
1747 int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum,
1748 		      int offs, int quiet);
1749 struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
1750 					int offs, void *sbuf);
1751 void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
1752 		    int lnum, int offs);
1753 int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
1754 		   void *buf, int offs);
1755 void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
1756 			      void *buf);
1757 
1758 /* log.c */
1759 void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud);
1760 void ubifs_create_buds_lists(struct ubifs_info *c);
1761 int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs);
1762 struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum);
1763 struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum);
1764 int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum);
1765 int ubifs_log_end_commit(struct ubifs_info *c, int new_ltail_lnum);
1766 int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum);
1767 int ubifs_consolidate_log(struct ubifs_info *c);
1768 
1769 /* journal.c */
1770 int ubifs_jnl_update(struct ubifs_info *c, const struct inode *dir,
1771 		     const struct fscrypt_name *nm, const struct inode *inode,
1772 		     int deletion, int xent);
1773 int ubifs_jnl_write_data(struct ubifs_info *c, const struct inode *inode,
1774 			 const union ubifs_key *key, const void *buf, int len);
1775 int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode);
1776 int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode);
1777 int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
1778 		      const struct inode *fst_inode,
1779 		      const struct fscrypt_name *fst_nm,
1780 		      const struct inode *snd_dir,
1781 		      const struct inode *snd_inode,
1782 		      const struct fscrypt_name *snd_nm, int sync);
1783 int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
1784 		     const struct inode *old_inode,
1785 		     const struct fscrypt_name *old_nm,
1786 		     const struct inode *new_dir,
1787 		     const struct inode *new_inode,
1788 		     const struct fscrypt_name *new_nm,
1789 		     const struct inode *whiteout, int sync);
1790 int ubifs_jnl_truncate(struct ubifs_info *c, const struct inode *inode,
1791 		       loff_t old_size, loff_t new_size);
1792 int ubifs_jnl_delete_xattr(struct ubifs_info *c, const struct inode *host,
1793 			   const struct inode *inode, const struct fscrypt_name *nm);
1794 int ubifs_jnl_change_xattr(struct ubifs_info *c, const struct inode *inode1,
1795 			   const struct inode *inode2);
1796 
1797 /* budget.c */
1798 int ubifs_budget_space(struct ubifs_info *c, struct ubifs_budget_req *req);
1799 void ubifs_release_budget(struct ubifs_info *c, struct ubifs_budget_req *req);
1800 void ubifs_release_dirty_inode_budget(struct ubifs_info *c,
1801 				      struct ubifs_inode *ui);
1802 int ubifs_budget_inode_op(struct ubifs_info *c, struct inode *inode,
1803 			  struct ubifs_budget_req *req);
1804 void ubifs_release_ino_dirty(struct ubifs_info *c, struct inode *inode,
1805 				struct ubifs_budget_req *req);
1806 void ubifs_cancel_ino_op(struct ubifs_info *c, struct inode *inode,
1807 			 struct ubifs_budget_req *req);
1808 long long ubifs_get_free_space(struct ubifs_info *c);
1809 long long ubifs_get_free_space_nolock(struct ubifs_info *c);
1810 int ubifs_calc_min_idx_lebs(struct ubifs_info *c);
1811 void ubifs_convert_page_budget(struct ubifs_info *c);
1812 long long ubifs_reported_space(const struct ubifs_info *c, long long free);
1813 long long ubifs_calc_available(const struct ubifs_info *c, int min_idx_lebs);
1814 
1815 /* find.c */
1816 int ubifs_find_free_space(struct ubifs_info *c, int min_space, int *offs,
1817 			  int squeeze);
1818 int ubifs_find_free_leb_for_idx(struct ubifs_info *c);
1819 int ubifs_find_dirty_leb(struct ubifs_info *c, struct ubifs_lprops *ret_lp,
1820 			 int min_space, int pick_free);
1821 int ubifs_find_dirty_idx_leb(struct ubifs_info *c);
1822 int ubifs_save_dirty_idx_lnums(struct ubifs_info *c);
1823 
1824 /* tnc.c */
1825 int ubifs_lookup_level0(struct ubifs_info *c, const union ubifs_key *key,
1826 			struct ubifs_znode **zn, int *n);
1827 int ubifs_tnc_lookup_nm(struct ubifs_info *c, const union ubifs_key *key,
1828 			void *node, const struct fscrypt_name *nm);
1829 int ubifs_tnc_lookup_dh(struct ubifs_info *c, const union ubifs_key *key,
1830 			void *node, uint32_t secondary_hash);
1831 int ubifs_tnc_locate(struct ubifs_info *c, const union ubifs_key *key,
1832 		     void *node, int *lnum, int *offs);
1833 int ubifs_tnc_add(struct ubifs_info *c, const union ubifs_key *key, int lnum,
1834 		  int offs, int len, const u8 *hash);
1835 int ubifs_tnc_replace(struct ubifs_info *c, const union ubifs_key *key,
1836 		      int old_lnum, int old_offs, int lnum, int offs, int len);
1837 int ubifs_tnc_add_nm(struct ubifs_info *c, const union ubifs_key *key,
1838 		     int lnum, int offs, int len, const u8 *hash,
1839 		     const struct fscrypt_name *nm);
1840 int ubifs_tnc_remove(struct ubifs_info *c, const union ubifs_key *key);
1841 int ubifs_tnc_remove_nm(struct ubifs_info *c, const union ubifs_key *key,
1842 			const struct fscrypt_name *nm);
1843 int ubifs_tnc_remove_dh(struct ubifs_info *c, const union ubifs_key *key,
1844 			uint32_t cookie);
1845 int ubifs_tnc_remove_range(struct ubifs_info *c, union ubifs_key *from_key,
1846 			   union ubifs_key *to_key);
1847 int ubifs_tnc_remove_ino(struct ubifs_info *c, ino_t inum);
1848 struct ubifs_dent_node *ubifs_tnc_next_ent(struct ubifs_info *c,
1849 					   union ubifs_key *key,
1850 					   const struct fscrypt_name *nm);
1851 void ubifs_tnc_close(struct ubifs_info *c);
1852 int ubifs_tnc_has_node(struct ubifs_info *c, union ubifs_key *key, int level,
1853 		       int lnum, int offs, int is_idx);
1854 int ubifs_dirty_idx_node(struct ubifs_info *c, union ubifs_key *key, int level,
1855 			 int lnum, int offs);
1856 /* Shared by tnc.c for tnc_commit.c */
1857 void destroy_old_idx(struct ubifs_info *c);
1858 int is_idx_node_in_tnc(struct ubifs_info *c, union ubifs_key *key, int level,
1859 		       int lnum, int offs);
1860 int insert_old_idx_znode(struct ubifs_info *c, struct ubifs_znode *znode);
1861 int ubifs_tnc_get_bu_keys(struct ubifs_info *c, struct bu_info *bu);
1862 int ubifs_tnc_bulk_read(struct ubifs_info *c, struct bu_info *bu);
1863 
1864 /* tnc_misc.c */
1865 struct ubifs_znode *ubifs_tnc_levelorder_next(const struct ubifs_info *c,
1866 					      struct ubifs_znode *zr,
1867 					      struct ubifs_znode *znode);
1868 int ubifs_search_zbranch(const struct ubifs_info *c,
1869 			 const struct ubifs_znode *znode,
1870 			 const union ubifs_key *key, int *n);
1871 struct ubifs_znode *ubifs_tnc_postorder_first(struct ubifs_znode *znode);
1872 struct ubifs_znode *ubifs_tnc_postorder_next(const struct ubifs_info *c,
1873 					     struct ubifs_znode *znode);
1874 long ubifs_destroy_tnc_subtree(const struct ubifs_info *c,
1875 			       struct ubifs_znode *zr);
1876 struct ubifs_znode *ubifs_load_znode(struct ubifs_info *c,
1877 				     struct ubifs_zbranch *zbr,
1878 				     struct ubifs_znode *parent, int iip);
1879 int ubifs_tnc_read_node(struct ubifs_info *c, struct ubifs_zbranch *zbr,
1880 			void *node);
1881 
1882 /* tnc_commit.c */
1883 int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot);
1884 int ubifs_tnc_end_commit(struct ubifs_info *c);
1885 
1886 /* shrinker.c */
1887 unsigned long ubifs_shrink_scan(struct shrinker *shrink,
1888 				struct shrink_control *sc);
1889 unsigned long ubifs_shrink_count(struct shrinker *shrink,
1890 				 struct shrink_control *sc);
1891 
1892 /* commit.c */
1893 int ubifs_bg_thread(void *info);
1894 void ubifs_commit_required(struct ubifs_info *c);
1895 void ubifs_request_bg_commit(struct ubifs_info *c);
1896 int ubifs_run_commit(struct ubifs_info *c);
1897 void ubifs_recovery_commit(struct ubifs_info *c);
1898 int ubifs_gc_should_commit(struct ubifs_info *c);
1899 void ubifs_wait_for_commit(struct ubifs_info *c);
1900 
1901 /* master.c */
1902 int ubifs_compare_master_node(struct ubifs_info *c, void *m1, void *m2);
1903 int ubifs_read_master(struct ubifs_info *c);
1904 int ubifs_write_master(struct ubifs_info *c);
1905 
1906 /* sb.c */
1907 int ubifs_read_superblock(struct ubifs_info *c);
1908 int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup);
1909 int ubifs_fixup_free_space(struct ubifs_info *c);
1910 int ubifs_enable_encryption(struct ubifs_info *c);
1911 
1912 /* replay.c */
1913 int ubifs_validate_entry(struct ubifs_info *c,
1914 			 const struct ubifs_dent_node *dent);
1915 int ubifs_replay_journal(struct ubifs_info *c);
1916 
1917 /* gc.c */
1918 int ubifs_garbage_collect(struct ubifs_info *c, int anyway);
1919 int ubifs_gc_start_commit(struct ubifs_info *c);
1920 int ubifs_gc_end_commit(struct ubifs_info *c);
1921 void ubifs_destroy_idx_gc(struct ubifs_info *c);
1922 int ubifs_get_idx_gc_leb(struct ubifs_info *c);
1923 int ubifs_garbage_collect_leb(struct ubifs_info *c, struct ubifs_lprops *lp);
1924 
1925 /* orphan.c */
1926 int ubifs_add_orphan(struct ubifs_info *c, ino_t inum);
1927 void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum);
1928 int ubifs_orphan_start_commit(struct ubifs_info *c);
1929 int ubifs_orphan_end_commit(struct ubifs_info *c);
1930 int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only);
1931 int ubifs_clear_orphans(struct ubifs_info *c);
1932 
1933 /* lpt.c */
1934 int ubifs_calc_lpt_geom(struct ubifs_info *c);
1935 int ubifs_create_dflt_lpt(struct ubifs_info *c, int *main_lebs, int lpt_first,
1936 			  int *lpt_lebs, int *big_lpt, u8 *hash);
1937 int ubifs_lpt_init(struct ubifs_info *c, int rd, int wr);
1938 struct ubifs_lprops *ubifs_lpt_lookup(struct ubifs_info *c, int lnum);
1939 struct ubifs_lprops *ubifs_lpt_lookup_dirty(struct ubifs_info *c, int lnum);
1940 int ubifs_lpt_scan_nolock(struct ubifs_info *c, int start_lnum, int end_lnum,
1941 			  ubifs_lpt_scan_callback scan_cb, void *data);
1942 
1943 /* Shared by lpt.c for lpt_commit.c */
1944 void ubifs_pack_lsave(struct ubifs_info *c, void *buf, int *lsave);
1945 void ubifs_pack_ltab(struct ubifs_info *c, void *buf,
1946 		     struct ubifs_lpt_lprops *ltab);
1947 void ubifs_pack_pnode(struct ubifs_info *c, void *buf,
1948 		      struct ubifs_pnode *pnode);
1949 void ubifs_pack_nnode(struct ubifs_info *c, void *buf,
1950 		      struct ubifs_nnode *nnode);
1951 struct ubifs_pnode *ubifs_get_pnode(struct ubifs_info *c,
1952 				    struct ubifs_nnode *parent, int iip);
1953 struct ubifs_nnode *ubifs_get_nnode(struct ubifs_info *c,
1954 				    struct ubifs_nnode *parent, int iip);
1955 struct ubifs_pnode *ubifs_pnode_lookup(struct ubifs_info *c, int i);
1956 int ubifs_read_nnode(struct ubifs_info *c, struct ubifs_nnode *parent, int iip);
1957 void ubifs_add_lpt_dirt(struct ubifs_info *c, int lnum, int dirty);
1958 void ubifs_add_nnode_dirt(struct ubifs_info *c, struct ubifs_nnode *nnode);
1959 uint32_t ubifs_unpack_bits(const struct ubifs_info *c, uint8_t **addr, int *pos, int nrbits);
1960 struct ubifs_nnode *ubifs_first_nnode(struct ubifs_info *c, int *hght);
1961 /* Needed only in debugging code in lpt_commit.c */
1962 int ubifs_unpack_nnode(const struct ubifs_info *c, void *buf,
1963 		       struct ubifs_nnode *nnode);
1964 int ubifs_lpt_calc_hash(struct ubifs_info *c, u8 *hash);
1965 
1966 /* lpt_commit.c */
1967 int ubifs_lpt_start_commit(struct ubifs_info *c);
1968 int ubifs_lpt_end_commit(struct ubifs_info *c);
1969 int ubifs_lpt_post_commit(struct ubifs_info *c);
1970 void ubifs_lpt_free(struct ubifs_info *c, int wr_only);
1971 
1972 /* lprops.c */
1973 const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
1974 					   const struct ubifs_lprops *lp,
1975 					   int free, int dirty, int flags,
1976 					   int idx_gc_cnt);
1977 void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *lst);
1978 void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
1979 		      int cat);
1980 void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
1981 		       struct ubifs_lprops *new_lprops);
1982 void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops);
1983 int ubifs_categorize_lprops(const struct ubifs_info *c,
1984 			    const struct ubifs_lprops *lprops);
1985 int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
1986 			int flags_set, int flags_clean, int idx_gc_cnt);
1987 int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
1988 			int flags_set, int flags_clean);
1989 int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp);
1990 const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c);
1991 const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c);
1992 const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c);
1993 const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c);
1994 int ubifs_calc_dark(const struct ubifs_info *c, int spc);
1995 
1996 /* file.c */
1997 int ubifs_fsync(struct file *file, loff_t start, loff_t end, int datasync);
1998 int ubifs_setattr(struct dentry *dentry, struct iattr *attr);
1999 #ifdef CONFIG_UBIFS_ATIME_SUPPORT
2000 int ubifs_update_time(struct inode *inode, struct timespec64 *time, int flags);
2001 #endif
2002 
2003 /* dir.c */
2004 struct inode *ubifs_new_inode(struct ubifs_info *c, struct inode *dir,
2005 			      umode_t mode);
2006 int ubifs_getattr(const struct path *path, struct kstat *stat,
2007 		  u32 request_mask, unsigned int flags);
2008 int ubifs_check_dir_empty(struct inode *dir);
2009 
2010 /* xattr.c */
2011 extern const struct xattr_handler *ubifs_xattr_handlers[];
2012 ssize_t ubifs_listxattr(struct dentry *dentry, char *buffer, size_t size);
2013 int ubifs_xattr_set(struct inode *host, const char *name, const void *value,
2014 		    size_t size, int flags, bool check_lock);
2015 ssize_t ubifs_xattr_get(struct inode *host, const char *name, void *buf,
2016 			size_t size);
2017 
2018 #ifdef CONFIG_UBIFS_FS_XATTR
2019 void ubifs_evict_xattr_inode(struct ubifs_info *c, ino_t xattr_inum);
2020 #else
2021 static inline void ubifs_evict_xattr_inode(struct ubifs_info *c,
2022 					   ino_t xattr_inum) { }
2023 #endif
2024 
2025 #ifdef CONFIG_UBIFS_FS_SECURITY
2026 extern int ubifs_init_security(struct inode *dentry, struct inode *inode,
2027 			const struct qstr *qstr);
2028 #else
2029 static inline int ubifs_init_security(struct inode *dentry,
2030 			struct inode *inode, const struct qstr *qstr)
2031 {
2032 	return 0;
2033 }
2034 #endif
2035 
2036 
2037 /* super.c */
2038 struct inode *ubifs_iget(struct super_block *sb, unsigned long inum);
2039 
2040 /* recovery.c */
2041 int ubifs_recover_master_node(struct ubifs_info *c);
2042 int ubifs_write_rcvrd_mst_node(struct ubifs_info *c);
2043 struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
2044 					 int offs, void *sbuf, int jhead);
2045 struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
2046 					     int offs, void *sbuf);
2047 int ubifs_recover_inl_heads(struct ubifs_info *c, void *sbuf);
2048 int ubifs_clean_lebs(struct ubifs_info *c, void *sbuf);
2049 int ubifs_rcvry_gc_commit(struct ubifs_info *c);
2050 int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
2051 			     int deletion, loff_t new_size);
2052 int ubifs_recover_size(struct ubifs_info *c, bool in_place);
2053 void ubifs_destroy_size_tree(struct ubifs_info *c);
2054 
2055 /* ioctl.c */
2056 long ubifs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
2057 void ubifs_set_inode_flags(struct inode *inode);
2058 #ifdef CONFIG_COMPAT
2059 long ubifs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
2060 #endif
2061 
2062 /* compressor.c */
2063 int __init ubifs_compressors_init(void);
2064 void ubifs_compressors_exit(void);
2065 void ubifs_compress(const struct ubifs_info *c, const void *in_buf, int in_len,
2066 		    void *out_buf, int *out_len, int *compr_type);
2067 int ubifs_decompress(const struct ubifs_info *c, const void *buf, int len,
2068 		     void *out, int *out_len, int compr_type);
2069 
2070 #include "debug.h"
2071 #include "misc.h"
2072 #include "key.h"
2073 
2074 #ifndef CONFIG_FS_ENCRYPTION
2075 static inline int ubifs_encrypt(const struct inode *inode,
2076 				struct ubifs_data_node *dn,
2077 				unsigned int in_len, unsigned int *out_len,
2078 				int block)
2079 {
2080 	struct ubifs_info *c = inode->i_sb->s_fs_info;
2081 	ubifs_assert(c, 0);
2082 	return -EOPNOTSUPP;
2083 }
2084 static inline int ubifs_decrypt(const struct inode *inode,
2085 				struct ubifs_data_node *dn,
2086 				unsigned int *out_len, int block)
2087 {
2088 	struct ubifs_info *c = inode->i_sb->s_fs_info;
2089 	ubifs_assert(c, 0);
2090 	return -EOPNOTSUPP;
2091 }
2092 #else
2093 /* crypto.c */
2094 int ubifs_encrypt(const struct inode *inode, struct ubifs_data_node *dn,
2095 		  unsigned int in_len, unsigned int *out_len, int block);
2096 int ubifs_decrypt(const struct inode *inode, struct ubifs_data_node *dn,
2097 		  unsigned int *out_len, int block);
2098 #endif
2099 
2100 extern const struct fscrypt_operations ubifs_crypt_operations;
2101 
2102 static inline bool ubifs_crypt_is_encrypted(const struct inode *inode)
2103 {
2104 	const struct ubifs_inode *ui = ubifs_inode(inode);
2105 
2106 	return ui->flags & UBIFS_CRYPT_FL;
2107 }
2108 
2109 /* Normal UBIFS messages */
2110 __printf(2, 3)
2111 void ubifs_msg(const struct ubifs_info *c, const char *fmt, ...);
2112 __printf(2, 3)
2113 void ubifs_err(const struct ubifs_info *c, const char *fmt, ...);
2114 __printf(2, 3)
2115 void ubifs_warn(const struct ubifs_info *c, const char *fmt, ...);
2116 /*
2117  * A conditional variant of 'ubifs_err()' which doesn't output anything
2118  * if probing (ie. SB_SILENT set).
2119  */
2120 #define ubifs_errc(c, fmt, ...)						\
2121 do {									\
2122 	if (!(c)->probing)						\
2123 		ubifs_err(c, fmt, ##__VA_ARGS__);			\
2124 } while (0)
2125 
2126 #endif /* !__UBIFS_H__ */
2127