LFS version 2; minor cleanups, nothing important
[unix-history] / usr / src / sys / ufs / lfs / lfs_balloc.c
index bbcdc04..ca75b9b 100644 (file)
@@ -4,7 +4,7 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)lfs_balloc.c        7.20 (Berkeley) %G%
+ *     @(#)lfs_balloc.c        7.22 (Berkeley) %G%
  */
 
 #include <sys/param.h>
  */
 
 #include <sys/param.h>
  * Bmap converts a the logical block number of a file to its physical block
  * number on the disk. The conversion is done by using the logical block
  * number to index into the array of block pointers described by the dinode.
  * Bmap converts a the logical block number of a file to its physical block
  * number on the disk. The conversion is done by using the logical block
  * number to index into the array of block pointers described by the dinode.
+ *
+ * LFS has a different version of bmap from FFS because of a naming conflict.
+ * In FFS, meta blocks are given real disk addresses at allocation time, and
+ * are linked into the device vnode, using a logical block number which is
+ * the same as the physical block number.  This can't be done by LFS because
+ * blocks aren't given disk addresses until they're written, so there's no
+ * way to distinguish the meta-data blocks for one file from any other file.
+ * This means that meta-data blocks have to be on the vnode for the file so
+ * they can be found, and have to have "names" different from the standard
+ * data blocks.  To do this, we divide the name space into positive and
+ * negative block numbers, and give the meta-data blocks negative logical
+ * numbers.  Indirect blocks are addressed by the negative address of the
+ * first data block to which they point.  Double indirect blocks are addressed
+ * by one less than the address of the first indirect block to which they
+ * point.  Triple indirect blocks are addressed by one less than the address
+ * of the first double indirect block to which they point.
  */
 int
 lfs_bmap(vp, bn, vpp, bnp)
  */
 int
 lfs_bmap(vp, bn, vpp, bnp)
@@ -38,12 +54,11 @@ lfs_bmap(vp, bn, vpp, bnp)
        register struct inode *ip;
        register struct lfs *fs;
        register daddr_t nb;
        register struct inode *ip;
        register struct lfs *fs;
        register daddr_t nb;
-       struct vnode *devvp;
        struct buf *bp;
        struct buf *bp;
-       daddr_t *bap, daddr;
-       daddr_t lbn_ind;
-       int j, off, sh;
-       int error;
+       struct vnode *devvp;
+       daddr_t *bap, daddr, metalbn;
+       long realbn;
+       int error, j, off, sh;
 
        /*
         * Check for underlying vnode requests and ensure that logical
 
        /*
         * Check for underlying vnode requests and ensure that logical
@@ -54,25 +69,13 @@ lfs_bmap(vp, bn, vpp, bnp)
                *vpp = ip->i_devvp;
        if (bnp == NULL)
                return (0);
                *vpp = ip->i_devvp;
        if (bnp == NULL)
                return (0);
-printf("lfs_bmap: block number %d, inode %d\n", bn, ip->i_number);
-       fs = ip->i_lfs;
 
 
-       /*
-        * We access all blocks in the cache, even indirect blocks by means
-        * of a logical address. Indirect blocks (single, double, triple) all
-        * have negative block numbers. The first NDADDR blocks are direct
-        * blocks, the first NIADDR negative blocks are the indirect block
-        * pointers.  The single, double and triple indirect blocks in the
-        * inode * are addressed: -1, -2 and -3 respectively.  
-        *
-        * XXX
-        * We don't handle triple indirect at all.
-        *
-        * XXX
-        * This panic shouldn't be here???
-        */
-       if (bn < 0)
-               panic("lfs_bmap: negative indirect block number %d", bn);
+#ifdef VERBOSE
+printf("lfs_bmap: block number %d, inode %d\n", bn, ip->i_number);
+#endif
+       realbn = bn;
+       if ((long)bn < 0)
+               bn = -(long)bn;
 
        /* The first NDADDR blocks are direct blocks. */
        if (bn < NDADDR) {
 
        /* The first NDADDR blocks are direct blocks. */
        if (bn < NDADDR) {
@@ -85,12 +88,16 @@ printf("lfs_bmap: block number %d, inode %d\n", bn, ip->i_number);
                return (0);
        }
 
                return (0);
        }
 
-       /* Determine the number of levels of indirection. */
-       sh = 1;
+       /* 
+        * Determine the number of levels of indirection.  After this loop
+        * is done, sh indicates the number of data blocks possible at the
+        * given level of indirection, and NIADDR - j is the number of levels
+        * of indirection needed to locate the requested block.
+        */
        bn -= NDADDR;
        bn -= NDADDR;
-       lbn_ind = 0;
+       fs = ip->i_lfs;
+       sh = 1;
        for (j = NIADDR; j > 0; j--) {
        for (j = NIADDR; j > 0; j--) {
-               lbn_ind--;
                sh *= NINDIR(fs);
                if (bn < sh)
                        break;
                sh *= NINDIR(fs);
                if (bn < sh)
                        break;
@@ -99,21 +106,52 @@ printf("lfs_bmap: block number %d, inode %d\n", bn, ip->i_number);
        if (j == 0)
                return (EFBIG);
 
        if (j == 0)
                return (EFBIG);
 
-       /* Fetch through the indirect blocks. */
-       vp = ITOV(ip);
+       /* Calculate the address of the first meta-block. */
+       if (realbn >= 0)
+               metalbn = -(realbn - bn + NIADDR - j);
+       else
+               metalbn = -(-realbn - bn + NIADDR - j);
+
+       /* 
+        * Fetch through the indirect blocks.  At each iteration, off is the
+        * offset into the bap array which is an array of disk addresses at
+        * the current level of indirection.
+        */
+       bp = NULL;
        devvp = VFSTOUFS(vp->v_mount)->um_devvp;
        for (off = NIADDR - j, bap = ip->i_ib; j <= NIADDR; j++) {
        devvp = VFSTOUFS(vp->v_mount)->um_devvp;
        for (off = NIADDR - j, bap = ip->i_ib; j <= NIADDR; j++) {
-               if((daddr = bap[off]) == 0) {
+               /*
+                * In LFS, it's possible to have a block appended to a file
+                * for which the meta-blocks have not yet been allocated.
+                * This is a win if the file never gets written or if the
+                * file's growing.
+                */
+               if ((daddr = bap[off]) == 0) {
                        daddr = UNASSIGNED;
                        break;
                }
                        daddr = UNASSIGNED;
                        break;
                }
+
+               /* If searching for a meta-data block, quit when found. */
+               if (metalbn == realbn)
+                       break;
+
+               /*
+                * Read in the appropriate indirect block.  LFS can't do a
+                * bread because bread knows that FFS will hand it the device
+                * vnode, not the file vnode, so the b_dev and b_blkno would
+                * be wrong.
+                *
+                * XXX
+                * This REALLY needs to be fixed, at the very least it needs
+                * to be rethought when the buffer cache goes away.
+                */
                if (bp)
                        brelse(bp);
                if (bp)
                        brelse(bp);
-               bp = getblk(vp, lbn_ind, fs->lfs_bsize);
+               bp = getblk(vp, metalbn, fs->lfs_bsize);
                if (bp->b_flags & (B_DONE | B_DELWRI)) {
                if (bp->b_flags & (B_DONE | B_DELWRI)) {
-                       trace(TR_BREADHIT, pack(vp, size), lbn_ind);
+                       trace(TR_BREADHIT, pack(vp, size), metalbn);
                } else {
                } else {
-                       trace(TR_BREADMISS, pack(vp, size), lbn_ind);
+                       trace(TR_BREADMISS, pack(vp, size), metalbn);
                        bp->b_blkno = daddr;
                        bp->b_flags |= B_READ;
                        bp->b_dev = devvp->v_rdev;
                        bp->b_blkno = daddr;
                        bp->b_flags |= B_READ;
                        bp->b_dev = devvp->v_rdev;
@@ -124,10 +162,11 @@ printf("lfs_bmap: block number %d, inode %d\n", bn, ip->i_number);
                                return (error);
                        }
                }
                                return (error);
                        }
                }
+
                bap = bp->b_un.b_daddr;
                sh /= NINDIR(fs);
                off = (bn / sh) % NINDIR(fs);
                bap = bp->b_un.b_daddr;
                sh /= NINDIR(fs);
                off = (bn / sh) % NINDIR(fs);
-               lbn_ind  = -(NIADDR + 1 + off);
+               metalbn -= -1 + off * sh;
        }
        if (bp)
                brelse(bp);
        }
        if (bp)
                brelse(bp);