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