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