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