lfs_balloc.c revision 1.7 1 /* $NetBSD: lfs_balloc.c,v 1.7 1998/03/03 09:02:51 drochner Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)lfs_balloc.c 8.4 (Berkeley) 5/8/95
36 */
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/buf.h>
40 #include <sys/proc.h>
41 #include <sys/vnode.h>
42 #include <sys/mount.h>
43 #include <sys/resourcevar.h>
44 #include <sys/trace.h>
45
46 #include <miscfs/specfs/specdev.h>
47
48 #include <ufs/ufs/quota.h>
49 #include <ufs/ufs/inode.h>
50 #include <ufs/ufs/ufsmount.h>
51 #include <ufs/ufs/ufs_extern.h>
52
53 #include <ufs/lfs/lfs.h>
54 #include <ufs/lfs/lfs_extern.h>
55
56 #include <vm/vm.h>
57 #include <vm/vm_extern.h>
58
59 int lfs_fragextend __P((struct vnode *, int, int, ufs_daddr_t, struct buf **));
60
61 int
62 lfs_balloc(vp, offset, iosize, lbn, bpp)
63 struct vnode *vp;
64 int offset;
65 u_long iosize;
66 ufs_daddr_t lbn;
67 struct buf **bpp;
68 {
69 struct buf *ibp, *bp;
70 struct inode *ip;
71 struct lfs *fs;
72 struct indir indirs[NIADDR+2];
73 ufs_daddr_t daddr, lastblock;
74 int bb; /* number of disk blocks in a block disk blocks */
75 int error, frags, i, nsize, osize, num;
76
77 ip = VTOI(vp);
78 fs = ip->i_lfs;
79
80 /*
81 * Three cases: it's a block beyond the end of file, it's a block in
82 * the file that may or may not have been assigned a disk address or
83 * we're writing an entire block. Note, if the daddr is unassigned,
84 * the block might still have existed in the cache (if it was read
85 * or written earlier). If it did, make sure we don't count it as a
86 * new block or zero out its contents. If it did not, make sure
87 * we allocate any necessary indirect blocks.
88 * If we are writing a block beyond the end of the file, we need to
89 * check if the old last block was a fragment. If it was, we need
90 * to rewrite it.
91 */
92
93 *bpp = NULL;
94 error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
95 if (error)
96 return (error);
97
98 /* Check for block beyond end of file and fragment extension needed. */
99 lastblock = lblkno(fs, ip->i_ffs_size);
100 if (lastblock < NDADDR && lastblock < lbn) {
101 osize = blksize(fs, ip, lastblock);
102 if (osize < fs->lfs_bsize && osize > 0) {
103 if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
104 lastblock, &bp)))
105 return(error);
106 ip->i_ffs_size = (lastblock + 1) * fs->lfs_bsize;
107 #if defined(UVM)
108 uvm_vnp_setsize(vp, ip->i_ffs_size);
109 #else
110 vnode_pager_setsize(vp, ip->i_ffs_size);
111 #endif
112 ip->i_flag |= IN_CHANGE | IN_UPDATE;
113 VOP_BWRITE(bp);
114 }
115 }
116
117 bb = VFSTOUFS(vp->v_mount)->um_seqinc;
118 if (daddr == UNASSIGNED)
119 /* May need to allocate indirect blocks */
120 for (i = 1; i < num; ++i)
121 if (!indirs[i].in_exists) {
122 ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize,
123 0, 0);
124 if ((ibp->b_flags & (B_DONE | B_DELWRI)))
125 panic ("Indirect block should not exist");
126
127 if (!ISSPACE(fs, bb, curproc->p_ucred)){
128 ibp->b_flags |= B_INVAL;
129 brelse(ibp);
130 return(ENOSPC);
131 } else {
132 ip->i_ffs_blocks += bb;
133 ip->i_lfs->lfs_bfree -= bb;
134 clrbuf(ibp);
135 if ((error = VOP_BWRITE(ibp)))
136 return(error);
137 }
138 }
139
140 /*
141 * If the block we are writing is a direct block, it's the last
142 * block in the file, and offset + iosize is less than a full
143 * block, we can write one or more fragments. There are two cases:
144 * the block is brand new and we should allocate it the correct
145 * size or it already exists and contains some fragments and
146 * may need to extend it.
147 */
148 if (lbn < NDADDR && lblkno(fs, ip->i_ffs_size) == lbn) {
149 nsize = fragroundup(fs, offset + iosize);
150 frags = numfrags(fs, nsize);
151 bb = fragstodb(fs, frags);
152 if (lblktosize(fs, lbn) == ip->i_ffs_size)
153 /* Brand new block or fragment */
154 *bpp = bp = getblk(vp, lbn, nsize, 0, 0);
155 else {
156 /* Extend existing block */
157 if ((error =
158 lfs_fragextend(vp, (int)blksize(fs, ip, lbn),
159 nsize, lbn, &bp)))
160 return(error);
161 *bpp = bp;
162 }
163 } else {
164 /*
165 * Get the existing block from the cache either because the
166 * block is 1) not a direct block or because it's not the last
167 * block in the file.
168 */
169 frags = dbtofrags(fs, bb);
170 *bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
171 }
172
173 /*
174 * The block we are writing may be a brand new block
175 * in which case we need to do accounting (i.e. check
176 * for free space and update the inode number of blocks.
177 */
178 if (!(bp->b_flags & (B_CACHE | B_DONE | B_DELWRI))) {
179 if (daddr == UNASSIGNED)
180 if (!ISSPACE(fs, bb, curproc->p_ucred)) {
181 bp->b_flags |= B_INVAL;
182 brelse(bp);
183 return(ENOSPC);
184 } else {
185 ip->i_ffs_blocks += bb;
186 ip->i_lfs->lfs_bfree -= bb;
187 if (iosize != fs->lfs_bsize)
188 clrbuf(bp);
189 }
190 else if (iosize == fs->lfs_bsize)
191 /* Optimization: I/O is unnecessary. */
192 bp->b_blkno = daddr;
193 else {
194 /*
195 * We need to read the block to preserve the
196 * existing bytes.
197 */
198 bp->b_blkno = daddr;
199 bp->b_flags |= B_READ;
200 VOP_STRATEGY(bp);
201 return(biowait(bp));
202 }
203 }
204 return (0);
205 }
206
207 int
208 lfs_fragextend(vp, osize, nsize, lbn, bpp)
209 struct vnode *vp;
210 int osize;
211 int nsize;
212 ufs_daddr_t lbn;
213 struct buf **bpp;
214 {
215 struct inode *ip;
216 struct lfs *fs;
217 long bb;
218 int error;
219
220 ip = VTOI(vp);
221 fs = ip->i_lfs;
222 bb = (long)fragstodb(fs, numfrags(fs, nsize - osize));
223 if (!ISSPACE(fs, bb, curproc->p_ucred)) {
224 return(ENOSPC);
225 }
226
227 if ((error = bread(vp, lbn, osize, NOCRED, bpp))) {
228 brelse(*bpp);
229 return(error);
230 }
231 #ifdef QUOTA
232 if ((error = chkdq(ip, bb, curproc->p_ucred, 0))) {
233 brelse(*bpp);
234 return (error);
235 }
236 #endif
237 ip->i_ffs_blocks += bb;
238 ip->i_flag |= IN_CHANGE | IN_UPDATE;
239 fs->lfs_bfree -= fragstodb(fs, numfrags(fs, (nsize - osize)));
240 allocbuf(*bpp, nsize);
241 bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
242 return(0);
243 }
244