storage_backend.h 14.4 KB
Newer Older
1 2 3
/*
 * storage_backend.h: internal storage driver backend contract
 *
4
 * Copyright (C) 2007-2010, 2012-2014 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2007-2008 Daniel P. Berrange
 *
 * This library 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; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24
 *
 * Author: Daniel P. Berrange <berrange@redhat.com>
 */

#ifndef __VIR_STORAGE_BACKEND_H__
25
# define __VIR_STORAGE_BACKEND_H__
26

E
Eric Blake 已提交
27 28
# include <sys/stat.h>

29 30
# include "internal.h"
# include "storage_conf.h"
31
# include "vircommand.h"
32
# include "storage_driver.h"
33

34 35 36
typedef char * (*virStorageBackendFindPoolSources)(virConnectPtr conn,
                                                   const char *srcSpec,
                                                   unsigned int flags);
37
typedef int (*virStorageBackendCheckPool)(virStoragePoolObjPtr pool,
38 39 40 41 42 43 44 45 46 47 48 49 50
                                          bool *active);
typedef int (*virStorageBackendStartPool)(virConnectPtr conn,
                                          virStoragePoolObjPtr pool);
typedef int (*virStorageBackendBuildPool)(virConnectPtr conn,
                                          virStoragePoolObjPtr pool,
                                          unsigned int flags);
typedef int (*virStorageBackendRefreshPool)(virConnectPtr conn,
                                            virStoragePoolObjPtr pool);
typedef int (*virStorageBackendStopPool)(virConnectPtr conn,
                                         virStoragePoolObjPtr pool);
typedef int (*virStorageBackendDeletePool)(virConnectPtr conn,
                                           virStoragePoolObjPtr pool,
                                           unsigned int flags);
51 52 53 54 55 56 57 58 59 60

/* A 'buildVol' backend must remove any volume created on error since
 * the storage driver does not distinguish whether the failure is due
 * to failure to create the volume, to reserve any space necessary for
 * the volume, to get data about the volume, to change it's accessibility,
 * etc. This avoids issues arising from a creation failure due to some
 * external action which created a volume of the same name that libvirt
 * was not aware of between checking the pool and the create attempt. It
 * also avoids extra round trips to just delete a file.
 */
61
typedef int (*virStorageBackendBuildVol)(virConnectPtr conn,
62 63
                                         virStoragePoolObjPtr pool,
                                         virStorageVolDefPtr vol,
64
                                         unsigned int flags);
65 66 67 68 69 70 71 72 73 74 75 76 77 78
typedef int (*virStorageBackendCreateVol)(virConnectPtr conn,
                                          virStoragePoolObjPtr pool,
                                          virStorageVolDefPtr vol);
typedef int (*virStorageBackendRefreshVol)(virConnectPtr conn,
                                           virStoragePoolObjPtr pool,
                                           virStorageVolDefPtr vol);
typedef int (*virStorageBackendDeleteVol)(virConnectPtr conn,
                                          virStoragePoolObjPtr pool,
                                          virStorageVolDefPtr vol,
                                          unsigned int flags);
typedef int (*virStorageBackendBuildVolFrom)(virConnectPtr conn,
                                             virStoragePoolObjPtr pool,
                                             virStorageVolDefPtr origvol,
                                             virStorageVolDefPtr newvol,
79
                                             unsigned int flags);
80 81 82 83 84
typedef int (*virStorageBackendVolumeResize)(virConnectPtr conn,
                                             virStoragePoolObjPtr pool,
                                             virStorageVolDefPtr vol,
                                             unsigned long long capacity,
                                             unsigned int flags);
85 86 87 88 89 90 91 92 93 94 95 96 97 98
typedef int (*virStorageBackendVolumeDownload)(virConnectPtr conn,
                                               virStoragePoolObjPtr obj,
                                               virStorageVolDefPtr vol,
                                               virStreamPtr stream,
                                               unsigned long long offset,
                                               unsigned long long length,
                                               unsigned int flags);
typedef int (*virStorageBackendVolumeUpload)(virConnectPtr conn,
                                             virStoragePoolObjPtr obj,
                                             virStorageVolDefPtr vol,
                                             virStreamPtr stream,
                                             unsigned long long offset,
                                             unsigned long long len,
                                             unsigned int flags);
99 100 101 102 103
typedef int (*virStorageBackendVolumeWipe)(virConnectPtr conn,
                                           virStoragePoolObjPtr pool,
                                           virStorageVolDefPtr vol,
                                           unsigned int algorithm,
                                           unsigned int flags);
104

105 106
/* File creation/cloning functions used for cloning between backends */
int virStorageBackendCreateRaw(virConnectPtr conn,
107
                               virStoragePoolObjPtr pool,
108
                               virStorageVolDefPtr vol,
109 110
                               virStorageVolDefPtr inputvol,
                               unsigned int flags);
