major overhaul to handle logical buffer pool (not quite right yet)
[unix-history] / usr / src / sys / ufs / ffs / ffs_alloc.c
index 41b9f53..f282721 100644 (file)
@@ -1,23 +1,34 @@
 /*
 /*
- * Copyright (c) 1982, 1986 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
+ * All rights reserved.
  *
  *
- *     @(#)ffs_alloc.c 7.5 (Berkeley) %G%
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ *     @(#)ffs_alloc.c 7.13 (Berkeley) %G%
  */
 
 #include "param.h"
 #include "systm.h"
 #include "mount.h"
  */
 
 #include "param.h"
 #include "systm.h"
 #include "mount.h"
-#include "fs.h"
 #include "buf.h"
 #include "buf.h"
-#include "inode.h"
-#include "dir.h"
 #include "user.h"
 #include "user.h"
-#include "quota.h"
+#include "vnode.h"
 #include "kernel.h"
 #include "syslog.h"
 #include "cmap.h"
 #include "kernel.h"
 #include "syslog.h"
 #include "cmap.h"
+#include "../ufs/quota.h"
+#include "../ufs/inode.h"
+#include "../ufs/fs.h"
 
 extern u_long          hashalloc();
 extern ino_t           ialloccg();
 
 extern u_long          hashalloc();
 extern ino_t           ialloccg();
@@ -48,17 +59,18 @@ extern unsigned char        *fragtbl[];
  *   2) quadradically rehash into other cylinder groups, until an
  *      available block is located.
  */
  *   2) quadradically rehash into other cylinder groups, until an
  *      available block is located.
  */
-struct buf *
-alloc(ip, bpref, size)
+alloc(ip, lbn, bpref, size, bnp)
        register struct inode *ip;
        register struct inode *ip;
-       daddr_t bpref;
+       daddr_t lbn, bpref;
        int size;
        int size;
