1/*	$NetBSD: dump.h,v 1.60 2021/06/19 13:56:34 christos 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#ifdef DUMP_LFS
36#include <ufs/lfs/lfs.h>
37#include <ufs/lfs/lfs_accessors.h>
38#endif
39#include <ufs/ufs/dinode.h>
40#include <protocols/dumprestore.h>
41
42union dinode {
43	struct ufs1_dinode dp1;
44	struct ufs2_dinode dp2;
45#ifdef DUMP_LFS
46	struct lfs32_dinode dlp32;
47	struct lfs64_dinode dlp64;
48#endif
49};
50#define DIP(dp, field) \
51	(is_ufs2 ? (dp)->dp2.di_##field : (dp)->dp1.di_##field)
52
53#define DIP_SET(dp, field, val) do {		\
54	if (is_ufs2)				\
55		(dp)->dp2.di_##field = (val);	\
56	else					\
57		(dp)->dp1.di_##field = (val);	\
58} while (0)
59
60/*
61 * Filestore-independent UFS data, so code can be more easily shared
62 * between ffs, lfs, and maybe ext2fs and others as well.
63 */
64struct ufsi {
65	int64_t ufs_dsize;	/* file system size, in sectors */
66	int32_t ufs_bsize;	/* block size */
67	int32_t ufs_bshift;	/* log2(ufs_bsize) */
68	int32_t ufs_fsize;	/* fragment size */
69	int32_t ufs_frag;	/* block size / frag size */
70	int32_t ufs_fsatoda;	/* disk address conversion constant */
71	int32_t	ufs_nindir;	/* disk addresses per indirect block */
72	int32_t ufs_inopb;	/* inodes per block */
73	int32_t ufs_maxsymlinklen; /* max symlink length */
74	int32_t ufs_bmask;	/* block mask */
75	int32_t ufs_fmask;	/* frag mask */
76	int64_t ufs_qbmask;	/* ~ufs_bmask */
77	int64_t ufs_qfmask;	/* ~ufs_fmask */
78};
79#define fsatoda(u,a) ((a) << (u)->ufs_fsatoda)
80#define ufs_fragroundup(u,size) /* calculates roundup(size, ufs_fsize) */ \
81	(((size) + (u)->ufs_qfmask) & (u)->ufs_fmask)
82#define ufs_blkoff(u,loc)   /* calculates (loc % u->ufs_bsize) */ \
83	((loc) & (u)->ufs_qbmask)
84#define ufs_dblksize(u,d,b) \
85	((((b) >= UFS_NDADDR || DIP((d), size) >= ((b)+1) << (u)->ufs_bshift \
86		? (u)->ufs_bsize \
87		: (ufs_fragroundup((u), ufs_blkoff(u, DIP((d), size)))))))
88extern struct ufsi *ufsib;
89
90/*
91 * Dump maps used to describe what is to be dumped.
92 */
93extern int	mapsize;	/* size of the state maps */
94extern char	*usedinomap;	/* map of allocated inodes */
95extern char	*dumpdirmap;	/* map of directories to be dumped */
96extern char	*dumpinomap;	/* map of files to be dumped */
97/*
98 * Map manipulation macros.
99 */
100#define	SETINO(ino, map) \
101	map[(u_int)((ino) - 1) / NBBY] |=  1 << ((u_int)((ino) - 1) % NBBY)
102#define	CLRINO(ino, map) \
103	map[(u_int)((ino) - 1) / NBBY] &=  ~(1 << ((u_int)((ino) - 1) % NBBY))
104#define	TSTINO(ino, map) \
105	(map[(u_int)((ino) - 1) / NBBY] &  (1 << ((u_int)((ino) - 1) % NBBY)))
106
107/*
108 *	All calculations done in 0.1" units!
109 */
110extern char	*disk;		/* name of the disk file */
111extern char	*disk_dev;	/* name of the raw device we are dumping */
112extern const char *tape;	/* name of the tape file */
113extern const char *dumpdates;	/* name of the file containing dump date information*/
114extern const char *temp;	/* name of the file for doing rewrite of dumpdates */
115extern char	lastlevel;	/* dump level of previous dump */
116extern char	level;		/* dump level of this dump */
117extern int	uflag;		/* update flag */
118extern const char *dumpdev;	/* device name in dumpdates */
119extern int	eflag;		/* eject flag */
120extern int	lflag;		/* autoload flag */
121extern int	diskfd;		/* disk file descriptor */
122extern int	tapefd;		/* tape file descriptor */
123extern int	pipeout;	/* true => output to standard output */
124extern int	trueinc;	/* true => "true incremental", i.e use last 9 as ref */
125extern ino_t	curino;		/* current inumber; used globally */
126extern int	newtape;	/* new tape flag */
127extern u_int64_t	tapesize;	/* estimated tape size, blocks */
128extern long	tsize;		/* tape size in 0.1" units */
129extern long	asize;		/* number of 0.1" units written on current tape */
130extern int	etapes;		/* estimated number of tapes */
131extern int	nonodump;	/* if set, do not honor UF_NODUMP user flags */
132extern int	unlimited;	/* if set, write to end of medium */
133
134extern int	density;	/* density in 0.1" units */
135extern int	notify;		/* notify operator flag */
136extern int	timestamp;	/* timestamp messages */
137extern u_int64_t	blockswritten;	/* blocks written on current tape */
138extern int	tapeno;		/* current tape number */
139extern int	is_ufs2;
140
141extern time_t	tstart_writing;	/* when started writing the first tape block */
142extern time_t	tstart_volume;	/* when started writing the current volume */
143extern int	xferrate;	/* averaged transfer rate of all volumes */
144extern char	sblock_buf[MAXBSIZE]; /* buffer to hold the superblock */
145extern long	dev_bsize;	/* block size of underlying disk device */
146extern int	dev_bshift;	/* log2(dev_bsize) */
147extern int	tp_bshift;	/* log2(TP_BSIZE) */
148extern int needswap;	/* file system in swapped byte order */
149
150
151/* some inline functions to help the byte-swapping mess */
152static inline u_int16_t iswap16(u_int16_t);
153static inline u_int32_t iswap32(u_int32_t);
154static inline u_int64_t iswap64(u_int64_t);
155
156static inline u_int16_t iswap16(u_int16_t x)
157{
158	if (needswap)
159		return bswap16(x);
160	else
161		return x;
162}
163
164static inline u_int32_t iswap32(u_int32_t x)
165{
166	if (needswap)
167		return bswap32(x);
168	else
169		return x;
170}
171
172static inline u_int64_t iswap64(u_int64_t x)
173{
174	if (needswap)
175		return bswap64(x);
176	else
177		return x;
178}
179
180/* filestore-specific hooks */
181int	fs_read_sblock(char *);
182struct ufsi *fs_parametrize(void);
183ino_t	fs_maxino(void);
184void	fs_mapinodes(ino_t, u_int64_t *, int *);
185
186/* operator interface functions */
187void	broadcast(const char *);
188void	lastdump(char);
189void	msg(const char *, ...) __printflike(1, 2);
190void	msgtail(const char *, ...) __printflike(1, 2);
191int	query(const char *);
192void	quit(const char *, ...) __printflike(1, 2);
193void	quite(int, const char *, ...) __printflike(2, 3);
194time_t	do_stats(void);
195void	statussig(int);
196void	timeest(void);
197time_t	unctime(const char *);
198
199/* mapping routines */
200union	dinode;
201int64_t	blockest(union dinode *);
202void	mapfileino(ino_t, u_int64_t *, int *);
203int	mapfiles(ino_t, u_int64_t *, char *, char * const *);
204int	mapdirs(ino_t, u_int64_t *);
205
206/* file dumping routines */
207void	blksout32(int32_t *, int, ino_t);
208void	blksout64(union dinode *, int64_t *, int, ino_t, int);
209void	dumpino(union dinode *, ino_t);
210#ifndef RRESTORE
211void	dumpmap(char *, int, ino_t);
212#endif
213void	writeheader(ino_t);
214
215/* data block caching */
216void	bread(daddr_t, char *, int);
217void	rawread(daddr_t, char *, int);
218void	initcache(int, int);
219void	printcachestats(void);
220
221/* tape writing routines */
222int	alloctape(void);
223void	close_rewind(void);
224void	dumpblock(daddr_t, int);
225void	startnewtape(int);
226void	trewind(int);
227void	writerec(const char *, int);
228
229void	Exit(int) __dead;
230void	dumpabort(int);
231void	getfstab(void);
232
233char	*rawname(char *);
234union	dinode *getino(ino_t);
235
236void	*xcalloc(size_t, size_t);
237void	*xmalloc(size_t);
238char	*xstrdup(const char *);
239
240/* LFS snapshot hooks */
241#ifdef DUMP_LFS
242int	lfs_wrap_stop(char *);
243void	lfs_wrap_go(void);
244#endif
245
246/* rdump routines */
247#if defined(RDUMP) || defined(RRESTORE)
248void	rmtclose(void);
249int	rmthost(const char *);
250int	rmtopen(const char *, int, int);
251int	rmtwrite(const char *, int);
252int	rmtioctl(int, int);
253#endif /* RDUMP || RRESTORE */
254
255void	interrupt(int);	/* in case operator bangs on console */
256
257/*
258 *	Exit status codes
259 */
260#define	X_FINOK		0	/* normal exit */
261#define	X_STARTUP	1	/* startup error */
262#define	X_REWRITE	2	/* restart writing from the check point */
263#define	X_ABORT		3	/* abort dump; don't attempt checkpointing */
264
265#define	OPGRENT	"operator"		/* group entry to notify */
266#define DIALUP	"ttyd"			/* prefix for dialups */
267
268struct	fstab *fstabsearch(const char *);	/* search fs_file and fs_spec */
269struct	statvfs *mntinfosearch(const char *key);
270
271#ifndef NAME_MAX
272#define NAME_MAX 511
273#endif
274
275/*
276 *	The contents of the file _PATH_DUMPDATES is maintained both on
277 *	a linked list, and then (eventually) arrayified.
278 */
279struct dumpdates {
280	/* see DUMP{IN,OUT}FMT in <protocols/dumprestore.h> */
281	char	dd_name[NAME_MAX+3];
282	char	dd_level;
283	time_t	dd_ddate;
284};
285
286extern int	nddates;		/* number of records (might be zero) */
287extern struct	dumpdates **ddatev;	/* the arrayfied version */
288
289void	initdumptimes(void);
290void	getdumptime(void);
291void	putdumptime(void);
292#define	ITITERATE(i, ddp) \
293	if (ddatev != NULL) \
294		for (ddp = ddatev[i = 0]; i < nddates; ddp = ddatev[++i])
295
296void	sig(int signo);
297
298#ifndef	_PATH_FSTAB
299#define	_PATH_FSTAB	"/etc/fstab"
300#endif
301