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