lfs_bio.c revision 1.35.2.5 1 /* $NetBSD: lfs_bio.c,v 1.35.2.5 2002/02/28 04:15:28 nathanw 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) 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_bio.c 8.10 (Berkeley) 6/10/95
71 */
72
73 #include <sys/cdefs.h>
74 __KERNEL_RCSID(0, "$NetBSD: lfs_bio.c,v 1.35.2.5 2002/02/28 04:15:28 nathanw Exp $");
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/proc.h>
79 #include <sys/buf.h>
80 #include <sys/vnode.h>
81 #include <sys/resourcevar.h>
82 #include <sys/mount.h>
83 #include <sys/kernel.h>
84
85 #include <ufs/ufs/inode.h>
86 #include <ufs/ufs/ufsmount.h>
87 #include <ufs/ufs/ufs_extern.h>
88
89 #include <sys/malloc.h>
90 #include <ufs/lfs/lfs.h>
91 #include <ufs/lfs/lfs_extern.h>
92
93 /* Macros to clear/set/test flags. */
94 # define SET(t, f) (t) |= (f)
95 # define CLR(t, f) (t) &= ~(f)
96 # define ISSET(t, f) ((t) & (f))
97
98 /*
99 * LFS block write function.
100 *
101 * XXX
102 * No write cost accounting is done.
103 * This is almost certainly wrong for synchronous operations and NFS.
104 */
105 int lfs_allclean_wakeup; /* Cleaner wakeup address. */
106 int locked_queue_count = 0; /* XXX Count of locked-down buffers. */
107 long locked_queue_bytes = 0L; /* XXX Total size of locked buffers. */
108 int lfs_writing = 0; /* Set if already kicked off a writer
109 because of buffer space */
110 extern int lfs_dostats;
111
112 /*
113 * Try to reserve some blocks, prior to performing a sensitive operation that
114 * requires the vnode lock to be honored. If there is not enough space, give
115 * up the vnode lock temporarily and wait for the space to become available.
116 *
117 * Called with vp locked. (Note nowever that if nb < 0, vp is ignored.)
118 */
119 int
120 lfs_reserve(struct lfs *fs, struct vnode *vp, int nb)
121 {
122 CLEANERINFO *cip;
123 struct buf *bp;
124 int error, slept;
125
126 slept = 0;
127 while (nb > 0 && !lfs_fits(fs, nb + fs->lfs_ravail)) {
128 VOP_UNLOCK(vp, 0);
129
130 if (!slept) {
131 #ifdef DEBUG
132 printf("lfs_reserve: waiting for %ld (bfree = %d,"
133 " est_bfree = %d)\n",
134 nb + fs->lfs_ravail, fs->lfs_bfree,
135 LFS_EST_BFREE(fs));
136 #endif
137 }
138 ++slept;
139
140 /* Wake up the cleaner */
141 LFS_CLEANERINFO(cip, fs, bp);
142 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
143 wakeup(&lfs_allclean_wakeup);
144 wakeup(&fs->lfs_nextseg);
145
146 error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_reserve",
147 0);
148 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
149 if (error)
150 return error;
151 }
152 if (slept)
153 printf("lfs_reserve: woke up\n");
154 fs->lfs_ravail += nb;
155 return 0;
156 }
157
158 /*
159 *
160 * XXX we don't let meta-data writes run out of space because they can
161 * come from the segment writer. We need to make sure that there is
162 * enough space reserved so that there's room to write meta-data
163 * blocks.
164 *
165 * Also, we don't let blocks that have come to us from the cleaner
166 * run out of space.
167 */
168 #define CANT_WAIT(BP,F) (IS_IFILE((BP)) || (BP)->b_lblkno < 0 || ((F) & BW_CLEAN))
169
170 int
171 lfs_bwrite(void *v)
172 {
173 struct vop_bwrite_args /* {
174 struct buf *a_bp;
175 } */ *ap = v;
176 struct buf *bp = ap->a_bp;
177
178 #ifdef DIAGNOSTIC
179 if (VTOI(bp->b_vp)->i_lfs->lfs_ronly == 0 && (bp->b_flags & B_ASYNC)) {
180 panic("bawrite LFS buffer");
181 }
182 #endif /* DIAGNOSTIC */
183 return lfs_bwrite_ext(bp,0);
184 }
185
186 /*
187 * Determine if there is enough room currently available to write db
188 * disk blocks. We need enough blocks for the new blocks, the current
189 * inode blocks, a summary block, plus potentially the ifile inode and
190 * the segment usage table, plus an ifile page.
191 */
192 int
193 lfs_fits(struct lfs *fs, int fsb)
194 {
195 int needed;
196
197 needed = fsb + btofsb(fs, fs->lfs_sumsize) +
198 fsbtodb(fs, howmany(fs->lfs_uinodes + 1, INOPB(fs)) +
199 fs->lfs_segtabsz + btofsb(fs, fs->lfs_sumsize));
200
201 if (needed >= fs->lfs_avail) {
202 #ifdef DEBUG
203 printf("lfs_fits: no fit: fsb = %d, uinodes = %d, "
204 "needed = %d, avail = %d\n",
205 fsb, fs->lfs_uinodes, needed, fs->lfs_avail);
206 #endif
207 return 0;
208 }
209 return 1;
210 }
211
212 int
213 lfs_availwait(struct lfs *fs, int db)
214 {
215 int error;
216 CLEANERINFO *cip;
217 struct buf *cbp;
218
219 while (!lfs_fits(fs, db)) {
220 /*
221 * Out of space, need cleaner to run.
222 * Update the cleaner info, then wake it up.
223 * Note the cleanerinfo block is on the ifile
224 * so it CANT_WAIT.
225 */
226 LFS_CLEANERINFO(cip, fs, cbp);
227 LFS_SYNC_CLEANERINFO(cip, fs, cbp, 0);
228
229 printf("lfs_availwait: out of available space, "
230 "waiting on cleaner\n");
231
232 wakeup(&lfs_allclean_wakeup);
233 wakeup(&fs->lfs_nextseg);
234 #ifdef DIAGNOSTIC
235 if (fs->lfs_seglock &&
236 fs->lfs_lockpid == curproc->l_proc->p_pid)
237 panic("lfs_availwait: deadlock");
238 #endif
239 error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", 0);
240 if (error)
241 return (error);
242 }
243 return 0;
244 }
245
246 int
247 lfs_bwrite_ext(struct buf *bp, int flags)
248 {
249 struct lfs *fs;
250 struct inode *ip;
251 int fsb, error, s;
252
253 /*
254 * Don't write *any* blocks if we're mounted read-only.
255 * In particular the cleaner can't write blocks either.
256 */
257 if (VTOI(bp->b_vp)->i_lfs->lfs_ronly) {
258 bp->b_flags &= ~(B_DELWRI | B_READ | B_ERROR);
259 LFS_UNLOCK_BUF(bp);
260 if (bp->b_flags & B_CALL)
261 bp->b_flags &= ~B_BUSY;
262 else
263 brelse(bp);
264 return EROFS;
265 }
266
267 /*
268 * Set the delayed write flag and use reassignbuf to move the buffer
269 * from the clean list to the dirty one.
270 *
271 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
272 * the buffer onto the LOCKED free list. This is necessary, otherwise
273 * getnewbuf() would try to reclaim the buffers using bawrite, which
274 * isn't going to work.
275 *
276 * XXX we don't let meta-data writes run out of space because they can
277 * come from the segment writer. We need to make sure that there is
278 * enough space reserved so that there's room to write meta-data
279 * blocks.
280 */
281 if (!(bp->b_flags & B_LOCKED)) {
282 fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
283 fsb = fragstofsb(fs, numfrags(fs, bp->b_bcount));
284 if (!CANT_WAIT(bp, flags)) {
285 if ((error = lfs_availwait(fs, fsb)) != 0) {
286 brelse(bp);
287 return error;
288 }
289 }
290
291 ip = VTOI(bp->b_vp);
292 if (bp->b_flags & B_CALL) {
293 LFS_SET_UINO(ip, IN_CLEANING);
294 } else {
295 LFS_SET_UINO(ip, IN_MODIFIED);
296 if (bp->b_lblkno >= 0)
297 LFS_SET_UINO(ip, IN_UPDATE);
298 }
299 fs->lfs_avail -= fsb;
300 bp->b_flags |= B_DELWRI;
301
302 LFS_LOCK_BUF(bp);
303 bp->b_flags &= ~(B_READ | B_ERROR);
304 s = splbio();
305 reassignbuf(bp, bp->b_vp);
306 splx(s);
307
308 }
309
310 if (bp->b_flags & B_CALL)
311 bp->b_flags &= ~B_BUSY;
312 else
313 brelse(bp);
314
315 return (0);
316 }
317
318 void
319 lfs_flush_fs(struct lfs *fs, int flags)
320 {
321 if (fs->lfs_ronly == 0 && fs->lfs_dirops == 0)
322 {
323 /* disallow dirops during flush */
324 fs->lfs_writer++;
325
326 /*
327 * We set the queue to 0 here because we
328 * are about to write all the dirty
329 * buffers we have. If more come in
330 * while we're writing the segment, they
331 * may not get written, so we want the
332 * count to reflect these new writes
333 * after the segwrite completes.
334 */
335 if (lfs_dostats)
336 ++lfs_stats.flush_invoked;
337 lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
338
339 /* XXX KS - allow dirops again */
340 if (--fs->lfs_writer == 0)
341 wakeup(&fs->lfs_dirops);
342 }
343 }
344
345 /*
346 * XXX
347 * This routine flushes buffers out of the B_LOCKED queue when LFS has too
348 * many locked down. Eventually the pageout daemon will simply call LFS
349 * when pages need to be reclaimed. Note, we have one static count of locked
350 * buffers, so we can't have more than a single file system. To make this
351 * work for multiple file systems, put the count into the mount structure.
352 */
353 void
354 lfs_flush(struct lfs *fs, int flags)
355 {
356 int s;
357 struct mount *mp, *nmp;
358
359 if (lfs_dostats)
360 ++lfs_stats.write_exceeded;
361 if (lfs_writing && flags == 0) {/* XXX flags */
362 #ifdef DEBUG_LFS
363 printf("lfs_flush: not flushing because another flush is active\n");
364 #endif
365 return;
366 }
367 lfs_writing = 1;
368
369 simple_lock(&mountlist_slock);
370 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
371 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
372 nmp = mp->mnt_list.cqe_next;
373 continue;
374 }
375 if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS, MFSNAMELEN) == 0)
376 lfs_flush_fs(((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs, flags);
377 simple_lock(&mountlist_slock);
378 nmp = mp->mnt_list.cqe_next;
379 vfs_unbusy(mp);
380 }
381 simple_unlock(&mountlist_slock);
382
383 #if 1 || defined(DEBUG)
384 s = splbio();
385 lfs_countlocked(&locked_queue_count, &locked_queue_bytes);
386 splx(s);
387 wakeup(&locked_queue_count);
388 #endif /* 1 || DEBUG */
389
390 lfs_writing = 0;
391 }
392
393 #define INOCOUNT(fs) howmany((fs)->lfs_uinodes, INOPB(fs))
394 #define INOBYTES(fs) ((fs)->lfs_uinodes * DINODE_SIZE)
395
396 int
397 lfs_check(struct vnode *vp, ufs_daddr_t blkno, int flags)
398 {
399 int error;
400 struct lfs *fs;
401 struct inode *ip;
402 extern int lfs_dirvcount;
403
404 error = 0;
405 ip = VTOI(vp);
406
407 /* If out of buffers, wait on writer */
408 /* XXX KS - if it's the Ifile, we're probably the cleaner! */
409 if (ip->i_number == LFS_IFILE_INUM)
410 return 0;
411 /* If we're being called from inside a dirop, don't sleep */
412 if (ip->i_flag & IN_ADIROP)
413 return 0;
414
415 fs = ip->i_lfs;
416
417 /*
418 * If we would flush below, but dirops are active, sleep.
419 * Note that a dirop cannot ever reach this code!
420 */
421 while (fs->lfs_dirops > 0 &&
422 (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
423 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
424 lfs_dirvcount > LFS_MAXDIROP || fs->lfs_diropwait > 0))
425 {
426 ++fs->lfs_diropwait;
427 tsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0);
428 --fs->lfs_diropwait;
429 }
430
431 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
432 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
433 lfs_dirvcount > LFS_MAXDIROP || fs->lfs_diropwait > 0)
434 {
435 ++fs->lfs_writer;
436 lfs_flush(fs, flags);
437 if (--fs->lfs_writer == 0)
438 wakeup(&fs->lfs_dirops);
439 }
440
441 while (locked_queue_count + INOCOUNT(fs) > LFS_WAIT_BUFS
442 || locked_queue_bytes + INOBYTES(fs) > LFS_WAIT_BYTES)
443 {
444 if (lfs_dostats)
445 ++lfs_stats.wait_exceeded;
446 #ifdef DEBUG_LFS
447 printf("lfs_check: waiting: count=%d, bytes=%ld\n",
448 locked_queue_count, locked_queue_bytes);
449 #endif
450 error = tsleep(&locked_queue_count, PCATCH | PUSER,
451 "buffers", hz * LFS_BUFWAIT);
452 if (error != EWOULDBLOCK)
453 break;
454 /*
455 * lfs_flush might not flush all the buffers, if some of the
456 * inodes were locked or if most of them were Ifile blocks
457 * and we weren't asked to checkpoint. Try flushing again
458 * to keep us from blocking indefinitely.
459 */
460 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
461 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
462 {
463 ++fs->lfs_writer;
464 lfs_flush(fs, flags | SEGM_CKP);
465 if (--fs->lfs_writer == 0)
466 wakeup(&fs->lfs_dirops);
467 }
468 }
469 return (error);
470 }
471
472 /*
473 * Allocate a new buffer header.
474 */
475 #ifdef MALLOCLOG
476 # define DOMALLOC(S, T, F) _malloc((S), (T), (F), file, line)
477 struct buf *
478 lfs_newbuf_malloclog(struct lfs *fs, struct vnode *vp, ufs_daddr_t daddr, size_t size, char *file, int line)
479 #else
480 # define DOMALLOC(S, T, F) malloc((S), (T), (F))
481 struct buf *
482 lfs_newbuf(struct lfs *fs, struct vnode *vp, ufs_daddr_t daddr, size_t size)
483 #endif
484 {
485 struct buf *bp;
486 size_t nbytes;
487 int s;
488
489 nbytes = roundup(size, fsbtob(fs, 1));
490
491 bp = DOMALLOC(sizeof(struct buf), M_SEGMENT, M_WAITOK);
492 bzero(bp, sizeof(struct buf));
493 if (nbytes)
494 bp->b_data = DOMALLOC(nbytes, M_SEGMENT, M_WAITOK);
495 if (nbytes) {
496 bzero(bp->b_data, nbytes);
497 }
498 #ifdef DIAGNOSTIC
499 if (vp == NULL)
500 panic("vp is NULL in lfs_newbuf");
501 if (bp == NULL)
502 panic("bp is NULL after malloc in lfs_newbuf");
503 #endif
504 s = splbio();
505 bgetvp(vp, bp);
506 splx(s);
507
508 bp->b_bufsize = size;
509 bp->b_bcount = size;
510 bp->b_lblkno = daddr;
511 bp->b_blkno = daddr;
512 bp->b_error = 0;
513 bp->b_resid = 0;
514 bp->b_iodone = lfs_callback;
515 bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
516
517 return (bp);
518 }
519
520 #ifdef MALLOCLOG
521 # define DOFREE(A, T) _free((A), (T), file, line)
522 void
523 lfs_freebuf_malloclog(struct buf *bp, char *file, int line)
524 #else
525 # define DOFREE(A, T) free((A), (T))
526 void
527 lfs_freebuf(struct buf *bp)
528 #endif
529 {
530 int s;
531
532 s = splbio();
533 if (bp->b_vp)
534 brelvp(bp);
535 splx(s);
536 if (!(bp->b_flags & B_INVAL)) { /* B_INVAL indicates a "fake" buffer */
537 DOFREE(bp->b_data, M_SEGMENT);
538 bp->b_data = NULL;
539 }
540 DOFREE(bp, M_SEGMENT);
541 }
542
543 /*
544 * Definitions for the buffer free lists.
545 */
546 #define BQUEUES 4 /* number of free buffer queues */
547
548 #define BQ_LOCKED 0 /* super-blocks &c */
549 #define BQ_LRU 1 /* lru, useful buffers */
550 #define BQ_AGE 2 /* rubbish */
551 #define BQ_EMPTY 3 /* buffer headers with no memory */
552
553 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
554
555 /*
556 * Return a count of buffers on the "locked" queue.
557 * Don't count malloced buffers, since they don't detract from the total.
558 */
559 void
560 lfs_countlocked(int *count, long *bytes)
561 {
562 struct buf *bp;
563 int n = 0;
564 long int size = 0L;
565
566 for (bp = bufqueues[BQ_LOCKED].tqh_first; bp;
567 bp = bp->b_freelist.tqe_next) {
568 if (bp->b_flags & B_CALL) /* Malloced buffer */
569 continue;
570 n++;
571 size += bp->b_bufsize;
572 #ifdef DEBUG_LOCKED_LIST
573 if (n > nbuf)
574 panic("lfs_countlocked: this can't happen: more"
575 " buffers locked than exist");
576 #endif
577 }
578 #ifdef DEBUG
579 /* Theoretically this function never really does anything */
580 if (n != *count)
581 printf("lfs_countlocked: adjusted buf count from %d to %d\n",
582 *count, n);
583 if (size != *bytes)
584 printf("lfs_countlocked: adjusted byte count from %ld to %ld\n",
585 *bytes, size);
586 #endif
587 *count = n;
588 *bytes = size;
589 return;
590 }
591