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