Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ubifs-media.h
Go to the documentation of this file.
1 /*
2  * This file is part of UBIFS.
3  *
4  * Copyright (C) 2006-2008 Nokia Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * Authors: Artem Bityutskiy (Битюцкий Артём)
20  * Adrian Hunter
21  */
22 
23 /*
24  * This file describes UBIFS on-flash format and contains definitions of all the
25  * relevant data structures and constants.
26  *
27  * All UBIFS on-flash objects are stored in the form of nodes. All nodes start
28  * with the UBIFS node magic number and have the same common header. Nodes
29  * always sit at 8-byte aligned positions on the media and node header sizes are
30  * also 8-byte aligned (except for the indexing node and the padding node).
31  */
32 
33 #ifndef __UBIFS_MEDIA_H__
34 #define __UBIFS_MEDIA_H__
35 
36 /* UBIFS node magic number (must not have the padding byte first or last) */
37 #define UBIFS_NODE_MAGIC 0x06101831
38 
39 /*
40  * UBIFS on-flash format version. This version is increased when the on-flash
41  * format is changing. If this happens, UBIFS is will support older versions as
42  * well. But older UBIFS code will not support newer formats. Format changes
43  * will be rare and only when absolutely necessary, e.g. to fix a bug or to add
44  * a new feature.
45  *
46  * UBIFS went into mainline kernel with format version 4. The older formats
47  * were development formats.
48  */
49 #define UBIFS_FORMAT_VERSION 4
50 
51 /*
52  * Read-only compatibility version. If the UBIFS format is changed, older UBIFS
53  * implementations will not be able to mount newer formats in read-write mode.
54  * However, depending on the change, it may be possible to mount newer formats
55  * in R/O mode. This is indicated by the R/O compatibility version which is
56  * stored in the super-block.
57  *
58  * This is needed to support boot-loaders which only need R/O mounting. With
59  * this flag it is possible to do UBIFS format changes without a need to update
60  * boot-loaders.
61  */
62 #define UBIFS_RO_COMPAT_VERSION 0
63 
64 /* Minimum logical eraseblock size in bytes */
65 #define UBIFS_MIN_LEB_SZ (15*1024)
66 
67 /* Initial CRC32 value used when calculating CRC checksums */
68 #define UBIFS_CRC32_INIT 0xFFFFFFFFU
69 
70 /*
71  * UBIFS does not try to compress data if its length is less than the below
72  * constant.
73  */
74 #define UBIFS_MIN_COMPR_LEN 128
75 
76 /*
77  * If compressed data length is less than %UBIFS_MIN_COMPRESS_DIFF bytes
78  * shorter than uncompressed data length, UBIFS prefers to leave this data
79  * node uncompress, because it'll be read faster.
80  */
81 #define UBIFS_MIN_COMPRESS_DIFF 64
82 
83 /* Root inode number */
84 #define UBIFS_ROOT_INO 1
85 
86 /* Lowest inode number used for regular inodes (not UBIFS-only internal ones) */
87 #define UBIFS_FIRST_INO 64
88 
89 /*
90  * Maximum file name and extended attribute length (must be a multiple of 8,
91  * minus 1).
92  */
93 #define UBIFS_MAX_NLEN 255
94 
95 /* Maximum number of data journal heads */
96 #define UBIFS_MAX_JHEADS 1
97 
98 /*
99  * Size of UBIFS data block. Note, UBIFS is not a block oriented file-system,
100  * which means that it does not treat the underlying media as consisting of
101  * blocks like in case of hard drives. Do not be confused. UBIFS block is just
102  * the maximum amount of data which one data node can have or which can be
103  * attached to an inode node.
104  */
105 #define UBIFS_BLOCK_SIZE 4096
106 #define UBIFS_BLOCK_SHIFT 12
107 
108 /* UBIFS padding byte pattern (must not be first or last byte of node magic) */
109 #define UBIFS_PADDING_BYTE 0xCE
110 
111 /* Maximum possible key length */
112 #define UBIFS_MAX_KEY_LEN 16
113 
114 /* Key length ("simple" format) */
115 #define UBIFS_SK_LEN 8
116 
117 /* Minimum index tree fanout */
118 #define UBIFS_MIN_FANOUT 3
119 
120 /* Maximum number of levels in UBIFS indexing B-tree */
121 #define UBIFS_MAX_LEVELS 512
122 
123 /* Maximum amount of data attached to an inode in bytes */
124 #define UBIFS_MAX_INO_DATA UBIFS_BLOCK_SIZE
125 
126 /* LEB Properties Tree fanout (must be power of 2) and fanout shift */
127 #define UBIFS_LPT_FANOUT 4
128 #define UBIFS_LPT_FANOUT_SHIFT 2
129 
130 /* LEB Properties Tree bit field sizes */
131 #define UBIFS_LPT_CRC_BITS 16
132 #define UBIFS_LPT_CRC_BYTES 2
133 #define UBIFS_LPT_TYPE_BITS 4
134 
135 /* The key is always at the same position in all keyed nodes */
136 #define UBIFS_KEY_OFFSET offsetof(struct ubifs_ino_node, key)
137 
138 /* Garbage collector journal head number */
139 #define UBIFS_GC_HEAD 0
140 /* Base journal head number */
141 #define UBIFS_BASE_HEAD 1
142 /* Data journal head number */
143 #define UBIFS_DATA_HEAD 2
144 
145 /*
146  * LEB Properties Tree node types.
147  *
148  * UBIFS_LPT_PNODE: LPT leaf node (contains LEB properties)
149  * UBIFS_LPT_NNODE: LPT internal node
150  * UBIFS_LPT_LTAB: LPT's own lprops table
151  * UBIFS_LPT_LSAVE: LPT's save table (big model only)
152  * UBIFS_LPT_NODE_CNT: count of LPT node types
153  * UBIFS_LPT_NOT_A_NODE: all ones (15 for 4 bits) is never a valid node type
154  */
155 enum {
162 };
163 
164 /*
165  * UBIFS inode types.
166  *
167  * UBIFS_ITYPE_REG: regular file
168  * UBIFS_ITYPE_DIR: directory
169  * UBIFS_ITYPE_LNK: soft link
170  * UBIFS_ITYPE_BLK: block device node
171  * UBIFS_ITYPE_CHR: character device node
172  * UBIFS_ITYPE_FIFO: fifo
173  * UBIFS_ITYPE_SOCK: socket
174  * UBIFS_ITYPES_CNT: count of supported file types
175  */
176 enum {
185 };
186 
187 /*
188  * Supported key hash functions.
189  *
190  * UBIFS_KEY_HASH_R5: R5 hash
191  * UBIFS_KEY_HASH_TEST: test hash which just returns first 4 bytes of the name
192  */
193 enum {
196 };
197 
198 /*
199  * Supported key formats.
200  *
201  * UBIFS_SIMPLE_KEY_FMT: simple key format
202  */
203 enum {
205 };
206 
207 /*
208  * The simple key format uses 29 bits for storing UBIFS block number and hash
209  * value.
210  */
211 #define UBIFS_S_KEY_BLOCK_BITS 29
212 #define UBIFS_S_KEY_BLOCK_MASK 0x1FFFFFFF
213 #define UBIFS_S_KEY_HASH_BITS UBIFS_S_KEY_BLOCK_BITS
214 #define UBIFS_S_KEY_HASH_MASK UBIFS_S_KEY_BLOCK_MASK
215 
216 /*
217  * Key types.
218  *
219  * UBIFS_INO_KEY: inode node key
220  * UBIFS_DATA_KEY: data node key
221  * UBIFS_DENT_KEY: directory entry node key
222  * UBIFS_XENT_KEY: extended attribute entry key
223  * UBIFS_KEY_TYPES_CNT: number of supported key types
224  */
225 enum {
231 };
232 
233 /* Count of LEBs reserved for the superblock area */
234 #define UBIFS_SB_LEBS 1
235 /* Count of LEBs reserved for the master area */
236 #define UBIFS_MST_LEBS 2
237 
238 /* First LEB of the superblock area */
239 #define UBIFS_SB_LNUM 0
240 /* First LEB of the master area */
241 #define UBIFS_MST_LNUM (UBIFS_SB_LNUM + UBIFS_SB_LEBS)
242 /* First LEB of the log area */
243 #define UBIFS_LOG_LNUM (UBIFS_MST_LNUM + UBIFS_MST_LEBS)
244 
245 /*
246  * The below constants define the absolute minimum values for various UBIFS
247  * media areas. Many of them actually depend of flash geometry and the FS
248  * configuration (number of journal heads, orphan LEBs, etc). This means that
249  * the smallest volume size which can be used for UBIFS cannot be pre-defined
250  * by these constants. The file-system that meets the below limitation will not
251  * necessarily mount. UBIFS does run-time calculations and validates the FS
252  * size.
253  */
254 
255 /* Minimum number of logical eraseblocks in the log */
256 #define UBIFS_MIN_LOG_LEBS 2
257 /* Minimum number of bud logical eraseblocks (one for each head) */
258 #define UBIFS_MIN_BUD_LEBS 3
259 /* Minimum number of journal logical eraseblocks */
260 #define UBIFS_MIN_JNL_LEBS (UBIFS_MIN_LOG_LEBS + UBIFS_MIN_BUD_LEBS)
261 /* Minimum number of LPT area logical eraseblocks */
262 #define UBIFS_MIN_LPT_LEBS 2
263 /* Minimum number of orphan area logical eraseblocks */
264 #define UBIFS_MIN_ORPH_LEBS 1
265 /*
266  * Minimum number of main area logical eraseblocks (buds, 3 for the index, 1
267  * for GC, 1 for deletions, and at least 1 for committed data).
268  */
269 #define UBIFS_MIN_MAIN_LEBS (UBIFS_MIN_BUD_LEBS + 6)
270 
271 /* Minimum number of logical eraseblocks */
272 #define UBIFS_MIN_LEB_CNT (UBIFS_SB_LEBS + UBIFS_MST_LEBS + \
273  UBIFS_MIN_LOG_LEBS + UBIFS_MIN_LPT_LEBS + \
274  UBIFS_MIN_ORPH_LEBS + UBIFS_MIN_MAIN_LEBS)
275 
276 /* Node sizes (N.B. these are guaranteed to be multiples of 8) */
277 #define UBIFS_CH_SZ sizeof(struct ubifs_ch)
278 #define UBIFS_INO_NODE_SZ sizeof(struct ubifs_ino_node)
279 #define UBIFS_DATA_NODE_SZ sizeof(struct ubifs_data_node)
280 #define UBIFS_DENT_NODE_SZ sizeof(struct ubifs_dent_node)
281 #define UBIFS_TRUN_NODE_SZ sizeof(struct ubifs_trun_node)
282 #define UBIFS_PAD_NODE_SZ sizeof(struct ubifs_pad_node)
283 #define UBIFS_SB_NODE_SZ sizeof(struct ubifs_sb_node)
284 #define UBIFS_MST_NODE_SZ sizeof(struct ubifs_mst_node)
285 #define UBIFS_REF_NODE_SZ sizeof(struct ubifs_ref_node)
286 #define UBIFS_IDX_NODE_SZ sizeof(struct ubifs_idx_node)
287 #define UBIFS_CS_NODE_SZ sizeof(struct ubifs_cs_node)
288 #define UBIFS_ORPH_NODE_SZ sizeof(struct ubifs_orph_node)
289 /* Extended attribute entry nodes are identical to directory entry nodes */
290 #define UBIFS_XENT_NODE_SZ UBIFS_DENT_NODE_SZ
291 /* Only this does not have to be multiple of 8 bytes */
292 #define UBIFS_BRANCH_SZ sizeof(struct ubifs_branch)
293 
294 /* Maximum node sizes (N.B. these are guaranteed to be multiples of 8) */
295 #define UBIFS_MAX_DATA_NODE_SZ (UBIFS_DATA_NODE_SZ + UBIFS_BLOCK_SIZE)
296 #define UBIFS_MAX_INO_NODE_SZ (UBIFS_INO_NODE_SZ + UBIFS_MAX_INO_DATA)
297 #define UBIFS_MAX_DENT_NODE_SZ (UBIFS_DENT_NODE_SZ + UBIFS_MAX_NLEN + 1)
298 #define UBIFS_MAX_XENT_NODE_SZ UBIFS_MAX_DENT_NODE_SZ
299 
300 /* The largest UBIFS node */
301 #define UBIFS_MAX_NODE_SZ UBIFS_MAX_INO_NODE_SZ
302 
303 /*
304  * On-flash inode flags.
305  *
306  * UBIFS_COMPR_FL: use compression for this inode
307  * UBIFS_SYNC_FL: I/O on this inode has to be synchronous
308  * UBIFS_IMMUTABLE_FL: inode is immutable
309  * UBIFS_APPEND_FL: writes to the inode may only append data
310  * UBIFS_DIRSYNC_FL: I/O on this directory inode has to be synchronous
311  * UBIFS_XATTR_FL: this inode is the inode for an extended attribute value
312  *
313  * Note, these are on-flash flags which correspond to ioctl flags
314  * (@FS_COMPR_FL, etc). They have the same values now, but generally, do not
315  * have to be the same.
316  */
317 enum {
324 };
325 
326 /* Inode flag bits used by UBIFS */
327 #define UBIFS_FL_MASK 0x0000001F
328 
329 /*
330  * UBIFS compression algorithms.
331  *
332  * UBIFS_COMPR_NONE: no compression
333  * UBIFS_COMPR_LZO: LZO compression
334  * UBIFS_COMPR_ZLIB: ZLIB compression
335  * UBIFS_COMPR_TYPES_CNT: count of supported compression types
336  */
337 enum {
342 };
343 
344 /*
345  * UBIFS node types.
346  *
347  * UBIFS_INO_NODE: inode node
348  * UBIFS_DATA_NODE: data node
349  * UBIFS_DENT_NODE: directory entry node
350  * UBIFS_XENT_NODE: extended attribute node
351  * UBIFS_TRUN_NODE: truncation node
352  * UBIFS_PAD_NODE: padding node
353  * UBIFS_SB_NODE: superblock node
354  * UBIFS_MST_NODE: master node
355  * UBIFS_REF_NODE: LEB reference node
356  * UBIFS_IDX_NODE: index node
357  * UBIFS_CS_NODE: commit start node
358  * UBIFS_ORPH_NODE: orphan node
359  * UBIFS_NODE_TYPES_CNT: count of supported node types
360  *
361  * Note, we index arrays by these numbers, so keep them low and contiguous.
362  * Node type constants for inodes, direntries and so on have to be the same as
363  * corresponding key type constants.
364  */
365 enum {
379 };
380 
381 /*
382  * Master node flags.
383  *
384  * UBIFS_MST_DIRTY: rebooted uncleanly - master node is dirty
385  * UBIFS_MST_NO_ORPHS: no orphan inodes present
386  * UBIFS_MST_RCVRY: written by recovery
387  */
388 enum {
392 };
393 
394 /*
395  * Node group type (used by recovery to recover whole group or none).
396  *
397  * UBIFS_NO_NODE_GROUP: this node is not part of a group
398  * UBIFS_IN_NODE_GROUP: this node is a part of a group
399  * UBIFS_LAST_OF_NODE_GROUP: this node is the last in a group
400  */
401 enum {
405 };
406 
407 /*
408  * Superblock flags.
409  *
410  * UBIFS_FLG_BIGLPT: if "big" LPT model is used if set
411  * UBIFS_FLG_SPACE_FIXUP: first-mount "fixup" of free space within LEBs needed
412  */
413 enum {
416 };
417 
431 struct ubifs_ch {
439 } __packed;
440 
451  __le32 new;
453 } __packed;
454 
491  struct ubifs_ch ch;
509  __u8 padding1[4]; /* Watch 'zero_ino_node_unused()' if changing! */
512  __u8 padding2[26]; /* Watch 'zero_ino_node_unused()' if changing! */
514 } __packed;
515 
531  struct ubifs_ch ch;
537  __u8 padding2[4]; /* Watch 'zero_dent_node_unused()' if changing! */
539 } __packed;
540 
554  struct ubifs_ch ch;
558  __u8 padding[2]; /* Watch 'zero_data_node_unused()' if changing! */
560 } __packed;
561 
575  struct ubifs_ch ch;
577  __u8 padding[12]; /* Watch 'zero_trun_node_unused()' if changing! */
580 } __packed;
581 
589  struct ubifs_ch ch;
591 } __packed;
592 
623  struct ubifs_ch ch;
646  __u8 uuid[16];
648  __u8 padding2[3968];
649 } __packed;
650 
686  struct ubifs_ch ch;
715  __u8 padding[344];
716 } __packed;
717 
727  struct ubifs_ch ch;
732 } __packed;
733 
741 struct ubifs_branch {
746 } __packed;
747 
756  struct ubifs_ch ch;
760 } __packed;
761 
768  struct ubifs_ch ch;
770 } __packed;
771 
779  struct ubifs_ch ch;
782 } __packed;
783 
784 #endif /* __UBIFS_MEDIA_H__ */