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