123396Smckusick /*
263375Sbostic * Copyright (c) 1989, 1991, 1993
363375Sbostic * The Regents of the University of California. All rights reserved.
423396Smckusick *
544537Sbostic * %sccs.include.redist.c%
637736Smckusick *
7*69294Smckusick * @(#)lfs_balloc.c 8.4 (Berkeley) 05/08/95
823396Smckusick */
951486Sbostic #include <sys/param.h>
1051486Sbostic #include <sys/buf.h>
1151486Sbostic #include <sys/proc.h>
1251486Sbostic #include <sys/vnode.h>
1351486Sbostic #include <sys/mount.h>
1451486Sbostic #include <sys/resourcevar.h>
1551486Sbostic #include <sys/trace.h>
167443Sroot
1755048Smckusick #include <miscfs/specfs/specdev.h>
1855048Smckusick
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
2651855Sbostic int
lfs_balloc(vp,offset,iosize,lbn,bpp)2769286Smckusick lfs_balloc(vp, offset, iosize, lbn, bpp)
2852082Sbostic struct vnode *vp;
2969286Smckusick int offset;
3052082Sbostic u_long iosize;
3168550Smckusick ufs_daddr_t lbn;
3252082Sbostic struct buf **bpp;
3352082Sbostic {
3456609Smargo struct buf *ibp, *bp;
3552082Sbostic struct inode *ip;
3652082Sbostic struct lfs *fs;
3756609Smargo struct indir indirs[NIADDR+2];
38*69294Smckusick ufs_daddr_t daddr, lastblock;
3969286Smckusick int bb; /* number of disk blocks in a block disk blocks */
4069286Smckusick int error, frags, i, nsize, osize, num;
4152082Sbostic
4252082Sbostic ip = VTOI(vp);
4352082Sbostic fs = ip->i_lfs;
4452082Sbostic
4552082Sbostic /*
4652082Sbostic * Three cases: it's a block beyond the end of file, it's a block in
4752082Sbostic * the file that may or may not have been assigned a disk address or
4852082Sbostic * we're writing an entire block. Note, if the daddr is unassigned,
4956609Smargo * the block might still have existed in the cache (if it was read
5056609Smargo * or written earlier). If it did, make sure we don't count it as a
5156609Smargo * new block or zero out its contents. If it did not, make sure
5256609Smargo * we allocate any necessary indirect blocks.
5369286Smckusick * If we are writing a block beyond the end of the file, we need to
5469286Smckusick * check if the old last block was a fragment. If it was, we need
5569286Smckusick * to rewrite it.
5652082Sbostic */
5755942Sbostic
5855942Sbostic *bpp = NULL;
5956609Smargo if (error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL ))
6052994Sbostic return (error);
6152082Sbostic
6269286Smckusick /* Check for block beyond end of file and fragment extension needed. */
6369286Smckusick lastblock = lblkno(fs, ip->i_size);
6469286Smckusick if (lastblock < NDADDR && lastblock < lbn) {
6569286Smckusick osize = blksize(fs, ip, lastblock);
6669286Smckusick if (osize < fs->lfs_bsize && osize > 0) {
6769286Smckusick if (error = lfs_fragextend(vp, osize, fs->lfs_bsize,
6869286Smckusick lastblock, &bp))
6969286Smckusick return(error);
7069286Smckusick ip->i_size = (lastblock + 1) * fs->lfs_bsize;
7169286Smckusick vnode_pager_setsize(vp, (u_long)ip->i_size);
7269286Smckusick ip->i_flag |= IN_CHANGE | IN_UPDATE;
7369286Smckusick VOP_BWRITE(bp);
7469286Smckusick }
7569286Smckusick }
7669286Smckusick
7756609Smargo bb = VFSTOUFS(vp->v_mount)->um_seqinc;
7856609Smargo if (daddr == UNASSIGNED)
7956609Smargo /* May need to allocate indirect blocks */
8056609Smargo for (i = 1; i < num; ++i)
8156609Smargo if (!indirs[i].in_exists) {
8269286Smckusick ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize,
8369286Smckusick 0, 0);
8469286Smckusick if ((ibp->b_flags & (B_DONE | B_DELWRI)))
8556609Smargo panic ("Indirect block should not exist");
8669286Smckusick
8769286Smckusick if (!ISSPACE(fs, bb, curproc->p_ucred)){
8869286Smckusick ibp->b_flags |= B_INVAL;
8969286Smckusick brelse(ibp);
9069286Smckusick return(ENOSPC);
9169286Smckusick } else {
9269286Smckusick ip->i_blocks += bb;
9369286Smckusick ip->i_lfs->lfs_bfree -= bb;
9469286Smckusick clrbuf(ibp);
9569286Smckusick if(error = VOP_BWRITE(ibp))
9669286Smckusick return(error);
9769286Smckusick }
9856609Smargo }
9969286Smckusick
10069286Smckusick /*
10169286Smckusick * If the block we are writing is a direct block, it's the last
10269286Smckusick * block in the file, and offset + iosize is less than a full
10369286Smckusick * block, we can write one or more fragments. There are two cases:
10469286Smckusick * the block is brand new and we should allocate it the correct
10569286Smckusick * size or it already exists and contains some fragments and
10669286Smckusick * may need to extend it.
10769286Smckusick */
10869286Smckusick if (lbn < NDADDR && lblkno(fs, ip->i_size) == lbn) {
10969286Smckusick nsize = fragroundup(fs, offset + iosize);
11069286Smckusick frags = numfrags(fs, nsize);
11169286Smckusick bb = fragstodb(fs, frags);
11269286Smckusick if (lblktosize(fs, lbn) == ip->i_size)
11369286Smckusick /* Brand new block or fragment */
11469286Smckusick *bpp = bp = getblk(vp, lbn, nsize, 0, 0);
11569286Smckusick else {
11669286Smckusick /* Extend existing block */
11769286Smckusick if (error = lfs_fragextend(vp, (int)blksize(fs, ip, lbn),
11869286Smckusick nsize, lbn, &bp))
11969286Smckusick return(error);
12069286Smckusick *bpp = bp;
12169286Smckusick }
12269286Smckusick } else {
12369286Smckusick /*
12469286Smckusick * Get the existing block from the cache either because the
12569286Smckusick * block is 1) not a direct block or because it's not the last
12669286Smckusick * block in the file.
12769286Smckusick */
12869286Smckusick frags = dbtofrags(fs, bb);
12969286Smckusick *bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
13056609Smargo }
13156609Smargo
13269286Smckusick /*
13369286Smckusick * The block we are writing may be a brand new block
13469286Smckusick * in which case we need to do accounting (i.e. check
13569286Smckusick * for free space and update the inode number of blocks.
13669286Smckusick */
13756609Smargo if (!(bp->b_flags & (B_CACHE | B_DONE | B_DELWRI))) {
13856609Smargo if (daddr == UNASSIGNED)
13956609Smargo if (!ISSPACE(fs, bb, curproc->p_ucred)) {
14056160Smargo bp->b_flags |= B_INVAL;
14156160Smargo brelse(bp);
14256609Smargo return(ENOSPC);
14356609Smargo } else {
14456609Smargo ip->i_blocks += bb;
14556609Smargo ip->i_lfs->lfs_bfree -= bb;
14656609Smargo if (iosize != fs->lfs_bsize)
14756609Smargo clrbuf(bp);
14856160Smargo }
14956609Smargo else if (iosize == fs->lfs_bsize)
15069286Smckusick /* Optimization: I/O is unnecessary. */
15169286Smckusick bp->b_blkno = daddr;
15256609Smargo else {
15369286Smckusick /*
15469286Smckusick * We need to read the block to preserve the
15569286Smckusick * existing bytes.
15669286Smckusick */
15756609Smargo bp->b_blkno = daddr;
15857063Smargo bp->b_flags |= B_READ;
15956609Smargo VOP_STRATEGY(bp);
16056864Smargo return(biowait(bp));
16152082Sbostic }
16252082Sbostic }
16369286Smckusick return (0);
16452082Sbostic }
16569286Smckusick
16669286Smckusick lfs_fragextend(vp, osize, nsize, lbn, bpp)
16769286Smckusick struct vnode *vp;
16869286Smckusick int osize;
16969286Smckusick int nsize;
17069286Smckusick daddr_t lbn;
17169286Smckusick struct buf **bpp;
17269286Smckusick {
17369286Smckusick struct inode *ip;
17469286Smckusick struct lfs *fs;
17569286Smckusick long bb;
17669286Smckusick int error;
17769286Smckusick
17869286Smckusick ip = VTOI(vp);
17969286Smckusick fs = ip->i_lfs;
18069286Smckusick bb = (long)fragstodb(fs, numfrags(fs, nsize - osize));
18169286Smckusick if (!ISSPACE(fs, bb, curproc->p_ucred)) {
18269286Smckusick return(ENOSPC);
18369286Smckusick }
18469286Smckusick
18569286Smckusick if (error = bread(vp, lbn, osize, NOCRED, bpp)) {
18669286Smckusick brelse(*bpp);
18769286Smckusick return(error);
18869286Smckusick }
18969286Smckusick #ifdef QUOTA
19069286Smckusick if (error = chkdq(ip, bb, curproc->p_ucred, 0)) {
19169286Smckusick brelse(*bpp);
19269286Smckusick return (error);
19369286Smckusick }
19469286Smckusick #endif
19569286Smckusick ip->i_blocks += bb;
19669286Smckusick ip->i_flag |= IN_CHANGE | IN_UPDATE;
19769286Smckusick fs->lfs_bfree -= fragstodb(fs, numfrags(fs, (nsize - osize)));
19869286Smckusick allocbuf(*bpp, nsize);
19969286Smckusick bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
20069286Smckusick return(0);
20169286Smckusick }
202