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