BSD 4_4_Lite2 release
[unix-history] / usr / src / libexec / lfs_cleanerd / library.c
index 3e2e861..32f7d37 100644 (file)
@@ -1,12 +1,38 @@
 /*-
 /*-
- * Copyright (c) 1992 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1992, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)library.c  5.8 (Berkeley) %G%";
+static char sccsid[] = "@(#)library.c  8.3 (Berkeley) 5/24/95";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -21,9 +47,10 @@ static char sccsid[] = "@(#)library.c        5.8 (Berkeley) %G%";
 
 #include <fcntl.h>
 #include <stdio.h>
 
 #include <fcntl.h>
 #include <stdio.h>
-#include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
+
 #include "clean.h"
 
 void    add_blocks __P((FS_INFO *, BLOCK_INFO *, int *, SEGSUM *, caddr_t,
 #include "clean.h"
 
 void    add_blocks __P((FS_INFO *, BLOCK_INFO *, int *, SEGSUM *, caddr_t,
@@ -37,66 +64,62 @@ int  get_superblock __P((FS_INFO *, struct lfs *));
 int     pseg_valid __P((FS_INFO *, SEGSUM *));
 
 /*
 int     pseg_valid __P((FS_INFO *, SEGSUM *));
 
 /*
- * This function will get information on all mounted file systems
- * of a given type.
+ * This function will get information on a a filesystem which matches
+ * the name and type given.  If a "name" is in a filesystem of the given
+ * type, then buf is filled with that filesystem's info, and the
+ * a non-zero value is returned.
  */
 int
  */
 int
-fs_getmntinfo(buf, type)
+fs_getmntinfo(buf, name, type)
        struct  statfs  **buf;
        struct  statfs  **buf;
