SPDX: Convert all of our single license tags to Linux Kernel style
[platform/kernel/u-boot.git] / fs / reiserfs / reiserfs_private.h
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  *  Copyright 2000-2002 by Hans Reiser, licensing governed by reiserfs/README
4  *
5  *  GRUB  --  GRand Unified Bootloader
6  *  Copyright (C) 2000, 2001  Free Software Foundation, Inc.
7  *
8  *  (C) Copyright 2003 - 2004
9  *  Sysgo AG, <www.elinos.com>, Pavel Bartusek <pba@sysgo.com>
10  *
11  */
12
13 /* An implementation for the ReiserFS filesystem ported from GRUB.
14  * Some parts of this code (mainly the structures and defines) are
15  * from the original reiser fs code, as found in the linux kernel.
16  */
17
18 #ifndef __BYTE_ORDER
19 #if defined(__LITTLE_ENDIAN) && !defined(__BIG_ENDIAN)
20 #define __BYTE_ORDER __LITTLE_ENDIAN
21 #elif defined(__BIG_ENDIAN) && !defined(__LITTLE_ENDIAN)
22 #define __BYTE_ORDER __BIG_ENDIAN
23 #else
24 #error "unable to define __BYTE_ORDER"
25 #endif
26 #endif /* not __BYTE_ORDER */
27
28 #define FSYS_BUFLEN  0x8000
29 #define FSYS_BUF     fsys_buf
30
31 /* This is the new super block of a journaling reiserfs system */
32 struct reiserfs_super_block
33 {
34   __u32 s_block_count;                  /* blocks count         */
35   __u32 s_free_blocks;                  /* free blocks count    */
36   __u32 s_root_block;                   /* root block number    */
37   __u32 s_journal_block;                /* journal block number    */
38   __u32 s_journal_dev;                  /* journal device number  */
39   __u32 s_journal_size;                 /* size of the journal on FS creation.  used to make sure they don't overflow it */
40   __u32 s_journal_trans_max;            /* max number of blocks in a transaction.  */
41   __u32 s_journal_magic;                /* random value made on fs creation */
42   __u32 s_journal_max_batch;            /* max number of blocks to batch into a trans */
43   __u32 s_journal_max_commit_age;       /* in seconds, how old can an async commit be */
44   __u32 s_journal_max_trans_age;        /* in seconds, how old can a transaction be */
45   __u16 s_blocksize;                    /* block size           */
46   __u16 s_oid_maxsize;                  /* max size of object id array  */
47   __u16 s_oid_cursize;                  /* current size of object id array */
48   __u16 s_state;                        /* valid or error       */
49   char s_magic[16];                     /* reiserfs magic string indicates that file system is reiserfs */
50   __u16 s_tree_height;                  /* height of disk tree */
51   __u16 s_bmap_nr;                      /* amount of bitmap blocks needed to address each block of file system */
52   __u16 s_version;
53   char s_unused[128];                   /* zero filled by mkreiserfs */
54 };
55
56
57 #define sb_root_block(sbp)            (__le32_to_cpu((sbp)->s_root_block))
58 #define sb_journal_block(sbp)         (__le32_to_cpu((sbp)->s_journal_block))
59 #define set_sb_journal_block(sbp,v)   ((sbp)->s_journal_block = __cpu_to_le32(v))
60 #define sb_journal_size(sbp)          (__le32_to_cpu((sbp)->s_journal_size))
61 #define sb_blocksize(sbp)             (__le16_to_cpu((sbp)->s_blocksize))
62 #define set_sb_blocksize(sbp,v)       ((sbp)->s_blocksize = __cpu_to_le16(v))
63 #define sb_version(sbp)               (__le16_to_cpu((sbp)->s_version))
64 #define set_sb_version(sbp,v)         ((sbp)->s_version = __cpu_to_le16(v))
65
66
67 #define REISERFS_MAX_SUPPORTED_VERSION 2
68 #define REISERFS_SUPER_MAGIC_STRING "ReIsErFs"
69 #define REISER2FS_SUPER_MAGIC_STRING "ReIsEr2Fs"
70 #define REISER3FS_SUPER_MAGIC_STRING "ReIsEr3Fs"
71
72 #define MAX_HEIGHT 7
73
74 /* must be correct to keep the desc and commit structs at 4k */
75 #define JOURNAL_TRANS_HALF 1018
76
77 /* first block written in a commit.  */
78 struct reiserfs_journal_desc {
79   __u32 j_trans_id;                     /* id of commit */
80   __u32 j_len;                          /* length of commit. len +1 is the commit block */
81   __u32 j_mount_id;                     /* mount id of this trans*/
82   __u32 j_realblock[JOURNAL_TRANS_HALF]; /* real locations for the first blocks */
83   char j_magic[12];
84 };
85
86 /* last block written in a commit */
87 struct reiserfs_journal_commit {
88   __u32 j_trans_id;                     /* must match j_trans_id from the desc block */
89   __u32 j_len;                  /* ditto */
90   __u32 j_realblock[JOURNAL_TRANS_HALF]; /* real locations for the last blocks */
91   char j_digest[16];                    /* md5 sum of all the blocks involved, including desc and commit. not used, kill it */
92 };
93
94 /* this header block gets written whenever a transaction is considered
95    fully flushed, and is more recent than the last fully flushed
96    transaction.
97    fully flushed means all the log blocks and all the real blocks are
98    on disk, and this transaction does not need to be replayed.
99 */
100 struct reiserfs_journal_header {
101   /* id of last fully flushed transaction */
102   __u32 j_last_flush_trans_id;
103   /* offset in the log of where to start replay after a crash */
104   __u32 j_first_unflushed_offset;
105   /* mount id to detect very old transactions */
106   __u32 j_mount_id;
107 };
108
109 /* magic string to find desc blocks in the journal */
110 #define JOURNAL_DESC_MAGIC "ReIsErLB"
111
112
113 /*
114  * directories use this key as well as old files
115  */
116 struct offset_v1
117 {
118   /*
119    * for regular files this is the offset to the first byte of the
120    * body, contained in the object-item, as measured from the start of
121    * the entire body of the object.
122    *
123    * for directory entries, k_offset consists of hash derived from
124    * hashing the name and using few bits (23 or more) of the resulting
125    * hash, and generation number that allows distinguishing names with
126    * hash collisions. If number of collisions overflows generation
127    * number, we return EEXIST.  High order bit is 0 always
128    */
129   __u32 k_offset;
130   __u32 k_uniqueness;
131 };
132
133 struct offset_v2 {
134   /*
135    * for regular files this is the offset to the first byte of the
136    * body, contained in the object-item, as measured from the start of
137    * the entire body of the object.
138    *
139    * for directory entries, k_offset consists of hash derived from
140    * hashing the name and using few bits (23 or more) of the resulting
141    * hash, and generation number that allows distinguishing names with
142    * hash collisions. If number of collisions overflows generation
143    * number, we return EEXIST.  High order bit is 0 always
144    */
145
146 #if defined(__LITTLE_ENDIAN_BITFIELD)
147             /* little endian version */
148             __u64 k_offset:60;
149             __u64 k_type: 4;
150 #elif defined(__BIG_ENDIAN_BITFIELD)
151             /* big endian version */
152             __u64 k_type: 4;
153             __u64 k_offset:60;
154 #else
155 #error "__LITTLE_ENDIAN_BITFIELD or __BIG_ENDIAN_BITFIELD must be defined"
156 #endif
157 } __attribute__ ((__packed__));
158
159 #define TYPE_MAXTYPE 3
160 #define TYPE_ANY 15
161
162 #if (__BYTE_ORDER == __BIG_ENDIAN)
163 typedef union {
164     struct offset_v2 offset_v2;
165     __u64 linear;
166 } __attribute__ ((__packed__)) offset_v2_esafe_overlay;
167
168 static inline __u16 offset_v2_k_type( const struct offset_v2 *v2 )
169 {
170     offset_v2_esafe_overlay tmp = *(const offset_v2_esafe_overlay *)v2;
171     tmp.linear = __le64_to_cpu( tmp.linear );
172     return (tmp.offset_v2.k_type <= TYPE_MAXTYPE)?tmp.offset_v2.k_type:TYPE_ANY;
173 }
174
175 static inline loff_t offset_v2_k_offset( const struct offset_v2 *v2 )
176 {
177     offset_v2_esafe_overlay tmp = *(const offset_v2_esafe_overlay *)v2;
178     tmp.linear = __le64_to_cpu( tmp.linear );
179     return tmp.offset_v2.k_offset;
180 }
181 #elif (__BYTE_ORDER == __LITTLE_ENDIAN)
182 # define offset_v2_k_type(v2)           ((v2)->k_type)
183 # define offset_v2_k_offset(v2)         ((v2)->k_offset)
184 #else
185 #error "__BYTE_ORDER must be __LITTLE_ENDIAN or __BIG_ENDIAN"
186 #endif
187
188 struct key
189 {
190   /* packing locality: by default parent directory object id */
191   __u32 k_dir_id;
192   /* object identifier */
193   __u32 k_objectid;
194   /* the offset and node type (old and new form) */
195   union
196   {
197     struct offset_v1 v1;
198     struct offset_v2 v2;
199   }
200   u;
201 };
202
203 #define KEY_SIZE (sizeof (struct key))
204
205 /* Header of a disk block.  More precisely, header of a formatted leaf
206    or internal node, and not the header of an unformatted node. */
207 struct block_head
208 {
209   __u16 blk_level;        /* Level of a block in the tree. */
210   __u16 blk_nr_item;      /* Number of keys/items in a block. */
211   __u16 blk_free_space;   /* Block free space in bytes. */
212   struct key  blk_right_delim_key; /* Right delimiting key for this block (supported for leaf level nodes
213                                       only) */
214 };
215 #define BLKH_SIZE (sizeof (struct block_head))
216 #define DISK_LEAF_NODE_LEVEL  1 /* Leaf node level.                       */
217
218 struct item_head
219 {
220         /* Everything in the tree is found by searching for it based on
221          * its key.*/
222         struct key ih_key;
223         union {
224                 /* The free space in the last unformatted node of an
225                    indirect item if this is an indirect item.  This
226                    equals 0xFFFF iff this is a direct item or stat data
227                    item. Note that the key, not this field, is used to
228                    determine the item type, and thus which field this
229                    union contains. */
230                 __u16 ih_free_space;
231                 /* Iff this is a directory item, this field equals the
232                    number of directory entries in the directory item. */
233                 __u16 ih_entry_count;
234         } __attribute__ ((__packed__)) u;
235         __u16 ih_item_len;           /* total size of the item body */
236         __u16 ih_item_location;      /* an offset to the item body
237                                       * within the block */
238         __u16 ih_version;            /* 0 for all old items, 2 for new
239                                         ones. Highest bit is set by fsck
240                                         temporary, cleaned after all
241                                         done */
242 } __attribute__ ((__packed__));
243
244 /* size of item header     */
245 #define IH_SIZE (sizeof (struct item_head))
246
247 #define ITEM_VERSION_1 0
248 #define ITEM_VERSION_2 1
249
250 #define ih_version(ih)    (__le16_to_cpu((ih)->ih_version))
251
252 #define IH_KEY_OFFSET(ih) (ih_version(ih) == ITEM_VERSION_1 \
253                            ? __le32_to_cpu((ih)->ih_key.u.v1.k_offset) \
254                            : offset_v2_k_offset(&((ih)->ih_key.u.v2)))
255
256 #define IH_KEY_ISTYPE(ih, type) (ih_version(ih) == ITEM_VERSION_1 \
257                                  ? __le32_to_cpu((ih)->ih_key.u.v1.k_uniqueness) == V1_##type \
258                                  : offset_v2_k_type(&((ih)->ih_key.u.v2)) == V2_##type)
259
260 /***************************************************************************/
261 /*                      DISK CHILD                                         */
262 /***************************************************************************/
263 /* Disk child pointer: The pointer from an internal node of the tree
264    to a node that is on disk. */
265 struct disk_child {
266   __u32       dc_block_number;              /* Disk child's block number. */
267   __u16       dc_size;                      /* Disk child's used space.   */
268   __u16       dc_reserved;
269 };
270
271 #define DC_SIZE (sizeof(struct disk_child))
272 #define dc_block_number(dc_p)   (__le32_to_cpu((dc_p)->dc_block_number))
273
274
275 /*
276  * old stat data is 32 bytes long. We are going to distinguish new one by
277  * different size
278  */
279 struct stat_data_v1
280 {
281     __u16 sd_mode;      /* file type, permissions */
282     __u16 sd_nlink;     /* number of hard links */
283     __u16 sd_uid;               /* owner */
284     __u16 sd_gid;               /* group */
285     __u32 sd_size;      /* file size */
286     __u32 sd_atime;     /* time of last access */
287     __u32 sd_mtime;     /* time file was last modified  */
288     __u32 sd_ctime;     /* time inode (stat data) was last changed (except changes to sd_atime and sd_mtime) */
289     union {
290         __u32 sd_rdev;
291         __u32 sd_blocks;        /* number of blocks file uses */
292     } __attribute__ ((__packed__)) u;
293     __u32 sd_first_direct_byte; /* first byte of file which is stored
294                                    in a direct item: except that if it
295                                    equals 1 it is a symlink and if it
296                                    equals ~(__u32)0 there is no
297                                    direct item.  The existence of this
298                                    field really grates on me. Let's
299                                    replace it with a macro based on
300                                    sd_size and our tail suppression
301                                    policy.  Someday.  -Hans */
302 } __attribute__ ((__packed__));
303
304 #define stat_data_v1(ih)        (ih_version(ih) == ITEM_VERSION_1)
305 #define sd_v1_mode(sdp)         ((sdp)->sd_mode)
306 #define sd_v1_nlink(sdp)        (__le16_to_cpu((sdp)->sd_nlink))
307 #define sd_v1_uid(sdp)          (__le16_to_cpu((sdp)->sd_uid))
308 #define sd_v1_gid(sdp)          (__le16_to_cpu((sdp)->sd_gid))
309 #define sd_v1_size(sdp)         (__le32_to_cpu((sdp)->sd_size))
310 #define sd_v1_mtime(sdp)        (__le32_to_cpu((sdp)->sd_mtime))
311
312 /* Stat Data on disk (reiserfs version of UFS disk inode minus the
313    address blocks) */
314 struct stat_data {
315     __u16 sd_mode;      /* file type, permissions */
316     __u16 sd_attrs;     /* persistent inode flags */
317     __u32 sd_nlink;     /* number of hard links */
318     __u64 sd_size;      /* file size */
319     __u32 sd_uid;               /* owner */
320     __u32 sd_gid;               /* group */
321     __u32 sd_atime;     /* time of last access */
322     __u32 sd_mtime;     /* time file was last modified  */
323     __u32 sd_ctime;     /* time inode (stat data) was last changed (except changes to sd_atime and sd_mtime) */
324     __u32 sd_blocks;
325     union {
326         __u32 sd_rdev;
327         __u32 sd_generation;
328       /*__u32 sd_first_direct_byte; */
329       /* first byte of file which is stored in a
330                                        direct item: except that if it equals 1
331                                        it is a symlink and if it equals
332                                        ~(__u32)0 there is no direct item.  The
333                                        existence of this field really grates
334                                        on me. Let's replace it with a macro
335                                        based on sd_size and our tail
336                                        suppression policy? */
337   } __attribute__ ((__packed__)) u;
338 } __attribute__ ((__packed__));
339
340 #define stat_data_v2(ih)        (ih_version(ih) == ITEM_VERSION_2)
341 #define sd_v2_mode(sdp)         (__le16_to_cpu((sdp)->sd_mode))
342 #define sd_v2_nlink(sdp)        (__le32_to_cpu((sdp)->sd_nlink))
343 #define sd_v2_size(sdp)         (__le64_to_cpu((sdp)->sd_size))
344 #define sd_v2_uid(sdp)          (__le32_to_cpu((sdp)->sd_uid))
345 #define sd_v2_gid(sdp)          (__le32_to_cpu((sdp)->sd_gid))
346 #define sd_v2_mtime(sdp)        (__le32_to_cpu((sdp)->sd_mtime))
347
348 #define sd_mode(sdp)         (__le16_to_cpu((sdp)->sd_mode))
349 #define sd_size(sdp)         (__le32_to_cpu((sdp)->sd_size))
350 #define sd_size_hi(sdp)      (__le32_to_cpu((sdp)->sd_size_hi))
351
352 struct reiserfs_de_head
353 {
354   __u32 deh_offset;  /* third component of the directory entry key */
355   __u32 deh_dir_id;  /* objectid of the parent directory of the
356                         object, that is referenced by directory entry */
357   __u32 deh_objectid;/* objectid of the object, that is referenced by
358                         directory entry */
359   __u16 deh_location;/* offset of name in the whole item */
360   __u16 deh_state;   /* whether 1) entry contains stat data (for
361                         future), and 2) whether entry is hidden
362                         (unlinked) */
363 };
364
365 #define DEH_SIZE (sizeof (struct reiserfs_de_head))
366 #define deh_offset(p_deh)         (__le32_to_cpu((p_deh)->deh_offset))
367 #define deh_dir_id(p_deh)         (__le32_to_cpu((p_deh)->deh_dir_id))
368 #define deh_objectid(p_deh)       (__le32_to_cpu((p_deh)->deh_objectid))
369 #define deh_location(p_deh)       (__le16_to_cpu((p_deh)->deh_location))
370 #define deh_state(p_deh)          (__le16_to_cpu((p_deh)->deh_state))
371
372
373 #define DEH_Statdata (1 << 0)                   /* not used now */
374 #define DEH_Visible  (1 << 2)
375
376 #define SD_OFFSET  0
377 #define SD_UNIQUENESS 0
378 #define DOT_OFFSET 1
379 #define DOT_DOT_OFFSET 2
380 #define DIRENTRY_UNIQUENESS 500
381
382 #define V1_TYPE_STAT_DATA 0x0
383 #define V1_TYPE_DIRECT 0xffffffff
384 #define V1_TYPE_INDIRECT 0xfffffffe
385 #define V1_TYPE_DIRECTORY_MAX 0xfffffffd
386 #define V2_TYPE_STAT_DATA 0
387 #define V2_TYPE_INDIRECT 1
388 #define V2_TYPE_DIRECT 2
389 #define V2_TYPE_DIRENTRY 3
390
391 #define REISERFS_ROOT_OBJECTID 2
392 #define REISERFS_ROOT_PARENT_OBJECTID 1
393 #define REISERFS_DISK_OFFSET_IN_BYTES (64 * 1024)
394 /* the spot for the super in versions 3.5 - 3.5.11 (inclusive) */
395 #define REISERFS_OLD_DISK_OFFSET_IN_BYTES (8 * 1024)
396 #define REISERFS_OLD_BLOCKSIZE 4096
397
398 #define S_ISREG(mode) (((mode) & 0170000) == 0100000)
399 #define S_ISDIR(mode) (((mode) & 0170000) == 0040000)
400 #define S_ISLNK(mode) (((mode) & 0170000) == 0120000)
401
402 #define PATH_MAX       1024     /* include/linux/limits.h */
403 #define MAX_LINK_COUNT    5     /* number of symbolic links to follow */
404
405 /* The size of the node cache */
406 #define FSYSREISER_CACHE_SIZE 24*1024
407 #define FSYSREISER_MIN_BLOCKSIZE SECTOR_SIZE
408 #define FSYSREISER_MAX_BLOCKSIZE FSYSREISER_CACHE_SIZE / 3
409
410 /* Info about currently opened file */
411 struct fsys_reiser_fileinfo
412 {
413   __u32 k_dir_id;
414   __u32 k_objectid;
415 };
416
417 /* In memory info about the currently mounted filesystem */
418 struct fsys_reiser_info
419 {
420   /* The last read item head */
421   struct item_head *current_ih;
422   /* The last read item */
423   char *current_item;
424   /* The information for the currently opened file */
425   struct fsys_reiser_fileinfo fileinfo;
426   /* The start of the journal */
427   __u32 journal_block;
428   /* The size of the journal */
429   __u32 journal_block_count;
430   /* The first valid descriptor block in journal
431      (relative to journal_block) */
432   __u32 journal_first_desc;
433
434   /* The ReiserFS version. */
435   __u16 version;
436   /* The current depth of the reiser tree. */
437   __u16 tree_depth;
438   /* SECTOR_SIZE << blocksize_shift == blocksize. */
439   __u8  blocksize_shift;
440   /* 1 << full_blocksize_shift == blocksize. */
441   __u8  fullblocksize_shift;
442   /* The reiserfs block size  (must be a power of 2) */
443   __u16 blocksize;
444   /* The number of cached tree nodes */
445   __u16 cached_slots;
446   /* The number of valid transactions in journal */
447   __u16 journal_transactions;
448
449   unsigned int blocks[MAX_HEIGHT];
450   unsigned int next_key_nr[MAX_HEIGHT];
451 };
452
453 /* The cached s+tree blocks in FSYS_BUF,  see below
454  * for a more detailed description.
455  */
456 #define ROOT     ((char *) ((int) FSYS_BUF))
457 #define CACHE(i) (ROOT + ((i) << INFO->fullblocksize_shift))
458 #define LEAF     CACHE (DISK_LEAF_NODE_LEVEL)
459
460 #define BLOCKHEAD(cache) ((struct block_head *) cache)
461 #define ITEMHEAD         ((struct item_head  *) ((int) LEAF + BLKH_SIZE))
462 #define KEY(cache)       ((struct key        *) ((int) cache + BLKH_SIZE))
463 #define DC(cache)        ((struct disk_child *) \
464                           ((int) cache + BLKH_SIZE + KEY_SIZE * nr_item))
465 /* The fsys_reiser_info block.
466  */
467 #define INFO \
468     ((struct fsys_reiser_info *) ((int) FSYS_BUF + FSYSREISER_CACHE_SIZE))
469 /*
470  * The journal cache.  For each transaction it contains the number of
471  * blocks followed by the real block numbers of this transaction.
472  *
473  * If the block numbers of some transaction won't fit in this space,
474  * this list is stopped with a 0xffffffff marker and the remaining
475  * uncommitted transactions aren't cached.
476  */
477 #define JOURNAL_START    ((__u32 *) (INFO + 1))
478 #define JOURNAL_END      ((__u32 *) (FSYS_BUF + FSYS_BUFLEN))
479
480
481 static __inline__ unsigned long
482 log2 (unsigned long word)
483 {
484 #ifdef __I386__
485   __asm__ ("bsfl %1,%0"
486            : "=r" (word)
487            : "r" (word));
488   return word;
489 #else
490   int i;
491
492   for(i=0; i<(8*sizeof(word)); i++)
493     if ((1<<i) & word)
494       return i;
495
496   return 0;
497 #endif
498 }
499
500 static __inline__ int
501 is_power_of_two (unsigned long word)
502 {
503   return (word & -word) == word;
504 }
505
506 extern const char *bb_mode_string(int mode);
507 extern int reiserfs_devread (int sector, int byte_offset, int byte_len, char *buf);