xref: /netbsd/external/gpl2/lvm2/dist/liblvm/lvm_lv.c (revision 7c604eea)
1*7c604eeaShaad /*	$NetBSD: lvm_lv.c,v 1.1.1.1 2009/12/02 00:26:15 haad Exp $	*/
2*7c604eeaShaad 
3*7c604eeaShaad /*
4*7c604eeaShaad  * Copyright (C) 2008,2009 Red Hat, Inc. All rights reserved.
5*7c604eeaShaad  *
6*7c604eeaShaad  * This file is part of LVM2.
7*7c604eeaShaad  *
8*7c604eeaShaad  * This copyrighted material is made available to anyone wishing to use,
9*7c604eeaShaad  * modify, copy, or redistribute it subject to the terms and conditions
10*7c604eeaShaad  * of the GNU Lesser General Public License v.2.1.
11*7c604eeaShaad  *
12*7c604eeaShaad  * You should have received a copy of the GNU Lesser General Public License
13*7c604eeaShaad  * along with this program; if not, write to the Free Software Foundation,
14*7c604eeaShaad  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15*7c604eeaShaad  */
16*7c604eeaShaad 
17*7c604eeaShaad #include "lib.h"
18*7c604eeaShaad #include "lvm2app.h"
19*7c604eeaShaad #include "metadata-exported.h"
20*7c604eeaShaad #include "lvm-string.h"
21*7c604eeaShaad #include "defaults.h"
22*7c604eeaShaad #include "segtype.h"
23*7c604eeaShaad #include "locking.h"
24*7c604eeaShaad #include "activate.h"
25*7c604eeaShaad 
26*7c604eeaShaad #include <string.h>
27*7c604eeaShaad 
28*7c604eeaShaad /* FIXME: have lib/report/report.c _disp function call lv_size()? */
lvm_lv_get_size(const lv_t lv)29*7c604eeaShaad uint64_t lvm_lv_get_size(const lv_t lv)
30*7c604eeaShaad {
31*7c604eeaShaad 	return lv_size(lv);
32*7c604eeaShaad }
33*7c604eeaShaad 
lvm_lv_get_uuid(const lv_t lv)34*7c604eeaShaad char *lvm_lv_get_uuid(const lv_t lv)
35*7c604eeaShaad {
36*7c604eeaShaad 	char uuid[64] __attribute((aligned(8)));
37*7c604eeaShaad 
38*7c604eeaShaad 	if (!id_write_format(&lv->lvid.id[1], uuid, sizeof(uuid))) {
39*7c604eeaShaad 		log_error("Internal error converting uuid");
40*7c604eeaShaad 		return NULL;
41*7c604eeaShaad 	}
42*7c604eeaShaad 	return strndup((const char *)uuid, 64);
43*7c604eeaShaad }
44*7c604eeaShaad 
lvm_lv_get_name(const lv_t lv)45*7c604eeaShaad char *lvm_lv_get_name(const lv_t lv)
46*7c604eeaShaad {
47*7c604eeaShaad 	char *name;
48*7c604eeaShaad 
49*7c604eeaShaad 	name = dm_malloc(NAME_LEN + 1);
50*7c604eeaShaad 	strncpy(name, (const char *)lv->name, NAME_LEN);
51*7c604eeaShaad 	name[NAME_LEN] = '\0';
52*7c604eeaShaad 	return name;
53*7c604eeaShaad }
54*7c604eeaShaad 
lvm_lv_is_active(const lv_t lv)55*7c604eeaShaad uint64_t lvm_lv_is_active(const lv_t lv)
56*7c604eeaShaad {
57*7c604eeaShaad 	struct lvinfo info;
58*7c604eeaShaad 	if (lv_info(lv->vg->cmd, lv, &info, 1, 0) &&
59*7c604eeaShaad 	    info.exists && info.live_table)
60*7c604eeaShaad 		return 1;
61*7c604eeaShaad 	return 0;
62*7c604eeaShaad }
63*7c604eeaShaad 
lvm_lv_is_suspended(const lv_t lv)64*7c604eeaShaad uint64_t lvm_lv_is_suspended(const lv_t lv)
65*7c604eeaShaad {
66*7c604eeaShaad 	struct lvinfo info;
67*7c604eeaShaad 	if (lv_info(lv->vg->cmd, lv, &info, 1, 0) &&
68*7c604eeaShaad 	    info.exists && info.suspended)
69*7c604eeaShaad 		return 1;
70*7c604eeaShaad 	return 0;
71*7c604eeaShaad }
72*7c604eeaShaad 
73*7c604eeaShaad /* Set defaults for non-segment specific LV parameters */
_lv_set_default_params(struct lvcreate_params * lp,vg_t vg,const char * lvname,uint64_t extents)74*7c604eeaShaad static void _lv_set_default_params(struct lvcreate_params *lp,
75*7c604eeaShaad 				   vg_t vg, const char *lvname,
76*7c604eeaShaad 				   uint64_t extents)
77*7c604eeaShaad {
78*7c604eeaShaad 	lp->zero = 1;
79*7c604eeaShaad 	lp->major = -1;
80*7c604eeaShaad 	lp->minor = -1;
81*7c604eeaShaad 	lp->vg_name = vg->name;
82*7c604eeaShaad 	lp->lv_name = lvname; /* FIXME: check this for safety */
83*7c604eeaShaad 	lp->pvh = &vg->pvs;
84*7c604eeaShaad 
85*7c604eeaShaad 	lp->extents = extents;
86*7c604eeaShaad 	lp->permission = LVM_READ | LVM_WRITE;
87*7c604eeaShaad 	lp->read_ahead = DM_READ_AHEAD_NONE;
88*7c604eeaShaad 	lp->alloc = ALLOC_INHERIT;
89*7c604eeaShaad 	lp->tag = NULL;
90*7c604eeaShaad }
91*7c604eeaShaad 
92*7c604eeaShaad /* Set default for linear segment specific LV parameters */
_lv_set_default_linear_params(struct cmd_context * cmd,struct lvcreate_params * lp)93*7c604eeaShaad static void _lv_set_default_linear_params(struct cmd_context *cmd,
94*7c604eeaShaad 					  struct lvcreate_params *lp)
95*7c604eeaShaad {
96*7c604eeaShaad 	lp->segtype = get_segtype_from_string(cmd, "striped");
97*7c604eeaShaad 	lp->stripes = 1;
98*7c604eeaShaad 	lp->stripe_size = DEFAULT_STRIPESIZE * 2;
99*7c604eeaShaad }
100*7c604eeaShaad 
101*7c604eeaShaad /*
102*7c604eeaShaad  * FIXME: This function should probably not commit to disk but require calling
103*7c604eeaShaad  * lvm_vg_write.  However, this appears to be non-trivial change until
104*7c604eeaShaad  * lv_create_single is refactored by segtype.
105*7c604eeaShaad  */
lvm_vg_create_lv_linear(vg_t vg,const char * name,uint64_t size)106*7c604eeaShaad lv_t lvm_vg_create_lv_linear(vg_t vg, const char *name, uint64_t size)
107*7c604eeaShaad {
108*7c604eeaShaad 	struct lvcreate_params lp;
109*7c604eeaShaad 	uint64_t extents;
110*7c604eeaShaad 	struct lv_list *lvl;
111*7c604eeaShaad 
112*7c604eeaShaad 	if (vg_read_error(vg))
113*7c604eeaShaad 		return NULL;
114*7c604eeaShaad 	if (!vg_check_write_mode(vg))
115*7c604eeaShaad 		return NULL;
116*7c604eeaShaad 	memset(&lp, 0, sizeof(lp));
117*7c604eeaShaad 	extents = extents_from_size(vg->cmd, size, vg->extent_size);
118*7c604eeaShaad 	_lv_set_default_params(&lp, vg, name, extents);
119*7c604eeaShaad 	_lv_set_default_linear_params(vg->cmd, &lp);
120*7c604eeaShaad 	if (!lv_create_single(vg, &lp))
121*7c604eeaShaad 		return NULL;
122*7c604eeaShaad 	lvl = find_lv_in_vg(vg, name);
123*7c604eeaShaad 	if (!lvl)
124*7c604eeaShaad 		return NULL;
125*7c604eeaShaad 	return (lv_t) lvl->lv;
126*7c604eeaShaad }
127*7c604eeaShaad 
128*7c604eeaShaad /*
129*7c604eeaShaad  * FIXME: This function should probably not commit to disk but require calling
130*7c604eeaShaad  * lvm_vg_write.
131*7c604eeaShaad  */
lvm_vg_remove_lv(lv_t lv)132*7c604eeaShaad int lvm_vg_remove_lv(lv_t lv)
133*7c604eeaShaad {
134*7c604eeaShaad 	if (!lv || !lv->vg || vg_read_error(lv->vg))
135*7c604eeaShaad 		return -1;
136*7c604eeaShaad 	if (!vg_check_write_mode(lv->vg))
137*7c604eeaShaad 		return -1;
138*7c604eeaShaad 	if (!lv_remove_single(lv->vg->cmd, lv, DONT_PROMPT))
139*7c604eeaShaad 		return -1;
140*7c604eeaShaad 	return 0;
141*7c604eeaShaad }
142*7c604eeaShaad 
lvm_lv_activate(lv_t lv)143*7c604eeaShaad int lvm_lv_activate(lv_t lv)
144*7c604eeaShaad {
145*7c604eeaShaad 	if (!lv || !lv->vg || vg_read_error(lv->vg) || !lv->vg->cmd)
146*7c604eeaShaad 		return -1;
147*7c604eeaShaad 
148*7c604eeaShaad 	/* FIXME: handle pvmove stuff later */
149*7c604eeaShaad 	if (lv->status & LOCKED) {
150*7c604eeaShaad 		log_error("Unable to activate locked LV");
151*7c604eeaShaad 		return -1;
152*7c604eeaShaad 	}
153*7c604eeaShaad 
154*7c604eeaShaad 	/* FIXME: handle lvconvert stuff later */
155*7c604eeaShaad 	if (lv->status & CONVERTING) {
156*7c604eeaShaad 		log_error("Unable to activate LV with in-progress lvconvert");
157*7c604eeaShaad 		return -1;
158*7c604eeaShaad 	}
159*7c604eeaShaad 
160*7c604eeaShaad 	if (lv_is_origin(lv)) {
161*7c604eeaShaad 		log_verbose("Activating logical volume \"%s\" "
162*7c604eeaShaad 			    "exclusively", lv->name);
163*7c604eeaShaad 		if (!activate_lv_excl(lv->vg->cmd, lv)) {
164*7c604eeaShaad 			log_error("Activate exclusive failed.");
165*7c604eeaShaad 			return -1;
166*7c604eeaShaad 		}
167*7c604eeaShaad 	} else {
168*7c604eeaShaad 		log_verbose("Activating logical volume \"%s\"",
169*7c604eeaShaad 			    lv->name);
170*7c604eeaShaad 		if (!activate_lv(lv->vg->cmd, lv)) {
171*7c604eeaShaad 			log_error("Activate failed.");
172*7c604eeaShaad 			return -1;
173*7c604eeaShaad 		}
174*7c604eeaShaad 	}
175*7c604eeaShaad 	return 0;
176*7c604eeaShaad }
177*7c604eeaShaad 
lvm_lv_deactivate(lv_t lv)178*7c604eeaShaad int lvm_lv_deactivate(lv_t lv)
179*7c604eeaShaad {
180*7c604eeaShaad 	if (!lv || !lv->vg || vg_read_error(lv->vg) || !lv->vg->cmd)
181*7c604eeaShaad 		return -1;
182*7c604eeaShaad 
183*7c604eeaShaad 	log_verbose("Deactivating logical volume \"%s\"", lv->name);
184*7c604eeaShaad 	if (!deactivate_lv(lv->vg->cmd, lv)) {
185*7c604eeaShaad 		log_error("Deactivate failed.");
186*7c604eeaShaad 		return -1;
187*7c604eeaShaad 	}
188*7c604eeaShaad 	return 0;
189*7c604eeaShaad }
190*7c604eeaShaad 
lvm_lv_resize(const lv_t lv,uint64_t new_size)191*7c604eeaShaad int lvm_lv_resize(const lv_t lv, uint64_t new_size)
192*7c604eeaShaad {
193*7c604eeaShaad 	/* FIXME: add lv resize code here */
194*7c604eeaShaad 	log_error("NOT IMPLEMENTED YET");
195*7c604eeaShaad 	return -1;
196*7c604eeaShaad }
197