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