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