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