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