summaryrefslogtreecommitdiff
path: root/open-vm-tools/lib/syncDriver/vmSyncDriver.c
blob: f5e044f09d2c74969ed1b4226a795dc975651d5b (plain)
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
/*********************************************************
 * Copyright (C) 2011 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no 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 Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/**
 * @file vmSyncDriver.c
 *
 * A sync driver backend that uses VMware's "vmsync" driver.
 */

#include <fcntl.h>
#include <sys/ioctl.h>
#include "debug.h"
#include "syncDriverInt.h"
#include "syncDriverIoc.h"
#include "util.h"

#define SYNC_PROC_PATH "/proc/driver/vmware-sync"

typedef struct VmSyncDriver {
   SyncHandle     driver;
   int            fd;
} VmSyncDriver;


/*
 *******************************************************************************
 * VmSyncThaw --                                                          */ /**
 *
 * Thaws filesystems previously frozen.
 *
 * @param[in] handle Handle returned by the freeze operation.
 *
 * @return A SyncDriverErr.
 *
 *******************************************************************************
 */

static SyncDriverErr
VmSyncThaw(const SyncDriverHandle handle)
{
   const VmSyncDriver *sync = (VmSyncDriver *) handle;
   ASSERT(handle != SYNCDRIVER_INVALID_HANDLE);
   return ioctl(sync->fd, SYNC_IOC_THAW) != -1 ? SD_SUCCESS : SD_ERROR;
}


/*
 *******************************************************************************
 * VmSyncClose --                                                         */ /**
 *
 * Closes the descriptor used to talk to the vmsync driver and frees memory
 * associated with it.
 *
 * @param[in] handle Handle to close.
 *
 *******************************************************************************
 */

static void
VmSyncClose(SyncDriverHandle handle)
{
   VmSyncDriver *sync = (VmSyncDriver *) handle;
   close(sync->fd);
   free(sync);
}


/*
 *******************************************************************************
 * VmSync_Freeze --                                                       */ /**
 *
 * Tries to freeze the requested filesystems with the vmsync driver.
 *
 * Opens a description to the driver's proc node, and if successful, send an
 * ioctl to freeze the requested filesystems.
 *
 * @param[in]  paths    Paths to freeze (colon-delimited).
 * @param[out] handle   Where to store the handle to use for thawing.
 *
 * @return A SyncDriverErr.
 *
 *******************************************************************************
 */

SyncDriverErr
VmSync_Freeze(const char *paths,
              SyncDriverHandle *handle)
{
   int file;
   VmSyncDriver *sync = NULL;

   Debug(LGPFX "Freezing using vmsync driver...\n");

   file = open(SYNC_PROC_PATH, O_RDONLY);
   if (file == -1) {
      return SD_UNAVAILABLE;
   }

   sync = calloc(1, sizeof *sync);
   if (sync == NULL) {
      goto exit;
   }

   sync->driver.thaw = VmSyncThaw;
   sync->driver.close = VmSyncClose;
   sync->fd = file;

   if (ioctl(file, SYNC_IOC_FREEZE, paths) == -1) {
      free(sync);
      sync = NULL;
   }

exit:
   if (sync == NULL) {
      if (file != -1) {
         close(file);
      }
   } else {
      *handle = &sync->driver;
   }
   return sync != NULL ? SD_SUCCESS : SD_ERROR;
}