1 /*	$NetBSD: lvm1-label.c,v 1.1.1.2 2009/12/02 00:26:49 haad Exp $	*/
2 
3 /*
4  * Copyright (C) 2002-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 "lvm1-label.h"
20 #include "disk-rep.h"
21 #include "label.h"
22 #include "metadata.h"
23 #include "xlate.h"
24 #include "format1.h"
25 
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 
29 static void _not_supported(const char *op)
30 {
31 	log_error("The '%s' operation is not supported for the lvm1 labeller.",
32 		  op);
33 }
34 
35 static int _lvm1_can_handle(struct labeller *l __attribute((unused)), void *buf, uint64_t sector)
36 {
37 	struct pv_disk *pvd = (struct pv_disk *) buf;
38 	uint32_t version;
39 
40 	/* LVM1 label must always be in first sector */
41 	if (sector)
42 		return 0;
43 
44 	version = xlate16(pvd->version);
45 
46 	if (pvd->id[0] == 'H' && pvd->id[1] == 'M' &&
47 	    (version == 1 || version == 2))
48 		return 1;
49 
50 	return 0;
51 }
52 
53 static int _lvm1_write(struct label *label __attribute((unused)), void *buf __attribute((unused)))
54 {
55 	_not_supported("write");
56 	return 0;
57 }
58 
59 static int _lvm1_read(struct labeller *l, struct device *dev, void *buf,
60 		 struct label **label)
61 {
62 	struct pv_disk *pvd = (struct pv_disk *) buf;
63 	struct vg_disk vgd;
64 	struct lvmcache_info *info;
65 	const char *vgid = FMT_LVM1_ORPHAN_VG_NAME;
66 	const char *vgname = FMT_LVM1_ORPHAN_VG_NAME;
67 	unsigned exported = 0;
68 
69 	munge_pvd(dev, pvd);
70 
71 	if (*pvd->vg_name) {
72 		if (!read_vgd(dev, &vgd, pvd))
73 			return_0;
74 		vgid = (char *) vgd.vg_uuid;
75 		vgname = (char *) pvd->vg_name;
76 		exported = pvd->pv_status & VG_EXPORTED;
77 	}
78 
79 	if (!(info = lvmcache_add(l, (char *)pvd->pv_uuid, dev, vgname, vgid,
80 				  exported)))
81 		return_0;
82 	*label = info->label;
83 
84 	info->device_size = xlate32(pvd->pv_size) << SECTOR_SHIFT;
85 	dm_list_init(&info->mdas);
86 
87 	info->status &= ~CACHE_INVALID;
88 
89 	return 1;
90 }
91 
92 static int _lvm1_initialise_label(struct labeller *l __attribute((unused)), struct label *label)
93 {
94 	strcpy(label->type, "LVM1");
95 
96 	return 1;
97 }
98 
99 static void _lvm1_destroy_label(struct labeller *l __attribute((unused)), struct label *label __attribute((unused)))
100 {
101 	return;
102 }
103 
104 static void _lvm1_destroy(struct labeller *l)
105 {
106 	dm_free(l);
107 }
108 
109 struct label_ops _lvm1_ops = {
110 	.can_handle = _lvm1_can_handle,
111 	.write = _lvm1_write,
112 	.read = _lvm1_read,
113 	.verify = _lvm1_can_handle,
114 	.initialise_label = _lvm1_initialise_label,
115 	.destroy_label = _lvm1_destroy_label,
116 	.destroy = _lvm1_destroy,
117 };
118 
119 struct labeller *lvm1_labeller_create(struct format_type *fmt)
120 {
121 	struct labeller *l;
122 
123 	if (!(l = dm_malloc(sizeof(*l)))) {
124 		log_error("Couldn't allocate labeller object.");
125 		return NULL;
126 	}
127 
128 	l->ops = &_lvm1_ops;
129 	l->private = (const void *) fmt;
130 
131 	return l;
132 }
133