-       int     type;
+       char    *name;
+       char    *type;
 {
 {
-       struct statfs *tstatfsp;
-       struct statfs *sbp;
-       int count, i, tcount;
-
-       tcount = getmntinfo(&tstatfsp, MNT_NOWAIT);
-
-       if (tcount < 0) {
-               err(0, "getmntinfo failed");
-               return (-1);
+       /* allocate space for the filesystem info */
+       *buf = (struct statfs *)malloc(sizeof(struct statfs));
+       if (*buf == NULL)
+               return 0;
+
+       /* grab the filesystem info */
+       if (statfs(name, *buf) < 0) {
+               free(*buf);
+               return 0;
        }
 
        }
 
-       for (count = 0, i = 0; i < tcount ; ++i)
-               if (tstatfsp[i].f_type == type)
-                       ++count;
-
-       if (count) {
-               if (!(*buf = (struct statfs *)
-                       malloc(count * sizeof(struct statfs))))
-                       err(1, "fs_getmntinfo: out of space");
-               for (i = 0, sbp = *buf; i < tcount ; ++i) {
-                       if (tstatfsp[i].f_type == type) {
-                               *sbp = tstatfsp[i];
-                               ++sbp;
-                       }
-               }
+       /* check to see if it's the one we want */
+       if (strcmp((*buf)->f_fstypename, type) ||
+           strncmp(name, (*buf)->f_mntonname, MNAMELEN)) {
+               /* "this is not the filesystem you're looking for */
+               free(*buf);
+               return 0;
        }
        }
-       return (count);
+
+       return 1;
 }
 
 /*
  * Get all the information available on an LFS file system.
 }
 
 /*
  * Get all the information available on an LFS file system.
- * Returns an array of FS_INFO structures, NULL on error.
+ * Returns an pointer to an FS_INFO structure, NULL on error.
  */
 FS_INFO *
  */
 FS_INFO *
-get_fs_info (lstatfsp, count, use_mmap)
-       struct statfs *lstatfsp;        /* IN: array of statfs structs */
-       int count;                      /* IN: number of file systems */
+get_fs_info (lstatfsp, use_mmap)
+       struct statfs *lstatfsp;        /* IN: pointer to statfs struct */
        int use_mmap;                   /* IN: mmap or read */
 {
        int use_mmap;                   /* IN: mmap or read */
 {
-       FS_INFO *fp, *fsp;
+       FS_INFO *fsp;
        int     i;
        
        int     i;
        
-       fsp = (FS_INFO *)calloc(count, sizeof(FS_INFO));
-
-       for (fp = fsp, i = 0; i < count; ++i, ++fp) {
-               fp->fi_statfsp = lstatfsp++;
-               if (get_superblock (fp, &fp->fi_lfs))
-                       err(1, "get_fs_info: get_superblock failed");
-               fp->fi_daddr_shift =
-                    fp->fi_lfs.lfs_bshift - fp->fi_lfs.lfs_fsbtodb;
-               get_ifile (fp, use_mmap);
-       }
+       fsp = (FS_INFO *)malloc(sizeof(FS_INFO));
+       if (fsp == NULL)
+               return NULL;
+       bzero(fsp, sizeof(FS_INFO));
+
+       fsp->fi_statfsp = lstatfsp;
+       if (get_superblock (fsp, &fsp->fi_lfs))
+               err(1, "get_fs_info: get_superblock failed");
+       fsp->fi_daddr_shift =
+            fsp->fi_lfs.lfs_bshift - fsp->fi_lfs.lfs_fsbtodb;
+       get_ifile (fsp, use_mmap);
        return (fsp);
 }
 
        return (fsp);
 }
 
@@ -106,17 +129,15 @@ get_fs_info (lstatfsp, count, use_mmap)
  * refresh the file system information (statfs) info.
  */
 void
  * refresh the file system information (statfs) info.
  */
 void
-reread_fs_info(fsp, count, use_mmap)
-       FS_INFO *fsp;   /* IN: array of fs_infos to free */
-       int count;      /* IN: number of file systems */
+reread_fs_info(fsp, use_mmap)
+       FS_INFO *fsp;   /* IN: prointer fs_infos to reread */
+       int use_mmap;
 {
        int i;
        
 {
        int i;
        
-       for (i = 0; i < count; ++i, ++fsp) {
-               if (statfs(fsp->fi_statfsp->f_mntonname, fsp->fi_statfsp))
-                       err(0, "reread_fs_info: statfs failed");
-               get_ifile (fsp, use_mmap);
-       }
+       if (statfs(fsp->fi_statfsp->f_mntonname, fsp->fi_statfsp))
+               err(1, "reread_fs_info: statfs failed");
+       get_ifile (fsp, use_mmap);
 }
 
 /* 
 }
 
 /* 
@@ -243,7 +264,7 @@ lfs_segmapv(fsp, seg, seg_buf, blocks, bcount)
        struct lfs *lfsp;
        caddr_t s, segend;
        daddr_t pseg_addr, seg_addr;
        struct lfs *lfsp;
        caddr_t s, segend;
        daddr_t pseg_addr, seg_addr;
-       int i, nelem, nblocks, sumsize;
+       int i, nelem, nblocks, nsegs, sumsize;
        time_t timestamp;
 
        lfsp = &fsp->fi_lfs;
        time_t timestamp;
 
        lfsp = &fsp->fi_lfs;
@@ -260,19 +281,22 @@ lfs_segmapv(fsp, seg, seg_buf, blocks, bcount)
 #endif /* VERBOSE */
 
        *bcount = 0;
 #endif /* VERBOSE */
 
        *bcount = 0;
-       for (segend = seg_buf + seg_size(lfsp), timestamp = 0; s < segend; ) {
+       for (nsegs = 0, timestamp = 0; nsegs < sup->su_nsums; nsegs++) {
                sp = (SEGSUM *)s;
 
                sp = (SEGSUM *)s;
 
+               nblocks = pseg_valid(fsp, sp);
+               if (nblocks <= 0) {
+                       printf("Warning: invalid segment summary at 0x%x\n",
+                           pseg_addr);
+                       break;
+               }
+
 #ifdef VERBOSE
                printf("\tpartial at: 0x%x\n", pseg_addr);
                print_SEGSUM(lfsp, sp);
                fflush(stdout);
 #endif /* VERBOSE */
 
 #ifdef VERBOSE
                printf("\tpartial at: 0x%x\n", pseg_addr);
                print_SEGSUM(lfsp, sp);
                fflush(stdout);
 #endif /* VERBOSE */
 
-               nblocks = pseg_valid(fsp, sp);
-               if (nblocks <= 0)
-                       break;
-
                /* Check if we have hit old data */
                if (timestamp > ((SEGSUM*)s)->ss_create)
                        break;
                /* Check if we have hit old data */
                if (timestamp > ((SEGSUM*)s)->ss_create)
                        break;
@@ -282,7 +306,7 @@ lfs_segmapv(fsp, seg, seg_buf, blocks, bcount)
                /* Verfiy size of summary block */
                sumsize = sizeof(SEGSUM) +
                    (sp->ss_ninos + INOPB(lfsp) - 1) / INOPB(lfsp);
                /* Verfiy size of summary block */
                sumsize = sizeof(SEGSUM) +
                    (sp->ss_ninos + INOPB(lfsp) - 1) / INOPB(lfsp);
-               for (fip = (FINFO *)(sp + 1); i < sp->ss_nfinfo; ++i) {
+               for (i = 0, fip = (FINFO *)(sp + 1); i < sp->ss_nfinfo; ++i) {
                        sumsize += sizeof(FINFO) +
                            (fip->fi_nblocks - 1) * sizeof(daddr_t);
                        fip = (FINFO *)(&fip->fi_blocks[fip->fi_nblocks]);
                        sumsize += sizeof(FINFO) +
                            (fip->fi_nblocks - 1) * sizeof(daddr_t);
                        fip = (FINFO *)(&fip->fi_blocks[fip->fi_nblocks]);
@@ -347,7 +371,9 @@ add_blocks (fsp, bip, countp, sp, seg_buf, segaddr, psegaddr)
        caddr_t bp;
        daddr_t *dp, *iaddrp;
        int db_per_block, i, j;
        caddr_t bp;
        daddr_t *dp, *iaddrp;
        int db_per_block, i, j;
+       int db_frag;
        u_long page_size;
        u_long page_size;
+long *lp;
 
 #ifdef VERBOSE
        printf("FILE INFOS\n");
 
 #ifdef VERBOSE
        printf("FILE INFOS\n");
@@ -379,8 +405,24 @@ add_blocks (fsp, bip, countp, sp, seg_buf, segaddr, psegaddr)
                        bip->bi_segcreate = (time_t)(sp->ss_create);
                        bip->bi_bp = bp;
                        bip->bi_version = ifp->if_version;
                        bip->bi_segcreate = (time_t)(sp->ss_create);
                        bip->bi_bp = bp;
                        bip->bi_version = ifp->if_version;
-                       psegaddr += db_per_block;
-                       bp += page_size;
+                       if (fip->fi_lastlength == page_size) {
+                               bip->bi_size = page_size;
+                               psegaddr += db_per_block;
+                               bp += page_size;
+                       } else {
+                               db_frag = fragstodb(&(fsp->fi_lfs), 
+                                   numfrags(&(fsp->fi_lfs),
+                                   fip->fi_lastlength));
+#ifdef VERBOSE
+                               printf("lastlength, frags: %d, %d, %d\n", 
+                                   fip->fi_lastlength, temp,
+                                   bytetoda(fsp, temp));
+                               fflush(stdout);
+#endif
+                               bip->bi_size = fip->fi_lastlength;
+                               bp += fip->fi_lastlength;
+                               psegaddr += db_frag;
+                       }
                        ++bip;
                        ++(*countp);
                }
                        ++bip;
                        ++(*countp);
                }
@@ -464,7 +506,9 @@ pseg_valid (fsp, ssp)
        caddr_t p;
        int i, nblocks;
        u_long *datap;
        caddr_t p;
        int i, nblocks;
        u_long *datap;
-       SEGUSE *sup;
+
+       if (ssp->ss_magic != SS_MAGIC)
+               return(0);
 
        if ((nblocks = dump_summary(&fsp->fi_lfs, ssp, 0, NULL)) <= 0 ||
            nblocks > fsp->fi_lfs.lfs_ssize - 1)
 
        if ((nblocks = dump_summary(&fsp->fi_lfs, ssp, 0, NULL)) <= 0 ||
            nblocks > fsp->fi_lfs.lfs_ssize - 1)
@@ -643,7 +687,7 @@ toss(p, nump, size, dotoss, client)
        for (i = *nump; --i > 0;) {
                p1 = p + size;
                if (dotoss(client, p, p1)) {
        for (i = *nump; --i > 0;) {
                p1 = p + size;
                if (dotoss(client, p, p1)) {
-                       bcopy(p1, p, i * size);
+                       memmove(p, p1, i * size);
                        --(*nump);
                } else 
                        p += size;
                        --(*nump);
                } else 
                        p += size;