brealloc => allocbuf
[unix-history] / usr / src / sys / ufs / ffs / ffs_alloc.c
index 3f558e2..416461f 100644 (file)
@@ -2,24 +2,13 @@
  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
  * All rights reserved.
  *
- * 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.
+ * %sccs.include.redist.c%
  *
  *
- *     @(#)ffs_alloc.c 7.10 (Berkeley) %G%
+ *     @(#)ffs_alloc.c 7.21 (Berkeley) %G%
  */
 
 #include "param.h"
 #include "systm.h"
  */
 
 #include "param.h"
 #include "systm.h"
-#include "mount.h"
 #include "buf.h"
 #include "user.h"
 #include "vnode.h"
 #include "buf.h"
 #include "user.h"
 #include "vnode.h"
@@ -59,19 +48,19 @@ 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.
  */
-alloc(ip, bpref, size, bpp, flags)
+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;
-       struct buf **bpp;
-       int flags;
+       daddr_t *bnp;
 {
        daddr_t bno;
        register struct fs *fs;
        register struct buf *bp;
        int cg, error;
 {
        daddr_t bno;
        register struct fs *fs;
        register struct buf *bp;
        int cg, error;
+       struct ucred *cred = u.u_cred;          /* XXX */
        
        
-       *bpp = 0;
+       *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",
@@ -80,10 +69,10 @@ alloc(ip, bpref, size, bpp, flags)
        }
        if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
                goto nospace;
        }
        if (size == fs->fs_bsize && fs->fs_cstotal.cs_nbfree == 0)
                goto nospace;
-       if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
+       if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
                goto nospace;
 #ifdef QUOTA
                goto nospace;
 #ifdef QUOTA
-       if (error = chkdq(ip, (long)btodb(size), 0))
+       if (error = chkdq(ip, (long)btodb(size), cred, 0))
                return (error);
 #endif
        if (bpref >= fs->fs_size)
                return (error);
 #endif
        if (bpref >= fs->fs_size)
@@ -94,20 +83,14 @@ alloc(ip, bpref, size, bpp, flags)
                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_devvp, fsbtodb(fs, bno), size);
-       if (flags & B_CLRBUF)
-               clrbuf(bp);
-       *bpp = bp;
-       return (0);
+       if (bno > 0) {
+               ip->i_blocks += btodb(size);
+               ip->i_flag |= IUPD|ICHG;
+               *bnp = bno;
+               return (0);
+       }
 nospace:
 nospace:
-       fserr(fs, "file system full");
+       fserr(fs, cred->cr_uid, "file system full");
        uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
        return (ENOSPC);
 }
        uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
        return (ENOSPC);
 }
@@ -120,17 +103,19 @@ 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.
  */
-realloccg(ip, bprev, bpref, osize, nsize, bpp)
+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;
        struct buf **bpp;
 {
        register struct fs *fs;
        struct buf *bp, *obp;
        int cg, request;
        int osize, nsize;
        struct buf **bpp;
 {
        register struct fs *fs;
        struct buf *bp, *obp;
        int cg, request;
-       daddr_t bno, bn;
+       daddr_t bprev, bno, bn;
        int i, error, count;
        int i, error, count;
+       struct ucred *cred = u.u_cred;          /* XXX */
        
        *bpp = 0;
        fs = ip->i_fs;
        
        *bpp = 0;
        fs = ip->i_fs;
@@ -140,39 +125,42 @@ realloccg(ip, bprev, bpref, osize, nsize, bpp)
                    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
                panic("realloccg: bad size");
        }
                    ip->i_dev, fs->fs_bsize, osize, nsize, fs->fs_fsmnt);
                panic("realloccg: bad size");
        }
