xref: /original-bsd/sys/ufs/lfs/lfs_balloc.c (revision b3c06cab)
1 /*
2  * Copyright (c) 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)lfs_balloc.c	8.4 (Berkeley) 05/08/95
8  */
9 #include <sys/param.h>
10 #include <sys/buf.h>
11 #include <sys/proc.h>
12 #include <sys/vnode.h>
13 #include <sys/mount.h>
14 #include <sys/resourcevar.h>
15 #include <sys/trace.h>
16 
17 #include <miscfs/specfs/specdev.h>
18 
19 #include <ufs/ufs/quota.h>
20 #include <ufs/ufs/inode.h>
21 #include <ufs/ufs/ufsmount.h>
22 
23 #include <ufs/lfs/lfs.h>
24 #include <ufs/lfs/lfs_extern.h>
25 
26 int
27 lfs_balloc(vp, offset, iosize, lbn, bpp)
28 	struct vnode *vp;
29 	int offset;
30 	u_long iosize;
31 	ufs_daddr_t lbn;
32 	struct buf **bpp;
33 {
34 	struct buf *ibp, *bp;
35 	struct inode *ip;
36 	struct lfs *fs;
37 	struct indir indirs[NIADDR+2];
38 	ufs_daddr_t	daddr, lastblock;
39  	int bb;		/* number of disk blocks in a block disk blocks */
40  	int error, frags, i, nsize, osize, num;
41 
42 	ip = VTOI(vp);
43 	fs = ip->i_lfs;
44 
45 	/*
46 	 * Three cases: it's a block beyond the end of file, it's a block in
47 	 * the file that may or may not have been assigned a disk address or
48 	 * we're writing an entire block.  Note, if the daddr is unassigned,
49 	 * the block might still have existed in the cache (if it was read
50 	 * or written earlier).  If it did, make sure we don't count it as a
51 	 * new block or zero out its contents.  If it did not, make sure
52 	 * we allocate any necessary indirect blocks.
53 	 * If we are writing a block beyond the end of the file, we need to
54 	 * check if the old last block was a fragment.  If it was, we need
55 	 * to rewrite it.
56 	 */
57 
58 	*bpp = NULL;
59 	if (error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL ))
60 		return (error);
61 
62 	/* Check for block beyond end of file and fragment extension needed. */
63 	lastblock = lblkno(fs, ip->i_size);
64 	if (lastblock < NDADDR && lastblock < lbn) {
65 		osize = blksize(fs, ip, lastblock);
66 		if (osize < fs->lfs_bsize && osize > 0) {
67 			if (error = lfs_fragextend(vp, osize, fs->lfs_bsize,
68 			    lastblock, &bp))
69 				return(error);
70 			ip->i_size = (lastblock + 1) * fs->lfs_bsize;
71 			vnode_pager_setsize(vp, (u_long)ip->i_size);
72 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
73 			VOP_BWRITE(bp);
74 		}
75 	}
76 
77 	bb = VFSTOUFS(vp->v_mount)->um_seqinc;
78 	if (daddr == UNASSIGNED)
79 		/* May need to allocate indirect blocks */
80 		for (i = 1; i < num; ++i)
81 			if (!indirs[i].in_exists) {
82 				ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize,
83 				    0, 0);
84 				if ((ibp->b_flags & (B_DONE | B_DELWRI)))
85 					panic ("Indirect block should not exist");
86 
87 				if (!ISSPACE(fs, bb, curproc->p_ucred)){
88 					ibp->b_flags |= B_INVAL;
89 					brelse(ibp);
90 					return(ENOSPC);
91 				} else {
92 					ip->i_blocks += bb;
93 					ip->i_lfs->lfs_bfree -= bb;
94 					clrbuf(ibp);
95 					if(error = VOP_BWRITE(ibp))
96 						return(error);
97 				}
98 			}
99 
100 	/*
101 	 * If the block we are writing is a direct block, it's the last
102 	 * block in the file, and offset + iosize is less than a full
103 	 * block, we can write one or more fragments.  There are two cases:
104 	 * the block is brand new and we should allocate it the correct
105 	 * size or it already exists and contains some fragments and
106 	 * may need to extend it.
107 	 */
108 	if (lbn < NDADDR && lblkno(fs, ip->i_size) == lbn) {
109 		nsize = fragroundup(fs, offset + iosize);
110 		frags = numfrags(fs, nsize);
111 		bb = fragstodb(fs, frags);
112 		if (lblktosize(fs, lbn) == ip->i_size)
113 			/* Brand new block or fragment */
114 			*bpp = bp = getblk(vp, lbn, nsize, 0, 0);
115 		else {
116 			/* Extend existing block */
117 			if (error = lfs_fragextend(vp, (int)blksize(fs, ip, lbn),
118 			    nsize, lbn, &bp))
119 				return(error);
120 			*bpp = bp;
121 		}
122 	} else {
123 		/*
124 		 * Get the existing block from the cache either because the
125 		 * block is 1) not a direct block or because it's not the last
126 		 * block in the file.
127 		 */
128 		frags = dbtofrags(fs, bb);
129 		*bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
130 	}
131 
132 	/*
133 	 * The block we are writing may be a brand new block
134 	 * in which case we need to do accounting (i.e. check
135 	 * for free space and update the inode number of blocks.
136 	 */
137 	if (!(bp->b_flags & (B_CACHE | B_DONE | B_DELWRI))) {
138 		if (daddr == UNASSIGNED)
139 			if (!ISSPACE(fs, bb, curproc->p_ucred)) {
140 				bp->b_flags |= B_INVAL;
141 				brelse(bp);
142 				return(ENOSPC);
143 			} else {
144 				ip->i_blocks += bb;
145 				ip->i_lfs->lfs_bfree -= bb;
146 				if (iosize != fs->lfs_bsize)
147 					clrbuf(bp);
148 			}
149 		else if (iosize == fs->lfs_bsize)
150 			/* Optimization: I/O is unnecessary. */
151 			bp->b_blkno = daddr;
152 		else  {
153 			/*
154 			 * We need to read the block to preserve the
155 			 * existing bytes.
156 			 */
157 			bp->b_blkno = daddr;
158 			bp->b_flags |= B_READ;
159 			VOP_STRATEGY(bp);
160 			return(biowait(bp));
161 		}
162 	}
163 	return (0);
164 }
165 
166 lfs_fragextend(vp, osize, nsize, lbn, bpp)
167 	struct vnode *vp;
168 	int osize;
169 	int nsize;
170 	daddr_t lbn;
171 	struct buf **bpp;
172 {
173 	struct inode *ip;
174 	struct lfs *fs;
175 	long bb;
176 	int error;
177 
178 	ip = VTOI(vp);
179 	fs = ip->i_lfs;
180 	bb = (long)fragstodb(fs, numfrags(fs, nsize - osize));
181 	if (!ISSPACE(fs, bb, curproc->p_ucred)) {
182 		return(ENOSPC);
183 	}
184 
185 	if (error = bread(vp, lbn, osize, NOCRED, bpp)) {
186 		brelse(*bpp);
187 		return(error);
188 	}
189 #ifdef QUOTA
190 	if (error = chkdq(ip, bb, curproc->p_ucred, 0)) {
191 		brelse(*bpp);
192 		return (error);
193 	}
194 #endif
195 	ip->i_blocks += bb;
196 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
197 	fs->lfs_bfree -= fragstodb(fs, numfrags(fs, (nsize - osize)));
198 	allocbuf(*bpp, nsize);
199 	bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
200 	return(0);
201 }
202