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