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