add ifdefs for dev_bsize (almost working)
[unix-history] / usr / src / sys / ufs / lfs / lfs_balloc.c
index 0183655..48870ad 100644 (file)
@@ -1,14 +1,19 @@
-/*     lfs_balloc.c    5.1     82/07/15        */
+/*
+ * 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.
+ *
+ *     @(#)lfs_balloc.c        7.1.1.1 (Berkeley) %G%
+ */
 
 
-#include "../h/param.h"
-#include "../h/systm.h"
-#include "../h/conf.h"
-#include "../h/inode.h"
-#include "../h/dir.h"
-#include "../h/user.h"
-#include "../h/buf.h"
-#include "../h/proc.h"
-#include "../h/fs.h"
+#include "param.h"
+#include "systm.h"
+#include "inode.h"
+#include "dir.h"
+#include "user.h"
+#include "buf.h"
+#include "proc.h"
+#include "fs.h"
 
 /*
  * Bmap defines the structure of file system storage
 
 /*
  * Bmap defines the structure of file system storage
@@ -31,7 +36,7 @@ bmap(ip, bn, rwflg, size)
        struct buf *bp, *nbp;
        struct fs *fs;
        int j, sh;
        struct buf *bp, *nbp;
        struct fs *fs;
        int j, sh;
-       daddr_t nb, *bap, pref, blkpref();
+       daddr_t nb, lbn, *bap, pref, blkpref();
 
        if (bn < 0) {
                u.u_error = EFBIG;
 
        if (bn < 0) {
                u.u_error = EFBIG;
@@ -51,8 +56,10 @@ bmap(ip, bn, rwflg, size)
                osize = blksize(fs, ip, nb);
                if (osize < fs->fs_bsize && osize > 0) {
                        bp = realloccg(ip, ip->i_db[nb],
                osize = blksize(fs, ip, nb);
                if (osize < fs->fs_bsize && osize > 0) {
                        bp = realloccg(ip, ip->i_db[nb],
-                               nb == 0 ? 0 : ip->i_db[nb - 1] + fs->fs_frag,
-                               osize, fs->fs_bsize);
+                               blkpref(ip, nb, (int)nb, &ip->i_db[0]),
+                               osize, (int)fs->fs_bsize);
+                       if (bp == NULL)
+                               return ((daddr_t)-1);
                        ip->i_size = (nb + 1) * fs->fs_bsize;
                        ip->i_db[nb] = dbtofsb(fs, bp->b_blkno);
                        ip->i_flag |= IUPD|ICHG;
                        ip->i_size = (nb + 1) * fs->fs_bsize;
                        ip->i_db[nb] = dbtofsb(fs, bp->b_blkno);
                        ip->i_flag |= IUPD|ICHG;
@@ -63,30 +70,29 @@ bmap(ip, bn, rwflg, size)
         * The first NDADDR blocks are direct blocks
         */
        if (bn < NDADDR) {
         * The first NDADDR blocks are direct blocks
         */
        if (bn < NDADDR) {
-               i = bn;
-               nb = ip->i_db[i];
+               nb = ip->i_db[bn];
                if (rwflg == B_READ) {
                        if (nb == 0)
                                return ((daddr_t)-1);
                        goto gotit;
                }
                if (rwflg == B_READ) {
                        if (nb == 0)
                                return ((daddr_t)-1);
                        goto gotit;
                }
-               if (nb == 0 || ip->i_size < (i + 1) * fs->fs_bsize) {
+               if (nb == 0 || ip->i_size < (bn + 1) * fs->fs_bsize) {
                        if (nb != 0) {
                                /* consider need to reallocate a frag */
                                osize = fragroundup(fs, blkoff(fs, ip->i_size));
                                nsize = fragroundup(fs, size);
                                if (nsize <= osize)
                                        goto gotit;
                        if (nb != 0) {
                                /* consider need to reallocate a frag */
                                osize = fragroundup(fs, blkoff(fs, ip->i_size));
                                nsize = fragroundup(fs, size);
                                if (nsize <= osize)
                                        goto gotit;
-                               bp = realloccg(ip, nb, i == 0 ?
-                                       0 : ip->i_db[i - 1] + fs->fs_frag,
+                               bp = realloccg(ip, nb,
+                                       blkpref(ip, bn, (int)bn, &ip->i_db[0]),
                                        osize, nsize);
                        } else {
                                        osize, nsize);
                        } else {
-                               if (ip->i_size < (i + 1) * fs->fs_bsize)
+                               if (ip->i_size < (bn + 1) * fs->fs_bsize)
                                        nsize = fragroundup(fs, size);
                                else
                                        nsize = fs->fs_bsize;
                                        nsize = fragroundup(fs, size);
                                else
                                        nsize = fs->fs_bsize;
-                               bp = alloc(ip, i > 0 ?
-                                       ip->i_db[i - 1] + fs->fs_frag : 0,
+                               bp = alloc(ip,
+                                       blkpref(ip, bn, (int)bn, &ip->i_db[0]),
                                        nsize);
                        }
                        if (bp == NULL)
                                        nsize);
                        }
                        if (bp == NULL)
@@ -101,13 +107,13 @@ bmap(ip, bn, rwflg, size)
                                bwrite(bp);
                        else
                                bdwrite(bp);
                                bwrite(bp);
                        else
                                bdwrite(bp);
-                       ip->i_db[i] = nb;
+                       ip->i_db[bn] = nb;
                        ip->i_flag |= IUPD|ICHG;
                }
 gotit:
                        ip->i_flag |= IUPD|ICHG;
                }
 gotit:
-               if (i < NDADDR - 1) {
-                       rablock = fsbtodb(fs, ip->i_db[i+1]);
-                       rasize = blksize(fs, ip, i+1);
+               if (bn < NDADDR - 1) {
+                       rablock = fsbtodb(fs, ip->i_db[bn + 1]);
+                       rasize = blksize(fs, ip, bn + 1);
                }
                return (nb);
        }
                }
                return (nb);
        }
