123396Smckusick /* 251494Sbostic * Copyright (c) 1989, 1991 Regents of the University of California. 337736Smckusick * All rights reserved. 423396Smckusick * 544537Sbostic * %sccs.include.redist.c% 637736Smckusick * 7*53591Sheideman * @(#)lfs_balloc.c 7.30 (Berkeley) 05/15/92 823396Smckusick */ 97443Sroot 1051486Sbostic #include <sys/param.h> 1151486Sbostic #include <sys/buf.h> 1251486Sbostic #include <sys/proc.h> 1351486Sbostic #include <sys/vnode.h> 1451486Sbostic #include <sys/mount.h> 1551486Sbostic #include <sys/resourcevar.h> 1651486Sbostic #include <sys/specdev.h> 1751486Sbostic #include <sys/trace.h> 187443Sroot 1951494Sbostic #include <ufs/ufs/quota.h> 2051494Sbostic #include <ufs/ufs/inode.h> 2151494Sbostic #include <ufs/ufs/ufsmount.h> 2247571Skarels 2351494Sbostic #include <ufs/lfs/lfs.h> 2451494Sbostic #include <ufs/lfs/lfs_extern.h> 2551486Sbostic 2651962Sbostic int lfs_getlbns __P((struct vnode *, daddr_t, INDIR *, int *)); 2751855Sbostic 287443Sroot /* 2951486Sbostic * Bmap converts a the logical block number of a file to its physical block 3051486Sbostic * number on the disk. The conversion is done by using the logical block 3151486Sbostic * number to index into the array of block pointers described by the dinode. 3251855Sbostic */ 3351855Sbostic int 3453528Sheideman lfs_bmap (ap) 3553528Sheideman struct vop_bmap_args *ap; 3651855Sbostic { 3751855Sbostic #ifdef VERBOSE 3851855Sbostic printf("lfs_bmap\n"); 3951855Sbostic #endif 4051855Sbostic /* 4151855Sbostic * Check for underlying vnode requests and ensure that logical 4251855Sbostic * to physical mapping is requested. 4351855Sbostic */ 44*53591Sheideman if (ap->a_vpp != NULL) 45*53591Sheideman *ap->a_vpp = VTOI(ap->a_vp)->i_devvp; 46*53591Sheideman if (ap->a_bnp == NULL) 4751855Sbostic return (0); 4851855Sbostic 49*53591Sheideman return (lfs_bmaparray(ap->a_vp, ap->a_bn, ap->a_bnp, NULL, NULL)); 5051855Sbostic } 5151855Sbostic 5251855Sbostic /* 5351846Sbostic * LFS has a different version of bmap from FFS because of a naming conflict. 5451846Sbostic * In FFS, meta blocks are given real disk addresses at allocation time, and 5551846Sbostic * are linked into the device vnode, using a logical block number which is 5651846Sbostic * the same as the physical block number. This can't be done by LFS because 5751846Sbostic * blocks aren't given disk addresses until they're written, so there's no 5851846Sbostic * way to distinguish the meta-data blocks for one file from any other file. 5951846Sbostic * This means that meta-data blocks have to be on the vnode for the file so 6051846Sbostic * they can be found, and have to have "names" different from the standard 6151846Sbostic * data blocks. To do this, we divide the name space into positive and 6251846Sbostic * negative block numbers, and give the meta-data blocks negative logical 6351847Sbostic * numbers. Indirect blocks are addressed by the negative address of the 6451847Sbostic * first data block to which they point. Double indirect blocks are addressed 6551847Sbostic * by one less than the address of the first indirect block to which they 6651847Sbostic * point. Triple indirect blocks are addressed by one less than the address 6751847Sbostic * of the first double indirect block to which they point. 687443Sroot */ 6951348Sroot int 7051855Sbostic lfs_bmaparray(vp, bn, bnp, ap, nump) 7151561Smckusick struct vnode *vp; 7237736Smckusick register daddr_t bn; 7351561Smckusick daddr_t *bnp; 7451855Sbostic INDIR *ap; 7551855Sbostic int *nump; 767443Sroot { 7753574Sheideman USES_VOP_STRATEGY; 7851561Smckusick register struct inode *ip; 7951846Sbostic struct buf *bp; 8051855Sbostic struct lfs *fs; 8151561Smckusick struct vnode *devvp; 8251855Sbostic INDIR a[NIADDR], *xap; 8351855Sbostic daddr_t *bap, daddr; 8451855Sbostic long metalbn; 8551855Sbostic int error, num, off; 8637736Smckusick 8751855Sbostic 8851561Smckusick ip = VTOI(vp); 8951846Sbostic #ifdef VERBOSE 9051855Sbostic printf("lfs_bmap: block number %d, inode %d\n", bn, ip->i_number); 9151846Sbostic #endif 9251855Sbostic #ifdef DIAGNOSTIC 9351855Sbostic if (ap != NULL && nump == NULL || ap == NULL && nump != NULL) 9451855Sbostic panic("lfs_bmaparray: invalid arguments"); 9551855Sbostic #endif 9651847Sbostic 9751855Sbostic xap = ap == NULL ? a : ap; 9851855Sbostic if (error = lfs_getlbns(vp, bn, xap, nump)) 9951855Sbostic return (error); 10051855Sbostic 10151855Sbostic num = *nump; 10252994Sbostic 10351855Sbostic if (num == 0) { 10451855Sbostic *bnp = ip->i_db[bn]; 10551855Sbostic if (*bnp == 0) 10651348Sroot *bnp = UNASSIGNED; 10737736Smckusick return (0); 10837736Smckusick } 10951486Sbostic 11052994Sbostic 11152994Sbostic /* Get disk address out of indirect block array */ 11252994Sbostic daddr = ip->i_ib[xap->in_off]; 11352994Sbostic 11451855Sbostic /* Fetch through the indirect blocks. */ 11552994Sbostic fs = ip->i_lfs; 11651183Sbostic devvp = VFSTOUFS(vp->v_mount)->um_devvp; 11752994Sbostic 11852994Sbostic for (bp = NULL, ++xap; daddr && --num; ++xap) { 11952994Sbostic /* If looking for a meta-block, break out when we find it. */ 12051855Sbostic metalbn = xap->in_lbn; 12151855Sbostic if (metalbn == bn) 12251847Sbostic break; 12351847Sbostic 12451846Sbostic /* 12551846Sbostic * Read in the appropriate indirect block. LFS can't do a 12651846Sbostic * bread because bread knows that FFS will hand it the device 12751846Sbostic * vnode, not the file vnode, so the b_dev and b_blkno would 12851846Sbostic * be wrong. 12951846Sbostic * 13051846Sbostic * XXX 13151846Sbostic * This REALLY needs to be fixed, at the very least it needs 13251855Sbostic * to be rethought when the buffer cache goes away. When it's 13351855Sbostic * fixed, change lfs_bmaparray and lfs_getlbns to take an ip, 13451855Sbostic * not a vp. 13551846Sbostic */ 13651183Sbostic if (bp) 13737736Smckusick brelse(bp); 13851847Sbostic bp = getblk(vp, metalbn, fs->lfs_bsize); 13951183Sbostic if (bp->b_flags & (B_DONE | B_DELWRI)) { 14051847Sbostic trace(TR_BREADHIT, pack(vp, size), metalbn); 14151183Sbostic } else { 14251847Sbostic trace(TR_BREADMISS, pack(vp, size), metalbn); 14351847Sbostic bp->b_blkno = daddr; 14451846Sbostic bp->b_flags |= B_READ; 14551183Sbostic bp->b_dev = devvp->v_rdev; 14653574Sheideman /* 14753574Sheideman * Call a strategy VOP by hand. 14853574Sheideman */ 14953574Sheideman vop_strategy_a.a_desc = VDESC(vop_strategy); 15053574Sheideman vop_strategy_a.a_bp=bp; 15153574Sheideman VOCALL(devvp->v_op, VOFFSET(vop_strategy), \ 15253574Sheideman &vop_strategy_a); 15351183Sbostic curproc->p_stats->p_ru.ru_inblock++; /* XXX */ 15451183Sbostic if (error = biowait(bp)) { 15551183Sbostic brelse(bp); 15651183Sbostic return (error); 15751183Sbostic } 15837736Smckusick } 15952994Sbostic daddr = bp->b_un.b_daddr[xap->in_off]; 16051183Sbostic } 16151183Sbostic if (bp) 16239679Smckusick brelse(bp); 16351183Sbostic 16452994Sbostic *bnp = daddr == 0 ? UNASSIGNED : daddr; 16537736Smckusick return (0); 16637736Smckusick } 16751855Sbostic 16851855Sbostic /* 16951855Sbostic * Create an array of logical block number/offset pairs which represent the 17051855Sbostic * path of indirect blocks required to access a data block. The first "pair" 17151855Sbostic * contains the logical block number of the appropriate single, double or 17251855Sbostic * triple indirect block and the offset into the inode indirect block array. 17351855Sbostic * Note, the logical block number of the inode single/double/triple indirect 17451855Sbostic * block appears twice in the array, once with the offset into the i_ib and 17551855Sbostic * once with the offset into the page itself. 17651855Sbostic */ 17751855Sbostic int 17851855Sbostic lfs_getlbns(vp, bn, ap, nump) 17951855Sbostic struct vnode *vp; 18051855Sbostic register daddr_t bn; 18151855Sbostic INDIR *ap; 18251855Sbostic int *nump; 18351855Sbostic { 18451855Sbostic struct lfs *fs; 18551855Sbostic long metalbn, realbn; 18651855Sbostic int j, off, sh; 18751855Sbostic 18851855Sbostic #ifdef VERBOSE 18951855Sbostic printf("lfs_getlbns: bn %d, inode %d\n", bn, VTOI(vp)->i_number); 19051855Sbostic #endif 19151855Sbostic *nump = 0; 19251855Sbostic realbn = bn; 19351855Sbostic if ((long)bn < 0) 19451855Sbostic bn = -(long)bn; 19551855Sbostic 19651855Sbostic /* The first NDADDR blocks are direct blocks. */ 19751855Sbostic if (bn < NDADDR) 19852994Sbostic return (0); 19951855Sbostic 20051855Sbostic /* 20151855Sbostic * Determine the number of levels of indirection. After this loop 20251855Sbostic * is done, sh indicates the number of data blocks possible at the 20351855Sbostic * given level of indirection, and NIADDR - j is the number of levels 20451855Sbostic * of indirection needed to locate the requested block. 20551855Sbostic */ 20651855Sbostic bn -= NDADDR; 20751855Sbostic fs = VTOI(vp)->i_lfs; 20851855Sbostic sh = 1; 20951855Sbostic for (j = NIADDR; j > 0; j--) { 21051855Sbostic sh *= NINDIR(fs); 21151855Sbostic if (bn < sh) 21251855Sbostic break; 21351855Sbostic bn -= sh; 21451855Sbostic } 21551855Sbostic if (j == 0) 21651855Sbostic return (EFBIG); 21751855Sbostic 21851855Sbostic /* Calculate the address of the first meta-block. */ 21951855Sbostic if (realbn >= 0) 22051855Sbostic metalbn = -(realbn - bn + NIADDR - j); 22151855Sbostic else 22251855Sbostic metalbn = -(-realbn - bn + NIADDR - j); 22351855Sbostic 22451855Sbostic /* 22551855Sbostic * At each iteration, off is the offset into the bap array which is 22651855Sbostic * an array of disk addresses at the current level of indirection. 22751855Sbostic * The logical block number and the offset in that block are stored 22851855Sbostic * into the argument array. 22951855Sbostic */ 23051855Sbostic ++*nump; 23151855Sbostic ap->in_lbn = metalbn; 23251855Sbostic ap->in_off = off = NIADDR - j; 23351855Sbostic ap++; 23451855Sbostic for (; j <= NIADDR; j++) { 23551855Sbostic /* If searching for a meta-data block, quit when found. */ 23651855Sbostic if (metalbn == realbn) 23751855Sbostic break; 23851855Sbostic 23951855Sbostic sh /= NINDIR(fs); 24051855Sbostic off = (bn / sh) % NINDIR(fs); 24151855Sbostic 24251855Sbostic ++*nump; 24351855Sbostic ap->in_lbn = metalbn; 24451855Sbostic ap->in_off = off; 24551855Sbostic ++ap; 24651855Sbostic 24751855Sbostic metalbn -= -1 + off * sh; 24851855Sbostic } 24951855Sbostic return (0); 25051855Sbostic } 25152082Sbostic 25252082Sbostic int 25352082Sbostic lfs_balloc(vp, iosize, lbn, bpp) 25452082Sbostic struct vnode *vp; 25552082Sbostic u_long iosize; 25652082Sbostic daddr_t lbn; 25752082Sbostic struct buf **bpp; 25852082Sbostic { 25953528Sheideman USES_VOP_BMAP; 26052082Sbostic struct buf *bp; 26152082Sbostic struct inode *ip; 26252082Sbostic struct lfs *fs; 26352082Sbostic daddr_t daddr; 26452082Sbostic int error, newblock; 26552082Sbostic 26652082Sbostic ip = VTOI(vp); 26752082Sbostic fs = ip->i_lfs; 26852082Sbostic 26952082Sbostic /* 27052082Sbostic * Three cases: it's a block beyond the end of file, it's a block in 27152082Sbostic * the file that may or may not have been assigned a disk address or 27252082Sbostic * we're writing an entire block. Note, if the daddr is unassigned, 27352082Sbostic * the block might still have existed in the cache. If it did, make 27452082Sbostic * sure we don't count it as a new block or zero out its contents. 27552082Sbostic */ 27652082Sbostic newblock = ip->i_size <= lbn << fs->lfs_bshift; 27753528Sheideman if (!newblock && (error = VOP_BMAP(vp, lbn, NULL, &daddr))) 27852994Sbostic return (error); 27952082Sbostic 28052172Sbostic if (newblock || daddr == UNASSIGNED || iosize == fs->lfs_bsize) { 28152082Sbostic *bpp = bp = getblk(vp, lbn, fs->lfs_bsize); 28252082Sbostic if (newblock || 28352172Sbostic daddr == UNASSIGNED && !(bp->b_flags & B_CACHE)) { 28452082Sbostic ++ip->i_blocks; 28552082Sbostic if (iosize != fs->lfs_bsize) 28652082Sbostic clrbuf(bp); 28752082Sbostic } 28852994Sbostic return (0); 28952082Sbostic } 29052994Sbostic return (bread(vp, lbn, fs->lfs_bsize, NOCRED, bpp)); 29152082Sbostic 29252082Sbostic } 293