lfs_bio.c revision 1.35.2.1 1 /* $NetBSD: lfs_bio.c,v 1.35.2.1 2001/03/05 22:50:07 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/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 * Try to reserve some blocks, prior to performing a sensitive operation that
112 * requires the vnode lock to be honored. If there is not enough space, give
113 * up the vnode lock temporarily and wait for the space to become available.
114 *
115 * Called with vp locked. (Note nowever that if nb < 0, vp is ignored.)
116 */
117 int
118 lfs_reserve(fs, vp, nb)
119 struct lfs *fs;
120 struct vnode *vp;
121 int nb;
122 {
123 CLEANERINFO *cip;
124 struct buf *bp;
125 int error, slept;
126
127 slept = 0;
128 while (nb > 0 && !lfs_fits(fs, nb + fs->lfs_ravail)) {
129 VOP_UNLOCK(vp, 0);
130
131 if (!slept) {
132 #ifdef DEBUG
133 printf("lfs_reserve: waiting for %ld (bfree = %d,"
134 " est_bfree = %d)\n",
135 nb + fs->lfs_ravail, fs->lfs_bfree,
136 LFS_EST_BFREE(fs));
137 #endif
138 }
139 ++slept;
140
141 /* Wake up the cleaner */
142 LFS_CLEANERINFO(cip, fs, bp);
143 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
144 wakeup(&lfs_allclean_wakeup);
145 wakeup(&fs->lfs_nextseg);
146
147 error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_reserve",
148 0);
149 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
150 if (error)
151 return error;
152 }
153 if (slept)
154 printf("lfs_reserve: woke up\n");
155 fs->lfs_ravail += nb;
156 return 0;
157 }
158
159 /*
160 *
161 * XXX we don't let meta-data writes run out of space because they can
162 * come from the segment writer. We need to make sure that there is
163 * enough space reserved so that there's room to write meta-data
164 * blocks.
165 *
166 * Also, we don't let blocks that have come to us from the cleaner
167 * run out of space.
168 */
169 #define CANT_WAIT(BP,F) (IS_IFILE((BP)) || (BP)->b_lblkno<0 || ((F) & BW_CLEAN))
170
171 int
172 lfs_bwrite(v)
173 void *v;
174 {
175 struct vop_bwrite_args /* {
176 struct buf *a_bp;
177 } */ *ap = v;
178 struct buf *bp = ap->a_bp;
179 struct inode *ip;
180
181 ip = VTOI(bp->b_vp);
182
183 #ifdef DIAGNOSTIC
184 if (VTOI(bp->b_vp)->i_lfs->lfs_ronly == 0 && (bp->b_flags & B_ASYNC)) {
185 panic("bawrite LFS buffer");
186 }
187 #endif /* DIAGNOSTIC */
188 return lfs_bwrite_ext(bp,0);
189 }
190
191 /*
192 * Determine if there is enough room currently available to write db
193 * disk blocks. We need enough blocks for the new blocks, the current
194 * inode blocks, a summary block, plus potentially the ifile inode and
195 * the segment usage table, plus an ifile page.
196 */
197 int
198 lfs_fits(struct lfs *fs, int db)
199 {
200 int needed;
201
202 needed = db + btodb(LFS_SUMMARY_SIZE) +
203 fsbtodb(fs, howmany(fs->lfs_uinodes + 1, INOPB(fs)) +
204 fs->lfs_segtabsz + 1);
205
206 if (needed >= fs->lfs_avail) {
207 #ifdef DEBUG
208 printf("lfs_fits: no fit: db = %d, uinodes = %d, "
209 "needed = %d, avail = %d\n",
210 db, fs->lfs_uinodes, needed, fs->lfs_avail);
211 #endif
212 return 0;
213 }
214 return 1;
215 }
216
217 int
218 lfs_availwait(fs, db)
219 struct lfs *fs;
220 int db;
221 {
222 int error;
223 CLEANERINFO *cip;
224 struct buf *cbp;
225
226 while (!lfs_fits(fs, db)) {
227 /*
228 * Out of space, need cleaner to run.
229 * Update the cleaner info, then wake it up.
230 * Note the cleanerinfo block is on the ifile
231 * so it CANT_WAIT.
232 */
233 LFS_CLEANERINFO(cip, fs, cbp);
234 LFS_SYNC_CLEANERINFO(cip, fs, cbp, 0);
235
236 printf("lfs_availwait: out of available space, "
237 "waiting on cleaner\n");
238
239 wakeup(&lfs_allclean_wakeup);
240 wakeup(&fs->lfs_nextseg);
241 #ifdef DIAGNOSTIC
242 if (fs->lfs_seglock &&
243 fs->lfs_lockpid == curproc->l_proc->p_pid)
244 panic("lfs_availwait: deadlock");
245 #endif
246 error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", 0);
247 if (error)
248 return (error);
249 }
250 return 0;
251 }
252
253 int
254 lfs_bwrite_ext(bp, flags)
255 struct buf *bp;
256 int flags;
257 {
258 struct lfs *fs;
259 struct inode *ip;
260 int db, error, s;
261
262 /*
263 * Don't write *any* blocks if we're mounted read-only.
264 * In particular the cleaner can't write blocks either.
265 */
266 if(VTOI(bp->b_vp)->i_lfs->lfs_ronly) {
267 bp->b_flags &= ~(B_DELWRI | B_READ | B_ERROR);
268 LFS_UNLOCK_BUF(bp);
269 if(bp->b_flags & B_CALL)
270 bp->b_flags &= ~B_BUSY;
271 else
272 brelse(bp);
273 return EROFS;
274 }
275
276 /*
277 * Set the delayed write flag and use reassignbuf to move the buffer
278 * from the clean list to the dirty one.
279 *
280 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
281 * the buffer onto the LOCKED free list. This is necessary, otherwise
282 * getnewbuf() would try to reclaim the buffers using bawrite, which
283 * isn't going to work.
284 *
285 * XXX we don't let meta-data writes run out of space because they can
286 * come from the segment writer. We need to make sure that there is
287 * enough space reserved so that there's room to write meta-data
288 * blocks.
289 */
290 if (!(bp->b_flags & B_LOCKED)) {
291 fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
292 db = fragstodb(fs, numfrags(fs, bp->b_bcount));
293 if (!CANT_WAIT(bp, flags)) {
294 if ((error = lfs_availwait(fs, db)) != 0) {
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 bp->b_flags |= B_DELWRI;
308
309 LFS_LOCK_BUF(bp);
310 bp->b_flags &= ~(B_READ | B_ERROR);
311 s = splbio();
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 #if 1 || defined(DEBUG)
395 s = splbio();
396 lfs_countlocked(&locked_queue_count, &locked_queue_bytes);
397 splx(s);
398 wakeup(&locked_queue_count);
399 #endif /* 1 || DEBUG */
400
401 lfs_writing = 0;
402 }
403
404 int
405 lfs_check(vp, blkno, flags)
406 struct vnode *vp;
407 ufs_daddr_t blkno;
408 int flags;
409 {
410 int error;
411 struct lfs *fs;
412 struct inode *ip;
413 extern int lfs_dirvcount;
414
415 error = 0;
416 ip = VTOI(vp);
417
418 /* If out of buffers, wait on writer */
419 /* XXX KS - if it's the Ifile, we're probably the cleaner! */
420 if (ip->i_number == LFS_IFILE_INUM)
421 return 0;
422 /* If we're being called from inside a dirop, don't sleep */
423 if (ip->i_flag & IN_ADIROP)
424 return 0;
425
426 fs = ip->i_lfs;
427
428 /*
429 * If we would flush below, but dirops are active, sleep.
430 * Note that a dirop cannot ever reach this code!
431 */
432 while (fs->lfs_dirops > 0 &&
433 (locked_queue_count > LFS_MAX_BUFS ||
434 locked_queue_bytes > LFS_MAX_BYTES ||
435 lfs_dirvcount > LFS_MAXDIROP || fs->lfs_diropwait > 0))
436 {
437 ++fs->lfs_diropwait;
438 tsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0);
439 --fs->lfs_diropwait;
440 }
441
442 if (locked_queue_count > LFS_MAX_BUFS ||
443 locked_queue_bytes > LFS_MAX_BYTES ||
444 lfs_dirvcount > LFS_MAXDIROP || fs->lfs_diropwait > 0)
445 {
446 ++fs->lfs_writer;
447 lfs_flush(fs, flags);
448 if(--fs->lfs_writer==0)
449 wakeup(&fs->lfs_dirops);
450 }
451
452 while (locked_queue_count > LFS_WAIT_BUFS
453 || locked_queue_bytes > LFS_WAIT_BYTES)
454 {
455 if(lfs_dostats)
456 ++lfs_stats.wait_exceeded;
457 #ifdef DEBUG
458 printf("lfs_check: waiting: count=%d, bytes=%ld\n",
459 locked_queue_count, locked_queue_bytes);
460 #endif
461 error = tsleep(&locked_queue_count, PCATCH | PUSER,
462 "buffers", hz * LFS_BUFWAIT);
463 if (error != EWOULDBLOCK)
464 break;
465 /*
466 * lfs_flush might not flush all the buffers, if some of the
467 * inodes were locked or if most of them were Ifile blocks
468 * and we weren't asked to checkpoint. Try flushing again
469 * to keep us from blocking indefinitely.
470 */
471 if (locked_queue_count > LFS_MAX_BUFS ||
472 locked_queue_bytes > LFS_MAX_BYTES)
473 {
474 ++fs->lfs_writer;
475 lfs_flush(fs, flags | SEGM_CKP);
476 if(--fs->lfs_writer==0)
477 wakeup(&fs->lfs_dirops);
478 }
479 }
480 return (error);
481 }
482
483 /*
484 * Allocate a new buffer header.
485 */
486 #ifdef MALLOCLOG
487 # define DOMALLOC(S, T, F) _malloc((S), (T), (F), file, line)
488 struct buf *
489 lfs_newbuf_malloclog(vp, daddr, size, file, line)
490 struct vnode *vp;
491 ufs_daddr_t daddr;
492 size_t size;
493 char *file;
494 int line;
495 #else
496 # define DOMALLOC(S, T, F) malloc((S), (T), (F))
497 struct buf *
498 lfs_newbuf(vp, daddr, size)
499 struct vnode *vp;
500 ufs_daddr_t daddr;
501 size_t size;
502 #endif
503 {
504 struct buf *bp;
505 size_t nbytes;
506 int s;
507
508 nbytes = roundup(size, DEV_BSIZE);
509
510 bp = DOMALLOC(sizeof(struct buf), M_SEGMENT, M_WAITOK);
511 bzero(bp, sizeof(struct buf));
512 if (nbytes)
513 bp->b_data = DOMALLOC(nbytes, M_SEGMENT, M_WAITOK);
514 if(nbytes) {
515 bzero(bp->b_data, nbytes);
516 }
517 #ifdef DIAGNOSTIC
518 if(vp==NULL)
519 panic("vp is NULL in lfs_newbuf");
520 if(bp==NULL)
521 panic("bp is NULL after malloc in lfs_newbuf");
522 #endif
523 s = splbio();
524 bgetvp(vp, bp);
525 splx(s);
526
527 bp->b_bufsize = size;
528 bp->b_bcount = size;
529 bp->b_lblkno = daddr;
530 bp->b_blkno = daddr;
531 bp->b_error = 0;
532 bp->b_resid = 0;
533 bp->b_iodone = lfs_callback;
534 bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
535
536 return (bp);
537 }
538
539 #ifdef MALLOCLOG
540 # define DOFREE(A, T) _free((A), (T), file, line)
541 void
542 lfs_freebuf_malloclog(bp, file, line)
543 struct buf *bp;
544 char *file;
545 int line;
546 #else
547 # define DOFREE(A, T) free((A), (T))
548 void
549 lfs_freebuf(bp)
550 struct buf *bp;
551 #endif
552 {
553 int s;
554
555 s = splbio();
556 if(bp->b_vp)
557 brelvp(bp);
558 splx(s);
559 if (!(bp->b_flags & B_INVAL)) { /* B_INVAL indicates a "fake" buffer */
560 DOFREE(bp->b_data, M_SEGMENT);
561 bp->b_data = NULL;
562 }
563 DOFREE(bp, M_SEGMENT);
564 }
565
566 /*
567 * Definitions for the buffer free lists.
568 */
569 #define BQUEUES 4 /* number of free buffer queues */
570
571 #define BQ_LOCKED 0 /* super-blocks &c */
572 #define BQ_LRU 1 /* lru, useful buffers */
573 #define BQ_AGE 2 /* rubbish */
574 #define BQ_EMPTY 3 /* buffer headers with no memory */
575
576 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
577
578 /*
579 * Return a count of buffers on the "locked" queue.
580 * Don't count malloced buffers, since they don't detract from the total.
581 */
582 void
583 lfs_countlocked(count, bytes)
584 int *count;
585 long *bytes;
586 {
587 struct buf *bp;
588 int n = 0;
589 long int size = 0L;
590
591 for (bp = bufqueues[BQ_LOCKED].tqh_first; bp;
592 bp = bp->b_freelist.tqe_next) {
593 if (bp->b_flags & B_CALL) /* Malloced buffer */
594 continue;
595 n++;
596 size += bp->b_bufsize;
597 #ifdef DEBUG_LOCKED_LIST
598 if (n > nbuf)
599 panic("lfs_countlocked: this can't happen: more"
600 " buffers locked than exist");
601 #endif
602 }
603 #ifdef DEBUG
604 /* Theoretically this function never really does anything */
605 if (n != *count)
606 printf("lfs_countlocked: adjusted buf count from %d to %d\n",
607 *count, n);
608 if (size != *bytes)
609 printf("lfs_countlocked: adjusted byte count from %ld to %ld\n",
610 *bytes, size);
611 #endif
612 *count = n;
613 *bytes = size;
614 return;
615 }
616