dummynet: Don't deliver freed mbuf to callers.
[dragonfly.git] / sbin / dump / tape.c
blob6856da5e2908ee57a393b5dc5faa969a85da8c35
1 /*-
2 * Copyright (c) 1980, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#)tape.c 8.4 (Berkeley) 5/1/95
30 * $FreeBSD: src/sbin/dump/tape.c,v 1.12.2.3 2002/02/23 22:32:51 iedowse Exp $
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/time.h>
36 #include <sys/wait.h>
37 #include <sys/stat.h>
38 #include <vfs/ufs/dinode.h>
39 #include <vfs/ufs/fs.h>
41 #include <protocols/dumprestore.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <setjmp.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
52 #include "dump.h"
54 static int writesize; /* size of malloc()ed buffer for tape */
55 static long lastspclrec = -1; /* tape block number of last written header */
56 static int trecno = 0; /* next record to write in current block */
57 static long blocksthisvol; /* number of blocks on current output file */
58 static const char *nexttape;
59 static int tapeno = 0; /* current tape number */
61 static int atomic_read(int, void *, int);
62 static int atomic_write(int, const void *, int);
63 #ifdef WRITEDEBUG
64 static void doslave(int, int);
65 #else
66 static void doslave(int);
67 #endif
68 static void enslave(void);
69 static void flushtape(void);
70 static void killall(void);
71 static void rollforward(void);
74 * Concurrent dump mods (Caltech) - disk block reading and tape writing
75 * are exported to several slave processes. While one slave writes the
76 * tape, the others read disk blocks; they pass control of the tape in
77 * a ring via signals. The parent process traverses the filesystem and
78 * sends writeheader()'s and lists of daddr's to the slaves via pipes.
79 * The following structure defines the instruction packets sent to slaves.
81 struct req {
82 daddr_t dblk;
83 int count;
85 int reqsiz;
87 #define SLAVES 3 /* 1 slave writing, 1 reading, 1 for slack */
88 struct slave {
89 int tapea; /* header number at start of this chunk */
90 int count; /* count to next header (used for TS_TAPE */
91 /* after EOT) */
92 int inode; /* inode that we are currently dealing with */
93 int fd; /* FD for this slave */
94 int pid; /* PID for this slave */
95 int sent; /* 1 == we've sent this slave requests */
96 int firstrec; /* record number of this block */
97 char (*tblock)[TP_BSIZE]; /* buffer for data blocks */
98 struct req *req; /* buffer for requests */
99 } slaves[SLAVES+1];
100 struct slave *slp;
102 char (*nextblock)[TP_BSIZE];
104 int master; /* pid of master, for sending error signals */
105 int tenths; /* length of tape used per block written */
106 static int caught; /* have we caught the signal to proceed? */
107 static int ready; /* have we reached the lock point without having */
108 /* received the SIGUSR2 signal from the prev slave? */
109 static jmp_buf jmpbuf; /* where to jump to if we are ready when the */
110 /* SIGUSR2 arrives from the previous slave */
113 alloctape(void)
115 int pgoff = getpagesize() - 1;
116 char *buf;
117 int i;
119 writesize = ntrec * TP_BSIZE;
120 reqsiz = (ntrec + 1) * sizeof(struct req);
122 * CDC 92181's and 92185's make 0.8" gaps in 1600-bpi start/stop mode
123 * (see DEC TU80 User's Guide). The shorter gaps of 6250-bpi require
124 * repositioning after stopping, i.e, streaming mode, where the gap is
125 * variable, 0.30" to 0.45". The gap is maximal when the tape stops.
127 if (blocksperfile == 0 && !unlimited)
128 tenths = writesize / density +
129 (cartridge ? 16 : density == 625 ? 5 : 8);
131 * Allocate tape buffer contiguous with the array of instruction
132 * packets, so flushtape() can write them together with one write().
133 * Align tape buffer on page boundary to speed up tape write().
135 for (i = 0; i <= SLAVES; i++) {
136 buf = (char *)
137 malloc((unsigned)(reqsiz + writesize + pgoff + TP_BSIZE));
138 if (buf == NULL)
139 return(0);
140 slaves[i].tblock = (char (*)[TP_BSIZE])
141 (((long)&buf[ntrec + 1] + pgoff) &~ pgoff);
142 slaves[i].req = (struct req *)slaves[i].tblock - ntrec - 1;
144 slp = &slaves[0];
145 slp->count = 1;
146 slp->tapea = 0;
147 slp->firstrec = 0;
148 nextblock = slp->tblock;
149 return(1);
152 void
153 writerec(const void *dp, int isspcl)
156 slp->req[trecno].dblk = (daddr_t)0;
157 slp->req[trecno].count = 1;
158 bcopy(dp, *(nextblock)++, sizeof (union u_spcl));
159 if (isspcl)
160 lastspclrec = spcl.c_tapea;
161 trecno++;
162 spcl.c_tapea++;
163 if (trecno >= ntrec)
164 flushtape();
167 void
168 dumpblock(daddr_t blkno, int size)
170 int avail, tpblks, dblkno;
172 dblkno = fsbtodb(sblock, blkno);
173 tpblks = size >> tp_bshift;
174 while ((avail = MIN(tpblks, ntrec - trecno)) > 0) {
175 slp->req[trecno].dblk = dblkno;
176 slp->req[trecno].count = avail;
177 trecno += avail;
178 spcl.c_tapea += avail;
179 if (trecno >= ntrec)
180 flushtape();
181 dblkno += avail << (tp_bshift - dev_bshift);
182 tpblks -= avail;
186 int nogripe = 0;
188 static void
189 tperror(int signo __unused)
192 if (pipeout) {
193 msg("write error on %s\n", tape);
194 quit("Cannot recover\n");
195 /* NOTREACHED */
197 msg("write error %ld blocks into volume %d\n", blocksthisvol, tapeno);
198 broadcast("DUMP WRITE ERROR!\n");
199 if (!query("Do you want to restart?"))
200 dumpabort(0);
201 msg("Closing this volume. Prepare to restart with new media;\n");
202 msg("this dump volume will be rewritten.\n");
203 killall();
204 nogripe = 1;
205 close_rewind();
206 Exit(X_REWRITE);
209 static void
210 sigpipe(int signo __unused)
213 quit("Broken pipe\n");
216 static void
217 flushtape(void)
219 int i, blks, got;
220 long lastfirstrec;
222 int siz = (char *)nextblock - (char *)slp->req;
224 slp->req[trecno].count = 0; /* Sentinel */
226 if (atomic_write(slp->fd, slp->req, siz) != siz)
227 quit("error writing command pipe: %s\n", strerror(errno));
228 slp->sent = 1; /* we sent a request, read the response later */
230 lastfirstrec = slp->firstrec;
232 if (++slp >= &slaves[SLAVES])
233 slp = &slaves[0];
235 /* Read results back from next slave */
236 if (slp->sent) {
237 if (atomic_read(slp->fd, &got, sizeof got)
238 != sizeof got) {
239 perror(" DUMP: error reading command pipe in master");
240 dumpabort(0);
242 slp->sent = 0;
244 /* Check for end of tape */
245 if (got < writesize) {
246 msg("End of tape detected\n");
249 * Drain the results, don't care what the values were.
250 * If we read them here then trewind won't...
252 for (i = 0; i < SLAVES; i++) {
253 if (slaves[i].sent) {
254 if (atomic_read(slaves[i].fd,
255 &got, sizeof got)
256 != sizeof got) {
257 perror(" DUMP: error reading command pipe in master");
258 dumpabort(0);
260 slaves[i].sent = 0;
264 close_rewind();
265 rollforward();
266 return;
270 blks = 0;
271 if (spcl.c_type != TS_END) {
272 for (i = 0; i < spcl.c_count; i++)
273 if (spcl.c_addr[i] != 0)
274 blks++;
276 slp->count = lastspclrec + blks + 1 - spcl.c_tapea;
277 slp->tapea = spcl.c_tapea;
278 slp->firstrec = lastfirstrec + ntrec;
279 slp->inode = curino;
280 nextblock = slp->tblock;
281 trecno = 0;
282 asize += tenths;
283 blockswritten += ntrec;
284 blocksthisvol += ntrec;
285 if (!pipeout && !unlimited && (blocksperfile ?
286 (blocksthisvol >= blocksperfile) : (asize > tsize))) {
287 close_rewind();
288 startnewtape(0);
290 timeest();
293 void
294 trewind(void)
296 struct stat sb;
297 int f;
298 int got;
300 for (f = 0; f < SLAVES; f++) {
302 * Drain the results, but unlike EOT we DO (or should) care
303 * what the return values were, since if we detect EOT after
304 * we think we've written the last blocks to the tape anyway,
305 * we have to replay those blocks with rollforward.
307 * fixme: punt for now.
309 if (slaves[f].sent) {
310 if (atomic_read(slaves[f].fd, &got, sizeof got)
311 != sizeof got) {
312 perror(" DUMP: error reading command pipe in master");
313 dumpabort(0);
315 slaves[f].sent = 0;
316 if (got != writesize) {
317 msg("EOT detected in last 2 tape records!\n");
318 msg("Use a longer tape, decrease the size estimate\n");
319 quit("or use no size estimate at all.\n");
322 close(slaves[f].fd);
324 while (wait(NULL) >= 0) /* wait for any signals from slaves */
325 /* void */;
327 if (pipeout)
328 return;
330 msg("Closing %s\n", tape);
332 #ifdef RDUMP
333 if (host) {
334 rmtclose();
335 while (rmtopen(tape, 0) < 0)
336 sleep(10);
337 rmtclose();
338 return;
340 #endif
341 if (fstat(tapefd, &sb) == 0 && S_ISFIFO(sb.st_mode)) {
342 close(tapefd);
343 return;
345 close(tapefd);
346 while ((f = open(tape, 0)) < 0)
347 sleep (10);
348 close(f);
351 void
352 close_rewind(void)
354 time_t tstart_changevol, tend_changevol;
356 trewind();
357 if (nexttape)
358 return;
359 time((time_t *)&(tstart_changevol));
360 if (!nogripe) {
361 msg("Change Volumes: Mount volume #%d\n", tapeno+1);
362 broadcast("CHANGE DUMP VOLUMES!\a\a\n");
364 while (!query("Is the new volume mounted and ready to go?"))
365 if (query("Do you want to abort?")) {
366 dumpabort(0);
367 /*NOTREACHED*/
369 time((time_t *)&(tend_changevol));
370 if ((tstart_changevol != (time_t)-1) && (tend_changevol != (time_t)-1))
371 tstart_writing += (tend_changevol - tstart_changevol);
374 void
375 rollforward(void)
377 struct req *p, *q, *prev;
378 struct slave *tslp;
379 int i, size, savedtapea, got;
380 union u_spcl *ntb, *otb;
381 tslp = &slaves[SLAVES];
382 ntb = (union u_spcl *)tslp->tblock[1];
385 * Each of the N slaves should have requests that need to
386 * be replayed on the next tape. Use the extra slave buffers
387 * (slaves[SLAVES]) to construct request lists to be sent to
388 * each slave in turn.
390 for (i = 0; i < SLAVES; i++) {
391 q = &tslp->req[1];
392 otb = (union u_spcl *)slp->tblock;
395 * For each request in the current slave, copy it to tslp.
398 prev = NULL;
399 for (p = slp->req; p->count > 0; p += p->count) {
400 *q = *p;
401 if (p->dblk == 0)
402 *ntb++ = *otb++; /* copy the datablock also */
403 prev = q;
404 q += q->count;
406 if (prev == NULL)
407 quit("rollforward: protocol botch");
408 if (prev->dblk != 0)
409 prev->count -= 1;
410 else
411 ntb--;
412 q -= 1;
413 q->count = 0;
414 q = &tslp->req[0];
415 if (i == 0) {
416 q->dblk = 0;
417 q->count = 1;
418 trecno = 0;
419 nextblock = tslp->tblock;
420 savedtapea = spcl.c_tapea;
421 spcl.c_tapea = slp->tapea;
422 startnewtape(0);
423 spcl.c_tapea = savedtapea;
424 lastspclrec = savedtapea - 1;
426 size = (char *)ntb - (char *)q;
427 if (atomic_write(slp->fd, q, size) != size) {
428 perror(" DUMP: error writing command pipe");
429 dumpabort(0);
431 slp->sent = 1;
432 if (++slp >= &slaves[SLAVES])
433 slp = &slaves[0];
435 q->count = 1;
437 if (prev->dblk != 0) {
439 * If the last one was a disk block, make the
440 * first of this one be the last bit of that disk
441 * block...
443 q->dblk = prev->dblk +
444 prev->count * (TP_BSIZE / DEV_BSIZE);
445 ntb = (union u_spcl *)tslp->tblock;
446 } else {
448 * It wasn't a disk block. Copy the data to its
449 * new location in the buffer.
451 q->dblk = 0;
452 *((union u_spcl *)tslp->tblock) = *ntb;
453 ntb = (union u_spcl *)tslp->tblock[1];
456 slp->req[0] = *q;
457 nextblock = slp->tblock;
458 if (q->dblk == 0)
459 nextblock++;
460 trecno = 1;
463 * Clear the first slaves' response. One hopes that it
464 * worked ok, otherwise the tape is much too short!
466 if (slp->sent) {
467 if (atomic_read(slp->fd, &got, sizeof got)
468 != sizeof got) {
469 perror(" DUMP: error reading command pipe in master");
470 dumpabort(0);
472 slp->sent = 0;
474 if (got != writesize) {
475 quit("EOT detected at start of the tape!\n");
481 * We implement taking and restoring checkpoints on the tape level.
482 * When each tape is opened, a new process is created by forking; this
483 * saves all of the necessary context in the parent. The child
484 * continues the dump; the parent waits around, saving the context.
485 * If the child returns X_REWRITE, then it had problems writing that tape;
486 * this causes the parent to fork again, duplicating the context, and
487 * everything continues as if nothing had happened.
489 void
490 startnewtape(int top)
492 int parentpid;
493 int childpid;
494 int status;
495 int wait_pid;
496 char *p;
497 sig_t interrupt_save;
499 interrupt_save = signal(SIGINT, SIG_IGN);
500 parentpid = getpid();
502 restore_check_point:
503 signal(SIGINT, interrupt_save);
505 * All signals are inherited...
507 setproctitle(NULL); /* Restore the proctitle. */
508 childpid = fork();
509 if (childpid < 0) {
510 msg("Context save fork fails in parent %d\n", parentpid);
511 Exit(X_ABORT);
513 if (childpid != 0) {
515 * PARENT:
516 * save the context by waiting
517 * until the child doing all of the work returns.
518 * don't catch the interrupt
520 signal(SIGINT, SIG_IGN);
521 #ifdef TDEBUG
522 msg("Tape: %d; parent process: %d child process %d\n",
523 tapeno+1, parentpid, childpid);
524 #endif /* TDEBUG */
525 while ((wait_pid = wait(&status)) != childpid)
526 msg("Parent %d waiting for child %d has another child %d return\n",
527 parentpid, childpid, wait_pid);
528 if (status & 0xFF) {
529 msg("Child %d returns LOB status %o\n",
530 childpid, status&0xFF);
532 status = (status >> 8) & 0xFF;
533 #ifdef TDEBUG
534 switch(status) {
535 case X_FINOK:
536 msg("Child %d finishes X_FINOK\n", childpid);
537 break;
538 case X_ABORT:
539 msg("Child %d finishes X_ABORT\n", childpid);
540 break;
541 case X_REWRITE:
542 msg("Child %d finishes X_REWRITE\n", childpid);
543 break;
544 default:
545 msg("Child %d finishes unknown %d\n",
546 childpid, status);
547 break;
549 #endif /* TDEBUG */
550 switch(status) {
551 case X_FINOK:
552 Exit(X_FINOK);
553 case X_ABORT:
554 Exit(X_ABORT);
555 case X_REWRITE:
556 goto restore_check_point;
557 default:
558 msg("Bad return code from dump: %d\n", status);
559 Exit(X_ABORT);
561 /*NOTREACHED*/
562 } else { /* we are the child; just continue */
563 #ifdef TDEBUG
564 sleep(4); /* allow time for parent's message to get out */
565 msg("Child on Tape %d has parent %d, my pid = %d\n",
566 tapeno+1, parentpid, getpid());
567 #endif /* TDEBUG */
569 * If we have a name like "/dev/rmt0,/dev/rmt1",
570 * use the name before the comma first, and save
571 * the remaining names for subsequent volumes.
573 tapeno++; /* current tape sequence */
574 if (nexttape || strchr(tape, ',')) {
575 if (nexttape && *nexttape)
576 tape = nexttape;
577 if ((p = strchr(tape, ',')) != NULL) {
578 *p = '\0';
579 nexttape = p + 1;
580 } else
581 nexttape = NULL;
582 msg("Dumping volume %d on %s\n", tapeno, tape);
584 #ifdef RDUMP
585 while ((tapefd = (host ? rmtopen(tape, 2) :
586 pipeout ? 1 : open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
587 #else
588 while ((tapefd = (pipeout ? 1 :
589 open(tape, O_WRONLY|O_CREAT, 0666))) < 0)
590 #endif
592 msg("Cannot open output \"%s\".\n", tape);
593 if (!query("Do you want to retry the open?"))
594 dumpabort(0);
597 enslave(); /* Share open tape file descriptor with slaves */
598 signal(SIGINFO, infosch);
600 asize = 0;
601 blocksthisvol = 0;
602 if (top)
603 newtape++; /* new tape signal */
604 spcl.c_count = slp->count;
606 * measure firstrec in TP_BSIZE units since restore doesn't
607 * know the correct ntrec value...
609 spcl.c_firstrec = slp->firstrec;
610 spcl.c_volume++;
611 spcl.c_type = TS_TAPE;
612 spcl.c_flags |= DR_NEWHEADER;
613 writeheader((ufs1_ino_t)slp->inode);
614 spcl.c_flags &=~ DR_NEWHEADER;
615 if (tapeno > 1)
616 msg("Volume %d begins with blocks from inode %d\n",
617 tapeno, slp->inode);
621 void
622 dumpabort(int signo __unused)
625 if (master != 0 && master != getpid())
626 /* Signals master to call dumpabort */
627 kill(master, SIGTERM);
628 else {
629 killall();
630 msg("The ENTIRE dump is aborted.\n");
632 #ifdef RDUMP
633 rmtclose();
634 #endif
635 Exit(X_ABORT);
638 void
639 Exit(int status)
642 #ifdef TDEBUG
643 msg("pid = %d exits with status %d\n", getpid(), status);
644 #endif /* TDEBUG */
645 exit(status);
649 * proceed - handler for SIGUSR2, used to synchronize IO between the slaves.
651 static void
652 proceed(int signo __unused)
655 if (ready)
656 longjmp(jmpbuf, 1);
657 caught++;
660 static void
661 enslave(void)
663 int cmd[2];
664 int i, j;
666 master = getpid();
668 signal(SIGTERM, dumpabort); /* Slave sends SIGTERM on dumpabort() */
669 signal(SIGPIPE, sigpipe);
670 signal(SIGUSR1, tperror); /* Slave sends SIGUSR1 on tape errors */
671 signal(SIGUSR2, proceed); /* Slave sends SIGUSR2 to next slave */
673 for (i = 0; i < SLAVES; i++) {
674 if (i == slp - &slaves[0]) {
675 caught = 1;
676 } else {
677 caught = 0;
680 if (socketpair(AF_UNIX, SOCK_STREAM, 0, cmd) < 0 ||
681 (slaves[i].pid = fork()) < 0)
682 quit("too many slaves, %d (recompile smaller): %s\n",
683 i, strerror(errno));
685 slaves[i].fd = cmd[1];
686 slaves[i].sent = 0;
687 if (slaves[i].pid == 0) { /* Slave starts up here */
688 for (j = 0; j <= i; j++)
689 close(slaves[j].fd);
690 signal(SIGINT, SIG_IGN); /* Master handles this */
691 #ifdef WRITEDEBUG
692 doslave(cmd[0], i);
693 #else
694 doslave(cmd[0]);
695 #endif
696 Exit(X_FINOK);
700 for (i = 0; i < SLAVES; i++)
701 atomic_write(slaves[i].fd, &slaves[(i + 1) % SLAVES].pid,
702 sizeof slaves[0].pid);
704 master = 0;
707 void
708 killall(void)
710 int i;
712 for (i = 0; i < SLAVES; i++)
713 if (slaves[i].pid > 0) {
714 kill(slaves[i].pid, SIGKILL);
715 slaves[i].sent = 0;
720 * Synchronization - each process has a lockfile, and shares file
721 * descriptors to the following process's lockfile. When our write
722 * completes, we release our lock on the following process's lock-
723 * file, allowing the following process to lock it and proceed. We
724 * get the lock back for the next cycle by swapping descriptors.
726 static void
727 #ifdef WRITEDEBUG
728 doslave(int cmd, int slave_number)
729 #else
730 doslave(int cmd)
731 #endif
733 int nread;
734 int nextslave, size, wrote, eot_count;
737 * Need our own seek pointer.
739 close(diskfd);
740 if ((diskfd = open(disk, O_RDONLY)) < 0)
741 quit("slave couldn't reopen disk: %s\n", strerror(errno));
744 * Need the pid of the next slave in the loop...
746 if ((nread = atomic_read(cmd, &nextslave, sizeof nextslave))
747 != sizeof nextslave) {
748 quit("master/slave protocol botched - didn't get pid of next slave.\n");
752 * Get list of blocks to dump, read the blocks into tape buffer
754 while ((nread = atomic_read(cmd, slp->req, reqsiz)) == reqsiz) {
755 struct req *p = slp->req;
757 for (trecno = 0; trecno < ntrec;
758 trecno += p->count, p += p->count) {
759 if (p->dblk) {
760 bread(p->dblk, slp->tblock[trecno],
761 p->count * TP_BSIZE);
762 } else {
763 if (p->count != 1 ||
764 atomic_read(cmd, slp->tblock[trecno],
765 TP_BSIZE) != TP_BSIZE)
766 quit("master/slave protocol botched.\n");
769 if (setjmp(jmpbuf) == 0) {
770 ready = 1;
771 if (!caught)
772 pause();
774 ready = 0;
775 caught = 0;
777 /* Try to write the data... */
778 eot_count = 0;
779 size = 0;
780 wrote = 0;
782 while (eot_count < 10 && size < writesize) {
783 #ifdef RDUMP
784 if (host)
785 wrote = rmtwrite(slp->tblock[0]+size,
786 writesize-size);
787 else
788 #endif
789 wrote = write(tapefd, slp->tblock[0]+size,
790 writesize-size);
791 #ifdef WRITEDEBUG
792 printf("slave %d wrote %d\n", slave_number, wrote);
793 #endif
794 if (wrote < 0)
795 break;
796 if (wrote == 0)
797 eot_count++;
798 size += wrote;
801 #ifdef WRITEDEBUG
802 if (size != writesize)
803 printf("slave %d only wrote %d out of %d bytes and gave up.\n",
804 slave_number, size, writesize);
805 #endif
808 * Handle ENOSPC as an EOT condition.
810 if (wrote < 0 && errno == ENOSPC) {
811 wrote = 0;
812 eot_count++;
815 if (eot_count > 0)
816 size = 0;
818 if (wrote < 0) {
819 kill(master, SIGUSR1);
820 for (;;)
821 sigpause(0);
822 } else {
824 * pass size of write back to master
825 * (for EOT handling)
827 atomic_write(cmd, &size, sizeof size);
831 * If partial write, don't want next slave to go.
832 * Also jolts him awake.
834 kill(nextslave, SIGUSR2);
836 if (nread != 0)
837 quit("error reading command pipe: %s\n", strerror(errno));
840 static int
841 atomic_read(int fd, void *buf, int count)
843 int got, need = count;
845 while ((got = read(fd, buf, need)) > 0 && (need -= got) > 0)
846 buf = (uint8_t *)buf + got;
847 return (got < 0 ? got : count - need);
850 static int
851 atomic_write(int fd, const void *buf, int count)
853 int got, need = count;
855 while ((got = write(fd, buf, need)) > 0 && (need -= got) > 0)
856 buf = (const uint8_t *)buf + got;
857 return (got < 0 ? got : count - need);