lfs_balloc.c revision 1.29 1 /* $NetBSD: lfs_balloc.c,v 1.29 2001/07/13 20:30:23 perseant Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 /*
39 * Copyright (c) 1989, 1991, 1993
40 * The Regents of the University of California. All rights reserved.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the University of
53 * California, Berkeley and its contributors.
54 * 4. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)lfs_balloc.c 8.4 (Berkeley) 5/8/95
71 */
72
73 #if defined(_KERNEL_OPT)
74 #include "opt_quota.h"
75 #endif
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/buf.h>
80 #include <sys/proc.h>
81 #include <sys/vnode.h>
82 #include <sys/mount.h>
83 #include <sys/resourcevar.h>
84 #include <sys/trace.h>
85
86 #include <miscfs/specfs/specdev.h>
87
88 #include <ufs/ufs/quota.h>
89 #include <ufs/ufs/inode.h>
90 #include <ufs/ufs/ufsmount.h>
91 #include <ufs/ufs/ufs_extern.h>
92
93 #include <ufs/lfs/lfs.h>
94 #include <ufs/lfs/lfs_extern.h>
95
96 int lfs_fragextend(struct vnode *, int, int, ufs_daddr_t, struct buf **, struct ucred *);
97
98 /*
99 * Allocate a block, and to inode and filesystem block accounting for it
100 * and for any indirect blocks the may need to be created in order for
101 * this block to be created.
102 *
103 * Blocks which have never been accounted for (i.e., which "do not exist")
104 * have disk address 0, which is translated by ufs_bmap to the special value
105 * UNASSIGNED == -1, as in the historical UFS.
106 *
107 * Blocks which have been accounted for but which have not yet been written
108 * to disk are given the new special disk address UNWRITTEN == -2, so that
109 * they can be differentiated from completely new blocks.
110 */
111 /* VOP_BWRITE NIADDR+2 times */
112 int
113 lfs_balloc(void *v)
114 {
115 struct vop_balloc_args /* {
116 struct vnode *a_vp;
117 off_t a_startoffset;
118 int a_size;
119 struct ucred *a_cred;
120 int a_flags;
121 struct buf *a_bpp;
122 } */ *ap = v;
123 struct vnode *vp;
124 int offset;
125 u_long iosize;
126 daddr_t daddr, idaddr;
127 struct buf *ibp, *bp;
128 struct inode *ip;
129 struct lfs *fs;
130 struct indir indirs[NIADDR+2], *idp;
131 ufs_daddr_t lbn, lastblock;
132 int bb, bcount;
133 int error, frags, i, nsize, osize, num;
134
135 vp = ap->a_vp;
136 ip = VTOI(vp);
137 fs = ip->i_lfs;
138 offset = blkoff(fs, ap->a_startoffset);
139 iosize = ap->a_size;
140 lbn = lblkno(fs, ap->a_startoffset);
141 (void)lfs_check(vp, lbn, 0);
142
143 /*
144 * Three cases: it's a block beyond the end of file, it's a block in
145 * the file that may or may not have been assigned a disk address or
146 * we're writing an entire block.
147 *
148 * Note, if the daddr is UNWRITTEN, the block already exists in
149 * the cache (it was read or written earlier). If so, make sure
150 * we don't count it as a new block or zero out its contents. If
151 * it did not, make sure we allocate any necessary indirect
152 * blocks.
153 *
154 * If we are writing a block beyond the end of the file, we need to
155 * check if the old last block was a fragment. If it was, we need
156 * to rewrite it.
157 */
158
159 *ap->a_bpp = NULL;
160
161 /* Check for block beyond end of file and fragment extension needed. */
162 lastblock = lblkno(fs, ip->i_ffs_size);
163 if (lastblock < NDADDR && lastblock < lbn) {
164 osize = blksize(fs, ip, lastblock);
165 if (osize < fs->lfs_bsize && osize > 0) {
166 if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
167 lastblock, &bp,
168 ap->a_cred)))
169 return(error);
170 ip->i_ffs_size = (lastblock + 1) * fs->lfs_bsize;
171 uvm_vnp_setsize(vp, ip->i_ffs_size);
172 ip->i_flag |= IN_CHANGE | IN_UPDATE;
173 (void) VOP_BWRITE(bp);
174 }
175 }
176
177 /*
178 * If the block we are writing is a direct block, it's the last
179 * block in the file, and offset + iosize is less than a full
180 * block, we can write one or more fragments. There are two cases:
181 * the block is brand new and we should allocate it the correct
182 * size or it already exists and contains some fragments and
183 * may need to extend it.
184 */
185 if (lbn < NDADDR && lblkno(fs, ip->i_ffs_size) <= lbn) {
186 osize = blksize(fs, ip, lbn);
187 nsize = fragroundup(fs, offset + iosize);
188 if (lblktosize(fs, lbn) >= ip->i_ffs_size) {
189 /* Brand new block or fragment */
190 frags = numfrags(fs, nsize);
191 bb = fragstofsb(fs, frags);
192 *ap->a_bpp = bp = getblk(vp, lbn, nsize, 0, 0);
193 ip->i_lfs_effnblks += bb;
194 ip->i_lfs->lfs_bfree -= bb;
195 ip->i_ffs_db[lbn] = bp->b_blkno = UNWRITTEN;
196 } else {
197 if (nsize <= osize) {
198 /* No need to extend */
199 if ((error = bread(vp, lbn, osize, NOCRED, &bp)))
200 return error;
201 } else {
202 /* Extend existing block */
203 if ((error =
204 lfs_fragextend(vp, osize, nsize, lbn, &bp,
205 ap->a_cred)))
206 return error;
207 }
208 *ap->a_bpp = bp;
209 }
210 return 0;
211 }
212
213 error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
214 if (error)
215 return (error);
216 /*
217 * Do byte accounting all at once, so we can gracefully fail *before*
218 * we start assigning blocks.
219 */
220 bb = VFSTOUFS(vp->v_mount)->um_seqinc;
221 bcount = 0;
222 if (daddr == UNASSIGNED) {
223 bcount = bb;
224 }
225 for (i = 1; i < num; ++i) {
226 if (!indirs[i].in_exists) {
227 bcount += bb;
228 }
229 }
230 if (ISSPACE(fs, bcount, ap->a_cred)) {
231 ip->i_lfs->lfs_bfree -= bcount;
232 ip->i_lfs_effnblks += bcount;
233 } else {
234 return ENOSPC;
235 }
236
237 if (daddr == UNASSIGNED) {
238 if (num > 0 && ip->i_ffs_ib[indirs[0].in_off] == 0) {
239 ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
240 }
241
242 /*
243 * Create new indirect blocks if necessary
244 */
245 if (num > 1)
246 idaddr = ip->i_ffs_ib[indirs[0].in_off];
247 for (i = 1; i < num; ++i) {
248 ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize, 0,0);
249 if (!indirs[i].in_exists) {
250 clrbuf(ibp);
251 ibp->b_blkno = UNWRITTEN;
252 } else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
253 ibp->b_blkno = fsbtodb(fs, idaddr);
254 ibp->b_flags |= B_READ;
255 VOP_STRATEGY(ibp);
256 biowait(ibp);
257 }
258 /*
259 * This block exists, but the next one may not.
260 * If that is the case mark it UNWRITTEN to keep
261 * the accounting straight.
262 */
263 if (((daddr_t *)ibp->b_data)[indirs[i].in_off]==0)
264 ((daddr_t *)ibp->b_data)[indirs[i].in_off] =
265 UNWRITTEN;
266 idaddr = ((daddr_t *)ibp->b_data)[indirs[i].in_off];
267 if ((error = VOP_BWRITE(ibp))) {
268 return error;
269 }
270 }
271 }
272
273
274 /*
275 * Get the existing block from the cache.
276 */
277 frags = fsbtofrags(fs, bb);
278 *ap->a_bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
279
280 /*
281 * The block we are writing may be a brand new block
282 * in which case we need to do accounting.
283 *
284 * We can tell a truly new block because ufs_bmaparray will say
285 * it is UNASSIGNED. Once we allocate it we will assign it the
286 * disk address UNWRITTEN.
287 */
288 if (daddr == UNASSIGNED) {
289 if (iosize != fs->lfs_bsize)
290 clrbuf(bp);
291
292 /* Note the new address */
293 bp->b_blkno = UNWRITTEN;
294
295 switch (num) {
296 case 0:
297 ip->i_ffs_db[lbn] = UNWRITTEN;
298 break;
299 case 1:
300 ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
301 break;
302 default:
303 idp = &indirs[num - 1];
304 if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
305 &ibp))
306 panic("lfs_balloc: bread bno %d", idp->in_lbn);
307 ((ufs_daddr_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
308 VOP_BWRITE(ibp);
309 }
310 } else if (!(bp->b_flags & (B_DONE|B_DELWRI))) {
311 /*
312 * Not a brand new block, also not in the cache;
313 * read it in from disk.
314 */
315 if (iosize == fs->lfs_bsize)
316 /* Optimization: I/O is unnecessary. */
317 bp->b_blkno = daddr;
318 else {
319 /*
320 * We need to read the block to preserve the
321 * existing bytes.
322 */
323 bp->b_blkno = daddr;
324 bp->b_flags |= B_READ;
325 VOP_STRATEGY(bp);
326 return(biowait(bp));
327 }
328 }
329
330 return (0);
331 }
332
333 /* VOP_BWRITE 1 time */
334 int
335 lfs_fragextend(struct vnode *vp, int osize, int nsize, ufs_daddr_t lbn, struct buf **bpp, struct ucred *cred)
336 {
337 struct inode *ip;
338 struct lfs *fs;
339 long bb;
340 int error;
341 extern long locked_queue_bytes;
342 struct buf *ibp;
343 size_t obufsize;
344 SEGUSE *sup;
345
346 ip = VTOI(vp);
347 fs = ip->i_lfs;
348 bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
349 error = 0;
350
351 /*
352 * Get the seglock so we don't enlarge blocks or change the segment
353 * accounting information while a segment is being written.
354 */
355 top:
356 lfs_seglock(fs, SEGM_PROT);
357
358 if (!ISSPACE(fs, bb, cred)) {
359 error = ENOSPC;
360 goto out;
361 }
362 if ((error = bread(vp, lbn, osize, NOCRED, bpp))) {
363 brelse(*bpp);
364 goto out;
365 }
366 #ifdef QUOTA
367 if ((error = chkdq(ip, bb, cred, 0))) {
368 brelse(*bpp);
369 goto out;
370 }
371 #endif
372 /*
373 * Adjust accounting for lfs_avail. If there's not enough room,
374 * we will have to wait for the cleaner, which we can't do while
375 * holding a block busy or while holding the seglock. In that case,
376 * release both and start over after waiting.
377 */
378 if ((*bpp)->b_flags & B_DELWRI) {
379 if (!lfs_fits(fs, bb)) {
380 brelse(*bpp);
381 #ifdef QUOTA
382 chkdq(ip, -bb, cred, 0);
383 #endif
384 lfs_segunlock(fs);
385 lfs_availwait(fs, bb);
386 goto top;
387 }
388 fs->lfs_avail -= bb;
389 }
390
391 /*
392 * Fix the allocation for this fragment so that it looks like the
393 * source segment contained a block of the new size. This overcounts;
394 * but the overcount only lasts until the block in question
395 * is written, so the on-disk live bytes count is always correct.
396 */
397 if ((*bpp)->b_blkno > 0) {
398 LFS_SEGENTRY(sup, fs, dtosn(fs, dbtofsb(fs, (*bpp)->b_blkno)), ibp);
399 sup->su_nbytes += (nsize - osize);
400 VOP_BWRITE(ibp);
401 ip->i_ffs_blocks += bb;
402 }
403 fs->lfs_bfree -= bb;
404 ip->i_lfs_effnblks += bb;
405 ip->i_flag |= IN_CHANGE | IN_UPDATE;
406
407 obufsize = (*bpp)->b_bufsize;
408 allocbuf(*bpp, nsize);
409
410 /* Adjust locked-list accounting */
411 if (((*bpp)->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED)
412 locked_queue_bytes += (*bpp)->b_bufsize - obufsize;
413
414 bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
415
416 out:
417 lfs_segunlock(fs);
418 return (error);
419 }
420