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