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