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