-       if (u.u_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
+       if (cred->cr_uid != 0 && freespace(fs, fs->fs_minfree) <= 0)
                goto nospace;
                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
-       if (error = chkdq(ip, (long)btodb(nsize - osize), 0))
+       if (error = chkdq(ip, (long)btodb(nsize - osize), cred, 0))
                return (error);
 #endif
                return (error);
 #endif
+       /*
+        * Allocate the extra space in the buffer.
+        */
+       if (error = bread(ITOV(ip), lbprev, osize, NOCRED, &bp)) {
+               brelse(bp);
+               return (error);
+       }
+       allocbuf(bp, nsize);
+       bp->b_flags |= B_DONE;
+       bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
+       /*
+        * 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
-                       error = bread(ip->i_devvp, fsbtodb(fs, bno),
-                               osize, &bp);
-                       if (error) {
-                               brelse(bp);
-                               return (error);
-                       }
-               } while (brealloc(bp, nsize) == 0);
-               bp->b_flags |= B_DONE;
-               bzero(bp->b_un.b_addr + osize, (unsigned)nsize - osize);
+       if (bno = fragextend(ip, cg, (long)bprev, osize, nsize)) {
+               if (bp->b_blkno != fsbtodb(fs, bno))
+                       panic("bad blockno");
                ip->i_blocks += btodb(nsize - osize);
                ip->i_flag |= IUPD|ICHG;
                *bpp = bp;
                return (0);
        }
                ip->i_blocks += btodb(nsize - osize);
                ip->i_flag |= IUPD|ICHG;
                *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) {
@@ -225,18 +213,7 @@ realloccg(ip, bprev, bpref, osize, nsize, bpp)
                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
-               error = bread(ip->i_devvp, fsbtodb(fs, bprev), osize, &obp);
-               if (error) {
-                       brelse(obp);
-                       return (error);
-               }
-               bn = fsbtodb(fs, bno);
-#ifdef SECSIZE
-               bp = getblk(ip->i_dev, bn, nsize, fs->fs_dbsize);
-#else SECSIZE
-               bp = getblk(ip->i_devvp, 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
@@ -244,12 +221,6 @@ realloccg(ip, bprev, bpref, osize, nsize, bpp)
 #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),
                blkfree(ip, bprev, (off_t)osize);
                if (nsize < request)
                        blkfree(ip, bno + numfrags(fs, nsize),
@@ -259,11 +230,12 @@ realloccg(ip, bprev, bpref, osize, nsize, bpp)
                *bpp = bp;
                return (0);
        }
                *bpp = bp;
                return (0);
        }
+       brelse(bp);
 nospace:
        /*
         * no space available
         */
 nospace:
        /*
         * no space available
         */
-       fserr(fs, "file system full");
+       fserr(fs, cred->cr_uid, "file system full");
        uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
        return (ENOSPC);
 }
        uprintf("\n%s: write failed, file system is full\n", fs->fs_fsmnt);
        return (ENOSPC);
 }
@@ -283,10 +255,11 @@ 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.
  */
-ialloc(pip, ipref, mode, ipp)
+ialloc(pip, ipref, mode, cred, ipp)
        register struct inode *pip;
        ino_t ipref;
        int mode;
        register struct inode *pip;
        ino_t ipref;
        int mode;
+       struct ucred *cred;
        struct inode **ipp;
 {
        ino_t ino;
        struct inode **ipp;
 {
        ino_t ino;
@@ -298,10 +271,6 @@ ialloc(pip, ipref, mode, ipp)
        fs = pip->i_fs;
        if (fs->fs_cstotal.cs_nifree == 0)
                goto noinodes;
        fs = pip->i_fs;
        if (fs->fs_cstotal.cs_nifree == 0)
                goto noinodes;
-#ifdef QUOTA
-       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;
        cg = itog(fs, ipref);
        if (ipref >= fs->fs_ncg * fs->fs_ipg)
                ipref = 0;
        cg = itog(fs, ipref);
@@ -310,7 +279,7 @@ ialloc(pip, ipref, mode, ipp)
                goto noinodes;
        error = iget(pip, ino, ipp);
        if (error) {
                goto noinodes;
        error = iget(pip, ino, ipp);
        if (error) {
-               ifree(pip, ino, 0);
+               ifree(pip, ino, mode);
                return (error);
        }
        ip = *ipp;
                return (error);
        }
        ip = *ipp;
@@ -324,6 +293,7 @@ ialloc(pip, ipref, mode, ipp)
                    fs->fs_fsmnt, ino, ip->i_blocks);
                ip->i_blocks = 0;
        }
                    fs->fs_fsmnt, ino, ip->i_blocks);
                ip->i_blocks = 0;
        }
+       ip->i_flags = 0;
        /*
         * Set up a new generation number for this inode.
         */
        /*
         * Set up a new generation number for this inode.
         */
@@ -332,7 +302,7 @@ ialloc(pip, ipref, mode, ipp)
        ip->i_gen = nextgennumber;
        return (0);
 noinodes:
        ip->i_gen = nextgennumber;
        return (0);
 noinodes:
