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