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