Imported Upstream version 1.47.0
[platform/upstream/e2fsprogs.git] / lib / ext2fs / mmp.c
index c21ae27..eb94170 100644 (file)
@@ -57,21 +57,21 @@ errcode_t ext2fs_mmp_read(ext2_filsys fs, blk64_t mmp_blk, void *buf)
         * regardless of how the io_manager is doing reads, to avoid caching of
         * the MMP block by the io_manager or the VM.  It needs to be fresh. */
        if (fs->mmp_fd <= 0) {
-               int flags = O_RDWR | O_DIRECT;
+               struct stat st;
+               int flags = O_RDONLY | O_DIRECT;
+
+               /*
+                * There is no reason for using O_DIRECT if we're working with
+                * regular file. Disabling it also avoids problems with
+                * alignment when the device of the host file system has sector
+                * size larger than blocksize of the fs we're working with.
+                */
+               if (stat(fs->device_name, &st) == 0 &&
+                   S_ISREG(st.st_mode))
+                       flags &= ~O_DIRECT;
 
-retry:
                fs->mmp_fd = open(fs->device_name, flags);
                if (fs->mmp_fd < 0) {
-                       struct stat st;
-
-                       /* Avoid O_DIRECT for filesystem image files if open
-                        * fails, since it breaks when running on tmpfs. */
-                       if (errno == EINVAL && (flags & O_DIRECT) &&
-                           stat(fs->device_name, &st) == 0 &&
-                           S_ISREG(st.st_mode)) {
-                               flags &= ~O_DIRECT;
-                               goto retry;
-                       }
                        retval = EXT2_ET_MMP_OPEN_DIRECT;
                        goto out;
                }
@@ -356,7 +356,7 @@ clean_seq:
 #ifdef HAVE_GETHOSTNAME
        gethostname((char *) mmp_s->mmp_nodename, sizeof(mmp_s->mmp_nodename));
 #else
-       strcpy(mmp_s->mmp_nodename, "unknown host");
+       strcpy((char *) mmp_s->mmp_nodename, "unknown host");
 #endif
        strncpy((char *) mmp_s->mmp_bdevname, fs->device_name,
                sizeof(mmp_s->mmp_bdevname));
@@ -403,10 +403,11 @@ errcode_t ext2fs_mmp_stop(ext2_filsys fs)
        errcode_t retval = 0;
 
        if (!ext2fs_has_feature_mmp(fs->super) ||
-           !(fs->flags & EXT2_FLAG_RW) || (fs->flags & EXT2_FLAG_SKIP_MMP))
+           !(fs->flags & EXT2_FLAG_RW) || (fs->flags & EXT2_FLAG_SKIP_MMP) ||
+           (fs->mmp_buf == NULL) || (fs->mmp_cmp == NULL))
                goto mmp_error;
 
-       retval = ext2fs_mmp_read(fs, fs->super->s_mmp_block, fs->mmp_buf);
+       retval = ext2fs_mmp_read(fs, fs->super->s_mmp_block, NULL);
        if (retval)
                goto mmp_error;