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