Debian Codegraph @

dump/tape.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <[email protected]>, 1994-1997
5 * Stelian Pop <[email protected]>, 1999-2000
6 * Stelian Pop <[email protected]> - AlcĂ´ve <www.alcove.com>, 2000-2002
7 */
8
9 /*-
10 * Copyright (c) 1980, 1991, 1993
11 * The Regents of the University of California. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 #include "config.h" // IWYU pragma: keep
39 #include "tape.h"
40
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include "compat/include/compaterr.h" // IWYU pragma: keep
46 #include "compat/include/system.h"
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50
51 #include <sys/types.h>
52 #include <sys/ioctl.h>
53 #include <sys/mount.h> /* for definition of BLKFLSBUF */
54 #ifndef BLKFLSBUF /* last resort... */
55 #define BLKFLSBUF _IO(0x12, 97) /* Flush buffer cache. */
56 #endif
57 #include <time.h>
58 #include <sys/param.h>
59 #include <sys/socket.h>
60 #include <sys/wait.h>
61 #include <sys/mtio.h>
62 #include <ext2fs/ext2fs.h>
63 #include "compat/include/bsdcompat.h"
64
65 #include "compat/include/protocols/dumprestore.h"
66
67 #include "optr.h"
68 #include "traverse.h"
69 #include "indexer.h"
70 #include "slave.h"
71
72 #include <et/com_err.h>
73 #include <ext2fs/ext2_io.h>
74 #include <stdint.h>
75 #include <sys/syscall.h>
76 #include "common/transformation.h"
77 #include "common/dumprmt.h"
78 #include "dump_globals.h"
79
80 static int writesize; /* size of malloc()ed buffer for tape */
81 static long long lastspclrec = -1; /* tape block number of last written header */
82 static int trecno = 0; /* next record to write in current block */
83 static long blocksperfiles_current; /* current position in blocksperfiles */
84 static long long blocksthisvol; /* number of blocks on current output file */
85 static char *nexttape;
86 static long long tapea_bytes = 0; /* bytes_written at start of current volume */
87
88 static ssize_t dump_atomic_read (int, char *, size_t);
89 static ssize_t dump_atomic_write (int, const char *, size_t);
90 static void doslave (int, int, int);
91 static void enslave (void);
92 static void flushtape (void);
93 static void killall (void);
94 static void rollforward (void);
95
96 static int reqsiz;
97
98 struct slave_results {
99 ssize_t unclen; /* uncompressed length */
100 ssize_t clen; /* compressed length */
101 };
102
103 static struct slave slaves[SLAVES+1];
104
105 static char (*nextblock)[TP_BSIZE];
106
107 static time_t tstart_volume; /* time of volume start */
108 static long long tapea_volume; /* value of spcl.c_tapea at volume start */
109
110 static int master; /* pid of master, for sending error signals */
111 static int tenths; /* length of tape overhead per block written */
112
113 /*
114 * Determine if we can use Linux' clone system call. If so, call it
115 * with the CLONE_IO flag so that all processes will share the same I/O
116 * context, allowing the I/O schedulers to make better scheduling decisions.
117 */
118 /* first, pull in the header files that define sys_clone and CLONE_IO */
119 #include <syscall.h> // IWYU pragma: keep
120 #include <sched.h>
121 #include <unistd.h> // IWYU pragma: keep
122
123 /* If either is not present, fall back on the fork behaviour */
124 #if ! defined(SYS_clone) || ! defined (CLONE_IO)
125 #define fork_clone_io fork
126 #else /* SYS_clone */
127 /* CLONE_IO is available, determine which version of sys_clone to use */
128 #include <linux/version.h>
129 /*
130 * Kernel 2.5.49 introduced two extra parameters to the clone system call.
131 * Neither is useful in our case, so this is easy to handle.
132 */
133 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,49)
134 /*
135 * Parameters of the sys_clone syscall are
136 * clone_flags, child_stack, parent_tidptr, child_tidptr
137 * on all architectures except s390 and s390x
138 * s390* have child_stack, clone_flags, parent_tidptr, child_tidptr
139 */
140 #if defined(__s390__) || defined(__s390x__)
141 #define CLONE_ARGS 0, SIGCHLD|CLONE_IO, NULL, NULL
142 #else
143 #define CLONE_ARGS SIGCHLD|CLONE_IO, 0, NULL, NULL
144 #endif
145 #else
146 #define CLONE_ARGS SIGCHLD|CLONE_IO, 0
147 #endif /* LINUX_VERSION_CODE */
148 pid_t fork_clone_io(void);
149 #endif /* SYS_clone */
150
151 int
152 alloctape(void)
153 {
154 int pgoff = getpagesize() - 1;
155 int i;
156
157 writesize = ntrec * TP_BSIZE;
158 reqsiz = (ntrec + 1) * sizeof(struct req);
159 /*
160 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
161 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
162 * repositioning after stopping, i.e, streaming mode, where the gap is
163 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
164 */
165 if (!blocksperfiles && !unlimited)
166 tenths = (cartridge ? 16 : density == 625 ? 5 : 8);
167 else {
168 tenths = 0;
169 density = 1;
170 }
171 /*
172 * Allocate tape buffer contiguous with the array of instruction
173 * packets, so flushtape() can write them together with one write().
174 * Align tape buffer on page boundary to speed up tape write().
175 */
176 for (i = 0; i <= SLAVES; i++) {
177 slaves[i].rawdata =
178 malloc((reqsiz + writesize + pgoff + TP_BSIZE));
179 if (slaves[i].rawdata == NULL)
180 return(0);
181 memset(slaves[i].rawdata, 0, reqsiz + writesize + pgoff + TP_BSIZE);
182 slaves[i].tblock = (char (*)[TP_BSIZE])
183 (((intptr_t)slaves[i].rawdata + reqsiz + pgoff) & ~pgoff);
184 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
185 memset(slaves[i].req, 0, sizeof *slaves[i].req);
186 }
187 slp = &slaves[0];
188 slp->count = 1;
189 slp->tapea = 0;
190 slp->firstrec = 0;
191 nextblock = slp->tblock;
192 return(1);
193 }
194
195 void
196 dealloctape(void)
197 {
198 int i;
199 for (i = 0; i <= SLAVES; i++) {
200 free(slaves[i].rawdata);
201 }
202 }
203
204 void
205 writerec(const void *dp, int isspcl)
206 {
207 slp->req[trecno].dblk = (ext2_loff_t)0;
208 slp->req[trecno].count = 1;
209 slp->req[trecno].uninit = 0;
210 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
211 *(union u_spcl *)(*(nextblock)) = *(const union u_spcl *)dp;
212
213 /* Need to write it to the archive file */
214 indexer->writerec(dp, isspcl);
215
216 nextblock++;
217 if (isspcl)
218 lastspclrec = u_spcl_c_tapea();
219 trecno++;
220 u_spcl_c_tapea_add(1);
221 if (trecno >= ntrec)
222 flushtape();
223 }
224
225 void
226 dumpblock(blk64_t blkno, int size, int uninit)
227 {
228 int avail, tpblks;
229 ext2_loff_t dblkno;
230
231 dblkno = fsbtodb(sblock, blkno);
232 tpblks = size >> tp_bshift;
233 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
234 slp->req[trecno].dblk = dblkno;
235 slp->req[trecno].count = avail;
236 slp->req[trecno].uninit = uninit;
237 trecno += avail;
238 u_spcl_c_tapea_add(avail);
239 if (trecno >= ntrec)
240 flushtape();
241 dblkno += avail << (tp_bshift - dev_bshift);
242 tpblks -= avail;
243 }
244 }
245
246 static int nogripe = 0;
247
248 static void
249 tperror(int errnum)
250 {
251
252 if (pipeout) {
253 msg("write error on %s: %s\n", tape, strerror(errnum));
254 quit("Cannot recover\n");
255 /* NOTREACHED */
256 }
257 msg("write error %lld blocks into volume %d: %s\n",
258 blocksthisvol, tapeno, strerror(errnum));
259 broadcast("DUMP WRITE ERROR!\n");
260 if (query("Do you want to rewrite this volume?")) {
261 msg("Closing this volume. Prepare to restart with new media;\n");
262 msg("this dump volume will be rewritten.\n");
263 killall();
264 nogripe = 1;
265 close_rewind();
266 Exit(X_REWRITE);
267 }
268 if (query("Do you want to start the next tape?"))
269 return;
270 dumpabort(0);
271 }
272
273 static void
274 sigpipe(UNUSED(int signo))
275 {
276 quit("Broken pipe\n");
277 }
278
279 /*
280 * do_stats --
281 * Update xferrate stats
282 */
283 time_t
284 do_stats(void)
285 {
286 time_t tnow, ttaken;
287 long blocks;
288
289 tnow = time(NULL);
290 ttaken = tnow - tstart_volume;
291 blocks = u_spcl_c_tapea() - tapea_volume;
292 msg("Volume %d completed at: %s", tapeno, ctime(&tnow));
293 if (! compressed)
294 msg("Volume %d %ld blocks (%.2fMB)\n", tapeno,
295 blocks, ((double)blocks * TP_BSIZE / 1048576));
296 if (ttaken > 0) {
297 long volkb = (bytes_written - tapea_bytes) / 1024;
298 long txfrate = volkb / ttaken;
299 msg("Volume %d took %jd:%02jd:%02jd\n", tapeno,
300 (intmax_t)(ttaken / 3600), (intmax_t)((ttaken % 3600) / 60), (intmax_t)(ttaken % 60));
301 msg("Volume %d transfer rate: %ld kB/s\n", tapeno,
302 txfrate);
303 xferrate += txfrate;
304 if (compressed) {
305 double rate = .0005 + (double) blocks / (double) volkb;
306 msg("Volume %d %ldkB uncompressed, %ldkB compressed,"
307 " %1.3f:1\n",
308 tapeno, blocks, volkb, rate);
309 }
310 }
311 return(tnow);
312 }
313
314 char *
315 mktimeest(time_t tnow)
316 {
317 static char msgbuf[128];
318 time_t deltat;
319
320 msgbuf[0] = '\0';
321
322 if (blockswritten < 500)
323 return NULL;
324 if (blockswritten > tapesize)
325 tapesize = blockswritten;
326 deltat = tstart_writing - tnow + (1.0 * (tnow - tstart_writing))
327 / blockswritten * tapesize;
328 if (tnow > tstart_volume)
329 (void)snprintf(msgbuf, sizeof(msgbuf),
330 "%3.2f%% done at %lld kB/s, finished in %d:%02d\n",
331 (blockswritten * 100.0) / tapesize,
332 (u_spcl_c_tapea() - tapea_volume) / (tnow - tstart_volume),
333 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
334 else
335 (void)snprintf(msgbuf, sizeof(msgbuf),
336 "%3.2f%% done, finished in %d:%02d\n",
337 (blockswritten * 100.0) / tapesize,
338 (int)(deltat / 3600), (int)((deltat % 3600) / 60));
339
340 return msgbuf;
341 }
342
343 #if defined(SIGINFO)
344 /*
345 * statussig --
346 * information message upon receipt of SIGINFO
347 */
348 void
349 statussig(int notused)
350 {
351 int save_errno = errno;
352 char *buf;
353
354 buf = mktimeest(time(NULL));
355 if (buf)
356 write(STDERR_FILENO, buf, strlen(buf));
357 errno = save_errno;
358 }
359 #endif
360
361 static void
362 flushtape(void)
363 {
364 int i, blks, got;
365 long lastfirstrec;
366 struct slave_results returned;
367
368 int siz = (char *)nextblock - (char *)slp->req;
369
370 /* make sure returned has sane values in case we don't read
371 * them from the slave in this pass */
372 returned.unclen = returned.clen = writesize;
373
374 slp->req[trecno].count = 0; /* Sentinel */
375
376 if (dump_atomic_write( slp->fd, (char *)slp->req, siz) != siz)
377 quit("error writing command pipe: %s\n", strerror(errno));
378
379 slp->sent = 1; /* we sent a request, read the response later */
380
381 lastfirstrec = slp->firstrec;
382
383 if (++slp >= &slaves[SLAVES])
384 slp = &slaves[0];
385
386 /* Read results back from next slave */
387 if (slp->sent) {
388 if (dump_atomic_read( slp->fd, (char *)&returned, sizeof returned)
389 != sizeof returned) {
390 perror(" DUMP: error reading command pipe in master");
391 dumpabort(0);
392 }
393 got = returned.unclen;
394 bytes_written += returned.clen;
395 if (returned.unclen == returned.clen)
396 uncomprblks++;
397 slp->sent = 0;
398
399 /* Check for errors or end of tape */
400 if (got <= 0) {
401 /* Check for errors */
402 if (got < 0)
403 tperror(-got);
404 else
405 msg("End of tape detected\n");
406
407 /*
408 * Drain the results, don't care what the values were.
409 * If we read them here then trewind won't...
410 */
411 for (i = 0; i < SLAVES; i++) {
412 if (slaves[i].sent) {
413 if (dump_atomic_read( slaves[i].fd,
414 (char *)&returned, sizeof returned)
415 != sizeof returned) {
416 perror(" DUMP: error reading command pipe in master");
417 dumpabort(0);
418 }
419 slaves[i].sent = 0;
420 }
421 }
422
423 close_rewind();
424 rollforward();
425 return;
426 }
427 }
428
429 blks = 0;
430 if (spcl.c_type == TS_CLRI || spcl.c_type == TS_BITS)
431 blks = spcl.c_count;
432 else {
433 if (spcl.c_type != TS_END) {
434 for (i = 0; i < spcl.c_count; i++)
435 if (spcl.c_addr[i] != 0)
436 blks++;
437 }
438 }
439 slp->count = lastspclrec + blks + 1 - u_spcl_c_tapea();
440 slp->tapea = u_spcl_c_tapea();
441 slp->firstrec = lastfirstrec + ntrec;
442 slp->inode = curino;
443 nextblock = slp->tblock;
444 trecno = 0;
445 asize += tenths + returned.clen / density;
446 blockswritten += ntrec;
447 blocksthisvol += ntrec;
448 if (!pipeout && !unlimited) {
449 if (blocksperfiles && blocksperfiles[blocksperfiles_current]) {
450 if ( compressed ? (bytes_written - tapea_bytes + SLAVES * (writesize + sizeof(struct tapebuf)))*1ULL >= ((blocksperfiles[blocksperfiles_current]) * 1024ULL)
451 : blocksthisvol >= blocksperfiles[blocksperfiles_current] ) {
452 close_rewind();
453 startnewtape(0);
454 }
455 }
456 else if (asize > tsize) {
457 close_rewind();
458 startnewtape(0);
459 }
460 }
461 timeest();
462 }
463
464 time_t
465 trewind(void)
466 {
467 int f;
468 int got;
469 struct slave_results returned;
470
471 for (f = 0; f < SLAVES; f++) {
472 /*
473 * Drain the results, but unlike EOT we DO (or should) care
474 * what the return values were, since if we detect EOT after
475 * we think we've written the last blocks to the tape anyway,
476 * we have to replay those blocks with rollforward.
477 *
478 * fixme: punt for now.
479 */
480 if (slaves[f].sent) {
481 if (dump_atomic_read( slaves[f].fd, (char *)&returned, sizeof returned)
482 != sizeof returned) {
483 perror(" DUMP: error reading command pipe in master");
484 dumpabort(0);
485 }
486 got = returned.unclen;
487 bytes_written += returned.clen;
488 if (returned.unclen == returned.clen)
489 uncomprblks++;
490 slaves[f].sent = 0;
491
492 if (got < 0)
493 tperror(-got);
494
495 if (got == 0) {
496 msg("EOT detected in last 2 tape records!\n");
497 msg("Use a longer tape, decrease the size estimate\n");
498 quit("or use no size estimate at all.\n");
499 }
500 }
501 (void) close(slaves[f].fd);
502 }
503 while (wait((int *)NULL) >= 0) /* wait for any signals from slaves */
504 /* void */;
505
506 if (!pipeout) {
507
508 msg("Closing %s\n", tape);
509
510 if (host) {
511 rmtclose();
512 while (rmtopen(tape, O_RDONLY) < 0)
513 sleep(10);
514 rmtclose();
515 } else {
516 (void) close(tapefd);
517 if (!fifoout) {
518 while ((f = open(tape, O_RDONLY)) < 0)
519 sleep (10);
520 (void) close(f);
521 }
522 }
523 }
524 return do_stats();
525 }
526
527
528 void
529 close_rewind(void)
530 {
531 int eot_code = 1;
532 (void)trewind();
533 if (eot_script) {
534 msg("Launching %s\n", eot_script);
535 eot_code = system_command(eot_script, tape, tapeno);
536 }
537 if (eot_code != 0 && eot_code != 1) {
538 msg("Dump aborted by the end of tape script\n");
539 dumpabort(0);
540 }
541 if (eot_code == 0)
542 return;
543 if (nexttape || Mflag)
544 return;
545 if (!nogripe) {
546 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
547 broadcast("CHANGE DUMP VOLUMES!\7\7\n");
548 }
549 while (!query("Is the new volume mounted and ready to go?"))
550 if (query("Do you want to abort?")) {
551 dumpabort(0);
552 /*NOTREACHED*/
553 }
554 }
555
556 void
557 rollforward(void)
558 {
559 struct req *p, *q = NULL, *prev;
560 struct slave *tslp;
561 int i, size, got;
562 long savedtapea;
563 union u_spcl *ntb, *otb;
564 struct slave_results returned;
565 long blks;
566 long lastfirstrec;
567 tslp = &slaves[SLAVES];
568 ntb = (union u_spcl *)tslp->tblock[1];
569
570 /* make sure returned has sane values in case we don't read
571 * them from the slave in this pass */
572 returned.unclen = returned.clen = writesize;
573
574 /*
575 * Each of the N slaves should have requests that need to
576 * be replayed on the next tape. Use the extra slave buffers
577 * (slaves[SLAVES]) to construct request lists to be sent to
578 * each slave in turn.
579 */
580 for (i = 0; i < SLAVES; i++) {
581 q = &tslp->req[1];
582 otb = (union u_spcl *)slp->tblock;
583
584 /*
585 * For each request in the current slave, copy it to tslp.
586 */
587
588 prev = NULL;
589 for (p = slp->req; p->count > 0; p += p->count) {
590 *q = *p;
591 if (p->dblk == 0)
592 *ntb++ = *otb++; /* copy the datablock also */
593 prev = q;
594 q += q->count;
595 }
596 if (prev == NULL)
597 quit("rollforward: protocol botch");
598 if (prev->dblk != 0)
599 prev->count -= 1;
600 else
601 ntb--;
602 q -= 1;
603 q->count = 0;
604 q = &tslp->req[0];
605 if (i == 0) {
606 q->dblk = 0;
607 q->count = 1;
608 trecno = 0;
609 nextblock = tslp->tblock;
610 savedtapea = u_spcl_c_tapea();
611 u_spcl_c_tapea_set(slp->tapea);
612 startnewtape(0);
613 u_spcl_c_tapea_set(savedtapea);
614 lastspclrec = savedtapea - 1;
615 }
616 size = (char *)ntb - (char *)q;
617 if (dump_atomic_write( slp->fd, (char *)q, size) != size) {
618 perror(" DUMP: error writing command pipe");
619 dumpabort(0);
620 }
621 slp->sent = 1;
622 lastfirstrec = slp->firstrec;
623 if (++slp >= &slaves[SLAVES])
624 slp = &slaves[0];
625
626 q->count = 1;
627
628 if (prev->dblk != 0) {
629 /*
630 * If the last one was a disk block, make the
631 * first of this one be the last bit of that disk
632 * block...
633 */
634 q->dblk = prev->dblk +
635 prev->count * (TP_BSIZE / DEV_BSIZE);
636 ntb = (union u_spcl *)tslp->tblock;
637 } else {
638 /*
639 * It wasn't a disk block. Copy the data to its
640 * new location in the buffer.
641 */
642 q->dblk = 0;
643 *((union u_spcl *)tslp->tblock) = *ntb;
644 ntb = (union u_spcl *)tslp->tblock[1];
645 }
646 }
647 slp->req[0] = *q;
648 nextblock = slp->tblock;
649 if (q->dblk == 0) {
650 /* XXX post increment triggers an egcs-1.1.2-12 bug on alpha/sparc */
651 *(union u_spcl *)(*nextblock) = *(union u_spcl *)tslp->tblock;
652 nextblock++;
653 }
654 trecno = 1;
655
656 /*
657 * Clear the first slaves' response. One hopes that it
658 * worked ok, otherwise the tape is much too short!
659 */
660 if (slp->sent) {
661 if (dump_atomic_read( slp->fd, (char *)&returned, sizeof returned)
662 != sizeof returned) {
663 perror(" DUMP: error reading command pipe in master");
664 dumpabort(0);
665 }
666 got = returned.unclen;
667 bytes_written += returned.clen;
668 if (returned.clen == returned.unclen)
669 uncomprblks++;
670 slp->sent = 0;
671
672 if (got < 0)
673 tperror(-got);
674
675 if (got == 0) {
676 quit("EOT detected at start of the tape!\n");
677 }
678 }
679
680 blks = 0;
681 if (spcl.c_type != TS_END) {
682 for (i = 0; i < spcl.c_count; i++)
683 if (spcl.c_addr[i] != 0)
684 blks++;
685 }
686
687 slp->firstrec = lastfirstrec + ntrec;
688 slp->count = lastspclrec + blks + 1 - u_spcl_c_tapea();
689 slp->inode = curino;
690 asize += tenths + returned.clen / density;
691 blockswritten += ntrec;
692 blocksthisvol += ntrec;
693 }
694
695 #if defined(SYS_clone) && defined(CLONE_IO)
696 pid_t
697 fork_clone_io(void)
698 {
•699 pid_t res,parent;
700 parent=getppid(); /* az hackety hack... */
701
702 res=syscall(SYS_clone, CLONE_ARGS);
703 getppid();
704 /* as per clone call manpage: caching! */
705 getpid();
706 #ifdef __alpha__
707 syscall(SYS_getxpid);
708 #else
709 syscall(SYS_getpid);
710 #endif
711
712 /* az: clone manpage doesn't say jack about what the
713 child receives, but it's NOT ZERO on sparc. however, it seems the
714 ppid is updated and trustworthy - so let's use that... */
715 return parent==getppid()?res:0;
716
717 }
718 #endif
719
720 /*
721 * We implement taking and restoring checkpoints on the tape level.
722 * When each tape is opened, a new process is created by forking; this
723 * saves all of the necessary context in the parent. The child
724 * continues the dump; the parent waits around, saving the context.
725 * If the child returns X_REWRITE, then it had problems writing that tape;
726 * this causes the parent to fork again, duplicating the context, and
727 * everything continues as if nothing had happened.
728 */
729 void
730 startnewtape(int top)
731 {
732 int parentpid;
733 int childpid;
734 int status;
735 int waitpid;
736 char *p;
737
738 sigset_t sigs;
739 sigemptyset(&sigs);
740 sigaddset(&sigs, SIGINT);
741 sigprocmask(SIG_BLOCK, &sigs, NULL);
742
743 parentpid = getpid();
744 tapea_volume = u_spcl_c_tapea();
745 tapea_bytes = bytes_written;
746 tstart_volume = time(NULL);
747
748 restore_check_point:
749 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
750 /*
751 * All signals are inherited...
752 */
753 childpid = fork_clone_io();
754 if (childpid < 0) {
755 msg("Context save fork fails in parent %d\n", parentpid);
756 Exit(X_ABORT);
757 }
758 if (childpid != 0) {
759 /*
760 * PARENT:
761 * save the context by waiting
762 * until the child doing all of the work returns.
763 * don't catch the interrupt
764 */
765 sigprocmask(SIG_BLOCK, &sigs, NULL);
766 Dmsg(TDEBUG, "Tape: %d; parent process: %d child process %d\n",
767 tapeno+1, parentpid, childpid);
768 while ((waitpid = wait(&status)) != childpid)
769 if (waitpid != rshpid)
770 msg("Parent %d waiting for child %d has another child %d return\n",
771 parentpid, childpid, waitpid);
772 if (status & 0xFF) {
773 msg("Child %d returns LOB status %o\n",
774 childpid, status&0xFF);
775 }
776 status = (status >> 8) & 0xFF;
777 switch(status) {
778 case X_FINOK:
779 Dmsg(TDEBUG, "Child %d finishes X_FINOK\n", childpid);
780 break;
781 case X_ABORT:
782 Dmsg(TDEBUG, "Child %d finishes X_ABORT\n", childpid);
783 break;
784 case X_REWRITE:
785 Dmsg(TDEBUG, "Child %d finishes X_REWRITE\n", childpid);
786 break;
787 default:
788 msg("Child %d finishes unknown %d\n",
789 childpid, status);
790 break;
791 }
792 switch(status) {
793 case X_FINOK:
794 Exit(X_FINOK);
795 case X_ABORT:
796 Exit(X_ABORT);
797 case X_REWRITE:
798 goto restore_check_point;
799 default:
800 msg("Bad return code from dump: %d\n", status);
801 Exit(X_ABORT);
802 }
803 /*NOTREACHED*/
804 } else { /* we are the child; just continue */
805 Dmsg(TDEBUG, "Child on Tape %d has parent %d, my pid = %d\n",
806 tapeno+1, parentpid, getpid());
807 /*
808 * If we have a name like "/dev/rmt0,/dev/rmt1",
809 * use the name before the comma first, and save
810 * the remaining names for subsequent volumes.
811 */
812 tapeno++; /* current tape sequence */
813 if (Mflag) {
814 snprintf(tape, MAXPATHLEN, "%s%03d", tapeprefix, tapeno);
815 tape[MAXPATHLEN - 1] = '\0';
816 msg("Dumping volume %d on %s\n", tapeno, tape);
817 }
818 else if (nexttape || strchr(tapeprefix, ',')) {
819 if (nexttape && *nexttape)
820 tapeprefix = nexttape;
821 if ((p = strchr(tapeprefix, ',')) != NULL) {
822 *p = '\0';
823 nexttape = p + 1;
824 } else
825 nexttape = NULL;
826 strncpy(tape, tapeprefix, MAXPATHLEN);
827 tape[MAXPATHLEN - 1] = '\0';
828 msg("Dumping volume %d on %s\n", tapeno, tape);
829 }
830 if (blocksperfiles && blocksperfiles_current < *blocksperfiles)
831 blocksperfiles_current++;
832 while ((tapefd = (host ? rmtopen(tape, O_WRONLY|O_CREAT|O_TRUNC) : pipeout ?
833 fileno(stdout) :
834 open(tape, O_WRONLY|O_CREAT|O_TRUNC, 0666))) < 0)
835 {
836 msg("Cannot open output \"%s\": %s\n", tape,
837 strerror(errno));
838 if (!query("Do you want to retry the open?"))
839 dumpabort(0);
840 }
841 if (!host) {
842 struct mtget mt_stat;
843 magtapeout = ioctl(tapefd, MTIOCGET, (char *)&mt_stat) == 0;
844 /*
845 msg("Output is to %s\n",
846 magtapeout ? "tape" : "file/pipe");
847 */
848 }
849
850 enslave(); /* Share open tape file descriptor with slaves */
851
852 asize = 0;
853 blocksthisvol = 0;
854 if (top)
855 newtape++; /* new tape signal */
856 spcl.c_count = slp->count;
857 /*
858 * measure firstrec in TP_BSIZE units since restore doesn't
859 * know the correct ntrec value...
860 */
861 u_spcl_c_firstrec_set(slp->firstrec);
862 spcl.c_volume++;
863 spcl.c_type = TS_TAPE;
864 spcl.c_flags |= DR_NEWHEADER;
865 spcl.c_ntrec = ntrec;
866 if (compressed)
867 spcl.c_flags |= DR_COMPRESSED;
868 writeheader((dump_ino_t)slp->inode);
869 spcl.c_flags &=~ DR_NEWHEADER;
870 msg("Volume %d started with block %lld at: %s", tapeno,
871 u_spcl_c_tapea(), ctime(&tstart_volume));
872 if (tapeno > 1)
873 msg("Volume %d begins with blocks from inode %d\n",
874 tapeno, slp->inode);
875 if (tapeno < (int)TP_NINOS)
876 volinfo[tapeno] = slp->inode;
877 transformation->startNewTape(transformation, NULL, 0);
878 }
879 }
880
881 void
882 dumpabort(UNUSED(int signo))
883 {
884
885 if (master != 0 && master != getpid())
886 /* Signals master to call dumpabort */
887 (void) kill(master, SIGTERM);
888 else {
889 killall();
890 msg("The ENTIRE dump is aborted.\n");
891 }
892 rmtclose();
893 Exit(X_ABORT);
894 }
895
896 void
897 Exit(int status)
898 {
899
900 Dmsg(TDEBUG, "pid = %d exits with status %d\n", getpid(), status);
901 exit(status);
902 }
903
904 /*
905 * proceed - no-op handler for SIGUSR1 and SIGUSR2, used to synchronize IO between the slaves.
906 */
907 static void
908 proceed(UNUSED(int signo))
909 {
910 }
911
912 void
913 enslave(void)
914 {
915 int cmd[2];
916 int i, j;
917
918 master = getpid();
919
920 { struct sigaction sa;
921 memset(&sa, 0, sizeof sa);
922 sigemptyset(&sa.sa_mask);
923 sa.sa_handler = dumpabort;
924 sigaction(SIGTERM, &sa, NULL); /* Slave sends SIGTERM on dumpabort() */
925 sa.sa_handler = sigpipe;
926 sigaction(SIGPIPE, &sa, NULL);
927 sa.sa_handler = proceed;
928 sa.sa_flags = SA_RESTART;
929 sigaction(SIGUSR1, &sa, NULL); /* Slave sends SIGUSR1 to next slave */
930 sa.sa_handler = proceed;
931 sa.sa_flags = SA_RESTART;
932 sigaction(SIGUSR2, &sa, NULL); /* Slave sends SIGUSR2 to next slave */
933 }
934
935 for (i = 0; i < SLAVES; i++) {
936
937 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
938 (slaves[i].pid = fork_clone_io()) < 0)
939 quit("too many slaves, %d (recompile smaller): %s\n",
940 i, strerror(errno));
941
942 slaves[i].fd = cmd[1];
943 slaves[i].sent = 0;
944 if (slaves[i].pid == 0) { /* Slave starts up here */
945 sigset_t sigs;
946 for (j = 0; j <= i; j++)
947 (void) close(slaves[j].fd);
948 sigemptyset(&sigs);
949 sigaddset(&sigs, SIGINT); /* Master handles this */
950 sigaddset(&sigs, SIGUSR1); /* Block these until we're ready for them */
951 sigaddset(&sigs, SIGUSR2);
952 #if defined(SIGINFO)
953 sigaddset(&sigs, SIGINFO);
954 #endif
955 sigprocmask(SIG_BLOCK, &sigs, NULL);
956
957 /* Synchronize, tell master that we're ready for signals */
958 if (dump_atomic_write( cmd[0], (char *) &i, sizeof i)
959 != sizeof i)
960 quit("master/slave protocol botched 3\n");
961
962 doslave(cmd[0],
963 i,
964 (slaves[i].pid == slp->pid));
965 Exit(X_FINOK);
966 }
967 else
968 close(cmd[0]);
969 }
970
971 for (j = 0; j < SLAVES; j++) {
972 /* Synchronize - slave is ready for signals */
973 if (dump_atomic_read( slaves[j].fd, (char *) &i, sizeof i) != sizeof i)
974 quit("master/slave protocol botched 4\n");
975 if (dump_atomic_write( slaves[j].fd,
976 (char *) &slaves[(j + 1) % SLAVES].pid,
977 sizeof slaves[0].pid) != sizeof slaves[0].pid)
978 quit("master/slave protocol botched 5\n");
979 if (j == slp - &slaves[0]) {
980 /* signal current slave to start reading and writing (resume after tape change) */
981 (void) kill(slaves[j].pid, SIGUSR1);
982 (void) kill(slaves[j].pid, SIGUSR2);
983 }
984 }
985
986 master = 0;
987 }
988
989 void
990 killall(void)
991 {
992 int i;
993
994 for (i = 0; i < SLAVES; i++)
995 if (slaves[i].pid > 0) {
996 (void) kill(slaves[i].pid, SIGKILL);
997 slaves[i].sent = 0;
998 }
999 }
1000
1001 /*
1002 * Synchronization - each process waits for a SIGUSR2 from the
1003 * previous process before writing to the tape, and sends SIGUSR2
1004 * to the next process when the tape write completes. On tape errors
1005 * a SIGUSR1 is sent to the master which then terminates all of the
1006 * slaves. Each process sends SIGUSR1 to the next to signal that it
1007 * is time to start reading from the disk, after it finishes reading
1008 * and moves to the compression phase.
1009 */
1010 static void
1011 doslave(int cmd,
1012 int slave_number,
1013 #if defined(HAVE_BLOCK_TRANSFORMATION)
1014 int first
1015 #else
1016 UNUSED(int first)
1017 #endif
1018 )
1019 {
1020 int nread;
1021 int nextslave;
1022 volatile int wrote = 0, size, eot_count, bufsize;
1023 char * volatile buffer;
1024 #if defined(HAVE_BLOCK_TRANSFORMATION)
1025 struct tapebuf * volatile comp_buf = NULL;
1026 int compresult;
1027 volatile int do_compress = !first;
1028 unsigned long worklen;
1029 #endif /* HAVE_BLOCK_TRANSFORMATION */
1030
1031 struct slave_results returns;
1032 errcode_t retval;
1033 #ifdef USE_QFA
1034 QFA_State qfa_state;
1035 #endif /* USE_QFA */
1036 sigset_t set;
1037
1038 #ifdef HAVE_BLOCK_TRANSFORMATION
1039 transformation->startDiskIOProcess(transformation);
1040 #endif /* HAVE_BLOCK_TRANSFORMATION */
1041
1042 #ifdef USE_QFA
1043 indexer->openQfaState(&qfa_state);
1044 #endif
1045
1046 /*
1047 * Need our own seek pointer.
1048 */
1049 (void) close(diskfd);
1050 if ((diskfd = open(disk, O_RDONLY)) < 0)
1051 quit("slave couldn't reopen disk: %s\n", strerror(errno));
1052 #ifdef BLKFLSBUF
1053 (void)ioctl(diskfd, BLKFLSBUF, 0);
1054 #endif
1055 ext2fs_close(fs);
1056 retval = dump_fs_open(disk, &fs);
1057 if (retval)
1058 quit("slave couldn't reopen disk: %s\n", error_message(retval));
1059
1060 /*
1061 * Need the pid of the next slave in the loop...
1062 */
1063 if ((nread = dump_atomic_read( cmd, (char *)&nextslave, sizeof nextslave))
1064 != sizeof nextslave) {
1065 quit("master/slave protocol botched - didn't get pid of next slave.\n");
1066 }
1067
1068 #if defined(HAVE_BLOCK_TRANSFORMATION)
1069 /* if we're doing a compressed dump, allocate the compress buffer */
1070 if (compressed) {
1071 int bsiz = sizeof(struct tapebuf) + writesize;
1072 /* Add extra space to deal with compression or encryption enlarging the buffer */
1073 if (TP_BSIZE > writesize/16 + 200)
1074 bsiz += TP_BSIZE;
1075 else
1076 bsiz += writesize/16 + 200;
1077 comp_buf = malloc(bsiz);
1078 if (comp_buf == NULL)
1079 quit("couldn't allocate a compress buffer.\n");
1080 transformation->initialize(transformation, 1);
1081 switch(zipflag) {
1082 case COMPRESS_ZLIB:
1083 case COMPRESS_BZLIB:
1084 case COMPRESS_LZO:
1085 tapebuf_setflags(comp_buf, zipflag);
1086 break;
1087 default:
1088 quit("internal error - unknown compression method: %d\n", zipflag);
1089 }
1090 }
1091 #endif /* HAVE_BLOCK_TRANSFORMATION */
1092
1093 /*
1094 * Get list of blocks to dump, read the blocks into tape buffer
1095 */
1096 while ((nread = dump_atomic_read( cmd, (char *)slp->req, reqsiz)) == reqsiz) {
1097 /* wait for previous slave to finish reading */
1098 sigfillset(&set);
1099 sigdelset(&set, SIGUSR1);
1100 sigsuspend(&set);
1101
1102 struct req *p = slp->req;
1103
1104 for (trecno = 0; trecno < ntrec;
1105 trecno += p->count, p += p->count) {
1106
1107 if (p->dblk) { /* read a disk block */
1108 if(p->uninit) {
1109 memset(slp->tblock[trecno], 0, p->count * TP_BSIZE);
1110 } else {
1111 bread(p->dblk, slp->tblock[trecno],
1112 p->count * TP_BSIZE);
1113 }
1114 } else { /* read record from pipe */
1115 if (p->count != 1 || dump_atomic_read( cmd,
1116 (char *)slp->tblock[trecno],
1117 TP_BSIZE) != TP_BSIZE)
1118 quit("master/slave protocol botched.\n");
1119 }
1120 }
1121 /* signal next slave to start reading */
1122 (void) kill(nextslave, SIGUSR1);
1123
1124 /* Try to write the data... */
1125 wrote = 0;
1126 eot_count = 0;
1127 size = 0;
1128 buffer = (char *) slp->tblock[0]; /* set write pointer */
1129 bufsize = writesize; /* length to write */
1130 returns.clen = returns.unclen = bufsize;
1131
1132 #if defined(HAVE_BLOCK_TRANSFORMATION)
1133 /*
1134 * When writing a compressed dump, each block except
1135 * the first one on each tape is written
1136 * from struct tapebuf with an 4 byte prefix
1137 * followed by the data. This can be less than
1138 * writesize. Restore, on a short read, can compare the
1139 * length read to the compressed length in the header
1140 * to verify that the read was good. Blocks which don't
1141 * compress well are written uncompressed.
1142 * The first block written by each slave is not compressed
1143 * and does not have a prefix.
1144 */
1145 if (compressed && do_compress) {
1146 tapebuf_setlength(comp_buf, bufsize);
1147 worklen = TP_BSIZE + writesize;
1148 compresult = 1;
1149
1150 // tapebuf: compressed, flags, length
1151 compresult = transformation->compress(transformation, comp_buf,
1152 &worklen, slp->tblock[0], writesize);
1153 if (compresult && (ssize_t)worklen <= writesize - 16) {
1154 /* write the compressed buffer */
1155 tapebuf_setlength(comp_buf, worklen);
1156 tapebuf_setcompressed(comp_buf, 1);
1157 buffer = (char *) comp_buf;
1158 returns.clen = bufsize = worklen + sizeof(struct tapebuf);
1159 }
1160 else {
1161 /* write the data uncompressed */
1162 tapebuf_setlength(comp_buf, writesize);
1163 tapebuf_setcompressed(comp_buf, 0);
1164 buffer = (char *) comp_buf;
1165 returns.clen = bufsize = writesize + sizeof(struct tapebuf);
1166 returns.unclen = returns.clen;
1167 memcpy(comp_buf->buf, (char *)slp->tblock[0], writesize);
1168 }
1169 }
1170 /* compress the remaining blocks if we're compressing */
1171 do_compress = compressed;
1172 #endif /* HAVE_BLOCK_TRANSFORMATION */
1173
1174 sigfillset(&set);
1175 sigdelset(&set, SIGUSR2);
1176 sigsuspend(&set);
1177
1178 #ifdef USE_QFA
1179 indexer->updateQfa(&qfa_state);
1180 #endif
1181
1182 int bytes_to_write = bufsize - size;
1183 while (eot_count < 10 && size < bufsize) {
1184 if (host)
1185 wrote = rmtwrite(buffer + size, bytes_to_write);
1186 else
1187 wrote = write(tapefd, buffer + size, bytes_to_write);
1188 Dmsg(WRITEDEBUG, "slave %d wrote %d\n", slave_number, wrote);
1189 // This can only happen when our data is incompressible.
1190 // Assume we've exceeded the tape dev size and write a block that is four bytes shorter
1191 if (wrote < 0 && size==0 && bytes_to_write > ntrec*TP_BSIZE) {
1192 warnx("Incompressible block could not be written to tape. Splitting it");
1193 bytes_to_write = ntrec*TP_BSIZE;
1194 } else if (wrote < 0 && errno != ENOSPC)
1195 break;
1196 if (wrote == 0 || (wrote < 0 && errno == ENOSPC)) {
1197 eot_count++;
1198 }
1199 else if (wrote > 0) {
1200 size += wrote;
1201 bytes_to_write = bufsize - size;
1202 }
1203 }
1204
1205 if (size != bufsize)
1206 Dmsg(WRITEDEBUG, "slave %d only wrote %d out of %d bytes and gave up.\n",
1207 slave_number, size, bufsize);
1208
1209 /*
1210 * Handle ENOSPC as an EOT condition.
1211 */
1212 if (wrote < 0 && errno == ENOSPC) {
1213 wrote = 0;
1214 eot_count++;
1215 }
1216
1217 if (eot_count > 0)
1218 returns.clen = returns.unclen = 0;
1219
1220 /*
1221 * pass errno back to master for special handling
1222 */
1223 if (wrote < 0)
1224 returns.unclen = -errno;
1225
1226 /*
1227 * pass size of data and size of write back to master
1228 * (for EOT handling)
1229 */
1230 (void) dump_atomic_write( cmd, (char *)&returns, sizeof returns);
1231
1232 /*
1233 * Signal the next slave to go.
1234 */
1235 (void) kill(nextslave, SIGUSR2);
1236
1237 #ifdef USE_QFA
1238 indexer->updateQfaState(&qfa_state);
1239 #endif
1240 }
1241
1242 #ifdef HAVE_BLOCK_TRANSFORMATION
1243 transformation->endDiskIOProcess(transformation);
1244 #endif /* HAVE_BLOCK_TRANSFORMATION */
1245
1246 if (nread != 0)
1247 quit("error reading command pipe: %s\n", strerror(errno));
1248 free(comp_buf);
1249 }
1250
1251 /*
1252 * Since a read from a pipe may not return all we asked for,
1253 * or a write may not write all we ask if we get a signal,
1254 * loop until the count is satisfied (or error).
1255 */
1256 static ssize_t
1257 dump_atomic_read(int fd, char *buf, size_t count)
1258 {
1259 int got, need = count;
1260
1261 do {
1262 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
1263 buf += got;
1264 } while (got == -1 && errno == EINTR);
1265 return (got < 0 ? got : (ssize_t)count - need);
1266 }
1267
1268 /*
1269 * Since a read from a pipe may not return all we asked for,
1270 * or a write may not write all we ask if we get a signal,
1271 * loop until the count is satisfied (or error).
1272 */
1273 static ssize_t
1274 dump_atomic_write(int fd, const char *buf, size_t count)
1275 {
1276 int got, need = count;
1277
1278 do {
1279 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
1280 buf += got;
1281 } while (got == -1 && errno == EINTR);
1282 return (got < 0 ? got : (ssize_t)count - need);
1283 }