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