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