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