-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathggatelog.c
367 lines (341 loc) · 8.94 KB
/
ggatelog.c
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2004 Pawel Jakub Dawidek <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#if __BSD_VISIBLE
#include <stdbool.h>
#endif
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <err.h>
#include <errno.h>
#include <assert.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/bio.h>
#include <sys/disk.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/syslog.h>
#include <geom/gate/g_gate.h>
#include "ggate.h"
#include "logstor.h"
static enum {
LOG_UNSET,
LOG_CREATE,
LOG_DESTROY,
LOG_LIST,
LOG_RESCUE,
LOG_INIT,
} log_action = LOG_UNSET;
static const char *log_path = NULL;
static int log_unit = G_GATE_UNIT_AUTO;
static unsigned log_flags = 0;
static int log_force = 0;
static unsigned log_sectorsize = 0;
static unsigned log_timeout = G_GATE_TIMEOUT;
static void
log_usage(void)
{
fprintf(stderr, "usage: %s create [-v] [-o <ro|wo|rw>] "
"[-s sectorsize] [-t timeout] [-u unit] <path>\n", getprogname());
fprintf(stderr, " %s rescue [-v] [-o <ro|wo|rw>] <-u unit> "
"<path>\n", getprogname());
fprintf(stderr, " %s destroy [-f] <-u unit>\n", getprogname());
fprintf(stderr, " %s list [-v] [-u unit]\n", getprogname());
exit(EXIT_FAILURE);
}
static int
g_gatelog_openflags(unsigned ggflags)
{
if ((ggflags & G_GATE_FLAG_READONLY) != 0)
return (O_RDONLY);
else if ((ggflags & G_GATE_FLAG_WRITEONLY) != 0)
return (O_WRONLY);
return (O_RDWR);
}
static void
g_gatelog_serve(void)
{
struct g_gate_ctl_io ggio;
size_t bsize;
if (g_gate_verbose == 0) {
if (daemon(0, 0) == -1) {
g_gate_destroy(log_unit, 1);
err(EXIT_FAILURE, "Cannot daemonize");
}
}
g_gate_log(LOG_DEBUG, "Worker created: %u.", getpid());
ggio.gctl_version = G_GATE_VERSION;
ggio.gctl_unit = log_unit;
bsize = log_sectorsize;
ggio.gctl_data = malloc(bsize);
for (;;) {
int error;
once_again:
ggio.gctl_length = bsize;
ggio.gctl_error = 0;
g_gate_ioctl(G_GATE_CMD_START, &ggio);
error = ggio.gctl_error;
switch (error) {
case 0:
break;
case ECANCELED:
/* Exit gracefully. */
free(ggio.gctl_data);
g_gate_close_device();
logstor_close();
logstor_fini();
exit(EXIT_SUCCESS);
case ENOMEM:
/* Buffer too small. */
assert(ggio.gctl_cmd == BIO_DELETE ||
ggio.gctl_cmd == BIO_WRITE);
ggio.gctl_data = realloc(ggio.gctl_data,
ggio.gctl_length);
if (ggio.gctl_data != NULL) {
bsize = ggio.gctl_length;
goto once_again;
}
/* FALLTHROUGH */
case ENXIO:
default:
g_gate_xlog("ioctl(/dev/%s): %s.", G_GATE_CTL_NAME,
strerror(error));
}
error = 0;
switch (ggio.gctl_cmd) {
case BIO_READ:
if ((size_t)ggio.gctl_length > bsize) {
ggio.gctl_data = realloc(ggio.gctl_data,
ggio.gctl_length);
if (ggio.gctl_data != NULL)
bsize = ggio.gctl_length;
else
error = ENOMEM;
}
if (error)
break;
error = logstor_read(ggio.gctl_offset, ggio.gctl_data, ggio.gctl_length);
#if 0
if (error == 0) {
if (pread(fd, ggio.gctl_data, ggio.gctl_length,
ggio.gctl_offset) == -1) {
error = errno;
}
}
#endif
break;
case BIO_DELETE:
// To enable BIO_DELETE, the following statement must be added
// in "case BIO_GETATTR" of g_gate_start() of g_gate.c
// if (g_handleattr_int(pbp, "GEOM::candelete", 1))
// return;
// and the command below must be executed before mounting the device
// tunefs -t enabled /dev/ggate0
error = logstor_delete(ggio.gctl_offset, ggio.gctl_data, ggio.gctl_length);
break;
case BIO_WRITE:
error = logstor_write(ggio.gctl_offset, ggio.gctl_data, ggio.gctl_length);
#if 0
if (pwrite(fd, ggio.gctl_data, ggio.gctl_length,
ggio.gctl_offset) == -1) {
error = errno;
}
#endif
break;
default:
error = EOPNOTSUPP;
}
ggio.gctl_error = error;
g_gate_ioctl(G_GATE_CMD_DONE, &ggio);
}
}
static void
g_gatelog_create(void)
{
struct g_gate_ctl_create ggioc;
logstor_init();
logstor_open(log_path);
memset(&ggioc, 0, sizeof(ggioc));
ggioc.gctl_version = G_GATE_VERSION;
ggioc.gctl_unit = log_unit;
ggioc.gctl_mediasize = (off_t)logstor_get_block_cnt() * SECTOR_SIZE;
//if (log_sectorsize == 0)
//log_sectorsize = g_gate_sectorsize(fd);
log_sectorsize = SECTOR_SIZE;
ggioc.gctl_sectorsize = log_sectorsize;
ggioc.gctl_timeout = log_timeout;
ggioc.gctl_flags = log_flags;
ggioc.gctl_maxcount = 0;
strlcpy(ggioc.gctl_info, log_path, sizeof(ggioc.gctl_info));
g_gate_ioctl(G_GATE_CMD_CREATE, &ggioc);
if (log_unit == -1)
printf("%s%u\n", G_GATE_PROVIDER_NAME, ggioc.gctl_unit);
log_unit = ggioc.gctl_unit;
g_gatelog_serve();
}
static void
g_gatelog_rescue(void)
{
struct g_gate_ctl_cancel ggioc;
logstor_init();
logstor_open(log_path);
ggioc.gctl_version = G_GATE_VERSION;
ggioc.gctl_unit = log_unit;
ggioc.gctl_seq = 0;
g_gate_ioctl(G_GATE_CMD_CANCEL, &ggioc);
g_gatelog_serve();
}
int
main(int argc, char *argv[])
{
if (argc < 2)
log_usage();
if (strcasecmp(argv[1], "create") == 0)
log_action = LOG_CREATE;
else if (strcasecmp(argv[1], "rescue") == 0)
log_action = LOG_RESCUE;
else if (strcasecmp(argv[1], "destroy") == 0)
log_action = LOG_DESTROY;
else if (strcasecmp(argv[1], "list") == 0)
log_action = LOG_LIST;
else if (strcasecmp(argv[1], "init") == 0)
log_action = LOG_INIT;
else
log_usage();
argc -= 1;
argv += 1;
log_flags = 0; // rw
log_sectorsize = SECTOR_SIZE;
g_gate_verbose = 0;
for (;;) {
int ch;
ch = getopt(argc, argv, "ft:u:v");
if (ch == -1)
break;
switch (ch) {
case 'f': // Forcibly destroy ggate provider
if (log_action != LOG_DESTROY)
log_usage();
log_force = 1;
break;
case 'o':
if (log_action != LOG_CREATE && log_action != LOG_RESCUE)
log_usage();
if (strcasecmp("ro", optarg) == 0)
log_flags = G_GATE_FLAG_READONLY;
else if (strcasecmp("wo", optarg) == 0)
log_flags = G_GATE_FLAG_WRITEONLY;
else if (strcasecmp("rw", optarg) == 0)
log_flags = 0;
else {
errx(EXIT_FAILURE,
"Invalid argument for '-o' option.");
}
break;
case 's': // Sector size
if (log_action != LOG_CREATE)
log_usage();
errno = 0;
log_sectorsize = strtoul(optarg, NULL, 10);
if (log_sectorsize == 0 && errno != 0)
errx(EXIT_FAILURE, "Invalid sectorsize.");
break;
case 't': // timeout
if (log_action != LOG_CREATE)
log_usage();
errno = 0;
log_timeout = strtoul(optarg, NULL, 10);
if (log_timeout == 0 && errno != 0)
errx(EXIT_FAILURE, "Invalid timeout.");
break;
case 'u':
errno = 0;
log_unit = strtol(optarg, NULL, 10);
if (log_unit == 0 && errno != 0)
errx(EXIT_FAILURE, "Invalid unit number.");
break;
case 'v':
if (log_action == LOG_DESTROY)
log_usage();
g_gate_verbose++;
break;
default:
log_usage();
}
}
argc -= optind;
argv += optind;
log_path = NULL;
if (argc == 1)
log_path = argv[0];
switch (log_action) {
case LOG_CREATE:
if (log_path == NULL)
log_usage();
g_gate_load_module();
g_gate_open_device();
g_gatelog_create();
break;
case LOG_RESCUE:
if (log_unit == -1) {
fprintf(stderr, "Required unit number.\n");
log_usage();
}
if (log_path == NULL)
log_usage();
g_gate_open_device();
g_gatelog_rescue();
break;
case LOG_DESTROY:
if (log_unit == -1) {
fprintf(stderr, "Required unit number.\n");
log_usage();
}
g_gate_verbose = 1;
g_gate_open_device();
g_gate_destroy(log_unit, log_force);
break;
case LOG_LIST:
g_gate_list(log_unit, g_gate_verbose);
break;
case LOG_INIT:
logstor_superblock_init(log_path);
break;
case LOG_UNSET:
default:
log_usage();
}
g_gate_close_device();
exit(EXIT_SUCCESS);
}