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