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