111

112 113 114 115 116 117
int virStorageBackendCreateQemuImg(virConnectPtr conn,
                                   virStoragePoolObjPtr pool,
                                   virStorageVolDefPtr vol,
                                   virStorageVolDefPtr inputvol,
                                   unsigned int flags);

118 119 120 121 122 123
int virStorageBackendCreatePloop(virConnectPtr conn,
                                 virStoragePoolObjPtr pool,
                                 virStorageVolDefPtr vol,
                                 virStorageVolDefPtr inputvol,
                                 unsigned int flags);

124 125
int virStoragePloopResize(virStorageVolDefPtr vol,
                          unsigned long long capacity);
126

127 128 129 130 131
int virStorageBackendRedoPloopUpdate(virStorageSourcePtr target,
                                     struct stat *sb, int *fd,
                                     unsigned int flags);
bool virStorageBackendIsPloopDir(char *path);

132
virStorageBackendBuildVolFrom
133
virStorageBackendGetBuildVolFromFunction(virStorageVolDefPtr vol,
134 135
                                         virStorageVolDefPtr inputvol);

136 137
int virStorageBackendFindGlusterPoolSources(const char *host,
                                            int pooltype,
138 139
                                            virStoragePoolSourceListPtr list,
                                            bool report);
140

141 142 143 144 145 146 147 148 149 150 151 152 153 154
int virStorageBackendVolUploadLocal(virConnectPtr conn,
                                    virStoragePoolObjPtr pool,
                                    virStorageVolDefPtr vol,
                                    virStreamPtr stream,
                                    unsigned long long offset,
                                    unsigned long long len,
                                    unsigned int flags);
int virStorageBackendVolDownloadLocal(virConnectPtr conn,
                                      virStoragePoolObjPtr pool,
                                      virStorageVolDefPtr vol,
                                      virStreamPtr stream,
                                      unsigned long long offset,
                                      unsigned long long len,
                                      unsigned int flags);
155

156 157 158 159 160 161
int virStorageBackendVolWipeLocal(virConnectPtr conn,
                                  virStoragePoolObjPtr pool,
                                  virStorageVolDefPtr vol,
                                  unsigned int algorithm,
                                  unsigned int flags);

162
bool virStorageBackendDeviceIsEmpty(const char *devpath,
163 164
                                    const char *format,
                                    bool writelabel);
165

166 167 168
typedef struct _virStorageBackend virStorageBackend;
typedef virStorageBackend *virStorageBackendPtr;

169 170
/* Callbacks are optional unless documented otherwise; but adding more
 * callbacks provides better pool support.  */
171 172 173
struct _virStorageBackend {
    int type;

174
    virStorageBackendFindPoolSources findPoolSources;
175
    virStorageBackendCheckPool checkPool;
176 177
    virStorageBackendStartPool startPool;
    virStorageBackendBuildPool buildPool;
178
    virStorageBackendRefreshPool refreshPool; /* Must be non-NULL */
179 180 181
    virStorageBackendStopPool stopPool;
    virStorageBackendDeletePool deletePool;

182
    virStorageBackendBuildVol buildVol;
183
    virStorageBackendBuildVolFrom buildVolFrom;
184 185 186
    virStorageBackendCreateVol createVol;
    virStorageBackendRefreshVol refreshVol;
    virStorageBackendDeleteVol deleteVol;
187
    virStorageBackendVolumeResize resizeVol;
188 189
    virStorageBackendVolumeUpload uploadVol;
    virStorageBackendVolumeDownload downloadVol;
190
    virStorageBackendVolumeWipe wipeVol;
191 192 193
};

virStorageBackendPtr virStorageBackendForType(int type);
194

195 196
/* VolOpenCheckMode flags */
enum {
197 198 199 200 201 202
    VIR_STORAGE_VOL_OPEN_NOERROR = 1 << 0, /* don't error if unexpected type
                                            * encountered, just warn */
    VIR_STORAGE_VOL_OPEN_REG     = 1 << 1, /* regular files okay */
    VIR_STORAGE_VOL_OPEN_BLOCK   = 1 << 2, /* block files okay */
    VIR_STORAGE_VOL_OPEN_CHAR    = 1 << 3, /* char files okay */
    VIR_STORAGE_VOL_OPEN_DIR     = 1 << 4, /* directories okay */
203 204
};

J
John Ferlan 已提交
205 206 207 208 209 210 211 212 213
/* VolReadErrorMode flags
 * If flag is present, then operation won't cause fatal error for
 * specified operation, rather a VIR_WARN will be issued and a -2 returned
 * for function call
 */