+       daddr_t *bnp;
 {
        daddr_t bno;
        register struct fs *fs;
        register struct buf *bp;
 {
        daddr_t bno;
        register struct fs *fs;
        register struct buf *bp;
-       int cg;
+       int cg, error;
        
        
+       *bnp = 0;
        fs = ip->i_fs;
        if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
                printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
        fs = ip->i_fs;
        if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
                printf("dev = 0x%x, bsize = %d, size = %d, fs = %s\n",
@@ -70,9 +82,8 @@ alloc(ip, bpref, size)
        if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
                goto nospace;
 #ifdef QUOTA
        if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
                goto nospace;
 #ifdef QUOTA
-       u.u_error = chkdq(ip, (long)btodb(size), 0);
-       if (u.u_error)
-               return (NULL);
+       if (error = chkdq(ip, (long)btodb(size), 0))
+               return (error);
 #endif
        if (bpref >= fs->fs_size)
                bpref = 0;
 #endif
        if (bpref >= fs->fs_size)
                bpref = 0;
@@ -82,22 +93,16 @@ alloc(ip, bpref, size)
                cg = dtog(fs, bpref);
        bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size,
                (u_long (*)())alloccg);
                cg = dtog(fs, bpref);
        bno = (daddr_t)hashalloc(ip, cg, (long)bpref, size,
                (u_long (*)())alloccg);
-       if (bno <= 0)
-               goto nospace;
-       ip->i_blocks += btodb(size);
-       ip->i_flag |= IUPD|ICHG;
-#ifdef SECSIZE
-       bp = getblk(ip->i_dev, fsbtodb(fs, bno), size, fs->fs_dbsize);
-#else SECSIZE
-       bp = getblk(ip->i_dev, fsbtodb(fs, bno), size);
-#endif SECSIZE
-       clrbuf(bp);
-       return (bp);
+       if (bno > 0) {
+               ip->i_blocks += btodb(size);
+               ip->i_flag |= IUPD|ICHG;
+               *bnp = bno;
+               return (0);
+       }
 nospace:
        fserr(fs, "file system full");
        uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
 nospace:
        fserr(fs, "file system full");
        uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
-       u.u_error = ENOSPC;
-       return (NULL);
+       return (ENOSPC);
 }
 
 /*
 }
 
 /*
@@ -108,18 +113,20 @@ nospace:
  * the original block. Failing that, the regular block allocator is
  * invoked to get an appropriate block.
  */
  * the original block. Failing that, the regular block allocator is
  * invoked to get an appropriate block.
  */
-struct buf *
-realloccg(ip, bprev, bpref, osize, nsize)
+realloccg(ip, lbprev, bpref, osize, nsize, bpp)
        register struct inode *ip;
        register struct inode *ip;
-       daddr_t bprev, bpref;
+       off_t lbprev;
+       daddr_t bpref;
        int osize, nsize;
        int osize, nsize;
+       struct buf **bpp;
 {
        register struct fs *fs;
 {
        register struct fs *fs;
-       register struct buf *bp, *obp;
+       struct buf *bp, *obp;
        int cg, request;
        int cg, request;
-       daddr_t bno, bn;
-       int i, count;
+       daddr_t bprev, bno, bn;
+       int i, error, count;
        
        
+       *bpp = 0;
        fs = ip->i_fs;
        if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
            (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
        fs = ip->i_fs;
        if ((unsigned)osize > fs->fs_bsize || fragoff(fs, osize) != 0 ||
            (unsigned)nsize > fs->fs_bsize || fragoff(fs, nsize) != 0) {
@@ -129,37 +136,40 @@ realloccg(ip, bprev, bpref, osize, nsize)
        }
        if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
                goto nospace;
        }
        if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
                goto nospace;
-       if (bprev == 0) {
+       if ((bprev = ip->i_db[lbprev]) == 0) {
                printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
                    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
                panic("realloccg: bad bprev");
        }
 #ifdef QUOTA
                printf("dev = 0x%x, bsize = %d, bprev = %d, fs = %s\n",
                    ip->i_dev, fs->fs_bsize, bprev, fs->fs_fsmnt);
                panic("realloccg: bad bprev");
        }
 #ifdef QUOTA
-       u.u_error = chkdq(ip, (long)btodb(nsize - osize), 0);
-       if (u.u_error)
-               return (NULL);
+       if (error = chkdq(ip, (long)btodb(nsize - osize), 0))
+               return (error);
 #endif
 #endif
+       /*
+        * Allocate the extra space in the buffer.
+        */
+       if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) {
+               brelse(bp);
+               return (error);
+       }
+       brealloc(bp, nsize);
+       bp->b_flags |= B_DONE;
+       bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
+       ip->i_blocks += btodb(nsize - osize);
+       ip->i_flag |= IUPD|ICHG;
+       /*
+        * Check for extension in the existing location.
+        */
        cg = dtog(fs, bprev);
        cg = dtog(fs, bprev);
-       bno = fragextend(ip, cg, (long)bprev, osize, nsize);
-       if (bno != 0) {
-               do {
-#ifdef SECSIZE
-                       bp = bread(ip->i_dev, fsbtodb(fs, bno), osize,
-                           fs->fs_dbsize);
-#else SECSIZE
-                       bp = bread(ip->i_dev, fsbtodb(fs, bno), osize);
-#endif SECSIZE
-                       if (bp->b_flags & B_ERROR) {
-                               brelse(bp);
-                               return (NULL);
-                       }
-               } while (brealloc(bp, nsize) == 0);
-               bp->b_flags |= B_DONE;
-               bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
-               ip->i_blocks += btodb(nsize - osize);
-               ip->i_flag |= IUPD|ICHG;
-               return (bp);
+       if (bno = fragextend(ip, cg, (long)bprev, osize, nsize)) {
+               if (bp->b_blkno != bno)
+                       panic("bad blockno");
+               *bpp = bp;
+               return (0);
        }
        }
+       /*
+        * Allocate a new disk location.
+        */
        if (bpref >= fs->fs_size)
                bpref = 0;
        switch ((int)fs->fs_optim) {
        if (bpref >= fs->fs_size)
                bpref = 0;
        switch ((int)fs->fs_optim) {
@@ -212,19 +222,7 @@ realloccg(ip, bprev, bpref, osize, nsize)
                obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize,
                    fs->fs_dbsize);
 #else SECSIZE
                obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize,
                    fs->fs_dbsize);
 #else SECSIZE
-               obp = bread(ip->i_dev, fsbtodb(fs, bprev), osize);
-#endif SECSIZE
-               if (obp->b_flags & B_ERROR) {
-                       brelse(obp);
-                       return (NULL);
-               }
-               bn = fsbtodb(fs, bno);
-#ifdef SECSIZE
-               bp = getblk(ip->i_dev, bn, nsize, fs->fs_dbsize);
-#else SECSIZE
-               bp = getblk(ip->i_dev, bn, nsize);
-#endif SECSIZE
-               bcopy(obp->b_un.b_addr, bp->b_un.b_addr, (u_int)osize);
+               bp->b_blkno = bn = fsbtodb(fs, bno);
                count = howmany(osize, CLBYTES);
                for (i = 0; i < count; i++)
 #ifdef SECSIZE
                count = howmany(osize, CLBYTES);
                for (i = 0; i < count; i++)
 #ifdef SECSIZE
@@ -232,28 +230,23 @@ realloccg(ip, bprev, bpref, osize, nsize)
 #else SECSIZE
                        munhash(ip->i_dev, bn + i * CLBYTES / DEV_BSIZE);
 #endif SECSIZE
 #else SECSIZE
                        munhash(ip->i_dev, bn + i * CLBYTES / DEV_BSIZE);
 #endif SECSIZE
-               bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
-               if (obp->b_flags & B_DELWRI) {
-                       obp->b_flags &= ~B_DELWRI;
-                       u.u_ru.ru_oublock--;            /* delete charge */
-               }
-               brelse(obp);
                blkfree(ip, bprev, (off_t)osize);
                if (nsize < request)
                        blkfree(ip, bno + numfrags(fs, nsize),
                                (off_t)(request - nsize));
                ip->i_blocks += btodb(nsize - osize);
                ip->i_flag |= IUPD|ICHG;
                blkfree(ip, bprev, (off_t)osize);
                if (nsize < request)
                        blkfree(ip, bno + numfrags(fs, nsize),
                                (off_t)(request - nsize));
                ip->i_blocks += btodb(nsize - osize);
                ip->i_flag |= IUPD|ICHG;
-               return (bp);
+               *bpp = bp;
+               return (0);
        }
        }
