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