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