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