dump.h revision 1.56 1 /* $NetBSD: dump.h,v 1.56 2019/03/01 16:42:11 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
42 union 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 */
64 struct 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)))))))
88 struct ufsi *ufsib;
89
90 /*
91 * Dump maps used to describe what is to be dumped.
92 */
93 int mapsize; /* size of the state maps */
94 char *usedinomap; /* map of allocated inodes */
95 char *dumpdirmap; /* map of directories to be dumped */
96 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 */
110 char *disk; /* name of the disk file */
111 char *disk_dev; /* name of the raw device we are dumping */
112 const char *tape; /* name of the tape file */
113 const char *dumpdates; /* name of the file containing dump date information*/
114 const char *temp; /* name of the file for doing rewrite of dumpdates */
115 char lastlevel; /* dump level of previous dump */
116 char level; /* dump level of this dump */
117 int uflag; /* update flag */
118 int eflag; /* eject flag */
119 int lflag; /* autoload flag */
120 int diskfd; /* disk file descriptor */
121 int tapefd; /* tape file descriptor */
122 int pipeout; /* true => output to standard output */
123 int trueinc; /* true => "true incremental", i.e use last 9 as ref */
124 ino_t curino; /* current inumber; used globally */
125 int newtape; /* new tape flag */
126 u_int64_t tapesize; /* estimated tape size, blocks */
127 long tsize; /* tape size in 0.1" units */
128 long asize; /* number of 0.1" units written on current tape */
129 int etapes; /* estimated number of tapes */
130 int nonodump; /* if set, do not honor UF_NODUMP user flags */
131 int unlimited; /* if set, write to end of medium */
132
133 extern int density; /* density in 0.1" units */
134 extern int notify; /* notify operator flag */
135 extern int timestamp; /* timestamp messages */
136 extern int blockswritten; /* number of blocks written on current tape */
137 extern int tapeno; /* current tape number */
138 extern int is_ufs2;
139
140 time_t tstart_writing; /* when started writing the first tape block */
141 time_t tstart_volume; /* when started writing the current volume */
142 int xferrate; /* averaged transfer rate of all volumes */
143 char sblock_buf[MAXBSIZE]; /* buffer to hold the superblock */
144 extern long dev_bsize; /* block size of underlying disk device */
145 int dev_bshift; /* log2(dev_bsize) */
146 int tp_bshift; /* log2(TP_BSIZE) */
147 int needswap; /* file system in swapped byte order */
148
149
150 /* some inline functions to help the byte-swapping mess */
151 static inline u_int16_t iswap16(u_int16_t);
152 static inline u_int32_t iswap32(u_int32_t);
153 static inline u_int64_t iswap64(u_int64_t);
154
155 static inline u_int16_t iswap16(u_int16_t x)
156 {
157 if (needswap)
158 return bswap16(x);
159 else
160 return x;
161 }
162
163 static inline u_int32_t iswap32(u_int32_t x)
164 {
165 if (needswap)
166 return bswap32(x);
167 else
168 return x;
169 }
170
171 static inline u_int64_t iswap64(u_int64_t x)
172 {
173 if (needswap)
174 return bswap64(x);
175 else
176 return x;
177 }
178
179 /* filestore-specific hooks */
180 int fs_read_sblock(char *);
181 struct ufsi *fs_parametrize(void);
182 ino_t fs_maxino(void);
183 void fs_mapinodes(ino_t, u_int64_t *, int *);
184
185 /* operator interface functions */
186 void broadcast(const char *);
187 void lastdump(char);
188 void msg(const char *, ...) __printflike(1, 2);
189 void msgtail(const char *, ...) __printflike(1, 2);
190 int query(const char *);
191 void quit(const char *, ...) __printflike(1, 2);
192 void quite(int, const char *, ...) __printflike(2, 3);
193 time_t do_stats(void);
194 void statussig(int);
195 void timeest(void);
196 time_t unctime(const char *);
197
198 /* mapping routines */
199 union dinode;
200 int64_t blockest(union dinode *);
201 void mapfileino(ino_t, u_int64_t *, int *);
202 int mapfiles(ino_t, u_int64_t *, char *, char * const *);
203 int mapdirs(ino_t, u_int64_t *);
204
205 /* file dumping routines */
206 void blksout32(int32_t *, int, ino_t);
207 void blksout64(int64_t *, int, ino_t);
208 void dumpino(union dinode *, ino_t);
209 #ifndef RRESTORE
210 void dumpmap(char *, int, ino_t);
211 #endif
212 void writeheader(ino_t);
213
214 /* data block caching */
215 void bread(daddr_t, char *, int);
216 void rawread(daddr_t, char *, int);
217 void initcache(int, int);
218 void printcachestats(void);
219
220 /* tape writing routines */
221 int alloctape(void);
222 void close_rewind(void);
223 void dumpblock(daddr_t, int);
224 void startnewtape(int);
225 void trewind(int);
226 void writerec(const char *, int);
227
228 void Exit(int) __dead;
229 void dumpabort(int);
230 void getfstab(void);
231
232 char *rawname(char *);
233 union dinode *getino(ino_t);
234
235 void *xcalloc(size_t, size_t);
236 void *xmalloc(size_t);
237 char *xstrdup(const char *);
238
239 /* LFS snapshot hooks */
240 #ifdef DUMP_LFS
241 int lfs_wrap_stop(char *);
242 void lfs_wrap_go(void);
243 #endif
244
245 /* rdump routines */
246 #if defined(RDUMP) || defined(RRESTORE)
247 void rmtclose(void);
248 int rmthost(const char *);
249 int rmtopen(const char *, int, int);
250 int rmtwrite(const char *, int);
251 int rmtioctl(int, int);
252 #endif /* RDUMP || RRESTORE */
253
254 void interrupt(int); /* in case operator bangs on console */
255
256 /*
257 * Exit status codes
258 */
259 #define X_FINOK 0 /* normal exit */
260 #define X_STARTUP 1 /* startup error */
261 #define X_REWRITE 2 /* restart writing from the check point */
262 #define X_ABORT 3 /* abort dump; don't attempt checkpointing */
263
264 #define OPGRENT "operator" /* group entry to notify */
265 #define DIALUP "ttyd" /* prefix for dialups */
266
267 struct fstab *fstabsearch(const char *); /* search fs_file and fs_spec */
268 struct statvfs *mntinfosearch(const char *key);
269
270 #ifndef NAME_MAX
271 #define NAME_MAX 511
272 #endif
273
274 /*
275 * The contents of the file _PATH_DUMPDATES is maintained both on
276 * a linked list, and then (eventually) arrayified.
277 */
278 struct dumpdates {
279 /* see DUMP{IN,OUT}FMT in <protocols/dumprestore.h> */
280 char dd_name[NAME_MAX+3];
281 char dd_level;
282 time_t dd_ddate;
283 };
284
285 extern int nddates; /* number of records (might be zero) */
286 extern struct dumpdates **ddatev; /* the arrayfied version */
287
288 void initdumptimes(void);
289 void getdumptime(void);
290 void putdumptime(void);
291 #define ITITERATE(i, ddp) \
292 if (ddatev != NULL) \
293 for (ddp = ddatev[i = 0]; i < nddates; ddp = ddatev[++i])
294
295 void sig(int signo);
296
297 #ifndef _PATH_FSTAB
298 #define _PATH_FSTAB "/etc/fstab"
299 #endif
300