1 /* Copyright 2002, 2003 by Hans Reiser, licensing governed by reiser4/README */
3 #include "../../debug.h"
4 #include "../../dformat.h"
6 #include "../node/node.h"
7 #include "../space/space_allocator.h"
8 #include "disk_format40.h"
10 #include "../../txnmgr.h"
11 #include "../../jnode.h"
12 #include "../../tree.h"
13 #include "../../super.h"
14 #include "../../wander.h"
15 #include "../../inode.h"
16 #include "../../ktxnmgrd.h"
17 #include "../../status_flags.h"
19 #include <linux/types.h> /* for __u?? */
20 #include <linux/fs.h> /* for struct super_block */
21 #include <linux/buffer_head.h>
23 /* reiser 4.0 default disk layout */
25 /* Amount of free blocks needed to perform release_format40 when fs gets
26 mounted RW: 1 for SB, 1 for non-leaves in overwrite set, 2 for tx header
28 #define RELEASE_RESERVED 4
30 /* The greatest supported format40 version number */
31 #define FORMAT40_VERSION PLUGIN_LIBRARY_VERSION
33 /* This flag indicates that backup should be updated
34 (the update is performed by fsck) */
35 #define FORMAT40_UPDATE_BACKUP (1 << 31)
37 /* functions to access fields of format40_disk_super_block */
38 static __u64
get_format40_block_count(const format40_disk_super_block
* sb
)
40 return le64_to_cpu(get_unaligned(&sb
->block_count
));
43 static __u64
get_format40_free_blocks(const format40_disk_super_block
* sb
)
45 return le64_to_cpu(get_unaligned(&sb
->free_blocks
));
48 static __u64
get_format40_root_block(const format40_disk_super_block
* sb
)
50 return le64_to_cpu(get_unaligned(&sb
->root_block
));
53 static __u16
get_format40_tree_height(const format40_disk_super_block
* sb
)
55 return le16_to_cpu(get_unaligned(&sb
->tree_height
));
58 static __u64
get_format40_file_count(const format40_disk_super_block
* sb
)
60 return le64_to_cpu(get_unaligned(&sb
->file_count
));
63 static __u64
get_format40_oid(const format40_disk_super_block
* sb
)
65 return le64_to_cpu(get_unaligned(&sb
->oid
));
68 static __u32
get_format40_mkfs_id(const format40_disk_super_block
* sb
)
70 return le32_to_cpu(get_unaligned(&sb
->mkfs_id
));
73 static __u64
get_format40_flags(const format40_disk_super_block
* sb
)
75 return le64_to_cpu(get_unaligned(&sb
->flags
));
78 static __u32
get_format40_version(const format40_disk_super_block
* sb
)
80 return le32_to_cpu(get_unaligned(&sb
->version
)) &
81 ~FORMAT40_UPDATE_BACKUP
;
84 static int update_backup_version(const format40_disk_super_block
* sb
)
86 return (le32_to_cpu(get_unaligned(&sb
->version
)) &
87 FORMAT40_UPDATE_BACKUP
);
90 static int update_disk_version(const format40_disk_super_block
* sb
)
92 return (get_format40_version(sb
) < FORMAT40_VERSION
);
95 static int incomplete_compatibility(const format40_disk_super_block
* sb
)
97 return (get_format40_version(sb
) > FORMAT40_VERSION
);
100 static format40_super_info
*get_sb_info(struct super_block
*super
)
102 return &get_super_private(super
)->u
.format40
;
105 static int consult_diskmap(struct super_block
*s
)
107 format40_super_info
*info
;
108 journal_location
*jloc
;
110 info
= get_sb_info(s
);
111 jloc
= &get_super_private(s
)->jloc
;
112 /* Default format-specific locations, if there is nothing in
114 jloc
->footer
= FORMAT40_JOURNAL_FOOTER_BLOCKNR
;
115 jloc
->header
= FORMAT40_JOURNAL_HEADER_BLOCKNR
;
116 info
->loc
.super
= FORMAT40_OFFSET
/ s
->s_blocksize
;
117 #ifdef CONFIG_REISER4_BADBLOCKS
118 reiser4_get_diskmap_value(FORMAT40_PLUGIN_DISKMAP_ID
, FORMAT40_JF
,
120 reiser4_get_diskmap_value(FORMAT40_PLUGIN_DISKMAP_ID
, FORMAT40_JH
,
122 reiser4_get_diskmap_value(FORMAT40_PLUGIN_DISKMAP_ID
, FORMAT40_SUPER
,
128 /* find any valid super block of disk_format40 (even if the first
129 super block is destroyed), will change block numbers of actual journal header/footer (jf/jh)
131 static struct buffer_head
*find_a_disk_format40_super_block(struct super_block
134 struct buffer_head
*super_bh
;
135 format40_disk_super_block
*disk_sb
;
136 format40_super_info
*info
;
138 assert("umka-487", s
!= NULL
);
140 info
= get_sb_info(s
);
142 super_bh
= sb_bread(s
, info
->loc
.super
);
143 if (super_bh
== NULL
)
144 return ERR_PTR(RETERR(-EIO
));
146 disk_sb
= (format40_disk_super_block
*) super_bh
->b_data
;
147 if (strncmp(disk_sb
->magic
, FORMAT40_MAGIC
, sizeof(FORMAT40_MAGIC
))) {
149 return ERR_PTR(RETERR(-EINVAL
));
152 reiser4_set_block_count(s
, le64_to_cpu(get_unaligned(&disk_sb
->block_count
)));
153 reiser4_set_data_blocks(s
, le64_to_cpu(get_unaligned(&disk_sb
->block_count
)) -
154 le64_to_cpu(get_unaligned(&disk_sb
->free_blocks
)));
155 reiser4_set_free_blocks(s
, le64_to_cpu(get_unaligned(&disk_sb
->free_blocks
)));
160 /* find the most recent version of super block. This is called after journal is
162 static struct buffer_head
*read_super_block(struct super_block
*s UNUSED_ARG
)
164 /* Here the most recent superblock copy has to be read. However, as
165 journal replay isn't complete, we are using
166 find_a_disk_format40_super_block() function. */
167 return find_a_disk_format40_super_block(s
);
170 static int get_super_jnode(struct super_block
*s
)
172 reiser4_super_info_data
*sbinfo
= get_super_private(s
);
176 sb_jnode
= reiser4_alloc_io_head(&get_sb_info(s
)->loc
.super
);
178 ret
= jload(sb_jnode
);
181 reiser4_drop_io_head(sb_jnode
);
185 pin_jnode_data(sb_jnode
);
188 sbinfo
->u
.format40
.sb_jnode
= sb_jnode
;
193 static void done_super_jnode(struct super_block
*s
)
195 jnode
*sb_jnode
= get_super_private(s
)->u
.format40
.sb_jnode
;
198 unpin_jnode_data(sb_jnode
);
199 reiser4_drop_io_head(sb_jnode
);
203 typedef enum format40_init_stage
{
218 } format40_init_stage
;
220 static format40_disk_super_block
*copy_sb(const struct buffer_head
*super_bh
)
222 format40_disk_super_block
*sb_copy
;
224 sb_copy
= kmalloc(sizeof(format40_disk_super_block
),
225 reiser4_ctx_gfp_mask_get());
227 return ERR_PTR(RETERR(-ENOMEM
));
228 memcpy(sb_copy
, ((format40_disk_super_block
*) super_bh
->b_data
),
229 sizeof(format40_disk_super_block
));
233 static int check_key_format(const format40_disk_super_block
*sb_copy
)
235 if (!equi(REISER4_LARGE_KEY
,
236 get_format40_flags(sb_copy
) & (1 << FORMAT40_LARGE_KEYS
))) {
237 warning("nikita-3228", "Key format mismatch. "
238 "Only %s keys are supported.",
239 REISER4_LARGE_KEY
? "large" : "small");
240 return RETERR(-EINVAL
);
251 static int try_init_format40(struct super_block
*super
,
252 format40_init_stage
*stage
)
255 struct buffer_head
*super_bh
;
256 reiser4_super_info_data
*sbinfo
;
257 format40_disk_super_block
*sb_copy
;
259 reiser4_block_nr root_block
;
262 assert("vs-475", super
!= NULL
);
263 assert("vs-474", get_super_private(super
));
267 result
= consult_diskmap(super
);
270 *stage
= CONSULT_DISKMAP
;
272 super_bh
= find_a_disk_format40_super_block(super
);
273 if (IS_ERR(super_bh
))
274 return PTR_ERR(super_bh
);
276 *stage
= FIND_A_SUPER
;
278 /* ok, we are sure that filesystem format is a format40 format */
280 /* map jnodes for journal control blocks (header, footer) to disk */
281 result
= reiser4_init_journal_info(super
);
284 *stage
= INIT_JOURNAL_INFO
;
286 /* ok, we are sure that filesystem format is a format40 format */
287 /* Now check it's state */
288 result
= reiser4_status_init(FORMAT40_STATUS_BLOCKNR
);
289 if (result
!= 0 && result
!= -EINVAL
)
290 /* -EINVAL means there is no magic, so probably just old
293 *stage
= INIT_STATUS
;
295 result
= reiser4_status_query(NULL
, NULL
);
296 if (result
== REISER4_STATUS_MOUNT_WARN
)
297 notice("vpf-1363", "Warning: mounting %s with errors.",
299 if (result
== REISER4_STATUS_MOUNT_RO
)
300 notice("vpf-1364", "Warning: mounting %s with fatal errors,"
301 " forcing read-only mount.", super
->s_id
);
302 result
= reiser4_journal_replay(super
);
305 *stage
= JOURNAL_REPLAY
;
307 super_bh
= read_super_block(super
);
308 if (IS_ERR(super_bh
))
309 return PTR_ERR(super_bh
);
312 /* allocate and make a copy of format40_disk_super_block */
313 sb_copy
= copy_sb(super_bh
);
317 return PTR_ERR(sb_copy
);
318 printk("reiser4: %s: found disk format 4.0.%u.\n",
320 get_format40_version(sb_copy
));
321 if (incomplete_compatibility(sb_copy
))
322 printk("reiser4: Warning: The last completely supported "
323 "version of disk format40 is %u. Some objects of "
324 "the semantic tree can be unaccessible.\n",
326 /* make sure that key format of kernel and filesystem match */
327 result
= check_key_format(sb_copy
);
334 result
= oid_init_allocator(super
, get_format40_file_count(sb_copy
),
335 get_format40_oid(sb_copy
));
342 /* get things necessary to init reiser4_tree */
343 root_block
= get_format40_root_block(sb_copy
);
344 height
= get_format40_tree_height(sb_copy
);
345 nplug
= node_plugin_by_id(NODE40_ID
);
347 /* initialize reiser4_super_info_data */
348 sbinfo
= get_super_private(super
);
349 assert("", sbinfo
->tree
.super
== super
);
350 /* init reiser4_tree for the filesystem */
351 result
= reiser4_init_tree(&sbinfo
->tree
, &root_block
, height
, nplug
);
359 * initialize reiser4_super_info_data with data from format40 super
362 sbinfo
->default_uid
= 0;
363 sbinfo
->default_gid
= 0;
364 sbinfo
->mkfs_id
= get_format40_mkfs_id(sb_copy
);
365 /* number of blocks in filesystem and reserved space */
366 reiser4_set_block_count(super
, get_format40_block_count(sb_copy
));
367 sbinfo
->blocks_free
= get_format40_free_blocks(sb_copy
);
368 sbinfo
->version
= get_format40_version(sb_copy
);
371 if (update_backup_version(sb_copy
))
372 printk("reiser4: Warning: metadata backup is not updated. "
373 "Please run 'fsck.reiser4 --fix' on %s.\n",
377 sbinfo
->fs_flags
|= (1 << REISER4_ADG
); /* hard links for directories
378 * are not supported */
379 sbinfo
->fs_flags
|= (1 << REISER4_ONE_NODE_PLUGIN
); /* all nodes in
383 /* sbinfo->tmgr is initialized already */
385 /* recover sb data which were logged separately from sb block */
387 /* NOTE-NIKITA: reiser4_journal_recover_sb_data() calls
388 * oid_init_allocator() and reiser4_set_free_blocks() with new
389 * data. What's the reason to call them above? */
390 result
= reiser4_journal_recover_sb_data(super
);
393 *stage
= JOURNAL_RECOVER
;
396 * Set number of used blocks. The number of used blocks is not stored
397 * neither in on-disk super block nor in the journal footer blocks. At
398 * this moment actual values of total blocks and free block counters
399 * are set in the reiser4 super block (in-memory structure) and we can
400 * calculate number of used blocks from them.
402 reiser4_set_data_blocks(super
,
403 reiser4_block_count(super
) -
404 reiser4_free_blocks(super
));
407 sbinfo
->min_blocks_used
= 16 /* reserved area */ +
408 2 /* super blocks */ +
409 2 /* journal footer and header */ ;
412 /* init disk space allocator */
413 result
= sa_init_allocator(reiser4_get_space_allocator(super
),
419 result
= get_super_jnode(super
);
425 /* plugin->u.format.get_ready */
426 int init_format_format40(struct super_block
*s
, void *data UNUSED_ARG
)
429 format40_init_stage stage
;
431 result
= try_init_format40(s
, &stage
);
434 assert("nikita-3458", result
== 0);
439 sa_destroy_allocator(reiser4_get_space_allocator(s
), s
);
440 case JOURNAL_RECOVER
:
442 reiser4_done_tree(&get_super_private(s
)->tree
);
448 reiser4_status_finish();
449 case INIT_JOURNAL_INFO
:
450 reiser4_done_journal_info(s
);
452 case CONSULT_DISKMAP
:
456 impossible("nikita-3457", "init stage: %i", stage
);
459 if (!rofs_super(s
) && reiser4_free_blocks(s
) < RELEASE_RESERVED
)
460 return RETERR(-ENOSPC
);
465 static void pack_format40_super(const struct super_block
*s
, char *data
)
467 format40_disk_super_block
*super_data
=
468 (format40_disk_super_block
*) data
;
470 reiser4_super_info_data
*sbinfo
= get_super_private(s
);
472 assert("zam-591", data
!= NULL
);
474 put_unaligned(cpu_to_le64(reiser4_free_committed_blocks(s
)),
475 &super_data
->free_blocks
);
477 put_unaligned(cpu_to_le64(sbinfo
->tree
.root_block
),
478 &super_data
->root_block
);
480 put_unaligned(cpu_to_le64(oid_next(s
)),
483 put_unaligned(cpu_to_le64(oids_used(s
)),
484 &super_data
->file_count
);
486 put_unaligned(cpu_to_le16(sbinfo
->tree
.height
),
487 &super_data
->tree_height
);
489 if (update_disk_version(super_data
)) {
490 __u32 version
= FORMAT40_VERSION
| FORMAT40_UPDATE_BACKUP
;
492 put_unaligned(cpu_to_le32(version
), &super_data
->version
);
496 /* plugin->u.format.log_super
497 return a jnode which should be added to transaction when the super block
499 jnode
*log_super_format40(struct super_block
*s
)
503 sb_jnode
= get_super_private(s
)->u
.format40
.sb_jnode
;
507 pack_format40_super(s
, jdata(sb_jnode
));
514 /* plugin->u.format.release */
515 int release_format40(struct super_block
*s
)
518 reiser4_super_info_data
*sbinfo
;
520 sbinfo
= get_super_private(s
);
521 assert("zam-579", sbinfo
!= NULL
);
523 if (!rofs_super(s
)) {
524 ret
= reiser4_capture_super_block(s
);
527 "reiser4_capture_super_block failed: %d",
530 ret
= txnmgr_force_commit_all(s
, 1);
532 warning("jmacd-74438", "txn_force failed: %d", ret
);
537 sa_destroy_allocator(&sbinfo
->space_allocator
, s
);
538 reiser4_done_journal_info(s
);
542 reiser4_done_tree(&sbinfo
->tree
);
543 /* call finish_rcu(), because some znode were "released" in
544 * reiser4_done_tree(). */
550 #define FORMAT40_ROOT_LOCALITY 41
551 #define FORMAT40_ROOT_OBJECTID 42
553 /* plugin->u.format.root_dir_key */
554 const reiser4_key
*root_dir_key_format40(const struct super_block
*super
557 static const reiser4_key FORMAT40_ROOT_DIR_KEY
= {
559 __constant_cpu_to_le64((FORMAT40_ROOT_LOCALITY
<< 4) | KEY_SD_MINOR
),
560 #if REISER4_LARGE_KEY
563 __constant_cpu_to_le64(FORMAT40_ROOT_OBJECTID
),
568 return &FORMAT40_ROOT_DIR_KEY
;
571 /* plugin->u.format.check_open.
572 Check the opened object for validness. For now it checks for the valid oid &
573 locality only, can be improved later and it its work may depend on the mount
575 int check_open_format40(const struct inode
*object
)
579 max
= oid_next(object
->i_sb
) - 1;
582 oid
= get_inode_oid(object
);
584 warning("vpf-1360", "The object with the oid %llu "
585 "greater then the max used oid %llu found.",
586 (unsigned long long)oid
, (unsigned long long)max
);
591 /* Check the locality. */
592 oid
= reiser4_inode_data(object
)->locality_id
;
594 warning("vpf-1361", "The object with the locality %llu "
595 "greater then the max used oid %llu found.",
596 (unsigned long long)oid
, (unsigned long long)max
);
604 /* plugin->u.format.version_update.
605 Perform all version update operations from the on-disk
606 format40_disk_super_block.version on disk to FORMAT40_VERSION.
608 int version_update_format40(struct super_block
*super
) {
614 /* Nothing to do if RO mount or the on-disk version is not less. */
615 if (super
->s_flags
& MS_RDONLY
)
618 if (get_super_private(super
)->version
>= FORMAT40_VERSION
)
621 printk("reiser4: Updating disk format to 4.0.%u. The reiser4 metadata "
622 "backup is left unchanged. Please run 'fsck.reiser4 --fix' "
623 "on %s to update it too.\n", FORMAT40_VERSION
, super
->s_id
);
625 /* Mark the uber znode dirty to call log_super on write_logs. */
627 ret
= get_uber_znode(reiser4_get_tree(super
), ZNODE_WRITE_LOCK
,
628 ZNODE_LOCK_HIPRI
, &lh
);
632 znode_make_dirty(lh
.node
);
635 /* Update the backup blocks. */
637 /* Force write_logs immediately. */
638 trans
= get_current_context()->trans
;
639 atom
= get_current_atom_locked();
640 assert("vpf-1906", atom
!= NULL
);
642 spin_lock_txnh(trans
);
643 return force_commit_atom(trans
);
648 c-indentation-style: "K&R"