]> code.delx.au - refind/blob - filesystems/fsw_reiserfs_disk.h
Modified SIP/CSR feature to work ON MACS when the csr-active-config
[refind] / filesystems / fsw_reiserfs_disk.h
1 /**
2 * \file fsw_reiserfs_disk.h
3 * ReiserFS file system on-disk structures.
4 */
5
6 /*-
7 * Copyright (c) 2006 Christoph Pfisterer
8 * Portions Copyright (c) 1991-2006 by various Linux kernel contributors
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24
25 #ifndef _FSW_REISERFS_DISK_H_
26 #define _FSW_REISERFS_DISK_H_
27
28 // types
29
30 typedef fsw_s8 __s8;
31 typedef fsw_u8 __u8;
32 typedef fsw_s16 __s16;
33 typedef fsw_u16 __u16;
34 typedef fsw_s32 __s32;
35 typedef fsw_u32 __u32;
36 typedef fsw_s64 __s64;
37 typedef fsw_u64 __u64;
38
39 typedef __u16 __le16;
40 typedef __u32 __le32;
41 typedef __u64 __le64;
42
43 #define le16_to_cpu(x) (x)
44 #define cpu_to_le16(x) (x)
45 #define le32_to_cpu(x) (x)
46 #define cpu_to_le32(x) (x)
47 #define le64_to_cpu(x) (x)
48 #define cpu_to_le64(x) (x)
49
50 #ifdef __GCC__
51 #define ATTR_PACKED __attribute__ ((__packed__))
52 #else
53 #define ATTR_PACKED
54 #endif
55
56 #pragma pack(1)
57
58 //
59 // from Linux kernel, include/linux/reiserfs_fs.h
60 //
61
62
63
64 /*
65 * Disk Data Structures
66 */
67
68 /***************************************************************************/
69 /* SUPER BLOCK */
70 /***************************************************************************/
71
72 /*
73 * Structure of super block on disk, a version of which in RAM is often accessed as REISERFS_SB(s)->s_rs
74 * the version in RAM is part of a larger structure containing fields never written to disk.
75 */
76 #define UNSET_HASH 0 // read_super will guess about, what hash names
77 // in directories were sorted with
78 #define TEA_HASH 1
79 #define YURA_HASH 2
80 #define R5_HASH 3
81 #define DEFAULT_HASH R5_HASH
82
83 struct journal_params {
84 __le32 jp_journal_1st_block; /* where does journal start from on its
85 * device */
86 __le32 jp_journal_dev; /* journal device st_rdev */
87 __le32 jp_journal_size; /* size of the journal */
88 __le32 jp_journal_trans_max; /* max number of blocks in a transaction. */
89 __le32 jp_journal_magic; /* random value made on fs creation (this
90 * was sb_journal_block_count) */
91 __le32 jp_journal_max_batch; /* max number of blocks to batch into a
92 * trans */
93 __le32 jp_journal_max_commit_age; /* in seconds, how old can an async
94 * commit be */
95 __le32 jp_journal_max_trans_age; /* in seconds, how old can a transaction
96 * be */
97 };
98
99 /* this is the super from 3.5.X, where X >= 10 */
100 struct reiserfs_super_block_v1 {
101 __le32 s_block_count; /* blocks count */
102 __le32 s_free_blocks; /* free blocks count */
103 __le32 s_root_block; /* root block number */
104 struct journal_params s_journal;
105 __le16 s_blocksize; /* block size */
106 __le16 s_oid_maxsize; /* max size of object id array, see
107 * get_objectid() commentary */
108 __le16 s_oid_cursize; /* current size of object id array */
109 __le16 s_umount_state; /* this is set to 1 when filesystem was
110 * umounted, to 2 - when not */
111 char s_magic[10]; /* reiserfs magic string indicates that
112 * file system is reiserfs:
113 * "ReIsErFs" or "ReIsEr2Fs" or "ReIsEr3Fs" */
114 __le16 s_fs_state; /* it is set to used by fsck to mark which
115 * phase of rebuilding is done */
116 __le32 s_hash_function_code; /* indicate, what hash function is being use
117 * to sort names in a directory*/
118 __le16 s_tree_height; /* height of disk tree */
119 __le16 s_bmap_nr; /* amount of bitmap blocks needed to address
120 * each block of file system */
121 __le16 s_version; /* this field is only reliable on filesystem
122 * with non-standard journal */
123 __le16 s_reserved_for_journal; /* size in blocks of journal area on main
124 * device, we need to keep after
125 * making fs with non-standard journal */
126 } ATTR_PACKED;
127
128 #define SB_SIZE_V1 (sizeof(struct reiserfs_super_block_v1))
129
130 /* this is the on disk super block */
131 struct reiserfs_super_block {
132 struct reiserfs_super_block_v1 s_v1;
133 __le32 s_inode_generation;
134 __le32 s_flags; /* Right now used only by inode-attributes, if enabled */
135 unsigned char s_uuid[16]; /* filesystem unique identifier */
136 unsigned char s_label[16]; /* filesystem volume label */
137 char s_unused[88]; /* zero filled by mkreiserfs and
138 * reiserfs_convert_objectid_map_v1()
139 * so any additions must be updated
140 * there as well. */
141 } ATTR_PACKED;
142
143 #define SB_SIZE (sizeof(struct reiserfs_super_block))
144
145 #define REISERFS_VERSION_1 0
146 #define REISERFS_VERSION_2 2
147
148 // on-disk super block fields converted to cpu form
149 #define SB_DISK_SUPER_BLOCK(s) (REISERFS_SB(s)->s_rs)
150 #define SB_V1_DISK_SUPER_BLOCK(s) (&(SB_DISK_SUPER_BLOCK(s)->s_v1))
151 #define SB_BLOCKSIZE(s) \
152 le32_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_blocksize))
153 #define SB_BLOCK_COUNT(s) \
154 le32_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_block_count))
155 #define SB_FREE_BLOCKS(s) \
156 le32_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_free_blocks))
157 #define SB_REISERFS_MAGIC(s) \
158 (SB_V1_DISK_SUPER_BLOCK(s)->s_magic)
159 #define SB_ROOT_BLOCK(s) \
160 le32_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_root_block))
161 #define SB_TREE_HEIGHT(s) \
162 le16_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_tree_height))
163 #define SB_REISERFS_STATE(s) \
164 le16_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_umount_state))
165 #define SB_VERSION(s) le16_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_version))
166 #define SB_BMAP_NR(s) le16_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_bmap_nr))
167
168 #define SB_ONDISK_JP(s) (&SB_V1_DISK_SUPER_BLOCK(s)->s_journal)
169 #define SB_ONDISK_JOURNAL_SIZE(s) \
170 le32_to_cpu ((SB_ONDISK_JP(s)->jp_journal_size))
171 #define SB_ONDISK_JOURNAL_1st_BLOCK(s) \
172 le32_to_cpu ((SB_ONDISK_JP(s)->jp_journal_1st_block))
173 #define SB_ONDISK_JOURNAL_DEVICE(s) \
174 le32_to_cpu ((SB_ONDISK_JP(s)->jp_journal_dev))
175 #define SB_ONDISK_RESERVED_FOR_JOURNAL(s) \
176 le16_to_cpu ((SB_V1_DISK_SUPER_BLOCK(s)->s_reserved_for_journal))
177
178 #define is_block_in_log_or_reserved_area(s, block) \
179 block >= SB_JOURNAL_1st_RESERVED_BLOCK(s) \
180 && block < SB_JOURNAL_1st_RESERVED_BLOCK(s) + \
181 ((!is_reiserfs_jr(SB_DISK_SUPER_BLOCK(s)) ? \
182 SB_ONDISK_JOURNAL_SIZE(s) + 1 : SB_ONDISK_RESERVED_FOR_JOURNAL(s)))
183
184 /* used by gcc */
185 #define REISERFS_SUPER_MAGIC 0x52654973
186 /* used by file system utilities that
187 look at the superblock, etc. */
188 #define REISERFS_SUPER_MAGIC_STRING "ReIsErFs"
189 #define REISER2FS_SUPER_MAGIC_STRING "ReIsEr2Fs"
190 #define REISER2FS_JR_SUPER_MAGIC_STRING "ReIsEr3Fs"
191
192 /* ReiserFS leaves the first 64k unused, so that partition labels have
193 enough space. If someone wants to write a fancy bootloader that
194 needs more than 64k, let us know, and this will be increased in size.
195 This number must be larger than than the largest block size on any
196 platform, or code will break. -Hans */
197 #define REISERFS_DISK_OFFSET_IN_BYTES (64 * 1024)
198 #define REISERFS_FIRST_BLOCK unused_define
199 #define REISERFS_JOURNAL_OFFSET_IN_BYTES REISERFS_DISK_OFFSET_IN_BYTES
200
201 /* the spot for the super in versions 3.5 - 3.5.10 (inclusive) */
202 #define REISERFS_OLD_DISK_OFFSET_IN_BYTES (8 * 1024)
203
204 // reiserfs internal error code (used by search_by_key adn fix_nodes))
205 #define CARRY_ON 0
206 #define REPEAT_SEARCH -1
207 #define IO_ERROR -2
208 #define NO_DISK_SPACE -3
209 #define NO_BALANCING_NEEDED (-4)
210 #define NO_MORE_UNUSED_CONTIGUOUS_BLOCKS (-5)
211 #define QUOTA_EXCEEDED -6
212
213 typedef __u32 b_blocknr_t;
214 typedef __le32 unp_t;
215
216 struct unfm_nodeinfo {
217 unp_t unfm_nodenum;
218 unsigned short unfm_freespace;
219 };
220
221 /* there are two formats of keys: 3.5 and 3.6
222 */
223 #define KEY_FORMAT_3_5 0
224 #define KEY_FORMAT_3_6 1
225
226 /* there are two stat datas */
227 #define STAT_DATA_V1 0
228 #define STAT_DATA_V2 1
229
230
231
232 /** this says about version of key of all items (but stat data) the
233 object consists of */
234 #define get_inode_item_key_version( inode ) \
235 ((REISERFS_I(inode)->i_flags & i_item_key_version_mask) ? KEY_FORMAT_3_6 : KEY_FORMAT_3_5)
236
237 #define set_inode_item_key_version( inode, version ) \
238 ({ if((version)==KEY_FORMAT_3_6) \
239 REISERFS_I(inode)->i_flags |= i_item_key_version_mask; \
240 else \
241 REISERFS_I(inode)->i_flags &= ~i_item_key_version_mask; })
242
243 #define get_inode_sd_version(inode) \
244 ((REISERFS_I(inode)->i_flags & i_stat_data_version_mask) ? STAT_DATA_V2 : STAT_DATA_V1)
245
246 #define set_inode_sd_version(inode, version) \
247 ({ if((version)==STAT_DATA_V2) \
248 REISERFS_I(inode)->i_flags |= i_stat_data_version_mask; \
249 else \
250 REISERFS_I(inode)->i_flags &= ~i_stat_data_version_mask; })
251
252 /* This is an aggressive tail suppression policy, I am hoping it
253 improves our benchmarks. The principle behind it is that percentage
254 space saving is what matters, not absolute space saving. This is
255 non-intuitive, but it helps to understand it if you consider that the
256 cost to access 4 blocks is not much more than the cost to access 1
257 block, if you have to do a seek and rotate. A tail risks a
258 non-linear disk access that is significant as a percentage of total
259 time cost for a 4 block file and saves an amount of space that is
260 less significant as a percentage of space, or so goes the hypothesis.
261 -Hans */
262 #define STORE_TAIL_IN_UNFM_S1(n_file_size,n_tail_size,n_block_size) \
263 (\
264 (!(n_tail_size)) || \
265 (((n_tail_size) > MAX_DIRECT_ITEM_LEN(n_block_size)) || \
266 ( (n_file_size) >= (n_block_size) * 4 ) || \
267 ( ( (n_file_size) >= (n_block_size) * 3 ) && \
268 ( (n_tail_size) >= (MAX_DIRECT_ITEM_LEN(n_block_size))/4) ) || \
269 ( ( (n_file_size) >= (n_block_size) * 2 ) && \
270 ( (n_tail_size) >= (MAX_DIRECT_ITEM_LEN(n_block_size))/2) ) || \
271 ( ( (n_file_size) >= (n_block_size) ) && \
272 ( (n_tail_size) >= (MAX_DIRECT_ITEM_LEN(n_block_size) * 3)/4) ) ) \
273 )
274
275 /* Another strategy for tails, this one means only create a tail if all the
276 file would fit into one DIRECT item.
277 Primary intention for this one is to increase performance by decreasing
278 seeking.
279 */
280 #define STORE_TAIL_IN_UNFM_S2(n_file_size,n_tail_size,n_block_size) \
281 (\
282 (!(n_tail_size)) || \
283 (((n_file_size) > MAX_DIRECT_ITEM_LEN(n_block_size)) ) \
284 )
285
286 /*
287 * values for s_umount_state field
288 */
289 #define REISERFS_VALID_FS 1
290 #define REISERFS_ERROR_FS 2
291
292 //
293 // there are 5 item types currently
294 //
295 #define TYPE_STAT_DATA 0
296 #define TYPE_INDIRECT 1
297 #define TYPE_DIRECT 2
298 #define TYPE_DIRENTRY 3
299 #define TYPE_MAXTYPE 3
300 #define TYPE_ANY 15 // FIXME: comment is required
301
302 /***************************************************************************/
303 /* KEY & ITEM HEAD */
304 /***************************************************************************/
305
306 //
307 // directories use this key as well as old files
308 //
309 struct offset_v1 {
310 __le32 k_offset;
311 __le32 k_uniqueness;
312 } ATTR_PACKED;
313
314 struct offset_v2 {
315 __le64 v;
316 } ATTR_PACKED;
317
318 /*
319 static inline __u16 offset_v2_k_type(const struct offset_v2 *v2)
320 {
321 __u8 type = le64_to_cpu(v2->v) >> 60;
322 return (type <= TYPE_MAXTYPE) ? type : TYPE_ANY;
323 }
324
325 static inline loff_t offset_v2_k_offset(const struct offset_v2 *v2)
326 {
327 return le64_to_cpu(v2->v) & (~0ULL >> 4);
328 }
329 */
330
331 /* Key of an item determines its location in the S+tree, and
332 is composed of 4 components */
333 struct reiserfs_key {
334 __le32 k_dir_id; /* packing locality: by default parent
335 directory object id */
336 __le32 k_objectid; /* object identifier */
337 union {
338 struct offset_v1 k_offset_v1;
339 struct offset_v2 k_offset_v2;
340 } ATTR_PACKED u;
341 } ATTR_PACKED;
342
343 struct in_core_key {
344 __u32 k_dir_id; /* packing locality: by default parent
345 directory object id */
346 __u32 k_objectid; /* object identifier */
347 __u64 k_offset;
348 __u8 k_type;
349 };
350
351 struct cpu_key {
352 struct in_core_key on_disk_key;
353 int version;
354 int key_length; /* 3 in all cases but direct2indirect and
355 indirect2direct conversion */
356 };
357
358 /* Our function for comparing keys can compare keys of different
359 lengths. It takes as a parameter the length of the keys it is to
360 compare. These defines are used in determining what is to be passed
361 to it as that parameter. */
362 #define REISERFS_FULL_KEY_LEN 4
363 #define REISERFS_SHORT_KEY_LEN 2
364
365 /* The result of the key compare */
366 #define FIRST_GREATER 1
367 #define SECOND_GREATER -1
368 #define KEYS_IDENTICAL 0
369 #define KEY_FOUND 1
370 #define KEY_NOT_FOUND 0
371
372 #define KEY_SIZE (sizeof(struct reiserfs_key))
373 #define SHORT_KEY_SIZE (sizeof (__u32) + sizeof (__u32))
374
375 /* return values for search_by_key and clones */
376 #define ITEM_FOUND 1
377 #define ITEM_NOT_FOUND 0
378 #define ENTRY_FOUND 1
379 #define ENTRY_NOT_FOUND 0
380 #define DIRECTORY_NOT_FOUND -1
381 #define REGULAR_FILE_FOUND -2
382 #define DIRECTORY_FOUND -3
383 #define BYTE_FOUND 1
384 #define BYTE_NOT_FOUND 0
385 #define FILE_NOT_FOUND -1
386
387 #define POSITION_FOUND 1
388 #define POSITION_NOT_FOUND 0
389
390 // return values for reiserfs_find_entry and search_by_entry_key
391 #define NAME_FOUND 1
392 #define NAME_NOT_FOUND 0
393 #define GOTO_PREVIOUS_ITEM 2
394 #define NAME_FOUND_INVISIBLE 3
395
396 /* Everything in the filesystem is stored as a set of items. The
397 item head contains the key of the item, its free space (for
398 indirect items) and specifies the location of the item itself
399 within the block. */
400
401 struct item_head {
402 /* Everything in the tree is found by searching for it based on
403 * its key.*/
404 struct reiserfs_key ih_key;
405 union {
406 /* The free space in the last unformatted node of an
407 indirect item if this is an indirect item. This
408 equals 0xFFFF iff this is a direct item or stat data
409 item. Note that the key, not this field, is used to
410 determine the item type, and thus which field this
411 union contains. */
412 __le16 ih_free_space_reserved;
413 /* Iff this is a directory item, this field equals the
414 number of directory entries in the directory item. */
415 __le16 ih_entry_count;
416 } ATTR_PACKED u;
417 __le16 ih_item_len; /* total size of the item body */
418 __le16 ih_item_location; /* an offset to the item body
419 * within the block */
420 __le16 ih_version; /* 0 for all old items, 2 for new
421 ones. Highest bit is set by fsck
422 temporary, cleaned after all
423 done */
424 } ATTR_PACKED;
425 /* size of item header */
426 #define IH_SIZE (sizeof(struct item_head))
427
428 #define ih_free_space(ih) le16_to_cpu((ih)->u.ih_free_space_reserved)
429 #define ih_version(ih) le16_to_cpu((ih)->ih_version)
430 #define ih_entry_count(ih) le16_to_cpu((ih)->u.ih_entry_count)
431 #define ih_location(ih) le16_to_cpu((ih)->ih_item_location)
432 #define ih_item_len(ih) le16_to_cpu((ih)->ih_item_len)
433
434 #define unreachable_item(ih) (ih_version(ih) & (1 << 15))
435
436 #define get_ih_free_space(ih) (ih_version (ih) == KEY_FORMAT_3_6 ? 0 : ih_free_space (ih))
437
438 /* these operate on indirect items, where you've got an array of ints
439 ** at a possibly unaligned location. These are a noop on ia32
440 **
441 ** p is the array of __u32, i is the index into the array, v is the value
442 ** to store there.
443 */
444 #define get_block_num(p, i) le32_to_cpu(get_unaligned((p) + (i)))
445
446 //
447 // in old version uniqueness field shows key type
448 //
449 #define V1_SD_UNIQUENESS 0
450 #define V1_INDIRECT_UNIQUENESS 0xfffffffe
451 #define V1_DIRECT_UNIQUENESS 0xffffffff
452 #define V1_DIRENTRY_UNIQUENESS 500
453 #define V1_ANY_UNIQUENESS 555 // FIXME: comment is required
454
455 //
456 // here are conversion routines
457 //
458 /*
459 static inline int uniqueness2type(__u32 uniqueness)
460 {
461 switch ((int)uniqueness) {
462 case V1_SD_UNIQUENESS:
463 return TYPE_STAT_DATA;
464 case V1_INDIRECT_UNIQUENESS:
465 return TYPE_INDIRECT;
466 case V1_DIRECT_UNIQUENESS:
467 return TYPE_DIRECT;
468 case V1_DIRENTRY_UNIQUENESS:
469 return TYPE_DIRENTRY;
470 default:
471 reiserfs_warning(NULL, "vs-500: unknown uniqueness %d",
472 uniqueness);
473 case V1_ANY_UNIQUENESS:
474 return TYPE_ANY;
475 }
476 }
477
478 static inline __u32 type2uniqueness(int type)
479 {
480 switch (type) {
481 case TYPE_STAT_DATA:
482 return V1_SD_UNIQUENESS;
483 case TYPE_INDIRECT:
484 return V1_INDIRECT_UNIQUENESS;
485 case TYPE_DIRECT:
486 return V1_DIRECT_UNIQUENESS;
487 case TYPE_DIRENTRY:
488 return V1_DIRENTRY_UNIQUENESS;
489 default:
490 reiserfs_warning(NULL, "vs-501: unknown type %d", type);
491 case TYPE_ANY:
492 return V1_ANY_UNIQUENESS;
493 }
494 }
495 */
496
497 //
498 // key is pointer to on disk key which is stored in le, result is cpu,
499 // there is no way to get version of object from key, so, provide
500 // version to these defines
501 //
502 /*
503 static inline loff_t le_key_k_offset(int version,
504 const struct reiserfs_key *key)
505 {
506 return (version == KEY_FORMAT_3_5) ?
507 le32_to_cpu(key->u.k_offset_v1.k_offset) :
508 offset_v2_k_offset(&(key->u.k_offset_v2));
509 }
510
511 static inline loff_t le_ih_k_offset(const struct item_head *ih)
512 {
513 return le_key_k_offset(ih_version(ih), &(ih->ih_key));
514 }
515
516 static inline loff_t le_key_k_type(int version, const struct reiserfs_key *key)
517 {
518 return (version == KEY_FORMAT_3_5) ?
519 uniqueness2type(le32_to_cpu(key->u.k_offset_v1.k_uniqueness)) :
520 offset_v2_k_type(&(key->u.k_offset_v2));
521 }
522
523 static inline loff_t le_ih_k_type(const struct item_head *ih)
524 {
525 return le_key_k_type(ih_version(ih), &(ih->ih_key));
526 }
527 */
528
529 #define is_direntry_le_key(version,key) (le_key_k_type (version, key) == TYPE_DIRENTRY)
530 #define is_direct_le_key(version,key) (le_key_k_type (version, key) == TYPE_DIRECT)
531 #define is_indirect_le_key(version,key) (le_key_k_type (version, key) == TYPE_INDIRECT)
532 #define is_statdata_le_key(version,key) (le_key_k_type (version, key) == TYPE_STAT_DATA)
533
534 //
535 // item header has version.
536 //
537 #define is_direntry_le_ih(ih) is_direntry_le_key (ih_version (ih), &((ih)->ih_key))
538 #define is_direct_le_ih(ih) is_direct_le_key (ih_version (ih), &((ih)->ih_key))
539 #define is_indirect_le_ih(ih) is_indirect_le_key (ih_version(ih), &((ih)->ih_key))
540 #define is_statdata_le_ih(ih) is_statdata_le_key (ih_version (ih), &((ih)->ih_key))
541
542 //
543 // key is pointer to cpu key, result is cpu
544 //
545 /*
546 static inline loff_t cpu_key_k_offset(const struct cpu_key *key)
547 {
548 return key->on_disk_key.k_offset;
549 }
550
551 static inline loff_t cpu_key_k_type(const struct cpu_key *key)
552 {
553 return key->on_disk_key.k_type;
554 }
555
556 static inline void cpu_key_k_offset_dec(struct cpu_key *key)
557 {
558 key->on_disk_key.k_offset--;
559 }
560 */
561
562 #define is_direntry_cpu_key(key) (cpu_key_k_type (key) == TYPE_DIRENTRY)
563 #define is_direct_cpu_key(key) (cpu_key_k_type (key) == TYPE_DIRECT)
564 #define is_indirect_cpu_key(key) (cpu_key_k_type (key) == TYPE_INDIRECT)
565 #define is_statdata_cpu_key(key) (cpu_key_k_type (key) == TYPE_STAT_DATA)
566
567 /* are these used ? */
568 #define is_direntry_cpu_ih(ih) (is_direntry_cpu_key (&((ih)->ih_key)))
569 #define is_direct_cpu_ih(ih) (is_direct_cpu_key (&((ih)->ih_key)))
570 #define is_indirect_cpu_ih(ih) (is_indirect_cpu_key (&((ih)->ih_key)))
571 #define is_statdata_cpu_ih(ih) (is_statdata_cpu_key (&((ih)->ih_key)))
572
573 #define I_K_KEY_IN_ITEM(p_s_ih, p_s_key, n_blocksize) \
574 ( ! COMP_SHORT_KEYS(p_s_ih, p_s_key) && \
575 I_OFF_BYTE_IN_ITEM(p_s_ih, k_offset (p_s_key), n_blocksize) )
576
577 /* maximal length of item */
578 #define MAX_ITEM_LEN(block_size) (block_size - BLKH_SIZE - IH_SIZE)
579 #define MIN_ITEM_LEN 1
580
581 /* object identifier for root dir */
582 #define REISERFS_ROOT_OBJECTID 2
583 #define REISERFS_ROOT_PARENT_OBJECTID 1
584
585 /*
586 * Picture represents a leaf of the S+tree
587 * ______________________________________________________
588 * | | Array of | | |
589 * |Block | Object-Item | F r e e | Objects- |
590 * | head | Headers | S p a c e | Items |
591 * |______|_______________|___________________|___________|
592 */
593
594 /* Header of a disk block. More precisely, header of a formatted leaf
595 or internal node, and not the header of an unformatted node. */
596 struct block_head {
597 __le16 blk_level; /* Level of a block in the tree. */
598 __le16 blk_nr_item; /* Number of keys/items in a block. */
599 __le16 blk_free_space; /* Block free space in bytes. */
600 __le16 blk_reserved;
601 /* dump this in v4/planA */
602 struct reiserfs_key blk_right_delim_key; /* kept only for compatibility */
603 };
604
605 #define BLKH_SIZE (sizeof(struct block_head))
606 #define blkh_level(p_blkh) (le16_to_cpu((p_blkh)->blk_level))
607 #define blkh_nr_item(p_blkh) (le16_to_cpu((p_blkh)->blk_nr_item))
608 #define blkh_free_space(p_blkh) (le16_to_cpu((p_blkh)->blk_free_space))
609 #define blkh_reserved(p_blkh) (le16_to_cpu((p_blkh)->blk_reserved))
610 #define blkh_right_delim_key(p_blkh) ((p_blkh)->blk_right_delim_key)
611
612 /*
613 * values for blk_level field of the struct block_head
614 */
615
616 #define FREE_LEVEL 0 /* when node gets removed from the tree its
617 blk_level is set to FREE_LEVEL. It is then
618 used to see whether the node is still in the
619 tree */
620
621 #define DISK_LEAF_NODE_LEVEL 1 /* Leaf node level. */
622
623 /* Given the buffer head of a formatted node, resolve to the block head of that node. */
624 #define B_BLK_HEAD(p_s_bh) ((struct block_head *)((p_s_bh)->b_data))
625 /* Number of items that are in buffer. */
626 #define B_NR_ITEMS(p_s_bh) (blkh_nr_item(B_BLK_HEAD(p_s_bh)))
627 #define B_LEVEL(p_s_bh) (blkh_level(B_BLK_HEAD(p_s_bh)))
628 #define B_FREE_SPACE(p_s_bh) (blkh_free_space(B_BLK_HEAD(p_s_bh)))
629
630 /* Get right delimiting key. -- little endian */
631 #define B_PRIGHT_DELIM_KEY(p_s_bh) (&(blk_right_delim_key(B_BLK_HEAD(p_s_bh))
632
633 /* Does the buffer contain a disk leaf. */
634 #define B_IS_ITEMS_LEVEL(p_s_bh) (B_LEVEL(p_s_bh) == DISK_LEAF_NODE_LEVEL)
635
636 /* Does the buffer contain a disk internal node */
637 #define B_IS_KEYS_LEVEL(p_s_bh) (B_LEVEL(p_s_bh) > DISK_LEAF_NODE_LEVEL \
638 && B_LEVEL(p_s_bh) <= MAX_HEIGHT)
639
640 /***************************************************************************/
641 /* STAT DATA */
642 /***************************************************************************/
643
644 //
645 // old stat data is 32 bytes long. We are going to distinguish new one by
646 // different size
647 //
648 struct stat_data_v1 {
649 __le16 sd_mode; /* file type, permissions */
650 __le16 sd_nlink; /* number of hard links */
651 __le16 sd_uid; /* owner */
652 __le16 sd_gid; /* group */
653 __le32 sd_size; /* file size */
654 __le32 sd_atime; /* time of last access */
655 __le32 sd_mtime; /* time file was last modified */
656 __le32 sd_ctime; /* time inode (stat data) was last changed (except changes to sd_atime and sd_mtime) */
657 union {
658 __le32 sd_rdev;
659 __le32 sd_blocks; /* number of blocks file uses */
660 } ATTR_PACKED u;
661 __le32 sd_first_direct_byte; /* first byte of file which is stored
662 in a direct item: except that if it
663 equals 1 it is a symlink and if it
664 equals ~(__u32)0 there is no
665 direct item. The existence of this
666 field really grates on me. Let's
667 replace it with a macro based on
668 sd_size and our tail suppression
669 policy. Someday. -Hans */
670 } ATTR_PACKED;
671
672 #define SD_V1_SIZE (sizeof(struct stat_data_v1))
673 #define stat_data_v1(ih) (ih_version (ih) == KEY_FORMAT_3_5)
674 #define sd_v1_mode(sdp) (le16_to_cpu((sdp)->sd_mode))
675 #define set_sd_v1_mode(sdp,v) ((sdp)->sd_mode = cpu_to_le16(v))
676 #define sd_v1_nlink(sdp) (le16_to_cpu((sdp)->sd_nlink))
677 #define set_sd_v1_nlink(sdp,v) ((sdp)->sd_nlink = cpu_to_le16(v))
678 #define sd_v1_uid(sdp) (le16_to_cpu((sdp)->sd_uid))
679 #define set_sd_v1_uid(sdp,v) ((sdp)->sd_uid = cpu_to_le16(v))
680 #define sd_v1_gid(sdp) (le16_to_cpu((sdp)->sd_gid))
681 #define set_sd_v1_gid(sdp,v) ((sdp)->sd_gid = cpu_to_le16(v))
682 #define sd_v1_size(sdp) (le32_to_cpu((sdp)->sd_size))
683 #define set_sd_v1_size(sdp,v) ((sdp)->sd_size = cpu_to_le32(v))
684 #define sd_v1_atime(sdp) (le32_to_cpu((sdp)->sd_atime))
685 #define set_sd_v1_atime(sdp,v) ((sdp)->sd_atime = cpu_to_le32(v))
686 #define sd_v1_mtime(sdp) (le32_to_cpu((sdp)->sd_mtime))
687 #define set_sd_v1_mtime(sdp,v) ((sdp)->sd_mtime = cpu_to_le32(v))
688 #define sd_v1_ctime(sdp) (le32_to_cpu((sdp)->sd_ctime))
689 #define set_sd_v1_ctime(sdp,v) ((sdp)->sd_ctime = cpu_to_le32(v))
690 #define sd_v1_rdev(sdp) (le32_to_cpu((sdp)->u.sd_rdev))
691 #define set_sd_v1_rdev(sdp,v) ((sdp)->u.sd_rdev = cpu_to_le32(v))
692 #define sd_v1_blocks(sdp) (le32_to_cpu((sdp)->u.sd_blocks))
693 #define set_sd_v1_blocks(sdp,v) ((sdp)->u.sd_blocks = cpu_to_le32(v))
694 #define sd_v1_first_direct_byte(sdp) \
695 (le32_to_cpu((sdp)->sd_first_direct_byte))
696 #define set_sd_v1_first_direct_byte(sdp,v) \
697 ((sdp)->sd_first_direct_byte = cpu_to_le32(v))
698
699 /*
700 #include <linux/ext2_fs.h>
701 */
702
703 /* inode flags stored in sd_attrs (nee sd_reserved) */
704
705 /* we want common flags to have the same values as in ext2,
706 so chattr(1) will work without problems */
707 #define REISERFS_IMMUTABLE_FL EXT2_IMMUTABLE_FL
708 #define REISERFS_APPEND_FL EXT2_APPEND_FL
709 #define REISERFS_SYNC_FL EXT2_SYNC_FL
710 #define REISERFS_NOATIME_FL EXT2_NOATIME_FL
711 #define REISERFS_NODUMP_FL EXT2_NODUMP_FL
712 #define REISERFS_SECRM_FL EXT2_SECRM_FL
713 #define REISERFS_UNRM_FL EXT2_UNRM_FL
714 #define REISERFS_COMPR_FL EXT2_COMPR_FL
715 #define REISERFS_NOTAIL_FL EXT2_NOTAIL_FL
716
717 /* persistent flags that file inherits from the parent directory */
718 #define REISERFS_INHERIT_MASK ( REISERFS_IMMUTABLE_FL | \
719 REISERFS_SYNC_FL | \
720 REISERFS_NOATIME_FL | \
721 REISERFS_NODUMP_FL | \
722 REISERFS_SECRM_FL | \
723 REISERFS_COMPR_FL | \
724 REISERFS_NOTAIL_FL )
725
726 /* Stat Data on disk (reiserfs version of UFS disk inode minus the
727 address blocks) */
728 struct stat_data {
729 __le16 sd_mode; /* file type, permissions */
730 __le16 sd_attrs; /* persistent inode flags */
731 __le32 sd_nlink; /* number of hard links */
732 __le64 sd_size; /* file size */
733 __le32 sd_uid; /* owner */
734 __le32 sd_gid; /* group */
735 __le32 sd_atime; /* time of last access */
736 __le32 sd_mtime; /* time file was last modified */
737 __le32 sd_ctime; /* time inode (stat data) was last changed (except changes to sd_atime and sd_mtime) */
738 __le32 sd_blocks;
739 union {
740 __le32 sd_rdev;
741 __le32 sd_generation;
742 //__le32 sd_first_direct_byte;
743 /* first byte of file which is stored in a
744 direct item: except that if it equals 1
745 it is a symlink and if it equals
746 ~(__u32)0 there is no direct item. The
747 existence of this field really grates
748 on me. Let's replace it with a macro
749 based on sd_size and our tail
750 suppression policy? */
751 } ATTR_PACKED u;
752 } ATTR_PACKED;
753 //
754 // this is 44 bytes long
755 //
756 #define SD_SIZE (sizeof(struct stat_data))
757 #define SD_V2_SIZE SD_SIZE
758 #define stat_data_v2(ih) (ih_version (ih) == KEY_FORMAT_3_6)
759 #define sd_v2_mode(sdp) (le16_to_cpu((sdp)->sd_mode))
760 #define set_sd_v2_mode(sdp,v) ((sdp)->sd_mode = cpu_to_le16(v))
761 /* sd_reserved */
762 /* set_sd_reserved */
763 #define sd_v2_nlink(sdp) (le32_to_cpu((sdp)->sd_nlink))
764 #define set_sd_v2_nlink(sdp,v) ((sdp)->sd_nlink = cpu_to_le32(v))
765 #define sd_v2_size(sdp) (le64_to_cpu((sdp)->sd_size))
766 #define set_sd_v2_size(sdp,v) ((sdp)->sd_size = cpu_to_le64(v))
767 #define sd_v2_uid(sdp) (le32_to_cpu((sdp)->sd_uid))
768 #define set_sd_v2_uid(sdp,v) ((sdp)->sd_uid = cpu_to_le32(v))
769 #define sd_v2_gid(sdp) (le32_to_cpu((sdp)->sd_gid))
770 #define set_sd_v2_gid(sdp,v) ((sdp)->sd_gid = cpu_to_le32(v))
771 #define sd_v2_atime(sdp) (le32_to_cpu((sdp)->sd_atime))
772 #define set_sd_v2_atime(sdp,v) ((sdp)->sd_atime = cpu_to_le32(v))
773 #define sd_v2_mtime(sdp) (le32_to_cpu((sdp)->sd_mtime))
774 #define set_sd_v2_mtime(sdp,v) ((sdp)->sd_mtime = cpu_to_le32(v))
775 #define sd_v2_ctime(sdp) (le32_to_cpu((sdp)->sd_ctime))
776 #define set_sd_v2_ctime(sdp,v) ((sdp)->sd_ctime = cpu_to_le32(v))
777 #define sd_v2_blocks(sdp) (le32_to_cpu((sdp)->sd_blocks))
778 #define set_sd_v2_blocks(sdp,v) ((sdp)->sd_blocks = cpu_to_le32(v))
779 #define sd_v2_rdev(sdp) (le32_to_cpu((sdp)->u.sd_rdev))
780 #define set_sd_v2_rdev(sdp,v) ((sdp)->u.sd_rdev = cpu_to_le32(v))
781 #define sd_v2_generation(sdp) (le32_to_cpu((sdp)->u.sd_generation))
782 #define set_sd_v2_generation(sdp,v) ((sdp)->u.sd_generation = cpu_to_le32(v))
783 #define sd_v2_attrs(sdp) (le16_to_cpu((sdp)->sd_attrs))
784 #define set_sd_v2_attrs(sdp,v) ((sdp)->sd_attrs = cpu_to_le16(v))
785
786 /***************************************************************************/
787 /* DIRECTORY STRUCTURE */
788 /***************************************************************************/
789 /*
790 Picture represents the structure of directory items
791 ________________________________________________
792 | Array of | | | | | |
793 | directory |N-1| N-2 | .... | 1st |0th|
794 | entry headers | | | | | |
795 |_______________|___|_____|________|_______|___|
796 <---- directory entries ------>
797
798 First directory item has k_offset component 1. We store "." and ".."
799 in one item, always, we never split "." and ".." into differing
800 items. This makes, among other things, the code for removing
801 directories simpler. */
802 #define SD_OFFSET 0
803 #define SD_UNIQUENESS 0
804 #define DOT_OFFSET 1
805 #define DOT_DOT_OFFSET 2
806 #define DIRENTRY_UNIQUENESS 500
807
808 /* */
809 #define FIRST_ITEM_OFFSET 1
810
811 /*
812 Q: How to get key of object pointed to by entry from entry?
813
814 A: Each directory entry has its header. This header has deh_dir_id and deh_objectid fields, those are key
815 of object, entry points to */
816
817 /* NOT IMPLEMENTED:
818 Directory will someday contain stat data of object */
819
820 struct reiserfs_de_head {
821 __le32 deh_offset; /* third component of the directory entry key */
822 __le32 deh_dir_id; /* objectid of the parent directory of the object, that is referenced
823 by directory entry */
824 __le32 deh_objectid; /* objectid of the object, that is referenced by directory entry */
825 __le16 deh_location; /* offset of name in the whole item */
826 __le16 deh_state; /* whether 1) entry contains stat data (for future), and 2) whether
827 entry is hidden (unlinked) */
828 } ATTR_PACKED;
829 #define DEH_SIZE sizeof(struct reiserfs_de_head)
830 #define deh_offset(p_deh) (le32_to_cpu((p_deh)->deh_offset))
831 #define deh_dir_id(p_deh) (le32_to_cpu((p_deh)->deh_dir_id))
832 #define deh_objectid(p_deh) (le32_to_cpu((p_deh)->deh_objectid))
833 #define deh_location(p_deh) (le16_to_cpu((p_deh)->deh_location))
834 #define deh_state(p_deh) (le16_to_cpu((p_deh)->deh_state))
835
836 #define put_deh_offset(p_deh,v) ((p_deh)->deh_offset = cpu_to_le32((v)))
837 #define put_deh_dir_id(p_deh,v) ((p_deh)->deh_dir_id = cpu_to_le32((v)))
838 #define put_deh_objectid(p_deh,v) ((p_deh)->deh_objectid = cpu_to_le32((v)))
839 #define put_deh_location(p_deh,v) ((p_deh)->deh_location = cpu_to_le16((v)))
840 #define put_deh_state(p_deh,v) ((p_deh)->deh_state = cpu_to_le16((v)))
841
842 /* empty directory contains two entries "." and ".." and their headers */
843 #define EMPTY_DIR_SIZE \
844 (DEH_SIZE * 2 + ROUND_UP (strlen (".")) + ROUND_UP (strlen ("..")))
845
846 /* old format directories have this size when empty */
847 #define EMPTY_DIR_SIZE_V1 (DEH_SIZE * 2 + 3)
848
849 #define DEH_Statdata 0 /* not used now */
850 #define DEH_Visible 2
851
852 /* 64 bit systems (and the S/390) need to be aligned explicitly -jdm */
853 #if BITS_PER_LONG == 64 || defined(__s390__) || defined(__hppa__)
854 # define ADDR_UNALIGNED_BITS (3)
855 #endif
856
857 /* These are only used to manipulate deh_state.
858 * Because of this, we'll use the ext2_ bit routines,
859 * since they are little endian */
860 #ifdef ADDR_UNALIGNED_BITS
861
862 # define aligned_address(addr) ((void *)((long)(addr) & ~((1UL << ADDR_UNALIGNED_BITS) - 1)))
863 # define unaligned_offset(addr) (((int)((long)(addr) & ((1 << ADDR_UNALIGNED_BITS) - 1))) << 3)
864
865 # define set_bit_unaligned(nr, addr) ext2_set_bit((nr) + unaligned_offset(addr), aligned_address(addr))
866 # define clear_bit_unaligned(nr, addr) ext2_clear_bit((nr) + unaligned_offset(addr), aligned_address(addr))
867 # define test_bit_unaligned(nr, addr) ext2_test_bit((nr) + unaligned_offset(addr), aligned_address(addr))
868
869 #else
870
871 # define set_bit_unaligned(nr, addr) ext2_set_bit(nr, addr)
872 # define clear_bit_unaligned(nr, addr) ext2_clear_bit(nr, addr)
873 # define test_bit_unaligned(nr, addr) ext2_test_bit(nr, addr)
874
875 #endif
876
877 #define mark_de_with_sd(deh) set_bit_unaligned (DEH_Statdata, &((deh)->deh_state))
878 #define mark_de_without_sd(deh) clear_bit_unaligned (DEH_Statdata, &((deh)->deh_state))
879 #define mark_de_visible(deh) set_bit_unaligned (DEH_Visible, &((deh)->deh_state))
880 #define mark_de_hidden(deh) clear_bit_unaligned (DEH_Visible, &((deh)->deh_state))
881
882 #define de_with_sd(deh) test_bit_unaligned (DEH_Statdata, &((deh)->deh_state))
883 #define de_visible(deh) test_bit_unaligned (DEH_Visible, &((deh)->deh_state))
884 #define de_hidden(deh) !test_bit_unaligned (DEH_Visible, &((deh)->deh_state))
885
886 /*
887 extern void make_empty_dir_item_v1(char *body, __le32 dirid, __le32 objid,
888 __le32 par_dirid, __le32 par_objid);
889 extern void make_empty_dir_item(char *body, __le32 dirid, __le32 objid,
890 __le32 par_dirid, __le32 par_objid);
891 */
892
893 /* array of the entry headers */
894 /* get item body */
895 #define B_I_PITEM(bh,ih) ( (bh)->b_data + ih_location(ih) )
896 #define B_I_DEH(bh,ih) ((struct reiserfs_de_head *)(B_I_PITEM(bh,ih)))
897
898 /* length of the directory entry in directory item. This define
899 calculates length of i-th directory entry using directory entry
900 locations from dir entry head. When it calculates length of 0-th
901 directory entry, it uses length of whole item in place of entry
902 location of the non-existent following entry in the calculation.
903 See picture above.*/
904 /*
905 #define I_DEH_N_ENTRY_LENGTH(ih,deh,i) \
906 ((i) ? (deh_location((deh)-1) - deh_location((deh))) : (ih_item_len((ih)) - deh_location((deh))))
907 */
908 /*
909 static inline int entry_length(const struct buffer_head *bh,
910 const struct item_head *ih, int pos_in_item)
911 {
912 struct reiserfs_de_head *deh;
913
914 deh = B_I_DEH(bh, ih) + pos_in_item;
915 if (pos_in_item)
916 return deh_location(deh - 1) - deh_location(deh);
917
918 return ih_item_len(ih) - deh_location(deh);
919 }
920 */
921
922 /* number of entries in the directory item, depends on ENTRY_COUNT being at the start of directory dynamic data. */
923 #define I_ENTRY_COUNT(ih) (ih_entry_count((ih)))
924
925 /* name by bh, ih and entry_num */
926 #define B_I_E_NAME(bh,ih,entry_num) ((char *)(bh->b_data + ih_location(ih) + deh_location(B_I_DEH(bh,ih)+(entry_num))))
927
928 // two entries per block (at least)
929 #define REISERFS_MAX_NAME(block_size) 255
930
931 /* this structure is used for operations on directory entries. It is
932 not a disk structure. */
933 /* When reiserfs_find_entry or search_by_entry_key find directory
934 entry, they return filled reiserfs_dir_entry structure */
935 struct reiserfs_dir_entry {
936 struct buffer_head *de_bh;
937 int de_item_num;
938 struct item_head *de_ih;
939 int de_entry_num;
940 struct reiserfs_de_head *de_deh;
941 int de_entrylen;
942 int de_namelen;
943 char *de_name;
944 unsigned long *de_gen_number_bit_string;
945
946 __u32 de_dir_id;
947 __u32 de_objectid;
948
949 struct cpu_key de_entry_key;
950 };
951
952 /* these defines are useful when a particular member of a reiserfs_dir_entry is needed */
953
954 /* pointer to file name, stored in entry */
955 #define B_I_DEH_ENTRY_FILE_NAME(bh,ih,deh) (B_I_PITEM (bh, ih) + deh_location(deh))
956
957 /* length of name */
958 #define I_DEH_N_ENTRY_FILE_NAME_LENGTH(ih,deh,entry_num) \
959 (I_DEH_N_ENTRY_LENGTH (ih, deh, entry_num) - (de_with_sd (deh) ? SD_SIZE : 0))
960
961 /* hash value occupies bits from 7 up to 30 */
962 #define GET_HASH_VALUE(offset) ((offset) & 0x7fffff80LL)
963 /* generation number occupies 7 bits starting from 0 up to 6 */
964 #define GET_GENERATION_NUMBER(offset) ((offset) & 0x7fLL)
965 #define MAX_GENERATION_NUMBER 127
966
967 #define SET_GENERATION_NUMBER(offset,gen_number) (GET_HASH_VALUE(offset)|(gen_number))
968
969 /*
970 * Picture represents an internal node of the reiserfs tree
971 * ______________________________________________________
972 * | | Array of | Array of | Free |
973 * |block | keys | pointers | space |
974 * | head | N | N+1 | |
975 * |______|_______________|___________________|___________|
976 */
977
978 /***************************************************************************/
979 /* DISK CHILD */
980 /***************************************************************************/
981 /* Disk child pointer: The pointer from an internal node of the tree
982 to a node that is on disk. */
983 struct disk_child {
984 __le32 dc_block_number; /* Disk child's block number. */
985 __le16 dc_size; /* Disk child's used space. */
986 __le16 dc_reserved;
987 };
988
989 #define DC_SIZE (sizeof(struct disk_child))
990 #define dc_block_number(dc_p) (le32_to_cpu((dc_p)->dc_block_number))
991 #define dc_size(dc_p) (le16_to_cpu((dc_p)->dc_size))
992
993 /* Get disk child by buffer header and position in the tree node. */
994 #define B_N_CHILD(p_s_bh,n_pos) ((struct disk_child *)\
995 ((p_s_bh)->b_data+BLKH_SIZE+B_NR_ITEMS(p_s_bh)*KEY_SIZE+DC_SIZE*(n_pos)))
996
997 /* Get disk child number by buffer header and position in the tree node. */
998 #define B_N_CHILD_NUM(p_s_bh,n_pos) (dc_block_number(B_N_CHILD(p_s_bh,n_pos)))
999 #define PUT_B_N_CHILD_NUM(p_s_bh,n_pos, val) (put_dc_block_number(B_N_CHILD(p_s_bh,n_pos), val ))
1000
1001 /* maximal value of field child_size in structure disk_child */
1002 /* child size is the combined size of all items and their headers */
1003 #define MAX_CHILD_SIZE(bh) ((int)( (bh)->b_size - BLKH_SIZE ))
1004
1005 /* amount of used space in buffer (not including block head) */
1006 #define B_CHILD_SIZE(cur) (MAX_CHILD_SIZE(cur)-(B_FREE_SPACE(cur)))
1007
1008 /* max and min number of keys in internal node */
1009 #define MAX_NR_KEY(bh) ( (MAX_CHILD_SIZE(bh)-DC_SIZE)/(KEY_SIZE+DC_SIZE) )
1010 #define MIN_NR_KEY(bh) (MAX_NR_KEY(bh)/2)
1011
1012 /***************************************************************************/
1013 /* PATH STRUCTURES AND DEFINES */
1014 /***************************************************************************/
1015
1016 /* Search_by_key fills up the path from the root to the leaf as it descends the tree looking for the
1017 key. It uses reiserfs_bread to try to find buffers in the cache given their block number. If it
1018 does not find them in the cache it reads them from disk. For each node search_by_key finds using
1019 reiserfs_bread it then uses bin_search to look through that node. bin_search will find the
1020 position of the block_number of the next node if it is looking through an internal node. If it
1021 is looking through a leaf node bin_search will find the position of the item which has key either
1022 equal to given key, or which is the maximal key less than the given key. */
1023
1024 struct path_element {
1025 struct buffer_head *pe_buffer; /* Pointer to the buffer at the path in the tree. */
1026 int pe_position; /* Position in the tree node which is placed in the */
1027 /* buffer above. */
1028 };
1029
1030 #define MAX_HEIGHT 5 /* maximal height of a tree. don't change this without changing JOURNAL_PER_BALANCE_CNT */
1031 #define EXTENDED_MAX_HEIGHT 7 /* Must be equals MAX_HEIGHT + FIRST_PATH_ELEMENT_OFFSET */
1032 #define FIRST_PATH_ELEMENT_OFFSET 2 /* Must be equal to at least 2. */
1033
1034 #define ILLEGAL_PATH_ELEMENT_OFFSET 1 /* Must be equal to FIRST_PATH_ELEMENT_OFFSET - 1 */
1035 #define MAX_FEB_SIZE 6 /* this MUST be MAX_HEIGHT + 1. See about FEB below */
1036
1037 /* We need to keep track of who the ancestors of nodes are. When we
1038 perform a search we record which nodes were visited while
1039 descending the tree looking for the node we searched for. This list
1040 of nodes is called the path. This information is used while
1041 performing balancing. Note that this path information may become
1042 invalid, and this means we must check it when using it to see if it
1043 is still valid. You'll need to read search_by_key and the comments
1044 in it, especially about decrement_counters_in_path(), to understand
1045 this structure.
1046
1047 Paths make the code so much harder to work with and debug.... An
1048 enormous number of bugs are due to them, and trying to write or modify
1049 code that uses them just makes my head hurt. They are based on an
1050 excessive effort to avoid disturbing the precious VFS code.:-( The
1051 gods only know how we are going to SMP the code that uses them.
1052 znodes are the way! */
1053
1054 #define PATH_READA 0x1 /* do read ahead */
1055 #define PATH_READA_BACK 0x2 /* read backwards */
1056
1057 struct path {
1058 int path_length; /* Length of the array above. */
1059 int reada;
1060 struct path_element path_elements[EXTENDED_MAX_HEIGHT]; /* Array of the path elements. */
1061 int pos_in_item;
1062 };
1063
1064 #define pos_in_item(path) ((path)->pos_in_item)
1065
1066 #define INITIALIZE_PATH(var) \
1067 struct path var = {.path_length = ILLEGAL_PATH_ELEMENT_OFFSET, .reada = 0,}
1068
1069 /* Get path element by path and path position. */
1070 #define PATH_OFFSET_PELEMENT(p_s_path,n_offset) ((p_s_path)->path_elements +(n_offset))
1071
1072 /* Get buffer header at the path by path and path position. */
1073 #define PATH_OFFSET_PBUFFER(p_s_path,n_offset) (PATH_OFFSET_PELEMENT(p_s_path,n_offset)->pe_buffer)
1074
1075 /* Get position in the element at the path by path and path position. */
1076 #define PATH_OFFSET_POSITION(p_s_path,n_offset) (PATH_OFFSET_PELEMENT(p_s_path,n_offset)->pe_position)
1077
1078 #define PATH_PLAST_BUFFER(p_s_path) (PATH_OFFSET_PBUFFER((p_s_path), (p_s_path)->path_length))
1079 /* you know, to the person who didn't
1080 write this the macro name does not
1081 at first suggest what it does.
1082 Maybe POSITION_FROM_PATH_END? Or
1083 maybe we should just focus on
1084 dumping paths... -Hans */
1085 #define PATH_LAST_POSITION(p_s_path) (PATH_OFFSET_POSITION((p_s_path), (p_s_path)->path_length))
1086
1087 #define PATH_PITEM_HEAD(p_s_path) B_N_PITEM_HEAD(PATH_PLAST_BUFFER(p_s_path),PATH_LAST_POSITION(p_s_path))
1088
1089 /* in do_balance leaf has h == 0 in contrast with path structure,
1090 where root has level == 0. That is why we need these defines */
1091 #define PATH_H_PBUFFER(p_s_path, h) PATH_OFFSET_PBUFFER (p_s_path, p_s_path->path_length - (h)) /* tb->S[h] */
1092 #define PATH_H_PPARENT(path, h) PATH_H_PBUFFER (path, (h) + 1) /* tb->F[h] or tb->S[0]->b_parent */
1093 #define PATH_H_POSITION(path, h) PATH_OFFSET_POSITION (path, path->path_length - (h))
1094 #define PATH_H_B_ITEM_ORDER(path, h) PATH_H_POSITION(path, h + 1) /* tb->S[h]->b_item_order */
1095
1096 #define PATH_H_PATH_OFFSET(p_s_path, n_h) ((p_s_path)->path_length - (n_h))
1097
1098 #define get_last_bh(path) PATH_PLAST_BUFFER(path)
1099 #define get_ih(path) PATH_PITEM_HEAD(path)
1100 #define get_item_pos(path) PATH_LAST_POSITION(path)
1101 #define get_item(path) ((void *)B_N_PITEM(PATH_PLAST_BUFFER(path), PATH_LAST_POSITION (path)))
1102 #define item_moved(ih,path) comp_items(ih, path)
1103 #define path_changed(ih,path) comp_items (ih, path)
1104
1105 /***************************************************************************/
1106 /* MISC */
1107 /***************************************************************************/
1108
1109 /* Size of pointer to the unformatted node. */
1110 #define UNFM_P_SIZE (sizeof(unp_t))
1111 #define UNFM_P_SHIFT 2
1112
1113 // in in-core inode key is stored on le form
1114 #define INODE_PKEY(inode) ((struct reiserfs_key *)(REISERFS_I(inode)->i_key))
1115
1116 #define MAX_UL_INT 0xffffffff
1117 #define MAX_INT 0x7ffffff
1118 #define MAX_US_INT 0xffff
1119
1120 // reiserfs version 2 has max offset 60 bits. Version 1 - 32 bit offset
1121 #define U32_MAX (~(__u32)0)
1122
1123 /*
1124 static inline loff_t max_reiserfs_offset(struct inode *inode)
1125 {
1126 if (get_inode_item_key_version(inode) == KEY_FORMAT_3_5)
1127 return (loff_t) U32_MAX;
1128
1129 return (loff_t) ((~(__u64) 0) >> 4);
1130 }
1131 */
1132
1133 /*#define MAX_KEY_UNIQUENESS MAX_UL_INT*/
1134 #define MAX_KEY_OBJECTID MAX_UL_INT
1135
1136 #define MAX_B_NUM MAX_UL_INT
1137 #define MAX_FC_NUM MAX_US_INT
1138
1139 /* the purpose is to detect overflow of an unsigned short */
1140 #define REISERFS_LINK_MAX (MAX_US_INT - 1000)
1141
1142 /* The following defines are used in reiserfs_insert_item and reiserfs_append_item */
1143 #define REISERFS_KERNEL_MEM 0 /* reiserfs kernel memory mode */
1144 #define REISERFS_USER_MEM 1 /* reiserfs user memory mode */
1145
1146 #define fs_generation(s) (REISERFS_SB(s)->s_generation_counter)
1147 #define get_generation(s) atomic_read (&fs_generation(s))
1148 #define FILESYSTEM_CHANGED_TB(tb) (get_generation((tb)->tb_sb) != (tb)->fs_gen)
1149 #define __fs_changed(gen,s) (gen != get_generation (s))
1150 #define fs_changed(gen,s) ({cond_resched(); __fs_changed(gen, s);})
1151
1152 /***************************************************************************/
1153 /* FIXATE NODES */
1154 /***************************************************************************/
1155
1156 #define VI_TYPE_LEFT_MERGEABLE 1
1157 #define VI_TYPE_RIGHT_MERGEABLE 2
1158
1159 /* To make any changes in the tree we always first find node, that
1160 contains item to be changed/deleted or place to insert a new
1161 item. We call this node S. To do balancing we need to decide what
1162 we will shift to left/right neighbor, or to a new node, where new
1163 item will be etc. To make this analysis simpler we build virtual
1164 node. Virtual node is an array of items, that will replace items of
1165 node S. (For instance if we are going to delete an item, virtual
1166 node does not contain it). Virtual node keeps information about
1167 item sizes and types, mergeability of first and last items, sizes
1168 of all entries in directory item. We use this array of items when
1169 calculating what we can shift to neighbors and how many nodes we
1170 have to have if we do not any shiftings, if we shift to left/right
1171 neighbor or to both. */
1172 struct virtual_item {
1173 int vi_index; // index in the array of item operations
1174 unsigned short vi_type; // left/right mergeability
1175 unsigned short vi_item_len; /* length of item that it will have after balancing */
1176 struct item_head *vi_ih;
1177 const char *vi_item; // body of item (old or new)
1178 const void *vi_new_data; // 0 always but paste mode
1179 void *vi_uarea; // item specific area
1180 };
1181
1182 struct virtual_node {
1183 char *vn_free_ptr; /* this is a pointer to the free space in the buffer */
1184 unsigned short vn_nr_item; /* number of items in virtual node */
1185 short vn_size; /* size of node , that node would have if it has unlimited size and no balancing is performed */
1186 short vn_mode; /* mode of balancing (paste, insert, delete, cut) */
1187 short vn_affected_item_num;
1188 short vn_pos_in_item;
1189 struct item_head *vn_ins_ih; /* item header of inserted item, 0 for other modes */
1190 const void *vn_data;
1191 struct virtual_item *vn_vi; /* array of items (including a new one, excluding item to be deleted) */
1192 };
1193
1194 /* used by directory items when creating virtual nodes */
1195 struct direntry_uarea {
1196 int flags;
1197 __u16 entry_count;
1198 __u16 entry_sizes[1];
1199 } ATTR_PACKED;
1200
1201 /***************************************************************************/
1202 /* TREE BALANCE */
1203 /***************************************************************************/
1204
1205 /* This temporary structure is used in tree balance algorithms, and
1206 constructed as we go to the extent that its various parts are
1207 needed. It contains arrays of nodes that can potentially be
1208 involved in the balancing of node S, and parameters that define how
1209 each of the nodes must be balanced. Note that in these algorithms
1210 for balancing the worst case is to need to balance the current node
1211 S and the left and right neighbors and all of their parents plus
1212 create a new node. We implement S1 balancing for the leaf nodes
1213 and S0 balancing for the internal nodes (S1 and S0 are defined in
1214 our papers.)*/
1215
1216 #define MAX_FREE_BLOCK 7 /* size of the array of buffers to free at end of do_balance */
1217
1218 /* maximum number of FEB blocknrs on a single level */
1219 #define MAX_AMOUNT_NEEDED 2
1220
1221 /* someday somebody will prefix every field in this struct with tb_ */
1222 struct tree_balance {
1223 int tb_mode;
1224 int need_balance_dirty;
1225 struct super_block *tb_sb;
1226 struct reiserfs_transaction_handle *transaction_handle;
1227 struct path *tb_path;
1228 struct buffer_head *L[MAX_HEIGHT]; /* array of left neighbors of nodes in the path */
1229 struct buffer_head *R[MAX_HEIGHT]; /* array of right neighbors of nodes in the path */
1230 struct buffer_head *FL[MAX_HEIGHT]; /* array of fathers of the left neighbors */
1231 struct buffer_head *FR[MAX_HEIGHT]; /* array of fathers of the right neighbors */
1232 struct buffer_head *CFL[MAX_HEIGHT]; /* array of common parents of center node and its left neighbor */
1233 struct buffer_head *CFR[MAX_HEIGHT]; /* array of common parents of center node and its right neighbor */
1234
1235 struct buffer_head *FEB[MAX_FEB_SIZE]; /* array of empty buffers. Number of buffers in array equals
1236 cur_blknum. */
1237 struct buffer_head *used[MAX_FEB_SIZE];
1238 struct buffer_head *thrown[MAX_FEB_SIZE];
1239 int lnum[MAX_HEIGHT]; /* array of number of items which must be
1240 shifted to the left in order to balance the
1241 current node; for leaves includes item that
1242 will be partially shifted; for internal
1243 nodes, it is the number of child pointers
1244 rather than items. It includes the new item
1245 being created. The code sometimes subtracts
1246 one to get the number of wholly shifted
1247 items for other purposes. */
1248 int rnum[MAX_HEIGHT]; /* substitute right for left in comment above */
1249 int lkey[MAX_HEIGHT]; /* array indexed by height h mapping the key delimiting L[h] and
1250 S[h] to its item number within the node CFL[h] */
1251 int rkey[MAX_HEIGHT]; /* substitute r for l in comment above */
1252 int insert_size[MAX_HEIGHT]; /* the number of bytes by we are trying to add or remove from
1253 S[h]. A negative value means removing. */
1254 int blknum[MAX_HEIGHT]; /* number of nodes that will replace node S[h] after
1255 balancing on the level h of the tree. If 0 then S is
1256 being deleted, if 1 then S is remaining and no new nodes
1257 are being created, if 2 or 3 then 1 or 2 new nodes is
1258 being created */
1259
1260 /* fields that are used only for balancing leaves of the tree */
1261 int cur_blknum; /* number of empty blocks having been already allocated */
1262 int s0num; /* number of items that fall into left most node when S[0] splits */
1263 int s1num; /* number of items that fall into first new node when S[0] splits */
1264 int s2num; /* number of items that fall into second new node when S[0] splits */
1265 int lbytes; /* number of bytes which can flow to the left neighbor from the left */
1266 /* most liquid item that cannot be shifted from S[0] entirely */
1267 /* if -1 then nothing will be partially shifted */
1268 int rbytes; /* number of bytes which will flow to the right neighbor from the right */
1269 /* most liquid item that cannot be shifted from S[0] entirely */
1270 /* if -1 then nothing will be partially shifted */
1271 int s1bytes; /* number of bytes which flow to the first new node when S[0] splits */
1272 /* note: if S[0] splits into 3 nodes, then items do not need to be cut */
1273 int s2bytes;
1274 struct buffer_head *buf_to_free[MAX_FREE_BLOCK]; /* buffers which are to be freed after do_balance finishes by unfix_nodes */
1275 char *vn_buf; /* kmalloced memory. Used to create
1276 virtual node and keep map of
1277 dirtied bitmap blocks */
1278 int vn_buf_size; /* size of the vn_buf */
1279 struct virtual_node *tb_vn; /* VN starts after bitmap of bitmap blocks */
1280
1281 int fs_gen; /* saved value of `reiserfs_generation' counter
1282 see FILESYSTEM_CHANGED() macro in reiserfs_fs.h */
1283 #ifdef DISPLACE_NEW_PACKING_LOCALITIES
1284 struct in_core_key key; /* key pointer, to pass to block allocator or
1285 another low-level subsystem */
1286 #endif
1287 };
1288
1289 /* These are modes of balancing */
1290
1291 /* When inserting an item. */
1292 #define M_INSERT 'i'
1293 /* When inserting into (directories only) or appending onto an already
1294 existant item. */
1295 #define M_PASTE 'p'
1296 /* When deleting an item. */
1297 #define M_DELETE 'd'
1298 /* When truncating an item or removing an entry from a (directory) item. */
1299 #define M_CUT 'c'
1300
1301 /* used when balancing on leaf level skipped (in reiserfsck) */
1302 #define M_INTERNAL 'n'
1303
1304 /* When further balancing is not needed, then do_balance does not need
1305 to be called. */
1306 #define M_SKIP_BALANCING 's'
1307 #define M_CONVERT 'v'
1308
1309 /* modes of leaf_move_items */
1310 #define LEAF_FROM_S_TO_L 0
1311 #define LEAF_FROM_S_TO_R 1
1312 #define LEAF_FROM_R_TO_L 2
1313 #define LEAF_FROM_L_TO_R 3
1314 #define LEAF_FROM_S_TO_SNEW 4
1315
1316 #define FIRST_TO_LAST 0
1317 #define LAST_TO_FIRST 1
1318
1319 /* used in do_balance for passing parent of node information that has
1320 been gotten from tb struct */
1321 struct buffer_info {
1322 struct tree_balance *tb;
1323 struct buffer_head *bi_bh;
1324 struct buffer_head *bi_parent;
1325 int bi_position;
1326 };
1327
1328 /* there are 4 types of items: stat data, directory item, indirect, direct.
1329 +-------------------+------------+--------------+------------+
1330 | | k_offset | k_uniqueness | mergeable? |
1331 +-------------------+------------+--------------+------------+
1332 | stat data | 0 | 0 | no |
1333 +-------------------+------------+--------------+------------+
1334 | 1st directory item| DOT_OFFSET |DIRENTRY_UNIQUENESS| no |
1335 | non 1st directory | hash value | | yes |
1336 | item | | | |
1337 +-------------------+------------+--------------+------------+
1338 | indirect item | offset + 1 |TYPE_INDIRECT | if this is not the first indirect item of the object
1339 +-------------------+------------+--------------+------------+
1340 | direct item | offset + 1 |TYPE_DIRECT | if not this is not the first direct item of the object
1341 +-------------------+------------+--------------+------------+
1342 */
1343
1344 struct item_operations {
1345 int (*bytes_number) (struct item_head * ih, int block_size);
1346 void (*decrement_key) (struct cpu_key *);
1347 int (*is_left_mergeable) (struct reiserfs_key * ih,
1348 unsigned long bsize);
1349 void (*print_item) (struct item_head *, char *item);
1350 void (*check_item) (struct item_head *, char *item);
1351
1352 int (*create_vi) (struct virtual_node * vn, struct virtual_item * vi,
1353 int is_affected, int insert_size);
1354 int (*check_left) (struct virtual_item * vi, int free,
1355 int start_skip, int end_skip);
1356 int (*check_right) (struct virtual_item * vi, int free);
1357 int (*part_size) (struct virtual_item * vi, int from, int to);
1358 int (*unit_num) (struct virtual_item * vi);
1359 void (*print_vi) (struct virtual_item * vi);
1360 };
1361
1362 extern struct item_operations *item_ops[TYPE_ANY + 1];
1363
1364 #define op_bytes_number(ih,bsize) item_ops[le_ih_k_type (ih)]->bytes_number (ih, bsize)
1365 #define op_is_left_mergeable(key,bsize) item_ops[le_key_k_type (le_key_version (key), key)]->is_left_mergeable (key, bsize)
1366 #define op_print_item(ih,item) item_ops[le_ih_k_type (ih)]->print_item (ih, item)
1367 #define op_check_item(ih,item) item_ops[le_ih_k_type (ih)]->check_item (ih, item)
1368 #define op_create_vi(vn,vi,is_affected,insert_size) item_ops[le_ih_k_type ((vi)->vi_ih)]->create_vi (vn,vi,is_affected,insert_size)
1369 #define op_check_left(vi,free,start_skip,end_skip) item_ops[(vi)->vi_index]->check_left (vi, free, start_skip, end_skip)
1370 #define op_check_right(vi,free) item_ops[(vi)->vi_index]->check_right (vi, free)
1371 #define op_part_size(vi,from,to) item_ops[(vi)->vi_index]->part_size (vi, from, to)
1372 #define op_unit_num(vi) item_ops[(vi)->vi_index]->unit_num (vi)
1373 #define op_print_vi(vi) item_ops[(vi)->vi_index]->print_vi (vi)
1374
1375 #define COMP_SHORT_KEYS comp_short_keys
1376
1377 /* number of blocks pointed to by the indirect item */
1378 #define I_UNFM_NUM(p_s_ih) ( ih_item_len(p_s_ih) / UNFM_P_SIZE )
1379
1380 /* the used space within the unformatted node corresponding to pos within the item pointed to by ih */
1381 #define I_POS_UNFM_SIZE(ih,pos,size) (((pos) == I_UNFM_NUM(ih) - 1 ) ? (size) - ih_free_space(ih) : (size))
1382
1383 /* number of bytes contained by the direct item or the unformatted nodes the indirect item points to */
1384
1385 /* get the item header */
1386 #define B_N_PITEM_HEAD(bh,item_num) ( (struct item_head * )((bh)->b_data + BLKH_SIZE) + (item_num) )
1387
1388 /* get key */
1389 #define B_N_PDELIM_KEY(bh,item_num) ( (struct reiserfs_key * )((bh)->b_data + BLKH_SIZE) + (item_num) )
1390
1391 /* get the key */
1392 #define B_N_PKEY(bh,item_num) ( &(B_N_PITEM_HEAD(bh,item_num)->ih_key) )
1393
1394 /* get item body */
1395 #define B_N_PITEM(bh,item_num) ( (bh)->b_data + ih_location(B_N_PITEM_HEAD((bh),(item_num))))
1396
1397 /* get the stat data by the buffer header and the item order */
1398 #define B_N_STAT_DATA(bh,nr) \
1399 ( (struct stat_data *)((bh)->b_data + ih_location(B_N_PITEM_HEAD((bh),(nr))) ) )
1400
1401 /* following defines use reiserfs buffer header and item header */
1402
1403 /* get stat-data */
1404 #define B_I_STAT_DATA(bh, ih) ( (struct stat_data * )((bh)->b_data + ih_location(ih)) )
1405
1406 // this is 3976 for size==4096
1407 #define MAX_DIRECT_ITEM_LEN(size) ((size) - BLKH_SIZE - 2*IH_SIZE - SD_SIZE - UNFM_P_SIZE)
1408
1409 /* indirect items consist of entries which contain blocknrs, pos
1410 indicates which entry, and B_I_POS_UNFM_POINTER resolves to the
1411 blocknr contained by the entry pos points to */
1412 #define B_I_POS_UNFM_POINTER(bh,ih,pos) le32_to_cpu(*(((unp_t *)B_I_PITEM(bh,ih)) + (pos)))
1413 #define PUT_B_I_POS_UNFM_POINTER(bh,ih,pos, val) do {*(((unp_t *)B_I_PITEM(bh,ih)) + (pos)) = cpu_to_le32(val); } while (0)
1414
1415 struct reiserfs_iget_args {
1416 __u32 objectid;
1417 __u32 dirid;
1418 };
1419
1420 /***************************************************************************/
1421 /* FUNCTION DECLARATIONS */
1422 /***************************************************************************/
1423
1424 /*#ifdef __KERNEL__*/
1425 #define get_journal_desc_magic(bh) (bh->b_data + bh->b_size - 12)
1426
1427 #define journal_trans_half(blocksize) \
1428 ((blocksize - sizeof (struct reiserfs_journal_desc) + sizeof (__u32) - 12) / sizeof (__u32))
1429
1430 /* journal.c see journal.c for all the comments here */
1431
1432 /* first block written in a commit. */
1433 struct reiserfs_journal_desc {
1434 __le32 j_trans_id; /* id of commit */
1435 __le32 j_len; /* length of commit. len +1 is the commit block */
1436 __le32 j_mount_id; /* mount id of this trans */
1437 __le32 j_realblock[1]; /* real locations for each block */
1438 };
1439
1440 #define get_desc_trans_id(d) le32_to_cpu((d)->j_trans_id)
1441 #define get_desc_trans_len(d) le32_to_cpu((d)->j_len)
1442 #define get_desc_mount_id(d) le32_to_cpu((d)->j_mount_id)
1443
1444 #define set_desc_trans_id(d,val) do { (d)->j_trans_id = cpu_to_le32 (val); } while (0)
1445 #define set_desc_trans_len(d,val) do { (d)->j_len = cpu_to_le32 (val); } while (0)
1446 #define set_desc_mount_id(d,val) do { (d)->j_mount_id = cpu_to_le32 (val); } while (0)
1447
1448 /* last block written in a commit */
1449 struct reiserfs_journal_commit {
1450 __le32 j_trans_id; /* must match j_trans_id from the desc block */
1451 __le32 j_len; /* ditto */
1452 __le32 j_realblock[1]; /* real locations for each block */
1453 };
1454
1455 #define get_commit_trans_id(c) le32_to_cpu((c)->j_trans_id)
1456 #define get_commit_trans_len(c) le32_to_cpu((c)->j_len)
1457 #define get_commit_mount_id(c) le32_to_cpu((c)->j_mount_id)
1458
1459 #define set_commit_trans_id(c,val) do { (c)->j_trans_id = cpu_to_le32 (val); } while (0)
1460 #define set_commit_trans_len(c,val) do { (c)->j_len = cpu_to_le32 (val); } while (0)
1461
1462 /* this header block gets written whenever a transaction is considered fully flushed, and is more recent than the
1463 ** last fully flushed transaction. fully flushed means all the log blocks and all the real blocks are on disk,
1464 ** and this transaction does not need to be replayed.
1465 */
1466 struct reiserfs_journal_header {
1467 __le32 j_last_flush_trans_id; /* id of last fully flushed transaction */
1468 __le32 j_first_unflushed_offset; /* offset in the log of where to start replay after a crash */
1469 __le32 j_mount_id;
1470 /* 12 */ struct journal_params jh_journal;
1471 };
1472
1473 /* biggest tunable defines are right here */
1474 #define JOURNAL_BLOCK_COUNT 8192 /* number of blocks in the journal */
1475 #define JOURNAL_TRANS_MAX_DEFAULT 1024 /* biggest possible single transaction, don't change for now (8/3/99) */
1476 #define JOURNAL_TRANS_MIN_DEFAULT 256
1477 #define JOURNAL_MAX_BATCH_DEFAULT 900 /* max blocks to batch into one transaction, don't make this any bigger than 900 */
1478 #define JOURNAL_MIN_RATIO 2
1479 #define JOURNAL_MAX_COMMIT_AGE 30
1480 #define JOURNAL_MAX_TRANS_AGE 30
1481 #define JOURNAL_PER_BALANCE_CNT (3 * (MAX_HEIGHT-2) + 9)
1482 #ifdef CONFIG_QUOTA
1483 /* We need to update data and inode (atime) */
1484 #define REISERFS_QUOTA_TRANS_BLOCKS(s) (REISERFS_SB(s)->s_mount_opt & (1<<REISERFS_QUOTA) ? 2 : 0)
1485 /* 1 balancing, 1 bitmap, 1 data per write + stat data update */
1486 #define REISERFS_QUOTA_INIT_BLOCKS(s) (REISERFS_SB(s)->s_mount_opt & (1<<REISERFS_QUOTA) ? \
1487 (DQUOT_INIT_ALLOC*(JOURNAL_PER_BALANCE_CNT+2)+DQUOT_INIT_REWRITE+1) : 0)
1488 /* same as with INIT */
1489 #define REISERFS_QUOTA_DEL_BLOCKS(s) (REISERFS_SB(s)->s_mount_opt & (1<<REISERFS_QUOTA) ? \
1490 (DQUOT_DEL_ALLOC*(JOURNAL_PER_BALANCE_CNT+2)+DQUOT_DEL_REWRITE+1) : 0)
1491 #else
1492 #define REISERFS_QUOTA_TRANS_BLOCKS(s) 0
1493 #define REISERFS_QUOTA_INIT_BLOCKS(s) 0
1494 #define REISERFS_QUOTA_DEL_BLOCKS(s) 0
1495 #endif
1496
1497 /* both of these can be as low as 1, or as high as you want. The min is the
1498 ** number of 4k bitmap nodes preallocated on mount. New nodes are allocated
1499 ** as needed, and released when transactions are committed. On release, if
1500 ** the current number of nodes is > max, the node is freed, otherwise,
1501 ** it is put on a free list for faster use later.
1502 */
1503 #define REISERFS_MIN_BITMAP_NODES 10
1504 #define REISERFS_MAX_BITMAP_NODES 100
1505
1506 #define JBH_HASH_SHIFT 13 /* these are based on journal hash size of 8192 */
1507 #define JBH_HASH_MASK 8191
1508
1509 #define _jhashfn(sb,block) \
1510 (((unsigned long)sb>>L1_CACHE_SHIFT) ^ \
1511 (((block)<<(JBH_HASH_SHIFT - 6)) ^ ((block) >> 13) ^ ((block) << (JBH_HASH_SHIFT - 12))))
1512 #define journal_hash(t,sb,block) ((t)[_jhashfn((sb),(block)) & JBH_HASH_MASK])
1513
1514 // We need these to make journal.c code more readable
1515 #define journal_find_get_block(s, block) __find_get_block(SB_JOURNAL(s)->j_dev_bd, block, s->s_blocksize)
1516 #define journal_getblk(s, block) __getblk(SB_JOURNAL(s)->j_dev_bd, block, s->s_blocksize)
1517 #define journal_bread(s, block) __bread(SB_JOURNAL(s)->j_dev_bd, block, s->s_blocksize)
1518
1519 //enum reiserfs_bh_state_bits {
1520 // BH_JDirty = BH_PrivateStart, /* buffer is in current transaction */
1521 // BH_JDirty_wait,
1522 // BH_JNew, /* disk block was taken off free list before
1523 // * being in a finished transaction, or
1524 // * written to disk. Can be reused immed. */
1525 // BH_JPrepared,
1526 // BH_JRestore_dirty,
1527 // BH_JTest, // debugging only will go away
1528 //};
1529
1530 /*
1531 BUFFER_FNS(JDirty, journaled);
1532 TAS_BUFFER_FNS(JDirty, journaled);
1533 BUFFER_FNS(JDirty_wait, journal_dirty);
1534 TAS_BUFFER_FNS(JDirty_wait, journal_dirty);
1535 BUFFER_FNS(JNew, journal_new);
1536 TAS_BUFFER_FNS(JNew, journal_new);
1537 BUFFER_FNS(JPrepared, journal_prepared);
1538 TAS_BUFFER_FNS(JPrepared, journal_prepared);
1539 BUFFER_FNS(JRestore_dirty, journal_restore_dirty);
1540 TAS_BUFFER_FNS(JRestore_dirty, journal_restore_dirty);
1541 BUFFER_FNS(JTest, journal_test);
1542 TAS_BUFFER_FNS(JTest, journal_test);
1543 */
1544
1545 /*
1546 ** transaction handle which is passed around for all journal calls
1547 */
1548 struct reiserfs_transaction_handle {
1549 struct super_block *t_super; /* super for this FS when journal_begin was
1550 called. saves calls to reiserfs_get_super
1551 also used by nested transactions to make
1552 sure they are nesting on the right FS
1553 _must_ be first in the handle
1554 */
1555 int t_refcount;
1556 int t_blocks_logged; /* number of blocks this writer has logged */
1557 int t_blocks_allocated; /* number of blocks this writer allocated */
1558 unsigned long t_trans_id; /* sanity check, equals the current trans id */
1559 void *t_handle_save; /* save existing current->journal_info */
1560 unsigned displace_new_blocks:1; /* if new block allocation occurres, that block
1561 should be displaced from others */
1562 //struct list_head t_list;
1563 };
1564
1565 /* used to keep track of ordered and tail writes, attached to the buffer
1566 * head through b_journal_head.
1567 */
1568 struct reiserfs_jh {
1569 struct reiserfs_journal_list *jl;
1570 struct buffer_head *bh;
1571 //struct list_head list;
1572 };
1573
1574 //
1575 // get key version from on disk key - kludge
1576 //
1577 /*
1578 static inline int le_key_version(const struct reiserfs_key *key)
1579 {
1580 int type;
1581
1582 type = offset_v2_k_type(&(key->u.k_offset_v2));
1583 if (type != TYPE_DIRECT && type != TYPE_INDIRECT
1584 && type != TYPE_DIRENTRY)
1585 return KEY_FORMAT_3_5;
1586
1587 return KEY_FORMAT_3_6;
1588
1589 }
1590
1591 static inline void copy_key(struct reiserfs_key *to,
1592 const struct reiserfs_key *from)
1593 {
1594 memcpy(to, from, KEY_SIZE);
1595 }
1596 */
1597
1598 #define i_block_size(inode) ((inode)->i_sb->s_blocksize)
1599 #define file_size(inode) ((inode)->i_size)
1600 #define tail_size(inode) (file_size (inode) & (i_block_size (inode) - 1))
1601
1602 #define tail_has_to_be_packed(inode) (have_large_tails ((inode)->i_sb)?\
1603 !STORE_TAIL_IN_UNFM_S1(file_size (inode), tail_size(inode), inode->i_sb->s_blocksize):have_small_tails ((inode)->i_sb)?!STORE_TAIL_IN_UNFM_S2(file_size (inode), tail_size(inode), inode->i_sb->s_blocksize):0 )
1604
1605 /* inode.c */
1606 /* args for the create parameter of reiserfs_get_block */
1607 #define GET_BLOCK_NO_CREATE 0 /* don't create new blocks or convert tails */
1608 #define GET_BLOCK_CREATE 1 /* add anything you need to find block */
1609 #define GET_BLOCK_NO_HOLE 2 /* return -ENOENT for file holes */
1610 #define GET_BLOCK_READ_DIRECT 4 /* read the tail if indirect item not found */
1611 #define GET_BLOCK_NO_IMUX 8 /* i_mutex is not held, don't preallocate */
1612 #define GET_BLOCK_NO_DANGLE 16 /* don't leave any transactions running */
1613
1614 /* bitmap.c */
1615
1616 /* structure contains hints for block allocator, and it is a container for
1617 * arguments, such as node, search path, transaction_handle, etc. */
1618 struct __reiserfs_blocknr_hint {
1619 struct inode *inode; /* inode passed to allocator, if we allocate unf. nodes */
1620 long block; /* file offset, in blocks */
1621 struct in_core_key key;
1622 struct path *path; /* search path, used by allocator to deternine search_start by
1623 * various ways */
1624 struct reiserfs_transaction_handle *th; /* transaction handle is needed to log super blocks and
1625 * bitmap blocks changes */
1626 b_blocknr_t beg, end;
1627 b_blocknr_t search_start; /* a field used to transfer search start value (block number)
1628 * between different block allocator procedures
1629 * (determine_search_start() and others) */
1630 int prealloc_size; /* is set in determine_prealloc_size() function, used by underlayed
1631 * function that do actual allocation */
1632
1633 unsigned formatted_node:1; /* the allocator uses different polices for getting disk space for
1634 * formatted/unformatted blocks with/without preallocation */
1635 unsigned preallocate:1;
1636 };
1637
1638 typedef struct __reiserfs_blocknr_hint reiserfs_blocknr_hint_t;
1639
1640 /* hashes.c */
1641 __u32 keyed_hash(const signed char *msg, int len);
1642 __u32 yura_hash(const signed char *msg, int len);
1643 __u32 r5_hash(const signed char *msg, int len);
1644
1645 /* the ext2 bit routines adjust for big or little endian as
1646 ** appropriate for the arch, so in our laziness we use them rather
1647 ** than using the bit routines they call more directly. These
1648 ** routines must be used when changing on disk bitmaps. */
1649 #define reiserfs_test_and_set_le_bit ext2_set_bit
1650 #define reiserfs_test_and_clear_le_bit ext2_clear_bit
1651 #define reiserfs_test_le_bit ext2_test_bit
1652 #define reiserfs_find_next_zero_le_bit ext2_find_next_zero_bit
1653
1654 /* sometimes reiserfs_truncate may require to allocate few new blocks
1655 to perform indirect2direct conversion. People probably used to
1656 think, that truncate should work without problems on a filesystem
1657 without free disk space. They may complain that they can not
1658 truncate due to lack of free disk space. This spare space allows us
1659 to not worry about it. 500 is probably too much, but it should be
1660 absolutely safe */
1661 #define SPARE_SPACE 500
1662
1663 /* ioctl's command */
1664 #define REISERFS_IOC_UNPACK _IOW(0xCD,1,long)
1665 /* define following flags to be the same as in ext2, so that chattr(1),
1666 lsattr(1) will work with us. */
1667 #define REISERFS_IOC_GETFLAGS EXT2_IOC_GETFLAGS
1668 #define REISERFS_IOC_SETFLAGS EXT2_IOC_SETFLAGS
1669 #define REISERFS_IOC_GETVERSION EXT2_IOC_GETVERSION
1670 #define REISERFS_IOC_SETVERSION EXT2_IOC_SETVERSION
1671
1672
1673
1674 #pragma pack()
1675
1676
1677 #endif