summaryrefslogtreecommitdiff
path: root/liblvm/lvm_vg.c
blob: 405a91a1044e7316aa2fccdc121cf0ae75b93c44 (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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * Copyright (C) 2008,2009 Red Hat, Inc. All rights reserved.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "lib.h"
#include "toolcontext.h"
#include "metadata.h"
#include "archiver.h"
#include "locking.h"
#include "lvmcache.h"
#include "lvm_misc.h"
#include "lvm2app.h"

int lvm_vg_add_tag(vg_t vg, const char *tag)
{
	if (vg_read_error(vg))
		return -1;

	if (!vg_check_write_mode(vg))
		return -1;

	if (!vg_change_tag(vg, tag, 1))
		return -1;
	return 0;
}


int lvm_vg_remove_tag(vg_t vg, const char *tag)
{
	if (vg_read_error(vg))
		return -1;

	if (!vg_check_write_mode(vg))
		return -1;

	if (!vg_change_tag(vg, tag, 0))
		return -1;
	return 0;
}


vg_t lvm_vg_create(lvm_t libh, const char *vg_name)
{
	struct volume_group *vg;

	vg = vg_create((struct cmd_context *)libh, vg_name);
	/* FIXME: error handling is still TBD */
	if (vg_read_error(vg)) {
		release_vg(vg);
		return NULL;
	}
	vg->open_mode = 'w';
	return (vg_t) vg;
}

int lvm_vg_extend(vg_t vg, const char *device)
{
	struct pvcreate_params pp;

	if (vg_read_error(vg))
		return -1;

	if (!vg_check_write_mode(vg))
		return -1;

	if (!lock_vol(vg->cmd, VG_ORPHANS, LCK_VG_WRITE)) {
		log_error("Can't get lock for orphan PVs");
		return -1;
	}

	pvcreate_params_set_defaults(&pp);
	if (!vg_extend(vg, 1, &device, &pp)) {
		unlock_vg(vg->cmd, VG_ORPHANS);
		return -1;
	}
	/*
	 * FIXME: Either commit to disk, or keep holding VG_ORPHANS and
	 * release in lvm_vg_close().
	 */
	unlock_vg(vg->cmd, VG_ORPHANS);
	return 0;
}

int lvm_vg_reduce(vg_t vg, const char *device)
{
	if (vg_read_error(vg))
		return -1;
	if (!vg_check_write_mode(vg))
		return -1;

	if (!vg_reduce(vg, device))
		return -1;
	return 0;
}

int lvm_vg_set_extent_size(vg_t vg, uint32_t new_size)
{
	if (vg_read_error(vg))
		return -1;
	if (!vg_check_write_mode(vg))
		return -1;

	if (!vg_set_extent_size(vg, new_size / SECTOR_SIZE))
		return -1;
	return 0;
}

int lvm_vg_write(vg_t vg)
{
	struct pv_list *pvl;

	if (vg_read_error(vg))
		return -1;
	if (!vg_check_write_mode(vg))
		return -1;

	if (dm_list_empty(&vg->pvs)) {
		if (!vg_remove(vg))
			return -1;
		return 0;
	}

	if (! dm_list_empty(&vg->removed_pvs)) {
		if (!lock_vol(vg->cmd, VG_ORPHANS, LCK_VG_WRITE)) {
			log_error("Can't get lock for orphan PVs");
			return 0;
		}
	}

	if (!archive(vg))
		return -1;

	/* Store VG on disk(s) */
	if (!vg_write(vg) || !vg_commit(vg))
		return -1;

	if (! dm_list_empty(&vg->removed_pvs)) {
		dm_list_iterate_items(pvl, &vg->removed_pvs) {
			pv_write_orphan(vg->cmd, pvl->pv);
			pv_set_fid(pvl->pv, NULL);
			/* FIXME: do pvremove / label_remove()? */
		}
		dm_list_init(&vg->removed_pvs);
		unlock_vg(vg->cmd, VG_ORPHANS);
	}

	return 0;
}

int lvm_vg_close(vg_t vg)
{
	if (vg_read_error(vg) == FAILED_LOCKING)
		release_vg(vg);
	else
		unlock_and_release_vg(vg->cmd, vg, vg->name);
	return 0;
}

int lvm_vg_remove(vg_t vg)
{
	if (vg_read_error(vg))
		return -1;
	if (!vg_check_write_mode(vg))
		return -1;

	if (!vg_remove_check(vg))
		return -1;

	vg_remove_pvs(vg);

	return 0;
}

vg_t lvm_vg_open(lvm_t libh, const char *vgname, const char *mode,
		  uint32_t flags)
{
	uint32_t internal_flags = 0;
	struct volume_group *vg;

	if (!strncmp(mode, "w", 1))
		internal_flags |= READ_FOR_UPDATE;
	else if (strncmp(mode, "r", 1)) {
		log_errno(EINVAL, "Invalid VG open mode");
		return NULL;
	}

	vg = vg_read((struct cmd_context *)libh, vgname, NULL, internal_flags);
	if (vg_read_error(vg)) {
		/* FIXME: use log_errno either here in inside vg_read */
		release_vg(vg);
		return NULL;
	}
	/* FIXME: combine this with locking ? */
	vg->open_mode = mode[0];

	return (vg_t) vg;
}

struct dm_list *lvm_vg_list_pvs(vg_t vg)
{
	struct dm_list *list;
	pv_list_t *pvs;
	struct pv_list *pvl;

	if (dm_list_empty(&vg->pvs))
		return NULL;

	if (!(list = dm_pool_zalloc(vg->vgmem, sizeof(*list)))) {
		log_errno(ENOMEM, "Memory allocation fail for dm_list.");
		return NULL;
	}
	dm_list_init(list);

	dm_list_iterate_items(pvl, &vg->pvs) {
		if (!(pvs = dm_pool_zalloc(vg->vgmem, sizeof(*pvs)))) {
			log_errno(ENOMEM,
				"Memory allocation fail for lvm_pv_list.");
			return NULL;
		}
		pvs->pv = pvl->pv;
		dm_list_add(list, &pvs->list);
	}
	return list;
}

struct dm_list *lvm_vg_list_lvs(vg_t vg)
{
	struct dm_list *list;
	lv_list_t *lvs;
	struct lv_list *lvl;

	if (dm_list_empty(&vg->lvs))
		return NULL;

	if (!(list = dm_pool_zalloc(vg->vgmem, sizeof(*list)))) {
		log_errno(ENOMEM, "Memory allocation fail for dm_list.");
		return NULL;
	}
	dm_list_init(list);

	dm_list_iterate_items(lvl, &vg->lvs) {
		if (!(lvs = dm_pool_zalloc(vg->vgmem, sizeof(*lvs)))) {
			log_errno(ENOMEM,
				"Memory allocation fail for lvm_lv_list.");
			return NULL;
		}
		lvs->lv = lvl->lv;
		dm_list_add(list, &lvs->list);
	}
	return list;
}

struct dm_list *lvm_vg_get_tags(const vg_t vg)
{
	return tag_list_copy(vg->vgmem, &vg->tags);
}

uint64_t lvm_vg_get_seqno(const vg_t vg)
{
	return vg_seqno(vg);
}

uint64_t lvm_vg_is_clustered(const vg_t vg)
{
	return vg_is_clustered(vg);
}

uint64_t lvm_vg_is_exported(const vg_t vg)
{
	return vg_is_exported(vg);
}

uint64_t lvm_vg_is_partial(const vg_t vg)
{
	return (vg_missing_pv_count(vg) != 0);
}

/* FIXME: invalid handle? return INTMAX? */
uint64_t lvm_vg_get_size(const vg_t vg)
{
	return SECTOR_SIZE * vg_size(vg);
}

uint64_t lvm_vg_get_free_size(const vg_t vg)
{
	return SECTOR_SIZE * vg_free(vg);
}

uint64_t lvm_vg_get_extent_size(const vg_t vg)
{
	return SECTOR_SIZE * vg_extent_size(vg);
}

uint64_t lvm_vg_get_extent_count(const vg_t vg)
{
	return vg_extent_count(vg);
}

uint64_t lvm_vg_get_free_extent_count(const vg_t vg)
{
	return vg_free_count(vg);
}

uint64_t lvm_vg_get_pv_count(const vg_t vg)
{
	return vg_pv_count(vg);
}

uint64_t lvm_vg_get_max_pv(const vg_t vg)
{
	return vg_max_pv(vg);
}

uint64_t lvm_vg_get_max_lv(const vg_t vg)
{
	return vg_max_lv(vg);
}

const char *lvm_vg_get_uuid(const vg_t vg)
{
	return vg_uuid_dup(vg);
}

const char *lvm_vg_get_name(const vg_t vg)
{
	return dm_pool_strndup(vg->vgmem, (const char *)vg->name, NAME_LEN+1);
}


struct lvm_property_value lvm_vg_get_property(const vg_t vg, const char *name)
{
	return get_property(NULL, vg, NULL, NULL, NULL, name);
}

int lvm_vg_set_property(const vg_t vg, const char *name,
			struct lvm_property_value *value)
{
	return set_property(NULL, vg, NULL, name, value);
}

struct dm_list *lvm_list_vg_names(lvm_t libh)
{
	return get_vgnames((struct cmd_context *)libh, 0);
}

struct dm_list *lvm_list_vg_uuids(lvm_t libh)
{
	return get_vgids((struct cmd_context *)libh, 0);
}

/*
 * FIXME: Elaborate on when to use, side-effects, .cache file, etc
 */
int lvm_scan(lvm_t libh)
{
	if (!lvmcache_label_scan((struct cmd_context *)libh, 2))
		return -1;
	return 0;
}