metadata.c revision 204076
1/*-
2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * All rights reserved.
4 *
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sbin/hastd/metadata.c 204076 2010-02-18 23:16:19Z pjd $");
32
33#include <assert.h>
34#include <errno.h>
35#include <fcntl.h>
36#include <string.h>
37#include <strings.h>
38#include <unistd.h>
39
40#include <ebuf.h>
41#include <nv.h>
42#include <pjdlog.h>
43#include <subr.h>
44
45#include "metadata.h"
46
47int
48metadata_read(struct hast_resource *res, bool openrw)
49{
50	unsigned char *buf;
51	struct ebuf *eb;
52	struct nv *nv;
53	ssize_t done;
54	const char *str;
55	int rerrno;
56	bool opened_here;
57
58	opened_here = false;
59	rerrno = 0;
60
61	/*
62	 * Is this first metadata_read() call for this resource?
63	 */
64	if (res->hr_localfd == -1) {
65		if (provinfo(res, openrw) < 0) {
66			rerrno = errno;
67			goto fail;
68		}
69		opened_here = true;
70		pjdlog_debug(1, "Obtained info about %s.", res->hr_localpath);
71		if (openrw) {
72			if (flock(res->hr_localfd, LOCK_EX | LOCK_NB) < 0) {
73				rerrno = errno;
74				if (errno == EOPNOTSUPP) {
75					pjdlog_warning("Unable to lock %s (operation not supported), but continuing.",
76					    res->hr_localpath);
77				} else {
78					pjdlog_errno(LOG_ERR,
79					    "Unable to lock %s",
80					    res->hr_localpath);
81					goto fail;
82				}
83			}
84			pjdlog_debug(1, "Locked %s.", res->hr_localpath);
85		}
86	}
87
88	eb = ebuf_alloc(METADATA_SIZE);
89	if (eb == NULL) {
90		rerrno = errno;
91		pjdlog_errno(LOG_ERR,
92		    "Unable to allocate memory to read metadata");
93		goto fail;
94	}
95	if (ebuf_add_tail(eb, NULL, METADATA_SIZE) < 0) {
96		rerrno = errno;
97		pjdlog_errno(LOG_ERR,
98		    "Unable to allocate memory to read metadata");
99		goto fail;
100	}
101	buf = ebuf_data(eb, NULL);
102	assert(buf != NULL);
103	done = pread(res->hr_localfd, buf, METADATA_SIZE, 0);
104	if (done < 0 || done != METADATA_SIZE) {
105		rerrno = errno;
106		pjdlog_errno(LOG_ERR, "Unable to read metadata");
107		ebuf_free(eb);
108		goto fail;
109	}
110	nv = nv_ntoh(eb);
111	if (nv == NULL) {
112		rerrno = errno;
113		pjdlog_errno(LOG_ERR, "Metadata read from %s is invalid",
114		    res->hr_localpath);
115		ebuf_free(eb);
116		goto fail;
117	}
118
119	str = nv_get_string(nv, "resource");
120	if (strcmp(str, res->hr_name) != 0) {
121		pjdlog_error("Provider %s is not part of resource %s.",
122		    res->hr_localpath, res->hr_name);
123		nv_free(nv);
124		goto fail;
125	}
126
127	res->hr_datasize = nv_get_uint64(nv, "datasize");
128	res->hr_extentsize = (int)nv_get_uint32(nv, "extentsize");
129	res->hr_keepdirty = (int)nv_get_uint32(nv, "keepdirty");
130	res->hr_localoff = nv_get_uint64(nv, "offset");
131	res->hr_resuid = nv_get_uint64(nv, "resuid");
132	if (res->hr_role != HAST_ROLE_PRIMARY) {
133		/* Secondary or init role. */
134		res->hr_secondary_localcnt = nv_get_uint64(nv, "localcnt");
135		res->hr_secondary_remotecnt = nv_get_uint64(nv, "remotecnt");
136	}
137	if (res->hr_role != HAST_ROLE_SECONDARY) {
138		/* Primary or init role. */
139		res->hr_primary_localcnt = nv_get_uint64(nv, "localcnt");
140		res->hr_primary_remotecnt = nv_get_uint64(nv, "remotecnt");
141	}
142	str = nv_get_string(nv, "prevrole");
143	if (str != NULL) {
144		if (strcmp(str, "primary") == 0)
145			res->hr_previous_role = HAST_ROLE_PRIMARY;
146		else if (strcmp(str, "secondary") == 0)
147			res->hr_previous_role = HAST_ROLE_SECONDARY;
148	}
149
150	if (nv_error(nv) != 0) {
151		errno = rerrno = nv_error(nv);
152		pjdlog_errno(LOG_ERR, "Unable to read metadata from %s",
153		    res->hr_localpath);
154		nv_free(nv);
155		goto fail;
156	}
157	return (0);
158fail:
159	if (opened_here) {
160		close(res->hr_localfd);
161		res->hr_localfd = -1;
162	}
163	errno = rerrno;
164	return (-1);
165}
166
167int
168metadata_write(struct hast_resource *res)
169{
170	struct ebuf *eb;
171	struct nv *nv;
172	unsigned char *buf, *ptr;
173	size_t size;
174	ssize_t done;
175
176	buf = calloc(1, METADATA_SIZE);
177	if (buf == NULL) {
178		pjdlog_error("Unable to allocate %zu bytes for metadata.",
179		    (size_t)METADATA_SIZE);
180		return (-1);
181	}
182
183	nv = nv_alloc();
184	nv_add_string(nv, res->hr_name, "resource");
185	nv_add_uint64(nv, (uint64_t)res->hr_datasize, "datasize");
186	nv_add_uint32(nv, (uint32_t)res->hr_extentsize, "extentsize");
187	nv_add_uint32(nv, (uint32_t)res->hr_keepdirty, "keepdirty");
188	nv_add_uint64(nv, (uint64_t)res->hr_localoff, "offset");
189	nv_add_uint64(nv, res->hr_resuid, "resuid");
190	if (res->hr_role == HAST_ROLE_PRIMARY ||
191	    res->hr_role == HAST_ROLE_INIT) {
192		nv_add_uint64(nv, res->hr_primary_localcnt, "localcnt");
193		nv_add_uint64(nv, res->hr_primary_remotecnt, "remotecnt");
194	} else /* if (res->hr_role == HAST_ROLE_SECONDARY) */ {
195		assert(res->hr_role == HAST_ROLE_SECONDARY);
196		nv_add_uint64(nv, res->hr_secondary_localcnt, "localcnt");
197		nv_add_uint64(nv, res->hr_secondary_remotecnt, "remotecnt");
198	}
199	nv_add_string(nv, role2str(res->hr_role), "prevrole");
200	if (nv_error(nv) != 0) {
201		pjdlog_error("Unable to create metadata.");
202		goto fail;
203	}
204	res->hr_previous_role = res->hr_role;
205	eb = nv_hton(nv);
206	assert(eb != NULL);
207	ptr = ebuf_data(eb, &size);
208	assert(ptr != NULL);
209	assert(size < METADATA_SIZE);
210	bcopy(ptr, buf, size);
211	done = pwrite(res->hr_localfd, buf, METADATA_SIZE, 0);
212	if (done < 0 || done != METADATA_SIZE) {
213		pjdlog_errno(LOG_ERR, "Unable to write metadata");
214		goto fail;
215	}
216
217	return (0);
218fail:
219	free(buf);
220	nv_free(nv);
221	return (-1);
222}
223