lfs_balloc.c revision 1.35 1 /* $NetBSD: lfs_balloc.c,v 1.35 2003/01/24 21:55:26 fvdl 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 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: lfs_balloc.c,v 1.35 2003/01/24 21:55:26 fvdl Exp $");
75
76 #if defined(_KERNEL_OPT)
77 #include "opt_quota.h"
78 #endif
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/buf.h>
83 #include <sys/proc.h>
84 #include <sys/vnode.h>
85 #include <sys/mount.h>
86 #include <sys/resourcevar.h>
87 #include <sys/trace.h>
88
89 #include <miscfs/specfs/specdev.h>
90
91 #include <ufs/ufs/quota.h>
92 #include <ufs/ufs/inode.h>
93 #include <ufs/ufs/ufsmount.h>
94 #include <ufs/ufs/ufs_extern.h>
95
96 #include <ufs/lfs/lfs.h>
97 #include <ufs/lfs/lfs_extern.h>
98
99 int lfs_fragextend(struct vnode *, int, int, daddr_t, struct buf **, struct ucred *);
100
101 /*
102 * Allocate a block, and to inode and filesystem block accounting for it
103 * and for any indirect blocks the may need to be created in order for
104 * this block to be created.
105 *
106 * Blocks which have never been accounted for (i.e., which "do not exist")
107 * have disk address 0, which is translated by ufs_bmap to the special value
108 * UNASSIGNED == -1, as in the historical UFS.
109 *
110 * Blocks which have been accounted for but which have not yet been written
111 * to disk are given the new special disk address UNWRITTEN == -2, so that
112 * they can be differentiated from completely new blocks.
113 */
114 /* VOP_BWRITE NIADDR+2 times */
115 int
116 lfs_balloc(void *v)
117 {
118 struct vop_balloc_args /* {
119 struct vnode *a_vp;
120 off_t a_startoffset;
121 int a_size;
122 struct ucred *a_cred;
123 int a_flags;
124 struct buf *a_bpp;
125 } */ *ap = v;
126 struct vnode *vp;
127 int offset;
128 u_long iosize;
129 daddr_t daddr, idaddr;
130 struct buf *ibp, *bp;
131 struct inode *ip;
132 struct lfs *fs;
133 struct indir indirs[NIADDR+2], *idp;
134 daddr_t lbn, lastblock;
135 int bb, bcount;
136 int error, frags, i, nsize, osize, num;
137
138 vp = ap->a_vp;
139 ip = VTOI(vp);
140 fs = ip->i_lfs;
141 offset = blkoff(fs, ap->a_startoffset);
142 iosize = ap->a_size;
143 lbn = lblkno(fs, ap->a_startoffset);
144 (void)lfs_check(vp, lbn, 0);
145
146 /*
147 * Three cases: it's a block beyond the end of file, it's a block in
148 * the file that may or may not have been assigned a disk address or
149 * we're writing an entire block.
150 *
151 * Note, if the daddr is UNWRITTEN, the block already exists in
152 * the cache (it was read or written earlier). If so, make sure
153 * we don't count it as a new block or zero out its contents. If
154 * it did not, make sure we allocate any necessary indirect
155 * blocks.
156 *
157 * If we are writing a block beyond the end of the file, we need to
158 * check if the old last block was a fragment. If it was, we need
159 * to rewrite it.
160 */
161
162 *ap->a_bpp = NULL;
163
164 /* Check for block beyond end of file and fragment extension needed. */
165 lastblock = lblkno(fs, ip->i_ffs_size);
166 if (lastblock < NDADDR && lastblock < lbn) {
167 osize = blksize(fs, ip, lastblock);
168 if (osize < fs->lfs_bsize && osize > 0) {
169 if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
170 lastblock, &bp,
171 ap->a_cred)))
172 return (error);
173 ip->i_ffs_size = (lastblock + 1) * fs->lfs_bsize;
174 uvm_vnp_setsize(vp, ip->i_ffs_size);
175 ip->i_flag |= IN_CHANGE | IN_UPDATE;
176 (void) VOP_BWRITE(bp);
177 }
178 }
179
180 /*
181 * If the block we are writing is a direct block, it's the last
182 * block in the file, and offset + iosize is less than a full
183 * block, we can write one or more fragments. There are two cases:
184 * the block is brand new and we should allocate it the correct
185 * size or it already exists and contains some fragments and
186 * may need to extend it.
187 */
188 if (lbn < NDADDR && lblkno(fs, ip->i_ffs_size) <= lbn) {
189 osize = blksize(fs, ip, lbn);
190 nsize = fragroundup(fs, offset + iosize);
191 if (lblktosize(fs, lbn) >= ip->i_ffs_size) {
192 /* Brand new block or fragment */
193 frags = numfrags(fs, nsize);
194 bb = fragstofsb(fs, frags);
195 *ap->a_bpp = bp = getblk(vp, lbn, nsize, 0, 0);
196 if (ap->a_flags & B_CLRBUF)
197 clrbuf(bp);
198 ip->i_lfs_effnblks += bb;
199 ip->i_lfs->lfs_bfree -= bb;
200 ip->i_ffs_db[lbn] = bp->b_blkno = UNWRITTEN;
201 } else {
202 if (nsize <= osize) {
203 /* No need to extend */
204 if ((error = bread(vp, lbn, osize, NOCRED, &bp)))
205 return error;
206 } else {
207 /* Extend existing block */
208 if ((error =
209 lfs_fragextend(vp, osize, nsize, lbn, &bp,
210 ap->a_cred)))
211 return error;
212 }
213 *ap->a_bpp = bp;
214 }
215 return 0;
216 }
217
218 error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL );
219 if (error)
220 return (error);
221 /*
222 * Do byte accounting all at once, so we can gracefully fail *before*
223 * we start assigning blocks.
224 */
225 bb = VFSTOUFS(vp->v_mount)->um_seqinc;
226 bcount = 0;
227 if (daddr == UNASSIGNED) {
228 bcount = bb;
229 }
230 for (i = 1; i < num; ++i) {
231 if (!indirs[i].in_exists) {
232 bcount += bb;
233 }
234 }
235 if (ISSPACE(fs, bcount, ap->a_cred)) {
236 ip->i_lfs->lfs_bfree -= bcount;
237 ip->i_lfs_effnblks += bcount;
238 } else {
239 return ENOSPC;
240 }
241
242 if (daddr == UNASSIGNED) {
243 if (num > 0 && ip->i_ffs_ib[indirs[0].in_off] == 0) {
244 ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
245 }
246
247 /*
248 * Create new indirect blocks if necessary
249 */
250 if (num > 1)
251 idaddr = ip->i_ffs_ib[indirs[0].in_off];
252 for (i = 1; i < num; ++i) {
253 ibp = getblk(vp, indirs[i].in_lbn, fs->lfs_bsize, 0,0);
254 if (!indirs[i].in_exists) {
255 clrbuf(ibp);
256 ibp->b_blkno = UNWRITTEN;
257 } else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
258 ibp->b_blkno = fsbtodb(fs, idaddr);
259 ibp->b_flags |= B_READ;
260 VOP_STRATEGY(ibp);
261 biowait(ibp);
262 }
263 /*
264 * This block exists, but the next one may not.
265 * If that is the case mark it UNWRITTEN to keep
266 * the accounting straight.
267 */
268 /* XXX ondisk32 */
269 if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
270 ((int32_t *)ibp->b_data)[indirs[i].in_off] =
271 UNWRITTEN;
272 /* XXX ondisk32 */
273 idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
274 if ((error = VOP_BWRITE(ibp))) {
275 return error;
276 }
277 }
278 }
279
280
281 /*
282 * Get the existing block from the cache.
283 */
284 frags = fsbtofrags(fs, bb);
285 *ap->a_bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
286
287 /*
288 * The block we are writing may be a brand new block
289 * in which case we need to do accounting.
290 *
291 * We can tell a truly new block because ufs_bmaparray will say
292 * it is UNASSIGNED. Once we allocate it we will assign it the
293 * disk address UNWRITTEN.
294 */
295 if (daddr == UNASSIGNED) {
296 if (ap->a_flags & B_CLRBUF)
297 clrbuf(bp);
298
299 /* Note the new address */
300 bp->b_blkno = UNWRITTEN;
301
302 switch (num) {
303 case 0:
304 ip->i_ffs_db[lbn] = UNWRITTEN;
305 break;
306 case 1:
307 ip->i_ffs_ib[indirs[0].in_off] = UNWRITTEN;
308 break;
309 default:
310 idp = &indirs[num - 1];
311 if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
312 &ibp))
313 panic("lfs_balloc: bread bno %lld",
314 (long long)idp->in_lbn);
315 /* XXX ondisk32 */
316 ((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
317 VOP_BWRITE(ibp);
318 }
319 } else if (!(bp->b_flags & (B_DONE|B_DELWRI))) {
320 /*
321 * Not a brand new block, also not in the cache;
322 * read it in from disk.
323 */
324 if (iosize == fs->lfs_bsize)
325 /* Optimization: I/O is unnecessary. */
326 bp->b_blkno = daddr;
327 else {
328 /*
329 * We need to read the block to preserve the
330 * existing bytes.
331 */
332 bp->b_blkno = daddr;
333 bp->b_flags |= B_READ;
334 VOP_STRATEGY(bp);
335 return (biowait(bp));
336 }
337 }
338
339 return (0);
340 }
341
342 /* VOP_BWRITE 1 time */
343 int
344 lfs_fragextend(struct vnode *vp, int osize, int nsize, daddr_t lbn, struct buf **bpp, struct ucred *cred)
345 {
346 struct inode *ip;
347 struct lfs *fs;
348 long bb;
349 int error;
350 extern long locked_queue_bytes;
351 size_t obufsize;
352
353 ip = VTOI(vp);
354 fs = ip->i_lfs;
355 bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
356 error = 0;
357
358 /*
359 * Get the seglock so we don't enlarge blocks or change the segment
360 * accounting information while a segment is being written.
361 */
362 top:
363 #ifdef LFS_MALLOC_SEGLOCK
364 lfs_seglock(fs, SEGM_PROT);
365 #else
366 lockmgr(&fs->lfs_fraglock, LK_SHARED, 0);
367 #endif
368 if (!ISSPACE(fs, bb, cred)) {
369 error = ENOSPC;
370 goto out;
371 }
372 if ((error = bread(vp, lbn, osize, NOCRED, bpp))) {
373 brelse(*bpp);
374 goto out;
375 }
376 #ifdef QUOTA
377 if ((error = chkdq(ip, bb, cred, 0))) {
378 brelse(*bpp);
379 goto out;
380 }
381 #endif
382 /*
383 * Adjust accounting for lfs_avail. If there's not enough room,
384 * we will have to wait for the cleaner, which we can't do while
385 * holding a block busy or while holding the seglock. In that case,
386 * release both and start over after waiting.
387 */
388
389 if ((*bpp)->b_flags & B_DELWRI) {
390 if (!lfs_fits(fs, bb)) {
391 brelse(*bpp);
392 #ifdef QUOTA
393 chkdq(ip, -bb, cred, 0);
394 #endif
395 #ifdef LFS_FRAGSIZE_SEGLOCK
396 lfs_segunlock(fs);
397 #else
398 lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
399 #endif
400 lfs_availwait(fs, bb);
401 goto top;
402 }
403 fs->lfs_avail -= bb;
404 }
405
406 fs->lfs_bfree -= bb;
407 ip->i_lfs_effnblks += bb;
408 ip->i_flag |= IN_CHANGE | IN_UPDATE;
409
410 LFS_DEBUG_COUNTLOCKED("frag1");
411
412 obufsize = (*bpp)->b_bufsize;
413 allocbuf(*bpp, nsize);
414
415 /* Adjust locked-list accounting */
416 if (((*bpp)->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED)
417 locked_queue_bytes += (*bpp)->b_bufsize - obufsize;
418
419 LFS_DEBUG_COUNTLOCKED("frag2");
420
421 bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
422
423 out:
424 #ifdef LFS_FRAGSIZE_SEGLOCK
425 lfs_segunlock(fs);
426 #else
427 lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
428 #endif
429 return (error);
430 }
431