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