1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
/* libguestfs - the guestfsd daemon
* Copyright (C) 2009-2020 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/types.h>
#include "daemon.h"
#include "actions.h"
#ifdef WIN32
static int sync_win32 (void);
#endif
#ifdef HAVE_FSYNC
static void fsync_devices (void);
#endif
int
do_sync (void)
{
if (sync_disks () == -1) {
reply_with_perror ("sync");
return -1;
}
return 0;
}
/* This is a replacement for sync(2) which is called from
* this file and from other places in the daemon. It works
* on Windows too.
*/
int
sync_disks (void)
{
#if defined(HAVE_SYNC)
sync ();
/* On Linux, sync(2) doesn't perform a barrier, so qemu (which may
* have a writeback cache, even with cache=none) will still have
* some unwritten data. Force the data out of any qemu caches, by
* calling fsync on all block devices. Note we still need the
* call to sync above in order to schedule the writes.
* Thanks to: Avi Kivity, Kevin Wolf.
*/
#ifdef HAVE_FSYNC
fsync_devices ();
#endif
return 0;
#elif defined(WIN32)
return sync_win32 ();
#else
#error "no known sync() API"
#endif
}
#ifdef HAVE_FSYNC
static void
fsync_devices (void)
{
DIR *dir;
struct dirent *d;
int fd;
dir = opendir ("/sys/block");
if (!dir) {
perror ("opendir: /sys/block");
return;
}
for (;;) {
errno = 0;
d = readdir (dir);
if (!d) break;
if (STREQLEN (d->d_name, "sd", 2) ||
STREQLEN (d->d_name, "hd", 2) ||
STREQLEN (d->d_name, "ubd", 3) ||
STREQLEN (d->d_name, "vd", 2) ||
STREQLEN (d->d_name, "sr", 2)) {
CLEANUP_FREE char *dev_path = NULL;
if (asprintf (&dev_path, "/dev/%s", d->d_name) == -1) {
perror ("asprintf");
continue;
}
/* Ignore the root device. */
if (is_root_device (dev_path))
continue;
fd = open (dev_path, O_RDONLY|O_CLOEXEC);
if (fd == -1) {
perror (dev_path);
continue;
}
/* fsync the device. */
if (verbose)
fprintf (stderr, "fsync %s\n", dev_path);
if (fsync (fd) == -1)
perror ("fsync");
if (close (fd) == -1)
perror ("close");
}
}
/* Check readdir didn't fail */
if (errno != 0)
perror ("readdir: /sys/block");
/* Close the directory handle */
if (closedir (dir) == -1)
perror ("closedir: /sys/block");
}
#endif /* HAVE_FSYNC */
#ifdef WIN32
static int
sync_win32 (void)
{
DWORD n1, n2;
n1 = GetLogicalDriveStrings (0, NULL);
if (n1 == 0)
return -1;
TCHAR buffer[n1+2]; /* sic */
n2 = GetLogicalDriveStrings (n1, buffer);
if (n2 == 0)
return -1;
TCHAR *p = buffer;
/* The MSDN example code itself assumes that there is always one
* drive in the system. However we will be better than that and not
* make the assumption ...
*/
while (*p) {
HANDLE drive;
DWORD drive_type;
/* Ignore removable drives. */
drive_type = GetDriveType (p);
if (drive_type == DRIVE_FIXED) {
/* To open the volume you have to specify the volume name, not
* the mount point. MSDN documents use of the constant 50
* below.
*/
TCHAR volname[50];
if (!GetVolumeNameForVolumeMountPoint (p, volname, 50))
return -1;
drive = CreateFile (volname, GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, 0);
if (drive == INVALID_HANDLE_VALUE)
return -1;
BOOL r;
/* This always fails in Wine:
* http://bugs.winehq.org/show_bug.cgi?id=14915
*/
r = FlushFileBuffers (drive);
CloseHandle (drive);
if (!r)
return -1;
}
/* Skip to next \0 character. */
while (*p++);
}
return 0;
}
#endif /* WIN32 */
|