forked from jcnelson/vdev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkqueue.cpp
More file actions
565 lines (390 loc) · 14.1 KB
/
workqueue.cpp
File metadata and controls
565 lines (390 loc) · 14.1 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
vdev: a virtual device manager for *nix
Copyright (C) 2014 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU General
Public License, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#include "workqueue.h"
#include "os/methods.h"
#include "action.h"
#include "vdev.h"
#include "fskit/fskit.h"
// create a request
// return 0 on success
// return -ENOMEM if we can't allocate device parameters
int vdev_device_request_init( struct vdev_device_request* req, struct vdev_state* state, vdev_device_request_t req_type, char const* path ) {
memset( req, 0, sizeof(struct vdev_device_request) );
if( path != NULL ) {
req->path = vdev_strdup_or_null( path );
if( req->path == NULL ) {
return -ENOMEM;
}
}
req->state = state;
req->type = req_type;
req->params = new (nothrow) vdev_device_params_t();
if( req->params == NULL ) {
if( req->path != NULL ) {
free( req->path );
}
return -ENOMEM;
}
return 0;
}
// free a request
int vdev_device_request_free( struct vdev_device_request* req ) {
if( req->params != NULL ) {
delete req->params;
req->params = NULL;
}
if( req->path != NULL ) {
free( req->path );
req->path = NULL;
}
memset( req, 0, sizeof(struct vdev_device_request) );
return 0;
}
// add a device parameter (must be unique)
// return 0 on success
// return -EEXIST if the parameter exists
// return -ENOMEM if OOM
int vdev_device_request_add_param( struct vdev_device_request* req, char const* key, char const* value ) {
try {
vdev_device_params_t::iterator itr = req->params->find( string(key) );
if( itr == req->params->end() ) {
(*req->params)[ string(key) ] = string(value);
}
else {
return -EEXIST;
}
}
catch ( bad_alloc& ba ) {
return -ENOMEM;
}
return 0;
}
// create a KEY=VALUE string
static int vdev_device_request_make_env_str( char const* key, char const* value, char** ret ) {
char* e = VDEV_CALLOC( char, strlen(key) + 1 + strlen(value) + 1 );
if( e == NULL ) {
return -ENOMEM;
}
sprintf(e, "%s=%s", key, value );
*ret = e;
return 0;
}
// action to const string
static char const* vdev_device_request_type_to_string( vdev_device_request_t req ) {
static char const* req_tbl[] = {
"ADD",
"REMOVE",
"ANY",
"NONE"
};
if( req < 0 || req > VDEV_DEVICE_ANY ) {
return req_tbl[3];
}
return req_tbl[req-1];
}
// mode to const string
static char const* vdev_device_request_mode_to_string( mode_t mode ) {
static char const* blk_str = "BLOCK";
static char const* chr_str = "CHAR";
static char const* none_str = "NONE";
if( S_ISBLK(mode) ) {
return blk_str;
}
else if( S_ISCHR(mode) ) {
return chr_str;
}
return none_str;
}
// convert a device request to a list of null-terminated KEY=VALUE environment variable strings
// put the resulting vector into **ret_env, and put the number of variables into *num_env
// return 0 on success
// return negative on error
int vdev_device_request_to_env( struct vdev_device_request* req, char*** ret_env, size_t* num_env ) {
// type --> VDEV_ACTION
// path --> VDEV_PATH
// dev --> VDEV_MAJOR, VDEV_MINOR
// mode --> VDEV_MODE
// params --> VDEV_OS_*
// mountpoint --> VDEV_MOUNTPOINT
size_t num_vars = 6 + req->params->size();
int i = 0;
int rc = 0;
char dev_buf[50];
char** env = VDEV_CALLOC( char*, num_vars + 1 );
if( env == NULL ) {
return -ENOMEM;
}
rc = vdev_device_request_make_env_str( "VDEV_MOUNTPOINT", req->state->mountpoint, &env[i] );
if( rc != 0 ) {
VDEV_FREE_LIST( env );
return rc;
}
i++;
rc = vdev_device_request_make_env_str( "VDEV_ACTION", vdev_device_request_type_to_string( req->type ), &env[i] );
if( rc != 0 ) {
VDEV_FREE_LIST( env );
return rc;
}
i++;
rc = vdev_device_request_make_env_str( "VDEV_PATH", req->path, &env[i] );
if( rc != 0 ) {
VDEV_FREE_LIST( env );
return rc;
}
i++;
sprintf(dev_buf, "%u", major(req->dev) );
rc = vdev_device_request_make_env_str( "VDEV_MAJOR", dev_buf, &env[i] );
if( rc != 0 ) {
VDEV_FREE_LIST( env );
return rc;
}
i++;
sprintf(dev_buf, "%u", minor(req->dev) );
rc = vdev_device_request_make_env_str( "VDEV_MINOR", dev_buf, &env[i] );
if( rc != 0 ) {
VDEV_FREE_LIST( env );
return rc;
}
i++;
rc = vdev_device_request_make_env_str( "VDEV_MODE", vdev_device_request_mode_to_string( req->mode ), &env[i] );
if( rc != 0 ) {
VDEV_FREE_LIST( env );
return rc;
}
i++;
// add all OS-specific parameters
for( vdev_device_params_t::iterator itr = req->params->begin(); itr != req->params->end(); itr++ ) {
char const* param_key = itr->first.c_str();
char const* param_value = itr->second.c_str();
// prepend with "VDEV_OS_"
char* varname = VDEV_CALLOC( char, strlen(param_key) + 1 + strlen("VDEV_OS_") );
if( varname == NULL ) {
VDEV_FREE_LIST( env );
return rc;
}
sprintf( varname, "VDEV_OS_%s", param_key );
rc = vdev_device_request_make_env_str( varname, param_value, &env[i] );
free( varname );
if( rc != 0 ) {
VDEV_FREE_LIST( env );
return rc;
}
i++;
}
*ret_env = env;
*num_env = i;
return 0;
}
// set the path
int vdev_device_request_set_path( struct vdev_device_request* req, char const* path ) {
if( req->path != NULL ) {
free( req->path );
}
req->path = vdev_strdup_or_null( path );
return 0;
}
// set the device
int vdev_device_request_set_dev( struct vdev_device_request* req, dev_t dev ) {
req->dev = dev;
return 0;
}
// device request sanity check
// return 0 if valid
// return -EINVAL if not valid
int vdev_device_request_sanity_check( struct vdev_device_request* req ) {
if( req->path == NULL ) {
vdev_error("request %p missing path\n", req );
return -EINVAL;
}
if( req->type == VDEV_DEVICE_INVALID ) {
vdev_error("request %p has no request type\n", req );
return -EINVAL;
}
if( req->params == NULL ) {
vdev_error("request %p has no params\n", req );
return -EINVAL;
}
return 0;
}
// set the request type
int vdev_device_request_set_type( struct vdev_device_request* req, vdev_device_request_t req_type ) {
req->type = req_type;
return 0;
}
// set the request device mode
int vdev_device_request_set_mode( struct vdev_device_request* req, mode_t mode ) {
req->mode = mode;
return 0;
}
// handler to add a device
static int vdev_device_add( struct fskit_wreq* wreq, void* cls ) {
int rc = 0;
struct vdev_device_request* req = (struct vdev_device_request*)cls;
char* renamed_path = NULL;
// do the rename, possibly generating it
rc = vdev_action_create_path( req, req->state->acts, req->state->num_acts, &renamed_path );
if( rc != 0 ) {
vdev_error("vdev_action_create_path('%s') rc = %d\n", req->path, rc);
// done with this request
vdev_device_request_free( req );
free( req );
return rc;
}
if( renamed_path == NULL ) {
renamed_path = vdev_strdup_or_null( req->path );
}
vdev_debug("ADD device %p type '%s' at '%s' (%d:%d, original path='%s')\n", req, (S_ISBLK(req->mode) ? "block" : S_ISCHR(req->mode) ? "char" : "unknown"), renamed_path, major(req->dev), minor(req->dev), req->path );
if( renamed_path != NULL && req->dev != 0 && req->mode != 0 ) {
// make the device file
char* fp = fskit_fullpath( req->state->mountpoint, renamed_path, NULL );
char* fp_dir = fskit_dirname( fp, NULL );
if( strchr(renamed_path, '/') != NULL ) {
// make sure the directories leading to this path exist
rc = vdev_mkdirs( fp_dir, strlen(req->state->mountpoint), 0777 );
if( rc != 0 ) {
vdev_error("mkdirs(%s) rc = %d\n", fp_dir, rc );
}
}
free( fp_dir );
if( rc == 0 ) {
// call into our mknod() via FUSE (waking up inotify/kqueue listeners)
rc = mknod( fp, req->mode | 0777, req->dev );
if( rc != 0 ) {
vdev_error("mknod(%s, dev=(%u, %u)) rc = %d\n", fp, major(req->dev), minor(req->dev), rc );
}
else {
// save all OS-specific attributes to this, via setxattr
for( vdev_device_params_t::iterator itr = req->params->begin(); itr != req->params->end(); itr++ ) {
char const* param_name = itr->first.c_str();
char const* param_value = itr->second.c_str();
char* vdev_param_name = VDEV_CALLOC( char, strlen(VDEV_OS_XATTR_NAMESPACE) + 1 + strlen(param_name) + 1 );
if( vdev_param_name == NULL ) {
rc = -ENOMEM;
break;
}
sprintf(vdev_param_name, "%s%s", VDEV_OS_XATTR_NAMESPACE, param_name );
rc = setxattr( fp, vdev_param_name, param_value, strlen(param_value) + 1, 0 );
if( rc != 0 ) {
rc = -errno;
vdev_error("WARN: setxattr('%s' = '%s') rc = %d\n", vdev_param_name, param_value, rc );
// not a fatal error...
rc = 0;
}
free( vdev_param_name );
}
}
}
free( fp );
}
if( rc == 0 ) {
// success! call all ADD actions
rc = vdev_action_run_commands( req, req->state->acts, req->state->num_acts );
if( rc != 0 ) {
vdev_error("vdev_action_run_commands(ADD %p, dev=(%u, %u)) rc = %d\n", req, major(req->dev), minor(req->dev), rc );
}
}
if( renamed_path != NULL ) {
free( renamed_path );
}
// done with this request
vdev_device_request_free( req );
free( req );
return rc;
}
// handler to remove a device (unlink)
// need to fork to do this
static int vdev_device_remove( struct fskit_wreq* wreq, void* cls ) {
int rc = 0;
struct vdev_device_request* req = (struct vdev_device_request*)cls;
char* renamed_path = NULL;
// do the rename, possibly generating it
rc = vdev_action_create_path( req, req->state->acts, req->state->num_acts, &renamed_path );
if( rc != 0 ) {
vdev_error("vdev_action_create_path('%s') rc = %d\n", req->path, rc);
// done with this request
vdev_device_request_free( req );
free( req );
return rc;
}
if( renamed_path == NULL ) {
renamed_path = vdev_strdup_or_null( req->path );
}
vdev_debug("REMOVE device %p type '%s' at '%s' (%d:%d) (original path='%s')\n", req, (S_ISBLK(req->mode) ? "block" : S_ISCHR(req->mode) ? "char" : "unknown"), renamed_path, major(req->dev), minor(req->dev), req->path );
if( renamed_path != NULL ) {
// child
char* fp = fskit_fullpath( req->state->mountpoint, req->path, NULL );
// call into our unlink() via FUSE (waking up inotify/kqueue listeners)
rc = unlink( fp );
if( rc != 0 ) {
rc = -errno;
if( rc != -ENOENT ) {
vdev_error("unlink(%s) rc = %d\n", fp, rc );
}
else {
// not an error--someone else beat us to it
rc = 0;
}
}
free( fp );
}
if( rc == 0 ) {
// call all REMOVE actions
rc = vdev_action_run_commands( req, req->state->acts, req->state->num_acts );
if( rc != 0 ) {
vdev_error("vdev_action_run_all(REMOVE %p) rc = %d\n", req, rc );
}
}
if( renamed_path != NULL ) {
free( renamed_path );
}
// done with this request
vdev_device_request_free( req );
free( req );
return rc;
}
// enqueue a device request
int vdev_device_request_enqueue( struct fskit_wq* wq, struct vdev_device_request* req ) {
int rc = 0;
struct fskit_wreq wreq;
// sanity check
rc = vdev_device_request_sanity_check( req );
if( rc != 0 ) {
vdev_error("Invalid device request (type %d)\n", req->type );
return -EINVAL;
}
// which handler?
switch( req->type ) {
case VDEV_DEVICE_ADD: {
fskit_wreq_init( &wreq, vdev_device_add, req, 0 );
break;
}
case VDEV_DEVICE_REMOVE: {
fskit_wreq_init( &wreq, vdev_device_remove, req, 0 );
break;
}
default: {
vdev_error("Invalid device request type %d\n", req->type );
return -EINVAL;
}
}
rc = fskit_wq_add( wq, &wreq );
if( rc != 0 ) {
vdev_error("fskit_wq_add('%s') rc = %d\n", req->path, rc );
}
return rc;
}