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