Whamcloud - gitweb
e2fsprogs: fix compile error and warnings for old gcc versions
[tools/e2fsprogs.git] / lib / ext2fs / mmp.c
index b27d9a4..2da935e 100644 (file)
@@ -4,14 +4,19 @@
  * Copyright (C) 2011 Whamcloud, Inc.
  *
  * %Begin-Header%
- * This file may be redistributed under the terms of the GNU Public
- * License.
+ * This file may be redistributed under the terms of the GNU Library
+ * General Public License, version 2.
  * %End-Header%
  */
 
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE
 #endif
+#ifndef _DEFAULT_SOURCE
+#define _DEFAULT_SOURCE        /* since glibc 2.20 _SVID_SOURCE is deprecated */
+#endif
+
+#include "config.h"
 
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #include "ext2fs/ext2_fs.h"
 #include "ext2fs/ext2fs.h"
 
-static int mmp_pagesize(void)
-{
-#ifdef _SC_PAGESIZE
-       int sysval = sysconf(_SC_PAGESIZE);
-       if (sysval > 0)
-               return sysval;
-#endif /* _SC_PAGESIZE */
-#ifdef HAVE_GETPAGESIZE
-       return getpagesize();
-#else
-       return 4096;
-#endif
-}
-
 #ifndef O_DIRECT
 #define O_DIRECT 0
 #endif
 
+#if __GNUC_PREREQ (4, 6)
+#pragma GCC diagnostic push
+#ifndef CONFIG_MMP
+#pragma GCC diagnostic ignored "-Wunused-parameter"
+#endif
+#endif
+
 errcode_t ext2fs_mmp_read(ext2_filsys fs, blk64_t mmp_blk, void *buf)
 {
+#ifdef CONFIG_MMP
        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))
+           (mmp_blk >= ext2fs_blocks_count(fs->super)))
                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, so checking
         * mmp_fd <= 0 is OK to validate that the fd is valid.  This opens its
         * own fd to read the MMP block to ensure that it is using O_DIRECT,
@@ -79,7 +64,17 @@ errcode_t ext2fs_mmp_read(ext2_filsys fs, blk64_t mmp_blk, void *buf)
                }
        }
 
-       if (ext2fs_llseek(fs->mmp_fd, mmp_blk * fs->blocksize, SEEK_SET) !=
+       if (fs->mmp_cmp == NULL) {
+               int align = ext2fs_get_dio_alignment(fs->mmp_fd);
+
+               retval = ext2fs_get_memalign(fs->blocksize, align,
+                                            &fs->mmp_cmp);
+               if (retval)
+                       return retval;
+       }
+
+       if ((blk64_t) ext2fs_llseek(fs->mmp_fd, mmp_blk * fs->blocksize,
+                                   SEEK_SET) !=
            mmp_blk * fs->blocksize) {
                retval = EXT2_ET_LLSEEK_FAILED;
                goto out;
@@ -91,6 +86,11 @@ errcode_t ext2fs_mmp_read(ext2_filsys fs, blk64_t mmp_blk, void *buf)
        }
 
        mmp_cmp = fs->mmp_cmp;
+
+       if (!(fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) &&
+           !ext2fs_mmp_csum_verify(fs, mmp_cmp))
+               retval = EXT2_ET_MMP_CSUM_INVALID;
+
 #ifdef WORDS_BIGENDIAN
        ext2fs_swap_mmp(mmp_cmp);
 #endif
@@ -105,10 +105,14 @@ errcode_t ext2fs_mmp_read(ext2_filsys fs, blk64_t mmp_blk, void *buf)
 
 out:
        return retval;
+#else
+       return EXT2_ET_OP_NOT_SUPPORTED;
+#endif
 }
 
 errcode_t ext2fs_mmp_write(ext2_filsys fs, blk64_t mmp_blk, void *buf)
 {
+#ifdef CONFIG_MMP
        struct mmp_struct *mmp_s = buf;
        struct timeval tv;
        errcode_t retval = 0;
@@ -125,6 +129,10 @@ errcode_t ext2fs_mmp_write(ext2_filsys fs, blk64_t mmp_blk, void *buf)
        ext2fs_swap_mmp(mmp_s);
 #endif
 
+       retval = ext2fs_mmp_csum_set(fs, mmp_s);
+       if (retval)
+               return retval;
+
        /* 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, -(int)sizeof(struct mmp_struct), buf);
@@ -136,6 +144,9 @@ errcode_t ext2fs_mmp_write(ext2_filsys fs, blk64_t mmp_blk, void *buf)
        /* Make sure the block gets to disk quickly */
        io_channel_flush(fs->io);
        return retval;
+#else
+       return EXT2_ET_OP_NOT_SUPPORTED;
+#endif
 }
 
 #ifdef HAVE_SRANDOM
