lfs_segment.c revision 1.136 1 /* $NetBSD: lfs_segment.c,v 1.136 2003/10/03 15:35:54 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000, 2001, 2002, 2003 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. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)lfs_segment.c 8.10 (Berkeley) 6/10/95
67 */
68
69 #include <sys/cdefs.h>
70 __KERNEL_RCSID(0, "$NetBSD: lfs_segment.c,v 1.136 2003/10/03 15:35:54 yamt Exp $");
71
72 #define ivndebug(vp,str) printf("ino %d: %s\n",VTOI(vp)->i_number,(str))
73
74 #if defined(_KERNEL_OPT)
75 #include "opt_ddb.h"
76 #endif
77
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/namei.h>
81 #include <sys/kernel.h>
82 #include <sys/resourcevar.h>
83 #include <sys/file.h>
84 #include <sys/stat.h>
85 #include <sys/buf.h>
86 #include <sys/proc.h>
87 #include <sys/vnode.h>
88 #include <sys/mount.h>
89
90 #include <miscfs/specfs/specdev.h>
91 #include <miscfs/fifofs/fifo.h>
92
93 #include <ufs/ufs/inode.h>
94 #include <ufs/ufs/dir.h>
95 #include <ufs/ufs/ufsmount.h>
96 #include <ufs/ufs/ufs_extern.h>
97
98 #include <ufs/lfs/lfs.h>
99 #include <ufs/lfs/lfs_extern.h>
100
101 #include <uvm/uvm.h>
102 #include <uvm/uvm_extern.h>
103
104 MALLOC_DEFINE(M_SEGMENT, "LFS segment", "Segment for LFS");
105
106 extern int count_lock_queue(void);
107 extern struct simplelock vnode_free_list_slock; /* XXX */
108 extern struct simplelock bqueue_slock; /* XXX */
109
110 static void lfs_generic_callback(struct buf *, void (*)(struct buf *));
111 static void lfs_super_aiodone(struct buf *);
112 static void lfs_cluster_aiodone(struct buf *);
113 static void lfs_cluster_callback(struct buf *);
114
115 /*
116 * Determine if it's OK to start a partial in this segment, or if we need
117 * to go on to a new segment.
118 */
119 #define LFS_PARTIAL_FITS(fs) \
120 ((fs)->lfs_fsbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
121 fragstofsb((fs), (fs)->lfs_frag))
122
123 int lfs_match_fake(struct lfs *, struct buf *);
124 void lfs_newseg(struct lfs *);
125 /* XXX ondisk32 */
126 void lfs_shellsort(struct buf **, int32_t *, int, int);
127 void lfs_supercallback(struct buf *);
128 void lfs_updatemeta(struct segment *);
129 void lfs_writesuper(struct lfs *, daddr_t);
130 int lfs_writevnodes(struct lfs *fs, struct mount *mp,
131 struct segment *sp, int dirops);
132
133 int lfs_allclean_wakeup; /* Cleaner wakeup address. */
134 int lfs_writeindir = 1; /* whether to flush indir on non-ckp */
135 int lfs_clean_vnhead = 0; /* Allow freeing to head of vn list */
136 int lfs_dirvcount = 0; /* # active dirops */
137
138 /* Statistics Counters */
139 int lfs_dostats = 1;
140 struct lfs_stats lfs_stats;
141
142 /* op values to lfs_writevnodes */
143 #define VN_REG 0
144 #define VN_DIROP 1
145 #define VN_EMPTY 2
146 #define VN_CLEAN 3
147
148 #define LFS_MAX_ACTIVE 10
149
150 /*
151 * XXX KS - Set modification time on the Ifile, so the cleaner can
152 * read the fs mod time off of it. We don't set IN_UPDATE here,
153 * since we don't really need this to be flushed to disk (and in any
154 * case that wouldn't happen to the Ifile until we checkpoint).
155 */
156 void
157 lfs_imtime(struct lfs *fs)
158 {
159 struct timespec ts;
160 struct inode *ip;
161
162 TIMEVAL_TO_TIMESPEC(&time, &ts);
163 ip = VTOI(fs->lfs_ivnode);
164 ip->i_ffs1_mtime = ts.tv_sec;
165 ip->i_ffs1_mtimensec = ts.tv_nsec;
166 }
167
168 /*
169 * Ifile and meta data blocks are not marked busy, so segment writes MUST be
170 * single threaded. Currently, there are two paths into lfs_segwrite, sync()
171 * and getnewbuf(). They both mark the file system busy. Lfs_vflush()
172 * explicitly marks the file system busy. So lfs_segwrite is safe. I think.
173 */
174
175 #define SET_FLUSHING(fs,vp) (fs)->lfs_flushvp = (vp)
176 #define IS_FLUSHING(fs,vp) ((fs)->lfs_flushvp == (vp))
177 #define CLR_FLUSHING(fs,vp) (fs)->lfs_flushvp = NULL
178
179 int
180 lfs_vflush(struct vnode *vp)
181 {
182 struct inode *ip;
183 struct lfs *fs;
184 struct segment *sp;
185 struct buf *bp, *nbp, *tbp, *tnbp;
186 int error, s;
187 int flushed;
188 #if 0
189 int redo;
190 #endif
191
192 ip = VTOI(vp);
193 fs = VFSTOUFS(vp->v_mount)->um_lfs;
194
195 if (ip->i_flag & IN_CLEANING) {
196 #ifdef DEBUG_LFS
197 ivndebug(vp,"vflush/in_cleaning");
198 #endif
199 LFS_CLR_UINO(ip, IN_CLEANING);
200 LFS_SET_UINO(ip, IN_MODIFIED);
201
202 /*
203 * Toss any cleaning buffers that have real counterparts
204 * to avoid losing new data.
205 */
206 s = splbio();
207 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
208 nbp = LIST_NEXT(bp, b_vnbufs);
209 if (!LFS_IS_MALLOC_BUF(bp))
210 continue;
211 /*
212 * Look for pages matching the range covered
213 * by cleaning blocks. It's okay if more dirty
214 * pages appear, so long as none disappear out
215 * from under us.
216 */
217 if (bp->b_lblkno > 0 && vp->v_type == VREG &&
218 vp != fs->lfs_ivnode) {
219 struct vm_page *pg;
220 voff_t off;
221
222 simple_lock(&vp->v_interlock);
223 for (off = lblktosize(fs, bp->b_lblkno);
224 off < lblktosize(fs, bp->b_lblkno + 1);
225 off += PAGE_SIZE) {
226 pg = uvm_pagelookup(&vp->v_uobj, off);
227 if (pg == NULL)
228 continue;
229 if ((pg->flags & PG_CLEAN) == 0 ||
230 pmap_is_modified(pg)) {
231 fs->lfs_avail += btofsb(fs,
232 bp->b_bcount);
233 wakeup(&fs->lfs_avail);
234 lfs_freebuf(fs, bp);
235 bp = NULL;
236 goto nextbp;
237 }
238 }
239 simple_unlock(&vp->v_interlock);
240 }
241 for (tbp = LIST_FIRST(&vp->v_dirtyblkhd); tbp;
242 tbp = tnbp)
243 {
244 tnbp = LIST_NEXT(tbp, b_vnbufs);
245 if (tbp->b_vp == bp->b_vp
246 && tbp->b_lblkno == bp->b_lblkno
247 && tbp != bp)
248 {
249 fs->lfs_avail += btofsb(fs,
250 bp->b_bcount);
251 wakeup(&fs->lfs_avail);
252 lfs_freebuf(fs, bp);
253 bp = NULL;
254 break;
255 }
256 }
257 nextbp:
258 ;
259 }
260 splx(s);
261 }
262
263 /* If the node is being written, wait until that is done */
264 s = splbio();
265 if (WRITEINPROG(vp)) {
266 #ifdef DEBUG_LFS
267 ivndebug(vp,"vflush/writeinprog");
268 #endif
269 tsleep(vp, PRIBIO+1, "lfs_vw", 0);
270 }
271 splx(s);
272
273 /* Protect against VXLOCK deadlock in vinvalbuf() */
274 lfs_seglock(fs, SEGM_SYNC);
275
276 /* If we're supposed to flush a freed inode, just toss it */
277 /* XXX - seglock, so these buffers can't be gathered, right? */
278 if (ip->i_mode == 0) {
279 printf("lfs_vflush: ino %d is freed, not flushing\n",
280 ip->i_number);
281 s = splbio();
282 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
283 nbp = LIST_NEXT(bp, b_vnbufs);
284 if (bp->b_flags & B_DELWRI) { /* XXX always true? */
285 fs->lfs_avail += btofsb(fs, bp->b_bcount);
286 wakeup(&fs->lfs_avail);
287 }
288 /* Copied from lfs_writeseg */
289 if (bp->b_flags & B_CALL) {
290 biodone(bp);
291 } else {
292 bremfree(bp);
293 LFS_UNLOCK_BUF(bp);
294 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
295 B_GATHERED);
296 bp->b_flags |= B_DONE;
297 reassignbuf(bp, vp);
298 brelse(bp);
299 }
300 }
301 splx(s);
302 LFS_CLR_UINO(ip, IN_CLEANING);
303 LFS_CLR_UINO(ip, IN_MODIFIED | IN_ACCESSED);
304 ip->i_flag &= ~IN_ALLMOD;
305 printf("lfs_vflush: done not flushing ino %d\n",
306 ip->i_number);
307 lfs_segunlock(fs);
308 return 0;
309 }
310
311 SET_FLUSHING(fs,vp);
312 if (fs->lfs_nactive > LFS_MAX_ACTIVE ||
313 (fs->lfs_sp->seg_flags & SEGM_CKP)) {
314 error = lfs_segwrite(vp->v_mount, SEGM_CKP | SEGM_SYNC);
315 CLR_FLUSHING(fs,vp);
316 lfs_segunlock(fs);
317 return error;
318 }
319 sp = fs->lfs_sp;
320
321 flushed = 0;
322 if (VPISEMPTY(vp)) {
323 lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
324 ++flushed;
325 } else if ((ip->i_flag & IN_CLEANING) &&
326 (fs->lfs_sp->seg_flags & SEGM_CLEAN)) {
327 #ifdef DEBUG_LFS
328 ivndebug(vp,"vflush/clean");
329 #endif
330 lfs_writevnodes(fs, vp->v_mount, sp, VN_CLEAN);
331 ++flushed;
332 } else if (lfs_dostats) {
333 if (!VPISEMPTY(vp) || (VTOI(vp)->i_flag & IN_ALLMOD))
334 ++lfs_stats.vflush_invoked;
335 #ifdef DEBUG_LFS
336 ivndebug(vp,"vflush");
337 #endif
338 }
339
340 #ifdef DIAGNOSTIC
341 /* XXX KS This actually can happen right now, though it shouldn't(?) */
342 if (vp->v_flag & VDIROP) {
343 printf("lfs_vflush: flushing VDIROP, this shouldn\'t be\n");
344 /* panic("VDIROP being flushed...this can\'t happen"); */
345 }
346 if (vp->v_usecount < 0) {
347 printf("usecount=%ld\n", (long)vp->v_usecount);
348 panic("lfs_vflush: usecount<0");
349 }
350 #endif
351
352 #if 1
353 do {
354 do {
355 if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL)
356 lfs_writefile(fs, sp, vp);
357 } while (lfs_writeinode(fs, sp, ip));
358 } while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
359 #else
360 if (flushed && vp != fs->lfs_ivnode)
361 lfs_writeseg(fs, sp);
362 else do {
363 fs->lfs_flags &= ~LFS_IFDIRTY;
364 lfs_writefile(fs, sp, vp);
365 redo = lfs_writeinode(fs, sp, ip);
366 redo += lfs_writeseg(fs, sp);
367 redo += (fs->lfs_flags & LFS_IFDIRTY);
368 } while (redo && vp == fs->lfs_ivnode);
369 #endif
370 if (lfs_dostats) {
371 ++lfs_stats.nwrites;
372 if (sp->seg_flags & SEGM_SYNC)
373 ++lfs_stats.nsync_writes;
374 if (sp->seg_flags & SEGM_CKP)
375 ++lfs_stats.ncheckpoints;
376 }
377 /*
378 * If we were called from somewhere that has already held the seglock
379 * (e.g., lfs_markv()), the lfs_segunlock will not wait for
380 * the write to complete because we are still locked.
381 * Since lfs_vflush() must return the vnode with no dirty buffers,
382 * we must explicitly wait, if that is the case.
383 *
384 * We compare the iocount against 1, not 0, because it is
385 * artificially incremented by lfs_seglock().
386 */
387 simple_lock(&fs->lfs_interlock);
388 if (fs->lfs_seglock > 1) {
389 simple_unlock(&fs->lfs_interlock);
390 while (fs->lfs_iocount > 1)
391 (void)tsleep(&fs->lfs_iocount, PRIBIO + 1,
392 "lfs_vflush", 0);
393 } else
394 simple_unlock(&fs->lfs_interlock);
395
396 lfs_segunlock(fs);
397
398 /* Wait for these buffers to be recovered by aiodoned */
399 s = splbio();
400 simple_lock(&global_v_numoutput_slock);
401 while (vp->v_numoutput > 0) {
402 ltsleep(&vp->v_numoutput, PRIBIO + 1, "lfs_vf2", 0,
403 &global_v_numoutput_slock);
404 }
405 simple_unlock(&global_v_numoutput_slock);
406 splx(s);
407
408 CLR_FLUSHING(fs,vp);
409 return (0);
410 }
411
412 #ifdef DEBUG_LFS_VERBOSE
413 # define vndebug(vp,str) if (VTOI(vp)->i_flag & IN_CLEANING) printf("not writing ino %d because %s (op %d)\n",VTOI(vp)->i_number,(str),op)
414 #else
415 # define vndebug(vp,str)
416 #endif
417
418 int
419 lfs_writevnodes(struct lfs *fs, struct mount *mp, struct segment *sp, int op)
420 {
421 struct inode *ip;
422 struct vnode *vp, *nvp;
423 int inodes_written = 0, only_cleaning;
424
425 #ifndef LFS_NO_BACKVP_HACK
426 /* BEGIN HACK */
427 #define VN_OFFSET \
428 (((caddr_t)&LIST_NEXT(vp, v_mntvnodes)) - (caddr_t)vp)
429 #define BACK_VP(VP) \
430 ((struct vnode *)(((caddr_t)(VP)->v_mntvnodes.le_prev) - VN_OFFSET))
431 #define BEG_OF_VLIST \
432 ((struct vnode *)(((caddr_t)&LIST_FIRST(&mp->mnt_vnodelist)) \
433 - VN_OFFSET))
434
435 /* Find last vnode. */
436 loop: for (vp = LIST_FIRST(&mp->mnt_vnodelist);
437 vp && LIST_NEXT(vp, v_mntvnodes) != NULL;
438 vp = LIST_NEXT(vp, v_mntvnodes));
439 for (; vp && vp != BEG_OF_VLIST; vp = nvp) {
440 nvp = BACK_VP(vp);
441 #else
442 loop:
443 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
444 nvp = LIST_NEXT(vp, v_mntvnodes);
445 #endif
446 /*
447 * If the vnode that we are about to sync is no longer
448 * associated with this mount point, start over.
449 */
450 if (vp->v_mount != mp) {
451 printf("lfs_writevnodes: starting over\n");
452 /*
453 * After this, pages might be busy
454 * due to our own previous putpages.
455 * Start actual segment write here to avoid deadlock.
456 */
457 (void)lfs_writeseg(fs, sp);
458 goto loop;
459 }
460
461 if (vp->v_type == VNON) {
462 continue;
463 }
464
465 ip = VTOI(vp);
466 if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) ||
467 (op != VN_DIROP && op != VN_CLEAN &&
468 (vp->v_flag & VDIROP))) {
469 vndebug(vp,"dirop");
470 continue;
471 }
472
473 if (op == VN_EMPTY && !VPISEMPTY(vp)) {
474 vndebug(vp,"empty");
475 continue;
476 }
477
478 if (op == VN_CLEAN && ip->i_number != LFS_IFILE_INUM
479 && vp != fs->lfs_flushvp
480 && !(ip->i_flag & IN_CLEANING)) {
481 vndebug(vp,"cleaning");
482 continue;
483 }
484
485 if (lfs_vref(vp)) {
486 vndebug(vp,"vref");
487 continue;
488 }
489
490 only_cleaning = 0;
491 /*
492 * Write the inode/file if dirty and it's not the IFILE.
493 */
494 if ((ip->i_flag & IN_ALLMOD) || !VPISEMPTY(vp)) {
495 only_cleaning =
496 ((ip->i_flag & IN_ALLMOD) == IN_CLEANING);
497
498 if (ip->i_number != LFS_IFILE_INUM)
499 lfs_writefile(fs, sp, vp);
500 if (!VPISEMPTY(vp)) {
501 if (WRITEINPROG(vp)) {
502 #ifdef DEBUG_LFS
503 ivndebug(vp,"writevnodes/write2");
504 #endif
505 } else if (!(ip->i_flag & IN_ALLMOD)) {
506 #ifdef DEBUG_LFS
507 printf("<%d>",ip->i_number);
508 #endif
509 LFS_SET_UINO(ip, IN_MODIFIED);
510 }
511 }
512 (void) lfs_writeinode(fs, sp, ip);
513 inodes_written++;
514 }
515
516 if (lfs_clean_vnhead && only_cleaning)
517 lfs_vunref_head(vp);
518 else
519 lfs_vunref(vp);
520 }
521 return inodes_written;
522 }
523
524 /*
525 * Do a checkpoint.
526 */
527 int
528 lfs_segwrite(struct mount *mp, int flags)
529 {
530 struct buf *bp;
531 struct inode *ip;
532 struct lfs *fs;
533 struct segment *sp;
534 struct vnode *vp;
535 SEGUSE *segusep;
536 int do_ckp, did_ckp, error, s;
537 unsigned n, segleft, maxseg, sn, i, curseg;
538 int writer_set = 0;
539 int dirty;
540 int redo;
541
542 fs = VFSTOUFS(mp)->um_lfs;
543
544 if (fs->lfs_ronly)
545 return EROFS;
546
547 lfs_imtime(fs);
548
549 /*
550 * Allocate a segment structure and enough space to hold pointers to
551 * the maximum possible number of buffers which can be described in a
552 * single summary block.
553 */
554 do_ckp = (flags & SEGM_CKP) || fs->lfs_nactive > LFS_MAX_ACTIVE;
555 lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
556 sp = fs->lfs_sp;
557
558 /*
559 * If lfs_flushvp is non-NULL, we are called from lfs_vflush,
560 * in which case we have to flush *all* buffers off of this vnode.
561 * We don't care about other nodes, but write any non-dirop nodes
562 * anyway in anticipation of another getnewvnode().
563 *
564 * If we're cleaning we only write cleaning and ifile blocks, and
565 * no dirops, since otherwise we'd risk corruption in a crash.
566 */
567 if (sp->seg_flags & SEGM_CLEAN)
568 lfs_writevnodes(fs, mp, sp, VN_CLEAN);
569 else if (!(sp->seg_flags & SEGM_FORCE_CKP)) {
570 lfs_writevnodes(fs, mp, sp, VN_REG);
571 if (!fs->lfs_dirops || !fs->lfs_flushvp) {
572 error = lfs_writer_enter(fs, "lfs writer");
573 if (error) {
574 printf("segwrite mysterious error\n");
575 /* XXX why not segunlock? */
576 pool_put(&fs->lfs_bpppool, sp->bpp);
577 sp->bpp = NULL;
578 pool_put(&fs->lfs_segpool, sp);
579 sp = fs->lfs_sp = NULL;
580 return (error);
581 }
582 writer_set = 1;
583 lfs_writevnodes(fs, mp, sp, VN_DIROP);
584 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
585 }
586 }
587
588 /*
589 * If we are doing a checkpoint, mark everything since the
590 * last checkpoint as no longer ACTIVE.
591 */
592 if (do_ckp) {
593 segleft = fs->lfs_nseg;
594 curseg = 0;
595 for (n = 0; n < fs->lfs_segtabsz; n++) {
596 dirty = 0;
597 if (bread(fs->lfs_ivnode,
598 fs->lfs_cleansz + n, fs->lfs_bsize, NOCRED, &bp))
599 panic("lfs_segwrite: ifile read");
600 segusep = (SEGUSE *)bp->b_data;
601 maxseg = min(segleft, fs->lfs_sepb);
602 for (i = 0; i < maxseg; i++) {
603 sn = curseg + i;
604 if (sn != dtosn(fs, fs->lfs_curseg) &&
605 segusep->su_flags & SEGUSE_ACTIVE) {
606 segusep->su_flags &= ~SEGUSE_ACTIVE;
607 --fs->lfs_nactive;
608 ++dirty;
609 }
610 fs->lfs_suflags[fs->lfs_activesb][sn] =
611 segusep->su_flags;
612 if (fs->lfs_version > 1)
613 ++segusep;
614 else
615 segusep = (SEGUSE *)
616 ((SEGUSE_V1 *)segusep + 1);
617 }
618
619 if (dirty)
620 error = LFS_BWRITE_LOG(bp); /* Ifile */
621 else
622 brelse(bp);
623 segleft -= fs->lfs_sepb;
624 curseg += fs->lfs_sepb;
625 }
626 }
627
628 did_ckp = 0;
629 if (do_ckp || fs->lfs_doifile) {
630 do {
631 vp = fs->lfs_ivnode;
632
633 #ifdef DEBUG
634 LFS_ENTER_LOG("pretend", __FILE__, __LINE__, 0, 0);
635 #endif
636 fs->lfs_flags &= ~LFS_IFDIRTY;
637
638 ip = VTOI(vp);
639
640 if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL)
641 lfs_writefile(fs, sp, vp);
642
643 if (ip->i_flag & IN_ALLMOD)
644 ++did_ckp;
645 redo = lfs_writeinode(fs, sp, ip);
646 redo += lfs_writeseg(fs, sp);
647 redo += (fs->lfs_flags & LFS_IFDIRTY);
648 } while (redo && do_ckp);
649
650 /*
651 * Unless we are unmounting, the Ifile may continue to have
652 * dirty blocks even after a checkpoint, due to changes to
653 * inodes' atime. If we're checkpointing, it's "impossible"
654 * for other parts of the Ifile to be dirty after the loop
655 * above, since we hold the segment lock.
656 */
657 s = splbio();
658 if (LIST_EMPTY(&vp->v_dirtyblkhd)) {
659 LFS_CLR_UINO(ip, IN_ALLMOD);
660 }
661 #ifdef DIAGNOSTIC
662 else if (do_ckp) {
663 LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
664 if (bp->b_lblkno < fs->lfs_cleansz +
665 fs->lfs_segtabsz &&
666 !(bp->b_flags & B_GATHERED)) {
667 panic("dirty blocks");
668 }
669 }
670 }
671 #endif
672 splx(s);
673 } else {
674 (void) lfs_writeseg(fs, sp);
675 }
676
677 /* Note Ifile no longer needs to be written */
678 fs->lfs_doifile = 0;
679 if (writer_set)
680 lfs_writer_leave(fs);
681
682 /*
683 * If we didn't write the Ifile, we didn't really do anything.
684 * That means that (1) there is a checkpoint on disk and (2)
685 * nothing has changed since it was written.
686 *
687 * Take the flags off of the segment so that lfs_segunlock
688 * doesn't have to write the superblock either.
689 */
690 if (do_ckp && !did_ckp) {
691 sp->seg_flags &= ~SEGM_CKP;
692 }
693
694 if (lfs_dostats) {
695 ++lfs_stats.nwrites;
696 if (sp->seg_flags & SEGM_SYNC)
697 ++lfs_stats.nsync_writes;
698 if (sp->seg_flags & SEGM_CKP)
699 ++lfs_stats.ncheckpoints;
700 }
701 lfs_segunlock(fs);
702 return (0);
703 }
704
705 /*
706 * Write the dirty blocks associated with a vnode.
707 */
708 void
709 lfs_writefile(struct lfs *fs, struct segment *sp, struct vnode *vp)
710 {
711 struct buf *bp;
712 struct finfo *fip;
713 struct inode *ip;
714 IFILE *ifp;
715 int i, frag;
716
717 ip = VTOI(vp);
718
719 if (sp->seg_bytes_left < fs->lfs_bsize ||
720 sp->sum_bytes_left < sizeof(struct finfo))
721 (void) lfs_writeseg(fs, sp);
722
723 sp->sum_bytes_left -= FINFOSIZE;
724 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
725
726 if (vp->v_flag & VDIROP)
727 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
728
729 fip = sp->fip;
730 fip->fi_nblocks = 0;
731 fip->fi_ino = ip->i_number;
732 LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
733 fip->fi_version = ifp->if_version;
734 brelse(bp);
735
736 if (sp->seg_flags & SEGM_CLEAN) {
737 lfs_gather(fs, sp, vp, lfs_match_fake);
738 /*
739 * For a file being flushed, we need to write *all* blocks.
740 * This means writing the cleaning blocks first, and then
741 * immediately following with any non-cleaning blocks.
742 * The same is true of the Ifile since checkpoints assume
743 * that all valid Ifile blocks are written.
744 */
745 if (IS_FLUSHING(fs,vp) || vp == fs->lfs_ivnode) {
746 lfs_gather(fs, sp, vp, lfs_match_data);
747 /*
748 * Don't call VOP_PUTPAGES: if we're flushing,
749 * we've already done it, and the Ifile doesn't
750 * use the page cache.
751 */
752 }
753 } else {
754 lfs_gather(fs, sp, vp, lfs_match_data);
755 /*
756 * If we're flushing, we've already called VOP_PUTPAGES
757 * so don't do it again. Otherwise, we want to write
758 * everything we've got.
759 */
760 if (!IS_FLUSHING(fs, vp)) {
761 simple_lock(&vp->v_interlock);
762 VOP_PUTPAGES(vp, 0, 0,
763 PGO_CLEANIT | PGO_ALLPAGES | PGO_LOCKED);
764 }
765 }
766
767 /*
768 * It may not be necessary to write the meta-data blocks at this point,
769 * as the roll-forward recovery code should be able to reconstruct the
770 * list.
771 *
772 * We have to write them anyway, though, under two conditions: (1) the
773 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are
774 * checkpointing.
775 *
776 * BUT if we are cleaning, we might have indirect blocks that refer to
777 * new blocks not being written yet, in addition to fragments being
778 * moved out of a cleaned segment. If that is the case, don't
779 * write the indirect blocks, or the finfo will have a small block
780 * in the middle of it!
781 * XXX in this case isn't the inode size wrong too?
782 */
783 frag = 0;
784 if (sp->seg_flags & SEGM_CLEAN) {
785 for (i = 0; i < NDADDR; i++)
786 if (ip->i_lfs_fragsize[i] > 0 &&
787 ip->i_lfs_fragsize[i] < fs->lfs_bsize)
788 ++frag;
789 }
790 #ifdef DIAGNOSTIC
791 if (frag > 1)
792 panic("lfs_writefile: more than one fragment!");
793 #endif
794 if (IS_FLUSHING(fs, vp) ||
795 (frag == 0 && (lfs_writeindir || (sp->seg_flags & SEGM_CKP)))) {
796 lfs_gather(fs, sp, vp, lfs_match_indir);
797 lfs_gather(fs, sp, vp, lfs_match_dindir);
798 lfs_gather(fs, sp, vp, lfs_match_tindir);
799 }
800 fip = sp->fip;
801 if (fip->fi_nblocks != 0) {
802 sp->fip = (FINFO*)((caddr_t)fip + FINFOSIZE +
803 sizeof(int32_t) * (fip->fi_nblocks));
804 sp->start_lbp = &sp->fip->fi_blocks[0];
805 } else {
806 sp->sum_bytes_left += FINFOSIZE;
807 --((SEGSUM *)(sp->segsum))->ss_nfinfo;
808 }
809 }
810
811 int
812 lfs_writeinode(struct lfs *fs, struct segment *sp, struct inode *ip)
813 {
814 struct buf *bp, *ibp;
815 struct ufs1_dinode *cdp;
816 IFILE *ifp;
817 SEGUSE *sup;
818 daddr_t daddr;
819 int32_t *daddrp; /* XXX ondisk32 */
820 ino_t ino;
821 int error, i, ndx, fsb = 0;
822 int redo_ifile = 0;
823 struct timespec ts;
824 int gotblk = 0;
825
826 if (!(ip->i_flag & IN_ALLMOD))
827 return (0);
828
829 /* Allocate a new inode block if necessary. */
830 if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) &&
831 sp->ibp == NULL) {
832 /* Allocate a new segment if necessary. */
833 if (sp->seg_bytes_left < fs->lfs_ibsize ||
834 sp->sum_bytes_left < sizeof(int32_t))
835 (void) lfs_writeseg(fs, sp);
836
837 /* Get next inode block. */
838 daddr = fs->lfs_offset;
839 fs->lfs_offset += btofsb(fs, fs->lfs_ibsize);
840 sp->ibp = *sp->cbpp++ =
841 getblk(VTOI(fs->lfs_ivnode)->i_devvp,
842 fsbtodb(fs, daddr), fs->lfs_ibsize, 0, 0);
843 gotblk++;
844
845 /* Zero out inode numbers */
846 for (i = 0; i < INOPB(fs); ++i)
847 ((struct ufs1_dinode *)sp->ibp->b_data)[i].di_inumber =
848 0;
849
850 ++sp->start_bpp;
851 fs->lfs_avail -= btofsb(fs, fs->lfs_ibsize);
852 /* Set remaining space counters. */
853 sp->seg_bytes_left -= fs->lfs_ibsize;
854 sp->sum_bytes_left -= sizeof(int32_t);
855 ndx = fs->lfs_sumsize / sizeof(int32_t) -
856 sp->ninodes / INOPB(fs) - 1;
857 ((int32_t *)(sp->segsum))[ndx] = daddr;
858 }
859
860 /* Update the inode times and copy the inode onto the inode page. */
861 TIMEVAL_TO_TIMESPEC(&time, &ts);
862 /* XXX kludge --- don't redirty the ifile just to put times on it */
863 if (ip->i_number != LFS_IFILE_INUM)
864 LFS_ITIMES(ip, &ts, &ts, &ts);
865
866 /*
867 * If this is the Ifile, and we've already written the Ifile in this
868 * partial segment, just overwrite it (it's not on disk yet) and
869 * continue.
870 *
871 * XXX we know that the bp that we get the second time around has
872 * already been gathered.
873 */
874 if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
875 *(sp->idp) = *ip->i_din.ffs1_din;
876 ip->i_lfs_osize = ip->i_size;
877 return 0;
878 }
879
880 bp = sp->ibp;
881 cdp = ((struct ufs1_dinode *)bp->b_data) + (sp->ninodes % INOPB(fs));
882 *cdp = *ip->i_din.ffs1_din;
883 #ifdef LFS_IFILE_FRAG_ADDRESSING
884 if (fs->lfs_version > 1)
885 fsb = (sp->ninodes % INOPB(fs)) / INOPF(fs);
886 #endif
887
888 /*
889 * If we are cleaning, ensure that we don't write UNWRITTEN disk
890 * addresses to disk; possibly revert the inode size.
891 * XXX By not writing these blocks, we are making the lfs_avail
892 * XXX count on disk wrong by the same amount. We should be
893 * XXX able to "borrow" from lfs_avail and return it after the
894 * XXX Ifile is written. See also in lfs_writeseg.
895 */
896 if (ip->i_lfs_effnblks != ip->i_ffs1_blocks) {
897 cdp->di_size = ip->i_lfs_osize;
898 #ifdef DEBUG_LFS
899 printf("lfs_writeinode: cleansing ino %d (%d != %d)\n",
900 ip->i_number, ip->i_lfs_effnblks, ip->i_ffs1_blocks);
901 #endif
902 for (daddrp = cdp->di_db; daddrp < cdp->di_ib + NIADDR;
903 daddrp++) {
904 if (*daddrp == UNWRITTEN) {
905 #ifdef DEBUG_LFS
906 printf("lfs_writeinode: wiping UNWRITTEN\n");
907 #endif
908 *daddrp = 0;
909 }
910 }
911 } else {
912 /* If all blocks are goig to disk, update the "size on disk" */
913 ip->i_lfs_osize = ip->i_size;
914 }
915
916 if (ip->i_flag & IN_CLEANING)
917 LFS_CLR_UINO(ip, IN_CLEANING);
918 else {
919 /* XXX IN_ALLMOD */
920 LFS_CLR_UINO(ip, IN_ACCESSED | IN_ACCESS | IN_CHANGE |
921 IN_UPDATE);
922 if (ip->i_lfs_effnblks == ip->i_ffs1_blocks)
923 LFS_CLR_UINO(ip, IN_MODIFIED);
924 #ifdef DEBUG_LFS
925 else
926 printf("lfs_writeinode: ino %d: real blks=%d, "
927 "eff=%d\n", ip->i_number, ip->i_ffs1_blocks,
928 ip->i_lfs_effnblks);
929 #endif
930 }
931
932 if (ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
933 sp->idp = ((struct ufs1_dinode *)bp->b_data) +
934 (sp->ninodes % INOPB(fs));
935 if (gotblk) {
936 LFS_LOCK_BUF(bp);
937 brelse(bp);
938 }
939
940 /* Increment inode count in segment summary block. */
941 ++((SEGSUM *)(sp->segsum))->ss_ninos;
942
943 /* If this page is full, set flag to allocate a new page. */
944 if (++sp->ninodes % INOPB(fs) == 0)
945 sp->ibp = NULL;
946
947 /*
948 * If updating the ifile, update the super-block. Update the disk
949 * address and access times for this inode in the ifile.
950 */
951 ino = ip->i_number;
952 if (ino == LFS_IFILE_INUM) {
953 daddr = fs->lfs_idaddr;
954 fs->lfs_idaddr = dbtofsb(fs, bp->b_blkno);
955 } else {
956 LFS_IENTRY(ifp, fs, ino, ibp);
957 daddr = ifp->if_daddr;
958 ifp->if_daddr = dbtofsb(fs, bp->b_blkno) + fsb;
959 #ifdef LFS_DEBUG_NEXTFREE
960 if (ino > 3 && ifp->if_nextfree) {
961 vprint("lfs_writeinode",ITOV(ip));
962 printf("lfs_writeinode: updating free ino %d\n",
963 ip->i_number);
964 }
965 #endif
966 error = LFS_BWRITE_LOG(ibp); /* Ifile */
967 }
968
969 /*
970 * The inode's last address should not be in the current partial
971 * segment, except under exceptional circumstances (lfs_writevnodes
972 * had to start over, and in the meantime more blocks were written
973 * to a vnode). Both inodes will be accounted to this segment
974 * in lfs_writeseg so we need to subtract the earlier version
975 * here anyway. The segment count can temporarily dip below
976 * zero here; keep track of how many duplicates we have in
977 * "dupino" so we don't panic below.
978 */
979 if (daddr >= fs->lfs_lastpseg && daddr <= dbtofsb(fs, bp->b_blkno)) {
980 ++sp->ndupino;
981 printf("lfs_writeinode: last inode addr in current pseg "
982 "(ino %d daddr 0x%llx) ndupino=%d\n", ino,
983 (long long)daddr, sp->ndupino);
984 }
985 /*
986 * Account the inode: it no longer belongs to its former segment,
987 * though it will not belong to the new segment until that segment
988 * is actually written.
989 */
990 if (daddr != LFS_UNUSED_DADDR) {
991 u_int32_t oldsn = dtosn(fs, daddr);
992 #ifdef DIAGNOSTIC
993 int ndupino = (sp->seg_number == oldsn) ? sp->ndupino : 0;
994 #endif
995 LFS_SEGENTRY(sup, fs, oldsn, bp);
996 #ifdef DIAGNOSTIC
997 if (sup->su_nbytes +
998 sizeof (struct ufs1_dinode) * ndupino
999 < sizeof (struct ufs1_dinode)) {
1000 printf("lfs_writeinode: negative bytes "
1001 "(segment %" PRIu32 " short by %d, "
1002 "oldsn=%" PRIu32 ", cursn=%" PRIu32
1003 ", daddr=%" PRId64 ", su_nbytes=%u, "
1004 "ndupino=%d)\n",
1005 dtosn(fs, daddr),
1006 (int)sizeof (struct ufs1_dinode) *
1007 (1 - sp->ndupino) - sup->su_nbytes,
1008 oldsn, sp->seg_number, daddr,
1009 (unsigned int)sup->su_nbytes,
1010 sp->ndupino);
1011 panic("lfs_writeinode: negative bytes");
1012 sup->su_nbytes = sizeof (struct ufs1_dinode);
1013 }
1014 #endif
1015 #ifdef DEBUG_SU_NBYTES
1016 printf("seg %d -= %d for ino %d inode\n",
1017 dtosn(fs, daddr), sizeof (struct ufs1_dinode), ino);
1018 #endif
1019 sup->su_nbytes -= sizeof (struct ufs1_dinode);
1020 redo_ifile =
1021 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
1022 if (redo_ifile)
1023 fs->lfs_flags |= LFS_IFDIRTY;
1024 LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */
1025 }
1026 return (redo_ifile);
1027 }
1028
1029 int
1030 lfs_gatherblock(struct segment *sp, struct buf *bp, int *sptr)
1031 {
1032 struct lfs *fs;
1033 int version;
1034 int j, blksinblk;
1035
1036 /*
1037 * If full, finish this segment. We may be doing I/O, so
1038 * release and reacquire the splbio().
1039 */
1040 #ifdef DIAGNOSTIC
1041 if (sp->vp == NULL)
1042 panic ("lfs_gatherblock: Null vp in segment");
1043 #endif
1044 fs = sp->fs;
1045 blksinblk = howmany(bp->b_bcount, fs->lfs_bsize);
1046 if (sp->sum_bytes_left < sizeof(int32_t) * blksinblk ||
1047 sp->seg_bytes_left < bp->b_bcount) {
1048 if (sptr)
1049 splx(*sptr);
1050 lfs_updatemeta(sp);
1051
1052 version = sp->fip->fi_version;
1053 (void) lfs_writeseg(fs, sp);
1054
1055 sp->fip->fi_version = version;
1056 sp->fip->fi_ino = VTOI(sp->vp)->i_number;
1057 /* Add the current file to the segment summary. */
1058 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
1059 sp->sum_bytes_left -= FINFOSIZE;
1060
1061 if (sptr)
1062 *sptr = splbio();
1063 return (1);
1064 }
1065
1066 #ifdef DEBUG
1067 if (bp->b_flags & B_GATHERED) {
1068 printf("lfs_gatherblock: already gathered! Ino %d,"
1069 " lbn %" PRId64 "\n",
1070 sp->fip->fi_ino, bp->b_lblkno);
1071 return (0);
1072 }
1073 #endif
1074 /* Insert into the buffer list, update the FINFO block. */
1075 bp->b_flags |= B_GATHERED;
1076
1077 *sp->cbpp++ = bp;
1078 for (j = 0; j < blksinblk; j++)
1079 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno + j;
1080
1081 sp->sum_bytes_left -= sizeof(int32_t) * blksinblk;
1082 sp->seg_bytes_left -= bp->b_bcount;
1083 return (0);
1084 }
1085
1086 int
1087 lfs_gather(struct lfs *fs, struct segment *sp, struct vnode *vp,
1088 int (*match)(struct lfs *, struct buf *))
1089 {
1090 struct buf *bp, *nbp;
1091 int s, count = 0;
1092
1093 sp->vp = vp;
1094 s = splbio();
1095
1096 #ifndef LFS_NO_BACKBUF_HACK
1097 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
1098 # define BUF_OFFSET \
1099 (((caddr_t)&LIST_NEXT(bp, b_vnbufs)) - (caddr_t)bp)
1100 # define BACK_BUF(BP) \
1101 ((struct buf *)(((caddr_t)(BP)->b_vnbufs.le_prev) - BUF_OFFSET))
1102 # define BEG_OF_LIST \
1103 ((struct buf *)(((caddr_t)&LIST_FIRST(&vp->v_dirtyblkhd)) - BUF_OFFSET))
1104
1105 loop:
1106 /* Find last buffer. */
1107 for (bp = LIST_FIRST(&vp->v_dirtyblkhd);
1108 bp && LIST_NEXT(bp, b_vnbufs) != NULL;
1109 bp = LIST_NEXT(bp, b_vnbufs))
1110 /* nothing */;
1111 for (; bp && bp != BEG_OF_LIST; bp = nbp) {
1112 nbp = BACK_BUF(bp);
1113 #else /* LFS_NO_BACKBUF_HACK */
1114 loop:
1115 for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1116 nbp = LIST_NEXT(bp, b_vnbufs);
1117 #endif /* LFS_NO_BACKBUF_HACK */
1118 if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp)) {
1119 #ifdef DEBUG_LFS
1120 if (vp == fs->lfs_ivnode &&
1121 (bp->b_flags & (B_BUSY|B_GATHERED)) == B_BUSY)
1122 printf("(%" PRId64 ":%lx)",
1123 bp->b_lblkno, bp->b_flags);
1124 #endif
1125 continue;
1126 }
1127 if (vp->v_type == VBLK) {
1128 /* For block devices, just write the blocks. */
1129 /* XXX Do we really need to even do this? */
1130 #ifdef DEBUG_LFS
1131 if (count == 0)
1132 printf("BLK(");
1133 printf(".");
1134 #endif
1135 /*
1136 * Get the block before bwrite,
1137 * so we don't corrupt the free list
1138 */
1139 bp->b_flags |= B_BUSY;
1140 bremfree(bp);
1141 bwrite(bp);
1142 } else {
1143 #ifdef DIAGNOSTIC
1144 # ifdef LFS_USE_B_INVAL
1145 if ((bp->b_flags & (B_CALL|B_INVAL)) == B_INVAL) {
1146 printf("lfs_gather: lbn %" PRId64 " is "
1147 "B_INVAL\n", bp->b_lblkno);
1148 VOP_PRINT(bp->b_vp);
1149 }
1150 # endif /* LFS_USE_B_INVAL */
1151 if (!(bp->b_flags & B_DELWRI))
1152 panic("lfs_gather: bp not B_DELWRI");
1153 if (!(bp->b_flags & B_LOCKED)) {
1154 printf("lfs_gather: lbn %" PRId64 " blk "
1155 "%" PRId64 " not B_LOCKED\n",
1156 bp->b_lblkno,
1157 dbtofsb(fs, bp->b_blkno));
1158 VOP_PRINT(bp->b_vp);
1159 panic("lfs_gather: bp not B_LOCKED");
1160 }
1161 #endif
1162 if (lfs_gatherblock(sp, bp, &s)) {
1163 goto loop;
1164 }
1165 }
1166 count++;
1167 }
1168 splx(s);
1169 #ifdef DEBUG_LFS
1170 if (vp->v_type == VBLK && count)
1171 printf(")\n");
1172 #endif
1173 lfs_updatemeta(sp);
1174 sp->vp = NULL;
1175 return count;
1176 }
1177
1178 #if DEBUG
1179 # define DEBUG_OOFF(n) do { \
1180 if (ooff == 0) { \
1181 printf("lfs_updatemeta[%d]: warning: writing " \
1182 "ino %d lbn %" PRId64 " at 0x%" PRIx32 \
1183 ", was 0x0 (or %" PRId64 ")\n", \
1184 (n), ip->i_number, lbn, ndaddr, daddr); \
1185 } \
1186 } while (0)
1187 #else
1188 # define DEBUG_OOFF(n)
1189 #endif
1190
1191 /*
1192 * Change the given block's address to ndaddr, finding its previous
1193 * location using ufs_bmaparray().
1194 *
1195 * Account for this change in the segment table.
1196 */
1197 void
1198 lfs_update_single(struct lfs *fs, struct segment *sp, daddr_t lbn,
1199 int32_t ndaddr, int size)
1200 {
1201 SEGUSE *sup;
1202 struct buf *bp;
1203 struct indir a[NIADDR + 2], *ap;
1204 struct inode *ip;
1205 struct vnode *vp;
1206 daddr_t daddr, ooff;
1207 int num, error;
1208 int bb, osize, obb;
1209
1210 vp = sp->vp;
1211 ip = VTOI(vp);
1212
1213 error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL, NULL);
1214 if (error)
1215 panic("lfs_updatemeta: ufs_bmaparray returned %d", error);
1216
1217 KASSERT(daddr <= LFS_MAX_DADDR);
1218 if (daddr > 0)
1219 daddr = dbtofsb(fs, daddr);
1220
1221 bb = fragstofsb(fs, numfrags(fs, size));
1222 switch (num) {
1223 case 0:
1224 ooff = ip->i_ffs1_db[lbn];
1225 DEBUG_OOFF(0);
1226 if (ooff == UNWRITTEN)
1227 ip->i_ffs1_blocks += bb;
1228 else {
1229 /* possible fragment truncation or extension */
1230 obb = btofsb(fs, ip->i_lfs_fragsize[lbn]);
1231 ip->i_ffs1_blocks += (bb - obb);
1232 }
1233 ip->i_ffs1_db[lbn] = ndaddr;
1234 break;
1235 case 1:
1236 ooff = ip->i_ffs1_ib[a[0].in_off];
1237 DEBUG_OOFF(1);
1238 if (ooff == UNWRITTEN)
1239 ip->i_ffs1_blocks += bb;
1240 ip->i_ffs1_ib[a[0].in_off] = ndaddr;
1241 break;
1242 default:
1243 ap = &a[num - 1];
1244 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
1245 panic("lfs_updatemeta: bread bno %" PRId64,
1246 ap->in_lbn);
1247
1248 /* XXX ondisk32 */
1249 ooff = ((int32_t *)bp->b_data)[ap->in_off];
1250 DEBUG_OOFF(num);
1251 if (ooff == UNWRITTEN)
1252 ip->i_ffs1_blocks += bb;
1253 /* XXX ondisk32 */
1254 ((int32_t *)bp->b_data)[ap->in_off] = ndaddr;
1255 (void) VOP_BWRITE(bp);
1256 }
1257
1258 /*
1259 * Though we'd rather it couldn't, this *can* happen right now
1260 * if cleaning blocks and regular blocks coexist.
1261 */
1262 /* KASSERT(daddr < fs->lfs_lastpseg || daddr > ndaddr); */
1263
1264 /*
1265 * Update segment usage information, based on old size
1266 * and location.
1267 */
1268 if (daddr > 0) {
1269 u_int32_t oldsn = dtosn(fs, daddr);
1270 #ifdef DIAGNOSTIC
1271 int ndupino = (sp->seg_number == oldsn) ?
1272 sp->ndupino : 0;
1273 #endif
1274 KASSERT(oldsn >= 0 && oldsn < fs->lfs_nseg);
1275 if (lbn >= 0 && lbn < NDADDR)
1276 osize = ip->i_lfs_fragsize[lbn];
1277 else
1278 osize = fs->lfs_bsize;
1279 LFS_SEGENTRY(sup, fs, oldsn, bp);
1280 #ifdef DIAGNOSTIC
1281 if (sup->su_nbytes + sizeof (struct ufs1_dinode) * ndupino
1282 < osize) {
1283 printf("lfs_updatemeta: negative bytes "
1284 "(segment %" PRIu32 " short by %" PRId64
1285 ")\n", dtosn(fs, daddr),
1286 (int64_t)osize -
1287 (sizeof (struct ufs1_dinode) * sp->ndupino +
1288 sup->su_nbytes));
1289 printf("lfs_updatemeta: ino %d, lbn %" PRId64
1290 ", addr = 0x%" PRIx64 "\n",
1291 VTOI(sp->vp)->i_number, lbn, daddr);
1292 printf("lfs_updatemeta: ndupino=%d\n", ndupino);
1293 panic("lfs_updatemeta: negative bytes");
1294 sup->su_nbytes = osize -
1295 sizeof (struct ufs1_dinode) * sp->ndupino;
1296 }
1297 #endif
1298 #ifdef DEBUG_SU_NBYTES
1299 printf("seg %" PRIu32 " -= %d for ino %d lbn %" PRId64
1300 " db 0x%" PRIx64 "\n",
1301 dtosn(fs, daddr), osize,
1302 VTOI(sp->vp)->i_number, lbn, daddr);
1303 #endif
1304 sup->su_nbytes -= osize;
1305 if (!(bp->b_flags & B_GATHERED))
1306 fs->lfs_flags |= LFS_IFDIRTY;
1307 LFS_WRITESEGENTRY(sup, fs, oldsn, bp);
1308 }
1309 /*
1310 * Now that this block has a new address, and its old
1311 * segment no longer owns it, we can forget about its
1312 * old size.
1313 */
1314 if (lbn >= 0 && lbn < NDADDR)
1315 ip->i_lfs_fragsize[lbn] = size;
1316 }
1317
1318 /*
1319 * Update the metadata that points to the blocks listed in the FINFO
1320 * array.
1321 */
1322 void
1323 lfs_updatemeta(struct segment *sp)
1324 {
1325 struct buf *sbp;
1326 struct lfs *fs;
1327 struct vnode *vp;
1328 daddr_t lbn;
1329 int i, nblocks, num;
1330 int bb;
1331 int bytesleft, size;
1332
1333 vp = sp->vp;
1334 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
1335 KASSERT(nblocks >= 0);
1336 if (vp == NULL || nblocks == 0)
1337 return;
1338
1339 /*
1340 * This count may be high due to oversize blocks from lfs_gop_write.
1341 * Correct for this. (XXX we should be able to keep track of these.)
1342 */
1343 fs = sp->fs;
1344 for (i = 0; i < nblocks; i++) {
1345 if (sp->start_bpp[i] == NULL) {
1346 printf("nblocks = %d, not %d\n", i, nblocks);
1347 nblocks = i;
1348 break;
1349 }
1350 num = howmany(sp->start_bpp[i]->b_bcount, fs->lfs_bsize);
1351 KASSERT(sp->start_bpp[i]->b_lblkno >= 0 || num == 1);
1352 nblocks -= num - 1;
1353 }
1354
1355 KASSERT(vp->v_type == VREG ||
1356 nblocks == &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp);
1357 KASSERT(nblocks == sp->cbpp - sp->start_bpp);
1358
1359 /*
1360 * Sort the blocks.
1361 *
1362 * We have to sort even if the blocks come from the
1363 * cleaner, because there might be other pending blocks on the
1364 * same inode...and if we don't sort, and there are fragments
1365 * present, blocks may be written in the wrong place.
1366 */
1367 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks, fs->lfs_bsize);
1368
1369 /*
1370 * Record the length of the last block in case it's a fragment.
1371 * If there are indirect blocks present, they sort last. An
1372 * indirect block will be lfs_bsize and its presence indicates
1373 * that you cannot have fragments.
1374 *
1375 * XXX This last is a lie. A cleaned fragment can coexist with
1376 * XXX a later indirect block. This will continue to be
1377 * XXX true until lfs_markv is fixed to do everything with
1378 * XXX fake blocks (including fake inodes and fake indirect blocks).
1379 */
1380 sp->fip->fi_lastlength = ((sp->start_bpp[nblocks - 1]->b_bcount - 1) &
1381 fs->lfs_bmask) + 1;
1382
1383 /*
1384 * Assign disk addresses, and update references to the logical
1385 * block and the segment usage information.
1386 */
1387 for (i = nblocks; i--; ++sp->start_bpp) {
1388 sbp = *sp->start_bpp;
1389 lbn = *sp->start_lbp;
1390 KASSERT(sbp->b_lblkno == lbn);
1391
1392 sbp->b_blkno = fsbtodb(fs, fs->lfs_offset);
1393
1394 /*
1395 * If we write a frag in the wrong place, the cleaner won't
1396 * be able to correctly identify its size later, and the
1397 * segment will be uncleanable. (Even worse, it will assume
1398 * that the indirect block that actually ends the list
1399 * is of a smaller size!)
1400 */
1401 if ((sbp->b_bcount & fs->lfs_bmask) && i != 0)
1402 panic("lfs_updatemeta: fragment is not last block");
1403
1404 /*
1405 * For each subblock in this possibly oversized block,
1406 * update its address on disk.
1407 */
1408 KASSERT(lbn >= 0 || sbp->b_bcount == fs->lfs_bsize);
1409 for (bytesleft = sbp->b_bcount; bytesleft > 0;
1410 bytesleft -= fs->lfs_bsize) {
1411 size = MIN(bytesleft, fs->lfs_bsize);
1412 bb = fragstofsb(fs, numfrags(fs, size));
1413 lbn = *sp->start_lbp++;
1414 lfs_update_single(fs, sp, lbn, fs->lfs_offset, size);
1415 fs->lfs_offset += bb;
1416 }
1417
1418 }
1419 }
1420
1421 /*
1422 * Start a new segment.
1423 */
1424 int
1425 lfs_initseg(struct lfs *fs)
1426 {
1427 struct segment *sp;
1428 SEGUSE *sup;
1429 SEGSUM *ssp;
1430 struct buf *bp, *sbp;
1431 int repeat;
1432
1433 sp = fs->lfs_sp;
1434
1435 repeat = 0;
1436
1437 /* Advance to the next segment. */
1438 if (!LFS_PARTIAL_FITS(fs)) {
1439 /* lfs_avail eats the remaining space */
1440 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset -
1441 fs->lfs_curseg);
1442 /* Wake up any cleaning procs waiting on this file system. */
1443 wakeup(&lfs_allclean_wakeup);
1444 wakeup(&fs->lfs_nextseg);
1445 lfs_newseg(fs);
1446 repeat = 1;
1447 fs->lfs_offset = fs->lfs_curseg;
1448
1449 sp->seg_number = dtosn(fs, fs->lfs_curseg);
1450 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg);
1451
1452 /*
1453 * If the segment contains a superblock, update the offset
1454 * and summary address to skip over it.
1455 */
1456 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1457 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
1458 fs->lfs_offset += btofsb(fs, LFS_SBPAD);
1459 sp->seg_bytes_left -= LFS_SBPAD;
1460 }
1461 brelse(bp);
1462 /* Segment zero could also contain the labelpad */
1463 if (fs->lfs_version > 1 && sp->seg_number == 0 &&
1464 fs->lfs_start < btofsb(fs, LFS_LABELPAD)) {
1465 fs->lfs_offset +=
1466 btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
1467 sp->seg_bytes_left -=
1468 LFS_LABELPAD - fsbtob(fs, fs->lfs_start);
1469 }
1470 } else {
1471 sp->seg_number = dtosn(fs, fs->lfs_curseg);
1472 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg -
1473 (fs->lfs_offset - fs->lfs_curseg));
1474 }
1475 fs->lfs_lastpseg = fs->lfs_offset;
1476
1477 /* Record first address of this partial segment */
1478 if (sp->seg_flags & SEGM_CLEAN) {
1479 fs->lfs_cleanint[fs->lfs_cleanind] = fs->lfs_offset;
1480 if (++fs->lfs_cleanind >= LFS_MAX_CLEANIND) {
1481 /* "1" is the artificial inc in lfs_seglock */
1482 while (fs->lfs_iocount > 1) {
1483 tsleep(&fs->lfs_iocount, PRIBIO + 1,
1484 "lfs_initseg", 0);
1485 }
1486 fs->lfs_cleanind = 0;
1487 }
1488 }
1489
1490 sp->fs = fs;
1491 sp->ibp = NULL;
1492 sp->idp = NULL;
1493 sp->ninodes = 0;
1494 sp->ndupino = 0;
1495
1496 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
1497 sp->cbpp = sp->bpp;
1498 sbp = *sp->cbpp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp,
1499 fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize, LFS_NB_SUMMARY);
1500 sp->segsum = (*sp->cbpp)->b_data;
1501 memset(sp->segsum, 0, fs->lfs_sumsize);
1502 sp->start_bpp = ++sp->cbpp;
1503 fs->lfs_offset += btofsb(fs, fs->lfs_sumsize);
1504
1505 /* Set point to SEGSUM, initialize it. */
1506 ssp = sp->segsum;
1507 ssp->ss_next = fs->lfs_nextseg;
1508 ssp->ss_nfinfo = ssp->ss_ninos = 0;
1509 ssp->ss_magic = SS_MAGIC;
1510
1511 /* Set pointer to first FINFO, initialize it. */
1512 sp->fip = (struct finfo *)((caddr_t)sp->segsum + SEGSUM_SIZE(fs));
1513 sp->fip->fi_nblocks = 0;
1514 sp->start_lbp = &sp->fip->fi_blocks[0];
1515 sp->fip->fi_lastlength = 0;
1516
1517 sp->seg_bytes_left -= fs->lfs_sumsize;
1518 sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs);
1519
1520 return (repeat);
1521 }
1522
1523 /*
1524 * Return the next segment to write.
1525 */
1526 void
1527 lfs_newseg(struct lfs *fs)
1528 {
1529 CLEANERINFO *cip;
1530 SEGUSE *sup;
1531 struct buf *bp;
1532 int curseg, isdirty, sn;
1533
1534 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
1535 #ifdef DEBUG_SU_NBYTES
1536 printf("lfs_newseg: seg %d := 0 in newseg\n", /* XXXDEBUG */
1537 dtosn(fs, fs->lfs_nextseg)); /* XXXDEBUG */
1538 #endif
1539 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1540 sup->su_nbytes = 0;
1541 sup->su_nsums = 0;
1542 sup->su_ninos = 0;
1543 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
1544
1545 LFS_CLEANERINFO(cip, fs, bp);
1546 --cip->clean;
1547 ++cip->dirty;
1548 fs->lfs_nclean = cip->clean;
1549 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1550
1551 fs->lfs_lastseg = fs->lfs_curseg;
1552 fs->lfs_curseg = fs->lfs_nextseg;
1553 for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) {
1554 sn = (sn + 1) % fs->lfs_nseg;
1555 if (sn == curseg)
1556 panic("lfs_nextseg: no clean segments");
1557 LFS_SEGENTRY(sup, fs, sn, bp);
1558 isdirty = sup->su_flags & SEGUSE_DIRTY;
1559 /* Check SEGUSE_EMPTY as we go along */
1560 if (isdirty && sup->su_nbytes == 0 &&
1561 !(sup->su_flags & SEGUSE_EMPTY))
1562 LFS_WRITESEGENTRY(sup, fs, sn, bp);
1563 else
1564 brelse(bp);
1565
1566 if (!isdirty)
1567 break;
1568 }
1569
1570 ++fs->lfs_nactive;
1571 fs->lfs_nextseg = sntod(fs, sn);
1572 if (lfs_dostats) {
1573 ++lfs_stats.segsused;
1574 }
1575 }
1576
1577 static struct buf *
1578 lfs_newclusterbuf(struct lfs *fs, struct vnode *vp, daddr_t addr, int n)
1579 {
1580 struct lfs_cluster *cl;
1581 struct buf **bpp, *bp;
1582 int s;
1583
1584 cl = (struct lfs_cluster *)pool_get(&fs->lfs_clpool, PR_WAITOK);
1585 bpp = (struct buf **)pool_get(&fs->lfs_bpppool, PR_WAITOK);
1586 memset(cl, 0, sizeof(*cl));
1587 cl->fs = fs;
1588 cl->bpp = bpp;
1589 cl->bufcount = 0;
1590 cl->bufsize = 0;
1591
1592 /* If this segment is being written synchronously, note that */
1593 if (fs->lfs_sp->seg_flags & SEGM_SYNC) {
1594 cl->flags |= LFS_CL_SYNC;
1595 cl->seg = fs->lfs_sp;
1596 ++cl->seg->seg_iocount;
1597 /* printf("+ %x => %d\n", cl->seg, cl->seg->seg_iocount); */
1598 }
1599
1600 /* Get an empty buffer header, or maybe one with something on it */
1601 s = splbio();
1602 bp = pool_get(&bufpool, PR_WAITOK); /* XXX should use lfs_malloc? */
1603 splx(s);
1604 memset(bp, 0, sizeof(*bp));
1605 BUF_INIT(bp);
1606
1607 bp->b_flags = B_BUSY | B_CALL;
1608 bp->b_dev = NODEV;
1609 bp->b_blkno = bp->b_lblkno = addr;
1610 bp->b_iodone = lfs_cluster_callback;
1611 bp->b_saveaddr = (caddr_t)cl;
1612 bp->b_vp = vp;
1613
1614 return bp;
1615 }
1616
1617 int
1618 lfs_writeseg(struct lfs *fs, struct segment *sp)
1619 {
1620 struct buf **bpp, *bp, *cbp, *newbp;
1621 SEGUSE *sup;
1622 SEGSUM *ssp;
1623 dev_t i_dev;
1624 char *datap, *dp;
1625 int i, s;
1626 int do_again, nblocks, byteoffset;
1627 size_t el_size;
1628 struct lfs_cluster *cl;
1629 int (*strategy)(void *);
1630 struct vop_strategy_args vop_strategy_a;
1631 u_short ninos;
1632 struct vnode *devvp;
1633 char *p;
1634 struct vnode *vp;
1635 int32_t *daddrp; /* XXX ondisk32 */
1636 int changed;
1637 #if defined(DEBUG) && defined(LFS_PROPELLER)
1638 static int propeller;
1639 char propstring[4] = "-\\|/";
1640
1641 printf("%c\b",propstring[propeller++]);
1642 if (propeller == 4)
1643 propeller = 0;
1644 #endif
1645
1646 /*
1647 * If there are no buffers other than the segment summary to write
1648 * and it is not a checkpoint, don't do anything. On a checkpoint,
1649 * even if there aren't any buffers, you need to write the superblock.
1650 */
1651 if ((nblocks = sp->cbpp - sp->bpp) == 1)
1652 return (0);
1653
1654 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
1655 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
1656
1657 /* Update the segment usage information. */
1658 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1659
1660 /* Loop through all blocks, except the segment summary. */
1661 for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
1662 if ((*bpp)->b_vp != devvp) {
1663 sup->su_nbytes += (*bpp)->b_bcount;
1664 #ifdef DEBUG_SU_NBYTES
1665 printf("seg %" PRIu32 " += %ld for ino %d lbn %" PRId64
1666 " db 0x%" PRIx64 "\n", sp->seg_number, (*bpp)->b_bcount,
1667 VTOI((*bpp)->b_vp)->i_number, (*bpp)->b_lblkno,
1668 (*bpp)->b_blkno);
1669 #endif
1670 }
1671 }
1672
1673 ssp = (SEGSUM *)sp->segsum;
1674
1675 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
1676 #ifdef DEBUG_SU_NBYTES
1677 printf("seg %d += %d for %d inodes\n", /* XXXDEBUG */
1678 sp->seg_number, ssp->ss_ninos * sizeof (struct ufs1_dinode),
1679 ssp->ss_ninos);
1680 #endif
1681 sup->su_nbytes += ssp->ss_ninos * sizeof (struct ufs1_dinode);
1682 /* sup->su_nbytes += fs->lfs_sumsize; */
1683 if (fs->lfs_version == 1)
1684 sup->su_olastmod = time.tv_sec;
1685 else
1686 sup->su_lastmod = time.tv_sec;
1687 sup->su_ninos += ninos;
1688 ++sup->su_nsums;
1689 fs->lfs_dmeta += (btofsb(fs, fs->lfs_sumsize) + btofsb(fs, ninos *
1690 fs->lfs_ibsize));
1691 fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize);
1692
1693 do_again = !(bp->b_flags & B_GATHERED);
1694 LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp); /* Ifile */
1695
1696 /*
1697 * Mark blocks B_BUSY, to prevent then from being changed between
1698 * the checksum computation and the actual write.
1699 *
1700 * If we are cleaning, check indirect blocks for UNWRITTEN, and if
1701 * there are any, replace them with copies that have UNASSIGNED
1702 * instead.
1703 */
1704 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
1705 ++bpp;
1706 bp = *bpp;
1707 if (bp->b_flags & B_CALL) { /* UBC or malloced buffer */
1708 bp->b_flags |= B_BUSY;
1709 continue;
1710 }
1711 again:
1712 s = splbio();
1713 if (bp->b_flags & B_BUSY) {
1714 #ifdef DEBUG
1715 printf("lfs_writeseg: avoiding potential data summary "
1716 "corruption for ino %d, lbn %" PRId64 "\n",
1717 VTOI(bp->b_vp)->i_number, bp->b_lblkno);
1718 #endif
1719 bp->b_flags |= B_WANTED;
1720 tsleep(bp, (PRIBIO + 1), "lfs_writeseg", 0);
1721 splx(s);
1722 goto again;
1723 }
1724 bp->b_flags |= B_BUSY;
1725 splx(s);
1726 /*
1727 * Check and replace indirect block UNWRITTEN bogosity.
1728 * XXX See comment in lfs_writefile.
1729 */
1730 if (bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp &&
1731 VTOI(bp->b_vp)->i_ffs1_blocks !=
1732 VTOI(bp->b_vp)->i_lfs_effnblks) {
1733 #ifdef DEBUG_LFS
1734 printf("lfs_writeseg: cleansing ino %d (%d != %d)\n",
1735 VTOI(bp->b_vp)->i_number,
1736 VTOI(bp->b_vp)->i_lfs_effnblks,
1737 VTOI(bp->b_vp)->i_ffs1_blocks);
1738 #endif
1739 /* Make a copy we'll make changes to */
1740 newbp = lfs_newbuf(fs, bp->b_vp, bp->b_lblkno,
1741 bp->b_bcount, LFS_NB_IBLOCK);
1742 newbp->b_blkno = bp->b_blkno;
1743 memcpy(newbp->b_data, bp->b_data,
1744 newbp->b_bcount);
1745
1746 changed = 0;
1747 /* XXX ondisk32 */
1748 for (daddrp = (int32_t *)(newbp->b_data);
1749 daddrp < (int32_t *)(newbp->b_data +
1750 newbp->b_bcount); daddrp++) {
1751 if (*daddrp == UNWRITTEN) {
1752 #ifdef DEBUG_LFS
1753 off_t doff;
1754 int32_t ioff;
1755
1756 ioff =
1757 daddrp - (int32_t *)(newbp->b_data);
1758 doff =
1759 (-bp->b_lblkno + ioff) * fs->lfs_bsize;
1760 printf("ino %d lbn %" PRId64
1761 " entry %d off %" PRIx64 "\n",
1762 VTOI(bp->b_vp)->i_number,
1763 bp->b_lblkno, ioff, doff);
1764 if (bp->b_vp->v_type == VREG) {
1765 /*
1766 * What is up with this page?
1767 */
1768 struct vm_page *pg;
1769 for (; doff / fs->lfs_bsize == (-bp->b_lblkno + ioff);
1770 doff += PAGE_SIZE) {
1771 pg = uvm_pagelookup(&bp->b_vp->v_uobj, doff);
1772 if (pg == NULL)
1773 printf(" page at %" PRIx64 " is NULL\n", doff);
1774 else
1775 printf(" page at %" PRIx64
1776 " flags 0x%x pqflags 0x%x\n",
1777 doff, pg->flags, pg->pqflags);
1778 }
1779 }
1780 #endif /* DEBUG_LFS */
1781 ++changed;
1782 *daddrp = 0;
1783 }
1784 }
1785 /*
1786 * Get rid of the old buffer. Don't mark it clean,
1787 * though, if it still has dirty data on it.
1788 */
1789 if (changed) {
1790 #ifdef DEBUG_LFS
1791 printf("lfs_writeseg: replacing UNWRITTEN(%d):"
1792 " bp = %p newbp = %p\n", changed, bp,
1793 newbp);
1794 #endif
1795 *bpp = newbp;
1796 bp->b_flags &= ~(B_ERROR | B_GATHERED);
1797 if (bp->b_flags & B_CALL) {
1798 printf("lfs_writeseg: "
1799 "indir bp should not be B_CALL\n");
1800 s = splbio();
1801 biodone(bp);
1802 splx(s);
1803 bp = NULL;
1804 } else {
1805 /* Still on free list, leave it there */
1806 s = splbio();
1807 bp->b_flags &= ~B_BUSY;
1808 if (bp->b_flags & B_WANTED)
1809 wakeup(bp);
1810 splx(s);
1811 /*
1812 * We have to re-decrement lfs_avail
1813 * since this block is going to come
1814 * back around to us in the next
1815 * segment.
1816 */
1817 fs->lfs_avail -=
1818 btofsb(fs, bp->b_bcount);
1819 }
1820 } else {
1821 lfs_freebuf(fs, newbp);
1822 }
1823 }
1824 }
1825 /*
1826 * Compute checksum across data and then across summary; the first
1827 * block (the summary block) is skipped. Set the create time here
1828 * so that it's guaranteed to be later than the inode mod times.
1829 *
1830 * XXX
1831 * Fix this to do it inline, instead of malloc/copy.
1832 */
1833 datap = dp = pool_get(&fs->lfs_bpppool, PR_WAITOK);
1834 if (fs->lfs_version == 1)
1835 el_size = sizeof(u_long);
1836 else
1837 el_size = sizeof(u_int32_t);
1838 for (bpp = sp->bpp, i = nblocks - 1; i--; ) {
1839 ++bpp;
1840 /* Loop through gop_write cluster blocks */
1841 for (byteoffset = 0; byteoffset < (*bpp)->b_bcount;
1842 byteoffset += fs->lfs_bsize) {
1843 #ifdef LFS_USE_B_INVAL
1844 if (((*bpp)->b_flags & (B_CALL | B_INVAL)) ==
1845 (B_CALL | B_INVAL)) {
1846 if (copyin((caddr_t)(*bpp)->b_saveaddr +
1847 byteoffset, dp, el_size)) {
1848 panic("lfs_writeseg: copyin failed [1]:"
1849 " ino %d blk %" PRId64,
1850 VTOI((*bpp)->b_vp)->i_number,
1851 (*bpp)->b_lblkno);
1852 }
1853 } else
1854 #endif /* LFS_USE_B_INVAL */
1855 {
1856 memcpy(dp, (*bpp)->b_data + byteoffset,
1857 el_size);
1858 }
1859 dp += el_size;
1860 }
1861 }
1862 if (fs->lfs_version == 1)
1863 ssp->ss_ocreate = time.tv_sec;
1864 else {
1865 ssp->ss_create = time.tv_sec;
1866 ssp->ss_serial = ++fs->lfs_serial;
1867 ssp->ss_ident = fs->lfs_ident;
1868 }
1869 ssp->ss_datasum = cksum(datap, dp - datap);
1870 ssp->ss_sumsum =
1871 cksum(&ssp->ss_datasum, fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
1872 pool_put(&fs->lfs_bpppool, datap);
1873 datap = dp = NULL;
1874 #ifdef DIAGNOSTIC
1875 if (fs->lfs_bfree <
1876 btofsb(fs, ninos * fs->lfs_ibsize) + btofsb(fs, fs->lfs_sumsize))
1877 panic("lfs_writeseg: No diskspace for summary");
1878 #endif
1879 fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) +
1880 btofsb(fs, fs->lfs_sumsize));
1881
1882 strategy = devvp->v_op[VOFFSET(vop_strategy)];
1883
1884 /*
1885 * When we simply write the blocks we lose a rotation for every block
1886 * written. To avoid this problem, we cluster the buffers into a
1887 * chunk and write the chunk. MAXPHYS is the largest size I/O
1888 * devices can handle, use that for the size of the chunks.
1889 *
1890 * Blocks that are already clusters (from GOP_WRITE), however, we
1891 * don't bother to copy into other clusters.
1892 */
1893
1894 #define CHUNKSIZE MAXPHYS
1895
1896 if (devvp == NULL)
1897 panic("devvp is NULL");
1898 for (bpp = sp->bpp, i = nblocks; i;) {
1899 cbp = lfs_newclusterbuf(fs, devvp, (*bpp)->b_blkno, i);
1900 cl = (struct lfs_cluster *)cbp->b_saveaddr;
1901
1902 cbp->b_dev = i_dev;
1903 cbp->b_flags |= B_ASYNC | B_BUSY;
1904 cbp->b_bcount = 0;
1905
1906 #if defined(DEBUG) && defined(DIAGNOSTIC)
1907 if (bpp - sp->bpp > (fs->lfs_sumsize - SEGSUM_SIZE(fs))
1908 / sizeof(int32_t)) {
1909 panic("lfs_writeseg: real bpp overwrite");
1910 }
1911 if (bpp - sp->bpp > fs->lfs_ssize / fs->lfs_fsize) {
1912 panic("lfs_writeseg: theoretical bpp overwrite");
1913 }
1914 #endif
1915
1916 /*
1917 * Construct the cluster.
1918 */
1919 ++fs->lfs_iocount;
1920 while (i && cbp->b_bcount < CHUNKSIZE) {
1921 bp = *bpp;
1922
1923 if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
1924 break;
1925 if (cbp->b_bcount > 0 && !(cl->flags & LFS_CL_MALLOC))
1926 break;
1927
1928 /* Clusters from GOP_WRITE are expedited */
1929 if (bp->b_bcount > fs->lfs_bsize) {
1930 if (cbp->b_bcount > 0)
1931 /* Put in its own buffer */
1932 break;
1933 else {
1934 cbp->b_data = bp->b_data;
1935 }
1936 } else if (cbp->b_bcount == 0) {
1937 p = cbp->b_data = lfs_malloc(fs, CHUNKSIZE,
1938 LFS_NB_CLUSTER);
1939 cl->flags |= LFS_CL_MALLOC;
1940 }
1941 #ifdef DIAGNOSTIC
1942 if (dtosn(fs, dbtofsb(fs, bp->b_blkno +
1943 btodb(bp->b_bcount - 1))) !=
1944 sp->seg_number) {
1945 printf("blk size %ld daddr %" PRIx64
1946 " not in seg %d\n",
1947 bp->b_bcount, bp->b_blkno,
1948 sp->seg_number);
1949 panic("segment overwrite");
1950 }
1951 #endif
1952
1953 #ifdef LFS_USE_B_INVAL
1954 /*
1955 * Fake buffers from the cleaner are marked as B_INVAL.
1956 * We need to copy the data from user space rather than
1957 * from the buffer indicated.
1958 * XXX == what do I do on an error?
1959 */
1960 if ((bp->b_flags & (B_CALL|B_INVAL)) ==
1961 (B_CALL|B_INVAL)) {
1962 if (copyin(bp->b_saveaddr, p, bp->b_bcount))
1963 panic("lfs_writeseg: "
1964 "copyin failed [2]");
1965 } else
1966 #endif /* LFS_USE_B_INVAL */
1967 if (cl->flags & LFS_CL_MALLOC) {
1968 bcopy(bp->b_data, p, bp->b_bcount);
1969 }
1970
1971 p += bp->b_bcount;
1972 cbp->b_bcount += bp->b_bcount;
1973 cl->bufsize += bp->b_bcount;
1974
1975 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI | B_DONE);
1976 cl->bpp[cl->bufcount++] = bp;
1977 vp = bp->b_vp;
1978 s = splbio();
1979 reassignbuf(bp, vp);
1980 V_INCR_NUMOUTPUT(vp);
1981 splx(s);
1982
1983 bpp++;
1984 i--;
1985 }
1986 s = splbio();
1987 V_INCR_NUMOUTPUT(devvp);
1988 splx(s);
1989 vop_strategy_a.a_desc = VDESC(vop_strategy);
1990 vop_strategy_a.a_bp = cbp;
1991 (strategy)(&vop_strategy_a);
1992 curproc->p_stats->p_ru.ru_oublock++;
1993 }
1994
1995 if (lfs_dostats) {
1996 ++lfs_stats.psegwrites;
1997 lfs_stats.blocktot += nblocks - 1;
1998 if (fs->lfs_sp->seg_flags & SEGM_SYNC)
1999 ++lfs_stats.psyncwrites;
2000 if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
2001 ++lfs_stats.pcleanwrites;
2002 lfs_stats.cleanblocks += nblocks - 1;
2003 }
2004 }
2005 return (lfs_initseg(fs) || do_again);
2006 }
2007
2008 void
2009 lfs_writesuper(struct lfs *fs, daddr_t daddr)
2010 {
2011 struct buf *bp;
2012 dev_t i_dev;
2013 int (*strategy)(void *);
2014 int s;
2015 struct vop_strategy_args vop_strategy_a;
2016
2017 /*
2018 * If we can write one superblock while another is in
2019 * progress, we risk not having a complete checkpoint if we crash.
2020 * So, block here if a superblock write is in progress.
2021 */
2022 s = splbio();
2023 while (fs->lfs_sbactive) {
2024 tsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0);
2025 }
2026 fs->lfs_sbactive = daddr;
2027 splx(s);
2028 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
2029 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
2030
2031 /* Set timestamp of this version of the superblock */
2032 if (fs->lfs_version == 1)
2033 fs->lfs_otstamp = time.tv_sec;
2034 fs->lfs_tstamp = time.tv_sec;
2035
2036 /* Checksum the superblock and copy it into a buffer. */
2037 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
2038 bp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp,
2039 fsbtodb(fs, daddr), LFS_SBPAD, LFS_NB_SBLOCK);
2040 memset(bp->b_data + sizeof(struct dlfs), 0,
2041 LFS_SBPAD - sizeof(struct dlfs));
2042 *(struct dlfs *)bp->b_data = fs->lfs_dlfs;
2043
2044 bp->b_dev = i_dev;
2045 bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
2046 bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
2047 bp->b_iodone = lfs_supercallback;
2048 /* XXX KS - same nasty hack as above */
2049 bp->b_saveaddr = (caddr_t)fs;
2050
2051 vop_strategy_a.a_desc = VDESC(vop_strategy);
2052 vop_strategy_a.a_bp = bp;
2053 curproc->p_stats->p_ru.ru_oublock++;
2054 s = splbio();
2055 V_INCR_NUMOUTPUT(bp->b_vp);
2056 splx(s);
2057 ++fs->lfs_iocount;
2058 (strategy)(&vop_strategy_a);
2059 }
2060
2061 /*
2062 * Logical block number match routines used when traversing the dirty block
2063 * chain.
2064 */
2065 int
2066 lfs_match_fake(struct lfs *fs, struct buf *bp)
2067 {
2068
2069 return LFS_IS_MALLOC_BUF(bp);
2070 }
2071
2072 #if 0
2073 int
2074 lfs_match_real(struct lfs *fs, struct buf *bp)
2075 {
2076
2077 return (lfs_match_data(fs, bp) && !lfs_match_fake(fs, bp));
2078 }
2079 #endif
2080
2081 int
2082 lfs_match_data(struct lfs *fs, struct buf *bp)
2083 {
2084
2085 return (bp->b_lblkno >= 0);
2086 }
2087
2088 int
2089 lfs_match_indir(struct lfs *fs, struct buf *bp)
2090 {
2091 daddr_t lbn;
2092
2093 lbn = bp->b_lblkno;
2094 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
2095 }
2096
2097 int
2098 lfs_match_dindir(struct lfs *fs, struct buf *bp)
2099 {
2100 daddr_t lbn;
2101
2102 lbn = bp->b_lblkno;
2103 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
2104 }
2105
2106 int
2107 lfs_match_tindir(struct lfs *fs, struct buf *bp)
2108 {
2109 daddr_t lbn;
2110
2111 lbn = bp->b_lblkno;
2112 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
2113 }
2114
2115 /*
2116 * XXX - The only buffers that are going to hit these functions are the
2117 * segment write blocks, or the segment summaries, or the superblocks.
2118 *
2119 * All of the above are created by lfs_newbuf, and so do not need to be
2120 * released via brelse.
2121 */
2122 void
2123 lfs_callback(struct buf *bp)
2124 {
2125 struct lfs *fs;
2126
2127 fs = (struct lfs *)bp->b_saveaddr;
2128 lfs_freebuf(fs, bp);
2129 }
2130
2131 static void
2132 lfs_super_aiodone(struct buf *bp)
2133 {
2134 struct lfs *fs;
2135
2136 fs = (struct lfs *)bp->b_saveaddr;
2137 fs->lfs_sbactive = 0;
2138 wakeup(&fs->lfs_sbactive);
2139 if (--fs->lfs_iocount <= 1)
2140 wakeup(&fs->lfs_iocount);
2141 lfs_freebuf(fs, bp);
2142 }
2143
2144 static void
2145 lfs_cluster_aiodone(struct buf *bp)
2146 {
2147 struct lfs_cluster *cl;
2148 struct lfs *fs;
2149 struct buf *tbp, *fbp;
2150 struct vnode *vp, *devvp;
2151 struct inode *ip;
2152 int s, error=0;
2153 char *cp;
2154
2155 if (bp->b_flags & B_ERROR)
2156 error = bp->b_error;
2157
2158 cl = (struct lfs_cluster *)bp->b_saveaddr;
2159 fs = cl->fs;
2160 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
2161
2162 cp = (char *)bp->b_data + cl->bufsize;
2163 /* Put the pages back, and release the buffer */
2164 while (cl->bufcount--) {
2165 tbp = cl->bpp[cl->bufcount];
2166 if (error) {
2167 tbp->b_flags |= B_ERROR;
2168 tbp->b_error = error;
2169 }
2170
2171 /*
2172 * We're done with tbp. If it has not been re-dirtied since
2173 * the cluster was written, free it. Otherwise, keep it on
2174 * the locked list to be written again.
2175 */
2176 vp = tbp->b_vp;
2177
2178 tbp->b_flags &= ~B_GATHERED;
2179
2180 LFS_BCLEAN_LOG(fs, tbp);
2181
2182 if (!(tbp->b_flags & B_CALL)) {
2183 KASSERT(tbp->b_flags & B_LOCKED);
2184 s = splbio();
2185 simple_lock(&bqueue_slock);
2186 bremfree(tbp);
2187 simple_unlock(&bqueue_slock);
2188 if (vp)
2189 reassignbuf(tbp, vp);
2190 splx(s);
2191 tbp->b_flags |= B_ASYNC; /* for biodone */
2192 }
2193
2194 if ((tbp->b_flags & (B_LOCKED | B_DELWRI)) == B_LOCKED)
2195 LFS_UNLOCK_BUF(tbp);
2196
2197 #ifdef DIAGNOSTIC
2198 if (tbp->b_flags & B_DONE) {
2199 printf("blk %d biodone already (flags %lx)\n",
2200 cl->bufcount, (long)tbp->b_flags);
2201 }
2202 #endif
2203 if (tbp->b_flags & (B_BUSY | B_CALL)) {
2204 if ((tbp->b_flags & B_CALL) &&
2205 !LFS_IS_MALLOC_BUF(tbp)) {
2206 /* printf("flags 0x%lx\n", tbp->b_flags); */
2207 /*
2208 * A buffer from the page daemon.
2209 * We use the same iodone as it does,
2210 * so we must manually disassociate its
2211 * buffers from the vp.
2212 */
2213 if (tbp->b_vp) {
2214 /* This is just silly */
2215 s = splbio();
2216 brelvp(tbp);
2217 tbp->b_vp = vp;
2218 splx(s);
2219 }
2220 /* Put it back the way it was */
2221 tbp->b_flags |= B_ASYNC;
2222 /* Master buffers have B_AGE */
2223 if (tbp->b_private == tbp)
2224 tbp->b_flags |= B_AGE;
2225 }
2226 s = splbio();
2227 biodone(tbp);
2228
2229 /*
2230 * If this is the last block for this vnode, but
2231 * there are other blocks on its dirty list,
2232 * set IN_MODIFIED/IN_CLEANING depending on what
2233 * sort of block. Only do this for our mount point,
2234 * not for, e.g., inode blocks that are attached to
2235 * the devvp.
2236 * XXX KS - Shouldn't we set *both* if both types
2237 * of blocks are present (traverse the dirty list?)
2238 */
2239 simple_lock(&global_v_numoutput_slock);
2240 if (vp != devvp && vp->v_numoutput == 0 &&
2241 (fbp = LIST_FIRST(&vp->v_dirtyblkhd)) != NULL) {
2242 ip = VTOI(vp);
2243 #ifdef DEBUG_LFS
2244 printf("lfs_cluster_aiodone: marking ino %d\n",
2245 ip->i_number);
2246 #endif
2247 if (LFS_IS_MALLOC_BUF(fbp))
2248 LFS_SET_UINO(ip, IN_CLEANING);
2249 else
2250 LFS_SET_UINO(ip, IN_MODIFIED);
2251 }
2252 simple_unlock(&global_v_numoutput_slock);
2253 splx(s);
2254 wakeup(vp);
2255 }
2256 }
2257
2258 /* Fix up the cluster buffer, and release it */
2259 if (cl->flags & LFS_CL_MALLOC)
2260 lfs_free(fs, bp->b_data, LFS_NB_CLUSTER);
2261 s = splbio();
2262 pool_put(&bufpool, bp); /* XXX should use lfs_free? */
2263 splx(s);
2264
2265 /* Note i/o done */
2266 if (cl->flags & LFS_CL_SYNC) {
2267 if (--cl->seg->seg_iocount == 0)
2268 wakeup(&cl->seg->seg_iocount);
2269 /* printf("- %x => %d\n", cl->seg, cl->seg->seg_iocount); */
2270 }
2271 #ifdef DIAGNOSTIC
2272 if (fs->lfs_iocount == 0)
2273 panic("lfs_cluster_aiodone: zero iocount");
2274 #endif
2275 if (--fs->lfs_iocount <= 1)
2276 wakeup(&fs->lfs_iocount);
2277
2278 pool_put(&fs->lfs_bpppool, cl->bpp);
2279 cl->bpp = NULL;
2280 pool_put(&fs->lfs_clpool, cl);
2281 }
2282
2283 static void
2284 lfs_generic_callback(struct buf *bp, void (*aiodone)(struct buf *))
2285 {
2286 /* reset b_iodone for when this is a single-buf i/o. */
2287 bp->b_iodone = aiodone;
2288
2289 simple_lock(&uvm.aiodoned_lock); /* locks uvm.aio_done */
2290 TAILQ_INSERT_TAIL(&uvm.aio_done, bp, b_freelist);
2291 wakeup(&uvm.aiodoned);
2292 simple_unlock(&uvm.aiodoned_lock);
2293 }
2294
2295 static void
2296 lfs_cluster_callback(struct buf *bp)
2297 {
2298
2299 lfs_generic_callback(bp, lfs_cluster_aiodone);
2300 }
2301
2302 void
2303 lfs_supercallback(struct buf *bp)
2304 {
2305
2306 lfs_generic_callback(bp, lfs_super_aiodone);
2307 }
2308
2309 /*
2310 * Shellsort (diminishing increment sort) from Data Structures and
2311 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
2312 * see also Knuth Vol. 3, page 84. The increments are selected from
2313 * formula (8), page 95. Roughly O(N^3/2).
2314 */
2315 /*
2316 * This is our own private copy of shellsort because we want to sort
2317 * two parallel arrays (the array of buffer pointers and the array of
2318 * logical block numbers) simultaneously. Note that we cast the array
2319 * of logical block numbers to a unsigned in this routine so that the
2320 * negative block numbers (meta data blocks) sort AFTER the data blocks.
2321 */
2322
2323 void
2324 lfs_shellsort(struct buf **bp_array, int32_t *lb_array, int nmemb, int size)
2325 {
2326 static int __rsshell_increments[] = { 4, 1, 0 };
2327 int incr, *incrp, t1, t2;
2328 struct buf *bp_temp;
2329
2330 #ifdef DEBUG
2331 incr = 0;
2332 for (t1 = 0; t1 < nmemb; t1++) {
2333 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
2334 if (lb_array[incr++] != bp_array[t1]->b_lblkno + t2) {
2335 /* dump before panic */
2336 printf("lfs_shellsort: nmemb=%d, size=%d\n",
2337 nmemb, size);
2338 incr = 0;
2339 for (t1 = 0; t1 < nmemb; t1++) {
2340 const struct buf *bp = bp_array[t1];
2341
2342 printf("bp[%d]: lbn=%" PRIu64 ", size=%"
2343 PRIu64 "\n", t1,
2344 (uint64_t)bp->b_bcount,
2345 (uint64_t)bp->b_lblkno);
2346 printf("lbns:");
2347 for (t2 = 0; t2 * size < bp->b_bcount;
2348 t2++) {
2349 printf(" %" PRId32,
2350 lb_array[incr++]);
2351 }
2352 printf("\n");
2353 }
2354 panic("lfs_shellsort: inconsistent input");
2355 }
2356 }
2357 }
2358 #endif
2359
2360 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
2361 for (t1 = incr; t1 < nmemb; ++t1)
2362 for (t2 = t1 - incr; t2 >= 0;)
2363 if ((u_int32_t)bp_array[t2]->b_lblkno >
2364 (u_int32_t)bp_array[t2 + incr]->b_lblkno) {
2365 bp_temp = bp_array[t2];
2366 bp_array[t2] = bp_array[t2 + incr];
2367 bp_array[t2 + incr] = bp_temp;
2368 t2 -= incr;
2369 } else
2370 break;
2371
2372 /* Reform the list of logical blocks */
2373 incr = 0;
2374 for (t1 = 0; t1 < nmemb; t1++) {
2375 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
2376 lb_array[incr++] = bp_array[t1]->b_lblkno + t2;
2377 }
2378 }
2379 }
2380
2381 /*
2382 * Check VXLOCK. Return 1 if the vnode is locked. Otherwise, vget it.
2383 */
2384 int
2385 lfs_vref(struct vnode *vp)
2386 {
2387 /*
2388 * If we return 1 here during a flush, we risk vinvalbuf() not
2389 * being able to flush all of the pages from this vnode, which
2390 * will cause it to panic. So, return 0 if a flush is in progress.
2391 */
2392 if (vp->v_flag & VXLOCK) {
2393 if (IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
2394 return 0;
2395 }
2396 return (1);
2397 }
2398 return (vget(vp, 0));
2399 }
2400
2401 /*
2402 * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
2403 * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
2404 */
2405 void
2406 lfs_vunref(struct vnode *vp)
2407 {
2408 /*
2409 * Analogous to lfs_vref, if the node is flushing, fake it.
2410 */
2411 if ((vp->v_flag & VXLOCK) && IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
2412 return;
2413 }
2414
2415 simple_lock(&vp->v_interlock);
2416 #ifdef DIAGNOSTIC
2417 if (vp->v_usecount <= 0) {
2418 printf("lfs_vunref: inum is %d\n", VTOI(vp)->i_number);
2419 printf("lfs_vunref: flags are 0x%lx\n", (u_long)vp->v_flag);
2420 printf("lfs_vunref: usecount = %ld\n", (long)vp->v_usecount);
2421 panic("lfs_vunref: v_usecount<0");
2422 }
2423 #endif
2424 vp->v_usecount--;
2425 if (vp->v_usecount > 0) {
2426 simple_unlock(&vp->v_interlock);
2427 return;
2428 }
2429 /*
2430 * insert at tail of LRU list
2431 */
2432 simple_lock(&vnode_free_list_slock);
2433 if (vp->v_holdcnt > 0)
2434 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
2435 else
2436 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2437 simple_unlock(&vnode_free_list_slock);
2438 simple_unlock(&vp->v_interlock);
2439 }
2440
2441 /*
2442 * We use this when we have vnodes that were loaded in solely for cleaning.
2443 * There is no reason to believe that these vnodes will be referenced again
2444 * soon, since the cleaning process is unrelated to normal filesystem
2445 * activity. Putting cleaned vnodes at the tail of the list has the effect
2446 * of flushing the vnode LRU. So, put vnodes that were loaded only for
2447 * cleaning at the head of the list, instead.
2448 */
2449 void
2450 lfs_vunref_head(struct vnode *vp)
2451 {
2452
2453 simple_lock(&vp->v_interlock);
2454 #ifdef DIAGNOSTIC
2455 if (vp->v_usecount == 0) {
2456 panic("lfs_vunref: v_usecount<0");
2457 }
2458 #endif
2459 vp->v_usecount--;
2460 if (vp->v_usecount > 0) {
2461 simple_unlock(&vp->v_interlock);
2462 return;
2463 }
2464 /*
2465 * insert at head of LRU list
2466 */
2467 simple_lock(&vnode_free_list_slock);
2468 if (vp->v_holdcnt > 0)
2469 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
2470 else
2471 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2472 simple_unlock(&vnode_free_list_slock);
2473 simple_unlock(&vp->v_interlock);
2474 }
2475
2476