1 /*	$NetBSD: pool_label.c,v 1.1.1.1 2008/12/22 00:17:51 haad Exp $	*/
2 
3 /*
4  * Copyright (C) 1997-2004 Sistina Software, Inc. All rights reserved.
5  * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
6  *
7  * This file is part of LVM2.
8  *
9  * This copyrighted material is made available to anyone wishing to use,
10  * modify, copy, or redistribute it subject to the terms and conditions
11  * of the GNU Lesser General Public License v.2.1.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17 
18 #include "lib.h"
19 #include "label.h"
20 #include "metadata.h"
21 #include "xlate.h"
22 #include "disk_rep.h"
23 #include "pool_label.h"
24 
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 
28 static void _pool_not_supported(const char *op)
29 {
30 	log_error("The '%s' operation is not supported for the pool labeller.",
31 		  op);
32 }
33 
34 static int _pool_can_handle(struct labeller *l __attribute((unused)), void *buf, uint64_t sector)
35 {
36 
37 	struct pool_disk pd;
38 
39 	/*
40 	 * POOL label must always be in first sector
41 	 */
42 	if (sector)
43 		return 0;
44 
45 	pool_label_in(&pd, buf);
46 
47 	/* can ignore 8 rightmost bits for ondisk format check */
48 	if ((pd.pl_magic == POOL_MAGIC) &&
49 	    (pd.pl_version >> 8 == POOL_VERSION >> 8))
50 		return 1;
51 
52 	return 0;
53 }
54 
55 static int _pool_write(struct label *label __attribute((unused)), void *buf __attribute((unused)))
56 {
57 	_pool_not_supported("write");
58 	return 0;
59 }
60 
61 static int _pool_read(struct labeller *l, struct device *dev, void *buf,
62 		 struct label **label)
63 {
64 	struct pool_list pl;
65 
66 	return read_pool_label(&pl, l, dev, buf, label);
67 }
68 
69 static int _pool_initialise_label(struct labeller *l __attribute((unused)), struct label *label)
70 {
71 	strcpy(label->type, "POOL");
72 
73 	return 1;
74 }
75 
76 static void _pool_destroy_label(struct labeller *l __attribute((unused)), struct label *label __attribute((unused)))
77 {
78 	return;
79 }
80 
81 static void _label_pool_destroy(struct labeller *l)
82 {
83 	dm_free(l);
84 }
85 
86 struct label_ops _pool_ops = {
87       .can_handle = _pool_can_handle,
88       .write = _pool_write,
89       .read = _pool_read,
90       .verify = _pool_can_handle,
91       .initialise_label = _pool_initialise_label,
92       .destroy_label = _pool_destroy_label,
93       .destroy = _label_pool_destroy,
94 };
95 
96 struct labeller *pool_labeller_create(struct format_type *fmt)
97 {
98 	struct labeller *l;
99 
100 	if (!(l = dm_malloc(sizeof(*l)))) {
101 		log_error("Couldn't allocate labeller object.");
102 		return NULL;
103 	}
104 
105 	l->ops = &_pool_ops;
106 	l->private = (const void *) fmt;
107 
108 	return l;
109 }
110