xref: /dflybsd-src/contrib/lvm2/dist/lib/zero/zero.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*	$NetBSD: zero.c,v 1.1.1.2 2009/12/02 00:26:25 haad Exp $	*/
2*86d7f5d3SJohn Marino 
3*86d7f5d3SJohn Marino /*
4*86d7f5d3SJohn Marino  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
5*86d7f5d3SJohn Marino  *
6*86d7f5d3SJohn Marino  * This file is part of LVM2.
7*86d7f5d3SJohn Marino  *
8*86d7f5d3SJohn Marino  * This copyrighted material is made available to anyone wishing to use,
9*86d7f5d3SJohn Marino  * modify, copy, or redistribute it subject to the terms and conditions
10*86d7f5d3SJohn Marino  * of the GNU Lesser General Public License v.2.1.
11*86d7f5d3SJohn Marino  *
12*86d7f5d3SJohn Marino  * You should have received a copy of the GNU Lesser General Public License
13*86d7f5d3SJohn Marino  * along with this program; if not, write to the Free Software Foundation,
14*86d7f5d3SJohn Marino  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15*86d7f5d3SJohn Marino  */
16*86d7f5d3SJohn Marino 
17*86d7f5d3SJohn Marino #include "lib.h"
18*86d7f5d3SJohn Marino #include "toolcontext.h"
19*86d7f5d3SJohn Marino #include "segtype.h"
20*86d7f5d3SJohn Marino #include "display.h"
21*86d7f5d3SJohn Marino #include "text_export.h"
22*86d7f5d3SJohn Marino #include "text_import.h"
23*86d7f5d3SJohn Marino #include "config.h"
24*86d7f5d3SJohn Marino #include "str_list.h"
25*86d7f5d3SJohn Marino #include "targets.h"
26*86d7f5d3SJohn Marino #include "lvm-string.h"
27*86d7f5d3SJohn Marino #include "activate.h"
28*86d7f5d3SJohn Marino #include "metadata.h"
29*86d7f5d3SJohn Marino 
_zero_name(const struct lv_segment * seg)30*86d7f5d3SJohn Marino static const char *_zero_name(const struct lv_segment *seg)
31*86d7f5d3SJohn Marino {
32*86d7f5d3SJohn Marino 	return seg->segtype->name;
33*86d7f5d3SJohn Marino }
34*86d7f5d3SJohn Marino 
_zero_merge_segments(struct lv_segment * seg1,struct lv_segment * seg2)35*86d7f5d3SJohn Marino static int _zero_merge_segments(struct lv_segment *seg1, struct lv_segment *seg2)
36*86d7f5d3SJohn Marino {
37*86d7f5d3SJohn Marino 	seg1->len += seg2->len;
38*86d7f5d3SJohn Marino 	seg1->area_len += seg2->area_len;
39*86d7f5d3SJohn Marino 
40*86d7f5d3SJohn Marino 	return 1;
41*86d7f5d3SJohn Marino }
42*86d7f5d3SJohn Marino 
43*86d7f5d3SJohn Marino #ifdef DEVMAPPER_SUPPORT
_zero_add_target_line(struct dev_manager * dm __attribute ((unused)),struct dm_pool * mem __attribute ((unused)),struct cmd_context * cmd __attribute ((unused)),void ** target_state __attribute ((unused)),struct lv_segment * seg __attribute ((unused)),struct dm_tree_node * node,uint64_t len,uint32_t * pvmove_mirror_count __attribute ((unused)))44*86d7f5d3SJohn Marino static int _zero_add_target_line(struct dev_manager *dm __attribute((unused)),
45*86d7f5d3SJohn Marino 				 struct dm_pool *mem __attribute((unused)),
46*86d7f5d3SJohn Marino 				 struct cmd_context *cmd __attribute((unused)),
47*86d7f5d3SJohn Marino 				 void **target_state __attribute((unused)),
48*86d7f5d3SJohn Marino 				 struct lv_segment *seg __attribute((unused)),
49*86d7f5d3SJohn Marino 				 struct dm_tree_node *node,uint64_t len,
50*86d7f5d3SJohn Marino 				 uint32_t *pvmove_mirror_count __attribute((unused)))
51*86d7f5d3SJohn Marino {
52*86d7f5d3SJohn Marino 	return dm_tree_node_add_zero_target(node, len);
53*86d7f5d3SJohn Marino }
54*86d7f5d3SJohn Marino 
_zero_target_present(struct cmd_context * cmd,const struct lv_segment * seg __attribute ((unused)),unsigned * attributes __attribute ((unused)))55*86d7f5d3SJohn Marino static int _zero_target_present(struct cmd_context *cmd,
56*86d7f5d3SJohn Marino 				const struct lv_segment *seg __attribute((unused)),
57*86d7f5d3SJohn Marino 				unsigned *attributes __attribute((unused)))
58*86d7f5d3SJohn Marino {
59*86d7f5d3SJohn Marino 	static int _zero_checked = 0;
60*86d7f5d3SJohn Marino 	static int _zero_present = 0;
61*86d7f5d3SJohn Marino 
62*86d7f5d3SJohn Marino 	if (!_zero_checked)
63*86d7f5d3SJohn Marino 		_zero_present = target_present(cmd, "zero", 1);
64*86d7f5d3SJohn Marino 
65*86d7f5d3SJohn Marino 	_zero_checked = 1;
66*86d7f5d3SJohn Marino 
67*86d7f5d3SJohn Marino 	return _zero_present;
68*86d7f5d3SJohn Marino }
69*86d7f5d3SJohn Marino #endif
70*86d7f5d3SJohn Marino 
_zero_modules_needed(struct dm_pool * mem,const struct lv_segment * seg __attribute ((unused)),struct dm_list * modules)71*86d7f5d3SJohn Marino static int _zero_modules_needed(struct dm_pool *mem,
72*86d7f5d3SJohn Marino 				const struct lv_segment *seg __attribute((unused)),
73*86d7f5d3SJohn Marino 				struct dm_list *modules)
74*86d7f5d3SJohn Marino {
75*86d7f5d3SJohn Marino 	if (!str_list_add(mem, modules, "zero")) {
76*86d7f5d3SJohn Marino 		log_error("zero module string list allocation failed");
77*86d7f5d3SJohn Marino 		return 0;
78*86d7f5d3SJohn Marino 	}
79*86d7f5d3SJohn Marino 
80*86d7f5d3SJohn Marino 	return 1;
81*86d7f5d3SJohn Marino }
82*86d7f5d3SJohn Marino 
_zero_destroy(const struct segment_type * segtype)83*86d7f5d3SJohn Marino static void _zero_destroy(const struct segment_type *segtype)
84*86d7f5d3SJohn Marino {
85*86d7f5d3SJohn Marino 	dm_free((void *) segtype);
86*86d7f5d3SJohn Marino }
87*86d7f5d3SJohn Marino 
88*86d7f5d3SJohn Marino static struct segtype_handler _zero_ops = {
89*86d7f5d3SJohn Marino 	.name = _zero_name,
90*86d7f5d3SJohn Marino 	.merge_segments = _zero_merge_segments,
91*86d7f5d3SJohn Marino #ifdef DEVMAPPER_SUPPORT
92*86d7f5d3SJohn Marino 	.add_target_line = _zero_add_target_line,
93*86d7f5d3SJohn Marino 	.target_present = _zero_target_present,
94*86d7f5d3SJohn Marino #endif
95*86d7f5d3SJohn Marino 	.modules_needed = _zero_modules_needed,
96*86d7f5d3SJohn Marino 	.destroy = _zero_destroy,
97*86d7f5d3SJohn Marino };
98*86d7f5d3SJohn Marino 
init_zero_segtype(struct cmd_context * cmd)99*86d7f5d3SJohn Marino struct segment_type *init_zero_segtype(struct cmd_context *cmd)
100*86d7f5d3SJohn Marino {
101*86d7f5d3SJohn Marino 	struct segment_type *segtype = dm_malloc(sizeof(*segtype));
102*86d7f5d3SJohn Marino 
103*86d7f5d3SJohn Marino 	if (!segtype)
104*86d7f5d3SJohn Marino 		return_NULL;
105*86d7f5d3SJohn Marino 
106*86d7f5d3SJohn Marino 	segtype->cmd = cmd;
107*86d7f5d3SJohn Marino 	segtype->ops = &_zero_ops;
108*86d7f5d3SJohn Marino 	segtype->name = "zero";
109*86d7f5d3SJohn Marino 	segtype->private = NULL;
110*86d7f5d3SJohn Marino 	segtype->flags = SEG_CAN_SPLIT | SEG_VIRTUAL | SEG_CANNOT_BE_ZEROED;
111*86d7f5d3SJohn Marino 
112*86d7f5d3SJohn Marino 	log_very_verbose("Initialised segtype: %s", segtype->name);
113*86d7f5d3SJohn Marino 
114*86d7f5d3SJohn Marino 	return segtype;
115*86d7f5d3SJohn Marino }
116