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