@@ -143,8 +154,9 @@ errcode_t ext2fs_mmp_write(ext2_filsys fs, blk64_t mmp_blk, void *buf)
 #define rand()         random()
 #endif
 
-unsigned ext2fs_mmp_new_seq()
+unsigned ext2fs_mmp_new_seq(void)
 {
+#ifdef CONFIG_MMP
        unsigned new_seq;
        struct timeval tv;
 
@@ -161,8 +173,12 @@ unsigned ext2fs_mmp_new_seq()
        } while (new_seq > EXT4_MMP_SEQ_MAX);
 
        return new_seq;
+#else
+       return EXT2_ET_OP_NOT_SUPPORTED;
+#endif
 }
 
+#ifdef CONFIG_MMP
 static errcode_t ext2fs_mmp_reset(ext2_filsys fs)
 {
        struct mmp_struct *mmp_s = NULL;
@@ -180,7 +196,7 @@ static errcode_t ext2fs_mmp_reset(ext2_filsys fs)
        mmp_s->mmp_magic = EXT4_MMP_MAGIC;
        mmp_s->mmp_seq = EXT4_MMP_SEQ_CLEAN;
        mmp_s->mmp_time = 0;
-#if _BSD_SOURCE || _XOPEN_SOURCE >= 500
+#ifdef HAVE_GETHOSTNAME
        gethostname(mmp_s->mmp_nodename, sizeof(mmp_s->mmp_nodename));
 #else
        mmp_s->mmp_nodename[0] = '\0';
@@ -196,9 +212,16 @@ static errcode_t ext2fs_mmp_reset(ext2_filsys fs)
 out:
        return retval;
 }
+#endif
+
+errcode_t ext2fs_mmp_update(ext2_filsys fs)
+{
+       return ext2fs_mmp_update2(fs, 0);
+}
 
 errcode_t ext2fs_mmp_clear(ext2_filsys fs)
 {
+#ifdef CONFIG_MMP
        errcode_t retval = 0;
 
        if (!(fs->flags & EXT2_FLAG_RW))
@@ -207,10 +230,14 @@ errcode_t ext2fs_mmp_clear(ext2_filsys fs)
        retval = ext2fs_mmp_reset(fs);
 
        return retval;
+#else
+       return EXT2_ET_OP_NOT_SUPPORTED;
+#endif
 }
 
 errcode_t ext2fs_mmp_init(ext2_filsys fs)
 {
+#ifdef CONFIG_MMP
        struct ext2_super_block *sb = fs->super;
        blk64_t mmp_block;
        errcode_t retval;
@@ -239,13 +266,21 @@ errcode_t ext2fs_mmp_init(ext2_filsys fs)
 
 out:
        return retval;
+#else
+       return EXT2_ET_OP_NOT_SUPPORTED;
+#endif
 }
 
