ext2fs: add multi-mount protection (INCOMPAT_MMP)
PATCH 1/2 ext2fs: add multi-mount protection (INCOMPAT_MMP)
Multi-mount protection is an optional feature that allows mke2fs, e2fsck, and others to detect if the filesystem is mounted on a remote node (on SAN disks) and avoid corrupting the filesystem. For e2fsprogs this only means that it check the MMP block to see if the filesystem is in use, and mark the filesystem busy while e2fsck is running on the system.
There is no requirement that e2fsck updates the MMP block in any regular interval, but e2fsck does this occasionally to provide additional information to the sysadmin in case of conflict.
For the kernel (since Linux 3.0) this adds a "heartbeat" mechanism to periodically write to the disk (every few seconds by default) to notify other nodes that the filesystem is still in use.
--- debugfs/debug_cmds.ct | 6 + debugfs/debugfs.c | 44 +++++ debugfs/set_fields.c | 82 +++++++++- e2fsck/e2fsck.c | 2 + e2fsck/e2fsck.h | 2 + e2fsck/journal.c | 2 + e2fsck/pass1.c | 10 + e2fsck/pass1b.c | 4 + e2fsck/problem.c | 11 +- e2fsck/problem.h | 6 + e2fsck/unix.c | 99 ++++++++++- e2fsck/util.c | 28 +++ lib/e2p/feature.c | 4 +- lib/e2p/ls.c | 6 + lib/ext2fs/Makefile.in | 4 + lib/ext2fs/closefs.c | 5 + lib/ext2fs/ext2_err.et.in | 21 +++ lib/ext2fs/ext2_fs.h | 59 +++++-- lib/ext2fs/ext2fs.h | 26 +++ lib/ext2fs/freefs.c | 5 + lib/ext2fs/mmp.c | 413 +++++++++++++++++++++++++++++++++++++++++++ lib/ext2fs/openfs.c | 16 ++ lib/ext2fs/swapfs.c | 10 + lib/ext2fs/tst_super_size.c | 2 +- misc/mke2fs.8.in | 10 + misc/mke2fs.c | 30 +++- misc/tune2fs.8.in | 23 +++ misc/tune2fs.c | 272 +++++++++++++++++++++++----- misc/util.c | 14 ++ misc/util.h | 1 + 30 files changed, 1144 insertions(+), 73 deletions(-) create mode 100644 lib/ext2fs/mmp.c
debugfs/debug_cmds.ct +6 -0
@@ -166,5 +166,11 @@ request do_set_current_time, "Set current time to use when setting filesystme fi request do_supported_features, "Print features supported by this version of e2fsprogs", supported_features; +request do_dump_mmp, "Dump MMP information", + dump_mmp; + +request do_set_mmp_value, "Set MMP value", + set_mmp_value, smmp; + end;
debugfs/debugfs.c +44 -0
56 lines not on this page · raw message
debugfs/set_fields.c +81 -1
91 lines not on this page · raw message
e2fsck/e2fsck.c +2 -0
@@ -217,6 +217,8 @@ int e2fsck_run(e2fsck_t ctx)for (i=0 ; (e2fsck_pass = e2fsck_passes[i]); i++) {if (ctx->flags &E2F_FLAG_RUN_RETURN )break ; +if (e2fsck_mmp_update (ctx->fs)) +fatal_error (ctx,0 );e2fsck_pass (ctx);if (ctx->progress) (void ) (ctx->progress)(ctx,0 ,0 ,0 );
e2fsck/e2fsck.h +2 -0
@@ -523,6 +523,8 @@ extern blk_t get_backup_sb(e2fsck_t ctx, ext2_filsys fs, const char *name, io_manager manager);extern int ext2_file_type (unsigned int mode);extern int write_all (int fd,char *buf,size_t count); +void dump_mmp_msg (struct mmp_struct *mmp,const char *msg); +errcode_t e2fsck_mmp_update (ext2_filsys fs);/* unix.c */ extern void e2fsck_clear_progbar (e2fsck_t ctx);
e2fsck/journal.c +2 -0
@@ -881,6 +881,8 @@ int e2fsck_run_ext3_journal(e2fsck_t ctx) ctx->fs->io->manager->get_stats (ctx->fs->io, &stats);if (stats && stats->bytes_written) kbytes_written = stats->bytes_written >>10 ; + +ext2fs_mmp_stop (ctx->fs);ext2fs_free (ctx->fs); retval = ext2fs_open(ctx->filesystem_name, EXT2_FLAG_RW, ctx->superblock, blocksize, io_ptr,
e2fsck/pass1.c +10 -0
@@ -704,7 +704,17 @@ void e2fsck_pass1(e2fsck_t ctx) (fs->super->s_mtime < fs->super->s_inodes_count)) busted_fs_time =1 ; +if ((fs->super->s_feature_incompat &EXT4_FEATURE_INCOMPAT_MMP ) && + !(fs->super->s_mmp_block <= fs->super->s_first_data_block || + fs->super->s_mmp_block >= fs->super->s_blocks_count)) +ext2fs_mark_block_bitmap2 (ctx->block_found_map, + fs->super->s_mmp_block); +while (1 ) { +if (ino % (fs->super->s_inodes_per_group *4 ) ==1 ) { +if (e2fsck_mmp_update (fs)) +fatal_error (ctx,0 ); + } old_op =ehandler_operation (_ ("getting next inode from scan" )); pctx.errcode =ext2fs_get_next_inode_full (scan, &ino, inode, inode_size);
e2fsck/pass1b.c +4 -0
@@ -287,6 +287,10 @@ static void pass1b(e2fsck_t ctx, char *block_buf) pb.pctx = &pctx; pctx.str ="pass1b" ;while (1 ) { +if (ino % (fs->super->s_inodes_per_group *4 ) ==1 ) { +if (e2fsck_mmp_update (fs)) +fatal_error (ctx,0 ); + } pctx.errcode =ext2fs_get_next_inode (scan, &ino, &inode);if (pctx.errcode ==EXT2_ET_BAD_BLOCK_IN_INODE_TABLE )continue ;
e2fsck/problem.c +10 -1
@@ -412,6 +412,16 @@ static struct e2fsck_problem problem_table[] = {N_ ("Setting free @bs count to %c (was %b)\n" ), PROMPT_NONE, PR_PREEN_NOMSG }, +/* Superblock has invalid MMP block. */ + { PR_0_MMP_INVALID_BLK, +N_ ("@S has invalid MMP block. " ), + PROMPT_CLEAR, PR_PREEN_OK }, + +/* Superblock has invalid MMP magic. */ + { PR_0_MMP_INVALID_MAGIC, +N_ ("@S has invalid MMP magic. " ), + PROMPT_FIX, PR_PREEN_OK | PR_NO_OK}, +/* Pass 1 errors */ /* Pass 1: Checking inodes, blocks, and sizes */
@@ -948,7 +958,6 @@ static struct e2fsck_problem problem_table[] = {N_ ("Error adjusting refcount for @a @b %b (@i %i): %m\n" ), PROMPT_NONE, 0 }, -/* Pass 1C: Scan directories for inodes with multiply-claimed blocks. */ { PR_1C_PASS_HEADER, N_("Pass 1C: Scanning directories for @is with @m @bs\n"),
e2fsck/problem.h +6 -0
@@ -233,6 +233,12 @@ struct problem_context {/* Free blocks count wrong */ #define PR_0_FREE_BLOCK_COUNT 0x000040 +/* Superblock has invalid MMP block. */ +#define PR_0_MMP_INVALID_BLK 0x000041 + +/* Superblock has invalid MMP magic. */ +#define PR_0_MMP_INVALID_MAGIC 0x000042 +/* * Pass 1 errors
e2fsck/unix.c +98 -1
123 lines not on this page · raw message
e2fsck/util.c +28 -0
@@ -40,6 +40,7 @@extern e2fsck_t e2fsck_global_ctx;/* Try your very best not to use this! */ +#include <time.h> #include <sys/time.h> #include <sys/resource.h>
@@ -48,6 +49,7 @@ void fatal_error(e2fsck_t ctx, const char *msg)if (msg)fprintf (stderr,"e2fsck: %s\n" , msg);if (ctx->fs && ctx->fs->io) { +ext2fs_mmp_stop (ctx->fs);if (ctx->fs->io->magic ==EXT2_ET_MAGIC_IO_CHANNEL )io_channel_flush (ctx->fs->io);else
@@ -709,3 +711,29 @@ int write_all(int fd, char *buf, size_t count) }return c; } + +void dump_mmp_msg (struct mmp_struct *mmp,const char *msg) +{ + +if (msg) +printf ("MMP check failed: %s\n" , msg); +if (mmp) { +time_t t = mmp->mmp_time; + +printf ("MMP error info: last update: %s node: %s device: %s\n" , +ctime (&t), mmp->mmp_nodename, mmp->mmp_bdevname); + } +} + +errcode_t e2fsck_mmp_update (ext2_filsys fs) +{ +errcode_t retval; + + retval =ext2fs_mmp_update (fs); +if (retval ==EXT2_ET_MMP_CHANGE_ABORT ) +dump_mmp_msg (fs->mmp_cmp, +_ ("UNEXPECTED INCONSISTENCY: the filesystem is " +"being modified while fsck is running.\n" )); + +return retval; +}
lib/e2p/feature.c +3 -1
@@ -76,8 +76,10 @@ static struct feature feature_list[] = { "meta_bg" }, {E2P_FEATURE_INCOMPAT ,EXT4_FEATURE_INCOMPAT_64BIT , "64bit" }, + { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_MMP, + "mmp" }, { E2P_FEATURE_INCOMPAT, EXT4_FEATURE_INCOMPAT_FLEX_BG, - "flex_bg"}, + "flex_bg"}, { 0, 0, 0 }, };
lib/e2p/ls.c +6 -0
@@ -407,6 +407,12 @@ void list_super2(struct ext2_super_block * sb, FILE *f)fprintf (f,"Last error block #: %llu\n" , sb->s_last_error_block); } +if (sb->s_feature_incompat &EXT4_FEATURE_INCOMPAT_MMP ) { +fprintf (f,"MMP block number: %llu\n" , + (long long )sb->s_mmp_block); +fprintf (f,"MMP update interval: %u\n" , + sb->s_mmp_update_interval); + }if (sb->s_usr_quota_inum)fprintf (f,"User quota inode: %u\n" , sb->s_usr_quota_inum);
lib/ext2fs/Makefile.in +4 -0
@@ -64,6 +64,7 @@ OBJS= $(DEBUGFS_LIB_OBJS) $(RESIZE_LIB_OBJS) $(E2IMAGE_LIB_OBJS) \ lookup.o \ mkdir.o \ mkjournal.o \ + mmp.o \ namei.o \ native.o \ newdir.o \
@@ -133,6 +134,7 @@ SRCS= ext2_err.c \ $(srcdir)/lookup.c \ $(srcdir)/mkdir.c \ $(srcdir)/mkjournal.c \ + $(srcdir)/mmp.c \ $(srcdir)/namei.c \ $(srcdir)/native.c \ $(srcdir)/newdir.c \
@@ -655,6 +657,8 @@ mkjournal.o: $(srcdir)/mkjournal.c $(srcdir)/ext2_fs.h \ $(top_builddir)/lib/ext2fs/ext2_err.h $(srcdir)/ext2_ext_attr.h \ $(srcdir)/bitops.h $(srcdir)/jfs_user.h $(srcdir)/kernel-jbd.h \ $(srcdir)/jfs_compat.h $(srcdir)/kernel-list.h +mmp.o: $(srcdir)/ext2_fs.h $(srcdir)/ext2fs.h \ + $(top_builddir)/lib/ext2fs/ext2_err.h namei.o: $(srcdir)/namei.c $(srcdir)/ext2_fs.h \ $(top_builddir)/lib/ext2fs/ext2_types.h $(srcdir)/ext2fs.h \ $(srcdir)/ext2_fs.h $(srcdir)/ext3_extents.h $(top_srcdir)/lib/et/com_err.h \
lib/ext2fs/closefs.c +5 -0
@@ -457,6 +457,11 @@ errcode_t ext2fs_close(ext2_filsys fs)if (retval)return retval; } + + retval =ext2fs_mmp_stop (fs); +if (retval) +return retval; +ext2fs_free (fs);return 0 ; }
lib/ext2fs/ext2_err.et.in +21 -0
@@ -422,4 +422,25 @@ ec EXT2_NO_MTAB_FILE, ec EXT2_ET_CANT_USE_LEGACY_BITMAPS, "Filesystem too large to use legacy bitmaps" +ec EXT2_ET_MMP_MAGIC_INVALID, + "MMP: invalid magic number" + +ec EXT2_ET_MMP_FAILED, + "MMP: device currently active" + +ec EXT2_ET_MMP_FSCK_ON, + "MMP: fsck being run" + +ec EXT2_ET_MMP_BAD_BLOCK, + "MMP: block number beyond filesystem range" + +ec EXT2_ET_MMP_UNKNOWN_SEQ, + "MMP: undergoing an unknown operation" + +ec EXT2_ET_MMP_CHANGE_ABORT, + "MMP: filesystem still in use" + +ec EXT2_ET_MMP_OPEN_DIRECT, + "MMP: open with O_DIRECT failed" + end
lib/ext2fs/ext2_fs.h +42 -17
84 lines not on this page · raw message
lib/ext2fs/ext2fs.h +26 -0
@@ -195,6 +195,7 @@ typedef struct ext2_file *ext2_file_t;#define EXT2_FLAG_64BITS 0x20000#define EXT2_FLAG_PRINT_PROGRESS 0x40000#define EXT2_FLAG_DIRECT_IO 0x80000 +#define EXT2_FLAG_SKIP_MMP 0x100000/* * Special flag in the ext2 inode i_flag field that means that this is
@@ -256,6 +257,18 @@ struct struct_ext2_filsys {io_channel image_io;/* +* Buffers for Multiple mount protection(MMP) block. +*/ +void *mmp_buf; +void *mmp_cmp; +int mmp_fd; + +/* +* Time at which e2fsck last updated the MMP block. +*/ +long mmp_last_written; + +/* * More callback functions */ errcode_t (*get_alloc_block)(ext2_filsys fs, blk64_t goal,
@@ -548,6 +561,7 @@ typedef struct ext2_icount *ext2_icount_t; EXT3_FEATURE_INCOMPAT_RECOVER|\ EXT3_FEATURE_INCOMPAT_EXTENTS|\ EXT4_FEATURE_INCOMPAT_FLEX_BG|\ + EXT4_FEATURE_INCOMPAT_MMP|\ EXT4_FEATURE_INCOMPAT_64BIT)#else #define EXT2_LIB_FEATURE_INCOMPAT_SUPP (EXT2_FEATURE_INCOMPAT_FILETYPE|\
@@ -556,6 +570,7 @@ typedef struct ext2_icount *ext2_icount_t; EXT3_FEATURE_INCOMPAT_RECOVER|\ EXT3_FEATURE_INCOMPAT_EXTENTS|\ EXT4_FEATURE_INCOMPAT_FLEX_BG|\ + EXT4_FEATURE_INCOMPAT_MMP|\ EXT4_FEATURE_INCOMPAT_64BIT)#endif #define EXT2_LIB_FEATURE_RO_COMPAT_SUPP (EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER|\
@@ -1308,6 +1323,16 @@ errcode_t ext2fs_link(ext2_filsys fs, ext2_ino_t dir, const char *name,errcode_t ext2fs_unlink (ext2_filsys fs,ext2_ino_t dir,const char *name,ext2_ino_t ino,int flags); +/* mmp.c */ +errcode_t ext2fs_mmp_read (ext2_filsys fs,blk64_t mmp_blk,void *buf); +errcode_t ext2fs_mmp_write (ext2_filsys fs,blk64_t mmp_blk,void *buf); +errcode_t ext2fs_mmp_clear (ext2_filsys fs); +errcode_t ext2fs_mmp_init (ext2_filsys fs); +errcode_t ext2fs_mmp_start (ext2_filsys fs); +errcode_t ext2fs_mmp_update (ext2_filsys fs); +errcode_t ext2fs_mmp_stop (ext2_filsys fs); +unsigned ext2fs_mmp_new_seq (); +/* read_bb.c */ extern errcode_t ext2fs_read_bb_inode (ext2_filsys fs,ext2_badblocks_list *bb_list);
@@ -1343,6 +1368,7 @@ extern void ext2fs_swap_inode_full(ext2_filsys fs, struct ext2_inode_large *t, int bufsize);extern void ext2fs_swap_inode (ext2_filsys fs,struct ext2_inode *t,struct ext2_inode *f,int hostorder); +extern void ext2fs_swap_mmp (struct mmp_struct *mmp);/* valid_blk.c */ extern int ext2fs_inode_has_valid_blocks (struct ext2_inode *inode);
lib/ext2fs/freefs.c +5 -0
@@ -53,6 +53,11 @@ void ext2fs_free(ext2_filsys fs)if (fs->icache)ext2fs_free_inode_cache (fs->icache); +if (fs->mmp_buf) +ext2fs_free_mem (&fs->mmp_buf); +if (fs->mmp_cmp) +ext2fs_free_mem (&fs->mmp_cmp); + fs->magic =0 ;ext2fs_free_mem (&fs);
lib/ext2fs/mmp.c new file +413 -0
413 lines not on this page · raw message
lib/ext2fs/openfs.c +16 -0
@@ -22,6 +22,9 @@#if HAVE_SYS_TYPES_H #include <sys/types.h> #endif +#ifdef HAVE_ERRNO_H +#include <errno.h> +#endif #include "ext2_fs.h"
@@ -82,6 +85,7 @@ errcode_t ext2fs_open(const char *name, int flags, int superblock,* EXT2_FLAG_FORCE - Open the filesystem even if some of the * features aren't supported. * EXT2_FLAG_JOURNAL_DEV_OK - Open an ext3 journal device +* EXT2_FLAG_SKIP_MMP - Open without multi-mount protection check. */ errcode_t ext2fs_open2(const char *name, const char *io_options, int flags, int superblock,
@@ -379,6 +383,18 @@ errcode_t ext2fs_open2(const char *name, const char *io_options, fs->flags &= ~EXT2_FLAG_NOFREE_ON_ERROR ; *ret_fs = fs; + +if ((fs->super->s_feature_incompat &EXT4_FEATURE_INCOMPAT_MMP ) && + !(flags &EXT2_FLAG_SKIP_MMP ) && + (flags & (EXT2_FLAG_RW |EXT2_FLAG_EXCLUSIVE ))) { + retval =ext2fs_mmp_start (fs); +if (retval) { + fs->flags |=EXT2_FLAG_SKIP_MMP ;/* just do cleanup */ +ext2fs_mmp_stop (fs); + goto cleanup; + } + } +return 0 ; cleanup:if (flags &EXT2_FLAG_NOFREE_ON_ERROR )
lib/ext2fs/swapfs.c +10 -0
@@ -70,6 +70,8 @@ void ext2fs_swap_super(struct ext2_super_block * sb) sb->s_min_extra_isize =ext2fs_swab16 (sb->s_min_extra_isize); sb->s_want_extra_isize =ext2fs_swab16 (sb->s_want_extra_isize); sb->s_flags =ext2fs_swab32 (sb->s_flags); + sb->s_mmp_update_interval =ext2fs_swab16 (sb->s_mmp_update_interval); + sb->s_mmp_block =ext2fs_swab64 (sb->s_mmp_block); sb->s_kbytes_written =ext2fs_swab64 (sb->s_kbytes_written); sb->s_snapshot_inum =ext2fs_swab32 (sb->s_snapshot_inum); sb->s_snapshot_id =ext2fs_swab32 (sb->s_snapshot_id);
@@ -312,4 +314,12 @@ void ext2fs_swap_inode(ext2_filsys fs, struct ext2_inode *t, sizeof(struct ext2_inode)); } +void ext2fs_swap_mmp (struct mmp_struct *mmp) +{ + mmp->mmp_magic =ext2fs_swab32 (mmp->mmp_magic); + mmp->mmp_seq =ext2fs_swab32 (mmp->mmp_seq); + mmp->mmp_time =ext2fs_swab64 (mmp->mmp_time); + mmp->mmp_check_interval =ext2fs_swab16 (mmp->mmp_check_interval); +} +#endif
lib/ext2fs/tst_super_size.c +1 -1
@@ -100,7 +100,7 @@ void check_superblock_fields()check_field (s_want_extra_isize);check_field (s_flags);check_field (s_raid_stride); -check_field (s_mmp_interval); +check_field (s_mmp_update_interval);check_field (s_mmp_block);check_field (s_raid_stripe_width);check_field (s_log_groups_per_flex);
misc/mke2fs.8.in +10 -0
@@ -198,6 +198,16 @@ option is still accepted for backwards compatibility. The following extended options are supported: .RS 1.2i .TP +.BI mmp_update_interval= interval +Adjust the initial MMP update interval to +.I interval +seconds. Specifying an +.I interval +of 0 means to use the default interval. The specified interval must +be less than 300 seconds. Requires that the +.B mmp +feature be enabled. +.TP .BI stride= stride-size Configure the filesystem for a RAID array with .I stride-size
misc/mke2fs.c +29 -1
@@ -673,7 +673,21 @@ static void parse_extended_opts(struct ext2_super_block *param, *arg =0 ; arg++; } -if (strcmp (token,"stride" ) ==0 ) { +if (strcmp (token,"mmp_update_interval" ) ==0 ) { +if (!arg) { + r_usage++; + badopt = token; +continue ; + } + param->s_mmp_update_interval =strtoul (arg, &p,0 ); +if (*p) { +fprintf (stderr, +_ ("Invalid mmp_update_interval: %s\n" ), + arg); + r_usage++; +continue ; + } + }else if (strcmp (token,"stride" ) ==0 ) {if (!arg) { r_usage++; badopt = token;
@@ -821,6 +835,7 @@ static __u32 ok_features[3] = {EXT3_FEATURE_INCOMPAT_JOURNAL_DEV |EXT2_FEATURE_INCOMPAT_META_BG |EXT4_FEATURE_INCOMPAT_FLEX_BG | +EXT4_FEATURE_INCOMPAT_MMP |EXT4_FEATURE_INCOMPAT_64BIT ,/* R/O compat */ EXT2_FEATURE_RO_COMPAT_LARGE_FILE|
@@ -2462,6 +2477,19 @@ int main (int argc, char *argv[])printf (_ ("done\n" )); } no_journal: +if (!super_only && + fs->super->s_feature_incompat &EXT4_FEATURE_INCOMPAT_MMP ) { + retval =ext2fs_mmp_init (fs); +if (retval) { +fprintf (stderr,_ ("\nError while enabling multiple " +"mount protection feature." )); +exit (1 ); + } +if (!quiet) +printf (_ ("Multiple mount protection is enabled " +"with update interval %d seconds.\n" ), + fs->super->s_mmp_update_interval); + }if (EXT2_HAS_RO_COMPAT_FEATURE (&fs_param,EXT4_FEATURE_RO_COMPAT_BIGALLOC ))
misc/tune2fs.8.in +23 -0
@@ -167,6 +167,21 @@ separated, and may take an argument using the equals ('=') sign. The following extended options are supported: .RS 1.2i .TP +.B clear_mmp +Reset the MMP block (if any) back to the clean state. Use only if +absolutely certain the device is not currently mounted or being +fscked, or major filesystem corruption can result. Needs '-f'. +.TP +.BI mmp_update_interval= interval +Adjust the initial MMP update interval to +.I interval +seconds. Specifying an +.I interval +of 0 means to use the default interval. The specified interval must +be less than 300 seconds. Requires that the +.B mmp +feature be enabled. +.TP .BI stride= stride-size Configure the filesystem for a RAID array with .I stride-size
@@ -522,6 +537,11 @@ future. .B Tune2fs only supports clearing this filesystem feature. .TP +.B mmp +Enable or disable multiple mount protection(MMP) feature. MMP helps to protect +the filesystem from being multiply mounted and is useful in shared storage +environments. +.TP .B sparse_super Limit the number of backup superblocks to save space on large filesystems. .TP
@@ -558,6 +578,9 @@ and .BR flex_bg features are only supported by the ext4 filesystem. .TP +.BI \-p " mmp_check_interval" +Set the desired MMP check interval in seconds. It is 5 seconds by default. +.TP .BI \-r " reserved-blocks-count" Set the number of reserved filesystem blocks. .TP
misc/tune2fs.c +222 -50
538 lines not on this page · raw message
misc/util.c +14 -0
@@ -23,6 +23,7 @@#ifdef HAVE_SYS_STAT_H #include <sys/stat.h> #endif +#include <time.h> #include "et/com_err.h" #include "e2p/e2p.h"
@@ -291,3 +292,16 @@ void print_check_message(ext2_filsys fs) fs->super->s_max_mnt_count, (double)fs->super->s_checkinterval / (3600 * 24)); } + +void dump_mmp_msg (struct mmp_struct *mmp,const char *msg) +{ + +if (msg) +printf ("MMP check failed: %s\n" , msg); +if (mmp) { +time_t t = mmp->mmp_time; + +printf ("MMP error info: last update: %s node: %s device: %s\n" , +ctime (&t), mmp->mmp_nodename, mmp->mmp_bdevname); + } +}
misc/util.h +1 -0
@@ -24,3 +24,4 @@ extern void parse_journal_opts(const char *opts);extern void check_mount (const char *device,int force,const char *type);extern unsigned int figure_journal_size (int size,ext2_filsys fs);extern void print_check_message (ext2_filsys fs); +extern void dump_mmp_msg (struct mmp_struct *mmp,const char *msg);
Signature · 1 line
1.7.3.4
PATCH 2/2 e2fsck: regression tests for INCOMPAT_MMP feature
Add tests for the MMP feature - creating a filesystem with mke2fs and MMP enabled, enable/disable MMP with tune2fs, disabling the e2fsck MMP flag with tune2fs after a failed e2fsck, and e2fsck checking and fixing a corrupt MMP block.
The MMP tests need to be run from a real disk, not tmpfs, because tmpfs doesn't support O_DIRECT reads, which MMP uses to ensure that reads from the MMP block are not filled from the page cache. Using a local disk does not slow down the tests noticably, since they wait to detect if the MMP block is being modified.
--- tests/f_mmp/expect.1 | 79 ++++++++++++++++++++++++++++++++++++++++++ tests/f_mmp/script | 15 ++++++++ tests/f_mmp_1on/name | 1 + tests/f_mmp_1on/script | 40 +++++++++++++++++++++ tests/f_mmp_2off/name | 1 + tests/f_mmp_2off/script | 40 +++++++++++++++++++++ tests/f_mmp_e2fsck/name | 1 + tests/f_mmp_e2fsck/script | 67 +++++++++++++++++++++++++++++++++++ tests/f_mmp_garbage/expect.1 | 9 +++++ tests/f_mmp_garbage/expect.2 | 7 ++++ tests/f_mmp_garbage/name | 1 + tests/f_mmp_garbage/script | 28 +++++++++++++++ 12 files changed, 289 insertions(+), 0 deletions(-) create mode 100644 tests/f_mmp/expect.1 create mode 100644 tests/f_mmp/script create mode 100644 tests/f_mmp_1on/name create mode 100644 tests/f_mmp_1on/script create mode 100644 tests/f_mmp_2off/name create mode 100644 tests/f_mmp_2off/script create mode 100644 tests/f_mmp_e2fsck/name create mode 100644 tests/f_mmp_e2fsck/script create mode 100644 tests/f_mmp_garbage/expect.1 create mode 100644 tests/f_mmp_garbage/expect.2 create mode 100644 tests/f_mmp_garbage/name create mode 100644 tests/f_mmp_garbage/script
tests/f_mmp/expect.1 new file +79 -0
@@ -0,0 +1,79 @@ +Filesystem label= +OS type: Linux +Block size=2048 (log=1) +Fragment size=2048 (log=1) +Stride=0 blocks, Stripe width=0 blocks +16384 inodes, 32768 blocks +1638 blocks (5.00%) reserved for the super user +First data block=0 +Maximum filesystem blocks=33554432 +2 block groups +16384 blocks per group, 16384 fragments per group +8192 inodes per group +Superblock backups stored on blocks: + 16384 + +Allocating group tables: 0/21/2 done +Writing inode tables: 0/21/2 done +Multiple mount protection is enabled with update interval 5 seconds. +Writing superblocks and filesystem accounting information: 0/21/2 done + +Filesystem features: ext_attr resize_inode dir_index filetype mmp sparse_super + +Pass 1: Checking inodes, blocks, and sizes +Pass 2: Checking directory structure +Pass 3: Checking directory connectivity +Pass 4: Checking reference counts +Pass 5: Checking group summary information +test_filesys: 11/16384 files (0.0% non-contiguous), 1105/32768 blocks +Exit status is 0 + +Filesystem volume name: <none> +Last mounted on: <not available> +Filesystem magic number: 0xEF53 +Filesystem revision #: 1 (dynamic) +Filesystem features: ext_attr resize_inode dir_index filetype mmp sparse_super +Default mount options: (none) +Filesystem state: clean +Errors behavior: Continue +Filesystem OS type: Linux +Inode count: 16384 +Block count: 32768 +Reserved block count: 1638 +Free blocks: 31663 +Free inodes: 16373 +First block: 0 +Block size: 2048 +Fragment size: 2048 +Reserved GDT blocks: 31 +Blocks per group: 16384 +Fragments per group: 16384 +Inodes per group: 8192 +Inode blocks per group: 512 +Mount count: 0 +Check interval: 15552000 (6 months) +Reserved blocks uid: 0 +Reserved blocks gid: 0 +First inode: 11 +Inode size: 128 +Default directory hash: half_md4 +MMP block number: 557 +MMP update interval: 5 + + +Group 0: (Blocks 0-16383) + Primary superblock at 0, Group descriptors at 1-1 + Reserved GDT blocks at 2-32 + Block bitmap at 33 (+33), Inode bitmap at 34 (+34) + Inode table at 35-546 (+35) + 15826 free blocks, 8181 free inodes, 2 directories + Free blocks: 558-16383 + Free inodes: 12-8192 +Group 1: (Blocks 16384-32767) + Backup superblock at 16384, Group descriptors at 16385-16385 + Reserved GDT blocks at 16386-16416 + Block bitmap at 16417 (+33), Inode bitmap at 16418 (+34) + Inode table at 16419-16930 (+35) + 15837 free blocks, 8192 free inodes, 0 directories + Free blocks: 16931-32767 + Free inodes: 8193-16384
tests/f_mmp/script new file +15 -0
@@ -0,0 +1,15 @@ +DESCRIPTION="enable MMP during mke2fs" +FS_SIZE=65536 +MKE2FS_DEVICE_SECTSIZE=2048 +export MKE2FS_DEVICE_SECTSIZE +TMPFILE=test.img +> $TMPFILE +stat -f $TMPFILE | grep -q "Type: tmpfs" +if [ $? == 0 ]; then + rm -f $TMPFILE + echo "skipped for tmpfs (no O_DIRECT support)" + return 0 +fi +MKE2FS_OPTS="-O mmp" +. $cmd_dir/run_mke2fs +unset MKE2FS_DEVICE_SECTSIZE
tests/f_mmp_1on/name new file +1 -0
@@ -0,0 +1 @@ +enable MMP using tune2fs
tests/f_mmp_1on/script new file +40 -0
@@ -0,0 +1,40 @@ +FSCK_OPT=-yf + +TMPFILE=test.img +rm -f $test_name.failed $test_name.ok +> $TMPFILE + +stat -f $TMPFILE | grep -q "Type: tmpfs" +if [ $? == 0 ] ; then + rm -f $TMPFILE + echo "skipped for tmpfs (no O_DIRECT support)" + return 0 +fi + +$MKE2FS -q -F -o Linux -b 1024 $TMPFILE 100 > $test_name.log 2>&1 +status=$? +if [ "$status" != 0 ] ; then + echo "mke2fs failed" > $test_name.failed + echo "failed" + return $status +fi + +$TUNE2FS -O mmp -E mmp_update_interval=1 $TMPFILE >> $test_name.log 2>&1 +status=$? +if [ "$status" != 0 ] ; then + echo "tune2fs -O mmp failed with $status" > $test_name.failed + echo "failed" + return $status +fi + +$FSCK $FSCK_OPT $TMPFILE >> $test_name.log 2>&1 +status=$? +if [ "$status" = 0 ] ; then + echo "ok" + touch $test_name.ok +else + echo "e2fsck with MMP enabled failed with $status" > $test_name.failed + echo "failed" + return $status +fi +rm -f $TMPFILE
tests/f_mmp_2off/name new file +1 -0
@@ -0,0 +1 @@ +disable MMP using tune2fs
tests/f_mmp_2off/script new file +40 -0
@@ -0,0 +1,40 @@ +FSCK_OPT=-yf + +TMPFILE=test.img +rm -f $test_name.failed $test_name.ok +> $TMPFILE + +stat -f $TMPFILE | grep -q "Type: tmpfs" +if [ $? == 0 ]; then + rm -f $TMPFILE + echo "skipped for tmpfs (no O_DIRECT support)" + return 0 +fi + +$MKE2FS -q -F -o Linux -b 1024 -O mmp $TMPFILE 100 > $test_name.log 2>&1 +status=$? +if [ "$status" != 0 ] ; then + echo "mke2fs -O mmp failed" > $test_name.failed + echo "failed" + return $status +fi + +$TUNE2FS -O ^mmp $TMPFILE > $test_name.log 2>&1 +status=$? +if [ "$status" != 0 ] ; then + echo "tune2fs -O ^mmp failed" > $test_name.failed + echo "failed" + return $status +fi + +$FSCK $FSCK_OPT $TMPFILE > $test_name.log 2>&1 +status=$? +if [ "$status" = 0 ] ; then + echo "ok" + touch $test_name.ok +else + echo "e2fsck after MMP disable failed" > $test_name.failed + echo "failed" + return $status +fi +rm -f $TMPFILE
tests/f_mmp_e2fsck/name new file +1 -0
@@ -0,0 +1 @@ +disable MMP with tune2fs after e2fsck killed
tests/f_mmp_e2fsck/script new file +67 -0
67 lines not on this page · raw message
tests/f_mmp_garbage/expect.1 new file +9 -0
@@ -0,0 +1,9 @@ +Superblock has invalid MMP magic. Fix? yes + +Pass 1: Checking inodes, blocks, and sizes +Pass 2: Checking directory structure +Pass 3: Checking directory connectivity +Pass 4: Checking reference counts +Pass 5: Checking group summary information +test_filesys: 11/16 files (0.0% non-contiguous), 22/100 blocks +Exit status is 0
tests/f_mmp_garbage/expect.2 new file +7 -0
@@ -0,0 +1,7 @@ +Pass 1: Checking inodes, blocks, and sizes +Pass 2: Checking directory structure +Pass 3: Checking directory connectivity +Pass 4: Checking reference counts +Pass 5: Checking group summary information +test_filesys: 11/16 files (0.0% non-contiguous), 22/100 blocks +Exit status is 0
tests/f_mmp_garbage/name new file +1 -0
@@ -0,0 +1 @@ +repair MMP when it is corrupted
tests/f_mmp_garbage/script new file +28 -0
@@ -0,0 +1,28 @@ +FSCK_OPT=-yf + +TMPFILE=test.img +rm -f $test_name.failed $test_name.ok +> $TMPFILE + +stat -f $TMPFILE | grep -q "Type: tmpfs" +if [ $? == 0 ] ; then + rm -f $TMPFILE + echo "skipped for tmpfs (no O_DIRECT support)" + return 0 +fi + +echo "make the test image ..." > $test_name.log +$MKE2FS -q -F -o Linux -b 1024 -O mmp -E mmp_update_interval=1 $TMPFILE 100 >> $test_name.log 2>&1 +status=$? +if [ "$status" != 0 ] ; then + echo "mke2fs -O mmp failed" > $test_name.failed + echo "failed" + return $status +fi + +# create a corrupted image +echo "modify the mmp sequence ..." >> $test_name.log +$DEBUGFS -w -R "set_mmp_value magic 0x12345678" $TMPFILE >> $test_name.log 2>&1 + +SKIP_GUNZIP=true +. $cmd_dir/run_e2fsck
Signature · 1 line
1.7.3.4
Andreas, Johann,
thanks a lot for sending those patches! From a previous review I still remember ext2fs_mmp_read(), see below. I will look through the other parts later on.
Quoted 32 lines
lib/ext2fs/mmp.c new file +32
-0+errcode_t ext2fs_mmp_read (ext2_filsys fs,blk64_t mmp_blk,void *buf) +{ +struct mmp_struct *mmp_cmp; +errcode_t retval =0 ; + +if ((mmp_blk<= fs->super->s_first_data_block) || + (mmp_blk>= fs->super->s_blocks_count)) +return EXT2_ET_MMP_BAD_BLOCK ; + +if (fs->mmp_cmp ==NULL ) { +/* O_DIRECT in linux 2.4: page aligned +* O_DIRECT in linux 2.6: sector aligned +* A filesystem cannot be created with blocksize< sector size, +* or with blocksize> page_size. */ +int bufsize = fs->blocksize; + +if (bufsize<mmp_pagesize ()) + bufsize =mmp_pagesize (); + retval =ext2fs_get_memalign (bufsize, bufsize,&fs->mmp_cmp); +if (retval) +return retval; + } + +/* ext2fs_open reserves fd0,1,2 to avoid stdio collision */ +if (fs->mmp_fd<=0 ) { + fs->mmp_fd =open (fs->device_name,O_RDWR |O_DIRECT ); +if (fs->mmp_fd<0 ) { + retval =EXT2_ET_MMP_OPEN_DIRECT ; + goto out; + } + } +
lib/ext2fs/mmp.c line 87
Quoted 10 lines
lib/ext2fs/mmp.c new file +10
-0+if (ext2fs_llseek (fs->mmp_fd, mmp_blk * fs->blocksize,SEEK_SET ) != + mmp_blk * fs->blocksize) { + retval =EXT2_ET_LLSEEK_FAILED ; + goto out; + } + +if (read (fs->mmp_fd, fs->mmp_cmp, fs->blocksize) != fs->blocksize) { + retval =EXT2_ET_SHORT_READ ; + goto out; + }
While ext2fs_llseek() and read() works fine, I still wonder why the code does not use io_channel_read_blk64() here, similarly as io_channel_write_blk64() write in ext2fs_mmp_write().
Quoted 41 lines
lib/ext2fs/mmp.c new file +41
-0+ + mmp_cmp = fs->mmp_cmp; +#ifdef EXT2FS_ENABLE_SWAPFS +if (fs->flags&EXT2_FLAG_SWAP_BYTES ) +ext2fs_swap_mmp (mmp_cmp); +#endif + +if (buf !=NULL && buf != fs->mmp_cmp) +memcpy (buf, fs->mmp_cmp, fs->blocksize); + +if (mmp_cmp->mmp_magic !=EXT4_MMP_MAGIC ) { + retval =EXT2_ET_MMP_MAGIC_INVALID ; + goto out; + } + +out: +return retval; +} + +errcode_t ext2fs_mmp_write (ext2_filsys fs,blk64_t mmp_blk,void *buf) +{ +struct mmp_struct *mmp_s = buf; +struct timeval tv; +errcode_t retval =0 ; + +gettimeofday (&tv,0 ); + mmp_s->mmp_time = tv.tv_sec; + fs->mmp_last_written = tv.tv_sec; + +if (fs->super->s_mmp_block< fs->super->s_first_data_block || + fs->super->s_mmp_block>ext2fs_blocks_count (fs->super)) +return EXT2_ET_MMP_BAD_BLOCK ; + +#ifdef EXT2FS_ENABLE_SWAPFS +if (fs->super->s_magic ==ext2fs_swab16 (EXT2_SUPER_MAGIC )) +ext2fs_swap_mmp (mmp_s); +#endif + +/* I was tempted to make this use O_DIRECT and the mmp_fd, but +* this caused no end of grief, while leaving it as-is works. */ + retval =io_channel_write_blk64 (fs->io, mmp_blk, -fs->blocksize, buf);
lib/ext2fs/mmp.c line 138
Quoted 10 lines
lib/ext2fs/mmp.c new file +10
-0+ +#ifdef EXT2FS_ENABLE_SWAPFS +if (fs->super->s_magic ==ext2fs_swab16 (EXT2_SUPER_MAGIC )) +ext2fs_swap_mmp (mmp_s); +#endif + +/* Make sure the block gets to disk quickly */ +io_channel_flush (fs->io); +return retval; +}
Thanks,
Bernd
Quoted 18 lines · Bernd Schubert
thanks a lot for sending those patches! From a previous review I still remember
ext2fs_mmp_read(), see below. I will look through the other parts later on.Quoted 15 lines
lib/ext2fs/mmp.c new file +15
-0+errcode_t ext2fs_mmp_read (ext2_filsys fs,blk64_t mmp_blk,void *buf) +{ +/* ext2fs_open reserves fd0,1,2 to avoid stdio collision */ +if (fs->mmp_fd<=0 ) { + fs->mmp_fd =open (fs->device_name,O_RDWR |O_DIRECT ); +if (fs->mmp_fd<0 ) { + retval =EXT2_ET_MMP_OPEN_DIRECT ; + goto out; + } + } + +if (ext2fs_llseek (fs->mmp_fd, mmp_blk * fs->blocksize,SEEK_SET ) != + mmp_blk * fs->blocksize) { + retval =EXT2_ET_LLSEEK_FAILED ; + goto out;
Quoted 8 lines
lib/ext2fs/mmp.c line 87
Quoted 6 lines
lib/ext2fs/mmp.c new file +6
-0+ } + +if (read (fs->mmp_fd, fs->mmp_cmp, fs->blocksize) != fs->blocksize) { + retval =EXT2_ET_SHORT_READ ; + goto out; + }While
ext2fs_llseek()andread()works fine, I still wonder why the code does not useio_channel_read_blk64()here, similarly asio_channel_write_blk64()write inext2fs_mmp_write().
One of the reasons for doing a direct open+seek+read here is because previous versions of e2fsprogs did not allow O_DIRECT reads via io_channel_read_blk64(), which is critical for the correct MMP functioning. It cannot allow read from a cached block, either in libext2fs or in the VM, though a cached block on the disk is OK as long as it is visible to all hosts that are accessing it.
Cheers, Andreas
--
Andreas Dilger
Principal Engineer
Whamcloud, Inc.
Quoted 28 lines · Andreas Dilger
Quoted 18 lines · Bernd Schubert
thanks a lot for sending those patches! From a previous review I still remember
ext2fs_mmp_read(), see below. I will look through the other parts later on.Quoted 15 lines
lib/ext2fs/mmp.c new file +15
-0+errcode_t ext2fs_mmp_read (ext2_filsys fs,blk64_t mmp_blk,void *buf) +{ +/* ext2fs_open reserves fd0,1,2 to avoid stdio collision */ +if (fs->mmp_fd<=0 ) { + fs->mmp_fd =open (fs->device_name,O_RDWR |O_DIRECT ); +if (fs->mmp_fd<0 ) { + retval =EXT2_ET_MMP_OPEN_DIRECT ; + goto out; + } + } + +if (ext2fs_llseek (fs->mmp_fd, mmp_blk * fs->blocksize,SEEK_SET ) != + mmp_blk * fs->blocksize) { + retval =EXT2_ET_LLSEEK_FAILED ; + goto out;Quoted 8 lines
lib/ext2fs/mmp.c line 87
Quoted 6 lines
lib/ext2fs/mmp.c new file +6
-0+ } + +if (read (fs->mmp_fd, fs->mmp_cmp, fs->blocksize) != fs->blocksize) { + retval =EXT2_ET_SHORT_READ ; + goto out; + }While
ext2fs_llseek()andread()works fine, I still wonder why the code does not useio_channel_read_blk64()here, similarly asio_channel_write_blk64()write inext2fs_mmp_write().
Quoted 5 lines
One of the reasons for doing a direct open+seek+read here is because previous versions of e2fsprogs did not allow
O_DIRECTreads viaio_channel_read_blk64(), which is critical for the correct MMP functioning. It cannot allow read from a cached block, either in libext2fs or in the VM, though a cached block on the disk is OK as long as it is visible to all hosts that are accessing it.
Yeah, I understand that it had to be O_DIRECT, but I didn't know about (previous) limitation of io_channel_read_blk64(). Maybe you could add a comment about it to the code about that, if you want to keep it? Something like "/* We don't use io_channel_read_blk64() here to make sure blocks are really not cached */"?
Thanks,
Bernd