147082Smckusick /*- 247082Smckusick * Copyright (c) 1980, 1988, 1991 The Regents of the University of California. 347082Smckusick * All rights reserved. 447082Smckusick * 547082Smckusick * %sccs.include.redist.c% 622041Sdist */ 75329Smckusic 822041Sdist #ifndef lint 9*51605Sbostic static char sccsid[] = "@(#)traverse.c 5.15 (Berkeley) 11/10/91"; 1046584Storek #endif /* not lint */ 1122041Sdist 1250499Smckusick #ifdef sunos 1350499Smckusick #include <stdio.h> 1450499Smckusick #include <ctype.h> 1546795Sbostic #include <sys/param.h> 1650499Smckusick #include <sys/stat.h> 1750499Smckusick #include <sys/time.h> 1850499Smckusick #include <sys/dir.h> 1950499Smckusick #include <sys/vnode.h> 2050499Smckusick #include <ufs/inode.h> 21*51605Sbostic #include <ufs/fs.h> 2250499Smckusick #else 2350499Smckusick #include <sys/param.h> 24*51605Sbostic #include <ufs/ufs/dir.h> 25*51605Sbostic #include <ufs/ufs/dinode.h> 26*51605Sbostic #include <ufs/ffs/fs.h> 2750499Smckusick #endif 2846795Sbostic #include <protocols/dumprestore.h> 2946795Sbostic #ifdef __STDC__ 3046795Sbostic #include <unistd.h> 3146795Sbostic #include <string.h> 3246795Sbostic #endif 331426Sroot #include "dump.h" 341426Sroot 3546792Smckusick void dmpindir(); 3646792Smckusick #define HASDUMPEDFILE 0x1 3746792Smckusick #define HASSUBDIRS 0x2 3846584Storek 3946584Storek /* 4046584Storek * This is an estimation of the number of TP_BSIZE blocks in the file. 4146584Storek * It estimates the number of blocks in files with holes by assuming 4246584Storek * that all of the blocks accounted for by di_blocks are data blocks 4346584Storek * (when some of the blocks are usually used for indirect pointers); 4446584Storek * hence the estimate may be high. 4546584Storek */ 4646792Smckusick long 4747058Smckusick blockest(dp) 4847058Smckusick register struct dinode *dp; 4946584Storek { 5046792Smckusick long blkest, sizeest; 5146584Storek 5246584Storek /* 5347058Smckusick * dp->di_size is the size of the file in bytes. 5447058Smckusick * dp->di_blocks stores the number of sectors actually in the file. 5546584Storek * If there are more sectors than the size would indicate, this just 5646584Storek * means that there are indirect blocks in the file or unused 5746584Storek * sectors in the last file block; we can safely ignore these 5846792Smckusick * (blkest = sizeest below). 5946584Storek * If the file is bigger than the number of sectors would indicate, 6046584Storek * then the file has holes in it. In this case we must use the 6146584Storek * block count to estimate the number of data blocks used, but 6246584Storek * we use the actual size for estimating the number of indirect 6346792Smckusick * dump blocks (sizeest vs. blkest in the indirect block 6446792Smckusick * calculation). 6546584Storek */ 6647058Smckusick blkest = howmany(dbtob(dp->di_blocks), TP_BSIZE); 6747058Smckusick sizeest = howmany(dp->di_size, TP_BSIZE); 6846792Smckusick if (blkest > sizeest) 6946792Smckusick blkest = sizeest; 7047058Smckusick if (dp->di_size > sblock->fs_bsize * NDADDR) { 7146584Storek /* calculate the number of indirect blocks on the dump tape */ 7246792Smckusick blkest += 7346792Smckusick howmany(sizeest - NDADDR * sblock->fs_bsize / TP_BSIZE, 7446584Storek TP_NINDIR); 7546584Storek } 7646792Smckusick return (blkest + 1); 7746584Storek } 7846584Storek 7946792Smckusick /* 8046792Smckusick * Dump pass 1. 8146792Smckusick * 8246792Smckusick * Walk the inode list for a filesystem to find all allocated inodes 8346792Smckusick * that have been modified since the previous dump time. Also, find all 8446792Smckusick * the directories in the filesystem. 8546792Smckusick */ 8646792Smckusick mapfiles(maxino, tapesize) 8746792Smckusick ino_t maxino; 8846792Smckusick long *tapesize; 8946584Storek { 9046792Smckusick register int mode; 9146792Smckusick register ino_t ino; 9246792Smckusick register struct dinode *dp; 9346792Smckusick int anydirskipped = 0; 9446584Storek 9546792Smckusick for (ino = 0; ino < maxino; ino++) { 9646792Smckusick dp = getino(ino); 9746792Smckusick if ((mode = (dp->di_mode & IFMT)) == 0) 9846792Smckusick continue; 9946792Smckusick SETINO(ino, usedinomap); 10046792Smckusick if (mode == IFDIR) 10146792Smckusick SETINO(ino, dumpdirmap); 10246792Smckusick if (dp->di_mtime >= spcl.c_ddate || 10346792Smckusick dp->di_ctime >= spcl.c_ddate) { 10446792Smckusick SETINO(ino, dumpinomap); 10546792Smckusick if (mode != IFREG && mode != IFDIR && mode != IFLNK) { 10646792Smckusick *tapesize += 1; 10746792Smckusick continue; 10846792Smckusick } 10946792Smckusick *tapesize += blockest(dp); 11046792Smckusick continue; 11146792Smckusick } 11246792Smckusick if (mode == IFDIR) 11346792Smckusick anydirskipped = 1; 11446792Smckusick } 11546792Smckusick /* 11646792Smckusick * Restore gets very upset if the root is not dumped, 11746792Smckusick * so ensure that it always is dumped. 11846792Smckusick */ 11951374Smckusick SETINO(ROOTINO, dumpinomap); 12046792Smckusick return (anydirskipped); 12146584Storek } 12246584Storek 12346792Smckusick /* 12446792Smckusick * Dump pass 2. 12546792Smckusick * 12646792Smckusick * Scan each directory on the filesystem to see if it has any modified 12746792Smckusick * files in it. If it does, and has not already been added to the dump 12846792Smckusick * list (because it was itself modified), then add it. If a directory 12946792Smckusick * has not been modified itself, contains no modified files and has no 13046792Smckusick * subdirectories, then it can be deleted from the dump list and from 13146792Smckusick * the list of directories. By deleting it from the list of directories, 13246792Smckusick * its parent may now qualify for the same treatment on this or a later 13346792Smckusick * pass using this algorithm. 13446792Smckusick */ 13546792Smckusick mapdirs(maxino, tapesize) 13646792Smckusick ino_t maxino; 13746792Smckusick long *tapesize; 13846792Smckusick { 13946792Smckusick register struct dinode *dp; 14047058Smckusick register int i, dirty; 14125797Smckusick register char *map; 14246792Smckusick register ino_t ino; 14346792Smckusick long filesize, blkcnt = 0; 14446792Smckusick int ret, change = 0; 1451426Sroot 14646792Smckusick for (map = dumpdirmap, ino = 0; ino < maxino; ) { 14746584Storek if ((ino % NBBY) == 0) 14847058Smckusick dirty = *map++; 14946792Smckusick else 15047058Smckusick dirty >>= 1; 1514702Smckusic ino++; 15247058Smckusick if ((dirty & 1) == 0 || TSTINO(ino, dumpinomap)) 15346792Smckusick continue; 15446792Smckusick dp = getino(ino); 15546792Smckusick filesize = dp->di_size; 15646792Smckusick for (ret = 0, i = 0; filesize > 0 && i < NDADDR; i++) { 15746792Smckusick if (dp->di_db[i] != 0) 15846792Smckusick ret |= searchdir(ino, dp->di_db[i], 15946796Smckusick dblksize(sblock, dp, i), filesize); 16046792Smckusick if (ret & HASDUMPEDFILE) 16146792Smckusick filesize = 0; 16246792Smckusick else 16346792Smckusick filesize -= sblock->fs_bsize; 16446792Smckusick } 16546792Smckusick for (i = 0; filesize > 0 && i < NIADDR; i++) { 16646792Smckusick if (dp->di_ib[i] == 0) 16746792Smckusick continue; 16846792Smckusick ret |= dirindir(ino, dp->di_ib[i], i, &filesize); 16946792Smckusick } 17046792Smckusick if (ret & HASDUMPEDFILE) { 17146792Smckusick if (!TSTINO(ino, dumpinomap)) { 17246792Smckusick SETINO(ino, dumpinomap); 17346792Smckusick *tapesize += blockest(dp); 17446792Smckusick } 17546792Smckusick change = 1; 17646792Smckusick continue; 17746792Smckusick } 17846792Smckusick if ((ret & HASSUBDIRS) == 0) { 17946792Smckusick if (!TSTINO(ino, dumpinomap)) { 18046792Smckusick CLRINO(ino, dumpdirmap); 18146792Smckusick change = 1; 18246792Smckusick } 18346792Smckusick } 1841426Sroot } 18546792Smckusick return (change); 1861426Sroot } 1871426Sroot 18846792Smckusick /* 18946792Smckusick * Read indirect blocks, and pass the data blocks to be searched 19046792Smckusick * as directories. Quit as soon as any entry is found that will 19146792Smckusick * require the directory to be dumped. 19246792Smckusick */ 19346792Smckusick dirindir(ino, blkno, level, filesize) 19446792Smckusick ino_t ino; 19546792Smckusick daddr_t blkno; 19646792Smckusick int level, *filesize; 1971426Sroot { 19846792Smckusick int ret = 0; 19946792Smckusick register int i; 20046792Smckusick daddr_t idblk[MAXNINDIR]; 2011426Sroot 20246792Smckusick bread(fsbtodb(sblock, blkno), (char *)idblk, sblock->fs_bsize); 20346792Smckusick if (level <= 0) { 20446792Smckusick for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) { 20546792Smckusick blkno = idblk[i]; 20646792Smckusick if (blkno != 0) 20746796Smckusick ret |= searchdir(ino, blkno, sblock->fs_bsize, 20846796Smckusick filesize); 20946792Smckusick if (ret & HASDUMPEDFILE) 21046792Smckusick *filesize = 0; 21146792Smckusick else 21246792Smckusick *filesize -= sblock->fs_bsize; 2131426Sroot } 21446792Smckusick return (ret); 2155329Smckusic } 21646792Smckusick level--; 21746792Smckusick for (i = 0; *filesize > 0 && i < NINDIR(sblock); i++) { 21846792Smckusick blkno = idblk[i]; 21946792Smckusick if (blkno != 0) 22046792Smckusick ret |= dirindir(ino, blkno, level, filesize); 2215329Smckusic } 22246792Smckusick return (ret); 2231426Sroot } 2241426Sroot 22546792Smckusick /* 22646792Smckusick * Scan a disk block containing directory information looking to see if 22746792Smckusick * any of the entries are on the dump list and to see if the directory 22846792Smckusick * contains any subdirectories. 22946792Smckusick */ 23046796Smckusick searchdir(ino, blkno, size, filesize) 23146792Smckusick ino_t ino; 23246792Smckusick daddr_t blkno; 23346792Smckusick register int size; 23446796Smckusick int filesize; 2355329Smckusic { 23646792Smckusick register struct direct *dp; 23746792Smckusick register long loc; 23846792Smckusick char dblk[MAXBSIZE]; 2395329Smckusic 24046792Smckusick bread(fsbtodb(sblock, blkno), dblk, size); 24146796Smckusick if (filesize < size) 24246796Smckusick size = filesize; 24346792Smckusick for (loc = 0; loc < size; ) { 24446792Smckusick dp = (struct direct *)(dblk + loc); 24546792Smckusick if (dp->d_reclen == 0) { 24646792Smckusick msg("corrupted directory, inumber %d\n", ino); 24746792Smckusick break; 2485329Smckusic } 24946792Smckusick loc += dp->d_reclen; 25046792Smckusick if (dp->d_ino == 0) 25146792Smckusick continue; 25246792Smckusick if (dp->d_name[0] == '.') { 25346792Smckusick if (dp->d_name[1] == '\0') 25446792Smckusick continue; 25546792Smckusick if (dp->d_name[1] == '.' && dp->d_name[2] == '\0') 25646792Smckusick continue; 2575329Smckusic } 25846792Smckusick if (TSTINO(dp->d_ino, dumpinomap)) 25946792Smckusick return (HASDUMPEDFILE); 26046792Smckusick if (TSTINO(dp->d_ino, dumpdirmap)) 26146792Smckusick return (HASSUBDIRS); 2625329Smckusic } 26346792Smckusick return (0); 2645329Smckusic } 2655329Smckusic 26646792Smckusick /* 26746792Smckusick * Dump passes 3 and 4. 26846792Smckusick * 26946792Smckusick * Dump the contents of an inode to tape. 27046792Smckusick */ 27146584Storek void 27247058Smckusick dumpino(dp, ino) 27347058Smckusick register struct dinode *dp; 27446792Smckusick ino_t ino; 27517234Smckusick { 27646792Smckusick int mode, level, cnt; 2774777Smckusic long size; 2781426Sroot 27946792Smckusick if (newtape) { 2801426Sroot newtape = 0; 28146792Smckusick dumpmap(dumpinomap, TS_BITS, ino); 2821426Sroot } 28346792Smckusick CLRINO(ino, dumpinomap); 28447058Smckusick spcl.c_dinode = *dp; 2851426Sroot spcl.c_type = TS_INODE; 2861426Sroot spcl.c_count = 0; 28746792Smckusick /* 28846792Smckusick * Check for freed inode. 28946792Smckusick */ 29047058Smckusick if ((mode = (dp->di_mode & IFMT)) == 0) 29117234Smckusick return; 29246792Smckusick if ((mode != IFDIR && mode != IFREG && mode != IFLNK) || 29347058Smckusick dp->di_size == 0) { 29446792Smckusick writeheader(ino); 2951426Sroot return; 2961426Sroot } 29747058Smckusick if (dp->di_size > NDADDR * sblock->fs_bsize) 29846792Smckusick cnt = NDADDR * sblock->fs_frag; 2994777Smckusic else 30047058Smckusick cnt = howmany(dp->di_size, sblock->fs_fsize); 30147058Smckusick blksout(&dp->di_db[0], cnt, ino); 30247058Smckusick if ((size = dp->di_size - NDADDR * sblock->fs_bsize) <= 0) 3034777Smckusic return; 30446792Smckusick for (level = 0; level < NIADDR; level++) { 30547058Smckusick dmpindir(ino, dp->di_ib[level], level, &size); 3064777Smckusic if (size <= 0) 3074777Smckusic return; 3084777Smckusic } 3091426Sroot } 3101426Sroot 31146792Smckusick /* 31246792Smckusick * Read indirect blocks, and pass the data blocks to be dumped. 31346792Smckusick */ 31446584Storek void 31546792Smckusick dmpindir(ino, blk, level, size) 31646792Smckusick ino_t ino; 3174777Smckusic daddr_t blk; 31846792Smckusick int level; 3194777Smckusic long *size; 3201426Sroot { 3214777Smckusic int i, cnt; 3225329Smckusic daddr_t idblk[MAXNINDIR]; 3231426Sroot 3244777Smckusic if (blk != 0) 3255329Smckusic bread(fsbtodb(sblock, blk), (char *)idblk, sblock->fs_bsize); 3264777Smckusic else 32746584Storek bzero((char *)idblk, sblock->fs_bsize); 32846792Smckusick if (level <= 0) { 3295329Smckusic if (*size < NINDIR(sblock) * sblock->fs_bsize) 3305329Smckusic cnt = howmany(*size, sblock->fs_fsize); 3314777Smckusic else 3325329Smckusic cnt = NINDIR(sblock) * sblock->fs_frag; 3335329Smckusic *size -= NINDIR(sblock) * sblock->fs_bsize; 33446792Smckusick blksout(&idblk[0], cnt, ino); 3354777Smckusic return; 3361426Sroot } 33746792Smckusick level--; 3385329Smckusic for (i = 0; i < NINDIR(sblock); i++) { 33946792Smckusick dmpindir(ino, idblk[i], level, size); 3404777Smckusic if (*size <= 0) 3414777Smckusic return; 3424777Smckusic } 3431426Sroot } 3441426Sroot 34546792Smckusick /* 34646792Smckusick * Collect up the data into tape record sized buffers and output them. 34746792Smckusick */ 34846584Storek void 34946792Smckusick blksout(blkp, frags, ino) 3504777Smckusic daddr_t *blkp; 3514777Smckusic int frags; 35246792Smckusick ino_t ino; 3534777Smckusic { 35446584Storek register daddr_t *bp; 3555329Smckusic int i, j, count, blks, tbperdb; 3564777Smckusic 3579403Smckusick blks = howmany(frags * sblock->fs_fsize, TP_BSIZE); 35846584Storek tbperdb = sblock->fs_bsize >> tp_bshift; 3594777Smckusic for (i = 0; i < blks; i += TP_NINDIR) { 3604777Smckusic if (i + TP_NINDIR > blks) 3614777Smckusic count = blks; 3624777Smckusic else 3634777Smckusic count = i + TP_NINDIR; 3644777Smckusic for (j = i; j < count; j++) 3655329Smckusic if (blkp[j / tbperdb] != 0) 3664777Smckusic spcl.c_addr[j - i] = 1; 3674777Smckusic else 3684777Smckusic spcl.c_addr[j - i] = 0; 3694777Smckusic spcl.c_count = count - i; 37046792Smckusick writeheader(ino); 37146584Storek bp = &blkp[i / tbperdb]; 37246584Storek for (j = i; j < count; j += tbperdb, bp++) 37346584Storek if (*bp != 0) 3745329Smckusic if (j + tbperdb <= count) 37546792Smckusick dumpblock(*bp, sblock->fs_bsize); 3764777Smckusic else 37746792Smckusick dumpblock(*bp, (count - j) * TP_BSIZE); 3784777Smckusic spcl.c_type = TS_ADDR; 3794777Smckusic } 3804777Smckusic } 3814777Smckusic 38246792Smckusick /* 38346792Smckusick * Dump a map to the tape. 38446792Smckusick */ 38546584Storek void 38646792Smckusick dumpmap(map, type, ino) 3875329Smckusic char *map; 38846792Smckusick int type; 38946792Smckusick ino_t ino; 3901426Sroot { 39146792Smckusick register int i; 3921426Sroot char *cp; 3931426Sroot 39446792Smckusick spcl.c_type = type; 39546792Smckusick spcl.c_count = howmany(mapsize * sizeof(char), TP_BSIZE); 39646792Smckusick writeheader(ino); 3975329Smckusic for (i = 0, cp = map; i < spcl.c_count; i++, cp += TP_BSIZE) 39846792Smckusick writerec(cp); 3991426Sroot } 4001426Sroot 40146792Smckusick /* 40246792Smckusick * Write a header record to the dump tape. 40346792Smckusick */ 40446584Storek void 40546792Smckusick writeheader(ino) 40646792Smckusick ino_t ino; 4071426Sroot { 40846792Smckusick register long sum, cnt, *lp; 4091426Sroot 4101426Sroot spcl.c_inumber = ino; 4118368Smckusick spcl.c_magic = NFS_MAGIC; 4121426Sroot spcl.c_checksum = 0; 41346792Smckusick lp = (long *)&spcl; 41446792Smckusick sum = 0; 41546792Smckusick cnt = sizeof(union u_spcl) / (4 * sizeof(long)); 41646792Smckusick while (--cnt >= 0) { 41746792Smckusick sum += *lp++; 41846792Smckusick sum += *lp++; 41946792Smckusick sum += *lp++; 42046792Smckusick sum += *lp++; 42124168Smckusick } 42246792Smckusick spcl.c_checksum = CHECKSUM - sum; 42346792Smckusick writerec((char *)&spcl); 4241426Sroot } 4251426Sroot 4264702Smckusic struct dinode * 42746792Smckusick getino(inum) 42846792Smckusick ino_t inum; 4294702Smckusic { 4304702Smckusic static daddr_t minino, maxino; 43146792Smckusick static struct dinode inoblock[MAXINOPB]; 4324702Smckusic 43346792Smckusick curino = inum; 43446792Smckusick if (inum >= minino && inum < maxino) 43546792Smckusick return (&inoblock[inum - minino]); 43646792Smckusick bread(fsbtodb(sblock, itod(sblock, inum)), inoblock, sblock->fs_bsize); 43746792Smckusick minino = inum - (inum % INOPB(sblock)); 4385329Smckusic maxino = minino + INOPB(sblock); 43946792Smckusick return (&inoblock[inum - minino]); 4404702Smckusic } 4414702Smckusic 44246792Smckusick /* 44346792Smckusick * Read a chunk of data from the disk. 44446792Smckusick * Try to recover from hard errors by reading in sector sized pieces. 44546792Smckusick * Error recovery is attempted at most BREADEMAX times before seeking 44646792Smckusick * consent from the operator to continue. 44746792Smckusick */ 4481426Sroot int breaderrors = 0; 4491426Sroot #define BREADEMAX 32 4501426Sroot 45146584Storek void 45246792Smckusick bread(blkno, buf, size) 45346792Smckusick daddr_t blkno; 45446792Smckusick char *buf; 45546792Smckusick int size; 4561426Sroot { 45746792Smckusick int cnt, i; 45834359Skarels extern int errno; 4591426Sroot 46015095Smckusick loop: 46146792Smckusick if (lseek(diskfd, (long)(blkno << dev_bshift), 0) < 0) 4621426Sroot msg("bread: lseek fails\n"); 46346792Smckusick if ((cnt = read(diskfd, buf, size)) == size) 46415095Smckusick return; 46546792Smckusick if (blkno + (size / dev_bsize) > fsbtodb(sblock, sblock->fs_size)) { 46615095Smckusick /* 46715095Smckusick * Trying to read the final fragment. 46815095Smckusick * 46915095Smckusick * NB - dump only works in TP_BSIZE blocks, hence 47030560Smckusick * rounds `dev_bsize' fragments up to TP_BSIZE pieces. 47115095Smckusick * It should be smarter about not actually trying to 47215095Smckusick * read more than it can get, but for the time being 47315095Smckusick * we punt and scale back the read only when it gets 47415095Smckusick * us into trouble. (mkm 9/25/83) 47515095Smckusick */ 47646792Smckusick size -= dev_bsize; 47715095Smckusick goto loop; 4781426Sroot } 47950905Smckusick if (cnt == -1) 48050905Smckusick msg("read error from %s: %s: [block %d]: count=%d\n", 48150905Smckusick disk, strerror(errno), blkno, size); 48250905Smckusick else 48350905Smckusick msg("short read error from %s: [block %d]: count=%d, got=%d\n", 48450905Smckusick disk, blkno, size, cnt); 48546584Storek if (++breaderrors > BREADEMAX) { 48615095Smckusick msg("More than %d block read errors from %d\n", 48715095Smckusick BREADEMAX, disk); 48815095Smckusick broadcast("DUMP IS AILING!\n"); 48915095Smckusick msg("This is an unrecoverable error.\n"); 49015095Smckusick if (!query("Do you want to attempt to continue?")){ 49115095Smckusick dumpabort(); 49215095Smckusick /*NOTREACHED*/ 49315095Smckusick } else 49415095Smckusick breaderrors = 0; 49515095Smckusick } 49634359Skarels /* 49734359Skarels * Zero buffer, then try to read each sector of buffer separately. 49834359Skarels */ 49946792Smckusick bzero(buf, size); 50046792Smckusick for (i = 0; i < size; i += dev_bsize, buf += dev_bsize, blkno++) { 50146792Smckusick if (lseek(diskfd, (long)(blkno << dev_bshift), 0) < 0) 50234359Skarels msg("bread: lseek2 fails!\n"); 50350905Smckusick if ((cnt = read(diskfd, buf, dev_bsize)) == dev_bsize) 50450905Smckusick continue; 50550905Smckusick if (cnt == -1) { 50650905Smckusick msg("read error from %s: %s: [sector %d]: count=%d\n", 50750905Smckusick disk, strerror(errno), blkno, dev_bsize); 50850905Smckusick continue; 50950905Smckusick } 51050905Smckusick msg("short read error from %s: [sector %d]: count=%d, got=%d\n", 51150905Smckusick disk, blkno, dev_bsize, cnt); 51234359Skarels } 5131426Sroot } 514