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