2 * Ported to Linux's Second Extended File System as part of the
3 * dump and restore backup suit
4 * Remy Card <card@Linux.EU.Org>, 1994-1997
5 * Stelian Pop <stelian@popies.net>, 1999-2000
6 * Stelian Pop <stelian@popies.net> - Alcôve <www.alcove.com>, 2000-2002
10 * Copyright (c) 1983, 1993
11 * The Regents of the University of California. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static const char rcsid[] =
44 "$Id: rmt.c,v 1.19 2002/04/16 21:00:59 stelian Exp $";
45 #endif /* not linux */
51 #include <compatlfs.h>
52 #include <sys/types.h>
53 #include <sys/socket.h>
68 static int maxrecsize = -1;
71 static char device[SSIZE];
72 static char count[SSIZE], filemode[SSIZE], pos[SSIZE], op[SSIZE];
74 static char resp[BUFSIZ];
77 #define DEBUG(f) if (debug) fprintf(debug, f)
78 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
79 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
82 * Support for Sun's extended RMT protocol
83 * code originally written by Jörg Schilling <schilling@fokus.gmd.de>
84 * and relicensed by his permission from GPL to BSD for use in dump.
86 * rmt_version is 0 for regular clients (Linux included)
87 * rmt_version is 1 for extended clients (Sun especially). In this case
88 * we support some extended commands (see below) and we remap
89 * the ioctl commands to the UNIX "standard", as per:
90 * ftp://ftp.fokus.gmd.de/pub/unix/star/README.mtio
92 * In order to use rmt version 1, a client must send "I-1\n0\n"
93 * before issuing the other I commands.
95 static int rmt_version = 0;
96 #define RMTI_VERSION -1
99 /* Extended 'i' commands */
101 #define RMTI_NOCACHE 1
107 /* Extended 's' comands */
109 #define MTS_DSREG 'D'
110 #define MTS_ERREG 'E'
111 #define MTS_RESID 'R'
112 #define MTS_FILENO 'F'
113 #define MTS_BLKNO 'B'
114 #define MTS_FLAGS 'f'
117 char *checkbuf __P((char *, int));
118 void error __P((int));
119 void getstring __P((char *));
122 main(int argc, char *argv[])
127 unsigned long block = 0;
131 debug = fopen(*argv, "w");
134 (void)setbuf(debug, (char *)0);
139 if (read(0, &c, 1) != 1)
148 DEBUG2("rmtd: O %s %s\n", device, filemode);
150 * XXX the rmt protocol does not provide a means to
151 * specify the permission bits; allow rw for everyone,
152 * as modified by the users umask
154 tape = OPEN(device, atoi(filemode), 0666);
161 DEBUG1("rmtd: C (%lu blocks)\n", block);
162 getstring(device); /* discard */
172 DEBUG2("rmtd: L %s %s\n", count, pos);
173 rval = LSEEK(tape, (off_t)atol(count), atoi(pos));
181 DEBUG2("rmtd: W %s (block = %lu)\n", count, block);
182 record = checkbuf(record, n);
183 for (i = 0; i < n; i += cc) {
184 cc = read(0, &record[i], n - i);
186 DEBUG("rmtd: premature eof\n");
190 rval = write(tape, record, n);
198 DEBUG2("rmtd: R %s (block %lu)\n", count, block);
200 record = checkbuf(record, n);
201 rval = read(tape, record, n);
204 (void)sprintf(resp, "A%d\n", rval);
205 (void)write(1, resp, strlen(resp));
206 (void)write(1, record, rval);
213 DEBUG2("rmtd: I %s %s\n", op, count);
214 if (atoi(op) == RMTI_VERSION) {
222 /* rmt version 1, assume UNIX client */
265 if (mtop.mt_op == -1) {
271 /* rmt version 0, assume linux client */
272 mtop.mt_op = atoi(op);
274 mtop.mt_count = atoi(count);
275 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
277 rval = mtop.mt_count;
286 DEBUG2 ("rmtd: i %s %s\n", op, count);
290 mtop.mt_op = MTCACHE;
295 mtop.mt_op = MTNOCACHE;
300 mtop.mt_op = MTRETEN;
305 mtop.mt_op = MTERASE;
322 mtop.mt_count = atoi (count);
323 if (ioctl (tape, MTIOCTOP, (char *) &mtop) < 0)
326 rval = mtop.mt_count;
331 case 'S': /* status */
333 { struct mtget mtget;
334 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
336 rval = sizeof (mtget);
337 (void)sprintf(resp, "A%d\n", rval);
338 (void)write(1, resp, strlen(resp));
339 (void)write(1, (char *)&mtget, sizeof (mtget));
347 if (read (0, &s, 1) != 1)
350 if (ioctl (tape, MTIOCGET, (char *) &mtget) < 0)
355 rval = mtget.mt_type;
358 rval = mtget.mt_dsreg;
361 rval = mtget.mt_erreg;
364 rval = mtget.mt_resid;
367 rval = mtget.mt_fileno;
370 rval = mtget.mt_blkno;
373 rval = mtget.mt_gstat;
386 case 'V': /* version */
388 DEBUG1("rmtd: V %s\n", op);
393 DEBUG1("rmtd: garbage command %c\n", c);
397 DEBUG1("rmtd: A %d\n", rval);
398 (void)sprintf(resp, "A%d\n", rval);
399 (void)write(1, resp, strlen(resp));
406 void getstring(char *bp)
411 for (i = 0; i < SSIZE; i++) {
412 if (read(0, cp+i, 1) != 1)
421 checkbuf(char *record, int size)
424 if (size <= maxrecsize)
428 record = malloc(size);
430 DEBUG("rmtd: cannot allocate buffer space\n");
434 while (size > 1024 &&
435 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
444 DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
445 (void)snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
446 (void)write(1, resp, strlen(resp));