cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob c08e0b85294b147345c5506b9875fef956c57a39 5277 bytes (raw)
$ git show HEAD:mgmt_fn.c	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
 
/*
 * Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
 * License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
 */
#include "cmogstored.h"
#include "mgmt.h"
#include "iov_str.h"
#include "digest.h"

static char *
get_path(struct iovec *dst, struct mog_mgmt *mgmt, char *buf, bool sdup)
{
	dst->iov_base = buf + mgmt->mark[0];
	dst->iov_len = mgmt->mark[1] - mgmt->mark[0];

	if (mog_valid_path(dst->iov_base, dst->iov_len)) {
		char *path;

		if (sdup) {
			path = malloc(dst->iov_len + 1);
			if (!path) {
				struct iovec iov;

				IOV_STR(&iov, "ERROR: out-of-memory\r\n");
				mog_mgmt_writev(mgmt, &iov, 1);

				return NULL;
			}

			memcpy(path, dst->iov_base, dst->iov_len);
		} else {
			path = dst->iov_base;
		}

		path[dst->iov_len] = '\0';
		return path;
	} else {
		struct iovec iov;

		IOV_STR(&iov, "ERROR: uri invalid (contains ..)\r\n");
		mog_mgmt_writev(mgmt, &iov, 1);
		return NULL;
	}
}

/* starts the MD5 request */
void mog_mgmt_fn_digest(struct mog_fd *mfd, char *buf)
{
	struct mog_mgmt *mgmt = &mfd->as.mgmt;
	struct iovec iov[2];
	char *path = get_path(iov, mgmt, buf, true);

	if (!path) return;

	TRACE(CMOGSTORED_MGMT_DIG_START(mfd->fd, mgmt->alg, path));

	mgmt->forward = mog_file_open_read(mgmt->svc, path);
	if (mgmt->forward) {
		struct mog_file *file = &mgmt->forward->as.file;

		mog_fadv_noreuse(mgmt->forward->fd, 0, 0 /* ALL */);
		mog_fadv_sequential(mgmt->forward->fd, 0, 0 /* ALL */);
		mog_digest_init(&file->digest, mgmt->alg);
		file->pathlen = iov[0].iov_len;
		file->path = path;
	} else {
		switch (mgmt->alg) {
		case GC_MD5:
			IOV_STR(&iov[1], " MD5=-1\r\n");
			break;
		case GC_SHA1:
			IOV_STR(&iov[1], " SHA-1=-1\r\n");
			break;
		default: /* Ragel parser prevents this: */
			die("BUG: unhandled algorithm: %d", mgmt->alg);
		}
		TRACE(CMOGSTORED_MGMT_DIG_DONE(mfd->fd, -1));
		mog_mgmt_writev(mgmt, iov, 2);
		free(path);
	}
}

/* finishes the MD5 request */
#define CLEN(s) (sizeof(s)-1)

void mog_mgmt_fn_digest_err(struct mog_fd *mfd)
{
	struct mog_mgmt *mgmt = &mfd->as.mgmt;
	struct iovec iov[3];
	struct mog_fd *file_mfd = mgmt->forward;
	struct mog_file *file = &file_mfd->as.file;
	long long offset = (long long)lseek(file_mfd->fd, 0, SEEK_CUR);
	char buf[sizeof(" at 18446744073709551615 failed\r\n") - 1];

	/* offset could be -1 here, but there ain't much we can do */

	IOV_STR(iov, "ERR read ");
	iov[1].iov_base = file->path;
	iov[1].iov_len = file->pathlen;
	iov[2].iov_base = buf;
	iov[2].iov_len = snprintf(buf, sizeof(buf),
	                          " at %lld failed\r\n", offset);
	TRACE(CMOGSTORED_MGMT_DIG_DONE(mfd->fd, offset));
	mog_mgmt_writev(mgmt, iov, 3);
}