enum {
    VIR_STORAGE_VOL_READ_NOERROR    = 1 << 0, /* ignore *read errors */
};

214
# define VIR_STORAGE_VOL_OPEN_DEFAULT (VIR_STORAGE_VOL_OPEN_REG      |\
215
                                       VIR_STORAGE_VOL_OPEN_BLOCK)
216

217 218
int virStorageBackendVolOpen(const char *path, struct stat *sb,
                             unsigned int flags)
E
Eric Blake 已提交
219 220
    ATTRIBUTE_RETURN_CHECK
    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
221

222 223 224
# define VIR_STORAGE_DEFAULT_POOL_PERM_MODE 0755
# define VIR_STORAGE_DEFAULT_VOL_PERM_MODE  0600

225
int virStorageBackendUpdateVolInfo(virStorageVolDefPtr vol,
226
                                   bool withBlockVolFormat,
227 228
                                   unsigned int openflags,
                                   unsigned int readflags);
229
int virStorageBackendUpdateVolTargetInfo(virStorageSourcePtr target,
230
                                         bool withBlockVolFormat,
231 232
                                         unsigned int openflags,
                                         unsigned int readflags);
233
int virStorageBackendUpdateVolTargetInfoFD(virStorageSourcePtr target,
234
                                           int fd,
235
                                           struct stat *sb);
236

237
bool virStorageBackendPoolPathIsStable(const char *path);
238
char *virStorageBackendStablePath(virStoragePoolObjPtr pool,
239
                                  const char *devpath,
240
                                  bool loop);
241

242
virCommandPtr
243 244 245 246 247 248
virStorageBackendCreateQemuImgCmdFromVol(virConnectPtr conn,
                                         virStoragePoolObjPtr pool,
                                         virStorageVolDefPtr vol,
                                         virStorageVolDefPtr inputvol,
                                         unsigned int flags,
                                         const char *create_tool,
249 250
                                         int imgformat,
                                         const char *secretPath);
251

252
/* ------- virStorageFile backends ------------ */
253 254 255 256 257 258
typedef struct _virStorageFileBackend virStorageFileBackend;
typedef virStorageFileBackend *virStorageFileBackendPtr;

struct _virStorageDriverData {
    virStorageFileBackendPtr backend;
    void *priv;
259 260 261

    uid_t uid;
    gid_t gid;
262 263
};

264
typedef int
265
(*virStorageFileBackendInit)(virStorageSourcePtr src);
266 267

typedef void
268
(*virStorageFileBackendDeinit)(virStorageSourcePtr src);
269 270

typedef int
271
(*virStorageFileBackendCreate)(virStorageSourcePtr src);
272 273

typedef int
274
(*virStorageFileBackendUnlink)(virStorageSourcePtr src);
275 276

typedef int
277
(*virStorageFileBackendStat)(virStorageSourcePtr src,
278 279
                             struct stat *st);

280 281 282 283 284
typedef ssize_t
(*virStorageFileBackendReadHeader)(virStorageSourcePtr src,
                                   ssize_t max_len,
                                   char **buf);

285 286
typedef const char *
(*virStorageFileBackendGetUniqueIdentifier)(virStorageSourcePtr src);
287

288 289 290 291
typedef int
(*virStorageFileBackendAccess)(virStorageSourcePtr src,
                               int mode);

292
typedef int
293
(*virStorageFileBackendChown)(const virStorageSource *src,
294 295 296
                              uid_t uid,
                              gid_t gid);

297
virStorageFileBackendPtr virStorageFileBackendForType(int type, int protocol);
298 299 300
virStorageFileBackendPtr virStorageFileBackendForTypeInternal(int type,
                                                              int protocol,
                                                              bool report);
301 302


303 304 305 306 307 308 309 310 311 312
struct _virStorageFileBackend {
    int type;
    int protocol;

    /* All storage file callbacks may be omitted if not implemented */

    /* The following group of callbacks is expected to set a libvirt
     * error on failure. */
    virStorageFileBackendInit backendInit;
    virStorageFileBackendDeinit backendDeinit;
313
    virStorageFileBackendReadHeader storageFileReadHeader;
314
    virStorageFileBackendGetUniqueIdentifier storageFileGetUniqueIdentifier;
315 316 317 318 319 320

    /* The following group of callbacks is expected to set errno
     * and return -1 on error. No libvirt error shall be reported */
    virStorageFileBackendCreate storageFileCreate;
    virStorageFileBackendUnlink storageFileUnlink;
    virStorageFileBackendStat   storageFileStat;
321
    virStorageFileBackendAccess storageFileAccess;
322
    virStorageFileBackendChown  storageFileChown;
323 324
};

325
#endif /* __VIR_STORAGE_BACKEND_H__ */