xref: /dragonfly/contrib/lvm2/dist/lib/error/errseg.c (revision 37de577a)
1 /*	$NetBSD: errseg.c,v 1.1.1.2 2009/12/02 00:26:45 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 "str_list.h"
29 #include "metadata.h"
30 
31 static const char *_errseg_name(const struct lv_segment *seg)
32 {
33 	return seg->segtype->name;
34 }
35 
36 static int _errseg_merge_segments(struct lv_segment *seg1, struct lv_segment *seg2)
37 {
38 	seg1->len += seg2->len;
39 	seg1->area_len += seg2->area_len;
40 
41 	return 1;
42 }
43 
44 #ifdef DEVMAPPER_SUPPORT
45 static int _errseg_add_target_line(struct dev_manager *dm __attribute((unused)),
46 				struct dm_pool *mem __attribute((unused)),
47 				struct cmd_context *cmd __attribute((unused)),
48 				void **target_state __attribute((unused)),
49 				struct lv_segment *seg __attribute((unused)),
50 				struct dm_tree_node *node, uint64_t len,
51 				uint32_t *pvmove_mirror_count __attribute((unused)))
52 {
53 	return dm_tree_node_add_error_target(node, len);
54 }
55 
56 static int _errseg_target_present(struct cmd_context *cmd,
57 				  const struct lv_segment *seg __attribute((unused)),
58 				  unsigned *attributes __attribute((unused)))
59 {
60 	static int _errseg_checked = 0;
61 	static int _errseg_present = 0;
62 
63 	/* Reported truncated in older kernels */
64 	if (!_errseg_checked &&
65 	    (target_present(cmd, "error", 0) ||
66 	     target_present(cmd, "erro", 0)))
67 		_errseg_present = 1;
68 
69 	_errseg_checked = 1;
70 	return _errseg_present;
71 }
72 #endif
73 
74 static int _errseg_modules_needed(struct dm_pool *mem,
75 				  const struct lv_segment *seg __attribute((unused)),
76 				  struct dm_list *modules)
77 {
78 	if (!str_list_add(mem, modules, "error")) {
79 		log_error("error module string list allocation failed");
80 		return 0;
81 	}
82 
83 	return 1;
84 }
85 
86 static void _errseg_destroy(const struct segment_type *segtype)
87 {
88 	dm_free((void *)segtype);
89 }
90 
91 static struct segtype_handler _error_ops = {
92 	.name = _errseg_name,
93 	.merge_segments = _errseg_merge_segments,
94 #ifdef DEVMAPPER_SUPPORT
95 	.add_target_line = _errseg_add_target_line,
96 	.target_present = _errseg_target_present,
97 #endif
98 	.modules_needed = _errseg_modules_needed,
99 	.destroy = _errseg_destroy,
100 };
101 
102 struct segment_type *init_error_segtype(struct cmd_context *cmd)
103 {
104 	struct segment_type *segtype = dm_malloc(sizeof(*segtype));
105 
106 	if (!segtype)
107 		return_NULL;
108 
109 	segtype->cmd = cmd;
110 	segtype->ops = &_error_ops;
111 	segtype->name = "error";
112 	segtype->private = NULL;
113 	segtype->flags = SEG_CAN_SPLIT | SEG_VIRTUAL | SEG_CANNOT_BE_ZEROED;
114 
115 	log_very_verbose("Initialised segtype: %s", segtype->name);
116 
117 	return segtype;
118 }
119