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