]> git.wh0rd.org - dump.git/blob - dump/traverse.c
0ea5b96f7b355255844bc5f85672543ed2c25f15
[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.32 2001/04/10 13:42:22 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 int
374 maponefile(dump_ino_t maxino, long *tapesize, char *directory)
375 {
376 errcode_t retval;
377 ext2_ino_t dir_ino;
378 char dir_name [MAXPATHLEN];
379 int i, anydirskipped = 0;
380
381 /*
382 * Mark every directory in the path as being dumped
383 */
384 for (i = 0; i < strlen (directory); i++) {
385 if (directory[i] == '/') {
386 strncpy (dir_name, directory, i);
387 dir_name[i] = '\0';
388 retval = ext2fs_namei(fs, ROOTINO, ROOTINO,
389 dir_name, &dir_ino);
390 if (retval) {
391 com_err(disk, retval,
392 "while translating %s", dir_name);
393 exit(X_ABORT);
394 }
395 mapfileino((dump_ino_t) dir_ino, 0,
396 tapesize, &anydirskipped);
397 }
398 }
399 /*
400 * Mark the final directory
401 */
402 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, directory, &dir_ino);
403 if (retval) {
404 com_err(disk, retval, "while translating %s", directory);
405 exit(X_ABORT);
406 }
407 mapfileino((dump_ino_t)dir_ino, 0, tapesize, &anydirskipped);
408
409 mapfileino(ROOTINO, 0, tapesize, &anydirskipped);
410
411 /*
412 * Restore gets very upset if the root is not dumped,
413 * so ensure that it always is dumped.
414 */
415 SETINO(ROOTINO, dumpdirmap);
416 return anydirskipped;
417 }
418 #endif /* __linux__ */
419
420 #ifdef __linux__
421 struct mapfile_context {
422 long *tapesize;
423 int *anydirskipped;
424 };
425
426 static int
427 mapfilesindir(struct ext2_dir_entry *dirent, int offset, int blocksize, char *buf, void *private)
428 {
429 register struct dinode const *dp;
430 register int mode;
431 errcode_t retval;
432 struct mapfile_context *mfc;
433 ext2_ino_t ino;
434
435 ino = dirent->inode;
436 mfc = (struct mapfile_context *)private;
437 dp = getino(dirent->inode);
438
439 mapfileino(dirent->inode, dp, mfc->tapesize, mfc->anydirskipped);
440
441 mode = dp->di_mode & IFMT;
442 if (mode == IFDIR && dp->di_nlink != 0 && dp->di_dtime == 0) {
443 if ((dirent->name[0] != '.' || ( dirent->name_len & 0xFF ) != 1) &&
444 (dirent->name[0] != '.' || dirent->name[1] != '.' ||
445 ( dirent->name_len & 0xFF ) != 2)) {
446 retval = ext2fs_dir_iterate(fs, ino, 0, NULL,
447 mapfilesindir, private);
448 if (retval)
449 return retval;
450 }
451 }
452 return 0;
453 }
454
455 /*
456 * Dump pass 1.
457 *
458 * Walk the inode list for a filesystem to find all allocated inodes
459 * that have been modified since the previous dump time. Also, find all
460 * the directories in the filesystem.
461 */
462 int
463 mapfilesfromdir(dump_ino_t maxino, long *tapesize, char *directory)
464 {
465 errcode_t retval;
466 struct mapfile_context mfc;
467 ext2_ino_t dir_ino;
468 char dir_name [MAXPATHLEN];
469 int i, anydirskipped = 0;
470
471 /*
472 * Mark every directory in the path as being dumped
473 */
474 for (i = 0; i < strlen (directory); i++) {
475 if (directory[i] == '/') {
476 strncpy (dir_name, directory, i);
477 dir_name[i] = '\0';
478 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, dir_name,
479 &dir_ino);
480 if (retval) {
481 com_err(disk, retval, "while translating %s",
482 dir_name);
483 exit(X_ABORT);
484 }
485 mapfileino(dir_ino, 0, tapesize, &anydirskipped);
486 }
487 }
488 /*
489 * Mark the final directory
490 */
491 retval = ext2fs_namei(fs, ROOTINO, ROOTINO, directory, &dir_ino);
492 if (retval) {
493 com_err(disk, retval, "while translating %s", directory);
494 exit(X_ABORT);
495 }
496 mapfileino(dir_ino, 0, tapesize, &anydirskipped);
497
498 mfc.tapesize = tapesize;
499 mfc.anydirskipped = &anydirskipped;
500 retval = ext2fs_dir_iterate(fs, dir_ino, 0, NULL, mapfilesindir,
501 (void *)&mfc);
502
503 if (retval) {
504 com_err(disk, retval, "while mapping files in %s", directory);
505 exit(X_ABORT);
506 }
507 /*
508 * Ensure that the root inode actually appears in the file list
509 * for a subdir
510 */
511 mapfileino(ROOTINO, 0, tapesize, &anydirskipped);
512 /*
513 * Restore gets very upset if the root is not dumped,
514 * so ensure that it always is dumped.
515 */
516 SETINO(ROOTINO, dumpinomap);
517 return anydirskipped;
518 }
519 #endif
520
521 #ifdef __linux__
522 struct mapdirs_context {
523 int *ret;
524 int nodump;
525 long *tapesize;
526 };
527 #endif
528
529 /*
530 * Dump pass 2.
531 *
532 * Scan each directory on the filesystem to see if it has any modified
533 * files in it. If it does, and has not already been added to the dump
534 * list (because it was itself modified), then add it. If a directory
535 * has not been modified itself, contains no modified files and has no
536 * subdirectories, then it can be deleted from the dump list and from
537 * the list of directories. By deleting it from the list of directories,
538 * its parent may now qualify for the same treatment on this or a later
539 * pass using this algorithm.
540 */
541 int
542 mapdirs(dump_ino_t maxino, long *tapesize)
543 {
544 register struct dinode *dp;
545 register int isdir;
546 register char *map;
547 register dump_ino_t ino;
548 #ifndef __linux__
549 register int i;
550 long filesize;
551 #else
552 struct mapdirs_context mdc;
553 #endif
554 int ret, change = 0, nodump;
555
556 isdir = 0; /* XXX just to get gcc to shut up */
557 for (map = dumpdirmap, ino = 1; ino < maxino; ino++) {
558 if (((ino - 1) % NBBY) == 0) /* map is offset by 1 */
559 isdir = *map++;
560 else
561 isdir >>= 1;
562 /*
563 * If dir has been removed from the used map, it's either
564 * because it had the nodump flag, or it herited it from
565 * its parent. A directory can't be in dumpinomap if not
566 * in usedinomap, but we have to go through it anyway
567 * to propagate the nodump attribute.
568 */
569 nodump = (TSTINO(ino, usedinomap) == 0);
570 if ((isdir & 1) == 0 ||
571 (TSTINO(ino, dumpinomap) && nodump == 0))
572 continue;
573 dp = getino(ino);
574 #ifdef __linux__
575 ret = 0;
576 mdc.ret = &ret;
577 mdc.nodump = nodump;
578 mdc.tapesize = tapesize;
579 ext2fs_dir_iterate(fs, ino, 0, NULL, searchdir, (void *) &mdc);
580 #else /* __linux__ */
581 filesize = dp->di_size;
582 for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) {
583 if (dp->di_db[i] != 0)
584 ret |= searchdir(ino, dp->di_db[i],
585 (long)dblksize(sblock, dp, i),
586 filesize);
587 if (ret & HASDUMPEDFILE)
588 filesize = 0;
589 else
590 filesize -= sblock->fs_bsize;
591 }
592 for (i = 0; filesize > 0 && i < NIADDR; i++) {
593 if (dp->di_ib[i] == 0)
594 continue;
595 ret |= dirindir(ino, dp->di_ib[i], i, &filesize);
596 }
597 #endif /* __linux__ */
598 if (ret & HASDUMPEDFILE) {
599 SETINO(ino, dumpinomap);
600 *tapesize += blockest(dp);
601 change = 1;
602 continue;
603 }
604 if (nodump) {
605 if (ret & HASSUBDIRS)
606 change = 1; /* subdirs have inherited nodump */
607 CLRINO(ino, dumpdirmap);
608 } else if ((ret & HASSUBDIRS) == 0) {
609 if (!TSTINO(ino, dumpinomap)) {
610 CLRINO(ino, dumpdirmap);
611 change = 1;
612 }
613 }
614 }
615 return (change);
616 }
617
618 #ifndef __linux__
619 /*
620 * Read indirect blocks, and pass the data blocks to be searched
621 * as directories. Quit as soon as any entry is found that will
622 * require the directory to be dumped.
623 */
624 static int
625 dirindir(dump_ino_t ino, daddr_t blkno, int ind_level, long *filesize)
626 {
627 int ret = 0;
628 register int i;
629 daddr_t idblk[MAXNINDIR];
630
631 bread(fsbtodb(sblock, blkno), (char *)idblk, (int)sblock->fs_bsize);
632 if (ind_level <= 0) {
633 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
634 blkno = idblk[i];
635 if (blkno != 0)
636 ret |= searchdir(ino, blkno, sblock->fs_bsize,
637 *filesize);
638 if (ret & HASDUMPEDFILE)
639 *filesize = 0;
640 else
641 *filesize -= sblock->fs_bsize;
642 }
643 return (ret);
644 }
645 ind_level--;
646 for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) {
647 blkno = idblk[i];
648 if (blkno != 0)
649 ret |= dirindir(ino, blkno, ind_level, filesize);
650 }
651 return (ret);
652 }
653 #endif /* !__linux__ */
654
655 /*
656 * Scan a disk block containing directory information looking to see if
657 * any of the entries are on the dump list and to see if the directory
658 * contains any subdirectories.
659 */
660 #ifdef __linux__
661 static int
662 searchdir(struct ext2_dir_entry *dp, int offset, int blocksize, char *buf, void *private)
663 {
664 struct mapdirs_context *mdc;
665 int *ret;
666 long *tapesize;
667 struct dinode *ip;
668
669 mdc = (struct mapdirs_context *)private;
670 ret = mdc->ret;
671 tapesize = mdc->tapesize;
672
673 if (dp->inode == 0)
674 return 0;
675 if (dp->name[0] == '.') {
676 if (( dp->name_len & 0xFF ) == 1)
677 return 0;
678 if (dp->name[1] == '.' && ( dp->name_len & 0xFF ) == 2)
679 return 0;
680 }
681 if (mdc->nodump) {
682 ip = getino(dp->inode);
683 if (TSTINO(dp->inode, dumpinomap)) {
684 CLRINO(dp->inode, dumpinomap);
685 CLRINO(dp->inode, usedinomap);
686 *tapesize -= blockest(ip);
687 }
688 /* Add dir back to the dir map, to propagate nodump */
689 if ((ip->di_mode & IFMT) == IFDIR) {
690 SETINO(dp->inode, dumpdirmap);
691 *ret |= HASSUBDIRS;
692 }
693 } else {
694 if (TSTINO(dp->inode, dumpinomap)) {
695 *ret |= HASDUMPEDFILE;
696 if (*ret & HASSUBDIRS)
697 return DIRENT_ABORT;
698 }
699 if (TSTINO(dp->inode, dumpdirmap)) {
700 *ret |= HASSUBDIRS;
701 if (*ret & HASDUMPEDFILE)
702 return DIRENT_ABORT;
703 }
704 }
705 return 0;
706 }
707
708 #else /* __linux__ */
709
710 static int
711 searchdir(dump_ino_t ino, daddr_t blkno, long size, long filesize)
712 {
713 register struct direct *dp;
714 register long loc, ret = 0;
715 char dblk[MAXBSIZE];
716
717 bread(fsbtodb(sblock, blkno), dblk, (int)size);
718 if (filesize < size)
719 size = filesize;
720 for (loc = 0; loc < size; ) {
721 dp = (struct direct *)(dblk + loc);
722 if (dp->d_reclen == 0) {
723 msg("corrupted directory, inumber %d\n", ino);
724 break;
725 }
726 loc += dp->d_reclen;
727 if (dp->d_ino == 0)
728 continue;
729 if (dp->d_name[0] == '.') {
730 if (dp->d_name[1] == '\0')
731 continue;
732 if (dp->d_name[1] == '.' && dp->d_name[2] == '\0')
733 continue;
734 }
735 if (TSTINO(dp->d_ino, dumpinomap)) {
736 ret |= HASDUMPEDFILE;
737 if (ret & HASSUBDIRS)
738 break;
739 }
740 if (TSTINO(dp->d_ino, dumpdirmap)) {
741 ret |= HASSUBDIRS;
742 if (ret & HASDUMPEDFILE)
743 break;
744 }
745 }
746 return (ret);
747 }
748 #endif /* __linux__ */
749
750 #ifdef __linux__
751
752 struct block_context {
753 ext2_ino_t ino;
754 int *buf;
755 int cnt;
756 int max;
757 int next_block;
758 };
759
760 /*
761 * Dump a block to the tape
762 */
763 static int
764 dumponeblock(ext2_filsys fs, blk_t *blocknr, e2_blkcnt_t blockcnt,
765 blk_t ref_block, int ref_offset, void * private)
766 {
767 struct block_context *p;
768 int i;
769
770 if (blockcnt < NDADDR)
771 return 0;
772 p = (struct block_context *)private;
773 for (i = p->next_block; i < blockcnt; i++) {
774 p->buf[p->cnt++] = 0;
775 if (p->cnt == p->max) {
776 blksout (p->buf, p->cnt, p->ino);
777 p->cnt = 0;
778 }
779 }
780 p->buf[p->cnt++] = *blocknr;
781 if (p->cnt == p->max) {
782 blksout (p->buf, p->cnt, p->ino);
783 p->cnt = 0;
784 }
785 p->next_block = blockcnt + 1;
786 return 0;
787 }
788 #endif
789
790 /*
791 * Dump passes 3 and 4.
792 *
793 * Dump the contents of an inode to tape.
794 */
795 void
796 dumpino(struct dinode *dp, dump_ino_t ino)
797 {
798 unsigned long cnt;
799 fsizeT size;
800 char buf[TP_BSIZE];
801 struct old_bsd_inode obi;
802 #ifdef __linux__
803 struct block_context bc;
804 #else
805 int ind_level;
806 #endif
807 u_quad_t i_size = dp->di_size + ((u_quad_t) dp->di_size_high << 32);
808
809 if (newtape) {
810 newtape = 0;
811 dumpmap(dumpinomap, TS_BITS, ino);
812 }
813 CLRINO(ino, dumpinomap);
814 #ifdef __linux__
815 memset(&obi, 0, sizeof(obi));
816 obi.di_mode = dp->di_mode;
817 obi.di_uid = dp->di_uid;
818 obi.di_gid = dp->di_gid;
819 obi.di_qsize.v = i_size;
820 obi.di_atime = dp->di_atime;
821 obi.di_mtime = dp->di_mtime;
822 obi.di_ctime = dp->di_ctime;
823 obi.di_nlink = dp->di_nlink;
824 obi.di_blocks = dp->di_blocks;
825 obi.di_flags = dp->di_flags;
826 obi.di_gen = dp->di_gen;
827 memmove(&obi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
828 if (dp->di_file_acl)
829 warn("ACLs in inode #%ld won't be dumped", (long)ino);
830 memmove(&spcl.c_dinode, &obi, sizeof(obi));
831 #else /* __linux__ */
832 spcl.c_dinode = *dp;
833 #endif /* __linux__ */
834 spcl.c_type = TS_INODE;
835 spcl.c_count = 0;
836 switch (dp->di_mode & S_IFMT) {
837
838 case 0:
839 /*
840 * Freed inode.
841 */
842 return;
843
844 #ifdef __linux__
845 case S_IFDIR:
846 msg("Warning: dumpino called on a directory (ino %d)\n", ino);
847 return;
848 #endif
849
850 case S_IFLNK:
851 /*
852 * Check for short symbolic link.
853 */
854 #ifdef __linux__
855 if (i_size > 0 &&
856 i_size < EXT2_N_BLOCKS * sizeof (daddr_t)) {
857 spcl.c_addr[0] = 1;
858 spcl.c_count = 1;
859 writeheader(ino);
860 memmove(buf, dp->di_db, (u_long)dp->di_size);
861 buf[dp->di_size] = '\0';
862 writerec(buf, 0);
863 return;
864 }
865 #endif /* __linux__ */
866 #ifdef FS_44INODEFMT
867 if (dp->di_size > 0 &&
868 dp->di_size < sblock->fs_maxsymlinklen) {
869 spcl.c_addr[0] = 1;
870 spcl.c_count = 1;
871 writeheader(ino);
872 memmove(buf, dp->di_shortlink, (u_long)dp->di_size);
873 buf[dp->di_size] = '\0';
874 writerec(buf, 0);
875 return;
876 }
877 #endif
878 /* fall through */
879
880 #ifndef __linux__
881 case S_IFDIR:
882 #endif
883 case S_IFREG:
884 if (i_size)
885 break;
886 /* fall through */
887
888 case S_IFIFO:
889 case S_IFSOCK:
890 case S_IFCHR:
891 case S_IFBLK:
892 writeheader(ino);
893 return;
894
895 default:
896 msg("Warning: undefined file type 0%o\n", dp->di_mode & IFMT);
897 return;
898 }
899 if (i_size > NDADDR * sblock->fs_bsize)
900 #ifdef __linux__
901 cnt = NDADDR * EXT2_FRAGS_PER_BLOCK(fs->super);
902 #else
903 cnt = NDADDR * sblock->fs_frag;
904 #endif
905 else
906 cnt = howmany(i_size, sblock->fs_fsize);
907 blksout(&dp->di_db[0], cnt, ino);
908 if ((quad_t) (size = i_size - NDADDR * sblock->fs_bsize) <= 0)
909 return;
910 #ifdef __linux__
911 bc.max = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
912 bc.buf = (int *)malloc (bc.max * sizeof (int));
913 bc.cnt = 0;
914 bc.ino = ino;
915 bc.next_block = NDADDR;
916
917 ext2fs_block_iterate2(fs, (ext2_ino_t)ino, 0, NULL, dumponeblock, (void *)&bc);
918 if (bc.cnt > 0) {
919 blksout (bc.buf, bc.cnt, bc.ino);
920 }
921 free(bc.buf);
922 #else
923 for (ind_level = 0; ind_level < NIADDR; ind_level++) {
924 dmpindir(ino, dp->di_ib[ind_level], ind_level, &size);
925 if (size <= 0)
926 return;
927 }
928 #endif
929 }
930
931 #ifdef __linux__
932
933 struct convert_dir_context {
934 char *buf;
935 int prev_offset;
936 int offset;
937 int bs;
938 };
939
940 /*
941 * This function converts an ext2fs directory entry to the BSD format.
942 *
943 * Basically, it adds a null-character at the end of the name, recomputes the
944 * size of the entry, and creates it in a temporary buffer
945 */
946 static int
947 convert_dir(struct ext2_dir_entry *dirent, int offset, int blocksize, char *buf, void *private)
948 {
949 struct convert_dir_context *p;
950 struct olddirect *dp;
951 int reclen;
952
953 p = (struct convert_dir_context *)private;
954
955 reclen = EXT2_DIR_REC_LEN((dirent->name_len & 0xFF) + 1);
956 if (((p->offset + reclen - 1) / p->bs) != (p->offset / p->bs)) {
957 dp = (struct olddirect *)(p->buf + p->prev_offset);
958 dp->d_reclen += p->bs - (p->offset % p->bs);
959 p->offset += p->bs - (p->offset % p->bs);
960 }
961
962 dp = (struct olddirect *)(p->buf + p->offset);
963 dp->d_ino = dirent->inode;
964 dp->d_reclen = reclen;
965 dp->d_namlen = dirent->name_len & 0xFF;
966 strncpy(dp->d_name, dirent->name, dp->d_namlen);
967 dp->d_name[dp->d_namlen] = '\0';
968 p->prev_offset = p->offset;
969 p->offset += reclen;
970
971 return 0;
972 }
973
974 /*
975 * Dump pass 3
976 *
977 * Dumps a directory to tape after converting it to the BSD format
978 */
979 void
980 dumpdirino(struct dinode *dp, dump_ino_t ino)
981 {
982 fsizeT size;
983 char buf[TP_BSIZE];
984 struct old_bsd_inode obi;
985 struct convert_dir_context cdc;
986 errcode_t retval;
987 struct ext2_dir_entry *de;
988 fsizeT dir_size;
989
990 if (newtape) {
991 newtape = 0;
992 dumpmap(dumpinomap, TS_BITS, ino);
993 }
994 CLRINO(ino, dumpinomap);
995
996 /*
997 * Convert the directory to the BSD format
998 */
999 /* Allocate a buffer for the conversion (twice the size of the
1000 ext2fs directory to avoid problems ;-) */
1001 cdc.buf = (char *)malloc(dp->di_size * 2 * sizeof(char));
1002 if (cdc.buf == NULL)
1003 err(1, "Cannot allocate buffer to convert directory #%lu\n",
1004 (unsigned long)ino);
1005 cdc.offset = 0;
1006 cdc.prev_offset = 0;
1007 cdc.bs = MIN(DIRBLKSIZ, TP_BSIZE);
1008 /* Do the conversion */
1009 retval = ext2fs_dir_iterate(fs, (ext2_ino_t)ino, 0, NULL, convert_dir, (void *)&cdc);
1010 if (retval) {
1011 com_err(disk, retval, "while converting directory #%ld\n", (long)ino);
1012 exit(X_ABORT);
1013 }
1014 /* Fix the last entry */
1015 if ((cdc.offset % cdc.bs) != 0) {
1016 de = (struct ext2_dir_entry *)(cdc.buf + cdc.prev_offset);
1017 de->rec_len += cdc.bs - (cdc.offset % cdc.bs);
1018 cdc.offset += cdc.bs - (cdc.offset % cdc.bs);
1019 }
1020
1021 dir_size = cdc.offset;
1022
1023 #ifdef __linux__
1024 memset(&obi, 0, sizeof(obi));
1025 obi.di_mode = dp->di_mode;
1026 obi.di_uid = dp->di_uid;
1027 obi.di_gid = dp->di_gid;
1028 obi.di_qsize.v = dir_size; /* (u_quad_t)dp->di_size; */
1029 obi.di_atime = dp->di_atime;
1030 obi.di_mtime = dp->di_mtime;
1031 obi.di_ctime = dp->di_ctime;
1032 obi.di_nlink = dp->di_nlink;
1033 obi.di_blocks = dp->di_blocks;
1034 obi.di_flags = dp->di_flags;
1035 obi.di_gen = dp->di_gen;
1036 memmove(&obi.di_db, &dp->di_db, (NDADDR + NIADDR) * sizeof(daddr_t));
1037 if (dp->di_file_acl)
1038 warn("ACLs in inode #%ld won't be dumped", (long)ino);
1039 memmove(&spcl.c_dinode, &obi, sizeof(obi));
1040 #else /* __linux__ */
1041 spcl.c_dinode = *dp;
1042 #endif /* __linux__ */
1043 spcl.c_type = TS_INODE;
1044 spcl.c_count = 0;
1045 switch (dp->di_mode & S_IFMT) {
1046
1047 case 0:
1048 /*
1049 * Freed inode.
1050 */
1051 return;
1052
1053 case S_IFDIR:
1054 if (dir_size > 0)
1055 break;
1056 msg("Warning: size of directory inode #%d is <= 0 (%d)!\n",
1057 ino, dir_size);
1058 return;
1059
1060 default:
1061 msg("Warning: dumpdirino called with file type 0%o (inode #%d)\n",
1062 dp->di_mode & IFMT, ino);
1063 return;
1064 }
1065 for (size = 0; size < dir_size; size += TP_BSIZE) {
1066 spcl.c_addr[0] = 1;
1067 spcl.c_count = 1;
1068 writeheader(ino);
1069 memmove(buf, cdc.buf + size, TP_BSIZE);
1070 writerec(buf, 0);
1071 spcl.c_type = TS_ADDR;
1072 }
1073
1074 (void)free(cdc.buf);
1075 }
1076 #endif /* __linux__ */
1077
1078 #ifndef __linux__
1079 /*
1080 * Read indirect blocks, and pass the data blocks to be dumped.
1081 */
1082 static void
1083 dmpindir(dump_ino_t ino, daddr_t blk, int ind_level, fsizeT *size)
1084 {
1085 int i, cnt;
1086 #ifdef __linux__
1087 int max;
1088 blk_t *swapme;
1089 #endif
1090 daddr_t idblk[MAXNINDIR];
1091
1092 if (blk != 0) {
1093 bread(fsbtodb(sblock, blk), (char *)idblk, (int) sblock->fs_bsize);
1094 #ifdef __linux__
1095 /*
1096 * My RedHat 4.0 system doesn't have these flags; I haven't
1097 * upgraded e2fsprogs yet
1098 */
1099 #if defined(EXT2_FLAG_SWAP_BYTES)
1100 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ||
1101 (fs->flags & EXT2_FLAG_SWAP_BYTES_READ))
1102 #endif
1103 {
1104 max = sblock->fs_bsize >> 2;
1105 swapme = (blk_t *) idblk;
1106 for (i = 0; i < max; i++, swapme++)
1107 *swapme = ext2fs_swab32(*swapme);
1108 }
1109 #endif /* __linux__ */
1110 else
1111 memset(idblk, 0, (int)sblock->fs_bsize);
1112 if (ind_level <= 0) {
1113 if (*size < NINDIR(sblock) * sblock->fs_bsize)
1114 cnt = howmany(*size, sblock->fs_fsize);
1115 else
1116 #ifdef __linux__
1117 cnt = NINDIR(sblock) * EXT2_FRAGS_PER_BLOCK(fs->super);
1118 #else
1119 cnt = NINDIR(sblock) * sblock->fs_frag;
1120 #endif
1121 *size -= NINDIR(sblock) * sblock->fs_bsize;
1122 blksout(&idblk[0], cnt, ino);
1123 return;
1124 }
1125 ind_level--;
1126 for (i = 0; i < NINDIR(sblock); i++) {
1127 dmpindir(ino, idblk[i], ind_level, size);
1128 if (*size <= 0)
1129 return;
1130 }
1131 }
1132 #endif
1133
1134 /*
1135 * Collect up the data into tape record sized buffers and output them.
1136 */
1137 void
1138 blksout(daddr_t *blkp, int frags, dump_ino_t ino)
1139 {
1140 register daddr_t *bp;
1141 int i, j, count, blks, tbperdb;
1142
1143 blks = howmany(frags * sblock->fs_fsize, TP_BSIZE);
1144 tbperdb = sblock->fs_bsize >> tp_bshift;
1145 for (i = 0; i < blks; i += TP_NINDIR) {
1146 if (i + TP_NINDIR > blks)
1147 count = blks;
1148 else
1149 count = i + TP_NINDIR;
1150 for (j = i; j < count; j++)
1151 if (blkp[j / tbperdb] != 0)
1152 spcl.c_addr[j - i] = 1;
1153 else
1154 spcl.c_addr[j - i] = 0;
1155 spcl.c_count = count - i;
1156 writeheader(ino);
1157 bp = &blkp[i / tbperdb];
1158 for (j = i; j < count; j += tbperdb, bp++) {
1159 if (*bp != 0) {
1160 if (j + tbperdb <= count)
1161 dumpblock(*bp, (int)sblock->fs_bsize);
1162 else
1163 dumpblock(*bp, (count - j) * TP_BSIZE);
1164 }
1165 }
1166 spcl.c_type = TS_ADDR;
1167 }
1168 }
1169
1170 /*
1171 * Dump a map to the tape.
1172 */
1173 void
1174 dumpmap(char *map, int type, dump_ino_t ino)
1175 {
1176 register int i;
1177 char *cp;
1178
1179 spcl.c_type = type;
1180 spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE);
1181 writeheader(ino);
1182 for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE)
1183 writerec(cp, 0);
1184 }
1185
1186 /*
1187 * Write a header record to the dump tape.
1188 */
1189 #if defined __linux__ && !defined(int32_t)
1190 #define int32_t __s32
1191 #endif
1192 void
1193 writeheader(dump_ino_t ino)
1194 {
1195 register int32_t sum, cnt, *lp;
1196
1197 spcl.c_inumber = ino;
1198 spcl.c_magic = NFS_MAGIC;
1199 spcl.c_checksum = 0;
1200 lp = (int32_t *)&spcl;
1201 sum = 0;
1202 cnt = sizeof(union u_spcl) / (4 * sizeof(int32_t));
1203 while (--cnt >= 0) {
1204 sum += *lp++;
1205 sum += *lp++;
1206 sum += *lp++;
1207 sum += *lp++;
1208 }
1209 spcl.c_checksum = CHECKSUM - sum;
1210 writerec((char *)&spcl, 1);
1211 }
1212
1213 #ifdef __linux__
1214 struct dinode *
1215 getino(dump_ino_t inum)
1216 {
1217 static struct dinode dinode;
1218 errcode_t err;
1219
1220 curino = inum;
1221 err = ext2fs_read_inode(fs, (ext2_ino_t)inum, (struct ext2_inode *) &dinode);
1222 if (err) {
1223 com_err(disk, err, "while reading inode #%ld\n", (long)inum);
1224 exit(X_ABORT);
1225 }
1226 return &dinode;
1227 }
1228 #else /* __linux__ */
1229 struct dinode *
1230 getino(dump_ino_t inum)
1231 {
1232 static daddr_t minino, maxino;
1233 static struct dinode inoblock[MAXINOPB];
1234
1235 curino = inum;
1236 if (inum >= minino && inum < maxino)
1237 return (&inoblock[inum - minino]);
1238 bread(fsbtodb(sblock, ino_to_fsba(sblock, inum)), (char *)inoblock,
1239 (int)sblock->fs_bsize);
1240 minino = inum - (inum % INOPB(sblock));
1241 maxino = minino + INOPB(sblock);
1242 return (&inoblock[inum - minino]);
1243 }
1244 #endif /* __linux__ */
1245
1246 /*
1247 * Read a chunk of data from the disk.
1248 * Try to recover from hard errors by reading in sector sized pieces.
1249 * Error recovery is attempted at most BREADEMAX times before seeking
1250 * consent from the operator to continue.
1251 */
1252 int breaderrors = 0;
1253 #define BREADEMAX 32
1254
1255 void
1256 bread(daddr_t blkno, char *buf, int size)
1257 {
1258 int cnt, i;
1259 extern int errno;
1260
1261 loop:
1262 #ifdef __linux__
1263 if (ext2fs_llseek(diskfd, (((ext2_loff_t)blkno) << dev_bshift), 0) !=
1264 (((ext2_loff_t)blkno) << dev_bshift))
1265 #else
1266 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1267 ((off_t)blkno << dev_bshift))
1268 #endif
1269 msg("bread: lseek fails\n");
1270 if ((cnt = read(diskfd, buf, size)) == size)
1271 return;
1272 if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) {
1273 /*
1274 * Trying to read the final fragment.
1275 *
1276 * NB - dump only works in TP_BSIZE blocks, hence
1277 * rounds `dev_bsize' fragments up to TP_BSIZE pieces.
1278 * It should be smarter about not actually trying to
1279 * read more than it can get, but for the time being
1280 * we punt and scale back the read only when it gets
1281 * us into trouble. (mkm 9/25/83)
1282 */
1283 size -= dev_bsize;
1284 goto loop;
1285 }
1286 if (cnt == -1)
1287 msg("read error from %s: %s: [block %d]: count=%d\n",
1288 disk, strerror(errno), blkno, size);
1289 else
1290 msg("short read error from %s: [block %d]: count=%d, got=%d\n",
1291 disk, blkno, size, cnt);
1292 if (++breaderrors > BREADEMAX) {
1293 msg("More than %d block read errors from %d\n",
1294 BREADEMAX, disk);
1295 broadcast("DUMP IS AILING!\n");
1296 msg("This is an unrecoverable error.\n");
1297 if (!query("Do you want to attempt to continue?")){
1298 dumpabort(0);
1299 /*NOTREACHED*/
1300 } else
1301 breaderrors = 0;
1302 }
1303 /*
1304 * Zero buffer, then try to read each sector of buffer separately.
1305 */
1306 memset(buf, 0, size);
1307 for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) {
1308 #ifdef __linux__
1309 if (ext2fs_llseek(diskfd, (((ext2_loff_t)blkno) << dev_bshift), 0) !=
1310 (((ext2_loff_t)blkno) << dev_bshift))
1311 #else
1312 if (lseek(diskfd, ((off_t)blkno << dev_bshift), 0) !=
1313 ((off_t)blkno << dev_bshift))
1314 #endif
1315 msg("bread: lseek2 fails!\n");
1316 if ((cnt = read(diskfd, buf, (int)dev_bsize)) == dev_bsize)
1317 continue;
1318 if (cnt == -1) {
1319 msg("read error from %s: %s: [sector %d]: count=%d\n",
1320 disk, strerror(errno), blkno, dev_bsize);
1321 continue;
1322 }
1323 msg("short read error from %s: [sector %d]: count=%d, got=%d\n",
1324 disk, blkno, dev_bsize, cnt);
1325 }
1326 }