+       brelse(bp);
 nospace:
        /*
         * no space available
         */
        fserr(fs, "file system full");
        uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
 nospace:
        /*
         * no space available
         */
        fserr(fs, "file system full");
        uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
-       u.u_error = ENOSPC;
-       return (NULL);
+       return (ENOSPC);
 }
 
 /*
 }
 
 /*
@@ -271,24 +264,24 @@ nospace:
  *   2) quadradically rehash into other cylinder groups, until an
  *      available inode is located.
  */
  *   2) quadradically rehash into other cylinder groups, until an
  *      available inode is located.
  */
-struct inode *
-ialloc(pip, ipref, mode)
+ialloc(pip, ipref, mode, ipp)
        register struct inode *pip;
        ino_t ipref;
        int mode;
        register struct inode *pip;
        ino_t ipref;
        int mode;
+       struct inode **ipp;
 {
        ino_t ino;
        register struct fs *fs;
        register struct inode *ip;
 {
        ino_t ino;
        register struct fs *fs;
        register struct inode *ip;
-       int cg;
+       int cg, error;
        
        
+       *ipp = 0;
        fs = pip->i_fs;
        if (fs->fs_cstotal.cs_nifree == 0)
                goto noinodes;
 #ifdef QUOTA
        fs = pip->i_fs;
        if (fs->fs_cstotal.cs_nifree == 0)
                goto noinodes;
 #ifdef QUOTA
-       u.u_error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0);
-       if (u.u_error)
-               return (NULL);
+       if (error = chkiq(pip->i_dev, (struct inode *)NULL, u.u_uid, 0))
+               return (error);
 #endif
        if (ipref >= fs->fs_ncg * fs->fs_ipg)
                ipref = 0;
 #endif
        if (ipref >= fs->fs_ncg * fs->fs_ipg)
                ipref = 0;
@@ -296,11 +289,12 @@ ialloc(pip, ipref, mode)
        ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
        if (ino == 0)
                goto noinodes;
        ino = (ino_t)hashalloc(pip, cg, (long)ipref, mode, ialloccg);
        if (ino == 0)
                goto noinodes;
-       ip = iget(pip->i_dev, pip->i_fs, ino);
-       if (ip == NULL) {
+       error = iget(pip, ino, ipp);
+       if (error) {
                ifree(pip, ino, 0);
                ifree(pip, ino, 0);
-               return (NULL);
+               return (error);
        }
        }
+       ip = *ipp;
        if (ip->i_mode) {
                printf("mode = 0%o, inum = %d, fs = %s\n",
                    ip->i_mode, ip->i_number, fs->fs_fsmnt);
        if (ip->i_mode) {
                printf("mode = 0%o, inum = %d, fs = %s\n",
                    ip->i_mode, ip->i_number, fs->fs_fsmnt);
@@ -311,12 +305,18 @@ ialloc(pip, ipref, mode)
                    fs->fs_fsmnt, ino, ip->i_blocks);
                ip->i_blocks = 0;
        }
                    fs->fs_fsmnt, ino, ip->i_blocks);
                ip->i_blocks = 0;
        }
-       return (ip);
+       ip->i_flags = 0;
+       /*
+        * Set up a new generation number for this inode.
+        */
+       if (++nextgennumber < (u_long)time.tv_sec)
+               nextgennumber = time.tv_sec;
+       ip->i_gen = nextgennumber;
+       return (0);
 noinodes:
        fserr(fs, "out of inodes");
        uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
 noinodes:
        fserr(fs, "out of inodes");
        uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
