Imported Upstream version 1.2.5
[archive/platform/upstream/libvirt.git] / src / storage / storage_backend.h
1 /*
2  * storage_backend.h: internal storage driver backend contract
3  *
4  * Copyright (C) 2007-2010, 2012-2014 Red Hat, Inc.
5  * Copyright (C) 2007-2008 Daniel P. Berrange
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library.  If not, see
19  * <http://www.gnu.org/licenses/>.
20  *
21  * Author: Daniel P. Berrange <berrange@redhat.com>
22  */
23
24 #ifndef __VIR_STORAGE_BACKEND_H__
25 # define __VIR_STORAGE_BACKEND_H__
26
27 # include <sys/stat.h>
28
29 # include "internal.h"
30 # include "storage_conf.h"
31 # include "vircommand.h"
32 # include "storage_driver.h"
33
34 typedef char * (*virStorageBackendFindPoolSources)(virConnectPtr conn,
35                                                    const char *srcSpec,
36                                                    unsigned int flags);
37 typedef int (*virStorageBackendCheckPool)(virConnectPtr conn,
38                                           virStoragePoolObjPtr pool,
39                                           bool *active);
40 typedef int (*virStorageBackendStartPool)(virConnectPtr conn,
41                                           virStoragePoolObjPtr pool);
42 typedef int (*virStorageBackendBuildPool)(virConnectPtr conn,
43                                           virStoragePoolObjPtr pool,
44                                           unsigned int flags);
45 typedef int (*virStorageBackendRefreshPool)(virConnectPtr conn,
46                                             virStoragePoolObjPtr pool);
47 typedef int (*virStorageBackendStopPool)(virConnectPtr conn,
48                                          virStoragePoolObjPtr pool);
49 typedef int (*virStorageBackendDeletePool)(virConnectPtr conn,
50                                            virStoragePoolObjPtr pool,
51                                            unsigned int flags);
52 typedef int (*virStorageBackendBuildVol)(virConnectPtr conn,
53                                          virStoragePoolObjPtr pool,
54                                          virStorageVolDefPtr vol,
55                                          unsigned int flags);
56 typedef int (*virStorageBackendCreateVol)(virConnectPtr conn,
57                                           virStoragePoolObjPtr pool,
58                                           virStorageVolDefPtr vol);
59 typedef int (*virStorageBackendRefreshVol)(virConnectPtr conn,
60                                            virStoragePoolObjPtr pool,
61                                            virStorageVolDefPtr vol);
62 typedef int (*virStorageBackendDeleteVol)(virConnectPtr conn,
63                                           virStoragePoolObjPtr pool,
64                                           virStorageVolDefPtr vol,
65                                           unsigned int flags);
66 typedef int (*virStorageBackendBuildVolFrom)(virConnectPtr conn,
67                                              virStoragePoolObjPtr pool,
68                                              virStorageVolDefPtr origvol,
69                                              virStorageVolDefPtr newvol,
70                                              unsigned int flags);
71 typedef int (*virStorageBackendVolumeResize)(virConnectPtr conn,
72                                              virStoragePoolObjPtr pool,
73                                              virStorageVolDefPtr vol,
74                                              unsigned long long capacity,
75                                              unsigned int flags);
76
77 /* File creation/cloning functions used for cloning between backends */
78 int virStorageBackendCreateRaw(virConnectPtr conn,
79                                virStoragePoolObjPtr pool,
80                                virStorageVolDefPtr vol,
81                                virStorageVolDefPtr inputvol,
82                                unsigned int flags);
83 virStorageBackendBuildVolFrom
84 virStorageBackendGetBuildVolFromFunction(virStorageVolDefPtr vol,
85                                          virStorageVolDefPtr inputvol);
86 int virStorageBackendFindFSImageTool(char **tool);
87 virStorageBackendBuildVolFrom
88 virStorageBackendFSImageToolTypeToFunc(int tool_type);
89
90 int virStorageBackendFindGlusterPoolSources(const char *host,
91                                             int pooltype,
92                                             virStoragePoolSourceListPtr list);
93
94
95 typedef struct _virStorageBackend virStorageBackend;
96 typedef virStorageBackend *virStorageBackendPtr;
97
98 /* Callbacks are optional unless documented otherwise; but adding more
99  * callbacks provides better pool support.  */
100 struct _virStorageBackend {
101     int type;
102
103     virStorageBackendFindPoolSources findPoolSources;
104     virStorageBackendCheckPool checkPool;
105     virStorageBackendStartPool startPool;
106     virStorageBackendBuildPool buildPool;
107     virStorageBackendRefreshPool refreshPool; /* Must be non-NULL */
108     virStorageBackendStopPool stopPool;
109     virStorageBackendDeletePool deletePool;
110
111     virStorageBackendBuildVol buildVol;
112     virStorageBackendBuildVolFrom buildVolFrom;
113     virStorageBackendCreateVol createVol;
114     virStorageBackendRefreshVol refreshVol;
115     virStorageBackendDeleteVol deleteVol;
116     virStorageBackendVolumeResize resizeVol;
117 };
118
119 virStorageBackendPtr virStorageBackendForType(int type);
120
121 /* VolOpenCheckMode flags */
122 enum {
123     VIR_STORAGE_VOL_OPEN_NOERROR = 1 << 0, /* don't error if unexpected type
124                                             * encountered, just warn */
125     VIR_STORAGE_VOL_OPEN_REG     = 1 << 1, /* regular files okay */
126     VIR_STORAGE_VOL_OPEN_BLOCK   = 1 << 2, /* block files okay */
127     VIR_STORAGE_VOL_OPEN_CHAR    = 1 << 3, /* char files okay */
128     VIR_STORAGE_VOL_OPEN_DIR     = 1 << 4, /* directories okay */
129 };
130
131 # define VIR_STORAGE_VOL_OPEN_DEFAULT (VIR_STORAGE_VOL_OPEN_REG      |\
132                                        VIR_STORAGE_VOL_OPEN_BLOCK)
133
134 int virStorageBackendVolOpen(const char *path, struct stat *sb,
135                              unsigned int flags)
136     ATTRIBUTE_RETURN_CHECK
137     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
138
139 int virStorageBackendUpdateVolInfo(virStorageVolDefPtr vol,
140                                    bool updateCapacity,
141                                    bool withBlockVolFormat,
142                                    unsigned int openflags);
143 int virStorageBackendUpdateVolTargetInfo(virStorageSourcePtr target,
144                                          bool updateCapacity,
145                                          bool withBlockVolFormat,
146                                          unsigned int openflags);
147 int virStorageBackendUpdateVolTargetInfoFD(virStorageSourcePtr target,
148                                            int fd,
149                                            struct stat *sb,
150                                            bool updateCapacity);
151
152 char *virStorageBackendStablePath(virStoragePoolObjPtr pool,
153                                   const char *devpath,
154                                   bool loop);
155
156 virCommandPtr
157 virStorageBackendCreateQemuImgCmd(virConnectPtr conn,
158                                   virStoragePoolObjPtr pool,
159                                   virStorageVolDefPtr vol,
160                                   virStorageVolDefPtr inputvol,
161                                   unsigned int flags,
162                                   const char *create_tool,
163                                   int imgformat);
164
165 /* ------- virStorageFile backends ------------ */
166 typedef struct _virStorageFileBackend virStorageFileBackend;
167 typedef virStorageFileBackend *virStorageFileBackendPtr;
168
169 struct _virStorageDriverData {
170     virStorageFileBackendPtr backend;
171     void *priv;
172
173     uid_t uid;
174     gid_t gid;
175 };
176
177 typedef int
178 (*virStorageFileBackendInit)(virStorageSourcePtr src);
179
180 typedef void
181 (*virStorageFileBackendDeinit)(virStorageSourcePtr src);
182
183 typedef int
184 (*virStorageFileBackendCreate)(virStorageSourcePtr src);
185
186 typedef int
187 (*virStorageFileBackendUnlink)(virStorageSourcePtr src);
188
189 typedef int
190 (*virStorageFileBackendStat)(virStorageSourcePtr src,
191                              struct stat *st);
192
193 typedef ssize_t
194 (*virStorageFileBackendReadHeader)(virStorageSourcePtr src,
195                                    ssize_t max_len,
196                                    char **buf);
197
198
199 virStorageFileBackendPtr virStorageFileBackendForType(int type, int protocol);
200
201
202
203 struct _virStorageFileBackend {
204     int type;
205     int protocol;
206
207     /* All storage file callbacks may be omitted if not implemented */
208
209     /* The following group of callbacks is expected to set a libvirt
210      * error on failure. */
211     virStorageFileBackendInit backendInit;
212     virStorageFileBackendDeinit backendDeinit;
213     virStorageFileBackendReadHeader storageFileReadHeader;
214
215     /* The following group of callbacks is expected to set errno
216      * and return -1 on error. No libvirt error shall be reported */
217     virStorageFileBackendCreate storageFileCreate;
218     virStorageFileBackendUnlink storageFileUnlink;
219     virStorageFileBackendStat   storageFileStat;
220 };
221
222 #endif /* __VIR_STORAGE_BACKEND_H__ */