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