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