]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/gnu/fs/reiserfs/reiserfs_fs.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / gnu / fs / reiserfs / reiserfs_fs.h
1 /*-
2  * Copyright 2000 Hans Reiser
3  * See README for licensing and copyright details
4  * 
5  * Ported to FreeBSD by Jean-Sébastien Pédron <jspedron@club-internet.fr>
6  * 
7  * $FreeBSD$
8  */
9
10 #ifndef _GNU_REISERFS_REISERFS_FS_H
11 #define _GNU_REISERFS_REISERFS_FS_H
12
13 #include <sys/cdefs.h>
14 #include <sys/types.h>
15 #include <sys/endian.h>
16 #include <sys/param.h>
17 #include <sys/systm.h>
18 #include <sys/kernel.h>
19 #include <sys/mount.h>
20 #include <sys/namei.h>
21 #include <sys/priv.h>
22 #include <sys/proc.h>
23 #include <sys/vnode.h>
24 #include <sys/unistd.h>
25
26 #include <sys/bio.h>
27 #include <sys/buf.h>
28 #include <sys/conf.h>
29 #include <sys/fcntl.h>
30 #include <sys/syslog.h>
31
32 #include <sys/malloc.h>
33 #include <sys/dirent.h>
34 #include <sys/stat.h>
35 //#include <sys/mutex.h>
36
37 #include <sys/ctype.h>
38 #include <sys/bitstring.h>
39
40 #include <geom/geom.h>
41 #include <geom/geom_vfs.h>
42
43 #include <gnu/fs/reiserfs/reiserfs_mount.h>
44 #include <gnu/fs/reiserfs/reiserfs_fs_sb.h>
45 #include <gnu/fs/reiserfs/reiserfs_fs_i.h>
46
47 /* n must be power of 2 */
48 #define _ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u))
49
50 /* To be ok for alpha and others we have to align structures to 8 byte
51  * boundary. */
52 #define ROUND_UP(x)     _ROUND_UP(x, 8LL)
53
54 /* -------------------------------------------------------------------
55  * Global variables
56  * -------------------------------------------------------------------*/
57
58 extern struct vop_vector reiserfs_vnodeops;
59 extern struct vop_vector reiserfs_specops;
60
61 /* -------------------------------------------------------------------
62  * Super block
63  * -------------------------------------------------------------------*/
64
65 #define REISERFS_BSIZE 1024
66
67 /* ReiserFS leaves the first 64k unused, so that partition labels have
68  * enough space. If someone wants to write a fancy bootloader that needs
69  * more than 64k, let us know, and this will be increased in size.
70  * This number must be larger than than the largest block size on any
71  * platform, or code will break. -Hans */
72 #define REISERFS_DISK_OFFSET 64
73 #define REISERFS_DISK_OFFSET_IN_BYTES                                        \
74     ((REISERFS_DISK_OFFSET) * (REISERFS_BSIZE))
75
76 /* The spot for the super in versions 3.5 - 3.5.10 (inclusive) */
77 #define REISERFS_OLD_DISK_OFFSET 8
78 #define REISERFS_OLD_DISK_OFFSET_IN_BYTES                                    \
79     ((REISERFS_OLD_DISK_OFFSET) * (REISERFS_BSIZE))
80
81 /*
82  * Structure of a super block on disk, a version of which in RAM is
83  * often accessed as REISERFS_SB(s)->r_rs. The version in RAM is part of
84  * a larger structure containing fields never written to disk.
85  */
86
87 #define UNSET_HASH      0 /* read_super will guess about, what hash names
88                              in directories were sorted with */
89 #define TEA_HASH        1
90 #define YURA_HASH       2
91 #define R5_HASH         3
92 #define DEFAULT_HASH    R5_HASH
93
94 struct journal_params {
95         uint32_t        jp_journal_1st_block;      /* Where does journal start
96                                                       from on its device */
97         uint32_t        jp_journal_dev;            /* Journal device st_rdev */
98         uint32_t        jp_journal_size;           /* Size of the journal */
99         uint32_t        jp_journal_trans_max;      /* Max number of blocks in
100                                                       a transaction */
101         uint32_t        jp_journal_magic;          /* Random value made on
102                                                       fs creation (this was
103                                                       sb_journal_block_count) */
104         uint32_t        jp_journal_max_batch;      /* Max number of blocks to
105                                                       batch into a
106                                                       transaction */
107         uint32_t        jp_journal_max_commit_age; /* In seconds, how old can
108                                                       an async commit be */
109         uint32_t        jp_journal_max_trans_age;  /* In seconds, how old a
110                                                       transaction be */
111 };
112
113 struct reiserfs_super_block_v1 {
114         uint32_t        s_block_count; /* Blocks count      */
115         uint32_t        s_free_blocks; /* Free blocks count */
116         uint32_t        s_root_block;  /* Root block number */
117
118         struct journal_params s_journal;
119
120         uint16_t        s_blocksize;
121         uint16_t        s_oid_maxsize;
122         uint16_t        s_oid_cursize;
123         uint16_t        s_umount_state;
124
125         char            s_magic[10];
126
127         uint16_t        s_fs_state;
128         uint32_t        s_hash_function_code;
129         uint16_t        s_tree_height;
130         uint16_t        s_bmap_nr;
131         uint16_t        s_version;
132         uint16_t        s_reserved_for_journal;
133 } __packed;
134
135 #define SB_SIZE_V1 (sizeof(struct reiserfs_super_block_v1))
136
137 struct reiserfs_super_block {
138         struct reiserfs_super_block_v1 s_v1;
139         uint32_t        s_inode_generation;
140         uint32_t        s_flags;
141         unsigned char   s_uuid[16];
142         unsigned char   s_label[16];
143         char            s_unused[88];
144 } __packed;
145
146 #define SB_SIZE (sizeof(struct reiserfs_super_block))
147
148 #define REISERFS_VERSION_1      0
149 #define REISERFS_VERSION_2      2
150
151 #define REISERFS_SB(sbi)                (sbi)
152 #define SB_DISK_SUPER_BLOCK(sbi)        (REISERFS_SB(sbi)->s_rs)
153 #define SB_V1_DISK_SUPER_BLOCK(sbi)     (&(SB_DISK_SUPER_BLOCK(sbi)->s_v1))
154
155 #define SB_BLOCKSIZE(sbi)                                               \
156     le32toh((SB_V1_DISK_SUPER_BLOCK(sbi)->s_blocksize))
157 #define SB_BLOCK_COUNT(sbi)                                             \
158     le32toh((SB_V1_DISK_SUPER_BLOCK(sbi)->s_block_count))
159 #define SB_FREE_BLOCKS(s)                                               \
160     le32toh((SB_V1_DISK_SUPER_BLOCK(sbi)->s_free_blocks))
161
162 #define SB_REISERFS_MAGIC(sbi)                                          \
163     (SB_V1_DISK_SUPER_BLOCK(sbi)->s_magic)
164
165 #define SB_ROOT_BLOCK(sbi)                                              \
166     le32toh((SB_V1_DISK_SUPER_BLOCK(sbi)->s_root_block))
167
168 #define SB_TREE_HEIGHT(sbi)                                             \
169     le16toh((SB_V1_DISK_SUPER_BLOCK(sbi)->s_tree_height))
170
171 #define SB_REISERFS_STATE(sbi)                                          \
172     le16toh((SB_V1_DISK_SUPER_BLOCK(sbi)->s_umount_state))
173
174 #define SB_VERSION(sbi) le16toh((SB_V1_DISK_SUPER_BLOCK(sbi)->s_version))
175 #define SB_BMAP_NR(sbi) le16toh((SB_V1_DISK_SUPER_BLOCK(sbi)->s_bmap_nr))
176
177 #define REISERFS_SUPER_MAGIC_STRING     "ReIsErFs"
178 #define REISER2FS_SUPER_MAGIC_STRING    "ReIsEr2Fs"
179 #define REISER2FS_JR_SUPER_MAGIC_STRING "ReIsEr3Fs"
180
181 extern const char reiserfs_3_5_magic_string[];
182 extern const char reiserfs_3_6_magic_string[];
183 extern const char reiserfs_jr_magic_string[];
184
185 int     is_reiserfs_3_5(struct reiserfs_super_block *rs);
186 int     is_reiserfs_3_6(struct reiserfs_super_block *rs);
187 int     is_reiserfs_jr(struct reiserfs_super_block *rs);
188
189 /* ReiserFS internal error code (used by search_by_key and fix_nodes) */
190 #define IO_ERROR        -2
191
192 typedef uint32_t b_blocknr_t;
193 typedef uint32_t unp_t;
194
195 struct unfm_nodeinfo {
196         unp_t           unfm_nodenum;
197         unsigned short  unfm_freespace;
198 };
199
200 /* There are two formats of keys: 3.5 and 3.6 */
201 #define KEY_FORMAT_3_5  0
202 #define KEY_FORMAT_3_6  1
203
204 /* There are two stat datas */
205 #define STAT_DATA_V1    0
206 #define STAT_DATA_V2    1
207
208 #define REISERFS_I(ip)  (ip)
209
210 #define get_inode_item_key_version(ip)                                  \
211     ((REISERFS_I(ip)->i_flags & i_item_key_version_mask) ?              \
212      KEY_FORMAT_3_6 : KEY_FORMAT_3_5)
213
214 #define set_inode_item_key_version(ip, version) ({                      \
215         if ((version) == KEY_FORMAT_3_6)                                \
216                 REISERFS_I(ip)->i_flags |= i_item_key_version_mask;     \
217         else                                                            \
218                 REISERFS_I(ip)->i_flags &= ~i_item_key_version_mask;    \
219 })
220
221 #define get_inode_sd_version(ip)                                        \
222     ((REISERFS_I(ip)->i_flags & i_stat_data_version_mask) ?             \
223      STAT_DATA_V2 : STAT_DATA_V1)
224
225 #define set_inode_sd_version(inode, version) ({                         \
226         if((version) == STAT_DATA_V2)                                   \
227                 REISERFS_I(ip)->i_flags |= i_stat_data_version_mask;    \
228         else                                                            \
229                 REISERFS_I(ip)->i_flags &= ~i_stat_data_version_mask;   \
230 })
231
232 /* Values for s_umount_state field */
233 #define REISERFS_VALID_FS       1
234 #define REISERFS_ERROR_FS       2
235
236 /* There are 5 item types currently */
237 #define TYPE_STAT_DATA          0
238 #define TYPE_INDIRECT           1
239 #define TYPE_DIRECT             2
240 #define TYPE_DIRENTRY           3
241 #define TYPE_MAXTYPE            3
242 #define TYPE_ANY                15
243
244 /* -------------------------------------------------------------------
245  * Key & item head
246  * -------------------------------------------------------------------*/
247
248 struct offset_v1 {
249         uint32_t        k_offset;
250         uint32_t        k_uniqueness;
251 } __packed;
252
253 struct offset_v2 {
254 #if BYTE_ORDER == LITTLE_ENDIAN
255         /* little endian version */
256         uint64_t        k_offset:60;
257         uint64_t        k_type:4;
258 #else
259         /* big endian version */
260         uint64_t        k_type:4;
261         uint64_t        k_offset:60;
262 #endif
263 } __packed;
264
265 #if (BYTE_ORDER == BIG_ENDIAN)
266 typedef union {
267         struct offset_v2        offset_v2;
268         uint64_t                linear;
269 } __packed offset_v2_esafe_overlay;
270
271 static inline uint16_t
272 offset_v2_k_type(const struct offset_v2 *v2)
273 {
274
275         offset_v2_esafe_overlay tmp = *(const offset_v2_esafe_overlay *)v2;
276         tmp.linear = le64toh(tmp.linear);
277         return ((tmp.offset_v2.k_type <= TYPE_MAXTYPE) ?
278             tmp.offset_v2.k_type : TYPE_ANY);
279 }
280
281 static inline void
282 set_offset_v2_k_type(struct offset_v2 *v2, int type)
283 {
284
285         offset_v2_esafe_overlay *tmp = (offset_v2_esafe_overlay *)v2;
286         tmp->linear = le64toh(tmp->linear);
287         tmp->offset_v2.k_type = type;
288         tmp->linear = htole64(tmp->linear);
289 }
290
291 static inline off_t
292 offset_v2_k_offset(const struct offset_v2 *v2)
293 {
294
295         offset_v2_esafe_overlay tmp = *(const offset_v2_esafe_overlay *)v2;
296         tmp.linear = le64toh(tmp.linear);
297         return (tmp.offset_v2.k_offset);
298 }
299
300 static inline void
301 set_offset_v2_k_offset(struct offset_v2 *v2, off_t offset)
302 {
303
304         offset_v2_esafe_overlay *tmp = (offset_v2_esafe_overlay *)v2;
305         tmp->linear = le64toh(tmp->linear);
306         tmp->offset_v2.k_offset = offset;
307         tmp->linear = htole64(tmp->linear);
308 }
309 #else /* BYTE_ORDER != BIG_ENDIAN */
310 #define offset_v2_k_type(v2)            ((v2)->k_type)
311 #define set_offset_v2_k_type(v2, val)   (offset_v2_k_type(v2) = (val))
312 #define offset_v2_k_offset(v2)          ((v2)->k_offset)
313 #define set_offset_v2_k_offset(v2, val) (offset_v2_k_offset(v2) = (val))
314 #endif /* BYTE_ORDER == BIG_ENDIAN */
315
316 /*
317  * Key of an item determines its location in the S+tree, and
318  * is composed of 4 components
319  */
320 struct key {
321         uint32_t        k_dir_id;    /* Packing locality: by default parent
322                                         directory object id */
323         uint32_t        k_objectid;  /* Object identifier */
324         union {
325                 struct offset_v1        k_offset_v1;
326                 struct offset_v2        k_offset_v2;
327         } __packed u;
328 } __packed;
329
330 struct cpu_key {
331         struct key      on_disk_key;
332         int             version;
333         int             key_length; /* 3 in all cases but direct2indirect
334                                        and indirect2direct conversion */
335 };
336
337 /*
338  * Our function for comparing keys can compare keys of different
339  * lengths. It takes as a parameter the length of the keys it is to
340  * compare. These defines are used in determining what is to be passed
341  * to it as that parameter.
342  */
343 #define REISERFS_FULL_KEY_LEN   4
344 #define REISERFS_SHORT_KEY_LEN  2
345
346 #define KEY_SIZE        (sizeof(struct key))
347 #define SHORT_KEY_SIZE  (sizeof(uint32_t) + sizeof(uint32_t))
348
349 /* Return values for search_by_key and clones */
350 #define ITEM_FOUND               1
351 #define ITEM_NOT_FOUND           0
352 #define ENTRY_FOUND              1
353 #define ENTRY_NOT_FOUND          0
354 #define DIRECTORY_NOT_FOUND     -1
355 #define REGULAR_FILE_FOUND      -2
356 #define DIRECTORY_FOUND         -3
357 #define BYTE_FOUND               1
358 #define BYTE_NOT_FOUND           0
359 #define FILE_NOT_FOUND          -1
360
361 #define POSITION_FOUND           1
362 #define POSITION_NOT_FOUND       0
363
364 /* Return values for reiserfs_find_entry and search_by_entry_key */
365 #define NAME_FOUND              1
366 #define NAME_NOT_FOUND          0
367 #define GOTO_PREVIOUS_ITEM      2
368 #define NAME_FOUND_INVISIBLE    3
369
370 /*
371  * Everything in the filesystem is stored as a set of items. The item
372  * head contains the key of the item, its free space (for indirect
373  * items) and specifies the location of the item itself within the
374  * block.
375  */
376 struct item_head {
377         /*
378          * Everything in the tree is found by searching for it based on
379          * its key.
380          */
381         struct key      ih_key;
382         union {
383                 /*
384                  * The free space in the last unformatted node of an
385                  * indirect item if this is an indirect item. This
386                  * equals 0xFFFF iff this is a direct item or stat data
387                  * item. Note that the key, not this field, is used to
388                  * determine the item type, and thus which field this
389                  * union contains.
390                  */
391                 uint16_t        ih_free_space_reserved;
392
393                 /*
394                  * If this is a directory item, this field equals the number of
395                  * directory entries in the directory item.
396                  */
397                 uint16_t        ih_entry_count;
398         } __packed u;
399         uint16_t        ih_item_len;      /* Total size of the item body */
400         uint16_t        ih_item_location; /* An offset to the item body within
401                                              the block */
402         uint16_t        ih_version;       /* 0 for all old items, 2 for new
403                                              ones. Highest bit is set by fsck
404                                              temporary, cleaned after all
405                                              done */
406 } __packed;
407
408 /* Size of item header */
409 #define IH_SIZE (sizeof(struct item_head))
410
411 #define ih_free_space(ih)       le16toh((ih)->u.ih_free_space_reserved)
412 #define ih_version(ih)          le16toh((ih)->ih_version)
413 #define ih_entry_count(ih)      le16toh((ih)->u.ih_entry_count)
414 #define ih_location(ih)         le16toh((ih)->ih_item_location)
415 #define ih_item_len(ih)         le16toh((ih)->ih_item_len)
416
417 /*
418  * These operate on indirect items, where you've got an array of ints at
419  * a possibly unaligned location. These are a noop on IA32.
420  * 
421  * p is the array of uint32_t, i is the index into the array, v is the
422  * value to store there.
423  */
424 #define get_unaligned(ptr)                                              \
425     ({ __typeof__(*(ptr)) __tmp;                                        \
426      memcpy(&__tmp, (ptr), sizeof(*(ptr))); __tmp; })
427
428 #define put_unaligned(val, ptr)                                         \
429     ({ __typeof__(*(ptr)) __tmp = (val);                                \
430      memcpy((ptr), &__tmp, sizeof(*(ptr)));                             \
431      (void)0; })
432
433 #define get_block_num(p, i)     le32toh(get_unaligned((p) + (i)))
434 #define put_block_num(p, i, v)  put_unaligned(htole32(v), (p) + (i))
435
436 /* In old version uniqueness field shows key type */
437 #define V1_SD_UNIQUENESS        0
438 #define V1_INDIRECT_UNIQUENESS  0xfffffffe
439 #define V1_DIRECT_UNIQUENESS    0xffffffff
440 #define V1_DIRENTRY_UNIQUENESS  500
441 #define V1_ANY_UNIQUENESS       555
442
443 /* Here are conversion routines */
444 static inline int       uniqueness2type(uint32_t uniqueness);
445 static inline uint32_t  type2uniqueness(int type);
446
447 static inline int
448 uniqueness2type(uint32_t uniqueness)
449 {
450
451         switch ((int)uniqueness) {
452         case V1_SD_UNIQUENESS:
453                 return (TYPE_STAT_DATA);
454         case V1_INDIRECT_UNIQUENESS:
455                 return (TYPE_INDIRECT);
456         case V1_DIRECT_UNIQUENESS:
457                 return (TYPE_DIRECT);
458         case V1_DIRENTRY_UNIQUENESS:
459                 return (TYPE_DIRENTRY);
460         default:
461                 log(LOG_NOTICE, "reiserfs: unknown uniqueness (%u)\n",
462                     uniqueness);
463         case V1_ANY_UNIQUENESS:
464                 return (TYPE_ANY);
465         }
466 }
467
468 static inline uint32_t
469 type2uniqueness(int type)
470 {
471
472         switch (type) {
473         case TYPE_STAT_DATA:
474                 return (V1_SD_UNIQUENESS);
475         case TYPE_INDIRECT:
476                 return (V1_INDIRECT_UNIQUENESS);
477         case TYPE_DIRECT:
478                 return (V1_DIRECT_UNIQUENESS);
479         case TYPE_DIRENTRY:
480                 return (V1_DIRENTRY_UNIQUENESS);
481         default:
482                 log(LOG_NOTICE, "reiserfs: unknown type (%u)\n", type);
483         case TYPE_ANY:
484                 return (V1_ANY_UNIQUENESS);
485         }
486 }
487
488 /*
489  * Key is pointer to on disk key which is stored in le, result is cpu,
490  * there is no way to get version of object from key, so, provide
491  * version to these defines.
492  */
493 static inline off_t
494 le_key_k_offset(int version, const struct key *key)
495 {
496
497         return ((version == KEY_FORMAT_3_5) ?
498             le32toh(key->u.k_offset_v1.k_offset) :
499             offset_v2_k_offset(&(key->u.k_offset_v2)));
500 }
501
502 static inline off_t
503 le_ih_k_offset(const struct item_head *ih)
504 {
505
506         return (le_key_k_offset(ih_version(ih), &(ih->ih_key)));
507 }
508
509 static inline off_t
510 le_key_k_type(int version, const struct key *key)
511 {
512
513         return ((version == KEY_FORMAT_3_5) ?
514             uniqueness2type(le32toh(key->u.k_offset_v1.k_uniqueness)) :
515             offset_v2_k_type(&(key->u.k_offset_v2)));
516 }
517
518 static inline off_t
519 le_ih_k_type(const struct item_head *ih)
520 {
521         return (le_key_k_type(ih_version(ih), &(ih->ih_key)));
522 }
523
524 static inline void
525 set_le_key_k_offset(int version, struct key *key, off_t offset)
526 {
527
528         (version == KEY_FORMAT_3_5) ?
529             (key->u.k_offset_v1.k_offset = htole32(offset)) :
530             (set_offset_v2_k_offset(&(key->u.k_offset_v2), offset));
531 }
532
533 static inline void
534 set_le_ih_k_offset(struct item_head *ih, off_t offset)
535 {
536
537         set_le_key_k_offset(ih_version(ih), &(ih->ih_key), offset);
538 }
539
540 static inline void
541 set_le_key_k_type(int version, struct key *key, int type)
542 {
543
544         (version == KEY_FORMAT_3_5) ?
545             (key->u.k_offset_v1.k_uniqueness =
546              htole32(type2uniqueness(type))) :
547             (set_offset_v2_k_type(&(key->u.k_offset_v2), type));
548 }
549
550 static inline void
551 set_le_ih_k_type(struct item_head *ih, int type)
552 {
553
554         set_le_key_k_type(ih_version(ih), &(ih->ih_key), type);
555 }
556
557 #define is_direntry_le_key(version, key)                                \
558     (le_key_k_type(version, key) == TYPE_DIRENTRY)
559 #define is_direct_le_key(version, key)                                  \
560     (le_key_k_type(version, key) == TYPE_DIRECT)
561 #define is_indirect_le_key(version, key)                                \
562     (le_key_k_type(version, key) == TYPE_INDIRECT)
563 #define is_statdata_le_key(version, key)                                \
564     (le_key_k_type(version, key) == TYPE_STAT_DATA)
565
566 /* Item header has version. */
567 #define is_direntry_le_ih(ih)                                           \
568     is_direntry_le_key(ih_version(ih), &((ih)->ih_key))
569 #define is_direct_le_ih(ih)                                             \
570     is_direct_le_key(ih_version(ih), &((ih)->ih_key))
571 #define is_indirect_le_ih(ih)                                           \
572     is_indirect_le_key(ih_version(ih), &((ih)->ih_key))
573 #define is_statdata_le_ih(ih)                                           \
574     is_statdata_le_key(ih_version(ih), &((ih)->ih_key))
575
576 static inline void
577 set_cpu_key_k_offset(struct cpu_key *key, off_t offset)
578 {
579
580         (key->version == KEY_FORMAT_3_5) ?
581             (key->on_disk_key.u.k_offset_v1.k_offset = offset) :
582             (key->on_disk_key.u.k_offset_v2.k_offset = offset);
583 }
584
585 static inline void
586 set_cpu_key_k_type(struct cpu_key *key, int type)
587 {
588
589         (key->version == KEY_FORMAT_3_5) ?
590             (key->on_disk_key.u.k_offset_v1.k_uniqueness =
591              type2uniqueness(type)):
592             (key->on_disk_key.u.k_offset_v2.k_type = type);
593 }
594
595 #define is_direntry_cpu_key(key)        (cpu_key_k_type (key) == TYPE_DIRENTRY)
596 #define is_direct_cpu_key(key)          (cpu_key_k_type (key) == TYPE_DIRECT)
597 #define is_indirect_cpu_key(key)        (cpu_key_k_type (key) == TYPE_INDIRECT)
598 #define is_statdata_cpu_key(key)        (cpu_key_k_type (key) == TYPE_STAT_DATA)
599
600 /* Maximal length of item */
601 #define MAX_ITEM_LEN(block_size)        (block_size - BLKH_SIZE - IH_SIZE)
602 #define MIN_ITEM_LEN                    1
603
604 /* Object identifier for root dir */
605 #define REISERFS_ROOT_OBJECTID          2
606 #define REISERFS_ROOT_PARENT_OBJECTID   1
607
608 /* key is pointer to cpu key, result is cpu */
609 static inline off_t
610 cpu_key_k_offset(const struct cpu_key *key)
611 {
612
613         return ((key->version == KEY_FORMAT_3_5) ?
614             key->on_disk_key.u.k_offset_v1.k_offset :
615             key->on_disk_key.u.k_offset_v2.k_offset);
616 }
617
618 static inline off_t
619 cpu_key_k_type(const struct cpu_key *key)
620 {
621
622         return ((key->version == KEY_FORMAT_3_5) ?
623             uniqueness2type(key->on_disk_key.u.k_offset_v1.k_uniqueness) :
624             key->on_disk_key.u.k_offset_v2.k_type);
625 }
626
627 /*
628  * Header of a disk block.  More precisely, header of a formatted leaf
629  * or internal node, and not the header of an unformatted node.
630  */
631 struct block_head {
632         uint16_t        blk_level;            /* Level of a block in the
633                                                  tree. */
634         uint16_t        blk_nr_item;          /* Number of keys/items in a
635                                                  block. */
636         uint16_t        blk_free_space;       /* Block free space in bytes. */
637         uint16_t        blk_reserved;         /* Dump this in v4/planA */
638         struct key      blk_right_delim_key;  /* Kept only for compatibility */
639 };
640
641 #define BLKH_SIZE               (sizeof(struct block_head))
642 #define blkh_level(p_blkh)      (le16toh((p_blkh)->blk_level))
643 #define blkh_nr_item(p_blkh)    (le16toh((p_blkh)->blk_nr_item))
644 #define blkh_free_space(p_blkh) (le16toh((p_blkh)->blk_free_space))
645
646 #define FREE_LEVEL      0 /* When node gets removed from the tree its
647                              blk_level is set to FREE_LEVEL. It is then
648                              used to see whether the node is still in the
649                              tree */
650
651 /* Values for blk_level field of the struct block_head */
652 #define DISK_LEAF_NODE_LEVEL    1 /* Leaf node level.*/
653
654 /*
655  * Given the buffer head of a formatted node, resolve to the block head
656  * of that node.
657  */
658 #define B_BLK_HEAD(p_s_bp)      ((struct block_head *)((p_s_bp)->b_data))
659 #define B_NR_ITEMS(p_s_bp)      (blkh_nr_item(B_BLK_HEAD(p_s_bp)))
660 #define B_LEVEL(p_s_bp)         (blkh_level(B_BLK_HEAD(p_s_bp)))
661 #define B_FREE_SPACE(p_s_bp)    (blkh_free_space(B_BLK_HEAD(p_s_bp)))
662
663 /* -------------------------------------------------------------------
664  * Stat data
665  * -------------------------------------------------------------------*/
666
667 /*
668  * Old stat data is 32 bytes long. We are going to distinguish new one
669  * by different size.
670  */
671 struct stat_data_v1 {
672         uint16_t        sd_mode;  /* File type, permissions */
673         uint16_t        sd_nlink; /* Number of hard links */
674         uint16_t        sd_uid;   /* Owner */
675         uint16_t        sd_gid;   /* Group */
676         uint32_t        sd_size;  /* File size */
677         uint32_t        sd_atime; /* Time of last access */
678         uint32_t        sd_mtime; /* Time file was last modified  */
679         uint32_t        sd_ctime; /* Time inode (stat data) was last changed
680                                      (except changes to sd_atime and
681                                      sd_mtime) */
682         union {
683                 uint32_t        sd_rdev;
684                 uint32_t        sd_blocks;  /* Number of blocks file uses */
685         } __packed u;
686         uint32_t        sd_first_direct_byte; /* First byte of file which is
687                                                  stored in a direct item:
688                                                  except that if it equals 1
689                                                  it is a symlink and if it
690                                                  equals ~(uint32_t)0 there
691                                                  is no direct item. The
692                                                  existence of this field
693                                                  really grates on me. Let's
694                                                  replace it with a macro based
695                                                  on sd_size and our tail
696                                                  suppression policy. Someday.
697                                                  -Hans */
698 } __packed;
699
700 #define SD_V1_SIZE                      (sizeof(struct stat_data_v1))
701 #define stat_data_v1(ih)                (ih_version (ih) == KEY_FORMAT_3_5)
702 #define sd_v1_mode(sdp)                 (le16toh((sdp)->sd_mode))
703 #define set_sd_v1_mode(sdp, v)          ((sdp)->sd_mode = htole16(v))
704 #define sd_v1_nlink(sdp)                (le16toh((sdp)->sd_nlink))
705 #define set_sd_v1_nlink(sdp, v)         ((sdp)->sd_nlink = htole16(v))
706 #define sd_v1_uid(sdp)                  (le16toh((sdp)->sd_uid))
707 #define set_sd_v1_uid(sdp, v)           ((sdp)->sd_uid = htole16(v))
708 #define sd_v1_gid(sdp)                  (le16toh((sdp)->sd_gid))
709 #define set_sd_v1_gid(sdp, v)           ((sdp)->sd_gid = htole16(v))
710 #define sd_v1_size(sdp)                 (le32toh((sdp)->sd_size))
711 #define set_sd_v1_size(sdp, v)          ((sdp)->sd_size = htole32(v))
712 #define sd_v1_atime(sdp)                (le32toh((sdp)->sd_atime))
713 #define set_sd_v1_atime(sdp, v)         ((sdp)->sd_atime = htole32(v))
714 #define sd_v1_mtime(sdp)                (le32toh((sdp)->sd_mtime))
715 #define set_sd_v1_mtime(sdp, v)         ((sdp)->sd_mtime = htole32(v))
716 #define sd_v1_ctime(sdp)                (le32toh((sdp)->sd_ctime))
717 #define set_sd_v1_ctime(sdp, v)         ((sdp)->sd_ctime = htole32(v))
718 #define sd_v1_rdev(sdp)                 (le32toh((sdp)->u.sd_rdev))
719 #define set_sd_v1_rdev(sdp, v)          ((sdp)->u.sd_rdev = htole32(v))
720 #define sd_v1_blocks(sdp)               (le32toh((sdp)->u.sd_blocks))
721 #define set_sd_v1_blocks(sdp, v)        ((sdp)->u.sd_blocks = htole32(v))
722 #define sd_v1_first_direct_byte(sdp)                                    \
723     (le32toh((sdp)->sd_first_direct_byte))
724 #define set_sd_v1_first_direct_byte(sdp, v)                             \
725     ((sdp)->sd_first_direct_byte = htole32(v))
726
727 /*
728  * We want common flags to have the same values as in ext2,
729  * so chattr(1) will work without problems
730  */
731 #include <fs/ext2fs/ext2fs.h>
732 #include <fs/ext2fs/ext2_dinode.h>
733 #define REISERFS_IMMUTABLE_FL   EXT2_IMMUTABLE
734 #define REISERFS_APPEND_FL      EXT2_APPEND
735 #define REISERFS_SYNC_FL        EXT2_SYNC
736 #define REISERFS_NOATIME_FL     EXT2_NOATIME
737 #define REISERFS_NODUMP_FL      EXT2_NODUMP
738 #define REISERFS_SECRM_FL       EXT2_SECRM
739 #define REISERFS_UNRM_FL        EXT2_UNRM
740 #define REISERFS_COMPR_FL       EXT2_COMPR
741 #define REISERFS_NOTAIL_FL      EXT2_NOTAIL_FL
742
743 /*
744  * Stat Data on disk (reiserfs version of UFS disk inode minus the
745  * address blocks)
746  */
747 struct stat_data {
748         uint16_t        sd_mode;  /* File type, permissions */
749         uint16_t        sd_attrs; /* Persistent inode flags */
750         uint32_t        sd_nlink; /* Number of hard links */
751         uint64_t        sd_size;  /* File size */
752         uint32_t        sd_uid;   /* Owner */
753         uint32_t        sd_gid;   /* Group */
754         uint32_t        sd_atime; /* Time of last access */
755         uint32_t        sd_mtime; /* Time file was last modified  */
756         uint32_t        sd_ctime; /* Time inode (stat data) was last changed
757                                      (except changes to sd_atime and
758                                      sd_mtime) */
759         uint32_t        sd_blocks;
760         union {
761                 uint32_t        sd_rdev;
762                 uint32_t        sd_generation;
763                 //uint32_t      sd_first_direct_byte; 
764                 /*
765                  * First byte of file which is stored in a
766                  * direct item: except that if it equals 1
767                  * it is a symlink and if it equals
768                  * ~(uint32_t)0 there is no direct item.  The
769                  * existence of this field really grates
770                  * on me. Let's replace it with a macro
771                  * based on sd_size and our tail
772                  * suppression policy?
773                  */
774         } __packed u;
775 } __packed;
776
777 /* This is 44 bytes long */
778 #define SD_SIZE                         (sizeof(struct stat_data))
779 #define SD_V2_SIZE                      SD_SIZE
780 #define stat_data_v2(ih)                (ih_version (ih) == KEY_FORMAT_3_6)
781 #define sd_v2_mode(sdp)                 (le16toh((sdp)->sd_mode))
782 #define set_sd_v2_mode(sdp, v)          ((sdp)->sd_mode = htole16(v))
783 /* sd_reserved */
784 /* set_sd_reserved */
785 #define sd_v2_nlink(sdp)                (le32toh((sdp)->sd_nlink))
786 #define set_sd_v2_nlink(sdp, v)         ((sdp)->sd_nlink = htole32(v))
787 #define sd_v2_size(sdp)                 (le64toh((sdp)->sd_size))
788 #define set_sd_v2_size(sdp, v)          ((sdp)->sd_size = cpu_to_le64(v))
789 #define sd_v2_uid(sdp)                  (le32toh((sdp)->sd_uid))
790 #define set_sd_v2_uid(sdp, v)           ((sdp)->sd_uid = htole32(v))
791 #define sd_v2_gid(sdp)                  (le32toh((sdp)->sd_gid))
792 #define set_sd_v2_gid(sdp, v)           ((sdp)->sd_gid = htole32(v))
793 #define sd_v2_atime(sdp)                (le32toh((sdp)->sd_atime))
794 #define set_sd_v2_atime(sdp, v)         ((sdp)->sd_atime = htole32(v))
795 #define sd_v2_mtime(sdp)                (le32toh((sdp)->sd_mtime))
796 #define set_sd_v2_mtime(sdp, v)         ((sdp)->sd_mtime = htole32(v))
797 #define sd_v2_ctime(sdp)                (le32toh((sdp)->sd_ctime))
798 #define set_sd_v2_ctime(sdp, v)         ((sdp)->sd_ctime = htole32(v))
799 #define sd_v2_blocks(sdp)               (le32toh((sdp)->sd_blocks))
800 #define set_sd_v2_blocks(sdp, v)        ((sdp)->sd_blocks = htole32(v))
801 #define sd_v2_rdev(sdp)                 (le32toh((sdp)->u.sd_rdev))
802 #define set_sd_v2_rdev(sdp, v)          ((sdp)->u.sd_rdev = htole32(v))
803 #define sd_v2_generation(sdp)           (le32toh((sdp)->u.sd_generation))
804 #define set_sd_v2_generation(sdp, v)    ((sdp)->u.sd_generation = htole32(v))
805 #define sd_v2_attrs(sdp)                (le16toh((sdp)->sd_attrs))
806 #define set_sd_v2_attrs(sdp, v)         ((sdp)->sd_attrs = htole16(v))
807
808 /* -------------------------------------------------------------------
809  * Directory structure
810  * -------------------------------------------------------------------*/
811
812 #define SD_OFFSET               0
813 #define SD_UNIQUENESS           0
814 #define DOT_OFFSET              1
815 #define DOT_DOT_OFFSET          2
816 #define DIRENTRY_UNIQUENESS     500
817
818 #define FIRST_ITEM_OFFSET       1
819
820 struct reiserfs_de_head {
821         uint32_t        deh_offset;    /* Third component of the directory
822                                           entry key */
823         uint32_t        deh_dir_id;    /* Objectid of the parent directory of
824                                           the object, that is referenced by
825                                           directory entry */
826         uint32_t        deh_objectid;  /* Objectid of the object, that is
827                                           referenced by directory entry */
828         uint16_t        deh_location;  /* Offset of name in the whole item */
829         uint16_t        deh_state;     /* Whether 1) entry contains stat data
830                                           (for future), and 2) whether entry
831                                           is hidden (unlinked) */
832 } __packed;
833
834 #define DEH_SIZE                        sizeof(struct reiserfs_de_head)
835 #define deh_offset(p_deh)               (le32toh((p_deh)->deh_offset))
836 #define deh_dir_id(p_deh)               (le32toh((p_deh)->deh_dir_id))
837 #define deh_objectid(p_deh)             (le32toh((p_deh)->deh_objectid))
838 #define deh_location(p_deh)             (le16toh((p_deh)->deh_location))
839 #define deh_state(p_deh)                (le16toh((p_deh)->deh_state))
840
841 #define put_deh_offset(p_deh, v)        ((p_deh)->deh_offset = htole32((v)))
842 #define put_deh_dir_id(p_deh, v)        ((p_deh)->deh_dir_id = htole32((v)))
843 #define put_deh_objectid(p_deh, v)      ((p_deh)->deh_objectid = htole32((v)))
844 #define put_deh_location(p_deh, v)      ((p_deh)->deh_location = htole16((v)))
845 #define put_deh_state(p_deh, v)         ((p_deh)->deh_state = htole16((v)))
846
847 /* Empty directory contains two entries "." and ".." and their headers */
848 #define EMPTY_DIR_SIZE                                                  \
849     (DEH_SIZE * 2 + ROUND_UP(strlen(".")) + ROUND_UP(strlen("..")))
850
851 /* Old format directories have this size when empty */
852 #define EMPTY_DIR_SIZE_V1       (DEH_SIZE * 2 + 3)
853
854 #define DEH_Statdata    0 /* Not used now */
855 #define DEH_Visible     2
856
857 /* Macro to map Linux' *_bit function to bitstring.h macros */
858 #define set_bit(bit, name)              bit_set((bitstr_t *)name, bit)
859 #define clear_bit(bit, name)            bit_clear((bitstr_t *)name, bit)
860 #define test_bit(bit, name)             bit_test((bitstr_t *)name, bit)
861
862 #define set_bit_unaligned(bit, name)    set_bit(bit, name)
863 #define clear_bit_unaligned(bit, name)  clear_bit(bit, name)
864 #define test_bit_unaligned(bit, name)   test_bit(bit, name)
865
866 #define mark_de_with_sd(deh)                                            \
867     set_bit_unaligned(DEH_Statdata, &((deh)->deh_state))
868 #define mark_de_without_sd(deh)                                         \
869     clear_bit_unaligned(DEH_Statdata, &((deh)->deh_state))
870 #define mark_de_visible(deh)                                            \
871     set_bit_unaligned (DEH_Visible, &((deh)->deh_state))
872 #define mark_de_hidden(deh)                                             \
873     clear_bit_unaligned (DEH_Visible, &((deh)->deh_state))
874
875 #define de_with_sd(deh)                                                 \
876     test_bit_unaligned(DEH_Statdata, &((deh)->deh_state))
877 #define de_visible(deh)                                                 \
878     test_bit_unaligned(DEH_Visible, &((deh)->deh_state))
879 #define de_hidden(deh)                                                  \
880     !test_bit_unaligned(DEH_Visible, &((deh)->deh_state))
881
882 /* Two entries per block (at least) */
883 #define REISERFS_MAX_NAME(block_size)   255
884
885 /*
886  * This structure is used for operations on directory entries. It is not
887  * a disk structure. When reiserfs_find_entry or search_by_entry_key
888  * find directory entry, they return filled reiserfs_dir_entry structure
889  */
890 struct reiserfs_dir_entry {
891         struct buf *de_bp;
892         int      de_item_num;
893         struct item_head *de_ih;
894         int      de_entry_num;
895         struct reiserfs_de_head *de_deh;
896         int      de_entrylen;
897         int      de_namelen;
898         char    *de_name;
899         char    *de_gen_number_bit_string;
900
901         uint32_t de_dir_id;
902         uint32_t de_objectid;
903
904         struct cpu_key de_entry_key;
905 };
906
907 /* Pointer to file name, stored in entry */
908 #define B_I_DEH_ENTRY_FILE_NAME(bp, ih, deh)                            \
909     (B_I_PITEM(bp, ih) + deh_location(deh))
910
911 /* Length of name */
912 #define I_DEH_N_ENTRY_FILE_NAME_LENGTH(ih, deh, entry_num)              \
913     (I_DEH_N_ENTRY_LENGTH(ih, deh, entry_num) -                         \
914      (de_with_sd(deh) ? SD_SIZE : 0))
915
916 /* Hash value occupies bits from 7 up to 30 */
917 #define GET_HASH_VALUE(offset)          ((offset) & 0x7fffff80LL)
918
919 /* Generation number occupies 7 bits starting from 0 up to 6 */
920 #define GET_GENERATION_NUMBER(offset)   ((offset) & 0x7fLL)
921 #define MAX_GENERATION_NUMBER           127
922
923 /* Get item body */
924 #define B_I_PITEM(bp, ih)       ((bp)->b_data + ih_location(ih))
925 #define B_I_DEH(bp, ih)         ((struct reiserfs_de_head *)(B_I_PITEM(bp, ih)))
926
927 /*
928  * Length of the directory entry in directory item. This define
929  * calculates length of i-th directory entry using directory entry
930  * locations from dir entry head. When it calculates length of 0-th
931  * directory entry, it uses length of whole item in place of entry
932  * location of the non-existent following entry in the calculation. See
933  * picture above.
934  */
935 static inline int
936 entry_length (const struct buf *bp, const struct item_head *ih,
937     int pos_in_item)
938 {
939         struct reiserfs_de_head *deh;
940
941         deh = B_I_DEH(bp, ih) + pos_in_item;
942         if (pos_in_item)
943                 return (deh_location(deh - 1) - deh_location(deh));
944
945         return (ih_item_len(ih) - deh_location(deh));
946 }
947
948 /*
949  * Number of entries in the directory item, depends on ENTRY_COUNT
950  * being at the start of directory dynamic data.
951  */
952 #define I_ENTRY_COUNT(ih)       (ih_entry_count((ih)))
953
954 /* -------------------------------------------------------------------
955  * Disk child
956  * -------------------------------------------------------------------*/
957
958 /*
959  * Disk child pointer: The pointer from an internal node of the tree
960  * to a node that is on disk.
961  */
962 struct disk_child {
963         uint32_t        dc_block_number; /* Disk child's block number. */
964         uint16_t        dc_size;         /* Disk child's used space. */
965         uint16_t        dc_reserved;
966 };
967
968 #define DC_SIZE                 (sizeof(struct disk_child))
969 #define dc_block_number(dc_p)   (le32toh((dc_p)->dc_block_number))
970 #define dc_size(dc_p)           (le16toh((dc_p)->dc_size))
971 #define put_dc_block_number(dc_p, val)                                  \
972     do { (dc_p)->dc_block_number = htole32(val); } while (0)
973 #define put_dc_size(dc_p, val)                                          \
974     do { (dc_p)->dc_size = htole16(val); } while (0)
975
976 /* Get disk child by buffer header and position in the tree node. */
977 #define B_N_CHILD(p_s_bp, n_pos)                                        \
978     ((struct disk_child *)((p_s_bp)->b_data + BLKH_SIZE +               \
979                            B_NR_ITEMS(p_s_bp) * KEY_SIZE +              \
980                            DC_SIZE * (n_pos)))
981
982 /* Get disk child number by buffer header and position in the tree node. */
983 #define B_N_CHILD_NUM(p_s_bp, n_pos)                                    \
984     (dc_block_number(B_N_CHILD(p_s_bp, n_pos)))
985 #define PUT_B_N_CHILD_NUM(p_s_bp, n_pos, val)                           \
986     (put_dc_block_number(B_N_CHILD(p_s_bp, n_pos), val))
987
988 /* -------------------------------------------------------------------
989  * Path structures and defines
990  * -------------------------------------------------------------------*/
991
992 struct path_element {
993         struct buf      *pe_buffer;  /* Pointer to the buffer at the path in
994                                         the tree. */
995         int             pe_position; /* Position in the tree node which is
996                                         placed in the buffer above. */
997 };
998
999 #define MAX_HEIGHT                      5 /* Maximal height of a tree. Don't
1000                                              change this without changing
1001                                              JOURNAL_PER_BALANCE_CNT */
1002 #define EXTENDED_MAX_HEIGHT             7 /* Must be equals MAX_HEIGHT +
1003                                              FIRST_PATH_ELEMENT_OFFSET */
1004 #define FIRST_PATH_ELEMENT_OFFSET       2 /* Must be equal to at least 2. */
1005 #define ILLEGAL_PATH_ELEMENT_OFFSET     1 /* Must be equal to
1006                                              FIRST_PATH_ELEMENT_OFFSET - 1 */
1007 #define MAX_FEB_SIZE                    6 /* This MUST be MAX_HEIGHT + 1.
1008                                              See about FEB below */
1009
1010 struct path {
1011         /* Length of the array below. */
1012         int     path_length;
1013         /* Array of the path element */
1014         struct path_element path_elements[EXTENDED_MAX_HEIGHT];
1015         int     pos_in_item;
1016 };
1017
1018 #define pos_in_item(path)       ((path)->pos_in_item)
1019
1020 #ifdef __amd64__
1021 /* To workaround a bug in gcc. He generates a call to memset() which
1022  * is a inline function; this causes a compile time error. */
1023 #define INITIALIZE_PATH(var)                                            \
1024     struct path var;                                                    \
1025     bzero(&var, sizeof(var));                                           \
1026     var.path_length = ILLEGAL_PATH_ELEMENT_OFFSET;
1027 #else
1028 #define INITIALIZE_PATH(var)                                            \
1029     struct path var = { ILLEGAL_PATH_ELEMENT_OFFSET, }
1030 #endif
1031
1032 /* Get path element by path and path position. */
1033 #define PATH_OFFSET_PELEMENT(p_s_path, n_offset)                        \
1034     ((p_s_path)->path_elements + (n_offset))
1035
1036 /* Get buffer header at the path by path and path position. */
1037 #define PATH_OFFSET_PBUFFER(p_s_path, n_offset)                         \
1038     (PATH_OFFSET_PELEMENT(p_s_path, n_offset)->pe_buffer)
1039
1040 /* Get position in the element at the path by path and path position. */
1041 #define PATH_OFFSET_POSITION(p_s_path, n_offset)                        \
1042     (PATH_OFFSET_PELEMENT(p_s_path, n_offset)->pe_position)
1043
1044 #define PATH_PLAST_BUFFER(p_s_path)                                     \
1045     (PATH_OFFSET_PBUFFER((p_s_path), (p_s_path)->path_length))
1046
1047 #define PATH_LAST_POSITION(p_s_path)                                    \
1048     (PATH_OFFSET_POSITION((p_s_path), (p_s_path)->path_length))
1049
1050 #define PATH_PITEM_HEAD(p_s_path)                                       \
1051     B_N_PITEM_HEAD(PATH_PLAST_BUFFER(p_s_path), PATH_LAST_POSITION(p_s_path))
1052
1053 #define get_last_bp(path)       PATH_PLAST_BUFFER(path)
1054 #define get_ih(path)            PATH_PITEM_HEAD(path)
1055
1056 /* -------------------------------------------------------------------
1057  * Misc.
1058  * -------------------------------------------------------------------*/
1059
1060 /* Size of pointer to the unformatted node. */
1061 #define UNFM_P_SIZE     (sizeof(unp_t))
1062 #define UNFM_P_SHIFT    2
1063
1064 /* In in-core inode key is stored on le form */
1065 #define INODE_PKEY(ip)  ((struct key *)(REISERFS_I(ip)->i_key))
1066
1067 #define MAX_UL_INT      0xffffffff
1068 #define MAX_INT         0x7ffffff
1069 #define MAX_US_INT      0xffff
1070
1071 /* The purpose is to detect overflow of an unsigned short */
1072 #define REISERFS_LINK_MAX       (MAX_US_INT - 1000)
1073
1074 #define fs_generation(sbi)      (REISERFS_SB(sbi)->s_generation_counter)
1075 #define get_generation(sbi)     (fs_generation(sbi))
1076
1077 #define __fs_changed(gen, sbi)  (gen != get_generation (sbi))
1078 /*#define       fs_changed(gen, sbi)    ({ cond_resched();              \
1079     __fs_changed(gen, sbi); })*/
1080 #define fs_changed(gen, sbi)    (__fs_changed(gen, sbi))
1081
1082 /* -------------------------------------------------------------------
1083  * Fixate node
1084  * -------------------------------------------------------------------*/
1085
1086 /*
1087  * To make any changes in the tree we always first find node, that
1088  * contains item to be changed/deleted or place to insert a new item.
1089  * We call this node S. To do balancing we need to decide what we will
1090  * shift to left/right neighbor, or to a new node, where new item will
1091  * be etc. To make this analysis simpler we build virtual node. Virtual
1092  * node is an array of items, that will replace items of node S. (For
1093  * instance if we are going to delete an item, virtual node does not
1094  * contain it). Virtual node keeps information about item sizes and
1095  * types, mergeability of first and last items, sizes of all entries in
1096  * directory item. We use this array of items when calculating what we
1097  * can shift to neighbors and how many nodes we have to have if we do
1098  * not any shiftings, if we shift to left/right neighbor or to both.
1099  */
1100 struct virtual_item {
1101         int                      vi_index;    /* Index in the array of item
1102                                                  operations */
1103         unsigned short           vi_type;     /* Left/right mergeability */
1104         unsigned short           vi_item_len; /* Length of item that it will
1105                                                  have after balancing */
1106         struct item_head        *vi_ih;
1107         const char              *vi_item;     /* Body of item (old or new) */
1108         const void              *vi_new_data; /* 0 always but paste mode */
1109         void                    *vi_uarea;    /* Item specific area */
1110 };
1111
1112 struct virtual_node {
1113         char            *vn_free_ptr; /* This is a pointer to the free space
1114                                          in the buffer */
1115         unsigned short   vn_nr_item;  /* Number of items in virtual node */
1116         short            vn_size;     /* Size of node , that node would have
1117                                          if it has unlimited size and no
1118                                          balancing is performed */
1119         short            vn_mode;     /* Mode of balancing (paste, insert,
1120                                          delete, cut) */
1121         short            vn_affected_item_num;
1122         short            vn_pos_in_item;
1123         struct item_head *vn_ins_ih;  /* Item header of inserted item, 0 for
1124                                          other modes */
1125         const void      *vn_data;
1126         struct virtual_item *vn_vi;   /* Array of items (including a new one,
1127                                          excluding item to be deleted) */
1128 };
1129
1130 /* Used by directory items when creating virtual nodes */
1131 struct direntry_uarea {
1132         int             flags;
1133         uint16_t        entry_count;
1134         uint16_t        entry_sizes[1];
1135 } __packed;
1136
1137 /* -------------------------------------------------------------------
1138  * Tree balance
1139  * -------------------------------------------------------------------*/
1140
1141 struct reiserfs_iget_args {
1142         uint32_t        objectid;
1143         uint32_t        dirid;
1144 };
1145
1146 struct item_operations {
1147         int     (*bytes_number)(struct item_head * ih, int block_size);
1148         void    (*decrement_key)(struct cpu_key *);
1149         int     (*is_left_mergeable)(struct key * ih, unsigned long bsize);
1150         void    (*print_item)(struct item_head *, char * item);
1151         void    (*check_item)(struct item_head *, char * item);
1152
1153         int     (*create_vi)(struct virtual_node * vn,
1154             struct virtual_item * vi, int is_affected, int insert_size);
1155         int     (*check_left)(struct virtual_item * vi, int free,
1156             int start_skip, int end_skip);
1157         int     (*check_right)(struct virtual_item * vi, int free);
1158         int     (*part_size)(struct virtual_item * vi, int from, int to);
1159         int     (*unit_num)(struct virtual_item * vi);
1160         void    (*print_vi)(struct virtual_item * vi);
1161 };
1162
1163 extern struct item_operations *item_ops[TYPE_ANY + 1];
1164
1165 #define op_bytes_number(ih, bsize)                                      \
1166     item_ops[le_ih_k_type(ih)]->bytes_number(ih, bsize)
1167
1168 #define COMP_KEYS       comp_keys
1169 #define COMP_SHORT_KEYS comp_short_keys
1170
1171 /* Get the item header */
1172 #define B_N_PITEM_HEAD(bp, item_num)                                    \
1173     ((struct item_head *)((bp)->b_data + BLKH_SIZE) + (item_num))
1174
1175 /* Get key */
1176 #define B_N_PDELIM_KEY(bp, item_num)                                    \
1177     ((struct key *)((bp)->b_data + BLKH_SIZE) + (item_num))
1178
1179 /* -------------------------------------------------------------------
1180  * Function declarations
1181  * -------------------------------------------------------------------*/
1182
1183 /* reiserfs_stree.c */
1184 int     B_IS_IN_TREE(const struct buf *p_s_bp);
1185
1186 extern void     copy_item_head(struct item_head * p_v_to,
1187                     const struct item_head * p_v_from);
1188
1189 extern int      comp_keys(const struct key *le_key,
1190                     const struct cpu_key *cpu_key);
1191 extern int      comp_short_keys(const struct key *le_key,
1192                     const struct cpu_key *cpu_key);
1193
1194 extern int      comp_le_keys(const struct key *, const struct key *);
1195
1196 static inline int
1197 le_key_version(const struct key *key)
1198 {
1199         int type;
1200
1201         type = offset_v2_k_type(&(key->u.k_offset_v2));
1202         if (type != TYPE_DIRECT && type != TYPE_INDIRECT &&
1203             type != TYPE_DIRENTRY)
1204                 return (KEY_FORMAT_3_5);
1205
1206         return (KEY_FORMAT_3_6);
1207 }
1208
1209 static inline void
1210 copy_key(struct key *to, const struct key *from)
1211 {
1212
1213         memcpy(to, from, KEY_SIZE);
1214 }
1215
1216 const struct key        *get_lkey(const struct path *p_s_chk_path,
1217                             const struct reiserfs_sb_info *p_s_sbi);
1218 const struct key        *get_rkey(const struct path *p_s_chk_path,
1219                             const struct reiserfs_sb_info *p_s_sbi);
1220 int     bin_search(const void * p_v_key, const void * p_v_base,
1221                     int p_n_num, int p_n_width, int * p_n_pos);
1222
1223 void    pathrelse(struct path *p_s_search_path);
1224 int     reiserfs_check_path(struct path *p);
1225
1226 int     search_by_key(struct reiserfs_sb_info *p_s_sbi,
1227             const struct cpu_key *p_s_key,
1228             struct path *p_s_search_path,
1229             int n_stop_level);
1230 #define search_item(sbi, key, path)                                     \
1231     search_by_key(sbi, key, path, DISK_LEAF_NODE_LEVEL)
1232 int     search_for_position_by_key(struct reiserfs_sb_info *p_s_sbi,
1233             const struct cpu_key *p_s_cpu_key,
1234             struct path *p_s_search_path);
1235 void    decrement_counters_in_path(struct path *p_s_search_path);
1236
1237 /* reiserfs_inode.c */
1238 vop_read_t      reiserfs_read;
1239 vop_inactive_t  reiserfs_inactive;
1240 vop_reclaim_t   reiserfs_reclaim;
1241
1242 int     reiserfs_get_block(struct reiserfs_node *ip, long block,
1243             off_t offset, struct uio *uio);
1244
1245 void    make_cpu_key(struct cpu_key *cpu_key, struct reiserfs_node *ip,
1246             off_t offset, int type, int key_length);
1247
1248 void    reiserfs_read_locked_inode(struct reiserfs_node *ip,
1249             struct reiserfs_iget_args *args);
1250 int     reiserfs_iget(struct mount *mp, const struct cpu_key *key,
1251             struct vnode **vpp, struct thread *td);
1252
1253 void    sd_attrs_to_i_attrs(uint16_t sd_attrs, struct reiserfs_node *ip);
1254 void    i_attrs_to_sd_attrs(struct reiserfs_node *ip, uint16_t *sd_attrs);
1255
1256 /* reiserfs_namei.c */
1257 vop_readdir_t           reiserfs_readdir;
1258 vop_cachedlookup_t      reiserfs_lookup;
1259
1260 void    set_de_name_and_namelen(struct reiserfs_dir_entry * de);
1261 int     search_by_entry_key(struct reiserfs_sb_info *sbi,
1262             const struct cpu_key *key, struct path *path,
1263             struct reiserfs_dir_entry *de);
1264
1265 /* reiserfs_prints.c */
1266 char    *reiserfs_hashname(int code);
1267 void     reiserfs_dump_buffer(caddr_t buf, off_t len);
1268
1269 #if defined(REISERFS_DEBUG)
1270 #define reiserfs_log(lvl, fmt, ...)                                     \
1271     log(lvl, "ReiserFS/%s: " fmt, __func__, ## __VA_ARGS__)
1272 #elif defined (REISERFS_DEBUG_CONS)
1273 #define reiserfs_log(lvl, fmt, ...)                                     \
1274     printf("%s:%d: " fmt, __func__, __LINE__, ## __VA_ARGS__)
1275 #else
1276 #define reiserfs_log(lvl, fmt, ...)
1277 #endif
1278
1279 #define reiserfs_log_0(lvl, fmt, ...)                                   \
1280     printf("%s:%d: " fmt, __func__, __LINE__, ## __VA_ARGS__)
1281
1282 /* reiserfs_hashes.c */
1283 uint32_t        keyed_hash(const signed char *msg, int len);
1284 uint32_t        yura_hash(const signed char *msg, int len);
1285 uint32_t        r5_hash(const signed char *msg, int len);
1286
1287 #define reiserfs_test_le_bit  test_bit
1288
1289 #endif /* !defined _GNU_REISERFS_REISERFS_FS_H */