summaryrefslogtreecommitdiff
path: root/liblvm/lvm_lv.c
blob: ee0d48bb498091bd214fa5c0160141e9d870573d (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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
/*
 * Copyright (C) 2008-2013 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "lib.h"
#include "metadata.h"
#include "lvm-string.h"
#include "defaults.h"
#include "segtype.h"
#include "locking.h"
#include "activate.h"
#include "lvm_misc.h"
#include "lvm2app.h"

/* FIXME Improve all the log messages to include context. Which VG/LV as a minimum? */

struct lvm_lv_create_params
{
	uint32_t magic;
	vg_t vg;
	struct lvcreate_params lvp;
};

#define LV_CREATE_PARAMS_MAGIC 0xFEED0001

static int _lv_check_handle(const lv_t lv, const int vg_writeable)
{
	if (!lv || !lv->vg || vg_read_error(lv->vg))
		return -1;
	if (vg_writeable && !vg_check_write_mode(lv->vg))
		return -1;
	return 0;
}

/* FIXME: have lib/report/report.c _disp function call lv_size()? */
uint64_t lvm_lv_get_size(const lv_t lv)
{
	uint64_t rc;
	struct saved_env e = store_user_env(lv->vg->cmd);
	rc = SECTOR_SIZE * lv_size(lv);
	restore_user_env(&e);
	return rc;
}

const char *lvm_lv_get_uuid(const lv_t lv)
{
	const char *rc;
	struct saved_env e = store_user_env(lv->vg->cmd);
	rc = lv_uuid_dup(lv->vg->vgmem, lv);
	restore_user_env(&e);
	return rc;
}

const char *lvm_lv_get_name(const lv_t lv)
{
	const char *rc;
	struct saved_env e = store_user_env(lv->vg->cmd);
	rc = dm_pool_strndup(lv->vg->vgmem, (const char *)lv->name,
			       NAME_LEN+1);
	restore_user_env(&e);
	return rc;
}

const char *lvm_lv_get_attr(const lv_t lv)
{
	const char *rc;
	struct saved_env e = store_user_env(lv->vg->cmd);
	rc = lv_attr_dup(lv->vg->vgmem, lv);
	restore_user_env(&e);
	return rc;
}

const char *lvm_lv_get_origin(const lv_t lv)
{
	const char *rc;
	struct saved_env e = store_user_env(lv->vg->cmd);
	rc = lv_origin_dup(lv->vg->vgmem, lv);
	restore_user_env(&e);
	return rc;
}

struct lvm_property_value lvm_lv_get_property(const lv_t lv, const char *name)
{
	struct lvm_property_value rc;
	struct saved_env e = store_user_env(lv->vg->cmd);
	rc = get_property(NULL, NULL, lv, NULL, NULL, NULL, NULL, name);
	restore_user_env(&e);
	return rc;
}

struct lvm_property_value lvm_lvseg_get_property(const lvseg_t lvseg,
						 const char *name)
{
	struct lvm_property_value rc;
	struct saved_env e = store_user_env(lvseg->lv->vg->cmd);
	rc = get_property(NULL, NULL, NULL, lvseg, NULL, NULL, NULL, name);
	restore_user_env(&e);
	return rc;
}

uint64_t lvm_lv_is_active(const lv_t lv)
{
	uint64_t rc = 0;
	struct lvinfo info;

	struct saved_env e = store_user_env(lv->vg->cmd);

	if (lv_info(lv->vg->cmd, lv, 0, &info, 0, 0) &&
	    info.exists && info.live_table)
		rc = 1;

	restore_user_env(&e);
	return rc;
}

uint64_t lvm_lv_is_suspended(const lv_t lv)
{
	uint64_t rc = 0;
	struct lvinfo info;
	struct saved_env e = store_user_env(lv->vg->cmd);

	if (lv_info(lv->vg->cmd, lv, 0, &info, 0, 0) &&
	    info.exists && info.suspended)
		rc = 1;

	restore_user_env(&e);
	return rc;
}

static int _lvm_lv_add_tag(lv_t lv, const char *tag)
{
	if (_lv_check_handle(lv, 1))
		return -1;
	if (!lv_change_tag(lv, tag, 1))
		return -1;
	return 0;
}

int lvm_lv_add_tag(lv_t lv, const char *tag)
{
	int rc;
	struct saved_env e = store_user_env(lv->vg->cmd);
	rc = _lvm_lv_add_tag(lv, tag);
	restore_user_env(&e);
	return rc;
}


static int _lvm_lv_remove_tag(lv_t lv, const char *tag)
{
	if (_lv_check_handle(lv, 1))
		return -1;
	if (!lv_change_tag(lv, tag, 0))
		return -1;
	return 0;
}

int lvm_lv_remove_tag(lv_t lv, const char *tag)
{
	int rc;
	struct saved_env e = store_user_env(lv->vg->cmd);
	rc = _lvm_lv_remove_tag(lv, tag);
	restore_user_env(&e);
	return rc;
}


struct dm_list *lvm_lv_get_tags(const lv_t lv)
{
	struct dm_list *rc;
	struct saved_env e = store_user_env(lv->vg->cmd);
	rc = tag_list_copy(lv->vg->vgmem, &lv->tags);
	restore_user_env(&e);
	return rc;
}

/* Set defaults for non-segment specific LV parameters */
static void _lv_set_default_params(struct lvcreate_params *lp,
				   vg_t vg, const char *lvname,
				   uint64_t extents)
{
	lp->zero = 1;
	lp->wipe_signatures = 0;
	lp->major = -1;
	lp->minor = -1;
	lp->activate = CHANGE_AY;
	lp->lv_name = lvname; /* FIXME: check this for safety */
	lp->pvh = &vg->pvs;

	lp->extents = extents;
	lp->permission = LVM_READ | LVM_WRITE;
	lp->read_ahead = DM_READ_AHEAD_NONE;
	lp->alloc = ALLOC_INHERIT;
	dm_list_init(&lp->tags);
}

static struct segment_type * _get_segtype(struct cmd_context *cmd) {
	struct segment_type *rc = get_segtype_from_string(cmd, SEG_TYPE_NAME_STRIPED);
	if (!rc) {
		log_error(INTERNAL_ERROR "Segtype striped not found.");
	}
	return rc;
}

/* Set default for linear segment specific LV parameters */
static int _lv_set_default_linear_params(struct cmd_context *cmd,
					  struct lvcreate_params *lp)
{
	if (!(lp->segtype = _get_segtype(cmd))) {
		return 0;
	}

	lp->stripes = 1;

	return 1;
}

/*
 * FIXME: This function should probably not commit to disk but require calling
 * lvm_vg_write.  However, this appears to be non-trivial change until
 * lv_create_single is refactored by segtype.
 */
static lv_t _lvm_vg_create_lv_linear(vg_t vg, const char *name, uint64_t size)
{
	struct lvcreate_params lp = { 0 };
	uint64_t extents;
	struct logical_volume *lv;

	if (vg_read_error(vg))
		return NULL;
	if (!vg_check_write_mode(vg))
		return NULL;

	if (!(extents = extents_from_size(vg->cmd, size / SECTOR_SIZE,
					  vg->extent_size))) {
		log_error("Unable to create LV without size.");
		return NULL;
	}

	_lv_set_default_params(&lp, vg, name, extents);
	if (!_lv_set_default_linear_params(vg->cmd, &lp))
		return_NULL;
	if (!(lv = lv_create_single(vg, &lp)))
		return_NULL;
	return (lv_t) lv;
}

lv_t lvm_vg_create_lv_linear(vg_t vg, const char *name, uint64_t size)
{
	lv_t rc;
	struct saved_env e = store_user_env(vg->cmd);
	rc = _lvm_vg_create_lv_linear(vg, name, size);
	restore_user_env(&e);
	return rc;
}

/*
 * FIXME: This function should probably not commit to disk but require calling
 * lvm_vg_write.
 */
static int _lvm_vg_remove_lv(lv_t lv)
{
	if (!lv || !lv->vg || vg_read_error(lv->vg))
		return -1;
	if (!vg_check_write_mode(lv->vg))
		return -1;
	if (!lv_remove_single(lv->vg->cmd, lv, DONT_PROMPT, 0))
		return -1;
	return 0;
}

int lvm_vg_remove_lv(lv_t lv)
{
	int rc;
	struct saved_env e = store_user_env(lv->vg->cmd);
	rc = _lvm_vg_remove_lv(lv);
	restore_user_env(&e);
	return rc;
}

static int _lvm_lv_activate(lv_t lv)
{
	if (!lv || !lv->vg || vg_read_error(lv->vg) || !lv->vg->cmd)
		return -1;

	/* FIXME: handle pvmove stuff later */
	if (lv_is_locked(lv)) {
		log_error("Unable to activate locked LV");
		return -1;
	}

	/* FIXME: handle lvconvert stuff later */
	if (lv_is_converting(lv)) {
		log_error("Unable to activate LV with in-progress lvconvert");
		return -1;
	}

	if (lv_is_origin(lv)) {
		log_verbose("Activating logical volume \"%s\" "
			    "exclusively", lv->name);
		if (!activate_lv_excl(lv->vg->cmd, lv)) {
			/* FIXME Improve msg */
			log_error("Activate exclusive failed.");
			return -1;
		}
	} else {
		log_verbose("Activating logical volume \"%s\"",
			    lv->name);
		if (!activate_lv(lv->vg->cmd, lv)) {
			/* FIXME Improve msg */
			log_error("Activate failed.");
			return -1;
		}
	}
	return 0;
}

int lvm_lv_activate(lv_t lv)
{
	int rc;
	struct saved_env e = store_user_env(lv->vg->cmd);
	rc = _lvm_lv_activate(lv);
	restore_user_env(&e);
	return rc;
}

static int _lvm_lv_deactivate(lv_t lv)
{
	if (!lv || !lv->vg || vg_read_error(lv->vg) || !lv->vg->cmd)
		return -1;

	log_verbose("Deactivating logical volume \"%s\"", lv->name);
	if (!deactivate_lv(lv->vg->cmd, lv)) {
		log_error("Deactivate failed.");
		return -1;
	}
	return 0;
}

int lvm_lv_deactivate(lv_t lv)
{
	int rc;
	struct saved_env e = store_user_env(lv->vg->cmd);
	rc = _lvm_lv_deactivate(lv);
	restore_user_env(&e);
	return rc;
}

static struct dm_list *_lvm_lv_list_lvsegs(lv_t lv)
{
	struct dm_list *list;
	lvseg_list_t *lvseg;
	struct lv_segment *lvl;

	if (dm_list_empty(&lv->segments))
		return NULL;

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

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

struct dm_list *lvm_lv_list_lvsegs(lv_t lv)
{
	struct dm_list *rc;
	struct saved_env e = store_user_env(lv->vg->cmd);
	rc = _lvm_lv_list_lvsegs(lv);
	restore_user_env(&e);
	return rc;
}

lv_t lvm_lv_from_name(vg_t vg, const char *name)
{
	lv_t rc = NULL;
	struct lv_list *lvl;

	struct saved_env e = store_user_env(vg->cmd);
	dm_list_iterate_items(lvl, &vg->lvs) {
		if (!strcmp(name, lvl->lv->name)) {
			rc = lvl->lv;
			break;
		}
	}
	restore_user_env(&e);
	return rc;
}

static lv_t _lvm_lv_from_uuid(vg_t vg, const char *uuid)
{
	struct lv_list *lvl;
	struct id id;

	if (strlen(uuid) < ID_LEN) {
		log_errno (EINVAL, "Invalid UUID string length");
		return NULL;
	}

	if (!id_read_format(&id, uuid)) {
		log_errno(EINVAL, "Invalid UUID format.");
		return NULL;
	}

	dm_list_iterate_items(lvl, &vg->lvs) {
		if (id_equal(&vg->id, &lvl->lv->lvid.id[0]) &&
		    id_equal(&id, &lvl->lv->lvid.id[1]))
			return lvl->lv;
	}
	return NULL;
}

lv_t lvm_lv_from_uuid(vg_t vg, const char *uuid)
{
	lv_t rc;
	struct saved_env e = store_user_env(vg->cmd);
	rc = _lvm_lv_from_uuid(vg, uuid);
	restore_user_env(&e);
	return rc;
}

int lvm_lv_rename(lv_t lv, const char *new_name)
{
	int rc = 0;
	struct saved_env e = store_user_env(lv->vg->cmd);
	if (!lv_rename(lv->vg->cmd, lv, new_name)) {
		/* FIXME Improve msg */
		log_error("LV rename failed.");
		rc = -1;
	}
	restore_user_env(&e);
	return rc;
}

int lvm_lv_resize(const lv_t lv, uint64_t new_size)
{
	int rc = 0;
	struct lvresize_params lp = {
		.lv_name = lv->name,
		.sign = SIGN_NONE,
		.percent = PERCENT_NONE,
		.resize = LV_ANY,
		.size = new_size >> SECTOR_SHIFT,
		.ac_force = 1,	/* Assume the user has a good backup? */
		.sizeargs = 1,
	};
	struct saved_env e = store_user_env(lv->vg->cmd);

	if (!lv_resize_prepare(lv->vg->cmd, lv, &lp, &lv->vg->pvs) ||
	    !lv_resize(lv->vg->cmd, lv, &lp, &lv->vg->pvs)) {
		/* FIXME Improve msg */
		log_error("LV resize failed.");
		/* FIXME Define consistent symbolic return codes */
		rc = -1;
	}
	restore_user_env(&e);
	return rc;
}

lv_t lvm_lv_snapshot(const lv_t lv, const char *snap_name,
						uint64_t max_snap_size)
{
	lv_t rc = NULL;
	struct lvm_lv_create_params *lvcp = NULL;
	struct saved_env e = store_user_env(lv->vg->cmd);

	lvcp = lvm_lv_params_create_snapshot(lv, snap_name, max_snap_size);
	if (lvcp) {
		rc = lvm_lv_create(lvcp);
	}
	restore_user_env(&e);
	return rc;
}

/* Set defaults for thin pool specific LV parameters */
static int _lv_set_pool_params(struct lvcreate_params *lp,
				vg_t vg, const char *pool_name,
				uint64_t extents, uint64_t meta_size)
{
	uint64_t pool_metadata_size;

	_lv_set_default_params(lp, vg, pool_name, extents);

	lp->create_pool = 1;
	lp->segtype = get_segtype_from_string(vg->cmd, SEG_TYPE_NAME_THIN_POOL);
	lp->stripes = 1;

	if (!meta_size) {
		pool_metadata_size = extents * vg->extent_size /
			(lp->chunk_size * (SECTOR_SIZE / 64));
		while ((pool_metadata_size >
			(2 * DEFAULT_THIN_POOL_OPTIMAL_SIZE / SECTOR_SIZE)) &&
		       lp->chunk_size < DM_THIN_MAX_DATA_BLOCK_SIZE) {
			lp->chunk_size <<= 1;
			pool_metadata_size >>= 1;
	         }
	} else
		pool_metadata_size = meta_size;

	if (pool_metadata_size % vg->extent_size)
		pool_metadata_size +=
			vg->extent_size - pool_metadata_size % vg->extent_size;

	if (!(lp->pool_metadata_extents =
	      extents_from_size(vg->cmd, pool_metadata_size / SECTOR_SIZE,
				vg->extent_size)))
		return_0;

	return 1;
}

static lv_create_params_t _lvm_lv_params_create_thin_pool(vg_t vg,
		const char *pool_name, uint64_t size, uint32_t chunk_size,
		uint64_t meta_size, lvm_thin_discards_t discard)
{
	uint64_t extents = 0;
	struct lvm_lv_create_params *lvcp = NULL;

	if (meta_size > (2 * DEFAULT_THIN_POOL_MAX_METADATA_SIZE)) {
		log_error("Invalid metadata size");
		return NULL;
	}

	if (meta_size &&
		meta_size < (2 * DEFAULT_THIN_POOL_MIN_METADATA_SIZE)) {
		log_error("Invalid metadata size");
		return NULL;
	}

	if (vg_read_error(vg))
		return NULL;

	if (!vg_check_write_mode(vg))
		return NULL;

	if (pool_name == NULL || !strlen(pool_name)) {
		log_error("pool_name invalid");
		return NULL;
	}

	if (!(extents = extents_from_size(vg->cmd, size / SECTOR_SIZE,
					  vg->extent_size))) {
		log_error("Unable to create LV thin pool without size.");
		return NULL;
	}

	lvcp = dm_pool_zalloc(vg->vgmem, sizeof (struct lvm_lv_create_params));

	if (lvcp) {
		lvcp->vg = vg;
		lvcp->lvp.discards = (thin_discards_t) discard;

		if (chunk_size)
			lvcp->lvp.chunk_size = chunk_size;
		else
			lvcp->lvp.chunk_size = DEFAULT_THIN_POOL_CHUNK_SIZE * 2;

		if (lvcp->lvp.chunk_size < DM_THIN_MIN_DATA_BLOCK_SIZE ||
				lvcp->lvp.chunk_size > DM_THIN_MAX_DATA_BLOCK_SIZE) {
			log_error("Invalid chunk_size");
			return NULL;
		}

		if (!_lv_set_pool_params(&lvcp->lvp, vg, pool_name, extents, meta_size))
			return_NULL;

		lvcp->magic = LV_CREATE_PARAMS_MAGIC;
	}
	return lvcp;
}

lv_create_params_t lvm_lv_params_create_thin_pool(vg_t vg,
		const char *pool_name, uint64_t size, uint32_t chunk_size,
		uint64_t meta_size, lvm_thin_discards_t discard)
{
	lv_create_params_t rc;
	struct saved_env e = store_user_env(vg->cmd);
	rc = _lvm_lv_params_create_thin_pool(vg, pool_name, size, chunk_size,
										meta_size, discard);
	restore_user_env(&e);
	return rc;
}

/* Set defaults for thin LV specific parameters */
static int _lv_set_thin_params(struct lvcreate_params *lp,
			       vg_t vg, const char *pool_name,
			       const char *lvname,
			       uint32_t extents)
{
	_lv_set_default_params(lp, vg, lvname, 0);

	lp->pool_name = pool_name;
	lp->segtype = get_segtype_from_string(vg->cmd, SEG_TYPE_NAME_THIN);
	lp->virtual_extents = extents;
	lp->stripes = 1;

	return 1;
}

static lv_create_params_t _lvm_lv_params_create_snapshot(const lv_t lv,
							 const char *snap_name,
							 uint64_t max_snap_size)
{
	uint64_t size = 0;
	uint64_t extents = 0;
	struct lvm_lv_create_params *lvcp = NULL;

	if (vg_read_error(lv->vg)) {
		return NULL;
	}

	if (!vg_check_write_mode(lv->vg))
			return NULL;

	if (snap_name == NULL || !strlen(snap_name)) {
		log_error("snap_name invalid");
		return NULL;
	}

	if (max_snap_size) {
		size = max_snap_size >> SECTOR_SHIFT;
		if (!(extents = extents_from_size(lv->vg->cmd, size, lv->vg->extent_size)))
			return_NULL;
	}

	if (!size && !lv_is_thin_volume(lv) ) {
		log_error("Origin is not thin, specify size of snapshot");
		return NULL;
	}

	lvcp = dm_pool_zalloc(lv->vg->vgmem, sizeof (struct lvm_lv_create_params));
	if (lvcp) {
		lvcp->vg = lv->vg;
		_lv_set_default_params(&lvcp->lvp, lv->vg, snap_name, extents);

		if (size) {
			if (!(lvcp->lvp.segtype = get_segtype_from_string(lv->vg->cmd, SEG_TYPE_NAME_SNAPSHOT))) {
				log_error("Segtype snapshot not found.");
				return NULL;
			}
			lvcp->lvp.chunk_size = 8;
			lvcp->lvp.snapshot = 1;
		} else {
			if (!(lvcp->lvp.segtype = get_segtype_from_string(lv->vg->cmd, SEG_TYPE_NAME_THIN))) {
				log_error("Segtype thin not found.");
				return NULL;
			}

			lvcp->lvp.pool_name = first_seg(lv)->pool_lv->name;
		}

		lvcp->lvp.stripes = 1;
		lvcp->lvp.origin_name = lv->name;

		lvcp->magic = LV_CREATE_PARAMS_MAGIC;
	}

	return lvcp;
}

lv_create_params_t lvm_lv_params_create_snapshot(const lv_t lv,
						 const char *snap_name,
						 uint64_t max_snap_size)
{
	lv_create_params_t rc;
	struct saved_env e = store_user_env(lv->vg->cmd);
	rc = _lvm_lv_params_create_snapshot(lv, snap_name, max_snap_size);
	restore_user_env(&e);
	return rc;
}

static lv_create_params_t _lvm_lv_params_create_thin(const vg_t vg,
									const char *pool_name,
									const char *lvname, uint64_t size)
{
	struct lvm_lv_create_params *lvcp = NULL;
	uint32_t extents = 0;

	/* precondition checks */
	if (vg_read_error(vg))
		return NULL;

	if (!vg_check_write_mode(vg))
		return NULL;

	if (pool_name == NULL || !strlen(pool_name)) {
		log_error("pool_name invalid");
		return NULL;
	}

	if (lvname == NULL || !strlen(lvname)) {
		log_error("lvname invalid");
		return NULL;
	}

	if (!(extents = extents_from_size(vg->cmd, size / SECTOR_SIZE,
			vg->extent_size))) {
		log_error("Unable to create thin LV without size.");
		return NULL;
	}

	lvcp = dm_pool_zalloc(vg->vgmem, sizeof (struct lvm_lv_create_params));
	if (lvcp) {
		lvcp->vg = vg;
		if (!_lv_set_thin_params(&lvcp->lvp, vg, pool_name, lvname, extents))
			return_NULL;

		lvcp->magic = LV_CREATE_PARAMS_MAGIC;
	}

	return lvcp;
}

lv_create_params_t lvm_lv_params_create_thin(const vg_t vg, const char *pool_name,
									const char *lvname, uint64_t size)
{
	lv_create_params_t rc;
	struct saved_env e = store_user_env(vg->cmd);
	rc = _lvm_lv_params_create_thin(vg, pool_name, lvname, size);
	restore_user_env(&e);
	return rc;
}

struct lvm_property_value lvm_lv_params_get_property(
						const lv_create_params_t params,
						const char *name)
{
	struct lvm_property_value rc = { .is_valid = 0 };

	if (params && params->magic == LV_CREATE_PARAMS_MAGIC) {
		struct saved_env e = store_user_env(params->vg->cmd);
		rc = get_property(NULL, NULL, NULL, NULL, NULL, &params->lvp, NULL, name);
		restore_user_env(&e);
	} else
		log_error("Invalid lv_create_params parameter");

	return rc;
}

int lvm_lv_params_set_property(lv_create_params_t params, const char *name,
								struct lvm_property_value *prop)
{
	int rc = -1;

	if (params && params->magic == LV_CREATE_PARAMS_MAGIC) {
		struct saved_env e = store_user_env(params->vg->cmd);
		rc = set_property(NULL, NULL, NULL, &params->lvp, NULL, name, prop);
		restore_user_env(&e);
	} else
		log_error("Invalid lv_create_params parameter");

	return rc;
}

static lv_t _lvm_lv_create(lv_create_params_t params)
{
	struct lv_list *lvl = NULL;

	if (params && params->magic == LV_CREATE_PARAMS_MAGIC) {
		if (!params->lvp.segtype) {
			log_error("segtype parameter is NULL");
			return_NULL;
		}
		if (!lv_create_single(params->vg, &params->lvp))
				return_NULL;

		/*
		 * In some case we are making a thin pool so lv_name is not valid, but
		 * pool is.
		 */
		if (!(lvl = find_lv_in_vg(params->vg,
				(params->lvp.lv_name) ? params->lvp.lv_name : params->lvp.pool_name)))
			return_NULL;
		return (lv_t) lvl->lv;
	}
	log_error("Invalid lv_create_params parameter");
	return NULL;
}

lv_t lvm_lv_create(lv_create_params_t params)
{
	lv_t rc;
	struct saved_env e = store_user_env(params->vg->cmd);
	rc = _lvm_lv_create(params);
	restore_user_env(&e);
	return rc;
}