-       fserr(fs, "out of inodes");
+       fserr(fs, cred->cr_uid, "out of inodes");
        uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
        return (ENOSPC);
 }
        uprintf("\n%s: create/symlink failed, no inodes free\n", fs->fs_fsmnt);
        return (ENOSPC);
 }
@@ -541,7 +511,7 @@ fragextend(ip, cg, bprev, osize, nsize)
            fs->fs_dbsize);
 #else SECSIZE
        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
            fs->fs_dbsize);
 #else SECSIZE
        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
-               (int)fs->fs_cgsize, &bp);
+               (int)fs->fs_cgsize, NOCRED, &bp);
        if (error) {
                brelse(bp);
                return (NULL);
        if (error) {
                brelse(bp);
                return (NULL);
@@ -609,7 +579,7 @@ alloccg(ip, cg, bpref, size)
            fs->fs_dbsize);
 #else SECSIZE
        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
            fs->fs_dbsize);
 #else SECSIZE
        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
-               (int)fs->fs_cgsize, &bp);
+               (int)fs->fs_cgsize, NOCRED, &bp);
        if (error) {
                brelse(bp);
                return (NULL);
        if (error) {
                brelse(bp);
                return (NULL);
@@ -815,7 +785,7 @@ ialloccg(ip, cg, ipref, mode)
            fs->fs_dbsize);
 #else SECSIZE
        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
            fs->fs_dbsize);
 #else SECSIZE
        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
-               (int)fs->fs_cgsize, &bp);
+               (int)fs->fs_cgsize, NOCRED, &bp);
        if (error) {
                brelse(bp);
                return (NULL);
        if (error) {
                brelse(bp);
                return (NULL);
@@ -890,6 +860,7 @@ blkfree(ip, bno, size)
        struct buf *bp;
        int error, cg, blk, frags, bbase;
        register int i;
        struct buf *bp;
        int error, cg, blk, frags, bbase;
        register int i;
+       struct ucred *cred = u.u_cred;  /* XXX */
 
        fs = ip->i_fs;
        if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
 
        fs = ip->i_fs;
        if ((unsigned)size > fs->fs_bsize || fragoff(fs, size) != 0) {
@@ -898,8 +869,9 @@ blkfree(ip, bno, size)
                panic("blkfree: bad size");
        }
        cg = dtog(fs, bno);
                panic("blkfree: bad size");
        }
        cg = dtog(fs, bno);
-       if (badblock(fs, bno)) {
+       if ((unsigned)bno >= fs->fs_size) {
                printf("bad block %d, ino %d\n", bno, ip->i_number);
                printf("bad block %d, ino %d\n", bno, ip->i_number);
+               fserr(fs, cred->cr_uid, "bad block");
                return;
        }
 #ifdef SECSIZE
                return;
        }
 #ifdef SECSIZE
@@ -907,7 +879,7 @@ blkfree(ip, bno, size)
            fs->fs_dbsize);
 #else SECSIZE
        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
            fs->fs_dbsize);
 #else SECSIZE
        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
-               (int)fs->fs_cgsize, &bp);
+               (int)fs->fs_cgsize, NOCRED, &bp);
        if (error) {
                brelse(bp);
                return;
        if (error) {
                brelse(bp);
                return;
@@ -1007,7 +979,7 @@ ifree(ip, ino, mode)
            fs->fs_dbsize);
 #else SECSIZE
        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
            fs->fs_dbsize);
 #else SECSIZE
        error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
-               (int)fs->fs_cgsize, &bp);
+               (int)fs->fs_cgsize, NOCRED, &bp);
        if (error) {
                brelse(bp);
                return;
        if (error) {
                brelse(bp);
                return;
@@ -1111,10 +1083,11 @@ mapsearch(fs, cgp, bpref, allocsiz)
  * The form of the error message is:
  *     fs: error message
  */
  * The form of the error message is:
  *     fs: error message
  */
-fserr(fs, cp)
+fserr(fs, uid, cp)
        struct fs *fs;
        struct fs *fs;
+       uid_t uid;
        char *cp;
 {
 
        char *cp;
 {
 
-       log(LOG_ERR, "%s: %s\n", fs->fs_fsmnt, cp);
+       log(LOG_ERR, "uid %d on %s: %s\n", uid, fs->fs_fsmnt, cp);
 }
 }