-       u.u_error = ENOSPC;
-       return (NULL);
+       return (ENOSPC);
 }
 
 /*
 }
 
 /*
@@ -503,11 +503,11 @@ fragextend(ip, cg, bprev, osize, nsize)
        int osize, nsize;
 {
        register struct fs *fs;
        int osize, nsize;
 {
        register struct fs *fs;
-       register struct buf *bp;
        register struct cg *cgp;
        register struct cg *cgp;
+       struct buf *bp;
        long bno;
        int frags, bbase;
        long bno;
        int frags, bbase;
-       int i;
+       int i, error;
 
        fs = ip->i_fs;
        if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
 
        fs = ip->i_fs;
        if (fs->fs_cs(fs, cg).cs_nffree < numfrags(fs, nsize - osize))
@@ -522,10 +522,15 @@ fragextend(ip, cg, bprev, osize, nsize)
        bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
            fs->fs_dbsize);
 #else SECSIZE
        bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
            fs->fs_dbsize);
 #else SECSIZE
-       bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
+       error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
+               (int)fs->fs_cgsize, NOCRED, &bp);
+       if (error) {
+               brelse(bp);
+               return (NULL);
+       }
 #endif SECSIZE
        cgp = bp->b_un.b_cg;
 #endif SECSIZE
        cgp = bp->b_un.b_cg;
-       if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
+       if (!cg_chkmagic(cgp)) {
                brelse(bp);
                return (NULL);
        }
                brelse(bp);
                return (NULL);
        }
@@ -573,11 +578,10 @@ alloccg(ip, cg, bpref, size)
        int size;
 {
        register struct fs *fs;
        int size;
 {
        register struct fs *fs;
-       register struct buf *bp;
        register struct cg *cgp;
        register struct cg *cgp;
-       int bno, frags;
-       int allocsiz;
+       struct buf *bp;
        register int i;
        register int i;
+       int error, bno, frags, allocsiz;
 
        fs = ip->i_fs;
        if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
 
        fs = ip->i_fs;
        if (fs->fs_cs(fs, cg).cs_nbfree == 0 && size == fs->fs_bsize)
@@ -586,10 +590,15 @@ alloccg(ip, cg, bpref, size)
        bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
            fs->fs_dbsize);
 #else SECSIZE
        bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
            fs->fs_dbsize);
 #else SECSIZE
-       bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
+       error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
+               (int)fs->fs_cgsize, NOCRED, &bp);
+       if (error) {
+               brelse(bp);
+               return (NULL);
+       }
 #endif SECSIZE
        cgp = bp->b_un.b_cg;
 #endif SECSIZE
        cgp = bp->b_un.b_cg;
-       if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) ||
+       if (!cg_chkmagic(cgp) ||
            (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
                brelse(bp);
                return (NULL);
            (cgp->cg_cs.cs_nbfree == 0 && size == fs->fs_bsize)) {
                brelse(bp);
                return (NULL);
@@ -778,7 +787,7 @@ ialloccg(ip, cg, ipref, mode)
        register struct fs *fs;
        register struct cg *cgp;
        struct buf *bp;
        register struct fs *fs;
        register struct cg *cgp;
        struct buf *bp;
-       int start, len, loc, map, i;
+       int error, start, len, loc, map, i;
 
        fs = ip->i_fs;
        if (fs->fs_cs(fs, cg).cs_nifree == 0)
 
        fs = ip->i_fs;
        if (fs->fs_cs(fs, cg).cs_nifree == 0)
@@ -787,11 +796,15 @@ ialloccg(ip, cg, ipref, mode)
        bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
            fs->fs_dbsize);
 #else SECSIZE
        bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
            fs->fs_dbsize);
 #else SECSIZE
-       bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
+       error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
+               (int)fs->fs_cgsize, NOCRED, &bp);
+       if (error) {
+               brelse(bp);
+               return (NULL);
+       }
 #endif SECSIZE
        cgp = bp->b_un.b_cg;
 #endif SECSIZE
        cgp = bp->b_un.b_cg;
-       if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp) ||
-           cgp->cg_cs.cs_nifree == 0) {
+       if (!cg_chkmagic(cgp) || cgp->cg_cs.cs_nifree == 0) {
                brelse(bp);
                return (NULL);
        }
                brelse(bp);
                return (NULL);
        }
@@ -856,8 +869,8 @@ blkfree(ip, bno, size)
 {
        register struct fs *fs;
        register struct cg *cgp;
 {
        register struct fs *fs;
        register struct cg *cgp;
-       register struct buf *bp;
-       int cg, blk, frags, bbase;
+       struct buf *bp;
+       int error, cg, blk, frags, bbase;
        register int i;
 
        fs = ip->i_fs;
        register int i;
 
        fs = ip->i_fs;
@@ -875,10 +888,15 @@ blkfree(ip, bno, size)
        bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
            fs->fs_dbsize);
 #else SECSIZE
        bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
            fs->fs_dbsize);
 #else SECSIZE
-       bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
+       error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
+               (int)fs->fs_cgsize, NOCRED, &bp);
+       if (error) {
+               brelse(bp);
+               return;
+       }
 #endif SECSIZE
        cgp = bp->b_un.b_cg;
 #endif SECSIZE
        cgp = bp->b_un.b_cg;
-       if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
+       if (!cg_chkmagic(cgp)) {
                brelse(bp);
                return;
        }
                brelse(bp);
                return;
        }
@@ -927,7 +945,8 @@ blkfree(ip, bno, size)
                /*
                 * if a complete block has been reassembled, account for it
                 */
                /*
                 * if a complete block has been reassembled, account for it
                 */
