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