]>
Commit | Line | Data |
---|---|---|
1227625a SP |
1 | /* |
2 | * Ported to Linux's Second Extended File System as part of the | |
3 | * dump and restore backup suit | |
b45f51d6 | 4 | * Remy Card <card@Linux.EU.Org>, 1994-1997 |
ebcbe7f6 | 5 | * Stelian Pop <pop@cybercable.fr>, 1999-2000 |
1227625a SP |
6 | */ |
7 | ||
8 | /*- | |
9 | * Copyright (c) 1980, 1988, 1991, 1993 | |
10 | * The Regents of the University of California. All rights reserved. | |
11 | * | |
12 | * Redistribution and use in source and binary forms, with or without | |
13 | * modification, are permitted provided that the following conditions | |
14 | * are met: | |
15 | * 1. Redistributions of source code must retain the above copyright | |
16 | * notice, this list of conditions and the following disclaimer. | |
17 | * 2. Redistributions in binary form must reproduce the above copyright | |
18 | * notice, this list of conditions and the following disclaimer in the | |
19 | * documentation and/or other materials provided with the distribution. | |
20 | * 3. All advertising materials mentioning features or use of this software | |
21 | * must display the following acknowledgement: | |
22 | * This product includes software developed by the University of | |
23 | * California, Berkeley and its contributors. | |
24 | * 4. Neither the name of the University nor the names of its contributors | |
25 | * may be used to endorse or promote products derived from this software | |
26 | * without specific prior written permission. | |
27 | * | |
28 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
29 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
30 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
31 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
32 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
36 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
37 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
38 | * SUCH DAMAGE. | |
39 | */ | |
40 | ||
df9ae507 SP |
41 | #ifndef lint |
42 | static const char rcsid[] = | |
95cd8801 | 43 | "$Id: traverse.c,v 1.21 2000/11/10 13:52:43 stelian Exp $"; |
df9ae507 SP |
44 | #endif /* not lint */ |
45 | ||
1227625a | 46 | #include <sys/param.h> |
1227625a SP |
47 | #include <sys/stat.h> |
48 | #ifdef __linux__ | |
49 | #include <linux/ext2_fs.h> | |
50 | #include <bsdcompat.h> | |
ddd2ef55 | 51 | #include <compaterr.h> |
1227625a | 52 | #include <stdlib.h> |
b45f51d6 | 53 | #define swab32(x) ext2fs_swab32(x) |
1227625a | 54 | #else /* __linux__ */ |
ddd2ef55 | 55 | #define swab32(x) x |
1227625a SP |
56 | #ifdef sunos |
57 | #include <sys/vnode.h> | |
58 | ||
59 | #include <ufs/fs.h> | |
60 | #include <ufs/fsdir.h> | |
61 | #include <ufs/inode.h> | |
62 | #else | |
63 | #include <ufs/ufs/dir.h> | |
64 | #include <ufs/ufs/dinode.h> | |
65 | #include <ufs/ffs/fs.h> | |
66 | #endif | |
67 | #endif /* __linux__ */ | |
68 | ||
69 | #include <protocols/dumprestore.h> | |
70 | ||
71 | #include <ctype.h> | |
72 | #include <stdio.h> | |
73 | #ifdef __STDC__ | |
74 | #include <string.h> | |
75 | #include <unistd.h> | |
76 | #endif | |
77 | ||
78 | #ifdef __linux__ | |
79 | #include <ext2fs/ext2fs.h> | |
80 | #endif | |
81 | ||
82 | #include "dump.h" | |
83 | ||
84 | #define HASDUMPEDFILE 0x1 | |
85 | #define HASSUBDIRS 0x2 | |
86 | ||
abc63c56 SP |
87 | #ifdef __linux__ |
88 | typedef u_quad_t fsizeT; | |
89 | #else | |
1227625a SP |
90 | #ifdef FS_44INODEFMT |
91 | typedef quad_t fsizeT; | |
92 | #else | |
93 | typedef long fsizeT; | |
94 | #endif | |
abc63c56 | 95 | #endif |
1227625a | 96 | |
1227625a SP |
97 | #ifdef __linux__ |
98 | static int searchdir __P((struct ext2_dir_entry *dp, int offset, | |
99 | int blocksize, char *buf, void *private)); | |
100 | #else | |
b45f51d6 SP |
101 | static int dirindir __P((ino_t ino, daddr_t blkno, int level, long *size)); |
102 | static void dmpindir __P((ino_t ino, daddr_t blk, int level, fsizeT *size)); | |
1227625a SP |
103 | static int searchdir __P((ino_t ino, daddr_t blkno, long size, long filesize)); |
104 | #endif | |
4267a8ad | 105 | static void mapfileino __P((ino_t ino, long *tapesize, int *dirskipped)); |
1cd75c4c | 106 | static int exclude_ino __P((ino_t ino)); |
1227625a | 107 | |
95cd8801 SP |
108 | /* #define EXT3_FEATURE_INCOMPAT_RECOVER 0x0004 */ |
109 | #ifdef EXT3_FEATURE_INCOMPAT_RECOVER | |
110 | #define FORCE_OPEN EXT2_FLAG_FORCE | |
111 | #else | |
112 | #define FORCE_OPEN 0 | |
113 | #endif | |
c930abff SP |
114 | |
115 | int dump_fs_open(const char *disk, ext2_filsys *fs) | |
116 | { | |
117 | int retval; | |
118 | struct ext2fs_sb *s; | |
119 | ||
95cd8801 | 120 | retval = ext2fs_open(disk, FORCE_OPEN, 0, 0, unix_io_manager, fs); |
a110deb5 | 121 | #if defined(EXT2_LIB_FEATURE_COMPAT_SUPP) && defined(EXT2_LIB_FEATURE_INCOMPAT_SUPP) && defined(EXT2_LIB_FEATURE_RO_COMPAT_SUPP) && defined(EXT2_ET_UNSUPP_FEATURE) && defined(EXT2_ET_RO_UNSUPP_FEATURE) |
c930abff SP |
122 | if (!retval) { |
123 | s = (struct ext2fs_sb *) (*fs)->super; | |
124 | if ((s->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) || | |
125 | #ifdef EXT3_FEATURE_INCOMPAT_RECOVER | |
126 | (s->s_feature_incompat & ~(EXT3_FEATURE_INCOMPAT_RECOVER | EXT2_LIB_FEATURE_INCOMPAT_SUPP))) { | |
127 | #else | |
128 | (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) { | |
129 | #endif | |
130 | retval = EXT2_ET_UNSUPP_FEATURE; | |
131 | } | |
132 | else if (s->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) { | |
133 | retval = EXT2_ET_RO_UNSUPP_FEATURE; | |
134 | } | |
135 | } | |
a110deb5 | 136 | #endif /* defined && defined && defined... */ |
c930abff SP |
137 | return retval; |
138 | } | |
139 | ||
1227625a SP |
140 | /* |
141 | * This is an estimation of the number of TP_BSIZE blocks in the file. | |
142 | * It estimates the number of blocks in files with holes by assuming | |
143 | * that all of the blocks accounted for by di_blocks are data blocks | |
144 | * (when some of the blocks are usually used for indirect pointers); | |
145 | * hence the estimate may be high. | |
146 | */ | |
147 | long | |
ddd2ef55 | 148 | blockest(struct dinode *dp) |
1227625a SP |
149 | { |
150 | long blkest, sizeest; | |
151 | ||
152 | /* | |
153 | * dp->di_size is the size of the file in bytes. | |
154 | * dp->di_blocks stores the number of sectors actually in the file. | |
155 | * If there are more sectors than the size would indicate, this just | |
156 | * means that there are indirect blocks in the file or unused | |
157 | * sectors in the last file block; we can safely ignore these | |
158 | * (blkest = sizeest below). | |
159 | * If the file is bigger than the number of sectors would indicate, | |
160 | * then the file has holes in it. In this case we must use the | |
161 | * block count to estimate the number of data blocks used, but | |
162 | * we use the actual size for estimating the number of indirect | |
163 | * dump blocks (sizeest vs. blkest in the indirect block | |
164 | * calculation). | |
165 | */ | |
166 | blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE); | |
167 | sizeest = howmany(dp->di_size, TP_BSIZE); | |
168 | if (blkest > sizeest) | |
169 | blkest = sizeest; | |
170 | #ifdef __linux__ | |
171 | if (dp->di_size > fs->blocksize * NDADDR) { | |
172 | /* calculate the number of indirect blocks on the dump tape */ | |
173 | blkest += | |
174 | howmany(sizeest - NDADDR * fs->blocksize / TP_BSIZE, | |
a0d8fe2d | 175 | NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super)); |
1227625a SP |
176 | } |
177 | #else | |
178 | if (dp->di_size > sblock->fs_bsize * NDADDR) { | |
179 | /* calculate the number of indirect blocks on the dump tape */ | |
180 | blkest += | |
181 | howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE, | |
182 | TP_NINDIR); | |
183 | } | |
184 | #endif | |
185 | return (blkest + 1); | |
186 | } | |
187 | ||
1cd75c4c SP |
188 | extern ino_t iexclude_list[IEXCLUDE_MAXNUM]; /* the inode exclude list */ |
189 | extern int iexclude_num; /* number of elements in the list */ | |
190 | ||
191 | /* | |
192 | * This tests whether an inode is in the exclude list | |
193 | */ | |
194 | int | |
195 | exclude_ino(ino_t ino) | |
196 | { | |
197 | /* 04-Feb-00 ILC */ | |
198 | if (iexclude_num) { /* if there are inodes in the exclude list */ | |
199 | int idx; /* then check this inode against it */ | |
200 | for (idx = 0; idx < iexclude_num; idx++) | |
201 | if (ino == iexclude_list[idx]) | |
202 | return 1; | |
203 | } | |
204 | return 0; | |
205 | } | |
206 | ||
1227625a SP |
207 | /* Auxiliary macro to pick up files changed since previous dump. */ |
208 | #define CHANGEDSINCE(dp, t) \ | |
209 | ((dp)->di_mtime >= (t) || (dp)->di_ctime >= (t)) | |
210 | ||
1cd75c4c | 211 | /* The NODUMP_FLAG macro tests if a file has the nodump flag. */ |
1227625a | 212 | #ifdef UF_NODUMP |
1cd75c4c | 213 | #define NODUMP_FLAG(dp) (!nonodump && (((dp)->di_flags & UF_NODUMP) == UF_NODUMP)) |
1227625a | 214 | #else |
1cd75c4c | 215 | #define NODUMP_FLAG(dp) 0 |
1227625a SP |
216 | #endif |
217 | ||
1cd75c4c SP |
218 | /* The WANTTODUMP macro decides whether a file should be dumped. */ |
219 | #define WANTTODUMP(dp, ino) \ | |
220 | (CHANGEDSINCE(dp, spcl.c_ddate) && \ | |
221 | (!NODUMP_FLAG(dp)) && \ | |
222 | (!exclude_ino(ino))) | |
20c345aa | 223 | |
4267a8ad SP |
224 | /* |
225 | * Determine if given inode should be dumped | |
226 | */ | |
227 | void | |
228 | mapfileino(ino_t ino, long *tapesize, int *dirskipped) | |
229 | { | |
230 | register int mode; | |
231 | register struct dinode *dp; | |
232 | ||
233 | /* | |
234 | * Skip inode if we've already marked it for dumping | |
235 | */ | |
236 | if (TSTINO(ino, usedinomap)) | |
237 | return; | |
238 | dp = getino(ino); | |
239 | if ((mode = (dp->di_mode & IFMT)) == 0) | |
240 | return; | |
241 | #ifdef __linux__ | |
242 | if (dp->di_nlink == 0 || dp->di_dtime != 0) | |
243 | return; | |
244 | #endif | |
245 | /* | |
246 | * Put all dirs in dumpdirmap, inodes that are to be dumped in the | |
247 | * used map. All inode but dirs who have the nodump attribute go | |
248 | * to the usedinomap. | |
249 | */ | |
250 | SETINO(ino, usedinomap); | |
20c345aa | 251 | |
4267a8ad SP |
252 | if (mode == IFDIR) |
253 | SETINO(ino, dumpdirmap); | |
1cd75c4c | 254 | if (WANTTODUMP(dp, ino)) { |
4267a8ad SP |
255 | SETINO(ino, dumpinomap); |
256 | if (mode != IFREG && mode != IFDIR && mode != IFLNK) | |
257 | *tapesize += 1; | |
258 | else | |
259 | *tapesize += blockest(dp); | |
260 | return; | |
261 | } | |
262 | if (mode == IFDIR) { | |
1cd75c4c | 263 | if ( NODUMP_FLAG(dp) || exclude_ino(ino) ) |
4267a8ad | 264 | CLRINO(ino, usedinomap); |
4267a8ad SP |
265 | *dirskipped = 1; |
266 | } | |
267 | } | |
268 | ||
1227625a SP |
269 | /* |
270 | * Dump pass 1. | |
271 | * | |
272 | * Walk the inode list for a filesystem to find all allocated inodes | |
273 | * that have been modified since the previous dump time. Also, find all | |
274 | * the directories in the filesystem. | |
275 | */ | |
276 | int | |
ddd2ef55 | 277 | mapfiles(ino_t maxino, long *tapesize) |
1227625a | 278 | { |
1227625a | 279 | register ino_t ino; |
1227625a SP |
280 | int anydirskipped = 0; |
281 | ||
4267a8ad SP |
282 | for (ino = ROOTINO; ino < maxino; ino++) |
283 | mapfileino(ino, tapesize, &anydirskipped); | |
284 | ||
1227625a SP |
285 | /* |
286 | * Restore gets very upset if the root is not dumped, | |
287 | * so ensure that it always is dumped. | |
288 | */ | |
289 | SETINO(ROOTINO, dumpinomap); | |
290 | return (anydirskipped); | |
291 | } | |
292 | ||
293 | #ifdef __linux__ | |
294 | struct mapfile_context { | |
295 | long *tapesize; | |
4267a8ad | 296 | int *anydirskipped; |
1227625a SP |
297 | }; |
298 | ||
299 | static int | |
ddd2ef55 | 300 | mapfilesindir(struct ext2_dir_entry *dirent, int offset, int blocksize, char *buf, void *private) |
1227625a SP |
301 | { |
302 | register struct dinode *dp; | |
303 | register int mode; | |
1227625a SP |
304 | errcode_t retval; |
305 | struct mapfile_context *mfc; | |
4267a8ad | 306 | ino_t ino; |
1227625a SP |
307 | |
308 | ino = dirent->inode; | |
309 | mfc = (struct mapfile_context *)private; | |
4267a8ad SP |
310 | |
311 | mapfileino(dirent->inode, mfc->tapesize, mfc->anydirskipped); | |
312 | ||
313 | dp = getino(dirent->inode); | |
314 | mode = dp->di_mode & IFMT; | |
315 | if (mode == IFDIR && dp->di_nlink != 0 && dp->di_dtime == 0) { | |
f346e363 | 316 | if ((dirent->name[0] != '.' || ( dirent->name_len & 0xFF ) != 1) && |
4267a8ad | 317 | (dirent->name[0] != '.' || dirent->name[1] != '.' || |
f346e363 | 318 | ( dirent->name_len & 0xFF ) != 2)) { |
4267a8ad SP |
319 | retval = ext2fs_dir_iterate(fs, ino, 0, NULL, |
320 | mapfilesindir, private); | |
321 | if (retval) | |
322 | return retval; | |
1227625a SP |
323 | } |
324 | } | |
325 | return 0; | |
326 | } | |
327 | ||
328 | /* | |
329 | * Dump pass 1. | |
330 | * | |
331 | * Walk the inode list for a filesystem to find all allocated inodes | |
332 | * that have been modified since the previous dump time. Also, find all | |
333 | * the directories in the filesystem. | |
334 | */ | |
335 | int | |
ddd2ef55 | 336 | mapfilesfromdir(ino_t maxino, long *tapesize, char *directory) |
1227625a SP |
337 | { |
338 | errcode_t retval; | |
339 | struct mapfile_context mfc; | |
340 | ino_t dir_ino; | |
341 | char dir_name [MAXPATHLEN]; | |
4267a8ad | 342 | int i, anydirskipped = 0; |
1227625a SP |
343 | |
344 | /* | |
345 | * Mark every directory in the path as being dumped | |
346 | */ | |
347 | for (i = 0; i < strlen (directory); i++) { | |
348 | if (directory[i] == '/') { | |
349 | strncpy (dir_name, directory, i); | |
350 | dir_name[i] = '\0'; | |
351 | retval = ext2fs_namei(fs, ROOTINO, ROOTINO, dir_name, | |
352 | &dir_ino); | |
353 | if (retval) { | |
354 | com_err(disk, retval, "while translating %s", | |
355 | dir_name); | |
356 | exit(X_ABORT); | |
357 | } | |
4267a8ad | 358 | mapfileino(dir_ino, tapesize, &anydirskipped); |
1227625a SP |
359 | } |
360 | } | |
361 | /* | |
362 | * Mark the final directory | |
363 | */ | |
364 | retval = ext2fs_namei(fs, ROOTINO, ROOTINO, directory, &dir_ino); | |
365 | if (retval) { | |
366 | com_err(disk, retval, "while translating %s", directory); | |
367 | exit(X_ABORT); | |
368 | } | |
4267a8ad | 369 | mapfileino(dir_ino, tapesize, &anydirskipped); |
1227625a SP |
370 | |
371 | mfc.tapesize = tapesize; | |
4267a8ad | 372 | mfc.anydirskipped = &anydirskipped; |
1227625a SP |
373 | retval = ext2fs_dir_iterate(fs, dir_ino, 0, NULL, mapfilesindir, |
374 | (void *)&mfc); | |
375 | ||
376 | if (retval) { | |
377 | com_err(disk, retval, "while mapping files in %s", directory); | |
378 | exit(X_ABORT); | |
379 | } | |
4267a8ad SP |
380 | /* |
381 | * Ensure that the root inode actually appears in the file list | |
382 | * for a subdir | |
383 | */ | |
384 | mapfileino(ROOTINO, tapesize, &anydirskipped); | |
1227625a SP |
385 | /* |
386 | * Restore gets very upset if the root is not dumped, | |
387 | * so ensure that it always is dumped. | |
388 | */ | |
4267a8ad SP |
389 | SETINO(ROOTINO, dumpinomap); |
390 | return anydirskipped; | |
1227625a SP |
391 | } |
392 | #endif | |
393 | ||
4267a8ad SP |
394 | #ifdef __linux__ |
395 | struct mapdirs_context { | |
396 | int *ret; | |
397 | int nodump; | |
398 | long *tapesize; | |
399 | }; | |
400 | #endif | |
401 | ||
1227625a SP |
402 | /* |
403 | * Dump pass 2. | |
404 | * | |
405 | * Scan each directory on the filesystem to see if it has any modified | |
406 | * files in it. If it does, and has not already been added to the dump | |
407 | * list (because it was itself modified), then add it. If a directory | |
408 | * has not been modified itself, contains no modified files and has no | |
409 | * subdirectories, then it can be deleted from the dump list and from | |
410 | * the list of directories. By deleting it from the list of directories, | |
411 | * its parent may now qualify for the same treatment on this or a later | |
412 | * pass using this algorithm. | |
413 | */ | |
414 | int | |
ddd2ef55 | 415 | mapdirs(ino_t maxino, long *tapesize) |
1227625a SP |
416 | { |
417 | register struct dinode *dp; | |
b45f51d6 | 418 | register int isdir; |
1227625a SP |
419 | register char *map; |
420 | register ino_t ino; | |
8d4197bb | 421 | #ifndef __linux__ |
b45f51d6 | 422 | register int i; |
1227625a | 423 | long filesize; |
4267a8ad SP |
424 | #else |
425 | struct mapdirs_context mdc; | |
b45f51d6 | 426 | #endif |
4267a8ad | 427 | int ret, change = 0, nodump; |
1227625a SP |
428 | |
429 | isdir = 0; /* XXX just to get gcc to shut up */ | |
430 | for (map = dumpdirmap, ino = 1; ino < maxino; ino++) { | |
431 | if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */ | |
432 | isdir = *map++; | |
433 | else | |
434 | isdir >>= 1; | |
4267a8ad SP |
435 | /* |
436 | * If dir has been removed from the used map, it's either | |
437 | * because it had the nodump flag, or it herited it from | |
438 | * its parent. A directory can't be in dumpinomap if not | |
439 | * in usedinomap, but we have to go through it anyway | |
440 | * to propagate the nodump attribute. | |
441 | */ | |
442 | nodump = (TSTINO(ino, usedinomap) == 0); | |
443 | if ((isdir & 1) == 0 || | |
444 | (TSTINO(ino, dumpinomap) && nodump == 0)) | |
1227625a | 445 | continue; |
1227625a | 446 | dp = getino(ino); |
b45f51d6 | 447 | #ifdef __linux__ |
1227625a | 448 | ret = 0; |
4267a8ad SP |
449 | mdc.ret = &ret; |
450 | mdc.nodump = nodump; | |
451 | mdc.tapesize = tapesize; | |
452 | ext2fs_dir_iterate(fs, ino, 0, NULL, searchdir, (void *) &mdc); | |
1227625a | 453 | #else /* __linux__ */ |
1227625a SP |
454 | filesize = dp->di_size; |
455 | for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) { | |
456 | if (dp->di_db[i] != 0) | |
457 | ret |= searchdir(ino, dp->di_db[i], | |
458 | (long)dblksize(sblock, dp, i), | |
459 | filesize); | |
460 | if (ret & HASDUMPEDFILE) | |
461 | filesize = 0; | |
462 | else | |
463 | filesize -= sblock->fs_bsize; | |
464 | } | |
465 | for (i = 0; filesize > 0 && i < NIADDR; i++) { | |
466 | if (dp->di_ib[i] == 0) | |
467 | continue; | |
468 | ret |= dirindir(ino, dp->di_ib[i], i, &filesize); | |
469 | } | |
470 | #endif /* __linux__ */ | |
471 | if (ret & HASDUMPEDFILE) { | |
472 | SETINO(ino, dumpinomap); | |
473 | *tapesize += blockest(dp); | |
474 | change = 1; | |
475 | continue; | |
476 | } | |
4267a8ad SP |
477 | if (nodump) { |
478 | if (ret & HASSUBDIRS) | |
479 | change = 1; /* subdirs have inherited nodump */ | |
480 | CLRINO(ino, dumpdirmap); | |
481 | } else if ((ret & HASSUBDIRS) == 0) { | |
1227625a SP |
482 | if (!TSTINO(ino, dumpinomap)) { |
483 | CLRINO(ino, dumpdirmap); | |
484 | change = 1; | |
485 | } | |
486 | } | |
487 | } | |
488 | return (change); | |
489 | } | |
490 | ||
491 | #ifndef __linux__ | |
492 | /* | |
493 | * Read indirect blocks, and pass the data blocks to be searched | |
494 | * as directories. Quit as soon as any entry is found that will | |
495 | * require the directory to be dumped. | |
496 | */ | |
497 | static int | |
ddd2ef55 | 498 | dirindir(ino_t ino, daddr_t blkno, int ind_level, long *filesize) |
1227625a SP |
499 | { |
500 | int ret = 0; | |
501 | register int i; | |
502 | daddr_t idblk[MAXNINDIR]; | |
503 | ||
504 | bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize); | |
505 | if (ind_level <= 0) { | |
506 | for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) { | |
507 | blkno = idblk[i]; | |
508 | if (blkno != 0) | |
509 | ret |= searchdir(ino, blkno, sblock->fs_bsize, | |
510 | *filesize); | |
511 | if (ret & HASDUMPEDFILE) | |
512 | *filesize = 0; | |
513 | else | |
514 | *filesize -= sblock->fs_bsize; | |
515 | } | |
516 | return (ret); | |
517 | } | |
518 | ind_level--; | |
519 | for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) { | |
520 | blkno = idblk[i]; | |
521 | if (blkno != 0) | |
522 | ret |= dirindir(ino, blkno, ind_level, filesize); | |
523 | } | |
524 | return (ret); | |
525 | } | |
526 | #endif /* !__linux__ */ | |
527 | ||
528 | /* | |
529 | * Scan a disk block containing directory information looking to see if | |
530 | * any of the entries are on the dump list and to see if the directory | |
531 | * contains any subdirectories. | |
532 | */ | |
533 | #ifdef __linux__ | |
534 | static int | |
ddd2ef55 | 535 | searchdir(struct ext2_dir_entry *dp, int offset, int blocksize, char *buf, void *private) |
1227625a | 536 | { |
4267a8ad SP |
537 | struct mapdirs_context *mdc; |
538 | int *ret; | |
539 | long *tapesize; | |
540 | struct dinode *ip; | |
541 | ||
542 | mdc = (struct mapdirs_context *)private; | |
543 | ret = mdc->ret; | |
544 | tapesize = mdc->tapesize; | |
1227625a SP |
545 | |
546 | if (dp->inode == 0) | |
547 | return 0; | |
548 | if (dp->name[0] == '.') { | |
f346e363 | 549 | if (( dp->name_len & 0xFF ) == 1) |
1227625a | 550 | return 0; |
f346e363 | 551 | if (dp->name[1] == '.' && ( dp->name_len & 0xFF ) == 2) |
1227625a SP |
552 | return 0; |
553 | } | |
4267a8ad SP |
554 | if (mdc->nodump) { |
555 | ip = getino(dp->inode); | |
556 | if (TSTINO(dp->inode, dumpinomap)) { | |
557 | CLRINO(dp->inode, dumpinomap); | |
558 | CLRINO(dp->inode, usedinomap); | |
559 | *tapesize -= blockest(ip); | |
560 | } | |
561 | /* Add dir back to the dir map, to propagate nodump */ | |
562 | if ((ip->di_mode & IFMT) == IFDIR) { | |
563 | SETINO(dp->inode, dumpdirmap); | |
564 | *ret |= HASSUBDIRS; | |
565 | } | |
566 | } else { | |
567 | if (TSTINO(dp->inode, dumpinomap)) { | |
568 | *ret |= HASDUMPEDFILE; | |
569 | if (*ret & HASSUBDIRS) | |
570 | return DIRENT_ABORT; | |
571 | } | |
572 | if (TSTINO(dp->inode, dumpdirmap)) { | |
573 | *ret |= HASSUBDIRS; | |
574 | if (*ret & HASDUMPEDFILE) | |
575 | return DIRENT_ABORT; | |
576 | } | |
1227625a SP |
577 | } |
578 | return 0; | |
579 | } | |
580 | ||
581 | #else /* __linux__ */ | |
582 | ||
583 | static int | |
ddd2ef55 | 584 | searchdir(ino_t ino, daddr_t blkno, long size, long filesize) |
1227625a SP |
585 | { |
586 | register struct direct *dp; | |
587 | register long loc, ret = 0; | |
588 | char dblk[MAXBSIZE]; | |
589 | ||
590 | bread(fsbtodb(sblock, blkno), dblk, (int)size); | |
591 | if (filesize < size) | |
592 | size = filesize; | |
593 | for (loc = 0; loc < size; ) { | |
594 | dp = (struct direct *)(dblk + loc); | |
595 | if (dp->d_reclen == 0) { | |
596 | msg("corrupted directory, inumber %d\n", ino); | |
597 | break; | |
598 | } | |
599 | loc += dp->d_reclen; | |
600 | if (dp->d_ino == 0) | |
601 | continue; | |
602 | if (dp->d_name[0] == '.') { | |
603 | if (dp->d_name[1] == '\0') | |
604 | continue; | |
605 | if (dp->d_name[1] == '.' && dp->d_name[2] == '\0') | |
606 | continue; | |
607 | } | |
608 | if (TSTINO(dp->d_ino, dumpinomap)) { | |
609 | ret |= HASDUMPEDFILE; | |
610 | if (ret & HASSUBDIRS) | |
611 | break; | |
612 | } | |
613 | if (TSTINO(dp->d_ino, dumpdirmap)) { | |
614 | ret |= HASSUBDIRS; | |
615 | if (ret & HASDUMPEDFILE) | |
616 | break; | |
617 | } | |
618 | } | |
619 | return (ret); | |
620 | } | |
621 | #endif /* __linux__ */ | |
622 | ||
623 | #ifdef __linux__ | |
624 | ||
625 | struct block_context { | |
626 | ino_t ino; | |
627 | int *buf; | |
628 | int cnt; | |
629 | int max; | |
630 | int next_block; | |
631 | }; | |
632 | ||
633 | /* | |
634 | * Dump a block to the tape | |
635 | */ | |
636 | static int | |
637 | dumponeblock(ext2_filsys fs, blk_t *blocknr, int blockcnt, void *private) | |
638 | { | |
639 | struct block_context *p; | |
640 | int i; | |
641 | ||
642 | if (blockcnt < NDADDR) | |
643 | return 0; | |
644 | p = (struct block_context *)private; | |
645 | for (i = p->next_block; i < blockcnt; i++) { | |
646 | p->buf[p->cnt++] = 0; | |
647 | if (p->cnt == p->max) { | |
648 | blksout (p->buf, p->cnt, p->ino); | |
649 | p->cnt = 0; | |
650 | } | |
651 | } | |
652 | p->buf[p->cnt++] = *blocknr; | |
653 | if (p->cnt == p->max) { | |
654 | blksout (p->buf, p->cnt, p->ino); | |
655 | p->cnt = 0; | |
656 | } | |
657 | p->next_block = blockcnt + 1; | |
658 | return 0; | |
659 | } | |
660 | #endif | |
661 | ||
662 | /* | |
663 | * Dump passes 3 and 4. | |
664 | * | |
665 | * Dump the contents of an inode to tape. | |
666 | */ | |
667 | void | |
ddd2ef55 | 668 | dumpino(struct dinode *dp, ino_t ino) |
1227625a | 669 | { |
b45f51d6 | 670 | int cnt; |
1227625a SP |
671 | fsizeT size; |
672 | char buf[TP_BSIZE]; | |
673 | struct old_bsd_inode obi; | |
674 | #ifdef __linux__ | |
675 | struct block_context bc; | |
b45f51d6 SP |
676 | #else |
677 | int ind_level; | |
1227625a SP |
678 | #endif |
679 | ||
680 | if (newtape) { | |
681 | newtape = 0; | |
682 | dumpmap(dumpinomap, TS_BITS, ino); | |
683 | } | |
684 | CLRINO(ino, dumpinomap); | |
685 | #ifdef __linux__ | |
686 | memset(&obi, 0, sizeof(obi)); | |
687 | obi.di_mode = dp->di_mode; | |
688 | obi.di_uid = dp->di_uid; | |
689 | obi.di_gid = dp->di_gid; | |
690 | obi.di_qsize.v = (u_quad_t)dp->di_size; | |
691 | obi.di_atime = dp->di_atime; | |
692 | obi.di_mtime = dp->di_mtime; | |
693 | obi.di_ctime = dp->di_ctime; | |
694 | obi.di_nlink = dp->di_nlink; | |
695 | obi.di_blocks = dp->di_blocks; | |
696 | obi.di_flags = dp->di_flags; | |
697 | obi.di_gen = dp->di_gen; | |
698 | memmove(&obi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t)); | |
699 | if (dp->di_file_acl || dp->di_dir_acl) | |
b9c89cf4 | 700 | warn("ACLs in inode #%ld won't be dumped", (long)ino); |
1227625a SP |
701 | memmove(&spcl.c_dinode, &obi, sizeof(obi)); |
702 | #else /* __linux__ */ | |
703 | spcl.c_dinode = *dp; | |
704 | #endif /* __linux__ */ | |
705 | spcl.c_type = TS_INODE; | |
706 | spcl.c_count = 0; | |
707 | switch (dp->di_mode & S_IFMT) { | |
708 | ||
709 | case 0: | |
710 | /* | |
711 | * Freed inode. | |
712 | */ | |
713 | return; | |
714 | ||
715 | #ifdef __linux__ | |
716 | case S_IFDIR: | |
717 | msg("Warning: dumpino called on a directory (ino %d)\n", ino); | |
718 | return; | |
719 | #endif | |
720 | ||
721 | case S_IFLNK: | |
722 | /* | |
723 | * Check for short symbolic link. | |
724 | */ | |
725 | #ifdef __linux__ | |
726 | if (dp->di_size > 0 && | |
727 | dp->di_size < EXT2_N_BLOCKS * sizeof (daddr_t)) { | |
728 | spcl.c_addr[0] = 1; | |
729 | spcl.c_count = 1; | |
730 | writeheader(ino); | |
731 | memmove(buf, dp->di_db, (u_long)dp->di_size); | |
732 | buf[dp->di_size] = '\0'; | |
733 | writerec(buf, 0); | |
734 | return; | |
735 | } | |
736 | #endif /* __linux__ */ | |
737 | #ifdef FS_44INODEFMT | |
738 | if (dp->di_size > 0 && | |
739 | dp->di_size < sblock->fs_maxsymlinklen) { | |
740 | spcl.c_addr[0] = 1; | |
741 | spcl.c_count = 1; | |
742 | writeheader(ino); | |
743 | memmove(buf, dp->di_shortlink, (u_long)dp->di_size); | |
744 | buf[dp->di_size] = '\0'; | |
745 | writerec(buf, 0); | |
746 | return; | |
747 | } | |
748 | #endif | |
749 | /* fall through */ | |
750 | ||
751 | #ifndef __linux__ | |
752 | case S_IFDIR: | |
753 | #endif | |
754 | case S_IFREG: | |
755 | if (dp->di_size > 0) | |
756 | break; | |
757 | /* fall through */ | |
758 | ||
759 | case S_IFIFO: | |
760 | case S_IFSOCK: | |
761 | case S_IFCHR: | |
762 | case S_IFBLK: | |
763 | writeheader(ino); | |
764 | return; | |
765 | ||
766 | default: | |
767 | msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT); | |
768 | return; | |
769 | } | |
770 | if (dp->di_size > NDADDR * sblock->fs_bsize) | |
771 | #ifdef __linux__ | |
772 | cnt = NDADDR * EXT2_FRAGS_PER_BLOCK(fs->super); | |
773 | #else | |
774 | cnt = NDADDR * sblock->fs_frag; | |
775 | #endif | |
776 | else | |
777 | cnt = howmany(dp->di_size, sblock->fs_fsize); | |
778 | blksout(&dp->di_db[0], cnt, ino); | |
b45f51d6 | 779 | if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0) |
1227625a SP |
780 | return; |
781 | #ifdef __linux__ | |
782 | bc.max = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super); | |
783 | bc.buf = (int *)malloc (bc.max * sizeof (int)); | |
784 | bc.cnt = 0; | |
785 | bc.ino = ino; | |
786 | bc.next_block = NDADDR; | |
787 | ||
788 | ext2fs_block_iterate (fs, ino, 0, NULL, dumponeblock, (void *)&bc); | |
789 | if (bc.cnt > 0) { | |
790 | blksout (bc.buf, bc.cnt, bc.ino); | |
791 | } | |
7fbb9b02 | 792 | free(bc.buf); |
1227625a SP |
793 | #else |
794 | for (ind_level = 0; ind_level < NIADDR; ind_level++) { | |
795 | dmpindir(ino, dp->di_ib[ind_level], ind_level, &size); | |
796 | if (size <= 0) | |
797 | return; | |
798 | } | |
799 | #endif | |
800 | } | |
801 | ||
802 | #ifdef __linux__ | |
803 | ||
804 | struct convert_dir_context { | |
805 | char *buf; | |
806 | int prev_offset; | |
807 | int offset; | |
808 | int bs; | |
809 | }; | |
810 | ||
811 | /* | |
812 | * This function converts an ext2fs directory entry to the BSD format. | |
813 | * | |
814 | * Basically, it adds a null-character at the end of the name, recomputes the | |
815 | * size of the entry, and creates it in a temporary buffer | |
816 | */ | |
817 | static int | |
ddd2ef55 | 818 | convert_dir(struct ext2_dir_entry *dirent, int offset, int blocksize, char *buf, void *private) |
1227625a SP |
819 | { |
820 | struct convert_dir_context *p; | |
8eb05297 | 821 | struct olddirect *dp; |
1227625a SP |
822 | int reclen; |
823 | ||
824 | p = (struct convert_dir_context *)private; | |
825 | ||
c5886bd7 | 826 | reclen = EXT2_DIR_REC_LEN((dirent->name_len & 0xFF) + 1); |
1227625a | 827 | if (((p->offset + reclen - 1) / p->bs) != (p->offset / p->bs)) { |
8eb05297 | 828 | dp = (struct olddirect *)(p->buf + p->prev_offset); |
1227625a SP |
829 | dp->d_reclen += p->bs - (p->offset % p->bs); |
830 | p->offset += p->bs - (p->offset % p->bs); | |
831 | } | |
832 | ||
8eb05297 | 833 | dp = (struct olddirect *)(p->buf + p->offset); |
1227625a SP |
834 | dp->d_ino = dirent->inode; |
835 | dp->d_reclen = reclen; | |
c5886bd7 SP |
836 | dp->d_namlen = dirent->name_len & 0xFF; |
837 | strncpy(dp->d_name, dirent->name, dp->d_namlen); | |
1227625a SP |
838 | dp->d_name[dp->d_namlen] = '\0'; |
839 | p->prev_offset = p->offset; | |
840 | p->offset += reclen; | |
841 | ||
842 | return 0; | |
843 | } | |
844 | ||
845 | /* | |
846 | * Dump pass 3 | |
847 | * | |
848 | * Dumps a directory to tape after converting it to the BSD format | |
849 | */ | |
850 | void | |
ddd2ef55 | 851 | dumpdirino(struct dinode *dp, ino_t ino) |
1227625a SP |
852 | { |
853 | fsizeT size; | |
854 | char buf[TP_BSIZE]; | |
855 | struct old_bsd_inode obi; | |
856 | struct convert_dir_context cdc; | |
857 | errcode_t retval; | |
858 | struct ext2_dir_entry *de; | |
859 | fsizeT dir_size; | |
860 | ||
861 | if (newtape) { | |
862 | newtape = 0; | |
863 | dumpmap(dumpinomap, TS_BITS, ino); | |
864 | } | |
865 | CLRINO(ino, dumpinomap); | |
866 | ||
867 | /* | |
868 | * Convert the directory to the BSD format | |
869 | */ | |
870 | /* Allocate a buffer for the conversion (twice the size of the | |
871 | ext2fs directory to avoid problems ;-) */ | |
872 | cdc.buf = (char *)malloc(dp->di_size * 2 * sizeof(char)); | |
873 | if (cdc.buf == NULL) | |
ddd2ef55 SP |
874 | err(1, "Cannot allocate buffer to convert directory #%lu\n", |
875 | (unsigned long)ino); | |
1227625a SP |
876 | cdc.offset = 0; |
877 | cdc.prev_offset = 0; | |
878 | cdc.bs = MIN(DIRBLKSIZ, TP_BSIZE); | |
879 | /* Do the conversion */ | |
880 | retval = ext2fs_dir_iterate(fs, ino, 0, NULL, convert_dir, (void *)&cdc); | |
881 | if (retval) { | |
b9c89cf4 | 882 | com_err(disk, retval, "while converting directory #%ld\n", (long)ino); |
1227625a SP |
883 | exit(X_ABORT); |
884 | } | |
885 | /* Fix the last entry */ | |
886 | if ((cdc.offset % cdc.bs) != 0) { | |
887 | de = (struct ext2_dir_entry *)(cdc.buf + cdc.prev_offset); | |
888 | de->rec_len += cdc.bs - (cdc.offset % cdc.bs); | |
889 | cdc.offset += cdc.bs - (cdc.offset % cdc.bs); | |
890 | } | |
891 | ||
892 | dir_size = cdc.offset; | |
893 | ||
894 | #ifdef __linux__ | |
895 | memset(&obi, 0, sizeof(obi)); | |
896 | obi.di_mode = dp->di_mode; | |
897 | obi.di_uid = dp->di_uid; | |
898 | obi.di_gid = dp->di_gid; | |
899 | obi.di_qsize.v = dir_size; /* (u_quad_t)dp->di_size; */ | |
900 | obi.di_atime = dp->di_atime; | |
901 | obi.di_mtime = dp->di_mtime; | |
902 | obi.di_ctime = dp->di_ctime; | |
903 | obi.di_nlink = dp->di_nlink; | |
904 | obi.di_blocks = dp->di_blocks; | |
905 | obi.di_flags = dp->di_flags; | |
906 | obi.di_gen = dp->di_gen; | |
907 | memmove(&obi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t)); | |
908 | if (dp->di_file_acl || dp->di_dir_acl) | |
b9c89cf4 | 909 | warn("ACLs in inode #%ld won't be dumped", (long)ino); |
1227625a SP |
910 | memmove(&spcl.c_dinode, &obi, sizeof(obi)); |
911 | #else /* __linux__ */ | |
912 | spcl.c_dinode = *dp; | |
913 | #endif /* __linux__ */ | |
914 | spcl.c_type = TS_INODE; | |
915 | spcl.c_count = 0; | |
916 | switch (dp->di_mode & S_IFMT) { | |
917 | ||
918 | case 0: | |
919 | /* | |
920 | * Freed inode. | |
921 | */ | |
922 | return; | |
923 | ||
924 | case S_IFDIR: | |
925 | if (dir_size > 0) | |
926 | break; | |
927 | msg("Warning: size of directory inode #%d is <= 0 (%d)!\n", | |
928 | ino, dir_size); | |
929 | return; | |
930 | ||
931 | default: | |
932 | msg("Warning: dumpdirino called with file type 0%o (inode #%d)\n", | |
933 | dp->di_mode & IFMT, ino); | |
934 | return; | |
935 | } | |
936 | for (size = 0; size < dir_size; size += TP_BSIZE) { | |
937 | spcl.c_addr[0] = 1; | |
938 | spcl.c_count = 1; | |
939 | writeheader(ino); | |
940 | memmove(buf, cdc.buf + size, TP_BSIZE); | |
941 | writerec(buf, 0); | |
942 | spcl.c_type = TS_ADDR; | |
943 | } | |
944 | ||
945 | (void)free(cdc.buf); | |
946 | } | |
947 | #endif /* __linux__ */ | |
948 | ||
949 | #ifndef __linux__ | |
950 | /* | |
951 | * Read indirect blocks, and pass the data blocks to be dumped. | |
952 | */ | |
953 | static void | |
ddd2ef55 | 954 | dmpindir(ino_t ino, daddr_t blk, int ind_level, fsizeT *size) |
1227625a SP |
955 | { |
956 | int i, cnt; | |
b45f51d6 SP |
957 | #ifdef __linux__ |
958 | int max; | |
959 | blk_t *swapme; | |
960 | #endif | |
1227625a SP |
961 | daddr_t idblk[MAXNINDIR]; |
962 | ||
b45f51d6 | 963 | if (blk != 0) { |
1227625a | 964 | bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize); |
b45f51d6 SP |
965 | #ifdef __linux__ |
966 | /* | |
967 | * My RedHat 4.0 system doesn't have these flags; I haven't | |
968 | * upgraded e2fsprogs yet | |
969 | */ | |
970 | #if defined(EXT2_FLAG_SWAP_BYTES) | |
971 | if ((fs->flags & EXT2_FLAG_SWAP_BYTES) || | |
972 | (fs->flags & EXT2_FLAG_SWAP_BYTES_READ)) { | |
973 | #endif | |
974 | max = sblock->fs_bsize >> 2; | |
975 | swapme = (blk_t *) idblk; | |
976 | for (i = 0; i < max; i++, swapme++) | |
977 | *swapme = swab32(*swapme); | |
978 | #if defined(EXT2_FLAG_SWAP_BYTES) | |
979 | } | |
980 | #endif | |
981 | #endif | |
1227625a SP |
982 | else |
983 | memset(idblk, 0, (int)sblock->fs_bsize); | |
984 | if (ind_level <= 0) { | |
985 | if (*size < NINDIR(sblock) * sblock->fs_bsize) | |
986 | cnt = howmany(*size, sblock->fs_fsize); | |
987 | else | |
988 | #ifdef __linux__ | |
989 | cnt = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super); | |
990 | #else | |
991 | cnt = NINDIR(sblock) * sblock->fs_frag; | |
992 | #endif | |
993 | *size -= NINDIR(sblock) * sblock->fs_bsize; | |
994 | blksout(&idblk[0], cnt, ino); | |
995 | return; | |
996 | } | |
997 | ind_level--; | |
998 | for (i = 0; i < NINDIR(sblock); i++) { | |
999 | dmpindir(ino, idblk[i], ind_level, size); | |
1000 | if (*size <= 0) | |
1001 | return; | |
1002 | } | |
1003 | } | |
1004 | #endif | |
1005 | ||
1006 | /* | |
1007 | * Collect up the data into tape record sized buffers and output them. | |
1008 | */ | |
1009 | void | |
ddd2ef55 | 1010 | blksout(daddr_t *blkp, int frags, ino_t ino) |
1227625a SP |
1011 | { |
1012 | register daddr_t *bp; | |
1013 | int i, j, count, blks, tbperdb; | |
1014 | ||
1015 | blks = howmany(frags * sblock->fs_fsize, TP_BSIZE); | |
1016 | tbperdb = sblock->fs_bsize >> tp_bshift; | |
1017 | for (i = 0; i < blks; i += TP_NINDIR) { | |
1018 | if (i + TP_NINDIR > blks) | |
1019 | count = blks; | |
1020 | else | |
1021 | count = i + TP_NINDIR; | |
1022 | for (j = i; j < count; j++) | |
1023 | if (blkp[j / tbperdb] != 0) | |
1024 | spcl.c_addr[j - i] = 1; | |
1025 | else | |
1026 | spcl.c_addr[j - i] = 0; | |
1027 | spcl.c_count = count - i; | |
1028 | writeheader(ino); | |
1029 | bp = &blkp[i / tbperdb]; | |
ddd2ef55 SP |
1030 | for (j = i; j < count; j += tbperdb, bp++) { |
1031 | if (*bp != 0) { | |
1227625a SP |
1032 | if (j + tbperdb <= count) |
1033 | dumpblock(*bp, (int)sblock->fs_bsize); | |
1034 | else | |
1035 | dumpblock(*bp, (count - j) * TP_BSIZE); | |
ddd2ef55 SP |
1036 | } |
1037 | } | |
1227625a SP |
1038 | spcl.c_type = TS_ADDR; |
1039 | } | |
1040 | } | |
1041 | ||
1042 | /* | |
1043 | * Dump a map to the tape. | |
1044 | */ | |
1045 | void | |
ddd2ef55 | 1046 | dumpmap(char *map, int type, ino_t ino) |
1227625a SP |
1047 | { |
1048 | register int i; | |
1049 | char *cp; | |
1050 | ||
1051 | spcl.c_type = type; | |
1052 | spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE); | |
1053 | writeheader(ino); | |
1054 | for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE) | |
1055 | writerec(cp, 0); | |
1056 | } | |
1057 | ||
1058 | /* | |
1059 | * Write a header record to the dump tape. | |
1060 | */ | |
1061 | void | |
ddd2ef55 | 1062 | writeheader(ino_t ino) |
1227625a SP |
1063 | { |
1064 | #ifdef __linux__ | |
1065 | register __s32 sum, cnt, *lp; | |
1066 | #else | |
b45f51d6 | 1067 | register int32_t sum, cnt, *lp; |
1227625a SP |
1068 | #endif |
1069 | ||
1070 | spcl.c_inumber = ino; | |
1071 | spcl.c_magic = NFS_MAGIC; | |
1072 | spcl.c_checksum = 0; | |
1073 | #ifdef __linux__ | |
1074 | lp = (__s32 *)&spcl; | |
1075 | #else | |
b45f51d6 | 1076 | lp = (int32_t *)&spcl; |
1227625a SP |
1077 | #endif |
1078 | sum = 0; | |
1079 | #ifdef __linux__ | |
1080 | cnt = sizeof(union u_spcl) / (4 * sizeof(__s32)); | |
1081 | #else | |
b45f51d6 | 1082 | cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t)); |
1227625a SP |
1083 | #endif |
1084 | while (--cnt >= 0) { | |
1085 | sum += *lp++; | |
1086 | sum += *lp++; | |
1087 | sum += *lp++; | |
1088 | sum += *lp++; | |
1089 | } | |
1090 | spcl.c_checksum = CHECKSUM - sum; | |
1091 | writerec((char *)&spcl, 1); | |
1092 | } | |
1093 | ||
1094 | #ifdef __linux__ | |
1095 | struct dinode * | |
ddd2ef55 | 1096 | getino(ino_t inum) |
1227625a SP |
1097 | { |
1098 | static struct dinode dinode; | |
1099 | ||
1100 | curino = inum; | |
1101 | ext2fs_read_inode(fs, inum, (struct ext2_inode *) &dinode); | |
1102 | return &dinode; | |
1103 | } | |
1104 | #else /* __linux__ */ | |
1105 | struct dinode * | |
ddd2ef55 | 1106 | getino(ino_t inum) |
1227625a SP |
1107 | { |
1108 | static daddr_t minino, maxino; | |
1109 | static struct dinode inoblock[MAXINOPB]; | |
1110 | ||
1111 | curino = inum; | |
1112 | if (inum >= minino && inum < maxino) | |
1113 | return (&inoblock[inum - minino]); | |
1114 | bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock, | |
1115 | (int)sblock->fs_bsize); | |
1116 | minino = inum - (inum % INOPB(sblock)); | |
1117 | maxino = minino + INOPB(sblock); | |
1118 | return (&inoblock[inum - minino]); | |
1119 | } | |
1120 | #endif /* __linux__ */ | |
1121 | ||
1122 | /* | |
1123 | * Read a chunk of data from the disk. | |
1124 | * Try to recover from hard errors by reading in sector sized pieces. | |
1125 | * Error recovery is attempted at most BREADEMAX times before seeking | |
1126 | * consent from the operator to continue. | |
1127 | */ | |
b45f51d6 | 1128 | int breaderrors = 0; |
1227625a SP |
1129 | #define BREADEMAX 32 |
1130 | ||
1131 | void | |
ddd2ef55 | 1132 | bread(daddr_t blkno, char *buf, int size) |
1227625a SP |
1133 | { |
1134 | int cnt, i; | |
1135 | extern int errno; | |
1136 | ||
1137 | loop: | |
1138 | #ifdef __linux__ | |
f34aca37 SP |
1139 | if (ext2fs_llseek(diskfd, (((ext2_loff_t)blkno) << dev_bshift), 0) != |
1140 | (((ext2_loff_t)blkno) << dev_bshift)) | |
1227625a | 1141 | #else |
b45f51d6 SP |
1142 | if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) != |
1143 | ((off_t)blkno << dev_bshift)) | |
1227625a SP |
1144 | #endif |
1145 | msg("bread: lseek fails\n"); | |
1146 | if ((cnt = read(diskfd, buf, size)) == size) | |
1147 | return; | |
1148 | if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) { | |
1149 | /* | |
1150 | * Trying to read the final fragment. | |
1151 | * | |
1152 | * NB - dump only works in TP_BSIZE blocks, hence | |
1153 | * rounds `dev_bsize' fragments up to TP_BSIZE pieces. | |
1154 | * It should be smarter about not actually trying to | |
1155 | * read more than it can get, but for the time being | |
1156 | * we punt and scale back the read only when it gets | |
1157 | * us into trouble. (mkm 9/25/83) | |
1158 | */ | |
1159 | size -= dev_bsize; | |
1160 | goto loop; | |
1161 | } | |
1162 | if (cnt == -1) | |
1163 | msg("read error from %s: %s: [block %d]: count=%d\n", | |
1164 | disk, strerror(errno), blkno, size); | |
1165 | else | |
1166 | msg("short read error from %s: [block %d]: count=%d, got=%d\n", | |
1167 | disk, blkno, size, cnt); | |
1168 | if (++breaderrors > BREADEMAX) { | |
1169 | msg("More than %d block read errors from %d\n", | |
1170 | BREADEMAX, disk); | |
1171 | broadcast("DUMP IS AILING!\n"); | |
1172 | msg("This is an unrecoverable error.\n"); | |
1173 | if (!query("Do you want to attempt to continue?")){ | |
1174 | dumpabort(0); | |
1175 | /*NOTREACHED*/ | |
1176 | } else | |
1177 | breaderrors = 0; | |
1178 | } | |
1179 | /* | |
1180 | * Zero buffer, then try to read each sector of buffer separately. | |
1181 | */ | |
1182 | memset(buf, 0, size); | |
1183 | for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) { | |
1184 | #ifdef __linux__ | |
f34aca37 SP |
1185 | if (ext2fs_llseek(diskfd, (((ext2_loff_t)blkno) << dev_bshift), 0) != |
1186 | (((ext2_loff_t)blkno) << dev_bshift)) | |
1227625a | 1187 | #else |
b45f51d6 SP |
1188 | if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) != |
1189 | ((off_t)blkno << dev_bshift)) | |
1227625a SP |
1190 | #endif |
1191 | msg("bread: lseek2 fails!\n"); | |
1192 | if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize) | |
1193 | continue; | |
1194 | if (cnt == -1) { | |
1195 | msg("read error from %s: %s: [sector %d]: count=%d\n", | |
1196 | disk, strerror(errno), blkno, dev_bsize); | |
1197 | continue; | |
1198 | } | |
1199 | msg("short read error from %s: [sector %d]: count=%d, got=%d\n", | |
1200 | disk, blkno, dev_bsize, cnt); | |
1201 | } | |
1202 | } |