-               if (isblock(fs, cg_blksfree(cgp), fragstoblks(fs, bbase))) {
+               if (isblock(fs, cg_blksfree(cgp),
+                   (daddr_t)fragstoblks(fs, bbase))) {
                        cgp->cg_cs.cs_nffree -= fs->fs_frag;
                        fs->fs_cstotal.cs_nffree -= fs->fs_frag;
                        fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
                        cgp->cg_cs.cs_nffree -= fs->fs_frag;
                        fs->fs_cstotal.cs_nffree -= fs->fs_frag;
                        fs->fs_cs(fs, cg).cs_nffree -= fs->fs_frag;
@@ -955,8 +974,8 @@ ifree(ip, ino, mode)
 {
        register struct fs *fs;
        register struct cg *cgp;
 {
        register struct fs *fs;
        register struct cg *cgp;
-       register struct buf *bp;
-       int cg;
+       struct buf *bp;
+       int error, cg;
 
        fs = ip->i_fs;
        if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
 
        fs = ip->i_fs;
        if ((unsigned)ino >= fs->fs_ipg*fs->fs_ncg) {
@@ -969,10 +988,15 @@ ifree(ip, ino, mode)
        bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
            fs->fs_dbsize);
 #else SECSIZE
        bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize,
            fs->fs_dbsize);
 #else SECSIZE
-       bp = bread(ip->i_dev, fsbtodb(fs, cgtod(fs, cg)), (int)fs->fs_cgsize);
+       error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
+               (int)fs->fs_cgsize, NOCRED, &bp);
+       if (error) {
+               brelse(bp);
+               return;
+       }
 #endif SECSIZE
        cgp = bp->b_un.b_cg;
 #endif SECSIZE
        cgp = bp->b_un.b_cg;
-       if (bp->b_flags & B_ERROR || !cg_chkmagic(cgp)) {
+       if (!cg_chkmagic(cgp)) {
                brelse(bp);
                return;
        }
                brelse(bp);
                return;
        }
@@ -1024,15 +1048,15 @@ mapsearch(fs, cgp, bpref, allocsiz)
        else
                start = cgp->cg_frotor / NBBY;
        len = howmany(fs->fs_fpg, NBBY) - start;
        else
                start = cgp->cg_frotor / NBBY;
        len = howmany(fs->fs_fpg, NBBY) - start;
-       loc = scanc((unsigned)len, (caddr_t)&cg_blksfree(cgp)[start],
-               (caddr_t)fragtbl[fs->fs_frag],
-               (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
+       loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[start],
+               (u_char *)fragtbl[fs->fs_frag],
+               (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
        if (loc == 0) {
                len = start + 1;
                start = 0;
        if (loc == 0) {
                len = start + 1;
                start = 0;
-               loc = scanc((unsigned)len, (caddr_t)&cg_blksfree(cgp)[0],
-                       (caddr_t)fragtbl[fs->fs_frag],
-                       (int)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
+               loc = scanc((unsigned)len, (u_char *)&cg_blksfree(cgp)[0],
+                       (u_char *)fragtbl[fs->fs_frag],
+                       (u_char)(1 << (allocsiz - 1 + (fs->fs_frag % NBBY))));
                if (loc == 0) {
                        printf("start = %d, len = %d, fs = %s\n",
                            start, len, fs->fs_fsmnt);
                if (loc == 0) {
                        printf("start = %d, len = %d, fs = %s\n",
                            start, len, fs->fs_fsmnt);