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