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