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