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