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