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