lfs_segment.c revision 1.74.2.3 1 /* $NetBSD: lfs_segment.c,v 1.74.2.3 2002/07/15 10:37:27 gehenna 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.74.2.3 2002/07/15 10:37:27 gehenna 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 struct inode *ip;
723 IFILE *ifp;
724 int i, frag;
725
726 ip = VTOI(vp);
727
728 if (sp->seg_bytes_left < fs->lfs_bsize ||
729 sp->sum_bytes_left < sizeof(struct finfo))
730 (void) lfs_writeseg(fs, sp);
731
732 sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(ufs_daddr_t);
733 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
734
735 if (vp->v_flag & VDIROP)
736 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
737
738 fip = sp->fip;
739 fip->fi_nblocks = 0;
740 fip->fi_ino = ip->i_number;
741 LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
742 fip->fi_version = ifp->if_version;
743 brelse(bp);
744
745 if (sp->seg_flags & SEGM_CLEAN) {
746 lfs_gather(fs, sp, vp, lfs_match_fake);
747 /*
748 * For a file being flushed, we need to write *all* blocks.
749 * This means writing the cleaning blocks first, and then
750 * immediately following with any non-cleaning blocks.
751 * The same is true of the Ifile since checkpoints assume
752 * that all valid Ifile blocks are written.
753 */
754 if (IS_FLUSHING(fs,vp) || vp == fs->lfs_ivnode)
755 lfs_gather(fs, sp, vp, lfs_match_data);
756 } else
757 lfs_gather(fs, sp, vp, lfs_match_data);
758
759 /*
760 * It may not be necessary to write the meta-data blocks at this point,
761 * as the roll-forward recovery code should be able to reconstruct the
762 * list.
763 *
764 * We have to write them anyway, though, under two conditions: (1) the
765 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are
766 * checkpointing.
767 *
768 * BUT if we are cleaning, we might have indirect blocks that refer to
769 * new blocks not being written yet, in addition to fragments being
770 * moved out of a cleaned segment. If that is the case, don't
771 * write the indirect blocks, or the finfo will have a small block
772 * in the middle of it!
773 * XXX in this case isn't the inode size wrong too?
774 */
775 frag = 0;
776 if (sp->seg_flags & SEGM_CLEAN) {
777 for (i = 0; i < NDADDR; i++)
778 if (ip->i_lfs_fragsize[i] > 0 &&
779 ip->i_lfs_fragsize[i] < fs->lfs_bsize)
780 ++frag;
781 }
782 #ifdef DIAGNOSTIC
783 if (frag > 1)
784 panic("lfs_writefile: more than one fragment!");
785 #endif
786 if (IS_FLUSHING(fs, vp) ||
787 (frag == 0 && (lfs_writeindir || (sp->seg_flags & SEGM_CKP)))) {
788 lfs_gather(fs, sp, vp, lfs_match_indir);
789 lfs_gather(fs, sp, vp, lfs_match_dindir);
790 lfs_gather(fs, sp, vp, lfs_match_tindir);
791 }
792 fip = sp->fip;
793 if (fip->fi_nblocks != 0) {
794 sp->fip = (FINFO*)((caddr_t)fip + sizeof(struct finfo) +
795 sizeof(ufs_daddr_t) * (fip->fi_nblocks-1));
796 sp->start_lbp = &sp->fip->fi_blocks[0];
797 } else {
798 sp->sum_bytes_left += sizeof(FINFO) - sizeof(ufs_daddr_t);
799 --((SEGSUM *)(sp->segsum))->ss_nfinfo;
800 }
801 }
802
803 int
804 lfs_writeinode(struct lfs *fs, struct segment *sp, struct inode *ip)
805 {
806 struct buf *bp, *ibp;
807 struct dinode *cdp;
808 IFILE *ifp;
809 SEGUSE *sup;
810 ufs_daddr_t daddr;
811 daddr_t *daddrp;
812 ino_t ino;
813 int error, i, ndx, fsb = 0;
814 int redo_ifile = 0;
815 struct timespec ts;
816 int gotblk = 0;
817
818 if (!(ip->i_flag & IN_ALLMOD))
819 return (0);
820
821 /* Allocate a new inode block if necessary. */
822 if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) && sp->ibp == NULL) {
823 /* Allocate a new segment if necessary. */
824 if (sp->seg_bytes_left < fs->lfs_ibsize ||
825 sp->sum_bytes_left < sizeof(ufs_daddr_t))
826 (void) lfs_writeseg(fs, sp);
827
828 /* Get next inode block. */
829 daddr = fs->lfs_offset;
830 fs->lfs_offset += btofsb(fs, fs->lfs_ibsize);
831 sp->ibp = *sp->cbpp++ =
832 getblk(VTOI(fs->lfs_ivnode)->i_devvp, fsbtodb(fs, daddr),
833 fs->lfs_ibsize, 0, 0);
834 gotblk++;
835
836 /* Zero out inode numbers */
837 for (i = 0; i < INOPB(fs); ++i)
838 ((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
839
840 ++sp->start_bpp;
841 fs->lfs_avail -= btofsb(fs, fs->lfs_ibsize);
842 /* Set remaining space counters. */
843 sp->seg_bytes_left -= fs->lfs_ibsize;
844 sp->sum_bytes_left -= sizeof(ufs_daddr_t);
845 ndx = fs->lfs_sumsize / sizeof(ufs_daddr_t) -
846 sp->ninodes / INOPB(fs) - 1;
847 ((ufs_daddr_t *)(sp->segsum))[ndx] = daddr;
848 }
849
850 /* Update the inode times and copy the inode onto the inode page. */
851 TIMEVAL_TO_TIMESPEC(&time, &ts);
852 /* XXX kludge --- don't redirty the ifile just to put times on it */
853 if (ip->i_number != LFS_IFILE_INUM)
854 LFS_ITIMES(ip, &ts, &ts, &ts);
855
856 /*
857 * If this is the Ifile, and we've already written the Ifile in this
858 * partial segment, just overwrite it (it's not on disk yet) and
859 * continue.
860 *
861 * XXX we know that the bp that we get the second time around has
862 * already been gathered.
863 */
864 if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
865 *(sp->idp) = ip->i_din.ffs_din;
866 ip->i_lfs_osize = ip->i_ffs_size;
867 return 0;
868 }
869
870 bp = sp->ibp;
871 cdp = ((struct dinode *)bp->b_data) + (sp->ninodes % INOPB(fs));
872 *cdp = ip->i_din.ffs_din;
873 #ifdef LFS_IFILE_FRAG_ADDRESSING
874 if (fs->lfs_version > 1)
875 fsb = (sp->ninodes % INOPB(fs)) / INOPF(fs);
876 #endif
877
878 /*
879 * If we are cleaning, ensure that we don't write UNWRITTEN disk
880 * addresses to disk; possibly revert the inode size.
881 */
882 if (ip->i_lfs_effnblks != ip->i_ffs_blocks) {
883 cdp->di_size = ip->i_lfs_osize;
884 #ifdef DEBUG_LFS
885 printf("lfs_writeinode: cleansing ino %d (%d != %d)\n",
886 ip->i_number, ip->i_lfs_effnblks, ip->i_ffs_blocks);
887 #endif
888 for (daddrp = cdp->di_db; daddrp < cdp->di_ib + NIADDR;
889 daddrp++) {
890 if (*daddrp == UNWRITTEN) {
891 #ifdef DEBUG_LFS
892 printf("lfs_writeinode: wiping UNWRITTEN\n");
893 #endif
894 *daddrp = 0;
895 }
896 }
897 } else {
898 /* If all blocks are goig to disk, update the "size on disk" */
899 ip->i_lfs_osize = ip->i_ffs_size;
900 }
901
902 if (ip->i_flag & IN_CLEANING)
903 LFS_CLR_UINO(ip, IN_CLEANING);
904 else {
905 /* XXX IN_ALLMOD */
906 LFS_CLR_UINO(ip, IN_ACCESSED | IN_ACCESS | IN_CHANGE |
907 IN_UPDATE);
908 if (ip->i_lfs_effnblks == ip->i_ffs_blocks)
909 LFS_CLR_UINO(ip, IN_MODIFIED);
910 #ifdef DEBUG_LFS
911 else
912 printf("lfs_writeinode: ino %d: real blks=%d, "
913 "eff=%d\n", ip->i_number, ip->i_ffs_blocks,
914 ip->i_lfs_effnblks);
915 #endif
916 }
917
918 if (ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
919 sp->idp = ((struct dinode *)bp->b_data) +
920 (sp->ninodes % INOPB(fs));
921 if (gotblk) {
922 LFS_LOCK_BUF(bp);
923 brelse(bp);
924 }
925
926 /* Increment inode count in segment summary block. */
927 ++((SEGSUM *)(sp->segsum))->ss_ninos;
928
929 /* If this page is full, set flag to allocate a new page. */
930 if (++sp->ninodes % INOPB(fs) == 0)
931 sp->ibp = NULL;
932
933 /*
934 * If updating the ifile, update the super-block. Update the disk
935 * address and access times for this inode in the ifile.
936 */
937 ino = ip->i_number;
938 if (ino == LFS_IFILE_INUM) {
939 daddr = fs->lfs_idaddr;
940 fs->lfs_idaddr = dbtofsb(fs, bp->b_blkno);
941 } else {
942 LFS_IENTRY(ifp, fs, ino, ibp);
943 daddr = ifp->if_daddr;
944 ifp->if_daddr = dbtofsb(fs, bp->b_blkno) + fsb;
945 #ifdef LFS_DEBUG_NEXTFREE
946 if (ino > 3 && ifp->if_nextfree) {
947 vprint("lfs_writeinode",ITOV(ip));
948 printf("lfs_writeinode: updating free ino %d\n",
949 ip->i_number);
950 }
951 #endif
952 error = LFS_BWRITE_LOG(ibp); /* Ifile */
953 }
954
955 /*
956 * The inode's last address should not be in the current partial
957 * segment, except under exceptional circumstances (lfs_writevnodes
958 * had to start over, and in the meantime more blocks were written
959 * to a vnode). Both inodes will be accounted to this segment
960 * in lfs_writeseg so we need to subtract the earlier version
961 * here anyway. The segment count can temporarily dip below
962 * zero here; keep track of how many duplicates we have in
963 * "dupino" so we don't panic below.
964 */
965 if (daddr >= fs->lfs_lastpseg && daddr <= dbtofsb(fs, bp->b_blkno)) {
966 ++sp->ndupino;
967 printf("lfs_writeinode: last inode addr in current pseg "
968 "(ino %d daddr 0x%x) ndupino=%d\n", ino, daddr,
969 sp->ndupino);
970 }
971 /*
972 * Account the inode: it no longer belongs to its former segment,
973 * though it will not belong to the new segment until that segment
974 * is actually written.
975 */
976 if (daddr != LFS_UNUSED_DADDR) {
977 LFS_SEGENTRY(sup, fs, dtosn(fs, daddr), bp);
978 #ifdef DIAGNOSTIC
979 if (sup->su_nbytes < DINODE_SIZE * (1 + sp->ndupino)) {
980 printf("lfs_writeinode: negative bytes "
981 "(segment %d short by %d)\n",
982 dtosn(fs, daddr),
983 (int)DINODE_SIZE - sup->su_nbytes);
984 panic("lfs_writeinode: negative bytes");
985 sup->su_nbytes = DINODE_SIZE;
986 }
987 #endif
988 #ifdef DEBUG_SU_NBYTES
989 printf("seg %d -= %d for ino %d inode\n",
990 dtosn(fs, daddr), DINODE_SIZE, ino);
991 #endif
992 sup->su_nbytes -= DINODE_SIZE;
993 redo_ifile =
994 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
995 if (redo_ifile)
996 fs->lfs_flags |= LFS_IFDIRTY;
997 error = LFS_BWRITE_LOG(bp); /* Ifile */
998 }
999 return (redo_ifile);
1000 }
1001
1002 int
1003 lfs_gatherblock(struct segment *sp, struct buf *bp, int *sptr)
1004 {
1005 struct lfs *fs;
1006 int version;
1007
1008 /*
1009 * If full, finish this segment. We may be doing I/O, so
1010 * release and reacquire the splbio().
1011 */
1012 #ifdef DIAGNOSTIC
1013 if (sp->vp == NULL)
1014 panic ("lfs_gatherblock: Null vp in segment");
1015 #endif
1016 fs = sp->fs;
1017 if (sp->sum_bytes_left < sizeof(ufs_daddr_t) ||
1018 sp->seg_bytes_left < bp->b_bcount) {
1019 if (sptr)
1020 splx(*sptr);
1021 lfs_updatemeta(sp);
1022
1023 version = sp->fip->fi_version;
1024 (void) lfs_writeseg(fs, sp);
1025
1026 sp->fip->fi_version = version;
1027 sp->fip->fi_ino = VTOI(sp->vp)->i_number;
1028 /* Add the current file to the segment summary. */
1029 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
1030 sp->sum_bytes_left -=
1031 sizeof(struct finfo) - sizeof(ufs_daddr_t);
1032
1033 if (sptr)
1034 *sptr = splbio();
1035 return (1);
1036 }
1037
1038 #ifdef DEBUG
1039 if (bp->b_flags & B_GATHERED) {
1040 printf("lfs_gatherblock: already gathered! Ino %d, lbn %d\n",
1041 sp->fip->fi_ino, bp->b_lblkno);
1042 return (0);
1043 }
1044 #endif
1045 /* Insert into the buffer list, update the FINFO block. */
1046 bp->b_flags |= B_GATHERED;
1047 bp->b_flags &= ~B_DONE;
1048
1049 *sp->cbpp++ = bp;
1050 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
1051
1052 sp->sum_bytes_left -= sizeof(ufs_daddr_t);
1053 sp->seg_bytes_left -= bp->b_bcount;
1054 return (0);
1055 }
1056
1057 int
1058 lfs_gather(struct lfs *fs, struct segment *sp, struct vnode *vp, int (*match)(struct lfs *, struct buf *))
1059 {
1060 struct buf *bp, *nbp;
1061 int s, count = 0;
1062
1063 sp->vp = vp;
1064 s = splbio();
1065
1066 #ifndef LFS_NO_BACKBUF_HACK
1067 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
1068 # define BUF_OFFSET (((caddr_t)&LIST_NEXT(bp, b_vnbufs)) - (caddr_t)bp)
1069 # define BACK_BUF(BP) ((struct buf *)(((caddr_t)(BP)->b_vnbufs.le_prev) - BUF_OFFSET))
1070 # define BEG_OF_LIST ((struct buf *)(((caddr_t)&LIST_FIRST(&vp->v_dirtyblkhd)) - BUF_OFFSET))
1071 /* Find last buffer. */
1072 loop: for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp && LIST_NEXT(bp, b_vnbufs) != NULL;
1073 bp = LIST_NEXT(bp, b_vnbufs));
1074 for (; bp && bp != BEG_OF_LIST; bp = nbp) {
1075 nbp = BACK_BUF(bp);
1076 #else /* LFS_NO_BACKBUF_HACK */
1077 loop: for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1078 nbp = LIST_NEXT(bp, b_vnbufs);
1079 #endif /* LFS_NO_BACKBUF_HACK */
1080 if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp)) {
1081 #ifdef DEBUG_LFS
1082 if (vp == fs->lfs_ivnode && (bp->b_flags & (B_BUSY|B_GATHERED)) == B_BUSY)
1083 printf("(%d:%lx)", bp->b_lblkno, bp->b_flags);
1084 #endif
1085 continue;
1086 }
1087 if (vp->v_type == VBLK) {
1088 /* For block devices, just write the blocks. */
1089 /* XXX Do we really need to even do this? */
1090 #ifdef DEBUG_LFS
1091 if (count == 0)
1092 printf("BLK(");
1093 printf(".");
1094 #endif
1095 /* Get the block before bwrite, so we don't corrupt the free list */
1096 bp->b_flags |= B_BUSY;
1097 bremfree(bp);
1098 bwrite(bp);
1099 } else {
1100 #ifdef DIAGNOSTIC
1101 if ((bp->b_flags & (B_CALL|B_INVAL)) == B_INVAL) {
1102 printf("lfs_gather: lbn %d is B_INVAL\n",
1103 bp->b_lblkno);
1104 VOP_PRINT(bp->b_vp);
1105 }
1106 if (!(bp->b_flags & B_DELWRI))
1107 panic("lfs_gather: bp not B_DELWRI");
1108 if (!(bp->b_flags & B_LOCKED)) {
1109 printf("lfs_gather: lbn %d blk %d"
1110 " not B_LOCKED\n", bp->b_lblkno,
1111 dbtofsb(fs, bp->b_blkno));
1112 VOP_PRINT(bp->b_vp);
1113 panic("lfs_gather: bp not B_LOCKED");
1114 }
1115 #endif
1116 if (lfs_gatherblock(sp, bp, &s)) {
1117 goto loop;
1118 }
1119 }
1120 count++;
1121 }
1122 splx(s);
1123 #ifdef DEBUG_LFS
1124 if (vp->v_type == VBLK && count)
1125 printf(")\n");
1126 #endif
1127 lfs_updatemeta(sp);
1128 sp->vp = NULL;
1129 return count;
1130 }
1131
1132 /*
1133 * Update the metadata that points to the blocks listed in the FINFO
1134 * array.
1135 */
1136 void
1137 lfs_updatemeta(struct segment *sp)
1138 {
1139 SEGUSE *sup;
1140 struct buf *bp, *sbp;
1141 struct lfs *fs;
1142 struct vnode *vp;
1143 struct indir a[NIADDR + 2], *ap;
1144 struct inode *ip;
1145 ufs_daddr_t daddr, lbn, off;
1146 daddr_t ooff;
1147 int error, i, nblocks, num;
1148 int bb, osize, obb;
1149
1150 vp = sp->vp;
1151 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
1152 if (nblocks < 0)
1153 panic("This is a bad thing\n");
1154 if (vp == NULL || nblocks == 0)
1155 return;
1156
1157 /* Sort the blocks. */
1158 /*
1159 * XXX KS - We have to sort even if the blocks come from the
1160 * cleaner, because there might be other pending blocks on the
1161 * same inode...and if we don't sort, and there are fragments
1162 * present, blocks may be written in the wrong place.
1163 */
1164 /* if (!(sp->seg_flags & SEGM_CLEAN)) */
1165 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
1166
1167 /*
1168 * Record the length of the last block in case it's a fragment.
1169 * If there are indirect blocks present, they sort last. An
1170 * indirect block will be lfs_bsize and its presence indicates
1171 * that you cannot have fragments.
1172 *
1173 * XXX This last is a lie. A cleaned fragment can coexist with
1174 * XXX a later indirect block. This will continue to be
1175 * XXX true until lfs_markv is fixed to do everything with
1176 * XXX fake blocks (including fake inodes and fake indirect blocks).
1177 */
1178 sp->fip->fi_lastlength = sp->start_bpp[nblocks - 1]->b_bcount;
1179
1180 /*
1181 * Assign disk addresses, and update references to the logical
1182 * block and the segment usage information.
1183 */
1184 fs = sp->fs;
1185 for (i = nblocks; i--; ++sp->start_bpp) {
1186 lbn = *sp->start_lbp++;
1187 sbp = *sp->start_bpp;
1188
1189 sbp->b_blkno = fsbtodb(fs, fs->lfs_offset);
1190 off = fs->lfs_offset;
1191 if (sbp->b_blkno == sbp->b_lblkno) {
1192 printf("lfs_updatemeta: ino %d blk %d"
1193 " has same lbn and daddr\n",
1194 VTOI(vp)->i_number, off);
1195 }
1196
1197 /*
1198 * If we write a frag in the wrong place, the cleaner won't
1199 * be able to correctly identify its size later, and the
1200 * segment will be uncleanable. (Even worse, it will assume
1201 * that the indirect block that actually ends the list
1202 * is of a smaller size!)
1203 */
1204 if (sbp->b_bcount < fs->lfs_bsize && i != 0)
1205 panic("lfs_updatemeta: fragment is not last block\n");
1206
1207 bb = fragstofsb(fs, numfrags(fs, sbp->b_bcount));
1208 fs->lfs_offset += bb;
1209 error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL);
1210 if (daddr > 0)
1211 daddr = dbtofsb(fs, daddr);
1212 if (error)
1213 panic("lfs_updatemeta: ufs_bmaparray %d", error);
1214 ip = VTOI(vp);
1215 switch (num) {
1216 case 0:
1217 ooff = ip->i_ffs_db[lbn];
1218 #ifdef DEBUG
1219 if (ooff == 0) {
1220 printf("lfs_updatemeta[1]: warning: writing "
1221 "ino %d lbn %d at 0x%x, was 0x0\n",
1222 ip->i_number, lbn, off);
1223 }
1224 #endif
1225 if (ooff == UNWRITTEN)
1226 ip->i_ffs_blocks += bb;
1227 else {
1228 /* possible fragment truncation or extension */
1229 obb = btofsb(fs, ip->i_lfs_fragsize[lbn]);
1230 ip->i_ffs_blocks += (bb - obb);
1231 }
1232 ip->i_ffs_db[lbn] = off;
1233 break;
1234 case 1:
1235 ooff = ip->i_ffs_ib[a[0].in_off];
1236 #ifdef DEBUG
1237 if (ooff == 0) {
1238 printf("lfs_updatemeta[2]: warning: writing "
1239 "ino %d lbn %d at 0x%x, was 0x0\n",
1240 ip->i_number, lbn, off);
1241 }
1242 #endif
1243 if (ooff == UNWRITTEN)
1244 ip->i_ffs_blocks += bb;
1245 ip->i_ffs_ib[a[0].in_off] = off;
1246 break;
1247 default:
1248 ap = &a[num - 1];
1249 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
1250 panic("lfs_updatemeta: bread bno %d",
1251 ap->in_lbn);
1252
1253 ooff = ((ufs_daddr_t *)bp->b_data)[ap->in_off];
1254 #if DEBUG
1255 if (ooff == 0) {
1256 printf("lfs_updatemeta[3]: warning: writing "
1257 "ino %d lbn %d at 0x%x, was 0x0\n",
1258 ip->i_number, lbn, off);
1259 }
1260 #endif
1261 if (ooff == UNWRITTEN)
1262 ip->i_ffs_blocks += bb;
1263 ((ufs_daddr_t *)bp->b_data)[ap->in_off] = off;
1264 (void) VOP_BWRITE(bp);
1265 }
1266 #ifdef DEBUG
1267 if (daddr >= fs->lfs_lastpseg && daddr <= off) {
1268 printf("lfs_updatemeta: ino %d, lbn %d, addr = %x "
1269 "in same pseg\n", VTOI(sp->vp)->i_number,
1270 sbp->b_lblkno, daddr);
1271 }
1272 #endif
1273 /*
1274 * Update segment usage information, based on old size
1275 * and location.
1276 */
1277 if (daddr > 0) {
1278 if (lbn >= 0 && lbn < NDADDR)
1279 osize = ip->i_lfs_fragsize[lbn];
1280 else
1281 osize = fs->lfs_bsize;
1282 LFS_SEGENTRY(sup, fs, dtosn(fs, daddr), bp);
1283 #ifdef DIAGNOSTIC
1284 if (sup->su_nbytes < osize + DINODE_SIZE * sp->ndupino) {
1285 printf("lfs_updatemeta: negative bytes "
1286 "(segment %d short by %d)\n",
1287 dtosn(fs, daddr),
1288 osize - sup->su_nbytes);
1289 printf("lfs_updatemeta: ino %d, lbn %d, "
1290 "addr = 0x%x\n", VTOI(sp->vp)->i_number,
1291 lbn, daddr);
1292 panic("lfs_updatemeta: negative bytes");
1293 sup->su_nbytes = osize + DINODE_SIZE * sp->ndupino;
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 int 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 int 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 int 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\n");
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