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