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