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