1 /* $NetBSD: lfs_balloc.c,v 1.48 2004/01/25 18:06:49 hannken 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@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. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)lfs_balloc.c 8.4 (Berkeley) 5/8/95
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: lfs_balloc.c,v 1.48 2004/01/25 18:06:49 hannken Exp $");
71
72 #if defined(_KERNEL_OPT)
73 #include "opt_quota.h"
74 #endif
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/buf.h>
79 #include <sys/proc.h>
80 #include <sys/vnode.h>
81 #include <sys/mount.h>
82 #include <sys/resourcevar.h>
83 #include <sys/trace.h>
84
85 #include <miscfs/specfs/specdev.h>
86
87 #include <ufs/ufs/quota.h>
88 #include <ufs/ufs/inode.h>
89 #include <ufs/ufs/ufsmount.h>
90 #include <ufs/ufs/ufs_extern.h>
91
92 #include <ufs/lfs/lfs.h>
93 #include <ufs/lfs/lfs_extern.h>
94
95 #include <uvm/uvm.h>
96
97 int lfs_fragextend(struct vnode *, int, int, daddr_t, struct buf **, struct ucred *);
98
99 /*
100 * Allocate a block, and to inode and filesystem block accounting for it
101 * and for any indirect blocks the may need to be created in order for
102 * this block to be created.
103 *
104 * Blocks which have never been accounted for (i.e., which "do not exist")
105 * have disk address 0, which is translated by ufs_bmap to the special value
106 * UNASSIGNED == -1, as in the historical UFS.
107 *
108 * Blocks which have been accounted for but which have not yet been written
109 * to disk are given the new special disk address UNWRITTEN == -2, so that
110 * they can be differentiated from completely new blocks.
111 */
112 /* VOP_BWRITE NIADDR+2 times */
113 int
114 lfs_balloc(void *v)
115 {
116 struct vop_balloc_args /* {
117 struct vnode *a_vp;
118 off_t a_startoffset;
119 int a_size;
120 struct ucred *a_cred;
121 int a_flags;
122 struct buf *a_bpp;
123 } */ *ap = v;
124 struct vnode *vp;
125 int offset;
126 u_long iosize;
127 daddr_t daddr, idaddr;
128 struct buf *ibp, *bp, **bpp;
129 struct inode *ip;
130 struct lfs *fs;
131 struct indir indirs[NIADDR+2], *idp;
132 daddr_t lbn, lastblock;
133 int bb, bcount;
134 int error, frags, i, nsize, osize, num;
135
136 vp = ap->a_vp;
137 ip = VTOI(vp);
138 fs = ip->i_lfs;
139 offset = blkoff(fs, ap->a_startoffset);
140 iosize = ap->a_size;
141 KASSERT(iosize <= fs->lfs_bsize);
142 lbn = lblkno(fs, ap->a_startoffset);
143 /* (void)lfs_check(vp, lbn, 0); */
144 bpp = ap->a_bpp;
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 if (bpp)
163 *bpp = NULL;
164
165 /* Check for block beyond end of file and fragment extension needed. */
166 lastblock = lblkno(fs, ip->i_size);
167 if (lastblock < NDADDR && lastblock < lbn) {
168 osize = blksize(fs, ip, lastblock);
169 if (osize < fs->lfs_bsize && osize > 0) {
170 if ((error = lfs_fragextend(vp, osize, fs->lfs_bsize,
171 lastblock,
172 (bpp ? &bp : NULL),
173 ap->a_cred)))
174 return (error);
175 ip->i_ffs1_size = ip->i_size =
176 (lastblock + 1) * fs->lfs_bsize;
177 uvm_vnp_setsize(vp, ip->i_size);
178 ip->i_flag |= IN_CHANGE | IN_UPDATE;
179 if (bpp)
180 (void) VOP_BWRITE(bp);
181 }
182 }
183
184 /*
185 * If the block we are writing is a direct block, it's the last
186 * block in the file, and offset + iosize is less than a full
187 * block, we can write one or more fragments. There are two cases:
188 * the block is brand new and we should allocate it the correct
189 * size or it already exists and contains some fragments and
190 * may need to extend it.
191 */
192 if (lbn < NDADDR && lblkno(fs, ip->i_size) <= lbn) {
193 osize = blksize(fs, ip, lbn);
194 nsize = fragroundup(fs, offset + iosize);
195 if (lblktosize(fs, lbn) >= ip->i_size) {
196 /* Brand new block or fragment */
197 frags = numfrags(fs, nsize);
198 bb = fragstofsb(fs, frags);
199 if (bpp) {
200 *ap->a_bpp = bp = getblk(vp, lbn, nsize, 0, 0);
201 bp->b_blkno = UNWRITTEN;
202 if (ap->a_flags & B_CLRBUF)
203 clrbuf(bp);
204 }
205 ip->i_lfs_effnblks += bb;
206 ip->i_lfs->lfs_bfree -= bb;
207 ip->i_ffs1_db[lbn] = UNWRITTEN;
208 } else {
209 if (nsize <= osize) {
210 /* No need to extend */
211 if (bpp && (error = bread(vp, lbn, osize, NOCRED, &bp)))
212 return error;
213 } else {
214 /* Extend existing block */
215 if ((error =
216 lfs_fragextend(vp, osize, nsize, lbn,
217 (bpp ? &bp : NULL),
218 ap->a_cred)))
219 return error;
220 }
221 if (bpp)
222 *bpp = bp;
223 }
224 return 0;
225 }
226
227 error = ufs_bmaparray(vp, lbn, &daddr, &indirs[0], &num, NULL, NULL);
228 if (error)
229 return (error);
230 /*
231 * Do byte accounting all at once, so we can gracefully fail *before*
232 * we start assigning blocks.
233 */
234 bb = VFSTOUFS(vp->v_mount)->um_seqinc;
235 bcount = 0;
236 if (daddr == UNASSIGNED) {
237 bcount = bb;
238 }
239 for (i = 1; i < num; ++i) {
240 if (!indirs[i].in_exists) {
241 bcount += bb;
242 }
243 }
244 if (ISSPACE(fs, bcount, ap->a_cred)) {
245 ip->i_lfs->lfs_bfree -= bcount;
246 ip->i_lfs_effnblks += bcount;
247 } else {
248 return ENOSPC;
249 }
250
251 if (daddr == UNASSIGNED) {
252 if (num > 0 && ip->i_ffs1_ib[indirs[0].in_off] == 0) {
253 ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
254 }
255
256 /*
257 * Create new indirect blocks if necessary
258 */
259 if (num > 1) {
260 idaddr = ip->i_ffs1_ib[indirs[0].in_off];
261 for (i = 1; i < num; ++i) {
262 ibp = getblk(vp, indirs[i].in_lbn,
263 fs->lfs_bsize, 0,0);
264 if (!indirs[i].in_exists) {
265 clrbuf(ibp);
266 ibp->b_blkno = UNWRITTEN;
267 } else if (!(ibp->b_flags & (B_DELWRI | B_DONE))) {
268 ibp->b_blkno = fsbtodb(fs, idaddr);
269 ibp->b_flags |= B_READ;
270 VOP_STRATEGY(vp, ibp);
271 biowait(ibp);
272 }
273 /*
274 * This block exists, but the next one may not.
275 * If that is the case mark it UNWRITTEN to keep
276 * the accounting straight.
277 */
278 /* XXX ondisk32 */
279 if (((int32_t *)ibp->b_data)[indirs[i].in_off] == 0)
280 ((int32_t *)ibp->b_data)[indirs[i].in_off] =
281 UNWRITTEN;
282 /* XXX ondisk32 */
283 idaddr = ((int32_t *)ibp->b_data)[indirs[i].in_off];
284 if ((error = VOP_BWRITE(ibp)))
285 return error;
286 }
287 }
288 }
289
290
291 /*
292 * Get the existing block from the cache, if requested.
293 */
294 frags = fsbtofrags(fs, bb);
295 if (bpp)
296 *bpp = bp = getblk(vp, lbn, blksize(fs, ip, lbn), 0, 0);
297
298 /*
299 * The block we are writing may be a brand new block
300 * in which case we need to do accounting.
301 *
302 * We can tell a truly new block because ufs_bmaparray will say
303 * it is UNASSIGNED. Once we allocate it we will assign it the
304 * disk address UNWRITTEN.
305 */
306 if (daddr == UNASSIGNED) {
307 if (bpp) {
308 if (ap->a_flags & B_CLRBUF)
309 clrbuf(bp);
310
311 /* Note the new address */
312 bp->b_blkno = UNWRITTEN;
313 }
314
315 switch (num) {
316 case 0:
317 ip->i_ffs1_db[lbn] = UNWRITTEN;
318 break;
319 case 1:
320 ip->i_ffs1_ib[indirs[0].in_off] = UNWRITTEN;
321 break;
322 default:
323 idp = &indirs[num - 1];
324 if (bread(vp, idp->in_lbn, fs->lfs_bsize, NOCRED,
325 &ibp))
326 panic("lfs_balloc: bread bno %lld",
327 (long long)idp->in_lbn);
328 /* XXX ondisk32 */
329 ((int32_t *)ibp->b_data)[idp->in_off] = UNWRITTEN;
330 VOP_BWRITE(ibp);
331 }
332 } else if (bpp && !(bp->b_flags & (B_DONE|B_DELWRI))) {
333 /*
334 * Not a brand new block, also not in the cache;
335 * read it in from disk.
336 */
337 if (iosize == fs->lfs_bsize)
338 /* Optimization: I/O is unnecessary. */
339 bp->b_blkno = daddr;
340 else {
341 /*
342 * We need to read the block to preserve the
343 * existing bytes.
344 */
345 bp->b_blkno = daddr;
346 bp->b_flags |= B_READ;
347 VOP_STRATEGY(vp, bp);
348 return (biowait(bp));
349 }
350 }
351
352 return (0);
353 }
354
355 /* VOP_BWRITE 1 time */
356 int
357 lfs_fragextend(struct vnode *vp, int osize, int nsize, daddr_t lbn, struct buf **bpp, struct ucred *cred)
358 {
359 struct inode *ip;
360 struct lfs *fs;
361 long bb;
362 int error;
363 extern long locked_queue_bytes;
364 size_t obufsize;
365
366 ip = VTOI(vp);
367 fs = ip->i_lfs;
368 bb = (long)fragstofsb(fs, numfrags(fs, nsize - osize));
369 error = 0;
370
371 /*
372 * Get the seglock so we don't enlarge blocks while a segment
373 * is being written. If we're called with bpp==NULL, though,
374 * we are only pretending to change a buffer, so we don't have to
375 * lock.
376 */
377 top:
378 if (bpp) {
379 lockmgr(&fs->lfs_fraglock, LK_SHARED, 0);
380 LFS_DEBUG_COUNTLOCKED("frag");
381 }
382
383 if (!ISSPACE(fs, bb, cred)) {
384 error = ENOSPC;
385 goto out;
386 }
387
388 /*
389 * If we are not asked to actually return the block, all we need
390 * to do is allocate space for it. UBC will handle dirtying the
391 * appropriate things and making sure it all goes to disk.
392 * Don't bother to read in that case.
393 */
394 if (bpp && (error = bread(vp, lbn, osize, NOCRED, bpp))) {
395 brelse(*bpp);
396 goto out;
397 }
398 #ifdef QUOTA
399 if ((error = chkdq(ip, bb, cred, 0))) {
400 if (bpp)
401 brelse(*bpp);
402 goto out;
403 }
404 #endif
405 /*
406 * Adjust accounting for lfs_avail. If there's not enough room,
407 * we will have to wait for the cleaner, which we can't do while
408 * holding a block busy or while holding the seglock. In that case,
409 * release both and start over after waiting.
410 */
411
412 if (bpp && ((*bpp)->b_flags & B_DELWRI)) {
413 if (!lfs_fits(fs, bb)) {
414 if (bpp)
415 brelse(*bpp);
416 #ifdef QUOTA
417 chkdq(ip, -bb, cred, 0);
418 #endif
419 lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
420 lfs_availwait(fs, bb);
421 goto top;
422 }
423 fs->lfs_avail -= bb;
424 }
425
426 fs->lfs_bfree -= bb;
427 ip->i_lfs_effnblks += bb;
428 ip->i_flag |= IN_CHANGE | IN_UPDATE;
429
430 if (bpp) {
431 obufsize = (*bpp)->b_bufsize;
432 allocbuf(*bpp, nsize, 1);
433
434 /* Adjust locked-list accounting */
435 if (((*bpp)->b_flags & (B_LOCKED | B_CALL)) == B_LOCKED)
436 locked_queue_bytes += (*bpp)->b_bufsize - obufsize;
437
438 bzero((char *)((*bpp)->b_data) + osize, (u_int)(nsize - osize));
439 }
440
441 out:
442 if (bpp) {
443 lockmgr(&fs->lfs_fraglock, LK_RELEASE, 0);
444 }
445 return (error);
446 }
Cache object: 6c1a322625dc335a13509de92e416bdd
|