xref: /netbsd-src/sbin/dump/dump.h (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: dump.h,v 1.45 2008/02/16 17:58:01 matt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)dump.h	8.2 (Berkeley) 4/28/95
32  */
33 
34 #include <machine/bswap.h>
35 
36 union dinode {
37 	struct ufs1_dinode dp1;
38 	struct ufs2_dinode dp2;
39 };
40 #define DIP(dp, field) \
41 	(is_ufs2 ? (dp)->dp2.di_##field : (dp)->dp1.di_##field)
42 
43 #define DIP_SET(dp, field, val) do {		\
44 	if (is_ufs2)				\
45 		(dp)->dp2.di_##field = (val);	\
46 	else					\
47 		(dp)->dp1.di_##field = (val);	\
48 } while (0)
49 
50 /*
51  * Filestore-independent UFS data, so code can be more easily shared
52  * between ffs, lfs, and maybe ext2fs and others as well.
53  */
54 struct ufsi {
55 	int64_t ufs_dsize;	/* file system size, in sectors */
56 	int32_t ufs_bsize;	/* block size */
57 	int32_t ufs_bshift;	/* log2(ufs_bsize) */
58 	int32_t ufs_fsize;	/* fragment size */
59 	int32_t ufs_frag;	/* block size / frag size */
60 	int32_t ufs_fsatoda;	/* disk address conversion constant */
61 	int32_t	ufs_nindir;	/* disk addresses per indirect block */
62 	int32_t ufs_inopb;	/* inodes per block */
63 	int32_t ufs_maxsymlinklen; /* max symlink length */
64 	int32_t ufs_bmask;	/* block mask */
65 	int32_t ufs_fmask;	/* frag mask */
66 	int64_t ufs_qbmask;	/* ~ufs_bmask */
67 	int64_t ufs_qfmask;	/* ~ufs_fmask */
68 };
69 #define fsatoda(u,a) ((a) << (u)->ufs_fsatoda)
70 #define ufs_fragroundup(u,size) /* calculates roundup(size, ufs_fsize) */ \
71 	(((size) + (u)->ufs_qfmask) & (u)->ufs_fmask)
72 #define ufs_blkoff(u,loc)   /* calculates (loc % u->ufs_bsize) */ \
73 	((loc) & (u)->ufs_qbmask)
74 #define ufs_dblksize(u,d,b) \
75 	((((b) >= NDADDR || DIP((d), size) >= ((b)+1) << (u)->ufs_bshift \
76 		? (u)->ufs_bsize \
77 		: (ufs_fragroundup((u), ufs_blkoff(u, DIP((d), size)))))))
78 struct ufsi *ufsib;
79 
80 /*
81  * Dump maps used to describe what is to be dumped.
82  */
83 int	mapsize;	/* size of the state maps */
84 char	*usedinomap;	/* map of allocated inodes */
85 char	*dumpdirmap;	/* map of directories to be dumped */
86 char	*dumpinomap;	/* map of files to be dumped */
87 /*
88  * Map manipulation macros.
89  */
90 #define	SETINO(ino, map) \
91 	map[(u_int)((ino) - 1) / NBBY] |=  1 << ((u_int)((ino) - 1) % NBBY)
92 #define	CLRINO(ino, map) \
93 	map[(u_int)((ino) - 1) / NBBY] &=  ~(1 << ((u_int)((ino) - 1) % NBBY))
94 #define	TSTINO(ino, map) \
95 	(map[(u_int)((ino) - 1) / NBBY] &  (1 << ((u_int)((ino) - 1) % NBBY)))
96 
97 /*
98  *	All calculations done in 0.1" units!
99  */
100 char	*disk;		/* name of the disk file */
101 char	*disk_dev;	/* name of the raw device we are dumping */
102 const char *tape;	/* name of the tape file */
103 const char *dumpdates;	/* name of the file containing dump date information*/
104 const char *temp;	/* name of the file for doing rewrite of dumpdates */
105 char	lastlevel;	/* dump level of previous dump */
106 char	level;		/* dump level of this dump */
107 int	uflag;		/* update flag */
108 int	eflag;		/* eject flag */
109 int	lflag;		/* autoload flag */
110 int	diskfd;		/* disk file descriptor */
111 int	tapefd;		/* tape file descriptor */
112 int	pipeout;	/* true => output to standard output */
113 ino_t	curino;		/* current inumber; used globally */
114 int	newtape;	/* new tape flag */
115 u_int64_t	tapesize;	/* estimated tape size, blocks */
116 long	tsize;		/* tape size in 0.1" units */
117 long	asize;		/* number of 0.1" units written on current tape */
118 int	etapes;		/* estimated number of tapes */
119 int	nonodump;	/* if set, do not honor UF_NODUMP user flags */
120 int	unlimited;	/* if set, write to end of medium */
121 
122 extern int	density;	/* density in 0.1" units */
123 extern int	notify;		/* notify operator flag */
124 extern int	timestamp;	/* timestamp messages */
125 extern int	blockswritten;	/* number of blocks written on current tape */
126 extern int	tapeno;		/* current tape number */
127 extern int	is_ufs2;
128 
129 time_t	tstart_writing;	/* when started writing the first tape block */
130 time_t	tstart_volume;	/* when started writing the current volume */
131 int	xferrate;	/* averaged transfer rate of all volumes */
132 char	sblock_buf[MAXBSIZE]; /* buffer to hold the superblock */
133 extern long	dev_bsize;	/* block size of underlying disk device */
134 int	dev_bshift;	/* log2(dev_bsize) */
135 int	tp_bshift;	/* log2(TP_BSIZE) */
136 int needswap;	/* file system in swapped byte order */
137 
138 
139 /* some inline functions to help the byte-swapping mess */
140 static inline u_int16_t iswap16(u_int16_t);
141 static inline u_int32_t iswap32(u_int32_t);
142 static inline u_int64_t iswap64(u_int64_t);
143 
144 static inline u_int16_t iswap16(u_int16_t x)
145 {
146 	if (needswap)
147 		return bswap16(x);
148 	else
149 		return x;
150 }
151 
152 static inline u_int32_t iswap32(u_int32_t x)
153 {
154 	if (needswap)
155 		return bswap32(x);
156 	else
157 		return x;
158 }
159 
160 static inline u_int64_t iswap64(u_int64_t x)
161 {
162 	if (needswap)
163 		return bswap64(x);
164 	else
165 		return x;
166 }
167 
168 /* filestore-specific hooks */
169 int	fs_read_sblock(char *);
170 struct ufsi *fs_parametrize(void);
171 ino_t	fs_maxino(void);
172 void	fs_mapinodes(ino_t, u_int64_t *, int *);
173 
174 /* operator interface functions */
175 void	broadcast(const char *);
176 void	lastdump(char);
177 void	msg(const char *fmt, ...) __attribute__((__format__(__printf__,1,2)));
178 void	msgtail(const char *fmt, ...) __attribute__((__format__(__printf__,1,2)));
179 int	query(const char *);
180 void	quit(const char *fmt, ...) __attribute__((__format__(__printf__,1,2)));
181 time_t	do_stats(void);
182 void	statussig(int);
183 void	timeest(void);
184 time_t	unctime(char *);
185 
186 /* mapping routines */
187 union	dinode;
188 int64_t	blockest(union dinode *);
189 void	mapfileino(ino_t, u_int64_t *, int *);
190 int	mapfiles(ino_t, u_int64_t *, char *, char * const *);
191 int	mapdirs(ino_t, u_int64_t *);
192 
193 /* file dumping routines */
194 void	blksout32(int32_t *, int, ino_t);
195 void	blksout64(int64_t *, int, ino_t);
196 void	dumpino(union dinode *, ino_t);
197 #ifndef RRESTORE
198 void	dumpmap(char *, int, ino_t);
199 #endif
200 void	writeheader(ino_t);
201 
202 /* data block caching */
203 void	bread(daddr_t, char *, int);
204 void	rawread(daddr_t, char *, int);
205 void	initcache(int, int);
206 void	printcachestats(void);
207 
208 /* tape writing routines */
209 int	alloctape(void);
210 void	close_rewind(void);
211 void	dumpblock(daddr_t, int);
212 void	startnewtape(int);
213 void	trewind(int);
214 void	writerec(char *, int);
215 
216 void	Exit(int);
217 void	dumpabort(int);
218 void	getfstab(void);
219 
220 char	*rawname(char *);
221 union	dinode *getino(ino_t);
222 
223 void	*xcalloc(size_t, size_t);
224 void	*xmalloc(size_t);
225 char	*xstrdup(const char *);
226 
227 /* LFS snapshot hooks */
228 #ifdef DUMP_LFS
229 int	lfs_wrap_stop(char *);
230 void	lfs_wrap_go(void);
231 #endif
232 
233 /* rdump routines */
234 #if defined(RDUMP) || defined(RRESTORE)
235 void	rmtclose(void);
236 int	rmthost(const char *);
237 int	rmtopen(const char *, int, int);
238 int	rmtwrite(const char *, int);
239 int	rmtioctl(int, int);
240 #endif /* RDUMP || RRESTORE */
241 
242 void	interrupt(int);	/* in case operator bangs on console */
243 
244 /*
245  *	Exit status codes
246  */
247 #define	X_FINOK		0	/* normal exit */
248 #define	X_STARTUP	1	/* startup error */
249 #define	X_REWRITE	2	/* restart writing from the check point */
250 #define	X_ABORT		3	/* abort dump; don't attempt checkpointing */
251 
252 #define	OPGRENT	"operator"		/* group entry to notify */
253 #define DIALUP	"ttyd"			/* prefix for dialups */
254 
255 struct	fstab *fstabsearch(const char *);	/* search fs_file and fs_spec */
256 struct	statvfs *mntinfosearch(const char *key);
257 
258 #ifndef NAME_MAX
259 #define NAME_MAX 255
260 #endif
261 
262 /*
263  *	The contents of the file _PATH_DUMPDATES is maintained both on
264  *	a linked list, and then (eventually) arrayified.
265  */
266 struct dumpdates {
267 	char	dd_name[NAME_MAX+3];
268 	char	dd_level;
269 	time_t	dd_ddate;
270 };
271 
272 extern int	nddates;		/* number of records (might be zero) */
273 extern struct	dumpdates **ddatev;	/* the arrayfied version */
274 
275 void	initdumptimes(void);
276 void	getdumptime(void);
277 void	putdumptime(void);
278 #define	ITITERATE(i, ddp) \
279 	if (ddatev != NULL) \
280 		for (ddp = ddatev[i = 0]; i < nddates; ddp = ddatev[++i])
281 
282 void	sig(int signo);
283 
284 /*
285  * Compatibility with old systems.
286  */
287 #ifdef COMPAT
288 #include <sys/file.h>
289 #define	strchr(a,b)	index(a,b)
290 #define	strrchr(a,b)	rindex(a,b)
291 extern char *strdup(), *ctime();
292 extern int read(), write();
293 extern int errno;
294 #endif
295 
296 #ifndef	_PATH_FSTAB
297 #define	_PATH_FSTAB	"/etc/fstab"
298 #endif
299