lfs_bio.c revision 1.126 1 /* $NetBSD: lfs_bio.c,v 1.126 2013/07/28 01:05:52 dholland Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003, 2008 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 /*
32 * Copyright (c) 1991, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)lfs_bio.c 8.10 (Berkeley) 6/10/95
60 */
61
62 #include <sys/cdefs.h>
63 __KERNEL_RCSID(0, "$NetBSD: lfs_bio.c,v 1.126 2013/07/28 01:05:52 dholland Exp $");
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/proc.h>
68 #include <sys/buf.h>
69 #include <sys/vnode.h>
70 #include <sys/resourcevar.h>
71 #include <sys/mount.h>
72 #include <sys/kernel.h>
73 #include <sys/kauth.h>
74
75 #include <ufs/lfs/ulfs_inode.h>
76 #include <ufs/lfs/ulfsmount.h>
77 #include <ufs/lfs/ulfs_extern.h>
78
79 #include <ufs/lfs/lfs.h>
80 #include <ufs/lfs/lfs_extern.h>
81 #include <ufs/lfs/lfs_kernel.h>
82
83 #include <uvm/uvm.h>
84
85 /*
86 * LFS block write function.
87 *
88 * XXX
89 * No write cost accounting is done.
90 * This is almost certainly wrong for synchronous operations and NFS.
91 *
92 * protected by lfs_lock.
93 */
94 int locked_queue_count = 0; /* Count of locked-down buffers. */
95 long locked_queue_bytes = 0L; /* Total size of locked buffers. */
96 int lfs_subsys_pages = 0L; /* Total number LFS-written pages */
97 int lfs_fs_pagetrip = 0; /* # of pages to trip per-fs write */
98 int lfs_writing = 0; /* Set if already kicked off a writer
99 because of buffer space */
100 int locked_queue_waiters = 0; /* Number of processes waiting on lq */
101
102 /* Lock and condition variables for above. */
103 kcondvar_t locked_queue_cv;
104 kcondvar_t lfs_writing_cv;
105 kmutex_t lfs_lock;
106
107 extern int lfs_dostats;
108
109 /*
110 * reserved number/bytes of locked buffers
111 */
112 int locked_queue_rcount = 0;
113 long locked_queue_rbytes = 0L;
114
115 static int lfs_fits_buf(struct lfs *, int, int);
116 static int lfs_reservebuf(struct lfs *, struct vnode *vp, struct vnode *vp2,
117 int, int);
118 static int lfs_reserveavail(struct lfs *, struct vnode *vp, struct vnode *vp2,
119 int);
120
121 static int
122 lfs_fits_buf(struct lfs *fs, int n, int bytes)
123 {
124 int count_fit, bytes_fit;
125
126 ASSERT_NO_SEGLOCK(fs);
127 KASSERT(mutex_owned(&lfs_lock));
128
129 count_fit =
130 (locked_queue_count + locked_queue_rcount + n <= LFS_WAIT_BUFS);
131 bytes_fit =
132 (locked_queue_bytes + locked_queue_rbytes + bytes <= LFS_WAIT_BYTES);
133
134 #ifdef DEBUG
135 if (!count_fit) {
136 DLOG((DLOG_AVAIL, "lfs_fits_buf: no fit count: %d + %d + %d >= %d\n",
137 locked_queue_count, locked_queue_rcount,
138 n, LFS_WAIT_BUFS));
139 }
140 if (!bytes_fit) {
141 DLOG((DLOG_AVAIL, "lfs_fits_buf: no fit bytes: %ld + %ld + %d >= %ld\n",
142 locked_queue_bytes, locked_queue_rbytes,
143 bytes, LFS_WAIT_BYTES));
144 }
145 #endif /* DEBUG */
146
147 return (count_fit && bytes_fit);
148 }
149
150 /* ARGSUSED */
151 static int
152 lfs_reservebuf(struct lfs *fs, struct vnode *vp,
153 struct vnode *vp2, int n, int bytes)
154 {
155 int cantwait;
156
157 ASSERT_MAYBE_SEGLOCK(fs);
158 KASSERT(locked_queue_rcount >= 0);
159 KASSERT(locked_queue_rbytes >= 0);
160
161 cantwait = (VTOI(vp)->i_flag & IN_ADIROP) || fs->lfs_unlockvp == vp;
162 mutex_enter(&lfs_lock);
163 while (!cantwait && n > 0 && !lfs_fits_buf(fs, n, bytes)) {
164 int error;
165
166 lfs_flush(fs, 0, 0);
167
168 DLOG((DLOG_AVAIL, "lfs_reservebuf: waiting: count=%d, bytes=%ld\n",
169 locked_queue_count, locked_queue_bytes));
170 ++locked_queue_waiters;
171 error = cv_timedwait_sig(&locked_queue_cv, &lfs_lock,
172 hz * LFS_BUFWAIT);
173 --locked_queue_waiters;
174 if (error && error != EWOULDBLOCK) {
175 mutex_exit(&lfs_lock);
176 return error;
177 }
178 }
179
180 locked_queue_rcount += n;
181 locked_queue_rbytes += bytes;
182
183 if (n < 0 && locked_queue_waiters > 0) {
184 DLOG((DLOG_AVAIL, "lfs_reservebuf: broadcast: count=%d, bytes=%ld\n",
185 locked_queue_count, locked_queue_bytes));
186 cv_broadcast(&locked_queue_cv);
187 }
188
189 mutex_exit(&lfs_lock);
190
191 KASSERT(locked_queue_rcount >= 0);
192 KASSERT(locked_queue_rbytes >= 0);
193
194 return 0;
195 }
196
197 /*
198 * Try to reserve some blocks, prior to performing a sensitive operation that
199 * requires the vnode lock to be honored. If there is not enough space, give
200 * up the vnode lock temporarily and wait for the space to become available.
201 *
202 * Called with vp locked. (Note nowever that if fsb < 0, vp is ignored.)
203 *
204 * XXX YAMT - it isn't safe to unlock vp here
205 * because the node might be modified while we sleep.
206 * (eg. cached states like i_offset might be stale,
207 * the vnode might be truncated, etc..)
208 * maybe we should have a way to restart the vnodeop (EVOPRESTART?)
209 * or rearrange vnodeop interface to leave vnode locking to file system
210 * specific code so that each file systems can have their own vnode locking and
211 * vnode re-using strategies.
212 */
213 static int
214 lfs_reserveavail(struct lfs *fs, struct vnode *vp,
215 struct vnode *vp2, int fsb)
216 {
217 CLEANERINFO *cip;
218 struct buf *bp;
219 int error, slept;
220 int cantwait;
221
222 ASSERT_MAYBE_SEGLOCK(fs);
223 slept = 0;
224 mutex_enter(&lfs_lock);
225 cantwait = (VTOI(vp)->i_flag & IN_ADIROP) || fs->lfs_unlockvp == vp;
226 while (!cantwait && fsb > 0 &&
227 !lfs_fits(fs, fsb + fs->lfs_ravail + fs->lfs_favail)) {
228 mutex_exit(&lfs_lock);
229
230 if (!slept) {
231 DLOG((DLOG_AVAIL, "lfs_reserve: waiting for %ld (bfree = %d,"
232 " est_bfree = %d)\n",
233 fsb + fs->lfs_ravail + fs->lfs_favail,
234 fs->lfs_bfree, LFS_EST_BFREE(fs)));
235 }
236 ++slept;
237
238 /* Wake up the cleaner */
239 LFS_CLEANERINFO(cip, fs, bp);
240 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
241 lfs_wakeup_cleaner(fs);
242
243 mutex_enter(&lfs_lock);
244 /* Cleaner might have run while we were reading, check again */
245 if (lfs_fits(fs, fsb + fs->lfs_ravail + fs->lfs_favail))
246 break;
247
248 error = mtsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_reserve",
249 0, &lfs_lock);
250 if (error) {
251 mutex_exit(&lfs_lock);
252 return error;
253 }
254 }
255 #ifdef DEBUG
256 if (slept) {
257 DLOG((DLOG_AVAIL, "lfs_reserve: woke up\n"));
258 }
259 #endif
260 fs->lfs_ravail += fsb;
261 mutex_exit(&lfs_lock);
262
263 return 0;
264 }
265
266 #ifdef DIAGNOSTIC
267 int lfs_rescount;
268 int lfs_rescountdirop;
269 #endif
270
271 int
272 lfs_reserve(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
273 {
274 int error;
275
276 ASSERT_MAYBE_SEGLOCK(fs);
277 if (vp2) {
278 /* Make sure we're not in the process of reclaiming vp2 */
279 mutex_enter(&lfs_lock);
280 while(fs->lfs_flags & LFS_UNDIROP) {
281 mtsleep(&fs->lfs_flags, PRIBIO + 1, "lfsrundirop", 0,
282 &lfs_lock);
283 }
284 mutex_exit(&lfs_lock);
285 }
286
287 KASSERT(fsb < 0 || VOP_ISLOCKED(vp));
288 KASSERT(vp2 == NULL || fsb < 0 || VOP_ISLOCKED(vp2));
289 KASSERT(vp2 == NULL || vp2 != fs->lfs_unlockvp);
290
291 #ifdef DIAGNOSTIC
292 mutex_enter(&lfs_lock);
293 if (fsb > 0)
294 lfs_rescount++;
295 else if (fsb < 0)
296 lfs_rescount--;
297 if (lfs_rescount < 0)
298 panic("lfs_rescount");
299 mutex_exit(&lfs_lock);
300 #endif
301
302 /*
303 * XXX
304 * vref vnodes here so that cleaner doesn't try to reuse them.
305 * (see XXX comment in lfs_reserveavail)
306 */
307 vhold(vp);
308 if (vp2 != NULL) {
309 vhold(vp2);
310 }
311
312 error = lfs_reserveavail(fs, vp, vp2, fsb);
313 if (error)
314 goto done;
315
316 /*
317 * XXX just a guess. should be more precise.
318 */
319 error = lfs_reservebuf(fs, vp, vp2, fsb, lfs_fsbtob(fs, fsb));
320 if (error)
321 lfs_reserveavail(fs, vp, vp2, -fsb);
322
323 done:
324 holdrele(vp);
325 if (vp2 != NULL) {
326 holdrele(vp2);
327 }
328
329 return error;
330 }
331
332 int
333 lfs_bwrite(void *v)
334 {
335 struct vop_bwrite_args /* {
336 struct vnode *a_vp;
337 struct buf *a_bp;
338 } */ *ap = v;
339 struct buf *bp = ap->a_bp;
340
341 #ifdef DIAGNOSTIC
342 if (VTOI(bp->b_vp)->i_lfs->lfs_ronly == 0 && (bp->b_flags & B_ASYNC)) {
343 panic("bawrite LFS buffer");
344 }
345 #endif /* DIAGNOSTIC */
346 return lfs_bwrite_ext(bp, 0);
347 }
348
349 /*
350 * Determine if there is enough room currently available to write fsb
351 * blocks. We need enough blocks for the new blocks, the current
352 * inode blocks (including potentially the ifile inode), a summary block,
353 * and the segment usage table, plus an ifile block.
354 */
355 int
356 lfs_fits(struct lfs *fs, int fsb)
357 {
358 int needed;
359
360 ASSERT_NO_SEGLOCK(fs);
361 needed = fsb + lfs_btofsb(fs, fs->lfs_sumsize) +
362 ((howmany(fs->lfs_uinodes + 1, LFS_INOPB(fs)) + fs->lfs_segtabsz +
363 1) << (fs->lfs_bshift - fs->lfs_ffshift));
364
365 if (needed >= fs->lfs_avail) {
366 #ifdef DEBUG
367 DLOG((DLOG_AVAIL, "lfs_fits: no fit: fsb = %ld, uinodes = %ld, "
368 "needed = %ld, avail = %ld\n",
369 (long)fsb, (long)fs->lfs_uinodes, (long)needed,
370 (long)fs->lfs_avail));
371 #endif
372 return 0;
373 }
374 return 1;
375 }
376
377 int
378 lfs_availwait(struct lfs *fs, int fsb)
379 {
380 int error;
381 CLEANERINFO *cip;
382 struct buf *cbp;
383
384 ASSERT_NO_SEGLOCK(fs);
385 /* Push cleaner blocks through regardless */
386 mutex_enter(&lfs_lock);
387 if (LFS_SEGLOCK_HELD(fs) &&
388 fs->lfs_sp->seg_flags & (SEGM_CLEAN | SEGM_FORCE_CKP)) {
389 mutex_exit(&lfs_lock);
390 return 0;
391 }
392 mutex_exit(&lfs_lock);
393
394 while (!lfs_fits(fs, fsb)) {
395 /*
396 * Out of space, need cleaner to run.
397 * Update the cleaner info, then wake it up.
398 * Note the cleanerinfo block is on the ifile
399 * so it CANT_WAIT.
400 */
401 LFS_CLEANERINFO(cip, fs, cbp);
402 LFS_SYNC_CLEANERINFO(cip, fs, cbp, 0);
403
404 #ifdef DEBUG
405 DLOG((DLOG_AVAIL, "lfs_availwait: out of available space, "
406 "waiting on cleaner\n"));
407 #endif
408
409 lfs_wakeup_cleaner(fs);
410 #ifdef DIAGNOSTIC
411 if (LFS_SEGLOCK_HELD(fs))
412 panic("lfs_availwait: deadlock");
413 #endif
414 error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", 0);
415 if (error)
416 return (error);
417 }
418 return 0;
419 }
420
421 int
422 lfs_bwrite_ext(struct buf *bp, int flags)
423 {
424 struct lfs *fs;
425 struct inode *ip;
426 struct vnode *vp;
427 int fsb;
428
429 vp = bp->b_vp;
430 fs = VFSTOULFS(vp->v_mount)->um_lfs;
431
432 ASSERT_MAYBE_SEGLOCK(fs);
433 KASSERT(bp->b_cflags & BC_BUSY);
434 KASSERT(flags & BW_CLEAN || !LFS_IS_MALLOC_BUF(bp));
435 KASSERT(((bp->b_oflags | bp->b_flags) & (BO_DELWRI|B_LOCKED))
436 != BO_DELWRI);
437
438 /*
439 * Don't write *any* blocks if we're mounted read-only, or
440 * if we are "already unmounted".
441 *
442 * In particular the cleaner can't write blocks either.
443 */
444 if (fs->lfs_ronly || (fs->lfs_pflags & LFS_PF_CLEAN)) {
445 bp->b_oflags &= ~BO_DELWRI;
446 bp->b_flags |= B_READ; /* XXX is this right? --ks */
447 bp->b_error = 0;
448 mutex_enter(&bufcache_lock);
449 LFS_UNLOCK_BUF(bp);
450 if (LFS_IS_MALLOC_BUF(bp))
451 bp->b_cflags &= ~BC_BUSY;
452 else
453 brelsel(bp, 0);
454 mutex_exit(&bufcache_lock);
455 return (fs->lfs_ronly ? EROFS : 0);
456 }
457
458 /*
459 * Set the delayed write flag and use reassignbuf to move the buffer
460 * from the clean list to the dirty one.
461 *
462 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
463 * the buffer onto the LOCKED free list. This is necessary, otherwise
464 * getnewbuf() would try to reclaim the buffers using bawrite, which
465 * isn't going to work.
466 *
467 * XXX we don't let meta-data writes run out of space because they can
468 * come from the segment writer. We need to make sure that there is
469 * enough space reserved so that there's room to write meta-data
470 * blocks.
471 */
472 if ((bp->b_flags & B_LOCKED) == 0) {
473 fsb = lfs_numfrags(fs, bp->b_bcount);
474
475 ip = VTOI(vp);
476 mutex_enter(&lfs_lock);
477 if (flags & BW_CLEAN) {
478 LFS_SET_UINO(ip, IN_CLEANING);
479 } else {
480 LFS_SET_UINO(ip, IN_MODIFIED);
481 }
482 mutex_exit(&lfs_lock);
483 fs->lfs_avail -= fsb;
484
485 mutex_enter(&bufcache_lock);
486 mutex_enter(vp->v_interlock);
487 bp->b_oflags = (bp->b_oflags | BO_DELWRI) & ~BO_DONE;
488 LFS_LOCK_BUF(bp);
489 bp->b_flags &= ~B_READ;
490 bp->b_error = 0;
491 reassignbuf(bp, bp->b_vp);
492 mutex_exit(vp->v_interlock);
493 } else {
494 mutex_enter(&bufcache_lock);
495 }
496
497 if (bp->b_iodone != NULL)
498 bp->b_cflags &= ~BC_BUSY;
499 else
500 brelsel(bp, 0);
501 mutex_exit(&bufcache_lock);
502
503 return (0);
504 }
505
506 /*
507 * Called and return with the lfs_lock held.
508 */
509 void
510 lfs_flush_fs(struct lfs *fs, int flags)
511 {
512 ASSERT_NO_SEGLOCK(fs);
513 KASSERT(mutex_owned(&lfs_lock));
514 if (fs->lfs_ronly)
515 return;
516
517 if (lfs_dostats)
518 ++lfs_stats.flush_invoked;
519
520 fs->lfs_pdflush = 0;
521 mutex_exit(&lfs_lock);
522 lfs_writer_enter(fs, "fldirop");
523 lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
524 lfs_writer_leave(fs);
525 mutex_enter(&lfs_lock);
526 fs->lfs_favail = 0; /* XXX */
527 }
528
529 /*
530 * This routine initiates segment writes when LFS is consuming too many
531 * resources. Ideally the pageout daemon would be able to direct LFS
532 * more subtly.
533 * XXX We have one static count of locked buffers;
534 * XXX need to think more about the multiple filesystem case.
535 *
536 * Called and return with lfs_lock held.
537 * If fs != NULL, we hold the segment lock for fs.
538 */
539 void
540 lfs_flush(struct lfs *fs, int flags, int only_onefs)
541 {
542 extern u_int64_t locked_fakequeue_count;
543 struct mount *mp, *nmp;
544 struct lfs *tfs;
545
546 KASSERT(mutex_owned(&lfs_lock));
547 KDASSERT(fs == NULL || !LFS_SEGLOCK_HELD(fs));
548
549 if (lfs_dostats)
550 ++lfs_stats.write_exceeded;
551 /* XXX should we include SEGM_CKP here? */
552 if (lfs_writing && !(flags & SEGM_SYNC)) {
553 DLOG((DLOG_FLUSH, "lfs_flush: not flushing because another flush is active\n"));
554 return;
555 }
556 while (lfs_writing)
557 cv_wait(&lfs_writing_cv, &lfs_lock);
558 lfs_writing = 1;
559
560 mutex_exit(&lfs_lock);
561
562 if (only_onefs) {
563 KASSERT(fs != NULL);
564 if (vfs_busy(fs->lfs_ivnode->v_mount, NULL))
565 goto errout;
566 mutex_enter(&lfs_lock);
567 lfs_flush_fs(fs, flags);
568 mutex_exit(&lfs_lock);
569 vfs_unbusy(fs->lfs_ivnode->v_mount, false, NULL);
570 } else {
571 locked_fakequeue_count = 0;
572 mutex_enter(&mountlist_lock);
573 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
574 mp = nmp) {
575 if (vfs_busy(mp, &nmp)) {
576 DLOG((DLOG_FLUSH, "lfs_flush: fs vfs_busy\n"));
577 continue;
578 }
579 if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS,
580 sizeof(mp->mnt_stat.f_fstypename)) == 0) {
581 tfs = VFSTOULFS(mp)->um_lfs;
582 mutex_enter(&lfs_lock);
583 lfs_flush_fs(tfs, flags);
584 mutex_exit(&lfs_lock);
585 }
586 vfs_unbusy(mp, false, &nmp);
587 }
588 mutex_exit(&mountlist_lock);
589 }
590 LFS_DEBUG_COUNTLOCKED("flush");
591 wakeup(&lfs_subsys_pages);
592
593 errout:
594 mutex_enter(&lfs_lock);
595 KASSERT(lfs_writing);
596 lfs_writing = 0;
597 wakeup(&lfs_writing);
598 }
599
600 #define INOCOUNT(fs) howmany((fs)->lfs_uinodes, LFS_INOPB(fs))
601 #define INOBYTES(fs) ((fs)->lfs_uinodes * sizeof (struct ulfs1_dinode))
602
603 /*
604 * make sure that we don't have too many locked buffers.
605 * flush buffers if needed.
606 */
607 int
608 lfs_check(struct vnode *vp, daddr_t blkno, int flags)
609 {
610 int error;
611 struct lfs *fs;
612 struct inode *ip;
613 extern pid_t lfs_writer_daemon;
614
615 error = 0;
616 ip = VTOI(vp);
617
618 /* If out of buffers, wait on writer */
619 /* XXX KS - if it's the Ifile, we're probably the cleaner! */
620 if (ip->i_number == LFS_IFILE_INUM)
621 return 0;
622 /* If we're being called from inside a dirop, don't sleep */
623 if (ip->i_flag & IN_ADIROP)
624 return 0;
625
626 fs = ip->i_lfs;
627
628 ASSERT_NO_SEGLOCK(fs);
629
630 /*
631 * If we would flush below, but dirops are active, sleep.
632 * Note that a dirop cannot ever reach this code!
633 */
634 mutex_enter(&lfs_lock);
635 while (fs->lfs_dirops > 0 &&
636 (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
637 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
638 lfs_subsys_pages > LFS_MAX_PAGES ||
639 fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
640 lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0))
641 {
642 ++fs->lfs_diropwait;
643 mtsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0,
644 &lfs_lock);
645 --fs->lfs_diropwait;
646 }
647
648 #ifdef DEBUG
649 if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS)
650 DLOG((DLOG_FLUSH, "lfs_check: lqc = %d, max %d\n",
651 locked_queue_count + INOCOUNT(fs), LFS_MAX_BUFS));
652 if (locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
653 DLOG((DLOG_FLUSH, "lfs_check: lqb = %ld, max %ld\n",
654 locked_queue_bytes + INOBYTES(fs), LFS_MAX_BYTES));
655 if (lfs_subsys_pages > LFS_MAX_PAGES)
656 DLOG((DLOG_FLUSH, "lfs_check: lssp = %d, max %d\n",
657 lfs_subsys_pages, LFS_MAX_PAGES));
658 if (lfs_fs_pagetrip && fs->lfs_pages > lfs_fs_pagetrip)
659 DLOG((DLOG_FLUSH, "lfs_check: fssp = %d, trip at %d\n",
660 fs->lfs_pages, lfs_fs_pagetrip));
661 if (lfs_dirvcount > LFS_MAX_DIROP)
662 DLOG((DLOG_FLUSH, "lfs_check: ldvc = %d, max %d\n",
663 lfs_dirvcount, LFS_MAX_DIROP));
664 if (fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs))
665 DLOG((DLOG_FLUSH, "lfs_check: lfdvc = %d, max %d\n",
666 fs->lfs_dirvcount, LFS_MAX_FSDIROP(fs)));
667 if (fs->lfs_diropwait > 0)
668 DLOG((DLOG_FLUSH, "lfs_check: ldvw = %d\n",
669 fs->lfs_diropwait));
670 #endif
671
672 /* If there are too many pending dirops, we have to flush them. */
673 if (fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
674 lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0) {
675 mutex_exit(&lfs_lock);
676 lfs_flush_dirops(fs);
677 mutex_enter(&lfs_lock);
678 } else if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
679 locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
680 lfs_subsys_pages > LFS_MAX_PAGES ||
681 fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
682 lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0) {
683 lfs_flush(fs, flags, 0);
684 } else if (lfs_fs_pagetrip && fs->lfs_pages > lfs_fs_pagetrip) {
685 /*
686 * If we didn't flush the whole thing, some filesystems
687 * still might want to be flushed.
688 */
689 ++fs->lfs_pdflush;
690 wakeup(&lfs_writer_daemon);
691 }
692
693 while (locked_queue_count + INOCOUNT(fs) >= LFS_WAIT_BUFS ||
694 locked_queue_bytes + INOBYTES(fs) >= LFS_WAIT_BYTES ||
695 lfs_subsys_pages > LFS_WAIT_PAGES ||
696 fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
697 lfs_dirvcount > LFS_MAX_DIROP) {
698
699 if (lfs_dostats)
700 ++lfs_stats.wait_exceeded;
701 DLOG((DLOG_AVAIL, "lfs_check: waiting: count=%d, bytes=%ld\n",
702 locked_queue_count, locked_queue_bytes));
703 ++locked_queue_waiters;
704 error = cv_timedwait_sig(&locked_queue_cv, &lfs_lock,
705 hz * LFS_BUFWAIT);
706 --locked_queue_waiters;
707 if (error != EWOULDBLOCK)
708 break;
709
710 /*
711 * lfs_flush might not flush all the buffers, if some of the
712 * inodes were locked or if most of them were Ifile blocks
713 * and we weren't asked to checkpoint. Try flushing again
714 * to keep us from blocking indefinitely.
715 */
716 if (locked_queue_count + INOCOUNT(fs) >= LFS_MAX_BUFS ||
717 locked_queue_bytes + INOBYTES(fs) >= LFS_MAX_BYTES) {
718 lfs_flush(fs, flags | SEGM_CKP, 0);
719 }
720 }
721 mutex_exit(&lfs_lock);
722 return (error);
723 }
724
725 /*
726 * Allocate a new buffer header.
727 */
728 struct buf *
729 lfs_newbuf(struct lfs *fs, struct vnode *vp, daddr_t daddr, size_t size, int type)
730 {
731 struct buf *bp;
732 size_t nbytes;
733
734 ASSERT_MAYBE_SEGLOCK(fs);
735 nbytes = roundup(size, lfs_fsbtob(fs, 1));
736
737 bp = getiobuf(NULL, true);
738 if (nbytes) {
739 bp->b_data = lfs_malloc(fs, nbytes, type);
740 /* memset(bp->b_data, 0, nbytes); */
741 }
742 #ifdef DIAGNOSTIC
743 if (vp == NULL)
744 panic("vp is NULL in lfs_newbuf");
745 if (bp == NULL)
746 panic("bp is NULL after malloc in lfs_newbuf");
747 #endif
748
749 bp->b_bufsize = size;
750 bp->b_bcount = size;
751 bp->b_lblkno = daddr;
752 bp->b_blkno = daddr;
753 bp->b_error = 0;
754 bp->b_resid = 0;
755 bp->b_iodone = lfs_callback;
756 bp->b_cflags = BC_BUSY | BC_NOCACHE;
757 bp->b_private = fs;
758
759 mutex_enter(&bufcache_lock);
760 mutex_enter(vp->v_interlock);
761 bgetvp(vp, bp);
762 mutex_exit(vp->v_interlock);
763 mutex_exit(&bufcache_lock);
764
765 return (bp);
766 }
767
768 void
769 lfs_freebuf(struct lfs *fs, struct buf *bp)
770 {
771 struct vnode *vp;
772
773 if ((vp = bp->b_vp) != NULL) {
774 mutex_enter(&bufcache_lock);
775 mutex_enter(vp->v_interlock);
776 brelvp(bp);
777 mutex_exit(vp->v_interlock);
778 mutex_exit(&bufcache_lock);
779 }
780 if (!(bp->b_cflags & BC_INVAL)) { /* BC_INVAL indicates a "fake" buffer */
781 lfs_free(fs, bp->b_data, LFS_NB_UNKNOWN);
782 bp->b_data = NULL;
783 }
784 putiobuf(bp);
785 }
786
787 /*
788 * Count buffers on the "locked" queue, and compare it to a pro-forma count.
789 * Don't count malloced buffers, since they don't detract from the total.
790 */
791 void
792 lfs_countlocked(int *count, long *bytes, const char *msg)
793 {
794 struct buf *bp;
795 int n = 0;
796 long int size = 0L;
797
798 mutex_enter(&bufcache_lock);
799 TAILQ_FOREACH(bp, &bufqueues[BQ_LOCKED].bq_queue, b_freelist) {
800 KASSERT(bp->b_iodone == NULL);
801 n++;
802 size += bp->b_bufsize;
803 #ifdef DIAGNOSTIC
804 if (n > nbuf)
805 panic("lfs_countlocked: this can't happen: more"
806 " buffers locked than exist");
807 #endif
808 }
809 /*
810 * Theoretically this function never really does anything.
811 * Give a warning if we have to fix the accounting.
812 */
813 if (n != *count) {
814 DLOG((DLOG_LLIST, "lfs_countlocked: %s: adjusted buf count"
815 " from %d to %d\n", msg, *count, n));
816 }
817 if (size != *bytes) {
818 DLOG((DLOG_LLIST, "lfs_countlocked: %s: adjusted byte count"
819 " from %ld to %ld\n", msg, *bytes, size));
820 }
821 *count = n;
822 *bytes = size;
823 mutex_exit(&bufcache_lock);
824 return;
825 }
826
827 int
828 lfs_wait_pages(void)
829 {
830 int active, inactive;
831
832 uvm_estimatepageable(&active, &inactive);
833 return LFS_WAIT_RESOURCE(active + inactive + uvmexp.free, 1);
834 }
835
836 int
837 lfs_max_pages(void)
838 {
839 int active, inactive;
840
841 uvm_estimatepageable(&active, &inactive);
842 return LFS_MAX_RESOURCE(active + inactive + uvmexp.free, 1);
843 }
844