@@ -115,7 +121,9 @@ gotit:
        /*
         * Determine how many levels of indirection.
         */
        /*
         * Determine how many levels of indirection.
         */
+       pref = 0;
        sh = 1;
        sh = 1;
+       lbn = bn;
        bn -= NDADDR;
        for (j = NIADDR; j>0; j--) {
                sh *= NINDIR(fs);
        bn -= NDADDR;
        for (j = NIADDR; j>0; j--) {
                sh *= NINDIR(fs);
@@ -133,8 +141,11 @@ gotit:
         */
        nb = ip->i_ib[NIADDR - j];
        if (nb == 0) {
         */
        nb = ip->i_ib[NIADDR - j];
        if (nb == 0) {
-               if (rwflg==B_READ ||
-                   (bp = alloc(ip, (daddr_t)0, fs->fs_bsize)) == NULL)
+               if (rwflg == B_READ)
+                       return ((daddr_t)-1);
+               pref = blkpref(ip, lbn, 0, (daddr_t *)0);
+               bp = alloc(ip, pref, (int)fs->fs_bsize);
+               if (bp == NULL)
                        return ((daddr_t)-1);
                nb = dbtofsb(fs, bp->b_blkno);
                /*
                        return ((daddr_t)-1);
                nb = dbtofsb(fs, bp->b_blkno);
                /*
@@ -150,7 +161,12 @@ gotit:
         * fetch through the indirect blocks
         */
        for (; j <= NIADDR; j++) {
         * fetch through the indirect blocks
         */
        for (; j <= NIADDR; j++) {
-               bp = bread(ip->i_dev, fsbtodb(fs, nb), fs->fs_bsize);
+#ifdef SECSIZE
+               bp = bread(ip->i_dev, fsbtodb(fs, nb), (int)fs->fs_bsize,
+                   fs->fs_dbsize);
+#else SECSIZE
+               bp = bread(ip->i_dev, fsbtodb(fs, nb), (int)fs->fs_bsize);
+#endif SECSIZE
                if (bp->b_flags & B_ERROR) {
                        brelse(bp);
                        return ((daddr_t)0);
                if (bp->b_flags & B_ERROR) {
                        brelse(bp);
                        return ((daddr_t)0);
@@ -164,12 +180,13 @@ gotit:
                                brelse(bp);
                                return ((daddr_t)-1);
                        }
                                brelse(bp);
                                return ((daddr_t)-1);
                        }
-                       if (i % (fs->fs_fsize / sizeof(daddr_t)) == 0 ||
-                           bap[i - 1] == 0)
-                               pref = blkpref(ip->i_fs);
-                       else
-                               pref = bap[i - 1] + fs->fs_frag;
-                       nbp = alloc(ip, pref, fs->fs_bsize);
+                       if (pref == 0)
+                               if (j < NIADDR)
+                                       pref = blkpref(ip, lbn, 0,
+                                               (daddr_t *)0);
+                               else
+                                       pref = blkpref(ip, lbn, i, &bap[0]);
+                       nbp = alloc(ip, pref, (int)fs->fs_bsize);
                        if (nbp == NULL) {
                                brelse(bp);
                                return ((daddr_t)-1);
                        if (nbp == NULL) {
                                brelse(bp);
                                return ((daddr_t)-1);