lfs_bio.c revision 1.32 1 /* $NetBSD: lfs_bio.c,v 1.32 2000/11/17 19:14:41 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 * 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 && fs->lfs_lockpid == curproc->p_pid)
243 panic("lfs_availwait: deadlock");
244 #endif
245 error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", 0);
246 if (error)
247 return (error);
248 }
249 return 0;
250 }
251
252 int
253 lfs_bwrite_ext(bp, flags)
254 struct buf *bp;
255 int flags;
256 {
257 struct lfs *fs;
258 struct inode *ip;
259 int db, error, s;
260
261 /*
262 * Don't write *any* blocks if we're mounted read-only.
263 * In particular the cleaner can't write blocks either.
264 */
265 if(VTOI(bp->b_vp)->i_lfs->lfs_ronly) {
266 bp->b_flags &= ~(B_DELWRI | B_READ | B_ERROR);
267 LFS_UNLOCK_BUF(bp);
268 if(bp->b_flags & B_CALL)
269 bp->b_flags &= ~B_BUSY;
270 else
271 brelse(bp);
272 return EROFS;
273 }
274
275 /*
276 * Set the delayed write flag and use reassignbuf to move the buffer
277 * from the clean list to the dirty one.
278 *
279 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
280 * the buffer onto the LOCKED free list. This is necessary, otherwise
281 * getnewbuf() would try to reclaim the buffers using bawrite, which
282 * isn't going to work.
283 *
284 * XXX we don't let meta-data writes run out of space because they can
285 * come from the segment writer. We need to make sure that there is
286 * enough space reserved so that there's room to write meta-data
287 * blocks.
288 */
289 if (!(bp->b_flags & B_LOCKED)) {
290 fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
291 db = fragstodb(fs, numfrags(fs, bp->b_bcount));
292 if (!CANT_WAIT(bp, flags)) {
293 if ((error = lfs_availwait(fs, db)) != 0) {
294 brelse(bp);
295 return error;
296 }
297 }
298
299 ip = VTOI(bp->b_vp);
300 if (bp->b_flags & B_CALL) {
301 LFS_SET_UINO(ip, IN_CLEANING);
302 } else {
303 LFS_SET_UINO(ip, IN_CHANGE | IN_MODIFIED | IN_UPDATE);
304 }
305 fs->lfs_avail -= db;
306 bp->b_flags |= B_DELWRI;
307
308 LFS_LOCK_BUF(bp);
309 bp->b_flags &= ~(B_READ | B_ERROR);
310 s = splbio();
311 reassignbuf(bp, bp->b_vp);
312 splx(s);
313
314 }
315
316 if(bp->b_flags & B_CALL)
317 bp->b_flags &= ~B_BUSY;
318 else
319 brelse(bp);
320
321 return (0);
322 }
323
324 void
325 lfs_flush_fs(fs, flags)
326 struct lfs *fs;
327 int flags;
328 {
329 if(fs->lfs_ronly == 0 && fs->lfs_dirops == 0)
330 {
331 /* disallow dirops during flush */
332 fs->lfs_writer++;
333
334 /*
335 * We set the queue to 0 here because we
336 * are about to write all the dirty
337 * buffers we have. If more come in
338 * while we're writing the segment, they
339 * may not get written, so we want the
340 * count to reflect these new writes
341 * after the segwrite completes.
342 */
343 if(lfs_dostats)
344 ++lfs_stats.flush_invoked;
345 lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
346
347 /* XXX KS - allow dirops again */
348 if(--fs->lfs_writer==0)
349 wakeup(&fs->lfs_dirops);
350 }
351 }
352
353 /*
354 * XXX
355 * This routine flushes buffers out of the B_LOCKED queue when LFS has too
356 * many locked down. Eventually the pageout daemon will simply call LFS
357 * when pages need to be reclaimed. Note, we have one static count of locked
358 * buffers, so we can't have more than a single file system. To make this
359 * work for multiple file systems, put the count into the mount structure.
360 */
361 void
362 lfs_flush(fs, flags)
363 struct lfs *fs;
364 int flags;
365 {
366 int s;
367 struct mount *mp, *nmp;
368
369 if(lfs_dostats)
370 ++lfs_stats.write_exceeded;
371 if (lfs_writing && flags==0) {/* XXX flags */
372 #ifdef DEBUG_LFS
373 printf("lfs_flush: not flushing because another flush is active\n");
374 #endif
375 return;
376 }
377 lfs_writing = 1;
378
379 simple_lock(&mountlist_slock);
380 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
381 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
382 nmp = mp->mnt_list.cqe_next;
383 continue;
384 }
385 if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS, MFSNAMELEN)==0)
386 lfs_flush_fs(((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs, flags);
387 simple_lock(&mountlist_slock);
388 nmp = mp->mnt_list.cqe_next;
389 vfs_unbusy(mp);
390 }
391 simple_unlock(&mountlist_slock);
392
393 #if 1 || defined(DEBUG)
394 s = splbio();
395 lfs_countlocked(&locked_queue_count, &locked_queue_bytes);
396 splx(s);
397 wakeup(&locked_queue_count);
398 #endif /* 1 || DEBUG */
399
400 lfs_writing = 0;
401 }
402
403 int
404 lfs_check(vp, blkno, flags)
405 struct vnode *vp;
406 ufs_daddr_t blkno;
407 int flags;
408 {
409 int error;
410 struct lfs *fs;
411 struct inode *ip;
412 extern int lfs_dirvcount;
413
414 error = 0;
415 ip = VTOI(vp);
416
417 /* If out of buffers, wait on writer */
418 /* XXX KS - if it's the Ifile, we're probably the cleaner! */
419 if (ip->i_number == LFS_IFILE_INUM)
420 return 0;
421 /* If we're being called from inside a dirop, don't sleep */
422 if (ip->i_flag & IN_ADIROP)
423 return 0;
424
425 fs = ip->i_lfs;
426
427 /*
428 * If we would flush below, but dirops are active, sleep.
429 * Note that a dirop cannot ever reach this code!
430 */
431 while (fs->lfs_dirops > 0 &&
432 (locked_queue_count > LFS_MAX_BUFS ||
433 locked_queue_bytes > LFS_MAX_BYTES ||
434 lfs_dirvcount > LFS_MAXDIROP || fs->lfs_diropwait > 0))
435 {
436 ++fs->lfs_diropwait;
437 tsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0);
438 --fs->lfs_diropwait;
439 }
440
441 if (locked_queue_count > LFS_MAX_BUFS ||
442 locked_queue_bytes > LFS_MAX_BYTES ||
443 lfs_dirvcount > LFS_MAXDIROP || fs->lfs_diropwait > 0)
444 {
445 ++fs->lfs_writer;
446 lfs_flush(fs, flags);
447 if(--fs->lfs_writer==0)
448 wakeup(&fs->lfs_dirops);
449 }
450
451 while (locked_queue_count > LFS_WAIT_BUFS
452 || locked_queue_bytes > LFS_WAIT_BYTES)
453 {
454 if(lfs_dostats)
455 ++lfs_stats.wait_exceeded;
456 #ifdef DEBUG_LFS
457 printf("lfs_check: waiting: count=%d, bytes=%ld\n",
458 locked_queue_count, locked_queue_bytes);
459 #endif
460 error = tsleep(&locked_queue_count, PCATCH | PUSER,
461 "buffers", hz * LFS_BUFWAIT);
462 /*
463 * lfs_flush might not flush all the buffers, if some of the
464 * inodes were locked. Try flushing again to keep us from
465 * blocking indefinitely.
466 */
467 if (locked_queue_count > LFS_MAX_BUFS ||
468 locked_queue_bytes > LFS_MAX_BYTES)
469 {
470 ++fs->lfs_writer;
471 lfs_flush(fs, flags);
472 if(--fs->lfs_writer==0)
473 wakeup(&fs->lfs_dirops);
474 }
475 }
476 return (error);
477 }
478
479 /*
480 * Allocate a new buffer header.
481 */
482 struct buf *
483 lfs_newbuf(vp, daddr, size)
484 struct vnode *vp;
485 ufs_daddr_t daddr;
486 size_t size;
487 {
488 struct buf *bp;
489 size_t nbytes;
490 int s;
491
492 nbytes = roundup(size, DEV_BSIZE);
493
494 bp = malloc(sizeof(struct buf), M_SEGMENT, M_WAITOK);
495 bzero(bp, sizeof(struct buf));
496 if (nbytes)
497 bp->b_data = malloc(nbytes, M_SEGMENT, M_WAITOK);
498 if(nbytes) {
499 bzero(bp->b_data, nbytes);
500 }
501 #ifdef DIAGNOSTIC
502 if(vp==NULL)
503 panic("vp is NULL in lfs_newbuf");
504 if(bp==NULL)
505 panic("bp is NULL after malloc in lfs_newbuf");
506 #endif
507 s = splbio();
508 bgetvp(vp, bp);
509 splx(s);
510
511 bp->b_bufsize = size;
512 bp->b_bcount = size;
513 bp->b_lblkno = daddr;
514 bp->b_blkno = daddr;
515 bp->b_error = 0;
516 bp->b_resid = 0;
517 bp->b_iodone = lfs_callback;
518 bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
519
520 return (bp);
521 }
522
523 void
524 lfs_freebuf(bp)
525 struct buf *bp;
526 {
527 int s;
528
529 s = splbio();
530 if(bp->b_vp)
531 brelvp(bp);
532 splx(s);
533 if (!(bp->b_flags & B_INVAL)) { /* B_INVAL indicates a "fake" buffer */
534 free(bp->b_data, M_SEGMENT);
535 bp->b_data = NULL;
536 }
537 free(bp, M_SEGMENT);
538 }
539
540 /*
541 * Definitions for the buffer free lists.
542 */
543 #define BQUEUES 4 /* number of free buffer queues */
544
545 #define BQ_LOCKED 0 /* super-blocks &c */
546 #define BQ_LRU 1 /* lru, useful buffers */
547 #define BQ_AGE 2 /* rubbish */
548 #define BQ_EMPTY 3 /* buffer headers with no memory */
549
550 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
551
552 /*
553 * Return a count of buffers on the "locked" queue.
554 * Don't count malloced buffers, since they don't detract from the total.
555 */
556 void
557 lfs_countlocked(count, bytes)
558 int *count;
559 long *bytes;
560 {
561 struct buf *bp;
562 int n = 0;
563 long int size = 0L;
564
565 for (bp = bufqueues[BQ_LOCKED].tqh_first; bp;
566 bp = bp->b_freelist.tqe_next) {
567 if (bp->b_flags & B_CALL) /* Malloced buffer */
568 continue;
569 n++;
570 size += bp->b_bufsize;
571 #ifdef DEBUG_LOCKED_LIST
572 if (n > nbuf)
573 panic("lfs_countlocked: this can't happen: more"
574 " buffers locked than exist");
575 #endif
576 }
577 #ifdef DEBUG
578 /* Theoretically this function never really does anything */
579 if (n != *count)
580 printf("lfs_countlocked: adjusted buf count from %d to %d\n",
581 *count, n);
582 if (size != *bytes)
583 printf("lfs_countlocked: adjusted byte count from %ld to %ld\n",
584 *bytes, size);
585 #endif
586 *count = n;
587 *bytes = size;
588 return;
589 }
590