lfs_segment.c revision 1.68.4.2 1 /* $NetBSD: lfs_segment.c,v 1.68.4.2 2002/01/10 20:05:13 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1999, 2000 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.68.4.2 2002/01/10 20:05:13 thorpej 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/conf.h>
92 #include <sys/vnode.h>
93 #include <sys/malloc.h>
94 #include <sys/mount.h>
95
96 #include <miscfs/specfs/specdev.h>
97 #include <miscfs/fifofs/fifo.h>
98
99 #include <ufs/ufs/inode.h>
100 #include <ufs/ufs/dir.h>
101 #include <ufs/ufs/ufsmount.h>
102 #include <ufs/ufs/ufs_extern.h>
103
104 #include <ufs/lfs/lfs.h>
105 #include <ufs/lfs/lfs_extern.h>
106
107 extern int count_lock_queue(void);
108 extern struct simplelock vnode_free_list_slock; /* XXX */
109
110 /*
111 * Determine if it's OK to start a partial in this segment, or if we need
112 * to go on to a new segment.
113 */
114 #define LFS_PARTIAL_FITS(fs) \
115 ((fs)->lfs_fsbpseg - ((fs)->lfs_offset - (fs)->lfs_curseg) > \
116 fragstofsb((fs), (fs)->lfs_frag))
117
118 void lfs_callback(struct buf *);
119 int lfs_gather(struct lfs *, struct segment *,
120 struct vnode *, int (*)(struct lfs *, struct buf *));
121 int lfs_gatherblock(struct segment *, struct buf *, int *);
122 void lfs_iset(struct inode *, ufs_daddr_t, time_t);
123 int lfs_match_fake(struct lfs *, struct buf *);
124 int lfs_match_data(struct lfs *, struct buf *);
125 int lfs_match_dindir(struct lfs *, struct buf *);
126 int lfs_match_indir(struct lfs *, struct buf *);
127 int lfs_match_tindir(struct lfs *, struct buf *);
128 void lfs_newseg(struct lfs *);
129 void lfs_shellsort(struct buf **, ufs_daddr_t *, int);
130 void lfs_supercallback(struct buf *);
131 void lfs_updatemeta(struct segment *);
132 int lfs_vref(struct vnode *);
133 void lfs_vunref(struct vnode *);
134 void lfs_writefile(struct lfs *, struct segment *, struct vnode *);
135 int lfs_writeinode(struct lfs *, struct segment *, struct inode *);
136 int lfs_writeseg(struct lfs *, struct segment *);
137 void lfs_writesuper(struct lfs *, daddr_t);
138 int lfs_writevnodes(struct lfs *fs, struct mount *mp,
139 struct segment *sp, int dirops);
140
141 int lfs_allclean_wakeup; /* Cleaner wakeup address. */
142 int lfs_writeindir = 1; /* whether to flush indir on non-ckp */
143 int lfs_clean_vnhead = 0; /* Allow freeing to head of vn list */
144 int lfs_dirvcount = 0; /* # active dirops */
145
146 /* Statistics Counters */
147 int lfs_dostats = 1;
148 struct lfs_stats lfs_stats;
149
150 extern int locked_queue_count;
151 extern long locked_queue_bytes;
152
153 /* op values to lfs_writevnodes */
154 #define VN_REG 0
155 #define VN_DIROP 1
156 #define VN_EMPTY 2
157 #define VN_CLEAN 3
158
159 #define LFS_MAX_ACTIVE 10
160
161 /*
162 * XXX KS - Set modification time on the Ifile, so the cleaner can
163 * read the fs mod time off of it. We don't set IN_UPDATE here,
164 * since we don't really need this to be flushed to disk (and in any
165 * case that wouldn't happen to the Ifile until we checkpoint).
166 */
167 void
168 lfs_imtime(struct lfs *fs)
169 {
170 struct timespec ts;
171 struct inode *ip;
172
173 TIMEVAL_TO_TIMESPEC(&time, &ts);
174 ip = VTOI(fs->lfs_ivnode);
175 ip->i_ffs_mtime = ts.tv_sec;
176 ip->i_ffs_mtimensec = ts.tv_nsec;
177 }
178
179 /*
180 * Ifile and meta data blocks are not marked busy, so segment writes MUST be
181 * single threaded. Currently, there are two paths into lfs_segwrite, sync()
182 * and getnewbuf(). They both mark the file system busy. Lfs_vflush()
183 * explicitly marks the file system busy. So lfs_segwrite is safe. I think.
184 */
185
186 #define SET_FLUSHING(fs,vp) (fs)->lfs_flushvp = (vp)
187 #define IS_FLUSHING(fs,vp) ((fs)->lfs_flushvp == (vp))
188 #define CLR_FLUSHING(fs,vp) (fs)->lfs_flushvp = NULL
189
190 int
191 lfs_vflush(struct vnode *vp)
192 {
193 struct inode *ip;
194 struct lfs *fs;
195 struct segment *sp;
196 struct buf *bp, *nbp, *tbp, *tnbp;
197 int error, s;
198
199 ip = VTOI(vp);
200 fs = VFSTOUFS(vp->v_mount)->um_lfs;
201
202 if (ip->i_flag & IN_CLEANING) {
203 #ifdef DEBUG_LFS
204 ivndebug(vp,"vflush/in_cleaning");
205 #endif
206 LFS_CLR_UINO(ip, IN_CLEANING);
207 LFS_SET_UINO(ip, IN_MODIFIED);
208
209 /*
210 * Toss any cleaning buffers that have real counterparts
211 * to avoid losing new data
212 */
213 s = splbio();
214 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
215 nbp = bp->b_vnbufs.le_next;
216 if (bp->b_flags & B_CALL) {
217 for (tbp = vp->v_dirtyblkhd.lh_first; tbp;
218 tbp = tnbp)
219 {
220 tnbp = tbp->b_vnbufs.le_next;
221 if (tbp->b_vp == bp->b_vp
222 && tbp->b_lblkno == bp->b_lblkno
223 && tbp != bp)
224 {
225 fs->lfs_avail += btofsb(fs, bp->b_bcount);
226 wakeup(&fs->lfs_avail);
227 lfs_freebuf(bp);
228 bp = NULL;
229 break;
230 }
231 }
232 }
233 }
234 splx(s);
235 }
236
237 /* If the node is being written, wait until that is done */
238 if (WRITEINPROG(vp)) {
239 #ifdef DEBUG_LFS
240 ivndebug(vp,"vflush/writeinprog");
241 #endif
242 tsleep(vp, PRIBIO+1, "lfs_vw", 0);
243 }
244
245 /* Protect against VXLOCK deadlock in vinvalbuf() */
246 lfs_seglock(fs, SEGM_SYNC);
247
248 /* If we're supposed to flush a freed inode, just toss it */
249 /* XXX - seglock, so these buffers can't be gathered, right? */
250 if (ip->i_ffs_mode == 0) {
251 printf("lfs_vflush: ino %d is freed, not flushing\n",
252 ip->i_number);
253 s = splbio();
254 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
255 nbp = bp->b_vnbufs.le_next;
256 if (bp->b_flags & B_DELWRI) { /* XXX always true? */
257 fs->lfs_avail += btofsb(fs, bp->b_bcount);
258 wakeup(&fs->lfs_avail);
259 }
260 /* Copied from lfs_writeseg */
261 if (bp->b_flags & B_CALL) {
262 /* if B_CALL, it was created with newbuf */
263 lfs_freebuf(bp);
264 bp = NULL;
265 } else {
266 bremfree(bp);
267 LFS_UNLOCK_BUF(bp);
268 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
269 B_GATHERED);
270 bp->b_flags |= B_DONE;
271 reassignbuf(bp, vp);
272 brelse(bp);
273 }
274 }
275 splx(s);
276 LFS_CLR_UINO(ip, IN_CLEANING);
277 LFS_CLR_UINO(ip, IN_MODIFIED | IN_ACCESSED);
278 ip->i_flag &= ~IN_ALLMOD;
279 printf("lfs_vflush: done not flushing ino %d\n",
280 ip->i_number);
281 lfs_segunlock(fs);
282 return 0;
283 }
284
285 SET_FLUSHING(fs,vp);
286 if (fs->lfs_nactive > LFS_MAX_ACTIVE) {
287 error = lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP);
288 CLR_FLUSHING(fs,vp);
289 lfs_segunlock(fs);
290 return error;
291 }
292 sp = fs->lfs_sp;
293
294 if (vp->v_dirtyblkhd.lh_first == NULL) {
295 lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
296 } else if ((ip->i_flag & IN_CLEANING) &&
297 (fs->lfs_sp->seg_flags & SEGM_CLEAN)) {
298 #ifdef DEBUG_LFS
299 ivndebug(vp,"vflush/clean");
300 #endif
301 lfs_writevnodes(fs, vp->v_mount, sp, VN_CLEAN);
302 }
303 else if (lfs_dostats) {
304 if (vp->v_dirtyblkhd.lh_first || (VTOI(vp)->i_flag & IN_ALLMOD))
305 ++lfs_stats.vflush_invoked;
306 #ifdef DEBUG_LFS
307 ivndebug(vp,"vflush");
308 #endif
309 }
310
311 #ifdef DIAGNOSTIC
312 /* XXX KS This actually can happen right now, though it shouldn't(?) */
313 if (vp->v_flag & VDIROP) {
314 printf("lfs_vflush: flushing VDIROP, this shouldn\'t be\n");
315 /* panic("VDIROP being flushed...this can\'t happen"); */
316 }
317 if (vp->v_usecount < 0) {
318 printf("usecount=%ld\n", (long)vp->v_usecount);
319 panic("lfs_vflush: usecount<0");
320 }
321 #endif
322
323 do {
324 do {
325 if (vp->v_dirtyblkhd.lh_first != NULL)
326 lfs_writefile(fs, sp, vp);
327 } while (lfs_writeinode(fs, sp, ip));
328 } while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
329
330 if (lfs_dostats) {
331 ++lfs_stats.nwrites;
332 if (sp->seg_flags & SEGM_SYNC)
333 ++lfs_stats.nsync_writes;
334 if (sp->seg_flags & SEGM_CKP)
335 ++lfs_stats.ncheckpoints;
336 }
337 lfs_segunlock(fs);
338
339 CLR_FLUSHING(fs,vp);
340 return (0);
341 }
342
343 #ifdef DEBUG_LFS_VERBOSE
344 # 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)
345 #else
346 # define vndebug(vp,str)
347 #endif
348
349 int
350 lfs_writevnodes(struct lfs *fs, struct mount *mp, struct segment *sp, int op)
351 {
352 struct inode *ip;
353 struct vnode *vp;
354 int inodes_written = 0, only_cleaning;
355 int needs_unlock;
356
357 #ifndef LFS_NO_BACKVP_HACK
358 /* BEGIN HACK */
359 #define VN_OFFSET (((caddr_t)&vp->v_mntvnodes.le_next) - (caddr_t)vp)
360 #define BACK_VP(VP) ((struct vnode *)(((caddr_t)VP->v_mntvnodes.le_prev) - VN_OFFSET))
361 #define BEG_OF_VLIST ((struct vnode *)(((caddr_t)&mp->mnt_vnodelist.lh_first) - VN_OFFSET))
362
363 /* Find last vnode. */
364 loop: for (vp = mp->mnt_vnodelist.lh_first;
365 vp && vp->v_mntvnodes.le_next != NULL;
366 vp = vp->v_mntvnodes.le_next);
367 for (; vp && vp != BEG_OF_VLIST; vp = BACK_VP(vp)) {
368 #else
369 loop:
370 for (vp = mp->mnt_vnodelist.lh_first;
371 vp != NULL;
372 vp = vp->v_mntvnodes.le_next) {
373 #endif
374 /*
375 * If the vnode that we are about to sync is no longer
376 * associated with this mount point, start over.
377 */
378 if (vp->v_mount != mp) {
379 printf("lfs_writevnodes: starting over\n");
380 goto loop;
381 }
382
383 ip = VTOI(vp);
384 if ((op == VN_DIROP && !(vp->v_flag & VDIROP)) ||
385 (op != VN_DIROP && op != VN_CLEAN && (vp->v_flag & VDIROP))) {
386 vndebug(vp,"dirop");
387 continue;
388 }
389
390 if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first) {
391 vndebug(vp,"empty");
392 continue;
393 }
394
395 if (vp->v_type == VNON) {
396 continue;
397 }
398
399 if (op == VN_CLEAN && ip->i_number != LFS_IFILE_INUM
400 && vp != fs->lfs_flushvp
401 && !(ip->i_flag & IN_CLEANING)) {
402 vndebug(vp,"cleaning");
403 continue;
404 }
405
406 if (lfs_vref(vp)) {
407 vndebug(vp,"vref");
408 continue;
409 }
410
411 needs_unlock = 0;
412 if (VOP_ISLOCKED(vp)) {
413 if (vp != fs->lfs_ivnode &&
414 vp->v_lock.lk_lockholder != curproc->p_pid) {
415 #ifdef DEBUG_LFS
416 printf("lfs_writevnodes: not writing ino %d,"
417 " locked by pid %d\n",
418 VTOI(vp)->i_number,
419 vp->v_lock.lk_lockholder);
420 #endif
421 lfs_vunref(vp);
422 continue;
423 }
424 } else if (vp != fs->lfs_ivnode) {
425 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
426 needs_unlock = 1;
427 }
428
429 only_cleaning = 0;
430 /*
431 * Write the inode/file if dirty and it's not the IFILE.
432 */
433 if ((ip->i_flag & IN_ALLMOD) ||
434 (vp->v_dirtyblkhd.lh_first != NULL))
435 {
436 only_cleaning = ((ip->i_flag & IN_ALLMOD) == IN_CLEANING);
437
438 if (ip->i_number != LFS_IFILE_INUM
439 && vp->v_dirtyblkhd.lh_first != NULL)
440 {
441 lfs_writefile(fs, sp, vp);
442 }
443 if (vp->v_dirtyblkhd.lh_first != NULL) {
444 if (WRITEINPROG(vp)) {
445 #ifdef DEBUG_LFS
446 ivndebug(vp,"writevnodes/write2");
447 #endif
448 } else if (!(ip->i_flag & IN_ALLMOD)) {
449 #ifdef DEBUG_LFS
450 printf("<%d>",ip->i_number);
451 #endif
452 LFS_SET_UINO(ip, IN_MODIFIED);
453 }
454 }
455 (void) lfs_writeinode(fs, sp, ip);
456 inodes_written++;
457 }
458
459 if (needs_unlock)
460 VOP_UNLOCK(vp, 0);
461
462 if (lfs_clean_vnhead && only_cleaning)
463 lfs_vunref_head(vp);
464 else
465 lfs_vunref(vp);
466 }
467 return inodes_written;
468 }
469
470 /*
471 * Do a checkpoint.
472 */
473 int
474 lfs_segwrite(struct mount *mp, int flags)
475 {
476 struct buf *bp;
477 struct inode *ip;
478 struct lfs *fs;
479 struct segment *sp;
480 struct vnode *vp;
481 SEGUSE *segusep;
482 ufs_daddr_t ibno;
483 int do_ckp, did_ckp, error, i;
484 int writer_set = 0;
485 int dirty;
486
487 fs = VFSTOUFS(mp)->um_lfs;
488
489 if (fs->lfs_ronly)
490 return EROFS;
491
492 lfs_imtime(fs);
493
494 /* printf("lfs_segwrite: ifile flags are 0x%lx\n",
495 (long)(VTOI(fs->lfs_ivnode)->i_flag)); */
496
497 #if 0
498 /*
499 * If we are not the cleaner, and there is no space available,
500 * wait until cleaner writes.
501 */
502 if (!(flags & SEGM_CLEAN) && !(fs->lfs_seglock && fs->lfs_sp &&
503 (fs->lfs_sp->seg_flags & SEGM_CLEAN)))
504 {
505 while (fs->lfs_avail <= 0) {
506 LFS_CLEANERINFO(cip, fs, bp);
507 LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
508
509 wakeup(&lfs_allclean_wakeup);
510 wakeup(&fs->lfs_nextseg);
511 error = tsleep(&fs->lfs_avail, PRIBIO + 1, "lfs_av2",
512 0);
513 if (error) {
514 return (error);
515 }
516 }
517 }
518 #endif
519 /*
520 * Allocate a segment structure and enough space to hold pointers to
521 * the maximum possible number of buffers which can be described in a
522 * single summary block.
523 */
524 do_ckp = (flags & SEGM_CKP) || fs->lfs_nactive > LFS_MAX_ACTIVE;
525 lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
526 sp = fs->lfs_sp;
527
528 /*
529 * If lfs_flushvp is non-NULL, we are called from lfs_vflush,
530 * in which case we have to flush *all* buffers off of this vnode.
531 * We don't care about other nodes, but write any non-dirop nodes
532 * anyway in anticipation of another getnewvnode().
533 *
534 * If we're cleaning we only write cleaning and ifile blocks, and
535 * no dirops, since otherwise we'd risk corruption in a crash.
536 */
537 if (sp->seg_flags & SEGM_CLEAN)
538 lfs_writevnodes(fs, mp, sp, VN_CLEAN);
539 else {
540 lfs_writevnodes(fs, mp, sp, VN_REG);
541 if (!fs->lfs_dirops || !fs->lfs_flushvp) {
542 while (fs->lfs_dirops)
543 if ((error = tsleep(&fs->lfs_writer, PRIBIO + 1,
544 "lfs writer", 0)))
545 {
546 /* XXX why not segunlock? */
547 free(sp->bpp, M_SEGMENT);
548 sp->bpp = NULL;
549 free(sp, M_SEGMENT);
550 fs->lfs_sp = NULL;
551 return (error);
552 }
553 fs->lfs_writer++;
554 writer_set = 1;
555 lfs_writevnodes(fs, mp, sp, VN_DIROP);
556 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
557 }
558 }
559
560 /*
561 * If we are doing a checkpoint, mark everything since the
562 * last checkpoint as no longer ACTIVE.
563 */
564 if (do_ckp) {
565 for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
566 --ibno >= fs->lfs_cleansz; ) {
567 dirty = 0;
568 if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize, NOCRED, &bp))
569
570 panic("lfs_segwrite: ifile read");
571 segusep = (SEGUSE *)bp->b_data;
572 for (i = fs->lfs_sepb; i--;) {
573 if (segusep->su_flags & SEGUSE_ACTIVE) {
574 segusep->su_flags &= ~SEGUSE_ACTIVE;
575 ++dirty;
576 }
577 if (fs->lfs_version > 1)
578 ++segusep;
579 else
580 segusep = (SEGUSE *)
581 ((SEGUSE_V1 *)segusep + 1);
582 }
583
584 /* But the current segment is still ACTIVE */
585 segusep = (SEGUSE *)bp->b_data;
586 if (dtosn(fs, fs->lfs_curseg) / fs->lfs_sepb ==
587 (ibno-fs->lfs_cleansz)) {
588 if (fs->lfs_version > 1)
589 segusep[dtosn(fs, fs->lfs_curseg) %
590 fs->lfs_sepb].su_flags |=
591 SEGUSE_ACTIVE;
592 else
593 ((SEGUSE *)
594 ((SEGUSE_V1 *)(bp->b_data) +
595 (dtosn(fs, fs->lfs_curseg) %
596 fs->lfs_sepb)))->su_flags
597 |= SEGUSE_ACTIVE;
598 --dirty;
599 }
600 if (dirty)
601 error = VOP_BWRITE(bp); /* Ifile */
602 else
603 brelse(bp);
604 }
605 }
606
607 did_ckp = 0;
608 if (do_ckp || fs->lfs_doifile) {
609 do {
610 vp = fs->lfs_ivnode;
611
612 vget(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY);
613
614 ip = VTOI(vp);
615 if (vp->v_dirtyblkhd.lh_first != NULL)
616 lfs_writefile(fs, sp, vp);
617 if (ip->i_flag & IN_ALLMOD)
618 ++did_ckp;
619 (void) lfs_writeinode(fs, sp, ip);
620
621 vput(vp);
622 } while (lfs_writeseg(fs, sp) && do_ckp);
623
624 /* The ifile should now be all clear */
625 LFS_CLR_UINO(ip, IN_ALLMOD);
626 } else {
627 (void) lfs_writeseg(fs, sp);
628 }
629
630 /*
631 * If the I/O count is non-zero, sleep until it reaches zero.
632 * At the moment, the user's process hangs around so we can
633 * sleep.
634 */
635 fs->lfs_doifile = 0;
636 if (writer_set && --fs->lfs_writer == 0)
637 wakeup(&fs->lfs_dirops);
638
639 /*
640 * If we didn't write the Ifile, we didn't really do anything.
641 * That means that (1) there is a checkpoint on disk and (2)
642 * nothing has changed since it was written.
643 *
644 * Take the flags off of the segment so that lfs_segunlock
645 * doesn't have to write the superblock either.
646 */
647 if (did_ckp == 0) {
648 sp->seg_flags &= ~(SEGM_SYNC|SEGM_CKP);
649 /* if (do_ckp) printf("lfs_segwrite: no checkpoint\n"); */
650 }
651
652 if (lfs_dostats) {
653 ++lfs_stats.nwrites;
654 if (sp->seg_flags & SEGM_SYNC)
655 ++lfs_stats.nsync_writes;
656 if (sp->seg_flags & SEGM_CKP)
657 ++lfs_stats.ncheckpoints;
658 }
659 lfs_segunlock(fs);
660 return (0);
661 }
662
663 /*
664 * Write the dirty blocks associated with a vnode.
665 */
666 void
667 lfs_writefile(struct lfs *fs, struct segment *sp, struct vnode *vp)
668 {
669 struct buf *bp;
670 struct finfo *fip;
671 IFILE *ifp;
672
673
674 if (sp->seg_bytes_left < fs->lfs_bsize ||
675 sp->sum_bytes_left < sizeof(struct finfo))
676 (void) lfs_writeseg(fs, sp);
677
678 sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(ufs_daddr_t);
679 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
680
681 if (vp->v_flag & VDIROP)
682 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
683
684 fip = sp->fip;
685 fip->fi_nblocks = 0;
686 fip->fi_ino = VTOI(vp)->i_number;
687 LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
688 fip->fi_version = ifp->if_version;
689 brelse(bp);
690
691 if (sp->seg_flags & SEGM_CLEAN)
692 {
693 lfs_gather(fs, sp, vp, lfs_match_fake);
694 /*
695 * For a file being flushed, we need to write *all* blocks.
696 * This means writing the cleaning blocks first, and then
697 * immediately following with any non-cleaning blocks.
698 * The same is true of the Ifile since checkpoints assume
699 * that all valid Ifile blocks are written.
700 */
701 if (IS_FLUSHING(fs,vp) || VTOI(vp)->i_number == LFS_IFILE_INUM)
702 lfs_gather(fs, sp, vp, lfs_match_data);
703 } else
704 lfs_gather(fs, sp, vp, lfs_match_data);
705
706 /*
707 * It may not be necessary to write the meta-data blocks at this point,
708 * as the roll-forward recovery code should be able to reconstruct the
709 * list.
710 *
711 * We have to write them anyway, though, under two conditions: (1) the
712 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are
713 * checkpointing.
714 */
715 if (lfs_writeindir
716 || IS_FLUSHING(fs,vp)
717 || (sp->seg_flags & SEGM_CKP))
718 {
719 lfs_gather(fs, sp, vp, lfs_match_indir);
720 lfs_gather(fs, sp, vp, lfs_match_dindir);
721 lfs_gather(fs, sp, vp, lfs_match_tindir);
722 }
723 fip = sp->fip;
724 if (fip->fi_nblocks != 0) {
725 sp->fip = (FINFO*)((caddr_t)fip + sizeof(struct finfo) +
726 sizeof(ufs_daddr_t) * (fip->fi_nblocks-1));
727 sp->start_lbp = &sp->fip->fi_blocks[0];
728 } else {
729 sp->sum_bytes_left += sizeof(FINFO) - sizeof(ufs_daddr_t);
730 --((SEGSUM *)(sp->segsum))->ss_nfinfo;
731 }
732 }
733
734 int
735 lfs_writeinode(struct lfs *fs, struct segment *sp, struct inode *ip)
736 {
737 struct buf *bp, *ibp;
738 struct dinode *cdp;
739 IFILE *ifp;
740 SEGUSE *sup;
741 ufs_daddr_t daddr;
742 daddr_t *daddrp;
743 ino_t ino;
744 int error, i, ndx, fsb = 0;
745 int redo_ifile = 0;
746 struct timespec ts;
747 int gotblk = 0;
748
749 if (!(ip->i_flag & IN_ALLMOD))
750 return (0);
751
752 /* Allocate a new inode block if necessary. */
753 if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) && sp->ibp == NULL) {
754 /* Allocate a new segment if necessary. */
755 if (sp->seg_bytes_left < fs->lfs_ibsize ||
756 sp->sum_bytes_left < sizeof(ufs_daddr_t))
757 (void) lfs_writeseg(fs, sp);
758
759 /* Get next inode block. */
760 daddr = fs->lfs_offset;
761 fs->lfs_offset += btofsb(fs, fs->lfs_ibsize);
762 sp->ibp = *sp->cbpp++ =
763 getblk(VTOI(fs->lfs_ivnode)->i_devvp, fsbtodb(fs, daddr),
764 fs->lfs_ibsize, 0, 0);
765 gotblk++;
766
767 /* Zero out inode numbers */
768 for (i = 0; i < INOPB(fs); ++i)
769 ((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
770
771 ++sp->start_bpp;
772 fs->lfs_avail -= btofsb(fs, fs->lfs_ibsize);
773 /* Set remaining space counters. */
774 sp->seg_bytes_left -= fs->lfs_ibsize;
775 sp->sum_bytes_left -= sizeof(ufs_daddr_t);
776 ndx = fs->lfs_sumsize / sizeof(ufs_daddr_t) -
777 sp->ninodes / INOPB(fs) - 1;
778 ((ufs_daddr_t *)(sp->segsum))[ndx] = daddr;
779 }
780
781 /* Update the inode times and copy the inode onto the inode page. */
782 TIMEVAL_TO_TIMESPEC(&time, &ts);
783 LFS_ITIMES(ip, &ts, &ts, &ts);
784
785 /*
786 * If this is the Ifile, and we've already written the Ifile in this
787 * partial segment, just overwrite it (it's not on disk yet) and
788 * continue.
789 *
790 * XXX we know that the bp that we get the second time around has
791 * already been gathered.
792 */
793 if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
794 *(sp->idp) = ip->i_din.ffs_din;
795 return 0;
796 }
797
798 bp = sp->ibp;
799 cdp = ((struct dinode *)bp->b_data) + (sp->ninodes % INOPB(fs));
800 *cdp = ip->i_din.ffs_din;
801 #ifdef LFS_IFILE_FRAG_ADDRESSING
802 if (fs->lfs_version > 1)
803 fsb = (sp->ninodes % INOPB(fs)) / INOPF(fs);
804 #endif
805
806 /*
807 * If we are cleaning, ensure that we don't write UNWRITTEN disk
808 * addresses to disk.
809 */
810 if (ip->i_lfs_effnblks != ip->i_ffs_blocks) {
811 #ifdef DEBUG_LFS
812 printf("lfs_writeinode: cleansing ino %d (%d != %d)\n",
813 ip->i_number, ip->i_lfs_effnblks, ip->i_ffs_blocks);
814 #endif
815 for (daddrp = cdp->di_db; daddrp < cdp->di_ib + NIADDR;
816 daddrp++) {
817 if (*daddrp == UNWRITTEN) {
818 #ifdef DEBUG_LFS
819 printf("lfs_writeinode: wiping UNWRITTEN\n");
820 #endif
821 *daddrp = 0;
822 }
823 }
824 }
825
826 if (ip->i_flag & IN_CLEANING)
827 LFS_CLR_UINO(ip, IN_CLEANING);
828 else {
829 /* XXX IN_ALLMOD */
830 LFS_CLR_UINO(ip, IN_ACCESSED | IN_ACCESS | IN_CHANGE |
831 IN_UPDATE);
832 if (ip->i_lfs_effnblks == ip->i_ffs_blocks)
833 LFS_CLR_UINO(ip, IN_MODIFIED);
834 #ifdef DEBUG_LFS
835 else
836 printf("lfs_writeinode: ino %d: real blks=%d, "
837 "eff=%d\n", ip->i_number, ip->i_ffs_blocks,
838 ip->i_lfs_effnblks);
839 #endif
840 }
841
842 if (ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
843 sp->idp = ((struct dinode *)bp->b_data) +
844 (sp->ninodes % INOPB(fs));
845 if (gotblk) {
846 LFS_LOCK_BUF(bp);
847 brelse(bp);
848 }
849
850 /* Increment inode count in segment summary block. */
851 ++((SEGSUM *)(sp->segsum))->ss_ninos;
852
853 /* If this page is full, set flag to allocate a new page. */
854 if (++sp->ninodes % INOPB(fs) == 0)
855 sp->ibp = NULL;
856
857 /*
858 * If updating the ifile, update the super-block. Update the disk
859 * address and access times for this inode in the ifile.
860 */
861 ino = ip->i_number;
862 if (ino == LFS_IFILE_INUM) {
863 daddr = fs->lfs_idaddr;
864 fs->lfs_idaddr = dbtofsb(fs, bp->b_blkno);
865 } else {
866 LFS_IENTRY(ifp, fs, ino, ibp);
867 daddr = ifp->if_daddr;
868 ifp->if_daddr = dbtofsb(fs, bp->b_blkno) + fsb;
869 #ifdef LFS_DEBUG_NEXTFREE
870 if (ino > 3 && ifp->if_nextfree) {
871 vprint("lfs_writeinode",ITOV(ip));
872 printf("lfs_writeinode: updating free ino %d\n",
873 ip->i_number);
874 }
875 #endif
876 error = VOP_BWRITE(ibp); /* Ifile */
877 }
878
879 /*
880 * Account the inode: it no longer belongs to its former segment,
881 * though it will not belong to the new segment until that segment
882 * is actually written.
883 */
884 #ifdef DEBUG
885 /*
886 * The inode's last address should not be in the current partial
887 * segment, except under exceptional circumstances (lfs_writevnodes
888 * had to start over, and in the meantime more blocks were written
889 * to a vnode). Although the previous inode won't be accounted in
890 * su_nbytes until lfs_writeseg, this shouldn't be a problem as we
891 * have more data blocks in the current partial segment.
892 */
893 if (daddr >= fs->lfs_lastpseg && daddr <= dbtofsb(fs, bp->b_blkno))
894 printf("lfs_writeinode: last inode addr in current pseg "
895 "(ino %d daddr 0x%x)\n", ino, daddr);
896 #endif
897 if (daddr != LFS_UNUSED_DADDR) {
898 LFS_SEGENTRY(sup, fs, dtosn(fs, daddr), bp);
899 #ifdef DIAGNOSTIC
900 if (sup->su_nbytes < DINODE_SIZE) {
901 printf("lfs_writeinode: negative bytes "
902 "(segment %d short by %d)\n",
903 dtosn(fs, daddr),
904 (int)DINODE_SIZE - sup->su_nbytes);
905 panic("lfs_writeinode: negative bytes");
906 sup->su_nbytes = DINODE_SIZE;
907 }
908 #endif
909 #ifdef DEBUG_SU_NBYTES
910 printf("seg %d -= %d for ino %d inode\n",
911 dtosn(fs, daddr), DINODE_SIZE, ino);
912 #endif
913 sup->su_nbytes -= DINODE_SIZE;
914 redo_ifile =
915 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
916 error = VOP_BWRITE(bp); /* Ifile */
917 }
918 return (redo_ifile);
919 }
920
921 int
922 lfs_gatherblock(struct segment *sp, struct buf *bp, int *sptr)
923 {
924 struct lfs *fs;
925 int version;
926
927 /*
928 * If full, finish this segment. We may be doing I/O, so
929 * release and reacquire the splbio().
930 */
931 #ifdef DIAGNOSTIC
932 if (sp->vp == NULL)
933 panic ("lfs_gatherblock: Null vp in segment");
934 #endif
935 fs = sp->fs;
936 if (sp->sum_bytes_left < sizeof(ufs_daddr_t) ||
937 sp->seg_bytes_left < bp->b_bcount) {
938 if (sptr)
939 splx(*sptr);
940 lfs_updatemeta(sp);
941
942 version = sp->fip->fi_version;
943 (void) lfs_writeseg(fs, sp);
944
945 sp->fip->fi_version = version;
946 sp->fip->fi_ino = VTOI(sp->vp)->i_number;
947 /* Add the current file to the segment summary. */
948 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
949 sp->sum_bytes_left -=
950 sizeof(struct finfo) - sizeof(ufs_daddr_t);
951
952 if (sptr)
953 *sptr = splbio();
954 return (1);
955 }
956
957 #ifdef DEBUG
958 if (bp->b_flags & B_GATHERED) {
959 printf("lfs_gatherblock: already gathered! Ino %d, lbn %d\n",
960 sp->fip->fi_ino, bp->b_lblkno);
961 return (0);
962 }
963 #endif
964 /* Insert into the buffer list, update the FINFO block. */
965 bp->b_flags |= B_GATHERED;
966 *sp->cbpp++ = bp;
967 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
968
969 sp->sum_bytes_left -= sizeof(ufs_daddr_t);
970 sp->seg_bytes_left -= bp->b_bcount;
971 return (0);
972 }
973
974 int
975 lfs_gather(struct lfs *fs, struct segment *sp, struct vnode *vp, int (*match)(struct lfs *, struct buf *))
976 {
977 struct buf *bp;
978 int s, count = 0;
979
980 sp->vp = vp;
981 s = splbio();
982
983 #ifndef LFS_NO_BACKBUF_HACK
984 loop: for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
985 #else /* LFS_NO_BACKBUF_HACK */
986 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
987 # define BUF_OFFSET (((void *)&bp->b_vnbufs.le_next) - (void *)bp)
988 # define BACK_BUF(BP) ((struct buf *)(((void *)BP->b_vnbufs.le_prev) - BUF_OFFSET))
989 # define BEG_OF_LIST ((struct buf *)(((void *)&vp->v_dirtyblkhd.lh_first) - BUF_OFFSET))
990 /* Find last buffer. */
991 loop: for (bp = vp->v_dirtyblkhd.lh_first; bp && bp->b_vnbufs.le_next != NULL;
992 bp = bp->b_vnbufs.le_next);
993 for (; bp && bp != BEG_OF_LIST; bp = BACK_BUF(bp)) {
994 #endif /* LFS_NO_BACKBUF_HACK */
995 if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp))
996 continue;
997 if (vp->v_type == VBLK) {
998 /* For block devices, just write the blocks. */
999 /* XXX Do we really need to even do this? */
1000 #ifdef DEBUG_LFS
1001 if (count == 0)
1002 printf("BLK(");
1003 printf(".");
1004 #endif
1005 /* Get the block before bwrite, so we don't corrupt the free list */
1006 bp->b_flags |= B_BUSY;
1007 bremfree(bp);
1008 bwrite(bp);
1009 } else {
1010 #ifdef DIAGNOSTIC
1011 if ((bp->b_flags & (B_CALL|B_INVAL)) == B_INVAL) {
1012 printf("lfs_gather: lbn %d is B_INVAL\n",
1013 bp->b_lblkno);
1014 VOP_PRINT(bp->b_vp);
1015 }
1016 if (!(bp->b_flags & B_DELWRI))
1017 panic("lfs_gather: bp not B_DELWRI");
1018 if (!(bp->b_flags & B_LOCKED)) {
1019 printf("lfs_gather: lbn %d blk %d"
1020 " not B_LOCKED\n", bp->b_lblkno,
1021 dbtofsb(fs, bp->b_blkno));
1022 VOP_PRINT(bp->b_vp);
1023 panic("lfs_gather: bp not B_LOCKED");
1024 }
1025 #endif
1026 if (lfs_gatherblock(sp, bp, &s)) {
1027 goto loop;
1028 }
1029 }
1030 count++;
1031 }
1032 splx(s);
1033 #ifdef DEBUG_LFS
1034 if (vp->v_type == VBLK && count)
1035 printf(")\n");
1036 #endif
1037 lfs_updatemeta(sp);
1038 sp->vp = NULL;
1039 return count;
1040 }
1041
1042 /*
1043 * Update the metadata that points to the blocks listed in the FINFO
1044 * array.
1045 */
1046 void
1047 lfs_updatemeta(struct segment *sp)
1048 {
1049 SEGUSE *sup;
1050 struct buf *bp;
1051 struct lfs *fs;
1052 struct vnode *vp;
1053 struct indir a[NIADDR + 2], *ap;
1054 struct inode *ip;
1055 ufs_daddr_t daddr, lbn, off;
1056 daddr_t ooff;
1057 int error, i, nblocks, num;
1058 int bb;
1059
1060 vp = sp->vp;
1061 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
1062 if (nblocks < 0)
1063 panic("This is a bad thing\n");
1064 if (vp == NULL || nblocks == 0)
1065 return;
1066
1067 /* Sort the blocks. */
1068 /*
1069 * XXX KS - We have to sort even if the blocks come from the
1070 * cleaner, because there might be other pending blocks on the
1071 * same inode...and if we don't sort, and there are fragments
1072 * present, blocks may be written in the wrong place.
1073 */
1074 /* if (!(sp->seg_flags & SEGM_CLEAN)) */
1075 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
1076
1077 /*
1078 * Record the length of the last block in case it's a fragment.
1079 * If there are indirect blocks present, they sort last. An
1080 * indirect block will be lfs_bsize and its presence indicates
1081 * that you cannot have fragments.
1082 */
1083 sp->fip->fi_lastlength = sp->start_bpp[nblocks - 1]->b_bcount;
1084
1085 /*
1086 * Assign disk addresses, and update references to the logical
1087 * block and the segment usage information.
1088 */
1089 fs = sp->fs;
1090 for (i = nblocks; i--; ++sp->start_bpp) {
1091 lbn = *sp->start_lbp++;
1092
1093 (*sp->start_bpp)->b_blkno = fsbtodb(fs, fs->lfs_offset);
1094 off = fs->lfs_offset;
1095 if ((*sp->start_bpp)->b_blkno == (*sp->start_bpp)->b_lblkno) {
1096 printf("lfs_updatemeta: ino %d blk %d"
1097 " has same lbn and daddr\n",
1098 VTOI(vp)->i_number, off);
1099 }
1100 #ifdef DIAGNOSTIC
1101 if ((*sp->start_bpp)->b_bcount < fs->lfs_bsize && i != 0)
1102 panic("lfs_updatemeta: fragment is not last block\n");
1103 #endif
1104 bb = fragstofsb(fs, numfrags(fs, (*sp->start_bpp)->b_bcount));
1105 fs->lfs_offset += bb;
1106 error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL);
1107 if (daddr > 0)
1108 daddr = dbtofsb(fs, daddr);
1109 if (error)
1110 panic("lfs_updatemeta: ufs_bmaparray %d", error);
1111 ip = VTOI(vp);
1112 switch (num) {
1113 case 0:
1114 ooff = ip->i_ffs_db[lbn];
1115 #ifdef DEBUG
1116 if (ooff == 0) {
1117 printf("lfs_updatemeta[1]: warning: writing "
1118 "ino %d lbn %d at 0x%x, was 0x0\n",
1119 ip->i_number, lbn, off);
1120 }
1121 #endif
1122 if (ooff == UNWRITTEN)
1123 ip->i_ffs_blocks += bb;
1124 ip->i_ffs_db[lbn] = off;
1125 break;
1126 case 1:
1127 ooff = ip->i_ffs_ib[a[0].in_off];
1128 #ifdef DEBUG
1129 if (ooff == 0) {
1130 printf("lfs_updatemeta[2]: warning: writing "
1131 "ino %d lbn %d at 0x%x, was 0x0\n",
1132 ip->i_number, lbn, off);
1133 }
1134 #endif
1135 if (ooff == UNWRITTEN)
1136 ip->i_ffs_blocks += bb;
1137 ip->i_ffs_ib[a[0].in_off] = off;
1138 break;
1139 default:
1140 ap = &a[num - 1];
1141 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
1142 panic("lfs_updatemeta: bread bno %d",
1143 ap->in_lbn);
1144
1145 ooff = ((ufs_daddr_t *)bp->b_data)[ap->in_off];
1146 #if DEBUG
1147 if (ooff == 0) {
1148 printf("lfs_updatemeta[3]: warning: writing "
1149 "ino %d lbn %d at 0x%x, was 0x0\n",
1150 ip->i_number, lbn, off);
1151 }
1152 #endif
1153 if (ooff == UNWRITTEN)
1154 ip->i_ffs_blocks += bb;
1155 ((ufs_daddr_t *)bp->b_data)[ap->in_off] = off;
1156 (void) VOP_BWRITE(bp);
1157 }
1158 #ifdef DEBUG
1159 if (daddr >= fs->lfs_lastpseg && daddr <= off) {
1160 printf("lfs_updatemeta: ino %d, lbn %d, addr = %x "
1161 "in same pseg\n", VTOI(sp->vp)->i_number,
1162 (*sp->start_bpp)->b_lblkno, daddr);
1163 }
1164 #endif
1165 /* Update segment usage information. */
1166 if (daddr > 0) {
1167 LFS_SEGENTRY(sup, fs, dtosn(fs, daddr), bp);
1168 #ifdef DIAGNOSTIC
1169 if (sup->su_nbytes < (*sp->start_bpp)->b_bcount) {
1170 /* XXX -- Change to a panic. */
1171 printf("lfs_updatemeta: negative bytes "
1172 "(segment %d short by %ld)\n",
1173 dtosn(fs, daddr),
1174 (*sp->start_bpp)->b_bcount -
1175 sup->su_nbytes);
1176 printf("lfs_updatemeta: ino %d, lbn %d, "
1177 "addr = 0x%x\n", VTOI(sp->vp)->i_number,
1178 (*sp->start_bpp)->b_lblkno, daddr);
1179 panic("lfs_updatemeta: negative bytes");
1180 sup->su_nbytes = (*sp->start_bpp)->b_bcount;
1181 }
1182 #endif
1183 #ifdef DEBUG_SU_NBYTES
1184 printf("seg %d -= %ld for ino %d lbn %d db 0x%x\n",
1185 dtosn(fs, daddr), (*sp->start_bpp)->b_bcount,
1186 VTOI(sp->vp)->i_number,
1187 (*sp->start_bpp)->b_lblkno, daddr);
1188 #endif
1189 sup->su_nbytes -= (*sp->start_bpp)->b_bcount;
1190 error = VOP_BWRITE(bp); /* Ifile */
1191 }
1192 }
1193 }
1194
1195 /*
1196 * Start a new segment.
1197 */
1198 int
1199 lfs_initseg(struct lfs *fs)
1200 {
1201 struct segment *sp;
1202 SEGUSE *sup;
1203 SEGSUM *ssp;
1204 struct buf *bp;
1205 int repeat;
1206
1207 sp = fs->lfs_sp;
1208
1209 repeat = 0;
1210 /* Advance to the next segment. */
1211 if (!LFS_PARTIAL_FITS(fs)) {
1212 /* lfs_avail eats the remaining space */
1213 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset -
1214 fs->lfs_curseg);
1215 /* Wake up any cleaning procs waiting on this file system. */
1216 wakeup(&lfs_allclean_wakeup);
1217 wakeup(&fs->lfs_nextseg);
1218 lfs_newseg(fs);
1219 repeat = 1;
1220 fs->lfs_offset = fs->lfs_curseg;
1221 sp->seg_number = dtosn(fs, fs->lfs_curseg);
1222 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg);
1223 /*
1224 * If the segment contains a superblock, update the offset
1225 * and summary address to skip over it.
1226 */
1227 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1228 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
1229 fs->lfs_offset += btofsb(fs, LFS_SBPAD);
1230 sp->seg_bytes_left -= LFS_SBPAD;
1231 }
1232 brelse(bp);
1233 /* Segment zero could also contain the labelpad */
1234 if (fs->lfs_version > 1 && sp->seg_number == 0 &&
1235 fs->lfs_start < btofsb(fs, LFS_LABELPAD)) {
1236 fs->lfs_offset += btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
1237 sp->seg_bytes_left -= LFS_LABELPAD - fsbtob(fs, fs->lfs_start);
1238 }
1239 } else {
1240 sp->seg_number = dtosn(fs, fs->lfs_curseg);
1241 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg -
1242 (fs->lfs_offset - fs->lfs_curseg));
1243 }
1244 fs->lfs_lastpseg = fs->lfs_offset;
1245
1246 sp->fs = fs;
1247 sp->ibp = NULL;
1248 sp->idp = NULL;
1249 sp->ninodes = 0;
1250
1251 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
1252 sp->cbpp = sp->bpp;
1253 *sp->cbpp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp,
1254 fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize);
1255 sp->segsum = (*sp->cbpp)->b_data;
1256 bzero(sp->segsum, fs->lfs_sumsize);
1257 sp->start_bpp = ++sp->cbpp;
1258 fs->lfs_offset += btofsb(fs, fs->lfs_sumsize);
1259
1260 /* Set point to SEGSUM, initialize it. */
1261 ssp = sp->segsum;
1262 ssp->ss_next = fs->lfs_nextseg;
1263 ssp->ss_nfinfo = ssp->ss_ninos = 0;
1264 ssp->ss_magic = SS_MAGIC;
1265
1266 /* Set pointer to first FINFO, initialize it. */
1267 sp->fip = (struct finfo *)((caddr_t)sp->segsum + SEGSUM_SIZE(fs));
1268 sp->fip->fi_nblocks = 0;
1269 sp->start_lbp = &sp->fip->fi_blocks[0];
1270 sp->fip->fi_lastlength = 0;
1271
1272 sp->seg_bytes_left -= fs->lfs_sumsize;
1273 sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs);
1274
1275 return (repeat);
1276 }
1277
1278 /*
1279 * Return the next segment to write.
1280 */
1281 void
1282 lfs_newseg(struct lfs *fs)
1283 {
1284 CLEANERINFO *cip;
1285 SEGUSE *sup;
1286 struct buf *bp;
1287 int curseg, isdirty, sn;
1288
1289 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
1290 #ifdef DEBUG_SU_NBYTES
1291 printf("lfs_newseg: seg %d := 0 in newseg\n", /* XXXDEBUG */
1292 dtosn(fs, fs->lfs_nextseg)); /* XXXDEBUG */
1293 #endif
1294 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1295 sup->su_nbytes = 0;
1296 sup->su_nsums = 0;
1297 sup->su_ninos = 0;
1298 (void) VOP_BWRITE(bp); /* Ifile */
1299
1300 LFS_CLEANERINFO(cip, fs, bp);
1301 --cip->clean;
1302 ++cip->dirty;
1303 fs->lfs_nclean = cip->clean;
1304 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1305
1306 fs->lfs_lastseg = fs->lfs_curseg;
1307 fs->lfs_curseg = fs->lfs_nextseg;
1308 for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) {
1309 sn = (sn + 1) % fs->lfs_nseg;
1310 if (sn == curseg)
1311 panic("lfs_nextseg: no clean segments");
1312 LFS_SEGENTRY(sup, fs, sn, bp);
1313 isdirty = sup->su_flags & SEGUSE_DIRTY;
1314 brelse(bp);
1315 if (!isdirty)
1316 break;
1317 }
1318
1319 ++fs->lfs_nactive;
1320 fs->lfs_nextseg = sntod(fs, sn);
1321 if (lfs_dostats) {
1322 ++lfs_stats.segsused;
1323 }
1324 }
1325
1326 int
1327 lfs_writeseg(struct lfs *fs, struct segment *sp)
1328 {
1329 struct buf **bpp, *bp, *cbp, *newbp;
1330 SEGUSE *sup;
1331 SEGSUM *ssp;
1332 dev_t i_dev;
1333 char *datap, *dp;
1334 int do_again, i, nblocks, s;
1335 size_t el_size;
1336 #ifdef LFS_TRACK_IOS
1337 int j;
1338 #endif
1339 int (*strategy)(void *);
1340 struct vop_strategy_args vop_strategy_a;
1341 u_short ninos;
1342 struct vnode *devvp;
1343 char *p;
1344 struct vnode *vp;
1345 struct inode *ip;
1346 daddr_t *daddrp;
1347 int changed;
1348 #if defined(DEBUG) && defined(LFS_PROPELLER)
1349 static int propeller;
1350 char propstring[4] = "-\\|/";
1351
1352 printf("%c\b",propstring[propeller++]);
1353 if (propeller == 4)
1354 propeller = 0;
1355 #endif
1356
1357 /*
1358 * If there are no buffers other than the segment summary to write
1359 * and it is not a checkpoint, don't do anything. On a checkpoint,
1360 * even if there aren't any buffers, you need to write the superblock.
1361 */
1362 if ((nblocks = sp->cbpp - sp->bpp) == 1)
1363 return (0);
1364
1365 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
1366 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
1367
1368 /* Update the segment usage information. */
1369 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1370
1371 /* Loop through all blocks, except the segment summary. */
1372 for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
1373 if ((*bpp)->b_vp != devvp) {
1374 sup->su_nbytes += (*bpp)->b_bcount;
1375 #ifdef DEBUG_SU_NBYTES
1376 printf("seg %d += %ld for ino %d lbn %d db 0x%x\n",
1377 sp->seg_number, (*bpp)->b_bcount,
1378 VTOI((*bpp)->b_vp)->i_number,
1379 (*bpp)->b_lblkno, (*bpp)->b_blkno);
1380 #endif
1381 }
1382 }
1383
1384 ssp = (SEGSUM *)sp->segsum;
1385
1386 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
1387 #ifdef DEBUG_SU_NBYTES
1388 printf("seg %d += %d for %d inodes\n", /* XXXDEBUG */
1389 sp->seg_number, ssp->ss_ninos * DINODE_SIZE,
1390 ssp->ss_ninos);
1391 #endif
1392 sup->su_nbytes += ssp->ss_ninos * DINODE_SIZE;
1393 /* sup->su_nbytes += fs->lfs_sumsize; */
1394 if (fs->lfs_version == 1)
1395 sup->su_olastmod = time.tv_sec;
1396 else
1397 sup->su_lastmod = time.tv_sec;
1398 sup->su_ninos += ninos;
1399 ++sup->su_nsums;
1400 fs->lfs_dmeta += (btofsb(fs, fs->lfs_sumsize) + btofsb(fs, ninos *
1401 fs->lfs_ibsize));
1402 fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize);
1403
1404 do_again = !(bp->b_flags & B_GATHERED);
1405 (void)VOP_BWRITE(bp); /* Ifile */
1406 /*
1407 * Mark blocks B_BUSY, to prevent then from being changed between
1408 * the checksum computation and the actual write.
1409 *
1410 * If we are cleaning, check indirect blocks for UNWRITTEN, and if
1411 * there are any, replace them with copies that have UNASSIGNED
1412 * instead.
1413 */
1414 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
1415 ++bpp;
1416 if ((*bpp)->b_flags & B_CALL)
1417 continue;
1418 bp = *bpp;
1419 again:
1420 s = splbio();
1421 if (bp->b_flags & B_BUSY) {
1422 #ifdef DEBUG
1423 printf("lfs_writeseg: avoiding potential data "
1424 "summary corruption for ino %d, lbn %d\n",
1425 VTOI(bp->b_vp)->i_number, bp->b_lblkno);
1426 #endif
1427 bp->b_flags |= B_WANTED;
1428 tsleep(bp, (PRIBIO + 1), "lfs_writeseg", 0);
1429 splx(s);
1430 goto again;
1431 }
1432 bp->b_flags |= B_BUSY;
1433 splx(s);
1434 /* Check and replace indirect block UNWRITTEN bogosity */
1435 if (bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp &&
1436 VTOI(bp->b_vp)->i_ffs_blocks !=
1437 VTOI(bp->b_vp)->i_lfs_effnblks) {
1438 #ifdef DEBUG_LFS
1439 printf("lfs_writeseg: cleansing ino %d (%d != %d)\n",
1440 VTOI(bp->b_vp)->i_number,
1441 VTOI(bp->b_vp)->i_lfs_effnblks,
1442 VTOI(bp->b_vp)->i_ffs_blocks);
1443 #endif
1444 /* Make a copy we'll make changes to */
1445 newbp = lfs_newbuf(fs, bp->b_vp, bp->b_lblkno,
1446 bp->b_bcount);
1447 newbp->b_blkno = bp->b_blkno;
1448 memcpy(newbp->b_data, bp->b_data,
1449 newbp->b_bcount);
1450 *bpp = newbp;
1451
1452 changed = 0;
1453 for (daddrp = (daddr_t *)(newbp->b_data);
1454 daddrp < (daddr_t *)(newbp->b_data +
1455 newbp->b_bcount); daddrp++) {
1456 if (*daddrp == UNWRITTEN) {
1457 ++changed;
1458 #ifdef DEBUG_LFS
1459 printf("lfs_writeseg: replacing UNWRITTEN\n");
1460 #endif
1461 *daddrp = 0;
1462 }
1463 }
1464 /*
1465 * Get rid of the old buffer. Don't mark it clean,
1466 * though, if it still has dirty data on it.
1467 */
1468 if (changed) {
1469 bp->b_flags &= ~(B_ERROR | B_GATHERED);
1470 if (bp->b_flags & B_CALL) {
1471 lfs_freebuf(bp);
1472 bp = NULL;
1473 } else {
1474 /* Still on free list, leave it there */
1475 s = splbio();
1476 bp->b_flags &= ~B_BUSY;
1477 if (bp->b_flags & B_WANTED)
1478 wakeup(bp);
1479 splx(s);
1480 /*
1481 * We have to re-decrement lfs_avail
1482 * since this block is going to come
1483 * back around to us in the next
1484 * segment.
1485 */
1486 fs->lfs_avail -= btofsb(fs, bp->b_bcount);
1487 }
1488 } else {
1489 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
1490 B_GATHERED);
1491 LFS_UNLOCK_BUF(bp);
1492 if (bp->b_flags & B_CALL) {
1493 lfs_freebuf(bp);
1494 bp = NULL;
1495 } else {
1496 bremfree(bp);
1497 bp->b_flags |= B_DONE;
1498 reassignbuf(bp, bp->b_vp);
1499 brelse(bp);
1500 }
1501 }
1502
1503 }
1504 }
1505 /*
1506 * Compute checksum across data and then across summary; the first
1507 * block (the summary block) is skipped. Set the create time here
1508 * so that it's guaranteed to be later than the inode mod times.
1509 *
1510 * XXX
1511 * Fix this to do it inline, instead of malloc/copy.
1512 */
1513 if (fs->lfs_version == 1)
1514 el_size = sizeof(u_long);
1515 else
1516 el_size = sizeof(u_int32_t);
1517 datap = dp = malloc(nblocks * el_size, M_SEGMENT, M_WAITOK);
1518 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
1519 if (((*++bpp)->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
1520 if (copyin((*bpp)->b_saveaddr, dp, el_size))
1521 panic("lfs_writeseg: copyin failed [1]: "
1522 "ino %d blk %d",
1523 VTOI((*bpp)->b_vp)->i_number,
1524 (*bpp)->b_lblkno);
1525 } else
1526 memcpy(dp, (*bpp)->b_data, el_size);
1527 dp += el_size;
1528 }
1529 if (fs->lfs_version == 1)
1530 ssp->ss_ocreate = time.tv_sec;
1531 else {
1532 ssp->ss_create = time.tv_sec;
1533 ssp->ss_serial = ++fs->lfs_serial;
1534 ssp->ss_ident = fs->lfs_ident;
1535 }
1536 ssp->ss_datasum = cksum(datap, (nblocks - 1) * el_size);
1537 ssp->ss_sumsum =
1538 cksum(&ssp->ss_datasum, fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
1539 free(datap, M_SEGMENT);
1540 datap = dp = NULL;
1541 #ifdef DIAGNOSTIC
1542 if (fs->lfs_bfree < btofsb(fs, ninos * fs->lfs_ibsize) + btofsb(fs, fs->lfs_sumsize))
1543 panic("lfs_writeseg: No diskspace for summary");
1544 #endif
1545 fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) +
1546 btofsb(fs, fs->lfs_sumsize));
1547
1548 strategy = devvp->v_op[VOFFSET(vop_strategy)];
1549
1550 /*
1551 * When we simply write the blocks we lose a rotation for every block
1552 * written. To avoid this problem, we allocate memory in chunks, copy
1553 * the buffers into the chunk and write the chunk. CHUNKSIZE is the
1554 * largest size I/O devices can handle.
1555 * When the data is copied to the chunk, turn off the B_LOCKED bit
1556 * and brelse the buffer (which will move them to the LRU list). Add
1557 * the B_CALL flag to the buffer header so we can count I/O's for the
1558 * checkpoints and so we can release the allocated memory.
1559 *
1560 * XXX
1561 * This should be removed if the new virtual memory system allows us to
1562 * easily make the buffers contiguous in kernel memory and if that's
1563 * fast enough.
1564 */
1565
1566 #define CHUNKSIZE MAXPHYS
1567
1568 if (devvp == NULL)
1569 panic("devvp is NULL");
1570 for (bpp = sp->bpp,i = nblocks; i;) {
1571 cbp = lfs_newbuf(fs, devvp, (*bpp)->b_blkno, CHUNKSIZE);
1572 cbp->b_dev = i_dev;
1573 cbp->b_flags |= B_ASYNC | B_BUSY;
1574 cbp->b_bcount = 0;
1575
1576 #ifdef DIAGNOSTIC
1577 if (dtosn(fs, dbtofsb(fs, (*bpp)->b_blkno) + btofsb(fs, (*bpp)->b_bcount) - 1) !=
1578 dtosn(fs, dbtofsb(fs, cbp->b_blkno))) {
1579 panic("lfs_writeseg: Segment overwrite");
1580 }
1581 #endif
1582
1583 s = splbio();
1584 if (fs->lfs_iocount >= LFS_THROTTLE) {
1585 tsleep(&fs->lfs_iocount, PRIBIO+1, "lfs throttle", 0);
1586 }
1587 ++fs->lfs_iocount;
1588 #ifdef LFS_TRACK_IOS
1589 for (j = 0; j < LFS_THROTTLE; j++) {
1590 if (fs->lfs_pending[j] == LFS_UNUSED_DADDR) {
1591 fs->lfs_pending[j] = dbtofsb(fs, cbp->b_blkno);
1592 break;
1593 }
1594 }
1595 #endif /* LFS_TRACK_IOS */
1596 for (p = cbp->b_data; i && cbp->b_bcount < CHUNKSIZE; i--) {
1597 bp = *bpp;
1598
1599 if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
1600 break;
1601
1602 /*
1603 * Fake buffers from the cleaner are marked as B_INVAL.
1604 * We need to copy the data from user space rather than
1605 * from the buffer indicated.
1606 * XXX == what do I do on an error?
1607 */
1608 if ((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
1609 if (copyin(bp->b_saveaddr, p, bp->b_bcount))
1610 panic("lfs_writeseg: copyin failed [2]");
1611 } else
1612 bcopy(bp->b_data, p, bp->b_bcount);
1613 p += bp->b_bcount;
1614 cbp->b_bcount += bp->b_bcount;
1615 LFS_UNLOCK_BUF(bp);
1616 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
1617 B_GATHERED);
1618 vp = bp->b_vp;
1619 if (bp->b_flags & B_CALL) {
1620 /* if B_CALL, it was created with newbuf */
1621 lfs_freebuf(bp);
1622 bp = NULL;
1623 } else {
1624 bremfree(bp);
1625 bp->b_flags |= B_DONE;
1626 if (vp)
1627 reassignbuf(bp, vp);
1628 brelse(bp);
1629 }
1630
1631 bpp++;
1632
1633 /*
1634 * If this is the last block for this vnode, but
1635 * there are other blocks on its dirty list,
1636 * set IN_MODIFIED/IN_CLEANING depending on what
1637 * sort of block. Only do this for our mount point,
1638 * not for, e.g., inode blocks that are attached to
1639 * the devvp.
1640 * XXX KS - Shouldn't we set *both* if both types
1641 * of blocks are present (traverse the dirty list?)
1642 */
1643 if ((i == 1 ||
1644 (i > 1 && vp && *bpp && (*bpp)->b_vp != vp)) &&
1645 (bp = vp->v_dirtyblkhd.lh_first) != NULL &&
1646 vp->v_mount == fs->lfs_ivnode->v_mount)
1647 {
1648 ip = VTOI(vp);
1649 #ifdef DEBUG_LFS
1650 printf("lfs_writeseg: marking ino %d\n",
1651 ip->i_number);
1652 #endif
1653 if (bp->b_flags & B_CALL)
1654 LFS_SET_UINO(ip, IN_CLEANING);
1655 else
1656 LFS_SET_UINO(ip, IN_MODIFIED);
1657 }
1658 wakeup(vp);
1659 }
1660 ++cbp->b_vp->v_numoutput;
1661 splx(s);
1662 /*
1663 * XXXX This is a gross and disgusting hack. Since these
1664 * buffers are physically addressed, they hang off the
1665 * device vnode (devvp). As a result, they have no way
1666 * of getting to the LFS superblock or lfs structure to
1667 * keep track of the number of I/O's pending. So, I am
1668 * going to stuff the fs into the saveaddr field of
1669 * the buffer (yuk).
1670 */
1671 cbp->b_saveaddr = (caddr_t)fs;
1672 vop_strategy_a.a_desc = VDESC(vop_strategy);
1673 vop_strategy_a.a_bp = cbp;
1674 (strategy)(&vop_strategy_a);
1675 }
1676 #if 1 || defined(DEBUG)
1677 /*
1678 * After doing a big write, we recalculate how many buffers are
1679 * really still left on the locked queue.
1680 */
1681 s = splbio();
1682 lfs_countlocked(&locked_queue_count, &locked_queue_bytes);
1683 splx(s);
1684 wakeup(&locked_queue_count);
1685 #endif /* 1 || DEBUG */
1686 if (lfs_dostats) {
1687 ++lfs_stats.psegwrites;
1688 lfs_stats.blocktot += nblocks - 1;
1689 if (fs->lfs_sp->seg_flags & SEGM_SYNC)
1690 ++lfs_stats.psyncwrites;
1691 if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
1692 ++lfs_stats.pcleanwrites;
1693 lfs_stats.cleanblocks += nblocks - 1;
1694 }
1695 }
1696 return (lfs_initseg(fs) || do_again);
1697 }
1698
1699 void
1700 lfs_writesuper(struct lfs *fs, daddr_t daddr)
1701 {
1702 struct buf *bp;
1703 dev_t i_dev;
1704 int (*strategy)(void *);
1705 int s;
1706 struct vop_strategy_args vop_strategy_a;
1707
1708 /*
1709 * If we can write one superblock while another is in
1710 * progress, we risk not having a complete checkpoint if we crash.
1711 * So, block here if a superblock write is in progress.
1712 */
1713 s = splbio();
1714 while (fs->lfs_sbactive) {
1715 tsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0);
1716 }
1717 fs->lfs_sbactive = daddr;
1718 splx(s);
1719 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
1720 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
1721
1722 /* Set timestamp of this version of the superblock */
1723 if (fs->lfs_version == 1)
1724 fs->lfs_otstamp = time.tv_sec;
1725 fs->lfs_tstamp = time.tv_sec;
1726
1727 /* Checksum the superblock and copy it into a buffer. */
1728 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
1729 bp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp, fsbtodb(fs, daddr), LFS_SBPAD);
1730 *(struct dlfs *)bp->b_data = fs->lfs_dlfs;
1731
1732 bp->b_dev = i_dev;
1733 bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
1734 bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
1735 bp->b_iodone = lfs_supercallback;
1736 /* XXX KS - same nasty hack as above */
1737 bp->b_saveaddr = (caddr_t)fs;
1738
1739 vop_strategy_a.a_desc = VDESC(vop_strategy);
1740 vop_strategy_a.a_bp = bp;
1741 s = splbio();
1742 ++bp->b_vp->v_numoutput;
1743 ++fs->lfs_iocount;
1744 splx(s);
1745 (strategy)(&vop_strategy_a);
1746 }
1747
1748 /*
1749 * Logical block number match routines used when traversing the dirty block
1750 * chain.
1751 */
1752 int
1753 lfs_match_fake(struct lfs *fs, struct buf *bp)
1754 {
1755 return (bp->b_flags & B_CALL);
1756 }
1757
1758 int
1759 lfs_match_data(struct lfs *fs, struct buf *bp)
1760 {
1761 return (bp->b_lblkno >= 0);
1762 }
1763
1764 int
1765 lfs_match_indir(struct lfs *fs, struct buf *bp)
1766 {
1767 int lbn;
1768
1769 lbn = bp->b_lblkno;
1770 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
1771 }
1772
1773 int
1774 lfs_match_dindir(struct lfs *fs, struct buf *bp)
1775 {
1776 int lbn;
1777
1778 lbn = bp->b_lblkno;
1779 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
1780 }
1781
1782 int
1783 lfs_match_tindir(struct lfs *fs, struct buf *bp)
1784 {
1785 int lbn;
1786
1787 lbn = bp->b_lblkno;
1788 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
1789 }
1790
1791 /*
1792 * XXX - The only buffers that are going to hit these functions are the
1793 * segment write blocks, or the segment summaries, or the superblocks.
1794 *
1795 * All of the above are created by lfs_newbuf, and so do not need to be
1796 * released via brelse.
1797 */
1798 void
1799 lfs_callback(struct buf *bp)
1800 {
1801 struct lfs *fs;
1802 #ifdef LFS_TRACK_IOS
1803 int j;
1804 #endif
1805
1806 fs = (struct lfs *)bp->b_saveaddr;
1807 #ifdef DIAGNOSTIC
1808 if (fs->lfs_iocount == 0)
1809 panic("lfs_callback: zero iocount\n");
1810 #endif
1811 if (--fs->lfs_iocount < LFS_THROTTLE)
1812 wakeup(&fs->lfs_iocount);
1813 #ifdef LFS_TRACK_IOS
1814 for (j = 0; j < LFS_THROTTLE; j++) {
1815 if (fs->lfs_pending[j] == dbtofsb(fs, bp->b_blkno)) {
1816 fs->lfs_pending[j] = LFS_UNUSED_DADDR;
1817 wakeup(&(fs->lfs_pending[j]));
1818 break;
1819 }
1820 }
1821 #endif /* LFS_TRACK_IOS */
1822
1823 lfs_freebuf(bp);
1824 }
1825
1826 void
1827 lfs_supercallback(struct buf *bp)
1828 {
1829 struct lfs *fs;
1830
1831 fs = (struct lfs *)bp->b_saveaddr;
1832 fs->lfs_sbactive = 0;
1833 wakeup(&fs->lfs_sbactive);
1834 if (--fs->lfs_iocount < LFS_THROTTLE)
1835 wakeup(&fs->lfs_iocount);
1836 lfs_freebuf(bp);
1837 }
1838
1839 /*
1840 * Shellsort (diminishing increment sort) from Data Structures and
1841 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
1842 * see also Knuth Vol. 3, page 84. The increments are selected from
1843 * formula (8), page 95. Roughly O(N^3/2).
1844 */
1845 /*
1846 * This is our own private copy of shellsort because we want to sort
1847 * two parallel arrays (the array of buffer pointers and the array of
1848 * logical block numbers) simultaneously. Note that we cast the array
1849 * of logical block numbers to a unsigned in this routine so that the
1850 * negative block numbers (meta data blocks) sort AFTER the data blocks.
1851 */
1852
1853 void
1854 lfs_shellsort(struct buf **bp_array, ufs_daddr_t *lb_array, int nmemb)
1855 {
1856 static int __rsshell_increments[] = { 4, 1, 0 };
1857 int incr, *incrp, t1, t2;
1858 struct buf *bp_temp;
1859 u_long lb_temp;
1860
1861 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
1862 for (t1 = incr; t1 < nmemb; ++t1)
1863 for (t2 = t1 - incr; t2 >= 0;)
1864 if (lb_array[t2] > lb_array[t2 + incr]) {
1865 lb_temp = lb_array[t2];
1866 lb_array[t2] = lb_array[t2 + incr];
1867 lb_array[t2 + incr] = lb_temp;
1868 bp_temp = bp_array[t2];
1869 bp_array[t2] = bp_array[t2 + incr];
1870 bp_array[t2 + incr] = bp_temp;
1871 t2 -= incr;
1872 } else
1873 break;
1874 }
1875
1876 /*
1877 * Check VXLOCK. Return 1 if the vnode is locked. Otherwise, vget it.
1878 */
1879 int
1880 lfs_vref(struct vnode *vp)
1881 {
1882 /*
1883 * If we return 1 here during a flush, we risk vinvalbuf() not
1884 * being able to flush all of the pages from this vnode, which
1885 * will cause it to panic. So, return 0 if a flush is in progress.
1886 */
1887 if (vp->v_flag & VXLOCK) {
1888 if (IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
1889 return 0;
1890 }
1891 return (1);
1892 }
1893 return (vget(vp, 0));
1894 }
1895
1896 /*
1897 * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
1898 * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
1899 */
1900 void
1901 lfs_vunref(struct vnode *vp)
1902 {
1903 /*
1904 * Analogous to lfs_vref, if the node is flushing, fake it.
1905 */
1906 if ((vp->v_flag & VXLOCK) && IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
1907 return;
1908 }
1909
1910 simple_lock(&vp->v_interlock);
1911 #ifdef DIAGNOSTIC
1912 if (vp->v_usecount <= 0) {
1913 printf("lfs_vunref: inum is %d\n", VTOI(vp)->i_number);
1914 printf("lfs_vunref: flags are 0x%lx\n", (u_long)vp->v_flag);
1915 printf("lfs_vunref: usecount = %ld\n", (long)vp->v_usecount);
1916 panic("lfs_vunref: v_usecount<0");
1917 }
1918 #endif
1919 vp->v_usecount--;
1920 if (vp->v_usecount > 0) {
1921 simple_unlock(&vp->v_interlock);
1922 return;
1923 }
1924 /*
1925 * insert at tail of LRU list
1926 */
1927 simple_lock(&vnode_free_list_slock);
1928 if (vp->v_holdcnt > 0)
1929 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
1930 else
1931 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
1932 simple_unlock(&vnode_free_list_slock);
1933 simple_unlock(&vp->v_interlock);
1934 }
1935
1936 /*
1937 * We use this when we have vnodes that were loaded in solely for cleaning.
1938 * There is no reason to believe that these vnodes will be referenced again
1939 * soon, since the cleaning process is unrelated to normal filesystem
1940 * activity. Putting cleaned vnodes at the tail of the list has the effect
1941 * of flushing the vnode LRU. So, put vnodes that were loaded only for
1942 * cleaning at the head of the list, instead.
1943 */
1944 void
1945 lfs_vunref_head(struct vnode *vp)
1946 {
1947 simple_lock(&vp->v_interlock);
1948 #ifdef DIAGNOSTIC
1949 if (vp->v_usecount == 0) {
1950 panic("lfs_vunref: v_usecount<0");
1951 }
1952 #endif
1953 vp->v_usecount--;
1954 if (vp->v_usecount > 0) {
1955 simple_unlock(&vp->v_interlock);
1956 return;
1957 }
1958 /*
1959 * insert at head of LRU list
1960 */
1961 simple_lock(&vnode_free_list_slock);
1962 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1963 simple_unlock(&vnode_free_list_slock);
1964 simple_unlock(&vp->v_interlock);
1965 }
1966
1967