Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2016, Citrix Systems, Inc.
3 : : *
4 : : * All rights reserved.
5 : : *
6 : : * Redistribution and use in source and binary forms, with or without
7 : : * modification, are permitted provided that the following conditions are met:
8 : : *
9 : : * 1. Redistributions of source code must retain the above copyright
10 : : * notice, this list of conditions and the following disclaimer.
11 : : * 2. Redistributions in binary form must reproduce the above copyright
12 : : * notice, this list of conditions and the following disclaimer in the
13 : : * documentation and/or other materials provided with the distribution.
14 : : * 3. Neither the name of the copyright holder nor the names of its
15 : : * contributors may be used to endorse or promote products derived from
16 : : * this software without specific prior written permission.
17 : : *
18 : : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 : : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 : : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 : : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22 : : * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 : : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 : : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 : : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 : : * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 : : * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 : : * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 : : */
30 : :
31 : : #ifdef HAVE_CONFIG_H
32 : : #include "config.h"
33 : : #endif
34 : :
35 : : #include <stdio.h>
36 : : #include <errno.h>
37 : : #include <fcntl.h>
38 : : #include <regex.h>
39 : : #include <unistd.h>
40 : : #include <stdlib.h>
41 : : #include <libgen.h>
42 : : #include <sys/mman.h>
43 : : #include <sys/ioctl.h>
44 : : #include <sys/stat.h>
45 : : #include <sys/types.h>
46 : : #include <sys/file.h>
47 : :
48 : : #include "debug.h"
49 : : #include "libvhd.h"
50 : : #include "tapdisk-image.h"
51 : : #include "tapdisk-driver.h"
52 : : #include "tapdisk-server.h"
53 : : #include "tapdisk-vbd.h"
54 : : #include "tapdisk-metrics.h"
55 : : #include "tapdisk-disktype.h"
56 : : #include "tapdisk-interface.h"
57 : : #include "tapdisk-stats.h"
58 : : #include "tapdisk-storage.h"
59 : : #include "tapdisk-nbdserver.h"
60 : : #include "td-stats.h"
61 : : #include "tapdisk-utils.h"
62 : :
63 : : #define DBG(_level, _f, _a...) tlog_write(_level, _f, ##_a)
64 : : #define ERR(_err, _f, _a...) tlog_error(_err, _f, ##_a)
65 : :
66 : : #define INFO(_f, _a...) tlog_syslog(TLOG_INFO, "vbd: " _f, ##_a)
67 : : #define ERROR(_f, _a...) tlog_syslog(TLOG_WARN, "vbd: " _f, ##_a)
68 : :
69 : : #define TD_VBD_EIO_RETRIES 10
70 : : #define TD_VBD_EIO_SLEEP 1
71 : : #define TD_VBD_WATCHDOG_TIMEOUT 10
72 : :
73 : : char* op_strings[TD_OPS_END] ={"read", "write", "block_status"};
74 : :
75 : : static void tapdisk_vbd_complete_vbd_request(td_vbd_t *, td_vbd_request_t *);
76 : : static int tapdisk_vbd_queue_ready(td_vbd_t *);
77 : : static void tapdisk_vbd_check_complete_requests(td_vbd_t *);
78 : : static void tapdisk_vbd_check_requests_for_issue(td_vbd_t *);
79 : :
80 : : static bool log=true;
81 : :
82 : : /*
83 : : * initialization
84 : : */
85 : :
86 : : static void
87 : : tapdisk_vbd_mark_progress(td_vbd_t *vbd)
88 : : {
89 : 2 : gettimeofday(&vbd->ts, NULL);
90 : : }
91 : :
92 : : td_vbd_t*
93 : 0 : tapdisk_vbd_create(uint16_t uuid)
94 : : {
95 : : td_vbd_t *vbd;
96 : :
97 : 0 : vbd = calloc(1, sizeof(td_vbd_t));
98 [ # # ]: 0 : if (!vbd) {
99 : : EPRINTF("failed to allocate tapdisk state\n");
100 : 0 : return NULL;
101 : : }
102 : :
103 : 0 : shm_init(&vbd->rrd.shm);
104 : :
105 : 0 : vbd->uuid = uuid;
106 : 0 : vbd->req_timeout = TD_VBD_REQUEST_TIMEOUT;
107 : 0 : vbd->watchdog_warned = false;
108 : :
109 : 0 : INIT_LIST_HEAD(&vbd->images);
110 : 0 : INIT_LIST_HEAD(&vbd->new_requests);
111 : 0 : INIT_LIST_HEAD(&vbd->pending_requests);
112 : 0 : INIT_LIST_HEAD(&vbd->failed_requests);
113 : 0 : INIT_LIST_HEAD(&vbd->completed_requests);
114 : 0 : INIT_LIST_HEAD(&vbd->next);
115 : 0 : INIT_LIST_HEAD(&vbd->rings);
116 : 0 : INIT_LIST_HEAD(&vbd->dead_rings);
117 : : tapdisk_vbd_mark_progress(vbd);
118 : :
119 : 0 : return vbd;
120 : : }
121 : :
122 : : int
123 : 0 : tapdisk_vbd_initialize(int rfd, int wfd, uint16_t uuid)
124 : : {
125 : : td_vbd_t *vbd;
126 : :
127 : 0 : vbd = tapdisk_server_get_vbd(uuid);
128 [ # # ]: 0 : if (vbd) {
129 : 0 : EPRINTF("duplicate vbds! %u\n", uuid);
130 : 0 : return -EEXIST;
131 : : }
132 : :
133 : 0 : vbd = tapdisk_vbd_create(uuid);
134 [ # # ]: 0 : if (!vbd) {
135 : : EPRINTF("failed to create vbd\n");
136 : 0 : return -ENOMEM;
137 : : }
138 : :
139 : 0 : tapdisk_server_add_vbd(vbd);
140 : :
141 : 0 : return 0;
142 : : }
143 : :
144 : : static inline void
145 : : tapdisk_vbd_add_image(td_vbd_t *vbd, td_image_t *image)
146 : : {
147 : : list_add_tail(&image->next, &vbd->images);
148 : : }
149 : :
150 : : static inline int
151 : : tapdisk_vbd_is_last_image(td_vbd_t *vbd, td_image_t *image)
152 : : {
153 : 0 : return list_is_last(&image->next, &vbd->images);
154 : : }
155 : :
156 : : static inline td_image_t *
157 : : tapdisk_vbd_first_image(td_vbd_t *vbd)
158 : : {
159 : 1 : td_image_t *image = NULL;
160 [ # # ][ + - ]: 1 : if (!list_empty(&vbd->images))
[ # # ][ # # ]
[ # # # # ]
[ # # ][ # # ]
161 : 1 : image = list_entry(vbd->images.next, td_image_t, next);
162 : : return image;
163 : : }
164 : :
165 : : static inline td_image_t *
166 : : tapdisk_vbd_last_image(td_vbd_t *vbd)
167 : : {
168 : : td_image_t *image = NULL;
169 : : if (!list_empty(&vbd->images))
170 : : image = list_entry(vbd->images.prev, td_image_t, next);
171 : : return image;
172 : : }
173 : :
174 : : static inline td_image_t *
175 : 0 : tapdisk_vbd_next_image(td_image_t *image)
176 : : {
177 : 0 : return list_entry(image->next.next, td_image_t, next);
178 : : }
179 : :
180 : : static int
181 : : tapdisk_vbd_validate_chain(td_vbd_t *vbd)
182 : : {
183 : 0 : return tapdisk_image_validate_chain(&vbd->images);
184 : : }
185 : :
186 : : static int
187 : 0 : vbd_stats_destroy(td_vbd_t *vbd) {
188 : :
189 : 0 : int err = 0;
190 : :
191 [ # # ]: 0 : ASSERT(vbd);
192 : :
193 : 0 : err = shm_destroy(&vbd->rrd.shm);
194 [ # # ]: 0 : if (unlikely(err)) {
195 : 0 : EPRINTF("failed to destroy RRD file: %s\n", strerror(err));
196 : : goto out;
197 : : }
198 : :
199 : 0 : free(vbd->rrd.shm.path);
200 : 0 : vbd->rrd.shm.path = NULL;
201 : :
202 : : out:
203 : 0 : return -err;
204 : : }
205 : :
206 : : static int
207 : 0 : vbd_stats_create(td_vbd_t *vbd) {
208 : :
209 : : int err;
210 : :
211 [ # # ]: 0 : ASSERT(vbd);
212 : :
213 : 0 : err = mkdir("/dev/shm/metrics", S_IRUSR | S_IWUSR);
214 [ # # ]: 0 : if (likely(err)) {
215 : 0 : err = errno;
216 [ # # ]: 0 : if (unlikely(err != EEXIST))
217 : : goto out;
218 : : else
219 : : err = 0;
220 : : }
221 : :
222 : : /*
223 : : * FIXME Rename this to something like "vbd3-domid-devid". Consider
224 : : * consolidating this with the io_ring shared memory file. Check if blkback
225 : : * exports the same information in some sysfs file and if so move this to
226 : : * the ring location.
227 : : */
228 : 0 : err = asprintf(&vbd->rrd.shm.path, "/dev/shm/metrics/tap-%d-%d", getpid(),
229 : 0 : vbd->uuid);
230 [ # # ]: 0 : if (err == -1) {
231 : 0 : err = errno;
232 : 0 : vbd->rrd.shm.path = NULL;
233 : 0 : EPRINTF("failed to create metric file: %s\n", strerror(err));
234 : : goto out;
235 : : }
236 : 0 : err = 0;
237 : :
238 : 0 : vbd->rrd.shm.size = PAGE_SIZE;
239 : 0 : err = shm_create(&vbd->rrd.shm);
240 [ # # ]: 0 : if (err)
241 : 0 : EPRINTF("failed to create RRD: %s\n", strerror(err));
242 : :
243 : : out:
244 [ # # ]: 0 : if (err) {
245 : 0 : int err2 = vbd_stats_destroy(vbd);
246 [ # # ]: 0 : if (err2)
247 : 0 : EPRINTF("failed to clean up failed RRD shared memory creation: "
248 : : "%s (error ignored)\n", strerror(-err2));
249 : : }
250 : 0 : return -err;
251 : : }
252 : :
253 : : void
254 : 0 : tapdisk_vbd_close_vdi(td_vbd_t *vbd)
255 : : {
256 : : int err;
257 : :
258 : 0 : err = vbd_stats_destroy(vbd);
259 [ # # ]: 0 : if (err) {
260 : 0 : EPRINTF("failed to destroy RRD stats file: %s (error ignored)\n",
261 : : strerror(-err));
262 : : }
263 : :
264 : 0 : err = td_metrics_vdi_stop(&vbd->vdi_stats);
265 [ # # ]: 0 : if (err) {
266 : 0 : EPRINTF("failed to destroy stats file: %s\n", strerror(-err));
267 : : }
268 : :
269 : 0 : tapdisk_image_close_chain(&vbd->images);
270 : :
271 [ # # ][ # # ]: 0 : if (vbd->secondary &&
272 : 0 : vbd->secondary_mode != TD_VBD_SECONDARY_MIRROR) {
273 : 0 : tapdisk_image_close(vbd->secondary);
274 : 0 : vbd->secondary = NULL;
275 : : }
276 : :
277 [ # # ]: 0 : if (vbd->retired) {
278 : 0 : tapdisk_image_close(vbd->retired);
279 : 0 : vbd->retired = NULL;
280 : : }
281 : :
282 : 0 : td_flag_set(vbd->state, TD_VBD_CLOSED);
283 : 0 : }
284 : :
285 : : static int
286 : 0 : tapdisk_vbd_add_block_cache(td_vbd_t *vbd)
287 : : {
288 : : td_image_t *cache, *image, *target, *tmp;
289 : : int err;
290 : :
291 : 0 : target = NULL;
292 : :
293 [ # # ]: 0 : tapdisk_vbd_for_each_image(vbd, image, tmp)
294 [ # # ]: 0 : if (td_flag_test(image->flags, TD_OPEN_RDONLY) &&
295 : : td_flag_test(image->flags, TD_OPEN_SHAREABLE)) {
296 : : target = image;
297 : : break;
298 : : }
299 : :
300 [ # # ]: 0 : if (!target)
301 : : return 0;
302 : :
303 : 0 : cache = tapdisk_image_allocate(target->name,
304 : : DISK_TYPE_BLOCK_CACHE,
305 : : target->flags);
306 [ # # ]: 0 : if (!cache)
307 : : return -ENOMEM;
308 : :
309 : : /* try to load existing cache */
310 : 0 : err = td_load(cache);
311 [ # # ]: 0 : if (!err)
312 : : goto done;
313 : :
314 : : /* hack driver to send open() correct image size */
315 [ # # ]: 0 : if (!target->driver) {
316 : : err = -ENODEV;
317 : : goto fail;
318 : : }
319 : :
320 : 0 : cache->driver = tapdisk_driver_allocate(cache->type,
321 : 0 : cache->name,
322 : : cache->flags);
323 [ # # ]: 0 : if (!cache->driver) {
324 : : err = -ENOMEM;
325 : : goto fail;
326 : : }
327 : :
328 : 0 : cache->driver->info = target->driver->info;
329 : :
330 : : /* try to open new cache */
331 : 0 : err = td_open(cache, &vbd->encryption);
332 [ # # ]: 0 : if (!err)
333 : : goto done;
334 : :
335 : : fail:
336 : : /* give up */
337 : 0 : tapdisk_image_free(target);
338 : 0 : tapdisk_image_free(cache);
339 : 0 : return err;
340 : :
341 : : done:
342 : : /* insert cache before image */
343 : 0 : list_add(&cache->next, target->next.prev);
344 : 0 : return 0;
345 : : }
346 : :
347 : : static int
348 : 0 : tapdisk_vbd_add_local_cache(td_vbd_t *vbd)
349 : : {
350 : : td_image_t *cache, *parent;
351 : : int err;
352 : :
353 : 0 : parent = tapdisk_vbd_first_image(vbd);
354 [ # # ]: 0 : if (tapdisk_vbd_is_last_image(vbd, parent)) {
355 : : DPRINTF("Single-image chain, nothing to cache");
356 : 0 : return 0;
357 : : }
358 : :
359 : 0 : cache = tapdisk_image_allocate(parent->name,
360 : : DISK_TYPE_LCACHE,
361 : : parent->flags);
362 : :
363 [ # # ]: 0 : if (!cache)
364 : : return -ENOMEM;
365 : :
366 : : /* try to load existing cache */
367 : 0 : err = td_load(cache);
368 [ # # ]: 0 : if (!err)
369 : : goto done;
370 : :
371 : 0 : cache->driver = tapdisk_driver_allocate(cache->type,
372 : 0 : cache->name,
373 : : cache->flags);
374 [ # # ]: 0 : if (!cache->driver) {
375 : : err = -ENOMEM;
376 : : goto fail;
377 : : }
378 : :
379 : 0 : cache->driver->info = parent->driver->info;
380 : :
381 : : /* try to open new cache */
382 : 0 : err = td_open(cache, &vbd->encryption);
383 [ # # ]: 0 : if (!err)
384 : : goto done;
385 : :
386 : : fail:
387 : 0 : tapdisk_image_free(cache);
388 : 0 : return err;
389 : :
390 : : done:
391 : : /* insert cache right above leaf image */
392 : 0 : list_add(&cache->next, &parent->next);
393 : :
394 : : DPRINTF("Added local_cache driver\n");
395 : 0 : return 0;
396 : : }
397 : :
398 : : int
399 : 0 : tapdisk_vbd_add_secondary(td_vbd_t *vbd)
400 : : {
401 : 0 : td_image_t *leaf, *second = NULL;
402 : : const char *path;
403 : : int type, err;
404 : :
405 [ # # ]: 0 : if (strcmp(vbd->secondary_name, "null") == 0) {
406 : : DPRINTF("Removing secondary image\n");
407 : 0 : vbd->secondary_mode = TD_VBD_SECONDARY_DISABLED;
408 : 0 : vbd->secondary = NULL;
409 : 0 : vbd->nbd_mirror_failed = 0;
410 : 0 : return 0;
411 : : }
412 : :
413 : 0 : DPRINTF("Adding secondary image: %s\n", vbd->secondary_name);
414 : :
415 : 0 : type = tapdisk_disktype_parse_params(vbd->secondary_name, &path);
416 [ # # ]: 0 : if (type < 0)
417 : : return type;
418 : :
419 : 0 : leaf = tapdisk_vbd_first_image(vbd);
420 [ # # ]: 0 : if (!leaf) {
421 : : err = -EINVAL;
422 : : goto fail;
423 : : }
424 : :
425 : 0 : err = tapdisk_image_open(type, path, leaf->flags, &vbd->encryption, &second);
426 [ # # ]: 0 : if (err) {
427 [ # # ]: 0 : if (type == DISK_TYPE_NBD)
428 : 0 : vbd->nbd_mirror_failed = 1;
429 : :
430 : 0 : vbd->secondary=NULL;
431 : 0 : vbd->secondary_mode=TD_VBD_SECONDARY_DISABLED;
432 : :
433 : 0 : goto fail;
434 : : }
435 : :
436 [ # # ]: 0 : if (second->info.size != leaf->info.size) {
437 : 0 : EPRINTF("Secondary image size %"PRIu64" != image size %"PRIu64"\n",
438 : : second->info.size, leaf->info.size);
439 : : err = -EINVAL;
440 : : goto fail;
441 : : }
442 : :
443 : 0 : vbd->secondary = second;
444 : 0 : leaf->flags |= TD_IGNORE_ENOSPC;
445 [ # # ]: 0 : if (td_flag_test(vbd->flags, TD_OPEN_STANDBY)) {
446 : : DPRINTF("In standby mode\n");
447 : 0 : vbd->secondary_mode = TD_VBD_SECONDARY_STANDBY;
448 : : } else {
449 : : DPRINTF("In mirror mode\n");
450 : 0 : vbd->secondary_mode = TD_VBD_SECONDARY_MIRROR;
451 : : /*
452 : : * we actually need this image to also be part of the chain,
453 : : * since it may already contain data
454 : : */
455 : 0 : list_add(&second->next, &leaf->next);
456 : : }
457 : :
458 : : DPRINTF("Added secondary image\n");
459 : : return 0;
460 : :
461 : : fail:
462 [ # # ]: 0 : if (second)
463 : 0 : tapdisk_image_close(second);
464 : 0 : return err;
465 : : }
466 : :
467 : 0 : static void signal_enospc(td_vbd_t *vbd)
468 : : {
469 : : int fd, err;
470 : : char *fn;
471 : :
472 : 0 : err = asprintf(&fn, BLKTAP2_ENOSPC_SIGNAL_FILE"%d", vbd->uuid);
473 [ # # ]: 0 : if (err == -1) {
474 : : EPRINTF("Failed to signal ENOSPC condition\n");
475 : 0 : return;
476 : : }
477 : :
478 : 0 : fd = open(fn, O_WRONLY | O_CREAT | O_NONBLOCK, 0666);
479 [ # # ]: 0 : if (fd == -1)
480 : : EPRINTF("Failed to open file to signal ENOSPC condition\n");
481 : : else
482 : 0 : close(fd);
483 : :
484 : 0 : free(fn);
485 : : }
486 : :
487 : : #if 0
488 : : static int
489 : : tapdisk_vbd_open_index(td_vbd_t *vbd)
490 : : {
491 : : int err;
492 : : char *path;
493 : : td_flag_t flags;
494 : : td_image_t *last, *image;
495 : :
496 : : last = tapdisk_vbd_last_image(vbd);
497 : : err = asprintf(&path, "%s.bat", last->name);
498 : : if (err == -1)
499 : : return -errno;
500 : :
501 : : err = access(path, R_OK);
502 : : if (err == -1) {
503 : : free(path);
504 : : return -errno;
505 : : }
506 : :
507 : : flags = vbd->flags | TD_OPEN_RDONLY | TD_OPEN_SHAREABLE;
508 : : image = tapdisk_image_allocate(path, DISK_TYPE_VINDEX, flags);
509 : : if (!image) {
510 : : err = -ENOMEM;
511 : : goto fail;
512 : : }
513 : :
514 : : err = td_open(image);
515 : : if (err)
516 : : goto fail;
517 : :
518 : : tapdisk_vbd_add_image(vbd, image);
519 : : return 0;
520 : :
521 : : fail:
522 : : if (image)
523 : : tapdisk_image_free(image);
524 : : free(path);
525 : : return err;
526 : : }
527 : : #endif
528 : :
529 : 0 : static int tapdisk_vbd_add_dirty_log(td_vbd_t *vbd)
530 : : {
531 : : int err;
532 : : td_driver_t *driver;
533 : : td_image_t *log, *parent;
534 : :
535 : 0 : driver = NULL;
536 : 0 : log = NULL;
537 : :
538 : 0 : DPRINTF("CBT:tapdisk_vbd_add_dirty_log called for %s with log file %s\n",
539 : : vbd->name, vbd->logpath);
540 : :
541 : 0 : parent = tapdisk_vbd_first_image(vbd);
542 : :
543 : 0 : DPRINTF("CBT: Size in Image: %"PRIu64" sectors\n", parent->info.size);
544 : :
545 : 0 : log = tapdisk_image_allocate(vbd->logpath,
546 : : DISK_TYPE_LOG,
547 : : parent->flags);
548 [ # # ]: 0 : if (!log)
549 : : return -ENOMEM;
550 : :
551 : 0 : driver = tapdisk_driver_allocate(log->type,
552 : 0 : log->name,
553 : : log->flags);
554 [ # # ]: 0 : if (!driver) {
555 : : err = -ENOMEM;
556 : : goto fail;
557 : : }
558 : :
559 : 0 : driver->info = parent->driver->info;
560 : 0 : log->driver = driver;
561 : :
562 : 0 : err = td_open(log, &vbd->encryption);
563 [ # # ]: 0 : if (err)
564 : : goto fail;
565 : :
566 : : /* insert log before image */
567 : 0 : list_add(&log->next, parent->next.prev);
568 : 0 : return 0;
569 : :
570 : : fail:
571 : 0 : tapdisk_image_free(log);
572 : 0 : return err;
573 : : }
574 : :
575 : : int
576 : 0 : tapdisk_vbd_open_vdi(td_vbd_t *vbd, const char *name, td_flag_t flags, int prt_devnum)
577 : : {
578 : 0 : char *tmp = vbd->name;
579 : : int err;
580 : :
581 [ # # ]: 0 : if (!list_empty(&vbd->images)) {
582 : : err = -EBUSY;
583 : : goto fail;
584 : : }
585 : :
586 [ # # ][ # # ]: 0 : if (!name && !vbd->name) {
587 : : err = -EINVAL;
588 : : goto fail;
589 : : }
590 : :
591 [ # # ]: 0 : if (name) {
592 : 0 : vbd->name = strdup(name);
593 [ # # ]: 0 : if (!vbd->name) {
594 : 0 : err = -errno;
595 : 0 : goto fail;
596 : : }
597 : : }
598 : :
599 : 0 : err = tapdisk_image_open_chain(vbd->name, flags, prt_devnum, &vbd->encryption, &vbd->images);
600 [ # # ]: 0 : if (err)
601 : : goto fail;
602 : :
603 : 0 : td_flag_clear(vbd->state, TD_VBD_CLOSED);
604 : 0 : vbd->flags = flags;
605 : :
606 [ # # ]: 0 : if (td_flag_test(vbd->flags, TD_OPEN_ADD_LOG)) {
607 [ # # ]: 0 : if (!vbd->logpath) {
608 : : err = -EINVAL;
609 : : goto fail;
610 : : }
611 : 0 : err = tapdisk_vbd_add_dirty_log(vbd);
612 [ # # ]: 0 : if (err)
613 : : goto fail;
614 : : }
615 : :
616 [ # # ]: 0 : if (td_flag_test(vbd->flags, TD_OPEN_ADD_CACHE)) {
617 : 0 : err = tapdisk_vbd_add_block_cache(vbd);
618 [ # # ]: 0 : if (err)
619 : : goto fail;
620 : : }
621 : :
622 : 0 : err = tapdisk_vbd_validate_chain(vbd);
623 [ # # ]: 0 : if (err)
624 : : goto fail;
625 : :
626 [ # # ]: 0 : if (td_flag_test(vbd->flags, TD_OPEN_SECONDARY)) {
627 : 0 : err = tapdisk_vbd_add_secondary(vbd);
628 [ # # ]: 0 : if (err) {
629 [ # # ]: 0 : if (vbd->nbd_mirror_failed != 1)
630 : : goto fail;
631 : 0 : INFO("Ignoring failed NBD secondary attach\n");
632 : 0 : err = 0;
633 : : }
634 : : }
635 : :
636 [ # # ]: 0 : if (td_flag_test(vbd->flags, TD_OPEN_LOCAL_CACHE)) {
637 : 0 : err = tapdisk_vbd_add_local_cache(vbd);
638 [ # # ]: 0 : if (err)
639 : : goto fail;
640 : : }
641 : :
642 : 0 : err = vbd_stats_create(vbd);
643 [ # # ]: 0 : if (err)
644 : : goto fail;
645 : :
646 : 0 : err = td_metrics_vdi_start(vbd->uuid, &vbd->vdi_stats);
647 [ # # ]: 0 : if (err)
648 : : goto fail;
649 [ # # ]: 0 : if (tmp != vbd->name)
650 : 0 : free(tmp);
651 : :
652 : 0 : return err;
653 : :
654 : : fail:
655 [ # # ]: 0 : if (vbd->name != tmp) {
656 : 0 : free(vbd->name);
657 : 0 : vbd->name = tmp;
658 : : }
659 : :
660 [ # # ]: 0 : if (!list_empty(&vbd->images))
661 : 0 : tapdisk_image_close_chain(&vbd->images);
662 : :
663 : 0 : vbd->flags = 0;
664 : :
665 : 0 : return err;
666 : : }
667 : :
668 : : static int
669 : 0 : open_vbd_marker(int id)
670 : : {
671 : 0 : char *path = NULL;
672 : : int err, fid;
673 : :
674 : 0 : err = asprintf(&path, "%s/tapdisk-%d", BLKTAP2_NP_RUN_DIR, id);
675 [ # # ]: 0 : if (err == -1) {
676 : 0 : return -errno;
677 : : }
678 : :
679 : 0 : fid = open(path, O_RDONLY, 0600);
680 [ # # ]: 0 : if (fid == -1) {
681 : 0 : err = -errno;
682 : : EPRINTF("Failed to open VBD marker file for %d\n", id);
683 : : goto out;
684 : : }
685 : :
686 : : return fid;
687 : :
688 : : out:
689 [ # # ]: 0 : if (path) {
690 : 0 : free(path);
691 : : }
692 : 0 : return err;
693 : : }
694 : :
695 : 0 : void tapdisk_vbd_unlock(td_vbd_t *vbd)
696 : : {
697 : 0 : flock(vbd->lock_fd, LOCK_UN);
698 : 0 : close(vbd->lock_fd);
699 : 0 : }
700 : :
701 : 0 : int tapdisk_vbd_lock(td_vbd_t *vbd)
702 : : {
703 : : int fid;
704 : :
705 : 0 : fid = open_vbd_marker(vbd->uuid);
706 [ # # ]: 0 : if (fid < 0) {
707 : : /* Already logged */
708 : : return -1;
709 : : }
710 : :
711 : 0 : vbd->lock_fd = fid;
712 : 0 : flock(vbd->lock_fd, LOCK_EX);
713 : :
714 : 0 : return 0;
715 : : }
716 : :
717 : : /*
718 : : int
719 : : tapdisk_vbd_open(td_vbd_t *vbd, const char *name,
720 : : int minor, const char *ring, td_flag_t flags)
721 : : {
722 : : int err;
723 : :
724 : : err = tapdisk_vbd_open_vdi(vbd, name, flags, -1);
725 : : if (err)
726 : : goto out;
727 : :
728 : : err = tapdisk_vbd_attach(vbd, ring, minor);
729 : : if (err)
730 : : goto out;
731 : :
732 : : return 0;
733 : :
734 : : out:
735 : : tapdisk_vbd_detach(vbd);
736 : : tapdisk_vbd_close_vdi(vbd);
737 : : free(vbd->name);
738 : : vbd->name = NULL;
739 : : return err;
740 : : }
741 : : */
742 : :
743 : : static void
744 : 0 : tapdisk_vbd_queue_count(td_vbd_t *vbd, int *new,
745 : : int *pending, int *failed, int *completed)
746 : : {
747 : : int n, p, f, c;
748 : : td_vbd_request_t *vreq, *tvreq;
749 : :
750 : 0 : n = 0;
751 : 0 : p = 0;
752 : 0 : f = 0;
753 : 0 : c = 0;
754 : :
755 [ # # ]: 0 : tapdisk_vbd_for_each_request(vreq, tvreq, &vbd->new_requests)
756 : 0 : n++;
757 : :
758 [ # # ]: 0 : tapdisk_vbd_for_each_request(vreq, tvreq, &vbd->pending_requests)
759 : 0 : p++;
760 : :
761 [ # # ]: 0 : tapdisk_vbd_for_each_request(vreq, tvreq, &vbd->failed_requests)
762 : 0 : f++;
763 : :
764 [ # # ]: 0 : tapdisk_vbd_for_each_request(vreq, tvreq, &vbd->completed_requests)
765 : 0 : c++;
766 : :
767 : 0 : *new = n;
768 : 0 : *pending = p;
769 : 0 : *failed = f;
770 : 0 : *completed = c;
771 : 0 : }
772 : :
773 : : static int
774 : 0 : tapdisk_vbd_shutdown(td_vbd_t *vbd)
775 : : {
776 : : int new, pending, failed, completed;
777 : :
778 [ # # ]: 0 : if (!list_empty(&vbd->pending_requests))
779 : 0 : return -EAGAIN;
780 : :
781 : 0 : tapdisk_vbd_queue_count(vbd, &new, &pending, &failed, &completed);
782 : :
783 : 0 : DPRINTF("%s: state: 0x%08x, new: 0x%02x, pending: 0x%02x, "
784 : : "failed: 0x%02x, completed: 0x%02x\n",
785 : : vbd->name, vbd->state, new, pending, failed, completed);
786 : 0 : DPRINTF("last activity: %010ld.%06ld, errors: 0x%04"PRIx64", "
787 : : "retries: 0x%04"PRIx64", received: 0x%08"PRIx64", "
788 : : "returned: 0x%08"PRIx64", kicked: 0x%08"PRIx64"\n",
789 : : vbd->ts.tv_sec, vbd->ts.tv_usec,
790 : : vbd->errors, vbd->retries, vbd->received, vbd->returned,
791 : : vbd->kicked);
792 : :
793 : 0 : tapdisk_vbd_close_vdi(vbd);
794 : 0 : tapdisk_vbd_unlock(vbd);
795 : 0 : tapdisk_server_remove_vbd(vbd);
796 : 0 : tapdisk_vbd_free(vbd);
797 : :
798 : 0 : return 0;
799 : : }
800 : :
801 : : void
802 : 0 : tapdisk_vbd_free(td_vbd_t *vbd)
803 : : {
804 : 0 : free(vbd->name);
805 : 0 : free(vbd->encryption.encryption_key);
806 : 0 : free(vbd);
807 : 0 : }
808 : :
809 : : int
810 : 0 : tapdisk_vbd_close(td_vbd_t *vbd)
811 : : {
812 : : /*
813 : : * don't close if any requests are pending in the aio layer
814 : : */
815 [ # # ]: 0 : if (!list_empty(&vbd->pending_requests))
816 : : goto fail;
817 : :
818 : : /*
819 : : * if the queue is still active and we have more
820 : : * requests, try to complete them before closing.
821 : : */
822 [ # # ][ # # ]: 0 : if (tapdisk_vbd_queue_ready(vbd) &&
823 [ # # ]: 0 : (!list_empty(&vbd->new_requests) ||
824 [ # # ]: 0 : !list_empty(&vbd->failed_requests) ||
825 : 0 : !list_empty(&vbd->completed_requests)))
826 : : goto fail;
827 : :
828 : 0 : return tapdisk_vbd_shutdown(vbd);
829 : :
830 : : fail:
831 : 0 : td_flag_set(vbd->state, TD_VBD_SHUTDOWN_REQUESTED);
832 : 0 : DBG(TLOG_WARN, "%s: requests pending\n", vbd->name);
833 : 0 : return -EAGAIN;
834 : : }
835 : :
836 : : /*
837 : : * control operations
838 : : */
839 : :
840 : : void
841 : 0 : tapdisk_vbd_debug(td_vbd_t *vbd)
842 : : {
843 : : td_image_t *image, *tmp;
844 : : int new, pending, failed, completed;
845 : :
846 : 0 : tapdisk_vbd_queue_count(vbd, &new, &pending, &failed, &completed);
847 : :
848 : 0 : DBG(TLOG_WARN, "%s: state: 0x%08x, new: 0x%02x, pending: 0x%02x, "
849 : : "failed: 0x%02x, completed: 0x%02x, last activity: %010ld.%06ld, "
850 : : "errors: 0x%04"PRIx64", retries: 0x%04"PRIx64", "
851 : : "received: 0x%08"PRIx64", returned: 0x%08"PRIx64", "
852 : : "kicked: 0x%08"PRIx64"\n",
853 : : vbd->name, vbd->state, new, pending, failed, completed,
854 : : vbd->ts.tv_sec, vbd->ts.tv_usec, vbd->errors, vbd->retries,
855 : : vbd->received, vbd->returned, vbd->kicked);
856 : :
857 [ # # ]: 0 : tapdisk_vbd_for_each_image(vbd, image, tmp)
858 : 0 : td_debug(image);
859 : 0 : }
860 : :
861 : : static void
862 : 0 : tapdisk_vbd_drop_log(td_vbd_t *vbd)
863 : : {
864 [ # # ]: 0 : if (td_flag_test(vbd->state, TD_VBD_LOG_DROPPED))
865 : 0 : return;
866 : :
867 : 0 : tapdisk_vbd_debug(vbd);
868 : 0 : tlog_precious(0);
869 : 0 : td_flag_set(vbd->state, TD_VBD_LOG_DROPPED);
870 : : }
871 : :
872 : : int
873 : 0 : tapdisk_vbd_get_disk_info(td_vbd_t *vbd, td_disk_info_t *info)
874 : : {
875 [ # # ]: 0 : if (list_empty(&vbd->images))
876 : : return -EINVAL;
877 : :
878 : 0 : *info = tapdisk_vbd_first_image(vbd)->info;
879 : 0 : return 0;
880 : : }
881 : :
882 : : static int
883 : : tapdisk_vbd_queue_ready(td_vbd_t *vbd)
884 : : {
885 : : return (!td_flag_test(vbd->state, TD_VBD_DEAD) &&
886 : : !td_flag_test(vbd->state, TD_VBD_CLOSED) &&
887 : 1 : !td_flag_test(vbd->state, TD_VBD_QUIESCED) &&
888 : : !td_flag_test(vbd->state, TD_VBD_QUIESCE_REQUESTED));
889 : : }
890 : :
891 : : int
892 : 0 : tapdisk_vbd_retry_needed(td_vbd_t *vbd)
893 : : {
894 [ # # ][ # # ]: 0 : return !(list_empty(&vbd->failed_requests) &&
895 : 0 : list_empty(&vbd->new_requests));
896 : : }
897 : :
898 : : int
899 : 0 : tapdisk_vbd_quiesce_queue(td_vbd_t *vbd)
900 : : {
901 [ # # ]: 0 : if (!list_empty(&vbd->pending_requests)) {
902 : 0 : td_flag_set(vbd->state, TD_VBD_QUIESCE_REQUESTED);
903 : 0 : return -EAGAIN;
904 : : }
905 : :
906 : 0 : td_flag_clear(vbd->state, TD_VBD_QUIESCE_REQUESTED);
907 : 0 : td_flag_set(vbd->state, TD_VBD_QUIESCED);
908 : 0 : return 0;
909 : : }
910 : :
911 : : int
912 : 0 : tapdisk_vbd_start_queue(td_vbd_t *vbd)
913 : : {
914 : 0 : td_flag_clear(vbd->state, TD_VBD_QUIESCED);
915 : 0 : td_flag_clear(vbd->state, TD_VBD_QUIESCE_REQUESTED);
916 : : tapdisk_vbd_mark_progress(vbd);
917 : 0 : return 0;
918 : : }
919 : :
920 : : int
921 : 0 : tapdisk_vbd_kill_queue(td_vbd_t *vbd)
922 : : {
923 : 0 : tapdisk_vbd_quiesce_queue(vbd);
924 : 0 : td_flag_set(vbd->state, TD_VBD_DEAD);
925 : 0 : return 0;
926 : : }
927 : :
928 : : #if 0
929 : : static int
930 : : tapdisk_vbd_open_image(td_vbd_t *vbd, td_image_t *image)
931 : : {
932 : : int err;
933 : : td_image_t *parent;
934 : :
935 : : err = td_open(image);
936 : : if (err)
937 : : return err;
938 : :
939 : : if (!tapdisk_vbd_is_last_image(vbd, image)) {
940 : : parent = tapdisk_vbd_next_image(image);
941 : : err = td_validate_parent(image, parent);
942 : : if (err) {
943 : : td_close(image);
944 : : return err;
945 : : }
946 : : }
947 : :
948 : : return 0;
949 : : }
950 : : #endif
951 : :
952 : : /*
953 : : * Pausing a tapdisk can produce a lot of logging if the storage is not available
954 : : * and there are inflight data requests. All the caller to squash the logging
955 : : * when entering a retry process.
956 : : */
957 : 0 : void tapdisk_vbd_squash_pause_logging(bool squash)
958 : : {
959 : 0 : log = !squash;
960 : 0 : }
961 : :
962 : : int
963 : 0 : tapdisk_vbd_pause(td_vbd_t *vbd)
964 : : {
965 : : int err;
966 : : struct td_xenblkif *blkif;
967 : :
968 [ # # ]: 0 : if (log) {
969 : 0 : INFO("pause requested\n");
970 : : }
971 : :
972 : 0 : td_flag_set(vbd->state, TD_VBD_PAUSE_REQUESTED);
973 : :
974 [ # # ]: 0 : if (vbd->nbdserver)
975 : 0 : tapdisk_nbdserver_pause(vbd->nbdserver, log);
976 [ # # ]: 0 : if (vbd->nbdserver_new)
977 : 0 : tapdisk_nbdserver_pause(vbd->nbdserver_new, log);
978 : :
979 [ # # ]: 0 : list_for_each_entry(blkif, &vbd->rings, entry)
980 : 0 : tapdisk_xenblkif_suspend(blkif);
981 : :
982 : 0 : err = tapdisk_vbd_quiesce_queue(vbd);
983 [ # # ]: 0 : if (err)
984 : : return err;
985 : :
986 : :
987 : 0 : tapdisk_vbd_close_vdi(vbd);
988 : :
989 : : /* Don't guard this one as at this point the pause operation is complete */
990 : 0 : INFO("pause completed\n");
991 : :
992 [ # # ]: 0 : if (!list_empty(&vbd->failed_requests))
993 : 0 : INFO("warning: failed requests pending\n");
994 : :
995 : 0 : td_flag_clear(vbd->state, TD_VBD_PAUSE_REQUESTED);
996 : 0 : td_flag_set(vbd->state, TD_VBD_PAUSED);
997 : :
998 : 0 : return 0;
999 : : }
1000 : :
1001 : : int
1002 : 0 : tapdisk_vbd_resume(td_vbd_t *vbd, const char *name)
1003 : : {
1004 : : int i, err;
1005 : : struct td_xenblkif *blkif;
1006 : :
1007 : 0 : DBG(TLOG_DBG, "resume requested\n");
1008 : :
1009 [ # # ]: 0 : if (!td_flag_test(vbd->state, TD_VBD_PAUSED)) {
1010 : 0 : EPRINTF("resume request for unpaused vbd %s\n", vbd->name);
1011 : 0 : return -EINVAL;
1012 : : }
1013 : :
1014 [ # # ]: 0 : for (i = 0; i < TD_VBD_EIO_RETRIES; i++) {
1015 : 0 : err = tapdisk_vbd_open_vdi(vbd, name, vbd->flags | TD_OPEN_STRICT, -1);
1016 [ # # ]: 0 : if (!err)
1017 : : break;
1018 : :
1019 : 0 : sleep(TD_VBD_EIO_SLEEP);
1020 : : }
1021 : :
1022 [ # # ]: 0 : if (!err) {
1023 : : td_disk_info_t disk_info;
1024 : 0 : err = tapdisk_vbd_get_disk_info(vbd, &disk_info);
1025 [ # # ]: 0 : if (err) {
1026 : 0 : EPRINTF("VBD %d failed to get disk info: %s\n", vbd->uuid,
1027 : : strerror(-err));
1028 : 0 : goto resume_failed;
1029 : : }
1030 [ # # ]: 0 : if (vbd->disk_info.size != disk_info.size
1031 [ # # ]: 0 : || vbd->disk_info.sector_size != disk_info.sector_size
1032 [ # # ]: 0 : || vbd->disk_info.info != disk_info.info) {
1033 : 0 : EPRINTF("VBD %d cannot change disk info\n", vbd->uuid);
1034 : : err = -EMEDIUMTYPE;
1035 : : goto resume_failed;
1036 : : }
1037 : : }
1038 : : resume_failed:
1039 [ # # ]: 0 : if (err) {
1040 : 0 : td_flag_set(vbd->state, TD_VBD_RESUME_FAILED);
1041 : 0 : tapdisk_vbd_close_vdi(vbd);
1042 : 0 : return err;
1043 : : }
1044 : 0 : td_flag_clear(vbd->state, TD_VBD_RESUME_FAILED);
1045 : :
1046 : 0 : DBG(TLOG_DBG, "resume completed\n");
1047 : :
1048 : 0 : tapdisk_vbd_start_queue(vbd);
1049 : 0 : td_flag_clear(vbd->state, TD_VBD_PAUSED);
1050 : 0 : td_flag_clear(vbd->state, TD_VBD_PAUSE_REQUESTED);
1051 : 0 : tapdisk_vbd_check_state(vbd);
1052 : :
1053 [ # # ]: 0 : if (vbd->nbdserver)
1054 : 0 : tapdisk_nbdserver_unpause(vbd->nbdserver);
1055 [ # # ]: 0 : if (vbd->nbdserver_new)
1056 : 0 : tapdisk_nbdserver_unpause(vbd->nbdserver_new);
1057 : :
1058 [ # # ]: 0 : list_for_each_entry(blkif, &vbd->rings, entry)
1059 : 0 : tapdisk_xenblkif_resume(blkif);
1060 : :
1061 : :
1062 : 0 : DBG(TLOG_DBG, "state checked\n");
1063 : :
1064 : 0 : return 0;
1065 : : }
1066 : :
1067 : : static int
1068 : : tapdisk_vbd_request_ttl(td_vbd_request_t *vreq,
1069 : : const struct timeval *now)
1070 : : {
1071 : : struct timeval delta;
1072 [ # # ]: 0 : timersub(now, &vreq->ts, &delta);
1073 : 0 : return vreq->vbd->req_timeout - delta.tv_sec;
1074 : : }
1075 : :
1076 : : static int
1077 : 0 : __tapdisk_vbd_request_timeout(td_vbd_request_t *vreq,
1078 : : const struct timeval *now)
1079 : : {
1080 : : int timeout;
1081 : :
1082 : 0 : timeout = tapdisk_vbd_request_ttl(vreq, now) < 0;
1083 [ # # ]: 0 : if (timeout)
1084 : 0 : ERR(vreq->error,
1085 : : "req %s timed out, retried %d times\n",
1086 : : vreq->name, vreq->num_retries);
1087 : :
1088 : 0 : return timeout;
1089 : : }
1090 : :
1091 : : static int
1092 : 0 : tapdisk_vbd_request_timeout(td_vbd_request_t *vreq)
1093 : : {
1094 : : struct timeval now;
1095 : 0 : gettimeofday(&now, NULL);
1096 : 0 : return __tapdisk_vbd_request_timeout(vreq, &now);
1097 : : }
1098 : :
1099 : : static void
1100 : 0 : tapdisk_vbd_check_complete_requests(td_vbd_t *vbd)
1101 : : {
1102 : : td_vbd_request_t *vreq, *tmp;
1103 : : struct timeval now;
1104 : :
1105 : 0 : gettimeofday(&now, NULL);
1106 [ # # ]: 0 : tapdisk_vbd_for_each_request(vreq, tmp, &vbd->failed_requests)
1107 [ # # ]: 0 : if (__tapdisk_vbd_request_timeout(vreq, &now))
1108 : 0 : tapdisk_vbd_complete_vbd_request(vbd, vreq);
1109 : 0 : }
1110 : :
1111 : : static void
1112 : : tapdisk_vbd_check_requests_for_issue(td_vbd_t *vbd)
1113 : : {
1114 [ # # ][ # # ]: 0 : if (!list_empty(&vbd->new_requests) ||
1115 : 0 : !list_empty(&vbd->failed_requests))
1116 : 0 : tapdisk_vbd_issue_requests(vbd);
1117 : : }
1118 : :
1119 : : void
1120 : 0 : tapdisk_vbd_check_state(td_vbd_t *vbd)
1121 : : {
1122 : : struct td_xenblkif *blkif;
1123 : :
1124 : : /* Don't check if we're already quiesced */
1125 [ # # ]: 0 : if (td_flag_test(vbd->state, TD_VBD_QUIESCED))
1126 : 0 : return;
1127 : :
1128 : : /*
1129 : : * TODO don't ignore return value
1130 : : */
1131 [ # # ]: 0 : list_for_each_entry(blkif, &vbd->rings, entry)
1132 : 0 : tapdisk_xenblkif_ring_stats_update(blkif);
1133 : :
1134 : 0 : tapdisk_vbd_check_complete_requests(vbd);
1135 : :
1136 [ # # ]: 0 : if (!td_flag_test(vbd->state, TD_VBD_QUIESCE_REQUESTED) &&
1137 : : !td_flag_test(vbd->state, TD_VBD_PAUSE_REQUESTED))
1138 : : tapdisk_vbd_check_requests_for_issue(vbd);
1139 : :
1140 [ # # ]: 0 : if (td_flag_test(vbd->state, TD_VBD_QUIESCE_REQUESTED))
1141 : 0 : tapdisk_vbd_quiesce_queue(vbd);
1142 : :
1143 [ # # ]: 0 : if (td_flag_test(vbd->state, TD_VBD_PAUSE_REQUESTED))
1144 : 0 : tapdisk_vbd_pause(vbd);
1145 : :
1146 [ # # ]: 0 : if (td_flag_test(vbd->state, TD_VBD_SHUTDOWN_REQUESTED))
1147 : 0 : tapdisk_vbd_close(vbd);
1148 : : }
1149 : :
1150 : 0 : void watchdog_cleared(td_vbd_t *vbd)
1151 : : {
1152 [ # # ]: 0 : if (vbd->watchdog_warned) {
1153 : 0 : DBG(TLOG_WARN, "%s: watchdog timeout: requests were blocked\n", vbd->name);
1154 : : /* Ideally want a direct way to flush the log */
1155 : 0 : tlog_precious(1);
1156 : : }
1157 : 0 : vbd->watchdog_warned = false;
1158 : 0 : }
1159 : :
1160 : : void
1161 : 0 : tapdisk_vbd_check_progress(td_vbd_t *vbd)
1162 : : {
1163 : : time_t diff;
1164 : : struct timeval now, delta;
1165 : :
1166 [ # # ]: 0 : if (list_empty(&vbd->pending_requests)) {
1167 : 0 : watchdog_cleared(vbd);
1168 : 0 : return;
1169 : : }
1170 : :
1171 : 0 : gettimeofday(&now, NULL);
1172 [ # # ]: 0 : timersub(&now, &vbd->ts, &delta);
1173 : 0 : diff = delta.tv_sec;
1174 : :
1175 [ # # ]: 0 : if (diff >= TD_VBD_WATCHDOG_TIMEOUT)
1176 : : {
1177 [ # # ]: 0 : if(tapdisk_vbd_queue_ready(vbd))
1178 : : {
1179 [ # # ]: 0 : if (!vbd->watchdog_warned) {
1180 : 0 : DBG(TLOG_WARN, "%s: watchdog timeout: pending requests "
1181 : : "idle for %ld seconds\n", vbd->name, diff);
1182 : 0 : vbd->watchdog_warned = true;
1183 : : }
1184 : 0 : tapdisk_vbd_drop_log(vbd);
1185 : : }
1186 : : return;
1187 : : }
1188 : :
1189 : 0 : watchdog_cleared(vbd);
1190 : :
1191 : 0 : tapdisk_server_set_max_timeout(TD_VBD_WATCHDOG_TIMEOUT - diff);
1192 : : }
1193 : :
1194 : : /*
1195 : : * request submission
1196 : : */
1197 : :
1198 : : static int
1199 : : tapdisk_vbd_check_queue(td_vbd_t *vbd)
1200 : : {
1201 [ + - ]: 1 : if (list_empty(&vbd->images))
1202 : : return -ENOSYS;
1203 : :
1204 [ + - ]: 1 : if (!tapdisk_vbd_queue_ready(vbd))
1205 : : return -EAGAIN;
1206 : :
1207 : : return 0;
1208 : : }
1209 : :
1210 : : static int
1211 : 0 : tapdisk_vbd_request_should_retry(td_vbd_t *vbd, td_vbd_request_t *vreq)
1212 : : {
1213 [ # # ]: 0 : if (td_flag_test(vbd->state, TD_VBD_DEAD) ||
1214 : : td_flag_test(vbd->state, TD_VBD_SHUTDOWN_REQUESTED))
1215 : : return 0;
1216 : :
1217 [ # # ]: 0 : if (tapdisk_vbd_request_timeout(vreq))
1218 : : return 0;
1219 : :
1220 [ # # ][ # # ]: 0 : switch (abs(vreq->error)) {
1221 : : case EAGAIN:
1222 : : case EBUSY:
1223 : : case EINTR:
1224 : : return 1;
1225 : : }
1226 : :
1227 : 0 : return 0;
1228 : : }
1229 : :
1230 : : static void
1231 : 1 : tapdisk_vbd_complete_vbd_request(td_vbd_t *vbd, td_vbd_request_t *vreq)
1232 : : {
1233 [ - + ]: 1 : if (!vreq->submitting && !vreq->secs_pending) {
1234 [ # # # # ]: 0 : if (vreq->error &&
1235 : 0 : tapdisk_vbd_request_should_retry(vbd, vreq))
1236 : 0 : tapdisk_vbd_move_request(vreq, &vbd->failed_requests);
1237 : : else
1238 : 0 : tapdisk_vbd_move_request(vreq, &vbd->completed_requests);
1239 : : }
1240 : 1 : }
1241 : :
1242 : : static void
1243 : 0 : FIXME_maybe_count_enospc_redirect(td_vbd_t *vbd, td_request_t treq)
1244 : : {
1245 : 1 : int write = treq.op == TD_OP_WRITE;
1246 [ - + ][ # # ]: 1 : if (write &&
1247 [ # # ]: 0 : treq.image == tapdisk_vbd_first_image(vbd) &&
1248 : 0 : vbd->FIXME_enospc_redirect_count_enabled)
1249 : 0 : vbd->FIXME_enospc_redirect_count += treq.secs;
1250 : 0 : }
1251 : :
1252 : : static void
1253 : 2 : __tapdisk_vbd_complete_td_request(td_vbd_t *vbd, td_vbd_request_t *vreq,
1254 : : td_request_t treq, int res)
1255 : : {
1256 : 1 : td_image_t *image = treq.image;
1257 : : int err;
1258 : :
1259 : : long long interval;
1260 : :
1261 : 1 : err = (res <= 0 ? res : -res);
1262 : 1 : vbd->secs_pending -= treq.secs;
1263 : 1 : vreq->secs_pending -= treq.secs;
1264 : :
1265 [ + - ]: 1 : if (err != -EBUSY) {
1266 : 1 : int write = treq.op == TD_OP_WRITE;
1267 : 1 : td_sector_count_add(&image->stats.hits, treq.secs, write);
1268 [ - + ]: 1 : if (err)
1269 : 0 : td_sector_count_add(&image->stats.fail,
1270 : : treq.secs, write);
1271 : :
1272 : 1 : FIXME_maybe_count_enospc_redirect(vbd, treq);
1273 : : }
1274 : :
1275 [ - + ]: 1 : if (err) {
1276 [ # # ]: 0 : if (err != -EBUSY) {
1277 [ # # ][ # # ]: 0 : if (!vreq->error &&
1278 : 0 : err != vreq->prev_error)
1279 [ # # ]: 0 : tlog_drv_error(image->driver, err,
1280 : : "req %s: %s 0x%04x secs @ 0x%08"PRIx64" - %s",
1281 : : vreq->name,
1282 : : op_strings[treq.op],
1283 : : treq.secs, treq.sec, strerror(abs(err)));
1284 : 0 : vbd->errors++;
1285 : : }
1286 [ # # ]: 0 : vreq->error = (vreq->error ? : err);
1287 : : }
1288 : :
1289 : 3 : interval = timeval_to_us(&vbd->ts) - timeval_to_us(&vreq->ts);
1290 : :
1291 [ - + ]: 1 : if(treq.op == TD_OP_READ) {
1292 : 0 : vbd->vdi_stats.stats->read_reqs_completed++;
1293 : 0 : vbd->vdi_stats.stats->read_sectors += treq.secs;
1294 : 0 : vbd->vdi_stats.stats->read_total_ticks += interval;
1295 : : }
1296 : :
1297 [ - + ]: 1 : if(treq.op == TD_OP_WRITE) {
1298 : 0 : vbd->vdi_stats.stats->write_reqs_completed++;
1299 : 0 : vbd->vdi_stats.stats->write_sectors += treq.secs;
1300 : 0 : vbd->vdi_stats.stats->write_total_ticks += interval;
1301 : : }
1302 : :
1303 : 1 : tapdisk_vbd_complete_vbd_request(vbd, vreq);
1304 : 1 : }
1305 : :
1306 : : static void
1307 : 0 : __tapdisk_vbd_reissue_td_request(td_vbd_t *vbd,
1308 : : td_image_t *image, td_request_t treq)
1309 : : {
1310 : : td_image_t *parent;
1311 : : td_vbd_request_t *vreq;
1312 : :
1313 : 0 : vreq = treq.vreq;
1314 : 0 : gettimeofday(&vreq->last_try, NULL);
1315 : :
1316 : 0 : vreq->submitting++;
1317 : :
1318 [ # # ]: 0 : if (tapdisk_vbd_is_last_image(vbd, image)) {
1319 [ # # ]: 0 : if (unlikely(treq.op == TD_OP_BLOCK_STATUS)) {
1320 : 0 : treq.status = TD_BLOCK_STATE_HOLE;
1321 : : } else {
1322 : 0 : memset(treq.buf, 0, (size_t)treq.secs << SECTOR_SHIFT);
1323 : : }
1324 : 0 : td_complete_request(treq, 0);
1325 : 0 : goto done;
1326 : : }
1327 : : /*
1328 : : * If intellicache is enabled, check to confirm mirroring
1329 : : * is disabled due to out of space.
1330 : : */
1331 [ # # ][ # # ]: 0 : if (unlikely(vbd->retired && vbd->retired == image))
1332 : 0 : parent = tapdisk_vbd_first_image(vbd);
1333 : : else
1334 : 0 : parent = tapdisk_vbd_next_image(image);
1335 : :
1336 : 0 : treq.image = parent;
1337 : :
1338 : : /* return zeros for requests that extend beyond end of parent image */
1339 [ # # ]: 0 : if (treq.sec + treq.secs > parent->info.size) {
1340 : 0 : td_request_t clone = treq;
1341 : :
1342 [ # # ]: 0 : if (parent->info.size > treq.sec) {
1343 : 0 : int secs = parent->info.size - treq.sec;
1344 : 0 : clone.sec += secs;
1345 : 0 : clone.secs -= secs;
1346 : 0 : clone.buf += (secs << SECTOR_SHIFT);
1347 : 0 : treq.secs = secs;
1348 : : } else
1349 : 0 : treq.secs = 0;
1350 : :
1351 : 0 : memset(clone.buf, 0, (size_t)clone.secs << SECTOR_SHIFT);
1352 : 0 : td_complete_request(clone, 0);
1353 : :
1354 [ # # ]: 0 : if (!treq.secs)
1355 : : goto done;
1356 : : }
1357 : :
1358 [ # # # # ]: 0 : switch (treq.op) {
1359 : : case TD_OP_WRITE:
1360 : 0 : td_queue_write(parent, treq);
1361 : 0 : break;
1362 : :
1363 : : case TD_OP_READ:
1364 : 0 : td_queue_read(parent, treq);
1365 : 0 : break;
1366 : : case TD_OP_BLOCK_STATUS:
1367 : 0 : td_queue_block_status(parent, &treq);
1368 : 0 : break;
1369 : : }
1370 : :
1371 : : done:
1372 : 0 : vreq->submitting--;
1373 [ # # ]: 0 : if (!vreq->secs_pending)
1374 : 0 : tapdisk_vbd_complete_vbd_request(vbd, vreq);
1375 : 0 : }
1376 : :
1377 : : void
1378 : 0 : tapdisk_vbd_forward_request(td_request_t treq)
1379 : : {
1380 : : td_vbd_t *vbd;
1381 : : td_image_t *image;
1382 : : td_vbd_request_t *vreq;
1383 : :
1384 : 0 : image = treq.image;
1385 : 0 : vreq = treq.vreq;
1386 : 0 : vbd = vreq->vbd;
1387 : :
1388 : : tapdisk_vbd_mark_progress(vbd);
1389 : :
1390 [ # # ]: 0 : if (tapdisk_vbd_queue_ready(vbd))
1391 : 0 : __tapdisk_vbd_reissue_td_request(vbd, image, treq);
1392 : : else
1393 : 0 : td_complete_request(treq, -EBUSY);
1394 : 0 : }
1395 : :
1396 : : int
1397 : 2 : add_extent(tapdisk_extents_t *extents, td_request_t *vreq)
1398 : : {
1399 : : tapdisk_extent_t *extent;
1400 : 2 : extent = (tapdisk_extent_t*)malloc(sizeof(*extent));
1401 [ + - ]: 2 : if(extent == NULL)
1402 : : return -1;
1403 : :
1404 : 2 : extent->start = vreq->sec;
1405 : 2 : extent->length = vreq->secs;
1406 : 2 : extent->flag = vreq->status;
1407 : 2 : extent->next = NULL;
1408 : :
1409 [ - + ][ # # ]: 2 : if(extents->head != NULL && extents->tail != NULL) {
1410 : 0 : extents->tail->next = extent;
1411 : 0 : extents->tail = extent;
1412 : : } else {
1413 : 2 : extents->tail = extent;
1414 : 2 : extents->head = extent;
1415 : : }
1416 : :
1417 : 2 : extents->count += 1;
1418 : 2 : return 0;
1419 : : }
1420 : :
1421 : : int
1422 : 1 : block_status_add_extent(tapdisk_extents_t *extents, td_request_t *vreq)
1423 : : {
1424 : 1 : int ret = 0;
1425 [ + - ]: 1 : if(extents->tail == NULL) {
1426 : 1 : ret = add_extent(extents, vreq);
1427 : : } else {
1428 [ # # ]: 0 : if(extents->tail->flag == vreq->status) {
1429 : 0 : extents->tail->length += vreq->secs;
1430 : : } else {
1431 : 0 : ret = add_extent(extents, vreq);
1432 : : }
1433 : : }
1434 : 1 : return ret;
1435 : : }
1436 : :
1437 : : void
1438 : 1 : tapdisk_vbd_complete_block_status_request(td_request_t treq, int res)
1439 : : {
1440 : : td_vbd_t *vbd;
1441 : : td_image_t *image;
1442 : : td_vbd_request_t *vreq;
1443 : :
1444 : 1 : image = treq.image;
1445 : 1 : vreq = treq.vreq;
1446 : 1 : vbd = vreq->vbd;
1447 : : tapdisk_vbd_mark_progress(vbd);
1448 : :
1449 : : /* Record this extents in the vreqs data */
1450 : 1 : tapdisk_extents_t* extents = (tapdisk_extents_t*)vreq->data;
1451 [ - + ]: 1 : if( block_status_add_extent(extents, &treq) != 0) {
1452 : 0 : ERROR("Could not allocate extent structure");
1453 : : /* Propagate the ENOMEM */
1454 : 0 : res = -ENOMEM;
1455 : : }
1456 : :
1457 : 1 : DBG(TLOG_DBG, "%s: req %s seg %d sec 0x%08"PRIx64
1458 : : " secs 0x%04x buf %p op %d res %d\n", image->name,
1459 : : vreq->name, treq.sidx, treq.sec, treq.secs,
1460 : : treq.buf, vreq->op, res);
1461 : :
1462 : 1 : __tapdisk_vbd_complete_td_request(vbd, vreq, treq, res);
1463 : 1 : }
1464 : :
1465 : : void
1466 : 0 : tapdisk_vbd_complete_td_request(td_request_t treq, int res)
1467 : : {
1468 : 0 : td_vbd_t *vbd;
1469 : : td_image_t *image, *leaf;
1470 : : td_vbd_request_t *vreq;
1471 : :
1472 : 0 : image = treq.image;
1473 : 0 : vreq = treq.vreq;
1474 : 0 : vbd = vreq->vbd;
1475 : :
1476 : : tapdisk_vbd_mark_progress(vbd);
1477 : :
1478 [ # # ][ # # ]: 0 : if (abs(res) == ENOSPC && td_flag_test(image->flags,
1479 : : TD_IGNORE_ENOSPC)) {
1480 : 0 : res = 0;
1481 : 0 : leaf = tapdisk_vbd_first_image(vbd);
1482 [ # # ]: 0 : if (vbd->secondary_mode == TD_VBD_SECONDARY_MIRROR) {
1483 : : DPRINTF("ENOSPC: disabling mirroring\n");
1484 : 0 : list_del_init(&leaf->next);
1485 : 0 : vbd->retired = leaf;
1486 [ # # ]: 0 : } else if (vbd->secondary_mode == TD_VBD_SECONDARY_STANDBY) {
1487 : : DPRINTF("ENOSPC: failing over to secondary image\n");
1488 : 0 : list_add(&vbd->secondary->next, leaf->next.prev);
1489 : 0 : vbd->FIXME_enospc_redirect_count_enabled = 1;
1490 : : }
1491 [ # # ]: 0 : if (vbd->secondary_mode != TD_VBD_SECONDARY_DISABLED) {
1492 : 0 : vbd->secondary = NULL;
1493 : 0 : vbd->secondary_mode = TD_VBD_SECONDARY_DISABLED;
1494 : 0 : signal_enospc(vbd);
1495 : : }
1496 : : }
1497 : :
1498 [ # # ][ # # ]: 0 : if (res != 0 && image->type == DISK_TYPE_NBD &&
[ # # ]
1499 [ # # ]: 0 : ((image == vbd->secondary) ||
1500 : 0 : (image == vbd->retired))) {
1501 : 0 : ERROR("Got non-zero res %d for NBD secondary - disabling "
1502 : : "mirroring: %s", res, vreq->name);
1503 : 0 : vbd->nbd_mirror_failed = 1;
1504 : 0 : res = 0; /* Pretend the writes have completed successfully */
1505 : :
1506 : : /* It was the secondary that timed out - disable secondary */
1507 : 0 : list_del_init(&image->next);
1508 : 0 : vbd->retired = image;
1509 [ # # ]: 0 : if (vbd->secondary_mode != TD_VBD_SECONDARY_DISABLED) {
1510 : 0 : vbd->secondary = NULL;
1511 : 0 : vbd->secondary_mode = TD_VBD_SECONDARY_DISABLED;
1512 : : }
1513 : : }
1514 : :
1515 : 0 : DBG(TLOG_DBG, "%s: req %s seg %d sec 0x%08"PRIx64
1516 : : " secs 0x%04x buf %p op %d res %d\n", image->name,
1517 : : vreq->name, treq.sidx, treq.sec, treq.secs,
1518 : : treq.buf, vreq->op, res);
1519 : :
1520 : 0 : __tapdisk_vbd_complete_td_request(vbd, vreq, treq, res);
1521 : 0 : }
1522 : :
1523 : : static inline void
1524 : 0 : queue_mirror_req(td_vbd_t *vbd, td_request_t clone)
1525 : : {
1526 : 0 : clone.image = vbd->secondary;
1527 : 0 : td_queue_write(vbd->secondary, clone);
1528 : 0 : }
1529 : :
1530 : : int
1531 : 1 : tapdisk_vbd_issue_request(td_vbd_t *vbd, td_vbd_request_t *vreq)
1532 : : {
1533 : : td_image_t *image;
1534 : : td_request_t treq;
1535 : : bzero(&treq, sizeof(treq));
1536 : : td_sector_t sec;
1537 : : int i, err;
1538 : :
1539 : 1 : sec = vreq->sec;
1540 : 1 : image = tapdisk_vbd_first_image(vbd);
1541 : :
1542 : 1 : vreq->submitting = 1;
1543 : :
1544 : : tapdisk_vbd_mark_progress(vbd);
1545 : 1 : vreq->last_try = vbd->ts;
1546 : :
1547 : 1 : tapdisk_vbd_move_request(vreq, &vbd->pending_requests);
1548 : :
1549 : 1 : err = tapdisk_vbd_check_queue(vbd);
1550 [ - + ]: 1 : if (err) {
1551 : 0 : vreq->error = err;
1552 : 0 : goto fail;
1553 : : }
1554 : :
1555 : 1 : err = tapdisk_image_check_request(image, vreq);
1556 [ + - ]: 1 : if (err) {
1557 : 0 : vreq->error = err;
1558 : 0 : goto fail;
1559 : : }
1560 : :
1561 [ + + ]: 2 : for (i = 0; i < vreq->iovcnt; i++) {
1562 : 1 : struct td_iovec *iov = &vreq->iov[i];
1563 : :
1564 : 1 : treq.sidx = i;
1565 : 1 : treq.buf = iov->base;
1566 : 1 : treq.sec = sec;
1567 : 1 : treq.secs = iov->secs;
1568 : 1 : treq.image = image;
1569 : 1 : treq.cb = tapdisk_vbd_complete_td_request;
1570 : 1 : treq.cb_data = NULL;
1571 : 1 : treq.vreq = vreq;
1572 : :
1573 : :
1574 : 1 : vreq->secs_pending += iov->secs;
1575 : 1 : vbd->secs_pending += iov->secs;
1576 [ - + ][ # # ]: 1 : if (vbd->secondary_mode == TD_VBD_SECONDARY_MIRROR &&
1577 [ # # ]: 0 : vreq->op == TD_OP_WRITE &&
1578 : 0 : likely(vreq->skip_mirror == false))
1579 : : {
1580 : 0 : vreq->secs_pending += iov->secs;
1581 : 0 : vbd->secs_pending += iov->secs;
1582 : : }
1583 : :
1584 [ - - + - ]: 1 : switch (vreq->op) {
1585 : : case TD_OP_WRITE:
1586 : 0 : treq.op = TD_OP_WRITE;
1587 : 0 : vbd->vdi_stats.stats->write_reqs_submitted++;
1588 : : /*
1589 : : * it's important to queue the mirror request before
1590 : : * queuing the main one. If the main image runs into
1591 : : * ENOSPC, the mirroring could be disabled before
1592 : : * td_queue_write returns, so if the mirror request was
1593 : : * queued after (which would then not happen), we'd
1594 : : * lose that write and cause the process to hang with
1595 : : * unacknowledged writes.
1596 : : *
1597 : : * Skip when lcache is storing a read-through.
1598 : : */
1599 [ # # ][ # # ]: 0 : if (vbd->secondary_mode == TD_VBD_SECONDARY_MIRROR &&
1600 : 0 : likely(vreq->skip_mirror == false)) {
1601 : 0 : queue_mirror_req(vbd, treq);
1602 : : }
1603 : :
1604 : 0 : td_queue_write(treq.image, treq);
1605 : : break;
1606 : :
1607 : : case TD_OP_READ:
1608 : 0 : treq.op = TD_OP_READ;
1609 : 0 : vbd->vdi_stats.stats->read_reqs_submitted++;
1610 : 0 : td_queue_read(treq.image, treq);
1611 : : break;
1612 : : case TD_OP_BLOCK_STATUS:
1613 : 1 : treq.op = TD_OP_BLOCK_STATUS;
1614 : 1 : treq.cb = tapdisk_vbd_complete_block_status_request;
1615 : 1 : td_queue_block_status(treq.image, &treq);
1616 : : break;
1617 : : }
1618 : :
1619 : 1 : DBG(TLOG_DBG, "%s: req %s seg %d sec 0x%08"PRIx64" secs 0x%04x "
1620 : : "buf %p op %d\n", image->name, vreq->name, i, treq.sec, treq.secs,
1621 : : treq.buf, vreq->op);
1622 : 1 : sec += iov->secs;
1623 : : }
1624 : :
1625 : : err = 0;
1626 : :
1627 : : out:
1628 : 1 : vreq->submitting--;
1629 [ - + ]: 1 : if (!vreq->secs_pending) {
1630 [ # # ]: 0 : err = (err ? : vreq->error);
1631 : 0 : tapdisk_vbd_complete_vbd_request(vbd, vreq);
1632 : : }
1633 : :
1634 : 1 : return err;
1635 : :
1636 : : fail:
1637 : 0 : vreq->error = err;
1638 : 0 : goto out;
1639 : : }
1640 : :
1641 : : static int
1642 : 0 : tapdisk_vbd_request_completed(td_vbd_t *vbd, td_vbd_request_t *vreq)
1643 : : {
1644 : 0 : return vreq->list_head == &vbd->completed_requests;
1645 : : }
1646 : :
1647 : : static int
1648 : 0 : tapdisk_vbd_reissue_failed_requests(td_vbd_t *vbd)
1649 : : {
1650 : : int err;
1651 : : struct timeval now;
1652 : 0 : td_vbd_request_t *vreq, *tmp;
1653 : :
1654 : 0 : err = 0;
1655 : 0 : gettimeofday(&now, NULL);
1656 : :
1657 [ # # ]: 0 : tapdisk_vbd_for_each_request(vreq, tmp, &vbd->failed_requests) {
1658 [ # # ]: 0 : if (vreq->secs_pending)
1659 : 0 : continue;
1660 : :
1661 [ # # ]: 0 : if (td_flag_test(vbd->state, TD_VBD_SHUTDOWN_REQUESTED)) {
1662 : 0 : tapdisk_vbd_complete_vbd_request(vbd, vreq);
1663 : 0 : continue;
1664 : : }
1665 : :
1666 [ # # ][ # # ]: 0 : if (vreq->error != -EBUSY &&
1667 : 0 : now.tv_sec - vreq->last_try.tv_sec < TD_VBD_RETRY_INTERVAL)
1668 : 0 : continue;
1669 : :
1670 : 0 : vbd->retries++;
1671 : 0 : vreq->num_retries++;
1672 : :
1673 : 0 : vreq->prev_error = vreq->error;
1674 : 0 : vreq->error = 0;
1675 : :
1676 : 0 : DBG(TLOG_DBG, "retry #%d of req %s, "
1677 : : "sec 0x%08"PRIx64", iovcnt: %d\n", vreq->num_retries,
1678 : : vreq->name, vreq->sec, vreq->iovcnt);
1679 : :
1680 : 0 : err = tapdisk_vbd_issue_request(vbd, vreq);
1681 : : /*
1682 : : * if this request failed, but was not completed,
1683 : : * we'll back off for a while.
1684 : : */
1685 [ # # ][ # # ]: 0 : if (err && !tapdisk_vbd_request_completed(vbd, vreq))
1686 : : break;
1687 : : }
1688 : :
1689 : 0 : return 0;
1690 : : }
1691 : :
1692 : : static void
1693 : 0 : tapdisk_vbd_count_new_request(td_vbd_t *vbd, td_vbd_request_t *vreq)
1694 : : {
1695 : : struct td_iovec *iov;
1696 : : int write;
1697 : :
1698 : 0 : write = vreq->op == TD_OP_WRITE;
1699 : :
1700 [ # # ]: 0 : for (iov = &vreq->iov[0]; iov < &vreq->iov[vreq->iovcnt]; iov++)
1701 : 0 : td_sector_count_add(&vbd->secs, iov->secs, write);
1702 : 0 : }
1703 : :
1704 : : static int
1705 : 0 : tapdisk_vbd_issue_new_requests(td_vbd_t *vbd)
1706 : : {
1707 : : int err;
1708 : 0 : td_vbd_request_t *vreq, *tmp;
1709 : :
1710 [ # # ]: 0 : tapdisk_vbd_for_each_request(vreq, tmp, &vbd->new_requests) {
1711 : 0 : err = tapdisk_vbd_issue_request(vbd, vreq);
1712 : : /*
1713 : : * if this request failed, but was not completed,
1714 : : * we'll back off for a while.
1715 : : */
1716 [ # # # # ]: 0 : if (err && !tapdisk_vbd_request_completed(vbd, vreq))
1717 : : return err;
1718 : :
1719 : 0 : tapdisk_vbd_count_new_request(vbd, vreq);
1720 : : }
1721 : :
1722 : : return 0;
1723 : : }
1724 : :
1725 : : int
1726 : 0 : tapdisk_vbd_recheck_state(td_vbd_t *vbd)
1727 : : {
1728 : 0 : int err = 0;
1729 : :
1730 [ # # ]: 0 : if (list_empty(&vbd->new_requests))
1731 : : return 0;
1732 : :
1733 [ # # ]: 0 : if (td_flag_test(vbd->state, TD_VBD_QUIESCED) ||
1734 : : td_flag_test(vbd->state, TD_VBD_QUIESCE_REQUESTED))
1735 : : return 0;
1736 : :
1737 : 0 : err = tapdisk_vbd_issue_requests(vbd);
1738 : :
1739 : : /* If we have errors stop checking in this cycle */
1740 : 0 : return err ? 0 : 1;
1741 : : }
1742 : :
1743 : : static int
1744 : 0 : tapdisk_vbd_kill_requests(td_vbd_t *vbd)
1745 : : {
1746 : : td_vbd_request_t *vreq, *tmp;
1747 : :
1748 [ # # ]: 0 : tapdisk_vbd_for_each_request(vreq, tmp, &vbd->new_requests) {
1749 : 0 : vreq->error = -ESHUTDOWN;
1750 : 0 : tapdisk_vbd_move_request(vreq, &vbd->completed_requests);
1751 : : }
1752 : :
1753 [ # # ]: 0 : tapdisk_vbd_for_each_request(vreq, tmp, &vbd->failed_requests) {
1754 : 0 : vreq->error = -ESHUTDOWN;
1755 : 0 : tapdisk_vbd_move_request(vreq, &vbd->completed_requests);
1756 : : }
1757 : :
1758 : 0 : return 0;
1759 : : }
1760 : :
1761 : : int
1762 : 0 : tapdisk_vbd_issue_requests(td_vbd_t *vbd)
1763 : : {
1764 : : int err;
1765 : :
1766 [ # # ]: 0 : if (td_flag_test(vbd->state, TD_VBD_DEAD))
1767 : 0 : return tapdisk_vbd_kill_requests(vbd);
1768 : :
1769 [ # # ]: 0 : if (td_flag_test(vbd->state, TD_VBD_QUIESCED) ||
1770 : : td_flag_test(vbd->state, TD_VBD_QUIESCE_REQUESTED)) {
1771 : :
1772 [ # # ]: 0 : if (td_flag_test(vbd->state, TD_VBD_RESUME_FAILED))
1773 : 0 : return tapdisk_vbd_kill_requests(vbd);
1774 : : else
1775 : : return -EAGAIN;
1776 : : }
1777 : :
1778 : 0 : err = tapdisk_vbd_reissue_failed_requests(vbd);
1779 [ # # ]: 0 : if (err)
1780 : : return err;
1781 : :
1782 : 0 : return tapdisk_vbd_issue_new_requests(vbd);
1783 : : }
1784 : :
1785 : : int
1786 : 0 : tapdisk_vbd_queue_request(td_vbd_t *vbd, td_vbd_request_t *vreq)
1787 : : {
1788 : 0 : gettimeofday(&vreq->ts, NULL);
1789 : 0 : vreq->vbd = vbd;
1790 : :
1791 : 0 : list_add_tail(&vreq->next, &vbd->new_requests);
1792 : 0 : vbd->received++;
1793 : :
1794 : 0 : return 0;
1795 : : }
1796 : :
1797 : : void
1798 : 0 : tapdisk_vbd_kick(td_vbd_t *vbd)
1799 : : {
1800 : 0 : const struct list_head *list = &vbd->completed_requests;
1801 : : td_vbd_request_t *vreq, *prev, *next;
1802 : :
1803 : 0 : vbd->kicked++;
1804 : :
1805 [ # # ]: 0 : while (!list_empty(list)) {
1806 : :
1807 : : /*
1808 : : * Take one request off the completed requests list, and then look for
1809 : : * other requests in the same list that have the same token and
1810 : : * complete them. This way we complete requests against the same token
1811 : : * in one go before we proceed to completing requests with other
1812 : : * tokens. The token is usually used to point back to some other
1813 : : * structure, e.g. a blktap or a tapdisk3 connexion. Once all requests
1814 : : * with a specific token have been completed, proceed to the next one
1815 : : * until the list is empty.
1816 : : */
1817 : 0 : prev = list_entry(list->next, td_vbd_request_t, next);
1818 : 0 : list_del(&prev->next);
1819 : :
1820 [ # # ]: 0 : tapdisk_vbd_for_each_request(vreq, next, list) {
1821 [ # # ]: 0 : if (vreq->token == prev->token) {
1822 : :
1823 : 0 : prev->cb(prev, prev->error, prev->token, 0);
1824 : 0 : vbd->returned++;
1825 : :
1826 : 0 : list_del(&vreq->next);
1827 : 0 : prev = vreq;
1828 : : }
1829 : : }
1830 : :
1831 : 0 : prev->cb(prev, prev->error, prev->token, 1);
1832 : 0 : vbd->returned++;
1833 : : }
1834 : 0 : }
1835 : :
1836 : : int
1837 : 0 : tapdisk_vbd_start_nbdservers(td_vbd_t *vbd)
1838 : : {
1839 : : td_disk_info_t info;
1840 : : int err;
1841 : :
1842 : 0 : err = tapdisk_vbd_get_disk_info(vbd, &info);
1843 : :
1844 [ # # ]: 0 : if (err)
1845 : 0 : return err;
1846 : :
1847 : 0 : vbd->nbdserver = tapdisk_nbdserver_alloc(vbd, info, TAPDISK_NBD_PROTOCOL_OLD);
1848 [ # # ]: 0 : if (!vbd->nbdserver) {
1849 : : EPRINTF("Error starting nbd server");
1850 : : return -1;
1851 : : }
1852 : 0 : err = tapdisk_nbdserver_listen_unix(vbd->nbdserver);
1853 [ # # ]: 0 : if (err) {
1854 : 0 : tapdisk_nbdserver_free(vbd->nbdserver);
1855 : 0 : EPRINTF("failed to listen on the UNIX domain socket: %s\n",
1856 : : strerror(-err));
1857 : : return err;
1858 : : }
1859 : :
1860 : 0 : vbd->nbdserver_new = tapdisk_nbdserver_alloc(vbd, info, TAPDISK_NBD_PROTOCOL_NEW);
1861 [ # # ]: 0 : if (!vbd->nbdserver_new) {
1862 : : EPRINTF("Error starting new-style nbd server");
1863 : : return -1;
1864 : : }
1865 : 0 : err = tapdisk_nbdserver_listen_unix(vbd->nbdserver_new);
1866 [ # # ]: 0 : if (err) {
1867 : 0 : tapdisk_nbdserver_free(vbd->nbdserver_new);
1868 : 0 : EPRINTF("failed to listen on the UNIX domain socket: %s\n",
1869 : : strerror(-err));
1870 : : return err;
1871 : : }
1872 : :
1873 : : return 0;
1874 : : }
1875 : :
1876 : :
1877 : : static int
1878 : 0 : tapdisk_vbd_reqs_outstanding(td_vbd_t *vbd)
1879 : : {
1880 : : int new, pending, failed, completed;
1881 : :
1882 [ # # ]: 0 : ASSERT(vbd);
1883 : :
1884 : 0 : tapdisk_vbd_queue_count(vbd, &new, &pending, &failed, &completed);
1885 : :
1886 : 0 : return new + pending + failed + completed;
1887 : : }
1888 : :
1889 : :
1890 : : void
1891 : 0 : tapdisk_vbd_stats(td_vbd_t *vbd, td_stats_t *st)
1892 : : {
1893 : : td_image_t *image, *next;
1894 : : struct td_xenblkif *blkif;
1895 : 0 : const bool read_caching =
1896 : 0 : TD_OPEN_NO_O_DIRECT == (vbd->flags & TD_OPEN_NO_O_DIRECT);
1897 : :
1898 : 0 : tapdisk_stats_enter(st, '{');
1899 : 0 : tapdisk_stats_field(st, "name", "s", vbd->name);
1900 : :
1901 : 0 : tapdisk_stats_field(st, "secs", "[");
1902 : 0 : tapdisk_stats_val(st, "llu", vbd->secs.rd);
1903 : 0 : tapdisk_stats_val(st, "llu", vbd->secs.wr);
1904 : 0 : tapdisk_stats_leave(st, ']');
1905 : :
1906 : 0 : tapdisk_stats_field(st, "images", "[");
1907 [ # # ]: 0 : tapdisk_vbd_for_each_image(vbd, image, next)
1908 : 0 : tapdisk_image_stats(image, st);
1909 : 0 : tapdisk_stats_leave(st, ']');
1910 : :
1911 : : /*
1912 : : * TODO Is this used by any one?
1913 : : */
1914 [ # # ]: 0 : if (!list_empty(&vbd->rings)) {
1915 : 0 : tapdisk_stats_field(st, "xenbus", "{");
1916 [ # # ]: 0 : list_for_each_entry(blkif, &vbd->rings, entry)
1917 : 0 : tapdisk_xenblkif_stats(blkif, st);
1918 : 0 : tapdisk_stats_leave(st, '}');
1919 : : }
1920 : :
1921 : 0 : tapdisk_stats_field(st,
1922 : : "FIXME_enospc_redirect_count",
1923 : : "llu", vbd->FIXME_enospc_redirect_count);
1924 : :
1925 : 0 : tapdisk_stats_field(st,
1926 : : "nbd_mirror_failed",
1927 : : "d", vbd->nbd_mirror_failed);
1928 : :
1929 : 0 : tapdisk_stats_field(st,
1930 : : "reqs_outstanding",
1931 : : "d", tapdisk_vbd_reqs_outstanding(vbd));
1932 : :
1933 [ # # ]: 0 : tapdisk_stats_field(st,
1934 : : "read_caching",
1935 : : "s", read_caching ? "true": "false");
1936 : :
1937 : 0 : tapdisk_stats_leave(st, '}');
1938 : 0 : }
1939 : :
1940 : :
1941 : : bool
1942 : 0 : tapdisk_vbd_contains_dead_rings(td_vbd_t * vbd)
1943 : : {
1944 : 0 : return !list_empty(&vbd->dead_rings);
1945 : : }
|