lfs_segment.c revision 1.126 1 /* $NetBSD: lfs_segment.c,v 1.126 2003/07/02 13:43:03 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.126 2003/07/02 13:43:03 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 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 error = lfs_writer_enter(fs, "lfs writer");
587 if (error) {
588 printf("segwrite mysterious error\n");
589 /* XXX why not segunlock? */
590 pool_put(&fs->lfs_bpppool, sp->bpp);
591 sp->bpp = NULL;
592 pool_put(&fs->lfs_segpool, sp);
593 sp = fs->lfs_sp = NULL;
594 return (error);
595 }
596 writer_set = 1;
597 lfs_writevnodes(fs, mp, sp, VN_DIROP);
598 ((SEGSUM *)(sp->segsum))->ss_flags &= ~(SS_CONT);
599 }
600 }
601
602 /*
603 * If we are doing a checkpoint, mark everything since the
604 * last checkpoint as no longer ACTIVE.
605 */
606 if (do_ckp) {
607 segleft = fs->lfs_nseg;
608 curseg = 0;
609 for (n = 0; n < fs->lfs_segtabsz; n++) {
610 dirty = 0;
611 if (bread(fs->lfs_ivnode,
612 fs->lfs_cleansz + n, fs->lfs_bsize, NOCRED, &bp))
613
614 panic("lfs_segwrite: ifile read");
615 segusep = (SEGUSE *)bp->b_data;
616 maxseg = min(segleft, fs->lfs_sepb);
617 for (i = 0; i < maxseg; i++) {
618 sn = curseg + i;
619 if (sn != fs->lfs_curseg &&
620 segusep->su_flags & SEGUSE_ACTIVE) {
621 segusep->su_flags &= ~SEGUSE_ACTIVE;
622 --fs->lfs_nactive;
623 ++dirty;
624 }
625 fs->lfs_suflags[fs->lfs_activesb][sn] =
626 segusep->su_flags;
627 if (fs->lfs_version > 1)
628 ++segusep;
629 else
630 segusep = (SEGUSE *)
631 ((SEGUSE_V1 *)segusep + 1);
632 }
633
634 if (dirty)
635 error = LFS_BWRITE_LOG(bp); /* Ifile */
636 else
637 brelse(bp);
638 segleft -= fs->lfs_sepb;
639 curseg += fs->lfs_sepb;
640 }
641 }
642
643 did_ckp = 0;
644 if (do_ckp || fs->lfs_doifile) {
645 do {
646 vp = fs->lfs_ivnode;
647
648 #ifdef DEBUG
649 LFS_ENTER_LOG("pretend", __FILE__, __LINE__, 0, 0);
650 #endif
651 fs->lfs_flags &= ~LFS_IFDIRTY;
652
653 ip = VTOI(vp);
654
655 if (LIST_FIRST(&vp->v_dirtyblkhd) != NULL)
656 lfs_writefile(fs, sp, vp);
657
658 if (ip->i_flag & IN_ALLMOD)
659 ++did_ckp;
660 redo = lfs_writeinode(fs, sp, ip);
661 redo += lfs_writeseg(fs, sp);
662 redo += (fs->lfs_flags & LFS_IFDIRTY);
663 } while (redo && do_ckp);
664
665 /*
666 * Unless we are unmounting, the Ifile may continue to have
667 * dirty blocks even after a checkpoint, due to changes to
668 * inodes' atime. If we're checkpointing, it's "impossible"
669 * for other parts of the Ifile to be dirty after the loop
670 * above, since we hold the segment lock.
671 */
672 s = splbio();
673 if (LIST_EMPTY(&vp->v_dirtyblkhd)) {
674 LFS_CLR_UINO(ip, IN_ALLMOD);
675 }
676 #ifdef DIAGNOSTIC
677 else if (do_ckp) {
678 LIST_FOREACH(bp, &vp->v_dirtyblkhd, b_vnbufs) {
679 if (bp->b_lblkno < fs->lfs_cleansz +
680 fs->lfs_segtabsz &&
681 !(bp->b_flags & B_GATHERED)) {
682 panic("dirty blocks");
683 }
684 }
685 }
686 #endif
687 splx(s);
688 } else {
689 (void) lfs_writeseg(fs, sp);
690 }
691
692 /* Note Ifile no longer needs to be written */
693 fs->lfs_doifile = 0;
694 if (writer_set)
695 lfs_writer_leave(fs);
696
697 /*
698 * If we didn't write the Ifile, we didn't really do anything.
699 * That means that (1) there is a checkpoint on disk and (2)
700 * nothing has changed since it was written.
701 *
702 * Take the flags off of the segment so that lfs_segunlock
703 * doesn't have to write the superblock either.
704 */
705 if (do_ckp && !did_ckp) {
706 sp->seg_flags &= ~SEGM_CKP;
707 }
708
709 if (lfs_dostats) {
710 ++lfs_stats.nwrites;
711 if (sp->seg_flags & SEGM_SYNC)
712 ++lfs_stats.nsync_writes;
713 if (sp->seg_flags & SEGM_CKP)
714 ++lfs_stats.ncheckpoints;
715 }
716 lfs_segunlock(fs);
717 return (0);
718 }
719
720 /*
721 * Write the dirty blocks associated with a vnode.
722 */
723 void
724 lfs_writefile(struct lfs *fs, struct segment *sp, struct vnode *vp)
725 {
726 struct buf *bp;
727 struct finfo *fip;
728 struct inode *ip;
729 IFILE *ifp;
730 int i, frag;
731
732 ip = VTOI(vp);
733
734 if (sp->seg_bytes_left < fs->lfs_bsize ||
735 sp->sum_bytes_left < sizeof(struct finfo))
736 (void) lfs_writeseg(fs, sp);
737
738 sp->sum_bytes_left -= FINFOSIZE;
739 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
740
741 if (vp->v_flag & VDIROP)
742 ((SEGSUM *)(sp->segsum))->ss_flags |= (SS_DIROP|SS_CONT);
743
744 fip = sp->fip;
745 fip->fi_nblocks = 0;
746 fip->fi_ino = ip->i_number;
747 LFS_IENTRY(ifp, fs, fip->fi_ino, bp);
748 fip->fi_version = ifp->if_version;
749 brelse(bp);
750
751 if (sp->seg_flags & SEGM_CLEAN) {
752 lfs_gather(fs, sp, vp, lfs_match_fake);
753 /*
754 * For a file being flushed, we need to write *all* blocks.
755 * This means writing the cleaning blocks first, and then
756 * immediately following with any non-cleaning blocks.
757 * The same is true of the Ifile since checkpoints assume
758 * that all valid Ifile blocks are written.
759 */
760 if (IS_FLUSHING(fs,vp) || vp == fs->lfs_ivnode) {
761 lfs_gather(fs, sp, vp, lfs_match_data);
762 /*
763 * Don't call VOP_PUTPAGES: if we're flushing,
764 * we've already done it, and the Ifile doesn't
765 * use the page cache.
766 */
767 }
768 } else {
769 lfs_gather(fs, sp, vp, lfs_match_data);
770 /*
771 * If we're flushing, we've already called VOP_PUTPAGES
772 * so don't do it again. Otherwise, we want to write
773 * everything we've got.
774 */
775 if (!IS_FLUSHING(fs, vp)) {
776 simple_lock(&vp->v_interlock);
777 VOP_PUTPAGES(vp, 0, 0,
778 PGO_CLEANIT | PGO_ALLPAGES | PGO_LOCKED);
779 }
780 }
781
782 /*
783 * It may not be necessary to write the meta-data blocks at this point,
784 * as the roll-forward recovery code should be able to reconstruct the
785 * list.
786 *
787 * We have to write them anyway, though, under two conditions: (1) the
788 * vnode is being flushed (for reuse by vinvalbuf); or (2) we are
789 * checkpointing.
790 *
791 * BUT if we are cleaning, we might have indirect blocks that refer to
792 * new blocks not being written yet, in addition to fragments being
793 * moved out of a cleaned segment. If that is the case, don't
794 * write the indirect blocks, or the finfo will have a small block
795 * in the middle of it!
796 * XXX in this case isn't the inode size wrong too?
797 */
798 frag = 0;
799 if (sp->seg_flags & SEGM_CLEAN) {
800 for (i = 0; i < NDADDR; i++)
801 if (ip->i_lfs_fragsize[i] > 0 &&
802 ip->i_lfs_fragsize[i] < fs->lfs_bsize)
803 ++frag;
804 }
805 #ifdef DIAGNOSTIC
806 if (frag > 1)
807 panic("lfs_writefile: more than one fragment!");
808 #endif
809 if (IS_FLUSHING(fs, vp) ||
810 (frag == 0 && (lfs_writeindir || (sp->seg_flags & SEGM_CKP)))) {
811 lfs_gather(fs, sp, vp, lfs_match_indir);
812 lfs_gather(fs, sp, vp, lfs_match_dindir);
813 lfs_gather(fs, sp, vp, lfs_match_tindir);
814 }
815 fip = sp->fip;
816 if (fip->fi_nblocks != 0) {
817 sp->fip = (FINFO*)((caddr_t)fip + FINFOSIZE +
818 sizeof(int32_t) * (fip->fi_nblocks));
819 sp->start_lbp = &sp->fip->fi_blocks[0];
820 } else {
821 sp->sum_bytes_left += FINFOSIZE;
822 --((SEGSUM *)(sp->segsum))->ss_nfinfo;
823 }
824 }
825
826 int
827 lfs_writeinode(struct lfs *fs, struct segment *sp, struct inode *ip)
828 {
829 struct buf *bp, *ibp;
830 struct ufs1_dinode *cdp;
831 IFILE *ifp;
832 SEGUSE *sup;
833 daddr_t daddr;
834 int32_t *daddrp; /* XXX ondisk32 */
835 ino_t ino;
836 int error, i, ndx, fsb = 0;
837 int redo_ifile = 0;
838 struct timespec ts;
839 int gotblk = 0;
840
841 if (!(ip->i_flag & IN_ALLMOD))
842 return (0);
843
844 /* Allocate a new inode block if necessary. */
845 if ((ip->i_number != LFS_IFILE_INUM || sp->idp == NULL) && sp->ibp == NULL) {
846 /* Allocate a new segment if necessary. */
847 if (sp->seg_bytes_left < fs->lfs_ibsize ||
848 sp->sum_bytes_left < sizeof(int32_t))
849 (void) lfs_writeseg(fs, sp);
850
851 /* Get next inode block. */
852 daddr = fs->lfs_offset;
853 fs->lfs_offset += btofsb(fs, fs->lfs_ibsize);
854 sp->ibp = *sp->cbpp++ =
855 getblk(VTOI(fs->lfs_ivnode)->i_devvp, fsbtodb(fs, daddr),
856 fs->lfs_ibsize, 0, 0);
857 gotblk++;
858
859 /* Zero out inode numbers */
860 for (i = 0; i < INOPB(fs); ++i)
861 ((struct ufs1_dinode *)sp->ibp->b_data)[i].di_inumber = 0;
862
863 ++sp->start_bpp;
864 fs->lfs_avail -= btofsb(fs, fs->lfs_ibsize);
865 /* Set remaining space counters. */
866 sp->seg_bytes_left -= fs->lfs_ibsize;
867 sp->sum_bytes_left -= sizeof(int32_t);
868 ndx = fs->lfs_sumsize / sizeof(int32_t) -
869 sp->ninodes / INOPB(fs) - 1;
870 ((int32_t *)(sp->segsum))[ndx] = daddr;
871 }
872
873 /* Update the inode times and copy the inode onto the inode page. */
874 TIMEVAL_TO_TIMESPEC(&time, &ts);
875 /* XXX kludge --- don't redirty the ifile just to put times on it */
876 if (ip->i_number != LFS_IFILE_INUM)
877 LFS_ITIMES(ip, &ts, &ts, &ts);
878
879 /*
880 * If this is the Ifile, and we've already written the Ifile in this
881 * partial segment, just overwrite it (it's not on disk yet) and
882 * continue.
883 *
884 * XXX we know that the bp that we get the second time around has
885 * already been gathered.
886 */
887 if (ip->i_number == LFS_IFILE_INUM && sp->idp) {
888 *(sp->idp) = *ip->i_din.ffs1_din;
889 ip->i_lfs_osize = ip->i_size;
890 return 0;
891 }
892
893 bp = sp->ibp;
894 cdp = ((struct ufs1_dinode *)bp->b_data) + (sp->ninodes % INOPB(fs));
895 *cdp = *ip->i_din.ffs1_din;
896 #ifdef LFS_IFILE_FRAG_ADDRESSING
897 if (fs->lfs_version > 1)
898 fsb = (sp->ninodes % INOPB(fs)) / INOPF(fs);
899 #endif
900
901 /*
902 * If we are cleaning, ensure that we don't write UNWRITTEN disk
903 * addresses to disk; possibly revert the inode size.
904 * XXX By not writing these blocks, we are making the lfs_avail
905 * XXX count on disk wrong by the same amount. We should be
906 * XXX able to "borrow" from lfs_avail and return it after the
907 * XXX Ifile is written. See also in lfs_writeseg.
908 */
909 if (ip->i_lfs_effnblks != ip->i_ffs1_blocks) {
910 cdp->di_size = ip->i_lfs_osize;
911 #ifdef DEBUG_LFS
912 printf("lfs_writeinode: cleansing ino %d (%d != %d)\n",
913 ip->i_number, ip->i_lfs_effnblks, ip->i_ffs1_blocks);
914 #endif
915 for (daddrp = cdp->di_db; daddrp < cdp->di_ib + NIADDR;
916 daddrp++) {
917 if (*daddrp == UNWRITTEN) {
918 #ifdef DEBUG_LFS
919 printf("lfs_writeinode: wiping UNWRITTEN\n");
920 #endif
921 *daddrp = 0;
922 }
923 }
924 } else {
925 /* If all blocks are goig to disk, update the "size on disk" */
926 ip->i_lfs_osize = ip->i_size;
927 }
928
929 if (ip->i_flag & IN_CLEANING)
930 LFS_CLR_UINO(ip, IN_CLEANING);
931 else {
932 /* XXX IN_ALLMOD */
933 LFS_CLR_UINO(ip, IN_ACCESSED | IN_ACCESS | IN_CHANGE |
934 IN_UPDATE);
935 if (ip->i_lfs_effnblks == ip->i_ffs1_blocks)
936 LFS_CLR_UINO(ip, IN_MODIFIED);
937 #ifdef DEBUG_LFS
938 else
939 printf("lfs_writeinode: ino %d: real blks=%d, "
940 "eff=%d\n", ip->i_number, ip->i_ffs1_blocks,
941 ip->i_lfs_effnblks);
942 #endif
943 }
944
945 if (ip->i_number == LFS_IFILE_INUM) /* We know sp->idp == NULL */
946 sp->idp = ((struct ufs1_dinode *)bp->b_data) +
947 (sp->ninodes % INOPB(fs));
948 if (gotblk) {
949 LFS_LOCK_BUF(bp);
950 brelse(bp);
951 }
952
953 /* Increment inode count in segment summary block. */
954 ++((SEGSUM *)(sp->segsum))->ss_ninos;
955
956 /* If this page is full, set flag to allocate a new page. */
957 if (++sp->ninodes % INOPB(fs) == 0)
958 sp->ibp = NULL;
959
960 /*
961 * If updating the ifile, update the super-block. Update the disk
962 * address and access times for this inode in the ifile.
963 */
964 ino = ip->i_number;
965 if (ino == LFS_IFILE_INUM) {
966 daddr = fs->lfs_idaddr;
967 fs->lfs_idaddr = dbtofsb(fs, bp->b_blkno);
968 } else {
969 LFS_IENTRY(ifp, fs, ino, ibp);
970 daddr = ifp->if_daddr;
971 ifp->if_daddr = dbtofsb(fs, bp->b_blkno) + fsb;
972 #ifdef LFS_DEBUG_NEXTFREE
973 if (ino > 3 && ifp->if_nextfree) {
974 vprint("lfs_writeinode",ITOV(ip));
975 printf("lfs_writeinode: updating free ino %d\n",
976 ip->i_number);
977 }
978 #endif
979 error = LFS_BWRITE_LOG(ibp); /* Ifile */
980 }
981
982 /*
983 * The inode's last address should not be in the current partial
984 * segment, except under exceptional circumstances (lfs_writevnodes
985 * had to start over, and in the meantime more blocks were written
986 * to a vnode). Both inodes will be accounted to this segment
987 * in lfs_writeseg so we need to subtract the earlier version
988 * here anyway. The segment count can temporarily dip below
989 * zero here; keep track of how many duplicates we have in
990 * "dupino" so we don't panic below.
991 */
992 if (daddr >= fs->lfs_lastpseg && daddr <= dbtofsb(fs, bp->b_blkno)) {
993 ++sp->ndupino;
994 printf("lfs_writeinode: last inode addr in current pseg "
995 "(ino %d daddr 0x%llx) ndupino=%d\n", ino,
996 (long long)daddr, sp->ndupino);
997 }
998 /*
999 * Account the inode: it no longer belongs to its former segment,
1000 * though it will not belong to the new segment until that segment
1001 * is actually written.
1002 */
1003 if (daddr != LFS_UNUSED_DADDR) {
1004 u_int32_t oldsn = dtosn(fs, daddr);
1005 #ifdef DIAGNOSTIC
1006 int ndupino = (sp->seg_number == oldsn) ? sp->ndupino : 0;
1007 #endif
1008 LFS_SEGENTRY(sup, fs, oldsn, bp);
1009 #ifdef DIAGNOSTIC
1010 if (sup->su_nbytes +
1011 sizeof (struct ufs1_dinode) * ndupino
1012 < sizeof (struct ufs1_dinode)) {
1013 printf("lfs_writeinode: negative bytes "
1014 "(segment %" PRIu32 " short by %d, "
1015 "oldsn=%" PRIu32 ", cursn=%" PRIu32
1016 ", daddr=%" PRId64 ", su_nbytes=%u, "
1017 "ndupino=%d)\n",
1018 dtosn(fs, daddr),
1019 (int)sizeof (struct ufs1_dinode) * (1 - sp->ndupino)
1020 - sup->su_nbytes,
1021 oldsn, sp->seg_number, daddr,
1022 (unsigned int)sup->su_nbytes,
1023 sp->ndupino);
1024 panic("lfs_writeinode: negative bytes");
1025 sup->su_nbytes = sizeof (struct ufs1_dinode);
1026 }
1027 #endif
1028 #ifdef DEBUG_SU_NBYTES
1029 printf("seg %d -= %d for ino %d inode\n",
1030 dtosn(fs, daddr), sizeof (struct ufs1_dinode), ino);
1031 #endif
1032 sup->su_nbytes -= sizeof (struct ufs1_dinode);
1033 redo_ifile =
1034 (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
1035 if (redo_ifile)
1036 fs->lfs_flags |= LFS_IFDIRTY;
1037 LFS_WRITESEGENTRY(sup, fs, oldsn, bp); /* Ifile */
1038 }
1039 return (redo_ifile);
1040 }
1041
1042 int
1043 lfs_gatherblock(struct segment *sp, struct buf *bp, int *sptr)
1044 {
1045 struct lfs *fs;
1046 int version;
1047 int j, blksinblk;
1048
1049 /*
1050 * If full, finish this segment. We may be doing I/O, so
1051 * release and reacquire the splbio().
1052 */
1053 #ifdef DIAGNOSTIC
1054 if (sp->vp == NULL)
1055 panic ("lfs_gatherblock: Null vp in segment");
1056 #endif
1057 fs = sp->fs;
1058 blksinblk = howmany(bp->b_bcount, fs->lfs_bsize);
1059 if (sp->sum_bytes_left < sizeof(int32_t) * blksinblk ||
1060 sp->seg_bytes_left < bp->b_bcount) {
1061 if (sptr)
1062 splx(*sptr);
1063 lfs_updatemeta(sp);
1064
1065 version = sp->fip->fi_version;
1066 (void) lfs_writeseg(fs, sp);
1067
1068 sp->fip->fi_version = version;
1069 sp->fip->fi_ino = VTOI(sp->vp)->i_number;
1070 /* Add the current file to the segment summary. */
1071 ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
1072 sp->sum_bytes_left -= FINFOSIZE;
1073
1074 if (sptr)
1075 *sptr = splbio();
1076 return (1);
1077 }
1078
1079 #ifdef DEBUG
1080 if (bp->b_flags & B_GATHERED) {
1081 printf("lfs_gatherblock: already gathered! Ino %d,"
1082 " lbn %" PRId64 "\n",
1083 sp->fip->fi_ino, bp->b_lblkno);
1084 return (0);
1085 }
1086 #endif
1087 /* Insert into the buffer list, update the FINFO block. */
1088 bp->b_flags |= B_GATHERED;
1089
1090 *sp->cbpp++ = bp;
1091 for (j = 0; j < blksinblk; j++)
1092 sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno + j;
1093
1094 sp->sum_bytes_left -= sizeof(int32_t) * blksinblk;
1095 sp->seg_bytes_left -= bp->b_bcount;
1096 return (0);
1097 }
1098
1099 int
1100 lfs_gather(struct lfs *fs, struct segment *sp, struct vnode *vp, int (*match)(struct lfs *, struct buf *))
1101 {
1102 struct buf *bp, *nbp;
1103 int s, count = 0;
1104
1105 sp->vp = vp;
1106 s = splbio();
1107
1108 #ifndef LFS_NO_BACKBUF_HACK
1109 /* This is a hack to see if ordering the blocks in LFS makes a difference. */
1110 # define BUF_OFFSET (((caddr_t)&LIST_NEXT(bp, b_vnbufs)) - (caddr_t)bp)
1111 # define BACK_BUF(BP) ((struct buf *)(((caddr_t)(BP)->b_vnbufs.le_prev) - BUF_OFFSET))
1112 # define BEG_OF_LIST ((struct buf *)(((caddr_t)&LIST_FIRST(&vp->v_dirtyblkhd)) - BUF_OFFSET))
1113 /* Find last buffer. */
1114 loop: for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp && LIST_NEXT(bp, b_vnbufs) != NULL;
1115 bp = LIST_NEXT(bp, b_vnbufs));
1116 for (; bp && bp != BEG_OF_LIST; bp = nbp) {
1117 nbp = BACK_BUF(bp);
1118 #else /* LFS_NO_BACKBUF_HACK */
1119 loop: for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
1120 nbp = LIST_NEXT(bp, b_vnbufs);
1121 #endif /* LFS_NO_BACKBUF_HACK */
1122 if ((bp->b_flags & (B_BUSY|B_GATHERED)) || !match(fs, bp)) {
1123 #ifdef DEBUG_LFS
1124 if (vp == fs->lfs_ivnode && (bp->b_flags & (B_BUSY|B_GATHERED)) == B_BUSY)
1125 printf("(%" PRId64 ":%lx)", bp->b_lblkno, bp->b_flags);
1126 #endif
1127 continue;
1128 }
1129 if (vp->v_type == VBLK) {
1130 /* For block devices, just write the blocks. */
1131 /* XXX Do we really need to even do this? */
1132 #ifdef DEBUG_LFS
1133 if (count == 0)
1134 printf("BLK(");
1135 printf(".");
1136 #endif
1137 /* Get the block before bwrite, so we don't corrupt the free list */
1138 bp->b_flags |= B_BUSY;
1139 bremfree(bp);
1140 bwrite(bp);
1141 } else {
1142 #ifdef DIAGNOSTIC
1143 # ifdef LFS_USE_B_INVAL
1144 if ((bp->b_flags & (B_CALL|B_INVAL)) == B_INVAL) {
1145 printf("lfs_gather: lbn %" PRId64 " is "
1146 "B_INVAL\n", bp->b_lblkno);
1147 VOP_PRINT(bp->b_vp);
1148 }
1149 # endif /* LFS_USE_B_INVAL */
1150 if (!(bp->b_flags & B_DELWRI))
1151 panic("lfs_gather: bp not B_DELWRI");
1152 if (!(bp->b_flags & B_LOCKED)) {
1153 printf("lfs_gather: lbn %" PRId64 " blk "
1154 "%" PRId64 " not B_LOCKED\n",
1155 bp->b_lblkno,
1156 dbtofsb(fs, bp->b_blkno));
1157 VOP_PRINT(bp->b_vp);
1158 panic("lfs_gather: bp not B_LOCKED");
1159 }
1160 #endif
1161 if (lfs_gatherblock(sp, bp, &s)) {
1162 goto loop;
1163 }
1164 }
1165 count++;
1166 }
1167 splx(s);
1168 #ifdef DEBUG_LFS
1169 if (vp->v_type == VBLK && count)
1170 printf(")\n");
1171 #endif
1172 lfs_updatemeta(sp);
1173 sp->vp = NULL;
1174 return count;
1175 }
1176
1177 #if DEBUG
1178 # define DEBUG_OOFF(n) do { \
1179 if (ooff == 0) { \
1180 printf("lfs_updatemeta[%d]: warning: writing " \
1181 "ino %d lbn %" PRId64 " at 0x%" PRIx32 \
1182 ", was 0x0 (or %" PRId64 ")\n", \
1183 (n), ip->i_number, lbn, ndaddr, daddr); \
1184 } \
1185 } while (0)
1186 #else
1187 # define DEBUG_OOFF(n)
1188 #endif
1189
1190 /*
1191 * Change the given block's address to ndaddr, finding its previous
1192 * location using ufs_bmaparray().
1193 *
1194 * Account for this change in the segment table.
1195 */
1196 void
1197 lfs_update_single(struct lfs *fs, struct segment *sp, daddr_t lbn,
1198 int32_t ndaddr, int size)
1199 {
1200 SEGUSE *sup;
1201 struct buf *bp;
1202 struct indir a[NIADDR + 2], *ap;
1203 struct inode *ip;
1204 struct vnode *vp;
1205 daddr_t daddr, ooff;
1206 int num, error;
1207 int bb, osize, obb;
1208
1209 vp = sp->vp;
1210 ip = VTOI(vp);
1211
1212 error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL, NULL);
1213 if (error)
1214 panic("lfs_updatemeta: ufs_bmaparray returned %d", error);
1215
1216 KASSERT(daddr <= LFS_MAX_DADDR);
1217 if (daddr > 0)
1218 daddr = dbtofsb(fs, daddr);
1219
1220 bb = fragstofsb(fs, numfrags(fs, size));
1221 switch (num) {
1222 case 0:
1223 ooff = ip->i_ffs1_db[lbn];
1224 DEBUG_OOFF(0);
1225 if (ooff == UNWRITTEN)
1226 ip->i_ffs1_blocks += bb;
1227 else {
1228 /* possible fragment truncation or extension */
1229 obb = btofsb(fs, ip->i_lfs_fragsize[lbn]);
1230 ip->i_ffs1_blocks += (bb - obb);
1231 }
1232 ip->i_ffs1_db[lbn] = ndaddr;
1233 break;
1234 case 1:
1235 ooff = ip->i_ffs1_ib[a[0].in_off];
1236 DEBUG_OOFF(1);
1237 if (ooff == UNWRITTEN)
1238 ip->i_ffs1_blocks += bb;
1239 ip->i_ffs1_ib[a[0].in_off] = ndaddr;
1240 break;
1241 default:
1242 ap = &a[num - 1];
1243 if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
1244 panic("lfs_updatemeta: bread bno %" PRId64,
1245 ap->in_lbn);
1246
1247 /* XXX ondisk32 */
1248 ooff = ((int32_t *)bp->b_data)[ap->in_off];
1249 DEBUG_OOFF(num);
1250 if (ooff == UNWRITTEN)
1251 ip->i_ffs1_blocks += bb;
1252 /* XXX ondisk32 */
1253 ((int32_t *)bp->b_data)[ap->in_off] = ndaddr;
1254 (void) VOP_BWRITE(bp);
1255 }
1256
1257 /*
1258 * Though we'd rather it couldn't, this *can* happen right now
1259 * if cleaning blocks and regular blocks coexist.
1260 */
1261 /* KASSERT(daddr < fs->lfs_lastpseg || daddr > ndaddr); */
1262
1263 /*
1264 * Update segment usage information, based on old size
1265 * and location.
1266 */
1267 if (daddr > 0) {
1268 u_int32_t oldsn = dtosn(fs, daddr);
1269 #ifdef DIAGNOSTIC
1270 int ndupino = (sp->seg_number == oldsn) ?
1271 sp->ndupino : 0;
1272 #endif
1273 KASSERT(oldsn >= 0 && oldsn < fs->lfs_nseg);
1274 if (lbn >= 0 && lbn < NDADDR)
1275 osize = ip->i_lfs_fragsize[lbn];
1276 else
1277 osize = fs->lfs_bsize;
1278 LFS_SEGENTRY(sup, fs, oldsn, bp);
1279 #ifdef DIAGNOSTIC
1280 if (sup->su_nbytes + sizeof (struct ufs1_dinode) * ndupino
1281 < osize) {
1282 printf("lfs_updatemeta: negative bytes "
1283 "(segment %" PRIu32 " short by %" PRId64
1284 ")\n", dtosn(fs, daddr),
1285 (int64_t)osize -
1286 (sizeof (struct ufs1_dinode) * sp->ndupino +
1287 sup->su_nbytes));
1288 printf("lfs_updatemeta: ino %d, lbn %" PRId64
1289 ", addr = 0x%" PRIx64 "\n",
1290 VTOI(sp->vp)->i_number, lbn, daddr);
1291 printf("lfs_updatemeta: ndupino=%d\n", ndupino);
1292 panic("lfs_updatemeta: negative bytes");
1293 sup->su_nbytes = osize -
1294 sizeof (struct ufs1_dinode) * sp->ndupino;
1295 }
1296 #endif
1297 #ifdef DEBUG_SU_NBYTES
1298 printf("seg %" PRIu32 " -= %d for ino %d lbn %" PRId64
1299 " db 0x%" PRIx64 "\n",
1300 dtosn(fs, daddr), osize,
1301 VTOI(sp->vp)->i_number, lbn, daddr);
1302 #endif
1303 sup->su_nbytes -= osize;
1304 if (!(bp->b_flags & B_GATHERED))
1305 fs->lfs_flags |= LFS_IFDIRTY;
1306 LFS_WRITESEGENTRY(sup, fs, oldsn, bp);
1307 }
1308 /*
1309 * Now that this block has a new address, and its old
1310 * segment no longer owns it, we can forget about its
1311 * old size.
1312 */
1313 if (lbn >= 0 && lbn < NDADDR)
1314 ip->i_lfs_fragsize[lbn] = size;
1315 }
1316
1317 /*
1318 * Update the metadata that points to the blocks listed in the FINFO
1319 * array.
1320 */
1321 void
1322 lfs_updatemeta(struct segment *sp)
1323 {
1324 struct buf *sbp;
1325 struct lfs *fs;
1326 struct vnode *vp;
1327 daddr_t lbn;
1328 int i, nblocks, num;
1329 int bb;
1330 int bytesleft, size;
1331
1332 vp = sp->vp;
1333 nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
1334 KASSERT(nblocks >= 0);
1335 if (vp == NULL || nblocks == 0)
1336 return;
1337
1338 /*
1339 * This count may be high due to oversize blocks from lfs_gop_write.
1340 * Correct for this. (XXX we should be able to keep track of these.)
1341 */
1342 fs = sp->fs;
1343 for (i = 0; i < nblocks; i++) {
1344 if (sp->start_bpp[i] == NULL) {
1345 printf("nblocks = %d, not %d\n", i, nblocks);
1346 nblocks = i;
1347 break;
1348 }
1349 num = howmany(sp->start_bpp[i]->b_bcount, fs->lfs_bsize);
1350 KASSERT(sp->start_bpp[i]->b_lblkno >= 0 || num == 1);
1351 nblocks -= num - 1;
1352 }
1353
1354 KASSERT(vp->v_type == VREG ||
1355 nblocks == &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp);
1356 KASSERT(nblocks == sp->cbpp - sp->start_bpp);
1357
1358 /*
1359 * Sort the blocks.
1360 *
1361 * We have to sort even if the blocks come from the
1362 * cleaner, because there might be other pending blocks on the
1363 * same inode...and if we don't sort, and there are fragments
1364 * present, blocks may be written in the wrong place.
1365 */
1366 lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks, fs->lfs_bsize);
1367
1368 /*
1369 * Record the length of the last block in case it's a fragment.
1370 * If there are indirect blocks present, they sort last. An
1371 * indirect block will be lfs_bsize and its presence indicates
1372 * that you cannot have fragments.
1373 *
1374 * XXX This last is a lie. A cleaned fragment can coexist with
1375 * XXX a later indirect block. This will continue to be
1376 * XXX true until lfs_markv is fixed to do everything with
1377 * XXX fake blocks (including fake inodes and fake indirect blocks).
1378 */
1379 sp->fip->fi_lastlength = ((sp->start_bpp[nblocks - 1]->b_bcount - 1) &
1380 fs->lfs_bmask) + 1;
1381
1382 /*
1383 * Assign disk addresses, and update references to the logical
1384 * block and the segment usage information.
1385 */
1386 for (i = nblocks; i--; ++sp->start_bpp) {
1387 sbp = *sp->start_bpp;
1388 lbn = *sp->start_lbp;
1389 KASSERT(sbp->b_lblkno == lbn);
1390
1391 sbp->b_blkno = fsbtodb(fs, fs->lfs_offset);
1392
1393 /*
1394 * If we write a frag in the wrong place, the cleaner won't
1395 * be able to correctly identify its size later, and the
1396 * segment will be uncleanable. (Even worse, it will assume
1397 * that the indirect block that actually ends the list
1398 * is of a smaller size!)
1399 */
1400 if ((sbp->b_bcount & fs->lfs_bmask) && i != 0)
1401 panic("lfs_updatemeta: fragment is not last block");
1402
1403 /*
1404 * For each subblock in this possibly oversized block,
1405 * update its address on disk.
1406 */
1407 KASSERT(lbn >= 0 || sbp->b_bcount == fs->lfs_bsize);
1408 for (bytesleft = sbp->b_bcount; bytesleft > 0;
1409 bytesleft -= fs->lfs_bsize) {
1410 size = MIN(bytesleft, fs->lfs_bsize);
1411 bb = fragstofsb(fs, numfrags(fs, size));
1412 lbn = *sp->start_lbp++;
1413 lfs_update_single(fs, sp, lbn, fs->lfs_offset, size);
1414 fs->lfs_offset += bb;
1415 }
1416
1417 }
1418 }
1419
1420 /*
1421 * Start a new segment.
1422 */
1423 int
1424 lfs_initseg(struct lfs *fs)
1425 {
1426 struct segment *sp;
1427 SEGUSE *sup;
1428 SEGSUM *ssp;
1429 struct buf *bp, *sbp;
1430 int repeat;
1431
1432 sp = fs->lfs_sp;
1433
1434 repeat = 0;
1435
1436 /* Advance to the next segment. */
1437 if (!LFS_PARTIAL_FITS(fs)) {
1438 /* lfs_avail eats the remaining space */
1439 fs->lfs_avail -= fs->lfs_fsbpseg - (fs->lfs_offset -
1440 fs->lfs_curseg);
1441 /* Wake up any cleaning procs waiting on this file system. */
1442 wakeup(&lfs_allclean_wakeup);
1443 wakeup(&fs->lfs_nextseg);
1444 lfs_newseg(fs);
1445 repeat = 1;
1446 fs->lfs_offset = fs->lfs_curseg;
1447
1448 sp->seg_number = dtosn(fs, fs->lfs_curseg);
1449 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg);
1450
1451 /*
1452 * If the segment contains a superblock, update the offset
1453 * and summary address to skip over it.
1454 */
1455 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1456 if (sup->su_flags & SEGUSE_SUPERBLOCK) {
1457 fs->lfs_offset += btofsb(fs, LFS_SBPAD);
1458 sp->seg_bytes_left -= LFS_SBPAD;
1459 }
1460 brelse(bp);
1461 /* Segment zero could also contain the labelpad */
1462 if (fs->lfs_version > 1 && sp->seg_number == 0 &&
1463 fs->lfs_start < btofsb(fs, LFS_LABELPAD)) {
1464 fs->lfs_offset += btofsb(fs, LFS_LABELPAD) - fs->lfs_start;
1465 sp->seg_bytes_left -= LFS_LABELPAD - fsbtob(fs, fs->lfs_start);
1466 }
1467 } else {
1468 sp->seg_number = dtosn(fs, fs->lfs_curseg);
1469 sp->seg_bytes_left = fsbtob(fs, fs->lfs_fsbpseg -
1470 (fs->lfs_offset - fs->lfs_curseg));
1471 }
1472 fs->lfs_lastpseg = fs->lfs_offset;
1473
1474 /* Record first address of this partial segment */
1475 if (sp->seg_flags & SEGM_CLEAN) {
1476 fs->lfs_cleanint[fs->lfs_cleanind] = fs->lfs_offset;
1477 if (++fs->lfs_cleanind >= LFS_MAX_CLEANIND) {
1478 /* "1" is the artificial inc in lfs_seglock */
1479 while (fs->lfs_iocount > 1) {
1480 tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs_initseg", 0);
1481 }
1482 fs->lfs_cleanind = 0;
1483 }
1484 }
1485
1486 sp->fs = fs;
1487 sp->ibp = NULL;
1488 sp->idp = NULL;
1489 sp->ninodes = 0;
1490 sp->ndupino = 0;
1491
1492 /* Get a new buffer for SEGSUM and enter it into the buffer list. */
1493 sp->cbpp = sp->bpp;
1494 sbp = *sp->cbpp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp,
1495 fsbtodb(fs, fs->lfs_offset), fs->lfs_sumsize, LFS_NB_SUMMARY);
1496 sp->segsum = (*sp->cbpp)->b_data;
1497 memset(sp->segsum, 0, fs->lfs_sumsize);
1498 sp->start_bpp = ++sp->cbpp;
1499 fs->lfs_offset += btofsb(fs, fs->lfs_sumsize);
1500
1501 /* Set point to SEGSUM, initialize it. */
1502 ssp = sp->segsum;
1503 ssp->ss_next = fs->lfs_nextseg;
1504 ssp->ss_nfinfo = ssp->ss_ninos = 0;
1505 ssp->ss_magic = SS_MAGIC;
1506
1507 /* Set pointer to first FINFO, initialize it. */
1508 sp->fip = (struct finfo *)((caddr_t)sp->segsum + SEGSUM_SIZE(fs));
1509 sp->fip->fi_nblocks = 0;
1510 sp->start_lbp = &sp->fip->fi_blocks[0];
1511 sp->fip->fi_lastlength = 0;
1512
1513 sp->seg_bytes_left -= fs->lfs_sumsize;
1514 sp->sum_bytes_left = fs->lfs_sumsize - SEGSUM_SIZE(fs);
1515
1516 return (repeat);
1517 }
1518
1519 /*
1520 * Return the next segment to write.
1521 */
1522 void
1523 lfs_newseg(struct lfs *fs)
1524 {
1525 CLEANERINFO *cip;
1526 SEGUSE *sup;
1527 struct buf *bp;
1528 int curseg, isdirty, sn;
1529
1530 LFS_SEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
1531 #ifdef DEBUG_SU_NBYTES
1532 printf("lfs_newseg: seg %d := 0 in newseg\n", /* XXXDEBUG */
1533 dtosn(fs, fs->lfs_nextseg)); /* XXXDEBUG */
1534 #endif
1535 sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
1536 sup->su_nbytes = 0;
1537 sup->su_nsums = 0;
1538 sup->su_ninos = 0;
1539 LFS_WRITESEGENTRY(sup, fs, dtosn(fs, fs->lfs_nextseg), bp);
1540
1541 LFS_CLEANERINFO(cip, fs, bp);
1542 --cip->clean;
1543 ++cip->dirty;
1544 fs->lfs_nclean = cip->clean;
1545 LFS_SYNC_CLEANERINFO(cip, fs, bp, 1);
1546
1547 fs->lfs_lastseg = fs->lfs_curseg;
1548 fs->lfs_curseg = fs->lfs_nextseg;
1549 for (sn = curseg = dtosn(fs, fs->lfs_curseg) + fs->lfs_interleave;;) {
1550 sn = (sn + 1) % fs->lfs_nseg;
1551 if (sn == curseg)
1552 panic("lfs_nextseg: no clean segments");
1553 LFS_SEGENTRY(sup, fs, sn, bp);
1554 isdirty = sup->su_flags & SEGUSE_DIRTY;
1555 /* Check SEGUSE_EMPTY as we go along */
1556 if (isdirty && sup->su_nbytes == 0 && !(sup->su_flags & SEGUSE_EMPTY))
1557 LFS_WRITESEGENTRY(sup, fs, sn, bp);
1558 else
1559 brelse(bp);
1560
1561 if (!isdirty)
1562 break;
1563 }
1564
1565 ++fs->lfs_nactive;
1566 fs->lfs_nextseg = sntod(fs, sn);
1567 if (lfs_dostats) {
1568 ++lfs_stats.segsused;
1569 }
1570 }
1571
1572 #define BQUEUES 4 /* XXX */
1573 #define BQ_EMPTY 3 /* XXX */
1574 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
1575 extern struct simplelock bqueue_slock;
1576
1577 #define BUFHASH(dvp, lbn) \
1578 (&bufhashtbl[((long)(dvp) / sizeof(*(dvp)) + (int)(lbn)) & bufhash])
1579 extern LIST_HEAD(bufhashhdr, buf) invalhash;
1580 /*
1581 * Insq/Remq for the buffer hash lists.
1582 */
1583 #define binshash(bp, dp) LIST_INSERT_HEAD(dp, bp, b_hash)
1584 #define bremhash(bp) LIST_REMOVE(bp, b_hash)
1585
1586 static struct buf *
1587 lfs_newclusterbuf(struct lfs *fs, struct vnode *vp, daddr_t addr, int n)
1588 {
1589 struct lfs_cluster *cl;
1590 struct buf **bpp, *bp;
1591 int s;
1592
1593 cl = (struct lfs_cluster *)pool_get(&fs->lfs_clpool, PR_WAITOK);
1594 bpp = (struct buf **)pool_get(&fs->lfs_bpppool, PR_WAITOK);
1595 memset(cl, 0, sizeof(*cl));
1596 cl->fs = fs;
1597 cl->bpp = bpp;
1598 cl->bufcount = 0;
1599 cl->bufsize = 0;
1600
1601 /* If this segment is being written synchronously, note that */
1602 if (fs->lfs_sp->seg_flags & SEGM_SYNC) {
1603 cl->flags |= LFS_CL_SYNC;
1604 cl->seg = fs->lfs_sp;
1605 ++cl->seg->seg_iocount;
1606 /* printf("+ %x => %d\n", cl->seg, cl->seg->seg_iocount); */
1607 }
1608
1609 /* Get an empty buffer header, or maybe one with something on it */
1610 s = splbio();
1611 simple_lock(&bqueue_slock);
1612 if ((bp = TAILQ_FIRST(&bufqueues[BQ_EMPTY])) != NULL) {
1613 simple_lock(&bp->b_interlock);
1614 bremfree(bp);
1615 /* clear out various other fields */
1616 bp->b_flags = B_BUSY;
1617 bp->b_dev = NODEV;
1618 bp->b_blkno = bp->b_lblkno = 0;
1619 bp->b_error = 0;
1620 bp->b_resid = 0;
1621 bp->b_bcount = 0;
1622
1623 /* nuke any credentials we were holding */
1624 /* XXXXXX */
1625
1626 bremhash(bp);
1627
1628 /* disassociate us from our vnode, if we had one... */
1629 if (bp->b_vp)
1630 brelvp(bp);
1631 }
1632 while (!bp)
1633 bp = getnewbuf(0, 0);
1634 bgetvp(vp, bp);
1635 binshash(bp,&invalhash);
1636 simple_unlock(&bp->b_interlock);
1637 simple_unlock(&bqueue_slock);
1638 splx(s);
1639 bp->b_bcount = 0;
1640 bp->b_blkno = bp->b_lblkno = addr;
1641
1642 bp->b_flags |= B_CALL;
1643 bp->b_iodone = lfs_cluster_callback;
1644 cl->saveaddr = bp->b_saveaddr; /* XXX is this ever used? */
1645 bp->b_saveaddr = (caddr_t)cl;
1646
1647 return bp;
1648 }
1649
1650 int
1651 lfs_writeseg(struct lfs *fs, struct segment *sp)
1652 {
1653 struct buf **bpp, *bp, *cbp, *newbp;
1654 SEGUSE *sup;
1655 SEGSUM *ssp;
1656 dev_t i_dev;
1657 char *datap, *dp;
1658 int i, s;
1659 int do_again, nblocks, byteoffset;
1660 size_t el_size;
1661 struct lfs_cluster *cl;
1662 int (*strategy)(void *);
1663 struct vop_strategy_args vop_strategy_a;
1664 u_short ninos;
1665 struct vnode *devvp;
1666 char *p;
1667 struct vnode *vp;
1668 int32_t *daddrp; /* XXX ondisk32 */
1669 int changed;
1670 #if defined(DEBUG) && defined(LFS_PROPELLER)
1671 static int propeller;
1672 char propstring[4] = "-\\|/";
1673
1674 printf("%c\b",propstring[propeller++]);
1675 if (propeller == 4)
1676 propeller = 0;
1677 #endif
1678
1679 /*
1680 * If there are no buffers other than the segment summary to write
1681 * and it is not a checkpoint, don't do anything. On a checkpoint,
1682 * even if there aren't any buffers, you need to write the superblock.
1683 */
1684 if ((nblocks = sp->cbpp - sp->bpp) == 1)
1685 return (0);
1686
1687 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
1688 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
1689
1690 /* Update the segment usage information. */
1691 LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
1692
1693 /* Loop through all blocks, except the segment summary. */
1694 for (bpp = sp->bpp; ++bpp < sp->cbpp; ) {
1695 if ((*bpp)->b_vp != devvp) {
1696 sup->su_nbytes += (*bpp)->b_bcount;
1697 #ifdef DEBUG_SU_NBYTES
1698 printf("seg %" PRIu32 " += %ld for ino %d lbn %" PRId64
1699 " db 0x%" PRIx64 "\n", sp->seg_number, (*bpp)->b_bcount,
1700 VTOI((*bpp)->b_vp)->i_number, (*bpp)->b_lblkno,
1701 (*bpp)->b_blkno);
1702 #endif
1703 }
1704 }
1705
1706 ssp = (SEGSUM *)sp->segsum;
1707
1708 ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
1709 #ifdef DEBUG_SU_NBYTES
1710 printf("seg %d += %d for %d inodes\n", /* XXXDEBUG */
1711 sp->seg_number, ssp->ss_ninos * sizeof (struct ufs1_dinode),
1712 ssp->ss_ninos);
1713 #endif
1714 sup->su_nbytes += ssp->ss_ninos * sizeof (struct ufs1_dinode);
1715 /* sup->su_nbytes += fs->lfs_sumsize; */
1716 if (fs->lfs_version == 1)
1717 sup->su_olastmod = time.tv_sec;
1718 else
1719 sup->su_lastmod = time.tv_sec;
1720 sup->su_ninos += ninos;
1721 ++sup->su_nsums;
1722 fs->lfs_dmeta += (btofsb(fs, fs->lfs_sumsize) + btofsb(fs, ninos *
1723 fs->lfs_ibsize));
1724 fs->lfs_avail -= btofsb(fs, fs->lfs_sumsize);
1725
1726 do_again = !(bp->b_flags & B_GATHERED);
1727 LFS_WRITESEGENTRY(sup, fs, sp->seg_number, bp); /* Ifile */
1728
1729 /*
1730 * Mark blocks B_BUSY, to prevent then from being changed between
1731 * the checksum computation and the actual write.
1732 *
1733 * If we are cleaning, check indirect blocks for UNWRITTEN, and if
1734 * there are any, replace them with copies that have UNASSIGNED
1735 * instead.
1736 */
1737 for (bpp = sp->bpp, i = nblocks - 1; i--;) {
1738 ++bpp;
1739 bp = *bpp;
1740 if (bp->b_flags & B_CALL) { /* UBC or malloced buffer */
1741 bp->b_flags |= B_BUSY;
1742 continue;
1743 }
1744 again:
1745 s = splbio();
1746 if (bp->b_flags & B_BUSY) {
1747 #ifdef DEBUG
1748 printf("lfs_writeseg: avoiding potential data summary "
1749 "corruption for ino %d, lbn %" PRId64 "\n",
1750 VTOI(bp->b_vp)->i_number, bp->b_lblkno);
1751 #endif
1752 bp->b_flags |= B_WANTED;
1753 tsleep(bp, (PRIBIO + 1), "lfs_writeseg", 0);
1754 splx(s);
1755 goto again;
1756 }
1757 bp->b_flags |= B_BUSY;
1758 splx(s);
1759 /*
1760 * Check and replace indirect block UNWRITTEN bogosity.
1761 * XXX See comment in lfs_writefile.
1762 */
1763 if (bp->b_lblkno < 0 && bp->b_vp != devvp && bp->b_vp &&
1764 VTOI(bp->b_vp)->i_ffs1_blocks !=
1765 VTOI(bp->b_vp)->i_lfs_effnblks) {
1766 #ifdef DEBUG_LFS
1767 printf("lfs_writeseg: cleansing ino %d (%d != %d)\n",
1768 VTOI(bp->b_vp)->i_number,
1769 VTOI(bp->b_vp)->i_lfs_effnblks,
1770 VTOI(bp->b_vp)->i_ffs1_blocks);
1771 #endif
1772 /* Make a copy we'll make changes to */
1773 newbp = lfs_newbuf(fs, bp->b_vp, bp->b_lblkno,
1774 bp->b_bcount, LFS_NB_IBLOCK);
1775 newbp->b_blkno = bp->b_blkno;
1776 memcpy(newbp->b_data, bp->b_data,
1777 newbp->b_bcount);
1778
1779 changed = 0;
1780 /* XXX ondisk32 */
1781 for (daddrp = (int32_t *)(newbp->b_data);
1782 daddrp < (int32_t *)(newbp->b_data +
1783 newbp->b_bcount); daddrp++) {
1784 if (*daddrp == UNWRITTEN) {
1785 #ifdef DEBUG_LFS
1786 off_t doff;
1787 int32_t ioff;
1788
1789 ioff = daddrp - (int32_t *)(newbp->b_data);
1790 doff = (-bp->b_lblkno + ioff) * fs->lfs_bsize;
1791 printf("ino %d lbn %" PRId64 " entry %d off %" PRIx64 "\n",
1792 VTOI(bp->b_vp)->i_number,
1793 bp->b_lblkno, ioff, doff);
1794 if (bp->b_vp->v_type == VREG) {
1795 /*
1796 * What is up with this page?
1797 */
1798 struct vm_page *pg;
1799 for (; doff / fs->lfs_bsize == (-bp->b_lblkno + ioff); doff += PAGE_SIZE) {
1800 pg = uvm_pagelookup(&bp->b_vp->v_uobj, doff);
1801 if (pg == NULL)
1802 printf(" page at %" PRIx64 " is NULL\n", doff);
1803 else
1804 printf(" page at %" PRIx64 " flags 0x%x pqflags 0x%x\n", doff, pg->flags, pg->pqflags);
1805 }
1806 }
1807 #endif /* DEBUG_LFS */
1808 ++changed;
1809 *daddrp = 0;
1810 }
1811 }
1812 /*
1813 * Get rid of the old buffer. Don't mark it clean,
1814 * though, if it still has dirty data on it.
1815 */
1816 if (changed) {
1817 #ifdef DEBUG_LFS
1818 printf("lfs_writeseg: replacing UNWRITTEN(%d):"
1819 " bp = %p newbp = %p\n", changed, bp,
1820 newbp);
1821 #endif
1822 *bpp = newbp;
1823 bp->b_flags &= ~(B_ERROR | B_GATHERED);
1824 if (bp->b_flags & B_CALL) {
1825 printf("lfs_writeseg: indir bp should not be B_CALL\n");
1826 s = splbio();
1827 biodone(bp);
1828 splx(s);
1829 bp = NULL;
1830 } else {
1831 /* Still on free list, leave it there */
1832 s = splbio();
1833 bp->b_flags &= ~B_BUSY;
1834 if (bp->b_flags & B_WANTED)
1835 wakeup(bp);
1836 splx(s);
1837 /*
1838 * We have to re-decrement lfs_avail
1839 * since this block is going to come
1840 * back around to us in the next
1841 * segment.
1842 */
1843 fs->lfs_avail -= btofsb(fs, bp->b_bcount);
1844 }
1845 } else {
1846 lfs_freebuf(fs, newbp);
1847 }
1848 }
1849 }
1850 /*
1851 * Compute checksum across data and then across summary; the first
1852 * block (the summary block) is skipped. Set the create time here
1853 * so that it's guaranteed to be later than the inode mod times.
1854 *
1855 * XXX
1856 * Fix this to do it inline, instead of malloc/copy.
1857 */
1858 datap = dp = pool_get(&fs->lfs_bpppool, PR_WAITOK);
1859 if (fs->lfs_version == 1)
1860 el_size = sizeof(u_long);
1861 else
1862 el_size = sizeof(u_int32_t);
1863 for (bpp = sp->bpp, i = nblocks - 1; i--; ) {
1864 ++bpp;
1865 /* Loop through gop_write cluster blocks */
1866 for (byteoffset = 0; byteoffset < (*bpp)->b_bcount;
1867 byteoffset += fs->lfs_bsize) {
1868 #ifdef LFS_USE_B_INVAL
1869 if (((*bpp)->b_flags & (B_CALL | B_INVAL)) ==
1870 (B_CALL | B_INVAL)) {
1871 if (copyin((caddr_t)(*bpp)->b_saveaddr +
1872 byteoffset, dp, el_size)) {
1873 panic("lfs_writeseg: copyin failed [1]: "
1874 "ino %d blk %" PRId64,
1875 VTOI((*bpp)->b_vp)->i_number,
1876 (*bpp)->b_lblkno);
1877 }
1878 } else
1879 #endif /* LFS_USE_B_INVAL */
1880 {
1881 memcpy(dp, (*bpp)->b_data + byteoffset,
1882 el_size);
1883 }
1884 dp += el_size;
1885 }
1886 }
1887 if (fs->lfs_version == 1)
1888 ssp->ss_ocreate = time.tv_sec;
1889 else {
1890 ssp->ss_create = time.tv_sec;
1891 ssp->ss_serial = ++fs->lfs_serial;
1892 ssp->ss_ident = fs->lfs_ident;
1893 }
1894 ssp->ss_datasum = cksum(datap, dp - datap);
1895 ssp->ss_sumsum =
1896 cksum(&ssp->ss_datasum, fs->lfs_sumsize - sizeof(ssp->ss_sumsum));
1897 pool_put(&fs->lfs_bpppool, datap);
1898 datap = dp = NULL;
1899 #ifdef DIAGNOSTIC
1900 if (fs->lfs_bfree < btofsb(fs, ninos * fs->lfs_ibsize) + btofsb(fs, fs->lfs_sumsize))
1901 panic("lfs_writeseg: No diskspace for summary");
1902 #endif
1903 fs->lfs_bfree -= (btofsb(fs, ninos * fs->lfs_ibsize) +
1904 btofsb(fs, fs->lfs_sumsize));
1905
1906 strategy = devvp->v_op[VOFFSET(vop_strategy)];
1907
1908 /*
1909 * When we simply write the blocks we lose a rotation for every block
1910 * written. To avoid this problem, we cluster the buffers into a
1911 * chunk and write the chunk. MAXPHYS is the largest size I/O
1912 * devices can handle, use that for the size of the chunks.
1913 *
1914 * Blocks that are already clusters (from GOP_WRITE), however, we
1915 * don't bother to copy into other clusters.
1916 */
1917
1918 #define CHUNKSIZE MAXPHYS
1919
1920 if (devvp == NULL)
1921 panic("devvp is NULL");
1922 for (bpp = sp->bpp, i = nblocks; i;) {
1923 cbp = lfs_newclusterbuf(fs, devvp, (*bpp)->b_blkno, i);
1924 cl = (struct lfs_cluster *)cbp->b_saveaddr;
1925
1926 cbp->b_dev = i_dev;
1927 cbp->b_flags |= B_ASYNC | B_BUSY;
1928 cbp->b_bcount = 0;
1929
1930 cl->olddata = cbp->b_data;
1931 #if defined(DEBUG) && defined(DIAGNOSTIC)
1932 if (bpp - sp->bpp > (fs->lfs_sumsize - SEGSUM_SIZE(fs))
1933 / sizeof(int32_t)) {
1934 panic("lfs_writeseg: real bpp overwrite");
1935 }
1936 if (bpp - sp->bpp > fs->lfs_ssize / fs->lfs_fsize) {
1937 panic("lfs_writeseg: theoretical bpp overwrite");
1938 }
1939 #endif
1940
1941 /*
1942 * Construct the cluster.
1943 */
1944 ++fs->lfs_iocount;
1945 while (i && cbp->b_bcount < CHUNKSIZE) {
1946 bp = *bpp;
1947
1948 if (bp->b_bcount > (CHUNKSIZE - cbp->b_bcount))
1949 break;
1950 if (cbp->b_bcount > 0 && !(cl->flags & LFS_CL_MALLOC))
1951 break;
1952
1953 /* Clusters from GOP_WRITE are expedited */
1954 if (bp->b_bcount > fs->lfs_bsize) {
1955 if (cbp->b_bcount > 0)
1956 /* Put in its own buffer */
1957 break;
1958 else {
1959 cbp->b_data = bp->b_data;
1960 }
1961 } else if (cbp->b_bcount == 0) {
1962 p = cbp->b_data = lfs_malloc(fs, CHUNKSIZE,
1963 LFS_NB_CLUSTER);
1964 cl->flags |= LFS_CL_MALLOC;
1965 }
1966 #ifdef DIAGNOSTIC
1967 if (dtosn(fs, dbtofsb(fs, bp->b_blkno +
1968 btodb(bp->b_bcount - 1))) !=
1969 sp->seg_number) {
1970 printf("blk size %ld daddr %" PRIx64 " not in seg %d\n",
1971 bp->b_bcount, bp->b_blkno,
1972 sp->seg_number);
1973 panic("segment overwrite");
1974 }
1975 #endif
1976
1977 #ifdef LFS_USE_B_INVAL
1978 /*
1979 * Fake buffers from the cleaner are marked as B_INVAL.
1980 * We need to copy the data from user space rather than
1981 * from the buffer indicated.
1982 * XXX == what do I do on an error?
1983 */
1984 if ((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)) {
1985 if (copyin(bp->b_saveaddr, p, bp->b_bcount))
1986 panic("lfs_writeseg: copyin failed [2]");
1987 } else
1988 #endif /* LFS_USE_B_INVAL */
1989 if (cl->flags & LFS_CL_MALLOC) {
1990 bcopy(bp->b_data, p, bp->b_bcount);
1991 }
1992
1993 p += bp->b_bcount;
1994 cbp->b_bcount += bp->b_bcount;
1995 cl->bufsize += bp->b_bcount;
1996
1997 bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI | B_DONE);
1998 cl->bpp[cl->bufcount++] = bp;
1999 vp = bp->b_vp;
2000 s = splbio();
2001 V_INCR_NUMOUTPUT(vp);
2002 splx(s);
2003
2004 bpp++;
2005 i--;
2006 }
2007 s = splbio();
2008 V_INCR_NUMOUTPUT(devvp);
2009 splx(s);
2010 vop_strategy_a.a_desc = VDESC(vop_strategy);
2011 vop_strategy_a.a_bp = cbp;
2012 (strategy)(&vop_strategy_a);
2013 curproc->p_stats->p_ru.ru_oublock++;
2014 }
2015
2016 if (lfs_dostats) {
2017 ++lfs_stats.psegwrites;
2018 lfs_stats.blocktot += nblocks - 1;
2019 if (fs->lfs_sp->seg_flags & SEGM_SYNC)
2020 ++lfs_stats.psyncwrites;
2021 if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
2022 ++lfs_stats.pcleanwrites;
2023 lfs_stats.cleanblocks += nblocks - 1;
2024 }
2025 }
2026 return (lfs_initseg(fs) || do_again);
2027 }
2028
2029 void
2030 lfs_writesuper(struct lfs *fs, daddr_t daddr)
2031 {
2032 struct buf *bp;
2033 dev_t i_dev;
2034 int (*strategy)(void *);
2035 int s;
2036 struct vop_strategy_args vop_strategy_a;
2037
2038 /*
2039 * If we can write one superblock while another is in
2040 * progress, we risk not having a complete checkpoint if we crash.
2041 * So, block here if a superblock write is in progress.
2042 */
2043 s = splbio();
2044 while (fs->lfs_sbactive) {
2045 tsleep(&fs->lfs_sbactive, PRIBIO+1, "lfs sb", 0);
2046 }
2047 fs->lfs_sbactive = daddr;
2048 splx(s);
2049 i_dev = VTOI(fs->lfs_ivnode)->i_dev;
2050 strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
2051
2052 /* Set timestamp of this version of the superblock */
2053 if (fs->lfs_version == 1)
2054 fs->lfs_otstamp = time.tv_sec;
2055 fs->lfs_tstamp = time.tv_sec;
2056
2057 /* Checksum the superblock and copy it into a buffer. */
2058 fs->lfs_cksum = lfs_sb_cksum(&(fs->lfs_dlfs));
2059 bp = lfs_newbuf(fs, VTOI(fs->lfs_ivnode)->i_devvp, fsbtodb(fs, daddr), LFS_SBPAD, LFS_NB_SBLOCK);
2060 memset(bp->b_data + sizeof(struct dlfs), 0, LFS_SBPAD - sizeof(struct dlfs));
2061 *(struct dlfs *)bp->b_data = fs->lfs_dlfs;
2062
2063 bp->b_dev = i_dev;
2064 bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
2065 bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
2066 bp->b_iodone = lfs_supercallback;
2067 /* XXX KS - same nasty hack as above */
2068 bp->b_saveaddr = (caddr_t)fs;
2069
2070 vop_strategy_a.a_desc = VDESC(vop_strategy);
2071 vop_strategy_a.a_bp = bp;
2072 curproc->p_stats->p_ru.ru_oublock++;
2073 s = splbio();
2074 V_INCR_NUMOUTPUT(bp->b_vp);
2075 splx(s);
2076 ++fs->lfs_iocount;
2077 (strategy)(&vop_strategy_a);
2078 }
2079
2080 /*
2081 * Logical block number match routines used when traversing the dirty block
2082 * chain.
2083 */
2084 int
2085 lfs_match_fake(struct lfs *fs, struct buf *bp)
2086 {
2087 return LFS_IS_MALLOC_BUF(bp);
2088 }
2089
2090 #if 0
2091 int
2092 lfs_match_real(struct lfs *fs, struct buf *bp)
2093 {
2094 return (lfs_match_data(fs, bp) && !lfs_match_fake(fs, bp));
2095 }
2096 #endif
2097
2098 int
2099 lfs_match_data(struct lfs *fs, struct buf *bp)
2100 {
2101 return (bp->b_lblkno >= 0);
2102 }
2103
2104 int
2105 lfs_match_indir(struct lfs *fs, struct buf *bp)
2106 {
2107 daddr_t lbn;
2108
2109 lbn = bp->b_lblkno;
2110 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 0);
2111 }
2112
2113 int
2114 lfs_match_dindir(struct lfs *fs, struct buf *bp)
2115 {
2116 daddr_t lbn;
2117
2118 lbn = bp->b_lblkno;
2119 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 1);
2120 }
2121
2122 int
2123 lfs_match_tindir(struct lfs *fs, struct buf *bp)
2124 {
2125 daddr_t lbn;
2126
2127 lbn = bp->b_lblkno;
2128 return (lbn < 0 && (-lbn - NDADDR) % NINDIR(fs) == 2);
2129 }
2130
2131 /*
2132 * XXX - The only buffers that are going to hit these functions are the
2133 * segment write blocks, or the segment summaries, or the superblocks.
2134 *
2135 * All of the above are created by lfs_newbuf, and so do not need to be
2136 * released via brelse.
2137 */
2138 void
2139 lfs_callback(struct buf *bp)
2140 {
2141 struct lfs *fs;
2142
2143 fs = (struct lfs *)bp->b_saveaddr;
2144 lfs_freebuf(fs, bp);
2145 }
2146
2147 static void
2148 lfs_super_aiodone(struct buf *bp)
2149 {
2150 struct lfs *fs;
2151
2152 fs = (struct lfs *)bp->b_saveaddr;
2153 fs->lfs_sbactive = 0;
2154 wakeup(&fs->lfs_sbactive);
2155 if (--fs->lfs_iocount <= 1)
2156 wakeup(&fs->lfs_iocount);
2157 lfs_freebuf(fs, bp);
2158 }
2159
2160 static void
2161 lfs_cluster_aiodone(struct buf *bp)
2162 {
2163 struct lfs_cluster *cl;
2164 struct lfs *fs;
2165 struct buf *tbp, *fbp;
2166 struct vnode *vp, *devvp;
2167 struct inode *ip;
2168 int s, error=0;
2169 char *cp;
2170 extern int locked_queue_count;
2171 extern long locked_queue_bytes;
2172
2173 if (bp->b_flags & B_ERROR)
2174 error = bp->b_error;
2175
2176 cl = (struct lfs_cluster *)bp->b_saveaddr;
2177 fs = cl->fs;
2178 devvp = VTOI(fs->lfs_ivnode)->i_devvp;
2179 bp->b_saveaddr = cl->saveaddr;
2180
2181 cp = (char *)bp->b_data + cl->bufsize;
2182 /* Put the pages back, and release the buffer */
2183 while (cl->bufcount--) {
2184 tbp = cl->bpp[cl->bufcount];
2185 if (error) {
2186 tbp->b_flags |= B_ERROR;
2187 tbp->b_error = error;
2188 }
2189
2190 /*
2191 * We're done with tbp. If it has not been re-dirtied since
2192 * the cluster was written, free it. Otherwise, keep it on
2193 * the locked list to be written again.
2194 */
2195 vp = tbp->b_vp;
2196
2197 if ((tbp->b_flags & (B_LOCKED | B_DELWRI)) == B_LOCKED)
2198 LFS_UNLOCK_BUF(tbp);
2199
2200 tbp->b_flags &= ~B_GATHERED;
2201
2202 LFS_BCLEAN_LOG(fs, tbp);
2203
2204 if (!(tbp->b_flags & B_CALL)) {
2205 bremfree(tbp);
2206 s = splbio();
2207 if (vp)
2208 reassignbuf(tbp, vp);
2209 splx(s);
2210 tbp->b_flags |= B_ASYNC; /* for biodone */
2211 }
2212 #ifdef DIAGNOSTIC
2213 if (tbp->b_flags & B_DONE) {
2214 printf("blk %d biodone already (flags %lx)\n",
2215 cl->bufcount, (long)tbp->b_flags);
2216 }
2217 #endif
2218 if (tbp->b_flags & (B_BUSY | B_CALL)) {
2219 if ((tbp->b_flags & B_CALL) && !LFS_IS_MALLOC_BUF(tbp)) {
2220 /* printf("flags 0x%lx\n", tbp->b_flags); */
2221 /*
2222 * A buffer from the page daemon.
2223 * We use the same iodone as it does,
2224 * so we must manually disassociate its
2225 * buffers from the vp.
2226 */
2227 if (tbp->b_vp) {
2228 /* This is just silly */
2229 s = splbio();
2230 brelvp(tbp);
2231 tbp->b_vp = vp;
2232 splx(s);
2233 }
2234 /* Put it back the way it was */
2235 tbp->b_flags |= B_ASYNC;
2236 /* Master buffers have B_AGE */
2237 if (tbp->b_private == tbp)
2238 tbp->b_flags |= B_AGE;
2239 }
2240 s = splbio();
2241 biodone(tbp);
2242
2243 /*
2244 * If this is the last block for this vnode, but
2245 * there are other blocks on its dirty list,
2246 * set IN_MODIFIED/IN_CLEANING depending on what
2247 * sort of block. Only do this for our mount point,
2248 * not for, e.g., inode blocks that are attached to
2249 * the devvp.
2250 * XXX KS - Shouldn't we set *both* if both types
2251 * of blocks are present (traverse the dirty list?)
2252 */
2253 simple_lock(&global_v_numoutput_slock);
2254 if (vp != devvp && vp->v_numoutput == 0 &&
2255 (fbp = LIST_FIRST(&vp->v_dirtyblkhd)) != NULL) {
2256 ip = VTOI(vp);
2257 #ifdef DEBUG_LFS
2258 printf("lfs_cluster_aiodone: marking ino %d\n",
2259 ip->i_number);
2260 #endif
2261 if (LFS_IS_MALLOC_BUF(fbp))
2262 LFS_SET_UINO(ip, IN_CLEANING);
2263 else
2264 LFS_SET_UINO(ip, IN_MODIFIED);
2265 }
2266 simple_unlock(&global_v_numoutput_slock);
2267 splx(s);
2268 wakeup(vp);
2269 }
2270 }
2271
2272 /* Fix up the cluster buffer, and release it */
2273 if (cl->flags & LFS_CL_MALLOC)
2274 lfs_free(fs, bp->b_data, LFS_NB_CLUSTER);
2275 bp->b_data = cl->olddata;
2276 bp->b_bcount = 0;
2277 bp->b_iodone = NULL;
2278 bp->b_flags &= ~B_DELWRI;
2279 bp->b_flags |= B_DONE;
2280 s = splbio();
2281 reassignbuf(bp, bp->b_vp);
2282 splx(s);
2283 brelse(bp);
2284
2285 /* Note i/o done */
2286 if (cl->flags & LFS_CL_SYNC) {
2287 if (--cl->seg->seg_iocount == 0)
2288 wakeup(&cl->seg->seg_iocount);
2289 /* printf("- %x => %d\n", cl->seg, cl->seg->seg_iocount); */
2290 }
2291 #ifdef DIAGNOSTIC
2292 if (fs->lfs_iocount == 0)
2293 panic("lfs_cluster_aiodone: zero iocount");
2294 #endif
2295 if (--fs->lfs_iocount <= 1)
2296 wakeup(&fs->lfs_iocount);
2297
2298 pool_put(&fs->lfs_bpppool, cl->bpp);
2299 cl->bpp = NULL;
2300 pool_put(&fs->lfs_clpool, cl);
2301 }
2302
2303 static void
2304 lfs_generic_callback(struct buf *bp, void (*aiodone)(struct buf *))
2305 {
2306 /* reset b_iodone for when this is a single-buf i/o. */
2307 bp->b_iodone = aiodone;
2308
2309 simple_lock(&uvm.aiodoned_lock); /* locks uvm.aio_done */
2310 TAILQ_INSERT_TAIL(&uvm.aio_done, bp, b_freelist);
2311 wakeup(&uvm.aiodoned);
2312 simple_unlock(&uvm.aiodoned_lock);
2313 }
2314
2315 static void
2316 lfs_cluster_callback(struct buf *bp)
2317 {
2318 lfs_generic_callback(bp, lfs_cluster_aiodone);
2319 }
2320
2321 void
2322 lfs_supercallback(struct buf *bp)
2323 {
2324 lfs_generic_callback(bp, lfs_super_aiodone);
2325 }
2326
2327 /*
2328 * Shellsort (diminishing increment sort) from Data Structures and
2329 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
2330 * see also Knuth Vol. 3, page 84. The increments are selected from
2331 * formula (8), page 95. Roughly O(N^3/2).
2332 */
2333 /*
2334 * This is our own private copy of shellsort because we want to sort
2335 * two parallel arrays (the array of buffer pointers and the array of
2336 * logical block numbers) simultaneously. Note that we cast the array
2337 * of logical block numbers to a unsigned in this routine so that the
2338 * negative block numbers (meta data blocks) sort AFTER the data blocks.
2339 */
2340
2341 void
2342 lfs_shellsort(struct buf **bp_array, int32_t *lb_array, int nmemb, int size)
2343 {
2344 static int __rsshell_increments[] = { 4, 1, 0 };
2345 int incr, *incrp, t1, t2;
2346 struct buf *bp_temp;
2347
2348 #ifdef DEBUG
2349 incr = 0;
2350 for (t1 = 0; t1 < nmemb; t1++) {
2351 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
2352 if (lb_array[incr++] != bp_array[t1]->b_lblkno + t2) {
2353 /* dump before panic */
2354 printf("lfs_shellsort: nmemb=%d, size=%d\n",
2355 nmemb, size);
2356 incr = 0;
2357 for (t1 = 0; t1 < nmemb; t1++) {
2358 const struct buf *bp = bp_array[t1];
2359
2360 printf("bp[%d]: lbn=%" PRIu64 ", size=%"
2361 PRIu64 "\n", t1,
2362 (uint64_t)bp->b_bcount,
2363 (uint64_t)bp->b_lblkno);
2364 printf("lbns:");
2365 for (t2 = 0; t2 * size < bp->b_bcount;
2366 t2++) {
2367 printf(" %" PRId32,
2368 lb_array[incr++]);
2369 }
2370 printf("\n");
2371 }
2372 panic("lfs_shellsort: inconsistent input");
2373 }
2374 }
2375 }
2376 #endif
2377
2378 for (incrp = __rsshell_increments; (incr = *incrp++) != 0;)
2379 for (t1 = incr; t1 < nmemb; ++t1)
2380 for (t2 = t1 - incr; t2 >= 0;)
2381 if ((u_int32_t)bp_array[t2]->b_lblkno >
2382 (u_int32_t)bp_array[t2 + incr]->b_lblkno) {
2383 bp_temp = bp_array[t2];
2384 bp_array[t2] = bp_array[t2 + incr];
2385 bp_array[t2 + incr] = bp_temp;
2386 t2 -= incr;
2387 } else
2388 break;
2389
2390 /* Reform the list of logical blocks */
2391 incr = 0;
2392 for (t1 = 0; t1 < nmemb; t1++) {
2393 for (t2 = 0; t2 * size < bp_array[t1]->b_bcount; t2++) {
2394 lb_array[incr++] = bp_array[t1]->b_lblkno + t2;
2395 }
2396 }
2397 }
2398
2399 /*
2400 * Check VXLOCK. Return 1 if the vnode is locked. Otherwise, vget it.
2401 */
2402 int
2403 lfs_vref(struct vnode *vp)
2404 {
2405 /*
2406 * If we return 1 here during a flush, we risk vinvalbuf() not
2407 * being able to flush all of the pages from this vnode, which
2408 * will cause it to panic. So, return 0 if a flush is in progress.
2409 */
2410 if (vp->v_flag & VXLOCK) {
2411 if (IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
2412 return 0;
2413 }
2414 return (1);
2415 }
2416 return (vget(vp, 0));
2417 }
2418
2419 /*
2420 * This is vrele except that we do not want to VOP_INACTIVE this vnode. We
2421 * inline vrele here to avoid the vn_lock and VOP_INACTIVE call at the end.
2422 */
2423 void
2424 lfs_vunref(struct vnode *vp)
2425 {
2426 /*
2427 * Analogous to lfs_vref, if the node is flushing, fake it.
2428 */
2429 if ((vp->v_flag & VXLOCK) && IS_FLUSHING(VTOI(vp)->i_lfs,vp)) {
2430 return;
2431 }
2432
2433 simple_lock(&vp->v_interlock);
2434 #ifdef DIAGNOSTIC
2435 if (vp->v_usecount <= 0) {
2436 printf("lfs_vunref: inum is %d\n", VTOI(vp)->i_number);
2437 printf("lfs_vunref: flags are 0x%lx\n", (u_long)vp->v_flag);
2438 printf("lfs_vunref: usecount = %ld\n", (long)vp->v_usecount);
2439 panic("lfs_vunref: v_usecount<0");
2440 }
2441 #endif
2442 vp->v_usecount--;
2443 if (vp->v_usecount > 0) {
2444 simple_unlock(&vp->v_interlock);
2445 return;
2446 }
2447 /*
2448 * insert at tail of LRU list
2449 */
2450 simple_lock(&vnode_free_list_slock);
2451 if (vp->v_holdcnt > 0)
2452 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
2453 else
2454 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2455 simple_unlock(&vnode_free_list_slock);
2456 simple_unlock(&vp->v_interlock);
2457 }
2458
2459 /*
2460 * We use this when we have vnodes that were loaded in solely for cleaning.
2461 * There is no reason to believe that these vnodes will be referenced again
2462 * soon, since the cleaning process is unrelated to normal filesystem
2463 * activity. Putting cleaned vnodes at the tail of the list has the effect
2464 * of flushing the vnode LRU. So, put vnodes that were loaded only for
2465 * cleaning at the head of the list, instead.
2466 */
2467 void
2468 lfs_vunref_head(struct vnode *vp)
2469 {
2470 simple_lock(&vp->v_interlock);
2471 #ifdef DIAGNOSTIC
2472 if (vp->v_usecount == 0) {
2473 panic("lfs_vunref: v_usecount<0");
2474 }
2475 #endif
2476 vp->v_usecount--;
2477 if (vp->v_usecount > 0) {
2478 simple_unlock(&vp->v_interlock);
2479 return;
2480 }
2481 /*
2482 * insert at head of LRU list
2483 */
2484 simple_lock(&vnode_free_list_slock);
2485 if (vp->v_holdcnt > 0)
2486 TAILQ_INSERT_TAIL(&vnode_hold_list, vp, v_freelist);
2487 else
2488 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2489 simple_unlock(&vnode_free_list_slock);
2490 simple_unlock(&vp->v_interlock);
2491 }
2492
2493