/* output: "/$PATH MD5=hex\r\n" */
void mog_mgmt_fn_digest_emit(struct mog_fd *mfd)
{
	struct mog_mgmt *mgmt = &mfd->as.mgmt;
	struct iovec iov[2];
	char buf[CLEN(" SHA-1=") + 40 + CLEN("\r\n")];
	char *b = buf;
	struct mog_fd *file_mfd = mgmt->forward;
	struct mog_file *file = &file_mfd->as.file;
	size_t len;

	iov[0].iov_base = file->path;
	iov[0].iov_len = file->pathlen;

	/* ugh, clean this up... */
	switch (mgmt->alg) {
	case GC_MD5:
		b = mempcpy(b, " MD5=", CLEN(" MD5="));
		len = 32;
		iov[1].iov_len = CLEN(" MD5=") + 32 + CLEN("\r\n");
		break;
	case GC_SHA1:
		b = mempcpy(b, " SHA-1=", CLEN(" SHA-1="));
		len = 40;
		iov[1].iov_len = sizeof(buf);
		break;
	default:
		die("BUG: unhandled algorithm: %d", mgmt->alg);
	}

	mog_digest_hex(&file->digest, b, len);
	b[len] = '\r';
	b[len + 1] = '\n';
	iov[1].iov_base = buf;
	TRACE(CMOGSTORED_MGMT_DIG_DONE(mfd->fd, 0));
	mog_mgmt_writev(mgmt, iov, 2);
}

/*
 * writes to mgmt fd:
 *   "URI $SIZE\r\n" on success
 *   "URI -1\r\n" on failure
 *   "ERROR: uri invalid (contains ..)\r\n" on invalid paths
 */
void mog_mgmt_fn_size(struct mog_mgmt *mgmt, char *buf)
{
	struct stat sb;
	struct iovec iov[2];
	char tmp[sizeof(" 18446744073709551615\r\n") - 1];
	char *path = get_path(iov, mgmt, buf, false);

	if (!path) return;

	if (mog_stat(mgmt->svc, path, &sb) == 0) {
		long long size = (long long)sb.st_size;

		iov[1].iov_base = tmp;
		iov[1].iov_len = snprintf(tmp, sizeof(tmp), " %lld\r\n", size);
	} else {
		IOV_STR(&iov[1], " -1\r\n");
	}

	mog_mgmt_writev(mgmt, iov, 2);
}

void mog_mgmt_fn_blank(struct mog_mgmt *mgmt)
{
	struct iovec iov;

	IOV_STR(&iov, "\r\n");
	mog_mgmt_writev(mgmt, &iov, 1);
}

void mog_mgmt_fn_unknown(struct mog_mgmt *mgmt, char *buf)
{
	struct iovec iov[3];

	IOV_STR(&iov[0], "ERROR: unknown command: ");
	iov[1].iov_base = mgmt->mark[0] + buf;
	iov[1].iov_len = mgmt->mark[1] - mgmt->mark[0];
	IOV_STR(&iov[2], "\r\n");
	mog_mgmt_writev(mgmt, iov, 3);
}

void mog_mgmt_fn_watch_err(struct mog_mgmt *mgmt)
{
	struct iovec iov;

	IOV_STR(&iov, "ERR iostat unavailable\r\n");
	mog_mgmt_writev(mgmt, &iov, 1);
}

void mog_mgmt_fn_aio_threads(struct mog_mgmt *mgmt, char *buf)
{
	char *end;
	unsigned long long nr;
	char *nptr = buf + mgmt->mark[0];
	char *eor = nptr + mgmt->mark[1] - mgmt->mark[0];

	assert((*eor == '\n' || *eor == '\r') && "missing end-of-record");
	*eor = 0;

	nr = strtoull(nptr, &end, 10);
	assert(*end == 0 && "ragel misfed mog_mgmt_fn_set_aio_threads");

	if (nr > 0 && nr <= (size_t)INT_MAX)
		mog_svc_aio_threads_enqueue(mgmt->svc, (unsigned)nr);

	mog_mgmt_fn_blank(mgmt);
}

git clone https://yhbt.net/cmogstored.git