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