1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26/*
27 * Copyright (c) 1980 Regents of the University of California.
28 * All rights reserved. The Berkeley software License Agreement
29 * specifies the terms and conditions for redistribution.
30 */
31
32#ifndef _DUMP_H
33#define	_DUMP_H
34
35#include <stdio.h>
36#include <locale.h>
37#include <sys/types.h>
38#include <ctype.h>
39#include <string.h>
40#include <syslog.h>
41#include <errno.h>
42#include <fcntl.h>
43#include <utmpx.h>
44#include <signal.h>
45#include <stdlib.h>
46#include <time.h>
47#include <sys/param.h>	/* for MAXBSIZE */
48#include <sys/stat.h>
49#include <sys/time.h>
50#include <sys/wait.h>
51#include <sys/vnode.h>	/* needed by inode.h */
52#include <setjmp.h>
53#include <sys/mman.h>
54#include <assert.h>
55#include <dumpusg.h>
56#include <kstat.h>
57#include <sys/fssnap_if.h>
58#include <libgen.h>
59#include <limits.h>
60
61#ifdef	__cplusplus
62extern "C" {
63#endif
64
65#define	SUPPORTS_MTB_TAPE_FORMAT
66#include <protocols/dumprestore.h>
67#include <memutils.h>
68#include <note.h>
69
70#define	NI		16
71#define	MAXINOPB	(MAXBSIZE / sizeof (struct dinode))
72#define	MAXNINDIR	(MAXBSIZE / sizeof (daddr32_t))
73
74#ifndef roundup
75#define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))
76#endif
77#ifndef MIN
78#define	MIN(a, b)	(((a) < (b)) ? (a) : (b))
79#endif
80#ifndef MAX
81#define	MAX(a, b)	(((a) > (b)) ? (a) : (b))
82#endif
83
84/*
85 * Define an overflow-free version of howmany so that we don't
86 * run into trouble with large files.
87 */
88#define	d_howmany(x, y)	((x) / (y) + ((x) % (y) != 0))
89
90#define	MWORD(m, i)	(m[(ino_t)(i-1)/NBBY])
91#define	MBIT(i)		((1<<((ino_t)(i-1)%NBBY))&0xff)
92#define	BIS(i, w)	(MWORD(w, i) |= MBIT(i))
93#define	BIC(i, w)	(MWORD(w, i) &= ~MBIT(i))
94#define	BIT(i, w)	(MWORD(w, i) & MBIT(i))
95
96uint_t	msiz;
97uchar_t	*clrmap;
98uchar_t	*dirmap;
99uchar_t	*filmap;
100uchar_t	*nodmap;
101uchar_t	*shamap;
102uchar_t	*activemap;
103
104/*
105 *	All calculations done in 0.1" units!
106 */
107
108char	*disk;		/* name of the disk file */
109char	*dname;		/* name to put in /etc/dumpdates */
110int	disk_dynamic;	/* true if disk refers to dynamic storage */
111char	*tape;		/* name of the tape file */
112char	*host;		/* name of the remote tape host (may be "user@host") */
113char	*dumpdev;	/* hostname:device for current volume */
114char	*sdumpdev;	/* short form of dumpdev (no user name if remote) */
115char	*increm;	/* name of file containing incremental information */
116char	*filesystem;	/* name of the file system */
117char	*myname;	/* argv[0] without leading path components */
118char	lastincno;	/* increment number of previous dump */
119char	incno;		/* increment number */
120char	*tlabel;	/* what goes in tape header c_label field */
121int	uflag;		/* update flag */
122int	fi;		/* disk file descriptor */
123int	to;		/* tape file descriptor */
124int	mapfd;		/* block disk device descriptor for mmap */
125int	pipeout;	/* true => output to standard output */
126int	tapeout;	/* true => output to a tape drive */
127ino_t	ino;		/* current inumber; used globally */
128off_t	pos;		/* starting offset within ino; used globally */
129int	leftover;	/* number of tape recs left over from prev vol */
130int	nsubdir;	/* counts subdirs, for deciding to dump a dir */
131int	newtape;	/* new tape flag */
132int	nadded;		/* number of added sub directories */
133int	dadded;		/* directory added flag */
134int	density;	/* density in 0.1" units */
135ulong_t	tsize;		/* tape size in 0.1" units */
136u_offset_t esize;	/* estimated tape size, blocks */
137u_offset_t o_esize;	/* number of header blocks (overhead) */
138u_offset_t f_esize;	/* number of TP_BSIZE blocks for files/maps */
139uint_t	etapes;		/* estimated number of tapes */
140uint_t	ntrec;		/* 1K records per tape block */
141int	tenthsperirg;	/* 1/10" per tape inter-record gap */
142dev_t 	partial_dev;	/* id of BLOCK device used in partial mode */
143pid_t	dumppid;	/* process-ID of top-level process */
144
145int	verify;		/* verify each volume */
146int	doingverify;	/* true => doing a verify pass */
147int	active;		/* recopy active files */
148int	doingactive;	/* true => redumping active files */
149int	archive;	/* true => saving a archive in archivefile */
150char	*archivefile;	/* name of archivefile */
151int	archive_opened;	/* have opened/created the archivefile */
152int	notify;		/* notify operator flag */
153int	diskette;	/* true if dumping to a diskette */
154int	cartridge;	/* true if dumping to a cartridge tape */
155uint_t	tracks;		/* number of tracks on a cartridge tape */
156int	printsize;	/* just print estimated size and exit */
157int	offline;	/* take tape offline after rewinding */
158int	autoload;	/* wait for next tape to autoload; implies offline */
159int	autoload_tries;	/* number of times to check on autoload */
160int	autoload_period; /* seconds, tries*period = total wait time */
161int	doposition;	/* move to specified... */
162daddr32_t filenum;	/* position of dump on 1st volume */
163int	dumpstate;	/* dump output state (see below) */
164int	dumptoarchive;	/* mark records to be archived */
165
166int	blockswritten;	/* number of blocks written on current tape */
167uint_t	tapeno;		/* current tape number */
168
169struct fs *sblock;	/* the file system super block */
170int	shortmeta;	/* current file has small amount of metadata */
171union u_shadow c_shadow_save[1];
172
173time_t	*telapsed;	/* time spent writing previous tapes */
174time_t	*tstart_writing; /* when we started writing the latest tape */
175time_t	*tschedule;	/* when next to give a remaining-time estimate */
176
177char	*debug_chdir;	/* non-NULL means to mkdir this/pid, and chdir there, */
178			/* once for each separate child */
179
180/*
181 * Defines for the msec part of
182 * inode-based times, since we're
183 * not part of the kernel.
184 */
185#define	di_atspare	di_ic.ic_atspare
186#define	di_mtspare	di_ic.ic_mtspare
187#define	di_ctspare	di_ic.ic_ctspare
188
189#define	HOUR	(60L*60L)
190#define	DAY	(24L*HOUR)
191#define	YEAR	(365L*DAY)
192
193/*
194 *	Dump output states
195 */
196#define	DS_INIT		0
197#define	DS_START	1
198#define	DS_CLRI		2
199#define	DS_BITS		3
200#define	DS_DIRS		4
201#define	DS_FILES	5
202#define	DS_END		6
203#define	DS_DONE		7
204
205/*
206 *	Exit status codes
207 */
208#define	X_FINOK		0	/* normal exit */
209#define	X_REWRITE	2	/* restart writing from the check point */
210#define	X_ABORT		3	/* abort all of dump; no checkpoint restart */
211#define	X_VERIFY	4	/* verify the reel just written */
212#define	X_RESTART	5	/* abort all progress so far; attempt restart */
213
214#define	NINCREM	"/etc/dumpdates"	/* new format incremental info */
215
216#define	TAPE	"/dev/rmt/0b"		/* default tape device */
217#define	OPGRENT	"sys"			/* group entry to notify */
218#define	DIALUP	"ttyd"			/* prefix for dialups */
219
220#define	DISKETTE	"/dev/rfd0c"
221
222#define	NBUF		64		/* number of output buffers */
223#define	MAXNTREC	256		/* max tape blocking factor (in Kb) */
224
225/*
226 *	The contents of the file NINCREM are maintained both on
227 *	a linked list and then (eventually) arrayified.
228 */
229struct	idates {
230	char	id_name[MAXNAMLEN+3];
231	char	id_incno;
232	time32_t id_ddate;
233};
234
235size_t	nidates;		/* number of records (might be zero) */
236struct	idates	**idatev;	/* the arrayfied version */
237#define	ITITERATE(i, ip)	\
238	for (i = 0; i < nidates && (ip = idatev[i]) != NULL; i++)
239
240/*
241 * Function declarations
242 */
243#ifdef __STDC__
244/*
245 * dumpfstab.c
246 */
247extern void mnttabread(void);
248extern struct mntent *mnttabsearch(char *, int);
249extern void setmnttab(void);
250extern struct mntent *getmnttab(void);
251/*
252 * dumpitime.c
253 */
254extern char *prdate(time_t);
255extern void inititimes(void);
256extern void getitime(void);
257extern void putitime(void);
258extern void est(struct dinode *);
259extern time32_t is_fssnap_dump(char *);
260extern void bmapest(uchar_t *);
261/*
262 * dumplabel.c
263 */
264extern void getlabel(void);
265/*
266 * dumpmain.c
267 */
268extern void child_chdir(void);
269extern char *unrawname(char *);
270extern void sigAbort(int);
271extern char *rawname(char *);
272extern char *lf_rawname(char *);
273extern time32_t timeclock(time32_t);
274#ifdef signal
275extern void (*nsignal(int, void (*)(int)))(int);
276#endif
277extern int safe_file_open(const char *file, int mode, int perms);
278extern int safe_device_open(const char *file, int mode, int perms);
279extern FILE *safe_fopen(const char *filename, const char *smode, int perms);
280/*
281 * dumponline.c
282 */
283extern void allocino(void);
284extern void freeino(void);
285extern void saveino(ino_t, struct dinode *);
286extern void resetino(ino_t);
287extern long getigen(ino_t);
288extern int lf_ismounted(char *, char *);
289extern int isoperator(uid_t, gid_t);
290extern int lockfs(char *, char *);
291extern int openi(ino_t, long, char *);
292extern caddr_t mapfile(int, off_t, off_t, int);
293extern void unmapfile(void);
294extern void stattoi(struct stat *, struct dinode *);
295extern void dumpfile(int, caddr_t, off_t, off_t, off_t, int, int);
296extern void activepass(void);
297/*
298 * dumpoptr.c
299 */
300extern int query(char *);
301extern int query_once(char *, int);
302extern void interrupt(int);
303extern void broadcast(char *);
304extern void timeest(int, int);
305/*PRINTFLIKE1*/
306extern void msg(const char *, ...);
307/*PRINTFLIKE1*/
308extern void msgtail(const char *, ...);
309extern void lastdump(int);
310extern char *getresponse(char *, char *);
311/*
312 * dumptape.c
313 */
314extern void alloctape(void);
315extern void reset(void);
316extern void spclrec(void);
317extern void taprec(uchar_t *, int, int);
318extern void dmpblk(daddr32_t, size_t, off_t);
319extern void toslave(void (*)(ino_t), ino_t);
320extern void doinode(ino_t);
321extern void dospcl(ino_t);
322extern void flushcmds(void);
323extern void flusht(void);
324extern void nextdevice(void);
325extern int isrewind(int);
326extern void trewind(void);
327extern void close_rewind(void);
328extern void changevol(void);
329extern void otape(int);
330extern void dumpabort(void);
331extern void dumpailing(void);
332extern void Exit(int);
333extern void positiontape(char *);
334/*
335 * dumptraverse.c
336 */
337extern void pass(void (*)(struct dinode *), uchar_t *);
338extern void mark(struct dinode *);
339extern void active_mark(struct dinode *);
340extern void markshad(struct dinode *);
341extern void estshad(struct dinode *);
342extern void freeshad();
343extern void add(struct dinode *);
344extern void dirdump(struct dinode *);
345extern void dump(struct dinode *);
346extern void lf_dump(struct dinode *);
347extern void dumpblocks(ino_t);
348extern void bitmap(uchar_t *, int);
349extern struct dinode *getino(ino_t);
350extern void bread(diskaddr_t, uchar_t *, size_t);
351extern int hasshortmeta(struct dinode **ip);
352/*
353 * lftw.c
354 */
355extern int lftw(const char *,
356	int (*)(const char *, const struct stat *, int), int);
357extern int lf_lftw(const char *,
358	int (*)(const char *, const struct stat64 *, int), int);
359/*
360 * partial.c
361 */
362extern void partial_check(void);
363extern void lf_partial_check(void);
364extern int partial_mark(int, char **);
365/*
366 * unctime.c
367 */
368extern time_t unctime(char *);
369#else	/* !STDC */
370/*
371 * dumpfstab.c
372 */
373extern void mnttabread();
374extern struct mntent *mnttabsearch();
375extern void setmnttab();
376extern struct mntent *getmnttab();
377/*
378 * dumpitime.c
379 */
380extern char *prdate();
381extern void inititimes();
382extern void getitime();
383extern void putitime();
384extern void est();
385extern time32_t is_fssnap_dump();
386extern void bmapest();
387/*
388 * dumplabel.c
389 */
390extern void getlabel();
391/*
392 * dumpmain.c
393 */
394extern void child_chdir();
395extern char *unrawname();
396extern void sigAbort();
397extern char *rawname();
398extern char *lf_rawname();
399extern time_t timeclock();
400#ifdef signal
401extern void nsignal();
402#endif
403extern int safe_file_open();
404extern int safe_device_open();
405extern FILE *safe_fopen();
406/*
407 * dumponline.c
408 */
409extern void allocino();
410extern void freeino();
411extern void saveino();
412extern void resetino();
413extern long getigen();
414extern int lf_ismounted();
415extern int isoperator();
416extern ulong_t lockfs();
417extern int openi();
418extern caddr_t mapfile();
419extern void unmapfile();
420extern void stattoi();
421extern void dumpfile();
422extern void activepass();
423/*
424 * dumpoptr.c
425 */
426extern int query();
427extern int query_once();
428extern void interrupt();
429extern void broadcast();
430extern void timeest();
431extern void msg();
432extern void msgtail();
433extern void lastdump();
434extern char *getresponse();
435/*
436 * dumptape.c
437 */
438extern void alloctape();
439extern void reset();
440extern void spclrec();
441extern void taprec();
442extern void dmpblk();
443extern void toslave();
444extern void doinode();
445extern void dospcl();
446extern void flushcmds();
447extern void flusht();
448extern void nextdevice();
449extern int isrewind();
450extern void trewind();
451extern void close_rewind();
452extern void changevol();
453extern void otape();
454extern void dumpabort();
455extern void dumpailing();
456extern void Exit();
457extern void positiontape();
458/*
459 * dumptraverse.c
460 */
461extern void pass();
462extern void mark();
463extern void active_mark();
464extern void markshad();
465extern void estshad();
466extern void freeshad();
467extern void add();
468extern void dirdump();
469extern void dump();
470extern void lf_dump();
471extern void dumpblocks();
472extern void bitmap();
473extern struct dinode *getino();
474extern void bread();
475extern int hasshortmeta();
476/*
477 * lftw.c
478 */
479extern int lftw();
480extern int lf_lftw();
481/*
482 * partial.c
483 */
484extern void partial_check();
485extern void lf_partial_check();
486extern int partial_mark();
487/*
488 * unctime.c
489 */
490extern time_t unctime();
491#endif /* __STDC__ */
492
493/* Insufficiently-featureful system header files... */
494NOTE(ALIGNMENT(mmap, 8))
495
496
497#ifdef	__cplusplus
498}
499#endif
500
501#endif /* _DUMP_H */
502