2 * Copyright (c) 2009-2010 The FreeBSD Foundation
5 * This software was developed by Pawel Jakub Dawidek under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
47 metadata_read(struct hast_resource *res, bool openrw)
61 * Is this first metadata_read() call for this resource?
63 if (res->hr_localfd == -1) {
64 if (provinfo(res, openrw) == -1) {
69 pjdlog_debug(1, "Obtained info about %s.", res->hr_localpath);
71 if (flock(res->hr_localfd, LOCK_EX | LOCK_NB) == -1) {
73 if (errno == EOPNOTSUPP) {
74 pjdlog_warning("Unable to lock %s (operation not supported), but continuing.",
83 pjdlog_debug(1, "Locked %s.", res->hr_localpath);
87 eb = ebuf_alloc(METADATA_SIZE);
91 "Unable to allocate memory to read metadata");
94 if (ebuf_add_tail(eb, NULL, METADATA_SIZE) == -1) {
97 "Unable to allocate memory to read metadata");
101 buf = ebuf_data(eb, NULL);
102 PJDLOG_ASSERT(buf != NULL);
103 done = pread(res->hr_localfd, buf, METADATA_SIZE, 0);
104 if (done == -1 || done != METADATA_SIZE) {
106 pjdlog_errno(LOG_ERR, "Unable to read metadata");
113 pjdlog_errno(LOG_ERR, "Metadata read from %s is invalid",
119 str = nv_get_string(nv, "resource");
120 if (str != NULL && strcmp(str, res->hr_name) != 0) {
121 pjdlog_error("Provider %s is not part of resource %s.",
122 res->hr_localpath, res->hr_name);
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");
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");
142 str = nv_get_string(nv, "prevrole");
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;
150 if (nv_error(nv) != 0) {
151 errno = rerrno = nv_error(nv);
152 pjdlog_errno(LOG_ERR, "Unable to read metadata from %s",
161 close(res->hr_localfd);
162 res->hr_localfd = -1;
169 metadata_write(struct hast_resource *res)
173 unsigned char *buf, *ptr;
178 buf = calloc(1, METADATA_SIZE);
180 pjdlog_error("Unable to allocate %zu bytes for metadata.",
181 (size_t)METADATA_SIZE);
188 nv_add_string(nv, res->hr_name, "resource");
189 nv_add_uint64(nv, (uint64_t)res->hr_datasize, "datasize");
190 nv_add_uint32(nv, (uint32_t)res->hr_extentsize, "extentsize");
191 nv_add_uint32(nv, (uint32_t)res->hr_keepdirty, "keepdirty");
192 nv_add_uint64(nv, (uint64_t)res->hr_localoff, "offset");
193 nv_add_uint64(nv, res->hr_resuid, "resuid");
194 if (res->hr_role == HAST_ROLE_PRIMARY ||
195 res->hr_role == HAST_ROLE_INIT) {
196 nv_add_uint64(nv, res->hr_primary_localcnt, "localcnt");
197 nv_add_uint64(nv, res->hr_primary_remotecnt, "remotecnt");
198 } else /* if (res->hr_role == HAST_ROLE_SECONDARY) */ {
199 PJDLOG_ASSERT(res->hr_role == HAST_ROLE_SECONDARY);
200 nv_add_uint64(nv, res->hr_secondary_localcnt, "localcnt");
201 nv_add_uint64(nv, res->hr_secondary_remotecnt, "remotecnt");
203 nv_add_string(nv, role2str(res->hr_role), "prevrole");
204 if (nv_error(nv) != 0) {
205 pjdlog_error("Unable to create metadata.");
208 res->hr_previous_role = res->hr_role;
210 PJDLOG_ASSERT(eb != NULL);
211 ptr = ebuf_data(eb, &size);
212 PJDLOG_ASSERT(ptr != NULL);
213 PJDLOG_ASSERT(size < METADATA_SIZE);
214 bcopy(ptr, buf, size);
215 done = pwrite(res->hr_localfd, buf, METADATA_SIZE, 0);
216 if (done == -1 || done != METADATA_SIZE) {
217 pjdlog_errno(LOG_ERR, "Unable to write metadata");