+#ifndef min
+#define min(x, y) ((x) < (y) ? (x) : (y))
+#endif
+
 /*
  * Make sure that the fs is not mounted or being fsck'ed while opening the fs.
  */
 errcode_t ext2fs_mmp_start(ext2_filsys fs)
 {
+#ifdef CONFIG_MMP
        struct mmp_struct *mmp_s;
        unsigned seq;
        unsigned int mmp_check_interval;
@@ -287,7 +322,7 @@ errcode_t ext2fs_mmp_start(ext2_filsys fs)
        if (mmp_s->mmp_check_interval > mmp_check_interval)
                mmp_check_interval = mmp_s->mmp_check_interval;
 
-       sleep(2 * mmp_check_interval + 1);
+       sleep(min(mmp_check_interval * 2 + 1, mmp_check_interval + 60));
 
        retval = ext2fs_mmp_read(fs, fs->super->s_mmp_block, fs->mmp_buf);
        if (retval)
@@ -303,7 +338,7 @@ clean_seq:
                goto mmp_error;
 
        mmp_s->mmp_seq = seq = ext2fs_mmp_new_seq();
-#if _BSD_SOURCE || _XOPEN_SOURCE >= 500
+#ifdef HAVE_GETHOSTNAME
        gethostname(mmp_s->mmp_nodename, sizeof(mmp_s->mmp_nodename));
 #else
        strcpy(mmp_s->mmp_nodename, "unknown host");
@@ -315,7 +350,7 @@ clean_seq:
        if (retval)
                goto mmp_error;
 
-       sleep(2 * mmp_check_interval + 1);
+       sleep(min(2 * mmp_check_interval + 1, mmp_check_interval + 60));
 
        retval = ext2fs_mmp_read(fs, fs->super->s_mmp_block, fs->mmp_buf);
        if (retval)
@@ -335,6 +370,9 @@ clean_seq:
 
 mmp_error:
        return retval;
+#else
+       return EXT2_ET_OP_NOT_SUPPORTED;
+#endif
 }
 
 /*
@@ -345,10 +383,11 @@ mmp_error:
  */
 errcode_t ext2fs_mmp_stop(ext2_filsys fs)
 {
+#ifdef CONFIG_MMP
        struct mmp_struct *mmp, *mmp_cmp;
        errcode_t retval = 0;
 
-       if (!(fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) ||
+       if (!ext2fs_has_feature_mmp(fs->super) ||
            !(fs->flags & EXT2_FLAG_RW) || (fs->flags & EXT2_FLAG_SKIP_MMP))
                goto mmp_error;
 
@@ -374,6 +413,13 @@ mmp_error:
        }
 
        return retval;
+#else
+       if (!ext2fs_has_feature_mmp(fs->super) ||
+           !(fs->flags & EXT2_FLAG_RW) || (fs->flags & EXT2_FLAG_SKIP_MMP))
+               return 0;
+
+       return EXT2_ET_OP_NOT_SUPPORTED;
+#endif
 }
 
 #define EXT2_MIN_MMP_UPDATE_INTERVAL 60
@@ -381,18 +427,20 @@ mmp_error:
 /*
  * Update the on-disk mmp buffer, after checking that it hasn't been changed.
  */
-errcode_t ext2fs_mmp_update(ext2_filsys fs)
+errcode_t ext2fs_mmp_update2(ext2_filsys fs, int immediately)
 {
+#ifdef CONFIG_MMP
        struct mmp_struct *mmp, *mmp_cmp;
        struct timeval tv;
        errcode_t retval = 0;
 
-       if (!(fs->super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_MMP) ||
+       if (!ext2fs_has_feature_mmp(fs->super) ||
            !(fs->flags & EXT2_FLAG_RW) || (fs->flags & EXT2_FLAG_SKIP_MMP))
                return 0;
 
        gettimeofday(&tv, 0);
-       if (tv.tv_sec - fs->mmp_last_written < EXT2_MIN_MMP_UPDATE_INTERVAL)
+       if (!immediately &&
+           tv.tv_sec - fs->mmp_last_written < EXT2_MIN_MMP_UPDATE_INTERVAL)
                return 0;
 
        retval = ext2fs_mmp_read(fs, fs->super->s_mmp_block, NULL);
@@ -411,4 +459,14 @@ errcode_t ext2fs_mmp_update(ext2_filsys fs)
 
 mmp_error:
        return retval;
+#else
+       if (!ext2fs_has_feature_mmp(fs->super) ||
+           !(fs->flags & EXT2_FLAG_RW) || (fs->flags & EXT2_FLAG_SKIP_MMP))
+               return 0;
+
+       return EXT2_ET_OP_NOT_SUPPORTED;
+#endif
 }
+#if __GNUC_PREREQ (4, 6)
+#pragma GCC diagnostic pop
+#endif